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 | */ |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 7 | #include "SkBitmap.h" |
reed@android.com | 311c82d | 2009-05-05 23:13:23 +0000 | [diff] [blame] | 8 | #include "SkRect.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame^] | 9 | #include "Test.h" |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 10 | |
| 11 | static const char* boolStr(bool value) { |
| 12 | return value ? "true" : "false"; |
| 13 | } |
| 14 | |
| 15 | // these are in the same order as the SkBitmap::Config enum |
| 16 | static const char* gConfigName[] = { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 17 | "None", "A8", "Index8", "565", "4444", "8888" |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 18 | }; |
| 19 | |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 20 | static void report_opaqueness(skiatest::Reporter* reporter, const SkBitmap& src, |
| 21 | const SkBitmap& dst) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 22 | ERRORF(reporter, "src %s opaque:%d, dst %s opaque:%d", |
| 23 | gConfigName[src.config()], src.isOpaque(), |
| 24 | gConfigName[dst.config()], dst.isOpaque()); |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | static bool canHaveAlpha(SkBitmap::Config config) { |
| 28 | return config != SkBitmap::kRGB_565_Config; |
| 29 | } |
| 30 | |
| 31 | // copyTo() should preserve isOpaque when it makes sense |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 32 | static void test_isOpaque(skiatest::Reporter* reporter, |
| 33 | const SkBitmap& srcOpaque, const SkBitmap& srcPremul, |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 34 | SkBitmap::Config dstConfig) { |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 35 | SkBitmap dst; |
| 36 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 37 | if (canHaveAlpha(srcPremul.config()) && canHaveAlpha(dstConfig)) { |
| 38 | REPORTER_ASSERT(reporter, srcPremul.copyTo(&dst, dstConfig)); |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 39 | REPORTER_ASSERT(reporter, dst.config() == dstConfig); |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 40 | if (srcPremul.isOpaque() != dst.isOpaque()) { |
| 41 | report_opaqueness(reporter, srcPremul, dst); |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 45 | REPORTER_ASSERT(reporter, srcOpaque.copyTo(&dst, dstConfig)); |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 46 | REPORTER_ASSERT(reporter, dst.config() == dstConfig); |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 47 | if (srcOpaque.isOpaque() != dst.isOpaque()) { |
| 48 | report_opaqueness(reporter, srcOpaque, dst); |
reed@android.com | cafc9f9 | 2009-08-22 03:44:57 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 52 | static void init_src(const SkBitmap& bitmap) { |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 53 | SkAutoLockPixels lock(bitmap); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 54 | if (bitmap.getPixels()) { |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 55 | if (bitmap.getColorTable()) { |
reed@google.com | 9ce6e75 | 2011-01-10 14:04:07 +0000 | [diff] [blame] | 56 | sk_bzero(bitmap.getPixels(), bitmap.getSize()); |
| 57 | } else { |
| 58 | bitmap.eraseColor(SK_ColorWHITE); |
| 59 | } |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 63 | static SkColorTable* init_ctable(SkAlphaType alphaType) { |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 64 | static const SkColor colors[] = { |
| 65 | SK_ColorBLACK, SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE |
| 66 | }; |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 67 | return new SkColorTable(colors, SK_ARRAY_COUNT(colors), alphaType); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | struct Pair { |
| 71 | SkBitmap::Config fConfig; |
| 72 | const char* fValid; |
| 73 | }; |
| 74 | |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 75 | // Utility functions for copyPixelsTo()/copyPixelsFrom() tests. |
| 76 | // getPixel() |
| 77 | // setPixel() |
| 78 | // getSkConfigName() |
| 79 | // struct Coordinates |
| 80 | // reportCopyVerification() |
| 81 | // writeCoordPixels() |
| 82 | |
| 83 | // Utility function to read the value of a given pixel in bm. All |
| 84 | // values converted to uint32_t for simplification of comparisons. |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 85 | static uint32_t getPixel(int x, int y, const SkBitmap& bm) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 86 | uint32_t val = 0; |
| 87 | uint16_t val16; |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 88 | uint8_t val8; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 89 | SkAutoLockPixels lock(bm); |
| 90 | const void* rawAddr = bm.getAddr(x,y); |
| 91 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 92 | switch (bm.config()) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 93 | case SkBitmap::kARGB_8888_Config: |
| 94 | memcpy(&val, rawAddr, sizeof(uint32_t)); |
| 95 | break; |
| 96 | case SkBitmap::kARGB_4444_Config: |
| 97 | case SkBitmap::kRGB_565_Config: |
| 98 | memcpy(&val16, rawAddr, sizeof(uint16_t)); |
| 99 | val = val16; |
| 100 | break; |
| 101 | case SkBitmap::kA8_Config: |
| 102 | case SkBitmap::kIndex8_Config: |
| 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 | |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 121 | switch (bm.config()) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 122 | case SkBitmap::kARGB_8888_Config: |
| 123 | memcpy(rawAddr, &val, sizeof(uint32_t)); |
| 124 | break; |
| 125 | case SkBitmap::kARGB_4444_Config: |
| 126 | case SkBitmap::kRGB_565_Config: |
| 127 | val16 = val & 0xFFFF; |
| 128 | memcpy(rawAddr, &val16, sizeof(uint16_t)); |
| 129 | break; |
| 130 | case SkBitmap::kA8_Config: |
| 131 | case SkBitmap::kIndex8_Config: |
| 132 | val8 = val & 0xFF; |
| 133 | memcpy(rawAddr, &val8, sizeof(uint8_t)); |
| 134 | break; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 135 | default: |
| 136 | // Ignore. |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | // Utility to return string containing name of each format, to |
| 142 | // simplify diagnostic output. |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 143 | static const char* getSkConfigName(const SkBitmap& bm) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 144 | switch (bm.config()) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 145 | case SkBitmap::kNo_Config: return "SkBitmap::kNo_Config"; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 146 | case SkBitmap::kA8_Config: return "SkBitmap::kA8_Config"; |
| 147 | case SkBitmap::kIndex8_Config: return "SkBitmap::kIndex8_Config"; |
| 148 | case SkBitmap::kRGB_565_Config: return "SkBitmap::kRGB_565_Config"; |
| 149 | case SkBitmap::kARGB_4444_Config: return "SkBitmap::kARGB_4444_Config"; |
| 150 | case SkBitmap::kARGB_8888_Config: return "SkBitmap::kARGB_8888_Config"; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 151 | default: return "Unknown SkBitmap configuration."; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | // Helper struct to contain pixel locations, while avoiding need for STL. |
| 156 | struct Coordinates { |
| 157 | |
| 158 | const int length; |
| 159 | SkIPoint* const data; |
| 160 | |
| 161 | explicit Coordinates(int _length): length(_length) |
| 162 | , data(new SkIPoint[length]) { } |
| 163 | |
| 164 | ~Coordinates(){ |
| 165 | delete [] data; |
| 166 | } |
| 167 | |
| 168 | SkIPoint* operator[](int i) const { |
| 169 | // Use with care, no bounds checking. |
| 170 | return data + i; |
| 171 | } |
| 172 | }; |
| 173 | |
| 174 | // A function to verify that two bitmaps contain the same pixel values |
| 175 | // at all coordinates indicated by coords. Simplifies verification of |
| 176 | // copied bitmaps. |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 177 | static void reportCopyVerification(const SkBitmap& bm1, const SkBitmap& bm2, |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 178 | Coordinates& coords, |
| 179 | const char* msg, |
| 180 | skiatest::Reporter* reporter){ |
| 181 | bool success = true; |
| 182 | |
| 183 | // Confirm all pixels in the list match. |
scroggo@google.com | d5764e8 | 2012-08-22 15:00:05 +0000 | [diff] [blame] | 184 | for (int i = 0; i < coords.length; ++i) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 185 | success = success && |
| 186 | (getPixel(coords[i]->fX, coords[i]->fY, bm1) == |
| 187 | getPixel(coords[i]->fX, coords[i]->fY, bm2)); |
scroggo@google.com | d5764e8 | 2012-08-22 15:00:05 +0000 | [diff] [blame] | 188 | } |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 189 | |
| 190 | if (!success) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 191 | ERRORF(reporter, "%s [config = %s]", msg, getSkConfigName(bm1)); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
| 195 | // Writes unique pixel values at locations specified by coords. |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 196 | static void writeCoordPixels(SkBitmap& bm, const Coordinates& coords) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 197 | for (int i = 0; i < coords.length; ++i) |
| 198 | setPixel(coords[i]->fX, coords[i]->fY, i, bm); |
| 199 | } |
| 200 | |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 201 | static const Pair gPairs[] = { |
| 202 | { SkBitmap::kNo_Config, "0000000" }, |
| 203 | { SkBitmap::kA8_Config, "0101010" }, |
| 204 | { SkBitmap::kIndex8_Config, "0111010" }, |
| 205 | { SkBitmap::kRGB_565_Config, "0101010" }, |
| 206 | { SkBitmap::kARGB_4444_Config, "0101110" }, |
| 207 | { SkBitmap::kARGB_8888_Config, "0101110" }, |
| 208 | }; |
commit-bot@chromium.org | 5c6f1d4 | 2014-01-10 18:28:23 +0000 | [diff] [blame] | 209 | |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 210 | static const int W = 20; |
| 211 | static const int H = 33; |
| 212 | |
| 213 | static void setup_src_bitmaps(SkBitmap* srcOpaque, SkBitmap* srcPremul, |
| 214 | SkBitmap::Config config) { |
| 215 | SkColorTable* ctOpaque = NULL; |
| 216 | SkColorTable* ctPremul = NULL; |
| 217 | |
| 218 | srcOpaque->setConfig(config, W, H, 0, kOpaque_SkAlphaType); |
| 219 | srcPremul->setConfig(config, W, H, 0, kPremul_SkAlphaType); |
| 220 | if (SkBitmap::kIndex8_Config == config) { |
| 221 | ctOpaque = init_ctable(kOpaque_SkAlphaType); |
| 222 | ctPremul = init_ctable(kPremul_SkAlphaType); |
| 223 | } |
| 224 | srcOpaque->allocPixels(ctOpaque); |
| 225 | srcPremul->allocPixels(ctPremul); |
| 226 | SkSafeUnref(ctOpaque); |
| 227 | SkSafeUnref(ctPremul); |
| 228 | init_src(*srcOpaque); |
| 229 | init_src(*srcPremul); |
| 230 | } |
| 231 | |
| 232 | DEF_TEST(BitmapCopy_extractSubset, reporter) { |
| 233 | for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) { |
| 234 | SkBitmap srcOpaque, srcPremul; |
| 235 | setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fConfig); |
| 236 | |
| 237 | SkBitmap bitmap(srcOpaque); |
| 238 | SkBitmap subset; |
| 239 | SkIRect r; |
| 240 | // Extract a subset which has the same width as the original. This |
| 241 | // catches a bug where we cloned the genID incorrectly. |
| 242 | r.set(0, 1, W, 3); |
| 243 | bitmap.setIsVolatile(true); |
| 244 | if (bitmap.extractSubset(&subset, r)) { |
| 245 | REPORTER_ASSERT(reporter, subset.width() == W); |
| 246 | REPORTER_ASSERT(reporter, subset.height() == 2); |
| 247 | REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType()); |
| 248 | REPORTER_ASSERT(reporter, subset.isVolatile() == true); |
| 249 | |
| 250 | // Test copying an extracted subset. |
| 251 | for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) { |
| 252 | SkBitmap copy; |
| 253 | bool success = subset.copyTo(©, gPairs[j].fConfig); |
| 254 | if (!success) { |
| 255 | // Skip checking that success matches fValid, which is redundant |
| 256 | // with the code below. |
| 257 | REPORTER_ASSERT(reporter, gPairs[i].fConfig != gPairs[j].fConfig); |
| 258 | continue; |
| 259 | } |
| 260 | |
| 261 | // When performing a copy of an extracted subset, the gen id should |
| 262 | // change. |
| 263 | REPORTER_ASSERT(reporter, copy.getGenerationID() != subset.getGenerationID()); |
| 264 | |
| 265 | REPORTER_ASSERT(reporter, copy.width() == W); |
| 266 | REPORTER_ASSERT(reporter, copy.height() == 2); |
| 267 | |
| 268 | if (gPairs[i].fConfig == gPairs[j].fConfig) { |
| 269 | SkAutoLockPixels alp0(subset); |
| 270 | SkAutoLockPixels alp1(copy); |
| 271 | // they should both have, or both not-have, a colortable |
| 272 | bool hasCT = subset.getColorTable() != NULL; |
| 273 | REPORTER_ASSERT(reporter, (copy.getColorTable() != NULL) == hasCT); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | bitmap = srcPremul; |
| 279 | bitmap.setIsVolatile(false); |
| 280 | if (bitmap.extractSubset(&subset, r)) { |
| 281 | REPORTER_ASSERT(reporter, subset.alphaType() == bitmap.alphaType()); |
| 282 | REPORTER_ASSERT(reporter, subset.isVolatile() == false); |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | DEF_TEST(BitmapCopy, reporter) { |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 288 | static const bool isExtracted[] = { |
| 289 | false, true |
| 290 | }; |
| 291 | |
commit-bot@chromium.org | 5c6f1d4 | 2014-01-10 18:28:23 +0000 | [diff] [blame] | 292 | for (size_t i = 0; i < SK_ARRAY_COUNT(gPairs); i++) { |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 293 | SkBitmap srcOpaque, srcPremul; |
| 294 | setup_src_bitmaps(&srcOpaque, &srcPremul, gPairs[i].fConfig); |
| 295 | |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 296 | for (size_t j = 0; j < SK_ARRAY_COUNT(gPairs); j++) { |
scroggo@google.com | 5ccae2c | 2014-01-15 16:56:52 +0000 | [diff] [blame] | 297 | SkBitmap dst; |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 298 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 299 | bool success = srcPremul.copyTo(&dst, gPairs[j].fConfig); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 300 | bool expected = gPairs[i].fValid[j] != '0'; |
| 301 | if (success != expected) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 302 | ERRORF(reporter, "SkBitmap::copyTo from %s to %s. expected %s " |
| 303 | "returned %s", gConfigName[i], gConfigName[j], |
| 304 | boolStr(expected), boolStr(success)); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 305 | } |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 306 | |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 307 | bool canSucceed = srcPremul.canCopyTo(gPairs[j].fConfig); |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 308 | if (success != canSucceed) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 309 | ERRORF(reporter, "SkBitmap::copyTo from %s to %s. returned %s " |
| 310 | "canCopyTo %s", gConfigName[i], gConfigName[j], |
| 311 | boolStr(success), boolStr(canSucceed)); |
reed@android.com | fbaa88d | 2009-05-06 17:44:34 +0000 | [diff] [blame] | 312 | } |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 313 | |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 314 | if (success) { |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 315 | REPORTER_ASSERT(reporter, srcPremul.width() == dst.width()); |
| 316 | REPORTER_ASSERT(reporter, srcPremul.height() == dst.height()); |
weita@google.com | f9ab99a | 2009-05-03 18:23:30 +0000 | [diff] [blame] | 317 | REPORTER_ASSERT(reporter, dst.config() == gPairs[j].fConfig); |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 318 | test_isOpaque(reporter, srcOpaque, srcPremul, dst.config()); |
| 319 | if (srcPremul.config() == dst.config()) { |
| 320 | SkAutoLockPixels srcLock(srcPremul); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 321 | SkAutoLockPixels dstLock(dst); |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 322 | REPORTER_ASSERT(reporter, srcPremul.readyToDraw()); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 323 | REPORTER_ASSERT(reporter, dst.readyToDraw()); |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 324 | const char* srcP = (const char*)srcPremul.getAddr(0, 0); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 325 | const char* dstP = (const char*)dst.getAddr(0, 0); |
| 326 | REPORTER_ASSERT(reporter, srcP != dstP); |
| 327 | REPORTER_ASSERT(reporter, !memcmp(srcP, dstP, |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 328 | srcPremul.getSize())); |
| 329 | REPORTER_ASSERT(reporter, srcPremul.getGenerationID() == dst.getGenerationID()); |
scroggo@google.com | d5764e8 | 2012-08-22 15:00:05 +0000 | [diff] [blame] | 330 | } else { |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 331 | REPORTER_ASSERT(reporter, srcPremul.getGenerationID() != dst.getGenerationID()); |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 332 | } |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 333 | } else { |
| 334 | // dst should be unchanged from its initial state |
| 335 | REPORTER_ASSERT(reporter, dst.config() == SkBitmap::kNo_Config); |
| 336 | REPORTER_ASSERT(reporter, dst.width() == 0); |
| 337 | REPORTER_ASSERT(reporter, dst.height() == 0); |
| 338 | } |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 339 | } // for (size_t j = ... |
| 340 | |
| 341 | // Tests for getSafeSize(), getSafeSize64(), copyPixelsTo(), |
| 342 | // copyPixelsFrom(). |
| 343 | // |
| 344 | for (size_t copyCase = 0; copyCase < SK_ARRAY_COUNT(isExtracted); |
| 345 | ++copyCase) { |
| 346 | // Test copying to/from external buffer. |
| 347 | // Note: the tests below have hard-coded values --- |
| 348 | // Please take care if modifying. |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 349 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 350 | // Tests for getSafeSize64(). |
| 351 | // Test with a very large configuration without pixel buffer |
| 352 | // attached. |
| 353 | SkBitmap tstSafeSize; |
| 354 | tstSafeSize.setConfig(gPairs[i].fConfig, 100000000U, |
| 355 | 100000000U); |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 356 | int64_t safeSize = tstSafeSize.computeSafeSize64(); |
| 357 | if (safeSize < 0) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 358 | ERRORF(reporter, "getSafeSize64() negative: %s", |
| 359 | getSkConfigName(tstSafeSize)); |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 360 | } |
| 361 | bool sizeFail = false; |
| 362 | // Compare against hand-computed values. |
| 363 | switch (gPairs[i].fConfig) { |
| 364 | case SkBitmap::kNo_Config: |
| 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 | case SkBitmap::kA8_Config: |
| 368 | case SkBitmap::kIndex8_Config: |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 369 | if (safeSize != 0x2386F26FC10000LL) { |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 370 | sizeFail = true; |
reed@google.com | 01c41a5 | 2013-12-20 14:24:21 +0000 | [diff] [blame] | 371 | } |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 372 | break; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 373 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 374 | case SkBitmap::kRGB_565_Config: |
| 375 | case SkBitmap::kARGB_4444_Config: |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 376 | if (safeSize != 0x470DE4DF820000LL) { |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 377 | sizeFail = true; |
reed@google.com | 01c41a5 | 2013-12-20 14:24:21 +0000 | [diff] [blame] | 378 | } |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 379 | break; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 380 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 381 | case SkBitmap::kARGB_8888_Config: |
reed@google.com | 57212f9 | 2013-12-30 14:40:38 +0000 | [diff] [blame] | 382 | if (safeSize != 0x8E1BC9BF040000LL) { |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 383 | sizeFail = true; |
reed@google.com | 01c41a5 | 2013-12-20 14:24:21 +0000 | [diff] [blame] | 384 | } |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 385 | break; |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 386 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 387 | default: |
| 388 | break; |
| 389 | } |
| 390 | if (sizeFail) { |
halcanary@google.com | a9325fa | 2014-01-10 14:58:10 +0000 | [diff] [blame] | 391 | ERRORF(reporter, "computeSafeSize64() wrong size: %s", |
| 392 | getSkConfigName(tstSafeSize)); |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 393 | } |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 394 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 395 | int subW = 2; |
| 396 | int subH = 2; |
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 | // Create bitmap to act as source for copies and subsets. |
| 399 | SkBitmap src, subset; |
| 400 | SkColorTable* ct = NULL; |
| 401 | if (isExtracted[copyCase]) { // A larger image to extract from. |
| 402 | src.setConfig(gPairs[i].fConfig, 2 * subW + 1, subH); |
| 403 | } else { // Tests expect a 2x2 bitmap, so make smaller. |
| 404 | src.setConfig(gPairs[i].fConfig, subW, subH); |
| 405 | } |
| 406 | if (SkBitmap::kIndex8_Config == src.config()) { |
reed@google.com | 0a6151d | 2013-10-10 14:44:56 +0000 | [diff] [blame] | 407 | ct = init_ctable(kPremul_SkAlphaType); |
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@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 410 | src.allocPixels(ct); |
| 411 | SkSafeUnref(ct); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 412 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 413 | // Either copy src or extract into 'subset', which is used |
| 414 | // for subsequent calls to copyPixelsTo/From. |
| 415 | bool srcReady = false; |
| 416 | if (isExtracted[copyCase]) { |
| 417 | // The extractedSubset() test case allows us to test copy- |
| 418 | // ing when src and dst mave possibly different strides. |
| 419 | SkIRect r; |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 420 | r.set(1, 0, 1 + subW, subH); // 2x2 extracted bitmap |
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 | srcReady = src.extractSubset(&subset, r); |
| 423 | } else { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 424 | srcReady = src.copyTo(&subset, src.config()); |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 425 | } |
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 | // Not all configurations will generate a valid 'subset'. |
| 428 | if (srcReady) { |
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 | // Allocate our target buffer 'buf' for all copies. |
| 431 | // To simplify verifying correctness of copies attach |
| 432 | // buf to a SkBitmap, but copies are done using the |
| 433 | // raw buffer pointer. |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 434 | const size_t bufSize = subH * |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 435 | SkBitmap::ComputeRowBytes(src.config(), subW) * 2; |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 436 | SkAutoMalloc autoBuf (bufSize); |
| 437 | uint8_t* buf = static_cast<uint8_t*>(autoBuf.get()); |
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 | SkBitmap bufBm; // Attach buf to this bitmap. |
| 440 | bool successExpected; |
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 | // Set up values for each pixel being copied. |
| 443 | Coordinates coords(subW * subH); |
robertphillips@google.com | e9cd27d | 2013-10-16 17:48:11 +0000 | [diff] [blame] | 444 | for (int x = 0; x < subW; ++x) |
| 445 | for (int y = 0; y < subH; ++y) |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 446 | { |
| 447 | int index = y * subW + x; |
| 448 | SkASSERT(index < coords.length); |
| 449 | coords[index]->fX = x; |
| 450 | coords[index]->fY = y; |
| 451 | } |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 452 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 453 | writeCoordPixels(subset, coords); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 454 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 455 | // Test #1 //////////////////////////////////////////// |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 456 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 457 | // Before/after comparisons easier if we attach buf |
| 458 | // to an appropriately configured SkBitmap. |
| 459 | memset(buf, 0xFF, bufSize); |
| 460 | // Config with stride greater than src but that fits in buf. |
| 461 | bufBm.setConfig(gPairs[i].fConfig, subW, subH, |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 462 | SkBitmap::ComputeRowBytes(subset.config(), subW) * 2); |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 463 | bufBm.setPixels(buf); |
| 464 | successExpected = false; |
| 465 | // Then attempt to copy with a stride that is too large |
| 466 | // to fit in the buffer. |
| 467 | REPORTER_ASSERT(reporter, |
| 468 | subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes() * 3) |
| 469 | == successExpected); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 470 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 471 | if (successExpected) |
| 472 | reportCopyVerification(subset, bufBm, coords, |
| 473 | "copyPixelsTo(buf, bufSize, 1.5*maxRowBytes)", |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 474 | reporter); |
| 475 | |
reed@google.com | 2cb1480 | 2013-06-26 14:35:02 +0000 | [diff] [blame] | 476 | // Test #2 //////////////////////////////////////////// |
| 477 | // This test should always succeed, but in the case |
| 478 | // of extracted bitmaps only because we handle the |
| 479 | // issue of getSafeSize(). Without getSafeSize() |
| 480 | // buffer overrun/read would occur. |
| 481 | memset(buf, 0xFF, bufSize); |
| 482 | bufBm.setConfig(gPairs[i].fConfig, subW, subH, |
| 483 | subset.rowBytes()); |
| 484 | bufBm.setPixels(buf); |
| 485 | successExpected = subset.getSafeSize() <= bufSize; |
| 486 | REPORTER_ASSERT(reporter, |
| 487 | subset.copyPixelsTo(buf, bufSize) == |
| 488 | successExpected); |
| 489 | if (successExpected) |
| 490 | reportCopyVerification(subset, bufBm, coords, |
| 491 | "copyPixelsTo(buf, bufSize)", reporter); |
| 492 | |
| 493 | // Test #3 //////////////////////////////////////////// |
| 494 | // Copy with different stride between src and dst. |
| 495 | memset(buf, 0xFF, bufSize); |
| 496 | bufBm.setConfig(gPairs[i].fConfig, subW, subH, |
| 497 | subset.rowBytes()+1); |
| 498 | bufBm.setPixels(buf); |
| 499 | successExpected = true; // Should always work. |
| 500 | REPORTER_ASSERT(reporter, |
| 501 | subset.copyPixelsTo(buf, bufSize, |
| 502 | subset.rowBytes()+1) == successExpected); |
| 503 | if (successExpected) |
| 504 | reportCopyVerification(subset, bufBm, coords, |
| 505 | "copyPixelsTo(buf, bufSize, rowBytes+1)", reporter); |
| 506 | |
| 507 | // Test #4 //////////////////////////////////////////// |
| 508 | // Test copy with stride too small. |
| 509 | memset(buf, 0xFF, bufSize); |
| 510 | bufBm.setConfig(gPairs[i].fConfig, subW, subH); |
| 511 | bufBm.setPixels(buf); |
| 512 | successExpected = false; |
| 513 | // Request copy with stride too small. |
| 514 | REPORTER_ASSERT(reporter, |
| 515 | subset.copyPixelsTo(buf, bufSize, bufBm.rowBytes()-1) |
| 516 | == successExpected); |
| 517 | if (successExpected) |
| 518 | reportCopyVerification(subset, bufBm, coords, |
| 519 | "copyPixelsTo(buf, bufSize, rowBytes()-1)", reporter); |
| 520 | |
| 521 | #if 0 // copyPixelsFrom is gone |
| 522 | // Test #5 //////////////////////////////////////////// |
| 523 | // Tests the case where the source stride is too small |
| 524 | // for the source configuration. |
| 525 | memset(buf, 0xFF, bufSize); |
| 526 | bufBm.setConfig(gPairs[i].fConfig, subW, subH); |
| 527 | bufBm.setPixels(buf); |
| 528 | writeCoordPixels(bufBm, coords); |
| 529 | REPORTER_ASSERT(reporter, |
| 530 | subset.copyPixelsFrom(buf, bufSize, 1) == false); |
| 531 | |
| 532 | // Test #6 /////////////////////////////////////////// |
| 533 | // Tests basic copy from an external buffer to the bitmap. |
| 534 | // If the bitmap is "extracted", this also tests the case |
| 535 | // where the source stride is different from the dest. |
| 536 | // stride. |
| 537 | // We've made the buffer large enough to always succeed. |
| 538 | bufBm.setConfig(gPairs[i].fConfig, subW, subH); |
| 539 | bufBm.setPixels(buf); |
| 540 | writeCoordPixels(bufBm, coords); |
| 541 | REPORTER_ASSERT(reporter, |
| 542 | subset.copyPixelsFrom(buf, bufSize, bufBm.rowBytes()) == |
| 543 | true); |
| 544 | reportCopyVerification(bufBm, subset, coords, |
| 545 | "copyPixelsFrom(buf, bufSize)", |
| 546 | reporter); |
| 547 | |
| 548 | // Test #7 //////////////////////////////////////////// |
| 549 | // Tests the case where the source buffer is too small |
| 550 | // for the transfer. |
| 551 | REPORTER_ASSERT(reporter, |
| 552 | subset.copyPixelsFrom(buf, 1, subset.rowBytes()) == |
| 553 | false); |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 554 | |
reed@google.com | ab77aaf | 2011-11-01 16:03:35 +0000 | [diff] [blame] | 555 | #endif |
wjmaclean@chromium.org | 86bff1f | 2010-11-16 20:22:41 +0000 | [diff] [blame] | 556 | } |
| 557 | } // for (size_t copyCase ... |
reed@android.com | 4226396 | 2009-05-01 04:00:01 +0000 | [diff] [blame] | 558 | } |
| 559 | } |