blob: 9ddc97d8341828a60df322e14f6942702800ddeb [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));
21
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000022 SkNEW_IN_TLAZY(&fACR, GrDrawTarget::AutoClipRestore, (target));
23 SkNEW_IN_TLAZY(&fAGP, GrDrawTarget::AutoGeometryPush, (target));
24}
25
26void 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().
joshualitt50408ad2014-11-03 12:31:14 -080032 tar->init(this, fDrawBuffer);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000033}
34
35///////////////////////////////////////////////////////////////////////////////
36
37void GrContext::setMaxTextureSizeOverride(int maxTextureSizeOverride) {
38 fMaxTextureSizeOverride = maxTextureSizeOverride;
39}
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000040
41void GrContext::purgeAllUnlockedResources() {
bsalomon0ea80f42015-02-11 10:49:59 -080042 fResourceCache->purgeAllUnlocked();
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000043}
bsalomon33435572014-11-05 14:47:41 -080044
mtkleinb9eb4ac2015-02-02 18:26:03 -080045void GrContext::dumpCacheStats(SkString* out) const {
46#if GR_CACHE_STATS
bsalomon0ea80f42015-02-11 10:49:59 -080047 fResourceCache->dumpStats(out);
mtkleinb9eb4ac2015-02-02 18:26:03 -080048#endif
49}
50
51void GrContext::printCacheStats() const {
52 SkString out;
53 this->dumpCacheStats(&out);
kkinnunen297aaf92015-02-19 06:32:12 -080054 SkDebugf("%s", out.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -080055}
56
57void GrContext::dumpGpuStats(SkString* out) const {
58#if GR_GPU_STATS
59 return fGpu->stats()->dump(out);
60#endif
61}
62
63void GrContext::printGpuStats() const {
64 SkString out;
65 this->dumpGpuStats(&out);
kkinnunen297aaf92015-02-19 06:32:12 -080066 SkDebugf("%s", out.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -080067}
68
69#if GR_GPU_STATS
70void GrGpu::Stats::dump(SkString* out) {
71 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
72 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
bsalomonb12ea412015-02-02 21:19:50 -080073 out->appendf("Textures Created: %d\n", fTextureCreates);
74 out->appendf("Texture Uploads: %d\n", fTextureUploads);
mtkleinb9eb4ac2015-02-02 18:26:03 -080075}
76#endif
77
78#if GR_CACHE_STATS
bsalomon0ea80f42015-02-11 10:49:59 -080079void GrResourceCache::dumpStats(SkString* out) const {
mtkleinb9eb4ac2015-02-02 18:26:03 -080080 this->validate();
81
bsalomonf320e042015-02-17 15:09:34 -080082 int locked = fNonpurgeableResources.count();
mtkleinb9eb4ac2015-02-02 18:26:03 -080083
bsalomonf320e042015-02-17 15:09:34 -080084 struct Stats {
85 int fScratch;
86 int fWrapped;
87 size_t fUnbudgetedSize;
mtkleinb9eb4ac2015-02-02 18:26:03 -080088
bsalomonf320e042015-02-17 15:09:34 -080089 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 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800101 }
bsalomonf320e042015-02-17 15:09:34 -0800102 };
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));
mtkleinb9eb4ac2015-02-02 18:26:03 -0800111 }
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",
bsalomonf320e042015-02-17 15:09:34 -0800119 fCount, fBudgetedCount, stats.fWrapped, locked, stats.fScratch, countUtilization,
mtkleinb9eb4ac2015-02-02 18:26:03 -0800120 fHighWaterCount);
121 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
bsalomonf320e042015-02-17 15:09:34 -0800122 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
123 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
mtkleinb9eb4ac2015-02-02 18:26:03 -0800124}
125
126#endif
127
bsalomonddf30e62015-02-19 11:38:44 -0800128///////////////////////////////////////////////////////////////////////////////
129
130void GrResourceCache::changeTimestamp(uint32_t newTimestamp) { fTimestamp = newTimestamp; }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800131
bsalomon33435572014-11-05 14:47:41 -0800132///////////////////////////////////////////////////////////////////////////////
133// Code for the mock context. It's built on a mock GrGpu class that does nothing.
134////
135
136#include "GrBufferAllocPool.h"
137#include "GrInOrderDrawBuffer.h"
138#include "GrGpu.h"
139
egdaniel8dd688b2015-01-22 10:16:09 -0800140class GrPipeline;
joshualittd53a8272014-11-10 16:03:14 -0800141
bsalomon33435572014-11-05 14:47:41 -0800142class MockGpu : public GrGpu {
143public:
144 MockGpu(GrContext* context) : INHERITED(context) { fCaps.reset(SkNEW(GrDrawTargetCaps)); }
bsalomonf90a02b2014-11-26 12:28:00 -0800145 ~MockGpu() SK_OVERRIDE {}
146 bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const SK_OVERRIDE {
bsalomon33435572014-11-05 14:47:41 -0800147 return true;
148 }
149
bsalomonf90a02b2014-11-26 12:28:00 -0800150 bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
151 int left, int top,
152 int width, int height,
153 GrPixelConfig config,
154 size_t rowBytes) const SK_OVERRIDE { return false; }
joshualitt873ad0e2015-01-20 09:08:51 -0800155 void buildProgramDesc(GrProgramDesc*,const GrPrimitiveProcessor&,
egdaniel8dd688b2015-01-22 10:16:09 -0800156 const GrPipeline&,
joshualitt873ad0e2015-01-20 09:08:51 -0800157 const GrBatchTracker&) const SK_OVERRIDE {}
bsalomon33435572014-11-05 14:47:41 -0800158
bsalomonf90a02b2014-11-26 12:28:00 -0800159 void discard(GrRenderTarget*) SK_OVERRIDE {}
bsalomon33435572014-11-05 14:47:41 -0800160
bsalomonf90a02b2014-11-26 12:28:00 -0800161 bool canCopySurface(const GrSurface* dst,
162 const GrSurface* src,
163 const SkIRect& srcRect,
164 const SkIPoint& dstPoint) SK_OVERRIDE { return false; };
joshualitt3322fa42014-11-07 08:48:51 -0800165
bsalomonf90a02b2014-11-26 12:28:00 -0800166 bool copySurface(GrSurface* dst,
167 GrSurface* src,
168 const SkIRect& srcRect,
169 const SkIPoint& dstPoint) SK_OVERRIDE { return false; };
170
171 bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) SK_OVERRIDE {
172 return false;
173 }
joshualitt3322fa42014-11-07 08:48:51 -0800174
bsalomon33435572014-11-05 14:47:41 -0800175private:
bsalomonf90a02b2014-11-26 12:28:00 -0800176 void onResetContext(uint32_t resetBits) SK_OVERRIDE {}
177
bsalomon5236cf42015-01-14 10:42:08 -0800178 GrTexture* onCreateTexture(const GrSurfaceDesc& desc, bool budgeted, const void* srcData,
bsalomonf90a02b2014-11-26 12:28:00 -0800179 size_t rowBytes) SK_OVERRIDE {
bsalomon33435572014-11-05 14:47:41 -0800180 return NULL;
181 }
182
bsalomon5236cf42015-01-14 10:42:08 -0800183 GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, bool budgeted,
bsalomonf90a02b2014-11-26 12:28:00 -0800184 const void* srcData) SK_OVERRIDE {
bsalomon33435572014-11-05 14:47:41 -0800185 return NULL;
186 }
187
bsalomonf90a02b2014-11-26 12:28:00 -0800188 GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) SK_OVERRIDE { return NULL; }
189
190 GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) SK_OVERRIDE {
bsalomon33435572014-11-05 14:47:41 -0800191 return NULL;
192 }
193
bsalomonf90a02b2014-11-26 12:28:00 -0800194 GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) SK_OVERRIDE { return NULL; }
195
196 GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) SK_OVERRIDE { return NULL; }
197
198 void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color,
199 bool canIgnoreRect) SK_OVERRIDE {}
200
201 void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) SK_OVERRIDE {}
202
joshualitt873ad0e2015-01-20 09:08:51 -0800203 void onDraw(const DrawArgs&, const GrDrawTarget::DrawInfo&) SK_OVERRIDE {}
bsalomonf90a02b2014-11-26 12:28:00 -0800204
bsalomon3e791242014-12-17 13:43:13 -0800205 void onStencilPath(const GrPath* path, const StencilPathState& state) SK_OVERRIDE {}
bsalomond95263c2014-12-16 13:05:12 -0800206
joshualitt873ad0e2015-01-20 09:08:51 -0800207 void onDrawPath(const DrawArgs&, const GrPath*, const GrStencilSettings&) SK_OVERRIDE {}
bsalomond95263c2014-12-16 13:05:12 -0800208
joshualitt873ad0e2015-01-20 09:08:51 -0800209 void onDrawPaths(const DrawArgs&,
bsalomond95263c2014-12-16 13:05:12 -0800210 const GrPathRange*,
211 const void* indices,
212 GrDrawTarget::PathIndexType,
213 const float transformValues[],
214 GrDrawTarget::PathTransformType,
215 int count,
216 const GrStencilSettings&) SK_OVERRIDE {}
217
bsalomonf90a02b2014-11-26 12:28:00 -0800218 bool onReadPixels(GrRenderTarget* target,
219 int left, int top, int width, int height,
220 GrPixelConfig,
221 void* buffer,
222 size_t rowBytes) SK_OVERRIDE {
223 return false;
bsalomon33435572014-11-05 14:47:41 -0800224 }
225
bsalomonf90a02b2014-11-26 12:28:00 -0800226 bool onWriteTexturePixels(GrTexture* texture,
bsalomon33435572014-11-05 14:47:41 -0800227 int left, int top, int width, int height,
bsalomonf90a02b2014-11-26 12:28:00 -0800228 GrPixelConfig config, const void* buffer,
229 size_t rowBytes) SK_OVERRIDE {
bsalomon33435572014-11-05 14:47:41 -0800230 return false;
231 }
232
bsalomonf90a02b2014-11-26 12:28:00 -0800233 void onResolveRenderTarget(GrRenderTarget* target) SK_OVERRIDE { return; }
234
bsalomon02a44a42015-02-19 09:09:00 -0800235 bool createStencilBufferForRenderTarget(GrRenderTarget*, int width, int height) SK_OVERRIDE {
bsalomon33435572014-11-05 14:47:41 -0800236 return false;
237 }
238
bsalomonf90a02b2014-11-26 12:28:00 -0800239 bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) SK_OVERRIDE {
bsalomon33435572014-11-05 14:47:41 -0800240 return false;
241 }
242
bsalomonf90a02b2014-11-26 12:28:00 -0800243 void clearStencil(GrRenderTarget* target) SK_OVERRIDE {}
bsalomon33435572014-11-05 14:47:41 -0800244
bsalomond95263c2014-12-16 13:05:12 -0800245 void didAddGpuTraceMarker() SK_OVERRIDE {}
bsalomon33435572014-11-05 14:47:41 -0800246
bsalomond95263c2014-12-16 13:05:12 -0800247 void didRemoveGpuTraceMarker() SK_OVERRIDE {}
bsalomon33435572014-11-05 14:47:41 -0800248
249 typedef GrGpu INHERITED;
250};
251
252GrContext* GrContext::CreateMockContext() {
253 GrContext* context = SkNEW_ARGS(GrContext, (Options()));
254
255 context->initMockContext();
256 return context;
257}
258
259void 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}