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