blob: be6cb6624a751ff3ae60f6111b68b74a927395dc [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.com74ce6f02013-05-22 15:13:18 +000030 lua_State* L = this->getL();
31 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
37private:
reed@google.com74ce6f02013-05-22 15:13:18 +000038 typedef SkLua INHERITED;
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000039};
40
reed@google.com74ce6f02013-05-22 15:13:18 +000041#define AUTO_LUA(verb) AutoCallLua lua(fL, fFunc.c_str(), verb)
reed@google.com9a731042013-05-21 17:52:33 +000042
43///////////////////////////////////////////////////////////////////////////////
44
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000045void SkLuaCanvas::pushThis() {
reed@google.com74ce6f02013-05-22 15:13:18 +000046 SkLua(fL).pushCanvas(this);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000047}
48
49///////////////////////////////////////////////////////////////////////////////
50
reed@google.comdff7e112013-05-15 19:34:20 +000051static SkBitmap make_bm(int width, int height) {
52 SkBitmap bm;
53 bm.setConfig(SkBitmap::kNo_Config, width, height);
54 return bm;
55}
56
57SkLuaCanvas::SkLuaCanvas(int width, int height, lua_State* L, const char func[])
58 : INHERITED(make_bm(width, height))
59 , fL(L)
60 , fFunc(func) {
61}
62
63SkLuaCanvas::~SkLuaCanvas() {}
64
65int SkLuaCanvas::save(SaveFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000066 AUTO_LUA("save");
reed@google.comdff7e112013-05-15 19:34:20 +000067 return this->INHERITED::save(flags);
68}
69
70int SkLuaCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
71 SaveFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000072 AUTO_LUA("saveLayer");
73 if (bounds) {
reed@google.com74ce6f02013-05-22 15:13:18 +000074 lua.pushRect(*bounds, "bounds");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000075 }
76 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +000077 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000078 }
reed@google.comdff7e112013-05-15 19:34:20 +000079 return this->INHERITED::save(flags);
80}
81
82void SkLuaCanvas::restore() {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000083 AUTO_LUA("restore");
reed@google.comdff7e112013-05-15 19:34:20 +000084 this->INHERITED::restore();
85}
86
87bool SkLuaCanvas::translate(SkScalar dx, SkScalar dy) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000088 AUTO_LUA("translate");
reed@google.com74ce6f02013-05-22 15:13:18 +000089 lua.pushScalar(dx, "dx");
90 lua.pushScalar(dy, "dy");
reed@google.comdff7e112013-05-15 19:34:20 +000091 return this->INHERITED::translate(dx, dy);
92}
93
94bool SkLuaCanvas::scale(SkScalar sx, SkScalar sy) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000095 AUTO_LUA("scale");
reed@google.com74ce6f02013-05-22 15:13:18 +000096 lua.pushScalar(sx, "sx");
97 lua.pushScalar(sy, "sy");
reed@google.comdff7e112013-05-15 19:34:20 +000098 return this->INHERITED::scale(sx, sy);
99}
100
101bool SkLuaCanvas::rotate(SkScalar degrees) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000102 AUTO_LUA("rotate");
reed@google.com74ce6f02013-05-22 15:13:18 +0000103 lua.pushScalar(degrees, "degrees");
reed@google.comdff7e112013-05-15 19:34:20 +0000104 return this->INHERITED::rotate(degrees);
105}
106
reed@google.com74ce6f02013-05-22 15:13:18 +0000107bool SkLuaCanvas::skew(SkScalar kx, SkScalar ky) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000108 AUTO_LUA("skew");
reed@google.com74ce6f02013-05-22 15:13:18 +0000109 lua.pushScalar(kx, "kx");
110 lua.pushScalar(ky, "ky");
111 return this->INHERITED::skew(kx, ky);
reed@google.comdff7e112013-05-15 19:34:20 +0000112}
113
114bool SkLuaCanvas::concat(const SkMatrix& matrix) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000115 AUTO_LUA("concat");
reed@google.comdff7e112013-05-15 19:34:20 +0000116 return this->INHERITED::concat(matrix);
117}
118
119void SkLuaCanvas::setMatrix(const SkMatrix& matrix) {
120 this->INHERITED::setMatrix(matrix);
121}
122
123bool SkLuaCanvas::clipRect(const SkRect& r, SkRegion::Op op, bool doAA) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000124 AUTO_LUA("clipRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000125 lua.pushRect(r, "rect");
126 lua.pushBool(doAA, "aa");
reed@google.comdff7e112013-05-15 19:34:20 +0000127 return this->INHERITED::clipRect(r, op, doAA);
128}
129
130bool SkLuaCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000131 AUTO_LUA("clipRRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000132 lua.pushRRect(rrect, "rrect");
133 lua.pushBool(doAA, "aa");
reed@google.comdff7e112013-05-15 19:34:20 +0000134 return this->INHERITED::clipRRect(rrect, op, doAA);
135}
136
137bool SkLuaCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000138 AUTO_LUA("clipPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000139 lua.pushPath(path, "path");
140 lua.pushBool(doAA, "aa");
reed@google.comdff7e112013-05-15 19:34:20 +0000141 return this->INHERITED::clipPath(path, op, doAA);
142}
143
144bool SkLuaCanvas::clipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000145 AUTO_LUA("clipRegion");
reed@google.comdff7e112013-05-15 19:34:20 +0000146 return this->INHERITED::clipRegion(deviceRgn, op);
147}
148
149void SkLuaCanvas::drawPaint(const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000150 AUTO_LUA("drawPaint");
reed@google.com74ce6f02013-05-22 15:13:18 +0000151 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000152}
153
154void SkLuaCanvas::drawPoints(PointMode mode, size_t count,
155 const SkPoint pts[], const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000156 AUTO_LUA("drawPoints");
reed@google.com74ce6f02013-05-22 15:13:18 +0000157 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000158}
159
160void SkLuaCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000161 AUTO_LUA("drawOval");
reed@google.com74ce6f02013-05-22 15:13:18 +0000162 lua.pushRect(rect, "rect");
163 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000164}
165
166void SkLuaCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000167 AUTO_LUA("drawRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000168 lua.pushRect(rect, "rect");
169 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000170}
171
172void SkLuaCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000173 AUTO_LUA("drawRRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000174 lua.pushRRect(rrect, "rrect");
175 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000176}
177
178void SkLuaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000179 AUTO_LUA("drawPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000180 lua.pushPath(path, "path");
181 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000182}
183
184void SkLuaCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
185 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000186 AUTO_LUA("drawBitmap");
187 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000188 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000189 }
reed@google.comdff7e112013-05-15 19:34:20 +0000190}
191
192void SkLuaCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
193 const SkRect& dst, const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000194 AUTO_LUA("drawBitmapRectToRect");
195 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000196 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000197 }
reed@google.comdff7e112013-05-15 19:34:20 +0000198}
199
200void SkLuaCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
201 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000202 AUTO_LUA("drawBitmapMatrix");
203 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000204 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000205 }
reed@google.comdff7e112013-05-15 19:34:20 +0000206}
207
208void SkLuaCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
209 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000210 AUTO_LUA("drawSprite");
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::drawText(const void* text, size_t byteLength, SkScalar x,
217 SkScalar y, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000218 AUTO_LUA("drawText");
reed@google.com74ce6f02013-05-22 15:13:18 +0000219 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000220}
221
222void SkLuaCanvas::drawPosText(const void* text, size_t byteLength,
223 const SkPoint pos[], const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000224 AUTO_LUA("drawPosText");
reed@google.com74ce6f02013-05-22 15:13:18 +0000225 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000226}
227
228void SkLuaCanvas::drawPosTextH(const void* text, size_t byteLength,
229 const SkScalar xpos[], SkScalar constY,
230 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000231 AUTO_LUA("drawPosTextH");
reed@google.com74ce6f02013-05-22 15:13:18 +0000232 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000233}
234
235void SkLuaCanvas::drawTextOnPath(const void* text, size_t byteLength,
236 const SkPath& path, const SkMatrix* matrix,
237 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000238 AUTO_LUA("drawTextOnPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000239 lua.pushPath(path, "path");
240 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000241}
242
243void SkLuaCanvas::drawPicture(SkPicture& picture) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000244 AUTO_LUA("drawPicture");
reed@google.comdff7e112013-05-15 19:34:20 +0000245 // call through so we can see the nested picture ops
246 this->INHERITED::drawPicture(picture);
247}
248
249void SkLuaCanvas::drawVertices(VertexMode vmode, int vertexCount,
250 const SkPoint vertices[], const SkPoint texs[],
251 const SkColor colors[], SkXfermode* xmode,
252 const uint16_t indices[], int indexCount,
253 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000254 AUTO_LUA("drawVertices");
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::drawData(const void* data, size_t length) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000259 AUTO_LUA("drawData");
reed@google.comdff7e112013-05-15 19:34:20 +0000260}