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
didConcat44(const SkScalar m[16])113 void SkLuaCanvas::didConcat44(const SkScalar m[16]) {
114 // TODO
115 }
didScale(SkScalar x,SkScalar y)116 void SkLuaCanvas::didScale(SkScalar x, SkScalar y) {
117 this->didConcat(SkMatrix::MakeScale(x, y));
118 }
didTranslate(SkScalar x,SkScalar y)119 void SkLuaCanvas::didTranslate(SkScalar x, SkScalar y) {
120 this->didConcat(SkMatrix::MakeTrans(x, y));
121 }
didConcat(const SkMatrix & matrix)122 void SkLuaCanvas::didConcat(const SkMatrix& matrix) {
123 switch (matrix.getType()) {
124 case SkMatrix::kTranslate_Mask: {
125 AUTO_LUA("translate");
126 lua.pushScalar(matrix.getTranslateX(), "dx");
127 lua.pushScalar(matrix.getTranslateY(), "dy");
128 break;
129 }
130 case SkMatrix::kScale_Mask: {
131 AUTO_LUA("scale");
132 lua.pushScalar(matrix.getScaleX(), "sx");
133 lua.pushScalar(matrix.getScaleY(), "sy");
134 break;
135 }
136 default: {
137 AUTO_LUA("concat");
138 // pushMatrix added in https://codereview.chromium.org/203203004/
139 // Doesn't seem to have ever been working correctly since added
140 // lua.pushMatrix(matrix);
141 break;
142 }
143 }
144
145 this->INHERITED::didConcat(matrix);
146 }
147
didSetMatrix(const SkMatrix & matrix)148 void SkLuaCanvas::didSetMatrix(const SkMatrix& matrix) {
149 this->INHERITED::didSetMatrix(matrix);
150 }
151
onClipRect(const SkRect & r,SkClipOp op,ClipEdgeStyle edgeStyle)152 void SkLuaCanvas::onClipRect(const SkRect& r, SkClipOp op, ClipEdgeStyle edgeStyle) {
153 AUTO_LUA("clipRect");
154 lua.pushRect(r, "rect");
155 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
156 this->INHERITED::onClipRect(r, op, edgeStyle);
157 }
158
onClipRRect(const SkRRect & rrect,SkClipOp op,ClipEdgeStyle edgeStyle)159 void SkLuaCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
160 AUTO_LUA("clipRRect");
161 lua.pushRRect(rrect, "rrect");
162 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
163 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
164 }
165
onClipPath(const SkPath & path,SkClipOp op,ClipEdgeStyle edgeStyle)166 void SkLuaCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
167 AUTO_LUA("clipPath");
168 lua.pushPath(path, "path");
169 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
170 this->INHERITED::onClipPath(path, op, edgeStyle);
171 }
172
onClipRegion(const SkRegion & deviceRgn,SkClipOp op)173 void SkLuaCanvas::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {
174 AUTO_LUA("clipRegion");
175 this->INHERITED::onClipRegion(deviceRgn, op);
176 }
177
onDrawPaint(const SkPaint & paint)178 void SkLuaCanvas::onDrawPaint(const SkPaint& paint) {
179 AUTO_LUA("drawPaint");
180 lua.pushPaint(paint, "paint");
181 }
182
onDrawPoints(PointMode mode,size_t count,const SkPoint pts[],const SkPaint & paint)183 void SkLuaCanvas::onDrawPoints(PointMode mode, size_t count,
184 const SkPoint pts[], const SkPaint& paint) {
185 AUTO_LUA("drawPoints");
186 lua.pushArrayPoint(pts, SkToInt(count), "points");
187 lua.pushPaint(paint, "paint");
188 }
189
onDrawOval(const SkRect & rect,const SkPaint & paint)190 void SkLuaCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
191 AUTO_LUA("drawOval");
192 lua.pushRect(rect, "rect");
193 lua.pushPaint(paint, "paint");
194 }
195
onDrawArc(const SkRect & rect,SkScalar startAngle,SkScalar sweepAngle,bool useCenter,const SkPaint & paint)196 void SkLuaCanvas::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
197 bool useCenter, const SkPaint& paint) {
198 AUTO_LUA("drawArc");
199 lua.pushRect(rect, "rect");
200 lua.pushScalar(startAngle, "startAngle");
201 lua.pushScalar(sweepAngle, "sweepAngle");
202 lua.pushBool(useCenter, "useCenter");
203 lua.pushPaint(paint, "paint");
204 }
205
onDrawRect(const SkRect & rect,const SkPaint & paint)206 void SkLuaCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
207 AUTO_LUA("drawRect");
208 lua.pushRect(rect, "rect");
209 lua.pushPaint(paint, "paint");
210 }
211
onDrawRRect(const SkRRect & rrect,const SkPaint & paint)212 void SkLuaCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
213 AUTO_LUA("drawRRect");
214 lua.pushRRect(rrect, "rrect");
215 lua.pushPaint(paint, "paint");
216 }
217
onDrawDRRect(const SkRRect & outer,const SkRRect & inner,const SkPaint & paint)218 void SkLuaCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
219 const SkPaint& paint) {
220 AUTO_LUA("drawDRRect");
221 lua.pushRRect(outer, "outer");
222 lua.pushRRect(inner, "inner");
223 lua.pushPaint(paint, "paint");
224 }
225
onDrawPath(const SkPath & path,const SkPaint & paint)226 void SkLuaCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
227 AUTO_LUA("drawPath");
228 lua.pushPath(path, "path");
229 lua.pushPaint(paint, "paint");
230 }
231
onDrawBitmap(const SkBitmap & bitmap,SkScalar x,SkScalar y,const SkPaint * paint)232 void SkLuaCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
233 const SkPaint* paint) {
234 AUTO_LUA("drawBitmap");
235 if (paint) {
236 lua.pushPaint(*paint, "paint");
237 }
238 }
239
onDrawBitmapRect(const SkBitmap & bitmap,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint)240 void SkLuaCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
241 const SkPaint* paint, SrcRectConstraint) {
242 AUTO_LUA("drawBitmapRect");
243 if (paint) {
244 lua.pushPaint(*paint, "paint");
245 }
246 }
247
onDrawBitmapNine(const SkBitmap & bitmap,const SkIRect & center,const SkRect & dst,const SkPaint * paint)248 void SkLuaCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst,
249 const SkPaint* paint) {
250 AUTO_LUA("drawBitmapNine");
251 if (paint) {
252 lua.pushPaint(*paint, "paint");
253 }
254 }
255
onDrawImage(const SkImage * image,SkScalar x,SkScalar y,const SkPaint * paint)256 void SkLuaCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) {
257 AUTO_LUA("drawImage");
258 if (paint) {
259 lua.pushPaint(*paint, "paint");
260 }
261 }
262
onDrawImageRect(const SkImage * image,const SkRect * src,const SkRect & dst,const SkPaint * paint,SrcRectConstraint)263 void SkLuaCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
264 const SkPaint* paint, SrcRectConstraint) {
265 AUTO_LUA("drawImageRect");
266 if (paint) {
267 lua.pushPaint(*paint, "paint");
268 }
269 }
270
onDrawTextBlob(const SkTextBlob * blob,SkScalar x,SkScalar y,const SkPaint & paint)271 void SkLuaCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y,
272 const SkPaint &paint) {
273 AUTO_LUA("drawTextBlob");
274 lua.pushTextBlob(blob, "blob");
275 lua.pushScalar(x, "x");
276 lua.pushScalar(y, "y");
277 lua.pushPaint(paint, "paint");
278 }
279
onDrawPicture(const SkPicture * picture,const SkMatrix * matrix,const SkPaint * paint)280 void SkLuaCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
281 const SkPaint* paint) {
282 AUTO_LUA("drawPicture");
283 // call through so we can see the nested picture ops
284 this->INHERITED::onDrawPicture(picture, matrix, paint);
285 }
286
onDrawDrawable(SkDrawable * drawable,const SkMatrix * matrix)287 void SkLuaCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
288 AUTO_LUA("drawDrawable");
289 // call through so we can see the nested ops
290 this->INHERITED::onDrawDrawable(drawable, matrix);
291 }
292
onDrawVerticesObject(const SkVertices *,const SkVertices::Bone[],int,SkBlendMode,const SkPaint & paint)293 void SkLuaCanvas::onDrawVerticesObject(const SkVertices*, const SkVertices::Bone[], int,
294 SkBlendMode, const SkPaint& paint) {
295 AUTO_LUA("drawVertices");
296 lua.pushPaint(paint, "paint");
297 }
298