blob: 92e9d785491ef3dc349dc36bd8c980f64f1acf5e [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 }
bsalomon49f085d2014-09-05 13:34:00 -070066 REPORTER_ASSERT(reporter, b1.getPixels());
67 REPORTER_ASSERT(reporter, b2.getPixels());
commit-bot@chromium.org75854792013-10-29 19:55:00 +000068 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.
bsalomon49f085d2014-09-05 13:34:00 -0700125 REPORTER_ASSERT(reporter, lazy.getPixels());
halcanary@google.com36d08c52013-12-05 14:00:03 +0000126 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.
bsalomon49f085d2014-09-05 13:34:00 -0700134 REPORTER_ASSERT(reporter, lazy.getPixels());
halcanary@google.com36d08c52013-12-05 14:00:03 +0000135 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 {
bsalomon49f085d2014-09-05 13:34:00 -0700187 REPORTER_ASSERT(fReporter, info);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000188 if ((NULL == info) || (kFailGetInfo_TestType == fType)) {
189 return false;
190 }
reede5ea5002014-09-03 11:54:58 -0700191 *info = SkImageInfo::MakeN32(TestImageGenerator::Width(),
192 TestImageGenerator::Height(),
193 kOpaque_SkAlphaType);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000194 return true;
195 }
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000196
197 virtual bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
198 SkPMColor ctable[], int* ctableCount) SK_OVERRIDE {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000199 REPORTER_ASSERT(fReporter, pixels != NULL);
reede5ea5002014-09-03 11:54:58 -0700200 size_t minRowBytes = static_cast<size_t>(info.width() * info.bytesPerPixel());
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000201 REPORTER_ASSERT(fReporter, rowBytes >= minRowBytes);
202 if ((NULL == pixels)
203 || (fType != kSucceedGetPixels_TestType)
reede5ea5002014-09-03 11:54:58 -0700204 || (info.colorType() != kN32_SkColorType)) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000205 return false;
206 }
207 char* bytePtr = static_cast<char*>(pixels);
reede5ea5002014-09-03 11:54:58 -0700208 for (int y = 0; y < info.height(); ++y) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000209 sk_memset32(reinterpret_cast<SkColor*>(bytePtr),
reede5ea5002014-09-03 11:54:58 -0700210 TestImageGenerator::Color(), info.width());
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000211 bytePtr += rowBytes;
212 }
213 return true;
214 }
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000215
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000216private:
217 const TestType fType;
218 skiatest::Reporter* const fReporter;
219};
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000220
tfarina@chromium.org58674812014-01-21 23:39:22 +0000221static void check_test_image_generator_bitmap(skiatest::Reporter* reporter,
222 const SkBitmap& bm) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000223 REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width());
224 REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height());
225 SkAutoLockPixels autoLockPixels(bm);
bsalomon49f085d2014-09-05 13:34:00 -0700226 REPORTER_ASSERT(reporter, bm.getPixels());
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000227 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
241enum PixelRefType {
242 kSkCaching_PixelRefType,
243 kSkDiscardable_PixelRefType,
244 kLast_PixelRefType = kSkDiscardable_PixelRefType
245};
tfarina@chromium.org58674812014-01-21 23:39:22 +0000246
247static void check_pixelref(TestImageGenerator::TestType type,
248 skiatest::Reporter* reporter,
249 PixelRefType pixelRefType,
250 SkDiscardableMemory::Factory* factory) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000251 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) {
reed011f39a2014-08-28 13:35:23 -0700258 // Ignore factory; use global cache.
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000259 success = SkCachingPixelRef::Install(gen.detach(), &lazy);
260 } else {
halcanary@google.comedd370f2013-12-10 21:11:12 +0000261 success = SkInstallDiscardablePixelRef(gen.detach(), &lazy, factory);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000262 }
263 REPORTER_ASSERT(reporter, success
264 == (TestImageGenerator::kFailGetInfo_TestType != type));
265 if (TestImageGenerator::kSucceedGetPixels_TestType == type) {
tfarina@chromium.org58674812014-01-21 23:39:22 +0000266 check_test_image_generator_bitmap(reporter, lazy);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000267 } else if (TestImageGenerator::kFailGetPixels_TestType == type) {
268 SkAutoLockPixels autoLockPixels(lazy);
269 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
270 }
271}
reed@google.com1d0654f2013-12-12 22:37:32 +0000272
273// new/lock/delete is an odd pattern for a pixelref, but it needs to not assert
274static void test_newlockdelete(skiatest::Reporter* reporter) {
275 SkBitmap bm;
276 SkImageGenerator* ig = new TestImageGenerator(
tfarina@chromium.org58674812014-01-21 23:39:22 +0000277 TestImageGenerator::kSucceedGetPixels_TestType, reporter);
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +0000278 SkInstallDiscardablePixelRef(ig, &bm);
reed@google.com1d0654f2013-12-12 22:37:32 +0000279 bm.pixelRef()->lockPixels();
280}
281
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000282/**
283 * This tests the basic functionality of SkDiscardablePixelRef with a
284 * basic SkImageGenerator implementation and several
285 * SkDiscardableMemory::Factory choices.
286 */
287DEF_TEST(DiscardableAndCachingPixelRef, reporter) {
reed@google.com1d0654f2013-12-12 22:37:32 +0000288 test_newlockdelete(reporter);
289
tfarina@chromium.org58674812014-01-21 23:39:22 +0000290 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.com2c7c7ee2013-12-05 18:31:42 +0000296
tfarina@chromium.org58674812014-01-21 23:39:22 +0000297 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.com2c7c7ee2013-12-05 18:31:42 +0000303
304 SkAutoTUnref<SkDiscardableMemoryPool> pool(
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000305 SkDiscardableMemoryPool::Create(1, NULL));
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000306 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
tfarina@chromium.org58674812014-01-21 23:39:22 +0000307 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
308 reporter, kSkDiscardable_PixelRefType, pool);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000309 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
tfarina@chromium.org58674812014-01-21 23:39:22 +0000310 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
311 reporter, kSkDiscardable_PixelRefType, pool);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000312 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
313
314 SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool();
halcanary@google.combc55eec2013-12-10 18:33:07 +0000315 // 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.org58674812014-01-21 23:39:22 +0000318 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
319 reporter, kSkDiscardable_PixelRefType, globalPool);
320 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
321 reporter, kSkDiscardable_PixelRefType, globalPool);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000322}
halcanaryea4673f2014-08-18 08:27:09 -0700323
324////////////////////////////////////////////////////////////////////////////////
325
326DEF_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;
reed84825042014-09-02 12:50:45 -0700349 bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::Height());
halcanaryea4673f2014-08-18 08:27:09 -0700350 SkCanvas canvas(bitmap);
351 const SkColor kDefaultColor = 0xffabcdef;
352 canvas.clear(kDefaultColor);
353 image->draw(&canvas, 0, 0, NULL);
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}