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