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 "SkCanvas.h"
      9 #include "SkSurface.h"
     10 #include "Test.h"
     11 #include "sk_tool_utils.h"
     12 
     13 #if SK_SUPPORT_GPU
     14 #include "GrContext.h"
     15 #endif
     16 
     17 static uint32_t pack_unpremul_rgba(SkColor c) {
     18     uint32_t packed;
     19     uint8_t* byte = reinterpret_cast<uint8_t*>(&packed);
     20     byte[0] = SkColorGetR(c);
     21     byte[1] = SkColorGetG(c);
     22     byte[2] = SkColorGetB(c);
     23     byte[3] = SkColorGetA(c);
     24     return packed;
     25 }
     26 
     27 static uint32_t pack_unpremul_bgra(SkColor c) {
     28     uint32_t packed;
     29     uint8_t* byte = reinterpret_cast<uint8_t*>(&packed);
     30     byte[0] = SkColorGetB(c);
     31     byte[1] = SkColorGetG(c);
     32     byte[2] = SkColorGetR(c);
     33     byte[3] = SkColorGetA(c);
     34     return packed;
     35 }
     36 
     37 typedef uint32_t (*PackUnpremulProc)(SkColor);
     38 
     39 const struct {
     40     SkColorType         fColorType;
     41     PackUnpremulProc    fPackProc;
     42 } gUnpremul[] = {
     43     { kRGBA_8888_SkColorType, pack_unpremul_rgba },
     44     { kBGRA_8888_SkColorType, pack_unpremul_bgra },
     45 };
     46 
     47 static void fill_surface(SkSurface* surf, SkColorType colorType, PackUnpremulProc proc) {
     48     // Don't strictly need a bitmap, but its a handy way to allocate the pixels
     49     SkBitmap bmp;
     50     bmp.allocN32Pixels(256, 256);
     51 
     52     for (int a = 0; a < 256; ++a) {
     53         uint32_t* pixels = bmp.getAddr32(0, a);
     54         for (int r = 0; r < 256; ++r) {
     55             pixels[r] = proc(SkColorSetARGB(a, r, 0, 0));
     56         }
     57     }
     58 
     59     const SkImageInfo info = SkImageInfo::Make(bmp.width(), bmp.height(),
     60                                                colorType, kUnpremul_SkAlphaType);
     61     surf->writePixels({info, bmp.getPixels(), bmp.rowBytes()}, 0, 0);
     62 }
     63 
     64 static void test_premul_alpha_roundtrip(skiatest::Reporter* reporter, SkSurface* surf) {
     65     for (size_t upmaIdx = 0; upmaIdx < SK_ARRAY_COUNT(gUnpremul); ++upmaIdx) {
     66         fill_surface(surf, gUnpremul[upmaIdx].fColorType, gUnpremul[upmaIdx].fPackProc);
     67 
     68         const SkImageInfo info = SkImageInfo::Make(256, 256, gUnpremul[upmaIdx].fColorType,
     69                                                    kUnpremul_SkAlphaType);
     70         SkBitmap readBmp1;
     71         readBmp1.allocPixels(info);
     72         SkBitmap readBmp2;
     73         readBmp2.allocPixels(info);
     74 
     75         readBmp1.eraseColor(0);
     76         readBmp2.eraseColor(0);
     77 
     78         surf->readPixels(readBmp1, 0, 0);
     79         sk_tool_utils::write_pixels(surf, readBmp1, 0, 0, gUnpremul[upmaIdx].fColorType,
     80                                     kUnpremul_SkAlphaType);
     81         surf->readPixels(readBmp2, 0, 0);
     82 
     83         bool success = true;
     84         for (int y = 0; y < 256 && success; ++y) {
     85             const uint32_t* pixels1 = readBmp1.getAddr32(0, y);
     86             const uint32_t* pixels2 = readBmp2.getAddr32(0, y);
     87             for (int x = 0; x < 256 && success; ++x) {
     88                 // We see sporadic failures here. May help to see where it goes wrong.
     89                 if (pixels1[x] != pixels2[x]) {
     90                     SkDebugf("%x != %x, x = %d, y = %d\n", pixels1[x], pixels2[x], x, y);
     91                 }
     92                 REPORTER_ASSERT(reporter, success = pixels1[x] == pixels2[x]);
     93             }
     94         }
     95     }
     96 }
     97 
     98 DEF_TEST(PremulAlphaRoundTrip, reporter) {
     99     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
    100 
    101     sk_sp<SkSurface> surf(SkSurface::MakeRaster(info));
    102 
    103     test_premul_alpha_roundtrip(reporter, surf.get());
    104 }
    105 #if SK_SUPPORT_GPU
    106 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(PremulAlphaRoundTrip_Gpu, reporter, ctxInfo) {
    107     const SkImageInfo info = SkImageInfo::MakeN32Premul(256, 256);
    108 
    109     sk_sp<SkSurface> surf(SkSurface::MakeRenderTarget(ctxInfo.grContext(),
    110                                                       SkBudgeted::kNo,
    111                                                       info));
    112     test_premul_alpha_roundtrip(reporter, surf.get());
    113 }
    114 #endif
    115