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 "SkForceLinking.h" | 11 #include "SkForceLinking.h" |
12 #include "SkImageDecoder.h" | 12 #include "SkImageDecoder.h" |
13 #include "SkImagePriv.h" | 13 #include "SkImagePriv.h" |
| 14 #include "SkLazyCachingPixelRef.h" |
14 #include "SkLazyPixelRef.h" | 15 #include "SkLazyPixelRef.h" |
15 #include "SkScaledImageCache.h" | 16 #include "SkScaledImageCache.h" |
16 #include "SkStream.h" | 17 #include "SkStream.h" |
| 18 |
17 #include "Test.h" | 19 #include "Test.h" |
| 20 #include "TestClassDef.h" |
18 | 21 |
19 __SK_FORCE_IMAGE_DECODER_LINKING; | 22 __SK_FORCE_IMAGE_DECODER_LINKING; |
20 | 23 |
21 /** | 24 /** |
22 * Fill this bitmap with some color. | 25 * Fill this bitmap with some color. |
23 */ | 26 */ |
24 static void make_test_image(SkBitmap* bm) { | 27 static void make_test_image(SkBitmap* bm) { |
25 static const int W = 50, H = 50; | 28 static const int W = 50, H = 50; |
26 static const SkBitmap::Config config = SkBitmap::kARGB_8888_Config; | 29 static const SkBitmap::Config config = SkBitmap::kARGB_8888_Config; |
27 bm->setConfig(config, W, H); | 30 bm->setConfig(config, W, H); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 if ((!(b1.getPixels())) || (!(b2.getPixels()))) { | 88 if ((!(b1.getPixels())) || (!(b2.getPixels()))) { |
86 return; | 89 return; |
87 } | 90 } |
88 if ((b1.width() != b2.width()) || | 91 if ((b1.width() != b2.width()) || |
89 (b1.height() != b2.height())) { | 92 (b1.height() != b2.height())) { |
90 return; | 93 return; |
91 } | 94 } |
92 if (!pixelPerfect) { | 95 if (!pixelPerfect) { |
93 return; | 96 return; |
94 } | 97 } |
| 98 |
95 int pixelErrors = 0; | 99 int pixelErrors = 0; |
96 for (int y = 0; y < b2.height(); ++y) { | 100 for (int y = 0; y < b2.height(); ++y) { |
97 for (int x = 0; x < b2.width(); ++x) { | 101 for (int x = 0; x < b2.width(); ++x) { |
98 if (b1.getColor(x, y) != b2.getColor(x, y)) { | 102 if (b1.getColor(x, y) != b2.getColor(x, y)) { |
99 ++pixelErrors; | 103 ++pixelErrors; |
100 } | 104 } |
101 } | 105 } |
102 } | 106 } |
103 REPORTER_ASSERT(reporter, 0 == pixelErrors); | 107 REPORTER_ASSERT(reporter, 0 == pixelErrors); |
104 } | 108 } |
105 | 109 |
| 110 |
| 111 typedef void(*CompareEncodedToOriginal)(skiatest::Reporter* reporter, |
| 112 SkData* encoded, |
| 113 const SkBitmap& original, |
| 114 bool pixelPerfect); |
106 /** | 115 /** |
107 * This checks to see that a SkLazyPixelRef works as advertized. | 116 this function tests three differently encoded images against the |
108 */ | 117 original bitmap */ |
109 #include "TestClassDef.h" | 118 static void test_three_encodings(skiatest::Reporter* reporter, |
110 DEF_TEST(CachedDecodingPixelRefTest, reporter) { | 119 CompareEncodedToOriginal comp) { |
111 SkBitmap original; | 120 SkBitmap original; |
112 make_test_image(&original); | 121 make_test_image(&original); |
113 const size_t bitmapSize = original.getSize(); | 122 REPORTER_ASSERT(reporter, !original.empty()); |
114 const size_t oldByteLimit = SkScaledImageCache::GetByteLimit(); | 123 REPORTER_ASSERT(reporter, !original.isNull()); |
115 REPORTER_ASSERT(reporter, (!(original.empty())) && (!(original.isNull()))); | 124 if (original.empty() || original.isNull()) { |
116 | 125 return; |
| 126 } |
117 static const SkImageEncoder::Type types[] = { | 127 static const SkImageEncoder::Type types[] = { |
118 SkImageEncoder::kPNG_Type, | 128 SkImageEncoder::kPNG_Type, |
119 SkImageEncoder::kJPEG_Type, | 129 SkImageEncoder::kJPEG_Type, |
120 SkImageEncoder::kWEBP_Type | 130 SkImageEncoder::kWEBP_Type |
121 }; | 131 }; |
122 | |
123 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) { | 132 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) { |
124 SkImageEncoder::Type type = types[i]; | 133 SkImageEncoder::Type type = types[i]; |
125 SkAutoDataUnref encoded(create_data_from_bitmap(original, type)); | 134 SkAutoDataUnref encoded(create_data_from_bitmap(original, type)); |
126 REPORTER_ASSERT(reporter, encoded.get() != NULL); | 135 REPORTER_ASSERT(reporter, encoded.get() != NULL); |
127 if (NULL == encoded.get()) { | 136 if (NULL != encoded.get()) { |
128 continue; | 137 bool comparePixels = (SkImageEncoder::kPNG_Type == type); |
| 138 comp(reporter, encoded, original, comparePixels); |
129 } | 139 } |
130 SkBitmap lazy; | |
131 static const SkBitmapFactory::DecodeProc decoder = | |
132 &(SkImageDecoder::DecodeMemoryToTarget); | |
133 bool success = simple_bitmap_factory(decoder, encoded.get(), &lazy); | |
134 | |
135 REPORTER_ASSERT(reporter, success); | |
136 | |
137 size_t bytesUsed = SkScaledImageCache::GetBytesUsed(); | |
138 | |
139 if (oldByteLimit < bitmapSize) { | |
140 SkScaledImageCache::SetByteLimit(bitmapSize + oldByteLimit); | |
141 } | |
142 void* lazyPixels = NULL; | |
143 | |
144 // Since this is lazy, it shouldn't have fPixels yet! | |
145 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | |
146 { | |
147 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | |
148 lazyPixels = lazy.getPixels(); | |
149 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | |
150 // first time we lock pixels, we should get bump in the size | |
151 // of the cache by exactly bitmapSize. | |
152 REPORTER_ASSERT(reporter, bytesUsed + bitmapSize | |
153 == SkScaledImageCache::GetBytesUsed()); | |
154 bytesUsed = SkScaledImageCache::GetBytesUsed(); | |
155 } | |
156 // pixels should be gone! | |
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 // verify that the same pixels are used this time. | |
163 REPORTER_ASSERT(reporter, lazy.getPixels() == lazyPixels); | |
164 } | |
165 | |
166 bool comparePixels = (SkImageEncoder::kPNG_Type == type); | |
167 // Only PNG is pixel-perfect. | |
168 compare_bitmaps(reporter, original, lazy, comparePixels); | |
169 | |
170 // force the cache to clear by making it too small. | |
171 SkScaledImageCache::SetByteLimit(bitmapSize / 2); | |
172 compare_bitmaps(reporter, original, lazy, comparePixels); | |
173 | |
174 // I'm pretty sure that the logic of the cache should mean | |
175 // that it will clear to zero, regardless of where it started. | |
176 REPORTER_ASSERT(reporter, SkScaledImageCache::GetBytesUsed() == 0); | |
177 // TODO(someone) - write a custom allocator that can verify | |
178 // that the memory where those pixels were cached really did | |
179 // get freed. | |
180 | |
181 //////////////////////////////////////////////////////////////////////// | |
182 // The following commented-out code happens to work on my | |
183 // machine, and indicates to me that the SkLazyPixelRef is | |
184 // behaving as designed. But I don't know an easy way to | |
185 // guarantee that a second allocation of the same size will | |
186 // give a different address. | |
187 //////////////////////////////////////////////////////////////////////// | |
188 // { | |
189 // // confuse the heap allocation system | |
190 // SkAutoMalloc autoMalloc(bitmapSize); | |
191 // REPORTER_ASSERT(reporter, autoMalloc.get() == lazyPixels); | |
192 // { | |
193 // SkAutoLockPixels autoLockPixels(lazy); | |
194 // // verify that *different* pixels are used this time. | |
195 // REPORTER_ASSERT(reporter, lazy.getPixels() != lazyPixels); | |
196 // compare_bitmaps(reporter, original, lazy, comparePixels); | |
197 // } | |
198 // } | |
199 | |
200 // restore cache size | |
201 SkScaledImageCache::SetByteLimit(oldByteLimit); | |
202 } | 140 } |
203 } | 141 } |
| 142 |
| 143 /** |
| 144 * This checks to see that a SkLazyPixelRef works as advertised. |
| 145 */ |
| 146 static void compare_with_skLazyPixelRef(skiatest::Reporter* reporter, |
| 147 SkData* encoded, |
| 148 const SkBitmap& original, |
| 149 bool comparePixels) { |
| 150 SkBitmap lazy; |
| 151 static const SkBitmapFactory::DecodeProc decoder = |
| 152 &(SkImageDecoder::DecodeMemoryToTarget); |
| 153 bool success = simple_bitmap_factory(decoder, encoded, &lazy); |
| 154 REPORTER_ASSERT(reporter, success); |
| 155 |
| 156 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 157 { |
| 158 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
| 159 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); |
| 160 } |
| 161 // pixels should be gone! |
| 162 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 163 { |
| 164 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
| 165 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); |
| 166 } |
| 167 compare_bitmaps(reporter, original, lazy, comparePixels); |
| 168 } |
| 169 DEF_TEST(LazyPixelRef, reporter) { |
| 170 test_three_encodings(reporter, compare_with_skLazyPixelRef); |
| 171 } |
| 172 |
| 173 |
| 174 |
| 175 /** |
| 176 * This checks to see that a SkLazyCachedPixelRef works as advertised. |
| 177 */ |
| 178 |
| 179 static void compare_with_skLazyCachedPixelRef(skiatest::Reporter* reporter, |
| 180 SkData* encoded, |
| 181 const SkBitmap& original, |
| 182 bool comparePixels) { |
| 183 SkBitmap lazy; |
| 184 static const SkBitmapFactory::DecodeProc decoder = |
| 185 &(SkImageDecoder::DecodeMemoryToTarget); |
| 186 bool success = SkLazyCachingPixelRef::Install(decoder, encoded, &lazy); |
| 187 REPORTER_ASSERT(reporter, success); |
| 188 |
| 189 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 190 { |
| 191 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
| 192 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); |
| 193 } |
| 194 // pixels should be gone! |
| 195 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 196 { |
| 197 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
| 198 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); |
| 199 } |
| 200 compare_bitmaps(reporter, original, lazy, comparePixels); |
| 201 } |
| 202 DEF_TEST(LazyCachedPixelRef, reporter) { |
| 203 test_three_encodings(reporter, compare_with_skLazyCachedPixelRef); |
| 204 } |
| 205 |
| 206 class TestPixelRef : public SkCachingPixelRef { |
| 207 public: |
| 208 TestPixelRef(int x) : fX(x) { } |
| 209 virtual ~TestPixelRef() { } |
| 210 static bool Install(SkBitmap* destination, int x) { |
| 211 SkAutoTUnref<TestPixelRef> ref(SkNEW_ARGS(TestPixelRef, (x))); |
| 212 return ref->configure(destination) && destination->setPixelRef(ref); |
| 213 } |
| 214 SK_DECLARE_UNFLATTENABLE_OBJECT() |
| 215 protected: |
| 216 virtual bool onDecodeInfo(SkImageInfo* info) SK_OVERRIDE { |
| 217 if (fX == 0) { |
| 218 return false; |
| 219 } |
| 220 SkASSERT(info); |
| 221 info->fWidth = 10; |
| 222 info->fHeight = 10; |
| 223 info->fColorType = kRGBA_8888_SkColorType; |
| 224 info->fAlphaType = kOpaque_SkAlphaType; |
| 225 return true; |
| 226 } |
| 227 virtual bool onDecodePixels(const SkImageInfo& info, |
| 228 void* pixels, |
| 229 size_t rowBytes) SK_OVERRIDE { |
| 230 return false; |
| 231 } |
| 232 private: |
| 233 int fX; // controls where the failure happens |
| 234 typedef SkCachingPixelRef INHERITED; |
| 235 }; |
| 236 |
| 237 DEF_TEST(CachingPixelRef, reporter) { |
| 238 SkBitmap lazy; |
| 239 // test the error handling |
| 240 REPORTER_ASSERT(reporter, !TestPixelRef::Install(&lazy, 0)); |
| 241 // onDecodeInfo should succeed, allowing installation |
| 242 REPORTER_ASSERT(reporter, TestPixelRef::Install(&lazy, 1)); |
| 243 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. |
| 244 // onDecodePixels should fail, so getting pixels will fail. |
| 245 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); |
| 246 } |
OLD | NEW |