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

Side by Side Diff: Source/core/rendering/animation/WebAnimationProvider.cpp

Issue 23431021: Refactoring animation code in accelerated path. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years, 3 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
« no previous file with comments | « Source/core/rendering/animation/WebAnimationProvider.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2013 Intel Corporation. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27
28 #include "core/rendering/animation/WebAnimationProvider.h"
29
30 #include "core/platform/animation/AnimationTranslationUtil.h"
31 #include "core/platform/animation/CSSAnimationData.h"
32 #include "core/rendering/style/KeyframeList.h"
33 #include "core/rendering/style/RenderStyle.h"
34 #include "public/platform/WebAnimation.h"
35 #include "wtf/text/StringBuilder.h"
36
37 using WebKit::WebAnimation;
38
39 namespace WebCore {
40
41 namespace {
42
43 String animationNameForTransition(AnimatedPropertyID property)
44 {
45 // | is not a valid identifier character in CSS, so this can never conflict with a keyframe identifier.
46 StringBuilder id;
47 id.appendLiteral("-|transition");
48 id.appendNumber(static_cast<int>(property));
49 id.append('-');
50 return id.toString();
51 }
52
53 AnimatedPropertyID cssToGraphicsLayerProperty(CSSPropertyID cssProperty)
54 {
55 switch (cssProperty) {
56 case CSSPropertyWebkitTransform:
57 return AnimatedPropertyWebkitTransform;
58 case CSSPropertyOpacity:
59 return AnimatedPropertyOpacity;
60 case CSSPropertyBackgroundColor:
61 return AnimatedPropertyBackgroundColor;
Ian Vollick 2013/09/19 15:04:30 Are we accelerating this already?
dshwang 2013/09/19 15:56:48 Code looks like accelerated. void GraphicsLayer::s
62 case CSSPropertyWebkitFilter:
63 return AnimatedPropertyWebkitFilter;
64 default:
65 // It's fine if we see other css properties here; they are just not acce lerated.
66 break;
67 }
68 return AnimatedPropertyInvalid;
69 }
70
71 } // namespace
72
73 WebAnimations::WebAnimations()
74 {
75 }
76
77 WebAnimations::~WebAnimations()
78 {
79 }
80
81 // Copy constructor is needed to use this struct as a return value. It actually moves the ownership, not copy.
82 WebAnimations::WebAnimations(const WebAnimations& other)
83 {
84 ASSERT(isEmpty());
85 m_transformAnimation.swap(const_cast<OwnPtr<WebAnimation>& >(other.m_transfo rmAnimation));
86 m_opacityAnimation.swap(const_cast<OwnPtr<WebAnimation>& >(other.m_opacityAn imation));
87 m_filterAnimation.swap(const_cast<OwnPtr<WebAnimation>& >(other.m_filterAnim ation));
88 ASSERT(other.isEmpty());
89 }
90
91 bool WebAnimations::isEmpty() const
92 {
93 return !m_transformAnimation && !m_opacityAnimation && !m_filterAnimation;
94 }
95
96 WebAnimationProvider::WebAnimationProvider()
97 {
98 }
99
100 WebAnimationProvider::~WebAnimationProvider()
101 {
102 }
103
104 int WebAnimationProvider::getWebAnimationId(const String& animationName) const
105 {
106 if (!m_animationIdMap.contains(animationName))
107 return 0;
108 return m_animationIdMap.get(animationName);
109 }
110
111 int WebAnimationProvider::getWebAnimationId(CSSPropertyID property) const
112 {
113 AnimatedPropertyID animatedProperty = cssToGraphicsLayerProperty(property);
114 if (animatedProperty == AnimatedPropertyInvalid)
115 return 0;
116 return getWebAnimationId(animationNameForTransition(animatedProperty));
117 }
118
119 WebAnimations WebAnimationProvider::startAnimation(double timeOffset, const CSSA nimationData* anim, const KeyframeList& keyframes, bool hasTransform, const IntS ize& boxSize)
120 {
121 ASSERT(hasTransform || boxSize.isEmpty());
122 bool hasOpacity = keyframes.containsProperty(CSSPropertyOpacity);
123 bool hasFilter = keyframes.containsProperty(CSSPropertyWebkitFilter);
124
125 if (!hasOpacity && !hasTransform && !hasFilter)
126 return WebAnimations();
127
128 KeyframeValueList transformVector(AnimatedPropertyWebkitTransform);
129 KeyframeValueList opacityVector(AnimatedPropertyOpacity);
130 KeyframeValueList filterVector(AnimatedPropertyWebkitFilter);
131
132 size_t numKeyframes = keyframes.size();
133 for (size_t i = 0; i < numKeyframes; ++i) {
134 const KeyframeValue& currentKeyframe = keyframes[i];
135 const RenderStyle* keyframeStyle = currentKeyframe.style();
136 double key = currentKeyframe.key();
137
138 if (!keyframeStyle)
139 continue;
140
141 // Get timing function.
142 RefPtr<TimingFunction> tf = currentKeyframe.timingFunction(keyframes.ani mationName());
143
144 bool isFirstOrLastKeyframe = !key || key == 1;
145 if ((hasTransform && isFirstOrLastKeyframe) || currentKeyframe.containsP roperty(CSSPropertyWebkitTransform))
146 transformVector.insert(adoptPtr(new TransformAnimationValue(key, &(k eyframeStyle->transform()), tf)));
147
148 if ((hasOpacity && isFirstOrLastKeyframe) || currentKeyframe.containsPro perty(CSSPropertyOpacity))
149 opacityVector.insert(adoptPtr(new FloatAnimationValue(key, keyframeS tyle->opacity(), tf)));
150
151 if ((hasFilter && isFirstOrLastKeyframe) || currentKeyframe.containsProp erty(CSSPropertyWebkitFilter))
152 filterVector.insert(adoptPtr(new FilterAnimationValue(key, &(keyfram eStyle->filter()), tf)));
153 }
154 WebAnimations resultAnimations;
155 if (hasTransform)
156 resultAnimations.m_transformAnimation = appendWebAnimation(transformVect or, boxSize, anim, keyframes.animationName(), timeOffset);
Ian Vollick 2013/09/19 15:04:30 appendWebAnimation feels a bit confusing; I can't
dshwang 2013/09/19 15:56:48 I'll rename createWebAnimation. appendWebAnimation
157 if (hasOpacity)
158 resultAnimations.m_opacityAnimation = appendWebAnimation(opacityVector, IntSize(), anim, keyframes.animationName(), timeOffset);
159 if (hasFilter)
160 resultAnimations.m_filterAnimation = appendWebAnimation(filterVector, In tSize(), anim, keyframes.animationName(), timeOffset);
161
162 return resultAnimations;
163 }
164
165 WebAnimations WebAnimationProvider::startTransition(double timeOffset, CSSProper tyID property, const RenderStyle* fromStyle, const RenderStyle* toStyle, bool ha sTransform, bool hasFilter, const IntSize& boxSize, float fromOpacity, float toO pacity)
166 {
167 ASSERT(property != CSSPropertyInvalid);
168 ASSERT(property == CSSPropertyOpacity || (!fromOpacity && !toOpacity));
169
170 WebAnimations resultAnimations;
171 if (property == CSSPropertyOpacity) {
172 const CSSAnimationData* opacityAnim = toStyle->transitionForProperty(CSS PropertyOpacity);
173 if (opacityAnim && !opacityAnim->isEmptyOrZeroDuration()) {
174 KeyframeValueList opacityVector(AnimatedPropertyOpacity);
175 opacityVector.insert(adoptPtr(new FloatAnimationValue(0, fromOpacity )));
176 opacityVector.insert(adoptPtr(new FloatAnimationValue(1, toOpacity)) );
177 resultAnimations.m_opacityAnimation = appendWebAnimation(opacityVect or, IntSize(), opacityAnim, animationNameForTransition(AnimatedPropertyOpacity), timeOffset);
178 }
179 }
180 if (property == CSSPropertyWebkitTransform && hasTransform) {
181 const CSSAnimationData* transformAnim = toStyle->transitionForProperty(C SSPropertyWebkitTransform);
182 if (transformAnim && !transformAnim->isEmptyOrZeroDuration()) {
183 KeyframeValueList transformVector(AnimatedPropertyWebkitTransform);
184 transformVector.insert(adoptPtr(new TransformAnimationValue(0, &from Style->transform())));
185 transformVector.insert(adoptPtr(new TransformAnimationValue(1, &toSt yle->transform())));
186 resultAnimations.m_transformAnimation = appendWebAnimation(transform Vector, boxSize, transformAnim, animationNameForTransition(AnimatedPropertyWebki tTransform), timeOffset);
187 }
188 }
189 if (property == CSSPropertyWebkitFilter && hasFilter) {
190 const CSSAnimationData* filterAnim = toStyle->transitionForProperty(CSSP ropertyWebkitFilter);
191 if (filterAnim && !filterAnim->isEmptyOrZeroDuration()) {
192 KeyframeValueList filterVector(AnimatedPropertyWebkitFilter);
193 filterVector.insert(adoptPtr(new FilterAnimationValue(0, &fromStyle- >filter())));
194 filterVector.insert(adoptPtr(new FilterAnimationValue(1, &toStyle->f ilter())));
195 resultAnimations.m_filterAnimation = appendWebAnimation(filterVector , IntSize(), filterAnim, animationNameForTransition(AnimatedPropertyWebkitFilter ), timeOffset);
196 }
197 }
198
199 return resultAnimations;
200 }
201
202 PassOwnPtr<WebAnimation> WebAnimationProvider::appendWebAnimation(const Keyframe ValueList& values, const IntSize& boxSize, const CSSAnimationData* animation, co nst String& animationName, double timeOffset)
203 {
204 int animationId = getWebAnimationId(animationName);
205 OwnPtr<WebAnimation> webAnimation(createWebAnimation(values, animation, anim ationId, timeOffset, boxSize));
206 if (!webAnimation)
207 return PassOwnPtr<WebAnimation>();
208
209 if (!animationId)
210 m_animationIdMap.set(animationName, webAnimation->id());
211 return webAnimation.release();
212 }
213
214 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/rendering/animation/WebAnimationProvider.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698