blob: f92b1de1bf4a7b2b0868d9fd3e07905054f5f1e5 [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 "SkDiscardablePixelRef.h"
13#include "SkDiscardableMemoryPool.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000014#include "SkImageDecoder.h"
halcanary@google.com36d08c52013-12-05 14:00:03 +000015#include "SkCachingPixelRef.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"
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000021#include "TestClassDef.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000022
commit-bot@chromium.org75854792013-10-29 19:55:00 +000023/**
24 * Fill this bitmap with some color.
25 */
26static void make_test_image(SkBitmap* bm) {
27 static const int W = 50, H = 50;
28 static const SkBitmap::Config config = SkBitmap::kARGB_8888_Config;
29 bm->setConfig(config, W, H);
30 bm->allocPixels();
31 bm->eraseColor(SK_ColorBLACK);
32 SkCanvas canvas(*bm);
33 SkPaint paint;
34 paint.setColor(SK_ColorBLUE);
35 canvas.drawRectCoords(0, 0, SkIntToScalar(W/2),
36 SkIntToScalar(H/2), paint);
37 paint.setColor(SK_ColorWHITE);
38 canvas.drawRectCoords(SkIntToScalar(W/2), SkIntToScalar(H/2),
39 SkIntToScalar(W), SkIntToScalar(H), paint);
40}
41
42/**
43 * encode this bitmap into some data via SkImageEncoder
44 */
45static SkData* create_data_from_bitmap(const SkBitmap& bm,
46 SkImageEncoder::Type type) {
47 SkDynamicMemoryWStream stream;
48 if (SkImageEncoder::EncodeStream(&stream, bm, type, 100)) {
49 return stream.copyToData();
50 }
51 return NULL;
52}
53
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000054////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org75854792013-10-29 19:55:00 +000055
56static void compare_bitmaps(skiatest::Reporter* reporter,
57 const SkBitmap& b1, const SkBitmap& b2,
58 bool pixelPerfect = true) {
59 REPORTER_ASSERT(reporter, b1.empty() == b2.empty());
60 REPORTER_ASSERT(reporter, b1.width() == b2.width());
61 REPORTER_ASSERT(reporter, b1.height() == b2.height());
62 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
63 SkAutoLockPixels autoLockPixels1(b1);
64 SkAutoLockPixels autoLockPixels2(b2);
65 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
66 if (b1.isNull() || b1.empty()) {
67 return;
68 }
69 REPORTER_ASSERT(reporter, NULL != b1.getPixels());
70 REPORTER_ASSERT(reporter, NULL != b2.getPixels());
71 if ((!(b1.getPixels())) || (!(b2.getPixels()))) {
72 return;
73 }
74 if ((b1.width() != b2.width()) ||
75 (b1.height() != b2.height())) {
76 return;
77 }
78 if (!pixelPerfect) {
79 return;
80 }
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000081
commit-bot@chromium.org75854792013-10-29 19:55:00 +000082 int pixelErrors = 0;
83 for (int y = 0; y < b2.height(); ++y) {
84 for (int x = 0; x < b2.width(); ++x) {
85 if (b1.getColor(x, y) != b2.getColor(x, y)) {
86 ++pixelErrors;
87 }
88 }
89 }
90 REPORTER_ASSERT(reporter, 0 == pixelErrors);
91}
92
halcanary@google.com36d08c52013-12-05 14:00:03 +000093typedef bool (*InstallEncoded)(SkData* encoded, SkBitmap* dst);
94
commit-bot@chromium.org75854792013-10-29 19:55:00 +000095/**
halcanary@google.com36d08c52013-12-05 14:00:03 +000096 This function tests three differently encoded images against the
97 original bitmap */
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000098static void test_three_encodings(skiatest::Reporter* reporter,
halcanary@google.com36d08c52013-12-05 14:00:03 +000099 InstallEncoded install) {
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000100 SkBitmap original;
101 make_test_image(&original);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000102 REPORTER_ASSERT(reporter, !original.empty());
103 REPORTER_ASSERT(reporter, !original.isNull());
104 if (original.empty() || original.isNull()) {
105 return;
106 }
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000107 static const SkImageEncoder::Type types[] = {
108 SkImageEncoder::kPNG_Type,
109 SkImageEncoder::kJPEG_Type,
110 SkImageEncoder::kWEBP_Type
111 };
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000112 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) {
113 SkImageEncoder::Type type = types[i];
114 SkAutoDataUnref encoded(create_data_from_bitmap(original, type));
115 REPORTER_ASSERT(reporter, encoded.get() != NULL);
halcanary@google.com36d08c52013-12-05 14:00:03 +0000116 if (NULL == encoded.get()) {
117 continue;
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000118 }
halcanary@google.com36d08c52013-12-05 14:00:03 +0000119 SkBitmap lazy;
120 bool installSuccess = install(encoded.get(), &lazy);
121 REPORTER_ASSERT(reporter, installSuccess);
122 if (!installSuccess) {
123 continue;
124 }
125 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
126 {
127 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
128 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
129 if (NULL == lazy.getPixels()) {
130 continue;
131 }
132 }
133 // pixels should be gone!
134 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
135 {
136 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
137 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
138 if (NULL == lazy.getPixels()) {
139 continue;
140 }
141 }
142 bool comparePixels = (SkImageEncoder::kPNG_Type == type);
143 compare_bitmaps(reporter, original, lazy, comparePixels);
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000144 }
145}
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000146
halcanary@google.com36d08c52013-12-05 14:00:03 +0000147////////////////////////////////////////////////////////////////////////////////
halcanary@google.comdcfebfa2013-12-05 14:18:07 +0000148static bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) {
halcanary@google.com36d08c52013-12-05 14:00:03 +0000149 return SkCachingPixelRef::Install(
150 SkNEW_ARGS(SkDecodingImageGenerator, (encoded)), 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.
154 return SkDecodingImageGenerator::Install(encoded, dst, NULL);
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
168////////////////////////////////////////////////////////////////////////////////
169namespace {
170class 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 }
185 ~TestImageGenerator() { }
186 bool getInfo(SkImageInfo* info) SK_OVERRIDE {
187 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();
193 info->fColorType = kPMColor_SkColorType;
194 info->fAlphaType = kOpaque_SkAlphaType;
195 return true;
196 }
197 bool getPixels(const SkImageInfo& info,
198 void* pixels,
199 size_t rowBytes) SK_OVERRIDE {
200 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)
206 || (info.fColorType != kPMColor_SkColorType)) {
207 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 }
217private:
218 const TestType fType;
219 skiatest::Reporter* const fReporter;
220};
221void CheckTestImageGeneratorBitmap(skiatest::Reporter* reporter,
222 const SkBitmap& bm) {
223 REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width());
224 REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height());
225 SkAutoLockPixels autoLockPixels(bm);
226 REPORTER_ASSERT(reporter, NULL != bm.getPixels());
227 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};
246void CheckPixelRef(TestImageGenerator::TestType type,
247 skiatest::Reporter* reporter,
248 PixelRefType pixelRefType,
249 SkDiscardableMemory::Factory* factory) {
250 SkASSERT((pixelRefType >= 0) && (pixelRefType <= kLast_PixelRefType));
251 SkAutoTDelete<SkImageGenerator> gen(SkNEW_ARGS(TestImageGenerator,
252 (type, reporter)));
253 REPORTER_ASSERT(reporter, gen.get() != NULL);
254 SkBitmap lazy;
255 bool success;
256 if (kSkCaching_PixelRefType == pixelRefType) {
257 // Ignore factory; use global SkScaledImageCache.
258 success = SkCachingPixelRef::Install(gen.detach(), &lazy);
259 } else {
260 success = SkDiscardablePixelRef::Install(gen.detach(), &lazy, factory);
261 }
262 REPORTER_ASSERT(reporter, success
263 == (TestImageGenerator::kFailGetInfo_TestType != type));
264 if (TestImageGenerator::kSucceedGetPixels_TestType == type) {
265 CheckTestImageGeneratorBitmap(reporter, lazy);
266 } else if (TestImageGenerator::kFailGetPixels_TestType == type) {
267 SkAutoLockPixels autoLockPixels(lazy);
268 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
269 }
270}
271} // namespace
272/**
273 * This tests the basic functionality of SkDiscardablePixelRef with a
274 * basic SkImageGenerator implementation and several
275 * SkDiscardableMemory::Factory choices.
276 */
277DEF_TEST(DiscardableAndCachingPixelRef, reporter) {
278 CheckPixelRef(TestImageGenerator::kFailGetInfo_TestType,
279 reporter, kSkCaching_PixelRefType, NULL);
280 CheckPixelRef(TestImageGenerator::kFailGetPixels_TestType,
281 reporter, kSkCaching_PixelRefType, NULL);
282 CheckPixelRef(TestImageGenerator::kSucceedGetPixels_TestType,
283 reporter, kSkCaching_PixelRefType, NULL);
284
285 CheckPixelRef(TestImageGenerator::kFailGetInfo_TestType,
286 reporter, kSkDiscardable_PixelRefType, NULL);
287 CheckPixelRef(TestImageGenerator::kFailGetPixels_TestType,
288 reporter, kSkDiscardable_PixelRefType, NULL);
289 CheckPixelRef(TestImageGenerator::kSucceedGetPixels_TestType,
290 reporter, kSkDiscardable_PixelRefType, NULL);
291
292 SkAutoTUnref<SkDiscardableMemoryPool> pool(
293 SkNEW_ARGS(SkDiscardableMemoryPool, (1, NULL)));
294 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
295 CheckPixelRef(TestImageGenerator::kFailGetPixels_TestType,
296 reporter, kSkDiscardable_PixelRefType, pool);
297 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
298 CheckPixelRef(TestImageGenerator::kSucceedGetPixels_TestType,
299 reporter, kSkDiscardable_PixelRefType, pool);
300 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
301
302 SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool();
303 CheckPixelRef(TestImageGenerator::kFailGetPixels_TestType,
304 reporter, kSkDiscardable_PixelRefType, globalPool);
305 CheckPixelRef(TestImageGenerator::kSucceedGetPixels_TestType,
306 reporter, kSkDiscardable_PixelRefType, globalPool);
307
308 // TODO(halcanary): When ashmem-backed SkDiscardableMemory lands,
309 // test that here (on platforms where it is availible).
310}
311////////////////////////////////////////////////////////////////////////////////
312