blob: 0b3d030cdb61244d061c6da745b01a9295f74e1e [file] [log] [blame]
commit-bot@chromium.org78a10782013-08-21 19:27:48 +00001
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
bsalomon3582d3e2015-02-13 14:20:05 -080011#include "GrGpuResourceCacheAccess.h"
joshualitt50408ad2014-11-03 12:31:14 -080012#include "GrInOrderDrawBuffer.h"
bsalomon0ea80f42015-02-11 10:49:59 -080013#include "GrResourceCache.h"
mtkleinb9eb4ac2015-02-02 18:26:03 -080014#include "SkString.h"
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000015
16void GrTestTarget::init(GrContext* ctx, GrDrawTarget* target) {
17 SkASSERT(!fContext);
18
19 fContext.reset(SkRef(ctx));
20 fDrawTarget.reset(SkRef(target));
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000021}
22
23void 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().
joshualitt50408ad2014-11-03 12:31:14 -080029 tar->init(this, fDrawBuffer);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000030}
31
32///////////////////////////////////////////////////////////////////////////////
33
34void GrContext::setMaxTextureSizeOverride(int maxTextureSizeOverride) {
35 fMaxTextureSizeOverride = maxTextureSizeOverride;
36}
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000037
38void GrContext::purgeAllUnlockedResources() {
bsalomon0ea80f42015-02-11 10:49:59 -080039 fResourceCache->purgeAllUnlocked();
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000040}
bsalomon33435572014-11-05 14:47:41 -080041
mtkleinb9eb4ac2015-02-02 18:26:03 -080042void GrContext::dumpCacheStats(SkString* out) const {
43#if GR_CACHE_STATS
bsalomon0ea80f42015-02-11 10:49:59 -080044 fResourceCache->dumpStats(out);
mtkleinb9eb4ac2015-02-02 18:26:03 -080045#endif
46}
47
48void GrContext::printCacheStats() const {
49 SkString out;
50 this->dumpCacheStats(&out);
kkinnunen297aaf92015-02-19 06:32:12 -080051 SkDebugf("%s", out.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -080052}
53
54void GrContext::dumpGpuStats(SkString* out) const {
55#if GR_GPU_STATS
56 return fGpu->stats()->dump(out);
57#endif
58}
59
60void GrContext::printGpuStats() const {
61 SkString out;
62 this->dumpGpuStats(&out);
kkinnunen297aaf92015-02-19 06:32:12 -080063 SkDebugf("%s", out.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -080064}
65
66#if GR_GPU_STATS
67void GrGpu::Stats::dump(SkString* out) {
68 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
69 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
bsalomonb12ea412015-02-02 21:19:50 -080070 out->appendf("Textures Created: %d\n", fTextureCreates);
71 out->appendf("Texture Uploads: %d\n", fTextureUploads);
egdaniel8dc7c3a2015-04-16 11:22:42 -070072 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
mtkleinb9eb4ac2015-02-02 18:26:03 -080073}
74#endif
75
76#if GR_CACHE_STATS
bsalomon0ea80f42015-02-11 10:49:59 -080077void GrResourceCache::dumpStats(SkString* out) const {
mtkleinb9eb4ac2015-02-02 18:26:03 -080078 this->validate();
79
bsalomonf320e042015-02-17 15:09:34 -080080 int locked = fNonpurgeableResources.count();
mtkleinb9eb4ac2015-02-02 18:26:03 -080081
bsalomonf320e042015-02-17 15:09:34 -080082 struct Stats {
83 int fScratch;
84 int fWrapped;
85 size_t fUnbudgetedSize;
mtkleinb9eb4ac2015-02-02 18:26:03 -080086
bsalomonf320e042015-02-17 15:09:34 -080087 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 }
mtkleinb9eb4ac2015-02-02 18:26:03 -080099 }
bsalomonf320e042015-02-17 15:09:34 -0800100 };
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));
mtkleinb9eb4ac2015-02-02 18:26:03 -0800109 }
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",
bsalomon52057c82015-02-23 12:12:59 -0800117 this->getResourceCount(), fBudgetedCount, stats.fWrapped, locked, stats.fScratch,
118 countUtilization, fHighWaterCount);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800119 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
bsalomonf320e042015-02-17 15:09:34 -0800120 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
121 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
mtkleinb9eb4ac2015-02-02 18:26:03 -0800122}
123
124#endif
125
bsalomonddf30e62015-02-19 11:38:44 -0800126///////////////////////////////////////////////////////////////////////////////
127
128void GrResourceCache::changeTimestamp(uint32_t newTimestamp) { fTimestamp = newTimestamp; }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800129
bsalomon33435572014-11-05 14:47:41 -0800130///////////////////////////////////////////////////////////////////////////////
131// Code for the mock context. It's built on a mock GrGpu class that does nothing.
132////
133
bsalomon33435572014-11-05 14:47:41 -0800134#include "GrInOrderDrawBuffer.h"
135#include "GrGpu.h"
136
egdaniel8dd688b2015-01-22 10:16:09 -0800137class GrPipeline;
joshualittd53a8272014-11-10 16:03:14 -0800138
bsalomon33435572014-11-05 14:47:41 -0800139class MockGpu : public GrGpu {
140public:
141 MockGpu(GrContext* context) : INHERITED(context) { fCaps.reset(SkNEW(GrDrawTargetCaps)); }
mtklein36352bf2015-03-25 18:17:31 -0700142 ~MockGpu() override {}
143 bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const override {
bsalomon33435572014-11-05 14:47:41 -0800144 return true;
145 }
146
bsalomonf90a02b2014-11-26 12:28:00 -0800147 bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
148 int left, int top,
149 int width, int height,
150 GrPixelConfig config,
mtklein36352bf2015-03-25 18:17:31 -0700151 size_t rowBytes) const override { return false; }
joshualitt873ad0e2015-01-20 09:08:51 -0800152 void buildProgramDesc(GrProgramDesc*,const GrPrimitiveProcessor&,
egdaniel8dd688b2015-01-22 10:16:09 -0800153 const GrPipeline&,
mtklein36352bf2015-03-25 18:17:31 -0700154 const GrBatchTracker&) const override {}
bsalomon33435572014-11-05 14:47:41 -0800155
mtklein36352bf2015-03-25 18:17:31 -0700156 void discard(GrRenderTarget*) override {}
bsalomon33435572014-11-05 14:47:41 -0800157
bsalomonf90a02b2014-11-26 12:28:00 -0800158 bool canCopySurface(const GrSurface* dst,
159 const GrSurface* src,
160 const SkIRect& srcRect,
mtklein36352bf2015-03-25 18:17:31 -0700161 const SkIPoint& dstPoint) override { return false; };
joshualitt3322fa42014-11-07 08:48:51 -0800162
bsalomonf90a02b2014-11-26 12:28:00 -0800163 bool copySurface(GrSurface* dst,
164 GrSurface* src,
165 const SkIRect& srcRect,
mtklein36352bf2015-03-25 18:17:31 -0700166 const SkIPoint& dstPoint) override { return false; };
bsalomonf90a02b2014-11-26 12:28:00 -0800167
mtklein36352bf2015-03-25 18:17:31 -0700168 bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) override {
bsalomonf90a02b2014-11-26 12:28:00 -0800169 return false;
170 }
joshualitt3322fa42014-11-07 08:48:51 -0800171
cdalton231c5fd2015-05-13 12:35:36 -0700172 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {}
cdalton9954bc32015-04-29 14:17:00 -0700173
bsalomon33435572014-11-05 14:47:41 -0800174private:
mtklein36352bf2015-03-25 18:17:31 -0700175 void onResetContext(uint32_t resetBits) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800176
egdanielb0e1be22015-04-22 13:27:39 -0700177 GrTexture* onCreateTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle lifeCycle,
178 const void* srcData, size_t rowBytes) override {
bsalomon33435572014-11-05 14:47:41 -0800179 return NULL;
180 }
181
egdanielb0e1be22015-04-22 13:27:39 -0700182 GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle,
mtklein36352bf2015-03-25 18:17:31 -0700183 const void* srcData) override {
bsalomon33435572014-11-05 14:47:41 -0800184 return NULL;
185 }
186
mtklein36352bf2015-03-25 18:17:31 -0700187 GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) override { return NULL; }
bsalomonf90a02b2014-11-26 12:28:00 -0800188
mtklein36352bf2015-03-25 18:17:31 -0700189 GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) override {
bsalomon33435572014-11-05 14:47:41 -0800190 return NULL;
191 }
192
mtklein36352bf2015-03-25 18:17:31 -0700193 GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) override { return NULL; }
bsalomonf90a02b2014-11-26 12:28:00 -0800194
mtklein36352bf2015-03-25 18:17:31 -0700195 GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) override { return NULL; }
bsalomonf90a02b2014-11-26 12:28:00 -0800196
197 void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color,
mtklein36352bf2015-03-25 18:17:31 -0700198 bool canIgnoreRect) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800199
mtklein36352bf2015-03-25 18:17:31 -0700200 void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800201
bsalomone64eb572015-05-07 11:35:55 -0700202 void onDraw(const DrawArgs&, const GrNonInstancedVertices&) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800203
mtklein36352bf2015-03-25 18:17:31 -0700204 void onStencilPath(const GrPath* path, const StencilPathState& state) override {}
bsalomond95263c2014-12-16 13:05:12 -0800205
mtklein36352bf2015-03-25 18:17:31 -0700206 void onDrawPath(const DrawArgs&, const GrPath*, const GrStencilSettings&) override {}
bsalomond95263c2014-12-16 13:05:12 -0800207
joshualitt873ad0e2015-01-20 09:08:51 -0800208 void onDrawPaths(const DrawArgs&,
bsalomond95263c2014-12-16 13:05:12 -0800209 const GrPathRange*,
210 const void* indices,
211 GrDrawTarget::PathIndexType,
212 const float transformValues[],
213 GrDrawTarget::PathTransformType,
214 int count,
mtklein36352bf2015-03-25 18:17:31 -0700215 const GrStencilSettings&) override {}
bsalomond95263c2014-12-16 13:05:12 -0800216
bsalomonf90a02b2014-11-26 12:28:00 -0800217 bool onReadPixels(GrRenderTarget* target,
218 int left, int top, int width, int height,
219 GrPixelConfig,
220 void* buffer,
mtklein36352bf2015-03-25 18:17:31 -0700221 size_t rowBytes) override {
bsalomonf90a02b2014-11-26 12:28:00 -0800222 return false;
bsalomon33435572014-11-05 14:47:41 -0800223 }
224
bsalomonf90a02b2014-11-26 12:28:00 -0800225 bool onWriteTexturePixels(GrTexture* texture,
bsalomon33435572014-11-05 14:47:41 -0800226 int left, int top, int width, int height,
bsalomonf90a02b2014-11-26 12:28:00 -0800227 GrPixelConfig config, const void* buffer,
mtklein36352bf2015-03-25 18:17:31 -0700228 size_t rowBytes) override {
bsalomon33435572014-11-05 14:47:41 -0800229 return false;
230 }
231
mtklein36352bf2015-03-25 18:17:31 -0700232 void onResolveRenderTarget(GrRenderTarget* target) override { return; }
bsalomonf90a02b2014-11-26 12:28:00 -0800233
egdaniel8dc7c3a2015-04-16 11:22:42 -0700234 bool createStencilAttachmentForRenderTarget(GrRenderTarget*, int width, int height) override {
bsalomon33435572014-11-05 14:47:41 -0800235 return false;
236 }
237
egdaniel8dc7c3a2015-04-16 11:22:42 -0700238 bool attachStencilAttachmentToRenderTarget(GrStencilAttachment*, GrRenderTarget*) override {
bsalomon33435572014-11-05 14:47:41 -0800239 return false;
240 }
241
mtklein36352bf2015-03-25 18:17:31 -0700242 void clearStencil(GrRenderTarget* target) override {}
bsalomon33435572014-11-05 14:47:41 -0800243
mtklein36352bf2015-03-25 18:17:31 -0700244 void didAddGpuTraceMarker() override {}
bsalomon33435572014-11-05 14:47:41 -0800245
mtklein36352bf2015-03-25 18:17:31 -0700246 void didRemoveGpuTraceMarker() override {}
bsalomon33435572014-11-05 14:47:41 -0800247
248 typedef GrGpu INHERITED;
249};
250
251GrContext* GrContext::CreateMockContext() {
252 GrContext* context = SkNEW_ARGS(GrContext, (Options()));
253
254 context->initMockContext();
255 return context;
256}
257
258void GrContext::initMockContext() {
259 SkASSERT(NULL == fGpu);
260 fGpu = SkNEW_ARGS(MockGpu, (this));
261 SkASSERT(fGpu);
262 this->initCommon();
263
264 // We delete these because we want to test the cache starting with zero resources. Also, none of
265 // these objects are required for any of tests that use this context. TODO: make stop allocating
266 // resources in the buffer pools.
267 SkDELETE(fDrawBuffer);
bsalomon33435572014-11-05 14:47:41 -0800268 fDrawBuffer = NULL;
robertphillipse40d3972015-05-07 09:51:43 -0700269
bsalomon33435572014-11-05 14:47:41 -0800270}