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 | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 30 | * BaseRenderNodeAnimator |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 31 | ************************************************************/ |
| 32 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 33 | BaseRenderNodeAnimator::BaseRenderNodeAnimator(float finalValue) |
| 34 | : mFinalValue(finalValue) |
| 35 | , mDeltaValue(0) |
| 36 | , mFromValue(0) |
| 37 | , mInterpolator(0) |
| 38 | , mPlayState(NEEDS_START) |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 39 | , mStartTime(0) |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 40 | , mDelayUntil(0) |
| 41 | , mDuration(300) |
| 42 | , mStartDelay(0) { |
| 43 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 44 | } |
| 45 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 46 | BaseRenderNodeAnimator::~BaseRenderNodeAnimator() { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 47 | setInterpolator(NULL); |
| 48 | } |
| 49 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 50 | void BaseRenderNodeAnimator::setInterpolator(Interpolator* interpolator) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 51 | delete mInterpolator; |
| 52 | mInterpolator = interpolator; |
| 53 | } |
| 54 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 55 | void BaseRenderNodeAnimator::setStartValue(float value) { |
| 56 | LOG_ALWAYS_FATAL_IF(mPlayState != NEEDS_START, |
| 57 | "Cannot set the start value after the animator has started!"); |
| 58 | mFromValue = value; |
| 59 | mDeltaValue = (mFinalValue - mFromValue); |
| 60 | mPlayState = PENDING; |
| 61 | } |
| 62 | |
| 63 | void BaseRenderNodeAnimator::setupStartValueIfNecessary(RenderNode* target, TreeInfo& info) { |
| 64 | if (mPlayState == NEEDS_START) { |
| 65 | setStartValue(getValue(target)); |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 69 | void BaseRenderNodeAnimator::setDuration(nsecs_t duration) { |
| 70 | mDuration = duration; |
| 71 | } |
| 72 | |
| 73 | void BaseRenderNodeAnimator::setStartDelay(nsecs_t startDelay) { |
| 74 | mStartDelay = startDelay; |
| 75 | } |
| 76 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 77 | bool BaseRenderNodeAnimator::animate(RenderNode* target, TreeInfo& info) { |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 78 | if (mPlayState == PENDING && mStartDelay > 0 && mDelayUntil == 0) { |
| 79 | mDelayUntil = info.frameTimeMs + mStartDelay; |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | if (mDelayUntil > info.frameTimeMs) { |
| 84 | return false; |
| 85 | } |
| 86 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 87 | if (mPlayState == PENDING) { |
| 88 | mPlayState = RUNNING; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 89 | mStartTime = info.frameTimeMs; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 90 | // No interpolator was set, use the default |
| 91 | if (!mInterpolator) { |
| 92 | setInterpolator(Interpolator::createDefaultInterpolator()); |
| 93 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | float fraction = 1.0f; |
| 97 | if (mPlayState == RUNNING) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 98 | fraction = mDuration > 0 ? (float)(info.frameTimeMs - mStartTime) / mDuration : 1.0f; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 99 | if (fraction >= 1.0f) { |
| 100 | fraction = 1.0f; |
| 101 | mPlayState = FINISHED; |
| 102 | } |
| 103 | } |
| 104 | fraction = mInterpolator->interpolate(fraction); |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 105 | setValue(target, mFromValue + (mDeltaValue * fraction)); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 106 | |
| 107 | if (mPlayState == FINISHED) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 108 | callOnFinishedListener(info); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 109 | return true; |
| 110 | } |
| 111 | return false; |
| 112 | } |
| 113 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 114 | void BaseRenderNodeAnimator::callOnFinishedListener(TreeInfo& info) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 115 | if (mListener.get()) { |
| 116 | if (!info.animationHook) { |
| 117 | mListener->onAnimationFinished(this); |
| 118 | } else { |
| 119 | info.animationHook->callOnFinished(this, mListener.get()); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /************************************************************ |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 125 | * RenderPropertyAnimator |
| 126 | ************************************************************/ |
| 127 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 128 | struct RenderPropertyAnimator::PropertyAccessors { |
| 129 | RenderNode::DirtyPropertyMask dirtyMask; |
| 130 | GetFloatProperty getter; |
| 131 | SetFloatProperty setter; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 132 | }; |
| 133 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 134 | // Maps RenderProperty enum to accessors |
| 135 | const RenderPropertyAnimator::PropertyAccessors RenderPropertyAnimator::PROPERTY_ACCESSOR_LUT[] = { |
| 136 | {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationX, &RenderProperties::setTranslationX }, |
| 137 | {RenderNode::TRANSLATION_Y, &RenderProperties::getTranslationY, &RenderProperties::setTranslationY }, |
| 138 | {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationZ, &RenderProperties::setTranslationZ }, |
| 139 | {RenderNode::SCALE_X, &RenderProperties::getScaleX, &RenderProperties::setScaleX }, |
| 140 | {RenderNode::SCALE_Y, &RenderProperties::getScaleY, &RenderProperties::setScaleY }, |
| 141 | {RenderNode::ROTATION, &RenderProperties::getRotation, &RenderProperties::setRotation }, |
| 142 | {RenderNode::ROTATION_X, &RenderProperties::getRotationX, &RenderProperties::setRotationX }, |
| 143 | {RenderNode::ROTATION_Y, &RenderProperties::getRotationY, &RenderProperties::setRotationY }, |
| 144 | {RenderNode::X, &RenderProperties::getX, &RenderProperties::setX }, |
| 145 | {RenderNode::Y, &RenderProperties::getY, &RenderProperties::setY }, |
| 146 | {RenderNode::Z, &RenderProperties::getZ, &RenderProperties::setZ }, |
| 147 | {RenderNode::ALPHA, &RenderProperties::getAlpha, &RenderProperties::setAlpha }, |
| 148 | }; |
| 149 | |
| 150 | RenderPropertyAnimator::RenderPropertyAnimator(RenderProperty property, float finalValue) |
| 151 | : BaseRenderNodeAnimator(finalValue) |
| 152 | , mPropertyAccess(&(PROPERTY_ACCESSOR_LUT[property])) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 153 | } |
| 154 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 155 | void RenderPropertyAnimator::onAttached(RenderNode* target) { |
John Reck | c6b3264 | 2014-06-02 11:00:09 -0700 | [diff] [blame] | 156 | if (mPlayState == NEEDS_START |
| 157 | && target->isPropertyFieldDirty(mPropertyAccess->dirtyMask)) { |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 158 | setStartValue((target->stagingProperties().*mPropertyAccess->getter)()); |
| 159 | } |
| 160 | (target->mutateStagingProperties().*mPropertyAccess->setter)(finalValue()); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 161 | } |
| 162 | |
John Reck | 2218472 | 2014-06-20 07:19:30 -0700 | [diff] [blame^] | 163 | uint32_t RenderPropertyAnimator::dirtyMask() { |
| 164 | return mPropertyAccess->dirtyMask; |
| 165 | } |
| 166 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 167 | float RenderPropertyAnimator::getValue(RenderNode* target) const { |
| 168 | return (target->properties().*mPropertyAccess->getter)(); |
| 169 | } |
| 170 | |
| 171 | void RenderPropertyAnimator::setValue(RenderNode* target, float value) { |
| 172 | (target->animatorProperties().*mPropertyAccess->setter)(value); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 173 | } |
| 174 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 175 | /************************************************************ |
| 176 | * CanvasPropertyPrimitiveAnimator |
| 177 | ************************************************************/ |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 178 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 179 | CanvasPropertyPrimitiveAnimator::CanvasPropertyPrimitiveAnimator( |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 180 | CanvasPropertyPrimitive* property, float finalValue) |
| 181 | : BaseRenderNodeAnimator(finalValue) |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 182 | , mProperty(property) { |
| 183 | } |
| 184 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 185 | float CanvasPropertyPrimitiveAnimator::getValue(RenderNode* target) const { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 186 | return mProperty->value; |
| 187 | } |
| 188 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 189 | void CanvasPropertyPrimitiveAnimator::setValue(RenderNode* target, float value) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 190 | mProperty->value = value; |
| 191 | } |
| 192 | |
| 193 | /************************************************************ |
| 194 | * CanvasPropertySkPaintAnimator |
| 195 | ************************************************************/ |
| 196 | |
| 197 | CanvasPropertyPaintAnimator::CanvasPropertyPaintAnimator( |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 198 | CanvasPropertyPaint* property, PaintField field, float finalValue) |
| 199 | : BaseRenderNodeAnimator(finalValue) |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 200 | , mProperty(property) |
| 201 | , mField(field) { |
| 202 | } |
| 203 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 204 | float CanvasPropertyPaintAnimator::getValue(RenderNode* target) const { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 205 | switch (mField) { |
| 206 | case STROKE_WIDTH: |
| 207 | return mProperty->value.getStrokeWidth(); |
| 208 | case ALPHA: |
| 209 | return mProperty->value.getAlpha(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 210 | } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 211 | LOG_ALWAYS_FATAL("Unknown field %d", (int) mField); |
| 212 | return -1; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 213 | } |
| 214 | |
John Reck | 531ee70 | 2014-05-13 10:06:08 -0700 | [diff] [blame] | 215 | static uint8_t to_uint8(float value) { |
| 216 | int c = (int) (value + .5f); |
| 217 | return static_cast<uint8_t>( c < 0 ? 0 : c > 255 ? 255 : c ); |
| 218 | } |
| 219 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 220 | void CanvasPropertyPaintAnimator::setValue(RenderNode* target, float value) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 221 | switch (mField) { |
| 222 | case STROKE_WIDTH: |
| 223 | mProperty->value.setStrokeWidth(value); |
| 224 | return; |
| 225 | case ALPHA: |
John Reck | 531ee70 | 2014-05-13 10:06:08 -0700 | [diff] [blame] | 226 | mProperty->value.setAlpha(to_uint8(value)); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 227 | return; |
| 228 | } |
| 229 | LOG_ALWAYS_FATAL("Unknown field %d", (int) mField); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | } /* namespace uirenderer */ |
| 233 | } /* namespace android */ |