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