epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #include "GrInOrderDrawBuffer.h" |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 12 | #include "GrRenderTarget.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 13 | #include "GrTexture.h" |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 14 | #include "GrBufferAllocPool.h" |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 15 | #include "GrIndexBuffer.h" |
| 16 | #include "GrVertexBuffer.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | #include "GrGpu.h" |
| 18 | |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 19 | GrInOrderDrawBuffer::GrInOrderDrawBuffer(const GrGpu* gpu, |
| 20 | GrVertexBufferAllocPool* vertexPool, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 21 | GrIndexBufferAllocPool* indexPool) |
bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 22 | : fAutoFlushTarget(NULL) |
| 23 | , fClipSet(true) |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 24 | , fLastRectVertexLayout(0) |
| 25 | , fQuadIndexBuffer(NULL) |
| 26 | , fMaxQuads(0) |
| 27 | , fCurrQuad(0) |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 28 | , fVertexPool(*vertexPool) |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 29 | , fIndexPool(*indexPool) { |
bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 30 | |
| 31 | fCaps = gpu->getCaps(); |
| 32 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 33 | GrAssert(NULL != vertexPool); |
| 34 | GrAssert(NULL != indexPool); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 35 | |
| 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 |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | GrInOrderDrawBuffer::~GrInOrderDrawBuffer() { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 48 | this->reset(); |
bsalomon@google.com | 4a018bb | 2011-10-28 19:50:21 +0000 | [diff] [blame] | 49 | // This must be called by before the GrDrawTarget destructor |
| 50 | this->releaseGeometry(); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 51 | GrSafeUnref(fQuadIndexBuffer); |
bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 52 | GrSafeUnref(fAutoFlushTarget); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void GrInOrderDrawBuffer::initializeDrawStateAndClip(const GrDrawTarget& target) { |
| 56 | this->copyDrawState(target); |
| 57 | this->setClip(target.getClip()); |
| 58 | } |
| 59 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 60 | void GrInOrderDrawBuffer::setQuadIndexBuffer(const GrIndexBuffer* indexBuffer) { |
| 61 | bool newIdxBuffer = fQuadIndexBuffer != indexBuffer; |
| 62 | if (newIdxBuffer) { |
| 63 | GrSafeUnref(fQuadIndexBuffer); |
| 64 | fQuadIndexBuffer = indexBuffer; |
| 65 | GrSafeRef(fQuadIndexBuffer); |
| 66 | fCurrQuad = 0; |
| 67 | fMaxQuads = (NULL == indexBuffer) ? 0 : indexBuffer->maxQuads(); |
| 68 | } else { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 69 | GrAssert((NULL == indexBuffer && 0 == fMaxQuads) || |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 70 | (indexBuffer->maxQuads() == fMaxQuads)); |
| 71 | } |
| 72 | } |
| 73 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 74 | void GrInOrderDrawBuffer::drawRect(const GrRect& rect, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 75 | const GrMatrix* matrix, |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 76 | StageMask stageMask, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 77 | const GrRect* srcRects[], |
| 78 | const GrMatrix* srcMatrices[]) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 79 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 80 | GrAssert(!(NULL == fQuadIndexBuffer && fCurrQuad)); |
| 81 | GrAssert(!(fDraws.empty() && fCurrQuad)); |
| 82 | GrAssert(!(0 != fMaxQuads && NULL == fQuadIndexBuffer)); |
| 83 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 84 | GrDrawState* drawState = this->drawState(); |
| 85 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 86 | // if we have a quad IB then either append to the previous run of |
| 87 | // rects or start a new run |
| 88 | if (fMaxQuads) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 89 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 90 | bool appendToPreviousDraw = false; |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 91 | GrVertexLayout layout = GetRectVertexLayout(stageMask, srcRects); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 92 | AutoReleaseGeometry geo(this, layout, 4, 0); |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 93 | if (!geo.succeeded()) { |
| 94 | GrPrintf("Failed to get space for vertices!\n"); |
| 95 | return; |
| 96 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 97 | GrMatrix combinedMatrix = drawState->getViewMatrix(); |
bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 98 | // We go to device space so that matrix changes allow us to concat |
| 99 | // rect draws. When the caller has provided explicit source rects |
| 100 | // then we don't want to modify the sampler matrices. Otherwise we do |
| 101 | // we have to account for the view matrix change in the sampler |
| 102 | // matrices. |
| 103 | StageMask devCoordMask = (NULL == srcRects) ? stageMask : 0; |
| 104 | GrDrawTarget::AutoDeviceCoordDraw adcd(this, devCoordMask); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 105 | if (NULL != matrix) { |
| 106 | combinedMatrix.preConcat(*matrix); |
| 107 | } |
| 108 | |
| 109 | SetRectVertices(rect, &combinedMatrix, srcRects, srcMatrices, layout, geo.vertices()); |
| 110 | |
| 111 | // we don't want to miss an opportunity to batch rects together |
| 112 | // simply because the clip has changed if the clip doesn't affect |
| 113 | // the rect. |
| 114 | bool disabledClip = false; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 115 | if (drawState->isClipState() && fClip.isRect()) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 116 | |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 117 | GrRect clipRect = fClip.getRect(0); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 118 | // If the clip rect touches the edge of the viewport, extended it |
| 119 | // out (close) to infinity to avoid bogus intersections. |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 120 | // We might consider a more exact clip to viewport if this |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 121 | // conservative test fails. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 122 | const GrRenderTarget* target = drawState->getRenderTarget(); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 123 | if (0 >= clipRect.fLeft) { |
| 124 | clipRect.fLeft = GR_ScalarMin; |
| 125 | } |
| 126 | if (target->width() <= clipRect.fRight) { |
| 127 | clipRect.fRight = GR_ScalarMax; |
| 128 | } |
| 129 | if (0 >= clipRect.top()) { |
| 130 | clipRect.fTop = GR_ScalarMin; |
| 131 | } |
| 132 | if (target->height() <= clipRect.fBottom) { |
| 133 | clipRect.fBottom = GR_ScalarMax; |
| 134 | } |
| 135 | int stride = VertexSize(layout); |
| 136 | bool insideClip = true; |
| 137 | for (int v = 0; v < 4; ++v) { |
| 138 | const GrPoint& p = *GetVertexPoint(geo.vertices(), v, stride); |
| 139 | if (!clipRect.contains(p)) { |
| 140 | insideClip = false; |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | if (insideClip) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 145 | drawState->disableState(GrDrawState::kClip_StateBit); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 146 | disabledClip = true; |
| 147 | } |
| 148 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 149 | if (!needsNewClip() && !needsNewState() && fCurrQuad > 0 && |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 150 | fCurrQuad < fMaxQuads && layout == fLastRectVertexLayout) { |
| 151 | |
| 152 | int vsize = VertexSize(layout); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 153 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 154 | Draw& lastDraw = fDraws.back(); |
| 155 | |
| 156 | GrAssert(lastDraw.fIndexBuffer == fQuadIndexBuffer); |
| 157 | GrAssert(kTriangles_PrimitiveType == lastDraw.fPrimitiveType); |
| 158 | GrAssert(0 == lastDraw.fVertexCount % 4); |
| 159 | GrAssert(0 == lastDraw.fIndexCount % 6); |
| 160 | GrAssert(0 == lastDraw.fStartIndex); |
| 161 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 162 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 163 | bool clearSinceLastDraw = |
| 164 | fClears.count() && |
| 165 | fClears.back().fBeforeDrawIdx == fDraws.count(); |
| 166 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 167 | appendToPreviousDraw = |
| 168 | !clearSinceLastDraw && |
| 169 | lastDraw.fVertexBuffer == poolState.fPoolVertexBuffer && |
| 170 | (fCurrQuad * 4 + lastDraw.fStartVertex) == poolState.fPoolStartVertex; |
| 171 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 172 | if (appendToPreviousDraw) { |
| 173 | lastDraw.fVertexCount += 4; |
| 174 | lastDraw.fIndexCount += 6; |
| 175 | fCurrQuad += 1; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 176 | // we reserved above, so we should be the first |
| 177 | // use of this vertex reserveation. |
| 178 | GrAssert(0 == poolState.fUsedPoolVertexBytes); |
| 179 | poolState.fUsedPoolVertexBytes = 4 * vsize; |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | if (!appendToPreviousDraw) { |
| 183 | this->setIndexSourceToBuffer(fQuadIndexBuffer); |
bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 184 | this->drawIndexed(kTriangles_PrimitiveType, 0, 0, 4, 6); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 185 | fCurrQuad = 1; |
| 186 | fLastRectVertexLayout = layout; |
| 187 | } |
| 188 | if (disabledClip) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 189 | drawState->enableState(GrDrawState::kClip_StateBit); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 190 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 191 | } else { |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 192 | INHERITED::drawRect(rect, matrix, stageMask, srcRects, srcMatrices); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 196 | void GrInOrderDrawBuffer::onDrawIndexed(GrPrimitiveType primitiveType, |
| 197 | int startVertex, |
| 198 | int startIndex, |
| 199 | int vertexCount, |
| 200 | int indexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 201 | |
| 202 | if (!vertexCount || !indexCount) { |
| 203 | return; |
| 204 | } |
| 205 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 206 | fCurrQuad = 0; |
| 207 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 208 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 209 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 210 | Draw& draw = fDraws.push_back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 211 | draw.fPrimitiveType = primitiveType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 212 | draw.fStartVertex = startVertex; |
| 213 | draw.fStartIndex = startIndex; |
| 214 | draw.fVertexCount = vertexCount; |
| 215 | draw.fIndexCount = indexCount; |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 216 | |
| 217 | draw.fClipChanged = this->needsNewClip(); |
| 218 | if (draw.fClipChanged) { |
| 219 | this->pushClip(); |
| 220 | } |
| 221 | |
| 222 | draw.fStateChanged = this->needsNewState(); |
| 223 | if (draw.fStateChanged) { |
| 224 | this->pushState(); |
| 225 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 226 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 227 | draw.fVertexLayout = this->getGeomSrc().fVertexLayout; |
| 228 | switch (this->getGeomSrc().fVertexSrc) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 229 | case kBuffer_GeometrySrcType: |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 230 | draw.fVertexBuffer = this->getGeomSrc().fVertexBuffer; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 231 | break; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 232 | case kReserved_GeometrySrcType: // fallthrough |
| 233 | case kArray_GeometrySrcType: { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 234 | size_t vertexBytes = (vertexCount + startVertex) * |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 235 | VertexSize(this->getGeomSrc().fVertexLayout); |
| 236 | poolState.fUsedPoolVertexBytes = |
| 237 | GrMax(poolState.fUsedPoolVertexBytes, vertexBytes); |
| 238 | draw.fVertexBuffer = poolState.fPoolVertexBuffer; |
| 239 | draw.fStartVertex += poolState.fPoolStartVertex; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 240 | break; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 241 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 242 | default: |
| 243 | GrCrash("unknown geom src type"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 244 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 245 | draw.fVertexBuffer->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 246 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 247 | switch (this->getGeomSrc().fIndexSrc) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 248 | case kBuffer_GeometrySrcType: |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 249 | draw.fIndexBuffer = this->getGeomSrc().fIndexBuffer; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 250 | break; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 251 | case kReserved_GeometrySrcType: // fallthrough |
| 252 | case kArray_GeometrySrcType: { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 253 | size_t indexBytes = (indexCount + startIndex) * sizeof(uint16_t); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 254 | poolState.fUsedPoolIndexBytes = |
| 255 | GrMax(poolState.fUsedPoolIndexBytes, indexBytes); |
| 256 | draw.fIndexBuffer = poolState.fPoolIndexBuffer; |
bsalomon@google.com | d127ffe | 2012-02-24 20:11:52 +0000 | [diff] [blame] | 257 | draw.fStartIndex += poolState.fPoolStartIndex; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 258 | break; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 259 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 260 | default: |
| 261 | GrCrash("unknown geom src type"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 262 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 263 | draw.fIndexBuffer->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 264 | } |
| 265 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 266 | void GrInOrderDrawBuffer::onDrawNonIndexed(GrPrimitiveType primitiveType, |
| 267 | int startVertex, |
| 268 | int vertexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 269 | if (!vertexCount) { |
| 270 | return; |
| 271 | } |
| 272 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 273 | fCurrQuad = 0; |
| 274 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 275 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 276 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 277 | Draw& draw = fDraws.push_back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 278 | draw.fPrimitiveType = primitiveType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 279 | draw.fStartVertex = startVertex; |
| 280 | draw.fStartIndex = 0; |
| 281 | draw.fVertexCount = vertexCount; |
| 282 | draw.fIndexCount = 0; |
| 283 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 284 | draw.fClipChanged = this->needsNewClip(); |
| 285 | if (draw.fClipChanged) { |
| 286 | this->pushClip(); |
| 287 | } |
| 288 | |
| 289 | draw.fStateChanged = this->needsNewState(); |
| 290 | if (draw.fStateChanged) { |
| 291 | this->pushState(); |
| 292 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 293 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 294 | draw.fVertexLayout = this->getGeomSrc().fVertexLayout; |
| 295 | switch (this->getGeomSrc().fVertexSrc) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 296 | case kBuffer_GeometrySrcType: |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 297 | draw.fVertexBuffer = this->getGeomSrc().fVertexBuffer; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 298 | break; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 299 | case kReserved_GeometrySrcType: // fallthrough |
| 300 | case kArray_GeometrySrcType: { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 301 | size_t vertexBytes = (vertexCount + startVertex) * |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 302 | VertexSize(this->getGeomSrc().fVertexLayout); |
| 303 | poolState.fUsedPoolVertexBytes = |
| 304 | GrMax(poolState.fUsedPoolVertexBytes, vertexBytes); |
| 305 | draw.fVertexBuffer = poolState.fPoolVertexBuffer; |
| 306 | draw.fStartVertex += poolState.fPoolStartVertex; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 307 | break; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 308 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 309 | default: |
| 310 | GrCrash("unknown geom src type"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 311 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 312 | draw.fVertexBuffer->ref(); |
| 313 | draw.fIndexBuffer = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 314 | } |
| 315 | |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 316 | void GrInOrderDrawBuffer::clear(const GrIRect* rect, GrColor color) { |
| 317 | GrIRect r; |
| 318 | if (NULL == rect) { |
| 319 | // We could do something smart and remove previous draws and clears to |
| 320 | // the current render target. If we get that smart we have to make sure |
| 321 | // those draws aren't read before this clear (render-to-texture). |
| 322 | r.setLTRB(0, 0, |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 323 | this->getDrawState().getRenderTarget()->width(), |
| 324 | this->getDrawState().getRenderTarget()->height()); |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 325 | rect = &r; |
| 326 | } |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 327 | Clear& clr = fClears.push_back(); |
| 328 | clr.fColor = color; |
| 329 | clr.fBeforeDrawIdx = fDraws.count(); |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 330 | clr.fRect = *rect; |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 331 | } |
| 332 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 333 | void GrInOrderDrawBuffer::reset() { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 334 | GrAssert(1 == fGeoPoolStateStack.count()); |
| 335 | this->resetVertexSource(); |
| 336 | this->resetIndexSource(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 337 | uint32_t numStates = fStates.count(); |
| 338 | for (uint32_t i = 0; i < numStates; ++i) { |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 339 | const GrDrawState& dstate = this->accessSavedDrawState(fStates[i]); |
| 340 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 341 | GrSafeUnref(dstate.getTexture(s)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 342 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 343 | GrSafeUnref(dstate.getRenderTarget()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 344 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 345 | int numDraws = fDraws.count(); |
| 346 | for (int d = 0; d < numDraws; ++d) { |
| 347 | // we always have a VB, but not always an IB |
| 348 | GrAssert(NULL != fDraws[d].fVertexBuffer); |
| 349 | fDraws[d].fVertexBuffer->unref(); |
| 350 | GrSafeUnref(fDraws[d].fIndexBuffer); |
| 351 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 352 | fDraws.reset(); |
| 353 | fStates.reset(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 354 | |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 355 | fClears.reset(); |
| 356 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 357 | fVertexPool.reset(); |
| 358 | fIndexPool.reset(); |
| 359 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 360 | fClips.reset(); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 361 | |
| 362 | fCurrQuad = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | void GrInOrderDrawBuffer::playback(GrDrawTarget* target) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 366 | GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fVertexSrc); |
| 367 | GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fIndexSrc); |
bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 368 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 369 | GrAssert(NULL != target); |
| 370 | GrAssert(target != this); // not considered and why? |
| 371 | |
bsalomon@google.com | 898d9e5 | 2011-04-26 13:22:33 +0000 | [diff] [blame] | 372 | int numDraws = fDraws.count(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 373 | if (!numDraws) { |
| 374 | return; |
| 375 | } |
| 376 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 377 | fVertexPool.unlock(); |
| 378 | fIndexPool.unlock(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 379 | |
| 380 | GrDrawTarget::AutoStateRestore asr(target); |
| 381 | GrDrawTarget::AutoClipRestore acr(target); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 382 | AutoGeometryPush agp(target); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 383 | |
bsalomon@google.com | 6a77cc5 | 2011-04-28 17:33:34 +0000 | [diff] [blame] | 384 | int currState = ~0; |
| 385 | int currClip = ~0; |
| 386 | int currClear = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 387 | |
bsalomon@google.com | 898d9e5 | 2011-04-26 13:22:33 +0000 | [diff] [blame] | 388 | for (int i = 0; i < numDraws; ++i) { |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 389 | while (currClear < fClears.count() && |
| 390 | i == fClears[currClear].fBeforeDrawIdx) { |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 391 | target->clear(&fClears[currClear].fRect, fClears[currClear].fColor); |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 392 | ++currClear; |
| 393 | } |
| 394 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 395 | const Draw& draw = fDraws[i]; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 396 | if (draw.fStateChanged) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 397 | ++currState; |
| 398 | target->restoreDrawState(fStates[currState]); |
| 399 | } |
| 400 | if (draw.fClipChanged) { |
| 401 | ++currClip; |
| 402 | target->setClip(fClips[currClip]); |
| 403 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 404 | |
| 405 | target->setVertexSourceToBuffer(draw.fVertexLayout, draw.fVertexBuffer); |
| 406 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 407 | if (draw.fIndexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 408 | target->setIndexSourceToBuffer(draw.fIndexBuffer); |
| 409 | } |
| 410 | |
| 411 | if (draw.fIndexCount) { |
| 412 | target->drawIndexed(draw.fPrimitiveType, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 413 | draw.fStartVertex, |
| 414 | draw.fStartIndex, |
| 415 | draw.fVertexCount, |
| 416 | draw.fIndexCount); |
| 417 | } else { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 418 | target->drawNonIndexed(draw.fPrimitiveType, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 419 | draw.fStartVertex, |
| 420 | draw.fVertexCount); |
| 421 | } |
| 422 | } |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 423 | while (currClear < fClears.count()) { |
| 424 | GrAssert(fDraws.count() == fClears[currClear].fBeforeDrawIdx); |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 425 | target->clear(&fClears[currClear].fRect, fClears[currClear].fColor); |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 426 | ++currClear; |
| 427 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 428 | } |
| 429 | |
bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 430 | void GrInOrderDrawBuffer::setAutoFlushTarget(GrDrawTarget* target) { |
| 431 | GrSafeAssign(fAutoFlushTarget, target); |
| 432 | } |
| 433 | |
| 434 | void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace( |
| 435 | GrVertexLayout vertexLayout, |
| 436 | int vertexCount, |
| 437 | int indexCount) { |
| 438 | if (NULL != fAutoFlushTarget) { |
| 439 | // We use geometryHints() to know whether to flush the draw buffer. We |
| 440 | // can't flush if we are inside an unbalanced pushGeometrySource. |
| 441 | // Moreover, flushing blows away vertex and index data that was |
| 442 | // previously reserved. So if the vertex or index data is pulled from |
| 443 | // reserved space and won't be released by this request then we can't |
| 444 | // flush. |
| 445 | bool insideGeoPush = fGeoPoolStateStack.count() > 1; |
| 446 | |
| 447 | bool unreleasedVertexSpace = |
| 448 | !vertexCount && |
| 449 | kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc; |
| 450 | |
| 451 | bool unreleasedIndexSpace = |
| 452 | !indexCount && |
| 453 | kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc; |
| 454 | |
| 455 | // we don't want to finalize any reserved geom on the target since |
| 456 | // we don't know that the client has finished writing to it. |
| 457 | bool targetHasReservedGeom = |
| 458 | fAutoFlushTarget->hasReservedVerticesOrIndices(); |
| 459 | |
| 460 | int vcount = vertexCount; |
| 461 | int icount = indexCount; |
| 462 | |
| 463 | if (!insideGeoPush && |
| 464 | !unreleasedVertexSpace && |
| 465 | !unreleasedIndexSpace && |
| 466 | !targetHasReservedGeom && |
| 467 | this->geometryHints(vertexLayout, &vcount, &icount)) { |
| 468 | |
| 469 | this->flushTo(fAutoFlushTarget); |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 474 | bool GrInOrderDrawBuffer::geometryHints(GrVertexLayout vertexLayout, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 475 | int* vertexCount, |
| 476 | int* indexCount) const { |
| 477 | // we will recommend a flush if the data could fit in a single |
| 478 | // preallocated buffer but none are left and it can't fit |
| 479 | // in the current buffer (which may not be prealloced). |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 480 | bool flush = false; |
| 481 | if (NULL != indexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 482 | int32_t currIndices = fIndexPool.currentBufferIndices(); |
| 483 | if (*indexCount > currIndices && |
| 484 | (!fIndexPool.preallocatedBuffersRemaining() && |
| 485 | *indexCount <= fIndexPool.preallocatedBufferIndices())) { |
| 486 | |
| 487 | flush = true; |
| 488 | } |
| 489 | *indexCount = currIndices; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 490 | } |
| 491 | if (NULL != vertexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 492 | int32_t currVertices = fVertexPool.currentBufferVertices(vertexLayout); |
| 493 | if (*vertexCount > currVertices && |
| 494 | (!fVertexPool.preallocatedBuffersRemaining() && |
| 495 | *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexLayout))) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 496 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 497 | flush = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 498 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 499 | *vertexCount = currVertices; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 500 | } |
| 501 | return flush; |
| 502 | } |
| 503 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 504 | bool GrInOrderDrawBuffer::onReserveVertexSpace(GrVertexLayout vertexLayout, |
| 505 | int vertexCount, |
| 506 | void** vertices) { |
| 507 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 508 | GrAssert(vertexCount > 0); |
| 509 | GrAssert(NULL != vertices); |
| 510 | GrAssert(0 == poolState.fUsedPoolVertexBytes); |
| 511 | |
| 512 | *vertices = fVertexPool.makeSpace(vertexLayout, |
| 513 | vertexCount, |
| 514 | &poolState.fPoolVertexBuffer, |
| 515 | &poolState.fPoolStartVertex); |
| 516 | return NULL != *vertices; |
| 517 | } |
| 518 | |
| 519 | bool GrInOrderDrawBuffer::onReserveIndexSpace(int indexCount, void** indices) { |
| 520 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 521 | GrAssert(indexCount > 0); |
| 522 | GrAssert(NULL != indices); |
| 523 | GrAssert(0 == poolState.fUsedPoolIndexBytes); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 524 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 525 | *indices = fIndexPool.makeSpace(indexCount, |
| 526 | &poolState.fPoolIndexBuffer, |
| 527 | &poolState.fPoolStartIndex); |
| 528 | return NULL != *indices; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 529 | } |
| 530 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 531 | void GrInOrderDrawBuffer::releaseReservedVertexSpace() { |
| 532 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 533 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 534 | |
| 535 | GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc); |
bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 536 | |
| 537 | // When the caller reserved vertex buffer space we gave it back a pointer |
| 538 | // provided by the vertex buffer pool. At each draw we tracked the largest |
| 539 | // offset into the pool's pointer that was referenced. Now we return to the |
| 540 | // pool any portion at the tail of the allocation that no draw referenced. |
| 541 | size_t reservedVertexBytes = VertexSize(geoSrc.fVertexLayout) * |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 542 | geoSrc.fVertexCount; |
| 543 | fVertexPool.putBack(reservedVertexBytes - |
| 544 | poolState.fUsedPoolVertexBytes); |
| 545 | poolState.fUsedPoolVertexBytes = 0; |
bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 546 | poolState.fPoolVertexBuffer = NULL; |
| 547 | poolState.fPoolStartVertex = 0; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 548 | } |
| 549 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 550 | void GrInOrderDrawBuffer::releaseReservedIndexSpace() { |
| 551 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 552 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| 553 | |
| 554 | GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc); |
bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 555 | |
| 556 | // Similar to releaseReservedVertexSpace we return any unused portion at |
| 557 | // the tail |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 558 | size_t reservedIndexBytes = sizeof(uint16_t) * geoSrc.fIndexCount; |
| 559 | fIndexPool.putBack(reservedIndexBytes - poolState.fUsedPoolIndexBytes); |
| 560 | poolState.fUsedPoolIndexBytes = 0; |
bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 561 | poolState.fPoolIndexBuffer = NULL; |
| 562 | poolState.fPoolStartIndex = 0; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 563 | } |
| 564 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 565 | void GrInOrderDrawBuffer::onSetVertexSourceToArray(const void* vertexArray, |
| 566 | int vertexCount) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 567 | |
| 568 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 569 | GrAssert(0 == poolState.fUsedPoolVertexBytes); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 570 | #if GR_DEBUG |
| 571 | bool success = |
| 572 | #endif |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 573 | fVertexPool.appendVertices(this->getGeomSrc().fVertexLayout, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 574 | vertexCount, |
| 575 | vertexArray, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 576 | &poolState.fPoolVertexBuffer, |
| 577 | &poolState.fPoolStartVertex); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 578 | GR_DEBUGASSERT(success); |
| 579 | } |
| 580 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 581 | void GrInOrderDrawBuffer::onSetIndexSourceToArray(const void* indexArray, |
| 582 | int indexCount) { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 583 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 584 | GrAssert(0 == poolState.fUsedPoolIndexBytes); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 585 | #if GR_DEBUG |
| 586 | bool success = |
| 587 | #endif |
| 588 | fIndexPool.appendIndices(indexCount, |
| 589 | indexArray, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 590 | &poolState.fPoolIndexBuffer, |
| 591 | &poolState.fPoolStartIndex); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 592 | GR_DEBUGASSERT(success); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 593 | } |
| 594 | |
bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 595 | void GrInOrderDrawBuffer::releaseVertexArray() { |
| 596 | // When the client provides an array as the vertex source we handled it |
| 597 | // by copying their array into reserved space. |
| 598 | this->GrInOrderDrawBuffer::releaseReservedVertexSpace(); |
| 599 | } |
| 600 | |
| 601 | void GrInOrderDrawBuffer::releaseIndexArray() { |
| 602 | // When the client provides an array as the index source we handled it |
| 603 | // by copying their array into reserved space. |
| 604 | this->GrInOrderDrawBuffer::releaseReservedIndexSpace(); |
| 605 | } |
| 606 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 607 | void GrInOrderDrawBuffer::geometrySourceWillPush() { |
| 608 | GeometryPoolState& poolState = fGeoPoolStateStack.push_back(); |
| 609 | poolState.fUsedPoolVertexBytes = 0; |
| 610 | poolState.fUsedPoolIndexBytes = 0; |
| 611 | #if GR_DEBUG |
| 612 | poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0; |
| 613 | poolState.fPoolStartVertex = ~0; |
| 614 | poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0; |
| 615 | poolState.fPoolStartIndex = ~0; |
| 616 | #endif |
| 617 | } |
| 618 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 619 | void GrInOrderDrawBuffer::geometrySourceWillPop( |
| 620 | const GeometrySrcState& restoredState) { |
| 621 | GrAssert(fGeoPoolStateStack.count() > 1); |
| 622 | fGeoPoolStateStack.pop_back(); |
| 623 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 624 | // we have to assume that any slack we had in our vertex/index data |
| 625 | // is now unreleasable because data may have been appended later in the |
| 626 | // pool. |
| 627 | if (kReserved_GeometrySrcType == restoredState.fVertexSrc || |
| 628 | kArray_GeometrySrcType == restoredState.fVertexSrc) { |
| 629 | poolState.fUsedPoolVertexBytes = |
| 630 | VertexSize(restoredState.fVertexLayout) * |
| 631 | restoredState.fVertexCount; |
| 632 | } |
| 633 | if (kReserved_GeometrySrcType == restoredState.fIndexSrc || |
| 634 | kArray_GeometrySrcType == restoredState.fIndexSrc) { |
bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 635 | poolState.fUsedPoolIndexBytes = sizeof(uint16_t) * |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 636 | restoredState.fIndexCount; |
| 637 | } |
| 638 | } |
| 639 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 640 | bool GrInOrderDrawBuffer::needsNewState() const { |
| 641 | if (fStates.empty()) { |
| 642 | return true; |
| 643 | } else { |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 644 | const GrDrawState& old = this->accessSavedDrawState(fStates.back()); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 645 | return old != fCurrDrawState; |
| 646 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 647 | } |
| 648 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 649 | void GrInOrderDrawBuffer::pushState() { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 650 | const GrDrawState& drawState = this->getDrawState(); |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 651 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 652 | GrSafeRef(drawState.getTexture(s)); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 653 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 654 | GrSafeRef(drawState.getRenderTarget()); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 655 | this->saveCurrentDrawState(&fStates.push_back()); |
| 656 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 657 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 658 | bool GrInOrderDrawBuffer::needsNewClip() const { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 659 | if (this->getDrawState().isClipState()) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 660 | if (fClips.empty() || (fClipSet && fClips.back() != fClip)) { |
| 661 | return true; |
| 662 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 663 | } |
| 664 | return false; |
| 665 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 666 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 667 | void GrInOrderDrawBuffer::pushClip() { |
| 668 | fClips.push_back() = fClip; |
| 669 | fClipSet = false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 670 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 671 | |
bsalomon@google.com | dea2f8d | 2011-08-01 15:51:05 +0000 | [diff] [blame] | 672 | void GrInOrderDrawBuffer::clipWillBeSet(const GrClip& newClip) { |
| 673 | INHERITED::clipWillBeSet(newClip); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 674 | fClipSet = true; |
| 675 | } |