summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/graphics/animation_worklet_mutator_dispatcher_impl.cc
blob: e34a9cccf596402704d3c458aae4144b0d6275e5 (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
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
// Copyright 2016 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/platform/graphics/animation_worklet_mutator_dispatcher_impl.h"

#include "base/barrier_closure.h"
#include "base/callback_helpers.h"
#include "base/metrics/histogram_macros.h"
#include "base/synchronization/waitable_event.h"
#include "base/time/default_tick_clock.h"
#include "base/timer/elapsed_timer.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/platform/graphics/animation_worklet_mutator.h"
#include "third_party/blink/renderer/platform/graphics/compositor_mutator_client.h"
#include "third_party/blink/renderer/platform/graphics/main_thread_mutator_client.h"
#include "third_party/blink/renderer/platform/instrumentation/tracing/trace_event.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread_scheduler.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"

namespace blink {

namespace {

int g_next_async_mutation_id = 0;
int GetNextAsyncMutationId() {
  return g_next_async_mutation_id++;
}

}  // end namespace

// Wrap output vector in a thread safe and ref-counted object since it is
// accessed from animation worklet threads and its lifetime must be guaranteed
// to outlive the mutation update cycle.
class AnimationWorkletMutatorDispatcherImpl::OutputVectorRef
    : public ThreadSafeRefCounted<OutputVectorRef> {
 public:
  static scoped_refptr<OutputVectorRef> Create() {
    return base::AdoptRef(new OutputVectorRef());
  }
  Vector<std::unique_ptr<AnimationWorkletDispatcherOutput>>& get() {
    return vector_;
  }

 private:
  OutputVectorRef() = default;
  Vector<std::unique_ptr<AnimationWorkletDispatcherOutput>> vector_;
};

struct AnimationWorkletMutatorDispatcherImpl::AsyncMutationRequest {
  base::TimeTicks request_time;
  std::unique_ptr<AnimationWorkletDispatcherInput> input_state;
  AsyncMutationCompleteCallback done_callback;

  AsyncMutationRequest(
      base::TimeTicks request_time,
      std::unique_ptr<AnimationWorkletDispatcherInput> input_state,
      AsyncMutationCompleteCallback done_callback)
      : request_time(request_time),
        input_state(std::move(input_state)),
        done_callback(std::move(done_callback)) {}

  ~AsyncMutationRequest() = default;
};

AnimationWorkletMutatorDispatcherImpl::AnimationWorkletMutatorDispatcherImpl(
    bool main_thread_task_runner)
    : client_(nullptr), outputs_(OutputVectorRef::Create()) {
  // By default web tests run without threaded compositing. See
  // https://crbug.com/770028. If threaded compositing is disabled or
  // |main_thread_task_runner| is true we run on the main thread's compositor
  // task runner otherwise we run tasks on the compositor thread's default
  // task runner.
  host_queue_ = main_thread_task_runner || !Thread::CompositorThread()
                    ? Thread::MainThread()->Scheduler()->CompositorTaskRunner()
                    : Thread::CompositorThread()->GetTaskRunner();
  tick_clock_ = std::make_unique<base::DefaultTickClock>();
}

AnimationWorkletMutatorDispatcherImpl::
    ~AnimationWorkletMutatorDispatcherImpl() {}

// static
template <typename ClientType>
std::unique_ptr<ClientType> AnimationWorkletMutatorDispatcherImpl::CreateClient(
    base::WeakPtr<AnimationWorkletMutatorDispatcherImpl>* weak_interface,
    scoped_refptr<base::SingleThreadTaskRunner>* queue,
    bool main_thread_client) {
  DCHECK(IsMainThread());
  auto mutator = std::make_unique<AnimationWorkletMutatorDispatcherImpl>(
      main_thread_client);
  // This is allowed since we own the class for the duration of creation.
  *weak_interface = mutator->weak_factory_.GetWeakPtr();
  *queue = mutator->GetTaskRunner();

  return std::make_unique<ClientType>(std::move(mutator));
}

// static
std::unique_ptr<CompositorMutatorClient>
AnimationWorkletMutatorDispatcherImpl::CreateCompositorThreadClient(
    base::WeakPtr<AnimationWorkletMutatorDispatcherImpl>* weak_interface,
    scoped_refptr<base::SingleThreadTaskRunner>* queue) {
  return CreateClient<CompositorMutatorClient>(weak_interface, queue, false);
}

// static
std::unique_ptr<MainThreadMutatorClient>
AnimationWorkletMutatorDispatcherImpl::CreateMainThreadClient(
    base::WeakPtr<AnimationWorkletMutatorDispatcherImpl>* weak_interface,
    scoped_refptr<base::SingleThreadTaskRunner>* queue) {
  return CreateClient<MainThreadMutatorClient>(weak_interface, queue, true);
}

void AnimationWorkletMutatorDispatcherImpl::MutateSynchronously(
    std::unique_ptr<AnimationWorkletDispatcherInput> mutator_input) {
  TRACE_EVENT0("cc", "AnimationWorkletMutatorDispatcherImpl::mutate");
  if (mutator_map_.IsEmpty() || !mutator_input)
    return;
  base::ElapsedTimer timer;
  DCHECK(client_);
  DCHECK(host_queue_->BelongsToCurrentThread());
  DCHECK(mutator_input_map_.IsEmpty());
  DCHECK(outputs_->get().IsEmpty());

  mutator_input_map_ = CreateInputMap(*mutator_input);
  if (mutator_input_map_.IsEmpty())
    return;

  base::WaitableEvent event;
  CrossThreadOnceClosure on_done = CrossThreadBindOnce(
      &base::WaitableEvent::Signal, WTF::CrossThreadUnretained(&event));
  RequestMutations(std::move(on_done));
  event.Wait();

  ApplyMutationsOnHostThread();

  UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES(
      "Animation.AnimationWorklet.Dispatcher.SynchronousMutateDuration",
      timer.Elapsed(), base::TimeDelta::FromMicroseconds(1),
      base::TimeDelta::FromMilliseconds(100), 50);
}

base::TimeTicks AnimationWorkletMutatorDispatcherImpl::NowTicks() const {
  DCHECK(tick_clock_);
  return tick_clock_->NowTicks();
}

bool AnimationWorkletMutatorDispatcherImpl::MutateAsynchronously(
    std::unique_ptr<AnimationWorkletDispatcherInput> mutator_input,
    MutateQueuingStrategy queuing_strategy,
    AsyncMutationCompleteCallback done_callback) {
  DCHECK(client_);
  DCHECK(host_queue_->BelongsToCurrentThread());
  if (mutator_map_.IsEmpty() || !mutator_input)
    return false;

  base::TimeTicks request_time = NowTicks();
  if (!mutator_input_map_.IsEmpty()) {
    // Still running mutations from a previous frame.
    switch (queuing_strategy) {
      case MutateQueuingStrategy::kDrop:
        // Skip this frame to avoid lagging behind.
        return false;

      case MutateQueuingStrategy::kQueueHighPriority:
        // Can only have one priority request in-flight.
        DCHECK(!queued_priority_request.get());
        queued_priority_request = std::make_unique<AsyncMutationRequest>(
            request_time, std::move(mutator_input), std::move(done_callback));
        return true;

      case MutateQueuingStrategy::kQueueAndReplaceNormalPriority:
        if (queued_replaceable_request.get()) {
          // Cancel previously queued request.
          request_time = queued_replaceable_request->request_time;
          std::move(queued_replaceable_request->done_callback)
              .Run(MutateStatus::kCanceled);
        }
        queued_replaceable_request = std::make_unique<AsyncMutationRequest>(
            request_time, std::move(mutator_input), std::move(done_callback));
        return true;
    }
  }

  mutator_input_map_ = CreateInputMap(*mutator_input);
  if (mutator_input_map_.IsEmpty())
    return false;

  MutateAsynchronouslyInternal(request_time, std::move(done_callback));
  return true;
}

void AnimationWorkletMutatorDispatcherImpl::MutateAsynchronouslyInternal(
    base::TimeTicks request_time,
    AsyncMutationCompleteCallback done_callback) {
  DCHECK(host_queue_->BelongsToCurrentThread());
  on_async_mutation_complete_ = std::move(done_callback);
  int next_async_mutation_id = GetNextAsyncMutationId();
  TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(
      "cc", "AnimationWorkletMutatorDispatcherImpl::MutateAsync",
      TRACE_ID_LOCAL(next_async_mutation_id));

  CrossThreadOnceClosure on_done = CrossThreadBindOnce(
      [](scoped_refptr<base::SingleThreadTaskRunner> host_queue,
         base::WeakPtr<AnimationWorkletMutatorDispatcherImpl> dispatcher,
         int next_async_mutation_id, base::TimeTicks request_time) {
        PostCrossThreadTask(
            *host_queue, FROM_HERE,
            CrossThreadBindOnce(
                &AnimationWorkletMutatorDispatcherImpl::AsyncMutationsDone,
                dispatcher, next_async_mutation_id, request_time));
      },
      host_queue_, weak_factory_.GetWeakPtr(), next_async_mutation_id,
      request_time);

  RequestMutations(std::move(on_done));
}

void AnimationWorkletMutatorDispatcherImpl::AsyncMutationsDone(
    int async_mutation_id,
    base::TimeTicks request_time) {
  DCHECK(client_);
  DCHECK(host_queue_->BelongsToCurrentThread());
  bool update_applied = ApplyMutationsOnHostThread();
  auto done_callback = std::move(on_async_mutation_complete_);
  std::unique_ptr<AsyncMutationRequest> queued_request;
  if (queued_priority_request.get()) {
    queued_request.reset(queued_priority_request.release());
  } else if (queued_replaceable_request.get()) {
    queued_request.reset(queued_replaceable_request.release());
  }
  if (queued_request.get()) {
    mutator_input_map_ = CreateInputMap(*queued_request->input_state);
    MutateAsynchronouslyInternal(queued_request->request_time,
                                 std::move(queued_request->done_callback));
  }
  // The trace event deos not include queuing time. It covers the interval
  // between dispatching the request and retrieving the results.
  TRACE_EVENT_NESTABLE_ASYNC_END0(
      "cc", "AnimationWorkletMutatorDispatcherImpl::MutateAsync",
      TRACE_ID_LOCAL(async_mutation_id));
  // The Async mutation duration is the total time between request and
  // completion, and thus includes queuing time.
  UMA_HISTOGRAM_CUSTOM_MICROSECONDS_TIMES(
      "Animation.AnimationWorklet.Dispatcher.AsynchronousMutateDuration",
      NowTicks() - request_time, base::TimeDelta::FromMicroseconds(1),
      base::TimeDelta::FromMilliseconds(100), 50);

  std::move(done_callback)
      .Run(update_applied ? MutateStatus::kCompletedWithUpdate
                          : MutateStatus::kCompletedNoUpdate);
}

void AnimationWorkletMutatorDispatcherImpl::RegisterAnimationWorkletMutator(
    CrossThreadPersistent<AnimationWorkletMutator> mutator,
    scoped_refptr<base::SingleThreadTaskRunner> mutator_runner) {
  TRACE_EVENT0(
      "cc",
      "AnimationWorkletMutatorDispatcherImpl::RegisterAnimationWorkletMutator");

  DCHECK(mutator);
  DCHECK(host_queue_->BelongsToCurrentThread());

  mutator_map_.insert(mutator, mutator_runner);
}

void AnimationWorkletMutatorDispatcherImpl::UnregisterAnimationWorkletMutator(
    CrossThreadPersistent<AnimationWorkletMutator> mutator) {
  TRACE_EVENT0("cc",
               "AnimationWorkletMutatorDispatcherImpl::"
               "UnregisterAnimationWorkletMutator");
  DCHECK(mutator);
  DCHECK(host_queue_->BelongsToCurrentThread());

  mutator_map_.erase(mutator);
}

void AnimationWorkletMutatorDispatcherImpl::SynchronizeAnimatorName(
    const String& animator_name) {
  client_->SynchronizeAnimatorName(animator_name);
}

bool AnimationWorkletMutatorDispatcherImpl::HasMutators() {
  return !mutator_map_.IsEmpty();
}

AnimationWorkletMutatorDispatcherImpl::InputMap
AnimationWorkletMutatorDispatcherImpl::CreateInputMap(
    AnimationWorkletDispatcherInput& mutator_input) const {
  InputMap input_map;
  for (const auto& pair : mutator_map_) {
    AnimationWorkletMutator* mutator = pair.key;
    const int worklet_id = mutator->GetWorkletId();
    std::unique_ptr<AnimationWorkletInput> input =
        mutator_input.TakeWorkletState(worklet_id);
    if (input) {
      input_map.insert(worklet_id, std::move(input));
    }
  }
  return input_map;
}

void AnimationWorkletMutatorDispatcherImpl::RequestMutations(
    CrossThreadOnceClosure done_callback) {
  DCHECK(client_);
  DCHECK(outputs_->get().IsEmpty());

  int num_requests = mutator_map_.size();
  if (num_requests == 0) {
    std::move(done_callback).Run();
    return;
  }

  int next_request_index = 0;
  outputs_->get().Grow(num_requests);
  base::RepeatingClosure on_mutator_done = base::BarrierClosure(
      num_requests, ConvertToBaseOnceCallback(std::move(done_callback)));

  for (const auto& pair : mutator_map_) {
    AnimationWorkletMutator* mutator = pair.key;
    scoped_refptr<base::SingleThreadTaskRunner> worklet_queue = pair.value;
    int worklet_id = mutator->GetWorkletId();
    DCHECK(!worklet_queue->BelongsToCurrentThread());

    // Wrap the barrier closure in a ScopedClosureRunner to guarantee it runs
    // even if the posted task does not run.
    auto on_done_runner =
        std::make_unique<base::ScopedClosureRunner>(on_mutator_done);

    auto it = mutator_input_map_.find(worklet_id);
    if (it == mutator_input_map_.end()) {
      // Here the on_done_runner goes out of scope which causes the barrier
      // closure to run.
      continue;
    }

    PostCrossThreadTask(
        *worklet_queue, FROM_HERE,
        CrossThreadBindOnce(
            [](AnimationWorkletMutator* mutator,
               std::unique_ptr<AnimationWorkletInput> input,
               scoped_refptr<OutputVectorRef> outputs, int index,
               std::unique_ptr<base::ScopedClosureRunner> on_done_runner) {
              std::unique_ptr<AnimationWorkletOutput> output =
                  mutator ? mutator->Mutate(std::move(input)) : nullptr;
              outputs->get()[index] = std::move(output);
              on_done_runner->RunAndReset();
            },
            // The mutator is created and destroyed on the worklet thread.
            WrapCrossThreadWeakPersistent(mutator),
            // The worklet input is not required after the Mutate call.
            WTF::Passed(std::move(it->value)),
            // The vector of outputs is wrapped in a scoped_refptr initialized
            // on the host thread. It can outlive the dispatcher during shutdown
            // of a process with a running animation.
            outputs_, next_request_index++,
            WTF::Passed(std::move(on_done_runner))));
  }
}

bool AnimationWorkletMutatorDispatcherImpl::ApplyMutationsOnHostThread() {
  DCHECK(client_);
  DCHECK(host_queue_->BelongsToCurrentThread());
  bool update_applied = false;
  for (auto& output : outputs_->get()) {
    if (output) {
      client_->SetMutationUpdate(std::move(output));
      update_applied = true;
    }
  }
  mutator_input_map_.clear();
  outputs_->get().clear();
  return update_applied;
}

}  // namespace blink