blob: 65623d0e4f9b0e7527e445353046959f3e6a9be9 [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"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +00009#include "SkCachingPixelRef.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000010#include "SkCanvas.h"
11#include "SkData.h"
halcanary@google.comad04eb42013-11-21 15:32:08 +000012#include "SkDecodingImageGenerator.h"
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000013#include "SkDiscardableMemoryPool.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000014#include "SkImageDecoder.h"
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +000015#include "SkImageGeneratorPriv.h"
reed011f39a2014-08-28 13:35:23 -070016#include "SkResourceCache.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000017#include "SkStream.h"
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000018#include "SkUtils.h"
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000019
commit-bot@chromium.org75854792013-10-29 19:55:00 +000020#include "Test.h"
21
commit-bot@chromium.org75854792013-10-29 19:55:00 +000022/**
23 * Fill this bitmap with some color.
24 */
25static void make_test_image(SkBitmap* bm) {
reed6c225732014-06-09 19:52:07 -070026 const int W = 50, H = 50;
27 bm->allocN32Pixels(W, H);
commit-bot@chromium.org75854792013-10-29 19:55:00 +000028 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 */
42static 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.com2c7c7ee2013-12-05 18:31:42 +000051////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org75854792013-10-29 19:55:00 +000052
53static 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 }
66 REPORTER_ASSERT(reporter, NULL != b1.getPixels());
67 REPORTER_ASSERT(reporter, NULL != b2.getPixels());
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.org6e3e4222013-11-06 10:08:30 +000078
commit-bot@chromium.org75854792013-10-29 19:55:00 +000079 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.com36d08c52013-12-05 14:00:03 +000090typedef bool (*InstallEncoded)(SkData* encoded, SkBitmap* dst);
91
commit-bot@chromium.org75854792013-10-29 19:55:00 +000092/**
halcanary@google.com36d08c52013-12-05 14:00:03 +000093 This function tests three differently encoded images against the
94 original bitmap */
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000095static void test_three_encodings(skiatest::Reporter* reporter,
halcanary@google.com36d08c52013-12-05 14:00:03 +000096 InstallEncoded install) {
commit-bot@chromium.org75854792013-10-29 19:55:00 +000097 SkBitmap original;
98 make_test_image(&original);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000099 REPORTER_ASSERT(reporter, !original.empty());
100 REPORTER_ASSERT(reporter, !original.isNull());
101 if (original.empty() || original.isNull()) {
102 return;
103 }
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000104 static const SkImageEncoder::Type types[] = {
105 SkImageEncoder::kPNG_Type,
106 SkImageEncoder::kJPEG_Type,
107 SkImageEncoder::kWEBP_Type
108 };
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000109 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.com36d08c52013-12-05 14:00:03 +0000113 if (NULL == encoded.get()) {
114 continue;
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000115 }
halcanary@google.com36d08c52013-12-05 14:00:03 +0000116 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.
125 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
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.
134 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
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.org75854792013-10-29 19:55:00 +0000141 }
142}
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000143
halcanary@google.com36d08c52013-12-05 14:00:03 +0000144////////////////////////////////////////////////////////////////////////////////
halcanary@google.comdcfebfa2013-12-05 14:18:07 +0000145static bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) {
halcanary@google.com36d08c52013-12-05 14:00:03 +0000146 return SkCachingPixelRef::Install(
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000147 SkDecodingImageGenerator::Create(
148 encoded, SkDecodingImageGenerator::Options()), dst);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000149}
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000150static bool install_skDiscardablePixelRef(SkData* encoded, SkBitmap* dst) {
151 // Use system-default discardable memory.
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000152 return SkInstallDiscardablePixelRef(
153 SkDecodingImageGenerator::Create(
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +0000154 encoded, SkDecodingImageGenerator::Options()), dst);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000155}
halcanary@google.comad04eb42013-11-21 15:32:08 +0000156
halcanary@google.com36d08c52013-12-05 14:00:03 +0000157////////////////////////////////////////////////////////////////////////////////
158/**
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000159 * This checks to see that a SkCachingPixelRef and a
160 * SkDiscardablePixelRef works as advertised with a
161 * SkDecodingImageGenerator.
halcanary@google.com36d08c52013-12-05 14:00:03 +0000162 */
halcanary@google.comad04eb42013-11-21 15:32:08 +0000163DEF_TEST(DecodingImageGenerator, reporter) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000164 test_three_encodings(reporter, install_skCachingPixelRef);
165 test_three_encodings(reporter, install_skDiscardablePixelRef);
halcanary@google.comad04eb42013-11-21 15:32:08 +0000166}
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000167
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000168class TestImageGenerator : public SkImageGenerator {
169public:
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; }
halcanaryea4673f2014-08-18 08:27:09 -0700178 static uint32_t Color() { return 0xff123456; }
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000179 TestImageGenerator(TestType type, skiatest::Reporter* reporter)
180 : fType(type), fReporter(reporter) {
181 SkASSERT((fType <= kLast_TestType) && (fType >= 0));
182 }
tfarina@chromium.org58674812014-01-21 23:39:22 +0000183 virtual ~TestImageGenerator() { }
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000184
185protected:
186 virtual bool onGetInfo(SkImageInfo* info) SK_OVERRIDE {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000187 REPORTER_ASSERT(fReporter, NULL != info);
188 if ((NULL == info) || (kFailGetInfo_TestType == fType)) {
189 return false;
190 }
191 info->fWidth = TestImageGenerator::Width();
192 info->fHeight = TestImageGenerator::Height();
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000193 info->fColorType = kN32_SkColorType;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000194 info->fAlphaType = kOpaque_SkAlphaType;
195 return true;
196 }
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000197
198 virtual bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
199 SkPMColor ctable[], int* ctableCount) SK_OVERRIDE {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000200 REPORTER_ASSERT(fReporter, pixels != NULL);
201 size_t minRowBytes
202 = static_cast<size_t>(info.fWidth * info.bytesPerPixel());
203 REPORTER_ASSERT(fReporter, rowBytes >= minRowBytes);
204 if ((NULL == pixels)
205 || (fType != kSucceedGetPixels_TestType)
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000206 || (info.fColorType != kN32_SkColorType)) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000207 return false;
208 }
209 char* bytePtr = static_cast<char*>(pixels);
210 for (int y = 0; y < info.fHeight; ++y) {
211 sk_memset32(reinterpret_cast<SkColor*>(bytePtr),
212 TestImageGenerator::Color(), info.fWidth);
213 bytePtr += rowBytes;
214 }
215 return true;
216 }
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000217
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000218private:
219 const TestType fType;
220 skiatest::Reporter* const fReporter;
221};
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000222
tfarina@chromium.org58674812014-01-21 23:39:22 +0000223static void check_test_image_generator_bitmap(skiatest::Reporter* reporter,
224 const SkBitmap& bm) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000225 REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width());
226 REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height());
227 SkAutoLockPixels autoLockPixels(bm);
228 REPORTER_ASSERT(reporter, NULL != bm.getPixels());
229 if (NULL == bm.getPixels()) {
230 return;
231 }
232 int errors = 0;
233 for (int y = 0; y < bm.height(); ++y) {
234 for (int x = 0; x < bm.width(); ++x) {
235 if (TestImageGenerator::Color() != *bm.getAddr32(x, y)) {
236 ++errors;
237 }
238 }
239 }
240 REPORTER_ASSERT(reporter, 0 == errors);
241}
242
243enum PixelRefType {
244 kSkCaching_PixelRefType,
245 kSkDiscardable_PixelRefType,
246 kLast_PixelRefType = kSkDiscardable_PixelRefType
247};
tfarina@chromium.org58674812014-01-21 23:39:22 +0000248
249static void check_pixelref(TestImageGenerator::TestType type,
250 skiatest::Reporter* reporter,
251 PixelRefType pixelRefType,
252 SkDiscardableMemory::Factory* factory) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000253 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) {
reed011f39a2014-08-28 13:35:23 -0700260 // Ignore factory; use global cache.
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000261 success = SkCachingPixelRef::Install(gen.detach(), &lazy);
262 } else {
halcanary@google.comedd370f2013-12-10 21:11:12 +0000263 success = SkInstallDiscardablePixelRef(gen.detach(), &lazy, factory);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000264 }
265 REPORTER_ASSERT(reporter, success
266 == (TestImageGenerator::kFailGetInfo_TestType != type));
267 if (TestImageGenerator::kSucceedGetPixels_TestType == type) {
tfarina@chromium.org58674812014-01-21 23:39:22 +0000268 check_test_image_generator_bitmap(reporter, lazy);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000269 } else if (TestImageGenerator::kFailGetPixels_TestType == type) {
270 SkAutoLockPixels autoLockPixels(lazy);
271 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
272 }
273}
reed@google.com1d0654f2013-12-12 22:37:32 +0000274
275// new/lock/delete is an odd pattern for a pixelref, but it needs to not assert
276static void test_newlockdelete(skiatest::Reporter* reporter) {
277 SkBitmap bm;
278 SkImageGenerator* ig = new TestImageGenerator(
tfarina@chromium.org58674812014-01-21 23:39:22 +0000279 TestImageGenerator::kSucceedGetPixels_TestType, reporter);
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +0000280 SkInstallDiscardablePixelRef(ig, &bm);
reed@google.com1d0654f2013-12-12 22:37:32 +0000281 bm.pixelRef()->lockPixels();
282}
283
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000284/**
285 * This tests the basic functionality of SkDiscardablePixelRef with a
286 * basic SkImageGenerator implementation and several
287 * SkDiscardableMemory::Factory choices.
288 */
289DEF_TEST(DiscardableAndCachingPixelRef, reporter) {
reed@google.com1d0654f2013-12-12 22:37:32 +0000290 test_newlockdelete(reporter);
291
tfarina@chromium.org58674812014-01-21 23:39:22 +0000292 check_pixelref(TestImageGenerator::kFailGetInfo_TestType,
293 reporter, kSkCaching_PixelRefType, NULL);
294 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
295 reporter, kSkCaching_PixelRefType, NULL);
296 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
297 reporter, kSkCaching_PixelRefType, NULL);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000298
tfarina@chromium.org58674812014-01-21 23:39:22 +0000299 check_pixelref(TestImageGenerator::kFailGetInfo_TestType,
300 reporter, kSkDiscardable_PixelRefType, NULL);
301 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
302 reporter, kSkDiscardable_PixelRefType, NULL);
303 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
304 reporter, kSkDiscardable_PixelRefType, NULL);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000305
306 SkAutoTUnref<SkDiscardableMemoryPool> pool(
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000307 SkDiscardableMemoryPool::Create(1, NULL));
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000308 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
tfarina@chromium.org58674812014-01-21 23:39:22 +0000309 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
310 reporter, kSkDiscardable_PixelRefType, pool);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000311 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
tfarina@chromium.org58674812014-01-21 23:39:22 +0000312 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
313 reporter, kSkDiscardable_PixelRefType, pool);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000314 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
315
316 SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool();
halcanary@google.combc55eec2013-12-10 18:33:07 +0000317 // Only acts differently from NULL on a platform that has a
318 // default discardable memory implementation that differs from the
319 // global DM pool.
tfarina@chromium.org58674812014-01-21 23:39:22 +0000320 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
321 reporter, kSkDiscardable_PixelRefType, globalPool);
322 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
323 reporter, kSkDiscardable_PixelRefType, globalPool);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000324}
halcanaryea4673f2014-08-18 08:27:09 -0700325
326////////////////////////////////////////////////////////////////////////////////
327
328DEF_TEST(Image_NewFromGenerator, r) {
329 TestImageGenerator::TestType testTypes[] = {
330 TestImageGenerator::kFailGetInfo_TestType,
331 TestImageGenerator::kFailGetPixels_TestType,
332 TestImageGenerator::kSucceedGetPixels_TestType,
333 };
334 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) {
335 TestImageGenerator::TestType test = testTypes[i];
336 SkImageGenerator* gen = SkNEW_ARGS(TestImageGenerator, (test, r));
337 SkAutoTUnref<SkImage> image(SkImage::NewFromGenerator(gen));
338 if (TestImageGenerator::kFailGetInfo_TestType == test) {
339 REPORTER_ASSERT(r, NULL == image.get());
340 continue;
341 }
342 if (NULL == image.get()) {
343 ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed ["
344 SK_SIZE_T_SPECIFIER "]", i);
345 continue;
346 }
347 REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width());
348 REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height());
349
350 SkBitmap bitmap;
351 SkAssertResult(bitmap.allocN32Pixels(TestImageGenerator::Width(),
352 TestImageGenerator::Height()));
353 SkCanvas canvas(bitmap);
354 const SkColor kDefaultColor = 0xffabcdef;
355 canvas.clear(kDefaultColor);
356 image->draw(&canvas, 0, 0, NULL);
357 if (TestImageGenerator::kSucceedGetPixels_TestType == test) {
358 REPORTER_ASSERT(
359 r, TestImageGenerator::Color() == *bitmap.getAddr32(0, 0));
360 } else {
361 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0,0));
362 }
363 }
364}