michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #include "SkAnimateActive.h" michael@0: #include "SkAnimateBase.h" michael@0: #include "SkAnimateMaker.h" michael@0: #include "SkAnimateSet.h" michael@0: #include "SkDrawGroup.h" michael@0: #ifdef SK_DEBUG michael@0: #include "SkTime.h" michael@0: #endif michael@0: michael@0: // SkActive holds array of interpolators michael@0: michael@0: SkActive::SkActive(SkApply& apply, SkAnimateMaker& maker) : fApply(apply), michael@0: fMaxTime(0), fMaker(maker), fDrawIndex(0), fDrawMax(0) { michael@0: } michael@0: michael@0: void SkActive::init() michael@0: { michael@0: fAnimators = fApply.fAnimators; michael@0: int animators = fAnimators.count(); michael@0: fInterpolators.setCount(animators); michael@0: memset(fInterpolators.begin(), 0, animators * sizeof(SkOperandInterpolator*)); michael@0: fState.setCount(animators); michael@0: int index; michael@0: for (index = 0; index < animators; index++) michael@0: fInterpolators[index] = SkNEW(SkOperandInterpolator); michael@0: initState(&fApply, 0); michael@0: // for (index = 0; index < animators; index++) michael@0: // fState[index].bumpSave(); michael@0: SkASSERT(fInterpolators.count() == fAnimators.count()); michael@0: } michael@0: michael@0: SkActive::~SkActive() { michael@0: int index; michael@0: for (index = 0; index < fSaveRestore.count(); index++) michael@0: delete[] fSaveRestore[index]; michael@0: for (index = 0; index < fSaveInterpolators.count(); index++) michael@0: delete[] fSaveInterpolators[index]; michael@0: for (index = 0; index < fInterpolators.count(); index++) michael@0: delete fInterpolators[index]; michael@0: } michael@0: michael@0: void SkActive::advance() { michael@0: if (fDrawMax < fDrawIndex) michael@0: fDrawMax = fDrawIndex; michael@0: fDrawIndex += fAnimators.count(); michael@0: } michael@0: michael@0: void SkActive::append(SkApply* apply) { michael@0: int oldCount = fAnimators.count(); michael@0: SkTDAnimateArray& animates = apply->fAnimators; michael@0: int newCount = animates.count(); michael@0: int index; michael@0: int total = oldCount + newCount; michael@0: if (total == 0) michael@0: return; michael@0: fInterpolators.setCount(total); michael@0: memset(&fInterpolators.begin()[oldCount], 0, newCount * sizeof(SkOperandInterpolator*)); michael@0: for (index = oldCount; index < total; index++) michael@0: fInterpolators[index] = SkNEW(SkOperandInterpolator); michael@0: fAnimators.setCount(total); michael@0: memcpy(&fAnimators[oldCount], animates.begin(), sizeof(fAnimators[0]) * michael@0: newCount); michael@0: fState.setCount(total); michael@0: initState(apply, oldCount); michael@0: SkASSERT(fApply.scope == apply->scope); michael@0: for (index = 0; index < newCount; index++) { michael@0: SkAnimateBase* test = animates[index]; michael@0: // SkASSERT(fApply.scope == test->fTarget || fApply.scope->contains(test->fTarget)); michael@0: SkActive::SkState& testState = fState[oldCount + index]; michael@0: for (int inner = 0; inner < oldCount; inner++) { michael@0: SkAnimateBase* oldGuard = fAnimators[inner]; michael@0: SkActive::SkState& oldState = fState[inner]; michael@0: if (oldGuard->fTarget == test->fTarget && oldGuard->fFieldInfo == test->fFieldInfo && michael@0: testState.fBegin == oldState.fBegin) { michael@0: delete fInterpolators[inner]; michael@0: fInterpolators.remove(inner); michael@0: fAnimators.remove(inner); michael@0: testState.fSave = oldState.fSave; michael@0: if (oldState.fUnpostedEndEvent) { michael@0: // SkDEBUGF(("%8x %8x active append: post on end\n", this, oldGuard)); michael@0: fMaker.postOnEnd(oldGuard, oldState.fBegin + oldState.fDuration); michael@0: } michael@0: fState.remove(inner); michael@0: if (fApply.restore) { michael@0: int saveIndex = fSaveRestore.count(); michael@0: SkASSERT(fSaveInterpolators.count() == saveIndex); michael@0: saveIndex += inner; michael@0: do { michael@0: saveIndex -= oldCount; michael@0: delete[] fSaveRestore[saveIndex]; michael@0: fSaveRestore.remove(saveIndex); michael@0: delete[] fSaveInterpolators[saveIndex]; michael@0: fSaveInterpolators.remove(saveIndex); michael@0: } while (saveIndex > 0); michael@0: } michael@0: oldCount--; michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: // total = oldCount + newCount; michael@0: // for (index = oldCount; index < total; index++) michael@0: // fState[index].bumpSave(); michael@0: SkASSERT(fInterpolators.count() == fAnimators.count()); michael@0: } michael@0: michael@0: void SkActive::appendSave(int oldCount) { michael@0: SkASSERT(fDrawMax == 0); // if true, we can optimize below quite a bit michael@0: int newCount = fAnimators.count(); michael@0: int saveIndex = fSaveRestore.count(); michael@0: SkASSERT(fSaveInterpolators.count() == saveIndex); michael@0: int records = saveIndex / oldCount; michael@0: int newTotal = records * newCount; michael@0: fSaveRestore.setCount(newTotal); michael@0: do { michael@0: saveIndex -= oldCount; michael@0: newTotal -= newCount; michael@0: SkASSERT(saveIndex >= 0); michael@0: SkASSERT(newTotal >= 0); michael@0: memmove(&fSaveRestore[newTotal], &fSaveRestore[saveIndex], oldCount); michael@0: memset(&fSaveRestore[newTotal + oldCount], 0, michael@0: sizeof(fSaveRestore[0]) * (newCount - oldCount)); michael@0: memmove(&fSaveInterpolators[newTotal], michael@0: &fSaveInterpolators[saveIndex], oldCount); michael@0: memset(&fSaveInterpolators[newTotal + oldCount], 0, michael@0: sizeof(fSaveRestore[0]) * (newCount - oldCount)); michael@0: } while (saveIndex > 0); michael@0: SkASSERT(newTotal == 0); michael@0: } michael@0: michael@0: void SkActive::calcDurations(int index) michael@0: { michael@0: SkAnimateBase* animate = fAnimators[index]; michael@0: SkMSec duration = animate->dur; michael@0: SkState& state = fState[index]; michael@0: switch (state.fMode) { michael@0: case SkApply::kMode_immediate: michael@0: case SkApply::kMode_create: michael@0: duration = state.fSteps ? state.fSteps * SK_MSec1 : 1; michael@0: break; michael@0: // case SkApply::kMode_hold: { michael@0: // int entries = animate->entries(); michael@0: // SkScriptValue value; michael@0: // value.fOperand = animate->getValues()[entries - 1]; michael@0: // value.fType = animate->getValuesType(); michael@0: // bool result = SkScriptEngine::ConvertTo(NULL, SkType_Int, &value); michael@0: // SkASSERT(result); michael@0: // duration = value.fOperand.fS32 * SK_MSec1; michael@0: // break; michael@0: // } michael@0: } michael@0: state.fDuration = duration; michael@0: SkMSec maxTime = state.fBegin + duration; michael@0: if (fMaxTime < maxTime) michael@0: fMaxTime = maxTime; michael@0: } michael@0: michael@0: void SkActive::create(SkDrawable* drawable, SkMSec time) { michael@0: fApply.fLastTime = time; michael@0: fApply.refresh(fMaker); michael@0: for (int index = 0; index < fAnimators.count(); index++) { michael@0: SkAnimateBase* animate = fAnimators[index]; michael@0: SkOperandInterpolator& interpolator = *fInterpolators[index]; michael@0: int count = animate->components(); michael@0: if (animate->formula.size() > 0) { michael@0: SkTDOperandArray values; michael@0: values.setCount(count); michael@0: SkDEBUGCODE(bool success = ) animate->fFieldInfo->setValue(fMaker, &values, 0, 0, NULL, michael@0: animate->getValuesType(), animate->formula); michael@0: SkASSERT(success); michael@0: fApply.applyValues(index, values.begin(), count, animate->getValuesType(), time); michael@0: } else { michael@0: SkAutoSTMalloc<16, SkOperand> values(count); michael@0: interpolator.timeToValues(time, values.get()); michael@0: fApply.applyValues(index, values.get(), count, animate->getValuesType(), time); michael@0: } michael@0: } michael@0: drawable->enable(fMaker); michael@0: SkASSERT(fAnimators.count() == fInterpolators.count()); michael@0: } michael@0: michael@0: bool SkActive::immediate(bool enable) { michael@0: SkMSec time = 0; michael@0: bool result = false; michael@0: SkDrawable* drawable = fApply.scope; michael@0: SkMSec final = fMaxTime; michael@0: do { michael@0: bool applied = fAnimators.count() == 0; michael@0: fApply.fLastTime = time; michael@0: fApply.refresh(fMaker); michael@0: for (int index = 0; index < fAnimators.count(); index++) { michael@0: SkAnimateBase* animate = fAnimators[index]; michael@0: SkState& state = fState[index]; michael@0: if (state.fMode != SkApply::kMode_immediate) michael@0: continue; michael@0: if (state.fBegin > time) michael@0: continue; michael@0: if (time > state.fBegin + state.fDuration) michael@0: continue; michael@0: applied = true; michael@0: SkOperandInterpolator& interpolator = *fInterpolators[index]; michael@0: int count = animate->components(); michael@0: if (animate->formula.size() > 0) { michael@0: SkTDOperandArray values; michael@0: values.setCount(count); michael@0: SkDEBUGCODE(bool success = ) animate->fFieldInfo->setValue(fMaker, &values, 0, 0, NULL, michael@0: animate->getValuesType(), animate->formula); michael@0: SkASSERT(success); michael@0: fApply.applyValues(index, values.begin(), count, animate->getValuesType(), time); michael@0: } else { michael@0: SkAutoSTMalloc<16, SkOperand> values(count); michael@0: interpolator.timeToValues(time, values.get()); michael@0: fApply.applyValues(index, values.get(), count, animate->getValuesType(), time); michael@0: } michael@0: } michael@0: if (enable) michael@0: drawable->enable(fMaker); michael@0: else if (applied) michael@0: result |= drawable->draw(fMaker); michael@0: time += SK_MSec1; michael@0: } while (time <= final); michael@0: return result; michael@0: } michael@0: michael@0: void SkActive::fixInterpolator(SkBool save) { michael@0: int animators = fAnimators.count(); michael@0: for (int index = 0; index < animators; index++) { michael@0: SkAnimateBase* animate = fAnimators[index]; michael@0: if (save) { // saved slots increased michael@0: animate->refresh(fMaker); michael@0: SkOperand* values = animate->getValues(); michael@0: setInterpolator(index, values); michael@0: saveInterpolatorValues(index); michael@0: } else michael@0: restoreInterpolatorValues(index); michael@0: } michael@0: } michael@0: michael@0: SkMSec SkActive::getTime(SkMSec inTime, int animatorIndex) { michael@0: fState[animatorIndex].fTicks = inTime; michael@0: return inTime - fState[animatorIndex].fStartTime; michael@0: } michael@0: michael@0: bool SkActive::initializeSave() { michael@0: int animators = fAnimators.count(); michael@0: int activeTotal = fDrawIndex + animators; michael@0: int oldCount = fSaveRestore.count(); michael@0: if (oldCount < activeTotal) { michael@0: fSaveRestore.setCount(activeTotal); michael@0: memset(&fSaveRestore[oldCount], 0, sizeof(fSaveRestore[0]) * (activeTotal - oldCount)); michael@0: SkASSERT(fSaveInterpolators.count() == oldCount); michael@0: fSaveInterpolators.setCount(activeTotal); michael@0: memset(&fSaveInterpolators[oldCount], 0, michael@0: sizeof(fSaveInterpolators[0]) * (activeTotal - oldCount)); michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: void SkActive::initState(SkApply* apply, int offset) { michael@0: int count = fState.count(); michael@0: for (int index = offset; index < count; index++) { michael@0: SkState& state = fState[index]; michael@0: SkAnimateBase* animate = fAnimators[index]; michael@0: #if 0 // def SK_DEBUG michael@0: if (animate->fHasEndEvent) michael@0: SkDebugf("%8x %8x active initState:\n", this, animate); michael@0: #endif michael@0: SkOperand* from = animate->getValues(); michael@0: state.fStartTime = state.fBegin = apply->begin + animate->begin; michael@0: state.fMode = apply->mode; michael@0: state.fTransition = apply->transition; michael@0: #if 0 michael@0: state.fPickup = (SkBool8) apply->pickup; michael@0: #endif michael@0: state.fRestore = (SkBool8) apply->restore; michael@0: state.fSave = apply->begin; michael@0: state.fStarted = false; michael@0: state.fSteps = apply->steps; michael@0: state.fTicks = 0; michael@0: state.fUnpostedEndEvent = (SkBool8) animate->fHasEndEvent; michael@0: calcDurations(index); michael@0: setInterpolator(index, from); michael@0: } michael@0: if (count == 0 && (apply->mode == SkApply::kMode_immediate || apply->mode == SkApply::kMode_create)) michael@0: fMaxTime = apply->begin + apply->steps * SK_MSec1; michael@0: } michael@0: michael@0: void SkActive::pickUp(SkActive* existing) { michael@0: SkTDOperandArray existingValues; michael@0: for (int index = 0; index < fAnimators.count(); index++) { michael@0: SkAnimateBase* animate = fAnimators[index]; michael@0: SkASSERT(animate->getValuesType() == SkType_Float); michael@0: int components = animate->components(); michael@0: SkOperand* from = animate->getValues(); michael@0: SkOperand* to = &from[animate->components()]; michael@0: existingValues.setCount(components); michael@0: existing->fInterpolators[index]->timeToValues( michael@0: existing->fState[index].fTicks - existing->fState[index].fStartTime, existingValues.begin()); michael@0: SkScalar originalSum = 0; michael@0: SkScalar workingSum = 0; michael@0: for (int cIndex = 0; cIndex < components; cIndex++) { michael@0: SkScalar delta = to[cIndex].fScalar - from[cIndex].fScalar; michael@0: originalSum += SkScalarMul(delta, delta); michael@0: delta = to[cIndex].fScalar - existingValues[cIndex].fScalar; michael@0: workingSum += SkScalarMul(delta, delta); michael@0: } michael@0: if (workingSum < originalSum) { michael@0: SkScalar originalDistance = SkScalarSqrt(originalSum); michael@0: SkScalar workingDistance = SkScalarSqrt(workingSum); michael@0: existing->fState[index].fDuration = (SkMSec) SkScalarMulDiv(fState[index].fDuration, michael@0: workingDistance, originalDistance); michael@0: } michael@0: fInterpolators[index]->reset(components, 2, SkType_Float); michael@0: fInterpolators[index]->setKeyFrame(0, 0, existingValues.begin(), animate->blend[0]); michael@0: fInterpolators[index]->setKeyFrame(1, fState[index].fDuration, to, animate->blend[0]); michael@0: } michael@0: } michael@0: michael@0: void SkActive::resetInterpolators() { michael@0: int animators = fAnimators.count(); michael@0: for (int index = 0; index < animators; index++) { michael@0: SkAnimateBase* animate = fAnimators[index]; michael@0: SkOperand* values = animate->getValues(); michael@0: setInterpolator(index, values); michael@0: } michael@0: } michael@0: michael@0: void SkActive::resetState() { michael@0: fDrawIndex = 0; michael@0: int count = fState.count(); michael@0: for (int index = 0; index < count; index++) { michael@0: SkState& state = fState[index]; michael@0: SkAnimateBase* animate = fAnimators[index]; michael@0: #if 0 // def SK_DEBUG michael@0: if (animate->fHasEndEvent) michael@0: SkDebugf("%8x %8x active resetState: has end event\n", this, animate); michael@0: #endif michael@0: state.fStartTime = state.fBegin = fApply.begin + animate->begin; michael@0: state.fStarted = false; michael@0: state.fTicks = 0; michael@0: } michael@0: } michael@0: michael@0: void SkActive::restoreInterpolatorValues(int index) { michael@0: SkOperandInterpolator& interpolator = *fInterpolators[index]; michael@0: index += fDrawIndex ; michael@0: int count = interpolator.getValuesCount(); michael@0: memcpy(interpolator.getValues(), fSaveInterpolators[index], count * sizeof(SkOperand)); michael@0: } michael@0: michael@0: void SkActive::saveInterpolatorValues(int index) { michael@0: SkOperandInterpolator& interpolator = *fInterpolators[index]; michael@0: index += fDrawIndex ; michael@0: int count = interpolator.getValuesCount(); michael@0: SkOperand* cache = new SkOperand[count]; // this should use sk_malloc/sk_free since SkOperand does not have a constructor/destructor michael@0: fSaveInterpolators[index] = cache; michael@0: memcpy(cache, interpolator.getValues(), count * sizeof(SkOperand)); michael@0: } michael@0: michael@0: void SkActive::setInterpolator(int index, SkOperand* from) { michael@0: if (from == NULL) // legitimate for set string michael@0: return; michael@0: SkAnimateBase* animate = fAnimators[index]; michael@0: int entries = animate->entries(); michael@0: SkASSERT(entries > 0); michael@0: SkMSec duration = fState[index].fDuration; michael@0: int components = animate->components(); michael@0: SkOperandInterpolator& interpolator = *fInterpolators[index]; michael@0: interpolator.reset(components, entries == 1 ? 2 : entries, animate->getValuesType()); michael@0: interpolator.setMirror(SkToBool(animate->fMirror)); michael@0: interpolator.setReset(SkToBool(animate->fReset)); michael@0: interpolator.setRepeatCount(animate->repeat); michael@0: if (entries == 1) { michael@0: interpolator.setKeyFrame(0, 0, from, animate->blend[0]); michael@0: interpolator.setKeyFrame(1, duration, from, animate->blend[0]); michael@0: return; michael@0: } michael@0: for (int entry = 0; entry < entries; entry++) { michael@0: int blendIndex = SkMin32(animate->blend.count() - 1, entry); michael@0: interpolator.setKeyFrame(entry, entry * duration / (entries - 1), from, michael@0: animate->blend[blendIndex]); michael@0: from += components; michael@0: } michael@0: } michael@0: michael@0: void SkActive::setSteps(int steps) { michael@0: int count = fState.count(); michael@0: fMaxTime = 0; michael@0: for (int index = 0; index < count; index++) { michael@0: SkState& state = fState[index]; michael@0: state.fSteps = steps; michael@0: calcDurations(index); michael@0: } michael@0: } michael@0: michael@0: void SkActive::start() { michael@0: int count = fState.count(); michael@0: SkASSERT(count == fAnimators.count()); michael@0: SkASSERT(count == fInterpolators.count()); michael@0: for (int index = 0; index < count; index++) { michael@0: SkState& state = fState[index]; michael@0: if (state.fStarted) michael@0: continue; michael@0: state.fStarted = true; michael@0: #if defined SK_DEBUG && defined SK_DEBUG_ANIMATION_TIMING michael@0: SkString debugOut; michael@0: SkMSec time = fMaker.getAppTime(); michael@0: debugOut.appendS32(time - fMaker.fDebugTimeBase); michael@0: debugOut.append(" active start adjust delay id="); michael@0: debugOut.append(fApply._id); michael@0: debugOut.append("; "); michael@0: debugOut.append(fAnimators[index]->_id); michael@0: debugOut.append("="); michael@0: debugOut.appendS32(fAnimators[index]->fStart - fMaker.fDebugTimeBase); michael@0: debugOut.append(":"); michael@0: debugOut.appendS32(state.fStartTime); michael@0: #endif michael@0: if (state.fStartTime > 0) { michael@0: SkMSec future = fAnimators[index]->fStart + state.fStartTime; michael@0: if (future > fMaker.fEnableTime) michael@0: fMaker.notifyInvalTime(future); michael@0: else michael@0: fMaker.notifyInval(); michael@0: #if defined SK_DEBUG && defined SK_DEBUG_ANIMATION_TIMING michael@0: debugOut.append(":"); michael@0: debugOut.appendS32(future - fMaker.fDebugTimeBase); michael@0: #endif michael@0: } michael@0: if (state.fStartTime >= fMaker.fAdjustedStart) { michael@0: state.fStartTime -= fMaker.fAdjustedStart; michael@0: #if defined SK_DEBUG && defined SK_DEBUG_ANIMATION_TIMING michael@0: debugOut.append(" (less adjust = "); michael@0: debugOut.appendS32(fMaker.fAdjustedStart); michael@0: #endif michael@0: } michael@0: state.fStartTime += fAnimators[index]->fStart; michael@0: #if defined SK_DEBUG && defined SK_DEBUG_ANIMATION_TIMING michael@0: debugOut.append(") new start = "); michael@0: debugOut.appendS32(state.fStartTime - fMaker.fDebugTimeBase); michael@0: SkDebugf("%s\n", debugOut.c_str()); michael@0: // SkASSERT((int) (state.fStartTime - fMaker.fDebugTimeBase) >= 0); michael@0: #endif michael@0: } michael@0: SkASSERT(fAnimators.count() == fInterpolators.count()); michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: void SkActive::validate() { michael@0: int count = fState.count(); michael@0: SkASSERT(count == fAnimators.count()); michael@0: SkASSERT(count == fInterpolators.count()); michael@0: for (int index = 0; index < count; index++) { michael@0: SkASSERT(fAnimators[index]); michael@0: SkASSERT(fInterpolators[index]); michael@0: // SkAnimateBase* test = fAnimators[index]; michael@0: // SkASSERT(fApply.scope == test->fTarget || fApply.scope->contains(test->fTarget)); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: // think about this michael@0: // there should only be one animate object, not two, to go up and down michael@0: // when the apply with reverse came into play, it needs to pick up the value michael@0: // of the existing animate object then remove it from the list michael@0: // the code below should only be bumping fSave, and there shouldn't be anything michael@0: // it needs to be synchronized with michael@0: michael@0: // however, if there are two animates both operating on the same field, then michael@0: // when one replaces the other, it may make sense to pick up the old value as a starting michael@0: // value for the new one somehow. michael@0: michael@0: //void SkActive::SkState::bumpSave() { michael@0: // if (fMode != SkApply::kMode_hold) michael@0: // return; michael@0: // if (fTransition == SkApply::kTransition_reverse) { michael@0: // if (fSave > 0) michael@0: // fSave -= SK_MSec1; michael@0: // } else if (fSave < fDuration) michael@0: // fSave += SK_MSec1; michael@0: //} michael@0: michael@0: SkMSec SkActive::SkState::getRelativeTime(SkMSec time) { michael@0: SkMSec result = time; michael@0: // if (fMode == SkApply::kMode_hold) michael@0: // result = fSave; michael@0: // else michael@0: if (fTransition == SkApply::kTransition_reverse) { michael@0: if (SkMSec_LT(fDuration, time)) michael@0: result = 0; michael@0: else michael@0: result = fDuration - time; michael@0: } michael@0: return result; michael@0: }