gfx/skia/trunk/src/animator/SkAnimatorScript.cpp

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

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 "SkAnimatorScript.h"
michael@0 11 #include "SkAnimateBase.h"
michael@0 12 #include "SkAnimateMaker.h"
michael@0 13 #include "SkDisplayTypes.h"
michael@0 14 #include "SkExtras.h"
michael@0 15 #include "SkMemberInfo.h"
michael@0 16 #include "SkParse.h"
michael@0 17
michael@0 18 static const SkDisplayEnumMap gEnumMaps[] = {
michael@0 19 { SkType_AddMode, "indirect|immediate" },
michael@0 20 { SkType_Align, "left|center|right" },
michael@0 21 { SkType_ApplyMode, "create|immediate|once" },
michael@0 22 { SkType_ApplyTransition, "normal|reverse" },
michael@0 23 { SkType_BitmapEncoding, "jpeg|png" },
michael@0 24 { SkType_BitmapFormat, "none|A1|A8|Index8|RGB16|RGB32" },
michael@0 25 { SkType_Boolean, "false|true" },
michael@0 26 { SkType_Cap, "butt|round|square" },
michael@0 27 { SkType_EventCode, "none|leftSoftKey|rightSoftKey|home|back|send|end|key0|key1|key2|key3|key4|key5|key6|key7|key8|key9|star|hash|up|down|left|right|OK|volUp|volDown|camera" },
michael@0 28 { SkType_EventKind, "none|keyChar|keyPress|keyPressUp|mouseDown|mouseDrag|mouseMove|mouseUp|onEnd|onLoad|user" },
michael@0 29 { SkType_EventMode, "deferred|immediate" },
michael@0 30 { SkType_FillType, "winding|evenOdd" },
michael@0 31 { SkType_FilterType, "none|bilinear" },
michael@0 32 { SkType_FontStyle, "normal|bold|italic|boldItalic" },
michael@0 33 { SkType_FromPathMode, "normal|angle|position" },
michael@0 34 { SkType_Join, "miter|round|blunt" },
michael@0 35 { SkType_MaskFilterBlurStyle, "normal|solid|outer|inner" },
michael@0 36 { SkType_PathDirection, "cw|ccw" },
michael@0 37 { SkType_Style, "fill|stroke|strokeAndFill" },
michael@0 38 { SkType_TextBoxAlign, "start|center|end" },
michael@0 39 { SkType_TextBoxMode, "oneLine|lineBreak" },
michael@0 40 { SkType_TileMode, "clamp|repeat|mirror" },
michael@0 41 { SkType_Xfermode, "clear|src|dst|srcOver|dstOver|srcIn|dstIn|srcOut|dstOut|"
michael@0 42 "srcATop|dstATop|xor|darken|lighten" },
michael@0 43 };
michael@0 44
michael@0 45 static int gEnumMapCount = SK_ARRAY_COUNT(gEnumMaps);
michael@0 46
michael@0 47 SkAnimatorScript::SkAnimatorScript(SkAnimateMaker& maker, SkDisplayable* working, SkDisplayTypes type)
michael@0 48 : SkScriptEngine(SkScriptEngine::ToOpType(type)), fMaker(maker), fParent(NULL), fWorking(working)
michael@0 49 {
michael@0 50 memberCallBack(EvalMember, (void*) this);
michael@0 51 memberFunctionCallBack(EvalMemberFunction, (void*) this);
michael@0 52 boxCallBack(Box, (void*) this);
michael@0 53 unboxCallBack(Unbox, (void*) &maker);
michael@0 54 propertyCallBack(EvalID, (void*) this); // must be first (entries are prepended, will be last), since it never fails
michael@0 55 propertyCallBack(Infinity, (void*) this);
michael@0 56 propertyCallBack(NaN, (void*) this);
michael@0 57 functionCallBack(Eval, (void*) this);
michael@0 58 functionCallBack(IsFinite, (void*) this);
michael@0 59 functionCallBack(IsNaN, (void*) this);
michael@0 60 if (type == SkType_ARGB) {
michael@0 61 functionCallBack(EvalRGB, (void*) this);
michael@0 62 propertyCallBack(EvalNamedColor, (void*) &maker.fIDs);
michael@0 63 }
michael@0 64 if (SkDisplayType::IsEnum(&maker, type)) {
michael@0 65 // !!! for SpiderMonkey, iterate through the enum values, and map them to globals
michael@0 66 const SkDisplayEnumMap& map = GetEnumValues(type);
michael@0 67 propertyCallBack(EvalEnum, (void*) map.fValues);
michael@0 68 }
michael@0 69 for (SkExtras** extraPtr = maker.fExtras.begin(); extraPtr < maker.fExtras.end(); extraPtr++) {
michael@0 70 SkExtras* extra = *extraPtr;
michael@0 71 if (extra->fExtraCallBack)
michael@0 72 propertyCallBack(extra->fExtraCallBack, extra->fExtraStorage);
michael@0 73 }
michael@0 74 }
michael@0 75
michael@0 76 SkAnimatorScript::~SkAnimatorScript() {
michael@0 77 for (SkDisplayable** dispPtr = fTrackDisplayable.begin(); dispPtr < fTrackDisplayable.end(); dispPtr++)
michael@0 78 delete *dispPtr;
michael@0 79 }
michael@0 80
michael@0 81 bool SkAnimatorScript::evaluate(const char* original, SkScriptValue* result, SkDisplayTypes type) {
michael@0 82 const char* script = original;
michael@0 83 bool success = evaluateScript(&script, result);
michael@0 84 if (success == false || result->fType != type) {
michael@0 85 fMaker.setScriptError(*this);
michael@0 86 return false;
michael@0 87 }
michael@0 88 return true;
michael@0 89 }
michael@0 90
michael@0 91 bool SkAnimatorScript::Box(void* user, SkScriptValue* scriptValue) {
michael@0 92 SkAnimatorScript* engine = (SkAnimatorScript*) user;
michael@0 93 SkDisplayTypes type = scriptValue->fType;
michael@0 94 SkDisplayable* displayable;
michael@0 95 switch (type) {
michael@0 96 case SkType_Array: {
michael@0 97 SkDisplayArray* boxedValue = new SkDisplayArray(*scriptValue->fOperand.fArray);
michael@0 98 displayable = boxedValue;
michael@0 99 } break;
michael@0 100 case SkType_Boolean: {
michael@0 101 SkDisplayBoolean* boxedValue = new SkDisplayBoolean;
michael@0 102 displayable = boxedValue;
michael@0 103 boxedValue->value = !! scriptValue->fOperand.fS32;
michael@0 104 } break;
michael@0 105 case SkType_Int: {
michael@0 106 SkDisplayInt* boxedValue = new SkDisplayInt;
michael@0 107 displayable = boxedValue;
michael@0 108 boxedValue->value = scriptValue->fOperand.fS32;
michael@0 109 } break;
michael@0 110 case SkType_Float: {
michael@0 111 SkDisplayFloat* boxedValue = new SkDisplayFloat;
michael@0 112 displayable = boxedValue;
michael@0 113 boxedValue->value = scriptValue->fOperand.fScalar;
michael@0 114 } break;
michael@0 115 case SkType_String: {
michael@0 116 SkDisplayString* boxedValue = new SkDisplayString(*scriptValue->fOperand.fString);
michael@0 117 displayable = boxedValue;
michael@0 118 } break;
michael@0 119 case SkType_Displayable:
michael@0 120 scriptValue->fOperand.fObject = scriptValue->fOperand.fDisplayable;
michael@0 121 scriptValue->fType = SkType_Displayable;
michael@0 122 return true;
michael@0 123 default:
michael@0 124 SkASSERT(0);
michael@0 125 return false;
michael@0 126 }
michael@0 127 engine->track(displayable);
michael@0 128 scriptValue->fOperand.fObject = displayable;
michael@0 129 scriptValue->fType = SkType_Displayable;
michael@0 130 return true;
michael@0 131 }
michael@0 132
michael@0 133 bool SkAnimatorScript::Eval(const char* function, size_t len, SkTDArray<SkScriptValue>& params,
michael@0 134 void* eng, SkScriptValue* value) {
michael@0 135 if (SK_LITERAL_STR_EQUAL("eval", function, len) == false)
michael@0 136 return false;
michael@0 137 if (params.count() != 1)
michael@0 138 return false;
michael@0 139 SkAnimatorScript* host = (SkAnimatorScript*) eng;
michael@0 140 SkAnimatorScript engine(host->fMaker, host->fWorking, SkScriptEngine::ToDisplayType(host->fReturnType));
michael@0 141 SkScriptValue* scriptValue = params.begin();
michael@0 142 bool success = true;
michael@0 143 if (scriptValue->fType == SkType_String) {
michael@0 144 const char* script = scriptValue->fOperand.fString->c_str();
michael@0 145 success = engine.evaluateScript(&script, value);
michael@0 146 } else
michael@0 147 *value = *scriptValue;
michael@0 148 return success;
michael@0 149 }
michael@0 150
michael@0 151 bool SkAnimatorScript::EvalEnum(const char* token, size_t len, void* callBack, SkScriptValue* value) {
michael@0 152 const char* tokens = (const char*) callBack;
michael@0 153 value->fType = SkType_Int;
michael@0 154 if (MapEnums(tokens, token, len, (int*)&value->fOperand.fS32))
michael@0 155 return true;
michael@0 156 return false;
michael@0 157 }
michael@0 158
michael@0 159 bool SkAnimatorScript::EvalID(const char* token, size_t len, void* user, SkScriptValue* value) {
michael@0 160 SkAnimatorScript* engine = (SkAnimatorScript*) user;
michael@0 161 SkTDict<SkDisplayable*>* ids = &engine->fMaker.fIDs;
michael@0 162 SkDisplayable* displayable;
michael@0 163 bool success = ids->find(token, len, &displayable);
michael@0 164 if (success == false) {
michael@0 165 displayable = engine->fWorking;
michael@0 166 if (SK_LITERAL_STR_EQUAL("parent", token, len)) {
michael@0 167 SkDisplayable* parent = displayable->getParent();
michael@0 168 if (parent == NULL)
michael@0 169 parent = engine->fParent;
michael@0 170 if (parent) {
michael@0 171 value->fOperand.fDisplayable = parent;
michael@0 172 value->fType = SkType_Displayable;
michael@0 173 return true;
michael@0 174 }
michael@0 175 }
michael@0 176 if (displayable && EvalMember(token, len, displayable, engine, value))
michael@0 177 return true;
michael@0 178 value->fOperand.fString = NULL;
michael@0 179 value->fType = SkType_String;
michael@0 180 } else {
michael@0 181 SkDisplayable* working = engine->fWorking;
michael@0 182 value->fOperand.fDisplayable = displayable;
michael@0 183 value->fType = SkType_Displayable;
michael@0 184 if (displayable->canContainDependents() && working && working->isAnimate()) {
michael@0 185 SkAnimateBase* animator = (SkAnimateBase*) working;
michael@0 186 if (animator->isDynamic()) {
michael@0 187 SkDisplayDepend* depend = (SkDisplayDepend* ) displayable;
michael@0 188 depend->addDependent(working);
michael@0 189 }
michael@0 190 }
michael@0 191 }
michael@0 192 return true;
michael@0 193 }
michael@0 194
michael@0 195 bool SkAnimatorScript::EvalNamedColor(const char* token, size_t len, void* callback, SkScriptValue* value) {
michael@0 196 value->fType = SkType_Int;
michael@0 197 if (SkParse::FindNamedColor(token, len, (SkColor*) &value->fOperand.fS32) != NULL)
michael@0 198 return true;
michael@0 199 return false;
michael@0 200 }
michael@0 201
michael@0 202 bool SkAnimatorScript::EvalRGB(const char* function, size_t len, SkTDArray<SkScriptValue>& params,
michael@0 203 void* eng, SkScriptValue* value) {
michael@0 204 if (SK_LITERAL_STR_EQUAL("rgb", function, len) == false)
michael@0 205 return false;
michael@0 206 if (params.count() != 3)
michael@0 207 return false;
michael@0 208 SkScriptEngine* engine = (SkScriptEngine*) eng;
michael@0 209 unsigned result = 0xFF000000;
michael@0 210 int shift = 16;
michael@0 211 for (SkScriptValue* valuePtr = params.begin(); valuePtr < params.end(); valuePtr++) {
michael@0 212 engine->convertTo(SkType_Int, valuePtr);
michael@0 213 result |= SkClampMax(valuePtr->fOperand.fS32, 255) << shift;
michael@0 214 shift -= 8;
michael@0 215 }
michael@0 216 value->fOperand.fS32 = result;
michael@0 217 value->fType = SkType_Int;
michael@0 218 return true;
michael@0 219 }
michael@0 220
michael@0 221 bool SkAnimatorScript::EvalMemberCommon(SkScriptEngine* engine, const SkMemberInfo* info,
michael@0 222 SkDisplayable* displayable, SkScriptValue* value) {
michael@0 223 SkDisplayTypes original;
michael@0 224 SkDisplayTypes type = original = (SkDisplayTypes) info->getType();
michael@0 225 if (info->fType == SkType_Array)
michael@0 226 type = SkType_Array;
michael@0 227 switch (type) {
michael@0 228 case SkType_ARGB:
michael@0 229 type = SkType_Int;
michael@0 230 case SkType_Boolean:
michael@0 231 case SkType_Int:
michael@0 232 case SkType_MSec:
michael@0 233 case SkType_Float:
michael@0 234 SkASSERT(info->getCount() == 1);
michael@0 235 if (info->fType != SkType_MemberProperty && info->fType != SkType_MemberFunction)
michael@0 236 value->fOperand.fS32 = *(int32_t*) info->memberData(displayable); // OK for SkScalar too
michael@0 237 if (type == SkType_MSec) {
michael@0 238 value->fOperand.fScalar = SkScalarDiv((SkScalar) value->fOperand.fS32, 1000); // dividing two ints is the same as dividing two scalars
michael@0 239 type = SkType_Float;
michael@0 240 }
michael@0 241 break;
michael@0 242 case SkType_String: {
michael@0 243 SkString* displayableString;
michael@0 244 if (info->fType != SkType_MemberProperty && info->fType != SkType_MemberFunction) {
michael@0 245 info->getString(displayable, &displayableString);
michael@0 246 value->fOperand.fString = new SkString(*displayableString);
michael@0 247 }
michael@0 248 } break;
michael@0 249 case SkType_Array: {
michael@0 250 SkASSERT(info->fType != SkType_MemberProperty); // !!! incomplete
michael@0 251 SkTDOperandArray* displayableArray = (SkTDOperandArray*) info->memberData(displayable);
michael@0 252 if (displayable->getType() == SkType_Array) {
michael@0 253 SkDisplayArray* typedArray = (SkDisplayArray*) displayable;
michael@0 254 original = typedArray->values.getType();
michael@0 255 }
michael@0 256 SkASSERT(original != SkType_Unknown);
michael@0 257 SkTypedArray* array = value->fOperand.fArray = new SkTypedArray(original);
michael@0 258 engine->track(array);
michael@0 259 int count = displayableArray->count();
michael@0 260 if (count > 0) {
michael@0 261 array->setCount(count);
michael@0 262 memcpy(array->begin(), displayableArray->begin(), count * sizeof(SkOperand));
michael@0 263 }
michael@0 264 } break;
michael@0 265 default:
michael@0 266 SkASSERT(0); // unimplemented
michael@0 267 }
michael@0 268 value->fType = type;
michael@0 269 return true;
michael@0 270 }
michael@0 271
michael@0 272 bool SkAnimatorScript::EvalMember(const char* member, size_t len, void* object, void* eng,
michael@0 273 SkScriptValue* value) {
michael@0 274 SkScriptEngine* engine = (SkScriptEngine*) eng;
michael@0 275 SkDisplayable* displayable = (SkDisplayable*) object;
michael@0 276 SkString name(member, len);
michael@0 277 SkDisplayable* named = displayable->contains(name);
michael@0 278 if (named) {
michael@0 279 value->fOperand.fDisplayable = named;
michael@0 280 value->fType = SkType_Displayable;
michael@0 281 return true;
michael@0 282 }
michael@0 283 const SkMemberInfo* info = displayable->getMember(name.c_str());
michael@0 284 if (info == NULL)
michael@0 285 return false;
michael@0 286 if (info->fType == SkType_MemberProperty) {
michael@0 287 if (displayable->getProperty(info->propertyIndex(), value) == false) {
michael@0 288 SkASSERT(0);
michael@0 289 return false;
michael@0 290 }
michael@0 291 }
michael@0 292 return EvalMemberCommon(engine, info, displayable, value);
michael@0 293 }
michael@0 294
michael@0 295 bool SkAnimatorScript::EvalMemberFunction(const char* member, size_t len, void* object,
michael@0 296 SkTDArray<SkScriptValue>& params, void* eng, SkScriptValue* value) {
michael@0 297 SkScriptEngine* engine = (SkScriptEngine*) eng;
michael@0 298 SkDisplayable* displayable = (SkDisplayable*) object;
michael@0 299 SkString name(member, len);
michael@0 300 const SkMemberInfo* info = displayable->getMember(name.c_str());
michael@0 301 SkASSERT(info != NULL); /* !!! error handling unimplemented */
michael@0 302 if (info->fType != SkType_MemberFunction) {
michael@0 303 SkASSERT(0);
michael@0 304 return false;
michael@0 305 }
michael@0 306 displayable->executeFunction(displayable, info->functionIndex(), params, info->getType(),
michael@0 307 value);
michael@0 308 return EvalMemberCommon(engine, info, displayable, value);
michael@0 309 }
michael@0 310
michael@0 311 bool SkAnimatorScript::EvaluateDisplayable(SkAnimateMaker& maker, SkDisplayable* displayable, const char* script, SkDisplayable** result) {
michael@0 312 SkAnimatorScript engine(maker, displayable, SkType_Displayable);
michael@0 313 SkScriptValue value;
michael@0 314 bool success = engine.evaluate(script, &value, SkType_Displayable);
michael@0 315 if (success)
michael@0 316 *result = value.fOperand.fDisplayable;
michael@0 317 return success;
michael@0 318 }
michael@0 319
michael@0 320 bool SkAnimatorScript::EvaluateInt(SkAnimateMaker& maker, SkDisplayable* displayable, const char* script, int32_t* result) {
michael@0 321 SkAnimatorScript engine(maker, displayable, SkType_Int);
michael@0 322 SkScriptValue value;
michael@0 323 bool success = engine.evaluate(script, &value, SkType_Int);
michael@0 324 if (success)
michael@0 325 *result = value.fOperand.fS32;
michael@0 326 return success;
michael@0 327 }
michael@0 328
michael@0 329 bool SkAnimatorScript::EvaluateFloat(SkAnimateMaker& maker, SkDisplayable* displayable, const char* script, SkScalar* result) {
michael@0 330 SkAnimatorScript engine(maker, displayable, SkType_Float);
michael@0 331 SkScriptValue value;
michael@0 332 bool success = engine.evaluate(script, &value, SkType_Float);
michael@0 333 if (success)
michael@0 334 *result = value.fOperand.fScalar;
michael@0 335 return success;
michael@0 336 }
michael@0 337
michael@0 338 bool SkAnimatorScript::EvaluateString(SkAnimateMaker& maker, SkDisplayable* displayable, const char* script, SkString* result) {
michael@0 339 SkAnimatorScript engine(maker, displayable, SkType_String);
michael@0 340 SkScriptValue value;
michael@0 341 bool success = engine.evaluate(script, &value, SkType_String);
michael@0 342 if (success)
michael@0 343 result->set(*(value.fOperand.fString));
michael@0 344 return success;
michael@0 345 }
michael@0 346
michael@0 347 bool SkAnimatorScript::EvaluateString(SkAnimateMaker& maker, SkDisplayable* displayable, SkDisplayable* parent, const char* script, SkString* result) {
michael@0 348 SkAnimatorScript engine(maker, displayable, SkType_String);
michael@0 349 engine.fParent = parent;
michael@0 350 SkScriptValue value;
michael@0 351 bool success = engine.evaluate(script, &value, SkType_String);
michael@0 352 if (success)
michael@0 353 result->set(*(value.fOperand.fString));
michael@0 354 return success;
michael@0 355 }
michael@0 356
michael@0 357 const SkDisplayEnumMap& SkAnimatorScript::GetEnumValues(SkDisplayTypes type) {
michael@0 358 int index = SkTSearch<SkDisplayTypes>(&gEnumMaps[0].fType, gEnumMapCount, type,
michael@0 359 sizeof(SkDisplayEnumMap));
michael@0 360 SkASSERT(index >= 0);
michael@0 361 return gEnumMaps[index];
michael@0 362 }
michael@0 363
michael@0 364 bool SkAnimatorScript::Infinity(const char* token, size_t len, void* user, SkScriptValue* value) {
michael@0 365 if (SK_LITERAL_STR_EQUAL("Infinity", token, len) == false)
michael@0 366 return false;
michael@0 367 value->fType = SkType_Float;
michael@0 368 value->fOperand.fScalar = SK_ScalarInfinity;
michael@0 369 return true;
michael@0 370 }
michael@0 371
michael@0 372 bool SkAnimatorScript::IsFinite(const char* function, size_t len, SkTDArray<SkScriptValue>& params,
michael@0 373 void* eng, SkScriptValue* value) {
michael@0 374 if (SK_LITERAL_STR_EQUAL(function, "isFinite", len) == false)
michael@0 375 return false;
michael@0 376 if (params.count() != 1)
michael@0 377 return false;
michael@0 378 SkScriptValue* scriptValue = params.begin();
michael@0 379 SkDisplayTypes type = scriptValue->fType;
michael@0 380 SkScalar scalar = scriptValue->fOperand.fScalar;
michael@0 381 value->fType = SkType_Int;
michael@0 382 value->fOperand.fS32 = type == SkType_Float ? SkScalarIsNaN(scalar) == false &&
michael@0 383 SkScalarAbs(scalar) != SK_ScalarInfinity : type == SkType_Int;
michael@0 384 return true;
michael@0 385 }
michael@0 386
michael@0 387 bool SkAnimatorScript::IsNaN(const char* function, size_t len, SkTDArray<SkScriptValue>& params,
michael@0 388 void* eng, SkScriptValue* value) {
michael@0 389 if (SK_LITERAL_STR_EQUAL("isNaN", function, len) == false)
michael@0 390 return false;
michael@0 391 if (params.count() != 1)
michael@0 392 return false;
michael@0 393 SkScriptValue* scriptValue = params.begin();
michael@0 394 value->fType = SkType_Int;
michael@0 395 value->fOperand.fS32 = scriptValue->fType == SkType_Float ? SkScalarIsNaN(scriptValue->fOperand.fScalar) : 0;
michael@0 396 return true;
michael@0 397 }
michael@0 398
michael@0 399 bool SkAnimatorScript::MapEnums(const char* ptr, const char* match, size_t len, int* value) {
michael@0 400 int index = 0;
michael@0 401 bool more = true;
michael@0 402 do {
michael@0 403 const char* last = strchr(ptr, '|');
michael@0 404 if (last == NULL) {
michael@0 405 last = &ptr[strlen(ptr)];
michael@0 406 more = false;
michael@0 407 }
michael@0 408 size_t length = last - ptr;
michael@0 409 if (len == length && strncmp(ptr, match, length) == 0) {
michael@0 410 *value = index;
michael@0 411 return true;
michael@0 412 }
michael@0 413 index++;
michael@0 414 ptr = last + 1;
michael@0 415 } while (more);
michael@0 416 return false;
michael@0 417 }
michael@0 418
michael@0 419 bool SkAnimatorScript::NaN(const char* token, size_t len, void* user, SkScriptValue* value) {
michael@0 420 if (SK_LITERAL_STR_EQUAL("NaN", token, len) == false)
michael@0 421 return false;
michael@0 422 value->fType = SkType_Float;
michael@0 423 value->fOperand.fScalar = SK_ScalarNaN;
michael@0 424 return true;
michael@0 425 }
michael@0 426
michael@0 427 #if 0
michael@0 428 bool SkAnimatorScript::ObjectToString(void* object, void* user, SkScriptValue* value) {
michael@0 429 SkTDict<SkDisplayable*>* ids = (SkTDict<SkDisplayable*>*) user;
michael@0 430 SkDisplayable* displayable = (SkDisplayable*) object;
michael@0 431 const char* key;
michael@0 432 bool success = ids->findKey(displayable, &key);
michael@0 433 if (success == false)
michael@0 434 return false;
michael@0 435 value->fOperand.fString = new SkString(key);
michael@0 436 value->fType = SkType_String;
michael@0 437 return true;
michael@0 438 }
michael@0 439 #endif
michael@0 440
michael@0 441 bool SkAnimatorScript::Unbox(void* m, SkScriptValue* scriptValue) {
michael@0 442 SkAnimateMaker* maker = (SkAnimateMaker*) m;
michael@0 443 SkASSERT((unsigned) scriptValue->fType == (unsigned) SkType_Displayable);
michael@0 444 SkDisplayable* displayable = (SkDisplayable*) scriptValue->fOperand.fObject;
michael@0 445 SkDisplayTypes type = displayable->getType();
michael@0 446 switch (displayable->getType()) {
michael@0 447 case SkType_Array: {
michael@0 448 SkDisplayArray* boxedValue = (SkDisplayArray*) displayable;
michael@0 449 scriptValue->fOperand.fArray = &boxedValue->values;
michael@0 450 } break;
michael@0 451 case SkType_Boolean: {
michael@0 452 SkDisplayBoolean* boxedValue = (SkDisplayBoolean*) displayable;
michael@0 453 scriptValue->fOperand.fS32 = boxedValue->value;
michael@0 454 } break;
michael@0 455 case SkType_Int: {
michael@0 456 SkDisplayInt* boxedValue = (SkDisplayInt*) displayable;
michael@0 457 scriptValue->fOperand.fS32 = boxedValue->value;
michael@0 458 } break;
michael@0 459 case SkType_Float: {
michael@0 460 SkDisplayFloat* boxedValue = (SkDisplayFloat*) displayable;
michael@0 461 scriptValue->fOperand.fScalar = boxedValue->value;
michael@0 462 } break;
michael@0 463 case SkType_String: {
michael@0 464 SkDisplayString* boxedValue = (SkDisplayString*) displayable;
michael@0 465 scriptValue->fOperand.fString = SkNEW_ARGS(SkString, (boxedValue->value));
michael@0 466 } break;
michael@0 467 default: {
michael@0 468 const char* id = NULL;
michael@0 469 SkDEBUGCODE(bool success = ) maker->findKey(displayable, &id);
michael@0 470 SkASSERT(success);
michael@0 471 scriptValue->fOperand.fString = SkNEW_ARGS(SkString, (id));
michael@0 472 type = SkType_String;
michael@0 473 }
michael@0 474 }
michael@0 475 scriptValue->fType = type;
michael@0 476 return true;
michael@0 477 }
michael@0 478
michael@0 479 #if defined SK_SUPPORT_UNITTEST
michael@0 480
michael@0 481 #include "SkAnimator.h"
michael@0 482
michael@0 483 static const char scriptTestSetup[] =
michael@0 484 "<screenplay>\n"
michael@0 485 "<text id='label' text='defg'/>\n"
michael@0 486 "<add id='addLabel' use='label'/>\n"
michael@0 487 "<text id='text1' text='test'/>\n"
michael@0 488 "<apply scope='addLabel'>\n"
michael@0 489 "<set target='label' field='text' to='#script:text1.text'/>\n"
michael@0 490 "</apply>\n"
michael@0 491 "<apply>\n"
michael@0 492 "<paint id='labelPaint'>\n"
michael@0 493 "<emboss id='emboss' direction='[1,1,1]' />\n"
michael@0 494 "</paint>\n"
michael@0 495 "<animate id='animation' field='direction' target='emboss' from='[1,1,1]' to='[-1,1,1]' dur='1'/>\n"
michael@0 496 "<set lval='direction[0]' target='emboss' to='-1' />\n"
michael@0 497 "</apply>\n"
michael@0 498 "<color id='testColor' color='0 ? rgb(0,0,0) : rgb(255,255,255)' />\n"
michael@0 499 "<color id='xColor' color='rgb(12,34,56)' />\n"
michael@0 500 "<array id='emptyArray' />\n"
michael@0 501 "<array id='intArray' values='[1, 4, 6]' />\n"
michael@0 502 "<int id='idx' value='2' />\n"
michael@0 503 "<int id='idy' value='2' />\n"
michael@0 504 "<string id='alpha' value='abc' />\n"
michael@0 505 "<rect id='testRect' left='Math.cos(0)' top='2' right='12' bottom='5' />\n"
michael@0 506 "<event id='evt'>\n"
michael@0 507 "<input name='x' />\n"
michael@0 508 "<apply scope='idy'>\n"
michael@0 509 "<set field='value' to='evt.x.int' />\n"
michael@0 510 "</apply>\n"
michael@0 511 "</event>\n"
michael@0 512 "</screenplay>";
michael@0 513
michael@0 514 #define DEFAULT_ANSWER , 0
michael@0 515
michael@0 516 static const SkScriptNAnswer scriptTests[] = {
michael@0 517 { "label.text.length == 4", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 518 // { "labelPaint.measureText(label.text) > 0 ? labelPaint.measureText(label.text)+10 : 40", SkType_Float, 0, SkIntToScalar(0x23) },
michael@0 519 { "Number.POSITIVE_INFINITY >= Number.MAX_VALUE ? 1 : 0", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 520 { "Infinity >= Number.MAX_VALUE ? 1 : 0", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 521 { "Number.NEGATIVE_INFINITY <= -Number.MAX_VALUE ? 1 : 0", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 522 { "Number.MIN_VALUE > 0 ? 1 : 0", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 523 { "isNaN(Number.NaN)", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 524 { "isNaN(NaN)", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 525 { "Math.sin(0)", SkType_Float, 0, SkIntToScalar(0) DEFAULT_ANSWER },
michael@0 526 { "alpha+alpha", SkType_String, 0, 0, "abcabc" },
michael@0 527 { "intArray[4]", SkType_Unknown DEFAULT_ANSWER DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 528 { "emptyArray[4]", SkType_Unknown DEFAULT_ANSWER DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 529 { "idx", SkType_Int, 2 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 530 { "intArray.length", SkType_Int, 3 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 531 { "intArray.values[0]", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 532 { "intArray[0]", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 533 { "idx.value", SkType_Int, 2 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 534 { "alpha.value", SkType_String, 0, 0, "abc" },
michael@0 535 { "alpha", SkType_String, 0, 0, "abc" },
michael@0 536 { "alpha.value+alpha.value", SkType_String, 0, 0, "abcabc" },
michael@0 537 { "alpha+idx", SkType_String, 0, 0, "abc2" },
michael@0 538 { "idx+alpha", SkType_String, 0, 0, "2abc" },
michael@0 539 { "intArray[idx]", SkType_Int, 6 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 540 { "alpha.slice(1,2)", SkType_String, 0, 0, "b" },
michael@0 541 { "alpha.value.slice(1,2)", SkType_String, 0, 0, "b" },
michael@0 542 { "testRect.left+2", SkType_Float, 0, SkIntToScalar(3) DEFAULT_ANSWER },
michael@0 543 { "0 ? Math.sin(0) : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 544 { "0 ? intArray[0] : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 545 { "0 ? intArray.values[0] : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 546 { "0 ? idx : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 547 { "0 ? idx.value : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 548 { "0 ? alpha.slice(1,2) : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 549 { "0 ? alpha.value.slice(1,2) : 1", SkType_Int, 1 DEFAULT_ANSWER DEFAULT_ANSWER },
michael@0 550 { "idy", SkType_Int, 3 DEFAULT_ANSWER DEFAULT_ANSWER }
michael@0 551 };
michael@0 552
michael@0 553 #define SkScriptNAnswer_testCount SK_ARRAY_COUNT(scriptTests)
michael@0 554
michael@0 555 void SkAnimatorScript::UnitTest() {
michael@0 556 #if defined(SK_SUPPORT_UNITTEST)
michael@0 557 SkAnimator animator;
michael@0 558 SkASSERT(animator.decodeMemory(scriptTestSetup, sizeof(scriptTestSetup)-1));
michael@0 559 SkEvent evt;
michael@0 560 evt.setString("id", "evt");
michael@0 561 evt.setS32("x", 3);
michael@0 562 animator.doUserEvent(evt);
michael@0 563 // set up animator with memory script above, then run value tests
michael@0 564 for (unsigned index = 0; index < SkScriptNAnswer_testCount; index++) {
michael@0 565 SkAnimatorScript engine(*animator.fMaker, NULL, scriptTests[index].fType);
michael@0 566 SkScriptValue value;
michael@0 567 const char* script = scriptTests[index].fScript;
michael@0 568 bool success = engine.evaluateScript(&script, &value);
michael@0 569 if (success == false) {
michael@0 570 SkDebugf("script failed: %s\n", scriptTests[index].fScript);
michael@0 571 SkASSERT(scriptTests[index].fType == SkType_Unknown);
michael@0 572 continue;
michael@0 573 }
michael@0 574 SkASSERT(value.fType == scriptTests[index].fType);
michael@0 575 SkScalar error;
michael@0 576 switch (value.fType) {
michael@0 577 case SkType_Int:
michael@0 578 SkASSERT(value.fOperand.fS32 == scriptTests[index].fIntAnswer);
michael@0 579 break;
michael@0 580 case SkType_Float:
michael@0 581 error = SkScalarAbs(value.fOperand.fScalar - scriptTests[index].fScalarAnswer);
michael@0 582 SkASSERT(error < SK_Scalar1 / 10000);
michael@0 583 break;
michael@0 584 case SkType_String:
michael@0 585 SkASSERT(strcmp(value.fOperand.fString->c_str(), scriptTests[index].fStringAnswer) == 0);
michael@0 586 break;
michael@0 587 default:
michael@0 588 SkASSERT(0);
michael@0 589 }
michael@0 590 }
michael@0 591 #endif
michael@0 592 }
michael@0 593
michael@0 594 #endif

mercurial