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