blob: 7b6cb5ed37071fc14b7f41e691f8d35fe541d04c [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"
joshualitt17d833b2015-08-03 10:17:44 -070015#include "GrTextBlobCache.h"
mtkleinb9eb4ac2015-02-02 18:26:03 -080016#include "SkString.h"
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000017
jvanverth672bb7f2015-07-13 07:19:57 -070018void GrTestTarget::init(GrContext* ctx, GrDrawTarget* target) {
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000019 SkASSERT(!fContext);
20
21 fContext.reset(SkRef(ctx));
22 fDrawTarget.reset(SkRef(target));
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000023}
24
25void GrContext::getTestTarget(GrTestTarget* tar) {
26 this->flush();
27 // We could create a proxy GrDrawTarget that passes through to fGpu until ~GrTextTarget() and
28 // then disconnects. This would help prevent test writers from mixing using the returned
29 // GrDrawTarget and regular drawing. We could also assert or fail in GrContext drawing methods
30 // until ~GrTestTarget().
jvanverth672bb7f2015-07-13 07:19:57 -070031 tar->init(this, fDrawingMgr.fDrawTarget);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000032}
33
joshualitt17d833b2015-08-03 10:17:44 -070034void GrContext::setTextBlobCacheLimit_ForTesting(size_t bytes) {
35 fTextBlobCache->setBudget(bytes);
36}
37
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000038///////////////////////////////////////////////////////////////////////////////
39
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000040void GrContext::purgeAllUnlockedResources() {
bsalomon0ea80f42015-02-11 10:49:59 -080041 fResourceCache->purgeAllUnlocked();
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000042}
bsalomon33435572014-11-05 14:47:41 -080043
mtkleinb9eb4ac2015-02-02 18:26:03 -080044void GrContext::dumpCacheStats(SkString* out) const {
45#if GR_CACHE_STATS
bsalomon0ea80f42015-02-11 10:49:59 -080046 fResourceCache->dumpStats(out);
mtkleinb9eb4ac2015-02-02 18:26:03 -080047#endif
48}
49
50void GrContext::printCacheStats() const {
51 SkString out;
52 this->dumpCacheStats(&out);
kkinnunen297aaf92015-02-19 06:32:12 -080053 SkDebugf("%s", out.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -080054}
55
56void GrContext::dumpGpuStats(SkString* out) const {
57#if GR_GPU_STATS
58 return fGpu->stats()->dump(out);
59#endif
60}
61
62void GrContext::printGpuStats() const {
63 SkString out;
64 this->dumpGpuStats(&out);
kkinnunen297aaf92015-02-19 06:32:12 -080065 SkDebugf("%s", out.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -080066}
67
68#if GR_GPU_STATS
69void GrGpu::Stats::dump(SkString* out) {
70 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
71 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
bsalomonb12ea412015-02-02 21:19:50 -080072 out->appendf("Textures Created: %d\n", fTextureCreates);
73 out->appendf("Texture Uploads: %d\n", fTextureUploads);
egdaniel8dc7c3a2015-04-16 11:22:42 -070074 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
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;
bsalomon6dc6f5f2015-06-18 09:12:16 -070086 int fExternal;
87 int fBorrowed;
88 int fAdopted;
bsalomonf320e042015-02-17 15:09:34 -080089 size_t fUnbudgetedSize;
mtkleinb9eb4ac2015-02-02 18:26:03 -080090
bsalomon6dc6f5f2015-06-18 09:12:16 -070091 Stats() : fScratch(0), fExternal(0), fBorrowed(0), fAdopted(0), fUnbudgetedSize(0) {}
bsalomonf320e042015-02-17 15:09:34 -080092
93 void update(GrGpuResource* resource) {
94 if (resource->cacheAccess().isScratch()) {
95 ++fScratch;
96 }
bsalomon6dc6f5f2015-06-18 09:12:16 -070097 if (resource->cacheAccess().isExternal()) {
98 ++fExternal;
99 }
100 if (resource->cacheAccess().isBorrowed()) {
101 ++fBorrowed;
102 }
103 if (resource->cacheAccess().isAdopted()) {
104 ++fAdopted;
bsalomonf320e042015-02-17 15:09:34 -0800105 }
106 if (!resource->resourcePriv().isBudgeted()) {
107 fUnbudgetedSize += resource->gpuMemorySize();
108 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800109 }
bsalomonf320e042015-02-17 15:09:34 -0800110 };
111
112 Stats stats;
113
114 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
115 stats.update(fNonpurgeableResources[i]);
116 }
117 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
118 stats.update(fPurgeableQueue.at(i));
mtkleinb9eb4ac2015-02-02 18:26:03 -0800119 }
120
121 float countUtilization = (100.f * fBudgetedCount) / fMaxCount;
122 float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
123
124 out->appendf("Budget: %d items %d bytes\n", fMaxCount, (int)fMaxBytes);
125 out->appendf("\t\tEntry Count: current %d"
bsalomon6dc6f5f2015-06-18 09:12:16 -0700126 " (%d budgeted, %d external(%d borrowed, %d adopted), %d locked, %d scratch %.2g%% full), high %d\n",
127 this->getResourceCount(), fBudgetedCount, stats.fExternal, stats.fBorrowed,
128 stats.fAdopted, locked, stats.fScratch, countUtilization, fHighWaterCount);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800129 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
bsalomonf320e042015-02-17 15:09:34 -0800130 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
131 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
mtkleinb9eb4ac2015-02-02 18:26:03 -0800132}
133
134#endif
135
bsalomonddf30e62015-02-19 11:38:44 -0800136///////////////////////////////////////////////////////////////////////////////
137
138void GrResourceCache::changeTimestamp(uint32_t newTimestamp) { fTimestamp = newTimestamp; }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800139
bsalomon33435572014-11-05 14:47:41 -0800140///////////////////////////////////////////////////////////////////////////////
141// Code for the mock context. It's built on a mock GrGpu class that does nothing.
142////
143
bsalomon33435572014-11-05 14:47:41 -0800144#include "GrGpu.h"
145
egdaniel8dd688b2015-01-22 10:16:09 -0800146class GrPipeline;
joshualittd53a8272014-11-10 16:03:14 -0800147
bsalomon33435572014-11-05 14:47:41 -0800148class MockGpu : public GrGpu {
149public:
bsalomon682c2692015-05-22 14:01:46 -0700150 MockGpu(GrContext* context, const GrContextOptions& options) : INHERITED(context) {
151 fCaps.reset(SkNEW_ARGS(GrCaps, (options)));
152 }
mtklein36352bf2015-03-25 18:17:31 -0700153 ~MockGpu() override {}
bsalomon33435572014-11-05 14:47:41 -0800154
bsalomonf0674512015-07-28 13:26:15 -0700155 bool onGetReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight, size_t rowBytes,
156 GrPixelConfig readConfig, DrawPreference*,
157 ReadPixelTempDrawInfo*) override { return false; }
158
159 bool onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height, size_t rowBytes,
160 GrPixelConfig srcConfig, DrawPreference*,
161 WritePixelTempDrawInfo*) override { return false; }
bsalomon39826022015-07-23 08:07:21 -0700162
joshualitt873ad0e2015-01-20 09:08:51 -0800163 void buildProgramDesc(GrProgramDesc*,const GrPrimitiveProcessor&,
egdaniel8dd688b2015-01-22 10:16:09 -0800164 const GrPipeline&,
mtklein36352bf2015-03-25 18:17:31 -0700165 const GrBatchTracker&) const override {}
bsalomon33435572014-11-05 14:47:41 -0800166
mtklein36352bf2015-03-25 18:17:31 -0700167 void discard(GrRenderTarget*) override {}
bsalomon33435572014-11-05 14:47:41 -0800168
bsalomonf90a02b2014-11-26 12:28:00 -0800169 bool copySurface(GrSurface* dst,
170 GrSurface* src,
171 const SkIRect& srcRect,
mtklein36352bf2015-03-25 18:17:31 -0700172 const SkIPoint& dstPoint) override { return false; };
bsalomonf90a02b2014-11-26 12:28:00 -0800173
joshualitt1c735482015-07-13 08:08:25 -0700174 bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const override {
bsalomonf90a02b2014-11-26 12:28:00 -0800175 return false;
176 }
joshualitt3322fa42014-11-07 08:48:51 -0800177
bsalomon33435572014-11-05 14:47:41 -0800178private:
mtklein36352bf2015-03-25 18:17:31 -0700179 void onResetContext(uint32_t resetBits) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800180
bsalomoncb02b382015-08-12 11:14:50 -0700181 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {}
182
egdanielb0e1be22015-04-22 13:27:39 -0700183 GrTexture* onCreateTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle lifeCycle,
184 const void* srcData, size_t rowBytes) override {
bsalomon33435572014-11-05 14:47:41 -0800185 return NULL;
186 }
187
egdanielb0e1be22015-04-22 13:27:39 -0700188 GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle,
mtklein36352bf2015-03-25 18:17:31 -0700189 const void* srcData) override {
bsalomon33435572014-11-05 14:47:41 -0800190 return NULL;
191 }
192
bsalomon6dc6f5f2015-06-18 09:12:16 -0700193 GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&,
194 GrWrapOwnership) override { return NULL; }
bsalomonf90a02b2014-11-26 12:28:00 -0800195
bsalomon6dc6f5f2015-06-18 09:12:16 -0700196 GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&,
197 GrWrapOwnership) override {
bsalomon33435572014-11-05 14:47:41 -0800198 return NULL;
199 }
200
mtklein36352bf2015-03-25 18:17:31 -0700201 GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) override { return NULL; }
bsalomonf90a02b2014-11-26 12:28:00 -0800202
mtklein36352bf2015-03-25 18:17:31 -0700203 GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) override { return NULL; }
bsalomonf90a02b2014-11-26 12:28:00 -0800204
egdaniel51c8d402015-08-06 10:54:13 -0700205 void onClear(GrRenderTarget*, const SkIRect& rect, GrColor color) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800206
mtklein36352bf2015-03-25 18:17:31 -0700207 void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800208
bsalomone64eb572015-05-07 11:35:55 -0700209 void onDraw(const DrawArgs&, const GrNonInstancedVertices&) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800210
bsalomon6cb3cbe2015-07-30 07:34:27 -0700211 bool onReadPixels(GrSurface* surface,
bsalomonf90a02b2014-11-26 12:28:00 -0800212 int left, int top, int width, int height,
213 GrPixelConfig,
214 void* buffer,
mtklein36352bf2015-03-25 18:17:31 -0700215 size_t rowBytes) override {
bsalomonf90a02b2014-11-26 12:28:00 -0800216 return false;
bsalomon33435572014-11-05 14:47:41 -0800217 }
218
bsalomon6cb3cbe2015-07-30 07:34:27 -0700219 bool onWritePixels(GrSurface* surface,
220 int left, int top, int width, int height,
221 GrPixelConfig config, const void* buffer,
222 size_t rowBytes) override {
bsalomon33435572014-11-05 14:47:41 -0800223 return false;
224 }
225
mtklein36352bf2015-03-25 18:17:31 -0700226 void onResolveRenderTarget(GrRenderTarget* target) override { return; }
bsalomonf90a02b2014-11-26 12:28:00 -0800227
egdaniel8dc7c3a2015-04-16 11:22:42 -0700228 bool createStencilAttachmentForRenderTarget(GrRenderTarget*, int width, int height) override {
bsalomon33435572014-11-05 14:47:41 -0800229 return false;
230 }
231
egdaniel8dc7c3a2015-04-16 11:22:42 -0700232 bool attachStencilAttachmentToRenderTarget(GrStencilAttachment*, GrRenderTarget*) override {
bsalomon33435572014-11-05 14:47:41 -0800233 return false;
234 }
235
mtklein36352bf2015-03-25 18:17:31 -0700236 void clearStencil(GrRenderTarget* target) override {}
bsalomon33435572014-11-05 14:47:41 -0800237
mtklein36352bf2015-03-25 18:17:31 -0700238 void didAddGpuTraceMarker() override {}
bsalomon33435572014-11-05 14:47:41 -0800239
mtklein36352bf2015-03-25 18:17:31 -0700240 void didRemoveGpuTraceMarker() override {}
bsalomon33435572014-11-05 14:47:41 -0800241
jvanverth88957922015-07-14 11:02:52 -0700242 GrBackendObject createTestingOnlyBackendTexture(void* pixels, int w, int h,
243 GrPixelConfig config) const override {
244 return 0;
245 }
246 bool isTestingOnlyBackendTexture(GrBackendObject id) const override { return false; }
247 void deleteTestingOnlyBackendTexture(GrBackendObject id) const override {}
jvanverth672bb7f2015-07-13 07:19:57 -0700248
bsalomon33435572014-11-05 14:47:41 -0800249 typedef GrGpu INHERITED;
250};
251
252GrContext* GrContext::CreateMockContext() {
bsalomon682c2692015-05-22 14:01:46 -0700253 GrContext* context = SkNEW(GrContext);
bsalomon33435572014-11-05 14:47:41 -0800254
255 context->initMockContext();
256 return context;
257}
258
259void GrContext::initMockContext() {
bsalomon682c2692015-05-22 14:01:46 -0700260 GrContextOptions options;
joshualitt8b081592015-06-01 16:17:03 -0700261 options.fGeometryBufferMapThreshold = 0;
bsalomon33435572014-11-05 14:47:41 -0800262 SkASSERT(NULL == fGpu);
bsalomon682c2692015-05-22 14:01:46 -0700263 fGpu = SkNEW_ARGS(MockGpu, (this, options));
bsalomon33435572014-11-05 14:47:41 -0800264 SkASSERT(fGpu);
265 this->initCommon();
266
267 // We delete these because we want to test the cache starting with zero resources. Also, none of
268 // these objects are required for any of tests that use this context. TODO: make stop allocating
269 // resources in the buffer pools.
robertphillipsea461502015-05-26 11:38:03 -0700270 fDrawingMgr.abandon();
bsalomon33435572014-11-05 14:47:41 -0800271}