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 | */ |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 7 | |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 8 | #include "SkBitmap.h" |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 9 | #include "SkRect.h" |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 10 | #include "SkTemplates.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 11 | #include "Test.h" |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 12 | |
| 13 | static const char* boolStr(bool value) { |
| 14 | return value ? "true" : "false"; |
| 15 | } |
| 16 | |
Matt Sarett | d9836f4 | 2017-04-05 15:41:53 -0400 | [diff] [blame] | 17 | static const char* color_type_name(SkColorType colorType) { |
| 18 | switch (colorType) { |
| 19 | case kUnknown_SkColorType: |
| 20 | return "None"; |
| 21 | case kAlpha_8_SkColorType: |
| 22 | return "A8"; |
| 23 | case kRGB_565_SkColorType: |
| 24 | return "565"; |
| 25 | case kARGB_4444_SkColorType: |
| 26 | return "4444"; |
| 27 | case kRGBA_8888_SkColorType: |
| 28 | return "RGBA"; |
| 29 | case kBGRA_8888_SkColorType: |
| 30 | return "BGRA"; |
| 31 | case kIndex_8_SkColorType: |
| 32 | return "Index8"; |
| 33 | case kGray_8_SkColorType: |
| 34 | return "Gray8"; |
| 35 | case kRGBA_F16_SkColorType: |
| 36 | return "F16"; |
| 37 | } |
| 38 | return ""; |
| 39 | } |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 40 | |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 41 | static void report_opaqueness(skiatest::Reporter* reporter, const SkBitmap& src, |
| 42 | const SkBitmap& dst) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 43 | ERRORF(reporter, "src %s opaque:%d, dst %s opaque:%d", |
Matt Sarett | d9836f4 | 2017-04-05 15:41:53 -0400 | [diff] [blame] | 44 | color_type_name(src.colorType()), src.isOpaque(), |
| 45 | color_type_name(dst.colorType()), dst.isOpaque()); |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 46 | } |
| 47 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 48 | static bool canHaveAlpha(SkColorType ct) { |
| 49 | return kRGB_565_SkColorType != ct; |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | // copyTo() should preserve isOpaque when it makes sense |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 53 | static void test_isOpaque(skiatest::Reporter* reporter, |
| 54 | const SkBitmap& srcOpaque, const SkBitmap& srcPremul, |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 55 | SkColorType dstColorType) { |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 56 | SkBitmap dst; |
| 57 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 58 | if (canHaveAlpha(srcPremul.colorType()) && canHaveAlpha(dstColorType)) { |
| 59 | REPORTER_ASSERT(reporter, srcPremul.copyTo(&dst, dstColorType)); |
| 60 | REPORTER_ASSERT(reporter, dst.colorType() == dstColorType); |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 61 | if (srcPremul.isOpaque() != dst.isOpaque()) { |
| 62 | report_opaqueness(reporter, srcPremul, dst); |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 66 | REPORTER_ASSERT(reporter, srcOpaque.copyTo(&dst, dstColorType)); |
| 67 | REPORTER_ASSERT(reporter, dst.colorType() == dstColorType); |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 68 | if (srcOpaque.isOpaque() != dst.isOpaque()) { |
| 69 | report_opaqueness(reporter, srcOpaque, dst); |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 73 | static void init_src(const SkBitmap& bitmap) { |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 74 | SkAutoLockPixels lock(bitmap); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 75 | if (bitmap.getPixels()) { |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 76 | if (bitmap.getColorTable()) { |
reed@google.com | 9ce6e75 | 2011-01-10 14:04:07 +0000 | [diff] [blame] | 77 | sk_bzero(bitmap.getPixels(), bitmap.getSize()); |
| 78 | } else { |
| 79 | bitmap.eraseColor(SK_ColorWHITE); |
| 80 | } |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 84 | static sk_sp<SkColorTable> init_ctable() { |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 85 | static const SkColor colors[] = { |
| 86 | SK_ColorBLACK, SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE |
| 87 | }; |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 88 | return SkColorTable::Make(colors, SK_ARRAY_COUNT(colors)); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | struct Pair { |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 92 | SkColorType fColorType; |
| 93 | const char* fValid; |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 96 | // Utility functions for copyPixelsTo()/copyPixelsFrom() tests. |
| 97 | // getPixel() |
| 98 | // setPixel() |
| 99 | // getSkConfigName() |
| 100 | // struct Coordinates |
| 101 | // reportCopyVerification() |
| 102 | // writeCoordPixels() |
| 103 | |
| 104 | // Utility function to read the value of a given pixel in bm. All |
| 105 | // values converted to uint32_t for simplification of comparisons. |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 106 | static uint32_t getPixel(int x, int y, const SkBitmap& bm) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 107 | uint32_t val = 0; |
| 108 | uint16_t val16; |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 109 | uint8_t val8; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 110 | SkAutoLockPixels lock(bm); |
| 111 | const void* rawAddr = bm.getAddr(x,y); |
| 112 | |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 113 | switch (bm.bytesPerPixel()) { |
| 114 | case 4: |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 115 | memcpy(&val, rawAddr, sizeof(uint32_t)); |
| 116 | break; |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 117 | case 2: |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 118 | memcpy(&val16, rawAddr, sizeof(uint16_t)); |
| 119 | val = val16; |
| 120 | break; |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 121 | case 1: |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 122 | memcpy(&val8, rawAddr, sizeof(uint8_t)); |
| 123 | val = val8; |
| 124 | break; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 125 | default: |
| 126 | break; |
| 127 | } |
| 128 | return val; |
| 129 | } |
| 130 | |
| 131 | // Utility function to set value of any pixel in bm. |
| 132 | // bm.getConfig() specifies what format 'val' must be |
| 133 | // converted to, but at present uint32_t can handle all formats. |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 134 | static void setPixel(int x, int y, uint32_t val, SkBitmap& bm) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 135 | uint16_t val16; |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 136 | uint8_t val8; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 137 | SkAutoLockPixels lock(bm); |
| 138 | void* rawAddr = bm.getAddr(x,y); |
| 139 | |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 140 | switch (bm.bytesPerPixel()) { |
| 141 | case 4: |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 142 | memcpy(rawAddr, &val, sizeof(uint32_t)); |
| 143 | break; |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 144 | case 2: |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 145 | val16 = val & 0xFFFF; |
| 146 | memcpy(rawAddr, &val16, sizeof(uint16_t)); |
| 147 | break; |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 148 | case 1: |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 149 | val8 = val & 0xFF; |
| 150 | memcpy(rawAddr, &val8, sizeof(uint8_t)); |
| 151 | break; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 152 | default: |
| 153 | // Ignore. |
| 154 | break; |
| 155 | } |
| 156 | } |
| 157 | |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 158 | // Helper struct to contain pixel locations, while avoiding need for STL. |
| 159 | struct Coordinates { |
| 160 | |
| 161 | const int length; |
| 162 | SkIPoint* const data; |
| 163 | |
| 164 | explicit Coordinates(int _length): length(_length) |
| 165 | , data(new SkIPoint[length]) { } |
| 166 | |
| 167 | ~Coordinates(){ |
| 168 | delete [] data; |
| 169 | } |
| 170 | |
| 171 | SkIPoint* operator[](int i) const { |
| 172 | // Use with care, no bounds checking. |
| 173 | return data + i; |
| 174 | } |
| 175 | }; |
| 176 | |
| 177 | // A function to verify that two bitmaps contain the same pixel values |
| 178 | // at all coordinates indicated by coords. Simplifies verification of |
| 179 | // copied bitmaps. |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 180 | static void reportCopyVerification(const SkBitmap& bm1, const SkBitmap& bm2, |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 181 | Coordinates& coords, |
| 182 | const char* msg, |
| 183 | skiatest::Reporter* reporter){ |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 184 | // Confirm all pixels in the list match. |
scroggo@google.com | d5764e8 | 2012-08-22 15:00:05 +0000 | [diff] [blame] | 185 | for (int i = 0; i < coords.length; ++i) { |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 186 | uint32_t p1 = getPixel(coords[i]->fX, coords[i]->fY, bm1); |
| 187 | uint32_t p2 = getPixel(coords[i]->fX, coords[i]->fY, bm2); |
| 188 | // SkDebugf("[%d] (%d %d) p1=%x p2=%x\n", i, coords[i]->fX, coords[i]->fY, p1, p2); |
| 189 | if (p1 != p2) { |
Matt Sarett | d9836f4 | 2017-04-05 15:41:53 -0400 | [diff] [blame] | 190 | ERRORF(reporter, "%s [colortype = %s]", msg, color_type_name(bm1.colorType())); |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 191 | break; |
| 192 | } |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | // Writes unique pixel values at locations specified by coords. |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 197 | static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 198 | for (int i = 0; i < coords.length; ++i) |
| 199 | setPixel(coords[i]->fX, coords[i]->fY, i, bm); |
| 200 | } |
| 201 | |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 202 | static const Pair gPairs[] = { |
Matt Sarett | d9836f4 | 2017-04-05 15:41:53 -0400 | [diff] [blame] | 203 | { kUnknown_SkColorType, "0000000" }, |
| 204 | { kAlpha_8_SkColorType, "0100000" }, |
| 205 | { kIndex_8_SkColorType, "0101111" }, |
| 206 | { kRGB_565_SkColorType, "0101011" }, |
| 207 | { kARGB_4444_SkColorType, "0101111" }, |
| 208 | { kN32_SkColorType, "0101111" }, |
| 209 | { kRGBA_F16_SkColorType, "0101011" }, |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 210 | }; |
commit-bot@chromium.org | 5c6f1d4 | 2014-01-10 18:28:23 +0000 | [diff] [blame] | 211 | |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 212 | static const int W = 20; |
| 213 | static const int H = 33; |
| 214 | |
| 215 | static void setup_src_bitmaps(SkBitmap* srcOpaque, SkBitmap* srcPremul, |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 216 | SkColorType ct) { |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 217 | sk_sp<SkColorTable> ctable; |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 218 | if (kIndex_8_SkColorType == ct) { |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 219 | ctable = init_ctable(); |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 220 | } |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 221 | |
Matt Sarett | d9836f4 | 2017-04-05 15:41:53 -0400 | [diff] [blame] | 222 | sk_sp<SkColorSpace> colorSpace = nullptr; |
| 223 | if (kRGBA_F16_SkColorType == ct) { |
| 224 | colorSpace = SkColorSpace::MakeSRGBLinear(); |
| 225 | } |
| 226 | |
| 227 | srcOpaque->allocPixels(SkImageInfo::Make(W, H, ct, kOpaque_SkAlphaType, colorSpace), ctable); |
| 228 | srcPremul->allocPixels(SkImageInfo::Make(W, H, ct, kPremul_SkAlphaType, colorSpace), ctable); |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 229 | init_src(*srcOpaque); |
| 230 | init_src(*srcPremul); |
| 231 | } |
| 232 | |
| 233 | DEF_TEST(BitmapCopy_extractSubset, reporter) { |
| 234 | for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) { |
| 235 | SkBitmap srcOpaque, srcPremul; |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 236 | setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fColorType); |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 237 | |
| 238 | SkBitmap bitmap(srcOpaque); |
| 239 | SkBitmap subset; |
| 240 | SkIRect r; |
| 241 | // Extract a subset which has the same width as the original. This |
| 242 | // catches a bug where we cloned the genID incorrectly. |
| 243 | r.set(0, 1, W, 3); |
| 244 | bitmap.setIsVolatile(true); |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 245 | // Relies on old behavior of extractSubset failing if colortype is unknown |
| 246 | if (kUnknown_SkColorType != bitmap.colorType() && bitmap.extractSubset(&subset, r)) { |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 247 | REPORTER_ASSERT(reporter, subset.width() == W); |
| 248 | REPORTER_ASSERT(reporter, subset.height() == 2); |
| 249 | REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType()); |
| 250 | REPORTER_ASSERT(reporter, subset.isVolatile() == true); |
| 251 | |
| 252 | // Test copying an extracted subset. |
| 253 | for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) { |
| 254 | SkBitmap copy; |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 255 | bool success = subset.copyTo(©, gPairs[j].fColorType); |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 256 | if (!success) { |
| 257 | // Skip checking that success matches fValid, which is redundant |
| 258 | // with the code below. |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 259 | REPORTER_ASSERT(reporter, kIndex_8_SkColorType == gPairs[i].fColorType || |
| 260 | gPairs[i].fColorType != gPairs[j].fColorType); |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 261 | continue; |
| 262 | } |
| 263 | |
| 264 | // When performing a copy of an extracted subset, the gen id should |
| 265 | // change. |
| 266 | REPORTER_ASSERT(reporter, copy.getGenerationID() != subset.getGenerationID()); |
| 267 | |
| 268 | REPORTER_ASSERT(reporter, copy.width() == W); |
| 269 | REPORTER_ASSERT(reporter, copy.height() == 2); |
| 270 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 271 | if (gPairs[i].fColorType == gPairs[j].fColorType) { |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 272 | SkAutoLockPixels alp0(subset); |
| 273 | SkAutoLockPixels alp1(copy); |
| 274 | // they should both have, or both not-have, a colortable |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 275 | bool hasCT = subset.getColorTable() != nullptr; |
| 276 | REPORTER_ASSERT(reporter, (copy.getColorTable() != nullptr) == hasCT); |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | bitmap = srcPremul; |
| 282 | bitmap.setIsVolatile(false); |
| 283 | if (bitmap.extractSubset(&subset, r)) { |
| 284 | REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType()); |
| 285 | REPORTER_ASSERT(reporter, subset.isVolatile() == false); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | DEF_TEST(BitmapCopy, reporter) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 291 | static const bool isExtracted[] = { |
| 292 | false, true |
| 293 | }; |
| 294 | |
commit-bot@chromium.org | 5c6f1d4 | 2014-01-10 18:28:23 +0000 | [diff] [blame] | 295 | for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) { |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 296 | SkBitmap srcOpaque, srcPremul; |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 297 | setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fColorType); |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 298 | |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 299 | for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) { |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 300 | SkBitmap dst; |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 301 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 302 | bool success = srcPremul.copyTo(&dst, gPairs[j].fColorType); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 303 | bool expected = gPairs[i].fValid[j] != '0'; |
| 304 | if (success != expected) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 305 | ERRORF(reporter, "SkBitmap::copyTo from %s to %s. expected %s " |
Matt Sarett | d9836f4 | 2017-04-05 15:41:53 -0400 | [diff] [blame] | 306 | "returned %s", color_type_name(gPairs[i].fColorType), |
| 307 | color_type_name(gPairs[j].fColorType), |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 308 | boolStr(expected), boolStr(success)); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 309 | } |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 310 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 311 | bool canSucceed = srcPremul.canCopyTo(gPairs[j].fColorType); |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 312 | if (success != canSucceed) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 313 | ERRORF(reporter, "SkBitmap::copyTo from %s to %s. returned %s " |
Matt Sarett | d9836f4 | 2017-04-05 15:41:53 -0400 | [diff] [blame] | 314 | "canCopyTo %s", color_type_name(gPairs[i].fColorType), |
| 315 | color_type_name(gPairs[j].fColorType), |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 316 | boolStr(success), boolStr(canSucceed)); |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 317 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 318 | |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 319 | if (success) { |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 320 | REPORTER_ASSERT(reporter, srcPremul.width() == dst.width()); |
| 321 | REPORTER_ASSERT(reporter, srcPremul.height() == dst.height()); |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 322 | REPORTER_ASSERT(reporter, dst.colorType() == gPairs[j].fColorType); |
| 323 | test_isOpaque(reporter, srcOpaque, srcPremul, dst.colorType()); |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 324 | if (srcPremul.colorType() == dst.colorType()) { |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 325 | SkAutoLockPixels srcLock(srcPremul); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 326 | SkAutoLockPixels dstLock(dst); |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 327 | REPORTER_ASSERT(reporter, srcPremul.readyToDraw()); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 328 | REPORTER_ASSERT(reporter, dst.readyToDraw()); |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 329 | const char* srcP = (const char*)srcPremul.getAddr(0, 0); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 330 | const char* dstP = (const char*)dst.getAddr(0, 0); |
| 331 | REPORTER_ASSERT(reporter, srcP != dstP); |
| 332 | REPORTER_ASSERT(reporter, !memcmp(srcP, dstP, |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 333 | srcPremul.getSize())); |
| 334 | REPORTER_ASSERT(reporter, srcPremul.getGenerationID() == dst.getGenerationID()); |
scroggo@google.com | d5764e8 | 2012-08-22 15:00:05 +0000 | [diff] [blame] | 335 | } else { |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 336 | REPORTER_ASSERT(reporter, srcPremul.getGenerationID() != dst.getGenerationID()); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 337 | } |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 338 | } else { |
| 339 | // dst should be unchanged from its initial state |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 340 | REPORTER_ASSERT(reporter, dst.colorType() == kUnknown_SkColorType); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 341 | REPORTER_ASSERT(reporter, dst.width() == 0); |
| 342 | REPORTER_ASSERT(reporter, dst.height() == 0); |
| 343 | } |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 344 | } // for (size_t j = ... |
| 345 | |
| 346 | // Tests for getSafeSize(), getSafeSize64(), copyPixelsTo(), |
| 347 | // copyPixelsFrom(). |
| 348 | // |
| 349 | for (size_t copyCase = 0; copyCase < SK_ARRAY_COUNT(isExtracted); |
| 350 | ++copyCase) { |
| 351 | // Test copying to/from external buffer. |
| 352 | // Note: the tests below have hard-coded values --- |
| 353 | // Please take care if modifying. |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 354 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 355 | // Tests for getSafeSize64(). |
| 356 | // Test with a very large configuration without pixel buffer |
| 357 | // attached. |
| 358 | SkBitmap tstSafeSize; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 359 | tstSafeSize.setInfo(SkImageInfo::Make(100000000U, 100000000U, |
| 360 | gPairs[i].fColorType, kPremul_SkAlphaType)); |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 361 | int64_t safeSize = tstSafeSize.computeSafeSize64(); |
| 362 | if (safeSize < 0) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 363 | ERRORF(reporter, "getSafeSize64() negative: %s", |
Matt Sarett | d9836f4 | 2017-04-05 15:41:53 -0400 | [diff] [blame] | 364 | color_type_name(tstSafeSize.colorType())); |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 365 | } |
| 366 | bool sizeFail = false; |
| 367 | // Compare against hand-computed values. |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 368 | switch (gPairs[i].fColorType) { |
| 369 | case kUnknown_SkColorType: |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 370 | break; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 371 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 372 | case kAlpha_8_SkColorType: |
| 373 | case kIndex_8_SkColorType: |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 374 | if (safeSize != 0x2386F26FC10000LL) { |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 375 | sizeFail = true; |
reed@google.com | 01c41a5 | 2013-12-20 14:24:21 +0000 | [diff] [blame] | 376 | } |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 377 | break; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 378 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 379 | case kRGB_565_SkColorType: |
| 380 | case kARGB_4444_SkColorType: |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 381 | if (safeSize != 0x470DE4DF820000LL) { |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 382 | sizeFail = true; |
reed@google.com | 01c41a5 | 2013-12-20 14:24:21 +0000 | [diff] [blame] | 383 | } |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 384 | break; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 385 | |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 386 | case kN32_SkColorType: |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 387 | if (safeSize != 0x8E1BC9BF040000LL) { |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 388 | sizeFail = true; |
reed@google.com | 01c41a5 | 2013-12-20 14:24:21 +0000 | [diff] [blame] | 389 | } |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 390 | break; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 391 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 392 | default: |
| 393 | break; |
| 394 | } |
| 395 | if (sizeFail) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 396 | ERRORF(reporter, "computeSafeSize64() wrong size: %s", |
Matt Sarett | d9836f4 | 2017-04-05 15:41:53 -0400 | [diff] [blame] | 397 | color_type_name(tstSafeSize.colorType())); |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 398 | } |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 399 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 400 | int subW = 2; |
| 401 | int subH = 2; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 402 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 403 | // Create bitmap to act as source for copies and subsets. |
| 404 | SkBitmap src, subset; |
Mike Reed | 6b3155c | 2017-04-03 14:41:44 -0400 | [diff] [blame] | 405 | sk_sp<SkColorTable> ct; |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 406 | if (kIndex_8_SkColorType == src.colorType()) { |
reed | c5e15a1 | 2014-09-29 12:10:27 -0700 | [diff] [blame] | 407 | ct = init_ctable(); |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 408 | } |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 409 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 410 | int localSubW; |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 411 | if (isExtracted[copyCase]) { // A larger image to extract from. |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 412 | localSubW = 2 * subW + 1; |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 413 | } else { // Tests expect a 2x2 bitmap, so make smaller. |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 414 | localSubW = subW; |
| 415 | } |
| 416 | // could fail if we pass kIndex_8 for the colortype |
| 417 | if (src.tryAllocPixels(SkImageInfo::Make(localSubW, subH, gPairs[i].fColorType, |
| 418 | kPremul_SkAlphaType))) { |
| 419 | // failure is fine, as we will notice later on |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 420 | } |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 421 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 422 | // Either copy src or extract into 'subset', which is used |
| 423 | // for subsequent calls to copyPixelsTo/From. |
| 424 | bool srcReady = false; |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 425 | // Test relies on older behavior that extractSubset will fail on |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 426 | // kUnknown_SkColorType |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 427 | if (kUnknown_SkColorType != src.colorType() && |
| 428 | isExtracted[copyCase]) { |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 429 | // The extractedSubset() test case allows us to test copy- |
| 430 | // ing when src and dst mave possibly different strides. |
| 431 | SkIRect r; |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 432 | r.set(1, 0, 1 + subW, subH); // 2x2 extracted bitmap |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 433 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 434 | srcReady = src.extractSubset(&subset, r); |
| 435 | } else { |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 436 | srcReady = src.copyTo(&subset); |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 437 | } |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 438 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 439 | // Not all configurations will generate a valid 'subset'. |
| 440 | if (srcReady) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 441 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 442 | // Allocate our target buffer 'buf' for all copies. |
| 443 | // To simplify verifying correctness of copies attach |
| 444 | // buf to a SkBitmap, but copies are done using the |
| 445 | // raw buffer pointer. |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 446 | const size_t bufSize = subH * |
commit-bot@chromium.org | 8ef51b9 | 2014-03-05 13:43:15 +0000 | [diff] [blame] | 447 | SkColorTypeMinRowBytes(src.colorType(), subW) * 2; |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 448 | SkAutoTMalloc<uint8_t> autoBuf (bufSize); |
| 449 | uint8_t* buf = autoBuf.get(); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 450 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 451 | SkBitmap bufBm; // Attach buf to this bitmap. |
| 452 | bool successExpected; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 453 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 454 | // Set up values for each pixel being copied. |
| 455 | Coordinates coords(subW * subH); |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 456 | for (int x = 0; x < subW; ++x) |
| 457 | for (int y = 0; y < subH; ++y) |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 458 | { |
| 459 | int index = y * subW + x; |
| 460 | SkASSERT(index < coords.length); |
| 461 | coords[index]->fX = x; |
| 462 | coords[index]->fY = y; |
| 463 | } |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 464 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 465 | writeCoordPixels(subset, coords); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 466 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 467 | // Test #1 //////////////////////////////////////////// |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 468 | |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 469 | const SkImageInfo info = SkImageInfo::Make(subW, subH, |
| 470 | gPairs[i].fColorType, |
| 471 | kPremul_SkAlphaType); |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 472 | // Before/after comparisons easier if we attach buf |
| 473 | // to an appropriately configured SkBitmap. |
| 474 | memset(buf, 0xFF, bufSize); |
| 475 | // Config with stride greater than src but that fits in buf. |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 476 | bufBm.installPixels(info, buf, info.minRowBytes() * 2); |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 477 | successExpected = false; |
| 478 | // Then attempt to copy with a stride that is too large |
| 479 | // to fit in the buffer. |
| 480 | REPORTER_ASSERT(reporter, |
| 481 | subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes() * 3) |
| 482 | == successExpected); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 483 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 484 | if (successExpected) |
| 485 | reportCopyVerification(subset, bufBm, coords, |
| 486 | "copyPixelsTo(buf, bufSize, 1.5*maxRowBytes)", |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 487 | reporter); |
| 488 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 489 | // Test #2 //////////////////////////////////////////// |
| 490 | // This test should always succeed, but in the case |
| 491 | // of extracted bitmaps only because we handle the |
| 492 | // issue of getSafeSize(). Without getSafeSize() |
| 493 | // buffer overrun/read would occur. |
| 494 | memset(buf, 0xFF, bufSize); |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 495 | bufBm.installPixels(info, buf, subset.rowBytes()); |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 496 | successExpected = subset.getSafeSize() <= bufSize; |
| 497 | REPORTER_ASSERT(reporter, |
| 498 | subset.copyPixelsTo(buf, bufSize) == |
| 499 | successExpected); |
| 500 | if (successExpected) |
| 501 | reportCopyVerification(subset, bufBm, coords, |
| 502 | "copyPixelsTo(buf, bufSize)", reporter); |
| 503 | |
| 504 | // Test #3 //////////////////////////////////////////// |
| 505 | // Copy with different stride between src and dst. |
| 506 | memset(buf, 0xFF, bufSize); |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 507 | bufBm.installPixels(info, buf, subset.rowBytes()+1); |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 508 | successExpected = true; // Should always work. |
| 509 | REPORTER_ASSERT(reporter, |
| 510 | subset.copyPixelsTo(buf, bufSize, |
| 511 | subset.rowBytes()+1) == successExpected); |
| 512 | if (successExpected) |
| 513 | reportCopyVerification(subset, bufBm, coords, |
| 514 | "copyPixelsTo(buf, bufSize, rowBytes+1)", reporter); |
| 515 | |
| 516 | // Test #4 //////////////////////////////////////////// |
| 517 | // Test copy with stride too small. |
| 518 | memset(buf, 0xFF, bufSize); |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 519 | bufBm.installPixels(info, buf, info.minRowBytes()); |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 520 | successExpected = false; |
| 521 | // Request copy with stride too small. |
| 522 | REPORTER_ASSERT(reporter, |
| 523 | subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes()-1) |
| 524 | == successExpected); |
| 525 | if (successExpected) |
| 526 | reportCopyVerification(subset, bufBm, coords, |
| 527 | "copyPixelsTo(buf, bufSize, rowBytes()-1)", reporter); |
| 528 | |
| 529 | #if 0 // copyPixelsFrom is gone |
| 530 | // Test #5 //////////////////////////////////////////// |
| 531 | // Tests the case where the source stride is too small |
| 532 | // for the source configuration. |
| 533 | memset(buf, 0xFF, bufSize); |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 534 | bufBm.installPixels(info, buf, info.minRowBytes()); |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 535 | writeCoordPixels(bufBm, coords); |
| 536 | REPORTER_ASSERT(reporter, |
| 537 | subset.copyPixelsFrom(buf, bufSize, 1) == false); |
| 538 | |
| 539 | // Test #6 /////////////////////////////////////////// |
| 540 | // Tests basic copy from an external buffer to the bitmap. |
| 541 | // If the bitmap is "extracted", this also tests the case |
| 542 | // where the source stride is different from the dest. |
| 543 | // stride. |
| 544 | // We've made the buffer large enough to always succeed. |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 545 | bufBm.installPixels(info, buf, info.minRowBytes()); |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 546 | writeCoordPixels(bufBm, coords); |
| 547 | REPORTER_ASSERT(reporter, |
| 548 | subset.copyPixelsFrom(buf, bufSize, bufBm.rowBytes()) == |
| 549 | true); |
| 550 | reportCopyVerification(bufBm, subset, coords, |
| 551 | "copyPixelsFrom(buf, bufSize)", |
| 552 | reporter); |
| 553 | |
| 554 | // Test #7 //////////////////////////////////////////// |
| 555 | // Tests the case where the source buffer is too small |
| 556 | // for the transfer. |
| 557 | REPORTER_ASSERT(reporter, |
| 558 | subset.copyPixelsFrom(buf, 1, subset.rowBytes()) == |
| 559 | false); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 560 | |
reed@google.com | ab77aaf | 2011-11-01 16:03:35 +0000 | [diff] [blame] | 561 | #endif |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 562 | } |
| 563 | } // for (size_t copyCase ... |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 564 | } |
| 565 | } |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 566 | |
| 567 | #include "SkColorPriv.h" |
| 568 | #include "SkUtils.h" |
| 569 | |
| 570 | /** |
| 571 | * Construct 4x4 pixels where we can look at a color and determine where it should be in the grid. |
| 572 | * alpha = 0xFF, blue = 0x80, red = x, green = y |
| 573 | */ |
| 574 | static void fill_4x4_pixels(SkPMColor colors[16]) { |
| 575 | for (int y = 0; y < 4; ++y) { |
| 576 | for (int x = 0; x < 4; ++x) { |
| 577 | colors[y*4+x] = SkPackARGB32(0xFF, x, y, 0x80); |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | static bool check_4x4_pixel(SkPMColor color, unsigned x, unsigned y) { |
| 583 | SkASSERT(x < 4 && y < 4); |
| 584 | return 0xFF == SkGetPackedA32(color) && |
| 585 | x == SkGetPackedR32(color) && |
| 586 | y == SkGetPackedG32(color) && |
| 587 | 0x80 == SkGetPackedB32(color); |
| 588 | } |
| 589 | |
| 590 | /** |
| 591 | * Fill with all zeros, which will never match any value from fill_4x4_pixels |
| 592 | */ |
| 593 | static void clear_4x4_pixels(SkPMColor colors[16]) { |
| 594 | sk_memset32(colors, 0, 16); |
| 595 | } |
| 596 | |
| 597 | // Much of readPixels is exercised by copyTo testing, since readPixels is the backend for that |
| 598 | // method. Here we explicitly test subset copies. |
| 599 | // |
| 600 | DEF_TEST(BitmapReadPixels, reporter) { |
| 601 | const int W = 4; |
| 602 | const int H = 4; |
| 603 | const size_t rowBytes = W * sizeof(SkPMColor); |
| 604 | const SkImageInfo srcInfo = SkImageInfo::MakeN32Premul(W, H); |
| 605 | SkPMColor srcPixels[16]; |
| 606 | fill_4x4_pixels(srcPixels); |
| 607 | SkBitmap srcBM; |
| 608 | srcBM.installPixels(srcInfo, srcPixels, rowBytes); |
| 609 | |
| 610 | SkImageInfo dstInfo = SkImageInfo::MakeN32Premul(W, H); |
| 611 | SkPMColor dstPixels[16]; |
| 612 | |
| 613 | const struct { |
| 614 | bool fExpectedSuccess; |
| 615 | SkIPoint fRequestedSrcLoc; |
| 616 | SkISize fRequestedDstSize; |
| 617 | // If fExpectedSuccess, check these, otherwise ignore |
| 618 | SkIPoint fExpectedDstLoc; |
| 619 | SkIRect fExpectedSrcR; |
| 620 | } gRec[] = { |
| 621 | { true, { 0, 0 }, { 4, 4 }, { 0, 0 }, { 0, 0, 4, 4 } }, |
| 622 | { true, { 1, 1 }, { 2, 2 }, { 0, 0 }, { 1, 1, 3, 3 } }, |
| 623 | { true, { 2, 2 }, { 4, 4 }, { 0, 0 }, { 2, 2, 4, 4 } }, |
| 624 | { true, {-1,-1 }, { 2, 2 }, { 1, 1 }, { 0, 0, 1, 1 } }, |
| 625 | { false, {-1,-1 }, { 1, 1 }, { 0, 0 }, { 0, 0, 0, 0 } }, |
| 626 | }; |
| 627 | |
| 628 | for (size_t i = 0; i < SK_ARRAY_COUNT(gRec); ++i) { |
| 629 | clear_4x4_pixels(dstPixels); |
| 630 | |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 631 | dstInfo = dstInfo.makeWH(gRec[i].fRequestedDstSize.width(), |
| 632 | gRec[i].fRequestedDstSize.height()); |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 633 | bool success = srcBM.readPixels(dstInfo, dstPixels, rowBytes, |
| 634 | gRec[i].fRequestedSrcLoc.x(), gRec[i].fRequestedSrcLoc.y()); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 635 | |
reed | b184f7f | 2014-07-13 04:32:32 -0700 | [diff] [blame] | 636 | REPORTER_ASSERT(reporter, gRec[i].fExpectedSuccess == success); |
| 637 | if (success) { |
| 638 | const SkIRect srcR = gRec[i].fExpectedSrcR; |
| 639 | const int dstX = gRec[i].fExpectedDstLoc.x(); |
| 640 | const int dstY = gRec[i].fExpectedDstLoc.y(); |
| 641 | // Walk the dst pixels, and check if we got what we expected |
| 642 | for (int y = 0; y < H; ++y) { |
| 643 | for (int x = 0; x < W; ++x) { |
| 644 | SkPMColor dstC = dstPixels[y*4+x]; |
| 645 | // get into src coordinates |
| 646 | int sx = x - dstX + srcR.x(); |
| 647 | int sy = y - dstY + srcR.y(); |
| 648 | if (srcR.contains(sx, sy)) { |
| 649 | REPORTER_ASSERT(reporter, check_4x4_pixel(dstC, sx, sy)); |
| 650 | } else { |
| 651 | REPORTER_ASSERT(reporter, 0 == dstC); |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
Matt Sarett | d9836f4 | 2017-04-05 15:41:53 -0400 | [diff] [blame] | 659 | DEF_TEST(BitmapCopy_ColorSpaceMatch, r) { |
| 660 | // We should support matching color spaces, even if they are parametric. |
| 661 | SkColorSpaceTransferFn fn; |
Matt Sarett | 1a26ba9 | 2017-04-05 17:31:07 -0400 | [diff] [blame] | 662 | fn.fA = 1.f; fn.fB = 0.f; fn.fC = 0.f; fn.fD = 0.f; fn.fE = 0.f; fn.fF = 0.f; fn.fG = 1.8f; |
Matt Sarett | d9836f4 | 2017-04-05 15:41:53 -0400 | [diff] [blame] | 663 | sk_sp<SkColorSpace> cs = SkColorSpace::MakeRGB(fn, SkColorSpace::kRec2020_Gamut); |
| 664 | |
| 665 | SkImageInfo info = SkImageInfo::MakeN32Premul(1, 1, cs); |
| 666 | SkBitmap bitmap; |
| 667 | bitmap.allocPixels(info); |
| 668 | bitmap.eraseColor(0); |
| 669 | |
| 670 | SkBitmap copy; |
Mike Reed | 42ce38f | 2017-04-06 14:03:25 -0400 | [diff] [blame] | 671 | bool success = bitmap.copyTo(©, kN32_SkColorType); |
Matt Sarett | d9836f4 | 2017-04-05 15:41:53 -0400 | [diff] [blame] | 672 | REPORTER_ASSERT(r, success); |
| 673 | REPORTER_ASSERT(r, cs.get() == copy.colorSpace()); |
| 674 | } |