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