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