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

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: more changes Created 7 years, 2 months 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, 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,
mtklein 2013/10/24 17:03:25 &
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.
mtklein 2013/10/24 17:03:25 Are we halfway through a name search-and-replace h
109 */
110 static void CachedDecodingPixelRef(skiatest::Reporter* reporter) {
mtklein 2013/10/24 17:03:25 You may find it somewhat less verbose to just writ
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;
mtklein 2013/10/24 17:03:25 *
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 - write a custom allocator that can verify that the
178 // memory where those pixels were cached really did get freed.
179
180 ////////////////////////////////////////////////////////////////////////
181 // The following commented-out code happens to work on my
182 // machine, and indicates to me that the SkLazyPixelRef is
183 // behaving as designed. But I don't know an easy way to
184 // guarantee that a second allocation of the same size will
185 // give a different address.
186 ////////////////////////////////////////////////////////////////////////
187 // {
188 // // confuse the heap allocation system
189 // SkAutoMalloc autoMalloc(bitmapSize);
190 // REPORTER_ASSERT(reporter, autoMalloc.get() == lazyPixels);
191 // {
192 // SkAutoLockPixels autoLockPixels(lazy);
193 // // verify that *different* pixels are used this time.
194 // REPORTER_ASSERT(reporter, lazy.getPixels() != lazyPixels);
195 // compare_bitmaps(reporter, original, lazy, comparePixels);
196 // }
197 // }
198
199 // restore cache size
200 SkScaledImageCache::SetByteLimit(oldByteLimit);
201 }
202 }
203
204 #include "TestClassDef.h"
205 DEFINE_TESTCLASS("CachedDecodingPixelRefTest",
206 CachedDecodingPixelRefTestClass,
207 CachedDecodingPixelRef)
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