• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "GrGpuResourcePriv.h"
15 #include "GrContext.h"
16 #include "GrGpu.h"
17 #include "GrResourceCache.h"
18 #include "SkCanvas.h"
19 
20 enum {
21     CACHE_SIZE_COUNT = 4096,
22 };
23 
24 class BenchResource : public GrGpuResource {
25 public:
26     SK_DECLARE_INST_COUNT(BenchResource);
BenchResource(GrGpu * gpu)27     BenchResource (GrGpu* gpu)
28         : INHERITED(gpu, kCached_LifeCycle) {
29         this->registerWithCache();
30     }
31 
ComputeKey(int i,GrUniqueKey * key)32     static void ComputeKey(int i, GrUniqueKey* key) {
33         static GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
34         GrUniqueKey::Builder builder(key, kDomain, 1);
35         builder[0] = i;
36     }
37 
38 private:
onGpuMemorySize() const39     size_t onGpuMemorySize() const override { return 100; }
40 
41     typedef GrGpuResource INHERITED;
42 };
43 
populate_cache(GrGpu * gpu,int resourceCount)44 static void populate_cache(GrGpu* gpu, int resourceCount) {
45     for (int i = 0; i < resourceCount; ++i) {
46         GrUniqueKey key;
47         BenchResource::ComputeKey(i, &key);
48         GrGpuResource* resource = SkNEW_ARGS(BenchResource, (gpu));
49         resource->resourcePriv().setUniqueKey(key);
50         resource->unref();
51     }
52 }
53 
54 class GrResourceCacheBenchAdd : public Benchmark {
55 public:
isSuitableFor(Backend backend)56     bool isSuitableFor(Backend backend) override {
57         return backend == kNonRendering_Backend;
58     }
59 
60 protected:
onGetName()61     const char* onGetName() override {
62         return "grresourcecache_add";
63     }
64 
onDraw(const int loops,SkCanvas * canvas)65     void onDraw(const int loops, SkCanvas* canvas) override {
66         SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
67         if (NULL == context) {
68             return;
69         }
70         // Set the cache budget to be very large so no purging occurs.
71         context->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
72 
73         GrResourceCache* cache = context->getResourceCache();
74 
75         // Make sure the cache is empty.
76         cache->purgeAllUnlocked();
77         SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
78 
79         GrGpu* gpu = context->getGpu();
80 
81         for (int i = 0; i < loops; ++i) {
82             populate_cache(gpu, CACHE_SIZE_COUNT);
83             SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount());
84         }
85     }
86 
87 private:
88     typedef Benchmark INHERITED;
89 };
90 
91 class GrResourceCacheBenchFind : public Benchmark {
92 public:
isSuitableFor(Backend backend)93     bool isSuitableFor(Backend backend) override {
94         return backend == kNonRendering_Backend;
95     }
96 
97 protected:
onGetName()98     const char* onGetName() override {
99         return "grresourcecache_find";
100     }
101 
onPreDraw()102     void onPreDraw() override {
103         fContext.reset(GrContext::CreateMockContext());
104         if (!fContext) {
105             return;
106         }
107         // Set the cache budget to be very large so no purging occurs.
108         fContext->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
109 
110         GrResourceCache* cache = fContext->getResourceCache();
111 
112         // Make sure the cache is empty.
113         cache->purgeAllUnlocked();
114         SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
115 
116         GrGpu* gpu = fContext->getGpu();
117 
118         populate_cache(gpu, CACHE_SIZE_COUNT);
119     }
120 
onDraw(const int loops,SkCanvas * canvas)121     void onDraw(const int loops, SkCanvas* canvas) override {
122         if (!fContext) {
123             return;
124         }
125         GrResourceCache* cache = fContext->getResourceCache();
126         SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount());
127         for (int i = 0; i < loops; ++i) {
128             for (int k = 0; k < CACHE_SIZE_COUNT; ++k) {
129                 GrUniqueKey key;
130                 BenchResource::ComputeKey(k, &key);
131                 SkAutoTUnref<GrGpuResource> resource(cache->findAndRefUniqueResource(key));
132                 SkASSERT(resource);
133             }
134         }
135     }
136 
137 private:
138     SkAutoTUnref<GrContext> fContext;
139     typedef Benchmark INHERITED;
140 };
141 
142 DEF_BENCH( return new GrResourceCacheBenchAdd(); )
143 DEF_BENCH( return new GrResourceCacheBenchFind(); )
144 
145 #endif
146