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