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