| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 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" |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 9 | #include "SkLua.h" |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 10 | |
| 11 | extern "C" { |
| 12 | #include "lua.h" |
| mike@reedtribe.org | f02fe3d | 2013-05-21 12:20:39 +0000 | [diff] [blame] | 13 | #include "lauxlib.h" |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 14 | } |
| 15 | |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 16 | class AutoCallLua : public SkLua { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 17 | public: |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 18 | AutoCallLua(lua_State* L, const char func[], const char verb[]) : INHERITED(L) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 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 | } |
| skia.committer@gmail.com | 539f364 | 2013-05-16 07:01:00 +0000 | [diff] [blame] | 24 | |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 25 | lua_newtable(L); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 26 | this->pushString(verb, "verb"); |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | ~AutoCallLua() { |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 30 | lua_State* L = this->getL(); |
| 31 | if (lua_pcall(L, 1, 0, 0) != LUA_OK) { |
| 32 | SkDebugf("lua err: %s\n", lua_tostring(L, -1)); |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 33 | } |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 34 | lua_settop(L, -1); |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | private: |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 38 | typedef SkLua INHERITED; |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 41 | #define AUTO_LUA(verb) AutoCallLua lua(fL, fFunc.c_str(), verb) |
| reed@google.com | 9a73104 | 2013-05-21 17:52:33 +0000 | [diff] [blame] | 42 | |
| 43 | /////////////////////////////////////////////////////////////////////////////// |
| 44 | |
| mike@reedtribe.org | f02fe3d | 2013-05-21 12:20:39 +0000 | [diff] [blame] | 45 | void SkLuaCanvas::pushThis() { |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 46 | SkLua(fL).pushCanvas(this); |
| mike@reedtribe.org | f02fe3d | 2013-05-21 12:20:39 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | /////////////////////////////////////////////////////////////////////////////// |
| 50 | |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 51 | static SkBitmap make_bm(int width, int height) { |
| 52 | SkBitmap bm; |
| 53 | bm.setConfig(SkBitmap::kNo_Config, width, height); |
| 54 | return bm; |
| 55 | } |
| 56 | |
| 57 | SkLuaCanvas::SkLuaCanvas(int width, int height, lua_State* L, const char func[]) |
| 58 | : INHERITED(make_bm(width, height)) |
| 59 | , fL(L) |
| 60 | , fFunc(func) { |
| 61 | } |
| 62 | |
| 63 | SkLuaCanvas::~SkLuaCanvas() {} |
| 64 | |
| 65 | int SkLuaCanvas::save(SaveFlags flags) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 66 | AUTO_LUA("save"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 67 | return this->INHERITED::save(flags); |
| 68 | } |
| 69 | |
| 70 | int SkLuaCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 71 | SaveFlags flags) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 72 | AUTO_LUA("saveLayer"); |
| 73 | if (bounds) { |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 74 | lua.pushRect(*bounds, "bounds"); |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 75 | } |
| 76 | if (paint) { |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 77 | lua.pushPaint(*paint, "paint"); |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 78 | } |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 79 | return this->INHERITED::save(flags); |
| 80 | } |
| 81 | |
| 82 | void SkLuaCanvas::restore() { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 83 | AUTO_LUA("restore"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 84 | this->INHERITED::restore(); |
| 85 | } |
| 86 | |
| 87 | bool SkLuaCanvas::translate(SkScalar dx, SkScalar dy) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 88 | AUTO_LUA("translate"); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 89 | lua.pushScalar(dx, "dx"); |
| 90 | lua.pushScalar(dy, "dy"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 91 | return this->INHERITED::translate(dx, dy); |
| 92 | } |
| 93 | |
| 94 | bool SkLuaCanvas::scale(SkScalar sx, SkScalar sy) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 95 | AUTO_LUA("scale"); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 96 | lua.pushScalar(sx, "sx"); |
| 97 | lua.pushScalar(sy, "sy"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 98 | return this->INHERITED::scale(sx, sy); |
| 99 | } |
| 100 | |
| 101 | bool SkLuaCanvas::rotate(SkScalar degrees) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 102 | AUTO_LUA("rotate"); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 103 | lua.pushScalar(degrees, "degrees"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 104 | return this->INHERITED::rotate(degrees); |
| 105 | } |
| 106 | |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 107 | bool SkLuaCanvas::skew(SkScalar kx, SkScalar ky) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 108 | AUTO_LUA("skew"); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 109 | lua.pushScalar(kx, "kx"); |
| 110 | lua.pushScalar(ky, "ky"); |
| 111 | return this->INHERITED::skew(kx, ky); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | bool SkLuaCanvas::concat(const SkMatrix& matrix) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 115 | AUTO_LUA("concat"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 116 | return this->INHERITED::concat(matrix); |
| 117 | } |
| 118 | |
| 119 | void SkLuaCanvas::setMatrix(const SkMatrix& matrix) { |
| 120 | this->INHERITED::setMatrix(matrix); |
| 121 | } |
| 122 | |
| 123 | bool SkLuaCanvas::clipRect(const SkRect& r, SkRegion::Op op, bool doAA) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 124 | AUTO_LUA("clipRect"); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 125 | lua.pushRect(r, "rect"); |
| 126 | lua.pushBool(doAA, "aa"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 127 | return this->INHERITED::clipRect(r, op, doAA); |
| 128 | } |
| 129 | |
| 130 | bool SkLuaCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 131 | AUTO_LUA("clipRRect"); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 132 | lua.pushRRect(rrect, "rrect"); |
| 133 | lua.pushBool(doAA, "aa"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 134 | return this->INHERITED::clipRRect(rrect, op, doAA); |
| 135 | } |
| 136 | |
| 137 | bool SkLuaCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 138 | AUTO_LUA("clipPath"); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 139 | lua.pushPath(path, "path"); |
| 140 | lua.pushBool(doAA, "aa"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 141 | return this->INHERITED::clipPath(path, op, doAA); |
| 142 | } |
| 143 | |
| 144 | bool SkLuaCanvas::clipRegion(const SkRegion& deviceRgn, SkRegion::Op op) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 145 | AUTO_LUA("clipRegion"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 146 | return this->INHERITED::clipRegion(deviceRgn, op); |
| 147 | } |
| 148 | |
| 149 | void SkLuaCanvas::drawPaint(const SkPaint& paint) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 150 | AUTO_LUA("drawPaint"); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 151 | lua.pushPaint(paint, "paint"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void SkLuaCanvas::drawPoints(PointMode mode, size_t count, |
| 155 | const SkPoint pts[], const SkPaint& paint) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 156 | AUTO_LUA("drawPoints"); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 157 | lua.pushPaint(paint, "paint"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void SkLuaCanvas::drawOval(const SkRect& rect, const SkPaint& paint) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 161 | AUTO_LUA("drawOval"); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 162 | lua.pushRect(rect, "rect"); |
| 163 | lua.pushPaint(paint, "paint"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void SkLuaCanvas::drawRect(const SkRect& rect, const SkPaint& paint) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 167 | AUTO_LUA("drawRect"); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 168 | lua.pushRect(rect, "rect"); |
| 169 | lua.pushPaint(paint, "paint"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void SkLuaCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 173 | AUTO_LUA("drawRRect"); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 174 | lua.pushRRect(rrect, "rrect"); |
| 175 | lua.pushPaint(paint, "paint"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | void SkLuaCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 179 | AUTO_LUA("drawPath"); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 180 | lua.pushPath(path, "path"); |
| 181 | lua.pushPaint(paint, "paint"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void SkLuaCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y, |
| 185 | const SkPaint* paint) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 186 | AUTO_LUA("drawBitmap"); |
| 187 | if (paint) { |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 188 | lua.pushPaint(*paint, "paint"); |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 189 | } |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void SkLuaCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src, |
| 193 | const SkRect& dst, const SkPaint* paint) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 194 | AUTO_LUA("drawBitmapRectToRect"); |
| 195 | if (paint) { |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 196 | lua.pushPaint(*paint, "paint"); |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 197 | } |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void SkLuaCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m, |
| 201 | const SkPaint* paint) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 202 | AUTO_LUA("drawBitmapMatrix"); |
| 203 | if (paint) { |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 204 | lua.pushPaint(*paint, "paint"); |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 205 | } |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | void SkLuaCanvas::drawSprite(const SkBitmap& bitmap, int x, int y, |
| 209 | const SkPaint* paint) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 210 | AUTO_LUA("drawSprite"); |
| 211 | if (paint) { |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 212 | lua.pushPaint(*paint, "paint"); |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 213 | } |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void SkLuaCanvas::drawText(const void* text, size_t byteLength, SkScalar x, |
| 217 | SkScalar y, const SkPaint& paint) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 218 | AUTO_LUA("drawText"); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 219 | lua.pushPaint(paint, "paint"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void SkLuaCanvas::drawPosText(const void* text, size_t byteLength, |
| 223 | const SkPoint pos[], const SkPaint& paint) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 224 | AUTO_LUA("drawPosText"); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 225 | lua.pushPaint(paint, "paint"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void SkLuaCanvas::drawPosTextH(const void* text, size_t byteLength, |
| 229 | const SkScalar xpos[], SkScalar constY, |
| 230 | const SkPaint& paint) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 231 | AUTO_LUA("drawPosTextH"); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 232 | lua.pushPaint(paint, "paint"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | void SkLuaCanvas::drawTextOnPath(const void* text, size_t byteLength, |
| 236 | const SkPath& path, const SkMatrix* matrix, |
| 237 | const SkPaint& paint) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 238 | AUTO_LUA("drawTextOnPath"); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 239 | lua.pushPath(path, "path"); |
| 240 | lua.pushPaint(paint, "paint"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | void SkLuaCanvas::drawPicture(SkPicture& picture) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 244 | AUTO_LUA("drawPicture"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 245 | // call through so we can see the nested picture ops |
| 246 | this->INHERITED::drawPicture(picture); |
| 247 | } |
| 248 | |
| 249 | void SkLuaCanvas::drawVertices(VertexMode vmode, int vertexCount, |
| 250 | const SkPoint vertices[], const SkPoint texs[], |
| 251 | const SkColor colors[], SkXfermode* xmode, |
| 252 | const uint16_t indices[], int indexCount, |
| 253 | const SkPaint& paint) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 254 | AUTO_LUA("drawVertices"); |
| reed@google.com | 74ce6f0 | 2013-05-22 15:13:18 +0000 | [diff] [blame^] | 255 | lua.pushPaint(paint, "paint"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void SkLuaCanvas::drawData(const void* data, size_t length) { |
| mike@reedtribe.org | 1c5a94f | 2013-05-16 04:20:23 +0000 | [diff] [blame] | 259 | AUTO_LUA("drawData"); |
| reed@google.com | dff7e11 | 2013-05-15 19:34:20 +0000 | [diff] [blame] | 260 | } |