commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "SkBitmap.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkData.h" |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 11 | #include "SkDecodingImageGenerator.h" |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 12 | #include "SkDiscardableMemoryPool.h" |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 13 | #include "SkImageDecoder.h" |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 14 | #include "SkCachingPixelRef.h" |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 15 | #include "SkScaledImageCache.h" |
| 16 | #include "SkStream.h" |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 17 | #include "SkUtils.h" |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 18 | |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 19 | #include "Test.h" |
| 20 | |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 21 | /** |
| 22 | * Fill this bitmap with some color. |
| 23 | */ |
| 24 | static void make_test_image(SkBitmap* bm) { |
| 25 | static const int W = 50, H = 50; |
| 26 | static const SkBitmap::Config config = SkBitmap::kARGB_8888_Config; |
| 27 | bm->setConfig(config, W, H); |
| 28 | bm->allocPixels(); |
| 29 | bm->eraseColor(SK_ColorBLACK); |
| 30 | SkCanvas canvas(*bm); |
| 31 | SkPaint paint; |
| 32 | paint.setColor(SK_ColorBLUE); |
| 33 | canvas.drawRectCoords(0, 0, SkIntToScalar(W/2), |
| 34 | SkIntToScalar(H/2), paint); |
| 35 | paint.setColor(SK_ColorWHITE); |
| 36 | canvas.drawRectCoords(SkIntToScalar(W/2), SkIntToScalar(H/2), |
| 37 | SkIntToScalar(W), SkIntToScalar(H), paint); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * encode this bitmap into some data via SkImageEncoder |
| 42 | */ |
| 43 | static SkData* create_data_from_bitmap(const SkBitmap& bm, |
| 44 | SkImageEncoder::Type type) { |
| 45 | SkDynamicMemoryWStream stream; |
| 46 | if (SkImageEncoder::EncodeStream(&stream, bm, type, 100)) { |
| 47 | return stream.copyToData(); |
| 48 | } |
| 49 | return NULL; |
| 50 | } |
| 51 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 52 | //////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 53 | |
| 54 | static void compare_bitmaps(skiatest::Reporter* reporter, |
| 55 | const SkBitmap& b1, const SkBitmap& b2, |
| 56 | bool pixelPerfect = true) { |
| 57 | REPORTER_ASSERT(reporter, b1.empty() == b2.empty()); |
| 58 | REPORTER_ASSERT(reporter, b1.width() == b2.width()); |
| 59 | REPORTER_ASSERT(reporter, b1.height() == b2.height()); |
| 60 | REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull()); |
| 61 | SkAutoLockPixels autoLockPixels1(b1); |
| 62 | SkAutoLockPixels autoLockPixels2(b2); |
| 63 | REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull()); |
| 64 | if (b1.isNull() || b1.empty()) { |
| 65 | return; |
| 66 | } |
| 67 | REPORTER_ASSERT(reporter, NULL != b1.getPixels()); |
| 68 | REPORTER_ASSERT(reporter, NULL != b2.getPixels()); |
| 69 | if ((!(b1.getPixels())) || (!(b2.getPixels()))) { |
| 70 | return; |
| 71 | } |
| 72 | if ((b1.width() != b2.width()) || |
| 73 | (b1.height() != b2.height())) { |
| 74 | return; |
| 75 | } |
| 76 | if (!pixelPerfect) { |
| 77 | return; |
| 78 | } |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 79 | |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 80 | int pixelErrors = 0; |
| 81 | for (int y = 0; y < b2.height(); ++y) { |
| 82 | for (int x = 0; x < b2.width(); ++x) { |
| 83 | if (b1.getColor(x, y) != b2.getColor(x, y)) { |
| 84 | ++pixelErrors; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | REPORTER_ASSERT(reporter, 0 == pixelErrors); |
| 89 | } |
| 90 | |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 91 | typedef bool (*InstallEncoded)(SkData* encoded, SkBitmap* dst); |
| 92 | |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 93 | /** |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 94 | This function tests three differently encoded images against the |
| 95 | original bitmap */ |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 96 | static void test_three_encodings(skiatest::Reporter* reporter, |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 97 | InstallEncoded install) { |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 98 | SkBitmap original; |
| 99 | make_test_image(&original); |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 100 | REPORTER_ASSERT(reporter, !original.empty()); |
| 101 | REPORTER_ASSERT(reporter, !original.isNull()); |
| 102 | if (original.empty() || original.isNull()) { |
| 103 | return; |
| 104 | } |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 105 | static const SkImageEncoder::Type types[] = { |
| 106 | SkImageEncoder::kPNG_Type, |
| 107 | SkImageEncoder::kJPEG_Type, |
| 108 | SkImageEncoder::kWEBP_Type |
| 109 | }; |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 110 | for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) { |
| 111 | SkImageEncoder::Type type = types[i]; |
| 112 | SkAutoDataUnref encoded(create_data_from_bitmap(original, type)); |
| 113 | REPORTER_ASSERT(reporter, encoded.get() != NULL); |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 114 | if (NULL == encoded.get()) { |
| 115 | continue; |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 116 | } |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 117 | SkBitmap lazy; |
| 118 | bool installSuccess = install(encoded.get(), &lazy); |
| 119 | REPORTER_ASSERT(reporter, installSuccess); |
| 120 | if (!installSuccess) { |
| 121 | continue; |
| 122 | } |
| 123 | REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 124 | { |
| 125 | SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
| 126 | REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); |
| 127 | if (NULL == lazy.getPixels()) { |
| 128 | continue; |
| 129 | } |
| 130 | } |
| 131 | // pixels should be gone! |
| 132 | REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 133 | { |
| 134 | SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
| 135 | REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); |
| 136 | if (NULL == lazy.getPixels()) { |
| 137 | continue; |
| 138 | } |
| 139 | } |
| 140 | bool comparePixels = (SkImageEncoder::kPNG_Type == type); |
| 141 | compare_bitmaps(reporter, original, lazy, comparePixels); |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 144 | |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 145 | //////////////////////////////////////////////////////////////////////////////// |
halcanary@google.com | dcfebfa | 2013-12-05 14:18:07 +0000 | [diff] [blame] | 146 | static bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) { |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 147 | return SkCachingPixelRef::Install( |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 148 | SkDecodingImageGenerator::Create( |
| 149 | encoded, SkDecodingImageGenerator::Options()), dst); |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 150 | } |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 151 | static bool install_skDiscardablePixelRef(SkData* encoded, SkBitmap* dst) { |
| 152 | // Use system-default discardable memory. |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 153 | return SkInstallDiscardablePixelRef( |
| 154 | SkDecodingImageGenerator::Create( |
| 155 | encoded, SkDecodingImageGenerator::Options()), dst, NULL); |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 156 | } |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 157 | |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 158 | //////////////////////////////////////////////////////////////////////////////// |
| 159 | /** |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 160 | * This checks to see that a SkCachingPixelRef and a |
| 161 | * SkDiscardablePixelRef works as advertised with a |
| 162 | * SkDecodingImageGenerator. |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 163 | */ |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 164 | DEF_TEST(DecodingImageGenerator, reporter) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 165 | test_three_encodings(reporter, install_skCachingPixelRef); |
| 166 | test_three_encodings(reporter, install_skDiscardablePixelRef); |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 167 | } |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 168 | |
| 169 | //////////////////////////////////////////////////////////////////////////////// |
| 170 | namespace { |
| 171 | class TestImageGenerator : public SkImageGenerator { |
| 172 | public: |
| 173 | enum TestType { |
| 174 | kFailGetInfo_TestType, |
| 175 | kFailGetPixels_TestType, |
| 176 | kSucceedGetPixels_TestType, |
| 177 | kLast_TestType = kSucceedGetPixels_TestType |
| 178 | }; |
| 179 | static int Width() { return 10; } |
| 180 | static int Height() { return 10; } |
| 181 | static SkColor Color() { return SK_ColorCYAN; } |
| 182 | TestImageGenerator(TestType type, skiatest::Reporter* reporter) |
| 183 | : fType(type), fReporter(reporter) { |
| 184 | SkASSERT((fType <= kLast_TestType) && (fType >= 0)); |
| 185 | } |
| 186 | ~TestImageGenerator() { } |
| 187 | bool getInfo(SkImageInfo* info) SK_OVERRIDE { |
| 188 | REPORTER_ASSERT(fReporter, NULL != info); |
| 189 | if ((NULL == info) || (kFailGetInfo_TestType == fType)) { |
| 190 | return false; |
| 191 | } |
| 192 | info->fWidth = TestImageGenerator::Width(); |
| 193 | info->fHeight = TestImageGenerator::Height(); |
| 194 | info->fColorType = kPMColor_SkColorType; |
| 195 | info->fAlphaType = kOpaque_SkAlphaType; |
| 196 | return true; |
| 197 | } |
| 198 | bool getPixels(const SkImageInfo& info, |
| 199 | void* pixels, |
| 200 | size_t rowBytes) SK_OVERRIDE { |
| 201 | REPORTER_ASSERT(fReporter, pixels != NULL); |
| 202 | size_t minRowBytes |
| 203 | = static_cast<size_t>(info.fWidth * info.bytesPerPixel()); |
| 204 | REPORTER_ASSERT(fReporter, rowBytes >= minRowBytes); |
| 205 | if ((NULL == pixels) |
| 206 | || (fType != kSucceedGetPixels_TestType) |
| 207 | || (info.fColorType != kPMColor_SkColorType)) { |
| 208 | return false; |
| 209 | } |
| 210 | char* bytePtr = static_cast<char*>(pixels); |
| 211 | for (int y = 0; y < info.fHeight; ++y) { |
| 212 | sk_memset32(reinterpret_cast<SkColor*>(bytePtr), |
| 213 | TestImageGenerator::Color(), info.fWidth); |
| 214 | bytePtr += rowBytes; |
| 215 | } |
| 216 | return true; |
| 217 | } |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 218 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 219 | private: |
| 220 | const TestType fType; |
| 221 | skiatest::Reporter* const fReporter; |
| 222 | }; |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 223 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 224 | void CheckTestImageGeneratorBitmap(skiatest::Reporter* reporter, |
| 225 | const SkBitmap& bm) { |
| 226 | REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width()); |
| 227 | REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height()); |
| 228 | SkAutoLockPixels autoLockPixels(bm); |
| 229 | REPORTER_ASSERT(reporter, NULL != bm.getPixels()); |
| 230 | if (NULL == bm.getPixels()) { |
| 231 | return; |
| 232 | } |
| 233 | int errors = 0; |
| 234 | for (int y = 0; y < bm.height(); ++y) { |
| 235 | for (int x = 0; x < bm.width(); ++x) { |
| 236 | if (TestImageGenerator::Color() != *bm.getAddr32(x, y)) { |
| 237 | ++errors; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | REPORTER_ASSERT(reporter, 0 == errors); |
| 242 | } |
| 243 | |
| 244 | enum PixelRefType { |
| 245 | kSkCaching_PixelRefType, |
| 246 | kSkDiscardable_PixelRefType, |
| 247 | kLast_PixelRefType = kSkDiscardable_PixelRefType |
| 248 | }; |
| 249 | void CheckPixelRef(TestImageGenerator::TestType type, |
| 250 | skiatest::Reporter* reporter, |
| 251 | PixelRefType pixelRefType, |
| 252 | SkDiscardableMemory::Factory* factory) { |
| 253 | SkASSERT((pixelRefType >= 0) && (pixelRefType <= kLast_PixelRefType)); |
| 254 | SkAutoTDelete<SkImageGenerator> gen(SkNEW_ARGS(TestImageGenerator, |
| 255 | (type, reporter))); |
| 256 | REPORTER_ASSERT(reporter, gen.get() != NULL); |
| 257 | SkBitmap lazy; |
| 258 | bool success; |
| 259 | if (kSkCaching_PixelRefType == pixelRefType) { |
| 260 | // Ignore factory; use global SkScaledImageCache. |
| 261 | success = SkCachingPixelRef::Install(gen.detach(), &lazy); |
| 262 | } else { |
halcanary@google.com | edd370f | 2013-12-10 21:11:12 +0000 | [diff] [blame] | 263 | success = SkInstallDiscardablePixelRef(gen.detach(), &lazy, factory); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 264 | } |
| 265 | REPORTER_ASSERT(reporter, success |
| 266 | == (TestImageGenerator::kFailGetInfo_TestType != type)); |
| 267 | if (TestImageGenerator::kSucceedGetPixels_TestType == type) { |
| 268 | CheckTestImageGeneratorBitmap(reporter, lazy); |
| 269 | } else if (TestImageGenerator::kFailGetPixels_TestType == type) { |
| 270 | SkAutoLockPixels autoLockPixels(lazy); |
| 271 | REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 272 | } |
| 273 | } |
| 274 | } // namespace |
reed@google.com | 1d0654f | 2013-12-12 22:37:32 +0000 | [diff] [blame] | 275 | |
| 276 | // new/lock/delete is an odd pattern for a pixelref, but it needs to not assert |
| 277 | static void test_newlockdelete(skiatest::Reporter* reporter) { |
| 278 | SkBitmap bm; |
| 279 | SkImageGenerator* ig = new TestImageGenerator( |
| 280 | TestImageGenerator::kSucceedGetPixels_TestType, |
| 281 | reporter); |
| 282 | SkInstallDiscardablePixelRef(ig, &bm, NULL); |
| 283 | bm.pixelRef()->lockPixels(); |
| 284 | } |
| 285 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 286 | /** |
| 287 | * This tests the basic functionality of SkDiscardablePixelRef with a |
| 288 | * basic SkImageGenerator implementation and several |
| 289 | * SkDiscardableMemory::Factory choices. |
| 290 | */ |
| 291 | DEF_TEST(DiscardableAndCachingPixelRef, reporter) { |
reed@google.com | 1d0654f | 2013-12-12 22:37:32 +0000 | [diff] [blame] | 292 | test_newlockdelete(reporter); |
| 293 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 294 | CheckPixelRef(TestImageGenerator::kFailGetInfo_TestType, |
| 295 | reporter, kSkCaching_PixelRefType, NULL); |
| 296 | CheckPixelRef(TestImageGenerator::kFailGetPixels_TestType, |
| 297 | reporter, kSkCaching_PixelRefType, NULL); |
| 298 | CheckPixelRef(TestImageGenerator::kSucceedGetPixels_TestType, |
| 299 | reporter, kSkCaching_PixelRefType, NULL); |
| 300 | |
| 301 | CheckPixelRef(TestImageGenerator::kFailGetInfo_TestType, |
| 302 | reporter, kSkDiscardable_PixelRefType, NULL); |
| 303 | CheckPixelRef(TestImageGenerator::kFailGetPixels_TestType, |
| 304 | reporter, kSkDiscardable_PixelRefType, NULL); |
| 305 | CheckPixelRef(TestImageGenerator::kSucceedGetPixels_TestType, |
| 306 | reporter, kSkDiscardable_PixelRefType, NULL); |
| 307 | |
| 308 | SkAutoTUnref<SkDiscardableMemoryPool> pool( |
| 309 | SkNEW_ARGS(SkDiscardableMemoryPool, (1, NULL))); |
| 310 | REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); |
| 311 | CheckPixelRef(TestImageGenerator::kFailGetPixels_TestType, |
| 312 | reporter, kSkDiscardable_PixelRefType, pool); |
| 313 | REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); |
| 314 | CheckPixelRef(TestImageGenerator::kSucceedGetPixels_TestType, |
| 315 | reporter, kSkDiscardable_PixelRefType, pool); |
| 316 | REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); |
| 317 | |
| 318 | SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool(); |
halcanary@google.com | bc55eec | 2013-12-10 18:33:07 +0000 | [diff] [blame] | 319 | // Only acts differently from NULL on a platform that has a |
| 320 | // default discardable memory implementation that differs from the |
| 321 | // global DM pool. |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 322 | CheckPixelRef(TestImageGenerator::kFailGetPixels_TestType, |
| 323 | reporter, kSkDiscardable_PixelRefType, globalPool); |
| 324 | CheckPixelRef(TestImageGenerator::kSucceedGetPixels_TestType, |
| 325 | reporter, kSkDiscardable_PixelRefType, globalPool); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 326 | } |
| 327 | //////////////////////////////////////////////////////////////////////////////// |