blob: e1ec90d51106ea31bddc4e50e61effc0976f8b05 [file] [log] [blame]
commit-bot@chromium.org644629c2013-11-21 06:21:58 +00001
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
bsalomon8b79d232014-11-10 10:19:06 -08009#include "Benchmark.h"
10
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000011#if SK_SUPPORT_GPU
12
bsalomon6d3fe022014-07-25 08:35:45 -070013#include "GrGpuResource.h"
tfarinaf168b862014-06-19 12:32:29 -070014#include "GrContext.h"
bsalomonbcf0a522014-10-08 08:40:09 -070015#include "GrGpu.h"
bsalomon8b79d232014-11-10 10:19:06 -080016#include "GrResourceCache2.h"
bsalomon19cd0f12014-11-24 12:19:05 -080017#include "GrStencilBuffer.h"
18#include "GrTexture.h"
19#include "GrTexturePriv.h"
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000020#include "SkCanvas.h"
21
22enum {
bsalomon19cd0f12014-11-24 12:19:05 -080023 CACHE_SIZE_COUNT = 2048,
24 CACHE_SIZE_BYTES = 2 * 1024 * 1024,
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000025};
26
bsalomon19cd0f12014-11-24 12:19:05 -080027class StencilResource : public GrGpuResource {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000028public:
bsalomon19cd0f12014-11-24 12:19:05 -080029 SK_DECLARE_INST_COUNT(StencilResource);
30 StencilResource(GrGpu* gpu, int id)
31 : INHERITED(gpu, false)
32 , fID(id) {
bsalomon16961262014-08-26 14:01:07 -070033 this->registerWithCache();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000034 }
35
bsalomon19cd0f12014-11-24 12:19:05 -080036 static GrResourceKey ComputeKey(int width, int height, int sampleCnt) {
37 return GrStencilBuffer::ComputeKey(width, height, sampleCnt);
38 }
39
40 int fID;
41
42private:
43 virtual size_t onGpuMemorySize() const SK_OVERRIDE {
44 return 100 + ((fID % 1 == 0) ? -5 : 6);
45 }
46
47 typedef GrGpuResource INHERITED;
48};
49
50class TextureResource : public GrGpuResource {
51public:
52 SK_DECLARE_INST_COUNT(TextureResource);
53 TextureResource(GrGpu* gpu, int id)
54 : INHERITED(gpu, false)
55 , fID(id) {
56 this->registerWithCache();
57 }
58
59 static GrResourceKey ComputeKey(const GrSurfaceDesc& desc) {
bsalomon91175f12014-11-24 07:05:15 -080060 GrCacheID::Key key;
61 memset(&key, 0, sizeof(key));
bsalomon19cd0f12014-11-24 12:19:05 -080062 key.fData32[0] = (desc.fWidth) | (desc.fHeight << 16);
63 key.fData32[1] = desc.fConfig | desc.fSampleCnt << 16;
64 key.fData32[2] = desc.fFlags;
bsalomon91175f12014-11-24 07:05:15 -080065 static int gType = GrResourceKey::GenerateResourceType();
66 static int gDomain = GrCacheID::GenerateDomain();
67 return GrResourceKey(GrCacheID(gDomain, key), gType, 0);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000068 }
69
bsalomon19cd0f12014-11-24 12:19:05 -080070 int fID;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000071
72private:
bsalomon19cd0f12014-11-24 12:19:05 -080073 virtual size_t onGpuMemorySize() const SK_OVERRIDE {
74 return 100 + ((fID % 1 == 0) ? -40 : 33);
75 }
bsalomon69ed47f2014-11-12 11:13:39 -080076
bsalomon6d3fe022014-07-25 08:35:45 -070077 typedef GrGpuResource INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000078};
79
bsalomon19cd0f12014-11-24 12:19:05 -080080static void get_stencil(int i, int* w, int* h, int* s) {
81 *w = i % 1024;
82 *h = i * 2 % 1024;
83 *s = i % 1 == 0 ? 0 : 4;
84}
85
86static void get_texture_desc(int i, GrSurfaceDesc* desc) {
87 desc->fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag;
88 desc->fWidth = i % 1024;
89 desc->fHeight = i * 2 % 1024;
90 desc->fConfig = static_cast<GrPixelConfig>(i % (kLast_GrPixelConfig + 1));
91 desc->fSampleCnt = ((i % 2) == 0) ? 0 : 4;
92}
93
bsalomon71cb0c22014-11-14 12:10:14 -080094static void populate_cache(GrGpu* gpu, int resourceCount) {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000095 for (int i = 0; i < resourceCount; ++i) {
bsalomon19cd0f12014-11-24 12:19:05 -080096 int w, h, s;
97 get_stencil(i, &w, &h, &s);
98 GrResourceKey key = GrStencilBuffer::ComputeKey(w, h, s);
99 GrGpuResource* resource = SkNEW_ARGS(StencilResource, (gpu, i));
100 resource->cacheAccess().setContentKey(key);
101 resource->unref();
102 }
103
104 for (int i = 0; i < resourceCount; ++i) {
105 GrSurfaceDesc desc;
106 get_texture_desc(i, &desc);
107 GrResourceKey key = TextureResource::ComputeKey(desc);
108 GrGpuResource* resource = SkNEW_ARGS(TextureResource, (gpu, i));
bsalomon71cb0c22014-11-14 12:10:14 -0800109 resource->cacheAccess().setContentKey(key);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000110 resource->unref();
111 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000112}
113
bsalomon19cd0f12014-11-24 12:19:05 -0800114static void check_cache_contents_or_die(GrResourceCache2* cache, int k) {
115 // Benchmark find calls that succeed.
116 {
117 GrSurfaceDesc desc;
118 get_texture_desc(k, &desc);
119 GrResourceKey key = TextureResource::ComputeKey(desc);
120 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
121 if (!item) {
122 SkFAIL("cache add does not work as expected");
123 return;
124 }
125 if (static_cast<TextureResource*>(item.get())->fID != k) {
126 SkFAIL("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 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
135 if (!item) {
136 SkFAIL("cache add does not work as expected");
137 return;
138 }
139 if (static_cast<TextureResource*>(item.get())->fID != k) {
140 SkFAIL("cache add does not work as expected");
141 return;
142 }
143 }
144
145 // Benchmark also find calls that always fail.
146 {
147 GrSurfaceDesc desc;
148 get_texture_desc(k, &desc);
149 desc.fHeight |= 1;
150 GrResourceKey key = TextureResource::ComputeKey(desc);
151 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
152 if (item) {
153 SkFAIL("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 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
163 if (item) {
164 SkFAIL("cache add does not work as expected");
165 return;
166 }
167 }
168}
169
tfarinaf168b862014-06-19 12:32:29 -0700170class GrResourceCacheBenchAdd : public Benchmark {
bsalomon19cd0f12014-11-24 12:19:05 -0800171 enum {
172 RESOURCE_COUNT = CACHE_SIZE_COUNT / 2,
173 };
174
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000175public:
bsalomon19cd0f12014-11-24 12:19:05 -0800176 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -0800177 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000178 }
179
180protected:
bsalomon19cd0f12014-11-24 12:19:05 -0800181 virtual const char* onGetName() SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000182 return "grresourcecache_add";
183 }
184
bsalomon19cd0f12014-11-24 12:19:05 -0800185 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -0800186 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
187 if (NULL == context) {
188 return;
189 }
190 // Set the cache budget to be very large so no purging occurs.
bsalomon19cd0f12014-11-24 12:19:05 -0800191 context->setResourceCacheLimits(2 * RESOURCE_COUNT, 1 << 30);
bsalomon8b79d232014-11-10 10:19:06 -0800192
bsalomon8b79d232014-11-10 10:19:06 -0800193 GrResourceCache2* cache2 = context->getResourceCache2();
194
195 // Make sure the cache is empty.
bsalomon71cb0c22014-11-14 12:10:14 -0800196 cache2->purgeAllUnlocked();
197 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800198
199 GrGpu* gpu = context->getGpu();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000200
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000201 for (int i = 0; i < loops; ++i) {
bsalomon19cd0f12014-11-24 12:19:05 -0800202 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
203
204 populate_cache(gpu, RESOURCE_COUNT);
205
206 // Check that cache works.
207 for (int k = 0; k < RESOURCE_COUNT; k += 33) {
208 check_cache_contents_or_die(cache2, k);
209 }
210 cache2->purgeAllUnlocked();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000211 }
212 }
213
214private:
tfarinaf168b862014-06-19 12:32:29 -0700215 typedef Benchmark INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000216};
217
tfarinaf168b862014-06-19 12:32:29 -0700218class GrResourceCacheBenchFind : public Benchmark {
bsalomon19cd0f12014-11-24 12:19:05 -0800219 enum {
220 RESOURCE_COUNT = CACHE_SIZE_COUNT / 2,
221 };
222
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000223public:
bsalomon19cd0f12014-11-24 12:19:05 -0800224 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -0800225 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000226 }
227
228protected:
bsalomon19cd0f12014-11-24 12:19:05 -0800229 virtual const char* onGetName() SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000230 return "grresourcecache_find";
231 }
232
bsalomon19cd0f12014-11-24 12:19:05 -0800233 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
234 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
235 if (NULL == context) {
bsalomon8b79d232014-11-10 10:19:06 -0800236 return;
237 }
238 // Set the cache budget to be very large so no purging occurs.
bsalomon19cd0f12014-11-24 12:19:05 -0800239 context->setResourceCacheLimits(2 * RESOURCE_COUNT, 1 << 30);
bsalomon8b79d232014-11-10 10:19:06 -0800240
bsalomon19cd0f12014-11-24 12:19:05 -0800241 GrResourceCache2* cache2 = context->getResourceCache2();
bsalomon8b79d232014-11-10 10:19:06 -0800242
243 // Make sure the cache is empty.
bsalomon71cb0c22014-11-14 12:10:14 -0800244 cache2->purgeAllUnlocked();
245 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800246
bsalomon19cd0f12014-11-24 12:19:05 -0800247 GrGpu* gpu = context->getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800248
bsalomon19cd0f12014-11-24 12:19:05 -0800249 populate_cache(gpu, RESOURCE_COUNT);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000250
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000251 for (int i = 0; i < loops; ++i) {
bsalomon19cd0f12014-11-24 12:19:05 -0800252 for (int k = 0; k < RESOURCE_COUNT; ++k) {
253 check_cache_contents_or_die(cache2, k);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000254 }
255 }
256 }
257
258private:
tfarinaf168b862014-06-19 12:32:29 -0700259 typedef Benchmark INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000260};
261
262DEF_BENCH( return new GrResourceCacheBenchAdd(); )
263DEF_BENCH( return new GrResourceCacheBenchFind(); )
264
265#endif