bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "Test.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkConfig8888.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 12 | #include "SkDevice.h" |
| 13 | |
| 14 | #if SK_SUPPORT_GPU |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 15 | #include "GrContextFactory.h" |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 16 | #include "SkGpuDevice.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 17 | #endif |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 18 | |
| 19 | |
| 20 | namespace { |
| 21 | |
| 22 | void fillCanvas(SkCanvas* canvas, SkCanvas::Config8888 unpremulConfig) { |
| 23 | SkBitmap bmp; |
| 24 | bmp.setConfig(SkBitmap::kARGB_8888_Config, 256, 256); |
| 25 | bmp.allocPixels(); |
| 26 | SkAutoLockPixels alp(bmp); |
| 27 | uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels()); |
| 28 | |
| 29 | for (int a = 0; a < 256; ++a) { |
| 30 | for (int r = 0; r < 256; ++r) { |
| 31 | pixels[a * 256 + r] = SkPackConfig8888(unpremulConfig, a, r, 0, 0); |
| 32 | } |
| 33 | } |
| 34 | canvas->writePixels(bmp, 0, 0, unpremulConfig); |
| 35 | } |
| 36 | |
| 37 | static const SkCanvas::Config8888 gUnpremulConfigs[] = { |
| 38 | SkCanvas::kNative_Unpremul_Config8888, |
| 39 | /** |
| 40 | * There is a bug in Ganesh (http://code.google.com/p/skia/issues/detail?id=438) |
| 41 | * that causes the readback of pixels from BGRA canvas to an RGBA bitmap to |
| 42 | * fail. This should be removed as soon as the issue above is resolved. |
| 43 | */ |
| 44 | #if !defined(SK_BUILD_FOR_ANDROID) |
| 45 | SkCanvas::kBGRA_Unpremul_Config8888, |
| 46 | #endif |
| 47 | SkCanvas::kRGBA_Unpremul_Config8888, |
| 48 | }; |
| 49 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 50 | void PremulAlphaRoundTripTest(skiatest::Reporter* reporter, GrContextFactory* factory) { |
reed@google.com | 44a42ea | 2012-10-01 17:54:05 +0000 | [diff] [blame] | 51 | SkAutoTUnref<SkDevice> device; |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 52 | for (int dtype = 0; dtype < 2; ++dtype) { |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 53 | |
| 54 | int glCtxTypeCnt = 1; |
| 55 | #if SK_SUPPORT_GPU |
| 56 | if (0 != dtype) { |
| 57 | glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt; |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 58 | } |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 59 | #endif |
| 60 | for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) { |
| 61 | if (0 == dtype) { |
| 62 | device.reset(new SkDevice(SkBitmap::kARGB_8888_Config, |
| 63 | 256, |
| 64 | 256, |
| 65 | false)); |
| 66 | } else { |
| 67 | #if SK_SUPPORT_GPU |
| 68 | GrContextFactory::GLContextType type = |
| 69 | static_cast<GrContextFactory::GLContextType>(glCtxType); |
| 70 | if (!GrContextFactory::IsRenderingGLContext(type)) { |
| 71 | continue; |
| 72 | } |
| 73 | GrContext* context = factory->get(type); |
| 74 | if (NULL == context) { |
| 75 | continue; |
| 76 | } |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 77 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 78 | device.reset(new SkGpuDevice(context, SkBitmap::kARGB_8888_Config, 256, 256)); |
| 79 | #else |
| 80 | continue; |
| 81 | #endif |
| 82 | } |
| 83 | SkCanvas canvas(device); |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 84 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 85 | SkBitmap readBmp1; |
| 86 | readBmp1.setConfig(SkBitmap::kARGB_8888_Config, 256, 256); |
| 87 | readBmp1.allocPixels(); |
| 88 | SkBitmap readBmp2; |
| 89 | readBmp2.setConfig(SkBitmap::kARGB_8888_Config, 256, 256); |
| 90 | readBmp2.allocPixels(); |
| 91 | |
| 92 | for (size_t upmaIdx = 0; |
| 93 | upmaIdx < SK_ARRAY_COUNT(gUnpremulConfigs); |
| 94 | ++upmaIdx) { |
| 95 | fillCanvas(&canvas, gUnpremulConfigs[upmaIdx]); |
| 96 | { |
| 97 | SkAutoLockPixels alp1(readBmp1); |
| 98 | SkAutoLockPixels alp2(readBmp2); |
| 99 | sk_bzero(readBmp1.getPixels(), readBmp1.getSafeSize()); |
| 100 | sk_bzero(readBmp2.getPixels(), readBmp2.getSafeSize()); |
| 101 | } |
| 102 | |
| 103 | canvas.readPixels(&readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]); |
| 104 | canvas.writePixels(readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]); |
| 105 | canvas.readPixels(&readBmp2, 0, 0, gUnpremulConfigs[upmaIdx]); |
| 106 | |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 107 | SkAutoLockPixels alp1(readBmp1); |
| 108 | SkAutoLockPixels alp2(readBmp2); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 109 | uint32_t* pixels1 = |
| 110 | reinterpret_cast<uint32_t*>(readBmp1.getPixels()); |
| 111 | uint32_t* pixels2 = |
| 112 | reinterpret_cast<uint32_t*>(readBmp2.getPixels()); |
| 113 | bool success = true; |
| 114 | for (int y = 0; y < 256 && success; ++y) { |
| 115 | for (int x = 0; x < 256 && success; ++x) { |
| 116 | int i = y * 256 + x; |
| 117 | REPORTER_ASSERT(reporter, success = pixels1[i] == pixels2[i]); |
| 118 | } |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | #include "TestClassDef.h" |
| 127 | DEFINE_GPUTESTCLASS("PremulAlphaRoundTripTest", PremulAlphaRoundTripTestClass, PremulAlphaRoundTripTest) |