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 "SkColorPriv.h"
     10 #include "SkMathPriv.h"
     11 #include "SkSurface.h"
     12 #include "Test.h"
     13 #include "sk_tool_utils.h"
     14 
     15 #if SK_SUPPORT_GPU
     16 #include "GrContext.h"
     17 #include "GrGpu.h"
     18 #endif
     19 
     20 #include <initializer_list>
     21 
     22 static const int DEV_W = 100, DEV_H = 100;
     23 static const SkIRect DEV_RECT = SkIRect::MakeWH(DEV_W, DEV_H);
     24 static const SkRect DEV_RECT_S = SkRect::MakeWH(DEV_W * SK_Scalar1,
     25                                                 DEV_H * SK_Scalar1);
     26 static const U8CPU DEV_PAD = 0xee;
     27 
     28 static SkPMColor get_canvas_color(int x, int y) {
     29     SkASSERT(x >= 0 && x < DEV_W);
     30     SkASSERT(y >= 0 && y < DEV_H);
     31 
     32     U8CPU r = x;
     33     U8CPU g = y;
     34     U8CPU b = 0xc;
     35 
     36     U8CPU a = 0x0;
     37     switch ((x+y) % 5) {
     38         case 0:
     39             a = 0xff;
     40             break;
     41         case 1:
     42             a = 0x80;
     43             break;
     44         case 2:
     45             a = 0xCC;
     46             break;
     47         case 3:
     48             a = 0x00;
     49             break;
     50         case 4:
     51             a = 0x01;
     52             break;
     53     }
     54     return SkPremultiplyARGBInline(a, r, g, b);
     55 }
     56 
     57 // assumes any premu/.unpremul has been applied
     58 static uint32_t pack_color_type(SkColorType ct, U8CPU a, U8CPU r, U8CPU g, U8CPU b) {
     59     uint32_t r32;
     60     uint8_t* result = reinterpret_cast<uint8_t*>(&r32);
     61     switch (ct) {
     62         case kBGRA_8888_SkColorType:
     63             result[0] = b;
     64             result[1] = g;
     65             result[2] = r;
     66             result[3] = a;
     67             break;
     68         case kRGBA_8888_SkColorType:
     69             result[0] = r;
     70             result[1] = g;
     71             result[2] = b;
     72             result[3] = a;
     73             break;
     74         default:
     75             SkASSERT(0);
     76             return 0;
     77     }
     78     return r32;
     79 }
     80 
     81 static uint32_t get_bitmap_color(int x, int y, int w, SkColorType ct, SkAlphaType at) {
     82     int n = y * w + x;
     83     U8CPU b = n & 0xff;
     84     U8CPU g = (n >> 8) & 0xff;
     85     U8CPU r = (n >> 16) & 0xff;
     86     U8CPU a = 0;
     87     switch ((x+y) % 5) {
     88         case 4:
     89             a = 0xff;
     90             break;
     91         case 3:
     92             a = 0x80;
     93             break;
     94         case 2:
     95             a = 0xCC;
     96             break;
     97         case 1:
     98             a = 0x01;
     99             break;
    100         case 0:
    101             a = 0x00;
    102             break;
    103     }
    104     if (kPremul_SkAlphaType == at) {
    105         r = SkMulDiv255Ceiling(r, a);
    106         g = SkMulDiv255Ceiling(g, a);
    107         b = SkMulDiv255Ceiling(b, a);
    108     }
    109     return pack_color_type(ct, a, r, g , b);
    110 }
    111 
    112 static void fill_canvas(SkCanvas* canvas) {
    113     SkBitmap bmp;
    114     if (bmp.isNull()) {
    115         bmp.allocN32Pixels(DEV_W, DEV_H);
    116         for (int y = 0; y < DEV_H; ++y) {
    117             for (int x = 0; x < DEV_W; ++x) {
    118                 *bmp.getAddr32(x, y) = get_canvas_color(x, y);
    119             }
    120         }
    121     }
    122     canvas->save();
    123     canvas->setMatrix(SkMatrix::I());
    124     canvas->clipRect(DEV_RECT_S, kReplace_SkClipOp);
    125     SkPaint paint;
    126     paint.setBlendMode(SkBlendMode::kSrc);
    127     canvas->drawBitmap(bmp, 0, 0, &paint);
    128     canvas->restore();
    129 }
    130 
    131 /**
    132  *  Lucky for us, alpha is always in the same spot (SK_A32_SHIFT), for both RGBA and BGRA.
    133  *  Thus this routine doesn't need to know the exact colortype
    134  */
    135 static uint32_t premul(uint32_t color) {
    136     unsigned a = SkGetPackedA32(color);
    137     // these next three are not necessarily r,g,b in that order, but they are r,g,b in some order.
    138     unsigned c0 = SkGetPackedR32(color);
    139     unsigned c1 = SkGetPackedG32(color);
    140     unsigned c2 = SkGetPackedB32(color);
    141     c0 = SkMulDiv255Ceiling(c0, a);
    142     c1 = SkMulDiv255Ceiling(c1, a);
    143     c2 = SkMulDiv255Ceiling(c2, a);
    144     return SkPackARGB32NoCheck(a, c0, c1, c2);
    145 }
    146 
    147 static SkPMColor convert_to_PMColor(SkColorType ct, SkAlphaType at, uint32_t color) {
    148     if (kUnpremul_SkAlphaType == at) {
    149         color = premul(color);
    150     }
    151     switch (ct) {
    152         case kRGBA_8888_SkColorType:
    153             color = SkSwizzle_RGBA_to_PMColor(color);
    154             break;
    155         case kBGRA_8888_SkColorType:
    156             color = SkSwizzle_BGRA_to_PMColor(color);
    157             break;
    158         default:
    159             SkASSERT(0);
    160             break;
    161     }
    162     return color;
    163 }
    164 
    165 static bool check_pixel(SkPMColor a, SkPMColor b, bool didPremulConversion) {
    166     if (!didPremulConversion) {
    167         return a == b;
    168     }
    169     int32_t aA = static_cast<int32_t>(SkGetPackedA32(a));
    170     int32_t aR = static_cast<int32_t>(SkGetPackedR32(a));
    171     int32_t aG = static_cast<int32_t>(SkGetPackedG32(a));
    172     int32_t aB = SkGetPackedB32(a);
    173 
    174     int32_t bA = static_cast<int32_t>(SkGetPackedA32(b));
    175     int32_t bR = static_cast<int32_t>(SkGetPackedR32(b));
    176     int32_t bG = static_cast<int32_t>(SkGetPackedG32(b));
    177     int32_t bB = static_cast<int32_t>(SkGetPackedB32(b));
    178 
    179     return aA == bA &&
    180            SkAbs32(aR - bR) <= 1 &&
    181            SkAbs32(aG - bG) <= 1 &&
    182            SkAbs32(aB - bB) <= 1;
    183 }
    184 
    185 static bool check_write(skiatest::Reporter* reporter, SkCanvas* canvas, const SkBitmap& bitmap,
    186                        int writeX, int writeY) {
    187     const SkImageInfo canvasInfo = canvas->imageInfo();
    188     size_t canvasRowBytes;
    189     const uint32_t* canvasPixels;
    190 
    191     // Can't use canvas->peekPixels(), as we are trying to look at GPU pixels sometimes as well.
    192     // At some point this will be unsupported, as we won't allow accessBitmap() to magically call
    193     // readPixels for the client.
    194     SkBitmap secretDevBitmap;
    195     secretDevBitmap.allocN32Pixels(canvasInfo.width(), canvasInfo.height());
    196     if (!canvas->readPixels(secretDevBitmap, 0, 0)) {
    197         return false;
    198     }
    199 
    200     canvasRowBytes = secretDevBitmap.rowBytes();
    201     canvasPixels = static_cast<const uint32_t*>(secretDevBitmap.getPixels());
    202 
    203     if (nullptr == canvasPixels) {
    204         return false;
    205     }
    206 
    207     if (canvasInfo.width() != DEV_W ||
    208         canvasInfo.height() != DEV_H ||
    209         canvasInfo.colorType() != kN32_SkColorType) {
    210         return false;
    211     }
    212 
    213     const SkImageInfo bmInfo = bitmap.info();
    214 
    215     SkIRect writeRect = SkIRect::MakeXYWH(writeX, writeY, bitmap.width(), bitmap.height());
    216     for (int cy = 0; cy < DEV_H; ++cy) {
    217         for (int cx = 0; cx < DEV_W; ++cx) {
    218             SkPMColor canvasPixel = canvasPixels[cx];
    219             if (writeRect.contains(cx, cy)) {
    220                 int bx = cx - writeX;
    221                 int by = cy - writeY;
    222                 uint32_t bmpColor8888 = get_bitmap_color(bx, by, bitmap.width(),
    223                                                        bmInfo.colorType(), bmInfo.alphaType());
    224                 bool mul = (kUnpremul_SkAlphaType == bmInfo.alphaType());
    225                 SkPMColor bmpPMColor = convert_to_PMColor(bmInfo.colorType(), bmInfo.alphaType(),
    226                                                           bmpColor8888);
    227                 if (!check_pixel(bmpPMColor, canvasPixel, mul)) {
    228                     ERRORF(reporter, "Expected canvas pixel at %d, %d to be 0x%08x, got 0x%08x. "
    229                            "Write performed premul: %d", cx, cy, bmpPMColor, canvasPixel, mul);
    230                     return false;
    231                 }
    232             } else {
    233                 SkPMColor testColor = get_canvas_color(cx, cy);
    234                 if (canvasPixel != testColor) {
    235                     ERRORF(reporter, "Canvas pixel outside write rect at %d, %d changed."
    236                            " Should be 0x%08x, got 0x%08x. ", cx, cy, testColor, canvasPixel);
    237                     return false;
    238                 }
    239             }
    240         }
    241         if (cy != DEV_H -1) {
    242             const char* pad = reinterpret_cast<const char*>(canvasPixels + DEV_W);
    243             for (size_t px = 0; px < canvasRowBytes - 4 * DEV_W; ++px) {
    244                 bool check;
    245                 REPORTER_ASSERT(reporter, check = (pad[px] == static_cast<char>(DEV_PAD)));
    246                 if (!check) {
    247                     return false;
    248                 }
    249             }
    250         }
    251         canvasPixels += canvasRowBytes/4;
    252     }
    253 
    254     return true;
    255 }
    256 
    257 #include "SkMallocPixelRef.h"
    258 
    259 // This is a tricky pattern, because we have to setConfig+rowBytes AND specify
    260 // a custom pixelRef (which also has to specify its rowBytes), so we have to be
    261 // sure that the two rowBytes match (and the infos match).
    262 //
    263 static bool alloc_row_bytes(SkBitmap* bm, const SkImageInfo& info, size_t rowBytes) {
    264     if (!bm->setInfo(info, rowBytes)) {
    265         return false;
    266     }
    267     sk_sp<SkPixelRef> pr = SkMallocPixelRef::MakeAllocate(info, rowBytes);
    268     bm->setPixelRef(std::move(pr), 0, 0);
    269     return true;
    270 }
    271 
    272 static void free_pixels(void* pixels, void* ctx) {
    273     sk_free(pixels);
    274 }
    275 
    276 static bool setup_bitmap(SkBitmap* bm, SkColorType ct, SkAlphaType at, int w, int h, int tightRB) {
    277     size_t rowBytes = tightRB ? 0 : 4 * w + 60;
    278     SkImageInfo info = SkImageInfo::Make(w, h, ct, at);
    279     if (!alloc_row_bytes(bm, info, rowBytes)) {
    280         return false;
    281     }
    282     for (int y = 0; y < h; ++y) {
    283         for (int x = 0; x < w; ++x) {
    284             *bm->getAddr32(x, y) = get_bitmap_color(x, y, w, ct, at);
    285         }
    286     }
    287     return true;
    288 }
    289 
    290 static void call_writepixels(SkCanvas* canvas) {
    291     const SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1);
    292     SkPMColor pixel = 0;
    293     canvas->writePixels(info, &pixel, sizeof(SkPMColor), 0, 0);
    294 }
    295 
    296 DEF_TEST(WritePixelsSurfaceGenID, reporter) {
    297     const SkImageInfo info = SkImageInfo::MakeN32Premul(100, 100);
    298     auto surface(SkSurface::MakeRaster(info));
    299     uint32_t genID1 = surface->generationID();
    300     call_writepixels(surface->getCanvas());
    301     uint32_t genID2 = surface->generationID();
    302     REPORTER_ASSERT(reporter, genID1 != genID2);
    303 }
    304 
    305 static void test_write_pixels(skiatest::Reporter* reporter, SkSurface* surface) {
    306     const SkIRect testRects[] = {
    307         // entire thing
    308         DEV_RECT,
    309         // larger on all sides
    310         SkIRect::MakeLTRB(-10, -10, DEV_W + 10, DEV_H + 10),
    311         // fully contained
    312         SkIRect::MakeLTRB(DEV_W / 4, DEV_H / 4, 3 * DEV_W / 4, 3 * DEV_H / 4),
    313         // outside top left
    314         SkIRect::MakeLTRB(-10, -10, -1, -1),
    315         // touching top left corner
    316         SkIRect::MakeLTRB(-10, -10, 0, 0),
    317         // overlapping top left corner
    318         SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H / 4),
    319         // overlapping top left and top right corners
    320         SkIRect::MakeLTRB(-10, -10, DEV_W  + 10, DEV_H / 4),
    321         // touching entire top edge
    322         SkIRect::MakeLTRB(-10, -10, DEV_W  + 10, 0),
    323         // overlapping top right corner
    324         SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W  + 10, DEV_H / 4),
    325         // contained in x, overlapping top edge
    326         SkIRect::MakeLTRB(DEV_W / 4, -10, 3 * DEV_W  / 4, DEV_H / 4),
    327         // outside top right corner
    328         SkIRect::MakeLTRB(DEV_W + 1, -10, DEV_W + 10, -1),
    329         // touching top right corner
    330         SkIRect::MakeLTRB(DEV_W, -10, DEV_W + 10, 0),
    331         // overlapping top left and bottom left corners
    332         SkIRect::MakeLTRB(-10, -10, DEV_W / 4, DEV_H + 10),
    333         // touching entire left edge
    334         SkIRect::MakeLTRB(-10, -10, 0, DEV_H + 10),
    335         // overlapping bottom left corner
    336         SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W / 4, DEV_H + 10),
    337         // contained in y, overlapping left edge
    338         SkIRect::MakeLTRB(-10, DEV_H / 4, DEV_W / 4, 3 * DEV_H / 4),
    339         // outside bottom left corner
    340         SkIRect::MakeLTRB(-10, DEV_H + 1, -1, DEV_H + 10),
    341         // touching bottom left corner
    342         SkIRect::MakeLTRB(-10, DEV_H, 0, DEV_H + 10),
    343         // overlapping bottom left and bottom right corners
    344         SkIRect::MakeLTRB(-10, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
    345         // touching entire left edge
    346         SkIRect::MakeLTRB(0, DEV_H, DEV_W, DEV_H + 10),
    347         // overlapping bottom right corner
    348         SkIRect::MakeLTRB(3 * DEV_W / 4, 3 * DEV_H / 4, DEV_W + 10, DEV_H + 10),
    349         // overlapping top right and bottom right corners
    350         SkIRect::MakeLTRB(3 * DEV_W / 4, -10, DEV_W + 10, DEV_H + 10),
    351     };
    352 
    353     SkCanvas& canvas = *surface->getCanvas();
    354 
    355     static const struct {
    356         SkColorType fColorType;
    357         SkAlphaType fAlphaType;
    358     } gSrcConfigs[] = {
    359         { kRGBA_8888_SkColorType, kPremul_SkAlphaType },
    360         { kRGBA_8888_SkColorType, kUnpremul_SkAlphaType },
    361         { kBGRA_8888_SkColorType, kPremul_SkAlphaType },
    362         { kBGRA_8888_SkColorType, kUnpremul_SkAlphaType },
    363     };
    364     for (size_t r = 0; r < SK_ARRAY_COUNT(testRects); ++r) {
    365         const SkIRect& rect = testRects[r];
    366         for (int tightBmp = 0; tightBmp < 2; ++tightBmp) {
    367             for (size_t c = 0; c < SK_ARRAY_COUNT(gSrcConfigs); ++c) {
    368                 const SkColorType ct = gSrcConfigs[c].fColorType;
    369                 const SkAlphaType at = gSrcConfigs[c].fAlphaType;
    370 
    371                 fill_canvas(&canvas);
    372                 SkBitmap bmp;
    373                 REPORTER_ASSERT(reporter, setup_bitmap(&bmp, ct, at, rect.width(),
    374                                                        rect.height(), SkToBool(tightBmp)));
    375                 uint32_t idBefore = surface->generationID();
    376 
    377                 // sk_tool_utils::write_pixels(&canvas, bmp, rect.fLeft, rect.fTop, ct, at);
    378                 canvas.writePixels(bmp, rect.fLeft, rect.fTop);
    379 
    380                 uint32_t idAfter = surface->generationID();
    381                 REPORTER_ASSERT(reporter, check_write(reporter, &canvas, bmp,
    382                                                       rect.fLeft, rect.fTop));
    383 
    384                 // we should change the genID iff pixels were actually written.
    385                 SkIRect canvasRect = SkIRect::MakeSize(canvas.getBaseLayerSize());
    386                 SkIRect writeRect = SkIRect::MakeXYWH(rect.fLeft, rect.fTop,
    387                                                       bmp.width(), bmp.height());
    388                 bool intersects = SkIRect::Intersects(canvasRect, writeRect) ;
    389                 REPORTER_ASSERT(reporter, intersects == (idBefore != idAfter));
    390             }
    391         }
    392     }
    393 }
    394 DEF_TEST(WritePixels, reporter) {
    395     const SkImageInfo info = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
    396     for (auto& tightRowBytes : { true, false }) {
    397         const size_t rowBytes = tightRowBytes ? info.minRowBytes() : 4 * DEV_W + 100;
    398         const size_t size = info.getSafeSize(rowBytes);
    399         void* pixels = sk_malloc_throw(size);
    400         // if rowBytes isn't tight then set the padding to a known value
    401         if (!tightRowBytes) {
    402             memset(pixels, DEV_PAD, size);
    403         }
    404         auto surface(SkSurface::MakeRasterDirectReleaseProc(info, pixels, rowBytes,
    405                                                             free_pixels, nullptr));
    406         test_write_pixels(reporter, surface.get());
    407     }
    408 }
    409 #if SK_SUPPORT_GPU
    410 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixels_Gpu, reporter, ctxInfo) {
    411     const SkImageInfo ii = SkImageInfo::MakeN32Premul(DEV_W, DEV_H);
    412 
    413     for (auto& origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
    414         for (int sampleCnt : {0, 4}) {
    415             sk_sp<SkSurface> surface(SkSurface::MakeRenderTarget(ctxInfo.grContext(),
    416                                                                  SkBudgeted::kNo, ii, sampleCnt,
    417                                                                  origin, nullptr));
    418             if (!surface && sampleCnt > 0) {
    419                 // Some platforms don't support MSAA
    420                 continue;
    421             }
    422             test_write_pixels(reporter, surface.get());
    423         }
    424     }
    425 }
    426 
    427 DEF_GPUTEST_FOR_RENDERING_CONTEXTS(WritePixelsNonTexture_Gpu, reporter, ctxInfo) {
    428     GrContext* context = ctxInfo.grContext();
    429 
    430     for (auto& origin : { kTopLeft_GrSurfaceOrigin, kBottomLeft_GrSurfaceOrigin }) {
    431         for (int sampleCnt : {0, 4}) {
    432             GrBackendTextureDesc desc;
    433             desc.fConfig = kSkia8888_GrPixelConfig;
    434             desc.fWidth = DEV_W;
    435             desc.fHeight = DEV_H;
    436             desc.fFlags = kRenderTarget_GrBackendTextureFlag;
    437             desc.fSampleCnt = sampleCnt;
    438             desc.fOrigin = origin;
    439             desc.fTextureHandle = context->getGpu()->createTestingOnlyBackendTexture(
    440                 nullptr, DEV_W, DEV_H, kSkia8888_GrPixelConfig, true);
    441             sk_sp<SkSurface> surface(SkSurface::MakeFromBackendTextureAsRenderTarget(context, desc,
    442                                                                                      nullptr));
    443             if (!surface) {
    444                 context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
    445                 continue;
    446             }
    447 
    448             test_write_pixels(reporter, surface.get());
    449 
    450             surface.reset();
    451             context->getGpu()->deleteTestingOnlyBackendTexture(desc.fTextureHandle);
    452         }
    453     }
    454 }
    455 #endif
    456