1/*
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"
9
10#include "SkLua.h"
11#include "SkStringUtils.h"
12
13extern "C" {
14    #include "lua.h"
15    #include "lauxlib.h"
16}
17
18class AutoCallLua : public SkLua {
19public:
20    AutoCallLua(lua_State* L, const char func[], const char verb[]) : INHERITED(L) {
21        lua_getglobal(L, func);
22        if (!lua_isfunction(L, -1)) {
23            int t = lua_type(L, -1);
24            SkDebugf("--- expected function %d\n", t);
25        }
26
27        lua_newtable(L);
28        this->pushString(verb, "verb");
29    }
30
31    ~AutoCallLua() {
32        lua_State* L = this->get();
33        if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
34            SkDebugf("lua err: %s\n", lua_tostring(L, -1));
35        }
36        lua_settop(L, -1);
37    }
38
39    void pushEncodedText(SkPaint::TextEncoding, const void*, size_t);
40
41private:
42    typedef SkLua INHERITED;
43};
44
45#define AUTO_LUA(verb)  AutoCallLua lua(fL, fFunc.c_str(), verb)
46
47
48///////////////////////////////////////////////////////////////////////////////
49
50void AutoCallLua::pushEncodedText(SkPaint::TextEncoding enc, const void* text,
51                                  size_t length) {
52    switch (enc) {
53        case SkPaint::kUTF8_TextEncoding:
54            this->pushString((const char*)text, length, "text");
55            break;
56        case SkPaint::kUTF16_TextEncoding:
57            this->pushString(SkStringFromUTF16((const uint16_t*)text, length), "text");
58            break;
59        case SkPaint::kGlyphID_TextEncoding:
60            this->pushArrayU16((const uint16_t*)text, SkToInt(length >> 1),
61                               "glyphs");
62            break;
63        case SkPaint::kUTF32_TextEncoding:
64            break;
65    }
66}
67
68///////////////////////////////////////////////////////////////////////////////
69
70void SkLuaCanvas::pushThis() {
71    SkLua(fL).pushCanvas(this);
72}
73
74///////////////////////////////////////////////////////////////////////////////
75
76SkLuaCanvas::SkLuaCanvas(int width, int height, lua_State* L, const char func[])
77    : INHERITED(width, height)
78    , fL(L)
79    , fFunc(func) {
80}
81
82SkLuaCanvas::~SkLuaCanvas() {}
83
84void SkLuaCanvas::willSave() {
85    AUTO_LUA("save");
86    this->INHERITED::willSave();
87}
88
89SkCanvas::SaveLayerStrategy SkLuaCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
90    AUTO_LUA("saveLayer");
91    if (rec.fBounds) {
92        lua.pushRect(*rec.fBounds, "bounds");
93    }
94    if (rec.fPaint) {
95        lua.pushPaint(*rec.fPaint, "paint");
96    }
97
98    (void)this->INHERITED::getSaveLayerStrategy(rec);
99    // No need for a layer.
100    return kNoLayer_SaveLayerStrategy;
101}
102
103void SkLuaCanvas::willRestore() {
104    AUTO_LUA("restore");
105    this->INHERITED::willRestore();
106}
107
108void SkLuaCanvas::didConcat(const SkMatrix& matrix) {
109    switch (matrix.getType()) {
110        case SkMatrix::kTranslate_Mask: {
111            AUTO_LUA("translate");
112            lua.pushScalar(matrix.getTranslateX(), "dx");
113            lua.pushScalar(matrix.getTranslateY(), "dy");
114            break;
115        }
116        case SkMatrix::kScale_Mask: {
117            AUTO_LUA("scale");
118            lua.pushScalar(matrix.getScaleX(), "sx");
119            lua.pushScalar(matrix.getScaleY(), "sy");
120            break;
121        }
122        default: {
123            AUTO_LUA("concat");
124            // pushMatrix added in https://codereview.chromium.org/203203004/
125            // Doesn't seem to have ever been working correctly since added
126            // lua.pushMatrix(matrix);
127            break;
128        }
129    }
130
131    this->INHERITED::didConcat(matrix);
132}
133
134void SkLuaCanvas::didSetMatrix(const SkMatrix& matrix) {
135    this->INHERITED::didSetMatrix(matrix);
136}
137
138void SkLuaCanvas::onClipRect(const SkRect& r, SkClipOp op, ClipEdgeStyle edgeStyle) {
139    AUTO_LUA("clipRect");
140    lua.pushRect(r, "rect");
141    lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
142    this->INHERITED::onClipRect(r, op, edgeStyle);
143}
144
145void SkLuaCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
146    AUTO_LUA("clipRRect");
147    lua.pushRRect(rrect, "rrect");
148    lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
149    this->INHERITED::onClipRRect(rrect, op, edgeStyle);
150}
151
152void SkLuaCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
153    AUTO_LUA("clipPath");
154    lua.pushPath(path, "path");
155    lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
156    this->INHERITED::onClipPath(path, op, edgeStyle);
157}
158
159void SkLuaCanvas::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {
160    AUTO_LUA("clipRegion");
161    this->INHERITED::onClipRegion(deviceRgn, op);
162}
163
164void SkLuaCanvas::onDrawPaint(const SkPaint& paint) {
165    AUTO_LUA("drawPaint");
166    lua.pushPaint(paint, "paint");
167}
168
169void SkLuaCanvas::onDrawPoints(PointMode mode, size_t count,
170                               const SkPoint pts[], const SkPaint& paint) {
171    AUTO_LUA("drawPoints");
172    lua.pushArrayPoint(pts, SkToInt(count), "points");
173    lua.pushPaint(paint, "paint");
174}
175
176void SkLuaCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
177    AUTO_LUA("drawOval");
178    lua.pushRect(rect, "rect");
179    lua.pushPaint(paint, "paint");
180}
181
182void SkLuaCanvas::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
183                            bool useCenter, const SkPaint& paint) {
184    AUTO_LUA("drawArc");
185    lua.pushRect(rect, "rect");
186    lua.pushScalar(startAngle, "startAngle");
187    lua.pushScalar(sweepAngle, "sweepAngle");
188    lua.pushBool(useCenter, "useCenter");
189    lua.pushPaint(paint, "paint");
190}
191
192void SkLuaCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
193    AUTO_LUA("drawRect");
194    lua.pushRect(rect, "rect");
195    lua.pushPaint(paint, "paint");
196}
197
198void SkLuaCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
199    AUTO_LUA("drawRRect");
200    lua.pushRRect(rrect, "rrect");
201    lua.pushPaint(paint, "paint");
202}
203
204void SkLuaCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
205                               const SkPaint& paint) {
206    AUTO_LUA("drawDRRect");
207    lua.pushRRect(outer, "outer");
208    lua.pushRRect(inner, "inner");
209    lua.pushPaint(paint, "paint");
210}
211
212void SkLuaCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
213    AUTO_LUA("drawPath");
214    lua.pushPath(path, "path");
215    lua.pushPaint(paint, "paint");
216}
217
218void SkLuaCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
219                               const SkPaint* paint) {
220    AUTO_LUA("drawBitmap");
221    if (paint) {
222        lua.pushPaint(*paint, "paint");
223    }
224}
225
226void SkLuaCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
227                                   const SkPaint* paint, SrcRectConstraint) {
228    AUTO_LUA("drawBitmapRect");
229    if (paint) {
230        lua.pushPaint(*paint, "paint");
231    }
232}
233
234void SkLuaCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst,
235                                   const SkPaint* paint) {
236    AUTO_LUA("drawBitmapNine");
237    if (paint) {
238        lua.pushPaint(*paint, "paint");
239    }
240}
241
242void SkLuaCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) {
243    AUTO_LUA("drawImage");
244    if (paint) {
245        lua.pushPaint(*paint, "paint");
246    }
247}
248
249void SkLuaCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
250                                  const SkPaint* paint, SrcRectConstraint) {
251    AUTO_LUA("drawImageRect");
252    if (paint) {
253        lua.pushPaint(*paint, "paint");
254    }
255}
256
257void SkLuaCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
258                             const SkPaint& paint) {
259    AUTO_LUA("drawText");
260    lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
261    lua.pushPaint(paint, "paint");
262}
263
264void SkLuaCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
265                                const SkPaint& paint) {
266    AUTO_LUA("drawPosText");
267    lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
268    lua.pushPaint(paint, "paint");
269}
270
271void SkLuaCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
272                                 SkScalar constY, const SkPaint& paint) {
273    AUTO_LUA("drawPosTextH");
274    lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
275    lua.pushPaint(paint, "paint");
276}
277
278void SkLuaCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
279                                   const SkMatrix* matrix, const SkPaint& paint) {
280    AUTO_LUA("drawTextOnPath");
281    lua.pushPath(path, "path");
282    lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
283    lua.pushPaint(paint, "paint");
284}
285
286void SkLuaCanvas::onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[],
287                                    const SkRect* cull, const SkPaint& paint) {
288    AUTO_LUA("drawTextRSXform");
289    lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
290    // TODO: export other params
291    lua.pushPaint(paint, "paint");
292}
293
294void SkLuaCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y,
295                                 const SkPaint &paint) {
296    AUTO_LUA("drawTextBlob");
297    lua.pushTextBlob(blob, "blob");
298    lua.pushScalar(x, "x");
299    lua.pushScalar(y, "y");
300    lua.pushPaint(paint, "paint");
301}
302
303void SkLuaCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
304                                const SkPaint* paint) {
305    AUTO_LUA("drawPicture");
306    // call through so we can see the nested picture ops
307    this->INHERITED::onDrawPicture(picture, matrix, paint);
308}
309
310void SkLuaCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
311    AUTO_LUA("drawDrawable");
312    // call through so we can see the nested ops
313    this->INHERITED::onDrawDrawable(drawable, matrix);
314}
315
316void SkLuaCanvas::onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint& paint) {
317    AUTO_LUA("drawVertices");
318    lua.pushPaint(paint, "paint");
319}
320