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