blob: ef97ed89711c9472c2c06b9cec5dfb4f47ad8224 [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"
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000017#include "GrStencilBuffer.h"
18#include "GrTexture.h"
bsalomonafbf2d62014-09-30 12:18:44 -070019#include "GrTexturePriv.h"
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000020#include "SkCanvas.h"
21
22enum {
23 CACHE_SIZE_COUNT = 2048,
24 CACHE_SIZE_BYTES = 2 * 1024 * 1024,
25};
26
bsalomon6d3fe022014-07-25 08:35:45 -070027class StencilResource : public GrGpuResource {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000028public:
29 SK_DECLARE_INST_COUNT(StencilResource);
bsalomonc44be0e2014-07-25 07:32:33 -070030 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
bsalomonc44be0e2014-07-25 07:32:33 -070036 virtual ~StencilResource() { this->release(); }
37
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000038 static GrResourceKey ComputeKey(int width, int height, int sampleCnt) {
39 return GrStencilBuffer::ComputeKey(width, height, sampleCnt);
40 }
41
42 int fID;
43
44private:
bsalomon69ed47f2014-11-12 11:13:39 -080045 virtual size_t onGpuMemorySize() const SK_OVERRIDE {
46 return 100 + ((fID % 1 == 0) ? -5 : 6);
47 }
48
bsalomon6d3fe022014-07-25 08:35:45 -070049 typedef GrGpuResource INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000050};
51
bsalomon6d3fe022014-07-25 08:35:45 -070052class TextureResource : public GrGpuResource {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000053public:
54 SK_DECLARE_INST_COUNT(TextureResource);
bsalomonc44be0e2014-07-25 07:32:33 -070055 TextureResource(GrGpu* gpu, int id)
56 : INHERITED(gpu, false)
57 , fID(id) {
bsalomon16961262014-08-26 14:01:07 -070058 this->registerWithCache();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000059 }
60
bsalomonc44be0e2014-07-25 07:32:33 -070061 virtual ~TextureResource() { this->release(); }
62
bsalomonf2703d82014-10-28 14:33:06 -070063 static GrResourceKey ComputeKey(const GrSurfaceDesc& desc) {
bsalomon1e2530b2014-10-09 09:57:18 -070064 GrCacheID::Key key;
65 memset(&key, 0, sizeof(key));
66 key.fData32[0] = (desc.fWidth) | (desc.fHeight << 16);
67 key.fData32[1] = desc.fConfig | desc.fSampleCnt << 16;
68 key.fData32[2] = desc.fFlags;
69 static int gType = GrResourceKey::GenerateResourceType();
70 static int gDomain = GrCacheID::GenerateDomain();
71 return GrResourceKey(GrCacheID(gDomain, key), gType, 0);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000072 }
73
74 int fID;
75
76private:
bsalomon69ed47f2014-11-12 11:13:39 -080077 virtual size_t onGpuMemorySize() const SK_OVERRIDE {
78 return 100 + ((fID % 1 == 0) ? -40 : 33);
79 }
80
bsalomon6d3fe022014-07-25 08:35:45 -070081 typedef GrGpuResource INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000082};
83
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000084static void get_stencil(int i, int* w, int* h, int* s) {
85 *w = i % 1024;
86 *h = i * 2 % 1024;
87 *s = i % 1 == 0 ? 0 : 4;
88}
89
bsalomonf2703d82014-10-28 14:33:06 -070090static void get_texture_desc(int i, GrSurfaceDesc* desc) {
bsalomon71cb0c22014-11-14 12:10:14 -080091 desc->fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000092 desc->fWidth = i % 1024;
93 desc->fHeight = i * 2 % 1024;
94 desc->fConfig = static_cast<GrPixelConfig>(i % (kLast_GrPixelConfig + 1));
bsalomon71cb0c22014-11-14 12:10:14 -080095 desc->fSampleCnt = ((i % 2) == 0) ? 0 : 4;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000096}
97
bsalomon71cb0c22014-11-14 12:10:14 -080098static void populate_cache(GrGpu* gpu, int resourceCount) {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000099 for (int i = 0; i < resourceCount; ++i) {
100 int w, h, s;
101 get_stencil(i, &w, &h, &s);
102 GrResourceKey key = GrStencilBuffer::ComputeKey(w, h, s);
bsalomon6d3fe022014-07-25 08:35:45 -0700103 GrGpuResource* resource = SkNEW_ARGS(StencilResource, (gpu, i));
bsalomon71cb0c22014-11-14 12:10:14 -0800104 resource->cacheAccess().setContentKey(key);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000105 resource->unref();
106 }
107
108 for (int i = 0; i < resourceCount; ++i) {
bsalomonf2703d82014-10-28 14:33:06 -0700109 GrSurfaceDesc desc;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000110 get_texture_desc(i, &desc);
111 GrResourceKey key = TextureResource::ComputeKey(desc);
bsalomon6d3fe022014-07-25 08:35:45 -0700112 GrGpuResource* resource = SkNEW_ARGS(TextureResource, (gpu, i));
bsalomon71cb0c22014-11-14 12:10:14 -0800113 resource->cacheAccess().setContentKey(key);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000114 resource->unref();
115 }
116}
117
bsalomon8b79d232014-11-10 10:19:06 -0800118static void check_cache_contents_or_die(GrResourceCache2* cache, int k) {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000119 // Benchmark find calls that succeed.
120 {
bsalomonf2703d82014-10-28 14:33:06 -0700121 GrSurfaceDesc desc;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000122 get_texture_desc(k, &desc);
123 GrResourceKey key = TextureResource::ComputeKey(desc);
bsalomon8b79d232014-11-10 10:19:06 -0800124 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
125 if (!item) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000126 SkFAIL("cache add does not work as expected");
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000127 return;
128 }
bsalomon8b79d232014-11-10 10:19:06 -0800129 if (static_cast<TextureResource*>(item.get())->fID != k) {
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 }
133 }
134 {
135 int w, h, s;
136 get_stencil(k, &w, &h, &s);
137 GrResourceKey key = StencilResource::ComputeKey(w, h, s);
bsalomon8b79d232014-11-10 10:19:06 -0800138 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
139 if (!item) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000140 SkFAIL("cache add does not work as expected");
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000141 return;
142 }
bsalomon8b79d232014-11-10 10:19:06 -0800143 if (static_cast<TextureResource*>(item.get())->fID != k) {
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 }
147 }
148
149 // Benchmark also find calls that always fail.
150 {
bsalomonf2703d82014-10-28 14:33:06 -0700151 GrSurfaceDesc desc;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000152 get_texture_desc(k, &desc);
153 desc.fHeight |= 1;
154 GrResourceKey key = TextureResource::ComputeKey(desc);
bsalomon8b79d232014-11-10 10:19:06 -0800155 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
bsalomon49f085d2014-09-05 13:34:00 -0700156 if (item) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000157 SkFAIL("cache add does not work as expected");
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000158 return;
159 }
160 }
161 {
162 int w, h, s;
163 get_stencil(k, &w, &h, &s);
164 h |= 1;
165 GrResourceKey key = StencilResource::ComputeKey(w, h, s);
bsalomon8b79d232014-11-10 10:19:06 -0800166 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key));
bsalomon49f085d2014-09-05 13:34:00 -0700167 if (item) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000168 SkFAIL("cache add does not work as expected");
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000169 return;
170 }
171 }
172}
173
tfarinaf168b862014-06-19 12:32:29 -0700174class GrResourceCacheBenchAdd : public Benchmark {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000175 enum {
176 RESOURCE_COUNT = CACHE_SIZE_COUNT / 2,
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000177 };
178
179public:
180 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -0800181 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000182 }
183
184protected:
185 virtual const char* onGetName() SK_OVERRIDE {
186 return "grresourcecache_add";
187 }
188
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000189 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -0800190 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
191 if (NULL == context) {
192 return;
193 }
194 // Set the cache budget to be very large so no purging occurs.
195 context->setResourceCacheLimits(2 * RESOURCE_COUNT, 1 << 30);
196
bsalomon8b79d232014-11-10 10:19:06 -0800197 GrResourceCache2* cache2 = context->getResourceCache2();
198
199 // Make sure the cache is empty.
bsalomon71cb0c22014-11-14 12:10:14 -0800200 cache2->purgeAllUnlocked();
201 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800202
203 GrGpu* gpu = context->getGpu();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000204
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000205 for (int i = 0; i < loops; ++i) {
bsalomon71cb0c22014-11-14 12:10:14 -0800206 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800207
bsalomon71cb0c22014-11-14 12:10:14 -0800208 populate_cache(gpu, RESOURCE_COUNT);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000209
210 // Check that cache works.
211 for (int k = 0; k < RESOURCE_COUNT; k += 33) {
bsalomon8b79d232014-11-10 10:19:06 -0800212 check_cache_contents_or_die(cache2, k);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000213 }
bsalomon71cb0c22014-11-14 12:10:14 -0800214 cache2->purgeAllUnlocked();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000215 }
216 }
217
218private:
tfarinaf168b862014-06-19 12:32:29 -0700219 typedef Benchmark INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000220};
221
tfarinaf168b862014-06-19 12:32:29 -0700222class GrResourceCacheBenchFind : public Benchmark {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000223 enum {
bsalomon8b79d232014-11-10 10:19:06 -0800224 RESOURCE_COUNT = CACHE_SIZE_COUNT / 2,
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000225 };
226
227public:
228 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -0800229 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000230 }
231
232protected:
233 virtual const char* onGetName() SK_OVERRIDE {
234 return "grresourcecache_find";
235 }
236
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000237 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -0800238 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
239 if (NULL == context) {
240 return;
241 }
242 // Set the cache budget to be very large so no purging occurs.
243 context->setResourceCacheLimits(2 * RESOURCE_COUNT, 1 << 30);
244
bsalomon8b79d232014-11-10 10:19:06 -0800245 GrResourceCache2* cache2 = context->getResourceCache2();
246
247 // Make sure the cache is empty.
bsalomon71cb0c22014-11-14 12:10:14 -0800248 cache2->purgeAllUnlocked();
249 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800250
251 GrGpu* gpu = context->getGpu();
252
bsalomon71cb0c22014-11-14 12:10:14 -0800253 populate_cache(gpu, RESOURCE_COUNT);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000254
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000255 for (int i = 0; i < loops; ++i) {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000256 for (int k = 0; k < RESOURCE_COUNT; ++k) {
bsalomon8b79d232014-11-10 10:19:06 -0800257 check_cache_contents_or_die(cache2, k);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000258 }
259 }
260 }
261
262private:
tfarinaf168b862014-06-19 12:32:29 -0700263 typedef Benchmark INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000264};
265
266DEF_BENCH( return new GrResourceCacheBenchAdd(); )
267DEF_BENCH( return new GrResourceCacheBenchFind(); )
268
269#endif