blob: 441857f664c147a3eb02db75d44871e878d964ff [file] [log] [blame]
reed@google.comdff7e112013-05-15 19:34:20 +00001/*
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
8#include "SkLuaCanvas.h"
Hal Canaryee08b4a2018-03-01 15:56:37 -05009
reed@google.com74ce6f02013-05-22 15:13:18 +000010#include "SkLua.h"
Hal Canaryee08b4a2018-03-01 15:56:37 -050011#include "SkStringUtils.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040012#include "SkTo.h"
reed@google.comdff7e112013-05-15 19:34:20 +000013
14extern "C" {
15 #include "lua.h"
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000016 #include "lauxlib.h"
reed@google.comdff7e112013-05-15 19:34:20 +000017}
18
reed@google.com74ce6f02013-05-22 15:13:18 +000019class AutoCallLua : public SkLua {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000020public:
reed@google.com74ce6f02013-05-22 15:13:18 +000021 AutoCallLua(lua_State* L, const char func[], const char verb[]) : INHERITED(L) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000022 lua_getglobal(L, func);
23 if (!lua_isfunction(L, -1)) {
24 int t = lua_type(L, -1);
25 SkDebugf("--- expected function %d\n", t);
26 }
skia.committer@gmail.com539f3642013-05-16 07:01:00 +000027
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000028 lua_newtable(L);
reed@google.com74ce6f02013-05-22 15:13:18 +000029 this->pushString(verb, "verb");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000030 }
31
32 ~AutoCallLua() {
reed@google.com3597b732013-05-22 20:12:50 +000033 lua_State* L = this->get();
reed@google.com74ce6f02013-05-22 15:13:18 +000034 if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
35 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000036 }
reed@google.com74ce6f02013-05-22 15:13:18 +000037 lua_settop(L, -1);
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000038 }
39
reed@google.come3823fd2013-05-30 18:55:14 +000040 void pushEncodedText(SkPaint::TextEncoding, const void*, size_t);
41
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000042private:
reed@google.com74ce6f02013-05-22 15:13:18 +000043 typedef SkLua INHERITED;
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000044};
45
reed@google.com74ce6f02013-05-22 15:13:18 +000046#define AUTO_LUA(verb) AutoCallLua lua(fL, fFunc.c_str(), verb)
reed@google.com9a731042013-05-21 17:52:33 +000047
reed@google.come3823fd2013-05-30 18:55:14 +000048
49///////////////////////////////////////////////////////////////////////////////
50
51void AutoCallLua::pushEncodedText(SkPaint::TextEncoding enc, const void* text,
52 size_t length) {
53 switch (enc) {
54 case SkPaint::kUTF8_TextEncoding:
55 this->pushString((const char*)text, length, "text");
56 break;
Hal Canaryee08b4a2018-03-01 15:56:37 -050057 case SkPaint::kUTF16_TextEncoding:
58 this->pushString(SkStringFromUTF16((const uint16_t*)text, length), "text");
59 break;
reed@google.come3823fd2013-05-30 18:55:14 +000060 case SkPaint::kGlyphID_TextEncoding:
reed@google.com7fa2a652014-01-27 13:42:58 +000061 this->pushArrayU16((const uint16_t*)text, SkToInt(length >> 1),
62 "glyphs");
reed@google.come3823fd2013-05-30 18:55:14 +000063 break;
64 case SkPaint::kUTF32_TextEncoding:
65 break;
66 }
67}
68
reed@google.com9a731042013-05-21 17:52:33 +000069///////////////////////////////////////////////////////////////////////////////
70
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000071void SkLuaCanvas::pushThis() {
reed@google.com74ce6f02013-05-22 15:13:18 +000072 SkLua(fL).pushCanvas(this);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000073}
74
75///////////////////////////////////////////////////////////////////////////////
76
reed@google.comdff7e112013-05-15 19:34:20 +000077SkLuaCanvas::SkLuaCanvas(int width, int height, lua_State* L, const char func[])
commit-bot@chromium.orge2543102014-01-31 19:42:58 +000078 : INHERITED(width, height)
reed@google.comdff7e112013-05-15 19:34:20 +000079 , fL(L)
80 , fFunc(func) {
81}
82
83SkLuaCanvas::~SkLuaCanvas() {}
84
Florin Malita5f6102d2014-06-30 10:13:28 -040085void SkLuaCanvas::willSave() {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000086 AUTO_LUA("save");
Florin Malita5f6102d2014-06-30 10:13:28 -040087 this->INHERITED::willSave();
reed@google.comdff7e112013-05-15 19:34:20 +000088}
89
reed4960eee2015-12-18 07:09:18 -080090SkCanvas::SaveLayerStrategy SkLuaCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000091 AUTO_LUA("saveLayer");
reed4960eee2015-12-18 07:09:18 -080092 if (rec.fBounds) {
93 lua.pushRect(*rec.fBounds, "bounds");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000094 }
reed4960eee2015-12-18 07:09:18 -080095 if (rec.fPaint) {
96 lua.pushPaint(*rec.fPaint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000097 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +000098
reed4960eee2015-12-18 07:09:18 -080099 (void)this->INHERITED::getSaveLayerStrategy(rec);
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000100 // No need for a layer.
101 return kNoLayer_SaveLayerStrategy;
reed@google.comdff7e112013-05-15 19:34:20 +0000102}
103
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000104void SkLuaCanvas::willRestore() {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000105 AUTO_LUA("restore");
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000106 this->INHERITED::willRestore();
reed@google.comdff7e112013-05-15 19:34:20 +0000107}
108
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000109void SkLuaCanvas::didConcat(const SkMatrix& matrix) {
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000110 switch (matrix.getType()) {
111 case SkMatrix::kTranslate_Mask: {
112 AUTO_LUA("translate");
113 lua.pushScalar(matrix.getTranslateX(), "dx");
114 lua.pushScalar(matrix.getTranslateY(), "dy");
115 break;
116 }
117 case SkMatrix::kScale_Mask: {
118 AUTO_LUA("scale");
119 lua.pushScalar(matrix.getScaleX(), "sx");
120 lua.pushScalar(matrix.getScaleY(), "sy");
121 break;
122 }
123 default: {
124 AUTO_LUA("concat");
commit-bot@chromium.orgab885be2014-05-13 20:32:32 +0000125 // pushMatrix added in https://codereview.chromium.org/203203004/
126 // Doesn't seem to have ever been working correctly since added
127 // lua.pushMatrix(matrix);
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000128 break;
129 }
130 }
131
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000132 this->INHERITED::didConcat(matrix);
reed@google.comdff7e112013-05-15 19:34:20 +0000133}
134
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000135void SkLuaCanvas::didSetMatrix(const SkMatrix& matrix) {
136 this->INHERITED::didSetMatrix(matrix);
reed@google.comdff7e112013-05-15 19:34:20 +0000137}
138
Mike Reedc1f77742016-12-09 09:00:50 -0500139void SkLuaCanvas::onClipRect(const SkRect& r, SkClipOp op, ClipEdgeStyle edgeStyle) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000140 AUTO_LUA("clipRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000141 lua.pushRect(r, "rect");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000142 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
143 this->INHERITED::onClipRect(r, op, edgeStyle);
reed@google.comdff7e112013-05-15 19:34:20 +0000144}
145
Mike Reedc1f77742016-12-09 09:00:50 -0500146void SkLuaCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000147 AUTO_LUA("clipRRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000148 lua.pushRRect(rrect, "rrect");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000149 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
150 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
reed@google.comdff7e112013-05-15 19:34:20 +0000151}
152
Mike Reedc1f77742016-12-09 09:00:50 -0500153void SkLuaCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000154 AUTO_LUA("clipPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000155 lua.pushPath(path, "path");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000156 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
157 this->INHERITED::onClipPath(path, op, edgeStyle);
reed@google.comdff7e112013-05-15 19:34:20 +0000158}
159
Mike Reedc1f77742016-12-09 09:00:50 -0500160void SkLuaCanvas::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000161 AUTO_LUA("clipRegion");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000162 this->INHERITED::onClipRegion(deviceRgn, op);
reed@google.comdff7e112013-05-15 19:34:20 +0000163}
164
reed41af9662015-01-05 07:49:08 -0800165void SkLuaCanvas::onDrawPaint(const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000166 AUTO_LUA("drawPaint");
reed@google.com74ce6f02013-05-22 15:13:18 +0000167 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000168}
169
reed41af9662015-01-05 07:49:08 -0800170void SkLuaCanvas::onDrawPoints(PointMode mode, size_t count,
reed@google.comdff7e112013-05-15 19:34:20 +0000171 const SkPoint pts[], const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000172 AUTO_LUA("drawPoints");
commit-bot@chromium.org2cfa3202014-04-19 22:00:40 +0000173 lua.pushArrayPoint(pts, SkToInt(count), "points");
reed@google.com74ce6f02013-05-22 15:13:18 +0000174 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000175}
176
reed41af9662015-01-05 07:49:08 -0800177void SkLuaCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000178 AUTO_LUA("drawOval");
reed@google.com74ce6f02013-05-22 15:13:18 +0000179 lua.pushRect(rect, "rect");
180 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000181}
182
bsalomonac3aa242016-08-19 11:25:19 -0700183void SkLuaCanvas::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
184 bool useCenter, const SkPaint& paint) {
185 AUTO_LUA("drawArc");
186 lua.pushRect(rect, "rect");
187 lua.pushScalar(startAngle, "startAngle");
188 lua.pushScalar(sweepAngle, "sweepAngle");
189 lua.pushBool(useCenter, "useCenter");
190 lua.pushPaint(paint, "paint");
191}
192
reed41af9662015-01-05 07:49:08 -0800193void SkLuaCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000194 AUTO_LUA("drawRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000195 lua.pushRect(rect, "rect");
196 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000197}
198
reed41af9662015-01-05 07:49:08 -0800199void SkLuaCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000200 AUTO_LUA("drawRRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000201 lua.pushRRect(rrect, "rrect");
202 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000203}
204
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000205void SkLuaCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
206 const SkPaint& paint) {
207 AUTO_LUA("drawDRRect");
208 lua.pushRRect(outer, "outer");
209 lua.pushRRect(inner, "inner");
210 lua.pushPaint(paint, "paint");
211}
212
reed41af9662015-01-05 07:49:08 -0800213void SkLuaCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000214 AUTO_LUA("drawPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000215 lua.pushPath(path, "path");
216 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000217}
218
reed41af9662015-01-05 07:49:08 -0800219void SkLuaCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
220 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000221 AUTO_LUA("drawBitmap");
222 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000223 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000224 }
reed@google.comdff7e112013-05-15 19:34:20 +0000225}
226
reed41af9662015-01-05 07:49:08 -0800227void SkLuaCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700228 const SkPaint* paint, SrcRectConstraint) {
reed41af9662015-01-05 07:49:08 -0800229 AUTO_LUA("drawBitmapRect");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000230 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000231 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000232 }
reed@google.comdff7e112013-05-15 19:34:20 +0000233}
234
reed41af9662015-01-05 07:49:08 -0800235void SkLuaCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst,
236 const SkPaint* paint) {
237 AUTO_LUA("drawBitmapNine");
238 if (paint) {
239 lua.pushPaint(*paint, "paint");
240 }
241}
242
243void SkLuaCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) {
244 AUTO_LUA("drawImage");
245 if (paint) {
246 lua.pushPaint(*paint, "paint");
247 }
248}
249
250void SkLuaCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700251 const SkPaint* paint, SrcRectConstraint) {
reed41af9662015-01-05 07:49:08 -0800252 AUTO_LUA("drawImageRect");
253 if (paint) {
254 lua.pushPaint(*paint, "paint");
255 }
256}
257
reed@google.come0d9ce82014-04-23 04:00:17 +0000258void SkLuaCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
259 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000260 AUTO_LUA("drawText");
reed@google.come3823fd2013-05-30 18:55:14 +0000261 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000262 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000263}
264
reed@google.come0d9ce82014-04-23 04:00:17 +0000265void SkLuaCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
266 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000267 AUTO_LUA("drawPosText");
reed@google.come3823fd2013-05-30 18:55:14 +0000268 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000269 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000270}
271
reed@google.come0d9ce82014-04-23 04:00:17 +0000272void SkLuaCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
273 SkScalar constY, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000274 AUTO_LUA("drawPosTextH");
reed@google.come3823fd2013-05-30 18:55:14 +0000275 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000276 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000277}
278
reed@google.come0d9ce82014-04-23 04:00:17 +0000279void SkLuaCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
280 const SkMatrix* matrix, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000281 AUTO_LUA("drawTextOnPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000282 lua.pushPath(path, "path");
reed@google.come3823fd2013-05-30 18:55:14 +0000283 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000284 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000285}
286
reed45561a02016-07-07 12:47:17 -0700287void SkLuaCanvas::onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
288 const SkRect* cull, const SkPaint& paint) {
289 AUTO_LUA("drawTextRSXform");
290 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
291 // TODO: export other params
292 lua.pushPaint(paint, "paint");
293}
294
fmalitab7425172014-08-26 07:56:44 -0700295void SkLuaCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y,
296 const SkPaint &paint) {
297 AUTO_LUA("drawTextBlob");
298 lua.pushTextBlob(blob, "blob");
299 lua.pushScalar(x, "x");
300 lua.pushScalar(y, "y");
301 lua.pushPaint(paint, "paint");
302}
303
reedd5fa1a42014-08-09 11:08:05 -0700304void SkLuaCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
305 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000306 AUTO_LUA("drawPicture");
reed@google.comdff7e112013-05-15 19:34:20 +0000307 // call through so we can see the nested picture ops
reedd5fa1a42014-08-09 11:08:05 -0700308 this->INHERITED::onDrawPicture(picture, matrix, paint);
reed@google.comdff7e112013-05-15 19:34:20 +0000309}
310
Florin Malitaadaeaed2017-09-21 15:28:29 -0400311void SkLuaCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
312 AUTO_LUA("drawDrawable");
313 // call through so we can see the nested ops
314 this->INHERITED::onDrawDrawable(drawable, matrix);
315}
316
Ruiqi Maof5101492018-06-29 14:32:21 -0400317void SkLuaCanvas::onDrawVerticesObject(const SkVertices*, const SkMatrix*, int, SkBlendMode,
318 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000319 AUTO_LUA("drawVertices");
reed@google.com74ce6f02013-05-22 15:13:18 +0000320 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000321}