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); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 75 | } |
| 76 | #endif |
| 77 | |
| 78 | #if GR_CACHE_STATS |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 79 | void GrResourceCache::dumpStats(SkString* out) const { |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 80 | this->validate(); |
| 81 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 82 | int locked = fNonpurgeableResources.count(); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 83 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 84 | struct Stats { |
| 85 | int fScratch; |
| 86 | int fWrapped; |
| 87 | size_t fUnbudgetedSize; |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 88 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 89 | Stats() : fScratch(0), fWrapped(0), fUnbudgetedSize(0) {} |
| 90 | |
| 91 | void update(GrGpuResource* resource) { |
| 92 | if (resource->cacheAccess().isScratch()) { |
| 93 | ++fScratch; |
| 94 | } |
| 95 | if (resource->cacheAccess().isWrapped()) { |
| 96 | ++fWrapped; |
| 97 | } |
| 98 | if (!resource->resourcePriv().isBudgeted()) { |
| 99 | fUnbudgetedSize += resource->gpuMemorySize(); |
| 100 | } |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 101 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | Stats stats; |
| 105 | |
| 106 | for (int i = 0; i < fNonpurgeableResources.count(); ++i) { |
| 107 | stats.update(fNonpurgeableResources[i]); |
| 108 | } |
| 109 | for (int i = 0; i < fPurgeableQueue.count(); ++i) { |
| 110 | stats.update(fPurgeableQueue.at(i)); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | float countUtilization = (100.f * fBudgetedCount) / fMaxCount; |
| 114 | float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes; |
| 115 | |
| 116 | out->appendf("Budget: %d items %d bytes\n", fMaxCount, (int)fMaxBytes); |
| 117 | out->appendf("\t\tEntry Count: current %d" |
| 118 | " (%d budgeted, %d wrapped, %d locked, %d scratch %.2g%% full), high %d\n", |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 119 | fCount, fBudgetedCount, stats.fWrapped, locked, stats.fScratch, countUtilization, |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 120 | fHighWaterCount); |
| 121 | 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] | 122 | SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization, |
| 123 | SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes)); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | #endif |
| 127 | |
| 128 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 129 | /////////////////////////////////////////////////////////////////////////////// |
| 130 | // Code for the mock context. It's built on a mock GrGpu class that does nothing. |
| 131 | //// |
| 132 | |
| 133 | #include "GrBufferAllocPool.h" |
| 134 | #include "GrInOrderDrawBuffer.h" |
| 135 | #include "GrGpu.h" |
| 136 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 137 | class GrPipeline; |
joshualitt | d53a827 | 2014-11-10 16:03:14 -0800 | [diff] [blame] | 138 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 139 | class MockGpu : public GrGpu { |
| 140 | public: |
| 141 | MockGpu(GrContext* context) : INHERITED(context) { fCaps.reset(SkNEW(GrDrawTargetCaps)); } |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 142 | ~MockGpu() SK_OVERRIDE {} |
| 143 | bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const SK_OVERRIDE { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 144 | return true; |
| 145 | } |
| 146 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 147 | bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget, |
| 148 | int left, int top, |
| 149 | int width, int height, |
| 150 | GrPixelConfig config, |
| 151 | size_t rowBytes) const SK_OVERRIDE { return false; } |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 152 | void buildProgramDesc(GrProgramDesc*,const GrPrimitiveProcessor&, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 153 | const GrPipeline&, |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 154 | const GrBatchTracker&) const SK_OVERRIDE {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 155 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 156 | void discard(GrRenderTarget*) SK_OVERRIDE {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 157 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 158 | bool canCopySurface(const GrSurface* dst, |
| 159 | const GrSurface* src, |
| 160 | const SkIRect& srcRect, |
| 161 | const SkIPoint& dstPoint) SK_OVERRIDE { return false; }; |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 162 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 163 | bool copySurface(GrSurface* dst, |
| 164 | GrSurface* src, |
| 165 | const SkIRect& srcRect, |
| 166 | const SkIPoint& dstPoint) SK_OVERRIDE { return false; }; |
| 167 | |
| 168 | bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) SK_OVERRIDE { |
| 169 | return false; |
| 170 | } |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 171 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 172 | private: |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 173 | void onResetContext(uint32_t resetBits) SK_OVERRIDE {} |
| 174 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 175 | GrTexture* onCreateTexture(const GrSurfaceDesc& desc, bool budgeted, const void* srcData, |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 176 | size_t rowBytes) SK_OVERRIDE { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 177 | return NULL; |
| 178 | } |
| 179 | |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 180 | GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, bool budgeted, |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 181 | const void* srcData) SK_OVERRIDE { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 182 | return NULL; |
| 183 | } |
| 184 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 185 | GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) SK_OVERRIDE { return NULL; } |
| 186 | |
| 187 | GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) SK_OVERRIDE { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 188 | return NULL; |
| 189 | } |
| 190 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 191 | GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) SK_OVERRIDE { return NULL; } |
| 192 | |
| 193 | GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) SK_OVERRIDE { return NULL; } |
| 194 | |
| 195 | void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color, |
| 196 | bool canIgnoreRect) SK_OVERRIDE {} |
| 197 | |
| 198 | void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) SK_OVERRIDE {} |
| 199 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 200 | void onDraw(const DrawArgs&, const GrDrawTarget::DrawInfo&) SK_OVERRIDE {} |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 201 | |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 202 | void onStencilPath(const GrPath* path, const StencilPathState& state) SK_OVERRIDE {} |
bsalomon | d95263c | 2014-12-16 13:05:12 -0800 | [diff] [blame] | 203 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 204 | void onDrawPath(const DrawArgs&, const GrPath*, const GrStencilSettings&) SK_OVERRIDE {} |
bsalomon | d95263c | 2014-12-16 13:05:12 -0800 | [diff] [blame] | 205 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 206 | void onDrawPaths(const DrawArgs&, |
bsalomon | d95263c | 2014-12-16 13:05:12 -0800 | [diff] [blame] | 207 | const GrPathRange*, |
| 208 | const void* indices, |
| 209 | GrDrawTarget::PathIndexType, |
| 210 | const float transformValues[], |
| 211 | GrDrawTarget::PathTransformType, |
| 212 | int count, |
| 213 | const GrStencilSettings&) SK_OVERRIDE {} |
| 214 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 215 | bool onReadPixels(GrRenderTarget* target, |
| 216 | int left, int top, int width, int height, |
| 217 | GrPixelConfig, |
| 218 | void* buffer, |
| 219 | size_t rowBytes) SK_OVERRIDE { |
| 220 | return false; |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 221 | } |
| 222 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 223 | bool onWriteTexturePixels(GrTexture* texture, |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 224 | int left, int top, int width, int height, |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 225 | GrPixelConfig config, const void* buffer, |
| 226 | size_t rowBytes) SK_OVERRIDE { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 227 | return false; |
| 228 | } |
| 229 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 230 | void onResolveRenderTarget(GrRenderTarget* target) SK_OVERRIDE { return; } |
| 231 | |
bsalomon | 02a44a4 | 2015-02-19 09:09:00 -0800 | [diff] [blame^] | 232 | bool createStencilBufferForRenderTarget(GrRenderTarget*, int width, int height) SK_OVERRIDE { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 233 | return false; |
| 234 | } |
| 235 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 236 | bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) 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 | void clearStencil(GrRenderTarget* target) SK_OVERRIDE {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 241 | |
bsalomon | d95263c | 2014-12-16 13:05:12 -0800 | [diff] [blame] | 242 | void didAddGpuTraceMarker() SK_OVERRIDE {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 243 | |
bsalomon | d95263c | 2014-12-16 13:05:12 -0800 | [diff] [blame] | 244 | void didRemoveGpuTraceMarker() SK_OVERRIDE {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 245 | |
| 246 | typedef GrGpu INHERITED; |
| 247 | }; |
| 248 | |
| 249 | GrContext* GrContext::CreateMockContext() { |
| 250 | GrContext* context = SkNEW_ARGS(GrContext, (Options())); |
| 251 | |
| 252 | context->initMockContext(); |
| 253 | return context; |
| 254 | } |
| 255 | |
| 256 | void GrContext::initMockContext() { |
| 257 | SkASSERT(NULL == fGpu); |
| 258 | fGpu = SkNEW_ARGS(MockGpu, (this)); |
| 259 | SkASSERT(fGpu); |
| 260 | this->initCommon(); |
| 261 | |
| 262 | // We delete these because we want to test the cache starting with zero resources. Also, none of |
| 263 | // these objects are required for any of tests that use this context. TODO: make stop allocating |
| 264 | // resources in the buffer pools. |
| 265 | SkDELETE(fDrawBuffer); |
| 266 | SkDELETE(fDrawBufferVBAllocPool); |
| 267 | SkDELETE(fDrawBufferIBAllocPool); |
| 268 | |
| 269 | fDrawBuffer = NULL; |
| 270 | fDrawBufferVBAllocPool = NULL; |
| 271 | fDrawBufferIBAllocPool = NULL; |
| 272 | } |