| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkBitmapDevice.h" | 8 #include "SkBitmapDevice.h" |
| 9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkConfig8888.h" | 10 #include "SkConfig8888.h" |
| 11 #include "Test.h" | 11 #include "Test.h" |
| 12 #include "sk_tool_utils.h" | 12 #include "sk_tool_utils.h" |
| 13 | 13 |
| 14 #if SK_SUPPORT_GPU | 14 #if SK_SUPPORT_GPU |
| 15 #include "GrContextFactory.h" | 15 #include "GrContext.h" |
| 16 #include "SkGpuDevice.h" | 16 #include "SkGpuDevice.h" |
| 17 #endif | 17 #endif |
| 18 | 18 |
| 19 static uint32_t pack_unpremul_rgba(SkColor c) { | 19 static uint32_t pack_unpremul_rgba(SkColor c) { |
| 20 uint32_t packed; | 20 uint32_t packed; |
| 21 uint8_t* byte = reinterpret_cast<uint8_t*>(&packed); | 21 uint8_t* byte = reinterpret_cast<uint8_t*>(&packed); |
| 22 byte[0] = SkColorGetR(c); | 22 byte[0] = SkColorGetR(c); |
| 23 byte[1] = SkColorGetG(c); | 23 byte[1] = SkColorGetG(c); |
| 24 byte[2] = SkColorGetB(c); | 24 byte[2] = SkColorGetB(c); |
| 25 byte[3] = SkColorGetA(c); | 25 byte[3] = SkColorGetA(c); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 56 for (int r = 0; r < 256; ++r) { | 56 for (int r = 0; r < 256; ++r) { |
| 57 pixels[r] = proc(SkColorSetARGB(a, r, 0, 0)); | 57 pixels[r] = proc(SkColorSetARGB(a, r, 0, 0)); |
| 58 } | 58 } |
| 59 } | 59 } |
| 60 | 60 |
| 61 const SkImageInfo info = SkImageInfo::Make(bmp.width(), bmp.height(), | 61 const SkImageInfo info = SkImageInfo::Make(bmp.width(), bmp.height(), |
| 62 colorType, kUnpremul_SkAlphaType)
; | 62 colorType, kUnpremul_SkAlphaType)
; |
| 63 canvas->writePixels(info, bmp.getPixels(), bmp.rowBytes(), 0, 0); | 63 canvas->writePixels(info, bmp.getPixels(), bmp.rowBytes(), 0, 0); |
| 64 } | 64 } |
| 65 | 65 |
| 66 DEF_GPUTEST(PremulAlphaRoundTrip, reporter, factory) { | 66 static void test_premul_alpha_roundtrip(skiatest::Reporter* reporter, SkBaseDevi
ce* device) { |
| 67 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256); | 67 SkCanvas canvas(device); |
| 68 for (size_t upmaIdx = 0; upmaIdx < SK_ARRAY_COUNT(gUnpremul); ++upmaIdx) { |
| 69 fillCanvas(&canvas, gUnpremul[upmaIdx].fColorType, gUnpremul[upmaIdx].fP
ackProc); |
| 68 | 70 |
| 69 for (int dtype = 0; dtype < 2; ++dtype) { | 71 const SkImageInfo info = SkImageInfo::Make(256, 256, gUnpremul[upmaIdx].
fColorType, |
| 72 kUnpremul_SkAlphaType); |
| 73 SkBitmap readBmp1; |
| 74 readBmp1.allocPixels(info); |
| 75 SkBitmap readBmp2; |
| 76 readBmp2.allocPixels(info); |
| 70 | 77 |
| 71 int glCtxTypeCnt = 1; | 78 readBmp1.eraseColor(0); |
| 72 #if SK_SUPPORT_GPU | 79 readBmp2.eraseColor(0); |
| 73 if (0 != dtype) { | 80 |
| 74 glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt; | 81 canvas.readPixels(&readBmp1, 0, 0); |
| 75 } | 82 sk_tool_utils::write_pixels(&canvas, readBmp1, 0, 0, gUnpremul[upmaIdx].
fColorType, |
| 76 #endif | 83 kUnpremul_SkAlphaType); |
| 77 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); | 84 canvas.readPixels(&readBmp2, 0, 0); |
| 78 for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) { | 85 |
| 79 SkAutoTUnref<SkBaseDevice> device; | 86 bool success = true; |
| 80 if (0 == dtype) { | 87 for (int y = 0; y < 256 && success; ++y) { |
| 81 device.reset(SkBitmapDevice::Create(info, props)); | 88 const uint32_t* pixels1 = readBmp1.getAddr32(0, y); |
| 82 } else { | 89 const uint32_t* pixels2 = readBmp2.getAddr32(0, y); |
| 83 #if SK_SUPPORT_GPU | 90 for (int x = 0; x < 256 && success; ++x) { |
| 84 GrContextFactory::GLContextType type = | 91 // We see sporadic failures here. May help to see where it goes
wrong. |
| 85 static_cast<GrContextFactory::GLContextType>(glCtxType); | 92 if (pixels1[x] != pixels2[x]) { |
| 86 if (!GrContextFactory::IsRenderingGLContext(type)) { | 93 SkDebugf("%x != %x, x = %d, y = %d\n", pixels1[x], pixels2[x
], x, y); |
| 87 continue; | |
| 88 } | 94 } |
| 89 GrContext* ctx = factory->get(type); | 95 REPORTER_ASSERT(reporter, success = pixels1[x] == pixels2[x]); |
| 90 if (nullptr == ctx) { | |
| 91 continue; | |
| 92 } | |
| 93 device.reset(SkGpuDevice::Create(ctx, SkSurface::kNo_Budgeted, i
nfo, 0, &props, | |
| 94 SkGpuDevice::kUninit_InitConten
ts)); | |
| 95 #else | |
| 96 continue; | |
| 97 #endif | |
| 98 } | |
| 99 SkCanvas canvas(device); | |
| 100 | |
| 101 for (size_t upmaIdx = 0; upmaIdx < SK_ARRAY_COUNT(gUnpremul); ++upma
Idx) { | |
| 102 fillCanvas(&canvas, gUnpremul[upmaIdx].fColorType, gUnpremul[upm
aIdx].fPackProc); | |
| 103 | |
| 104 const SkImageInfo info = SkImageInfo::Make(256, 256, gUnpremul[u
pmaIdx].fColorType, | |
| 105 kUnpremul_SkAlphaType
); | |
| 106 SkBitmap readBmp1; | |
| 107 readBmp1.allocPixels(info); | |
| 108 SkBitmap readBmp2; | |
| 109 readBmp2.allocPixels(info); | |
| 110 | |
| 111 readBmp1.eraseColor(0); | |
| 112 readBmp2.eraseColor(0); | |
| 113 | |
| 114 canvas.readPixels(&readBmp1, 0, 0); | |
| 115 sk_tool_utils::write_pixels(&canvas, readBmp1, 0, 0, gUnpremul[u
pmaIdx].fColorType, | |
| 116 kUnpremul_SkAlphaType); | |
| 117 canvas.readPixels(&readBmp2, 0, 0); | |
| 118 | |
| 119 bool success = true; | |
| 120 for (int y = 0; y < 256 && success; ++y) { | |
| 121 const uint32_t* pixels1 = readBmp1.getAddr32(0, y); | |
| 122 const uint32_t* pixels2 = readBmp2.getAddr32(0, y); | |
| 123 for (int x = 0; x < 256 && success; ++x) { | |
| 124 // We see sporadic failures here. May help to see where
it goes wrong. | |
| 125 if (pixels1[x] != pixels2[x]) { | |
| 126 SkDebugf("%x != %x, x = %d, y = %d\n", pixels1[x], p
ixels2[x], x, y); | |
| 127 } | |
| 128 REPORTER_ASSERT(reporter, success = pixels1[x] == pixels
2[x]); | |
| 129 } | |
| 130 } | |
| 131 } | 96 } |
| 132 } | 97 } |
| 133 } | 98 } |
| 134 } | 99 } |
| 100 |
| 101 DEF_TEST(PremulAlphaRoundTrip, reporter) { |
| 102 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256); |
| 103 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); |
| 104 SkAutoTUnref<SkBaseDevice> device(SkBitmapDevice::Create(info, props)); |
| 105 test_premul_alpha_roundtrip(reporter, device); |
| 106 } |
| 107 #if SK_SUPPORT_GPU |
| 108 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PremulAlphaRoundTrip_Gpu, reporter, context)
{ |
| 109 const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256); |
| 110 SkSurfaceProps props(SkSurfaceProps::kLegacyFontHost_InitType); |
| 111 SkAutoTUnref<SkBaseDevice> device( |
| 112 SkGpuDevice::Create(context, SkSurface::kNo_Budgeted, info, 0, &props, |
| 113 SkGpuDevice::kUninit_InitContents)); |
| 114 test_premul_alpha_roundtrip(reporter, device); |
| 115 } |
| 116 #endif |
| 117 |
| OLD | NEW |