Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(417)

Side by Side Diff: third_party/WebKit/Source/modules/compositorworker/WorkletAnimation.cpp

Issue 2869183002: Initial implementation of WorkletAnimation (Closed)
Patch Set: Remove WorkletAnimationTest.cpp from BUILD.gn Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "modules/compositorworker/WorkletAnimation.h"
6
7 #include "core/animation/ElementAnimations.h"
8 #include "core/animation/KeyframeEffectModel.h"
9 #include "core/animation/ScrollTimeline.h"
10 #include "core/animation/Timing.h"
11 #include "core/dom/Node.h"
12 #include "core/dom/NodeComputedStyle.h"
13 #include "platform/wtf/text/WTFString.h"
14 #include "public/platform/Platform.h"
15 #include "public/platform/WebCompositorSupport.h"
16
17 namespace blink {
18
19 namespace {
20 bool ValidateEffects(const HeapVector<Member<KeyframeEffectReadOnly>>& effects,
21 String& error_string) {
22 if (effects.IsEmpty()) {
23 error_string = "Effects array must be non-empty";
24 return false;
25 }
26
27 Document& target_document = effects.at(0)->Target()->GetDocument();
28 for (const auto& effect : effects) {
29 if (effect->Target()->GetDocument() != target_document) {
30 error_string = "All effects must target elements in the same document";
31 return false;
32 }
33 }
34 return true;
35 }
36
37 bool ValidateTimelines(HeapVector<DocumentTimelineOrScrollTimeline>& timelines,
38 String& error_string) {
39 if (timelines.IsEmpty()) {
40 error_string = "Timelines array must be non-empty";
41 return false;
42 }
43
44 for (const auto& timeline : timelines) {
45 if (timeline.isScrollTimeline()) {
46 DoubleOrScrollTimelineAutoKeyword time_range;
47 timeline.getAsScrollTimeline()->timeRange(time_range);
48 if (time_range.isScrollTimelineAutoKeyword()) {
49 error_string = "ScrollTimeline timeRange must have non-auto value";
50 return false;
51 }
52 }
53 }
54 return true;
55 }
56
57 String AnimationPlayStateToString(
58 WorkletAnimation::AnimationPlayState play_state) {
59 switch (play_state) {
60 case WorkletAnimation::kIdle:
61 return "idle";
62 case WorkletAnimation::kPending:
63 return "pending";
64 case WorkletAnimation::kRunning:
65 return "running";
66 default:
67 NOTREACHED();
68 return "";
69 }
70 }
71 } // namespace
72
73 WorkletAnimation* WorkletAnimation::Create(
74 String animator_name,
75 const HeapVector<Member<KeyframeEffectReadOnly>>& effects,
76 HeapVector<DocumentTimelineOrScrollTimeline>& timelines,
77 RefPtr<SerializedScriptValue> options,
78 ExceptionState& exception_state) {
79 String error_string;
80 if (!ValidateEffects(effects, error_string)) {
81 exception_state.ThrowDOMException(kNotSupportedError, error_string);
82 return nullptr;
83 }
84
85 if (!ValidateTimelines(timelines, error_string)) {
86 exception_state.ThrowDOMException(kNotSupportedError, error_string);
87 return nullptr;
88 }
89
90 Document& document = effects.at(0)->Target()->GetDocument();
91 WorkletAnimation* animation = new WorkletAnimation(
92 animator_name, document, effects, timelines, std::move(options));
93 document.Timeline().CompositorTimeline()->PlayerAttached(*animation);
94
95 return animation;
96 }
97
98 WorkletAnimation::WorkletAnimation(
99 String animator_name,
100 Document& document,
101 const HeapVector<Member<KeyframeEffectReadOnly>>& effects,
102 HeapVector<DocumentTimelineOrScrollTimeline>& timelines,
103 RefPtr<SerializedScriptValue> options)
104 : animator_name_(animator_name),
105 document_(document),
106 effects_(effects),
107 timelines_(timelines),
108 options_(std::move(options)) {
109 DCHECK(Platform::Current()->IsThreadedAnimationEnabled());
110 DCHECK(Platform::Current()->CompositorSupport());
111 compositor_player_ = CompositorAnimationPlayer::Create();
112 compositor_player_->SetAnimationDelegate(this);
113 }
114
115 String WorkletAnimation::playState() {
116 return AnimationPlayStateToString(play_state_);
117 }
118
119 void WorkletAnimation::play() {
120 document_->GetWorkletAnimationController().AttachAnimation(*this);
121 play_state_ = kPending;
122 }
123
124 void WorkletAnimation::cancel() {
125 document_->GetWorkletAnimationController().DetachAnimation(*this);
126 play_state_ = kIdle;
127 }
128
129 bool WorkletAnimation::StartOnCompositor() {
130 // TODO(smcgruer): We need to start all of the effects, not just the first.
131 double animation_playback_rate = 1;
132 Element& target = *effects_.at(0)->Target();
133 ToKeyframeEffectModelBase(effects_.at(0)->Model())
134 ->SnapshotAllCompositorKeyframes(target, target.ComputedStyleRef(),
135 target.ParentComputedStyle());
136 bool success =
137 effects_.at(0)
138 ->CheckCanStartAnimationOnCompositor(animation_playback_rate)
139 .Ok();
140 // Currently |StartAnimationOnCompositor| is unable to propagate a
141 // WorkletAnimation effect to the compositor, so this method is a no-op.
142 // TODO(smcgruer): Actually create the element animations on the compositor.
143 play_state_ = success ? kRunning : kIdle;
144 return success;
145 }
146
147 void WorkletAnimation::Dispose() {
148 document_->Timeline().CompositorTimeline()->PlayerDestroyed(*this);
149 compositor_player_->SetAnimationDelegate(nullptr);
150 compositor_player_ = nullptr;
151 }
152
153 DEFINE_TRACE(WorkletAnimation) {
154 visitor->Trace(document_);
155 visitor->Trace(effects_);
156 visitor->Trace(timelines_);
157 WorkletAnimationBase::Trace(visitor);
158 }
159
160 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698