blob: 486611b6de27e773bd608723e665289b7aaa9c95 [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 void pushEncodedText(SkTextEncoding, const void*, size_t);
reed@google.come3823fd2013-05-30 18:55:14 +000041
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
Mike Reed97f3cc22018-12-03 09:45:17 -050051void AutoCallLua::pushEncodedText(SkTextEncoding enc, const void* text, size_t length) {
reed@google.come3823fd2013-05-30 18:55:14 +000052 switch (enc) {
Mike Reed97f3cc22018-12-03 09:45:17 -050053 case kUTF8_SkTextEncoding:
reed@google.come3823fd2013-05-30 18:55:14 +000054 this->pushString((const char*)text, length, "text");
55 break;
Mike Reed97f3cc22018-12-03 09:45:17 -050056 case kUTF16_SkTextEncoding:
Hal Canaryee08b4a2018-03-01 15:56:37 -050057 this->pushString(SkStringFromUTF16((const uint16_t*)text, length), "text");
58 break;
Mike Reed97f3cc22018-12-03 09:45:17 -050059 case kGlyphID_SkTextEncoding:
reed@google.com7fa2a652014-01-27 13:42:58 +000060 this->pushArrayU16((const uint16_t*)text, SkToInt(length >> 1),
61 "glyphs");
reed@google.come3823fd2013-05-30 18:55:14 +000062 break;
Mike Reed97f3cc22018-12-03 09:45:17 -050063 case kUTF32_SkTextEncoding:
reed@google.come3823fd2013-05-30 18:55:14 +000064 break;
65 }
66}
67
reed@google.com9a731042013-05-21 17:52:33 +000068///////////////////////////////////////////////////////////////////////////////
69
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000070void SkLuaCanvas::pushThis() {
reed@google.com74ce6f02013-05-22 15:13:18 +000071 SkLua(fL).pushCanvas(this);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000072}
73
74///////////////////////////////////////////////////////////////////////////////
75
reed@google.comdff7e112013-05-15 19:34:20 +000076SkLuaCanvas::SkLuaCanvas(int width, int height, lua_State* L, const char func[])
commit-bot@chromium.orge2543102014-01-31 19:42:58 +000077 : INHERITED(width, height)
reed@google.comdff7e112013-05-15 19:34:20 +000078 , fL(L)
79 , fFunc(func) {
80}
81
82SkLuaCanvas::~SkLuaCanvas() {}
83
Florin Malita5f6102d2014-06-30 10:13:28 -040084void SkLuaCanvas::willSave() {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000085 AUTO_LUA("save");
Florin Malita5f6102d2014-06-30 10:13:28 -040086 this->INHERITED::willSave();
reed@google.comdff7e112013-05-15 19:34:20 +000087}
88
reed4960eee2015-12-18 07:09:18 -080089SkCanvas::SaveLayerStrategy SkLuaCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000090 AUTO_LUA("saveLayer");
reed4960eee2015-12-18 07:09:18 -080091 if (rec.fBounds) {
92 lua.pushRect(*rec.fBounds, "bounds");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000093 }
reed4960eee2015-12-18 07:09:18 -080094 if (rec.fPaint) {
95 lua.pushPaint(*rec.fPaint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000096 }
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +000097
reed4960eee2015-12-18 07:09:18 -080098 (void)this->INHERITED::getSaveLayerStrategy(rec);
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +000099 // No need for a layer.
100 return kNoLayer_SaveLayerStrategy;
reed@google.comdff7e112013-05-15 19:34:20 +0000101}
102
Mike Reed148b7fd2018-12-18 17:38:18 -0500103bool SkLuaCanvas::onDoSaveBehind(const SkRect*) {
104 // TODO
105 return false;
106}
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
fmalitab7425172014-08-26 07:56:44 -0700262void SkLuaCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y,
263 const SkPaint &paint) {
264 AUTO_LUA("drawTextBlob");
265 lua.pushTextBlob(blob, "blob");
266 lua.pushScalar(x, "x");
267 lua.pushScalar(y, "y");
268 lua.pushPaint(paint, "paint");
269}
270
reedd5fa1a42014-08-09 11:08:05 -0700271void SkLuaCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
272 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000273 AUTO_LUA("drawPicture");
reed@google.comdff7e112013-05-15 19:34:20 +0000274 // call through so we can see the nested picture ops
reedd5fa1a42014-08-09 11:08:05 -0700275 this->INHERITED::onDrawPicture(picture, matrix, paint);
reed@google.comdff7e112013-05-15 19:34:20 +0000276}
277
Florin Malitaadaeaed2017-09-21 15:28:29 -0400278void SkLuaCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
279 AUTO_LUA("drawDrawable");
280 // call through so we can see the nested ops
281 this->INHERITED::onDrawDrawable(drawable, matrix);
282}
283
Ruiqi Maoc97a3392018-08-15 10:44:19 -0400284void SkLuaCanvas::onDrawVerticesObject(const SkVertices*, const SkVertices::Bone[], int,
285 SkBlendMode, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000286 AUTO_LUA("drawVertices");
reed@google.com74ce6f02013-05-22 15:13:18 +0000287 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000288}