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 | SK_DECLARE_INST_COUNT(BenchResource); |
| 27 | BenchResource (GrGpu* gpu) |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 28 | : INHERITED(gpu, kCached_LifeCycle) { |
bsalomon | 1696126 | 2014-08-26 14:01:07 -0700 | [diff] [blame] | 29 | this->registerWithCache(); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 30 | } |
| 31 | |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 32 | static void ComputeKey(int i, GrContentKey* key) { |
| 33 | static GrContentKey::Domain kDomain = GrContentKey::GenerateDomain(); |
| 34 | GrContentKey::Builder builder(key, kDomain, 1); |
| 35 | builder[0] = i; |
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: |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 39 | size_t onGpuMemorySize() const SK_OVERRIDE { return 100; } |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 40 | |
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 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 44 | static void populate_cache(GrGpu* gpu, int resourceCount) { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 45 | for (int i = 0; i < resourceCount; ++i) { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 46 | GrContentKey key; |
| 47 | BenchResource::ComputeKey(i, &key); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 48 | GrGpuResource* resource = SkNEW_ARGS(BenchResource, (gpu)); |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 49 | resource->resourcePriv().setContentKey(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: |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 56 | bool isSuitableFor(Backend backend) SK_OVERRIDE { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 57 | return backend == kNonRendering_Backend; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | protected: |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 61 | const char* onGetName() SK_OVERRIDE { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 62 | return "grresourcecache_add"; |
| 63 | } |
| 64 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 65 | void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 66 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 67 | if (NULL == context) { |
| 68 | return; |
| 69 | } |
| 70 | // Set the cache budget to be very large so no purging occurs. |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 71 | context->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 72 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 73 | GrResourceCache* cache = context->getResourceCache(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 74 | |
| 75 | // Make sure the cache is empty. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 76 | cache->purgeAllUnlocked(); |
| 77 | SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 78 | |
| 79 | GrGpu* gpu = context->getGpu(); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 80 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 81 | for (int i = 0; i < loops; ++i) { |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 82 | populate_cache(gpu, CACHE_SIZE_COUNT); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 83 | SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount()); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| 87 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 88 | typedef Benchmark INHERITED; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 91 | class GrResourceCacheBenchFind : public Benchmark { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 92 | public: |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 93 | bool isSuitableFor(Backend backend) SK_OVERRIDE { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 94 | return backend == kNonRendering_Backend; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | protected: |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 98 | const char* onGetName() SK_OVERRIDE { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 99 | return "grresourcecache_find"; |
| 100 | } |
| 101 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 102 | void onPreDraw() SK_OVERRIDE { |
| 103 | fContext.reset(GrContext::CreateMockContext()); |
| 104 | if (!fContext) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 105 | return; |
| 106 | } |
| 107 | // Set the cache budget to be very large so no purging occurs. |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 108 | fContext->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 109 | |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 110 | GrResourceCache* cache = fContext->getResourceCache(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 111 | |
| 112 | // Make sure the cache is empty. |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 113 | cache->purgeAllUnlocked(); |
| 114 | SkASSERT(0 == cache->getResourceCount() && 0 == cache->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 115 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 116 | GrGpu* gpu = fContext->getGpu(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 117 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 118 | populate_cache(gpu, CACHE_SIZE_COUNT); |
| 119 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 120 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 121 | void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
| 122 | if (!fContext) { |
| 123 | return; |
| 124 | } |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 125 | GrResourceCache* cache = fContext->getResourceCache(); |
| 126 | SkASSERT(CACHE_SIZE_COUNT == cache->getResourceCount()); |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 127 | for (int i = 0; i < loops; ++i) { |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 128 | for (int k = 0; k < CACHE_SIZE_COUNT; ++k) { |
bsalomon | 24db3b1 | 2015-01-23 04:24:04 -0800 | [diff] [blame] | 129 | GrContentKey key; |
| 130 | BenchResource::ComputeKey(k, &key); |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 131 | SkAutoTUnref<GrGpuResource> resource(cache->findAndRefContentResource(key)); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 132 | SkASSERT(resource); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | private: |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 138 | SkAutoTUnref<GrContext> fContext; |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 139 | typedef Benchmark INHERITED; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | DEF_BENCH( return new GrResourceCacheBenchAdd(); ) |
| 143 | DEF_BENCH( return new GrResourceCacheBenchFind(); ) |
| 144 | |
| 145 | #endif |