3 * Copyright 2013 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
11 #include "GrGpuResourceCacheAccess.h"
12 #include "GrInOrderDrawBuffer.h"
13 #include "GrResourceCache.h"
16 void GrTestTarget::init(GrContext* ctx, GrDrawTarget* target) {
19 fContext.reset(SkRef(ctx));
20 fDrawTarget.reset(SkRef(target));
22 SkNEW_IN_TLAZY(&fAGP, GrDrawTarget::AutoGeometryPush, (target));
25 void GrContext::getTestTarget(GrTestTarget* tar) {
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().
31 tar->init(this, fDrawBuffer);
34 ///////////////////////////////////////////////////////////////////////////////
36 void GrContext::setMaxTextureSizeOverride(int maxTextureSizeOverride) {
37 fMaxTextureSizeOverride = maxTextureSizeOverride;
40 void GrContext::purgeAllUnlockedResources() {
41 fResourceCache->purgeAllUnlocked();
44 void GrContext::dumpCacheStats(SkString* out) const {
46 fResourceCache->dumpStats(out);
50 void GrContext::printCacheStats() const {
52 this->dumpCacheStats(&out);
53 SkDebugf("%s", out.c_str());
56 void GrContext::dumpGpuStats(SkString* out) const {
58 return fGpu->stats()->dump(out);
62 void GrContext::printGpuStats() const {
64 this->dumpGpuStats(&out);
65 SkDebugf("%s", out.c_str());
69 void GrGpu::Stats::dump(SkString* out) {
70 out->appendf("Render Target Binds: %d\n", fRenderTargetBinds);
71 out->appendf("Shader Compilations: %d\n", fShaderCompilations);
72 out->appendf("Textures Created: %d\n", fTextureCreates);
73 out->appendf("Texture Uploads: %d\n", fTextureUploads);
74 out->appendf("Stencil Buffer Creates: %d\n", fStencilBufferCreates);
79 void GrResourceCache::dumpStats(SkString* out) const {
82 int locked = fNonpurgeableResources.count();
87 size_t fUnbudgetedSize;
89 Stats() : fScratch(0), fWrapped(0), fUnbudgetedSize(0) {}
91 void update(GrGpuResource* resource) {
92 if (resource->cacheAccess().isScratch()) {
95 if (resource->cacheAccess().isWrapped()) {
98 if (!resource->resourcePriv().isBudgeted()) {
99 fUnbudgetedSize += resource->gpuMemorySize();
106 for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
107 stats.update(fNonpurgeableResources[i]);
109 for (int i = 0; i < fPurgeableQueue.count(); ++i) {
110 stats.update(fPurgeableQueue.at(i));
113 float countUtilization = (100.f * fBudgetedCount) / fMaxCount;
114 float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
116 out->appendf("Budget: %d items %d bytes\n", fMaxCount, (int)fMaxBytes);
117 out->appendf("\t\tEntry Count: current %d"
118 " (%d budgeted, %d wrapped, %d locked, %d scratch %.2g%% full), high %d\n",
119 this->getResourceCount(), fBudgetedCount, stats.fWrapped, locked, stats.fScratch,
120 countUtilization, fHighWaterCount);
121 out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
122 SkToInt(fBytes), SkToInt(fBudgetedBytes), byteUtilization,
123 SkToInt(stats.fUnbudgetedSize), SkToInt(fHighWaterBytes));
128 ///////////////////////////////////////////////////////////////////////////////
130 void GrResourceCache::changeTimestamp(uint32_t newTimestamp) { fTimestamp = newTimestamp; }
132 ///////////////////////////////////////////////////////////////////////////////
133 // Code for the mock context. It's built on a mock GrGpu class that does nothing.
136 #include "GrBufferAllocPool.h"
137 #include "GrInOrderDrawBuffer.h"
142 class MockGpu : public GrGpu {
144 MockGpu(GrContext* context) : INHERITED(context) { fCaps.reset(SkNEW(GrDrawTargetCaps)); }
145 ~MockGpu() override {}
146 bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const override {
150 bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
152 int width, int height,
153 GrPixelConfig config,
154 size_t rowBytes) const override { return false; }
155 void buildProgramDesc(GrProgramDesc*,const GrPrimitiveProcessor&,
157 const GrBatchTracker&) const override {}
159 void discard(GrRenderTarget*) override {}
161 bool canCopySurface(const GrSurface* dst,
162 const GrSurface* src,
163 const SkIRect& srcRect,
164 const SkIPoint& dstPoint) override { return false; };
166 bool copySurface(GrSurface* dst,
168 const SkIRect& srcRect,
169 const SkIPoint& dstPoint) override { return false; };
171 bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) override {
176 void onResetContext(uint32_t resetBits) override {}
178 GrTexture* onCreateTexture(const GrSurfaceDesc& desc, bool budgeted, const void* srcData,
179 size_t rowBytes) override {
183 GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, bool budgeted,
184 const void* srcData) override {
188 GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) override { return NULL; }
190 GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) override {
194 GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) override { return NULL; }
196 GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) override { return NULL; }
198 void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color,
199 bool canIgnoreRect) override {}
201 void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) override {}
203 void onDraw(const DrawArgs&, const GrDrawTarget::DrawInfo&) override {}
205 void onStencilPath(const GrPath* path, const StencilPathState& state) override {}
207 void onDrawPath(const DrawArgs&, const GrPath*, const GrStencilSettings&) override {}
209 void onDrawPaths(const DrawArgs&,
212 GrDrawTarget::PathIndexType,
213 const float transformValues[],
214 GrDrawTarget::PathTransformType,
216 const GrStencilSettings&) override {}
218 bool onReadPixels(GrRenderTarget* target,
219 int left, int top, int width, int height,
222 size_t rowBytes) override {
226 bool onWriteTexturePixels(GrTexture* texture,
227 int left, int top, int width, int height,
228 GrPixelConfig config, const void* buffer,
229 size_t rowBytes) override {
233 void onResolveRenderTarget(GrRenderTarget* target) override { return; }
235 bool createStencilBufferForRenderTarget(GrRenderTarget*, int width, int height) override {
239 bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) override {
243 void clearStencil(GrRenderTarget* target) override {}
245 void didAddGpuTraceMarker() override {}
247 void didRemoveGpuTraceMarker() override {}
249 typedef GrGpu INHERITED;
252 GrContext* GrContext::CreateMockContext() {
253 GrContext* context = SkNEW_ARGS(GrContext, (Options()));
255 context->initMockContext();
259 void GrContext::initMockContext() {
260 SkASSERT(NULL == fGpu);
261 fGpu = SkNEW_ARGS(MockGpu, (this));
265 // We delete these because we want to test the cache starting with zero resources. Also, none of
266 // these objects are required for any of tests that use this context. TODO: make stop allocating
267 // resources in the buffer pools.
268 SkDELETE(fDrawBuffer);
269 SkDELETE(fDrawBufferVBAllocPool);
270 SkDELETE(fDrawBufferIBAllocPool);
273 fDrawBufferVBAllocPool = NULL;
274 fDrawBufferIBAllocPool = NULL;