add -pict option to bench, which draws everything through a picture, and then
compares the result to the original draws



git-svn-id: http://skia.googlecode.com/svn/trunk@139 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/bench/main.cpp b/bench/main.cpp
index 77f49dc..f92277a 100644
--- a/bench/main.cpp
+++ b/bench/main.cpp
@@ -2,11 +2,38 @@
 #include "SkColorPriv.h"
 #include "SkGraphics.h"
 #include "SkImageEncoder.h"
+#include "SkNWayCanvas.h"
+#include "SkPicture.h"
 #include "SkString.h"
 #include "SkTime.h"
 
 #include "SkBenchmark.h"
 
+static void erase(SkBitmap& bm) {
+    if (bm.config() == SkBitmap::kA8_Config) {
+        bm.eraseColor(0);
+    } else {
+        bm.eraseColor(SK_ColorWHITE);
+    }
+}
+
+static bool equal(const SkBitmap& bm1, const SkBitmap& bm2) {
+    if (bm1.width() != bm2.width() ||
+        bm1.height() != bm2.height() ||
+        bm1.config() != bm2.config()) {
+        return false;
+    }
+    
+    size_t pixelBytes = bm1.width() * bm1.bytesPerPixel();
+    for (int y = 0; y < bm1.height(); y++) {
+        if (memcmp(bm1.getAddr(0, y), bm2.getAddr(0, y), pixelBytes)) {
+            return false;
+        }
+    }
+
+    return true;
+}
+
 class Iter {
 public:
     Iter() {
@@ -91,6 +118,21 @@
     canvas->translate(-x, -y);
 }
 
+static void compare_pict_to_bitmap(SkPicture* pict, const SkBitmap& bm) {
+    SkBitmap bm2;
+    
+    bm2.setConfig(bm.config(), bm.width(), bm.height());
+    bm2.allocPixels();
+    erase(bm2);
+
+    SkCanvas canvas(bm2);
+    canvas.drawPicture(*pict);
+
+    if (!equal(bm, bm2)) {
+        SkDebugf("----- compare_pict_to_bitmap failed\n");
+    }
+}
+
 static const struct {
     SkBitmap::Config    fConfig;
     const char*         fName;
@@ -128,6 +170,7 @@
     bool forceAA = true;
     bool doRotate = false;
     bool doClip = false;
+    bool doPict = false;
 
     SkString outDir;
     SkBitmap::Config outConfig = SkBitmap::kARGB_8888_Config;
@@ -142,6 +185,8 @@
                     outDir.append("/");
                 }
             }
+        } else if (strcmp(*argv, "-pict") == 0) {
+            doPict = true;
         } else if (strcmp(*argv, "-repeat") == 0) {
             argv++;
             if (argv < stop) {
@@ -198,12 +243,7 @@
             SkBitmap bm;
             bm.setConfig(outConfig, dim.fX, dim.fY);
             bm.allocPixels();
-            
-            if (bm.config() == SkBitmap::kA8_Config) {
-                bm.eraseColor(0);
-            } else {
-                bm.eraseColor(SK_ColorWHITE);
-            }
+            erase(bm);
 
             SkCanvas canvas(bm);
 
@@ -216,8 +256,24 @@
 
             SkMSec now = SkTime::GetMSecs();
             for (int i = 0; i < repeatDraw; i++) {
-                SkAutoCanvasRestore acr(&canvas, true);
-                bench->draw(&canvas);
+                SkCanvas* c = &canvas;
+
+                SkNWayCanvas nway;
+                SkPicture* pict = NULL;
+                if (doPict) {
+                    pict = new SkPicture;
+                    nway.addCanvas(pict->beginRecording(bm.width(), bm.height()));
+                    nway.addCanvas(&canvas);
+                    c = &nway;
+                }
+
+                SkAutoCanvasRestore acr(c, true);
+                bench->draw(c);
+                
+                if (pict) {
+                    compare_pict_to_bitmap(pict, bm);
+                    pict->unref();
+                }
             }
             if (repeatDraw > 1) {
                 printf("  %4s:%7.2f", configName,