blob: 528698478a140e7de011249fb606199ddcf48d78 [file] [log] [blame]
commit-bot@chromium.org78a10782013-08-21 19:27:48 +00001
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
joshualitt50408ad2014-11-03 12:31:14 -080011#include "GrInOrderDrawBuffer.h"
bsalomon71cb0c22014-11-14 12:10:14 -080012#include "GrResourceCache2.h"
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000013
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(&fASR, GrDrawTarget::AutoStateRestore, (target, GrDrawTarget::kReset_ASRInit));
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().
joshualitt50408ad2014-11-03 12:31:14 -080031 tar->init(this, fDrawBuffer);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000032}
33
34///////////////////////////////////////////////////////////////////////////////
35
36void GrContext::setMaxTextureSizeOverride(int maxTextureSizeOverride) {
37 fMaxTextureSizeOverride = maxTextureSizeOverride;
38}
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000039
40void GrContext::purgeAllUnlockedResources() {
bsalomon71cb0c22014-11-14 12:10:14 -080041 fResourceCache2->purgeAllUnlocked();
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000042}
bsalomon33435572014-11-05 14:47:41 -080043
44///////////////////////////////////////////////////////////////////////////////
45// Code for the mock context. It's built on a mock GrGpu class that does nothing.
46////
47
48#include "GrBufferAllocPool.h"
49#include "GrInOrderDrawBuffer.h"
50#include "GrGpu.h"
51
joshualittd53a8272014-11-10 16:03:14 -080052class GrOptDrawState;
53
bsalomon33435572014-11-05 14:47:41 -080054class MockGpu : public GrGpu {
55public:
56 MockGpu(GrContext* context) : INHERITED(context) { fCaps.reset(SkNEW(GrDrawTargetCaps)); }
57 virtual ~MockGpu() { }
58 virtual bool canWriteTexturePixels(const GrTexture*,
59 GrPixelConfig srcConfig) const SK_OVERRIDE {
60 return true;
61 }
62
63 virtual bool readPixelsWillPayForYFlip(GrRenderTarget* renderTarget,
64 int left, int top,
65 int width, int height,
66 GrPixelConfig config,
67 size_t rowBytes) const SK_OVERRIDE { return false; }
68 virtual void buildProgramDesc(const GrOptDrawState&,
69 const GrProgramDesc::DescInfo&,
70 GrGpu::DrawType,
71 const GrDeviceCoordTexture* dstCopy,
72 GrProgramDesc* desc) SK_OVERRIDE { }
73
74 virtual void discard(GrRenderTarget*) SK_OVERRIDE { }
75
joshualitt3322fa42014-11-07 08:48:51 -080076 virtual bool canCopySurface(GrSurface* dst,
77 GrSurface* src,
78 const SkIRect& srcRect,
79 const SkIPoint& dstPoint) SK_OVERRIDE { return false; };
80
81 virtual bool copySurface(GrSurface* dst,
82 GrSurface* src,
83 const SkIRect& srcRect,
84 const SkIPoint& dstPoint) SK_OVERRIDE { return false; };
85
bsalomon33435572014-11-05 14:47:41 -080086private:
87 virtual void onResetContext(uint32_t resetBits) { };
88 virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc,
89 const void* srcData,
90 size_t rowBytes) SK_OVERRIDE {
91 return NULL;
92 }
93
94 virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
95 const void* srcData) SK_OVERRIDE {
96 return NULL;
97 }
98
99 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) SK_OVERRIDE {
100 return NULL;
101 }
102
103 virtual GrRenderTarget* onWrapBackendRenderTarget(
104 const GrBackendRenderTargetDesc&) SK_OVERRIDE {
105 return NULL;
106 }
107
108 virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) SK_OVERRIDE {
109 return NULL;
110 }
111
112 virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) SK_OVERRIDE {
113 return NULL;
114 }
115
joshualitt4b68ec02014-11-07 14:11:45 -0800116 virtual void onClear(GrRenderTarget*, const SkIRect* rect, GrColor color,
117 bool canIgnoreRect) SK_OVERRIDE { }
bsalomon33435572014-11-05 14:47:41 -0800118
119 virtual void onClearStencilClip(GrRenderTarget*,
120 const SkIRect& rect,
121 bool insideClip) SK_OVERRIDE { }
122
joshualittd53a8272014-11-10 16:03:14 -0800123 virtual void onDraw(const GrOptDrawState&, const GrDrawTarget::DrawInfo&) SK_OVERRIDE { }
bsalomon33435572014-11-05 14:47:41 -0800124 virtual bool onReadPixels(GrRenderTarget* target,
125 int left, int top, int width, int height,
126 GrPixelConfig,
127 void* buffer,
128 size_t rowBytes) SK_OVERRIDE {
129 return false;
130 }
131
132 virtual bool onWriteTexturePixels(GrTexture* texture,
133 int left, int top, int width, int height,
134 GrPixelConfig config, const void* buffer,
135 size_t rowBytes) SK_OVERRIDE {
136 return false;
137 }
138
139 virtual void onResolveRenderTarget(GrRenderTarget* target) SK_OVERRIDE {
140 return;
141 }
142
143 virtual bool createStencilBufferForRenderTarget(GrRenderTarget*, int width,
144 int height) SK_OVERRIDE {
145 return false;
146 }
147
148 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) SK_OVERRIDE {
149 return false;
150 }
151
joshualittd53a8272014-11-10 16:03:14 -0800152 virtual bool flushGraphicsState(const GrOptDrawState&,
153 DrawType,
bsalomon33435572014-11-05 14:47:41 -0800154 const GrClipMaskManager::ScissorState&,
155 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE {
156 return false;
157 }
158
159 virtual void clearStencil(GrRenderTarget* target) SK_OVERRIDE { }
160
161 virtual void didAddGpuTraceMarker() SK_OVERRIDE { }
162 virtual void didRemoveGpuTraceMarker() SK_OVERRIDE { }
163
164 typedef GrGpu INHERITED;
165};
166
167GrContext* GrContext::CreateMockContext() {
168 GrContext* context = SkNEW_ARGS(GrContext, (Options()));
169
170 context->initMockContext();
171 return context;
172}
173
174void GrContext::initMockContext() {
175 SkASSERT(NULL == fGpu);
176 fGpu = SkNEW_ARGS(MockGpu, (this));
177 SkASSERT(fGpu);
178 this->initCommon();
179
180 // We delete these because we want to test the cache starting with zero resources. Also, none of
181 // these objects are required for any of tests that use this context. TODO: make stop allocating
182 // resources in the buffer pools.
183 SkDELETE(fDrawBuffer);
184 SkDELETE(fDrawBufferVBAllocPool);
185 SkDELETE(fDrawBufferIBAllocPool);
186
187 fDrawBuffer = NULL;
188 fDrawBufferVBAllocPool = NULL;
189 fDrawBufferIBAllocPool = NULL;
190}