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