blob: 51546e7baa8d02611ddf064dfdee1d4beb68a564 [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();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000303 draw->fIndexBuffer = geomSrc.fIndexBuffer;
304 geomSrc.fIndexBuffer->ref();
305 draw->fVertexBuffer = vertexBuffer;
306 vertexBuffer->ref();
307 draw->fPrimitiveType = type;
308 draw->fStartIndex = 0;
309 draw->fIndexCount = 0;
310 draw->fStartVertex = poolState.fPoolStartVertex;
311 draw->fVertexCount = 0;
312 draw->fVertexLayout = geomSrc.fVertexLayout;
313 } 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();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000346 draw->fIndexBuffer = geomSrc.fIndexBuffer;
347 geomSrc.fIndexBuffer->ref();
348 draw->fVertexBuffer = vertexBuffer;
349 vertexBuffer->ref();
350 draw->fPrimitiveType = type;
351 draw->fStartIndex = 0;
352 draw->fStartVertex = startVertex;
353 draw->fVertexCount = 0;
354 draw->fVertexLayout = geomSrc.fVertexLayout;
355 instancesToConcat = maxInstancesPerDraw;
356 }
357 draw->fVertexCount += instancesToConcat * verticesPerInstance;
358 draw->fIndexCount += instancesToConcat * indicesPerInstance;
359 instanceCount -= instancesToConcat;
360 instancesToConcat = 0;
361 }
362
363 // update draw tracking for next draw
364 fCurrQuad = 0;
365 fInstancedDrawTracker.fVerticesPerInstance = verticesPerInstance;
366 fInstancedDrawTracker.fIndicesPerInstance = indicesPerInstance;
367 } else {
368 this->INHERITED::drawIndexedInstances(type,
369 instanceCount,
370 verticesPerInstance,
371 indicesPerInstance);
372 }
373
374}
375
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000376void GrInOrderDrawBuffer::onDrawIndexed(GrPrimitiveType primitiveType,
377 int startVertex,
378 int startIndex,
379 int vertexCount,
380 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000381
382 if (!vertexCount || !indexCount) {
383 return;
384 }
385
bsalomon@google.com934c5702012-03-20 21:17:58 +0000386 this->resetDrawTracking();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000387
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000388 GeometryPoolState& poolState = fGeoPoolStateStack.back();
389
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000390 if (this->needsNewClip()) {
391 this->recordClip();
392 }
393 if (this->needsNewState()) {
394 this->recordState();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000395 }
396
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000397 Draw* draw = this->recordDraw();
reed@google.comac10a2d2010-12-22 21:39:39 +0000398
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000399 draw->fPrimitiveType = primitiveType;
400 draw->fStartVertex = startVertex;
401 draw->fStartIndex = startIndex;
402 draw->fVertexCount = vertexCount;
403 draw->fIndexCount = indexCount;
404
405 draw->fVertexLayout = this->getVertexLayout();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000406 switch (this->getGeomSrc().fVertexSrc) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000407 case kBuffer_GeometrySrcType:
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000408 draw->fVertexBuffer = this->getGeomSrc().fVertexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000409 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000410 case kReserved_GeometrySrcType: // fallthrough
411 case kArray_GeometrySrcType: {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000412 size_t vertexBytes = (vertexCount + startVertex) *
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000413 VertexSize(draw->fVertexLayout);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000414 poolState.fUsedPoolVertexBytes =
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000415 GrMax(poolState.fUsedPoolVertexBytes, vertexBytes);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000416 draw->fVertexBuffer = poolState.fPoolVertexBuffer;
417 draw->fStartVertex += poolState.fPoolStartVertex;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000418 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000419 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000420 default:
421 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000422 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000423 draw->fVertexBuffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000424
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000425 switch (this->getGeomSrc().fIndexSrc) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000426 case kBuffer_GeometrySrcType:
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000427 draw->fIndexBuffer = this->getGeomSrc().fIndexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000428 break;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000429 case kReserved_GeometrySrcType: // fallthrough
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000430 case kArray_GeometrySrcType: {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000431 size_t indexBytes = (indexCount + startIndex) * sizeof(uint16_t);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000432 poolState.fUsedPoolIndexBytes =
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000433 GrMax(poolState.fUsedPoolIndexBytes, indexBytes);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000434 draw->fIndexBuffer = poolState.fPoolIndexBuffer;
435 draw->fStartIndex += poolState.fPoolStartIndex;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000436 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000437 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000438 default:
439 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000440 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000441 draw->fIndexBuffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000442}
443
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000444void GrInOrderDrawBuffer::onDrawNonIndexed(GrPrimitiveType primitiveType,
445 int startVertex,
446 int vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000447 if (!vertexCount) {
448 return;
449 }
450
bsalomon@google.com934c5702012-03-20 21:17:58 +0000451 this->resetDrawTracking();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000452
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000453 GeometryPoolState& poolState = fGeoPoolStateStack.back();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000454 if (this->needsNewClip()) {
455 this->recordClip();
456 }
457 if (this->needsNewState()) {
458 this->recordState();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000459 }
460
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000461 Draw* draw = this->recordDraw();
462 draw->fPrimitiveType = primitiveType;
463 draw->fStartVertex = startVertex;
464 draw->fStartIndex = 0;
465 draw->fVertexCount = vertexCount;
466 draw->fIndexCount = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000467
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000468 draw->fVertexLayout = this->getVertexLayout();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000469 switch (this->getGeomSrc().fVertexSrc) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000470 case kBuffer_GeometrySrcType:
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000471 draw->fVertexBuffer = this->getGeomSrc().fVertexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000472 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000473 case kReserved_GeometrySrcType: // fallthrough
474 case kArray_GeometrySrcType: {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000475 size_t vertexBytes = (vertexCount + startVertex) *
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000476 VertexSize(draw->fVertexLayout);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000477 poolState.fUsedPoolVertexBytes =
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000478 GrMax(poolState.fUsedPoolVertexBytes, vertexBytes);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000479 draw->fVertexBuffer = poolState.fPoolVertexBuffer;
480 draw->fStartVertex += poolState.fPoolStartVertex;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000481 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000482 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000483 default:
484 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000485 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000486 draw->fVertexBuffer->ref();
487 draw->fIndexBuffer = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000488}
489
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000490GrInOrderDrawBuffer::StencilPath::StencilPath() : fStroke(SkStrokeRec::kFill_InitStyle) {}
491
492void GrInOrderDrawBuffer::onStencilPath(const GrPath* path, const SkStrokeRec& stroke,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000493 SkPath::FillType fill) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000494 if (this->needsNewClip()) {
495 this->recordClip();
496 }
497 // Only compare the subset of GrDrawState relevant to path stenciling?
498 if (this->needsNewState()) {
499 this->recordState();
500 }
501 StencilPath* sp = this->recordStencilPath();
502 sp->fPath.reset(path);
503 path->ref();
504 sp->fFill = fill;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000505 sp->fStroke = stroke;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000506}
507
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000508void GrInOrderDrawBuffer::clear(const GrIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000509 GrColor color,
510 GrRenderTarget* renderTarget) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000511 GrIRect r;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000512 if (NULL == renderTarget) {
513 renderTarget = this->drawState()->getRenderTarget();
514 GrAssert(NULL != renderTarget);
515 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000516 if (NULL == rect) {
517 // We could do something smart and remove previous draws and clears to
518 // the current render target. If we get that smart we have to make sure
519 // those draws aren't read before this clear (render-to-texture).
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000520 r.setLTRB(0, 0, renderTarget->width(), renderTarget->height());
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000521 rect = &r;
522 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000523 Clear* clr = this->recordClear();
524 clr->fColor = color;
525 clr->fRect = *rect;
526 clr->fRenderTarget = renderTarget;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000527 renderTarget->ref();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000528}
529
reed@google.comac10a2d2010-12-22 21:39:39 +0000530void GrInOrderDrawBuffer::reset() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000531 GrAssert(1 == fGeoPoolStateStack.count());
532 this->resetVertexSource();
533 this->resetIndexSource();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000534 int numDraws = fDraws.count();
535 for (int d = 0; d < numDraws; ++d) {
536 // we always have a VB, but not always an IB
537 GrAssert(NULL != fDraws[d].fVertexBuffer);
538 fDraws[d].fVertexBuffer->unref();
539 GrSafeUnref(fDraws[d].fIndexBuffer);
540 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000541 fCmds.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000542 fDraws.reset();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000543 fStencilPaths.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000544 fStates.reset();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000545 fClears.reset();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000546 fVertexPool.reset();
547 fIndexPool.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000548 fClips.reset();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000549 fClipOrigins.reset();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000550 fClipSet = true;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000551
bsalomon@google.com934c5702012-03-20 21:17:58 +0000552 this->resetDrawTracking();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000553
554 // we start off with a default clip and state so that we don't have
555 // to do count checks on fClips, fStates, or fCmds before checking their
556 // last entry.
557 this->recordDefaultState();
558 this->recordDefaultClip();
reed@google.comac10a2d2010-12-22 21:39:39 +0000559}
560
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000561bool GrInOrderDrawBuffer::playback(GrDrawTarget* target) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000562 GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fVertexSrc);
563 GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000564
reed@google.comac10a2d2010-12-22 21:39:39 +0000565 GrAssert(NULL != target);
566 GrAssert(target != this); // not considered and why?
567
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000568 int numCmds = fCmds.count();
569 GrAssert(numCmds >= 2);
570 if (2 == numCmds) {
571 GrAssert(kSetState_Cmd == fCmds[0]);
572 GrAssert(kSetClip_Cmd == fCmds[1]);
573 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000574 }
575
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000576 fVertexPool.unlock();
577 fIndexPool.unlock();
reed@google.comac10a2d2010-12-22 21:39:39 +0000578
reed@google.comac10a2d2010-12-22 21:39:39 +0000579 GrDrawTarget::AutoClipRestore acr(target);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000580 AutoGeometryPush agp(target);
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000581 GrDrawState* prevDrawState = target->drawState();
582 prevDrawState->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000583
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000584 GrClipData clipData;
585
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000586 int currState = 0;
587 int currClip = 0;
588 int currClear = 0;
589 int currDraw = 0;
590 int currStencilPath = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000591
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000592 for (int c = 0; c < numCmds; ++c) {
593 switch (fCmds[c]) {
594 case kDraw_Cmd: {
595 const Draw& draw = fDraws[currDraw];
596 target->setVertexSourceToBuffer(draw.fVertexLayout, draw.fVertexBuffer);
597 if (draw.fIndexCount) {
598 target->setIndexSourceToBuffer(draw.fIndexBuffer);
599 }
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000600
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000601 if (draw.fIndexCount) {
602 target->drawIndexed(draw.fPrimitiveType,
603 draw.fStartVertex,
604 draw.fStartIndex,
605 draw.fVertexCount,
606 draw.fIndexCount);
607 } else {
608 target->drawNonIndexed(draw.fPrimitiveType,
609 draw.fStartVertex,
610 draw.fVertexCount);
611 }
612 ++currDraw;
613 break;
614 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000615 case kStencilPath_Cmd: {
616 const StencilPath& sp = fStencilPaths[currStencilPath];
sugoi@google.com12b4e272012-12-06 20:13:11 +0000617 target->stencilPath(sp.fPath.get(), sp.fStroke, sp.fFill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000618 ++currStencilPath;
619 break;
620 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000621 case kSetState_Cmd:
622 target->setDrawState(&fStates[currState]);
623 ++currState;
624 break;
625 case kSetClip_Cmd:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000626 clipData.fClipStack = &fClips[currClip];
627 clipData.fOrigin = fClipOrigins[currClip];
628 target->setClip(&clipData);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000629 ++currClip;
630 break;
631 case kClear_Cmd:
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000632 target->clear(&fClears[currClear].fRect,
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000633 fClears[currClear].fColor,
634 fClears[currClear].fRenderTarget);
635 ++currClear;
636 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000637 }
638 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000639 // we should have consumed all the states, clips, etc.
640 GrAssert(fStates.count() == currState);
641 GrAssert(fClips.count() == currClip);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000642 GrAssert(fClipOrigins.count() == currClip);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000643 GrAssert(fClears.count() == currClear);
644 GrAssert(fDraws.count() == currDraw);
645
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000646 target->setDrawState(prevDrawState);
647 prevDrawState->unref();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000648 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000649}
650
bsalomon@google.com97805382012-03-13 14:32:07 +0000651void GrInOrderDrawBuffer::setAutoFlushTarget(GrDrawTarget* target) {
652 GrSafeAssign(fAutoFlushTarget, target);
653}
654
655void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace(
656 GrVertexLayout vertexLayout,
657 int vertexCount,
658 int indexCount) {
659 if (NULL != fAutoFlushTarget) {
660 // We use geometryHints() to know whether to flush the draw buffer. We
661 // can't flush if we are inside an unbalanced pushGeometrySource.
662 // Moreover, flushing blows away vertex and index data that was
663 // previously reserved. So if the vertex or index data is pulled from
664 // reserved space and won't be released by this request then we can't
665 // flush.
666 bool insideGeoPush = fGeoPoolStateStack.count() > 1;
667
668 bool unreleasedVertexSpace =
669 !vertexCount &&
670 kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc;
671
672 bool unreleasedIndexSpace =
673 !indexCount &&
674 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
675
676 // we don't want to finalize any reserved geom on the target since
677 // we don't know that the client has finished writing to it.
678 bool targetHasReservedGeom =
679 fAutoFlushTarget->hasReservedVerticesOrIndices();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000680
bsalomon@google.com97805382012-03-13 14:32:07 +0000681 int vcount = vertexCount;
682 int icount = indexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000683
bsalomon@google.com97805382012-03-13 14:32:07 +0000684 if (!insideGeoPush &&
685 !unreleasedVertexSpace &&
686 !unreleasedIndexSpace &&
687 !targetHasReservedGeom &&
688 this->geometryHints(vertexLayout, &vcount, &icount)) {
689
690 this->flushTo(fAutoFlushTarget);
691 }
692 }
693}
694
reed@google.comac10a2d2010-12-22 21:39:39 +0000695bool GrInOrderDrawBuffer::geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000696 int* vertexCount,
697 int* indexCount) const {
698 // we will recommend a flush if the data could fit in a single
699 // preallocated buffer but none are left and it can't fit
700 // in the current buffer (which may not be prealloced).
reed@google.comac10a2d2010-12-22 21:39:39 +0000701 bool flush = false;
702 if (NULL != indexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000703 int32_t currIndices = fIndexPool.currentBufferIndices();
704 if (*indexCount > currIndices &&
705 (!fIndexPool.preallocatedBuffersRemaining() &&
706 *indexCount <= fIndexPool.preallocatedBufferIndices())) {
707
708 flush = true;
709 }
710 *indexCount = currIndices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000711 }
712 if (NULL != vertexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000713 int32_t currVertices = fVertexPool.currentBufferVertices(vertexLayout);
714 if (*vertexCount > currVertices &&
715 (!fVertexPool.preallocatedBuffersRemaining() &&
716 *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexLayout))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000717
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000718 flush = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000719 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000720 *vertexCount = currVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000721 }
722 return flush;
723}
724
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000725bool GrInOrderDrawBuffer::onReserveVertexSpace(GrVertexLayout vertexLayout,
726 int vertexCount,
727 void** vertices) {
728 GeometryPoolState& poolState = fGeoPoolStateStack.back();
729 GrAssert(vertexCount > 0);
730 GrAssert(NULL != vertices);
731 GrAssert(0 == poolState.fUsedPoolVertexBytes);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000732
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000733 *vertices = fVertexPool.makeSpace(vertexLayout,
734 vertexCount,
735 &poolState.fPoolVertexBuffer,
736 &poolState.fPoolStartVertex);
737 return NULL != *vertices;
738}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000739
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000740bool GrInOrderDrawBuffer::onReserveIndexSpace(int indexCount, void** indices) {
741 GeometryPoolState& poolState = fGeoPoolStateStack.back();
742 GrAssert(indexCount > 0);
743 GrAssert(NULL != indices);
744 GrAssert(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000745
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000746 *indices = fIndexPool.makeSpace(indexCount,
747 &poolState.fPoolIndexBuffer,
748 &poolState.fPoolStartIndex);
749 return NULL != *indices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000750}
751
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000752void GrInOrderDrawBuffer::releaseReservedVertexSpace() {
753 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000754 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000755
756 // If we get a release vertex space call then our current source should either be reserved
757 // or array (which we copied into reserved space).
758 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc ||
759 kArray_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000760
761 // When the caller reserved vertex buffer space we gave it back a pointer
762 // provided by the vertex buffer pool. At each draw we tracked the largest
763 // offset into the pool's pointer that was referenced. Now we return to the
764 // pool any portion at the tail of the allocation that no draw referenced.
765 size_t reservedVertexBytes = VertexSize(geoSrc.fVertexLayout) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000766 geoSrc.fVertexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000767 fVertexPool.putBack(reservedVertexBytes -
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000768 poolState.fUsedPoolVertexBytes);
769 poolState.fUsedPoolVertexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000770 poolState.fPoolVertexBuffer = NULL;
771 poolState.fPoolStartVertex = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000772}
773
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000774void GrInOrderDrawBuffer::releaseReservedIndexSpace() {
775 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000776 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000777
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000778 // If we get a release index space call then our current source should either be reserved
779 // or array (which we copied into reserved space).
780 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc ||
781 kArray_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000782
783 // Similar to releaseReservedVertexSpace we return any unused portion at
784 // the tail
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000785 size_t reservedIndexBytes = sizeof(uint16_t) * geoSrc.fIndexCount;
786 fIndexPool.putBack(reservedIndexBytes - poolState.fUsedPoolIndexBytes);
787 poolState.fUsedPoolIndexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000788 poolState.fPoolIndexBuffer = NULL;
789 poolState.fPoolStartIndex = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000790}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000791
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000792void GrInOrderDrawBuffer::onSetVertexSourceToArray(const void* vertexArray,
793 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000794
795 GeometryPoolState& poolState = fGeoPoolStateStack.back();
796 GrAssert(0 == poolState.fUsedPoolVertexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000797#if GR_DEBUG
798 bool success =
799#endif
bsalomon@google.come79c8152012-03-29 19:07:12 +0000800 fVertexPool.appendVertices(this->getVertexLayout(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000801 vertexCount,
802 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000803 &poolState.fPoolVertexBuffer,
804 &poolState.fPoolStartVertex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000805 GR_DEBUGASSERT(success);
806}
807
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000808void GrInOrderDrawBuffer::onSetIndexSourceToArray(const void* indexArray,
809 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000810 GeometryPoolState& poolState = fGeoPoolStateStack.back();
811 GrAssert(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000812#if GR_DEBUG
813 bool success =
814#endif
815 fIndexPool.appendIndices(indexCount,
816 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000817 &poolState.fPoolIndexBuffer,
818 &poolState.fPoolStartIndex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000819 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000820}
821
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000822void GrInOrderDrawBuffer::releaseVertexArray() {
823 // When the client provides an array as the vertex source we handled it
824 // by copying their array into reserved space.
825 this->GrInOrderDrawBuffer::releaseReservedVertexSpace();
826}
827
828void GrInOrderDrawBuffer::releaseIndexArray() {
829 // When the client provides an array as the index source we handled it
830 // by copying their array into reserved space.
831 this->GrInOrderDrawBuffer::releaseReservedIndexSpace();
832}
833
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000834void GrInOrderDrawBuffer::geometrySourceWillPush() {
835 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
836 poolState.fUsedPoolVertexBytes = 0;
837 poolState.fUsedPoolIndexBytes = 0;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000838 this->resetDrawTracking();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000839#if GR_DEBUG
840 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
841 poolState.fPoolStartVertex = ~0;
842 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
843 poolState.fPoolStartIndex = ~0;
844#endif
845}
846
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000847void GrInOrderDrawBuffer::geometrySourceWillPop(
848 const GeometrySrcState& restoredState) {
849 GrAssert(fGeoPoolStateStack.count() > 1);
850 fGeoPoolStateStack.pop_back();
851 GeometryPoolState& poolState = fGeoPoolStateStack.back();
852 // we have to assume that any slack we had in our vertex/index data
853 // is now unreleasable because data may have been appended later in the
854 // pool.
855 if (kReserved_GeometrySrcType == restoredState.fVertexSrc ||
856 kArray_GeometrySrcType == restoredState.fVertexSrc) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000857 poolState.fUsedPoolVertexBytes =
858 VertexSize(restoredState.fVertexLayout) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000859 restoredState.fVertexCount;
860 }
861 if (kReserved_GeometrySrcType == restoredState.fIndexSrc ||
862 kArray_GeometrySrcType == restoredState.fIndexSrc) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000863 poolState.fUsedPoolIndexBytes = sizeof(uint16_t) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000864 restoredState.fIndexCount;
865 }
bsalomon@google.com934c5702012-03-20 21:17:58 +0000866 this->resetDrawTracking();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000867}
868
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000869bool GrInOrderDrawBuffer::needsNewState() const {
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000870 // we should have recorded a default state in reset()
871 GrAssert(!fStates.empty());
872 return fStates.back() != this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +0000873}
874
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000875bool GrInOrderDrawBuffer::needsNewClip() const {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000876 if (this->getDrawState().isClipState()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000877 if (fClipSet &&
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000878 (fClips.back() != *fClip->fClipStack ||
879 fClipOrigins.back() != fClip->fOrigin)) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000880 return true;
881 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000882 }
883 return false;
884}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000885
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000886void GrInOrderDrawBuffer::recordClip() {
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000887 fClips.push_back() = *fClip->fClipStack;
888 fClipOrigins.push_back() = fClip->fOrigin;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000889 fClipSet = false;
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000890 fCmds.push_back(kSetClip_Cmd);
891}
892
893void GrInOrderDrawBuffer::recordDefaultClip() {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000894 fClips.push_back() = SkClipStack();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000895 fClipOrigins.push_back() = SkIPoint::Make(0, 0);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000896 fCmds.push_back(kSetClip_Cmd);
897}
898
899void GrInOrderDrawBuffer::recordState() {
900 fStates.push_back(this->getDrawState());
901 fCmds.push_back(kSetState_Cmd);
902}
903
904void GrInOrderDrawBuffer::recordDefaultState() {
905 fStates.push_back(GrDrawState());
906 fCmds.push_back(kSetState_Cmd);
907}
908
909GrInOrderDrawBuffer::Draw* GrInOrderDrawBuffer::recordDraw() {
910 fCmds.push_back(kDraw_Cmd);
911 return &fDraws.push_back();
912}
913
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000914GrInOrderDrawBuffer::StencilPath* GrInOrderDrawBuffer::recordStencilPath() {
915 fCmds.push_back(kStencilPath_Cmd);
916 return &fStencilPaths.push_back();
917}
918
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000919GrInOrderDrawBuffer::Clear* GrInOrderDrawBuffer::recordClear() {
920 fCmds.push_back(kClear_Cmd);
921 return &fClears.push_back();
reed@google.comac10a2d2010-12-22 21:39:39 +0000922}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000923
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000924void GrInOrderDrawBuffer::clipWillBeSet(const GrClipData* newClipData) {
925 INHERITED::clipWillBeSet(newClipData);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000926 fClipSet = true;
927}