add initial scraper for dashing
authorcommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 17 Mar 2014 23:09:47 +0000 (23:09 +0000)
committercommit-bot@chromium.org <commit-bot@chromium.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 17 Mar 2014 23:09:47 +0000 (23:09 +0000)
BUG=skia:
R=bsalomon@google.com

Author: reed@google.com

Review URL: https://codereview.chromium.org/196603027

git-svn-id: http://skia.googlecode.com/svn/trunk@13843 2bbb7eff-a529-9590-31e7-b0007b416f81

include/utils/SkLua.h
src/utils/SkLua.cpp
src/utils/SkLuaCanvas.cpp
tools/lua/scrape_dashing.lua [new file with mode: 0644]

index 2f29343d595c4f863245ad0591e84297e058dddf..9a1ebfdebb3842ce82ca19fe4baf00b5a9ea95d2 100644 (file)
@@ -45,6 +45,7 @@ public:
     void pushString(const char[], size_t len, const char tableKey[] = NULL);
     void pushString(const SkString&, const char tableKey[] = NULL);
     void pushArrayU16(const uint16_t[], int count, const char tableKey[] = NULL);
+    void pushArrayPoint(const SkPoint[], int count, const char key[] = NULL);
     void pushColor(SkColor, const char tableKey[] = NULL);
     void pushU32(uint32_t, const char tableKey[] = NULL);
     void pushScalar(SkScalar, const char tableKey[] = NULL);
index 44f2211b8f4f37253678a44d120fb0f9885d32df..346899a5dde785b0e00fd233aa1df8df494b7c06 100644 (file)
@@ -18,6 +18,7 @@
 #include "SkMatrix.h"
 #include "SkPaint.h"
 #include "SkPath.h"
+#include "SkPathEffect.h"
 #include "SkPixelRef.h"
 #include "SkRRect.h"
 #include "SkString.h"
@@ -43,6 +44,7 @@ DEF_MTNAME(SkMatrix)
 DEF_MTNAME(SkRRect)
 DEF_MTNAME(SkPath)
 DEF_MTNAME(SkPaint)
+DEF_MTNAME(SkPathEffect)
 DEF_MTNAME(SkShader)
 DEF_MTNAME(SkTypeface)
 
@@ -205,6 +207,18 @@ void SkLua::pushArrayU16(const uint16_t array[], int count, const char key[]) {
     CHECK_SETFIELD(key);
 }
 
