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 "SkPictureRecorder.h" | |
22 #include "SkPixelRef.h" | 23 #include "SkPixelRef.h" |
23 #include "SkRRect.h" | 24 #include "SkRRect.h" |
24 #include "SkString.h" | 25 #include "SkString.h" |
25 #include "SkSurface.h" | 26 #include "SkSurface.h" |
26 #include "SkTextBlob.h" | 27 #include "SkTextBlob.h" |
27 #include "SkTypeface.h" | 28 #include "SkTypeface.h" |
28 | 29 |
29 extern "C" { | 30 extern "C" { |
30 #include "lua.h" | 31 #include "lua.h" |
31 #include "lualib.h" | 32 #include "lualib.h" |
32 #include "lauxlib.h" | 33 #include "lauxlib.h" |
33 } | 34 } |
34 | 35 |
35 // return the metatable name for a given class | 36 // return the metatable name for a given class |
36 template <typename T> const char* get_mtname(); | 37 template <typename T> const char* get_mtname(); |
37 #define DEF_MTNAME(T) \ | 38 #define DEF_MTNAME(T) \ |
38 template <> const char* get_mtname<T>() { \ | 39 template <> const char* get_mtname<T>() { \ |
39 return #T "_LuaMetaTableName"; \ | 40 return #T "_LuaMetaTableName"; \ |
40 } | 41 } |
41 | 42 |
42 DEF_MTNAME(SkCanvas) | 43 DEF_MTNAME(SkCanvas) |
43 DEF_MTNAME(SkDocument) | 44 DEF_MTNAME(SkDocument) |
44 DEF_MTNAME(SkImage) | 45 DEF_MTNAME(SkImage) |
45 DEF_MTNAME(SkMatrix) | 46 DEF_MTNAME(SkMatrix) |
46 DEF_MTNAME(SkRRect) | 47 DEF_MTNAME(SkRRect) |
47 DEF_MTNAME(SkPath) | 48 DEF_MTNAME(SkPath) |
48 DEF_MTNAME(SkPaint) | 49 DEF_MTNAME(SkPaint) |
49 DEF_MTNAME(SkPathEffect) | 50 DEF_MTNAME(SkPathEffect) |
51 DEF_MTNAME(SkPicture) | |
52 DEF_MTNAME(SkPictureRecorder) | |
50 DEF_MTNAME(SkShader) | 53 DEF_MTNAME(SkShader) |
51 DEF_MTNAME(SkSurface) | 54 DEF_MTNAME(SkSurface) |
52 DEF_MTNAME(SkTextBlob) | 55 DEF_MTNAME(SkTextBlob) |
53 DEF_MTNAME(SkTypeface) | 56 DEF_MTNAME(SkTypeface) |
54 | 57 |
55 template <typename T> T* push_new(lua_State* L) { | 58 template <typename T> T* push_new(lua_State* L) { |
56 T* addr = (T*)lua_newuserdata(L, sizeof(T)); | 59 T* addr = (T*)lua_newuserdata(L, sizeof(T)); |
57 new (addr) T; | 60 new (addr) T; |
58 luaL_getmetatable(L, get_mtname<T>()); | 61 luaL_getmetatable(L, get_mtname<T>()); |
59 lua_setmetatable(L, -2); | 62 lua_setmetatable(L, -2); |
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
502 canvas->drawImageRect(image, srcRPtr, dstR, lua2OptionalPaint(L, 5, &paint)) ; | 505 canvas->drawImageRect(image, srcRPtr, dstR, lua2OptionalPaint(L, 5, &paint)) ; |
503 return 0; | 506 return 0; |
504 } | 507 } |
505 | 508 |
506 static int lcanvas_drawPath(lua_State* L) { | 509 static int lcanvas_drawPath(lua_State* L) { |
507 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2), | 510 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2), |
508 *get_obj<SkPaint>(L, 3)); | 511 *get_obj<SkPaint>(L, 3)); |
509 return 0; | 512 return 0; |
510 } | 513 } |
511 | 514 |
515 // drawPicture(pic, x, y, paint) | |
516 static int lcanvas_drawPicture(lua_State* L) { | |
517 SkCanvas* canvas = get_ref<SkCanvas>(L, 1); | |
518 SkPicture* picture = get_ref<SkPicture>(L, 2); | |
519 SkScalar x = lua2scalar_def(L, 3, 0); | |
520 SkScalar y = lua2scalar_def(L, 4, 0); | |
521 SkMatrix matrix, *matrixPtr = NULL; | |
522 if (x || y) { | |
523 matrix.setTranslate(x, y); | |
524 matrixPtr = &matrix; | |
525 } | |
526 SkPaint paint; | |
527 canvas->drawPicture(picture, matrixPtr, lua2OptionalPaint(L, 5, &paint)); | |
528 return 0; | |
529 } | |
530 | |
512 static int lcanvas_drawText(lua_State* L) { | 531 static int lcanvas_drawText(lua_State* L) { |
513 if (lua_gettop(L) < 5) { | 532 if (lua_gettop(L) < 5) { |
514 return 0; | 533 return 0; |
515 } | 534 } |
516 | 535 |
517 if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) { | 536 if (lua_isstring(L, 2) && lua_isnumber(L, 3) && lua_isnumber(L, 4)) { |
518 size_t len; | 537 size_t len; |
519 const char* text = lua_tolstring(L, 2, &len); | 538 const char* text = lua_tolstring(L, 2, &len); |
520 get_ref<SkCanvas>(L, 1)->drawText(text, len, | 539 get_ref<SkCanvas>(L, 1)->drawText(text, len, |
521 lua2scalar(L, 3), lua2scalar(L, 4), | 540 lua2scalar(L, 3), lua2scalar(L, 4), |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
629 | 648 |
630 const struct luaL_Reg gSkCanvas_Methods[] = { | 649 const struct luaL_Reg gSkCanvas_Methods[] = { |
631 { "clear", lcanvas_clear }, | 650 { "clear", lcanvas_clear }, |
632 { "drawColor", lcanvas_drawColor }, | 651 { "drawColor", lcanvas_drawColor }, |
633 { "drawRect", lcanvas_drawRect }, | 652 { "drawRect", lcanvas_drawRect }, |
634 { "drawOval", lcanvas_drawOval }, | 653 { "drawOval", lcanvas_drawOval }, |
635 { "drawCircle", lcanvas_drawCircle }, | 654 { "drawCircle", lcanvas_drawCircle }, |
636 { "drawImage", lcanvas_drawImage }, | 655 { "drawImage", lcanvas_drawImage }, |
637 { "drawImageRect", lcanvas_drawImageRect }, | 656 { "drawImageRect", lcanvas_drawImageRect }, |
638 { "drawPath", lcanvas_drawPath }, | 657 { "drawPath", lcanvas_drawPath }, |
658 { "drawPicture", lcanvas_drawPicture }, | |
639 { "drawText", lcanvas_drawText }, | 659 { "drawText", lcanvas_drawText }, |
640 { "getSaveCount", lcanvas_getSaveCount }, | 660 { "getSaveCount", lcanvas_getSaveCount }, |
641 { "getTotalMatrix", lcanvas_getTotalMatrix }, | 661 { "getTotalMatrix", lcanvas_getTotalMatrix }, |
642 { "getClipStack", lcanvas_getClipStack }, | 662 { "getClipStack", lcanvas_getClipStack }, |
643 #if SK_SUPPORT_GPU | 663 #if SK_SUPPORT_GPU |
644 { "getReducedClipStack", SkLua::lcanvas_getReducedClipStack }, | 664 { "getReducedClipStack", SkLua::lcanvas_getReducedClipStack }, |
645 #endif | 665 #endif |
646 { "save", lcanvas_save }, | 666 { "save", lcanvas_save }, |
647 { "restore", lcanvas_restore }, | 667 { "restore", lcanvas_restore }, |
648 { "scale", lcanvas_scale }, | 668 { "scale", lcanvas_scale }, |
(...skipping 810 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1459 lua_pushnil(L); | 1479 lua_pushnil(L); |
1460 } else { | 1480 } else { |
1461 push_ref(L, image); | 1481 push_ref(L, image); |
1462 image->unref(); | 1482 image->unref(); |
1463 } | 1483 } |
1464 return 1; | 1484 return 1; |
1465 } | 1485 } |
1466 | 1486 |
1467 static int lsurface_newSurface(lua_State* L) { | 1487 static int lsurface_newSurface(lua_State* L) { |
1468 int width = lua2int_def(L, 2, 0); | 1488 int width = lua2int_def(L, 2, 0); |
1469 int height = lua2int_def(L, 2, 0); | 1489 int height = lua2int_def(L, 3, 0); |
1470 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); | 1490 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
1471 SkSurface* surface = get_ref<SkSurface>(L, 1)->newSurface(info); | 1491 SkSurface* surface = get_ref<SkSurface>(L, 1)->newSurface(info); |
1472 if (NULL == surface) { | 1492 if (NULL == surface) { |
1473 lua_pushnil(L); | 1493 lua_pushnil(L); |
1474 } else { | 1494 } else { |
1475 push_ref(L, surface); | 1495 push_ref(L, surface); |
1476 surface->unref(); | 1496 surface->unref(); |
1477 } | 1497 } |
1478 return 1; | 1498 return 1; |
1479 } | 1499 } |
1480 | 1500 |
1481 static int lsurface_gc(lua_State* L) { | 1501 static int lsurface_gc(lua_State* L) { |
1482 get_ref<SkSurface>(L, 1)->unref(); | 1502 get_ref<SkSurface>(L, 1)->unref(); |
1483 return 0; | 1503 return 0; |
1484 } | 1504 } |
1485 | 1505 |
1486 static const struct luaL_Reg gSkSurface_Methods[] = { | 1506 static const struct luaL_Reg gSkSurface_Methods[] = { |
1487 { "width", lsurface_width }, | 1507 { "width", lsurface_width }, |
1488 { "height", lsurface_height }, | 1508 { "height", lsurface_height }, |
1489 { "getCanvas", lsurface_getCanvas }, | 1509 { "getCanvas", lsurface_getCanvas }, |
1490 { "newImageSnapshot", lsurface_newImageSnapshot }, | 1510 { "newImageSnapshot", lsurface_newImageSnapshot }, |
1491 { "newSurface", lsurface_newSurface }, | 1511 { "newSurface", lsurface_newSurface }, |
1492 { "__gc", lsurface_gc }, | 1512 { "__gc", lsurface_gc }, |
1493 { NULL, NULL } | 1513 { NULL, NULL } |
1494 }; | 1514 }; |
1495 | 1515 |
1496 /////////////////////////////////////////////////////////////////////////////// | 1516 /////////////////////////////////////////////////////////////////////////////// |
1497 | 1517 |
1518 static int lpicturerecorder_beginRecording(lua_State* L) { | |
1519 const SkScalar w = lua2scalar_def(L, 2, -1); | |
1520 const SkScalar h = lua2scalar_def(L, 3, -1); | |
1521 if (w <= 0 || h <= 0) { | |
1522 lua_pushnil(L); | |
1523 return 1; | |
1524 } | |
1525 | |
1526 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->beginRecording(w, h); | |
1527 if (NULL == canvas) { | |
1528 lua_pushnil(L); | |
1529 return 1; | |
1530 } | |
1531 | |
1532 push_ref(L, canvas); | |
1533 return 1; | |
1534 } | |
1535 | |
1536 static int lpicturerecorder_getCanvas(lua_State* L) { | |
1537 SkCanvas* canvas = get_obj<SkPictureRecorder>(L, 1)->getRecordingCanvas(); | |
1538 if (NULL == canvas) { | |
1539 lua_pushnil(L); | |
1540 return 1; | |
1541 } | |
1542 push_ref(L, canvas); | |
1543 return 1; | |
1544 } | |
1545 | |
1546 static int lpicturerecorder_endRecording(lua_State* L) { | |
1547 SkPicture* pic = get_obj<SkPictureRecorder>(L, 1)->endRecording(); | |
1548 if (NULL == pic) { | |
1549 lua_pushnil(L); | |
1550 return 1; | |
1551 } | |
1552 push_ref(L, pic); | |
1553 pic->unref(); // lua is the only owner, so we unref ours | |
1554 return 1; | |
1555 } | |
1556 | |
1557 static int lpicturerecorder_gc(lua_State* L) { | |
1558 get_obj<SkPictureRecorder>(L, 1)->~SkPictureRecorder(); | |
1559 return 0; | |
1560 } | |
1561 | |
1562 static const struct luaL_Reg gSkPictureRecorder_Methods[] = { | |
1563 { "beginRecording", lpicturerecorder_beginRecording }, | |
1564 { "getCanvas", lpicturerecorder_getCanvas }, | |
1565 { "endRecording", lpicturerecorder_endRecording }, | |
1566 { "__gc", lpicturerecorder_gc }, | |
1567 { NULL, NULL } | |
1568 }; | |
1569 | |
1570 /////////////////////////////////////////////////////////////////////////////// | |
1571 | |
robertphillips
2014/10/13 18:31:59
float ?
reed1
2014/10/13 18:34:37
the lua function returns the number of arguments i
| |
1572 static int lpicture_width(lua_State* L) { | |
1573 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().width()); | |
1574 return 1; | |
1575 } | |
1576 | |
robertphillips
2014/10/13 18:31:59
float ?
| |
1577 static int lpicture_height(lua_State* L) { | |
1578 lua_pushnumber(L, get_ref<SkPicture>(L, 1)->cullRect().height()); | |
1579 return 1; | |
1580 } | |
1581 | |
1582 static int lpicture_gc(lua_State* L) { | |
1583 get_ref<SkPicture>(L, 1)->unref(); | |
1584 return 0; | |
1585 } | |
1586 | |
1587 static const struct luaL_Reg gSkPicture_Methods[] = { | |
1588 { "width", lpicture_width }, | |
1589 { "height", lpicture_height }, | |
1590 { "__gc", lpicture_gc }, | |
1591 { NULL, NULL } | |
1592 }; | |
1593 | |
1594 /////////////////////////////////////////////////////////////////////////////// | |
1595 | |
1498 static int ltypeface_gc(lua_State* L) { | 1596 static int ltypeface_gc(lua_State* L) { |
1499 SkSafeUnref(get_ref<SkTypeface>(L, 1)); | 1597 SkSafeUnref(get_ref<SkTypeface>(L, 1)); |
1500 return 0; | 1598 return 0; |
1501 } | 1599 } |
1502 | 1600 |
1503 static const struct luaL_Reg gSkTypeface_Methods[] = { | 1601 static const struct luaL_Reg gSkTypeface_Methods[] = { |
1504 { "__gc", ltypeface_gc }, | 1602 { "__gc", ltypeface_gc }, |
1505 { NULL, NULL } | 1603 { NULL, NULL } |
1506 }; | 1604 }; |
1507 | 1605 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1555 static int lsk_newPaint(lua_State* L) { | 1653 static int lsk_newPaint(lua_State* L) { |
1556 push_new<SkPaint>(L); | 1654 push_new<SkPaint>(L); |
1557 return 1; | 1655 return 1; |
1558 } | 1656 } |
1559 | 1657 |
1560 static int lsk_newPath(lua_State* L) { | 1658 static int lsk_newPath(lua_State* L) { |
1561 push_new<SkPath>(L); | 1659 push_new<SkPath>(L); |
1562 return 1; | 1660 return 1; |
1563 } | 1661 } |
1564 | 1662 |
1663 static int lsk_newPictureRecorder(lua_State* L) { | |
1664 push_new<SkPictureRecorder>(L); | |
1665 return 1; | |
1666 } | |
1667 | |
1565 static int lsk_newRRect(lua_State* L) { | 1668 static int lsk_newRRect(lua_State* L) { |
1566 SkRRect* rr = push_new<SkRRect>(L); | 1669 SkRRect* rr = push_new<SkRRect>(L); |
1567 rr->setEmpty(); | 1670 rr->setEmpty(); |
1568 return 1; | 1671 return 1; |
1569 } | 1672 } |
1570 | 1673 |
1571 static int lsk_newTypeface(lua_State* L) { | 1674 static int lsk_newTypeface(lua_State* L) { |
1572 const char* name = NULL; | 1675 const char* name = NULL; |
1573 int style = SkTypeface::kNormal; | 1676 int style = SkTypeface::kNormal; |
1574 | 1677 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1626 static void register_Sk(lua_State* L) { | 1729 static void register_Sk(lua_State* L) { |
1627 lua_newtable(L); | 1730 lua_newtable(L); |
1628 lua_pushvalue(L, -1); | 1731 lua_pushvalue(L, -1); |
1629 lua_setglobal(L, "Sk"); | 1732 lua_setglobal(L, "Sk"); |
1630 // the Sk table is still on top | 1733 // the Sk table is still on top |
1631 | 1734 |
1632 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF); | 1735 setfield_function(L, "newDocumentPDF", lsk_newDocumentPDF); |
1633 setfield_function(L, "loadImage", lsk_loadImage); | 1736 setfield_function(L, "loadImage", lsk_loadImage); |
1634 setfield_function(L, "newPaint", lsk_newPaint); | 1737 setfield_function(L, "newPaint", lsk_newPaint); |
1635 setfield_function(L, "newPath", lsk_newPath); | 1738 setfield_function(L, "newPath", lsk_newPath); |
1739 setfield_function(L, "newPictureRecorder", lsk_newPictureRecorder); | |
1636 setfield_function(L, "newRRect", lsk_newRRect); | 1740 setfield_function(L, "newRRect", lsk_newRRect); |
1637 setfield_function(L, "newRasterSurface", lsk_newRasterSurface); | 1741 setfield_function(L, "newRasterSurface", lsk_newRasterSurface); |
1638 setfield_function(L, "newTypeface", lsk_newTypeface); | 1742 setfield_function(L, "newTypeface", lsk_newTypeface); |
1639 lua_pop(L, 1); // pop off the Sk table | 1743 lua_pop(L, 1); // pop off the Sk table |
1640 } | 1744 } |
1641 | 1745 |
1642 #define REG_CLASS(L, C) \ | 1746 #define REG_CLASS(L, C) \ |
1643 do { \ | 1747 do { \ |
1644 luaL_newmetatable(L, get_mtname<C>()); \ | 1748 luaL_newmetatable(L, get_mtname<C>()); \ |
1645 lua_pushvalue(L, -1); \ | 1749 lua_pushvalue(L, -1); \ |
1646 lua_setfield(L, -2, "__index"); \ | 1750 lua_setfield(L, -2, "__index"); \ |
1647 luaL_setfuncs(L, g##C##_Methods, 0); \ | 1751 luaL_setfuncs(L, g##C##_Methods, 0); \ |
1648 lua_pop(L, 1); /* pop off the meta-table */ \ | 1752 lua_pop(L, 1); /* pop off the meta-table */ \ |
1649 } while (0) | 1753 } while (0) |
1650 | 1754 |
1651 void SkLua::Load(lua_State* L) { | 1755 void SkLua::Load(lua_State* L) { |
1652 register_Sk(L); | 1756 register_Sk(L); |
1653 REG_CLASS(L, SkCanvas); | 1757 REG_CLASS(L, SkCanvas); |
1654 REG_CLASS(L, SkDocument); | 1758 REG_CLASS(L, SkDocument); |
1655 REG_CLASS(L, SkImage); | 1759 REG_CLASS(L, SkImage); |
1656 REG_CLASS(L, SkPaint); | 1760 REG_CLASS(L, SkPaint); |
1657 REG_CLASS(L, SkPath); | 1761 REG_CLASS(L, SkPath); |
1658 REG_CLASS(L, SkPathEffect); | 1762 REG_CLASS(L, SkPathEffect); |
1763 REG_CLASS(L, SkPicture); | |
1764 REG_CLASS(L, SkPictureRecorder); | |
1659 REG_CLASS(L, SkRRect); | 1765 REG_CLASS(L, SkRRect); |
1660 REG_CLASS(L, SkShader); | 1766 REG_CLASS(L, SkShader); |
1661 REG_CLASS(L, SkSurface); | 1767 REG_CLASS(L, SkSurface); |
1662 REG_CLASS(L, SkTypeface); | 1768 REG_CLASS(L, SkTypeface); |
1663 REG_CLASS(L, SkMatrix); | 1769 REG_CLASS(L, SkMatrix); |
1664 } | 1770 } |
1665 | 1771 |
1666 extern "C" int luaopen_skia(lua_State* L); | 1772 extern "C" int luaopen_skia(lua_State* L); |
1667 extern "C" int luaopen_skia(lua_State* L) { | 1773 extern "C" int luaopen_skia(lua_State* L) { |
1668 SkLua::Load(L); | 1774 SkLua::Load(L); |
1669 return 0; | 1775 return 0; |
1670 } | 1776 } |
OLD | NEW |