blob: efb37ca88c38ac023c44684a39b02802de68c324 [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"
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000012#include "GrResourceCache.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() {
commit-bot@chromium.org95c20032014-05-09 14:29:32 +000041 fResourceCache->purgeAllUnlocked();
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000042}
bsalomon820dd6c2014-11-05 12:09:45 -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
52class MockGpu : public GrGpu {
53public:
54 MockGpu(GrContext* context) : INHERITED(context) { fCaps.reset(SkNEW(GrDrawTargetCaps)); }
55 virtual ~MockGpu() { }
56 virtual bool canWriteTexturePixels(const GrTexture*,
57 GrPixelConfig srcConfig) const SK_OVERRIDE {
58 return true;
59 }
60
61 virtual 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 virtual void buildProgramDesc(const GrOptDrawState&,
67 const GrProgramDesc::DescInfo&,
68 GrGpu::DrawType,
69 const GrDeviceCoordTexture* dstCopy,
70 GrProgramDesc* desc) SK_OVERRIDE { }
71
72 virtual void discard(GrRenderTarget*) SK_OVERRIDE { }
73
74private:
75 virtual void onResetContext(uint32_t resetBits) { };
76 virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc,
77 const void* srcData,
78 size_t rowBytes) SK_OVERRIDE {
79 return NULL;
80 }
81
82 virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
83 const void* srcData) SK_OVERRIDE {
84 return NULL;
85 }
86
87 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) SK_OVERRIDE {
88 return NULL;
89 }
90
91 virtual GrRenderTarget* onWrapBackendRenderTarget(
92 const GrBackendRenderTargetDesc&) SK_OVERRIDE {
93 return NULL;
94 }
95
96 virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) SK_OVERRIDE {
97 return NULL;
98 }
99
100 virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) SK_OVERRIDE {
101 return NULL;
102 }
103
104 virtual void onGpuClear(GrRenderTarget*, const SkIRect* rect, GrColor color,
105 bool canIgnoreRect) SK_OVERRIDE { }
106
107 virtual void onClearStencilClip(GrRenderTarget*,
108 const SkIRect& rect,
109 bool insideClip) SK_OVERRIDE { }
110
111 virtual void onGpuDraw(const DrawInfo&) SK_OVERRIDE { }
112 virtual bool onReadPixels(GrRenderTarget* target,
113 int left, int top, int width, int height,
114 GrPixelConfig,
115 void* buffer,
116 size_t rowBytes) SK_OVERRIDE {
117 return false;
118 }
119
120 virtual bool onWriteTexturePixels(GrTexture* texture,
121 int left, int top, int width, int height,
122 GrPixelConfig config, const void* buffer,
123 size_t rowBytes) SK_OVERRIDE {
124 return false;
125 }
126
127 virtual void onResolveRenderTarget(GrRenderTarget* target) SK_OVERRIDE {
128 return;
129 }
130
131 virtual bool createStencilBufferForRenderTarget(GrRenderTarget*, int width,
132 int height) SK_OVERRIDE {
133 return false;
134 }
135
136 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) SK_OVERRIDE {
137 return false;
138 }
139
140 virtual bool flushGraphicsState(DrawType,
141 const GrClipMaskManager::ScissorState&,
142 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE {
143 return false;
144 }
145
146 virtual void clearStencil(GrRenderTarget* target) SK_OVERRIDE { }
147
148 virtual void didAddGpuTraceMarker() SK_OVERRIDE { }
149 virtual void didRemoveGpuTraceMarker() SK_OVERRIDE { }
150
151 typedef GrGpu INHERITED;
152};
153
154GrContext* GrContext::CreateMockContext() {
155 GrContext* context = SkNEW_ARGS(GrContext, (Options()));
156
157 context->initMockContext();
158 return context;
159}
160
161void GrContext::initMockContext() {
162 SkASSERT(NULL == fGpu);
163 fGpu = SkNEW_ARGS(MockGpu, (this));
164 SkASSERT(fGpu);
165 this->initCommon();
166
167 // We delete these because we want to test the cache starting with zero resources. Also, none of
168 // these objects are required for any of tests that use this context. TODO: make stop allocating
169 // resources in the buffer pools.
170 SkDELETE(fDrawBuffer);
171 SkDELETE(fDrawBufferVBAllocPool);
172 SkDELETE(fDrawBufferIBAllocPool);
173
174 fDrawBuffer = NULL;
175 fDrawBufferVBAllocPool = NULL;
176 fDrawBufferIBAllocPool = NULL;
177}