| 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" | 16 #include "SkLazyPixelRef.h" |
| 17 #include "SkScaledImageCache.h" | 17 #include "SkScaledImageCache.h" |
| 18 #include "SkStream.h" | 18 #include "SkStream.h" |
| 19 #include "SkLruImageCache.h" |
| 19 | 20 |
| 20 #include "Test.h" | 21 #include "Test.h" |
| 21 #include "TestClassDef.h" | 22 #include "TestClassDef.h" |
| 22 | 23 |
| 23 __SK_FORCE_IMAGE_DECODER_LINKING; | 24 __SK_FORCE_IMAGE_DECODER_LINKING; |
| 24 | 25 |
| 25 /** | 26 /** |
| 26 * Fill this bitmap with some color. | 27 * Fill this bitmap with some color. |
| 27 */ | 28 */ |
| 28 static void make_test_image(SkBitmap* bm) { | 29 static void make_test_image(SkBitmap* bm) { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 52 } | 53 } |
| 53 return NULL; | 54 return NULL; |
| 54 } | 55 } |
| 55 | 56 |
| 56 /** | 57 /** |
| 57 * A simplified version of SkBitmapFactory | 58 * A simplified version of SkBitmapFactory |
| 58 */ | 59 */ |
| 59 static bool simple_bitmap_factory(SkBitmapFactory::DecodeProc proc, | 60 static bool simple_bitmap_factory(SkBitmapFactory::DecodeProc proc, |
| 60 SkData* data, | 61 SkData* data, |
| 61 SkBitmap* dst) { | 62 SkBitmap* dst) { |
| 62 SkImageInfo info; | 63 SkBitmapFactory fact(proc); |
| 63 if (!proc(data->data(), data->size(), &info, NULL)) { | 64 size_t budget = 1000000; |
| 64 return false; | 65 SkAutoTUnref<SkImageCache> cache(SkNEW_ARGS(SkLruImageCache, (budget))); |
| 65 } | 66 fact.setImageCache(cache.get()); |
| 66 dst->setConfig(SkImageInfoToBitmapConfig(info), info.fWidth, | 67 return fact.installPixelRef(data, dst); |
| 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 } | 68 } |
| 73 | 69 |
| 74 static void compare_bitmaps(skiatest::Reporter* reporter, | 70 static void compare_bitmaps(skiatest::Reporter* reporter, |
| 75 const SkBitmap& b1, const SkBitmap& b2, | 71 const SkBitmap& b1, const SkBitmap& b2, |
| 76 bool pixelPerfect = true) { | 72 bool pixelPerfect = true) { |
| 77 REPORTER_ASSERT(reporter, b1.empty() == b2.empty()); | 73 REPORTER_ASSERT(reporter, b1.empty() == b2.empty()); |
| 78 REPORTER_ASSERT(reporter, b1.width() == b2.width()); | 74 REPORTER_ASSERT(reporter, b1.width() == b2.width()); |
| 79 REPORTER_ASSERT(reporter, b1.height() == b2.height()); | 75 REPORTER_ASSERT(reporter, b1.height() == b2.height()); |
| 80 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull()); | 76 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull()); |
| 81 SkAutoLockPixels autoLockPixels1(b1); | 77 SkAutoLockPixels autoLockPixels1(b1); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 { | 193 { |
| 198 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | 194 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
| 199 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | 195 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); |
| 200 } | 196 } |
| 201 compare_bitmaps(reporter, original, lazy, comparePixels); | 197 compare_bitmaps(reporter, original, lazy, comparePixels); |
| 202 } | 198 } |
| 203 DEF_TEST(LazyCachedPixelRef, reporter) { | 199 DEF_TEST(LazyCachedPixelRef, reporter) { |
| 204 test_three_encodings(reporter, compare_with_skLazyCachedPixelRef); | 200 test_three_encodings(reporter, compare_with_skLazyCachedPixelRef); |
| 205 } | 201 } |
| 206 | 202 |
| 203 namespace { |
| 207 class TestPixelRef : public SkCachingPixelRef { | 204 class TestPixelRef : public SkCachingPixelRef { |
| 208 public: | 205 public: |
| 209 TestPixelRef(int x) : fX(x) { } | 206 explicit TestPixelRef(int x) : fX(x) { } |
| 210 virtual ~TestPixelRef() { } | 207 virtual ~TestPixelRef() { } |
| 211 static bool Install(SkBitmap* destination, int x) { | 208 static bool Install(SkBitmap* destination, int x) { |
| 212 SkAutoTUnref<TestPixelRef> ref(SkNEW_ARGS(TestPixelRef, (x))); | 209 SkAutoTUnref<TestPixelRef> ref(SkNEW_ARGS(TestPixelRef, (x))); |
| 213 return ref->configure(destination) && destination->setPixelRef(ref); | 210 return ref->configure(destination) && destination->setPixelRef(ref); |
| 214 } | 211 } |
| 215 SK_DECLARE_UNFLATTENABLE_OBJECT() | 212 SK_DECLARE_UNFLATTENABLE_OBJECT() |
| 213 |
| 216 protected: | 214 protected: |
| 217 virtual bool onDecodeInfo(SkImageInfo* info) SK_OVERRIDE { | 215 virtual bool onDecodeInfo(SkImageInfo* info) SK_OVERRIDE { |
| 218 if (fX == 0) { | 216 if (fX == 0) { |
| 219 return false; | 217 return false; |
| 220 } | 218 } |
| 221 SkASSERT(info); | 219 SkASSERT(info); |
| 222 info->fWidth = 10; | 220 info->fWidth = 10; |
| 223 info->fHeight = 10; | 221 info->fHeight = 10; |
| 224 info->fColorType = kRGBA_8888_SkColorType; | 222 info->fColorType = kRGBA_8888_SkColorType; |
| 225 info->fAlphaType = kOpaque_SkAlphaType; | 223 info->fAlphaType = kOpaque_SkAlphaType; |
| 226 return true; | 224 return true; |
| 227 } | 225 } |
| 228 virtual bool onDecodePixels(const SkImageInfo& info, | 226 virtual bool onDecodePixels(const SkImageInfo& info, |
| 229 void* pixels, | 227 void* pixels, |
| 230 size_t rowBytes) SK_OVERRIDE { | 228 size_t rowBytes) SK_OVERRIDE { |
| 231 return false; | 229 return false; |
| 232 } | 230 } |
| 231 |
| 233 private: | 232 private: |
| 234 int fX; // controls where the failure happens | 233 int fX; // controls where the failure happens |
| 235 typedef SkCachingPixelRef INHERITED; | 234 typedef SkCachingPixelRef INHERITED; |
| 236 }; | 235 }; |
| 236 } // namespace |
| 237 | 237 |
| 238 DEF_TEST(CachingPixelRef, reporter) { | 238 DEF_TEST(CachingPixelRef, reporter) { |
| 239 SkBitmap lazy; | 239 SkBitmap lazy; |
| 240 // test the error handling | 240 // test the error handling |
| 241 REPORTER_ASSERT(reporter, !TestPixelRef::Install(&lazy, 0)); | 241 REPORTER_ASSERT(reporter, !TestPixelRef::Install(&lazy, 0)); |
| 242 // onDecodeInfo should succeed, allowing installation | 242 // onDecodeInfo should succeed, allowing installation |
| 243 REPORTER_ASSERT(reporter, TestPixelRef::Install(&lazy, 1)); | 243 REPORTER_ASSERT(reporter, TestPixelRef::Install(&lazy, 1)); |
| 244 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | 244 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
| 245 // onDecodePixels should fail, so getting pixels will fail. | 245 // onDecodePixels should fail, so getting pixels will fail. |
| 246 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | 246 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 247 } | 247 } |
| 248 | 248 |
| 249 static void compare_with_SkDecodingImageGenerator(skiatest::Reporter* reporter, | 249 static void compare_with_SkDecodingImageGenerator(skiatest::Reporter* reporter, |
| 250 SkData* encoded, | 250 SkData* encoded, |
| 251 const SkBitmap& original, | 251 const SkBitmap& original, |
| 252 bool comparePixels) { | 252 bool comparePixels) { |
| 253 | |
| 254 SkBitmap lazy; | 253 SkBitmap lazy; |
| 255 bool success = SkDecodingImageGenerator::Install(encoded, &lazy); | 254 bool success = SkDecodingImageGenerator::Install(encoded, &lazy); |
| 256 REPORTER_ASSERT(reporter, success); | 255 REPORTER_ASSERT(reporter, success); |
| 257 if (!success) { | 256 if (!success) { |
| 258 return; | 257 return; |
| 259 } | 258 } |
| 260 | 259 |
| 261 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | 260 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 262 { | 261 { |
| 263 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | 262 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
| 264 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | 263 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); |
| 265 if (NULL == lazy.getPixels()) { | 264 if (NULL == lazy.getPixels()) { |
| 266 return; | 265 return; |
| 267 } | 266 } |
| 268 } | 267 } |
| 269 // pixels should be gone! | 268 // pixels should be gone! |
| 270 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | 269 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 271 { | 270 { |
| 272 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | 271 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
| 273 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | 272 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); |
| 274 } | 273 } |
| 275 compare_bitmaps(reporter, original, lazy, comparePixels); | 274 compare_bitmaps(reporter, original, lazy, comparePixels); |
| 276 } | 275 } |
| 277 DEF_TEST(DecodingImageGenerator, reporter) { | 276 DEF_TEST(DecodingImageGenerator, reporter) { |
| 278 test_three_encodings(reporter, compare_with_SkDecodingImageGenerator); | 277 test_three_encodings(reporter, compare_with_SkDecodingImageGenerator); |
| 279 } | 278 } |
| OLD | NEW |