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

Side by Side Diff: tests/CachedDecodingPixelRefTest.cpp

Issue 37343002: Allow SkLazyPixelRef to use SkScaledImageCache (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: lint 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
« src/lazy/SkLazyPixelRef.cpp ('K') | « src/lazy/SkLazyPixelRef.cpp ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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,
57 SkBitmap* dst,
58 SkBitmap::Allocator* allocator = NULL) {
59 SkImage::Info info;
60 if (!proc(data->data(), data->size(), &info, NULL)) {
61 return false;
62 }
63 dst->setConfig(SkImageInfoToBitmapConfig(info), info.fWidth,
64 info.fHeight, 0, info.fAlphaType);
65 SkAutoTUnref<SkLazyPixelRef> ref(SkNEW_ARGS(SkLazyPixelRef,
66 (data, proc, NULL)));
67 if (allocator) {
68 ref->setAllocator(allocator);
69 }
70 dst->setPixelRef(ref);
71 return true;
72 }
73
74 static void compare_bitmaps(skiatest::Reporter* reporter,
75 const SkBitmap& b1, const SkBitmap& b2,
76 bool pixelPerfect = true) {
77 REPORTER_ASSERT(reporter, b1.empty() == b2.empty());
78 REPORTER_ASSERT(reporter, b1.width() == b2.width());
79 REPORTER_ASSERT(reporter, b1.height() == b2.height());
80 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
81 SkAutoLockPixels autoLockPixels1(b1);
82 SkAutoLockPixels autoLockPixels2(b2);
83 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
84 if (b1.isNull() || b1.empty()) {
85 return;
86 }
87 REPORTER_ASSERT(reporter, NULL != b1.getPixels());
88 REPORTER_ASSERT(reporter, NULL != b2.getPixels());
89 if ((!(b1.getPixels())) || (!(b2.getPixels()))) {
90 return;
91 }
92 if ((b1.width() != b2.width()) ||
93 (b1.height() != b2.height())) {
94 return;
95 }
96 if (!pixelPerfect) {
97 return;
98 }
99 int pixelErrors = 0;
100 for (int y = 0; y < b2.height(); ++y) {
101 for (int x = 0; x < b2.width(); ++x) {
102 if (b1.getColor(x, y) != b2.getColor(x, y)) {
103 ++pixelErrors;
104 }
105 }
106 }
107 REPORTER_ASSERT(reporter, 0 == pixelErrors);
108 }
109
110 /**
111 * This checks to see that a SkLazyPixelRef works as advertized.
112 */
113 #include "TestClassDef.h"
114 DEF_TEST(CachedDecodingPixelRefTest, reporter) {
115 SkBitmap original;
116 make_test_image(&original);
117 const size_t bitmapSize = original.getSize();
118 const size_t oldByteLimit = SkScaledImageCache::GetByteLimit();
119 REPORTER_ASSERT(reporter, (!(original.empty())) && (!(original.isNull())));
120
121 static const SkImageEncoder::Type types[] = {
122 SkImageEncoder::kPNG_Type,
123 SkImageEncoder::kJPEG_Type,
124 SkImageEncoder::kWEBP_Type
125 };
126
127 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) {
128 SkImageEncoder::Type type = types[i];
129 SkAutoDataUnref encoded(create_data_from_bitmap(original, type));
130 REPORTER_ASSERT(reporter, encoded.get() != NULL);
131 if (NULL == encoded.get()) {
132 continue;
133 }
134 SkBitmap lazy;
135 static const SkBitmapFactory::DecodeProc decoder =
136 &(SkImageDecoder::DecodeMemoryToTarget);
137 bool success = simple_bitmap_factory(decoder, encoded.get(), &lazy);
138
139 REPORTER_ASSERT(reporter, success);
140
141 size_t bytesUsed = SkScaledImageCache::GetBytesUsed();
142
143 if (oldByteLimit < bitmapSize) {
144 SkScaledImageCache::SetByteLimit(bitmapSize + oldByteLimit);
145 }
146 void* lazyPixels = NULL;
147
148 // Since this is lazy, it shouldn't have fPixels yet!
149 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
150 {
151 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
152 lazyPixels = lazy.getPixels();
153 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
154 // first time we lock pixels, we should get bump in the size
155 // of the cache by exactly bitmapSize.
156 REPORTER_ASSERT(reporter, bytesUsed + bitmapSize
157 == SkScaledImageCache::GetBytesUsed());
158 bytesUsed = SkScaledImageCache::GetBytesUsed();
159 }
160 // pixels should be gone!
161 REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
162 {
163 SkAutoLockPixels autoLockPixels(lazy); // now pixels are good.
164 REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
165
166 // verify that the same pixels are used this time.
167 REPORTER_ASSERT(reporter, lazy.getPixels() == lazyPixels);
168 }
169
170 bool comparePixels = (SkImageEncoder::kPNG_Type == type);
171 // Only PNG is pixel-perfect.
172 compare_bitmaps(reporter, original, lazy, comparePixels);
173
174 // force the cache to clear by making it too small.
175 SkScaledImageCache::SetByteLimit(bitmapSize / 2);
176 compare_bitmaps(reporter, original, lazy, comparePixels);
177
178 // I'm pretty sure that the logic of the cache should mean
179 // that it will clear to zero, regardless of where it started.
180 REPORTER_ASSERT(reporter, SkScaledImageCache::GetBytesUsed() == 0);
181 // TODO(someone) - write a custom allocator that can verify
182 // that the memory where those pixels were cached really did
183 // get freed.
184
185 ////////////////////////////////////////////////////////////////////////
186 // The following commented-out code happens to work on my
187 // machine, and indicates to me that the SkLazyPixelRef is
188 // behaving as designed. But I don't know an easy way to
189 // guarantee that a second allocation of the same size will
190 // give a different address.
191 ////////////////////////////////////////////////////////////////////////
192 // {
193 // // confuse the heap allocation system
194 // SkAutoMalloc autoMalloc(bitmapSize);
195 // REPORTER_ASSERT(reporter, autoMalloc.get() == lazyPixels);
196 // {
197 // SkAutoLockPixels autoLockPixels(lazy);
198 // // verify that *different* pixels are used this time.
199 // REPORTER_ASSERT(reporter, lazy.getPixels() != lazyPixels);
200 // compare_bitmaps(reporter, original, lazy, comparePixels);
201 // }
202 // }
203
204 // restore cache size
205 SkScaledImageCache::SetByteLimit(oldByteLimit);
206 }
207 }
208
OLDNEW
« src/lazy/SkLazyPixelRef.cpp ('K') | « src/lazy/SkLazyPixelRef.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698