blob: 6133c076571365ab751933c8c5ecc542a6ce2a6c [file] [log] [blame]
commit-bot@chromium.org644629c2013-11-21 06:21:58 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
bsalomon8b79d232014-11-10 10:19:06 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Robert Phillipsf0288102020-07-06 13:45:34 -040011#include "include/gpu/GrDirectContext.h"
Adlai Hollera0693042020-10-14 11:23:11 -040012#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/GrGpu.h"
Greg Daniel456f9b52020-03-05 19:14:18 +000014#include "src/gpu/GrGpuResource.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrGpuResourcePriv.h"
16#include "src/gpu/GrResourceCache.h"
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000017
18enum {
bsalomon10e23ca2014-11-25 05:52:06 -080019 CACHE_SIZE_COUNT = 4096,
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000020};
21
bsalomon10e23ca2014-11-25 05:52:06 -080022class BenchResource : public GrGpuResource {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000023public:
bsalomon10e23ca2014-11-25 05:52:06 -080024 BenchResource (GrGpu* gpu)
kkinnunen2e6055b2016-04-22 01:48:29 -070025 : INHERITED(gpu) {
26 this->registerWithCache(SkBudgeted::kYes);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000027 }
28
kkinnunen54b85112015-05-18 22:47:33 -070029 static void ComputeKey(int i, int keyData32Count, GrUniqueKey* key) {
bsalomon8718aaf2015-02-19 07:24:21 -080030 static GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
kkinnunen54b85112015-05-18 22:47:33 -070031 GrUniqueKey::Builder builder(key, kDomain, keyData32Count);
32 for (int j = 0; j < keyData32Count; ++j) {
33 builder[j] = i + j;
34 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000035 }
36
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000037private:
mtklein36352bf2015-03-25 18:17:31 -070038 size_t onGpuMemorySize() const override { return 100; }
Derek Sollenbergercf6da8c2018-03-29 13:40:02 -040039 const char* getResourceType() const override { return "bench"; }
John Stiles7571f9e2020-09-02 22:42:33 -040040 using INHERITED = GrGpuResource;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000041};
42
kkinnunen54b85112015-05-18 22:47:33 -070043static void populate_cache(GrGpu* gpu, int resourceCount, int keyData32Count) {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000044 for (int i = 0; i < resourceCount; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -080045 GrUniqueKey key;
kkinnunen54b85112015-05-18 22:47:33 -070046 BenchResource::ComputeKey(i, keyData32Count, &key);
halcanary385fe4d2015-08-26 13:07:48 -070047 GrGpuResource* resource = new BenchResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -080048 resource->resourcePriv().setUniqueKey(key);
bsalomon19cd0f12014-11-24 12:19:05 -080049 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:
kkinnunen54b85112015-05-18 22:47:33 -070055 GrResourceCacheBenchAdd(int keyData32Count)
56 : fFullName("grresourcecache_add")
57 , fKeyData32Count(keyData32Count) {
58 if (keyData32Count > 1) {
59 fFullName.appendf("_%d", fKeyData32Count);
60 }
61 }
62
mtklein36352bf2015-03-25 18:17:31 -070063 bool isSuitableFor(Backend backend) override {
bsalomon8b79d232014-11-10 10:19:06 -080064 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000065 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000066protected:
mtklein36352bf2015-03-25 18:17:31 -070067 const char* onGetName() override {
kkinnunen54b85112015-05-18 22:47:33 -070068 return fFullName.c_str();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000069 }
70
mtkleina1ebeb22015-10-01 09:43:39 -070071 void onDraw(int loops, SkCanvas* canvas) override {
Robert Phillipsf4f80112020-07-13 16:13:31 -040072 sk_sp<GrDirectContext> context(GrDirectContext::MakeMock(nullptr));
halcanary96fcdcc2015-08-27 07:41:13 -070073 if (nullptr == context) {
bsalomon8b79d232014-11-10 10:19:06 -080074 return;
75 }
76 // Set the cache budget to be very large so no purging occurs.
bsalomon10e23ca2014-11-25 05:52:06 -080077 context->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
bsalomon8b79d232014-11-10 10:19:06 -080078
Robert Phillips9da87e02019-02-04 13:26:26 -050079 GrResourceCache* cache = context->priv().getResourceCache();
bsalomon8b79d232014-11-10 10:19:06 -080080
81 // Make sure the cache is empty.
bsalomon0ea80f42015-02-11 10:49:59 -080082 cache->purgeAllUnlocked();
83 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -080084
Robert Phillips9da87e02019-02-04 13:26:26 -050085 GrGpu* gpu = context->priv().getGpu();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000086
commit-bot@chromium.org33614712013-12-03 18:17:16 +000087 for (int i = 0; i < loops; ++i) {
kkinnunen54b85112015-05-18 22:47:33 -070088 populate_cache(gpu, CACHE_SIZE_COUNT, fKeyData32Count);
bsalomon0ea80f42015-02-11 10:49:59 -080089 SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount());
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000090 }
91 }
92
93private:
kkinnunen54b85112015-05-18 22:47:33 -070094 SkString fFullName;
95 int fKeyData32Count;
John Stiles7571f9e2020-09-02 22:42:33 -040096 using INHERITED = Benchmark;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000097};
98
tfarinaf168b862014-06-19 12:32:29 -070099class GrResourceCacheBenchFind : public Benchmark {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000100public:
kkinnunen54b85112015-05-18 22:47:33 -0700101 GrResourceCacheBenchFind(int keyData32Count)
102 : fFullName("grresourcecache_find")
103 , fKeyData32Count(keyData32Count) {
104 if (keyData32Count > 1) {
105 fFullName.appendf("_%d", fKeyData32Count);
106 }
107 }
108
mtklein36352bf2015-03-25 18:17:31 -0700109 bool isSuitableFor(Backend backend) override {
bsalomon8b79d232014-11-10 10:19:06 -0800110 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000111 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000112protected:
mtklein36352bf2015-03-25 18:17:31 -0700113 const char* onGetName() override {
kkinnunen54b85112015-05-18 22:47:33 -0700114 return fFullName.c_str();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000115 }
116
joshualitt8a6697a2015-09-30 12:11:07 -0700117 void onDelayedSetup() override {
Robert Phillipsf4f80112020-07-13 16:13:31 -0400118 fContext = GrDirectContext::MakeMock(nullptr);
bsalomon10e23ca2014-11-25 05:52:06 -0800119 if (!fContext) {
bsalomon8b79d232014-11-10 10:19:06 -0800120 return;
121 }
122 // Set the cache budget to be very large so no purging occurs.
bsalomon10e23ca2014-11-25 05:52:06 -0800123 fContext->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
bsalomon8b79d232014-11-10 10:19:06 -0800124
Robert Phillips9da87e02019-02-04 13:26:26 -0500125 GrResourceCache* cache = fContext->priv().getResourceCache();
bsalomon8b79d232014-11-10 10:19:06 -0800126
127 // Make sure the cache is empty.
bsalomon0ea80f42015-02-11 10:49:59 -0800128 cache->purgeAllUnlocked();
129 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800130
Robert Phillips9da87e02019-02-04 13:26:26 -0500131 GrGpu* gpu = fContext->priv().getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800132
kkinnunen54b85112015-05-18 22:47:33 -0700133 populate_cache(gpu, CACHE_SIZE_COUNT, fKeyData32Count);
bsalomon10e23ca2014-11-25 05:52:06 -0800134 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000135
mtkleina1ebeb22015-10-01 09:43:39 -0700136 void onDraw(int loops, SkCanvas* canvas) override {
bsalomon10e23ca2014-11-25 05:52:06 -0800137 if (!fContext) {
138 return;
139 }
Robert Phillips9da87e02019-02-04 13:26:26 -0500140 GrResourceCache* cache = fContext->priv().getResourceCache();
bsalomon0ea80f42015-02-11 10:49:59 -0800141 SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount());
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000142 for (int i = 0; i < loops; ++i) {
bsalomon10e23ca2014-11-25 05:52:06 -0800143 for (int k = 0; k < CACHE_SIZE_COUNT; ++k) {
bsalomon8718aaf2015-02-19 07:24:21 -0800144 GrUniqueKey key;
kkinnunen54b85112015-05-18 22:47:33 -0700145 BenchResource::ComputeKey(k, fKeyData32Count, &key);
Hal Canary2db83612016-11-04 13:02:54 -0400146 sk_sp<GrGpuResource> resource(cache->findAndRefUniqueResource(key));
bsalomon10e23ca2014-11-25 05:52:06 -0800147 SkASSERT(resource);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000148 }
149 }
150 }
151
152private:
Robert Phillipsf4f80112020-07-13 16:13:31 -0400153 sk_sp<GrDirectContext> fContext;
kkinnunen54b85112015-05-18 22:47:33 -0700154 SkString fFullName;
155 int fKeyData32Count;
John Stiles7571f9e2020-09-02 22:42:33 -0400156 using INHERITED = Benchmark;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000157};
158
kkinnunen54b85112015-05-18 22:47:33 -0700159DEF_BENCH( return new GrResourceCacheBenchAdd(1); )
160#ifdef SK_RELEASE
161// Only on release because on debug the SkTDynamicHash validation is too slow.
162DEF_BENCH( return new GrResourceCacheBenchAdd(2); )
163DEF_BENCH( return new GrResourceCacheBenchAdd(3); )
164DEF_BENCH( return new GrResourceCacheBenchAdd(4); )
165DEF_BENCH( return new GrResourceCacheBenchAdd(5); )
166DEF_BENCH( return new GrResourceCacheBenchAdd(10); )
167DEF_BENCH( return new GrResourceCacheBenchAdd(25); )
168DEF_BENCH( return new GrResourceCacheBenchAdd(54); )
169DEF_BENCH( return new GrResourceCacheBenchAdd(55); )
170DEF_BENCH( return new GrResourceCacheBenchAdd(56); )
171#endif
172
173DEF_BENCH( return new GrResourceCacheBenchFind(1); )
174#ifdef SK_RELEASE
175DEF_BENCH( return new GrResourceCacheBenchFind(2); )
176DEF_BENCH( return new GrResourceCacheBenchFind(3); )
177DEF_BENCH( return new GrResourceCacheBenchFind(4); )
178DEF_BENCH( return new GrResourceCacheBenchFind(5); )
179DEF_BENCH( return new GrResourceCacheBenchFind(10); )
180DEF_BENCH( return new GrResourceCacheBenchFind(25); )
181DEF_BENCH( return new GrResourceCacheBenchFind(54); )
182DEF_BENCH( return new GrResourceCacheBenchFind(55); )
183DEF_BENCH( return new GrResourceCacheBenchFind(56); )
184#endif