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