add GPU config
authormike@reedtribe.org <mike@reedtribe.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 17 May 2011 02:25:05 +0000 (02:25 +0000)
committermike@reedtribe.org <mike@reedtribe.org@2bbb7eff-a529-9590-31e7-b0007b416f81>
Tue, 17 May 2011 02:25:05 +0000 (02:25 +0000)
git-svn-id: http://skia.googlecode.com/svn/trunk@1341 2bbb7eff-a529-9590-31e7-b0007b416f81

bench/benchmain.cpp

index 4f6d81c..38724c1 100644 (file)
@@ -6,6 +6,9 @@
 #include "SkPicture.h"
 #include "SkString.h"
 #include "SkTime.h"
+#include "GrContext.h"
+#include "SkGpuDevice.h"
+#include "SkEGLContext.h"
 
 #include "SkBenchmark.h"
 
@@ -30,6 +33,7 @@ static void erase(SkBitmap& bm) {
     }
 }
 
+#if 0
 static bool equal(const SkBitmap& bm1, const SkBitmap& bm2) {
     if (bm1.width() != bm2.width() ||
         bm1.height() != bm2.height() ||
@@ -43,9 +47,9 @@ static bool equal(const SkBitmap& bm1, const SkBitmap& bm2) {
             return false;
         }
     }
-
     return true;
 }
+#endif
 
 class Iter {
 public:
@@ -143,21 +147,6 @@ static void performScale(SkCanvas* canvas, int w, int h) {
     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 bool parse_bool_arg(char * const* argv, char* const* stop, bool* var) {
     if (argv < stop) {
         *var = atoi(*argv) != 0;
@@ -166,16 +155,43 @@ static bool parse_bool_arg(char * const* argv, char* const* stop, bool* var) {
     return false;
 }
 
+enum Backend {
+    kRaster_Backend,
+    kGPU_Backend,
+    kPDF_Backend,
+};
+
+static SkDevice* make_device(SkBitmap::Config config, const SkIPoint& size,
+                             Backend backend, GrContext* context) {
+    SkDevice* device = NULL;
+    SkBitmap bitmap;
+    bitmap.setConfig(config, size.fX, size.fY);
+
+    switch (backend) {
+        case kRaster_Backend:
+            bitmap.allocPixels();
+            erase(bitmap);
+            device = new SkDevice(NULL, bitmap, true);
+            break;
+        case kGPU_Backend:
+            device = new SkGpuDevice(context, bitmap, SkGpuDevice::Current3DApiRenderTarget());
+//            device->clear(0xFFFFFFFF);
+            break;
+        case kPDF_Backend:
+        default:
+            SkASSERT(!"unsupported");
+    }
+    return device;
+}
+
 static const struct {
     SkBitmap::Config    fConfig;
     const char*         fName;
+    Backend             fBackend;
 } gConfigs[] = {
-    { SkBitmap::kARGB_8888_Config,  "8888" },
-    { SkBitmap::kRGB_565_Config,    "565",  },
-#if 0
-    { SkBitmap::kARGB_4444_Config,  "4444", },
-    { SkBitmap::kA8_Config,         "A8",   }
-#endif
+    { SkBitmap::kARGB_8888_Config,  "8888",     kRaster_Backend },
+    { SkBitmap::kRGB_565_Config,    "565",      kRaster_Backend },
+    { SkBitmap::kARGB_8888_Config,  "GPU",      kGPU_Backend },
 };
 
 static int findConfig(const char config[]) {
@@ -199,7 +215,6 @@ int main (int argc, char * const argv[]) {
     bool doScale = false;
     bool doRotate = false;
     bool doClip = false;
-    bool doPict = false;
     const char* matchStr = NULL;
     bool hasStrokeWidth = false;
     float strokeWidth;
@@ -207,6 +222,7 @@ int main (int argc, char * const argv[]) {
     SkString outDir;
     SkBitmap::Config outConfig = SkBitmap::kNo_Config;
     const char* configName = "";
+    Backend backend = kRaster_Backend;  // for warning
     int configCount = SK_ARRAY_COUNT(gConfigs);
 
     char* const* stop = argv + argc;
@@ -219,8 +235,6 @@ int main (int argc, char * const argv[]) {
                     outDir.append("/");
                 }
             }
-        } else if (strcmp(*argv, "-pict") == 0) {
-            doPict = true;
         } else if (strcmp(*argv, "-repeat") == 0) {
             argv++;
             if (argv < stop) {
@@ -290,6 +304,7 @@ int main (int argc, char * const argv[]) {
                 if (index >= 0) {
                     outConfig = gConfigs[index].fConfig;
                     configName = gConfigs[index].fName;
+                    backend = gConfigs[index].fBackend;
                     configCount = 1;
                 } else {
                     SkString str;
@@ -324,7 +339,13 @@ int main (int argc, char * const argv[]) {
                    forceAlpha, forceAA, forceFilter);
         log_progress(str);
     }
-                   
+
+    GrContext* context = NULL;
+    SkEGLContext eglContext;
+    if (eglContext.init(1024, 1024)) {
+        context = GrContext::CreateGLShaderContext();
+    }
+    
     Iter iter(&defineDict);
     SkBenchmark* bench;
     while ((bench = iter.next()) != NULL) {
@@ -357,14 +378,16 @@ int main (int argc, char * const argv[]) {
             if (configCount > 1) {
                 outConfig = gConfigs[configIndex].fConfig;
                 configName = gConfigs[configIndex].fName;
+                backend = gConfigs[configIndex].fBackend;
+            }
+
+            if (kGPU_Backend == backend && NULL == context) {
+                continue;
             }
-            
-            SkBitmap bm;
-            bm.setConfig(outConfig, dim.fX, dim.fY);
-            bm.allocPixels();
-            erase(bm);
 
-            SkCanvas canvas(bm);
+            SkDevice* device = make_device(outConfig, dim, backend, context);
+            SkCanvas canvas(device);
+            device->unref();
 
             if (doClip) {
                 performClip(&canvas, dim.fX, dim.fY);
@@ -383,25 +406,13 @@ int main (int argc, char * const argv[]) {
 
             SkMSec now = SkTime::GetMSecs();
             for (int i = 0; i < repeatDraw; i++) {
-                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();
-                }
+                SkAutoCanvasRestore acr(&canvas, true);
+                bench->draw(&canvas);
+            }
+            if (context) {
+                context->flush();
             }
+
             if (repeatDraw > 1) {
                 double duration = SkTime::GetMSecs() - now;
                 SkString str;
@@ -409,7 +420,8 @@ int main (int argc, char * const argv[]) {
                 log_progress(str);
             }
             if (outDir.size() > 0) {
-                saveFile(bench->getName(), configName, outDir.c_str(), bm);
+                saveFile(bench->getName(), configName, outDir.c_str(),
+                         device->accessBitmap(false));
             }
         }
         log_progress("\n");