blob: 26b54522b52abae4fc9142446726d9871ac6431e [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"
jvanverth8e80d172014-06-19 12:01:10 -070012#include "GrTextStrike.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000013#include "GrGpu.h"
14#include "GrIndexBuffer.h"
15#include "GrPath.h"
cdaltonb85a0aa2014-07-21 15:32:44 -070016#include "GrPathRange.h"
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000017#include "GrRenderTarget.h"
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000018#include "GrTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000019#include "GrTexture.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000020#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000021
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000022GrInOrderDrawBuffer::GrInOrderDrawBuffer(GrGpu* gpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +000023 GrVertexBufferAllocPool* vertexPool,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000024 GrIndexBufferAllocPool* indexPool)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000025 : GrDrawTarget(gpu->getContext())
26 , fDstGpu(gpu)
bsalomon@google.com97805382012-03-13 14:32:07 +000027 , fClipSet(true)
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000028 , fClipProxyState(kUnknown_ClipProxyState)
robertphillips@google.com69705572012-03-21 19:46:50 +000029 , fVertexPool(*vertexPool)
30 , fIndexPool(*indexPool)
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000031 , fFlushing(false)
32 , fDrawID(0) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +000033
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000034 fDstGpu->ref();
bsalomon@google.combcce8922013-03-25 15:38:39 +000035 fCaps.reset(SkRef(fDstGpu->caps()));
bsalomon@google.com18c9c192011-09-22 21:01:31 +000036
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000037 SkASSERT(NULL != vertexPool);
38 SkASSERT(NULL != indexPool);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000039
40 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
41 poolState.fUsedPoolVertexBytes = 0;
42 poolState.fUsedPoolIndexBytes = 0;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000043#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000044 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
45 poolState.fPoolStartVertex = ~0;
46 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
47 poolState.fPoolStartIndex = ~0;
48#endif
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000049 this->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000050}
51
52GrInOrderDrawBuffer::~GrInOrderDrawBuffer() {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000053 this->reset();
bsalomon@google.com4a018bb2011-10-28 19:50:21 +000054 // This must be called by before the GrDrawTarget destructor
55 this->releaseGeometry();
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000056 fDstGpu->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +000057}
58
bsalomon@google.com934c5702012-03-20 21:17:58 +000059////////////////////////////////////////////////////////////////////////////////
60
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000061namespace {
62void get_vertex_bounds(const void* vertices,
63 size_t vertexSize,
64 int vertexCount,
65 SkRect* bounds) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000066 SkASSERT(vertexSize >= sizeof(SkPoint));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000067 SkASSERT(vertexCount > 0);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000068 const SkPoint* point = static_cast<const SkPoint*>(vertices);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000069 bounds->fLeft = bounds->fRight = point->fX;
70 bounds->fTop = bounds->fBottom = point->fY;
71 for (int i = 1; i < vertexCount; ++i) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000072 point = reinterpret_cast<SkPoint*>(reinterpret_cast<intptr_t>(point) + vertexSize);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000073 bounds->growToInclude(point->fX, point->fY);
74 }
75}
bsalomon@google.com934c5702012-03-20 21:17:58 +000076}
77
robertphillips@google.com42903302013-04-20 12:26:07 +000078
79namespace {
80
81extern const GrVertexAttrib kRectPosColorUVAttribs[] = {
82 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000083 {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVertexAttribBinding},
84 {kVec2f_GrVertexAttribType, sizeof(SkPoint)+sizeof(GrColor),
robertphillips@google.com42903302013-04-20 12:26:07 +000085 kLocalCoord_GrVertexAttribBinding},
86};
87
88extern const GrVertexAttrib kRectPosUVAttribs[] = {
89 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000090 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +000091};
92
93static void set_vertex_attributes(GrDrawState* drawState,
94 bool hasColor, bool hasUVs,
95 int* colorOffset, int* localOffset) {
96 *colorOffset = -1;
97 *localOffset = -1;
98
99 // Using per-vertex colors allows batching across colors. (A lot of rects in a row differing
100 // only in color is a common occurrence in tables). However, having per-vertex colors disables
101 // blending optimizations because we don't know if the color will be solid or not. These
102 // optimizations help determine whether coverage and color can be blended correctly when
103 // dual-source blending isn't available. This comes into play when there is coverage. If colors
104 // were a stage it could take a hint that every vertex's color will be opaque.
105 if (hasColor && hasUVs) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000106 *colorOffset = sizeof(SkPoint);
107 *localOffset = sizeof(SkPoint) + sizeof(GrColor);
robertphillips@google.com42903302013-04-20 12:26:07 +0000108 drawState->setVertexAttribs<kRectPosColorUVAttribs>(3);
109 } else if (hasColor) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000110 *colorOffset = sizeof(SkPoint);
robertphillips@google.com42903302013-04-20 12:26:07 +0000111 drawState->setVertexAttribs<kRectPosColorUVAttribs>(2);
112 } else if (hasUVs) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000113 *localOffset = sizeof(SkPoint);
robertphillips@google.com42903302013-04-20 12:26:07 +0000114 drawState->setVertexAttribs<kRectPosUVAttribs>(2);
115 } else {
116 drawState->setVertexAttribs<kRectPosUVAttribs>(1);
117 }
118}
119
120};
121
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000122enum {
123 kTraceCmdBit = 0x80,
124 kCmdMask = 0x7f,
125};
126
127static uint8_t add_trace_bit(uint8_t cmd) {
128 return cmd | kTraceCmdBit;
129}
130
131static uint8_t strip_trace_bit(uint8_t cmd) {
132 return cmd & kCmdMask;
133}
134
135static bool cmd_has_trace_marker(uint8_t cmd) {
commit-bot@chromium.orge6984a82014-03-25 15:49:59 +0000136 return SkToBool(cmd & kTraceCmdBit);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000137}
138
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000139void GrInOrderDrawBuffer::onDrawRect(const SkRect& rect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000140 const SkMatrix* matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000141 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000142 const SkMatrix* localMatrix) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000143 GrDrawState::AutoColorRestore acr;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000144
145 GrDrawState* drawState = this->drawState();
146
147 GrColor color = drawState->getColor();
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000148
robertphillips@google.com42903302013-04-20 12:26:07 +0000149 int colorOffset, localOffset;
150 set_vertex_attributes(drawState,
151 this->caps()->dualSourceBlendingSupport() || drawState->hasSolidCoverage(),
152 NULL != localRect,
153 &colorOffset, &localOffset);
154 if (colorOffset >= 0) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000155 // We set the draw state's color to white here. This is done so that any batching performed
156 // in our subclass's onDraw() won't get a false from GrDrawState::op== due to a color
157 // mismatch. TODO: Once vertex layout is owned by GrDrawState it should skip comparing the
rmistry@google.comc9f3b382013-04-22 12:45:30 +0000158 // 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 +0000159 // this.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000160 acr.set(drawState, 0xFFFFFFFF);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000161 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000162
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000163 AutoReleaseGeometry geo(this, 4, 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000164 if (!geo.succeeded()) {
165 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com934c5702012-03-20 21:17:58 +0000166 return;
167 }
168
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000169 // Go to device coords to allow batching across matrix changes
170 SkMatrix combinedMatrix;
171 if (NULL != matrix) {
172 combinedMatrix = *matrix;
173 } else {
174 combinedMatrix.reset();
175 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000176 combinedMatrix.postConcat(drawState->getViewMatrix());
jvanverth@google.com39768252013-02-14 15:25:44 +0000177 // 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 +0000178 // modify that stage's matrix. Otherwise if the effect is generating its source rect from
179 // the vertex positions then we have to account for the view matrix change.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000180 GrDrawState::AutoViewMatrixRestore avmr;
181 if (!avmr.setIdentity(drawState)) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000182 return;
183 }
184
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000185 size_t vsize = drawState->getVertexSize();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000186
187 geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vsize);
188 combinedMatrix.mapPointsWithStride(geo.positions(), vsize, 4);
189
190 SkRect devBounds;
191 // since we already computed the dev verts, set the bounds hint. This will help us avoid
192 // unnecessary clipping in our onDraw().
193 get_vertex_bounds(geo.vertices(), vsize, 4, &devBounds);
194
bsalomon@google.comc7818882013-03-20 19:19:53 +0000195 if (localOffset >= 0) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000196 SkPoint* coords = GrTCast<SkPoint*>(GrTCast<intptr_t>(geo.vertices()) + localOffset);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000197 coords->setRectFan(localRect->fLeft, localRect->fTop,
198 localRect->fRight, localRect->fBottom,
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000199 vsize);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000200 if (NULL != localMatrix) {
201 localMatrix->mapPointsWithStride(coords, vsize, 4);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000202 }
203 }
204
205 if (colorOffset >= 0) {
206 GrColor* vertColor = GrTCast<GrColor*>(GrTCast<intptr_t>(geo.vertices()) + colorOffset);
207 for (int i = 0; i < 4; ++i) {
208 *vertColor = color;
209 vertColor = (GrColor*) ((intptr_t) vertColor + vsize);
210 }
211 }
212
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000213 this->setIndexSourceToBuffer(this->getContext()->getQuadIndexBuffer());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000214 this->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, 4, 6, &devBounds);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000215
216 // to ensure that stashing the drawState ptr is valid
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000217 SkASSERT(this->drawState() == drawState);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000218}
219
220bool GrInOrderDrawBuffer::quickInsideClip(const SkRect& devBounds) {
221 if (!this->getDrawState().isClipState()) {
222 return true;
223 }
224 if (kUnknown_ClipProxyState == fClipProxyState) {
225 SkIRect rect;
226 bool iior;
227 this->getClip()->getConservativeBounds(this->getDrawState().getRenderTarget(), &rect, &iior);
228 if (iior) {
229 // The clip is a rect. We will remember that in fProxyClip. It is common for an edge (or
230 // all edges) of the clip to be at the edge of the RT. However, we get that clipping for
231 // free via the viewport. We don't want to think that clipping must be enabled in this
232 // case. So we extend the clip outward from the edge to avoid these false negatives.
233 fClipProxyState = kValid_ClipProxyState;
reed@google.com44699382013-10-31 17:28:30 +0000234 fClipProxy = SkRect::Make(rect);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000235
236 if (fClipProxy.fLeft <= 0) {
237 fClipProxy.fLeft = SK_ScalarMin;
238 }
239 if (fClipProxy.fTop <= 0) {
240 fClipProxy.fTop = SK_ScalarMin;
241 }
242 if (fClipProxy.fRight >= this->getDrawState().getRenderTarget()->width()) {
243 fClipProxy.fRight = SK_ScalarMax;
244 }
245 if (fClipProxy.fBottom >= this->getDrawState().getRenderTarget()->height()) {
246 fClipProxy.fBottom = SK_ScalarMax;
247 }
248 } else {
249 fClipProxyState = kInvalid_ClipProxyState;
250 }
251 }
252 if (kValid_ClipProxyState == fClipProxyState) {
253 return fClipProxy.contains(devBounds);
254 }
255 SkPoint originOffset = {SkIntToScalar(this->getClip()->fOrigin.fX),
256 SkIntToScalar(this->getClip()->fOrigin.fY)};
257 SkRect clipSpaceBounds = devBounds;
258 clipSpaceBounds.offset(originOffset);
259 return this->getClip()->fClipStack->quickContains(clipSpaceBounds);
260}
261
262int GrInOrderDrawBuffer::concatInstancedDraw(const DrawInfo& info) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000263 SkASSERT(info.isInstanced());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000264
bsalomon@google.com934c5702012-03-20 21:17:58 +0000265 const GeometrySrcState& geomSrc = this->getGeomSrc();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000266 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000267
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000268 // we only attempt to concat the case when reserved verts are used with a client-specified index
269 // buffer. To make this work with client-specified VBs we'd need to know if the VB was updated
270 // between draws.
271 if (kReserved_GeometrySrcType != geomSrc.fVertexSrc ||
272 kBuffer_GeometrySrcType != geomSrc.fIndexSrc) {
273 return 0;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000274 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000275 // Check if there is a draw info that is compatible that uses the same VB from the pool and
276 // the same IB
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000277 if (kDraw_Cmd != strip_trace_bit(fCmds.back())) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000278 return 0;
279 }
280
281 DrawRecord* draw = &fDraws.back();
282 GeometryPoolState& poolState = fGeoPoolStateStack.back();
283 const GrVertexBuffer* vertexBuffer = poolState.fPoolVertexBuffer;
284
285 if (!draw->isInstanced() ||
286 draw->verticesPerInstance() != info.verticesPerInstance() ||
287 draw->indicesPerInstance() != info.indicesPerInstance() ||
288 draw->fVertexBuffer != vertexBuffer ||
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000289 draw->fIndexBuffer != geomSrc.fIndexBuffer) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000290 return 0;
291 }
292 // info does not yet account for the offset from the start of the pool's VB while the previous
293 // draw record does.
294 int adjustedStartVertex = poolState.fPoolStartVertex + info.startVertex();
295 if (draw->startVertex() + draw->vertexCount() != adjustedStartVertex) {
296 return 0;
297 }
298
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000299 SkASSERT(poolState.fPoolStartVertex == draw->startVertex() + draw->vertexCount());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000300
301 // how many instances can be concat'ed onto draw given the size of the index buffer
302 int instancesToConcat = this->indexCountInCurrentSource() / info.indicesPerInstance();
303 instancesToConcat -= draw->instanceCount();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000304 instancesToConcat = SkTMin(instancesToConcat, info.instanceCount());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000305
306 // update the amount of reserved vertex data actually referenced in draws
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000307 size_t vertexBytes = instancesToConcat * info.verticesPerInstance() *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000308 drawState.getVertexSize();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000309 poolState.fUsedPoolVertexBytes = SkTMax(poolState.fUsedPoolVertexBytes, vertexBytes);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000310
311 draw->adjustInstanceCount(instancesToConcat);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000312
313 // update last fGpuCmdMarkers to include any additional trace markers that have been added
314 if (this->getActiveTraceMarkers().count() > 0) {
315 if (cmd_has_trace_marker(fCmds.back())) {
316 fGpuCmdMarkers.back().addSet(this->getActiveTraceMarkers());
317 } else {
318 fGpuCmdMarkers.push_back(this->getActiveTraceMarkers());
319 fCmds.back() = add_trace_bit(fCmds.back());
320 }
321 }
322
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000323 return instancesToConcat;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000324}
325
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000326class AutoClipReenable {
327public:
328 AutoClipReenable() : fDrawState(NULL) {}
329 ~AutoClipReenable() {
330 if (NULL != fDrawState) {
331 fDrawState->enableState(GrDrawState::kClip_StateBit);
332 }
333 }
334 void set(GrDrawState* drawState) {
335 if (drawState->isClipState()) {
336 fDrawState = drawState;
337 drawState->disableState(GrDrawState::kClip_StateBit);
338 }
339 }
340private:
341 GrDrawState* fDrawState;
342};
343
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000344void GrInOrderDrawBuffer::onDraw(const DrawInfo& info) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000345
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000346 GeometryPoolState& poolState = fGeoPoolStateStack.back();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000347 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000348 AutoClipReenable acr;
349
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000350 if (drawState.isClipState() &&
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000351 NULL != info.getDevBounds() &&
352 this->quickInsideClip(*info.getDevBounds())) {
353 acr.set(this->drawState());
354 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000355
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000356 if (this->needsNewClip()) {
357 this->recordClip();
358 }
359 if (this->needsNewState()) {
360 this->recordState();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000361 }
362
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000363 DrawRecord* draw;
364 if (info.isInstanced()) {
365 int instancesConcated = this->concatInstancedDraw(info);
366 if (info.instanceCount() > instancesConcated) {
367 draw = this->recordDraw(info);
368 draw->adjustInstanceCount(-instancesConcated);
369 } else {
370 return;
371 }
372 } else {
373 draw = this->recordDraw(info);
374 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000375
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000376 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000377 case kBuffer_GeometrySrcType:
378 draw->fVertexBuffer = this->getGeomSrc().fVertexBuffer;
379 break;
380 case kReserved_GeometrySrcType: // fallthrough
381 case kArray_GeometrySrcType: {
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000382 size_t vertexBytes = (info.vertexCount() + info.startVertex()) *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000383 drawState.getVertexSize();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000384 poolState.fUsedPoolVertexBytes = SkTMax(poolState.fUsedPoolVertexBytes, vertexBytes);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000385 draw->fVertexBuffer = poolState.fPoolVertexBuffer;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000386 draw->adjustStartVertex(poolState.fPoolStartVertex);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000387 break;
388 }
389 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000390 SkFAIL("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000391 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000392 draw->fVertexBuffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000393
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000394 if (info.isIndexed()) {
395 switch (this->getGeomSrc().fIndexSrc) {
396 case kBuffer_GeometrySrcType:
397 draw->fIndexBuffer = this->getGeomSrc().fIndexBuffer;
398 break;
399 case kReserved_GeometrySrcType: // fallthrough
400 case kArray_GeometrySrcType: {
401 size_t indexBytes = (info.indexCount() + info.startIndex()) * sizeof(uint16_t);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000402 poolState.fUsedPoolIndexBytes = SkTMax(poolState.fUsedPoolIndexBytes, indexBytes);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000403 draw->fIndexBuffer = poolState.fPoolIndexBuffer;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000404 draw->adjustStartIndex(poolState.fPoolStartIndex);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000405 break;
406 }
407 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000408 SkFAIL("unknown geom src type");
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000409 }
410 draw->fIndexBuffer->ref();
411 } else {
412 draw->fIndexBuffer = NULL;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000413 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000414}
415
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000416GrInOrderDrawBuffer::StencilPath::StencilPath() {}
417GrInOrderDrawBuffer::DrawPath::DrawPath() {}
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000418GrInOrderDrawBuffer::DrawPaths::DrawPaths() {}
419GrInOrderDrawBuffer::DrawPaths::~DrawPaths() {
420 if (fTransforms) {
421 SkDELETE_ARRAY(fTransforms);
422 }
cdaltonb85a0aa2014-07-21 15:32:44 -0700423 if (fIndices) {
424 SkDELETE_ARRAY(fIndices);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000425 }
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000426}
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000427
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000428void GrInOrderDrawBuffer::onStencilPath(const GrPath* path, SkPath::FillType fill) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000429 if (this->needsNewClip()) {
430 this->recordClip();
431 }
432 // Only compare the subset of GrDrawState relevant to path stenciling?
433 if (this->needsNewState()) {
434 this->recordState();
435 }
436 StencilPath* sp = this->recordStencilPath();
437 sp->fPath.reset(path);
438 path->ref();
439 sp->fFill = fill;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000440}
441
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000442void GrInOrderDrawBuffer::onDrawPath(const GrPath* path,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000443 SkPath::FillType fill, const GrDeviceCoordTexture* dstCopy) {
444 if (this->needsNewClip()) {
445 this->recordClip();
446 }
447 // TODO: Only compare the subset of GrDrawState relevant to path covering?
448 if (this->needsNewState()) {
449 this->recordState();
450 }
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000451 DrawPath* cp = this->recordDrawPath();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000452 cp->fPath.reset(path);
453 path->ref();
454 cp->fFill = fill;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000455 if (NULL != dstCopy) {
456 cp->fDstCopy = *dstCopy;
457 }
458}
459
cdaltonb85a0aa2014-07-21 15:32:44 -0700460void GrInOrderDrawBuffer::onDrawPaths(const GrPathRange* pathRange,
461 const uint32_t indices[], int count,
462 const float transforms[], PathTransformType transformsType,
463 SkPath::FillType fill, const GrDeviceCoordTexture* dstCopy) {
464 SkASSERT(NULL != pathRange);
465 SkASSERT(NULL != indices);
466 SkASSERT(NULL != transforms);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000467
468 if (this->needsNewClip()) {
469 this->recordClip();
470 }
471 if (this->needsNewState()) {
472 this->recordState();
473 }
474 DrawPaths* dp = this->recordDrawPaths();
cdaltonb85a0aa2014-07-21 15:32:44 -0700475 dp->fPathRange.reset(SkRef(pathRange));
476 dp->fIndices = SkNEW_ARRAY(uint32_t, count); // TODO: Accomplish this without a malloc
477 memcpy(dp->fIndices, indices, sizeof(uint32_t) * count);
478 dp->fCount = count;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000479
cdaltonb85a0aa2014-07-21 15:32:44 -0700480 const int transformsLength = PathTransformSize(transformsType) * count;
481 dp->fTransforms = SkNEW_ARRAY(float, transformsLength);
482 memcpy(dp->fTransforms, transforms, sizeof(float) * transformsLength);
483 dp->fTransformsType = transformsType;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000484
485 dp->fFill = fill;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000486
487 if (NULL != dstCopy) {
488 dp->fDstCopy = *dstCopy;
489 }
490}
491
skia.committer@gmail.com18bb41e2013-11-01 07:02:15 +0000492void GrInOrderDrawBuffer::clear(const SkIRect* rect, GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000493 bool canIgnoreRect, GrRenderTarget* renderTarget) {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000494 SkIRect r;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000495 if (NULL == renderTarget) {
496 renderTarget = this->drawState()->getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000497 SkASSERT(NULL != renderTarget);
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000498 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000499 if (NULL == rect) {
500 // We could do something smart and remove previous draws and clears to
501 // the current render target. If we get that smart we have to make sure
502 // those draws aren't read before this clear (render-to-texture).
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000503 r.setLTRB(0, 0, renderTarget->width(), renderTarget->height());
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000504 rect = &r;
505 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000506 Clear* clr = this->recordClear();
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000507 GrColorIsPMAssert(color);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000508 clr->fColor = color;
509 clr->fRect = *rect;
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000510 clr->fCanIgnoreRect = canIgnoreRect;
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000511 clr->fRenderTarget = renderTarget;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000512 renderTarget->ref();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000513}
514
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000515void GrInOrderDrawBuffer::discard(GrRenderTarget* renderTarget) {
516 if (!this->caps()->discardRenderTargetSupport()) {
517 return;
518 }
519 if (NULL == renderTarget) {
520 renderTarget = this->drawState()->getRenderTarget();
521 SkASSERT(NULL != renderTarget);
522 }
523 Clear* clr = this->recordClear();
524 clr->fColor = GrColor_ILLEGAL;
525 clr->fRenderTarget = renderTarget;
526 renderTarget->ref();
527}
528
reed@google.comac10a2d2010-12-22 21:39:39 +0000529void GrInOrderDrawBuffer::reset() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000530 SkASSERT(1 == fGeoPoolStateStack.count());
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000531 this->resetVertexSource();
532 this->resetIndexSource();
bsalomonbce3d6d2014-07-02 07:54:42 -0700533
534 DrawAllocator::Iter drawIter(&fDraws);
535 while (drawIter.next()) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000536 // we always have a VB, but not always an IB
bsalomonbce3d6d2014-07-02 07:54:42 -0700537 SkASSERT(NULL != drawIter->fVertexBuffer);
538 drawIter->fVertexBuffer->unref();
539 SkSafeUnref(drawIter->fIndexBuffer);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000540 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000541 fCmds.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000542 fDraws.reset();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000543 fStencilPaths.reset();
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000544 fDrawPath.reset();
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000545 fDrawPaths.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000546 fStates.reset();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000547 fClears.reset();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000548 fVertexPool.reset();
549 fIndexPool.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000550 fClips.reset();
bsalomon@google.com116ad842013-04-09 15:38:19 +0000551 fCopySurfaces.reset();
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000552 fGpuCmdMarkers.reset();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000553 fClipSet = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000554}
555
robertphillips@google.com1267fbd2013-07-03 18:37:27 +0000556void GrInOrderDrawBuffer::flush() {
557 if (fFlushing) {
558 return;
559 }
560
jvanverth8e80d172014-06-19 12:01:10 -0700561 this->getContext()->getFontCache()->updateTextures();
562
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000563 SkASSERT(kReserved_GeometrySrcType != this->getGeomSrc().fVertexSrc);
564 SkASSERT(kReserved_GeometrySrcType != this->getGeomSrc().fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000565
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000566 int numCmds = fCmds.count();
bsalomon@google.com358e4272013-01-10 14:40:28 +0000567 if (0 == numCmds) {
robertphillips@google.com1267fbd2013-07-03 18:37:27 +0000568 return;
reed@google.comac10a2d2010-12-22 21:39:39 +0000569 }
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000570
571 GrAutoTRestore<bool> flushRestore(&fFlushing);
572 fFlushing = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000573
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000574 fVertexPool.unmap();
575 fIndexPool.unmap();
reed@google.comac10a2d2010-12-22 21:39:39 +0000576
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000577 GrDrawTarget::AutoClipRestore acr(fDstGpu);
578 AutoGeometryAndStatePush agasp(fDstGpu, kPreserve_ASRInit);
bsalomon@google.comca432082013-01-23 19:53:46 +0000579
bsalomona70353e2014-07-07 08:15:07 -0700580 GrDrawState* prevDrawState = SkRef(fDstGpu->drawState());
reed@google.comac10a2d2010-12-22 21:39:39 +0000581
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000582 GrClipData clipData;
583
bsalomonbce3d6d2014-07-02 07:54:42 -0700584 StateAllocator::Iter stateIter(&fStates);
585 ClipAllocator::Iter clipIter(&fClips);
bsalomonbce3d6d2014-07-02 07:54:42 -0700586 ClearAllocator::Iter clearIter(&fClears);
587 DrawAllocator::Iter drawIter(&fDraws);
588 StencilPathAllocator::Iter stencilPathIter(&fStencilPaths);
589 DrawPathAllocator::Iter drawPathIter(&fDrawPath);
590 DrawPathsAllocator::Iter drawPathsIter(&fDrawPaths);
591 CopySurfaceAllocator::Iter copySurfaceIter(&fCopySurfaces);
592
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000593 int currCmdMarker = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000594
egdaniel3eee3832014-06-18 13:09:11 -0700595 fDstGpu->saveActiveTraceMarkers();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000596 for (int c = 0; c < numCmds; ++c) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000597 GrGpuTraceMarker newMarker("", -1);
egdanield78a1682014-07-09 10:41:26 -0700598 SkString traceString;
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000599 if (cmd_has_trace_marker(fCmds[c])) {
egdanield78a1682014-07-09 10:41:26 -0700600 traceString = fGpuCmdMarkers[currCmdMarker].toString();
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000601 newMarker.fMarker = traceString.c_str();
602 fDstGpu->addGpuTraceMarker(&newMarker);
603 ++currCmdMarker;
604 }
605 switch (strip_trace_bit(fCmds[c])) {
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000606 case kDraw_Cmd: {
bsalomona70353e2014-07-07 08:15:07 -0700607 SkASSERT(fDstGpu->drawState() != prevDrawState);
bsalomonbce3d6d2014-07-02 07:54:42 -0700608 SkAssertResult(drawIter.next());
609 fDstGpu->setVertexSourceToBuffer(drawIter->fVertexBuffer);
610 if (drawIter->isIndexed()) {
611 fDstGpu->setIndexSourceToBuffer(drawIter->fIndexBuffer);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000612 }
bsalomonbce3d6d2014-07-02 07:54:42 -0700613 fDstGpu->executeDraw(*drawIter);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000614 break;
615 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000616 case kStencilPath_Cmd: {
bsalomona70353e2014-07-07 08:15:07 -0700617 SkASSERT(fDstGpu->drawState() != prevDrawState);
bsalomonbce3d6d2014-07-02 07:54:42 -0700618 SkAssertResult(stencilPathIter.next());
619 fDstGpu->stencilPath(stencilPathIter->fPath.get(), stencilPathIter->fFill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000620 break;
621 }
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000622 case kDrawPath_Cmd: {
bsalomona70353e2014-07-07 08:15:07 -0700623 SkASSERT(fDstGpu->drawState() != prevDrawState);
bsalomonbce3d6d2014-07-02 07:54:42 -0700624 SkAssertResult(drawPathIter.next());
625 fDstGpu->executeDrawPath(drawPathIter->fPath.get(), drawPathIter->fFill,
626 NULL != drawPathIter->fDstCopy.texture() ?
627 &drawPathIter->fDstCopy :
628 NULL);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000629 break;
630 }
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000631 case kDrawPaths_Cmd: {
bsalomona70353e2014-07-07 08:15:07 -0700632 SkASSERT(fDstGpu->drawState() != prevDrawState);
bsalomonbce3d6d2014-07-02 07:54:42 -0700633 SkAssertResult(drawPathsIter.next());
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000634 const GrDeviceCoordTexture* dstCopy =
bsalomonbce3d6d2014-07-02 07:54:42 -0700635 NULL !=drawPathsIter->fDstCopy.texture() ? &drawPathsIter->fDstCopy : NULL;
cdaltonb85a0aa2014-07-21 15:32:44 -0700636 fDstGpu->executeDrawPaths(drawPathsIter->fPathRange.get(),
637 drawPathsIter->fIndices,
638 drawPathsIter->fCount,
639 drawPathsIter->fTransforms,
640 drawPathsIter->fTransformsType,
641 drawPathsIter->fFill,
642 dstCopy);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000643 break;
644 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000645 case kSetState_Cmd:
bsalomonbce3d6d2014-07-02 07:54:42 -0700646 SkAssertResult(stateIter.next());
bsalomona70353e2014-07-07 08:15:07 -0700647 fDstGpu->setDrawState(stateIter.get());
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000648 break;
649 case kSetClip_Cmd:
bsalomonbce3d6d2014-07-02 07:54:42 -0700650 SkAssertResult(clipIter.next());
bsalomonf0480b12014-07-02 12:11:24 -0700651 clipData.fClipStack = &clipIter->fStack;
652 clipData.fOrigin = clipIter->fOrigin;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000653 fDstGpu->setClip(&clipData);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000654 break;
655 case kClear_Cmd:
bsalomonbce3d6d2014-07-02 07:54:42 -0700656 SkAssertResult(clearIter.next());
657 if (GrColor_ILLEGAL == clearIter->fColor) {
658 fDstGpu->discard(clearIter->fRenderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000659 } else {
bsalomonbce3d6d2014-07-02 07:54:42 -0700660 fDstGpu->clear(&clearIter->fRect,
661 clearIter->fColor,
662 clearIter->fCanIgnoreRect,
663 clearIter->fRenderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000664 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000665 break;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000666 case kCopySurface_Cmd:
bsalomonbce3d6d2014-07-02 07:54:42 -0700667 SkAssertResult(copySurfaceIter.next());
668 fDstGpu->copySurface(copySurfaceIter->fDst.get(),
669 copySurfaceIter->fSrc.get(),
670 copySurfaceIter->fSrcRect,
671 copySurfaceIter->fDstPoint);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000672 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000673 }
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000674 if (cmd_has_trace_marker(fCmds[c])) {
675 fDstGpu->removeGpuTraceMarker(&newMarker);
676 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000677 }
egdaniel3eee3832014-06-18 13:09:11 -0700678 fDstGpu->restoreActiveTraceMarkers();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000679 // we should have consumed all the states, clips, etc.
bsalomonbce3d6d2014-07-02 07:54:42 -0700680 SkASSERT(!stateIter.next());
681 SkASSERT(!clipIter.next());
bsalomonbce3d6d2014-07-02 07:54:42 -0700682 SkASSERT(!clearIter.next());
683 SkASSERT(!drawIter.next());
684 SkASSERT(!copySurfaceIter.next());
685 SkASSERT(!stencilPathIter.next());
686 SkASSERT(!drawPathIter.next());
687 SkASSERT(!drawPathsIter.next());
688
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000689 SkASSERT(fGpuCmdMarkers.count() == currCmdMarker);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000690
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000691 fDstGpu->setDrawState(prevDrawState);
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000692 prevDrawState->unref();
bsalomon@google.com55e4a202013-01-11 13:54:21 +0000693 this->reset();
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000694 ++fDrawID;
reed@google.comac10a2d2010-12-22 21:39:39 +0000695}
696
bsalomon@google.com116ad842013-04-09 15:38:19 +0000697bool GrInOrderDrawBuffer::onCopySurface(GrSurface* dst,
698 GrSurface* src,
699 const SkIRect& srcRect,
700 const SkIPoint& dstPoint) {
701 if (fDstGpu->canCopySurface(dst, src, srcRect, dstPoint)) {
702 CopySurface* cs = this->recordCopySurface();
703 cs->fDst.reset(SkRef(dst));
704 cs->fSrc.reset(SkRef(src));
705 cs->fSrcRect = srcRect;
706 cs->fDstPoint = dstPoint;
707 return true;
708 } else {
709 return false;
710 }
711}
712
713bool GrInOrderDrawBuffer::onCanCopySurface(GrSurface* dst,
714 GrSurface* src,
715 const SkIRect& srcRect,
716 const SkIPoint& dstPoint) {
717 return fDstGpu->canCopySurface(dst, src, srcRect, dstPoint);
718}
719
bsalomon@google.comeb851172013-04-15 13:51:00 +0000720void GrInOrderDrawBuffer::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
721 fDstGpu->initCopySurfaceDstDesc(src, desc);
722}
723
robertphillips@google.com9528bdb2013-09-18 22:33:57 +0000724void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace(int vertexCount,
725 int indexCount) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000726 // We use geometryHints() to know whether to flush the draw buffer. We
727 // can't flush if we are inside an unbalanced pushGeometrySource.
728 // Moreover, flushing blows away vertex and index data that was
729 // previously reserved. So if the vertex or index data is pulled from
730 // reserved space and won't be released by this request then we can't
731 // flush.
732 bool insideGeoPush = fGeoPoolStateStack.count() > 1;
bsalomon@google.com97805382012-03-13 14:32:07 +0000733
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000734 bool unreleasedVertexSpace =
735 !vertexCount &&
736 kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc;
bsalomon@google.com97805382012-03-13 14:32:07 +0000737
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000738 bool unreleasedIndexSpace =
739 !indexCount &&
740 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
bsalomon@google.com97805382012-03-13 14:32:07 +0000741
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000742 // we don't want to finalize any reserved geom on the target since
743 // we don't know that the client has finished writing to it.
744 bool targetHasReservedGeom = fDstGpu->hasReservedVerticesOrIndices();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000745
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000746 int vcount = vertexCount;
747 int icount = indexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000748
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000749 if (!insideGeoPush &&
750 !unreleasedVertexSpace &&
751 !unreleasedIndexSpace &&
752 !targetHasReservedGeom &&
753 this->geometryHints(&vcount, &icount)) {
bsalomon@google.com97805382012-03-13 14:32:07 +0000754
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000755 this->flush();
bsalomon@google.com97805382012-03-13 14:32:07 +0000756 }
757}
758
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000759bool GrInOrderDrawBuffer::geometryHints(int* vertexCount,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000760 int* indexCount) const {
761 // we will recommend a flush if the data could fit in a single
762 // preallocated buffer but none are left and it can't fit
763 // in the current buffer (which may not be prealloced).
reed@google.comac10a2d2010-12-22 21:39:39 +0000764 bool flush = false;
765 if (NULL != indexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000766 int32_t currIndices = fIndexPool.currentBufferIndices();
767 if (*indexCount > currIndices &&
768 (!fIndexPool.preallocatedBuffersRemaining() &&
769 *indexCount <= fIndexPool.preallocatedBufferIndices())) {
770
771 flush = true;
772 }
773 *indexCount = currIndices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000774 }
775 if (NULL != vertexCount) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000776 size_t vertexSize = this->getDrawState().getVertexSize();
jvanverth@google.coma6338982013-01-31 21:34:25 +0000777 int32_t currVertices = fVertexPool.currentBufferVertices(vertexSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000778 if (*vertexCount > currVertices &&
779 (!fVertexPool.preallocatedBuffersRemaining() &&
jvanverth@google.coma6338982013-01-31 21:34:25 +0000780 *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexSize))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000781
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000782 flush = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000783 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000784 *vertexCount = currVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000785 }
786 return flush;
787}
788
jvanverth@google.coma6338982013-01-31 21:34:25 +0000789bool GrInOrderDrawBuffer::onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000790 int vertexCount,
791 void** vertices) {
792 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000793 SkASSERT(vertexCount > 0);
794 SkASSERT(NULL != vertices);
795 SkASSERT(0 == poolState.fUsedPoolVertexBytes);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000796
jvanverth@google.coma6338982013-01-31 21:34:25 +0000797 *vertices = fVertexPool.makeSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000798 vertexCount,
799 &poolState.fPoolVertexBuffer,
800 &poolState.fPoolStartVertex);
801 return NULL != *vertices;
802}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000803
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000804bool GrInOrderDrawBuffer::onReserveIndexSpace(int indexCount, void** indices) {
805 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000806 SkASSERT(indexCount > 0);
807 SkASSERT(NULL != indices);
808 SkASSERT(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000809
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000810 *indices = fIndexPool.makeSpace(indexCount,
811 &poolState.fPoolIndexBuffer,
812 &poolState.fPoolStartIndex);
813 return NULL != *indices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000814}
815
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000816void GrInOrderDrawBuffer::releaseReservedVertexSpace() {
817 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000818 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000819
820 // If we get a release vertex space call then our current source should either be reserved
821 // or array (which we copied into reserved space).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000822 SkASSERT(kReserved_GeometrySrcType == geoSrc.fVertexSrc ||
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000823 kArray_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000824
825 // When the caller reserved vertex buffer space we gave it back a pointer
826 // provided by the vertex buffer pool. At each draw we tracked the largest
827 // offset into the pool's pointer that was referenced. Now we return to the
828 // pool any portion at the tail of the allocation that no draw referenced.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000829 size_t reservedVertexBytes = geoSrc.fVertexSize * geoSrc.fVertexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000830 fVertexPool.putBack(reservedVertexBytes -
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000831 poolState.fUsedPoolVertexBytes);
832 poolState.fUsedPoolVertexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000833 poolState.fPoolVertexBuffer = NULL;
834 poolState.fPoolStartVertex = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000835}
836
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000837void GrInOrderDrawBuffer::releaseReservedIndexSpace() {
838 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000839 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000840
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000841 // If we get a release index space call then our current source should either be reserved
842 // or array (which we copied into reserved space).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000843 SkASSERT(kReserved_GeometrySrcType == geoSrc.fIndexSrc ||
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000844 kArray_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000845
846 // Similar to releaseReservedVertexSpace we return any unused portion at
847 // the tail
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000848 size_t reservedIndexBytes = sizeof(uint16_t) * geoSrc.fIndexCount;
849 fIndexPool.putBack(reservedIndexBytes - poolState.fUsedPoolIndexBytes);
850 poolState.fUsedPoolIndexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000851 poolState.fPoolIndexBuffer = NULL;
852 poolState.fPoolStartIndex = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000853}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000854
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000855void GrInOrderDrawBuffer::onSetVertexSourceToArray(const void* vertexArray,
856 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000857
858 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000859 SkASSERT(0 == poolState.fUsedPoolVertexBytes);
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000860#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000861 bool success =
862#endif
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000863 fVertexPool.appendVertices(this->getVertexSize(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000864 vertexCount,
865 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000866 &poolState.fPoolVertexBuffer,
867 &poolState.fPoolStartVertex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000868 GR_DEBUGASSERT(success);
869}
870
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000871void GrInOrderDrawBuffer::onSetIndexSourceToArray(const void* indexArray,
872 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000873 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000874 SkASSERT(0 == poolState.fUsedPoolIndexBytes);
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000875#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000876 bool success =
877#endif
878 fIndexPool.appendIndices(indexCount,
879 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000880 &poolState.fPoolIndexBuffer,
881 &poolState.fPoolStartIndex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000882 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000883}
884
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000885void GrInOrderDrawBuffer::releaseVertexArray() {
886 // When the client provides an array as the vertex source we handled it
887 // by copying their array into reserved space.
888 this->GrInOrderDrawBuffer::releaseReservedVertexSpace();
889}
890
891void GrInOrderDrawBuffer::releaseIndexArray() {
892 // When the client provides an array as the index source we handled it
893 // by copying their array into reserved space.
894 this->GrInOrderDrawBuffer::releaseReservedIndexSpace();
895}
896
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000897void GrInOrderDrawBuffer::geometrySourceWillPush() {
898 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
899 poolState.fUsedPoolVertexBytes = 0;
900 poolState.fUsedPoolIndexBytes = 0;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000901#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000902 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
903 poolState.fPoolStartVertex = ~0;
904 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
905 poolState.fPoolStartIndex = ~0;
906#endif
907}
908
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000909void GrInOrderDrawBuffer::geometrySourceWillPop(
910 const GeometrySrcState& restoredState) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000911 SkASSERT(fGeoPoolStateStack.count() > 1);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000912 fGeoPoolStateStack.pop_back();
913 GeometryPoolState& poolState = fGeoPoolStateStack.back();
914 // we have to assume that any slack we had in our vertex/index data
915 // is now unreleasable because data may have been appended later in the
916 // pool.
917 if (kReserved_GeometrySrcType == restoredState.fVertexSrc ||
918 kArray_GeometrySrcType == restoredState.fVertexSrc) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000919 poolState.fUsedPoolVertexBytes = restoredState.fVertexSize * restoredState.fVertexCount;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000920 }
921 if (kReserved_GeometrySrcType == restoredState.fIndexSrc ||
922 kArray_GeometrySrcType == restoredState.fIndexSrc) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000923 poolState.fUsedPoolIndexBytes = sizeof(uint16_t) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000924 restoredState.fIndexCount;
925 }
926}
927
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000928bool GrInOrderDrawBuffer::needsNewState() const {
bsalomona70353e2014-07-07 08:15:07 -0700929 return fStates.empty() || fStates.back() != this->getDrawState();
reed@google.comac10a2d2010-12-22 21:39:39 +0000930}
931
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000932bool GrInOrderDrawBuffer::needsNewClip() const {
bsalomon@google.com358e4272013-01-10 14:40:28 +0000933 if (this->getDrawState().isClipState()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000934 if (fClipSet &&
bsalomon@google.com358e4272013-01-10 14:40:28 +0000935 (fClips.empty() ||
bsalomonf0480b12014-07-02 12:11:24 -0700936 fClips.back().fStack != *this->getClip()->fClipStack ||
937 fClips.back().fOrigin != this->getClip()->fOrigin)) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000938 return true;
939 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000940 }
941 return false;
942}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000943
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000944void GrInOrderDrawBuffer::addToCmdBuffer(uint8_t cmd) {
945 SkASSERT(!cmd_has_trace_marker(cmd));
946 const GrTraceMarkerSet& activeTraceMarkers = this->getActiveTraceMarkers();
947 if (activeTraceMarkers.count() > 0) {
948 fCmds.push_back(add_trace_bit(cmd));
949 fGpuCmdMarkers.push_back(activeTraceMarkers);
950 } else {
951 fCmds.push_back(cmd);
952 }
953}
954
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000955void GrInOrderDrawBuffer::recordClip() {
bsalomonf0480b12014-07-02 12:11:24 -0700956 fClips.push_back().fStack = *this->getClip()->fClipStack;
957 fClips.back().fOrigin = this->getClip()->fOrigin;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000958 fClipSet = false;
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000959 this->addToCmdBuffer(kSetClip_Cmd);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000960}
961
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000962void GrInOrderDrawBuffer::recordState() {
bsalomona70353e2014-07-07 08:15:07 -0700963 fStates.push_back() = this->getDrawState();
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000964 this->addToCmdBuffer(kSetState_Cmd);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000965}
966
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000967GrInOrderDrawBuffer::DrawRecord* GrInOrderDrawBuffer::recordDraw(const DrawInfo& info) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000968 this->addToCmdBuffer(kDraw_Cmd);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000969 return &fDraws.push_back(info);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000970}
971
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000972GrInOrderDrawBuffer::StencilPath* GrInOrderDrawBuffer::recordStencilPath() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000973 this->addToCmdBuffer(kStencilPath_Cmd);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000974 return &fStencilPaths.push_back();
975}
976
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000977GrInOrderDrawBuffer::DrawPath* GrInOrderDrawBuffer::recordDrawPath() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000978 this->addToCmdBuffer(kDrawPath_Cmd);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000979 return &fDrawPath.push_back();
980}
981
982GrInOrderDrawBuffer::DrawPaths* GrInOrderDrawBuffer::recordDrawPaths() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000983 this->addToCmdBuffer(kDrawPaths_Cmd);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000984 return &fDrawPaths.push_back();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000985}
986
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000987GrInOrderDrawBuffer::Clear* GrInOrderDrawBuffer::recordClear() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000988 this->addToCmdBuffer(kClear_Cmd);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000989 return &fClears.push_back();
reed@google.comac10a2d2010-12-22 21:39:39 +0000990}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000991
bsalomon@google.com116ad842013-04-09 15:38:19 +0000992GrInOrderDrawBuffer::CopySurface* GrInOrderDrawBuffer::recordCopySurface() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000993 this->addToCmdBuffer(kCopySurface_Cmd);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000994 return &fCopySurfaces.push_back();
995}
996
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000997void GrInOrderDrawBuffer::clipWillBeSet(const GrClipData* newClipData) {
998 INHERITED::clipWillBeSet(newClipData);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000999 fClipSet = true;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +00001000 fClipProxyState = kUnknown_ClipProxyState;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001001}