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