reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 2 | Copyright 2011 Google Inc. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 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) : |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 27 | fDraws(&fDrawStorage), |
| 28 | fStates(&fStateStorage), |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 29 | fClears(&fClearStorage), |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 30 | fClips(&fClipStorage), |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 31 | fClipSet(true), |
| 32 | |
| 33 | fLastRectVertexLayout(0), |
| 34 | fQuadIndexBuffer(NULL), |
| 35 | fMaxQuads(0), |
| 36 | fCurrQuad(0), |
| 37 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 38 | fVertexPool(*vertexPool), |
| 39 | fCurrPoolVertexBuffer(NULL), |
| 40 | fCurrPoolStartVertex(0), |
| 41 | fIndexPool(*indexPool), |
| 42 | fCurrPoolIndexBuffer(NULL), |
| 43 | fCurrPoolStartIndex(0), |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 44 | fReservedVertexBytes(0), |
| 45 | fReservedIndexBytes(0), |
| 46 | fUsedReservedVertexBytes(0), |
| 47 | fUsedReservedIndexBytes(0) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 48 | GrAssert(NULL != vertexPool); |
| 49 | GrAssert(NULL != indexPool); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | GrInOrderDrawBuffer::~GrInOrderDrawBuffer() { |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 53 | this->reset(); |
| 54 | GrSafeUnref(fQuadIndexBuffer); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void GrInOrderDrawBuffer::initializeDrawStateAndClip(const GrDrawTarget& target) { |
| 58 | this->copyDrawState(target); |
| 59 | this->setClip(target.getClip()); |
| 60 | } |
| 61 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 62 | void GrInOrderDrawBuffer::setQuadIndexBuffer(const GrIndexBuffer* indexBuffer) { |
| 63 | bool newIdxBuffer = fQuadIndexBuffer != indexBuffer; |
| 64 | if (newIdxBuffer) { |
| 65 | GrSafeUnref(fQuadIndexBuffer); |
| 66 | fQuadIndexBuffer = indexBuffer; |
| 67 | GrSafeRef(fQuadIndexBuffer); |
| 68 | fCurrQuad = 0; |
| 69 | fMaxQuads = (NULL == indexBuffer) ? 0 : indexBuffer->maxQuads(); |
| 70 | } else { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 71 | GrAssert((NULL == indexBuffer && 0 == fMaxQuads) || |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 72 | (indexBuffer->maxQuads() == fMaxQuads)); |
| 73 | } |
| 74 | } |
| 75 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 76 | void GrInOrderDrawBuffer::drawRect(const GrRect& rect, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 77 | const GrMatrix* matrix, |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 78 | StageBitfield stageEnableBitfield, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 79 | const GrRect* srcRects[], |
| 80 | const GrMatrix* srcMatrices[]) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 81 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 82 | GrAssert(!(NULL == fQuadIndexBuffer && fCurrQuad)); |
| 83 | GrAssert(!(fDraws.empty() && fCurrQuad)); |
| 84 | GrAssert(!(0 != fMaxQuads && NULL == fQuadIndexBuffer)); |
| 85 | |
| 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 | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 91 | GrVertexLayout layout = GetRectVertexLayout(stageEnableBitfield, srcRects); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 92 | AutoReleaseGeometry geo(this, layout, 4, 0); |
| 93 | AutoViewMatrixRestore avmr(this); |
| 94 | GrMatrix combinedMatrix = this->getViewMatrix(); |
| 95 | this->setViewMatrix(GrMatrix::I()); |
| 96 | if (NULL != matrix) { |
| 97 | combinedMatrix.preConcat(*matrix); |
| 98 | } |
| 99 | |
| 100 | SetRectVertices(rect, &combinedMatrix, srcRects, srcMatrices, layout, geo.vertices()); |
| 101 | |
| 102 | // we don't want to miss an opportunity to batch rects together |
| 103 | // simply because the clip has changed if the clip doesn't affect |
| 104 | // the rect. |
| 105 | bool disabledClip = false; |
| 106 | if (this->isClipState() && fClip.isRect()) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 107 | |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 108 | GrRect clipRect = fClip.getRect(0); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 109 | // If the clip rect touches the edge of the viewport, extended it |
| 110 | // out (close) to infinity to avoid bogus intersections. |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 111 | // We might consider a more exact clip to viewport if this |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 112 | // conservative test fails. |
| 113 | const GrRenderTarget* target = this->getRenderTarget(); |
| 114 | if (0 >= clipRect.fLeft) { |
| 115 | clipRect.fLeft = GR_ScalarMin; |
| 116 | } |
| 117 | if (target->width() <= clipRect.fRight) { |
| 118 | clipRect.fRight = GR_ScalarMax; |
| 119 | } |
| 120 | if (0 >= clipRect.top()) { |
| 121 | clipRect.fTop = GR_ScalarMin; |
| 122 | } |
| 123 | if (target->height() <= clipRect.fBottom) { |
| 124 | clipRect.fBottom = GR_ScalarMax; |
| 125 | } |
| 126 | int stride = VertexSize(layout); |
| 127 | bool insideClip = true; |
| 128 | for (int v = 0; v < 4; ++v) { |
| 129 | const GrPoint& p = *GetVertexPoint(geo.vertices(), v, stride); |
| 130 | if (!clipRect.contains(p)) { |
| 131 | insideClip = false; |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | if (insideClip) { |
| 136 | this->disableState(kClip_StateBit); |
| 137 | disabledClip = true; |
| 138 | } |
| 139 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 140 | if (!needsNewClip() && !needsNewState() && fCurrQuad > 0 && |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 141 | fCurrQuad < fMaxQuads && layout == fLastRectVertexLayout) { |
| 142 | |
| 143 | int vsize = VertexSize(layout); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 144 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 145 | Draw& lastDraw = fDraws.back(); |
| 146 | |
| 147 | GrAssert(lastDraw.fIndexBuffer == fQuadIndexBuffer); |
| 148 | GrAssert(kTriangles_PrimitiveType == lastDraw.fPrimitiveType); |
| 149 | GrAssert(0 == lastDraw.fVertexCount % 4); |
| 150 | GrAssert(0 == lastDraw.fIndexCount % 6); |
| 151 | GrAssert(0 == lastDraw.fStartIndex); |
| 152 | |
| 153 | appendToPreviousDraw = lastDraw.fVertexBuffer == fCurrPoolVertexBuffer && |
| 154 | (fCurrQuad * 4 + lastDraw.fStartVertex) == fCurrPoolStartVertex; |
| 155 | if (appendToPreviousDraw) { |
| 156 | lastDraw.fVertexCount += 4; |
| 157 | lastDraw.fIndexCount += 6; |
| 158 | fCurrQuad += 1; |
| 159 | GrAssert(0 == fUsedReservedVertexBytes); |
| 160 | fUsedReservedVertexBytes = 4 * vsize; |
| 161 | } |
| 162 | } |
| 163 | if (!appendToPreviousDraw) { |
| 164 | this->setIndexSourceToBuffer(fQuadIndexBuffer); |
| 165 | drawIndexed(kTriangles_PrimitiveType, 0, 0, 4, 6); |
| 166 | fCurrQuad = 1; |
| 167 | fLastRectVertexLayout = layout; |
| 168 | } |
| 169 | if (disabledClip) { |
| 170 | this->enableState(kClip_StateBit); |
| 171 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 172 | } else { |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 173 | INHERITED::drawRect(rect, matrix, stageEnableBitfield, srcRects, srcMatrices); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 177 | void GrInOrderDrawBuffer::drawIndexed(GrPrimitiveType primitiveType, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 178 | int startVertex, |
| 179 | int startIndex, |
| 180 | int vertexCount, |
| 181 | int indexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 182 | |
| 183 | if (!vertexCount || !indexCount) { |
| 184 | return; |
| 185 | } |
| 186 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 187 | fCurrQuad = 0; |
| 188 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 189 | Draw& draw = fDraws.push_back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 190 | draw.fPrimitiveType = primitiveType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 191 | draw.fStartVertex = startVertex; |
| 192 | draw.fStartIndex = startIndex; |
| 193 | draw.fVertexCount = vertexCount; |
| 194 | draw.fIndexCount = indexCount; |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 195 | |
| 196 | draw.fClipChanged = this->needsNewClip(); |
| 197 | if (draw.fClipChanged) { |
| 198 | this->pushClip(); |
| 199 | } |
| 200 | |
| 201 | draw.fStateChanged = this->needsNewState(); |
| 202 | if (draw.fStateChanged) { |
| 203 | this->pushState(); |
| 204 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 205 | |
| 206 | draw.fVertexLayout = fGeometrySrc.fVertexLayout; |
| 207 | switch (fGeometrySrc.fVertexSrc) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 208 | case kBuffer_GeometrySrcType: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 209 | draw.fVertexBuffer = fGeometrySrc.fVertexBuffer; |
| 210 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 211 | case kReserved_GeometrySrcType: { |
| 212 | size_t vertexBytes = (vertexCount + startVertex) * |
| 213 | VertexSize(fGeometrySrc.fVertexLayout); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 214 | fUsedReservedVertexBytes = GrMax(fUsedReservedVertexBytes, vertexBytes); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 215 | } // fallthrough |
| 216 | case kArray_GeometrySrcType: |
| 217 | draw.fVertexBuffer = fCurrPoolVertexBuffer; |
| 218 | draw.fStartVertex += fCurrPoolStartVertex; |
| 219 | break; |
| 220 | default: |
| 221 | GrCrash("unknown geom src type"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 222 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 223 | draw.fVertexBuffer->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 224 | |
| 225 | switch (fGeometrySrc.fIndexSrc) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 226 | case kBuffer_GeometrySrcType: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 227 | draw.fIndexBuffer = fGeometrySrc.fIndexBuffer; |
| 228 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 229 | case kReserved_GeometrySrcType: { |
| 230 | size_t indexBytes = (indexCount + startIndex) * sizeof(uint16_t); |
| 231 | fUsedReservedIndexBytes = GrMax(fUsedReservedIndexBytes, indexBytes); |
| 232 | } // fallthrough |
| 233 | case kArray_GeometrySrcType: |
| 234 | draw.fIndexBuffer = fCurrPoolIndexBuffer; |
| 235 | draw.fStartIndex += fCurrPoolStartVertex; |
| 236 | break; |
| 237 | default: |
| 238 | GrCrash("unknown geom src type"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 239 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 240 | draw.fIndexBuffer->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 241 | } |
| 242 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 243 | void GrInOrderDrawBuffer::drawNonIndexed(GrPrimitiveType primitiveType, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 244 | int startVertex, |
| 245 | int vertexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 246 | if (!vertexCount) { |
| 247 | return; |
| 248 | } |
| 249 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 250 | fCurrQuad = 0; |
| 251 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 252 | Draw& draw = fDraws.push_back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 253 | draw.fPrimitiveType = primitiveType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 254 | draw.fStartVertex = startVertex; |
| 255 | draw.fStartIndex = 0; |
| 256 | draw.fVertexCount = vertexCount; |
| 257 | draw.fIndexCount = 0; |
| 258 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 259 | draw.fClipChanged = this->needsNewClip(); |
| 260 | if (draw.fClipChanged) { |
| 261 | this->pushClip(); |
| 262 | } |
| 263 | |
| 264 | draw.fStateChanged = this->needsNewState(); |
| 265 | if (draw.fStateChanged) { |
| 266 | this->pushState(); |
| 267 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 268 | |
| 269 | draw.fVertexLayout = fGeometrySrc.fVertexLayout; |
| 270 | switch (fGeometrySrc.fVertexSrc) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 271 | case kBuffer_GeometrySrcType: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 272 | draw.fVertexBuffer = fGeometrySrc.fVertexBuffer; |
| 273 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 274 | case kReserved_GeometrySrcType: { |
| 275 | size_t vertexBytes = (vertexCount + startVertex) * |
| 276 | VertexSize(fGeometrySrc.fVertexLayout); |
| 277 | fUsedReservedVertexBytes = GrMax(fUsedReservedVertexBytes, |
| 278 | vertexBytes); |
| 279 | } // fallthrough |
| 280 | case kArray_GeometrySrcType: |
| 281 | draw.fVertexBuffer = fCurrPoolVertexBuffer; |
| 282 | draw.fStartVertex += fCurrPoolStartVertex; |
| 283 | break; |
| 284 | default: |
| 285 | GrCrash("unknown geom src type"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 286 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 287 | draw.fVertexBuffer->ref(); |
| 288 | draw.fIndexBuffer = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 289 | } |
| 290 | |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 291 | void GrInOrderDrawBuffer::clear(GrColor color) { |
| 292 | Clear& clr = fClears.push_back(); |
| 293 | clr.fColor = color; |
| 294 | clr.fBeforeDrawIdx = fDraws.count(); |
| 295 | |
| 296 | // We could do something smart and remove previous draws and clears to the |
| 297 | // current render target. If we get that smart we have to make sure those |
| 298 | // draws aren't read before this clear (render-to-texture). |
| 299 | } |
| 300 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 301 | void GrInOrderDrawBuffer::reset() { |
| 302 | GrAssert(!fReservedGeometry.fLocked); |
| 303 | uint32_t numStates = fStates.count(); |
| 304 | for (uint32_t i = 0; i < numStates; ++i) { |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 305 | const DrState& dstate = this->accessSavedDrawState(fStates[i]); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 306 | for (int s = 0; s < kNumStages; ++s) { |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 307 | GrSafeUnref(dstate.fTextures[s]); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 308 | } |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 309 | GrSafeUnref(dstate.fRenderTarget); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 310 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 311 | int numDraws = fDraws.count(); |
| 312 | for (int d = 0; d < numDraws; ++d) { |
| 313 | // we always have a VB, but not always an IB |
| 314 | GrAssert(NULL != fDraws[d].fVertexBuffer); |
| 315 | fDraws[d].fVertexBuffer->unref(); |
| 316 | GrSafeUnref(fDraws[d].fIndexBuffer); |
| 317 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 318 | fDraws.reset(); |
| 319 | fStates.reset(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 320 | |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 321 | fClears.reset(); |
| 322 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 323 | fVertexPool.reset(); |
| 324 | fIndexPool.reset(); |
| 325 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 326 | fClips.reset(); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 327 | |
| 328 | fCurrQuad = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | void GrInOrderDrawBuffer::playback(GrDrawTarget* target) { |
reed@google.com | 0ebe81a | 2011-04-04 20:06:59 +0000 | [diff] [blame] | 332 | GrAssert(!fReservedGeometry.fLocked); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 333 | GrAssert(NULL != target); |
| 334 | GrAssert(target != this); // not considered and why? |
| 335 | |
bsalomon@google.com | 898d9e5 | 2011-04-26 13:22:33 +0000 | [diff] [blame^] | 336 | int numDraws = fDraws.count(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 337 | if (!numDraws) { |
| 338 | return; |
| 339 | } |
| 340 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 341 | fVertexPool.unlock(); |
| 342 | fIndexPool.unlock(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 343 | |
| 344 | GrDrawTarget::AutoStateRestore asr(target); |
| 345 | GrDrawTarget::AutoClipRestore acr(target); |
| 346 | // important to not mess with reserve/lock geometry in the target with this |
| 347 | // on the stack. |
| 348 | GrDrawTarget::AutoGeometrySrcRestore agsr(target); |
| 349 | |
| 350 | uint32_t currState = ~0; |
| 351 | uint32_t currClip = ~0; |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 352 | uint32_t currClear = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 353 | |
bsalomon@google.com | 898d9e5 | 2011-04-26 13:22:33 +0000 | [diff] [blame^] | 354 | for (int i = 0; i < numDraws; ++i) { |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 355 | while (currClear < fClears.count() && |
| 356 | i == fClears[currClear].fBeforeDrawIdx) { |
| 357 | target->clear(fClears[currClear].fColor); |
| 358 | ++currClear; |
| 359 | } |
| 360 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 361 | const Draw& draw = fDraws[i]; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 362 | if (draw.fStateChanged) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 363 | ++currState; |
| 364 | target->restoreDrawState(fStates[currState]); |
| 365 | } |
| 366 | if (draw.fClipChanged) { |
| 367 | ++currClip; |
| 368 | target->setClip(fClips[currClip]); |
| 369 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 370 | |
| 371 | target->setVertexSourceToBuffer(draw.fVertexLayout, draw.fVertexBuffer); |
| 372 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 373 | if (draw.fIndexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 374 | target->setIndexSourceToBuffer(draw.fIndexBuffer); |
| 375 | } |
| 376 | |
| 377 | if (draw.fIndexCount) { |
| 378 | target->drawIndexed(draw.fPrimitiveType, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 379 | draw.fStartVertex, |
| 380 | draw.fStartIndex, |
| 381 | draw.fVertexCount, |
| 382 | draw.fIndexCount); |
| 383 | } else { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 384 | target->drawNonIndexed(draw.fPrimitiveType, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 385 | draw.fStartVertex, |
| 386 | draw.fVertexCount); |
| 387 | } |
| 388 | } |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 389 | while (currClear < fClears.count()) { |
| 390 | GrAssert(fDraws.count() == fClears[currClear].fBeforeDrawIdx); |
| 391 | target->clear(fClears[currClear].fColor); |
| 392 | ++currClear; |
| 393 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | bool GrInOrderDrawBuffer::geometryHints(GrVertexLayout vertexLayout, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 397 | int* vertexCount, |
| 398 | int* indexCount) const { |
| 399 | // we will recommend a flush if the data could fit in a single |
| 400 | // preallocated buffer but none are left and it can't fit |
| 401 | // in the current buffer (which may not be prealloced). |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 402 | bool flush = false; |
| 403 | if (NULL != indexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 404 | int32_t currIndices = fIndexPool.currentBufferIndices(); |
| 405 | if (*indexCount > currIndices && |
| 406 | (!fIndexPool.preallocatedBuffersRemaining() && |
| 407 | *indexCount <= fIndexPool.preallocatedBufferIndices())) { |
| 408 | |
| 409 | flush = true; |
| 410 | } |
| 411 | *indexCount = currIndices; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 412 | } |
| 413 | if (NULL != vertexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 414 | int32_t currVertices = fVertexPool.currentBufferVertices(vertexLayout); |
| 415 | if (*vertexCount > currVertices && |
| 416 | (!fVertexPool.preallocatedBuffersRemaining() && |
| 417 | *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexLayout))) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 418 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 419 | flush = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 420 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 421 | *vertexCount = currVertices; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 422 | } |
| 423 | return flush; |
| 424 | } |
| 425 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 426 | bool GrInOrderDrawBuffer::onAcquireGeometry(GrVertexLayout vertexLayout, |
| 427 | void** vertices, |
| 428 | void** indices) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 429 | GrAssert(!fReservedGeometry.fLocked); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 430 | if (fReservedGeometry.fVertexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 431 | GrAssert(NULL != vertices); |
| 432 | GrAssert(0 == fReservedVertexBytes); |
| 433 | GrAssert(0 == fUsedReservedVertexBytes); |
| 434 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 435 | fReservedVertexBytes = VertexSize(vertexLayout) * |
| 436 | fReservedGeometry.fVertexCount; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 437 | *vertices = fVertexPool.makeSpace(vertexLayout, |
| 438 | fReservedGeometry.fVertexCount, |
| 439 | &fCurrPoolVertexBuffer, |
| 440 | &fCurrPoolStartVertex); |
| 441 | if (NULL == *vertices) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 442 | return false; |
| 443 | } |
| 444 | } |
| 445 | if (fReservedGeometry.fIndexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 446 | GrAssert(NULL != indices); |
| 447 | GrAssert(0 == fReservedIndexBytes); |
| 448 | GrAssert(0 == fUsedReservedIndexBytes); |
| 449 | |
| 450 | *indices = fIndexPool.makeSpace(fReservedGeometry.fIndexCount, |
| 451 | &fCurrPoolIndexBuffer, |
| 452 | &fCurrPoolStartIndex); |
| 453 | if (NULL == *indices) { |
| 454 | fVertexPool.putBack(fReservedVertexBytes); |
| 455 | fReservedVertexBytes = 0; |
| 456 | fCurrPoolVertexBuffer = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 457 | return false; |
| 458 | } |
| 459 | } |
| 460 | return true; |
| 461 | } |
| 462 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 463 | void GrInOrderDrawBuffer::onReleaseGeometry() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 464 | GrAssert(fUsedReservedVertexBytes <= fReservedVertexBytes); |
| 465 | GrAssert(fUsedReservedIndexBytes <= fReservedIndexBytes); |
| 466 | |
| 467 | size_t vertexSlack = fReservedVertexBytes - fUsedReservedVertexBytes; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 468 | fVertexPool.putBack(vertexSlack); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 469 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 470 | size_t indexSlack = fReservedIndexBytes - fUsedReservedIndexBytes; |
| 471 | fIndexPool.putBack(indexSlack); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 472 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 473 | fReservedVertexBytes = 0; |
| 474 | fReservedIndexBytes = 0; |
| 475 | fUsedReservedVertexBytes = 0; |
| 476 | fUsedReservedIndexBytes = 0; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 477 | fCurrPoolVertexBuffer = 0; |
| 478 | fCurrPoolStartVertex = 0; |
| 479 | |
| 480 | } |
| 481 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 482 | void GrInOrderDrawBuffer::onSetVertexSourceToArray(const void* vertexArray, |
| 483 | int vertexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 484 | GrAssert(!fReservedGeometry.fLocked || !fReservedGeometry.fVertexCount); |
| 485 | #if GR_DEBUG |
| 486 | bool success = |
| 487 | #endif |
| 488 | fVertexPool.appendVertices(fGeometrySrc.fVertexLayout, |
| 489 | vertexCount, |
| 490 | vertexArray, |
| 491 | &fCurrPoolVertexBuffer, |
| 492 | &fCurrPoolStartVertex); |
| 493 | GR_DEBUGASSERT(success); |
| 494 | } |
| 495 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 496 | void GrInOrderDrawBuffer::onSetIndexSourceToArray(const void* indexArray, |
| 497 | int indexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 498 | GrAssert(!fReservedGeometry.fLocked || !fReservedGeometry.fIndexCount); |
| 499 | #if GR_DEBUG |
| 500 | bool success = |
| 501 | #endif |
| 502 | fIndexPool.appendIndices(indexCount, |
| 503 | indexArray, |
| 504 | &fCurrPoolIndexBuffer, |
| 505 | &fCurrPoolStartIndex); |
| 506 | GR_DEBUGASSERT(success); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 507 | } |
| 508 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 509 | bool GrInOrderDrawBuffer::needsNewState() const { |
| 510 | if (fStates.empty()) { |
| 511 | return true; |
| 512 | } else { |
| 513 | const DrState& old = this->accessSavedDrawState(fStates.back()); |
| 514 | return old != fCurrDrawState; |
| 515 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 516 | } |
| 517 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 518 | void GrInOrderDrawBuffer::pushState() { |
| 519 | for (int s = 0; s < kNumStages; ++s) { |
| 520 | GrSafeRef(fCurrDrawState.fTextures[s]); |
| 521 | } |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 522 | GrSafeRef(fCurrDrawState.fRenderTarget); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 523 | this->saveCurrentDrawState(&fStates.push_back()); |
| 524 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 525 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 526 | bool GrInOrderDrawBuffer::needsNewClip() const { |
| 527 | if (fCurrDrawState.fFlagBits & kClip_StateBit) { |
| 528 | if (fClips.empty() || (fClipSet && fClips.back() != fClip)) { |
| 529 | return true; |
| 530 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 531 | } |
| 532 | return false; |
| 533 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 534 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 535 | void GrInOrderDrawBuffer::pushClip() { |
| 536 | fClips.push_back() = fClip; |
| 537 | fClipSet = false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 538 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 539 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 540 | void GrInOrderDrawBuffer::clipWillBeSet(const GrClip& newClip) { |
| 541 | fClipSet = true; |
| 542 | } |