blob: fd2908eb766e2a519d21d5a6cb866c960cdda9cd [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"
joshualittb542bae2015-07-28 09:58:39 -070010
11#include "GrBufferedDrawTarget.h"
bsalomon682c2692015-05-22 14:01:46 -070012#include "GrContextOptions.h"
bsalomon3582d3e2015-02-13 14:20:05 -080013#include "GrGpuResourceCacheAccess.h"
bsalomon0ea80f42015-02-11 10:49:59 -080014#include "GrResourceCache.h"
mtkleinb9eb4ac2015-02-02 18:26:03 -080015#include "SkString.h"
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000016
jvanverth672bb7f2015-07-13 07:19:57 -070017void GrTestTarget::init(GrContext* ctx, GrDrawTarget* target) {
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000018 SkASSERT(!fContext);
19
20 fContext.reset(SkRef(ctx));
21 fDrawTarget.reset(SkRef(target));
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000022}
23
24void GrContext::getTestTarget(GrTestTarget* tar) {
25 this->flush();
26 // We could create a proxy GrDrawTarget that passes through to fGpu until ~GrTextTarget() and
27 // then disconnects. This would help prevent test writers from mixing using the returned
28 // GrDrawTarget and regular drawing. We could also assert or fail in GrContext drawing methods
29 // until ~GrTestTarget().
jvanverth672bb7f2015-07-13 07:19:57 -070030 tar->init(this, fDrawingMgr.fDrawTarget);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000031}
32
33///////////////////////////////////////////////////////////////////////////////
34
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000035void GrContext::purgeAllUnlockedResources() {
bsalomon0ea80f42015-02-11 10:49:59 -080036 fResourceCache->purgeAllUnlocked();
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000037}
bsalomon33435572014-11-05 14:47:41 -080038
mtkleinb9eb4ac2015-02-02 18:26:03 -080039void GrContext::dumpCacheStats(SkString* out) const {
40#if GR_CACHE_STATS
bsalomon0ea80f42015-02-11 10:49:59 -080041 fResourceCache->dumpStats(out);
mtkleinb9eb4ac2015-02-02 18:26:03 -080042#endif
43}
44
45void GrContext::printCacheStats() const {
46 SkString out;
47 this->dumpCacheStats(&out);
kkinnunen297aaf92015-02-19 06:32:12 -080048 SkDebugf("%s", out.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -080049}
50
51void GrContext::dumpGpuStats(SkString* out) const {
52#if GR_GPU_STATS
53 return fGpu->stats()->dump(out);
54#endif
55}
56
57void GrContext::printGpuStats() const {
58 SkString out;
59 this->dumpGpuStats(&out);
kkinnunen297aaf92015-02-19 06:32:12 -080060 SkDebugf("%s", out.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -080061}
62
63#if GR_GPU_STATS
64void GrGpu::Stats::dump(SkString* out) {
65 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
66 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
bsalomonb12ea412015-02-02 21:19:50 -080067 out->appendf("Textures Created: %d\n", fTextureCreates);
68 out->appendf("Texture Uploads: %d\n", fTextureUploads);
egdaniel8dc7c3a2015-04-16 11:22:42 -070069 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
mtkleinb9eb4ac2015-02-02 18:26:03 -080070}
71#endif
72
73#if GR_CACHE_STATS
bsalomon0ea80f42015-02-11 10:49:59 -080074void GrResourceCache::dumpStats(SkString* out) const {
mtkleinb9eb4ac2015-02-02 18:26:03 -080075 this->validate();
76
bsalomonf320e042015-02-17 15:09:34 -080077 int locked = fNonpurgeableResources.count();
mtkleinb9eb4ac2015-02-02 18:26:03 -080078
bsalomonf320e042015-02-17 15:09:34 -080079 struct Stats {
80 int fScratch;
bsalomon6dc6f5f2015-06-18 09:12:16 -070081 int fExternal;
82 int fBorrowed;
83 int fAdopted;
bsalomonf320e042015-02-17 15:09:34 -080084 size_t fUnbudgetedSize;
mtkleinb9eb4ac2015-02-02 18:26:03 -080085
bsalomon6dc6f5f2015-06-18 09:12:16 -070086 Stats() : fScratch(0), fExternal(0), fBorrowed(0), fAdopted(0), fUnbudgetedSize(0) {}
bsalomonf320e042015-02-17 15:09:34 -080087
88 void update(GrGpuResource* resource) {
89 if (resource->cacheAccess().isScratch()) {
90 ++fScratch;
91 }
bsalomon6dc6f5f2015-06-18 09:12:16 -070092 if (resource->cacheAccess().isExternal()) {
93 ++fExternal;
94 }
95 if (resource->cacheAccess().isBorrowed()) {
96 ++fBorrowed;
97 }
98 if (resource->cacheAccess().isAdopted()) {
99 ++fAdopted;
bsalomonf320e042015-02-17 15:09:34 -0800100 }
101 if (!resource->resourcePriv().isBudgeted()) {
102 fUnbudgetedSize += resource->gpuMemorySize();
103 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800104 }
bsalomonf320e042015-02-17 15:09:34 -0800105 };
106
107 Stats stats;
108
109 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
110 stats.update(fNonpurgeableResources[i]);
111 }
112 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
113 stats.update(fPurgeableQueue.at(i));
mtkleinb9eb4ac2015-02-02 18:26:03 -0800114 }
115
116 float countUtilization = (100.f * fBudgetedCount) / fMaxCount;
117 float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
118
119 out->appendf("Budget: %d items %d bytes\n", fMaxCount, (int)fMaxBytes);
120 out->appendf("\t\tEntry Count: current %d"
bsalomon6dc6f5f2015-06-18 09:12:16 -0700121 " (%d budgeted, %d external(%d borrowed, %d adopted), %d locked, %d scratch %.2g%% full), high %d\n",
122 this->getResourceCount(), fBudgetedCount, stats.fExternal, stats.fBorrowed,
123 stats.fAdopted, locked, stats.fScratch, countUtilization, fHighWaterCount);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800124 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
bsalomonf320e042015-02-17 15:09:34 -0800125 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
126 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
mtkleinb9eb4ac2015-02-02 18:26:03 -0800127}
128
129#endif
130
bsalomonddf30e62015-02-19 11:38:44 -0800131///////////////////////////////////////////////////////////////////////////////
132
133void GrResourceCache::changeTimestamp(uint32_t newTimestamp) { fTimestamp = newTimestamp; }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800134
bsalomon33435572014-11-05 14:47:41 -0800135///////////////////////////////////////////////////////////////////////////////
136// Code for the mock context. It's built on a mock GrGpu class that does nothing.
137////
138
bsalomon33435572014-11-05 14:47:41 -0800139#include "GrGpu.h"
140
egdaniel8dd688b2015-01-22 10:16:09 -0800141class GrPipeline;
joshualittd53a8272014-11-10 16:03:14 -0800142
bsalomon33435572014-11-05 14:47:41 -0800143class MockGpu : public GrGpu {
144public:
bsalomon682c2692015-05-22 14:01:46 -0700145 MockGpu(GrContext* context, const GrContextOptions& options) : INHERITED(context) {
146 fCaps.reset(SkNEW_ARGS(GrCaps, (options)));
147 }
mtklein36352bf2015-03-25 18:17:31 -0700148 ~MockGpu() override {}
bsalomon33435572014-11-05 14:47:41 -0800149
bsalomonf0674512015-07-28 13:26:15 -0700150 bool onGetReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight, size_t rowBytes,
151 GrPixelConfig readConfig, DrawPreference*,
152 ReadPixelTempDrawInfo*) override { return false; }
153
154 bool onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height, size_t rowBytes,
155 GrPixelConfig srcConfig, DrawPreference*,
156 WritePixelTempDrawInfo*) override { return false; }
bsalomon39826022015-07-23 08:07:21 -0700157
joshualitt873ad0e2015-01-20 09:08:51 -0800158 void buildProgramDesc(GrProgramDesc*,const GrPrimitiveProcessor&,
egdaniel8dd688b2015-01-22 10:16:09 -0800159 const GrPipeline&,
mtklein36352bf2015-03-25 18:17:31 -0700160 const GrBatchTracker&) const override {}
bsalomon33435572014-11-05 14:47:41 -0800161
mtklein36352bf2015-03-25 18:17:31 -0700162 void discard(GrRenderTarget*) override {}
bsalomon33435572014-11-05 14:47:41 -0800163
bsalomonf90a02b2014-11-26 12:28:00 -0800164 bool copySurface(GrSurface* dst,
165 GrSurface* src,
166 const SkIRect& srcRect,
mtklein36352bf2015-03-25 18:17:31 -0700167 const SkIPoint& dstPoint) override { return false; };
bsalomonf90a02b2014-11-26 12:28:00 -0800168
joshualitt1c735482015-07-13 08:08:25 -0700169 bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const override {
bsalomonf90a02b2014-11-26 12:28:00 -0800170 return false;
171 }
joshualitt3322fa42014-11-07 08:48:51 -0800172
cdalton231c5fd2015-05-13 12:35:36 -0700173 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {}
cdalton9954bc32015-04-29 14:17:00 -0700174
bsalomon33435572014-11-05 14:47:41 -0800175private:
mtklein36352bf2015-03-25 18:17:31 -0700176 void onResetContext(uint32_t resetBits) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800177
egdanielb0e1be22015-04-22 13:27:39 -0700178 GrTexture* onCreateTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle lifeCycle,
179 const void* srcData, size_t rowBytes) override {
bsalomon33435572014-11-05 14:47:41 -0800180 return NULL;
181 }
182
egdanielb0e1be22015-04-22 13:27:39 -0700183 GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle,
mtklein36352bf2015-03-25 18:17:31 -0700184 const void* srcData) override {
bsalomon33435572014-11-05 14:47:41 -0800185 return NULL;
186 }
187
bsalomon6dc6f5f2015-06-18 09:12:16 -0700188 GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&,
189 GrWrapOwnership) override { return NULL; }
bsalomonf90a02b2014-11-26 12:28:00 -0800190
bsalomon6dc6f5f2015-06-18 09:12:16 -0700191 GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&,
192 GrWrapOwnership) override {
bsalomon33435572014-11-05 14:47:41 -0800193 return NULL;
194 }
195
mtklein36352bf2015-03-25 18:17:31 -0700196 GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) override { return NULL; }
bsalomonf90a02b2014-11-26 12:28:00 -0800197
mtklein36352bf2015-03-25 18:17:31 -0700198 GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) override { return NULL; }
bsalomonf90a02b2014-11-26 12:28:00 -0800199
200 void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color,
mtklein36352bf2015-03-25 18:17:31 -0700201 bool canIgnoreRect) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800202
mtklein36352bf2015-03-25 18:17:31 -0700203 void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800204
bsalomone64eb572015-05-07 11:35:55 -0700205 void onDraw(const DrawArgs&, const GrNonInstancedVertices&) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800206
207 bool onReadPixels(GrRenderTarget* target,
208 int left, int top, int width, int height,
209 GrPixelConfig,
210 void* buffer,
mtklein36352bf2015-03-25 18:17:31 -0700211 size_t rowBytes) override {
bsalomonf90a02b2014-11-26 12:28:00 -0800212 return false;
bsalomon33435572014-11-05 14:47:41 -0800213 }
214
bsalomonf90a02b2014-11-26 12:28:00 -0800215 bool onWriteTexturePixels(GrTexture* texture,
bsalomon33435572014-11-05 14:47:41 -0800216 int left, int top, int width, int height,
bsalomonf90a02b2014-11-26 12:28:00 -0800217 GrPixelConfig config, const void* buffer,
mtklein36352bf2015-03-25 18:17:31 -0700218 size_t rowBytes) override {
bsalomon33435572014-11-05 14:47:41 -0800219 return false;
220 }
221
mtklein36352bf2015-03-25 18:17:31 -0700222 void onResolveRenderTarget(GrRenderTarget* target) override { return; }
bsalomonf90a02b2014-11-26 12:28:00 -0800223
egdaniel8dc7c3a2015-04-16 11:22:42 -0700224 bool createStencilAttachmentForRenderTarget(GrRenderTarget*, int width, int height) override {
bsalomon33435572014-11-05 14:47:41 -0800225 return false;
226 }
227
egdaniel8dc7c3a2015-04-16 11:22:42 -0700228 bool attachStencilAttachmentToRenderTarget(GrStencilAttachment*, GrRenderTarget*) override {
bsalomon33435572014-11-05 14:47:41 -0800229 return false;
230 }
231
mtklein36352bf2015-03-25 18:17:31 -0700232 void clearStencil(GrRenderTarget* target) override {}
bsalomon33435572014-11-05 14:47:41 -0800233
mtklein36352bf2015-03-25 18:17:31 -0700234 void didAddGpuTraceMarker() override {}
bsalomon33435572014-11-05 14:47:41 -0800235
mtklein36352bf2015-03-25 18:17:31 -0700236 void didRemoveGpuTraceMarker() override {}
bsalomon33435572014-11-05 14:47:41 -0800237
jvanverth88957922015-07-14 11:02:52 -0700238 GrBackendObject createTestingOnlyBackendTexture(void* pixels, int w, int h,
239 GrPixelConfig config) const override {
240 return 0;
241 }
242 bool isTestingOnlyBackendTexture(GrBackendObject id) const override { return false; }
243 void deleteTestingOnlyBackendTexture(GrBackendObject id) const override {}
jvanverth672bb7f2015-07-13 07:19:57 -0700244
bsalomon33435572014-11-05 14:47:41 -0800245 typedef GrGpu INHERITED;
246};
247
248GrContext* GrContext::CreateMockContext() {
bsalomon682c2692015-05-22 14:01:46 -0700249 GrContext* context = SkNEW(GrContext);
bsalomon33435572014-11-05 14:47:41 -0800250
251 context->initMockContext();
252 return context;
253}
254
255void GrContext::initMockContext() {
bsalomon682c2692015-05-22 14:01:46 -0700256 GrContextOptions options;
joshualitt8b081592015-06-01 16:17:03 -0700257 options.fGeometryBufferMapThreshold = 0;
bsalomon33435572014-11-05 14:47:41 -0800258 SkASSERT(NULL == fGpu);
bsalomon682c2692015-05-22 14:01:46 -0700259 fGpu = SkNEW_ARGS(MockGpu, (this, options));
bsalomon33435572014-11-05 14:47:41 -0800260 SkASSERT(fGpu);
261 this->initCommon();
262
263 // We delete these because we want to test the cache starting with zero resources. Also, none of
264 // these objects are required for any of tests that use this context. TODO: make stop allocating
265 // resources in the buffer pools.
robertphillipsea461502015-05-26 11:38:03 -0700266 fDrawingMgr.abandon();
bsalomon33435572014-11-05 14:47:41 -0800267}