Make GPU unit tests use GrContexts of different GL types.
Review URL: https://codereview.appspot.com/7281046

git-svn-id: http://skia.googlecode.com/svn/trunk@7540 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/tests/ReadPixelsTest.cpp b/tests/ReadPixelsTest.cpp
index a33e819..41ecccf 100644
--- a/tests/ReadPixelsTest.cpp
+++ b/tests/ReadPixelsTest.cpp
@@ -14,6 +14,7 @@
 #include "SkRegion.h"
 #if SK_SUPPORT_GPU
 #include "SkGpuDevice.h"
+#include "GrContextFactory.h"
 #endif
 
 
@@ -254,7 +255,7 @@
     }
 }
 
-void ReadPixelsTest(skiatest::Reporter* reporter, GrContext* context) {
+void ReadPixelsTest(skiatest::Reporter* reporter, GrContextFactory* factory) {
     const SkIRect testRects[] = {
         // entire thing
         DEV_RECT,
@@ -303,92 +304,102 @@
     };
 
     for (int dtype = 0; dtype < 2; ++dtype) {
-        SkAutoTUnref<SkDevice> device;
-        if (0 == dtype) {
-            device.reset(new SkDevice(SkBitmap::kARGB_8888_Config,
-                                          DEV_W,
-                                          DEV_H,
-                                          false));
-        } else {
-// GPU device known not to work in the fixed pt build.
-#if defined(SK_SCALAR_IS_FIXED) || !SK_SUPPORT_GPU
-            continue;
-#else
-            device.reset(new SkGpuDevice(context,
-                                             SkBitmap::kARGB_8888_Config,
-                                             DEV_W,
-                                             DEV_H));
-#endif
+        int glCtxTypeCnt = 1;
+#if SK_SUPPORT_GPU
+        if (0 != dtype)  {
+            glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
         }
-        SkCanvas canvas(device);
-        fillCanvas(&canvas);
-
-        static const SkCanvas::Config8888 gReadConfigs[] = {
-            SkCanvas::kNative_Premul_Config8888,
-            SkCanvas::kNative_Unpremul_Config8888,
-/**
- * There is a bug in Ganesh (http://code.google.com/p/skia/issues/detail?id=438)
- * that causes the readback of pixels from BGRA canvas to an RGBA bitmap to
- * fail. This should be removed as soon as the issue above is resolved.
- */
-#if !defined(SK_BUILD_FOR_ANDROID)
-            SkCanvas::kBGRA_Premul_Config8888,
-            SkCanvas::kBGRA_Unpremul_Config8888,
 #endif
-            SkCanvas::kRGBA_Premul_Config8888,
-            SkCanvas::kRGBA_Unpremul_Config8888,
-        };
-        for (size_t rect = 0; rect < SK_ARRAY_COUNT(testRects); ++rect) {
-            const SkIRect& srcRect = testRects[rect];
-            for (BitmapInit bmi = kFirstBitmapInit;
-                 bmi < kBitmapInitCnt;
-                 bmi = nextBMI(bmi)) {
-                for (size_t c = 0; c < SK_ARRAY_COUNT(gReadConfigs); ++c) {
-                    SkCanvas::Config8888 config8888 = gReadConfigs[c];
-                    SkBitmap bmp;
-                    init_bitmap(&bmp, srcRect, bmi);
-
-                    // if the bitmap has pixels allocated before the readPixels,
-                    // note that and fill them with pattern
-                    bool startsWithPixels = !bmp.isNull();
-                    if (startsWithPixels) {
-                        fillBitmap(&bmp);
-                    }
-                    uint32_t idBefore = canvas.getDevice()->accessBitmap(false).getGenerationID();
-                    bool success =
-                        canvas.readPixels(&bmp, srcRect.fLeft,
-                                          srcRect.fTop, config8888);
-                    uint32_t idAfter = canvas.getDevice()->accessBitmap(false).getGenerationID();
-
-                    // we expect to succeed when the read isn't fully clipped
-                    // out.
-                    bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT);
-                    // determine whether we expected the read to succeed.
-                    REPORTER_ASSERT(reporter, success == expectSuccess);
-                    // read pixels should never change the gen id
-                    REPORTER_ASSERT(reporter, idBefore == idAfter);
-
-                    if (success || startsWithPixels) {
-                        checkRead(reporter, bmp, srcRect.fLeft, srcRect.fTop,
-                                  success, startsWithPixels, config8888);
-                    } else {
-                        // if we had no pixels beforehand and the readPixels
-                        // failed then our bitmap should still not have pixels
-                        REPORTER_ASSERT(reporter, bmp.isNull());
-                    }
+        for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
+            SkAutoTUnref<SkDevice> device;
+            if (0 == dtype) {
+                device.reset(new SkDevice(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H, false));
+            } else {
+#if SK_SUPPORT_GPU
+                GrContextFactory::GLContextType type =
+                    static_cast<GrContextFactory::GLContextType>(glCtxType);
+                if (!GrContextFactory::IsRenderingGLContext(type)) {
+                    continue;
                 }
-                // check the old webkit version of readPixels that clips the
-                // bitmap size
-                SkBitmap wkbmp;
-                bool success = canvas.readPixels(srcRect, &wkbmp);
-                SkIRect clippedRect = DEV_RECT;
-                if (clippedRect.intersect(srcRect)) {
-                    REPORTER_ASSERT(reporter, success);
-                    checkRead(reporter, wkbmp, clippedRect.fLeft,
-                              clippedRect.fTop, true, false,
-                              SkCanvas::kNative_Premul_Config8888);
-                } else {
-                    REPORTER_ASSERT(reporter, !success);
+                GrContext* context = factory->get(type);
+                if (NULL == context) {
+                    continue;
+                }
+                device.reset(new SkGpuDevice(context, SkBitmap::kARGB_8888_Config, DEV_W, DEV_H));
+#else
+                continue;
+#endif
+            }
+            SkCanvas canvas(device);
+            fillCanvas(&canvas);
+
+            static const SkCanvas::Config8888 gReadConfigs[] = {
+                SkCanvas::kNative_Premul_Config8888,
+                SkCanvas::kNative_Unpremul_Config8888,
+/**
+    * There is a bug in Ganesh (http://code.google.com/p/skia/issues/detail?id=438)
+    * that causes the readback of pixels from BGRA canvas to an RGBA bitmap to
+    * fail. This should be removed as soon as the issue above is resolved.
+    */
+#if !defined(SK_BUILD_FOR_ANDROID)
+                SkCanvas::kBGRA_Premul_Config8888,
+                SkCanvas::kBGRA_Unpremul_Config8888,
+#endif
+                SkCanvas::kRGBA_Premul_Config8888,
+                SkCanvas::kRGBA_Unpremul_Config8888,
+            };
+            for (size_t rect = 0; rect < SK_ARRAY_COUNT(testRects); ++rect) {
+                const SkIRect& srcRect = testRects[rect];
+                for (BitmapInit bmi = kFirstBitmapInit;
+                     bmi < kBitmapInitCnt;
+                     bmi = nextBMI(bmi)) {
+                    for (size_t c = 0; c < SK_ARRAY_COUNT(gReadConfigs); ++c) {
+                        SkCanvas::Config8888 config8888 = gReadConfigs[c];
+                        SkBitmap bmp;
+                        init_bitmap(&bmp, srcRect, bmi);
+
+                        // if the bitmap has pixels allocated before the readPixels,
+                        // note that and fill them with pattern
+                        bool startsWithPixels = !bmp.isNull();
+                        if (startsWithPixels) {
+                            fillBitmap(&bmp);
+                        }
+                        uint32_t idBefore = canvas.getDevice()->accessBitmap(false).getGenerationID();
+                        bool success =
+                            canvas.readPixels(&bmp, srcRect.fLeft,
+                                              srcRect.fTop, config8888);
+                        uint32_t idAfter = canvas.getDevice()->accessBitmap(false).getGenerationID();
+
+                        // we expect to succeed when the read isn't fully clipped
+                        // out.
+                        bool expectSuccess = SkIRect::Intersects(srcRect, DEV_RECT);
+                        // determine whether we expected the read to succeed.
+                        REPORTER_ASSERT(reporter, success == expectSuccess);
+                        // read pixels should never change the gen id
+                        REPORTER_ASSERT(reporter, idBefore == idAfter);
+
+                        if (success || startsWithPixels) {
+                            checkRead(reporter, bmp, srcRect.fLeft, srcRect.fTop,
+                                      success, startsWithPixels, config8888);
+                        } else {
+                            // if we had no pixels beforehand and the readPixels
+                            // failed then our bitmap should still not have pixels
+                            REPORTER_ASSERT(reporter, bmp.isNull());
+                        }
+                    }
+                    // check the old webkit version of readPixels that clips the
+                    // bitmap size
+                    SkBitmap wkbmp;
+                    bool success = canvas.readPixels(srcRect, &wkbmp);
+                    SkIRect clippedRect = DEV_RECT;
+                    if (clippedRect.intersect(srcRect)) {
+                        REPORTER_ASSERT(reporter, success);
+                        checkRead(reporter, wkbmp, clippedRect.fLeft,
+                                  clippedRect.fTop, true, false,
+                                  SkCanvas::kNative_Premul_Config8888);
+                    } else {
+                        REPORTER_ASSERT(reporter, !success);
+                    }
                 }
             }
         }