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