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 #if SK_SUPPORT_GPU | 9 #if SK_SUPPORT_GPU |
10 | 10 |
11 #include "Benchmark.h" | 11 #include "Benchmark.h" |
12 #include "GrCacheable.h" | 12 #include "GrGpuObject.h" |
13 #include "GrContext.h" | 13 #include "GrContext.h" |
14 #include "GrResourceCache.h" | 14 #include "GrResourceCache.h" |
15 #include "GrStencilBuffer.h" | 15 #include "GrStencilBuffer.h" |
16 #include "GrTexture.h" | 16 #include "GrTexture.h" |
17 #include "SkCanvas.h" | 17 #include "SkCanvas.h" |
18 | 18 |
19 enum { | 19 enum { |
20 CACHE_SIZE_COUNT = 2048, | 20 CACHE_SIZE_COUNT = 2048, |
21 CACHE_SIZE_BYTES = 2 * 1024 * 1024, | 21 CACHE_SIZE_BYTES = 2 * 1024 * 1024, |
22 }; | 22 }; |
23 | 23 |
24 class StencilResource : public GrCacheable { | 24 class StencilResource : public GrGpuObject { |
25 public: | 25 public: |
26 SK_DECLARE_INST_COUNT(StencilResource); | 26 SK_DECLARE_INST_COUNT(StencilResource); |
27 StencilResource(int id) | 27 StencilResource(GrGpu* gpu, int id) |
28 : fID(id) { | 28 : INHERITED(gpu, false) |
| 29 , fID(id) { |
29 } | 30 } |
30 | 31 |
| 32 virtual ~StencilResource() { this->release(); } |
| 33 |
31 virtual size_t gpuMemorySize() const SK_OVERRIDE { | 34 virtual size_t gpuMemorySize() const SK_OVERRIDE { |
32 return 100 + ((fID % 1 == 0) ? -5 : 6); | 35 return 100 + ((fID % 1 == 0) ? -5 : 6); |
33 } | 36 } |
34 | 37 |
35 virtual bool isValidOnGpu() const SK_OVERRIDE { | |
36 return true; | |
37 } | |
38 | |
39 static GrResourceKey ComputeKey(int width, int height, int sampleCnt) { | 38 static GrResourceKey ComputeKey(int width, int height, int sampleCnt) { |
40 return GrStencilBuffer::ComputeKey(width, height, sampleCnt); | 39 return GrStencilBuffer::ComputeKey(width, height, sampleCnt); |
41 } | 40 } |
42 | 41 |
43 int fID; | 42 int fID; |
44 | 43 |
45 private: | 44 private: |
46 typedef GrCacheable INHERITED; | 45 typedef GrGpuObject INHERITED; |
47 }; | 46 }; |
48 | 47 |
49 class TextureResource : public GrCacheable { | 48 class TextureResource : public GrGpuObject { |
50 public: | 49 public: |
51 SK_DECLARE_INST_COUNT(TextureResource); | 50 SK_DECLARE_INST_COUNT(TextureResource); |
52 TextureResource(int id) | 51 TextureResource(GrGpu* gpu, int id) |
53 : fID(id) { | 52 : INHERITED(gpu, false) |
| 53 , fID(id) { |
54 } | 54 } |
55 | 55 |
| 56 virtual ~TextureResource() { this->release(); } |
| 57 |
56 virtual size_t gpuMemorySize() const SK_OVERRIDE { | 58 virtual size_t gpuMemorySize() const SK_OVERRIDE { |
57 return 100 + ((fID % 1 == 0) ? -40 : 33); | 59 return 100 + ((fID % 1 == 0) ? -40 : 33); |
58 } | 60 } |
59 | 61 |
60 virtual bool isValidOnGpu() const SK_OVERRIDE { | |
61 return true; | |
62 } | |
63 | |
64 static GrResourceKey ComputeKey(const GrTextureDesc& desc) { | 62 static GrResourceKey ComputeKey(const GrTextureDesc& desc) { |
65 return GrTextureImpl::ComputeScratchKey(desc); | 63 return GrTextureImpl::ComputeScratchKey(desc); |
66 } | 64 } |
67 | 65 |
68 int fID; | 66 int fID; |
69 | 67 |
70 private: | 68 private: |
71 typedef GrCacheable INHERITED; | 69 typedef GrGpuObject INHERITED; |
72 }; | 70 }; |
73 | 71 |
74 static void get_stencil(int i, int* w, int* h, int* s) { | 72 static void get_stencil(int i, int* w, int* h, int* s) { |
75 *w = i % 1024; | 73 *w = i % 1024; |
76 *h = i * 2 % 1024; | 74 *h = i * 2 % 1024; |
77 *s = i % 1 == 0 ? 0 : 4; | 75 *s = i % 1 == 0 ? 0 : 4; |
78 } | 76 } |
79 | 77 |
80 static void get_texture_desc(int i, GrTextureDesc* desc) { | 78 static void get_texture_desc(int i, GrTextureDesc* desc) { |
81 desc->fFlags = kRenderTarget_GrTextureFlagBit | | 79 desc->fFlags = kRenderTarget_GrTextureFlagBit | |
82 kNoStencil_GrTextureFlagBit; | 80 kNoStencil_GrTextureFlagBit; |
83 desc->fWidth = i % 1024; | 81 desc->fWidth = i % 1024; |
84 desc->fHeight = i * 2 % 1024; | 82 desc->fHeight = i * 2 % 1024; |
85 desc->fConfig = static_cast<GrPixelConfig>(i % (kLast_GrPixelConfig + 1)); | 83 desc->fConfig = static_cast<GrPixelConfig>(i % (kLast_GrPixelConfig + 1)); |
86 desc->fSampleCnt = i % 1 == 0 ? 0 : 4; | 84 desc->fSampleCnt = i % 1 == 0 ? 0 : 4; |
87 } | 85 } |
88 | 86 |
89 static void populate_cache(GrResourceCache* cache, GrGpu* gpu, int resourceCount
) { | 87 static void populate_cache(GrResourceCache* cache, GrGpu* gpu, int resourceCount
) { |
90 for (int i = 0; i < resourceCount; ++i) { | 88 for (int i = 0; i < resourceCount; ++i) { |
91 int w, h, s; | 89 int w, h, s; |
92 get_stencil(i, &w, &h, &s); | 90 get_stencil(i, &w, &h, &s); |
93 GrResourceKey key = GrStencilBuffer::ComputeKey(w, h, s); | 91 GrResourceKey key = GrStencilBuffer::ComputeKey(w, h, s); |
94 GrCacheable* resource = SkNEW_ARGS(StencilResource, (i)); | 92 GrGpuObject* resource = SkNEW_ARGS(StencilResource, (gpu, i)); |
95 cache->purgeAsNeeded(1, resource->gpuMemorySize()); | 93 cache->purgeAsNeeded(1, resource->gpuMemorySize()); |
96 cache->addResource(key, resource); | 94 cache->addResource(key, resource); |
97 resource->unref(); | 95 resource->unref(); |
98 } | 96 } |
99 | 97 |
100 for (int i = 0; i < resourceCount; ++i) { | 98 for (int i = 0; i < resourceCount; ++i) { |
101 GrTextureDesc desc; | 99 GrTextureDesc desc; |
102 get_texture_desc(i, &desc); | 100 get_texture_desc(i, &desc); |
103 GrResourceKey key = TextureResource::ComputeKey(desc); | 101 GrResourceKey key = TextureResource::ComputeKey(desc); |
104 GrCacheable* resource = SkNEW_ARGS(TextureResource, (i)); | 102 GrGpuObject* resource = SkNEW_ARGS(TextureResource, (gpu, i)); |
105 cache->purgeAsNeeded(1, resource->gpuMemorySize()); | 103 cache->purgeAsNeeded(1, resource->gpuMemorySize()); |
106 cache->addResource(key, resource); | 104 cache->addResource(key, resource); |
107 resource->unref(); | 105 resource->unref(); |
108 } | 106 } |
109 } | 107 } |
110 | 108 |
111 static void check_cache_contents_or_die(GrResourceCache* cache, int k) { | 109 static void check_cache_contents_or_die(GrResourceCache* cache, int k) { |
112 // Benchmark find calls that succeed. | 110 // Benchmark find calls that succeed. |
113 { | 111 { |
114 GrTextureDesc desc; | 112 GrTextureDesc desc; |
115 get_texture_desc(k, &desc); | 113 get_texture_desc(k, &desc); |
116 GrResourceKey key = TextureResource::ComputeKey(desc); | 114 GrResourceKey key = TextureResource::ComputeKey(desc); |
117 GrCacheable* item = cache->find(key); | 115 GrGpuObject* item = cache->find(key); |
118 if (NULL == item) { | 116 if (NULL == item) { |
119 SkFAIL("cache add does not work as expected"); | 117 SkFAIL("cache add does not work as expected"); |
120 return; | 118 return; |
121 } | 119 } |
122 if (static_cast<TextureResource*>(item)->fID != k) { | 120 if (static_cast<TextureResource*>(item)->fID != k) { |
123 SkFAIL("cache add does not work as expected"); | 121 SkFAIL("cache add does not work as expected"); |
124 return; | 122 return; |
125 } | 123 } |
126 } | 124 } |
127 { | 125 { |
128 int w, h, s; | 126 int w, h, s; |
129 get_stencil(k, &w, &h, &s); | 127 get_stencil(k, &w, &h, &s); |
130 GrResourceKey key = StencilResource::ComputeKey(w, h, s); | 128 GrResourceKey key = StencilResource::ComputeKey(w, h, s); |
131 GrCacheable* item = cache->find(key); | 129 GrGpuObject* item = cache->find(key); |
132 if (NULL == item) { | 130 if (NULL == item) { |
133 SkFAIL("cache add does not work as expected"); | 131 SkFAIL("cache add does not work as expected"); |
134 return; | 132 return; |
135 } | 133 } |
136 if (static_cast<TextureResource*>(item)->fID != k) { | 134 if (static_cast<TextureResource*>(item)->fID != k) { |
137 SkFAIL("cache add does not work as expected"); | 135 SkFAIL("cache add does not work as expected"); |
138 return; | 136 return; |
139 } | 137 } |
140 } | 138 } |
141 | 139 |
142 // Benchmark also find calls that always fail. | 140 // Benchmark also find calls that always fail. |
143 { | 141 { |
144 GrTextureDesc desc; | 142 GrTextureDesc desc; |
145 get_texture_desc(k, &desc); | 143 get_texture_desc(k, &desc); |
146 desc.fHeight |= 1; | 144 desc.fHeight |= 1; |
147 GrResourceKey key = TextureResource::ComputeKey(desc); | 145 GrResourceKey key = TextureResource::ComputeKey(desc); |
148 GrCacheable* item = cache->find(key); | 146 GrGpuObject* item = cache->find(key); |
149 if (NULL != item) { | 147 if (NULL != item) { |
150 SkFAIL("cache add does not work as expected"); | 148 SkFAIL("cache add does not work as expected"); |
151 return; | 149 return; |
152 } | 150 } |
153 } | 151 } |
154 { | 152 { |
155 int w, h, s; | 153 int w, h, s; |
156 get_stencil(k, &w, &h, &s); | 154 get_stencil(k, &w, &h, &s); |
157 h |= 1; | 155 h |= 1; |
158 GrResourceKey key = StencilResource::ComputeKey(w, h, s); | 156 GrResourceKey key = StencilResource::ComputeKey(w, h, s); |
159 GrCacheable* item = cache->find(key); | 157 GrGpuObject* item = cache->find(key); |
160 if (NULL != item) { | 158 if (NULL != item) { |
161 SkFAIL("cache add does not work as expected"); | 159 SkFAIL("cache add does not work as expected"); |
162 return; | 160 return; |
163 } | 161 } |
164 } | 162 } |
165 } | 163 } |
166 | 164 |
167 class GrResourceCacheBenchAdd : public Benchmark { | 165 class GrResourceCacheBenchAdd : public Benchmark { |
168 enum { | 166 enum { |
169 RESOURCE_COUNT = CACHE_SIZE_COUNT / 2, | 167 RESOURCE_COUNT = CACHE_SIZE_COUNT / 2, |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 } | 228 } |
231 | 229 |
232 private: | 230 private: |
233 typedef Benchmark INHERITED; | 231 typedef Benchmark INHERITED; |
234 }; | 232 }; |
235 | 233 |
236 DEF_BENCH( return new GrResourceCacheBenchAdd(); ) | 234 DEF_BENCH( return new GrResourceCacheBenchAdd(); ) |
237 DEF_BENCH( return new GrResourceCacheBenchFind(); ) | 235 DEF_BENCH( return new GrResourceCacheBenchFind(); ) |
238 | 236 |
239 #endif | 237 #endif |
OLD | NEW |