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

Commit 2213b80

Browse files
authored
[Impeller Scene] Use std::chrono for animation durations (#38606)
1 parent c0b3f8f commit 2213b80

11 files changed

+71
-43
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,7 @@ ORIGIN: ../../../flutter/impeller/base/thread.cc + ../../../flutter/LICENSE
11461146
ORIGIN: ../../../flutter/impeller/base/thread.h + ../../../flutter/LICENSE
11471147
ORIGIN: ../../../flutter/impeller/base/thread_safety.cc + ../../../flutter/LICENSE
11481148
ORIGIN: ../../../flutter/impeller/base/thread_safety.h + ../../../flutter/LICENSE
1149+
ORIGIN: ../../../flutter/impeller/base/timing.h + ../../../flutter/LICENSE
11491150
ORIGIN: ../../../flutter/impeller/base/validation.cc + ../../../flutter/LICENSE
11501151
ORIGIN: ../../../flutter/impeller/base/validation.h + ../../../flutter/LICENSE
11511152
ORIGIN: ../../../flutter/impeller/base/version.cc + ../../../flutter/LICENSE
@@ -3614,6 +3615,7 @@ FILE: ../../../flutter/impeller/base/thread.cc
36143615
FILE: ../../../flutter/impeller/base/thread.h
36153616
FILE: ../../../flutter/impeller/base/thread_safety.cc
36163617
FILE: ../../../flutter/impeller/base/thread_safety.h
3618+
FILE: ../../../flutter/impeller/base/timing.h
36173619
FILE: ../../../flutter/impeller/base/validation.cc
36183620
FILE: ../../../flutter/impeller/base/validation.h
36193621
FILE: ../../../flutter/impeller/base/version.cc

impeller/base/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ impeller_component("base") {
2020
"thread.h",
2121
"thread_safety.cc",
2222
"thread_safety.h",
23+
"timing.h",
2324
"validation.cc",
2425
"validation.h",
2526
"version.cc",

impeller/base/timing.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#pragma once
6+
7+
#include <chrono>
8+
9+
namespace impeller {
10+
11+
using SecondsF = std::chrono::duration<float>;
12+
using Clock = std::chrono::high_resolution_clock;
13+
using TimePoint = std::chrono::time_point<std::chrono::high_resolution_clock>;
14+
15+
} // namespace impeller

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+
SecondsF Animation::GetEndTime() const {
121121
return end_time_;
122122
}
123123

impeller/scene/animation/animation.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "flutter/fml/hash_combine.h"
1212
#include "flutter/fml/macros.h"
13+
#include "impeller/base/timing.h"
1314
#include "impeller/geometry/quaternion.h"
1415
#include "impeller/geometry/scalar.h"
1516
#include "impeller/geometry/vector.h"
@@ -60,14 +61,14 @@ class Animation final {
6061

6162
const std::vector<Channel>& GetChannels() const;
6263

63-
Scalar GetEndTime() const;
64+
SecondsF GetEndTime() const;
6465

6566
private:
6667
Animation();
6768

6869
std::string name_;
6970
std::vector<Channel> channels_;
70-
Scalar end_time_ = 0;
71+
SecondsF end_time_;
7172

7273
FML_DISALLOW_COPY_AND_ASSIGN(Animation);
7374
};

impeller/scene/animation/animation_clip.cc

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ void AnimationClip::Pause() {
4343

4444
void AnimationClip::Stop() {
4545
SetPlaying(false);
46-
Seek(0);
46+
Seek(SecondsF::zero());
4747
}
4848

4949
bool AnimationClip::GetLoop() const {
@@ -70,39 +70,43 @@ void AnimationClip::SetWeight(Scalar weight) {
7070
weight_ = weight;
7171
}
7272

73-
Scalar AnimationClip::GetPlaybackTime() const {
73+
SecondsF AnimationClip::GetPlaybackTime() const {
7474
return playback_time_;
7575
}
7676

77-
void AnimationClip::Seek(Scalar time) {
78-
playback_time_ = std::clamp(time, 0.0f, animation_->GetEndTime());
77+
void AnimationClip::Seek(SecondsF time) {
78+
playback_time_ = std::clamp(time, SecondsF::zero(), animation_->GetEndTime());
7979
}
8080

81-
void AnimationClip::Advance(Scalar delta_time) {
82-
if (!playing_ || delta_time <= 0) {
81+
void AnimationClip::Advance(SecondsF delta_time) {
82+
if (!playing_ || delta_time <= SecondsF::zero()) {
8383
return;
8484
}
8585
delta_time *= playback_time_scale_;
8686
playback_time_ += delta_time;
8787

8888
/// Handle looping behavior.
8989

90-
Scalar end_time = animation_->GetEndTime();
91-
if (end_time == 0) {
92-
playback_time_ = 0;
90+
auto end_time = animation_->GetEndTime();
91+
if (end_time == SecondsF::zero()) {
92+
playback_time_ = SecondsF::zero();
9393
return;
9494
}
95-
if (!loop_ && (playback_time_ < 0 || playback_time_ > end_time)) {
95+
if (!loop_ &&
96+
(playback_time_ < SecondsF::zero() || playback_time_ > end_time)) {
9697
// If looping is disabled, clamp to the end (or beginning, if playing in
9798
// reverse) and pause.
9899
Pause();
99-
playback_time_ = std::clamp(playback_time_, 0.0f, end_time);
100+
playback_time_ = std::clamp(playback_time_, SecondsF::zero(), end_time);
100101
} else if (/* loop && */ playback_time_ > end_time) {
101102
// 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) {
103+
playback_time_ =
104+
SecondsF(std::fmod(std::abs(playback_time_.count()), end_time.count()));
105+
} else if (/* loop && */ playback_time_ < SecondsF::zero()) {
104106
// 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);
107+
playback_time_ =
108+
end_time -
109+
SecondsF(std::fmod(std::abs(playback_time_.count()), end_time.count()));
106110
}
107111
}
108112

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+
SecondsF 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(SecondsF 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(SecondsF 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+
SecondsF 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
@@ -7,6 +7,7 @@
77
#include <memory>
88

99
#include "flutter/fml/time/time_point.h"
10+
#include "impeller/base/timing.h"
1011
#include "impeller/scene/node.h"
1112

1213
namespace impeller {
@@ -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_ = 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 = 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "flutter/fml/hash_combine.h"
1313
#include "flutter/fml/macros.h"
1414
#include "flutter/fml/time/time_delta.h"
15+
#include "impeller/base/timing.h"
1516
#include "impeller/geometry/matrix.h"
1617
#include "impeller/scene/animation/animation_clip.h"
1718

@@ -42,7 +43,7 @@ class AnimationPlayer final {
4243

4344
std::vector<AnimationClip> clips_;
4445

45-
std::optional<fml::TimeDelta> previous_time_;
46+
std::optional<TimePoint> previous_time_;
4647

4748
FML_DISALLOW_COPY_AND_ASSIGN(AnimationPlayer);
4849
};

impeller/scene/animation/property_resolver.cc

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

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

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

61-
TimelineResolver::TimelineKey TimelineResolver::GetTimelineKey(Scalar time) {
62-
if (times_.size() <= 1 || time <= times_.front()) {
61+
TimelineResolver::TimelineKey TimelineResolver::GetTimelineKey(SecondsF time) {
62+
if (times_.size() <= 1 || time.count() <= times_.front()) {
6363
return {.index = 0, .lerp = 1};
6464
}
65-
if (time >= times_.back()) {
65+
if (time.count() >= times_.back()) {
6666
return {.index = times_.size() - 1, .lerp = 1};
6767
}
68-
auto it = std::lower_bound(times_.begin(), times_.end(), time);
68+
auto it = std::lower_bound(times_.begin(), times_.end(), time.count());
6969
size_t index = std::distance(times_.begin(), it);
7070

7171
Scalar previous_time = *(it - 1);
7272
Scalar next_time = *it;
7373
return {.index = index,
74-
.lerp = (time - previous_time) / (next_time - previous_time)};
74+
.lerp = (time.count() - previous_time) / (next_time - previous_time)};
7575
}
7676

7777
TranslationTimelineResolver::TranslationTimelineResolver() = default;
7878

7979
TranslationTimelineResolver::~TranslationTimelineResolver() = default;
8080

8181
void TranslationTimelineResolver::Apply(Node& target,
82-
Scalar time,
82+
SecondsF time,
8383
Scalar weight) {
8484
if (values_.empty()) {
8585
return;
@@ -97,7 +97,9 @@ RotationTimelineResolver::RotationTimelineResolver() = default;
9797

9898
RotationTimelineResolver::~RotationTimelineResolver() = default;
9999

100-
void RotationTimelineResolver::Apply(Node& target, Scalar time, Scalar weight) {
100+
void RotationTimelineResolver::Apply(Node& target,
101+
SecondsF time,
102+
Scalar weight) {
101103
if (values_.empty()) {
102104
return;
103105
}
@@ -114,7 +116,7 @@ ScaleTimelineResolver::ScaleTimelineResolver() = default;
114116

115117
ScaleTimelineResolver::~ScaleTimelineResolver() = default;
116118

117-
void ScaleTimelineResolver::Apply(Node& target, Scalar time, Scalar weight) {
119+
void ScaleTimelineResolver::Apply(Node& target, SecondsF time, Scalar weight) {
118120
if (values_.empty()) {
119121
return;
120122
}

impeller/scene/animation/property_resolver.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "flutter/fml/hash_combine.h"
1212
#include "flutter/fml/macros.h"
13+
#include "impeller/base/timing.h"
1314
#include "impeller/geometry/quaternion.h"
1415
#include "impeller/geometry/scalar.h"
1516
#include "impeller/geometry/vector.h"
@@ -38,22 +39,22 @@ class PropertyResolver {
3839

3940
virtual ~PropertyResolver();
4041

41-
virtual Scalar GetEndTime() = 0;
42+
virtual SecondsF 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, SecondsF time, Scalar weight) = 0;
4950
};
5051

5152
class TimelineResolver : public PropertyResolver {
5253
public:
5354
virtual ~TimelineResolver();
5455

5556
// |Resolver|
56-
Scalar GetEndTime();
57+
SecondsF GetEndTime();
5758

5859
protected:
5960
struct TimelineKey {
@@ -63,7 +64,7 @@ class TimelineResolver : public PropertyResolver {
6364
/// and `timeline_index`. The range of this value should always be `0>N>=1`.
6465
Scalar lerp = 1;
6566
};
66-
TimelineKey GetTimelineKey(Scalar time);
67+
TimelineKey GetTimelineKey(SecondsF time);
6768

6869
std::vector<Scalar> times_;
6970
};
@@ -73,7 +74,7 @@ class TranslationTimelineResolver final : public TimelineResolver {
7374
~TranslationTimelineResolver();
7475

7576
// |Resolver|
76-
void Apply(Node& target, Scalar time, Scalar weight) override;
77+
void Apply(Node& target, SecondsF time, Scalar weight) override;
7778

7879
private:
7980
TranslationTimelineResolver();
@@ -90,7 +91,7 @@ class RotationTimelineResolver final : public TimelineResolver {
9091
~RotationTimelineResolver();
9192

9293
// |Resolver|
93-
void Apply(Node& target, Scalar time, Scalar weight) override;
94+
void Apply(Node& target, SecondsF time, Scalar weight) override;
9495

9596
private:
9697
RotationTimelineResolver();
@@ -107,7 +108,7 @@ class ScaleTimelineResolver final : public TimelineResolver {
107108
~ScaleTimelineResolver();
108109

109110
// |Resolver|
110-
void Apply(Node& target, Scalar time, Scalar weight) override;
111+
void Apply(Node& target, SecondsF time, Scalar weight) override;
111112

112113
private:
113114
ScaleTimelineResolver();

0 commit comments

Comments
 (0)