blob: eff77e215da9ab68e8cfb47dd8e8ef3430756e7a [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"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000016#include "SkScaledImageCache.h"
17#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) {
26 static const int W = 50, H = 50;
27 static const SkBitmap::Config config = SkBitmap::kARGB_8888_Config;
28 bm->setConfig(config, W, H);
29 bm->allocPixels();
30 bm->eraseColor(SK_ColorBLACK);
31 SkCanvas canvas(*bm);
32 SkPaint paint;
33 paint.setColor(SK_ColorBLUE);
34 canvas.drawRectCoords(0, 0, SkIntToScalar(W/2),
35 SkIntToScalar(H/2), paint);
36 paint.setColor(SK_ColorWHITE);
37 canvas.drawRectCoords(SkIntToScalar(W/2), SkIntToScalar(H/2),
38 SkIntToScalar(W), SkIntToScalar(H), paint);
39}
40
41/**
42 * encode this bitmap into some data via SkImageEncoder
43 */
44static SkData* create_data_from_bitmap(const SkBitmap& bm,
45 SkImageEncoder::Type type) {
46 SkDynamicMemoryWStream stream;
47 if (SkImageEncoder::EncodeStream(&stream, bm, type, 100)) {
48 return stream.copyToData();
49 }
50 return NULL;
51}
52
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000053////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org75854792013-10-29 19:55:00 +000054
55static void compare_bitmaps(skiatest::Reporter* reporter,
56 const SkBitmap& b1, const SkBitmap& b2,
57 bool pixelPerfect = true) {
58 REPORTER_ASSERT(reporter, b1.empty() == b2.empty());
59 REPORTER_ASSERT(reporter, b1.width() == b2.width());
60 REPORTER_ASSERT(reporter, b1.height() == b2.height());
61 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
62 SkAutoLockPixels autoLockPixels1(b1);
63 SkAutoLockPixels autoLockPixels2(b2);
64 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
65 if (b1.isNull() || b1.empty()) {
66 return;
67 }
68 REPORTER_ASSERT(reporter, NULL != b1.getPixels());
69 REPORTER_ASSERT(reporter, NULL != b2.getPixels());
70 if ((!(b1.getPixels())) || (!(b2.getPixels()))) {
71 return;
72 }
73 if ((b1.width() != b2.width()) ||
74 (b1.height() != b2.height())) {
75 return;
76 }
77 if (!pixelPerfect) {
78 return;
79 }
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000080
commit-bot@chromium.org75854792013-10-29 19:55:00 +000081 int pixelErrors = 0;
82 for (int y = 0; y < b2.height(); ++y) {
83 for (int x = 0; x < b2.width(); ++x) {
84 if (b1.getColor(x, y) != b2.getColor(x, y)) {
85 ++pixelErrors;
86 }
87 }
88 }
89 REPORTER_ASSERT(reporter, 0 == pixelErrors);
90}
91
halcanary@google.com36d08c52013-12-05 14:00:03 +000092typedef bool (*InstallEncoded)(SkData* encoded, SkBitmap* dst);
93
commit-bot@chromium.org75854792013-10-29 19:55:00 +000094/**
halcanary@google.com36d08c52013-12-05 14:00:03 +000095 This function tests three differently encoded images against the
96 original bitmap */
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000097static void test_three_encodings(skiatest::Reporter* reporter,
halcanary@google.com36d08c52013-12-05 14:00:03 +000098 InstallEncoded install) {
commit-bot@chromium.org75854792013-10-29 19:55:00 +000099 SkBitmap original;
100 make_test_image(&original);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000101 REPORTER_ASSERT(reporter, !original.empty());
102 REPORTER_ASSERT(reporter, !original.isNull());
103 if (original.empty() || original.isNull()) {
104 return;
105 }
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000106 static const SkImageEncoder::Type types[] = {
107 SkImageEncoder::kPNG_Type,
108 SkImageEncoder::kJPEG_Type,
109 SkImageEncoder::kWEBP_Type
110 };
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000111 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) {
112 SkImageEncoder::Type type = types[i];
113 SkAutoDataUnref encoded(create_data_from_bitmap(original, type));
114 REPORTER_ASSERT(reporter, encoded.get() != NULL);
halcanary@google.com36d08c52013-12-05 14:00:03 +0000115 if (NULL == encoded.get()) {
116 continue;
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000117 }
halcanary@google.com36d08c52013-12-05 14:00:03 +0000118 SkBitmap lazy;
119 bool installSuccess = install(encoded.get(), &lazy);
120 REPORTER_ASSERT(reporter, installSuccess);
121 if (!installSuccess) {
122 continue;
123 }
124 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
125 {
126 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
127 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
128 if (NULL == lazy.getPixels()) {
129 continue;
130 }
131 }
132 // pixels should be gone!
133 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
134 {
135 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
136 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
137 if (NULL == lazy.getPixels()) {
138 continue;
139 }
140 }
141 bool comparePixels = (SkImageEncoder::kPNG_Type == type);
142 compare_bitmaps(reporter, original, lazy, comparePixels);
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000143 }
144}
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000145
halcanary@google.com36d08c52013-12-05 14:00:03 +0000146////////////////////////////////////////////////////////////////////////////////
halcanary@google.comdcfebfa2013-12-05 14:18:07 +0000147static bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) {
halcanary@google.com36d08c52013-12-05 14:00:03 +0000148 return SkCachingPixelRef::Install(
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000149 SkDecodingImageGenerator::Create(
150 encoded, SkDecodingImageGenerator::Options()), dst);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000151}
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000152static bool install_skDiscardablePixelRef(SkData* encoded, SkBitmap* dst) {
153 // Use system-default discardable memory.
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000154 return SkInstallDiscardablePixelRef(
155 SkDecodingImageGenerator::Create(
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +0000156 encoded, SkDecodingImageGenerator::Options()), dst);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000157}
halcanary@google.comad04eb42013-11-21 15:32:08 +0000158
halcanary@google.com36d08c52013-12-05 14:00:03 +0000159////////////////////////////////////////////////////////////////////////////////
160/**
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000161 * This checks to see that a SkCachingPixelRef and a
162 * SkDiscardablePixelRef works as advertised with a
163 * SkDecodingImageGenerator.
halcanary@google.com36d08c52013-12-05 14:00:03 +0000164 */
halcanary@google.comad04eb42013-11-21 15:32:08 +0000165DEF_TEST(DecodingImageGenerator, reporter) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000166 test_three_encodings(reporter, install_skCachingPixelRef);
167 test_three_encodings(reporter, install_skDiscardablePixelRef);
halcanary@google.comad04eb42013-11-21 15:32:08 +0000168}
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000169
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000170class TestImageGenerator : public SkImageGenerator {
171public:
172 enum TestType {
173 kFailGetInfo_TestType,
174 kFailGetPixels_TestType,
175 kSucceedGetPixels_TestType,
176 kLast_TestType = kSucceedGetPixels_TestType
177 };
178 static int Width() { return 10; }
179 static int Height() { return 10; }
180 static SkColor Color() { return SK_ColorCYAN; }
181 TestImageGenerator(TestType type, skiatest::Reporter* reporter)
182 : fType(type), fReporter(reporter) {
183 SkASSERT((fType <= kLast_TestType) && (fType >= 0));
184 }
tfarina@chromium.org58674812014-01-21 23:39:22 +0000185 virtual ~TestImageGenerator() { }
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000186
187protected:
188 virtual bool onGetInfo(SkImageInfo* info) SK_OVERRIDE {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000189 REPORTER_ASSERT(fReporter, NULL != info);
190 if ((NULL == info) || (kFailGetInfo_TestType == fType)) {
191 return false;
192 }
193 info->fWidth = TestImageGenerator::Width();
194 info->fHeight = TestImageGenerator::Height();
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000195 info->fColorType = kN32_SkColorType;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000196 info->fAlphaType = kOpaque_SkAlphaType;
197 return true;
198 }
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000199
200 virtual bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
201 SkPMColor ctable[], int* ctableCount) SK_OVERRIDE {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000202 REPORTER_ASSERT(fReporter, pixels != NULL);
203 size_t minRowBytes
204 = static_cast<size_t>(info.fWidth * info.bytesPerPixel());
205 REPORTER_ASSERT(fReporter, rowBytes >= minRowBytes);
206 if ((NULL == pixels)
207 || (fType != kSucceedGetPixels_TestType)
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000208 || (info.fColorType != kN32_SkColorType)) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000209 return false;
210 }
211 char* bytePtr = static_cast<char*>(pixels);
212 for (int y = 0; y < info.fHeight; ++y) {
213 sk_memset32(reinterpret_cast<SkColor*>(bytePtr),
214 TestImageGenerator::Color(), info.fWidth);
215 bytePtr += rowBytes;
216 }
217 return true;
218 }
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000219
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000220private:
221 const TestType fType;
222 skiatest::Reporter* const fReporter;
223};
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000224
tfarina@chromium.org58674812014-01-21 23:39:22 +0000225static void check_test_image_generator_bitmap(skiatest::Reporter* reporter,
226 const SkBitmap& bm) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000227 REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width());
228 REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height());
229 SkAutoLockPixels autoLockPixels(bm);
230 REPORTER_ASSERT(reporter, NULL != bm.getPixels());
231 if (NULL == bm.getPixels()) {
232 return;
233 }
234 int errors = 0;
235 for (int y = 0; y < bm.height(); ++y) {
236 for (int x = 0; x < bm.width(); ++x) {
237 if (TestImageGenerator::Color() != *bm.getAddr32(x, y)) {
238 ++errors;
239 }
240 }
241 }
242 REPORTER_ASSERT(reporter, 0 == errors);
243}
244
245enum PixelRefType {
246 kSkCaching_PixelRefType,
247 kSkDiscardable_PixelRefType,
248 kLast_PixelRefType = kSkDiscardable_PixelRefType
249};
tfarina@chromium.org58674812014-01-21 23:39:22 +0000250
251static void check_pixelref(TestImageGenerator::TestType type,
252 skiatest::Reporter* reporter,
253 PixelRefType pixelRefType,
254 SkDiscardableMemory::Factory* factory) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000255 SkASSERT((pixelRefType >= 0) && (pixelRefType <= kLast_PixelRefType));
256 SkAutoTDelete<SkImageGenerator> gen(SkNEW_ARGS(TestImageGenerator,
257 (type, reporter)));
258 REPORTER_ASSERT(reporter, gen.get() != NULL);
259 SkBitmap lazy;
260 bool success;
261 if (kSkCaching_PixelRefType == pixelRefType) {
262 // Ignore factory; use global SkScaledImageCache.
263 success = SkCachingPixelRef::Install(gen.detach(), &lazy);
264 } else {
halcanary@google.comedd370f2013-12-10 21:11:12 +0000265 success = SkInstallDiscardablePixelRef(gen.detach(), &lazy, factory);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000266 }
267 REPORTER_ASSERT(reporter, success
268 == (TestImageGenerator::kFailGetInfo_TestType != type));
269 if (TestImageGenerator::kSucceedGetPixels_TestType == type) {
tfarina@chromium.org58674812014-01-21 23:39:22 +0000270 check_test_image_generator_bitmap(reporter, lazy);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000271 } else if (TestImageGenerator::kFailGetPixels_TestType == type) {
272 SkAutoLockPixels autoLockPixels(lazy);
273 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
274 }
275}
reed@google.com1d0654f2013-12-12 22:37:32 +0000276
277// new/lock/delete is an odd pattern for a pixelref, but it needs to not assert
278static void test_newlockdelete(skiatest::Reporter* reporter) {
279 SkBitmap bm;
280 SkImageGenerator* ig = new TestImageGenerator(
tfarina@chromium.org58674812014-01-21 23:39:22 +0000281 TestImageGenerator::kSucceedGetPixels_TestType, reporter);
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +0000282 SkInstallDiscardablePixelRef(ig, &bm);
reed@google.com1d0654f2013-12-12 22:37:32 +0000283 bm.pixelRef()->lockPixels();
284}
285
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000286/**
287 * This tests the basic functionality of SkDiscardablePixelRef with a
288 * basic SkImageGenerator implementation and several
289 * SkDiscardableMemory::Factory choices.
290 */
291DEF_TEST(DiscardableAndCachingPixelRef, reporter) {
reed@google.com1d0654f2013-12-12 22:37:32 +0000292 test_newlockdelete(reporter);
293
tfarina@chromium.org58674812014-01-21 23:39:22 +0000294 check_pixelref(TestImageGenerator::kFailGetInfo_TestType,
295 reporter, kSkCaching_PixelRefType, NULL);
296 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
297 reporter, kSkCaching_PixelRefType, NULL);
298 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
299 reporter, kSkCaching_PixelRefType, NULL);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000300
tfarina@chromium.org58674812014-01-21 23:39:22 +0000301 check_pixelref(TestImageGenerator::kFailGetInfo_TestType,
302 reporter, kSkDiscardable_PixelRefType, NULL);
303 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
304 reporter, kSkDiscardable_PixelRefType, NULL);
305 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
306 reporter, kSkDiscardable_PixelRefType, NULL);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000307
308 SkAutoTUnref<SkDiscardableMemoryPool> pool(
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000309 SkDiscardableMemoryPool::Create(1, NULL));
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000310 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
tfarina@chromium.org58674812014-01-21 23:39:22 +0000311 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
312 reporter, kSkDiscardable_PixelRefType, pool);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000313 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
tfarina@chromium.org58674812014-01-21 23:39:22 +0000314 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
315 reporter, kSkDiscardable_PixelRefType, pool);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000316 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
317
318 SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool();
halcanary@google.combc55eec2013-12-10 18:33:07 +0000319 // Only acts differently from NULL on a platform that has a
320 // default discardable memory implementation that differs from the
321 // global DM pool.
tfarina@chromium.org58674812014-01-21 23:39:22 +0000322 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
323 reporter, kSkDiscardable_PixelRefType, globalPool);
324 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
325 reporter, kSkDiscardable_PixelRefType, globalPool);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000326}