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