blob: 8546cf1ac4dd6bd61d7f3770c90b58d67c949744 [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"
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000011
bsalomon3582d3e2015-02-13 14:20:05 -080012#include "GrGpuResourceCacheAccess.h"
joshualitt50408ad2014-11-03 12:31:14 -080013#include "GrInOrderDrawBuffer.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
17void GrTestTarget::init(GrContext* ctx, GrDrawTarget* target) {
18 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().
robertphillipsea461502015-05-26 11:38:03 -070030 tar->init(this, fDrawingMgr.fDrawTarget);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000031}
32
33///////////////////////////////////////////////////////////////////////////////
34
35void GrContext::setMaxTextureSizeOverride(int maxTextureSizeOverride) {
36 fMaxTextureSizeOverride = maxTextureSizeOverride;
37}
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000038
39void GrContext::purgeAllUnlockedResources() {
bsalomon0ea80f42015-02-11 10:49:59 -080040 fResourceCache->purgeAllUnlocked();
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000041}
bsalomon33435572014-11-05 14:47:41 -080042
mtkleinb9eb4ac2015-02-02 18:26:03 -080043void GrContext::dumpCacheStats(SkString* out) const {
44#if GR_CACHE_STATS
bsalomon0ea80f42015-02-11 10:49:59 -080045 fResourceCache->dumpStats(out);
mtkleinb9eb4ac2015-02-02 18:26:03 -080046#endif
47}
48
49void GrContext::printCacheStats() const {
50 SkString out;
51 this->dumpCacheStats(&out);
kkinnunen297aaf92015-02-19 06:32:12 -080052 SkDebugf("%s", out.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -080053}
54
55void GrContext::dumpGpuStats(SkString* out) const {
56#if GR_GPU_STATS
57 return fGpu->stats()->dump(out);
58#endif
59}
60
61void GrContext::printGpuStats() const {
62 SkString out;
63 this->dumpGpuStats(&out);
kkinnunen297aaf92015-02-19 06:32:12 -080064 SkDebugf("%s", out.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -080065}
66
67#if GR_GPU_STATS
68void GrGpu::Stats::dump(SkString* out) {
69 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
70 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
bsalomonb12ea412015-02-02 21:19:50 -080071 out->appendf("Textures Created: %d\n", fTextureCreates);
72 out->appendf("Texture Uploads: %d\n", fTextureUploads);
egdaniel8dc7c3a2015-04-16 11:22:42 -070073 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
mtkleinb9eb4ac2015-02-02 18:26:03 -080074}
75#endif
76
77#if GR_CACHE_STATS
bsalomon0ea80f42015-02-11 10:49:59 -080078void GrResourceCache::dumpStats(SkString* out) const {
mtkleinb9eb4ac2015-02-02 18:26:03 -080079 this->validate();
80
bsalomonf320e042015-02-17 15:09:34 -080081 int locked = fNonpurgeableResources.count();
mtkleinb9eb4ac2015-02-02 18:26:03 -080082
bsalomonf320e042015-02-17 15:09:34 -080083 struct Stats {
84 int fScratch;
85 int fWrapped;
86 size_t fUnbudgetedSize;
mtkleinb9eb4ac2015-02-02 18:26:03 -080087
bsalomonf320e042015-02-17 15:09:34 -080088 Stats() : fScratch(0), fWrapped(0), fUnbudgetedSize(0) {}
89
90 void update(GrGpuResource* resource) {
91 if (resource->cacheAccess().isScratch()) {
92 ++fScratch;
93 }
94 if (resource->cacheAccess().isWrapped()) {
95 ++fWrapped;
96 }
97 if (!resource->resourcePriv().isBudgeted()) {
98 fUnbudgetedSize += resource->gpuMemorySize();
99 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800100 }
bsalomonf320e042015-02-17 15:09:34 -0800101 };
102
103 Stats stats;
104
105 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
106 stats.update(fNonpurgeableResources[i]);
107 }
108 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
109 stats.update(fPurgeableQueue.at(i));
mtkleinb9eb4ac2015-02-02 18:26:03 -0800110 }
111
112 float countUtilization = (100.f * fBudgetedCount) / fMaxCount;
113 float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
114
115 out->appendf("Budget: %d items %d bytes\n", fMaxCount, (int)fMaxBytes);
116 out->appendf("\t\tEntry Count: current %d"
117 " (%d budgeted, %d wrapped, %d locked, %d scratch %.2g%% full), high %d\n",
bsalomon52057c82015-02-23 12:12:59 -0800118 this->getResourceCount(), fBudgetedCount, stats.fWrapped, locked, stats.fScratch,
119 countUtilization, fHighWaterCount);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800120 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
bsalomonf320e042015-02-17 15:09:34 -0800121 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
122 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
mtkleinb9eb4ac2015-02-02 18:26:03 -0800123}
124
125#endif
126
bsalomonddf30e62015-02-19 11:38:44 -0800127///////////////////////////////////////////////////////////////////////////////
128
129void GrResourceCache::changeTimestamp(uint32_t newTimestamp) { fTimestamp = newTimestamp; }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800130
bsalomon33435572014-11-05 14:47:41 -0800131///////////////////////////////////////////////////////////////////////////////
132// Code for the mock context. It's built on a mock GrGpu class that does nothing.
133////
134
bsalomon33435572014-11-05 14:47:41 -0800135#include "GrInOrderDrawBuffer.h"
136#include "GrGpu.h"
137
egdaniel8dd688b2015-01-22 10:16:09 -0800138class GrPipeline;
joshualittd53a8272014-11-10 16:03:14 -0800139
bsalomon33435572014-11-05 14:47:41 -0800140class MockGpu : public GrGpu {
141public:
bsalomon682c2692015-05-22 14:01:46 -0700142 MockGpu(GrContext* context, const GrContextOptions& options) : INHERITED(context) {
143 fCaps.reset(SkNEW_ARGS(GrCaps, (options)));
144 }
mtklein36352bf2015-03-25 18:17:31 -0700145 ~MockGpu() override {}
146 bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const 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,
mtklein36352bf2015-03-25 18:17:31 -0700154 size_t rowBytes) const override { return false; }
joshualitt873ad0e2015-01-20 09:08:51 -0800155 void buildProgramDesc(GrProgramDesc*,const GrPrimitiveProcessor&,
egdaniel8dd688b2015-01-22 10:16:09 -0800156 const GrPipeline&,
mtklein36352bf2015-03-25 18:17:31 -0700157 const GrBatchTracker&) const override {}
bsalomon33435572014-11-05 14:47:41 -0800158
mtklein36352bf2015-03-25 18:17:31 -0700159 void discard(GrRenderTarget*) override {}
bsalomon33435572014-11-05 14:47:41 -0800160
mtklein404b3b22015-05-18 09:29:10 -0700161 bool canCopySurface(const GrSurface* dst,
162 const GrSurface* src,
163 const SkIRect& srcRect,
164 const SkIPoint& dstPoint) override { return false; };
165
bsalomonf90a02b2014-11-26 12:28:00 -0800166 bool copySurface(GrSurface* dst,
167 GrSurface* src,
168 const SkIRect& srcRect,
mtklein36352bf2015-03-25 18:17:31 -0700169 const SkIPoint& dstPoint) override { return false; };
bsalomonf90a02b2014-11-26 12:28:00 -0800170
mtklein36352bf2015-03-25 18:17:31 -0700171 bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) override {
bsalomonf90a02b2014-11-26 12:28:00 -0800172 return false;
173 }
joshualitt3322fa42014-11-07 08:48:51 -0800174
cdalton231c5fd2015-05-13 12:35:36 -0700175 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {}
cdalton9954bc32015-04-29 14:17:00 -0700176
bsalomon33435572014-11-05 14:47:41 -0800177private:
mtklein36352bf2015-03-25 18:17:31 -0700178 void onResetContext(uint32_t resetBits) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800179
egdanielb0e1be22015-04-22 13:27:39 -0700180 GrTexture* onCreateTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle lifeCycle,
181 const void* srcData, size_t rowBytes) override {
bsalomon33435572014-11-05 14:47:41 -0800182 return NULL;
183 }
184
egdanielb0e1be22015-04-22 13:27:39 -0700185 GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle,
mtklein36352bf2015-03-25 18:17:31 -0700186 const void* srcData) override {
bsalomon33435572014-11-05 14:47:41 -0800187 return NULL;
188 }
189
mtklein36352bf2015-03-25 18:17:31 -0700190 GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) override { return NULL; }
bsalomonf90a02b2014-11-26 12:28:00 -0800191
mtklein36352bf2015-03-25 18:17:31 -0700192 GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) 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
mtklein36352bf2015-03-25 18:17:31 -0700207 void onStencilPath(const GrPath* path, const StencilPathState& state) override {}
bsalomond95263c2014-12-16 13:05:12 -0800208
mtklein36352bf2015-03-25 18:17:31 -0700209 void onDrawPath(const DrawArgs&, const GrPath*, const GrStencilSettings&) override {}
bsalomond95263c2014-12-16 13:05:12 -0800210
joshualitt873ad0e2015-01-20 09:08:51 -0800211 void onDrawPaths(const DrawArgs&,
bsalomond95263c2014-12-16 13:05:12 -0800212 const GrPathRange*,
213 const void* indices,
214 GrDrawTarget::PathIndexType,
215 const float transformValues[],
216 GrDrawTarget::PathTransformType,
217 int count,
mtklein36352bf2015-03-25 18:17:31 -0700218 const GrStencilSettings&) override {}
bsalomond95263c2014-12-16 13:05:12 -0800219
bsalomonf90a02b2014-11-26 12:28:00 -0800220 bool onReadPixels(GrRenderTarget* target,
221 int left, int top, int width, int height,
222 GrPixelConfig,
223 void* buffer,
mtklein36352bf2015-03-25 18:17:31 -0700224 size_t rowBytes) override {
bsalomonf90a02b2014-11-26 12:28:00 -0800225 return false;
bsalomon33435572014-11-05 14:47:41 -0800226 }
227
bsalomonf90a02b2014-11-26 12:28:00 -0800228 bool onWriteTexturePixels(GrTexture* texture,
bsalomon33435572014-11-05 14:47:41 -0800229 int left, int top, int width, int height,
bsalomonf90a02b2014-11-26 12:28:00 -0800230 GrPixelConfig config, const void* buffer,
mtklein36352bf2015-03-25 18:17:31 -0700231 size_t rowBytes) override {
bsalomon33435572014-11-05 14:47:41 -0800232 return false;
233 }
234
mtklein36352bf2015-03-25 18:17:31 -0700235 void onResolveRenderTarget(GrRenderTarget* target) override { return; }
bsalomonf90a02b2014-11-26 12:28:00 -0800236
egdaniel8dc7c3a2015-04-16 11:22:42 -0700237 bool createStencilAttachmentForRenderTarget(GrRenderTarget*, int width, int height) override {
bsalomon33435572014-11-05 14:47:41 -0800238 return false;
239 }
240
egdaniel8dc7c3a2015-04-16 11:22:42 -0700241 bool attachStencilAttachmentToRenderTarget(GrStencilAttachment*, GrRenderTarget*) override {
bsalomon33435572014-11-05 14:47:41 -0800242 return false;
243 }
244
mtklein36352bf2015-03-25 18:17:31 -0700245 void clearStencil(GrRenderTarget* target) override {}
bsalomon33435572014-11-05 14:47:41 -0800246
mtklein36352bf2015-03-25 18:17:31 -0700247 void didAddGpuTraceMarker() override {}
bsalomon33435572014-11-05 14:47:41 -0800248
mtklein36352bf2015-03-25 18:17:31 -0700249 void didRemoveGpuTraceMarker() override {}
bsalomon33435572014-11-05 14:47:41 -0800250
251 typedef GrGpu INHERITED;
252};
253
254GrContext* GrContext::CreateMockContext() {
bsalomon682c2692015-05-22 14:01:46 -0700255 GrContext* context = SkNEW(GrContext);
bsalomon33435572014-11-05 14:47:41 -0800256
257 context->initMockContext();
258 return context;
259}
260
261void GrContext::initMockContext() {
bsalomon682c2692015-05-22 14:01:46 -0700262 GrContextOptions options;
bsalomon33435572014-11-05 14:47:41 -0800263 SkASSERT(NULL == fGpu);
bsalomon682c2692015-05-22 14:01:46 -0700264 fGpu = SkNEW_ARGS(MockGpu, (this, options));
bsalomon33435572014-11-05 14:47:41 -0800265 SkASSERT(fGpu);
266 this->initCommon();
267
268 // We delete these because we want to test the cache starting with zero resources. Also, none of
269 // these objects are required for any of tests that use this context. TODO: make stop allocating
270 // resources in the buffer pools.
robertphillipsea461502015-05-26 11:38:03 -0700271 fDrawingMgr.abandon();
bsalomon33435572014-11-05 14:47:41 -0800272}