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