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() { 85 AUTO_LUA("save"); 86 this->INHERITED::willSave(); 87 } 88 89 SkCanvas::SaveLayerStrategy SkLuaCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) { 90 AUTO_LUA("saveLayer"); 91 if (rec.fBounds) { 92 lua.pushRect(*rec.fBounds, "bounds"); 93 } 94 if (rec.fPaint) { 95 lua.pushPaint(*rec.fPaint, "paint"); 96 } 97 98 (void)this->INHERITED::getSaveLayerStrategy(rec); 99 // No need for a layer. 100 return kNoLayer_SaveLayerStrategy; 101 } 102 103 void SkLuaCanvas::willRestore() { 104 AUTO_LUA("restore"); 105 this->INHERITED::willRestore(); 106 } 107 108 void SkLuaCanvas::didConcat(const SkMatrix& matrix) { 109 switch (matrix.getType()) { 110 case SkMatrix::kTranslate_Mask: { 111 AUTO_LUA("translate"); 112 lua.pushScalar(matrix.getTranslateX(), "dx"); 113 lua.pushScalar(matrix.getTranslateY(), "dy"); 114 break; 115 } 116 case SkMatrix::kScale_Mask: { 117 AUTO_LUA("scale"); 118 lua.pushScalar(matrix.getScaleX(), "sx"); 119 lua.pushScalar(matrix.getScaleY(), "sy"); 120 break; 121 } 122 default: { 123 AUTO_LUA("concat"); 124 // pushMatrix added in https://codereview.chromium.org/203203004/ 125 // Doesn't seem to have ever been working correctly since added 126 // lua.pushMatrix(matrix); 127 break; 128 } 129 } 130 131 this->INHERITED::didConcat(matrix); 132 } 133 134 void SkLuaCanvas::didSetMatrix(const SkMatrix& matrix) { 135 this->INHERITED::didSetMatrix(matrix); 136 } 137 138 void SkLuaCanvas::onClipRect(const SkRect& r, SkClipOp op, ClipEdgeStyle edgeStyle) { 139 AUTO_LUA("clipRect"); 140 lua.pushRect(r, "rect"); 141 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa"); 142 this->INHERITED::onClipRect(r, op, edgeStyle); 143 } 144 145 void SkLuaCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) { 146 AUTO_LUA("clipRRect"); 147 lua.pushRRect(rrect, "rrect"); 148 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa"); 149 this->INHERITED::onClipRRect(rrect, op, edgeStyle); 150 } 151 152 void SkLuaCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) { 153 AUTO_LUA("clipPath"); 154 lua.pushPath(path, "path"); 155 lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa"); 156 this->INHERITED::onClipPath(path, op, edgeStyle); 157 } 158 159 void SkLuaCanvas::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) { 160 AUTO_LUA("clipRegion"); 161 this->INHERITED::onClipRegion(deviceRgn, op); 162 } 163 164 void SkLuaCanvas::onDrawPaint(const SkPaint& paint) { 165 AUTO_LUA("drawPaint"); 166 lua.pushPaint(paint, "paint"); 167 } 168 169 void SkLuaCanvas::onDrawPoints(PointMode mode, size_t count, 170 const SkPoint pts[], const SkPaint& paint) { 171 AUTO_LUA("drawPoints"); 172 lua.pushArrayPoint(pts, SkToInt(count), "points"); 173 lua.pushPaint(paint, "paint"); 174 } 175 176 void SkLuaCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) { 177 AUTO_LUA("drawOval"); 178 lua.pushRect(rect, "rect"); 179 lua.pushPaint(paint, "paint"); 180 } 181 182 void SkLuaCanvas::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle, 183 bool useCenter, const SkPaint& paint) { 184 AUTO_LUA("drawArc"); 185 lua.pushRect(rect, "rect"); 186 lua.pushScalar(startAngle, "startAngle"); 187 lua.pushScalar(sweepAngle, "sweepAngle"); 188 lua.pushBool(useCenter, "useCenter"); 189 lua.pushPaint(paint, "paint"); 190 } 191 192 void SkLuaCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) { 193 AUTO_LUA("drawRect"); 194 lua.pushRect(rect, "rect"); 195 lua.pushPaint(paint, "paint"); 196 } 197 198 void SkLuaCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) { 199 AUTO_LUA("drawRRect"); 200 lua.pushRRect(rrect, "rrect"); 201 lua.pushPaint(paint, "paint"); 202 } 203 204 void SkLuaCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, 205 const SkPaint& paint) { 206 AUTO_LUA("drawDRRect"); 207 lua.pushRRect(outer, "outer"); 208 lua.pushRRect(inner, "inner"); 209 lua.pushPaint(paint, "paint"); 210 } 211 212 void SkLuaCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) { 213 AUTO_LUA("drawPath"); 214 lua.pushPath(path, "path"); 215 lua.pushPaint(paint, "paint"); 216 } 217 218 void SkLuaCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y, 219 const SkPaint* paint) { 220 AUTO_LUA("drawBitmap"); 221 if (paint) { 222 lua.pushPaint(*paint, "paint"); 223 } 224 } 225 226 void SkLuaCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst, 227 const SkPaint* paint, SrcRectConstraint) { 228 AUTO_LUA("drawBitmapRect"); 229 if (paint) { 230 lua.pushPaint(*paint, "paint"); 231 } 232 } 233 234 void SkLuaCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst, 235 const SkPaint* paint) { 236 AUTO_LUA("drawBitmapNine"); 237 if (paint) { 238 lua.pushPaint(*paint, "paint"); 239 } 240 } 241 242 void SkLuaCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) { 243 AUTO_LUA("drawImage"); 244 if (paint) { 245 lua.pushPaint(*paint, "paint"); 246 } 247 } 248 249 void SkLuaCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst, 250 const SkPaint* paint, SrcRectConstraint) { 251 AUTO_LUA("drawImageRect"); 252 if (paint) { 253 lua.pushPaint(*paint, "paint"); 254 } 255 } 256 257 void SkLuaCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, 258 const SkPaint& paint) { 259 AUTO_LUA("drawText"); 260 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength); 261 lua.pushPaint(paint, "paint"); 262 } 263 264 void SkLuaCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], 265 const SkPaint& paint) { 266 AUTO_LUA("drawPosText"); 267 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength); 268 lua.pushPaint(paint, "paint"); 269 } 270 271 void SkLuaCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], 272 SkScalar constY, const SkPaint& paint) { 273 AUTO_LUA("drawPosTextH"); 274 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength); 275 lua.pushPaint(paint, "paint"); 276 } 277 278 void SkLuaCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, 279 const SkMatrix* matrix, const SkPaint& paint) { 280 AUTO_LUA("drawTextOnPath"); 281 lua.pushPath(path, "path"); 282 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength); 283 lua.pushPaint(paint, "paint"); 284 } 285 286 void SkLuaCanvas::onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[], 287 const SkRect* cull, const SkPaint& paint) { 288 AUTO_LUA("drawTextRSXform"); 289 lua.pushEncodedText(paint.getTextEncoding(), text, byteLength); 290 // TODO: export other params 291 lua.pushPaint(paint, "paint"); 292 } 293 294 void SkLuaCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, 295 const SkPaint &paint) { 296 AUTO_LUA("drawTextBlob"); 297 lua.pushTextBlob(blob, "blob"); 298 lua.pushScalar(x, "x"); 299 lua.pushScalar(y, "y"); 300 lua.pushPaint(paint, "paint"); 301 } 302 303 void SkLuaCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, 304 const SkPaint* paint) { 305 AUTO_LUA("drawPicture"); 306 // call through so we can see the nested picture ops 307 this->INHERITED::onDrawPicture(picture, matrix, paint); 308 } 309 310 void SkLuaCanvas::onDrawVerticesObject(const SkVertices*, SkBlendMode, const SkPaint& paint) { 311 AUTO_LUA("drawVertices"); 312 lua.pushPaint(paint, "paint"); 313 } 314