Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(457)

Side by Side Diff: tests/CachedDecodingPixelRefTest.cpp

Issue 54203006: Break up SkLazyPixelRef functionally into class hierarchy. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "SkLazyPixelRef.h" 14 #include "SkLazyPixelRef.h"
15 #include "SkLazyCachingPixelRef.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
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
106 /** 110 /**
107 * This checks to see that a SkLazyPixelRef works as advertized. 111 * This checks to see that a SkLazyPixelRef works as advertized.
108 */ 112 */
109 #include "TestClassDef.h"
110 DEF_TEST(CachedDecodingPixelRefTest, reporter) { 113 DEF_TEST(CachedDecodingPixelRefTest, reporter) {
111 SkBitmap original; 114 SkBitmap original;
112 make_test_image(&original); 115 make_test_image(&original);
113 const size_t bitmapSize = original.getSize();
114 const size_t oldByteLimit = SkScaledImageCache::GetByteLimit();
115 REPORTER_ASSERT(reporter, (!(original.empty())) && (!(original.isNull()))); 116 REPORTER_ASSERT(reporter, (!(original.empty())) && (!(original.isNull())));
116 117
117 static const SkImageEncoder::Type types[] = { 118 static const SkImageEncoder::Type types[] = {
118 SkImageEncoder::kPNG_Type, 119 SkImageEncoder::kPNG_Type,
119 SkImageEncoder::kJPEG_Type, 120 SkImageEncoder::kJPEG_Type,
120 SkImageEncoder::kWEBP_Type 121 SkImageEncoder::kWEBP_Type
121 }; 122 };
122 123
123 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) { 124 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) {
124 SkImageEncoder::Type type = types[i]; 125 SkImageEncoder::Type type = types[i];
125 SkAutoDataUnref encoded(create_data_from_bitmap(original, type)); 126 SkAutoDataUnref encoded(create_data_from_bitmap(original, type));
126 REPORTER_ASSERT(reporter, encoded.get() != NULL); 127 REPORTER_ASSERT(reporter, encoded.get() != NULL);
127 if (NULL == encoded.get()) { 128 if (NULL == encoded.get()) {
128 continue; 129 continue;
129 } 130 }
130 SkBitmap lazy; 131 SkBitmap lazy;
131 static const SkBitmapFactory::DecodeProc decoder = 132 static const SkBitmapFactory::DecodeProc decoder =
132 &(SkImageDecoder::DecodeMemoryToTarget); 133 &(SkImageDecoder::DecodeMemoryToTarget);
133 bool success = simple_bitmap_factory(decoder, encoded.get(), &lazy); 134 bool success = simple_bitmap_factory(decoder, encoded.get(), &lazy);
134 135
135 REPORTER_ASSERT(reporter, success); 136 REPORTER_ASSERT(reporter, success);
136 137
137 size_t bytesUsed = SkScaledImageCache::GetBytesUsed(); 138 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
139 {
140 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
141 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
142 }
143 // pixels should be gone!
144 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
145 {
146 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
147 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
148 }
149
150 bool comparePixels = (SkImageEncoder::kPNG_Type == type);
151 // Only PNG is pixel-perfect.
152 compare_bitmaps(reporter, original, lazy, comparePixels);
153 }
154 }
155
156
157 /**
158 * This checks to see that a SkLazyPixelRef works as advertized.
scroggo 2013/10/31 21:53:06 advertised.
hal.canary 2013/11/01 03:29:24 Done.
159 */
160 DEF_TEST(LazyCachedPixelRefTest, reporter) {
scroggo 2013/10/31 21:53:06 Could these two tests share code?
hal.canary 2013/11/01 03:29:24 Done.
161 // By using a non-global cache, our unit tests won't collide with
162 // other unit tests. Unit tests can be run in separate threads.
163
164 // TODO(halcanary): Find a way to robustly test the global
165 // instance of SkScaledImageCache.
166 SkScaledImageCache cache(2000000, NULL);
167
168 SkBitmap original;
169 make_test_image(&original);
170 REPORTER_ASSERT(reporter, (!(original.empty())) && (!(original.isNull())));
171 const size_t bitmapSize = original.getSize();
172 const size_t oldByteLimit = cache.getByteLimit();
173
174 static const SkImageEncoder::Type types[] = {
175 SkImageEncoder::kPNG_Type,
176 SkImageEncoder::kJPEG_Type,
177 SkImageEncoder::kWEBP_Type
178 };
179
180 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) {
181 SkImageEncoder::Type type = types[i];
182 SkAutoDataUnref encoded(create_data_from_bitmap(original, type));
183 REPORTER_ASSERT(reporter, encoded.get() != NULL);
184 if (NULL == encoded.get()) {
185 continue;
186 }
187 SkBitmap lazy;
188 static const SkBitmapFactory::DecodeProc decoder =
189 &(SkImageDecoder::DecodeMemoryToTarget);
190
191 bool success = SkLazyCachingPixelRef::Install(decoder, encoded,
192 &lazy, &cache);
193 REPORTER_ASSERT(reporter, success);
194
195 size_t bytesUsed = cache.getBytesUsed();
138 196
139 if (oldByteLimit < bitmapSize) { 197 if (oldByteLimit < bitmapSize) {
140 SkScaledImageCache::SetByteLimit(bitmapSize + oldByteLimit); 198 cache.setByteLimit(bitmapSize + oldByteLimit);
141 } 199 }
142 void* lazyPixels = NULL; 200 void* lazyPixels = NULL;
143 201
144 // Since this is lazy, it shouldn't have fPixels yet! 202 // Since this is lazy, it shouldn't have fPixels yet!
145 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); 203 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
146 { 204 {
147 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. 205 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
148 lazyPixels = lazy.getPixels(); 206 lazyPixels = lazy.getPixels();
149 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); 207 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
150 // first time we lock pixels, we should get bump in the size 208 // first time we lock pixels, we should get bump in the size
151 // of the cache by exactly bitmapSize. 209 // of the cache by exactly bitmapSize.
152 REPORTER_ASSERT(reporter, bytesUsed + bitmapSize 210 REPORTER_ASSERT(reporter, bytesUsed + bitmapSize
153 == SkScaledImageCache::GetBytesUsed()); 211 == cache.getBytesUsed());
154 bytesUsed = SkScaledImageCache::GetBytesUsed(); 212 bytesUsed = cache.getBytesUsed();
155 } 213 }
156 // pixels should be gone! 214 // pixels should be gone!
157 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); 215 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
158 { 216 {
159 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. 217 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
160 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); 218 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
161 219
162 // verify that the same pixels are used this time. 220 // verify that the same pixels are used this time.
163 REPORTER_ASSERT(reporter, lazy.getPixels() == lazyPixels); 221 REPORTER_ASSERT(reporter, lazy.getPixels() == lazyPixels);
164 } 222 }
165 223
166 bool comparePixels = (SkImageEncoder::kPNG_Type == type); 224 bool comparePixels = (SkImageEncoder::kPNG_Type == type);
167 // Only PNG is pixel-perfect. 225 // Only PNG is pixel-perfect.
168 compare_bitmaps(reporter, original, lazy, comparePixels); 226 compare_bitmaps(reporter, original, lazy, comparePixels);
169 227
170 // force the cache to clear by making it too small. 228 // force the cache to clear by making it too small.
171 SkScaledImageCache::SetByteLimit(bitmapSize / 2); 229 cache.setByteLimit(bitmapSize / 2);
230
172 compare_bitmaps(reporter, original, lazy, comparePixels); 231 compare_bitmaps(reporter, original, lazy, comparePixels);
173 232
174 // I'm pretty sure that the logic of the cache should mean 233 // I'm pretty sure that the logic of the cache should mean
175 // that it will clear to zero, regardless of where it started. 234 // that it will clear to zero, regardless of where it started.
176 REPORTER_ASSERT(reporter, SkScaledImageCache::GetBytesUsed() == 0); 235 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
236 REPORTER_ASSERT(reporter, cache.getBytesUsed() == 0);
177 // TODO(someone) - write a custom allocator that can verify 237 // TODO(someone) - write a custom allocator that can verify
178 // that the memory where those pixels were cached really did 238 // that the memory where those pixels were cached really did
179 // get freed. 239 // get freed.
180 240
181 //////////////////////////////////////////////////////////////////////// 241 ////////////////////////////////////////////////////////////////////////
182 // The following commented-out code happens to work on my 242 // The following commented-out code happens to work on my
183 // machine, and indicates to me that the SkLazyPixelRef is 243 // machine, and indicates to me that the SkLazyPixelRef is
184 // behaving as designed. But I don't know an easy way to 244 // behaving as designed. But I don't know an easy way to
185 // guarantee that a second allocation of the same size will 245 // guarantee that a second allocation of the same size will
186 // give a different address. 246 // give a different address.
187 //////////////////////////////////////////////////////////////////////// 247 ////////////////////////////////////////////////////////////////////////
188 // { 248 // {
189 // // confuse the heap allocation system 249 // // confuse the heap allocation system
190 // SkAutoMalloc autoMalloc(bitmapSize); 250 // SkAutoMalloc autoMalloc(bitmapSize);
191 // REPORTER_ASSERT(reporter, autoMalloc.get() == lazyPixels); 251 // REPORTER_ASSERT(reporter, autoMalloc.get() == lazyPixels);
192 // { 252 // {
193 // SkAutoLockPixels autoLockPixels(lazy); 253 // SkAutoLockPixels autoLockPixels(lazy);
194 // // verify that *different* pixels are used this time. 254 // // verify that *different* pixels are used this time.
195 // REPORTER_ASSERT(reporter, lazy.getPixels() != lazyPixels); 255 // REPORTER_ASSERT(reporter, lazy.getPixels() != lazyPixels);
196 // compare_bitmaps(reporter, original, lazy, comparePixels); 256 // compare_bitmaps(reporter, original, lazy, comparePixels);
197 // } 257 // }
198 // } 258 // }
199 259
200 // restore cache size 260 // restore cache size
201 SkScaledImageCache::SetByteLimit(oldByteLimit); 261 cache.setByteLimit(oldByteLimit);
202 } 262 }
203 } 263 }
OLDNEW
« src/lazy/SkLazyCachingPixelRef.cpp ('K') | « src/lazy/SkLazyCachingPixelRef.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698