|
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: |
|
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 |
|
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 |
|
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 |
|
70 void SkLuaCanvas::pushThis() { |
|
71 SkLua(fL).pushCanvas(this); |
|
72 } |
|
73 |
|
74 /////////////////////////////////////////////////////////////////////////////// |
|
75 |
|
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 |
|
82 SkLuaCanvas::~SkLuaCanvas() {} |
|
83 |
|
84 void SkLuaCanvas::willSave(SaveFlags flags) { |
|
85 AUTO_LUA("save"); |
|
86 this->INHERITED::willSave(flags); |
|
87 } |
|
88 |
|
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 |
|
104 void SkLuaCanvas::willRestore() { |
|
105 AUTO_LUA("restore"); |
|
106 this->INHERITED::willRestore(); |
|
107 } |
|
108 |
|
109 void SkLuaCanvas::didTranslate(SkScalar dx, SkScalar dy) { |
|
110 AUTO_LUA("translate"); |
|
111 lua.pushScalar(dx, "dx"); |
|
112 lua.pushScalar(dy, "dy"); |
|
113 this->INHERITED::didTranslate(dx, dy); |
|
114 } |
|
115 |
|
116 void SkLuaCanvas::didScale(SkScalar sx, SkScalar sy) { |
|
117 AUTO_LUA("scale"); |
|
118 lua.pushScalar(sx, "sx"); |
|
119 lua.pushScalar(sy, "sy"); |
|
120 this->INHERITED::didScale(sx, sy); |
|
121 } |
|
122 |
|
123 void SkLuaCanvas::didRotate(SkScalar degrees) { |
|
124 AUTO_LUA("rotate"); |
|
125 lua.pushScalar(degrees, "degrees"); |
|
126 this->INHERITED::didRotate(degrees); |
|
127 } |
|
128 |
|
129 void SkLuaCanvas::didSkew(SkScalar kx, SkScalar ky) { |
|
130 AUTO_LUA("skew"); |
|
131 lua.pushScalar(kx, "kx"); |
|
132 lua.pushScalar(ky, "ky"); |
|
133 this->INHERITED::didSkew(kx, ky); |
|
134 } |
|
135 |
|
136 void SkLuaCanvas::didConcat(const SkMatrix& matrix) { |
|
137 AUTO_LUA("concat"); |
|
138 this->INHERITED::didConcat(matrix); |
|
139 } |
|
140 |
|
141 void SkLuaCanvas::didSetMatrix(const SkMatrix& matrix) { |
|
142 this->INHERITED::didSetMatrix(matrix); |
|
143 } |
|
144 |
|
145 void SkLuaCanvas::onClipRect(const SkRect& r, SkRegion::Op op, ClipEdgeStyle edgeStyle) { |
|
146 AUTO_LUA("clipRect"); |
|
147 lua.pushRect(r, "rect"); |
|
148 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa"); |
|
149 this->INHERITED::onClipRect(r, op, edgeStyle); |
|
150 } |
|
151 |
|
152 void SkLuaCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) { |
|
153 AUTO_LUA("clipRRect"); |
|
154 lua.pushRRect(rrect, "rrect"); |
|
155 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa"); |
|
156 this->INHERITED::onClipRRect(rrect, op, edgeStyle); |
|
157 } |
|
158 |
|
159 void SkLuaCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) { |
|
160 AUTO_LUA("clipPath"); |
|
161 lua.pushPath(path, "path"); |
|
162 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa"); |
|
163 this->INHERITED::onClipPath(path, op, edgeStyle); |
|
164 } |
|
165 |
|
166 void SkLuaCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) { |
|
167 AUTO_LUA("clipRegion"); |
|
168 this->INHERITED::onClipRegion(deviceRgn, op); |
|
169 } |
|
170 |
|
171 void SkLuaCanvas::drawPaint(const SkPaint& paint) { |
|
172 AUTO_LUA("drawPaint"); |
|
173 lua.pushPaint(paint, "paint"); |
|
174 } |
|
175 |
|
176 void SkLuaCanvas::drawPoints(PointMode mode, size_t count, |
|
177 const SkPoint pts[], const SkPaint& paint) { |
|
178 AUTO_LUA("drawPoints"); |
|
179 lua.pushPaint(paint, "paint"); |
|
180 } |
|
181 |
|
182 void SkLuaCanvas::drawOval(const SkRect& rect, const SkPaint& paint) { |
|
183 AUTO_LUA("drawOval"); |
|
184 lua.pushRect(rect, "rect"); |
|
185 lua.pushPaint(paint, "paint"); |
|
186 } |
|
187 |
|
188 void SkLuaCanvas::drawRect(const SkRect& rect, const SkPaint& paint) { |
|
189 AUTO_LUA("drawRect"); |
|
190 lua.pushRect(rect, "rect"); |
|
191 lua.pushPaint(paint, "paint"); |
|
192 } |
|
193 |
|
194 void SkLuaCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
|
195 AUTO_LUA("drawRRect"); |
|
196 lua.pushRRect(rrect, "rrect"); |
|
197 lua.pushPaint(paint, "paint"); |
|
198 } |
|
199 |
|
200 void SkLuaCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, |
|
201 const SkPaint& paint) { |
|
202 AUTO_LUA("drawDRRect"); |
|
203 lua.pushRRect(outer, "outer"); |
|
204 lua.pushRRect(inner, "inner"); |
|
205 lua.pushPaint(paint, "paint"); |
|
206 } |
|
207 |
|
208 void SkLuaCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
|
209 AUTO_LUA("drawPath"); |
|
210 lua.pushPath(path, "path"); |
|
211 lua.pushPaint(paint, "paint"); |
|
212 } |
|
213 |
|
214 void SkLuaCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y, |
|
215 const SkPaint* paint) { |
|
216 AUTO_LUA("drawBitmap"); |
|
217 if (paint) { |
|
218 lua.pushPaint(*paint, "paint"); |
|
219 } |
|
220 } |
|
221 |
|
222 void SkLuaCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src, |
|
223 const SkRect& dst, const SkPaint* paint, |
|
224 DrawBitmapRectFlags flags) { |
|
225 AUTO_LUA("drawBitmapRectToRect"); |
|
226 if (paint) { |
|
227 lua.pushPaint(*paint, "paint"); |
|
228 } |
|
229 } |
|
230 |
|
231 void SkLuaCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m, |
|
232 const SkPaint* paint) { |
|
233 AUTO_LUA("drawBitmapMatrix"); |
|
234 if (paint) { |
|
235 lua.pushPaint(*paint, "paint"); |
|
236 } |
|
237 } |
|
238 |
|
239 void SkLuaCanvas::drawSprite(const SkBitmap& bitmap, int x, int y, |
|
240 const SkPaint* paint) { |
|
241 AUTO_LUA("drawSprite"); |
|
242 if (paint) { |
|
243 lua.pushPaint(*paint, "paint"); |
|
244 } |
|
245 } |
|
246 |
|
247 void SkLuaCanvas::drawText(const void* text, size_t byteLength, SkScalar x, |
|
248 SkScalar y, const SkPaint& paint) { |
|
249 AUTO_LUA("drawText"); |
|
250 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength); |
|
251 lua.pushPaint(paint, "paint"); |
|
252 } |
|
253 |
|
254 void SkLuaCanvas::drawPosText(const void* text, size_t byteLength, |
|
255 const SkPoint pos[], const SkPaint& paint) { |
|
256 AUTO_LUA("drawPosText"); |
|
257 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength); |
|
258 lua.pushPaint(paint, "paint"); |
|
259 } |
|
260 |
|
261 void SkLuaCanvas::drawPosTextH(const void* text, size_t byteLength, |
|
262 const SkScalar xpos[], SkScalar constY, |
|
263 const SkPaint& paint) { |
|
264 AUTO_LUA("drawPosTextH"); |
|
265 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength); |
|
266 lua.pushPaint(paint, "paint"); |
|
267 } |
|
268 |
|
269 void SkLuaCanvas::drawTextOnPath(const void* text, size_t byteLength, |
|
270 const SkPath& path, const SkMatrix* matrix, |
|
271 const SkPaint& paint) { |
|
272 AUTO_LUA("drawTextOnPath"); |
|
273 lua.pushPath(path, "path"); |
|
274 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength); |
|
275 lua.pushPaint(paint, "paint"); |
|
276 } |
|
277 |
|
278 void SkLuaCanvas::drawPicture(SkPicture& picture) { |
|
279 AUTO_LUA("drawPicture"); |
|
280 // call through so we can see the nested picture ops |
|
281 this->INHERITED::drawPicture(picture); |
|
282 } |
|
283 |
|
284 void SkLuaCanvas::drawVertices(VertexMode vmode, int vertexCount, |
|
285 const SkPoint vertices[], const SkPoint texs[], |
|
286 const SkColor colors[], SkXfermode* xmode, |
|
287 const uint16_t indices[], int indexCount, |
|
288 const SkPaint& paint) { |
|
289 AUTO_LUA("drawVertices"); |
|
290 lua.pushPaint(paint, "paint"); |
|
291 } |
|
292 |
|
293 void SkLuaCanvas::drawData(const void* data, size_t length) { |
|
294 AUTO_LUA("drawData"); |
|
295 } |