| 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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 SkImageEncoder::Type type = types[i]; | 115 SkImageEncoder::Type type = types[i]; |
| 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 /** | |
| 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 | 125 |
| 176 /** | 126 /** |
| 177 * This checks to see that a SkLazyCachedPixelRef works as advertised. | 127 * This checks to see that a SkLazyCachedPixelRef works as advertised. |
| 178 */ | 128 */ |
| 179 | 129 |
| 180 static void compare_with_skLazyCachedPixelRef(skiatest::Reporter* reporter, | 130 static void compare_with_skLazyCachedPixelRef(skiatest::Reporter* reporter, |
| 181 SkData* encoded, | 131 SkData* encoded, |
| 182 const SkBitmap& original, | 132 const SkBitmap& original, |
| 183 bool comparePixels) { | 133 bool comparePixels) { |
| 184 SkBitmap lazy; | 134 SkBitmap lazy; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 199 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | 149 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); |
| 200 } | 150 } |
| 201 compare_bitmaps(reporter, original, lazy, comparePixels); | 151 compare_bitmaps(reporter, original, lazy, comparePixels); |
| 202 } | 152 } |
| 203 DEF_TEST(LazyCachedPixelRef, reporter) { | 153 DEF_TEST(LazyCachedPixelRef, reporter) { |
| 204 test_three_encodings(reporter, compare_with_skLazyCachedPixelRef); | 154 test_three_encodings(reporter, compare_with_skLazyCachedPixelRef); |
| 205 } | 155 } |
| 206 | 156 |
| 207 class TestPixelRef : public SkCachingPixelRef { | 157 class TestPixelRef : public SkCachingPixelRef { |
| 208 public: | 158 public: |
| 209 TestPixelRef(int x) : fX(x) { } | 159 explicit TestPixelRef(int x) : fX(x) { } |
| 210 virtual ~TestPixelRef() { } | 160 virtual ~TestPixelRef() { } |
| 211 static bool Install(SkBitmap* destination, int x) { | 161 static bool Install(SkBitmap* destination, int x) { |
| 212 SkAutoTUnref<TestPixelRef> ref(SkNEW_ARGS(TestPixelRef, (x))); | 162 SkAutoTUnref<TestPixelRef> ref(SkNEW_ARGS(TestPixelRef, (x))); |
| 213 return ref->configure(destination) && destination->setPixelRef(ref); | 163 return ref->configure(destination) && destination->setPixelRef(ref); |
| 214 } | 164 } |
| 215 SK_DECLARE_UNFLATTENABLE_OBJECT() | 165 SK_DECLARE_UNFLATTENABLE_OBJECT() |
| 166 |
| 216 protected: | 167 protected: |
| 217 virtual bool onDecodeInfo(SkImageInfo* info) SK_OVERRIDE { | 168 virtual bool onDecodeInfo(SkImageInfo* info) SK_OVERRIDE { |
| 218 if (fX == 0) { | 169 if (fX == 0) { |
| 219 return false; | 170 return false; |
| 220 } | 171 } |
| 221 SkASSERT(info); | 172 SkASSERT(info); |
| 222 info->fWidth = 10; | 173 info->fWidth = 10; |
| 223 info->fHeight = 10; | 174 info->fHeight = 10; |
| 224 info->fColorType = kRGBA_8888_SkColorType; | 175 info->fColorType = kRGBA_8888_SkColorType; |
| 225 info->fAlphaType = kOpaque_SkAlphaType; | 176 info->fAlphaType = kOpaque_SkAlphaType; |
| 226 return true; | 177 return true; |
| 227 } | 178 } |
| 228 virtual bool onDecodePixels(const SkImageInfo& info, | 179 virtual bool onDecodePixels(const SkImageInfo& info, |
| 229 void* pixels, | 180 void* pixels, |
| 230 size_t rowBytes) SK_OVERRIDE { | 181 size_t rowBytes) SK_OVERRIDE { |
| 231 return false; | 182 return false; |
| 232 } | 183 } |
| 184 |
| 233 private: | 185 private: |
| 234 int fX; // controls where the failure happens | 186 int fX; // controls where the failure happens |
| 235 typedef SkCachingPixelRef INHERITED; | 187 typedef SkCachingPixelRef INHERITED; |
| 236 }; | 188 }; |
| 237 | 189 |
| 238 DEF_TEST(CachingPixelRef, reporter) { | 190 DEF_TEST(CachingPixelRef, reporter) { |
| 239 SkBitmap lazy; | 191 SkBitmap lazy; |
| 240 // test the error handling | 192 // test the error handling |
| 241 REPORTER_ASSERT(reporter, !TestPixelRef::Install(&lazy, 0)); | 193 REPORTER_ASSERT(reporter, !TestPixelRef::Install(&lazy, 0)); |
| 242 // onDecodeInfo should succeed, allowing installation | 194 // onDecodeInfo should succeed, allowing installation |
| 243 REPORTER_ASSERT(reporter, TestPixelRef::Install(&lazy, 1)); | 195 REPORTER_ASSERT(reporter, TestPixelRef::Install(&lazy, 1)); |
| 244 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | 196 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
| 245 // onDecodePixels should fail, so getting pixels will fail. | 197 // onDecodePixels should fail, so getting pixels will fail. |
| 246 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | 198 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 247 } | 199 } |
| 248 | 200 |
| 249 static void compare_with_SkDecodingImageGenerator(skiatest::Reporter* reporter, | 201 static void compare_with_SkDecodingImageGenerator(skiatest::Reporter* reporter, |
| 250 SkData* encoded, | 202 SkData* encoded, |
| 251 const SkBitmap& original, | 203 const SkBitmap& original, |
| 252 bool comparePixels) { | 204 bool comparePixels) { |
| 253 | |
| 254 SkBitmap lazy; | 205 SkBitmap lazy; |
| 255 bool success = SkDecodingImageGenerator::Install(encoded, &lazy); | 206 bool success = SkDecodingImageGenerator::Install(encoded, &lazy); |
| 256 REPORTER_ASSERT(reporter, success); | 207 REPORTER_ASSERT(reporter, success); |
| 257 if (!success) { | 208 if (!success) { |
| 258 return; | 209 return; |
| 259 } | 210 } |
| 260 | 211 |
| 261 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | 212 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 262 { | 213 { |
| 263 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | 214 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
| 264 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | 215 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); |
| 265 if (NULL == lazy.getPixels()) { | 216 if (NULL == lazy.getPixels()) { |
| 266 return; | 217 return; |
| 267 } | 218 } |
| 268 } | 219 } |
| 269 // pixels should be gone! | 220 // pixels should be gone! |
| 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 |