blob: c5ec00691356d93b6fa03f3a18dedd80a1ac59f4 [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:
60 this->pushArrayU16((const uint16_t*)text, length >> 1, "glyphs");
61 break;
62 case SkPaint::kUTF32_TextEncoding:
63 break;
64 }
65}
66
reed@google.com9a731042013-05-21 17:52:33 +000067///////////////////////////////////////////////////////////////////////////////
68
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000069void SkLuaCanvas::pushThis() {
reed@google.com74ce6f02013-05-22 15:13:18 +000070 SkLua(fL).pushCanvas(this);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000071}
72
73///////////////////////////////////////////////////////////////////////////////
74
reed@google.comdff7e112013-05-15 19:34:20 +000075static SkBitmap make_bm(int width, int height) {
76 SkBitmap bm;
77 bm.setConfig(SkBitmap::kNo_Config, width, height);
78 return bm;
79}
80
81SkLuaCanvas::SkLuaCanvas(int width, int height, lua_State* L, const char func[])
82 : INHERITED(make_bm(width, height))
83 , fL(L)
84 , fFunc(func) {
85}
86
87SkLuaCanvas::~SkLuaCanvas() {}
88
89int SkLuaCanvas::save(SaveFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000090 AUTO_LUA("save");
reed@google.comdff7e112013-05-15 19:34:20 +000091 return this->INHERITED::save(flags);
92}
93
94int SkLuaCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
95 SaveFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000096 AUTO_LUA("saveLayer");
97 if (bounds) {
reed@google.com74ce6f02013-05-22 15:13:18 +000098 lua.pushRect(*bounds, "bounds");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000099 }
100 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000101 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000102 }
reed@google.comdff7e112013-05-15 19:34:20 +0000103 return this->INHERITED::save(flags);
104}
105
106void SkLuaCanvas::restore() {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000107 AUTO_LUA("restore");
reed@google.comdff7e112013-05-15 19:34:20 +0000108 this->INHERITED::restore();
109}
110
111bool SkLuaCanvas::translate(SkScalar dx, SkScalar dy) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000112 AUTO_LUA("translate");
reed@google.com74ce6f02013-05-22 15:13:18 +0000113 lua.pushScalar(dx, "dx");
114 lua.pushScalar(dy, "dy");
reed@google.comdff7e112013-05-15 19:34:20 +0000115 return this->INHERITED::translate(dx, dy);
116}
117
118bool SkLuaCanvas::scale(SkScalar sx, SkScalar sy) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000119 AUTO_LUA("scale");
reed@google.com74ce6f02013-05-22 15:13:18 +0000120 lua.pushScalar(sx, "sx");
121 lua.pushScalar(sy, "sy");
reed@google.comdff7e112013-05-15 19:34:20 +0000122 return this->INHERITED::scale(sx, sy);
123}
124
125bool SkLuaCanvas::rotate(SkScalar degrees) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000126 AUTO_LUA("rotate");
reed@google.com74ce6f02013-05-22 15:13:18 +0000127 lua.pushScalar(degrees, "degrees");
reed@google.comdff7e112013-05-15 19:34:20 +0000128 return this->INHERITED::rotate(degrees);
129}
130
reed@google.com74ce6f02013-05-22 15:13:18 +0000131bool SkLuaCanvas::skew(SkScalar kx, SkScalar ky) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000132 AUTO_LUA("skew");
reed@google.com74ce6f02013-05-22 15:13:18 +0000133 lua.pushScalar(kx, "kx");
134 lua.pushScalar(ky, "ky");
135 return this->INHERITED::skew(kx, ky);
reed@google.comdff7e112013-05-15 19:34:20 +0000136}
137
138bool SkLuaCanvas::concat(const SkMatrix& matrix) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000139 AUTO_LUA("concat");
reed@google.comdff7e112013-05-15 19:34:20 +0000140 return this->INHERITED::concat(matrix);
141}
142
143void SkLuaCanvas::setMatrix(const SkMatrix& matrix) {
144 this->INHERITED::setMatrix(matrix);
145}
146
147bool SkLuaCanvas::clipRect(const SkRect& r, SkRegion::Op op, bool doAA) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000148 AUTO_LUA("clipRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000149 lua.pushRect(r, "rect");
150 lua.pushBool(doAA, "aa");
reed@google.comdff7e112013-05-15 19:34:20 +0000151 return this->INHERITED::clipRect(r, op, doAA);
152}
153
154bool SkLuaCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000155 AUTO_LUA("clipRRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000156 lua.pushRRect(rrect, "rrect");
157 lua.pushBool(doAA, "aa");
reed@google.comdff7e112013-05-15 19:34:20 +0000158 return this->INHERITED::clipRRect(rrect, op, doAA);
159}
160
161bool SkLuaCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000162 AUTO_LUA("clipPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000163 lua.pushPath(path, "path");
164 lua.pushBool(doAA, "aa");
reed@google.comdff7e112013-05-15 19:34:20 +0000165 return this->INHERITED::clipPath(path, op, doAA);
166}
167
168bool SkLuaCanvas::clipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000169 AUTO_LUA("clipRegion");
reed@google.comdff7e112013-05-15 19:34:20 +0000170 return this->INHERITED::clipRegion(deviceRgn, op);
171}
172
173void SkLuaCanvas::drawPaint(const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000174 AUTO_LUA("drawPaint");
reed@google.com74ce6f02013-05-22 15:13:18 +0000175 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000176}
177
178void SkLuaCanvas::drawPoints(PointMode mode, size_t count,
179 const SkPoint pts[], const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000180 AUTO_LUA("drawPoints");
reed@google.com74ce6f02013-05-22 15:13:18 +0000181 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000182}
183
184void SkLuaCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000185 AUTO_LUA("drawOval");
reed@google.com74ce6f02013-05-22 15:13:18 +0000186 lua.pushRect(rect, "rect");
187 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000188}
189
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000190void SkLuaCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000191 AUTO_LUA("drawRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000192 lua.pushRect(rect, "rect");
193 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000194}
195
196void SkLuaCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000197 AUTO_LUA("drawRRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000198 lua.pushRRect(rrect, "rrect");
199 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000200}
201
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000202void SkLuaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000203 AUTO_LUA("drawPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000204 lua.pushPath(path, "path");
205 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000206}
207
208void SkLuaCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000209 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000210 AUTO_LUA("drawBitmap");
211 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000212 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000213 }
reed@google.comdff7e112013-05-15 19:34:20 +0000214}
215
216void SkLuaCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000217 const SkRect& dst, const SkPaint* paint,
218 DrawBitmapRectFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000219 AUTO_LUA("drawBitmapRectToRect");
220 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000221 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000222 }
reed@google.comdff7e112013-05-15 19:34:20 +0000223}
224
225void SkLuaCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000226 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000227 AUTO_LUA("drawBitmapMatrix");
228 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000229 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000230 }
reed@google.comdff7e112013-05-15 19:34:20 +0000231}
232
233void SkLuaCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
234 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000235 AUTO_LUA("drawSprite");
236 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000237 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000238 }
reed@google.comdff7e112013-05-15 19:34:20 +0000239}
240
241void SkLuaCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
242 SkScalar y, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000243 AUTO_LUA("drawText");
reed@google.come3823fd2013-05-30 18:55:14 +0000244 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000245 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000246}
247
248void SkLuaCanvas::drawPosText(const void* text, size_t byteLength,
249 const SkPoint pos[], const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000250 AUTO_LUA("drawPosText");
reed@google.come3823fd2013-05-30 18:55:14 +0000251 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000252 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000253}
254
255void SkLuaCanvas::drawPosTextH(const void* text, size_t byteLength,
256 const SkScalar xpos[], SkScalar constY,
257 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000258 AUTO_LUA("drawPosTextH");
reed@google.come3823fd2013-05-30 18:55:14 +0000259 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000260 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000261}
262
263void SkLuaCanvas::drawTextOnPath(const void* text, size_t byteLength,
264 const SkPath& path, const SkMatrix* matrix,
265 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000266 AUTO_LUA("drawTextOnPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000267 lua.pushPath(path, "path");
reed@google.come3823fd2013-05-30 18:55:14 +0000268 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000269 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000270}
271
272void SkLuaCanvas::drawPicture(SkPicture& picture) {
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
275 this->INHERITED::drawPicture(picture);
276}
277
278void SkLuaCanvas::drawVertices(VertexMode vmode, int vertexCount,
279 const SkPoint vertices[], const SkPoint texs[],
280 const SkColor colors[], SkXfermode* xmode,
281 const uint16_t indices[], int indexCount,
282 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000283 AUTO_LUA("drawVertices");
reed@google.com74ce6f02013-05-22 15:13:18 +0000284 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000285}
286
287void SkLuaCanvas::drawData(const void* data, size_t length) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000288 AUTO_LUA("drawData");
reed@google.comdff7e112013-05-15 19:34:20 +0000289}