blob: 3883924c0dc0572c64f585ba70413486c4f57d19 [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"
reed@google.com74ce6f02013-05-22 15:13:18 +00009#include "SkLua.h"
reed@google.comdff7e112013-05-15 19:34:20 +000010
11extern "C" {
12 #include "lua.h"
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000013 #include "lauxlib.h"
reed@google.comdff7e112013-05-15 19:34:20 +000014}
15
reed@google.com74ce6f02013-05-22 15:13:18 +000016class AutoCallLua : public SkLua {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000017public:
reed@google.com74ce6f02013-05-22 15:13:18 +000018 AutoCallLua(lua_State* L, const char func[], const char verb[]) : INHERITED(L) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000019 lua_getglobal(L, func);
20 if (!lua_isfunction(L, -1)) {
21 int t = lua_type(L, -1);
22 SkDebugf("--- expected function %d\n", t);
23 }
skia.committer@gmail.com539f3642013-05-16 07:01:00 +000024
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000025 lua_newtable(L);
reed@google.com74ce6f02013-05-22 15:13:18 +000026 this->pushString(verb, "verb");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000027 }
28
29 ~AutoCallLua() {
reed@google.com3597b732013-05-22 20:12:50 +000030 lua_State* L = this->get();
reed@google.com74ce6f02013-05-22 15:13:18 +000031 if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
32 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000033 }
reed@google.com74ce6f02013-05-22 15:13:18 +000034 lua_settop(L, -1);
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000035 }
36
reed@google.come3823fd2013-05-30 18:55:14 +000037 void pushEncodedText(SkPaint::TextEncoding, const void*, size_t);
38
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000039private:
reed@google.com74ce6f02013-05-22 15:13:18 +000040 typedef SkLua INHERITED;
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000041};
42
reed@google.com74ce6f02013-05-22 15:13:18 +000043#define AUTO_LUA(verb) AutoCallLua lua(fL, fFunc.c_str(), verb)
reed@google.com9a731042013-05-21 17:52:33 +000044
reed@google.come3823fd2013-05-30 18:55:14 +000045
46///////////////////////////////////////////////////////////////////////////////
47
48void AutoCallLua::pushEncodedText(SkPaint::TextEncoding enc, const void* text,
49 size_t length) {
50 switch (enc) {
51 case SkPaint::kUTF8_TextEncoding:
52 this->pushString((const char*)text, length, "text");
53 break;
54 case SkPaint::kUTF16_TextEncoding: {
55 SkString str;
56 str.setUTF16((const uint16_t*)text, length);
57 this->pushString(str, "text");
58 } break;
59 case SkPaint::kGlyphID_TextEncoding:
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;
63 case SkPaint::kUTF32_TextEncoding:
64 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
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000103void SkLuaCanvas::willRestore() {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000104 AUTO_LUA("restore");
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000105 this->INHERITED::willRestore();
reed@google.comdff7e112013-05-15 19:34:20 +0000106}
107
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000108void SkLuaCanvas::didConcat(const SkMatrix& matrix) {
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000109 switch (matrix.getType()) {
110 case SkMatrix::kTranslate_Mask: {
111 AUTO_LUA("translate");
112 lua.pushScalar(matrix.getTranslateX(), "dx");
113 lua.pushScalar(matrix.getTranslateY(), "dy");
114 break;
115 }
116 case SkMatrix::kScale_Mask: {
117 AUTO_LUA("scale");
118 lua.pushScalar(matrix.getScaleX(), "sx");
119 lua.pushScalar(matrix.getScaleY(), "sy");
120 break;
121 }
122 default: {
123 AUTO_LUA("concat");
commit-bot@chromium.orgab885be2014-05-13 20:32:32 +0000124 // pushMatrix added in https://codereview.chromium.org/203203004/
125 // Doesn't seem to have ever been working correctly since added
126 // lua.pushMatrix(matrix);
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000127 break;
128 }
129 }
130
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000131 this->INHERITED::didConcat(matrix);
reed@google.comdff7e112013-05-15 19:34:20 +0000132}
133
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000134void SkLuaCanvas::didSetMatrix(const SkMatrix& matrix) {
135 this->INHERITED::didSetMatrix(matrix);
reed@google.comdff7e112013-05-15 19:34:20 +0000136}
137
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000138void SkLuaCanvas::onClipRect(const SkRect& r, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000139 AUTO_LUA("clipRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000140 lua.pushRect(r, "rect");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000141 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
142 this->INHERITED::onClipRect(r, op, edgeStyle);
reed@google.comdff7e112013-05-15 19:34:20 +0000143}
144
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000145void SkLuaCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000146 AUTO_LUA("clipRRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000147 lua.pushRRect(rrect, "rrect");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000148 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
149 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
reed@google.comdff7e112013-05-15 19:34:20 +0000150}
151
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000152void SkLuaCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000153 AUTO_LUA("clipPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000154 lua.pushPath(path, "path");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000155 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
156 this->INHERITED::onClipPath(path, op, edgeStyle);
reed@google.comdff7e112013-05-15 19:34:20 +0000157}
158
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000159void SkLuaCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000160 AUTO_LUA("clipRegion");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000161 this->INHERITED::onClipRegion(deviceRgn, op);
reed@google.comdff7e112013-05-15 19:34:20 +0000162}
163
reed41af9662015-01-05 07:49:08 -0800164void SkLuaCanvas::onDrawPaint(const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000165 AUTO_LUA("drawPaint");
reed@google.com74ce6f02013-05-22 15:13:18 +0000166 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000167}
168
reed41af9662015-01-05 07:49:08 -0800169void SkLuaCanvas::onDrawPoints(PointMode mode, size_t count,
reed@google.comdff7e112013-05-15 19:34:20 +0000170 const SkPoint pts[], const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000171 AUTO_LUA("drawPoints");
commit-bot@chromium.org2cfa3202014-04-19 22:00:40 +0000172 lua.pushArrayPoint(pts, SkToInt(count), "points");
reed@google.com74ce6f02013-05-22 15:13:18 +0000173 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000174}
175
reed41af9662015-01-05 07:49:08 -0800176void SkLuaCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000177 AUTO_LUA("drawOval");
reed@google.com74ce6f02013-05-22 15:13:18 +0000178 lua.pushRect(rect, "rect");
179 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000180}
181
bsalomonac3aa242016-08-19 11:25:19 -0700182void SkLuaCanvas::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
183 bool useCenter, const SkPaint& paint) {
184 AUTO_LUA("drawArc");
185 lua.pushRect(rect, "rect");
186 lua.pushScalar(startAngle, "startAngle");
187 lua.pushScalar(sweepAngle, "sweepAngle");
188 lua.pushBool(useCenter, "useCenter");
189 lua.pushPaint(paint, "paint");
190}
191
reed41af9662015-01-05 07:49:08 -0800192void SkLuaCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000193 AUTO_LUA("drawRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000194 lua.pushRect(rect, "rect");
195 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000196}
197
reed41af9662015-01-05 07:49:08 -0800198void SkLuaCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000199 AUTO_LUA("drawRRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000200 lua.pushRRect(rrect, "rrect");
201 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000202}
203
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000204void SkLuaCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
205 const SkPaint& paint) {
206 AUTO_LUA("drawDRRect");
207 lua.pushRRect(outer, "outer");
208 lua.pushRRect(inner, "inner");
209 lua.pushPaint(paint, "paint");
210}
211
reed41af9662015-01-05 07:49:08 -0800212void SkLuaCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000213 AUTO_LUA("drawPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000214 lua.pushPath(path, "path");
215 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000216}
217
reed41af9662015-01-05 07:49:08 -0800218void SkLuaCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
219 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000220 AUTO_LUA("drawBitmap");
221 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000222 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000223 }
reed@google.comdff7e112013-05-15 19:34:20 +0000224}
225
reed41af9662015-01-05 07:49:08 -0800226void SkLuaCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700227 const SkPaint* paint, SrcRectConstraint) {
reed41af9662015-01-05 07:49:08 -0800228 AUTO_LUA("drawBitmapRect");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000229 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000230 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000231 }
reed@google.comdff7e112013-05-15 19:34:20 +0000232}
233
reed41af9662015-01-05 07:49:08 -0800234void SkLuaCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst,
235 const SkPaint* paint) {
236 AUTO_LUA("drawBitmapNine");
237 if (paint) {
238 lua.pushPaint(*paint, "paint");
239 }
240}
241
242void SkLuaCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) {
243 AUTO_LUA("drawImage");
244 if (paint) {
245 lua.pushPaint(*paint, "paint");
246 }
247}
248
249void SkLuaCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
reed562fe472015-07-28 07:35:14 -0700250 const SkPaint* paint, SrcRectConstraint) {
reed41af9662015-01-05 07:49:08 -0800251 AUTO_LUA("drawImageRect");
252 if (paint) {
253 lua.pushPaint(*paint, "paint");
254 }
255}
256
reed@google.come0d9ce82014-04-23 04:00:17 +0000257void SkLuaCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
258 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000259 AUTO_LUA("drawText");
reed@google.come3823fd2013-05-30 18:55:14 +0000260 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000261 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000262}
263
reed@google.come0d9ce82014-04-23 04:00:17 +0000264void SkLuaCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
265 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000266 AUTO_LUA("drawPosText");
reed@google.come3823fd2013-05-30 18:55:14 +0000267 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000268 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000269}
270
reed@google.come0d9ce82014-04-23 04:00:17 +0000271void SkLuaCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
272 SkScalar constY, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000273 AUTO_LUA("drawPosTextH");
reed@google.come3823fd2013-05-30 18:55:14 +0000274 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000275 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000276}
277
reed@google.come0d9ce82014-04-23 04:00:17 +0000278void SkLuaCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
279 const SkMatrix* matrix, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000280 AUTO_LUA("drawTextOnPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000281 lua.pushPath(path, "path");
reed@google.come3823fd2013-05-30 18:55:14 +0000282 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000283 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000284}
285
reed45561a02016-07-07 12:47:17 -0700286void SkLuaCanvas::onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
287 const SkRect* cull, const SkPaint& paint) {
288 AUTO_LUA("drawTextRSXform");
289 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
290 // TODO: export other params
291 lua.pushPaint(paint, "paint");
292}
293
fmalitab7425172014-08-26 07:56:44 -0700294void SkLuaCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y,
295 const SkPaint &paint) {
296 AUTO_LUA("drawTextBlob");
297 lua.pushTextBlob(blob, "blob");
298 lua.pushScalar(x, "x");
299 lua.pushScalar(y, "y");
300 lua.pushPaint(paint, "paint");
301}
302
reedd5fa1a42014-08-09 11:08:05 -0700303void SkLuaCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
304 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000305 AUTO_LUA("drawPicture");
reed@google.comdff7e112013-05-15 19:34:20 +0000306 // call through so we can see the nested picture ops
reedd5fa1a42014-08-09 11:08:05 -0700307 this->INHERITED::onDrawPicture(picture, matrix, paint);
reed@google.comdff7e112013-05-15 19:34:20 +0000308}
309
reed41af9662015-01-05 07:49:08 -0800310void SkLuaCanvas::onDrawVertices(VertexMode vmode, int vertexCount,
reed@google.comdff7e112013-05-15 19:34:20 +0000311 const SkPoint vertices[], const SkPoint texs[],
312 const SkColor colors[], SkXfermode* xmode,
313 const uint16_t indices[], int indexCount,
314 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}