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