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