C++11 override should now be supported by all of {bots,Chrome,Android,Mozilla}
[platform/upstream/libSkiaSharp.git] / src / gpu / GrTest.cpp
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 "GrGpuResourceCacheAccess.h"
12 #include "GrInOrderDrawBuffer.h"
13 #include "GrResourceCache.h"
14 #include "SkString.h"
15
16 void GrTestTarget::init(GrContext* ctx, GrDrawTarget* target) {
17     SkASSERT(!fContext);
18
19     fContext.reset(SkRef(ctx));
20     fDrawTarget.reset(SkRef(target));
21
22     SkNEW_IN_TLAZY(&fAGP, GrDrawTarget::AutoGeometryPush, (target));
23 }
24
25 void 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
36 void GrContext::setMaxTextureSizeOverride(int maxTextureSizeOverride) {
37     fMaxTextureSizeOverride = maxTextureSizeOverride;
38 }
39
40 void GrContext::purgeAllUnlockedResources() {
41     fResourceCache->purgeAllUnlocked();
42 }
43
44 void GrContext::dumpCacheStats(SkString* out) const {
45 #if GR_CACHE_STATS
46     fResourceCache->dumpStats(out);
47 #endif
48 }
49
50 void GrContext::printCacheStats() const {
51     SkString out;
52     this->dumpCacheStats(&out);
53     SkDebugf("%s", out.c_str());
54 }
55
56 void GrContext::dumpGpuStats(SkString* out) const {
57 #if GR_GPU_STATS
58     return fGpu->stats()->dump(out);
59 #endif
60 }
61
62 void GrContext::printGpuStats() const {
63     SkString out;
64     this->dumpGpuStats(&out);
65     SkDebugf("%s", out.c_str());
66 }
67
68 #if GR_GPU_STATS
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);
75 }
76 #endif
77
78 #if GR_CACHE_STATS
79 void GrResourceCache::dumpStats(SkString* out) const {
80     this->validate();
81
82     int locked = fNonpurgeableResources.count();
83
84     struct Stats {
85         int fScratch;
86         int fWrapped;
87         size_t fUnbudgetedSize;
88
89         Stats() : fScratch(0), fWrapped(0), fUnbudgetedSize(0) {}
90
91         void update(GrGpuResource* resource) {
92             if (resource->cacheAccess().isScratch()) {
93                 ++fScratch;
94             }
95             if (resource->cacheAccess().isWrapped()) {
96                 ++fWrapped;
97             }
98             if (!resource->resourcePriv().isBudgeted()) {
99                 fUnbudgetedSize += resource->gpuMemorySize();
100             }
101         }
102     };
103
104     Stats stats;
105
106     for (int i = 0; i < fNonpurgeableResources.count(); ++i) {
107         stats.update(fNonpurgeableResources[i]);
108     }
109     for (int i = 0; i < fPurgeableQueue.count(); ++i) {
110         stats.update(fPurgeableQueue.at(i));
111     }
112
113     float countUtilization = (100.f * fBudgetedCount) / fMaxCount;
114     float byteUtilization = (100.f * fBudgetedBytes) / fMaxBytes;
115
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));
124 }
125
126 #endif
127
128 ///////////////////////////////////////////////////////////////////////////////
129
130 void GrResourceCache::changeTimestamp(uint32_t newTimestamp) { fTimestamp = newTimestamp; }
131
132 ///////////////////////////////////////////////////////////////////////////////
133 // Code for the mock context. It's built on a mock GrGpu class that does nothing.
134 ////
135
136 #include "GrBufferAllocPool.h"
137 #include "GrInOrderDrawBuffer.h"
138 #include "GrGpu.h"
139
140 class GrPipeline;
141
142 class MockGpu : public GrGpu {
143 public:
144     MockGpu(GrContext* context) : INHERITED(context) { fCaps.reset(SkNEW(GrDrawTargetCaps)); }
145     ~MockGpu() override {}
146     bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const override {
147         return true;
148     }
149
150     bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
151                                    int left, int top,
152                                    int width, int height,
153                                    GrPixelConfig config,
154                                    size_t rowBytes) const override { return false; }
155     void buildProgramDesc(GrProgramDesc*,const GrPrimitiveProcessor&,
156                           const GrPipeline&,
157                           const GrBatchTracker&) const override {}
158
159     void discard(GrRenderTarget*) override {}
160
161     bool canCopySurface(const GrSurface* dst,
162                         const GrSurface* src,
163                         const SkIRect& srcRect,
164                         const SkIPoint& dstPoint) override { return false; };
165
166     bool copySurface(GrSurface* dst,
167                      GrSurface* src,
168                      const SkIRect& srcRect,
169                      const SkIPoint& dstPoint) override { return false; };
170
171     bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) override {
172         return false;
173     }
174
175 private:
176     void onResetContext(uint32_t resetBits) override {}
177
178     GrTexture* onCreateTexture(const GrSurfaceDesc& desc, bool budgeted, const void* srcData,
179                                size_t rowBytes) override {
180         return NULL;
181     }
182
183     GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, bool budgeted,
184                                          const void* srcData) override {
185         return NULL;
186     }
187
188     GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) override { return NULL; }
189
190     GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) override {
191         return NULL;
192     }
193
194     GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) override { return NULL; }
195
196     GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) override { return NULL; }
197
198     void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color,
199                          bool canIgnoreRect) override {}
200
201     void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) override {}
202
203     void onDraw(const DrawArgs&, const GrDrawTarget::DrawInfo&) override {}
204
205     void onStencilPath(const GrPath* path, const StencilPathState& state) override {}
206
207     void onDrawPath(const DrawArgs&, const GrPath*, const GrStencilSettings&) override {}
208
209     void onDrawPaths(const DrawArgs&,
210                      const GrPathRange*,
211                      const void* indices,
212                      GrDrawTarget::PathIndexType,
213                      const float transformValues[],
214                      GrDrawTarget::PathTransformType,
215                      int count,
216                      const GrStencilSettings&) override {}
217
218     bool onReadPixels(GrRenderTarget* target,
219                       int left, int top, int width, int height,
220                       GrPixelConfig,
221                       void* buffer,
222                       size_t rowBytes) override {
223         return false;
224     }
225
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 {
230         return false;
231     }
232
233     void onResolveRenderTarget(GrRenderTarget* target) override { return; }
234
235     bool createStencilBufferForRenderTarget(GrRenderTarget*, int width, int height) override {
236         return false;
237     }
238
239     bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) override {
240         return false;
241     }
242
243     void clearStencil(GrRenderTarget* target) override  {}
244
245     void didAddGpuTraceMarker() override {}
246
247     void didRemoveGpuTraceMarker() override {}
248
249     typedef GrGpu INHERITED;
250 };
251
252 GrContext* GrContext::CreateMockContext() {
253     GrContext* context = SkNEW_ARGS(GrContext, (Options()));
254
255     context->initMockContext();
256     return context;
257 }
258
259 void GrContext::initMockContext() {
260     SkASSERT(NULL == fGpu);
261     fGpu = SkNEW_ARGS(MockGpu, (this));
262     SkASSERT(fGpu);
263     this->initCommon();
264
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);
271
272     fDrawBuffer = NULL;
273     fDrawBufferVBAllocPool = NULL;
274     fDrawBufferIBAllocPool = NULL;
275 }