Home | History | Annotate | Download | only in tests
      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 #include "Test.h"
      9 #include "SkBitmap.h"
     10 #include "SkCanvas.h"
     11 #include "SkColorPriv.h"
     12 #include "SkGradientShader.h"
     13 #include "SkRect.h"
     14 
     15 static inline const char* boolStr(bool value) {
     16     return value ? "true" : "false";
     17 }
     18 
     19 // these are in the same order as the SkBitmap::Config enum
     20 static const char* gConfigName[] = {
     21     "None", "A1", "A8", "Index8", "565", "4444", "8888", "RLE_Index8"
     22 };
     23 
     24 /** Returns -1 on success, else the x coord of the first bad pixel, return its
     25     value in bad
     26  */
     27 typedef int (*Proc)(const void*, int width, uint32_t expected, uint32_t* bad);
     28 
     29 static int proc_32(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
     30     const SkPMColor* addr = static_cast<const SkPMColor*>(ptr);
     31     for (int x = 0; x < w; x++) {
     32         if (addr[x] != expected) {
     33             *bad = addr[x];
     34             return x;
     35         }
     36     }
     37     return -1;
     38 }
     39 
     40 static int proc_16(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
     41     const uint16_t* addr = static_cast<const uint16_t*>(ptr);
     42     for (int x = 0; x < w; x++) {
     43         if (addr[x] != expected) {
     44             *bad = addr[x];
     45             return x;
     46         }
     47     }
     48     return -1;
     49 }
     50 
     51 static int proc_8(const void* ptr, int w, uint32_t expected, uint32_t* bad) {
     52     const SkPMColor* addr = static_cast<const SkPMColor*>(ptr);
     53     for (int x = 0; x < w; x++) {
     54         if (SkGetPackedA32(addr[x]) != expected) {
     55             *bad = SkGetPackedA32(addr[x]);
     56             return x;
     57         }
     58     }
     59     return -1;
     60 }
     61 
     62 static int proc_bad(const void*, int, uint32_t, uint32_t* bad) {
     63     *bad = 0;
     64     return 0;
     65 }
     66 
     67 static Proc find_proc(const SkBitmap& bm, SkPMColor expect32, uint16_t expect16,
     68                       uint8_t expect8, uint32_t* expect) {
     69     switch (bm.config()) {
     70         case SkBitmap::kARGB_8888_Config:
     71             *expect = expect32;
     72             return proc_32;
     73         case SkBitmap::kARGB_4444_Config:
     74         case SkBitmap::kRGB_565_Config:
     75             *expect = expect16;
     76             return proc_16;
     77         case SkBitmap::kA8_Config:
     78             *expect = expect8;
     79             return proc_8;
     80         default:
     81             *expect = 0;
     82             return proc_bad;
     83     }
     84 }
     85 
     86 static bool check_color(const SkBitmap& bm, SkPMColor expect32,
     87                         uint16_t expect16, uint8_t expect8,
     88                         skiatest::Reporter* reporter) {
     89     uint32_t expect;
     90     Proc proc = find_proc(bm, expect32, expect16, expect8, &expect);
     91     for (int y = 0; y < bm.height(); y++) {
     92         uint32_t bad;
     93         int x = proc(bm.getAddr(0, y), bm.width(), expect, &bad);
     94         if (x >= 0) {
     95             SkString str;
     96             str.printf("BlitRow config=%s [%d %d] expected %x got %x",
     97                        gConfigName[bm.config()], x, y, expect, bad);
     98             reporter->reportFailed(str);
     99             return false;
    100         }
    101     }
    102     return true;
    103 }
    104 
    105 // Make sure our blits always map src==0 to a noop, and src==FF to full opaque
    106 static void test_00_FF(skiatest::Reporter* reporter) {
    107     static const int W = 256;
    108 
    109     static const SkBitmap::Config gDstConfig[] = {
    110         SkBitmap::kARGB_8888_Config,
    111         SkBitmap::kRGB_565_Config,
    112 //        SkBitmap::kARGB_4444_Config,
    113 //        SkBitmap::kA8_Config,
    114     };
    115 
    116     static const struct {
    117         SkColor     fSrc;
    118         SkColor     fDst;
    119         SkPMColor   fResult32;
    120         uint16_t    fResult16;
    121         uint8_t     fResult8;
    122     } gSrcRec[] = {
    123         { 0,            0,          0,                                    0,      0 },
    124         { 0,            0xFFFFFFFF, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
    125         { 0xFFFFFFFF,   0,          SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
    126         { 0xFFFFFFFF,   0xFFFFFFFF, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF },
    127     };
    128 
    129     SkPaint paint;
    130 
    131     SkBitmap srcBM;
    132     srcBM.setConfig(SkBitmap::kARGB_8888_Config, W, 1);
    133     srcBM.allocPixels();
    134 
    135     for (size_t i = 0; i < SK_ARRAY_COUNT(gDstConfig); i++) {
    136         SkBitmap dstBM;
    137         dstBM.setConfig(gDstConfig[i], W, 1);
    138         dstBM.allocPixels();
    139 
    140         SkCanvas canvas(dstBM);
    141         for (size_t j = 0; j < SK_ARRAY_COUNT(gSrcRec); j++) {
    142             srcBM.eraseColor(gSrcRec[j].fSrc);
    143             dstBM.eraseColor(gSrcRec[j].fDst);
    144 
    145             for (int k = 0; k < 4; k++) {
    146                 bool dither = (k & 1) != 0;
    147                 bool blend = (k & 2) != 0;
    148                 if (gSrcRec[j].fSrc != 0 && blend) {
    149                     // can't make a numerical promise about blending anything
    150                     // but 0
    151                  //   continue;
    152                 }
    153                 paint.setDither(dither);
    154                 paint.setAlpha(blend ? 0x80 : 0xFF);
    155                 canvas.drawBitmap(srcBM, 0, 0, &paint);
    156                 if (!check_color(dstBM, gSrcRec[j].fResult32, gSrcRec[j].fResult16,
    157                                  gSrcRec[j].fResult8, reporter)) {
    158                     SkDebugf("--- src index %d dither %d blend %d\n", j, dither, blend);
    159                 }
    160             }
    161         }
    162     }
    163 }
    164 
    165 ///////////////////////////////////////////////////////////////////////////////
    166 
    167 struct Mesh {
    168     SkPoint     fPts[4];
    169 
    170     Mesh(const SkBitmap& bm, SkPaint* paint) {
    171         const SkScalar w = SkIntToScalar(bm.width());
    172         const SkScalar h = SkIntToScalar(bm.height());
    173         fPts[0].set(0, 0);
    174         fPts[1].set(w, 0);
    175         fPts[2].set(w, h);
    176         fPts[3].set(0, h);
    177         SkShader* s = SkShader::CreateBitmapShader(bm, SkShader::kClamp_TileMode,
    178                                                    SkShader::kClamp_TileMode);
    179         paint->setShader(s)->unref();
    180 
    181     }
    182 
    183     void draw(SkCanvas* canvas, SkPaint* paint) {
    184         canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, fPts, fPts,
    185                              NULL, NULL, NULL, 0, *paint);
    186     }
    187 };
    188 
    189 #include "SkImageEncoder.h"
    190 static void save_bm(const SkBitmap& bm, const char name[]) {
    191     SkImageEncoder::EncodeFile(name, bm, SkImageEncoder::kPNG_Type, 100);
    192 }
    193 
    194 static bool gOnce;
    195 
    196 // Make sure our blits are invariant with the width of the blit (i.e. that
    197 // special case for 8 at a time have the same results as narrower blits)
    198 static void test_diagonal(skiatest::Reporter* reporter) {
    199     static const int W = 64;
    200     static const int H = W;
    201 
    202     static const SkBitmap::Config gDstConfig[] = {
    203         SkBitmap::kARGB_8888_Config,
    204         SkBitmap::kRGB_565_Config,
    205         //        SkBitmap::kARGB_4444_Config,
    206         //        SkBitmap::kA8_Config,
    207     };
    208 
    209     static const SkColor gDstBG[] = { 0, 0xFFFFFFFF };
    210 
    211     SkPaint paint;
    212 
    213     SkBitmap srcBM;
    214     srcBM.setConfig(SkBitmap::kARGB_8888_Config, W, H);
    215     srcBM.allocPixels();
    216     SkRect srcR = {
    217         0, 0, SkIntToScalar(srcBM.width()), SkIntToScalar(srcBM.height()) };
    218 
    219     // cons up a mesh to draw the bitmap with
    220     Mesh mesh(srcBM, &paint);
    221 
    222     for (size_t i = 0; i < SK_ARRAY_COUNT(gDstConfig); i++) {
    223         SkBitmap dstBM0, dstBM1;
    224         dstBM0.setConfig(gDstConfig[i], W, H);
    225         dstBM1.setConfig(gDstConfig[i], W, H);
    226         dstBM0.allocPixels();
    227         dstBM1.allocPixels();
    228 
    229         SkCanvas canvas0(dstBM0);
    230         SkCanvas canvas1(dstBM1);
    231         SkColor bgColor;
    232 
    233         for (size_t j = 0; j < SK_ARRAY_COUNT(gDstBG); j++) {
    234             bgColor = gDstBG[j];
    235 
    236             for (int c = 0; c <= 0xFF; c++) {
    237                 srcBM.eraseARGB(0xFF, c, c, c);
    238 
    239                 for (int k = 0; k < 4; k++) {
    240                     bool dither = (k & 1) != 0;
    241                     uint8_t alpha = (k & 2) ? 0x80 : 0xFF;
    242                     paint.setDither(dither);
    243                     paint.setAlpha(alpha);
    244 
    245                     dstBM0.eraseColor(bgColor);
    246                     dstBM1.eraseColor(bgColor);
    247 
    248                     canvas0.drawRect(srcR, paint);
    249                     mesh.draw(&canvas1, &paint);
    250 
    251                     if (!gOnce && false) {
    252                         save_bm(dstBM0, "drawBitmap.png");
    253                         save_bm(dstBM1, "drawMesh.png");
    254                         gOnce = true;
    255                     }
    256 
    257                     if (memcmp(dstBM0.getPixels(), dstBM1.getPixels(), dstBM0.getSize())) {
    258                         SkString str;
    259                         str.printf("Diagonal config=%s bg=0x%x dither=%d alpha=0x%x src=0x%x",
    260                                    gConfigName[gDstConfig[i]], bgColor, dither, alpha, c);
    261                         reporter->reportFailed(str);
    262                     }
    263                 }
    264             }
    265         }
    266     }
    267 }
    268 
    269 static void TestBlitRow(skiatest::Reporter* reporter) {
    270     test_00_FF(reporter);
    271     test_diagonal(reporter);
    272 }
    273 
    274 #include "TestClassDef.h"
    275 DEFINE_TESTCLASS("BlitRow", TestBlitRowClass, TestBlitRow)
    276