blob: 2d8a0e70737a66c376987caf9c2adb055026f054 [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"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000012#include "SkForceLinking.h"
13#include "SkImageDecoder.h"
14#include "SkImagePriv.h"
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000015#include "SkLazyCachingPixelRef.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000016#include "SkLazyPixelRef.h"
17#include "SkScaledImageCache.h"
18#include "SkStream.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
23__SK_FORCE_IMAGE_DECODER_LINKING;
24
25/**
26 * Fill this bitmap with some color.
27 */
28static void make_test_image(SkBitmap* bm) {
29 static const int W = 50, H = 50;
30 static const SkBitmap::Config config = SkBitmap::kARGB_8888_Config;
31 bm->setConfig(config, W, H);
32 bm->allocPixels();
33 bm->eraseColor(SK_ColorBLACK);
34 SkCanvas canvas(*bm);
35 SkPaint paint;
36 paint.setColor(SK_ColorBLUE);
37 canvas.drawRectCoords(0, 0, SkIntToScalar(W/2),
38 SkIntToScalar(H/2), paint);
39 paint.setColor(SK_ColorWHITE);
40 canvas.drawRectCoords(SkIntToScalar(W/2), SkIntToScalar(H/2),
41 SkIntToScalar(W), SkIntToScalar(H), paint);
42}
43
44/**
45 * encode this bitmap into some data via SkImageEncoder
46 */
47static SkData* create_data_from_bitmap(const SkBitmap& bm,
48 SkImageEncoder::Type type) {
49 SkDynamicMemoryWStream stream;
50 if (SkImageEncoder::EncodeStream(&stream, bm, type, 100)) {
51 return stream.copyToData();
52 }
53 return NULL;
54}
55
56/**
57 * A simplified version of SkBitmapFactory
58 */
59static bool simple_bitmap_factory(SkBitmapFactory::DecodeProc proc,
60 SkData* data,
61 SkBitmap* dst) {
reed@google.com2bd8b812013-11-01 13:46:54 +000062 SkImageInfo info;
commit-bot@chromium.org75854792013-10-29 19:55:00 +000063 if (!proc(data->data(), data->size(), &info, NULL)) {
64 return false;
65 }
66 dst->setConfig(SkImageInfoToBitmapConfig(info), info.fWidth,
67 info.fHeight, 0, info.fAlphaType);
68 SkAutoTUnref<SkLazyPixelRef> ref(SkNEW_ARGS(SkLazyPixelRef,
69 (data, proc, NULL)));
70 dst->setPixelRef(ref);
71 return true;
72}
73
74static void compare_bitmaps(skiatest::Reporter* reporter,
75 const SkBitmap& b1, const SkBitmap& b2,
76 bool pixelPerfect = true) {
77 REPORTER_ASSERT(reporter, b1.empty() == b2.empty());
78 REPORTER_ASSERT(reporter, b1.width() == b2.width());
79 REPORTER_ASSERT(reporter, b1.height() == b2.height());
80 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
81 SkAutoLockPixels autoLockPixels1(b1);
82 SkAutoLockPixels autoLockPixels2(b2);
83 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
84 if (b1.isNull() || b1.empty()) {
85 return;
86 }
87 REPORTER_ASSERT(reporter, NULL != b1.getPixels());
88 REPORTER_ASSERT(reporter, NULL != b2.getPixels());
89 if ((!(b1.getPixels())) || (!(b2.getPixels()))) {
90 return;
91 }
92 if ((b1.width() != b2.width()) ||
93 (b1.height() != b2.height())) {
94 return;
95 }
96 if (!pixelPerfect) {
97 return;
98 }
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +000099
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000100 int pixelErrors = 0;
101 for (int y = 0; y < b2.height(); ++y) {
102 for (int x = 0; x < b2.width(); ++x) {
103 if (b1.getColor(x, y) != b2.getColor(x, y)) {
104 ++pixelErrors;
105 }
106 }
107 }
108 REPORTER_ASSERT(reporter, 0 == pixelErrors);
109}
110
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000111
112typedef void(*CompareEncodedToOriginal)(skiatest::Reporter* reporter,
113 SkData* encoded,
114 const SkBitmap& original,
115 bool pixelPerfect);
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000116/**
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000117 this function tests three differently encoded images against the
118 original bitmap */
119static void test_three_encodings(skiatest::Reporter* reporter,
120 CompareEncodedToOriginal comp) {
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000121 SkBitmap original;
122 make_test_image(&original);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000123 REPORTER_ASSERT(reporter, !original.empty());
124 REPORTER_ASSERT(reporter, !original.isNull());
125 if (original.empty() || original.isNull()) {
126 return;
127 }
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000128 static const SkImageEncoder::Type types[] = {
129 SkImageEncoder::kPNG_Type,
130 SkImageEncoder::kJPEG_Type,
131 SkImageEncoder::kWEBP_Type
132 };
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000133 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) {
134 SkImageEncoder::Type type = types[i];
135 SkAutoDataUnref encoded(create_data_from_bitmap(original, type));
136 REPORTER_ASSERT(reporter, encoded.get() != NULL);
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000137 if (NULL != encoded.get()) {
138 bool comparePixels = (SkImageEncoder::kPNG_Type == type);
139 comp(reporter, encoded, original, comparePixels);
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000140 }
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000141 }
142}
commit-bot@chromium.org6e3e4222013-11-06 10:08:30 +0000143
144/**
145 * This checks to see that a SkLazyPixelRef works as advertised.
146 */
147static void compare_with_skLazyPixelRef(skiatest::Reporter* reporter,
148 SkData* encoded,
149 const SkBitmap& original,
150 bool comparePixels) {
151 SkBitmap lazy;
152 static const SkBitmapFactory::DecodeProc decoder =
153 &(SkImageDecoder::DecodeMemoryToTarget);
154 bool success = simple_bitmap_factory(decoder, encoded, &lazy);
155 REPORTER_ASSERT(reporter, success);
156
157 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
158 {
159 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
160 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
161 }
162 // pixels should be gone!
163 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
164 {
165 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
166 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
167 }
168 compare_bitmaps(reporter, original, lazy, comparePixels);
169}
170DEF_TEST(LazyPixelRef, reporter) {
171 test_three_encodings(reporter, compare_with_skLazyPixelRef);
172}
173
174
175
176/**
177 * This checks to see that a SkLazyCachedPixelRef works as advertised.
178 */
179
180static void compare_with_skLazyCachedPixelRef(skiatest::Reporter* reporter,
181 SkData* encoded,
182 const SkBitmap& original,
183 bool comparePixels) {
184 SkBitmap lazy;
185 static const SkBitmapFactory::DecodeProc decoder =
186 &(SkImageDecoder::DecodeMemoryToTarget);
187 bool success = SkLazyCachingPixelRef::Install(decoder, encoded, &lazy);
188 REPORTER_ASSERT(reporter, success);
189
190 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
191 {
192 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
193 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
194 }
195 // pixels should be gone!
196 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
197 {
198 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
199 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
200 }
201 compare_bitmaps(reporter, original, lazy, comparePixels);
202}
203DEF_TEST(LazyCachedPixelRef, reporter) {
204 test_three_encodings(reporter, compare_with_skLazyCachedPixelRef);
205}
206
207class TestPixelRef : public SkCachingPixelRef {
208public:
209 TestPixelRef(int x) : fX(x) { }
210 virtual ~TestPixelRef() { }
211 static bool Install(SkBitmap* destination, int x) {
212 SkAutoTUnref<TestPixelRef> ref(SkNEW_ARGS(TestPixelRef, (x)));
213 return ref->configure(destination) && destination->setPixelRef(ref);
214 }
215 SK_DECLARE_UNFLATTENABLE_OBJECT()
216protected:
217 virtual bool onDecodeInfo(SkImageInfo* info) SK_OVERRIDE {
218 if (fX == 0) {
219 return false;
220 }
221 SkASSERT(info);
222 info->fWidth = 10;
223 info->fHeight = 10;
224 info->fColorType = kRGBA_8888_SkColorType;
225 info->fAlphaType = kOpaque_SkAlphaType;
226 return true;
227 }
228 virtual bool onDecodePixels(const SkImageInfo& info,
229 void* pixels,
230 size_t rowBytes) SK_OVERRIDE {
231 return false;
232 }
233private:
234 int fX; // controls where the failure happens
235 typedef SkCachingPixelRef INHERITED;
236};
237
238DEF_TEST(CachingPixelRef, reporter) {
239 SkBitmap lazy;
240 // test the error handling
241 REPORTER_ASSERT(reporter, !TestPixelRef::Install(&lazy, 0));
242 // onDecodeInfo should succeed, allowing installation
243 REPORTER_ASSERT(reporter, TestPixelRef::Install(&lazy, 1));
244 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
245 // onDecodePixels should fail, so getting pixels will fail.
246 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
247}
halcanary@google.comad04eb42013-11-21 15:32:08 +0000248
249static void compare_with_SkDecodingImageGenerator(skiatest::Reporter* reporter,
250 SkData* encoded,
251 const SkBitmap& original,
252 bool comparePixels) {
253
254 SkBitmap lazy;
255 bool success = SkDecodingImageGenerator::Install(encoded, &lazy);
256 REPORTER_ASSERT(reporter, success);
257 if (!success) {
258 return;
259 }
260
261 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
262 {
263 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
264 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
265 if (NULL == lazy.getPixels()) {
266 return;
267 }
268 }
269 // pixels should be gone!
270 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
271 {
272 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
273 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
274 }
275 compare_bitmaps(reporter, original, lazy, comparePixels);
276}
277DEF_TEST(DecodingImageGenerator, reporter) {
278 test_three_encodings(reporter, compare_with_SkDecodingImageGenerator);
279}