blob: 6a7c89acf8925fbe6045da3daf4512fb4ea259d4 [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
9#if SK_SUPPORT_GPU
10
tfarinaf168b862014-06-19 12:32:29 -070011#include "Benchmark.h"
bsalomon6d3fe022014-07-25 08:35:45 -070012#include "GrGpuResource.h"
tfarinaf168b862014-06-19 12:32:29 -070013#include "GrContext.h"
bsalomonbcf0a522014-10-08 08:40:09 -070014#include "GrGpu.h"
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000015#include "GrResourceCache.h"
16#include "GrStencilBuffer.h"
17#include "GrTexture.h"
bsalomonafbf2d62014-09-30 12:18:44 -070018#include "GrTexturePriv.h"
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000019#include "SkCanvas.h"
20
21enum {
22 CACHE_SIZE_COUNT = 2048,
23 CACHE_SIZE_BYTES = 2 * 1024 * 1024,
24};
25
bsalomon6d3fe022014-07-25 08:35:45 -070026class StencilResource : public GrGpuResource {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000027public:
28 SK_DECLARE_INST_COUNT(StencilResource);
bsalomonc44be0e2014-07-25 07:32:33 -070029 StencilResource(GrGpu* gpu, int id)
30 : INHERITED(gpu, false)
31 , fID(id) {
bsalomon16961262014-08-26 14:01:07 -070032 this->registerWithCache();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000033 }
34
bsalomonc44be0e2014-07-25 07:32:33 -070035 virtual ~StencilResource() { this->release(); }
36
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000037 virtual size_t gpuMemorySize() const SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000038 return 100 + ((fID % 1 == 0) ? -5 : 6);
39 }
40
41 static GrResourceKey ComputeKey(int width, int height, int sampleCnt) {
42 return GrStencilBuffer::ComputeKey(width, height, sampleCnt);
43 }
44
45 int fID;
46
47private:
bsalomon6d3fe022014-07-25 08:35:45 -070048 typedef GrGpuResource INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000049};
50
bsalomon6d3fe022014-07-25 08:35:45 -070051class TextureResource : public GrGpuResource {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000052public:
53 SK_DECLARE_INST_COUNT(TextureResource);
bsalomonc44be0e2014-07-25 07:32:33 -070054 TextureResource(GrGpu* gpu, int id)
55 : INHERITED(gpu, false)
56 , fID(id) {
bsalomon16961262014-08-26 14:01:07 -070057 this->registerWithCache();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000058 }
59
bsalomonc44be0e2014-07-25 07:32:33 -070060 virtual ~TextureResource() { this->release(); }
61
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000062 virtual size_t gpuMemorySize() const SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000063 return 100 + ((fID % 1 == 0) ? -40 : 33);
64 }
65
66 static GrResourceKey ComputeKey(const GrTextureDesc& desc) {
bsalomon1e2530b2014-10-09 09:57:18 -070067 GrCacheID::Key key;
68 memset(&key, 0, sizeof(key));
69 key.fData32[0] = (desc.fWidth) | (desc.fHeight << 16);
70 key.fData32[1] = desc.fConfig | desc.fSampleCnt << 16;
71 key.fData32[2] = desc.fFlags;
72 static int gType = GrResourceKey::GenerateResourceType();
73 static int gDomain = GrCacheID::GenerateDomain();
74 return GrResourceKey(GrCacheID(gDomain, key), gType, 0);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000075 }
76
77 int fID;
78
79private:
bsalomon6d3fe022014-07-25 08:35:45 -070080 typedef GrGpuResource INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000081};
82
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000083static void get_stencil(int i, int* w, int* h, int* s) {
84 *w = i % 1024;
85 *h = i * 2 % 1024;
86 *s = i % 1 == 0 ? 0 : 4;
87}
88
89static void get_texture_desc(int i, GrTextureDesc* desc) {
90 desc->fFlags = kRenderTarget_GrTextureFlagBit |
91 kNoStencil_GrTextureFlagBit;
92 desc->fWidth = i % 1024;
93 desc->fHeight = i * 2 % 1024;
94 desc->fConfig = static_cast<GrPixelConfig>(i % (kLast_GrPixelConfig + 1));
95 desc->fSampleCnt = i % 1 == 0 ? 0 : 4;
96}
97
98static void populate_cache(GrResourceCache* cache, GrGpu* gpu, int resourceCount) {
99 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));
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000104 cache->purgeAsNeeded(1, resource->gpuMemorySize());
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000105 cache->addResource(key, resource);
106 resource->unref();
107 }
108
109 for (int i = 0; i < resourceCount; ++i) {
110 GrTextureDesc desc;
111 get_texture_desc(i, &desc);
112 GrResourceKey key = TextureResource::ComputeKey(desc);
bsalomon6d3fe022014-07-25 08:35:45 -0700113 GrGpuResource* resource = SkNEW_ARGS(TextureResource, (gpu, i));
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000114 cache->purgeAsNeeded(1, resource->gpuMemorySize());
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000115 cache->addResource(key, resource);
116 resource->unref();
117 }
118}
119
120static void check_cache_contents_or_die(GrResourceCache* cache, int k) {
121 // Benchmark find calls that succeed.
122 {
123 GrTextureDesc desc;
124 get_texture_desc(k, &desc);
125 GrResourceKey key = TextureResource::ComputeKey(desc);
bsalomon6d3fe022014-07-25 08:35:45 -0700126 GrGpuResource* item = cache->find(key);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000127 if (NULL == item) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000128 SkFAIL("cache add does not work as expected");
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000129 return;
130 }
131 if (static_cast<TextureResource*>(item)->fID != k) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000132 SkFAIL("cache add does not work as expected");
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000133 return;
134 }
135 }
136 {
137 int w, h, s;
138 get_stencil(k, &w, &h, &s);
139 GrResourceKey key = StencilResource::ComputeKey(w, h, s);
bsalomon6d3fe022014-07-25 08:35:45 -0700140 GrGpuResource* item = cache->find(key);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000141 if (NULL == item) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000142 SkFAIL("cache add does not work as expected");
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000143 return;
144 }
145 if (static_cast<TextureResource*>(item)->fID != k) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000146 SkFAIL("cache add does not work as expected");
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000147 return;
148 }
149 }
150
151 // Benchmark also find calls that always fail.
152 {
153 GrTextureDesc desc;
154 get_texture_desc(k, &desc);
155 desc.fHeight |= 1;
156 GrResourceKey key = TextureResource::ComputeKey(desc);
bsalomon6d3fe022014-07-25 08:35:45 -0700157 GrGpuResource* item = cache->find(key);
bsalomon49f085d2014-09-05 13:34:00 -0700158 if (item) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000159 SkFAIL("cache add does not work as expected");
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000160 return;
161 }
162 }
163 {
164 int w, h, s;
165 get_stencil(k, &w, &h, &s);
166 h |= 1;
167 GrResourceKey key = StencilResource::ComputeKey(w, h, s);
bsalomon6d3fe022014-07-25 08:35:45 -0700168 GrGpuResource* item = cache->find(key);
bsalomon49f085d2014-09-05 13:34:00 -0700169 if (item) {
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000170 SkFAIL("cache add does not work as expected");
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000171 return;
172 }
173 }
174}
175
tfarinaf168b862014-06-19 12:32:29 -0700176class GrResourceCacheBenchAdd : public Benchmark {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000177 enum {
178 RESOURCE_COUNT = CACHE_SIZE_COUNT / 2,
179 DUPLICATE_COUNT = CACHE_SIZE_COUNT / 4,
180 };
181
182public:
183 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
184 return backend == kGPU_Backend;
185 }
186
187protected:
188 virtual const char* onGetName() SK_OVERRIDE {
189 return "grresourcecache_add";
190 }
191
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000192 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000193 GrGpu* gpu = canvas->getGrContext()->getGpu();
194
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000195 for (int i = 0; i < loops; ++i) {
bsalomonbcf0a522014-10-08 08:40:09 -0700196 GrResourceCache cache(gpu->caps(), CACHE_SIZE_COUNT, CACHE_SIZE_BYTES);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000197 populate_cache(&cache, gpu, DUPLICATE_COUNT);
198 populate_cache(&cache, gpu, RESOURCE_COUNT);
199
200 // Check that cache works.
201 for (int k = 0; k < RESOURCE_COUNT; k += 33) {
202 check_cache_contents_or_die(&cache, k);
203 }
204 cache.purgeAllUnlocked();
205 }
206 }
207
208private:
tfarinaf168b862014-06-19 12:32:29 -0700209 typedef Benchmark INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000210};
211
tfarinaf168b862014-06-19 12:32:29 -0700212class GrResourceCacheBenchFind : public Benchmark {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000213 enum {
214 RESOURCE_COUNT = (CACHE_SIZE_COUNT / 2) - 100,
215 DUPLICATE_COUNT = 100
216 };
217
218public:
219 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
220 return backend == kGPU_Backend;
221 }
222
223protected:
224 virtual const char* onGetName() SK_OVERRIDE {
225 return "grresourcecache_find";
226 }
227
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000228 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000229 GrGpu* gpu = canvas->getGrContext()->getGpu();
bsalomonbcf0a522014-10-08 08:40:09 -0700230 GrResourceCache cache(gpu->caps(), CACHE_SIZE_COUNT, CACHE_SIZE_BYTES);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000231 populate_cache(&cache, gpu, DUPLICATE_COUNT);
232 populate_cache(&cache, gpu, RESOURCE_COUNT);
233
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000234 for (int i = 0; i < loops; ++i) {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000235 for (int k = 0; k < RESOURCE_COUNT; ++k) {
236 check_cache_contents_or_die(&cache, k);
237 }
238 }
239 }
240
241private:
tfarinaf168b862014-06-19 12:32:29 -0700242 typedef Benchmark INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000243};
244
245DEF_BENCH( return new GrResourceCacheBenchAdd(); )
246DEF_BENCH( return new GrResourceCacheBenchFind(); )
247
248#endif