blob: b2ecb0f3ab0d7cf2abd4e5def7e8e4385627c66f [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.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"
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) {
25 static const int W = 50, H = 50;
26 static const SkBitmap::Config config = SkBitmap::kARGB_8888_Config;
27 bm->setConfig(config, W, H);
28 bm->allocPixels();
29 bm->eraseColor(SK_ColorBLACK);
30 SkCanvas canvas(*bm);
31 SkPaint paint;
32 paint.setColor(SK_ColorBLUE);
33 canvas.drawRectCoords(0, 0, SkIntToScalar(W/2),
34 SkIntToScalar(H/2), paint);
35 paint.setColor(SK_ColorWHITE);
36 canvas.drawRectCoords(SkIntToScalar(W/2), SkIntToScalar(H/2),
37 SkIntToScalar(W), SkIntToScalar(H), paint);
38}
39
40/**
41 * encode this bitmap into some data via SkImageEncoder
42 */
43static SkData* create_data_from_bitmap(const SkBitmap& bm,
44 SkImageEncoder::Type type) {
45 SkDynamicMemoryWStream stream;
46 if (SkImageEncoder::EncodeStream(&stream, bm, type, 100)) {
47 return stream.copyToData();
48 }
49 return NULL;
50}
51
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000052////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org75854792013-10-29 19:55:00 +000053
54static void compare_bitmaps(skiatest::Reporter* reporter,
55 const SkBitmap& b1, const SkBitmap& b2,
56 bool pixelPerfect = true) {
57 REPORTER_ASSERT(reporter, b1.empty() == b2.empty());
58 REPORTER_ASSERT(reporter, b1.width() == b2.width());
59 REPORTER_ASSERT(reporter, b1.height() == b2.height());
60 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
61 SkAutoLockPixels autoLockPixels1(b1);
62 SkAutoLockPixels autoLockPixels2(b2);
63 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
64 if (b1.isNull() || b1.empty()) {
65 return;
66 }
67 REPORTER_ASSERT(reporter, NULL != b1.getPixels());
68 REPORTER_ASSERT(reporter, NULL != b2.getPixels());
69 if ((!(b1.getPixels())) || (!(b2.getPixels()))) {
70 return;
71 }
72 if ((b1.width() != b2.width()) ||
73 (b1.height() != b2.height())) {
74 return;
75 }
76 if (!pixelPerfect) {
77 return;
78 }
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000079
commit-bot@chromium.org75854792013-10-29 19:55:00 +000080 int pixelErrors = 0;
81 for (int y = 0; y < b2.height(); ++y) {
82 for (int x = 0; x < b2.width(); ++x) {
83 if (b1.getColor(x, y) != b2.getColor(x, y)) {
84 ++pixelErrors;
85 }
86 }
87 }
88 REPORTER_ASSERT(reporter, 0 == pixelErrors);
89}
90
halcanary@google.com36d08c52013-12-05 14:00:03 +000091typedef bool (*InstallEncoded)(SkData* encoded, SkBitmap* dst);
92
commit-bot@chromium.org75854792013-10-29 19:55:00 +000093/**
halcanary@google.com36d08c52013-12-05 14:00:03 +000094 This function tests three differently encoded images against the
95 original bitmap */
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000096static void test_three_encodings(skiatest::Reporter* reporter,
halcanary@google.com36d08c52013-12-05 14:00:03 +000097 InstallEncoded install) {
commit-bot@chromium.org75854792013-10-29 19:55:00 +000098 SkBitmap original;
99 make_test_image(&original);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000100 REPORTER_ASSERT(reporter, !original.empty());
101 REPORTER_ASSERT(reporter, !original.isNull());
102 if (original.empty() || original.isNull()) {
103 return;
104 }
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000105 static const SkImageEncoder::Type types[] = {
106 SkImageEncoder::kPNG_Type,
107 SkImageEncoder::kJPEG_Type,
108 SkImageEncoder::kWEBP_Type
109 };
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000110 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) {
111 SkImageEncoder::Type type = types[i];
112 SkAutoDataUnref encoded(create_data_from_bitmap(original, type));
113 REPORTER_ASSERT(reporter, encoded.get() != NULL);
halcanary@google.com36d08c52013-12-05 14:00:03 +0000114 if (NULL == encoded.get()) {
115 continue;
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000116 }
halcanary@google.com36d08c52013-12-05 14:00:03 +0000117 SkBitmap lazy;
118 bool installSuccess = install(encoded.get(), &lazy);
119 REPORTER_ASSERT(reporter, installSuccess);
120 if (!installSuccess) {
121 continue;
122 }
123 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
124 {
125 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
126 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
127 if (NULL == lazy.getPixels()) {
128 continue;
129 }
130 }
131 // pixels should be gone!
132 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
133 {
134 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
135 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
136 if (NULL == lazy.getPixels()) {
137 continue;
138 }
139 }
140 bool comparePixels = (SkImageEncoder::kPNG_Type == type);
141 compare_bitmaps(reporter, original, lazy, comparePixels);
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000142 }
143}
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000144
halcanary@google.com36d08c52013-12-05 14:00:03 +0000145////////////////////////////////////////////////////////////////////////////////
halcanary@google.comdcfebfa2013-12-05 14:18:07 +0000146static bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) {
halcanary@google.com36d08c52013-12-05 14:00:03 +0000147 return SkCachingPixelRef::Install(
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000148 SkDecodingImageGenerator::Create(
149 encoded, SkDecodingImageGenerator::Options()), dst);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000150}
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000151static bool install_skDiscardablePixelRef(SkData* encoded, SkBitmap* dst) {
152 // Use system-default discardable memory.
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000153 return SkInstallDiscardablePixelRef(
154 SkDecodingImageGenerator::Create(
155 encoded, SkDecodingImageGenerator::Options()), dst, NULL);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000156}
halcanary@google.comad04eb42013-11-21 15:32:08 +0000157
halcanary@google.com36d08c52013-12-05 14:00:03 +0000158////////////////////////////////////////////////////////////////////////////////
159/**
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000160 * This checks to see that a SkCachingPixelRef and a
161 * SkDiscardablePixelRef works as advertised with a
162 * SkDecodingImageGenerator.
halcanary@google.com36d08c52013-12-05 14:00:03 +0000163 */
halcanary@google.comad04eb42013-11-21 15:32:08 +0000164DEF_TEST(DecodingImageGenerator, reporter) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000165 test_three_encodings(reporter, install_skCachingPixelRef);
166 test_three_encodings(reporter, install_skDiscardablePixelRef);
halcanary@google.comad04eb42013-11-21 15:32:08 +0000167}
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000168
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000169class TestImageGenerator : public SkImageGenerator {
170public:
171 enum TestType {
172 kFailGetInfo_TestType,
173 kFailGetPixels_TestType,
174 kSucceedGetPixels_TestType,
175 kLast_TestType = kSucceedGetPixels_TestType
176 };
177 static int Width() { return 10; }
178 static int Height() { return 10; }
179 static SkColor Color() { return SK_ColorCYAN; }
180 TestImageGenerator(TestType type, skiatest::Reporter* reporter)
181 : fType(type), fReporter(reporter) {
182 SkASSERT((fType <= kLast_TestType) && (fType >= 0));
183 }
tfarina@chromium.org58674812014-01-21 23:39:22 +0000184 virtual ~TestImageGenerator() { }
185 virtual bool getInfo(SkImageInfo* info) SK_OVERRIDE {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000186 REPORTER_ASSERT(fReporter, NULL != info);
187 if ((NULL == info) || (kFailGetInfo_TestType == fType)) {
188 return false;
189 }
190 info->fWidth = TestImageGenerator::Width();
191 info->fHeight = TestImageGenerator::Height();
commit-bot@chromium.org96edc242014-04-11 13:47:30 +0000192 info->fColorType = kPMColor_SkColorType;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000193 info->fAlphaType = kOpaque_SkAlphaType;
194 return true;
195 }
tfarina@chromium.org58674812014-01-21 23:39:22 +0000196 virtual bool getPixels(const SkImageInfo& info,
197 void* pixels,
198 size_t rowBytes) SK_OVERRIDE {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000199 REPORTER_ASSERT(fReporter, pixels != NULL);
200 size_t minRowBytes
201 = static_cast<size_t>(info.fWidth * info.bytesPerPixel());
202 REPORTER_ASSERT(fReporter, rowBytes >= minRowBytes);
203 if ((NULL == pixels)
204 || (fType != kSucceedGetPixels_TestType)
commit-bot@chromium.org96edc242014-04-11 13:47:30 +0000205 || (info.fColorType != kPMColor_SkColorType)) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000206 return false;
207 }
208 char* bytePtr = static_cast<char*>(pixels);
209 for (int y = 0; y < info.fHeight; ++y) {
210 sk_memset32(reinterpret_cast<SkColor*>(bytePtr),
211 TestImageGenerator::Color(), info.fWidth);
212 bytePtr += rowBytes;
213 }
214 return true;
215 }
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000216
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000217private:
218 const TestType fType;
219 skiatest::Reporter* const fReporter;
220};
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000221
tfarina@chromium.org58674812014-01-21 23:39:22 +0000222static void check_test_image_generator_bitmap(skiatest::Reporter* reporter,
223 const SkBitmap& bm) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000224 REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width());
225 REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height());
226 SkAutoLockPixels autoLockPixels(bm);
227 REPORTER_ASSERT(reporter, NULL != bm.getPixels());
228 if (NULL == bm.getPixels()) {
229 return;
230 }
231 int errors = 0;
232 for (int y = 0; y < bm.height(); ++y) {
233 for (int x = 0; x < bm.width(); ++x) {
234 if (TestImageGenerator::Color() != *bm.getAddr32(x, y)) {
235 ++errors;
236 }
237 }
238 }
239 REPORTER_ASSERT(reporter, 0 == errors);
240}
241
242enum PixelRefType {
243 kSkCaching_PixelRefType,
244 kSkDiscardable_PixelRefType,
245 kLast_PixelRefType = kSkDiscardable_PixelRefType
246};
tfarina@chromium.org58674812014-01-21 23:39:22 +0000247
248static void check_pixelref(TestImageGenerator::TestType type,
249 skiatest::Reporter* reporter,
250 PixelRefType pixelRefType,
251 SkDiscardableMemory::Factory* factory) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000252 SkASSERT((pixelRefType >= 0) && (pixelRefType <= kLast_PixelRefType));
253 SkAutoTDelete<SkImageGenerator> gen(SkNEW_ARGS(TestImageGenerator,
254 (type, reporter)));
255 REPORTER_ASSERT(reporter, gen.get() != NULL);
256 SkBitmap lazy;
257 bool success;
258 if (kSkCaching_PixelRefType == pixelRefType) {
259 // Ignore factory; use global SkScaledImageCache.
260 success = SkCachingPixelRef::Install(gen.detach(), &lazy);
261 } else {
halcanary@google.comedd370f2013-12-10 21:11:12 +0000262 success = SkInstallDiscardablePixelRef(gen.detach(), &lazy, factory);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000263 }
264 REPORTER_ASSERT(reporter, success
265 == (TestImageGenerator::kFailGetInfo_TestType != type));
266 if (TestImageGenerator::kSucceedGetPixels_TestType == type) {
tfarina@chromium.org58674812014-01-21 23:39:22 +0000267 check_test_image_generator_bitmap(reporter, lazy);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000268 } else if (TestImageGenerator::kFailGetPixels_TestType == type) {
269 SkAutoLockPixels autoLockPixels(lazy);
270 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
271 }
272}
reed@google.com1d0654f2013-12-12 22:37:32 +0000273
274// new/lock/delete is an odd pattern for a pixelref, but it needs to not assert
275static void test_newlockdelete(skiatest::Reporter* reporter) {
276 SkBitmap bm;
277 SkImageGenerator* ig = new TestImageGenerator(
tfarina@chromium.org58674812014-01-21 23:39:22 +0000278 TestImageGenerator::kSucceedGetPixels_TestType, reporter);
reed@google.com1d0654f2013-12-12 22:37:32 +0000279 SkInstallDiscardablePixelRef(ig, &bm, NULL);
280 bm.pixelRef()->lockPixels();
281}
282
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000283/**
284 * This tests the basic functionality of SkDiscardablePixelRef with a
285 * basic SkImageGenerator implementation and several
286 * SkDiscardableMemory::Factory choices.
287 */
288DEF_TEST(DiscardableAndCachingPixelRef, reporter) {
reed@google.com1d0654f2013-12-12 22:37:32 +0000289 test_newlockdelete(reporter);
290
tfarina@chromium.org58674812014-01-21 23:39:22 +0000291 check_pixelref(TestImageGenerator::kFailGetInfo_TestType,
292 reporter, kSkCaching_PixelRefType, NULL);
293 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
294 reporter, kSkCaching_PixelRefType, NULL);
295 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
296 reporter, kSkCaching_PixelRefType, NULL);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000297
tfarina@chromium.org58674812014-01-21 23:39:22 +0000298 check_pixelref(TestImageGenerator::kFailGetInfo_TestType,
299 reporter, kSkDiscardable_PixelRefType, NULL);
300 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
301 reporter, kSkDiscardable_PixelRefType, NULL);
302 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
303 reporter, kSkDiscardable_PixelRefType, NULL);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000304
305 SkAutoTUnref<SkDiscardableMemoryPool> pool(
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000306 SkDiscardableMemoryPool::Create(1, NULL));
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000307 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
tfarina@chromium.org58674812014-01-21 23:39:22 +0000308 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
309 reporter, kSkDiscardable_PixelRefType, pool);
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::kSucceedGetPixels_TestType,
312 reporter, kSkDiscardable_PixelRefType, pool);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000313 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
314
315 SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool();
halcanary@google.combc55eec2013-12-10 18:33:07 +0000316 // Only acts differently from NULL on a platform that has a
317 // default discardable memory implementation that differs from the
318 // global DM pool.
tfarina@chromium.org58674812014-01-21 23:39:22 +0000319 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
320 reporter, kSkDiscardable_PixelRefType, globalPool);
321 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
322 reporter, kSkDiscardable_PixelRefType, globalPool);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000323}