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" |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 10 | #include "GrContextOptions.h" |
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 | |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame^] | 16 | void GrTestTarget::init(GrContext* ctx, GrDrawTarget* target) { |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 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(). |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame^] | 29 | tar->init(this, fDrawingMgr.fDrawTarget); |
commit-bot@chromium.org | 78a1078 | 2013-08-21 19:27:48 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | /////////////////////////////////////////////////////////////////////////////// |
| 33 | |
robertphillips@google.com | 94d8f1e | 2013-12-18 17:25:33 +0000 | [diff] [blame] | 34 | void GrContext::purgeAllUnlockedResources() { |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 35 | fResourceCache->purgeAllUnlocked(); |
robertphillips@google.com | 94d8f1e | 2013-12-18 17:25:33 +0000 | [diff] [blame] | 36 | } |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 37 | |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 38 | void GrContext::dumpCacheStats(SkString* out) const { |
| 39 | #if GR_CACHE_STATS |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 40 | fResourceCache->dumpStats(out); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 41 | #endif |
| 42 | } |
| 43 | |
| 44 | void GrContext::printCacheStats() const { |
| 45 | SkString out; |
| 46 | this->dumpCacheStats(&out); |
kkinnunen | 297aaf9 | 2015-02-19 06:32:12 -0800 | [diff] [blame] | 47 | SkDebugf("%s", out.c_str()); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void GrContext::dumpGpuStats(SkString* out) const { |
| 51 | #if GR_GPU_STATS |
| 52 | return fGpu->stats()->dump(out); |
| 53 | #endif |
| 54 | } |
| 55 | |
| 56 | void GrContext::printGpuStats() const { |
| 57 | SkString out; |
| 58 | this->dumpGpuStats(&out); |
kkinnunen | 297aaf9 | 2015-02-19 06:32:12 -0800 | [diff] [blame] | 59 | SkDebugf("%s", out.c_str()); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | #if GR_GPU_STATS |
| 63 | void GrGpu::Stats::dump(SkString* out) { |
| 64 | out->appendf("Render Target Binds: %d\n", fRenderTargetBinds); |
| 65 | out->appendf("Shader Compilations: %d\n", fShaderCompilations); |
bsalomon | b12ea41 | 2015-02-02 21:19:50 -0800 | [diff] [blame] | 66 | out->appendf("Textures Created: %d\n", fTextureCreates); |
| 67 | out->appendf("Texture Uploads: %d\n", fTextureUploads); |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 68 | out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 69 | } |
| 70 | #endif |
| 71 | |
| 72 | #if GR_CACHE_STATS |
bsalomon | 0ea80f4 | 2015-02-11 10:49:59 -0800 | [diff] [blame] | 73 | void GrResourceCache::dumpStats(SkString* out) const { |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 74 | this->validate(); |
| 75 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 76 | int locked = fNonpurgeableResources.count(); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 77 | |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 78 | struct Stats { |
| 79 | int fScratch; |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 80 | int fExternal; |
| 81 | int fBorrowed; |
| 82 | int fAdopted; |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 83 | size_t fUnbudgetedSize; |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 84 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 85 | Stats() : fScratch(0), fExternal(0), fBorrowed(0), fAdopted(0), fUnbudgetedSize(0) {} |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 86 | |
| 87 | void update(GrGpuResource* resource) { |
| 88 | if (resource->cacheAccess().isScratch()) { |
| 89 | ++fScratch; |
| 90 | } |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 91 | if (resource->cacheAccess().isExternal()) { |
| 92 | ++fExternal; |
| 93 | } |
| 94 | if (resource->cacheAccess().isBorrowed()) { |
| 95 | ++fBorrowed; |
| 96 | } |
| 97 | if (resource->cacheAccess().isAdopted()) { |
| 98 | ++fAdopted; |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 99 | } |
| 100 | if (!resource->resourcePriv().isBudgeted()) { |
| 101 | fUnbudgetedSize += resource->gpuMemorySize(); |
| 102 | } |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 103 | } |
bsalomon | f320e04 | 2015-02-17 15:09:34 -0800 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | Stats stats; |
| 107 | |
| 108 | for (int i = 0; i < fNonpurgeableResources.count(); ++i) { |
| 109 | stats.update(fNonpurgeableResources[i]); |
| 110 | } |
| 111 | for (int i = 0; i < fPurgeableQueue.count(); ++i) { |
| 112 | stats.update(fPurgeableQueue.at(i)); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | float countUtilization = (100.f * fBudgetedCount) / fMaxCount; |
| 116 | float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes; |
| 117 | |
| 118 | out->appendf("Budget: %d items %d bytes\n", fMaxCount, (int)fMaxBytes); |
| 119 | out->appendf("\t\tEntry Count: current %d" |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 120 | " (%d budgeted, %d external(%d borrowed, %d adopted), %d locked, %d scratch %.2g%% full), high %d\n", |
| 121 | this->getResourceCount(), fBudgetedCount, stats.fExternal, stats.fBorrowed, |
| 122 | stats.fAdopted, locked, stats.fScratch, countUtilization, fHighWaterCount); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 123 | 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] | 124 | SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization, |
| 125 | SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes)); |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | #endif |
| 129 | |
bsalomon | ddf30e6 | 2015-02-19 11:38:44 -0800 | [diff] [blame] | 130 | /////////////////////////////////////////////////////////////////////////////// |
| 131 | |
| 132 | void GrResourceCache::changeTimestamp(uint32_t newTimestamp) { fTimestamp = newTimestamp; } |
mtklein | b9eb4ac | 2015-02-02 18:26:03 -0800 | [diff] [blame] | 133 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 134 | /////////////////////////////////////////////////////////////////////////////// |
| 135 | // Code for the mock context. It's built on a mock GrGpu class that does nothing. |
| 136 | //// |
| 137 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 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: |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 145 | MockGpu(GrContext* context, const GrContextOptions& options) : INHERITED(context) { |
| 146 | fCaps.reset(SkNEW_ARGS(GrCaps, (options))); |
| 147 | } |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 148 | ~MockGpu() override {} |
| 149 | bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const override { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 150 | return true; |
| 151 | } |
| 152 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 153 | bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget, |
| 154 | int left, int top, |
| 155 | int width, int height, |
| 156 | GrPixelConfig config, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 157 | size_t rowBytes) const override { return false; } |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 158 | void buildProgramDesc(GrProgramDesc*,const GrPrimitiveProcessor&, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 159 | const GrPipeline&, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 160 | const GrBatchTracker&) const override {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 161 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 162 | void discard(GrRenderTarget*) override {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -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 | 231c5fd | 2015-05-13 12:35:36 -0700 | [diff] [blame] | 173 | void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {} |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 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 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 188 | GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&, |
| 189 | GrWrapOwnership) override { return NULL; } |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 190 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 191 | GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&, |
| 192 | GrWrapOwnership) override { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 193 | return NULL; |
| 194 | } |
| 195 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 196 | GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) override { return NULL; } |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 197 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 198 | GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) override { return NULL; } |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 199 | |
| 200 | void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 201 | bool canIgnoreRect) 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 onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) override {} |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 204 | |
bsalomon | e64eb57 | 2015-05-07 11:35:55 -0700 | [diff] [blame] | 205 | void onDraw(const DrawArgs&, const GrNonInstancedVertices&) override {} |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 206 | |
| 207 | bool onReadPixels(GrRenderTarget* target, |
| 208 | int left, int top, int width, int height, |
| 209 | GrPixelConfig, |
| 210 | void* buffer, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 211 | size_t rowBytes) override { |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 212 | return false; |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 213 | } |
| 214 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 215 | bool onWriteTexturePixels(GrTexture* texture, |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 216 | int left, int top, int width, int height, |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 217 | GrPixelConfig config, const void* buffer, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 218 | size_t rowBytes) override { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 219 | return false; |
| 220 | } |
| 221 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 222 | void onResolveRenderTarget(GrRenderTarget* target) override { return; } |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 223 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 224 | bool createStencilAttachmentForRenderTarget(GrRenderTarget*, int width, int height) override { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 225 | return false; |
| 226 | } |
| 227 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 228 | bool attachStencilAttachmentToRenderTarget(GrStencilAttachment*, GrRenderTarget*) override { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 229 | return false; |
| 230 | } |
| 231 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 232 | void clearStencil(GrRenderTarget* target) override {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 233 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 234 | void didAddGpuTraceMarker() override {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 235 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 236 | void didRemoveGpuTraceMarker() override {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 237 | |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame^] | 238 | GrBackendObject createBackendTexture(void* pixels, int w, int h, |
| 239 | GrPixelConfig config) const override { return 0; } |
| 240 | bool isBackendTexture(GrBackendObject id) const override { return false; } |
| 241 | void deleteBackendTexture(GrBackendObject id) const override {} |
| 242 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 243 | typedef GrGpu INHERITED; |
| 244 | }; |
| 245 | |
| 246 | GrContext* GrContext::CreateMockContext() { |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 247 | GrContext* context = SkNEW(GrContext); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 248 | |
| 249 | context->initMockContext(); |
| 250 | return context; |
| 251 | } |
| 252 | |
| 253 | void GrContext::initMockContext() { |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 254 | GrContextOptions options; |
joshualitt | 8b08159 | 2015-06-01 16:17:03 -0700 | [diff] [blame] | 255 | options.fGeometryBufferMapThreshold = 0; |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 256 | SkASSERT(NULL == fGpu); |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 257 | fGpu = SkNEW_ARGS(MockGpu, (this, options)); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 258 | SkASSERT(fGpu); |
| 259 | this->initCommon(); |
| 260 | |
| 261 | // We delete these because we want to test the cache starting with zero resources. Also, none of |
| 262 | // these objects are required for any of tests that use this context. TODO: make stop allocating |
| 263 | // resources in the buffer pools. |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 264 | fDrawingMgr.abandon(); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 265 | } |