Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2423)

Unified Diff: cc/animation/keyframed_animation_curve.cc

Issue 598853003: CC: Add curve (whole animation) timing function to compositor animations. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adressed comments, split up GetProgress function. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « cc/animation/keyframed_animation_curve.h ('k') | cc/animation/keyframed_animation_curve_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cc/animation/keyframed_animation_curve.cc
diff --git a/cc/animation/keyframed_animation_curve.cc b/cc/animation/keyframed_animation_curve.cc
index 4522ca2e6d98029a155e540743d4a2a7a653c429..ef50cd4de47ce64da9fad7bca6510a2f2e473958 100644
--- a/cc/animation/keyframed_animation_curve.cc
+++ b/cc/animation/keyframed_animation_curve.cc
@@ -29,14 +29,44 @@ void InsertKeyframe(scoped_ptr<Keyframe> keyframe,
keyframes.push_back(keyframe.Pass());
}
-template <class Keyframes>
-float GetProgress(double t, size_t i, const Keyframes& keyframes) {
- float progress =
- static_cast<float>((t - keyframes[i]->Time()) /
- (keyframes[i + 1]->Time() - keyframes[i]->Time()));
+template <typename Keyframe>
+double GetCurveTime(const ScopedPtrVector<Keyframe>& keyframes,
+ const scoped_ptr<TimingFunction>& timing_function,
+ double time) {
+ if (timing_function) {
+ double start_time = keyframes.front()->Time();
+ double duration = keyframes.back()->Time() - start_time;
+ double progress = (time - start_time) / duration;
+
+ time = timing_function->GetValue(progress) * duration + start_time;
+ }
+
+ return time;
+}
+
+template <typename Keyframe>
+size_t GetActiveKeyframe(const ScopedPtrVector<Keyframe>& keyframes,
+ double time) {
+ size_t i = 0;
+ for (; i < keyframes.size() - 2; ++i) { // Last keyframe is never active.
+ if (time < keyframes[i + 1]->Time())
+ break;
+ }
+
+ return i;
+}
- if (keyframes[i]->timing_function())
+template <typename Keyframe>
+double GetKeyframeProgress(const ScopedPtrVector<Keyframe>& keyframes,
+ double time,
+ size_t i) {
+ double progress = (time - keyframes[i]->Time()) /
+ (keyframes[i + 1]->Time() - keyframes[i]->Time());
+
+ if (keyframes[i]->timing_function()) {
progress = keyframes[i]->timing_function()->GetValue(progress);
+ }
+
return progress;
}
@@ -189,6 +219,10 @@ scoped_ptr<AnimationCurve> KeyframedColorAnimationCurve::Clone() const {
KeyframedColorAnimationCurve::Create());
for (size_t i = 0; i < keyframes_.size(); ++i)
to_return->AddKeyframe(keyframes_[i]->Clone());
+
+ if (timing_function_)
+ to_return->SetTimingFunction(CloneTimingFunction(timing_function_.get()));
+
return to_return.PassAs<AnimationCurve>();
}
@@ -199,13 +233,9 @@ SkColor KeyframedColorAnimationCurve::GetValue(double t) const {
if (t >= keyframes_.back()->Time())
return keyframes_.back()->Value();
- size_t i = 0;
- for (; i < keyframes_.size() - 1; ++i) {
- if (t < keyframes_[i + 1]->Time())
- break;
- }
-
- float progress = GetProgress(t, i, keyframes_);
+ t = GetCurveTime(keyframes_, timing_function_, t);
+ size_t i = GetActiveKeyframe(keyframes_, t);
+ double progress = GetKeyframeProgress(keyframes_, t, i);
samli 2014/09/25 07:03:48 Maybe TransformedAnimationTime and TransformedKeyf
ikilpatrick 2014/09/25 09:01:45 Renamed to: TransformedAnimationTime & Transformed
return gfx::Tween::ColorValueBetween(
progress, keyframes_[i]->Value(), keyframes_[i + 1]->Value());
@@ -236,6 +266,10 @@ scoped_ptr<AnimationCurve> KeyframedFloatAnimationCurve::Clone() const {
KeyframedFloatAnimationCurve::Create());
for (size_t i = 0; i < keyframes_.size(); ++i)
to_return->AddKeyframe(keyframes_[i]->Clone());
+
+ if (timing_function_)
+ to_return->SetTimingFunction(CloneTimingFunction(timing_function_.get()));
+
return to_return.PassAs<AnimationCurve>();
}
@@ -246,13 +280,9 @@ float KeyframedFloatAnimationCurve::GetValue(double t) const {
if (t >= keyframes_.back()->Time())
return keyframes_.back()->Value();
- size_t i = 0;
- for (; i < keyframes_.size() - 1; ++i) {
- if (t < keyframes_[i+1]->Time())
- break;
- }
-
- float progress = GetProgress(t, i, keyframes_);
+ t = GetCurveTime(keyframes_, timing_function_, t);
+ size_t i = GetActiveKeyframe(keyframes_, t);
+ double progress = GetKeyframeProgress(keyframes_, t, i);
return keyframes_[i]->Value() +
(keyframes_[i+1]->Value() - keyframes_[i]->Value()) * progress;
@@ -281,26 +311,11 @@ scoped_ptr<AnimationCurve> KeyframedTransformAnimationCurve::Clone() const {
KeyframedTransformAnimationCurve::Create());
for (size_t i = 0; i < keyframes_.size(); ++i)
to_return->AddKeyframe(keyframes_[i]->Clone());
- return to_return.PassAs<AnimationCurve>();
-}
-
-// Assumes that (*keyframes).front()->Time() < t < (*keyframes).back()-Time().
-template<typename ValueType, typename KeyframeType>
-static ValueType GetCurveValue(const ScopedPtrVector<KeyframeType>* keyframes,
- double t) {
- size_t i = 0;
- for (; i < keyframes->size() - 1; ++i) {
- if (t < (*keyframes)[i+1]->Time())
- break;
- }
- double progress = (t - (*keyframes)[i]->Time()) /
- ((*keyframes)[i+1]->Time() - (*keyframes)[i]->Time());
+ if (timing_function_)
+ to_return->SetTimingFunction(CloneTimingFunction(timing_function_.get()));
- if ((*keyframes)[i]->timing_function())
- progress = (*keyframes)[i]->timing_function()->GetValue(progress);
-
- return (*keyframes)[i+1]->Value().Blend((*keyframes)[i]->Value(), progress);
+ return to_return.PassAs<AnimationCurve>();
}
gfx::Transform KeyframedTransformAnimationCurve::GetValue(double t) const {
@@ -310,7 +325,11 @@ gfx::Transform KeyframedTransformAnimationCurve::GetValue(double t) const {
if (t >= keyframes_.back()->Time())
return keyframes_.back()->Value().Apply();
- return GetCurveValue<gfx::Transform, TransformKeyframe>(&keyframes_, t);
+ t = GetCurveTime(keyframes_, timing_function_, t);
+ size_t i = GetActiveKeyframe(keyframes_, t);
+ double progress = GetKeyframeProgress(keyframes_, t, i);
+
+ return keyframes_[i + 1]->Value().Blend(keyframes_[i]->Value(), progress);
}
bool KeyframedTransformAnimationCurve::AnimatedBoundsForBox(
@@ -396,6 +415,10 @@ scoped_ptr<AnimationCurve> KeyframedFilterAnimationCurve::Clone() const {
KeyframedFilterAnimationCurve::Create());
for (size_t i = 0; i < keyframes_.size(); ++i)
to_return->AddKeyframe(keyframes_[i]->Clone());
+
+ if (timing_function_)
+ to_return->SetTimingFunction(CloneTimingFunction(timing_function_.get()));
+
return to_return.PassAs<AnimationCurve>();
}
@@ -406,7 +429,11 @@ FilterOperations KeyframedFilterAnimationCurve::GetValue(double t) const {
if (t >= keyframes_.back()->Time())
return keyframes_.back()->Value();
- return GetCurveValue<FilterOperations, FilterKeyframe>(&keyframes_, t);
+ t = GetCurveTime(keyframes_, timing_function_, t);
+ size_t i = GetActiveKeyframe(keyframes_, t);
+ double progress = GetKeyframeProgress(keyframes_, t, i);
+
+ return keyframes_[i + 1]->Value().Blend(keyframes_[i]->Value(), progress);
}
bool KeyframedFilterAnimationCurve::HasFilterThatMovesPixels() const {
« no previous file with comments | « cc/animation/keyframed_animation_curve.h ('k') | cc/animation/keyframed_animation_curve_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698