forked from google/skia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProxyCacheTest.cpp
454 lines (356 loc) · 18.6 KB
/
ProxyCacheTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* Copyright 2023 Google LLC
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "tests/Test.h"
#include "include/core/SkBitmap.h"
#include "include/gpu/graphite/Context.h"
#include "include/gpu/graphite/Recorder.h"
#include "src/gpu/GpuTypesPriv.h"
#include "src/gpu/graphite/Caps.h"
#include "src/gpu/graphite/ContextPriv.h"
#include "src/gpu/graphite/ProxyCache.h"
#include "src/gpu/graphite/RecorderPriv.h"
#include "src/gpu/graphite/Texture.h"
#include "src/gpu/graphite/TextureProxy.h"
#include "tools/DecodeUtils.h"
#include "tools/Resources.h"
#include "tools/graphite/GraphiteTestContext.h"
#include <thread>
namespace skgpu::graphite {
// This test exercises the basic MessageBus behavior of the ProxyCache by manually inserting an
// SkBitmap into the proxy cache and then changing its contents. This simple test should create
// an IDChangeListener that will remove the entry in the cache when the bitmap is changed and
// the resulting message processed.
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ProxyCacheTest1, r, context, CtsEnforcement::kNextRelease) {
std::unique_ptr<Recorder> recorder = context->makeRecorder();
ProxyCache* proxyCache = recorder->priv().proxyCache();
SkBitmap bitmap;
bool success = ToolUtils::GetResourceAsBitmap("images/mandrill_128.png", &bitmap);
REPORTER_ASSERT(r, success);
if (!success) {
return;
}
REPORTER_ASSERT(r, proxyCache->numCached() == 0);
sk_sp<TextureProxy> proxy = proxyCache->findOrCreateCachedProxy(recorder.get(), bitmap,
Mipmapped::kNo);
REPORTER_ASSERT(r, proxyCache->numCached() == 1);
bitmap.eraseColor(SK_ColorBLACK);
proxyCache->forceProcessInvalidKeyMsgs();
REPORTER_ASSERT(r, proxyCache->numCached() == 0);
}
// This test checks that, if the same bitmap is added to two separate ProxyCaches, when it is
// changed, both of the ProxyCaches will receive the message.
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ProxyCacheTest2, r, context, CtsEnforcement::kNextRelease) {
std::unique_ptr<Recorder> recorder1 = context->makeRecorder();
ProxyCache* proxyCache1 = recorder1->priv().proxyCache();
std::unique_ptr<Recorder> recorder2 = context->makeRecorder();
ProxyCache* proxyCache2 = recorder2->priv().proxyCache();
SkBitmap bitmap;
bool success = ToolUtils::GetResourceAsBitmap("images/mandrill_128.png", &bitmap);
REPORTER_ASSERT(r, success);
if (!success) {
return;
}
REPORTER_ASSERT(r, proxyCache1->numCached() == 0);
REPORTER_ASSERT(r, proxyCache2->numCached() == 0);
sk_sp<TextureProxy> proxy1 = proxyCache1->findOrCreateCachedProxy(recorder1.get(), bitmap,
Mipmapped::kNo);
sk_sp<TextureProxy> proxy2 = proxyCache2->findOrCreateCachedProxy(recorder2.get(), bitmap,
Mipmapped::kNo);
REPORTER_ASSERT(r, proxyCache1->numCached() == 1);
REPORTER_ASSERT(r, proxyCache2->numCached() == 1);
bitmap.eraseColor(SK_ColorBLACK);
proxyCache1->forceProcessInvalidKeyMsgs();
proxyCache2->forceProcessInvalidKeyMsgs();
REPORTER_ASSERT(r, proxyCache1->numCached() == 0);
REPORTER_ASSERT(r, proxyCache2->numCached() == 0);
}
// This test exercises mipmap selectivity of the ProxyCache. Mipmapped and non-mipmapped version
// of the same bitmap are keyed differently. Requesting a non-mipmapped version will
// return a mipmapped version, if it exists.
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ProxyCacheTest3, r, context, CtsEnforcement::kNextRelease) {
std::unique_ptr<Recorder> recorder = context->makeRecorder();
ProxyCache* proxyCache = recorder->priv().proxyCache();
SkBitmap bitmap;
bool success = ToolUtils::GetResourceAsBitmap("images/mandrill_128.png", &bitmap);
REPORTER_ASSERT(r, success);
if (!success) {
return;
}
REPORTER_ASSERT(r, proxyCache->numCached() == 0);
sk_sp<TextureProxy> nonMipmapped = proxyCache->findOrCreateCachedProxy(recorder.get(), bitmap,
Mipmapped::kNo);
REPORTER_ASSERT(r, nonMipmapped->mipmapped() == Mipmapped::kNo);
REPORTER_ASSERT(r, proxyCache->numCached() == 1);
sk_sp<TextureProxy> test = proxyCache->findOrCreateCachedProxy(recorder.get(), bitmap,
Mipmapped::kNo);
REPORTER_ASSERT(r, nonMipmapped == test);
REPORTER_ASSERT(r, proxyCache->numCached() == 1);
sk_sp<TextureProxy> mipmapped = proxyCache->findOrCreateCachedProxy(recorder.get(), bitmap,
Mipmapped::kYes);
REPORTER_ASSERT(r, mipmapped->mipmapped() == Mipmapped::kYes);
REPORTER_ASSERT(r, mipmapped != nonMipmapped);
REPORTER_ASSERT(r, proxyCache->numCached() == 2);
test = proxyCache->findOrCreateCachedProxy(recorder.get(), bitmap, Mipmapped::kNo);
REPORTER_ASSERT(r, mipmapped == test);
REPORTER_ASSERT(r, proxyCache->numCached() == 2);
test = proxyCache->findOrCreateCachedProxy(recorder.get(), bitmap, Mipmapped::kYes);
REPORTER_ASSERT(r, mipmapped == test);
REPORTER_ASSERT(r, proxyCache->numCached() == 2);
}
namespace {
struct ProxyCacheSetup {
bool valid() const {
return !fBitmap1.empty() && !fBitmap2.empty() && fProxy1 && fProxy2;
}
SkBitmap fBitmap1;
sk_sp<TextureProxy> fProxy1;
SkBitmap fBitmap2;
sk_sp<TextureProxy> fProxy2;
skgpu::StdSteadyClock::time_point fTimeBetweenProxyCreation;
skgpu::StdSteadyClock::time_point fTimeAfterAllProxyCreation;
};
ProxyCacheSetup setup_test(Context* context,
skiatest::graphite::GraphiteTestContext* testContext,
Recorder* recorder,
skiatest::Reporter* r) {
ProxyCache* proxyCache = recorder->priv().proxyCache();
ProxyCacheSetup setup;
bool success1 = ToolUtils::GetResourceAsBitmap("images/mandrill_32.png", &setup.fBitmap1);
bool success2 = ToolUtils::GetResourceAsBitmap("images/mandrill_64.png", &setup.fBitmap2);
if (!success1 || !success2) {
return {};
}
REPORTER_ASSERT(r, proxyCache->numCached() == 0);
setup.fProxy1 = proxyCache->findOrCreateCachedProxy(recorder, setup.fBitmap1, Mipmapped::kNo);
REPORTER_ASSERT(r, proxyCache->numCached() == 1);
{
// Ensure proxy1's Texture is created (and timestamped) at this time
auto recording = recorder->snap();
context->insertRecording({ recording.get() });
context->submit(SyncToCpu::kYes);
}
std::this_thread::sleep_for(std::chrono::milliseconds(2));
setup.fTimeBetweenProxyCreation = skgpu::StdSteadyClock::now();
std::this_thread::sleep_for(std::chrono::milliseconds(2));
setup.fProxy2 = proxyCache->findOrCreateCachedProxy(recorder, setup.fBitmap2, Mipmapped::kNo);
REPORTER_ASSERT(r, proxyCache->numCached() == 2);
{
// Ensure proxy2's Texture is created (and timestamped) at this time
auto recording = recorder->snap();
context->insertRecording({ recording.get() });
testContext->syncedSubmit(context);
}
std::this_thread::sleep_for(std::chrono::milliseconds(2));
setup.fTimeAfterAllProxyCreation = skgpu::StdSteadyClock::now();
std::this_thread::sleep_for(std::chrono::milliseconds(2));
return setup;
}
} // anonymous namespace
// This test exercises the ProxyCache's freeUniquelyHeld method.
DEF_CONDITIONAL_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ProxyCacheTest4,
r,
context,
testContext,
true,
CtsEnforcement::kNextRelease) {
std::unique_ptr<Recorder> recorder = context->makeRecorder();
ProxyCache* proxyCache = recorder->priv().proxyCache();
ProxyCacheSetup setup = setup_test(context, testContext, recorder.get(), r);
REPORTER_ASSERT(r, setup.valid());
if (!setup.valid()) {
return;
}
proxyCache->forceFreeUniquelyHeld();
REPORTER_ASSERT(r, proxyCache->numCached() == 2);
setup.fProxy1.reset();
proxyCache->forceFreeUniquelyHeld();
REPORTER_ASSERT(r, proxyCache->numCached() == 1);
setup.fProxy2.reset();
proxyCache->forceFreeUniquelyHeld();
REPORTER_ASSERT(r, proxyCache->numCached() == 0);
}
// This test exercises the ProxyCache's purgeProxiesNotUsedSince method.
DEF_CONDITIONAL_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ProxyCacheTest5,
r,
context,
testContext,
true,
CtsEnforcement::kNextRelease) {
std::unique_ptr<Recorder> recorder = context->makeRecorder();
ProxyCache* proxyCache = recorder->priv().proxyCache();
ProxyCacheSetup setup = setup_test(context, testContext, recorder.get(), r);
REPORTER_ASSERT(r, setup.valid());
if (!setup.valid()) {
return;
}
REPORTER_ASSERT(r, !setup.fProxy1->texture()->testingShouldDeleteASAP());
REPORTER_ASSERT(r, !setup.fProxy2->texture()->testingShouldDeleteASAP());
proxyCache->forcePurgeProxiesNotUsedSince(setup.fTimeBetweenProxyCreation);
REPORTER_ASSERT(r, proxyCache->numCached() == 1);
REPORTER_ASSERT(r, setup.fProxy1->texture()->testingShouldDeleteASAP());
REPORTER_ASSERT(r, !setup.fProxy2->texture()->testingShouldDeleteASAP());
sk_sp<TextureProxy> test = proxyCache->find(setup.fBitmap1, Mipmapped::kNo);
REPORTER_ASSERT(r, !test); // proxy1 should've been purged
proxyCache->forcePurgeProxiesNotUsedSince(setup.fTimeAfterAllProxyCreation);
REPORTER_ASSERT(r, proxyCache->numCached() == 0);
REPORTER_ASSERT(r, setup.fProxy1->texture()->testingShouldDeleteASAP());
REPORTER_ASSERT(r, setup.fProxy2->texture()->testingShouldDeleteASAP());
}
// This test simply verifies that the ProxyCache is correctly updating the Resource's
// last access time stamp.
DEF_CONDITIONAL_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ProxyCacheTest6,
r,
context,
testContext,
true,
CtsEnforcement::kNextRelease) {
std::unique_ptr<Recorder> recorder = context->makeRecorder();
ProxyCache* proxyCache = recorder->priv().proxyCache();
ProxyCacheSetup setup = setup_test(context, testContext, recorder.get(), r);
REPORTER_ASSERT(r, setup.valid());
if (!setup.valid()) {
return;
}
REPORTER_ASSERT(r, !setup.fProxy1->texture()->testingShouldDeleteASAP());
REPORTER_ASSERT(r, !setup.fProxy2->texture()->testingShouldDeleteASAP());
// update proxy1's timestamp
sk_sp<TextureProxy> test = proxyCache->findOrCreateCachedProxy(recorder.get(), setup.fBitmap1,
Mipmapped::kNo);
REPORTER_ASSERT(r, test == setup.fProxy1);
std::this_thread::sleep_for(std::chrono::milliseconds(2));
auto timeAfterProxy1Update = skgpu::StdSteadyClock::now();
proxyCache->forcePurgeProxiesNotUsedSince(setup.fTimeBetweenProxyCreation);
REPORTER_ASSERT(r, proxyCache->numCached() == 2);
REPORTER_ASSERT(r, !setup.fProxy1->texture()->testingShouldDeleteASAP());
REPORTER_ASSERT(r, !setup.fProxy2->texture()->testingShouldDeleteASAP());
proxyCache->forcePurgeProxiesNotUsedSince(setup.fTimeAfterAllProxyCreation);
REPORTER_ASSERT(r, proxyCache->numCached() == 1);
REPORTER_ASSERT(r, !setup.fProxy1->texture()->testingShouldDeleteASAP());
REPORTER_ASSERT(r, setup.fProxy2->texture()->testingShouldDeleteASAP());
test = proxyCache->find(setup.fBitmap2, Mipmapped::kNo);
REPORTER_ASSERT(r, !test); // proxy2 should've been purged
proxyCache->forcePurgeProxiesNotUsedSince(timeAfterProxy1Update);
REPORTER_ASSERT(r, proxyCache->numCached() == 0);
REPORTER_ASSERT(r, setup.fProxy1->texture()->testingShouldDeleteASAP());
REPORTER_ASSERT(r, setup.fProxy2->texture()->testingShouldDeleteASAP());
}
// Verify that the ProxyCache's purgeProxiesNotUsedSince method can clear out multiple proxies.
DEF_CONDITIONAL_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ProxyCacheTest7,
r,
context,
testContext,
true,
CtsEnforcement::kNextRelease) {
std::unique_ptr<Recorder> recorder = context->makeRecorder();
ProxyCache* proxyCache = recorder->priv().proxyCache();
ProxyCacheSetup setup = setup_test(context, testContext, recorder.get(), r);
REPORTER_ASSERT(r, setup.valid());
if (!setup.valid()) {
return;
}
REPORTER_ASSERT(r, !setup.fProxy1->texture()->testingShouldDeleteASAP());
REPORTER_ASSERT(r, !setup.fProxy2->texture()->testingShouldDeleteASAP());
proxyCache->forcePurgeProxiesNotUsedSince(setup.fTimeAfterAllProxyCreation);
REPORTER_ASSERT(r, proxyCache->numCached() == 0);
REPORTER_ASSERT(r, setup.fProxy1->texture()->testingShouldDeleteASAP());
REPORTER_ASSERT(r, setup.fProxy2->texture()->testingShouldDeleteASAP());
}
// Verify that the ProxyCache's freeUniquelyHeld behavior is working in the ResourceCache.
DEF_CONDITIONAL_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ProxyCacheTest8,
r,
context,
testContext,
true,
CtsEnforcement::kNextRelease) {
std::unique_ptr<Recorder> recorder = context->makeRecorder();
ResourceCache* resourceCache = recorder->priv().resourceCache();
ProxyCache* proxyCache = recorder->priv().proxyCache();
resourceCache->setMaxBudget(0);
ProxyCacheSetup setup = setup_test(context, testContext, recorder.get(), r);
REPORTER_ASSERT(r, setup.valid());
if (!setup.valid()) {
return;
}
resourceCache->forcePurgeAsNeeded();
REPORTER_ASSERT(r, proxyCache->numCached() == 2);
setup.fProxy1.reset();
proxyCache->forceProcessInvalidKeyMsgs();
// unreffing fProxy1 and forcing message processing shouldn't purge proxy1 from the cache
sk_sp<TextureProxy> test = proxyCache->find(setup.fBitmap1, Mipmapped::kNo);
REPORTER_ASSERT(r, test);
test.reset();
resourceCache->forcePurgeAsNeeded();
REPORTER_ASSERT(r, proxyCache->numCached() == 1);
test = proxyCache->find(setup.fBitmap1, Mipmapped::kNo);
REPORTER_ASSERT(r, !test); // proxy1 should've been purged
setup.fProxy2.reset();
proxyCache->forceProcessInvalidKeyMsgs();
// unreffing fProxy2 and forcing message processing shouldn't purge proxy2 from the cache
test = proxyCache->find(setup.fBitmap2, Mipmapped::kNo);
REPORTER_ASSERT(r, test);
test.reset();
resourceCache->forcePurgeAsNeeded();
REPORTER_ASSERT(r, proxyCache->numCached() == 0);
}
// Verify that the ProxyCache's purgeProxiesNotUsedSince behavior is working when triggered from
// ResourceCache.
DEF_CONDITIONAL_GRAPHITE_TEST_FOR_ALL_CONTEXTS(ProxyCacheTest9,
r,
context,
testContext,
true,
CtsEnforcement::kNextRelease) {
std::unique_ptr<Recorder> recorder = context->makeRecorder();
ResourceCache* resourceCache = recorder->priv().resourceCache();
ProxyCache* proxyCache = recorder->priv().proxyCache();
ProxyCacheSetup setup = setup_test(context, testContext, recorder.get(), r);
REPORTER_ASSERT(r, setup.valid());
if (!setup.valid()) {
return;
}
REPORTER_ASSERT(r, setup.fProxy1->isInstantiated());
REPORTER_ASSERT(r, setup.fProxy2->isInstantiated());
if (!setup.fProxy1->texture() || !setup.fProxy2->texture()) {
return;
}
// Clear out resources used to setup bitmap proxies so we can track things easier.
resourceCache->setMaxBudget(0);
resourceCache->setMaxBudget(256 * (1 << 20));
REPORTER_ASSERT(r, proxyCache->numCached() == 2);
int baselineResourceCount = resourceCache->getResourceCount();
// When buffer maps are async it can take extra time for buffers to be returned to the cache.
if (context->priv().caps()->bufferMapsAreAsync()) {
// We expect at least 2 textures (and possibly buffers).
REPORTER_ASSERT(r, baselineResourceCount >= 2);
} else {
REPORTER_ASSERT(r, baselineResourceCount == 2);
}
// Force a command buffer ref on the second proxy in the cache so it can't be purged immediately
setup.fProxy2->texture()->refCommandBuffer();
Resource* proxy2ResourcePtr = setup.fProxy2->texture();
setup.fProxy1.reset();
setup.fProxy2.reset();
REPORTER_ASSERT(r, proxyCache->numCached() == 2);
auto timeAfterProxyCreation = skgpu::StdSteadyClock::now();
// This should trigger both proxies to be purged from the ProxyCache. The first proxy should
// immediately be purged from the ResourceCache as well since it has not other refs. The second
// proxy will not be purged from the ResourceCache since it still has a command buffer ref.
// However, that resource should have its deleteASAP flag set.
resourceCache->purgeResourcesNotUsedSince(timeAfterProxyCreation);
REPORTER_ASSERT(r, proxyCache->numCached() == 0);
REPORTER_ASSERT(r, resourceCache->getResourceCount() == baselineResourceCount - 1);
REPORTER_ASSERT(r, resourceCache->topOfPurgeableQueue() == nullptr);
REPORTER_ASSERT(r, proxy2ResourcePtr->testingShouldDeleteASAP());
// Removing the command buffer ref and returning proxy2Resource to the cache should cause it to
// immediately get deleted without going in the purgeable queue.
proxy2ResourcePtr->unrefCommandBuffer();
resourceCache->forceProcessReturnedResources();
REPORTER_ASSERT(r, proxyCache->numCached() == 0);
REPORTER_ASSERT(r, resourceCache->getResourceCount() == baselineResourceCount - 2);
REPORTER_ASSERT(r, resourceCache->topOfPurgeableQueue() == nullptr);
}
} // namespace skgpu::graphite