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