blob: 7f279caf1c4bb72c96b8cc67a183ccbbfe4c4147 [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
Mike Reed97f3cc22018-12-03 09:45:17 -050040#ifdef SK_SUPPORT_LEGACY_TEXTENCODINGENUM
41 void pushEncodedText(SkPaint::TextEncoding enc, const void* text, size_t length) {
42 this->pushEncodedText((SkTextEncoding)enc, text, length);
43 }
44#endif
45 void pushEncodedText(SkTextEncoding, const void*, size_t);
reed@google.come3823fd2013-05-30 18:55:14 +000046
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000047private:
reed@google.com74ce6f02013-05-22 15:13:18 +000048 typedef SkLua INHERITED;
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000049};
50
reed@google.com74ce6f02013-05-22 15:13:18 +000051#define AUTO_LUA(verb) AutoCallLua lua(fL, fFunc.c_str(), verb)
reed@google.com9a731042013-05-21 17:52:33 +000052
reed@google.come3823fd2013-05-30 18:55:14 +000053
54///////////////////////////////////////////////////////////////////////////////
55
Mike Reed97f3cc22018-12-03 09:45:17 -050056void AutoCallLua::pushEncodedText(SkTextEncoding enc, const void* text, size_t length) {
reed@google.come3823fd2013-05-30 18:55:14 +000057 switch (enc) {
Mike Reed97f3cc22018-12-03 09:45:17 -050058 case kUTF8_SkTextEncoding:
reed@google.come3823fd2013-05-30 18:55:14 +000059 this->pushString((const char*)text, length, "text");
60 break;
Mike Reed97f3cc22018-12-03 09:45:17 -050061 case kUTF16_SkTextEncoding:
Hal Canaryee08b4a2018-03-01 15:56:37 -050062 this->pushString(SkStringFromUTF16((const uint16_t*)text, length), "text");
63 break;
Mike Reed97f3cc22018-12-03 09:45:17 -050064 case kGlyphID_SkTextEncoding:
reed@google.com7fa2a652014-01-27 13:42:58 +000065 this->pushArrayU16((const uint16_t*)text, SkToInt(length >> 1),
66 "glyphs");
reed@google.come3823fd2013-05-30 18:55:14 +000067 break;
Mike Reed97f3cc22018-12-03 09:45:17 -050068 case kUTF32_SkTextEncoding:
reed@google.come3823fd2013-05-30 18:55:14 +000069 break;
70 }
71}
72
reed@google.com9a731042013-05-21 17:52:33 +000073///////////////////////////////////////////////////////////////////////////////
74
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000075void SkLuaCanvas::pushThis() {
reed@google.com74ce6f02013-05-22 15:13:18 +000076 SkLua(fL).pushCanvas(this);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000077}
78
79///////////////////////////////////////////////////////////////////////////////
80
reed@google.comdff7e112013-05-15 19:34:20 +000081SkLuaCanvas::SkLuaCanvas(int width, int height, lua_State* L, const char func[])
commit-bot@chromium.orge2543102014-01-31 19:42:58 +000082 : INHERITED(width, height)
reed@google.comdff7e112013-05-15 19:34:20 +000083 , fL(L)
84 , fFunc(func) {
85}
86
87SkLuaCanvas::~SkLuaCanvas() {}
88
Florin Malita5f6102d2014-06-30 10:13:28 -040089void SkLuaCanvas::willSave() {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000090 AUTO_LUA("save");
Florin Malita5f6102d2014-06-30 10:13:28 -040091 this->INHERITED::willSave();
reed@google.comdff7e112013-05-15 19:34:20 +000092}
93
reed4960eee2015-12-18 07:09:18 -080094SkCanvas::SaveLayerStrategy SkLuaCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000095 AUTO_LUA("saveLayer");
reed4960eee2015-12-18 07:09:18 -080096 if (rec.fBounds) {
97 lua.pushRect(*rec.fBounds, "bounds");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000098 }
reed4960eee2015-12-18 07:09:18 -080099 if (rec.fPaint) {
100 lua.pushPaint(*rec.fPaint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000101 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000102
reed4960eee2015-12-18 07:09:18 -0800103 (void)this->INHERITED::getSaveLayerStrategy(rec);
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000104 // No need for a layer.
105 return kNoLayer_SaveLayerStrategy;
reed@google.comdff7e112013-05-15 19:34:20 +0000106}
107
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000108void SkLuaCanvas::willRestore() {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000109 AUTO_LUA("restore");
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000110 this->INHERITED::willRestore();
reed@google.comdff7e112013-05-15 19:34:20 +0000111}
112
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000113void SkLuaCanvas::didConcat(const SkMatrix& matrix) {
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000114 switch (matrix.getType()) {
115 case SkMatrix::kTranslate_Mask: {
116 AUTO_LUA("translate");
117 lua.pushScalar(matrix.getTranslateX(), "dx");
118 lua.pushScalar(matrix.getTranslateY(), "dy");
119 break;
120 }
121 case SkMatrix::kScale_Mask: {
122 AUTO_LUA("scale");
123 lua.pushScalar(matrix.getScaleX(), "sx");
124 lua.pushScalar(matrix.getScaleY(), "sy");
125 break;
126 }
127 default: {
128 AUTO_LUA("concat");
commit-bot@chromium.orgab885be2014-05-13 20:32:32 +0000129 // pushMatrix added in https://codereview.chromium.org/203203004/
130 // Doesn't seem to have ever been working correctly since added
131 // lua.pushMatrix(matrix);
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000132 break;
133 }
134 }
135
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000136 this->INHERITED::didConcat(matrix);
reed@google.comdff7e112013-05-15 19:34:20 +0000137}
138
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000139void SkLuaCanvas::didSetMatrix(const SkMatrix& matrix) {
140 this->INHERITED::didSetMatrix(matrix);
reed@google.comdff7e112013-05-15 19:34:20 +0000141}
142
Mike Reedc1f77742016-12-09 09:00:50 -0500143void SkLuaCanvas::onClipRect(const SkRect& r, SkClipOp op, ClipEdgeStyle edgeStyle) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000144 AUTO_LUA("clipRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000145 lua.pushRect(r, "rect");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000146 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
147 this->INHERITED::onClipRect(r, op, edgeStyle);
reed@google.comdff7e112013-05-15 19:34:20 +0000148}
149
Mike Reedc1f77742016-12-09 09:00:50 -0500150void SkLuaCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000151 AUTO_LUA("clipRRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000152 lua.pushRRect(rrect, "rrect");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000153 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
154 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
reed@google.comdff7e112013-05-15 19:34:20 +0000155}
156
Mike Reedc1f77742016-12-09 09:00:50 -0500157void SkLuaCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000158 AUTO_LUA("clipPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000159 lua.pushPath(path, "path");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000160 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
161 this->INHERITED::onClipPath(path, op, edgeStyle);
reed@google.comdff7e112013-05-15 19:34:20 +0000162}
163
Mike Reedc1f77742016-12-09 09:00:50 -0500164void SkLuaCanvas::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000165 AUTO_LUA("clipRegion");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000166 this->INHERITED::onClipRegion(deviceRgn, op);
reed@google.comdff7e112013-05-15 19:34:20 +0000167}
168
reed41af9662015-01-05 07:49:08 -0800169void SkLuaCanvas::onDrawPaint(const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000170 AUTO_LUA("drawPaint");
reed@google.com74ce6f02013-05-22 15:13:18 +0000171 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000172}
173
reed41af9662015-01-05 07:49:08 -0800174void SkLuaCanvas::onDrawPoints(PointMode mode, size_t count,
reed@google.comdff7e112013-05-15 19:34:20 +0000175 const SkPoint pts[], const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000176 AUTO_LUA("drawPoints");
commit-bot@chromium.org2cfa3202014-04-19 22:00:40 +0000177 lua.pushArrayPoint(pts, SkToInt(count), "points");
reed@google.com74ce6f02013-05-22 15:13:18 +0000178 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000179}
180
reed41af9662015-01-05 07:49:08 -0800181void SkLuaCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000182 AUTO_LUA("drawOval");
reed@google.com74ce6f02013-05-22 15:13:18 +0000183 lua.pushRect(rect, "rect");
184 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000185}
186
bsalomonac3aa242016-08-19 11:25:19 -0700187void SkLuaCanvas::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
188 bool useCenter, const SkPaint& paint) {
189 AUTO_LUA("drawArc");
190 lua.pushRect(rect, "rect");
191 lua.pushScalar(startAngle, "startAngle");
192 lua.pushScalar(sweepAngle, "sweepAngle");
193 lua.pushBool(useCenter, "useCenter");
194 lua.pushPaint(paint, "paint");
195}
196
reed41af9662015-01-05 07:49:08 -0800197void SkLuaCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000198 AUTO_LUA("drawRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000199 lua.pushRect(rect, "rect");
200 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000201}
202
reed41af9662015-01-05 07:49:08 -0800203void SkLuaCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000204 AUTO_LUA("drawRRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000205 lua.pushRRect(rrect, "rrect");
206 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000207}
208
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000209void SkLuaCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
210 const SkPaint& paint) {
211 AUTO_LUA("drawDRRect");
212 lua.pushRRect(outer, "outer");
213 lua.pushRRect(inner, "inner");
214 lua.pushPaint(paint, "paint");
215}
216
reed41af9662015-01-05 07:49:08 -0800217void SkLuaCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000218 AUTO_LUA("drawPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000219 lua.pushPath(path, "path");
220 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000221}
222
reed41af9662015-01-05 07:49:08 -0800223void SkLuaCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
224 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000225 AUTO_LUA("drawBitmap");
226 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000227 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000228 }
reed@google.comdff7e112013-05-15 19:34:20 +0000229}
230
reed41af9662015-01-05 07:49:08 -0800231void SkLuaCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700232 const SkPaint* paint, SrcRectConstraint) {
reed41af9662015-01-05 07:49:08 -0800233 AUTO_LUA("drawBitmapRect");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000234 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000235 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000236 }
reed@google.comdff7e112013-05-15 19:34:20 +0000237}
238
reed41af9662015-01-05 07:49:08 -0800239void SkLuaCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst,
240 const SkPaint* paint) {
241 AUTO_LUA("drawBitmapNine");
242 if (paint) {
243 lua.pushPaint(*paint, "paint");
244 }
245}
246
247void SkLuaCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) {
248 AUTO_LUA("drawImage");
249 if (paint) {
250 lua.pushPaint(*paint, "paint");
251 }
252}
253
254void SkLuaCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700255 const SkPaint* paint, SrcRectConstraint) {
reed41af9662015-01-05 07:49:08 -0800256 AUTO_LUA("drawImageRect");
257 if (paint) {
258 lua.pushPaint(*paint, "paint");
259 }
260}
261
reed@google.come0d9ce82014-04-23 04:00:17 +0000262void SkLuaCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
263 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000264 AUTO_LUA("drawText");
reed@google.come3823fd2013-05-30 18:55:14 +0000265 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000266 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000267}
268
reed@google.come0d9ce82014-04-23 04:00:17 +0000269void SkLuaCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
270 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000271 AUTO_LUA("drawPosText");
reed@google.come3823fd2013-05-30 18:55:14 +0000272 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000273 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000274}
275
reed@google.come0d9ce82014-04-23 04:00:17 +0000276void SkLuaCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
277 SkScalar constY, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000278 AUTO_LUA("drawPosTextH");
reed@google.come3823fd2013-05-30 18:55:14 +0000279 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000280 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000281}
282
reed45561a02016-07-07 12:47:17 -0700283void SkLuaCanvas::onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
284 const SkRect* cull, const SkPaint& paint) {
285 AUTO_LUA("drawTextRSXform");
286 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
287 // TODO: export other params
288 lua.pushPaint(paint, "paint");
289}
290
fmalitab7425172014-08-26 07:56:44 -0700291void SkLuaCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y,
292 const SkPaint &paint) {
293 AUTO_LUA("drawTextBlob");
294 lua.pushTextBlob(blob, "blob");
295 lua.pushScalar(x, "x");
296 lua.pushScalar(y, "y");
297 lua.pushPaint(paint, "paint");
298}
299
reedd5fa1a42014-08-09 11:08:05 -0700300void SkLuaCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
301 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000302 AUTO_LUA("drawPicture");
reed@google.comdff7e112013-05-15 19:34:20 +0000303 // call through so we can see the nested picture ops
reedd5fa1a42014-08-09 11:08:05 -0700304 this->INHERITED::onDrawPicture(picture, matrix, paint);
reed@google.comdff7e112013-05-15 19:34:20 +0000305}
306
Florin Malitaadaeaed2017-09-21 15:28:29 -0400307void SkLuaCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
308 AUTO_LUA("drawDrawable");
309 // call through so we can see the nested ops
310 this->INHERITED::onDrawDrawable(drawable, matrix);
311}
312
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400313void SkLuaCanvas::onDrawVerticesObject(const SkVertices*, const SkVertices::Bone[], int,
314 SkBlendMode, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000315 AUTO_LUA("drawVertices");
reed@google.com74ce6f02013-05-22 15:13:18 +0000316 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000317}