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

Side by Side Diff: tests/ResourceCacheTest.cpp

Issue 608883003: GrResourceCache2 manages scratch texture. (Closed) Base URL: https://skia.googlesource.com/skia.git@surfimpl
Patch Set: remove todo Created 6 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
« no previous file with comments | « src/image/SkSurface_Gpu.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
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 #if SK_SUPPORT_GPU 8 #if SK_SUPPORT_GPU
9 9
10 #include "GrContext.h"
11 #include "GrContextFactory.h"
12 #include "GrGpu.h"
13 #include "GrResourceCache.h"
10 #include "SkCanvas.h" 14 #include "SkCanvas.h"
11 #include "GrContextFactory.h"
12 #include "GrResourceCache.h"
13 #include "SkSurface.h" 15 #include "SkSurface.h"
14 #include "Test.h" 16 #include "Test.h"
15 17
16 static const int gWidth = 640; 18 static const int gWidth = 640;
17 static const int gHeight = 480; 19 static const int gHeight = 480;
18 20
19 //////////////////////////////////////////////////////////////////////////////// 21 ////////////////////////////////////////////////////////////////////////////////
20 static void test_cache(skiatest::Reporter* reporter, 22 static void test_cache(skiatest::Reporter* reporter,
21 GrContext* context, 23 GrContext* context,
22 SkCanvas* canvas) { 24 SkCanvas* canvas) {
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 int TestResource::fAlive = 0; 110 int TestResource::fAlive = 0;
109 111
110 static void test_purge_invalidated(skiatest::Reporter* reporter, GrContext* cont ext) { 112 static void test_purge_invalidated(skiatest::Reporter* reporter, GrContext* cont ext) {
111 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); 113 GrCacheID::Domain domain = GrCacheID::GenerateDomain();
112 GrCacheID::Key keyData; 114 GrCacheID::Key keyData;
113 keyData.fData64[0] = 5; 115 keyData.fData64[0] = 5;
114 keyData.fData64[1] = 18; 116 keyData.fData64[1] = 18;
115 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); 117 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
116 GrResourceKey key(GrCacheID(domain, keyData), t, 0); 118 GrResourceKey key(GrCacheID(domain, keyData), t, 0);
117 119
118 GrResourceCache cache(5, 30000); 120 GrResourceCache cache(context->getGpu()->caps(), 5, 30000);
119 121
120 // Add two resources with the same key that delete each other from the cache when destroyed. 122 // Add two resources with the same key that delete each other from the cache when destroyed.
121 TestResource* a = new TestResource(context->getGpu()); 123 TestResource* a = new TestResource(context->getGpu());
122 TestResource* b = new TestResource(context->getGpu()); 124 TestResource* b = new TestResource(context->getGpu());
123 cache.addResource(key, a); 125 cache.addResource(key, a);
124 cache.addResource(key, b); 126 cache.addResource(key, b);
125 // Circle back. 127 // Circle back.
126 a->setDeleteWhenDestroyed(&cache, b); 128 a->setDeleteWhenDestroyed(&cache, b);
127 b->setDeleteWhenDestroyed(&cache, a); 129 b->setDeleteWhenDestroyed(&cache, a);
128 a->unref(); 130 a->unref();
(...skipping 17 matching lines...) Expand all
146 GrCacheID::Domain domain = GrCacheID::GenerateDomain(); 148 GrCacheID::Domain domain = GrCacheID::GenerateDomain();
147 GrCacheID::Key keyData; 149 GrCacheID::Key keyData;
148 keyData.fData64[0] = 5; 150 keyData.fData64[0] = 5;
149 keyData.fData64[1] = 0; 151 keyData.fData64[1] = 0;
150 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType(); 152 GrResourceKey::ResourceType t = GrResourceKey::GenerateResourceType();
151 153
152 GrResourceKey key(GrCacheID(domain, keyData), t, 0); 154 GrResourceKey key(GrCacheID(domain, keyData), t, 0);
153 155
154 { 156 {
155 { 157 {
156 GrResourceCache cache(3, 30000); 158 GrResourceCache cache(context->getGpu()->caps(), 3, 30000);
157 TestResource* a = new TestResource(context->getGpu()); 159 TestResource* a = new TestResource(context->getGpu());
158 TestResource* b = new TestResource(context->getGpu()); 160 TestResource* b = new TestResource(context->getGpu());
159 cache.addResource(key, a); 161 cache.addResource(key, a);
160 cache.addResource(key, b); 162 cache.addResource(key, b);
161 163
162 a->setDeleteWhenDestroyed(&cache, b); 164 a->setDeleteWhenDestroyed(&cache, b);
163 b->setDeleteWhenDestroyed(&cache, a); 165 b->setDeleteWhenDestroyed(&cache, a);
164 166
165 a->unref(); 167 a->unref();
166 b->unref(); 168 b->unref();
167 REPORTER_ASSERT(reporter, 2 == TestResource::alive()); 169 REPORTER_ASSERT(reporter, 2 == TestResource::alive());
168 } 170 }
169 REPORTER_ASSERT(reporter, 0 == TestResource::alive()); 171 REPORTER_ASSERT(reporter, 0 == TestResource::alive());
170 } 172 }
171 { 173 {
172 GrResourceCache cache(3, 30000); 174 GrResourceCache cache(context->getGpu()->caps(), 3, 30000);
173 TestResource* a = new TestResource(context->getGpu()); 175 TestResource* a = new TestResource(context->getGpu());
174 TestResource* b = new TestResource(context->getGpu()); 176 TestResource* b = new TestResource(context->getGpu());
175 cache.addResource(key, a); 177 cache.addResource(key, a);
176 cache.addResource(key, b); 178 cache.addResource(key, b);
177 179
178 a->setDeleteWhenDestroyed(&cache, b); 180 a->setDeleteWhenDestroyed(&cache, b);
179 b->setDeleteWhenDestroyed(&cache, a); 181 b->setDeleteWhenDestroyed(&cache, a);
180 182
181 a->unref(); 183 a->unref();
182 b->unref(); 184 b->unref();
(...skipping 14 matching lines...) Expand all
197 key1Data.fData64[1] = 0; 199 key1Data.fData64[1] = 0;
198 GrResourceKey key1(GrCacheID(domain, key1Data), t, 0); 200 GrResourceKey key1(GrCacheID(domain, key1Data), t, 0);
199 201
200 GrCacheID::Key key2Data; 202 GrCacheID::Key key2Data;
201 key2Data.fData64[0] = 1; 203 key2Data.fData64[0] = 1;
202 key2Data.fData64[1] = 0; 204 key2Data.fData64[1] = 0;
203 GrResourceKey key2(GrCacheID(domain, key2Data), t, 0); 205 GrResourceKey key2(GrCacheID(domain, key2Data), t, 0);
204 206
205 // Test changing resources sizes (both increase & decrease). 207 // Test changing resources sizes (both increase & decrease).
206 { 208 {
207 GrResourceCache cache(2, 300); 209 GrResourceCache cache(context->getGpu()->caps(), 2, 300);
208 210
209 TestResource* a = new TestResource(context->getGpu()); 211 TestResource* a = new TestResource(context->getGpu());
210 a->setSize(100); // Test didChangeGpuMemorySize() when not in the cache. 212 a->setSize(100); // Test didChangeGpuMemorySize() when not in the cache.
211 cache.addResource(key1, a); 213 cache.addResource(key1, a);
212 a->unref(); 214 a->unref();
213 215
214 TestResource* b = new TestResource(context->getGpu()); 216 TestResource* b = new TestResource(context->getGpu());
215 b->setSize(100); 217 b->setSize(100);
216 cache.addResource(key2, b); 218 cache.addResource(key2, b);
217 b->unref(); 219 b->unref();
218 220
219 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes()); 221 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes());
220 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); 222 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
221 223
222 static_cast<TestResource*>(cache.find(key2))->setSize(200); 224 static_cast<TestResource*>(cache.find(key2))->setSize(200);
223 static_cast<TestResource*>(cache.find(key1))->setSize(50); 225 static_cast<TestResource*>(cache.find(key1))->setSize(50);
224 226
225 REPORTER_ASSERT(reporter, 250 == cache.getCachedResourceBytes()); 227 REPORTER_ASSERT(reporter, 250 == cache.getCachedResourceBytes());
226 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); 228 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
227 } 229 }
228 230
229 // Test increasing a resources size beyond the cache budget. 231 // Test increasing a resources size beyond the cache budget.
230 { 232 {
231 GrResourceCache cache(2, 300); 233 GrResourceCache cache(context->getGpu()->caps(), 2, 300);
232 234
233 TestResource* a = new TestResource(context->getGpu(), 100); 235 TestResource* a = new TestResource(context->getGpu(), 100);
234 cache.addResource(key1, a); 236 cache.addResource(key1, a);
235 a->unref(); 237 a->unref();
236 238
237 TestResource* b = new TestResource(context->getGpu(), 100); 239 TestResource* b = new TestResource(context->getGpu(), 100);
238 cache.addResource(key2, b); 240 cache.addResource(key2, b);
239 b->unref(); 241 b->unref();
240 242
241 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes()); 243 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes());
242 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount()); 244 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
243 245
244 static_cast<TestResource*>(cache.find(key2))->setSize(201); 246 static_cast<TestResource*>(cache.find(key2))->setSize(201);
245 REPORTER_ASSERT(reporter, NULL == cache.find(key1)); 247 REPORTER_ASSERT(reporter, NULL == cache.find(key1));
246 248
247 REPORTER_ASSERT(reporter, 201 == cache.getCachedResourceBytes()); 249 REPORTER_ASSERT(reporter, 201 == cache.getCachedResourceBytes());
248 REPORTER_ASSERT(reporter, 1 == cache.getCachedResourceCount()); 250 REPORTER_ASSERT(reporter, 1 == cache.getCachedResourceCount());
249 } 251 }
250
251 // Test changing the size of an exclusively-held resource.
252 {
253 GrResourceCache cache(2, 300);
254
255 TestResource* a = new TestResource(context->getGpu(), 100);
256 cache.addResource(key1, a);
257 cache.makeExclusive(a->getCacheEntry());
258
259 TestResource* b = new TestResource(context->getGpu(), 100);
260 cache.addResource(key2, b);
261 b->unref();
262
263 REPORTER_ASSERT(reporter, 200 == cache.getCachedResourceBytes());
264 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
265 REPORTER_ASSERT(reporter, NULL == cache.find(key1));
266
267 a->setSize(200);
268
269 REPORTER_ASSERT(reporter, 300 == cache.getCachedResourceBytes());
270 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
271 // Internal resource cache validation will test the detached size (debug mode only).
272
273 cache.makeNonExclusive(a->getCacheEntry());
274 a->unref();
275
276 REPORTER_ASSERT(reporter, 300 == cache.getCachedResourceBytes());
277 REPORTER_ASSERT(reporter, 2 == cache.getCachedResourceCount());
278 REPORTER_ASSERT(reporter, cache.find(key1));
279 // Internal resource cache validation will test the detached size (debug mode only).
280 }
281 } 252 }
282 253
283 //////////////////////////////////////////////////////////////////////////////// 254 ////////////////////////////////////////////////////////////////////////////////
284 DEF_GPUTEST(ResourceCache, reporter, factory) { 255 DEF_GPUTEST(ResourceCache, reporter, factory) {
285 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { 256 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) {
286 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G LContextType>(type); 257 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G LContextType>(type);
287 if (!GrContextFactory::IsRenderingGLContext(glType)) { 258 if (!GrContextFactory::IsRenderingGLContext(glType)) {
288 continue; 259 continue;
289 } 260 }
290 GrContext* context = factory->get(glType); 261 GrContext* context = factory->get(glType);
(...skipping 10 matching lines...) Expand all
301 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, info )); 272 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, info ));
302 273
303 test_cache(reporter, context, surface->getCanvas()); 274 test_cache(reporter, context, surface->getCanvas());
304 test_purge_invalidated(reporter, context); 275 test_purge_invalidated(reporter, context);
305 test_cache_delete_on_destruction(reporter, context); 276 test_cache_delete_on_destruction(reporter, context);
306 test_resource_size_changed(reporter, context); 277 test_resource_size_changed(reporter, context);
307 } 278 }
308 } 279 }
309 280
310 #endif 281 #endif
OLDNEW
« no previous file with comments | « src/image/SkSurface_Gpu.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698