blob: e426de358a8bf0eaaa9bc44599d9f8917e4d94a3 [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.com6fcbfce2013-10-31 16:31:11 +0000216 fClipProxy = SkRect::MakeFromIRect(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
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000422void GrInOrderDrawBuffer::clear(const SkIRect* rect, GrColor color, GrRenderTarget* renderTarget) {
423 SkIRect r;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000424 if (NULL == renderTarget) {
425 renderTarget = this->drawState()->getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000426 SkASSERT(NULL != renderTarget);
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000427 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000428 if (NULL == rect) {
429 // We could do something smart and remove previous draws and clears to
430 // the current render target. If we get that smart we have to make sure
431 // those draws aren't read before this clear (render-to-texture).
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000432 r.setLTRB(0, 0, renderTarget->width(), renderTarget->height());
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000433 rect = &r;
434 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000435 Clear* clr = this->recordClear();
436 clr->fColor = color;
437 clr->fRect = *rect;
438 clr->fRenderTarget = renderTarget;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000439 renderTarget->ref();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000440}
441
reed@google.comac10a2d2010-12-22 21:39:39 +0000442void GrInOrderDrawBuffer::reset() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000443 SkASSERT(1 == fGeoPoolStateStack.count());
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000444 this->resetVertexSource();
445 this->resetIndexSource();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000446 int numDraws = fDraws.count();
447 for (int d = 0; d < numDraws; ++d) {
448 // we always have a VB, but not always an IB
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000449 SkASSERT(NULL != fDraws[d].fVertexBuffer);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000450 fDraws[d].fVertexBuffer->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000451 SkSafeUnref(fDraws[d].fIndexBuffer);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000452 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000453 fCmds.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000454 fDraws.reset();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000455 fStencilPaths.reset();
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000456 fDrawPaths.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000457 fStates.reset();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000458 fClears.reset();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000459 fVertexPool.reset();
460 fIndexPool.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000461 fClips.reset();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000462 fClipOrigins.reset();
bsalomon@google.com116ad842013-04-09 15:38:19 +0000463 fCopySurfaces.reset();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000464 fClipSet = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000465}
466
robertphillips@google.com1267fbd2013-07-03 18:37:27 +0000467void GrInOrderDrawBuffer::flush() {
468 if (fFlushing) {
469 return;
470 }
471
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000472 SkASSERT(kReserved_GeometrySrcType != this->getGeomSrc().fVertexSrc);
473 SkASSERT(kReserved_GeometrySrcType != this->getGeomSrc().fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000474
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000475 int numCmds = fCmds.count();
bsalomon@google.com358e4272013-01-10 14:40:28 +0000476 if (0 == numCmds) {
robertphillips@google.com1267fbd2013-07-03 18:37:27 +0000477 return;
reed@google.comac10a2d2010-12-22 21:39:39 +0000478 }
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000479
480 GrAutoTRestore<bool> flushRestore(&fFlushing);
481 fFlushing = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000482
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000483 fVertexPool.unlock();
484 fIndexPool.unlock();
reed@google.comac10a2d2010-12-22 21:39:39 +0000485
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000486 GrDrawTarget::AutoClipRestore acr(fDstGpu);
487 AutoGeometryAndStatePush agasp(fDstGpu, kPreserve_ASRInit);
bsalomon@google.comca432082013-01-23 19:53:46 +0000488
489 GrDrawState playbackState;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000490 GrDrawState* prevDrawState = fDstGpu->drawState();
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000491 prevDrawState->ref();
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000492 fDstGpu->setDrawState(&playbackState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000493
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000494 GrClipData clipData;
495
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000496 int currState = 0;
497 int currClip = 0;
498 int currClear = 0;
499 int currDraw = 0;
500 int currStencilPath = 0;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000501 int currDrawPath = 0;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000502 int currCopySurface = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000503
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000504 for (int c = 0; c < numCmds; ++c) {
505 switch (fCmds[c]) {
506 case kDraw_Cmd: {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000507 const DrawRecord& draw = fDraws[currDraw];
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000508 fDstGpu->setVertexSourceToBuffer(draw.fVertexBuffer);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000509 if (draw.isIndexed()) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000510 fDstGpu->setIndexSourceToBuffer(draw.fIndexBuffer);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000511 }
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000512 fDstGpu->executeDraw(draw);
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000513
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000514 ++currDraw;
515 break;
516 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000517 case kStencilPath_Cmd: {
518 const StencilPath& sp = fStencilPaths[currStencilPath];
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000519 fDstGpu->stencilPath(sp.fPath.get(), sp.fFill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000520 ++currStencilPath;
521 break;
522 }
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000523 case kDrawPath_Cmd: {
524 const DrawPath& cp = fDrawPaths[currDrawPath];
525 fDstGpu->executeDrawPath(cp.fPath.get(), cp.fFill,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000526 NULL != cp.fDstCopy.texture() ? &cp.fDstCopy : NULL);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000527 ++currDrawPath;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000528 break;
529 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000530 case kSetState_Cmd:
bsalomon@google.comca432082013-01-23 19:53:46 +0000531 fStates[currState].restoreTo(&playbackState);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000532 ++currState;
533 break;
534 case kSetClip_Cmd:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000535 clipData.fClipStack = &fClips[currClip];
536 clipData.fOrigin = fClipOrigins[currClip];
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000537 fDstGpu->setClip(&clipData);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000538 ++currClip;
539 break;
540 case kClear_Cmd:
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000541 fDstGpu->clear(&fClears[currClear].fRect,
542 fClears[currClear].fColor,
543 fClears[currClear].fRenderTarget);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000544 ++currClear;
545 break;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000546 case kCopySurface_Cmd:
547 fDstGpu->copySurface(fCopySurfaces[currCopySurface].fDst.get(),
548 fCopySurfaces[currCopySurface].fSrc.get(),
549 fCopySurfaces[currCopySurface].fSrcRect,
550 fCopySurfaces[currCopySurface].fDstPoint);
551 ++currCopySurface;
552 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000553 }
554 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000555 // we should have consumed all the states, clips, etc.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000556 SkASSERT(fStates.count() == currState);
557 SkASSERT(fClips.count() == currClip);
558 SkASSERT(fClipOrigins.count() == currClip);
559 SkASSERT(fClears.count() == currClear);
560 SkASSERT(fDraws.count() == currDraw);
561 SkASSERT(fCopySurfaces.count() == currCopySurface);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000562
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000563 fDstGpu->setDrawState(prevDrawState);
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000564 prevDrawState->unref();
bsalomon@google.com55e4a202013-01-11 13:54:21 +0000565 this->reset();
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000566 ++fDrawID;
reed@google.comac10a2d2010-12-22 21:39:39 +0000567}
568
bsalomon@google.com116ad842013-04-09 15:38:19 +0000569bool GrInOrderDrawBuffer::onCopySurface(GrSurface* dst,
570 GrSurface* src,
571 const SkIRect& srcRect,
572 const SkIPoint& dstPoint) {
573 if (fDstGpu->canCopySurface(dst, src, srcRect, dstPoint)) {
574 CopySurface* cs = this->recordCopySurface();
575 cs->fDst.reset(SkRef(dst));
576 cs->fSrc.reset(SkRef(src));
577 cs->fSrcRect = srcRect;
578 cs->fDstPoint = dstPoint;
579 return true;
580 } else {
581 return false;
582 }
583}
584
585bool GrInOrderDrawBuffer::onCanCopySurface(GrSurface* dst,
586 GrSurface* src,
587 const SkIRect& srcRect,
588 const SkIPoint& dstPoint) {
589 return fDstGpu->canCopySurface(dst, src, srcRect, dstPoint);
590}
591
bsalomon@google.comeb851172013-04-15 13:51:00 +0000592void GrInOrderDrawBuffer::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
593 fDstGpu->initCopySurfaceDstDesc(src, desc);
594}
595
robertphillips@google.com9528bdb2013-09-18 22:33:57 +0000596void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace(int vertexCount,
597 int indexCount) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000598 // We use geometryHints() to know whether to flush the draw buffer. We
599 // can't flush if we are inside an unbalanced pushGeometrySource.
600 // Moreover, flushing blows away vertex and index data that was
601 // previously reserved. So if the vertex or index data is pulled from
602 // reserved space and won't be released by this request then we can't
603 // flush.
604 bool insideGeoPush = fGeoPoolStateStack.count() > 1;
bsalomon@google.com97805382012-03-13 14:32:07 +0000605
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000606 bool unreleasedVertexSpace =
607 !vertexCount &&
608 kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc;
bsalomon@google.com97805382012-03-13 14:32:07 +0000609
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000610 bool unreleasedIndexSpace =
611 !indexCount &&
612 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
bsalomon@google.com97805382012-03-13 14:32:07 +0000613
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000614 // we don't want to finalize any reserved geom on the target since
615 // we don't know that the client has finished writing to it.
616 bool targetHasReservedGeom = fDstGpu->hasReservedVerticesOrIndices();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000617
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000618 int vcount = vertexCount;
619 int icount = indexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000620
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000621 if (!insideGeoPush &&
622 !unreleasedVertexSpace &&
623 !unreleasedIndexSpace &&
624 !targetHasReservedGeom &&
625 this->geometryHints(&vcount, &icount)) {
bsalomon@google.com97805382012-03-13 14:32:07 +0000626
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000627 this->flush();
bsalomon@google.com97805382012-03-13 14:32:07 +0000628 }
629}
630
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000631bool GrInOrderDrawBuffer::geometryHints(int* vertexCount,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000632 int* indexCount) const {
633 // we will recommend a flush if the data could fit in a single
634 // preallocated buffer but none are left and it can't fit
635 // in the current buffer (which may not be prealloced).
reed@google.comac10a2d2010-12-22 21:39:39 +0000636 bool flush = false;
637 if (NULL != indexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000638 int32_t currIndices = fIndexPool.currentBufferIndices();
639 if (*indexCount > currIndices &&
640 (!fIndexPool.preallocatedBuffersRemaining() &&
641 *indexCount <= fIndexPool.preallocatedBufferIndices())) {
642
643 flush = true;
644 }
645 *indexCount = currIndices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000646 }
647 if (NULL != vertexCount) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000648 size_t vertexSize = this->getDrawState().getVertexSize();
jvanverth@google.coma6338982013-01-31 21:34:25 +0000649 int32_t currVertices = fVertexPool.currentBufferVertices(vertexSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000650 if (*vertexCount > currVertices &&
651 (!fVertexPool.preallocatedBuffersRemaining() &&
jvanverth@google.coma6338982013-01-31 21:34:25 +0000652 *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexSize))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000653
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000654 flush = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000655 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000656 *vertexCount = currVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000657 }
658 return flush;
659}
660
jvanverth@google.coma6338982013-01-31 21:34:25 +0000661bool GrInOrderDrawBuffer::onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000662 int vertexCount,
663 void** vertices) {
664 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000665 SkASSERT(vertexCount > 0);
666 SkASSERT(NULL != vertices);
667 SkASSERT(0 == poolState.fUsedPoolVertexBytes);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000668
jvanverth@google.coma6338982013-01-31 21:34:25 +0000669 *vertices = fVertexPool.makeSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000670 vertexCount,
671 &poolState.fPoolVertexBuffer,
672 &poolState.fPoolStartVertex);
673 return NULL != *vertices;
674}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000675
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000676bool GrInOrderDrawBuffer::onReserveIndexSpace(int indexCount, void** indices) {
677 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000678 SkASSERT(indexCount > 0);
679 SkASSERT(NULL != indices);
680 SkASSERT(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000681
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000682 *indices = fIndexPool.makeSpace(indexCount,
683 &poolState.fPoolIndexBuffer,
684 &poolState.fPoolStartIndex);
685 return NULL != *indices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000686}
687
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000688void GrInOrderDrawBuffer::releaseReservedVertexSpace() {
689 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000690 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000691
692 // If we get a release vertex space call then our current source should either be reserved
693 // or array (which we copied into reserved space).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000694 SkASSERT(kReserved_GeometrySrcType == geoSrc.fVertexSrc ||
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000695 kArray_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000696
697 // When the caller reserved vertex buffer space we gave it back a pointer
698 // provided by the vertex buffer pool. At each draw we tracked the largest
699 // offset into the pool's pointer that was referenced. Now we return to the
700 // pool any portion at the tail of the allocation that no draw referenced.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000701 size_t reservedVertexBytes = geoSrc.fVertexSize * geoSrc.fVertexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000702 fVertexPool.putBack(reservedVertexBytes -
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000703 poolState.fUsedPoolVertexBytes);
704 poolState.fUsedPoolVertexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000705 poolState.fPoolVertexBuffer = NULL;
706 poolState.fPoolStartVertex = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000707}
708
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000709void GrInOrderDrawBuffer::releaseReservedIndexSpace() {
710 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000711 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000712
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000713 // If we get a release index space call then our current source should either be reserved
714 // or array (which we copied into reserved space).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000715 SkASSERT(kReserved_GeometrySrcType == geoSrc.fIndexSrc ||
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000716 kArray_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000717
718 // Similar to releaseReservedVertexSpace we return any unused portion at
719 // the tail
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000720 size_t reservedIndexBytes = sizeof(uint16_t) * geoSrc.fIndexCount;
721 fIndexPool.putBack(reservedIndexBytes - poolState.fUsedPoolIndexBytes);
722 poolState.fUsedPoolIndexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000723 poolState.fPoolIndexBuffer = NULL;
724 poolState.fPoolStartIndex = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000725}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000726
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000727void GrInOrderDrawBuffer::onSetVertexSourceToArray(const void* vertexArray,
728 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000729
730 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000731 SkASSERT(0 == poolState.fUsedPoolVertexBytes);
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000732#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000733 bool success =
734#endif
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000735 fVertexPool.appendVertices(this->getVertexSize(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000736 vertexCount,
737 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000738 &poolState.fPoolVertexBuffer,
739 &poolState.fPoolStartVertex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000740 GR_DEBUGASSERT(success);
741}
742
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000743void GrInOrderDrawBuffer::onSetIndexSourceToArray(const void* indexArray,
744 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000745 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000746 SkASSERT(0 == poolState.fUsedPoolIndexBytes);
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000747#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000748 bool success =
749#endif
750 fIndexPool.appendIndices(indexCount,
751 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000752 &poolState.fPoolIndexBuffer,
753 &poolState.fPoolStartIndex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000754 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000755}
756
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000757void GrInOrderDrawBuffer::releaseVertexArray() {
758 // When the client provides an array as the vertex source we handled it
759 // by copying their array into reserved space.
760 this->GrInOrderDrawBuffer::releaseReservedVertexSpace();
761}
762
763void GrInOrderDrawBuffer::releaseIndexArray() {
764 // When the client provides an array as the index source we handled it
765 // by copying their array into reserved space.
766 this->GrInOrderDrawBuffer::releaseReservedIndexSpace();
767}
768
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000769void GrInOrderDrawBuffer::geometrySourceWillPush() {
770 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
771 poolState.fUsedPoolVertexBytes = 0;
772 poolState.fUsedPoolIndexBytes = 0;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000773#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000774 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
775 poolState.fPoolStartVertex = ~0;
776 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
777 poolState.fPoolStartIndex = ~0;
778#endif
779}
780
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000781void GrInOrderDrawBuffer::geometrySourceWillPop(
782 const GeometrySrcState& restoredState) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000783 SkASSERT(fGeoPoolStateStack.count() > 1);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000784 fGeoPoolStateStack.pop_back();
785 GeometryPoolState& poolState = fGeoPoolStateStack.back();
786 // we have to assume that any slack we had in our vertex/index data
787 // is now unreleasable because data may have been appended later in the
788 // pool.
789 if (kReserved_GeometrySrcType == restoredState.fVertexSrc ||
790 kArray_GeometrySrcType == restoredState.fVertexSrc) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000791 poolState.fUsedPoolVertexBytes = restoredState.fVertexSize * restoredState.fVertexCount;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000792 }
793 if (kReserved_GeometrySrcType == restoredState.fIndexSrc ||
794 kArray_GeometrySrcType == restoredState.fIndexSrc) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000795 poolState.fUsedPoolIndexBytes = sizeof(uint16_t) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000796 restoredState.fIndexCount;
797 }
798}
799
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000800bool GrInOrderDrawBuffer::needsNewState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000801 return fStates.empty() || !fStates.back().isEqual(this->getDrawState());
reed@google.comac10a2d2010-12-22 21:39:39 +0000802}
803
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000804bool GrInOrderDrawBuffer::needsNewClip() const {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000805 SkASSERT(fClips.count() == fClipOrigins.count());
bsalomon@google.com358e4272013-01-10 14:40:28 +0000806 if (this->getDrawState().isClipState()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000807 if (fClipSet &&
bsalomon@google.com358e4272013-01-10 14:40:28 +0000808 (fClips.empty() ||
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000809 fClips.back() != *this->getClip()->fClipStack ||
810 fClipOrigins.back() != this->getClip()->fOrigin)) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000811 return true;
812 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000813 }
814 return false;
815}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000816
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000817void GrInOrderDrawBuffer::recordClip() {
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000818 fClips.push_back() = *this->getClip()->fClipStack;
819 fClipOrigins.push_back() = this->getClip()->fOrigin;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000820 fClipSet = false;
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000821 fCmds.push_back(kSetClip_Cmd);
822}
823
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000824void GrInOrderDrawBuffer::recordState() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000825 fStates.push_back().saveFrom(this->getDrawState());
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000826 fCmds.push_back(kSetState_Cmd);
827}
828
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000829GrInOrderDrawBuffer::DrawRecord* GrInOrderDrawBuffer::recordDraw(const DrawInfo& info) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000830 fCmds.push_back(kDraw_Cmd);
831 return &fDraws.push_back(info);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000832}
833
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000834GrInOrderDrawBuffer::StencilPath* GrInOrderDrawBuffer::recordStencilPath() {
835 fCmds.push_back(kStencilPath_Cmd);
836 return &fStencilPaths.push_back();
837}
838
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000839GrInOrderDrawBuffer::DrawPath* GrInOrderDrawBuffer::recordDrawPath() {
840 fCmds.push_back(kDrawPath_Cmd);
841 return &fDrawPaths.push_back();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000842}
843
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000844GrInOrderDrawBuffer::Clear* GrInOrderDrawBuffer::recordClear() {
845 fCmds.push_back(kClear_Cmd);
846 return &fClears.push_back();
reed@google.comac10a2d2010-12-22 21:39:39 +0000847}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000848
bsalomon@google.com116ad842013-04-09 15:38:19 +0000849GrInOrderDrawBuffer::CopySurface* GrInOrderDrawBuffer::recordCopySurface() {
850 fCmds.push_back(kCopySurface_Cmd);
851 return &fCopySurfaces.push_back();
852}
853
854
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000855void GrInOrderDrawBuffer::clipWillBeSet(const GrClipData* newClipData) {
856 INHERITED::clipWillBeSet(newClipData);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000857 fClipSet = true;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000858 fClipProxyState = kUnknown_ClipProxyState;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000859}