report back colorfilters in lua
authorreed <reed@google.com>
Sat, 5 Dec 2015 04:45:59 +0000 (20:45 -0800)
committerCommit bot <commit-bot@chromium.org>
Sat, 5 Dec 2015 04:45:59 +0000 (20:45 -0800)
BUG=skia:
TBR=

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

src/utils/SkLua.cpp
tools/lua/filter-counter.lua [new file with mode: 0644]

index 307e5f6..d85fb91 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "SkBlurImageFilter.h"
 #include "SkCanvas.h"
+#include "SkColorFilter.h"
 #include "SkData.h"
 #include "SkDocument.h"
 #include "SkGradientShader.h"
@@ -42,6 +43,7 @@ template <typename T> const char* get_mtname();
     }
 
 DEF_MTNAME(SkCanvas)
+DEF_MTNAME(SkColorFilter)
 DEF_MTNAME(SkDocument)
 DEF_MTNAME(SkImage)
 DEF_MTNAME(SkImageFilter)
@@ -1079,6 +1081,22 @@ static int lpaint_getEffects(lua_State* L) {
     return 1;
 }
 
+static int lpaint_getColorFilter(lua_State* L) {
+    const SkPaint* paint = get_obj<SkPaint>(L, 1);
+    SkColorFilter* cf = paint->getColorFilter();
+    if (cf) {
+        push_ref(L, cf);
+        return 1;
+    }
+    return 0;
+}
+
+static int lpaint_setColorFilter(lua_State* L) {
+    SkPaint* paint = get_obj<SkPaint>(L, 1);
+    paint->setColorFilter(get_ref<SkColorFilter>(L, 2));
+    return 0;
+}
+
 static int lpaint_getImageFilter(lua_State* L) {
     const SkPaint* paint = get_obj<SkPaint>(L, 1);
     SkImageFilter* imf = paint->getImageFilter();
@@ -1170,6 +1188,8 @@ static const struct luaL_Reg gSkPaint_Methods[] = {
     { "measureText", lpaint_measureText },
     { "getFontMetrics", lpaint_getFontMetrics },
     { "getEffects", lpaint_getEffects },
+    { "getColorFilter", lpaint_getColorFilter },
+    { "setColorFilter", lpaint_setColorFilter },
     { "getImageFilter", lpaint_getImageFilter },
     { "setImageFilter", lpaint_setImageFilter },
     { "getShader", lpaint_getShader },
@@ -1294,6 +1314,18 @@ static const struct luaL_Reg gSkPathEffect_Methods[] = {
 
 ///////////////////////////////////////////////////////////////////////////////
 
+static int lpcolorfilter_gc(lua_State* L) {
+    get_ref<SkColorFilter>(L, 1)->unref();
+    return 0;
+}
+
+static const struct luaL_Reg gSkColorFilter_Methods[] = {
+    { "__gc",       lpcolorfilter_gc },
+    { nullptr, nullptr }
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
 static int lpimagefilter_gc(lua_State* L) {
     get_ref<SkImageFilter>(L, 1)->unref();
     return 0;
@@ -2048,6 +2080,7 @@ static void register_Sk(lua_State* L) {
 void SkLua::Load(lua_State* L) {
     register_Sk(L);
     REG_CLASS(L, SkCanvas);
+    REG_CLASS(L, SkColorFilter);
     REG_CLASS(L, SkDocument);
     REG_CLASS(L, SkImage);
     REG_CLASS(L, SkImageFilter);
diff --git a/tools/lua/filter-counter.lua b/tools/lua/filter-counter.lua
new file mode 100644 (file)
index 0000000..f678bbd
--- /dev/null
@@ -0,0 +1,91 @@
+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 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
+
+--[[
+    Called with the parameters to each canvas.draw call, where canvas is the
+    current canvas as set by startcanvas()
+]]
+
+local gCF_Count = 0
+local gIF_Count = 0
+local gBOTH_Count = 0
+
+function sk_scrape_accumulate(t)
+    if not t.paint then
+        return
+    end
+
+    local colorFilter = t.paint:getColorFilter()
+    local imageFilter = t.paint:getImageFilter()
+
+    if colorFilter then
+        gCF_Count = gCF_Count + 1
+    end
+    if imageFilter then
+        gIF_Count = gIF_Count + 1
+    end
+    if colorFilter and imageFilter then
+        gBOTH_Count = gBOTH_Count + 1
+    end
+end
+
+--[[
+    lua_pictures will call this function after all of the pictures have been
+    "accumulated".
+]]
+function sk_scrape_summarize()
+    io.write("colorfilters ", gCF_Count, ", imagefilters ", gIF_Count, ", both_filters ", gBOTH_Count, "\n")
+
+--[[
+    io.write("\n\nFirst glyph spread\n\n")
+    for k, v in next, gFirstGlyphs do
+        io.write("glyph, ", k, ",count, ", v, "\n")
+    end
+]]
+end
+
+function test_summary()
+    io.write("just testing test_summary\n")
+end
+