blob: 669167c419bbc9680bb2dabbd344a0ef8354256d [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)
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000030 , fFlushing(false)
31 , fDrawID(0) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +000032
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000033 fDstGpu->ref();
bsalomon@google.combcce8922013-03-25 15:38:39 +000034 fCaps.reset(SkRef(fDstGpu->caps()));
bsalomon@google.com18c9c192011-09-22 21:01:31 +000035
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000036 SkASSERT(NULL != vertexPool);
37 SkASSERT(NULL != indexPool);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000038
39 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
40 poolState.fUsedPoolVertexBytes = 0;
41 poolState.fUsedPoolIndexBytes = 0;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000042#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000043 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
44 poolState.fPoolStartVertex = ~0;
45 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
46 poolState.fPoolStartIndex = ~0;
47#endif
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000048 this->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000049}
50
51GrInOrderDrawBuffer::~GrInOrderDrawBuffer() {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000052 this->reset();
bsalomon@google.com4a018bb2011-10-28 19:50:21 +000053 // This must be called by before the GrDrawTarget destructor
54 this->releaseGeometry();
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000055 fDstGpu->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +000056}
57
bsalomon@google.com934c5702012-03-20 21:17:58 +000058////////////////////////////////////////////////////////////////////////////////
59
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000060namespace {
61void get_vertex_bounds(const void* vertices,
62 size_t vertexSize,
63 int vertexCount,
64 SkRect* bounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000065 SkASSERT(vertexSize >= sizeof(GrPoint));
66 SkASSERT(vertexCount > 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000067 const GrPoint* point = static_cast<const GrPoint*>(vertices);
68 bounds->fLeft = bounds->fRight = point->fX;
69 bounds->fTop = bounds->fBottom = point->fY;
70 for (int i = 1; i < vertexCount; ++i) {
71 point = reinterpret_cast<GrPoint*>(reinterpret_cast<intptr_t>(point) + vertexSize);
72 bounds->growToInclude(point->fX, point->fY);
73 }
74}
bsalomon@google.com934c5702012-03-20 21:17:58 +000075}
76
robertphillips@google.com42903302013-04-20 12:26:07 +000077
78namespace {
79
80extern const GrVertexAttrib kRectPosColorUVAttribs[] = {
81 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
82 {kVec4ub_GrVertexAttribType, sizeof(GrPoint), kColor_GrVertexAttribBinding},
rmistry@google.comc9f3b382013-04-22 12:45:30 +000083 {kVec2f_GrVertexAttribType, sizeof(GrPoint)+sizeof(GrColor),
robertphillips@google.com42903302013-04-20 12:26:07 +000084 kLocalCoord_GrVertexAttribBinding},
85};
86
87extern const GrVertexAttrib kRectPosUVAttribs[] = {
88 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
89 {kVec2f_GrVertexAttribType, sizeof(GrPoint), kLocalCoord_GrVertexAttribBinding},
90};
91
92static void set_vertex_attributes(GrDrawState* drawState,
93 bool hasColor, bool hasUVs,
94 int* colorOffset, int* localOffset) {
95 *colorOffset = -1;
96 *localOffset = -1;
97
98 // Using per-vertex colors allows batching across colors. (A lot of rects in a row differing
99 // only in color is a common occurrence in tables). However, having per-vertex colors disables
100 // blending optimizations because we don't know if the color will be solid or not. These
101 // optimizations help determine whether coverage and color can be blended correctly when
102 // dual-source blending isn't available. This comes into play when there is coverage. If colors
103 // were a stage it could take a hint that every vertex's color will be opaque.
104 if (hasColor && hasUVs) {
105 *colorOffset = sizeof(GrPoint);
106 *localOffset = sizeof(GrPoint) + sizeof(GrColor);
107 drawState->setVertexAttribs<kRectPosColorUVAttribs>(3);
108 } else if (hasColor) {
109 *colorOffset = sizeof(GrPoint);
110 drawState->setVertexAttribs<kRectPosColorUVAttribs>(2);
111 } else if (hasUVs) {
112 *localOffset = sizeof(GrPoint);
113 drawState->setVertexAttribs<kRectPosUVAttribs>(2);
114 } else {
115 drawState->setVertexAttribs<kRectPosUVAttribs>(1);
116 }
117}
118
119};
120
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000121void GrInOrderDrawBuffer::onDrawRect(const SkRect& rect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000122 const SkMatrix* matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000123 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000124 const SkMatrix* localMatrix) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000125 GrDrawState::AutoColorRestore acr;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000126
127 GrDrawState* drawState = this->drawState();
128
129 GrColor color = drawState->getColor();
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000130
robertphillips@google.com42903302013-04-20 12:26:07 +0000131 int colorOffset, localOffset;
132 set_vertex_attributes(drawState,
133 this->caps()->dualSourceBlendingSupport() || drawState->hasSolidCoverage(),
134 NULL != localRect,
135 &colorOffset, &localOffset);
136 if (colorOffset >= 0) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000137 // We set the draw state's color to white here. This is done so that any batching performed
138 // in our subclass's onDraw() won't get a false from GrDrawState::op== due to a color
139 // mismatch. TODO: Once vertex layout is owned by GrDrawState it should skip comparing the
rmistry@google.comc9f3b382013-04-22 12:45:30 +0000140 // 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 +0000141 // this.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000142 acr.set(drawState, 0xFFFFFFFF);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000143 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000144
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000145 AutoReleaseGeometry geo(this, 4, 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000146 if (!geo.succeeded()) {
147 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com934c5702012-03-20 21:17:58 +0000148 return;
149 }
150
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000151 // Go to device coords to allow batching across matrix changes
152 SkMatrix combinedMatrix;
153 if (NULL != matrix) {
154 combinedMatrix = *matrix;
155 } else {
156 combinedMatrix.reset();
157 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000158 combinedMatrix.postConcat(drawState->getViewMatrix());
jvanverth@google.com39768252013-02-14 15:25:44 +0000159 // 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 +0000160 // modify that stage's matrix. Otherwise if the effect is generating its source rect from
161 // the vertex positions then we have to account for the view matrix change.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000162 GrDrawState::AutoViewMatrixRestore avmr;
163 if (!avmr.setIdentity(drawState)) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000164 return;
165 }
166
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000167 size_t vsize = drawState->getVertexSize();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000168
169 geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vsize);
170 combinedMatrix.mapPointsWithStride(geo.positions(), vsize, 4);
171
172 SkRect devBounds;
173 // since we already computed the dev verts, set the bounds hint. This will help us avoid
174 // unnecessary clipping in our onDraw().
175 get_vertex_bounds(geo.vertices(), vsize, 4, &devBounds);
176
bsalomon@google.comc7818882013-03-20 19:19:53 +0000177 if (localOffset >= 0) {
178 GrPoint* coords = GrTCast<GrPoint*>(GrTCast<intptr_t>(geo.vertices()) + localOffset);
179 coords->setRectFan(localRect->fLeft, localRect->fTop,
180 localRect->fRight, localRect->fBottom,
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000181 vsize);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000182 if (NULL != localMatrix) {
183 localMatrix->mapPointsWithStride(coords, vsize, 4);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000184 }
185 }
186
187 if (colorOffset >= 0) {
188 GrColor* vertColor = GrTCast<GrColor*>(GrTCast<intptr_t>(geo.vertices()) + colorOffset);
189 for (int i = 0; i < 4; ++i) {
190 *vertColor = color;
191 vertColor = (GrColor*) ((intptr_t) vertColor + vsize);
192 }
193 }
194
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000195 this->setIndexSourceToBuffer(this->getContext()->getQuadIndexBuffer());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000196 this->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, 4, 6, &devBounds);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000197
198 // to ensure that stashing the drawState ptr is valid
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000199 SkASSERT(this->drawState() == drawState);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000200}
201
202bool GrInOrderDrawBuffer::quickInsideClip(const SkRect& devBounds) {
203 if (!this->getDrawState().isClipState()) {
204 return true;
205 }
206 if (kUnknown_ClipProxyState == fClipProxyState) {
207 SkIRect rect;
208 bool iior;
209 this->getClip()->getConservativeBounds(this->getDrawState().getRenderTarget(), &rect, &iior);
210 if (iior) {
211 // The clip is a rect. We will remember that in fProxyClip. It is common for an edge (or
212 // all edges) of the clip to be at the edge of the RT. However, we get that clipping for
213 // free via the viewport. We don't want to think that clipping must be enabled in this
214 // case. So we extend the clip outward from the edge to avoid these false negatives.
215 fClipProxyState = kValid_ClipProxyState;
reed@google.com44699382013-10-31 17:28:30 +0000216 fClipProxy = SkRect::Make(rect);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000217
218 if (fClipProxy.fLeft <= 0) {
219 fClipProxy.fLeft = SK_ScalarMin;
220 }
221 if (fClipProxy.fTop <= 0) {
222 fClipProxy.fTop = SK_ScalarMin;
223 }
224 if (fClipProxy.fRight >= this->getDrawState().getRenderTarget()->width()) {
225 fClipProxy.fRight = SK_ScalarMax;
226 }
227 if (fClipProxy.fBottom >= this->getDrawState().getRenderTarget()->height()) {
228 fClipProxy.fBottom = SK_ScalarMax;
229 }
230 } else {
231 fClipProxyState = kInvalid_ClipProxyState;
232 }
233 }
234 if (kValid_ClipProxyState == fClipProxyState) {
235 return fClipProxy.contains(devBounds);
236 }
237 SkPoint originOffset = {SkIntToScalar(this->getClip()->fOrigin.fX),
238 SkIntToScalar(this->getClip()->fOrigin.fY)};
239 SkRect clipSpaceBounds = devBounds;
240 clipSpaceBounds.offset(originOffset);
241 return this->getClip()->fClipStack->quickContains(clipSpaceBounds);
242}
243
244int GrInOrderDrawBuffer::concatInstancedDraw(const DrawInfo& info) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000245 SkASSERT(info.isInstanced());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000246
bsalomon@google.com934c5702012-03-20 21:17:58 +0000247 const GeometrySrcState& geomSrc = this->getGeomSrc();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000248 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000249
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000250 // we only attempt to concat the case when reserved verts are used with a client-specified index
251 // buffer. To make this work with client-specified VBs we'd need to know if the VB was updated
252 // between draws.
253 if (kReserved_GeometrySrcType != geomSrc.fVertexSrc ||
254 kBuffer_GeometrySrcType != geomSrc.fIndexSrc) {
255 return 0;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000256 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000257 // Check if there is a draw info that is compatible that uses the same VB from the pool and
258 // the same IB
259 if (kDraw_Cmd != fCmds.back()) {
260 return 0;
261 }
262
263 DrawRecord* draw = &fDraws.back();
264 GeometryPoolState& poolState = fGeoPoolStateStack.back();
265 const GrVertexBuffer* vertexBuffer = poolState.fPoolVertexBuffer;
266
267 if (!draw->isInstanced() ||
268 draw->verticesPerInstance() != info.verticesPerInstance() ||
269 draw->indicesPerInstance() != info.indicesPerInstance() ||
270 draw->fVertexBuffer != vertexBuffer ||
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000271 draw->fIndexBuffer != geomSrc.fIndexBuffer) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000272 return 0;
273 }
274 // info does not yet account for the offset from the start of the pool's VB while the previous
275 // draw record does.
276 int adjustedStartVertex = poolState.fPoolStartVertex + info.startVertex();
277 if (draw->startVertex() + draw->vertexCount() != adjustedStartVertex) {
278 return 0;
279 }
280
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000281 SkASSERT(poolState.fPoolStartVertex == draw->startVertex() + draw->vertexCount());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000282
283 // how many instances can be concat'ed onto draw given the size of the index buffer
284 int instancesToConcat = this->indexCountInCurrentSource() / info.indicesPerInstance();
285 instancesToConcat -= draw->instanceCount();
286 instancesToConcat = GrMin(instancesToConcat, info.instanceCount());
287
288 // update the amount of reserved vertex data actually referenced in draws
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000289 size_t vertexBytes = instancesToConcat * info.verticesPerInstance() *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000290 drawState.getVertexSize();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000291 poolState.fUsedPoolVertexBytes = GrMax(poolState.fUsedPoolVertexBytes, vertexBytes);
292
293 draw->adjustInstanceCount(instancesToConcat);
294 return instancesToConcat;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000295}
296
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000297class AutoClipReenable {
298public:
299 AutoClipReenable() : fDrawState(NULL) {}
300 ~AutoClipReenable() {
301 if (NULL != fDrawState) {
302 fDrawState->enableState(GrDrawState::kClip_StateBit);
303 }
304 }
305 void set(GrDrawState* drawState) {
306 if (drawState->isClipState()) {
307 fDrawState = drawState;
308 drawState->disableState(GrDrawState::kClip_StateBit);
309 }
310 }
311private:
312 GrDrawState* fDrawState;
313};
314
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000315void GrInOrderDrawBuffer::onDraw(const DrawInfo& info) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000316
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000317 GeometryPoolState& poolState = fGeoPoolStateStack.back();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000318 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000319 AutoClipReenable acr;
320
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000321 if (drawState.isClipState() &&
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000322 NULL != info.getDevBounds() &&
323 this->quickInsideClip(*info.getDevBounds())) {
324 acr.set(this->drawState());
325 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000326
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000327 if (this->needsNewClip()) {
328 this->recordClip();
329 }
330 if (this->needsNewState()) {
331 this->recordState();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000332 }
333
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000334 DrawRecord* draw;
335 if (info.isInstanced()) {
336 int instancesConcated = this->concatInstancedDraw(info);
337 if (info.instanceCount() > instancesConcated) {
338 draw = this->recordDraw(info);
339 draw->adjustInstanceCount(-instancesConcated);
340 } else {
341 return;
342 }
343 } else {
344 draw = this->recordDraw(info);
345 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000346
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000347 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000348 case kBuffer_GeometrySrcType:
349 draw->fVertexBuffer = this->getGeomSrc().fVertexBuffer;
350 break;
351 case kReserved_GeometrySrcType: // fallthrough
352 case kArray_GeometrySrcType: {
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000353 size_t vertexBytes = (info.vertexCount() + info.startVertex()) *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000354 drawState.getVertexSize();
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000355 poolState.fUsedPoolVertexBytes = GrMax(poolState.fUsedPoolVertexBytes, vertexBytes);
356 draw->fVertexBuffer = poolState.fPoolVertexBuffer;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000357 draw->adjustStartVertex(poolState.fPoolStartVertex);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000358 break;
359 }
360 default:
361 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000362 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000363 draw->fVertexBuffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000364
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000365 if (info.isIndexed()) {
366 switch (this->getGeomSrc().fIndexSrc) {
367 case kBuffer_GeometrySrcType:
368 draw->fIndexBuffer = this->getGeomSrc().fIndexBuffer;
369 break;
370 case kReserved_GeometrySrcType: // fallthrough
371 case kArray_GeometrySrcType: {
372 size_t indexBytes = (info.indexCount() + info.startIndex()) * sizeof(uint16_t);
373 poolState.fUsedPoolIndexBytes = GrMax(poolState.fUsedPoolIndexBytes, indexBytes);
374 draw->fIndexBuffer = poolState.fPoolIndexBuffer;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000375 draw->adjustStartIndex(poolState.fPoolStartIndex);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000376 break;
377 }
378 default:
379 GrCrash("unknown geom src type");
380 }
381 draw->fIndexBuffer->ref();
382 } else {
383 draw->fIndexBuffer = NULL;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000384 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000385}
386
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000387GrInOrderDrawBuffer::StencilPath::StencilPath() {}
388GrInOrderDrawBuffer::DrawPath::DrawPath() {}
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000389
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000390void GrInOrderDrawBuffer::onStencilPath(const GrPath* path, SkPath::FillType fill) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000391 if (this->needsNewClip()) {
392 this->recordClip();
393 }
394 // Only compare the subset of GrDrawState relevant to path stenciling?
395 if (this->needsNewState()) {
396 this->recordState();
397 }
398 StencilPath* sp = this->recordStencilPath();
399 sp->fPath.reset(path);
400 path->ref();
401 sp->fFill = fill;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000402}
403
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000404void GrInOrderDrawBuffer::onDrawPath(const GrPath* path,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000405 SkPath::FillType fill, const GrDeviceCoordTexture* dstCopy) {
406 if (this->needsNewClip()) {
407 this->recordClip();
408 }
409 // TODO: Only compare the subset of GrDrawState relevant to path covering?
410 if (this->needsNewState()) {
411 this->recordState();
412 }
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000413 DrawPath* cp = this->recordDrawPath();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000414 cp->fPath.reset(path);
415 path->ref();
416 cp->fFill = fill;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000417 if (NULL != dstCopy) {
418 cp->fDstCopy = *dstCopy;
419 }
420}
421
skia.committer@gmail.com18bb41e2013-11-01 07:02:15 +0000422void GrInOrderDrawBuffer::clear(const SkIRect* rect, GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000423 bool canIgnoreRect, GrRenderTarget* renderTarget) {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000424 SkIRect r;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000425 if (NULL == renderTarget) {
426 renderTarget = this->drawState()->getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000427 SkASSERT(NULL != renderTarget);
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000428 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000429 if (NULL == rect) {
430 // We could do something smart and remove previous draws and clears to
431 // the current render target. If we get that smart we have to make sure
432 // those draws aren't read before this clear (render-to-texture).
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000433 r.setLTRB(0, 0, renderTarget->width(), renderTarget->height());
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000434 rect = &r;
435 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000436 Clear* clr = this->recordClear();
437 clr->fColor = color;
438 clr->fRect = *rect;
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000439 clr->fCanIgnoreRect = canIgnoreRect;
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000440 clr->fRenderTarget = renderTarget;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000441 renderTarget->ref();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000442}
443
reed@google.comac10a2d2010-12-22 21:39:39 +0000444void GrInOrderDrawBuffer::reset() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000445 SkASSERT(1 == fGeoPoolStateStack.count());
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000446 this->resetVertexSource();
447 this->resetIndexSource();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000448 int numDraws = fDraws.count();
449 for (int d = 0; d < numDraws; ++d) {
450 // we always have a VB, but not always an IB
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000451 SkASSERT(NULL != fDraws[d].fVertexBuffer);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000452 fDraws[d].fVertexBuffer->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000453 SkSafeUnref(fDraws[d].fIndexBuffer);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000454 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000455 fCmds.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000456 fDraws.reset();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000457 fStencilPaths.reset();
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000458 fDrawPaths.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000459 fStates.reset();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000460 fClears.reset();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000461 fVertexPool.reset();
462 fIndexPool.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000463 fClips.reset();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000464 fClipOrigins.reset();
bsalomon@google.com116ad842013-04-09 15:38:19 +0000465 fCopySurfaces.reset();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000466 fClipSet = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000467}
468
robertphillips@google.com1267fbd2013-07-03 18:37:27 +0000469void GrInOrderDrawBuffer::flush() {
470 if (fFlushing) {
471 return;
472 }
473
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000474 SkASSERT(kReserved_GeometrySrcType != this->getGeomSrc().fVertexSrc);
475 SkASSERT(kReserved_GeometrySrcType != this->getGeomSrc().fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000476
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000477 int numCmds = fCmds.count();
bsalomon@google.com358e4272013-01-10 14:40:28 +0000478 if (0 == numCmds) {
robertphillips@google.com1267fbd2013-07-03 18:37:27 +0000479 return;
reed@google.comac10a2d2010-12-22 21:39:39 +0000480 }
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000481
482 GrAutoTRestore<bool> flushRestore(&fFlushing);
483 fFlushing = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000484
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000485 fVertexPool.unlock();
486 fIndexPool.unlock();
reed@google.comac10a2d2010-12-22 21:39:39 +0000487
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000488 GrDrawTarget::AutoClipRestore acr(fDstGpu);
489 AutoGeometryAndStatePush agasp(fDstGpu, kPreserve_ASRInit);
bsalomon@google.comca432082013-01-23 19:53:46 +0000490
491 GrDrawState playbackState;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000492 GrDrawState* prevDrawState = fDstGpu->drawState();
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000493 prevDrawState->ref();
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000494 fDstGpu->setDrawState(&playbackState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000495
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000496 GrClipData clipData;
497
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000498 int currState = 0;
499 int currClip = 0;
500 int currClear = 0;
501 int currDraw = 0;
502 int currStencilPath = 0;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000503 int currDrawPath = 0;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000504 int currCopySurface = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000505
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000506 for (int c = 0; c < numCmds; ++c) {
507 switch (fCmds[c]) {
508 case kDraw_Cmd: {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000509 const DrawRecord& draw = fDraws[currDraw];
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000510 fDstGpu->setVertexSourceToBuffer(draw.fVertexBuffer);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000511 if (draw.isIndexed()) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000512 fDstGpu->setIndexSourceToBuffer(draw.fIndexBuffer);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000513 }
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000514 fDstGpu->executeDraw(draw);
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000515
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000516 ++currDraw;
517 break;
518 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000519 case kStencilPath_Cmd: {
520 const StencilPath& sp = fStencilPaths[currStencilPath];
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000521 fDstGpu->stencilPath(sp.fPath.get(), sp.fFill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000522 ++currStencilPath;
523 break;
524 }
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000525 case kDrawPath_Cmd: {
526 const DrawPath& cp = fDrawPaths[currDrawPath];
527 fDstGpu->executeDrawPath(cp.fPath.get(), cp.fFill,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000528 NULL != cp.fDstCopy.texture() ? &cp.fDstCopy : NULL);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000529 ++currDrawPath;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000530 break;
531 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000532 case kSetState_Cmd:
bsalomon@google.comca432082013-01-23 19:53:46 +0000533 fStates[currState].restoreTo(&playbackState);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000534 ++currState;
535 break;
536 case kSetClip_Cmd:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000537 clipData.fClipStack = &fClips[currClip];
538 clipData.fOrigin = fClipOrigins[currClip];
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000539 fDstGpu->setClip(&clipData);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000540 ++currClip;
541 break;
542 case kClear_Cmd:
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000543 fDstGpu->clear(&fClears[currClear].fRect,
544 fClears[currClear].fColor,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000545 fClears[currClear].fCanIgnoreRect,
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000546 fClears[currClear].fRenderTarget);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000547 ++currClear;
548 break;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000549 case kCopySurface_Cmd:
550 fDstGpu->copySurface(fCopySurfaces[currCopySurface].fDst.get(),
551 fCopySurfaces[currCopySurface].fSrc.get(),
552 fCopySurfaces[currCopySurface].fSrcRect,
553 fCopySurfaces[currCopySurface].fDstPoint);
554 ++currCopySurface;
555 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000556 }
557 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000558 // we should have consumed all the states, clips, etc.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000559 SkASSERT(fStates.count() == currState);
560 SkASSERT(fClips.count() == currClip);
561 SkASSERT(fClipOrigins.count() == currClip);
562 SkASSERT(fClears.count() == currClear);
563 SkASSERT(fDraws.count() == currDraw);
564 SkASSERT(fCopySurfaces.count() == currCopySurface);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000565
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000566 fDstGpu->setDrawState(prevDrawState);
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000567 prevDrawState->unref();
bsalomon@google.com55e4a202013-01-11 13:54:21 +0000568 this->reset();
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000569 ++fDrawID;
reed@google.comac10a2d2010-12-22 21:39:39 +0000570}
571
bsalomon@google.com116ad842013-04-09 15:38:19 +0000572bool GrInOrderDrawBuffer::onCopySurface(GrSurface* dst,
573 GrSurface* src,
574 const SkIRect& srcRect,
575 const SkIPoint& dstPoint) {
576 if (fDstGpu->canCopySurface(dst, src, srcRect, dstPoint)) {
577 CopySurface* cs = this->recordCopySurface();
578 cs->fDst.reset(SkRef(dst));
579 cs->fSrc.reset(SkRef(src));
580 cs->fSrcRect = srcRect;
581 cs->fDstPoint = dstPoint;
582 return true;
583 } else {
584 return false;
585 }
586}
587
588bool GrInOrderDrawBuffer::onCanCopySurface(GrSurface* dst,
589 GrSurface* src,
590 const SkIRect& srcRect,
591 const SkIPoint& dstPoint) {
592 return fDstGpu->canCopySurface(dst, src, srcRect, dstPoint);
593}
594
bsalomon@google.comeb851172013-04-15 13:51:00 +0000595void GrInOrderDrawBuffer::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
596 fDstGpu->initCopySurfaceDstDesc(src, desc);
597}
598
robertphillips@google.com9528bdb2013-09-18 22:33:57 +0000599void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace(int vertexCount,
600 int indexCount) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000601 // We use geometryHints() to know whether to flush the draw buffer. We
602 // can't flush if we are inside an unbalanced pushGeometrySource.
603 // Moreover, flushing blows away vertex and index data that was
604 // previously reserved. So if the vertex or index data is pulled from
605 // reserved space and won't be released by this request then we can't
606 // flush.
607 bool insideGeoPush = fGeoPoolStateStack.count() > 1;
bsalomon@google.com97805382012-03-13 14:32:07 +0000608
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000609 bool unreleasedVertexSpace =
610 !vertexCount &&
611 kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc;
bsalomon@google.com97805382012-03-13 14:32:07 +0000612
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000613 bool unreleasedIndexSpace =
614 !indexCount &&
615 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
bsalomon@google.com97805382012-03-13 14:32:07 +0000616
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000617 // we don't want to finalize any reserved geom on the target since
618 // we don't know that the client has finished writing to it.
619 bool targetHasReservedGeom = fDstGpu->hasReservedVerticesOrIndices();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000620
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000621 int vcount = vertexCount;
622 int icount = indexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000623
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000624 if (!insideGeoPush &&
625 !unreleasedVertexSpace &&
626 !unreleasedIndexSpace &&
627 !targetHasReservedGeom &&
628 this->geometryHints(&vcount, &icount)) {
bsalomon@google.com97805382012-03-13 14:32:07 +0000629
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000630 this->flush();
bsalomon@google.com97805382012-03-13 14:32:07 +0000631 }
632}
633
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000634bool GrInOrderDrawBuffer::geometryHints(int* vertexCount,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000635 int* indexCount) const {
636 // we will recommend a flush if the data could fit in a single
637 // preallocated buffer but none are left and it can't fit
638 // in the current buffer (which may not be prealloced).
reed@google.comac10a2d2010-12-22 21:39:39 +0000639 bool flush = false;
640 if (NULL != indexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000641 int32_t currIndices = fIndexPool.currentBufferIndices();
642 if (*indexCount > currIndices &&
643 (!fIndexPool.preallocatedBuffersRemaining() &&
644 *indexCount <= fIndexPool.preallocatedBufferIndices())) {
645
646 flush = true;
647 }
648 *indexCount = currIndices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000649 }
650 if (NULL != vertexCount) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000651 size_t vertexSize = this->getDrawState().getVertexSize();
jvanverth@google.coma6338982013-01-31 21:34:25 +0000652 int32_t currVertices = fVertexPool.currentBufferVertices(vertexSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000653 if (*vertexCount > currVertices &&
654 (!fVertexPool.preallocatedBuffersRemaining() &&
jvanverth@google.coma6338982013-01-31 21:34:25 +0000655 *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexSize))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000656
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000657 flush = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000658 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000659 *vertexCount = currVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000660 }
661 return flush;
662}
663
jvanverth@google.coma6338982013-01-31 21:34:25 +0000664bool GrInOrderDrawBuffer::onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000665 int vertexCount,
666 void** vertices) {
667 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000668 SkASSERT(vertexCount > 0);
669 SkASSERT(NULL != vertices);
670 SkASSERT(0 == poolState.fUsedPoolVertexBytes);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000671
jvanverth@google.coma6338982013-01-31 21:34:25 +0000672 *vertices = fVertexPool.makeSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000673 vertexCount,
674 &poolState.fPoolVertexBuffer,
675 &poolState.fPoolStartVertex);
676 return NULL != *vertices;
677}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000678
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000679bool GrInOrderDrawBuffer::onReserveIndexSpace(int indexCount, void** indices) {
680 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000681 SkASSERT(indexCount > 0);
682 SkASSERT(NULL != indices);
683 SkASSERT(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000684
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000685 *indices = fIndexPool.makeSpace(indexCount,
686 &poolState.fPoolIndexBuffer,
687 &poolState.fPoolStartIndex);
688 return NULL != *indices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000689}
690
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000691void GrInOrderDrawBuffer::releaseReservedVertexSpace() {
692 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000693 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000694
695 // If we get a release vertex space call then our current source should either be reserved
696 // or array (which we copied into reserved space).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000697 SkASSERT(kReserved_GeometrySrcType == geoSrc.fVertexSrc ||
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000698 kArray_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000699
700 // When the caller reserved vertex buffer space we gave it back a pointer
701 // provided by the vertex buffer pool. At each draw we tracked the largest
702 // offset into the pool's pointer that was referenced. Now we return to the
703 // pool any portion at the tail of the allocation that no draw referenced.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000704 size_t reservedVertexBytes = geoSrc.fVertexSize * geoSrc.fVertexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000705 fVertexPool.putBack(reservedVertexBytes -
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000706 poolState.fUsedPoolVertexBytes);
707 poolState.fUsedPoolVertexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000708 poolState.fPoolVertexBuffer = NULL;
709 poolState.fPoolStartVertex = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000710}
711
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000712void GrInOrderDrawBuffer::releaseReservedIndexSpace() {
713 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000714 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000715
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000716 // If we get a release index space call then our current source should either be reserved
717 // or array (which we copied into reserved space).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000718 SkASSERT(kReserved_GeometrySrcType == geoSrc.fIndexSrc ||
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000719 kArray_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000720
721 // Similar to releaseReservedVertexSpace we return any unused portion at
722 // the tail
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000723 size_t reservedIndexBytes = sizeof(uint16_t) * geoSrc.fIndexCount;
724 fIndexPool.putBack(reservedIndexBytes - poolState.fUsedPoolIndexBytes);
725 poolState.fUsedPoolIndexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000726 poolState.fPoolIndexBuffer = NULL;
727 poolState.fPoolStartIndex = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000728}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000729
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000730void GrInOrderDrawBuffer::onSetVertexSourceToArray(const void* vertexArray,
731 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000732
733 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000734 SkASSERT(0 == poolState.fUsedPoolVertexBytes);
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000735#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000736 bool success =
737#endif
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000738 fVertexPool.appendVertices(this->getVertexSize(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000739 vertexCount,
740 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000741 &poolState.fPoolVertexBuffer,
742 &poolState.fPoolStartVertex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000743 GR_DEBUGASSERT(success);
744}
745
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000746void GrInOrderDrawBuffer::onSetIndexSourceToArray(const void* indexArray,
747 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000748 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000749 SkASSERT(0 == poolState.fUsedPoolIndexBytes);
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000750#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000751 bool success =
752#endif
753 fIndexPool.appendIndices(indexCount,
754 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000755 &poolState.fPoolIndexBuffer,
756 &poolState.fPoolStartIndex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000757 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000758}
759
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000760void GrInOrderDrawBuffer::releaseVertexArray() {
761 // When the client provides an array as the vertex source we handled it
762 // by copying their array into reserved space.
763 this->GrInOrderDrawBuffer::releaseReservedVertexSpace();
764}
765
766void GrInOrderDrawBuffer::releaseIndexArray() {
767 // When the client provides an array as the index source we handled it
768 // by copying their array into reserved space.
769 this->GrInOrderDrawBuffer::releaseReservedIndexSpace();
770}
771
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000772void GrInOrderDrawBuffer::geometrySourceWillPush() {
773 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
774 poolState.fUsedPoolVertexBytes = 0;
775 poolState.fUsedPoolIndexBytes = 0;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000776#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000777 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
778 poolState.fPoolStartVertex = ~0;
779 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
780 poolState.fPoolStartIndex = ~0;
781#endif
782}
783
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000784void GrInOrderDrawBuffer::geometrySourceWillPop(
785 const GeometrySrcState& restoredState) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000786 SkASSERT(fGeoPoolStateStack.count() > 1);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000787 fGeoPoolStateStack.pop_back();
788 GeometryPoolState& poolState = fGeoPoolStateStack.back();
789 // we have to assume that any slack we had in our vertex/index data
790 // is now unreleasable because data may have been appended later in the
791 // pool.
792 if (kReserved_GeometrySrcType == restoredState.fVertexSrc ||
793 kArray_GeometrySrcType == restoredState.fVertexSrc) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000794 poolState.fUsedPoolVertexBytes = restoredState.fVertexSize * restoredState.fVertexCount;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000795 }
796 if (kReserved_GeometrySrcType == restoredState.fIndexSrc ||
797 kArray_GeometrySrcType == restoredState.fIndexSrc) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000798 poolState.fUsedPoolIndexBytes = sizeof(uint16_t) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000799 restoredState.fIndexCount;
800 }
801}
802
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000803bool GrInOrderDrawBuffer::needsNewState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000804 return fStates.empty() || !fStates.back().isEqual(this->getDrawState());
reed@google.comac10a2d2010-12-22 21:39:39 +0000805}
806
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000807bool GrInOrderDrawBuffer::needsNewClip() const {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000808 SkASSERT(fClips.count() == fClipOrigins.count());
bsalomon@google.com358e4272013-01-10 14:40:28 +0000809 if (this->getDrawState().isClipState()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000810 if (fClipSet &&
bsalomon@google.com358e4272013-01-10 14:40:28 +0000811 (fClips.empty() ||
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000812 fClips.back() != *this->getClip()->fClipStack ||
813 fClipOrigins.back() != this->getClip()->fOrigin)) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000814 return true;
815 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000816 }
817 return false;
818}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000819
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000820void GrInOrderDrawBuffer::recordClip() {
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000821 fClips.push_back() = *this->getClip()->fClipStack;
822 fClipOrigins.push_back() = this->getClip()->fOrigin;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000823 fClipSet = false;
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000824 fCmds.push_back(kSetClip_Cmd);
825}
826
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000827void GrInOrderDrawBuffer::recordState() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000828 fStates.push_back().saveFrom(this->getDrawState());
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000829 fCmds.push_back(kSetState_Cmd);
830}
831
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000832GrInOrderDrawBuffer::DrawRecord* GrInOrderDrawBuffer::recordDraw(const DrawInfo& info) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000833 fCmds.push_back(kDraw_Cmd);
834 return &fDraws.push_back(info);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000835}
836
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000837GrInOrderDrawBuffer::StencilPath* GrInOrderDrawBuffer::recordStencilPath() {
838 fCmds.push_back(kStencilPath_Cmd);
839 return &fStencilPaths.push_back();
840}
841
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000842GrInOrderDrawBuffer::DrawPath* GrInOrderDrawBuffer::recordDrawPath() {
843 fCmds.push_back(kDrawPath_Cmd);
844 return &fDrawPaths.push_back();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000845}
846
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000847GrInOrderDrawBuffer::Clear* GrInOrderDrawBuffer::recordClear() {
848 fCmds.push_back(kClear_Cmd);
849 return &fClears.push_back();
reed@google.comac10a2d2010-12-22 21:39:39 +0000850}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000851
bsalomon@google.com116ad842013-04-09 15:38:19 +0000852GrInOrderDrawBuffer::CopySurface* GrInOrderDrawBuffer::recordCopySurface() {
853 fCmds.push_back(kCopySurface_Cmd);
854 return &fCopySurfaces.push_back();
855}
856
857
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000858void GrInOrderDrawBuffer::clipWillBeSet(const GrClipData* newClipData) {
859 INHERITED::clipWillBeSet(newClipData);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000860 fClipSet = true;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000861 fClipProxyState = kUnknown_ClipProxyState;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000862}