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 { |
| 70 | GrAssert((NULL == indexBuffer && 0 == fMaxQuads) || |
| 71 | (indexBuffer->maxQuads() == fMaxQuads)); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void GrInOrderDrawBuffer::drawRect(const GrRect& rect, |
| 76 | const GrMatrix* matrix, |
| 77 | int stageEnableMask, |
| 78 | const GrRect* srcRects[], |
| 79 | const GrMatrix* srcMatrices[]) { |
| 80 | |
| 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) { |
| 88 | |
| 89 | bool appendToPreviousDraw = false; |
| 90 | GrVertexLayout layout = GetRectVertexLayout(stageEnableMask, srcRects); |
| 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()) { |
| 106 | GrRect clipRect = GrRect(*fClip.getRects()); |
| 107 | // If the clip rect touches the edge of the viewport, extended it |
| 108 | // out (close) to infinity to avoid bogus intersections. |
| 109 | // We might consider a more exact clip to viewport if this |
| 110 | // conservative test fails. |
| 111 | const GrRenderTarget* target = this->getRenderTarget(); |
| 112 | if (0 >= clipRect.fLeft) { |
| 113 | clipRect.fLeft = GR_ScalarMin; |
| 114 | } |
| 115 | if (target->width() <= clipRect.fRight) { |
| 116 | clipRect.fRight = GR_ScalarMax; |
| 117 | } |
| 118 | if (0 >= clipRect.top()) { |
| 119 | clipRect.fTop = GR_ScalarMin; |
| 120 | } |
| 121 | if (target->height() <= clipRect.fBottom) { |
| 122 | clipRect.fBottom = GR_ScalarMax; |
| 123 | } |
| 124 | int stride = VertexSize(layout); |
| 125 | bool insideClip = true; |
| 126 | for (int v = 0; v < 4; ++v) { |
| 127 | const GrPoint& p = *GetVertexPoint(geo.vertices(), v, stride); |
| 128 | if (!clipRect.contains(p)) { |
| 129 | insideClip = false; |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | if (insideClip) { |
| 134 | this->disableState(kClip_StateBit); |
| 135 | disabledClip = true; |
| 136 | } |
| 137 | } |
| 138 | if (!needsNewClip() && !needsNewState() && fCurrQuad > 0 && |
| 139 | fCurrQuad < fMaxQuads && layout == fLastRectVertexLayout) { |
| 140 | |
| 141 | int vsize = VertexSize(layout); |
| 142 | |
| 143 | Draw& lastDraw = fDraws.back(); |
| 144 | |
| 145 | GrAssert(lastDraw.fIndexBuffer == fQuadIndexBuffer); |
| 146 | GrAssert(kTriangles_PrimitiveType == lastDraw.fPrimitiveType); |
| 147 | GrAssert(0 == lastDraw.fVertexCount % 4); |
| 148 | GrAssert(0 == lastDraw.fIndexCount % 6); |
| 149 | GrAssert(0 == lastDraw.fStartIndex); |
| 150 | |
| 151 | appendToPreviousDraw = lastDraw.fVertexBuffer == fCurrPoolVertexBuffer && |
| 152 | (fCurrQuad * 4 + lastDraw.fStartVertex) == fCurrPoolStartVertex; |
| 153 | if (appendToPreviousDraw) { |
| 154 | lastDraw.fVertexCount += 4; |
| 155 | lastDraw.fIndexCount += 6; |
| 156 | fCurrQuad += 1; |
| 157 | GrAssert(0 == fUsedReservedVertexBytes); |
| 158 | fUsedReservedVertexBytes = 4 * vsize; |
| 159 | } |
| 160 | } |
| 161 | if (!appendToPreviousDraw) { |
| 162 | this->setIndexSourceToBuffer(fQuadIndexBuffer); |
| 163 | drawIndexed(kTriangles_PrimitiveType, 0, 0, 4, 6); |
| 164 | fCurrQuad = 1; |
| 165 | fLastRectVertexLayout = layout; |
| 166 | } |
| 167 | if (disabledClip) { |
| 168 | this->enableState(kClip_StateBit); |
| 169 | } |
| 170 | this->enableState(kClip_StateBit); |
| 171 | } else { |
| 172 | INHERITED::drawRect(rect, matrix, stageEnableMask, srcRects, srcMatrices); |
| 173 | } |
| 174 | } |
| 175 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 176 | void GrInOrderDrawBuffer::drawIndexed(PrimitiveType primitiveType, |
| 177 | int startVertex, |
| 178 | int startIndex, |
| 179 | int vertexCount, |
| 180 | int indexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 181 | |
| 182 | if (!vertexCount || !indexCount) { |
| 183 | return; |
| 184 | } |
| 185 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 186 | fCurrQuad = 0; |
| 187 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 188 | Draw& draw = fDraws.push_back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 189 | draw.fPrimitiveType = primitiveType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 190 | draw.fStartVertex = startVertex; |
| 191 | draw.fStartIndex = startIndex; |
| 192 | draw.fVertexCount = vertexCount; |
| 193 | draw.fIndexCount = indexCount; |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 194 | |
| 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 204 | |
| 205 | draw.fVertexLayout = fGeometrySrc.fVertexLayout; |
| 206 | switch (fGeometrySrc.fVertexSrc) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 207 | case kBuffer_GeometrySrcType: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 208 | draw.fVertexBuffer = fGeometrySrc.fVertexBuffer; |
| 209 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 210 | case kReserved_GeometrySrcType: { |
| 211 | size_t vertexBytes = (vertexCount + startVertex) * |
| 212 | VertexSize(fGeometrySrc.fVertexLayout); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 213 | fUsedReservedVertexBytes = GrMax(fUsedReservedVertexBytes, vertexBytes); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 214 | } // 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 221 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 222 | draw.fVertexBuffer->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 223 | |
| 224 | switch (fGeometrySrc.fIndexSrc) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 225 | case kBuffer_GeometrySrcType: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 226 | draw.fIndexBuffer = fGeometrySrc.fIndexBuffer; |
| 227 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 228 | 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 238 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 239 | draw.fIndexBuffer->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 240 | } |
| 241 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 242 | void GrInOrderDrawBuffer::drawNonIndexed(PrimitiveType primitiveType, |
| 243 | int startVertex, |
| 244 | int vertexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 245 | if (!vertexCount) { |
| 246 | return; |
| 247 | } |
| 248 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 249 | fCurrQuad = 0; |
| 250 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 251 | Draw& draw = fDraws.push_back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 252 | draw.fPrimitiveType = primitiveType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 253 | draw.fStartVertex = startVertex; |
| 254 | draw.fStartIndex = 0; |
| 255 | draw.fVertexCount = vertexCount; |
| 256 | draw.fIndexCount = 0; |
| 257 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 258 | 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 267 | |
| 268 | draw.fVertexLayout = fGeometrySrc.fVertexLayout; |
| 269 | switch (fGeometrySrc.fVertexSrc) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 270 | case kBuffer_GeometrySrcType: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 271 | draw.fVertexBuffer = fGeometrySrc.fVertexBuffer; |
| 272 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 273 | 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 285 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 286 | draw.fVertexBuffer->ref(); |
| 287 | draw.fIndexBuffer = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void GrInOrderDrawBuffer::reset() { |
| 291 | GrAssert(!fReservedGeometry.fLocked); |
| 292 | uint32_t numStates = fStates.count(); |
| 293 | for (uint32_t i = 0; i < numStates; ++i) { |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 294 | for (int s = 0; s < kNumStages; ++s) { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 295 | GrTexture* tex = this->accessSavedDrawState(fStates[i]).fTextures[s]; |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 296 | if (NULL != tex) { |
| 297 | tex->unref(); |
| 298 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 301 | int numDraws = fDraws.count(); |
| 302 | for (int d = 0; d < numDraws; ++d) { |
| 303 | // we always have a VB, but not always an IB |
| 304 | GrAssert(NULL != fDraws[d].fVertexBuffer); |
| 305 | fDraws[d].fVertexBuffer->unref(); |
| 306 | GrSafeUnref(fDraws[d].fIndexBuffer); |
| 307 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 308 | fDraws.reset(); |
| 309 | fStates.reset(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 310 | |
| 311 | fVertexPool.reset(); |
| 312 | fIndexPool.reset(); |
| 313 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 314 | fClips.reset(); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 315 | |
| 316 | fCurrQuad = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | void GrInOrderDrawBuffer::playback(GrDrawTarget* target) { |
| 320 | 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 328 | fVertexPool.unlock(); |
| 329 | fIndexPool.unlock(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 330 | |
| 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 342 | if (draw.fStateChanged) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 343 | ++currState; |
| 344 | target->restoreDrawState(fStates[currState]); |
| 345 | } |
| 346 | if (draw.fClipChanged) { |
| 347 | ++currClip; |
| 348 | target->setClip(fClips[currClip]); |
| 349 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 350 | uint32_t vertexReserveCount = 0; |
| 351 | uint32_t indexReserveCount = 0; |
| 352 | |
| 353 | target->setVertexSourceToBuffer(draw.fVertexLayout, draw.fVertexBuffer); |
| 354 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 355 | if (draw.fIndexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 356 | target->setIndexSourceToBuffer(draw.fIndexBuffer); |
| 357 | } |
| 358 | |
| 359 | if (draw.fIndexCount) { |
| 360 | target->drawIndexed(draw.fPrimitiveType, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 361 | draw.fStartVertex, |
| 362 | draw.fStartIndex, |
| 363 | draw.fVertexCount, |
| 364 | draw.fIndexCount); |
| 365 | } else { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 366 | target->drawNonIndexed(draw.fPrimitiveType, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 367 | draw.fStartVertex, |
| 368 | draw.fVertexCount); |
| 369 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 370 | if (vertexReserveCount || indexReserveCount) { |
| 371 | target->releaseReservedGeometry(); |
| 372 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | bool GrInOrderDrawBuffer::geometryHints(GrVertexLayout vertexLayout, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 377 | int* vertexCount, |
| 378 | int* indexCount) const { |
| 379 | // we will recommend a flush if the data could fit in a single |
| 380 | // preallocated buffer but none are left and it can't fit |
| 381 | // in the current buffer (which may not be prealloced). |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 382 | bool flush = false; |
| 383 | if (NULL != indexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 384 | int32_t currIndices = fIndexPool.currentBufferIndices(); |
| 385 | if (*indexCount > currIndices && |
| 386 | (!fIndexPool.preallocatedBuffersRemaining() && |
| 387 | *indexCount <= fIndexPool.preallocatedBufferIndices())) { |
| 388 | |
| 389 | flush = true; |
| 390 | } |
| 391 | *indexCount = currIndices; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 392 | } |
| 393 | if (NULL != vertexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 394 | int32_t currVertices = fVertexPool.currentBufferVertices(vertexLayout); |
| 395 | if (*vertexCount > currVertices && |
| 396 | (!fVertexPool.preallocatedBuffersRemaining() && |
| 397 | *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexLayout))) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 398 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 399 | flush = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 400 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 401 | *vertexCount = currVertices; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 402 | } |
| 403 | return flush; |
| 404 | } |
| 405 | |
| 406 | bool GrInOrderDrawBuffer::acquireGeometryHelper(GrVertexLayout vertexLayout, |
| 407 | void** vertices, |
| 408 | void** indices) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 409 | GrAssert(!fReservedGeometry.fLocked); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 410 | if (fReservedGeometry.fVertexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 411 | GrAssert(NULL != vertices); |
| 412 | GrAssert(0 == fReservedVertexBytes); |
| 413 | GrAssert(0 == fUsedReservedVertexBytes); |
| 414 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 415 | fReservedVertexBytes = VertexSize(vertexLayout) * |
| 416 | fReservedGeometry.fVertexCount; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 417 | *vertices = fVertexPool.makeSpace(vertexLayout, |
| 418 | fReservedGeometry.fVertexCount, |
| 419 | &fCurrPoolVertexBuffer, |
| 420 | &fCurrPoolStartVertex); |
| 421 | if (NULL == *vertices) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 422 | return false; |
| 423 | } |
| 424 | } |
| 425 | if (fReservedGeometry.fIndexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 426 | GrAssert(NULL != indices); |
| 427 | GrAssert(0 == fReservedIndexBytes); |
| 428 | GrAssert(0 == fUsedReservedIndexBytes); |
| 429 | |
| 430 | *indices = fIndexPool.makeSpace(fReservedGeometry.fIndexCount, |
| 431 | &fCurrPoolIndexBuffer, |
| 432 | &fCurrPoolStartIndex); |
| 433 | if (NULL == *indices) { |
| 434 | fVertexPool.putBack(fReservedVertexBytes); |
| 435 | fReservedVertexBytes = 0; |
| 436 | fCurrPoolVertexBuffer = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 437 | return false; |
| 438 | } |
| 439 | } |
| 440 | return true; |
| 441 | } |
| 442 | |
| 443 | void GrInOrderDrawBuffer::releaseGeometryHelper() { |
| 444 | GrAssert(fUsedReservedVertexBytes <= fReservedVertexBytes); |
| 445 | GrAssert(fUsedReservedIndexBytes <= fReservedIndexBytes); |
| 446 | |
| 447 | size_t vertexSlack = fReservedVertexBytes - fUsedReservedVertexBytes; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 448 | fVertexPool.putBack(vertexSlack); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 449 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 450 | size_t indexSlack = fReservedIndexBytes - fUsedReservedIndexBytes; |
| 451 | fIndexPool.putBack(indexSlack); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 452 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 453 | fReservedVertexBytes = 0; |
| 454 | fReservedIndexBytes = 0; |
| 455 | fUsedReservedVertexBytes = 0; |
| 456 | fUsedReservedIndexBytes = 0; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 457 | fCurrPoolVertexBuffer = 0; |
| 458 | fCurrPoolStartVertex = 0; |
| 459 | |
| 460 | } |
| 461 | |
| 462 | void GrInOrderDrawBuffer::setVertexSourceToArrayHelper(const void* vertexArray, |
| 463 | int vertexCount) { |
| 464 | GrAssert(!fReservedGeometry.fLocked || !fReservedGeometry.fVertexCount); |
| 465 | #if GR_DEBUG |
| 466 | bool success = |
| 467 | #endif |
| 468 | fVertexPool.appendVertices(fGeometrySrc.fVertexLayout, |
| 469 | vertexCount, |
| 470 | vertexArray, |
| 471 | &fCurrPoolVertexBuffer, |
| 472 | &fCurrPoolStartVertex); |
| 473 | GR_DEBUGASSERT(success); |
| 474 | } |
| 475 | |
| 476 | void GrInOrderDrawBuffer::setIndexSourceToArrayHelper(const void* indexArray, |
| 477 | int indexCount) { |
| 478 | GrAssert(!fReservedGeometry.fLocked || !fReservedGeometry.fIndexCount); |
| 479 | #if GR_DEBUG |
| 480 | bool success = |
| 481 | #endif |
| 482 | fIndexPool.appendIndices(indexCount, |
| 483 | indexArray, |
| 484 | &fCurrPoolIndexBuffer, |
| 485 | &fCurrPoolStartIndex); |
| 486 | GR_DEBUGASSERT(success); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 487 | } |
| 488 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 489 | bool GrInOrderDrawBuffer::needsNewState() const { |
| 490 | if (fStates.empty()) { |
| 491 | return true; |
| 492 | } else { |
| 493 | const DrState& old = this->accessSavedDrawState(fStates.back()); |
| 494 | return old != fCurrDrawState; |
| 495 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 496 | } |
| 497 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 498 | void GrInOrderDrawBuffer::pushState() { |
| 499 | for (int s = 0; s < kNumStages; ++s) { |
| 500 | GrSafeRef(fCurrDrawState.fTextures[s]); |
| 501 | } |
| 502 | this->saveCurrentDrawState(&fStates.push_back()); |
| 503 | } |
| 504 | |
| 505 | bool GrInOrderDrawBuffer::needsNewClip() const { |
| 506 | if (fCurrDrawState.fFlagBits & kClip_StateBit) { |
| 507 | if (fClips.empty() || (fClipSet && fClips.back() != fClip)) { |
| 508 | return true; |
| 509 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 510 | } |
| 511 | return false; |
| 512 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 513 | |
| 514 | void GrInOrderDrawBuffer::pushClip() { |
| 515 | fClips.push_back() = fClip; |
| 516 | fClipSet = false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 517 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame^] | 518 | |
| 519 | void GrInOrderDrawBuffer::clipWillBeSet(const GrClip& newClip) { |
| 520 | fClipSet = true; |
| 521 | } |