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 "SkAnimator.h" michael@0: #include "SkAnimateMaker.h" michael@0: #include "SkCanvas.h" michael@0: #include "SkDisplayApply.h" michael@0: #include "SkDisplayMovie.h" michael@0: #include "SkDisplayTypes.h" michael@0: #include "SkDisplayXMLParser.h" michael@0: #include "SkStream.h" michael@0: #include "SkScript.h" michael@0: #include "SkScript2.h" // compiled script experiment michael@0: #include "SkSystemEventTypes.h" michael@0: #include "SkTypedArray.h" michael@0: #ifdef SK_BUILD_FOR_ANDROID michael@0: #include "SkDrawExtraPathEffect.h" michael@0: #endif michael@0: #ifdef SK_DEBUG michael@0: #include "SkTime.h" michael@0: #endif michael@0: michael@0: #if defined SK_BUILD_FOR_WIN32 && defined SK_DEBUG michael@0: #define _static michael@0: extern const char gMathPrimerText[]; michael@0: extern const char gMathPrimerBinary[]; michael@0: #else michael@0: #define _static static michael@0: #endif michael@0: michael@0: _static const char gMathPrimerText[] = michael@0: "" michael@0: "" michael@0: "" michael@0: ""; michael@0: michael@0: #define gMathPrimer gMathPrimerText michael@0: michael@0: SkAnimator::SkAnimator() : fMaker(NULL) { michael@0: initialize(); michael@0: } michael@0: michael@0: SkAnimator::~SkAnimator() { michael@0: SkDELETE(fMaker); michael@0: } michael@0: michael@0: void SkAnimator::addExtras(SkExtras* extras) { michael@0: *fMaker->fExtras.append() = extras; michael@0: } michael@0: michael@0: bool SkAnimator::appendStream(SkStream* stream) { michael@0: return decodeStream(stream); michael@0: } michael@0: michael@0: bool SkAnimator::decodeMemory(const void* buffer, size_t size) michael@0: { michael@0: fMaker->fFileName.reset(); michael@0: SkDisplayXMLParser parser(*fMaker); michael@0: return parser.parse((const char*)buffer, size); michael@0: } michael@0: michael@0: bool SkAnimator::decodeStream(SkStream* stream) michael@0: { michael@0: SkDisplayXMLParser parser(*fMaker); michael@0: bool result = parser.parse(*stream); michael@0: fMaker->setErrorString(); michael@0: return result; michael@0: } michael@0: michael@0: bool SkAnimator::decodeDOM(const SkDOM& dom, const SkDOMNode* node) michael@0: { michael@0: fMaker->fFileName.reset(); michael@0: SkDisplayXMLParser parser(*fMaker); michael@0: return parser.parse(dom, node); michael@0: } michael@0: michael@0: bool SkAnimator::decodeURI(const char uri[]) { michael@0: // SkDebugf("animator decode %s\n", uri); michael@0: michael@0: // SkStream* stream = SkStream::GetURIStream(fMaker->fPrefix.c_str(), uri); michael@0: SkAutoTUnref stream(SkStream::NewFromFile(uri)); michael@0: if (stream.get()) { michael@0: this->setURIBase(uri); michael@0: return decodeStream(stream); michael@0: } else { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: bool SkAnimator::doCharEvent(SkUnichar code) { michael@0: if (code == 0) michael@0: return false; michael@0: struct SkEventState state; michael@0: state.fCode = code; michael@0: fMaker->fEnableTime = fMaker->getAppTime(); michael@0: bool result = fMaker->fEvents.doEvent(*fMaker, SkDisplayEvent::kKeyChar, &state); michael@0: fMaker->notifyInval(); michael@0: return result; michael@0: } michael@0: michael@0: bool SkAnimator::doClickEvent(int clickState, SkScalar x, SkScalar y) { michael@0: SkASSERT(clickState >= 0 && clickState <= 2); michael@0: struct SkEventState state; michael@0: state.fX = x; michael@0: state.fY = y; michael@0: fMaker->fEnableTime = fMaker->getAppTime(); michael@0: bool result = fMaker->fEvents.doEvent(*fMaker, michael@0: clickState == 0 ? SkDisplayEvent::kMouseDown : michael@0: clickState == 1 ? SkDisplayEvent::kMouseDrag : michael@0: SkDisplayEvent::kMouseUp, &state); michael@0: fMaker->notifyInval(); michael@0: return result; michael@0: } michael@0: michael@0: bool SkAnimator::doKeyEvent(SkKey code) { michael@0: if (code == 0) michael@0: return false; michael@0: struct SkEventState state; michael@0: state.fCode = code; michael@0: fMaker->fEnableTime = fMaker->getAppTime(); michael@0: bool result = fMaker->fEvents.doEvent(*fMaker, SkDisplayEvent::kKeyPress, &state); michael@0: fMaker->notifyInval(); michael@0: return result; michael@0: } michael@0: michael@0: bool SkAnimator::doKeyUpEvent(SkKey code) { michael@0: if (code == 0) michael@0: return false; michael@0: struct SkEventState state; michael@0: state.fCode = code; michael@0: fMaker->fEnableTime = fMaker->getAppTime(); michael@0: bool result = fMaker->fEvents.doEvent(*fMaker, SkDisplayEvent::kKeyPressUp, &state); michael@0: fMaker->notifyInval(); michael@0: return result; michael@0: } michael@0: michael@0: bool SkAnimator::doUserEvent(const SkEvent& evt) { michael@0: fMaker->fEnableTime = fMaker->getAppTime(); michael@0: return onEvent(evt); michael@0: } michael@0: michael@0: SkAnimator::DifferenceType SkAnimator::draw(SkCanvas* canvas, SkPaint* paint, SkMSec time) { michael@0: if (paint == NULL) michael@0: return draw(canvas, time); michael@0: fMaker->fScreenplay.time = time; michael@0: fMaker->fCanvas = canvas; michael@0: fMaker->fPaint = paint; michael@0: fMaker->fDisplayList.fHasUnion = false; michael@0: int result = fMaker->fDisplayList.draw(*fMaker, time); michael@0: if (result) michael@0: result += fMaker->fDisplayList.fHasUnion; michael@0: return (DifferenceType) result; michael@0: } michael@0: michael@0: SkAnimator::DifferenceType SkAnimator::draw(SkCanvas* canvas, SkMSec time) { michael@0: SkPaint paint; michael@0: return draw(canvas, &paint, time); michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: void SkAnimator::eventDone(const SkEvent& ) { michael@0: } michael@0: #endif michael@0: michael@0: bool SkAnimator::findClickEvent(SkScalar x, SkScalar y) { michael@0: struct SkEventState state; michael@0: state.fDisable = true; michael@0: state.fX = x; michael@0: state.fY = y; michael@0: fMaker->fEnableTime = fMaker->getAppTime(); michael@0: bool result = fMaker->fEvents.doEvent(*fMaker, SkDisplayEvent::kMouseDown, &state); michael@0: fMaker->notifyInval(); michael@0: return result; michael@0: } michael@0: michael@0: const SkAnimator* SkAnimator::getAnimator(const SkDisplayable* displayable) const { michael@0: if (displayable->getType() != SkType_Movie) michael@0: return NULL; michael@0: const SkDisplayMovie* movie = (const SkDisplayMovie*) displayable; michael@0: return movie->getAnimator(); michael@0: } michael@0: michael@0: const SkDisplayable* SkAnimator::getElement(const char* id) { michael@0: SkDisplayable* element; michael@0: if (fMaker->find(id, &element) == false) michael@0: return NULL; michael@0: return (const SkDisplayable*) element; michael@0: } michael@0: michael@0: SkElementType SkAnimator::getElementType(const SkDisplayable* ae) { michael@0: SkDisplayable* element = (SkDisplayable*) ae; michael@0: const SkMemberInfo* info = SkDisplayType::GetMembers(fMaker, element->getType(), NULL); michael@0: return (SkElementType) SkDisplayType::Find(fMaker, info); michael@0: } michael@0: michael@0: SkElementType SkAnimator::getElementType(const char* id) { michael@0: const SkDisplayable* element = getElement(id); michael@0: return getElementType(element); michael@0: } michael@0: michael@0: const SkMemberInfo* SkAnimator::getField(const SkDisplayable* ae, const char* field) { michael@0: SkDisplayable* element = (SkDisplayable*) ae; michael@0: const SkMemberInfo* info = element->getMember(field); michael@0: return (const SkMemberInfo*) info; michael@0: } michael@0: michael@0: const SkMemberInfo* SkAnimator::getField(const char* elementID, const char* field) { michael@0: const SkDisplayable* element = getElement(elementID); michael@0: return getField(element, field); michael@0: } michael@0: michael@0: SkFieldType SkAnimator::getFieldType(const SkMemberInfo* ai) { michael@0: const SkMemberInfo* info = (const SkMemberInfo*) ai; michael@0: return (SkFieldType) info->getType(); michael@0: } michael@0: michael@0: SkFieldType SkAnimator::getFieldType(const char* id, const char* fieldID) { michael@0: const SkMemberInfo* field = getField(id, fieldID); michael@0: return getFieldType(field); michael@0: } michael@0: michael@0: static bool getArrayCommon(const SkDisplayable* ae, const SkMemberInfo* ai, michael@0: int index, SkOperand* operand) { michael@0: const SkDisplayable* element = (const SkDisplayable*) ae; michael@0: const SkMemberInfo* info = (const SkMemberInfo*) ai; michael@0: SkASSERT(info->fType == SkType_Array); michael@0: return info->getArrayValue(element, index, operand); michael@0: } michael@0: michael@0: int32_t SkAnimator::getArrayInt(const SkDisplayable* ae, michael@0: const SkMemberInfo* ai, int index) { michael@0: SkOperand operand; michael@0: bool result = getArrayCommon(ae, ai, index, &operand); michael@0: return result ? operand.fS32 : SK_NaN32; michael@0: } michael@0: michael@0: int32_t SkAnimator::getArrayInt(const char* id, const char* fieldID, int index) { michael@0: const SkDisplayable* element = getElement(id); michael@0: if (element == NULL) michael@0: return SK_NaN32; michael@0: const SkMemberInfo* field = getField(element, fieldID); michael@0: if (field == NULL) michael@0: return SK_NaN32; michael@0: return getArrayInt(element, field, index); michael@0: } michael@0: michael@0: SkScalar SkAnimator::getArrayScalar(const SkDisplayable* ae, michael@0: const SkMemberInfo* ai, int index) { michael@0: SkOperand operand; michael@0: bool result = getArrayCommon(ae, ai, index, &operand); michael@0: return result ? operand.fScalar : SK_ScalarNaN; michael@0: } michael@0: michael@0: SkScalar SkAnimator::getArrayScalar(const char* id, const char* fieldID, int index) { michael@0: const SkDisplayable* element = getElement(id); michael@0: if (element == NULL) michael@0: return SK_ScalarNaN; michael@0: const SkMemberInfo* field = getField(element, fieldID); michael@0: if (field == NULL) michael@0: return SK_ScalarNaN; michael@0: return getArrayScalar(element, field, index); michael@0: } michael@0: michael@0: const char* SkAnimator::getArrayString(const SkDisplayable* ae, michael@0: const SkMemberInfo* ai, int index) { michael@0: SkOperand operand; michael@0: bool result = getArrayCommon(ae, ai, index, &operand); michael@0: return result ? operand.fString->c_str() : NULL; michael@0: } michael@0: michael@0: const char* SkAnimator::getArrayString(const char* id, const char* fieldID, int index) { michael@0: const SkDisplayable* element = getElement(id); michael@0: if (element == NULL) michael@0: return NULL; michael@0: const SkMemberInfo* field = getField(element, fieldID); michael@0: if (field == NULL) michael@0: return NULL; michael@0: return getArrayString(element, field, index); michael@0: } michael@0: michael@0: SkMSec SkAnimator::getInterval() { michael@0: return fMaker->fMinimumInterval == (SkMSec) -1 ? 0 : fMaker->fMinimumInterval; michael@0: } michael@0: michael@0: void SkAnimator::getInvalBounds(SkRect* inval) { michael@0: if (fMaker->fDisplayList.fHasUnion) { michael@0: inval->fLeft = SkIntToScalar(fMaker->fDisplayList.fInvalBounds.fLeft); michael@0: inval->fTop = SkIntToScalar(fMaker->fDisplayList.fInvalBounds.fTop); michael@0: inval->fRight = SkIntToScalar(fMaker->fDisplayList.fInvalBounds.fRight); michael@0: inval->fBottom = SkIntToScalar(fMaker->fDisplayList.fInvalBounds.fBottom); michael@0: } else { michael@0: inval->fLeft = inval->fTop = -SK_ScalarMax; michael@0: inval->fRight = inval->fBottom = SK_ScalarMax; michael@0: } michael@0: } michael@0: michael@0: const SkXMLParserError* SkAnimator::getParserError() { michael@0: return &fMaker->fError; michael@0: } michael@0: michael@0: const char* SkAnimator::getParserErrorString() { michael@0: if (fMaker->fErrorString.size() == 0 && fMaker->fError.hasError()) michael@0: fMaker->setErrorString(); michael@0: return fMaker->fErrorString.c_str(); michael@0: } michael@0: michael@0: int32_t SkAnimator::getInt(const SkDisplayable* element, const SkMemberInfo* info) { michael@0: if (info->fType != SkType_MemberProperty) { michael@0: SkOperand operand; michael@0: if (info->getType() == SkType_Int) { michael@0: info->getValue(element, &operand, 1); michael@0: return operand.fS32; michael@0: } michael@0: return SK_NaN32; michael@0: } michael@0: SkScriptValue scriptValue; michael@0: bool success = element->getProperty(info->propertyIndex(), &scriptValue); michael@0: if (success && scriptValue.fType == SkType_Int) michael@0: return scriptValue.fOperand.fS32; michael@0: return SK_NaN32; michael@0: } michael@0: michael@0: int32_t SkAnimator::getInt(const char* id, const char* fieldID) { michael@0: const SkDisplayable* element = getElement(id); michael@0: if (element == NULL) michael@0: return SK_NaN32; michael@0: const SkMemberInfo* field = getField(element, fieldID); michael@0: if (field == NULL) michael@0: return SK_NaN32; michael@0: return getInt(element, field); michael@0: } michael@0: michael@0: SkScalar SkAnimator::getScalar(const SkDisplayable* element, const SkMemberInfo* info) { michael@0: if (info->fType != SkType_MemberProperty) { michael@0: SkOperand operand; michael@0: if (info->getType() == SkType_Float) { michael@0: info->getValue(element, &operand, 1); michael@0: return operand.fScalar; michael@0: } michael@0: return SK_ScalarNaN; michael@0: } michael@0: SkScriptValue scriptValue; michael@0: bool success = element->getProperty(info->propertyIndex(), &scriptValue); michael@0: if (success && scriptValue.fType == SkType_Float) michael@0: return scriptValue.fOperand.fScalar; michael@0: return SK_ScalarNaN; michael@0: } michael@0: michael@0: SkScalar SkAnimator::getScalar(const char* id, const char* fieldID) { michael@0: const SkDisplayable* element = getElement(id); michael@0: if (element == NULL) michael@0: return SK_ScalarNaN; michael@0: const SkMemberInfo* field = getField(element, fieldID); michael@0: if (field == NULL) michael@0: return SK_ScalarNaN; michael@0: return getScalar(element, field); michael@0: } michael@0: michael@0: const char* SkAnimator::getString(const SkDisplayable* ae, michael@0: const SkMemberInfo* ai) { michael@0: const SkDisplayable* element = (const SkDisplayable*) ae; michael@0: const SkMemberInfo* info = (const SkMemberInfo*) ai; michael@0: SkString* temp; michael@0: info->getString(element, &temp); michael@0: return temp->c_str(); michael@0: } michael@0: michael@0: const char* SkAnimator::getString(const char* id, const char* fieldID) { michael@0: const SkDisplayable* element = getElement(id); michael@0: if (element == NULL) michael@0: return NULL; michael@0: const SkMemberInfo* field = getField(element, fieldID); michael@0: if (field == NULL) michael@0: return NULL; michael@0: return getString(element, field); michael@0: } michael@0: michael@0: const char* SkAnimator::getURIBase() { michael@0: return fMaker->fPrefix.c_str(); michael@0: } michael@0: michael@0: void SkAnimator::initialize() { michael@0: SkDELETE(fMaker); michael@0: fMaker = SkNEW_ARGS(SkAnimateMaker, (this, NULL, NULL)); michael@0: decodeMemory(gMathPrimer, sizeof(gMathPrimer)-1); michael@0: #ifdef SK_BUILD_FOR_ANDROID michael@0: InitializeSkExtraPathEffects(this); michael@0: #endif michael@0: } michael@0: michael@0: michael@0: #ifdef SK_DEBUG michael@0: bool SkAnimator::isTrackingEvents() { michael@0: return false; michael@0: } michael@0: #endif michael@0: michael@0: bool SkAnimator::onEvent(const SkEvent& evt) { michael@0: #ifdef SK_DEBUG michael@0: SkAnimator* root = fMaker->getRoot(); michael@0: if (root == NULL) michael@0: root = this; michael@0: if (root->isTrackingEvents()) michael@0: root->eventDone(evt); michael@0: #endif michael@0: if (evt.isType(SK_EventType_OnEnd)) { michael@0: SkEventState eventState; michael@0: SkDEBUGCODE(bool success =) evt.findPtr("anim", (void**) &eventState.fDisplayable); michael@0: SkASSERT(success); michael@0: SkDEBUGCODE(success =) evt.findS32("time", (int32_t*) &fMaker->fEnableTime); michael@0: SkASSERT(success); michael@0: fMaker->fAdjustedStart = fMaker->getAppTime() - fMaker->fEnableTime; michael@0: fMaker->fEvents.doEvent(*fMaker, SkDisplayEvent::kOnEnd, &eventState); michael@0: fMaker->fAdjustedStart = 0; michael@0: goto inval; michael@0: } michael@0: if (evt.isType(SK_EventType_Delay)) { michael@0: fMaker->doDelayedEvent(); michael@0: goto inval; michael@0: } michael@0: { michael@0: const char* id = evt.findString("id"); michael@0: if (id == NULL) michael@0: return false; michael@0: SkDisplayable** firstMovie = fMaker->fMovies.begin(); michael@0: SkDisplayable** endMovie = fMaker->fMovies.end(); michael@0: for (SkDisplayable** ptr = firstMovie; ptr < endMovie; ptr++) { michael@0: SkDisplayMovie* movie = (SkDisplayMovie*) *ptr; michael@0: movie->doEvent(evt); michael@0: } michael@0: { michael@0: SkDisplayable* event; michael@0: if (fMaker->find(id, &event) == false) michael@0: return false; michael@0: #if defined SK_DEBUG && defined SK_DEBUG_ANIMATION_TIMING michael@0: SkString debugOut; michael@0: SkMSec realTime = fMaker->getAppTime(); michael@0: debugOut.appendS32(realTime - fMaker->fDebugTimeBase); michael@0: debugOut.append(" onEvent id="); michael@0: debugOut.append(id); michael@0: #endif michael@0: SkMSec time = evt.getFast32(); michael@0: if (time != 0) { michael@0: SkMSec app = fMaker->getAppTime(); michael@0: fMaker->setEnableTime(app, time); michael@0: #if defined SK_DEBUG && defined SK_DEBUG_ANIMATION_TIMING michael@0: debugOut.append(" time="); michael@0: debugOut.appendS32(time - fMaker->fDebugTimeBase); michael@0: debugOut.append(" adjust="); michael@0: debugOut.appendS32(fMaker->fAdjustedStart); michael@0: #endif michael@0: } michael@0: #if defined SK_DEBUG && defined SK_DEBUG_ANIMATION_TIMING michael@0: SkDebugf("%s\n", debugOut.c_str()); michael@0: #endif michael@0: SkASSERT(event->isEvent()); michael@0: SkDisplayEvent* displayEvent = (SkDisplayEvent*) event; michael@0: displayEvent->populateInput(*fMaker, evt); michael@0: displayEvent->enableEvent(*fMaker); michael@0: } michael@0: } michael@0: inval: michael@0: fMaker->notifyInval(); michael@0: return true; michael@0: } michael@0: michael@0: void SkAnimator::onEventPost(SkEvent* evt, SkEventSinkID sinkID) michael@0: { michael@0: #ifdef SK_DEBUG michael@0: SkAnimator* root = fMaker->getRoot(); michael@0: if (root) { michael@0: root->onEventPost(evt, sinkID); michael@0: return; michael@0: } michael@0: #else michael@0: SkASSERT(sinkID == this->getSinkID() || this->getHostEventSinkID() == sinkID); michael@0: #endif michael@0: evt->setTargetID(sinkID)->post(); michael@0: } michael@0: michael@0: void SkAnimator::onEventPostTime(SkEvent* evt, SkEventSinkID sinkID, SkMSec time) michael@0: { michael@0: #ifdef SK_DEBUG michael@0: SkAnimator* root = fMaker->getRoot(); michael@0: if (root) { michael@0: root->onEventPostTime(evt, sinkID, time); michael@0: return; michael@0: } michael@0: #else michael@0: SkASSERT(sinkID == this->getSinkID() || this->getHostEventSinkID() == sinkID); michael@0: #endif michael@0: evt->setTargetID(sinkID)->postTime(time); michael@0: } michael@0: michael@0: void SkAnimator::reset() { michael@0: fMaker->fDisplayList.reset(); michael@0: } michael@0: michael@0: SkEventSinkID SkAnimator::getHostEventSinkID() const { michael@0: return fMaker->fHostEventSinkID; michael@0: } michael@0: michael@0: void SkAnimator::setHostEventSinkID(SkEventSinkID target) { michael@0: fMaker->fHostEventSinkID = target; michael@0: } michael@0: michael@0: void SkAnimator::onSetHostHandler(Handler ) { michael@0: } michael@0: michael@0: void SkAnimator::setJavaOwner(Handler ) { michael@0: } michael@0: michael@0: bool SkAnimator::setArrayString(const char* id, const char* fieldID, const char** array, int num) michael@0: { michael@0: SkTypedArray tArray(SkType_String); michael@0: tArray.setCount(num); michael@0: for (int i = 0; i < num; i++) { michael@0: SkOperand op; michael@0: op.fString = new SkString(array[i]); michael@0: tArray[i] = op; michael@0: } michael@0: return setArray(id, fieldID, tArray); michael@0: } michael@0: bool SkAnimator::setArrayInt(const char* id, const char* fieldID, const int* array, int num) michael@0: { michael@0: SkTypedArray tArray(SkType_Int); michael@0: tArray.setCount(num); michael@0: for (int i = 0; i < num; i++) { michael@0: SkOperand op; michael@0: op.fS32 = array[i]; michael@0: tArray[i] = op; michael@0: } michael@0: return setArray(id, fieldID, tArray); michael@0: } michael@0: michael@0: bool SkAnimator::setArray(SkDisplayable* element, const SkMemberInfo* info, SkTypedArray array) { michael@0: if (info->fType != SkType_Array) michael@0: return false; //the field is not an array michael@0: //i think we can handle the case where the displayable itself is an array differently from the michael@0: //case where it has an array - for one thing, if it is an array, i think we can change its type michael@0: //if it's not, we cannot michael@0: SkDisplayTypes type = element->getType(); michael@0: if (type == SkType_Array) { michael@0: SkDisplayArray* dispArray = (SkDisplayArray*) element; michael@0: dispArray->values = array; michael@0: return true; michael@0: } michael@0: else michael@0: return false; //currently i don't care about this case michael@0: } michael@0: michael@0: bool SkAnimator::setArray(const char* id, const char* fieldID, SkTypedArray array) { michael@0: SkDisplayable* element = (SkDisplayable*) getElement(id); michael@0: //should I go ahead and change all 'NULL's to 'NULL'? michael@0: if (element == NULL) michael@0: return false; michael@0: const SkMemberInfo* field = getField(element, fieldID); michael@0: if (field == NULL) michael@0: return false; michael@0: return setArray(element, field, array); michael@0: } michael@0: michael@0: bool SkAnimator::setInt(SkDisplayable* element, const SkMemberInfo* info, int32_t s32) { michael@0: if (info->fType != SkType_MemberProperty) { michael@0: SkOperand operand; michael@0: operand.fS32 = s32; michael@0: SkASSERT(info->getType() == SkType_Int); michael@0: info->setValue(element, &operand, 1); michael@0: } else { michael@0: SkScriptValue scriptValue; michael@0: scriptValue.fType = SkType_Int; michael@0: scriptValue.fOperand.fS32 = s32; michael@0: element->setProperty(info->propertyIndex(), scriptValue); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool SkAnimator::setInt(const char* id, const char* fieldID, int32_t s32) { michael@0: SkDisplayable* element = (SkDisplayable*) getElement(id); michael@0: if (element == NULL) michael@0: return false; michael@0: const SkMemberInfo* field = getField(element, fieldID); michael@0: if (field == NULL) michael@0: return false; michael@0: return setInt(element, field, s32); michael@0: } michael@0: michael@0: bool SkAnimator::setScalar(SkDisplayable* element, const SkMemberInfo* info, SkScalar scalar) { michael@0: if (info->fType != SkType_MemberProperty) { michael@0: SkOperand operand; michael@0: operand.fScalar = scalar; michael@0: SkASSERT(info->getType() == SkType_Float); michael@0: info->setValue(element, &operand, 1); michael@0: } else { michael@0: SkScriptValue scriptValue; michael@0: scriptValue.fType = SkType_Float; michael@0: scriptValue.fOperand.fScalar = scalar; michael@0: element->setProperty(info->propertyIndex(), scriptValue); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool SkAnimator::setScalar(const char* id, const char* fieldID, SkScalar scalar) { michael@0: SkDisplayable* element = (SkDisplayable*) getElement(id); michael@0: if (element == NULL) michael@0: return false; michael@0: const SkMemberInfo* field = getField(element, fieldID); michael@0: if (field == NULL) michael@0: return false; michael@0: return setScalar(element, field, scalar); michael@0: } michael@0: michael@0: bool SkAnimator::setString(SkDisplayable* element, michael@0: const SkMemberInfo* info, const char* str) { michael@0: // !!! until this is fixed, can't call script with global references from here michael@0: info->setValue(*fMaker, NULL, 0, info->fCount, element, info->getType(), str, strlen(str)); michael@0: return true; michael@0: } michael@0: michael@0: bool SkAnimator::setString(const char* id, const char* fieldID, const char* str) { michael@0: SkDisplayable* element = (SkDisplayable*) getElement(id); michael@0: if (element == NULL) michael@0: return false; michael@0: const SkMemberInfo* field = getField(element, fieldID); michael@0: if (field == NULL) michael@0: return false; michael@0: return setString(element, field, str); michael@0: } michael@0: michael@0: void SkAnimator::setTimeline(const Timeline& timeline) { michael@0: fMaker->fTimeline = &timeline; michael@0: } michael@0: michael@0: void SkAnimator::setURIBase(const char* uri) { michael@0: if (uri) michael@0: { michael@0: const char* tail = strrchr(uri, '/'); michael@0: if (tail) { michael@0: SkString prefix(uri, tail - uri + 1); michael@0: if (uri[0] != '.' /*SkStream::IsAbsoluteURI(uri)*/) michael@0: fMaker->fPrefix.reset(); michael@0: fMaker->fPrefix.append(prefix); michael@0: fMaker->fFileName.set(tail + 1); michael@0: } else michael@0: fMaker->fFileName.set(uri); michael@0: } michael@0: } michael@0: michael@0: #ifdef SK_DEBUG michael@0: bool SkAnimator::NoLeaks() { michael@0: #ifdef SK_BUILD_FOR_MAC michael@0: if (SkDisplayable::fAllocations.count() == 0) michael@0: return true; michael@0: // return SkDisplayable::fAllocationCount == 0; michael@0: SkDebugf("!!! leaked %d displayables:\n", SkDisplayable::fAllocations.count()); michael@0: for (SkDisplayable** leak = SkDisplayable::fAllocations.begin(); leak < SkDisplayable::fAllocations.end(); leak++) michael@0: SkDebugf("%08x %s\n", *leak, (*leak)->id); michael@0: #endif michael@0: return false; michael@0: } michael@0: #endif michael@0: michael@0: #ifdef SK_SUPPORT_UNITTEST michael@0: #include "SkAnimatorScript.h" michael@0: #include "SkBase64.h" michael@0: #include "SkParse.h" michael@0: #include "SkMemberInfo.h" michael@0: michael@0: #define unittestline(type) { #type , type::UnitTest } michael@0: #endif michael@0: michael@0: michael@0: #ifdef SK_SUPPORT_UNITTEST michael@0: void SkAnimator::Init(bool runUnitTests) { michael@0: if (runUnitTests == false) michael@0: return; michael@0: static const struct { michael@0: const char* fTypeName; michael@0: void (*fUnitTest)( ); michael@0: } gUnitTests[] = { michael@0: unittestline(SkBase64), michael@0: unittestline(SkDisplayType), michael@0: unittestline(SkParse), michael@0: unittestline(SkScriptEngine), michael@0: // unittestline(SkScriptEngine2), // compiled script experiment michael@0: unittestline(SkAnimatorScript) michael@0: }; michael@0: for (int i = 0; i < (int)SK_ARRAY_COUNT(gUnitTests); i++) michael@0: { michael@0: SkDebugf("SkAnimator: Running UnitTest for %s\n", gUnitTests[i].fTypeName); michael@0: gUnitTests[i].fUnitTest(); michael@0: SkDebugf("SkAnimator: End UnitTest for %s\n", gUnitTests[i].fTypeName); michael@0: } michael@0: } michael@0: #else michael@0: void SkAnimator::Init(bool) {} michael@0: #endif michael@0: michael@0: void SkAnimator::Term() { michael@0: }