Override 32BPP format in SkCanvas::readPixels

Review URL: http://codereview.appspot.com/5330073/



git-svn-id: http://skia.googlecode.com/svn/trunk@2600 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/tests/ReadPixelsTest.cpp b/tests/ReadPixelsTest.cpp
index c39e03b..e2b21c4 100644
--- a/tests/ReadPixelsTest.cpp
+++ b/tests/ReadPixelsTest.cpp
@@ -21,18 +21,81 @@
 SkPMColor getCanvasColor(int x, int y) {
     SkASSERT(x >= 0 && x < DEV_W);
     SkASSERT(y >= 0 && y < DEV_H);
-    return SkPackARGB32(0xff, x, y, 0x0);
+
+    U8CPU r = x;
+    U8CPU g = y;
+    U8CPU b = 0xc;
+
+    U8CPU a = 0xff;
+    switch (x % 5) {
+        case 0:
+            a = 0xff;
+            break;
+        case 1:
+            a = 0x80;
+            break;
+        case 2:
+            a = 0xCC;
+            break;
+        case 4:
+            a = 0x01;
+            break;
+        case 3:
+            a = 0x00;
+            break;
+    }
+    return SkPremultiplyARGBInline(a, r, g, b);
 }
     
 SkPMColor getBitmapColor(int x, int y, int w, int h) {
     int n = y * w + x;
-    
+
     U8CPU b = n & 0xff;
     U8CPU g = (n >> 8) & 0xff;
     U8CPU r = (n >> 16) & 0xff;
     return SkPackARGB32(0xff, r, g , b);
 }
 
