Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1938)

Unified Diff: tests/WritePixelsTest.cpp

Issue 199733016: implement readPixels and writePixels natively, w/o using the (deprecated) (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: rebase Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tests/DeferredCanvasTest.cpp ('k') | tools/sk_tool_utils.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/WritePixelsTest.cpp
diff --git a/tests/WritePixelsTest.cpp b/tests/WritePixelsTest.cpp
index fc0fe37d6c4424d29ff963ae43f0182f7a2cae40..a1c978d1b1429d5b942a9f3ccc010b49e9e48e7f 100644
--- a/tests/WritePixelsTest.cpp
+++ b/tests/WritePixelsTest.cpp
@@ -56,41 +56,18 @@ static SkPMColor getCanvasColor(int x, int y) {
return SkPremultiplyARGBInline(a, r, g, b);
}
-static bool config8888IsPremul(SkCanvas::Config8888 config8888) {
- switch (config8888) {
- case SkCanvas::kNative_Premul_Config8888:
- case SkCanvas::kBGRA_Premul_Config8888:
- case SkCanvas::kRGBA_Premul_Config8888:
- return true;
- case SkCanvas::kNative_Unpremul_Config8888:
- case SkCanvas::kBGRA_Unpremul_Config8888:
- case SkCanvas::kRGBA_Unpremul_Config8888:
- return false;
- default:
- SkASSERT(0);
- return false;
- }
-}
-
// assumes any premu/.unpremul has been applied
-static uint32_t packConfig8888(SkCanvas::Config8888 config8888,
- U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
+static uint32_t packColorType(SkColorType ct, U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
uint32_t r32;
uint8_t* result = reinterpret_cast<uint8_t*>(&r32);
- switch (config8888) {
- case SkCanvas::kNative_Premul_Config8888:
- case SkCanvas::kNative_Unpremul_Config8888:
- r32 = SkPackARGB32NoCheck(a, r, g, b);
- break;
- case SkCanvas::kBGRA_Premul_Config8888:
- case SkCanvas::kBGRA_Unpremul_Config8888:
+ switch (ct) {
+ case kBGRA_8888_SkColorType:
result[0] = b;
result[1] = g;
result[2] = r;
result[3] = a;
break;
- case SkCanvas::kRGBA_Premul_Config8888:
- case SkCanvas::kRGBA_Unpremul_Config8888:
+ case kRGBA_8888_SkColorType:
result[0] = r;
result[1] = g;
result[2] = b;
@@ -103,7 +80,7 @@ static uint32_t packConfig8888(SkCanvas::Config8888 config8888,
return r32;
}
-static uint32_t getBitmapColor(int x, int y, int w, SkCanvas::Config8888 config8888) {
+static uint32_t getBitmapColor(int x, int y, int w, SkColorType ct, SkAlphaType at) {
int n = y * w + x;
U8CPU b = n & 0xff;
U8CPU g = (n >> 8) & 0xff;
@@ -126,12 +103,12 @@ static uint32_t getBitmapColor(int x, int y, int w, SkCanvas::Config8888 config8
a = 0x00;
break;
}
- if (config8888IsPremul(config8888)) {
+ if (kPremul_SkAlphaType == at) {
r = SkMulDiv255Ceiling(r, a);
g = SkMulDiv255Ceiling(g, a);
b = SkMulDiv255Ceiling(b, a);
}
- return packConfig8888(config8888, a, r, g , b);
+ return packColorType(ct, a, r, g , b);
}
static void fillCanvas(SkCanvas* canvas) {
@@ -154,48 +131,38 @@ static void fillCanvas(SkCanvas* canvas) {
canvas->restore();
}
-static SkPMColor convertConfig8888ToPMColor(SkCanvas::Config8888 config8888,
- uint32_t color,
- bool* premul) {
- const uint8_t* c = reinterpret_cast<uint8_t*>(&color);
- U8CPU a,r,g,b;
- *premul = false;
- switch (config8888) {
- case SkCanvas::kNative_Premul_Config8888:
- return color;
- case SkCanvas::kNative_Unpremul_Config8888:
- *premul = true;
- a = SkGetPackedA32(color);
- r = SkGetPackedR32(color);
- g = SkGetPackedG32(color);
- b = SkGetPackedB32(color);
- break;
- case SkCanvas::kBGRA_Unpremul_Config8888:
- *premul = 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]);
+/**
+ * Lucky for us, alpha is always in the same spot (SK_A32_SHIFT), for both RGBA and BGRA.
+ * Thus this routine doesn't need to know the exact colortype
+ */
+static uint32_t premul(uint32_t color) {
+ unsigned a = SkGetPackedA32(color);
+ // these next three are not necessarily r,g,b in that order, but they are r,g,b in some order.
+ unsigned c0 = SkGetPackedR32(color);
+ unsigned c1 = SkGetPackedG32(color);
+ unsigned c2 = SkGetPackedB32(color);
+ c0 = SkMulDiv255Ceiling(c0, a);
+ c1 = SkMulDiv255Ceiling(c1, a);
+ c2 = SkMulDiv255Ceiling(c2, a);
+ return SkPackARGB32NoCheck(a, c0, c1, c2);
+}
+
+static SkPMColor convert_to_PMColor(SkColorType ct, SkAlphaType at, uint32_t color) {
+ if (kUnpremul_SkAlphaType == at) {
+ color = premul(color);
+ }
+ switch (ct) {
+ case kRGBA_8888_SkColorType:
+ color = SkSwizzle_RGBA_to_PMColor(color);
break;
- case SkCanvas::kRGBA_Unpremul_Config8888:
- *premul = 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]);
+ case kBGRA_8888_SkColorType:
+ color = SkSwizzle_BGRA_to_PMColor(color);
break;
default:
- SkDEBUGFAIL("Unexpected Config8888");
- return 0;
- }
- if (*premul) {
- r = SkMulDiv255Ceiling(r, a);
- g = SkMulDiv255Ceiling(g, a);
- b = SkMulDiv255Ceiling(b, a);
+ SkASSERT(0);
+ break;
}
- return SkPackARGB32(a, r, g, b);
+ return color;
}
static bool checkPixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
@@ -218,39 +185,54 @@ static bool checkPixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
SkAbs32(aB - bB) <= 1;
}
-static bool checkWrite(skiatest::Reporter* reporter,
- SkCanvas* canvas,
- const SkBitmap& bitmap,
- int writeX, int writeY,
- SkCanvas::Config8888 config8888) {
- SkBaseDevice* dev = canvas->getDevice();
- if (!dev) {
+static bool checkWrite(skiatest::Reporter* reporter, SkCanvas* canvas, const SkBitmap& bitmap,
+ int writeX, int writeY) {
+ SkImageInfo canvasInfo;
+ size_t canvasRowBytes;
+ const uint32_t* canvasPixels;
+
+ // Can't use canvas->peekPixels(), as we are trying to look at GPU pixels sometimes as well.
+ // At some point this will be unsupported, as we won't allow accessBitmap() to magically call
+ // readPixels for the client.
+ SkBitmap secretDevBitmap;
+ {
+ SkBaseDevice* dev = canvas->getDevice();
+ if (!dev) {
+ return false;
+ }
+ secretDevBitmap = dev->accessBitmap(false);
+ }
+ SkAutoLockPixels alp(secretDevBitmap);
+ canvasInfo = secretDevBitmap.info();
+ canvasRowBytes = secretDevBitmap.rowBytes();
+ canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels());
+
+ if (NULL == canvasPixels) {
return false;
}
- SkBitmap devBmp = dev->accessBitmap(false);
- if (devBmp.width() != DEV_W ||
- devBmp.height() != DEV_H ||
- devBmp.config() != SkBitmap::kARGB_8888_Config ||
- devBmp.isNull()) {
+
+ if (canvasInfo.width() != DEV_W ||
+ canvasInfo.height() != DEV_H ||
+ canvasInfo.colorType() != kPMColor_SkColorType) {
return false;
}
- SkAutoLockPixels alp(devBmp);
- intptr_t canvasPixels = reinterpret_cast<intptr_t>(devBmp.getPixels());
- size_t canvasRowBytes = devBmp.rowBytes();
+ const SkImageInfo bmInfo = bitmap.info();
+
SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height());
for (int cy = 0; cy < DEV_H; ++cy) {
- const SkPMColor* canvasRow = reinterpret_cast<const SkPMColor*>(canvasPixels);
for (int cx = 0; cx < DEV_W; ++cx) {
- SkPMColor canvasPixel = canvasRow[cx];
+ SkPMColor canvasPixel = canvasPixels[cx];
if (writeRect.contains(cx, cy)) {
int bx = cx - writeX;
int by = cy - writeY;
- uint32_t bmpColor8888 = getBitmapColor(bx, by, bitmap.width(), config8888);
- bool mul;
- SkPMColor bmpPMColor = convertConfig8888ToPMColor(config8888, bmpColor8888, &mul);
- bool check;
- REPORTER_ASSERT(reporter, check = checkPixel(bmpPMColor, canvasPixel, mul));
+ uint32_t bmpColor8888 = getBitmapColor(bx, by, bitmap.width(),
+ bmInfo.colorType(), bmInfo.alphaType());
+ bool mul = (kUnpremul_SkAlphaType == bmInfo.alphaType());
+ SkPMColor bmpPMColor = convert_to_PMColor(bmInfo.colorType(), bmInfo.alphaType(),
+ bmpColor8888);
+ bool check = checkPixel(bmpPMColor, canvasPixel, mul);
+ REPORTER_ASSERT(reporter, check);
if (!check) {
return false;
}
@@ -264,7 +246,7 @@ static bool checkWrite(skiatest::Reporter* reporter,
}
}
if (cy != DEV_H -1) {
- const char* pad = reinterpret_cast<const char*>(canvasPixels + 4 * DEV_W);
+ const char* pad = reinterpret_cast<const char*>(canvasPixels + DEV_W);
for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) {
bool check;
REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD)));
@@ -273,7 +255,7 @@ static bool checkWrite(skiatest::Reporter* reporter,
}
}
}
- canvasPixels += canvasRowBytes;
+ canvasPixels += canvasRowBytes/4;
}
return true;
@@ -354,21 +336,16 @@ static SkBaseDevice* createDevice(const CanvasConfig& c, GrContext* grCtx) {
return NULL;
}
-static bool setupBitmap(SkBitmap* bitmap,
- SkCanvas::Config8888 config8888,
- int w, int h,
- bool tightRowBytes) {
- size_t rowBytes = tightRowBytes ? 0 : 4 * w + 60;
- SkImageInfo info = SkImageInfo::MakeN32Premul(w, h);
- if (!allocRowBytes(bitmap, info, rowBytes)) {
+static bool setupBitmap(SkBitmap* bm, SkColorType ct, SkAlphaType at, int w, int h, int tightRB) {
+ size_t rowBytes = tightRB ? 0 : 4 * w + 60;
+ SkImageInfo info = SkImageInfo::Make(w, h, ct, at);
+ if (!allocRowBytes(bm, info, rowBytes)) {
return false;
}
- SkAutoLockPixels alp(*bitmap);
- intptr_t pixels = reinterpret_cast<intptr_t>(bitmap->getPixels());
+ SkAutoLockPixels alp(*bm);
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
- uint32_t* pixel = reinterpret_cast<uint32_t*>(pixels + y * bitmap->rowBytes() + x * 4);
- *pixel = getBitmapColor(x, y, w, config8888);
+ *bm->getAddr32(x, y) = getBitmapColor(x, y, w, ct, at);
}
}
return true;
@@ -452,31 +429,33 @@ DEF_GPUTEST(WritePixels, reporter, factory) {
SkAutoTUnref<SkBaseDevice> device(createDevice(gCanvasConfigs[i], context));
SkCanvas canvas(device);
- static const SkCanvas::Config8888 gSrcConfigs[] = {
- SkCanvas::kNative_Premul_Config8888,
- SkCanvas::kNative_Unpremul_Config8888,
- SkCanvas::kBGRA_Premul_Config8888,
- SkCanvas::kBGRA_Unpremul_Config8888,
- SkCanvas::kRGBA_Premul_Config8888,
- SkCanvas::kRGBA_Unpremul_Config8888,
+ static const struct {
+ SkColorType fColorType;
+ SkAlphaType fAlphaType;
+ } gSrcConfigs[] = {
+ { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
+ { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
+ { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
+ { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
};
for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) {
const SkIRect& rect = testRects[r];
for (int tightBmp = 0; tightBmp < 2; ++tightBmp) {
for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) {
+ const SkColorType ct = gSrcConfigs[c].fColorType;
+ const SkAlphaType at = gSrcConfigs[c].fAlphaType;
+
fillCanvas(&canvas);
- SkCanvas::Config8888 config8888 = gSrcConfigs[c];
SkBitmap bmp;
- REPORTER_ASSERT(reporter, setupBitmap(&bmp, config8888, rect.width(), rect.height(), SkToBool(tightBmp)));
+ REPORTER_ASSERT(reporter, setupBitmap(&bmp, ct, at, rect.width(),
+ rect.height(), SkToBool(tightBmp)));
uint32_t idBefore = canvas.getDevice()->accessBitmap(false).getGenerationID();
- SkColorType ct;
- SkAlphaType at;
- sk_tool_utils::config8888_to_imagetypes(config8888, &ct, &at);
- sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at);
+ // sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at);
+ canvas.writePixels(bmp, rect.fLeft, rect.fTop);
uint32_t idAfter = canvas.getDevice()->accessBitmap(false).getGenerationID();
- REPORTER_ASSERT(reporter, checkWrite(reporter, &canvas, bmp, rect.fLeft, rect.fTop, config8888));
+ REPORTER_ASSERT(reporter, checkWrite(reporter, &canvas, bmp, rect.fLeft, rect.fTop));
// we should change the genID iff pixels were actually written.
SkIRect canvasRect = SkIRect::MakeSize(canvas.getDeviceSize());
« no previous file with comments | « tests/DeferredCanvasTest.cpp ('k') | tools/sk_tool_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698