blob: 7bd3f5d9963a3a1376399d5f3e4ded737416bc58 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
10#include "GrInOrderDrawBuffer.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000011#include "GrBufferAllocPool.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000012#include "GrDrawTargetCaps.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000013#include "GrGpu.h"
14#include "GrIndexBuffer.h"
15#include "GrPath.h"
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000016#include "GrRenderTarget.h"
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000017#include "GrTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018#include "GrTexture.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000019#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000021GrInOrderDrawBuffer::GrInOrderDrawBuffer(GrGpu* gpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +000022 GrVertexBufferAllocPool* vertexPool,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000023 GrIndexBufferAllocPool* indexPool)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000024 : GrDrawTarget(gpu->getContext())
25 , fDstGpu(gpu)
bsalomon@google.com97805382012-03-13 14:32:07 +000026 , fClipSet(true)
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000027 , fClipProxyState(kUnknown_ClipProxyState)
robertphillips@google.com69705572012-03-21 19:46:50 +000028 , fVertexPool(*vertexPool)
29 , fIndexPool(*indexPool)
robertphillips@google.comc82a8b72012-06-21 20:15:48 +000030 , fFlushing(false) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +000031
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000032 fDstGpu->ref();
bsalomon@google.combcce8922013-03-25 15:38:39 +000033 fCaps.reset(SkRef(fDstGpu->caps()));
bsalomon@google.com18c9c192011-09-22 21:01:31 +000034
bsalomon@google.com1c13c962011-02-14 16:51:21 +000035 GrAssert(NULL != vertexPool);
36 GrAssert(NULL != indexPool);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000037
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.coma4f6b102012-06-26 21:04:22 +000047 this->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000048}
49
50GrInOrderDrawBuffer::~GrInOrderDrawBuffer() {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000051 this->reset();
bsalomon@google.com4a018bb2011-10-28 19:50:21 +000052 // This must be called by before the GrDrawTarget destructor
53 this->releaseGeometry();
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000054 fDstGpu->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +000055}
56
bsalomon@google.com934c5702012-03-20 21:17:58 +000057////////////////////////////////////////////////////////////////////////////////
58
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000059namespace {
60void 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.com934c5702012-03-20 21:17:58 +000074}
75
bsalomon@google.com0406b9e2013-04-02 21:00:15 +000076void GrInOrderDrawBuffer::onDrawRect(const GrRect& rect,
77 const SkMatrix* matrix,
78 const GrRect* localRect,
79 const SkMatrix* localMatrix) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000080 GrDrawState::AutoColorRestore acr;
jvanverth@google.com9b855c72013-03-01 18:21:22 +000081
82 GrDrawState* drawState = this->drawState();
83
84 GrColor color = drawState->getColor();
85 GrVertexAttribArray<3> attribs;
jvanverth@google.com9b855c72013-03-01 18:21:22 +000086
87 // set position attrib
jvanverth@google.com054ae992013-04-01 20:06:51 +000088 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.com86afc2a2011-02-16 16:12:19 +000095
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000096 // 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.com054ae992013-04-01 20:06:51 +0000102 if (this->caps()->dualSourceBlendingSupport() || drawState->hasSolidCoverage()) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000103 colorOffset = currentOffset;
jvanverth@google.com054ae992013-04-01 20:06:51 +0000104 GrVertexAttrib colorAttrib =
jvanverth@google.comb084c772013-04-01 20:28:08 +0000105 {kVec4ub_GrVertexAttribType, currentOffset, kColor_GrVertexAttribBinding};
jvanverth@google.com054ae992013-04-01 20:06:51 +0000106 attribs.push_back(colorAttrib);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000107 currentOffset += sizeof(GrColor);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000108 // 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.com9b855c72013-03-01 18:21:22 +0000112 acr.set(drawState, 0xFFFFFFFF);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000113 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000114
bsalomon@google.comc7818882013-03-20 19:19:53 +0000115 if (NULL != localRect) {
bsalomon@google.comc7818882013-03-20 19:19:53 +0000116 localOffset = currentOffset;
jvanverth@google.com054ae992013-04-01 20:06:51 +0000117 GrVertexAttrib localCoordAttrib =
jvanverth@google.comb084c772013-04-01 20:28:08 +0000118 {kVec2f_GrVertexAttribType, currentOffset, kLocalCoord_GrVertexAttribBinding};
jvanverth@google.com054ae992013-04-01 20:06:51 +0000119 attribs.push_back(localCoordAttrib);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000120 currentOffset += sizeof(GrPoint);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000121 }
122
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000123 drawState->setVertexAttribs(attribs.begin(), attribs.count());
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000124 AutoReleaseGeometry geo(this, 4, 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000125 if (!geo.succeeded()) {
126 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com934c5702012-03-20 21:17:58 +0000127 return;
128 }
129
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000130 // 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.com9b855c72013-03-01 18:21:22 +0000137 combinedMatrix.postConcat(drawState->getViewMatrix());
jvanverth@google.com39768252013-02-14 15:25:44 +0000138 // When the caller has provided an explicit source rect for a stage then we don't want to
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000139 // 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.comc7818882013-03-20 19:19:53 +0000141 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000142 if (!adcd.succeeded()) {
143 return;
144 }
145
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000146 size_t vsize = drawState->getVertexSize();
147 GrAssert(vsize == currentOffset);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000148
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.comc7818882013-03-20 19:19:53 +0000157 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.com9b855c72013-03-01 18:21:22 +0000161 vsize);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000162 if (NULL != localMatrix) {
163 localMatrix->mapPointsWithStride(coords, vsize, 4);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000164 }
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.com6e4e6502013-02-25 20:12:45 +0000175 this->setIndexSourceToBuffer(this->getContext()->getQuadIndexBuffer());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000176 this->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, 4, 6, &devBounds);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000177
178 // to ensure that stashing the drawState ptr is valid
179 GrAssert(this->drawState() == drawState);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000180}
181
182bool 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
224int GrInOrderDrawBuffer::concatInstancedDraw(const DrawInfo& info) {
225 GrAssert(info.isInstanced());
226
bsalomon@google.com934c5702012-03-20 21:17:58 +0000227 const GeometrySrcState& geomSrc = this->getGeomSrc();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000228 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000229
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000230 // 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.com934c5702012-03-20 21:17:58 +0000236 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000237 // 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.comb75b0a02013-02-05 20:33:30 +0000251 draw->fIndexBuffer != geomSrc.fIndexBuffer) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000252 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.comae683922013-02-06 07:01:54 +0000269 size_t vertexBytes = instancesToConcat * info.verticesPerInstance() *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000270 drawState.getVertexSize();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000271 poolState.fUsedPoolVertexBytes = GrMax(poolState.fUsedPoolVertexBytes, vertexBytes);
272
273 draw->adjustInstanceCount(instancesToConcat);
274 return instancesToConcat;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000275}
276
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000277class AutoClipReenable {
278public:
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 }
291private:
292 GrDrawState* fDrawState;
293};
294
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000295void GrInOrderDrawBuffer::onDraw(const DrawInfo& info) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000296
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000297 GeometryPoolState& poolState = fGeoPoolStateStack.back();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000298 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000299 AutoClipReenable acr;
300
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000301 if (drawState.isClipState() &&
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000302 NULL != info.getDevBounds() &&
303 this->quickInsideClip(*info.getDevBounds())) {
304 acr.set(this->drawState());
305 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000306
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000307 if (this->needsNewClip()) {
308 this->recordClip();
309 }
310 if (this->needsNewState()) {
311 this->recordState();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000312 }
313
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000314 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.com74749cd2013-01-30 16:12:41 +0000326
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000327 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000328 case kBuffer_GeometrySrcType:
329 draw->fVertexBuffer = this->getGeomSrc().fVertexBuffer;
330 break;
331 case kReserved_GeometrySrcType: // fallthrough
332 case kArray_GeometrySrcType: {
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000333 size_t vertexBytes = (info.vertexCount() + info.startVertex()) *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000334 drawState.getVertexSize();
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000335 poolState.fUsedPoolVertexBytes = GrMax(poolState.fUsedPoolVertexBytes, vertexBytes);
336 draw->fVertexBuffer = poolState.fPoolVertexBuffer;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000337 draw->adjustStartVertex(poolState.fPoolStartVertex);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000338 break;
339 }
340 default:
341 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000342 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000343 draw->fVertexBuffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000344
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000345 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.comd62e88e2013-02-01 14:19:27 +0000355 draw->adjustStartIndex(poolState.fPoolStartIndex);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000356 break;
357 }
358 default:
359 GrCrash("unknown geom src type");
360 }
361 draw->fIndexBuffer->ref();
362 } else {
363 draw->fIndexBuffer = NULL;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000364 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000365}
366
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000367GrInOrderDrawBuffer::StencilPath::StencilPath() : fStroke(SkStrokeRec::kFill_InitStyle) {}
368
369void GrInOrderDrawBuffer::onStencilPath(const GrPath* path, const SkStrokeRec& stroke,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000370 SkPath::FillType fill) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000371 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.com12b4e272012-12-06 20:13:11 +0000382 sp->fStroke = stroke;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000383}
384
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000385void GrInOrderDrawBuffer::clear(const GrIRect* rect, GrColor color, GrRenderTarget* renderTarget) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000386 GrIRect r;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000387 if (NULL == renderTarget) {
388 renderTarget = this->drawState()->getRenderTarget();
389 GrAssert(NULL != renderTarget);
390 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000391 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.com1b3ce472012-08-17 13:43:08 +0000395 r.setLTRB(0, 0, renderTarget->width(), renderTarget->height());
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000396 rect = &r;
397 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000398 Clear* clr = this->recordClear();
399 clr->fColor = color;
400 clr->fRect = *rect;
401 clr->fRenderTarget = renderTarget;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000402 renderTarget->ref();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000403}
404
reed@google.comac10a2d2010-12-22 21:39:39 +0000405void GrInOrderDrawBuffer::reset() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000406 GrAssert(1 == fGeoPoolStateStack.count());
407 this->resetVertexSource();
408 this->resetIndexSource();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000409 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.coma4f6b102012-06-26 21:04:22 +0000416 fCmds.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000417 fDraws.reset();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000418 fStencilPaths.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000419 fStates.reset();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000420 fClears.reset();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000421 fVertexPool.reset();
422 fIndexPool.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000423 fClips.reset();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000424 fClipOrigins.reset();
bsalomon@google.com116ad842013-04-09 15:38:19 +0000425 fCopySurfaces.reset();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000426 fClipSet = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000427}
428
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000429bool GrInOrderDrawBuffer::flush() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000430 GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fVertexSrc);
431 GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000432
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000433 int numCmds = fCmds.count();
bsalomon@google.com358e4272013-01-10 14:40:28 +0000434 if (0 == numCmds) {
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000435 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000436 }
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000437 GrAssert(!fFlushing);
438
439 GrAutoTRestore<bool> flushRestore(&fFlushing);
440 fFlushing = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000441
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000442 fVertexPool.unlock();
443 fIndexPool.unlock();
reed@google.comac10a2d2010-12-22 21:39:39 +0000444
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000445 GrDrawTarget::AutoClipRestore acr(fDstGpu);
446 AutoGeometryAndStatePush agasp(fDstGpu, kPreserve_ASRInit);
bsalomon@google.comca432082013-01-23 19:53:46 +0000447
448 GrDrawState playbackState;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000449 GrDrawState* prevDrawState = fDstGpu->drawState();
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000450 prevDrawState->ref();
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000451 fDstGpu->setDrawState(&playbackState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000452
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000453 GrClipData clipData;
454
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000455 int currState = 0;
456 int currClip = 0;
457 int currClear = 0;
458 int currDraw = 0;
459 int currStencilPath = 0;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000460 int currCopySurface = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000461
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000462 for (int c = 0; c < numCmds; ++c) {
463 switch (fCmds[c]) {
464 case kDraw_Cmd: {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000465 const DrawRecord& draw = fDraws[currDraw];
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000466 fDstGpu->setVertexSourceToBuffer(draw.fVertexBuffer);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000467 if (draw.isIndexed()) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000468 fDstGpu->setIndexSourceToBuffer(draw.fIndexBuffer);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000469 }
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000470 fDstGpu->executeDraw(draw);
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000471
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000472 ++currDraw;
473 break;
474 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000475 case kStencilPath_Cmd: {
476 const StencilPath& sp = fStencilPaths[currStencilPath];
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000477 fDstGpu->stencilPath(sp.fPath.get(), sp.fStroke, sp.fFill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000478 ++currStencilPath;
479 break;
480 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000481 case kSetState_Cmd:
bsalomon@google.comca432082013-01-23 19:53:46 +0000482 fStates[currState].restoreTo(&playbackState);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000483 ++currState;
484 break;
485 case kSetClip_Cmd:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000486 clipData.fClipStack = &fClips[currClip];
487 clipData.fOrigin = fClipOrigins[currClip];
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000488 fDstGpu->setClip(&clipData);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000489 ++currClip;
490 break;
491 case kClear_Cmd:
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000492 fDstGpu->clear(&fClears[currClear].fRect,
493 fClears[currClear].fColor,
494 fClears[currClear].fRenderTarget);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000495 ++currClear;
496 break;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000497 case kCopySurface_Cmd:
498 fDstGpu->copySurface(fCopySurfaces[currCopySurface].fDst.get(),
499 fCopySurfaces[currCopySurface].fSrc.get(),
500 fCopySurfaces[currCopySurface].fSrcRect,
501 fCopySurfaces[currCopySurface].fDstPoint);
502 ++currCopySurface;
503 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000504 }
505 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000506 // we should have consumed all the states, clips, etc.
507 GrAssert(fStates.count() == currState);
508 GrAssert(fClips.count() == currClip);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000509 GrAssert(fClipOrigins.count() == currClip);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000510 GrAssert(fClears.count() == currClear);
511 GrAssert(fDraws.count() == currDraw);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000512 GrAssert(fCopySurfaces.count() == currCopySurface);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000513
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000514 fDstGpu->setDrawState(prevDrawState);
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000515 prevDrawState->unref();
bsalomon@google.com55e4a202013-01-11 13:54:21 +0000516 this->reset();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000517 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000518}
519
bsalomon@google.com116ad842013-04-09 15:38:19 +0000520bool GrInOrderDrawBuffer::onCopySurface(GrSurface* dst,
521 GrSurface* src,
522 const SkIRect& srcRect,
523 const SkIPoint& dstPoint) {
524 if (fDstGpu->canCopySurface(dst, src, srcRect, dstPoint)) {
525 CopySurface* cs = this->recordCopySurface();
526 cs->fDst.reset(SkRef(dst));
527 cs->fSrc.reset(SkRef(src));
528 cs->fSrcRect = srcRect;
529 cs->fDstPoint = dstPoint;
530 return true;
531 } else {
532 return false;
533 }
534}
535
536bool GrInOrderDrawBuffer::onCanCopySurface(GrSurface* dst,
537 GrSurface* src,
538 const SkIRect& srcRect,
539 const SkIPoint& dstPoint) {
540 return fDstGpu->canCopySurface(dst, src, srcRect, dstPoint);
541}
542
bsalomon@google.comeb851172013-04-15 13:51:00 +0000543void GrInOrderDrawBuffer::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
544 fDstGpu->initCopySurfaceDstDesc(src, desc);
545}
546
bsalomon@google.com97805382012-03-13 14:32:07 +0000547void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace(
bsalomon@google.com97805382012-03-13 14:32:07 +0000548 int vertexCount,
549 int indexCount) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000550 // We use geometryHints() to know whether to flush the draw buffer. We
551 // can't flush if we are inside an unbalanced pushGeometrySource.
552 // Moreover, flushing blows away vertex and index data that was
553 // previously reserved. So if the vertex or index data is pulled from
554 // reserved space and won't be released by this request then we can't
555 // flush.
556 bool insideGeoPush = fGeoPoolStateStack.count() > 1;
bsalomon@google.com97805382012-03-13 14:32:07 +0000557
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000558 bool unreleasedVertexSpace =
559 !vertexCount &&
560 kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc;
bsalomon@google.com97805382012-03-13 14:32:07 +0000561
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000562 bool unreleasedIndexSpace =
563 !indexCount &&
564 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
bsalomon@google.com97805382012-03-13 14:32:07 +0000565
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000566 // we don't want to finalize any reserved geom on the target since
567 // we don't know that the client has finished writing to it.
568 bool targetHasReservedGeom = fDstGpu->hasReservedVerticesOrIndices();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000569
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000570 int vcount = vertexCount;
571 int icount = indexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000572
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000573 if (!insideGeoPush &&
574 !unreleasedVertexSpace &&
575 !unreleasedIndexSpace &&
576 !targetHasReservedGeom &&
577 this->geometryHints(&vcount, &icount)) {
bsalomon@google.com97805382012-03-13 14:32:07 +0000578
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000579 this->flush();
bsalomon@google.com97805382012-03-13 14:32:07 +0000580 }
581}
582
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000583bool GrInOrderDrawBuffer::geometryHints(int* vertexCount,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000584 int* indexCount) const {
585 // we will recommend a flush if the data could fit in a single
586 // preallocated buffer but none are left and it can't fit
587 // in the current buffer (which may not be prealloced).
reed@google.comac10a2d2010-12-22 21:39:39 +0000588 bool flush = false;
589 if (NULL != indexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000590 int32_t currIndices = fIndexPool.currentBufferIndices();
591 if (*indexCount > currIndices &&
592 (!fIndexPool.preallocatedBuffersRemaining() &&
593 *indexCount <= fIndexPool.preallocatedBufferIndices())) {
594
595 flush = true;
596 }
597 *indexCount = currIndices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000598 }
599 if (NULL != vertexCount) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000600 size_t vertexSize = this->getDrawState().getVertexSize();
jvanverth@google.coma6338982013-01-31 21:34:25 +0000601 int32_t currVertices = fVertexPool.currentBufferVertices(vertexSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000602 if (*vertexCount > currVertices &&
603 (!fVertexPool.preallocatedBuffersRemaining() &&
jvanverth@google.coma6338982013-01-31 21:34:25 +0000604 *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexSize))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000605
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000606 flush = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000607 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000608 *vertexCount = currVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000609 }
610 return flush;
611}
612
jvanverth@google.coma6338982013-01-31 21:34:25 +0000613bool GrInOrderDrawBuffer::onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000614 int vertexCount,
615 void** vertices) {
616 GeometryPoolState& poolState = fGeoPoolStateStack.back();
617 GrAssert(vertexCount > 0);
618 GrAssert(NULL != vertices);
619 GrAssert(0 == poolState.fUsedPoolVertexBytes);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000620
jvanverth@google.coma6338982013-01-31 21:34:25 +0000621 *vertices = fVertexPool.makeSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000622 vertexCount,
623 &poolState.fPoolVertexBuffer,
624 &poolState.fPoolStartVertex);
625 return NULL != *vertices;
626}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000627
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000628bool GrInOrderDrawBuffer::onReserveIndexSpace(int indexCount, void** indices) {
629 GeometryPoolState& poolState = fGeoPoolStateStack.back();
630 GrAssert(indexCount > 0);
631 GrAssert(NULL != indices);
632 GrAssert(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000633
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000634 *indices = fIndexPool.makeSpace(indexCount,
635 &poolState.fPoolIndexBuffer,
636 &poolState.fPoolStartIndex);
637 return NULL != *indices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000638}
639
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000640void GrInOrderDrawBuffer::releaseReservedVertexSpace() {
641 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000642 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000643
644 // If we get a release vertex space call then our current source should either be reserved
645 // or array (which we copied into reserved space).
646 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc ||
647 kArray_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000648
649 // When the caller reserved vertex buffer space we gave it back a pointer
650 // provided by the vertex buffer pool. At each draw we tracked the largest
651 // offset into the pool's pointer that was referenced. Now we return to the
652 // pool any portion at the tail of the allocation that no draw referenced.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000653 size_t reservedVertexBytes = geoSrc.fVertexSize * geoSrc.fVertexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000654 fVertexPool.putBack(reservedVertexBytes -
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000655 poolState.fUsedPoolVertexBytes);
656 poolState.fUsedPoolVertexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000657 poolState.fPoolVertexBuffer = NULL;
658 poolState.fPoolStartVertex = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000659}
660
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000661void GrInOrderDrawBuffer::releaseReservedIndexSpace() {
662 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000663 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000664
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000665 // If we get a release index space call then our current source should either be reserved
666 // or array (which we copied into reserved space).
667 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc ||
668 kArray_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000669
670 // Similar to releaseReservedVertexSpace we return any unused portion at
671 // the tail
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000672 size_t reservedIndexBytes = sizeof(uint16_t) * geoSrc.fIndexCount;
673 fIndexPool.putBack(reservedIndexBytes - poolState.fUsedPoolIndexBytes);
674 poolState.fUsedPoolIndexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000675 poolState.fPoolIndexBuffer = NULL;
676 poolState.fPoolStartIndex = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000677}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000678
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000679void GrInOrderDrawBuffer::onSetVertexSourceToArray(const void* vertexArray,
680 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000681
682 GeometryPoolState& poolState = fGeoPoolStateStack.back();
683 GrAssert(0 == poolState.fUsedPoolVertexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000684#if GR_DEBUG
685 bool success =
686#endif
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000687 fVertexPool.appendVertices(this->getVertexSize(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000688 vertexCount,
689 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000690 &poolState.fPoolVertexBuffer,
691 &poolState.fPoolStartVertex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000692 GR_DEBUGASSERT(success);
693}
694
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000695void GrInOrderDrawBuffer::onSetIndexSourceToArray(const void* indexArray,
696 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000697 GeometryPoolState& poolState = fGeoPoolStateStack.back();
698 GrAssert(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000699#if GR_DEBUG
700 bool success =
701#endif
702 fIndexPool.appendIndices(indexCount,
703 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000704 &poolState.fPoolIndexBuffer,
705 &poolState.fPoolStartIndex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000706 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000707}
708
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000709void GrInOrderDrawBuffer::releaseVertexArray() {
710 // When the client provides an array as the vertex source we handled it
711 // by copying their array into reserved space.
712 this->GrInOrderDrawBuffer::releaseReservedVertexSpace();
713}
714
715void GrInOrderDrawBuffer::releaseIndexArray() {
716 // When the client provides an array as the index source we handled it
717 // by copying their array into reserved space.
718 this->GrInOrderDrawBuffer::releaseReservedIndexSpace();
719}
720
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000721void GrInOrderDrawBuffer::geometrySourceWillPush() {
722 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
723 poolState.fUsedPoolVertexBytes = 0;
724 poolState.fUsedPoolIndexBytes = 0;
725#if GR_DEBUG
726 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
727 poolState.fPoolStartVertex = ~0;
728 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
729 poolState.fPoolStartIndex = ~0;
730#endif
731}
732
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000733void GrInOrderDrawBuffer::geometrySourceWillPop(
734 const GeometrySrcState& restoredState) {
735 GrAssert(fGeoPoolStateStack.count() > 1);
736 fGeoPoolStateStack.pop_back();
737 GeometryPoolState& poolState = fGeoPoolStateStack.back();
738 // we have to assume that any slack we had in our vertex/index data
739 // is now unreleasable because data may have been appended later in the
740 // pool.
741 if (kReserved_GeometrySrcType == restoredState.fVertexSrc ||
742 kArray_GeometrySrcType == restoredState.fVertexSrc) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000743 poolState.fUsedPoolVertexBytes = restoredState.fVertexSize * restoredState.fVertexCount;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000744 }
745 if (kReserved_GeometrySrcType == restoredState.fIndexSrc ||
746 kArray_GeometrySrcType == restoredState.fIndexSrc) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000747 poolState.fUsedPoolIndexBytes = sizeof(uint16_t) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000748 restoredState.fIndexCount;
749 }
750}
751
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000752bool GrInOrderDrawBuffer::needsNewState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000753 return fStates.empty() || !fStates.back().isEqual(this->getDrawState());
reed@google.comac10a2d2010-12-22 21:39:39 +0000754}
755
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000756bool GrInOrderDrawBuffer::needsNewClip() const {
bsalomon@google.com358e4272013-01-10 14:40:28 +0000757 GrAssert(fClips.count() == fClipOrigins.count());
758 if (this->getDrawState().isClipState()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000759 if (fClipSet &&
bsalomon@google.com358e4272013-01-10 14:40:28 +0000760 (fClips.empty() ||
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000761 fClips.back() != *this->getClip()->fClipStack ||
762 fClipOrigins.back() != this->getClip()->fOrigin)) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000763 return true;
764 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000765 }
766 return false;
767}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000768
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000769void GrInOrderDrawBuffer::recordClip() {
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000770 fClips.push_back() = *this->getClip()->fClipStack;
771 fClipOrigins.push_back() = this->getClip()->fOrigin;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000772 fClipSet = false;
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000773 fCmds.push_back(kSetClip_Cmd);
774}
775
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000776void GrInOrderDrawBuffer::recordState() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000777 fStates.push_back().saveFrom(this->getDrawState());
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000778 fCmds.push_back(kSetState_Cmd);
779}
780
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000781GrInOrderDrawBuffer::DrawRecord* GrInOrderDrawBuffer::recordDraw(const DrawInfo& info) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000782 fCmds.push_back(kDraw_Cmd);
783 return &fDraws.push_back(info);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000784}
785
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000786GrInOrderDrawBuffer::StencilPath* GrInOrderDrawBuffer::recordStencilPath() {
787 fCmds.push_back(kStencilPath_Cmd);
788 return &fStencilPaths.push_back();
789}
790
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000791GrInOrderDrawBuffer::Clear* GrInOrderDrawBuffer::recordClear() {
792 fCmds.push_back(kClear_Cmd);
793 return &fClears.push_back();
reed@google.comac10a2d2010-12-22 21:39:39 +0000794}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000795
bsalomon@google.com116ad842013-04-09 15:38:19 +0000796GrInOrderDrawBuffer::CopySurface* GrInOrderDrawBuffer::recordCopySurface() {
797 fCmds.push_back(kCopySurface_Cmd);
798 return &fCopySurfaces.push_back();
799}
800
801
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000802void GrInOrderDrawBuffer::clipWillBeSet(const GrClipData* newClipData) {
803 INHERITED::clipWillBeSet(newClipData);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000804 fClipSet = true;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000805 fClipProxyState = kUnknown_ClipProxyState;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000806}