reed@android.com | 78cd105 | 2010-03-08 20:23:57 +0000 | [diff] [blame] | 1 | #include "Test.h" |
| 2 | #include "SkBitmap.h" |
| 3 | #include "SkCanvas.h" |
| 4 | #include "SkColorPriv.h" |
| 5 | #include "SkRect.h" |
| 6 | |
| 7 | static inline const char* boolStr(bool value) { |
| 8 | return value ? "true" : "false"; |
| 9 | } |
| 10 | |
| 11 | // these are in the same order as the SkBitmap::Config enum |
| 12 | static const char* gConfigName[] = { |
| 13 | "None", "A1", "A8", "Index8", "565", "4444", "8888", "RLE_Index8" |
| 14 | }; |
| 15 | |
| 16 | /** Returns -1 on success, else the x coord of the first bad pixel, return its |
| 17 | value in bad |
| 18 | */ |
| 19 | typedef int (*Proc)(const void*, int width, uint32_t expected, uint32_t* bad); |
| 20 | |
| 21 | static int proc_32(const void* ptr, int w, uint32_t expected, uint32_t* bad) { |
| 22 | const SkPMColor* addr = static_cast<const SkPMColor*>(ptr); |
| 23 | for (int x = 0; x < w; x++) { |
| 24 | if (addr[x] != expected) { |
| 25 | *bad = addr[x]; |
| 26 | return x; |
| 27 | } |
| 28 | } |
| 29 | return -1; |
| 30 | } |
| 31 | |
| 32 | static int proc_16(const void* ptr, int w, uint32_t expected, uint32_t* bad) { |
| 33 | const uint16_t* addr = static_cast<const uint16_t*>(ptr); |
| 34 | for (int x = 0; x < w; x++) { |
| 35 | if (addr[x] != expected) { |
| 36 | *bad = addr[x]; |
| 37 | return x; |
| 38 | } |
| 39 | } |
| 40 | return -1; |
| 41 | } |
| 42 | |
| 43 | static int proc_8(const void* ptr, int w, uint32_t expected, uint32_t* bad) { |
| 44 | const SkPMColor* addr = static_cast<const SkPMColor*>(ptr); |
| 45 | for (int x = 0; x < w; x++) { |
| 46 | if (SkGetPackedA32(addr[x]) != expected) { |
| 47 | *bad = SkGetPackedA32(addr[x]); |
| 48 | return x; |
| 49 | } |
| 50 | } |
| 51 | return -1; |
| 52 | } |
| 53 | |
| 54 | static int proc_bad(const void* ptr, int, uint32_t, uint32_t* bad) { |
| 55 | *bad = 0; |
| 56 | return 0; |
| 57 | } |
| 58 | |
| 59 | static Proc find_proc(const SkBitmap& bm, SkPMColor expect32, uint16_t expect16, |
| 60 | uint8_t expect8, uint32_t* expect) { |
| 61 | switch (bm.config()) { |
| 62 | case SkBitmap::kARGB_8888_Config: |
| 63 | *expect = expect32; |
| 64 | return proc_32; |
| 65 | case SkBitmap::kARGB_4444_Config: |
| 66 | case SkBitmap::kRGB_565_Config: |
| 67 | *expect = expect16; |
| 68 | return proc_16; |
| 69 | case SkBitmap::kA8_Config: |
| 70 | *expect = expect8; |
| 71 | return proc_8; |
| 72 | default: |
| 73 | *expect = 0; |
| 74 | return proc_bad; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static bool check_color(const SkBitmap& bm, SkPMColor expect32, |
| 79 | uint16_t expect16, uint8_t expect8, |
| 80 | skiatest::Reporter* reporter) { |
| 81 | uint32_t expect; |
| 82 | Proc proc = find_proc(bm, expect32, expect16, expect8, &expect); |
| 83 | for (int y = 0; y < bm.height(); y++) { |
| 84 | uint32_t bad; |
| 85 | int x = proc(bm.getAddr(0, y), bm.width(), expect, &bad); |
| 86 | if (x >= 0) { |
| 87 | SkString str; |
| 88 | str.printf("BlitRow config=%s [%d %d] expected %x got %x", |
| 89 | gConfigName[bm.config()], x, y, expect, bad); |
| 90 | reporter->reportFailed(str); |
| 91 | return false; |
| 92 | } |
| 93 | } |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | static void TestBlitRow(skiatest::Reporter* reporter) { |
| 98 | static const int W = 256; |
| 99 | |
| 100 | static const SkBitmap::Config gDstConfig[] = { |
| 101 | SkBitmap::kARGB_8888_Config, |
| 102 | SkBitmap::kRGB_565_Config, |
reed@android.com | 8e4c93b | 2010-03-09 15:21:28 +0000 | [diff] [blame^] | 103 | // SkBitmap::kARGB_4444_Config, |
reed@android.com | 78cd105 | 2010-03-08 20:23:57 +0000 | [diff] [blame] | 104 | // SkBitmap::kA8_Config, |
| 105 | }; |
| 106 | |
| 107 | static const struct { |
| 108 | SkColor fSrc; |
| 109 | SkColor fDst; |
| 110 | SkPMColor fResult32; |
| 111 | uint16_t fResult16; |
| 112 | uint8_t fResult8; |
| 113 | } gSrcRec[] = { |
| 114 | { 0, 0, 0, 0, 0 }, |
| 115 | { 0, 0xFFFFFFFF, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF }, |
| 116 | { 0xFFFFFFFF, 0, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF }, |
| 117 | { 0xFFFFFFFF, 0xFFFFFFFF, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF }, |
| 118 | }; |
reed@android.com | 1db89dc | 2010-03-08 22:00:55 +0000 | [diff] [blame] | 119 | |
| 120 | SkPaint paint; |
| 121 | paint.setDither(true); |
| 122 | |
reed@android.com | 78cd105 | 2010-03-08 20:23:57 +0000 | [diff] [blame] | 123 | SkBitmap srcBM; |
| 124 | srcBM.setConfig(SkBitmap::kARGB_8888_Config, W, 1); |
| 125 | srcBM.allocPixels(); |
| 126 | |
| 127 | for (size_t i = 0; i < SK_ARRAY_COUNT(gDstConfig); i++) { |
| 128 | SkBitmap dstBM; |
| 129 | dstBM.setConfig(gDstConfig[i], W, 1); |
| 130 | dstBM.allocPixels(); |
| 131 | |
| 132 | SkCanvas canvas(dstBM); |
| 133 | for (size_t j = 0; j < SK_ARRAY_COUNT(gSrcRec); j++) { |
| 134 | srcBM.eraseColor(gSrcRec[j].fSrc); |
| 135 | dstBM.eraseColor(gSrcRec[j].fDst); |
| 136 | |
reed@android.com | 1db89dc | 2010-03-08 22:00:55 +0000 | [diff] [blame] | 137 | for (int k = 0; k < 4; k++) { |
| 138 | bool dither = (k & 1) != 0; |
| 139 | bool blend = (k & 2) != 0; |
| 140 | if (gSrcRec[j].fSrc != 0 && blend) { |
| 141 | // can't make a numerical promise about blending anything |
| 142 | // but 0 |
reed@android.com | 8e4c93b | 2010-03-09 15:21:28 +0000 | [diff] [blame^] | 143 | // continue; |
reed@android.com | 1db89dc | 2010-03-08 22:00:55 +0000 | [diff] [blame] | 144 | } |
| 145 | paint.setDither(dither); |
| 146 | paint.setAlpha(blend ? 0x80 : 0xFF); |
| 147 | canvas.drawBitmap(srcBM, 0, 0, &paint); |
| 148 | if (!check_color(dstBM, gSrcRec[j].fResult32, gSrcRec[j].fResult16, |
| 149 | gSrcRec[j].fResult8, reporter)) { |
| 150 | SkDebugf("--- src index %d dither %d blend %d\n", j, dither, blend); |
| 151 | } |
reed@android.com | 78cd105 | 2010-03-08 20:23:57 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | #include "TestClassDef.h" |
| 158 | DEFINE_TESTCLASS("BlitRow", TestBlitRowClass, TestBlitRow) |