+SkPMColor convertConfig8888ToPMColor(SkCanvas::Config8888 config8888,
+                                     uint32_t color) {
+    const uint8_t* c = reinterpret_cast<uint8_t*>(&color);
+    U8CPU a,r,g,b;
+    bool mul = false;
+    switch (config8888) {
+        case SkCanvas::kNative_Premul_Config8888:
+            return color;
+        case SkCanvas::kNative_Unpremul_Config8888:
+            mul = true;
+            a = SkGetPackedA32(color);
+            r = SkGetPackedR32(color);
+            g = SkGetPackedG32(color);
+            b = SkGetPackedB32(color);
+            break;
+        case SkCanvas::kBGRA_Unpremul_Config8888:
+            mul = true; // fallthru
+        case SkCanvas::kBGRA_Premul_Config8888:
+            a = static_cast<U8CPU>(c[3]);
+            r = static_cast<U8CPU>(c[2]);
+            g = static_cast<U8CPU>(c[1]);
+            b = static_cast<U8CPU>(c[0]);
+            break;
+        case SkCanvas::kRGBA_Unpremul_Config8888:
+            mul = true; // fallthru
+        case SkCanvas::kRGBA_Premul_Config8888:
+            a = static_cast<U8CPU>(c[3]);
+            r = static_cast<U8CPU>(c[0]);
+            g = static_cast<U8CPU>(c[1]);
+            b = static_cast<U8CPU>(c[2]);
+            break;
+    }
+    if (mul) {
+        r = SkMulDiv255Ceiling(r, a);
+        g = SkMulDiv255Ceiling(g, a);
+        b = SkMulDiv255Ceiling(b, a);
+    }
+    return SkPackARGB32(a, r, g, b);
+}
+
 void fillCanvas(SkCanvas* canvas) {
     static SkBitmap bmp;
     if (bmp.isNull()) {
@@ -77,9 +140,12 @@
 bool checkRead(skiatest::Reporter* reporter,
                const SkBitmap& bitmap,
                int x, int y,
-               bool preFilledBmp) {
+               bool checkCanvasPixels,
+               bool checkBitmapPixels,
+               SkCanvas::Config8888 config8888) {
     SkASSERT(SkBitmap::kARGB_8888_Config == bitmap.config());
     SkASSERT(!bitmap.isNull());
+    SkASSERT(checkCanvasPixels || checkBitmapPixels);
     
     int bw = bitmap.width();
     int bh = bitmap.height();
@@ -100,11 +166,15 @@
             SkPMColor pixel = *reinterpret_cast<SkPMColor*>(pixels + by * bitmap.rowBytes() + bx * bitmap.bytesPerPixel());
 
             if (clippedSrcRect.contains(devx, devy)) {
-                REPORTER_ASSERT(reporter, getCanvasColor(devx, devy) == pixel);
-                if (getCanvasColor(devx, devy) != pixel) {
-                    return false;
+                if (checkCanvasPixels) {
+                    SkPMColor canvasPixel = getCanvasColor(devx, devy);
+                    pixel = convertConfig8888ToPMColor(config8888, pixel);
+                    REPORTER_ASSERT(reporter, canvasPixel == pixel);
+                    if (getCanvasColor(devx, devy) != pixel) {
+                        return false;
+                    }
                 }
-            } else if (preFilledBmp) {
+            } else if (checkBitmapPixels) {
                 REPORTER_ASSERT(reporter, getBitmapColor(bx, by, bw, bh) == pixel);
                 if (getBitmapColor(bx, by, bw, bh) != pixel) {
                     return false;
@@ -207,51 +277,79 @@
     for (int dtype = 0; dtype < 2; ++dtype) {
 
         if (0 == dtype) {
-            canvas.setDevice(new SkDevice(SkBitmap::kARGB_8888_Config, DEV_W, DEV_H, false))->unref();
+            canvas.setDevice(new SkDevice(SkBitmap::kARGB_8888_Config,
+                                          DEV_W,
+                                          DEV_H,
+                                          false))->unref();
         } else {
 #if SK_SCALAR_IS_FIXED
             // GPU device known not to work in the fixed pt build.
             continue;
 #endif
-            canvas.setDevice(new SkGpuDevice(context, SkBitmap::kARGB_8888_Config, DEV_W, DEV_H))->unref();
+            canvas.setDevice(new SkGpuDevice(context,
+                                             SkBitmap::kARGB_8888_Config,
+                                             DEV_W,
+                                             DEV_H))->unref();
         }
         fillCanvas(&canvas);
 
+        static const SkCanvas::Config8888 gReadConfigs[] = {
+            SkCanvas::kNative_Premul_Config8888,
+            SkCanvas::kNative_Unpremul_Config8888,
+            SkCanvas::kBGRA_Premul_Config8888,
+            SkCanvas::kBGRA_Unpremul_Config8888,
+            SkCanvas::kRGBA_Premul_Config8888,
+            SkCanvas::kRGBA_Unpremul_Config8888,
+        };
         for (int rect = 0; rect < SK_ARRAY_COUNT(testRects); ++rect) {
-            SkBitmap bmp;
-            for (BitmapInit bmi = kFirstBitmapInit; bmi < kBitmapInitCnt; bmi = nextBMI(bmi)) {
+            const SkIRect& srcRect = testRects[rect];
+            for (BitmapInit bmi = kFirstBitmapInit;
+                 bmi < kBitmapInitCnt;
+                 bmi = nextBMI(bmi)) {
+                for (int c = 0; c < SK_ARRAY_COUNT(gReadConfigs); ++c) {
+                    SkCanvas::Config8888 config8888 = gReadConfigs[c];
+                    SkBitmap bmp;
+                    init_bitmap(&bmp, srcRect, bmi);
 
-                const SkIRect& srcRect = testRects[rect];
+                    // if the bitmap has pixels allocated before the readPixels,
+                    // note that and fill them with pattern
+                    bool startsWithPixels = !bmp.isNull();
+                    if (startsWithPixels) {
+                        fillBitmap(&bmp);
+                    }
 
-                init_bitmap(&bmp, srcRect, bmi);
+                    bool success =
+                        canvas.readPixels(&bmp, srcRect.fLeft,
+                                          srcRect.fTop, config8888);
 
-                // if the bitmap has pixels allocated before the readPixels, note
-                // that and fill them with pattern
-                bool startsWithPixels = !bmp.isNull();
-                if (startsWithPixels) {
-                    fillBitmap(&bmp);
+                    // non-native not implemented on GPU yet
+                    bool expectSuccess =
+                        SkIRect::Intersects(srcRect, DEV_RECT) &&
+                        !(1 == dtype &&
+                          config8888 != SkCanvas::kNative_Premul_Config8888);
+
+                    // determine whether we expected the read to succeed.
+                    REPORTER_ASSERT(reporter, success == expectSuccess);
+
+                    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());
+                    }
                 }
-                
-                bool success = canvas.readPixels(&bmp, srcRect.fLeft, srcRect.fTop);
-                
-                // determine whether we expected the read to succeed.
-                REPORTER_ASSERT(reporter, success == SkIRect::Intersects(srcRect, DEV_RECT));
-
-                if (success || startsWithPixels) {
-                    checkRead(reporter, bmp, srcRect.fLeft, srcRect.fTop, startsWithPixels);
-                } else {
-                    // if we had no pixels beforehand and the readPixels failed then
-                    // our bitmap should still not have any pixels
-                    REPORTER_ASSERT(reporter, bmp.isNull());
-                }
-
-                // check the old webkit version of readPixels that clips the bitmap size
+                // check the old webkit version of readPixels that clips the
+                // bitmap size
                 SkBitmap wkbmp;
-                success = canvas.readPixels(srcRect, &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, false);
+                    checkRead(reporter, wkbmp, clippedRect.fLeft,
+                              clippedRect.fTop, true, false,
+                              SkCanvas::kNative_Premul_Config8888);
                 } else {
                     REPORTER_ASSERT(reporter, !success);
                 }