SkLuaCanvas.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright 2013 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/utils/SkLuaCanvas.h"
  8. #include "include/private/SkTo.h"
  9. #include "include/utils/SkLua.h"
  10. #include "src/core/SkStringUtils.h"
  11. extern "C" {
  12. #include "lua.h"
  13. #include "lauxlib.h"
  14. }
  15. class AutoCallLua : public SkLua {
  16. public:
  17. AutoCallLua(lua_State* L, const char func[], const char verb[]) : INHERITED(L) {
  18. lua_getglobal(L, func);
  19. if (!lua_isfunction(L, -1)) {
  20. int t = lua_type(L, -1);
  21. SkDebugf("--- expected function %d\n", t);
  22. }
  23. lua_newtable(L);
  24. this->pushString(verb, "verb");
  25. }
  26. ~AutoCallLua() {
  27. lua_State* L = this->get();
  28. if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
  29. SkDebugf("lua err: %s\n", lua_tostring(L, -1));
  30. }
  31. lua_settop(L, -1);
  32. }
  33. void pushEncodedText(SkTextEncoding, const void*, size_t);
  34. private:
  35. typedef SkLua INHERITED;
  36. };
  37. #define AUTO_LUA(verb) AutoCallLua lua(fL, fFunc.c_str(), verb)
  38. ///////////////////////////////////////////////////////////////////////////////
  39. void AutoCallLua::pushEncodedText(SkTextEncoding enc, const void* text, size_t length) {
  40. switch (enc) {
  41. case SkTextEncoding::kUTF8:
  42. this->pushString((const char*)text, length, "text");
  43. break;
  44. case SkTextEncoding::kUTF16:
  45. this->pushString(SkStringFromUTF16((const uint16_t*)text, length), "text");
  46. break;
  47. case SkTextEncoding::kGlyphID:
  48. this->pushArrayU16((const uint16_t*)text, SkToInt(length >> 1),
  49. "glyphs");
  50. break;
  51. case SkTextEncoding::kUTF32:
  52. break;
  53. }
  54. }
  55. ///////////////////////////////////////////////////////////////////////////////
  56. void SkLuaCanvas::pushThis() {
  57. SkLua(fL).pushCanvas(this);
  58. }
  59. ///////////////////////////////////////////////////////////////////////////////
  60. SkLuaCanvas::SkLuaCanvas(int width, int height, lua_State* L, const char func[])
  61. : INHERITED(width, height)
  62. , fL(L)
  63. , fFunc(func) {
  64. }
  65. SkLuaCanvas::~SkLuaCanvas() {}
  66. void SkLuaCanvas::willSave() {
  67. AUTO_LUA("save");
  68. this->INHERITED::willSave();
  69. }
  70. SkCanvas::SaveLayerStrategy SkLuaCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
  71. AUTO_LUA("saveLayer");
  72. if (rec.fBounds) {
  73. lua.pushRect(*rec.fBounds, "bounds");
  74. }
  75. if (rec.fPaint) {
  76. lua.pushPaint(*rec.fPaint, "paint");
  77. }
  78. (void)this->INHERITED::getSaveLayerStrategy(rec);
  79. // No need for a layer.
  80. return kNoLayer_SaveLayerStrategy;
  81. }
  82. bool SkLuaCanvas::onDoSaveBehind(const SkRect*) {
  83. // TODO
  84. return false;
  85. }
  86. void SkLuaCanvas::willRestore() {
  87. AUTO_LUA("restore");
  88. this->INHERITED::willRestore();
  89. }
  90. void SkLuaCanvas::didConcat(const SkMatrix& matrix) {
  91. switch (matrix.getType()) {
  92. case SkMatrix::kTranslate_Mask: {
  93. AUTO_LUA("translate");
  94. lua.pushScalar(matrix.getTranslateX(), "dx");
  95. lua.pushScalar(matrix.getTranslateY(), "dy");
  96. break;
  97. }
  98. case SkMatrix::kScale_Mask: {
  99. AUTO_LUA("scale");
  100. lua.pushScalar(matrix.getScaleX(), "sx");
  101. lua.pushScalar(matrix.getScaleY(), "sy");
  102. break;
  103. }
  104. default: {
  105. AUTO_LUA("concat");
  106. // pushMatrix added in https://codereview.chromium.org/203203004/
  107. // Doesn't seem to have ever been working correctly since added
  108. // lua.pushMatrix(matrix);
  109. break;
  110. }
  111. }
  112. this->INHERITED::didConcat(matrix);
  113. }
  114. void SkLuaCanvas::didSetMatrix(const SkMatrix& matrix) {
  115. this->INHERITED::didSetMatrix(matrix);
  116. }
  117. void SkLuaCanvas::onClipRect(const SkRect& r, SkClipOp op, ClipEdgeStyle edgeStyle) {
  118. AUTO_LUA("clipRect");
  119. lua.pushRect(r, "rect");
  120. lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
  121. this->INHERITED::onClipRect(r, op, edgeStyle);
  122. }
  123. void SkLuaCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
  124. AUTO_LUA("clipRRect");
  125. lua.pushRRect(rrect, "rrect");
  126. lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
  127. this->INHERITED::onClipRRect(rrect, op, edgeStyle);
  128. }
  129. void SkLuaCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
  130. AUTO_LUA("clipPath");
  131. lua.pushPath(path, "path");
  132. lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
  133. this->INHERITED::onClipPath(path, op, edgeStyle);
  134. }
  135. void SkLuaCanvas::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {
  136. AUTO_LUA("clipRegion");
  137. this->INHERITED::onClipRegion(deviceRgn, op);
  138. }
  139. void SkLuaCanvas::onDrawPaint(const SkPaint& paint) {
  140. AUTO_LUA("drawPaint");
  141. lua.pushPaint(paint, "paint");
  142. }
  143. void SkLuaCanvas::onDrawPoints(PointMode mode, size_t count,
  144. const SkPoint pts[], const SkPaint& paint) {
  145. AUTO_LUA("drawPoints");
  146. lua.pushArrayPoint(pts, SkToInt(count), "points");
  147. lua.pushPaint(paint, "paint");
  148. }
  149. void SkLuaCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
  150. AUTO_LUA("drawOval");
  151. lua.pushRect(rect, "rect");
  152. lua.pushPaint(paint, "paint");
  153. }
  154. void SkLuaCanvas::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
  155. bool useCenter, const SkPaint& paint) {
  156. AUTO_LUA("drawArc");
  157. lua.pushRect(rect, "rect");
  158. lua.pushScalar(startAngle, "startAngle");
  159. lua.pushScalar(sweepAngle, "sweepAngle");
  160. lua.pushBool(useCenter, "useCenter");
  161. lua.pushPaint(paint, "paint");
  162. }
  163. void SkLuaCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
  164. AUTO_LUA("drawRect");
  165. lua.pushRect(rect, "rect");
  166. lua.pushPaint(paint, "paint");
  167. }
  168. void SkLuaCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
  169. AUTO_LUA("drawRRect");
  170. lua.pushRRect(rrect, "rrect");
  171. lua.pushPaint(paint, "paint");
  172. }
  173. void SkLuaCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
  174. const SkPaint& paint) {
  175. AUTO_LUA("drawDRRect");
  176. lua.pushRRect(outer, "outer");
  177. lua.pushRRect(inner, "inner");
  178. lua.pushPaint(paint, "paint");
  179. }
  180. void SkLuaCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
  181. AUTO_LUA("drawPath");
  182. lua.pushPath(path, "path");
  183. lua.pushPaint(paint, "paint");
  184. }
  185. void SkLuaCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
  186. const SkPaint* paint) {
  187. AUTO_LUA("drawBitmap");
  188. if (paint) {
  189. lua.pushPaint(*paint, "paint");
  190. }
  191. }
  192. void SkLuaCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
  193. const SkPaint* paint, SrcRectConstraint) {
  194. AUTO_LUA("drawBitmapRect");
  195. if (paint) {
  196. lua.pushPaint(*paint, "paint");
  197. }
  198. }
  199. void SkLuaCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst,
  200. const SkPaint* paint) {
  201. AUTO_LUA("drawBitmapNine");
  202. if (paint) {
  203. lua.pushPaint(*paint, "paint");
  204. }
  205. }
  206. void SkLuaCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) {
  207. AUTO_LUA("drawImage");
  208. if (paint) {
  209. lua.pushPaint(*paint, "paint");
  210. }
  211. }
  212. void SkLuaCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
  213. const SkPaint* paint, SrcRectConstraint) {
  214. AUTO_LUA("drawImageRect");
  215. if (paint) {
  216. lua.pushPaint(*paint, "paint");
  217. }
  218. }
  219. void SkLuaCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y,
  220. const SkPaint &paint) {
  221. AUTO_LUA("drawTextBlob");
  222. lua.pushTextBlob(blob, "blob");
  223. lua.pushScalar(x, "x");
  224. lua.pushScalar(y, "y");
  225. lua.pushPaint(paint, "paint");
  226. }
  227. void SkLuaCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
  228. const SkPaint* paint) {
  229. AUTO_LUA("drawPicture");
  230. // call through so we can see the nested picture ops
  231. this->INHERITED::onDrawPicture(picture, matrix, paint);
  232. }
  233. void SkLuaCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
  234. AUTO_LUA("drawDrawable");
  235. // call through so we can see the nested ops
  236. this->INHERITED::onDrawDrawable(drawable, matrix);
  237. }
  238. void SkLuaCanvas::onDrawVerticesObject(const SkVertices*, const SkVertices::Bone[], int,
  239. SkBlendMode, const SkPaint& paint) {
  240. AUTO_LUA("drawVertices");
  241. lua.pushPaint(paint, "paint");
  242. }