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 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 17 | #include "Animator.h" |
| 18 | |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 20 | #include <set> |
| 21 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 22 | #include "AnimationContext.h" |
Tom Hudson | 2dc236b | 2014-10-15 15:46:42 -0400 | [diff] [blame] | 23 | #include "Interpolator.h" |
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) |
Chris Craik | d41c4d8 | 2015-01-05 15:51:13 -0800 | [diff] [blame] | 35 | : mTarget(nullptr) |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 36 | , mFinalValue(finalValue) |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 37 | , mDeltaValue(0) |
| 38 | , mFromValue(0) |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 39 | , mStagingPlayState(PlayState::NotStarted) |
| 40 | , mPlayState(PlayState::NotStarted) |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 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) |
Chris Craik | 572d9ac | 2014-09-12 17:40:20 -0700 | [diff] [blame] | 44 | , mStartDelay(0) |
Doris Liu | 0d20a27 | 2016-02-11 20:30:48 +0000 | [diff] [blame^] | 45 | , mMayRunAsync(true) |
| 46 | , mPlayTime(0) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 47 | } |
| 48 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 49 | BaseRenderNodeAnimator::~BaseRenderNodeAnimator() { |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void BaseRenderNodeAnimator::checkMutable() { |
| 53 | // Should be impossible to hit as the Java-side also has guards for this |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 54 | LOG_ALWAYS_FATAL_IF(mStagingPlayState != PlayState::NotStarted, |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 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(); |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 60 | mInterpolator.reset(interpolator); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 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 | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 84 | void BaseRenderNodeAnimator::attach(RenderNode* target) { |
| 85 | mTarget = target; |
| 86 | onAttached(); |
| 87 | } |
| 88 | |
Doris Liu | 0d20a27 | 2016-02-11 20:30:48 +0000 | [diff] [blame^] | 89 | void BaseRenderNodeAnimator::start() { |
| 90 | mStagingPlayState = PlayState::Running; |
| 91 | mStagingRequests.push_back(Request::Start); |
| 92 | onStagingPlayStateChanged(); |
| 93 | } |
| 94 | |
| 95 | void BaseRenderNodeAnimator::cancel() { |
| 96 | mStagingPlayState = PlayState::Finished; |
| 97 | mStagingRequests.push_back(Request::Cancel); |
| 98 | onStagingPlayStateChanged(); |
| 99 | } |
| 100 | |
| 101 | void BaseRenderNodeAnimator::reset() { |
| 102 | mStagingPlayState = PlayState::Finished; |
| 103 | mStagingRequests.push_back(Request::Reset); |
| 104 | onStagingPlayStateChanged(); |
| 105 | } |
| 106 | |
| 107 | void BaseRenderNodeAnimator::reverse() { |
| 108 | mStagingPlayState = PlayState::Reversing; |
| 109 | mStagingRequests.push_back(Request::Reverse); |
| 110 | onStagingPlayStateChanged(); |
| 111 | } |
| 112 | |
| 113 | void BaseRenderNodeAnimator::end() { |
| 114 | mStagingPlayState = PlayState::Finished; |
| 115 | mStagingRequests.push_back(Request::End); |
| 116 | onStagingPlayStateChanged(); |
| 117 | } |
| 118 | |
| 119 | void BaseRenderNodeAnimator::resolveStagingRequest(Request request) { |
| 120 | switch (request) { |
| 121 | case Request::Start: |
| 122 | mPlayTime = (mPlayState == PlayState::Running || mPlayState == PlayState::Reversing) ? |
| 123 | mPlayTime : 0; |
| 124 | mPlayState = PlayState::Running; |
| 125 | break; |
| 126 | case Request::Reverse: |
| 127 | mPlayTime = (mPlayState == PlayState::Running || mPlayState == PlayState::Reversing) ? |
| 128 | mPlayTime : mDuration; |
| 129 | mPlayState = PlayState::Reversing; |
| 130 | break; |
| 131 | case Request::Reset: |
| 132 | mPlayTime = 0; |
| 133 | mPlayState = PlayState::Finished; |
| 134 | break; |
| 135 | case Request::Cancel: |
| 136 | mPlayState = PlayState::Finished; |
| 137 | break; |
| 138 | case Request::End: |
| 139 | mPlayTime = mPlayState == PlayState::Reversing ? 0 : mDuration; |
| 140 | mPlayState = PlayState::Finished; |
| 141 | break; |
| 142 | default: |
| 143 | LOG_ALWAYS_FATAL("Invalid staging request: %d", static_cast<int>(request)); |
| 144 | }; |
| 145 | } |
| 146 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 147 | void BaseRenderNodeAnimator::pushStaging(AnimationContext& context) { |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 148 | if (!mHasStartValue) { |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 149 | doSetStartValue(getValue(mTarget)); |
Alan Viverette | ad2f8e3 | 2014-05-16 13:28:33 -0700 | [diff] [blame] | 150 | } |
Doris Liu | 0d20a27 | 2016-02-11 20:30:48 +0000 | [diff] [blame^] | 151 | |
| 152 | if (!mStagingRequests.empty()) { |
| 153 | // Keep track of the play state and play time before they are changed when |
| 154 | // staging requests are resolved. |
| 155 | nsecs_t currentPlayTime = mPlayTime; |
| 156 | PlayState prevFramePlayState = mPlayState; |
| 157 | |
| 158 | // Resolve staging requests one by one. |
| 159 | for (Request request : mStagingRequests) { |
| 160 | resolveStagingRequest(request); |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 161 | } |
Doris Liu | 0d20a27 | 2016-02-11 20:30:48 +0000 | [diff] [blame^] | 162 | mStagingRequests.clear(); |
| 163 | |
| 164 | if (mStagingPlayState == PlayState::Finished) { |
| 165 | // Set the staging play time and end the animation |
| 166 | updatePlayTime(mPlayTime); |
John Reck | 4d2c472 | 2014-08-29 10:40:56 -0700 | [diff] [blame] | 167 | callOnFinishedListener(context); |
Doris Liu | 0d20a27 | 2016-02-11 20:30:48 +0000 | [diff] [blame^] | 168 | } else if (mStagingPlayState == PlayState::Running |
| 169 | || mStagingPlayState == PlayState::Reversing) { |
| 170 | bool changed = currentPlayTime != mPlayTime || prevFramePlayState != mStagingPlayState; |
| 171 | if (prevFramePlayState != mStagingPlayState) { |
| 172 | transitionToRunning(context); |
| 173 | } |
| 174 | if (changed) { |
| 175 | // Now we need to seek to the stagingPlayTime (i.e. the animation progress that was |
| 176 | // requested from UI thread). It is achieved by modifying mStartTime, such that |
| 177 | // current time - mStartTime = stagingPlayTime (or mDuration -stagingPlayTime in the |
| 178 | // case of reversing) |
| 179 | nsecs_t currentFrameTime = context.frameTimeMs(); |
| 180 | if (mPlayState == PlayState::Reversing) { |
| 181 | // Reverse is not supported for animations with a start delay, so here we |
| 182 | // assume no start delay. |
| 183 | mStartTime = currentFrameTime - (mDuration - mPlayTime); |
| 184 | } else { |
| 185 | // Animation should play forward |
| 186 | if (mPlayTime == 0) { |
| 187 | // If the request is to start from the beginning, include start delay. |
| 188 | mStartTime = currentFrameTime + mStartDelay; |
| 189 | } else { |
| 190 | // If the request is to seek to a non-zero play time, then we skip start |
| 191 | // delay. |
| 192 | mStartTime = currentFrameTime - mPlayTime; |
| 193 | } |
| 194 | } |
| 195 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 196 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 197 | } |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 198 | } |
| 199 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 200 | void BaseRenderNodeAnimator::transitionToRunning(AnimationContext& context) { |
| 201 | nsecs_t frameTimeMs = context.frameTimeMs(); |
| 202 | LOG_ALWAYS_FATAL_IF(frameTimeMs <= 0, "%" PRId64 " isn't a real frame time!", frameTimeMs); |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 203 | if (mStartDelay < 0 || mStartDelay > 50000) { |
| 204 | ALOGW("Your start delay is strange and confusing: %" PRId64, mStartDelay); |
| 205 | } |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 206 | mStartTime = frameTimeMs + mStartDelay; |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 207 | if (mStartTime < 0) { |
| 208 | ALOGW("Ended up with a really weird start time of %" PRId64 |
| 209 | " with frame time %" PRId64 " and start delay %" PRId64, |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 210 | mStartTime, frameTimeMs, mStartDelay); |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 211 | // Set to 0 so that the animate() basically instantly finishes |
| 212 | mStartTime = 0; |
| 213 | } |
| 214 | // No interpolator was set, use the default |
| 215 | if (!mInterpolator) { |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 216 | mInterpolator.reset(Interpolator::createDefaultInterpolator()); |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 217 | } |
| 218 | if (mDuration < 0 || mDuration > 50000) { |
| 219 | ALOGW("Your duration is strange and confusing: %" PRId64, mDuration); |
| 220 | } |
| 221 | } |
| 222 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 223 | bool BaseRenderNodeAnimator::animate(AnimationContext& context) { |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 224 | if (mPlayState < PlayState::Running) { |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 225 | return false; |
| 226 | } |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 227 | if (mPlayState == PlayState::Finished) { |
John Reck | 32fb630 | 2014-07-07 09:50:32 -0700 | [diff] [blame] | 228 | return true; |
| 229 | } |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 230 | |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 231 | // This should be set before setValue() so animators can query this time when setValue |
| 232 | // is called. |
Doris Liu | 0d20a27 | 2016-02-11 20:30:48 +0000 | [diff] [blame^] | 233 | nsecs_t currentPlayTime = context.frameTimeMs() - mStartTime; |
| 234 | bool finished = updatePlayTime(currentPlayTime); |
| 235 | if (finished && mPlayState != PlayState::Finished) { |
| 236 | mPlayState = PlayState::Finished; |
| 237 | callOnFinishedListener(context); |
| 238 | } |
| 239 | return finished; |
| 240 | } |
Doris Liu | 766431a | 2016-02-04 22:17:11 +0000 | [diff] [blame] | 241 | |
Doris Liu | 0d20a27 | 2016-02-11 20:30:48 +0000 | [diff] [blame^] | 242 | bool BaseRenderNodeAnimator::updatePlayTime(nsecs_t playTime) { |
| 243 | mPlayTime = mPlayState == PlayState::Reversing ? mDuration - playTime : playTime; |
| 244 | onPlayTimeChanged(mPlayTime); |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 245 | // If BaseRenderNodeAnimator is handling the delay (not typical), then |
| 246 | // because the staging properties reflect the final value, we always need |
| 247 | // to call setValue even if the animation isn't yet running or is still |
| 248 | // being delayed as we need to override the staging value |
Doris Liu | 0d20a27 | 2016-02-11 20:30:48 +0000 | [diff] [blame^] | 249 | if (playTime < 0) { |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 250 | setValue(mTarget, mFromValue); |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 251 | return false; |
| 252 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 253 | |
| 254 | float fraction = 1.0f; |
Doris Liu | 0d20a27 | 2016-02-11 20:30:48 +0000 | [diff] [blame^] | 255 | if ((mPlayState == PlayState::Running || mPlayState == PlayState::Reversing) && mDuration > 0) { |
| 256 | fraction = mPlayTime / (float) mDuration; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 257 | } |
Doris Liu | 0d20a27 | 2016-02-11 20:30:48 +0000 | [diff] [blame^] | 258 | fraction = MathUtils::clamp(fraction, 0.0f, 1.0f); |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 259 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 260 | fraction = mInterpolator->interpolate(fraction); |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 261 | setValue(mTarget, mFromValue + (mDeltaValue * fraction)); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 262 | |
Doris Liu | 0d20a27 | 2016-02-11 20:30:48 +0000 | [diff] [blame^] | 263 | return playTime >= mDuration; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 264 | } |
| 265 | |
John Reck | e2478d4 | 2014-09-03 16:46:05 -0700 | [diff] [blame] | 266 | void BaseRenderNodeAnimator::forceEndNow(AnimationContext& context) { |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 267 | if (mPlayState < PlayState::Finished) { |
| 268 | mPlayState = PlayState::Finished; |
John Reck | e2478d4 | 2014-09-03 16:46:05 -0700 | [diff] [blame] | 269 | callOnFinishedListener(context); |
| 270 | } |
| 271 | } |
| 272 | |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 273 | void BaseRenderNodeAnimator::callOnFinishedListener(AnimationContext& context) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 274 | if (mListener.get()) { |
John Reck | 119907c | 2014-08-14 09:02:01 -0700 | [diff] [blame] | 275 | context.callOnFinished(this, mListener.get()); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | /************************************************************ |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 280 | * RenderPropertyAnimator |
| 281 | ************************************************************/ |
| 282 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 283 | struct RenderPropertyAnimator::PropertyAccessors { |
| 284 | RenderNode::DirtyPropertyMask dirtyMask; |
| 285 | GetFloatProperty getter; |
| 286 | SetFloatProperty setter; |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 287 | }; |
| 288 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 289 | // Maps RenderProperty enum to accessors |
| 290 | const RenderPropertyAnimator::PropertyAccessors RenderPropertyAnimator::PROPERTY_ACCESSOR_LUT[] = { |
| 291 | {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationX, &RenderProperties::setTranslationX }, |
| 292 | {RenderNode::TRANSLATION_Y, &RenderProperties::getTranslationY, &RenderProperties::setTranslationY }, |
| 293 | {RenderNode::TRANSLATION_X, &RenderProperties::getTranslationZ, &RenderProperties::setTranslationZ }, |
| 294 | {RenderNode::SCALE_X, &RenderProperties::getScaleX, &RenderProperties::setScaleX }, |
| 295 | {RenderNode::SCALE_Y, &RenderProperties::getScaleY, &RenderProperties::setScaleY }, |
| 296 | {RenderNode::ROTATION, &RenderProperties::getRotation, &RenderProperties::setRotation }, |
| 297 | {RenderNode::ROTATION_X, &RenderProperties::getRotationX, &RenderProperties::setRotationX }, |
| 298 | {RenderNode::ROTATION_Y, &RenderProperties::getRotationY, &RenderProperties::setRotationY }, |
| 299 | {RenderNode::X, &RenderProperties::getX, &RenderProperties::setX }, |
| 300 | {RenderNode::Y, &RenderProperties::getY, &RenderProperties::setY }, |
| 301 | {RenderNode::Z, &RenderProperties::getZ, &RenderProperties::setZ }, |
| 302 | {RenderNode::ALPHA, &RenderProperties::getAlpha, &RenderProperties::setAlpha }, |
| 303 | }; |
| 304 | |
| 305 | RenderPropertyAnimator::RenderPropertyAnimator(RenderProperty property, float finalValue) |
| 306 | : BaseRenderNodeAnimator(finalValue) |
| 307 | , mPropertyAccess(&(PROPERTY_ACCESSOR_LUT[property])) { |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 308 | } |
| 309 | |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 310 | void RenderPropertyAnimator::onAttached() { |
John Reck | 68bfe0a | 2014-06-24 15:34:58 -0700 | [diff] [blame] | 311 | if (!mHasStartValue |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 312 | && mTarget->isPropertyFieldDirty(mPropertyAccess->dirtyMask)) { |
| 313 | setStartValue((mTarget->stagingProperties().*mPropertyAccess->getter)()); |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 314 | } |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | void RenderPropertyAnimator::onStagingPlayStateChanged() { |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 318 | if (mStagingPlayState == PlayState::Running) { |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 319 | (mTarget->mutateStagingProperties().*mPropertyAccess->setter)(finalValue()); |
Chris Craik | b9ce116d | 2015-08-20 15:14:06 -0700 | [diff] [blame] | 320 | } else if (mStagingPlayState == PlayState::Finished) { |
John Reck | 32fb630 | 2014-07-07 09:50:32 -0700 | [diff] [blame] | 321 | // We're being canceled, so make sure that whatever values the UI thread |
| 322 | // is observing for us is pushed over |
| 323 | mTarget->setPropertyFieldsDirty(dirtyMask()); |
John Reck | 8d8af3c | 2014-07-01 15:23:45 -0700 | [diff] [blame] | 324 | } |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 325 | } |
| 326 | |
John Reck | 2218472 | 2014-06-20 07:19:30 -0700 | [diff] [blame] | 327 | uint32_t RenderPropertyAnimator::dirtyMask() { |
| 328 | return mPropertyAccess->dirtyMask; |
| 329 | } |
| 330 | |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 331 | float RenderPropertyAnimator::getValue(RenderNode* target) const { |
| 332 | return (target->properties().*mPropertyAccess->getter)(); |
| 333 | } |
| 334 | |
| 335 | void RenderPropertyAnimator::setValue(RenderNode* target, float value) { |
| 336 | (target->animatorProperties().*mPropertyAccess->setter)(value); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 337 | } |
| 338 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 339 | /************************************************************ |
| 340 | * CanvasPropertyPrimitiveAnimator |
| 341 | ************************************************************/ |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 342 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 343 | CanvasPropertyPrimitiveAnimator::CanvasPropertyPrimitiveAnimator( |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 344 | CanvasPropertyPrimitive* property, float finalValue) |
| 345 | : BaseRenderNodeAnimator(finalValue) |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 346 | , mProperty(property) { |
| 347 | } |
| 348 | |
Andreas Gampe | 64bb413 | 2014-11-22 00:35:09 +0000 | [diff] [blame] | 349 | float CanvasPropertyPrimitiveAnimator::getValue(RenderNode* target) const { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 350 | return mProperty->value; |
| 351 | } |
| 352 | |
Andreas Gampe | 64bb413 | 2014-11-22 00:35:09 +0000 | [diff] [blame] | 353 | void CanvasPropertyPrimitiveAnimator::setValue(RenderNode* target, float value) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 354 | mProperty->value = value; |
| 355 | } |
| 356 | |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 357 | uint32_t CanvasPropertyPrimitiveAnimator::dirtyMask() { |
| 358 | return RenderNode::DISPLAY_LIST; |
| 359 | } |
| 360 | |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 361 | /************************************************************ |
| 362 | * CanvasPropertySkPaintAnimator |
| 363 | ************************************************************/ |
| 364 | |
| 365 | CanvasPropertyPaintAnimator::CanvasPropertyPaintAnimator( |
John Reck | ff941dc | 2014-05-14 16:34:14 -0700 | [diff] [blame] | 366 | CanvasPropertyPaint* property, PaintField field, float finalValue) |
| 367 | : BaseRenderNodeAnimator(finalValue) |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 368 | , mProperty(property) |
| 369 | , mField(field) { |
| 370 | } |
| 371 | |
Andreas Gampe | 64bb413 | 2014-11-22 00:35:09 +0000 | [diff] [blame] | 372 | float CanvasPropertyPaintAnimator::getValue(RenderNode* target) const { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 373 | switch (mField) { |
| 374 | case STROKE_WIDTH: |
| 375 | return mProperty->value.getStrokeWidth(); |
| 376 | case ALPHA: |
| 377 | return mProperty->value.getAlpha(); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 378 | } |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 379 | LOG_ALWAYS_FATAL("Unknown field %d", (int) mField); |
| 380 | return -1; |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 381 | } |
| 382 | |
John Reck | 531ee70 | 2014-05-13 10:06:08 -0700 | [diff] [blame] | 383 | static uint8_t to_uint8(float value) { |
| 384 | int c = (int) (value + .5f); |
| 385 | return static_cast<uint8_t>( c < 0 ? 0 : c > 255 ? 255 : c ); |
| 386 | } |
| 387 | |
Andreas Gampe | 64bb413 | 2014-11-22 00:35:09 +0000 | [diff] [blame] | 388 | void CanvasPropertyPaintAnimator::setValue(RenderNode* target, float value) { |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 389 | switch (mField) { |
| 390 | case STROKE_WIDTH: |
| 391 | mProperty->value.setStrokeWidth(value); |
| 392 | return; |
| 393 | case ALPHA: |
John Reck | 531ee70 | 2014-05-13 10:06:08 -0700 | [diff] [blame] | 394 | mProperty->value.setAlpha(to_uint8(value)); |
John Reck | 52244ff | 2014-05-01 21:27:37 -0700 | [diff] [blame] | 395 | return; |
| 396 | } |
| 397 | LOG_ALWAYS_FATAL("Unknown field %d", (int) mField); |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 398 | } |
| 399 | |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 400 | uint32_t CanvasPropertyPaintAnimator::dirtyMask() { |
| 401 | return RenderNode::DISPLAY_LIST; |
| 402 | } |
| 403 | |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 404 | RevealAnimator::RevealAnimator(int centerX, int centerY, |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 405 | float startValue, float finalValue) |
| 406 | : BaseRenderNodeAnimator(finalValue) |
| 407 | , mCenterX(centerX) |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 408 | , mCenterY(centerY) { |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 409 | setStartValue(startValue); |
| 410 | } |
| 411 | |
| 412 | float RevealAnimator::getValue(RenderNode* target) const { |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 413 | return target->properties().getRevealClip().getRadius(); |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | void RevealAnimator::setValue(RenderNode* target, float value) { |
Chris Craik | af4d04c | 2014-07-29 12:50:14 -0700 | [diff] [blame] | 417 | target->animatorProperties().mutableRevealClip().set(true, |
John Reck | d3de42c | 2014-07-15 14:29:33 -0700 | [diff] [blame] | 418 | mCenterX, mCenterY, value); |
| 419 | } |
| 420 | |
John Reck | a7c2ea2 | 2014-08-08 13:21:00 -0700 | [diff] [blame] | 421 | uint32_t RevealAnimator::dirtyMask() { |
| 422 | return RenderNode::GENERIC; |
| 423 | } |
| 424 | |
John Reck | e45b1fd | 2014-04-15 09:50:16 -0700 | [diff] [blame] | 425 | } /* namespace uirenderer */ |
| 426 | } /* namespace android */ |