blob: da4a111b28ae4916f8ee93c2e700c7189df5d494 [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.com2c7c7ee2013-12-05 18:31:42 +000011#include "SkDiscardableMemoryPool.h"
msarett7f7ec202016-03-01 12:12:27 -080012#include "SkImage.h"
13#include "SkImageEncoder.h"
commit-bot@chromium.org2d970b52014-05-27 14:14:22 +000014#include "SkImageGeneratorPriv.h"
reed011f39a2014-08-28 13:35:23 -070015#include "SkResourceCache.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000016#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) {
reed6c225732014-06-09 19:52:07 -070025 const int W = 50, H = 50;
26 bm->allocN32Pixels(W, H);
commit-bot@chromium.org75854792013-10-29 19:55:00 +000027 bm->eraseColor(SK_ColorBLACK);
28 SkCanvas canvas(*bm);
29 SkPaint paint;
30 paint.setColor(SK_ColorBLUE);
31 canvas.drawRectCoords(0, 0, SkIntToScalar(W/2),
32 SkIntToScalar(H/2), paint);
33 paint.setColor(SK_ColorWHITE);
34 canvas.drawRectCoords(SkIntToScalar(W/2), SkIntToScalar(H/2),
35 SkIntToScalar(W), SkIntToScalar(H), paint);
36}
37
38/**
39 * encode this bitmap into some data via SkImageEncoder
40 */
41static SkData* create_data_from_bitmap(const SkBitmap& bm,
42 SkImageEncoder::Type type) {
43 SkDynamicMemoryWStream stream;
44 if (SkImageEncoder::EncodeStream(&stream, bm, type, 100)) {
45 return stream.copyToData();
46 }
halcanary96fcdcc2015-08-27 07:41:13 -070047 return nullptr;
commit-bot@chromium.org75854792013-10-29 19:55:00 +000048}
49
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000050////////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.org75854792013-10-29 19:55:00 +000051
52static void compare_bitmaps(skiatest::Reporter* reporter,
53 const SkBitmap& b1, const SkBitmap& b2,
54 bool pixelPerfect = true) {
55 REPORTER_ASSERT(reporter, b1.empty() == b2.empty());
56 REPORTER_ASSERT(reporter, b1.width() == b2.width());
57 REPORTER_ASSERT(reporter, b1.height() == b2.height());
58 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
59 SkAutoLockPixels autoLockPixels1(b1);
60 SkAutoLockPixels autoLockPixels2(b2);
61 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
62 if (b1.isNull() || b1.empty()) {
63 return;
64 }
bsalomon49f085d2014-09-05 13:34:00 -070065 REPORTER_ASSERT(reporter, b1.getPixels());
66 REPORTER_ASSERT(reporter, b2.getPixels());
commit-bot@chromium.org75854792013-10-29 19:55:00 +000067 if ((!(b1.getPixels())) || (!(b2.getPixels()))) {
68 return;
69 }
70 if ((b1.width() != b2.width()) ||
71 (b1.height() != b2.height())) {
72 return;
73 }
74 if (!pixelPerfect) {
75 return;
76 }
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000077
commit-bot@chromium.org75854792013-10-29 19:55:00 +000078 int pixelErrors = 0;
79 for (int y = 0; y < b2.height(); ++y) {
80 for (int x = 0; x < b2.width(); ++x) {
81 if (b1.getColor(x, y) != b2.getColor(x, y)) {
82 ++pixelErrors;
83 }
84 }
85 }
86 REPORTER_ASSERT(reporter, 0 == pixelErrors);
87}
88
halcanary@google.com36d08c52013-12-05 14:00:03 +000089typedef bool (*InstallEncoded)(SkData* encoded, SkBitmap* dst);
90
commit-bot@chromium.org75854792013-10-29 19:55:00 +000091/**
halcanary@google.com36d08c52013-12-05 14:00:03 +000092 This function tests three differently encoded images against the
93 original bitmap */
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000094static void test_three_encodings(skiatest::Reporter* reporter,
halcanary@google.com36d08c52013-12-05 14:00:03 +000095 InstallEncoded install) {
commit-bot@chromium.org75854792013-10-29 19:55:00 +000096 SkBitmap original;
97 make_test_image(&original);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000098 REPORTER_ASSERT(reporter, !original.empty());
99 REPORTER_ASSERT(reporter, !original.isNull());
100 if (original.empty() || original.isNull()) {
101 return;
102 }
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000103 static const SkImageEncoder::Type types[] = {
104 SkImageEncoder::kPNG_Type,
105 SkImageEncoder::kJPEG_Type,
106 SkImageEncoder::kWEBP_Type
107 };
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000108 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) {
109 SkImageEncoder::Type type = types[i];
110 SkAutoDataUnref encoded(create_data_from_bitmap(original, type));
halcanary96fcdcc2015-08-27 07:41:13 -0700111 REPORTER_ASSERT(reporter, encoded.get() != nullptr);
112 if (nullptr == encoded.get()) {
halcanary@google.com36d08c52013-12-05 14:00:03 +0000113 continue;
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000114 }
halcanary@google.com36d08c52013-12-05 14:00:03 +0000115 SkBitmap lazy;
116 bool installSuccess = install(encoded.get(), &lazy);
117 REPORTER_ASSERT(reporter, installSuccess);
118 if (!installSuccess) {
119 continue;
120 }
halcanary96fcdcc2015-08-27 07:41:13 -0700121 REPORTER_ASSERT(reporter, nullptr == lazy.getPixels());
halcanary@google.com36d08c52013-12-05 14:00:03 +0000122 {
123 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
bsalomon49f085d2014-09-05 13:34:00 -0700124 REPORTER_ASSERT(reporter, lazy.getPixels());
halcanary96fcdcc2015-08-27 07:41:13 -0700125 if (nullptr == lazy.getPixels()) {
halcanary@google.com36d08c52013-12-05 14:00:03 +0000126 continue;
127 }
128 }
129 // pixels should be gone!
halcanary96fcdcc2015-08-27 07:41:13 -0700130 REPORTER_ASSERT(reporter, nullptr == lazy.getPixels());
halcanary@google.com36d08c52013-12-05 14:00:03 +0000131 {
132 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
bsalomon49f085d2014-09-05 13:34:00 -0700133 REPORTER_ASSERT(reporter, lazy.getPixels());
halcanary96fcdcc2015-08-27 07:41:13 -0700134 if (nullptr == lazy.getPixels()) {
halcanary@google.com36d08c52013-12-05 14:00:03 +0000135 continue;
136 }
137 }
138 bool comparePixels = (SkImageEncoder::kPNG_Type == type);
139 compare_bitmaps(reporter, original, lazy, comparePixels);
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000140 }
141}
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000142
halcanary@google.com36d08c52013-12-05 14:00:03 +0000143////////////////////////////////////////////////////////////////////////////////
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000144static bool install_skDiscardablePixelRef(SkData* encoded, SkBitmap* dst) {
145 // Use system-default discardable memory.
reedd1146452015-09-25 06:56:57 -0700146 return SkDEPRECATED_InstallDiscardablePixelRef(encoded, dst);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000147}
halcanary@google.comad04eb42013-11-21 15:32:08 +0000148
halcanary@google.com36d08c52013-12-05 14:00:03 +0000149////////////////////////////////////////////////////////////////////////////////
150/**
reeddd42e5c2015-09-24 00:21:20 -0700151 * This checks to see that SkDiscardablePixelRef works as advertised with a
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000152 * SkDecodingImageGenerator.
halcanary@google.com36d08c52013-12-05 14:00:03 +0000153 */
halcanary@google.comad04eb42013-11-21 15:32:08 +0000154DEF_TEST(DecodingImageGenerator, reporter) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000155 test_three_encodings(reporter, install_skDiscardablePixelRef);
halcanary@google.comad04eb42013-11-21 15:32:08 +0000156}
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000157
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000158class TestImageGenerator : public SkImageGenerator {
159public:
160 enum TestType {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000161 kFailGetPixels_TestType,
162 kSucceedGetPixels_TestType,
163 kLast_TestType = kSucceedGetPixels_TestType
164 };
165 static int Width() { return 10; }
166 static int Height() { return 10; }
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800167 // value choosen so that there is no loss when converting to to RGB565 and back
168 static SkColor Color() { return 0xff10345a; }
169 static SkPMColor PMColor() { return SkPreMultiplyColor(Color()); }
170
171 TestImageGenerator(TestType type, skiatest::Reporter* reporter,
172 SkColorType colorType = kN32_SkColorType)
173 : INHERITED(GetMyInfo(colorType)), fType(type), fReporter(reporter) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000174 SkASSERT((fType <= kLast_TestType) && (fType >= 0));
175 }
tfarina@chromium.org58674812014-01-21 23:39:22 +0000176 virtual ~TestImageGenerator() { }
commit-bot@chromium.org00f8d6c2014-05-29 15:57:20 +0000177
178protected:
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800179 static SkImageInfo GetMyInfo(SkColorType colorType) {
180 return SkImageInfo::Make(TestImageGenerator::Width(), TestImageGenerator::Height(),
181 colorType, kOpaque_SkAlphaType);
reed3ef71e32015-03-19 08:31:14 -0700182 }
183
scroggo5315fd42015-07-09 09:08:00 -0700184 bool onGetPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
185 SkPMColor ctable[], int* ctableCount) override {
halcanary96fcdcc2015-08-27 07:41:13 -0700186 REPORTER_ASSERT(fReporter, pixels != nullptr);
scroggo08649082015-02-13 11:13:34 -0800187 REPORTER_ASSERT(fReporter, rowBytes >= info.minRowBytes());
188 if (fType != kSucceedGetPixels_TestType) {
scroggo5315fd42015-07-09 09:08:00 -0700189 return false;
scroggo08649082015-02-13 11:13:34 -0800190 }
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800191 if (info.colorType() != kN32_SkColorType && info.colorType() != getInfo().colorType()) {
scroggo5315fd42015-07-09 09:08:00 -0700192 return false;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000193 }
194 char* bytePtr = static_cast<char*>(pixels);
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800195 switch (info.colorType()) {
196 case kN32_SkColorType:
197 for (int y = 0; y < info.height(); ++y) {
198 sk_memset32((uint32_t*)bytePtr,
199 TestImageGenerator::PMColor(), info.width());
200 bytePtr += rowBytes;
201 }
202 break;
203 case kIndex_8_SkColorType:
204 *ctableCount = 1;
205 ctable[0] = TestImageGenerator::PMColor();
206 for (int y = 0; y < info.height(); ++y) {
207 memset(bytePtr, 0, info.width());
208 bytePtr += rowBytes;
209 }
210 break;
211 case kRGB_565_SkColorType:
212 for (int y = 0; y < info.height(); ++y) {
213 sk_memset16((uint16_t*)bytePtr,
214 SkPixel32ToPixel16(TestImageGenerator::PMColor()), info.width());
215 bytePtr += rowBytes;
216 }
217 break;
218 default:
219 return false;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000220 }
scroggo5315fd42015-07-09 09:08:00 -0700221 return true;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000222 }
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000223
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000224private:
225 const TestType fType;
226 skiatest::Reporter* const fReporter;
reed3ef71e32015-03-19 08:31:14 -0700227
228 typedef SkImageGenerator INHERITED;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000229};
halcanary@google.com3d50ea12014-01-02 13:15:13 +0000230
tfarina@chromium.org58674812014-01-21 23:39:22 +0000231static void check_test_image_generator_bitmap(skiatest::Reporter* reporter,
232 const SkBitmap& bm) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000233 REPORTER_ASSERT(reporter, TestImageGenerator::Width() == bm.width());
234 REPORTER_ASSERT(reporter, TestImageGenerator::Height() == bm.height());
235 SkAutoLockPixels autoLockPixels(bm);
bsalomon49f085d2014-09-05 13:34:00 -0700236 REPORTER_ASSERT(reporter, bm.getPixels());
halcanary96fcdcc2015-08-27 07:41:13 -0700237 if (nullptr == bm.getPixels()) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000238 return;
239 }
240 int errors = 0;
241 for (int y = 0; y < bm.height(); ++y) {
242 for (int x = 0; x < bm.width(); ++x) {
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800243 if (TestImageGenerator::Color() != bm.getColor(x, y)) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000244 ++errors;
245 }
246 }
247 }
248 REPORTER_ASSERT(reporter, 0 == errors);
249}
250
tfarina@chromium.org58674812014-01-21 23:39:22 +0000251static void check_pixelref(TestImageGenerator::TestType type,
252 skiatest::Reporter* reporter,
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800253 SkDiscardableMemory::Factory* factory,
254 SkColorType colorType) {
255 SkAutoTDelete<SkImageGenerator> gen(new TestImageGenerator(type, reporter, colorType));
halcanary96fcdcc2015-08-27 07:41:13 -0700256 REPORTER_ASSERT(reporter, gen.get() != nullptr);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000257 SkBitmap lazy;
mtklein18300a32016-03-16 13:53:35 -0700258 bool success = SkDEPRECATED_InstallDiscardablePixelRef(gen.release(), nullptr, &lazy, factory);
reeddd42e5c2015-09-24 00:21:20 -0700259
reed3ef71e32015-03-19 08:31:14 -0700260 REPORTER_ASSERT(reporter, success);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000261 if (TestImageGenerator::kSucceedGetPixels_TestType == type) {
tfarina@chromium.org58674812014-01-21 23:39:22 +0000262 check_test_image_generator_bitmap(reporter, lazy);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000263 } else if (TestImageGenerator::kFailGetPixels_TestType == type) {
264 SkAutoLockPixels autoLockPixels(lazy);
halcanary96fcdcc2015-08-27 07:41:13 -0700265 REPORTER_ASSERT(reporter, nullptr == lazy.getPixels());
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000266 }
267}
reed@google.com1d0654f2013-12-12 22:37:32 +0000268
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000269/**
270 * This tests the basic functionality of SkDiscardablePixelRef with a
271 * basic SkImageGenerator implementation and several
272 * SkDiscardableMemory::Factory choices.
273 */
274DEF_TEST(DiscardableAndCachingPixelRef, reporter) {
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800275 const SkColorType testColorTypes[] = {
276 kN32_SkColorType,
277 kIndex_8_SkColorType,
278 kRGB_565_SkColorType
279 };
280 for (const SkColorType testColorType : testColorTypes) {
281 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, nullptr,
282 testColorType);
283 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, nullptr,
284 testColorType);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000285
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800286 SkAutoTUnref<SkDiscardableMemoryPool> pool(
287 SkDiscardableMemoryPool::Create(1, nullptr));
288 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
289 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, pool,
290 testColorType);
291 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
292 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, pool,
293 testColorType);
294 REPORTER_ASSERT(reporter, 0 == pool->getRAMUsed());
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000295
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800296 SkDiscardableMemoryPool* globalPool = SkGetGlobalDiscardableMemoryPool();
297 // Only acts differently from nullptr on a platform that has a
298 // default discardable memory implementation that differs from the
299 // global DM pool.
300 check_pixelref(TestImageGenerator::kFailGetPixels_TestType, reporter, globalPool,
301 testColorType);
302 check_pixelref(TestImageGenerator::kSucceedGetPixels_TestType, reporter, globalPool,
303 testColorType);
304 }
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000305}
halcanaryea4673f2014-08-18 08:27:09 -0700306
307////////////////////////////////////////////////////////////////////////////////
308
309DEF_TEST(Image_NewFromGenerator, r) {
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800310 const TestImageGenerator::TestType testTypes[] = {
halcanaryea4673f2014-08-18 08:27:09 -0700311 TestImageGenerator::kFailGetPixels_TestType,
312 TestImageGenerator::kSucceedGetPixels_TestType,
313 };
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800314 const SkColorType testColorTypes[] = {
315 kN32_SkColorType,
316 kIndex_8_SkColorType,
317 kRGB_565_SkColorType
318 };
halcanaryea4673f2014-08-18 08:27:09 -0700319 for (size_t i = 0; i < SK_ARRAY_COUNT(testTypes); ++i) {
320 TestImageGenerator::TestType test = testTypes[i];
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800321 for (const SkColorType testColorType : testColorTypes) {
322 SkImageGenerator* gen = new TestImageGenerator(test, r, testColorType);
reed9ce9d672016-03-17 10:51:11 -0700323 sk_sp<SkImage> image(SkImage::MakeFromGenerator(gen));
324 if (nullptr == image) {
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800325 ERRORF(r, "SkImage::NewFromGenerator unexpecedly failed ["
326 SK_SIZE_T_SPECIFIER "]", i);
327 continue;
328 }
329 REPORTER_ASSERT(r, TestImageGenerator::Width() == image->width());
330 REPORTER_ASSERT(r, TestImageGenerator::Height() == image->height());
331 REPORTER_ASSERT(r, image->isLazyGenerated());
halcanaryea4673f2014-08-18 08:27:09 -0700332
aleksandar.stojiljkovic07e26922015-11-10 04:55:15 -0800333 SkBitmap bitmap;
334 bitmap.allocN32Pixels(TestImageGenerator::Width(), TestImageGenerator::Height());
335 SkCanvas canvas(bitmap);
336 const SkColor kDefaultColor = 0xffabcdef;
337 canvas.clear(kDefaultColor);
338 canvas.drawImage(image, 0, 0, nullptr);
339 if (TestImageGenerator::kSucceedGetPixels_TestType == test) {
340 REPORTER_ASSERT(
341 r, TestImageGenerator::Color() == bitmap.getColor(0, 0));
342 }
343 else {
344 REPORTER_ASSERT(r, kDefaultColor == bitmap.getColor(0, 0));
345 }
halcanaryea4673f2014-08-18 08:27:09 -0700346 }
347 }
348}