3 * Copyright 2006 The Android Open Source Project
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "SkAnimator.h"
11 #include "SkAnimateMaker.h"
13 #include "SkDisplayApply.h"
14 #include "SkDisplayMovie.h"
15 #include "SkDisplayTypes.h"
16 #include "SkDisplayXMLParser.h"
19 #include "SkScript2.h" // compiled script experiment
20 #include "SkSystemEventTypes.h"
21 #include "SkTypedArray.h"
22 #ifdef SK_BUILD_FOR_ANDROID
23 #include "SkDrawExtraPathEffect.h"
29 #if defined SK_BUILD_FOR_WIN32 && defined SK_DEBUG
31 extern const char gMathPrimerText[];
32 extern const char gMathPrimerBinary[];
34 #define _static static
37 _static const char gMathPrimerText[] =
40 "<Number id=\"Number\"/>"
43 #define gMathPrimer gMathPrimerText
45 SkAnimator::SkAnimator() : fMaker(NULL) {
49 SkAnimator::~SkAnimator() {
53 void SkAnimator::addExtras(SkExtras* extras) {
54 *fMaker->fExtras.append() = extras;
57 bool SkAnimator::appendStream(SkStream* stream) {
58 return decodeStream(stream);
61 bool SkAnimator::decodeMemory(const void* buffer, size_t size)
63 fMaker->fFileName.reset();
64 SkDisplayXMLParser parser(*fMaker);
65 return parser.parse((const char*)buffer, size);
68 bool SkAnimator::decodeStream(SkStream* stream)
70 SkDisplayXMLParser parser(*fMaker);
71 bool result = parser.parse(*stream);
72 fMaker->setErrorString();
76 bool SkAnimator::decodeDOM(const SkDOM& dom, const SkDOMNode* node)
78 fMaker->fFileName.reset();
79 SkDisplayXMLParser parser(*fMaker);
80 return parser.parse(dom, node);
83 bool SkAnimator::decodeURI(const char uri[]) {
84 // SkDebugf("animator decode %s\n", uri);
86 // SkStream* stream = SkStream::GetURIStream(fMaker->fPrefix.c_str(), uri);
87 SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(uri));
89 this->setURIBase(uri);
90 return decodeStream(stream);
96 bool SkAnimator::doCharEvent(SkUnichar code) {
99 struct SkEventState state;
101 fMaker->fEnableTime = fMaker->getAppTime();
102 bool result = fMaker->fEvents.doEvent(*fMaker, SkDisplayEvent::kKeyChar, &state);
103 fMaker->notifyInval();
107 bool SkAnimator::doClickEvent(int clickState, SkScalar x, SkScalar y) {
108 SkASSERT(clickState >= 0 && clickState <= 2);
109 struct SkEventState state;
112 fMaker->fEnableTime = fMaker->getAppTime();
113 bool result = fMaker->fEvents.doEvent(*fMaker,
114 clickState == 0 ? SkDisplayEvent::kMouseDown :
115 clickState == 1 ? SkDisplayEvent::kMouseDrag :
116 SkDisplayEvent::kMouseUp, &state);
117 fMaker->notifyInval();
121 bool SkAnimator::doKeyEvent(SkKey code) {
124 struct SkEventState state;
126 fMaker->fEnableTime = fMaker->getAppTime();
127 bool result = fMaker->fEvents.doEvent(*fMaker, SkDisplayEvent::kKeyPress, &state);
128 fMaker->notifyInval();
132 bool SkAnimator::doKeyUpEvent(SkKey code) {
135 struct SkEventState state;
137 fMaker->fEnableTime = fMaker->getAppTime();
138 bool result = fMaker->fEvents.doEvent(*fMaker, SkDisplayEvent::kKeyPressUp, &state);
139 fMaker->notifyInval();
143 bool SkAnimator::doUserEvent(const SkEvent& evt) {
144 fMaker->fEnableTime = fMaker->getAppTime();
148 SkAnimator::DifferenceType SkAnimator::draw(SkCanvas* canvas, SkPaint* paint, SkMSec time) {
150 return draw(canvas, time);
151 fMaker->fScreenplay.time = time;
152 fMaker->fCanvas = canvas;
153 fMaker->fPaint = paint;
154 fMaker->fDisplayList.fHasUnion = false;
155 int result = fMaker->fDisplayList.draw(*fMaker, time);
157 result += fMaker->fDisplayList.fHasUnion;
158 return (DifferenceType) result;
161 SkAnimator::DifferenceType SkAnimator::draw(SkCanvas* canvas, SkMSec time) {
163 return draw(canvas, &paint, time);
167 void SkAnimator::eventDone(const SkEvent& ) {
171 bool SkAnimator::findClickEvent(SkScalar x, SkScalar y) {
172 struct SkEventState state;
173 state.fDisable = true;
176 fMaker->fEnableTime = fMaker->getAppTime();
177 bool result = fMaker->fEvents.doEvent(*fMaker, SkDisplayEvent::kMouseDown, &state);
178 fMaker->notifyInval();
182 const SkAnimator* SkAnimator::getAnimator(const SkDisplayable* displayable) const {
183 if (displayable->getType() != SkType_Movie)
185 const SkDisplayMovie* movie = (const SkDisplayMovie*) displayable;
186 return movie->getAnimator();
189 const SkDisplayable* SkAnimator::getElement(const char* id) {
190 SkDisplayable* element;
191 if (fMaker->find(id, &element) == false)
193 return (const SkDisplayable*) element;
196 SkElementType SkAnimator::getElementType(const SkDisplayable* ae) {
197 SkDisplayable* element = (SkDisplayable*) ae;
198 const SkMemberInfo* info = SkDisplayType::GetMembers(fMaker, element->getType(), NULL);
199 return (SkElementType) SkDisplayType::Find(fMaker, info);
202 SkElementType SkAnimator::getElementType(const char* id) {
203 const SkDisplayable* element = getElement(id);
204 return getElementType(element);
207 const SkMemberInfo* SkAnimator::getField(const SkDisplayable* ae, const char* field) {
208 SkDisplayable* element = (SkDisplayable*) ae;
209 const SkMemberInfo* info = element->getMember(field);
210 return (const SkMemberInfo*) info;
213 const SkMemberInfo* SkAnimator::getField(const char* elementID, const char* field) {
214 const SkDisplayable* element = getElement(elementID);
215 return getField(element, field);
218 SkFieldType SkAnimator::getFieldType(const SkMemberInfo* ai) {
219 const SkMemberInfo* info = (const SkMemberInfo*) ai;
220 return (SkFieldType) info->getType();
223 SkFieldType SkAnimator::getFieldType(const char* id, const char* fieldID) {
224 const SkMemberInfo* field = getField(id, fieldID);
225 return getFieldType(field);
228 static bool getArrayCommon(const SkDisplayable* ae, const SkMemberInfo* ai,
229 int index, SkOperand* operand) {
230 const SkDisplayable* element = (const SkDisplayable*) ae;
231 const SkMemberInfo* info = (const SkMemberInfo*) ai;
232 SkASSERT(info->fType == SkType_Array);
233 return info->getArrayValue(element, index, operand);
236 int32_t SkAnimator::getArrayInt(const SkDisplayable* ae,
237 const SkMemberInfo* ai, int index) {
239 bool result = getArrayCommon(ae, ai, index, &operand);
240 return result ? operand.fS32 : SK_NaN32;
243 int32_t SkAnimator::getArrayInt(const char* id, const char* fieldID, int index) {
244 const SkDisplayable* element = getElement(id);
247 const SkMemberInfo* field = getField(element, fieldID);
250 return getArrayInt(element, field, index);
253 SkScalar SkAnimator::getArrayScalar(const SkDisplayable* ae,
254 const SkMemberInfo* ai, int index) {
256 bool result = getArrayCommon(ae, ai, index, &operand);
257 return result ? operand.fScalar : SK_ScalarNaN;
260 SkScalar SkAnimator::getArrayScalar(const char* id, const char* fieldID, int index) {
261 const SkDisplayable* element = getElement(id);
264 const SkMemberInfo* field = getField(element, fieldID);
267 return getArrayScalar(element, field, index);
270 const char* SkAnimator::getArrayString(const SkDisplayable* ae,
271 const SkMemberInfo* ai, int index) {
273 bool result = getArrayCommon(ae, ai, index, &operand);
274 return result ? operand.fString->c_str() : NULL;
277 const char* SkAnimator::getArrayString(const char* id, const char* fieldID, int index) {
278 const SkDisplayable* element = getElement(id);
281 const SkMemberInfo* field = getField(element, fieldID);
284 return getArrayString(element, field, index);
287 SkMSec SkAnimator::getInterval() {
288 return fMaker->fMinimumInterval == (SkMSec) -1 ? 0 : fMaker->fMinimumInterval;
291 void SkAnimator::getInvalBounds(SkRect* inval) {
292 if (fMaker->fDisplayList.fHasUnion) {
293 inval->fLeft = SkIntToScalar(fMaker->fDisplayList.fInvalBounds.fLeft);
294 inval->fTop = SkIntToScalar(fMaker->fDisplayList.fInvalBounds.fTop);
295 inval->fRight = SkIntToScalar(fMaker->fDisplayList.fInvalBounds.fRight);
296 inval->fBottom = SkIntToScalar(fMaker->fDisplayList.fInvalBounds.fBottom);
298 inval->fLeft = inval->fTop = -SK_ScalarMax;
299 inval->fRight = inval->fBottom = SK_ScalarMax;
303 const SkXMLParserError* SkAnimator::getParserError() {
304 return &fMaker->fError;
307 const char* SkAnimator::getParserErrorString() {
308 if (fMaker->fErrorString.size() == 0 && fMaker->fError.hasError())
309 fMaker->setErrorString();
310 return fMaker->fErrorString.c_str();
313 int32_t SkAnimator::getInt(const SkDisplayable* element, const SkMemberInfo* info) {
314 if (info->fType != SkType_MemberProperty) {
316 if (info->getType() == SkType_Int) {
317 info->getValue(element, &operand, 1);
322 SkScriptValue scriptValue;
323 bool success = element->getProperty(info->propertyIndex(), &scriptValue);
324 if (success && scriptValue.fType == SkType_Int)
325 return scriptValue.fOperand.fS32;
329 int32_t SkAnimator::getInt(const char* id, const char* fieldID) {
330 const SkDisplayable* element = getElement(id);
333 const SkMemberInfo* field = getField(element, fieldID);
336 return getInt(element, field);
339 SkScalar SkAnimator::getScalar(const SkDisplayable* element, const SkMemberInfo* info) {
340 if (info->fType != SkType_MemberProperty) {
342 if (info->getType() == SkType_Float) {
343 info->getValue(element, &operand, 1);
344 return operand.fScalar;
348 SkScriptValue scriptValue;
349 bool success = element->getProperty(info->propertyIndex(), &scriptValue);
350 if (success && scriptValue.fType == SkType_Float)
351 return scriptValue.fOperand.fScalar;
355 SkScalar SkAnimator::getScalar(const char* id, const char* fieldID) {
356 const SkDisplayable* element = getElement(id);
359 const SkMemberInfo* field = getField(element, fieldID);
362 return getScalar(element, field);
365 const char* SkAnimator::getString(const SkDisplayable* ae,
366 const SkMemberInfo* ai) {
367 const SkDisplayable* element = (const SkDisplayable*) ae;
368 const SkMemberInfo* info = (const SkMemberInfo*) ai;
370 info->getString(element, &temp);
371 return temp->c_str();
374 const char* SkAnimator::getString(const char* id, const char* fieldID) {
375 const SkDisplayable* element = getElement(id);
378 const SkMemberInfo* field = getField(element, fieldID);
381 return getString(element, field);
384 const char* SkAnimator::getURIBase() {
385 return fMaker->fPrefix.c_str();
388 void SkAnimator::initialize() {
390 fMaker = SkNEW_ARGS(SkAnimateMaker, (this, NULL, NULL));
391 decodeMemory(gMathPrimer, sizeof(gMathPrimer)-1);
392 #ifdef SK_BUILD_FOR_ANDROID
393 InitializeSkExtraPathEffects(this);
399 bool SkAnimator::isTrackingEvents() {
404 bool SkAnimator::onEvent(const SkEvent& evt) {
406 SkAnimator* root = fMaker->getRoot();
409 if (root->isTrackingEvents())
410 root->eventDone(evt);
412 if (evt.isType(SK_EventType_OnEnd)) {
413 SkEventState eventState;
414 SkDEBUGCODE(bool success =) evt.findPtr("anim", (void**) &eventState.fDisplayable);
416 SkDEBUGCODE(success =) evt.findS32("time", (int32_t*) &fMaker->fEnableTime);
418 fMaker->fAdjustedStart = fMaker->getAppTime() - fMaker->fEnableTime;
419 fMaker->fEvents.doEvent(*fMaker, SkDisplayEvent::kOnEnd, &eventState);
420 fMaker->fAdjustedStart = 0;
423 if (evt.isType(SK_EventType_Delay)) {
424 fMaker->doDelayedEvent();
428 const char* id = evt.findString("id");
431 SkDisplayable** firstMovie = fMaker->fMovies.begin();
432 SkDisplayable** endMovie = fMaker->fMovies.end();
433 for (SkDisplayable** ptr = firstMovie; ptr < endMovie; ptr++) {
434 SkDisplayMovie* movie = (SkDisplayMovie*) *ptr;
438 SkDisplayable* event;
439 if (fMaker->find(id, &event) == false)
441 #if defined SK_DEBUG && defined SK_DEBUG_ANIMATION_TIMING
443 SkMSec realTime = fMaker->getAppTime();
444 debugOut.appendS32(realTime - fMaker->fDebugTimeBase);
445 debugOut.append(" onEvent id=");
448 SkMSec time = evt.getFast32();
450 SkMSec app = fMaker->getAppTime();
451 fMaker->setEnableTime(app, time);
452 #if defined SK_DEBUG && defined SK_DEBUG_ANIMATION_TIMING
453 debugOut.append(" time=");
454 debugOut.appendS32(time - fMaker->fDebugTimeBase);
455 debugOut.append(" adjust=");
456 debugOut.appendS32(fMaker->fAdjustedStart);
459 #if defined SK_DEBUG && defined SK_DEBUG_ANIMATION_TIMING
460 SkDebugf("%s\n", debugOut.c_str());
462 SkASSERT(event->isEvent());
463 SkDisplayEvent* displayEvent = (SkDisplayEvent*) event;
464 displayEvent->populateInput(*fMaker, evt);
465 displayEvent->enableEvent(*fMaker);
469 fMaker->notifyInval();
473 void SkAnimator::onEventPost(SkEvent* evt, SkEventSinkID sinkID)
476 SkAnimator* root = fMaker->getRoot();
478 root->onEventPost(evt, sinkID);
482 SkASSERT(sinkID == this->getSinkID() || this->getHostEventSinkID() == sinkID);
484 evt->setTargetID(sinkID)->post();
487 void SkAnimator::onEventPostTime(SkEvent* evt, SkEventSinkID sinkID, SkMSec time)
490 SkAnimator* root = fMaker->getRoot();
492 root->onEventPostTime(evt, sinkID, time);
496 SkASSERT(sinkID == this->getSinkID() || this->getHostEventSinkID() == sinkID);
498 evt->setTargetID(sinkID)->postTime(time);
501 void SkAnimator::reset() {
502 fMaker->fDisplayList.reset();
505 SkEventSinkID SkAnimator::getHostEventSinkID() const {
506 return fMaker->fHostEventSinkID;
509 void SkAnimator::setHostEventSinkID(SkEventSinkID target) {
510 fMaker->fHostEventSinkID = target;
513 void SkAnimator::onSetHostHandler(Handler ) {
516 void SkAnimator::setJavaOwner(Handler ) {
519 bool SkAnimator::setArrayString(const char* id, const char* fieldID, const char** array, int num)
521 SkTypedArray tArray(SkType_String);
522 tArray.setCount(num);
523 for (int i = 0; i < num; i++) {
525 op.fString = new SkString(array[i]);
528 return setArray(id, fieldID, tArray);
530 bool SkAnimator::setArrayInt(const char* id, const char* fieldID, const int* array, int num)
532 SkTypedArray tArray(SkType_Int);
533 tArray.setCount(num);
534 for (int i = 0; i < num; i++) {
539 return setArray(id, fieldID, tArray);
542 bool SkAnimator::setArray(SkDisplayable* element, const SkMemberInfo* info, SkTypedArray array) {
543 if (info->fType != SkType_Array)
544 return false; //the field is not an array
545 //i think we can handle the case where the displayable itself is an array differently from the
546 //case where it has an array - for one thing, if it is an array, i think we can change its type
547 //if it's not, we cannot
548 SkDisplayTypes type = element->getType();
549 if (type == SkType_Array) {
550 SkDisplayArray* dispArray = (SkDisplayArray*) element;
551 dispArray->values = array;
555 return false; //currently i don't care about this case
558 bool SkAnimator::setArray(const char* id, const char* fieldID, SkTypedArray array) {
559 SkDisplayable* element = (SkDisplayable*) getElement(id);
560 //should I go ahead and change all 'NULL's to 'NULL'?
563 const SkMemberInfo* field = getField(element, fieldID);
566 return setArray(element, field, array);
569 bool SkAnimator::setInt(SkDisplayable* element, const SkMemberInfo* info, int32_t s32) {
570 if (info->fType != SkType_MemberProperty) {
573 SkASSERT(info->getType() == SkType_Int);
574 info->setValue(element, &operand, 1);
576 SkScriptValue scriptValue;
577 scriptValue.fType = SkType_Int;
578 scriptValue.fOperand.fS32 = s32;
579 element->setProperty(info->propertyIndex(), scriptValue);
584 bool SkAnimator::setInt(const char* id, const char* fieldID, int32_t s32) {
585 SkDisplayable* element = (SkDisplayable*) getElement(id);
588 const SkMemberInfo* field = getField(element, fieldID);
591 return setInt(element, field, s32);
594 bool SkAnimator::setScalar(SkDisplayable* element, const SkMemberInfo* info, SkScalar scalar) {
595 if (info->fType != SkType_MemberProperty) {
597 operand.fScalar = scalar;
598 SkASSERT(info->getType() == SkType_Float);
599 info->setValue(element, &operand, 1);
601 SkScriptValue scriptValue;
602 scriptValue.fType = SkType_Float;
603 scriptValue.fOperand.fScalar = scalar;
604 element->setProperty(info->propertyIndex(), scriptValue);
609 bool SkAnimator::setScalar(const char* id, const char* fieldID, SkScalar scalar) {
610 SkDisplayable* element = (SkDisplayable*) getElement(id);
613 const SkMemberInfo* field = getField(element, fieldID);
616 return setScalar(element, field, scalar);
619 bool SkAnimator::setString(SkDisplayable* element,
620 const SkMemberInfo* info, const char* str) {
621 // !!! until this is fixed, can't call script with global references from here
622 info->setValue(*fMaker, NULL, 0, info->fCount, element, info->getType(), str, strlen(str));
626 bool SkAnimator::setString(const char* id, const char* fieldID, const char* str) {
627 SkDisplayable* element = (SkDisplayable*) getElement(id);
630 const SkMemberInfo* field = getField(element, fieldID);
633 return setString(element, field, str);
636 void SkAnimator::setTimeline(const Timeline& timeline) {
637 fMaker->fTimeline = &timeline;
640 void SkAnimator::setURIBase(const char* uri) {
643 const char* tail = strrchr(uri, '/');
645 SkString prefix(uri, tail - uri + 1);
646 if (uri[0] != '.' /*SkStream::IsAbsoluteURI(uri)*/)
647 fMaker->fPrefix.reset();
648 fMaker->fPrefix.append(prefix);
649 fMaker->fFileName.set(tail + 1);
651 fMaker->fFileName.set(uri);
656 bool SkAnimator::NoLeaks() {
657 #ifdef SK_BUILD_FOR_MAC
658 if (SkDisplayable::fAllocations.count() == 0)
660 // return SkDisplayable::fAllocationCount == 0;
661 SkDebugf("!!! leaked %d displayables:\n", SkDisplayable::fAllocations.count());
662 for (SkDisplayable** leak = SkDisplayable::fAllocations.begin(); leak < SkDisplayable::fAllocations.end(); leak++)
663 SkDebugf("%08x %s\n", *leak, (*leak)->id);
669 #ifdef SK_SUPPORT_UNITTEST
670 #include "SkAnimatorScript.h"
671 #include "SkBase64.h"
673 #include "SkMemberInfo.h"
675 #define unittestline(type) { #type , type::UnitTest }
679 #ifdef SK_SUPPORT_UNITTEST
680 void SkAnimator::Init(bool runUnitTests) {
681 if (runUnitTests == false)
683 static const struct {
684 const char* fTypeName;
685 void (*fUnitTest)( );
687 unittestline(SkBase64),
688 unittestline(SkDisplayType),
689 unittestline(SkParse),
690 unittestline(SkScriptEngine),
691 // unittestline(SkScriptEngine2), // compiled script experiment
692 unittestline(SkAnimatorScript)
694 for (int i = 0; i < (int)SK_ARRAY_COUNT(gUnitTests); i++)
696 SkDebugf("SkAnimator: Running UnitTest for %s\n", gUnitTests[i].fTypeName);
697 gUnitTests[i].fUnitTest();
698 SkDebugf("SkAnimator: End UnitTest for %s\n", gUnitTests[i].fTypeName);
702 void SkAnimator::Init(bool) {}
705 void SkAnimator::Term() {