Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(348)

Side by Side Diff: bench/GrResourceCacheBench.cpp

Issue 73643005: Implement a benchmark for GrResourceCache (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: address review comments Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 #if SK_SUPPORT_GPU
10
11 #include "GrContext.h"
12 #include "GrResource.h"
13 #include "GrResourceCache.h"
14 #include "GrStencilBuffer.h"
15 #include "GrTexture.h"
16 #include "GrUtils.h"
17 #include "SkBenchmark.h"
18
19 enum {
20 CACHE_SIZE_COUNT = 2048,
21 CACHE_SIZE_BYTES = 2 * 1024 * 1024,
22 };
23
24 class StencilResource : public GrResource {
25 public:
26 SK_DECLARE_INST_COUNT(StencilResource);
27 StencilResource(GrGpu* gpu, int id)
28 : INHERITED(gpu, false),
29 fID(id) {
30 }
31 ~StencilResource() {
32 this->release();
33 }
34
35 virtual size_t sizeInBytes() const SK_OVERRIDE {
36 return 100 + ((fID % 1 == 0) ? -5 : 6);
37 }
38
39 static GrResourceKey ComputeKey(int width, int height, int sampleCnt) {
40 return GrStencilBuffer::ComputeKey(width, height, sampleCnt);
41 }
42
43 int fID;
44
45 private:
46 typedef GrResource INHERITED;
47 };
48
49 class TextureResource : public GrResource {
50 public:
51 SK_DECLARE_INST_COUNT(TextureResource);
52 TextureResource(GrGpu* gpu, int id)
53 : INHERITED(gpu, false),
54 fID(id) {
55 }
56 ~TextureResource() {
57 this->release();
58 }
59
60 virtual size_t sizeInBytes() const SK_OVERRIDE {
61 return 100 + ((fID % 1 == 0) ? -40 : 33);
62 }
63
64 static GrResourceKey ComputeKey(const GrTextureDesc& desc) {
65 return GrTexture::ComputeScratchKey(desc);
66 }
67
68 int fID;
69
70 private:
71 typedef GrResource INHERITED;
72 };
73
74 SK_DEFINE_INST_COUNT(StencilResource)
75 SK_DEFINE_INST_COUNT(TextureResource)
76
77 static void get_stencil(int i, int* w, int* h, int* s) {
78 *w = i % 1024;
79 *h = i * 2 % 1024;
80 *s = i % 1 == 0 ? 0 : 4;
81 }
82
83 static void get_texture_desc(int i, GrTextureDesc* desc) {
84 desc->fFlags = kRenderTarget_GrTextureFlagBit |
85 kNoStencil_GrTextureFlagBit;
86 desc->fWidth = i % 1024;
87 desc->fHeight = i * 2 % 1024;
88 desc->fConfig = static_cast<GrPixelConfig>(i % (kLast_GrPixelConfig + 1));
89 desc->fSampleCnt = i % 1 == 0 ? 0 : 4;
90 }
91
92 static void populate_cache(GrResourceCache* cache, GrGpu* gpu, int resourceCount ) {
93 for (int i = 0; i < resourceCount; ++i) {
94 int w, h, s;
95 get_stencil(i, &w, &h, &s);
96 GrResourceKey key = GrStencilBuffer::ComputeKey(w, h, s);
97 GrResource* resource = SkNEW_ARGS(StencilResource, (gpu, i));
98 cache->purgeAsNeeded(1, resource->sizeInBytes());
99 cache->addResource(key, resource);
100 resource->unref();
101 }
102
103 for (int i = 0; i < resourceCount; ++i) {
104 GrTextureDesc desc;
105 get_texture_desc(i, &desc);
106 GrResourceKey key = TextureResource::ComputeKey(desc);
107 GrResource* resource = SkNEW_ARGS(TextureResource, (gpu, i));
108 cache->purgeAsNeeded(1, resource->sizeInBytes());
109 cache->addResource(key, resource);
110 resource->unref();
111 }
112 }
113
114 static void check_cache_contents_or_die(GrResourceCache* cache, int k) {
115 // Benchmark find calls that succeed.
116 {
117 GrTextureDesc desc;
118 get_texture_desc(k, &desc);
119 GrResourceKey key = TextureResource::ComputeKey(desc);
120 GrResource* item = cache->find(key);
121 if (NULL == item) {
122 GrCrash("cache add does not work as expected");
123 return;
124 }
125 if (static_cast<TextureResource*>(item)->fID != k) {
126 GrCrash("cache add does not work as expected");
127 return;
128 }
129 }
130 {
131 int w, h, s;
132 get_stencil(k, &w, &h, &s);
133 GrResourceKey key = StencilResource::ComputeKey(w, h, s);
134 GrResource* item = cache->find(key);
135 if (NULL == item) {
136 GrCrash("cache add does not work as expected");
137 return;
138 }
139 if (static_cast<TextureResource*>(item)->fID != k) {
140 GrCrash("cache add does not work as expected");
141 return;
142 }
143 }
144
145 // Benchmark also find calls that always fail.
146 {
147 GrTextureDesc desc;
148 get_texture_desc(k, &desc);
149 desc.fHeight |= 1;
150 GrResourceKey key = TextureResource::ComputeKey(desc);
151 GrResource* item = cache->find(key);
152 if (NULL != item) {
153 GrCrash("cache add does not work as expected");
154 return;
155 }
156 }
157 {
158 int w, h, s;
159 get_stencil(k, &w, &h, &s);
160 h |= 1;
161 GrResourceKey key = StencilResource::ComputeKey(w, h, s);
162 GrResource* item = cache->find(key);
163 if (NULL != item) {
164 GrCrash("cache add does not work as expected");
165 return;
166 }
167 }
168 }
169
170
171 class GrResourceCacheBenchAdd : public SkBenchmark {
172 enum {
173 RESOURCE_COUNT = CACHE_SIZE_COUNT / 2,
174 DUPLICATE_COUNT = CACHE_SIZE_COUNT / 4,
175 };
176
177 public:
178 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
179 return backend == kGPU_Backend;
180 }
181
182 protected:
183 virtual const char* onGetName() SK_OVERRIDE {
184 return "grresourcecache_add";
185 }
186
187 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
188 GrContext* context = GrUtils::GetGr(canvas);
189 GrGpu* gpu = context->getGpu();
190
191 for (int i = 0; i < this->getLoops(); ++i) {
192 GrResourceCache cache(CACHE_SIZE_COUNT, CACHE_SIZE_BYTES);
193 populate_cache(&cache, gpu, DUPLICATE_COUNT);
194 populate_cache(&cache, gpu, RESOURCE_COUNT);
195
196 // Check that cache works.
197 for (int k = 0; k < RESOURCE_COUNT; k += 33) {
198 check_cache_contents_or_die(&cache, k);
199 }
200 cache.purgeAllUnlocked();
201 }
202 }
203
204 private:
205 typedef SkBenchmark INHERITED;
206 };
207
208
209 class GrResourceCacheBenchFind : public SkBenchmark {
210 enum {
211 RESOURCE_COUNT = (CACHE_SIZE_COUNT / 2) - 100,
212 DUPLICATE_COUNT = 100
213 };
214
215 public:
216 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
217 return backend == kGPU_Backend;
218 }
219
220 protected:
221 virtual const char* onGetName() SK_OVERRIDE {
222 return "grresourcecache_find";
223 }
224
225 virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
226 GrContext* context = GrUtils::GetGr(canvas);
227 GrGpu* gpu = context->getGpu();
228 GrResourceCache cache(CACHE_SIZE_COUNT, CACHE_SIZE_BYTES);
229 populate_cache(&cache, gpu, DUPLICATE_COUNT);
230 populate_cache(&cache, gpu, RESOURCE_COUNT);
231
232 for (int i = 0; i < this->getLoops(); ++i) {
233 for (int k = 0; k < RESOURCE_COUNT; ++k) {
234 check_cache_contents_or_die(&cache, k);
235 }
236 }
237 }
238
239 private:
240 typedef SkBenchmark INHERITED;
241 };
242
243 DEF_BENCH( return new GrResourceCacheBenchAdd(); )
244 DEF_BENCH( return new GrResourceCacheBenchFind(); )
245
246 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698