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 | |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 21 | #include <inttypes.h> |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 22 | #include <set> |
| 23 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 24 | #include "RenderNode.h" |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 25 | #include "RenderProperties.h" |
| 26 | |
| 27 | namespace android { |
| 28 | namespace uirenderer { |
| 29 | |
| 30 | /************************************************************ |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 31 | * BaseRenderNodeAnimator |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 32 | ************************************************************/ |
| 33 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 34 | BaseRenderNodeAnimator::BaseRenderNodeAnimator(float finalValue) |
| 35 | : mFinalValue(finalValue) |
| 36 | , mDeltaValue(0) |
| 37 | , mFromValue(0) |
| 38 | , mInterpolator(0) |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 39 | , mStagingPlayState(NOT_STARTED) |
| 40 | , mPlayState(NOT_STARTED) |
| 41 | , mHasStartValue(false) |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 42 | , mStartTime(0) |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 43 | , mDuration(300) |
| 44 | , mStartDelay(0) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 45 | } |
| 46 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 47 | BaseRenderNodeAnimator::~BaseRenderNodeAnimator() { |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 48 | delete mInterpolator; |
| 49 | } |
| 50 | |
| 51 | void BaseRenderNodeAnimator::checkMutable() { |
| 52 | // Should be impossible to hit as the Java-side also has guards for this |
| 53 | LOG_ALWAYS_FATAL_IF(mStagingPlayState != NOT_STARTED, |
| 54 | "Animator has already been started!"); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 55 | } |
| 56 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 57 | void BaseRenderNodeAnimator::setInterpolator(Interpolator* interpolator) { |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 58 | checkMutable(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 59 | delete mInterpolator; |
| 60 | mInterpolator = interpolator; |
| 61 | } |
| 62 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 63 | void BaseRenderNodeAnimator::setStartValue(float value) { |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 64 | checkMutable(); |
| 65 | doSetStartValue(value); |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 66 | } |
| 67 | |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 68 | void BaseRenderNodeAnimator::doSetStartValue(float value) { |
| 69 | mFromValue = value; |
| 70 | mDeltaValue = (mFinalValue - mFromValue); |
| 71 | mHasStartValue = true; |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 74 | void BaseRenderNodeAnimator::setDuration(nsecs_t duration) { |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 75 | checkMutable(); |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 76 | mDuration = duration; |
| 77 | } |
| 78 | |
| 79 | void BaseRenderNodeAnimator::setStartDelay(nsecs_t startDelay) { |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 80 | checkMutable(); |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 81 | mStartDelay = startDelay; |
| 82 | } |
| 83 | |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 84 | void BaseRenderNodeAnimator::pushStaging(RenderNode* target, TreeInfo& info) { |
| 85 | if (!mHasStartValue) { |
| 86 | doSetStartValue(getValue(target)); |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 87 | } |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 88 | if (mStagingPlayState > mPlayState) { |
| 89 | mPlayState = mStagingPlayState; |
| 90 | // Oh boy, we're starting! Man the battle stations! |
| 91 | if (mPlayState == RUNNING) { |
| 92 | transitionToRunning(info); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 93 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 94 | } |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void BaseRenderNodeAnimator::transitionToRunning(TreeInfo& info) { |
| 98 | LOG_ALWAYS_FATAL_IF(info.frameTimeMs <= 0, "%" PRId64 " isn't a real frame time!", info.frameTimeMs); |
| 99 | if (mStartDelay < 0 || mStartDelay > 50000) { |
| 100 | ALOGW("Your start delay is strange and confusing: %" PRId64, mStartDelay); |
| 101 | } |
| 102 | mStartTime = info.frameTimeMs + mStartDelay; |
| 103 | if (mStartTime < 0) { |
| 104 | ALOGW("Ended up with a really weird start time of %" PRId64 |
| 105 | " with frame time %" PRId64 " and start delay %" PRId64, |
| 106 | mStartTime, info.frameTimeMs, mStartDelay); |
| 107 | // Set to 0 so that the animate() basically instantly finishes |
| 108 | mStartTime = 0; |
| 109 | } |
| 110 | // No interpolator was set, use the default |
| 111 | if (!mInterpolator) { |
| 112 | setInterpolator(Interpolator::createDefaultInterpolator()); |
| 113 | } |
| 114 | if (mDuration < 0 || mDuration > 50000) { |
| 115 | ALOGW("Your duration is strange and confusing: %" PRId64, mDuration); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | bool BaseRenderNodeAnimator::animate(RenderNode* target, TreeInfo& info) { |
| 120 | if (mPlayState < RUNNING) { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | if (mStartTime > info.frameTimeMs) { |
| 125 | info.out.hasAnimations |= true; |
| 126 | return false; |
| 127 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 128 | |
| 129 | float fraction = 1.0f; |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 130 | if (mPlayState == RUNNING && mDuration > 0) { |
| 131 | fraction = (float)(info.frameTimeMs - mStartTime) / mDuration; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 132 | } |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 133 | if (fraction >= 1.0f) { |
| 134 | fraction = 1.0f; |
| 135 | mPlayState = FINISHED; |
| 136 | } |
| 137 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 138 | fraction = mInterpolator->interpolate(fraction); |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 139 | setValue(target, mFromValue + (mDeltaValue * fraction)); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 140 | |
| 141 | if (mPlayState == FINISHED) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 142 | callOnFinishedListener(info); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 143 | return true; |
| 144 | } |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 145 | |
| 146 | info.out.hasAnimations |= true; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 147 | return false; |
| 148 | } |
| 149 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 150 | void BaseRenderNodeAnimator::callOnFinishedListener(TreeInfo& info) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 151 | if (mListener.get()) { |
| 152 | if (!info.animationHook) { |
| 153 | mListener->onAnimationFinished(this); |
| 154 | } else { |
| 155 | info.animationHook->callOnFinished(this, mListener.get()); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /************************************************************ |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 161 | * RenderPropertyAnimator |
| 162 | ************************************************************/ |
| 163 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 164 | struct RenderPropertyAnimator::PropertyAccessors { |
| 165 | RenderNode::DirtyPropertyMask dirtyMask; |
| 166 | GetFloatProperty getter; |
| 167 | SetFloatProperty setter; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 168 | }; |
| 169 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 170 | // Maps RenderProperty enum to accessors |
| 171 | const RenderPropertyAnimator::PropertyAccessors RenderPropertyAnimator::PROPERTY_ACCESSOR_LUT[] = { |
| 172 | {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationX, &RenderProperties::setTranslationX }, |
| 173 | {RenderNode::TRANSLATION_Y, &RenderProperties::getTranslationY, &RenderProperties::setTranslationY }, |
| 174 | {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationZ, &RenderProperties::setTranslationZ }, |
| 175 | {RenderNode::SCALE_X, &RenderProperties::getScaleX, &RenderProperties::setScaleX }, |
| 176 | {RenderNode::SCALE_Y, &RenderProperties::getScaleY, &RenderProperties::setScaleY }, |
| 177 | {RenderNode::ROTATION, &RenderProperties::getRotation, &RenderProperties::setRotation }, |
| 178 | {RenderNode::ROTATION_X, &RenderProperties::getRotationX, &RenderProperties::setRotationX }, |
| 179 | {RenderNode::ROTATION_Y, &RenderProperties::getRotationY, &RenderProperties::setRotationY }, |
| 180 | {RenderNode::X, &RenderProperties::getX, &RenderProperties::setX }, |
| 181 | {RenderNode::Y, &RenderProperties::getY, &RenderProperties::setY }, |
| 182 | {RenderNode::Z, &RenderProperties::getZ, &RenderProperties::setZ }, |
| 183 | {RenderNode::ALPHA, &RenderProperties::getAlpha, &RenderProperties::setAlpha }, |
| 184 | }; |
| 185 | |
| 186 | RenderPropertyAnimator::RenderPropertyAnimator(RenderProperty property, float finalValue) |
| 187 | : BaseRenderNodeAnimator(finalValue) |
| 188 | , mPropertyAccess(&(PROPERTY_ACCESSOR_LUT[property])) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 189 | } |
| 190 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 191 | void RenderPropertyAnimator::onAttached(RenderNode* target) { |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 192 | if (!mHasStartValue |
John Reck | c6b3264 | 2014-06-02 11:00:09 -0700 | [diff] [blame] | 193 | && target->isPropertyFieldDirty(mPropertyAccess->dirtyMask)) { |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 194 | setStartValue((target->stagingProperties().*mPropertyAccess->getter)()); |
| 195 | } |
| 196 | (target->mutateStagingProperties().*mPropertyAccess->setter)(finalValue()); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 197 | } |
| 198 | |
John Reck | 2218472 | 2014-06-20 07:19:30 -0700 | [diff] [blame] | 199 | uint32_t RenderPropertyAnimator::dirtyMask() { |
| 200 | return mPropertyAccess->dirtyMask; |
| 201 | } |
| 202 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 203 | float RenderPropertyAnimator::getValue(RenderNode* target) const { |
| 204 | return (target->properties().*mPropertyAccess->getter)(); |
| 205 | } |
| 206 | |
| 207 | void RenderPropertyAnimator::setValue(RenderNode* target, float value) { |
| 208 | (target->animatorProperties().*mPropertyAccess->setter)(value); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 209 | } |
| 210 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 211 | /************************************************************ |
| 212 | * CanvasPropertyPrimitiveAnimator |
| 213 | ************************************************************/ |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 214 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 215 | CanvasPropertyPrimitiveAnimator::CanvasPropertyPrimitiveAnimator( |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 216 | CanvasPropertyPrimitive* property, float finalValue) |
| 217 | : BaseRenderNodeAnimator(finalValue) |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 218 | , mProperty(property) { |
| 219 | } |
| 220 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 221 | float CanvasPropertyPrimitiveAnimator::getValue(RenderNode* target) const { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 222 | return mProperty->value; |
| 223 | } |
| 224 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 225 | void CanvasPropertyPrimitiveAnimator::setValue(RenderNode* target, float value) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 226 | mProperty->value = value; |
| 227 | } |
| 228 | |
| 229 | /************************************************************ |
| 230 | * CanvasPropertySkPaintAnimator |
| 231 | ************************************************************/ |
| 232 | |
| 233 | CanvasPropertyPaintAnimator::CanvasPropertyPaintAnimator( |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 234 | CanvasPropertyPaint* property, PaintField field, float finalValue) |
| 235 | : BaseRenderNodeAnimator(finalValue) |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 236 | , mProperty(property) |
| 237 | , mField(field) { |
| 238 | } |
| 239 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 240 | float CanvasPropertyPaintAnimator::getValue(RenderNode* target) const { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 241 | switch (mField) { |
| 242 | case STROKE_WIDTH: |
| 243 | return mProperty->value.getStrokeWidth(); |
| 244 | case ALPHA: |
| 245 | return mProperty->value.getAlpha(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 246 | } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 247 | LOG_ALWAYS_FATAL("Unknown field %d", (int) mField); |
| 248 | return -1; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 249 | } |
| 250 | |
John Reck | 531ee70 | 2014-05-13 10:06:08 -0700 | [diff] [blame] | 251 | static uint8_t to_uint8(float value) { |
| 252 | int c = (int) (value + .5f); |
| 253 | return static_cast<uint8_t>( c < 0 ? 0 : c > 255 ? 255 : c ); |
| 254 | } |
| 255 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 256 | void CanvasPropertyPaintAnimator::setValue(RenderNode* target, float value) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 257 | switch (mField) { |
| 258 | case STROKE_WIDTH: |
| 259 | mProperty->value.setStrokeWidth(value); |
| 260 | return; |
| 261 | case ALPHA: |
John Reck | 531ee70 | 2014-05-13 10:06:08 -0700 | [diff] [blame] | 262 | mProperty->value.setAlpha(to_uint8(value)); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 263 | return; |
| 264 | } |
| 265 | LOG_ALWAYS_FATAL("Unknown field %d", (int) mField); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | } /* namespace uirenderer */ |
| 269 | } /* namespace android */ |