epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
reed@android.com | 78cd105 | 2010-03-08 20:23:57 +0000 | [diff] [blame] | 8 | #include "SkBitmap.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkColorPriv.h" |
reed@android.com | 9f8f48e | 2010-03-15 20:48:02 +0000 | [diff] [blame] | 11 | #include "SkGradientShader.h" |
reed@android.com | 78cd105 | 2010-03-08 20:23:57 +0000 | [diff] [blame] | 12 | #include "SkRect.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 13 | #include "Test.h" |
reed@android.com | 78cd105 | 2010-03-08 20:23:57 +0000 | [diff] [blame] | 14 | |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 15 | // these are in the same order as the SkColorType enum |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 16 | static const char* gColorTypeName[] = { |
| 17 | "None", "A8", "565", "4444", "RGBA", "BGRA", "Index8" |
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) { |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 65 | switch (bm.colorType()) { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 66 | case kN32_SkColorType: |
reed@android.com | 78cd105 | 2010-03-08 20:23:57 +0000 | [diff] [blame] | 67 | *expect = expect32; |
| 68 | return proc_32; |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 69 | case kARGB_4444_SkColorType: |
| 70 | case kRGB_565_SkColorType: |
reed@android.com | 78cd105 | 2010-03-08 20:23:57 +0000 | [diff] [blame] | 71 | *expect = expect16; |
| 72 | return proc_16; |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 73 | case kAlpha_8_SkColorType: |
reed@android.com | 78cd105 | 2010-03-08 20:23:57 +0000 | [diff] [blame] | 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) { |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 91 | ERRORF(reporter, "BlitRow colortype=%s [%d %d] expected %x got %x", |
| 92 | gColorTypeName[bm.colorType()], x, y, expect, bad); |
reed@android.com | 78cd105 | 2010-03-08 20:23:57 +0000 | [diff] [blame] | 93 | return false; |
| 94 | } |
| 95 | } |
| 96 | return true; |
| 97 | } |
| 98 | |
reed@android.com | 9f8f48e | 2010-03-15 20:48:02 +0000 | [diff] [blame] | 99 | // Make sure our blits always map src==0 to a noop, and src==FF to full opaque |
| 100 | static void test_00_FF(skiatest::Reporter* reporter) { |
reed@android.com | 78cd105 | 2010-03-08 20:23:57 +0000 | [diff] [blame] | 101 | static const int W = 256; |
| 102 | |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 103 | static const SkColorType gDstColorType[] = { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 104 | kN32_SkColorType, |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 105 | kRGB_565_SkColorType, |
reed@android.com | 78cd105 | 2010-03-08 20:23:57 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | static const struct { |
| 109 | SkColor fSrc; |
| 110 | SkColor fDst; |
| 111 | SkPMColor fResult32; |
| 112 | uint16_t fResult16; |
| 113 | uint8_t fResult8; |
| 114 | } gSrcRec[] = { |
| 115 | { 0, 0, 0, 0, 0 }, |
| 116 | { 0, 0xFFFFFFFF, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF }, |
| 117 | { 0xFFFFFFFF, 0, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF }, |
| 118 | { 0xFFFFFFFF, 0xFFFFFFFF, SkPackARGB32(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF, 0xFF }, |
| 119 | }; |
reed@android.com | 1db89dc | 2010-03-08 22:00:55 +0000 | [diff] [blame] | 120 | |
| 121 | SkPaint paint; |
reed@android.com | 1db89dc | 2010-03-08 22:00:55 +0000 | [diff] [blame] | 122 | |
reed@android.com | 78cd105 | 2010-03-08 20:23:57 +0000 | [diff] [blame] | 123 | SkBitmap srcBM; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 124 | srcBM.allocN32Pixels(W, 1); |
reed@android.com | 9f8f48e | 2010-03-15 20:48:02 +0000 | [diff] [blame] | 125 | |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 126 | for (size_t i = 0; i < SK_ARRAY_COUNT(gDstColorType); i++) { |
| 127 | SkImageInfo info = SkImageInfo::Make(W, 1, gDstColorType[i], |
| 128 | kPremul_SkAlphaType); |
reed@android.com | 78cd105 | 2010-03-08 20:23:57 +0000 | [diff] [blame] | 129 | SkBitmap dstBM; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 130 | dstBM.allocPixels(info); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 131 | |
reed@android.com | 78cd105 | 2010-03-08 20:23:57 +0000 | [diff] [blame] | 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 | |
reed@android.com | 9f8f48e | 2010-03-15 20:48:02 +0000 | [diff] [blame] | 157 | /////////////////////////////////////////////////////////////////////////////// |
| 158 | |
| 159 | struct Mesh { |
| 160 | SkPoint fPts[4]; |
reed@android.com | 9f8f48e | 2010-03-15 20:48:02 +0000 | [diff] [blame] | 161 | |
| 162 | Mesh(const SkBitmap& bm, SkPaint* paint) { |
| 163 | const SkScalar w = SkIntToScalar(bm.width()); |
| 164 | const SkScalar h = SkIntToScalar(bm.height()); |
| 165 | fPts[0].set(0, 0); |
| 166 | fPts[1].set(w, 0); |
| 167 | fPts[2].set(w, h); |
| 168 | fPts[3].set(0, h); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 169 | paint->setShader(SkShader::MakeBitmapShader(bm, SkShader::kClamp_TileMode, |
| 170 | SkShader::kClamp_TileMode)); |
reed@android.com | 9f8f48e | 2010-03-15 20:48:02 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | void draw(SkCanvas* canvas, SkPaint* paint) { |
| 174 | canvas->drawVertices(SkCanvas::kTriangleFan_VertexMode, 4, fPts, fPts, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 175 | nullptr, nullptr, nullptr, 0, *paint); |
reed@android.com | 9f8f48e | 2010-03-15 20:48:02 +0000 | [diff] [blame] | 176 | } |
| 177 | }; |
| 178 | |
| 179 | #include "SkImageEncoder.h" |
| 180 | static void save_bm(const SkBitmap& bm, const char name[]) { |
| 181 | SkImageEncoder::EncodeFile(name, bm, SkImageEncoder::kPNG_Type, 100); |
| 182 | } |
| 183 | |
| 184 | static bool gOnce; |
| 185 | |
| 186 | // Make sure our blits are invariant with the width of the blit (i.e. that |
| 187 | // special case for 8 at a time have the same results as narrower blits) |
| 188 | static void test_diagonal(skiatest::Reporter* reporter) { |
| 189 | static const int W = 64; |
| 190 | static const int H = W; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 191 | |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 192 | static const SkColorType gDstColorType[] = { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 193 | kN32_SkColorType, |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 194 | kRGB_565_SkColorType, |
reed@android.com | 9f8f48e | 2010-03-15 20:48:02 +0000 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | static const SkColor gDstBG[] = { 0, 0xFFFFFFFF }; |
| 198 | |
| 199 | SkPaint paint; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 200 | |
reed@android.com | 9f8f48e | 2010-03-15 20:48:02 +0000 | [diff] [blame] | 201 | SkBitmap srcBM; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 202 | srcBM.allocN32Pixels(W, H); |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 203 | SkRect srcR = { |
| 204 | 0, 0, SkIntToScalar(srcBM.width()), SkIntToScalar(srcBM.height()) }; |
reed@android.com | 9f8f48e | 2010-03-15 20:48:02 +0000 | [diff] [blame] | 205 | |
| 206 | // cons up a mesh to draw the bitmap with |
| 207 | Mesh mesh(srcBM, &paint); |
| 208 | |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 209 | SkImageInfo info = SkImageInfo::Make(W, H, kUnknown_SkColorType, |
| 210 | kPremul_SkAlphaType); |
| 211 | |
| 212 | for (size_t i = 0; i < SK_ARRAY_COUNT(gDstColorType); i++) { |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 213 | info = info.makeColorType(gDstColorType[i]); |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 214 | |
reed@android.com | 9f8f48e | 2010-03-15 20:48:02 +0000 | [diff] [blame] | 215 | SkBitmap dstBM0, dstBM1; |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 216 | dstBM0.allocPixels(info); |
| 217 | dstBM1.allocPixels(info); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 218 | |
reed@android.com | 9f8f48e | 2010-03-15 20:48:02 +0000 | [diff] [blame] | 219 | SkCanvas canvas0(dstBM0); |
| 220 | SkCanvas canvas1(dstBM1); |
| 221 | SkColor bgColor; |
| 222 | |
| 223 | for (size_t j = 0; j < SK_ARRAY_COUNT(gDstBG); j++) { |
| 224 | bgColor = gDstBG[j]; |
| 225 | |
| 226 | for (int c = 0; c <= 0xFF; c++) { |
| 227 | srcBM.eraseARGB(0xFF, c, c, c); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 228 | |
reed@android.com | 9f8f48e | 2010-03-15 20:48:02 +0000 | [diff] [blame] | 229 | for (int k = 0; k < 4; k++) { |
| 230 | bool dither = (k & 1) != 0; |
| 231 | uint8_t alpha = (k & 2) ? 0x80 : 0xFF; |
| 232 | paint.setDither(dither); |
| 233 | paint.setAlpha(alpha); |
| 234 | |
| 235 | dstBM0.eraseColor(bgColor); |
| 236 | dstBM1.eraseColor(bgColor); |
| 237 | |
| 238 | canvas0.drawRect(srcR, paint); |
| 239 | mesh.draw(&canvas1, &paint); |
| 240 | |
| 241 | if (!gOnce && false) { |
| 242 | save_bm(dstBM0, "drawBitmap.png"); |
| 243 | save_bm(dstBM1, "drawMesh.png"); |
| 244 | gOnce = true; |
| 245 | } |
| 246 | |
| 247 | if (memcmp(dstBM0.getPixels(), dstBM1.getPixels(), dstBM0.getSize())) { |
commit-bot@chromium.org | fa9e5fa | 2014-02-13 22:00:04 +0000 | [diff] [blame] | 248 | ERRORF(reporter, "Diagonal colortype=%s bg=0x%x dither=%d" |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 249 | " alpha=0x%x src=0x%x", |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 250 | gColorTypeName[gDstColorType[i]], bgColor, dither, |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 251 | alpha, c); |
reed@android.com | 9f8f48e | 2010-03-15 20:48:02 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 259 | DEF_TEST(BlitRow, reporter) { |
reed@android.com | 9f8f48e | 2010-03-15 20:48:02 +0000 | [diff] [blame] | 260 | test_00_FF(reporter); |
| 261 | test_diagonal(reporter); |
| 262 | } |