blob: 91c77fb589c4c4617ad0814026fd53d4cf4591bb [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"
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000016#include "GrResourceCache.h"
bsalomon8b79d232014-11-10 10:19:06 -080017#include "GrResourceCache2.h"
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000018#include "GrStencilBuffer.h"
19#include "GrTexture.h"
bsalomonafbf2d62014-09-30 12:18:44 -070020#include "GrTexturePriv.h"
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000021#include "SkCanvas.h"
22
23enum {
24 CACHE_SIZE_COUNT = 2048,
25 CACHE_SIZE_BYTES = 2 * 1024 * 1024,
26};
27
bsalomon6d3fe022014-07-25 08:35:45 -070028class StencilResource : public GrGpuResource {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000029public:
30 SK_DECLARE_INST_COUNT(StencilResource);
bsalomonc44be0e2014-07-25 07:32:33 -070031 StencilResource(GrGpu* gpu, int id)
32 : INHERITED(gpu, false)
33 , fID(id) {
bsalomon16961262014-08-26 14:01:07 -070034 this->registerWithCache();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000035 }
36
bsalomonc44be0e2014-07-25 07:32:33 -070037 virtual ~StencilResource() { this->release(); }
38
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000039 virtual size_t gpuMemorySize() const SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000040 return 100 + ((fID % 1 == 0) ? -5 : 6);
41 }
42
43 static GrResourceKey ComputeKey(int width, int height, int sampleCnt) {
44 return GrStencilBuffer::ComputeKey(width, height, sampleCnt);
45 }
46
47 int fID;
48
49private:
bsalomon6d3fe022014-07-25 08:35:45 -070050 typedef GrGpuResource INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000051};
52
bsalomon6d3fe022014-07-25 08:35:45 -070053class TextureResource : public GrGpuResource {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000054public:
55 SK_DECLARE_INST_COUNT(TextureResource);
bsalomonc44be0e2014-07-25 07:32:33 -070056 TextureResource(GrGpu* gpu, int id)
57 : INHERITED(gpu, false)
58 , fID(id) {
bsalomon16961262014-08-26 14:01:07 -070059 this->registerWithCache();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000060 }
61
bsalomonc44be0e2014-07-25 07:32:33 -070062 virtual ~TextureResource() { this->release(); }
63
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000064 virtual size_t gpuMemorySize() const SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000065 return 100 + ((fID % 1 == 0) ? -40 : 33);
66 }
67
bsalomonf2703d82014-10-28 14:33:06 -070068 static GrResourceKey ComputeKey(const GrSurfaceDesc& desc) {
bsalomon1e2530b2014-10-09 09:57:18 -070069 GrCacheID::Key key;
70 memset(&key, 0, sizeof(key));
71 key.fData32[0] = (desc.fWidth) | (desc.fHeight << 16);
72 key.fData32[1] = desc.fConfig | desc.fSampleCnt << 16;
73 key.fData32[2] = desc.fFlags;
74 static int gType = GrResourceKey::GenerateResourceType();
75 static int gDomain = GrCacheID::GenerateDomain();
76 return GrResourceKey(GrCacheID(gDomain, key), gType, 0);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000077 }
78
79 int fID;
80
81private:
bsalomon6d3fe022014-07-25 08:35:45 -070082 typedef GrGpuResource INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000083};
84
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000085static void get_stencil(int i, int* w, int* h, int* s) {
86 *w = i % 1024;
87 *h = i * 2 % 1024;
88 *s = i % 1 == 0 ? 0 : 4;
89}
90
bsalomonf2703d82014-10-28 14:33:06 -070091static void get_texture_desc(int i, GrSurfaceDesc* desc) {
92 desc->fFlags = kRenderTarget_GrSurfaceFlag |
93 kNoStencil_GrSurfaceFlag;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000094 desc->fWidth = i % 1024;
95 desc->fHeight = i * 2 % 1024;
96 desc->fConfig = static_cast<GrPixelConfig>(i % (kLast_GrPixelConfig + 1));
97 desc->fSampleCnt = i % 1 == 0 ? 0 : 4;
98}
99
100static void populate_cache(GrResourceCache* cache, GrGpu* gpu, int resourceCount) {
101 for (int i = 0; i < resourceCount; ++i) {
102 int w, h, s;
103 get_stencil(i, &w, &h, &s);
104 GrResourceKey key = GrStencilBuffer::ComputeKey(w, h, s);
bsalomon6d3fe022014-07-25 08:35:45 -0700105 GrGpuResource* resource = SkNEW_ARGS(StencilResource, (gpu, i));
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000106 cache->purgeAsNeeded(1, resource->gpuMemorySize());
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000107 cache->addResource(key, resource);
108 resource->unref();
109 }
110
111 for (int i = 0; i < resourceCount; ++i) {
bsalomonf2703d82014-10-28 14:33:06 -0700112 GrSurfaceDesc desc;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000113 get_texture_desc(i, &desc);
114 GrResourceKey key = TextureResource::ComputeKey(desc);
bsalomon6d3fe022014-07-25 08:35:45 -0700115 GrGpuResource* resource = SkNEW_ARGS(TextureResource, (gpu, i));
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000116 cache->purgeAsNeeded(1, resource->gpuMemorySize());
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000117 cache->addResource(key, resource);
118 resource->unref();
119 }
120}
121
bsalomon8b79d232014-11-10 10:19:06 -0800122static void check_cache_contents_or_die(GrResourceCache2* cache, int k) {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000123 // Benchmark find calls that succeed.
124 {
bsalomonf2703d82014-10-28 14:33:06 -0700125 GrSurfaceDesc desc;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000126 get_texture_desc(k, &desc);
127 GrResourceKey key = TextureResource::ComputeKey(desc);
bsalomon8b79d232014-11-10 10:19:06 -0800128 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
129 if (!item) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000130 SkFAIL("cache add does not work as expected");
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000131 return;
132 }
bsalomon8b79d232014-11-10 10:19:06 -0800133 if (static_cast<TextureResource*>(item.get())->fID != k) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000134 SkFAIL("cache add does not work as expected");
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000135 return;
136 }
137 }
138 {
139 int w, h, s;
140 get_stencil(k, &w, &h, &s);
141 GrResourceKey key = StencilResource::ComputeKey(w, h, s);
bsalomon8b79d232014-11-10 10:19:06 -0800142 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
143 if (!item) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000144 SkFAIL("cache add does not work as expected");
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000145 return;
146 }
bsalomon8b79d232014-11-10 10:19:06 -0800147 if (static_cast<TextureResource*>(item.get())->fID != k) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000148 SkFAIL("cache add does not work as expected");
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000149 return;
150 }
151 }
152
153 // Benchmark also find calls that always fail.
154 {
bsalomonf2703d82014-10-28 14:33:06 -0700155 GrSurfaceDesc desc;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000156 get_texture_desc(k, &desc);
157 desc.fHeight |= 1;
158 GrResourceKey key = TextureResource::ComputeKey(desc);
bsalomon8b79d232014-11-10 10:19:06 -0800159 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
bsalomon49f085d2014-09-05 13:34:00 -0700160 if (item) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000161 SkFAIL("cache add does not work as expected");
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000162 return;
163 }
164 }
165 {
166 int w, h, s;
167 get_stencil(k, &w, &h, &s);
168 h |= 1;
169 GrResourceKey key = StencilResource::ComputeKey(w, h, s);
bsalomon8b79d232014-11-10 10:19:06 -0800170 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
bsalomon49f085d2014-09-05 13:34:00 -0700171 if (item) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000172 SkFAIL("cache add does not work as expected");
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000173 return;
174 }
175 }
176}
177
tfarinaf168b862014-06-19 12:32:29 -0700178class GrResourceCacheBenchAdd : public Benchmark {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000179 enum {
180 RESOURCE_COUNT = CACHE_SIZE_COUNT / 2,
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000181 };
182
183public:
184 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -0800185 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000186 }
187
188protected:
189 virtual const char* onGetName() SK_OVERRIDE {
190 return "grresourcecache_add";
191 }
192
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000193 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -0800194 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
195 if (NULL == context) {
196 return;
197 }
198 // Set the cache budget to be very large so no purging occurs.
199 context->setResourceCacheLimits(2 * RESOURCE_COUNT, 1 << 30);
200
201 GrResourceCache* cache = context->getResourceCache();
202 GrResourceCache2* cache2 = context->getResourceCache2();
203
204 // Make sure the cache is empty.
205 cache->purgeAllUnlocked();
206 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResourceBytes());
207
208 GrGpu* gpu = context->getGpu();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000209
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000210 for (int i = 0; i < loops; ++i) {
bsalomon8b79d232014-11-10 10:19:06 -0800211 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResourceBytes());
212
213 populate_cache(cache, gpu, RESOURCE_COUNT);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000214
215 // Check that cache works.
216 for (int k = 0; k < RESOURCE_COUNT; k += 33) {
bsalomon8b79d232014-11-10 10:19:06 -0800217 check_cache_contents_or_die(cache2, k);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000218 }
bsalomon8b79d232014-11-10 10:19:06 -0800219 cache->purgeAllUnlocked();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000220 }
221 }
222
223private:
tfarinaf168b862014-06-19 12:32:29 -0700224 typedef Benchmark INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000225};
226
tfarinaf168b862014-06-19 12:32:29 -0700227class GrResourceCacheBenchFind : public Benchmark {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000228 enum {
bsalomon8b79d232014-11-10 10:19:06 -0800229 RESOURCE_COUNT = CACHE_SIZE_COUNT / 2,
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000230 };
231
232public:
233 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -0800234 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000235 }
236
237protected:
238 virtual const char* onGetName() SK_OVERRIDE {
239 return "grresourcecache_find";
240 }
241
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000242 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -0800243 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
244 if (NULL == context) {
245 return;
246 }
247 // Set the cache budget to be very large so no purging occurs.
248 context->setResourceCacheLimits(2 * RESOURCE_COUNT, 1 << 30);
249
250 GrResourceCache* cache = context->getResourceCache();
251 GrResourceCache2* cache2 = context->getResourceCache2();
252
253 // Make sure the cache is empty.
254 cache->purgeAllUnlocked();
255 SkASSERT(0 == cache->getCachedResourceCount() && 0 == cache->getCachedResourceBytes());
256
257 GrGpu* gpu = context->getGpu();
258
259 populate_cache(cache, gpu, RESOURCE_COUNT);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000260
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000261 for (int i = 0; i < loops; ++i) {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000262 for (int k = 0; k < RESOURCE_COUNT; ++k) {
bsalomon8b79d232014-11-10 10:19:06 -0800263 check_cache_contents_or_die(cache2, k);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000264 }
265 }
266 }
267
268private:
tfarinaf168b862014-06-19 12:32:29 -0700269 typedef Benchmark INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000270};
271
272DEF_BENCH( return new GrResourceCacheBenchAdd(); )
273DEF_BENCH( return new GrResourceCacheBenchFind(); )
274
275#endif