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" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 static bool simple_bitmap_factory(SkBitmapFactory::DecodeProc proc, | 59 static bool simple_bitmap_factory(SkBitmapFactory::DecodeProc proc, |
60 SkData* data, | 60 SkData* data, |
61 SkBitmap* dst) { | 61 SkBitmap* dst) { |
62 SkImageInfo info; | 62 SkImageInfo info; |
63 if (!proc(data->data(), data->size(), &info, NULL)) { | 63 if (!proc(data->data(), data->size(), &info, NULL)) { |
64 return false; | 64 return false; |
65 } | 65 } |
66 dst->setConfig(SkImageInfoToBitmapConfig(info), info.fWidth, | 66 dst->setConfig(SkImageInfoToBitmapConfig(info), info.fWidth, |
67 info.fHeight, 0, info.fAlphaType); | 67 info.fHeight, 0, info.fAlphaType); |
68 SkAutoTUnref<SkLazyPixelRef> ref(SkNEW_ARGS(SkLazyPixelRef, | 68 SkAutoTUnref<SkLazyPixelRef> ref(SkNEW_ARGS(SkLazyPixelRef, |
69 (data, proc, NULL))); | 69 (info, data, proc, NULL))); |
70 dst->setPixelRef(ref); | 70 dst->setPixelRef(ref); |
71 return true; | 71 return true; |
72 } | 72 } |
73 | 73 |
74 static void compare_bitmaps(skiatest::Reporter* reporter, | 74 static void compare_bitmaps(skiatest::Reporter* reporter, |
75 const SkBitmap& b1, const SkBitmap& b2, | 75 const SkBitmap& b1, const SkBitmap& b2, |
76 bool pixelPerfect = true) { | 76 bool pixelPerfect = true) { |
77 REPORTER_ASSERT(reporter, b1.empty() == b2.empty()); | 77 REPORTER_ASSERT(reporter, b1.empty() == b2.empty()); |
78 REPORTER_ASSERT(reporter, b1.width() == b2.width()); | 78 REPORTER_ASSERT(reporter, b1.width() == b2.width()); |
79 REPORTER_ASSERT(reporter, b1.height() == b2.height()); | 79 REPORTER_ASSERT(reporter, b1.height() == b2.height()); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | 198 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
199 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | 199 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); |
200 } | 200 } |
201 compare_bitmaps(reporter, original, lazy, comparePixels); | 201 compare_bitmaps(reporter, original, lazy, comparePixels); |
202 } | 202 } |
203 DEF_TEST(LazyCachedPixelRef, reporter) { | 203 DEF_TEST(LazyCachedPixelRef, reporter) { |
204 test_three_encodings(reporter, compare_with_skLazyCachedPixelRef); | 204 test_three_encodings(reporter, compare_with_skLazyCachedPixelRef); |
205 } | 205 } |
206 | 206 |
207 class TestPixelRef : public SkCachingPixelRef { | 207 class TestPixelRef : public SkCachingPixelRef { |
| 208 static SkImageInfo cons_up_info() { |
| 209 SkImageInfo info; |
| 210 info.fWidth = 10; |
| 211 info.fHeight = 10; |
| 212 info.fColorType = kRGBA_8888_SkColorType; |
| 213 info.fAlphaType = kOpaque_SkAlphaType; |
| 214 return info; |
| 215 } |
208 public: | 216 public: |
209 TestPixelRef(int x) : fX(x) { } | 217 TestPixelRef(int x) |
| 218 : INHERITED(cons_up_info()) |
| 219 , fX(x) { } |
210 virtual ~TestPixelRef() { } | 220 virtual ~TestPixelRef() { } |
211 static bool Install(SkBitmap* destination, int x) { | 221 static bool Install(SkBitmap* destination, int x) { |
212 SkAutoTUnref<TestPixelRef> ref(SkNEW_ARGS(TestPixelRef, (x))); | 222 SkAutoTUnref<TestPixelRef> ref(SkNEW_ARGS(TestPixelRef, (x))); |
213 return ref->configure(destination) && destination->setPixelRef(ref); | 223 return ref->configure(destination) && destination->setPixelRef(ref); |
214 } | 224 } |
215 SK_DECLARE_UNFLATTENABLE_OBJECT() | 225 SK_DECLARE_UNFLATTENABLE_OBJECT() |
216 protected: | 226 protected: |
217 virtual bool onDecodeInfo(SkImageInfo* info) SK_OVERRIDE { | 227 virtual bool onDecodePixels(void* pixels, |
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 { | 228 size_t rowBytes) SK_OVERRIDE { |
231 return false; | 229 return fX != 0; |
232 } | 230 } |
233 private: | 231 private: |
234 int fX; // controls where the failure happens | 232 int fX; // controls where the failure happens |
235 typedef SkCachingPixelRef INHERITED; | 233 typedef SkCachingPixelRef INHERITED; |
236 }; | 234 }; |
237 | 235 |
238 DEF_TEST(CachingPixelRef, reporter) { | 236 DEF_TEST(CachingPixelRef, reporter) { |
239 SkBitmap lazy; | 237 SkBitmap lazy; |
240 // test the error handling | 238 |
241 REPORTER_ASSERT(reporter, !TestPixelRef::Install(&lazy, 0)); | 239 { |
242 // onDecodeInfo should succeed, allowing installation | 240 REPORTER_ASSERT(reporter, TestPixelRef::Install(&lazy, 1)); |
243 REPORTER_ASSERT(reporter, TestPixelRef::Install(&lazy, 1)); | 241 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
244 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | 242 // onDecodePixels should succeed. |
245 // onDecodePixels should fail, so getting pixels will fail. | 243 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); |
246 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | 244 } |
| 245 { |
| 246 // onDecodeInfo should succeed, allowing installation |
| 247 REPORTER_ASSERT(reporter, TestPixelRef::Install(&lazy, 0)); |
| 248 SkAutoLockPixels autoLockPixels(lazy); // now pixels are bad. |
| 249 // onDecodePixels should fail. |
| 250 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 251 } |
247 } | 252 } |
248 | 253 |
249 static void compare_with_SkDecodingImageGenerator(skiatest::Reporter* reporter, | 254 static void compare_with_SkDecodingImageGenerator(skiatest::Reporter* reporter, |
250 SkData* encoded, | 255 SkData* encoded, |
251 const SkBitmap& original, | 256 const SkBitmap& original, |
252 bool comparePixels) { | 257 bool comparePixels) { |
253 | 258 |
254 SkBitmap lazy; | 259 SkBitmap lazy; |
255 bool success = SkDecodingImageGenerator::Install(encoded, &lazy); | 260 bool success = SkDecodingImageGenerator::Install(encoded, &lazy); |
256 REPORTER_ASSERT(reporter, success); | 261 REPORTER_ASSERT(reporter, success); |
(...skipping 13 matching lines...) Expand all Loading... |
270 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | 275 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
271 { | 276 { |
272 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | 277 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
273 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | 278 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); |
274 } | 279 } |
275 compare_bitmaps(reporter, original, lazy, comparePixels); | 280 compare_bitmaps(reporter, original, lazy, comparePixels); |
276 } | 281 } |
277 DEF_TEST(DecodingImageGenerator, reporter) { | 282 DEF_TEST(DecodingImageGenerator, reporter) { |
278 test_three_encodings(reporter, compare_with_SkDecodingImageGenerator); | 283 test_three_encodings(reporter, compare_with_SkDecodingImageGenerator); |
279 } | 284 } |
OLD | NEW |