Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit cac1a8e

Browse files
committed
[Impeller Scene] Use std::chrono for animation durations
1 parent adda2e8 commit cac1a8e

File tree

8 files changed

+68
-43
lines changed

8 files changed

+68
-43
lines changed

impeller/scene/animation/animation.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ const std::vector<Animation::Channel>& Animation::GetChannels() const {
117117
return channels_;
118118
}
119119

120-
Scalar Animation::GetEndTime() const {
120+
std::chrono::duration<Scalar> Animation::GetEndTime() const {
121121
return end_time_;
122122
}
123123

impeller/scene/animation/animation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ class Animation final {
6060

6161
const std::vector<Channel>& GetChannels() const;
6262

63-
Scalar GetEndTime() const;
63+
std::chrono::duration<Scalar> GetEndTime() const;
6464

6565
private:
6666
Animation();
6767

6868
std::string name_;
6969
std::vector<Channel> channels_;
70-
Scalar end_time_ = 0;
70+
std::chrono::duration<Scalar> end_time_;
7171

7272
FML_DISALLOW_COPY_AND_ASSIGN(Animation);
7373
};

impeller/scene/animation/animation_clip.cc

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "impeller/scene/animation/animation_clip.h"
66

77
#include <algorithm>
8+
#include <chrono>
89
#include <cmath>
910
#include <memory>
1011
#include <valarray>
@@ -43,7 +44,7 @@ void AnimationClip::Pause() {
4344

4445
void AnimationClip::Stop() {
4546
SetPlaying(false);
46-
Seek(0);
47+
Seek(std::chrono::duration<Scalar>::zero());
4748
}
4849

4950
bool AnimationClip::GetLoop() const {
@@ -70,39 +71,46 @@ void AnimationClip::SetWeight(Scalar weight) {
7071
weight_ = weight;
7172
}
7273

73-
Scalar AnimationClip::GetPlaybackTime() const {
74+
std::chrono::duration<Scalar> AnimationClip::GetPlaybackTime() const {
7475
return playback_time_;
7576
}
7677

77-
void AnimationClip::Seek(Scalar time) {
78-
playback_time_ = std::clamp(time, 0.0f, animation_->GetEndTime());
78+
void AnimationClip::Seek(std::chrono::duration<Scalar> time) {
79+
playback_time_ = std::clamp(time, std::chrono::duration<Scalar>::zero(),
80+
animation_->GetEndTime());
7981
}
8082

81-
void AnimationClip::Advance(Scalar delta_time) {
82-
if (!playing_ || delta_time <= 0) {
83+
void AnimationClip::Advance(std::chrono::duration<Scalar> delta_time) {
84+
if (!playing_ || delta_time <= std::chrono::duration<Scalar>::zero()) {
8385
return;
8486
}
8587
delta_time *= playback_time_scale_;
8688
playback_time_ += delta_time;
8789

8890
/// Handle looping behavior.
8991

90-
Scalar end_time = animation_->GetEndTime();
91-
if (end_time == 0) {
92-
playback_time_ = 0;
92+
auto end_time = animation_->GetEndTime();
93+
if (end_time == std::chrono::duration<Scalar>::zero()) {
94+
playback_time_ = std::chrono::duration<Scalar>::zero();
9395
return;
9496
}
95-
if (!loop_ && (playback_time_ < 0 || playback_time_ > end_time)) {
97+
if (!loop_ && (playback_time_ < std::chrono::duration<Scalar>::zero() ||
98+
playback_time_ > end_time)) {
9699
// If looping is disabled, clamp to the end (or beginning, if playing in
97100
// reverse) and pause.
98101
Pause();
99-
playback_time_ = std::clamp(playback_time_, 0.0f, end_time);
102+
playback_time_ = std::clamp(
103+
playback_time_, std::chrono::duration<Scalar>::zero(), end_time);
100104
} else if (/* loop && */ playback_time_ > end_time) {
101105
// If looping is enabled and we ran off the end, loop to the beginning.
102-
playback_time_ = std::fmod(std::abs(playback_time_), end_time);
103-
} else if (/* loop && */ playback_time_ < 0) {
106+
playback_time_ = std::chrono::duration<Scalar>(
107+
std::fmod(std::abs(playback_time_.count()), end_time.count()));
108+
} else if (/* loop && */ playback_time_ <
109+
std::chrono::duration<Scalar>::zero()) {
104110
// If looping is enabled and we ran off the beginning, loop to the end.
105-
playback_time_ = end_time - std::fmod(std::abs(playback_time_), end_time);
111+
playback_time_ =
112+
end_time - std::chrono::duration<Scalar>(std::fmod(
113+
std::abs(playback_time_.count()), end_time.count()));
106114
}
107115
}
108116

impeller/scene/animation/animation_clip.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ class AnimationClip final {
4949
void SetWeight(Scalar weight);
5050

5151
/// @brief Get the current playback time of the animation.
52-
Scalar GetPlaybackTime() const;
52+
std::chrono::duration<Scalar> GetPlaybackTime() const;
5353

5454
/// @brief Move the animation to the specified time. The given `time` is
5555
/// clamped to the animation's playback range.
56-
void Seek(Scalar time);
56+
void Seek(std::chrono::duration<Scalar> time);
5757

5858
/// @brief Advance the animation by `delta_time` seconds. Negative
5959
/// `delta_time` values do nothing.
60-
void Advance(Scalar delta_time);
60+
void Advance(std::chrono::duration<Scalar> delta_time);
6161

6262
/// @brief Applies the animation to all binded properties in the scene.
6363
void ApplyToBindings() const;
@@ -73,7 +73,7 @@ class AnimationClip final {
7373
std::shared_ptr<Animation> animation_;
7474
std::vector<ChannelBinding> bindings_;
7575

76-
Scalar playback_time_ = 0;
76+
std::chrono::duration<Scalar> playback_time_;
7777
Scalar playback_time_scale_ = 1; // Seconds multiplier, can be negative.
7878
Scalar weight_ = 1;
7979
bool playing_ = false;

impeller/scene/animation/animation_player.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "impeller/scene/animation/animation_player.h"
66

7+
#include <chrono>
78
#include <memory>
89

910
#include "flutter/fml/time/time_point.h"
@@ -36,10 +37,10 @@ AnimationClip& AnimationPlayer::AddAnimation(
3637

3738
void AnimationPlayer::Update() {
3839
if (!previous_time_.has_value()) {
39-
previous_time_ = fml::TimePoint::Now().ToEpochDelta();
40+
previous_time_ = std::chrono::high_resolution_clock::now();
4041
}
41-
auto new_time = fml::TimePoint::Now().ToEpochDelta();
42-
Scalar delta_time = (new_time - previous_time_.value()).ToSecondsF();
42+
auto new_time = std::chrono::high_resolution_clock::now();
43+
auto delta_time = new_time - previous_time_.value();
4344
previous_time_ = new_time;
4445

4546
Reset();

impeller/scene/animation/animation_player.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#pragma once
66

7+
#include <chrono>
78
#include <memory>
89
#include <optional>
910
#include <unordered_set>
@@ -42,7 +43,8 @@ class AnimationPlayer final {
4243

4344
std::vector<AnimationClip> clips_;
4445

45-
std::optional<fml::TimeDelta> previous_time_;
46+
std::optional<std::chrono::time_point<std::chrono::high_resolution_clock>>
47+
previous_time_;
4648

4749
FML_DISALLOW_COPY_AND_ASSIGN(AnimationPlayer);
4850
};

impeller/scene/animation/property_resolver.cc

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,35 +51,36 @@ PropertyResolver::~PropertyResolver() = default;
5151

5252
TimelineResolver::~TimelineResolver() = default;
5353

54-
Scalar TimelineResolver::GetEndTime() {
54+
std::chrono::duration<Scalar> TimelineResolver::GetEndTime() {
5555
if (times_.empty()) {
56-
return 0;
56+
return std::chrono::duration<Scalar>::zero();
5757
}
58-
return times_.back();
58+
return std::chrono::duration<Scalar>(times_.back());
5959
}
6060

61-
TimelineResolver::TimelineKey TimelineResolver::GetTimelineKey(Scalar time) {
62-
if (times_.size() <= 1 || time <= times_.front()) {
61+
TimelineResolver::TimelineKey TimelineResolver::GetTimelineKey(
62+
std::chrono::duration<Scalar> time) {
63+
if (times_.size() <= 1 || time.count() <= times_.front()) {
6364
return {.index = 0, .lerp = 1};
6465
}
65-
if (time >= times_.back()) {
66+
if (time.count() >= times_.back()) {
6667
return {.index = times_.size() - 1, .lerp = 1};
6768
}
68-
auto it = std::lower_bound(times_.begin(), times_.end(), time);
69+
auto it = std::lower_bound(times_.begin(), times_.end(), time.count());
6970
size_t index = std::distance(times_.begin(), it);
7071

7172
Scalar previous_time = *(it - 1);
7273
Scalar next_time = *it;
7374
return {.index = index,
74-
.lerp = (time - previous_time) / (next_time - previous_time)};
75+
.lerp = (time.count() - previous_time) / (next_time - previous_time)};
7576
}
7677

7778
TranslationTimelineResolver::TranslationTimelineResolver() = default;
7879

7980
TranslationTimelineResolver::~TranslationTimelineResolver() = default;
8081

8182
void TranslationTimelineResolver::Apply(Node& target,
82-
Scalar time,
83+
std::chrono::duration<Scalar> time,
8384
Scalar weight) {
8485
if (values_.empty()) {
8586
return;
@@ -97,7 +98,9 @@ RotationTimelineResolver::RotationTimelineResolver() = default;
9798

9899
RotationTimelineResolver::~RotationTimelineResolver() = default;
99100

100-
void RotationTimelineResolver::Apply(Node& target, Scalar time, Scalar weight) {
101+
void RotationTimelineResolver::Apply(Node& target,
102+
std::chrono::duration<Scalar> time,
103+
Scalar weight) {
101104
if (values_.empty()) {
102105
return;
103106
}
@@ -114,7 +117,9 @@ ScaleTimelineResolver::ScaleTimelineResolver() = default;
114117

115118
ScaleTimelineResolver::~ScaleTimelineResolver() = default;
116119

117-
void ScaleTimelineResolver::Apply(Node& target, Scalar time, Scalar weight) {
120+
void ScaleTimelineResolver::Apply(Node& target,
121+
std::chrono::duration<Scalar> time,
122+
Scalar weight) {
118123
if (values_.empty()) {
119124
return;
120125
}

impeller/scene/animation/property_resolver.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#pragma once
66

7+
#include <chrono>
78
#include <memory>
89
#include <string>
910
#include <vector>
@@ -38,22 +39,24 @@ class PropertyResolver {
3839

3940
virtual ~PropertyResolver();
4041

41-
virtual Scalar GetEndTime() = 0;
42+
virtual std::chrono::duration<Scalar> GetEndTime() = 0;
4243

4344
/// @brief Resolve and apply the property value to a target node. This
4445
/// operation is additive; a given node property may be amended by
4546
/// many different PropertyResolvers prior to rendering. For example,
4647
/// an AnimationPlayer may blend multiple Animations together by
4748
/// applying several AnimationClips.
48-
virtual void Apply(Node& target, Scalar time, Scalar weight) = 0;
49+
virtual void Apply(Node& target,
50+
std::chrono::duration<Scalar> time,
51+
Scalar weight) = 0;
4952
};
5053

5154
class TimelineResolver : public PropertyResolver {
5255
public:
5356
virtual ~TimelineResolver();
5457

5558
// |Resolver|
56-
Scalar GetEndTime();
59+
std::chrono::duration<Scalar> GetEndTime();
5760

5861
protected:
5962
struct TimelineKey {
@@ -63,7 +66,7 @@ class TimelineResolver : public PropertyResolver {
6366
/// and `timeline_index`. The range of this value should always be `0>N>=1`.
6467
Scalar lerp = 1;
6568
};
66-
TimelineKey GetTimelineKey(Scalar time);
69+
TimelineKey GetTimelineKey(std::chrono::duration<Scalar> time);
6770

6871
std::vector<Scalar> times_;
6972
};
@@ -73,7 +76,9 @@ class TranslationTimelineResolver final : public TimelineResolver {
7376
~TranslationTimelineResolver();
7477

7578
// |Resolver|
76-
void Apply(Node& target, Scalar time, Scalar weight) override;
79+
void Apply(Node& target,
80+
std::chrono::duration<Scalar> time,
81+
Scalar weight) override;
7782

7883
private:
7984
TranslationTimelineResolver();
@@ -90,7 +95,9 @@ class RotationTimelineResolver final : public TimelineResolver {
9095
~RotationTimelineResolver();
9196

9297
// |Resolver|
93-
void Apply(Node& target, Scalar time, Scalar weight) override;
98+
void Apply(Node& target,
99+
std::chrono::duration<Scalar> time,
100+
Scalar weight) override;
94101

95102
private:
96103
RotationTimelineResolver();
@@ -107,7 +114,9 @@ class ScaleTimelineResolver final : public TimelineResolver {
107114
~ScaleTimelineResolver();
108115

109116
// |Resolver|
110-
void Apply(Node& target, Scalar time, Scalar weight) override;
117+
void Apply(Node& target,
118+
std::chrono::duration<Scalar> time,
119+
Scalar weight) override;
111120

112121
private:
113122
ScaleTimelineResolver();

0 commit comments

Comments
 (0)