blob: a24936475281b8e50c515de52aca186f1833832b [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
2 Copyright 2010 Google Inc.
3
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
107 // single rect clip should have bounds
108 GrAssert(fClip.hasBounds());
109
110 GrRect clipRect = GrRect(fClip.getBounds());
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000111 // If the clip rect touches the edge of the viewport, extended it
112 // out (close) to infinity to avoid bogus intersections.
bsalomon@google.comd302f142011-03-03 13:54:13 +0000113 // We might consider a more exact clip to viewport if this
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000114 // conservative test fails.
115 const GrRenderTarget* target = this->getRenderTarget();
116 if (0 >= clipRect.fLeft) {
117 clipRect.fLeft = GR_ScalarMin;
118 }
119 if (target->width() <= clipRect.fRight) {
120 clipRect.fRight = GR_ScalarMax;
121 }
122 if (0 >= clipRect.top()) {
123 clipRect.fTop = GR_ScalarMin;
124 }
125 if (target->height() <= clipRect.fBottom) {
126 clipRect.fBottom = GR_ScalarMax;
127 }
128 int stride = VertexSize(layout);
129 bool insideClip = true;
130 for (int v = 0; v < 4; ++v) {
131 const GrPoint& p = *GetVertexPoint(geo.vertices(), v, stride);
132 if (!clipRect.contains(p)) {
133 insideClip = false;
134 break;
135 }
136 }
137 if (insideClip) {
138 this->disableState(kClip_StateBit);
139 disabledClip = true;
140 }
141 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000142 if (!needsNewClip() && !needsNewState() && fCurrQuad > 0 &&
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000143 fCurrQuad < fMaxQuads && layout == fLastRectVertexLayout) {
144
145 int vsize = VertexSize(layout);
bsalomon@google.comd302f142011-03-03 13:54:13 +0000146
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000147 Draw& lastDraw = fDraws.back();
148
149 GrAssert(lastDraw.fIndexBuffer == fQuadIndexBuffer);
150 GrAssert(kTriangles_PrimitiveType == lastDraw.fPrimitiveType);
151 GrAssert(0 == lastDraw.fVertexCount % 4);
152 GrAssert(0 == lastDraw.fIndexCount % 6);
153 GrAssert(0 == lastDraw.fStartIndex);
154
155 appendToPreviousDraw = lastDraw.fVertexBuffer == fCurrPoolVertexBuffer &&
156 (fCurrQuad * 4 + lastDraw.fStartVertex) == fCurrPoolStartVertex;
157 if (appendToPreviousDraw) {
158 lastDraw.fVertexCount += 4;
159 lastDraw.fIndexCount += 6;
160 fCurrQuad += 1;
161 GrAssert(0 == fUsedReservedVertexBytes);
162 fUsedReservedVertexBytes = 4 * vsize;
163 }
164 }
165 if (!appendToPreviousDraw) {
166 this->setIndexSourceToBuffer(fQuadIndexBuffer);
167 drawIndexed(kTriangles_PrimitiveType, 0, 0, 4, 6);
168 fCurrQuad = 1;
169 fLastRectVertexLayout = layout;
170 }
171 if (disabledClip) {
172 this->enableState(kClip_StateBit);
173 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000174 } else {
bsalomon@google.comffca4002011-02-22 20:34:01 +0000175 INHERITED::drawRect(rect, matrix, stageEnableBitfield, srcRects, srcMatrices);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000176 }
177}
178
bsalomon@google.comffca4002011-02-22 20:34:01 +0000179void GrInOrderDrawBuffer::drawIndexed(GrPrimitiveType primitiveType,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000180 int startVertex,
181 int startIndex,
182 int vertexCount,
183 int indexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000184
185 if (!vertexCount || !indexCount) {
186 return;
187 }
188
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000189 fCurrQuad = 0;
190
reed@google.comac10a2d2010-12-22 21:39:39 +0000191 Draw& draw = fDraws.push_back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000192 draw.fPrimitiveType = primitiveType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000193 draw.fStartVertex = startVertex;
194 draw.fStartIndex = startIndex;
195 draw.fVertexCount = vertexCount;
196 draw.fIndexCount = indexCount;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000197
198 draw.fClipChanged = this->needsNewClip();
199 if (draw.fClipChanged) {
200 this->pushClip();
201 }
202
203 draw.fStateChanged = this->needsNewState();
204 if (draw.fStateChanged) {
205 this->pushState();
206 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000207
208 draw.fVertexLayout = fGeometrySrc.fVertexLayout;
209 switch (fGeometrySrc.fVertexSrc) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000210 case kBuffer_GeometrySrcType:
reed@google.comac10a2d2010-12-22 21:39:39 +0000211 draw.fVertexBuffer = fGeometrySrc.fVertexBuffer;
212 break;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000213 case kReserved_GeometrySrcType: {
214 size_t vertexBytes = (vertexCount + startVertex) *
215 VertexSize(fGeometrySrc.fVertexLayout);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000216 fUsedReservedVertexBytes = GrMax(fUsedReservedVertexBytes, vertexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000217 } // fallthrough
218 case kArray_GeometrySrcType:
219 draw.fVertexBuffer = fCurrPoolVertexBuffer;
220 draw.fStartVertex += fCurrPoolStartVertex;
221 break;
222 default:
223 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000224 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000225 draw.fVertexBuffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000226
227 switch (fGeometrySrc.fIndexSrc) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000228 case kBuffer_GeometrySrcType:
reed@google.comac10a2d2010-12-22 21:39:39 +0000229 draw.fIndexBuffer = fGeometrySrc.fIndexBuffer;
230 break;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000231 case kReserved_GeometrySrcType: {
232 size_t indexBytes = (indexCount + startIndex) * sizeof(uint16_t);
233 fUsedReservedIndexBytes = GrMax(fUsedReservedIndexBytes, indexBytes);
234 } // fallthrough
235 case kArray_GeometrySrcType:
236 draw.fIndexBuffer = fCurrPoolIndexBuffer;
237 draw.fStartIndex += fCurrPoolStartVertex;
238 break;
239 default:
240 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000241 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000242 draw.fIndexBuffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000243}
244
bsalomon@google.comffca4002011-02-22 20:34:01 +0000245void GrInOrderDrawBuffer::drawNonIndexed(GrPrimitiveType primitiveType,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000246 int startVertex,
247 int vertexCount) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000248 if (!vertexCount) {
249 return;
250 }
251
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000252 fCurrQuad = 0;
253
reed@google.comac10a2d2010-12-22 21:39:39 +0000254 Draw& draw = fDraws.push_back();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000255 draw.fPrimitiveType = primitiveType;
reed@google.comac10a2d2010-12-22 21:39:39 +0000256 draw.fStartVertex = startVertex;
257 draw.fStartIndex = 0;
258 draw.fVertexCount = vertexCount;
259 draw.fIndexCount = 0;
260
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000261 draw.fClipChanged = this->needsNewClip();
262 if (draw.fClipChanged) {
263 this->pushClip();
264 }
265
266 draw.fStateChanged = this->needsNewState();
267 if (draw.fStateChanged) {
268 this->pushState();
269 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000270
271 draw.fVertexLayout = fGeometrySrc.fVertexLayout;
272 switch (fGeometrySrc.fVertexSrc) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000273 case kBuffer_GeometrySrcType:
reed@google.comac10a2d2010-12-22 21:39:39 +0000274 draw.fVertexBuffer = fGeometrySrc.fVertexBuffer;
275 break;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000276 case kReserved_GeometrySrcType: {
277 size_t vertexBytes = (vertexCount + startVertex) *
278 VertexSize(fGeometrySrc.fVertexLayout);
279 fUsedReservedVertexBytes = GrMax(fUsedReservedVertexBytes,
280 vertexBytes);
281 } // fallthrough
282 case kArray_GeometrySrcType:
283 draw.fVertexBuffer = fCurrPoolVertexBuffer;
284 draw.fStartVertex += fCurrPoolStartVertex;
285 break;
286 default:
287 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000288 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000289 draw.fVertexBuffer->ref();
290 draw.fIndexBuffer = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000291}
292
293void GrInOrderDrawBuffer::reset() {
294 GrAssert(!fReservedGeometry.fLocked);
295 uint32_t numStates = fStates.count();
296 for (uint32_t i = 0; i < numStates; ++i) {
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000297 for (int s = 0; s < kNumStages; ++s) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000298 GrTexture* tex = this->accessSavedDrawState(fStates[i]).fTextures[s];
bsalomon@google.com8531c1c2011-01-13 19:52:45 +0000299 if (NULL != tex) {
300 tex->unref();
301 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000302 }
303 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000304 int numDraws = fDraws.count();
305 for (int d = 0; d < numDraws; ++d) {
306 // we always have a VB, but not always an IB
307 GrAssert(NULL != fDraws[d].fVertexBuffer);
308 fDraws[d].fVertexBuffer->unref();
309 GrSafeUnref(fDraws[d].fIndexBuffer);
310 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000311 fDraws.reset();
312 fStates.reset();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000313
314 fVertexPool.reset();
315 fIndexPool.reset();
316
reed@google.comac10a2d2010-12-22 21:39:39 +0000317 fClips.reset();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000318
319 fCurrQuad = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000320}
321
322void GrInOrderDrawBuffer::playback(GrDrawTarget* target) {
323 GrAssert(NULL != target);
324 GrAssert(target != this); // not considered and why?
325
326 uint32_t numDraws = fDraws.count();
327 if (!numDraws) {
328 return;
329 }
330
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000331 fVertexPool.unlock();
332 fIndexPool.unlock();
reed@google.comac10a2d2010-12-22 21:39:39 +0000333
334 GrDrawTarget::AutoStateRestore asr(target);
335 GrDrawTarget::AutoClipRestore acr(target);
336 // important to not mess with reserve/lock geometry in the target with this
337 // on the stack.
338 GrDrawTarget::AutoGeometrySrcRestore agsr(target);
339
340 uint32_t currState = ~0;
341 uint32_t currClip = ~0;
342
343 for (uint32_t i = 0; i < numDraws; ++i) {
344 const Draw& draw = fDraws[i];
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000345 if (draw.fStateChanged) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000346 ++currState;
347 target->restoreDrawState(fStates[currState]);
348 }
349 if (draw.fClipChanged) {
350 ++currClip;
351 target->setClip(fClips[currClip]);
352 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000353 uint32_t vertexReserveCount = 0;
354 uint32_t indexReserveCount = 0;
355
356 target->setVertexSourceToBuffer(draw.fVertexLayout, draw.fVertexBuffer);
357
reed@google.comac10a2d2010-12-22 21:39:39 +0000358 if (draw.fIndexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000359 target->setIndexSourceToBuffer(draw.fIndexBuffer);
360 }
361
362 if (draw.fIndexCount) {
363 target->drawIndexed(draw.fPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +0000364 draw.fStartVertex,
365 draw.fStartIndex,
366 draw.fVertexCount,
367 draw.fIndexCount);
368 } else {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000369 target->drawNonIndexed(draw.fPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +0000370 draw.fStartVertex,
371 draw.fVertexCount);
372 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000373 if (vertexReserveCount || indexReserveCount) {
374 target->releaseReservedGeometry();
375 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000376 }
377}
378
379bool GrInOrderDrawBuffer::geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000380 int* vertexCount,
381 int* indexCount) const {
382 // we will recommend a flush if the data could fit in a single
383 // preallocated buffer but none are left and it can't fit
384 // in the current buffer (which may not be prealloced).
reed@google.comac10a2d2010-12-22 21:39:39 +0000385 bool flush = false;
386 if (NULL != indexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000387 int32_t currIndices = fIndexPool.currentBufferIndices();
388 if (*indexCount > currIndices &&
389 (!fIndexPool.preallocatedBuffersRemaining() &&
390 *indexCount <= fIndexPool.preallocatedBufferIndices())) {
391
392 flush = true;
393 }
394 *indexCount = currIndices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000395 }
396 if (NULL != vertexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000397 int32_t currVertices = fVertexPool.currentBufferVertices(vertexLayout);
398 if (*vertexCount > currVertices &&
399 (!fVertexPool.preallocatedBuffersRemaining() &&
400 *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexLayout))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000401
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000402 flush = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000403 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000404 *vertexCount = currVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000405 }
406 return flush;
407}
408
409bool GrInOrderDrawBuffer::acquireGeometryHelper(GrVertexLayout vertexLayout,
410 void** vertices,
411 void** indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000412 GrAssert(!fReservedGeometry.fLocked);
reed@google.comac10a2d2010-12-22 21:39:39 +0000413 if (fReservedGeometry.fVertexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000414 GrAssert(NULL != vertices);
415 GrAssert(0 == fReservedVertexBytes);
416 GrAssert(0 == fUsedReservedVertexBytes);
417
reed@google.comac10a2d2010-12-22 21:39:39 +0000418 fReservedVertexBytes = VertexSize(vertexLayout) *
419 fReservedGeometry.fVertexCount;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000420 *vertices = fVertexPool.makeSpace(vertexLayout,
421 fReservedGeometry.fVertexCount,
422 &fCurrPoolVertexBuffer,
423 &fCurrPoolStartVertex);
424 if (NULL == *vertices) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000425 return false;
426 }
427 }
428 if (fReservedGeometry.fIndexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000429 GrAssert(NULL != indices);
430 GrAssert(0 == fReservedIndexBytes);
431 GrAssert(0 == fUsedReservedIndexBytes);
432
433 *indices = fIndexPool.makeSpace(fReservedGeometry.fIndexCount,
434 &fCurrPoolIndexBuffer,
435 &fCurrPoolStartIndex);
436 if (NULL == *indices) {
437 fVertexPool.putBack(fReservedVertexBytes);
438 fReservedVertexBytes = 0;
439 fCurrPoolVertexBuffer = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000440 return false;
441 }
442 }
443 return true;
444}
445
446void GrInOrderDrawBuffer::releaseGeometryHelper() {
447 GrAssert(fUsedReservedVertexBytes <= fReservedVertexBytes);
448 GrAssert(fUsedReservedIndexBytes <= fReservedIndexBytes);
449
450 size_t vertexSlack = fReservedVertexBytes - fUsedReservedVertexBytes;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000451 fVertexPool.putBack(vertexSlack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000452
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000453 size_t indexSlack = fReservedIndexBytes - fUsedReservedIndexBytes;
454 fIndexPool.putBack(indexSlack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000455
reed@google.comac10a2d2010-12-22 21:39:39 +0000456 fReservedVertexBytes = 0;
457 fReservedIndexBytes = 0;
458 fUsedReservedVertexBytes = 0;
459 fUsedReservedIndexBytes = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000460 fCurrPoolVertexBuffer = 0;
461 fCurrPoolStartVertex = 0;
462
463}
464
465void GrInOrderDrawBuffer::setVertexSourceToArrayHelper(const void* vertexArray,
466 int vertexCount) {
467 GrAssert(!fReservedGeometry.fLocked || !fReservedGeometry.fVertexCount);
468#if GR_DEBUG
469 bool success =
470#endif
471 fVertexPool.appendVertices(fGeometrySrc.fVertexLayout,
472 vertexCount,
473 vertexArray,
474 &fCurrPoolVertexBuffer,
475 &fCurrPoolStartVertex);
476 GR_DEBUGASSERT(success);
477}
478
479void GrInOrderDrawBuffer::setIndexSourceToArrayHelper(const void* indexArray,
480 int indexCount) {
481 GrAssert(!fReservedGeometry.fLocked || !fReservedGeometry.fIndexCount);
482#if GR_DEBUG
483 bool success =
484#endif
485 fIndexPool.appendIndices(indexCount,
486 indexArray,
487 &fCurrPoolIndexBuffer,
488 &fCurrPoolStartIndex);
489 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000490}
491
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000492bool GrInOrderDrawBuffer::needsNewState() const {
493 if (fStates.empty()) {
494 return true;
495 } else {
496 const DrState& old = this->accessSavedDrawState(fStates.back());
497 return old != fCurrDrawState;
498 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000499}
500
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000501void GrInOrderDrawBuffer::pushState() {
502 for (int s = 0; s < kNumStages; ++s) {
503 GrSafeRef(fCurrDrawState.fTextures[s]);
504 }
505 this->saveCurrentDrawState(&fStates.push_back());
506 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000507
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000508bool GrInOrderDrawBuffer::needsNewClip() const {
509 if (fCurrDrawState.fFlagBits & kClip_StateBit) {
510 if (fClips.empty() || (fClipSet && fClips.back() != fClip)) {
511 return true;
512 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000513 }
514 return false;
515}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000516
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000517void GrInOrderDrawBuffer::pushClip() {
518 fClips.push_back() = fClip;
519 fClipSet = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000520}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000521
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000522void GrInOrderDrawBuffer::clipWillBeSet(const GrClip& newClip) {
523 fClipSet = true;
524}