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