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

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: re-upload 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
« no previous file with comments | « 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 SkImage::Info info;
59 if (!proc(data->data(), data->size(), &info, NULL)) {
60 return false;
61 }
62 dst->setConfig(SkImageInfoToBitmapConfig(info), info.fWidth,
63 info.fHeight, 0, info.fAlphaType);
64 SkAutoTUnref<SkLazyPixelRef> ref(SkNEW_ARGS(SkLazyPixelRef,
65 (data, proc, NULL)));
66 dst->setPixelRef(ref);
67 return true;
68 }
69
70 static void compare_bitmaps(skiatest::Reporter* reporter,
71 const SkBitmap& b1, const SkBitmap& b2,
72 bool pixelPerfect = true) {
73 REPORTER_ASSERT(reporter, b1.empty() == b2.empty());
74 REPORTER_ASSERT(reporter, b1.width() == b2.width());
75 REPORTER_ASSERT(reporter, b1.height() == b2.height());
76 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
77 SkAutoLockPixels autoLockPixels1(b1);
78 SkAutoLockPixels autoLockPixels2(b2);
79 REPORTER_ASSERT(reporter, b1.isNull() == b2.isNull());
80 if (b1.isNull() || b1.empty()) {
81 return;
82 }
83 REPORTER_ASSERT(reporter, NULL != b1.getPixels());
84 REPORTER_ASSERT(reporter, NULL != b2.getPixels());
85 if ((!(b1.getPixels())) || (!(b2.getPixels()))) {
86 return;
87 }
88 if ((b1.width() != b2.width()) ||
89 (b1.height() != b2.height())) {
90 return;
91 }
92 if (!pixelPerfect) {
93 return;
94 }
95 int pixelErrors = 0;
96 for (int y = 0; y < b2.height(); ++y) {
97 for (int x = 0; x < b2.width(); ++x) {
98 if (b1.getColor(x, y) != b2.getColor(x, y)) {
99 ++pixelErrors;
100 }
101 }
102 }
103 REPORTER_ASSERT(reporter, 0 == pixelErrors);
104 }
105
106 /**
107 * This checks to see that a SkLazyPixelRef works as advertized.
108 */
109 #include "TestClassDef.h"
110 DEF_TEST(CachedDecodingPixelRefTest, reporter) {
111 SkBitmap original;
112 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
117 static const SkImageEncoder::Type types[] = {
118 SkImageEncoder::kPNG_Type,
119 SkImageEncoder::kJPEG_Type,
120 SkImageEncoder::kWEBP_Type
121 };
122
123 for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) {
124 SkImageEncoder::Type type = types[i];
125 SkAutoDataUnref encoded(create_data_from_bitmap(original, type));
126 REPORTER_ASSERT(reporter, encoded.get() != NULL);
127 if (NULL == encoded.get()) {
128 continue;
129 }
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 }
203 }
204
OLDNEW
« no previous file with comments | « src/lazy/SkLazyPixelRef.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698