bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 8 | #include "SkBitmapDevice.h" |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
| 10 | #include "SkConfig8888.h" |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 11 | #include "Test.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 12 | |
| 13 | #if SK_SUPPORT_GPU |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 14 | #include "GrContextFactory.h" |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 15 | #include "SkGpuDevice.h" |
bsalomon@google.com | cf8fb1f | 2012-08-02 14:03:32 +0000 | [diff] [blame] | 16 | #endif |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 17 | |
commit-bot@chromium.org | ddf94cf | 2013-10-12 17:25:17 +0000 | [diff] [blame] | 18 | static void fillCanvas(SkCanvas* canvas, SkCanvas::Config8888 unpremulConfig) { |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 19 | SkBitmap bmp; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame^] | 20 | bmp.allocN32Pixels(256, 256); |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 21 | SkAutoLockPixels alp(bmp); |
| 22 | uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels()); |
| 23 | |
| 24 | for (int a = 0; a < 256; ++a) { |
| 25 | for (int r = 0; r < 256; ++r) { |
| 26 | pixels[a * 256 + r] = SkPackConfig8888(unpremulConfig, a, r, 0, 0); |
| 27 | } |
| 28 | } |
| 29 | canvas->writePixels(bmp, 0, 0, unpremulConfig); |
| 30 | } |
| 31 | |
| 32 | static const SkCanvas::Config8888 gUnpremulConfigs[] = { |
| 33 | SkCanvas::kNative_Unpremul_Config8888, |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 34 | SkCanvas::kBGRA_Unpremul_Config8888, |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 35 | SkCanvas::kRGBA_Unpremul_Config8888, |
| 36 | }; |
| 37 | |
tfarina@chromium.org | 4ee16bf | 2014-01-10 22:08:27 +0000 | [diff] [blame] | 38 | DEF_GPUTEST(PremulAlphaRoundTrip, reporter, factory) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 39 | SkAutoTUnref<SkBaseDevice> device; |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 40 | for (int dtype = 0; dtype < 2; ++dtype) { |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 41 | |
| 42 | int glCtxTypeCnt = 1; |
| 43 | #if SK_SUPPORT_GPU |
| 44 | if (0 != dtype) { |
| 45 | glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt; |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 46 | } |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 47 | #endif |
| 48 | for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) { |
| 49 | if (0 == dtype) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 50 | device.reset(new SkBitmapDevice(SkBitmap::kARGB_8888_Config, |
| 51 | 256, |
| 52 | 256, |
| 53 | false)); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 54 | } else { |
| 55 | #if SK_SUPPORT_GPU |
| 56 | GrContextFactory::GLContextType type = |
| 57 | static_cast<GrContextFactory::GLContextType>(glCtxType); |
| 58 | if (!GrContextFactory::IsRenderingGLContext(type)) { |
| 59 | continue; |
| 60 | } |
| 61 | GrContext* context = factory->get(type); |
| 62 | if (NULL == context) { |
| 63 | continue; |
| 64 | } |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 65 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 66 | device.reset(new SkGpuDevice(context, SkBitmap::kARGB_8888_Config, 256, 256)); |
| 67 | #else |
| 68 | continue; |
| 69 | #endif |
| 70 | } |
| 71 | SkCanvas canvas(device); |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 72 | |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 73 | SkBitmap readBmp1; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame^] | 74 | readBmp1.allocN32Pixels(256, 256); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 75 | SkBitmap readBmp2; |
mike@reedtribe.org | deee496 | 2014-02-13 14:41:43 +0000 | [diff] [blame^] | 76 | readBmp2.allocN32Pixels(256, 256); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 77 | |
| 78 | for (size_t upmaIdx = 0; |
| 79 | upmaIdx < SK_ARRAY_COUNT(gUnpremulConfigs); |
| 80 | ++upmaIdx) { |
| 81 | fillCanvas(&canvas, gUnpremulConfigs[upmaIdx]); |
| 82 | { |
| 83 | SkAutoLockPixels alp1(readBmp1); |
| 84 | SkAutoLockPixels alp2(readBmp2); |
| 85 | sk_bzero(readBmp1.getPixels(), readBmp1.getSafeSize()); |
| 86 | sk_bzero(readBmp2.getPixels(), readBmp2.getSafeSize()); |
| 87 | } |
| 88 | |
| 89 | canvas.readPixels(&readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]); |
| 90 | canvas.writePixels(readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]); |
| 91 | canvas.readPixels(&readBmp2, 0, 0, gUnpremulConfigs[upmaIdx]); |
| 92 | |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 93 | SkAutoLockPixels alp1(readBmp1); |
| 94 | SkAutoLockPixels alp2(readBmp2); |
bsalomon@google.com | 67b915d | 2013-02-04 16:13:32 +0000 | [diff] [blame] | 95 | uint32_t* pixels1 = |
| 96 | reinterpret_cast<uint32_t*>(readBmp1.getPixels()); |
| 97 | uint32_t* pixels2 = |
| 98 | reinterpret_cast<uint32_t*>(readBmp2.getPixels()); |
| 99 | bool success = true; |
| 100 | for (int y = 0; y < 256 && success; ++y) { |
| 101 | for (int x = 0; x < 256 && success; ++x) { |
| 102 | int i = y * 256 + x; |
| 103 | REPORTER_ASSERT(reporter, success = pixels1[i] == pixels2[i]); |
| 104 | } |
bsalomon@google.com | a91e923 | 2012-02-23 15:39:54 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | } |