blob: 9aef50e98cef25ace20b398bd1a71076a03c314a [file] [log] [blame]
reed@google.com74ce6f02013-05-22 15:13:18 +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 "SkLua.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkPath.h"
12#include "SkMatrix.h"
13#include "SkRRect.h"
14#include "SkString.h"
15
16extern "C" {
reed@google.com3597b732013-05-22 20:12:50 +000017 #include "lua.h"
18 #include "lualib.h"
19 #include "lauxlib.h"
reed@google.com74ce6f02013-05-22 15:13:18 +000020}
21
reed@google.comfd345872013-05-22 20:53:42 +000022// return the metatable name for a given class
reed@google.com3597b732013-05-22 20:12:50 +000023template <typename T> const char* get_mtname();
reed@google.comfd345872013-05-22 20:53:42 +000024#define DEF_MTNAME(T) \
25 template <> const char* get_mtname<T>() { \
26 return #T "_LuaMetaTableName"; \
27 }
28
29DEF_MTNAME(SkCanvas)
30DEF_MTNAME(SkMatrix)
31DEF_MTNAME(SkRRect)
32DEF_MTNAME(SkPath)
33DEF_MTNAME(SkPaint)
reed@google.com74ce6f02013-05-22 15:13:18 +000034
reed@google.com3597b732013-05-22 20:12:50 +000035template <typename T> T* push_new(lua_State* L) {
36 T* addr = (T*)lua_newuserdata(L, sizeof(T));
37 new (addr) T;
38 luaL_getmetatable(L, get_mtname<T>());
39 lua_setmetatable(L, -2);
40 return addr;
41}
reed@google.com74ce6f02013-05-22 15:13:18 +000042
43template <typename T> void push_obj(lua_State* L, const T& obj) {
44 new (lua_newuserdata(L, sizeof(T))) T(obj);
reed@google.com3597b732013-05-22 20:12:50 +000045 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000046 lua_setmetatable(L, -2);
47}
48
49template <typename T> void push_ref(lua_State* L, T* ref) {
50 *(T**)lua_newuserdata(L, sizeof(T*)) = SkRef(ref);
reed@google.com3597b732013-05-22 20:12:50 +000051 luaL_getmetatable(L, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000052 lua_setmetatable(L, -2);
53}
54
55template <typename T> T* get_ref(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000056 return *(T**)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000057}
58
59template <typename T> T* get_obj(lua_State* L, int index) {
reed@google.com3597b732013-05-22 20:12:50 +000060 return (T*)luaL_checkudata(L, index, get_mtname<T>());
reed@google.com74ce6f02013-05-22 15:13:18 +000061}
62
reed@google.com88c9ec92013-05-22 15:43:21 +000063static bool lua2bool(lua_State* L, int index) {
64 return !!lua_toboolean(L, index);
65}
66
reed@google.com74ce6f02013-05-22 15:13:18 +000067///////////////////////////////////////////////////////////////////////////////
68
reed@google.com3597b732013-05-22 20:12:50 +000069SkLua::SkLua(const char termCode[]) : fTermCode(termCode), fWeOwnL(true) {
70 fL = luaL_newstate();
71 luaL_openlibs(fL);
72 SkLua::Load(fL);
73}
74
75SkLua::SkLua(lua_State* L) : fL(L), fWeOwnL(false) {}
76
77SkLua::~SkLua() {
78 if (fWeOwnL) {
79 if (fTermCode.size() > 0) {
80 lua_getglobal(fL, fTermCode.c_str());
81 if (lua_pcall(fL, 0, 0, 0) != LUA_OK) {
82 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
83 }
84 }
85 lua_close(fL);
86 }
87}
88
89bool SkLua::runCode(const char code[]) {
90 int err = luaL_loadstring(fL, code) || lua_pcall(fL, 0, 0, 0);
91 if (err) {
92 SkDebugf("--- lua failed\n");
93 return false;
94 }
95 return true;
96}
97
98bool SkLua::runCode(const void* code, size_t size) {
99 SkString str((const char*)code, size);
100 return this->runCode(str.c_str());
101}
102
103///////////////////////////////////////////////////////////////////////////////
104
105#define CHECK_SETFIELD(key) do if (key) lua_setfield(fL, -2, key); while (0)
106
reed@google.com74ce6f02013-05-22 15:13:18 +0000107static void setfield_string(lua_State* L, const char key[], const char value[]) {
108 lua_pushstring(L, value);
109 lua_setfield(L, -2, key);
110}
111
112static void setfield_number(lua_State* L, const char key[], double value) {
113 lua_pushnumber(L, value);
114 lua_setfield(L, -2, key);
115}
116
reed@google.com3597b732013-05-22 20:12:50 +0000117static void setfield_function(lua_State* L,
118 const char key[], lua_CFunction value) {
119 lua_pushcfunction(L, value);
120 lua_setfield(L, -2, key);
reed@google.com74ce6f02013-05-22 15:13:18 +0000121}
122
reed@google.com74ce6f02013-05-22 15:13:18 +0000123void SkLua::pushBool(bool value, const char key[]) {
124 lua_pushboolean(fL, value);
125 CHECK_SETFIELD(key);
126}
127
128void SkLua::pushString(const char str[], const char key[]) {
129 lua_pushstring(fL, str);
130 CHECK_SETFIELD(key);
131}
132
133void SkLua::pushString(const SkString& str, const char key[]) {
134 lua_pushstring(fL, str.c_str());
135 CHECK_SETFIELD(key);
136}
137
138void SkLua::pushColor(SkColor color, const char key[]) {
139 lua_newtable(fL);
140 setfield_number(fL, "a", SkColorGetA(color) / 255.0);
141 setfield_number(fL, "r", SkColorGetR(color) / 255.0);
142 setfield_number(fL, "g", SkColorGetG(color) / 255.0);
143 setfield_number(fL, "b", SkColorGetB(color) / 255.0);
144 CHECK_SETFIELD(key);
145}
146
147void SkLua::pushScalar(SkScalar value, const char key[]) {
148 lua_pushnumber(fL, SkScalarToLua(value));
149 CHECK_SETFIELD(key);
150}
151
152void SkLua::pushRect(const SkRect& r, const char key[]) {
153 lua_newtable(fL);
154 setfield_number(fL, "left", SkScalarToLua(r.fLeft));
155 setfield_number(fL, "top", SkScalarToLua(r.fTop));
156 setfield_number(fL, "right", SkScalarToLua(r.fRight));
157 setfield_number(fL, "bottom", SkScalarToLua(r.fBottom));
158 CHECK_SETFIELD(key);
159}
160
161void SkLua::pushRRect(const SkRRect& rr, const char key[]) {
162 push_obj(fL, rr);
163 CHECK_SETFIELD(key);
164}
165
166void SkLua::pushMatrix(const SkMatrix& matrix, const char key[]) {
167 push_obj(fL, matrix);
168 CHECK_SETFIELD(key);
169}
170
171void SkLua::pushPaint(const SkPaint& paint, const char key[]) {
172 push_obj(fL, paint);
173 CHECK_SETFIELD(key);
174}
175
176void SkLua::pushPath(const SkPath& path, const char key[]) {
177 push_obj(fL, path);
178 CHECK_SETFIELD(key);
179}
180
181void SkLua::pushCanvas(SkCanvas* canvas, const char key[]) {
182 push_ref(fL, canvas);
183 CHECK_SETFIELD(key);
184}
185
186///////////////////////////////////////////////////////////////////////////////
187///////////////////////////////////////////////////////////////////////////////
188
189static SkScalar lua2scalar(lua_State* L, int index) {
190 SkASSERT(lua_isnumber(L, index));
191 return SkLuaToScalar(lua_tonumber(L, index));
192}
193
194static SkScalar getfield_scalar(lua_State* L, int index, const char key[]) {
195 SkASSERT(lua_istable(L, index));
196 lua_pushstring(L, key);
197 lua_gettable(L, index);
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000198
reed@google.com74ce6f02013-05-22 15:13:18 +0000199 SkScalar value = lua2scalar(L, -1);
200 lua_pop(L, 1);
201 return value;
202}
203
204static U8CPU unit2byte(SkScalar x) {
205 if (x <= 0) {
206 return 0;
207 } else if (x >= 1) {
208 return 255;
209 } else {
210 return SkScalarRoundToInt(x * 255);
211 }
212}
213
214static SkColor lua2color(lua_State* L, int index) {
215 return SkColorSetARGB(unit2byte(getfield_scalar(L, index, "a")),
216 unit2byte(getfield_scalar(L, index, "r")),
217 unit2byte(getfield_scalar(L, index, "g")),
218 unit2byte(getfield_scalar(L, index, "b")));
219}
220
221static SkRect* lua2rect(lua_State* L, int index, SkRect* rect) {
222 rect->set(getfield_scalar(L, index, "left"),
223 getfield_scalar(L, index, "top"),
224 getfield_scalar(L, index, "right"),
225 getfield_scalar(L, index, "bottom"));
226 return rect;
227}
228
229static int lcanvas_drawColor(lua_State* L) {
230 get_ref<SkCanvas>(L, 1)->drawColor(lua2color(L, 2));
231 return 0;
232}
233
234static int lcanvas_drawRect(lua_State* L) {
235 SkRect rect;
236 get_ref<SkCanvas>(L, 1)->drawRect(*lua2rect(L, 2, &rect),
237 *get_obj<SkPaint>(L, 3));
238 return 0;
239}
240
241static int lcanvas_drawOval(lua_State* L) {
242 SkRect rect;
243 get_ref<SkCanvas>(L, 1)->drawOval(*lua2rect(L, 2, &rect),
244 *get_obj<SkPaint>(L, 3));
245 return 0;
246}
247
248static int lcanvas_drawCircle(lua_State* L) {
249 get_ref<SkCanvas>(L, 1)->drawCircle(lua2scalar(L, 2),
250 lua2scalar(L, 3),
251 lua2scalar(L, 4),
252 *get_obj<SkPaint>(L, 5));
253 return 0;
254}
255
reed@google.comfd345872013-05-22 20:53:42 +0000256static int lcanvas_drawPath(lua_State* L) {
257 get_ref<SkCanvas>(L, 1)->drawPath(*get_obj<SkPath>(L, 2),
258 *get_obj<SkPaint>(L, 3));
259 return 0;
260}
261
reed@google.com74ce6f02013-05-22 15:13:18 +0000262static int lcanvas_getSaveCount(lua_State* L) {
263 lua_pushnumber(L, get_ref<SkCanvas>(L, 1)->getSaveCount());
264 return 1;
265}
266
267static int lcanvas_getTotalMatrix(lua_State* L) {
268 SkLua(L).pushMatrix(get_ref<SkCanvas>(L, 1)->getTotalMatrix());
269 return 1;
270}
271
reed@google.com3597b732013-05-22 20:12:50 +0000272static int lcanvas_translate(lua_State* L) {
273 get_ref<SkCanvas>(L, 1)->translate(lua2scalar(L, 2), lua2scalar(L, 3));
274 return 0;
275}
276
reed@google.com74ce6f02013-05-22 15:13:18 +0000277static int lcanvas_gc(lua_State* L) {
278 get_ref<SkCanvas>(L, 1)->unref();
279 return 0;
280}
281
282static const struct luaL_Reg gSkCanvas_Methods[] = {
283 { "drawColor", lcanvas_drawColor },
284 { "drawRect", lcanvas_drawRect },
285 { "drawOval", lcanvas_drawOval },
286 { "drawCircle", lcanvas_drawCircle },
reed@google.comfd345872013-05-22 20:53:42 +0000287 { "drawPath", lcanvas_drawPath },
reed@google.com74ce6f02013-05-22 15:13:18 +0000288 { "getSaveCount", lcanvas_getSaveCount },
289 { "getTotalMatrix", lcanvas_getTotalMatrix },
reed@google.com3597b732013-05-22 20:12:50 +0000290 { "translate", lcanvas_translate },
reed@google.com74ce6f02013-05-22 15:13:18 +0000291 { "__gc", lcanvas_gc },
292 { NULL, NULL }
293};
294
295///////////////////////////////////////////////////////////////////////////////
296
297static int lpaint_isAntiAlias(lua_State* L) {
298 lua_pushboolean(L, get_obj<SkPaint>(L, 1)->isAntiAlias());
299 return 1;
300}
301
302static int lpaint_setAntiAlias(lua_State* L) {
reed@google.com88c9ec92013-05-22 15:43:21 +0000303 get_obj<SkPaint>(L, 1)->setAntiAlias(lua2bool(L, 2));
reed@google.com74ce6f02013-05-22 15:13:18 +0000304 return 0;
305}
306
307static int lpaint_getColor(lua_State* L) {
308 SkLua(L).pushColor(get_obj<SkPaint>(L, 1)->getColor());
309 return 1;
310}
311
312static int lpaint_setColor(lua_State* L) {
313 get_obj<SkPaint>(L, 1)->setColor(lua2color(L, 2));
314 return 0;
315}
316
317static int lpaint_gc(lua_State* L) {
318 get_obj<SkPaint>(L, 1)->~SkPaint();
319 return 0;
320}
321
322static const struct luaL_Reg gSkPaint_Methods[] = {
323 { "isAntiAlias", lpaint_isAntiAlias },
324 { "setAntiAlias", lpaint_setAntiAlias },
325 { "getColor", lpaint_getColor },
326 { "setColor", lpaint_setColor },
327 { "__gc", lpaint_gc },
328 { NULL, NULL }
329};
330
331///////////////////////////////////////////////////////////////////////////////
332
333static int lpath_getBounds(lua_State* L) {
334 SkLua(L).pushRect(get_obj<SkPath>(L, 1)->getBounds());
335 return 1;
336}
337
338static int lpath_isEmpty(lua_State* L) {
339 lua_pushboolean(L, get_obj<SkPath>(L, 1)->isEmpty());
340 return 1;
341}
342
343static int lpath_isRect(lua_State* L) {
344 SkRect r;
345 bool pred = get_obj<SkPath>(L, 1)->isRect(&r);
346 int ret_count = 1;
347 lua_pushboolean(L, pred);
348 if (pred) {
349 SkLua(L).pushRect(r);
350 ret_count += 1;
351 }
352 return ret_count;
353}
354
355static const char* dir2string(SkPath::Direction dir) {
356 static const char* gStr[] = {
357 "unknown", "cw", "ccw"
358 };
359 SkASSERT((unsigned)dir < SK_ARRAY_COUNT(gStr));
360 return gStr[dir];
361}
362
363static int lpath_isNestedRects(lua_State* L) {
364 SkRect rects[2];
365 SkPath::Direction dirs[2];
366 bool pred = get_obj<SkPath>(L, 1)->isNestedRects(rects, dirs);
367 int ret_count = 1;
368 lua_pushboolean(L, pred);
369 if (pred) {
370 SkLua lua(L);
371 lua.pushRect(rects[0]);
372 lua.pushRect(rects[1]);
373 lua_pushstring(L, dir2string(dirs[0]));
374 lua_pushstring(L, dir2string(dirs[0]));
375 ret_count += 4;
376 }
377 return ret_count;
378}
379
380static int lpath_reset(lua_State* L) {
381 get_obj<SkPath>(L, 1)->reset();
382 return 0;
383}
384
385static int lpath_moveTo(lua_State* L) {
386 get_obj<SkPath>(L, 1)->moveTo(lua2scalar(L, 2), lua2scalar(L, 3));
387 return 0;
388}
389
390static int lpath_lineTo(lua_State* L) {
391 get_obj<SkPath>(L, 1)->lineTo(lua2scalar(L, 2), lua2scalar(L, 3));
392 return 0;
393}
394
395static int lpath_quadTo(lua_State* L) {
396 get_obj<SkPath>(L, 1)->quadTo(lua2scalar(L, 2), lua2scalar(L, 3),
397 lua2scalar(L, 4), lua2scalar(L, 5));
398 return 0;
399}
400
401static int lpath_cubicTo(lua_State* L) {
402 get_obj<SkPath>(L, 1)->cubicTo(lua2scalar(L, 2), lua2scalar(L, 3),
403 lua2scalar(L, 4), lua2scalar(L, 5),
404 lua2scalar(L, 6), lua2scalar(L, 7));
405 return 0;
406}
407
408static int lpath_close(lua_State* L) {
409 get_obj<SkPath>(L, 1)->close();
410 return 0;
411}
412
413static int lpath_gc(lua_State* L) {
414 get_obj<SkPath>(L, 1)->~SkPath();
415 return 0;
416}
417
418static const struct luaL_Reg gSkPath_Methods[] = {
419 { "getBounds", lpath_getBounds },
420 { "isEmpty", lpath_isEmpty },
421 { "isRect", lpath_isRect },
422 { "isNestedRects", lpath_isNestedRects },
423 { "reset", lpath_reset },
424 { "moveTo", lpath_moveTo },
425 { "lineTo", lpath_lineTo },
426 { "quadTo", lpath_quadTo },
427 { "cubicTo", lpath_cubicTo },
428 { "close", lpath_close },
429 { "__gc", lpath_gc },
430 { NULL, NULL }
431};
432
433///////////////////////////////////////////////////////////////////////////////
434
435static const char* rrect_type(const SkRRect& rr) {
436 switch (rr.getType()) {
437 case SkRRect::kUnknown_Type: return "unknown";
438 case SkRRect::kEmpty_Type: return "empty";
439 case SkRRect::kRect_Type: return "rect";
440 case SkRRect::kOval_Type: return "oval";
441 case SkRRect::kSimple_Type: return "simple";
442 case SkRRect::kComplex_Type: return "complex";
443 }
444 SkASSERT(!"never get here");
445 return "";
446}
447
448static int lrrect_rect(lua_State* L) {
449 SkLua(L).pushRect(get_obj<SkRRect>(L, 1)->rect());
450 return 1;
451}
452
453static int lrrect_type(lua_State* L) {
454 lua_pushstring(L, rrect_type(*get_obj<SkRRect>(L, 1)));
455 return 1;
456}
457
458static int lrrect_radii(lua_State* L) {
459 int corner = lua_tointeger(L, 2);
460 SkVector v;
461 if (corner < 0 || corner > 3) {
462 SkDebugf("bad corner index %d", corner);
463 v.set(0, 0);
464 } else {
465 v = get_obj<SkRRect>(L, 1)->radii((SkRRect::Corner)corner);
466 }
467 lua_pushnumber(L, v.fX);
468 lua_pushnumber(L, v.fY);
469 return 2;
470}
471
472static int lrrect_gc(lua_State* L) {
473 get_obj<SkRRect>(L, 1)->~SkRRect();
474 return 0;
475}
476
477static const struct luaL_Reg gSkRRect_Methods[] = {
478 { "rect", lrrect_rect },
479 { "type", lrrect_type },
480 { "radii", lrrect_radii },
481 { "__gc", lrrect_gc },
482 { NULL, NULL }
483};
484
485///////////////////////////////////////////////////////////////////////////////
486
487class AutoCallLua {
488public:
489 AutoCallLua(lua_State* L, const char func[], const char verb[]) : fL(L) {
490 lua_getglobal(L, func);
491 if (!lua_isfunction(L, -1)) {
492 int t = lua_type(L, -1);
493 SkDebugf("--- expected function %d\n", t);
494 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000495
reed@google.com74ce6f02013-05-22 15:13:18 +0000496 lua_newtable(L);
497 setfield_string(L, "verb", verb);
498 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000499
reed@google.com74ce6f02013-05-22 15:13:18 +0000500 ~AutoCallLua() {
501 if (lua_pcall(fL, 1, 0, 0) != LUA_OK) {
502 SkDebugf("lua err: %s\n", lua_tostring(fL, -1));
503 }
504 lua_settop(fL, -1);
505 }
skia.committer@gmail.com2d816ad2013-05-23 07:01:22 +0000506
reed@google.com74ce6f02013-05-22 15:13:18 +0000507private:
508 lua_State* fL;
509};
510
511#define AUTO_LUA(verb) AutoCallLua acl(fL, fFunc.c_str(), verb)
512
513///////////////////////////////////////////////////////////////////////////////
514
reed@google.com3597b732013-05-22 20:12:50 +0000515static int lsk_newPaint(lua_State* L) {
516 push_new<SkPaint>(L);
517 return 1;
518}
519
520static int lsk_newPath(lua_State* L) {
521 push_new<SkPath>(L);
522 return 1;
523}
524
525static int lsk_newRRect(lua_State* L) {
526 SkRRect* rr = push_new<SkRRect>(L);
527 rr->setEmpty();
528 return 1;
529}
530
531static const struct luaL_Reg gSk_Functions[] = {
532 { "newPaint", lsk_newPaint },
533 { "newPath", lsk_newPath },
534 { "newRRect", lsk_newRRect },
535 { NULL, NULL }
536};
537
538static void register_Sk(lua_State* L) {
539 lua_newtable(L);
540 lua_pushvalue(L, -1);
541 lua_setglobal(L, "Sk");
542 // the Sk table is still on top
543
544 setfield_function(L, "newPaint", lsk_newPaint);
545 setfield_function(L, "newPath", lsk_newPath);
546 setfield_function(L, "newRRect", lsk_newRRect);
547 lua_pop(L, 1); // pop off the Sk table
548}
549
reed@google.com74ce6f02013-05-22 15:13:18 +0000550#define REG_CLASS(L, C) \
551 do { \
reed@google.com3597b732013-05-22 20:12:50 +0000552 luaL_newmetatable(L, get_mtname<C>()); \
reed@google.com74ce6f02013-05-22 15:13:18 +0000553 lua_pushvalue(L, -1); \
554 lua_setfield(L, -2, "__index"); \
555 luaL_setfuncs(L, g##C##_Methods, 0); \
556 lua_pop(L, 1); /* pop off the meta-table */ \
557 } while (0)
558
559void SkLua::Load(lua_State* L) {
reed@google.com3597b732013-05-22 20:12:50 +0000560 register_Sk(L);
reed@google.com74ce6f02013-05-22 15:13:18 +0000561 REG_CLASS(L, SkCanvas);
562 REG_CLASS(L, SkPath);
563 REG_CLASS(L, SkPaint);
564 REG_CLASS(L, SkRRect);
565}