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