blob: 4adc3f7b7b770585a34e92729b3efe509c4c60e9 [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
joshualitt7f9c9eb2015-08-21 11:08:00 -070011#include "GrBatchAtlas.h"
joshualittda04e0e2015-08-19 08:16:43 -070012#include "GrBatchFontCache.h"
bsalomon682c2692015-05-22 14:01:46 -070013#include "GrContextOptions.h"
robertphillips77a2e522015-10-17 07:43:27 -070014#include "GrDrawingManager.h"
bsalomon3582d3e2015-02-13 14:20:05 -080015#include "GrGpuResourceCacheAccess.h"
bsalomon0ea80f42015-02-11 10:49:59 -080016#include "GrResourceCache.h"
joshualitt17d833b2015-08-03 10:17:44 -070017#include "GrTextBlobCache.h"
mtkleinb9eb4ac2015-02-02 18:26:03 -080018#include "SkString.h"
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000019
joshualitt7f9c9eb2015-08-21 11:08:00 -070020namespace GrTest {
21void SetupAlwaysEvictAtlas(GrContext* context) {
22 // These sizes were selected because they allow each atlas to hold a single plot and will thus
23 // stress the atlas
24 int dim = GrBatchAtlas::kGlyphMaxDim;
25 GrBatchAtlasConfig configs[3];
26 configs[kA8_GrMaskFormat].fWidth = dim;
27 configs[kA8_GrMaskFormat].fHeight = dim;
28 configs[kA8_GrMaskFormat].fPlotWidth = dim;
29 configs[kA8_GrMaskFormat].fPlotHeight = dim;
30
31 configs[kA565_GrMaskFormat].fWidth = dim;
32 configs[kA565_GrMaskFormat].fHeight = dim;
33 configs[kA565_GrMaskFormat].fPlotWidth = dim;
34 configs[kA565_GrMaskFormat].fPlotHeight = dim;
35
36 configs[kARGB_GrMaskFormat].fWidth = dim;
37 configs[kARGB_GrMaskFormat].fHeight = dim;
38 configs[kARGB_GrMaskFormat].fPlotWidth = dim;
39 configs[kARGB_GrMaskFormat].fPlotHeight = dim;
40
41 context->setTextContextAtlasSizes_ForTesting(configs);
42}
43};
44
jvanverth672bb7f2015-07-13 07:19:57 -070045void GrTestTarget::init(GrContext* ctx, GrDrawTarget* target) {
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000046 SkASSERT(!fContext);
47
48 fContext.reset(SkRef(ctx));
49 fDrawTarget.reset(SkRef(target));
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000050}
51
52void GrContext::getTestTarget(GrTestTarget* tar) {
53 this->flush();
54 // We could create a proxy GrDrawTarget that passes through to fGpu until ~GrTextTarget() and
55 // then disconnects. This would help prevent test writers from mixing using the returned
56 // GrDrawTarget and regular drawing. We could also assert or fail in GrContext drawing methods
57 // until ~GrTestTarget().
robertphillips77a2e522015-10-17 07:43:27 -070058 SkAutoTUnref<GrDrawTarget> dt(fDrawingManager->newDrawTarget(nullptr));
robertphillipsa106c622015-10-16 09:07:06 -070059 tar->init(this, dt);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000060}
61
joshualitt17d833b2015-08-03 10:17:44 -070062void GrContext::setTextBlobCacheLimit_ForTesting(size_t bytes) {
63 fTextBlobCache->setBudget(bytes);
64}
65
joshualittda04e0e2015-08-19 08:16:43 -070066void GrContext::setTextContextAtlasSizes_ForTesting(const GrBatchAtlasConfig* configs) {
67 fBatchFontCache->setAtlasSizes_ForTesting(configs);
68}
69
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000070///////////////////////////////////////////////////////////////////////////////
71
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000072void GrContext::purgeAllUnlockedResources() {
bsalomon0ea80f42015-02-11 10:49:59 -080073 fResourceCache->purgeAllUnlocked();
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000074}
bsalomon33435572014-11-05 14:47:41 -080075
mtkleinb9eb4ac2015-02-02 18:26:03 -080076void GrContext::dumpCacheStats(SkString* out) const {
77#if GR_CACHE_STATS
bsalomon0ea80f42015-02-11 10:49:59 -080078 fResourceCache->dumpStats(out);
mtkleinb9eb4ac2015-02-02 18:26:03 -080079#endif
80}
81
82void GrContext::printCacheStats() const {
83 SkString out;
84 this->dumpCacheStats(&out);
kkinnunen297aaf92015-02-19 06:32:12 -080085 SkDebugf("%s", out.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -080086}
87
88void GrContext::dumpGpuStats(SkString* out) const {
89#if GR_GPU_STATS
90 return fGpu->stats()->dump(out);
91#endif
92}
93
94void GrContext::printGpuStats() const {
95 SkString out;
96 this->dumpGpuStats(&out);
kkinnunen297aaf92015-02-19 06:32:12 -080097 SkDebugf("%s", out.c_str());
mtkleinb9eb4ac2015-02-02 18:26:03 -080098}
99
100#if GR_GPU_STATS
101void GrGpu::Stats::dump(SkString* out) {
102 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
103 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
bsalomonb12ea412015-02-02 21:19:50 -0800104 out->appendf("Textures Created: %d\n", fTextureCreates);
105 out->appendf("Texture Uploads: %d\n", fTextureUploads);
egdaniel8dc7c3a2015-04-16 11:22:42 -0700106 out->appendf("Stencil Buffer Creates: %d\n", fStencilAttachmentCreates);
joshualitt87a5c9f2015-09-08 13:42:05 -0700107 out->appendf("Number of draws: %d\n", fNumDraws);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800108}
109#endif
110
111#if GR_CACHE_STATS
bsalomon0ea80f42015-02-11 10:49:59 -0800112void GrResourceCache::dumpStats(SkString* out) const {
mtkleinb9eb4ac2015-02-02 18:26:03 -0800113 this->validate();
114
bsalomonf320e042015-02-17 15:09:34 -0800115 int locked = fNonpurgeableResources.count();
mtkleinb9eb4ac2015-02-02 18:26:03 -0800116
bsalomonf320e042015-02-17 15:09:34 -0800117 struct Stats {
118 int fScratch;
bsalomon6dc6f5f2015-06-18 09:12:16 -0700119 int fExternal;
120 int fBorrowed;
121 int fAdopted;
bsalomonf320e042015-02-17 15:09:34 -0800122 size_t fUnbudgetedSize;
mtkleinb9eb4ac2015-02-02 18:26:03 -0800123
bsalomon6dc6f5f2015-06-18 09:12:16 -0700124 Stats() : fScratch(0), fExternal(0), fBorrowed(0), fAdopted(0), fUnbudgetedSize(0) {}
bsalomonf320e042015-02-17 15:09:34 -0800125
126 void update(GrGpuResource* resource) {
127 if (resource->cacheAccess().isScratch()) {
128 ++fScratch;
129 }
bsalomon6dc6f5f2015-06-18 09:12:16 -0700130 if (resource->cacheAccess().isExternal()) {
131 ++fExternal;
132 }
133 if (resource->cacheAccess().isBorrowed()) {
134 ++fBorrowed;
135 }
136 if (resource->cacheAccess().isAdopted()) {
137 ++fAdopted;
bsalomonf320e042015-02-17 15:09:34 -0800138 }
139 if (!resource->resourcePriv().isBudgeted()) {
140 fUnbudgetedSize += resource->gpuMemorySize();
141 }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800142 }
bsalomonf320e042015-02-17 15:09:34 -0800143 };
144
145 Stats stats;
146
147 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
148 stats.update(fNonpurgeableResources[i]);
149 }
150 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
151 stats.update(fPurgeableQueue.at(i));
mtkleinb9eb4ac2015-02-02 18:26:03 -0800152 }
153
154 float countUtilization = (100.f * fBudgetedCount) / fMaxCount;
155 float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
156
157 out->appendf("Budget: %d items %d bytes\n", fMaxCount, (int)fMaxBytes);
158 out->appendf("\t\tEntry Count: current %d"
bsalomon6dc6f5f2015-06-18 09:12:16 -0700159 " (%d budgeted, %d external(%d borrowed, %d adopted), %d locked, %d scratch %.2g%% full), high %d\n",
160 this->getResourceCount(), fBudgetedCount, stats.fExternal, stats.fBorrowed,
161 stats.fAdopted, locked, stats.fScratch, countUtilization, fHighWaterCount);
mtkleinb9eb4ac2015-02-02 18:26:03 -0800162 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
bsalomonf320e042015-02-17 15:09:34 -0800163 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
164 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
mtkleinb9eb4ac2015-02-02 18:26:03 -0800165}
166
167#endif
168
bsalomonddf30e62015-02-19 11:38:44 -0800169///////////////////////////////////////////////////////////////////////////////
170
171void GrResourceCache::changeTimestamp(uint32_t newTimestamp) { fTimestamp = newTimestamp; }
mtkleinb9eb4ac2015-02-02 18:26:03 -0800172
bsalomon33435572014-11-05 14:47:41 -0800173///////////////////////////////////////////////////////////////////////////////
174// Code for the mock context. It's built on a mock GrGpu class that does nothing.
175////
176
bsalomon33435572014-11-05 14:47:41 -0800177#include "GrGpu.h"
178
egdaniel8dd688b2015-01-22 10:16:09 -0800179class GrPipeline;
joshualittd53a8272014-11-10 16:03:14 -0800180
bsalomon33435572014-11-05 14:47:41 -0800181class MockGpu : public GrGpu {
182public:
bsalomon682c2692015-05-22 14:01:46 -0700183 MockGpu(GrContext* context, const GrContextOptions& options) : INHERITED(context) {
halcanary385fe4d2015-08-26 13:07:48 -0700184 fCaps.reset(new GrCaps(options));
bsalomon682c2692015-05-22 14:01:46 -0700185 }
mtklein36352bf2015-03-25 18:17:31 -0700186 ~MockGpu() override {}
bsalomon33435572014-11-05 14:47:41 -0800187
bsalomonf0674512015-07-28 13:26:15 -0700188 bool onGetReadPixelsInfo(GrSurface* srcSurface, int readWidth, int readHeight, size_t rowBytes,
189 GrPixelConfig readConfig, DrawPreference*,
190 ReadPixelTempDrawInfo*) override { return false; }
191
192 bool onGetWritePixelsInfo(GrSurface* dstSurface, int width, int height, size_t rowBytes,
193 GrPixelConfig srcConfig, DrawPreference*,
194 WritePixelTempDrawInfo*) override { return false; }
bsalomon39826022015-07-23 08:07:21 -0700195
joshualitt465283c2015-09-11 08:19:35 -0700196 void buildProgramDesc(GrProgramDesc*, const GrPrimitiveProcessor&,
197 const GrPipeline&) const override {}
bsalomon33435572014-11-05 14:47:41 -0800198
mtklein36352bf2015-03-25 18:17:31 -0700199 void discard(GrRenderTarget*) override {}
bsalomon33435572014-11-05 14:47:41 -0800200
joshualitt1cbdcde2015-08-21 11:53:29 -0700201 bool onCopySurface(GrSurface* dst,
202 GrSurface* src,
203 const SkIRect& srcRect,
204 const SkIPoint& dstPoint) override { return false; };
bsalomonf90a02b2014-11-26 12:28:00 -0800205
joshualitt1c735482015-07-13 08:08:25 -0700206 bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) const override {
bsalomonf90a02b2014-11-26 12:28:00 -0800207 return false;
208 }
joshualitt3322fa42014-11-07 08:48:51 -0800209
bsalomon33435572014-11-05 14:47:41 -0800210private:
mtklein36352bf2015-03-25 18:17:31 -0700211 void onResetContext(uint32_t resetBits) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800212
bsalomoncb02b382015-08-12 11:14:50 -0700213 void xferBarrier(GrRenderTarget*, GrXferBarrierType) override {}
214
egdanielb0e1be22015-04-22 13:27:39 -0700215 GrTexture* onCreateTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle lifeCycle,
216 const void* srcData, size_t rowBytes) override {
halcanary96fcdcc2015-08-27 07:41:13 -0700217 return nullptr;
bsalomon33435572014-11-05 14:47:41 -0800218 }
219
egdanielb0e1be22015-04-22 13:27:39 -0700220 GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, GrGpuResource::LifeCycle,
mtklein36352bf2015-03-25 18:17:31 -0700221 const void* srcData) override {
halcanary96fcdcc2015-08-27 07:41:13 -0700222 return nullptr;
bsalomon33435572014-11-05 14:47:41 -0800223 }
224
bsalomon6dc6f5f2015-06-18 09:12:16 -0700225 GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&,
halcanary96fcdcc2015-08-27 07:41:13 -0700226 GrWrapOwnership) override { return nullptr; }
bsalomonf90a02b2014-11-26 12:28:00 -0800227
bsalomon6dc6f5f2015-06-18 09:12:16 -0700228 GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&,
229 GrWrapOwnership) override {
halcanary96fcdcc2015-08-27 07:41:13 -0700230 return nullptr;
bsalomon33435572014-11-05 14:47:41 -0800231 }
232
halcanary96fcdcc2015-08-27 07:41:13 -0700233 GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) override { return nullptr; }
bsalomonf90a02b2014-11-26 12:28:00 -0800234
halcanary96fcdcc2015-08-27 07:41:13 -0700235 GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) override { return nullptr; }
bsalomonf90a02b2014-11-26 12:28:00 -0800236
egdaniel51c8d402015-08-06 10:54:13 -0700237 void onClear(GrRenderTarget*, const SkIRect& rect, GrColor color) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800238
mtklein36352bf2015-03-25 18:17:31 -0700239 void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800240
bsalomone64eb572015-05-07 11:35:55 -0700241 void onDraw(const DrawArgs&, const GrNonInstancedVertices&) override {}
bsalomonf90a02b2014-11-26 12:28:00 -0800242
bsalomon6cb3cbe2015-07-30 07:34:27 -0700243 bool onReadPixels(GrSurface* surface,
bsalomonf90a02b2014-11-26 12:28:00 -0800244 int left, int top, int width, int height,
245 GrPixelConfig,
246 void* buffer,
mtklein36352bf2015-03-25 18:17:31 -0700247 size_t rowBytes) override {
bsalomonf90a02b2014-11-26 12:28:00 -0800248 return false;
bsalomon33435572014-11-05 14:47:41 -0800249 }
250
bsalomon6cb3cbe2015-07-30 07:34:27 -0700251 bool onWritePixels(GrSurface* surface,
252 int left, int top, int width, int height,
253 GrPixelConfig config, const void* buffer,
254 size_t rowBytes) override {
bsalomon33435572014-11-05 14:47:41 -0800255 return false;
256 }
257
mtklein36352bf2015-03-25 18:17:31 -0700258 void onResolveRenderTarget(GrRenderTarget* target) override { return; }
bsalomonf90a02b2014-11-26 12:28:00 -0800259
egdanielec00d942015-09-14 12:56:10 -0700260 GrStencilAttachment* createStencilAttachmentForRenderTarget(const GrRenderTarget*,
261 int width,
262 int height) override {
263 return nullptr;
bsalomon33435572014-11-05 14:47:41 -0800264 }
265
mtklein36352bf2015-03-25 18:17:31 -0700266 void clearStencil(GrRenderTarget* target) override {}
bsalomon33435572014-11-05 14:47:41 -0800267
jvanverth88957922015-07-14 11:02:52 -0700268 GrBackendObject createTestingOnlyBackendTexture(void* pixels, int w, int h,
269 GrPixelConfig config) const override {
270 return 0;
271 }
272 bool isTestingOnlyBackendTexture(GrBackendObject id) const override { return false; }
273 void deleteTestingOnlyBackendTexture(GrBackendObject id) const override {}
jvanverth672bb7f2015-07-13 07:19:57 -0700274
bsalomon33435572014-11-05 14:47:41 -0800275 typedef GrGpu INHERITED;
276};
277
278GrContext* GrContext::CreateMockContext() {
halcanary385fe4d2015-08-26 13:07:48 -0700279 GrContext* context = new GrContext;
bsalomon33435572014-11-05 14:47:41 -0800280
281 context->initMockContext();
282 return context;
283}
284
285void GrContext::initMockContext() {
bsalomon682c2692015-05-22 14:01:46 -0700286 GrContextOptions options;
joshualitt8b081592015-06-01 16:17:03 -0700287 options.fGeometryBufferMapThreshold = 0;
halcanary96fcdcc2015-08-27 07:41:13 -0700288 SkASSERT(nullptr == fGpu);
halcanary385fe4d2015-08-26 13:07:48 -0700289 fGpu = new MockGpu(this, options);
bsalomon33435572014-11-05 14:47:41 -0800290 SkASSERT(fGpu);
bsalomon648c6962015-10-23 09:06:59 -0700291 this->initCommon(options);
bsalomon33435572014-11-05 14:47:41 -0800292
293 // We delete these because we want to test the cache starting with zero resources. Also, none of
294 // these objects are required for any of tests that use this context. TODO: make stop allocating
295 // resources in the buffer pools.
robertphillips77a2e522015-10-17 07:43:27 -0700296 fDrawingManager->abandon();
bsalomon33435572014-11-05 14:47:41 -0800297}