blob: 10e33ba841be16bbb079ad4397a340a53bd949c2 [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.org924205a2014-05-28 16:16:08 +0000186 virtual bool getInfo(SkImageInfo* info) SK_OVERRIDE {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000187 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();
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000193 info->fColorType = kN32_SkColorType;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000194 info->fAlphaType = kOpaque_SkAlphaType;
195 return true;
196 }
commit-bot@chromium.org924205a2014-05-28 16:16:08 +0000197 virtual bool getPixels(const SkImageInfo& info,
198 void* pixels,
199 size_t rowBytes) SK_OVERRIDE {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000200 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)
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000206 || (info.fColorType != kN32_SkColorType)) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000207 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 }
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000217
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000218private:
219 const TestType fType;
220 skiatest::Reporter* const fReporter;
221};
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000222
tfarina@chromium.org58674812014-01-21 23:39:22 +0000223static void check_test_image_generator_bitmap(skiatest::Reporter* reporter,
224 const SkBitmap& bm) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000225 REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width());
226 REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height());
227 SkAutoLockPixels autoLockPixels(bm);
228 REPORTER_ASSERT(reporter, NULL != bm.getPixels());
229 if (NULL == bm.getPixels()) {
230 return;
231 }
232 int errors = 0;
233 for (int y = 0; y < bm.height(); ++y) {
234 for (int x = 0; x < bm.width(); ++x) {
235 if (TestImageGenerator::Color() != *bm.getAddr32(x, y)) {
236 ++errors;
237 }
238 }
239 }
240 REPORTER_ASSERT(reporter, 0 == errors);
241}
242
243enum PixelRefType {
244 kSkCaching_PixelRefType,
245 kSkDiscardable_PixelRefType,
246 kLast_PixelRefType = kSkDiscardable_PixelRefType
247};
tfarina@chromium.org58674812014-01-21 23:39:22 +0000248
249static void check_pixelref(TestImageGenerator::TestType type,
250 skiatest::Reporter* reporter,
251 PixelRefType pixelRefType,
252 SkDiscardableMemory::Factory* factory) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000253 SkASSERT((pixelRefType >= 0) && (pixelRefType <= kLast_PixelRefType));
254 SkAutoTDelete<SkImageGenerator> gen(SkNEW_ARGS(TestImageGenerator,
255 (type, reporter)));
256 REPORTER_ASSERT(reporter, gen.get() != NULL);
257 SkBitmap lazy;
258 bool success;
259 if (kSkCaching_PixelRefType == pixelRefType) {
260 // Ignore factory; use global SkScaledImageCache.
261 success = SkCachingPixelRef::Install(gen.detach(), &lazy);
262 } else {
halcanary@google.comedd370f2013-12-10 21:11:12 +0000263 success = SkInstallDiscardablePixelRef(gen.detach(), &lazy, factory);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000264 }
265 REPORTER_ASSERT(reporter, success
266 == (TestImageGenerator::kFailGetInfo_TestType != type));
267 if (TestImageGenerator::kSucceedGetPixels_TestType == type) {
tfarina@chromium.org58674812014-01-21 23:39:22 +0000268 check_test_image_generator_bitmap(reporter, lazy);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000269 } else if (TestImageGenerator::kFailGetPixels_TestType == type) {
270 SkAutoLockPixels autoLockPixels(lazy);
271 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
272 }
273}
reed@google.com1d0654f2013-12-12 22:37:32 +0000274
275// new/lock/delete is an odd pattern for a pixelref, but it needs to not assert
276static void test_newlockdelete(skiatest::Reporter* reporter) {
277 SkBitmap bm;
278 SkImageGenerator* ig = new TestImageGenerator(
tfarina@chromium.org58674812014-01-21 23:39:22 +0000279 TestImageGenerator::kSucceedGetPixels_TestType, reporter);
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +0000280 SkInstallDiscardablePixelRef(ig, &bm);
reed@google.com1d0654f2013-12-12 22:37:32 +0000281 bm.pixelRef()->lockPixels();
282}
283
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000284/**
285 * This tests the basic functionality of SkDiscardablePixelRef with a
286 * basic SkImageGenerator implementation and several
287 * SkDiscardableMemory::Factory choices.
288 */
289DEF_TEST(DiscardableAndCachingPixelRef, reporter) {
reed@google.com1d0654f2013-12-12 22:37:32 +0000290 test_newlockdelete(reporter);
291
tfarina@chromium.org58674812014-01-21 23:39:22 +0000292 check_pixelref(TestImageGenerator::kFailGetInfo_TestType,
293 reporter, kSkCaching_PixelRefType, NULL);
294 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
295 reporter, kSkCaching_PixelRefType, NULL);
296 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
297 reporter, kSkCaching_PixelRefType, NULL);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000298
tfarina@chromium.org58674812014-01-21 23:39:22 +0000299 check_pixelref(TestImageGenerator::kFailGetInfo_TestType,
300 reporter, kSkDiscardable_PixelRefType, NULL);
301 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
302 reporter, kSkDiscardable_PixelRefType, NULL);
303 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
304 reporter, kSkDiscardable_PixelRefType, NULL);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000305
306 SkAutoTUnref<SkDiscardableMemoryPool> pool(
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000307 SkDiscardableMemoryPool::Create(1, NULL));
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000308 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
tfarina@chromium.org58674812014-01-21 23:39:22 +0000309 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
310 reporter, kSkDiscardable_PixelRefType, pool);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000311 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
tfarina@chromium.org58674812014-01-21 23:39:22 +0000312 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
313 reporter, kSkDiscardable_PixelRefType, pool);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000314 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
315
316 SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool();
halcanary@google.combc55eec2013-12-10 18:33:07 +0000317 // Only acts differently from NULL on a platform that has a
318 // default discardable memory implementation that differs from the
319 // global DM pool.
tfarina@chromium.org58674812014-01-21 23:39:22 +0000320 check_pixelref(TestImageGenerator::kFailGetPixels_TestType,
321 reporter, kSkDiscardable_PixelRefType, globalPool);
322 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType,
323 reporter, kSkDiscardable_PixelRefType, globalPool);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000324}