Skip to content

Commit

Permalink
CC: Support fractional iteration count on compositor animations
Browse files Browse the repository at this point in the history
BUG=398767

Review URL: https://codereview.chromium.org/423373003

Cr-Commit-Position: refs/heads/master@{#288931}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@288931 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
samli@chromium.org committed Aug 12, 2014
1 parent da89b85 commit 564f12a
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
6 changes: 4 additions & 2 deletions cc/animation/animation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,12 @@ double Animation::TrimTimeToCurrentIteration(
// We need to know the current iteration if we're alternating.
int iteration = 0;

// If we are past the active interval, return iteration duration.
// If we are past the active interval, return iteration duration of last
// iteration
if (is_past_total_duration) {
iteration = iterations_ - 1;
trimmed_in_seconds = curve_->Duration();
double frac = fmod(curve_->Duration() * iterations_, curve_->Duration());
trimmed_in_seconds = frac == 0 ? curve_->Duration() : frac;
} else {
iteration = static_cast<int>(trimmed_in_seconds / curve_->Duration());
// Calculate x where trimmed = x + n * curve_->Duration() for some positive
Expand Down
6 changes: 3 additions & 3 deletions cc/animation/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ class CC_EXPORT Animation {
// This is the number of times that the animation will play. If this
// value is zero the animation will not play. If it is negative, then
// the animation will loop indefinitely.
int iterations() const { return iterations_; }
void set_iterations(int n) { iterations_ = n; }
double iterations() const { return iterations_; }
void set_iterations(double n) { iterations_ = n; }

base::TimeTicks start_time() const { return start_time_; }

Expand Down Expand Up @@ -161,7 +161,7 @@ class CC_EXPORT Animation {

TargetProperty target_property_;
RunState run_state_;
int iterations_;
double iterations_;
base::TimeTicks start_time_;
Direction direction_;

Expand Down
35 changes: 31 additions & 4 deletions cc/animation/animation_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static base::TimeTicks TicksFromSecondsF(double seconds) {
base::Time::kMicrosecondsPerSecond);
}

scoped_ptr<Animation> CreateAnimation(int iterations, double duration) {
scoped_ptr<Animation> CreateAnimation(double iterations, double duration) {
scoped_ptr<Animation> to_return(Animation::Create(
make_scoped_ptr(
new FakeFloatAnimationCurve(duration)).PassAs<AnimationCurve>(),
Expand All @@ -29,7 +29,7 @@ scoped_ptr<Animation> CreateAnimation(int iterations, double duration) {
return to_return.Pass();
}

scoped_ptr<Animation> CreateAnimation(int iterations) {
scoped_ptr<Animation> CreateAnimation(double iterations) {
return CreateAnimation(iterations, 1);
}

Expand All @@ -48,11 +48,22 @@ TEST(AnimationTest, TrimTimeOneIteration) {
EXPECT_EQ(1, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
}

TEST(AnimationTest, TrimTimeOneHalfIteration) {
scoped_ptr<Animation> anim(CreateAnimation(1.5));
EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(-1.0)));
EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
EXPECT_EQ(0.9, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.9)));
EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.5)));
EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
}

TEST(AnimationTest, TrimTimeInfiniteIterations) {
scoped_ptr<Animation> anim(CreateAnimation(-1));
EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
EXPECT_EQ(0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.5)));
}

Expand Down Expand Up @@ -103,6 +114,22 @@ TEST(AnimationTest, TrimTimeAlternateTwoIterations) {
EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.25)));
}

TEST(AnimationTest, TrimTimeAlternateTwoHalfIterations) {
scoped_ptr<Animation> anim(CreateAnimation(2.5));
anim->set_direction(Animation::Alternate);
EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.0)));
EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.25)));
EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.5)));
EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(0.75)));
EXPECT_EQ(1.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.0)));
EXPECT_EQ(0.75, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.25)));
EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(1.75)));
EXPECT_EQ(0.0, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.0)));
EXPECT_EQ(0.25, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.25)));
EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.50)));
EXPECT_EQ(0.5, anim->TrimTimeToCurrentIteration(TicksFromSecondsF(2.75)));
}

TEST(AnimationTest, TrimTimeAlternateReverseInfiniteIterations) {
scoped_ptr<Animation> anim(CreateAnimation(-1));
anim->set_direction(Animation::AlternateReverse);
Expand Down
10 changes: 10 additions & 0 deletions content/renderer/compositor_bindings/web_animation_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,23 @@ WebCompositorAnimationImpl::targetProperty() const {
animation_->target_property());
}

#if WEB_ANIMATION_SUPPORTS_FRACTIONAL_ITERATIONS
double WebCompositorAnimationImpl::iterations() const {
return animation_->iterations();
}

void WebCompositorAnimationImpl::setIterations(double n) {
animation_->set_iterations(n);
}
#else
int WebCompositorAnimationImpl::iterations() const {
return animation_->iterations();
}

void WebCompositorAnimationImpl::setIterations(int n) {
animation_->set_iterations(n);
}
#endif

double WebCompositorAnimationImpl::startTime() const {
return (animation_->start_time() - base::TimeTicks()).InSecondsF();
Expand Down
5 changes: 5 additions & 0 deletions content/renderer/compositor_bindings/web_animation_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ class WebCompositorAnimationImpl : public blink::WebCompositorAnimation {
// blink::WebCompositorAnimation implementation
virtual int id();
virtual TargetProperty targetProperty() const;
#if WEB_ANIMATION_SUPPORTS_FRACTIONAL_ITERATIONS
virtual double iterations() const;
virtual void setIterations(double iterations);
#else
virtual int iterations() const;
virtual void setIterations(int iterations);
#endif
virtual double startTime() const;
virtual void setStartTime(double monotonic_time);
virtual double timeOffset() const;
Expand Down

0 comments on commit 564f12a

Please sign in to comment.