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 | 3982602 | 2015-07-23 08:07:21 -0700 | [diff] [blame^] | 153 | bool getReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight, size_t rowBytes, |
| 154 | GrPixelConfig readConfig, DrawPreference*, |
| 155 | ReadPixelTempDrawInfo*) override { return false; } |
| 156 | |
joshualitt | 873ad0e | 2015-01-20 09:08:51 -0800 | [diff] [blame] | 157 | void buildProgramDesc(GrProgramDesc*,const GrPrimitiveProcessor&, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 158 | const GrPipeline&, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 159 | const GrBatchTracker&) const override {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 160 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 161 | void discard(GrRenderTarget*) override {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -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, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 166 | const SkIPoint& dstPoint) override { return false; }; |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 167 | |
joshualitt | 1c73548 | 2015-07-13 08:08:25 -0700 | [diff] [blame] | 168 | bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const override { |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 169 | return false; |
| 170 | } |
joshualitt | 3322fa4 | 2014-11-07 08:48:51 -0800 | [diff] [blame] | 171 | |
cdalton | 231c5fd | 2015-05-13 12:35:36 -0700 | [diff] [blame] | 172 | void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {} |
cdalton | 9954bc3 | 2015-04-29 14:17:00 -0700 | [diff] [blame] | 173 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 174 | private: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 175 | void onResetContext(uint32_t resetBits) override {} |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 176 | |
egdaniel | b0e1be2 | 2015-04-22 13:27:39 -0700 | [diff] [blame] | 177 | GrTexture* onCreateTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle lifeCycle, |
| 178 | const void* srcData, size_t rowBytes) override { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 179 | return NULL; |
| 180 | } |
| 181 | |
egdaniel | b0e1be2 | 2015-04-22 13:27:39 -0700 | [diff] [blame] | 182 | GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 183 | const void* srcData) override { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 184 | return NULL; |
| 185 | } |
| 186 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 187 | GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&, |
| 188 | GrWrapOwnership) override { return NULL; } |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 189 | |
bsalomon | 6dc6f5f | 2015-06-18 09:12:16 -0700 | [diff] [blame] | 190 | GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&, |
| 191 | GrWrapOwnership) override { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 192 | return NULL; |
| 193 | } |
| 194 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 195 | GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) override { return NULL; } |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 196 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 197 | GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) override { return NULL; } |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 198 | |
| 199 | void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 200 | bool canIgnoreRect) override {} |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 201 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 202 | void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) override {} |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 203 | |
bsalomon | e64eb57 | 2015-05-07 11:35:55 -0700 | [diff] [blame] | 204 | void onDraw(const DrawArgs&, const GrNonInstancedVertices&) override {} |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 205 | |
| 206 | bool onReadPixels(GrRenderTarget* target, |
| 207 | int left, int top, int width, int height, |
| 208 | GrPixelConfig, |
| 209 | void* buffer, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 210 | size_t rowBytes) override { |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 211 | return false; |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 212 | } |
| 213 | |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 214 | bool onWriteTexturePixels(GrTexture* texture, |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 215 | int left, int top, int width, int height, |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 216 | GrPixelConfig config, const void* buffer, |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 217 | size_t rowBytes) override { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 218 | return false; |
| 219 | } |
| 220 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 221 | void onResolveRenderTarget(GrRenderTarget* target) override { return; } |
bsalomon | f90a02b | 2014-11-26 12:28:00 -0800 | [diff] [blame] | 222 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 223 | bool createStencilAttachmentForRenderTarget(GrRenderTarget*, int width, int height) override { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 224 | return false; |
| 225 | } |
| 226 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 227 | bool attachStencilAttachmentToRenderTarget(GrStencilAttachment*, GrRenderTarget*) override { |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 228 | return false; |
| 229 | } |
| 230 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 231 | void clearStencil(GrRenderTarget* target) override {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 232 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 233 | void didAddGpuTraceMarker() override {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 234 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 235 | void didRemoveGpuTraceMarker() override {} |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 236 | |
jvanverth | 8895792 | 2015-07-14 11:02:52 -0700 | [diff] [blame] | 237 | GrBackendObject createTestingOnlyBackendTexture(void* pixels, int w, int h, |
| 238 | GrPixelConfig config) const override { |
| 239 | return 0; |
| 240 | } |
| 241 | bool isTestingOnlyBackendTexture(GrBackendObject id) const override { return false; } |
| 242 | void deleteTestingOnlyBackendTexture(GrBackendObject id) const override {} |
jvanverth | 672bb7f | 2015-07-13 07:19:57 -0700 | [diff] [blame] | 243 | |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 244 | typedef GrGpu INHERITED; |
| 245 | }; |
| 246 | |
| 247 | GrContext* GrContext::CreateMockContext() { |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 248 | GrContext* context = SkNEW(GrContext); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 249 | |
| 250 | context->initMockContext(); |
| 251 | return context; |
| 252 | } |
| 253 | |
| 254 | void GrContext::initMockContext() { |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 255 | GrContextOptions options; |
joshualitt | 8b08159 | 2015-06-01 16:17:03 -0700 | [diff] [blame] | 256 | options.fGeometryBufferMapThreshold = 0; |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 257 | SkASSERT(NULL == fGpu); |
bsalomon | 682c269 | 2015-05-22 14:01:46 -0700 | [diff] [blame] | 258 | fGpu = SkNEW_ARGS(MockGpu, (this, options)); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 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. |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 265 | fDrawingMgr.abandon(); |
bsalomon | 3343557 | 2014-11-05 14:47:41 -0800 | [diff] [blame] | 266 | } |