| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
| 10 | #include "GrInOrderDrawBuffer.h" |
| bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 11 | #include "GrBufferAllocPool.h" |
| 12 | #include "GrGpu.h" |
| 13 | #include "GrIndexBuffer.h" |
| 14 | #include "GrPath.h" |
| bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 15 | #include "GrRenderTarget.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 16 | #include "GrTexture.h" |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 17 | #include "GrVertexBuffer.h" |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 18 | |
| bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 19 | GrInOrderDrawBuffer::GrInOrderDrawBuffer(const GrGpu* gpu, |
| 20 | GrVertexBufferAllocPool* vertexPool, |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 21 | GrIndexBufferAllocPool* indexPool) |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 22 | : fAutoFlushTarget(NULL) |
| 23 | , fClipSet(true) |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame^] | 24 | , fClipProxyState(kUnknown_ClipProxyState) |
| robertphillips@google.com | 6970557 | 2012-03-21 19:46:50 +0000 | [diff] [blame] | 25 | , fVertexPool(*vertexPool) |
| 26 | , fIndexPool(*indexPool) |
| robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 27 | , fFlushing(false) { |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 28 | |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame^] | 29 | fGpu.reset(SkRef(gpu)); |
| bsalomon@google.com | 18c9c19 | 2011-09-22 21:01:31 +0000 | [diff] [blame] | 30 | fCaps = gpu->getCaps(); |
| 31 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 32 | GrAssert(NULL != vertexPool); |
| 33 | GrAssert(NULL != indexPool); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 34 | |
| 35 | GeometryPoolState& poolState = fGeoPoolStateStack.push_back(); |
| 36 | poolState.fUsedPoolVertexBytes = 0; |
| 37 | poolState.fUsedPoolIndexBytes = 0; |
| 38 | #if GR_DEBUG |
| 39 | poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0; |
| 40 | poolState.fPoolStartVertex = ~0; |
| 41 | poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0; |
| 42 | poolState.fPoolStartIndex = ~0; |
| 43 | #endif |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 44 | this->reset(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | GrInOrderDrawBuffer::~GrInOrderDrawBuffer() { |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 48 | this->reset(); |
| bsalomon@google.com | 4a018bb | 2011-10-28 19:50:21 +0000 | [diff] [blame] | 49 | // This must be called by before the GrDrawTarget destructor |
| 50 | this->releaseGeometry(); |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 51 | GrSafeUnref(fAutoFlushTarget); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 54 | //////////////////////////////////////////////////////////////////////////////// |
| 55 | |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame^] | 56 | namespace { |
| 57 | void get_vertex_bounds(const void* vertices, |
| 58 | size_t vertexSize, |
| 59 | int vertexCount, |
| 60 | SkRect* bounds) { |
| 61 | GrAssert(vertexSize >= sizeof(GrPoint)); |
| 62 | GrAssert(vertexCount > 0); |
| 63 | const GrPoint* point = static_cast<const GrPoint*>(vertices); |
| 64 | bounds->fLeft = bounds->fRight = point->fX; |
| 65 | bounds->fTop = bounds->fBottom = point->fY; |
| 66 | for (int i = 1; i < vertexCount; ++i) { |
| 67 | point = reinterpret_cast<GrPoint*>(reinterpret_cast<intptr_t>(point) + vertexSize); |
| 68 | bounds->growToInclude(point->fX, point->fY); |
| 69 | } |
| 70 | } |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 73 | void GrInOrderDrawBuffer::drawRect(const GrRect& rect, |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 74 | const SkMatrix* matrix, |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 75 | const GrRect* srcRects[], |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 76 | const SkMatrix* srcMatrices[]) { |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 77 | |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame^] | 78 | GrVertexLayout layout = 0; |
| 79 | GrDrawState::AutoColorRestore acr; |
| 80 | GrColor color = this->drawState()->getColor(); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 81 | |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame^] | 82 | // Using per-vertex colors allows batching across colors. (A lot of rects in a row differing |
| 83 | // only in color is a common occurrence in tables). However, having per-vertex colors disables |
| 84 | // blending optimizations because we don't know if the color will be solid or not. These |
| 85 | // optimizations help determine whether coverage and color can be blended correctly when |
| 86 | // dual-source blending isn't available. This comes into play when there is coverage. If colors |
| 87 | // were a stage it could take a hint that every vertex's color will be opaque. |
| 88 | if (this->getCaps().dualSourceBlendingSupport() || |
| 89 | this->getDrawState().hasSolidCoverage(this->getGeomSrc().fVertexLayout)) { |
| 90 | layout |= GrDrawState::kColor_VertexLayoutBit;; |
| 91 | // We set the draw state's color to white here. This is done so that any batching performed |
| 92 | // in our subclass's onDraw() won't get a false from GrDrawState::op== due to a color |
| 93 | // mismatch. TODO: Once vertex layout is owned by GrDrawState it should skip comparing the |
| 94 | // constant color in its op== when the kColor layout bit is set and then we can remove this. |
| 95 | acr.set(this->drawState(), 0xFFFFFFFF); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 96 | } |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 97 | |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame^] | 98 | uint32_t explicitCoordMask = 0; |
| 99 | if (NULL != srcRects) { |
| 100 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
| 101 | int numTC = 0; |
| 102 | if (NULL != srcRects[s]) { |
| 103 | layout |= GrDrawState::StageTexCoordVertexLayoutBit(s, numTC); |
| 104 | ++numTC; |
| 105 | explicitCoordMask |= (1 << s); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | AutoReleaseGeometry geo(this, layout, 4, 0); |
| 111 | if (!geo.succeeded()) { |
| 112 | GrPrintf("Failed to get space for vertices!\n"); |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 113 | return; |
| 114 | } |
| 115 | |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame^] | 116 | // Go to device coords to allow batching across matrix changes |
| 117 | SkMatrix combinedMatrix; |
| 118 | if (NULL != matrix) { |
| 119 | combinedMatrix = *matrix; |
| 120 | } else { |
| 121 | combinedMatrix.reset(); |
| 122 | } |
| 123 | combinedMatrix.postConcat(this->drawState()->getViewMatrix()); |
| 124 | // When the caller has provided an explicit source rects for a stage then we don't want to |
| 125 | // modify that stage's matrix. Otherwise if the effect is generating its source rect from |
| 126 | // the vertex positions then we have to account for the view matrix change. |
| 127 | GrDrawState::AutoDeviceCoordDraw adcd(this->drawState(), explicitCoordMask); |
| 128 | if (!adcd.succeeded()) { |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | int stageOffsets[GrDrawState::kNumStages], colorOffset; |
| 133 | int vsize = GrDrawState::VertexSizeAndOffsetsByStage(layout, stageOffsets, |
| 134 | &colorOffset, NULL, NULL); |
| 135 | |
| 136 | geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vsize); |
| 137 | combinedMatrix.mapPointsWithStride(geo.positions(), vsize, 4); |
| 138 | |
| 139 | SkRect devBounds; |
| 140 | // since we already computed the dev verts, set the bounds hint. This will help us avoid |
| 141 | // unnecessary clipping in our onDraw(). |
| 142 | get_vertex_bounds(geo.vertices(), vsize, 4, &devBounds); |
| 143 | |
| 144 | for (int i = 0; i < GrDrawState::kNumStages; ++i) { |
| 145 | if (explicitCoordMask & (1 << i)) { |
| 146 | GrAssert(NULL != stageOffsets[i]); |
| 147 | GrPoint* coords = GrTCast<GrPoint*>(GrTCast<intptr_t>(geo.vertices()) + |
| 148 | stageOffsets[i]); |
| 149 | coords->setRectFan(srcRects[i]->fLeft, srcRects[i]->fTop, |
| 150 | srcRects[i]->fRight, srcRects[i]->fBottom, |
| 151 | vsize); |
| 152 | if (NULL != srcMatrices && NULL != srcMatrices[i]) { |
| 153 | srcMatrices[i]->mapPointsWithStride(coords, vsize, 4); |
| 154 | } |
| 155 | } else { |
| 156 | GrAssert(NULL == stageOffsets[i]); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | if (colorOffset >= 0) { |
| 161 | GrColor* vertColor = GrTCast<GrColor*>(GrTCast<intptr_t>(geo.vertices()) + colorOffset); |
| 162 | for (int i = 0; i < 4; ++i) { |
| 163 | *vertColor = color; |
| 164 | vertColor = (GrColor*) ((intptr_t) vertColor + vsize); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | this->setIndexSourceToBuffer(fGpu->getQuadIndexBuffer()); |
| 169 | this->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, 4, 6, &devBounds); |
| 170 | } |
| 171 | |
| 172 | bool GrInOrderDrawBuffer::quickInsideClip(const SkRect& devBounds) { |
| 173 | if (!this->getDrawState().isClipState()) { |
| 174 | return true; |
| 175 | } |
| 176 | if (kUnknown_ClipProxyState == fClipProxyState) { |
| 177 | SkIRect rect; |
| 178 | bool iior; |
| 179 | this->getClip()->getConservativeBounds(this->getDrawState().getRenderTarget(), &rect, &iior); |
| 180 | if (iior) { |
| 181 | // The clip is a rect. We will remember that in fProxyClip. It is common for an edge (or |
| 182 | // all edges) of the clip to be at the edge of the RT. However, we get that clipping for |
| 183 | // free via the viewport. We don't want to think that clipping must be enabled in this |
| 184 | // case. So we extend the clip outward from the edge to avoid these false negatives. |
| 185 | fClipProxyState = kValid_ClipProxyState; |
| 186 | fClipProxy = SkRect::MakeFromIRect(rect); |
| 187 | |
| 188 | if (fClipProxy.fLeft <= 0) { |
| 189 | fClipProxy.fLeft = SK_ScalarMin; |
| 190 | } |
| 191 | if (fClipProxy.fTop <= 0) { |
| 192 | fClipProxy.fTop = SK_ScalarMin; |
| 193 | } |
| 194 | if (fClipProxy.fRight >= this->getDrawState().getRenderTarget()->width()) { |
| 195 | fClipProxy.fRight = SK_ScalarMax; |
| 196 | } |
| 197 | if (fClipProxy.fBottom >= this->getDrawState().getRenderTarget()->height()) { |
| 198 | fClipProxy.fBottom = SK_ScalarMax; |
| 199 | } |
| 200 | } else { |
| 201 | fClipProxyState = kInvalid_ClipProxyState; |
| 202 | } |
| 203 | } |
| 204 | if (kValid_ClipProxyState == fClipProxyState) { |
| 205 | return fClipProxy.contains(devBounds); |
| 206 | } |
| 207 | SkPoint originOffset = {SkIntToScalar(this->getClip()->fOrigin.fX), |
| 208 | SkIntToScalar(this->getClip()->fOrigin.fY)}; |
| 209 | SkRect clipSpaceBounds = devBounds; |
| 210 | clipSpaceBounds.offset(originOffset); |
| 211 | return this->getClip()->fClipStack->quickContains(clipSpaceBounds); |
| 212 | } |
| 213 | |
| 214 | int GrInOrderDrawBuffer::concatInstancedDraw(const DrawInfo& info) { |
| 215 | GrAssert(info.isInstanced()); |
| 216 | |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 217 | const GeometrySrcState& geomSrc = this->getGeomSrc(); |
| 218 | |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame^] | 219 | // we only attempt to concat the case when reserved verts are used with a client-specified index |
| 220 | // buffer. To make this work with client-specified VBs we'd need to know if the VB was updated |
| 221 | // between draws. |
| 222 | if (kReserved_GeometrySrcType != geomSrc.fVertexSrc || |
| 223 | kBuffer_GeometrySrcType != geomSrc.fIndexSrc) { |
| 224 | return 0; |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 225 | } |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame^] | 226 | // Check if there is a draw info that is compatible that uses the same VB from the pool and |
| 227 | // the same IB |
| 228 | if (kDraw_Cmd != fCmds.back()) { |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | DrawRecord* draw = &fDraws.back(); |
| 233 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 234 | const GrVertexBuffer* vertexBuffer = poolState.fPoolVertexBuffer; |
| 235 | |
| 236 | if (!draw->isInstanced() || |
| 237 | draw->verticesPerInstance() != info.verticesPerInstance() || |
| 238 | draw->indicesPerInstance() != info.indicesPerInstance() || |
| 239 | draw->fVertexBuffer != vertexBuffer || |
| 240 | draw->fIndexBuffer != geomSrc.fIndexBuffer || |
| 241 | draw->fVertexLayout != geomSrc.fVertexLayout) { |
| 242 | return 0; |
| 243 | } |
| 244 | // info does not yet account for the offset from the start of the pool's VB while the previous |
| 245 | // draw record does. |
| 246 | int adjustedStartVertex = poolState.fPoolStartVertex + info.startVertex(); |
| 247 | if (draw->startVertex() + draw->vertexCount() != adjustedStartVertex) { |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | GrAssert(poolState.fPoolStartVertex == draw->startVertex() + draw->vertexCount()); |
| 252 | |
| 253 | // how many instances can be concat'ed onto draw given the size of the index buffer |
| 254 | int instancesToConcat = this->indexCountInCurrentSource() / info.indicesPerInstance(); |
| 255 | instancesToConcat -= draw->instanceCount(); |
| 256 | instancesToConcat = GrMin(instancesToConcat, info.instanceCount()); |
| 257 | |
| 258 | // update the amount of reserved vertex data actually referenced in draws |
| 259 | size_t vertexBytes = instancesToConcat * info.verticesPerInstance() * |
| 260 | GrDrawState::VertexSize(draw->fVertexLayout); |
| 261 | poolState.fUsedPoolVertexBytes = GrMax(poolState.fUsedPoolVertexBytes, vertexBytes); |
| 262 | |
| 263 | draw->adjustInstanceCount(instancesToConcat); |
| 264 | return instancesToConcat; |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame^] | 267 | class AutoClipReenable { |
| 268 | public: |
| 269 | AutoClipReenable() : fDrawState(NULL) {} |
| 270 | ~AutoClipReenable() { |
| 271 | if (NULL != fDrawState) { |
| 272 | fDrawState->enableState(GrDrawState::kClip_StateBit); |
| 273 | } |
| 274 | } |
| 275 | void set(GrDrawState* drawState) { |
| 276 | if (drawState->isClipState()) { |
| 277 | fDrawState = drawState; |
| 278 | drawState->disableState(GrDrawState::kClip_StateBit); |
| 279 | } |
| 280 | } |
| 281 | private: |
| 282 | GrDrawState* fDrawState; |
| 283 | }; |
| 284 | |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 285 | void GrInOrderDrawBuffer::onDraw(const DrawInfo& info) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 286 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 287 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame^] | 288 | AutoClipReenable acr; |
| 289 | |
| 290 | if (this->getDrawState().isClipState() && |
| 291 | NULL != info.getDevBounds() && |
| 292 | this->quickInsideClip(*info.getDevBounds())) { |
| 293 | acr.set(this->drawState()); |
| 294 | } |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 295 | |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 296 | if (this->needsNewClip()) { |
| 297 | this->recordClip(); |
| 298 | } |
| 299 | if (this->needsNewState()) { |
| 300 | this->recordState(); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame^] | 303 | DrawRecord* draw; |
| 304 | if (info.isInstanced()) { |
| 305 | int instancesConcated = this->concatInstancedDraw(info); |
| 306 | if (info.instanceCount() > instancesConcated) { |
| 307 | draw = this->recordDraw(info); |
| 308 | draw->adjustInstanceCount(-instancesConcated); |
| 309 | } else { |
| 310 | return; |
| 311 | } |
| 312 | } else { |
| 313 | draw = this->recordDraw(info); |
| 314 | } |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 315 | draw->fVertexLayout = this->getVertexLayout(); |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 316 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 317 | switch (this->getGeomSrc().fVertexSrc) { |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 318 | case kBuffer_GeometrySrcType: |
| 319 | draw->fVertexBuffer = this->getGeomSrc().fVertexBuffer; |
| 320 | break; |
| 321 | case kReserved_GeometrySrcType: // fallthrough |
| 322 | case kArray_GeometrySrcType: { |
| 323 | size_t vertexBytes = (info.vertexCount() + info.startVertex()) * |
| 324 | GrDrawState::VertexSize(draw->fVertexLayout); |
| 325 | poolState.fUsedPoolVertexBytes = GrMax(poolState.fUsedPoolVertexBytes, vertexBytes); |
| 326 | draw->fVertexBuffer = poolState.fPoolVertexBuffer; |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame^] | 327 | draw->adjustStartVertex(poolState.fPoolStartVertex); |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 328 | break; |
| 329 | } |
| 330 | default: |
| 331 | GrCrash("unknown geom src type"); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 332 | } |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 333 | draw->fVertexBuffer->ref(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 334 | |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 335 | if (info.isIndexed()) { |
| 336 | switch (this->getGeomSrc().fIndexSrc) { |
| 337 | case kBuffer_GeometrySrcType: |
| 338 | draw->fIndexBuffer = this->getGeomSrc().fIndexBuffer; |
| 339 | break; |
| 340 | case kReserved_GeometrySrcType: // fallthrough |
| 341 | case kArray_GeometrySrcType: { |
| 342 | size_t indexBytes = (info.indexCount() + info.startIndex()) * sizeof(uint16_t); |
| 343 | poolState.fUsedPoolIndexBytes = GrMax(poolState.fUsedPoolIndexBytes, indexBytes); |
| 344 | draw->fIndexBuffer = poolState.fPoolIndexBuffer; |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame^] | 345 | draw->adjustStartIndex(poolState.fPoolStartIndex); |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 346 | break; |
| 347 | } |
| 348 | default: |
| 349 | GrCrash("unknown geom src type"); |
| 350 | } |
| 351 | draw->fIndexBuffer->ref(); |
| 352 | } else { |
| 353 | draw->fIndexBuffer = NULL; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 354 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 357 | GrInOrderDrawBuffer::StencilPath::StencilPath() : fStroke(SkStrokeRec::kFill_InitStyle) {} |
| 358 | |
| 359 | void GrInOrderDrawBuffer::onStencilPath(const GrPath* path, const SkStrokeRec& stroke, |
| sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 360 | SkPath::FillType fill) { |
| bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 361 | if (this->needsNewClip()) { |
| 362 | this->recordClip(); |
| 363 | } |
| 364 | // Only compare the subset of GrDrawState relevant to path stenciling? |
| 365 | if (this->needsNewState()) { |
| 366 | this->recordState(); |
| 367 | } |
| 368 | StencilPath* sp = this->recordStencilPath(); |
| 369 | sp->fPath.reset(path); |
| 370 | path->ref(); |
| 371 | sp->fFill = fill; |
| sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 372 | sp->fStroke = stroke; |
| bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame^] | 375 | void GrInOrderDrawBuffer::clear(const GrIRect* rect, GrColor color, GrRenderTarget* renderTarget) { |
| bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 376 | GrIRect r; |
| bsalomon@google.com | 1b3ce47 | 2012-08-17 13:43:08 +0000 | [diff] [blame] | 377 | if (NULL == renderTarget) { |
| 378 | renderTarget = this->drawState()->getRenderTarget(); |
| 379 | GrAssert(NULL != renderTarget); |
| 380 | } |
| bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 381 | if (NULL == rect) { |
| 382 | // We could do something smart and remove previous draws and clears to |
| 383 | // the current render target. If we get that smart we have to make sure |
| 384 | // those draws aren't read before this clear (render-to-texture). |
| bsalomon@google.com | 1b3ce47 | 2012-08-17 13:43:08 +0000 | [diff] [blame] | 385 | r.setLTRB(0, 0, renderTarget->width(), renderTarget->height()); |
| bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 386 | rect = &r; |
| 387 | } |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 388 | Clear* clr = this->recordClear(); |
| 389 | clr->fColor = color; |
| 390 | clr->fRect = *rect; |
| 391 | clr->fRenderTarget = renderTarget; |
| bsalomon@google.com | 1b3ce47 | 2012-08-17 13:43:08 +0000 | [diff] [blame] | 392 | renderTarget->ref(); |
| bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 393 | } |
| 394 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 395 | void GrInOrderDrawBuffer::reset() { |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 396 | GrAssert(1 == fGeoPoolStateStack.count()); |
| 397 | this->resetVertexSource(); |
| 398 | this->resetIndexSource(); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 399 | int numDraws = fDraws.count(); |
| 400 | for (int d = 0; d < numDraws; ++d) { |
| 401 | // we always have a VB, but not always an IB |
| 402 | GrAssert(NULL != fDraws[d].fVertexBuffer); |
| 403 | fDraws[d].fVertexBuffer->unref(); |
| 404 | GrSafeUnref(fDraws[d].fIndexBuffer); |
| 405 | } |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 406 | fCmds.reset(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 407 | fDraws.reset(); |
| bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 408 | fStencilPaths.reset(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 409 | fStates.reset(); |
| bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 410 | fClears.reset(); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 411 | fVertexPool.reset(); |
| 412 | fIndexPool.reset(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 413 | fClips.reset(); |
| robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 414 | fClipOrigins.reset(); |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 415 | fClipSet = true; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| bsalomon@google.com | 55e4a20 | 2013-01-11 13:54:21 +0000 | [diff] [blame] | 418 | bool GrInOrderDrawBuffer::flushTo(GrDrawTarget* target) { |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 419 | GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fVertexSrc); |
| 420 | GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fIndexSrc); |
| bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 421 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 422 | GrAssert(NULL != target); |
| 423 | GrAssert(target != this); // not considered and why? |
| 424 | |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 425 | int numCmds = fCmds.count(); |
| bsalomon@google.com | 358e427 | 2013-01-10 14:40:28 +0000 | [diff] [blame] | 426 | if (0 == numCmds) { |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 427 | return false; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 430 | fVertexPool.unlock(); |
| 431 | fIndexPool.unlock(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 432 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 433 | GrDrawTarget::AutoClipRestore acr(target); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 434 | AutoGeometryPush agp(target); |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 435 | |
| 436 | GrDrawState playbackState; |
| bsalomon@google.com | a5d056a | 2012-03-27 15:59:58 +0000 | [diff] [blame] | 437 | GrDrawState* prevDrawState = target->drawState(); |
| 438 | prevDrawState->ref(); |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 439 | target->setDrawState(&playbackState); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 440 | |
| robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 441 | GrClipData clipData; |
| 442 | |
| bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 443 | int currState = 0; |
| 444 | int currClip = 0; |
| 445 | int currClear = 0; |
| 446 | int currDraw = 0; |
| 447 | int currStencilPath = 0; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 448 | |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 449 | |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 450 | for (int c = 0; c < numCmds; ++c) { |
| 451 | switch (fCmds[c]) { |
| 452 | case kDraw_Cmd: { |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 453 | const DrawRecord& draw = fDraws[currDraw]; |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 454 | target->setVertexSourceToBuffer(draw.fVertexLayout, draw.fVertexBuffer); |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame^] | 455 | if (draw.isIndexed()) { |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 456 | target->setIndexSourceToBuffer(draw.fIndexBuffer); |
| 457 | } |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame^] | 458 | target->executeDraw(draw); |
| bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 459 | |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 460 | ++currDraw; |
| 461 | break; |
| 462 | } |
| bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 463 | case kStencilPath_Cmd: { |
| 464 | const StencilPath& sp = fStencilPaths[currStencilPath]; |
| sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 465 | target->stencilPath(sp.fPath.get(), sp.fStroke, sp.fFill); |
| bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 466 | ++currStencilPath; |
| 467 | break; |
| 468 | } |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 469 | case kSetState_Cmd: |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 470 | fStates[currState].restoreTo(&playbackState); |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 471 | ++currState; |
| 472 | break; |
| 473 | case kSetClip_Cmd: |
| robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 474 | clipData.fClipStack = &fClips[currClip]; |
| 475 | clipData.fOrigin = fClipOrigins[currClip]; |
| 476 | target->setClip(&clipData); |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 477 | ++currClip; |
| 478 | break; |
| 479 | case kClear_Cmd: |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 480 | target->clear(&fClears[currClear].fRect, |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 481 | fClears[currClear].fColor, |
| 482 | fClears[currClear].fRenderTarget); |
| 483 | ++currClear; |
| 484 | break; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 485 | } |
| 486 | } |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 487 | // we should have consumed all the states, clips, etc. |
| 488 | GrAssert(fStates.count() == currState); |
| 489 | GrAssert(fClips.count() == currClip); |
| robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 490 | GrAssert(fClipOrigins.count() == currClip); |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 491 | GrAssert(fClears.count() == currClear); |
| 492 | GrAssert(fDraws.count() == currDraw); |
| 493 | |
| bsalomon@google.com | a5d056a | 2012-03-27 15:59:58 +0000 | [diff] [blame] | 494 | target->setDrawState(prevDrawState); |
| 495 | prevDrawState->unref(); |
| bsalomon@google.com | 55e4a20 | 2013-01-11 13:54:21 +0000 | [diff] [blame] | 496 | this->reset(); |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 497 | return true; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 500 | void GrInOrderDrawBuffer::setAutoFlushTarget(GrDrawTarget* target) { |
| 501 | GrSafeAssign(fAutoFlushTarget, target); |
| 502 | } |
| 503 | |
| 504 | void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace( |
| jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 505 | size_t vertexSize, |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 506 | int vertexCount, |
| 507 | int indexCount) { |
| 508 | if (NULL != fAutoFlushTarget) { |
| 509 | // We use geometryHints() to know whether to flush the draw buffer. We |
| 510 | // can't flush if we are inside an unbalanced pushGeometrySource. |
| 511 | // Moreover, flushing blows away vertex and index data that was |
| 512 | // previously reserved. So if the vertex or index data is pulled from |
| 513 | // reserved space and won't be released by this request then we can't |
| 514 | // flush. |
| 515 | bool insideGeoPush = fGeoPoolStateStack.count() > 1; |
| 516 | |
| 517 | bool unreleasedVertexSpace = |
| 518 | !vertexCount && |
| 519 | kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc; |
| 520 | |
| 521 | bool unreleasedIndexSpace = |
| 522 | !indexCount && |
| 523 | kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc; |
| 524 | |
| 525 | // we don't want to finalize any reserved geom on the target since |
| 526 | // we don't know that the client has finished writing to it. |
| 527 | bool targetHasReservedGeom = |
| 528 | fAutoFlushTarget->hasReservedVerticesOrIndices(); |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 529 | |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 530 | int vcount = vertexCount; |
| 531 | int icount = indexCount; |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 532 | |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 533 | if (!insideGeoPush && |
| 534 | !unreleasedVertexSpace && |
| 535 | !unreleasedIndexSpace && |
| 536 | !targetHasReservedGeom && |
| jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 537 | this->geometryHints(vertexSize, &vcount, &icount)) { |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 538 | |
| 539 | this->flushTo(fAutoFlushTarget); |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | |
| jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 544 | bool GrInOrderDrawBuffer::geometryHints(size_t vertexSize, |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 545 | int* vertexCount, |
| 546 | int* indexCount) const { |
| 547 | // we will recommend a flush if the data could fit in a single |
| 548 | // preallocated buffer but none are left and it can't fit |
| 549 | // in the current buffer (which may not be prealloced). |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 550 | bool flush = false; |
| 551 | if (NULL != indexCount) { |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 552 | int32_t currIndices = fIndexPool.currentBufferIndices(); |
| 553 | if (*indexCount > currIndices && |
| 554 | (!fIndexPool.preallocatedBuffersRemaining() && |
| 555 | *indexCount <= fIndexPool.preallocatedBufferIndices())) { |
| 556 | |
| 557 | flush = true; |
| 558 | } |
| 559 | *indexCount = currIndices; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 560 | } |
| 561 | if (NULL != vertexCount) { |
| jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 562 | int32_t currVertices = fVertexPool.currentBufferVertices(vertexSize); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 563 | if (*vertexCount > currVertices && |
| 564 | (!fVertexPool.preallocatedBuffersRemaining() && |
| jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 565 | *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexSize))) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 566 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 567 | flush = true; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 568 | } |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 569 | *vertexCount = currVertices; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 570 | } |
| 571 | return flush; |
| 572 | } |
| 573 | |
| jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 574 | bool GrInOrderDrawBuffer::onReserveVertexSpace(size_t vertexSize, |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 575 | int vertexCount, |
| 576 | void** vertices) { |
| 577 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 578 | GrAssert(vertexCount > 0); |
| 579 | GrAssert(NULL != vertices); |
| 580 | GrAssert(0 == poolState.fUsedPoolVertexBytes); |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 581 | |
| jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 582 | *vertices = fVertexPool.makeSpace(vertexSize, |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 583 | vertexCount, |
| 584 | &poolState.fPoolVertexBuffer, |
| 585 | &poolState.fPoolStartVertex); |
| 586 | return NULL != *vertices; |
| 587 | } |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 588 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 589 | bool GrInOrderDrawBuffer::onReserveIndexSpace(int indexCount, void** indices) { |
| 590 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 591 | GrAssert(indexCount > 0); |
| 592 | GrAssert(NULL != indices); |
| 593 | GrAssert(0 == poolState.fUsedPoolIndexBytes); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 594 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 595 | *indices = fIndexPool.makeSpace(indexCount, |
| 596 | &poolState.fPoolIndexBuffer, |
| 597 | &poolState.fPoolStartIndex); |
| 598 | return NULL != *indices; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 601 | void GrInOrderDrawBuffer::releaseReservedVertexSpace() { |
| 602 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 603 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| bsalomon@google.com | d57d71a | 2012-08-16 16:26:33 +0000 | [diff] [blame] | 604 | |
| 605 | // If we get a release vertex space call then our current source should either be reserved |
| 606 | // or array (which we copied into reserved space). |
| 607 | GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc || |
| 608 | kArray_GeometrySrcType == geoSrc.fVertexSrc); |
| bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 609 | |
| 610 | // When the caller reserved vertex buffer space we gave it back a pointer |
| 611 | // provided by the vertex buffer pool. At each draw we tracked the largest |
| 612 | // offset into the pool's pointer that was referenced. Now we return to the |
| 613 | // pool any portion at the tail of the allocation that no draw referenced. |
| jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 614 | size_t reservedVertexBytes = GrDrawState::VertexSize(geoSrc.fVertexLayout) * |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 615 | geoSrc.fVertexCount; |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 616 | fVertexPool.putBack(reservedVertexBytes - |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 617 | poolState.fUsedPoolVertexBytes); |
| 618 | poolState.fUsedPoolVertexBytes = 0; |
| bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 619 | poolState.fPoolVertexBuffer = NULL; |
| 620 | poolState.fPoolStartVertex = 0; |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 623 | void GrInOrderDrawBuffer::releaseReservedIndexSpace() { |
| 624 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 625 | const GeometrySrcState& geoSrc = this->getGeomSrc(); |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 626 | |
| bsalomon@google.com | d57d71a | 2012-08-16 16:26:33 +0000 | [diff] [blame] | 627 | // If we get a release index space call then our current source should either be reserved |
| 628 | // or array (which we copied into reserved space). |
| 629 | GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc || |
| 630 | kArray_GeometrySrcType == geoSrc.fIndexSrc); |
| bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 631 | |
| 632 | // Similar to releaseReservedVertexSpace we return any unused portion at |
| 633 | // the tail |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 634 | size_t reservedIndexBytes = sizeof(uint16_t) * geoSrc.fIndexCount; |
| 635 | fIndexPool.putBack(reservedIndexBytes - poolState.fUsedPoolIndexBytes); |
| 636 | poolState.fUsedPoolIndexBytes = 0; |
| bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 637 | poolState.fPoolIndexBuffer = NULL; |
| 638 | poolState.fPoolStartIndex = 0; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 639 | } |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 640 | |
| bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 641 | void GrInOrderDrawBuffer::onSetVertexSourceToArray(const void* vertexArray, |
| 642 | int vertexCount) { |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 643 | |
| 644 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 645 | GrAssert(0 == poolState.fUsedPoolVertexBytes); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 646 | #if GR_DEBUG |
| 647 | bool success = |
| 648 | #endif |
| jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 649 | fVertexPool.appendVertices(GrDrawState::VertexSize(this->getVertexLayout()), |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 650 | vertexCount, |
| 651 | vertexArray, |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 652 | &poolState.fPoolVertexBuffer, |
| 653 | &poolState.fPoolStartVertex); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 654 | GR_DEBUGASSERT(success); |
| 655 | } |
| 656 | |
| bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 657 | void GrInOrderDrawBuffer::onSetIndexSourceToArray(const void* indexArray, |
| 658 | int indexCount) { |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 659 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 660 | GrAssert(0 == poolState.fUsedPoolIndexBytes); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 661 | #if GR_DEBUG |
| 662 | bool success = |
| 663 | #endif |
| 664 | fIndexPool.appendIndices(indexCount, |
| 665 | indexArray, |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 666 | &poolState.fPoolIndexBuffer, |
| 667 | &poolState.fPoolStartIndex); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 668 | GR_DEBUGASSERT(success); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| bsalomon@google.com | 3f5a95e | 2012-03-08 16:41:42 +0000 | [diff] [blame] | 671 | void GrInOrderDrawBuffer::releaseVertexArray() { |
| 672 | // When the client provides an array as the vertex source we handled it |
| 673 | // by copying their array into reserved space. |
| 674 | this->GrInOrderDrawBuffer::releaseReservedVertexSpace(); |
| 675 | } |
| 676 | |
| 677 | void GrInOrderDrawBuffer::releaseIndexArray() { |
| 678 | // When the client provides an array as the index source we handled it |
| 679 | // by copying their array into reserved space. |
| 680 | this->GrInOrderDrawBuffer::releaseReservedIndexSpace(); |
| 681 | } |
| 682 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 683 | void GrInOrderDrawBuffer::geometrySourceWillPush() { |
| 684 | GeometryPoolState& poolState = fGeoPoolStateStack.push_back(); |
| 685 | poolState.fUsedPoolVertexBytes = 0; |
| 686 | poolState.fUsedPoolIndexBytes = 0; |
| 687 | #if GR_DEBUG |
| 688 | poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0; |
| 689 | poolState.fPoolStartVertex = ~0; |
| 690 | poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0; |
| 691 | poolState.fPoolStartIndex = ~0; |
| 692 | #endif |
| 693 | } |
| 694 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 695 | void GrInOrderDrawBuffer::geometrySourceWillPop( |
| 696 | const GeometrySrcState& restoredState) { |
| 697 | GrAssert(fGeoPoolStateStack.count() > 1); |
| 698 | fGeoPoolStateStack.pop_back(); |
| 699 | GeometryPoolState& poolState = fGeoPoolStateStack.back(); |
| 700 | // we have to assume that any slack we had in our vertex/index data |
| 701 | // is now unreleasable because data may have been appended later in the |
| 702 | // pool. |
| 703 | if (kReserved_GeometrySrcType == restoredState.fVertexSrc || |
| 704 | kArray_GeometrySrcType == restoredState.fVertexSrc) { |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 705 | poolState.fUsedPoolVertexBytes = |
| jvanverth@google.com | cc78238 | 2013-01-28 20:39:48 +0000 | [diff] [blame] | 706 | GrDrawState::VertexSize(restoredState.fVertexLayout) * |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 707 | restoredState.fVertexCount; |
| 708 | } |
| 709 | if (kReserved_GeometrySrcType == restoredState.fIndexSrc || |
| 710 | kArray_GeometrySrcType == restoredState.fIndexSrc) { |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 711 | poolState.fUsedPoolIndexBytes = sizeof(uint16_t) * |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 712 | restoredState.fIndexCount; |
| 713 | } |
| 714 | } |
| 715 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 716 | bool GrInOrderDrawBuffer::needsNewState() const { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 717 | return fStates.empty() || !fStates.back().isEqual(this->getDrawState()); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 720 | bool GrInOrderDrawBuffer::needsNewClip() const { |
| bsalomon@google.com | 358e427 | 2013-01-10 14:40:28 +0000 | [diff] [blame] | 721 | GrAssert(fClips.count() == fClipOrigins.count()); |
| 722 | if (this->getDrawState().isClipState()) { |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 723 | if (fClipSet && |
| bsalomon@google.com | 358e427 | 2013-01-10 14:40:28 +0000 | [diff] [blame] | 724 | (fClips.empty() || |
| bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 725 | fClips.back() != *this->getClip()->fClipStack || |
| 726 | fClipOrigins.back() != this->getClip()->fOrigin)) { |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 727 | return true; |
| 728 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 729 | } |
| 730 | return false; |
| 731 | } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 732 | |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 733 | void GrInOrderDrawBuffer::recordClip() { |
| bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 734 | fClips.push_back() = *this->getClip()->fClipStack; |
| 735 | fClipOrigins.push_back() = this->getClip()->fOrigin; |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 736 | fClipSet = false; |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 737 | fCmds.push_back(kSetClip_Cmd); |
| 738 | } |
| 739 | |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 740 | void GrInOrderDrawBuffer::recordState() { |
| bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 741 | fStates.push_back().saveFrom(this->getDrawState()); |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 742 | fCmds.push_back(kSetState_Cmd); |
| 743 | } |
| 744 | |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 745 | GrInOrderDrawBuffer::DrawRecord* GrInOrderDrawBuffer::recordDraw(const DrawInfo& info) { |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame^] | 746 | fCmds.push_back(kDraw_Cmd); |
| 747 | return &fDraws.push_back(info); |
| bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 748 | } |
| 749 | |
| bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 750 | GrInOrderDrawBuffer::StencilPath* GrInOrderDrawBuffer::recordStencilPath() { |
| 751 | fCmds.push_back(kStencilPath_Cmd); |
| 752 | return &fStencilPaths.push_back(); |
| 753 | } |
| 754 | |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 755 | GrInOrderDrawBuffer::Clear* GrInOrderDrawBuffer::recordClear() { |
| 756 | fCmds.push_back(kClear_Cmd); |
| 757 | return &fClears.push_back(); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 758 | } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 759 | |
| robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 760 | void GrInOrderDrawBuffer::clipWillBeSet(const GrClipData* newClipData) { |
| 761 | INHERITED::clipWillBeSet(newClipData); |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 762 | fClipSet = true; |
| bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame^] | 763 | fClipProxyState = kUnknown_ClipProxyState; |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 764 | } |