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