OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkAnimatorScript.h" | 10 #include "SkAnimatorScript.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 { SkType_TextBoxAlign, "start|center|end" }, | 38 { SkType_TextBoxAlign, "start|center|end" }, |
39 { SkType_TextBoxMode, "oneLine|lineBreak" }, | 39 { SkType_TextBoxMode, "oneLine|lineBreak" }, |
40 { SkType_TileMode, "clamp|repeat|mirror" }, | 40 { SkType_TileMode, "clamp|repeat|mirror" }, |
41 { SkType_Xfermode, "clear|src|dst|srcOver|dstOver|srcIn|dstIn|srcOut|dstOut|
" | 41 { SkType_Xfermode, "clear|src|dst|srcOver|dstOver|srcIn|dstIn|srcOut|dstOut|
" |
42 "srcATop|dstATop|xor|darken|lighten" }, | 42 "srcATop|dstATop|xor|darken|lighten" }, |
43 }; | 43 }; |
44 | 44 |
45 static int gEnumMapCount = SK_ARRAY_COUNT(gEnumMaps); | 45 static int gEnumMapCount = SK_ARRAY_COUNT(gEnumMaps); |
46 | 46 |
47 SkAnimatorScript::SkAnimatorScript(SkAnimateMaker& maker, SkDisplayable* working
, SkDisplayTypes type) | 47 SkAnimatorScript::SkAnimatorScript(SkAnimateMaker& maker, SkDisplayable* working
, SkDisplayTypes type) |
48 : SkScriptEngine(SkScriptEngine::ToOpType(type)), fMaker(maker), fParent(NUL
L), fWorking(working) | 48 : SkScriptEngine(SkScriptEngine::ToOpType(type)), fMaker(maker), fParent(nul
lptr), fWorking(working) |
49 { | 49 { |
50 memberCallBack(EvalMember, (void*) this); | 50 memberCallBack(EvalMember, (void*) this); |
51 memberFunctionCallBack(EvalMemberFunction, (void*) this); | 51 memberFunctionCallBack(EvalMemberFunction, (void*) this); |
52 boxCallBack(Box, (void*) this); | 52 boxCallBack(Box, (void*) this); |
53 unboxCallBack(Unbox, (void*) &maker); | 53 unboxCallBack(Unbox, (void*) &maker); |
54 propertyCallBack(EvalID, (void*) this); // must be first (entries are prepen
ded, will be last), since it never fails | 54 propertyCallBack(EvalID, (void*) this); // must be first (entries are prepen
ded, will be last), since it never fails |
55 propertyCallBack(Infinity, (void*) this); | 55 propertyCallBack(Infinity, (void*) this); |
56 propertyCallBack(NaN, (void*) this); | 56 propertyCallBack(NaN, (void*) this); |
57 functionCallBack(Eval, (void*) this); | 57 functionCallBack(Eval, (void*) this); |
58 functionCallBack(IsFinite, (void*) this); | 58 functionCallBack(IsFinite, (void*) this); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 | 158 |
159 bool SkAnimatorScript::EvalID(const char* token, size_t len, void* user, SkScrip
tValue* value) { | 159 bool SkAnimatorScript::EvalID(const char* token, size_t len, void* user, SkScrip
tValue* value) { |
160 SkAnimatorScript* engine = (SkAnimatorScript*) user; | 160 SkAnimatorScript* engine = (SkAnimatorScript*) user; |
161 SkTDict<SkDisplayable*>* ids = &engine->fMaker.fIDs; | 161 SkTDict<SkDisplayable*>* ids = &engine->fMaker.fIDs; |
162 SkDisplayable* displayable; | 162 SkDisplayable* displayable; |
163 bool success = ids->find(token, len, &displayable); | 163 bool success = ids->find(token, len, &displayable); |
164 if (success == false) { | 164 if (success == false) { |
165 displayable = engine->fWorking; | 165 displayable = engine->fWorking; |
166 if (SK_LITERAL_STR_EQUAL("parent", token, len)) { | 166 if (SK_LITERAL_STR_EQUAL("parent", token, len)) { |
167 SkDisplayable* parent = displayable->getParent(); | 167 SkDisplayable* parent = displayable->getParent(); |
168 if (parent == NULL) | 168 if (parent == nullptr) |
169 parent = engine->fParent; | 169 parent = engine->fParent; |
170 if (parent) { | 170 if (parent) { |
171 value->fOperand.fDisplayable = parent; | 171 value->fOperand.fDisplayable = parent; |
172 value->fType = SkType_Displayable; | 172 value->fType = SkType_Displayable; |
173 return true; | 173 return true; |
174 } | 174 } |
175 } | 175 } |
176 if (displayable && EvalMember(token, len, displayable, engine, value)) | 176 if (displayable && EvalMember(token, len, displayable, engine, value)) |
177 return true; | 177 return true; |
178 value->fOperand.fString = NULL; | 178 value->fOperand.fString = nullptr; |
179 value->fType = SkType_String; | 179 value->fType = SkType_String; |
180 } else { | 180 } else { |
181 SkDisplayable* working = engine->fWorking; | 181 SkDisplayable* working = engine->fWorking; |
182 value->fOperand.fDisplayable = displayable; | 182 value->fOperand.fDisplayable = displayable; |
183 value->fType = SkType_Displayable; | 183 value->fType = SkType_Displayable; |
184 if (displayable->canContainDependents() && working && working->isAnimate
()) { | 184 if (displayable->canContainDependents() && working && working->isAnimate
()) { |
185 SkAnimateBase* animator = (SkAnimateBase*) working; | 185 SkAnimateBase* animator = (SkAnimateBase*) working; |
186 if (animator->isDynamic()) { | 186 if (animator->isDynamic()) { |
187 SkDisplayDepend* depend = (SkDisplayDepend* ) displayable; | 187 SkDisplayDepend* depend = (SkDisplayDepend* ) displayable; |
188 depend->addDependent(working); | 188 depend->addDependent(working); |
189 } | 189 } |
190 } | 190 } |
191 } | 191 } |
192 return true; | 192 return true; |
193 } | 193 } |
194 | 194 |
195 bool SkAnimatorScript::EvalNamedColor(const char* token, size_t len, void* callb
ack, SkScriptValue* value) { | 195 bool SkAnimatorScript::EvalNamedColor(const char* token, size_t len, void* callb
ack, SkScriptValue* value) { |
196 value->fType = SkType_Int; | 196 value->fType = SkType_Int; |
197 if (SkParse::FindNamedColor(token, len, (SkColor*) &value->fOperand.fS32) !=
NULL) | 197 if (SkParse::FindNamedColor(token, len, (SkColor*) &value->fOperand.fS32) !=
nullptr) |
198 return true; | 198 return true; |
199 return false; | 199 return false; |
200 } | 200 } |
201 | 201 |
202 bool SkAnimatorScript::EvalRGB(const char* function, size_t len, SkTDArray<SkScr
iptValue>& params, | 202 bool SkAnimatorScript::EvalRGB(const char* function, size_t len, SkTDArray<SkScr
iptValue>& params, |
203 void* eng, SkScriptValue* value) { | 203 void* eng, SkScriptValue* value) { |
204 if (SK_LITERAL_STR_EQUAL("rgb", function, len) == false) | 204 if (SK_LITERAL_STR_EQUAL("rgb", function, len) == false) |
205 return false; | 205 return false; |
206 if (params.count() != 3) | 206 if (params.count() != 3) |
207 return false; | 207 return false; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 SkScriptEngine* engine = (SkScriptEngine*) eng; | 274 SkScriptEngine* engine = (SkScriptEngine*) eng; |
275 SkDisplayable* displayable = (SkDisplayable*) object; | 275 SkDisplayable* displayable = (SkDisplayable*) object; |
276 SkString name(member, len); | 276 SkString name(member, len); |
277 SkDisplayable* named = displayable->contains(name); | 277 SkDisplayable* named = displayable->contains(name); |
278 if (named) { | 278 if (named) { |
279 value->fOperand.fDisplayable = named; | 279 value->fOperand.fDisplayable = named; |
280 value->fType = SkType_Displayable; | 280 value->fType = SkType_Displayable; |
281 return true; | 281 return true; |
282 } | 282 } |
283 const SkMemberInfo* info = displayable->getMember(name.c_str()); | 283 const SkMemberInfo* info = displayable->getMember(name.c_str()); |
284 if (info == NULL) | 284 if (info == nullptr) |
285 return false; | 285 return false; |
286 if (info->fType == SkType_MemberProperty) { | 286 if (info->fType == SkType_MemberProperty) { |
287 if (displayable->getProperty(info->propertyIndex(), value) == false) { | 287 if (displayable->getProperty(info->propertyIndex(), value) == false) { |
288 SkASSERT(0); | 288 SkASSERT(0); |
289 return false; | 289 return false; |
290 } | 290 } |
291 } | 291 } |
292 return EvalMemberCommon(engine, info, displayable, value); | 292 return EvalMemberCommon(engine, info, displayable, value); |
293 } | 293 } |
294 | 294 |
295 bool SkAnimatorScript::EvalMemberFunction(const char* member, size_t len, void*
object, | 295 bool SkAnimatorScript::EvalMemberFunction(const char* member, size_t len, void*
object, |
296 SkTDArray<SkScriptValue>& params, void* eng, SkScriptValue* value) { | 296 SkTDArray<SkScriptValue>& params, void* eng, SkScriptValue* value) { |
297 SkScriptEngine* engine = (SkScriptEngine*) eng; | 297 SkScriptEngine* engine = (SkScriptEngine*) eng; |
298 SkDisplayable* displayable = (SkDisplayable*) object; | 298 SkDisplayable* displayable = (SkDisplayable*) object; |
299 SkString name(member, len); | 299 SkString name(member, len); |
300 const SkMemberInfo* info = displayable->getMember(name.c_str()); | 300 const SkMemberInfo* info = displayable->getMember(name.c_str()); |
301 SkASSERT(info != NULL); /* !!! error handling unimplemented */ | 301 SkASSERT(info != nullptr); /* !!! error handling unimplemented */ |
302 if (info->fType != SkType_MemberFunction) { | 302 if (info->fType != SkType_MemberFunction) { |
303 SkASSERT(0); | 303 SkASSERT(0); |
304 return false; | 304 return false; |
305 } | 305 } |
306 displayable->executeFunction(displayable, info->functionIndex(), params, inf
o->getType(), | 306 displayable->executeFunction(displayable, info->functionIndex(), params, inf
o->getType(), |
307 value); | 307 value); |
308 return EvalMemberCommon(engine, info, displayable, value); | 308 return EvalMemberCommon(engine, info, displayable, value); |
309 } | 309 } |
310 | 310 |
311 bool SkAnimatorScript::EvaluateDisplayable(SkAnimateMaker& maker, SkDisplayable*
displayable, const char* script, SkDisplayable** result) { | 311 bool SkAnimatorScript::EvaluateDisplayable(SkAnimateMaker& maker, SkDisplayable*
displayable, const char* script, SkDisplayable** result) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
394 value->fType = SkType_Int; | 394 value->fType = SkType_Int; |
395 value->fOperand.fS32 = scriptValue->fType == SkType_Float ? SkScalarIsNaN(sc
riptValue->fOperand.fScalar) : 0; | 395 value->fOperand.fS32 = scriptValue->fType == SkType_Float ? SkScalarIsNaN(sc
riptValue->fOperand.fScalar) : 0; |
396 return true; | 396 return true; |
397 } | 397 } |
398 | 398 |
399 bool SkAnimatorScript::MapEnums(const char* ptr, const char* match, size_t len,
int* value) { | 399 bool SkAnimatorScript::MapEnums(const char* ptr, const char* match, size_t len,
int* value) { |
400 int index = 0; | 400 int index = 0; |
401 bool more = true; | 401 bool more = true; |
402 do { | 402 do { |
403 const char* last = strchr(ptr, '|'); | 403 const char* last = strchr(ptr, '|'); |
404 if (last == NULL) { | 404 if (last == nullptr) { |
405 last = &ptr[strlen(ptr)]; | 405 last = &ptr[strlen(ptr)]; |
406 more = false; | 406 more = false; |
407 } | 407 } |
408 size_t length = last - ptr; | 408 size_t length = last - ptr; |
409 if (len == length && strncmp(ptr, match, length) == 0) { | 409 if (len == length && strncmp(ptr, match, length) == 0) { |
410 *value = index; | 410 *value = index; |
411 return true; | 411 return true; |
412 } | 412 } |
413 index++; | 413 index++; |
414 ptr = last + 1; | 414 ptr = last + 1; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 } break; | 458 } break; |
459 case SkType_Float: { | 459 case SkType_Float: { |
460 SkDisplayFloat* boxedValue = (SkDisplayFloat*) displayable; | 460 SkDisplayFloat* boxedValue = (SkDisplayFloat*) displayable; |
461 scriptValue->fOperand.fScalar = boxedValue->value; | 461 scriptValue->fOperand.fScalar = boxedValue->value; |
462 } break; | 462 } break; |
463 case SkType_String: { | 463 case SkType_String: { |
464 SkDisplayString* boxedValue = (SkDisplayString*) displayable; | 464 SkDisplayString* boxedValue = (SkDisplayString*) displayable; |
465 scriptValue->fOperand.fString = new SkString(boxedValue->value); | 465 scriptValue->fOperand.fString = new SkString(boxedValue->value); |
466 } break; | 466 } break; |
467 default: { | 467 default: { |
468 const char* id = NULL; | 468 const char* id = nullptr; |
469 SkDEBUGCODE(bool success = ) maker->findKey(displayable, &id); | 469 SkDEBUGCODE(bool success = ) maker->findKey(displayable, &id); |
470 SkASSERT(success); | 470 SkASSERT(success); |
471 scriptValue->fOperand.fString = new SkString(id); | 471 scriptValue->fOperand.fString = new SkString(id); |
472 type = SkType_String; | 472 type = SkType_String; |
473 } | 473 } |
474 } | 474 } |
475 scriptValue->fType = type; | 475 scriptValue->fType = type; |
476 return true; | 476 return true; |
477 } | 477 } |
478 | 478 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
555 void SkAnimatorScript::UnitTest() { | 555 void SkAnimatorScript::UnitTest() { |
556 #if defined(SK_SUPPORT_UNITTEST) | 556 #if defined(SK_SUPPORT_UNITTEST) |
557 SkAnimator animator; | 557 SkAnimator animator; |
558 SkASSERT(animator.decodeMemory(scriptTestSetup, sizeof(scriptTestSetup)-1)); | 558 SkASSERT(animator.decodeMemory(scriptTestSetup, sizeof(scriptTestSetup)-1)); |
559 SkEvent evt; | 559 SkEvent evt; |
560 evt.setString("id", "evt"); | 560 evt.setString("id", "evt"); |
561 evt.setS32("x", 3); | 561 evt.setS32("x", 3); |
562 animator.doUserEvent(evt); | 562 animator.doUserEvent(evt); |
563 // set up animator with memory script above, then run value tests | 563 // set up animator with memory script above, then run value tests |
564 for (unsigned index = 0; index < SkScriptNAnswer_testCount; index++) { | 564 for (unsigned index = 0; index < SkScriptNAnswer_testCount; index++) { |
565 SkAnimatorScript engine(*animator.fMaker, NULL, scriptTests[index].fType
); | 565 SkAnimatorScript engine(*animator.fMaker, nullptr, scriptTests[index].fT
ype); |
566 SkScriptValue value; | 566 SkScriptValue value; |
567 const char* script = scriptTests[index].fScript; | 567 const char* script = scriptTests[index].fScript; |
568 bool success = engine.evaluateScript(&script, &value); | 568 bool success = engine.evaluateScript(&script, &value); |
569 if (success == false) { | 569 if (success == false) { |
570 SkDebugf("script failed: %s\n", scriptTests[index].fScript); | 570 SkDebugf("script failed: %s\n", scriptTests[index].fScript); |
571 SkASSERT(scriptTests[index].fType == SkType_Unknown); | 571 SkASSERT(scriptTests[index].fType == SkType_Unknown); |
572 continue; | 572 continue; |
573 } | 573 } |
574 SkASSERT(value.fType == scriptTests[index].fType); | 574 SkASSERT(value.fType == scriptTests[index].fType); |
575 SkScalar error; | 575 SkScalar error; |
576 switch (value.fType) { | 576 switch (value.fType) { |
577 case SkType_Int: | 577 case SkType_Int: |
578 SkASSERT(value.fOperand.fS32 == scriptTests[index].fIntAnswer); | 578 SkASSERT(value.fOperand.fS32 == scriptTests[index].fIntAnswer); |
579 break; | 579 break; |
580 case SkType_Float: | 580 case SkType_Float: |
581 error = SkScalarAbs(value.fOperand.fScalar - scriptTests[index].
fScalarAnswer); | 581 error = SkScalarAbs(value.fOperand.fScalar - scriptTests[index].
fScalarAnswer); |
582 SkASSERT(error < SK_Scalar1 / 10000); | 582 SkASSERT(error < SK_Scalar1 / 10000); |
583 break; | 583 break; |
584 case SkType_String: | 584 case SkType_String: |
585 SkASSERT(strcmp(value.fOperand.fString->c_str(), scriptTests[ind
ex].fStringAnswer) == 0); | 585 SkASSERT(strcmp(value.fOperand.fString->c_str(), scriptTests[ind
ex].fStringAnswer) == 0); |
586 break; | 586 break; |
587 default: | 587 default: |
588 SkASSERT(0); | 588 SkASSERT(0); |
589 } | 589 } |
590 } | 590 } |
591 #endif | 591 #endif |
592 } | 592 } |
593 | 593 |
594 #endif | 594 #endif |
OLD | NEW |