summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/animationworklet/animator.h
blob: bfafe9739cb6b84fa059f79486e873211243f3e9 (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
// 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.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_ANIMATIONWORKLET_ANIMATOR_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_ANIMATIONWORKLET_ANIMATOR_H_

#include "third_party/blink/renderer/core/animation/timing.h"
#include "third_party/blink/renderer/modules/animationworklet/worklet_animation_effect_timings.h"
#include "third_party/blink/renderer/modules/animationworklet/worklet_animation_options.h"
#include "third_party/blink/renderer/modules/animationworklet/worklet_group_effect.h"
#include "third_party/blink/renderer/platform/bindings/name_client.h"
#include "third_party/blink/renderer/platform/bindings/trace_wrapper_v8_reference.h"
#include "third_party/blink/renderer/platform/graphics/animation_worklet_mutators_state.h"
#include "third_party/blink/renderer/platform/heap/handle.h"

#include "v8/include/v8.h"

namespace blink {

class AnimatorDefinition;

// Represents an animator instance. It owns the underlying |v8::Object| for the
// instance and knows how to invoke the |animate| function on it.
// See also |AnimationWorkletGlobalScope::CreateInstance|.
class Animator final : public GarbageCollected<Animator>, public NameClient {
 public:
  Animator(v8::Isolate*,
           AnimatorDefinition*,
           v8::Local<v8::Value> instance,
           const String& name,
           WorkletAnimationOptions options,
           const Vector<base::Optional<base::TimeDelta>>& local_times,
           const Vector<Timing>& timings);
  ~Animator();
  void Trace(blink::Visitor*);
  const char* NameInHeapSnapshot() const override { return "Animator"; }

  // Returns true if it successfully invoked animate callback in JS. It receives
  // latest state coming from |AnimationHost| as input and fills
  // the output state with new updates.
  bool Animate(v8::Isolate* isolate,
               double current_time,
               AnimationWorkletDispatcherOutput::AnimationState* output);
  v8::Local<v8::Value> State(v8::Isolate*, ExceptionState&);

  template <typename T>
  void GetLocalTimes(T& local_times) const {
    local_times.clear();

    const auto& children = group_effect_->getChildren();
    local_times.resize(children.size());

    for (wtf_size_t i = 0; i < children.size(); i++) {
      local_times[i] = children[i]->local_time();
    }
  }

  Vector<Timing> GetTimings() const;
  bool IsStateful() const;

  const String& name() const { return name_; }
  WorkletAnimationOptions options() { return options_; }

 private:
  // This object keeps the definition object, and animator instance alive.
  // It participates in wrapper tracing as it holds onto V8 wrappers.
  Member<AnimatorDefinition> definition_;
  TraceWrapperV8Reference<v8::Value> instance_;

  // The 'name' and 'options' of the animator need to be stored to be
  // migrated to the new animator upon switching global scopes. For other
  // properties, 'animation id' and 'number of effects' can be, and 'state'
  // should be queried on the fly.
  String name_;
  WorkletAnimationOptions options_;

  Member<WorkletGroupEffect> group_effect_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_ANIMATIONWORKLET_ANIMATOR_H_