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