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