123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- /*
- * Copyright 2013 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
- #include "include/utils/SkLuaCanvas.h"
- #include "include/private/SkTo.h"
- #include "include/utils/SkLua.h"
- #include "src/core/SkStringUtils.h"
- extern "C" {
- #include "lua.h"
- #include "lauxlib.h"
- }
- class AutoCallLua : public SkLua {
- public:
- AutoCallLua(lua_State* L, const char func[], const char verb[]) : INHERITED(L) {
- lua_getglobal(L, func);
- if (!lua_isfunction(L, -1)) {
- int t = lua_type(L, -1);
- SkDebugf("--- expected function %d\n", t);
- }
- lua_newtable(L);
- this->pushString(verb, "verb");
- }
- ~AutoCallLua() {
- lua_State* L = this->get();
- if (lua_pcall(L, 1, 0, 0) != LUA_OK) {
- SkDebugf("lua err: %s\n", lua_tostring(L, -1));
- }
- lua_settop(L, -1);
- }
- void pushEncodedText(SkTextEncoding, const void*, size_t);
- private:
- typedef SkLua INHERITED;
- };
- #define AUTO_LUA(verb) AutoCallLua lua(fL, fFunc.c_str(), verb)
- ///////////////////////////////////////////////////////////////////////////////
- void AutoCallLua::pushEncodedText(SkTextEncoding enc, const void* text, size_t length) {
- switch (enc) {
- case SkTextEncoding::kUTF8:
- this->pushString((const char*)text, length, "text");
- break;
- case SkTextEncoding::kUTF16:
- this->pushString(SkStringFromUTF16((const uint16_t*)text, length), "text");
- break;
- case SkTextEncoding::kGlyphID:
- this->pushArrayU16((const uint16_t*)text, SkToInt(length >> 1),
- "glyphs");
- break;
- case SkTextEncoding::kUTF32:
- break;
- }
- }
- ///////////////////////////////////////////////////////////////////////////////
- void SkLuaCanvas::pushThis() {
- SkLua(fL).pushCanvas(this);
- }
- ///////////////////////////////////////////////////////////////////////////////
- SkLuaCanvas::SkLuaCanvas(int width, int height, lua_State* L, const char func[])
- : INHERITED(width, height)
- , fL(L)
- , fFunc(func) {
- }
- SkLuaCanvas::~SkLuaCanvas() {}
- void SkLuaCanvas::willSave() {
- AUTO_LUA("save");
- this->INHERITED::willSave();
- }
- SkCanvas::SaveLayerStrategy SkLuaCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) {
- AUTO_LUA("saveLayer");
- if (rec.fBounds) {
- lua.pushRect(*rec.fBounds, "bounds");
- }
- if (rec.fPaint) {
- lua.pushPaint(*rec.fPaint, "paint");
- }
- (void)this->INHERITED::getSaveLayerStrategy(rec);
- // No need for a layer.
- return kNoLayer_SaveLayerStrategy;
- }
- bool SkLuaCanvas::onDoSaveBehind(const SkRect*) {
- // TODO
- return false;
- }
- void SkLuaCanvas::willRestore() {
- AUTO_LUA("restore");
- this->INHERITED::willRestore();
- }
- void SkLuaCanvas::didConcat(const SkMatrix& matrix) {
- switch (matrix.getType()) {
- case SkMatrix::kTranslate_Mask: {
- AUTO_LUA("translate");
- lua.pushScalar(matrix.getTranslateX(), "dx");
- lua.pushScalar(matrix.getTranslateY(), "dy");
- break;
- }
- case SkMatrix::kScale_Mask: {
- AUTO_LUA("scale");
- lua.pushScalar(matrix.getScaleX(), "sx");
- lua.pushScalar(matrix.getScaleY(), "sy");
- break;
- }
- default: {
- AUTO_LUA("concat");
- // pushMatrix added in https://codereview.chromium.org/203203004/
- // Doesn't seem to have ever been working correctly since added
- // lua.pushMatrix(matrix);
- break;
- }
- }
- this->INHERITED::didConcat(matrix);
- }
- void SkLuaCanvas::didSetMatrix(const SkMatrix& matrix) {
- this->INHERITED::didSetMatrix(matrix);
- }
- void SkLuaCanvas::onClipRect(const SkRect& r, SkClipOp op, ClipEdgeStyle edgeStyle) {
- AUTO_LUA("clipRect");
- lua.pushRect(r, "rect");
- lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
- this->INHERITED::onClipRect(r, op, edgeStyle);
- }
- void SkLuaCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) {
- AUTO_LUA("clipRRect");
- lua.pushRRect(rrect, "rrect");
- lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
- this->INHERITED::onClipRRect(rrect, op, edgeStyle);
- }
- void SkLuaCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) {
- AUTO_LUA("clipPath");
- lua.pushPath(path, "path");
- lua.pushBool(kSoft_ClipEdgeStyle == edgeStyle, "aa");
- this->INHERITED::onClipPath(path, op, edgeStyle);
- }
- void SkLuaCanvas::onClipRegion(const SkRegion& deviceRgn, SkClipOp op) {
- AUTO_LUA("clipRegion");
- this->INHERITED::onClipRegion(deviceRgn, op);
- }
- void SkLuaCanvas::onDrawPaint(const SkPaint& paint) {
- AUTO_LUA("drawPaint");
- lua.pushPaint(paint, "paint");
- }
- void SkLuaCanvas::onDrawPoints(PointMode mode, size_t count,
- const SkPoint pts[], const SkPaint& paint) {
- AUTO_LUA("drawPoints");
- lua.pushArrayPoint(pts, SkToInt(count), "points");
- lua.pushPaint(paint, "paint");
- }
- void SkLuaCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
- AUTO_LUA("drawOval");
- lua.pushRect(rect, "rect");
- lua.pushPaint(paint, "paint");
- }
- void SkLuaCanvas::onDrawArc(const SkRect& rect, SkScalar startAngle, SkScalar sweepAngle,
- bool useCenter, const SkPaint& paint) {
- AUTO_LUA("drawArc");
- lua.pushRect(rect, "rect");
- lua.pushScalar(startAngle, "startAngle");
- lua.pushScalar(sweepAngle, "sweepAngle");
- lua.pushBool(useCenter, "useCenter");
- lua.pushPaint(paint, "paint");
- }
- void SkLuaCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
- AUTO_LUA("drawRect");
- lua.pushRect(rect, "rect");
- lua.pushPaint(paint, "paint");
- }
- void SkLuaCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
- AUTO_LUA("drawRRect");
- lua.pushRRect(rrect, "rrect");
- lua.pushPaint(paint, "paint");
- }
- void SkLuaCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
- const SkPaint& paint) {
- AUTO_LUA("drawDRRect");
- lua.pushRRect(outer, "outer");
- lua.pushRRect(inner, "inner");
- lua.pushPaint(paint, "paint");
- }
- void SkLuaCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
- AUTO_LUA("drawPath");
- lua.pushPath(path, "path");
- lua.pushPaint(paint, "paint");
- }
- void SkLuaCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
- const SkPaint* paint) {
- AUTO_LUA("drawBitmap");
- if (paint) {
- lua.pushPaint(*paint, "paint");
- }
- }
- void SkLuaCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
- const SkPaint* paint, SrcRectConstraint) {
- AUTO_LUA("drawBitmapRect");
- if (paint) {
- lua.pushPaint(*paint, "paint");
- }
- }
- void SkLuaCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst,
- const SkPaint* paint) {
- AUTO_LUA("drawBitmapNine");
- if (paint) {
- lua.pushPaint(*paint, "paint");
- }
- }
- void SkLuaCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) {
- AUTO_LUA("drawImage");
- if (paint) {
- lua.pushPaint(*paint, "paint");
- }
- }
- void SkLuaCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
- const SkPaint* paint, SrcRectConstraint) {
- AUTO_LUA("drawImageRect");
- if (paint) {
- lua.pushPaint(*paint, "paint");
- }
- }
- void SkLuaCanvas::onDrawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y,
- const SkPaint &paint) {
- AUTO_LUA("drawTextBlob");
- lua.pushTextBlob(blob, "blob");
- lua.pushScalar(x, "x");
- lua.pushScalar(y, "y");
- lua.pushPaint(paint, "paint");
- }
- void SkLuaCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
- const SkPaint* paint) {
- AUTO_LUA("drawPicture");
- // call through so we can see the nested picture ops
- this->INHERITED::onDrawPicture(picture, matrix, paint);
- }
- void SkLuaCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matrix) {
- AUTO_LUA("drawDrawable");
- // call through so we can see the nested ops
- this->INHERITED::onDrawDrawable(drawable, matrix);
- }
- void SkLuaCanvas::onDrawVerticesObject(const SkVertices*, const SkVertices::Bone[], int,
- SkBlendMode, const SkPaint& paint) {
- AUTO_LUA("drawVertices");
- lua.pushPaint(paint, "paint");
- }
|