blob: b2fd99f6c39c42b6448f02441afb27df1add84fe [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"
bsalomon682c2692015-05-22 14:01:46 -070010#include "GrContextOptions.h"
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
jvanverth672bb7f2015-07-13 07:19:57 -070016void GrTestTarget::init(GrContext* ctx, GrDrawTarget* target) {
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000017 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().
jvanverth672bb7f2015-07-13 07:19:57 -070029 tar->init(this, fDrawingMgr.fDrawTarget);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000030}
31
32///////////////////////////////////////////////////////////////////////////////
33
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000034void GrContext::purgeAllUnlockedResources() {
bsalomon0ea80f42015-02-11 10:49:59 -080035 fResourceCache->purgeAllUnlocked();
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000036}
bsalomon33435572014-11-05 14:47:41 -080037
mtkleinb9eb4ac2015-02-02 18:26:03 -080038void GrContext::dumpCacheStats(SkString* out) const {
39#if GR_CACHE_STATS
bsalomon0ea80f42015-02-11 10:49:59 -080040 fResourceCache->dumpStats(out);
mtkleinb9eb4ac2015-02-02 18:26:03 -080041#endif
42}
43
44void GrContext::printCacheStats() const {
45 SkString out;
46 this->dumpCacheStats(&out);
kkinnunen297aaf92015-02-19 06:32:12 -080047 SkDebugf("%s", out.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -080048}
49
50void GrContext::dumpGpuStats(SkString* out) const {
51#if GR_GPU_STATS
52 return fGpu->stats()->dump(out);
53#endif
54}
55
56void GrContext::printGpuStats() const {
57 SkString out;
58 this->dumpGpuStats(&out);
kkinnunen297aaf92015-02-19 06:32:12 -080059 SkDebugf("%s", out.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -080060}
61
62#if GR_GPU_STATS
63void GrGpu::Stats::dump(SkString* out) {
64 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
65 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
bsalomonb12ea412015-02-02 21:19:50 -080066 out->appendf("Textures Created: %d\n", fTextureCreates);
67 out->appendf("Texture Uploads: %d\n", fTextureUploads);
egdaniel8dc7c3a2015-04-16 11:22:42 -070068 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
mtkleinb9eb4ac2015-02-02 18:26:03 -080069}
70#endif
71
72#if GR_CACHE_STATS
bsalomon0ea80f42015-02-11 10:49:59 -080073void GrResourceCache::dumpStats(SkString* out) const {
mtkleinb9eb4ac2015-02-02 18:26:03 -080074 this->validate();
75
bsalomonf320e042015-02-17 15:09:34 -080076 int locked = fNonpurgeableResources.count();
mtkleinb9eb4ac2015-02-02 18:26:03 -080077
bsalomonf320e042015-02-17 15:09:34 -080078 struct Stats {
79 int fScratch;
bsalomon6dc6f5f2015-06-18 09:12:16 -070080 int fExternal;
81 int fBorrowed;
82 int fAdopted;
bsalomonf320e042015-02-17 15:09:34 -080083 size_t fUnbudgetedSize;
mtkleinb9eb4ac2015-02-02 18:26:03 -080084
bsalomon6dc6f5f2015-06-18 09:12:16 -070085 Stats() : fScratch(0), fExternal(0), fBorrowed(0), fAdopted(0), fUnbudgetedSize(0) {}
bsalomonf320e042015-02-17 15:09:34 -080086
87 void update(GrGpuResource* resource) {
88 if (resource->cacheAccess().isScratch()) {
89 ++fScratch;
90 }
bsalomon6dc6f5f2015-06-18 09:12:16 -070091 if (resource->cacheAccess().isExternal()) {
92 ++fExternal;
93 }
94 if (resource->cacheAccess().isBorrowed()) {
95 ++fBorrowed;
96 }
97 if (resource->cacheAccess().isAdopted()) {
98 ++fAdopted;
bsalomonf320e042015-02-17 15:09:34 -080099 }
100 if (!resource->resourcePriv().isBudgeted()) {
101 fUnbudgetedSize += resource->gpuMemorySize();
102 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800103 }
bsalomonf320e042015-02-17 15:09:34 -0800104 };
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));
mtkleinb9eb4ac2015-02-02 18:26:03 -0800113 }
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"
bsalomon6dc6f5f2015-06-18 09:12:16 -0700120 " (%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);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800123 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
bsalomonf320e042015-02-17 15:09:34 -0800124 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
125 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
mtkleinb9eb4ac2015-02-02 18:26:03 -0800126}
127
128#endif
129
bsalomonddf30e62015-02-19 11:38:44 -0800130///////////////////////////////////////////////////////////////////////////////
131
132void GrResourceCache::changeTimestamp(uint32_t newTimestamp) { fTimestamp = newTimestamp; }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800133
bsalomon33435572014-11-05 14:47:41 -0800134///////////////////////////////////////////////////////////////////////////////
135// Code for the mock context. It's built on a mock GrGpu class that does nothing.
136////
137
bsalomon33435572014-11-05 14:47:41 -0800138#include "GrInOrderDrawBuffer.h"
139#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 {}
149 bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const override {
bsalomon33435572014-11-05 14:47:41 -0800150 return true;
151 }
152
bsalomon39826022015-07-23 08:07:21 -0700153 bool getReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight, size_t rowBytes,
154 GrPixelConfig readConfig, DrawPreference*,
155 ReadPixelTempDrawInfo*) override { return false; }
156
joshualitt873ad0e2015-01-20 09:08:51 -0800157 void buildProgramDesc(GrProgramDesc*,const GrPrimitiveProcessor&,
egdaniel8dd688b2015-01-22 10:16:09 -0800158 const GrPipeline&,
mtklein36352bf2015-03-25 18:17:31 -0700159 const GrBatchTracker&) const override {}
bsalomon33435572014-11-05 14:47:41 -0800160
mtklein36352bf2015-03-25 18:17:31 -0700161 void discard(GrRenderTarget*) override {}
bsalomon33435572014-11-05 14:47:41 -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
joshualitt1c735482015-07-13 08:08:25 -0700168 bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const 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
bsalomon6dc6f5f2015-06-18 09:12:16 -0700187 GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&,
188 GrWrapOwnership) override { return NULL; }
bsalomonf90a02b2014-11-26 12:28:00 -0800189
bsalomon6dc6f5f2015-06-18 09:12:16 -0700190 GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&,
191 GrWrapOwnership) override {
bsalomon33435572014-11-05 14:47:41 -0800192 return NULL;
193 }
194
mtklein36352bf2015-03-25 18:17:31 -0700195 GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) override { return NULL; }
bsalomonf90a02b2014-11-26 12:28:00 -0800196
mtklein36352bf2015-03-25 18:17:31 -0700197 GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) override { return NULL; }
bsalomonf90a02b2014-11-26 12:28:00 -0800198
199 void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color,
mtklein36352bf2015-03-25 18:17:31 -0700200 bool canIgnoreRect) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800201
mtklein36352bf2015-03-25 18:17:31 -0700202 void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800203
bsalomone64eb572015-05-07 11:35:55 -0700204 void onDraw(const DrawArgs&, const GrNonInstancedVertices&) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800205
206 bool onReadPixels(GrRenderTarget* target,
207 int left, int top, int width, int height,
208 GrPixelConfig,
209 void* buffer,
mtklein36352bf2015-03-25 18:17:31 -0700210 size_t rowBytes) override {
bsalomonf90a02b2014-11-26 12:28:00 -0800211 return false;
bsalomon33435572014-11-05 14:47:41 -0800212 }
213
bsalomonf90a02b2014-11-26 12:28:00 -0800214 bool onWriteTexturePixels(GrTexture* texture,
bsalomon33435572014-11-05 14:47:41 -0800215 int left, int top, int width, int height,
bsalomonf90a02b2014-11-26 12:28:00 -0800216 GrPixelConfig config, const void* buffer,
mtklein36352bf2015-03-25 18:17:31 -0700217 size_t rowBytes) override {
bsalomon33435572014-11-05 14:47:41 -0800218 return false;
219 }
220
mtklein36352bf2015-03-25 18:17:31 -0700221 void onResolveRenderTarget(GrRenderTarget* target) override { return; }
bsalomonf90a02b2014-11-26 12:28:00 -0800222
egdaniel8dc7c3a2015-04-16 11:22:42 -0700223 bool createStencilAttachmentForRenderTarget(GrRenderTarget*, int width, int height) override {
bsalomon33435572014-11-05 14:47:41 -0800224 return false;
225 }
226
egdaniel8dc7c3a2015-04-16 11:22:42 -0700227 bool attachStencilAttachmentToRenderTarget(GrStencilAttachment*, GrRenderTarget*) override {
bsalomon33435572014-11-05 14:47:41 -0800228 return false;
229 }
230
mtklein36352bf2015-03-25 18:17:31 -0700231 void clearStencil(GrRenderTarget* target) override {}
bsalomon33435572014-11-05 14:47:41 -0800232
mtklein36352bf2015-03-25 18:17:31 -0700233 void didAddGpuTraceMarker() override {}
bsalomon33435572014-11-05 14:47:41 -0800234
mtklein36352bf2015-03-25 18:17:31 -0700235 void didRemoveGpuTraceMarker() override {}
bsalomon33435572014-11-05 14:47:41 -0800236
jvanverth88957922015-07-14 11:02:52 -0700237 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 {}
jvanverth672bb7f2015-07-13 07:19:57 -0700243
bsalomon33435572014-11-05 14:47:41 -0800244 typedef GrGpu INHERITED;
245};
246
247GrContext* GrContext::CreateMockContext() {
bsalomon682c2692015-05-22 14:01:46 -0700248 GrContext* context = SkNEW(GrContext);
bsalomon33435572014-11-05 14:47:41 -0800249
250 context->initMockContext();
251 return context;
252}
253
254void GrContext::initMockContext() {
bsalomon682c2692015-05-22 14:01:46 -0700255 GrContextOptions options;
joshualitt8b081592015-06-01 16:17:03 -0700256 options.fGeometryBufferMapThreshold = 0;
bsalomon33435572014-11-05 14:47:41 -0800257 SkASSERT(NULL == fGpu);
bsalomon682c2692015-05-22 14:01:46 -0700258 fGpu = SkNEW_ARGS(MockGpu, (this, options));
bsalomon33435572014-11-05 14:47:41 -0800259 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.
robertphillipsea461502015-05-26 11:38:03 -0700265 fDrawingMgr.abandon();
bsalomon33435572014-11-05 14:47:41 -0800266}