GrTest.cpp revision b12ea41286ce36e085c5a14711da0cf9f240fdf1
1
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"
10
11#include "GrInOrderDrawBuffer.h"
12#include "GrResourceCache2.h"
13#include "SkString.h"
14
15void GrTestTarget::init(GrContext* ctx, GrDrawTarget* target) {
16    SkASSERT(!fContext);
17
18    fContext.reset(SkRef(ctx));
19    fDrawTarget.reset(SkRef(target));
20
21    SkNEW_IN_TLAZY(&fACR, GrDrawTarget::AutoClipRestore, (target));
22    SkNEW_IN_TLAZY(&fAGP, GrDrawTarget::AutoGeometryPush, (target));
23}
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().
31    tar->init(this, fDrawBuffer);
32}
33
34///////////////////////////////////////////////////////////////////////////////
35
36void GrContext::setMaxTextureSizeOverride(int maxTextureSizeOverride) {
37    fMaxTextureSizeOverride = maxTextureSizeOverride;
38}
39
40void GrContext::purgeAllUnlockedResources() {
41    fResourceCache2->purgeAllUnlocked();
42}
43
44void GrContext::dumpCacheStats(SkString* out) const {
45#if GR_CACHE_STATS
46    fResourceCache2->dumpStats(out);
47#endif
48}
49
50void GrContext::printCacheStats() const {
51    SkString out;
52    this->dumpCacheStats(&out);
53    SkDebugf(out.c_str());
54}
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);
65    SkDebugf(out.c_str());
66}
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);
72    out->appendf("Textures Created: %d\n", fTextureCreates);
73    out->appendf("Texture Uploads: %d\n", fTextureUploads);
74}
75#endif
76
77#if GR_CACHE_STATS
78void GrResourceCache2::dumpStats(SkString* out) const {
79    this->validate();
80
81    int locked = 0;
82    int scratch = 0;
83    int wrapped = 0;
84    size_t unbudgetedSize = 0;
85
86    ResourceList::Iter iter;
87    GrGpuResource* resource = iter.init(fResources, ResourceList::Iter::kHead_IterStart);
88
89    for ( ; resource; resource = iter.next()) {
90        if (!resource->isPurgeable()) {
91            ++locked;
92        }
93        if (resource->cacheAccess().isScratch()) {
94            ++scratch;
95        }
96        if (resource->cacheAccess().isWrapped()) {
97            ++wrapped;
98        }
99        if (!resource->cacheAccess().isBudgeted()) {
100            unbudgetedSize += resource->gpuMemorySize();
101        }
102    }
103
104    float countUtilization = (100.f * fBudgetedCount) / fMaxCount;
105    float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
106
107    out->appendf("Budget: %d items %d bytes\n", fMaxCount, (int)fMaxBytes);
108    out->appendf("\t\tEntry Count: current %d"
109                 " (%d budgeted, %d wrapped, %d locked, %d scratch %.2g%% full), high %d\n",
110                 fCount, fBudgetedCount, wrapped, locked, scratch, countUtilization,
111                 fHighWaterCount);
112    out->appendf("\t\tEntry Bytes: current %d (budgeted %d, %.2g%% full, %d unbudgeted) high %d\n",
113                 (int)fBytes, (int)fBudgetedBytes, byteUtilization,
114                 (int)unbudgetedSize, (int)fHighWaterBytes);
115}
116
117#endif
118
119
120///////////////////////////////////////////////////////////////////////////////
121// Code for the mock context. It's built on a mock GrGpu class that does nothing.
122////
123
124#include "GrBufferAllocPool.h"
125#include "GrInOrderDrawBuffer.h"
126#include "GrGpu.h"
127
128class GrPipeline;
129
130class MockGpu : public GrGpu {
131public:
132    MockGpu(GrContext* context) : INHERITED(context) { fCaps.reset(SkNEW(GrDrawTargetCaps)); }
133    ~MockGpu() SK_OVERRIDE {}
134    bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const SK_OVERRIDE {
135        return true;
136    }
137
138    bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
139                                   int left, int top,
140                                   int width, int height,
141                                   GrPixelConfig config,
142                                   size_t rowBytes) const SK_OVERRIDE { return false; }
143    void buildProgramDesc(GrProgramDesc*,const GrPrimitiveProcessor&,
144                          const GrPipeline&,
145                          const GrProgramDesc::DescInfo&,
146                          const GrBatchTracker&) const SK_OVERRIDE {}
147
148    void discard(GrRenderTarget*) SK_OVERRIDE {}
149
150    bool canCopySurface(const GrSurface* dst,
151                        const GrSurface* src,
152                        const SkIRect& srcRect,
153                        const SkIPoint& dstPoint) SK_OVERRIDE { return false; };
154
155    bool copySurface(GrSurface* dst,
156                     GrSurface* src,
157                     const SkIRect& srcRect,
158                     const SkIPoint& dstPoint) SK_OVERRIDE { return false; };
159
160    bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) SK_OVERRIDE {
161        return false;
162    }
163
164private:
165    void onResetContext(uint32_t resetBits) SK_OVERRIDE {}
166
167    GrTexture* onCreateTexture(const GrSurfaceDesc& desc, bool budgeted, const void* srcData,
168                               size_t rowBytes) SK_OVERRIDE {
169        return NULL;
170    }
171
172    GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, bool budgeted,
173                                         const void* srcData) SK_OVERRIDE {
174        return NULL;
175    }
176
177    GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) SK_OVERRIDE { return NULL; }
178
179    GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) SK_OVERRIDE {
180        return NULL;
181    }
182
183    GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) SK_OVERRIDE { return NULL; }
184
185    GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) SK_OVERRIDE { return NULL; }
186
187    void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color,
188                         bool canIgnoreRect) SK_OVERRIDE {}
189
190    void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) SK_OVERRIDE {}
191
192    void onDraw(const DrawArgs&, const GrDrawTarget::DrawInfo&) SK_OVERRIDE {}
193
194    void onStencilPath(const GrPath* path, const StencilPathState& state) SK_OVERRIDE {}
195
196    void onDrawPath(const DrawArgs&, const GrPath*, const GrStencilSettings&) SK_OVERRIDE {}
197
198    void onDrawPaths(const DrawArgs&,
199                     const GrPathRange*,
200                     const void* indices,
201                     GrDrawTarget::PathIndexType,
202                     const float transformValues[],
203                     GrDrawTarget::PathTransformType,
204                     int count,
205                     const GrStencilSettings&) SK_OVERRIDE {}
206
207    bool onReadPixels(GrRenderTarget* target,
208                      int left, int top, int width, int height,
209                      GrPixelConfig,
210                      void* buffer,
211                      size_t rowBytes) SK_OVERRIDE {
212        return false;
213    }
214
215    bool onWriteTexturePixels(GrTexture* texture,
216                              int left, int top, int width, int height,
217                              GrPixelConfig config, const void* buffer,
218                              size_t rowBytes) SK_OVERRIDE {
219        return false;
220    }
221
222    void onResolveRenderTarget(GrRenderTarget* target) SK_OVERRIDE { return; }
223
224    bool createStencilBufferForRenderTarget(GrRenderTarget*, bool budgeted,
225                                            int width, int height) SK_OVERRIDE {
226        return false;
227    }
228
229    bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) SK_OVERRIDE {
230        return false;
231    }
232
233    void clearStencil(GrRenderTarget* target) SK_OVERRIDE  {}
234
235    void didAddGpuTraceMarker() SK_OVERRIDE {}
236
237    void didRemoveGpuTraceMarker() SK_OVERRIDE {}
238
239    typedef GrGpu INHERITED;
240};
241
242GrContext* GrContext::CreateMockContext() {
243    GrContext* context = SkNEW_ARGS(GrContext, (Options()));
244
245    context->initMockContext();
246    return context;
247}
248
249void GrContext::initMockContext() {
250    SkASSERT(NULL == fGpu);
251    fGpu = SkNEW_ARGS(MockGpu, (this));
252    SkASSERT(fGpu);
253    this->initCommon();
254
255    // We delete these because we want to test the cache starting with zero resources. Also, none of
256    // these objects are required for any of tests that use this context. TODO: make stop allocating
257    // resources in the buffer pools.
258    SkDELETE(fDrawBuffer);
259    SkDELETE(fDrawBufferVBAllocPool);
260    SkDELETE(fDrawBufferIBAllocPool);
261
262    fDrawBuffer = NULL;
263    fDrawBufferVBAllocPool = NULL;
264    fDrawBufferIBAllocPool = NULL;
265}
266