aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/GpuLayerCacheTest.cpp
blob: b22809c553fc7b94ad55f57f1023349186200f50 (plain)
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
/*
* Copyright 2014 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/

// Disabling this test since it is for the layer hoister which is current disabled.
// The test fails when we add a discard to a newly created render target.
#if 0

#include "GrContext.h"
#include "GrLayerCache.h"
#include "GrResourceCache.h"
#include "SkPictureRecorder.h"
#include "Test.h"

class TestingAccess {
public:
    static int NumPlots() {
        return GrLayerCache::kNumPlotsX * GrLayerCache::kNumPlotsY;
    }
    static SkISize PlotSize() {
        return SkISize::Make(GrLayerCache::kAtlasTextureWidth / GrLayerCache::kNumPlotsX,
                             GrLayerCache::kAtlasTextureHeight / GrLayerCache::kNumPlotsY);
    }

    static GrTexture* GetBackingTexture(GrLayerCache* cache) {
        return cache->fAtlas->getTextureOrNull();
    }

    static int NumLayers(GrLayerCache* cache) {
        return cache->numLayers();
    }
    static void Purge(GrLayerCache* cache, uint32_t pictureID) {
        cache->purge(pictureID);
    }
    static int Uses(GrCachedLayer* layer) {
        return layer->uses();
    }
    static GrCachedLayer* Find(GrLayerCache* cache, uint32_t pictureID,
                               const SkMatrix& initialMat,
                               const int* key, int keySize) {
        return cache->findLayer(pictureID, initialMat, key, keySize);
    }
};

// Add several layers to the cache
static void create_layers(skiatest::Reporter* reporter,
                          GrLayerCache* cache,
                          const SkPicture& picture,
                          int numToAdd,
                          int idOffset) {

    for (int i = 0; i < numToAdd; ++i) {
        int key[1] = { idOffset+i+1 };
        GrCachedLayer* layer = cache->findLayerOrCreate(picture.uniqueID(),
                                                        idOffset+i+1, idOffset+i+2,
                                                        SkIRect::MakeEmpty(),
                                                        SkIRect::MakeEmpty(),
                                                        SkMatrix::I(),
                                                        key, 1,
                                                        nullptr);
        REPORTER_ASSERT(reporter, layer);
        GrCachedLayer* temp = TestingAccess::Find(cache, picture.uniqueID(), SkMatrix::I(),
                                                  key, 1);
        REPORTER_ASSERT(reporter, temp == layer);

        REPORTER_ASSERT(reporter, TestingAccess::NumLayers(cache) == idOffset + i + 1);

        REPORTER_ASSERT(reporter, picture.uniqueID() == layer->pictureID());
        REPORTER_ASSERT(reporter, layer->start() == idOffset + i + 1);
        REPORTER_ASSERT(reporter, layer->stop() == idOffset + i + 2);
        REPORTER_ASSERT(reporter, !layer->texture());
        REPORTER_ASSERT(reporter, !layer->paint());
        REPORTER_ASSERT(reporter, !layer->isAtlased());
    }
}

static void lock_layer(skiatest::Reporter* reporter,
                       GrLayerCache* cache,
                       GrCachedLayer* layer) {
    // Make each layer big enough to consume one whole plot in the atlas
    GrSurfaceDesc desc;
    desc.fFlags = kRenderTarget_GrSurfaceFlag;
    desc.fWidth = TestingAccess::PlotSize().fWidth;
    desc.fHeight = TestingAccess::PlotSize().fHeight;
    desc.fConfig = kSkia8888_GrPixelConfig;

    bool needsRerendering;
    bool inAtlas = cache->tryToAtlas(layer, desc, &needsRerendering);
    if (!inAtlas) {
        cache->lock(layer, desc, &needsRerendering);
    }
    REPORTER_ASSERT(reporter, needsRerendering);

    cache->lock(layer, desc, &needsRerendering);
    REPORTER_ASSERT(reporter, !needsRerendering);

    REPORTER_ASSERT(reporter, layer->texture());
    REPORTER_ASSERT(reporter, layer->locked());

    cache->addUse(layer);

    REPORTER_ASSERT(reporter, 1 == TestingAccess::Uses(layer));
}

// This test case exercises the public API of the GrLayerCache class.
// In particular it checks its interaction with the resource cache (w.r.t.
// locking & unlocking textures).
// TODO: need to add checks on VRAM usage!
DEF_GPUTEST_FOR_RENDERING_CONTEXTS(GpuLayerCache, reporter, ctxInfo) {
    // Add one more layer than can fit in the atlas
    static const int kInitialNumLayers = TestingAccess::NumPlots() + 1;

#if GR_CACHE_STATS
    GrResourceCache::Stats stats;
#endif

    sk_sp<SkPicture> picture;

    {
        SkPictureRecorder recorder;
        SkCanvas* c = recorder.beginRecording(1, 1);
        // Draw something, anything, to prevent an empty-picture optimization,
        // which is a singleton and never purged.
        c->drawRect(SkRect::MakeWH(1,1), SkPaint());
        picture = recorder.finishRecordingAsPicture();
    }

    GrResourceCache* resourceCache = ctxInfo.grContext()->getResourceCache();

    GrLayerCache cache(ctxInfo.grContext());

    create_layers(reporter, &cache, *picture, kInitialNumLayers, 0);

    for (int i = 0; i < kInitialNumLayers; ++i) {
        int key[1] = { i + 1 };
        GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID(), SkMatrix::I(),
                                                   key, 1);
        REPORTER_ASSERT(reporter, layer);

        lock_layer(reporter, &cache, layer);

#if GR_CACHE_STATS
        resourceCache->getStats(&stats);
#endif

        // The first 4 layers should be in the atlas (and thus have non-empty rects)
        if (i < TestingAccess::NumPlots()) {
            REPORTER_ASSERT(reporter, layer->isAtlased());
#if GR_CACHE_STATS
            REPORTER_ASSERT(reporter, 1 == stats.fTotal);
#endif
        } else {
            // The 5th layer couldn't fit in the atlas
            REPORTER_ASSERT(reporter, !layer->isAtlased());
#if GR_CACHE_STATS
            REPORTER_ASSERT(reporter, 2 == stats.fTotal);
#endif
        }
    }

    // Unlock the textures
    for (int i = 0; i < kInitialNumLayers; ++i) {
        int key[1] = { i+1 };

        GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID(), SkMatrix::I(),
                                                   key, 1);
        REPORTER_ASSERT(reporter, layer);
        cache.removeUse(layer);
    }

#if GR_CACHE_STATS
    resourceCache->getStats(&stats);
    REPORTER_ASSERT(reporter, 2 == stats.fTotal);
    // The floating layer is purgeable the cache is not
    REPORTER_ASSERT(reporter, 1 == stats.fNumPurgeable);
    REPORTER_ASSERT(reporter, 1 == stats.fNumNonPurgeable);
#endif

    for (int i = 0; i < kInitialNumLayers; ++i) {
        int key[1] = { i+1 };

        GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID(), SkMatrix::I(),
                                                   key, 1);
        REPORTER_ASSERT(reporter, layer);

        // All the layers should be unlocked
        REPORTER_ASSERT(reporter, !layer->locked());

        // When hoisted layers aren't cached they are aggressively removed
        // from the atlas
#if GR_CACHE_HOISTED_LAYERS
        // The first 4 layers should still be in the atlas.
        if (i < 4) {
            REPORTER_ASSERT(reporter, layer->texture());
            REPORTER_ASSERT(reporter, layer->isAtlased());
        } else {
#endif
            // The final layer should not be atlased.
            REPORTER_ASSERT(reporter, !layer->texture());
            REPORTER_ASSERT(reporter, !layer->isAtlased());
#if GR_CACHE_HOISTED_LAYERS
        }
#endif
    }

    // Let go of the backing texture
    cache.end();
    REPORTER_ASSERT(reporter, nullptr == TestingAccess::GetBackingTexture(&cache));

#if GR_CACHE_STATS
    resourceCache->getStats(&stats);
    REPORTER_ASSERT(reporter, 2 == stats.fTotal);
    // Now both the floater and the atlas are purgeable
    REPORTER_ASSERT(reporter, 2 == stats.fNumPurgeable);
#endif

    // re-attach to the backing texture
    cache.begin();
    REPORTER_ASSERT(reporter, TestingAccess::GetBackingTexture(&cache));

#if GR_CACHE_STATS
    resourceCache->getStats(&stats);
    REPORTER_ASSERT(reporter, 2 == stats.fTotal);
    // The atlas is restored to being non-purgeable
    REPORTER_ASSERT(reporter, 1 == stats.fNumPurgeable);
    REPORTER_ASSERT(reporter, 1 == stats.fNumNonPurgeable);
#endif

    {
        int key[1] = { kInitialNumLayers+1 };

        // Add an additional layer. Since all the layers are unlocked this
        // will force out the first atlased layer
        create_layers(reporter, &cache, *picture, 1, kInitialNumLayers);
        GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID(), SkMatrix::I(),
                                                   key, 1);
        REPORTER_ASSERT(reporter, layer);

        lock_layer(reporter, &cache, layer);
        cache.removeUse(layer);
    }

    for (int i = 0; i < kInitialNumLayers+1; ++i) {
        int key[1] = { i+1 };

        GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID(), SkMatrix::I(),
                                                   key, 1);
#if GR_CACHE_HOISTED_LAYERS
        // 3 old layers plus the new one should be in the atlas.
        if (1 == i || 2 == i || 3 == i || 5 == i) {
            REPORTER_ASSERT(reporter, layer);
            REPORTER_ASSERT(reporter, !layer->locked());
            REPORTER_ASSERT(reporter, layer->texture());
            REPORTER_ASSERT(reporter, layer->isAtlased());
        } else if (4 == i) {
#endif
            // The one that was never atlased should still be around
            REPORTER_ASSERT(reporter, layer);

            REPORTER_ASSERT(reporter, !layer->texture());
            REPORTER_ASSERT(reporter, !layer->isAtlased());
#if GR_CACHE_HOISTED_LAYERS
        } else {
            // The one bumped out of the atlas (i.e., 0) should be gone
            REPORTER_ASSERT(reporter, nullptr == layer);
        }
#endif
    }

    //--------------------------------------------------------------------
    // Free them all SkGpuDevice-style. This will not free up the
    // atlas' texture but will eliminate all the layers.
    TestingAccess::Purge(&cache, picture->uniqueID());

    REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0);

#if GR_CACHE_STATS
    resourceCache->getStats(&stats);
    REPORTER_ASSERT(reporter, 2 == stats.fTotal);
    // Atlas isn't purgeable
    REPORTER_ASSERT(reporter, 1 == stats.fNumPurgeable);
    REPORTER_ASSERT(reporter, 1 == stats.fNumNonPurgeable);
#endif

    //--------------------------------------------------------------------
    // Test out the GrContext-style purge. This should remove all the layers
    // and the atlas.
    // Re-create the layers
    create_layers(reporter, &cache, *picture, kInitialNumLayers, 0);

    // Free them again GrContext-style. This should free up everything.
    cache.freeAll();

    REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0);

    REPORTER_ASSERT(reporter, nullptr == TestingAccess::GetBackingTexture(&cache));

#if GR_CACHE_STATS
    resourceCache->getStats(&stats);
    REPORTER_ASSERT(reporter, 2 == stats.fTotal);
    REPORTER_ASSERT(reporter, 2 == stats.fNumPurgeable);
#endif

    // Purge the resource cache ...
    resourceCache->purgeAllUnlocked();

#if GR_CACHE_STATS
    resourceCache->getStats(&stats);
    REPORTER_ASSERT(reporter, 0 == stats.fTotal);
#endif

    // and try to re-attach to the backing texture. This should fail
    cache.begin();
    REPORTER_ASSERT(reporter, nullptr == TestingAccess::GetBackingTexture(&cache));

    //--------------------------------------------------------------------
    // Test out the MessageBus-style purge. This will not free the atlas
    // but should eliminate the free-floating layers.
    create_layers(reporter, &cache, *picture, kInitialNumLayers, 0);

    // Allocate/use the layers
    for (int i = 0; i < kInitialNumLayers; ++i) {
        int key[1] = { i + 1 };
        GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID(), SkMatrix::I(),
                                                   key, 1);
        REPORTER_ASSERT(reporter, layer);

        lock_layer(reporter, &cache, layer);
    }

#if GR_CACHE_STATS
    resourceCache->getStats(&stats);
    REPORTER_ASSERT(reporter, 2 == stats.fTotal);
    REPORTER_ASSERT(reporter, 2 == stats.fNumNonPurgeable);
#endif

    // Unlock the textures
    for (int i = 0; i < kInitialNumLayers; ++i) {
        int key[1] = { i+1 };

        GrCachedLayer* layer = TestingAccess::Find(&cache, picture->uniqueID(), SkMatrix::I(),
                                                   key, 1);
        REPORTER_ASSERT(reporter, layer);
        cache.removeUse(layer);
    }

    picture.reset(nullptr);
    cache.processDeletedPictures();

    REPORTER_ASSERT(reporter, TestingAccess::NumLayers(&cache) == 0);

#if GR_CACHE_STATS
    resourceCache->getStats(&stats);
    REPORTER_ASSERT(reporter, 2 == stats.fTotal);
    REPORTER_ASSERT(reporter, 1 == stats.fNumPurgeable);
    REPORTER_ASSERT(reporter, 1 == stats.fNumNonPurgeable);
#endif

    cache.end();

#if GR_CACHE_STATS
    resourceCache->getStats(&stats);
    REPORTER_ASSERT(reporter, 2 == stats.fTotal);
    REPORTER_ASSERT(reporter, 2 == stats.fNumPurgeable);
#endif
}

#endif