OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2013 Google Inc. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 #include "SkBitmap.h" | |
9 #include "SkCanvas.h" | |
10 #include "SkData.h" | |
11 #include "SkForceLinking.h" | |
12 #include "SkImageDecoder.h" | |
13 #include "SkImagePriv.h" | |
14 #include "SkLazyPixelRef.h" | |
15 #include "SkScaledImageCache.h" | |
16 #include "SkStream.h" | |
17 #include "Test.h" | |
18 | |
19 __SK_FORCE_IMAGE_DECODER_LINKING; | |
20 | |
21 /** | |
22 * Fill this bitmap with some color. | |
23 */ | |
24 static void make_test_image(SkBitmap* bm) { | |
25 static const int W = 50, H = 50; | |
26 static const SkBitmap::Config config = SkBitmap::kARGB_8888_Config; | |
27 bm->setConfig(config, W, H); | |
28 bm->allocPixels(); | |
29 bm->eraseColor(SK_ColorBLACK); | |
30 SkCanvas canvas(*bm); | |
31 SkPaint paint; | |
32 paint.setColor(SK_ColorBLUE); | |
33 canvas.drawRectCoords(0, 0, SkIntToScalar(W/2), | |
34 SkIntToScalar(H/2), paint); | |
35 paint.setColor(SK_ColorWHITE); | |
36 canvas.drawRectCoords(SkIntToScalar(W/2), SkIntToScalar(H/2), | |
37 SkIntToScalar(W), SkIntToScalar(H), paint); | |
38 } | |
39 | |
40 /** | |
41 * encode this bitmap into some data via SkImageEncoder | |
42 */ | |
43 static SkData* create_data_from_bitmap(const SkBitmap& bm, | |
44 SkImageEncoder::Type type) { | |
45 SkDynamicMemoryWStream stream; | |
46 if (SkImageEncoder::EncodeStream(&stream, bm, type, 100)) { | |
47 return stream.copyToData(); | |
48 } | |
49 return NULL; | |
50 } | |
51 | |
52 /** | |
53 * A simplified version of SkBitmapFactory | |
54 */ | |
55 static bool simple_bitmap_factory(SkBitmapFactory::DecodeProc proc, | |
56 SkData* data, SkBitmap* dst) { | |
57 SkImage::Info info; | |
58 if (!proc(data->data(), data->size(), &info, NULL)) { | |
59 return false; | |
60 } | |
61 SkBitmapFactory::Target target; | |
62 target.fRowBytes = SkImageMinRowBytes(info); | |
63 dst->setConfig(SkImageInfoToBitmapConfig(info), info.fWidth, | |
64 info.fHeight, target.fRowBytes, info.fAlphaType); | |
65 SkAutoTUnref<SkLazyPixelRef> ref(SkNEW_ARGS(SkLazyPixelRef, | |
66 (data, proc, NULL))); | |
67 dst->setPixelRef(ref); | |
68 return true; | |
69 } | |
70 | |
71 static void compare_bitmaps(skiatest::Reporter* reporter, | |
72 const SkBitmap & b1, const SkBitmap & b2, | |
73 bool pixelPerfect=true) { | |
74 REPORTER_ASSERT(reporter, b1.empty() == b2.empty()); | |
75 REPORTER_ASSERT(reporter, b1.width() == b2.width()); | |
76 REPORTER_ASSERT(reporter, b1.height() == b2.height()); | |
77 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull()); | |
78 SkAutoLockPixels autoLockPixels1(b1); | |
79 SkAutoLockPixels autoLockPixels2(b2); | |
80 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull()); | |
81 if (b1.isNull() || b1.empty()) { | |
82 return; | |
83 } | |
84 REPORTER_ASSERT(reporter, NULL != b1.getPixels()); | |
85 REPORTER_ASSERT(reporter, NULL != b2.getPixels()); | |
86 if ((!(b1.getPixels())) || (!(b2.getPixels()))) { | |
87 return; | |
88 } | |
89 if ((b1.width() != b2.width()) || | |
90 (b1.height() != b2.height())) { | |
91 return; | |
92 } | |
93 if (!pixelPerfect) { | |
94 return; | |
95 } | |
96 int pixelErrors = 0; | |
97 for (int y = 0; y < b2.height(); ++y) { | |
98 for (int x = 0; x < b2.width(); ++x) { | |
99 if (b1.getColor(x, y) != b2.getColor(x, y)) { | |
100 ++pixelErrors; | |
101 } | |
102 } | |
103 } | |
104 REPORTER_ASSERT(reporter, 0 == pixelErrors); | |
105 } | |
106 | |
107 /** | |
108 * This checks to see that a LazyPizelRef works as advertized. | |
109 */ | |
110 static void CachedDecodingPixelRef(skiatest::Reporter* reporter) { | |
111 SkBitmap original; | |
112 make_test_image(&original); | |
113 size_t bitmapSize = original.getSize(); | |
scroggo
2013/10/23 23:38:33
const
hal.canary
2013/10/24 14:21:55
Done.
| |
114 size_t oldByteLimit = SkScaledImageCache::GetByteLimit(); | |
scroggo
2013/10/23 23:38:33
Maybe this should be defaultByteLimit?
Also, it c
hal.canary
2013/10/24 14:21:55
Done.
| |
115 REPORTER_ASSERT(reporter, (!(original.empty())) && (!(original.isNull()))); | |
116 | |
117 static const SkImageEncoder::Type types[] = { | |
118 SkImageEncoder::kPNG_Type, | |
119 SkImageEncoder::kJPEG_Type, | |
120 SkImageEncoder::kWEBP_Type}; | |
scroggo
2013/10/23 23:38:33
nit: This would look cleaner as:
SkImageEncod
hal.canary
2013/10/24 14:21:55
Done.
| |
121 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) { | |
122 SkImageEncoder::Type type = types[i]; | |
123 SkAutoDataUnref encoded(create_data_from_bitmap(original, type)); | |
124 REPORTER_ASSERT(reporter, encoded.get() != NULL); | |
125 if (NULL == encoded.get()) { | |
126 continue; | |
127 } | |
128 SkBitmap lazy; | |
129 static const SkBitmapFactory::DecodeProc decoder = | |
130 &(SkImageDecoder::DecodeMemoryToTarget); | |
131 bool success = simple_bitmap_factory(decoder, encoded.get(), &lazy); | |
132 REPORTER_ASSERT(reporter, success); | |
133 | |
134 // Since this is lazy, it shouldn't have fPixels yet! | |
scroggo
2013/10/23 23:38:33
This comment seems to go with the REPORTER_ASSERT
hal.canary
2013/10/24 14:21:55
Done.
| |
135 size_t bytes = SkScaledImageCache::GetBytesUsed(); | |
scroggo
2013/10/23 23:38:33
Could you rename bytes to bytesUsed?
hal.canary
2013/10/24 14:21:55
Done.
| |
136 | |
137 if (oldByteLimit < bitmapSize) { | |
138 SkScaledImageCache::SetByteLimit(bitmapSize + oldByteLimit); | |
139 } | |
140 void * ptr = NULL; | |
scroggo
2013/10/23 23:38:33
Better name for ptr? lazyPixels?
hal.canary
2013/10/24 14:21:55
Done.
| |
141 REPORTER_ASSERT(reporter, NULL == lazy.getPixels()); | |
142 { | |
143 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good. | |
144 ptr = lazy.getPixels(); | |
145 REPORTER_ASSERT(reporter, NULL != lazy.getPixels()); | |
146 // first time we lock pixels, we should get bump in the size | |
147 // of the cache by exactly bitmapSize. | |
148 REPORTER_ASSERT(reporter, bytes + bitmapSize | |
149 == SkScaledImageCache::GetBytesUsed()); | |
150 bytes = SkScaledImageCache::GetBytesUsed(); | |
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 | |
158 // verify that the same pixels are used this time. | |
159 REPORTER_ASSERT(reporter, lazy.getPixels() == ptr); | |
160 } | |
161 | |
162 bool comparePixels = (SkImageEncoder::kPNG_Type == type); | |
163 // Only PNG is pixel-perfect. | |
164 compare_bitmaps(reporter, original, lazy, comparePixels); | |
165 | |
166 // force the cache to clear by making it too small. | |
167 SkScaledImageCache::SetByteLimit(bitmapSize / 2); | |
168 compare_bitmaps(reporter, original, lazy, comparePixels); | |
169 | |
170 // I'm pretty sure that the logic of the cache should mean | |
171 // that it will clear to zero, regardless of where it started. | |
172 REPORTER_ASSERT(reporter, SkScaledImageCache::GetBytesUsed() == 0); | |
173 // TODO - write a custom allocator that can verify that the | |
174 // memory where those pixels were cached really did get freed. | |
175 | |
176 //////////////////////////////////////////////////////////////////////// | |
177 // The following commented-out code happens to work on my | |
178 // machine, and indicates to me that the SkLazyPixelRef is | |
179 // behaving as designed. But I don't know an easy way to | |
180 // guarantee that a second allocation of the same size will | |
181 // give a different address. | |
182 //////////////////////////////////////////////////////////////////////// | |
183 // { | |
184 // // confuse the heap allocation system | |
185 // SkAutoMalloc autoMalloc(bitmapSize); | |
186 // REPORTER_ASSERT(reporter, autoMalloc.get() == ptr); | |
187 // { | |
188 // SkAutoLockPixels autoLockPixels(lazy); | |
189 // // verify that DIFFERENT pixels are used this time. | |
190 // REPORTER_ASSERT(reporter, lazy.getPixels() != ptr); | |
191 // compare_bitmaps(reporter, original, lazy, comparePixels); | |
192 // } | |
193 // } | |
194 | |
195 // restore cache size | |
196 SkScaledImageCache::SetByteLimit(oldByteLimit); | |
197 } | |
198 } | |
199 | |
200 #include "TestClassDef.h" | |
201 DEFINE_TESTCLASS("CachedDecodingPixelRefTest", | |
202 CachedDecodingPixelRefTestClass, | |
203 CachedDecodingPixelRef) | |
OLD | NEW |