John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "RT-Animator" |
| 18 | |
| 19 | #include "Animator.h" |
| 20 | |
| 21 | #include <set> |
| 22 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 23 | #include "RenderNode.h" |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 24 | #include "RenderProperties.h" |
| 25 | |
| 26 | namespace android { |
| 27 | namespace uirenderer { |
| 28 | |
| 29 | /************************************************************ |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 30 | * Base animator |
| 31 | ************************************************************/ |
| 32 | |
| 33 | BaseAnimator::BaseAnimator() |
| 34 | : mInterpolator(0) |
| 35 | , mPlayState(PENDING) |
| 36 | , mStartTime(0) |
| 37 | , mDuration(300) { |
| 38 | |
| 39 | } |
| 40 | |
| 41 | BaseAnimator::~BaseAnimator() { |
| 42 | setInterpolator(NULL); |
| 43 | } |
| 44 | |
| 45 | void BaseAnimator::setInterpolator(Interpolator* interpolator) { |
| 46 | delete mInterpolator; |
| 47 | mInterpolator = interpolator; |
| 48 | } |
| 49 | |
| 50 | void BaseAnimator::setDuration(nsecs_t duration) { |
| 51 | mDuration = duration; |
| 52 | } |
| 53 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 54 | bool BaseAnimator::animateFrame(TreeInfo& info) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 55 | if (mPlayState == PENDING) { |
| 56 | mPlayState = RUNNING; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 57 | mStartTime = info.frameTimeMs; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 58 | // No interpolator was set, use the default |
| 59 | if (!mInterpolator) { |
| 60 | setInterpolator(Interpolator::createDefaultInterpolator()); |
| 61 | } |
| 62 | onAnimationStarted(); |
| 63 | } |
| 64 | |
| 65 | float fraction = 1.0f; |
| 66 | if (mPlayState == RUNNING) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 67 | fraction = mDuration > 0 ? (float)(info.frameTimeMs - mStartTime) / mDuration : 1.0f; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 68 | if (fraction >= 1.0f) { |
| 69 | fraction = 1.0f; |
| 70 | mPlayState = FINISHED; |
| 71 | } |
| 72 | } |
| 73 | fraction = mInterpolator->interpolate(fraction); |
| 74 | onAnimationUpdated(fraction); |
| 75 | |
| 76 | if (mPlayState == FINISHED) { |
| 77 | onAnimationFinished(); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 78 | callOnFinishedListener(info); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | return false; |
| 82 | } |
| 83 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 84 | void BaseAnimator::callOnFinishedListener(TreeInfo& info) { |
| 85 | if (mListener.get()) { |
| 86 | if (!info.animationHook) { |
| 87 | mListener->onAnimationFinished(this); |
| 88 | } else { |
| 89 | info.animationHook->callOnFinished(this, mListener.get()); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /************************************************************ |
| 95 | * BaseRenderNodeAnimator |
| 96 | ************************************************************/ |
| 97 | |
| 98 | BaseRenderNodeAnimator::BaseRenderNodeAnimator( |
| 99 | BaseRenderNodeAnimator::DeltaValueType deltaType, float delta) |
| 100 | : mTarget(0) |
| 101 | , mDeltaValueType(deltaType) |
| 102 | , mDeltaValue(delta) |
| 103 | , mFromValue(-1) { |
| 104 | } |
| 105 | |
| 106 | bool BaseRenderNodeAnimator::animate(RenderNode* target, TreeInfo& info) { |
| 107 | mTarget = target; |
| 108 | bool finished = animateFrame(info); |
| 109 | mTarget = NULL; |
| 110 | return finished; |
| 111 | } |
| 112 | |
| 113 | void BaseRenderNodeAnimator::onAnimationStarted() { |
| 114 | mFromValue = getValue(); |
| 115 | |
| 116 | if (mDeltaValueType == BaseRenderNodeAnimator::ABSOLUTE) { |
| 117 | mDeltaValue = (mDeltaValue - mFromValue); |
| 118 | mDeltaValueType = BaseRenderNodeAnimator::DELTA; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void BaseRenderNodeAnimator::onAnimationUpdated(float fraction) { |
| 123 | float value = mFromValue + (mDeltaValue * fraction); |
| 124 | setValue(value); |
| 125 | } |
| 126 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 127 | /************************************************************ |
| 128 | * RenderPropertyAnimator |
| 129 | ************************************************************/ |
| 130 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 131 | // Maps RenderProperty enum to accessors |
| 132 | const RenderPropertyAnimator::PropertyAccessors RenderPropertyAnimator::PROPERTY_ACCESSOR_LUT[] = { |
| 133 | {&RenderProperties::getTranslationX, &RenderProperties::setTranslationX }, |
| 134 | {&RenderProperties::getTranslationY, &RenderProperties::setTranslationY }, |
| 135 | {&RenderProperties::getTranslationZ, &RenderProperties::setTranslationZ }, |
| 136 | {&RenderProperties::getScaleX, &RenderProperties::setScaleX }, |
| 137 | {&RenderProperties::getScaleY, &RenderProperties::setScaleY }, |
| 138 | {&RenderProperties::getRotation, &RenderProperties::setRotation }, |
| 139 | {&RenderProperties::getRotationX, &RenderProperties::setRotationX }, |
| 140 | {&RenderProperties::getRotationY, &RenderProperties::setRotationY }, |
| 141 | {&RenderProperties::getX, &RenderProperties::setX }, |
| 142 | {&RenderProperties::getY, &RenderProperties::setY }, |
| 143 | {&RenderProperties::getZ, &RenderProperties::setZ }, |
| 144 | {&RenderProperties::getAlpha, &RenderProperties::setAlpha }, |
| 145 | }; |
| 146 | |
| 147 | RenderPropertyAnimator::RenderPropertyAnimator(RenderProperty property, |
| 148 | DeltaValueType deltaType, float deltaValue) |
| 149 | : BaseRenderNodeAnimator(deltaType, deltaValue) |
| 150 | , mPropertyAccess(PROPERTY_ACCESSOR_LUT[property]) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 151 | } |
| 152 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 153 | float RenderPropertyAnimator::getValue() const { |
| 154 | return (target()->animatorProperties().*mPropertyAccess.getter)(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 155 | } |
| 156 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 157 | void RenderPropertyAnimator::setValue(float value) { |
| 158 | (target()->animatorProperties().*mPropertyAccess.setter)(value); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 159 | } |
| 160 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 161 | /************************************************************ |
| 162 | * CanvasPropertyPrimitiveAnimator |
| 163 | ************************************************************/ |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 164 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 165 | CanvasPropertyPrimitiveAnimator::CanvasPropertyPrimitiveAnimator( |
| 166 | CanvasPropertyPrimitive* property, DeltaValueType deltaType, float deltaValue) |
| 167 | : BaseRenderNodeAnimator(deltaType, deltaValue) |
| 168 | , mProperty(property) { |
| 169 | } |
| 170 | |
| 171 | float CanvasPropertyPrimitiveAnimator::getValue() const { |
| 172 | return mProperty->value; |
| 173 | } |
| 174 | |
| 175 | void CanvasPropertyPrimitiveAnimator::setValue(float value) { |
| 176 | mProperty->value = value; |
| 177 | } |
| 178 | |
| 179 | /************************************************************ |
| 180 | * CanvasPropertySkPaintAnimator |
| 181 | ************************************************************/ |
| 182 | |
| 183 | CanvasPropertyPaintAnimator::CanvasPropertyPaintAnimator( |
| 184 | CanvasPropertyPaint* property, PaintField field, |
| 185 | DeltaValueType deltaType, float deltaValue) |
| 186 | : BaseRenderNodeAnimator(deltaType, deltaValue) |
| 187 | , mProperty(property) |
| 188 | , mField(field) { |
| 189 | } |
| 190 | |
| 191 | float CanvasPropertyPaintAnimator::getValue() const { |
| 192 | switch (mField) { |
| 193 | case STROKE_WIDTH: |
| 194 | return mProperty->value.getStrokeWidth(); |
| 195 | case ALPHA: |
| 196 | return mProperty->value.getAlpha(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 197 | } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 198 | LOG_ALWAYS_FATAL("Unknown field %d", (int) mField); |
| 199 | return -1; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 200 | } |
| 201 | |
John Reck | 531ee70 | 2014-05-13 10:06:08 -0700 | [diff] [blame^] | 202 | static uint8_t to_uint8(float value) { |
| 203 | int c = (int) (value + .5f); |
| 204 | return static_cast<uint8_t>( c < 0 ? 0 : c > 255 ? 255 : c ); |
| 205 | } |
| 206 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 207 | void CanvasPropertyPaintAnimator::setValue(float value) { |
| 208 | switch (mField) { |
| 209 | case STROKE_WIDTH: |
| 210 | mProperty->value.setStrokeWidth(value); |
| 211 | return; |
| 212 | case ALPHA: |
John Reck | 531ee70 | 2014-05-13 10:06:08 -0700 | [diff] [blame^] | 213 | mProperty->value.setAlpha(to_uint8(value)); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 214 | return; |
| 215 | } |
| 216 | LOG_ALWAYS_FATAL("Unknown field %d", (int) mField); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | } /* namespace uirenderer */ |
| 220 | } /* namespace android */ |