blob: 5d31c15c060573d6010fcd2745c40103ed80b79f [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}
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
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
joshualitt3322fa42014-11-07 08:48:51 -080074 virtual bool canCopySurface(GrSurface* dst,
75 GrSurface* src,
76 const SkIRect& srcRect,
77 const SkIPoint& dstPoint) SK_OVERRIDE { return false; };
78
79 virtual bool copySurface(GrSurface* dst,
80 GrSurface* src,
81 const SkIRect& srcRect,
82 const SkIPoint& dstPoint) SK_OVERRIDE { return false; };
83
bsalomon33435572014-11-05 14:47:41 -080084private:
85 virtual void onResetContext(uint32_t resetBits) { };
86 virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc,
87 const void* srcData,
88 size_t rowBytes) SK_OVERRIDE {
89 return NULL;
90 }
91
92 virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
93 const void* srcData) SK_OVERRIDE {
94 return NULL;
95 }
96
97 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) SK_OVERRIDE {
98 return NULL;
99 }
100
101 virtual GrRenderTarget* onWrapBackendRenderTarget(
102 const GrBackendRenderTargetDesc&) SK_OVERRIDE {
103 return NULL;
104 }
105
106 virtual GrVertexBuffer* onCreateVertexBuffer(size_t size, bool dynamic) SK_OVERRIDE {
107 return NULL;
108 }
109
110 virtual GrIndexBuffer* onCreateIndexBuffer(size_t size, bool dynamic) SK_OVERRIDE {
111 return NULL;
112 }
113
114 virtual void onGpuClear(GrRenderTarget*, const SkIRect* rect, GrColor color,
115 bool canIgnoreRect) SK_OVERRIDE { }
116
117 virtual void onClearStencilClip(GrRenderTarget*,
118 const SkIRect& rect,
119 bool insideClip) SK_OVERRIDE { }
120
joshualitt3322fa42014-11-07 08:48:51 -0800121 virtual void onGpuDraw(const GrDrawTarget::DrawInfo&) SK_OVERRIDE { }
bsalomon33435572014-11-05 14:47:41 -0800122 virtual bool onReadPixels(GrRenderTarget* target,
123 int left, int top, int width, int height,
124 GrPixelConfig,
125 void* buffer,
126 size_t rowBytes) SK_OVERRIDE {
127 return false;
128 }
129
130 virtual bool onWriteTexturePixels(GrTexture* texture,
131 int left, int top, int width, int height,
132 GrPixelConfig config, const void* buffer,
133 size_t rowBytes) SK_OVERRIDE {
134 return false;
135 }
136
137 virtual void onResolveRenderTarget(GrRenderTarget* target) SK_OVERRIDE {
138 return;
139 }
140
141 virtual bool createStencilBufferForRenderTarget(GrRenderTarget*, int width,
142 int height) SK_OVERRIDE {
143 return false;
144 }
145
146 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) SK_OVERRIDE {
147 return false;
148 }
149
150 virtual bool flushGraphicsState(DrawType,
151 const GrClipMaskManager::ScissorState&,
152 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE {
153 return false;
154 }
155
156 virtual void clearStencil(GrRenderTarget* target) SK_OVERRIDE { }
157
158 virtual void didAddGpuTraceMarker() SK_OVERRIDE { }
159 virtual 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}