blob: 6a503bbfd582ecc6af5d9bfd2b35e8ee2bb7e103 [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"
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000015#include "GrRenderTarget.h"
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000016#include "GrTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000017#include "GrTexture.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000018#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000019
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000020GrInOrderDrawBuffer::GrInOrderDrawBuffer(GrGpu* gpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +000021 GrVertexBufferAllocPool* vertexPool,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000022 GrIndexBufferAllocPool* indexPool)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000023 : GrDrawTarget(gpu->getContext())
24 , fDstGpu(gpu)
bsalomon@google.com97805382012-03-13 14:32:07 +000025 , fClipSet(true)
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000026 , fClipProxyState(kUnknown_ClipProxyState)
robertphillips@google.com69705572012-03-21 19:46:50 +000027 , fVertexPool(*vertexPool)
28 , fIndexPool(*indexPool)
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000029 , fFlushing(false)
30 , fDrawID(0) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +000031
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000032 fDstGpu->ref();
bsalomon@google.combcce8922013-03-25 15:38:39 +000033 fCaps.reset(SkRef(fDstGpu->caps()));
bsalomon@google.com18c9c192011-09-22 21:01:31 +000034
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000035 SkASSERT(NULL != vertexPool);
36 SkASSERT(NULL != indexPool);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000037
38 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
39 poolState.fUsedPoolVertexBytes = 0;
40 poolState.fUsedPoolIndexBytes = 0;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000041#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000042 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
43 poolState.fPoolStartVertex = ~0;
44 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
45 poolState.fPoolStartIndex = ~0;
46#endif
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000047 this->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000048}
49
50GrInOrderDrawBuffer::~GrInOrderDrawBuffer() {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000051 this->reset();
bsalomon@google.com4a018bb2011-10-28 19:50:21 +000052 // This must be called by before the GrDrawTarget destructor
53 this->releaseGeometry();
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000054 fDstGpu->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +000055}
56
bsalomon@google.com934c5702012-03-20 21:17:58 +000057////////////////////////////////////////////////////////////////////////////////
58
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000059namespace {
60void get_vertex_bounds(const void* vertices,
61 size_t vertexSize,
62 int vertexCount,
63 SkRect* bounds) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000064 SkASSERT(vertexSize >= sizeof(SkPoint));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000065 SkASSERT(vertexCount > 0);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000066 const SkPoint* point = static_cast<const SkPoint*>(vertices);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000067 bounds->fLeft = bounds->fRight = point->fX;
68 bounds->fTop = bounds->fBottom = point->fY;
69 for (int i = 1; i < vertexCount; ++i) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000070 point = reinterpret_cast<SkPoint*>(reinterpret_cast<intptr_t>(point) + vertexSize);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000071 bounds->growToInclude(point->fX, point->fY);
72 }
73}
bsalomon@google.com934c5702012-03-20 21:17:58 +000074}
75
robertphillips@google.com42903302013-04-20 12:26:07 +000076
77namespace {
78
79extern const GrVertexAttrib kRectPosColorUVAttribs[] = {
80 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000081 {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVertexAttribBinding},
82 {kVec2f_GrVertexAttribType, sizeof(SkPoint)+sizeof(GrColor),
robertphillips@google.com42903302013-04-20 12:26:07 +000083 kLocalCoord_GrVertexAttribBinding},
84};
85
86extern const GrVertexAttrib kRectPosUVAttribs[] = {
87 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000088 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +000089};
90
91static void set_vertex_attributes(GrDrawState* drawState,
92 bool hasColor, bool hasUVs,
93 int* colorOffset, int* localOffset) {
94 *colorOffset = -1;
95 *localOffset = -1;
96
97 // Using per-vertex colors allows batching across colors. (A lot of rects in a row differing
98 // only in color is a common occurrence in tables). However, having per-vertex colors disables
99 // blending optimizations because we don't know if the color will be solid or not. These
100 // optimizations help determine whether coverage and color can be blended correctly when
101 // dual-source blending isn't available. This comes into play when there is coverage. If colors
102 // were a stage it could take a hint that every vertex's color will be opaque.
103 if (hasColor && hasUVs) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000104 *colorOffset = sizeof(SkPoint);
105 *localOffset = sizeof(SkPoint) + sizeof(GrColor);
robertphillips@google.com42903302013-04-20 12:26:07 +0000106 drawState->setVertexAttribs<kRectPosColorUVAttribs>(3);
107 } else if (hasColor) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000108 *colorOffset = sizeof(SkPoint);
robertphillips@google.com42903302013-04-20 12:26:07 +0000109 drawState->setVertexAttribs<kRectPosColorUVAttribs>(2);
110 } else if (hasUVs) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000111 *localOffset = sizeof(SkPoint);
robertphillips@google.com42903302013-04-20 12:26:07 +0000112 drawState->setVertexAttribs<kRectPosUVAttribs>(2);
113 } else {
114 drawState->setVertexAttribs<kRectPosUVAttribs>(1);
115 }
116}
117
118};
119
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000120enum {
121 kTraceCmdBit = 0x80,
122 kCmdMask = 0x7f,
123};
124
125static uint8_t add_trace_bit(uint8_t cmd) {
126 return cmd | kTraceCmdBit;
127}
128
129static uint8_t strip_trace_bit(uint8_t cmd) {
130 return cmd & kCmdMask;
131}
132
133static bool cmd_has_trace_marker(uint8_t cmd) {
commit-bot@chromium.orge6984a82014-03-25 15:49:59 +0000134 return SkToBool(cmd & kTraceCmdBit);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000135}
136
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000137void GrInOrderDrawBuffer::onDrawRect(const SkRect& rect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000138 const SkMatrix* matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000139 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000140 const SkMatrix* localMatrix) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000141 GrDrawState::AutoColorRestore acr;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000142
143 GrDrawState* drawState = this->drawState();
144
145 GrColor color = drawState->getColor();
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000146
robertphillips@google.com42903302013-04-20 12:26:07 +0000147 int colorOffset, localOffset;
148 set_vertex_attributes(drawState,
149 this->caps()->dualSourceBlendingSupport() || drawState->hasSolidCoverage(),
150 NULL != localRect,
151 &colorOffset, &localOffset);
152 if (colorOffset >= 0) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000153 // We set the draw state's color to white here. This is done so that any batching performed
154 // in our subclass's onDraw() won't get a false from GrDrawState::op== due to a color
155 // mismatch. TODO: Once vertex layout is owned by GrDrawState it should skip comparing the
rmistry@google.comc9f3b382013-04-22 12:45:30 +0000156 // 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 +0000157 // this.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000158 acr.set(drawState, 0xFFFFFFFF);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000159 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000160
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000161 AutoReleaseGeometry geo(this, 4, 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000162 if (!geo.succeeded()) {
163 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com934c5702012-03-20 21:17:58 +0000164 return;
165 }
166
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000167 // Go to device coords to allow batching across matrix changes
168 SkMatrix combinedMatrix;
169 if (NULL != matrix) {
170 combinedMatrix = *matrix;
171 } else {
172 combinedMatrix.reset();
173 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000174 combinedMatrix.postConcat(drawState->getViewMatrix());
jvanverth@google.com39768252013-02-14 15:25:44 +0000175 // 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 +0000176 // modify that stage's matrix. Otherwise if the effect is generating its source rect from
177 // the vertex positions then we have to account for the view matrix change.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000178 GrDrawState::AutoViewMatrixRestore avmr;
179 if (!avmr.setIdentity(drawState)) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000180 return;
181 }
182
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000183 size_t vsize = drawState->getVertexSize();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000184
185 geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vsize);
186 combinedMatrix.mapPointsWithStride(geo.positions(), vsize, 4);
187
188 SkRect devBounds;
189 // since we already computed the dev verts, set the bounds hint. This will help us avoid
190 // unnecessary clipping in our onDraw().
191 get_vertex_bounds(geo.vertices(), vsize, 4, &devBounds);
192
bsalomon@google.comc7818882013-03-20 19:19:53 +0000193 if (localOffset >= 0) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000194 SkPoint* coords = GrTCast<SkPoint*>(GrTCast<intptr_t>(geo.vertices()) + localOffset);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000195 coords->setRectFan(localRect->fLeft, localRect->fTop,
196 localRect->fRight, localRect->fBottom,
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000197 vsize);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000198 if (NULL != localMatrix) {
199 localMatrix->mapPointsWithStride(coords, vsize, 4);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000200 }
201 }
202
203 if (colorOffset >= 0) {
204 GrColor* vertColor = GrTCast<GrColor*>(GrTCast<intptr_t>(geo.vertices()) + colorOffset);
205 for (int i = 0; i < 4; ++i) {
206 *vertColor = color;
207 vertColor = (GrColor*) ((intptr_t) vertColor + vsize);
208 }
209 }
210
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000211 this->setIndexSourceToBuffer(this->getContext()->getQuadIndexBuffer());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000212 this->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, 4, 6, &devBounds);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000213
214 // to ensure that stashing the drawState ptr is valid
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000215 SkASSERT(this->drawState() == drawState);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000216}
217
218bool GrInOrderDrawBuffer::quickInsideClip(const SkRect& devBounds) {
219 if (!this->getDrawState().isClipState()) {
220 return true;
221 }
222 if (kUnknown_ClipProxyState == fClipProxyState) {
223 SkIRect rect;
224 bool iior;
225 this->getClip()->getConservativeBounds(this->getDrawState().getRenderTarget(), &rect, &iior);
226 if (iior) {
227 // The clip is a rect. We will remember that in fProxyClip. It is common for an edge (or
228 // all edges) of the clip to be at the edge of the RT. However, we get that clipping for
229 // free via the viewport. We don't want to think that clipping must be enabled in this
230 // case. So we extend the clip outward from the edge to avoid these false negatives.
231 fClipProxyState = kValid_ClipProxyState;
reed@google.com44699382013-10-31 17:28:30 +0000232 fClipProxy = SkRect::Make(rect);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000233
234 if (fClipProxy.fLeft <= 0) {
235 fClipProxy.fLeft = SK_ScalarMin;
236 }
237 if (fClipProxy.fTop <= 0) {
238 fClipProxy.fTop = SK_ScalarMin;
239 }
240 if (fClipProxy.fRight >= this->getDrawState().getRenderTarget()->width()) {
241 fClipProxy.fRight = SK_ScalarMax;
242 }
243 if (fClipProxy.fBottom >= this->getDrawState().getRenderTarget()->height()) {
244 fClipProxy.fBottom = SK_ScalarMax;
245 }
246 } else {
247 fClipProxyState = kInvalid_ClipProxyState;
248 }
249 }
250 if (kValid_ClipProxyState == fClipProxyState) {
251 return fClipProxy.contains(devBounds);
252 }
253 SkPoint originOffset = {SkIntToScalar(this->getClip()->fOrigin.fX),
254 SkIntToScalar(this->getClip()->fOrigin.fY)};
255 SkRect clipSpaceBounds = devBounds;
256 clipSpaceBounds.offset(originOffset);
257 return this->getClip()->fClipStack->quickContains(clipSpaceBounds);
258}
259
260int GrInOrderDrawBuffer::concatInstancedDraw(const DrawInfo& info) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000261 SkASSERT(info.isInstanced());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000262
bsalomon@google.com934c5702012-03-20 21:17:58 +0000263 const GeometrySrcState& geomSrc = this->getGeomSrc();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000264 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000265
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000266 // we only attempt to concat the case when reserved verts are used with a client-specified index
267 // buffer. To make this work with client-specified VBs we'd need to know if the VB was updated
268 // between draws.
269 if (kReserved_GeometrySrcType != geomSrc.fVertexSrc ||
270 kBuffer_GeometrySrcType != geomSrc.fIndexSrc) {
271 return 0;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000272 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000273 // Check if there is a draw info that is compatible that uses the same VB from the pool and
274 // the same IB
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000275 if (kDraw_Cmd != strip_trace_bit(fCmds.back())) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000276 return 0;
277 }
278
279 DrawRecord* draw = &fDraws.back();
280 GeometryPoolState& poolState = fGeoPoolStateStack.back();
281 const GrVertexBuffer* vertexBuffer = poolState.fPoolVertexBuffer;
282
283 if (!draw->isInstanced() ||
284 draw->verticesPerInstance() != info.verticesPerInstance() ||
285 draw->indicesPerInstance() != info.indicesPerInstance() ||
286 draw->fVertexBuffer != vertexBuffer ||
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000287 draw->fIndexBuffer != geomSrc.fIndexBuffer) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000288 return 0;
289 }
290 // info does not yet account for the offset from the start of the pool's VB while the previous
291 // draw record does.
292 int adjustedStartVertex = poolState.fPoolStartVertex + info.startVertex();
293 if (draw->startVertex() + draw->vertexCount() != adjustedStartVertex) {
294 return 0;
295 }
296
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000297 SkASSERT(poolState.fPoolStartVertex == draw->startVertex() + draw->vertexCount());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000298
299 // how many instances can be concat'ed onto draw given the size of the index buffer
300 int instancesToConcat = this->indexCountInCurrentSource() / info.indicesPerInstance();
301 instancesToConcat -= draw->instanceCount();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000302 instancesToConcat = SkTMin(instancesToConcat, info.instanceCount());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000303
304 // update the amount of reserved vertex data actually referenced in draws
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000305 size_t vertexBytes = instancesToConcat * info.verticesPerInstance() *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000306 drawState.getVertexSize();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000307 poolState.fUsedPoolVertexBytes = SkTMax(poolState.fUsedPoolVertexBytes, vertexBytes);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000308
309 draw->adjustInstanceCount(instancesToConcat);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000310
311 // update last fGpuCmdMarkers to include any additional trace markers that have been added
312 if (this->getActiveTraceMarkers().count() > 0) {
313 if (cmd_has_trace_marker(fCmds.back())) {
314 fGpuCmdMarkers.back().addSet(this->getActiveTraceMarkers());
315 } else {
316 fGpuCmdMarkers.push_back(this->getActiveTraceMarkers());
317 fCmds.back() = add_trace_bit(fCmds.back());
318 }
319 }
320
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000321 return instancesToConcat;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000322}
323
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000324class AutoClipReenable {
325public:
326 AutoClipReenable() : fDrawState(NULL) {}
327 ~AutoClipReenable() {
328 if (NULL != fDrawState) {
329 fDrawState->enableState(GrDrawState::kClip_StateBit);
330 }
331 }
332 void set(GrDrawState* drawState) {
333 if (drawState->isClipState()) {
334 fDrawState = drawState;
335 drawState->disableState(GrDrawState::kClip_StateBit);
336 }
337 }
338private:
339 GrDrawState* fDrawState;
340};
341
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000342void GrInOrderDrawBuffer::onDraw(const DrawInfo& info) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000343
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000344 GeometryPoolState& poolState = fGeoPoolStateStack.back();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000345 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000346 AutoClipReenable acr;
347
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000348 if (drawState.isClipState() &&
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000349 NULL != info.getDevBounds() &&
350 this->quickInsideClip(*info.getDevBounds())) {
351 acr.set(this->drawState());
352 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000353
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000354 if (this->needsNewClip()) {
355 this->recordClip();
356 }
357 if (this->needsNewState()) {
358 this->recordState();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000359 }
360
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000361 DrawRecord* draw;
362 if (info.isInstanced()) {
363 int instancesConcated = this->concatInstancedDraw(info);
364 if (info.instanceCount() > instancesConcated) {
365 draw = this->recordDraw(info);
366 draw->adjustInstanceCount(-instancesConcated);
367 } else {
368 return;
369 }
370 } else {
371 draw = this->recordDraw(info);
372 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000373
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000374 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000375 case kBuffer_GeometrySrcType:
376 draw->fVertexBuffer = this->getGeomSrc().fVertexBuffer;
377 break;
378 case kReserved_GeometrySrcType: // fallthrough
379 case kArray_GeometrySrcType: {
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000380 size_t vertexBytes = (info.vertexCount() + info.startVertex()) *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000381 drawState.getVertexSize();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000382 poolState.fUsedPoolVertexBytes = SkTMax(poolState.fUsedPoolVertexBytes, vertexBytes);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000383 draw->fVertexBuffer = poolState.fPoolVertexBuffer;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000384 draw->adjustStartVertex(poolState.fPoolStartVertex);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000385 break;
386 }
387 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000388 SkFAIL("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000389 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000390 draw->fVertexBuffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000391
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000392 if (info.isIndexed()) {
393 switch (this->getGeomSrc().fIndexSrc) {
394 case kBuffer_GeometrySrcType:
395 draw->fIndexBuffer = this->getGeomSrc().fIndexBuffer;
396 break;
397 case kReserved_GeometrySrcType: // fallthrough
398 case kArray_GeometrySrcType: {
399 size_t indexBytes = (info.indexCount() + info.startIndex()) * sizeof(uint16_t);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000400 poolState.fUsedPoolIndexBytes = SkTMax(poolState.fUsedPoolIndexBytes, indexBytes);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000401 draw->fIndexBuffer = poolState.fPoolIndexBuffer;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000402 draw->adjustStartIndex(poolState.fPoolStartIndex);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000403 break;
404 }
405 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000406 SkFAIL("unknown geom src type");
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000407 }
408 draw->fIndexBuffer->ref();
409 } else {
410 draw->fIndexBuffer = NULL;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000411 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000412}
413
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000414GrInOrderDrawBuffer::StencilPath::StencilPath() {}
415GrInOrderDrawBuffer::DrawPath::DrawPath() {}
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000416GrInOrderDrawBuffer::DrawPaths::DrawPaths() {}
417GrInOrderDrawBuffer::DrawPaths::~DrawPaths() {
418 if (fTransforms) {
419 SkDELETE_ARRAY(fTransforms);
420 }
commit-bot@chromium.orgecc45362014-03-28 21:31:34 +0000421 for (int i = 0; i < fPathCount; ++i) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000422 fPaths[i]->unref();
423 }
424 SkDELETE_ARRAY(fPaths);
425}
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000426
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000427void GrInOrderDrawBuffer::onStencilPath(const GrPath* path, SkPath::FillType fill) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000428 if (this->needsNewClip()) {
429 this->recordClip();
430 }
431 // Only compare the subset of GrDrawState relevant to path stenciling?
432 if (this->needsNewState()) {
433 this->recordState();
434 }
435 StencilPath* sp = this->recordStencilPath();
436 sp->fPath.reset(path);
437 path->ref();
438 sp->fFill = fill;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000439}
440
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000441void GrInOrderDrawBuffer::onDrawPath(const GrPath* path,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000442 SkPath::FillType fill, const GrDeviceCoordTexture* dstCopy) {
443 if (this->needsNewClip()) {
444 this->recordClip();
445 }
446 // TODO: Only compare the subset of GrDrawState relevant to path covering?
447 if (this->needsNewState()) {
448 this->recordState();
449 }
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000450 DrawPath* cp = this->recordDrawPath();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000451 cp->fPath.reset(path);
452 path->ref();
453 cp->fFill = fill;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000454 if (NULL != dstCopy) {
455 cp->fDstCopy = *dstCopy;
456 }
457}
458
commit-bot@chromium.orgecc45362014-03-28 21:31:34 +0000459void GrInOrderDrawBuffer::onDrawPaths(int pathCount, const GrPath** paths,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000460 const SkMatrix* transforms,
461 SkPath::FillType fill,
462 SkStrokeRec::Style stroke,
463 const GrDeviceCoordTexture* dstCopy) {
464 SkASSERT(pathCount);
465
466 if (this->needsNewClip()) {
467 this->recordClip();
468 }
469 if (this->needsNewState()) {
470 this->recordState();
471 }
472 DrawPaths* dp = this->recordDrawPaths();
473 dp->fPathCount = pathCount;
474 dp->fPaths = SkNEW_ARRAY(const GrPath*, pathCount);
475 memcpy(dp->fPaths, paths, sizeof(GrPath*) * pathCount);
commit-bot@chromium.orgecc45362014-03-28 21:31:34 +0000476 for (int i = 0; i < pathCount; ++i) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000477 dp->fPaths[i]->ref();
478 }
479
480 dp->fTransforms = SkNEW_ARRAY(SkMatrix, pathCount);
481 memcpy(dp->fTransforms, transforms, sizeof(SkMatrix) * pathCount);
482
483 dp->fFill = fill;
484 dp->fStroke = stroke;
485
486 if (NULL != dstCopy) {
487 dp->fDstCopy = *dstCopy;
488 }
489}
490
skia.committer@gmail.com18bb41e2013-11-01 07:02:15 +0000491void GrInOrderDrawBuffer::clear(const SkIRect* rect, GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000492 bool canIgnoreRect, GrRenderTarget* renderTarget) {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000493 SkIRect r;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000494 if (NULL == renderTarget) {
495 renderTarget = this->drawState()->getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000496 SkASSERT(NULL != renderTarget);
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000497 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000498 if (NULL == rect) {
499 // We could do something smart and remove previous draws and clears to
500 // the current render target. If we get that smart we have to make sure
501 // those draws aren't read before this clear (render-to-texture).
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000502 r.setLTRB(0, 0, renderTarget->width(), renderTarget->height());
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000503 rect = &r;
504 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000505 Clear* clr = this->recordClear();
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000506 GrColorIsPMAssert(color);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000507 clr->fColor = color;
508 clr->fRect = *rect;
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000509 clr->fCanIgnoreRect = canIgnoreRect;
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000510 clr->fRenderTarget = renderTarget;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000511 renderTarget->ref();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000512}
513
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000514void GrInOrderDrawBuffer::discard(GrRenderTarget* renderTarget) {
515 if (!this->caps()->discardRenderTargetSupport()) {
516 return;
517 }
518 if (NULL == renderTarget) {
519 renderTarget = this->drawState()->getRenderTarget();
520 SkASSERT(NULL != renderTarget);
521 }
522 Clear* clr = this->recordClear();
523 clr->fColor = GrColor_ILLEGAL;
524 clr->fRenderTarget = renderTarget;
525 renderTarget->ref();
526}
527
reed@google.comac10a2d2010-12-22 21:39:39 +0000528void GrInOrderDrawBuffer::reset() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000529 SkASSERT(1 == fGeoPoolStateStack.count());
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000530 this->resetVertexSource();
531 this->resetIndexSource();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000532 int numDraws = fDraws.count();
533 for (int d = 0; d < numDraws; ++d) {
534 // we always have a VB, but not always an IB
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000535 SkASSERT(NULL != fDraws[d].fVertexBuffer);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000536 fDraws[d].fVertexBuffer->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000537 SkSafeUnref(fDraws[d].fIndexBuffer);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000538 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000539 fCmds.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000540 fDraws.reset();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000541 fStencilPaths.reset();
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000542 fDrawPath.reset();
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000543 fDrawPaths.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000544 fStates.reset();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000545 fClears.reset();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000546 fVertexPool.reset();
547 fIndexPool.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000548 fClips.reset();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000549 fClipOrigins.reset();
bsalomon@google.com116ad842013-04-09 15:38:19 +0000550 fCopySurfaces.reset();
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000551 fGpuCmdMarkers.reset();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000552 fClipSet = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000553}
554
robertphillips@google.com1267fbd2013-07-03 18:37:27 +0000555void GrInOrderDrawBuffer::flush() {
556 if (fFlushing) {
557 return;
558 }
559
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000560 SkASSERT(kReserved_GeometrySrcType != this->getGeomSrc().fVertexSrc);
561 SkASSERT(kReserved_GeometrySrcType != this->getGeomSrc().fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000562
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000563 int numCmds = fCmds.count();
bsalomon@google.com358e4272013-01-10 14:40:28 +0000564 if (0 == numCmds) {
robertphillips@google.com1267fbd2013-07-03 18:37:27 +0000565 return;
reed@google.comac10a2d2010-12-22 21:39:39 +0000566 }
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000567
568 GrAutoTRestore<bool> flushRestore(&fFlushing);
569 fFlushing = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000570
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000571 fVertexPool.unmap();
572 fIndexPool.unmap();
reed@google.comac10a2d2010-12-22 21:39:39 +0000573
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000574 GrDrawTarget::AutoClipRestore acr(fDstGpu);
575 AutoGeometryAndStatePush agasp(fDstGpu, kPreserve_ASRInit);
bsalomon@google.comca432082013-01-23 19:53:46 +0000576
577 GrDrawState playbackState;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000578 GrDrawState* prevDrawState = fDstGpu->drawState();
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000579 prevDrawState->ref();
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000580 fDstGpu->setDrawState(&playbackState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000581
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000582 GrClipData clipData;
583
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000584 int currState = 0;
585 int currClip = 0;
586 int currClear = 0;
587 int currDraw = 0;
588 int currStencilPath = 0;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000589 int currDrawPath = 0;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000590 int currDrawPaths = 0;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000591 int currCopySurface = 0;
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000592 int currCmdMarker = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000593
egdaniel3eee3832014-06-18 13:09:11 -0700594 fDstGpu->saveActiveTraceMarkers();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000595 for (int c = 0; c < numCmds; ++c) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000596 GrGpuTraceMarker newMarker("", -1);
597 if (cmd_has_trace_marker(fCmds[c])) {
598 SkString traceString = fGpuCmdMarkers[currCmdMarker].toString();
599 newMarker.fMarker = traceString.c_str();
600 fDstGpu->addGpuTraceMarker(&newMarker);
601 ++currCmdMarker;
602 }
603 switch (strip_trace_bit(fCmds[c])) {
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000604 case kDraw_Cmd: {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000605 const DrawRecord& draw = fDraws[currDraw];
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000606 fDstGpu->setVertexSourceToBuffer(draw.fVertexBuffer);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000607 if (draw.isIndexed()) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000608 fDstGpu->setIndexSourceToBuffer(draw.fIndexBuffer);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000609 }
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000610 fDstGpu->executeDraw(draw);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000611 ++currDraw;
612 break;
613 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000614 case kStencilPath_Cmd: {
615 const StencilPath& sp = fStencilPaths[currStencilPath];
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000616 fDstGpu->stencilPath(sp.fPath.get(), sp.fFill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000617 ++currStencilPath;
618 break;
619 }
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000620 case kDrawPath_Cmd: {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000621 const DrawPath& cp = fDrawPath[currDrawPath];
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000622 fDstGpu->executeDrawPath(cp.fPath.get(), cp.fFill,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000623 NULL != cp.fDstCopy.texture() ? &cp.fDstCopy : NULL);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000624 ++currDrawPath;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000625 break;
626 }
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000627 case kDrawPaths_Cmd: {
628 DrawPaths& dp = fDrawPaths[currDrawPaths];
629 const GrDeviceCoordTexture* dstCopy =
630 NULL != dp.fDstCopy.texture() ? &dp.fDstCopy : NULL;
631 fDstGpu->executeDrawPaths(dp.fPathCount, dp.fPaths,
632 dp.fTransforms, dp.fFill, dp.fStroke,
633 dstCopy);
634 ++currDrawPaths;
635 break;
636 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000637 case kSetState_Cmd:
bsalomon@google.comca432082013-01-23 19:53:46 +0000638 fStates[currState].restoreTo(&playbackState);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000639 ++currState;
640 break;
641 case kSetClip_Cmd:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000642 clipData.fClipStack = &fClips[currClip];
643 clipData.fOrigin = fClipOrigins[currClip];
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000644 fDstGpu->setClip(&clipData);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000645 ++currClip;
646 break;
647 case kClear_Cmd:
bsalomon@google.comefa85aa2014-03-31 18:03:06 +0000648 if (GrColor_ILLEGAL == fClears[currClear].fColor) {
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000649 fDstGpu->discard(fClears[currClear].fRenderTarget);
650 } else {
651 fDstGpu->clear(&fClears[currClear].fRect,
652 fClears[currClear].fColor,
653 fClears[currClear].fCanIgnoreRect,
654 fClears[currClear].fRenderTarget);
655 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000656 ++currClear;
657 break;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000658 case kCopySurface_Cmd:
659 fDstGpu->copySurface(fCopySurfaces[currCopySurface].fDst.get(),
660 fCopySurfaces[currCopySurface].fSrc.get(),
661 fCopySurfaces[currCopySurface].fSrcRect,
662 fCopySurfaces[currCopySurface].fDstPoint);
663 ++currCopySurface;
664 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000665 }
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000666 if (cmd_has_trace_marker(fCmds[c])) {
667 fDstGpu->removeGpuTraceMarker(&newMarker);
668 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000669 }
egdaniel3eee3832014-06-18 13:09:11 -0700670 fDstGpu->restoreActiveTraceMarkers();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000671 // we should have consumed all the states, clips, etc.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000672 SkASSERT(fStates.count() == currState);
673 SkASSERT(fClips.count() == currClip);
674 SkASSERT(fClipOrigins.count() == currClip);
675 SkASSERT(fClears.count() == currClear);
676 SkASSERT(fDraws.count() == currDraw);
677 SkASSERT(fCopySurfaces.count() == currCopySurface);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000678 SkASSERT(fGpuCmdMarkers.count() == currCmdMarker);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000679
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000680 fDstGpu->setDrawState(prevDrawState);
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000681 prevDrawState->unref();
bsalomon@google.com55e4a202013-01-11 13:54:21 +0000682 this->reset();
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000683 ++fDrawID;
reed@google.comac10a2d2010-12-22 21:39:39 +0000684}
685
bsalomon@google.com116ad842013-04-09 15:38:19 +0000686bool GrInOrderDrawBuffer::onCopySurface(GrSurface* dst,
687 GrSurface* src,
688 const SkIRect& srcRect,
689 const SkIPoint& dstPoint) {
690 if (fDstGpu->canCopySurface(dst, src, srcRect, dstPoint)) {
691 CopySurface* cs = this->recordCopySurface();
692 cs->fDst.reset(SkRef(dst));
693 cs->fSrc.reset(SkRef(src));
694 cs->fSrcRect = srcRect;
695 cs->fDstPoint = dstPoint;
696 return true;
697 } else {
698 return false;
699 }
700}
701
702bool GrInOrderDrawBuffer::onCanCopySurface(GrSurface* dst,
703 GrSurface* src,
704 const SkIRect& srcRect,
705 const SkIPoint& dstPoint) {
706 return fDstGpu->canCopySurface(dst, src, srcRect, dstPoint);
707}
708
bsalomon@google.comeb851172013-04-15 13:51:00 +0000709void GrInOrderDrawBuffer::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
710 fDstGpu->initCopySurfaceDstDesc(src, desc);
711}
712
robertphillips@google.com9528bdb2013-09-18 22:33:57 +0000713void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace(int vertexCount,
714 int indexCount) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000715 // We use geometryHints() to know whether to flush the draw buffer. We
716 // can't flush if we are inside an unbalanced pushGeometrySource.
717 // Moreover, flushing blows away vertex and index data that was
718 // previously reserved. So if the vertex or index data is pulled from
719 // reserved space and won't be released by this request then we can't
720 // flush.
721 bool insideGeoPush = fGeoPoolStateStack.count() > 1;
bsalomon@google.com97805382012-03-13 14:32:07 +0000722
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000723 bool unreleasedVertexSpace =
724 !vertexCount &&
725 kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc;
bsalomon@google.com97805382012-03-13 14:32:07 +0000726
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000727 bool unreleasedIndexSpace =
728 !indexCount &&
729 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
bsalomon@google.com97805382012-03-13 14:32:07 +0000730
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000731 // we don't want to finalize any reserved geom on the target since
732 // we don't know that the client has finished writing to it.
733 bool targetHasReservedGeom = fDstGpu->hasReservedVerticesOrIndices();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000734
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000735 int vcount = vertexCount;
736 int icount = indexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000737
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000738 if (!insideGeoPush &&
739 !unreleasedVertexSpace &&
740 !unreleasedIndexSpace &&
741 !targetHasReservedGeom &&
742 this->geometryHints(&vcount, &icount)) {
bsalomon@google.com97805382012-03-13 14:32:07 +0000743
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000744 this->flush();
bsalomon@google.com97805382012-03-13 14:32:07 +0000745 }
746}
747
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000748bool GrInOrderDrawBuffer::geometryHints(int* vertexCount,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000749 int* indexCount) const {
750 // we will recommend a flush if the data could fit in a single
751 // preallocated buffer but none are left and it can't fit
752 // in the current buffer (which may not be prealloced).
reed@google.comac10a2d2010-12-22 21:39:39 +0000753 bool flush = false;
754 if (NULL != indexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000755 int32_t currIndices = fIndexPool.currentBufferIndices();
756 if (*indexCount > currIndices &&
757 (!fIndexPool.preallocatedBuffersRemaining() &&
758 *indexCount <= fIndexPool.preallocatedBufferIndices())) {
759
760 flush = true;
761 }
762 *indexCount = currIndices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000763 }
764 if (NULL != vertexCount) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000765 size_t vertexSize = this->getDrawState().getVertexSize();
jvanverth@google.coma6338982013-01-31 21:34:25 +0000766 int32_t currVertices = fVertexPool.currentBufferVertices(vertexSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000767 if (*vertexCount > currVertices &&
768 (!fVertexPool.preallocatedBuffersRemaining() &&
jvanverth@google.coma6338982013-01-31 21:34:25 +0000769 *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexSize))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000770
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000771 flush = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000772 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000773 *vertexCount = currVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000774 }
775 return flush;
776}
777
jvanverth@google.coma6338982013-01-31 21:34:25 +0000778bool GrInOrderDrawBuffer::onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000779 int vertexCount,
780 void** vertices) {
781 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000782 SkASSERT(vertexCount > 0);
783 SkASSERT(NULL != vertices);
784 SkASSERT(0 == poolState.fUsedPoolVertexBytes);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000785
jvanverth@google.coma6338982013-01-31 21:34:25 +0000786 *vertices = fVertexPool.makeSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000787 vertexCount,
788 &poolState.fPoolVertexBuffer,
789 &poolState.fPoolStartVertex);
790 return NULL != *vertices;
791}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000792
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000793bool GrInOrderDrawBuffer::onReserveIndexSpace(int indexCount, void** indices) {
794 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000795 SkASSERT(indexCount > 0);
796 SkASSERT(NULL != indices);
797 SkASSERT(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000798
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000799 *indices = fIndexPool.makeSpace(indexCount,
800 &poolState.fPoolIndexBuffer,
801 &poolState.fPoolStartIndex);
802 return NULL != *indices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000803}
804
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000805void GrInOrderDrawBuffer::releaseReservedVertexSpace() {
806 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000807 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000808
809 // If we get a release vertex space call then our current source should either be reserved
810 // or array (which we copied into reserved space).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000811 SkASSERT(kReserved_GeometrySrcType == geoSrc.fVertexSrc ||
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000812 kArray_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000813
814 // When the caller reserved vertex buffer space we gave it back a pointer
815 // provided by the vertex buffer pool. At each draw we tracked the largest
816 // offset into the pool's pointer that was referenced. Now we return to the
817 // pool any portion at the tail of the allocation that no draw referenced.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000818 size_t reservedVertexBytes = geoSrc.fVertexSize * geoSrc.fVertexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000819 fVertexPool.putBack(reservedVertexBytes -
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000820 poolState.fUsedPoolVertexBytes);
821 poolState.fUsedPoolVertexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000822 poolState.fPoolVertexBuffer = NULL;
823 poolState.fPoolStartVertex = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000824}
825
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000826void GrInOrderDrawBuffer::releaseReservedIndexSpace() {
827 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000828 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000829
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000830 // If we get a release index space call then our current source should either be reserved
831 // or array (which we copied into reserved space).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000832 SkASSERT(kReserved_GeometrySrcType == geoSrc.fIndexSrc ||
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000833 kArray_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000834
835 // Similar to releaseReservedVertexSpace we return any unused portion at
836 // the tail
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000837 size_t reservedIndexBytes = sizeof(uint16_t) * geoSrc.fIndexCount;
838 fIndexPool.putBack(reservedIndexBytes - poolState.fUsedPoolIndexBytes);
839 poolState.fUsedPoolIndexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000840 poolState.fPoolIndexBuffer = NULL;
841 poolState.fPoolStartIndex = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000842}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000843
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000844void GrInOrderDrawBuffer::onSetVertexSourceToArray(const void* vertexArray,
845 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000846
847 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000848 SkASSERT(0 == poolState.fUsedPoolVertexBytes);
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000849#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000850 bool success =
851#endif
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000852 fVertexPool.appendVertices(this->getVertexSize(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000853 vertexCount,
854 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000855 &poolState.fPoolVertexBuffer,
856 &poolState.fPoolStartVertex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000857 GR_DEBUGASSERT(success);
858}
859
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000860void GrInOrderDrawBuffer::onSetIndexSourceToArray(const void* indexArray,
861 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000862 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000863 SkASSERT(0 == poolState.fUsedPoolIndexBytes);
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000864#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000865 bool success =
866#endif
867 fIndexPool.appendIndices(indexCount,
868 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000869 &poolState.fPoolIndexBuffer,
870 &poolState.fPoolStartIndex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000871 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000872}
873
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000874void GrInOrderDrawBuffer::releaseVertexArray() {
875 // When the client provides an array as the vertex source we handled it
876 // by copying their array into reserved space.
877 this->GrInOrderDrawBuffer::releaseReservedVertexSpace();
878}
879
880void GrInOrderDrawBuffer::releaseIndexArray() {
881 // When the client provides an array as the index source we handled it
882 // by copying their array into reserved space.
883 this->GrInOrderDrawBuffer::releaseReservedIndexSpace();
884}
885
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000886void GrInOrderDrawBuffer::geometrySourceWillPush() {
887 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
888 poolState.fUsedPoolVertexBytes = 0;
889 poolState.fUsedPoolIndexBytes = 0;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000890#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000891 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
892 poolState.fPoolStartVertex = ~0;
893 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
894 poolState.fPoolStartIndex = ~0;
895#endif
896}
897
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000898void GrInOrderDrawBuffer::geometrySourceWillPop(
899 const GeometrySrcState& restoredState) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000900 SkASSERT(fGeoPoolStateStack.count() > 1);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000901 fGeoPoolStateStack.pop_back();
902 GeometryPoolState& poolState = fGeoPoolStateStack.back();
903 // we have to assume that any slack we had in our vertex/index data
904 // is now unreleasable because data may have been appended later in the
905 // pool.
906 if (kReserved_GeometrySrcType == restoredState.fVertexSrc ||
907 kArray_GeometrySrcType == restoredState.fVertexSrc) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000908 poolState.fUsedPoolVertexBytes = restoredState.fVertexSize * restoredState.fVertexCount;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000909 }
910 if (kReserved_GeometrySrcType == restoredState.fIndexSrc ||
911 kArray_GeometrySrcType == restoredState.fIndexSrc) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000912 poolState.fUsedPoolIndexBytes = sizeof(uint16_t) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000913 restoredState.fIndexCount;
914 }
915}
916
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000917bool GrInOrderDrawBuffer::needsNewState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000918 return fStates.empty() || !fStates.back().isEqual(this->getDrawState());
reed@google.comac10a2d2010-12-22 21:39:39 +0000919}
920
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000921bool GrInOrderDrawBuffer::needsNewClip() const {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000922 SkASSERT(fClips.count() == fClipOrigins.count());
bsalomon@google.com358e4272013-01-10 14:40:28 +0000923 if (this->getDrawState().isClipState()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000924 if (fClipSet &&
bsalomon@google.com358e4272013-01-10 14:40:28 +0000925 (fClips.empty() ||
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000926 fClips.back() != *this->getClip()->fClipStack ||
927 fClipOrigins.back() != this->getClip()->fOrigin)) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000928 return true;
929 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000930 }
931 return false;
932}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000933
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000934void GrInOrderDrawBuffer::addToCmdBuffer(uint8_t cmd) {
935 SkASSERT(!cmd_has_trace_marker(cmd));
936 const GrTraceMarkerSet& activeTraceMarkers = this->getActiveTraceMarkers();
937 if (activeTraceMarkers.count() > 0) {
938 fCmds.push_back(add_trace_bit(cmd));
939 fGpuCmdMarkers.push_back(activeTraceMarkers);
940 } else {
941 fCmds.push_back(cmd);
942 }
943}
944
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000945void GrInOrderDrawBuffer::recordClip() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000946 fClips.push_back(*this->getClip()->fClipStack);
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000947 fClipOrigins.push_back() = this->getClip()->fOrigin;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000948 fClipSet = false;
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000949 this->addToCmdBuffer(kSetClip_Cmd);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000950}
951
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000952void GrInOrderDrawBuffer::recordState() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000953 fStates.push_back().saveFrom(this->getDrawState());
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000954 this->addToCmdBuffer(kSetState_Cmd);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000955}
956
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000957GrInOrderDrawBuffer::DrawRecord* GrInOrderDrawBuffer::recordDraw(const DrawInfo& info) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000958 this->addToCmdBuffer(kDraw_Cmd);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000959 return &fDraws.push_back(info);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000960}
961
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000962GrInOrderDrawBuffer::StencilPath* GrInOrderDrawBuffer::recordStencilPath() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000963 this->addToCmdBuffer(kStencilPath_Cmd);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000964 return &fStencilPaths.push_back();
965}
966
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000967GrInOrderDrawBuffer::DrawPath* GrInOrderDrawBuffer::recordDrawPath() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000968 this->addToCmdBuffer(kDrawPath_Cmd);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000969 return &fDrawPath.push_back();
970}
971
972GrInOrderDrawBuffer::DrawPaths* GrInOrderDrawBuffer::recordDrawPaths() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000973 this->addToCmdBuffer(kDrawPaths_Cmd);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000974 return &fDrawPaths.push_back();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000975}
976
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000977GrInOrderDrawBuffer::Clear* GrInOrderDrawBuffer::recordClear() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000978 this->addToCmdBuffer(kClear_Cmd);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000979 return &fClears.push_back();
reed@google.comac10a2d2010-12-22 21:39:39 +0000980}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000981
bsalomon@google.com116ad842013-04-09 15:38:19 +0000982GrInOrderDrawBuffer::CopySurface* GrInOrderDrawBuffer::recordCopySurface() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000983 this->addToCmdBuffer(kCopySurface_Cmd);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000984 return &fCopySurfaces.push_back();
985}
986
987
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000988void GrInOrderDrawBuffer::clipWillBeSet(const GrClipData* newClipData) {
989 INHERITED::clipWillBeSet(newClipData);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000990 fClipSet = true;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000991 fClipProxyState = kUnknown_ClipProxyState;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000992}