GrResourceCacheBench.cpp revision 10e23caea3106be125acea10a637789e5a15c728
1
2/*
3 * Copyright 2013 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "Benchmark.h"
10
11#if SK_SUPPORT_GPU
12
13#include "GrGpuResource.h"
14#include "GrContext.h"
15#include "GrGpu.h"
16#include "GrResourceCache2.h"
17#include "SkCanvas.h"
18
19enum {
20    CACHE_SIZE_COUNT = 4096,
21};
22
23class BenchResource : public GrGpuResource {
24public:
25    SK_DECLARE_INST_COUNT(BenchResource);
26    BenchResource (GrGpu* gpu)
27        : INHERITED(gpu, false) {
28        this->registerWithCache();
29    }
30
31    static GrResourceKey ComputeKey(int i) {
32        GrCacheID::Key key;
33        memset(&key, 0, sizeof(key));
34        key.fData32[0] = i;
35        static int gType = GrResourceKey::GenerateResourceType();
36        static int gDomain = GrCacheID::GenerateDomain();
37        return GrResourceKey(GrCacheID(gDomain, key), gType, 0);
38    }
39
40
41private:
42    size_t onGpuMemorySize() const SK_OVERRIDE { return 100; }
43
44    typedef GrGpuResource INHERITED;
45};
46
47static void populate_cache(GrGpu* gpu, int resourceCount) {
48    for (int i = 0; i < resourceCount; ++i) {
49        GrResourceKey key = BenchResource::ComputeKey(i);
50        GrGpuResource* resource = SkNEW_ARGS(BenchResource, (gpu));
51        resource->cacheAccess().setContentKey(key);
52        resource->unref();
53    }
54}
55
56class GrResourceCacheBenchAdd : public Benchmark {
57public:
58    bool isSuitableFor(Backend backend) SK_OVERRIDE {
59        return backend == kNonRendering_Backend;
60    }
61
62protected:
63    const char* onGetName() SK_OVERRIDE {
64        return "grresourcecache_add";
65    }
66
67    void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
68        SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
69        if (NULL == context) {
70            return;
71        }
72        // Set the cache budget to be very large so no purging occurs.
73        context->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
74
75        GrResourceCache2* cache2 = context->getResourceCache2();
76
77        // Make sure the cache is empty.
78        cache2->purgeAllUnlocked();
79        SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
80
81        GrGpu* gpu = context->getGpu();
82
83        for (int i = 0; i < loops; ++i) {
84            populate_cache(gpu, CACHE_SIZE_COUNT);
85            SkASSERT(CACHE_SIZE_COUNT == cache2->getResourceCount());
86        }
87    }
88
89private:
90    typedef Benchmark INHERITED;
91};
92
93class GrResourceCacheBenchFind : public Benchmark {
94public:
95    bool isSuitableFor(Backend backend) SK_OVERRIDE {
96        return backend == kNonRendering_Backend;
97    }
98
99protected:
100    const char* onGetName() SK_OVERRIDE {
101        return "grresourcecache_find";
102    }
103
104    void onPreDraw() SK_OVERRIDE {
105        fContext.reset(GrContext::CreateMockContext());
106        if (!fContext) {
107            return;
108        }
109        // Set the cache budget to be very large so no purging occurs.
110        fContext->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
111
112        GrResourceCache2* cache2 = fContext->getResourceCache2();
113
114        // Make sure the cache is empty.
115        cache2->purgeAllUnlocked();
116        SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
117
118        GrGpu* gpu = fContext->getGpu();
119
120        populate_cache(gpu, CACHE_SIZE_COUNT);
121    }
122
123    void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
124        if (!fContext) {
125            return;
126        }
127        GrResourceCache2* cache2 = fContext->getResourceCache2();
128        SkASSERT(CACHE_SIZE_COUNT == cache2->getResourceCount());
129        for (int i = 0; i < loops; ++i) {
130            for (int k = 0; k < CACHE_SIZE_COUNT; ++k) {
131                GrResourceKey key = BenchResource::ComputeKey(k);
132                SkAutoTUnref<GrGpuResource> resource(cache2->findAndRefContentResource(key));
133                SkASSERT(resource);
134            }
135        }
136    }
137
138private:
139    SkAutoTUnref<GrContext> fContext;
140    typedef Benchmark INHERITED;
141};
142
143DEF_BENCH( return new GrResourceCacheBenchAdd(); )
144DEF_BENCH( return new GrResourceCacheBenchFind(); )
145
146#endif
147