blob: f861e42abe73413ef531c99f284239dfc76cf518 [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
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000020 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().
joshualitt50408ad2014-11-03 12:31:14 -080030 tar->init(this, fDrawBuffer);
commit-bot@chromium.org78a10782013-08-21 19:27:48 +000031}
32
33///////////////////////////////////////////////////////////////////////////////
34
35void GrContext::setMaxTextureSizeOverride(int maxTextureSizeOverride) {
36 fMaxTextureSizeOverride = maxTextureSizeOverride;
37}
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000038
39void GrContext::purgeAllUnlockedResources() {
bsalomon71cb0c22014-11-14 12:10:14 -080040 fResourceCache2->purgeAllUnlocked();
robertphillips@google.com94d8f1e2013-12-18 17:25:33 +000041}
bsalomon33435572014-11-05 14:47:41 -080042
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
joshualittd53a8272014-11-10 16:03:14 -080051class GrOptDrawState;
52
bsalomon33435572014-11-05 14:47:41 -080053class MockGpu : public GrGpu {
54public:
55 MockGpu(GrContext* context) : INHERITED(context) { fCaps.reset(SkNEW(GrDrawTargetCaps)); }
bsalomon8ee4e602014-11-26 10:20:45 -080056 virtual ~MockGpu() { }
57 virtual bool canWriteTexturePixels(const GrTexture*,
58 GrPixelConfig srcConfig) const SK_OVERRIDE {
bsalomon33435572014-11-05 14:47:41 -080059 return true;
60 }
61
bsalomon8ee4e602014-11-26 10:20:45 -080062 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 GrProgramDesc* desc) SK_OVERRIDE { }
bsalomon33435572014-11-05 14:47:41 -080071
bsalomon8ee4e602014-11-26 10:20:45 -080072 virtual void discard(GrRenderTarget*) SK_OVERRIDE { }
bsalomon33435572014-11-05 14:47:41 -080073
bsalomon8ee4e602014-11-26 10:20:45 -080074 virtual bool canCopySurface(const GrSurface* dst,
75 const GrSurface* src,
76 const SkIRect& srcRect,
77 const SkIPoint& dstPoint) SK_OVERRIDE { return false; };
joshualitt3322fa42014-11-07 08:48:51 -080078
bsalomon8ee4e602014-11-26 10:20:45 -080079 virtual bool copySurface(GrSurface* dst,
80 GrSurface* src,
81 const SkIRect& srcRect,
82 const SkIPoint& dstPoint) SK_OVERRIDE { return false; };
joshualitt3322fa42014-11-07 08:48:51 -080083
bsalomon33435572014-11-05 14:47:41 -080084private:
bsalomon8ee4e602014-11-26 10:20:45 -080085 virtual void onResetContext(uint32_t resetBits) { };
86 virtual GrTexture* onCreateTexture(const GrSurfaceDesc& desc,
87 const void* srcData,
88 size_t rowBytes) SK_OVERRIDE {
bsalomon33435572014-11-05 14:47:41 -080089 return NULL;
90 }
91
bsalomon8ee4e602014-11-26 10:20:45 -080092 virtual GrTexture* onCreateCompressedTexture(const GrSurfaceDesc& desc,
93 const void* srcData) SK_OVERRIDE {
bsalomon33435572014-11-05 14:47:41 -080094 return NULL;
95 }
96
bsalomon8ee4e602014-11-26 10:20:45 -080097 virtual GrTexture* onWrapBackendTexture(const GrBackendTextureDesc&) SK_OVERRIDE {
bsalomon33435572014-11-05 14:47:41 -080098 return NULL;
99 }
100
bsalomon8ee4e602014-11-26 10:20:45 -0800101 virtual GrRenderTarget* onWrapBackendRenderTarget(
102 const GrBackendRenderTargetDesc&) SK_OVERRIDE {
103 return NULL;
bsalomon33435572014-11-05 14:47:41 -0800104 }
105
bsalomon8ee4e602014-11-26 10:20:45 -0800106 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 onClear(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
121 virtual void onDraw(const GrOptDrawState&, const GrDrawTarget::DrawInfo&) SK_OVERRIDE { }
122 virtual bool onReadPixels(GrRenderTarget* target,
bsalomon33435572014-11-05 14:47:41 -0800123 int left, int top, int width, int height,
bsalomon8ee4e602014-11-26 10:20:45 -0800124 GrPixelConfig,
125 void* buffer,
126 size_t rowBytes) SK_OVERRIDE {
bsalomon33435572014-11-05 14:47:41 -0800127 return false;
128 }
129
bsalomon8ee4e602014-11-26 10:20:45 -0800130 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 {
bsalomon33435572014-11-05 14:47:41 -0800134 return false;
135 }
136
bsalomon8ee4e602014-11-26 10:20:45 -0800137 virtual void onResolveRenderTarget(GrRenderTarget* target) SK_OVERRIDE {
138 return;
139 }
140
141 virtual bool createStencilBufferForRenderTarget(GrRenderTarget*, int width,
142 int height) SK_OVERRIDE {
bsalomon33435572014-11-05 14:47:41 -0800143 return false;
144 }
145
bsalomon8ee4e602014-11-26 10:20:45 -0800146 virtual bool attachStencilBufferToRenderTarget(GrStencilBuffer*, GrRenderTarget*) SK_OVERRIDE {
147 return false;
148 }
bsalomon33435572014-11-05 14:47:41 -0800149
bsalomon8ee4e602014-11-26 10:20:45 -0800150 virtual bool flushGraphicsState(const GrOptDrawState&, DrawType) SK_OVERRIDE {
151 return false;
152 }
bsalomon33435572014-11-05 14:47:41 -0800153
bsalomon8ee4e602014-11-26 10:20:45 -0800154 virtual void clearStencil(GrRenderTarget* target) SK_OVERRIDE { }
bsalomon33435572014-11-05 14:47:41 -0800155
bsalomon8ee4e602014-11-26 10:20:45 -0800156 virtual void didAddGpuTraceMarker() SK_OVERRIDE { }
157 virtual void didRemoveGpuTraceMarker() SK_OVERRIDE { }
bsalomon33435572014-11-05 14:47:41 -0800158
159 typedef GrGpu INHERITED;
160};
161
162GrContext* GrContext::CreateMockContext() {
163 GrContext* context = SkNEW_ARGS(GrContext, (Options()));
164
165 context->initMockContext();
166 return context;
167}
168
169void GrContext::initMockContext() {
170 SkASSERT(NULL == fGpu);
171 fGpu = SkNEW_ARGS(MockGpu, (this));
172 SkASSERT(fGpu);
173 this->initCommon();
174
175 // We delete these because we want to test the cache starting with zero resources. Also, none of
176 // these objects are required for any of tests that use this context. TODO: make stop allocating
177 // resources in the buffer pools.
178 SkDELETE(fDrawBuffer);
179 SkDELETE(fDrawBufferVBAllocPool);
180 SkDELETE(fDrawBufferIBAllocPool);
181
182 fDrawBuffer = NULL;
183 fDrawBufferVBAllocPool = NULL;
184 fDrawBufferIBAllocPool = NULL;
185}