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" 12 #include "SkDevice.h" 13 14 #if SK_SUPPORT_GPU 15 #include "GrContextFactory.h" 16 #include "SkGpuDevice.h" 17 #endif 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 SkCanvas::kBGRA_Unpremul_Config8888, 40 SkCanvas::kRGBA_Unpremul_Config8888, 41 }; 42 43 void PremulAlphaRoundTripTest(skiatest::Reporter* reporter, GrContextFactory* factory) { 44 SkAutoTUnref<SkDevice> device; 45 for (int dtype = 0; dtype < 2; ++dtype) { 46 47 int glCtxTypeCnt = 1; 48 #if SK_SUPPORT_GPU 49 if (0 != dtype) { 50 glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt; 51 } 52 #endif 53 for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) { 54 if (0 == dtype) { 55 device.reset(new SkDevice(SkBitmap::kARGB_8888_Config, 56 256, 57 256, 58 false)); 59 } else { 60 #if SK_SUPPORT_GPU 61 GrContextFactory::GLContextType type = 62 static_cast<GrContextFactory::GLContextType>(glCtxType); 63 if (!GrContextFactory::IsRenderingGLContext(type)) { 64 continue; 65 } 66 GrContext* context = factory->get(type); 67 if (NULL == context) { 68 continue; 69 } 70 71 device.reset(new SkGpuDevice(context, SkBitmap::kARGB_8888_Config, 256, 256)); 72 #else 73 continue; 74 #endif 75 } 76 SkCanvas canvas(device); 77 78 SkBitmap readBmp1; 79 readBmp1.setConfig(SkBitmap::kARGB_8888_Config, 256, 256); 80 readBmp1.allocPixels(); 81 SkBitmap readBmp2; 82 readBmp2.setConfig(SkBitmap::kARGB_8888_Config, 256, 256); 83 readBmp2.allocPixels(); 84 85 for (size_t upmaIdx = 0; 86 upmaIdx < SK_ARRAY_COUNT(gUnpremulConfigs); 87 ++upmaIdx) { 88 fillCanvas(&canvas, gUnpremulConfigs[upmaIdx]); 89 { 90 SkAutoLockPixels alp1(readBmp1); 91 SkAutoLockPixels alp2(readBmp2); 92 sk_bzero(readBmp1.getPixels(), readBmp1.getSafeSize()); 93 sk_bzero(readBmp2.getPixels(), readBmp2.getSafeSize()); 94 } 95 96 canvas.readPixels(&readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]); 97 canvas.writePixels(readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]); 98 canvas.readPixels(&readBmp2, 0, 0, gUnpremulConfigs[upmaIdx]); 99 100 SkAutoLockPixels alp1(readBmp1); 101 SkAutoLockPixels alp2(readBmp2); 102 uint32_t* pixels1 = 103 reinterpret_cast<uint32_t*>(readBmp1.getPixels()); 104 uint32_t* pixels2 = 105 reinterpret_cast<uint32_t*>(readBmp2.getPixels()); 106 bool success = true; 107 for (int y = 0; y < 256 && success; ++y) { 108 for (int x = 0; x < 256 && success; ++x) { 109 int i = y * 256 + x; 110 REPORTER_ASSERT(reporter, success = pixels1[i] == pixels2[i]); 111 } 112 } 113 } 114 } 115 } 116 } 117 } 118 119 #include "TestClassDef.h" 120 DEFINE_GPUTESTCLASS("PremulAlphaRoundTripTest", PremulAlphaRoundTripTestClass, PremulAlphaRoundTripTest) 121