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 | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 16 | #include "GrResourceCache2.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 | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 31 | static GrResourceKey ComputeKey(int i) { |
bsalomon | 91175f1 | 2014-11-24 07:05:15 -0800 | [diff] [blame] | 32 | GrCacheID::Key key; |
| 33 | memset(&key, 0, sizeof(key)); |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 34 | key.fData32[0] = i; |
bsalomon | 91175f1 | 2014-11-24 07:05:15 -0800 | [diff] [blame] | 35 | static int gDomain = GrCacheID::GenerateDomain(); |
bsalomon | 7775c85 | 2014-12-30 12:50:52 -0800 | [diff] [blame] | 36 | return GrResourceKey(GrCacheID(gDomain, key), 0); |
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 | |
| 40 | private: |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 41 | size_t onGpuMemorySize() const SK_OVERRIDE { return 100; } |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 42 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 43 | typedef GrGpuResource INHERITED; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 46 | static void populate_cache(GrGpu* gpu, int resourceCount) { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 47 | for (int i = 0; i < resourceCount; ++i) { |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 48 | GrResourceKey key = BenchResource::ComputeKey(i); |
| 49 | GrGpuResource* resource = SkNEW_ARGS(BenchResource, (gpu)); |
bsalomon | 19cd0f1 | 2014-11-24 12:19:05 -0800 | [diff] [blame] | 50 | resource->cacheAccess().setContentKey(key); |
| 51 | resource->unref(); |
| 52 | } |
bsalomon | 19cd0f1 | 2014-11-24 12:19:05 -0800 | [diff] [blame] | 53 | } |
| 54 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 55 | class GrResourceCacheBenchAdd : public Benchmark { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 56 | public: |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 57 | bool isSuitableFor(Backend backend) SK_OVERRIDE { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 58 | return backend == kNonRendering_Backend; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | protected: |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 62 | const char* onGetName() SK_OVERRIDE { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 63 | return "grresourcecache_add"; |
| 64 | } |
| 65 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 66 | void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 67 | SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
| 68 | if (NULL == context) { |
| 69 | return; |
| 70 | } |
| 71 | // Set the cache budget to be very large so no purging occurs. |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 72 | context->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 73 | |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 74 | GrResourceCache2* cache2 = context->getResourceCache2(); |
| 75 | |
| 76 | // Make sure the cache is empty. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 77 | cache2->purgeAllUnlocked(); |
| 78 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 79 | |
| 80 | GrGpu* gpu = context->getGpu(); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 81 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 82 | for (int i = 0; i < loops; ++i) { |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 83 | populate_cache(gpu, CACHE_SIZE_COUNT); |
| 84 | SkASSERT(CACHE_SIZE_COUNT == cache2->getResourceCount()); |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 89 | typedef Benchmark INHERITED; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 92 | class GrResourceCacheBenchFind : public Benchmark { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 93 | public: |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 94 | bool isSuitableFor(Backend backend) SK_OVERRIDE { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 95 | return backend == kNonRendering_Backend; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | protected: |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 99 | const char* onGetName() SK_OVERRIDE { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 100 | return "grresourcecache_find"; |
| 101 | } |
| 102 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 103 | void onPreDraw() SK_OVERRIDE { |
| 104 | fContext.reset(GrContext::CreateMockContext()); |
| 105 | if (!fContext) { |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 106 | return; |
| 107 | } |
| 108 | // Set the cache budget to be very large so no purging occurs. |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 109 | fContext->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 110 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 111 | GrResourceCache2* cache2 = fContext->getResourceCache2(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 112 | |
| 113 | // Make sure the cache is empty. |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 114 | cache2->purgeAllUnlocked(); |
| 115 | SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceBytes()); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 116 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 117 | GrGpu* gpu = fContext->getGpu(); |
bsalomon | 8b79d23 | 2014-11-10 10:19:06 -0800 | [diff] [blame] | 118 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 119 | populate_cache(gpu, CACHE_SIZE_COUNT); |
| 120 | } |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 121 | |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 122 | void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
| 123 | if (!fContext) { |
| 124 | return; |
| 125 | } |
| 126 | GrResourceCache2* cache2 = fContext->getResourceCache2(); |
| 127 | SkASSERT(CACHE_SIZE_COUNT == cache2->getResourceCount()); |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 128 | for (int i = 0; i < loops; ++i) { |
bsalomon | 10e23ca | 2014-11-25 05:52:06 -0800 | [diff] [blame] | 129 | for (int k = 0; k < CACHE_SIZE_COUNT; ++k) { |
| 130 | GrResourceKey key = BenchResource::ComputeKey(k); |
| 131 | SkAutoTUnref<GrGpuResource> resource(cache2->findAndRefContentResource(key)); |
| 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 |