OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkBitmap.h" | 8 #include "SkBitmap.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkData.h" | 10 #include "SkData.h" |
11 #include "SkDecodingImageGenerator.h" | 11 #include "SkDecodingImageGenerator.h" |
12 #include "SkForceLinking.h" | 12 #include "SkForceLinking.h" |
13 #include "SkImageDecoder.h" | 13 #include "SkImageDecoder.h" |
14 #include "SkImagePriv.h" | 14 #include "SkImagePriv.h" |
15 #include "SkLazyCachingPixelRef.h" | 15 #include "SkLazyCachingPixelRef.h" |
16 #include "SkLazyPixelRef.h" | |
17 #include "SkScaledImageCache.h" | 16 #include "SkScaledImageCache.h" |
18 #include "SkStream.h" | 17 #include "SkStream.h" |
19 | 18 |
20 #include "Test.h" | 19 #include "Test.h" |
21 #include "TestClassDef.h" | 20 #include "TestClassDef.h" |
22 | 21 |
23 __SK_FORCE_IMAGE_DECODER_LINKING; | 22 __SK_FORCE_IMAGE_DECODER_LINKING; |
24 | 23 |
25 /** | 24 /** |
26 * Fill this bitmap with some color. | 25 * Fill this bitmap with some color. |
(...skipping 19 matching lines...) Expand all Loading... |
46 */ | 45 */ |
47 static SkData* create_data_from_bitmap(const SkBitmap& bm, | 46 static SkData* create_data_from_bitmap(const SkBitmap& bm, |
48 SkImageEncoder::Type type) { | 47 SkImageEncoder::Type type) { |
49 SkDynamicMemoryWStream stream; | 48 SkDynamicMemoryWStream stream; |
50 if (SkImageEncoder::EncodeStream(&stream, bm, type, 100)) { | 49 if (SkImageEncoder::EncodeStream(&stream, bm, type, 100)) { |
51 return stream.copyToData(); | 50 return stream.copyToData(); |
52 } | 51 } |
53 return NULL; | 52 return NULL; |
54 } | 53 } |
55 | 54 |
56 /** | |
57 * A simplified version of SkBitmapFactory | |
58 */ | |
59 static bool simple_bitmap_factory(SkBitmapFactory::DecodeProc proc, | |
60 SkData* data, | |
61 SkBitmap* dst) { | |
62 SkImageInfo info; | |
63 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 | |
74 static void compare_bitmaps(skiatest::Reporter* reporter, | 55 static void compare_bitmaps(skiatest::Reporter* reporter, |
75 const SkBitmap& b1, const SkBitmap& b2, | 56 const SkBitmap& b1, const SkBitmap& b2, |
76 bool pixelPerfect = true) { | 57 bool pixelPerfect = true) { |
77 REPORTER_ASSERT(reporter, b1.empty() == b2.empty()); | 58 REPORTER_ASSERT(reporter, b1.empty() == b2.empty()); |
78 REPORTER_ASSERT(reporter, b1.width() == b2.width()); | 59 REPORTER_ASSERT(reporter, b1.width() == b2.width()); |
79 REPORTER_ASSERT(reporter, b1.height() == b2.height()); | 60 REPORTER_ASSERT(reporter, b1.height() == b2.height()); |
80 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull()); | 61 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull()); |
81 SkAutoLockPixels autoLockPixels1(b1); | 62 SkAutoLockPixels autoLockPixels1(b1); |
82 SkAutoLockPixels autoLockPixels2(b2); | 63 SkAutoLockPixels autoLockPixels2(b2); |
83 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull()); | 64 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull()); |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 SkAutoDataUnref encoded(create_data_from_bitmap(original, type)); | 116 SkAutoDataUnref encoded(create_data_from_bitmap(original, type)); |
136 REPORTER_ASSERT(reporter, encoded.get() != NULL); | 117 REPORTER_ASSERT(reporter, encoded.get() != NULL); |
137 if (NULL != encoded.get()) { | 118 if (NULL != encoded.get()) { |
138 bool comparePixels = (SkImageEncoder::kPNG_Type == type); | 119 bool comparePixels = (SkImageEncoder::kPNG_Type == type); |
139 comp(reporter, encoded, original, comparePixels); | 120 comp(reporter, encoded, original, comparePixels); |
140 } | 121 } |
141 } | 122 } |
142 } | 123 } |
143 | 124 |
144 /** | 125 /** |
145 * This checks to see that a SkLazyPixelRef works as advertised. | |
146 */ | |
147 static 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 } | |
170 DEF_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. | 126 * This checks to see that a SkLazyCachedPixelRef works as advertised. |
178 */ | 127 */ |
179 | 128 |
180 static void compare_with_skLazyCachedPixelRef(skiatest::Reporter* reporter, | 129 static void compare_with_skLazyCachedPixelRef(skiatest::Reporter* reporter, |
181 SkData* encoded, | 130 SkData* encoded, |
182 const SkBitmap& original, | 131 const SkBitmap& original, |
183 bool comparePixels) { | 132 bool comparePixels) { |
184 SkBitmap lazy; | 133 SkBitmap lazy; |
185 static const SkBitmapFactory::DecodeProc decoder = | 134 static const SkLazyCachingPixelRef::DecodeProc decoder = |
186 &(SkImageDecoder::DecodeMemoryToTarget); | 135 &(SkImageDecoder::DecodeMemoryToTarget); |
187 bool success = SkLazyCachingPixelRef::Install(decoder, encoded, &lazy); | 136 bool success = SkLazyCachingPixelRef::Install(decoder, encoded, &lazy); |
188 REPORTER_ASSERT(reporter, success); | 137 REPORTER_ASSERT(reporter, success); |
189 | 138 |
190 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | 139 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
191 { | 140 { |
192 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | 141 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
193 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | 142 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); |
194 } | 143 } |
195 // pixels should be gone! | 144 // pixels should be gone! |
196 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | 145 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
197 { | 146 { |
198 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | 147 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
199 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | 148 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); |
200 } | 149 } |
201 compare_bitmaps(reporter, original, lazy, comparePixels); | 150 compare_bitmaps(reporter, original, lazy, comparePixels); |
202 } | 151 } |
203 DEF_TEST(LazyCachedPixelRef, reporter) { | 152 DEF_TEST(LazyCachedPixelRef, reporter) { |
204 test_three_encodings(reporter, compare_with_skLazyCachedPixelRef); | 153 test_three_encodings(reporter, compare_with_skLazyCachedPixelRef); |
205 } | 154 } |
206 | 155 |
207 class TestPixelRef : public SkCachingPixelRef { | 156 class TestPixelRef : public SkCachingPixelRef { |
208 public: | 157 public: |
209 TestPixelRef(int x) : fX(x) { } | 158 explicit TestPixelRef(int x) : fX(x) { } |
210 virtual ~TestPixelRef() { } | 159 virtual ~TestPixelRef() { } |
211 static bool Install(SkBitmap* destination, int x) { | 160 static bool Install(SkBitmap* destination, int x) { |
212 SkAutoTUnref<TestPixelRef> ref(SkNEW_ARGS(TestPixelRef, (x))); | 161 SkAutoTUnref<TestPixelRef> ref(SkNEW_ARGS(TestPixelRef, (x))); |
213 return ref->configure(destination) && destination->setPixelRef(ref); | 162 return ref->configure(destination) && destination->setPixelRef(ref); |
214 } | 163 } |
215 SK_DECLARE_UNFLATTENABLE_OBJECT() | 164 SK_DECLARE_UNFLATTENABLE_OBJECT() |
| 165 |
216 protected: | 166 protected: |
217 virtual bool onDecodeInfo(SkImageInfo* info) SK_OVERRIDE { | 167 virtual bool onDecodeInfo(SkImageInfo* info) SK_OVERRIDE { |
218 if (fX == 0) { | 168 if (fX == 0) { |
219 return false; | 169 return false; |
220 } | 170 } |
221 SkASSERT(info); | 171 SkASSERT(info); |
222 info->fWidth = 10; | 172 info->fWidth = 10; |
223 info->fHeight = 10; | 173 info->fHeight = 10; |
224 info->fColorType = kRGBA_8888_SkColorType; | 174 info->fColorType = kRGBA_8888_SkColorType; |
225 info->fAlphaType = kOpaque_SkAlphaType; | 175 info->fAlphaType = kOpaque_SkAlphaType; |
226 return true; | 176 return true; |
227 } | 177 } |
228 virtual bool onDecodePixels(const SkImageInfo& info, | 178 virtual bool onDecodePixels(const SkImageInfo& info, |
229 void* pixels, | 179 void* pixels, |
230 size_t rowBytes) SK_OVERRIDE { | 180 size_t rowBytes) SK_OVERRIDE { |
231 return false; | 181 return false; |
232 } | 182 } |
| 183 |
233 private: | 184 private: |
234 int fX; // controls where the failure happens | 185 int fX; // controls where the failure happens |
235 typedef SkCachingPixelRef INHERITED; | 186 typedef SkCachingPixelRef INHERITED; |
236 }; | 187 }; |
237 | 188 |
238 DEF_TEST(CachingPixelRef, reporter) { | 189 DEF_TEST(CachingPixelRef, reporter) { |
239 SkBitmap lazy; | 190 SkBitmap lazy; |
240 // test the error handling | 191 // test the error handling |
241 REPORTER_ASSERT(reporter, !TestPixelRef::Install(&lazy, 0)); | 192 REPORTER_ASSERT(reporter, !TestPixelRef::Install(&lazy, 0)); |
242 // onDecodeInfo should succeed, allowing installation | 193 // onDecodeInfo should succeed, allowing installation |
(...skipping 27 matching lines...) Expand all Loading... |
270 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | 221 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
271 { | 222 { |
272 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | 223 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
273 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | 224 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); |
274 } | 225 } |
275 compare_bitmaps(reporter, original, lazy, comparePixels); | 226 compare_bitmaps(reporter, original, lazy, comparePixels); |
276 } | 227 } |
277 DEF_TEST(DecodingImageGenerator, reporter) { | 228 DEF_TEST(DecodingImageGenerator, reporter) { |
278 test_three_encodings(reporter, compare_with_SkDecodingImageGenerator); | 229 test_three_encodings(reporter, compare_with_SkDecodingImageGenerator); |
279 } | 230 } |
OLD | NEW |