blob: f7b9537ecc8eb7f98e372b73cc69d214488a8cc9 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 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.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.comac10a2d2010-12-22 21:39:39 +000010#include "GrGpu.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000011
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000012#include "GrBufferAllocPool.h"
bsalomon@google.com558a75b2011-08-08 17:01:14 +000013#include "GrContext.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000014#include "GrDrawTargetCaps.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015#include "GrIndexBuffer.h"
tomhudson@google.comdd182cb2012-02-10 21:01:00 +000016#include "GrStencilBuffer.h"
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000017#include "GrVertexBuffer.h"
bsalomon@google.com1c13c962011-02-14 16:51:21 +000018
19// probably makes no sense for this to be less than a page
bsalomon@google.comee435122011-07-01 14:57:55 +000020static const size_t VERTEX_POOL_VB_SIZE = 1 << 18;
21static const int VERTEX_POOL_VB_COUNT = 4;
bsalomon@google.com25fd36c2011-07-06 17:41:08 +000022static const size_t INDEX_POOL_IB_SIZE = 1 << 16;
23static const int INDEX_POOL_IB_COUNT = 4;
reed@google.comac10a2d2010-12-22 21:39:39 +000024
bsalomon@google.comd302f142011-03-03 13:54:13 +000025////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000026
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000027#define DEBUG_INVAL_BUFFER 0xdeadcafe
28#define DEBUG_INVAL_START_IDX -1
29
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000030GrGpu::GrGpu(GrContext* context)
joshualitt6db519c2014-10-29 08:48:18 -070031 : INHERITED(context)
bsalomon@google.com979432b2011-11-05 21:38:22 +000032 , fResetTimestamp(kExpiredTimestamp+1)
bsalomon@google.com0a208a12013-06-28 18:57:35 +000033 , fResetBits(kAll_GrBackendState)
bsalomon@google.com8fe72472011-03-30 21:26:44 +000034 , fVertexPool(NULL)
35 , fIndexPool(NULL)
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000036 , fVertexPoolUseCnt(0)
37 , fIndexPoolUseCnt(0)
bsalomon@google.com0a208a12013-06-28 18:57:35 +000038 , fQuadIndexBuffer(NULL) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000039 fGeomPoolStateStack.push_back();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000040#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000041 GeometryPoolState& poolState = fGeomPoolStateStack.back();
42 poolState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
43 poolState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
44 poolState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
45 poolState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
46#endif
reed@google.comac10a2d2010-12-22 21:39:39 +000047}
48
bsalomon1d89ddc2014-08-19 14:20:58 -070049GrGpu::~GrGpu() {
bsalomon1d89ddc2014-08-19 14:20:58 -070050 SkSafeSetNull(fQuadIndexBuffer);
51 delete fVertexPool;
52 fVertexPool = NULL;
53 delete fIndexPool;
54 fIndexPool = NULL;
55}
56
robertphillipse3371302014-09-17 06:01:06 -070057void GrGpu::contextAbandoned() {}
reed@google.comac10a2d2010-12-22 21:39:39 +000058
bsalomon@google.comd302f142011-03-03 13:54:13 +000059////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +000060
bsalomonf2703d82014-10-28 14:33:06 -070061GrTexture* GrGpu::createTexture(const GrSurfaceDesc& desc,
bsalomon@google.coma7f84e12011-03-10 14:13:19 +000062 const void* srcData, size_t rowBytes) {
krajcevski9c0e6292014-06-02 07:38:14 -070063 if (!this->caps()->isConfigTexturable(desc.fConfig)) {
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000064 return NULL;
65 }
krajcevski9c0e6292014-06-02 07:38:14 -070066
bsalomonf2703d82014-10-28 14:33:06 -070067 if ((desc.fFlags & kRenderTarget_GrSurfaceFlag) &&
commit-bot@chromium.org6b7938f2013-10-15 14:18:16 +000068 !this->caps()->isConfigRenderable(desc.fConfig, desc.fSampleCnt > 0)) {
69 return NULL;
70 }
robertphillips@google.comd3eb3362012-10-31 13:56:35 +000071
krajcevski9c0e6292014-06-02 07:38:14 -070072 GrTexture *tex = NULL;
73 if (GrPixelConfigIsCompressed(desc.fConfig)) {
74 // We shouldn't be rendering into this
bsalomonf2703d82014-10-28 14:33:06 -070075 SkASSERT((desc.fFlags & kRenderTarget_GrSurfaceFlag) == 0);
krajcevski9c0e6292014-06-02 07:38:14 -070076
77 if (!this->caps()->npotTextureTileSupport() &&
tfarinaf9dae782014-06-06 06:35:28 -070078 (!SkIsPow2(desc.fWidth) || !SkIsPow2(desc.fHeight))) {
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000079 return NULL;
80 }
tfarinaf9dae782014-06-06 06:35:28 -070081
krajcevski9c0e6292014-06-02 07:38:14 -070082 this->handleDirtyContext();
83 tex = this->onCreateCompressedTexture(desc, srcData);
84 } else {
85 this->handleDirtyContext();
86 tex = this->onCreateTexture(desc, srcData, rowBytes);
bsalomon49f085d2014-09-05 13:34:00 -070087 if (tex &&
bsalomonf2703d82014-10-28 14:33:06 -070088 (kRenderTarget_GrSurfaceFlag & desc.fFlags) &&
89 !(kNoStencil_GrSurfaceFlag & desc.fFlags)) {
bsalomon49f085d2014-09-05 13:34:00 -070090 SkASSERT(tex->asRenderTarget());
krajcevski9c0e6292014-06-02 07:38:14 -070091 // TODO: defer this and attach dynamically
92 if (!this->attachStencilBufferToRenderTarget(tex->asRenderTarget())) {
93 tex->unref();
94 return NULL;
95 }
96 }
bsalomon@google.com81c3f8d2011-08-03 15:18:33 +000097 }
98 return tex;
99}
100
101bool GrGpu::attachStencilBufferToRenderTarget(GrRenderTarget* rt) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000102 SkASSERT(NULL == rt->getStencilBuffer());
rmistry@google.comd6176b02012-08-23 18:14:13 +0000103 GrStencilBuffer* sb =
robertphillips@google.com9fbcad02012-09-09 14:44:15 +0000104 this->getContext()->findStencilBuffer(rt->width(),
105 rt->height(),
106 rt->numSamples());
bsalomon49f085d2014-09-05 13:34:00 -0700107 if (sb) {
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000108 rt->setStencilBuffer(sb);
109 bool attached = this->attachStencilBufferToRenderTarget(sb, rt);
110 if (!attached) {
111 rt->setStencilBuffer(NULL);
112 }
113 return attached;
114 }
bsalomon@google.com99621082011-11-15 16:47:16 +0000115 if (this->createStencilBufferForRenderTarget(rt,
116 rt->width(), rt->height())) {
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000117 // Right now we're clearing the stencil buffer here after it is
118 // attached to an RT for the first time. When we start matching
119 // stencil buffers with smaller color targets this will no longer
120 // be correct because it won't be guaranteed to clear the entire
121 // sb.
122 // We used to clear down in the GL subclass using a special purpose
123 // FBO. But iOS doesn't allow a stencil-only FBO. It reports unsupported
124 // FBO status.
bsalomonb0bd4f62014-09-03 07:19:50 -0700125 this->clearStencil(rt);
bsalomon@google.com558a75b2011-08-08 17:01:14 +0000126 return true;
127 } else {
128 return false;
bsalomon@google.comedc177d2011-08-05 15:46:40 +0000129 }
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000130}
131
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000132GrTexture* GrGpu::wrapBackendTexture(const GrBackendTextureDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000133 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000134 GrTexture* tex = this->onWrapBackendTexture(desc);
bsalomon@google.coma14dd6d2012-01-03 21:08:12 +0000135 if (NULL == tex) {
136 return NULL;
137 }
bsalomon@google.come269f212011-11-07 13:29:52 +0000138 // TODO: defer this and attach dynamically
139 GrRenderTarget* tgt = tex->asRenderTarget();
bsalomon49f085d2014-09-05 13:34:00 -0700140 if (tgt &&
bsalomon@google.come269f212011-11-07 13:29:52 +0000141 !this->attachStencilBufferToRenderTarget(tgt)) {
142 tex->unref();
143 return NULL;
144 } else {
145 return tex;
146 }
147}
148
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000149GrRenderTarget* GrGpu::wrapBackendRenderTarget(const GrBackendRenderTargetDesc& desc) {
bsalomon@google.come269f212011-11-07 13:29:52 +0000150 this->handleDirtyContext();
bsalomon@google.com16e3dde2012-10-25 18:43:28 +0000151 return this->onWrapBackendRenderTarget(desc);
bsalomon@google.come269f212011-11-07 13:29:52 +0000152}
153
robertphillips@google.comadacc702013-10-14 21:53:24 +0000154GrVertexBuffer* GrGpu::createVertexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000155 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000156 return this->onCreateVertexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000157}
158
robertphillips@google.comadacc702013-10-14 21:53:24 +0000159GrIndexBuffer* GrGpu::createIndexBuffer(size_t size, bool dynamic) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000160 this->handleDirtyContext();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000161 return this->onCreateIndexBuffer(size, dynamic);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000162}
163
joshualitt5ead6da2014-10-22 16:00:29 -0700164GrIndexBuffer* GrGpu::createInstancedIndexBuffer(const uint16_t* pattern,
165 int patternSize,
166 int reps,
167 int vertCount,
168 bool isDynamic) {
169 size_t bufferSize = patternSize * reps * sizeof(uint16_t);
170 GrGpu* me = const_cast<GrGpu*>(this);
171 GrIndexBuffer* buffer = me->createIndexBuffer(bufferSize, isDynamic);
172 if (buffer) {
173 uint16_t* data = (uint16_t*) buffer->map();
174 bool useTempData = (NULL == data);
175 if (useTempData) {
176 data = SkNEW_ARRAY(uint16_t, reps * patternSize);
177 }
178 for (int i = 0; i < reps; ++i) {
179 int baseIdx = i * patternSize;
180 uint16_t baseVert = (uint16_t)(i * vertCount);
181 for (int j = 0; j < patternSize; ++j) {
182 data[baseIdx+j] = baseVert + pattern[j];
183 }
184 }
185 if (useTempData) {
186 if (!buffer->updateData(data, bufferSize)) {
187 SkFAIL("Can't get indices into buffer!");
188 }
189 SkDELETE_ARRAY(data);
190 } else {
191 buffer->unmap();
192 }
193 }
194 return buffer;
195}
196
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000197void GrGpu::clear(const SkIRect* rect,
rmistry@google.comd6176b02012-08-23 18:14:13 +0000198 GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000199 bool canIgnoreRect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000200 GrRenderTarget* renderTarget) {
bsalomon89c62982014-11-03 12:08:42 -0800201 SkASSERT(renderTarget);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000202 this->handleDirtyContext();
bsalomonb0bd4f62014-09-03 07:19:50 -0700203 this->onClear(renderTarget, rect, color, canIgnoreRect);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000204}
205
joshualitt6db519c2014-10-29 08:48:18 -0700206void GrGpu::clearStencilClip(const SkIRect& rect,
207 bool insideClip,
208 GrRenderTarget* renderTarget) {
209 if (NULL == renderTarget) {
210 renderTarget = this->getDrawState().getRenderTarget();
211 }
212 if (NULL == renderTarget) {
213 SkASSERT(0);
214 return;
215 }
216 this->handleDirtyContext();
217 this->onClearStencilClip(renderTarget, rect, insideClip);
218}
219
bsalomon@google.com669fdc42011-04-05 17:08:27 +0000220bool GrGpu::readPixels(GrRenderTarget* target,
221 int left, int top, int width, int height,
bsalomon@google.comc6980972011-11-02 19:57:21 +0000222 GrPixelConfig config, void* buffer,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000223 size_t rowBytes) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000224 this->handleDirtyContext();
bsalomon@google.comc6980972011-11-02 19:57:21 +0000225 return this->onReadPixels(target, left, top, width, height,
senorblanco@chromium.org3cb406b2013-02-05 19:50:46 +0000226 config, buffer, rowBytes);
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000227}
228
bsalomon@google.com9c680582013-02-06 18:17:50 +0000229bool GrGpu::writeTexturePixels(GrTexture* texture,
bsalomon@google.com6f379512011-11-16 20:36:03 +0000230 int left, int top, int width, int height,
231 GrPixelConfig config, const void* buffer,
232 size_t rowBytes) {
bsalomon@google.com6f379512011-11-16 20:36:03 +0000233 this->handleDirtyContext();
bsalomon@google.com9c680582013-02-06 18:17:50 +0000234 return this->onWriteTexturePixels(texture, left, top, width, height,
235 config, buffer, rowBytes);
bsalomon@google.com6f379512011-11-16 20:36:03 +0000236}
237
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000238void GrGpu::resolveRenderTarget(GrRenderTarget* target) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000239 SkASSERT(target);
bsalomon@google.com75f9f252012-01-31 13:35:56 +0000240 this->handleDirtyContext();
241 this->onResolveRenderTarget(target);
242}
243
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000244////////////////////////////////////////////////////////////////////////////////
245
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000246static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
reed@google.comac10a2d2010-12-22 21:39:39 +0000247
reed@google.com8195f672011-01-12 18:14:28 +0000248GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
reed@google.comac10a2d2010-12-22 21:39:39 +0000249
joshualitt5ead6da2014-10-22 16:00:29 -0700250static const uint16_t gQuadIndexPattern[] = {
251 0, 1, 2, 0, 2, 3
252};
reed@google.comac10a2d2010-12-22 21:39:39 +0000253
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000254const GrIndexBuffer* GrGpu::getQuadIndexBuffer() const {
bsalomonc8dc1f72014-08-21 13:02:13 -0700255 if (NULL == fQuadIndexBuffer || fQuadIndexBuffer->wasDestroyed()) {
256 SkSafeUnref(fQuadIndexBuffer);
reed@google.comac10a2d2010-12-22 21:39:39 +0000257 GrGpu* me = const_cast<GrGpu*>(this);
joshualitt5ead6da2014-10-22 16:00:29 -0700258 fQuadIndexBuffer = me->createInstancedIndexBuffer(gQuadIndexPattern,
259 6,
260 MAX_QUADS,
261 4);
reed@google.comac10a2d2010-12-22 21:39:39 +0000262 }
263
264 return fQuadIndexBuffer;
265}
266
bsalomon@google.comd302f142011-03-03 13:54:13 +0000267////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000268
joshualitta58fe352014-10-27 08:39:00 -0700269bool GrGpu::setupClipAndFlushState(DrawType type,
270 const GrDeviceCoordTexture* dstCopy,
joshualitt77b13072014-10-27 14:51:01 -0700271 const SkRect* devBounds,
joshualitt92e496f2014-10-31 13:56:50 -0700272 GrDrawState::AutoRestoreEffects* are,
273 GrDrawState::AutoRestoreStencil* ars) {
joshualitt6db519c2014-10-29 08:48:18 -0700274 GrClipMaskManager::ScissorState scissorState;
joshualitt77b13072014-10-27 14:51:01 -0700275 if (!fClipMaskManager.setupClipping(this->getClip(),
276 devBounds,
277 are,
joshualitt92e496f2014-10-31 13:56:50 -0700278 ars,
joshualitt77b13072014-10-27 14:51:01 -0700279 &scissorState)) {
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000280 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000281 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000282
joshualitt77b13072014-10-27 14:51:01 -0700283 if (!this->flushGraphicsState(type, scissorState, dstCopy)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000284 return false;
285 }
robertphillips@google.com730ebe52012-04-16 16:33:13 +0000286
reed@google.comac10a2d2010-12-22 21:39:39 +0000287 return true;
288}
289
bsalomon@google.comd302f142011-03-03 13:54:13 +0000290////////////////////////////////////////////////////////////////////////////////
reed@google.comac10a2d2010-12-22 21:39:39 +0000291
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000292void GrGpu::geometrySourceWillPush() {
293 const GeometrySrcState& geoSrc = this->getGeomSrc();
294 if (kArray_GeometrySrcType == geoSrc.fVertexSrc ||
295 kReserved_GeometrySrcType == geoSrc.fVertexSrc) {
296 this->finalizeReservedVertices();
297 }
298 if (kArray_GeometrySrcType == geoSrc.fIndexSrc ||
299 kReserved_GeometrySrcType == geoSrc.fIndexSrc) {
300 this->finalizeReservedIndices();
301 }
302 GeometryPoolState& newState = fGeomPoolStateStack.push_back();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000303#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000304 newState.fPoolVertexBuffer = (GrVertexBuffer*)DEBUG_INVAL_BUFFER;
305 newState.fPoolStartVertex = DEBUG_INVAL_START_IDX;
306 newState.fPoolIndexBuffer = (GrIndexBuffer*)DEBUG_INVAL_BUFFER;
307 newState.fPoolStartIndex = DEBUG_INVAL_START_IDX;
humper@google.com0e515772013-01-07 19:54:40 +0000308#else
309 (void) newState; // silence compiler warning
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000310#endif
311}
312
313void GrGpu::geometrySourceWillPop(const GeometrySrcState& restoredState) {
314 // if popping last entry then pops are unbalanced with pushes
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000315 SkASSERT(fGeomPoolStateStack.count() > 1);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000316 fGeomPoolStateStack.pop_back();
317}
318
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000319void GrGpu::onDraw(const DrawInfo& info) {
bsalomon@google.coma7f84e12011-03-10 14:13:19 +0000320 this->handleDirtyContext();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000321 GrDrawState::AutoRestoreEffects are;
joshualitt92e496f2014-10-31 13:56:50 -0700322 GrDrawState::AutoRestoreStencil ars;
bsalomon@google.com26e18b52013-03-29 19:22:36 +0000323 if (!this->setupClipAndFlushState(PrimTypeToDrawType(info.primitiveType()),
joshualitt77b13072014-10-27 14:51:01 -0700324 info.getDstCopy(),
325 info.getDevBounds(),
joshualitt92e496f2014-10-31 13:56:50 -0700326 &are,
327 &ars)) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000328 return;
329 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000330 this->onGpuDraw(info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000331}
332
joshualitt92e496f2014-10-31 13:56:50 -0700333
334// TODO hack
335static const GrStencilSettings& winding_path_stencil_settings() {
336 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
337 kIncClamp_StencilOp,
338 kIncClamp_StencilOp,
339 kAlwaysIfInClip_StencilFunc,
340 0xFFFF, 0xFFFF, 0xFFFF);
341 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
342}
343
344static const GrStencilSettings& even_odd_path_stencil_settings() {
345 GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings,
346 kInvert_StencilOp,
347 kInvert_StencilOp,
348 kAlwaysIfInClip_StencilFunc,
349 0xFFFF, 0xFFFF, 0xFFFF);
350 return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings);
351}
352
353static void get_path_stencil_settings_for_filltype(GrPathRendering::FillType fill,
354 GrStencilSettings* outStencilSettings) {
355
356 switch (fill) {
357 default:
358 SkFAIL("Unexpected path fill.");
359 case GrPathRendering::kWinding_FillType:
360 *outStencilSettings = winding_path_stencil_settings();
361 break;
362 case GrPathRendering::kEvenOdd_FillType:
363 *outStencilSettings = even_odd_path_stencil_settings();
364 break;
365 }
366}
367
368void GrGpu::onStencilPath(const GrPath* path, GrPathRendering::FillType fill) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000369 this->handleDirtyContext();
370
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000371 GrDrawState::AutoRestoreEffects are;
joshualitt92e496f2014-10-31 13:56:50 -0700372 GrDrawState::AutoRestoreStencil ars;
373 if (!this->setupClipAndFlushState(kStencilPath_DrawType, NULL, NULL, &are, &ars)) {
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000374 return;
375 }
376
joshualitt92e496f2014-10-31 13:56:50 -0700377 GrStencilSettings stencilSettings;
378 get_path_stencil_settings_for_filltype(fill, &stencilSettings);
379 fClipMaskManager.adjustPathStencilParams(&stencilSettings);
380
381 this->pathRendering()->stencilPath(path, stencilSettings);
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000382}
383
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000384
joshualitt92e496f2014-10-31 13:56:50 -0700385void GrGpu::onDrawPath(const GrPath* path, GrPathRendering::FillType fill,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000386 const GrDeviceCoordTexture* dstCopy) {
387 this->handleDirtyContext();
388
389 drawState()->setDefaultVertexAttribs();
390
391 GrDrawState::AutoRestoreEffects are;
joshualitt92e496f2014-10-31 13:56:50 -0700392 GrDrawState::AutoRestoreStencil ars;
393 if (!this->setupClipAndFlushState(kDrawPath_DrawType, dstCopy, NULL, &are, &ars)) {
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000394 return;
395 }
396
joshualitt92e496f2014-10-31 13:56:50 -0700397 GrStencilSettings stencilSettings;
398 get_path_stencil_settings_for_filltype(fill, &stencilSettings);
399 fClipMaskManager.adjustPathStencilParams(&stencilSettings);
400
401 this->pathRendering()->drawPath(path, stencilSettings);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000402}
403
cdaltonb85a0aa2014-07-21 15:32:44 -0700404void GrGpu::onDrawPaths(const GrPathRange* pathRange,
405 const uint32_t indices[], int count,
406 const float transforms[], PathTransformType transformsType,
joshualitt92e496f2014-10-31 13:56:50 -0700407 GrPathRendering::FillType fill, const GrDeviceCoordTexture* dstCopy) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000408 this->handleDirtyContext();
409
410 drawState()->setDefaultVertexAttribs();
411
412 GrDrawState::AutoRestoreEffects are;
joshualitt92e496f2014-10-31 13:56:50 -0700413 GrDrawState::AutoRestoreStencil ars;
414 if (!this->setupClipAndFlushState(kDrawPaths_DrawType, dstCopy, NULL, &are, &ars)) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000415 return;
416 }
417
joshualitt92e496f2014-10-31 13:56:50 -0700418 GrStencilSettings stencilSettings;
419 get_path_stencil_settings_for_filltype(fill, &stencilSettings);
420 fClipMaskManager.adjustPathStencilParams(&stencilSettings);
421
cdalton855d83f2014-09-18 13:51:53 -0700422 pathRange->willDrawPaths(indices, count);
joshualitt92e496f2014-10-31 13:56:50 -0700423 this->pathRendering()->drawPaths(pathRange, indices, count, transforms, transformsType,
424 stencilSettings);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000425}
426
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000427void GrGpu::finalizeReservedVertices() {
bsalomon49f085d2014-09-05 13:34:00 -0700428 SkASSERT(fVertexPool);
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000429 fVertexPool->unmap();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000430}
431
432void GrGpu::finalizeReservedIndices() {
bsalomon49f085d2014-09-05 13:34:00 -0700433 SkASSERT(fIndexPool);
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000434 fIndexPool->unmap();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000435}
436
437void GrGpu::prepareVertexPool() {
438 if (NULL == fVertexPool) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000439 SkASSERT(0 == fVertexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000440 fVertexPool = SkNEW_ARGS(GrVertexBufferAllocPool, (this, true,
bsalomon@google.comd302f142011-03-03 13:54:13 +0000441 VERTEX_POOL_VB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000442 VERTEX_POOL_VB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000443 fVertexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000444 } else if (!fVertexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000445 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000446 fVertexPool->reset();
447 }
448}
449
450void GrGpu::prepareIndexPool() {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000451 if (NULL == fIndexPool) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000452 SkASSERT(0 == fIndexPoolUseCnt);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000453 fIndexPool = SkNEW_ARGS(GrIndexBufferAllocPool, (this, true,
bsalomon@google.com25fd36c2011-07-06 17:41:08 +0000454 INDEX_POOL_IB_SIZE,
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000455 INDEX_POOL_IB_COUNT));
bsalomon@google.com11f0b512011-03-29 20:52:23 +0000456 fIndexPool->releaseGpuRef();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000457 } else if (!fIndexPoolUseCnt) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000458 // the client doesn't have valid data in the pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000459 fIndexPool->reset();
460 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000461}
462
jvanverth@google.coma6338982013-01-31 21:34:25 +0000463bool GrGpu::onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000464 int vertexCount,
465 void** vertices) {
466 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000467
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000468 SkASSERT(vertexCount > 0);
bsalomon49f085d2014-09-05 13:34:00 -0700469 SkASSERT(vertices);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000470
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000471 this->prepareVertexPool();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000472
jvanverth@google.coma6338982013-01-31 21:34:25 +0000473 *vertices = fVertexPool->makeSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000474 vertexCount,
475 &geomPoolState.fPoolVertexBuffer,
476 &geomPoolState.fPoolStartVertex);
477 if (NULL == *vertices) {
478 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000479 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000480 ++fVertexPoolUseCnt;
reed@google.comac10a2d2010-12-22 21:39:39 +0000481 return true;
482}
483
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000484bool GrGpu::onReserveIndexSpace(int indexCount, void** indices) {
485 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000486
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000487 SkASSERT(indexCount > 0);
bsalomon49f085d2014-09-05 13:34:00 -0700488 SkASSERT(indices);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000489
490 this->prepareIndexPool();
491
492 *indices = fIndexPool->makeSpace(indexCount,
493 &geomPoolState.fPoolIndexBuffer,
494 &geomPoolState.fPoolStartIndex);
495 if (NULL == *indices) {
496 return false;
497 }
498 ++fIndexPoolUseCnt;
499 return true;
500}
501
502void GrGpu::releaseReservedVertexSpace() {
503 const GeometrySrcState& geoSrc = this->getGeomSrc();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000504 SkASSERT(kReserved_GeometrySrcType == geoSrc.fVertexSrc);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000505 size_t bytes = geoSrc.fVertexCount * geoSrc.fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000506 fVertexPool->putBack(bytes);
507 --fVertexPoolUseCnt;
508}
509
510void GrGpu::releaseReservedIndexSpace() {
511 const GeometrySrcState& geoSrc = this->getGeomSrc();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000512 SkASSERT(kReserved_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000513 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
514 fIndexPool->putBack(bytes);
515 --fIndexPoolUseCnt;
516}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000517
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000518void GrGpu::onSetVertexSourceToArray(const void* vertexArray, int vertexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000519 this->prepareVertexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000520 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000521#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000522 bool success =
523#endif
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000524 fVertexPool->appendVertices(this->getVertexSize(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000525 vertexCount,
526 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000527 &geomPoolState.fPoolVertexBuffer,
528 &geomPoolState.fPoolStartVertex);
529 ++fVertexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000530 GR_DEBUGASSERT(success);
531}
532
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000533void GrGpu::onSetIndexSourceToArray(const void* indexArray, int indexCount) {
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000534 this->prepareIndexPool();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000535 GeometryPoolState& geomPoolState = fGeomPoolStateStack.back();
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000536#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000537 bool success =
538#endif
539 fIndexPool->appendIndices(indexCount,
540 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000541 &geomPoolState.fPoolIndexBuffer,
542 &geomPoolState.fPoolStartIndex);
543 ++fIndexPoolUseCnt;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000544 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000545}
546
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000547void GrGpu::releaseVertexArray() {
548 // if vertex source was array, we stowed data in the pool
549 const GeometrySrcState& geoSrc = this->getGeomSrc();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000550 SkASSERT(kArray_GeometrySrcType == geoSrc.fVertexSrc);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000551 size_t bytes = geoSrc.fVertexCount * geoSrc.fVertexSize;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000552 fVertexPool->putBack(bytes);
553 --fVertexPoolUseCnt;
554}
555
556void GrGpu::releaseIndexArray() {
557 // if index source was array, we stowed data in the pool
558 const GeometrySrcState& geoSrc = this->getGeomSrc();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000559 SkASSERT(kArray_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000560 size_t bytes = geoSrc.fIndexCount * sizeof(uint16_t);
561 fIndexPool->putBack(bytes);
562 --fIndexPoolUseCnt;
563}