• 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 "include/utils/SkLuaCanvas.h"
9 
10 #include "include/private/SkTo.h"
11 #include "include/utils/SkLua.h"
12 #include "src/core/SkStringUtils.h"
13 
14 extern "C" {
15     #include "lua.h"
16     #include "lauxlib.h"
17 }
18 
19 class AutoCallLua : public SkLua {
20 public:
AutoCallLua(lua_State * L,const char func[],const char verb[])21     AutoCallLua(lua_State* L, const char func[], const char verb[]) : INHERITED(L) {
22         lua_getglobal(L, func);
23         if (!lua_isfunction(L, -1)) {
24             int t = lua_type(L, -1);
25             SkDebugf("--- expected function %d\n", t);
26         }
27 
28         lua_newtable(L);
29         this->pushString(verb, "verb");
30     }
31 
~AutoCallLua()32     ~AutoCallLua() {
33         lua_State* L = this->get();
34         if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
35             SkDebugf("lua err: %s\n", lua_tostring(L, -1));
36         }
37         lua_settop(L, -1);
38     }
39 
40     void pushEncodedText(SkTextEncoding, const void*, size_t);
41 
42 private:
43     typedef SkLua INHERITED;
44 };
45 
46 #define AUTO_LUA(verb)  AutoCallLua lua(fL, fFunc.c_str(), verb)
47 
48 
49 ///////////////////////////////////////////////////////////////////////////////
50 
pushEncodedText(SkTextEncoding enc,const void * text,size_t length)51 void AutoCallLua::pushEncodedText(SkTextEncoding enc, const void* text, size_t length) {
52     switch (enc) {
53         case SkTextEncoding::kUTF8:
54             this->pushString((const char*)text, length, "text");
55             break;
56         case SkTextEncoding::kUTF16:
57             this->pushString(SkStringFromUTF16((const uint16_t*)text, length), "text");
58             break;
59         case SkTextEncoding::kGlyphID:
60             this->pushArrayU16((const uint16_t*)text, SkToInt(length >> 1),
61                                "glyphs");
62             break;
63         case SkTextEncoding::kUTF32:
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 
getSaveLayerStrategy(const SaveLayerRec & rec)89 SkCanvas::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 
onDoSaveBehind(const SkRect *)103 bool SkLuaCanvas::onDoSaveBehind(const SkRect*) {
104     // TODO
105     return false;
106 }
107 
willRestore()108 void SkLuaCanvas::willRestore() {
109     AUTO_LUA("restore");
110     this->INHERITED::willRestore();
111 }
112 
didConcat(const SkMatrix & matrix)113 void SkLuaCanvas::didConcat(const SkMatrix& matrix) {
114     switch (matrix.getType()) {
115         case SkMatrix::kTranslate_Mask: {
116             AUTO_LUA("translate");
117             lua.pushScalar(matrix.getTranslateX(), "dx");
118             lua.pushScalar(matrix.getTranslateY(), "dy");
119             break;
120         }
121         case SkMatrix::kScale_Mask: {
122             AUTO_LUA("scale");
123             lua.pushScalar(matrix.getScaleX(), "sx");
124             lua.pushScalar(matrix.getScaleY(), "sy");
125             break;
126         }
127         default: {
128             AUTO_LUA("concat");
129             // pushMatrix added in https://codereview.chromium.org/203203004/
130             // Doesn't seem to have ever been working correctly since added
131             // lua.pushMatrix(matrix);
132             break;
133         }
134     }
135 
136     this->INHERITED::didConcat(matrix);
137 }
138 
didSetMatrix(const SkMatrix & matrix)139 void SkLuaCanvas::didSetMatrix(const SkMatrix& matrix) {
140     this->INHERITED::didSetMatrix(matrix);
141 }
142 
onClipRect(const SkRect & r,SkClipOp op,ClipEdgeStyle edgeStyle)143 void SkLuaCanvas::onClipRect(const SkRect& r, SkClipOp op, ClipEdgeStyle edgeStyle) {
144     AUTO_LUA("clipRect");
145     lua.pushRect(r, "rect");
146     lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
147     this->INHERITED::onClipRect(r, op, edgeStyle);
148 }
149 
onClipRRect(const SkRRect & rrect,SkClipOp op,ClipEdgeStyle edgeStyle)150 void SkLuaCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
151     AUTO_LUA("clipRRect");
152     lua.pushRRect(rrect, "rrect");
153     lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
154     this->INHERITED::onClipRRect(rrect, op, edgeStyle);
155 }
156 
onClipPath(const SkPath & path,SkClipOp op,ClipEdgeStyle edgeStyle)157 void SkLuaCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
158     AUTO_LUA("clipPath");
159     lua.pushPath(path, "path");
160     lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
161     this->INHERITED::onClipPath(path, op, edgeStyle);
162 }
163 
onClipRegion(const SkRegion & deviceRgn,SkClipOp op)164 void SkLuaCanvas::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {
165     AUTO_LUA("clipRegion");
166     this->INHERITED::onClipRegion(deviceRgn, op);
167 }
168 
onDrawPaint(const SkPaint & paint)169 void SkLuaCanvas::onDrawPaint(const SkPaint& paint) {
170     AUTO_LUA("drawPaint");
171     lua.pushPaint(paint, "paint");
172 }
173 
onDrawPoints(PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)174 void SkLuaCanvas::onDrawPoints(PointMode mode, size_t count,
175                                const SkPoint pts[], const SkPaint& paint) {
176     AUTO_LUA("drawPoints");
177     lua.pushArrayPoint(pts, SkToInt(count), "points");
178     lua.pushPaint(paint, "paint");
179 }
180 
onDrawOval(const SkRect & rect,const SkPaint & paint)181 void SkLuaCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
182     AUTO_LUA("drawOval");
183     lua.pushRect(rect, "rect");
184     lua.pushPaint(paint, "paint");
185 }
186 
onDrawArc(const SkRect & rect,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)187 void SkLuaCanvas::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
188                             bool useCenter, const SkPaint& paint) {
189     AUTO_LUA("drawArc");
190     lua.pushRect(rect, "rect");
191     lua.pushScalar(startAngle, "startAngle");
192     lua.pushScalar(sweepAngle, "sweepAngle");
193     lua.pushBool(useCenter, "useCenter");
194     lua.pushPaint(paint, "paint");
195 }
196 
onDrawRect(const SkRect & rect,const SkPaint & paint)197 void SkLuaCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
198     AUTO_LUA("drawRect");
199     lua.pushRect(rect, "rect");
200     lua.pushPaint(paint, "paint");
201 }
202 
onDrawRRect(const SkRRect & rrect,const SkPaint & paint)203 void SkLuaCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
204     AUTO_LUA("drawRRect");
205     lua.pushRRect(rrect, "rrect");
206     lua.pushPaint(paint, "paint");
207 }
208 
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)209 void SkLuaCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
210                                const SkPaint& paint) {
211     AUTO_LUA("drawDRRect");
212     lua.pushRRect(outer, "outer");
213     lua.pushRRect(inner, "inner");
214     lua.pushPaint(paint, "paint");
215 }
216 
onDrawPath(const SkPath & path,const SkPaint & paint)217 void SkLuaCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
218     AUTO_LUA("drawPath");
219     lua.pushPath(path, "path");
220     lua.pushPaint(paint, "paint");
221 }
222 
onDrawBitmap(const SkBitmap & bitmap,SkScalar x,SkScalar y,const SkPaint * paint)223 void SkLuaCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
224                                const SkPaint* paint) {
225     AUTO_LUA("drawBitmap");
226     if (paint) {
227         lua.pushPaint(*paint, "paint");
228     }
229 }
230 
onDrawBitmapRect(const SkBitmap & bitmap,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint)231 void SkLuaCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
232                                    const SkPaint* paint, SrcRectConstraint) {
233     AUTO_LUA("drawBitmapRect");
234     if (paint) {
235         lua.pushPaint(*paint, "paint");
236     }
237 }
238 
onDrawBitmapNine(const SkBitmap & bitmap,const SkIRect & center,const SkRect & dst,const SkPaint * paint)239 void SkLuaCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst,
240                                    const SkPaint* paint) {
241     AUTO_LUA("drawBitmapNine");
242     if (paint) {
243         lua.pushPaint(*paint, "paint");
244     }
245 }
246 
onDrawImage(const SkImage * image,SkScalar x,SkScalar y,const SkPaint * paint)247 void SkLuaCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) {
248     AUTO_LUA("drawImage");
249     if (paint) {
250         lua.pushPaint(*paint, "paint");
251     }
252 }
253 
onDrawImageRect(const SkImage * image,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint)254 void SkLuaCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
255                                   const SkPaint* paint, SrcRectConstraint) {
256     AUTO_LUA("drawImageRect");
257     if (paint) {
258         lua.pushPaint(*paint, "paint");
259     }
260 }
261 
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)262 void SkLuaCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y,
263                                  const SkPaint &paint) {
264     AUTO_LUA("drawTextBlob");
265     lua.pushTextBlob(blob, "blob");
266     lua.pushScalar(x, "x");
267     lua.pushScalar(y, "y");
268     lua.pushPaint(paint, "paint");
269 }
270 
onDrawPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)271 void SkLuaCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
272                                 const SkPaint* paint) {
273     AUTO_LUA("drawPicture");
274     // call through so we can see the nested picture ops
275     this->INHERITED::onDrawPicture(picture, matrix, paint);
276 }
277 
onDrawDrawable(SkDrawable * drawable,const SkMatrix * matrix)278 void SkLuaCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
279     AUTO_LUA("drawDrawable");
280     // call through so we can see the nested ops
281     this->INHERITED::onDrawDrawable(drawable, matrix);
282 }
283 
onDrawVerticesObject(const SkVertices *,const SkVertices::Bone[],int,SkBlendMode,const SkPaint & paint)284 void SkLuaCanvas::onDrawVerticesObject(const SkVertices*, const SkVertices::Bone[], int,
285                                        SkBlendMode, const SkPaint& paint) {
286     AUTO_LUA("drawVertices");
287     lua.pushPaint(paint, "paint");
288 }
289