| 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" | |
| 16 #include "SkLazyPixelRef.h" | 15 #include "SkLazyPixelRef.h" |
| 16 #include "SkCachingPixelRef.h" |
| 17 #include "SkScaledImageCache.h" | 17 #include "SkScaledImageCache.h" |
| 18 #include "SkStream.h" | 18 #include "SkStream.h" |
| 19 | 19 |
| 20 #include "Test.h" | 20 #include "Test.h" |
| 21 #include "TestClassDef.h" | 21 #include "TestClassDef.h" |
| 22 | 22 |
| 23 __SK_FORCE_IMAGE_DECODER_LINKING; | 23 __SK_FORCE_IMAGE_DECODER_LINKING; |
| 24 | 24 |
| 25 /** | 25 /** |
| 26 * Fill this bitmap with some color. | 26 * Fill this bitmap with some color. |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 for (int x = 0; x < b2.width(); ++x) { | 102 for (int x = 0; x < b2.width(); ++x) { |
| 103 if (b1.getColor(x, y) != b2.getColor(x, y)) { | 103 if (b1.getColor(x, y) != b2.getColor(x, y)) { |
| 104 ++pixelErrors; | 104 ++pixelErrors; |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 } | 107 } |
| 108 REPORTER_ASSERT(reporter, 0 == pixelErrors); | 108 REPORTER_ASSERT(reporter, 0 == pixelErrors); |
| 109 } | 109 } |
| 110 | 110 |
| 111 | 111 |
| 112 typedef void(*CompareEncodedToOriginal)(skiatest::Reporter* reporter, | 112 typedef bool (*InstallEncoded)(SkData* encoded, SkBitmap* dst); |
| 113 SkData* encoded, | 113 |
| 114 const SkBitmap& original, | |
| 115 bool pixelPerfect); | |
| 116 /** | 114 /** |
| 117 this function tests three differently encoded images against the | 115 This function tests three differently encoded images against the |
| 118 original bitmap */ | 116 original bitmap */ |
| 119 static void test_three_encodings(skiatest::Reporter* reporter, | 117 static void test_three_encodings(skiatest::Reporter* reporter, |
| 120 CompareEncodedToOriginal comp) { | 118 InstallEncoded install) { |
| 121 SkBitmap original; | 119 SkBitmap original; |
| 122 make_test_image(&original); | 120 make_test_image(&original); |
| 123 REPORTER_ASSERT(reporter, !original.empty()); | 121 REPORTER_ASSERT(reporter, !original.empty()); |
| 124 REPORTER_ASSERT(reporter, !original.isNull()); | 122 REPORTER_ASSERT(reporter, !original.isNull()); |
| 125 if (original.empty() || original.isNull()) { | 123 if (original.empty() || original.isNull()) { |
| 126 return; | 124 return; |
| 127 } | 125 } |
| 128 static const SkImageEncoder::Type types[] = { | 126 static const SkImageEncoder::Type types[] = { |
| 129 SkImageEncoder::kPNG_Type, | 127 SkImageEncoder::kPNG_Type, |
| 130 SkImageEncoder::kJPEG_Type, | 128 SkImageEncoder::kJPEG_Type, |
| 131 SkImageEncoder::kWEBP_Type | 129 SkImageEncoder::kWEBP_Type |
| 132 }; | 130 }; |
| 133 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) { | 131 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) { |
| 134 SkImageEncoder::Type type = types[i]; | 132 SkImageEncoder::Type type = types[i]; |
| 135 SkAutoDataUnref encoded(create_data_from_bitmap(original, type)); | 133 SkAutoDataUnref encoded(create_data_from_bitmap(original, type)); |
| 136 REPORTER_ASSERT(reporter, encoded.get() != NULL); | 134 REPORTER_ASSERT(reporter, encoded.get() != NULL); |
| 137 if (NULL != encoded.get()) { | 135 if (NULL == encoded.get()) { |
| 138 bool comparePixels = (SkImageEncoder::kPNG_Type == type); | 136 continue; |
| 139 comp(reporter, encoded, original, comparePixels); | |
| 140 } | 137 } |
| 138 SkBitmap lazy; |
| 139 bool installSuccess = install(encoded.get(), &lazy); |
| 140 REPORTER_ASSERT(reporter, installSuccess); |
| 141 if (!installSuccess) { |
| 142 continue; |
| 143 } |
| 144 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 145 { |
| 146 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
| 147 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); |
| 148 if (NULL == lazy.getPixels()) { |
| 149 continue; |
| 150 } |
| 151 } |
| 152 // pixels should be gone! |
| 153 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 154 { |
| 155 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
| 156 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); |
| 157 if (NULL == lazy.getPixels()) { |
| 158 continue; |
| 159 } |
| 160 } |
| 161 bool comparePixels = (SkImageEncoder::kPNG_Type == type); |
| 162 compare_bitmaps(reporter, original, lazy, comparePixels); |
| 141 } | 163 } |
| 142 } | 164 } |
| 143 | 165 |
| 166 |
| 167 //////////////////////////////////////////////////////////////////////////////// |
| 144 /** | 168 /** |
| 145 * This checks to see that a SkLazyPixelRef works as advertised. | 169 * This checks to see that a SkLazyPixelRef works as advertised. |
| 146 */ | 170 */ |
| 147 static void compare_with_skLazyPixelRef(skiatest::Reporter* reporter, | 171 bool install_skLazyPixelRef(SkData* encoded, SkBitmap* dst) { |
| 148 SkData* encoded, | |
| 149 const SkBitmap& original, | |
| 150 bool comparePixels) { | |
| 151 SkBitmap lazy; | |
| 152 static const SkBitmapFactory::DecodeProc decoder = | 172 static const SkBitmapFactory::DecodeProc decoder = |
| 153 &(SkImageDecoder::DecodeMemoryToTarget); | 173 &(SkImageDecoder::DecodeMemoryToTarget); |
| 154 bool success = simple_bitmap_factory(decoder, encoded, &lazy); | 174 return simple_bitmap_factory(decoder, encoded, dst); |
| 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 } | 175 } |
| 170 DEF_TEST(LazyPixelRef, reporter) { | 176 DEF_TEST(LazyPixelRef, reporter) { |
| 171 test_three_encodings(reporter, compare_with_skLazyPixelRef); | 177 test_three_encodings(reporter, install_skLazyPixelRef); |
| 172 } | 178 } |
| 173 | 179 |
| 174 | 180 //////////////////////////////////////////////////////////////////////////////// |
| 175 | |
| 176 /** | 181 /** |
| 177 * This checks to see that a SkLazyCachedPixelRef works as advertised. | 182 * This checks to see that a SkCachingPixelRef works as advertised. |
| 178 */ | 183 */ |
| 179 | 184 bool install_skCachingPixelRef(SkData* encoded, SkBitmap* dst) { |
| 180 static void compare_with_skLazyCachedPixelRef(skiatest::Reporter* reporter, | 185 return SkCachingPixelRef::Install( |
| 181 SkData* encoded, | 186 SkNEW_ARGS(SkDecodingImageGenerator, (encoded)), dst); |
| 182 const SkBitmap& original, | |
| 183 bool comparePixels) { | |
| 184 SkBitmap lazy; | |
| 185 static const SkBitmapFactory::DecodeProc decoder = | |
| 186 &(SkImageDecoder::DecodeMemoryToTarget); | |
| 187 bool success = SkLazyCachingPixelRef::Install(decoder, encoded, &lazy); | |
| 188 REPORTER_ASSERT(reporter, success); | |
| 189 | |
| 190 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | |
| 191 { | |
| 192 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | |
| 193 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | |
| 194 } | |
| 195 // pixels should be gone! | |
| 196 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | |
| 197 { | |
| 198 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | |
| 199 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | |
| 200 } | |
| 201 compare_bitmaps(reporter, original, lazy, comparePixels); | |
| 202 } | 187 } |
| 203 DEF_TEST(LazyCachedPixelRef, reporter) { | 188 DEF_TEST(CachingPixelRef, reporter) { |
| 204 test_three_encodings(reporter, compare_with_skLazyCachedPixelRef); | 189 test_three_encodings(reporter, install_skCachingPixelRef); |
| 205 } | 190 } |
| 206 | 191 |
| 207 class TestPixelRef : public SkCachingPixelRef { | 192 //////////////////////////////////////////////////////////////////////////////// |
| 208 public: | 193 /** |
| 209 TestPixelRef(int x) : fX(x) { } | 194 * This checks to see that a SkDecodingImageGenerator works as advertised. |
| 210 virtual ~TestPixelRef() { } | 195 */ |
| 211 static bool Install(SkBitmap* destination, int x) { | 196 DEF_TEST(DecodingImageGenerator, reporter) { |
| 212 SkAutoTUnref<TestPixelRef> ref(SkNEW_ARGS(TestPixelRef, (x))); | 197 test_three_encodings(reporter, SkDecodingImageGenerator::Install); |
| 213 return ref->configure(destination) && destination->setPixelRef(ref); | |
| 214 } | |
| 215 SK_DECLARE_UNFLATTENABLE_OBJECT() | |
| 216 protected: | |
| 217 virtual bool onDecodeInfo(SkImageInfo* info) SK_OVERRIDE { | |
| 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 { | |
| 231 return false; | |
| 232 } | |
| 233 private: | |
| 234 int fX; // controls where the failure happens | |
| 235 typedef SkCachingPixelRef INHERITED; | |
| 236 }; | |
| 237 | |
| 238 DEF_TEST(CachingPixelRef, reporter) { | |
| 239 SkBitmap lazy; | |
| 240 // test the error handling | |
| 241 REPORTER_ASSERT(reporter, !TestPixelRef::Install(&lazy, 0)); | |
| 242 // onDecodeInfo should succeed, allowing installation | |
| 243 REPORTER_ASSERT(reporter, TestPixelRef::Install(&lazy, 1)); | |
| 244 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | |
| 245 // onDecodePixels should fail, so getting pixels will fail. | |
| 246 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | |
| 247 } | 198 } |
| 248 | |
| 249 static void compare_with_SkDecodingImageGenerator(skiatest::Reporter* reporter, | |
| 250 SkData* encoded, | |
| 251 const SkBitmap& original, | |
| 252 bool comparePixels) { | |
| 253 | |
| 254 SkBitmap lazy; | |
| 255 bool success = SkDecodingImageGenerator::Install(encoded, &lazy); | |
| 256 REPORTER_ASSERT(reporter, success); | |
| 257 if (!success) { | |
| 258 return; | |
| 259 } | |
| 260 | |
| 261 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | |
| 262 { | |
| 263 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | |
| 264 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | |
| 265 if (NULL == lazy.getPixels()) { | |
| 266 return; | |
| 267 } | |
| 268 } | |
| 269 // pixels should be gone! | |
| 270 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | |
| 271 { | |
| 272 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | |
| 273 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | |
| 274 } | |
| 275 compare_bitmaps(reporter, original, lazy, comparePixels); | |
| 276 } | |
| 277 DEF_TEST(DecodingImageGenerator, reporter) { | |
| 278 test_three_encodings(reporter, compare_with_SkDecodingImageGenerator); | |
| 279 } | |
| OLD | NEW |