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" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 9 | #include "SkCachingPixelRef.h" |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkData.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" |
commit-bot@chromium.org | 2d970b5 | 2014-05-27 14:14:22 +0000 | [diff] [blame] | 14 | #include "SkImageGeneratorPriv.h" |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 15 | #include "SkResourceCache.h" |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 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) { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 25 | const int W = 50, H = 50; |
| 26 | bm->allocN32Pixels(W, H); |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 27 | bm->eraseColor(SK_ColorBLACK); |
| 28 | SkCanvas canvas(*bm); |
| 29 | SkPaint paint; |
| 30 | paint.setColor(SK_ColorBLUE); |
| 31 | canvas.drawRectCoords(0, 0, SkIntToScalar(W/2), |
| 32 | SkIntToScalar(H/2), paint); |
| 33 | paint.setColor(SK_ColorWHITE); |
| 34 | canvas.drawRectCoords(SkIntToScalar(W/2), SkIntToScalar(H/2), |
| 35 | SkIntToScalar(W), SkIntToScalar(H), paint); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * encode this bitmap into some data via SkImageEncoder |
| 40 | */ |
| 41 | static SkData* create_data_from_bitmap(const SkBitmap& bm, |
| 42 | SkImageEncoder::Type type) { |
| 43 | SkDynamicMemoryWStream stream; |
| 44 | if (SkImageEncoder::EncodeStream(&stream, bm, type, 100)) { |
| 45 | return stream.copyToData(); |
| 46 | } |
| 47 | return NULL; |
| 48 | } |
| 49 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 50 | //////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 51 | |
| 52 | static void compare_bitmaps(skiatest::Reporter* reporter, |
| 53 | const SkBitmap& b1, const SkBitmap& b2, |
| 54 | bool pixelPerfect = true) { |
| 55 | REPORTER_ASSERT(reporter, b1.empty() == b2.empty()); |
| 56 | REPORTER_ASSERT(reporter, b1.width() == b2.width()); |
| 57 | REPORTER_ASSERT(reporter, b1.height() == b2.height()); |
| 58 | REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull()); |
| 59 | SkAutoLockPixels autoLockPixels1(b1); |
| 60 | SkAutoLockPixels autoLockPixels2(b2); |
| 61 | REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull()); |
| 62 | if (b1.isNull() || b1.empty()) { |
| 63 | return; |
| 64 | } |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 65 | REPORTER_ASSERT(reporter, b1.getPixels()); |
| 66 | REPORTER_ASSERT(reporter, b2.getPixels()); |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 67 | if ((!(b1.getPixels())) || (!(b2.getPixels()))) { |
| 68 | return; |
| 69 | } |
| 70 | if ((b1.width() != b2.width()) || |
| 71 | (b1.height() != b2.height())) { |
| 72 | return; |
| 73 | } |
| 74 | if (!pixelPerfect) { |
| 75 | return; |
| 76 | } |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 77 | |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 78 | int pixelErrors = 0; |
| 79 | for (int y = 0; y < b2.height(); ++y) { |
| 80 | for (int x = 0; x < b2.width(); ++x) { |
| 81 | if (b1.getColor(x, y) != b2.getColor(x, y)) { |
| 82 | ++pixelErrors; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | REPORTER_ASSERT(reporter, 0 == pixelErrors); |
| 87 | } |
| 88 | |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 89 | typedef bool (*InstallEncoded)(SkData* encoded, SkBitmap* dst); |
| 90 | |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 91 | /** |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 92 | This function tests three differently encoded images against the |
| 93 | original bitmap */ |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 94 | static void test_three_encodings(skiatest::Reporter* reporter, |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 95 | InstallEncoded install) { |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 96 | SkBitmap original; |
| 97 | make_test_image(&original); |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 98 | REPORTER_ASSERT(reporter, !original.empty()); |
| 99 | REPORTER_ASSERT(reporter, !original.isNull()); |
| 100 | if (original.empty() || original.isNull()) { |
| 101 | return; |
| 102 | } |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 103 | static const SkImageEncoder::Type types[] = { |
| 104 | SkImageEncoder::kPNG_Type, |
| 105 | SkImageEncoder::kJPEG_Type, |
| 106 | SkImageEncoder::kWEBP_Type |
| 107 | }; |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 108 | for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) { |
| 109 | SkImageEncoder::Type type = types[i]; |
| 110 | SkAutoDataUnref encoded(create_data_from_bitmap(original, type)); |
| 111 | REPORTER_ASSERT(reporter, encoded.get() != NULL); |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 112 | if (NULL == encoded.get()) { |
| 113 | continue; |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 114 | } |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 115 | SkBitmap lazy; |
| 116 | bool installSuccess = install(encoded.get(), &lazy); |
| 117 | REPORTER_ASSERT(reporter, installSuccess); |
| 118 | if (!installSuccess) { |
| 119 | continue; |
| 120 | } |
| 121 | REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 122 | { |
| 123 | SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 124 | REPORTER_ASSERT(reporter, lazy.getPixels()); |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 125 | if (NULL == lazy.getPixels()) { |
| 126 | continue; |
| 127 | } |
| 128 | } |
| 129 | // pixels should be gone! |
| 130 | REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 131 | { |
| 132 | SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 133 | REPORTER_ASSERT(reporter, lazy.getPixels()); |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 134 | if (NULL == lazy.getPixels()) { |
| 135 | continue; |
| 136 | } |
| 137 | } |
| 138 | bool comparePixels = (SkImageEncoder::kPNG_Type == type); |
| 139 | compare_bitmaps(reporter, original, lazy, comparePixels); |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 142 | |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 143 | //////////////////////////////////////////////////////////////////////////////// |
halcanary@google.com | dcfebfa | 2013-12-05 14:18:07 +0000 | [diff] [blame] | 144 | static bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) { |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 145 | return SkCachingPixelRef::Install(SkImageGenerator::NewFromData(encoded), dst); |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 146 | } |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 147 | static bool install_skDiscardablePixelRef(SkData* encoded, SkBitmap* dst) { |
| 148 | // Use system-default discardable memory. |
reed | 5965c8a | 2015-01-07 18:04:45 -0800 | [diff] [blame] | 149 | return SkInstallDiscardablePixelRef(encoded, dst); |
commit-bot@chromium.org | 6e3e422 | 2013-11-06 10:08:30 +0000 | [diff] [blame] | 150 | } |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 151 | |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 152 | //////////////////////////////////////////////////////////////////////////////// |
| 153 | /** |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 154 | * This checks to see that a SkCachingPixelRef and a |
| 155 | * SkDiscardablePixelRef works as advertised with a |
| 156 | * SkDecodingImageGenerator. |
halcanary@google.com | 36d08c5 | 2013-12-05 14:00:03 +0000 | [diff] [blame] | 157 | */ |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 158 | DEF_TEST(DecodingImageGenerator, reporter) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 159 | test_three_encodings(reporter, install_skCachingPixelRef); |
| 160 | test_three_encodings(reporter, install_skDiscardablePixelRef); |
halcanary@google.com | ad04eb4 | 2013-11-21 15:32:08 +0000 | [diff] [blame] | 161 | } |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 162 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 163 | class TestImageGenerator : public SkImageGenerator { |
| 164 | public: |
| 165 | enum TestType { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 166 | kFailGetPixels_TestType, |
| 167 | kSucceedGetPixels_TestType, |
| 168 | kLast_TestType = kSucceedGetPixels_TestType |
| 169 | }; |
| 170 | static int Width() { return 10; } |
| 171 | static int Height() { return 10; } |
halcanary | ea4673f | 2014-08-18 08:27:09 -0700 | [diff] [blame] | 172 | static uint32_t Color() { return 0xff123456; } |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 173 | TestImageGenerator(TestType type, skiatest::Reporter* reporter) |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 174 | : INHERITED(GetMyInfo()), fType(type), fReporter(reporter) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 175 | SkASSERT((fType <= kLast_TestType) && (fType >= 0)); |
| 176 | } |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 177 | virtual ~TestImageGenerator() { } |
commit-bot@chromium.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 178 | |
| 179 | protected: |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 180 | static SkImageInfo GetMyInfo() { |
| 181 | return SkImageInfo::MakeN32(TestImageGenerator::Width(), TestImageGenerator::Height(), |
| 182 | kOpaque_SkAlphaType); |
| 183 | } |
| 184 | |
scroggo | 5315fd4 | 2015-07-09 09:08:00 -0700 | [diff] [blame^] | 185 | #ifdef SK_LEGACY_IMAGE_GENERATOR_ENUMS_AND_OPTIONS |
| 186 | Result onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 187 | const Options&, |
| 188 | SkPMColor ctable[], int* ctableCount) override { |
| 189 | #else |
| 190 | bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes, |
| 191 | SkPMColor ctable[], int* ctableCount) override { |
| 192 | #endif |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 193 | REPORTER_ASSERT(fReporter, pixels != NULL); |
scroggo | 0864908 | 2015-02-13 11:13:34 -0800 | [diff] [blame] | 194 | REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes()); |
| 195 | if (fType != kSucceedGetPixels_TestType) { |
scroggo | 5315fd4 | 2015-07-09 09:08:00 -0700 | [diff] [blame^] | 196 | #ifdef SK_LEGACY_IMAGE_GENERATOR_ENUMS_AND_OPTIONS |
scroggo | 0864908 | 2015-02-13 11:13:34 -0800 | [diff] [blame] | 197 | return kUnimplemented; |
scroggo | 5315fd4 | 2015-07-09 09:08:00 -0700 | [diff] [blame^] | 198 | #else |
| 199 | return false; |
| 200 | #endif |
scroggo | 0864908 | 2015-02-13 11:13:34 -0800 | [diff] [blame] | 201 | } |
| 202 | if (info.colorType() != kN32_SkColorType) { |
scroggo | 5315fd4 | 2015-07-09 09:08:00 -0700 | [diff] [blame^] | 203 | #ifdef SK_LEGACY_IMAGE_GENERATOR_ENUMS_AND_OPTIONS |
scroggo | 0864908 | 2015-02-13 11:13:34 -0800 | [diff] [blame] | 204 | return kInvalidConversion; |
scroggo | 5315fd4 | 2015-07-09 09:08:00 -0700 | [diff] [blame^] | 205 | #else |
| 206 | return false; |
| 207 | #endif |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 208 | } |
| 209 | char* bytePtr = static_cast<char*>(pixels); |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 210 | for (int y = 0; y < info.height(); ++y) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 211 | sk_memset32(reinterpret_cast<SkColor*>(bytePtr), |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 212 | TestImageGenerator::Color(), info.width()); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 213 | bytePtr += rowBytes; |
| 214 | } |
scroggo | 5315fd4 | 2015-07-09 09:08:00 -0700 | [diff] [blame^] | 215 | #ifdef SK_LEGACY_IMAGE_GENERATOR_ENUMS_AND_OPTIONS |
scroggo | 0864908 | 2015-02-13 11:13:34 -0800 | [diff] [blame] | 216 | return kSuccess; |
scroggo | 5315fd4 | 2015-07-09 09:08:00 -0700 | [diff] [blame^] | 217 | #else |
| 218 | return true; |
| 219 | #endif |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 220 | } |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 221 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 222 | private: |
| 223 | const TestType fType; |
| 224 | skiatest::Reporter* const fReporter; |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 225 | |
| 226 | typedef SkImageGenerator INHERITED; |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 227 | }; |
halcanary@google.com | 3d50ea1 | 2014-01-02 13:15:13 +0000 | [diff] [blame] | 228 | |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 229 | static void check_test_image_generator_bitmap(skiatest::Reporter* reporter, |
| 230 | const SkBitmap& bm) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 231 | REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width()); |
| 232 | REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height()); |
| 233 | SkAutoLockPixels autoLockPixels(bm); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 234 | REPORTER_ASSERT(reporter, bm.getPixels()); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 235 | if (NULL == bm.getPixels()) { |
| 236 | return; |
| 237 | } |
| 238 | int errors = 0; |
| 239 | for (int y = 0; y < bm.height(); ++y) { |
| 240 | for (int x = 0; x < bm.width(); ++x) { |
| 241 | if (TestImageGenerator::Color() != *bm.getAddr32(x, y)) { |
| 242 | ++errors; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | REPORTER_ASSERT(reporter, 0 == errors); |
| 247 | } |
| 248 | |
| 249 | enum PixelRefType { |
| 250 | kSkCaching_PixelRefType, |
| 251 | kSkDiscardable_PixelRefType, |
| 252 | kLast_PixelRefType = kSkDiscardable_PixelRefType |
| 253 | }; |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 254 | |
| 255 | static void check_pixelref(TestImageGenerator::TestType type, |
| 256 | skiatest::Reporter* reporter, |
| 257 | PixelRefType pixelRefType, |
| 258 | SkDiscardableMemory::Factory* factory) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 259 | SkASSERT((pixelRefType >= 0) && (pixelRefType <= kLast_PixelRefType)); |
| 260 | SkAutoTDelete<SkImageGenerator> gen(SkNEW_ARGS(TestImageGenerator, |
| 261 | (type, reporter))); |
| 262 | REPORTER_ASSERT(reporter, gen.get() != NULL); |
| 263 | SkBitmap lazy; |
| 264 | bool success; |
| 265 | if (kSkCaching_PixelRefType == pixelRefType) { |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 266 | // Ignore factory; use global cache. |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 267 | success = SkCachingPixelRef::Install(gen.detach(), &lazy); |
| 268 | } else { |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 269 | success = SkInstallDiscardablePixelRef(gen.detach(), NULL, &lazy, factory); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 270 | } |
reed | 3ef71e3 | 2015-03-19 08:31:14 -0700 | [diff] [blame] | 271 | REPORTER_ASSERT(reporter, success); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 272 | if (TestImageGenerator::kSucceedGetPixels_TestType == type) { |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 273 | check_test_image_generator_bitmap(reporter, lazy); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 274 | } else if (TestImageGenerator::kFailGetPixels_TestType == type) { |
| 275 | SkAutoLockPixels autoLockPixels(lazy); |
| 276 | REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 277 | } |
| 278 | } |
reed@google.com | 1d0654f | 2013-12-12 22:37:32 +0000 | [diff] [blame] | 279 | |
| 280 | // new/lock/delete is an odd pattern for a pixelref, but it needs to not assert |
| 281 | static void test_newlockdelete(skiatest::Reporter* reporter) { |
reed | ab86577 | 2015-05-21 06:29:05 -0700 | [diff] [blame] | 282 | #ifdef SK_SUPPORT_LEGACY_UNBALANCED_PIXELREF_LOCKCOUNT |
reed@google.com | 1d0654f | 2013-12-12 22:37:32 +0000 | [diff] [blame] | 283 | SkBitmap bm; |
| 284 | SkImageGenerator* ig = new TestImageGenerator( |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 285 | TestImageGenerator::kSucceedGetPixels_TestType, reporter); |
commit-bot@chromium.org | 2d970b5 | 2014-05-27 14:14:22 +0000 | [diff] [blame] | 286 | SkInstallDiscardablePixelRef(ig, &bm); |
reed@google.com | 1d0654f | 2013-12-12 22:37:32 +0000 | [diff] [blame] | 287 | bm.pixelRef()->lockPixels(); |
reed | ab86577 | 2015-05-21 06:29:05 -0700 | [diff] [blame] | 288 | #endif |
reed@google.com | 1d0654f | 2013-12-12 22:37:32 +0000 | [diff] [blame] | 289 | } |
| 290 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 291 | /** |
| 292 | * This tests the basic functionality of SkDiscardablePixelRef with a |
| 293 | * basic SkImageGenerator implementation and several |
| 294 | * SkDiscardableMemory::Factory choices. |
| 295 | */ |
| 296 | DEF_TEST(DiscardableAndCachingPixelRef, reporter) { |
reed@google.com | 1d0654f | 2013-12-12 22:37:32 +0000 | [diff] [blame] | 297 | test_newlockdelete(reporter); |
| 298 | |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 299 | check_pixelref(TestImageGenerator::kFailGetPixels_TestType, |
| 300 | reporter, kSkCaching_PixelRefType, NULL); |
| 301 | check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, |
| 302 | reporter, kSkCaching_PixelRefType, NULL); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 303 | |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 304 | check_pixelref(TestImageGenerator::kFailGetPixels_TestType, |
| 305 | reporter, kSkDiscardable_PixelRefType, NULL); |
| 306 | check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, |
| 307 | reporter, kSkDiscardable_PixelRefType, NULL); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 308 | |
| 309 | SkAutoTUnref<SkDiscardableMemoryPool> pool( |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 310 | SkDiscardableMemoryPool::Create(1, NULL)); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 311 | REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 312 | check_pixelref(TestImageGenerator::kFailGetPixels_TestType, |
| 313 | reporter, kSkDiscardable_PixelRefType, pool); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 314 | REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 315 | check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, |
| 316 | reporter, kSkDiscardable_PixelRefType, pool); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 317 | REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed()); |
| 318 | |
| 319 | SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool(); |
halcanary@google.com | bc55eec | 2013-12-10 18:33:07 +0000 | [diff] [blame] | 320 | // Only acts differently from NULL on a platform that has a |
| 321 | // default discardable memory implementation that differs from the |
| 322 | // global DM pool. |
tfarina@chromium.org | 5867481 | 2014-01-21 23:39:22 +0000 | [diff] [blame] | 323 | check_pixelref(TestImageGenerator::kFailGetPixels_TestType, |
| 324 | reporter, kSkDiscardable_PixelRefType, globalPool); |
| 325 | check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, |
| 326 | reporter, kSkDiscardable_PixelRefType, globalPool); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 327 | } |
halcanary | ea4673f | 2014-08-18 08:27:09 -0700 | [diff] [blame] | 328 | |
| 329 | //////////////////////////////////////////////////////////////////////////////// |
| 330 | |
| 331 | DEF_TEST(Image_NewFromGenerator, r) { |
| 332 | TestImageGenerator::TestType testTypes[] = { |
halcanary | ea4673f | 2014-08-18 08:27:09 -0700 | [diff] [blame] | 333 | TestImageGenerator::kFailGetPixels_TestType, |
| 334 | TestImageGenerator::kSucceedGetPixels_TestType, |
| 335 | }; |
| 336 | for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) { |
| 337 | TestImageGenerator::TestType test = testTypes[i]; |
| 338 | SkImageGenerator* gen = SkNEW_ARGS(TestImageGenerator, (test, r)); |
| 339 | SkAutoTUnref<SkImage> image(SkImage::NewFromGenerator(gen)); |
halcanary | ea4673f | 2014-08-18 08:27:09 -0700 | [diff] [blame] | 340 | if (NULL == image.get()) { |
| 341 | ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed [" |
| 342 | SK_SIZE_T_SPECIFIER "]", i); |
| 343 | continue; |
| 344 | } |
| 345 | REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width()); |
| 346 | REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height()); |
| 347 | |
| 348 | SkBitmap bitmap; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 349 | bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::Height()); |
halcanary | ea4673f | 2014-08-18 08:27:09 -0700 | [diff] [blame] | 350 | SkCanvas canvas(bitmap); |
| 351 | const SkColor kDefaultColor = 0xffabcdef; |
| 352 | canvas.clear(kDefaultColor); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 353 | canvas.drawImage(image, 0, 0, NULL); |
halcanary | ea4673f | 2014-08-18 08:27:09 -0700 | [diff] [blame] | 354 | if (TestImageGenerator::kSucceedGetPixels_TestType == test) { |
| 355 | REPORTER_ASSERT( |
| 356 | r, TestImageGenerator::Color() == *bitmap.getAddr32(0, 0)); |
| 357 | } else { |
| 358 | REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0,0)); |
| 359 | } |
| 360 | } |
| 361 | } |