blob: 3ec95966d8aa6c454a826c7936e7cf126ffb73e4 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#include "GrInOrderDrawBuffer.h"
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00009
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000010#include "GrBufferAllocPool.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000011#include "GrDrawTargetCaps.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000012#include "GrGpu.h"
13#include "GrIndexBuffer.h"
14#include "GrPath.h"
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000015#include "GrPoint.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
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000120void GrInOrderDrawBuffer::onDrawRect(const SkRect& rect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000121 const SkMatrix* matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000122 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000123 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.com137f1342013-05-29 21:27:53 +0000161 GrDrawState::AutoViewMatrixRestore avmr;
162 if (!avmr.setIdentity(drawState)) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000163 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
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000404void GrInOrderDrawBuffer::clear(const SkIRect* rect, GrColor color, GrRenderTarget* renderTarget) {
405 SkIRect 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
robertphillips@google.com1267fbd2013-07-03 18:37:27 +0000448void GrInOrderDrawBuffer::flush() {
449 if (fFlushing) {
450 return;
451 }
452
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000453 GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fVertexSrc);
454 GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000455
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000456 int numCmds = fCmds.count();
bsalomon@google.com358e4272013-01-10 14:40:28 +0000457 if (0 == numCmds) {
robertphillips@google.com1267fbd2013-07-03 18:37:27 +0000458 return;
reed@google.comac10a2d2010-12-22 21:39:39 +0000459 }
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000460
461 GrAutoTRestore<bool> flushRestore(&fFlushing);
462 fFlushing = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000463
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000464 fVertexPool.unlock();
465 fIndexPool.unlock();
reed@google.comac10a2d2010-12-22 21:39:39 +0000466
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000467 GrDrawTarget::AutoClipRestore acr(fDstGpu);
468 AutoGeometryAndStatePush agasp(fDstGpu, kPreserve_ASRInit);
bsalomon@google.comca432082013-01-23 19:53:46 +0000469
470 GrDrawState playbackState;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000471 GrDrawState* prevDrawState = fDstGpu->drawState();
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000472 prevDrawState->ref();
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000473 fDstGpu->setDrawState(&playbackState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000474
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000475 GrClipData clipData;
476
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000477 int currState = 0;
478 int currClip = 0;
479 int currClear = 0;
480 int currDraw = 0;
481 int currStencilPath = 0;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000482 int currCopySurface = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000483
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000484 for (int c = 0; c < numCmds; ++c) {
485 switch (fCmds[c]) {
486 case kDraw_Cmd: {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000487 const DrawRecord& draw = fDraws[currDraw];
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000488 fDstGpu->setVertexSourceToBuffer(draw.fVertexBuffer);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000489 if (draw.isIndexed()) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000490 fDstGpu->setIndexSourceToBuffer(draw.fIndexBuffer);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000491 }
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000492 fDstGpu->executeDraw(draw);
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000493
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000494 ++currDraw;
495 break;
496 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000497 case kStencilPath_Cmd: {
498 const StencilPath& sp = fStencilPaths[currStencilPath];
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000499 fDstGpu->stencilPath(sp.fPath.get(), sp.fStroke, sp.fFill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000500 ++currStencilPath;
501 break;
502 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000503 case kSetState_Cmd:
bsalomon@google.comca432082013-01-23 19:53:46 +0000504 fStates[currState].restoreTo(&playbackState);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000505 ++currState;
506 break;
507 case kSetClip_Cmd:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000508 clipData.fClipStack = &fClips[currClip];
509 clipData.fOrigin = fClipOrigins[currClip];
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000510 fDstGpu->setClip(&clipData);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000511 ++currClip;
512 break;
513 case kClear_Cmd:
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000514 fDstGpu->clear(&fClears[currClear].fRect,
515 fClears[currClear].fColor,
516 fClears[currClear].fRenderTarget);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000517 ++currClear;
518 break;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000519 case kCopySurface_Cmd:
520 fDstGpu->copySurface(fCopySurfaces[currCopySurface].fDst.get(),
521 fCopySurfaces[currCopySurface].fSrc.get(),
522 fCopySurfaces[currCopySurface].fSrcRect,
523 fCopySurfaces[currCopySurface].fDstPoint);
524 ++currCopySurface;
525 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000526 }
527 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000528 // we should have consumed all the states, clips, etc.
529 GrAssert(fStates.count() == currState);
530 GrAssert(fClips.count() == currClip);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000531 GrAssert(fClipOrigins.count() == currClip);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000532 GrAssert(fClears.count() == currClear);
533 GrAssert(fDraws.count() == currDraw);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000534 GrAssert(fCopySurfaces.count() == currCopySurface);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000535
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000536 fDstGpu->setDrawState(prevDrawState);
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000537 prevDrawState->unref();
bsalomon@google.com55e4a202013-01-11 13:54:21 +0000538 this->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000539}
540
bsalomon@google.com116ad842013-04-09 15:38:19 +0000541bool GrInOrderDrawBuffer::onCopySurface(GrSurface* dst,
542 GrSurface* src,
543 const SkIRect& srcRect,
544 const SkIPoint& dstPoint) {
545 if (fDstGpu->canCopySurface(dst, src, srcRect, dstPoint)) {
546 CopySurface* cs = this->recordCopySurface();
547 cs->fDst.reset(SkRef(dst));
548 cs->fSrc.reset(SkRef(src));
549 cs->fSrcRect = srcRect;
550 cs->fDstPoint = dstPoint;
551 return true;
552 } else {
553 return false;
554 }
555}
556
557bool GrInOrderDrawBuffer::onCanCopySurface(GrSurface* dst,
558 GrSurface* src,
559 const SkIRect& srcRect,
560 const SkIPoint& dstPoint) {
561 return fDstGpu->canCopySurface(dst, src, srcRect, dstPoint);
562}
563
bsalomon@google.comeb851172013-04-15 13:51:00 +0000564void GrInOrderDrawBuffer::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
565 fDstGpu->initCopySurfaceDstDesc(src, desc);
566}
567
bsalomon@google.com97805382012-03-13 14:32:07 +0000568void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace(
bsalomon@google.com97805382012-03-13 14:32:07 +0000569 int vertexCount,
570 int indexCount) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000571 // We use geometryHints() to know whether to flush the draw buffer. We
572 // can't flush if we are inside an unbalanced pushGeometrySource.
573 // Moreover, flushing blows away vertex and index data that was
574 // previously reserved. So if the vertex or index data is pulled from
575 // reserved space and won't be released by this request then we can't
576 // flush.
577 bool insideGeoPush = fGeoPoolStateStack.count() > 1;
bsalomon@google.com97805382012-03-13 14:32:07 +0000578
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000579 bool unreleasedVertexSpace =
580 !vertexCount &&
581 kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc;
bsalomon@google.com97805382012-03-13 14:32:07 +0000582
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000583 bool unreleasedIndexSpace =
584 !indexCount &&
585 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
bsalomon@google.com97805382012-03-13 14:32:07 +0000586
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000587 // we don't want to finalize any reserved geom on the target since
588 // we don't know that the client has finished writing to it.
589 bool targetHasReservedGeom = fDstGpu->hasReservedVerticesOrIndices();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000590
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000591 int vcount = vertexCount;
592 int icount = indexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000593
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000594 if (!insideGeoPush &&
595 !unreleasedVertexSpace &&
596 !unreleasedIndexSpace &&
597 !targetHasReservedGeom &&
598 this->geometryHints(&vcount, &icount)) {
bsalomon@google.com97805382012-03-13 14:32:07 +0000599
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000600 this->flush();
bsalomon@google.com97805382012-03-13 14:32:07 +0000601 }
602}
603
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000604bool GrInOrderDrawBuffer::geometryHints(int* vertexCount,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000605 int* indexCount) const {
606 // we will recommend a flush if the data could fit in a single
607 // preallocated buffer but none are left and it can't fit
608 // in the current buffer (which may not be prealloced).
reed@google.comac10a2d2010-12-22 21:39:39 +0000609 bool flush = false;
610 if (NULL != indexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000611 int32_t currIndices = fIndexPool.currentBufferIndices();
612 if (*indexCount > currIndices &&
613 (!fIndexPool.preallocatedBuffersRemaining() &&
614 *indexCount <= fIndexPool.preallocatedBufferIndices())) {
615
616 flush = true;
617 }
618 *indexCount = currIndices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000619 }
620 if (NULL != vertexCount) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000621 size_t vertexSize = this->getDrawState().getVertexSize();
jvanverth@google.coma6338982013-01-31 21:34:25 +0000622 int32_t currVertices = fVertexPool.currentBufferVertices(vertexSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000623 if (*vertexCount > currVertices &&
624 (!fVertexPool.preallocatedBuffersRemaining() &&
jvanverth@google.coma6338982013-01-31 21:34:25 +0000625 *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexSize))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000626
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000627 flush = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000628 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000629 *vertexCount = currVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000630 }
631 return flush;
632}
633
jvanverth@google.coma6338982013-01-31 21:34:25 +0000634bool GrInOrderDrawBuffer::onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000635 int vertexCount,
636 void** vertices) {
637 GeometryPoolState& poolState = fGeoPoolStateStack.back();
638 GrAssert(vertexCount > 0);
639 GrAssert(NULL != vertices);
640 GrAssert(0 == poolState.fUsedPoolVertexBytes);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000641
jvanverth@google.coma6338982013-01-31 21:34:25 +0000642 *vertices = fVertexPool.makeSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000643 vertexCount,
644 &poolState.fPoolVertexBuffer,
645 &poolState.fPoolStartVertex);
646 return NULL != *vertices;
647}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000648
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000649bool GrInOrderDrawBuffer::onReserveIndexSpace(int indexCount, void** indices) {
650 GeometryPoolState& poolState = fGeoPoolStateStack.back();
651 GrAssert(indexCount > 0);
652 GrAssert(NULL != indices);
653 GrAssert(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000654
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000655 *indices = fIndexPool.makeSpace(indexCount,
656 &poolState.fPoolIndexBuffer,
657 &poolState.fPoolStartIndex);
658 return NULL != *indices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000659}
660
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000661void GrInOrderDrawBuffer::releaseReservedVertexSpace() {
662 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000663 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000664
665 // If we get a release vertex space call then our current source should either be reserved
666 // or array (which we copied into reserved space).
667 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc ||
668 kArray_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000669
670 // When the caller reserved vertex buffer space we gave it back a pointer
671 // provided by the vertex buffer pool. At each draw we tracked the largest
672 // offset into the pool's pointer that was referenced. Now we return to the
673 // pool any portion at the tail of the allocation that no draw referenced.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000674 size_t reservedVertexBytes = geoSrc.fVertexSize * geoSrc.fVertexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000675 fVertexPool.putBack(reservedVertexBytes -
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000676 poolState.fUsedPoolVertexBytes);
677 poolState.fUsedPoolVertexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000678 poolState.fPoolVertexBuffer = NULL;
679 poolState.fPoolStartVertex = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000680}
681
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000682void GrInOrderDrawBuffer::releaseReservedIndexSpace() {
683 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000684 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000685
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000686 // If we get a release index space call then our current source should either be reserved
687 // or array (which we copied into reserved space).
688 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc ||
689 kArray_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000690
691 // Similar to releaseReservedVertexSpace we return any unused portion at
692 // the tail
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000693 size_t reservedIndexBytes = sizeof(uint16_t) * geoSrc.fIndexCount;
694 fIndexPool.putBack(reservedIndexBytes - poolState.fUsedPoolIndexBytes);
695 poolState.fUsedPoolIndexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000696 poolState.fPoolIndexBuffer = NULL;
697 poolState.fPoolStartIndex = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000698}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000699
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000700void GrInOrderDrawBuffer::onSetVertexSourceToArray(const void* vertexArray,
701 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000702
703 GeometryPoolState& poolState = fGeoPoolStateStack.back();
704 GrAssert(0 == poolState.fUsedPoolVertexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000705#if GR_DEBUG
706 bool success =
707#endif
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000708 fVertexPool.appendVertices(this->getVertexSize(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000709 vertexCount,
710 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000711 &poolState.fPoolVertexBuffer,
712 &poolState.fPoolStartVertex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000713 GR_DEBUGASSERT(success);
714}
715
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000716void GrInOrderDrawBuffer::onSetIndexSourceToArray(const void* indexArray,
717 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000718 GeometryPoolState& poolState = fGeoPoolStateStack.back();
719 GrAssert(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000720#if GR_DEBUG
721 bool success =
722#endif
723 fIndexPool.appendIndices(indexCount,
724 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000725 &poolState.fPoolIndexBuffer,
726 &poolState.fPoolStartIndex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000727 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000728}
729
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000730void GrInOrderDrawBuffer::releaseVertexArray() {
731 // When the client provides an array as the vertex source we handled it
732 // by copying their array into reserved space.
733 this->GrInOrderDrawBuffer::releaseReservedVertexSpace();
734}
735
736void GrInOrderDrawBuffer::releaseIndexArray() {
737 // When the client provides an array as the index source we handled it
738 // by copying their array into reserved space.
739 this->GrInOrderDrawBuffer::releaseReservedIndexSpace();
740}
741
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000742void GrInOrderDrawBuffer::geometrySourceWillPush() {
743 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
744 poolState.fUsedPoolVertexBytes = 0;
745 poolState.fUsedPoolIndexBytes = 0;
746#if GR_DEBUG
747 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
748 poolState.fPoolStartVertex = ~0;
749 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
750 poolState.fPoolStartIndex = ~0;
751#endif
752}
753
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000754void GrInOrderDrawBuffer::geometrySourceWillPop(
755 const GeometrySrcState& restoredState) {
756 GrAssert(fGeoPoolStateStack.count() > 1);
757 fGeoPoolStateStack.pop_back();
758 GeometryPoolState& poolState = fGeoPoolStateStack.back();
759 // we have to assume that any slack we had in our vertex/index data
760 // is now unreleasable because data may have been appended later in the
761 // pool.
762 if (kReserved_GeometrySrcType == restoredState.fVertexSrc ||
763 kArray_GeometrySrcType == restoredState.fVertexSrc) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000764 poolState.fUsedPoolVertexBytes = restoredState.fVertexSize * restoredState.fVertexCount;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000765 }
766 if (kReserved_GeometrySrcType == restoredState.fIndexSrc ||
767 kArray_GeometrySrcType == restoredState.fIndexSrc) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000768 poolState.fUsedPoolIndexBytes = sizeof(uint16_t) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000769 restoredState.fIndexCount;
770 }
771}
772
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000773bool GrInOrderDrawBuffer::needsNewState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000774 return fStates.empty() || !fStates.back().isEqual(this->getDrawState());
reed@google.comac10a2d2010-12-22 21:39:39 +0000775}
776
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000777bool GrInOrderDrawBuffer::needsNewClip() const {
bsalomon@google.com358e4272013-01-10 14:40:28 +0000778 GrAssert(fClips.count() == fClipOrigins.count());
779 if (this->getDrawState().isClipState()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000780 if (fClipSet &&
bsalomon@google.com358e4272013-01-10 14:40:28 +0000781 (fClips.empty() ||
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000782 fClips.back() != *this->getClip()->fClipStack ||
783 fClipOrigins.back() != this->getClip()->fOrigin)) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000784 return true;
785 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000786 }
787 return false;
788}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000789
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000790void GrInOrderDrawBuffer::recordClip() {
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000791 fClips.push_back() = *this->getClip()->fClipStack;
792 fClipOrigins.push_back() = this->getClip()->fOrigin;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000793 fClipSet = false;
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000794 fCmds.push_back(kSetClip_Cmd);
795}
796
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000797void GrInOrderDrawBuffer::recordState() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000798 fStates.push_back().saveFrom(this->getDrawState());
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000799 fCmds.push_back(kSetState_Cmd);
800}
801
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000802GrInOrderDrawBuffer::DrawRecord* GrInOrderDrawBuffer::recordDraw(const DrawInfo& info) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000803 fCmds.push_back(kDraw_Cmd);
804 return &fDraws.push_back(info);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000805}
806
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000807GrInOrderDrawBuffer::StencilPath* GrInOrderDrawBuffer::recordStencilPath() {
808 fCmds.push_back(kStencilPath_Cmd);
809 return &fStencilPaths.push_back();
810}
811
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000812GrInOrderDrawBuffer::Clear* GrInOrderDrawBuffer::recordClear() {
813 fCmds.push_back(kClear_Cmd);
814 return &fClears.push_back();
reed@google.comac10a2d2010-12-22 21:39:39 +0000815}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000816
bsalomon@google.com116ad842013-04-09 15:38:19 +0000817GrInOrderDrawBuffer::CopySurface* GrInOrderDrawBuffer::recordCopySurface() {
818 fCmds.push_back(kCopySurface_Cmd);
819 return &fCopySurfaces.push_back();
820}
821
822
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000823void GrInOrderDrawBuffer::clipWillBeSet(const GrClipData* newClipData) {
824 INHERITED::clipWillBeSet(newClipData);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000825 fClipSet = true;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000826 fClipProxyState = kUnknown_ClipProxyState;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000827}