blob: 87ecc8579f95b8aeb62005c3afede7ab6265110f [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 2011 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
9
10#include "GrInOrderDrawBuffer.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000011#include "GrBufferAllocPool.h"
12#include "GrGpu.h"
13#include "GrIndexBuffer.h"
14#include "GrPath.h"
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000015#include "GrRenderTarget.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016#include "GrTexture.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000017#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018
bsalomon@google.com471d4712011-08-23 15:45:25 +000019GrInOrderDrawBuffer::GrInOrderDrawBuffer(const GrGpu* gpu,
20 GrVertexBufferAllocPool* vertexPool,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000021 GrIndexBufferAllocPool* indexPool)
bsalomon@google.com97805382012-03-13 14:32:07 +000022 : fAutoFlushTarget(NULL)
23 , fClipSet(true)
robertphillips@google.com69705572012-03-21 19:46:50 +000024 , fVertexPool(*vertexPool)
25 , fIndexPool(*indexPool)
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000026 , fLastRectVertexLayout(0)
27 , fQuadIndexBuffer(NULL)
28 , fMaxQuads(0)
robertphillips@google.comc82a8b72012-06-21 20:15:48 +000029 , fFlushing(false) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +000030
31 fCaps = gpu->getCaps();
32
bsalomon@google.com1c13c962011-02-14 16:51:21 +000033 GrAssert(NULL != vertexPool);
34 GrAssert(NULL != indexPool);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000035
36 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
37 poolState.fUsedPoolVertexBytes = 0;
38 poolState.fUsedPoolIndexBytes = 0;
39#if GR_DEBUG
40 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
41 poolState.fPoolStartVertex = ~0;
42 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
43 poolState.fPoolStartIndex = ~0;
44#endif
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000045 this->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000046}
47
48GrInOrderDrawBuffer::~GrInOrderDrawBuffer() {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000049 this->reset();
bsalomon@google.com4a018bb2011-10-28 19:50:21 +000050 // This must be called by before the GrDrawTarget destructor
51 this->releaseGeometry();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000052 GrSafeUnref(fQuadIndexBuffer);
bsalomon@google.com97805382012-03-13 14:32:07 +000053 GrSafeUnref(fAutoFlushTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +000054}
55
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000056void GrInOrderDrawBuffer::setQuadIndexBuffer(const GrIndexBuffer* indexBuffer) {
57 bool newIdxBuffer = fQuadIndexBuffer != indexBuffer;
58 if (newIdxBuffer) {
59 GrSafeUnref(fQuadIndexBuffer);
60 fQuadIndexBuffer = indexBuffer;
61 GrSafeRef(fQuadIndexBuffer);
62 fCurrQuad = 0;
63 fMaxQuads = (NULL == indexBuffer) ? 0 : indexBuffer->maxQuads();
64 } else {
bsalomon@google.comd302f142011-03-03 13:54:13 +000065 GrAssert((NULL == indexBuffer && 0 == fMaxQuads) ||
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000066 (indexBuffer->maxQuads() == fMaxQuads));
67 }
68}
69
bsalomon@google.com934c5702012-03-20 21:17:58 +000070////////////////////////////////////////////////////////////////////////////////
71
72void GrInOrderDrawBuffer::resetDrawTracking() {
73 fCurrQuad = 0;
74 fInstancedDrawTracker.reset();
75}
76
bsalomon@google.comd302f142011-03-03 13:54:13 +000077void GrInOrderDrawBuffer::drawRect(const GrRect& rect,
bsalomon@google.comb9086a02012-11-01 18:02:54 +000078 const SkMatrix* matrix,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000079 const GrRect* srcRects[],
bsalomon@google.comb9086a02012-11-01 18:02:54 +000080 const SkMatrix* srcMatrices[]) {
bsalomon@google.comd302f142011-03-03 13:54:13 +000081
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000082 GrAssert(!(NULL == fQuadIndexBuffer && fCurrQuad));
83 GrAssert(!(fDraws.empty() && fCurrQuad));
84 GrAssert(!(0 != fMaxQuads && NULL == fQuadIndexBuffer));
85
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000086 GrDrawState* drawState = this->drawState();
87
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000088 // if we have a quad IB then either append to the previous run of
89 // rects or start a new run
90 if (fMaxQuads) {
bsalomon@google.comd302f142011-03-03 13:54:13 +000091
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000092 bool appendToPreviousDraw = false;
bsalomon@google.come3d32162012-07-20 13:37:06 +000093 GrVertexLayout layout = GetRectVertexLayout(srcRects);
robertphillips@google.com8b129aa2012-10-05 15:37:00 +000094
robertphillips@google.comf04c40e2012-10-09 17:40:32 +000095 // Batching across colors means we move the draw color into the
96 // rect's vertex colors to allow greater batching (a lot of rects
97 // in a row differing only in color is a common occurence in tables).
98 bool batchAcrossColors = true;
99 if (!this->getCaps().dualSourceBlendingSupport()) {
100 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
101 if (this->getDrawState().isStageEnabled(s)) {
skia.committer@gmail.com439cb512012-10-10 02:01:30 +0000102 // We disable batching across colors when there is a texture
robertphillips@google.comf04c40e2012-10-09 17:40:32 +0000103 // present because (by pushing the the color to the vertices)
104 // Ganesh loses track of the rect's opacity. This, in turn, can
105 // cause some of the blending optimizations to be disabled. This
106 // becomes a huge problem on some of the smaller devices where
107 // shader derivatives and dual source blending aren't supported.
108 // In those cases paths are often drawn to a texture and then
109 // drawn as a texture (using this method). Because dual source
110 // blending is disabled (and the blend optimizations are short
111 // circuited) some of the more esoteric blend modes can no longer
112 // be supported.
113 // TODO: add tracking of batchAcrossColors's opacity
114 batchAcrossColors = false;
115 break;
116 }
117 }
118 }
119
120 if (batchAcrossColors) {
121 layout |= kColor_VertexLayoutBit;
122 }
robertphillips@google.com8b129aa2012-10-05 15:37:00 +0000123
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000124 AutoReleaseGeometry geo(this, layout, 4, 0);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000125 if (!geo.succeeded()) {
126 GrPrintf("Failed to get space for vertices!\n");
127 return;
128 }
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000129 SkMatrix combinedMatrix = drawState->getViewMatrix();
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000130 // We go to device space so that matrix changes allow us to concat
131 // rect draws. When the caller has provided explicit source rects
bsalomon@google.com08283af2012-10-26 13:01:20 +0000132 // then we don't want to modify the stages' matrices. Otherwise
133 // we have to account for the view matrix change in the stage
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000134 // matrices.
bsalomon@google.come3d32162012-07-20 13:37:06 +0000135 uint32_t explicitCoordMask = 0;
136 if (srcRects) {
137 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
138 if (srcRects[s]) {
139 explicitCoordMask |= (1 << s);
140 }
141 }
142 }
bsalomon@google.com5b3e8902012-10-05 20:13:28 +0000143 GrDrawState::AutoDeviceCoordDraw adcd(this->drawState(), explicitCoordMask);
bsalomon@google.come3d32162012-07-20 13:37:06 +0000144 if (!adcd.succeeded()) {
145 return;
146 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000147 if (NULL != matrix) {
148 combinedMatrix.preConcat(*matrix);
149 }
150
skia.committer@gmail.coma18ed032012-10-06 02:05:26 +0000151 SetRectVertices(rect, &combinedMatrix, srcRects, srcMatrices,
robertphillips@google.com8b129aa2012-10-05 15:37:00 +0000152 this->getDrawState().getColor(), layout, geo.vertices());
153
154 // Now that the paint's color is stored in the vertices set it to
155 // white so that the following code can batch all the rects regardless
156 // of paint color
skia.committer@gmail.com439cb512012-10-10 02:01:30 +0000157 GrDrawState::AutoColorRestore acr(this->drawState(),
robertphillips@google.comf04c40e2012-10-09 17:40:32 +0000158 batchAcrossColors ? SK_ColorWHITE
159 : this->getDrawState().getColor());
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000160
161 // we don't want to miss an opportunity to batch rects together
162 // simply because the clip has changed if the clip doesn't affect
163 // the rect.
164 bool disabledClip = false;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000165
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000166 if (drawState->isClipState()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000167
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000168 GrRect devClipRect;
169 bool isIntersectionOfRects = false;
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000170
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000171 fClip->fClipStack->getConservativeBounds(-fClip->fOrigin.fX,
172 -fClip->fOrigin.fY,
173 drawState->getRenderTarget()->width(),
174 drawState->getRenderTarget()->height(),
175 &devClipRect,
176 &isIntersectionOfRects);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000177
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000178 if (isIntersectionOfRects) {
179 // If the clip rect touches the edge of the viewport, extended it
180 // out (close) to infinity to avoid bogus intersections.
181 // We might consider a more exact clip to viewport if this
182 // conservative test fails.
183 const GrRenderTarget* target = drawState->getRenderTarget();
184 if (0 >= devClipRect.fLeft) {
bsalomon@google.com81712882012-11-01 17:12:34 +0000185 devClipRect.fLeft = SK_ScalarMin;
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000186 }
187 if (target->width() <= devClipRect.fRight) {
bsalomon@google.com81712882012-11-01 17:12:34 +0000188 devClipRect.fRight = SK_ScalarMax;
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000189 }
190 if (0 >= devClipRect.top()) {
bsalomon@google.com81712882012-11-01 17:12:34 +0000191 devClipRect.fTop = SK_ScalarMin;
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000192 }
193 if (target->height() <= devClipRect.fBottom) {
bsalomon@google.com81712882012-11-01 17:12:34 +0000194 devClipRect.fBottom = SK_ScalarMax;
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000195 }
196 int stride = VertexSize(layout);
197 bool insideClip = true;
198 for (int v = 0; v < 4; ++v) {
199 const GrPoint& p = *GetVertexPoint(geo.vertices(), v, stride);
200 if (!devClipRect.contains(p)) {
201 insideClip = false;
202 break;
203 }
204 }
205 if (insideClip) {
206 drawState->disableState(GrDrawState::kClip_StateBit);
207 disabledClip = true;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000208 }
209 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000210 }
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000211
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000212 if (!this->needsNewClip() &&
213 !this->needsNewState() &&
214 fCurrQuad > 0 &&
215 fCurrQuad < fMaxQuads &&
216 layout == fLastRectVertexLayout) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000217
218 int vsize = VertexSize(layout);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000219
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000220 Draw& lastDraw = fDraws.back();
221
222 GrAssert(lastDraw.fIndexBuffer == fQuadIndexBuffer);
bsalomon@google.com47059542012-06-06 20:51:20 +0000223 GrAssert(kTriangles_GrPrimitiveType == lastDraw.fPrimitiveType);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000224 GrAssert(0 == lastDraw.fVertexCount % 4);
225 GrAssert(0 == lastDraw.fIndexCount % 6);
226 GrAssert(0 == lastDraw.fStartIndex);
227
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000228 GeometryPoolState& poolState = fGeoPoolStateStack.back();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000229
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000230 appendToPreviousDraw =
robertphillips@google.com837ec432012-10-04 17:57:05 +0000231 kDraw_Cmd == fCmds.back() &&
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000232 lastDraw.fVertexBuffer == poolState.fPoolVertexBuffer &&
233 (fCurrQuad * 4 + lastDraw.fStartVertex) == poolState.fPoolStartVertex;
234
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000235 if (appendToPreviousDraw) {
236 lastDraw.fVertexCount += 4;
237 lastDraw.fIndexCount += 6;
238 fCurrQuad += 1;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000239 // we reserved above, so we should be the first
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000240 // use of this vertex reservation.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000241 GrAssert(0 == poolState.fUsedPoolVertexBytes);
242 poolState.fUsedPoolVertexBytes = 4 * vsize;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000243 }
244 }
245 if (!appendToPreviousDraw) {
246 this->setIndexSourceToBuffer(fQuadIndexBuffer);
bsalomon@google.com47059542012-06-06 20:51:20 +0000247 this->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 4, 6);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000248 fCurrQuad = 1;
249 fLastRectVertexLayout = layout;
250 }
251 if (disabledClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000252 drawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000253 }
bsalomon@google.com934c5702012-03-20 21:17:58 +0000254 fInstancedDrawTracker.reset();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000255 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000256 INHERITED::drawRect(rect, matrix, srcRects, srcMatrices);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000257 }
258}
259
bsalomon@google.com934c5702012-03-20 21:17:58 +0000260void GrInOrderDrawBuffer::drawIndexedInstances(GrPrimitiveType type,
261 int instanceCount,
262 int verticesPerInstance,
263 int indicesPerInstance) {
264 if (!verticesPerInstance || !indicesPerInstance) {
265 return;
266 }
267
268 const GeometrySrcState& geomSrc = this->getGeomSrc();
269
270 // we only attempt to concat the case when reserved verts are used with
271 // an index buffer.
272 if (kReserved_GeometrySrcType == geomSrc.fVertexSrc &&
273 kBuffer_GeometrySrcType == geomSrc.fIndexSrc) {
274
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000275 if (this->needsNewClip()) {
276 this->recordClip();
277 }
278 if (this->needsNewState()) {
279 this->recordState();
280 }
281
bsalomon@google.com934c5702012-03-20 21:17:58 +0000282 Draw* draw = NULL;
283 // if the last draw used the same indices/vertices per shape then we
284 // may be able to append to it.
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000285 if (kDraw_Cmd == fCmds.back() &&
286 verticesPerInstance == fInstancedDrawTracker.fVerticesPerInstance &&
bsalomon@google.com934c5702012-03-20 21:17:58 +0000287 indicesPerInstance == fInstancedDrawTracker.fIndicesPerInstance) {
288 GrAssert(fDraws.count());
289 draw = &fDraws.back();
290 }
291
bsalomon@google.com934c5702012-03-20 21:17:58 +0000292 GeometryPoolState& poolState = fGeoPoolStateStack.back();
293 const GrVertexBuffer* vertexBuffer = poolState.fPoolVertexBuffer;
294
295 // Check whether the draw is compatible with this draw in order to
296 // append
297 if (NULL == draw ||
bsalomon@google.com934c5702012-03-20 21:17:58 +0000298 draw->fIndexBuffer != geomSrc.fIndexBuffer ||
299 draw->fPrimitiveType != type ||
300 draw->fVertexBuffer != vertexBuffer) {
301
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000302 draw = this->recordDraw();
robertphillips@google.com6d067302012-12-18 21:47:47 +0000303 draw->fPrimitiveType = type;
304 draw->fStartVertex = poolState.fPoolStartVertex;
305 draw->fStartIndex = 0;
306 draw->fVertexCount = 0;
307 draw->fIndexCount = 0;
308 draw->fVertexLayout = geomSrc.fVertexLayout;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000309 draw->fVertexBuffer = vertexBuffer;
310 vertexBuffer->ref();
robertphillips@google.com6d067302012-12-18 21:47:47 +0000311 draw->fIndexBuffer = geomSrc.fIndexBuffer;
312 geomSrc.fIndexBuffer->ref();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000313 } else {
314 GrAssert(!(draw->fIndexCount % indicesPerInstance));
315 GrAssert(!(draw->fVertexCount % verticesPerInstance));
316 GrAssert(poolState.fPoolStartVertex == draw->fStartVertex +
317 draw->fVertexCount);
318 }
319
320 // how many instances can be in a single draw
321 int maxInstancesPerDraw = this->indexCountInCurrentSource() /
322 indicesPerInstance;
323 if (!maxInstancesPerDraw) {
324 return;
325 }
326 // how many instances should be concat'ed onto draw
327 int instancesToConcat = maxInstancesPerDraw - draw->fVertexCount /
328 verticesPerInstance;
329 if (maxInstancesPerDraw > instanceCount) {
330 maxInstancesPerDraw = instanceCount;
331 if (instancesToConcat > instanceCount) {
332 instancesToConcat = instanceCount;
333 }
334 }
335
336 // update the amount of reserved data actually referenced in draws
337 size_t vertexBytes = instanceCount * verticesPerInstance *
338 VertexSize(draw->fVertexLayout);
339 poolState.fUsedPoolVertexBytes =
340 GrMax(poolState.fUsedPoolVertexBytes, vertexBytes);
341
342 while (instanceCount) {
343 if (!instancesToConcat) {
344 int startVertex = draw->fStartVertex + draw->fVertexCount;
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000345 draw = this->recordDraw();
robertphillips@google.com6d067302012-12-18 21:47:47 +0000346 draw->fPrimitiveType = type;
347 draw->fStartVertex = startVertex;
348 draw->fStartIndex = 0;
349 draw->fVertexCount = 0;
350 draw->fIndexCount = 0;
351 draw->fVertexLayout = geomSrc.fVertexLayout;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000352 draw->fVertexBuffer = vertexBuffer;
353 vertexBuffer->ref();
robertphillips@google.com6d067302012-12-18 21:47:47 +0000354 draw->fIndexBuffer = geomSrc.fIndexBuffer;
355 geomSrc.fIndexBuffer->ref();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000356 instancesToConcat = maxInstancesPerDraw;
357 }
358 draw->fVertexCount += instancesToConcat * verticesPerInstance;
359 draw->fIndexCount += instancesToConcat * indicesPerInstance;
360 instanceCount -= instancesToConcat;
361 instancesToConcat = 0;
362 }
363
364 // update draw tracking for next draw
365 fCurrQuad = 0;
366 fInstancedDrawTracker.fVerticesPerInstance = verticesPerInstance;
367 fInstancedDrawTracker.fIndicesPerInstance = indicesPerInstance;
368 } else {
369 this->INHERITED::drawIndexedInstances(type,
370 instanceCount,
371 verticesPerInstance,
372 indicesPerInstance);
373 }
374
375}
376
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000377void GrInOrderDrawBuffer::onDrawIndexed(GrPrimitiveType primitiveType,
378 int startVertex,
379 int startIndex,
380 int vertexCount,
381 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000382
383 if (!vertexCount || !indexCount) {
384 return;
385 }
386
bsalomon@google.com934c5702012-03-20 21:17:58 +0000387 this->resetDrawTracking();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000388
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000389 GeometryPoolState& poolState = fGeoPoolStateStack.back();
390
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000391 if (this->needsNewClip()) {
392 this->recordClip();
393 }
394 if (this->needsNewState()) {
395 this->recordState();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000396 }
397
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000398 Draw* draw = this->recordDraw();
reed@google.comac10a2d2010-12-22 21:39:39 +0000399
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000400 draw->fPrimitiveType = primitiveType;
401 draw->fStartVertex = startVertex;
402 draw->fStartIndex = startIndex;
403 draw->fVertexCount = vertexCount;
404 draw->fIndexCount = indexCount;
405
406 draw->fVertexLayout = this->getVertexLayout();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000407 switch (this->getGeomSrc().fVertexSrc) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000408 case kBuffer_GeometrySrcType:
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000409 draw->fVertexBuffer = this->getGeomSrc().fVertexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000410 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000411 case kReserved_GeometrySrcType: // fallthrough
412 case kArray_GeometrySrcType: {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000413 size_t vertexBytes = (vertexCount + startVertex) *
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000414 VertexSize(draw->fVertexLayout);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000415 poolState.fUsedPoolVertexBytes =
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000416 GrMax(poolState.fUsedPoolVertexBytes, vertexBytes);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000417 draw->fVertexBuffer = poolState.fPoolVertexBuffer;
418 draw->fStartVertex += poolState.fPoolStartVertex;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000419 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000420 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000421 default:
422 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000423 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000424 draw->fVertexBuffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000425
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000426 switch (this->getGeomSrc().fIndexSrc) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000427 case kBuffer_GeometrySrcType:
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000428 draw->fIndexBuffer = this->getGeomSrc().fIndexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000429 break;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000430 case kReserved_GeometrySrcType: // fallthrough
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000431 case kArray_GeometrySrcType: {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000432 size_t indexBytes = (indexCount + startIndex) * sizeof(uint16_t);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000433 poolState.fUsedPoolIndexBytes =
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000434 GrMax(poolState.fUsedPoolIndexBytes, indexBytes);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000435 draw->fIndexBuffer = poolState.fPoolIndexBuffer;
436 draw->fStartIndex += poolState.fPoolStartIndex;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000437 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000438 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000439 default:
440 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000441 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000442 draw->fIndexBuffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000443}
444
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000445void GrInOrderDrawBuffer::onDrawNonIndexed(GrPrimitiveType primitiveType,
446 int startVertex,
447 int vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000448 if (!vertexCount) {
449 return;
450 }
451
bsalomon@google.com934c5702012-03-20 21:17:58 +0000452 this->resetDrawTracking();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000453
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000454 GeometryPoolState& poolState = fGeoPoolStateStack.back();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000455 if (this->needsNewClip()) {
456 this->recordClip();
457 }
458 if (this->needsNewState()) {
459 this->recordState();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000460 }
461
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000462 Draw* draw = this->recordDraw();
463 draw->fPrimitiveType = primitiveType;
464 draw->fStartVertex = startVertex;
465 draw->fStartIndex = 0;
466 draw->fVertexCount = vertexCount;
467 draw->fIndexCount = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000468
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000469 draw->fVertexLayout = this->getVertexLayout();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000470 switch (this->getGeomSrc().fVertexSrc) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000471 case kBuffer_GeometrySrcType:
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000472 draw->fVertexBuffer = this->getGeomSrc().fVertexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000473 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000474 case kReserved_GeometrySrcType: // fallthrough
475 case kArray_GeometrySrcType: {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000476 size_t vertexBytes = (vertexCount + startVertex) *
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000477 VertexSize(draw->fVertexLayout);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000478 poolState.fUsedPoolVertexBytes =
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000479 GrMax(poolState.fUsedPoolVertexBytes, vertexBytes);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000480 draw->fVertexBuffer = poolState.fPoolVertexBuffer;
481 draw->fStartVertex += poolState.fPoolStartVertex;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000482 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000483 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000484 default:
485 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000486 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000487 draw->fVertexBuffer->ref();
488 draw->fIndexBuffer = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000489}
490
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000491GrInOrderDrawBuffer::StencilPath::StencilPath() : fStroke(SkStrokeRec::kFill_InitStyle) {}
492
493void GrInOrderDrawBuffer::onStencilPath(const GrPath* path, const SkStrokeRec& stroke,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000494 SkPath::FillType fill) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000495 if (this->needsNewClip()) {
496 this->recordClip();
497 }
498 // Only compare the subset of GrDrawState relevant to path stenciling?
499 if (this->needsNewState()) {
500 this->recordState();
501 }
502 StencilPath* sp = this->recordStencilPath();
503 sp->fPath.reset(path);
504 path->ref();
505 sp->fFill = fill;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000506 sp->fStroke = stroke;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000507}
508
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000509void GrInOrderDrawBuffer::clear(const GrIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000510 GrColor color,
511 GrRenderTarget* renderTarget) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000512 GrIRect r;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000513 if (NULL == renderTarget) {
514 renderTarget = this->drawState()->getRenderTarget();
515 GrAssert(NULL != renderTarget);
516 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000517 if (NULL == rect) {
518 // We could do something smart and remove previous draws and clears to
519 // the current render target. If we get that smart we have to make sure
520 // those draws aren't read before this clear (render-to-texture).
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000521 r.setLTRB(0, 0, renderTarget->width(), renderTarget->height());
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000522 rect = &r;
523 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000524 Clear* clr = this->recordClear();
525 clr->fColor = color;
526 clr->fRect = *rect;
527 clr->fRenderTarget = renderTarget;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000528 renderTarget->ref();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000529}
530
reed@google.comac10a2d2010-12-22 21:39:39 +0000531void GrInOrderDrawBuffer::reset() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000532 GrAssert(1 == fGeoPoolStateStack.count());
533 this->resetVertexSource();
534 this->resetIndexSource();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000535 int numDraws = fDraws.count();
536 for (int d = 0; d < numDraws; ++d) {
537 // we always have a VB, but not always an IB
538 GrAssert(NULL != fDraws[d].fVertexBuffer);
539 fDraws[d].fVertexBuffer->unref();
540 GrSafeUnref(fDraws[d].fIndexBuffer);
541 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000542 fCmds.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000543 fDraws.reset();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000544 fStencilPaths.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000545 fStates.reset();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000546 fClears.reset();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000547 fVertexPool.reset();
548 fIndexPool.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000549 fClips.reset();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000550 fClipOrigins.reset();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000551 fClipSet = true;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000552
bsalomon@google.com934c5702012-03-20 21:17:58 +0000553 this->resetDrawTracking();
reed@google.comac10a2d2010-12-22 21:39:39 +0000554}
555
bsalomon@google.com55e4a202013-01-11 13:54:21 +0000556bool GrInOrderDrawBuffer::flushTo(GrDrawTarget* target) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000557 GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fVertexSrc);
558 GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000559
reed@google.comac10a2d2010-12-22 21:39:39 +0000560 GrAssert(NULL != target);
561 GrAssert(target != this); // not considered and why?
562
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000563 int numCmds = fCmds.count();
bsalomon@google.com358e4272013-01-10 14:40:28 +0000564 if (0 == numCmds) {
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000565 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000566 }
567
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000568 fVertexPool.unlock();
569 fIndexPool.unlock();
reed@google.comac10a2d2010-12-22 21:39:39 +0000570
reed@google.comac10a2d2010-12-22 21:39:39 +0000571 GrDrawTarget::AutoClipRestore acr(target);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000572 AutoGeometryPush agp(target);
bsalomon@google.comca432082013-01-23 19:53:46 +0000573
574 GrDrawState playbackState;
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000575 GrDrawState* prevDrawState = target->drawState();
576 prevDrawState->ref();
bsalomon@google.comca432082013-01-23 19:53:46 +0000577 target->setDrawState(&playbackState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000578
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000579 GrClipData clipData;
580
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000581 int currState = 0;
582 int currClip = 0;
583 int currClear = 0;
584 int currDraw = 0;
585 int currStencilPath = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000586
bsalomon@google.comca432082013-01-23 19:53:46 +0000587
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000588 for (int c = 0; c < numCmds; ++c) {
589 switch (fCmds[c]) {
590 case kDraw_Cmd: {
591 const Draw& draw = fDraws[currDraw];
592 target->setVertexSourceToBuffer(draw.fVertexLayout, draw.fVertexBuffer);
593 if (draw.fIndexCount) {
594 target->setIndexSourceToBuffer(draw.fIndexBuffer);
595 }
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000596
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000597 if (draw.fIndexCount) {
598 target->drawIndexed(draw.fPrimitiveType,
599 draw.fStartVertex,
600 draw.fStartIndex,
601 draw.fVertexCount,
602 draw.fIndexCount);
603 } else {
604 target->drawNonIndexed(draw.fPrimitiveType,
605 draw.fStartVertex,
606 draw.fVertexCount);
607 }
608 ++currDraw;
609 break;
610 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000611 case kStencilPath_Cmd: {
612 const StencilPath& sp = fStencilPaths[currStencilPath];
sugoi@google.com12b4e272012-12-06 20:13:11 +0000613 target->stencilPath(sp.fPath.get(), sp.fStroke, sp.fFill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000614 ++currStencilPath;
615 break;
616 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000617 case kSetState_Cmd:
bsalomon@google.comca432082013-01-23 19:53:46 +0000618 fStates[currState].restoreTo(&playbackState);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000619 ++currState;
620 break;
621 case kSetClip_Cmd:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000622 clipData.fClipStack = &fClips[currClip];
623 clipData.fOrigin = fClipOrigins[currClip];
624 target->setClip(&clipData);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000625 ++currClip;
626 break;
627 case kClear_Cmd:
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000628 target->clear(&fClears[currClear].fRect,
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000629 fClears[currClear].fColor,
630 fClears[currClear].fRenderTarget);
631 ++currClear;
632 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000633 }
634 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000635 // we should have consumed all the states, clips, etc.
636 GrAssert(fStates.count() == currState);
637 GrAssert(fClips.count() == currClip);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000638 GrAssert(fClipOrigins.count() == currClip);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000639 GrAssert(fClears.count() == currClear);
640 GrAssert(fDraws.count() == currDraw);
641
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000642 target->setDrawState(prevDrawState);
643 prevDrawState->unref();
bsalomon@google.com55e4a202013-01-11 13:54:21 +0000644 this->reset();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000645 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000646}
647
bsalomon@google.com97805382012-03-13 14:32:07 +0000648void GrInOrderDrawBuffer::setAutoFlushTarget(GrDrawTarget* target) {
649 GrSafeAssign(fAutoFlushTarget, target);
650}
651
652void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace(
653 GrVertexLayout vertexLayout,
654 int vertexCount,
655 int indexCount) {
656 if (NULL != fAutoFlushTarget) {
657 // We use geometryHints() to know whether to flush the draw buffer. We
658 // can't flush if we are inside an unbalanced pushGeometrySource.
659 // Moreover, flushing blows away vertex and index data that was
660 // previously reserved. So if the vertex or index data is pulled from
661 // reserved space and won't be released by this request then we can't
662 // flush.
663 bool insideGeoPush = fGeoPoolStateStack.count() > 1;
664
665 bool unreleasedVertexSpace =
666 !vertexCount &&
667 kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc;
668
669 bool unreleasedIndexSpace =
670 !indexCount &&
671 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
672
673 // we don't want to finalize any reserved geom on the target since
674 // we don't know that the client has finished writing to it.
675 bool targetHasReservedGeom =
676 fAutoFlushTarget->hasReservedVerticesOrIndices();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000677
bsalomon@google.com97805382012-03-13 14:32:07 +0000678 int vcount = vertexCount;
679 int icount = indexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000680
bsalomon@google.com97805382012-03-13 14:32:07 +0000681 if (!insideGeoPush &&
682 !unreleasedVertexSpace &&
683 !unreleasedIndexSpace &&
684 !targetHasReservedGeom &&
685 this->geometryHints(vertexLayout, &vcount, &icount)) {
686
687 this->flushTo(fAutoFlushTarget);
688 }
689 }
690}
691
reed@google.comac10a2d2010-12-22 21:39:39 +0000692bool GrInOrderDrawBuffer::geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000693 int* vertexCount,
694 int* indexCount) const {
695 // we will recommend a flush if the data could fit in a single
696 // preallocated buffer but none are left and it can't fit
697 // in the current buffer (which may not be prealloced).
reed@google.comac10a2d2010-12-22 21:39:39 +0000698 bool flush = false;
699 if (NULL != indexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000700 int32_t currIndices = fIndexPool.currentBufferIndices();
701 if (*indexCount > currIndices &&
702 (!fIndexPool.preallocatedBuffersRemaining() &&
703 *indexCount <= fIndexPool.preallocatedBufferIndices())) {
704
705 flush = true;
706 }
707 *indexCount = currIndices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000708 }
709 if (NULL != vertexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000710 int32_t currVertices = fVertexPool.currentBufferVertices(vertexLayout);
711 if (*vertexCount > currVertices &&
712 (!fVertexPool.preallocatedBuffersRemaining() &&
713 *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexLayout))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000714
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000715 flush = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000716 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000717 *vertexCount = currVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000718 }
719 return flush;
720}
721
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000722bool GrInOrderDrawBuffer::onReserveVertexSpace(GrVertexLayout vertexLayout,
723 int vertexCount,
724 void** vertices) {
725 GeometryPoolState& poolState = fGeoPoolStateStack.back();
726 GrAssert(vertexCount > 0);
727 GrAssert(NULL != vertices);
728 GrAssert(0 == poolState.fUsedPoolVertexBytes);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000729
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000730 *vertices = fVertexPool.makeSpace(vertexLayout,
731 vertexCount,
732 &poolState.fPoolVertexBuffer,
733 &poolState.fPoolStartVertex);
734 return NULL != *vertices;
735}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000736
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000737bool GrInOrderDrawBuffer::onReserveIndexSpace(int indexCount, void** indices) {
738 GeometryPoolState& poolState = fGeoPoolStateStack.back();
739 GrAssert(indexCount > 0);
740 GrAssert(NULL != indices);
741 GrAssert(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000742
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000743 *indices = fIndexPool.makeSpace(indexCount,
744 &poolState.fPoolIndexBuffer,
745 &poolState.fPoolStartIndex);
746 return NULL != *indices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000747}
748
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000749void GrInOrderDrawBuffer::releaseReservedVertexSpace() {
750 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000751 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000752
753 // If we get a release vertex space call then our current source should either be reserved
754 // or array (which we copied into reserved space).
755 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc ||
756 kArray_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000757
758 // When the caller reserved vertex buffer space we gave it back a pointer
759 // provided by the vertex buffer pool. At each draw we tracked the largest
760 // offset into the pool's pointer that was referenced. Now we return to the
761 // pool any portion at the tail of the allocation that no draw referenced.
762 size_t reservedVertexBytes = VertexSize(geoSrc.fVertexLayout) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000763 geoSrc.fVertexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000764 fVertexPool.putBack(reservedVertexBytes -
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000765 poolState.fUsedPoolVertexBytes);
766 poolState.fUsedPoolVertexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000767 poolState.fPoolVertexBuffer = NULL;
768 poolState.fPoolStartVertex = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000769}
770
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000771void GrInOrderDrawBuffer::releaseReservedIndexSpace() {
772 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000773 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000774
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000775 // If we get a release index space call then our current source should either be reserved
776 // or array (which we copied into reserved space).
777 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc ||
778 kArray_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000779
780 // Similar to releaseReservedVertexSpace we return any unused portion at
781 // the tail
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000782 size_t reservedIndexBytes = sizeof(uint16_t) * geoSrc.fIndexCount;
783 fIndexPool.putBack(reservedIndexBytes - poolState.fUsedPoolIndexBytes);
784 poolState.fUsedPoolIndexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000785 poolState.fPoolIndexBuffer = NULL;
786 poolState.fPoolStartIndex = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000787}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000788
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000789void GrInOrderDrawBuffer::onSetVertexSourceToArray(const void* vertexArray,
790 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000791
792 GeometryPoolState& poolState = fGeoPoolStateStack.back();
793 GrAssert(0 == poolState.fUsedPoolVertexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000794#if GR_DEBUG
795 bool success =
796#endif
bsalomon@google.come79c8152012-03-29 19:07:12 +0000797 fVertexPool.appendVertices(this->getVertexLayout(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000798 vertexCount,
799 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000800 &poolState.fPoolVertexBuffer,
801 &poolState.fPoolStartVertex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000802 GR_DEBUGASSERT(success);
803}
804
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000805void GrInOrderDrawBuffer::onSetIndexSourceToArray(const void* indexArray,
806 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000807 GeometryPoolState& poolState = fGeoPoolStateStack.back();
808 GrAssert(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000809#if GR_DEBUG
810 bool success =
811#endif
812 fIndexPool.appendIndices(indexCount,
813 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000814 &poolState.fPoolIndexBuffer,
815 &poolState.fPoolStartIndex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000816 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000817}
818
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000819void GrInOrderDrawBuffer::releaseVertexArray() {
820 // When the client provides an array as the vertex source we handled it
821 // by copying their array into reserved space.
822 this->GrInOrderDrawBuffer::releaseReservedVertexSpace();
823}
824
825void GrInOrderDrawBuffer::releaseIndexArray() {
826 // When the client provides an array as the index source we handled it
827 // by copying their array into reserved space.
828 this->GrInOrderDrawBuffer::releaseReservedIndexSpace();
829}
830
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000831void GrInOrderDrawBuffer::geometrySourceWillPush() {
832 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
833 poolState.fUsedPoolVertexBytes = 0;
834 poolState.fUsedPoolIndexBytes = 0;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000835 this->resetDrawTracking();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000836#if GR_DEBUG
837 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
838 poolState.fPoolStartVertex = ~0;
839 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
840 poolState.fPoolStartIndex = ~0;
841#endif
842}
843
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000844void GrInOrderDrawBuffer::geometrySourceWillPop(
845 const GeometrySrcState& restoredState) {
846 GrAssert(fGeoPoolStateStack.count() > 1);
847 fGeoPoolStateStack.pop_back();
848 GeometryPoolState& poolState = fGeoPoolStateStack.back();
849 // we have to assume that any slack we had in our vertex/index data
850 // is now unreleasable because data may have been appended later in the
851 // pool.
852 if (kReserved_GeometrySrcType == restoredState.fVertexSrc ||
853 kArray_GeometrySrcType == restoredState.fVertexSrc) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000854 poolState.fUsedPoolVertexBytes =
855 VertexSize(restoredState.fVertexLayout) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000856 restoredState.fVertexCount;
857 }
858 if (kReserved_GeometrySrcType == restoredState.fIndexSrc ||
859 kArray_GeometrySrcType == restoredState.fIndexSrc) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000860 poolState.fUsedPoolIndexBytes = sizeof(uint16_t) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000861 restoredState.fIndexCount;
862 }
bsalomon@google.com934c5702012-03-20 21:17:58 +0000863 this->resetDrawTracking();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000864}
865
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000866bool GrInOrderDrawBuffer::needsNewState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000867 return fStates.empty() || !fStates.back().isEqual(this->getDrawState());
reed@google.comac10a2d2010-12-22 21:39:39 +0000868}
869
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000870bool GrInOrderDrawBuffer::needsNewClip() const {
bsalomon@google.com358e4272013-01-10 14:40:28 +0000871 GrAssert(fClips.count() == fClipOrigins.count());
872 if (this->getDrawState().isClipState()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000873 if (fClipSet &&
bsalomon@google.com358e4272013-01-10 14:40:28 +0000874 (fClips.empty() ||
875 fClips.back() != *fClip->fClipStack ||
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000876 fClipOrigins.back() != fClip->fOrigin)) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000877 return true;
878 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000879 }
880 return false;
881}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000882
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000883void GrInOrderDrawBuffer::recordClip() {
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000884 fClips.push_back() = *fClip->fClipStack;
885 fClipOrigins.push_back() = fClip->fOrigin;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000886 fClipSet = false;
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000887 fCmds.push_back(kSetClip_Cmd);
888}
889
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000890void GrInOrderDrawBuffer::recordState() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000891 fStates.push_back().saveFrom(this->getDrawState());
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000892 fCmds.push_back(kSetState_Cmd);
893}
894
895GrInOrderDrawBuffer::Draw* GrInOrderDrawBuffer::recordDraw() {
896 fCmds.push_back(kDraw_Cmd);
897 return &fDraws.push_back();
898}
899
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000900GrInOrderDrawBuffer::StencilPath* GrInOrderDrawBuffer::recordStencilPath() {
901 fCmds.push_back(kStencilPath_Cmd);
902 return &fStencilPaths.push_back();
903}
904
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000905GrInOrderDrawBuffer::Clear* GrInOrderDrawBuffer::recordClear() {
906 fCmds.push_back(kClear_Cmd);
907 return &fClears.push_back();
reed@google.comac10a2d2010-12-22 21:39:39 +0000908}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000909
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000910void GrInOrderDrawBuffer::clipWillBeSet(const GrClipData* newClipData) {
911 INHERITED::clipWillBeSet(newClipData);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000912 fClipSet = true;
913}