| OLD | NEW |
| (Empty) |
| 1 // Copyright 2012 The Chromium 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 #include "cc/test/animation_test_common.h" | |
| 6 | |
| 7 #include "cc/animation/animation_id_provider.h" | |
| 8 #include "cc/animation/keyframed_animation_curve.h" | |
| 9 #include "cc/animation/layer_animation_controller.h" | |
| 10 #include "cc/animation/transform_operations.h" | |
| 11 #include "cc/base/time_util.h" | |
| 12 #include "cc/layers/layer.h" | |
| 13 #include "cc/layers/layer_impl.h" | |
| 14 | |
| 15 using cc::Animation; | |
| 16 using cc::AnimationCurve; | |
| 17 using cc::EaseTimingFunction; | |
| 18 using cc::FloatKeyframe; | |
| 19 using cc::KeyframedFloatAnimationCurve; | |
| 20 using cc::KeyframedTransformAnimationCurve; | |
| 21 using cc::TimingFunction; | |
| 22 using cc::TransformKeyframe; | |
| 23 | |
| 24 namespace cc { | |
| 25 | |
| 26 template <class Target> | |
| 27 int AddOpacityTransition(Target* target, | |
| 28 double duration, | |
| 29 float start_opacity, | |
| 30 float end_opacity, | |
| 31 bool use_timing_function) { | |
| 32 scoped_ptr<KeyframedFloatAnimationCurve> | |
| 33 curve(KeyframedFloatAnimationCurve::Create()); | |
| 34 | |
| 35 scoped_ptr<TimingFunction> func; | |
| 36 if (!use_timing_function) | |
| 37 func = EaseTimingFunction::Create(); | |
| 38 if (duration > 0.0) | |
| 39 curve->AddKeyframe( | |
| 40 FloatKeyframe::Create(base::TimeDelta(), start_opacity, func.Pass())); | |
| 41 curve->AddKeyframe(FloatKeyframe::Create( | |
| 42 base::TimeDelta::FromSecondsD(duration), end_opacity, nullptr)); | |
| 43 | |
| 44 int id = AnimationIdProvider::NextAnimationId(); | |
| 45 | |
| 46 scoped_ptr<Animation> animation( | |
| 47 Animation::Create(curve.Pass(), id, AnimationIdProvider::NextGroupId(), | |
| 48 Animation::OPACITY)); | |
| 49 animation->set_needs_synchronized_start_time(true); | |
| 50 | |
| 51 target->AddAnimation(animation.Pass()); | |
| 52 return id; | |
| 53 } | |
| 54 | |
| 55 template <class Target> | |
| 56 int AddAnimatedTransform(Target* target, | |
| 57 double duration, | |
| 58 TransformOperations start_operations, | |
| 59 TransformOperations operations) { | |
| 60 scoped_ptr<KeyframedTransformAnimationCurve> | |
| 61 curve(KeyframedTransformAnimationCurve::Create()); | |
| 62 | |
| 63 if (duration > 0.0) { | |
| 64 curve->AddKeyframe(TransformKeyframe::Create(base::TimeDelta(), | |
| 65 start_operations, nullptr)); | |
| 66 } | |
| 67 | |
| 68 curve->AddKeyframe(TransformKeyframe::Create( | |
| 69 base::TimeDelta::FromSecondsD(duration), operations, nullptr)); | |
| 70 | |
| 71 int id = AnimationIdProvider::NextAnimationId(); | |
| 72 | |
| 73 scoped_ptr<Animation> animation( | |
| 74 Animation::Create(curve.Pass(), id, AnimationIdProvider::NextGroupId(), | |
| 75 Animation::TRANSFORM)); | |
| 76 animation->set_needs_synchronized_start_time(true); | |
| 77 | |
| 78 target->AddAnimation(animation.Pass()); | |
| 79 return id; | |
| 80 } | |
| 81 | |
| 82 template <class Target> | |
| 83 int AddAnimatedTransform(Target* target, | |
| 84 double duration, | |
| 85 int delta_x, | |
| 86 int delta_y) { | |
| 87 TransformOperations start_operations; | |
| 88 if (duration > 0.0) { | |
| 89 start_operations.AppendTranslate(delta_x, delta_y, 0.0); | |
| 90 } | |
| 91 | |
| 92 TransformOperations operations; | |
| 93 operations.AppendTranslate(delta_x, delta_y, 0.0); | |
| 94 return AddAnimatedTransform(target, duration, start_operations, operations); | |
| 95 } | |
| 96 | |
| 97 template <class Target> | |
| 98 int AddAnimatedFilter(Target* target, | |
| 99 double duration, | |
| 100 float start_brightness, | |
| 101 float end_brightness) { | |
| 102 scoped_ptr<KeyframedFilterAnimationCurve> | |
| 103 curve(KeyframedFilterAnimationCurve::Create()); | |
| 104 | |
| 105 if (duration > 0.0) { | |
| 106 FilterOperations start_filters; | |
| 107 start_filters.Append( | |
| 108 FilterOperation::CreateBrightnessFilter(start_brightness)); | |
| 109 curve->AddKeyframe( | |
| 110 FilterKeyframe::Create(base::TimeDelta(), start_filters, nullptr)); | |
| 111 } | |
| 112 | |
| 113 FilterOperations filters; | |
| 114 filters.Append(FilterOperation::CreateBrightnessFilter(end_brightness)); | |
| 115 curve->AddKeyframe(FilterKeyframe::Create( | |
| 116 base::TimeDelta::FromSecondsD(duration), filters, nullptr)); | |
| 117 | |
| 118 int id = AnimationIdProvider::NextAnimationId(); | |
| 119 | |
| 120 scoped_ptr<Animation> animation(Animation::Create( | |
| 121 curve.Pass(), id, AnimationIdProvider::NextGroupId(), Animation::FILTER)); | |
| 122 animation->set_needs_synchronized_start_time(true); | |
| 123 | |
| 124 target->AddAnimation(animation.Pass()); | |
| 125 return id; | |
| 126 } | |
| 127 | |
| 128 FakeFloatAnimationCurve::FakeFloatAnimationCurve() | |
| 129 : duration_(base::TimeDelta::FromSecondsD(1.0)) { | |
| 130 } | |
| 131 | |
| 132 FakeFloatAnimationCurve::FakeFloatAnimationCurve(double duration) | |
| 133 : duration_(base::TimeDelta::FromSecondsD(duration)) { | |
| 134 } | |
| 135 | |
| 136 FakeFloatAnimationCurve::~FakeFloatAnimationCurve() {} | |
| 137 | |
| 138 base::TimeDelta FakeFloatAnimationCurve::Duration() const { | |
| 139 return duration_; | |
| 140 } | |
| 141 | |
| 142 float FakeFloatAnimationCurve::GetValue(base::TimeDelta now) const { | |
| 143 return 0.0f; | |
| 144 } | |
| 145 | |
| 146 scoped_ptr<AnimationCurve> FakeFloatAnimationCurve::Clone() const { | |
| 147 return make_scoped_ptr(new FakeFloatAnimationCurve); | |
| 148 } | |
| 149 | |
| 150 FakeTransformTransition::FakeTransformTransition(double duration) | |
| 151 : duration_(base::TimeDelta::FromSecondsD(duration)) { | |
| 152 } | |
| 153 | |
| 154 FakeTransformTransition::~FakeTransformTransition() {} | |
| 155 | |
| 156 base::TimeDelta FakeTransformTransition::Duration() const { | |
| 157 return duration_; | |
| 158 } | |
| 159 | |
| 160 gfx::Transform FakeTransformTransition::GetValue(base::TimeDelta time) const { | |
| 161 return gfx::Transform(); | |
| 162 } | |
| 163 | |
| 164 bool FakeTransformTransition::AnimatedBoundsForBox(const gfx::BoxF& box, | |
| 165 gfx::BoxF* bounds) const { | |
| 166 return false; | |
| 167 } | |
| 168 | |
| 169 bool FakeTransformTransition::AffectsScale() const { return false; } | |
| 170 | |
| 171 bool FakeTransformTransition::IsTranslation() const { return true; } | |
| 172 | |
| 173 bool FakeTransformTransition::PreservesAxisAlignment() const { | |
| 174 return true; | |
| 175 } | |
| 176 | |
| 177 bool FakeTransformTransition::MaximumTargetScale(bool forward_direction, | |
| 178 float* max_scale) const { | |
| 179 *max_scale = 1.f; | |
| 180 return true; | |
| 181 } | |
| 182 | |
| 183 scoped_ptr<AnimationCurve> FakeTransformTransition::Clone() const { | |
| 184 return make_scoped_ptr(new FakeTransformTransition(*this)); | |
| 185 } | |
| 186 | |
| 187 FakeFloatTransition::FakeFloatTransition(double duration, float from, float to) | |
| 188 : duration_(base::TimeDelta::FromSecondsD(duration)), from_(from), to_(to) { | |
| 189 } | |
| 190 | |
| 191 FakeFloatTransition::~FakeFloatTransition() {} | |
| 192 | |
| 193 base::TimeDelta FakeFloatTransition::Duration() const { | |
| 194 return duration_; | |
| 195 } | |
| 196 | |
| 197 float FakeFloatTransition::GetValue(base::TimeDelta time) const { | |
| 198 double progress = TimeUtil::Divide(time, duration_); | |
| 199 if (progress >= 1.0) | |
| 200 progress = 1.0; | |
| 201 return (1.0 - progress) * from_ + progress * to_; | |
| 202 } | |
| 203 | |
| 204 FakeLayerAnimationValueObserver::FakeLayerAnimationValueObserver() | |
| 205 : opacity_(0.0f), animation_waiting_for_deletion_(false) { | |
| 206 } | |
| 207 | |
| 208 FakeLayerAnimationValueObserver::~FakeLayerAnimationValueObserver() {} | |
| 209 | |
| 210 void FakeLayerAnimationValueObserver::OnFilterAnimated( | |
| 211 const FilterOperations& filters) { | |
| 212 filters_ = filters; | |
| 213 } | |
| 214 | |
| 215 void FakeLayerAnimationValueObserver::OnOpacityAnimated(float opacity) { | |
| 216 opacity_ = opacity; | |
| 217 } | |
| 218 | |
| 219 void FakeLayerAnimationValueObserver::OnTransformAnimated( | |
| 220 const gfx::Transform& transform) { | |
| 221 transform_ = transform; | |
| 222 } | |
| 223 | |
| 224 void FakeLayerAnimationValueObserver::OnScrollOffsetAnimated( | |
| 225 const gfx::ScrollOffset& scroll_offset) { | |
| 226 scroll_offset_ = scroll_offset; | |
| 227 } | |
| 228 | |
| 229 void FakeLayerAnimationValueObserver::OnAnimationWaitingForDeletion() { | |
| 230 animation_waiting_for_deletion_ = true; | |
| 231 } | |
| 232 | |
| 233 bool FakeLayerAnimationValueObserver::IsActive() const { | |
| 234 return true; | |
| 235 } | |
| 236 | |
| 237 bool FakeInactiveLayerAnimationValueObserver::IsActive() const { | |
| 238 return false; | |
| 239 } | |
| 240 | |
| 241 gfx::ScrollOffset FakeLayerAnimationValueProvider::ScrollOffsetForAnimation() | |
| 242 const { | |
| 243 return scroll_offset_; | |
| 244 } | |
| 245 | |
| 246 scoped_ptr<AnimationCurve> FakeFloatTransition::Clone() const { | |
| 247 return make_scoped_ptr(new FakeFloatTransition(*this)); | |
| 248 } | |
| 249 | |
| 250 int AddOpacityTransitionToController(LayerAnimationController* controller, | |
| 251 double duration, | |
| 252 float start_opacity, | |
| 253 float end_opacity, | |
| 254 bool use_timing_function) { | |
| 255 return AddOpacityTransition(controller, | |
| 256 duration, | |
| 257 start_opacity, | |
| 258 end_opacity, | |
| 259 use_timing_function); | |
| 260 } | |
| 261 | |
| 262 int AddAnimatedTransformToController(LayerAnimationController* controller, | |
| 263 double duration, | |
| 264 int delta_x, | |
| 265 int delta_y) { | |
| 266 return AddAnimatedTransform(controller, | |
| 267 duration, | |
| 268 delta_x, | |
| 269 delta_y); | |
| 270 } | |
| 271 | |
| 272 int AddAnimatedFilterToController(LayerAnimationController* controller, | |
| 273 double duration, | |
| 274 float start_brightness, | |
| 275 float end_brightness) { | |
| 276 return AddAnimatedFilter( | |
| 277 controller, duration, start_brightness, end_brightness); | |
| 278 } | |
| 279 | |
| 280 int AddOpacityTransitionToLayer(Layer* layer, | |
| 281 double duration, | |
| 282 float start_opacity, | |
| 283 float end_opacity, | |
| 284 bool use_timing_function) { | |
| 285 return AddOpacityTransition(layer, | |
| 286 duration, | |
| 287 start_opacity, | |
| 288 end_opacity, | |
| 289 use_timing_function); | |
| 290 } | |
| 291 | |
| 292 int AddOpacityTransitionToLayer(LayerImpl* layer, | |
| 293 double duration, | |
| 294 float start_opacity, | |
| 295 float end_opacity, | |
| 296 bool use_timing_function) { | |
| 297 return AddOpacityTransition(layer->layer_animation_controller(), | |
| 298 duration, | |
| 299 start_opacity, | |
| 300 end_opacity, | |
| 301 use_timing_function); | |
| 302 } | |
| 303 | |
| 304 int AddAnimatedTransformToLayer(Layer* layer, | |
| 305 double duration, | |
| 306 int delta_x, | |
| 307 int delta_y) { | |
| 308 return AddAnimatedTransform(layer, duration, delta_x, delta_y); | |
| 309 } | |
| 310 | |
| 311 int AddAnimatedTransformToLayer(LayerImpl* layer, | |
| 312 double duration, | |
| 313 int delta_x, | |
| 314 int delta_y) { | |
| 315 return AddAnimatedTransform(layer->layer_animation_controller(), | |
| 316 duration, | |
| 317 delta_x, | |
| 318 delta_y); | |
| 319 } | |
| 320 | |
| 321 int AddAnimatedTransformToLayer(Layer* layer, | |
| 322 double duration, | |
| 323 TransformOperations start_operations, | |
| 324 TransformOperations operations) { | |
| 325 return AddAnimatedTransform(layer, duration, start_operations, operations); | |
| 326 } | |
| 327 | |
| 328 int AddAnimatedTransformToLayer(LayerImpl* layer, | |
| 329 double duration, | |
| 330 TransformOperations start_operations, | |
| 331 TransformOperations operations) { | |
| 332 return AddAnimatedTransform(layer->layer_animation_controller(), | |
| 333 duration, | |
| 334 start_operations, | |
| 335 operations); | |
| 336 } | |
| 337 | |
| 338 int AddAnimatedFilterToLayer(Layer* layer, | |
| 339 double duration, | |
| 340 float start_brightness, | |
| 341 float end_brightness) { | |
| 342 return AddAnimatedFilter(layer, duration, start_brightness, end_brightness); | |
| 343 } | |
| 344 | |
| 345 int AddAnimatedFilterToLayer(LayerImpl* layer, | |
| 346 double duration, | |
| 347 float start_brightness, | |
| 348 float end_brightness) { | |
| 349 return AddAnimatedFilter(layer->layer_animation_controller(), | |
| 350 duration, | |
| 351 start_brightness, | |
| 352 end_brightness); | |
| 353 } | |
| 354 | |
| 355 } // namespace cc | |
| OLD | NEW |