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 | |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame^] | 153 | bool clearSinceLastDraw = |
| 154 | fClears.count() && |
| 155 | fClears.back().fBeforeDrawIdx == fDraws.count(); |
| 156 | |
| 157 | appendToPreviousDraw = !clearSinceLastDraw && |
| 158 | lastDraw.fVertexBuffer == fCurrPoolVertexBuffer && |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 159 | (fCurrQuad * 4 + lastDraw.fStartVertex) == fCurrPoolStartVertex; |
| 160 | if (appendToPreviousDraw) { |
| 161 | lastDraw.fVertexCount += 4; |
| 162 | lastDraw.fIndexCount += 6; |
| 163 | fCurrQuad += 1; |
| 164 | GrAssert(0 == fUsedReservedVertexBytes); |
| 165 | fUsedReservedVertexBytes = 4 * vsize; |
| 166 | } |
| 167 | } |
| 168 | if (!appendToPreviousDraw) { |
| 169 | this->setIndexSourceToBuffer(fQuadIndexBuffer); |
| 170 | drawIndexed(kTriangles_PrimitiveType, 0, 0, 4, 6); |
| 171 | fCurrQuad = 1; |
| 172 | fLastRectVertexLayout = layout; |
| 173 | } |
| 174 | if (disabledClip) { |
| 175 | this->enableState(kClip_StateBit); |
| 176 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 177 | } else { |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 178 | INHERITED::drawRect(rect, matrix, stageEnableBitfield, srcRects, srcMatrices); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 182 | void GrInOrderDrawBuffer::drawIndexed(GrPrimitiveType primitiveType, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 183 | int startVertex, |
| 184 | int startIndex, |
| 185 | int vertexCount, |
| 186 | int indexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 187 | |
| 188 | if (!vertexCount || !indexCount) { |
| 189 | return; |
| 190 | } |
| 191 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 192 | fCurrQuad = 0; |
| 193 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 194 | Draw& draw = fDraws.push_back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 195 | draw.fPrimitiveType = primitiveType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 196 | draw.fStartVertex = startVertex; |
| 197 | draw.fStartIndex = startIndex; |
| 198 | draw.fVertexCount = vertexCount; |
| 199 | draw.fIndexCount = indexCount; |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 200 | |
| 201 | draw.fClipChanged = this->needsNewClip(); |
| 202 | if (draw.fClipChanged) { |
| 203 | this->pushClip(); |
| 204 | } |
| 205 | |
| 206 | draw.fStateChanged = this->needsNewState(); |
| 207 | if (draw.fStateChanged) { |
| 208 | this->pushState(); |
| 209 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 210 | |
| 211 | draw.fVertexLayout = fGeometrySrc.fVertexLayout; |
| 212 | switch (fGeometrySrc.fVertexSrc) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 213 | case kBuffer_GeometrySrcType: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 214 | draw.fVertexBuffer = fGeometrySrc.fVertexBuffer; |
| 215 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 216 | case kReserved_GeometrySrcType: { |
| 217 | size_t vertexBytes = (vertexCount + startVertex) * |
| 218 | VertexSize(fGeometrySrc.fVertexLayout); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 219 | fUsedReservedVertexBytes = GrMax(fUsedReservedVertexBytes, vertexBytes); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 220 | } // fallthrough |
| 221 | case kArray_GeometrySrcType: |
| 222 | draw.fVertexBuffer = fCurrPoolVertexBuffer; |
| 223 | draw.fStartVertex += fCurrPoolStartVertex; |
| 224 | break; |
| 225 | default: |
| 226 | GrCrash("unknown geom src type"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 227 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 228 | draw.fVertexBuffer->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 229 | |
| 230 | switch (fGeometrySrc.fIndexSrc) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 231 | case kBuffer_GeometrySrcType: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 232 | draw.fIndexBuffer = fGeometrySrc.fIndexBuffer; |
| 233 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 234 | case kReserved_GeometrySrcType: { |
| 235 | size_t indexBytes = (indexCount + startIndex) * sizeof(uint16_t); |
| 236 | fUsedReservedIndexBytes = GrMax(fUsedReservedIndexBytes, indexBytes); |
| 237 | } // fallthrough |
| 238 | case kArray_GeometrySrcType: |
| 239 | draw.fIndexBuffer = fCurrPoolIndexBuffer; |
| 240 | draw.fStartIndex += fCurrPoolStartVertex; |
| 241 | break; |
| 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.fIndexBuffer->ref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 246 | } |
| 247 | |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 248 | void GrInOrderDrawBuffer::drawNonIndexed(GrPrimitiveType primitiveType, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 249 | int startVertex, |
| 250 | int vertexCount) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 251 | if (!vertexCount) { |
| 252 | return; |
| 253 | } |
| 254 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 255 | fCurrQuad = 0; |
| 256 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 257 | Draw& draw = fDraws.push_back(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 258 | draw.fPrimitiveType = primitiveType; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 259 | draw.fStartVertex = startVertex; |
| 260 | draw.fStartIndex = 0; |
| 261 | draw.fVertexCount = vertexCount; |
| 262 | draw.fIndexCount = 0; |
| 263 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 264 | draw.fClipChanged = this->needsNewClip(); |
| 265 | if (draw.fClipChanged) { |
| 266 | this->pushClip(); |
| 267 | } |
| 268 | |
| 269 | draw.fStateChanged = this->needsNewState(); |
| 270 | if (draw.fStateChanged) { |
| 271 | this->pushState(); |
| 272 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 273 | |
| 274 | draw.fVertexLayout = fGeometrySrc.fVertexLayout; |
| 275 | switch (fGeometrySrc.fVertexSrc) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 276 | case kBuffer_GeometrySrcType: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 277 | draw.fVertexBuffer = fGeometrySrc.fVertexBuffer; |
| 278 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 279 | case kReserved_GeometrySrcType: { |
| 280 | size_t vertexBytes = (vertexCount + startVertex) * |
| 281 | VertexSize(fGeometrySrc.fVertexLayout); |
| 282 | fUsedReservedVertexBytes = GrMax(fUsedReservedVertexBytes, |
| 283 | vertexBytes); |
| 284 | } // fallthrough |
| 285 | case kArray_GeometrySrcType: |
| 286 | draw.fVertexBuffer = fCurrPoolVertexBuffer; |
| 287 | draw.fStartVertex += fCurrPoolStartVertex; |
| 288 | break; |
| 289 | default: |
| 290 | GrCrash("unknown geom src type"); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 291 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 292 | draw.fVertexBuffer->ref(); |
| 293 | draw.fIndexBuffer = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 294 | } |
| 295 | |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame^] | 296 | void GrInOrderDrawBuffer::clear(const GrIRect* rect, GrColor color) { |
| 297 | GrIRect r; |
| 298 | if (NULL == rect) { |
| 299 | // We could do something smart and remove previous draws and clears to |
| 300 | // the current render target. If we get that smart we have to make sure |
| 301 | // those draws aren't read before this clear (render-to-texture). |
| 302 | r.setLTRB(0, 0, |
| 303 | this->getRenderTarget()->width(), |
| 304 | this->getRenderTarget()->height()); |
| 305 | rect = &r; |
| 306 | } |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 307 | Clear& clr = fClears.push_back(); |
| 308 | clr.fColor = color; |
| 309 | clr.fBeforeDrawIdx = fDraws.count(); |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame^] | 310 | clr.fRect = *rect; |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 311 | } |
| 312 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 313 | void GrInOrderDrawBuffer::reset() { |
| 314 | GrAssert(!fReservedGeometry.fLocked); |
| 315 | uint32_t numStates = fStates.count(); |
| 316 | for (uint32_t i = 0; i < numStates; ++i) { |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 317 | const DrState& dstate = this->accessSavedDrawState(fStates[i]); |
bsalomon@google.com | 8531c1c | 2011-01-13 19:52:45 +0000 | [diff] [blame] | 318 | for (int s = 0; s < kNumStages; ++s) { |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 319 | GrSafeUnref(dstate.fTextures[s]); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 320 | } |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 321 | GrSafeUnref(dstate.fRenderTarget); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 322 | } |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 323 | int numDraws = fDraws.count(); |
| 324 | for (int d = 0; d < numDraws; ++d) { |
| 325 | // we always have a VB, but not always an IB |
| 326 | GrAssert(NULL != fDraws[d].fVertexBuffer); |
| 327 | fDraws[d].fVertexBuffer->unref(); |
| 328 | GrSafeUnref(fDraws[d].fIndexBuffer); |
| 329 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 330 | fDraws.reset(); |
| 331 | fStates.reset(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 332 | |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 333 | fClears.reset(); |
| 334 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 335 | fVertexPool.reset(); |
| 336 | fIndexPool.reset(); |
| 337 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 338 | fClips.reset(); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 339 | |
| 340 | fCurrQuad = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | void GrInOrderDrawBuffer::playback(GrDrawTarget* target) { |
reed@google.com | 0ebe81a | 2011-04-04 20:06:59 +0000 | [diff] [blame] | 344 | GrAssert(!fReservedGeometry.fLocked); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 345 | GrAssert(NULL != target); |
| 346 | GrAssert(target != this); // not considered and why? |
| 347 | |
bsalomon@google.com | 898d9e5 | 2011-04-26 13:22:33 +0000 | [diff] [blame] | 348 | int numDraws = fDraws.count(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 349 | if (!numDraws) { |
| 350 | return; |
| 351 | } |
| 352 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 353 | fVertexPool.unlock(); |
| 354 | fIndexPool.unlock(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 355 | |
| 356 | GrDrawTarget::AutoStateRestore asr(target); |
| 357 | GrDrawTarget::AutoClipRestore acr(target); |
| 358 | // important to not mess with reserve/lock geometry in the target with this |
| 359 | // on the stack. |
| 360 | GrDrawTarget::AutoGeometrySrcRestore agsr(target); |
| 361 | |
| 362 | uint32_t currState = ~0; |
| 363 | uint32_t currClip = ~0; |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 364 | uint32_t currClear = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 365 | |
bsalomon@google.com | 898d9e5 | 2011-04-26 13:22:33 +0000 | [diff] [blame] | 366 | for (int i = 0; i < numDraws; ++i) { |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 367 | while (currClear < fClears.count() && |
| 368 | i == fClears[currClear].fBeforeDrawIdx) { |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame^] | 369 | target->clear(&fClears[currClear].fRect, fClears[currClear].fColor); |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 370 | ++currClear; |
| 371 | } |
| 372 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 373 | const Draw& draw = fDraws[i]; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 374 | if (draw.fStateChanged) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 375 | ++currState; |
| 376 | target->restoreDrawState(fStates[currState]); |
| 377 | } |
| 378 | if (draw.fClipChanged) { |
| 379 | ++currClip; |
| 380 | target->setClip(fClips[currClip]); |
| 381 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 382 | |
| 383 | target->setVertexSourceToBuffer(draw.fVertexLayout, draw.fVertexBuffer); |
| 384 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 385 | if (draw.fIndexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 386 | target->setIndexSourceToBuffer(draw.fIndexBuffer); |
| 387 | } |
| 388 | |
| 389 | if (draw.fIndexCount) { |
| 390 | target->drawIndexed(draw.fPrimitiveType, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 391 | draw.fStartVertex, |
| 392 | draw.fStartIndex, |
| 393 | draw.fVertexCount, |
| 394 | draw.fIndexCount); |
| 395 | } else { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 396 | target->drawNonIndexed(draw.fPrimitiveType, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 397 | draw.fStartVertex, |
| 398 | draw.fVertexCount); |
| 399 | } |
| 400 | } |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 401 | while (currClear < fClears.count()) { |
| 402 | GrAssert(fDraws.count() == fClears[currClear].fBeforeDrawIdx); |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame^] | 403 | target->clear(&fClears[currClear].fRect, fClears[currClear].fColor); |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 404 | ++currClear; |
| 405 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | bool GrInOrderDrawBuffer::geometryHints(GrVertexLayout vertexLayout, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 409 | int* vertexCount, |
| 410 | int* indexCount) const { |
| 411 | // we will recommend a flush if the data could fit in a single |
| 412 | // preallocated buffer but none are left and it can't fit |
| 413 | // in the current buffer (which may not be prealloced). |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 414 | bool flush = false; |
| 415 | if (NULL != indexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 416 | int32_t currIndices = fIndexPool.currentBufferIndices(); |
| 417 | if (*indexCount > currIndices && |
| 418 | (!fIndexPool.preallocatedBuffersRemaining() && |
| 419 | *indexCount <= fIndexPool.preallocatedBufferIndices())) { |
| 420 | |
| 421 | flush = true; |
| 422 | } |
| 423 | *indexCount = currIndices; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 424 | } |
| 425 | if (NULL != vertexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 426 | int32_t currVertices = fVertexPool.currentBufferVertices(vertexLayout); |
| 427 | if (*vertexCount > currVertices && |
| 428 | (!fVertexPool.preallocatedBuffersRemaining() && |
| 429 | *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexLayout))) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 430 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 431 | flush = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 432 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 433 | *vertexCount = currVertices; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 434 | } |
| 435 | return flush; |
| 436 | } |
| 437 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 438 | bool GrInOrderDrawBuffer::onAcquireGeometry(GrVertexLayout vertexLayout, |
| 439 | void** vertices, |
| 440 | void** indices) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 441 | GrAssert(!fReservedGeometry.fLocked); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 442 | if (fReservedGeometry.fVertexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 443 | GrAssert(NULL != vertices); |
| 444 | GrAssert(0 == fReservedVertexBytes); |
| 445 | GrAssert(0 == fUsedReservedVertexBytes); |
| 446 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 447 | fReservedVertexBytes = VertexSize(vertexLayout) * |
| 448 | fReservedGeometry.fVertexCount; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 449 | *vertices = fVertexPool.makeSpace(vertexLayout, |
| 450 | fReservedGeometry.fVertexCount, |
| 451 | &fCurrPoolVertexBuffer, |
| 452 | &fCurrPoolStartVertex); |
| 453 | if (NULL == *vertices) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 454 | return false; |
| 455 | } |
| 456 | } |
| 457 | if (fReservedGeometry.fIndexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 458 | GrAssert(NULL != indices); |
| 459 | GrAssert(0 == fReservedIndexBytes); |
| 460 | GrAssert(0 == fUsedReservedIndexBytes); |
| 461 | |
| 462 | *indices = fIndexPool.makeSpace(fReservedGeometry.fIndexCount, |
| 463 | &fCurrPoolIndexBuffer, |
| 464 | &fCurrPoolStartIndex); |
| 465 | if (NULL == *indices) { |
| 466 | fVertexPool.putBack(fReservedVertexBytes); |
| 467 | fReservedVertexBytes = 0; |
| 468 | fCurrPoolVertexBuffer = NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 469 | return false; |
| 470 | } |
| 471 | } |
| 472 | return true; |
| 473 | } |
| 474 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 475 | void GrInOrderDrawBuffer::onReleaseGeometry() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 476 | GrAssert(fUsedReservedVertexBytes <= fReservedVertexBytes); |
| 477 | GrAssert(fUsedReservedIndexBytes <= fReservedIndexBytes); |
| 478 | |
| 479 | size_t vertexSlack = fReservedVertexBytes - fUsedReservedVertexBytes; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 480 | fVertexPool.putBack(vertexSlack); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 481 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 482 | size_t indexSlack = fReservedIndexBytes - fUsedReservedIndexBytes; |
| 483 | fIndexPool.putBack(indexSlack); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 484 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 485 | fReservedVertexBytes = 0; |
| 486 | fReservedIndexBytes = 0; |
| 487 | fUsedReservedVertexBytes = 0; |
| 488 | fUsedReservedIndexBytes = 0; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 489 | fCurrPoolVertexBuffer = 0; |
| 490 | fCurrPoolStartVertex = 0; |
| 491 | |
| 492 | } |
| 493 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 494 | void GrInOrderDrawBuffer::onSetVertexSourceToArray(const void* vertexArray, |
| 495 | int vertexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 496 | GrAssert(!fReservedGeometry.fLocked || !fReservedGeometry.fVertexCount); |
| 497 | #if GR_DEBUG |
| 498 | bool success = |
| 499 | #endif |
| 500 | fVertexPool.appendVertices(fGeometrySrc.fVertexLayout, |
| 501 | vertexCount, |
| 502 | vertexArray, |
| 503 | &fCurrPoolVertexBuffer, |
| 504 | &fCurrPoolStartVertex); |
| 505 | GR_DEBUGASSERT(success); |
| 506 | } |
| 507 | |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 508 | void GrInOrderDrawBuffer::onSetIndexSourceToArray(const void* indexArray, |
| 509 | int indexCount) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 510 | GrAssert(!fReservedGeometry.fLocked || !fReservedGeometry.fIndexCount); |
| 511 | #if GR_DEBUG |
| 512 | bool success = |
| 513 | #endif |
| 514 | fIndexPool.appendIndices(indexCount, |
| 515 | indexArray, |
| 516 | &fCurrPoolIndexBuffer, |
| 517 | &fCurrPoolStartIndex); |
| 518 | GR_DEBUGASSERT(success); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 519 | } |
| 520 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 521 | bool GrInOrderDrawBuffer::needsNewState() const { |
| 522 | if (fStates.empty()) { |
| 523 | return true; |
| 524 | } else { |
| 525 | const DrState& old = this->accessSavedDrawState(fStates.back()); |
| 526 | return old != fCurrDrawState; |
| 527 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 528 | } |
| 529 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 530 | void GrInOrderDrawBuffer::pushState() { |
| 531 | for (int s = 0; s < kNumStages; ++s) { |
| 532 | GrSafeRef(fCurrDrawState.fTextures[s]); |
| 533 | } |
bsalomon@google.com | 1da0746 | 2011-03-10 14:51:57 +0000 | [diff] [blame] | 534 | GrSafeRef(fCurrDrawState.fRenderTarget); |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 535 | this->saveCurrentDrawState(&fStates.push_back()); |
| 536 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 537 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 538 | bool GrInOrderDrawBuffer::needsNewClip() const { |
| 539 | if (fCurrDrawState.fFlagBits & kClip_StateBit) { |
| 540 | if (fClips.empty() || (fClipSet && fClips.back() != fClip)) { |
| 541 | return true; |
| 542 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 543 | } |
| 544 | return false; |
| 545 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 546 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 547 | void GrInOrderDrawBuffer::pushClip() { |
| 548 | fClips.push_back() = fClip; |
| 549 | fClipSet = false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 550 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 551 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 552 | void GrInOrderDrawBuffer::clipWillBeSet(const GrClip& newClip) { |
| 553 | fClipSet = true; |
| 554 | } |