GrTest.cpp revision 54e0c12a5ab2d83fe249dd199d6879e8c0f04404
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    virtual ~MockGpu() { }
57    virtual bool canWriteTexturePixels(const GrTexture*,
58                                       GrPixelConfig srcConfig) const SK_OVERRIDE {
59        return true;
60    }
61
62    virtual bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
63                                           int left, int top,
64                                           int width, int height,
65                                           GrPixelConfig config,
66                                           size_t rowBytes) const SK_OVERRIDE { return false; }
67    virtual void buildProgramDesc(const GrOptDrawState&,
68                                  const GrProgramDesc::DescInfo&,
69                                  GrGpu::DrawType,
70                                  const GrDeviceCoordTexture* dstCopy,
71                                  GrProgramDesc* desc) SK_OVERRIDE { }
72
73    virtual void discard(GrRenderTarget*) SK_OVERRIDE { }
74
75    virtual bool canCopySurface(const GrSurface* dst,
76                                const GrSurface* src,
77                                const SkIRect& srcRect,
78                                const SkIPoint& dstPoint) SK_OVERRIDE { return false; };
79
80    virtual bool copySurface(GrSurface* dst,
81                             GrSurface* src,
82                             const SkIRect& srcRect,
83                             const SkIPoint& dstPoint) SK_OVERRIDE { return false; };
84
85private:
86    virtual void onResetContext(uint32_t resetBits) { };
87    virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc,
88                                       const void* srcData,
89                                       size_t rowBytes)  SK_OVERRIDE {
90        return NULL;
91    }
92
93    virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
94                                                 const void* srcData)  SK_OVERRIDE {
95        return NULL;
96    }
97
98    virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&)  SK_OVERRIDE {
99        return NULL;
100    }
101
102    virtual GrRenderTarget* onWrapBackendRenderTarget(
103                                    const GrBackendRenderTargetDesc&) SK_OVERRIDE {
104        return NULL;
105    }
106
107    virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic)  SK_OVERRIDE {
108        return NULL;
109    }
110
111    virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic)  SK_OVERRIDE {
112        return NULL;
113    }
114
115    virtual void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color,
116                         bool canIgnoreRect)  SK_OVERRIDE { }
117
118    virtual void onClearStencilClip(GrRenderTarget*,
119                                    const SkIRect& rect,
120                                    bool insideClip)  SK_OVERRIDE { }
121
122    virtual void onDraw(const GrOptDrawState&, const GrDrawTarget::DrawInfo&)  SK_OVERRIDE { }
123    virtual bool onReadPixels(GrRenderTarget* target,
124                              int left, int top, int width, int height,
125                              GrPixelConfig,
126                              void* buffer,
127                              size_t rowBytes)  SK_OVERRIDE {
128        return false;
129    }
130
131    virtual bool onWriteTexturePixels(GrTexture* texture,
132                                      int left, int top, int width, int height,
133                                      GrPixelConfig config, const void* buffer,
134                                      size_t rowBytes)  SK_OVERRIDE {
135        return false;
136    }
137
138    virtual void onResolveRenderTarget(GrRenderTarget* target)  SK_OVERRIDE {
139        return;
140    }
141
142    virtual bool createStencilBufferForRenderTarget(GrRenderTarget*, int width,
143                                                    int height) SK_OVERRIDE {
144        return false;
145    }
146
147    virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*)  SK_OVERRIDE {
148        return false;
149    }
150
151    virtual bool flushGraphicsState(const GrOptDrawState&,
152                                    DrawType,
153                                    const GrDeviceCoordTexture* dstCopy)  SK_OVERRIDE {
154        return false;
155    }
156
157    virtual void clearStencil(GrRenderTarget* target)  SK_OVERRIDE  { }
158
159    virtual void didAddGpuTraceMarker() SK_OVERRIDE { }
160    virtual void didRemoveGpuTraceMarker() SK_OVERRIDE { }
161
162    typedef GrGpu INHERITED;
163};
164
165GrContext* GrContext::CreateMockContext() {
166    GrContext* context = SkNEW_ARGS(GrContext, (Options()));
167
168    context->initMockContext();
169    return context;
170}
171
172void GrContext::initMockContext() {
173    SkASSERT(NULL == fGpu);
174    fGpu = SkNEW_ARGS(MockGpu, (this));
175    SkASSERT(fGpu);
176    this->initCommon();
177
178    // We delete these because we want to test the cache starting with zero resources. Also, none of
179    // these objects are required for any of tests that use this context. TODO: make stop allocating
180    // resources in the buffer pools.
181    SkDELETE(fDrawBuffer);
182    SkDELETE(fDrawBufferVBAllocPool);
183    SkDELETE(fDrawBufferIBAllocPool);
184
185    fDrawBuffer = NULL;
186    fDrawBufferVBAllocPool = NULL;
187    fDrawBufferIBAllocPool = NULL;
188}
189