blob: 0136231ce1de5ffc178865c3eae0680af3b1f408 [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 +000076static SkBitmap make_bm(int width, int height) {
77 SkBitmap bm;
78 bm.setConfig(SkBitmap::kNo_Config, width, height);
79 return bm;
80}
81
82SkLuaCanvas::SkLuaCanvas(int width, int height, lua_State* L, const char func[])
83 : INHERITED(make_bm(width, height))
84 , fL(L)
85 , fFunc(func) {
86}
87
88SkLuaCanvas::~SkLuaCanvas() {}
89
90int SkLuaCanvas::save(SaveFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000091 AUTO_LUA("save");
reed@google.comdff7e112013-05-15 19:34:20 +000092 return this->INHERITED::save(flags);
93}
94
95int SkLuaCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
96 SaveFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000097 AUTO_LUA("saveLayer");
98 if (bounds) {
reed@google.com74ce6f02013-05-22 15:13:18 +000099 lua.pushRect(*bounds, "bounds");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000100 }
101 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000102 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000103 }
reed@google.comdff7e112013-05-15 19:34:20 +0000104 return this->INHERITED::save(flags);
105}
106
107void SkLuaCanvas::restore() {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000108 AUTO_LUA("restore");
reed@google.comdff7e112013-05-15 19:34:20 +0000109 this->INHERITED::restore();
110}
111
112bool SkLuaCanvas::translate(SkScalar dx, SkScalar dy) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000113 AUTO_LUA("translate");
reed@google.com74ce6f02013-05-22 15:13:18 +0000114 lua.pushScalar(dx, "dx");
115 lua.pushScalar(dy, "dy");
reed@google.comdff7e112013-05-15 19:34:20 +0000116 return this->INHERITED::translate(dx, dy);
117}
118
119bool SkLuaCanvas::scale(SkScalar sx, SkScalar sy) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000120 AUTO_LUA("scale");
reed@google.com74ce6f02013-05-22 15:13:18 +0000121 lua.pushScalar(sx, "sx");
122 lua.pushScalar(sy, "sy");
reed@google.comdff7e112013-05-15 19:34:20 +0000123 return this->INHERITED::scale(sx, sy);
124}
125
126bool SkLuaCanvas::rotate(SkScalar degrees) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000127 AUTO_LUA("rotate");
reed@google.com74ce6f02013-05-22 15:13:18 +0000128 lua.pushScalar(degrees, "degrees");
reed@google.comdff7e112013-05-15 19:34:20 +0000129 return this->INHERITED::rotate(degrees);
130}
131
reed@google.com74ce6f02013-05-22 15:13:18 +0000132bool SkLuaCanvas::skew(SkScalar kx, SkScalar ky) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000133 AUTO_LUA("skew");
reed@google.com74ce6f02013-05-22 15:13:18 +0000134 lua.pushScalar(kx, "kx");
135 lua.pushScalar(ky, "ky");
136 return this->INHERITED::skew(kx, ky);
reed@google.comdff7e112013-05-15 19:34:20 +0000137}
138
139bool SkLuaCanvas::concat(const SkMatrix& matrix) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000140 AUTO_LUA("concat");
reed@google.comdff7e112013-05-15 19:34:20 +0000141 return this->INHERITED::concat(matrix);
142}
143
144void SkLuaCanvas::setMatrix(const SkMatrix& matrix) {
145 this->INHERITED::setMatrix(matrix);
146}
147
148bool SkLuaCanvas::clipRect(const SkRect& r, SkRegion::Op op, bool doAA) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000149 AUTO_LUA("clipRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000150 lua.pushRect(r, "rect");
151 lua.pushBool(doAA, "aa");
reed@google.comdff7e112013-05-15 19:34:20 +0000152 return this->INHERITED::clipRect(r, op, doAA);
153}
154
155bool SkLuaCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000156 AUTO_LUA("clipRRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000157 lua.pushRRect(rrect, "rrect");
158 lua.pushBool(doAA, "aa");
reed@google.comdff7e112013-05-15 19:34:20 +0000159 return this->INHERITED::clipRRect(rrect, op, doAA);
160}
161
162bool SkLuaCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000163 AUTO_LUA("clipPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000164 lua.pushPath(path, "path");
165 lua.pushBool(doAA, "aa");
reed@google.comdff7e112013-05-15 19:34:20 +0000166 return this->INHERITED::clipPath(path, op, doAA);
167}
168
169bool SkLuaCanvas::clipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000170 AUTO_LUA("clipRegion");
reed@google.comdff7e112013-05-15 19:34:20 +0000171 return this->INHERITED::clipRegion(deviceRgn, op);
172}
173
174void SkLuaCanvas::drawPaint(const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000175 AUTO_LUA("drawPaint");
reed@google.com74ce6f02013-05-22 15:13:18 +0000176 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000177}
178
179void SkLuaCanvas::drawPoints(PointMode mode, size_t count,
180 const SkPoint pts[], const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000181 AUTO_LUA("drawPoints");
reed@google.com74ce6f02013-05-22 15:13:18 +0000182 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000183}
184
185void SkLuaCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000186 AUTO_LUA("drawOval");
reed@google.com74ce6f02013-05-22 15:13:18 +0000187 lua.pushRect(rect, "rect");
188 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000189}
190
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000191void SkLuaCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000192 AUTO_LUA("drawRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000193 lua.pushRect(rect, "rect");
194 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000195}
196
197void SkLuaCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000198 AUTO_LUA("drawRRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000199 lua.pushRRect(rrect, "rrect");
200 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000201}
202
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000203void SkLuaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000204 AUTO_LUA("drawPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000205 lua.pushPath(path, "path");
206 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000207}
208
209void SkLuaCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000210 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000211 AUTO_LUA("drawBitmap");
212 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000213 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000214 }
reed@google.comdff7e112013-05-15 19:34:20 +0000215}
216
217void SkLuaCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000218 const SkRect& dst, const SkPaint* paint,
219 DrawBitmapRectFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000220 AUTO_LUA("drawBitmapRectToRect");
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
226void SkLuaCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000227 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000228 AUTO_LUA("drawBitmapMatrix");
229 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
234void SkLuaCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
235 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000236 AUTO_LUA("drawSprite");
237 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000238 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000239 }
reed@google.comdff7e112013-05-15 19:34:20 +0000240}
241
242void SkLuaCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
243 SkScalar y, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000244 AUTO_LUA("drawText");
reed@google.come3823fd2013-05-30 18:55:14 +0000245 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000246 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000247}
248
249void SkLuaCanvas::drawPosText(const void* text, size_t byteLength,
250 const SkPoint pos[], const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000251 AUTO_LUA("drawPosText");
reed@google.come3823fd2013-05-30 18:55:14 +0000252 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000253 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000254}
255
256void SkLuaCanvas::drawPosTextH(const void* text, size_t byteLength,
257 const SkScalar xpos[], SkScalar constY,
258 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000259 AUTO_LUA("drawPosTextH");
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
264void SkLuaCanvas::drawTextOnPath(const void* text, size_t byteLength,
265 const SkPath& path, const SkMatrix* matrix,
266 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000267 AUTO_LUA("drawTextOnPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000268 lua.pushPath(path, "path");
reed@google.come3823fd2013-05-30 18:55:14 +0000269 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000270 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000271}
272
273void SkLuaCanvas::drawPicture(SkPicture& picture) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000274 AUTO_LUA("drawPicture");
reed@google.comdff7e112013-05-15 19:34:20 +0000275 // call through so we can see the nested picture ops
276 this->INHERITED::drawPicture(picture);
277}
278
279void SkLuaCanvas::drawVertices(VertexMode vmode, int vertexCount,
280 const SkPoint vertices[], const SkPoint texs[],
281 const SkColor colors[], SkXfermode* xmode,
282 const uint16_t indices[], int indexCount,
283 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000284 AUTO_LUA("drawVertices");
reed@google.com74ce6f02013-05-22 15:13:18 +0000285 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000286}
287
288void SkLuaCanvas::drawData(const void* data, size_t length) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000289 AUTO_LUA("drawData");
reed@google.comdff7e112013-05-15 19:34:20 +0000290}