blob: 51af15fd39292dba66b0d675ab5042b7510dabc4 [file] [log] [blame]
commit-bot@chromium.org75854792013-10-29 19:55:00 +00001/*
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.com2c7c7ee2013-12-05 18:31:42 +000011#include "SkDiscardableMemoryPool.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000012#include "SkImageDecoder.h"
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +000013#include "SkImageGeneratorPriv.h"
reed011f39a2014-08-28 13:35:23 -070014#include "SkResourceCache.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000015#include "SkStream.h"
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000016#include "SkUtils.h"
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000017
commit-bot@chromium.org75854792013-10-29 19:55:00 +000018#include "Test.h"
19
commit-bot@chromium.org75854792013-10-29 19:55:00 +000020/**
21 * Fill this bitmap with some color.
22 */
23static void make_test_image(SkBitmap* bm) {
reed6c225732014-06-09 19:52:07 -070024 const int W = 50, H = 50;
25 bm->allocN32Pixels(W, H);
commit-bot@chromium.org75854792013-10-29 19:55:00 +000026 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 */
40static 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 }
halcanary96fcdcc2015-08-27 07:41:13 -070046 return nullptr;
commit-bot@chromium.org75854792013-10-29 19:55:00 +000047}
48
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000049////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org75854792013-10-29 19:55:00 +000050
51static 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 }
bsalomon49f085d2014-09-05 13:34:00 -070064 REPORTER_ASSERT(reporter, b1.getPixels());
65 REPORTER_ASSERT(reporter, b2.getPixels());
commit-bot@chromium.org75854792013-10-29 19:55:00 +000066 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.org6e3e4222013-11-06 10:08:30 +000076
commit-bot@chromium.org75854792013-10-29 19:55:00 +000077 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.com36d08c52013-12-05 14:00:03 +000088typedef bool (*InstallEncoded)(SkData* encoded, SkBitmap* dst);
89
commit-bot@chromium.org75854792013-10-29 19:55:00 +000090/**
halcanary@google.com36d08c52013-12-05 14:00:03 +000091 This function tests three differently encoded images against the
92 original bitmap */
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000093static void test_three_encodings(skiatest::Reporter* reporter,
halcanary@google.com36d08c52013-12-05 14:00:03 +000094 InstallEncoded install) {
commit-bot@chromium.org75854792013-10-29 19:55:00 +000095 SkBitmap original;
96 make_test_image(&original);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000097 REPORTER_ASSERT(reporter, !original.empty());
98 REPORTER_ASSERT(reporter, !original.isNull());
99 if (original.empty() || original.isNull()) {
100 return;
101 }
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000102 static const SkImageEncoder::Type types[] = {
103 SkImageEncoder::kPNG_Type,
104 SkImageEncoder::kJPEG_Type,
105 SkImageEncoder::kWEBP_Type
106 };
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000107 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));
halcanary96fcdcc2015-08-27 07:41:13 -0700110 REPORTER_ASSERT(reporter, encoded.get() != nullptr);
111 if (nullptr == encoded.get()) {
halcanary@google.com36d08c52013-12-05 14:00:03 +0000112 continue;
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000113 }
halcanary@google.com36d08c52013-12-05 14:00:03 +0000114 SkBitmap lazy;
115 bool installSuccess = install(encoded.get(), &lazy);
116 REPORTER_ASSERT(reporter, installSuccess);
117 if (!installSuccess) {
118 continue;
119 }
halcanary96fcdcc2015-08-27 07:41:13 -0700120 REPORTER_ASSERT(reporter, nullptr == lazy.getPixels());
halcanary@google.com36d08c52013-12-05 14:00:03 +0000121 {
122 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
bsalomon49f085d2014-09-05 13:34:00 -0700123 REPORTER_ASSERT(reporter, lazy.getPixels());
halcanary96fcdcc2015-08-27 07:41:13 -0700124 if (nullptr == lazy.getPixels()) {
halcanary@google.com36d08c52013-12-05 14:00:03 +0000125 continue;
126 }
127 }
128 // pixels should be gone!
halcanary96fcdcc2015-08-27 07:41:13 -0700129 REPORTER_ASSERT(reporter, nullptr == lazy.getPixels());
halcanary@google.com36d08c52013-12-05 14:00:03 +0000130 {
131 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
bsalomon49f085d2014-09-05 13:34:00 -0700132 REPORTER_ASSERT(reporter, lazy.getPixels());
halcanary96fcdcc2015-08-27 07:41:13 -0700133 if (nullptr == lazy.getPixels()) {
halcanary@google.com36d08c52013-12-05 14:00:03 +0000134 continue;
135 }
136 }
137 bool comparePixels = (SkImageEncoder::kPNG_Type == type);
138 compare_bitmaps(reporter, original, lazy, comparePixels);
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000139 }
140}
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000141
halcanary@google.com36d08c52013-12-05 14:00:03 +0000142////////////////////////////////////////////////////////////////////////////////
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000143static bool install_skDiscardablePixelRef(SkData* encoded, SkBitmap* dst) {
144 // Use system-default discardable memory.
reedd1146452015-09-25 06:56:57 -0700145 return SkDEPRECATED_InstallDiscardablePixelRef(encoded, dst);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000146}
halcanary@google.comad04eb42013-11-21 15:32:08 +0000147
halcanary@google.com36d08c52013-12-05 14:00:03 +0000148////////////////////////////////////////////////////////////////////////////////
149/**
reeddd42e5c2015-09-24 00:21:20 -0700150 * This checks to see that SkDiscardablePixelRef works as advertised with a
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000151 * SkDecodingImageGenerator.
halcanary@google.com36d08c52013-12-05 14:00:03 +0000152 */
halcanary@google.comad04eb42013-11-21 15:32:08 +0000153DEF_TEST(DecodingImageGenerator, reporter) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000154 test_three_encodings(reporter, install_skDiscardablePixelRef);
halcanary@google.comad04eb42013-11-21 15:32:08 +0000155}
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000156
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000157class TestImageGenerator : public SkImageGenerator {
158public:
159 enum TestType {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000160 kFailGetPixels_TestType,
161 kSucceedGetPixels_TestType,
162 kLast_TestType = kSucceedGetPixels_TestType
163 };
164 static int Width() { return 10; }
165 static int Height() { return 10; }
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800166 // 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.com2c7c7ee2013-12-05 18:31:42 +0000173 SkASSERT((fType <= kLast_TestType) && (fType >= 0));
174 }
tfarina@chromium.org58674812014-01-21 23:39:22 +0000175 virtual ~TestImageGenerator() { }
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000176
177protected:
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800178 static SkImageInfo GetMyInfo(SkColorType colorType) {
179 return SkImageInfo::Make(TestImageGenerator::Width(), TestImageGenerator::Height(),
180 colorType, kOpaque_SkAlphaType);
reed3ef71e32015-03-19 08:31:14 -0700181 }
182
scroggo5315fd42015-07-09 09:08:00 -0700183 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
184 SkPMColor ctable[], int* ctableCount) override {
halcanary96fcdcc2015-08-27 07:41:13 -0700185 REPORTER_ASSERT(fReporter, pixels != nullptr);
scroggo08649082015-02-13 11:13:34 -0800186 REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes());
187 if (fType != kSucceedGetPixels_TestType) {
scroggo5315fd42015-07-09 09:08:00 -0700188 return false;
scroggo08649082015-02-13 11:13:34 -0800189 }
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800190 if (info.colorType() != kN32_SkColorType && info.colorType() != getInfo().colorType()) {
scroggo5315fd42015-07-09 09:08:00 -0700191 return false;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000192 }
193 char* bytePtr = static_cast<char*>(pixels);
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800194 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.com2c7c7ee2013-12-05 18:31:42 +0000219 }
scroggo5315fd42015-07-09 09:08:00 -0700220 return true;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000221 }
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000222
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000223private:
224 const TestType fType;
225 skiatest::Reporter* const fReporter;
reed3ef71e32015-03-19 08:31:14 -0700226
227 typedef SkImageGenerator INHERITED;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000228};
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000229
tfarina@chromium.org58674812014-01-21 23:39:22 +0000230static void check_test_image_generator_bitmap(skiatest::Reporter* reporter,
231 const SkBitmap& bm) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000232 REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width());
233 REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height());
234 SkAutoLockPixels autoLockPixels(bm);
bsalomon49f085d2014-09-05 13:34:00 -0700235 REPORTER_ASSERT(reporter, bm.getPixels());
halcanary96fcdcc2015-08-27 07:41:13 -0700236 if (nullptr == bm.getPixels()) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000237 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.stojiljkovic07e26922015-11-10 04:55:15 -0800242 if (TestImageGenerator::Color() != bm.getColor(x, y)) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000243 ++errors;
244 }
245 }
246 }
247 REPORTER_ASSERT(reporter, 0 == errors);
248}
249
tfarina@chromium.org58674812014-01-21 23:39:22 +0000250static void check_pixelref(TestImageGenerator::TestType type,
251 skiatest::Reporter* reporter,
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800252 SkDiscardableMemory::Factory* factory,
253 SkColorType colorType) {
254 SkAutoTDelete<SkImageGenerator> gen(new TestImageGenerator(type, reporter, colorType));
halcanary96fcdcc2015-08-27 07:41:13 -0700255 REPORTER_ASSERT(reporter, gen.get() != nullptr);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000256 SkBitmap lazy;
reedd1146452015-09-25 06:56:57 -0700257 bool success = SkDEPRECATED_InstallDiscardablePixelRef(gen.detach(), nullptr, &lazy, factory);
reeddd42e5c2015-09-24 00:21:20 -0700258
reed3ef71e32015-03-19 08:31:14 -0700259 REPORTER_ASSERT(reporter, success);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000260 if (TestImageGenerator::kSucceedGetPixels_TestType == type) {
tfarina@chromium.org58674812014-01-21 23:39:22 +0000261 check_test_image_generator_bitmap(reporter, lazy);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000262 } else if (TestImageGenerator::kFailGetPixels_TestType == type) {
263 SkAutoLockPixels autoLockPixels(lazy);
halcanary96fcdcc2015-08-27 07:41:13 -0700264 REPORTER_ASSERT(reporter, nullptr == lazy.getPixels());
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000265 }
266}
reed@google.com1d0654f2013-12-12 22:37:32 +0000267
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000268/**
269 * This tests the basic functionality of SkDiscardablePixelRef with a
270 * basic SkImageGenerator implementation and several
271 * SkDiscardableMemory::Factory choices.
272 */
273DEF_TEST(DiscardableAndCachingPixelRef, reporter) {
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800274 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.com2c7c7ee2013-12-05 18:31:42 +0000284
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800285 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.com2c7c7ee2013-12-05 18:31:42 +0000294
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800295 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.com2c7c7ee2013-12-05 18:31:42 +0000304}
halcanaryea4673f2014-08-18 08:27:09 -0700305
306////////////////////////////////////////////////////////////////////////////////
307
308DEF_TEST(Image_NewFromGenerator, r) {
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800309 const TestImageGenerator::TestType testTypes[] = {
halcanaryea4673f2014-08-18 08:27:09 -0700310 TestImageGenerator::kFailGetPixels_TestType,
311 TestImageGenerator::kSucceedGetPixels_TestType,
312 };
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800313 const SkColorType testColorTypes[] = {
314 kN32_SkColorType,
315 kIndex_8_SkColorType,
316 kRGB_565_SkColorType
317 };
halcanaryea4673f2014-08-18 08:27:09 -0700318 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) {
319 TestImageGenerator::TestType test = testTypes[i];
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800320 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());
halcanaryea4673f2014-08-18 08:27:09 -0700331
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800332 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 }
halcanaryea4673f2014-08-18 08:27:09 -0700345 }
346 }
347}