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" | |
20 #include "SkCanvas.h" | 17 #include "SkCanvas.h" |
21 | 18 |
22 enum { | 19 enum { |
23 CACHE_SIZE_COUNT = 2048, | 20 CACHE_SIZE_COUNT = 4096, |
24 CACHE_SIZE_BYTES = 2 * 1024 * 1024, | |
25 }; | 21 }; |
26 | 22 |
27 class StencilResource : public GrGpuResource { | 23 class BenchResource : public GrGpuResource { |
28 public: | 24 public: |
29 SK_DECLARE_INST_COUNT(StencilResource); | 25 SK_DECLARE_INST_COUNT(BenchResource); |
30 StencilResource(GrGpu* gpu, int id) | 26 BenchResource (GrGpu* gpu) |
31 : INHERITED(gpu, false) | 27 : INHERITED(gpu, false) { |
32 , fID(id) { | |
33 this->registerWithCache(); | 28 this->registerWithCache(); |
34 } | 29 } |
35 | 30 |
36 static GrResourceKey ComputeKey(int width, int height, int sampleCnt) { | 31 static GrResourceKey ComputeKey(int i) { |
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) { | |
60 GrCacheID::Key key; | 32 GrCacheID::Key key; |
61 memset(&key, 0, sizeof(key)); | 33 memset(&key, 0, sizeof(key)); |
62 key.fData32[0] = (desc.fWidth) | (desc.fHeight << 16); | 34 key.fData32[0] = i; |
63 key.fData32[1] = desc.fConfig | desc.fSampleCnt << 16; | |
64 key.fData32[2] = desc.fFlags; | |
65 static int gType = GrResourceKey::GenerateResourceType(); | 35 static int gType = GrResourceKey::GenerateResourceType(); |
66 static int gDomain = GrCacheID::GenerateDomain(); | 36 static int gDomain = GrCacheID::GenerateDomain(); |
67 return GrResourceKey(GrCacheID(gDomain, key), gType, 0); | 37 return GrResourceKey(GrCacheID(gDomain, key), gType, 0); |
68 } | 38 } |
69 | 39 |
70 int fID; | |
71 | 40 |
72 private: | 41 private: |
73 virtual size_t onGpuMemorySize() const SK_OVERRIDE { | 42 size_t onGpuMemorySize() const SK_OVERRIDE { return 100; } |
74 return 100 + ((fID % 1 == 0) ? -40 : 33); | |
75 } | |
76 | 43 |
77 typedef GrGpuResource INHERITED; | 44 typedef GrGpuResource INHERITED; |
78 }; | 45 }; |
79 | 46 |
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 | |
94 static void populate_cache(GrGpu* gpu, int resourceCount) { | 47 static void populate_cache(GrGpu* gpu, int resourceCount) { |
95 for (int i = 0; i < resourceCount; ++i) { | 48 for (int i = 0; i < resourceCount; ++i) { |
96 int w, h, s; | 49 GrResourceKey key = BenchResource::ComputeKey(i); |
97 get_stencil(i, &w, &h, &s); | 50 GrGpuResource* resource = SkNEW_ARGS(BenchResource, (gpu)); |
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)); | |
109 resource->cacheAccess().setContentKey(key); | 51 resource->cacheAccess().setContentKey(key); |
110 resource->unref(); | 52 resource->unref(); |
111 } | 53 } |
112 } | 54 } |
113 | 55 |
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 | |
170 class GrResourceCacheBenchAdd : public Benchmark { | 56 class GrResourceCacheBenchAdd : public Benchmark { |
171 enum { | |
172 RESOURCE_COUNT = CACHE_SIZE_COUNT / 2, | |
173 }; | |
174 | |
175 public: | 57 public: |
176 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { | 58 bool isSuitableFor(Backend backend) SK_OVERRIDE { |
177 return backend == kNonRendering_Backend; | 59 return backend == kNonRendering_Backend; |
178 } | 60 } |
179 | 61 |
180 protected: | 62 protected: |
181 virtual const char* onGetName() SK_OVERRIDE { | 63 const char* onGetName() SK_OVERRIDE { |
182 return "grresourcecache_add"; | 64 return "grresourcecache_add"; |
183 } | 65 } |
184 | 66 |
185 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { | 67 void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { |
186 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 68 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); |
187 if (NULL == context) { | 69 if (NULL == context) { |
188 return; | 70 return; |
189 } | 71 } |
190 // Set the cache budget to be very large so no purging occurs. | 72 // Set the cache budget to be very large so no purging occurs. |
191 context->setResourceCacheLimits(2 * RESOURCE_COUNT, 1 << 30); | 73 context->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30); |
192 | 74 |
193 GrResourceCache2* cache2 = context->getResourceCache2(); | 75 GrResourceCache2* cache2 = context->getResourceCache2(); |
194 | 76 |
195 // Make sure the cache is empty. | 77 // Make sure the cache is empty. |
196 cache2->purgeAllUnlocked(); | 78 cache2->purgeAllUnlocked(); |
197 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceByte
s()); | 79 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceByte
s()); |
198 | 80 |
199 GrGpu* gpu = context->getGpu(); | 81 GrGpu* gpu = context->getGpu(); |
200 | 82 |
201 for (int i = 0; i < loops; ++i) { | 83 for (int i = 0; i < loops; ++i) { |
202 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResource
Bytes()); | 84 populate_cache(gpu, CACHE_SIZE_COUNT); |
203 | 85 SkASSERT(CACHE_SIZE_COUNT == cache2->getResourceCount()); |
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(); | |
211 } | 86 } |
212 } | 87 } |
213 | 88 |
214 private: | 89 private: |
215 typedef Benchmark INHERITED; | 90 typedef Benchmark INHERITED; |
216 }; | 91 }; |
217 | 92 |
218 class GrResourceCacheBenchFind : public Benchmark { | 93 class GrResourceCacheBenchFind : public Benchmark { |
219 enum { | |
220 RESOURCE_COUNT = CACHE_SIZE_COUNT / 2, | |
221 }; | |
222 | |
223 public: | 94 public: |
224 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { | 95 bool isSuitableFor(Backend backend) SK_OVERRIDE { |
225 return backend == kNonRendering_Backend; | 96 return backend == kNonRendering_Backend; |
226 } | 97 } |
227 | 98 |
228 protected: | 99 protected: |
229 virtual const char* onGetName() SK_OVERRIDE { | 100 const char* onGetName() SK_OVERRIDE { |
230 return "grresourcecache_find"; | 101 return "grresourcecache_find"; |
231 } | 102 } |
232 | 103 |
233 virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE { | 104 void onPreDraw() SK_OVERRIDE { |
234 SkAutoTUnref<GrContext> context(GrContext::CreateMockContext()); | 105 fContext.reset(GrContext::CreateMockContext()); |
235 if (NULL == context) { | 106 if (!fContext) { |
236 return; | 107 return; |
237 } | 108 } |
238 // Set the cache budget to be very large so no purging occurs. | 109 // Set the cache budget to be very large so no purging occurs. |
239 context->setResourceCacheLimits(2 * RESOURCE_COUNT, 1 << 30); | 110 fContext->setResourceCacheLimits(CACHE_SIZE_COUNT, 1 << 30); |
240 | 111 |
241 GrResourceCache2* cache2 = context->getResourceCache2(); | 112 GrResourceCache2* cache2 = fContext->getResourceCache2(); |
242 | 113 |
243 // Make sure the cache is empty. | 114 // Make sure the cache is empty. |
244 cache2->purgeAllUnlocked(); | 115 cache2->purgeAllUnlocked(); |
245 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceByte
s()); | 116 SkASSERT(0 == cache2->getResourceCount() && 0 == cache2->getResourceByte
s()); |
246 | 117 |
247 GrGpu* gpu = context->getGpu(); | 118 GrGpu* gpu = fContext->getGpu(); |
248 | 119 |
249 populate_cache(gpu, RESOURCE_COUNT); | 120 populate_cache(gpu, CACHE_SIZE_COUNT); |
| 121 } |
250 | 122 |
| 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()); |
251 for (int i = 0; i < loops; ++i) { | 129 for (int i = 0; i < loops; ++i) { |
252 for (int k = 0; k < RESOURCE_COUNT; ++k) { | 130 for (int k = 0; k < CACHE_SIZE_COUNT; ++k) { |
253 check_cache_contents_or_die(cache2, k); | 131 GrResourceKey key = BenchResource::ComputeKey(k); |
| 132 SkAutoTUnref<GrGpuResource> resource(cache2->findAndRefContentRe
source(key)); |
| 133 SkASSERT(resource); |
254 } | 134 } |
255 } | 135 } |
256 } | 136 } |
257 | 137 |
258 private: | 138 private: |
| 139 SkAutoTUnref<GrContext> fContext; |
259 typedef Benchmark INHERITED; | 140 typedef Benchmark INHERITED; |
260 }; | 141 }; |
261 | 142 |
262 DEF_BENCH( return new GrResourceCacheBenchAdd(); ) | 143 DEF_BENCH( return new GrResourceCacheBenchAdd(); ) |
263 DEF_BENCH( return new GrResourceCacheBenchFind(); ) | 144 DEF_BENCH( return new GrResourceCacheBenchFind(); ) |
264 | 145 |
265 #endif | 146 #endif |
OLD | NEW |