+void SkLua::pushArrayPoint(const SkPoint array[], int count, const char key[]) {
+    lua_newtable(fL);
+    for (int i = 0; i < count; ++i) {
+        // make it base-1 to match lua convention
+        lua_newtable(fL);
+        this->pushScalar(array[i].fX, "x");
+        this->pushScalar(array[i].fY, "y");
+        lua_rawseti(fL, -2, i + 1);
+    }
+    CHECK_SETFIELD(key);
+}
+
 void SkLua::pushRect(const SkRect& r, const char key[]) {
     lua_newtable(fL);
     setfield_scalar(fL, "left", r.fLeft);
@@ -845,6 +859,16 @@ static int lpaint_getShader(lua_State* L) {
     return 0;
 }
 
+static int lpaint_getPathEffect(lua_State* L) {
+    const SkPaint* paint = get_obj<SkPaint>(L, 1);
+    SkPathEffect* pe = paint->getPathEffect();
+    if (pe) {
+        push_ref(L, pe);
+        return 1;
+    }
+    return 0;
+}
+
 static int lpaint_gc(lua_State* L) {
     get_obj<SkPaint>(L, 1)->~SkPaint();
     return 0;
@@ -888,6 +912,7 @@ static const struct luaL_Reg gSkPaint_Methods[] = {
     { "getFontMetrics", lpaint_getFontMetrics },
     { "getEffects", lpaint_getEffects },
     { "getShader", lpaint_getShader },
+    { "getPathEffect", lpaint_getPathEffect },
     { "__gc", lpaint_gc },
     { NULL, NULL }
 };
@@ -981,6 +1006,18 @@ static const struct luaL_Reg gSkShader_Methods[] = {
 
 ///////////////////////////////////////////////////////////////////////////////
 
+static int lpatheffect_gc(lua_State* L) {
+    get_ref<SkPathEffect>(L, 1)->unref();
+    return 0;
+}
+
+static const struct luaL_Reg gSkPathEffect_Methods[] = {
+    { "__gc",           lpatheffect_gc },
+    { NULL, NULL }
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
 static int lmatrix_getType(lua_State* L) {
     SkMatrix::TypeMask mask = get_obj<SkMatrix>(L, 1)->getType();
 
@@ -1419,8 +1456,9 @@ void SkLua::Load(lua_State* L) {
     REG_CLASS(L, SkCanvas);
     REG_CLASS(L, SkDocument);
     REG_CLASS(L, SkImage);
-    REG_CLASS(L, SkPath);
     REG_CLASS(L, SkPaint);
+    REG_CLASS(L, SkPath);
+    REG_CLASS(L, SkPathEffect);
     REG_CLASS(L, SkRRect);
     REG_CLASS(L, SkShader);
     REG_CLASS(L, SkTypeface);
index 9289beed887a9fc3ec46f5aade60b2fa3797a4cc..b5789e058b95cb728882a897b64cf2067a81ec99 100644 (file)
@@ -176,6 +176,7 @@ void SkLuaCanvas::drawPaint(const SkPaint& paint) {
 void SkLuaCanvas::drawPoints(PointMode mode, size_t count,
                                const SkPoint pts[], const SkPaint& paint) {
     AUTO_LUA("drawPoints");
+    lua.pushArrayPoint(pts, count, "points");
     lua.pushPaint(paint, "paint");
 }
 
diff --git a/tools/lua/scrape_dashing.lua b/tools/lua/scrape_dashing.lua
new file mode 100644 (file)
index 0000000..48f3538
--- /dev/null
@@ -0,0 +1,93 @@
+function tostr(t)
+    local str = ""
+    for k, v in next, t do
+        if #str > 0 then
+            str = str .. ", "
+        end
+        if type(k) == "number" then
+            str = str .. "[" .. k .. "] = "
+        else
+            str = str .. tostring(k) .. " = "
+        end
+        if type(v) == "table" then
+            str = str .. "{ " .. tostr(v) .. " }"
+        else
+            str = str .. tostring(v)
+        end
+    end
+    return str
+end
+
+local total_found = {}    -- accumulate() stores its data in here
+local total_total = {}
+local canvas        -- holds the current canvas (from startcanvas())
+
+--[[
+    startcanvas() is called at the start of each picture file, passing the
+    canvas that we will be drawing into, and the name of the file.
+    
+    Following this call, there will be some number of calls to accumulate(t)
+    where t is a table of parameters that were passed to that draw-op.
+    
+        t.verb is a string holding the name of the draw-op (e.g. "drawRect")
+    
+    when a given picture is done, we call endcanvas(canvas, fileName)
+]]
+function sk_scrape_startcanvas(c, fileName)
+    canvas = c
+end
+
+--[[
+    Called when the current canvas is done drawing.
+]]
+function sk_scrape_endcanvas(c, fileName)
+    canvas = nil
+end
+
+function increment(table, key)
+    table[key] = (table[key] or 0) + 1
+end
+
+
+local drawPointsTable = {}
+local drawPointsTable_direction = {}
+
+function sk_scrape_accumulate(t)
+    increment(total_total, t.verb)
+
+    local p = t.paint
+    if p then
+        local pe = p:getPathEffect();
+        if pe then
+            increment(total_found, t.verb)
+        end
+    end
+
+    if "drawPoints" == t.verb then
+        local points = t.points
+        increment(drawPointsTable, #points)
+        if 2 == #points then
+            if points[1].y == points[2].y then
+                increment(drawPointsTable_direction, "hori")
+            elseif points[1].x == points[2].x then
+                increment(drawPointsTable_direction, "vert")
+            else
+                increment(drawPointsTable_direction, "other")
+            end
+        end
+    end
+end
+
+--[[
+    lua_pictures will call this function after all of the pictures have been
+    "accumulated".
+]]
+function sk_scrape_summarize()
+    for k, v in next, total_found do
+        io.write(k, " = ", v, "/", total_total[k], "\n")
+    end
+    print("histogram of point-counts for all drawPoints calls")
+    print(tostr(drawPointsTable))
+    print(tostr(drawPointsTable_direction))
+end
+