blob: 8191814a58ffabffb8653b8636e4ec6b5cf7667d [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) {
reed@google.com0ebe81a2011-04-04 20:06:59 +0000319 GrAssert(!fReservedGeometry.fLocked);
reed@google.comac10a2d2010-12-22 21:39:39 +0000320 GrAssert(NULL != target);
321 GrAssert(target != this); // not considered and why?
322
323 uint32_t numDraws = fDraws.count();
324 if (!numDraws) {
325 return;
326 }
327
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000328 fVertexPool.unlock();
329 fIndexPool.unlock();
reed@google.comac10a2d2010-12-22 21:39:39 +0000330
331 GrDrawTarget::AutoStateRestore asr(target);
332 GrDrawTarget::AutoClipRestore acr(target);
333 // important to not mess with reserve/lock geometry in the target with this
334 // on the stack.
335 GrDrawTarget::AutoGeometrySrcRestore agsr(target);
336
337 uint32_t currState = ~0;
338 uint32_t currClip = ~0;
339
340 for (uint32_t i = 0; i < numDraws; ++i) {
341 const Draw& draw = fDraws[i];
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000342 if (draw.fStateChanged) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000343 ++currState;
344 target->restoreDrawState(fStates[currState]);
345 }
346 if (draw.fClipChanged) {
347 ++currClip;
348 target->setClip(fClips[currClip]);
349 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000350
351 target->setVertexSourceToBuffer(draw.fVertexLayout, draw.fVertexBuffer);
352
reed@google.comac10a2d2010-12-22 21:39:39 +0000353 if (draw.fIndexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000354 target->setIndexSourceToBuffer(draw.fIndexBuffer);
355 }
356
357 if (draw.fIndexCount) {
358 target->drawIndexed(draw.fPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +0000359 draw.fStartVertex,
360 draw.fStartIndex,
361 draw.fVertexCount,
362 draw.fIndexCount);
363 } else {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000364 target->drawNonIndexed(draw.fPrimitiveType,
reed@google.comac10a2d2010-12-22 21:39:39 +0000365 draw.fStartVertex,
366 draw.fVertexCount);
367 }
368 }
369}
370
371bool GrInOrderDrawBuffer::geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000372 int* vertexCount,
373 int* indexCount) const {
374 // we will recommend a flush if the data could fit in a single
375 // preallocated buffer but none are left and it can't fit
376 // in the current buffer (which may not be prealloced).
reed@google.comac10a2d2010-12-22 21:39:39 +0000377 bool flush = false;
378 if (NULL != indexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000379 int32_t currIndices = fIndexPool.currentBufferIndices();
380 if (*indexCount > currIndices &&
381 (!fIndexPool.preallocatedBuffersRemaining() &&
382 *indexCount <= fIndexPool.preallocatedBufferIndices())) {
383
384 flush = true;
385 }
386 *indexCount = currIndices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000387 }
388 if (NULL != vertexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000389 int32_t currVertices = fVertexPool.currentBufferVertices(vertexLayout);
390 if (*vertexCount > currVertices &&
391 (!fVertexPool.preallocatedBuffersRemaining() &&
392 *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexLayout))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000393
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000394 flush = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000395 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000396 *vertexCount = currVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000397 }
398 return flush;
399}
400
401bool GrInOrderDrawBuffer::acquireGeometryHelper(GrVertexLayout vertexLayout,
402 void** vertices,
403 void** indices) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000404 GrAssert(!fReservedGeometry.fLocked);
reed@google.comac10a2d2010-12-22 21:39:39 +0000405 if (fReservedGeometry.fVertexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000406 GrAssert(NULL != vertices);
407 GrAssert(0 == fReservedVertexBytes);
408 GrAssert(0 == fUsedReservedVertexBytes);
409
reed@google.comac10a2d2010-12-22 21:39:39 +0000410 fReservedVertexBytes = VertexSize(vertexLayout) *
411 fReservedGeometry.fVertexCount;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000412 *vertices = fVertexPool.makeSpace(vertexLayout,
413 fReservedGeometry.fVertexCount,
414 &fCurrPoolVertexBuffer,
415 &fCurrPoolStartVertex);
416 if (NULL == *vertices) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000417 return false;
418 }
419 }
420 if (fReservedGeometry.fIndexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000421 GrAssert(NULL != indices);
422 GrAssert(0 == fReservedIndexBytes);
423 GrAssert(0 == fUsedReservedIndexBytes);
424
425 *indices = fIndexPool.makeSpace(fReservedGeometry.fIndexCount,
426 &fCurrPoolIndexBuffer,
427 &fCurrPoolStartIndex);
428 if (NULL == *indices) {
429 fVertexPool.putBack(fReservedVertexBytes);
430 fReservedVertexBytes = 0;
431 fCurrPoolVertexBuffer = NULL;
reed@google.comac10a2d2010-12-22 21:39:39 +0000432 return false;
433 }
434 }
435 return true;
436}
437
438void GrInOrderDrawBuffer::releaseGeometryHelper() {
439 GrAssert(fUsedReservedVertexBytes <= fReservedVertexBytes);
440 GrAssert(fUsedReservedIndexBytes <= fReservedIndexBytes);
441
442 size_t vertexSlack = fReservedVertexBytes - fUsedReservedVertexBytes;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000443 fVertexPool.putBack(vertexSlack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000444
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000445 size_t indexSlack = fReservedIndexBytes - fUsedReservedIndexBytes;
446 fIndexPool.putBack(indexSlack);
reed@google.comac10a2d2010-12-22 21:39:39 +0000447
reed@google.comac10a2d2010-12-22 21:39:39 +0000448 fReservedVertexBytes = 0;
449 fReservedIndexBytes = 0;
450 fUsedReservedVertexBytes = 0;
451 fUsedReservedIndexBytes = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000452 fCurrPoolVertexBuffer = 0;
453 fCurrPoolStartVertex = 0;
454
455}
456
457void GrInOrderDrawBuffer::setVertexSourceToArrayHelper(const void* vertexArray,
458 int vertexCount) {
459 GrAssert(!fReservedGeometry.fLocked || !fReservedGeometry.fVertexCount);
460#if GR_DEBUG
461 bool success =
462#endif
463 fVertexPool.appendVertices(fGeometrySrc.fVertexLayout,
464 vertexCount,
465 vertexArray,
466 &fCurrPoolVertexBuffer,
467 &fCurrPoolStartVertex);
468 GR_DEBUGASSERT(success);
469}
470
471void GrInOrderDrawBuffer::setIndexSourceToArrayHelper(const void* indexArray,
472 int indexCount) {
473 GrAssert(!fReservedGeometry.fLocked || !fReservedGeometry.fIndexCount);
474#if GR_DEBUG
475 bool success =
476#endif
477 fIndexPool.appendIndices(indexCount,
478 indexArray,
479 &fCurrPoolIndexBuffer,
480 &fCurrPoolStartIndex);
481 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000482}
483
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000484bool GrInOrderDrawBuffer::needsNewState() const {
485 if (fStates.empty()) {
486 return true;
487 } else {
488 const DrState& old = this->accessSavedDrawState(fStates.back());
489 return old != fCurrDrawState;
490 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000491}
492
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000493void GrInOrderDrawBuffer::pushState() {
494 for (int s = 0; s < kNumStages; ++s) {
495 GrSafeRef(fCurrDrawState.fTextures[s]);
496 }
bsalomon@google.com1da07462011-03-10 14:51:57 +0000497 GrSafeRef(fCurrDrawState.fRenderTarget);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000498 this->saveCurrentDrawState(&fStates.push_back());
499 }
bsalomon@google.comd302f142011-03-03 13:54:13 +0000500
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000501bool GrInOrderDrawBuffer::needsNewClip() const {
502 if (fCurrDrawState.fFlagBits & kClip_StateBit) {
503 if (fClips.empty() || (fClipSet && fClips.back() != fClip)) {
504 return true;
505 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000506 }
507 return false;
508}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000509
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000510void GrInOrderDrawBuffer::pushClip() {
511 fClips.push_back() = fClip;
512 fClipSet = false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000513}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000514
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000515void GrInOrderDrawBuffer::clipWillBeSet(const GrClip& newClip) {
516 fClipSet = true;
517}