blob: 531cbb00642e0400f6cd4fd3b2788c71b5e244e5 [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.orge54a23f2014-03-12 20:21:48 +000084void SkLuaCanvas::willSave(SaveFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +000085 AUTO_LUA("save");
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +000086 this->INHERITED::willSave(flags);
reed@google.comdff7e112013-05-15 19:34:20 +000087}
88
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +000089SkCanvas::SaveLayerStrategy SkLuaCanvas::willSaveLayer(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.orge54a23f2014-03-12 20:21:48 +000098
99 this->INHERITED::willSaveLayer(bounds, paint, flags);
100 // No need for a layer.
101 return kNoLayer_SaveLayerStrategy;
reed@google.comdff7e112013-05-15 19:34:20 +0000102}
103
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000104void SkLuaCanvas::willRestore() {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000105 AUTO_LUA("restore");
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000106 this->INHERITED::willRestore();
reed@google.comdff7e112013-05-15 19:34:20 +0000107}
108
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000109void SkLuaCanvas::didConcat(const SkMatrix& matrix) {
commit-bot@chromium.orgd9ea09e2014-03-25 17:32:26 +0000110 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 lua.pushMatrix(matrix);
126 break;
127 }
128 }
129
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000130 this->INHERITED::didConcat(matrix);
reed@google.comdff7e112013-05-15 19:34:20 +0000131}
132
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000133void SkLuaCanvas::didSetMatrix(const SkMatrix& matrix) {
134 this->INHERITED::didSetMatrix(matrix);
reed@google.comdff7e112013-05-15 19:34:20 +0000135}
136
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000137void SkLuaCanvas::onClipRect(const SkRect& r, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000138 AUTO_LUA("clipRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000139 lua.pushRect(r, "rect");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000140 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
141 this->INHERITED::onClipRect(r, op, edgeStyle);
reed@google.comdff7e112013-05-15 19:34:20 +0000142}
143
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000144void SkLuaCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000145 AUTO_LUA("clipRRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000146 lua.pushRRect(rrect, "rrect");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000147 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
148 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
reed@google.comdff7e112013-05-15 19:34:20 +0000149}
150
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000151void SkLuaCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000152 AUTO_LUA("clipPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000153 lua.pushPath(path, "path");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000154 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
155 this->INHERITED::onClipPath(path, op, edgeStyle);
reed@google.comdff7e112013-05-15 19:34:20 +0000156}
157
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000158void SkLuaCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000159 AUTO_LUA("clipRegion");
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000160 this->INHERITED::onClipRegion(deviceRgn, op);
reed@google.comdff7e112013-05-15 19:34:20 +0000161}
162
163void SkLuaCanvas::drawPaint(const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000164 AUTO_LUA("drawPaint");
reed@google.com74ce6f02013-05-22 15:13:18 +0000165 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000166}
167
168void SkLuaCanvas::drawPoints(PointMode mode, size_t count,
169 const SkPoint pts[], const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000170 AUTO_LUA("drawPoints");
commit-bot@chromium.org2cfa3202014-04-19 22:00:40 +0000171 lua.pushArrayPoint(pts, SkToInt(count), "points");
reed@google.com74ce6f02013-05-22 15:13:18 +0000172 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000173}
174
175void SkLuaCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000176 AUTO_LUA("drawOval");
reed@google.com74ce6f02013-05-22 15:13:18 +0000177 lua.pushRect(rect, "rect");
178 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000179}
180
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000181void SkLuaCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000182 AUTO_LUA("drawRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000183 lua.pushRect(rect, "rect");
184 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000185}
186
187void SkLuaCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000188 AUTO_LUA("drawRRect");
reed@google.com74ce6f02013-05-22 15:13:18 +0000189 lua.pushRRect(rrect, "rrect");
190 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000191}
192
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000193void SkLuaCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
194 const SkPaint& paint) {
195 AUTO_LUA("drawDRRect");
196 lua.pushRRect(outer, "outer");
197 lua.pushRRect(inner, "inner");
198 lua.pushPaint(paint, "paint");
199}
200
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000201void SkLuaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000202 AUTO_LUA("drawPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000203 lua.pushPath(path, "path");
204 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000205}
206
207void SkLuaCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000208 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000209 AUTO_LUA("drawBitmap");
210 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000211 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000212 }
reed@google.comdff7e112013-05-15 19:34:20 +0000213}
214
215void SkLuaCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000216 const SkRect& dst, const SkPaint* paint,
217 DrawBitmapRectFlags flags) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000218 AUTO_LUA("drawBitmapRectToRect");
219 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000220 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000221 }
reed@google.comdff7e112013-05-15 19:34:20 +0000222}
223
224void SkLuaCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000225 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000226 AUTO_LUA("drawBitmapMatrix");
227 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000228 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000229 }
reed@google.comdff7e112013-05-15 19:34:20 +0000230}
231
232void SkLuaCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
233 const SkPaint* paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000234 AUTO_LUA("drawSprite");
235 if (paint) {
reed@google.com74ce6f02013-05-22 15:13:18 +0000236 lua.pushPaint(*paint, "paint");
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000237 }
reed@google.comdff7e112013-05-15 19:34:20 +0000238}
239
commit-bot@chromium.org945ec3a2014-04-22 20:07:30 +0000240void SkLuaCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
241 SkScalar y, const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000242 AUTO_LUA("drawText");
reed@google.come3823fd2013-05-30 18:55:14 +0000243 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000244 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000245}
246
commit-bot@chromium.org945ec3a2014-04-22 20:07:30 +0000247void SkLuaCanvas::drawPosText(const void* text, size_t byteLength,
248 const SkPoint pos[], const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000249 AUTO_LUA("drawPosText");
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
commit-bot@chromium.org945ec3a2014-04-22 20:07:30 +0000254void SkLuaCanvas::drawPosTextH(const void* text, size_t byteLength,
255 const SkScalar xpos[], SkScalar constY,
256 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000257 AUTO_LUA("drawPosTextH");
reed@google.come3823fd2013-05-30 18:55:14 +0000258 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000259 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000260}
261
commit-bot@chromium.org945ec3a2014-04-22 20:07:30 +0000262void SkLuaCanvas::drawTextOnPath(const void* text, size_t byteLength,
263 const SkPath& path, const SkMatrix* matrix,
264 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000265 AUTO_LUA("drawTextOnPath");
reed@google.com74ce6f02013-05-22 15:13:18 +0000266 lua.pushPath(path, "path");
reed@google.come3823fd2013-05-30 18:55:14 +0000267 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
reed@google.com74ce6f02013-05-22 15:13:18 +0000268 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000269}
270
271void SkLuaCanvas::drawPicture(SkPicture& picture) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000272 AUTO_LUA("drawPicture");
reed@google.comdff7e112013-05-15 19:34:20 +0000273 // call through so we can see the nested picture ops
274 this->INHERITED::drawPicture(picture);
275}
276
277void SkLuaCanvas::drawVertices(VertexMode vmode, int vertexCount,
278 const SkPoint vertices[], const SkPoint texs[],
279 const SkColor colors[], SkXfermode* xmode,
280 const uint16_t indices[], int indexCount,
281 const SkPaint& paint) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000282 AUTO_LUA("drawVertices");
reed@google.com74ce6f02013-05-22 15:13:18 +0000283 lua.pushPaint(paint, "paint");
reed@google.comdff7e112013-05-15 19:34:20 +0000284}
285
286void SkLuaCanvas::drawData(const void* data, size_t length) {
mike@reedtribe.org1c5a94f2013-05-16 04:20:23 +0000287 AUTO_LUA("drawData");
reed@google.comdff7e112013-05-15 19:34:20 +0000288}