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