blob: 434724d0cf9532e4e83b805aa04ddd60d062cc9c [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.comad04eb42013-11-21 15:32:08 +000011#include "SkDecodingImageGenerator.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"
halcanary@google.com36d08c52013-12-05 14:00:03 +000014#include "SkCachingPixelRef.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000015#include "SkScaledImageCache.h"
16#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"
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000020#include "TestClassDef.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000021
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(
156 encoded, SkDecodingImageGenerator::Options()), dst, NULL);
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
170////////////////////////////////////////////////////////////////////////////////
171namespace {
172class TestImageGenerator : public SkImageGenerator {
173public:
174 enum TestType {
175 kFailGetInfo_TestType,
176 kFailGetPixels_TestType,
177 kSucceedGetPixels_TestType,
178 kLast_TestType = kSucceedGetPixels_TestType
179 };
180 static int Width() { return 10; }
181 static int Height() { return 10; }
182 static SkColor Color() { return SK_ColorCYAN; }
183 TestImageGenerator(TestType type, skiatest::Reporter* reporter)
184 : fType(type), fReporter(reporter) {
185 SkASSERT((fType <= kLast_TestType) && (fType >= 0));
186 }
187 ~TestImageGenerator() { }
188 bool getInfo(SkImageInfo* info) SK_OVERRIDE {
189 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();
195 info->fColorType = kPMColor_SkColorType;
196 info->fAlphaType = kOpaque_SkAlphaType;
197 return true;
198 }
199 bool getPixels(const SkImageInfo& info,
200 void* pixels,
201 size_t rowBytes) SK_OVERRIDE {
202 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)
208 || (info.fColorType != kPMColor_SkColorType)) {
209 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
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000225void CheckTestImageGeneratorBitmap(skiatest::Reporter* reporter,
226 const SkBitmap& bm) {
227 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};
250void CheckPixelRef(TestImageGenerator::TestType type,
251 skiatest::Reporter* reporter,
252 PixelRefType pixelRefType,
253 SkDiscardableMemory::Factory* factory) {
254 SkASSERT((pixelRefType >= 0) && (pixelRefType <= kLast_PixelRefType));
255 SkAutoTDelete<SkImageGenerator> gen(SkNEW_ARGS(TestImageGenerator,
256 (type, reporter)));
257 REPORTER_ASSERT(reporter, gen.get() != NULL);
258 SkBitmap lazy;
259 bool success;
260 if (kSkCaching_PixelRefType == pixelRefType) {
261 // Ignore factory; use global SkScaledImageCache.
262 success = SkCachingPixelRef::Install(gen.detach(), &lazy);
263 } else {
halcanary@google.comedd370f2013-12-10 21:11:12 +0000264 success = SkInstallDiscardablePixelRef(gen.detach(), &lazy, factory);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000265 }
266 REPORTER_ASSERT(reporter, success
267 == (TestImageGenerator::kFailGetInfo_TestType != type));
268 if (TestImageGenerator::kSucceedGetPixels_TestType == type) {
269 CheckTestImageGeneratorBitmap(reporter, lazy);
270 } else if (TestImageGenerator::kFailGetPixels_TestType == type) {
271 SkAutoLockPixels autoLockPixels(lazy);
272 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
273 }
274}
275} // namespace
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(
281 TestImageGenerator::kSucceedGetPixels_TestType,
282 reporter);
283 SkInstallDiscardablePixelRef(ig, &bm, NULL);
284 bm.pixelRef()->lockPixels();
285}
286
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000287/**
288 * This tests the basic functionality of SkDiscardablePixelRef with a
289 * basic SkImageGenerator implementation and several
290 * SkDiscardableMemory::Factory choices.
291 */
292DEF_TEST(DiscardableAndCachingPixelRef, reporter) {
reed@google.com1d0654f2013-12-12 22:37:32 +0000293 test_newlockdelete(reporter);
294
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000295 CheckPixelRef(TestImageGenerator::kFailGetInfo_TestType,
296 reporter, kSkCaching_PixelRefType, NULL);
297 CheckPixelRef(TestImageGenerator::kFailGetPixels_TestType,
298 reporter, kSkCaching_PixelRefType, NULL);
299 CheckPixelRef(TestImageGenerator::kSucceedGetPixels_TestType,
300 reporter, kSkCaching_PixelRefType, NULL);
301
302 CheckPixelRef(TestImageGenerator::kFailGetInfo_TestType,
303 reporter, kSkDiscardable_PixelRefType, NULL);
304 CheckPixelRef(TestImageGenerator::kFailGetPixels_TestType,
305 reporter, kSkDiscardable_PixelRefType, NULL);
306 CheckPixelRef(TestImageGenerator::kSucceedGetPixels_TestType,
307 reporter, kSkDiscardable_PixelRefType, NULL);
308
309 SkAutoTUnref<SkDiscardableMemoryPool> pool(
310 SkNEW_ARGS(SkDiscardableMemoryPool, (1, NULL)));
311 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
312 CheckPixelRef(TestImageGenerator::kFailGetPixels_TestType,
313 reporter, kSkDiscardable_PixelRefType, pool);
314 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
315 CheckPixelRef(TestImageGenerator::kSucceedGetPixels_TestType,
316 reporter, kSkDiscardable_PixelRefType, pool);
317 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
318
319 SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool();
halcanary@google.combc55eec2013-12-10 18:33:07 +0000320 // Only acts differently from NULL on a platform that has a
321 // default discardable memory implementation that differs from the
322 // global DM pool.
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000323 CheckPixelRef(TestImageGenerator::kFailGetPixels_TestType,
324 reporter, kSkDiscardable_PixelRefType, globalPool);
325 CheckPixelRef(TestImageGenerator::kSucceedGetPixels_TestType,
326 reporter, kSkDiscardable_PixelRefType, globalPool);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000327}
328////////////////////////////////////////////////////////////////////////////////