| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkLua.h" | 8 #include "SkLua.h" |
| 9 | 9 |
| 10 #if SK_SUPPORT_GPU | 10 #if SK_SUPPORT_GPU |
| 11 #include "GrReducedClip.h" | 11 #include "GrReducedClip.h" |
| 12 #endif | 12 #endif |
| 13 | 13 |
| 14 #include "SkCanvas.h" | 14 #include "SkCanvas.h" |
| 15 #include "SkData.h" | 15 #include "SkData.h" |
| 16 #include "SkDecodingImageGenerator.h" | 16 #include "SkDecodingImageGenerator.h" |
| 17 #include "SkDocument.h" | 17 #include "SkDocument.h" |
| 18 #include "SkImage.h" | 18 #include "SkImage.h" |
| 19 #include "SkMatrix.h" | 19 #include "SkMatrix.h" |
| 20 #include "SkPaint.h" | 20 #include "SkPaint.h" |
| 21 #include "SkPath.h" | 21 #include "SkPath.h" |
| 22 #include "SkPixelRef.h" | 22 #include "SkPixelRef.h" |
| 23 #include "SkRRect.h" | 23 #include "SkRRect.h" |
| 24 #include "SkString.h" | 24 #include "SkString.h" |
| 25 #include "SkSurface.h" |
| 25 #include "SkTextBlob.h" | 26 #include "SkTextBlob.h" |
| 26 #include "SkTypeface.h" | 27 #include "SkTypeface.h" |
| 27 | 28 |
| 28 extern "C" { | 29 extern "C" { |
| 29 #include "lua.h" | 30 #include "lua.h" |
| 30 #include "lualib.h" | 31 #include "lualib.h" |
| 31 #include "lauxlib.h" | 32 #include "lauxlib.h" |
| 32 } | 33 } |
| 33 | 34 |
| 34 // return the metatable name for a given class | 35 // return the metatable name for a given class |
| 35 template <typename T> const char* get_mtname(); | 36 template <typename T> const char* get_mtname(); |
| 36 #define DEF_MTNAME(T) \ | 37 #define DEF_MTNAME(T) \ |
| 37 template <> const char* get_mtname<T>() { \ | 38 template <> const char* get_mtname<T>() { \ |
| 38 return #T "_LuaMetaTableName"; \ | 39 return #T "_LuaMetaTableName"; \ |
| 39 } | 40 } |
| 40 | 41 |
| 41 DEF_MTNAME(SkCanvas) | 42 DEF_MTNAME(SkCanvas) |
| 42 DEF_MTNAME(SkDocument) | 43 DEF_MTNAME(SkDocument) |
| 43 DEF_MTNAME(SkImage) | 44 DEF_MTNAME(SkImage) |
| 44 DEF_MTNAME(SkMatrix) | 45 DEF_MTNAME(SkMatrix) |
| 45 DEF_MTNAME(SkRRect) | 46 DEF_MTNAME(SkRRect) |
| 46 DEF_MTNAME(SkPath) | 47 DEF_MTNAME(SkPath) |
| 47 DEF_MTNAME(SkPaint) | 48 DEF_MTNAME(SkPaint) |
| 48 DEF_MTNAME(SkPathEffect) | 49 DEF_MTNAME(SkPathEffect) |
| 49 DEF_MTNAME(SkShader) | 50 DEF_MTNAME(SkShader) |
| 51 DEF_MTNAME(SkSurface) |
| 50 DEF_MTNAME(SkTextBlob) | 52 DEF_MTNAME(SkTextBlob) |
| 51 DEF_MTNAME(SkTypeface) | 53 DEF_MTNAME(SkTypeface) |
| 52 | 54 |
| 53 template <typename T> T* push_new(lua_State* L) { | 55 template <typename T> T* push_new(lua_State* L) { |
| 54 T* addr = (T*)lua_newuserdata(L, sizeof(T)); | 56 T* addr = (T*)lua_newuserdata(L, sizeof(T)); |
| 55 new (addr) T; | 57 new (addr) T; |
| 56 luaL_getmetatable(L, get_mtname<T>()); | 58 luaL_getmetatable(L, get_mtname<T>()); |
| 57 lua_setmetatable(L, -2); | 59 lua_setmetatable(L, -2); |
| 58 return addr; | 60 return addr; |
| 59 } | 61 } |
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 } | 346 } |
| 345 this->pushString(region_op(element.getOp()), "op"); | 347 this->pushString(region_op(element.getOp()), "op"); |
| 346 this->pushBool(element.isAA(), "aa"); | 348 this->pushBool(element.isAA(), "aa"); |
| 347 CHECK_SETFIELD(key); | 349 CHECK_SETFIELD(key); |
| 348 } | 350 } |
| 349 | 351 |
| 350 | 352 |
| 351 /////////////////////////////////////////////////////////////////////////////// | 353 /////////////////////////////////////////////////////////////////////////////// |
| 352 /////////////////////////////////////////////////////////////////////////////// | 354 /////////////////////////////////////////////////////////////////////////////// |
| 353 | 355 |
| 356 static int lua2int_def(lua_State* L, int index, int defaultValue) { |
| 357 if (lua_isnumber(L, index)) { |
| 358 return (int)lua_tonumber(L, index); |
| 359 } else { |
| 360 return defaultValue; |
| 361 } |
| 362 } |
| 363 |
| 354 static SkScalar lua2scalar(lua_State* L, int index) { | 364 static SkScalar lua2scalar(lua_State* L, int index) { |
| 355 SkASSERT(lua_isnumber(L, index)); | 365 SkASSERT(lua_isnumber(L, index)); |
| 356 return SkLuaToScalar(lua_tonumber(L, index)); | 366 return SkLuaToScalar(lua_tonumber(L, index)); |
| 357 } | 367 } |
| 358 | 368 |
| 359 static SkScalar lua2scalar_def(lua_State* L, int index, SkScalar defaultValue) { | 369 static SkScalar lua2scalar_def(lua_State* L, int index, SkScalar defaultValue) { |
| 360 if (lua_isnumber(L, index)) { | 370 if (lua_isnumber(L, index)) { |
| 361 return SkLuaToScalar(lua_tonumber(L, index)); | 371 return SkLuaToScalar(lua_tonumber(L, index)); |
| 362 } else { | 372 } else { |
| 363 return defaultValue; | 373 return defaultValue; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 393 if (x <= 0) { | 403 if (x <= 0) { |
| 394 return 0; | 404 return 0; |
| 395 } else if (x >= 1) { | 405 } else if (x >= 1) { |
| 396 return 255; | 406 return 255; |
| 397 } else { | 407 } else { |
| 398 return SkScalarRoundToInt(x * 255); | 408 return SkScalarRoundToInt(x * 255); |
| 399 } | 409 } |
| 400 } | 410 } |
| 401 | 411 |
| 402 static SkColor lua2color(lua_State* L, int index) { | 412 static SkColor lua2color(lua_State* L, int index) { |
| 403 return SkColorSetARGB(unit2byte(getfield_scalar(L, index, "a")), | 413 return SkColorSetARGB(unit2byte(getfield_scalar_default(L, index, "a", 1)), |
| 404 unit2byte(getfield_scalar(L, index, "r")), | 414 unit2byte(getfield_scalar_default(L, index, "r", 0)), |
| 405 unit2byte(getfield_scalar(L, index, "g")), | 415 unit2byte(getfield_scalar_default(L, index, "g", 0)), |
| 406 unit2byte(getfield_scalar(L, index, "b"))); | 416 unit2byte(getfield_scalar_default(L, index, "b", 0))); |
| 407 } | 417 } |
| 408 | 418 |
| 409 static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) { | 419 static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) { |
| 410 rect->set(getfield_scalar_default(L, index, "left", 0), | 420 rect->set(getfield_scalar_default(L, index, "left", 0), |
| 411 getfield_scalar_default(L, index, "top", 0), | 421 getfield_scalar_default(L, index, "top", 0), |
| 412 getfield_scalar(L, index, "right"), | 422 getfield_scalar(L, index, "right"), |
| 413 getfield_scalar(L, index, "bottom")); | 423 getfield_scalar(L, index, "bottom")); |
| 414 return rect; | 424 return rect; |
| 415 } | 425 } |
| 416 | 426 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 434 } | 444 } |
| 435 | 445 |
| 436 static int lcanvas_drawCircle(lua_State* L) { | 446 static int lcanvas_drawCircle(lua_State* L) { |
| 437 get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2), | 447 get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2), |
| 438 lua2scalar(L, 3), | 448 lua2scalar(L, 3), |
| 439 lua2scalar(L, 4), | 449 lua2scalar(L, 4), |
| 440 *get_obj<SkPaint>(L, 5)); | 450 *get_obj<SkPaint>(L, 5)); |
| 441 return 0; | 451 return 0; |
| 442 } | 452 } |
| 443 | 453 |
| 454 static SkPaint* lua2OptionalPaint(lua_State* L, int index, SkPaint* paint) { |
| 455 if (lua_isnumber(L, index)) { |
| 456 paint->setAlpha(SkScalarRoundToInt(lua2scalar(L, index) * 255)); |
| 457 return paint; |
| 458 } else { |
| 459 const SkPaint* ptr = get_obj<SkPaint>(L, index); |
| 460 if (ptr) { |
| 461 *paint = *ptr; |
| 462 return paint; |
| 463 } |
| 464 } |
| 465 return NULL; |
| 466 } |
| 467 |
| 444 static int lcanvas_drawImage(lua_State* L) { | 468 static int lcanvas_drawImage(lua_State* L) { |
| 445 SkCanvas* canvas = get_ref<SkCanvas>(L, 1); | 469 SkCanvas* canvas = get_ref<SkCanvas>(L, 1); |
| 446 SkImage* image = get_ref<SkImage>(L, 2); | 470 SkImage* image = get_ref<SkImage>(L, 2); |
| 447 if (NULL == image) { | 471 if (NULL == image) { |
| 448 return 0; | 472 return 0; |
| 449 } | 473 } |
| 450 SkScalar x = lua2scalar(L, 3); | 474 SkScalar x = lua2scalar(L, 3); |
| 451 SkScalar y = lua2scalar(L, 4); | 475 SkScalar y = lua2scalar(L, 4); |
| 452 | 476 |
| 453 SkPaint paint; | 477 SkPaint paint; |
| 454 const SkPaint* paintPtr = NULL; | 478 canvas->drawImage(image, x, y, lua2OptionalPaint(L, 5, &paint)); |
| 455 if (lua_isnumber(L, 5)) { | |
| 456 paint.setAlpha(SkScalarRoundToInt(lua2scalar(L, 5) * 255)); | |
| 457 paintPtr = &paint; | |
| 458 } | |
| 459 canvas->drawImage(image, x, y, paintPtr); | |
| 460 return 0; | 479 return 0; |
| 461 } | 480 } |
| 462 | 481 |
| 463 static int lcanvas_drawImageRect(lua_State* L) { | 482 static int lcanvas_drawImageRect(lua_State* L) { |
| 464 SkCanvas* canvas = get_ref<SkCanvas>(L, 1); | 483 SkCanvas* canvas = get_ref<SkCanvas>(L, 1); |
| 465 SkImage* image = get_ref<SkImage>(L, 2); | 484 SkImage* image = get_ref<SkImage>(L, 2); |
| 466 if (NULL == image) { | 485 if (NULL == image) { |
| 467 return 0; | 486 return 0; |
| 468 } | 487 } |
| 469 | 488 |
| 470 SkRect srcR, dstR; | 489 SkRect srcR, dstR; |
| 471 SkRect* srcRPtr = NULL; | 490 SkRect* srcRPtr = NULL; |
| 472 if (!lua_isnil(L, 3)) { | 491 if (!lua_isnil(L, 3)) { |
| 473 srcRPtr = lua2rect(L, 3, &srcR); | 492 srcRPtr = lua2rect(L, 3, &srcR); |
| 474 } | 493 } |
| 475 lua2rect(L, 4, &dstR); | 494 lua2rect(L, 4, &dstR); |
| 476 | 495 |
| 477 SkPaint paint; | 496 SkPaint paint; |
| 478 const SkPaint* paintPtr = NULL; | 497 canvas->drawImageRect(image, srcRPtr, dstR, lua2OptionalPaint(L, 5, &paint))
; |
| 479 if (lua_isnumber(L, 5)) { | |
| 480 paint.setAlpha(SkScalarRoundToInt(lua2scalar(L, 5) * 255)); | |
| 481 paintPtr = &paint; | |
| 482 } | |
| 483 canvas->drawImageRect(image, srcRPtr, dstR, paintPtr); | |
| 484 return 0; | 498 return 0; |
| 485 } | 499 } |
| 486 | 500 |
| 487 static int lcanvas_drawPath(lua_State* L) { | 501 static int lcanvas_drawPath(lua_State* L) { |
| 488 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2), | 502 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2), |
| 489 *get_obj<SkPaint>(L, 3)); | 503 *get_obj<SkPaint>(L, 3)); |
| 490 return 0; | 504 return 0; |
| 491 } | 505 } |
| 492 | 506 |
| 493 static int lcanvas_drawText(lua_State* L) { | 507 static int lcanvas_drawText(lua_State* L) { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 582 get_ref<SkCanvas>(L, 1)->translate(tx, ty); | 596 get_ref<SkCanvas>(L, 1)->translate(tx, ty); |
| 583 return 0; | 597 return 0; |
| 584 } | 598 } |
| 585 | 599 |
| 586 static int lcanvas_rotate(lua_State* L) { | 600 static int lcanvas_rotate(lua_State* L) { |
| 587 SkScalar degrees = lua2scalar_def(L, 2, 0); | 601 SkScalar degrees = lua2scalar_def(L, 2, 0); |
| 588 get_ref<SkCanvas>(L, 1)->rotate(degrees); | 602 get_ref<SkCanvas>(L, 1)->rotate(degrees); |
| 589 return 0; | 603 return 0; |
| 590 } | 604 } |
| 591 | 605 |
| 606 static int lcanvas_newSurface(lua_State* L) { |
| 607 int width = lua2int_def(L, 2, 0); |
| 608 int height = lua2int_def(L, 2, 0); |
| 609 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
| 610 SkSurface* surface = get_ref<SkCanvas>(L, 1)->newSurface(info); |
| 611 if (NULL == surface) { |
| 612 lua_pushnil(L); |
| 613 } else { |
| 614 push_ref(L, surface); |
| 615 surface->unref(); |
| 616 } |
| 617 return 1; |
| 618 } |
| 619 |
| 592 static int lcanvas_gc(lua_State* L) { | 620 static int lcanvas_gc(lua_State* L) { |
| 593 get_ref<SkCanvas>(L, 1)->unref(); | 621 get_ref<SkCanvas>(L, 1)->unref(); |
| 594 return 0; | 622 return 0; |
| 595 } | 623 } |
| 596 | 624 |
| 597 const struct luaL_Reg gSkCanvas_Methods[] = { | 625 const struct luaL_Reg gSkCanvas_Methods[] = { |
| 598 { "drawColor", lcanvas_drawColor }, | 626 { "drawColor", lcanvas_drawColor }, |
| 599 { "drawRect", lcanvas_drawRect }, | 627 { "drawRect", lcanvas_drawRect }, |
| 600 { "drawOval", lcanvas_drawOval }, | 628 { "drawOval", lcanvas_drawOval }, |
| 601 { "drawCircle", lcanvas_drawCircle }, | 629 { "drawCircle", lcanvas_drawCircle }, |
| 602 { "drawImage", lcanvas_drawImage }, | 630 { "drawImage", lcanvas_drawImage }, |
| 603 { "drawImageRect", lcanvas_drawImageRect }, | 631 { "drawImageRect", lcanvas_drawImageRect }, |
| 604 { "drawPath", lcanvas_drawPath }, | 632 { "drawPath", lcanvas_drawPath }, |
| 605 { "drawText", lcanvas_drawText }, | 633 { "drawText", lcanvas_drawText }, |
| 606 { "getSaveCount", lcanvas_getSaveCount }, | 634 { "getSaveCount", lcanvas_getSaveCount }, |
| 607 { "getTotalMatrix", lcanvas_getTotalMatrix }, | 635 { "getTotalMatrix", lcanvas_getTotalMatrix }, |
| 608 { "getClipStack", lcanvas_getClipStack }, | 636 { "getClipStack", lcanvas_getClipStack }, |
| 609 #if SK_SUPPORT_GPU | 637 #if SK_SUPPORT_GPU |
| 610 { "getReducedClipStack", SkLua::lcanvas_getReducedClipStack }, | 638 { "getReducedClipStack", SkLua::lcanvas_getReducedClipStack }, |
| 611 #endif | 639 #endif |
| 612 { "save", lcanvas_save }, | 640 { "save", lcanvas_save }, |
| 613 { "restore", lcanvas_restore }, | 641 { "restore", lcanvas_restore }, |
| 614 { "scale", lcanvas_scale }, | 642 { "scale", lcanvas_scale }, |
| 615 { "translate", lcanvas_translate }, | 643 { "translate", lcanvas_translate }, |
| 616 { "rotate", lcanvas_rotate }, | 644 { "rotate", lcanvas_rotate }, |
| 645 |
| 646 { "newSurface", lcanvas_newSurface }, |
| 647 |
| 617 { "__gc", lcanvas_gc }, | 648 { "__gc", lcanvas_gc }, |
| 618 { NULL, NULL } | 649 { NULL, NULL } |
| 619 }; | 650 }; |
| 620 | 651 |
| 621 /////////////////////////////////////////////////////////////////////////////// | 652 /////////////////////////////////////////////////////////////////////////////// |
| 622 | 653 |
| 623 static int ldocument_beginPage(lua_State* L) { | 654 static int ldocument_beginPage(lua_State* L) { |
| 624 const SkRect* contentPtr = NULL; | 655 const SkRect* contentPtr = NULL; |
| 625 push_ref(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2), | 656 push_ref(L, get_ref<SkDocument>(L, 1)->beginPage(lua2scalar(L, 2), |
| 626 lua2scalar(L, 3), | 657 lua2scalar(L, 3), |
| (...skipping 757 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1384 | 1415 |
| 1385 static const struct luaL_Reg gSkImage_Methods[] = { | 1416 static const struct luaL_Reg gSkImage_Methods[] = { |
| 1386 { "width", limage_width }, | 1417 { "width", limage_width }, |
| 1387 { "height", limage_height }, | 1418 { "height", limage_height }, |
| 1388 { "__gc", limage_gc }, | 1419 { "__gc", limage_gc }, |
| 1389 { NULL, NULL } | 1420 { NULL, NULL } |
| 1390 }; | 1421 }; |
| 1391 | 1422 |
| 1392 /////////////////////////////////////////////////////////////////////////////// | 1423 /////////////////////////////////////////////////////////////////////////////// |
| 1393 | 1424 |
| 1425 static int lsurface_width(lua_State* L) { |
| 1426 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->width()); |
| 1427 return 1; |
| 1428 } |
| 1429 |
| 1430 static int lsurface_height(lua_State* L) { |
| 1431 lua_pushinteger(L, get_ref<SkSurface>(L, 1)->height()); |
| 1432 return 1; |
| 1433 } |
| 1434 |
| 1435 static int lsurface_getCanvas(lua_State* L) { |
| 1436 SkCanvas* canvas = get_ref<SkSurface>(L, 1)->getCanvas(); |
| 1437 if (NULL == canvas) { |
| 1438 lua_pushnil(L); |
| 1439 } else { |
| 1440 push_ref(L, canvas); |
| 1441 // note: we don't unref canvas, since getCanvas did not ref it. |
| 1442 // warning: this is weird: now Lua owns a ref on this canvas, but what i
f they let |
| 1443 // the real owner (the surface) go away, but still hold onto the canvas? |
| 1444 // *really* we want to sort of ref the surface again, but have the nativ
e object |
| 1445 // know that it is supposed to be treated as a canvas... |
| 1446 } |
| 1447 return 1; |
| 1448 } |
| 1449 |
| 1450 static int lsurface_newImageSnapshot(lua_State* L) { |
| 1451 SkImage* image = get_ref<SkSurface>(L, 1)->newImageSnapshot(); |
| 1452 if (NULL == image) { |
| 1453 lua_pushnil(L); |
| 1454 } else { |
| 1455 push_ref(L, image); |
| 1456 image->unref(); |
| 1457 } |
| 1458 return 1; |
| 1459 } |
| 1460 |
| 1461 static int lsurface_newSurface(lua_State* L) { |
| 1462 int width = lua2int_def(L, 2, 0); |
| 1463 int height = lua2int_def(L, 2, 0); |
| 1464 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
| 1465 SkSurface* surface = get_ref<SkSurface>(L, 1)->newSurface(info); |
| 1466 if (NULL == surface) { |
| 1467 lua_pushnil(L); |
| 1468 } else { |
| 1469 push_ref(L, surface); |
| 1470 surface->unref(); |
| 1471 } |
| 1472 return 1; |
| 1473 } |
| 1474 |
| 1475 static int lsurface_gc(lua_State* L) { |
| 1476 get_ref<SkSurface>(L, 1)->unref(); |
| 1477 return 0; |
| 1478 } |
| 1479 |
| 1480 static const struct luaL_Reg gSkSurface_Methods[] = { |
| 1481 { "width", lsurface_width }, |
| 1482 { "height", lsurface_height }, |
| 1483 { "getCanvas", lsurface_getCanvas }, |
| 1484 { "newImageSnapshot", lsurface_newImageSnapshot }, |
| 1485 { "newSurface", lsurface_newSurface }, |
| 1486 { "__gc", lsurface_gc }, |
| 1487 { NULL, NULL } |
| 1488 }; |
| 1489 |
| 1490 /////////////////////////////////////////////////////////////////////////////// |
| 1491 |
| 1394 static int ltypeface_gc(lua_State* L) { | 1492 static int ltypeface_gc(lua_State* L) { |
| 1395 SkSafeUnref(get_ref<SkTypeface>(L, 1)); | 1493 SkSafeUnref(get_ref<SkTypeface>(L, 1)); |
| 1396 return 0; | 1494 return 0; |
| 1397 } | 1495 } |
| 1398 | 1496 |
| 1399 static const struct luaL_Reg gSkTypeface_Methods[] = { | 1497 static const struct luaL_Reg gSkTypeface_Methods[] = { |
| 1400 { "__gc", ltypeface_gc }, | 1498 { "__gc", ltypeface_gc }, |
| 1401 { NULL, NULL } | 1499 { NULL, NULL } |
| 1402 }; | 1500 }; |
| 1403 | 1501 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1480 (SkTypeface::Style)style); | 1578 (SkTypeface::Style)style); |
| 1481 // SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, f
ace->getRefCnt()); | 1579 // SkDebugf("---- name <%s> style=%d, face=%p ref=%d\n", name, style, face, f
ace->getRefCnt()); |
| 1482 if (NULL == face) { | 1580 if (NULL == face) { |
| 1483 face = SkTypeface::RefDefault(); | 1581 face = SkTypeface::RefDefault(); |
| 1484 } | 1582 } |
| 1485 push_ref(L, face); | 1583 push_ref(L, face); |
| 1486 face->unref(); | 1584 face->unref(); |
| 1487 return 1; | 1585 return 1; |
| 1488 } | 1586 } |
| 1489 | 1587 |
| 1588 static int lsk_newRasterSurface(lua_State* L) { |
| 1589 int width = lua2int_def(L, 2, 0); |
| 1590 int height = lua2int_def(L, 2, 0); |
| 1591 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
| 1592 SkSurface* surface = SkSurface::NewRaster(info); |
| 1593 if (NULL == surface) { |
| 1594 lua_pushnil(L); |
| 1595 } else { |
| 1596 push_ref(L, surface); |
| 1597 surface->unref(); |
| 1598 } |
| 1599 return 1; |
| 1600 } |
| 1601 |
| 1490 static int lsk_loadImage(lua_State* L) { | 1602 static int lsk_loadImage(lua_State* L) { |
| 1491 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) { | 1603 if (lua_gettop(L) > 0 && lua_isstring(L, 1)) { |
| 1492 const char* name = lua_tolstring(L, 1, NULL); | 1604 const char* name = lua_tolstring(L, 1, NULL); |
| 1493 SkAutoDataUnref data(SkData::NewFromFileName(name)); | 1605 SkAutoDataUnref data(SkData::NewFromFileName(name)); |
| 1494 if (data.get()) { | 1606 if (data.get()) { |
| 1495 SkImage* image = SkImage::NewFromGenerator( | 1607 SkImage* image = SkImage::NewFromGenerator( |
| 1496 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator:
:Options())); | 1608 SkDecodingImageGenerator::Create(data, SkDecodingImageGenerator:
:Options())); |
| 1497 | 1609 |
| 1498 if (image) { | 1610 if (image) { |
| 1499 push_ref(L, image); | 1611 push_ref(L, image); |
| 1500 image->unref(); | 1612 image->unref(); |
| 1501 return 1; | 1613 return 1; |
| 1502 } | 1614 } |
| 1503 } | 1615 } |
| 1504 } | 1616 } |
| 1505 return 0; | 1617 return 0; |
| 1506 } | 1618 } |
| 1507 | 1619 |
| 1508 static void register_Sk(lua_State* L) { | 1620 static void register_Sk(lua_State* L) { |
| 1509 lua_newtable(L); | 1621 lua_newtable(L); |
| 1510 lua_pushvalue(L, -1); | 1622 lua_pushvalue(L, -1); |
| 1511 lua_setglobal(L, "Sk"); | 1623 lua_setglobal(L, "Sk"); |
| 1512 // the Sk table is still on top | 1624 // the Sk table is still on top |
| 1513 | 1625 |
| 1514 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF); | 1626 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF); |
| 1515 setfield_function(L, "loadImage", lsk_loadImage); | 1627 setfield_function(L, "loadImage", lsk_loadImage); |
| 1516 setfield_function(L, "newPaint", lsk_newPaint); | 1628 setfield_function(L, "newPaint", lsk_newPaint); |
| 1517 setfield_function(L, "newPath", lsk_newPath); | 1629 setfield_function(L, "newPath", lsk_newPath); |
| 1518 setfield_function(L, "newRRect", lsk_newRRect); | 1630 setfield_function(L, "newRRect", lsk_newRRect); |
| 1631 setfield_function(L, "newRasterSurface", lsk_newRasterSurface); |
| 1519 setfield_function(L, "newTypeface", lsk_newTypeface); | 1632 setfield_function(L, "newTypeface", lsk_newTypeface); |
| 1520 lua_pop(L, 1); // pop off the Sk table | 1633 lua_pop(L, 1); // pop off the Sk table |
| 1521 } | 1634 } |
| 1522 | 1635 |
| 1523 #define REG_CLASS(L, C) \ | 1636 #define REG_CLASS(L, C) \ |
| 1524 do { \ | 1637 do { \ |
| 1525 luaL_newmetatable(L, get_mtname<C>()); \ | 1638 luaL_newmetatable(L, get_mtname<C>()); \ |
| 1526 lua_pushvalue(L, -1); \ | 1639 lua_pushvalue(L, -1); \ |
| 1527 lua_setfield(L, -2, "__index"); \ | 1640 lua_setfield(L, -2, "__index"); \ |
| 1528 luaL_setfuncs(L, g##C##_Methods, 0); \ | 1641 luaL_setfuncs(L, g##C##_Methods, 0); \ |
| 1529 lua_pop(L, 1); /* pop off the meta-table */ \ | 1642 lua_pop(L, 1); /* pop off the meta-table */ \ |
| 1530 } while (0) | 1643 } while (0) |
| 1531 | 1644 |
| 1532 void SkLua::Load(lua_State* L) { | 1645 void SkLua::Load(lua_State* L) { |
| 1533 register_Sk(L); | 1646 register_Sk(L); |
| 1534 REG_CLASS(L, SkCanvas); | 1647 REG_CLASS(L, SkCanvas); |
| 1535 REG_CLASS(L, SkDocument); | 1648 REG_CLASS(L, SkDocument); |
| 1536 REG_CLASS(L, SkImage); | 1649 REG_CLASS(L, SkImage); |
| 1537 REG_CLASS(L, SkPaint); | 1650 REG_CLASS(L, SkPaint); |
| 1538 REG_CLASS(L, SkPath); | 1651 REG_CLASS(L, SkPath); |
| 1539 REG_CLASS(L, SkPathEffect); | 1652 REG_CLASS(L, SkPathEffect); |
| 1540 REG_CLASS(L, SkRRect); | 1653 REG_CLASS(L, SkRRect); |
| 1541 REG_CLASS(L, SkShader); | 1654 REG_CLASS(L, SkShader); |
| 1655 REG_CLASS(L, SkSurface); |
| 1542 REG_CLASS(L, SkTypeface); | 1656 REG_CLASS(L, SkTypeface); |
| 1543 REG_CLASS(L, SkMatrix); | 1657 REG_CLASS(L, SkMatrix); |
| 1544 } | 1658 } |
| 1545 | 1659 |
| 1546 extern "C" int luaopen_skia(lua_State* L); | 1660 extern "C" int luaopen_skia(lua_State* L); |
| 1547 extern "C" int luaopen_skia(lua_State* L) { | 1661 extern "C" int luaopen_skia(lua_State* L) { |
| 1548 SkLua::Load(L); | 1662 SkLua::Load(L); |
| 1549 return 0; | 1663 return 0; |
| 1550 } | 1664 } |
| OLD | NEW |