blob: a0dce188a8578ca9fb70ce0f1528ff26877fba43 [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
robertphillips@google.com42903302013-04-20 12:26:07 +000076
77namespace {
78
79extern const GrVertexAttrib kRectPosColorUVAttribs[] = {
80 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
81 {kVec4ub_GrVertexAttribType, sizeof(GrPoint), kColor_GrVertexAttribBinding},
rmistry@google.comc9f3b382013-04-22 12:45:30 +000082 {kVec2f_GrVertexAttribType, sizeof(GrPoint)+sizeof(GrColor),
robertphillips@google.com42903302013-04-20 12:26:07 +000083 kLocalCoord_GrVertexAttribBinding},
84};
85
86extern const GrVertexAttrib kRectPosUVAttribs[] = {
87 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
88 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kLocalCoord_GrVertexAttribBinding},
89};
90
91static void set_vertex_attributes(GrDrawState* drawState,
92 bool hasColor, bool hasUVs,
93 int* colorOffset, int* localOffset) {
94 *colorOffset = -1;
95 *localOffset = -1;
96
97 // Using per-vertex colors allows batching across colors. (A lot of rects in a row differing
98 // only in color is a common occurrence in tables). However, having per-vertex colors disables
99 // blending optimizations because we don't know if the color will be solid or not. These
100 // optimizations help determine whether coverage and color can be blended correctly when
101 // dual-source blending isn't available. This comes into play when there is coverage. If colors
102 // were a stage it could take a hint that every vertex's color will be opaque.
103 if (hasColor && hasUVs) {
104 *colorOffset = sizeof(GrPoint);
105 *localOffset = sizeof(GrPoint) + sizeof(GrColor);
106 drawState->setVertexAttribs<kRectPosColorUVAttribs>(3);
107 } else if (hasColor) {
108 *colorOffset = sizeof(GrPoint);
109 drawState->setVertexAttribs<kRectPosColorUVAttribs>(2);
110 } else if (hasUVs) {
111 *localOffset = sizeof(GrPoint);
112 drawState->setVertexAttribs<kRectPosUVAttribs>(2);
113 } else {
114 drawState->setVertexAttribs<kRectPosUVAttribs>(1);
115 }
116}
117
118};
119
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000120void GrInOrderDrawBuffer::onDrawRect(const GrRect& rect,
121 const SkMatrix* matrix,
122 const GrRect* localRect,
123 const SkMatrix* localMatrix) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000124 GrDrawState::AutoColorRestore acr;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000125
126 GrDrawState* drawState = this->drawState();
127
128 GrColor color = drawState->getColor();
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000129
robertphillips@google.com42903302013-04-20 12:26:07 +0000130 int colorOffset, localOffset;
131 set_vertex_attributes(drawState,
132 this->caps()->dualSourceBlendingSupport() || drawState->hasSolidCoverage(),
133 NULL != localRect,
134 &colorOffset, &localOffset);
135 if (colorOffset >= 0) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000136 // We set the draw state's color to white here. This is done so that any batching performed
137 // in our subclass's onDraw() won't get a false from GrDrawState::op== due to a color
138 // mismatch. TODO: Once vertex layout is owned by GrDrawState it should skip comparing the
rmistry@google.comc9f3b382013-04-22 12:45:30 +0000139 // constant color in its op== when the kColor layout bit is set and then we can remove
robertphillips@google.com42903302013-04-20 12:26:07 +0000140 // this.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000141 acr.set(drawState, 0xFFFFFFFF);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000142 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000143
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000144 AutoReleaseGeometry geo(this, 4, 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000145 if (!geo.succeeded()) {
146 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com934c5702012-03-20 21:17:58 +0000147 return;
148 }
149
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000150 // Go to device coords to allow batching across matrix changes
151 SkMatrix combinedMatrix;
152 if (NULL != matrix) {
153 combinedMatrix = *matrix;
154 } else {
155 combinedMatrix.reset();
156 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000157 combinedMatrix.postConcat(drawState->getViewMatrix());
jvanverth@google.com39768252013-02-14 15:25:44 +0000158 // 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 +0000159 // modify that stage's matrix. Otherwise if the effect is generating its source rect from
160 // the vertex positions then we have to account for the view matrix change.
bsalomon@google.comc7818882013-03-20 19:19:53 +0000161 GrDrawState::AutoDeviceCoordDraw adcd(drawState);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000162 if (!adcd.succeeded()) {
163 return;
164 }
165
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000166 size_t vsize = drawState->getVertexSize();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000167
168 geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vsize);
169 combinedMatrix.mapPointsWithStride(geo.positions(), vsize, 4);
170
171 SkRect devBounds;
172 // since we already computed the dev verts, set the bounds hint. This will help us avoid
173 // unnecessary clipping in our onDraw().
174 get_vertex_bounds(geo.vertices(), vsize, 4, &devBounds);
175
bsalomon@google.comc7818882013-03-20 19:19:53 +0000176 if (localOffset >= 0) {
177 GrPoint* coords = GrTCast<GrPoint*>(GrTCast<intptr_t>(geo.vertices()) + localOffset);
178 coords->setRectFan(localRect->fLeft, localRect->fTop,
179 localRect->fRight, localRect->fBottom,
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000180 vsize);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000181 if (NULL != localMatrix) {
182 localMatrix->mapPointsWithStride(coords, vsize, 4);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000183 }
184 }
185
186 if (colorOffset >= 0) {
187 GrColor* vertColor = GrTCast<GrColor*>(GrTCast<intptr_t>(geo.vertices()) + colorOffset);
188 for (int i = 0; i < 4; ++i) {
189 *vertColor = color;
190 vertColor = (GrColor*) ((intptr_t) vertColor + vsize);
191 }
192 }
193
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000194 this->setIndexSourceToBuffer(this->getContext()->getQuadIndexBuffer());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000195 this->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, 4, 6, &devBounds);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000196
197 // to ensure that stashing the drawState ptr is valid
198 GrAssert(this->drawState() == drawState);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000199}
200
201bool GrInOrderDrawBuffer::quickInsideClip(const SkRect& devBounds) {
202 if (!this->getDrawState().isClipState()) {
203 return true;
204 }
205 if (kUnknown_ClipProxyState == fClipProxyState) {
206 SkIRect rect;
207 bool iior;
208 this->getClip()->getConservativeBounds(this->getDrawState().getRenderTarget(), &rect, &iior);
209 if (iior) {
210 // The clip is a rect. We will remember that in fProxyClip. It is common for an edge (or
211 // all edges) of the clip to be at the edge of the RT. However, we get that clipping for
212 // free via the viewport. We don't want to think that clipping must be enabled in this
213 // case. So we extend the clip outward from the edge to avoid these false negatives.
214 fClipProxyState = kValid_ClipProxyState;
215 fClipProxy = SkRect::MakeFromIRect(rect);
216
217 if (fClipProxy.fLeft <= 0) {
218 fClipProxy.fLeft = SK_ScalarMin;
219 }
220 if (fClipProxy.fTop <= 0) {
221 fClipProxy.fTop = SK_ScalarMin;
222 }
223 if (fClipProxy.fRight >= this->getDrawState().getRenderTarget()->width()) {
224 fClipProxy.fRight = SK_ScalarMax;
225 }
226 if (fClipProxy.fBottom >= this->getDrawState().getRenderTarget()->height()) {
227 fClipProxy.fBottom = SK_ScalarMax;
228 }
229 } else {
230 fClipProxyState = kInvalid_ClipProxyState;
231 }
232 }
233 if (kValid_ClipProxyState == fClipProxyState) {
234 return fClipProxy.contains(devBounds);
235 }
236 SkPoint originOffset = {SkIntToScalar(this->getClip()->fOrigin.fX),
237 SkIntToScalar(this->getClip()->fOrigin.fY)};
238 SkRect clipSpaceBounds = devBounds;
239 clipSpaceBounds.offset(originOffset);
240 return this->getClip()->fClipStack->quickContains(clipSpaceBounds);
241}
242
243int GrInOrderDrawBuffer::concatInstancedDraw(const DrawInfo& info) {
244 GrAssert(info.isInstanced());
245
bsalomon@google.com934c5702012-03-20 21:17:58 +0000246 const GeometrySrcState& geomSrc = this->getGeomSrc();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000247 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000248
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000249 // we only attempt to concat the case when reserved verts are used with a client-specified index
250 // buffer. To make this work with client-specified VBs we'd need to know if the VB was updated
251 // between draws.
252 if (kReserved_GeometrySrcType != geomSrc.fVertexSrc ||
253 kBuffer_GeometrySrcType != geomSrc.fIndexSrc) {
254 return 0;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000255 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000256 // Check if there is a draw info that is compatible that uses the same VB from the pool and
257 // the same IB
258 if (kDraw_Cmd != fCmds.back()) {
259 return 0;
260 }
261
262 DrawRecord* draw = &fDraws.back();
263 GeometryPoolState& poolState = fGeoPoolStateStack.back();
264 const GrVertexBuffer* vertexBuffer = poolState.fPoolVertexBuffer;
265
266 if (!draw->isInstanced() ||
267 draw->verticesPerInstance() != info.verticesPerInstance() ||
268 draw->indicesPerInstance() != info.indicesPerInstance() ||
269 draw->fVertexBuffer != vertexBuffer ||
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000270 draw->fIndexBuffer != geomSrc.fIndexBuffer) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000271 return 0;
272 }
273 // info does not yet account for the offset from the start of the pool's VB while the previous
274 // draw record does.
275 int adjustedStartVertex = poolState.fPoolStartVertex + info.startVertex();
276 if (draw->startVertex() + draw->vertexCount() != adjustedStartVertex) {
277 return 0;
278 }
279
280 GrAssert(poolState.fPoolStartVertex == draw->startVertex() + draw->vertexCount());
281
282 // how many instances can be concat'ed onto draw given the size of the index buffer
283 int instancesToConcat = this->indexCountInCurrentSource() / info.indicesPerInstance();
284 instancesToConcat -= draw->instanceCount();
285 instancesToConcat = GrMin(instancesToConcat, info.instanceCount());
286
287 // update the amount of reserved vertex data actually referenced in draws
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000288 size_t vertexBytes = instancesToConcat * info.verticesPerInstance() *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000289 drawState.getVertexSize();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000290 poolState.fUsedPoolVertexBytes = GrMax(poolState.fUsedPoolVertexBytes, vertexBytes);
291
292 draw->adjustInstanceCount(instancesToConcat);
293 return instancesToConcat;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000294}
295
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000296class AutoClipReenable {
297public:
298 AutoClipReenable() : fDrawState(NULL) {}
299 ~AutoClipReenable() {
300 if (NULL != fDrawState) {
301 fDrawState->enableState(GrDrawState::kClip_StateBit);
302 }
303 }
304 void set(GrDrawState* drawState) {
305 if (drawState->isClipState()) {
306 fDrawState = drawState;
307 drawState->disableState(GrDrawState::kClip_StateBit);
308 }
309 }
310private:
311 GrDrawState* fDrawState;
312};
313
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000314void GrInOrderDrawBuffer::onDraw(const DrawInfo& info) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000315
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000316 GeometryPoolState& poolState = fGeoPoolStateStack.back();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000317 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000318 AutoClipReenable acr;
319
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000320 if (drawState.isClipState() &&
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000321 NULL != info.getDevBounds() &&
322 this->quickInsideClip(*info.getDevBounds())) {
323 acr.set(this->drawState());
324 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000325
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000326 if (this->needsNewClip()) {
327 this->recordClip();
328 }
329 if (this->needsNewState()) {
330 this->recordState();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000331 }
332
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000333 DrawRecord* draw;
334 if (info.isInstanced()) {
335 int instancesConcated = this->concatInstancedDraw(info);
336 if (info.instanceCount() > instancesConcated) {
337 draw = this->recordDraw(info);
338 draw->adjustInstanceCount(-instancesConcated);
339 } else {
340 return;
341 }
342 } else {
343 draw = this->recordDraw(info);
344 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000345
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000346 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000347 case kBuffer_GeometrySrcType:
348 draw->fVertexBuffer = this->getGeomSrc().fVertexBuffer;
349 break;
350 case kReserved_GeometrySrcType: // fallthrough
351 case kArray_GeometrySrcType: {
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000352 size_t vertexBytes = (info.vertexCount() + info.startVertex()) *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000353 drawState.getVertexSize();
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000354 poolState.fUsedPoolVertexBytes = GrMax(poolState.fUsedPoolVertexBytes, vertexBytes);
355 draw->fVertexBuffer = poolState.fPoolVertexBuffer;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000356 draw->adjustStartVertex(poolState.fPoolStartVertex);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000357 break;
358 }
359 default:
360 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000361 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000362 draw->fVertexBuffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000363
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000364 if (info.isIndexed()) {
365 switch (this->getGeomSrc().fIndexSrc) {
366 case kBuffer_GeometrySrcType:
367 draw->fIndexBuffer = this->getGeomSrc().fIndexBuffer;
368 break;
369 case kReserved_GeometrySrcType: // fallthrough
370 case kArray_GeometrySrcType: {
371 size_t indexBytes = (info.indexCount() + info.startIndex()) * sizeof(uint16_t);
372 poolState.fUsedPoolIndexBytes = GrMax(poolState.fUsedPoolIndexBytes, indexBytes);
373 draw->fIndexBuffer = poolState.fPoolIndexBuffer;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000374 draw->adjustStartIndex(poolState.fPoolStartIndex);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000375 break;
376 }
377 default:
378 GrCrash("unknown geom src type");
379 }
380 draw->fIndexBuffer->ref();
381 } else {
382 draw->fIndexBuffer = NULL;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000383 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000384}
385
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000386GrInOrderDrawBuffer::StencilPath::StencilPath() : fStroke(SkStrokeRec::kFill_InitStyle) {}
387
388void GrInOrderDrawBuffer::onStencilPath(const GrPath* path, const SkStrokeRec& stroke,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000389 SkPath::FillType fill) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000390 if (this->needsNewClip()) {
391 this->recordClip();
392 }
393 // Only compare the subset of GrDrawState relevant to path stenciling?
394 if (this->needsNewState()) {
395 this->recordState();
396 }
397 StencilPath* sp = this->recordStencilPath();
398 sp->fPath.reset(path);
399 path->ref();
400 sp->fFill = fill;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000401 sp->fStroke = stroke;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000402}
403
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000404void GrInOrderDrawBuffer::clear(const GrIRect* rect, GrColor color, GrRenderTarget* renderTarget) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000405 GrIRect r;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000406 if (NULL == renderTarget) {
407 renderTarget = this->drawState()->getRenderTarget();
408 GrAssert(NULL != renderTarget);
409 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000410 if (NULL == rect) {
411 // We could do something smart and remove previous draws and clears to
412 // the current render target. If we get that smart we have to make sure
413 // those draws aren't read before this clear (render-to-texture).
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000414 r.setLTRB(0, 0, renderTarget->width(), renderTarget->height());
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000415 rect = &r;
416 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000417 Clear* clr = this->recordClear();
418 clr->fColor = color;
419 clr->fRect = *rect;
420 clr->fRenderTarget = renderTarget;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000421 renderTarget->ref();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000422}
423
reed@google.comac10a2d2010-12-22 21:39:39 +0000424void GrInOrderDrawBuffer::reset() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000425 GrAssert(1 == fGeoPoolStateStack.count());
426 this->resetVertexSource();
427 this->resetIndexSource();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000428 int numDraws = fDraws.count();
429 for (int d = 0; d < numDraws; ++d) {
430 // we always have a VB, but not always an IB
431 GrAssert(NULL != fDraws[d].fVertexBuffer);
432 fDraws[d].fVertexBuffer->unref();
433 GrSafeUnref(fDraws[d].fIndexBuffer);
434 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000435 fCmds.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000436 fDraws.reset();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000437 fStencilPaths.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000438 fStates.reset();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000439 fClears.reset();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000440 fVertexPool.reset();
441 fIndexPool.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000442 fClips.reset();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000443 fClipOrigins.reset();
bsalomon@google.com116ad842013-04-09 15:38:19 +0000444 fCopySurfaces.reset();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000445 fClipSet = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000446}
447
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000448bool GrInOrderDrawBuffer::flush() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000449 GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fVertexSrc);
450 GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000451
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000452 int numCmds = fCmds.count();
bsalomon@google.com358e4272013-01-10 14:40:28 +0000453 if (0 == numCmds) {
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000454 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000455 }
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000456 GrAssert(!fFlushing);
457
458 GrAutoTRestore<bool> flushRestore(&fFlushing);
459 fFlushing = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000460
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000461 fVertexPool.unlock();
462 fIndexPool.unlock();
reed@google.comac10a2d2010-12-22 21:39:39 +0000463
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000464 GrDrawTarget::AutoClipRestore acr(fDstGpu);
465 AutoGeometryAndStatePush agasp(fDstGpu, kPreserve_ASRInit);
bsalomon@google.comca432082013-01-23 19:53:46 +0000466
467 GrDrawState playbackState;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000468 GrDrawState* prevDrawState = fDstGpu->drawState();
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000469 prevDrawState->ref();
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000470 fDstGpu->setDrawState(&playbackState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000471
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000472 GrClipData clipData;
473
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000474 int currState = 0;
475 int currClip = 0;
476 int currClear = 0;
477 int currDraw = 0;
478 int currStencilPath = 0;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000479 int currCopySurface = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000480
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000481 for (int c = 0; c < numCmds; ++c) {
482 switch (fCmds[c]) {
483 case kDraw_Cmd: {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000484 const DrawRecord& draw = fDraws[currDraw];
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000485 fDstGpu->setVertexSourceToBuffer(draw.fVertexBuffer);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000486 if (draw.isIndexed()) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000487 fDstGpu->setIndexSourceToBuffer(draw.fIndexBuffer);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000488 }
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000489 fDstGpu->executeDraw(draw);
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000490
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000491 ++currDraw;
492 break;
493 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000494 case kStencilPath_Cmd: {
495 const StencilPath& sp = fStencilPaths[currStencilPath];
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000496 fDstGpu->stencilPath(sp.fPath.get(), sp.fStroke, sp.fFill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000497 ++currStencilPath;
498 break;
499 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000500 case kSetState_Cmd:
bsalomon@google.comca432082013-01-23 19:53:46 +0000501 fStates[currState].restoreTo(&playbackState);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000502 ++currState;
503 break;
504 case kSetClip_Cmd:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000505 clipData.fClipStack = &fClips[currClip];
506 clipData.fOrigin = fClipOrigins[currClip];
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000507 fDstGpu->setClip(&clipData);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000508 ++currClip;
509 break;
510 case kClear_Cmd:
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000511 fDstGpu->clear(&fClears[currClear].fRect,
512 fClears[currClear].fColor,
513 fClears[currClear].fRenderTarget);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000514 ++currClear;
515 break;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000516 case kCopySurface_Cmd:
517 fDstGpu->copySurface(fCopySurfaces[currCopySurface].fDst.get(),
518 fCopySurfaces[currCopySurface].fSrc.get(),
519 fCopySurfaces[currCopySurface].fSrcRect,
520 fCopySurfaces[currCopySurface].fDstPoint);
521 ++currCopySurface;
522 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000523 }
524 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000525 // we should have consumed all the states, clips, etc.
526 GrAssert(fStates.count() == currState);
527 GrAssert(fClips.count() == currClip);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000528 GrAssert(fClipOrigins.count() == currClip);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000529 GrAssert(fClears.count() == currClear);
530 GrAssert(fDraws.count() == currDraw);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000531 GrAssert(fCopySurfaces.count() == currCopySurface);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000532
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000533 fDstGpu->setDrawState(prevDrawState);
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000534 prevDrawState->unref();
bsalomon@google.com55e4a202013-01-11 13:54:21 +0000535 this->reset();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000536 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000537}
538
bsalomon@google.com116ad842013-04-09 15:38:19 +0000539bool GrInOrderDrawBuffer::onCopySurface(GrSurface* dst,
540 GrSurface* src,
541 const SkIRect& srcRect,
542 const SkIPoint& dstPoint) {
543 if (fDstGpu->canCopySurface(dst, src, srcRect, dstPoint)) {
544 CopySurface* cs = this->recordCopySurface();
545 cs->fDst.reset(SkRef(dst));
546 cs->fSrc.reset(SkRef(src));
547 cs->fSrcRect = srcRect;
548 cs->fDstPoint = dstPoint;
549 return true;
550 } else {
551 return false;
552 }
553}
554
555bool GrInOrderDrawBuffer::onCanCopySurface(GrSurface* dst,
556 GrSurface* src,
557 const SkIRect& srcRect,
558 const SkIPoint& dstPoint) {
559 return fDstGpu->canCopySurface(dst, src, srcRect, dstPoint);
560}
561
bsalomon@google.comeb851172013-04-15 13:51:00 +0000562void GrInOrderDrawBuffer::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
563 fDstGpu->initCopySurfaceDstDesc(src, desc);
564}
565
bsalomon@google.com97805382012-03-13 14:32:07 +0000566void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace(
bsalomon@google.com97805382012-03-13 14:32:07 +0000567 int vertexCount,
568 int indexCount) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000569 // We use geometryHints() to know whether to flush the draw buffer. We
570 // can't flush if we are inside an unbalanced pushGeometrySource.
571 // Moreover, flushing blows away vertex and index data that was
572 // previously reserved. So if the vertex or index data is pulled from
573 // reserved space and won't be released by this request then we can't
574 // flush.
575 bool insideGeoPush = fGeoPoolStateStack.count() > 1;
bsalomon@google.com97805382012-03-13 14:32:07 +0000576
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000577 bool unreleasedVertexSpace =
578 !vertexCount &&
579 kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc;
bsalomon@google.com97805382012-03-13 14:32:07 +0000580
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000581 bool unreleasedIndexSpace =
582 !indexCount &&
583 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
bsalomon@google.com97805382012-03-13 14:32:07 +0000584
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000585 // we don't want to finalize any reserved geom on the target since
586 // we don't know that the client has finished writing to it.
587 bool targetHasReservedGeom = fDstGpu->hasReservedVerticesOrIndices();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000588
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000589 int vcount = vertexCount;
590 int icount = indexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000591
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000592 if (!insideGeoPush &&
593 !unreleasedVertexSpace &&
594 !unreleasedIndexSpace &&
595 !targetHasReservedGeom &&
596 this->geometryHints(&vcount, &icount)) {
bsalomon@google.com97805382012-03-13 14:32:07 +0000597
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000598 this->flush();
bsalomon@google.com97805382012-03-13 14:32:07 +0000599 }
600}
601
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000602bool GrInOrderDrawBuffer::geometryHints(int* vertexCount,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000603 int* indexCount) const {
604 // we will recommend a flush if the data could fit in a single
605 // preallocated buffer but none are left and it can't fit
606 // in the current buffer (which may not be prealloced).
reed@google.comac10a2d2010-12-22 21:39:39 +0000607 bool flush = false;
608 if (NULL != indexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000609 int32_t currIndices = fIndexPool.currentBufferIndices();
610 if (*indexCount > currIndices &&
611 (!fIndexPool.preallocatedBuffersRemaining() &&
612 *indexCount <= fIndexPool.preallocatedBufferIndices())) {
613
614 flush = true;
615 }
616 *indexCount = currIndices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000617 }
618 if (NULL != vertexCount) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000619 size_t vertexSize = this->getDrawState().getVertexSize();
jvanverth@google.coma6338982013-01-31 21:34:25 +0000620 int32_t currVertices = fVertexPool.currentBufferVertices(vertexSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000621 if (*vertexCount > currVertices &&
622 (!fVertexPool.preallocatedBuffersRemaining() &&
jvanverth@google.coma6338982013-01-31 21:34:25 +0000623 *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexSize))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000624
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000625 flush = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000626 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000627 *vertexCount = currVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000628 }
629 return flush;
630}
631
jvanverth@google.coma6338982013-01-31 21:34:25 +0000632bool GrInOrderDrawBuffer::onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000633 int vertexCount,
634 void** vertices) {
635 GeometryPoolState& poolState = fGeoPoolStateStack.back();
636 GrAssert(vertexCount > 0);
637 GrAssert(NULL != vertices);
638 GrAssert(0 == poolState.fUsedPoolVertexBytes);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000639
jvanverth@google.coma6338982013-01-31 21:34:25 +0000640 *vertices = fVertexPool.makeSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000641 vertexCount,
642 &poolState.fPoolVertexBuffer,
643 &poolState.fPoolStartVertex);
644 return NULL != *vertices;
645}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000646
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000647bool GrInOrderDrawBuffer::onReserveIndexSpace(int indexCount, void** indices) {
648 GeometryPoolState& poolState = fGeoPoolStateStack.back();
649 GrAssert(indexCount > 0);
650 GrAssert(NULL != indices);
651 GrAssert(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000652
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000653 *indices = fIndexPool.makeSpace(indexCount,
654 &poolState.fPoolIndexBuffer,
655 &poolState.fPoolStartIndex);
656 return NULL != *indices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000657}
658
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000659void GrInOrderDrawBuffer::releaseReservedVertexSpace() {
660 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000661 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000662
663 // If we get a release vertex space call then our current source should either be reserved
664 // or array (which we copied into reserved space).
665 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc ||
666 kArray_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000667
668 // When the caller reserved vertex buffer space we gave it back a pointer
669 // provided by the vertex buffer pool. At each draw we tracked the largest
670 // offset into the pool's pointer that was referenced. Now we return to the
671 // pool any portion at the tail of the allocation that no draw referenced.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000672 size_t reservedVertexBytes = geoSrc.fVertexSize * geoSrc.fVertexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000673 fVertexPool.putBack(reservedVertexBytes -
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000674 poolState.fUsedPoolVertexBytes);
675 poolState.fUsedPoolVertexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000676 poolState.fPoolVertexBuffer = NULL;
677 poolState.fPoolStartVertex = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000678}
679
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000680void GrInOrderDrawBuffer::releaseReservedIndexSpace() {
681 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000682 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000683
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000684 // If we get a release index space call then our current source should either be reserved
685 // or array (which we copied into reserved space).
686 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc ||
687 kArray_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000688
689 // Similar to releaseReservedVertexSpace we return any unused portion at
690 // the tail
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000691 size_t reservedIndexBytes = sizeof(uint16_t) * geoSrc.fIndexCount;
692 fIndexPool.putBack(reservedIndexBytes - poolState.fUsedPoolIndexBytes);
693 poolState.fUsedPoolIndexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000694 poolState.fPoolIndexBuffer = NULL;
695 poolState.fPoolStartIndex = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000696}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000697
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000698void GrInOrderDrawBuffer::onSetVertexSourceToArray(const void* vertexArray,
699 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000700
701 GeometryPoolState& poolState = fGeoPoolStateStack.back();
702 GrAssert(0 == poolState.fUsedPoolVertexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000703#if GR_DEBUG
704 bool success =
705#endif
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000706 fVertexPool.appendVertices(this->getVertexSize(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000707 vertexCount,
708 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000709 &poolState.fPoolVertexBuffer,
710 &poolState.fPoolStartVertex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000711 GR_DEBUGASSERT(success);
712}
713
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000714void GrInOrderDrawBuffer::onSetIndexSourceToArray(const void* indexArray,
715 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000716 GeometryPoolState& poolState = fGeoPoolStateStack.back();
717 GrAssert(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000718#if GR_DEBUG
719 bool success =
720#endif
721 fIndexPool.appendIndices(indexCount,
722 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000723 &poolState.fPoolIndexBuffer,
724 &poolState.fPoolStartIndex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000725 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000726}
727
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000728void GrInOrderDrawBuffer::releaseVertexArray() {
729 // When the client provides an array as the vertex source we handled it
730 // by copying their array into reserved space.
731 this->GrInOrderDrawBuffer::releaseReservedVertexSpace();
732}
733
734void GrInOrderDrawBuffer::releaseIndexArray() {
735 // When the client provides an array as the index source we handled it
736 // by copying their array into reserved space.
737 this->GrInOrderDrawBuffer::releaseReservedIndexSpace();
738}
739
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000740void GrInOrderDrawBuffer::geometrySourceWillPush() {
741 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
742 poolState.fUsedPoolVertexBytes = 0;
743 poolState.fUsedPoolIndexBytes = 0;
744#if GR_DEBUG
745 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
746 poolState.fPoolStartVertex = ~0;
747 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
748 poolState.fPoolStartIndex = ~0;
749#endif
750}
751
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000752void GrInOrderDrawBuffer::geometrySourceWillPop(
753 const GeometrySrcState& restoredState) {
754 GrAssert(fGeoPoolStateStack.count() > 1);
755 fGeoPoolStateStack.pop_back();
756 GeometryPoolState& poolState = fGeoPoolStateStack.back();
757 // we have to assume that any slack we had in our vertex/index data
758 // is now unreleasable because data may have been appended later in the
759 // pool.
760 if (kReserved_GeometrySrcType == restoredState.fVertexSrc ||
761 kArray_GeometrySrcType == restoredState.fVertexSrc) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000762 poolState.fUsedPoolVertexBytes = restoredState.fVertexSize * restoredState.fVertexCount;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000763 }
764 if (kReserved_GeometrySrcType == restoredState.fIndexSrc ||
765 kArray_GeometrySrcType == restoredState.fIndexSrc) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000766 poolState.fUsedPoolIndexBytes = sizeof(uint16_t) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000767 restoredState.fIndexCount;
768 }
769}
770
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000771bool GrInOrderDrawBuffer::needsNewState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000772 return fStates.empty() || !fStates.back().isEqual(this->getDrawState());
reed@google.comac10a2d2010-12-22 21:39:39 +0000773}
774
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000775bool GrInOrderDrawBuffer::needsNewClip() const {
bsalomon@google.com358e4272013-01-10 14:40:28 +0000776 GrAssert(fClips.count() == fClipOrigins.count());
777 if (this->getDrawState().isClipState()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000778 if (fClipSet &&
bsalomon@google.com358e4272013-01-10 14:40:28 +0000779 (fClips.empty() ||
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000780 fClips.back() != *this->getClip()->fClipStack ||
781 fClipOrigins.back() != this->getClip()->fOrigin)) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000782 return true;
783 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000784 }
785 return false;
786}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000787
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000788void GrInOrderDrawBuffer::recordClip() {
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000789 fClips.push_back() = *this->getClip()->fClipStack;
790 fClipOrigins.push_back() = this->getClip()->fOrigin;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000791 fClipSet = false;
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000792 fCmds.push_back(kSetClip_Cmd);
793}
794
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000795void GrInOrderDrawBuffer::recordState() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000796 fStates.push_back().saveFrom(this->getDrawState());
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000797 fCmds.push_back(kSetState_Cmd);
798}
799
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000800GrInOrderDrawBuffer::DrawRecord* GrInOrderDrawBuffer::recordDraw(const DrawInfo& info) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000801 fCmds.push_back(kDraw_Cmd);
802 return &fDraws.push_back(info);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000803}
804
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000805GrInOrderDrawBuffer::StencilPath* GrInOrderDrawBuffer::recordStencilPath() {
806 fCmds.push_back(kStencilPath_Cmd);
807 return &fStencilPaths.push_back();
808}
809
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000810GrInOrderDrawBuffer::Clear* GrInOrderDrawBuffer::recordClear() {
811 fCmds.push_back(kClear_Cmd);
812 return &fClears.push_back();
reed@google.comac10a2d2010-12-22 21:39:39 +0000813}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000814
bsalomon@google.com116ad842013-04-09 15:38:19 +0000815GrInOrderDrawBuffer::CopySurface* GrInOrderDrawBuffer::recordCopySurface() {
816 fCmds.push_back(kCopySurface_Cmd);
817 return &fCopySurfaces.push_back();
818}
819
820
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000821void GrInOrderDrawBuffer::clipWillBeSet(const GrClipData* newClipData) {
822 INHERITED::clipWillBeSet(newClipData);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000823 fClipSet = true;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000824 fClipProxyState = kUnknown_ClipProxyState;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000825}