gfx/skia/trunk/src/utils/SkLuaCanvas.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/utils/SkLuaCanvas.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,295 @@
     1.4 +/*
     1.5 + * Copyright 2013 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#include "SkLuaCanvas.h"
    1.12 +#include "SkLua.h"
    1.13 +
    1.14 +extern "C" {
    1.15 +    #include "lua.h"
    1.16 +    #include "lauxlib.h"
    1.17 +}
    1.18 +
    1.19 +class AutoCallLua : public SkLua {
    1.20 +public:
    1.21 +    AutoCallLua(lua_State* L, const char func[], const char verb[]) : INHERITED(L) {
    1.22 +        lua_getglobal(L, func);
    1.23 +        if (!lua_isfunction(L, -1)) {
    1.24 +            int t = lua_type(L, -1);
    1.25 +            SkDebugf("--- expected function %d\n", t);
    1.26 +        }
    1.27 +
    1.28 +        lua_newtable(L);
    1.29 +        this->pushString(verb, "verb");
    1.30 +    }
    1.31 +
    1.32 +    ~AutoCallLua() {
    1.33 +        lua_State* L = this->get();
    1.34 +        if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
    1.35 +            SkDebugf("lua err: %s\n", lua_tostring(L, -1));
    1.36 +        }
    1.37 +        lua_settop(L, -1);
    1.38 +    }
    1.39 +
    1.40 +    void pushEncodedText(SkPaint::TextEncoding, const void*, size_t);
    1.41 +
    1.42 +private:
    1.43 +    typedef SkLua INHERITED;
    1.44 +};
    1.45 +
    1.46 +#define AUTO_LUA(verb)  AutoCallLua lua(fL, fFunc.c_str(), verb)
    1.47 +
    1.48 +
    1.49 +///////////////////////////////////////////////////////////////////////////////
    1.50 +
    1.51 +void AutoCallLua::pushEncodedText(SkPaint::TextEncoding enc, const void* text,
    1.52 +                                  size_t length) {
    1.53 +    switch (enc) {
    1.54 +        case SkPaint::kUTF8_TextEncoding:
    1.55 +            this->pushString((const char*)text, length, "text");
    1.56 +            break;
    1.57 +        case SkPaint::kUTF16_TextEncoding: {
    1.58 +            SkString str;
    1.59 +            str.setUTF16((const uint16_t*)text, length);
    1.60 +            this->pushString(str, "text");
    1.61 +        } break;
    1.62 +        case SkPaint::kGlyphID_TextEncoding:
    1.63 +            this->pushArrayU16((const uint16_t*)text, SkToInt(length >> 1),
    1.64 +                               "glyphs");
    1.65 +            break;
    1.66 +        case SkPaint::kUTF32_TextEncoding:
    1.67 +            break;
    1.68 +    }
    1.69 +}
    1.70 +
    1.71 +///////////////////////////////////////////////////////////////////////////////
    1.72 +
    1.73 +void SkLuaCanvas::pushThis() {
    1.74 +    SkLua(fL).pushCanvas(this);
    1.75 +}
    1.76 +
    1.77 +///////////////////////////////////////////////////////////////////////////////
    1.78 +
    1.79 +SkLuaCanvas::SkLuaCanvas(int width, int height, lua_State* L, const char func[])
    1.80 +    : INHERITED(width, height)
    1.81 +    , fL(L)
    1.82 +    , fFunc(func) {
    1.83 +}
    1.84 +
    1.85 +SkLuaCanvas::~SkLuaCanvas() {}
    1.86 +
    1.87 +void SkLuaCanvas::willSave(SaveFlags flags) {
    1.88 +    AUTO_LUA("save");
    1.89 +    this->INHERITED::willSave(flags);
    1.90 +}
    1.91 +
    1.92 +SkCanvas::SaveLayerStrategy SkLuaCanvas::willSaveLayer(const SkRect* bounds, const SkPaint* paint,
    1.93 +                                                       SaveFlags flags) {
    1.94 +    AUTO_LUA("saveLayer");
    1.95 +    if (bounds) {
    1.96 +        lua.pushRect(*bounds, "bounds");
    1.97 +    }
    1.98 +    if (paint) {
    1.99 +        lua.pushPaint(*paint, "paint");
   1.100 +    }
   1.101 +
   1.102 +    this->INHERITED::willSaveLayer(bounds, paint, flags);
   1.103 +    // No need for a layer.
   1.104 +    return kNoLayer_SaveLayerStrategy;
   1.105 +}
   1.106 +
   1.107 +void SkLuaCanvas::willRestore() {
   1.108 +    AUTO_LUA("restore");
   1.109 +    this->INHERITED::willRestore();
   1.110 +}
   1.111 +
   1.112 +void SkLuaCanvas::didTranslate(SkScalar dx, SkScalar dy) {
   1.113 +    AUTO_LUA("translate");
   1.114 +    lua.pushScalar(dx, "dx");
   1.115 +    lua.pushScalar(dy, "dy");
   1.116 +    this->INHERITED::didTranslate(dx, dy);
   1.117 +}
   1.118 +
   1.119 +void SkLuaCanvas::didScale(SkScalar sx, SkScalar sy) {
   1.120 +    AUTO_LUA("scale");
   1.121 +    lua.pushScalar(sx, "sx");
   1.122 +    lua.pushScalar(sy, "sy");
   1.123 +    this->INHERITED::didScale(sx, sy);
   1.124 +}
   1.125 +
   1.126 +void SkLuaCanvas::didRotate(SkScalar degrees) {
   1.127 +    AUTO_LUA("rotate");
   1.128 +    lua.pushScalar(degrees, "degrees");
   1.129 +    this->INHERITED::didRotate(degrees);
   1.130 +}
   1.131 +
   1.132 +void SkLuaCanvas::didSkew(SkScalar kx, SkScalar ky) {
   1.133 +    AUTO_LUA("skew");
   1.134 +    lua.pushScalar(kx, "kx");
   1.135 +    lua.pushScalar(ky, "ky");
   1.136 +    this->INHERITED::didSkew(kx, ky);
   1.137 +}
   1.138 +
   1.139 +void SkLuaCanvas::didConcat(const SkMatrix& matrix) {
   1.140 +    AUTO_LUA("concat");
   1.141 +    this->INHERITED::didConcat(matrix);
   1.142 +}
   1.143 +
   1.144 +void SkLuaCanvas::didSetMatrix(const SkMatrix& matrix) {
   1.145 +    this->INHERITED::didSetMatrix(matrix);
   1.146 +}
   1.147 +
   1.148 +void SkLuaCanvas::onClipRect(const SkRect& r, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
   1.149 +    AUTO_LUA("clipRect");
   1.150 +    lua.pushRect(r, "rect");
   1.151 +    lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
   1.152 +    this->INHERITED::onClipRect(r, op, edgeStyle);
   1.153 +}
   1.154 +
   1.155 +void SkLuaCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
   1.156 +    AUTO_LUA("clipRRect");
   1.157 +    lua.pushRRect(rrect, "rrect");
   1.158 +    lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
   1.159 +    this->INHERITED::onClipRRect(rrect, op, edgeStyle);
   1.160 +}
   1.161 +
   1.162 +void SkLuaCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) {
   1.163 +    AUTO_LUA("clipPath");
   1.164 +    lua.pushPath(path, "path");
   1.165 +    lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
   1.166 +    this->INHERITED::onClipPath(path, op, edgeStyle);
   1.167 +}
   1.168 +
   1.169 +void SkLuaCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
   1.170 +    AUTO_LUA("clipRegion");
   1.171 +    this->INHERITED::onClipRegion(deviceRgn, op);
   1.172 +}
   1.173 +
   1.174 +void SkLuaCanvas::drawPaint(const SkPaint& paint) {
   1.175 +    AUTO_LUA("drawPaint");
   1.176 +    lua.pushPaint(paint, "paint");
   1.177 +}
   1.178 +
   1.179 +void SkLuaCanvas::drawPoints(PointMode mode, size_t count,
   1.180 +                               const SkPoint pts[], const SkPaint& paint) {
   1.181 +    AUTO_LUA("drawPoints");
   1.182 +    lua.pushPaint(paint, "paint");
   1.183 +}
   1.184 +
   1.185 +void SkLuaCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
   1.186 +    AUTO_LUA("drawOval");
   1.187 +    lua.pushRect(rect, "rect");
   1.188 +    lua.pushPaint(paint, "paint");
   1.189 +}
   1.190 +
   1.191 +void SkLuaCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
   1.192 +    AUTO_LUA("drawRect");
   1.193 +    lua.pushRect(rect, "rect");
   1.194 +    lua.pushPaint(paint, "paint");
   1.195 +}
   1.196 +
   1.197 +void SkLuaCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
   1.198 +    AUTO_LUA("drawRRect");
   1.199 +    lua.pushRRect(rrect, "rrect");
   1.200 +    lua.pushPaint(paint, "paint");
   1.201 +}
   1.202 +
   1.203 +void SkLuaCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
   1.204 +                               const SkPaint& paint) {
   1.205 +    AUTO_LUA("drawDRRect");
   1.206 +    lua.pushRRect(outer, "outer");
   1.207 +    lua.pushRRect(inner, "inner");
   1.208 +    lua.pushPaint(paint, "paint");
   1.209 +}
   1.210 +
   1.211 +void SkLuaCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
   1.212 +    AUTO_LUA("drawPath");
   1.213 +    lua.pushPath(path, "path");
   1.214 +    lua.pushPaint(paint, "paint");
   1.215 +}
   1.216 +
   1.217 +void SkLuaCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
   1.218 +                             const SkPaint* paint) {
   1.219 +    AUTO_LUA("drawBitmap");
   1.220 +    if (paint) {
   1.221 +        lua.pushPaint(*paint, "paint");
   1.222 +    }
   1.223 +}
   1.224 +
   1.225 +void SkLuaCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
   1.226 +                                       const SkRect& dst, const SkPaint* paint,
   1.227 +                                       DrawBitmapRectFlags flags) {
   1.228 +    AUTO_LUA("drawBitmapRectToRect");
   1.229 +    if (paint) {
   1.230 +        lua.pushPaint(*paint, "paint");
   1.231 +    }
   1.232 +}
   1.233 +
   1.234 +void SkLuaCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& m,
   1.235 +                                   const SkPaint* paint) {
   1.236 +    AUTO_LUA("drawBitmapMatrix");
   1.237 +    if (paint) {
   1.238 +        lua.pushPaint(*paint, "paint");
   1.239 +    }
   1.240 +}
   1.241 +
   1.242 +void SkLuaCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
   1.243 +                               const SkPaint* paint) {
   1.244 +    AUTO_LUA("drawSprite");
   1.245 +    if (paint) {
   1.246 +        lua.pushPaint(*paint, "paint");
   1.247 +    }
   1.248 +}
   1.249 +
   1.250 +void SkLuaCanvas::drawText(const void* text, size_t byteLength, SkScalar x,
   1.251 +                             SkScalar y, const SkPaint& paint) {
   1.252 +    AUTO_LUA("drawText");
   1.253 +    lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
   1.254 +    lua.pushPaint(paint, "paint");
   1.255 +}
   1.256 +
   1.257 +void SkLuaCanvas::drawPosText(const void* text, size_t byteLength,
   1.258 +                                const SkPoint pos[], const SkPaint& paint) {
   1.259 +    AUTO_LUA("drawPosText");
   1.260 +    lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
   1.261 +    lua.pushPaint(paint, "paint");
   1.262 +}
   1.263 +
   1.264 +void SkLuaCanvas::drawPosTextH(const void* text, size_t byteLength,
   1.265 +                                 const SkScalar xpos[], SkScalar constY,
   1.266 +                                 const SkPaint& paint) {
   1.267 +    AUTO_LUA("drawPosTextH");
   1.268 +    lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
   1.269 +    lua.pushPaint(paint, "paint");
   1.270 +}
   1.271 +
   1.272 +void SkLuaCanvas::drawTextOnPath(const void* text, size_t byteLength,
   1.273 +                                   const SkPath& path, const SkMatrix* matrix,
   1.274 +                                   const SkPaint& paint) {
   1.275 +    AUTO_LUA("drawTextOnPath");
   1.276 +    lua.pushPath(path, "path");
   1.277 +    lua.pushEncodedText(paint.getTextEncoding(), text, byteLength);
   1.278 +    lua.pushPaint(paint, "paint");
   1.279 +}
   1.280 +
   1.281 +void SkLuaCanvas::drawPicture(SkPicture& picture) {
   1.282 +    AUTO_LUA("drawPicture");
   1.283 +    // call through so we can see the nested picture ops
   1.284 +    this->INHERITED::drawPicture(picture);
   1.285 +}
   1.286 +
   1.287 +void SkLuaCanvas::drawVertices(VertexMode vmode, int vertexCount,
   1.288 +                                 const SkPoint vertices[], const SkPoint texs[],
   1.289 +                                 const SkColor colors[], SkXfermode* xmode,
   1.290 +                                 const uint16_t indices[], int indexCount,
   1.291 +                                 const SkPaint& paint) {
   1.292 +    AUTO_LUA("drawVertices");
   1.293 +    lua.pushPaint(paint, "paint");
   1.294 +}
   1.295 +
   1.296 +void SkLuaCanvas::drawData(const void* data, size_t length) {
   1.297 +    AUTO_LUA("drawData");
   1.298 +}

mercurial