blob: fc43514e462decbc3122b791eb3530a042069890 [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"
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +00009#include "SkRRect.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
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000016static void setfield_string(lua_State* L, const char key[], const char value[]) {
17 lua_pushstring(L, value);
18 lua_setfield(L, -2, key);
reed@google.comdff7e112013-05-15 19:34:20 +000019}
20
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000021static void setfield_number(lua_State* L, const char key[], double value) {
22 lua_pushnumber(L, value);
23 lua_setfield(L, -2, key);
24}
25
26static void setfield_bool(lua_State* L, const char key[], bool value) {
27 lua_pushboolean(L, value);
28 lua_setfield(L, -2, key);
29}
30
reed@google.com57de5cf2013-05-16 13:51:07 +000031// sets [1]...[count] in the table on the top of the stack
32static void setfield_arrayf(lua_State* L, const SkScalar array[], int count) {
33 for (int i = 0; i < count; ++i) {
34 lua_pushnumber(L, SkScalarToDouble(i + 1)); // key
35 lua_pushnumber(L, SkScalarToDouble(array[i])); // value
36 lua_settable(L, -3);
37 }
38}
39
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000040static void setfield_rect(lua_State* L, const char key[], const SkRect& r) {
41 lua_newtable(L);
42 setfield_number(L, "left", r.fLeft);
43 setfield_number(L, "top", r.fTop);
44 setfield_number(L, "right", r.fRight);
45 setfield_number(L, "bottom", r.fBottom);
46 lua_setfield(L, -2, key);
47}
48
reed@google.com57de5cf2013-05-16 13:51:07 +000049static const char* rrect_type(const SkRRect& rr) {
50 switch (rr.getType()) {
51 case SkRRect::kUnknown_Type: return "unknown";
52 case SkRRect::kEmpty_Type: return "empty";
53 case SkRRect::kRect_Type: return "rect";
54 case SkRRect::kOval_Type: return "oval";
55 case SkRRect::kSimple_Type: return "simple";
56 case SkRRect::kComplex_Type: return "complex";
57 }
58 SkASSERT(!"never get here");
59 return "";
60}
61
62static void setfield_rrect(lua_State* L, const char key[], const SkRRect& rr) {
63 lua_newtable(L);
64 setfield_rect(L, "rect", rr.getBounds());
65 setfield_string(L, "type", rrect_type(rr));
66
67 SkVector rad[4] = {
68 rr.radii(SkRRect::kUpperLeft_Corner),
69 rr.radii(SkRRect::kUpperRight_Corner),
70 rr.radii(SkRRect::kLowerRight_Corner),
71 rr.radii(SkRRect::kLowerLeft_Corner),
72 };
73 setfield_arrayf(L, &rad[0].fX, 8);
74 lua_setfield(L, -2, key);
75}
76
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000077static void push_matrix(lua_State* L, const SkMatrix& mat) {
78 SkScalar m[9];
79 for (int i = 0; i < 9; ++i) {
80 m[i] = mat[i];
81 }
82 lua_newtable(L);
83 setfield_arrayf(L, m, 9);
84}
85
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000086enum PaintUsage {
87 kText_PaintUsage,
88 kImage_PaintUsage,
89 kGeometry_PaintUsage
90};
91
92static const char* color2string(SkColor c, SkString* str) {
93 str->printf("0x%08X", c);
94 return str->c_str();
95}
96
97static void setfield_paint(lua_State* L, const SkPaint& p,
98 PaintUsage pu = kGeometry_PaintUsage) {
99 SkString str;
100
101 lua_newtable(L);
102 setfield_bool(L, "aa", p.isAntiAlias());
103 setfield_string(L, "color", color2string(p.getColor(), &str));
104
105 if (kGeometry_PaintUsage == pu) {
106 if (SkPaint::kFill_Style != p.getStyle()) {
107 setfield_number(L, "stroke-width", p.getStrokeWidth());
108 }
109 }
110 lua_setfield(L, -2, "paint");
111}
112
113class AutoCallLua {
114public:
115 AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
116 lua_getglobal(L, func);
117 if (!lua_isfunction(L, -1)) {
118 int t = lua_type(L, -1);
119 SkDebugf("--- expected function %d\n", t);
120 }
skia.committer@gmail.com539f3642013-05-16 07:01:00 +0000121
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000122 lua_newtable(L);
123 setfield_string(L, "verb", verb);
124 }
125
126 ~AutoCallLua() {
127 if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
128 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
129 }
130 lua_settop(fL, -1);
131 }
132
133private:
134 lua_State* fL;
135};
136
137#define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb)
138
reed@google.comdff7e112013-05-15 19:34:20 +0000139///////////////////////////////////////////////////////////////////////////////
140
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +0000141static const char gCanvasMetaTableName[] = "SkCanvas_MetaTable";
142
143static int lcanvas_getSaveCount(lua_State* L) {
144 SkCanvas* c = *(SkCanvas**)luaL_checkudata(L, 1, gCanvasMetaTableName);
145 lua_pushnumber(L, (double)c->getSaveCount());
146 return 1;
147}
148
149static int lcanvas_getTotalMatrix(lua_State* L) {
150 SkCanvas* c = *(SkCanvas**)luaL_checkudata(L, 1, gCanvasMetaTableName);
151 push_matrix(L, c->getTotalMatrix());
152 return 1;
153}
154
155static int lcanvas_gc(lua_State* L) {
156 SkCanvas** cptr = (SkCanvas**)luaL_checkudata(L, 1, gCanvasMetaTableName);
157 SkSafeUnref(*cptr);
158 *cptr = NULL;
159 return 0;
160}
161
162static const struct luaL_Reg gLuaCanvasMethods[] = {
163 { "getSaveCount", lcanvas_getSaveCount },
164 { "getTotalMatrix", lcanvas_getTotalMatrix },
165 { "__gc", lcanvas_gc },
166 { NULL, NULL }
167};
168
169static void ensure_canvas_metatable(lua_State* L) {
170 static bool gOnce;
171 if (gOnce) {
172 return;
173 }
174 gOnce = true;
175
176 luaL_newmetatable(L, gCanvasMetaTableName);
177 lua_pushvalue(L, -1);
178 lua_setfield(L, -2, "__index");
179
180 luaL_setfuncs(L, gLuaCanvasMethods, 0);
181 lua_settop(L, -2); // pop off the meta-table
182}
183
184void SkLuaCanvas::pushThis() {
185 ensure_canvas_metatable(fL);
186
187 SkCanvas** canvasPtr = (SkCanvas**)lua_newuserdata(fL, sizeof(SkCanvas*));
188 luaL_getmetatable(fL, gCanvasMetaTableName);
189 lua_setmetatable(fL, -2);
190
191 this->ref();
192 *canvasPtr = this;
193}
194
195///////////////////////////////////////////////////////////////////////////////
196
reed@google.comdff7e112013-05-15 19:34:20 +0000197static SkBitmap make_bm(int width, int height) {
198 SkBitmap bm;
199 bm.setConfig(SkBitmap::kNo_Config, width, height);
200 return bm;
201}
202
203SkLuaCanvas::SkLuaCanvas(int width, int height, lua_State* L, const char func[])
204 : INHERITED(make_bm(width, height))
205 , fL(L)
206 , fFunc(func) {
207}
208
209SkLuaCanvas::~SkLuaCanvas() {}
210
211int SkLuaCanvas::save(SaveFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000212 AUTO_LUA("save");
reed@google.comdff7e112013-05-15 19:34:20 +0000213 return this->INHERITED::save(flags);
214}
215
216int SkLuaCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
217 SaveFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000218 AUTO_LUA("saveLayer");
219 if (bounds) {
220 setfield_rect(fL, "bounds", *bounds);
221 }
222 if (paint) {
223 setfield_paint(fL, *paint);
224 }
reed@google.comdff7e112013-05-15 19:34:20 +0000225 return this->INHERITED::save(flags);
226}
227
228void SkLuaCanvas::restore() {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000229 AUTO_LUA("restore");
reed@google.comdff7e112013-05-15 19:34:20 +0000230 this->INHERITED::restore();
231}
232
233bool SkLuaCanvas::translate(SkScalar dx, SkScalar dy) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000234 AUTO_LUA("translate");
235 setfield_number(fL, "dx", dx);
236 setfield_number(fL, "dy", dy);
reed@google.comdff7e112013-05-15 19:34:20 +0000237 return this->INHERITED::translate(dx, dy);
238}
239
240bool SkLuaCanvas::scale(SkScalar sx, SkScalar sy) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000241 AUTO_LUA("scale");
242 setfield_number(fL, "sx", sx);
243 setfield_number(fL, "sy", sy);
reed@google.comdff7e112013-05-15 19:34:20 +0000244 return this->INHERITED::scale(sx, sy);
245}
246
247bool SkLuaCanvas::rotate(SkScalar degrees) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000248 AUTO_LUA("rotate");
249 setfield_number(fL, "degrees", degrees);
reed@google.comdff7e112013-05-15 19:34:20 +0000250 return this->INHERITED::rotate(degrees);
251}
252
253bool SkLuaCanvas::skew(SkScalar sx, SkScalar sy) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000254 AUTO_LUA("skew");
255 setfield_number(fL, "sx", sx);
256 setfield_number(fL, "sy", sy);
reed@google.comdff7e112013-05-15 19:34:20 +0000257 return this->INHERITED::skew(sx, sy);
258}
259
260bool SkLuaCanvas::concat(const SkMatrix& matrix) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000261 AUTO_LUA("concat");
reed@google.comdff7e112013-05-15 19:34:20 +0000262 return this->INHERITED::concat(matrix);
263}
264
265void SkLuaCanvas::setMatrix(const SkMatrix& matrix) {
266 this->INHERITED::setMatrix(matrix);
267}
268
269bool SkLuaCanvas::clipRect(const SkRect& r, SkRegion::Op op, bool doAA) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000270 AUTO_LUA("clipRect");
271 setfield_rect(fL, "rect", r);
272 setfield_bool(fL, "aa", doAA);
reed@google.comdff7e112013-05-15 19:34:20 +0000273 return this->INHERITED::clipRect(r, op, doAA);
274}
275
276bool SkLuaCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000277 AUTO_LUA("clipRRect");
reed@google.com57de5cf2013-05-16 13:51:07 +0000278 setfield_rrect(fL, "rrect", rrect);
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000279 setfield_bool(fL, "aa", doAA);
reed@google.comdff7e112013-05-15 19:34:20 +0000280 return this->INHERITED::clipRRect(rrect, op, doAA);
281}
282
283bool SkLuaCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000284 AUTO_LUA("clipPath");
285 setfield_bool(fL, "aa", doAA);
reed@google.comdff7e112013-05-15 19:34:20 +0000286 return this->INHERITED::clipPath(path, op, doAA);
287}
288
289bool SkLuaCanvas::clipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000290 AUTO_LUA("clipRegion");
reed@google.comdff7e112013-05-15 19:34:20 +0000291 return this->INHERITED::clipRegion(deviceRgn, op);
292}
293
294void SkLuaCanvas::drawPaint(const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000295 AUTO_LUA("drawPaint");
296 setfield_paint(fL, paint);
reed@google.comdff7e112013-05-15 19:34:20 +0000297}
298
299void SkLuaCanvas::drawPoints(PointMode mode, size_t count,
300 const SkPoint pts[], const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000301 AUTO_LUA("drawPoints");
302 setfield_paint(fL, paint);
reed@google.comdff7e112013-05-15 19:34:20 +0000303}
304
305void SkLuaCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000306 AUTO_LUA("drawOval");
reed@google.com57de5cf2013-05-16 13:51:07 +0000307 setfield_rect(fL, "oval", rect);
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000308 setfield_paint(fL, paint);
reed@google.comdff7e112013-05-15 19:34:20 +0000309}
310
311void SkLuaCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000312 AUTO_LUA("drawRect");
313 setfield_rect(fL, "rect", rect);
314 setfield_paint(fL, paint);
reed@google.comdff7e112013-05-15 19:34:20 +0000315}
316
317void SkLuaCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000318 AUTO_LUA("drawRRect");
reed@google.com57de5cf2013-05-16 13:51:07 +0000319 setfield_rrect(fL, "rrect", rrect);
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000320 setfield_paint(fL, paint);
reed@google.comdff7e112013-05-15 19:34:20 +0000321}
322
323void SkLuaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000324 AUTO_LUA("drawPath");
325 setfield_rect(fL, "bounds", path.getBounds());
reed@google.com57de5cf2013-05-16 13:51:07 +0000326 setfield_bool(fL, "isRect", path.isRect(NULL));
327 setfield_bool(fL, "isOval", path.isOval(NULL));
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000328 setfield_paint(fL, paint);
reed@google.comdff7e112013-05-15 19:34:20 +0000329}
330
331void SkLuaCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
332 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000333 AUTO_LUA("drawBitmap");
334 if (paint) {
335 setfield_paint(fL, *paint, kImage_PaintUsage);
336 }
reed@google.comdff7e112013-05-15 19:34:20 +0000337}
338
339void SkLuaCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
340 const SkRect& dst, const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000341 AUTO_LUA("drawBitmapRectToRect");
342 if (paint) {
343 setfield_paint(fL, *paint, kImage_PaintUsage);
344 }
reed@google.comdff7e112013-05-15 19:34:20 +0000345}
346
347void SkLuaCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
348 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000349 AUTO_LUA("drawBitmapMatrix");
350 if (paint) {
351 setfield_paint(fL, *paint, kImage_PaintUsage);
352 }
reed@google.comdff7e112013-05-15 19:34:20 +0000353}
354
355void SkLuaCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
356 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000357 AUTO_LUA("drawSprite");
358 if (paint) {
359 setfield_paint(fL, *paint, kImage_PaintUsage);
360 }
reed@google.comdff7e112013-05-15 19:34:20 +0000361}
362
363void SkLuaCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
364 SkScalar y, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000365 AUTO_LUA("drawText");
366 setfield_paint(fL, paint, kText_PaintUsage);
reed@google.comdff7e112013-05-15 19:34:20 +0000367}
368
369void SkLuaCanvas::drawPosText(const void* text, size_t byteLength,
370 const SkPoint pos[], const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000371 AUTO_LUA("drawPosText");
372 setfield_paint(fL, paint, kText_PaintUsage);
reed@google.comdff7e112013-05-15 19:34:20 +0000373}
374
375void SkLuaCanvas::drawPosTextH(const void* text, size_t byteLength,
376 const SkScalar xpos[], SkScalar constY,
377 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000378 AUTO_LUA("drawPosTextH");
379 setfield_paint(fL, paint, kText_PaintUsage);
reed@google.comdff7e112013-05-15 19:34:20 +0000380}
381
382void SkLuaCanvas::drawTextOnPath(const void* text, size_t byteLength,
383 const SkPath& path, const SkMatrix* matrix,
384 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000385 AUTO_LUA("drawTextOnPath");
386 setfield_paint(fL, paint, kText_PaintUsage);
reed@google.comdff7e112013-05-15 19:34:20 +0000387}
388
389void SkLuaCanvas::drawPicture(SkPicture& picture) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000390 AUTO_LUA("drawPicture");
reed@google.comdff7e112013-05-15 19:34:20 +0000391 // call through so we can see the nested picture ops
392 this->INHERITED::drawPicture(picture);
393}
394
395void SkLuaCanvas::drawVertices(VertexMode vmode, int vertexCount,
396 const SkPoint vertices[], const SkPoint texs[],
397 const SkColor colors[], SkXfermode* xmode,
398 const uint16_t indices[], int indexCount,
399 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000400 AUTO_LUA("drawVertices");
401 setfield_paint(fL, paint);
reed@google.comdff7e112013-05-15 19:34:20 +0000402}
403
404void SkLuaCanvas::drawData(const void* data, size_t length) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000405 AUTO_LUA("drawData");
reed@google.comdff7e112013-05-15 19:34:20 +0000406}