OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2013 Google Inc. | 3 * Copyright 2013 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "Benchmark.h" | 9 #include "Benchmark.h" |
10 | 10 |
11 #if SK_SUPPORT_GPU | 11 #if SK_SUPPORT_GPU |
12 | 12 |
13 #include "GrGpuResource.h" | 13 #include "GrGpuResource.h" |
14 #include "GrContext.h" | 14 #include "GrContext.h" |
15 #include "GrGpu.h" | 15 #include "GrGpu.h" |
16 #include "GrResourceCache2.h" | 16 #include "GrResourceCache2.h" |
| 17 #include "GrStencilBuffer.h" |
| 18 #include "GrTexture.h" |
| 19 #include "GrTexturePriv.h" |
17 #include "SkCanvas.h" | 20 #include "SkCanvas.h" |
18 | 21 |
19 enum { | 22 enum { |
20 CACHE_SIZE_COUNT = 4096, | 23 CACHE_SIZE_COUNT = 2048, |
| 24 CACHE_SIZE_BYTES = 2 * 1024 * 1024, |
21 }; | 25 }; |
22 | 26 |
23 class BenchResource : public GrGpuResource { | 27 class StencilResource : public GrGpuResource { |
24 public: | 28 public: |
25 SK_DECLARE_INST_COUNT(BenchResource); | 29 SK_DECLARE_INST_COUNT(StencilResource); |
26 BenchResource (GrGpu* gpu) | 30 StencilResource(GrGpu* gpu, int id) |
27 : INHERITED(gpu, false) { | 31 : INHERITED(gpu, false) |
| 32 , fID(id) { |
28 this->registerWithCache(); | 33 this->registerWithCache(); |
29 } | 34 } |
30 | 35 |
31 static GrResourceKey ComputeKey(int i) { | 36 static GrResourceKey ComputeKey(int width, int height, int sampleCnt) { |
| 37 return GrStencilBuffer::ComputeKey(width, height, sampleCnt); |
| 38 } |
| 39 |
| 40 int fID; |
| 41 |
| 42 private: |
| 43 virtual size_t onGpuMemorySize() const SK_OVERRIDE { |
| 44 return 100 + ((fID % 1 == 0) ? -5 : 6); |
| 45 } |
| 46 |
| 47 typedef GrGpuResource INHERITED; |
| 48 }; |
| 49 |
| 50 class TextureResource : public GrGpuResource { |
| 51 public: |
| 52 SK_DECLARE_INST_COUNT(TextureResource); |
| 53 TextureResource(GrGpu* gpu, int id) |
| 54 : INHERITED(gpu, false) |
| 55 , fID(id) { |
| 56 this->registerWithCache(); |
| 57 } |
| 58 |
| 59 static GrResourceKey ComputeKey(const GrSurfaceDesc& desc) { |
32 GrCacheID::Key key; | 60 GrCacheID::Key key; |
33 memset(&key, 0, sizeof(key)); | 61 memset(&key, 0, sizeof(key)); |
34 key.fData32[0] = i; | 62 key.fData32[0] = (desc.fWidth) | (desc.fHeight << 16); |
| 63 key.fData32[1] = desc.fConfig | desc.fSampleCnt << 16; |
| 64 key.fData32[2] = desc.fFlags; |
35 static int gType = GrResourceKey::GenerateResourceType(); | 65 static int gType = GrResourceKey::GenerateResourceType(); |
36 static int gDomain = GrCacheID::GenerateDomain(); | 66 static int gDomain = GrCacheID::GenerateDomain(); |
37 return GrResourceKey(GrCacheID(gDomain, key), gType, 0); | 67 return GrResourceKey(GrCacheID(gDomain, key), gType, 0); |
38 } | 68 } |
39 | 69 |
| 70 int fID; |
40 | 71 |
41 private: | 72 private: |
42 size_t onGpuMemorySize() const SK_OVERRIDE { return 100; } | 73 virtual size_t onGpuMemorySize() const SK_OVERRIDE { |
| 74 return 100 + ((fID % 1 == 0) ? -40 : 33); |
| 75 } |
43 | 76 |
44 typedef GrGpuResource INHERITED; | 77 typedef GrGpuResource INHERITED; |
45 }; | 78 }; |
46 | 79 |
| 80 static void get_stencil(int i, int* w, int* h, int* s) { |
| 81 *w = i % 1024; |
| 82 *h = i * 2 % 1024; |
| 83 *s = i % 1 == 0 ? 0 : 4; |
| 84 } |
| 85 |
| 86 static void get_texture_desc(int i, GrSurfaceDesc* desc) { |
| 87 desc->fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag; |
| 88 desc->fWidth = i % 1024; |
| 89 desc->fHeight = i * 2 % 1024; |
| 90 desc->fConfig = static_cast<GrPixelConfig>(i % (kLast_GrPixelConfig + 1)); |
| 91 desc->fSampleCnt = ((i % 2) == 0) ? 0 : 4; |
| 92 } |
| 93 |
47 static void populate_cache(GrGpu* gpu, int resourceCount) { | 94 static void populate_cache(GrGpu* gpu, int resourceCount) { |
48 for (int i = 0; i < resourceCount; ++i) { | 95 for (int i = 0; i < resourceCount; ++i) { |
49 GrResourceKey key = BenchResource::ComputeKey(i); | 96 int w, h, s; |
50 GrGpuResource* resource = SkNEW_ARGS(BenchResource, (gpu)); | 97 get_stencil(i, &w, &h, &s); |
| 98 GrResourceKey key = GrStencilBuffer::ComputeKey(w, h, s); |
| 99 GrGpuResource* resource = SkNEW_ARGS(StencilResource, (gpu, i)); |
| 100 resource->cacheAccess().setContentKey(key); |
| 101 resource->unref(); |
| 102 } |
| 103 |
| 104 for (int i = 0; i < resourceCount; ++i) { |
| 105 GrSurfaceDesc desc; |
| 106 get_texture_desc(i, &desc); |
| 107 GrResourceKey key = TextureResource::ComputeKey(desc); |
| 108 GrGpuResource* resource = SkNEW_ARGS(TextureResource, (gpu, i)); |
51 resource->cacheAccess().setContentKey(key); | 109 resource->cacheAccess().setContentKey(key); |
52 resource->unref(); | 110 resource->unref(); |
53 } | 111 } |
54 } | 112 } |
55 | 113 |
| 114 static void check_cache_contents_or_die(GrResourceCache2* cache, int k) { |
| 115 // Benchmark find calls that succeed. |
| 116 { |
| 117 GrSurfaceDesc desc; |
| 118 get_texture_desc(k, &desc); |
| 119 GrResourceKey key = TextureResource::ComputeKey(desc); |
| 120 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key)); |
| 121 if (!item) { |
| 122 SkFAIL("cache add does not work as expected"); |
| 123 return; |
| 124 } |
| 125 if (static_cast<TextureResource*>(item.get())->fID != k) { |
| 126 SkFAIL("cache add does not work as expected"); |
| 127 return; |
| 128 } |
| 129 } |
| 130 { |
| 131 int w, h, s; |
| 132 get_stencil(k, &w, &h, &s); |
| 133 GrResourceKey key = StencilResource::ComputeKey(w, h, s); |
| 134 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key)); |
| 135 if (!item) { |
| 136 SkFAIL("cache add does not work as expected"); |
| 137 return; |
| 138 } |
| 139 if (static_cast<TextureResource*>(item.get())->fID != k) { |
| 140 SkFAIL("cache add does not work as expected"); |
| 141 return; |
| 142 } |
| 143 } |
| 144 |
| 145 // Benchmark also find calls that always fail. |
| 146 { |
| 147 GrSurfaceDesc desc; |
| 148 get_texture_desc(k, &desc); |
| 149 desc.fHeight |= 1; |
| 150 GrResourceKey key = TextureResource::ComputeKey(desc); |
| 151 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key)); |
| 152 if (item) { |
| 153 SkFAIL("cache add does not work as expected"); |
| 154 return; |
| 155 } |
| 156 } |
| 157 { |
| 158 int w, h, s; |
| 159 get_stencil(k, &w, &h, &s); |
| 160 h |= 1; |
| 161 GrResourceKey key = StencilResource::ComputeKey(w, h, s); |
| 162 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key)); |
| 163 if (item) { |
| 164 SkFAIL("cache add does not work as expected"); |
| 165 return; |
| 166 } |
| 167 } |
| 168 } |
| 169 |
56 class GrResourceCacheBenchAdd : public Benchmark { | 170 class GrResourceCacheBenchAdd : public Benchmark { |
| 171 enum { |
| 172 RESOURCE_COUNT = CACHE_SIZE_COUNT / 2, |
| 173 }; |
| 174 |
57 public: | 175 public: |
58 bool isSuitableFor(Backend backend) SK_OVERRIDE { | 176 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
59 return backend == kNonRendering_Backend; | 177 return backend == kNonRendering_Backend; |
60 } | 178 } |
61 | 179 |
62 protected: | 180 protected: |
63 const char* onGetName() SK_OVERRIDE { | 181 virtual const char* onGetName() SK_OVERRIDE { |
64 return "grresourcecache_add"; | 182 return "grresourcecache_add"; |
65 } | 183 } |
66 | 184 |
67 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { | 185 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
68 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 186 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
69 if (NULL == context) { | 187 if (NULL == context) { |
70 return; | 188 return; |
71 } | 189 } |
72 // Set the cache budget to be very large so no purging occurs. | 190 // Set the cache budget to be very large so no purging occurs. |
73 context->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30); | 191 context->setResourceCacheLimits(2 * RESOURCE_COUNT, 1 << 30); |
74 | 192 |
75 GrResourceCache2* cache2 = context->getResourceCache2(); | 193 GrResourceCache2* cache2 = context->getResourceCache2(); |
76 | 194 |
77 // Make sure the cache is empty. | 195 // Make sure the cache is empty. |
78 cache2->purgeAllUnlocked(); | 196 cache2->purgeAllUnlocked(); |
79 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceByte
s()); | 197 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceByte
s()); |
80 | 198 |
81 GrGpu* gpu = context->getGpu(); | 199 GrGpu* gpu = context->getGpu(); |
82 | 200 |
83 for (int i = 0; i < loops; ++i) { | 201 for (int i = 0; i < loops; ++i) { |
84 populate_cache(gpu, CACHE_SIZE_COUNT); | 202 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResource
Bytes()); |
85 SkASSERT(CACHE_SIZE_COUNT == cache2->getResourceCount()); | 203 |
| 204 populate_cache(gpu, RESOURCE_COUNT); |
| 205 |
| 206 // Check that cache works. |
| 207 for (int k = 0; k < RESOURCE_COUNT; k += 33) { |
| 208 check_cache_contents_or_die(cache2, k); |
| 209 } |
| 210 cache2->purgeAllUnlocked(); |
86 } | 211 } |
87 } | 212 } |
88 | 213 |
89 private: | 214 private: |
90 typedef Benchmark INHERITED; | 215 typedef Benchmark INHERITED; |
91 }; | 216 }; |
92 | 217 |
93 class GrResourceCacheBenchFind : public Benchmark { | 218 class GrResourceCacheBenchFind : public Benchmark { |
| 219 enum { |
| 220 RESOURCE_COUNT = CACHE_SIZE_COUNT / 2, |
| 221 }; |
| 222 |
94 public: | 223 public: |
95 bool isSuitableFor(Backend backend) SK_OVERRIDE { | 224 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
96 return backend == kNonRendering_Backend; | 225 return backend == kNonRendering_Backend; |
97 } | 226 } |
98 | 227 |
99 protected: | 228 protected: |
100 const char* onGetName() SK_OVERRIDE { | 229 virtual const char* onGetName() SK_OVERRIDE { |
101 return "grresourcecache_find"; | 230 return "grresourcecache_find"; |
102 } | 231 } |
103 | 232 |
104 void onPreDraw() SK_OVERRIDE { | 233 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
105 fContext.reset(GrContext::CreateMockContext()); | 234 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
106 if (!fContext) { | 235 if (NULL == context) { |
107 return; | 236 return; |
108 } | 237 } |
109 // Set the cache budget to be very large so no purging occurs. | 238 // Set the cache budget to be very large so no purging occurs. |
110 fContext->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30); | 239 context->setResourceCacheLimits(2 * RESOURCE_COUNT, 1 << 30); |
111 | 240 |
112 GrResourceCache2* cache2 = fContext->getResourceCache2(); | 241 GrResourceCache2* cache2 = context->getResourceCache2(); |
113 | 242 |
114 // Make sure the cache is empty. | 243 // Make sure the cache is empty. |
115 cache2->purgeAllUnlocked(); | 244 cache2->purgeAllUnlocked(); |
116 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceByte
s()); | 245 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceByte
s()); |
117 | 246 |
118 GrGpu* gpu = fContext->getGpu(); | 247 GrGpu* gpu = context->getGpu(); |
119 | 248 |
120 populate_cache(gpu, CACHE_SIZE_COUNT); | 249 populate_cache(gpu, RESOURCE_COUNT); |
121 } | |
122 | 250 |
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()); | |
129 for (int i = 0; i < loops; ++i) { | 251 for (int i = 0; i < loops; ++i) { |
130 for (int k = 0; k < CACHE_SIZE_COUNT; ++k) { | 252 for (int k = 0; k < RESOURCE_COUNT; ++k) { |
131 GrResourceKey key = BenchResource::ComputeKey(k); | 253 check_cache_contents_or_die(cache2, k); |
132 SkAutoTUnref<GrGpuResource> resource(cache2->findAndRefContentRe
source(key)); | |
133 SkASSERT(resource); | |
134 } | 254 } |
135 } | 255 } |
136 } | 256 } |
137 | 257 |
138 private: | 258 private: |
139 SkAutoTUnref<GrContext> fContext; | |
140 typedef Benchmark INHERITED; | 259 typedef Benchmark INHERITED; |
141 }; | 260 }; |
142 | 261 |
143 DEF_BENCH( return new GrResourceCacheBenchAdd(); ) | 262 DEF_BENCH( return new GrResourceCacheBenchAdd(); ) |
144 DEF_BENCH( return new GrResourceCacheBenchFind(); ) | 263 DEF_BENCH( return new GrResourceCacheBenchFind(); ) |
145 | 264 |
146 #endif | 265 #endif |
OLD | NEW |