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 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 8 | #include "Benchmark.h" |
| 9 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 10 | #if SK_SUPPORT_GPU |
| 11 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 12 | #include "GrGpuResource.h" |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 13 | #include "GrGpuResourcePriv.h" |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 14 | #include "GrContext.h" |
bsalomon | bcf0a52 | 2014-10-08 08:40:09 -0700 | [diff] [blame] | 15 | #include "GrGpu.h" |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 16 | #include "GrResourceCache.h" |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 17 | #include "SkCanvas.h" |
| 18 | |
| 19 | enum { |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 20 | CACHE_SIZE_COUNT = 4096, |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 21 | }; |
| 22 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 23 | class BenchResource : public GrGpuResource { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 24 | public: |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 25 | BenchResource (GrGpu* gpu) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 26 | : INHERITED(gpu) { |
| 27 | this->registerWithCache(SkBudgeted::kYes); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 28 | } |
| 29 | |
kkinnunen | 54b8511 | 2015-05-18 22:47:33 -0700 | [diff] [blame] | 30 | static void ComputeKey(int i, int keyData32Count, GrUniqueKey* key) { |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 31 | static GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
kkinnunen | 54b8511 | 2015-05-18 22:47:33 -0700 | [diff] [blame] | 32 | GrUniqueKey::Builder builder(key, kDomain, keyData32Count); |
| 33 | for (int j = 0; j < keyData32Count; ++j) { |
| 34 | builder[j] = i + j; |
| 35 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 36 | } |
| 37 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 38 | private: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 39 | size_t onGpuMemorySize() const override { return 100; } |
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 { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 72 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
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 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 79 | GrResourceCache* cache = context->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 | |
| 85 | GrGpu* gpu = context->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 { |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 118 | fContext.reset(GrContext::CreateMockContext()); |
| 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 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 125 | GrResourceCache* cache = fContext->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 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 131 | GrGpu* gpu = fContext->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 | } |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 140 | GrResourceCache* cache = fContext->getResourceCache(); |
| 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); |
bsalomon | 8718aaf | 2015-02-19 07:24:21 -0800 | [diff] [blame] | 146 | SkAutoTUnref<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: |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 153 | SkAutoTUnref<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 |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 185 | |
| 186 | #endif |