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