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