summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/animation/worklet_animation_controller.cc
blob: b690076cf33d222d4053efb388bc2cc1c0837f9d (plain)
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// 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/worklet_animation_controller.h"

#include "third_party/blink/renderer/core/animation/document_timeline.h"
#include "third_party/blink/renderer/core/animation/scroll_timeline.h"
#include "third_party/blink/renderer/core/animation/worklet_animation_base.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/local_frame_view.h"
#include "third_party/blink/renderer/platform/graphics/animation_worklet_mutator_dispatcher_impl.h"
#include "third_party/blink/renderer/platform/graphics/main_thread_mutator_client.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"

namespace blink {

namespace {

int GetId(const WorkletAnimationBase& animation) {
  return animation.GetWorkletAnimationId().animation_id;
}

}  // namespace

WorkletAnimationController::WorkletAnimationController(Document* document)
    : document_(document) {}

WorkletAnimationController::~WorkletAnimationController() = default;

void WorkletAnimationController::AttachAnimation(
    WorkletAnimationBase& animation) {
  DCHECK(IsMainThread());
  DCHECK(!pending_animations_.Contains(&animation));
  DCHECK(!animations_.Contains(GetId(animation)));
  pending_animations_.insert(&animation);

  DCHECK_EQ(document_, animation.GetDocument());
  if (LocalFrameView* view = animation.GetDocument()->View())
    view->ScheduleAnimation();
}

void WorkletAnimationController::DetachAnimation(
    WorkletAnimationBase& animation) {
  DCHECK(IsMainThread());
  pending_animations_.erase(&animation);
  animations_.erase(GetId(animation));
}

void WorkletAnimationController::InvalidateAnimation(
    WorkletAnimationBase& animation) {
  DCHECK(IsMainThread());
  pending_animations_.insert(&animation);
  if (LocalFrameView* view = animation.GetDocument()->View())
    view->ScheduleAnimation();
}

void WorkletAnimationController::UpdateAnimationStates() {
  DCHECK(IsMainThread());
  HeapHashSet<Member<WorkletAnimationBase>> animations;
  animations.swap(pending_animations_);
  for (const auto& animation : animations) {
    animation->UpdateCompositingState();
    if (animation->IsActiveAnimation())
      animations_.insert(GetId(*animation), animation);
  }
  if (!animations_.IsEmpty() && document_->View())
    document_->View()->ScheduleAnimation();
}

void WorkletAnimationController::UpdateAnimationTimings(
    TimingUpdateReason reason) {
  DCHECK(IsMainThread());
  // Worklet animations inherited time values are only ever updated once per
  // animation frame. This means the inherited time does not change outside of
  // the frame so return early in the on-demand case.
  if (reason == kTimingUpdateOnDemand)
    return;

  MutateAnimations();
  ApplyAnimationTimings(reason);
}

void WorkletAnimationController::ScrollSourceCompositingStateChanged(
    Node* node) {
  DCHECK(ScrollTimeline::HasActiveScrollTimeline(node));
  for (const auto& animation : animations_.Values()) {
    if (animation->GetTimeline()->IsScrollTimeline() &&
        To<ScrollTimeline>(animation->GetTimeline())->scrollSource() == node) {
      InvalidateAnimation(*animation);
    }
  }
}

base::WeakPtr<AnimationWorkletMutatorDispatcherImpl>
WorkletAnimationController::EnsureMainThreadMutatorDispatcher(
    scoped_refptr<base::SingleThreadTaskRunner>* mutator_task_runner) {
  base::WeakPtr<AnimationWorkletMutatorDispatcherImpl> mutator_dispatcher;
  if (!mutator_task_runner_) {
    main_thread_mutator_client_ =
        AnimationWorkletMutatorDispatcherImpl::CreateMainThreadClient(
            &mutator_dispatcher, &mutator_task_runner_);
    main_thread_mutator_client_->SetDelegate(this);
  }

  DCHECK(main_thread_mutator_client_);
  DCHECK(mutator_task_runner_);
  DCHECK(mutator_dispatcher);
  *mutator_task_runner = mutator_task_runner_;
  return mutator_dispatcher;
}

// TODO(yigu): Currently one animator name is synced back per registration.
// Eventually all registered names should be synced in batch once a module
// completes its loading in the worklet scope. https://crbug.com/920722.
void WorkletAnimationController::SynchronizeAnimatorName(
    const String& animator_name) {
  animator_names_.insert(animator_name);
}

bool WorkletAnimationController::IsAnimatorRegistered(
    const String& animator_name) const {
  return animator_names_.Contains(animator_name);
}

void WorkletAnimationController::SetMutationUpdate(
    std::unique_ptr<AnimationWorkletOutput> output_state) {
  if (!output_state)
    return;

  for (auto& to_update : output_state->animations) {
    int id = to_update.worklet_animation_id.animation_id;
    if (auto* animation = animations_.at(id))
      animation->SetOutputState(to_update);
  }
}

void WorkletAnimationController::MutateAnimations() {
  if (!main_thread_mutator_client_)
    return;

  main_thread_mutator_client_->Mutator()->MutateSynchronously(
      CollectAnimationStates());
}

std::unique_ptr<AnimationWorkletDispatcherInput>
WorkletAnimationController::CollectAnimationStates() {
  std::unique_ptr<AnimationWorkletDispatcherInput> result =
      std::make_unique<AnimationWorkletDispatcherInput>();

  for (auto& animation : animations_.Values())
    animation->UpdateInputState(result.get());

  return result;
}

void WorkletAnimationController::ApplyAnimationTimings(
    TimingUpdateReason reason) {
  for (const auto& animation : animations_.Values())
    animation->Update(reason);
}

void WorkletAnimationController::Trace(Visitor* visitor) {
  visitor->Trace(pending_animations_);
  visitor->Trace(animations_);
  visitor->Trace(document_);
}

}  // namespace blink