reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 20 | #include "GrBufferAllocPool.h" |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 21 | #include "GrIndexBuffer.h" |
| 22 | #include "GrVertexBuffer.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 23 | #include "GrGpu.h" |
| 24 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 25 | GrInOrderDrawBuffer::GrInOrderDrawBuffer(GrVertexBufferAllocPool* vertexPool, |
| 26 | GrIndexBufferAllocPool* indexPool) : |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 27 | fDraws(DRAWS_BLOCK_SIZE, fDrawsStorage), |
| 28 | fStates(STATES_BLOCK_SIZE, fStatesStorage), |
| 29 | fClips(CLIPS_BLOCK_SIZE, fClipsStorage), |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 30 | fClipSet(true), |
| 31 | |
| 32 | fLastRectVertexLayout(0), |
| 33 | fQuadIndexBuffer(NULL), |
| 34 | fMaxQuads(0), |
| 35 | fCurrQuad(0), |
| 36 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 37 | fVertexPool(*vertexPool), |
| 38 | fCurrPoolVertexBuffer(NULL), |
| 39 | fCurrPoolStartVertex(0), |
| 40 | fIndexPool(*indexPool), |
| 41 | fCurrPoolIndexBuffer(NULL), |
| 42 | fCurrPoolStartIndex(0), |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 43 | fReservedVertexBytes(0), |
| 44 | fReservedIndexBytes(0), |
| 45 | fUsedReservedVertexBytes(0), |
| 46 | fUsedReservedIndexBytes(0) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 47 | GrAssert(NULL != vertexPool); |
| 48 | GrAssert(NULL != indexPool); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | GrInOrderDrawBuffer::~GrInOrderDrawBuffer() { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 52 | this->reset(); |
| 53 | GrSafeUnref(fQuadIndexBuffer); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | void GrInOrderDrawBuffer::initializeDrawStateAndClip(const GrDrawTarget& target) { |
| 57 | this->copyDrawState(target); |
| 58 | this->setClip(target.getClip()); |
| 59 | } |
| 60 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 61 | void 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.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 70 | GrAssert((NULL == indexBuffer && 0 == fMaxQuads) || |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 71 | (indexBuffer->maxQuads() == fMaxQuads)); |
| 72 | } |
| 73 | } |
| 74 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 75 | void GrInOrderDrawBuffer::drawRect(const GrRect& rect, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 76 | const GrMatrix* matrix, |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 77 | StageBitfield stageEnableBitfield, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 78 | const GrRect* srcRects[], |
| 79 | const GrMatrix* srcMatrices[]) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 80 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 81 | 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.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 88 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 89 | bool appendToPreviousDraw = false; |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 90 | GrVertexLayout layout = GetRectVertexLayout(stageEnableBitfield, srcRects); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 91 | 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.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 106 | |
| 107 | // single rect clip should have bounds |
| 108 | GrAssert(fClip.hasBounds()); |
| 109 | |
| 110 | GrRect clipRect = GrRect(fClip.getBounds()); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 111 | // If the clip rect touches the edge of the viewport, extended it |
| 112 | // out (close) to infinity to avoid bogus intersections. |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 113 | // We might consider a more exact clip to viewport if this |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 114 | // 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.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 142 | if (!needsNewClip() && !needsNewState() && fCurrQuad > 0 && |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 143 | fCurrQuad < fMaxQuads && layout == fLastRectVertexLayout) { |
| 144 | |
| 145 | int vsize = VertexSize(layout); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 146 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 147 | 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.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 174 | } else { |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 175 | INHERITED::drawRect(rect, matrix, stageEnableBitfield, srcRects, srcMatrices); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 179 | void GrInOrderDrawBuffer::drawIndexed(GrPrimitiveType primitiveType, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 180 | int startVertex, |
| 181 | int startIndex, |
| 182 | int vertexCount, |
| 183 | int indexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 184 | |
| 185 | if (!vertexCount || !indexCount) { |
| 186 | return; |
| 187 | } |
| 188 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 189 | fCurrQuad = 0; |
| 190 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 191 | Draw& draw = fDraws.push_back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 192 | draw.fPrimitiveType = primitiveType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 193 | draw.fStartVertex = startVertex; |
| 194 | draw.fStartIndex = startIndex; |
| 195 | draw.fVertexCount = vertexCount; |
| 196 | draw.fIndexCount = indexCount; |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 197 | |
| 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 207 | |
| 208 | draw.fVertexLayout = fGeometrySrc.fVertexLayout; |
| 209 | switch (fGeometrySrc.fVertexSrc) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 210 | case kBuffer_GeometrySrcType: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 211 | draw.fVertexBuffer = fGeometrySrc.fVertexBuffer; |
| 212 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 213 | case kReserved_GeometrySrcType: { |
| 214 | size_t vertexBytes = (vertexCount + startVertex) * |
| 215 | VertexSize(fGeometrySrc.fVertexLayout); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 216 | fUsedReservedVertexBytes = GrMax(fUsedReservedVertexBytes, vertexBytes); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 217 | } // 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 224 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 225 | draw.fVertexBuffer->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 226 | |
| 227 | switch (fGeometrySrc.fIndexSrc) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 228 | case kBuffer_GeometrySrcType: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 229 | draw.fIndexBuffer = fGeometrySrc.fIndexBuffer; |
| 230 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 231 | 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 241 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 242 | draw.fIndexBuffer->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 243 | } |
| 244 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 245 | void GrInOrderDrawBuffer::drawNonIndexed(GrPrimitiveType primitiveType, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 246 | int startVertex, |
| 247 | int vertexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 248 | if (!vertexCount) { |
| 249 | return; |
| 250 | } |
| 251 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 252 | fCurrQuad = 0; |
| 253 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 254 | Draw& draw = fDraws.push_back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 255 | draw.fPrimitiveType = primitiveType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 256 | draw.fStartVertex = startVertex; |
| 257 | draw.fStartIndex = 0; |
| 258 | draw.fVertexCount = vertexCount; |
| 259 | draw.fIndexCount = 0; |
| 260 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 261 | 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 270 | |
| 271 | draw.fVertexLayout = fGeometrySrc.fVertexLayout; |
| 272 | switch (fGeometrySrc.fVertexSrc) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 273 | case kBuffer_GeometrySrcType: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 274 | draw.fVertexBuffer = fGeometrySrc.fVertexBuffer; |
| 275 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 276 | 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 288 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 289 | draw.fVertexBuffer->ref(); |
| 290 | draw.fIndexBuffer = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void GrInOrderDrawBuffer::reset() { |
| 294 | GrAssert(!fReservedGeometry.fLocked); |
| 295 | uint32_t numStates = fStates.count(); |
| 296 | for (uint32_t i = 0; i < numStates; ++i) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 297 | for (int s = 0; s < kNumStages; ++s) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 298 | GrTexture* tex = this->accessSavedDrawState(fStates[i]).fTextures[s]; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 299 | if (NULL != tex) { |
| 300 | tex->unref(); |
| 301 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 302 | } |
| 303 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 304 | 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 311 | fDraws.reset(); |
| 312 | fStates.reset(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 313 | |
| 314 | fVertexPool.reset(); |
| 315 | fIndexPool.reset(); |
| 316 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 317 | fClips.reset(); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 318 | |
| 319 | fCurrQuad = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | void 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 331 | fVertexPool.unlock(); |
| 332 | fIndexPool.unlock(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 333 | |
| 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 345 | if (draw.fStateChanged) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 346 | ++currState; |
| 347 | target->restoreDrawState(fStates[currState]); |
| 348 | } |
| 349 | if (draw.fClipChanged) { |
| 350 | ++currClip; |
| 351 | target->setClip(fClips[currClip]); |
| 352 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 353 | uint32_t vertexReserveCount = 0; |
| 354 | uint32_t indexReserveCount = 0; |
| 355 | |
| 356 | target->setVertexSourceToBuffer(draw.fVertexLayout, draw.fVertexBuffer); |
| 357 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 358 | if (draw.fIndexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 359 | target->setIndexSourceToBuffer(draw.fIndexBuffer); |
| 360 | } |
| 361 | |
| 362 | if (draw.fIndexCount) { |
| 363 | target->drawIndexed(draw.fPrimitiveType, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 364 | draw.fStartVertex, |
| 365 | draw.fStartIndex, |
| 366 | draw.fVertexCount, |
| 367 | draw.fIndexCount); |
| 368 | } else { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 369 | target->drawNonIndexed(draw.fPrimitiveType, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 370 | draw.fStartVertex, |
| 371 | draw.fVertexCount); |
| 372 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 373 | if (vertexReserveCount || indexReserveCount) { |
| 374 | target->releaseReservedGeometry(); |
| 375 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
| 379 | bool GrInOrderDrawBuffer::geometryHints(GrVertexLayout vertexLayout, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 380 | 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 385 | bool flush = false; |
| 386 | if (NULL != indexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 387 | 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 395 | } |
| 396 | if (NULL != vertexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 397 | int32_t currVertices = fVertexPool.currentBufferVertices(vertexLayout); |
| 398 | if (*vertexCount > currVertices && |
| 399 | (!fVertexPool.preallocatedBuffersRemaining() && |
| 400 | *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexLayout))) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 401 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 402 | flush = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 403 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 404 | *vertexCount = currVertices; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 405 | } |
| 406 | return flush; |
| 407 | } |
| 408 | |
| 409 | bool GrInOrderDrawBuffer::acquireGeometryHelper(GrVertexLayout vertexLayout, |
| 410 | void** vertices, |
| 411 | void** indices) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 412 | GrAssert(!fReservedGeometry.fLocked); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 413 | if (fReservedGeometry.fVertexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 414 | GrAssert(NULL != vertices); |
| 415 | GrAssert(0 == fReservedVertexBytes); |
| 416 | GrAssert(0 == fUsedReservedVertexBytes); |
| 417 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 418 | fReservedVertexBytes = VertexSize(vertexLayout) * |
| 419 | fReservedGeometry.fVertexCount; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 420 | *vertices = fVertexPool.makeSpace(vertexLayout, |
| 421 | fReservedGeometry.fVertexCount, |
| 422 | &fCurrPoolVertexBuffer, |
| 423 | &fCurrPoolStartVertex); |
| 424 | if (NULL == *vertices) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 425 | return false; |
| 426 | } |
| 427 | } |
| 428 | if (fReservedGeometry.fIndexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 429 | 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 440 | return false; |
| 441 | } |
| 442 | } |
| 443 | return true; |
| 444 | } |
| 445 | |
| 446 | void GrInOrderDrawBuffer::releaseGeometryHelper() { |
| 447 | GrAssert(fUsedReservedVertexBytes <= fReservedVertexBytes); |
| 448 | GrAssert(fUsedReservedIndexBytes <= fReservedIndexBytes); |
| 449 | |
| 450 | size_t vertexSlack = fReservedVertexBytes - fUsedReservedVertexBytes; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 451 | fVertexPool.putBack(vertexSlack); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 452 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 453 | size_t indexSlack = fReservedIndexBytes - fUsedReservedIndexBytes; |
| 454 | fIndexPool.putBack(indexSlack); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 455 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 456 | fReservedVertexBytes = 0; |
| 457 | fReservedIndexBytes = 0; |
| 458 | fUsedReservedVertexBytes = 0; |
| 459 | fUsedReservedIndexBytes = 0; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 460 | fCurrPoolVertexBuffer = 0; |
| 461 | fCurrPoolStartVertex = 0; |
| 462 | |
| 463 | } |
| 464 | |
| 465 | void 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 | |
| 479 | void 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 490 | } |
| 491 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 492 | bool 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 499 | } |
| 500 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 501 | void 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.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 507 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 508 | bool GrInOrderDrawBuffer::needsNewClip() const { |
| 509 | if (fCurrDrawState.fFlagBits & kClip_StateBit) { |
| 510 | if (fClips.empty() || (fClipSet && fClips.back() != fClip)) { |
| 511 | return true; |
| 512 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 513 | } |
| 514 | return false; |
| 515 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 516 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 517 | void GrInOrderDrawBuffer::pushClip() { |
| 518 | fClips.push_back() = fClip; |
| 519 | fClipSet = false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 520 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 521 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 522 | void GrInOrderDrawBuffer::clipWillBeSet(const GrClip& newClip) { |
| 523 | fClipSet = true; |
| 524 | } |