GrTest.cpp revision 5236cf480daf82b2f36e42795abdbbc915533a59
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
14void GrTestTarget::init(GrContext* ctx, GrDrawTarget* target) {
15    SkASSERT(!fContext);
16
17    fContext.reset(SkRef(ctx));
18    fDrawTarget.reset(SkRef(target));
19
20    SkNEW_IN_TLAZY(&fACR, GrDrawTarget::AutoClipRestore, (target));
21    SkNEW_IN_TLAZY(&fAGP, GrDrawTarget::AutoGeometryPush, (target));
22}
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().
30    tar->init(this, fDrawBuffer);
31}
32
33///////////////////////////////////////////////////////////////////////////////
34
35void GrContext::setMaxTextureSizeOverride(int maxTextureSizeOverride) {
36    fMaxTextureSizeOverride = maxTextureSizeOverride;
37}
38
39void GrContext::purgeAllUnlockedResources() {
40    fResourceCache2->purgeAllUnlocked();
41}
42
43///////////////////////////////////////////////////////////////////////////////
44// Code for the mock context. It's built on a mock GrGpu class that does nothing.
45////
46
47#include "GrBufferAllocPool.h"
48#include "GrInOrderDrawBuffer.h"
49#include "GrGpu.h"
50
51class GrOptDrawState;
52
53class MockGpu : public GrGpu {
54public:
55    MockGpu(GrContext* context) : INHERITED(context) { fCaps.reset(SkNEW(GrDrawTargetCaps)); }
56    ~MockGpu() SK_OVERRIDE {}
57    bool canWriteTexturePixels(const GrTexture*, GrPixelConfig srcConfig) const SK_OVERRIDE {
58        return true;
59    }
60
61    bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
62                                   int left, int top,
63                                   int width, int height,
64                                   GrPixelConfig config,
65                                   size_t rowBytes) const SK_OVERRIDE { return false; }
66    void buildProgramDesc(const GrOptDrawState&,
67                          const GrProgramDesc::DescInfo&,
68                          GrGpu::DrawType,
69                          GrProgramDesc* desc) SK_OVERRIDE {}
70
71    void discard(GrRenderTarget*) SK_OVERRIDE {}
72
73    bool canCopySurface(const GrSurface* dst,
74                        const GrSurface* src,
75                        const SkIRect& srcRect,
76                        const SkIPoint& dstPoint) SK_OVERRIDE { return false; };
77
78    bool copySurface(GrSurface* dst,
79                     GrSurface* src,
80                     const SkIRect& srcRect,
81                     const SkIPoint& dstPoint) SK_OVERRIDE { return false; };
82
83    bool initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) SK_OVERRIDE {
84        return false;
85    }
86
87private:
88    void onResetContext(uint32_t resetBits) SK_OVERRIDE {}
89
90    GrTexture* onCreateTexture(const GrSurfaceDesc& desc, bool budgeted, const void* srcData,
91                               size_t rowBytes) SK_OVERRIDE {
92        return NULL;
93    }
94
95    GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc, bool budgeted,
96                                         const void* srcData) SK_OVERRIDE {
97        return NULL;
98    }
99
100    GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) SK_OVERRIDE { return NULL; }
101
102    GrRenderTarget* onWrapBackendRenderTarget(const GrBackendRenderTargetDesc&) SK_OVERRIDE {
103        return NULL;
104    }
105
106    GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) SK_OVERRIDE { return NULL; }
107
108    GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) SK_OVERRIDE { return NULL; }
109
110    void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color,
111                         bool canIgnoreRect) SK_OVERRIDE {}
112
113    void onClearStencilClip(GrRenderTarget*, const SkIRect& rect, bool insideClip) SK_OVERRIDE {}
114
115    void onDraw(const GrOptDrawState&, const GrDrawTarget::DrawInfo&) SK_OVERRIDE {}
116
117    void onStencilPath(const GrPath* path, const StencilPathState& state) SK_OVERRIDE {}
118
119    void onDrawPath(const GrOptDrawState&, const GrPath*, const GrStencilSettings&) SK_OVERRIDE {}
120
121    void onDrawPaths(const GrOptDrawState&,
122                     const GrPathRange*,
123                     const void* indices,
124                     GrDrawTarget::PathIndexType,
125                     const float transformValues[],
126                     GrDrawTarget::PathTransformType,
127                     int count,
128                     const GrStencilSettings&) SK_OVERRIDE {}
129
130    bool onReadPixels(GrRenderTarget* target,
131                      int left, int top, int width, int height,
132                      GrPixelConfig,
133                      void* buffer,
134                      size_t rowBytes) SK_OVERRIDE {
135        return false;
136    }
137
138    bool onWriteTexturePixels(GrTexture* texture,
139                              int left, int top, int width, int height,
140                              GrPixelConfig config, const void* buffer,
141                              size_t rowBytes) SK_OVERRIDE {
142        return false;
143    }
144
145    void onResolveRenderTarget(GrRenderTarget* target) SK_OVERRIDE { return; }
146
147    bool createStencilBufferForRenderTarget(GrRenderTarget*, int width, int height) SK_OVERRIDE {
148        return false;
149    }
150
151    bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) SK_OVERRIDE {
152        return false;
153    }
154
155    void clearStencil(GrRenderTarget* target) SK_OVERRIDE  {}
156
157    void didAddGpuTraceMarker() SK_OVERRIDE {}
158
159    void didRemoveGpuTraceMarker() SK_OVERRIDE {}
160
161    typedef GrGpu INHERITED;
162};
163
164GrContext* GrContext::CreateMockContext() {
165    GrContext* context = SkNEW_ARGS(GrContext, (Options()));
166
167    context->initMockContext();
168    return context;
169}
170
171void GrContext::initMockContext() {
172    SkASSERT(NULL == fGpu);
173    fGpu = SkNEW_ARGS(MockGpu, (this));
174    SkASSERT(fGpu);
175    this->initCommon();
176
177    // We delete these because we want to test the cache starting with zero resources. Also, none of
178    // these objects are required for any of tests that use this context. TODO: make stop allocating
179    // resources in the buffer pools.
180    SkDELETE(fDrawBuffer);
181    SkDELETE(fDrawBufferVBAllocPool);
182    SkDELETE(fDrawBufferIBAllocPool);
183
184    fDrawBuffer = NULL;
185    fDrawBufferVBAllocPool = NULL;
186    fDrawBufferIBAllocPool = NULL;
187}
188