blob: 58b0ee85d6230f4975c190babc74ed9d5e1e1b4d [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 SK_DECLARE_INST_COUNT(BenchResource);
27 BenchResource (GrGpu* gpu)
bsalomon5236cf42015-01-14 10:42:08 -080028 : INHERITED(gpu, kCached_LifeCycle) {
bsalomon16961262014-08-26 14:01:07 -070029 this->registerWithCache();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000030 }
31
kkinnunen54b85112015-05-18 22:47:33 -070032 static void ComputeKey(int i, int keyData32Count, GrUniqueKey* key) {
bsalomon8718aaf2015-02-19 07:24:21 -080033 static GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
kkinnunen54b85112015-05-18 22:47:33 -070034 GrUniqueKey::Builder builder(key, kDomain, keyData32Count);
35 for (int j = 0; j < keyData32Count; ++j) {
36 builder[j] = i + j;
37 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000038 }
39
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000040private:
mtklein36352bf2015-03-25 18:17:31 -070041 size_t onGpuMemorySize() const override { return 100; }
bsalomon6d3fe022014-07-25 08:35:45 -070042 typedef GrGpuResource INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000043};
44
kkinnunen54b85112015-05-18 22:47:33 -070045static void populate_cache(GrGpu* gpu, int resourceCount, int keyData32Count) {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000046 for (int i = 0; i < resourceCount; ++i) {
bsalomon8718aaf2015-02-19 07:24:21 -080047 GrUniqueKey key;
kkinnunen54b85112015-05-18 22:47:33 -070048 BenchResource::ComputeKey(i, keyData32Count, &key);
bsalomon10e23ca2014-11-25 05:52:06 -080049 GrGpuResource* resource = SkNEW_ARGS(BenchResource, (gpu));
bsalomon8718aaf2015-02-19 07:24:21 -080050 resource->resourcePriv().setUniqueKey(key);
bsalomon19cd0f12014-11-24 12:19:05 -080051 resource->unref();
52 }
bsalomon19cd0f12014-11-24 12:19:05 -080053}
54
tfarinaf168b862014-06-19 12:32:29 -070055class GrResourceCacheBenchAdd : public Benchmark {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000056public:
kkinnunen54b85112015-05-18 22:47:33 -070057 GrResourceCacheBenchAdd(int keyData32Count)
58 : fFullName("grresourcecache_add")
59 , fKeyData32Count(keyData32Count) {
60 if (keyData32Count > 1) {
61 fFullName.appendf("_%d", fKeyData32Count);
62 }
63 }
64
mtklein36352bf2015-03-25 18:17:31 -070065 bool isSuitableFor(Backend backend) override {
bsalomon8b79d232014-11-10 10:19:06 -080066 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000067 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000068protected:
mtklein36352bf2015-03-25 18:17:31 -070069 const char* onGetName() override {
kkinnunen54b85112015-05-18 22:47:33 -070070 return fFullName.c_str();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000071 }
72
mtklein36352bf2015-03-25 18:17:31 -070073 void onDraw(const int loops, SkCanvas* canvas) override {
bsalomon8b79d232014-11-10 10:19:06 -080074 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext());
75 if (NULL == context) {
76 return;
77 }
78 // Set the cache budget to be very large so no purging occurs.
bsalomon10e23ca2014-11-25 05:52:06 -080079 context->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
bsalomon8b79d232014-11-10 10:19:06 -080080
bsalomon0ea80f42015-02-11 10:49:59 -080081 GrResourceCache* cache = context->getResourceCache();
bsalomon8b79d232014-11-10 10:19:06 -080082
83 // Make sure the cache is empty.
bsalomon0ea80f42015-02-11 10:49:59 -080084 cache->purgeAllUnlocked();
85 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -080086
87 GrGpu* gpu = context->getGpu();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000088
commit-bot@chromium.org33614712013-12-03 18:17:16 +000089 for (int i = 0; i < loops; ++i) {
kkinnunen54b85112015-05-18 22:47:33 -070090 populate_cache(gpu, CACHE_SIZE_COUNT, fKeyData32Count);
bsalomon0ea80f42015-02-11 10:49:59 -080091 SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount());
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000092 }
93 }
94
95private:
kkinnunen54b85112015-05-18 22:47:33 -070096 SkString fFullName;
97 int fKeyData32Count;
tfarinaf168b862014-06-19 12:32:29 -070098 typedef Benchmark INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000099};
100
tfarinaf168b862014-06-19 12:32:29 -0700101class GrResourceCacheBenchFind : public Benchmark {
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000102public:
kkinnunen54b85112015-05-18 22:47:33 -0700103 GrResourceCacheBenchFind(int keyData32Count)
104 : fFullName("grresourcecache_find")
105 , fKeyData32Count(keyData32Count) {
106 if (keyData32Count > 1) {
107 fFullName.appendf("_%d", fKeyData32Count);
108 }
109 }
110
mtklein36352bf2015-03-25 18:17:31 -0700111 bool isSuitableFor(Backend backend) override {
bsalomon8b79d232014-11-10 10:19:06 -0800112 return backend == kNonRendering_Backend;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000113 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000114protected:
mtklein36352bf2015-03-25 18:17:31 -0700115 const char* onGetName() override {
kkinnunen54b85112015-05-18 22:47:33 -0700116 return fFullName.c_str();
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000117 }
118
mtklein36352bf2015-03-25 18:17:31 -0700119 void onPreDraw() override {
bsalomon10e23ca2014-11-25 05:52:06 -0800120 fContext.reset(GrContext::CreateMockContext());
121 if (!fContext) {
bsalomon8b79d232014-11-10 10:19:06 -0800122 return;
123 }
124 // Set the cache budget to be very large so no purging occurs.
bsalomon10e23ca2014-11-25 05:52:06 -0800125 fContext->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30);
bsalomon8b79d232014-11-10 10:19:06 -0800126
bsalomon0ea80f42015-02-11 10:49:59 -0800127 GrResourceCache* cache = fContext->getResourceCache();
bsalomon8b79d232014-11-10 10:19:06 -0800128
129 // Make sure the cache is empty.
bsalomon0ea80f42015-02-11 10:49:59 -0800130 cache->purgeAllUnlocked();
131 SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes());
bsalomon8b79d232014-11-10 10:19:06 -0800132
bsalomon10e23ca2014-11-25 05:52:06 -0800133 GrGpu* gpu = fContext->getGpu();
bsalomon8b79d232014-11-10 10:19:06 -0800134
kkinnunen54b85112015-05-18 22:47:33 -0700135 populate_cache(gpu, CACHE_SIZE_COUNT, fKeyData32Count);
bsalomon10e23ca2014-11-25 05:52:06 -0800136 }
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000137
mtklein36352bf2015-03-25 18:17:31 -0700138 void onDraw(const int loops, SkCanvas* canvas) override {
bsalomon10e23ca2014-11-25 05:52:06 -0800139 if (!fContext) {
140 return;
141 }
bsalomon0ea80f42015-02-11 10:49:59 -0800142 GrResourceCache* cache = fContext->getResourceCache();
143 SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount());
commit-bot@chromium.org33614712013-12-03 18:17:16 +0000144 for (int i = 0; i < loops; ++i) {
bsalomon10e23ca2014-11-25 05:52:06 -0800145 for (int k = 0; k < CACHE_SIZE_COUNT; ++k) {
bsalomon8718aaf2015-02-19 07:24:21 -0800146 GrUniqueKey key;
kkinnunen54b85112015-05-18 22:47:33 -0700147 BenchResource::ComputeKey(k, fKeyData32Count, &key);
bsalomon8718aaf2015-02-19 07:24:21 -0800148 SkAutoTUnref<GrGpuResource> resource(cache->findAndRefUniqueResource(key));
bsalomon10e23ca2014-11-25 05:52:06 -0800149 SkASSERT(resource);
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000150 }
151 }
152 }
153
154private:
bsalomon10e23ca2014-11-25 05:52:06 -0800155 SkAutoTUnref<GrContext> fContext;
kkinnunen54b85112015-05-18 22:47:33 -0700156 SkString fFullName;
157 int fKeyData32Count;
tfarinaf168b862014-06-19 12:32:29 -0700158 typedef Benchmark INHERITED;
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000159};
160
kkinnunen54b85112015-05-18 22:47:33 -0700161DEF_BENCH( return new GrResourceCacheBenchAdd(1); )
162#ifdef SK_RELEASE
163// Only on release because on debug the SkTDynamicHash validation is too slow.
164DEF_BENCH( return new GrResourceCacheBenchAdd(2); )
165DEF_BENCH( return new GrResourceCacheBenchAdd(3); )
166DEF_BENCH( return new GrResourceCacheBenchAdd(4); )
167DEF_BENCH( return new GrResourceCacheBenchAdd(5); )
168DEF_BENCH( return new GrResourceCacheBenchAdd(10); )
169DEF_BENCH( return new GrResourceCacheBenchAdd(25); )
170DEF_BENCH( return new GrResourceCacheBenchAdd(54); )
171DEF_BENCH( return new GrResourceCacheBenchAdd(55); )
172DEF_BENCH( return new GrResourceCacheBenchAdd(56); )
173#endif
174
175DEF_BENCH( return new GrResourceCacheBenchFind(1); )
176#ifdef SK_RELEASE
177DEF_BENCH( return new GrResourceCacheBenchFind(2); )
178DEF_BENCH( return new GrResourceCacheBenchFind(3); )
179DEF_BENCH( return new GrResourceCacheBenchFind(4); )
180DEF_BENCH( return new GrResourceCacheBenchFind(5); )
181DEF_BENCH( return new GrResourceCacheBenchFind(10); )
182DEF_BENCH( return new GrResourceCacheBenchFind(25); )
183DEF_BENCH( return new GrResourceCacheBenchFind(54); )
184DEF_BENCH( return new GrResourceCacheBenchFind(55); )
185DEF_BENCH( return new GrResourceCacheBenchFind(56); )
186#endif
commit-bot@chromium.org644629c2013-11-21 06:21:58 +0000187
188#endif