blob: a0585f7a6c3c98cfc640bec8e1858d5529441ab3 [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.com86afc2a2011-02-16 16:12:19 +000078 const GrMatrix* matrix,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000079 const GrRect* srcRects[],
80 const GrMatrix* 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);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000094 AutoReleaseGeometry geo(this, layout, 4, 0);
bsalomon@google.com6513cd02011-08-05 20:12:30 +000095 if (!geo.succeeded()) {
96 GrPrintf("Failed to get space for vertices!\n");
97 return;
98 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000099 GrMatrix combinedMatrix = drawState->getViewMatrix();
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000100 // We go to device space so that matrix changes allow us to concat
101 // rect draws. When the caller has provided explicit source rects
102 // then we don't want to modify the sampler matrices. Otherwise we do
103 // we have to account for the view matrix change in the sampler
104 // matrices.
bsalomon@google.come3d32162012-07-20 13:37:06 +0000105 uint32_t explicitCoordMask = 0;
106 if (srcRects) {
107 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
108 if (srcRects[s]) {
109 explicitCoordMask |= (1 << s);
110 }
111 }
112 }
113 GrDrawTarget::AutoDeviceCoordDraw adcd(this, explicitCoordMask);
114 if (!adcd.succeeded()) {
115 return;
116 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000117 if (NULL != matrix) {
118 combinedMatrix.preConcat(*matrix);
119 }
120
121 SetRectVertices(rect, &combinedMatrix, srcRects, srcMatrices, layout, geo.vertices());
122
123 // we don't want to miss an opportunity to batch rects together
124 // simply because the clip has changed if the clip doesn't affect
125 // the rect.
126 bool disabledClip = false;
bsalomon@google.comd302f142011-03-03 13:54:13 +0000127
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000128 if (drawState->isClipState()) {
129
130 GrRect devClipRect;
131 bool isIntersectionOfRects = false;
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000132
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000133 fClip->fClipStack->getConservativeBounds(-fClip->fOrigin.fX,
134 -fClip->fOrigin.fY,
135 drawState->getRenderTarget()->width(),
136 drawState->getRenderTarget()->height(),
137 &devClipRect,
138 &isIntersectionOfRects);
robertphillips@google.coma6f11c42012-07-23 17:39:44 +0000139
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000140 if (isIntersectionOfRects) {
141 // If the clip rect touches the edge of the viewport, extended it
142 // out (close) to infinity to avoid bogus intersections.
143 // We might consider a more exact clip to viewport if this
144 // conservative test fails.
145 const GrRenderTarget* target = drawState->getRenderTarget();
146 if (0 >= devClipRect.fLeft) {
147 devClipRect.fLeft = GR_ScalarMin;
148 }
149 if (target->width() <= devClipRect.fRight) {
150 devClipRect.fRight = GR_ScalarMax;
151 }
152 if (0 >= devClipRect.top()) {
153 devClipRect.fTop = GR_ScalarMin;
154 }
155 if (target->height() <= devClipRect.fBottom) {
156 devClipRect.fBottom = GR_ScalarMax;
157 }
158 int stride = VertexSize(layout);
159 bool insideClip = true;
160 for (int v = 0; v < 4; ++v) {
161 const GrPoint& p = *GetVertexPoint(geo.vertices(), v, stride);
162 if (!devClipRect.contains(p)) {
163 insideClip = false;
164 break;
165 }
166 }
167 if (insideClip) {
168 drawState->disableState(GrDrawState::kClip_StateBit);
169 disabledClip = true;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000170 }
171 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000172 }
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000173
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000174 if (!this->needsNewClip() &&
175 !this->needsNewState() &&
176 fCurrQuad > 0 &&
177 fCurrQuad < fMaxQuads &&
178 layout == fLastRectVertexLayout) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000179
180 int vsize = VertexSize(layout);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000181
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000182 Draw& lastDraw = fDraws.back();
183
184 GrAssert(lastDraw.fIndexBuffer == fQuadIndexBuffer);
bsalomon@google.com47059542012-06-06 20:51:20 +0000185 GrAssert(kTriangles_GrPrimitiveType == lastDraw.fPrimitiveType);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000186 GrAssert(0 == lastDraw.fVertexCount % 4);
187 GrAssert(0 == lastDraw.fIndexCount % 6);
188 GrAssert(0 == lastDraw.fStartIndex);
189
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000190 GeometryPoolState& poolState = fGeoPoolStateStack.back();
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000191
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000192 appendToPreviousDraw =
193 kDraw_Cmd != fCmds.back() &&
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000194 lastDraw.fVertexBuffer == poolState.fPoolVertexBuffer &&
195 (fCurrQuad * 4 + lastDraw.fStartVertex) == poolState.fPoolStartVertex;
196
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000197 if (appendToPreviousDraw) {
198 lastDraw.fVertexCount += 4;
199 lastDraw.fIndexCount += 6;
200 fCurrQuad += 1;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000201 // we reserved above, so we should be the first
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000202 // use of this vertex reservation.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000203 GrAssert(0 == poolState.fUsedPoolVertexBytes);
204 poolState.fUsedPoolVertexBytes = 4 * vsize;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000205 }
206 }
207 if (!appendToPreviousDraw) {
208 this->setIndexSourceToBuffer(fQuadIndexBuffer);
bsalomon@google.com47059542012-06-06 20:51:20 +0000209 this->drawIndexed(kTriangles_GrPrimitiveType, 0, 0, 4, 6);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000210 fCurrQuad = 1;
211 fLastRectVertexLayout = layout;
212 }
213 if (disabledClip) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000214 drawState->enableState(GrDrawState::kClip_StateBit);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000215 }
bsalomon@google.com934c5702012-03-20 21:17:58 +0000216 fInstancedDrawTracker.reset();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000217 } else {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000218 INHERITED::drawRect(rect, matrix, srcRects, srcMatrices);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000219 }
220}
221
bsalomon@google.com934c5702012-03-20 21:17:58 +0000222void GrInOrderDrawBuffer::drawIndexedInstances(GrPrimitiveType type,
223 int instanceCount,
224 int verticesPerInstance,
225 int indicesPerInstance) {
226 if (!verticesPerInstance || !indicesPerInstance) {
227 return;
228 }
229
230 const GeometrySrcState& geomSrc = this->getGeomSrc();
231
232 // we only attempt to concat the case when reserved verts are used with
233 // an index buffer.
234 if (kReserved_GeometrySrcType == geomSrc.fVertexSrc &&
235 kBuffer_GeometrySrcType == geomSrc.fIndexSrc) {
236
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000237 if (this->needsNewClip()) {
238 this->recordClip();
239 }
240 if (this->needsNewState()) {
241 this->recordState();
242 }
243
bsalomon@google.com934c5702012-03-20 21:17:58 +0000244 Draw* draw = NULL;
245 // if the last draw used the same indices/vertices per shape then we
246 // may be able to append to it.
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000247 if (kDraw_Cmd == fCmds.back() &&
248 verticesPerInstance == fInstancedDrawTracker.fVerticesPerInstance &&
bsalomon@google.com934c5702012-03-20 21:17:58 +0000249 indicesPerInstance == fInstancedDrawTracker.fIndicesPerInstance) {
250 GrAssert(fDraws.count());
251 draw = &fDraws.back();
252 }
253
bsalomon@google.com934c5702012-03-20 21:17:58 +0000254 GeometryPoolState& poolState = fGeoPoolStateStack.back();
255 const GrVertexBuffer* vertexBuffer = poolState.fPoolVertexBuffer;
256
257 // Check whether the draw is compatible with this draw in order to
258 // append
259 if (NULL == draw ||
bsalomon@google.com934c5702012-03-20 21:17:58 +0000260 draw->fIndexBuffer != geomSrc.fIndexBuffer ||
261 draw->fPrimitiveType != type ||
262 draw->fVertexBuffer != vertexBuffer) {
263
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000264 draw = this->recordDraw();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000265 draw->fIndexBuffer = geomSrc.fIndexBuffer;
266 geomSrc.fIndexBuffer->ref();
267 draw->fVertexBuffer = vertexBuffer;
268 vertexBuffer->ref();
269 draw->fPrimitiveType = type;
270 draw->fStartIndex = 0;
271 draw->fIndexCount = 0;
272 draw->fStartVertex = poolState.fPoolStartVertex;
273 draw->fVertexCount = 0;
274 draw->fVertexLayout = geomSrc.fVertexLayout;
275 } else {
276 GrAssert(!(draw->fIndexCount % indicesPerInstance));
277 GrAssert(!(draw->fVertexCount % verticesPerInstance));
278 GrAssert(poolState.fPoolStartVertex == draw->fStartVertex +
279 draw->fVertexCount);
280 }
281
282 // how many instances can be in a single draw
283 int maxInstancesPerDraw = this->indexCountInCurrentSource() /
284 indicesPerInstance;
285 if (!maxInstancesPerDraw) {
286 return;
287 }
288 // how many instances should be concat'ed onto draw
289 int instancesToConcat = maxInstancesPerDraw - draw->fVertexCount /
290 verticesPerInstance;
291 if (maxInstancesPerDraw > instanceCount) {
292 maxInstancesPerDraw = instanceCount;
293 if (instancesToConcat > instanceCount) {
294 instancesToConcat = instanceCount;
295 }
296 }
297
298 // update the amount of reserved data actually referenced in draws
299 size_t vertexBytes = instanceCount * verticesPerInstance *
300 VertexSize(draw->fVertexLayout);
301 poolState.fUsedPoolVertexBytes =
302 GrMax(poolState.fUsedPoolVertexBytes, vertexBytes);
303
304 while (instanceCount) {
305 if (!instancesToConcat) {
306 int startVertex = draw->fStartVertex + draw->fVertexCount;
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000307 draw = this->recordDraw();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000308 draw->fIndexBuffer = geomSrc.fIndexBuffer;
309 geomSrc.fIndexBuffer->ref();
310 draw->fVertexBuffer = vertexBuffer;
311 vertexBuffer->ref();
312 draw->fPrimitiveType = type;
313 draw->fStartIndex = 0;
314 draw->fStartVertex = startVertex;
315 draw->fVertexCount = 0;
316 draw->fVertexLayout = geomSrc.fVertexLayout;
317 instancesToConcat = maxInstancesPerDraw;
318 }
319 draw->fVertexCount += instancesToConcat * verticesPerInstance;
320 draw->fIndexCount += instancesToConcat * indicesPerInstance;
321 instanceCount -= instancesToConcat;
322 instancesToConcat = 0;
323 }
324
325 // update draw tracking for next draw
326 fCurrQuad = 0;
327 fInstancedDrawTracker.fVerticesPerInstance = verticesPerInstance;
328 fInstancedDrawTracker.fIndicesPerInstance = indicesPerInstance;
329 } else {
330 this->INHERITED::drawIndexedInstances(type,
331 instanceCount,
332 verticesPerInstance,
333 indicesPerInstance);
334 }
335
336}
337
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000338void GrInOrderDrawBuffer::onDrawIndexed(GrPrimitiveType primitiveType,
339 int startVertex,
340 int startIndex,
341 int vertexCount,
342 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000343
344 if (!vertexCount || !indexCount) {
345 return;
346 }
347
bsalomon@google.com934c5702012-03-20 21:17:58 +0000348 this->resetDrawTracking();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000349
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000350 GeometryPoolState& poolState = fGeoPoolStateStack.back();
351
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000352 if (this->needsNewClip()) {
353 this->recordClip();
354 }
355 if (this->needsNewState()) {
356 this->recordState();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000357 }
358
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000359 Draw* draw = this->recordDraw();
reed@google.comac10a2d2010-12-22 21:39:39 +0000360
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000361 draw->fPrimitiveType = primitiveType;
362 draw->fStartVertex = startVertex;
363 draw->fStartIndex = startIndex;
364 draw->fVertexCount = vertexCount;
365 draw->fIndexCount = indexCount;
366
367 draw->fVertexLayout = this->getVertexLayout();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000368 switch (this->getGeomSrc().fVertexSrc) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000369 case kBuffer_GeometrySrcType:
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000370 draw->fVertexBuffer = this->getGeomSrc().fVertexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000371 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000372 case kReserved_GeometrySrcType: // fallthrough
373 case kArray_GeometrySrcType: {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000374 size_t vertexBytes = (vertexCount + startVertex) *
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000375 VertexSize(draw->fVertexLayout);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000376 poolState.fUsedPoolVertexBytes =
377 GrMax(poolState.fUsedPoolVertexBytes, vertexBytes);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000378 draw->fVertexBuffer = poolState.fPoolVertexBuffer;
379 draw->fStartVertex += poolState.fPoolStartVertex;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000380 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000381 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000382 default:
383 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000384 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000385 draw->fVertexBuffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000386
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000387 switch (this->getGeomSrc().fIndexSrc) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000388 case kBuffer_GeometrySrcType:
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000389 draw->fIndexBuffer = this->getGeomSrc().fIndexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000390 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000391 case kReserved_GeometrySrcType: // fallthrough
392 case kArray_GeometrySrcType: {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000393 size_t indexBytes = (indexCount + startIndex) * sizeof(uint16_t);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000394 poolState.fUsedPoolIndexBytes =
395 GrMax(poolState.fUsedPoolIndexBytes, indexBytes);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000396 draw->fIndexBuffer = poolState.fPoolIndexBuffer;
397 draw->fStartIndex += poolState.fPoolStartIndex;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000398 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000399 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000400 default:
401 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000402 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000403 draw->fIndexBuffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000404}
405
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000406void GrInOrderDrawBuffer::onDrawNonIndexed(GrPrimitiveType primitiveType,
407 int startVertex,
408 int vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000409 if (!vertexCount) {
410 return;
411 }
412
bsalomon@google.com934c5702012-03-20 21:17:58 +0000413 this->resetDrawTracking();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000414
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000415 GeometryPoolState& poolState = fGeoPoolStateStack.back();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000416 if (this->needsNewClip()) {
417 this->recordClip();
418 }
419 if (this->needsNewState()) {
420 this->recordState();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000421 }
422
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000423 Draw* draw = this->recordDraw();
424 draw->fPrimitiveType = primitiveType;
425 draw->fStartVertex = startVertex;
426 draw->fStartIndex = 0;
427 draw->fVertexCount = vertexCount;
428 draw->fIndexCount = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000429
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000430 draw->fVertexLayout = this->getVertexLayout();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000431 switch (this->getGeomSrc().fVertexSrc) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000432 case kBuffer_GeometrySrcType:
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000433 draw->fVertexBuffer = this->getGeomSrc().fVertexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000434 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000435 case kReserved_GeometrySrcType: // fallthrough
436 case kArray_GeometrySrcType: {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000437 size_t vertexBytes = (vertexCount + startVertex) *
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000438 VertexSize(draw->fVertexLayout);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000439 poolState.fUsedPoolVertexBytes =
440 GrMax(poolState.fUsedPoolVertexBytes, vertexBytes);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000441 draw->fVertexBuffer = poolState.fPoolVertexBuffer;
442 draw->fStartVertex += poolState.fPoolStartVertex;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000443 break;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000444 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000445 default:
446 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000447 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000448 draw->fVertexBuffer->ref();
449 draw->fIndexBuffer = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000450}
451
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000452void GrInOrderDrawBuffer::onStencilPath(const GrPath* path, GrPathFill fill) {
453 if (this->needsNewClip()) {
454 this->recordClip();
455 }
456 // Only compare the subset of GrDrawState relevant to path stenciling?
457 if (this->needsNewState()) {
458 this->recordState();
459 }
460 StencilPath* sp = this->recordStencilPath();
461 sp->fPath.reset(path);
462 path->ref();
463 sp->fFill = fill;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000464}
465
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000466void GrInOrderDrawBuffer::clear(const GrIRect* rect,
467 GrColor color,
468 GrRenderTarget* renderTarget) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000469 GrIRect r;
470 if (NULL == rect) {
471 // We could do something smart and remove previous draws and clears to
472 // the current render target. If we get that smart we have to make sure
473 // those draws aren't read before this clear (render-to-texture).
474 r.setLTRB(0, 0,
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000475 this->getDrawState().getRenderTarget()->width(),
476 this->getDrawState().getRenderTarget()->height());
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000477 rect = &r;
478 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000479 Clear* clr = this->recordClear();
480 clr->fColor = color;
481 clr->fRect = *rect;
482 clr->fRenderTarget = renderTarget;
483 GrSafeRef(clr->fRenderTarget);
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000484}
485
reed@google.comac10a2d2010-12-22 21:39:39 +0000486void GrInOrderDrawBuffer::reset() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000487 GrAssert(1 == fGeoPoolStateStack.count());
488 this->resetVertexSource();
489 this->resetIndexSource();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000490 int numDraws = fDraws.count();
491 for (int d = 0; d < numDraws; ++d) {
492 // we always have a VB, but not always an IB
493 GrAssert(NULL != fDraws[d].fVertexBuffer);
494 fDraws[d].fVertexBuffer->unref();
495 GrSafeUnref(fDraws[d].fIndexBuffer);
496 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000497 fCmds.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000498 fDraws.reset();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000499 fStencilPaths.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000500 fStates.reset();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000501 fClears.reset();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000502 fVertexPool.reset();
503 fIndexPool.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000504 fClips.reset();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000505 fClipOrigins.reset();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000506 fClipSet = true;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000507
bsalomon@google.com934c5702012-03-20 21:17:58 +0000508 this->resetDrawTracking();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000509
510 // we start off with a default clip and state so that we don't have
511 // to do count checks on fClips, fStates, or fCmds before checking their
512 // last entry.
513 this->recordDefaultState();
514 this->recordDefaultClip();
reed@google.comac10a2d2010-12-22 21:39:39 +0000515}
516
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000517bool GrInOrderDrawBuffer::playback(GrDrawTarget* target) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000518 GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fVertexSrc);
519 GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000520
reed@google.comac10a2d2010-12-22 21:39:39 +0000521 GrAssert(NULL != target);
522 GrAssert(target != this); // not considered and why?
523
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000524 int numCmds = fCmds.count();
525 GrAssert(numCmds >= 2);
526 if (2 == numCmds) {
527 GrAssert(kSetState_Cmd == fCmds[0]);
528 GrAssert(kSetClip_Cmd == fCmds[1]);
529 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000530 }
531
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000532 fVertexPool.unlock();
533 fIndexPool.unlock();
reed@google.comac10a2d2010-12-22 21:39:39 +0000534
reed@google.comac10a2d2010-12-22 21:39:39 +0000535 GrDrawTarget::AutoClipRestore acr(target);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000536 AutoGeometryPush agp(target);
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000537 GrDrawState* prevDrawState = target->drawState();
538 prevDrawState->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000539
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000540 GrClipData clipData;
541
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000542 int currState = 0;
543 int currClip = 0;
544 int currClear = 0;
545 int currDraw = 0;
546 int currStencilPath = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000547
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000548 for (int c = 0; c < numCmds; ++c) {
549 switch (fCmds[c]) {
550 case kDraw_Cmd: {
551 const Draw& draw = fDraws[currDraw];
552 target->setVertexSourceToBuffer(draw.fVertexLayout, draw.fVertexBuffer);
553 if (draw.fIndexCount) {
554 target->setIndexSourceToBuffer(draw.fIndexBuffer);
555 }
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000556
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000557 if (draw.fIndexCount) {
558 target->drawIndexed(draw.fPrimitiveType,
559 draw.fStartVertex,
560 draw.fStartIndex,
561 draw.fVertexCount,
562 draw.fIndexCount);
563 } else {
564 target->drawNonIndexed(draw.fPrimitiveType,
565 draw.fStartVertex,
566 draw.fVertexCount);
567 }
568 ++currDraw;
569 break;
570 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000571 case kStencilPath_Cmd: {
572 const StencilPath& sp = fStencilPaths[currStencilPath];
573 target->stencilPath(sp.fPath.get(), sp.fFill);
574 ++currStencilPath;
575 break;
576 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000577 case kSetState_Cmd:
578 target->setDrawState(&fStates[currState]);
579 ++currState;
580 break;
581 case kSetClip_Cmd:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000582 clipData.fClipStack = &fClips[currClip];
583 clipData.fOrigin = fClipOrigins[currClip];
584 target->setClip(&clipData);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000585 ++currClip;
586 break;
587 case kClear_Cmd:
588 target->clear(&fClears[currClear].fRect,
589 fClears[currClear].fColor,
590 fClears[currClear].fRenderTarget);
591 ++currClear;
592 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000593 }
594 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000595 // we should have consumed all the states, clips, etc.
596 GrAssert(fStates.count() == currState);
597 GrAssert(fClips.count() == currClip);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000598 GrAssert(fClipOrigins.count() == currClip);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000599 GrAssert(fClears.count() == currClear);
600 GrAssert(fDraws.count() == currDraw);
601
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000602 target->setDrawState(prevDrawState);
603 prevDrawState->unref();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000604 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000605}
606
bsalomon@google.com97805382012-03-13 14:32:07 +0000607void GrInOrderDrawBuffer::setAutoFlushTarget(GrDrawTarget* target) {
608 GrSafeAssign(fAutoFlushTarget, target);
609}
610
611void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace(
612 GrVertexLayout vertexLayout,
613 int vertexCount,
614 int indexCount) {
615 if (NULL != fAutoFlushTarget) {
616 // We use geometryHints() to know whether to flush the draw buffer. We
617 // can't flush if we are inside an unbalanced pushGeometrySource.
618 // Moreover, flushing blows away vertex and index data that was
619 // previously reserved. So if the vertex or index data is pulled from
620 // reserved space and won't be released by this request then we can't
621 // flush.
622 bool insideGeoPush = fGeoPoolStateStack.count() > 1;
623
624 bool unreleasedVertexSpace =
625 !vertexCount &&
626 kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc;
627
628 bool unreleasedIndexSpace =
629 !indexCount &&
630 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
631
632 // we don't want to finalize any reserved geom on the target since
633 // we don't know that the client has finished writing to it.
634 bool targetHasReservedGeom =
635 fAutoFlushTarget->hasReservedVerticesOrIndices();
636
637 int vcount = vertexCount;
638 int icount = indexCount;
639
640 if (!insideGeoPush &&
641 !unreleasedVertexSpace &&
642 !unreleasedIndexSpace &&
643 !targetHasReservedGeom &&
644 this->geometryHints(vertexLayout, &vcount, &icount)) {
645
646 this->flushTo(fAutoFlushTarget);
647 }
648 }
649}
650
reed@google.comac10a2d2010-12-22 21:39:39 +0000651bool GrInOrderDrawBuffer::geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000652 int* vertexCount,
653 int* indexCount) const {
654 // we will recommend a flush if the data could fit in a single
655 // preallocated buffer but none are left and it can't fit
656 // in the current buffer (which may not be prealloced).
reed@google.comac10a2d2010-12-22 21:39:39 +0000657 bool flush = false;
658 if (NULL != indexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000659 int32_t currIndices = fIndexPool.currentBufferIndices();
660 if (*indexCount > currIndices &&
661 (!fIndexPool.preallocatedBuffersRemaining() &&
662 *indexCount <= fIndexPool.preallocatedBufferIndices())) {
663
664 flush = true;
665 }
666 *indexCount = currIndices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000667 }
668 if (NULL != vertexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000669 int32_t currVertices = fVertexPool.currentBufferVertices(vertexLayout);
670 if (*vertexCount > currVertices &&
671 (!fVertexPool.preallocatedBuffersRemaining() &&
672 *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexLayout))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000673
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000674 flush = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000675 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000676 *vertexCount = currVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000677 }
678 return flush;
679}
680
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000681bool GrInOrderDrawBuffer::onReserveVertexSpace(GrVertexLayout vertexLayout,
682 int vertexCount,
683 void** vertices) {
684 GeometryPoolState& poolState = fGeoPoolStateStack.back();
685 GrAssert(vertexCount > 0);
686 GrAssert(NULL != vertices);
687 GrAssert(0 == poolState.fUsedPoolVertexBytes);
688
689 *vertices = fVertexPool.makeSpace(vertexLayout,
690 vertexCount,
691 &poolState.fPoolVertexBuffer,
692 &poolState.fPoolStartVertex);
693 return NULL != *vertices;
694}
695
696bool GrInOrderDrawBuffer::onReserveIndexSpace(int indexCount, void** indices) {
697 GeometryPoolState& poolState = fGeoPoolStateStack.back();
698 GrAssert(indexCount > 0);
699 GrAssert(NULL != indices);
700 GrAssert(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000701
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000702 *indices = fIndexPool.makeSpace(indexCount,
703 &poolState.fPoolIndexBuffer,
704 &poolState.fPoolStartIndex);
705 return NULL != *indices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000706}
707
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000708void GrInOrderDrawBuffer::releaseReservedVertexSpace() {
709 GeometryPoolState& poolState = fGeoPoolStateStack.back();
710 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000711
712 // If we get a release vertex space call then our current source should either be reserved
713 // or array (which we copied into reserved space).
714 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc ||
715 kArray_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000716
717 // When the caller reserved vertex buffer space we gave it back a pointer
718 // provided by the vertex buffer pool. At each draw we tracked the largest
719 // offset into the pool's pointer that was referenced. Now we return to the
720 // pool any portion at the tail of the allocation that no draw referenced.
721 size_t reservedVertexBytes = VertexSize(geoSrc.fVertexLayout) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000722 geoSrc.fVertexCount;
723 fVertexPool.putBack(reservedVertexBytes -
724 poolState.fUsedPoolVertexBytes);
725 poolState.fUsedPoolVertexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000726 poolState.fPoolVertexBuffer = NULL;
727 poolState.fPoolStartVertex = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000728}
729
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000730void GrInOrderDrawBuffer::releaseReservedIndexSpace() {
731 GeometryPoolState& poolState = fGeoPoolStateStack.back();
732 const GeometrySrcState& geoSrc = this->getGeomSrc();
733
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000734 // If we get a release index space call then our current source should either be reserved
735 // or array (which we copied into reserved space).
736 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc ||
737 kArray_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000738
739 // Similar to releaseReservedVertexSpace we return any unused portion at
740 // the tail
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000741 size_t reservedIndexBytes = sizeof(uint16_t) * geoSrc.fIndexCount;
742 fIndexPool.putBack(reservedIndexBytes - poolState.fUsedPoolIndexBytes);
743 poolState.fUsedPoolIndexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000744 poolState.fPoolIndexBuffer = NULL;
745 poolState.fPoolStartIndex = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000746}
747
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000748void GrInOrderDrawBuffer::onSetVertexSourceToArray(const void* vertexArray,
749 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000750
751 GeometryPoolState& poolState = fGeoPoolStateStack.back();
752 GrAssert(0 == poolState.fUsedPoolVertexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000753#if GR_DEBUG
754 bool success =
755#endif
bsalomon@google.come79c8152012-03-29 19:07:12 +0000756 fVertexPool.appendVertices(this->getVertexLayout(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000757 vertexCount,
758 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000759 &poolState.fPoolVertexBuffer,
760 &poolState.fPoolStartVertex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000761 GR_DEBUGASSERT(success);
762}
763
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000764void GrInOrderDrawBuffer::onSetIndexSourceToArray(const void* indexArray,
765 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000766 GeometryPoolState& poolState = fGeoPoolStateStack.back();
767 GrAssert(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000768#if GR_DEBUG
769 bool success =
770#endif
771 fIndexPool.appendIndices(indexCount,
772 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000773 &poolState.fPoolIndexBuffer,
774 &poolState.fPoolStartIndex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000775 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000776}
777
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000778void GrInOrderDrawBuffer::releaseVertexArray() {
779 // When the client provides an array as the vertex source we handled it
780 // by copying their array into reserved space.
781 this->GrInOrderDrawBuffer::releaseReservedVertexSpace();
782}
783
784void GrInOrderDrawBuffer::releaseIndexArray() {
785 // When the client provides an array as the index source we handled it
786 // by copying their array into reserved space.
787 this->GrInOrderDrawBuffer::releaseReservedIndexSpace();
788}
789
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000790void GrInOrderDrawBuffer::geometrySourceWillPush() {
791 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
792 poolState.fUsedPoolVertexBytes = 0;
793 poolState.fUsedPoolIndexBytes = 0;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000794 this->resetDrawTracking();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000795#if GR_DEBUG
796 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
797 poolState.fPoolStartVertex = ~0;
798 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
799 poolState.fPoolStartIndex = ~0;
800#endif
801}
802
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000803void GrInOrderDrawBuffer::geometrySourceWillPop(
804 const GeometrySrcState& restoredState) {
805 GrAssert(fGeoPoolStateStack.count() > 1);
806 fGeoPoolStateStack.pop_back();
807 GeometryPoolState& poolState = fGeoPoolStateStack.back();
808 // we have to assume that any slack we had in our vertex/index data
809 // is now unreleasable because data may have been appended later in the
810 // pool.
811 if (kReserved_GeometrySrcType == restoredState.fVertexSrc ||
812 kArray_GeometrySrcType == restoredState.fVertexSrc) {
813 poolState.fUsedPoolVertexBytes =
814 VertexSize(restoredState.fVertexLayout) *
815 restoredState.fVertexCount;
816 }
817 if (kReserved_GeometrySrcType == restoredState.fIndexSrc ||
818 kArray_GeometrySrcType == restoredState.fIndexSrc) {
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000819 poolState.fUsedPoolIndexBytes = sizeof(uint16_t) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000820 restoredState.fIndexCount;
821 }
bsalomon@google.com934c5702012-03-20 21:17:58 +0000822 this->resetDrawTracking();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000823}
824
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000825bool GrInOrderDrawBuffer::needsNewState() const {
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000826 // we should have recorded a default state in reset()
827 GrAssert(!fStates.empty());
828 return fStates.back() != this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +0000829}
830
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000831bool GrInOrderDrawBuffer::needsNewClip() const {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000832 if (this->getDrawState().isClipState()) {
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000833 if (fClipSet &&
834 (fClips.back() != *fClip->fClipStack ||
835 fClipOrigins.back() != fClip->fOrigin)) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000836 return true;
837 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000838 }
839 return false;
840}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000841
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000842void GrInOrderDrawBuffer::recordClip() {
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000843 fClips.push_back() = *fClip->fClipStack;
844 fClipOrigins.push_back() = fClip->fOrigin;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000845 fClipSet = false;
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000846 fCmds.push_back(kSetClip_Cmd);
847}
848
849void GrInOrderDrawBuffer::recordDefaultClip() {
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000850 fClips.push_back() = SkClipStack();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000851 fClipOrigins.push_back() = SkIPoint::Make(0, 0);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000852 fCmds.push_back(kSetClip_Cmd);
853}
854
855void GrInOrderDrawBuffer::recordState() {
856 fStates.push_back(this->getDrawState());
857 fCmds.push_back(kSetState_Cmd);
858}
859
860void GrInOrderDrawBuffer::recordDefaultState() {
861 fStates.push_back(GrDrawState());
862 fCmds.push_back(kSetState_Cmd);
863}
864
865GrInOrderDrawBuffer::Draw* GrInOrderDrawBuffer::recordDraw() {
866 fCmds.push_back(kDraw_Cmd);
867 return &fDraws.push_back();
868}
869
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000870GrInOrderDrawBuffer::StencilPath* GrInOrderDrawBuffer::recordStencilPath() {
871 fCmds.push_back(kStencilPath_Cmd);
872 return &fStencilPaths.push_back();
873}
874
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000875GrInOrderDrawBuffer::Clear* GrInOrderDrawBuffer::recordClear() {
876 fCmds.push_back(kClear_Cmd);
877 return &fClears.push_back();
reed@google.comac10a2d2010-12-22 21:39:39 +0000878}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000879
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000880void GrInOrderDrawBuffer::clipWillBeSet(const GrClipData* newClipData) {
881 INHERITED::clipWillBeSet(newClipData);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000882 fClipSet = true;
883}