OLD | NEW |
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" | 10 #include "GrContext.h" |
11 #include "GrContextFactory.h" | 11 #include "GrContextFactory.h" |
12 #include "GrGpu.h" | 12 #include "GrGpu.h" |
13 #include "GrResourceCache2.h" | 13 #include "GrResourceCache.h" |
14 #include "SkCanvas.h" | 14 #include "SkCanvas.h" |
15 #include "SkGr.h" | 15 #include "SkGr.h" |
16 #include "SkMessageBus.h" | 16 #include "SkMessageBus.h" |
17 #include "SkSurface.h" | 17 #include "SkSurface.h" |
18 #include "Test.h" | 18 #include "Test.h" |
19 | 19 |
20 static const int gWidth = 640; | 20 static const int gWidth = 640; |
21 static const int gHeight = 480; | 21 static const int gHeight = 480; |
22 | 22 |
23 //////////////////////////////////////////////////////////////////////////////// | 23 //////////////////////////////////////////////////////////////////////////////// |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 typedef GrGpuResource INHERITED; | 153 typedef GrGpuResource INHERITED; |
154 }; | 154 }; |
155 int TestResource::fNumAlive = 0; | 155 int TestResource::fNumAlive = 0; |
156 | 156 |
157 class Mock { | 157 class Mock { |
158 public: | 158 public: |
159 Mock(int maxCnt, size_t maxBytes) { | 159 Mock(int maxCnt, size_t maxBytes) { |
160 fContext.reset(GrContext::CreateMockContext()); | 160 fContext.reset(GrContext::CreateMockContext()); |
161 SkASSERT(fContext); | 161 SkASSERT(fContext); |
162 fContext->setResourceCacheLimits(maxCnt, maxBytes); | 162 fContext->setResourceCacheLimits(maxCnt, maxBytes); |
163 GrResourceCache2* cache2 = fContext->getResourceCache2(); | 163 GrResourceCache* cache = fContext->getResourceCache(); |
164 cache2->purgeAllUnlocked(); | 164 cache->purgeAllUnlocked(); |
165 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceByte
s()); | 165 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes(
)); |
166 } | 166 } |
167 | 167 |
168 GrResourceCache2* cache() { return fContext->getResourceCache2(); } | 168 GrResourceCache* cache() { return fContext->getResourceCache(); } |
169 | 169 |
170 GrContext* context() { return fContext; } | 170 GrContext* context() { return fContext; } |
171 | 171 |
172 private: | 172 private: |
173 SkAutoTUnref<GrContext> fContext; | 173 SkAutoTUnref<GrContext> fContext; |
174 }; | 174 }; |
175 | 175 |
176 static void test_no_key(skiatest::Reporter* reporter) { | 176 static void test_no_key(skiatest::Reporter* reporter) { |
177 Mock mock(10, 30000); | 177 Mock mock(10, 30000); |
178 GrContext* context = mock.context(); | 178 GrContext* context = mock.context(); |
179 GrResourceCache2* cache2 = mock.cache(); | 179 GrResourceCache* cache = mock.cache(); |
180 | 180 |
181 // Create a bunch of resources with no keys | 181 // Create a bunch of resources with no keys |
182 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 182 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
183 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 183 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
184 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu())); | 184 TestResource* c = SkNEW_ARGS(TestResource, (context->getGpu())); |
185 TestResource* d = SkNEW_ARGS(TestResource, (context->getGpu())); | 185 TestResource* d = SkNEW_ARGS(TestResource, (context->getGpu())); |
186 a->setSize(11); | 186 a->setSize(11); |
187 b->setSize(12); | 187 b->setSize(12); |
188 c->setSize(13); | 188 c->setSize(13); |
189 d->setSize(14); | 189 d->setSize(14); |
190 | 190 |
191 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); | 191 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); |
192 REPORTER_ASSERT(reporter, 4 == cache2->getResourceCount()); | 192 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
193 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMe
morySize() + | 193 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() + c->gpuMe
morySize() + |
194 d->gpuMemorySize() == cache2->getResourceBytes()); | 194 d->gpuMemorySize() == cache->getResourceBytes()); |
195 | 195 |
196 // Should be safe to purge without deleting the resources since we still hav
e refs. | 196 // Should be safe to purge without deleting the resources since we still hav
e refs. |
197 cache2->purgeAllUnlocked(); | 197 cache->purgeAllUnlocked(); |
198 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); | 198 REPORTER_ASSERT(reporter, 4 == TestResource::NumAlive()); |
199 | 199 |
200 // Since the resources have neither content nor scratch keys, delete immedia
tely upon unref. | 200 // Since the resources have neither content nor scratch keys, delete immedia
tely upon unref. |
201 | 201 |
202 a->unref(); | 202 a->unref(); |
203 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); | 203 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); |
204 REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); | 204 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
205 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMe
morySize() == | 205 REPORTER_ASSERT(reporter, b->gpuMemorySize() + c->gpuMemorySize() + d->gpuMe
morySize() == |
206 cache2->getResourceBytes()); | 206 cache->getResourceBytes()); |
207 | 207 |
208 c->unref(); | 208 c->unref(); |
209 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 209 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
210 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 210 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
211 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() == | 211 REPORTER_ASSERT(reporter, b->gpuMemorySize() + d->gpuMemorySize() == |
212 cache2->getResourceBytes()); | 212 cache->getResourceBytes()); |
213 | 213 |
214 d->unref(); | 214 d->unref(); |
215 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | 215 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
216 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 216 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
217 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache2->getResourceBytes()); | 217 REPORTER_ASSERT(reporter, b->gpuMemorySize() == cache->getResourceBytes()); |
218 | 218 |
219 b->unref(); | 219 b->unref(); |
220 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 220 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
221 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 221 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
222 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); | 222 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
223 } | 223 } |
224 | 224 |
225 // Each integer passed as a template param creates a new domain. | 225 // Each integer passed as a template param creates a new domain. |
226 template <int> static void make_content_key(GrContentKey* key, int data) { | 226 template <int> static void make_content_key(GrContentKey* key, int data) { |
227 static GrContentKey::Domain d = GrContentKey::GenerateDomain(); | 227 static GrContentKey::Domain d = GrContentKey::GenerateDomain(); |
228 GrContentKey::Builder builder(key, d, 1); | 228 GrContentKey::Builder builder(key, d, 1); |
229 builder[0] = data; | 229 builder[0] = data; |
230 } | 230 } |
231 | 231 |
232 static void test_budgeting(skiatest::Reporter* reporter) { | 232 static void test_budgeting(skiatest::Reporter* reporter) { |
233 Mock mock(10, 300); | 233 Mock mock(10, 300); |
234 GrContext* context = mock.context(); | 234 GrContext* context = mock.context(); |
235 GrResourceCache2* cache2 = mock.cache(); | 235 GrResourceCache* cache = mock.cache(); |
236 | 236 |
237 GrContentKey contentKey; | 237 GrContentKey contentKey; |
238 make_content_key<0>(&contentKey, 0); | 238 make_content_key<0>(&contentKey, 0); |
239 | 239 |
240 // Create a scratch, a content, and a wrapped resource | 240 // Create a scratch, a content, and a wrapped resource |
241 TestResource* scratch = | 241 TestResource* scratch = |
242 TestResource::CreateScratch(context->getGpu(), TestResource::kB_Simu
latedProperty); | 242 TestResource::CreateScratch(context->getGpu(), TestResource::kB_Simu
latedProperty); |
243 scratch->setSize(10); | 243 scratch->setSize(10); |
244 TestResource* content = SkNEW_ARGS(TestResource, (context->getGpu())); | 244 TestResource* content = SkNEW_ARGS(TestResource, (context->getGpu())); |
245 content->setSize(11); | 245 content->setSize(11); |
246 REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); | 246 REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); |
247 TestResource* wrapped = SkNEW_ARGS(TestResource, | 247 TestResource* wrapped = SkNEW_ARGS(TestResource, |
248 (context->getGpu(), GrGpuResource::kWrapp
ed_LifeCycle)); | 248 (context->getGpu(), GrGpuResource::kWrapp
ed_LifeCycle)); |
249 wrapped->setSize(12); | 249 wrapped->setSize(12); |
250 TestResource* unbudgeted = SkNEW_ARGS(TestResource, | 250 TestResource* unbudgeted = SkNEW_ARGS(TestResource, |
251 (context->getGpu(), GrGpuResource::kUn
cached_LifeCycle)); | 251 (context->getGpu(), GrGpuResource::kUn
cached_LifeCycle)); |
252 unbudgeted->setSize(13); | 252 unbudgeted->setSize(13); |
253 | 253 |
254 // Make sure we can't add a content key to the wrapped resource | 254 // Make sure we can't add a content key to the wrapped resource |
255 GrContentKey contentKey2; | 255 GrContentKey contentKey2; |
256 make_content_key<0>(&contentKey2, 1); | 256 make_content_key<0>(&contentKey2, 1); |
257 REPORTER_ASSERT(reporter, !wrapped->cacheAccess().setContentKey(contentKey2)
); | 257 REPORTER_ASSERT(reporter, !wrapped->cacheAccess().setContentKey(contentKey2)
); |
258 REPORTER_ASSERT(reporter, NULL == cache2->findAndRefContentResource(contentK
ey2)); | 258 REPORTER_ASSERT(reporter, NULL == cache->findAndRefContentResource(contentKe
y2)); |
259 | 259 |
260 // Make sure sizes are as we expect | 260 // Make sure sizes are as we expect |
261 REPORTER_ASSERT(reporter, 4 == cache2->getResourceCount()); | 261 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
262 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) + | 262 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) + |
263 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySi
ze() == | 263 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySi
ze() == |
264 cache2->getResourceBytes()); | 264 cache->getResourceBytes()); |
265 REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); | 265 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
266 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) == | 266 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) == |
267 cache2->getBudgetedResourceBytes()); | 267 cache->getBudgetedResourceBytes()); |
268 | 268 |
269 // Our refs mean that the resources are non purgeable. | 269 // Our refs mean that the resources are non purgeable. |
270 cache2->purgeAllUnlocked(); | 270 cache->purgeAllUnlocked(); |
271 REPORTER_ASSERT(reporter, 4 == cache2->getResourceCount()); | 271 REPORTER_ASSERT(reporter, 4 == cache->getResourceCount()); |
272 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) + | 272 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) + |
273 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySi
ze() == | 273 wrapped->gpuMemorySize() + unbudgeted->gpuMemorySi
ze() == |
274 cache2->getResourceBytes()); | 274 cache->getResourceBytes()); |
275 REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); | 275 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
276 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) == | 276 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) == |
277 cache2->getBudgetedResourceBytes()); | 277 cache->getBudgetedResourceBytes()); |
278 | 278 |
279 // Unreffing the wrapped resource should free it right away. | 279 // Unreffing the wrapped resource should free it right away. |
280 wrapped->unref(); | 280 wrapped->unref(); |
281 REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); | 281 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
282 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) + | 282 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + content->gpuMemorySize(
) + |
283 unbudgeted->gpuMemorySize() == cache2->getResource
Bytes()); | 283 unbudgeted->gpuMemorySize() == cache->getResourceB
ytes()); |
284 | 284 |
285 // Now try freeing the budgeted resources first | 285 // Now try freeing the budgeted resources first |
286 wrapped = SkNEW_ARGS(TestResource, (context->getGpu(), GrGpuResource::kWrapp
ed_LifeCycle)); | 286 wrapped = SkNEW_ARGS(TestResource, (context->getGpu(), GrGpuResource::kWrapp
ed_LifeCycle)); |
287 scratch->setSize(12); | 287 scratch->setSize(12); |
288 content->unref(); | 288 content->unref(); |
289 cache2->purgeAllUnlocked(); | 289 cache->purgeAllUnlocked(); |
290 REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); | 290 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
291 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize(
) + | 291 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() + wrapped->gpuMemorySize(
) + |
292 unbudgeted->gpuMemorySize() == cache2->getResource
Bytes()); | 292 unbudgeted->gpuMemorySize() == cache->getResourceB
ytes()); |
293 REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); | 293 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
294 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache2->getBudgetedRes
ourceBytes()); | 294 REPORTER_ASSERT(reporter, scratch->gpuMemorySize() == cache->getBudgetedReso
urceBytes()); |
295 | 295 |
296 scratch->unref(); | 296 scratch->unref(); |
297 cache2->purgeAllUnlocked(); | 297 cache->purgeAllUnlocked(); |
298 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 298 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
299 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySi
ze() == | 299 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() + wrapped->gpuMemorySi
ze() == |
300 cache2->getResourceBytes()); | 300 cache->getResourceBytes()); |
301 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); | 301 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
302 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); | 302 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
303 | 303 |
304 wrapped->unref(); | 304 wrapped->unref(); |
305 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 305 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
306 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache2->getResource
Bytes()); | 306 REPORTER_ASSERT(reporter, unbudgeted->gpuMemorySize() == cache->getResourceB
ytes()); |
307 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); | 307 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
308 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); | 308 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
309 | 309 |
310 unbudgeted->unref(); | 310 unbudgeted->unref(); |
311 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 311 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
312 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); | 312 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
313 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); | 313 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
314 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); | 314 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
315 } | 315 } |
316 | 316 |
317 static void test_unbudgeted(skiatest::Reporter* reporter) { | 317 static void test_unbudgeted(skiatest::Reporter* reporter) { |
318 Mock mock(10, 30000); | 318 Mock mock(10, 30000); |
319 GrContext* context = mock.context(); | 319 GrContext* context = mock.context(); |
320 GrResourceCache2* cache2 = mock.cache(); | 320 GrResourceCache* cache = mock.cache(); |
321 | 321 |
322 GrContentKey contentKey; | 322 GrContentKey contentKey; |
323 make_content_key<0>(&contentKey, 0); | 323 make_content_key<0>(&contentKey, 0); |
324 | 324 |
325 TestResource* scratch; | 325 TestResource* scratch; |
326 TestResource* content; | 326 TestResource* content; |
327 TestResource* wrapped; | 327 TestResource* wrapped; |
328 TestResource* unbudgeted; | 328 TestResource* unbudgeted; |
329 | 329 |
330 // A large uncached or wrapped resource shouldn't evict anything. | 330 // A large uncached or wrapped resource shouldn't evict anything. |
331 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_Si
mulatedProperty); | 331 scratch = TestResource::CreateScratch(context->getGpu(), TestResource::kB_Si
mulatedProperty); |
332 scratch->setSize(10); | 332 scratch->setSize(10); |
333 scratch->unref(); | 333 scratch->unref(); |
334 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 334 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
335 REPORTER_ASSERT(reporter, 10 == cache2->getResourceBytes()); | 335 REPORTER_ASSERT(reporter, 10 == cache->getResourceBytes()); |
336 REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); | 336 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
337 REPORTER_ASSERT(reporter, 10 == cache2->getBudgetedResourceBytes()); | 337 REPORTER_ASSERT(reporter, 10 == cache->getBudgetedResourceBytes()); |
338 | 338 |
339 content = SkNEW_ARGS(TestResource, (context->getGpu())); | 339 content = SkNEW_ARGS(TestResource, (context->getGpu())); |
340 content->setSize(11); | 340 content->setSize(11); |
341 REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); | 341 REPORTER_ASSERT(reporter, content->cacheAccess().setContentKey(contentKey)); |
342 content->unref(); | 342 content->unref(); |
343 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 343 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
344 REPORTER_ASSERT(reporter, 21 == cache2->getResourceBytes()); | 344 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); |
345 REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); | 345 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
346 REPORTER_ASSERT(reporter, 21 == cache2->getBudgetedResourceBytes()); | 346 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
347 | 347 |
348 size_t large = 2 * cache2->getResourceBytes(); | 348 size_t large = 2 * cache->getResourceBytes(); |
349 unbudgeted = SkNEW_ARGS(TestResource, | 349 unbudgeted = SkNEW_ARGS(TestResource, |
350 (context->getGpu(), large, GrGpuResource::kUncached_
LifeCycle)); | 350 (context->getGpu(), large, GrGpuResource::kUncached_
LifeCycle)); |
351 REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); | 351 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
352 REPORTER_ASSERT(reporter, 21 + large == cache2->getResourceBytes()); | 352 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes()); |
353 REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); | 353 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
354 REPORTER_ASSERT(reporter, 21 == cache2->getBudgetedResourceBytes()); | 354 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
355 | 355 |
356 unbudgeted->unref(); | 356 unbudgeted->unref(); |
357 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 357 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
358 REPORTER_ASSERT(reporter, 21 == cache2->getResourceBytes()); | 358 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); |
359 REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); | 359 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
360 REPORTER_ASSERT(reporter, 21 == cache2->getBudgetedResourceBytes()); | 360 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
361 | 361 |
362 wrapped = SkNEW_ARGS(TestResource, | 362 wrapped = SkNEW_ARGS(TestResource, |
363 (context->getGpu(), large, GrGpuResource::kWrapped_Life
Cycle)); | 363 (context->getGpu(), large, GrGpuResource::kWrapped_Life
Cycle)); |
364 REPORTER_ASSERT(reporter, 3 == cache2->getResourceCount()); | 364 REPORTER_ASSERT(reporter, 3 == cache->getResourceCount()); |
365 REPORTER_ASSERT(reporter, 21 + large == cache2->getResourceBytes()); | 365 REPORTER_ASSERT(reporter, 21 + large == cache->getResourceBytes()); |
366 REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); | 366 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
367 REPORTER_ASSERT(reporter, 21 == cache2->getBudgetedResourceBytes()); | 367 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
368 | 368 |
369 wrapped->unref(); | 369 wrapped->unref(); |
370 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 370 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
371 REPORTER_ASSERT(reporter, 21 == cache2->getResourceBytes()); | 371 REPORTER_ASSERT(reporter, 21 == cache->getResourceBytes()); |
372 REPORTER_ASSERT(reporter, 2 == cache2->getBudgetedResourceCount()); | 372 REPORTER_ASSERT(reporter, 2 == cache->getBudgetedResourceCount()); |
373 REPORTER_ASSERT(reporter, 21 == cache2->getBudgetedResourceBytes()); | 373 REPORTER_ASSERT(reporter, 21 == cache->getBudgetedResourceBytes()); |
374 | 374 |
375 cache2->purgeAllUnlocked(); | 375 cache->purgeAllUnlocked(); |
376 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 376 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
377 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); | 377 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
378 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); | 378 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
379 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); | 379 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
380 } | 380 } |
381 | 381 |
382 static void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) { | 382 static void test_unbudgeted_to_scratch(skiatest::Reporter* reporter) { |
383 Mock mock(10, 300); | 383 Mock mock(10, 300); |
384 GrContext* context = mock.context(); | 384 GrContext* context = mock.context(); |
385 GrResourceCache2* cache2 = mock.cache(); | 385 GrResourceCache* cache = mock.cache(); |
386 | 386 |
387 TestResource* resource = | 387 TestResource* resource = |
388 TestResource::CreateScratch(context->getGpu(), TestResource::kA_Simulate
dProperty, false); | 388 TestResource::CreateScratch(context->getGpu(), TestResource::kA_Simulate
dProperty, false); |
389 GrScratchKey key; | 389 GrScratchKey key; |
390 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key); | 390 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &key); |
391 | 391 |
392 size_t size = resource->gpuMemorySize(); | 392 size_t size = resource->gpuMemorySize(); |
393 for (int i = 0; i < 2; ++i) { | 393 for (int i = 0; i < 2; ++i) { |
394 // Since this resource is unbudgeted, it should not be reachable as scra
tch. | 394 // Since this resource is unbudgeted, it should not be reachable as scra
tch. |
395 REPORTER_ASSERT(reporter, resource->cacheAccess().getScratchKey() == key
); | 395 REPORTER_ASSERT(reporter, resource->cacheAccess().getScratchKey() == key
); |
396 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch()); | 396 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch()); |
397 REPORTER_ASSERT(reporter, !resource->cacheAccess().isBudgeted()); | 397 REPORTER_ASSERT(reporter, !resource->cacheAccess().isBudgeted()); |
398 REPORTER_ASSERT(reporter, NULL == cache2->findAndRefScratchResource(key)
); | 398 REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(key))
; |
399 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 399 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
400 REPORTER_ASSERT(reporter, size == cache2->getResourceBytes()); | 400 REPORTER_ASSERT(reporter, size == cache->getResourceBytes()); |
401 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); | 401 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
402 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); | 402 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
403 | 403 |
404 // Once it is unrefed, it should become available as scratch. | 404 // Once it is unrefed, it should become available as scratch. |
405 resource->unref(); | 405 resource->unref(); |
406 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 406 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
407 REPORTER_ASSERT(reporter, size == cache2->getResourceBytes()); | 407 REPORTER_ASSERT(reporter, size == cache->getResourceBytes()); |
408 REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); | 408 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
409 REPORTER_ASSERT(reporter, size == cache2->getBudgetedResourceBytes()); | 409 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes()); |
410 resource = static_cast<TestResource*>(cache2->findAndRefScratchResource(
key)); | 410 resource = static_cast<TestResource*>(cache->findAndRefScratchResource(k
ey)); |
411 REPORTER_ASSERT(reporter, resource); | 411 REPORTER_ASSERT(reporter, resource); |
412 REPORTER_ASSERT(reporter, resource->cacheAccess().getScratchKey() == key
); | 412 REPORTER_ASSERT(reporter, resource->cacheAccess().getScratchKey() == key
); |
413 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch()); | 413 REPORTER_ASSERT(reporter, resource->cacheAccess().isScratch()); |
414 REPORTER_ASSERT(reporter, resource->cacheAccess().isBudgeted()); | 414 REPORTER_ASSERT(reporter, resource->cacheAccess().isBudgeted()); |
415 | 415 |
416 if (0 == i) { | 416 if (0 == i) { |
417 // If made unbudgeted, it should return to original state: ref'ed an
d unbudgeted. Try | 417 // If made unbudgeted, it should return to original state: ref'ed an
d unbudgeted. Try |
418 // the above tests again. | 418 // the above tests again. |
419 resource->cacheAccess().makeUnbudgeted(); | 419 resource->cacheAccess().makeUnbudgeted(); |
420 } else { | 420 } else { |
421 // After the second time around, try removing the scratch key | 421 // After the second time around, try removing the scratch key |
422 resource->cacheAccess().removeScratchKey(); | 422 resource->cacheAccess().removeScratchKey(); |
423 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 423 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
424 REPORTER_ASSERT(reporter, size == cache2->getResourceBytes()); | 424 REPORTER_ASSERT(reporter, size == cache->getResourceBytes()); |
425 REPORTER_ASSERT(reporter, 1 == cache2->getBudgetedResourceCount()); | 425 REPORTER_ASSERT(reporter, 1 == cache->getBudgetedResourceCount()); |
426 REPORTER_ASSERT(reporter, size == cache2->getBudgetedResourceBytes()
); | 426 REPORTER_ASSERT(reporter, size == cache->getBudgetedResourceBytes())
; |
427 REPORTER_ASSERT(reporter, !resource->cacheAccess().getScratchKey().i
sValid()); | 427 REPORTER_ASSERT(reporter, !resource->cacheAccess().getScratchKey().i
sValid()); |
428 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch()); | 428 REPORTER_ASSERT(reporter, !resource->cacheAccess().isScratch()); |
429 REPORTER_ASSERT(reporter, resource->cacheAccess().isBudgeted()); | 429 REPORTER_ASSERT(reporter, resource->cacheAccess().isBudgeted()); |
430 | 430 |
431 // now when it is unrefed it should die since it has no key. | 431 // now when it is unrefed it should die since it has no key. |
432 resource->unref(); | 432 resource->unref(); |
433 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 433 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
434 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); | 434 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
435 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceCount()); | 435 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceCount()); |
436 REPORTER_ASSERT(reporter, 0 == cache2->getBudgetedResourceBytes()); | 436 REPORTER_ASSERT(reporter, 0 == cache->getBudgetedResourceBytes()); |
437 } | 437 } |
438 } | 438 } |
439 } | 439 } |
440 | 440 |
441 static void test_duplicate_scratch_key(skiatest::Reporter* reporter) { | 441 static void test_duplicate_scratch_key(skiatest::Reporter* reporter) { |
442 Mock mock(5, 30000); | 442 Mock mock(5, 30000); |
443 GrContext* context = mock.context(); | 443 GrContext* context = mock.context(); |
444 GrResourceCache2* cache2 = mock.cache(); | 444 GrResourceCache* cache = mock.cache(); |
445 | 445 |
446 // Create two resources that have the same scratch key. | 446 // Create two resources that have the same scratch key. |
447 TestResource* a = TestResource::CreateScratch(context->getGpu(), | 447 TestResource* a = TestResource::CreateScratch(context->getGpu(), |
448 TestResource::kB_SimulatedProp
erty); | 448 TestResource::kB_SimulatedProp
erty); |
449 TestResource* b = TestResource::CreateScratch(context->getGpu(), | 449 TestResource* b = TestResource::CreateScratch(context->getGpu(), |
450 TestResource::kB_SimulatedProp
erty); | 450 TestResource::kB_SimulatedProp
erty); |
451 a->setSize(11); | 451 a->setSize(11); |
452 b->setSize(12); | 452 b->setSize(12); |
453 GrScratchKey scratchKey1; | 453 GrScratchKey scratchKey1; |
454 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratch
Key1); | 454 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratch
Key1); |
455 // Check for negative case consistency. (leaks upon test failure.) | 455 // Check for negative case consistency. (leaks upon test failure.) |
456 REPORTER_ASSERT(reporter, NULL == cache2->findAndRefScratchResource(scratchK
ey1)); | 456 REPORTER_ASSERT(reporter, NULL == cache->findAndRefScratchResource(scratchKe
y1)); |
457 | 457 |
458 GrScratchKey scratchKey; | 458 GrScratchKey scratchKey; |
459 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratch
Key); | 459 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratch
Key); |
460 | 460 |
461 // Scratch resources are registered with GrResourceCache2 just by existing.
There are 2. | 461 // Scratch resources are registered with GrResourceCache just by existing. T
here are 2. |
462 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 462 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
463 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey
(scratchKey));) | 463 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(
scratchKey));) |
464 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 464 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
465 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == | 465 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == |
466 cache2->getResourceBytes()); | 466 cache->getResourceBytes()); |
467 | 467 |
468 // Our refs mean that the resources are non purgeable. | 468 // Our refs mean that the resources are non purgeable. |
469 cache2->purgeAllUnlocked(); | 469 cache->purgeAllUnlocked(); |
470 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 470 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
471 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 471 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
472 | 472 |
473 // Unref but don't purge | 473 // Unref but don't purge |
474 a->unref(); | 474 a->unref(); |
475 b->unref(); | 475 b->unref(); |
476 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 476 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
477 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey
(scratchKey));) | 477 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(
scratchKey));) |
478 | 478 |
479 // Purge again. This time resources should be purgeable. | 479 // Purge again. This time resources should be purgeable. |
480 cache2->purgeAllUnlocked(); | 480 cache->purgeAllUnlocked(); |
481 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 481 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
482 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 482 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
483 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey
(scratchKey));) | 483 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(
scratchKey));) |
484 } | 484 } |
485 | 485 |
486 static void test_remove_scratch_key(skiatest::Reporter* reporter) { | 486 static void test_remove_scratch_key(skiatest::Reporter* reporter) { |
487 Mock mock(5, 30000); | 487 Mock mock(5, 30000); |
488 GrContext* context = mock.context(); | 488 GrContext* context = mock.context(); |
489 GrResourceCache2* cache2 = mock.cache(); | 489 GrResourceCache* cache = mock.cache(); |
490 | 490 |
491 // Create two resources that have the same scratch key. | 491 // Create two resources that have the same scratch key. |
492 TestResource* a = TestResource::CreateScratch(context->getGpu(), | 492 TestResource* a = TestResource::CreateScratch(context->getGpu(), |
493 TestResource::kB_SimulatedProp
erty); | 493 TestResource::kB_SimulatedProp
erty); |
494 TestResource* b = TestResource::CreateScratch(context->getGpu(), | 494 TestResource* b = TestResource::CreateScratch(context->getGpu(), |
495 TestResource::kB_SimulatedProp
erty); | 495 TestResource::kB_SimulatedProp
erty); |
496 a->unref(); | 496 a->unref(); |
497 b->unref(); | 497 b->unref(); |
498 | 498 |
499 | 499 |
500 GrScratchKey scratchKey; | 500 GrScratchKey scratchKey; |
501 // Ensure that scratch key lookup is correct for negative case. | 501 // Ensure that scratch key lookup is correct for negative case. |
502 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratch
Key); | 502 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratch
Key); |
503 // (following leaks upon test failure). | 503 // (following leaks upon test failure). |
504 REPORTER_ASSERT(reporter, cache2->findAndRefScratchResource(scratchKey) == N
ULL); | 504 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NU
LL); |
505 | 505 |
506 // Scratch resources are registered with GrResourceCache2 just by existing.
There are 2. | 506 // Scratch resources are registered with GrResourceCache just by existing. T
here are 2. |
507 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratch
Key); | 507 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratch
Key); |
508 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 508 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
509 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache2->countScratchEntriesForKey
(scratchKey));) | 509 SkDEBUGCODE(REPORTER_ASSERT(reporter, 2 == cache->countScratchEntriesForKey(
scratchKey));) |
510 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 510 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
511 | 511 |
512 // Find the first resource and remove its scratch key | 512 // Find the first resource and remove its scratch key |
513 GrGpuResource* find; | 513 GrGpuResource* find; |
514 find = cache2->findAndRefScratchResource(scratchKey); | 514 find = cache->findAndRefScratchResource(scratchKey); |
515 find->cacheAccess().removeScratchKey(); | 515 find->cacheAccess().removeScratchKey(); |
516 // It's still alive, but not cached by scratch key anymore | 516 // It's still alive, but not cached by scratch key anymore |
517 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 517 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
518 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache2->countScratchEntriesForKey
(scratchKey));) | 518 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(
scratchKey));) |
519 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 519 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
520 | 520 |
521 // The cache should immediately delete it when it's unrefed since it isn't a
ccessible. | 521 // The cache should immediately delete it when it's unrefed since it isn't a
ccessible. |
522 find->unref(); | 522 find->unref(); |
523 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | 523 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
524 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache2->countScratchEntriesForKey
(scratchKey));) | 524 SkDEBUGCODE(REPORTER_ASSERT(reporter, 1 == cache->countScratchEntriesForKey(
scratchKey));) |
525 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 525 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
526 | 526 |
527 // Repeat for the second resource. | 527 // Repeat for the second resource. |
528 find = cache2->findAndRefScratchResource(scratchKey); | 528 find = cache->findAndRefScratchResource(scratchKey); |
529 find->cacheAccess().removeScratchKey(); | 529 find->cacheAccess().removeScratchKey(); |
530 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | 530 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
531 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey
(scratchKey));) | 531 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(
scratchKey));) |
532 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 532 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
533 | 533 |
534 // Should be able to call this multiple times with no problem. | 534 // Should be able to call this multiple times with no problem. |
535 find->cacheAccess().removeScratchKey(); | 535 find->cacheAccess().removeScratchKey(); |
536 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | 536 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
537 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey
(scratchKey));) | 537 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(
scratchKey));) |
538 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 538 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
539 | 539 |
540 find->unref(); | 540 find->unref(); |
541 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 541 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
542 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache2->countScratchEntriesForKey
(scratchKey));) | 542 SkDEBUGCODE(REPORTER_ASSERT(reporter, 0 == cache->countScratchEntriesForKey(
scratchKey));) |
543 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 543 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
544 } | 544 } |
545 | 545 |
546 static void test_scratch_key_consistency(skiatest::Reporter* reporter) { | 546 static void test_scratch_key_consistency(skiatest::Reporter* reporter) { |
547 Mock mock(5, 30000); | 547 Mock mock(5, 30000); |
548 GrContext* context = mock.context(); | 548 GrContext* context = mock.context(); |
549 GrResourceCache2* cache2 = mock.cache(); | 549 GrResourceCache* cache = mock.cache(); |
550 | 550 |
551 // Create two resources that have the same scratch key. | 551 // Create two resources that have the same scratch key. |
552 TestResource* a = TestResource::CreateScratch(context->getGpu(), | 552 TestResource* a = TestResource::CreateScratch(context->getGpu(), |
553 TestResource::kB_SimulatedProp
erty); | 553 TestResource::kB_SimulatedProp
erty); |
554 TestResource* b = TestResource::CreateScratch(context->getGpu(), | 554 TestResource* b = TestResource::CreateScratch(context->getGpu(), |
555 TestResource::kB_SimulatedProp
erty); | 555 TestResource::kB_SimulatedProp
erty); |
556 a->unref(); | 556 a->unref(); |
557 b->unref(); | 557 b->unref(); |
558 | 558 |
559 GrScratchKey scratchKey; | 559 GrScratchKey scratchKey; |
(...skipping 14 matching lines...) Expand all Loading... |
574 scratchKey = scratchKey2; | 574 scratchKey = scratchKey2; |
575 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratch
KeySize()); | 575 REPORTER_ASSERT(reporter, scratchKey.size() == TestResource::ExpectedScratch
KeySize()); |
576 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey); | 576 REPORTER_ASSERT(reporter, scratchKey1 != scratchKey); |
577 REPORTER_ASSERT(reporter, scratchKey != scratchKey1); | 577 REPORTER_ASSERT(reporter, scratchKey != scratchKey1); |
578 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey); | 578 REPORTER_ASSERT(reporter, scratchKey2 == scratchKey); |
579 REPORTER_ASSERT(reporter, scratchKey == scratchKey2); | 579 REPORTER_ASSERT(reporter, scratchKey == scratchKey2); |
580 | 580 |
581 // Ensure that scratch key lookup is correct for negative case. | 581 // Ensure that scratch key lookup is correct for negative case. |
582 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratch
Key); | 582 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratch
Key); |
583 // (following leaks upon test failure). | 583 // (following leaks upon test failure). |
584 REPORTER_ASSERT(reporter, cache2->findAndRefScratchResource(scratchKey) == N
ULL); | 584 REPORTER_ASSERT(reporter, cache->findAndRefScratchResource(scratchKey) == NU
LL); |
585 | 585 |
586 // Find the first resource with a scratch key and a copy of a scratch key. | 586 // Find the first resource with a scratch key and a copy of a scratch key. |
587 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratch
Key); | 587 TestResource::ComputeScratchKey(TestResource::kB_SimulatedProperty, &scratch
Key); |
588 GrGpuResource* find = cache2->findAndRefScratchResource(scratchKey); | 588 GrGpuResource* find = cache->findAndRefScratchResource(scratchKey); |
589 REPORTER_ASSERT(reporter, find != NULL); | 589 REPORTER_ASSERT(reporter, find != NULL); |
590 find->unref(); | 590 find->unref(); |
591 | 591 |
592 scratchKey2 = scratchKey; | 592 scratchKey2 = scratchKey; |
593 find = cache2->findAndRefScratchResource(scratchKey2); | 593 find = cache->findAndRefScratchResource(scratchKey2); |
594 REPORTER_ASSERT(reporter, find != NULL); | 594 REPORTER_ASSERT(reporter, find != NULL); |
595 REPORTER_ASSERT(reporter, find == a || find == b); | 595 REPORTER_ASSERT(reporter, find == a || find == b); |
596 | 596 |
597 GrGpuResource* find2 = cache2->findAndRefScratchResource(scratchKey2); | 597 GrGpuResource* find2 = cache->findAndRefScratchResource(scratchKey2); |
598 REPORTER_ASSERT(reporter, find2 != NULL); | 598 REPORTER_ASSERT(reporter, find2 != NULL); |
599 REPORTER_ASSERT(reporter, find2 == a || find2 == b); | 599 REPORTER_ASSERT(reporter, find2 == a || find2 == b); |
600 REPORTER_ASSERT(reporter, find2 != find); | 600 REPORTER_ASSERT(reporter, find2 != find); |
601 find2->unref(); | 601 find2->unref(); |
602 find->unref(); | 602 find->unref(); |
603 } | 603 } |
604 | 604 |
605 static void test_duplicate_content_key(skiatest::Reporter* reporter) { | 605 static void test_duplicate_content_key(skiatest::Reporter* reporter) { |
606 Mock mock(5, 30000); | 606 Mock mock(5, 30000); |
607 GrContext* context = mock.context(); | 607 GrContext* context = mock.context(); |
608 GrResourceCache2* cache2 = mock.cache(); | 608 GrResourceCache* cache = mock.cache(); |
609 | 609 |
610 GrContentKey key; | 610 GrContentKey key; |
611 make_content_key<0>(&key, 0); | 611 make_content_key<0>(&key, 0); |
612 | 612 |
613 // Create two resources that we will attempt to register with the same conte
nt key. | 613 // Create two resources that we will attempt to register with the same conte
nt key. |
614 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 614 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
615 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 615 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
616 a->setSize(11); | 616 a->setSize(11); |
617 b->setSize(12); | 617 b->setSize(12); |
618 | 618 |
619 // Can't set the same content key on two resources. | 619 // Can't set the same content key on two resources. |
620 REPORTER_ASSERT(reporter, a->cacheAccess().setContentKey(key)); | 620 REPORTER_ASSERT(reporter, a->cacheAccess().setContentKey(key)); |
621 REPORTER_ASSERT(reporter, !b->cacheAccess().setContentKey(key)); | 621 REPORTER_ASSERT(reporter, !b->cacheAccess().setContentKey(key)); |
622 | 622 |
623 // Still have two resources because b is still reffed. | 623 // Still have two resources because b is still reffed. |
624 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 624 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
625 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == | 625 REPORTER_ASSERT(reporter, a->gpuMemorySize() + b->gpuMemorySize() == |
626 cache2->getResourceBytes()); | 626 cache->getResourceBytes()); |
627 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 627 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
628 | 628 |
629 b->unref(); | 629 b->unref(); |
630 // Now b should be gone. | 630 // Now b should be gone. |
631 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 631 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
632 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache2->getResourceBytes()); | 632 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes()); |
633 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | 633 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
634 | 634 |
635 cache2->purgeAllUnlocked(); | 635 cache->purgeAllUnlocked(); |
636 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 636 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
637 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache2->getResourceBytes()); | 637 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes()); |
638 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | 638 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
639 | 639 |
640 // Drop the ref on a but it isn't immediately purged as it still has a valid
scratch key. | 640 // Drop the ref on a but it isn't immediately purged as it still has a valid
scratch key. |
641 a->unref(); | 641 a->unref(); |
642 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 642 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
643 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache2->getResourceBytes()); | 643 REPORTER_ASSERT(reporter, a->gpuMemorySize() == cache->getResourceBytes()); |
644 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | 644 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
645 | 645 |
646 cache2->purgeAllUnlocked(); | 646 cache->purgeAllUnlocked(); |
647 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 647 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
648 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); | 648 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
649 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 649 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
650 } | 650 } |
651 | 651 |
652 static void test_purge_invalidated(skiatest::Reporter* reporter) { | 652 static void test_purge_invalidated(skiatest::Reporter* reporter) { |
653 Mock mock(5, 30000); | 653 Mock mock(5, 30000); |
654 GrContext* context = mock.context(); | 654 GrContext* context = mock.context(); |
655 GrResourceCache2* cache2 = mock.cache(); | 655 GrResourceCache* cache = mock.cache(); |
656 | 656 |
657 GrContentKey key1, key2, key3; | 657 GrContentKey key1, key2, key3; |
658 make_content_key<0>(&key1, 1); | 658 make_content_key<0>(&key1, 1); |
659 make_content_key<0>(&key2, 2); | 659 make_content_key<0>(&key2, 2); |
660 make_content_key<0>(&key3, 3); | 660 make_content_key<0>(&key3, 3); |
661 | 661 |
662 // Add three resources to the cache. Only c is usable as scratch. | 662 // Add three resources to the cache. Only c is usable as scratch. |
663 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 663 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
664 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 664 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
665 TestResource* c = TestResource::CreateScratch(context->getGpu(), | 665 TestResource* c = TestResource::CreateScratch(context->getGpu(), |
666 TestResource::kA_SimulatedProp
erty); | 666 TestResource::kA_SimulatedProp
erty); |
667 a->cacheAccess().setContentKey(key1); | 667 a->cacheAccess().setContentKey(key1); |
668 b->cacheAccess().setContentKey(key2); | 668 b->cacheAccess().setContentKey(key2); |
669 c->cacheAccess().setContentKey(key3); | 669 c->cacheAccess().setContentKey(key3); |
670 a->unref(); | 670 a->unref(); |
671 // hold b until *after* the message is sent. | 671 // hold b until *after* the message is sent. |
672 c->unref(); | 672 c->unref(); |
673 | 673 |
674 REPORTER_ASSERT(reporter, cache2->hasContentKey(key1)); | 674 REPORTER_ASSERT(reporter, cache->hasContentKey(key1)); |
675 REPORTER_ASSERT(reporter, cache2->hasContentKey(key2)); | 675 REPORTER_ASSERT(reporter, cache->hasContentKey(key2)); |
676 REPORTER_ASSERT(reporter, cache2->hasContentKey(key3)); | 676 REPORTER_ASSERT(reporter, cache->hasContentKey(key3)); |
677 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); | 677 REPORTER_ASSERT(reporter, 3 == TestResource::NumAlive()); |
678 | 678 |
679 typedef GrContentKeyInvalidatedMessage Msg; | 679 typedef GrContentKeyInvalidatedMessage Msg; |
680 typedef SkMessageBus<GrContentKeyInvalidatedMessage> Bus; | 680 typedef SkMessageBus<GrContentKeyInvalidatedMessage> Bus; |
681 | 681 |
682 // Invalidate two of the three, they should be purged and no longer accessib
le via their keys. | 682 // Invalidate two of the three, they should be purged and no longer accessib
le via their keys. |
683 Bus::Post(Msg(key1)); | 683 Bus::Post(Msg(key1)); |
684 Bus::Post(Msg(key2)); | 684 Bus::Post(Msg(key2)); |
685 cache2->purgeAsNeeded(); | 685 cache->purgeAsNeeded(); |
686 // a should be deleted now, but we still have a ref on b. | 686 // a should be deleted now, but we still have a ref on b. |
687 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); | 687 REPORTER_ASSERT(reporter, !cache->hasContentKey(key1)); |
688 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key2)); | 688 REPORTER_ASSERT(reporter, !cache->hasContentKey(key2)); |
689 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 689 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
690 REPORTER_ASSERT(reporter, cache2->hasContentKey(key3)); | 690 REPORTER_ASSERT(reporter, cache->hasContentKey(key3)); |
691 | 691 |
692 // Invalidate the third. | 692 // Invalidate the third. |
693 Bus::Post(Msg(key3)); | 693 Bus::Post(Msg(key3)); |
694 cache2->purgeAsNeeded(); | 694 cache->purgeAsNeeded(); |
695 // we still have a ref on b, c should be recycled as scratch. | 695 // we still have a ref on b, c should be recycled as scratch. |
696 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 696 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
697 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key3)); | 697 REPORTER_ASSERT(reporter, !cache->hasContentKey(key3)); |
698 | 698 |
699 // make b purgeable. It should be immediately deleted since it has no key. | 699 // make b purgeable. It should be immediately deleted since it has no key. |
700 b->unref(); | 700 b->unref(); |
701 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); | 701 REPORTER_ASSERT(reporter, 1 == TestResource::NumAlive()); |
702 | 702 |
703 // Make sure we actually get to c via it's scratch key, before we say goodby
e. | 703 // Make sure we actually get to c via it's scratch key, before we say goodby
e. |
704 GrScratchKey scratchKey; | 704 GrScratchKey scratchKey; |
705 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratch
Key); | 705 TestResource::ComputeScratchKey(TestResource::kA_SimulatedProperty, &scratch
Key); |
706 GrGpuResource* scratch = cache2->findAndRefScratchResource(scratchKey); | 706 GrGpuResource* scratch = cache->findAndRefScratchResource(scratchKey); |
707 REPORTER_ASSERT(reporter, scratch == c); | 707 REPORTER_ASSERT(reporter, scratch == c); |
708 SkSafeUnref(scratch); | 708 SkSafeUnref(scratch); |
709 | 709 |
710 // Get rid of c. | 710 // Get rid of c. |
711 cache2->purgeAllUnlocked(); | 711 cache->purgeAllUnlocked(); |
712 scratch = cache2->findAndRefScratchResource(scratchKey); | 712 scratch = cache->findAndRefScratchResource(scratchKey); |
713 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 713 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
714 REPORTER_ASSERT(reporter, 0 == cache2->getResourceCount()); | 714 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
715 REPORTER_ASSERT(reporter, 0 == cache2->getResourceBytes()); | 715 REPORTER_ASSERT(reporter, 0 == cache->getResourceBytes()); |
716 REPORTER_ASSERT(reporter, !scratch); | 716 REPORTER_ASSERT(reporter, !scratch); |
717 SkSafeUnref(scratch); | 717 SkSafeUnref(scratch); |
718 } | 718 } |
719 | 719 |
720 static void test_cache_chained_purge(skiatest::Reporter* reporter) { | 720 static void test_cache_chained_purge(skiatest::Reporter* reporter) { |
721 Mock mock(3, 30000); | 721 Mock mock(3, 30000); |
722 GrContext* context = mock.context(); | 722 GrContext* context = mock.context(); |
723 GrResourceCache2* cache2 = mock.cache(); | 723 GrResourceCache* cache = mock.cache(); |
724 | 724 |
725 GrContentKey key1, key2; | 725 GrContentKey key1, key2; |
726 make_content_key<0>(&key1, 1); | 726 make_content_key<0>(&key1, 1); |
727 make_content_key<0>(&key2, 2); | 727 make_content_key<0>(&key2, 2); |
728 | 728 |
729 | 729 |
730 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 730 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
731 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 731 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
732 a->cacheAccess().setContentKey(key1); | 732 a->cacheAccess().setContentKey(key1); |
733 b->cacheAccess().setContentKey(key2); | 733 b->cacheAccess().setContentKey(key2); |
734 | 734 |
735 // Make a cycle | 735 // Make a cycle |
736 a->setUnrefWhenDestroyed(b); | 736 a->setUnrefWhenDestroyed(b); |
737 b->setUnrefWhenDestroyed(a); | 737 b->setUnrefWhenDestroyed(a); |
738 | 738 |
739 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 739 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
740 | 740 |
741 a->unref(); | 741 a->unref(); |
742 b->unref(); | 742 b->unref(); |
743 | 743 |
744 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 744 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
745 | 745 |
746 cache2->purgeAllUnlocked(); | 746 cache->purgeAllUnlocked(); |
747 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 747 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
748 | 748 |
749 // Break the cycle | 749 // Break the cycle |
750 a->setUnrefWhenDestroyed(NULL); | 750 a->setUnrefWhenDestroyed(NULL); |
751 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); | 751 REPORTER_ASSERT(reporter, 2 == TestResource::NumAlive()); |
752 | 752 |
753 cache2->purgeAllUnlocked(); | 753 cache->purgeAllUnlocked(); |
754 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); | 754 REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); |
755 } | 755 } |
756 | 756 |
757 static void test_resource_size_changed(skiatest::Reporter* reporter) { | 757 static void test_resource_size_changed(skiatest::Reporter* reporter) { |
758 GrContentKey key1, key2; | 758 GrContentKey key1, key2; |
759 make_content_key<0>(&key1, 1); | 759 make_content_key<0>(&key1, 1); |
760 make_content_key<0>(&key2, 2); | 760 make_content_key<0>(&key2, 2); |
761 | 761 |
762 // Test changing resources sizes (both increase & decrease). | 762 // Test changing resources sizes (both increase & decrease). |
763 { | 763 { |
764 Mock mock(3, 30000); | 764 Mock mock(3, 30000); |
765 GrContext* context = mock.context(); | 765 GrContext* context = mock.context(); |
766 GrResourceCache2* cache2 = mock.cache(); | 766 GrResourceCache* cache = mock.cache(); |
767 | 767 |
768 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 768 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
769 a->cacheAccess().setContentKey(key1); | 769 a->cacheAccess().setContentKey(key1); |
770 a->unref(); | 770 a->unref(); |
771 | 771 |
772 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 772 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
773 b->cacheAccess().setContentKey(key2); | 773 b->cacheAccess().setContentKey(key2); |
774 b->unref(); | 774 b->unref(); |
775 | 775 |
776 REPORTER_ASSERT(reporter, 200 == cache2->getResourceBytes()); | 776 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes()); |
777 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 777 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
778 { | 778 { |
779 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2->
findAndRefContentResource(key2))); | 779 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache->f
indAndRefContentResource(key2))); |
780 find2->setSize(200); | 780 find2->setSize(200); |
781 SkAutoTUnref<TestResource> find1(static_cast<TestResource*>(cache2->
findAndRefContentResource(key1))); | 781 SkAutoTUnref<TestResource> find1(static_cast<TestResource*>(cache->f
indAndRefContentResource(key1))); |
782 find1->setSize(50); | 782 find1->setSize(50); |
783 } | 783 } |
784 | 784 |
785 REPORTER_ASSERT(reporter, 250 == cache2->getResourceBytes()); | 785 REPORTER_ASSERT(reporter, 250 == cache->getResourceBytes()); |
786 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 786 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
787 } | 787 } |
788 | 788 |
789 // Test increasing a resources size beyond the cache budget. | 789 // Test increasing a resources size beyond the cache budget. |
790 { | 790 { |
791 Mock mock(2, 300); | 791 Mock mock(2, 300); |
792 GrContext* context = mock.context(); | 792 GrContext* context = mock.context(); |
793 GrResourceCache2* cache2 = mock.cache(); | 793 GrResourceCache* cache = mock.cache(); |
794 | 794 |
795 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); | 795 TestResource* a = SkNEW_ARGS(TestResource, (context->getGpu())); |
796 a->setSize(100); | 796 a->setSize(100); |
797 a->cacheAccess().setContentKey(key1); | 797 a->cacheAccess().setContentKey(key1); |
798 a->unref(); | 798 a->unref(); |
799 | 799 |
800 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); | 800 TestResource* b = SkNEW_ARGS(TestResource, (context->getGpu())); |
801 b->setSize(100); | 801 b->setSize(100); |
802 b->cacheAccess().setContentKey(key2); | 802 b->cacheAccess().setContentKey(key2); |
803 b->unref(); | 803 b->unref(); |
804 | 804 |
805 REPORTER_ASSERT(reporter, 200 == cache2->getResourceBytes()); | 805 REPORTER_ASSERT(reporter, 200 == cache->getResourceBytes()); |
806 REPORTER_ASSERT(reporter, 2 == cache2->getResourceCount()); | 806 REPORTER_ASSERT(reporter, 2 == cache->getResourceCount()); |
807 | 807 |
808 { | 808 { |
809 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache2->
findAndRefContentResource(key2))); | 809 SkAutoTUnref<TestResource> find2(static_cast<TestResource*>(cache->f
indAndRefContentResource(key2))); |
810 find2->setSize(201); | 810 find2->setSize(201); |
811 } | 811 } |
812 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); | 812 REPORTER_ASSERT(reporter, !cache->hasContentKey(key1)); |
813 | 813 |
814 REPORTER_ASSERT(reporter, 201 == cache2->getResourceBytes()); | 814 REPORTER_ASSERT(reporter, 201 == cache->getResourceBytes()); |
815 REPORTER_ASSERT(reporter, 1 == cache2->getResourceCount()); | 815 REPORTER_ASSERT(reporter, 1 == cache->getResourceCount()); |
816 } | 816 } |
817 } | 817 } |
818 | 818 |
819 static void test_large_resource_count(skiatest::Reporter* reporter) { | 819 static void test_large_resource_count(skiatest::Reporter* reporter) { |
820 // Set the cache size to double the resource count because we're going to cr
eate 2x that number | 820 // Set the cache size to double the resource count because we're going to cr
eate 2x that number |
821 // resources, using two different key domains. Add a little slop to the byte
s because we resize | 821 // resources, using two different key domains. Add a little slop to the byte
s because we resize |
822 // down to 1 byte after creating the resource. | 822 // down to 1 byte after creating the resource. |
823 static const int kResourceCnt = 2000; | 823 static const int kResourceCnt = 2000; |
824 | 824 |
825 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000); | 825 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000); |
826 GrContext* context = mock.context(); | 826 GrContext* context = mock.context(); |
827 GrResourceCache2* cache2 = mock.cache(); | 827 GrResourceCache* cache = mock.cache(); |
828 | 828 |
829 for (int i = 0; i < kResourceCnt; ++i) { | 829 for (int i = 0; i < kResourceCnt; ++i) { |
830 GrContentKey key1, key2; | 830 GrContentKey key1, key2; |
831 make_content_key<1>(&key1, i); | 831 make_content_key<1>(&key1, i); |
832 make_content_key<2>(&key2, i); | 832 make_content_key<2>(&key2, i); |
833 | 833 |
834 TestResource* resource; | 834 TestResource* resource; |
835 | 835 |
836 resource = SkNEW_ARGS(TestResource, (context->getGpu())); | 836 resource = SkNEW_ARGS(TestResource, (context->getGpu())); |
837 resource->cacheAccess().setContentKey(key1); | 837 resource->cacheAccess().setContentKey(key1); |
838 resource->setSize(1); | 838 resource->setSize(1); |
839 resource->unref(); | 839 resource->unref(); |
840 | 840 |
841 resource = SkNEW_ARGS(TestResource, (context->getGpu())); | 841 resource = SkNEW_ARGS(TestResource, (context->getGpu())); |
842 resource->cacheAccess().setContentKey(key2); | 842 resource->cacheAccess().setContentKey(key2); |
843 resource->setSize(1); | 843 resource->setSize(1); |
844 resource->unref(); | 844 resource->unref(); |
845 } | 845 } |
846 | 846 |
847 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt); | 847 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2 * kResourceCnt); |
848 REPORTER_ASSERT(reporter, cache2->getBudgetedResourceBytes() == 2 * kResourc
eCnt); | 848 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 2 * kResource
Cnt); |
849 REPORTER_ASSERT(reporter, cache2->getBudgetedResourceCount() == 2 * kResourc
eCnt); | 849 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 2 * kResource
Cnt); |
850 REPORTER_ASSERT(reporter, cache2->getResourceBytes() == 2 * kResourceCnt); | 850 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 2 * kResourceCnt); |
851 REPORTER_ASSERT(reporter, cache2->getResourceCount() == 2 * kResourceCnt); | 851 REPORTER_ASSERT(reporter, cache->getResourceCount() == 2 * kResourceCnt); |
852 for (int i = 0; i < kResourceCnt; ++i) { | 852 for (int i = 0; i < kResourceCnt; ++i) { |
853 GrContentKey key1, key2; | 853 GrContentKey key1, key2; |
854 make_content_key<1>(&key1, i); | 854 make_content_key<1>(&key1, i); |
855 make_content_key<2>(&key2, i); | 855 make_content_key<2>(&key2, i); |
856 | 856 |
857 REPORTER_ASSERT(reporter, cache2->hasContentKey(key1)); | 857 REPORTER_ASSERT(reporter, cache->hasContentKey(key1)); |
858 REPORTER_ASSERT(reporter, cache2->hasContentKey(key2)); | 858 REPORTER_ASSERT(reporter, cache->hasContentKey(key2)); |
859 } | 859 } |
860 | 860 |
861 cache2->purgeAllUnlocked(); | 861 cache->purgeAllUnlocked(); |
862 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0); | 862 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 0); |
863 REPORTER_ASSERT(reporter, cache2->getBudgetedResourceBytes() == 0); | 863 REPORTER_ASSERT(reporter, cache->getBudgetedResourceBytes() == 0); |
864 REPORTER_ASSERT(reporter, cache2->getBudgetedResourceCount() == 0); | 864 REPORTER_ASSERT(reporter, cache->getBudgetedResourceCount() == 0); |
865 REPORTER_ASSERT(reporter, cache2->getResourceBytes() == 0); | 865 REPORTER_ASSERT(reporter, cache->getResourceBytes() == 0); |
866 REPORTER_ASSERT(reporter, cache2->getResourceCount() == 0); | 866 REPORTER_ASSERT(reporter, cache->getResourceCount() == 0); |
867 | 867 |
868 for (int i = 0; i < kResourceCnt; ++i) { | 868 for (int i = 0; i < kResourceCnt; ++i) { |
869 GrContentKey key1, key2; | 869 GrContentKey key1, key2; |
870 make_content_key<1>(&key1, i); | 870 make_content_key<1>(&key1, i); |
871 make_content_key<2>(&key2, i); | 871 make_content_key<2>(&key2, i); |
872 | 872 |
873 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key1)); | 873 REPORTER_ASSERT(reporter, !cache->hasContentKey(key1)); |
874 REPORTER_ASSERT(reporter, !cache2->hasContentKey(key2)); | 874 REPORTER_ASSERT(reporter, !cache->hasContentKey(key2)); |
875 } | 875 } |
876 } | 876 } |
877 | 877 |
878 | 878 |
879 //////////////////////////////////////////////////////////////////////////////// | 879 //////////////////////////////////////////////////////////////////////////////// |
880 DEF_GPUTEST(ResourceCache, reporter, factory) { | 880 DEF_GPUTEST(ResourceCache, reporter, factory) { |
881 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { | 881 for (int type = 0; type < GrContextFactory::kLastGLContextType; ++type) { |
882 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G
LContextType>(type); | 882 GrContextFactory::GLContextType glType = static_cast<GrContextFactory::G
LContextType>(type); |
883 if (!GrContextFactory::IsRenderingGLContext(glType)) { | 883 if (!GrContextFactory::IsRenderingGLContext(glType)) { |
884 continue; | 884 continue; |
(...skipping 22 matching lines...) Expand all Loading... |
907 test_duplicate_scratch_key(reporter); | 907 test_duplicate_scratch_key(reporter); |
908 test_remove_scratch_key(reporter); | 908 test_remove_scratch_key(reporter); |
909 test_scratch_key_consistency(reporter); | 909 test_scratch_key_consistency(reporter); |
910 test_purge_invalidated(reporter); | 910 test_purge_invalidated(reporter); |
911 test_cache_chained_purge(reporter); | 911 test_cache_chained_purge(reporter); |
912 test_resource_size_changed(reporter); | 912 test_resource_size_changed(reporter); |
913 test_large_resource_count(reporter); | 913 test_large_resource_count(reporter); |
914 } | 914 } |
915 | 915 |
916 #endif | 916 #endif |
OLD | NEW |