blob: 0f13073049e1797cd82ec173d8c235c2ea86e3ed [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 +000076SkLuaCanvas::SkLuaCanvas(int width, int height, lua_State* L, const char func[])
commit-bot@chromium.orge2543102014-01-31 19:42:58 +000077 : INHERITED(width, height)
reed@google.comdff7e112013-05-15 19:34:20 +000078 , fL(L)
79 , fFunc(func) {
80}
81
82SkLuaCanvas::~SkLuaCanvas() {}
83
84int SkLuaCanvas::save(SaveFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000085 AUTO_LUA("save");
reed@google.comdff7e112013-05-15 19:34:20 +000086 return this->INHERITED::save(flags);
87}
88
89int SkLuaCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
90 SaveFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000091 AUTO_LUA("saveLayer");
92 if (bounds) {
reed@google.com74ce6f02013-05-22 15:13:18 +000093 lua.pushRect(*bounds, "bounds");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000094 }
95 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +000096 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000097 }
reed@google.comdff7e112013-05-15 19:34:20 +000098 return this->INHERITED::save(flags);
99}
100
101void SkLuaCanvas::restore() {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000102 AUTO_LUA("restore");
reed@google.comdff7e112013-05-15 19:34:20 +0000103 this->INHERITED::restore();
104}
105
106bool SkLuaCanvas::translate(SkScalar dx, SkScalar dy) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000107 AUTO_LUA("translate");
reed@google.com74ce6f02013-05-22 15:13:18 +0000108 lua.pushScalar(dx, "dx");
109 lua.pushScalar(dy, "dy");
reed@google.comdff7e112013-05-15 19:34:20 +0000110 return this->INHERITED::translate(dx, dy);
111}
112
113bool SkLuaCanvas::scale(SkScalar sx, SkScalar sy) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000114 AUTO_LUA("scale");
reed@google.com74ce6f02013-05-22 15:13:18 +0000115 lua.pushScalar(sx, "sx");
116 lua.pushScalar(sy, "sy");
reed@google.comdff7e112013-05-15 19:34:20 +0000117 return this->INHERITED::scale(sx, sy);
118}
119
120bool SkLuaCanvas::rotate(SkScalar degrees) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000121 AUTO_LUA("rotate");
reed@google.com74ce6f02013-05-22 15:13:18 +0000122 lua.pushScalar(degrees, "degrees");
reed@google.comdff7e112013-05-15 19:34:20 +0000123 return this->INHERITED::rotate(degrees);
124}
125
reed@google.com74ce6f02013-05-22 15:13:18 +0000126bool SkLuaCanvas::skew(SkScalar kx, SkScalar ky) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000127 AUTO_LUA("skew");
reed@google.com74ce6f02013-05-22 15:13:18 +0000128 lua.pushScalar(kx, "kx");
129 lua.pushScalar(ky, "ky");
130 return this->INHERITED::skew(kx, ky);
reed@google.comdff7e112013-05-15 19:34:20 +0000131}
132
133bool SkLuaCanvas::concat(const SkMatrix& matrix) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000134 AUTO_LUA("concat");
reed@google.comdff7e112013-05-15 19:34:20 +0000135 return this->INHERITED::concat(matrix);
136}
137
138void SkLuaCanvas::setMatrix(const SkMatrix& matrix) {
139 this->INHERITED::setMatrix(matrix);
140}
141
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000142void SkLuaCanvas::onClipRect(const SkRect& r, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000143 AUTO_LUA("clipRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000144 lua.pushRect(r, "rect");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000145 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
146 this->INHERITED::onClipRect(r, op, edgeStyle);
reed@google.comdff7e112013-05-15 19:34:20 +0000147}
148
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000149void SkLuaCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000150 AUTO_LUA("clipRRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000151 lua.pushRRect(rrect, "rrect");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000152 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
153 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
reed@google.comdff7e112013-05-15 19:34:20 +0000154}
155
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000156void SkLuaCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000157 AUTO_LUA("clipPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000158 lua.pushPath(path, "path");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000159 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
160 this->INHERITED::onClipPath(path, op, edgeStyle);
reed@google.comdff7e112013-05-15 19:34:20 +0000161}
162
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000163void SkLuaCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000164 AUTO_LUA("clipRegion");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000165 this->INHERITED::onClipRegion(deviceRgn, op);
reed@google.comdff7e112013-05-15 19:34:20 +0000166}
167
168void SkLuaCanvas::drawPaint(const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000169 AUTO_LUA("drawPaint");
reed@google.com74ce6f02013-05-22 15:13:18 +0000170 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000171}
172
173void SkLuaCanvas::drawPoints(PointMode mode, size_t count,
174 const SkPoint pts[], const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000175 AUTO_LUA("drawPoints");
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::drawOval(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000180 AUTO_LUA("drawOval");
reed@google.com74ce6f02013-05-22 15:13:18 +0000181 lua.pushRect(rect, "rect");
182 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000183}
184
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000185void SkLuaCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000186 AUTO_LUA("drawRect");
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
191void SkLuaCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000192 AUTO_LUA("drawRRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000193 lua.pushRRect(rrect, "rrect");
194 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000195}
196
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000197void SkLuaCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
198 const SkPaint& paint) {
199 AUTO_LUA("drawDRRect");
200 lua.pushRRect(outer, "outer");
201 lua.pushRRect(inner, "inner");
202 lua.pushPaint(paint, "paint");
203}
204
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000205void SkLuaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000206 AUTO_LUA("drawPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000207 lua.pushPath(path, "path");
208 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000209}
210
211void SkLuaCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000212 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000213 AUTO_LUA("drawBitmap");
214 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000215 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000216 }
reed@google.comdff7e112013-05-15 19:34:20 +0000217}
218
219void SkLuaCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000220 const SkRect& dst, const SkPaint* paint,
221 DrawBitmapRectFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000222 AUTO_LUA("drawBitmapRectToRect");
223 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000224 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000225 }
reed@google.comdff7e112013-05-15 19:34:20 +0000226}
227
228void SkLuaCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000229 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000230 AUTO_LUA("drawBitmapMatrix");
231 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000232 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000233 }
reed@google.comdff7e112013-05-15 19:34:20 +0000234}
235
236void SkLuaCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
237 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000238 AUTO_LUA("drawSprite");
239 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000240 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000241 }
reed@google.comdff7e112013-05-15 19:34:20 +0000242}
243
244void SkLuaCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
245 SkScalar y, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000246 AUTO_LUA("drawText");
reed@google.come3823fd2013-05-30 18:55:14 +0000247 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000248 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000249}
250
251void SkLuaCanvas::drawPosText(const void* text, size_t byteLength,
252 const SkPoint pos[], const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000253 AUTO_LUA("drawPosText");
reed@google.come3823fd2013-05-30 18:55:14 +0000254 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000255 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000256}
257
258void SkLuaCanvas::drawPosTextH(const void* text, size_t byteLength,
259 const SkScalar xpos[], SkScalar constY,
260 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000261 AUTO_LUA("drawPosTextH");
reed@google.come3823fd2013-05-30 18:55:14 +0000262 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000263 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000264}
265
266void SkLuaCanvas::drawTextOnPath(const void* text, size_t byteLength,
267 const SkPath& path, const SkMatrix* matrix,
268 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000269 AUTO_LUA("drawTextOnPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000270 lua.pushPath(path, "path");
reed@google.come3823fd2013-05-30 18:55:14 +0000271 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000272 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000273}
274
275void SkLuaCanvas::drawPicture(SkPicture& picture) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000276 AUTO_LUA("drawPicture");
reed@google.comdff7e112013-05-15 19:34:20 +0000277 // call through so we can see the nested picture ops
278 this->INHERITED::drawPicture(picture);
279}
280
281void SkLuaCanvas::drawVertices(VertexMode vmode, int vertexCount,
282 const SkPoint vertices[], const SkPoint texs[],
283 const SkColor colors[], SkXfermode* xmode,
284 const uint16_t indices[], int indexCount,
285 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000286 AUTO_LUA("drawVertices");
reed@google.com74ce6f02013-05-22 15:13:18 +0000287 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000288}
289
290void SkLuaCanvas::drawData(const void* data, size_t length) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000291 AUTO_LUA("drawData");
reed@google.comdff7e112013-05-15 19:34:20 +0000292}