File: keyframe_effect.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (410 lines) | stat: -rw-r--r-- 15,390 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/animation/keyframe/keyframe_effect.h"

#include <algorithm>
#include <vector>

#include "ui/gfx/animation/keyframe/animation_curve.h"
#include "ui/gfx/animation/keyframe/keyframed_animation_curve.h"

namespace gfx {

namespace {

static int s_next_keyframe_model_id = 1;
static int s_next_group_id = 1;

void ReverseKeyframeModel(base::TimeTicks monotonic_time,
                          KeyframeModel* keyframe_model) {
  keyframe_model->set_direction(keyframe_model->direction() ==
                                        KeyframeModel::Direction::NORMAL
                                    ? KeyframeModel::Direction::REVERSE
                                    : KeyframeModel::Direction::NORMAL);
  // Our goal here is to reverse the given keyframe_model. That is, if
  // we're 20% of the way through the keyframe_model in the forward direction,
  // we'd like to be 80% of the way of the reversed keyframe model (so it will
  // end quickly).
  //
  // We can modify our "progress" through an animation by modifying the "time
  // offset", a value added to the current time by the animation system before
  // applying any other adjustments.
  //
  // Let our start time be s, our current time be t, and our final time (or
  // duration) be d. After reversing the keyframe_model, we would like to start
  // sampling from d - t as depicted below.
  //
  //  Forward:
  //  s    t                         d
  //  |----|-------------------------|
  //
  //  Reversed:
  //  s                         t    d
  //  |----|--------------------|----|
  //       -----time-offset----->
  //
  // Now, if we let o represent our desired offset, we need to ensure that
  //   t = d - (o + t)
  //
  // That is, sampling at the current time in either the forward or reverse
  // curves must result in the same value, otherwise we'll get jank.
  //
  // This implies that,
  //   0 = d - o - 2t
  //   o = d - 2t
  //
  // Now if there was a previous offset, we must adjust d by that offset before
  // performing this computation, so it becomes d - o_old - 2t:
  keyframe_model->set_time_offset(
      keyframe_model->curve()->Duration() - keyframe_model->time_offset() -
      (2 * (monotonic_time - keyframe_model->start_time())));
}

std::unique_ptr<CubicBezierTimingFunction> CreateTransitionTimingFunction() {
  return CubicBezierTimingFunction::CreatePreset(
      CubicBezierTimingFunction::EaseType::EASE);
}

base::TimeDelta GetStartTime(KeyframeModel* keyframe_model) {
  if (keyframe_model->direction() == KeyframeModel::Direction::NORMAL) {
    return base::TimeDelta();
  }
  return keyframe_model->curve()->Duration();
}

base::TimeDelta GetEndTime(KeyframeModel* keyframe_model) {
  if (keyframe_model->direction() == KeyframeModel::Direction::REVERSE) {
    return base::TimeDelta();
  }
  return keyframe_model->curve()->Duration();
}

template <typename ValueType>
void TransitionValueTo(KeyframeEffect* animator,
                       typename AnimationTraits<ValueType>::TargetType* target,
                       base::TimeTicks monotonic_time,
                       int target_property,
                       const ValueType& from,
                       const ValueType& to) {
  DCHECK(target);

  if (animator->transition().target_properties.find(target_property) ==
      animator->transition().target_properties.end()) {
    AnimationTraits<ValueType>::OnValueAnimated(target, to, target_property);
    return;
  }

  KeyframeModel* running_keyframe_model =
      animator->GetRunningKeyframeModelForProperty(target_property);

  ValueType effective_current = from;

  if (running_keyframe_model) {
    const auto* curve = AnimationTraits<ValueType>::ToDerivedCurve(
        running_keyframe_model->curve());

    if (running_keyframe_model->IsFinishedAt(monotonic_time)) {
      effective_current = curve->GetTransformedValue(
          GetEndTime(running_keyframe_model),
          gfx::TimingFunction::LimitDirection::RIGHT);
    } else {
      if (SufficientlyEqual(to,
                            curve->GetTransformedValue(
                                GetEndTime(running_keyframe_model),
                                gfx::TimingFunction::LimitDirection::RIGHT))) {
        return;
      }
      if (SufficientlyEqual(to,
                            curve->GetTransformedValue(
                                GetStartTime(running_keyframe_model),
                                gfx::TimingFunction::LimitDirection::RIGHT))) {
        ReverseKeyframeModel(monotonic_time, running_keyframe_model);
        return;
      }
    }
  } else if (SufficientlyEqual(to, from)) {
    return;
  }

  animator->RemoveKeyframeModels(target_property);

  std::unique_ptr<typename AnimationTraits<ValueType>::KeyframedCurveType>
      curve(AnimationTraits<ValueType>::KeyframedCurveType::Create());

  curve->AddKeyframe(AnimationTraits<ValueType>::KeyframeType::Create(
      base::TimeDelta(), effective_current, CreateTransitionTimingFunction()));

  curve->AddKeyframe(AnimationTraits<ValueType>::KeyframeType::Create(
      animator->transition().duration, to, CreateTransitionTimingFunction()));

  curve->set_target(target);

  animator->AddKeyframeModel(KeyframeModel::Create(
      std::move(curve), KeyframeEffect::GetNextKeyframeModelId(),
      target_property));
}

}  // namespace

int KeyframeEffect::GetNextKeyframeModelId() {
  return s_next_keyframe_model_id++;
}

int KeyframeEffect::GetNextGroupId() {
  return s_next_group_id++;
}

KeyframeEffect::KeyframeEffect() = default;
KeyframeEffect::KeyframeEffect(KeyframeEffect&&) = default;
KeyframeEffect::~KeyframeEffect() = default;

void KeyframeEffect::AddKeyframeModel(
    std::unique_ptr<KeyframeModel> keyframe_model) {
  keyframe_models_.push_back(std::move(keyframe_model));
}

void KeyframeEffect::RemoveKeyframeModel(int keyframe_model_id) {
  // Since we want to use the KeyframeModels that we're going to remove, we
  // need to use a stable_partition here instead of remove_if. remove_if leaves
  // the removed items in an unspecified state.
  auto keyframe_models_to_remove = std::stable_partition(
      keyframe_models_.begin(), keyframe_models_.end(),
      [keyframe_model_id](
          const std::unique_ptr<gfx::KeyframeModel>& keyframe_model) {
        return keyframe_model->id() != keyframe_model_id;
      });

  RemoveKeyframeModelRange(keyframe_models_to_remove, keyframe_models_.end());
}

void KeyframeEffect::RemoveKeyframeModels(int target_property) {
  auto keyframe_models_to_remove = std::stable_partition(
      keyframe_models_.begin(), keyframe_models_.end(),
      [target_property](
          const std::unique_ptr<gfx::KeyframeModel>& keyframe_model) {
        return keyframe_model->TargetProperty() != target_property;
      });
  RemoveKeyframeModelRange(keyframe_models_to_remove, keyframe_models_.end());
}

void KeyframeEffect::RemoveAllKeyframeModels() {
  RemoveKeyframeModelRange(keyframe_models_.begin(), keyframe_models_.end());
}

bool KeyframeEffect::Tick(base::TimeTicks monotonic_time) {
  return TickInternal(monotonic_time, true);
}

void KeyframeEffect::RemoveKeyframeModelRange(
    typename KeyframeModels::iterator to_remove_begin,
    typename KeyframeModels::iterator to_remove_end) {
  keyframe_models_.erase(to_remove_begin, to_remove_end);
}

void KeyframeEffect::TickKeyframeModel(base::TimeTicks monotonic_time,
                                       KeyframeModel* keyframe_model) {
  if ((keyframe_model->run_state() != KeyframeModel::STARTING &&
       keyframe_model->run_state() != KeyframeModel::RUNNING &&
       keyframe_model->run_state() != KeyframeModel::PAUSED &&
       keyframe_model->run_state() != KeyframeModel::WAITING_FOR_DELETION) ||
      !keyframe_model->HasActiveTime(monotonic_time)) {
    return;
  }

  AnimationCurve* curve = keyframe_model->curve();
  TimingFunction::LimitDirection limit_direction;
  base::TimeDelta trimmed = keyframe_model->TrimTimeToCurrentIteration(
      monotonic_time, &limit_direction);
  curve->Tick(trimmed, keyframe_model->TargetProperty(), keyframe_model,
              limit_direction);
}

bool KeyframeEffect::TickInternal(base::TimeTicks monotonic_time,
                                  bool include_infinite_animations) {
  StartKeyframeModels(monotonic_time, include_infinite_animations);

  bool active = false;
  for (auto& keyframe_model : keyframe_models_) {
    if (!include_infinite_animations &&
        keyframe_model->iterations() == std::numeric_limits<double>::infinity())
      continue;
    TickKeyframeModel(monotonic_time, keyframe_model.get());
    active = true;
  }

  // Remove finished keyframe_models.
  std::erase_if(
      keyframe_models_,
      [monotonic_time](const std::unique_ptr<KeyframeModel>& keyframe_model) {
        return !keyframe_model->is_finished() &&
               keyframe_model->IsFinishedAt(monotonic_time);
      });

  StartKeyframeModels(monotonic_time, include_infinite_animations);
  return active;
}

void KeyframeEffect::FinishAll() {
  base::TimeTicks now = base::TimeTicks::Now();
  const bool include_infinite_animations = false;
  TickInternal(now, include_infinite_animations);
  TickInternal(base::TimeTicks::Max(), include_infinite_animations);
#ifndef NDEBUG
  for (auto& keyframe_model : keyframe_models_) {
    DCHECK_EQ(std::numeric_limits<double>::infinity(),
              keyframe_model->iterations());
  }
#endif
}

void KeyframeEffect::SetTransitionedProperties(
    const std::set<int>& properties) {
  transition_.target_properties = properties;
}

void KeyframeEffect::SetTransitionDuration(base::TimeDelta delta) {
  transition_.duration = delta;
}

void KeyframeEffect::TransitionFloatTo(FloatAnimationCurve::Target* target,
                                       base::TimeTicks monotonic_time,
                                       int target_property,
                                       float from,
                                       float to) {
  TransitionValueTo<float>(this, target, monotonic_time, target_property, from,
                           to);
}

void KeyframeEffect::TransitionTransformOperationsTo(
    TransformAnimationCurve::Target* target,
    base::TimeTicks monotonic_time,
    int target_property,
    const gfx::TransformOperations& from,
    const gfx::TransformOperations& to) {
  TransitionValueTo<gfx::TransformOperations>(this, target, monotonic_time,
                                              target_property, from, to);
}

void KeyframeEffect::TransitionSizeTo(SizeAnimationCurve::Target* target,
                                      base::TimeTicks monotonic_time,
                                      int target_property,
                                      const gfx::SizeF& from,
                                      const gfx::SizeF& to) {
  TransitionValueTo<gfx::SizeF>(this, target, monotonic_time, target_property,
                                from, to);
}

void KeyframeEffect::TransitionColorTo(ColorAnimationCurve::Target* target,
                                       base::TimeTicks monotonic_time,
                                       int target_property,
                                       SkColor from,
                                       SkColor to) {
  TransitionValueTo<SkColor>(this, target, monotonic_time, target_property,
                             from, to);
}

bool KeyframeEffect::IsAnimatingProperty(int property) const {
  for (auto& keyframe_model : keyframe_models_) {
    if (keyframe_model->TargetProperty() == property)
      return true;
  }
  return false;
}

bool KeyframeEffect::IsAnimating() const {
  return !keyframe_models_.empty();
}

float KeyframeEffect::GetTargetFloatValue(int target_property,
                                          float default_value) const {
  return GetTargetValue<float>(target_property, default_value);
}

gfx::TransformOperations KeyframeEffect::GetTargetTransformOperationsValue(
    int target_property,
    const gfx::TransformOperations& default_value) const {
  return GetTargetValue<gfx::TransformOperations>(target_property,
                                                  default_value);
}

gfx::SizeF KeyframeEffect::GetTargetSizeValue(
    int target_property,
    const gfx::SizeF& default_value) const {
  return GetTargetValue<gfx::SizeF>(target_property, default_value);
}

SkColor KeyframeEffect::GetTargetColorValue(int target_property,
                                            SkColor default_value) const {
  return GetTargetValue<SkColor>(target_property, default_value);
}

void KeyframeEffect::StartKeyframeModels(base::TimeTicks monotonic_time,
                                         bool include_infinite_animations) {
  TargetProperties animated_properties;
  for (auto& keyframe_model : keyframe_models_) {
    if (!include_infinite_animations &&
        keyframe_model->iterations() == std::numeric_limits<double>::infinity())
      continue;
    if (keyframe_model->run_state() == KeyframeModel::RUNNING ||
        keyframe_model->run_state() == KeyframeModel::PAUSED) {
      animated_properties[keyframe_model->TargetProperty()] = true;
    }
  }
  for (auto& keyframe_model : keyframe_models_) {
    if (!include_infinite_animations &&
        keyframe_model->iterations() == std::numeric_limits<double>::infinity())
      continue;
    if (!animated_properties[keyframe_model->TargetProperty()] &&
        keyframe_model->run_state() ==
            KeyframeModel::WAITING_FOR_TARGET_AVAILABILITY) {
      animated_properties[keyframe_model->TargetProperty()] = true;
      keyframe_model->SetRunState(KeyframeModel::RUNNING, monotonic_time);
      keyframe_model->set_start_time(monotonic_time);
    }
  }
}

KeyframeModel* KeyframeEffect::GetRunningKeyframeModelForProperty(
    int target_property) const {
  for (auto& keyframe_model : keyframe_models_) {
    if ((keyframe_model->run_state() == KeyframeModel::RUNNING ||
         keyframe_model->run_state() == KeyframeModel::PAUSED) &&
        keyframe_model->TargetProperty() == target_property) {
      return keyframe_model.get();
    }
  }
  return nullptr;
}

KeyframeModel* KeyframeEffect::GetKeyframeModel(int target_property) const {
  for (size_t i = 0; i < keyframe_models().size(); ++i) {
    size_t index = keyframe_models().size() - i - 1;
    if (keyframe_models_[index]->TargetProperty() == target_property)
      return keyframe_models_[index].get();
  }
  return nullptr;
}

KeyframeModel* KeyframeEffect::GetKeyframeModelById(int id) const {
  for (auto& keyframe_model : keyframe_models())
    if (keyframe_model->id() == id)
      return keyframe_model.get();
  return nullptr;
}

template <typename ValueType>
ValueType KeyframeEffect::GetTargetValue(int target_property,
                                         const ValueType& default_value) const {
  KeyframeModel* running_keyframe_model = GetKeyframeModel(target_property);
  if (!running_keyframe_model) {
    return default_value;
  }
  const auto* curve = AnimationTraits<ValueType>::ToDerivedCurve(
      running_keyframe_model->curve());
  return curve->GetTransformedValue(GetEndTime(running_keyframe_model),
                                    gfx::TimingFunction::LimitDirection::RIGHT);
}

}  // namespace gfx