blob: 197cf0bac2809b72042eb445519ae9a3805d0bff [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) {
jvanverth@google.comcc782382013-01-28 20:39:48 +0000121 layout |= GrDrawState::kColor_VertexLayoutBit;
robertphillips@google.comf04c40e2012-10-09 17:40:32 +0000122 }
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;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000170 const GrClipData* clip = this->getClip();
171 clip->fClipStack->getConservativeBounds(-clip->fOrigin.fX,
172 -clip->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 }
jvanverth@google.comcc782382013-01-28 20:39:48 +0000196 int stride = GrDrawState::VertexSize(layout);
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000197 bool insideClip = true;
198 for (int v = 0; v < 4; ++v) {
jvanverth@google.comcc782382013-01-28 20:39:48 +0000199 const GrPoint& p = *GrDrawState::GetVertexPoint(geo.vertices(), v, stride);
robertphillips@google.com641f8b12012-07-31 19:15:58 +0000200 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
jvanverth@google.comcc782382013-01-28 20:39:48 +0000218 int vsize = GrDrawState::VertexSize(layout);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000219
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000220 DrawRecord& lastDraw = fDraws.back();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000221
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
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000230 appendToPreviousDraw = kDraw_Cmd == fCmds.back() &&
231 lastDraw.fVertexBuffer == poolState.fPoolVertexBuffer &&
232 (fCurrQuad * 4 + lastDraw.fStartVertex) ==
233 poolState.fPoolStartVertex;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000234
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.com74749cd2013-01-30 16:12:41 +0000282 DrawRecord* draw = NULL;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000283 // 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 *
jvanverth@google.comcc782382013-01-28 20:39:48 +0000338 GrDrawState::VertexSize(draw->fVertexLayout);
bsalomon@google.com934c5702012-03-20 21:17:58 +0000339 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 }
bsalomon@google.com934c5702012-03-20 21:17:58 +0000374}
375
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000376void GrInOrderDrawBuffer::onDraw(const DrawInfo& info) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000377
bsalomon@google.com934c5702012-03-20 21:17:58 +0000378 this->resetDrawTracking();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000379
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000380 GeometryPoolState& poolState = fGeoPoolStateStack.back();
381
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000382 if (this->needsNewClip()) {
383 this->recordClip();
384 }
385 if (this->needsNewState()) {
386 this->recordState();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000387 }
388
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000389 DrawRecord* draw = this->recordDraw(info);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000390 draw->fVertexLayout = this->getVertexLayout();
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000391
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000392 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000393 case kBuffer_GeometrySrcType:
394 draw->fVertexBuffer = this->getGeomSrc().fVertexBuffer;
395 break;
396 case kReserved_GeometrySrcType: // fallthrough
397 case kArray_GeometrySrcType: {
398 size_t vertexBytes = (info.vertexCount() + info.startVertex()) *
399 GrDrawState::VertexSize(draw->fVertexLayout);
400 poolState.fUsedPoolVertexBytes = GrMax(poolState.fUsedPoolVertexBytes, vertexBytes);
401 draw->fVertexBuffer = poolState.fPoolVertexBuffer;
402 draw->fStartVertex += poolState.fPoolStartVertex;
403 break;
404 }
405 default:
406 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000407 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000408 draw->fVertexBuffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000409
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000410 if (info.isIndexed()) {
411 switch (this->getGeomSrc().fIndexSrc) {
412 case kBuffer_GeometrySrcType:
413 draw->fIndexBuffer = this->getGeomSrc().fIndexBuffer;
414 break;
415 case kReserved_GeometrySrcType: // fallthrough
416 case kArray_GeometrySrcType: {
417 size_t indexBytes = (info.indexCount() + info.startIndex()) * sizeof(uint16_t);
418 poolState.fUsedPoolIndexBytes = GrMax(poolState.fUsedPoolIndexBytes, indexBytes);
419 draw->fIndexBuffer = poolState.fPoolIndexBuffer;
420 draw->fStartIndex += poolState.fPoolStartIndex;
421 break;
422 }
423 default:
424 GrCrash("unknown geom src type");
425 }
426 draw->fIndexBuffer->ref();
427 } else {
428 draw->fIndexBuffer = NULL;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000429 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000430}
431
reed@google.comac10a2d2010-12-22 21:39:39 +0000432
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000433GrInOrderDrawBuffer::StencilPath::StencilPath() : fStroke(SkStrokeRec::kFill_InitStyle) {}
434
435void GrInOrderDrawBuffer::onStencilPath(const GrPath* path, const SkStrokeRec& stroke,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000436 SkPath::FillType fill) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000437 if (this->needsNewClip()) {
438 this->recordClip();
439 }
440 // Only compare the subset of GrDrawState relevant to path stenciling?
441 if (this->needsNewState()) {
442 this->recordState();
443 }
444 StencilPath* sp = this->recordStencilPath();
445 sp->fPath.reset(path);
446 path->ref();
447 sp->fFill = fill;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000448 sp->fStroke = stroke;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000449}
450
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000451void GrInOrderDrawBuffer::clear(const GrIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000452 GrColor color,
453 GrRenderTarget* renderTarget) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000454 GrIRect r;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000455 if (NULL == renderTarget) {
456 renderTarget = this->drawState()->getRenderTarget();
457 GrAssert(NULL != renderTarget);
458 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000459 if (NULL == rect) {
460 // We could do something smart and remove previous draws and clears to
461 // the current render target. If we get that smart we have to make sure
462 // those draws aren't read before this clear (render-to-texture).
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000463 r.setLTRB(0, 0, renderTarget->width(), renderTarget->height());
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000464 rect = &r;
465 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000466 Clear* clr = this->recordClear();
467 clr->fColor = color;
468 clr->fRect = *rect;
469 clr->fRenderTarget = renderTarget;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000470 renderTarget->ref();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000471}
472
reed@google.comac10a2d2010-12-22 21:39:39 +0000473void GrInOrderDrawBuffer::reset() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000474 GrAssert(1 == fGeoPoolStateStack.count());
475 this->resetVertexSource();
476 this->resetIndexSource();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000477 int numDraws = fDraws.count();
478 for (int d = 0; d < numDraws; ++d) {
479 // we always have a VB, but not always an IB
480 GrAssert(NULL != fDraws[d].fVertexBuffer);
481 fDraws[d].fVertexBuffer->unref();
482 GrSafeUnref(fDraws[d].fIndexBuffer);
483 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000484 fCmds.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000485 fDraws.reset();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000486 fStencilPaths.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000487 fStates.reset();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000488 fClears.reset();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000489 fVertexPool.reset();
490 fIndexPool.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000491 fClips.reset();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000492 fClipOrigins.reset();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000493 fClipSet = true;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000494
bsalomon@google.com934c5702012-03-20 21:17:58 +0000495 this->resetDrawTracking();
reed@google.comac10a2d2010-12-22 21:39:39 +0000496}
497
bsalomon@google.com55e4a202013-01-11 13:54:21 +0000498bool GrInOrderDrawBuffer::flushTo(GrDrawTarget* target) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000499 GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fVertexSrc);
500 GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000501
reed@google.comac10a2d2010-12-22 21:39:39 +0000502 GrAssert(NULL != target);
503 GrAssert(target != this); // not considered and why?
504
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000505 int numCmds = fCmds.count();
bsalomon@google.com358e4272013-01-10 14:40:28 +0000506 if (0 == numCmds) {
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000507 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000508 }
509
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000510 fVertexPool.unlock();
511 fIndexPool.unlock();
reed@google.comac10a2d2010-12-22 21:39:39 +0000512
reed@google.comac10a2d2010-12-22 21:39:39 +0000513 GrDrawTarget::AutoClipRestore acr(target);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000514 AutoGeometryPush agp(target);
bsalomon@google.comca432082013-01-23 19:53:46 +0000515
516 GrDrawState playbackState;
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000517 GrDrawState* prevDrawState = target->drawState();
518 prevDrawState->ref();
bsalomon@google.comca432082013-01-23 19:53:46 +0000519 target->setDrawState(&playbackState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000520
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000521 GrClipData clipData;
522
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000523 int currState = 0;
524 int currClip = 0;
525 int currClear = 0;
526 int currDraw = 0;
527 int currStencilPath = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000528
bsalomon@google.comca432082013-01-23 19:53:46 +0000529
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000530 for (int c = 0; c < numCmds; ++c) {
531 switch (fCmds[c]) {
532 case kDraw_Cmd: {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000533 const DrawRecord& draw = fDraws[currDraw];
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000534 target->setVertexSourceToBuffer(draw.fVertexLayout, draw.fVertexBuffer);
535 if (draw.fIndexCount) {
536 target->setIndexSourceToBuffer(draw.fIndexBuffer);
537 }
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000538
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000539 if (draw.fIndexCount) {
540 target->drawIndexed(draw.fPrimitiveType,
541 draw.fStartVertex,
542 draw.fStartIndex,
543 draw.fVertexCount,
544 draw.fIndexCount);
545 } else {
546 target->drawNonIndexed(draw.fPrimitiveType,
547 draw.fStartVertex,
548 draw.fVertexCount);
549 }
550 ++currDraw;
551 break;
552 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000553 case kStencilPath_Cmd: {
554 const StencilPath& sp = fStencilPaths[currStencilPath];
sugoi@google.com12b4e272012-12-06 20:13:11 +0000555 target->stencilPath(sp.fPath.get(), sp.fStroke, sp.fFill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000556 ++currStencilPath;
557 break;
558 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000559 case kSetState_Cmd:
bsalomon@google.comca432082013-01-23 19:53:46 +0000560 fStates[currState].restoreTo(&playbackState);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000561 ++currState;
562 break;
563 case kSetClip_Cmd:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000564 clipData.fClipStack = &fClips[currClip];
565 clipData.fOrigin = fClipOrigins[currClip];
566 target->setClip(&clipData);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000567 ++currClip;
568 break;
569 case kClear_Cmd:
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000570 target->clear(&fClears[currClear].fRect,
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000571 fClears[currClear].fColor,
572 fClears[currClear].fRenderTarget);
573 ++currClear;
574 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000575 }
576 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000577 // we should have consumed all the states, clips, etc.
578 GrAssert(fStates.count() == currState);
579 GrAssert(fClips.count() == currClip);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000580 GrAssert(fClipOrigins.count() == currClip);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000581 GrAssert(fClears.count() == currClear);
582 GrAssert(fDraws.count() == currDraw);
583
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000584 target->setDrawState(prevDrawState);
585 prevDrawState->unref();
bsalomon@google.com55e4a202013-01-11 13:54:21 +0000586 this->reset();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000587 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000588}
589
bsalomon@google.com97805382012-03-13 14:32:07 +0000590void GrInOrderDrawBuffer::setAutoFlushTarget(GrDrawTarget* target) {
591 GrSafeAssign(fAutoFlushTarget, target);
592}
593
594void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace(
595 GrVertexLayout vertexLayout,
596 int vertexCount,
597 int indexCount) {
598 if (NULL != fAutoFlushTarget) {
599 // We use geometryHints() to know whether to flush the draw buffer. We
600 // can't flush if we are inside an unbalanced pushGeometrySource.
601 // Moreover, flushing blows away vertex and index data that was
602 // previously reserved. So if the vertex or index data is pulled from
603 // reserved space and won't be released by this request then we can't
604 // flush.
605 bool insideGeoPush = fGeoPoolStateStack.count() > 1;
606
607 bool unreleasedVertexSpace =
608 !vertexCount &&
609 kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc;
610
611 bool unreleasedIndexSpace =
612 !indexCount &&
613 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
614
615 // we don't want to finalize any reserved geom on the target since
616 // we don't know that the client has finished writing to it.
617 bool targetHasReservedGeom =
618 fAutoFlushTarget->hasReservedVerticesOrIndices();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000619
bsalomon@google.com97805382012-03-13 14:32:07 +0000620 int vcount = vertexCount;
621 int icount = indexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000622
bsalomon@google.com97805382012-03-13 14:32:07 +0000623 if (!insideGeoPush &&
624 !unreleasedVertexSpace &&
625 !unreleasedIndexSpace &&
626 !targetHasReservedGeom &&
627 this->geometryHints(vertexLayout, &vcount, &icount)) {
628
629 this->flushTo(fAutoFlushTarget);
630 }
631 }
632}
633
reed@google.comac10a2d2010-12-22 21:39:39 +0000634bool GrInOrderDrawBuffer::geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000635 int* vertexCount,
636 int* indexCount) const {
637 // we will recommend a flush if the data could fit in a single
638 // preallocated buffer but none are left and it can't fit
639 // in the current buffer (which may not be prealloced).
reed@google.comac10a2d2010-12-22 21:39:39 +0000640 bool flush = false;
641 if (NULL != indexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000642 int32_t currIndices = fIndexPool.currentBufferIndices();
643 if (*indexCount > currIndices &&
644 (!fIndexPool.preallocatedBuffersRemaining() &&
645 *indexCount <= fIndexPool.preallocatedBufferIndices())) {
646
647 flush = true;
648 }
649 *indexCount = currIndices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000650 }
651 if (NULL != vertexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000652 int32_t currVertices = fVertexPool.currentBufferVertices(vertexLayout);
653 if (*vertexCount > currVertices &&
654 (!fVertexPool.preallocatedBuffersRemaining() &&
655 *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexLayout))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000656
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000657 flush = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000658 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000659 *vertexCount = currVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000660 }
661 return flush;
662}
663
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000664bool GrInOrderDrawBuffer::onReserveVertexSpace(GrVertexLayout vertexLayout,
665 int vertexCount,
666 void** vertices) {
667 GeometryPoolState& poolState = fGeoPoolStateStack.back();
668 GrAssert(vertexCount > 0);
669 GrAssert(NULL != vertices);
670 GrAssert(0 == poolState.fUsedPoolVertexBytes);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000671
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000672 *vertices = fVertexPool.makeSpace(vertexLayout,
673 vertexCount,
674 &poolState.fPoolVertexBuffer,
675 &poolState.fPoolStartVertex);
676 return NULL != *vertices;
677}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000678
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000679bool GrInOrderDrawBuffer::onReserveIndexSpace(int indexCount, void** indices) {
680 GeometryPoolState& poolState = fGeoPoolStateStack.back();
681 GrAssert(indexCount > 0);
682 GrAssert(NULL != indices);
683 GrAssert(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000684
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000685 *indices = fIndexPool.makeSpace(indexCount,
686 &poolState.fPoolIndexBuffer,
687 &poolState.fPoolStartIndex);
688 return NULL != *indices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000689}
690
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000691void GrInOrderDrawBuffer::releaseReservedVertexSpace() {
692 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000693 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000694
695 // If we get a release vertex space call then our current source should either be reserved
696 // or array (which we copied into reserved space).
697 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc ||
698 kArray_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000699
700 // When the caller reserved vertex buffer space we gave it back a pointer
701 // provided by the vertex buffer pool. At each draw we tracked the largest
702 // offset into the pool's pointer that was referenced. Now we return to the
703 // pool any portion at the tail of the allocation that no draw referenced.
jvanverth@google.comcc782382013-01-28 20:39:48 +0000704 size_t reservedVertexBytes = GrDrawState::VertexSize(geoSrc.fVertexLayout) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000705 geoSrc.fVertexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000706 fVertexPool.putBack(reservedVertexBytes -
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000707 poolState.fUsedPoolVertexBytes);
708 poolState.fUsedPoolVertexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000709 poolState.fPoolVertexBuffer = NULL;
710 poolState.fPoolStartVertex = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000711}
712
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000713void GrInOrderDrawBuffer::releaseReservedIndexSpace() {
714 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000715 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000716
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000717 // If we get a release index space call then our current source should either be reserved
718 // or array (which we copied into reserved space).
719 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc ||
720 kArray_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000721
722 // Similar to releaseReservedVertexSpace we return any unused portion at
723 // the tail
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000724 size_t reservedIndexBytes = sizeof(uint16_t) * geoSrc.fIndexCount;
725 fIndexPool.putBack(reservedIndexBytes - poolState.fUsedPoolIndexBytes);
726 poolState.fUsedPoolIndexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000727 poolState.fPoolIndexBuffer = NULL;
728 poolState.fPoolStartIndex = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000729}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000730
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000731void GrInOrderDrawBuffer::onSetVertexSourceToArray(const void* vertexArray,
732 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000733
734 GeometryPoolState& poolState = fGeoPoolStateStack.back();
735 GrAssert(0 == poolState.fUsedPoolVertexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000736#if GR_DEBUG
737 bool success =
738#endif
bsalomon@google.come79c8152012-03-29 19:07:12 +0000739 fVertexPool.appendVertices(this->getVertexLayout(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000740 vertexCount,
741 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000742 &poolState.fPoolVertexBuffer,
743 &poolState.fPoolStartVertex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000744 GR_DEBUGASSERT(success);
745}
746
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000747void GrInOrderDrawBuffer::onSetIndexSourceToArray(const void* indexArray,
748 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000749 GeometryPoolState& poolState = fGeoPoolStateStack.back();
750 GrAssert(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000751#if GR_DEBUG
752 bool success =
753#endif
754 fIndexPool.appendIndices(indexCount,
755 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000756 &poolState.fPoolIndexBuffer,
757 &poolState.fPoolStartIndex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000758 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000759}
760
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000761void GrInOrderDrawBuffer::releaseVertexArray() {
762 // When the client provides an array as the vertex source we handled it
763 // by copying their array into reserved space.
764 this->GrInOrderDrawBuffer::releaseReservedVertexSpace();
765}
766
767void GrInOrderDrawBuffer::releaseIndexArray() {
768 // When the client provides an array as the index source we handled it
769 // by copying their array into reserved space.
770 this->GrInOrderDrawBuffer::releaseReservedIndexSpace();
771}
772
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000773void GrInOrderDrawBuffer::geometrySourceWillPush() {
774 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
775 poolState.fUsedPoolVertexBytes = 0;
776 poolState.fUsedPoolIndexBytes = 0;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000777 this->resetDrawTracking();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000778#if GR_DEBUG
779 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
780 poolState.fPoolStartVertex = ~0;
781 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
782 poolState.fPoolStartIndex = ~0;
783#endif
784}
785
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000786void GrInOrderDrawBuffer::geometrySourceWillPop(
787 const GeometrySrcState& restoredState) {
788 GrAssert(fGeoPoolStateStack.count() > 1);
789 fGeoPoolStateStack.pop_back();
790 GeometryPoolState& poolState = fGeoPoolStateStack.back();
791 // we have to assume that any slack we had in our vertex/index data
792 // is now unreleasable because data may have been appended later in the
793 // pool.
794 if (kReserved_GeometrySrcType == restoredState.fVertexSrc ||
795 kArray_GeometrySrcType == restoredState.fVertexSrc) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000796 poolState.fUsedPoolVertexBytes =
jvanverth@google.comcc782382013-01-28 20:39:48 +0000797 GrDrawState::VertexSize(restoredState.fVertexLayout) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000798 restoredState.fVertexCount;
799 }
800 if (kReserved_GeometrySrcType == restoredState.fIndexSrc ||
801 kArray_GeometrySrcType == restoredState.fIndexSrc) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000802 poolState.fUsedPoolIndexBytes = sizeof(uint16_t) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000803 restoredState.fIndexCount;
804 }
bsalomon@google.com934c5702012-03-20 21:17:58 +0000805 this->resetDrawTracking();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000806}
807
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000808bool GrInOrderDrawBuffer::needsNewState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000809 return fStates.empty() || !fStates.back().isEqual(this->getDrawState());
reed@google.comac10a2d2010-12-22 21:39:39 +0000810}
811
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000812bool GrInOrderDrawBuffer::needsNewClip() const {
bsalomon@google.com358e4272013-01-10 14:40:28 +0000813 GrAssert(fClips.count() == fClipOrigins.count());
814 if (this->getDrawState().isClipState()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000815 if (fClipSet &&
bsalomon@google.com358e4272013-01-10 14:40:28 +0000816 (fClips.empty() ||
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000817 fClips.back() != *this->getClip()->fClipStack ||
818 fClipOrigins.back() != this->getClip()->fOrigin)) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000819 return true;
820 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000821 }
822 return false;
823}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000824
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000825void GrInOrderDrawBuffer::recordClip() {
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000826 fClips.push_back() = *this->getClip()->fClipStack;
827 fClipOrigins.push_back() = this->getClip()->fOrigin;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000828 fClipSet = false;
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000829 fCmds.push_back(kSetClip_Cmd);
830}
831
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000832void GrInOrderDrawBuffer::recordState() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000833 fStates.push_back().saveFrom(this->getDrawState());
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000834 fCmds.push_back(kSetState_Cmd);
835}
836
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000837GrInOrderDrawBuffer::DrawRecord* GrInOrderDrawBuffer::recordDraw() {
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000838 fCmds.push_back(kDraw_Cmd);
839 return &fDraws.push_back();
840}
841
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000842GrInOrderDrawBuffer::DrawRecord* GrInOrderDrawBuffer::recordDraw(const DrawInfo& info) {
843 DrawRecord* record = this->recordDraw();
844 record->fPrimitiveType = info.primitiveType();
845 record->fStartVertex = info.startVertex();
846 record->fVertexCount = info.vertexCount();
847 record->fStartIndex = info.startIndex();
848 record->fIndexCount = info.indexCount();
849 return record;
850}
851
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000852GrInOrderDrawBuffer::StencilPath* GrInOrderDrawBuffer::recordStencilPath() {
853 fCmds.push_back(kStencilPath_Cmd);
854 return &fStencilPaths.push_back();
855}
856
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000857GrInOrderDrawBuffer::Clear* GrInOrderDrawBuffer::recordClear() {
858 fCmds.push_back(kClear_Cmd);
859 return &fClears.push_back();
reed@google.comac10a2d2010-12-22 21:39:39 +0000860}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000861
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000862void GrInOrderDrawBuffer::clipWillBeSet(const GrClipData* newClipData) {
863 INHERITED::clipWillBeSet(newClipData);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000864 fClipSet = true;
865}