Home | History | Annotate | Download | only in tests
      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 
      8 #include "Test.h"
      9 #include "SkCanvas.h"
     10 #include "SkConfig8888.h"
     11 #include "SkBitmapDevice.h"
     12 
     13 #if SK_SUPPORT_GPU
     14 #include "GrContextFactory.h"
     15 #include "SkGpuDevice.h"
     16 #endif
     17 
     18 static void fillCanvas(SkCanvas* canvas, SkCanvas::Config8888 unpremulConfig) {
     19     SkBitmap bmp;
     20     bmp.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
     21     bmp.allocPixels();
     22     SkAutoLockPixels alp(bmp);
     23     uint32_t* pixels = reinterpret_cast<uint32_t*>(bmp.getPixels());
     24 
     25     for (int a = 0; a < 256; ++a) {
     26         for (int r = 0; r < 256; ++r) {
     27             pixels[a * 256 + r] = SkPackConfig8888(unpremulConfig, a, r, 0, 0);
     28         }
     29     }
     30     canvas->writePixels(bmp, 0, 0, unpremulConfig);
     31 }
     32 
     33 static const SkCanvas::Config8888 gUnpremulConfigs[] = {
     34     SkCanvas::kNative_Unpremul_Config8888,
     35     SkCanvas::kBGRA_Unpremul_Config8888,
     36     SkCanvas::kRGBA_Unpremul_Config8888,
     37 };
     38 
     39 static void PremulAlphaRoundTripTest(skiatest::Reporter* reporter, GrContextFactory* factory) {
     40     SkAutoTUnref<SkBaseDevice> device;
     41     for (int dtype = 0; dtype < 2; ++dtype) {
     42 
     43         int glCtxTypeCnt = 1;
     44 #if SK_SUPPORT_GPU
     45         if (0 != dtype)  {
     46             glCtxTypeCnt = GrContextFactory::kGLContextTypeCnt;
     47         }
     48 #endif
     49         for (int glCtxType = 0; glCtxType < glCtxTypeCnt; ++glCtxType) {
     50             if (0 == dtype) {
     51                 device.reset(new SkBitmapDevice(SkBitmap::kARGB_8888_Config,
     52                                                 256,
     53                                                 256,
     54                                                 false));
     55             } else {
     56 #if SK_SUPPORT_GPU
     57                 GrContextFactory::GLContextType type =
     58                     static_cast<GrContextFactory::GLContextType>(glCtxType);
     59                 if (!GrContextFactory::IsRenderingGLContext(type)) {
     60                     continue;
     61                 }
     62                 GrContext* context = factory->get(type);
     63                 if (NULL == context) {
     64                     continue;
     65                 }
     66 
     67                 device.reset(new SkGpuDevice(context, SkBitmap::kARGB_8888_Config, 256, 256));
     68 #else
     69                 continue;
     70 #endif
     71             }
     72             SkCanvas canvas(device);
     73 
     74             SkBitmap readBmp1;
     75             readBmp1.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
     76             readBmp1.allocPixels();
     77             SkBitmap readBmp2;
     78             readBmp2.setConfig(SkBitmap::kARGB_8888_Config, 256, 256);
     79             readBmp2.allocPixels();
     80 
     81             for (size_t upmaIdx = 0;
     82                  upmaIdx < SK_ARRAY_COUNT(gUnpremulConfigs);
     83                  ++upmaIdx) {
     84                 fillCanvas(&canvas, gUnpremulConfigs[upmaIdx]);
     85                 {
     86                     SkAutoLockPixels alp1(readBmp1);
     87                     SkAutoLockPixels alp2(readBmp2);
     88                     sk_bzero(readBmp1.getPixels(), readBmp1.getSafeSize());
     89                     sk_bzero(readBmp2.getPixels(), readBmp2.getSafeSize());
     90                 }
     91 
     92                 canvas.readPixels(&readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]);
     93                 canvas.writePixels(readBmp1, 0, 0, gUnpremulConfigs[upmaIdx]);
     94                 canvas.readPixels(&readBmp2, 0, 0, gUnpremulConfigs[upmaIdx]);
     95 
     96                 SkAutoLockPixels alp1(readBmp1);
     97                 SkAutoLockPixels alp2(readBmp2);
     98                 uint32_t* pixels1 =
     99                     reinterpret_cast<uint32_t*>(readBmp1.getPixels());
    100                 uint32_t* pixels2 =
    101                     reinterpret_cast<uint32_t*>(readBmp2.getPixels());
    102                 bool success = true;
    103                 for (int y = 0; y < 256 && success; ++y) {
    104                     for (int x = 0; x < 256 && success; ++x) {
    105                         int i = y * 256 + x;
    106                         REPORTER_ASSERT(reporter, success = pixels1[i] == pixels2[i]);
    107                     }
    108                 }
    109             }
    110         }
    111     }
    112 }
    113 
    114 #include "TestClassDef.h"
    115 DEFINE_GPUTESTCLASS("PremulAlphaRoundTripTest", PremulAlphaRoundTripTestClass, PremulAlphaRoundTripTest)
    116