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 = 2048, | 23 CACHE_SIZE_COUNT = 2048, |
21 CACHE_SIZE_BYTES = 2 * 1024 * 1024, | 24 CACHE_SIZE_BYTES = 2 * 1024 * 1024, |
22 }; | 25 }; |
23 | 26 |
24 class FooResource : public GrGpuResource { | 27 class StencilResource : public GrGpuResource { |
25 public: | 28 public: |
26 SK_DECLARE_INST_COUNT(FooResource); | 29 SK_DECLARE_INST_COUNT(StencilResource); |
27 FooResource(GrGpu* gpu, int id) | 30 StencilResource(GrGpu* gpu, int id) |
28 : INHERITED(gpu, false) | 31 : INHERITED(gpu, false) |
29 , fID(id) { | 32 , fID(id) { |
30 this->registerWithCache(); | 33 this->registerWithCache(); |
31 } | 34 } |
32 | 35 |
33 static GrResourceKey ComputeKey(int width, int height, int sampleCnt) { | 36 static GrResourceKey ComputeKey(int width, int height, int sampleCnt) { |
34 GrCacheID::Key key; | 37 return GrStencilBuffer::ComputeKey(width, height, sampleCnt); |
35 memset(&key, 0, sizeof(key)); | |
36 key.fData32[0] = width; | |
37 key.fData32[1] = height; | |
38 key.fData32[2] = sampleCnt; | |
39 static int gType = GrResourceKey::GenerateResourceType(); | |
40 static int gDomain = GrCacheID::GenerateDomain(); | |
41 return GrResourceKey(GrCacheID(gDomain, key), gType, 0); | |
42 } | 38 } |
43 | 39 |
44 int fID; | 40 int fID; |
45 | 41 |
46 private: | 42 private: |
47 virtual size_t onGpuMemorySize() const SK_OVERRIDE { | 43 virtual size_t onGpuMemorySize() const SK_OVERRIDE { |
48 return 100 + ((fID % 1 == 0) ? -5 : 6); | 44 return 100 + ((fID % 1 == 0) ? -5 : 6); |
49 } | 45 } |
50 | 46 |
51 typedef GrGpuResource INHERITED; | 47 typedef GrGpuResource INHERITED; |
52 }; | 48 }; |
53 | 49 |
54 class BarResource : public GrGpuResource { | 50 class TextureResource : public GrGpuResource { |
55 public: | 51 public: |
56 SK_DECLARE_INST_COUNT(BarResource); | 52 SK_DECLARE_INST_COUNT(TextureResource); |
57 BarResource(GrGpu* gpu, int id) | 53 TextureResource(GrGpu* gpu, int id) |
58 : INHERITED(gpu, false) | 54 : INHERITED(gpu, false) |
59 , fID(id) { | 55 , fID(id) { |
60 this->registerWithCache(); | 56 this->registerWithCache(); |
61 } | 57 } |
62 | 58 |
63 static GrResourceKey ComputeKey(const GrSurfaceDesc& desc) { | 59 static GrResourceKey ComputeKey(const GrSurfaceDesc& desc) { |
64 GrCacheID::Key key; | 60 GrCacheID::Key key; |
65 memset(&key, 0, sizeof(key)); | 61 memset(&key, 0, sizeof(key)); |
66 key.fData32[0] = (desc.fWidth) | (desc.fHeight << 16); | 62 key.fData32[0] = (desc.fWidth) | (desc.fHeight << 16); |
67 key.fData32[1] = desc.fConfig | desc.fSampleCnt << 16; | 63 key.fData32[1] = desc.fConfig | desc.fSampleCnt << 16; |
68 key.fData32[2] = desc.fFlags; | 64 key.fData32[2] = desc.fFlags; |
69 static int gType = GrResourceKey::GenerateResourceType(); | 65 static int gType = GrResourceKey::GenerateResourceType(); |
70 static int gDomain = GrCacheID::GenerateDomain(); | 66 static int gDomain = GrCacheID::GenerateDomain(); |
71 return GrResourceKey(GrCacheID(gDomain, key), gType, 0); | 67 return GrResourceKey(GrCacheID(gDomain, key), gType, 0); |
72 } | 68 } |
73 | 69 |
74 int fID; | 70 int fID; |
75 | 71 |
76 private: | 72 private: |
77 virtual size_t onGpuMemorySize() const SK_OVERRIDE { | 73 virtual size_t onGpuMemorySize() const SK_OVERRIDE { |
78 return 100 + ((fID % 1 == 0) ? -40 : 33); | 74 return 100 + ((fID % 1 == 0) ? -40 : 33); |
79 } | 75 } |
80 | 76 |
81 typedef GrGpuResource INHERITED; | 77 typedef GrGpuResource INHERITED; |
82 }; | 78 }; |
83 | 79 |
84 static void get_foo_params(int i, int* w, int* h, int* s) { | 80 static void get_stencil(int i, int* w, int* h, int* s) { |
85 *w = i % 1024; | 81 *w = i % 1024; |
86 *h = i * 2 % 1024; | 82 *h = i * 2 % 1024; |
87 *s = i % 2 == 0 ? 0 : 4; | 83 *s = i % 1 == 0 ? 0 : 4; |
88 } | 84 } |
89 | 85 |
90 static void get_bar_surf_desc(int i, GrSurfaceDesc* desc) { | 86 static void get_texture_desc(int i, GrSurfaceDesc* desc) { |
91 desc->fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag; | 87 desc->fFlags = kRenderTarget_GrSurfaceFlag | kNoStencil_GrSurfaceFlag; |
92 desc->fWidth = i % 1024; | 88 desc->fWidth = i % 1024; |
93 desc->fHeight = i * 2 % 1024; | 89 desc->fHeight = i * 2 % 1024; |
94 desc->fConfig = static_cast<GrPixelConfig>(i % (kLast_GrPixelConfig + 1)); | 90 desc->fConfig = static_cast<GrPixelConfig>(i % (kLast_GrPixelConfig + 1)); |
95 desc->fSampleCnt = ((i % 2) == 0) ? 0 : 4; | 91 desc->fSampleCnt = ((i % 2) == 0) ? 0 : 4; |
96 } | 92 } |
97 | 93 |
98 static void populate_cache(GrGpu* gpu, int resourceCount) { | 94 static void populate_cache(GrGpu* gpu, int resourceCount) { |
99 for (int i = 0; i < resourceCount; ++i) { | 95 for (int i = 0; i < resourceCount; ++i) { |
100 int w, h, s; | 96 int w, h, s; |
101 get_foo_params(i, &w, &h, &s); | 97 get_stencil(i, &w, &h, &s); |
102 GrResourceKey key = FooResource::ComputeKey(w, h, s); | 98 GrResourceKey key = GrStencilBuffer::ComputeKey(w, h, s); |
103 GrGpuResource* resource = SkNEW_ARGS(FooResource, (gpu, i)); | 99 GrGpuResource* resource = SkNEW_ARGS(StencilResource, (gpu, i)); |
104 resource->cacheAccess().setContentKey(key); | 100 resource->cacheAccess().setContentKey(key); |
105 resource->unref(); | 101 resource->unref(); |
106 } | 102 } |
107 | 103 |
108 for (int i = 0; i < resourceCount; ++i) { | 104 for (int i = 0; i < resourceCount; ++i) { |
109 GrSurfaceDesc desc; | 105 GrSurfaceDesc desc; |
110 get_bar_surf_desc(i, &desc); | 106 get_texture_desc(i, &desc); |
111 GrResourceKey key = BarResource::ComputeKey(desc); | 107 GrResourceKey key = TextureResource::ComputeKey(desc); |
112 GrGpuResource* resource = SkNEW_ARGS(BarResource, (gpu, i)); | 108 GrGpuResource* resource = SkNEW_ARGS(TextureResource, (gpu, i)); |
113 resource->cacheAccess().setContentKey(key); | 109 resource->cacheAccess().setContentKey(key); |
114 resource->unref(); | 110 resource->unref(); |
115 } | 111 } |
116 } | 112 } |
117 | 113 |
118 static void check_cache_contents_or_die(GrResourceCache2* cache, int k) { | 114 static void check_cache_contents_or_die(GrResourceCache2* cache, int k) { |
119 // Benchmark find calls that succeed. | 115 // Benchmark find calls that succeed. |
120 { | 116 { |
121 GrSurfaceDesc desc; | 117 GrSurfaceDesc desc; |
122 get_bar_surf_desc(k, &desc); | 118 get_texture_desc(k, &desc); |
123 GrResourceKey key = BarResource::ComputeKey(desc); | 119 GrResourceKey key = TextureResource::ComputeKey(desc); |
124 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key)); | 120 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key)); |
125 if (!item) { | 121 if (!item) { |
126 SkFAIL("cache add does not work as expected"); | 122 SkFAIL("cache add does not work as expected"); |
127 return; | 123 return; |
128 } | 124 } |
129 if (static_cast<BarResource*>(item.get())->fID != k) { | 125 if (static_cast<TextureResource*>(item.get())->fID != k) { |
130 SkFAIL("cache add does not work as expected"); | 126 SkFAIL("cache add does not work as expected"); |
131 return; | 127 return; |
132 } | 128 } |
133 } | 129 } |
134 { | 130 { |
135 int w, h, s; | 131 int w, h, s; |
136 get_foo_params(k, &w, &h, &s); | 132 get_stencil(k, &w, &h, &s); |
137 GrResourceKey key = FooResource::ComputeKey(w, h, s); | 133 GrResourceKey key = StencilResource::ComputeKey(w, h, s); |
138 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key)); | 134 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key)); |
139 if (!item) { | 135 if (!item) { |
140 SkFAIL("cache add does not work as expected"); | 136 SkFAIL("cache add does not work as expected"); |
141 return; | 137 return; |
142 } | 138 } |
143 if (static_cast<FooResource*>(item.get())->fID != k) { | 139 if (static_cast<TextureResource*>(item.get())->fID != k) { |
144 SkFAIL("cache add does not work as expected"); | 140 SkFAIL("cache add does not work as expected"); |
145 return; | 141 return; |
146 } | 142 } |
147 } | 143 } |
148 | 144 |
149 // Benchmark also find calls that always fail. | 145 // Benchmark also find calls that always fail. |
150 { | 146 { |
151 GrSurfaceDesc desc; | 147 GrSurfaceDesc desc; |
152 get_bar_surf_desc(k, &desc); | 148 get_texture_desc(k, &desc); |
153 desc.fHeight |= 1; | 149 desc.fHeight |= 1; |
154 GrResourceKey key = BarResource::ComputeKey(desc); | 150 GrResourceKey key = TextureResource::ComputeKey(desc); |
155 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key)); | 151 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key)); |
156 if (item) { | 152 if (item) { |
157 SkFAIL("cache add does not work as expected"); | 153 SkFAIL("cache add does not work as expected"); |
158 return; | 154 return; |
159 } | 155 } |
160 } | 156 } |
161 { | 157 { |
162 int w, h, s; | 158 int w, h, s; |
163 get_foo_params(k, &w, &h, &s); | 159 get_stencil(k, &w, &h, &s); |
164 h |= 1; | 160 h |= 1; |
165 GrResourceKey key = FooResource::ComputeKey(w, h, s); | 161 GrResourceKey key = StencilResource::ComputeKey(w, h, s); |
166 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key)); | 162 SkAutoTUnref<GrGpuResource> item(cache->findAndRefContentResource(key)); |
167 if (item) { | 163 if (item) { |
168 SkFAIL("cache add does not work as expected"); | 164 SkFAIL("cache add does not work as expected"); |
169 return; | 165 return; |
170 } | 166 } |
171 } | 167 } |
172 } | 168 } |
173 | 169 |
174 class GrResourceCacheBenchAdd : public Benchmark { | 170 class GrResourceCacheBenchAdd : public Benchmark { |
175 enum { | 171 enum { |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 } | 256 } |
261 | 257 |
262 private: | 258 private: |
263 typedef Benchmark INHERITED; | 259 typedef Benchmark INHERITED; |
264 }; | 260 }; |
265 | 261 |
266 DEF_BENCH( return new GrResourceCacheBenchAdd(); ) | 262 DEF_BENCH( return new GrResourceCacheBenchAdd(); ) |
267 DEF_BENCH( return new GrResourceCacheBenchFind(); ) | 263 DEF_BENCH( return new GrResourceCacheBenchFind(); ) |
268 | 264 |
269 #endif | 265 #endif |
OLD | NEW |