blob: c69cd969f569c6fbd4d736015dfcc439ad1a67c5 [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.com2c7c7ee2013-12-05 18:31:42 +000012#include "SkDiscardableMemoryPool.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000013#include "SkImageDecoder.h"
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +000014#include "SkImageGeneratorPriv.h"
reed011f39a2014-08-28 13:35:23 -070015#include "SkResourceCache.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000016#include "SkStream.h"
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000017#include "SkUtils.h"
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000018
commit-bot@chromium.org75854792013-10-29 19:55:00 +000019#include "Test.h"
20
commit-bot@chromium.org75854792013-10-29 19:55:00 +000021/**
22 * Fill this bitmap with some color.
23 */
24static void make_test_image(SkBitmap* bm) {
reed6c225732014-06-09 19:52:07 -070025 const int W = 50, H = 50;
26 bm->allocN32Pixels(W, H);
commit-bot@chromium.org75854792013-10-29 19:55:00 +000027 bm->eraseColor(SK_ColorBLACK);
28 SkCanvas canvas(*bm);
29 SkPaint paint;
30 paint.setColor(SK_ColorBLUE);
31 canvas.drawRectCoords(0, 0, SkIntToScalar(W/2),
32 SkIntToScalar(H/2), paint);
33 paint.setColor(SK_ColorWHITE);
34 canvas.drawRectCoords(SkIntToScalar(W/2), SkIntToScalar(H/2),
35 SkIntToScalar(W), SkIntToScalar(H), paint);
36}
37
38/**
39 * encode this bitmap into some data via SkImageEncoder
40 */
41static SkData* create_data_from_bitmap(const SkBitmap& bm,
42 SkImageEncoder::Type type) {
43 SkDynamicMemoryWStream stream;
44 if (SkImageEncoder::EncodeStream(&stream, bm, type, 100)) {
45 return stream.copyToData();
46 }
47 return NULL;
48}
49
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000050////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org75854792013-10-29 19:55:00 +000051
52static void compare_bitmaps(skiatest::Reporter* reporter,
53 const SkBitmap& b1, const SkBitmap& b2,
54 bool pixelPerfect = true) {
55 REPORTER_ASSERT(reporter, b1.empty() == b2.empty());
56 REPORTER_ASSERT(reporter, b1.width() == b2.width());
57 REPORTER_ASSERT(reporter, b1.height() == b2.height());
58 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
59 SkAutoLockPixels autoLockPixels1(b1);
60 SkAutoLockPixels autoLockPixels2(b2);
61 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
62 if (b1.isNull() || b1.empty()) {
63 return;
64 }
bsalomon49f085d2014-09-05 13:34:00 -070065 REPORTER_ASSERT(reporter, b1.getPixels());
66 REPORTER_ASSERT(reporter, b2.getPixels());
commit-bot@chromium.org75854792013-10-29 19:55:00 +000067 if ((!(b1.getPixels())) || (!(b2.getPixels()))) {
68 return;
69 }
70 if ((b1.width() != b2.width()) ||
71 (b1.height() != b2.height())) {
72 return;
73 }
74 if (!pixelPerfect) {
75 return;
76 }
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000077
commit-bot@chromium.org75854792013-10-29 19:55:00 +000078 int pixelErrors = 0;
79 for (int y = 0; y < b2.height(); ++y) {
80 for (int x = 0; x < b2.width(); ++x) {
81 if (b1.getColor(x, y) != b2.getColor(x, y)) {
82 ++pixelErrors;
83 }
84 }
85 }
86 REPORTER_ASSERT(reporter, 0 == pixelErrors);
87}
88
halcanary@google.com36d08c52013-12-05 14:00:03 +000089typedef bool (*InstallEncoded)(SkData* encoded, SkBitmap* dst);
90
commit-bot@chromium.org75854792013-10-29 19:55:00 +000091/**
halcanary@google.com36d08c52013-12-05 14:00:03 +000092 This function tests three differently encoded images against the
93 original bitmap */
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000094static void test_three_encodings(skiatest::Reporter* reporter,
halcanary@google.com36d08c52013-12-05 14:00:03 +000095 InstallEncoded install) {
commit-bot@chromium.org75854792013-10-29 19:55:00 +000096 SkBitmap original;
97 make_test_image(&original);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000098 REPORTER_ASSERT(reporter, !original.empty());
99 REPORTER_ASSERT(reporter, !original.isNull());
100 if (original.empty() || original.isNull()) {
101 return;
102 }
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000103 static const SkImageEncoder::Type types[] = {
104 SkImageEncoder::kPNG_Type,
105 SkImageEncoder::kJPEG_Type,
106 SkImageEncoder::kWEBP_Type
107 };
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000108 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) {
109 SkImageEncoder::Type type = types[i];
110 SkAutoDataUnref encoded(create_data_from_bitmap(original, type));
111 REPORTER_ASSERT(reporter, encoded.get() != NULL);
halcanary@google.com36d08c52013-12-05 14:00:03 +0000112 if (NULL == encoded.get()) {
113 continue;
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000114 }
halcanary@google.com36d08c52013-12-05 14:00:03 +0000115 SkBitmap lazy;
116 bool installSuccess = install(encoded.get(), &lazy);
117 REPORTER_ASSERT(reporter, installSuccess);
118 if (!installSuccess) {
119 continue;
120 }
121 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
122 {
123 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
bsalomon49f085d2014-09-05 13:34:00 -0700124 REPORTER_ASSERT(reporter, lazy.getPixels());
halcanary@google.com36d08c52013-12-05 14:00:03 +0000125 if (NULL == lazy.getPixels()) {
126 continue;
127 }
128 }
129 // pixels should be gone!
130 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
131 {
132 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
bsalomon49f085d2014-09-05 13:34:00 -0700133 REPORTER_ASSERT(reporter, lazy.getPixels());
halcanary@google.com36d08c52013-12-05 14:00:03 +0000134 if (NULL == lazy.getPixels()) {
135 continue;
136 }
137 }
138 bool comparePixels = (SkImageEncoder::kPNG_Type == type);
139 compare_bitmaps(reporter, original, lazy, comparePixels);
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000140 }
141}
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000142
halcanary@google.com36d08c52013-12-05 14:00:03 +0000143////////////////////////////////////////////////////////////////////////////////
halcanary@google.comdcfebfa2013-12-05 14:18:07 +0000144static bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) {
reed1c846342015-07-09 11:47:36 -0700145 return SkCachingPixelRef::Install(SkImageGenerator::NewFromEncoded(encoded), dst);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000146}
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000147static bool install_skDiscardablePixelRef(SkData* encoded, SkBitmap* dst) {
148 // Use system-default discardable memory.
reed5965c8a2015-01-07 18:04:45 -0800149 return SkInstallDiscardablePixelRef(encoded, dst);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000150}
halcanary@google.comad04eb42013-11-21 15:32:08 +0000151
halcanary@google.com36d08c52013-12-05 14:00:03 +0000152////////////////////////////////////////////////////////////////////////////////
153/**
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000154 * This checks to see that a SkCachingPixelRef and a
155 * SkDiscardablePixelRef works as advertised with a
156 * SkDecodingImageGenerator.
halcanary@google.com36d08c52013-12-05 14:00:03 +0000157 */
halcanary@google.comad04eb42013-11-21 15:32:08 +0000158DEF_TEST(DecodingImageGenerator, reporter) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000159 test_three_encodings(reporter, install_skCachingPixelRef);
160 test_three_encodings(reporter, install_skDiscardablePixelRef);
halcanary@google.comad04eb42013-11-21 15:32:08 +0000161}
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000162
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000163class TestImageGenerator : public SkImageGenerator {
164public:
165 enum TestType {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000166 kFailGetPixels_TestType,
167 kSucceedGetPixels_TestType,
168 kLast_TestType = kSucceedGetPixels_TestType
169 };
170 static int Width() { return 10; }
171 static int Height() { return 10; }
halcanaryea4673f2014-08-18 08:27:09 -0700172 static uint32_t Color() { return 0xff123456; }
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000173 TestImageGenerator(TestType type, skiatest::Reporter* reporter)
reed3ef71e32015-03-19 08:31:14 -0700174 : INHERITED(GetMyInfo()), fType(type), fReporter(reporter) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000175 SkASSERT((fType <= kLast_TestType) && (fType >= 0));
176 }
tfarina@chromium.org58674812014-01-21 23:39:22 +0000177 virtual ~TestImageGenerator() { }
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000178
179protected:
reed3ef71e32015-03-19 08:31:14 -0700180 static SkImageInfo GetMyInfo() {
181 return SkImageInfo::MakeN32(TestImageGenerator::Width(), TestImageGenerator::Height(),
182 kOpaque_SkAlphaType);
183 }
184
scroggo5315fd42015-07-09 09:08:00 -0700185 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
186 SkPMColor ctable[], int* ctableCount) override {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000187 REPORTER_ASSERT(fReporter, pixels != NULL);
scroggo08649082015-02-13 11:13:34 -0800188 REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes());
189 if (fType != kSucceedGetPixels_TestType) {
scroggo5315fd42015-07-09 09:08:00 -0700190 return false;
scroggo08649082015-02-13 11:13:34 -0800191 }
192 if (info.colorType() != kN32_SkColorType) {
scroggo5315fd42015-07-09 09:08:00 -0700193 return false;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000194 }
195 char* bytePtr = static_cast<char*>(pixels);
reede5ea5002014-09-03 11:54:58 -0700196 for (int y = 0; y < info.height(); ++y) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000197 sk_memset32(reinterpret_cast<SkColor*>(bytePtr),
reede5ea5002014-09-03 11:54:58 -0700198 TestImageGenerator::Color(), info.width());
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000199 bytePtr += rowBytes;
200 }
scroggo5315fd42015-07-09 09:08:00 -0700201 return true;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000202 }
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000203
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000204private:
205 const TestType fType;
206 skiatest::Reporter* const fReporter;
reed3ef71e32015-03-19 08:31:14 -0700207
208 typedef SkImageGenerator INHERITED;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000209};
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000210
tfarina@chromium.org58674812014-01-21 23:39:22 +0000211static void check_test_image_generator_bitmap(skiatest::Reporter* reporter,
212 const SkBitmap& bm) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000213 REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width());
214 REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height());
215 SkAutoLockPixels autoLockPixels(bm);
bsalomon49f085d2014-09-05 13:34:00 -0700216 REPORTER_ASSERT(reporter, bm.getPixels());
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000217 if (NULL == bm.getPixels()) {
218 return;
219 }
220 int errors = 0;
221 for (int y = 0; y < bm.height(); ++y) {
222 for (int x = 0; x < bm.width(); ++x) {
223 if (TestImageGenerator::Color() != *bm.getAddr32(x, y)) {
224 ++errors;
225 }
226 }
227 }
228 REPORTER_ASSERT(reporter, 0 == errors);
229}
230
231enum PixelRefType {
232 kSkCaching_PixelRefType,
233 kSkDiscardable_PixelRefType,
234 kLast_PixelRefType = kSkDiscardable_PixelRefType
235};
tfarina@chromium.org58674812014-01-21 23:39:22 +0000236
237static void check_pixelref(TestImageGenerator::TestType type,
238 skiatest::Reporter* reporter,
239 PixelRefType pixelRefType,
240 SkDiscardableMemory::Factory* factory) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000241 SkASSERT((pixelRefType >= 0) && (pixelRefType <= kLast_PixelRefType));
242 SkAutoTDelete<SkImageGenerator> gen(SkNEW_ARGS(TestImageGenerator,
243 (type, reporter)));
244 REPORTER_ASSERT(reporter, gen.get() != NULL);
245 SkBitmap lazy;
246 bool success;
247 if (kSkCaching_PixelRefType == pixelRefType) {
reed011f39a2014-08-28 13:35:23 -0700248 // Ignore factory; use global cache.
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000249 success = SkCachingPixelRef::Install(gen.detach(), &lazy);
250 } else {
reed871872f2015-06-22 12:48:26 -0700251 success = SkInstallDiscardablePixelRef(gen.detach(), NULL, &lazy, factory);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000252 }
reed3ef71e32015-03-19 08:31:14 -0700253 REPORTER_ASSERT(reporter, success);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000254 if (TestImageGenerator::kSucceedGetPixels_TestType == type) {
tfarina@chromium.org58674812014-01-21 23:39:22 +0000255 check_test_image_generator_bitmap(reporter, lazy);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000256 } else if (TestImageGenerator::kFailGetPixels_TestType == type) {
257 SkAutoLockPixels autoLockPixels(lazy);
258 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
259 }
260}
reed@google.com1d0654f2013-12-12 22:37:32 +0000261
262// new/lock/delete is an odd pattern for a pixelref, but it needs to not assert
263static void test_newlockdelete(skiatest::Reporter* reporter) {
reedab865772015-05-21 06:29:05 -0700264#ifdef SK_SUPPORT_LEGACY_UNBALANCED_PIXELREF_LOCKCOUNT
reed@google.com1d0654f2013-12-12 22:37:32 +0000265 SkBitmap bm;
266 SkImageGenerator* ig = new TestImageGenerator(
tfarina@chromium.org58674812014-01-21 23:39:22 +0000267 TestImageGenerator::kSucceedGetPixels_TestType, reporter);
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +0000268 SkInstallDiscardablePixelRef(ig, &bm);
reed@google.com1d0654f2013-12-12 22:37:32 +0000269 bm.pixelRef()->lockPixels();
reedab865772015-05-21 06:29:05 -0700270#endif
reed@google.com1d0654f2013-12-12 22:37:32 +0000271}
272
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000273/**
274 * This tests the basic functionality of SkDiscardablePixelRef with a
275 * basic SkImageGenerator implementation and several
276 * SkDiscardableMemory::Factory choices.
277 */
278DEF_TEST(DiscardableAndCachingPixelRef, reporter) {
reed@google.com1d0654f2013-12-12 22:37:32 +0000279 test_newlockdelete(reporter);
280
tfarina@chromium.org58674812014-01-21 23:39:22 +0000281 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
282 reporter, kSkCaching_PixelRefType, NULL);
283 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
284 reporter, kSkCaching_PixelRefType, NULL);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000285
tfarina@chromium.org58674812014-01-21 23:39:22 +0000286 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
287 reporter, kSkDiscardable_PixelRefType, NULL);
288 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
289 reporter, kSkDiscardable_PixelRefType, NULL);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000290
291 SkAutoTUnref<SkDiscardableMemoryPool> pool(
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000292 SkDiscardableMemoryPool::Create(1, NULL));
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000293 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
tfarina@chromium.org58674812014-01-21 23:39:22 +0000294 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
295 reporter, kSkDiscardable_PixelRefType, pool);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000296 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
tfarina@chromium.org58674812014-01-21 23:39:22 +0000297 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
298 reporter, kSkDiscardable_PixelRefType, pool);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000299 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
300
301 SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool();
halcanary@google.combc55eec2013-12-10 18:33:07 +0000302 // Only acts differently from NULL on a platform that has a
303 // default discardable memory implementation that differs from the
304 // global DM pool.
tfarina@chromium.org58674812014-01-21 23:39:22 +0000305 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
306 reporter, kSkDiscardable_PixelRefType, globalPool);
307 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
308 reporter, kSkDiscardable_PixelRefType, globalPool);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000309}
halcanaryea4673f2014-08-18 08:27:09 -0700310
311////////////////////////////////////////////////////////////////////////////////
312
313DEF_TEST(Image_NewFromGenerator, r) {
314 TestImageGenerator::TestType testTypes[] = {
halcanaryea4673f2014-08-18 08:27:09 -0700315 TestImageGenerator::kFailGetPixels_TestType,
316 TestImageGenerator::kSucceedGetPixels_TestType,
317 };
318 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) {
319 TestImageGenerator::TestType test = testTypes[i];
320 SkImageGenerator* gen = SkNEW_ARGS(TestImageGenerator, (test, r));
321 SkAutoTUnref<SkImage> image(SkImage::NewFromGenerator(gen));
halcanaryea4673f2014-08-18 08:27:09 -0700322 if (NULL == image.get()) {
323 ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed ["
324 SK_SIZE_T_SPECIFIER "]", i);
325 continue;
326 }
327 REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width());
328 REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height());
fmalitaddbbdda2015-08-20 08:47:26 -0700329 REPORTER_ASSERT(r, image->isLazyGenerated());
halcanaryea4673f2014-08-18 08:27:09 -0700330
331 SkBitmap bitmap;
reed84825042014-09-02 12:50:45 -0700332 bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::Height());
halcanaryea4673f2014-08-18 08:27:09 -0700333 SkCanvas canvas(bitmap);
334 const SkColor kDefaultColor = 0xffabcdef;
335 canvas.clear(kDefaultColor);
piotaixrb5fae932014-09-24 13:03:30 -0700336 canvas.drawImage(image, 0, 0, NULL);
halcanaryea4673f2014-08-18 08:27:09 -0700337 if (TestImageGenerator::kSucceedGetPixels_TestType == test) {
338 REPORTER_ASSERT(
339 r, TestImageGenerator::Color() == *bitmap.getAddr32(0, 0));
340 } else {
341 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0,0));
342 }
343 }
344}