blob: bc3803518347bc7df392f35a569cc323bf456f72 [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"
bsalomon3582d3e2015-02-13 14:20:05 -080014#include "GrGpuResourcePriv.h"
tfarinaf168b862014-06-19 12:32:29 -070015#include "GrContext.h"
bsalomonbcf0a522014-10-08 08:40:09 -070016#include "GrGpu.h"
bsalomon0ea80f42015-02-11 10:49:59 -080017#include "GrResourceCache.h"
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000018#include "SkCanvas.h"
19
20enum {
bsalomon10e23ca2014-11-25 05:52:06 -080021 CACHE_SIZE_COUNT = 4096,
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000022};
23
bsalomon10e23ca2014-11-25 05:52:06 -080024class BenchResource : public GrGpuResource {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000025public:
bsalomon10e23ca2014-11-25 05:52:06 -080026 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
kkinnunen54b85112015-05-18 22:47:33 -070031 static void ComputeKey(int i, int keyData32Count, GrUniqueKey* key) {
bsalomon8718aaf2015-02-19 07:24:21 -080032 static GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
kkinnunen54b85112015-05-18 22:47:33 -070033 GrUniqueKey::Builder builder(key, kDomain, keyData32Count);
34 for (int j = 0; j < keyData32Count; ++j) {
35 builder[j] = i + j;
36 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000037 }
38
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000039private:
mtklein36352bf2015-03-25 18:17:31 -070040 size_t onGpuMemorySize() const override { return 100; }
bsalomon6d3fe022014-07-25 08:35:45 -070041 typedef GrGpuResource INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000042};
43
kkinnunen54b85112015-05-18 22:47:33 -070044static void populate_cache(GrGpu* gpu, int resourceCount, int keyData32Count) {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000045 for (int i = 0; i < resourceCount; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -080046 GrUniqueKey key;
kkinnunen54b85112015-05-18 22:47:33 -070047 BenchResource::ComputeKey(i, keyData32Count, &key);
halcanary385fe4d2015-08-26 13:07:48 -070048 GrGpuResource* resource = new BenchResource(gpu);
bsalomon8718aaf2015-02-19 07:24:21 -080049 resource->resourcePriv().setUniqueKey(key);
bsalomon19cd0f12014-11-24 12:19:05 -080050 resource->unref();
51 }
bsalomon19cd0f12014-11-24 12:19:05 -080052}
53
tfarinaf168b862014-06-19 12:32:29 -070054class GrResourceCacheBenchAdd : public Benchmark {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000055public:
kkinnunen54b85112015-05-18 22:47:33 -070056 GrResourceCacheBenchAdd(int keyData32Count)
57 : fFullName("grresourcecache_add")
58 , fKeyData32Count(keyData32Count) {
59 if (keyData32Count > 1) {
60 fFullName.appendf("_%d", fKeyData32Count);
61 }
62 }
63
mtklein36352bf2015-03-25 18:17:31 -070064 bool isSuitableFor(Backend backend) override {
bsalomon8b79d232014-11-10 10:19:06 -080065 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000066 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000067protected:
mtklein36352bf2015-03-25 18:17:31 -070068 const char* onGetName() override {
kkinnunen54b85112015-05-18 22:47:33 -070069 return fFullName.c_str();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000070 }
71
mtkleina1ebeb22015-10-01 09:43:39 -070072 void onDraw(int loops, SkCanvas* canvas) override {
bsalomon8b79d232014-11-10 10:19:06 -080073 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
halcanary96fcdcc2015-08-27 07:41:13 -070074 if (nullptr == context) {
bsalomon8b79d232014-11-10 10:19:06 -080075 return;
76 }
77 // Set the cache budget to be very large so no purging occurs.
bsalomon10e23ca2014-11-25 05:52:06 -080078 context->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
bsalomon8b79d232014-11-10 10:19:06 -080079
bsalomon0ea80f42015-02-11 10:49:59 -080080 GrResourceCache* cache = context->getResourceCache();
bsalomon8b79d232014-11-10 10:19:06 -080081
82 // Make sure the cache is empty.
bsalomon0ea80f42015-02-11 10:49:59 -080083 cache->purgeAllUnlocked();
84 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -080085
86 GrGpu* gpu = context->getGpu();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000087
commit-bot@chromium.org33614712013-12-03 18:17:16 +000088 for (int i = 0; i < loops; ++i) {
kkinnunen54b85112015-05-18 22:47:33 -070089 populate_cache(gpu, CACHE_SIZE_COUNT, fKeyData32Count);
bsalomon0ea80f42015-02-11 10:49:59 -080090 SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount());
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000091 }
92 }
93
94private:
kkinnunen54b85112015-05-18 22:47:33 -070095 SkString fFullName;
96 int fKeyData32Count;
tfarinaf168b862014-06-19 12:32:29 -070097 typedef Benchmark INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000098};
99
tfarinaf168b862014-06-19 12:32:29 -0700100class GrResourceCacheBenchFind : public Benchmark {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000101public:
kkinnunen54b85112015-05-18 22:47:33 -0700102 GrResourceCacheBenchFind(int keyData32Count)
103 : fFullName("grresourcecache_find")
104 , fKeyData32Count(keyData32Count) {
105 if (keyData32Count > 1) {
106 fFullName.appendf("_%d", fKeyData32Count);
107 }
108 }
109
mtklein36352bf2015-03-25 18:17:31 -0700110 bool isSuitableFor(Backend backend) override {
bsalomon8b79d232014-11-10 10:19:06 -0800111 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000112 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000113protected:
mtklein36352bf2015-03-25 18:17:31 -0700114 const char* onGetName() override {
kkinnunen54b85112015-05-18 22:47:33 -0700115 return fFullName.c_str();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000116 }
117
joshualitt8a6697a2015-09-30 12:11:07 -0700118 void onDelayedSetup() override {
bsalomon10e23ca2014-11-25 05:52:06 -0800119 fContext.reset(GrContext::CreateMockContext());
120 if (!fContext) {
bsalomon8b79d232014-11-10 10:19:06 -0800121 return;
122 }
123 // Set the cache budget to be very large so no purging occurs.
bsalomon10e23ca2014-11-25 05:52:06 -0800124 fContext->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
bsalomon8b79d232014-11-10 10:19:06 -0800125
bsalomon0ea80f42015-02-11 10:49:59 -0800126 GrResourceCache* cache = fContext->getResourceCache();
bsalomon8b79d232014-11-10 10:19:06 -0800127
128 // Make sure the cache is empty.
bsalomon0ea80f42015-02-11 10:49:59 -0800129 cache->purgeAllUnlocked();
130 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800131
bsalomon10e23ca2014-11-25 05:52:06 -0800132 GrGpu* gpu = fContext->getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800133
kkinnunen54b85112015-05-18 22:47:33 -0700134 populate_cache(gpu, CACHE_SIZE_COUNT, fKeyData32Count);
bsalomon10e23ca2014-11-25 05:52:06 -0800135 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000136
mtkleina1ebeb22015-10-01 09:43:39 -0700137 void onDraw(int loops, SkCanvas* canvas) override {
bsalomon10e23ca2014-11-25 05:52:06 -0800138 if (!fContext) {
139 return;
140 }
bsalomon0ea80f42015-02-11 10:49:59 -0800141 GrResourceCache* cache = fContext->getResourceCache();
142 SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount());
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000143 for (int i = 0; i < loops; ++i) {
bsalomon10e23ca2014-11-25 05:52:06 -0800144 for (int k = 0; k < CACHE_SIZE_COUNT; ++k) {
bsalomon8718aaf2015-02-19 07:24:21 -0800145 GrUniqueKey key;
kkinnunen54b85112015-05-18 22:47:33 -0700146 BenchResource::ComputeKey(k, fKeyData32Count, &key);
bsalomon8718aaf2015-02-19 07:24:21 -0800147 SkAutoTUnref<GrGpuResource> resource(cache->findAndRefUniqueResource(key));
bsalomon10e23ca2014-11-25 05:52:06 -0800148 SkASSERT(resource);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000149 }
150 }
151 }
152
153private:
bsalomon10e23ca2014-11-25 05:52:06 -0800154 SkAutoTUnref<GrContext> fContext;
kkinnunen54b85112015-05-18 22:47:33 -0700155 SkString fFullName;
156 int fKeyData32Count;
tfarinaf168b862014-06-19 12:32:29 -0700157 typedef Benchmark INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000158};
159
kkinnunen54b85112015-05-18 22:47:33 -0700160DEF_BENCH( return new GrResourceCacheBenchAdd(1); )
161#ifdef SK_RELEASE
162// Only on release because on debug the SkTDynamicHash validation is too slow.
163DEF_BENCH( return new GrResourceCacheBenchAdd(2); )
164DEF_BENCH( return new GrResourceCacheBenchAdd(3); )
165DEF_BENCH( return new GrResourceCacheBenchAdd(4); )
166DEF_BENCH( return new GrResourceCacheBenchAdd(5); )
167DEF_BENCH( return new GrResourceCacheBenchAdd(10); )
168DEF_BENCH( return new GrResourceCacheBenchAdd(25); )
169DEF_BENCH( return new GrResourceCacheBenchAdd(54); )
170DEF_BENCH( return new GrResourceCacheBenchAdd(55); )
171DEF_BENCH( return new GrResourceCacheBenchAdd(56); )
172#endif
173
174DEF_BENCH( return new GrResourceCacheBenchFind(1); )
175#ifdef SK_RELEASE
176DEF_BENCH( return new GrResourceCacheBenchFind(2); )
177DEF_BENCH( return new GrResourceCacheBenchFind(3); )
178DEF_BENCH( return new GrResourceCacheBenchFind(4); )
179DEF_BENCH( return new GrResourceCacheBenchFind(5); )
180DEF_BENCH( return new GrResourceCacheBenchFind(10); )
181DEF_BENCH( return new GrResourceCacheBenchFind(25); )
182DEF_BENCH( return new GrResourceCacheBenchFind(54); )
183DEF_BENCH( return new GrResourceCacheBenchFind(55); )
184DEF_BENCH( return new GrResourceCacheBenchFind(56); )
185#endif
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000186
187#endif