blob: 39007c30b2a6a63da090f9c6e91cca5bef92738c [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"
bsalomon0ea80f42015-02-11 10:49:59 -080016#include "GrResourceCache.h"
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000017#include "SkCanvas.h"
18
19enum {
bsalomon10e23ca2014-11-25 05:52:06 -080020 CACHE_SIZE_COUNT = 4096,
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000021};
22
bsalomon10e23ca2014-11-25 05:52:06 -080023class BenchResource : public GrGpuResource {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000024public:
bsalomon10e23ca2014-11-25 05:52:06 -080025 SK_DECLARE_INST_COUNT(BenchResource);
26 BenchResource (GrGpu* gpu)
bsalomon5236cf42015-01-14 10:42:08 -080027 : INHERITED(gpu, kCached_LifeCycle) {
bsalomon16961262014-08-26 14:01:07 -070028 this->registerWithCache();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000029 }
30
bsalomon24db3b12015-01-23 04:24:04 -080031 static void ComputeKey(int i, GrContentKey* key) {
32 static GrContentKey::Domain kDomain = GrContentKey::GenerateDomain();
33 GrContentKey::Builder builder(key, kDomain, 1);
34 builder[0] = i;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000035 }
36
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000037private:
bsalomon10e23ca2014-11-25 05:52:06 -080038 size_t onGpuMemorySize() const SK_OVERRIDE { return 100; }
bsalomon69ed47f2014-11-12 11:13:39 -080039
bsalomon6d3fe022014-07-25 08:35:45 -070040 typedef GrGpuResource INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000041};
42
bsalomon71cb0c22014-11-14 12:10:14 -080043static void populate_cache(GrGpu* gpu, int resourceCount) {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000044 for (int i = 0; i < resourceCount; ++i) {
bsalomon24db3b12015-01-23 04:24:04 -080045 GrContentKey key;
46 BenchResource::ComputeKey(i, &key);
bsalomon10e23ca2014-11-25 05:52:06 -080047 GrGpuResource* resource = SkNEW_ARGS(BenchResource, (gpu));
bsalomon19cd0f12014-11-24 12:19:05 -080048 resource->cacheAccess().setContentKey(key);
49 resource->unref();
50 }
bsalomon19cd0f12014-11-24 12:19:05 -080051}
52
tfarinaf168b862014-06-19 12:32:29 -070053class GrResourceCacheBenchAdd : public Benchmark {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000054public:
bsalomon10e23ca2014-11-25 05:52:06 -080055 bool isSuitableFor(Backend backend) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -080056 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000057 }
58
59protected:
bsalomon10e23ca2014-11-25 05:52:06 -080060 const char* onGetName() SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000061 return "grresourcecache_add";
62 }
63
bsalomon10e23ca2014-11-25 05:52:06 -080064 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -080065 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
66 if (NULL == context) {
67 return;
68 }
69 // Set the cache budget to be very large so no purging occurs.
bsalomon10e23ca2014-11-25 05:52:06 -080070 context->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
bsalomon8b79d232014-11-10 10:19:06 -080071
bsalomon0ea80f42015-02-11 10:49:59 -080072 GrResourceCache* cache = context->getResourceCache();
bsalomon8b79d232014-11-10 10:19:06 -080073
74 // Make sure the cache is empty.
bsalomon0ea80f42015-02-11 10:49:59 -080075 cache->purgeAllUnlocked();
76 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -080077
78 GrGpu* gpu = context->getGpu();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000079
commit-bot@chromium.org33614712013-12-03 18:17:16 +000080 for (int i = 0; i < loops; ++i) {
bsalomon10e23ca2014-11-25 05:52:06 -080081 populate_cache(gpu, CACHE_SIZE_COUNT);
bsalomon0ea80f42015-02-11 10:49:59 -080082 SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount());
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000083 }
84 }
85
86private:
tfarinaf168b862014-06-19 12:32:29 -070087 typedef Benchmark INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000088};
89
tfarinaf168b862014-06-19 12:32:29 -070090class GrResourceCacheBenchFind : public Benchmark {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000091public:
bsalomon10e23ca2014-11-25 05:52:06 -080092 bool isSuitableFor(Backend backend) SK_OVERRIDE {
bsalomon8b79d232014-11-10 10:19:06 -080093 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000094 }
95
96protected:
bsalomon10e23ca2014-11-25 05:52:06 -080097 const char* onGetName() SK_OVERRIDE {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000098 return "grresourcecache_find";
99 }
100
bsalomon10e23ca2014-11-25 05:52:06 -0800101 void onPreDraw() SK_OVERRIDE {
102 fContext.reset(GrContext::CreateMockContext());
103 if (!fContext) {
bsalomon8b79d232014-11-10 10:19:06 -0800104 return;
105 }
106 // Set the cache budget to be very large so no purging occurs.
bsalomon10e23ca2014-11-25 05:52:06 -0800107 fContext->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
bsalomon8b79d232014-11-10 10:19:06 -0800108
bsalomon0ea80f42015-02-11 10:49:59 -0800109 GrResourceCache* cache = fContext->getResourceCache();
bsalomon8b79d232014-11-10 10:19:06 -0800110
111 // Make sure the cache is empty.
bsalomon0ea80f42015-02-11 10:49:59 -0800112 cache->purgeAllUnlocked();
113 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800114
bsalomon10e23ca2014-11-25 05:52:06 -0800115 GrGpu* gpu = fContext->getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800116
bsalomon10e23ca2014-11-25 05:52:06 -0800117 populate_cache(gpu, CACHE_SIZE_COUNT);
118 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000119
bsalomon10e23ca2014-11-25 05:52:06 -0800120 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
121 if (!fContext) {
122 return;
123 }
bsalomon0ea80f42015-02-11 10:49:59 -0800124 GrResourceCache* cache = fContext->getResourceCache();
125 SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount());
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000126 for (int i = 0; i < loops; ++i) {
bsalomon10e23ca2014-11-25 05:52:06 -0800127 for (int k = 0; k < CACHE_SIZE_COUNT; ++k) {
bsalomon24db3b12015-01-23 04:24:04 -0800128 GrContentKey key;
129 BenchResource::ComputeKey(k, &key);
bsalomon0ea80f42015-02-11 10:49:59 -0800130 SkAutoTUnref<GrGpuResource> resource(cache->findAndRefContentResource(key));
bsalomon10e23ca2014-11-25 05:52:06 -0800131 SkASSERT(resource);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000132 }
133 }
134 }
135
136private:
bsalomon10e23ca2014-11-25 05:52:06 -0800137 SkAutoTUnref<GrContext> fContext;
tfarinaf168b862014-06-19 12:32:29 -0700138 typedef Benchmark INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000139};
140
141DEF_BENCH( return new GrResourceCacheBenchAdd(); )
142DEF_BENCH( return new GrResourceCacheBenchFind(); )
143
144#endif