commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2013 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "GrTest.h" |
| 10 | |
bsalomon | 3582d3e | 2015-02-13 14:20:05 -0800 | [diff] [blame] | 11 | #include "GrGpuResourceCacheAccess.h" |
joshualitt | 50408ad | 2014-11-03 12:31:14 -0800 | [diff] [blame] | 12 | #include "GrInOrderDrawBuffer.h" |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 13 | #include "GrResourceCache.h" |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 14 | #include "SkString.h" |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 15 | |
| 16 | void GrTestTarget::init(GrContext* ctx, GrDrawTarget* target) { |
| 17 | SkASSERT(!fContext); |
| 18 | |
| 19 | fContext.reset(SkRef(ctx)); |
| 20 | fDrawTarget.reset(SkRef(target)); |
| 21 | |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 22 | SkNEW_IN_TLAZY(&fACR, GrDrawTarget::AutoClipRestore, (target)); |
| 23 | SkNEW_IN_TLAZY(&fAGP, GrDrawTarget::AutoGeometryPush, (target)); |
| 24 | } |
| 25 | |
| 26 | void GrContext::getTestTarget(GrTestTarget* tar) { |
| 27 | this->flush(); |
| 28 | // We could create a proxy GrDrawTarget that passes through to fGpu until ~GrTextTarget() and |
| 29 | // then disconnects. This would help prevent test writers from mixing using the returned |
| 30 | // GrDrawTarget and regular drawing. We could also assert or fail in GrContext drawing methods |
| 31 | // until ~GrTestTarget(). |
joshualitt | 50408ad | 2014-11-03 12:31:14 -0800 | [diff] [blame] | 32 | tar->init(this, fDrawBuffer); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | /////////////////////////////////////////////////////////////////////////////// |
| 36 | |
| 37 | void GrContext::setMaxTextureSizeOverride(int maxTextureSizeOverride) { |
| 38 | fMaxTextureSizeOverride = maxTextureSizeOverride; |
| 39 | } |
robertphillips@google.com | 94d8f1e | 2013-12-18 17:25:33 +0000 | [diff] [blame] | 40 | |
| 41 | void GrContext::purgeAllUnlockedResources() { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 42 | fResourceCache->purgeAllUnlocked(); |
robertphillips@google.com | 94d8f1e | 2013-12-18 17:25:33 +0000 | [diff] [blame] | 43 | } |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 44 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 45 | void GrContext::dumpCacheStats(SkString* out) const { |
| 46 | #if GR_CACHE_STATS |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 47 | fResourceCache->dumpStats(out); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 48 | #endif |
| 49 | } |
| 50 | |
| 51 | void GrContext::printCacheStats() const { |
| 52 | SkString out; |
| 53 | this->dumpCacheStats(&out); |
kkinnunen | 297aaf9 | 2015-02-19 06:32:12 -0800 | [diff] [blame] | 54 | SkDebugf("%s", out.c_str()); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void GrContext::dumpGpuStats(SkString* out) const { |
| 58 | #if GR_GPU_STATS |
| 59 | return fGpu->stats()->dump(out); |
| 60 | #endif |
| 61 | } |
| 62 | |
| 63 | void GrContext::printGpuStats() const { |
| 64 | SkString out; |
| 65 | this->dumpGpuStats(&out); |
kkinnunen | 297aaf9 | 2015-02-19 06:32:12 -0800 | [diff] [blame] | 66 | SkDebugf("%s", out.c_str()); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | #if GR_GPU_STATS |
| 70 | void GrGpu::Stats::dump(SkString* out) { |
| 71 | out->appendf("Render Target Binds: %d\n", fRenderTargetBinds); |
| 72 | out->appendf("Shader Compilations: %d\n", fShaderCompilations); |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 73 | out->appendf("Textures Created: %d\n", fTextureCreates); |
| 74 | out->appendf("Texture Uploads: %d\n", fTextureUploads); |
bsalomon | 9e5fc72 | 2015-02-23 10:01:36 -0800 | [diff] [blame] | 75 | out->appendf("Stencil Buffer Creates: %d\n", fStencilBufferCreates); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 76 | } |
| 77 | #endif |
| 78 | |
| 79 | #if GR_CACHE_STATS |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 80 | void GrResourceCache::dumpStats(SkString* out) const { |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 81 | this->validate(); |
| 82 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 83 | int locked = fNonpurgeableResources.count(); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 84 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 85 | struct Stats { |
| 86 | int fScratch; |
| 87 | int fWrapped; |
| 88 | size_t fUnbudgetedSize; |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 89 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 90 | Stats() : fScratch(0), fWrapped(0), fUnbudgetedSize(0) {} |
| 91 | |
| 92 | void update(GrGpuResource* resource) { |
| 93 | if (resource->cacheAccess().isScratch()) { |
| 94 | ++fScratch; |
| 95 | } |
| 96 | if (resource->cacheAccess().isWrapped()) { |
| 97 | ++fWrapped; |
| 98 | } |
| 99 | if (!resource->resourcePriv().isBudgeted()) { |
| 100 | fUnbudgetedSize += resource->gpuMemorySize(); |
| 101 | } |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 102 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | Stats stats; |
| 106 | |
| 107 | for (int i = 0; i < fNonpurgeableResources.count(); ++i) { |
| 108 | stats.update(fNonpurgeableResources[i]); |
| 109 | } |
| 110 | for (int i = 0; i < fPurgeableQueue.count(); ++i) { |
| 111 | stats.update(fPurgeableQueue.at(i)); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | float countUtilization = (100.f * fBudgetedCount) / fMaxCount; |
| 115 | float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes; |
| 116 | |
| 117 | out->appendf("Budget: %d items %d bytes\n", fMaxCount, (int)fMaxBytes); |
| 118 | out->appendf("\t\tEntry Count: current %d" |
| 119 | " (%d budgeted, %d wrapped, %d locked, %d scratch %.2g%% full), high %d\n", |
bsalomon | 52057c8 | 2015-02-23 12:12:59 -0800 | [diff] [blame^] | 120 | this->getResourceCount(), fBudgetedCount, stats.fWrapped, locked, stats.fScratch, |
| 121 | countUtilization, fHighWaterCount); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 122 | out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n", |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 123 | SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization, |
| 124 | SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes)); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | #endif |
| 128 | |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 129 | /////////////////////////////////////////////////////////////////////////////// |
| 130 | |
| 131 | void GrResourceCache::changeTimestamp(uint32_t newTimestamp) { fTimestamp = newTimestamp; } |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 132 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 133 | /////////////////////////////////////////////////////////////////////////////// |
| 134 | // Code for the mock context. It's built on a mock GrGpu class that does nothing. |
| 135 | //// |
| 136 | |
| 137 | #include "GrBufferAllocPool.h" |
| 138 | #include "GrInOrderDrawBuffer.h" |
| 139 | #include "GrGpu.h" |
| 140 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 141 | class GrPipeline; |
joshualitt | d53a827 | 2014-11-10 16:03:14 -0800 | [diff] [blame] | 142 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 143 | class MockGpu : public GrGpu { |
| 144 | public: |
| 145 | MockGpu(GrContext* context) : INHERITED(context) { fCaps.reset(SkNEW(GrDrawTargetCaps)); } |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 146 | ~MockGpu() SK_OVERRIDE {} |
| 147 | bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const SK_OVERRIDE { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 148 | return true; |
| 149 | } |
| 150 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 151 | bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget, |
| 152 | int left, int top, |
| 153 | int width, int height, |
| 154 | GrPixelConfig config, |
| 155 | size_t rowBytes) const SK_OVERRIDE { return false; } |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 156 | void buildProgramDesc(GrProgramDesc*,const GrPrimitiveProcessor&, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 157 | const GrPipeline&, |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 158 | const GrBatchTracker&) const SK_OVERRIDE {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 159 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 160 | void discard(GrRenderTarget*) SK_OVERRIDE {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 161 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 162 | bool canCopySurface(const GrSurface* dst, |
| 163 | const GrSurface* src, |
| 164 | const SkIRect& srcRect, |
| 165 | const SkIPoint& dstPoint) SK_OVERRIDE { return false; }; |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 166 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 167 | bool copySurface(GrSurface* dst, |
| 168 | GrSurface* src, |
| 169 | const SkIRect& srcRect, |
| 170 | const SkIPoint& dstPoint) SK_OVERRIDE { return false; }; |
| 171 | |
| 172 | bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) SK_OVERRIDE { |
| 173 | return false; |
| 174 | } |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 175 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 176 | private: |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 177 | void onResetContext(uint32_t resetBits) SK_OVERRIDE {} |
| 178 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 179 | GrTexture* onCreateTexture(const GrSurfaceDesc& desc, bool budgeted, const void* srcData, |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 180 | size_t rowBytes) SK_OVERRIDE { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 181 | return NULL; |
| 182 | } |
| 183 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 184 | GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, bool budgeted, |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 185 | const void* srcData) SK_OVERRIDE { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 186 | return NULL; |
| 187 | } |
| 188 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 189 | GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) SK_OVERRIDE { return NULL; } |
| 190 | |
| 191 | GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) SK_OVERRIDE { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 192 | return NULL; |
| 193 | } |
| 194 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 195 | GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) SK_OVERRIDE { return NULL; } |
| 196 | |
| 197 | GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) SK_OVERRIDE { return NULL; } |
| 198 | |
| 199 | void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color, |
| 200 | bool canIgnoreRect) SK_OVERRIDE {} |
| 201 | |
| 202 | void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) SK_OVERRIDE {} |
| 203 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 204 | void onDraw(const DrawArgs&, const GrDrawTarget::DrawInfo&) SK_OVERRIDE {} |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 205 | |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 206 | void onStencilPath(const GrPath* path, const StencilPathState& state) SK_OVERRIDE {} |
bsalomon | d95263c | 2014-12-16 13:05:12 -0800 | [diff] [blame] | 207 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 208 | void onDrawPath(const DrawArgs&, const GrPath*, const GrStencilSettings&) SK_OVERRIDE {} |
bsalomon | d95263c | 2014-12-16 13:05:12 -0800 | [diff] [blame] | 209 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 210 | void onDrawPaths(const DrawArgs&, |
bsalomon | d95263c | 2014-12-16 13:05:12 -0800 | [diff] [blame] | 211 | const GrPathRange*, |
| 212 | const void* indices, |
| 213 | GrDrawTarget::PathIndexType, |
| 214 | const float transformValues[], |
| 215 | GrDrawTarget::PathTransformType, |
| 216 | int count, |
| 217 | const GrStencilSettings&) SK_OVERRIDE {} |
| 218 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 219 | bool onReadPixels(GrRenderTarget* target, |
| 220 | int left, int top, int width, int height, |
| 221 | GrPixelConfig, |
| 222 | void* buffer, |
| 223 | size_t rowBytes) SK_OVERRIDE { |
| 224 | return false; |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 225 | } |
| 226 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 227 | bool onWriteTexturePixels(GrTexture* texture, |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 228 | int left, int top, int width, int height, |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 229 | GrPixelConfig config, const void* buffer, |
| 230 | size_t rowBytes) SK_OVERRIDE { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 231 | return false; |
| 232 | } |
| 233 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 234 | void onResolveRenderTarget(GrRenderTarget* target) SK_OVERRIDE { return; } |
| 235 | |
bsalomon | 02a44a4 | 2015-02-19 09:09:00 -0800 | [diff] [blame] | 236 | bool createStencilBufferForRenderTarget(GrRenderTarget*, int width, int height) SK_OVERRIDE { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 237 | return false; |
| 238 | } |
| 239 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 240 | bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) SK_OVERRIDE { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 241 | return false; |
| 242 | } |
| 243 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 244 | void clearStencil(GrRenderTarget* target) SK_OVERRIDE {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 245 | |
bsalomon | d95263c | 2014-12-16 13:05:12 -0800 | [diff] [blame] | 246 | void didAddGpuTraceMarker() SK_OVERRIDE {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 247 | |
bsalomon | d95263c | 2014-12-16 13:05:12 -0800 | [diff] [blame] | 248 | void didRemoveGpuTraceMarker() SK_OVERRIDE {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 249 | |
| 250 | typedef GrGpu INHERITED; |
| 251 | }; |
| 252 | |
| 253 | GrContext* GrContext::CreateMockContext() { |
| 254 | GrContext* context = SkNEW_ARGS(GrContext, (Options())); |
| 255 | |
| 256 | context->initMockContext(); |
| 257 | return context; |
| 258 | } |
| 259 | |
| 260 | void GrContext::initMockContext() { |
| 261 | SkASSERT(NULL == fGpu); |
| 262 | fGpu = SkNEW_ARGS(MockGpu, (this)); |
| 263 | SkASSERT(fGpu); |
| 264 | this->initCommon(); |
| 265 | |
| 266 | // We delete these because we want to test the cache starting with zero resources. Also, none of |
| 267 | // these objects are required for any of tests that use this context. TODO: make stop allocating |
| 268 | // resources in the buffer pools. |
| 269 | SkDELETE(fDrawBuffer); |
| 270 | SkDELETE(fDrawBufferVBAllocPool); |
| 271 | SkDELETE(fDrawBufferIBAllocPool); |
| 272 | |
| 273 | fDrawBuffer = NULL; |
| 274 | fDrawBufferVBAllocPool = NULL; |
| 275 | fDrawBufferIBAllocPool = NULL; |
| 276 | } |