blob: 2679fd25bd327de1719f2c1bf6238d7b464d3332 [file] [log] [blame]
reed@google.comdff7e112013-05-15 19:34:20 +00001/*
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"
reed@google.com74ce6f02013-05-22 15:13:18 +00009#include "SkLua.h"
reed@google.comdff7e112013-05-15 19:34:20 +000010
11extern "C" {
12 #include "lua.h"
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000013 #include "lauxlib.h"
reed@google.comdff7e112013-05-15 19:34:20 +000014}
15
reed@google.com74ce6f02013-05-22 15:13:18 +000016class AutoCallLua : public SkLua {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000017public:
reed@google.com74ce6f02013-05-22 15:13:18 +000018 AutoCallLua(lua_State* L, const char func[], const char verb[]) : INHERITED(L) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000019 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 }
skia.committer@gmail.com539f3642013-05-16 07:01:00 +000024
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000025 lua_newtable(L);
reed@google.com74ce6f02013-05-22 15:13:18 +000026 this->pushString(verb, "verb");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000027 }
28
29 ~AutoCallLua() {
reed@google.com3597b732013-05-22 20:12:50 +000030 lua_State* L = this->get();
reed@google.com74ce6f02013-05-22 15:13:18 +000031 if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
32 SkDebugf("lua err: %s\n", lua_tostring(L, -1));
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000033 }
reed@google.com74ce6f02013-05-22 15:13:18 +000034 lua_settop(L, -1);
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000035 }
36
reed@google.come3823fd2013-05-30 18:55:14 +000037 void pushEncodedText(SkPaint::TextEncoding, const void*, size_t);
38
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000039private:
reed@google.com74ce6f02013-05-22 15:13:18 +000040 typedef SkLua INHERITED;
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000041};
42
reed@google.com74ce6f02013-05-22 15:13:18 +000043#define AUTO_LUA(verb) AutoCallLua lua(fL, fFunc.c_str(), verb)
reed@google.com9a731042013-05-21 17:52:33 +000044
reed@google.come3823fd2013-05-30 18:55:14 +000045
46///////////////////////////////////////////////////////////////////////////////
47
48void 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:
reed@google.com7fa2a652014-01-27 13:42:58 +000060 this->pushArrayU16((const uint16_t*)text, SkToInt(length >> 1),
61 "glyphs");
reed@google.come3823fd2013-05-30 18:55:14 +000062 break;
63 case SkPaint::kUTF32_TextEncoding:
64 break;
65 }
66}
67
reed@google.com9a731042013-05-21 17:52:33 +000068///////////////////////////////////////////////////////////////////////////////
69
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000070void SkLuaCanvas::pushThis() {
reed@google.com74ce6f02013-05-22 15:13:18 +000071 SkLua(fL).pushCanvas(this);
mike@reedtribe.orgf02fe3d2013-05-21 12:20:39 +000072}
73
74///////////////////////////////////////////////////////////////////////////////
75
reed@google.comdff7e112013-05-15 19:34:20 +000076SkLuaCanvas::SkLuaCanvas(int width, int height, lua_State* L, const char func[])
commit-bot@chromium.orge2543102014-01-31 19:42:58 +000077 : INHERITED(width, height)
reed@google.comdff7e112013-05-15 19:34:20 +000078 , fL(L)
79 , fFunc(func) {
80}
81
82SkLuaCanvas::~SkLuaCanvas() {}
83
commit-bot@chromium.org4fcd92d2014-03-11 23:47:35 +000084void SkLuaCanvas::onSave(SaveFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000085 AUTO_LUA("save");
commit-bot@chromium.org4fcd92d2014-03-11 23:47:35 +000086 this->INHERITED::onSave(flags);
reed@google.comdff7e112013-05-15 19:34:20 +000087}
88
commit-bot@chromium.org4fcd92d2014-03-11 23:47:35 +000089bool SkLuaCanvas::onSaveLayer(const SkRect* bounds, const SkPaint* paint,
90 SaveFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000091 AUTO_LUA("saveLayer");
92 if (bounds) {
reed@google.com74ce6f02013-05-22 15:13:18 +000093 lua.pushRect(*bounds, "bounds");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000094 }
95 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +000096 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000097 }
commit-bot@chromium.org4fcd92d2014-03-11 23:47:35 +000098
99 this->INHERITED::onSaveLayer(bounds, paint, flags);
100 // No need for a layer.
101 return false;
reed@google.comdff7e112013-05-15 19:34:20 +0000102}
103
commit-bot@chromium.org4fcd92d2014-03-11 23:47:35 +0000104void SkLuaCanvas::onRestore() {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000105 AUTO_LUA("restore");
commit-bot@chromium.org4fcd92d2014-03-11 23:47:35 +0000106 this->INHERITED::onRestore();
reed@google.comdff7e112013-05-15 19:34:20 +0000107}
108
109bool SkLuaCanvas::translate(SkScalar dx, SkScalar dy) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000110 AUTO_LUA("translate");
reed@google.com74ce6f02013-05-22 15:13:18 +0000111 lua.pushScalar(dx, "dx");
112 lua.pushScalar(dy, "dy");
reed@google.comdff7e112013-05-15 19:34:20 +0000113 return this->INHERITED::translate(dx, dy);
114}
115
116bool SkLuaCanvas::scale(SkScalar sx, SkScalar sy) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000117 AUTO_LUA("scale");
reed@google.com74ce6f02013-05-22 15:13:18 +0000118 lua.pushScalar(sx, "sx");
119 lua.pushScalar(sy, "sy");
reed@google.comdff7e112013-05-15 19:34:20 +0000120 return this->INHERITED::scale(sx, sy);
121}
122
123bool SkLuaCanvas::rotate(SkScalar degrees) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000124 AUTO_LUA("rotate");
reed@google.com74ce6f02013-05-22 15:13:18 +0000125 lua.pushScalar(degrees, "degrees");
reed@google.comdff7e112013-05-15 19:34:20 +0000126 return this->INHERITED::rotate(degrees);
127}
128
reed@google.com74ce6f02013-05-22 15:13:18 +0000129bool SkLuaCanvas::skew(SkScalar kx, SkScalar ky) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000130 AUTO_LUA("skew");
reed@google.com74ce6f02013-05-22 15:13:18 +0000131 lua.pushScalar(kx, "kx");
132 lua.pushScalar(ky, "ky");
133 return this->INHERITED::skew(kx, ky);
reed@google.comdff7e112013-05-15 19:34:20 +0000134}
135
136bool SkLuaCanvas::concat(const SkMatrix& matrix) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000137 AUTO_LUA("concat");
reed@google.comdff7e112013-05-15 19:34:20 +0000138 return this->INHERITED::concat(matrix);
139}
140
141void SkLuaCanvas::setMatrix(const SkMatrix& matrix) {
142 this->INHERITED::setMatrix(matrix);
143}
144
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000145void SkLuaCanvas::onClipRect(const SkRect& r, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000146 AUTO_LUA("clipRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000147 lua.pushRect(r, "rect");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000148 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
149 this->INHERITED::onClipRect(r, op, edgeStyle);
reed@google.comdff7e112013-05-15 19:34:20 +0000150}
151
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000152void SkLuaCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000153 AUTO_LUA("clipRRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000154 lua.pushRRect(rrect, "rrect");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000155 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
156 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
reed@google.comdff7e112013-05-15 19:34:20 +0000157}
158
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000159void SkLuaCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000160 AUTO_LUA("clipPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000161 lua.pushPath(path, "path");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000162 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
163 this->INHERITED::onClipPath(path, op, edgeStyle);
reed@google.comdff7e112013-05-15 19:34:20 +0000164}
165
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000166void SkLuaCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000167 AUTO_LUA("clipRegion");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000168 this->INHERITED::onClipRegion(deviceRgn, op);
reed@google.comdff7e112013-05-15 19:34:20 +0000169}
170
171void SkLuaCanvas::drawPaint(const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000172 AUTO_LUA("drawPaint");
reed@google.com74ce6f02013-05-22 15:13:18 +0000173 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000174}
175
176void SkLuaCanvas::drawPoints(PointMode mode, size_t count,
177 const SkPoint pts[], const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000178 AUTO_LUA("drawPoints");
reed@google.com74ce6f02013-05-22 15:13:18 +0000179 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000180}
181
182void SkLuaCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000183 AUTO_LUA("drawOval");
reed@google.com74ce6f02013-05-22 15:13:18 +0000184 lua.pushRect(rect, "rect");
185 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000186}
187
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000188void SkLuaCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000189 AUTO_LUA("drawRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000190 lua.pushRect(rect, "rect");
191 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000192}
193
194void SkLuaCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000195 AUTO_LUA("drawRRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000196 lua.pushRRect(rrect, "rrect");
197 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000198}
199
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000200void 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
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000208void SkLuaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000209 AUTO_LUA("drawPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000210 lua.pushPath(path, "path");
211 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000212}
213
214void SkLuaCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000215 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000216 AUTO_LUA("drawBitmap");
217 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000218 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000219 }
reed@google.comdff7e112013-05-15 19:34:20 +0000220}
221
222void SkLuaCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000223 const SkRect& dst, const SkPaint* paint,
224 DrawBitmapRectFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000225 AUTO_LUA("drawBitmapRectToRect");
226 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000227 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000228 }
reed@google.comdff7e112013-05-15 19:34:20 +0000229}
230
231void SkLuaCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000232 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000233 AUTO_LUA("drawBitmapMatrix");
234 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000235 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000236 }
reed@google.comdff7e112013-05-15 19:34:20 +0000237}
238
239void SkLuaCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
240 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000241 AUTO_LUA("drawSprite");
242 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000243 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000244 }
reed@google.comdff7e112013-05-15 19:34:20 +0000245}
246
247void SkLuaCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
248 SkScalar y, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000249 AUTO_LUA("drawText");
reed@google.come3823fd2013-05-30 18:55:14 +0000250 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000251 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000252}
253
254void SkLuaCanvas::drawPosText(const void* text, size_t byteLength,
255 const SkPoint pos[], const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000256 AUTO_LUA("drawPosText");
reed@google.come3823fd2013-05-30 18:55:14 +0000257 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000258 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000259}
260
261void SkLuaCanvas::drawPosTextH(const void* text, size_t byteLength,
262 const SkScalar xpos[], SkScalar constY,
263 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000264 AUTO_LUA("drawPosTextH");
reed@google.come3823fd2013-05-30 18:55:14 +0000265 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000266 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000267}
268
269void SkLuaCanvas::drawTextOnPath(const void* text, size_t byteLength,
270 const SkPath& path, const SkMatrix* matrix,
271 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000272 AUTO_LUA("drawTextOnPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000273 lua.pushPath(path, "path");
reed@google.come3823fd2013-05-30 18:55:14 +0000274 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000275 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000276}
277
278void SkLuaCanvas::drawPicture(SkPicture& picture) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000279 AUTO_LUA("drawPicture");
reed@google.comdff7e112013-05-15 19:34:20 +0000280 // call through so we can see the nested picture ops
281 this->INHERITED::drawPicture(picture);
282}
283
284void 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) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000289 AUTO_LUA("drawVertices");
reed@google.com74ce6f02013-05-22 15:13:18 +0000290 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000291}
292
293void SkLuaCanvas::drawData(const void* data, size_t length) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000294 AUTO_LUA("drawData");
reed@google.comdff7e112013-05-15 19:34:20 +0000295}