commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "bench/Benchmark.h" |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/gpu/GrContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrContextPriv.h" |
| 13 | #include "src/gpu/GrGpu.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 14 | #include "src/gpu/GrGpuResource.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/GrGpuResourcePriv.h" |
| 16 | #include "src/gpu/GrResourceCache.h" |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 17 | |
| 18 | enum { |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 19 | CACHE_SIZE_COUNT = 4096, |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 20 | }; |
| 21 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 22 | class BenchResource : public GrGpuResource { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 23 | public: |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 24 | BenchResource (GrGpu* gpu) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 25 | : INHERITED(gpu) { |
| 26 | this->registerWithCache(SkBudgeted::kYes); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 27 | } |
| 28 | |
kkinnunen | 54b8511 | 2015-05-18 22:47:33 -0700 | [diff] [blame] | 29 | static void ComputeKey(int i, int keyData32Count, GrUniqueKey* key) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 30 | static GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
kkinnunen | 54b8511 | 2015-05-18 22:47:33 -0700 | [diff] [blame] | 31 | GrUniqueKey::Builder builder(key, kDomain, keyData32Count); |
| 32 | for (int j = 0; j < keyData32Count; ++j) { |
| 33 | builder[j] = i + j; |
| 34 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 35 | } |
| 36 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 37 | private: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 38 | size_t onGpuMemorySize() const override { return 100; } |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 39 | const char* getResourceType() const override { return "bench"; } |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 40 | typedef GrGpuResource INHERITED; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
kkinnunen | 54b8511 | 2015-05-18 22:47:33 -0700 | [diff] [blame] | 43 | static void populate_cache(GrGpu* gpu, int resourceCount, int keyData32Count) { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 44 | for (int i = 0; i < resourceCount; ++i) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 45 | GrUniqueKey key; |
kkinnunen | 54b8511 | 2015-05-18 22:47:33 -0700 | [diff] [blame] | 46 | BenchResource::ComputeKey(i, keyData32Count, &key); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 47 | GrGpuResource* resource = new BenchResource(gpu); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 48 | resource->resourcePriv().setUniqueKey(key); |
bsalomon | 19cd0f1 | 2014-11-24 12:19:05 -0800 | [diff] [blame] | 49 | resource->unref(); |
| 50 | } |
bsalomon | 19cd0f1 | 2014-11-24 12:19:05 -0800 | [diff] [blame] | 51 | } |
| 52 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 53 | class GrResourceCacheBenchAdd : public Benchmark { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 54 | public: |
kkinnunen | 54b8511 | 2015-05-18 22:47:33 -0700 | [diff] [blame] | 55 | GrResourceCacheBenchAdd(int keyData32Count) |
| 56 | : fFullName("grresourcecache_add") |
| 57 | , fKeyData32Count(keyData32Count) { |
| 58 | if (keyData32Count > 1) { |
| 59 | fFullName.appendf("_%d", fKeyData32Count); |
| 60 | } |
| 61 | } |
| 62 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 63 | bool isSuitableFor(Backend backend) override { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 64 | return backend == kNonRendering_Backend; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 65 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 66 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 67 | const char* onGetName() override { |
kkinnunen | 54b8511 | 2015-05-18 22:47:33 -0700 | [diff] [blame] | 68 | return fFullName.c_str(); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 69 | } |
| 70 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 71 | void onDraw(int loops, SkCanvas* canvas) override { |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 72 | sk_sp<GrContext> context(GrContext::MakeMock(nullptr)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 73 | if (nullptr == context) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 74 | return; |
| 75 | } |
| 76 | // Set the cache budget to be very large so no purging occurs. |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 77 | context->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 78 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 79 | GrResourceCache* cache = context->priv().getResourceCache(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 80 | |
| 81 | // Make sure the cache is empty. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 82 | cache->purgeAllUnlocked(); |
| 83 | SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 84 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 85 | GrGpu* gpu = context->priv().getGpu(); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 86 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 87 | for (int i = 0; i < loops; ++i) { |
kkinnunen | 54b8511 | 2015-05-18 22:47:33 -0700 | [diff] [blame] | 88 | populate_cache(gpu, CACHE_SIZE_COUNT, fKeyData32Count); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 89 | SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount()); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | private: |
kkinnunen | 54b8511 | 2015-05-18 22:47:33 -0700 | [diff] [blame] | 94 | SkString fFullName; |
| 95 | int fKeyData32Count; |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 96 | typedef Benchmark INHERITED; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 99 | class GrResourceCacheBenchFind : public Benchmark { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 100 | public: |
kkinnunen | 54b8511 | 2015-05-18 22:47:33 -0700 | [diff] [blame] | 101 | GrResourceCacheBenchFind(int keyData32Count) |
| 102 | : fFullName("grresourcecache_find") |
| 103 | , fKeyData32Count(keyData32Count) { |
| 104 | if (keyData32Count > 1) { |
| 105 | fFullName.appendf("_%d", fKeyData32Count); |
| 106 | } |
| 107 | } |
| 108 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 109 | bool isSuitableFor(Backend backend) override { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 110 | return backend == kNonRendering_Backend; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 111 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 112 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 113 | const char* onGetName() override { |
kkinnunen | 54b8511 | 2015-05-18 22:47:33 -0700 | [diff] [blame] | 114 | return fFullName.c_str(); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 115 | } |
| 116 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 117 | void onDelayedSetup() override { |
Greg Daniel | 02611d9 | 2017-07-25 10:05:01 -0400 | [diff] [blame] | 118 | fContext = GrContext::MakeMock(nullptr); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 119 | if (!fContext) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 120 | return; |
| 121 | } |
| 122 | // Set the cache budget to be very large so no purging occurs. |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 123 | fContext->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 124 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 125 | GrResourceCache* cache = fContext->priv().getResourceCache(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 126 | |
| 127 | // Make sure the cache is empty. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 128 | cache->purgeAllUnlocked(); |
| 129 | SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 130 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 131 | GrGpu* gpu = fContext->priv().getGpu(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 132 | |
kkinnunen | 54b8511 | 2015-05-18 22:47:33 -0700 | [diff] [blame] | 133 | populate_cache(gpu, CACHE_SIZE_COUNT, fKeyData32Count); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 134 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 135 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 136 | void onDraw(int loops, SkCanvas* canvas) override { |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 137 | if (!fContext) { |
| 138 | return; |
| 139 | } |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 140 | GrResourceCache* cache = fContext->priv().getResourceCache(); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 141 | SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount()); |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 142 | for (int i = 0; i < loops; ++i) { |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 143 | for (int k = 0; k < CACHE_SIZE_COUNT; ++k) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 144 | GrUniqueKey key; |
kkinnunen | 54b8511 | 2015-05-18 22:47:33 -0700 | [diff] [blame] | 145 | BenchResource::ComputeKey(k, fKeyData32Count, &key); |
Hal Canary | 2db8361 | 2016-11-04 13:02:54 -0400 | [diff] [blame] | 146 | sk_sp<GrGpuResource> resource(cache->findAndRefUniqueResource(key)); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 147 | SkASSERT(resource); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | private: |
Hal Canary | 2db8361 | 2016-11-04 13:02:54 -0400 | [diff] [blame] | 153 | sk_sp<GrContext> fContext; |
kkinnunen | 54b8511 | 2015-05-18 22:47:33 -0700 | [diff] [blame] | 154 | SkString fFullName; |
| 155 | int fKeyData32Count; |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 156 | typedef Benchmark INHERITED; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 157 | }; |
| 158 | |
kkinnunen | 54b8511 | 2015-05-18 22:47:33 -0700 | [diff] [blame] | 159 | DEF_BENCH( return new GrResourceCacheBenchAdd(1); ) |
| 160 | #ifdef SK_RELEASE |
| 161 | // Only on release because on debug the SkTDynamicHash validation is too slow. |
| 162 | DEF_BENCH( return new GrResourceCacheBenchAdd(2); ) |
| 163 | DEF_BENCH( return new GrResourceCacheBenchAdd(3); ) |
| 164 | DEF_BENCH( return new GrResourceCacheBenchAdd(4); ) |
| 165 | DEF_BENCH( return new GrResourceCacheBenchAdd(5); ) |
| 166 | DEF_BENCH( return new GrResourceCacheBenchAdd(10); ) |
| 167 | DEF_BENCH( return new GrResourceCacheBenchAdd(25); ) |
| 168 | DEF_BENCH( return new GrResourceCacheBenchAdd(54); ) |
| 169 | DEF_BENCH( return new GrResourceCacheBenchAdd(55); ) |
| 170 | DEF_BENCH( return new GrResourceCacheBenchAdd(56); ) |
| 171 | #endif |
| 172 | |
| 173 | DEF_BENCH( return new GrResourceCacheBenchFind(1); ) |
| 174 | #ifdef SK_RELEASE |
| 175 | DEF_BENCH( return new GrResourceCacheBenchFind(2); ) |
| 176 | DEF_BENCH( return new GrResourceCacheBenchFind(3); ) |
| 177 | DEF_BENCH( return new GrResourceCacheBenchFind(4); ) |
| 178 | DEF_BENCH( return new GrResourceCacheBenchFind(5); ) |
| 179 | DEF_BENCH( return new GrResourceCacheBenchFind(10); ) |
| 180 | DEF_BENCH( return new GrResourceCacheBenchFind(25); ) |
| 181 | DEF_BENCH( return new GrResourceCacheBenchFind(54); ) |
| 182 | DEF_BENCH( return new GrResourceCacheBenchFind(55); ) |
| 183 | DEF_BENCH( return new GrResourceCacheBenchFind(56); ) |
| 184 | #endif |