2 * Copyright (C) 2009 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "AndroidAnimation.h"
20 #if USE(ACCELERATED_COMPOSITING)
22 #include "Animation.h"
23 #include "GraphicsLayerAndroid.h"
26 #include "TimingFunction.h"
27 #include "TranslateTransformOperation.h"
28 #include "UnitBezier.h"
30 #include <wtf/CurrentTime.h>
31 #include <cutils/log.h>
32 #include <wtf/text/CString.h>
35 #define XLOGC(...) android_printLog(ANDROID_LOG_DEBUG, "AndroidAnimation", __VA_ARGS__)
40 #define XLOG(...) android_printLog(ANDROID_LOG_DEBUG, "AndroidAnimation", __VA_ARGS__)
54 static long gDebugAndroidAnimationInstances;
56 long AndroidAnimation::instancesCount()
58 return gDebugAndroidAnimationInstances;
61 AndroidAnimation::AndroidAnimation(AnimatedPropertyID type,
62 const Animation* animation,
63 KeyframeValueList* operations,
65 : m_beginTime(beginTime)
66 , m_duration(animation->duration())
67 , m_fillsBackwards(animation->fillsBackwards())
68 , m_fillsForwards(animation->fillsForwards())
69 , m_iterationCount(animation->iterationCount())
70 , m_direction(animation->direction())
71 , m_timingFunction(animation->timingFunction())
73 , m_operations(operations)
74 , m_uniqueId(++gUniqueId)
75 , m_hasFinished(false)
77 ASSERT(m_timingFunction);
79 gDebugAndroidAnimationInstances++;
82 AndroidAnimation::~AndroidAnimation()
84 gDebugAndroidAnimationInstances--;
87 void AndroidAnimation::suggestBeginTime(double time)
89 if (m_beginTime <= 0.000001) // overflow or not yet set
93 double AndroidAnimation::elapsedTime(double time)
95 double elapsedTime = (m_beginTime < 0.000001) ? 0 : time - m_beginTime;
98 m_duration = 0.000001;
100 if (elapsedTime < 0) // animation not yet started.
106 bool AndroidAnimation::checkIterationsAndProgress(double time, float* finalProgress)
108 double progress = elapsedTime(time);
109 double dur = m_duration;
110 if (m_iterationCount > 0)
111 dur *= m_iterationCount;
116 // If not infinite, return false if we are done
117 if (m_iterationCount > 0 && progress > dur) {
118 *finalProgress = 1.0;
119 if (!m_hasFinished) {
120 // first time past duration, continue with progress 1.0 so the
121 // element's final position lines up with it's last keyframe
122 m_hasFinished = true;
129 double fractionalTime = progress / m_duration;
130 int integralTime = static_cast<int>(fractionalTime);
132 fractionalTime -= integralTime;
134 if ((m_direction == Animation::AnimationDirectionAlternate) && (integralTime & 1))
135 fractionalTime = 1 - fractionalTime;
137 *finalProgress = fractionalTime;
141 double AndroidAnimation::applyTimingFunction(float from, float to, double progress,
142 const TimingFunction* tf)
144 double fractionalTime = progress;
145 double offset = from;
146 double scale = 1.0 / (to - from);
148 if (scale != 1 || offset)
149 fractionalTime = (fractionalTime - offset) * scale;
151 const TimingFunction* timingFunction = tf;
154 timingFunction = m_timingFunction.get();
156 if (timingFunction && timingFunction->isCubicBezierTimingFunction()) {
157 const CubicBezierTimingFunction* bezierFunction = static_cast<const CubicBezierTimingFunction*>(timingFunction);
158 UnitBezier bezier(bezierFunction->x1(),
159 bezierFunction->y1(),
160 bezierFunction->x2(),
161 bezierFunction->y2());
163 fractionalTime = bezier.solve(fractionalTime, 1.0f / (200.0f * m_duration));
164 } else if (timingFunction && timingFunction->isStepsTimingFunction()) {
165 const StepsTimingFunction* stepFunction = static_cast<const StepsTimingFunction*>(timingFunction);
166 if (stepFunction->stepAtStart()) {
167 fractionalTime = (floor(stepFunction->numberOfSteps() * fractionalTime) + 1) / stepFunction->numberOfSteps();
168 if (fractionalTime > 1.0)
169 fractionalTime = 1.0;
171 fractionalTime = floor(stepFunction->numberOfSteps() * fractionalTime) / stepFunction->numberOfSteps();
174 return fractionalTime;
177 bool AndroidAnimation::evaluate(LayerAndroid* layer, double time)
180 if (!checkIterationsAndProgress(time, &progress)
181 && !(m_fillsBackwards || m_fillsForwards))
185 // The animation hasn't started yet
186 if (m_fillsBackwards || m_beginTime <= 0.000001) {
187 // in this case we want to apply the initial keyframe to the layer
188 applyForProgress(layer, 0);
190 // we still want to be evaluated until we get progress > 0
195 if (!m_fillsForwards)
200 if (!m_operations->size())
203 applyForProgress(layer, progress);
208 PassRefPtr<AndroidOpacityAnimation> AndroidOpacityAnimation::create(
209 const Animation* animation,
210 KeyframeValueList* operations,
213 return adoptRef(new AndroidOpacityAnimation(animation, operations,
217 AndroidOpacityAnimation::AndroidOpacityAnimation(const Animation* animation,
218 KeyframeValueList* operations,
220 : AndroidAnimation(AnimatedPropertyOpacity, animation, operations, beginTime)
224 void AndroidAnimation::pickValues(double progress, int* start, int* end)
227 unsigned int foundAt = 0;
228 for (unsigned int i = 0; i < m_operations->size(); i++) {
229 const AnimationValue* value = m_operations->at(i);
230 float key = value->keyTime();
231 float d = progress - key;
232 if (distance == -1 || (d >= 0 && d < distance && i + 1 < m_operations->size())) {
240 if (foundAt + 1 < m_operations->size())
246 void AndroidOpacityAnimation::applyForProgress(LayerAndroid* layer, float progress)
248 // First, we need to get the from and to values
250 pickValues(progress, &from, &to);
251 FloatAnimationValue* fromValue = (FloatAnimationValue*) m_operations->at(from);
252 FloatAnimationValue* toValue = (FloatAnimationValue*) m_operations->at(to);
254 XLOG("[layer %d] opacity fromValue %x, key %.2f, toValue %x, key %.2f for progress %.2f",
256 fromValue, fromValue->keyTime(),
257 toValue, toValue->keyTime(), progress);
259 // We now have the correct two values to work with, let's compute the
262 const TimingFunction* timingFunction = fromValue->timingFunction();
263 progress = applyTimingFunction(fromValue->keyTime(), toValue->keyTime(),
264 progress, timingFunction);
267 float value = fromValue->value() + ((toValue->value() - fromValue->value()) * progress);
269 layer->setOpacity(value);
272 PassRefPtr<AndroidTransformAnimation> AndroidTransformAnimation::create(
273 const Animation* animation,
274 KeyframeValueList* operations,
277 return adoptRef(new AndroidTransformAnimation(animation, operations, beginTime));
280 AndroidTransformAnimation::AndroidTransformAnimation(const Animation* animation,
281 KeyframeValueList* operations,
283 : AndroidAnimation(AnimatedPropertyWebkitTransform, animation, operations, beginTime)
287 void AndroidTransformAnimation::applyForProgress(LayerAndroid* layer, float progress)
289 // First, we need to get the from and to values
291 pickValues(progress, &from, &to);
293 TransformAnimationValue* fromValue = (TransformAnimationValue*) m_operations->at(from);
294 TransformAnimationValue* toValue = (TransformAnimationValue*) m_operations->at(to);
296 XLOG("[layer %d] fromValue %x, key %.2f, toValue %x, key %.2f for progress %.2f",
298 fromValue, fromValue->keyTime(),
299 toValue, toValue->keyTime(), progress);
301 // We now have the correct two values to work with, let's compute the
304 const TimingFunction* timingFunction = fromValue->timingFunction();
305 float p = applyTimingFunction(fromValue->keyTime(), toValue->keyTime(),
306 progress, timingFunction);
307 XLOG("progress %.2f => %.2f from: %.2f to: %.2f", progress, p, fromValue->keyTime(),
311 // With both values and the progress, we also need to check out that
312 // the operations are compatible (i.e. we are animating the same number
313 // of values; if not we do a matrix blend)
315 TransformationMatrix transformMatrix;
317 unsigned int fromSize = fromValue->value()->size();
319 if (toValue->value()->size() != fromSize)
322 for (unsigned int j = 0; j < fromSize && valid; j++) {
323 if (!fromValue->value()->operations()[j]->isSameType(
324 *toValue->value()->operations()[j]))
330 IntSize size(layer->getSize().width(), layer->getSize().height());
332 for (size_t i = 0; i < toValue->value()->size(); ++i)
333 toValue->value()->operations()[i]->blend(fromValue->value()->at(i),
334 progress)->apply(transformMatrix, size);
336 TransformationMatrix source;
338 fromValue->value()->apply(size, source);
339 toValue->value()->apply(size, transformMatrix);
341 transformMatrix.blend(source, progress);
344 // Set the final transform on the layer
345 layer->setTransform(transformMatrix);
348 } // namespace WebCore
350 #endif // USE(ACCELERATED_COMPOSITING)