File: animation_timeline.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 (248 lines) | stat: -rw-r--r-- 8,879 bytes parent folder | download | duplicates (2)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/animation/animation_timeline.h"

#include "base/trace_event/trace_event.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_union_cssnumericvalue_double.h"
#include "third_party/blink/renderer/core/animation/animation_trigger.h"
#include "third_party/blink/renderer/core/animation/document_animations.h"
#include "third_party/blink/renderer/core/animation/keyframe_effect.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/core/frame/local_frame_view.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/core/page/page_animator.h"

namespace blink {

AnimationTimeline::AnimationTimeline(Document* document)
    : document_(document), outdated_animation_count_(0) {
  document_->GetDocumentAnimations().AddTimeline(*this);
}

void AnimationTimeline::AnimationAttached(Animation* animation) {
  DCHECK(!animations_.Contains(animation));
  animations_.insert(animation);
  animation->ResolveTimelineOffsets(GetTimelineRange());
}

void AnimationTimeline::AnimationDetached(Animation* animation) {
  animations_.erase(animation);
  animations_needing_update_.erase(animation);
  if (animation->Outdated())
    outdated_animation_count_--;
  animation->ResolveTimelineOffsets(GetTimelineRange());
}

bool CompareAnimations(const Member<Animation>& left,
                       const Member<Animation>& right) {
  // This uses pointer order comparision because it is less expensive and
  // element order doesn't affect the animation result(http://crbug.com/1047316)
  return Animation::HasLowerCompositeOrdering(
      left.Get(), right.Get(),
      Animation::CompareAnimationsOrdering::kPointerOrder);
}

V8CSSNumberish* AnimationTimeline::currentTime() {
  const std::optional<base::TimeDelta>& result = CurrentPhaseAndTime().time;
  if (result)
    return MakeGarbageCollected<V8CSSNumberish>(result->InMillisecondsF());
  return nullptr;
}

std::optional<AnimationTimeDelta> AnimationTimeline::CurrentTime() {
  std::optional<base::TimeDelta> result = CurrentPhaseAndTime().time;
  return result ? std::make_optional(AnimationTimeDelta(result.value()))
                : std::nullopt;
}

std::optional<double> AnimationTimeline::CurrentTimeMilliseconds() {
  std::optional<base::TimeDelta> result = CurrentPhaseAndTime().time;
  return result ? std::make_optional(result->InMillisecondsF()) : std::nullopt;
}

std::optional<double> AnimationTimeline::CurrentTimeSeconds() {
  std::optional<base::TimeDelta> result = CurrentPhaseAndTime().time;
  return result ? std::make_optional(result->InSecondsF()) : std::nullopt;
}

V8CSSNumberish* AnimationTimeline::duration() {
  return nullptr;
}

void AnimationTimeline::ClearOutdatedAnimation(Animation* animation) {
  DCHECK(!animation->Outdated());
  outdated_animation_count_--;
}

wtf_size_t AnimationTimeline::AnimationsNeedingUpdateCount() const {
  wtf_size_t count = 0;
  for (const auto& animation : animations_needing_update_) {
    // Exclude animations which are not actively generating frames.
    if ((!animation->CompositorPending() && !animation->Playing() &&
         !IsProgressBased()) ||
        animation->AnimationHasNoEffect()) {
      continue;
    }
    count++;
  }
  return count;
}

bool AnimationTimeline::NeedsAnimationTimingUpdate() {
  PhaseAndTime current_phase_and_time = CurrentPhaseAndTime();
  if (current_phase_and_time == last_current_phase_and_time_)
    return false;

  // We allow |last_current_phase_and_time_| to advance here when there
  // are no animations to allow animations spawned during style
  // recalc to not invalidate this flag.
  if (animations_needing_update_.empty()) {
    last_current_phase_and_time_ = current_phase_and_time;
    // Make sure triggers get the chance to respond to the updated PhaseAndTime.
    update_triggers_ = true;
  }

  return !animations_needing_update_.empty();
}

void AnimationTimeline::ServiceAnimations(TimingUpdateReason reason) {
  TRACE_EVENT0("blink", "AnimationTimeline::serviceAnimations");

  auto current_phase_and_time = CurrentPhaseAndTime();

  if (IsProgressBased()) {
    if (last_current_phase_and_time_ != current_phase_and_time) {
      UpdateCompositorTimeline();
      update_triggers_ = true;
    }
    if (RuntimeEnabledFeatures::AnimationTriggerEnabled()) {
      if (update_triggers_) {
        for (Animation* animation : animations_for_triggering_) {
          if (AnimationTrigger* trigger = animation->GetTriggerInternal()) {
            trigger->ActionAnimation(animation);
          }
        }
        update_triggers_ = false;
      }
    }
  }

  last_current_phase_and_time_ = current_phase_and_time;

  HeapVector<Member<Animation>> animations;
  animations.ReserveInitialCapacity(animations_needing_update_.size());
  for (Animation* animation : animations_needing_update_)
    animations.push_back(animation);

  std::sort(animations.begin(), animations.end(), CompareAnimations);

  for (Animation* animation : animations) {
    if (!animation->Update(reason))
      animations_needing_update_.erase(animation);
  }

  DCHECK_EQ(outdated_animation_count_, 0U);
  DCHECK(last_current_phase_and_time_ == CurrentPhaseAndTime());

#if DCHECK_IS_ON()
  for (const auto& animation : animations_needing_update_)
    DCHECK(!animation->Outdated());
#endif
  // Explicitly free the backing store to avoid memory regressions.
  // TODO(bikineev): Revisit when young generation is done.
  animations.clear();
}

// https://drafts.csswg.org/web-animations-1/#removing-replaced-animations
void AnimationTimeline::getReplaceableAnimations(
    AnimationTimeline::ReplaceableAnimationsMap* replaceable_animations_map) {
  for (Animation* animation : animations_) {
    // Initial conditions for removal:
    // * has an associated animation effect whose effect target is a descendant
    //    of doc, and
    // * is replaceable
    if (!animation->IsReplaceable())
      continue;
    DCHECK(animation->effect());
    Element* target = To<KeyframeEffect>(animation->effect())->EffectTarget();
    DCHECK(target);
    if (target->GetDocument() != animation->GetDocument())
      continue;

    auto inserted = replaceable_animations_map->insert(target, nullptr);
    if (inserted.is_new_entry) {
      inserted.stored_value->value =
          MakeGarbageCollected<GCedHeapVector<Member<Animation>>>();
    }
    inserted.stored_value->value->push_back(animation);
  }
}

void AnimationTimeline::SetOutdatedAnimation(Animation* animation) {
  DCHECK(animation->Outdated());
  outdated_animation_count_++;
  animations_needing_update_.insert(animation);
  if (IsActive() && document_->GetPage() &&
      !document_->GetPage()->Animator().IsServicingAnimations()) {
    ScheduleServiceOnNextFrame();
  }
}

void AnimationTimeline::ScheduleServiceOnNextFrame() {
  if (document_->View())
    document_->View()->ScheduleAnimation();
}

Animation* AnimationTimeline::Play(AnimationEffect* child,
                                   ExceptionState& exception_state) {
  Animation* animation = Animation::Create(child, this, exception_state);
  if (animation) {
    DCHECK(animations_.Contains(animation));
    animation->play();
    DCHECK(animations_needing_update_.Contains(animation));
  }

  return animation;
}

void AnimationTimeline::MarkAnimationsCompositorPending(bool source_changed) {
  Animation::CompositorPendingReason reason =
      source_changed ? Animation::CompositorPendingReason::kPendingEffectChange
                     : Animation::CompositorPendingReason::kPendingUpdate;
  for (const auto& animation : animations_) {
    animation->SetCompositorPending(reason);
  }
}

void AnimationTimeline::MarkPendingIfCompositorPropertyAnimationChanges(
    const PaintArtifactCompositor* paint_artifact_compositor) {
  for (const auto& animation : animations_) {
    animation->MarkPendingIfCompositorPropertyAnimationChanges(
        paint_artifact_compositor);
  }
}

void AnimationTimeline::AddAnimationForTriggering(Animation* animation) {
  AnimationTrigger* trigger = animation->GetTriggerInternal();
  DCHECK(IsProgressBased() && trigger &&
         trigger->GetTimelineInternal() == this);
  animations_for_triggering_.insert(animation);
  update_triggers_ = true;
}

void AnimationTimeline::RemoveAnimationForTriggering(Animation* animation) {
  animations_for_triggering_.erase(animation);
}

void AnimationTimeline::Trace(Visitor* visitor) const {
  visitor->Trace(document_);
  visitor->Trace(animations_needing_update_);
  visitor->Trace(animations_);
  visitor->Trace(animations_for_triggering_);
  ScriptWrappable::Trace(visitor);
}

}  // namespace blink