blob: bd54967c20f7e3a2f9809139be534c08fa8645d8 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
10#include "GrInOrderDrawBuffer.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000011#include "GrBufferAllocPool.h"
12#include "GrGpu.h"
13#include "GrIndexBuffer.h"
14#include "GrPath.h"
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000015#include "GrRenderTarget.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000016#include "GrTexture.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000017#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018
bsalomon@google.com471d4712011-08-23 15:45:25 +000019GrInOrderDrawBuffer::GrInOrderDrawBuffer(const GrGpu* gpu,
20 GrVertexBufferAllocPool* vertexPool,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000021 GrIndexBufferAllocPool* indexPool)
bsalomon@google.com97805382012-03-13 14:32:07 +000022 : fAutoFlushTarget(NULL)
23 , fClipSet(true)
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000024 , fClipProxyState(kUnknown_ClipProxyState)
robertphillips@google.com69705572012-03-21 19:46:50 +000025 , fVertexPool(*vertexPool)
26 , fIndexPool(*indexPool)
robertphillips@google.comc82a8b72012-06-21 20:15:48 +000027 , fFlushing(false) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +000028
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000029 fGpu.reset(SkRef(gpu));
bsalomon@google.com18c9c192011-09-22 21:01:31 +000030 fCaps = gpu->getCaps();
31
bsalomon@google.com1c13c962011-02-14 16:51:21 +000032 GrAssert(NULL != vertexPool);
33 GrAssert(NULL != indexPool);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000034
35 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
36 poolState.fUsedPoolVertexBytes = 0;
37 poolState.fUsedPoolIndexBytes = 0;
38#if GR_DEBUG
39 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
40 poolState.fPoolStartVertex = ~0;
41 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
42 poolState.fPoolStartIndex = ~0;
43#endif
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000044 this->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000045}
46
47GrInOrderDrawBuffer::~GrInOrderDrawBuffer() {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000048 this->reset();
bsalomon@google.com4a018bb2011-10-28 19:50:21 +000049 // This must be called by before the GrDrawTarget destructor
50 this->releaseGeometry();
bsalomon@google.com97805382012-03-13 14:32:07 +000051 GrSafeUnref(fAutoFlushTarget);
reed@google.comac10a2d2010-12-22 21:39:39 +000052}
53
bsalomon@google.com934c5702012-03-20 21:17:58 +000054////////////////////////////////////////////////////////////////////////////////
55
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000056namespace {
57void get_vertex_bounds(const void* vertices,
58 size_t vertexSize,
59 int vertexCount,
60 SkRect* bounds) {
61 GrAssert(vertexSize >= sizeof(GrPoint));
62 GrAssert(vertexCount > 0);
63 const GrPoint* point = static_cast<const GrPoint*>(vertices);
64 bounds->fLeft = bounds->fRight = point->fX;
65 bounds->fTop = bounds->fBottom = point->fY;
66 for (int i = 1; i < vertexCount; ++i) {
67 point = reinterpret_cast<GrPoint*>(reinterpret_cast<intptr_t>(point) + vertexSize);
68 bounds->growToInclude(point->fX, point->fY);
69 }
70}
bsalomon@google.com934c5702012-03-20 21:17:58 +000071}
72
bsalomon@google.comd302f142011-03-03 13:54:13 +000073void GrInOrderDrawBuffer::drawRect(const GrRect& rect,
bsalomon@google.comb9086a02012-11-01 18:02:54 +000074 const SkMatrix* matrix,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000075 const GrRect* srcRects[],
bsalomon@google.comb9086a02012-11-01 18:02:54 +000076 const SkMatrix* srcMatrices[]) {
bsalomon@google.comd302f142011-03-03 13:54:13 +000077
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000078 GrVertexLayout layout = 0;
79 GrDrawState::AutoColorRestore acr;
80 GrColor color = this->drawState()->getColor();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000081
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000082 // Using per-vertex colors allows batching across colors. (A lot of rects in a row differing
83 // only in color is a common occurrence in tables). However, having per-vertex colors disables
84 // blending optimizations because we don't know if the color will be solid or not. These
85 // optimizations help determine whether coverage and color can be blended correctly when
86 // dual-source blending isn't available. This comes into play when there is coverage. If colors
87 // were a stage it could take a hint that every vertex's color will be opaque.
88 if (this->getCaps().dualSourceBlendingSupport() ||
89 this->getDrawState().hasSolidCoverage(this->getGeomSrc().fVertexLayout)) {
90 layout |= GrDrawState::kColor_VertexLayoutBit;;
91 // We set the draw state's color to white here. This is done so that any batching performed
92 // in our subclass's onDraw() won't get a false from GrDrawState::op== due to a color
93 // mismatch. TODO: Once vertex layout is owned by GrDrawState it should skip comparing the
94 // constant color in its op== when the kColor layout bit is set and then we can remove this.
95 acr.set(this->drawState(), 0xFFFFFFFF);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000096 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000097
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000098 uint32_t explicitCoordMask = 0;
99 if (NULL != srcRects) {
100 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
101 int numTC = 0;
102 if (NULL != srcRects[s]) {
103 layout |= GrDrawState::StageTexCoordVertexLayoutBit(s, numTC);
104 ++numTC;
105 explicitCoordMask |= (1 << s);
106 }
107 }
108 }
109
110 AutoReleaseGeometry geo(this, layout, 4, 0);
111 if (!geo.succeeded()) {
112 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com934c5702012-03-20 21:17:58 +0000113 return;
114 }
115
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000116 // Go to device coords to allow batching across matrix changes
117 SkMatrix combinedMatrix;
118 if (NULL != matrix) {
119 combinedMatrix = *matrix;
120 } else {
121 combinedMatrix.reset();
122 }
123 combinedMatrix.postConcat(this->drawState()->getViewMatrix());
124 // When the caller has provided an explicit source rects for a stage then we don't want to
125 // modify that stage's matrix. Otherwise if the effect is generating its source rect from
126 // the vertex positions then we have to account for the view matrix change.
127 GrDrawState::AutoDeviceCoordDraw adcd(this->drawState(), explicitCoordMask);
128 if (!adcd.succeeded()) {
129 return;
130 }
131
132 int stageOffsets[GrDrawState::kNumStages], colorOffset;
133 int vsize = GrDrawState::VertexSizeAndOffsetsByStage(layout, stageOffsets,
134 &colorOffset, NULL, NULL);
135
136 geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vsize);
137 combinedMatrix.mapPointsWithStride(geo.positions(), vsize, 4);
138
139 SkRect devBounds;
140 // since we already computed the dev verts, set the bounds hint. This will help us avoid
141 // unnecessary clipping in our onDraw().
142 get_vertex_bounds(geo.vertices(), vsize, 4, &devBounds);
143
144 for (int i = 0; i < GrDrawState::kNumStages; ++i) {
145 if (explicitCoordMask & (1 << i)) {
reed@google.com009dfe22013-02-01 15:05:18 +0000146 GrAssert(0 != stageOffsets[i]);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000147 GrPoint* coords = GrTCast<GrPoint*>(GrTCast<intptr_t>(geo.vertices()) +
148 stageOffsets[i]);
149 coords->setRectFan(srcRects[i]->fLeft, srcRects[i]->fTop,
150 srcRects[i]->fRight, srcRects[i]->fBottom,
151 vsize);
152 if (NULL != srcMatrices && NULL != srcMatrices[i]) {
153 srcMatrices[i]->mapPointsWithStride(coords, vsize, 4);
154 }
155 } else {
reed@google.com009dfe22013-02-01 15:05:18 +0000156 GrAssert(0 == stageOffsets[i]);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000157 }
158 }
159
160 if (colorOffset >= 0) {
161 GrColor* vertColor = GrTCast<GrColor*>(GrTCast<intptr_t>(geo.vertices()) + colorOffset);
162 for (int i = 0; i < 4; ++i) {
163 *vertColor = color;
164 vertColor = (GrColor*) ((intptr_t) vertColor + vsize);
165 }
166 }
167
168 this->setIndexSourceToBuffer(fGpu->getQuadIndexBuffer());
169 this->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, 4, 6, &devBounds);
170}
171
172bool GrInOrderDrawBuffer::quickInsideClip(const SkRect& devBounds) {
173 if (!this->getDrawState().isClipState()) {
174 return true;
175 }
176 if (kUnknown_ClipProxyState == fClipProxyState) {
177 SkIRect rect;
178 bool iior;
179 this->getClip()->getConservativeBounds(this->getDrawState().getRenderTarget(), &rect, &iior);
180 if (iior) {
181 // The clip is a rect. We will remember that in fProxyClip. It is common for an edge (or
182 // all edges) of the clip to be at the edge of the RT. However, we get that clipping for
183 // free via the viewport. We don't want to think that clipping must be enabled in this
184 // case. So we extend the clip outward from the edge to avoid these false negatives.
185 fClipProxyState = kValid_ClipProxyState;
186 fClipProxy = SkRect::MakeFromIRect(rect);
187
188 if (fClipProxy.fLeft <= 0) {
189 fClipProxy.fLeft = SK_ScalarMin;
190 }
191 if (fClipProxy.fTop <= 0) {
192 fClipProxy.fTop = SK_ScalarMin;
193 }
194 if (fClipProxy.fRight >= this->getDrawState().getRenderTarget()->width()) {
195 fClipProxy.fRight = SK_ScalarMax;
196 }
197 if (fClipProxy.fBottom >= this->getDrawState().getRenderTarget()->height()) {
198 fClipProxy.fBottom = SK_ScalarMax;
199 }
200 } else {
201 fClipProxyState = kInvalid_ClipProxyState;
202 }
203 }
204 if (kValid_ClipProxyState == fClipProxyState) {
205 return fClipProxy.contains(devBounds);
206 }
207 SkPoint originOffset = {SkIntToScalar(this->getClip()->fOrigin.fX),
208 SkIntToScalar(this->getClip()->fOrigin.fY)};
209 SkRect clipSpaceBounds = devBounds;
210 clipSpaceBounds.offset(originOffset);
211 return this->getClip()->fClipStack->quickContains(clipSpaceBounds);
212}
213
214int GrInOrderDrawBuffer::concatInstancedDraw(const DrawInfo& info) {
215 GrAssert(info.isInstanced());
216
bsalomon@google.com934c5702012-03-20 21:17:58 +0000217 const GeometrySrcState& geomSrc = this->getGeomSrc();
218
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000219 // we only attempt to concat the case when reserved verts are used with a client-specified index
220 // buffer. To make this work with client-specified VBs we'd need to know if the VB was updated
221 // between draws.
222 if (kReserved_GeometrySrcType != geomSrc.fVertexSrc ||
223 kBuffer_GeometrySrcType != geomSrc.fIndexSrc) {
224 return 0;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000225 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000226 // Check if there is a draw info that is compatible that uses the same VB from the pool and
227 // the same IB
228 if (kDraw_Cmd != fCmds.back()) {
229 return 0;
230 }
231
232 DrawRecord* draw = &fDraws.back();
233 GeometryPoolState& poolState = fGeoPoolStateStack.back();
234 const GrVertexBuffer* vertexBuffer = poolState.fPoolVertexBuffer;
235
236 if (!draw->isInstanced() ||
237 draw->verticesPerInstance() != info.verticesPerInstance() ||
238 draw->indicesPerInstance() != info.indicesPerInstance() ||
239 draw->fVertexBuffer != vertexBuffer ||
240 draw->fIndexBuffer != geomSrc.fIndexBuffer ||
241 draw->fVertexLayout != geomSrc.fVertexLayout) {
242 return 0;
243 }
244 // info does not yet account for the offset from the start of the pool's VB while the previous
245 // draw record does.
246 int adjustedStartVertex = poolState.fPoolStartVertex + info.startVertex();
247 if (draw->startVertex() + draw->vertexCount() != adjustedStartVertex) {
248 return 0;
249 }
250
251 GrAssert(poolState.fPoolStartVertex == draw->startVertex() + draw->vertexCount());
252
253 // how many instances can be concat'ed onto draw given the size of the index buffer
254 int instancesToConcat = this->indexCountInCurrentSource() / info.indicesPerInstance();
255 instancesToConcat -= draw->instanceCount();
256 instancesToConcat = GrMin(instancesToConcat, info.instanceCount());
257
258 // update the amount of reserved vertex data actually referenced in draws
259 size_t vertexBytes = instancesToConcat * info.verticesPerInstance() *
260 GrDrawState::VertexSize(draw->fVertexLayout);
261 poolState.fUsedPoolVertexBytes = GrMax(poolState.fUsedPoolVertexBytes, vertexBytes);
262
263 draw->adjustInstanceCount(instancesToConcat);
264 return instancesToConcat;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000265}
266
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000267class AutoClipReenable {
268public:
269 AutoClipReenable() : fDrawState(NULL) {}
270 ~AutoClipReenable() {
271 if (NULL != fDrawState) {
272 fDrawState->enableState(GrDrawState::kClip_StateBit);
273 }
274 }
275 void set(GrDrawState* drawState) {
276 if (drawState->isClipState()) {
277 fDrawState = drawState;
278 drawState->disableState(GrDrawState::kClip_StateBit);
279 }
280 }
281private:
282 GrDrawState* fDrawState;
283};
284
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000285void GrInOrderDrawBuffer::onDraw(const DrawInfo& info) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000286
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000287 GeometryPoolState& poolState = fGeoPoolStateStack.back();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000288 AutoClipReenable acr;
289
290 if (this->getDrawState().isClipState() &&
291 NULL != info.getDevBounds() &&
292 this->quickInsideClip(*info.getDevBounds())) {
293 acr.set(this->drawState());
294 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000295
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000296 if (this->needsNewClip()) {
297 this->recordClip();
298 }
299 if (this->needsNewState()) {
300 this->recordState();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000301 }
302
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000303 DrawRecord* draw;
304 if (info.isInstanced()) {
305 int instancesConcated = this->concatInstancedDraw(info);
306 if (info.instanceCount() > instancesConcated) {
307 draw = this->recordDraw(info);
308 draw->adjustInstanceCount(-instancesConcated);
309 } else {
310 return;
311 }
312 } else {
313 draw = this->recordDraw(info);
314 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000315 draw->fVertexLayout = this->getVertexLayout();
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000316
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000317 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000318 case kBuffer_GeometrySrcType:
319 draw->fVertexBuffer = this->getGeomSrc().fVertexBuffer;
320 break;
321 case kReserved_GeometrySrcType: // fallthrough
322 case kArray_GeometrySrcType: {
323 size_t vertexBytes = (info.vertexCount() + info.startVertex()) *
324 GrDrawState::VertexSize(draw->fVertexLayout);
325 poolState.fUsedPoolVertexBytes = GrMax(poolState.fUsedPoolVertexBytes, vertexBytes);
326 draw->fVertexBuffer = poolState.fPoolVertexBuffer;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000327 draw->adjustStartVertex(poolState.fPoolStartVertex);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000328 break;
329 }
330 default:
331 GrCrash("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000332 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000333 draw->fVertexBuffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000334
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000335 if (info.isIndexed()) {
336 switch (this->getGeomSrc().fIndexSrc) {
337 case kBuffer_GeometrySrcType:
338 draw->fIndexBuffer = this->getGeomSrc().fIndexBuffer;
339 break;
340 case kReserved_GeometrySrcType: // fallthrough
341 case kArray_GeometrySrcType: {
342 size_t indexBytes = (info.indexCount() + info.startIndex()) * sizeof(uint16_t);
343 poolState.fUsedPoolIndexBytes = GrMax(poolState.fUsedPoolIndexBytes, indexBytes);
344 draw->fIndexBuffer = poolState.fPoolIndexBuffer;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000345 draw->adjustStartIndex(poolState.fPoolStartIndex);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000346 break;
347 }
348 default:
349 GrCrash("unknown geom src type");
350 }
351 draw->fIndexBuffer->ref();
352 } else {
353 draw->fIndexBuffer = NULL;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000354 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000355}
356
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000357GrInOrderDrawBuffer::StencilPath::StencilPath() : fStroke(SkStrokeRec::kFill_InitStyle) {}
358
359void GrInOrderDrawBuffer::onStencilPath(const GrPath* path, const SkStrokeRec& stroke,
sugoi@google.com12b4e272012-12-06 20:13:11 +0000360 SkPath::FillType fill) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000361 if (this->needsNewClip()) {
362 this->recordClip();
363 }
364 // Only compare the subset of GrDrawState relevant to path stenciling?
365 if (this->needsNewState()) {
366 this->recordState();
367 }
368 StencilPath* sp = this->recordStencilPath();
369 sp->fPath.reset(path);
370 path->ref();
371 sp->fFill = fill;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000372 sp->fStroke = stroke;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000373}
374
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000375void GrInOrderDrawBuffer::clear(const GrIRect* rect, GrColor color, GrRenderTarget* renderTarget) {
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000376 GrIRect r;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000377 if (NULL == renderTarget) {
378 renderTarget = this->drawState()->getRenderTarget();
379 GrAssert(NULL != renderTarget);
380 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000381 if (NULL == rect) {
382 // We could do something smart and remove previous draws and clears to
383 // the current render target. If we get that smart we have to make sure
384 // those draws aren't read before this clear (render-to-texture).
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000385 r.setLTRB(0, 0, renderTarget->width(), renderTarget->height());
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000386 rect = &r;
387 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000388 Clear* clr = this->recordClear();
389 clr->fColor = color;
390 clr->fRect = *rect;
391 clr->fRenderTarget = renderTarget;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000392 renderTarget->ref();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000393}
394
reed@google.comac10a2d2010-12-22 21:39:39 +0000395void GrInOrderDrawBuffer::reset() {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000396 GrAssert(1 == fGeoPoolStateStack.count());
397 this->resetVertexSource();
398 this->resetIndexSource();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000399 int numDraws = fDraws.count();
400 for (int d = 0; d < numDraws; ++d) {
401 // we always have a VB, but not always an IB
402 GrAssert(NULL != fDraws[d].fVertexBuffer);
403 fDraws[d].fVertexBuffer->unref();
404 GrSafeUnref(fDraws[d].fIndexBuffer);
405 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000406 fCmds.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000407 fDraws.reset();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000408 fStencilPaths.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000409 fStates.reset();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000410 fClears.reset();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000411 fVertexPool.reset();
412 fIndexPool.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000413 fClips.reset();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000414 fClipOrigins.reset();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000415 fClipSet = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000416}
417
bsalomon@google.com55e4a202013-01-11 13:54:21 +0000418bool GrInOrderDrawBuffer::flushTo(GrDrawTarget* target) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000419 GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fVertexSrc);
420 GrAssert(kReserved_GeometrySrcType != this->getGeomSrc().fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000421
reed@google.comac10a2d2010-12-22 21:39:39 +0000422 GrAssert(NULL != target);
423 GrAssert(target != this); // not considered and why?
424
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000425 int numCmds = fCmds.count();
bsalomon@google.com358e4272013-01-10 14:40:28 +0000426 if (0 == numCmds) {
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000427 return false;
reed@google.comac10a2d2010-12-22 21:39:39 +0000428 }
429
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000430 fVertexPool.unlock();
431 fIndexPool.unlock();
reed@google.comac10a2d2010-12-22 21:39:39 +0000432
reed@google.comac10a2d2010-12-22 21:39:39 +0000433 GrDrawTarget::AutoClipRestore acr(target);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000434 AutoGeometryPush agp(target);
bsalomon@google.comca432082013-01-23 19:53:46 +0000435
436 GrDrawState playbackState;
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000437 GrDrawState* prevDrawState = target->drawState();
438 prevDrawState->ref();
bsalomon@google.comca432082013-01-23 19:53:46 +0000439 target->setDrawState(&playbackState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000440
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000441 GrClipData clipData;
442
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000443 int currState = 0;
444 int currClip = 0;
445 int currClear = 0;
446 int currDraw = 0;
447 int currStencilPath = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000448
bsalomon@google.comca432082013-01-23 19:53:46 +0000449
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000450 for (int c = 0; c < numCmds; ++c) {
451 switch (fCmds[c]) {
452 case kDraw_Cmd: {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000453 const DrawRecord& draw = fDraws[currDraw];
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000454 target->setVertexSourceToBuffer(draw.fVertexLayout, draw.fVertexBuffer);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000455 if (draw.isIndexed()) {
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000456 target->setIndexSourceToBuffer(draw.fIndexBuffer);
457 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000458 target->executeDraw(draw);
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000459
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000460 ++currDraw;
461 break;
462 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000463 case kStencilPath_Cmd: {
464 const StencilPath& sp = fStencilPaths[currStencilPath];
sugoi@google.com12b4e272012-12-06 20:13:11 +0000465 target->stencilPath(sp.fPath.get(), sp.fStroke, sp.fFill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000466 ++currStencilPath;
467 break;
468 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000469 case kSetState_Cmd:
bsalomon@google.comca432082013-01-23 19:53:46 +0000470 fStates[currState].restoreTo(&playbackState);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000471 ++currState;
472 break;
473 case kSetClip_Cmd:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000474 clipData.fClipStack = &fClips[currClip];
475 clipData.fOrigin = fClipOrigins[currClip];
476 target->setClip(&clipData);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000477 ++currClip;
478 break;
479 case kClear_Cmd:
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000480 target->clear(&fClears[currClear].fRect,
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000481 fClears[currClear].fColor,
482 fClears[currClear].fRenderTarget);
483 ++currClear;
484 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000485 }
486 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000487 // we should have consumed all the states, clips, etc.
488 GrAssert(fStates.count() == currState);
489 GrAssert(fClips.count() == currClip);
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000490 GrAssert(fClipOrigins.count() == currClip);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000491 GrAssert(fClears.count() == currClear);
492 GrAssert(fDraws.count() == currDraw);
493
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000494 target->setDrawState(prevDrawState);
495 prevDrawState->unref();
bsalomon@google.com55e4a202013-01-11 13:54:21 +0000496 this->reset();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000497 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000498}
499
bsalomon@google.com97805382012-03-13 14:32:07 +0000500void GrInOrderDrawBuffer::setAutoFlushTarget(GrDrawTarget* target) {
501 GrSafeAssign(fAutoFlushTarget, target);
502}
503
504void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace(
jvanverth@google.coma6338982013-01-31 21:34:25 +0000505 size_t vertexSize,
bsalomon@google.com97805382012-03-13 14:32:07 +0000506 int vertexCount,
507 int indexCount) {
508 if (NULL != fAutoFlushTarget) {
509 // We use geometryHints() to know whether to flush the draw buffer. We
510 // can't flush if we are inside an unbalanced pushGeometrySource.
511 // Moreover, flushing blows away vertex and index data that was
512 // previously reserved. So if the vertex or index data is pulled from
513 // reserved space and won't be released by this request then we can't
514 // flush.
515 bool insideGeoPush = fGeoPoolStateStack.count() > 1;
516
517 bool unreleasedVertexSpace =
518 !vertexCount &&
519 kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc;
520
521 bool unreleasedIndexSpace =
522 !indexCount &&
523 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
524
525 // we don't want to finalize any reserved geom on the target since
526 // we don't know that the client has finished writing to it.
527 bool targetHasReservedGeom =
528 fAutoFlushTarget->hasReservedVerticesOrIndices();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000529
bsalomon@google.com97805382012-03-13 14:32:07 +0000530 int vcount = vertexCount;
531 int icount = indexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000532
bsalomon@google.com97805382012-03-13 14:32:07 +0000533 if (!insideGeoPush &&
534 !unreleasedVertexSpace &&
535 !unreleasedIndexSpace &&
536 !targetHasReservedGeom &&
jvanverth@google.coma6338982013-01-31 21:34:25 +0000537 this->geometryHints(vertexSize, &vcount, &icount)) {
bsalomon@google.com97805382012-03-13 14:32:07 +0000538
539 this->flushTo(fAutoFlushTarget);
540 }
541 }
542}
543
jvanverth@google.coma6338982013-01-31 21:34:25 +0000544bool GrInOrderDrawBuffer::geometryHints(size_t vertexSize,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000545 int* vertexCount,
546 int* indexCount) const {
547 // we will recommend a flush if the data could fit in a single
548 // preallocated buffer but none are left and it can't fit
549 // in the current buffer (which may not be prealloced).
reed@google.comac10a2d2010-12-22 21:39:39 +0000550 bool flush = false;
551 if (NULL != indexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000552 int32_t currIndices = fIndexPool.currentBufferIndices();
553 if (*indexCount > currIndices &&
554 (!fIndexPool.preallocatedBuffersRemaining() &&
555 *indexCount <= fIndexPool.preallocatedBufferIndices())) {
556
557 flush = true;
558 }
559 *indexCount = currIndices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000560 }
561 if (NULL != vertexCount) {
jvanverth@google.coma6338982013-01-31 21:34:25 +0000562 int32_t currVertices = fVertexPool.currentBufferVertices(vertexSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000563 if (*vertexCount > currVertices &&
564 (!fVertexPool.preallocatedBuffersRemaining() &&
jvanverth@google.coma6338982013-01-31 21:34:25 +0000565 *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexSize))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000566
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000567 flush = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000568 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000569 *vertexCount = currVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000570 }
571 return flush;
572}
573
jvanverth@google.coma6338982013-01-31 21:34:25 +0000574bool GrInOrderDrawBuffer::onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000575 int vertexCount,
576 void** vertices) {
577 GeometryPoolState& poolState = fGeoPoolStateStack.back();
578 GrAssert(vertexCount > 0);
579 GrAssert(NULL != vertices);
580 GrAssert(0 == poolState.fUsedPoolVertexBytes);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000581
jvanverth@google.coma6338982013-01-31 21:34:25 +0000582 *vertices = fVertexPool.makeSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000583 vertexCount,
584 &poolState.fPoolVertexBuffer,
585 &poolState.fPoolStartVertex);
586 return NULL != *vertices;
587}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000588
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000589bool GrInOrderDrawBuffer::onReserveIndexSpace(int indexCount, void** indices) {
590 GeometryPoolState& poolState = fGeoPoolStateStack.back();
591 GrAssert(indexCount > 0);
592 GrAssert(NULL != indices);
593 GrAssert(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000594
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000595 *indices = fIndexPool.makeSpace(indexCount,
596 &poolState.fPoolIndexBuffer,
597 &poolState.fPoolStartIndex);
598 return NULL != *indices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000599}
600
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000601void GrInOrderDrawBuffer::releaseReservedVertexSpace() {
602 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000603 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000604
605 // If we get a release vertex space call then our current source should either be reserved
606 // or array (which we copied into reserved space).
607 GrAssert(kReserved_GeometrySrcType == geoSrc.fVertexSrc ||
608 kArray_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000609
610 // When the caller reserved vertex buffer space we gave it back a pointer
611 // provided by the vertex buffer pool. At each draw we tracked the largest
612 // offset into the pool's pointer that was referenced. Now we return to the
613 // pool any portion at the tail of the allocation that no draw referenced.
jvanverth@google.comcc782382013-01-28 20:39:48 +0000614 size_t reservedVertexBytes = GrDrawState::VertexSize(geoSrc.fVertexLayout) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000615 geoSrc.fVertexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000616 fVertexPool.putBack(reservedVertexBytes -
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000617 poolState.fUsedPoolVertexBytes);
618 poolState.fUsedPoolVertexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000619 poolState.fPoolVertexBuffer = NULL;
620 poolState.fPoolStartVertex = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000621}
622
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000623void GrInOrderDrawBuffer::releaseReservedIndexSpace() {
624 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000625 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000626
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000627 // If we get a release index space call then our current source should either be reserved
628 // or array (which we copied into reserved space).
629 GrAssert(kReserved_GeometrySrcType == geoSrc.fIndexSrc ||
630 kArray_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000631
632 // Similar to releaseReservedVertexSpace we return any unused portion at
633 // the tail
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000634 size_t reservedIndexBytes = sizeof(uint16_t) * geoSrc.fIndexCount;
635 fIndexPool.putBack(reservedIndexBytes - poolState.fUsedPoolIndexBytes);
636 poolState.fUsedPoolIndexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000637 poolState.fPoolIndexBuffer = NULL;
638 poolState.fPoolStartIndex = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000639}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000640
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000641void GrInOrderDrawBuffer::onSetVertexSourceToArray(const void* vertexArray,
642 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000643
644 GeometryPoolState& poolState = fGeoPoolStateStack.back();
645 GrAssert(0 == poolState.fUsedPoolVertexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000646#if GR_DEBUG
647 bool success =
648#endif
jvanverth@google.coma6338982013-01-31 21:34:25 +0000649 fVertexPool.appendVertices(GrDrawState::VertexSize(this->getVertexLayout()),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000650 vertexCount,
651 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000652 &poolState.fPoolVertexBuffer,
653 &poolState.fPoolStartVertex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000654 GR_DEBUGASSERT(success);
655}
656
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000657void GrInOrderDrawBuffer::onSetIndexSourceToArray(const void* indexArray,
658 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000659 GeometryPoolState& poolState = fGeoPoolStateStack.back();
660 GrAssert(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000661#if GR_DEBUG
662 bool success =
663#endif
664 fIndexPool.appendIndices(indexCount,
665 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000666 &poolState.fPoolIndexBuffer,
667 &poolState.fPoolStartIndex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000668 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000669}
670
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000671void GrInOrderDrawBuffer::releaseVertexArray() {
672 // When the client provides an array as the vertex source we handled it
673 // by copying their array into reserved space.
674 this->GrInOrderDrawBuffer::releaseReservedVertexSpace();
675}
676
677void GrInOrderDrawBuffer::releaseIndexArray() {
678 // When the client provides an array as the index source we handled it
679 // by copying their array into reserved space.
680 this->GrInOrderDrawBuffer::releaseReservedIndexSpace();
681}
682
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000683void GrInOrderDrawBuffer::geometrySourceWillPush() {
684 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
685 poolState.fUsedPoolVertexBytes = 0;
686 poolState.fUsedPoolIndexBytes = 0;
687#if GR_DEBUG
688 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
689 poolState.fPoolStartVertex = ~0;
690 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
691 poolState.fPoolStartIndex = ~0;
692#endif
693}
694
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000695void GrInOrderDrawBuffer::geometrySourceWillPop(
696 const GeometrySrcState& restoredState) {
697 GrAssert(fGeoPoolStateStack.count() > 1);
698 fGeoPoolStateStack.pop_back();
699 GeometryPoolState& poolState = fGeoPoolStateStack.back();
700 // we have to assume that any slack we had in our vertex/index data
701 // is now unreleasable because data may have been appended later in the
702 // pool.
703 if (kReserved_GeometrySrcType == restoredState.fVertexSrc ||
704 kArray_GeometrySrcType == restoredState.fVertexSrc) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000705 poolState.fUsedPoolVertexBytes =
jvanverth@google.comcc782382013-01-28 20:39:48 +0000706 GrDrawState::VertexSize(restoredState.fVertexLayout) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000707 restoredState.fVertexCount;
708 }
709 if (kReserved_GeometrySrcType == restoredState.fIndexSrc ||
710 kArray_GeometrySrcType == restoredState.fIndexSrc) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000711 poolState.fUsedPoolIndexBytes = sizeof(uint16_t) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000712 restoredState.fIndexCount;
713 }
714}
715
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000716bool GrInOrderDrawBuffer::needsNewState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000717 return fStates.empty() || !fStates.back().isEqual(this->getDrawState());
reed@google.comac10a2d2010-12-22 21:39:39 +0000718}
719
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000720bool GrInOrderDrawBuffer::needsNewClip() const {
bsalomon@google.com358e4272013-01-10 14:40:28 +0000721 GrAssert(fClips.count() == fClipOrigins.count());
722 if (this->getDrawState().isClipState()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000723 if (fClipSet &&
bsalomon@google.com358e4272013-01-10 14:40:28 +0000724 (fClips.empty() ||
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000725 fClips.back() != *this->getClip()->fClipStack ||
726 fClipOrigins.back() != this->getClip()->fOrigin)) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000727 return true;
728 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000729 }
730 return false;
731}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000732
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000733void GrInOrderDrawBuffer::recordClip() {
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000734 fClips.push_back() = *this->getClip()->fClipStack;
735 fClipOrigins.push_back() = this->getClip()->fOrigin;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000736 fClipSet = false;
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000737 fCmds.push_back(kSetClip_Cmd);
738}
739
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000740void GrInOrderDrawBuffer::recordState() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000741 fStates.push_back().saveFrom(this->getDrawState());
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000742 fCmds.push_back(kSetState_Cmd);
743}
744
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000745GrInOrderDrawBuffer::DrawRecord* GrInOrderDrawBuffer::recordDraw(const DrawInfo& info) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000746 fCmds.push_back(kDraw_Cmd);
747 return &fDraws.push_back(info);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000748}
749
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000750GrInOrderDrawBuffer::StencilPath* GrInOrderDrawBuffer::recordStencilPath() {
751 fCmds.push_back(kStencilPath_Cmd);
752 return &fStencilPaths.push_back();
753}
754
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000755GrInOrderDrawBuffer::Clear* GrInOrderDrawBuffer::recordClear() {
756 fCmds.push_back(kClear_Cmd);
757 return &fClears.push_back();
reed@google.comac10a2d2010-12-22 21:39:39 +0000758}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000759
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000760void GrInOrderDrawBuffer::clipWillBeSet(const GrClipData* newClipData) {
761 INHERITED::clipWillBeSet(newClipData);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000762 fClipSet = true;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000763 fClipProxyState = kUnknown_ClipProxyState;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000764}