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