Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | |
michael@0 | 2 | /* |
michael@0 | 3 | * Copyright 2006 The Android Open Source Project |
michael@0 | 4 | * |
michael@0 | 5 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 6 | * found in the LICENSE file. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | |
michael@0 | 10 | #include "SkDisplayApply.h" |
michael@0 | 11 | #include "SkAnimateActive.h" |
michael@0 | 12 | #include "SkAnimateMaker.h" |
michael@0 | 13 | #include "SkAnimateSet.h" |
michael@0 | 14 | #include "SkAnimatorScript.h" |
michael@0 | 15 | #include "SkDisplayType.h" |
michael@0 | 16 | #include "SkDrawGroup.h" |
michael@0 | 17 | #include "SkParse.h" |
michael@0 | 18 | #include "SkScript.h" |
michael@0 | 19 | #include "SkSystemEventTypes.h" |
michael@0 | 20 | #ifdef SK_DEBUG |
michael@0 | 21 | #include "SkTime.h" |
michael@0 | 22 | #endif |
michael@0 | 23 | #include <ctype.h> |
michael@0 | 24 | |
michael@0 | 25 | enum SkApply_Properties { |
michael@0 | 26 | SK_PROPERTY(animator), |
michael@0 | 27 | SK_PROPERTY(step), |
michael@0 | 28 | SK_PROPERTY(steps), |
michael@0 | 29 | SK_PROPERTY(time) |
michael@0 | 30 | }; |
michael@0 | 31 | |
michael@0 | 32 | #if SK_USE_CONDENSED_INFO == 0 |
michael@0 | 33 | |
michael@0 | 34 | // if no attibutes, enclosed displayable is both scope & target |
michael@0 | 35 | // only if both scope & target are specified, or if target and enclosed displayable, are scope and target different |
michael@0 | 36 | const SkMemberInfo SkApply::fInfo[] = { |
michael@0 | 37 | SK_MEMBER_PROPERTY(animator, Animate), |
michael@0 | 38 | SK_MEMBER(begin, MSec), |
michael@0 | 39 | SK_MEMBER(dontDraw, Boolean), |
michael@0 | 40 | SK_MEMBER(dynamicScope, String), |
michael@0 | 41 | SK_MEMBER(interval, MSec), // recommended redraw interval |
michael@0 | 42 | SK_MEMBER(mode, ApplyMode), |
michael@0 | 43 | #if 0 |
michael@0 | 44 | SK_MEMBER(pickup, Boolean), |
michael@0 | 45 | #endif |
michael@0 | 46 | SK_MEMBER(restore, Boolean), |
michael@0 | 47 | SK_MEMBER(scope, Drawable), // thing that scopes animation (unnamed enclosed displayable goes here) |
michael@0 | 48 | SK_MEMBER_PROPERTY(step, Int), |
michael@0 | 49 | SK_MEMBER_PROPERTY(steps, Int), |
michael@0 | 50 | SK_MEMBER_PROPERTY(time, MSec), |
michael@0 | 51 | SK_MEMBER(transition, ApplyTransition) |
michael@0 | 52 | }; |
michael@0 | 53 | |
michael@0 | 54 | #endif |
michael@0 | 55 | |
michael@0 | 56 | DEFINE_GET_MEMBER(SkApply); |
michael@0 | 57 | |
michael@0 | 58 | SkApply::SkApply() : begin(0), dontDraw(false), interval((SkMSec) -1), mode((Mode) -1), /*pickup(false), */ |
michael@0 | 59 | restore(false), scope(NULL), steps(-1), transition((Transition) -1), fActive(NULL), /*fCurrentScope(NULL),*/ |
michael@0 | 60 | fLastTime(0), fAppended(false), fContainsScope(false), fDeleteScope(false), fEmbedded(false), |
michael@0 | 61 | fEnabled(false), fEnabling(false) { |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | SkApply::~SkApply() { |
michael@0 | 65 | for (SkDrawable** curPtr = fScopes.begin(); curPtr < fScopes.end(); curPtr++) |
michael@0 | 66 | delete *curPtr; |
michael@0 | 67 | if (fDeleteScope) |
michael@0 | 68 | delete scope; |
michael@0 | 69 | // !!! caller must call maker.removeActive(fActive) |
michael@0 | 70 | delete fActive; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | void SkApply::activate(SkAnimateMaker& maker) { |
michael@0 | 74 | if (fActive != NULL) { |
michael@0 | 75 | if (fActive->fDrawIndex == 0 && fActive->fDrawMax == 0) |
michael@0 | 76 | return; // if only one use, nothing more to do |
michael@0 | 77 | if (restore == false) |
michael@0 | 78 | return; // all share same state, regardless of instance number |
michael@0 | 79 | bool save = fActive->initializeSave(); |
michael@0 | 80 | fActive->fixInterpolator(save); |
michael@0 | 81 | } else { |
michael@0 | 82 | fActive = new SkActive(*this, maker); |
michael@0 | 83 | fActive->init(); |
michael@0 | 84 | maker.appendActive(fActive); |
michael@0 | 85 | if (restore) { |
michael@0 | 86 | fActive->initializeSave(); |
michael@0 | 87 | int animators = fAnimators.count(); |
michael@0 | 88 | for (int index = 0; index < animators; index++) |
michael@0 | 89 | fActive->saveInterpolatorValues(index); |
michael@0 | 90 | } |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | void SkApply::append(SkApply* apply) { |
michael@0 | 95 | if (fActive == NULL) |
michael@0 | 96 | return; |
michael@0 | 97 | int oldCount = fActive->fAnimators.count(); |
michael@0 | 98 | fActive->append(apply); |
michael@0 | 99 | if (restore) { |
michael@0 | 100 | fActive->appendSave(oldCount); |
michael@0 | 101 | int newCount = fActive->fAnimators.count(); |
michael@0 | 102 | for (int index = oldCount; index < newCount; index++) |
michael@0 | 103 | fActive->saveInterpolatorValues(index); |
michael@0 | 104 | } |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | void SkApply::applyValues(int animatorIndex, SkOperand* values, int count, |
michael@0 | 108 | SkDisplayTypes valuesType, SkMSec time) |
michael@0 | 109 | { |
michael@0 | 110 | SkAnimateBase* animator = fActive->fAnimators[animatorIndex]; |
michael@0 | 111 | const SkMemberInfo * info = animator->fFieldInfo; |
michael@0 | 112 | SkASSERT(animator); |
michael@0 | 113 | SkASSERT(info != NULL); |
michael@0 | 114 | SkDisplayTypes type = (SkDisplayTypes) info->fType; |
michael@0 | 115 | SkDisplayable* target = getTarget(animator); |
michael@0 | 116 | if (animator->hasExecute() || type == SkType_MemberFunction || type == SkType_MemberProperty) { |
michael@0 | 117 | SkDisplayable* executor = animator->hasExecute() ? animator : target; |
michael@0 | 118 | if (type != SkType_MemberProperty) { |
michael@0 | 119 | SkTDArray<SkScriptValue> typedValues; |
michael@0 | 120 | for (int index = 0; index < count; index++) { |
michael@0 | 121 | SkScriptValue temp; |
michael@0 | 122 | temp.fType = valuesType; |
michael@0 | 123 | temp.fOperand = values[index]; |
michael@0 | 124 | *typedValues.append() = temp; |
michael@0 | 125 | } |
michael@0 | 126 | executor->executeFunction(target, info->functionIndex(), typedValues, info->getType(), NULL); |
michael@0 | 127 | } else { |
michael@0 | 128 | SkScriptValue scriptValue; |
michael@0 | 129 | scriptValue.fOperand = values[0]; |
michael@0 | 130 | scriptValue.fType = info->getType(); |
michael@0 | 131 | target->setProperty(info->propertyIndex(), scriptValue); |
michael@0 | 132 | } |
michael@0 | 133 | } else { |
michael@0 | 134 | SkTypedArray converted; |
michael@0 | 135 | if (type == SkType_ARGB) { |
michael@0 | 136 | if (count == 4) { |
michael@0 | 137 | // !!! assert that it is SkType_Float ? |
michael@0 | 138 | animator->packARGB(&values->fScalar, count, &converted); |
michael@0 | 139 | values = converted.begin(); |
michael@0 | 140 | count = converted.count(); |
michael@0 | 141 | } else { |
michael@0 | 142 | SkASSERT(count == 1); |
michael@0 | 143 | } |
michael@0 | 144 | } |
michael@0 | 145 | // SkASSERT(type == SkType_ARGB || type == SkType_String ||info->isSettable()); |
michael@0 | 146 | if (type == SkType_String || type == SkType_DynamicString) |
michael@0 | 147 | info->setString(target, values->fString); |
michael@0 | 148 | else if (type == SkType_Drawable || type == SkType_Displayable) |
michael@0 | 149 | target->setReference(info, values->fDisplayable); |
michael@0 | 150 | else |
michael@0 | 151 | info->setValue(target, values, count); |
michael@0 | 152 | } |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | bool SkApply::contains(SkDisplayable* child) { |
michael@0 | 156 | for (SkDrawable** curPtr = fScopes.begin(); curPtr < fScopes.end(); curPtr++) { |
michael@0 | 157 | if (*curPtr == child || (*curPtr)->contains(child)) |
michael@0 | 158 | return true; |
michael@0 | 159 | } |
michael@0 | 160 | return fDeleteScope && scope == child; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | SkDisplayable* SkApply::deepCopy(SkAnimateMaker* maker) { |
michael@0 | 164 | SkDrawable* saveScope = scope; |
michael@0 | 165 | scope = NULL; |
michael@0 | 166 | SkApply* result = (SkApply*) INHERITED::deepCopy(maker); |
michael@0 | 167 | result->scope = scope = saveScope; |
michael@0 | 168 | SkAnimateBase** end = fAnimators.end(); |
michael@0 | 169 | for (SkAnimateBase** animPtr = fAnimators.begin(); animPtr < end; animPtr++) { |
michael@0 | 170 | SkAnimateBase* anim = (SkAnimateBase*) (*animPtr)->deepCopy(maker); |
michael@0 | 171 | *result->fAnimators.append() = anim; |
michael@0 | 172 | maker->helperAdd(anim); |
michael@0 | 173 | } |
michael@0 | 174 | return result; |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | void SkApply::disable() { |
michael@0 | 178 | //!!! this is the right thing to do, but has bad side effects because of other problems |
michael@0 | 179 | // currently, if an apply is in a g and scopes a statement in another g, it ends up as members |
michael@0 | 180 | // of both containers. The disabling here incorrectly disables both instances |
michael@0 | 181 | // maybe the fEnabled flag needs to be moved to the fActive data so that both |
michael@0 | 182 | // instances are not affected. |
michael@0 | 183 | // fEnabled = false; |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | bool SkApply::draw(SkAnimateMaker& maker) { |
michael@0 | 187 | if (scope ==NULL) |
michael@0 | 188 | return false; |
michael@0 | 189 | if (scope->isApply() || scope->isDrawable() == false) |
michael@0 | 190 | return false; |
michael@0 | 191 | if (fEnabled == false) |
michael@0 | 192 | enable(maker); |
michael@0 | 193 | SkASSERT(scope); |
michael@0 | 194 | activate(maker); |
michael@0 | 195 | if (mode == kMode_immediate) |
michael@0 | 196 | return fActive->draw(); |
michael@0 | 197 | bool result = interpolate(maker, maker.getInTime()); |
michael@0 | 198 | if (dontDraw == false) { |
michael@0 | 199 | // if (scope->isDrawable()) |
michael@0 | 200 | result |= scope->draw(maker); |
michael@0 | 201 | } |
michael@0 | 202 | if (restore) { |
michael@0 | 203 | for (int index = 0; index < fActive->fAnimators.count(); index++) |
michael@0 | 204 | endSave(index); |
michael@0 | 205 | fActive->advance(); |
michael@0 | 206 | } |
michael@0 | 207 | return result; |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | #ifdef SK_DUMP_ENABLED |
michael@0 | 211 | void SkApply::dump(SkAnimateMaker* maker) { |
michael@0 | 212 | dumpBase(maker); |
michael@0 | 213 | if (dynamicScope.isEmpty() == false) |
michael@0 | 214 | SkDebugf("dynamicScope=\"%s\" ", dynamicScope.c_str()); |
michael@0 | 215 | if (dontDraw) |
michael@0 | 216 | SkDebugf("dontDraw=\"true\" "); |
michael@0 | 217 | if (begin != 0) //perhaps we want this no matter what? |
michael@0 | 218 | SkDebugf("begin=\"%g\" ", (float) begin/1000.0f); //is this correct? |
michael@0 | 219 | if (interval != (SkMSec) -1) |
michael@0 | 220 | SkDebugf("interval=\"%g\" ", (float) interval/1000.0f); |
michael@0 | 221 | if (steps != -1) |
michael@0 | 222 | SkDebugf("steps=\"%d\" ", steps); |
michael@0 | 223 | if (restore) |
michael@0 | 224 | SkDebugf("restore=\"true\" "); |
michael@0 | 225 | if (transition == kTransition_reverse) |
michael@0 | 226 | SkDebugf("transition=\"reverse\" "); |
michael@0 | 227 | if (mode == kMode_immediate) { |
michael@0 | 228 | SkDebugf("mode=\"immediate\" "); |
michael@0 | 229 | } |
michael@0 | 230 | else if (mode == kMode_create) { |
michael@0 | 231 | SkDebugf("mode=\"create\" "); |
michael@0 | 232 | } |
michael@0 | 233 | bool closedYet = false; |
michael@0 | 234 | SkDisplayList::fIndent += 4; |
michael@0 | 235 | int save = SkDisplayList::fDumpIndex; |
michael@0 | 236 | if (scope) { |
michael@0 | 237 | if (closedYet == false) { |
michael@0 | 238 | SkDebugf(">\n"); |
michael@0 | 239 | closedYet = true; |
michael@0 | 240 | } |
michael@0 | 241 | scope->dump(maker); |
michael@0 | 242 | } |
michael@0 | 243 | int index; |
michael@0 | 244 | // if (fActive) { |
michael@0 | 245 | for (index = 0; index < fAnimators.count(); index++) { |
michael@0 | 246 | if (closedYet == false) { |
michael@0 | 247 | SkDebugf(">\n"); |
michael@0 | 248 | closedYet = true; |
michael@0 | 249 | } |
michael@0 | 250 | SkAnimateBase* animator = fAnimators[index]; |
michael@0 | 251 | animator->dump(maker); |
michael@0 | 252 | // } |
michael@0 | 253 | } |
michael@0 | 254 | SkDisplayList::fIndent -= 4; |
michael@0 | 255 | SkDisplayList::fDumpIndex = save; |
michael@0 | 256 | if (closedYet) |
michael@0 | 257 | dumpEnd(maker); |
michael@0 | 258 | else |
michael@0 | 259 | SkDebugf("/>\n"); |
michael@0 | 260 | } |
michael@0 | 261 | #endif |
michael@0 | 262 | |
michael@0 | 263 | bool SkApply::enable(SkAnimateMaker& maker) { |
michael@0 | 264 | fEnabled = true; |
michael@0 | 265 | bool initialized = fActive != NULL; |
michael@0 | 266 | if (dynamicScope.size() > 0) |
michael@0 | 267 | enableDynamic(maker); |
michael@0 | 268 | if (maker.fError.hasError()) |
michael@0 | 269 | return false; |
michael@0 | 270 | int animators = fAnimators.count(); |
michael@0 | 271 | int index; |
michael@0 | 272 | for (index = 0; index < animators; index++) { |
michael@0 | 273 | SkAnimateBase* animator = fAnimators[index]; |
michael@0 | 274 | animator->fStart = maker.fEnableTime; |
michael@0 | 275 | animator->fResetPending = animator->fReset; |
michael@0 | 276 | } |
michael@0 | 277 | if (scope && scope->isApply()) |
michael@0 | 278 | ((SkApply*) scope)->setEmbedded(); |
michael@0 | 279 | /* if (mode == kMode_once) { |
michael@0 | 280 | if (scope) { |
michael@0 | 281 | activate(maker); |
michael@0 | 282 | interpolate(maker, maker.fEnableTime); |
michael@0 | 283 | inactivate(maker); |
michael@0 | 284 | } |
michael@0 | 285 | return true; |
michael@0 | 286 | }*/ |
michael@0 | 287 | if ((mode == kMode_immediate || mode == kMode_create) && scope == NULL) |
michael@0 | 288 | return false; // !!! error? |
michael@0 | 289 | bool enableMe = scope && (scope->hasEnable() || scope->isApply() || scope->isDrawable() == false); |
michael@0 | 290 | if ((mode == kMode_immediate && enableMe) || mode == kMode_create) |
michael@0 | 291 | activate(maker); // for non-drawables like post, prime them here |
michael@0 | 292 | if (mode == kMode_immediate && enableMe) |
michael@0 | 293 | fActive->enable(); |
michael@0 | 294 | if (mode == kMode_create && scope != NULL) { |
michael@0 | 295 | enableCreate(maker); |
michael@0 | 296 | return true; |
michael@0 | 297 | } |
michael@0 | 298 | if (mode == kMode_immediate) { |
michael@0 | 299 | return scope->isApply() || scope->isDrawable() == false; |
michael@0 | 300 | } |
michael@0 | 301 | refresh(maker); |
michael@0 | 302 | SkDisplayList& displayList = maker.fDisplayList; |
michael@0 | 303 | SkDrawable* drawable; |
michael@0 | 304 | #if defined SK_DEBUG && defined SK_DEBUG_ANIMATION_TIMING |
michael@0 | 305 | SkString debugOut; |
michael@0 | 306 | SkMSec time = maker.getAppTime(); |
michael@0 | 307 | debugOut.appendS32(time - maker.fDebugTimeBase); |
michael@0 | 308 | debugOut.append(" apply enable id="); |
michael@0 | 309 | debugOut.append(_id); |
michael@0 | 310 | debugOut.append("; start="); |
michael@0 | 311 | debugOut.appendS32(maker.fEnableTime - maker.fDebugTimeBase); |
michael@0 | 312 | SkDebugf("%s\n", debugOut.c_str()); |
michael@0 | 313 | #endif |
michael@0 | 314 | if (scope == NULL || scope->isApply() || scope->getType() == SkType_Movie || scope->isDrawable() == false) { |
michael@0 | 315 | activate(maker); // for non-drawables like post, prime them here |
michael@0 | 316 | if (initialized) { |
michael@0 | 317 | append(this); |
michael@0 | 318 | } |
michael@0 | 319 | fEnabling = true; |
michael@0 | 320 | interpolate(maker, maker.fEnableTime); |
michael@0 | 321 | fEnabling = false; |
michael@0 | 322 | if (scope != NULL && dontDraw == false) |
michael@0 | 323 | scope->enable(maker); |
michael@0 | 324 | return true; |
michael@0 | 325 | } else if (initialized && restore == false) |
michael@0 | 326 | append(this); |
michael@0 | 327 | #if 0 |
michael@0 | 328 | bool wasActive = inactivate(maker); // start fresh |
michael@0 | 329 | if (wasActive) { |
michael@0 | 330 | activate(maker); |
michael@0 | 331 | interpolate(maker, maker.fEnableTime); |
michael@0 | 332 | return true; |
michael@0 | 333 | } |
michael@0 | 334 | #endif |
michael@0 | 335 | // start here; |
michael@0 | 336 | // now that one apply might embed another, only the parent apply should replace the scope |
michael@0 | 337 | // or get appended to the display list |
michael@0 | 338 | // similarly, an apply added by an add immediate has already been located in the display list |
michael@0 | 339 | // and should not get moved or added again here |
michael@0 | 340 | if (fEmbedded) { |
michael@0 | 341 | return false; // already added to display list by embedder |
michael@0 | 342 | } |
michael@0 | 343 | drawable = (SkDrawable*) scope; |
michael@0 | 344 | SkTDDrawableArray* parentList; |
michael@0 | 345 | SkTDDrawableArray* grandList; |
michael@0 | 346 | SkGroup* parentGroup; |
michael@0 | 347 | SkGroup* thisGroup; |
michael@0 | 348 | int old = displayList.findGroup(drawable, &parentList, &parentGroup, &thisGroup, &grandList); |
michael@0 | 349 | if (old < 0) |
michael@0 | 350 | goto append; |
michael@0 | 351 | else if (fContainsScope) { |
michael@0 | 352 | if ((*parentList)[old] != this || restore) { |
michael@0 | 353 | append: |
michael@0 | 354 | if (parentGroup) |
michael@0 | 355 | parentGroup->markCopySize(old); |
michael@0 | 356 | if (parentList->count() < 10000) { |
michael@0 | 357 | fAppended = true; |
michael@0 | 358 | *parentList->append() = this; |
michael@0 | 359 | } else |
michael@0 | 360 | maker.setErrorCode(SkDisplayXMLParserError::kDisplayTreeTooDeep); |
michael@0 | 361 | old = -1; |
michael@0 | 362 | } else |
michael@0 | 363 | reset(); |
michael@0 | 364 | } else { |
michael@0 | 365 | SkASSERT(old < parentList->count()); |
michael@0 | 366 | if ((*parentList)[old]->isApply()) { |
michael@0 | 367 | SkApply* apply = (SkApply*) (*parentList)[old]; |
michael@0 | 368 | if (apply != this && apply->fActive == NULL) |
michael@0 | 369 | apply->activate(maker); |
michael@0 | 370 | apply->append(this); |
michael@0 | 371 | parentGroup = NULL; |
michael@0 | 372 | } else { |
michael@0 | 373 | if (parentGroup) |
michael@0 | 374 | parentGroup->markCopySize(old); |
michael@0 | 375 | SkDrawable** newApplyLocation = &(*parentList)[old]; |
michael@0 | 376 | SkGroup* pGroup; |
michael@0 | 377 | int oldApply = displayList.findGroup(this, &parentList, &pGroup, &thisGroup, &grandList); |
michael@0 | 378 | if (oldApply >= 0) { |
michael@0 | 379 | (*parentList)[oldApply] = (SkDrawable*) SkDisplayType::CreateInstance(&maker, SkType_Apply); |
michael@0 | 380 | parentGroup = NULL; |
michael@0 | 381 | fDeleteScope = true; |
michael@0 | 382 | } |
michael@0 | 383 | *newApplyLocation = this; |
michael@0 | 384 | } |
michael@0 | 385 | } |
michael@0 | 386 | if (parentGroup) { |
michael@0 | 387 | parentGroup->markCopySet(old); |
michael@0 | 388 | fDeleteScope = dynamicScope.size() == 0; |
michael@0 | 389 | } |
michael@0 | 390 | return true; |
michael@0 | 391 | } |
michael@0 | 392 | |
michael@0 | 393 | void SkApply::enableCreate(SkAnimateMaker& maker) { |
michael@0 | 394 | SkString newID; |
michael@0 | 395 | for (int step = 0; step <= steps; step++) { |
michael@0 | 396 | fLastTime = step * SK_MSec1; |
michael@0 | 397 | bool success = maker.computeID(scope, this, &newID); |
michael@0 | 398 | if (success == false) |
michael@0 | 399 | return; |
michael@0 | 400 | if (maker.find(newID.c_str(), NULL)) |
michael@0 | 401 | continue; |
michael@0 | 402 | SkApply* copy = (SkApply*) deepCopy(&maker); // work on copy of animator state |
michael@0 | 403 | if (mode == kMode_create) |
michael@0 | 404 | copy->mode = (Mode) -1; |
michael@0 | 405 | SkDrawable* copyScope = copy->scope = (SkDrawable*) scope->deepCopy(&maker); |
michael@0 | 406 | *fScopes.append() = copyScope; |
michael@0 | 407 | if (copyScope->resolveIDs(maker, scope, this)) { |
michael@0 | 408 | step = steps; // quit |
michael@0 | 409 | goto next; // resolveIDs failed |
michael@0 | 410 | } |
michael@0 | 411 | if (newID.size() > 0) |
michael@0 | 412 | maker.setID(copyScope, newID); |
michael@0 | 413 | if (copy->resolveIDs(maker, this, this)) { // fix up all fields, including target |
michael@0 | 414 | step = steps; // quit |
michael@0 | 415 | goto next; // resolveIDs failed |
michael@0 | 416 | } |
michael@0 | 417 | copy->activate(maker); |
michael@0 | 418 | copy->interpolate(maker, step * SK_MSec1); |
michael@0 | 419 | maker.removeActive(copy->fActive); |
michael@0 | 420 | next: |
michael@0 | 421 | delete copy; |
michael@0 | 422 | } |
michael@0 | 423 | } |
michael@0 | 424 | |
michael@0 | 425 | void SkApply::enableDynamic(SkAnimateMaker& maker) { |
michael@0 | 426 | SkASSERT(mode != kMode_create); // create + dynamic are not currently compatible |
michael@0 | 427 | SkDisplayable* newScope; |
michael@0 | 428 | bool success = SkAnimatorScript::EvaluateDisplayable(maker, this, dynamicScope.c_str(), |
michael@0 | 429 | &newScope); |
michael@0 | 430 | if (success && scope != newScope) { |
michael@0 | 431 | SkTDDrawableArray* pList, * gList; |
michael@0 | 432 | SkGroup* pGroup = NULL, * found = NULL; |
michael@0 | 433 | int old = maker.fDisplayList.findGroup(scope, &pList, &pGroup, &found, &gList); |
michael@0 | 434 | if (pList && old >= 0 && (*pList)[old]->isApply() && (*pList)[old] != this) { |
michael@0 | 435 | if (fAppended == false) { |
michael@0 | 436 | if (found != NULL) { |
michael@0 | 437 | SkDisplayable* oldChild = (*pList)[old]; |
michael@0 | 438 | if (oldChild->isApply() && found->copySet(old)) { |
michael@0 | 439 | found->markCopyClear(old); |
michael@0 | 440 | // delete oldChild; |
michael@0 | 441 | } |
michael@0 | 442 | } |
michael@0 | 443 | (*pList)[old] = scope; |
michael@0 | 444 | } else |
michael@0 | 445 | pList->remove(old); |
michael@0 | 446 | } |
michael@0 | 447 | scope = (SkDrawable*) newScope; |
michael@0 | 448 | onEndElement(maker); |
michael@0 | 449 | } |
michael@0 | 450 | maker.removeActive(fActive); |
michael@0 | 451 | delete fActive; |
michael@0 | 452 | fActive = NULL; |
michael@0 | 453 | } |
michael@0 | 454 | |
michael@0 | 455 | void SkApply::endSave(int index) { |
michael@0 | 456 | SkAnimateBase* animate = fActive->fAnimators[index]; |
michael@0 | 457 | const SkMemberInfo* info = animate->fFieldInfo; |
michael@0 | 458 | SkDisplayTypes type = (SkDisplayTypes) info->fType; |
michael@0 | 459 | if (type == SkType_MemberFunction) |
michael@0 | 460 | return; |
michael@0 | 461 | SkDisplayable* target = getTarget(animate); |
michael@0 | 462 | size_t size = info->getSize(target); |
michael@0 | 463 | int count = (int) (size / sizeof(SkScalar)); |
michael@0 | 464 | int activeIndex = fActive->fDrawIndex + index; |
michael@0 | 465 | SkOperand* last = new SkOperand[count]; |
michael@0 | 466 | SkAutoTDelete<SkOperand> autoLast(last); |
michael@0 | 467 | if (type != SkType_MemberProperty) { |
michael@0 | 468 | info->getValue(target, last, count); |
michael@0 | 469 | SkOperand* saveOperand = fActive->fSaveRestore[activeIndex]; |
michael@0 | 470 | if (saveOperand) |
michael@0 | 471 | info->setValue(target, fActive->fSaveRestore[activeIndex], count); |
michael@0 | 472 | } else { |
michael@0 | 473 | SkScriptValue scriptValue; |
michael@0 | 474 | SkDEBUGCODE(bool success = ) target->getProperty(info->propertyIndex(), &scriptValue); |
michael@0 | 475 | SkASSERT(success == true); |
michael@0 | 476 | last[0] = scriptValue.fOperand; |
michael@0 | 477 | scriptValue.fOperand = fActive->fSaveRestore[activeIndex][0]; |
michael@0 | 478 | target->setProperty(info->propertyIndex(), scriptValue); |
michael@0 | 479 | } |
michael@0 | 480 | SkOperand* save = fActive->fSaveRestore[activeIndex]; |
michael@0 | 481 | if (save) |
michael@0 | 482 | memcpy(save, last, count * sizeof(SkOperand)); |
michael@0 | 483 | } |
michael@0 | 484 | |
michael@0 | 485 | bool SkApply::getProperty(int index, SkScriptValue* value) const { |
michael@0 | 486 | switch (index) { |
michael@0 | 487 | case SK_PROPERTY(step): |
michael@0 | 488 | value->fType = SkType_Int; |
michael@0 | 489 | value->fOperand.fS32 = fLastTime / SK_MSec1; |
michael@0 | 490 | break; |
michael@0 | 491 | case SK_PROPERTY(steps): |
michael@0 | 492 | value->fType = SkType_Int; |
michael@0 | 493 | value->fOperand.fS32 = steps; |
michael@0 | 494 | break; |
michael@0 | 495 | case SK_PROPERTY(time): |
michael@0 | 496 | value->fType = SkType_MSec; |
michael@0 | 497 | value->fOperand.fS32 = fLastTime; |
michael@0 | 498 | break; |
michael@0 | 499 | default: |
michael@0 | 500 | // SkASSERT(0); |
michael@0 | 501 | return false; |
michael@0 | 502 | } |
michael@0 | 503 | return true; |
michael@0 | 504 | } |
michael@0 | 505 | |
michael@0 | 506 | void SkApply::getStep(SkScriptValue* value) { |
michael@0 | 507 | getProperty(SK_PROPERTY(step), value); |
michael@0 | 508 | } |
michael@0 | 509 | |
michael@0 | 510 | SkDrawable* SkApply::getTarget(SkAnimateBase* animate) { |
michael@0 | 511 | if (animate->fTargetIsScope == false || mode != kMode_create) |
michael@0 | 512 | return animate->fTarget; |
michael@0 | 513 | return scope; |
michael@0 | 514 | } |
michael@0 | 515 | |
michael@0 | 516 | bool SkApply::hasDelayedAnimator() const { |
michael@0 | 517 | SkAnimateBase* const* animEnd = fAnimators.end(); |
michael@0 | 518 | for (SkAnimateBase* const* animPtr = fAnimators.begin(); animPtr < animEnd; animPtr++) { |
michael@0 | 519 | SkAnimateBase* const animator = *animPtr; |
michael@0 | 520 | if (animator->fDelayed) |
michael@0 | 521 | return true; |
michael@0 | 522 | } |
michael@0 | 523 | return false; |
michael@0 | 524 | } |
michael@0 | 525 | |
michael@0 | 526 | bool SkApply::hasEnable() const { |
michael@0 | 527 | return true; |
michael@0 | 528 | } |
michael@0 | 529 | |
michael@0 | 530 | bool SkApply::inactivate(SkAnimateMaker& maker) { |
michael@0 | 531 | if (fActive == NULL) |
michael@0 | 532 | return false; |
michael@0 | 533 | maker.removeActive(fActive); |
michael@0 | 534 | delete fActive; |
michael@0 | 535 | fActive = NULL; |
michael@0 | 536 | return true; |
michael@0 | 537 | } |
michael@0 | 538 | |
michael@0 | 539 | #ifdef SK_DEBUG |
michael@0 | 540 | SkMSec lastTime = (SkMSec) -1; |
michael@0 | 541 | #endif |
michael@0 | 542 | |
michael@0 | 543 | bool SkApply::interpolate(SkAnimateMaker& maker, SkMSec rawTime) { |
michael@0 | 544 | if (fActive == NULL) |
michael@0 | 545 | return false; |
michael@0 | 546 | bool result = false; |
michael@0 | 547 | #if defined SK_DEBUG && defined SK_DEBUG_ANIMATION_TIMING |
michael@0 | 548 | SkMSec time = maker.getAppTime(); |
michael@0 | 549 | if (lastTime == (SkMSec) -1) |
michael@0 | 550 | lastTime = rawTime - 1; |
michael@0 | 551 | if (fActive != NULL && |
michael@0 | 552 | strcmp(id, "a3") == 0 && rawTime > lastTime) { |
michael@0 | 553 | lastTime += 1000; |
michael@0 | 554 | SkString debugOut; |
michael@0 | 555 | debugOut.appendS32(time - maker.fDebugTimeBase); |
michael@0 | 556 | debugOut.append(" apply id="); |
michael@0 | 557 | debugOut.append(_id); |
michael@0 | 558 | debugOut.append("; "); |
michael@0 | 559 | debugOut.append(fActive->fAnimators[0]->_id); |
michael@0 | 560 | debugOut.append("="); |
michael@0 | 561 | debugOut.appendS32(rawTime - fActive->fState[0].fStartTime); |
michael@0 | 562 | debugOut.append(")"); |
michael@0 | 563 | SkDebugf("%s\n", debugOut.c_str()); |
michael@0 | 564 | } |
michael@0 | 565 | #endif |
michael@0 | 566 | fActive->start(); |
michael@0 | 567 | if (restore) |
michael@0 | 568 | fActive->initializeSave(); |
michael@0 | 569 | int animators = fActive->fAnimators.count(); |
michael@0 | 570 | for (int inner = 0; inner < animators; inner++) { |
michael@0 | 571 | SkAnimateBase* animate = fActive->fAnimators[inner]; |
michael@0 | 572 | if (animate->fChanged) { |
michael@0 | 573 | animate->fChanged = false; |
michael@0 | 574 | animate->fStart = rawTime; |
michael@0 | 575 | // SkTypedArray values; |
michael@0 | 576 | // int count = animate->fValues.count(); |
michael@0 | 577 | // values.setCount(count); |
michael@0 | 578 | // memcpy(values.begin(), animate->fValues.begin(), sizeof(SkOperand) * count); |
michael@0 | 579 | animate->onEndElement(maker); |
michael@0 | 580 | // if (memcmp(values.begin(), animate->fValues.begin(), sizeof(SkOperand) * count) != 0) { |
michael@0 | 581 | fActive->append(this); |
michael@0 | 582 | fActive->start(); |
michael@0 | 583 | // } |
michael@0 | 584 | } |
michael@0 | 585 | SkMSec time = fActive->getTime(rawTime, inner); |
michael@0 | 586 | SkActive::SkState& state = fActive->fState[inner]; |
michael@0 | 587 | if (SkMSec_LT(rawTime, state.fStartTime)) { |
michael@0 | 588 | if (fEnabling) { |
michael@0 | 589 | animate->fDelayed = true; |
michael@0 | 590 | maker.delayEnable(this, state.fStartTime); |
michael@0 | 591 | } |
michael@0 | 592 | continue; |
michael@0 | 593 | } else |
michael@0 | 594 | animate->fDelayed = false; |
michael@0 | 595 | SkMSec innerTime = fLastTime = state.getRelativeTime(time); |
michael@0 | 596 | if (restore) |
michael@0 | 597 | fActive->restoreInterpolatorValues(inner); |
michael@0 | 598 | if (animate->fReset) { |
michael@0 | 599 | if (transition != SkApply::kTransition_reverse) { |
michael@0 | 600 | if (SkMSec_LT(state.fBegin + state.fDuration, innerTime)) { |
michael@0 | 601 | if (animate->fResetPending) { |
michael@0 | 602 | innerTime = 0; |
michael@0 | 603 | animate->fResetPending = false; |
michael@0 | 604 | } else |
michael@0 | 605 | continue; |
michael@0 | 606 | } |
michael@0 | 607 | } else if (innerTime == 0) { |
michael@0 | 608 | if (animate->fResetPending) { |
michael@0 | 609 | innerTime = state.fBegin + state.fDuration; |
michael@0 | 610 | animate->fResetPending = false; |
michael@0 | 611 | } else |
michael@0 | 612 | continue; |
michael@0 | 613 | } |
michael@0 | 614 | } |
michael@0 | 615 | int count = animate->components(); |
michael@0 | 616 | SkAutoSTMalloc<16, SkOperand> values(count); |
michael@0 | 617 | SkInterpolatorBase::Result interpResult = fActive->fInterpolators[inner]->timeToValues( |
michael@0 | 618 | innerTime, values.get()); |
michael@0 | 619 | result |= (interpResult != SkInterpolatorBase::kFreezeEnd_Result); |
michael@0 | 620 | if (((transition != SkApply::kTransition_reverse && interpResult == SkInterpolatorBase::kFreezeEnd_Result) || |
michael@0 | 621 | (transition == SkApply::kTransition_reverse && fLastTime == 0)) && state.fUnpostedEndEvent) { |
michael@0 | 622 | // SkDEBUGF(("interpolate: post on end\n")); |
michael@0 | 623 | state.fUnpostedEndEvent = false; |
michael@0 | 624 | maker.postOnEnd(animate, state.fBegin + state.fDuration); |
michael@0 | 625 | maker.fAdjustedStart = 0; // !!! left over from synchronizing animation days, undoubtably out of date (and broken) |
michael@0 | 626 | } |
michael@0 | 627 | if (animate->formula.size() > 0) { |
michael@0 | 628 | if (fLastTime > animate->dur) |
michael@0 | 629 | fLastTime = animate->dur; |
michael@0 | 630 | SkTypedArray formulaValues; |
michael@0 | 631 | formulaValues.setCount(count); |
michael@0 | 632 | SkDEBUGCODE(bool success = ) animate->fFieldInfo->setValue(maker, &formulaValues, 0, 0, NULL, |
michael@0 | 633 | animate->getValuesType(), animate->formula); |
michael@0 | 634 | SkASSERT(success); |
michael@0 | 635 | if (restore) |
michael@0 | 636 | save(inner); // save existing value |
michael@0 | 637 | applyValues(inner, formulaValues.begin(), count, animate->getValuesType(), innerTime); |
michael@0 | 638 | } else { |
michael@0 | 639 | if (restore) |
michael@0 | 640 | save(inner); // save existing value |
michael@0 | 641 | applyValues(inner, values.get(), count, animate->getValuesType(), innerTime); |
michael@0 | 642 | } |
michael@0 | 643 | } |
michael@0 | 644 | return result; |
michael@0 | 645 | } |
michael@0 | 646 | |
michael@0 | 647 | void SkApply::initialize() { |
michael@0 | 648 | if (scope == NULL) |
michael@0 | 649 | return; |
michael@0 | 650 | if (scope->isApply() || scope->isDrawable() == false) |
michael@0 | 651 | return; |
michael@0 | 652 | scope->initialize(); |
michael@0 | 653 | } |
michael@0 | 654 | |
michael@0 | 655 | void SkApply::onEndElement(SkAnimateMaker& maker) |
michael@0 | 656 | { |
michael@0 | 657 | SkDrawable* scopePtr = scope; |
michael@0 | 658 | while (scopePtr && scopePtr->isApply()) { |
michael@0 | 659 | SkApply* scopedApply = (SkApply*) scopePtr; |
michael@0 | 660 | if (scopedApply->scope == this) { |
michael@0 | 661 | maker.setErrorCode(SkDisplayXMLParserError::kApplyScopesItself); |
michael@0 | 662 | return; |
michael@0 | 663 | } |
michael@0 | 664 | scopePtr = scopedApply->scope; |
michael@0 | 665 | } |
michael@0 | 666 | if (mode == kMode_create) |
michael@0 | 667 | return; |
michael@0 | 668 | if (scope != NULL && steps >= 0 && scope->isApply() == false && scope->isDrawable()) |
michael@0 | 669 | scope->setSteps(steps); |
michael@0 | 670 | for (SkAnimateBase** animPtr = fAnimators.begin(); animPtr < fAnimators.end(); animPtr++) { |
michael@0 | 671 | SkAnimateBase* anim = *animPtr; |
michael@0 | 672 | //for reusing apply statements with dynamic scope |
michael@0 | 673 | if (anim->fTarget == NULL || anim->fTargetIsScope) { |
michael@0 | 674 | anim->fTargetIsScope = true; |
michael@0 | 675 | if (scope) |
michael@0 | 676 | anim->fTarget = scope; |
michael@0 | 677 | else |
michael@0 | 678 | anim->setTarget(maker); |
michael@0 | 679 | anim->onEndElement(maker); // allows animate->fFieldInfo to be set |
michael@0 | 680 | } |
michael@0 | 681 | if (scope != NULL && steps >= 0 && anim->fTarget != scope && anim->fTarget->isDrawable()) |
michael@0 | 682 | anim->fTarget->setSteps(steps); |
michael@0 | 683 | } |
michael@0 | 684 | } |
michael@0 | 685 | |
michael@0 | 686 | const SkMemberInfo* SkApply::preferredChild(SkDisplayTypes type) { |
michael@0 | 687 | SkASSERT(SkDisplayType::IsAnimate(type) == false); |
michael@0 | 688 | fContainsScope = true; |
michael@0 | 689 | return getMember("scope"); // !!! cwap! need to refer to member through enum like kScope instead |
michael@0 | 690 | } |
michael@0 | 691 | |
michael@0 | 692 | void SkApply::refresh(SkAnimateMaker& maker) { |
michael@0 | 693 | for (SkAnimateBase** animPtr = fAnimators.begin(); animPtr < fAnimators.end(); animPtr++) { |
michael@0 | 694 | SkAnimateBase* animate = *animPtr; |
michael@0 | 695 | animate->onEndElement(maker); |
michael@0 | 696 | } |
michael@0 | 697 | if (fActive) |
michael@0 | 698 | fActive->resetInterpolators(); |
michael@0 | 699 | } |
michael@0 | 700 | |
michael@0 | 701 | void SkApply::reset() { |
michael@0 | 702 | if (fActive) |
michael@0 | 703 | fActive->resetState(); |
michael@0 | 704 | } |
michael@0 | 705 | |
michael@0 | 706 | bool SkApply::resolveIDs(SkAnimateMaker& maker, SkDisplayable* original, SkApply* apply) { // replace to/formula strings in animators of the form xxx.step with the step value, if xxx.step is in scope |
michael@0 | 707 | if (resolveField(maker, apply, &dynamicScope) == false) |
michael@0 | 708 | return true; // failed |
michael@0 | 709 | SkAnimateBase** endPtr = fAnimators.end(); |
michael@0 | 710 | SkAnimateBase** origPtr = ((SkApply*) original)->fAnimators.begin(); |
michael@0 | 711 | for (SkAnimateBase** animPtr = fAnimators.begin(); animPtr < endPtr; ) { |
michael@0 | 712 | SkAnimateBase* animator = *animPtr++; |
michael@0 | 713 | maker.resolveID(animator, *origPtr++); |
michael@0 | 714 | if (resolveField(maker, this, &animator->target) == false) |
michael@0 | 715 | return true; |
michael@0 | 716 | if (resolveField(maker, this, &animator->from) == false) |
michael@0 | 717 | return true; |
michael@0 | 718 | if (resolveField(maker, this, &animator->to) == false) |
michael@0 | 719 | return true; |
michael@0 | 720 | if (resolveField(maker, this, &animator->formula) == false) |
michael@0 | 721 | return true; |
michael@0 | 722 | } |
michael@0 | 723 | // setEmbedded(); |
michael@0 | 724 | onEndElement(maker); |
michael@0 | 725 | return false; // succeeded |
michael@0 | 726 | } |
michael@0 | 727 | |
michael@0 | 728 | bool SkApply::resolveField(SkAnimateMaker& maker, SkDisplayable* parent, SkString* str) { |
michael@0 | 729 | const char* script = str->c_str(); |
michael@0 | 730 | if (str->startsWith("#string:") == false) |
michael@0 | 731 | return true; |
michael@0 | 732 | script += sizeof("#string:") - 1; |
michael@0 | 733 | return SkAnimatorScript::EvaluateString(maker, this, parent, script, str); |
michael@0 | 734 | } |
michael@0 | 735 | |
michael@0 | 736 | void SkApply::save(int index) { |
michael@0 | 737 | SkAnimateBase* animate = fActive->fAnimators[index]; |
michael@0 | 738 | const SkMemberInfo * info = animate->fFieldInfo; |
michael@0 | 739 | SkDisplayable* target = getTarget(animate); |
michael@0 | 740 | // if (animate->hasExecute()) |
michael@0 | 741 | // info = animate->getResolvedInfo(); |
michael@0 | 742 | SkDisplayTypes type = (SkDisplayTypes) info->fType; |
michael@0 | 743 | if (type == SkType_MemberFunction) |
michael@0 | 744 | return; // nothing to save |
michael@0 | 745 | size_t size = info->getSize(target); |
michael@0 | 746 | int count = (int) (size / sizeof(SkScalar)); |
michael@0 | 747 | bool useLast = true; |
michael@0 | 748 | // !!! this all may be unneeded, at least in the dynamic case ?? |
michael@0 | 749 | int activeIndex = fActive->fDrawIndex + index; |
michael@0 | 750 | SkTDOperandArray last; |
michael@0 | 751 | if (fActive->fSaveRestore[activeIndex] == NULL) { |
michael@0 | 752 | fActive->fSaveRestore[activeIndex] = new SkOperand[count]; |
michael@0 | 753 | useLast = false; |
michael@0 | 754 | } else { |
michael@0 | 755 | last.setCount(count); |
michael@0 | 756 | memcpy(last.begin(), fActive->fSaveRestore[activeIndex], count * sizeof(SkOperand)); |
michael@0 | 757 | } |
michael@0 | 758 | if (type != SkType_MemberProperty) { |
michael@0 | 759 | info->getValue(target, fActive->fSaveRestore[activeIndex], count); |
michael@0 | 760 | if (useLast) |
michael@0 | 761 | info->setValue(target, last.begin(), count); |
michael@0 | 762 | } else { |
michael@0 | 763 | SkScriptValue scriptValue; |
michael@0 | 764 | SkDEBUGCODE(bool success = ) target->getProperty(info->propertyIndex(), &scriptValue); |
michael@0 | 765 | SkASSERT(success == true); |
michael@0 | 766 | SkASSERT(scriptValue.fType == SkType_Float); |
michael@0 | 767 | fActive->fSaveRestore[activeIndex][0] = scriptValue.fOperand; |
michael@0 | 768 | if (useLast) { |
michael@0 | 769 | SkScriptValue scriptValue; |
michael@0 | 770 | scriptValue.fType = type; |
michael@0 | 771 | scriptValue.fOperand = last[0]; |
michael@0 | 772 | target->setProperty(info->propertyIndex(), scriptValue); |
michael@0 | 773 | } |
michael@0 | 774 | } |
michael@0 | 775 | // !!! end of unneeded |
michael@0 | 776 | } |
michael@0 | 777 | |
michael@0 | 778 | bool SkApply::setProperty(int index, SkScriptValue& scriptValue) { |
michael@0 | 779 | switch (index) { |
michael@0 | 780 | case SK_PROPERTY(animator): { |
michael@0 | 781 | SkAnimateBase* animate = (SkAnimateBase*) scriptValue.fOperand.fDisplayable; |
michael@0 | 782 | SkASSERT(animate->isAnimate()); |
michael@0 | 783 | *fAnimators.append() = animate; |
michael@0 | 784 | return true; |
michael@0 | 785 | } |
michael@0 | 786 | case SK_PROPERTY(steps): |
michael@0 | 787 | steps = scriptValue.fOperand.fS32; |
michael@0 | 788 | if (fActive) |
michael@0 | 789 | fActive->setSteps(steps); |
michael@0 | 790 | return true; |
michael@0 | 791 | } |
michael@0 | 792 | return false; |
michael@0 | 793 | } |
michael@0 | 794 | |
michael@0 | 795 | void SkApply::setSteps(int _steps) { |
michael@0 | 796 | steps = _steps; |
michael@0 | 797 | } |
michael@0 | 798 | |
michael@0 | 799 | #ifdef SK_DEBUG |
michael@0 | 800 | void SkApply::validate() { |
michael@0 | 801 | if (fActive) |
michael@0 | 802 | fActive->validate(); |
michael@0 | 803 | } |
michael@0 | 804 | #endif |