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