Add point count to lua SkPath.

Add some scripts for classifying rrects and counting complex clip combinations.

R=reed@google.com

Author: bsalomon@google.com

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

git-svn-id: http://skia.googlecode.com/svn/trunk@13601 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/utils/SkLua.cpp b/src/utils/SkLua.cpp
index d1193a7..4b87458 100644
--- a/src/utils/SkLua.cpp
+++ b/src/utils/SkLua.cpp
@@ -1137,6 +1137,11 @@
     return ret_count;
 }
 
+static int lpath_countPoints(lua_State* L) {
+    lua_pushinteger(L, get_obj<SkPath>(L, 1)->countPoints());
+    return 1;
+}
+
 static int lpath_reset(lua_State* L) {
     get_obj<SkPath>(L, 1)->reset();
     return 0;
@@ -1183,6 +1188,7 @@
     { "isEmpty", lpath_isEmpty },
     { "isRect", lpath_isRect },
     { "isNestedRects", lpath_isNestedRects },
+    { "countPoints", lpath_countPoints },
     { "reset", lpath_reset },
     { "moveTo", lpath_moveTo },
     { "lineTo", lpath_lineTo },
diff --git a/tools/lua/classify_rrect_clips.lua b/tools/lua/classify_rrect_clips.lua
new file mode 100644
index 0000000..792d169
--- /dev/null
+++ b/tools/lua/classify_rrect_clips.lua
@@ -0,0 +1,109 @@
+
+function sk_scrape_startcanvas(c, fileName) end
+
+function sk_scrape_endcanvas(c, fileName) end
+
+function classify_rrect(rrect)
+    if (rrect:type() == "simple") then
+        local x, y = rrect:radii(0)
+        if (x == y) then
+            return "simple_circle"
+        else
+            return "simple_oval"
+        end
+    elseif (rrect:type() == "complex") then
+        local numNotSquare = 0
+        local rx, ry
+        local same = true;
+        local first_not_square_corner
+        local last_not_square_corner
+        for i = 1, 4 do
+            local x, y = rrect:radii(i-1)
+            if (x ~= 0 and y ~= 0) then
+                if (numNotSquare == 0) then
+                    rx = x
+                    ry = y
+                    first_not_square_corner = i
+                else
+                   last_not_square_corner = i
+                   if (rx ~= x or ry ~=y) then
+                       same = false
+                   end
+                end
+                numNotSquare = numNotSquare + 1
+            end
+        end
+        local numSquare = 4 - numNotSquare
+        if (numSquare > 0 and same) then
+            local corners = "corners"
+            if (numSquare == 2) then
+                if ((last_not_square_corner - 1 == first_not_square_corner) or
+                    (1 == first_not_square_corner and 4 == last_not_square_corner )) then
+                    corners = "adjacent_" .. corners
+                else
+                    corners = "opposite_" .. corners
+                end
+            elseif (1 == numSquare) then
+                corners = "corner"
+            end
+            if (rx == ry) then
+                return "circles_with_" .. numSquare .. "_square_" .. corners
+            else
+                return "ovals_with_" .. numSquare .. "_square_" .. corners
+            end
+        end
+        return "complex_unclassified"
+    elseif (rrect:type() == "rect") then
+        return "rect"
+    elseif (rrect:type() == "oval") then
+        local x, y = rrect:radii(0)
+        if (x == y) then
+            return "circle"
+        else
+            return "oval"
+        end
+    elseif (rrect:type() == "empty") then
+        return "empty"
+    else
+        return "unknown"
+    end
+end
+
+function print_classes(class_table)
+  function sort_classes(a, b)
+     return a.count > b.count
+  end
+  array = {}
+  for k, v in pairs(class_table) do
+      if (type(v) == "number") then
+          array[#array + 1] = {class = k, count = v};
+      end
+  end
+  table.sort(array, sort_classes)
+  local i
+  for i = 1, #array do
+      io.write(array[i].class, ": ", array[i].count, " (", array[i].count/class_table["total"] * 100, "%)\n");
+  end
+end
+
+function sk_scrape_accumulate(t)
+    if (t.verb == "clipRRect") then
+        local rrect = t.rrect
+        table["total"] = (table["total"] or 0) + 1
+        local class = classify_rrect(rrect)
+        table[class] = (table[class] or 0) + 1
+    end
+end
+
+function sk_scrape_summarize()
+  print_classes(table)
+  --[[ To use the web scraper comment out the above call to print_classes, run the code below,
+       and in the aggregator pass agg_table to print_classes.
+  for k, v in pairs(table) do
+      if (type(v) == "number") then
+          local t = "agg_table[\"" .. k .. "\"]"
+          io.write(t, " = (", t, " or 0) + ", table[k], "\n" );
+      end
+  end
+  --]]
+end
diff --git a/tools/lua/count_reduced_clipstacks.lua b/tools/lua/count_reduced_clipstacks.lua
new file mode 100644
index 0000000..1ea7a44
--- /dev/null
+++ b/tools/lua/count_reduced_clipstacks.lua
@@ -0,0 +1,87 @@
+stats = {}
+
+-- switch this to run on the automated scraper system
+newline = "\n"
+-- newline = "\\n"
+
+function sk_scrape_startcanvas(c, fileName)
+    canvas = c
+    oldstackstr = "<invalid>"
+end
+
+function sk_scrape_endcanvas(c, fileName)
+    canvas = nil
+end
+
+function string.starts(String,Start)
+   return string.sub(String,1,string.len(Start))==Start
+end
+
+function build_stack_string(stack)
+    local info = ""
+    for i = 1, #stack do
+        local element = stack[i];
+        info = info .. element["op"] .. ", " .. element["type"] .. ", aa:" .. tostring(element["aa"])
+        if (element["type"] == "path") then
+            if (element["path"]:getSegmentTypes() == "line" and element["path"]:isConvex()) then
+                info = info .. ", convex_poly " .. element["path"]:countPoints() .. " points"
+            else
+                info = info .. ", fill: " .. element["path"]:getFillType()
+                info = info .. ", segments: (" .. element["path"]:getSegmentTypes() .. ")"
+                info = info .. ", convex:" .. tostring(element["path"]:isConvex())
+            end
+        end
+        info = info .. newline
+    end
+    return info
+end
+
+function sk_scrape_accumulate(t)
+    if (string.starts(t.verb, "draw")) then
+        local stack = canvas:getReducedClipStack()
+        local stackstr = build_stack_string(stack)
+        if (stackstr ~= "") then
+            if (stats[stackstr] == nil) then
+                stats[stackstr] = {}
+                stats[stackstr].drawCnt = 0
+                stats[stackstr].instanceCnt = 0
+            end
+            stats[stackstr].drawCnt = stats[stackstr].drawCnt + 1
+            if (stackstr ~= oldstackstr) then
+                stats[stackstr].instanceCnt = stats[stackstr].instanceCnt + 1
+            end
+        end
+        oldstackstr = stackstr
+    end
+end
+
+function print_stats(stats)
+    function sort_by_draw_cnt(a, b)
+        return a.data.drawCnt > b.data.drawCnt
+    end
+    array = {}
+    for k,v in pairs(stats) do
+        array[#array + 1] = { name = k, data = v }
+    end
+    table.sort(array, sort_by_draw_cnt)
+    for i = 1, #array do
+        io.write("\n-------\n", array[i].name, tostring(array[i].data.drawCnt), " draws, ", tostring(array[i].data.instanceCnt), " instances.\n")
+    end
+end
+
+function sk_scrape_summarize()
+    print_stats(stats)
+    --[[ To use the web scraper comment out the print above, run the code below to generate an
+         aggregate table on the automated scraper system. Then use the print_stats function on
+         agg_stats in the aggregator step.
+    for k,v in pairs(stats) do
+        if (v.drawCnt ~= nil) then
+             -- io.write("\n-------\n", k, tostring(v.drawCnt), " draws, ", tostring(v.instanceCnt), " instances.\n")
+             local tableEntry = 'agg_stats["' .. k .. '"]'
+             io.write(tableEntry, " = ", tableEntry, " or {}\n")
+             io.write(tableEntry, ".drawCnt = (", tableEntry, ".drawCnt or 0 ) + ", v.drawCnt, "\n")
+             io.write(tableEntry, ".instanceCnt = (", tableEntry, ".instanceCnt or 0 ) + ", v.instanceCnt, "\n")
+        end
+    end
+    --]]
+end