blob: 8225425b4135a1ce1992602f10555b77c961c56b [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
bsalomon@google.com1da07462011-03-10 14:51:57 +00002 Copyright 2011 Google Inc.
reed@google.comac10a2d2010-12-22 21:39:39 +00003
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17
18#include "GrInOrderDrawBuffer.h"
19#include "GrTexture.h"
bsalomon@google.com1c13c962011-02-14 16:51:21 +000020#include "GrBufferAllocPool.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000021#include "GrIndexBuffer.h"
22#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000023#include "GrGpu.h"
24
bsalomon@google.com1c13c962011-02-14 16:51:21 +000025GrInOrderDrawBuffer::GrInOrderDrawBuffer(GrVertexBufferAllocPool* vertexPool,
26 GrIndexBufferAllocPool* indexPool) :
reed@google.comac10a2d2010-12-22 21:39:39 +000027 fDraws(DRAWS_BLOCK_SIZE, fDrawsStorage),
28 fStates(STATES_BLOCK_SIZE, fStatesStorage),
29 fClips(CLIPS_BLOCK_SIZE, fClipsStorage),
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000030 fClipSet(true),
31
32 fLastRectVertexLayout(0),
33 fQuadIndexBuffer(NULL),
34 fMaxQuads(0),
35 fCurrQuad(0),
36
bsalomon@google.com1c13c962011-02-14 16:51:21 +000037 fVertexPool(*vertexPool),
38 fCurrPoolVertexBuffer(NULL),
39 fCurrPoolStartVertex(0),
40 fIndexPool(*indexPool),
41 fCurrPoolIndexBuffer(NULL),
42 fCurrPoolStartIndex(0),
reed@google.comac10a2d2010-12-22 21:39:39 +000043 fReservedVertexBytes(0),
44 fReservedIndexBytes(0),
45 fUsedReservedVertexBytes(0),
46 fUsedReservedIndexBytes(0) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000047 GrAssert(NULL != vertexPool);
48 GrAssert(NULL != indexPool);
reed@google.comac10a2d2010-12-22 21:39:39 +000049}
50
51GrInOrderDrawBuffer::~GrInOrderDrawBuffer() {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000052 this->reset();
53 GrSafeUnref(fQuadIndexBuffer);
reed@google.comac10a2d2010-12-22 21:39:39 +000054}
55
56void GrInOrderDrawBuffer::initializeDrawStateAndClip(const GrDrawTarget& target) {
57 this->copyDrawState(target);
58 this->setClip(target.getClip());
59}
60
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000061void GrInOrderDrawBuffer::setQuadIndexBuffer(const GrIndexBuffer* indexBuffer) {
62 bool newIdxBuffer = fQuadIndexBuffer != indexBuffer;
63 if (newIdxBuffer) {
64 GrSafeUnref(fQuadIndexBuffer);
65 fQuadIndexBuffer = indexBuffer;
66 GrSafeRef(fQuadIndexBuffer);
67 fCurrQuad = 0;
68 fMaxQuads = (NULL == indexBuffer) ? 0 : indexBuffer->maxQuads();
69 } else {
bsalomon@google.comd302f142011-03-03 13:54:13 +000070 GrAssert((NULL == indexBuffer && 0 == fMaxQuads) ||
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000071 (indexBuffer->maxQuads() == fMaxQuads));
72 }
73}
74
bsalomon@google.comd302f142011-03-03 13:54:13 +000075void GrInOrderDrawBuffer::drawRect(const GrRect& rect,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000076 const GrMatrix* matrix,
bsalomon@google.comffca4002011-02-22 20:34:01 +000077 StageBitfield stageEnableBitfield,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000078 const GrRect* srcRects[],
79 const GrMatrix* srcMatrices[]) {
bsalomon@google.comd302f142011-03-03 13:54:13 +000080
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000081 GrAssert(!(NULL == fQuadIndexBuffer && fCurrQuad));
82 GrAssert(!(fDraws.empty() && fCurrQuad));
83 GrAssert(!(0 != fMaxQuads && NULL == fQuadIndexBuffer));
84
85 // if we have a quad IB then either append to the previous run of
86 // rects or start a new run
87 if (fMaxQuads) {
bsalomon@google.comd302f142011-03-03 13:54:13 +000088
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000089 bool appendToPreviousDraw = false;
bsalomon@google.comffca4002011-02-22 20:34:01 +000090 GrVertexLayout layout = GetRectVertexLayout(stageEnableBitfield, srcRects);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000091 AutoReleaseGeometry geo(this, layout, 4, 0);
92 AutoViewMatrixRestore avmr(this);
93 GrMatrix combinedMatrix = this->getViewMatrix();
94 this->setViewMatrix(GrMatrix::I());
95 if (NULL != matrix) {
96 combinedMatrix.preConcat(*matrix);
97 }
98
99 SetRectVertices(rect, &combinedMatrix, srcRects, srcMatrices, layout, geo.vertices());
100
101 // we don't want to miss an opportunity to batch rects together
102 // simply because the clip has changed if the clip doesn't affect
103 // the rect.
104 bool disabledClip = false;
105 if (this->isClipState() && fClip.isRect()) {
bsalomon@google.comd302f142011-03-03 13:54:13 +0000106
bsalomon@google.com0b50b2e2011-03-08 21:07:21 +0000107 GrRect clipRect = fClip.getRect(0);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000108 // If the clip rect touches the edge of the viewport, extended it
109 // out (close) to infinity to avoid bogus intersections.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000110 // We might consider a more exact clip to viewport if this
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000111 // conservative test fails.
112 const GrRenderTarget* target = this->getRenderTarget();
113 if (0 >= clipRect.fLeft) {
114 clipRect.fLeft = GR_ScalarMin;
115 }
116 if (target->width() <= clipRect.fRight) {
117 clipRect.fRight = GR_ScalarMax;
118 }
119 if (0 >= clipRect.top()) {
120 clipRect.fTop = GR_ScalarMin;
121 }
122 if (target->height() <= clipRect.fBottom) {
123 clipRect.fBottom = GR_ScalarMax;
124 }
125 int stride = VertexSize(layout);
126 bool insideClip = true;
127 for (int v = 0; v < 4; ++v) {
128 const GrPoint& p = *GetVertexPoint(geo.vertices(), v, stride);
129 if (!clipRect.contains(p)) {
130 insideClip = false;
131 break;
132 }
133 }
134 if (insideClip) {
135 this->disableState(kClip_StateBit);
136 disabledClip = true;
137 }
138 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000139 if (!needsNewClip() && !needsNewState() && fCurrQuad > 0 &&
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000140 fCurrQuad < fMaxQuads && layout == fLastRectVertexLayout) {
141
142 int vsize = VertexSize(layout);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000143
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000144 Draw& lastDraw = fDraws.back();
145
146 GrAssert(lastDraw.fIndexBuffer == fQuadIndexBuffer);
147 GrAssert(kTriangles_PrimitiveType == lastDraw.fPrimitiveType);
148 GrAssert(0 == lastDraw.fVertexCount % 4);
149 GrAssert(0 == lastDraw.fIndexCount % 6);
150 GrAssert(0 == lastDraw.fStartIndex);
151
152 appendToPreviousDraw = lastDraw.fVertexBuffer == fCurrPoolVertexBuffer &&
153 (fCurrQuad * 4 + lastDraw.fStartVertex) == fCurrPoolStartVertex;
154 if (appendToPreviousDraw) {
155 lastDraw.fVertexCount += 4;
156 lastDraw.fIndexCount += 6;
157 fCurrQuad += 1;
158 GrAssert(0 == fUsedReservedVertexBytes);
159 fUsedReservedVertexBytes = 4 * vsize;
160 }
161 }
162 if (!appendToPreviousDraw) {
163 this->setIndexSourceToBuffer(fQuadIndexBuffer);
164 drawIndexed(kTriangles_PrimitiveType, 0, 0, 4, 6);
165 fCurrQuad = 1;
166 fLastRectVertexLayout = layout;
167 }
168 if (disabledClip) {
169 this->enableState(kClip_StateBit);
170 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000171 } else {
bsalomon@google.comffca4002011-02-22 20:34:01 +0000172 INHERITED::drawRect(rect, matrix, stageEnableBitfield, srcRects, srcMatrices);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000173 }
174}
175
bsalomon@google.comffca4002011-02-22 20:34:01 +0000176void GrInOrderDrawBuffer::drawIndexed(GrPrimitiveType primitiveType,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000177 int startVertex,
178 int startIndex,
179 int vertexCount,
180 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000181
182 if (!vertexCount || !indexCount) {
183 return;
184 }
185
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000186 fCurrQuad = 0;
187
reed@google.comac10a2d2010-12-22 21:39:39 +0000188 Draw& draw = fDraws.push_back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000189 draw.fPrimitiveType = primitiveType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000190 draw.fStartVertex = startVertex;
191 draw.fStartIndex = startIndex;
192 draw.fVertexCount = vertexCount;
193 draw.fIndexCount = indexCount;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000194
195 draw.fClipChanged = this->needsNewClip();
196 if (draw.fClipChanged) {
197 this->pushClip();
198 }
199
200 draw.fStateChanged = this->needsNewState();
201 if (draw.fStateChanged) {
202 this->pushState();
203 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000204
205 draw.fVertexLayout = fGeometrySrc.fVertexLayout;
206 switch (fGeometrySrc.fVertexSrc) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000207 case kBuffer_GeometrySrcType:
reed@google.comac10a2d2010-12-22 21:39:39 +0000208 draw.fVertexBuffer = fGeometrySrc.fVertexBuffer;
209 break;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000210 case kReserved_GeometrySrcType: {
211 size_t vertexBytes = (vertexCount + startVertex) *
212 VertexSize(fGeometrySrc.fVertexLayout);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000213 fUsedReservedVertexBytes = GrMax(fUsedReservedVertexBytes, vertexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000214 } // fallthrough
215 case kArray_GeometrySrcType:
216 draw.fVertexBuffer = fCurrPoolVertexBuffer;
217 draw.fStartVertex += fCurrPoolStartVertex;
218 break;
219 default:
220 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000221 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000222 draw.fVertexBuffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000223
224 switch (fGeometrySrc.fIndexSrc) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000225 case kBuffer_GeometrySrcType:
reed@google.comac10a2d2010-12-22 21:39:39 +0000226 draw.fIndexBuffer = fGeometrySrc.fIndexBuffer;
227 break;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000228 case kReserved_GeometrySrcType: {
229 size_t indexBytes = (indexCount + startIndex) * sizeof(uint16_t);
230 fUsedReservedIndexBytes = GrMax(fUsedReservedIndexBytes, indexBytes);
231 } // fallthrough
232 case kArray_GeometrySrcType:
233 draw.fIndexBuffer = fCurrPoolIndexBuffer;
234 draw.fStartIndex += fCurrPoolStartVertex;
235 break;
236 default:
237 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000238 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000239 draw.fIndexBuffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000240}
241
bsalomon@google.comffca4002011-02-22 20:34:01 +0000242void GrInOrderDrawBuffer::drawNonIndexed(GrPrimitiveType primitiveType,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000243 int startVertex,
244 int vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000245 if (!vertexCount) {
246 return;
247 }
248
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000249 fCurrQuad = 0;
250
reed@google.comac10a2d2010-12-22 21:39:39 +0000251 Draw& draw = fDraws.push_back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000252 draw.fPrimitiveType = primitiveType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000253 draw.fStartVertex = startVertex;
254 draw.fStartIndex = 0;
255 draw.fVertexCount = vertexCount;
256 draw.fIndexCount = 0;
257
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000258 draw.fClipChanged = this->needsNewClip();
259 if (draw.fClipChanged) {
260 this->pushClip();
261 }
262
263 draw.fStateChanged = this->needsNewState();
264 if (draw.fStateChanged) {
265 this->pushState();
266 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000267
268 draw.fVertexLayout = fGeometrySrc.fVertexLayout;
269 switch (fGeometrySrc.fVertexSrc) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000270 case kBuffer_GeometrySrcType:
reed@google.comac10a2d2010-12-22 21:39:39 +0000271 draw.fVertexBuffer = fGeometrySrc.fVertexBuffer;
272 break;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000273 case kReserved_GeometrySrcType: {
274 size_t vertexBytes = (vertexCount + startVertex) *
275 VertexSize(fGeometrySrc.fVertexLayout);
276 fUsedReservedVertexBytes = GrMax(fUsedReservedVertexBytes,
277 vertexBytes);
278 } // fallthrough
279 case kArray_GeometrySrcType:
280 draw.fVertexBuffer = fCurrPoolVertexBuffer;
281 draw.fStartVertex += fCurrPoolStartVertex;
282 break;
283 default:
284 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000285 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000286 draw.fVertexBuffer->ref();
287 draw.fIndexBuffer = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000288}
289
290void GrInOrderDrawBuffer::reset() {
291 GrAssert(!fReservedGeometry.fLocked);
292 uint32_t numStates = fStates.count();
293 for (uint32_t i = 0; i < numStates; ++i) {
bsalomon@google.com1da07462011-03-10 14:51:57 +0000294 const DrState& dstate = this->accessSavedDrawState(fStates[i]);
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000295 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com1da07462011-03-10 14:51:57 +0000296 GrSafeUnref(dstate.fTextures[s]);
reed@google.comac10a2d2010-12-22 21:39:39 +0000297 }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000298 GrSafeUnref(dstate.fRenderTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +0000299 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000300 int numDraws = fDraws.count();
301 for (int d = 0; d < numDraws; ++d) {
302 // we always have a VB, but not always an IB
303 GrAssert(NULL != fDraws[d].fVertexBuffer);
304 fDraws[d].fVertexBuffer->unref();
305 GrSafeUnref(fDraws[d].fIndexBuffer);
306 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000307 fDraws.reset();
308 fStates.reset();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000309
310 fVertexPool.reset();
311 fIndexPool.reset();
312
reed@google.comac10a2d2010-12-22 21:39:39 +0000313 fClips.reset();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000314
315 fCurrQuad = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000316}
317
318void GrInOrderDrawBuffer::playback(GrDrawTarget* target) {
319 GrAssert(NULL != target);
320 GrAssert(target != this); // not considered and why?
321
322 uint32_t numDraws = fDraws.count();
323 if (!numDraws) {
324 return;
325 }
326
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000327 fVertexPool.unlock();
328 fIndexPool.unlock();
reed@google.comac10a2d2010-12-22 21:39:39 +0000329
330 GrDrawTarget::AutoStateRestore asr(target);
331 GrDrawTarget::AutoClipRestore acr(target);
332 // important to not mess with reserve/lock geometry in the target with this
333 // on the stack.
334 GrDrawTarget::AutoGeometrySrcRestore agsr(target);
335
336 uint32_t currState = ~0;
337 uint32_t currClip = ~0;
338
339 for (uint32_t i = 0; i < numDraws; ++i) {
340 const Draw& draw = fDraws[i];
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000341 if (draw.fStateChanged) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000342 ++currState;
343 target->restoreDrawState(fStates[currState]);
344 }
345 if (draw.fClipChanged) {
346 ++currClip;
347 target->setClip(fClips[currClip]);
348 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000349 uint32_t vertexReserveCount = 0;
350 uint32_t indexReserveCount = 0;
351
352 target->setVertexSourceToBuffer(draw.fVertexLayout, draw.fVertexBuffer);
353
reed@google.comac10a2d2010-12-22 21:39:39 +0000354 if (draw.fIndexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000355 target->setIndexSourceToBuffer(draw.fIndexBuffer);
356 }
357
358 if (draw.fIndexCount) {
359 target->drawIndexed(draw.fPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +0000360 draw.fStartVertex,
361 draw.fStartIndex,
362 draw.fVertexCount,
363 draw.fIndexCount);
364 } else {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000365 target->drawNonIndexed(draw.fPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +0000366 draw.fStartVertex,
367 draw.fVertexCount);
368 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000369 if (vertexReserveCount || indexReserveCount) {
370 target->releaseReservedGeometry();
371 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000372 }
373}
374
375bool GrInOrderDrawBuffer::geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000376 int* vertexCount,
377 int* indexCount) const {
378 // we will recommend a flush if the data could fit in a single
379 // preallocated buffer but none are left and it can't fit
380 // in the current buffer (which may not be prealloced).
reed@google.comac10a2d2010-12-22 21:39:39 +0000381 bool flush = false;
382 if (NULL != indexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000383 int32_t currIndices = fIndexPool.currentBufferIndices();
384 if (*indexCount > currIndices &&
385 (!fIndexPool.preallocatedBuffersRemaining() &&
386 *indexCount <= fIndexPool.preallocatedBufferIndices())) {
387
388 flush = true;
389 }
390 *indexCount = currIndices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000391 }
392 if (NULL != vertexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000393 int32_t currVertices = fVertexPool.currentBufferVertices(vertexLayout);
394 if (*vertexCount > currVertices &&
395 (!fVertexPool.preallocatedBuffersRemaining() &&
396 *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexLayout))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000397
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000398 flush = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000399 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000400 *vertexCount = currVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000401 }
402 return flush;
403}
404
405bool GrInOrderDrawBuffer::acquireGeometryHelper(GrVertexLayout vertexLayout,
406 void** vertices,
407 void** indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000408 GrAssert(!fReservedGeometry.fLocked);
reed@google.comac10a2d2010-12-22 21:39:39 +0000409 if (fReservedGeometry.fVertexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000410 GrAssert(NULL != vertices);
411 GrAssert(0 == fReservedVertexBytes);
412 GrAssert(0 == fUsedReservedVertexBytes);
413
reed@google.comac10a2d2010-12-22 21:39:39 +0000414 fReservedVertexBytes = VertexSize(vertexLayout) *
415 fReservedGeometry.fVertexCount;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000416 *vertices = fVertexPool.makeSpace(vertexLayout,
417 fReservedGeometry.fVertexCount,
418 &fCurrPoolVertexBuffer,
419 &fCurrPoolStartVertex);
420 if (NULL == *vertices) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000421 return false;
422 }
423 }
424 if (fReservedGeometry.fIndexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000425 GrAssert(NULL != indices);
426 GrAssert(0 == fReservedIndexBytes);
427 GrAssert(0 == fUsedReservedIndexBytes);
428
429 *indices = fIndexPool.makeSpace(fReservedGeometry.fIndexCount,
430 &fCurrPoolIndexBuffer,
431 &fCurrPoolStartIndex);
432 if (NULL == *indices) {
433 fVertexPool.putBack(fReservedVertexBytes);
434 fReservedVertexBytes = 0;
435 fCurrPoolVertexBuffer = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000436 return false;
437 }
438 }
439 return true;
440}
441
442void GrInOrderDrawBuffer::releaseGeometryHelper() {
443 GrAssert(fUsedReservedVertexBytes <= fReservedVertexBytes);
444 GrAssert(fUsedReservedIndexBytes <= fReservedIndexBytes);
445
446 size_t vertexSlack = fReservedVertexBytes - fUsedReservedVertexBytes;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000447 fVertexPool.putBack(vertexSlack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000448
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000449 size_t indexSlack = fReservedIndexBytes - fUsedReservedIndexBytes;
450 fIndexPool.putBack(indexSlack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000451
reed@google.comac10a2d2010-12-22 21:39:39 +0000452 fReservedVertexBytes = 0;
453 fReservedIndexBytes = 0;
454 fUsedReservedVertexBytes = 0;
455 fUsedReservedIndexBytes = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000456 fCurrPoolVertexBuffer = 0;
457 fCurrPoolStartVertex = 0;
458
459}
460
461void GrInOrderDrawBuffer::setVertexSourceToArrayHelper(const void* vertexArray,
462 int vertexCount) {
463 GrAssert(!fReservedGeometry.fLocked || !fReservedGeometry.fVertexCount);
464#if GR_DEBUG
465 bool success =
466#endif
467 fVertexPool.appendVertices(fGeometrySrc.fVertexLayout,
468 vertexCount,
469 vertexArray,
470 &fCurrPoolVertexBuffer,
471 &fCurrPoolStartVertex);
472 GR_DEBUGASSERT(success);
473}
474
475void GrInOrderDrawBuffer::setIndexSourceToArrayHelper(const void* indexArray,
476 int indexCount) {
477 GrAssert(!fReservedGeometry.fLocked || !fReservedGeometry.fIndexCount);
478#if GR_DEBUG
479 bool success =
480#endif
481 fIndexPool.appendIndices(indexCount,
482 indexArray,
483 &fCurrPoolIndexBuffer,
484 &fCurrPoolStartIndex);
485 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000486}
487
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000488bool GrInOrderDrawBuffer::needsNewState() const {
489 if (fStates.empty()) {
490 return true;
491 } else {
492 const DrState& old = this->accessSavedDrawState(fStates.back());
493 return old != fCurrDrawState;
494 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000495}
496
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000497void GrInOrderDrawBuffer::pushState() {
498 for (int s = 0; s < kNumStages; ++s) {
499 GrSafeRef(fCurrDrawState.fTextures[s]);
500 }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000501 GrSafeRef(fCurrDrawState.fRenderTarget);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000502 this->saveCurrentDrawState(&fStates.push_back());
503 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000504
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000505bool GrInOrderDrawBuffer::needsNewClip() const {
506 if (fCurrDrawState.fFlagBits & kClip_StateBit) {
507 if (fClips.empty() || (fClipSet && fClips.back() != fClip)) {
508 return true;
509 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000510 }
511 return false;
512}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000513
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000514void GrInOrderDrawBuffer::pushClip() {
515 fClips.push_back() = fClip;
516 fClipSet = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000517}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000518
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000519void GrInOrderDrawBuffer::clipWillBeSet(const GrClip& newClip) {
520 fClipSet = true;
521}