blob: 6e447590b01720462a50224feafd723aa128888c [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,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000140 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000141 const SkMatrix* localMatrix) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000142 GrDrawState::AutoColorRestore acr;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000143
144 GrDrawState* drawState = this->drawState();
145
146 GrColor color = drawState->getColor();
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000147
robertphillips@google.com42903302013-04-20 12:26:07 +0000148 int colorOffset, localOffset;
149 set_vertex_attributes(drawState,
150 this->caps()->dualSourceBlendingSupport() || drawState->hasSolidCoverage(),
151 NULL != localRect,
152 &colorOffset, &localOffset);
153 if (colorOffset >= 0) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000154 // We set the draw state's color to white here. This is done so that any batching performed
155 // in our subclass's onDraw() won't get a false from GrDrawState::op== due to a color
156 // mismatch. TODO: Once vertex layout is owned by GrDrawState it should skip comparing the
rmistry@google.comc9f3b382013-04-22 12:45:30 +0000157 // 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 +0000158 // this.
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000159 acr.set(drawState, 0xFFFFFFFF);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000160 }
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000161
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000162 AutoReleaseGeometry geo(this, 4, 0);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000163 if (!geo.succeeded()) {
164 GrPrintf("Failed to get space for vertices!\n");
bsalomon@google.com934c5702012-03-20 21:17:58 +0000165 return;
166 }
167
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000168 // Go to device coords to allow batching across matrix changes
bsalomon01c8da12014-08-04 09:21:30 -0700169 SkMatrix matrix = drawState->getViewMatrix();
170
jvanverth@google.com39768252013-02-14 15:25:44 +0000171 // 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 +0000172 // modify that stage's matrix. Otherwise if the effect is generating its source rect from
173 // the vertex positions then we have to account for the view matrix change.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000174 GrDrawState::AutoViewMatrixRestore avmr;
175 if (!avmr.setIdentity(drawState)) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000176 return;
177 }
178
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000179 size_t vsize = drawState->getVertexSize();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000180
181 geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vsize);
bsalomon01c8da12014-08-04 09:21:30 -0700182 matrix.mapPointsWithStride(geo.positions(), vsize, 4);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000183
184 SkRect devBounds;
185 // since we already computed the dev verts, set the bounds hint. This will help us avoid
186 // unnecessary clipping in our onDraw().
187 get_vertex_bounds(geo.vertices(), vsize, 4, &devBounds);
188
bsalomon@google.comc7818882013-03-20 19:19:53 +0000189 if (localOffset >= 0) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000190 SkPoint* coords = GrTCast<SkPoint*>(GrTCast<intptr_t>(geo.vertices()) + localOffset);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000191 coords->setRectFan(localRect->fLeft, localRect->fTop,
192 localRect->fRight, localRect->fBottom,
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000193 vsize);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000194 if (NULL != localMatrix) {
195 localMatrix->mapPointsWithStride(coords, vsize, 4);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000196 }
197 }
198
199 if (colorOffset >= 0) {
200 GrColor* vertColor = GrTCast<GrColor*>(GrTCast<intptr_t>(geo.vertices()) + colorOffset);
201 for (int i = 0; i < 4; ++i) {
202 *vertColor = color;
203 vertColor = (GrColor*) ((intptr_t) vertColor + vsize);
204 }
205 }
206
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000207 this->setIndexSourceToBuffer(this->getContext()->getQuadIndexBuffer());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000208 this->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, 4, 6, &devBounds);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000209
210 // to ensure that stashing the drawState ptr is valid
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000211 SkASSERT(this->drawState() == drawState);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000212}
213
214bool GrInOrderDrawBuffer::quickInsideClip(const SkRect& devBounds) {
215 if (!this->getDrawState().isClipState()) {
216 return true;
217 }
218 if (kUnknown_ClipProxyState == fClipProxyState) {
219 SkIRect rect;
220 bool iior;
221 this->getClip()->getConservativeBounds(this->getDrawState().getRenderTarget(), &rect, &iior);
222 if (iior) {
223 // The clip is a rect. We will remember that in fProxyClip. It is common for an edge (or
224 // all edges) of the clip to be at the edge of the RT. However, we get that clipping for
225 // free via the viewport. We don't want to think that clipping must be enabled in this
226 // case. So we extend the clip outward from the edge to avoid these false negatives.
227 fClipProxyState = kValid_ClipProxyState;
reed@google.com44699382013-10-31 17:28:30 +0000228 fClipProxy = SkRect::Make(rect);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000229
230 if (fClipProxy.fLeft <= 0) {
231 fClipProxy.fLeft = SK_ScalarMin;
232 }
233 if (fClipProxy.fTop <= 0) {
234 fClipProxy.fTop = SK_ScalarMin;
235 }
236 if (fClipProxy.fRight >= this->getDrawState().getRenderTarget()->width()) {
237 fClipProxy.fRight = SK_ScalarMax;
238 }
239 if (fClipProxy.fBottom >= this->getDrawState().getRenderTarget()->height()) {
240 fClipProxy.fBottom = SK_ScalarMax;
241 }
242 } else {
243 fClipProxyState = kInvalid_ClipProxyState;
244 }
245 }
246 if (kValid_ClipProxyState == fClipProxyState) {
247 return fClipProxy.contains(devBounds);
248 }
249 SkPoint originOffset = {SkIntToScalar(this->getClip()->fOrigin.fX),
250 SkIntToScalar(this->getClip()->fOrigin.fY)};
251 SkRect clipSpaceBounds = devBounds;
252 clipSpaceBounds.offset(originOffset);
253 return this->getClip()->fClipStack->quickContains(clipSpaceBounds);
254}
255
256int GrInOrderDrawBuffer::concatInstancedDraw(const DrawInfo& info) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000257 SkASSERT(info.isInstanced());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000258
bsalomon@google.com934c5702012-03-20 21:17:58 +0000259 const GeometrySrcState& geomSrc = this->getGeomSrc();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000260 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000261
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000262 // we only attempt to concat the case when reserved verts are used with a client-specified index
263 // buffer. To make this work with client-specified VBs we'd need to know if the VB was updated
264 // between draws.
265 if (kReserved_GeometrySrcType != geomSrc.fVertexSrc ||
266 kBuffer_GeometrySrcType != geomSrc.fIndexSrc) {
267 return 0;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000268 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000269 // Check if there is a draw info that is compatible that uses the same VB from the pool and
270 // the same IB
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000271 if (kDraw_Cmd != strip_trace_bit(fCmds.back())) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000272 return 0;
273 }
274
275 DrawRecord* draw = &fDraws.back();
276 GeometryPoolState& poolState = fGeoPoolStateStack.back();
277 const GrVertexBuffer* vertexBuffer = poolState.fPoolVertexBuffer;
278
279 if (!draw->isInstanced() ||
280 draw->verticesPerInstance() != info.verticesPerInstance() ||
281 draw->indicesPerInstance() != info.indicesPerInstance() ||
282 draw->fVertexBuffer != vertexBuffer ||
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000283 draw->fIndexBuffer != geomSrc.fIndexBuffer) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000284 return 0;
285 }
286 // info does not yet account for the offset from the start of the pool's VB while the previous
287 // draw record does.
288 int adjustedStartVertex = poolState.fPoolStartVertex + info.startVertex();
289 if (draw->startVertex() + draw->vertexCount() != adjustedStartVertex) {
290 return 0;
291 }
292
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000293 SkASSERT(poolState.fPoolStartVertex == draw->startVertex() + draw->vertexCount());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000294
295 // how many instances can be concat'ed onto draw given the size of the index buffer
296 int instancesToConcat = this->indexCountInCurrentSource() / info.indicesPerInstance();
297 instancesToConcat -= draw->instanceCount();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000298 instancesToConcat = SkTMin(instancesToConcat, info.instanceCount());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000299
300 // update the amount of reserved vertex data actually referenced in draws
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000301 size_t vertexBytes = instancesToConcat * info.verticesPerInstance() *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000302 drawState.getVertexSize();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000303 poolState.fUsedPoolVertexBytes = SkTMax(poolState.fUsedPoolVertexBytes, vertexBytes);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000304
305 draw->adjustInstanceCount(instancesToConcat);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000306
307 // update last fGpuCmdMarkers to include any additional trace markers that have been added
308 if (this->getActiveTraceMarkers().count() > 0) {
309 if (cmd_has_trace_marker(fCmds.back())) {
310 fGpuCmdMarkers.back().addSet(this->getActiveTraceMarkers());
311 } else {
312 fGpuCmdMarkers.push_back(this->getActiveTraceMarkers());
313 fCmds.back() = add_trace_bit(fCmds.back());
314 }
315 }
316
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000317 return instancesToConcat;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000318}
319
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000320class AutoClipReenable {
321public:
322 AutoClipReenable() : fDrawState(NULL) {}
323 ~AutoClipReenable() {
324 if (NULL != fDrawState) {
325 fDrawState->enableState(GrDrawState::kClip_StateBit);
326 }
327 }
328 void set(GrDrawState* drawState) {
329 if (drawState->isClipState()) {
330 fDrawState = drawState;
331 drawState->disableState(GrDrawState::kClip_StateBit);
332 }
333 }
334private:
335 GrDrawState* fDrawState;
336};
337
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000338void GrInOrderDrawBuffer::onDraw(const DrawInfo& info) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000339
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000340 GeometryPoolState& poolState = fGeoPoolStateStack.back();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000341 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000342 AutoClipReenable acr;
343
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000344 if (drawState.isClipState() &&
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000345 NULL != info.getDevBounds() &&
346 this->quickInsideClip(*info.getDevBounds())) {
347 acr.set(this->drawState());
348 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000349
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000350 if (this->needsNewClip()) {
351 this->recordClip();
352 }
bsalomon838f62d2014-08-05 07:15:57 -0700353 this->recordStateIfNecessary();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000354
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000355 DrawRecord* draw;
356 if (info.isInstanced()) {
357 int instancesConcated = this->concatInstancedDraw(info);
358 if (info.instanceCount() > instancesConcated) {
359 draw = this->recordDraw(info);
360 draw->adjustInstanceCount(-instancesConcated);
361 } else {
362 return;
363 }
364 } else {
365 draw = this->recordDraw(info);
366 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000367
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000368 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000369 case kBuffer_GeometrySrcType:
370 draw->fVertexBuffer = this->getGeomSrc().fVertexBuffer;
371 break;
372 case kReserved_GeometrySrcType: // fallthrough
373 case kArray_GeometrySrcType: {
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000374 size_t vertexBytes = (info.vertexCount() + info.startVertex()) *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000375 drawState.getVertexSize();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000376 poolState.fUsedPoolVertexBytes = SkTMax(poolState.fUsedPoolVertexBytes, vertexBytes);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000377 draw->fVertexBuffer = poolState.fPoolVertexBuffer;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000378 draw->adjustStartVertex(poolState.fPoolStartVertex);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000379 break;
380 }
381 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000382 SkFAIL("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000383 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000384 draw->fVertexBuffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000385
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000386 if (info.isIndexed()) {
387 switch (this->getGeomSrc().fIndexSrc) {
388 case kBuffer_GeometrySrcType:
389 draw->fIndexBuffer = this->getGeomSrc().fIndexBuffer;
390 break;
391 case kReserved_GeometrySrcType: // fallthrough
392 case kArray_GeometrySrcType: {
393 size_t indexBytes = (info.indexCount() + info.startIndex()) * sizeof(uint16_t);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000394 poolState.fUsedPoolIndexBytes = SkTMax(poolState.fUsedPoolIndexBytes, indexBytes);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000395 draw->fIndexBuffer = poolState.fPoolIndexBuffer;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000396 draw->adjustStartIndex(poolState.fPoolStartIndex);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000397 break;
398 }
399 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000400 SkFAIL("unknown geom src type");
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000401 }
402 draw->fIndexBuffer->ref();
403 } else {
404 draw->fIndexBuffer = NULL;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000405 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000406}
407
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000408GrInOrderDrawBuffer::StencilPath::StencilPath() {}
409GrInOrderDrawBuffer::DrawPath::DrawPath() {}
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000410GrInOrderDrawBuffer::DrawPaths::DrawPaths() {}
411GrInOrderDrawBuffer::DrawPaths::~DrawPaths() {
412 if (fTransforms) {
413 SkDELETE_ARRAY(fTransforms);
414 }
cdaltonb85a0aa2014-07-21 15:32:44 -0700415 if (fIndices) {
416 SkDELETE_ARRAY(fIndices);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000417 }
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000418}
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000419
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000420void GrInOrderDrawBuffer::onStencilPath(const GrPath* path, SkPath::FillType fill) {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000421 if (this->needsNewClip()) {
422 this->recordClip();
423 }
424 // Only compare the subset of GrDrawState relevant to path stenciling?
bsalomon838f62d2014-08-05 07:15:57 -0700425 this->recordStateIfNecessary();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000426 StencilPath* sp = this->recordStencilPath();
427 sp->fPath.reset(path);
428 path->ref();
429 sp->fFill = fill;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000430}
431
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000432void GrInOrderDrawBuffer::onDrawPath(const GrPath* path,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000433 SkPath::FillType fill, const GrDeviceCoordTexture* dstCopy) {
434 if (this->needsNewClip()) {
435 this->recordClip();
436 }
437 // TODO: Only compare the subset of GrDrawState relevant to path covering?
bsalomon838f62d2014-08-05 07:15:57 -0700438 this->recordStateIfNecessary();
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000439 DrawPath* cp = this->recordDrawPath();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000440 cp->fPath.reset(path);
441 path->ref();
442 cp->fFill = fill;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000443 if (NULL != dstCopy) {
444 cp->fDstCopy = *dstCopy;
445 }
446}
447
cdaltonb85a0aa2014-07-21 15:32:44 -0700448void GrInOrderDrawBuffer::onDrawPaths(const GrPathRange* pathRange,
449 const uint32_t indices[], int count,
450 const float transforms[], PathTransformType transformsType,
451 SkPath::FillType fill, const GrDeviceCoordTexture* dstCopy) {
452 SkASSERT(NULL != pathRange);
453 SkASSERT(NULL != indices);
454 SkASSERT(NULL != transforms);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000455
456 if (this->needsNewClip()) {
457 this->recordClip();
458 }
bsalomon838f62d2014-08-05 07:15:57 -0700459 this->recordStateIfNecessary();
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000460 DrawPaths* dp = this->recordDrawPaths();
cdaltonb85a0aa2014-07-21 15:32:44 -0700461 dp->fPathRange.reset(SkRef(pathRange));
462 dp->fIndices = SkNEW_ARRAY(uint32_t, count); // TODO: Accomplish this without a malloc
463 memcpy(dp->fIndices, indices, sizeof(uint32_t) * count);
464 dp->fCount = count;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000465
cdaltonb85a0aa2014-07-21 15:32:44 -0700466 const int transformsLength = PathTransformSize(transformsType) * count;
467 dp->fTransforms = SkNEW_ARRAY(float, transformsLength);
468 memcpy(dp->fTransforms, transforms, sizeof(float) * transformsLength);
469 dp->fTransformsType = transformsType;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000470
471 dp->fFill = fill;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000472
473 if (NULL != dstCopy) {
474 dp->fDstCopy = *dstCopy;
475 }
476}
477
skia.committer@gmail.com18bb41e2013-11-01 07:02:15 +0000478void GrInOrderDrawBuffer::clear(const SkIRect* rect, GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000479 bool canIgnoreRect, GrRenderTarget* renderTarget) {
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000480 SkIRect r;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000481 if (NULL == renderTarget) {
482 renderTarget = this->drawState()->getRenderTarget();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000483 SkASSERT(NULL != renderTarget);
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000484 }
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000485 if (NULL == rect) {
486 // We could do something smart and remove previous draws and clears to
487 // the current render target. If we get that smart we have to make sure
488 // those draws aren't read before this clear (render-to-texture).
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000489 r.setLTRB(0, 0, renderTarget->width(), renderTarget->height());
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000490 rect = &r;
491 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000492 Clear* clr = this->recordClear();
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000493 GrColorIsPMAssert(color);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000494 clr->fColor = color;
495 clr->fRect = *rect;
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000496 clr->fCanIgnoreRect = canIgnoreRect;
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000497 clr->fRenderTarget = renderTarget;
bsalomon@google.com1b3ce472012-08-17 13:43:08 +0000498 renderTarget->ref();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000499}
500
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000501void GrInOrderDrawBuffer::discard(GrRenderTarget* renderTarget) {
502 if (!this->caps()->discardRenderTargetSupport()) {
503 return;
504 }
505 if (NULL == renderTarget) {
506 renderTarget = this->drawState()->getRenderTarget();
507 SkASSERT(NULL != renderTarget);
508 }
509 Clear* clr = this->recordClear();
510 clr->fColor = GrColor_ILLEGAL;
511 clr->fRenderTarget = renderTarget;
512 renderTarget->ref();
513}
514
reed@google.comac10a2d2010-12-22 21:39:39 +0000515void GrInOrderDrawBuffer::reset() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000516 SkASSERT(1 == fGeoPoolStateStack.count());
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000517 this->resetVertexSource();
518 this->resetIndexSource();
bsalomonbce3d6d2014-07-02 07:54:42 -0700519
520 DrawAllocator::Iter drawIter(&fDraws);
521 while (drawIter.next()) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000522 // we always have a VB, but not always an IB
bsalomonbce3d6d2014-07-02 07:54:42 -0700523 SkASSERT(NULL != drawIter->fVertexBuffer);
524 drawIter->fVertexBuffer->unref();
525 SkSafeUnref(drawIter->fIndexBuffer);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000526 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000527 fCmds.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000528 fDraws.reset();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000529 fStencilPaths.reset();
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000530 fDrawPath.reset();
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000531 fDrawPaths.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000532 fStates.reset();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000533 fClears.reset();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000534 fVertexPool.reset();
535 fIndexPool.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000536 fClips.reset();
bsalomon@google.com116ad842013-04-09 15:38:19 +0000537 fCopySurfaces.reset();
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000538 fGpuCmdMarkers.reset();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000539 fClipSet = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000540}
541
robertphillips@google.com1267fbd2013-07-03 18:37:27 +0000542void GrInOrderDrawBuffer::flush() {
543 if (fFlushing) {
544 return;
545 }
546
jvanverth8e80d172014-06-19 12:01:10 -0700547 this->getContext()->getFontCache()->updateTextures();
548
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000549 SkASSERT(kReserved_GeometrySrcType != this->getGeomSrc().fVertexSrc);
550 SkASSERT(kReserved_GeometrySrcType != this->getGeomSrc().fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000551
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000552 int numCmds = fCmds.count();
bsalomon@google.com358e4272013-01-10 14:40:28 +0000553 if (0 == numCmds) {
robertphillips@google.com1267fbd2013-07-03 18:37:27 +0000554 return;
reed@google.comac10a2d2010-12-22 21:39:39 +0000555 }
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000556
557 GrAutoTRestore<bool> flushRestore(&fFlushing);
558 fFlushing = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000559
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000560 fVertexPool.unmap();
561 fIndexPool.unmap();
reed@google.comac10a2d2010-12-22 21:39:39 +0000562
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000563 GrDrawTarget::AutoClipRestore acr(fDstGpu);
564 AutoGeometryAndStatePush agasp(fDstGpu, kPreserve_ASRInit);
bsalomon@google.comca432082013-01-23 19:53:46 +0000565
bsalomona70353e2014-07-07 08:15:07 -0700566 GrDrawState* prevDrawState = SkRef(fDstGpu->drawState());
reed@google.comac10a2d2010-12-22 21:39:39 +0000567
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000568 GrClipData clipData;
569
bsalomonbce3d6d2014-07-02 07:54:42 -0700570 StateAllocator::Iter stateIter(&fStates);
571 ClipAllocator::Iter clipIter(&fClips);
bsalomonbce3d6d2014-07-02 07:54:42 -0700572 ClearAllocator::Iter clearIter(&fClears);
573 DrawAllocator::Iter drawIter(&fDraws);
574 StencilPathAllocator::Iter stencilPathIter(&fStencilPaths);
575 DrawPathAllocator::Iter drawPathIter(&fDrawPath);
576 DrawPathsAllocator::Iter drawPathsIter(&fDrawPaths);
577 CopySurfaceAllocator::Iter copySurfaceIter(&fCopySurfaces);
578
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000579 int currCmdMarker = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000580
egdaniel3eee3832014-06-18 13:09:11 -0700581 fDstGpu->saveActiveTraceMarkers();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000582 for (int c = 0; c < numCmds; ++c) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000583 GrGpuTraceMarker newMarker("", -1);
egdanield78a1682014-07-09 10:41:26 -0700584 SkString traceString;
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000585 if (cmd_has_trace_marker(fCmds[c])) {
egdanield78a1682014-07-09 10:41:26 -0700586 traceString = fGpuCmdMarkers[currCmdMarker].toString();
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000587 newMarker.fMarker = traceString.c_str();
588 fDstGpu->addGpuTraceMarker(&newMarker);
589 ++currCmdMarker;
590 }
591 switch (strip_trace_bit(fCmds[c])) {
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000592 case kDraw_Cmd: {
bsalomona70353e2014-07-07 08:15:07 -0700593 SkASSERT(fDstGpu->drawState() != prevDrawState);
bsalomonbce3d6d2014-07-02 07:54:42 -0700594 SkAssertResult(drawIter.next());
595 fDstGpu->setVertexSourceToBuffer(drawIter->fVertexBuffer);
596 if (drawIter->isIndexed()) {
597 fDstGpu->setIndexSourceToBuffer(drawIter->fIndexBuffer);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000598 }
bsalomonbce3d6d2014-07-02 07:54:42 -0700599 fDstGpu->executeDraw(*drawIter);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000600 break;
601 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000602 case kStencilPath_Cmd: {
bsalomona70353e2014-07-07 08:15:07 -0700603 SkASSERT(fDstGpu->drawState() != prevDrawState);
bsalomonbce3d6d2014-07-02 07:54:42 -0700604 SkAssertResult(stencilPathIter.next());
605 fDstGpu->stencilPath(stencilPathIter->fPath.get(), stencilPathIter->fFill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000606 break;
607 }
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000608 case kDrawPath_Cmd: {
bsalomona70353e2014-07-07 08:15:07 -0700609 SkASSERT(fDstGpu->drawState() != prevDrawState);
bsalomonbce3d6d2014-07-02 07:54:42 -0700610 SkAssertResult(drawPathIter.next());
611 fDstGpu->executeDrawPath(drawPathIter->fPath.get(), drawPathIter->fFill,
612 NULL != drawPathIter->fDstCopy.texture() ?
613 &drawPathIter->fDstCopy :
614 NULL);
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000615 break;
616 }
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000617 case kDrawPaths_Cmd: {
bsalomona70353e2014-07-07 08:15:07 -0700618 SkASSERT(fDstGpu->drawState() != prevDrawState);
bsalomonbce3d6d2014-07-02 07:54:42 -0700619 SkAssertResult(drawPathsIter.next());
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000620 const GrDeviceCoordTexture* dstCopy =
bsalomonbce3d6d2014-07-02 07:54:42 -0700621 NULL !=drawPathsIter->fDstCopy.texture() ? &drawPathsIter->fDstCopy : NULL;
cdaltonb85a0aa2014-07-21 15:32:44 -0700622 fDstGpu->executeDrawPaths(drawPathsIter->fPathRange.get(),
623 drawPathsIter->fIndices,
624 drawPathsIter->fCount,
625 drawPathsIter->fTransforms,
626 drawPathsIter->fTransformsType,
627 drawPathsIter->fFill,
628 dstCopy);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000629 break;
630 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000631 case kSetState_Cmd:
bsalomonbce3d6d2014-07-02 07:54:42 -0700632 SkAssertResult(stateIter.next());
bsalomona70353e2014-07-07 08:15:07 -0700633 fDstGpu->setDrawState(stateIter.get());
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000634 break;
635 case kSetClip_Cmd:
bsalomonbce3d6d2014-07-02 07:54:42 -0700636 SkAssertResult(clipIter.next());
bsalomonf0480b12014-07-02 12:11:24 -0700637 clipData.fClipStack = &clipIter->fStack;
638 clipData.fOrigin = clipIter->fOrigin;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000639 fDstGpu->setClip(&clipData);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000640 break;
641 case kClear_Cmd:
bsalomonbce3d6d2014-07-02 07:54:42 -0700642 SkAssertResult(clearIter.next());
643 if (GrColor_ILLEGAL == clearIter->fColor) {
644 fDstGpu->discard(clearIter->fRenderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000645 } else {
bsalomonbce3d6d2014-07-02 07:54:42 -0700646 fDstGpu->clear(&clearIter->fRect,
647 clearIter->fColor,
648 clearIter->fCanIgnoreRect,
649 clearIter->fRenderTarget);
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000650 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000651 break;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000652 case kCopySurface_Cmd:
bsalomonbce3d6d2014-07-02 07:54:42 -0700653 SkAssertResult(copySurfaceIter.next());
654 fDstGpu->copySurface(copySurfaceIter->fDst.get(),
655 copySurfaceIter->fSrc.get(),
656 copySurfaceIter->fSrcRect,
657 copySurfaceIter->fDstPoint);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000658 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000659 }
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000660 if (cmd_has_trace_marker(fCmds[c])) {
661 fDstGpu->removeGpuTraceMarker(&newMarker);
662 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000663 }
egdaniel3eee3832014-06-18 13:09:11 -0700664 fDstGpu->restoreActiveTraceMarkers();
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000665 // we should have consumed all the states, clips, etc.
bsalomonbce3d6d2014-07-02 07:54:42 -0700666 SkASSERT(!stateIter.next());
667 SkASSERT(!clipIter.next());
bsalomonbce3d6d2014-07-02 07:54:42 -0700668 SkASSERT(!clearIter.next());
669 SkASSERT(!drawIter.next());
670 SkASSERT(!copySurfaceIter.next());
671 SkASSERT(!stencilPathIter.next());
672 SkASSERT(!drawPathIter.next());
673 SkASSERT(!drawPathsIter.next());
674
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000675 SkASSERT(fGpuCmdMarkers.count() == currCmdMarker);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000676
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000677 fDstGpu->setDrawState(prevDrawState);
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000678 prevDrawState->unref();
bsalomon@google.com55e4a202013-01-11 13:54:21 +0000679 this->reset();
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000680 ++fDrawID;
reed@google.comac10a2d2010-12-22 21:39:39 +0000681}
682
bsalomon@google.com116ad842013-04-09 15:38:19 +0000683bool GrInOrderDrawBuffer::onCopySurface(GrSurface* dst,
684 GrSurface* src,
685 const SkIRect& srcRect,
686 const SkIPoint& dstPoint) {
687 if (fDstGpu->canCopySurface(dst, src, srcRect, dstPoint)) {
688 CopySurface* cs = this->recordCopySurface();
689 cs->fDst.reset(SkRef(dst));
690 cs->fSrc.reset(SkRef(src));
691 cs->fSrcRect = srcRect;
692 cs->fDstPoint = dstPoint;
693 return true;
694 } else {
695 return false;
696 }
697}
698
699bool GrInOrderDrawBuffer::onCanCopySurface(GrSurface* dst,
700 GrSurface* src,
701 const SkIRect& srcRect,
702 const SkIPoint& dstPoint) {
703 return fDstGpu->canCopySurface(dst, src, srcRect, dstPoint);
704}
705
bsalomon@google.comeb851172013-04-15 13:51:00 +0000706void GrInOrderDrawBuffer::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
707 fDstGpu->initCopySurfaceDstDesc(src, desc);
708}
709
robertphillips@google.com9528bdb2013-09-18 22:33:57 +0000710void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace(int vertexCount,
711 int indexCount) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000712 // We use geometryHints() to know whether to flush the draw buffer. We
713 // can't flush if we are inside an unbalanced pushGeometrySource.
714 // Moreover, flushing blows away vertex and index data that was
715 // previously reserved. So if the vertex or index data is pulled from
716 // reserved space and won't be released by this request then we can't
717 // flush.
718 bool insideGeoPush = fGeoPoolStateStack.count() > 1;
bsalomon@google.com97805382012-03-13 14:32:07 +0000719
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000720 bool unreleasedVertexSpace =
721 !vertexCount &&
722 kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc;
bsalomon@google.com97805382012-03-13 14:32:07 +0000723
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000724 bool unreleasedIndexSpace =
725 !indexCount &&
726 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
bsalomon@google.com97805382012-03-13 14:32:07 +0000727
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000728 // we don't want to finalize any reserved geom on the target since
729 // we don't know that the client has finished writing to it.
730 bool targetHasReservedGeom = fDstGpu->hasReservedVerticesOrIndices();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000731
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000732 int vcount = vertexCount;
733 int icount = indexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000734
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000735 if (!insideGeoPush &&
736 !unreleasedVertexSpace &&
737 !unreleasedIndexSpace &&
738 !targetHasReservedGeom &&
739 this->geometryHints(&vcount, &icount)) {
bsalomon@google.com97805382012-03-13 14:32:07 +0000740
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000741 this->flush();
bsalomon@google.com97805382012-03-13 14:32:07 +0000742 }
743}
744
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000745bool GrInOrderDrawBuffer::geometryHints(int* vertexCount,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000746 int* indexCount) const {
747 // we will recommend a flush if the data could fit in a single
748 // preallocated buffer but none are left and it can't fit
749 // in the current buffer (which may not be prealloced).
reed@google.comac10a2d2010-12-22 21:39:39 +0000750 bool flush = false;
751 if (NULL != indexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000752 int32_t currIndices = fIndexPool.currentBufferIndices();
753 if (*indexCount > currIndices &&
754 (!fIndexPool.preallocatedBuffersRemaining() &&
755 *indexCount <= fIndexPool.preallocatedBufferIndices())) {
756
757 flush = true;
758 }
759 *indexCount = currIndices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000760 }
761 if (NULL != vertexCount) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000762 size_t vertexSize = this->getDrawState().getVertexSize();
jvanverth@google.coma6338982013-01-31 21:34:25 +0000763 int32_t currVertices = fVertexPool.currentBufferVertices(vertexSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000764 if (*vertexCount > currVertices &&
765 (!fVertexPool.preallocatedBuffersRemaining() &&
jvanverth@google.coma6338982013-01-31 21:34:25 +0000766 *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexSize))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000767
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000768 flush = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000769 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000770 *vertexCount = currVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000771 }
772 return flush;
773}
774
jvanverth@google.coma6338982013-01-31 21:34:25 +0000775bool GrInOrderDrawBuffer::onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000776 int vertexCount,
777 void** vertices) {
778 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000779 SkASSERT(vertexCount > 0);
780 SkASSERT(NULL != vertices);
781 SkASSERT(0 == poolState.fUsedPoolVertexBytes);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000782
jvanverth@google.coma6338982013-01-31 21:34:25 +0000783 *vertices = fVertexPool.makeSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000784 vertexCount,
785 &poolState.fPoolVertexBuffer,
786 &poolState.fPoolStartVertex);
787 return NULL != *vertices;
788}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000789
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000790bool GrInOrderDrawBuffer::onReserveIndexSpace(int indexCount, void** indices) {
791 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000792 SkASSERT(indexCount > 0);
793 SkASSERT(NULL != indices);
794 SkASSERT(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000795
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000796 *indices = fIndexPool.makeSpace(indexCount,
797 &poolState.fPoolIndexBuffer,
798 &poolState.fPoolStartIndex);
799 return NULL != *indices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000800}
801
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000802void GrInOrderDrawBuffer::releaseReservedVertexSpace() {
803 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000804 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000805
806 // If we get a release vertex space call then our current source should either be reserved
807 // or array (which we copied into reserved space).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000808 SkASSERT(kReserved_GeometrySrcType == geoSrc.fVertexSrc ||
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000809 kArray_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000810
811 // When the caller reserved vertex buffer space we gave it back a pointer
812 // provided by the vertex buffer pool. At each draw we tracked the largest
813 // offset into the pool's pointer that was referenced. Now we return to the
814 // pool any portion at the tail of the allocation that no draw referenced.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000815 size_t reservedVertexBytes = geoSrc.fVertexSize * geoSrc.fVertexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000816 fVertexPool.putBack(reservedVertexBytes -
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000817 poolState.fUsedPoolVertexBytes);
818 poolState.fUsedPoolVertexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000819 poolState.fPoolVertexBuffer = NULL;
820 poolState.fPoolStartVertex = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000821}
822
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000823void GrInOrderDrawBuffer::releaseReservedIndexSpace() {
824 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000825 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000826
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000827 // If we get a release index space call then our current source should either be reserved
828 // or array (which we copied into reserved space).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000829 SkASSERT(kReserved_GeometrySrcType == geoSrc.fIndexSrc ||
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000830 kArray_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000831
832 // Similar to releaseReservedVertexSpace we return any unused portion at
833 // the tail
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000834 size_t reservedIndexBytes = sizeof(uint16_t) * geoSrc.fIndexCount;
835 fIndexPool.putBack(reservedIndexBytes - poolState.fUsedPoolIndexBytes);
836 poolState.fUsedPoolIndexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000837 poolState.fPoolIndexBuffer = NULL;
838 poolState.fPoolStartIndex = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000839}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000840
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000841void GrInOrderDrawBuffer::onSetVertexSourceToArray(const void* vertexArray,
842 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000843
844 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000845 SkASSERT(0 == poolState.fUsedPoolVertexBytes);
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000846#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000847 bool success =
848#endif
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000849 fVertexPool.appendVertices(this->getVertexSize(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000850 vertexCount,
851 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000852 &poolState.fPoolVertexBuffer,
853 &poolState.fPoolStartVertex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000854 GR_DEBUGASSERT(success);
855}
856
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000857void GrInOrderDrawBuffer::onSetIndexSourceToArray(const void* indexArray,
858 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000859 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000860 SkASSERT(0 == poolState.fUsedPoolIndexBytes);
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000861#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000862 bool success =
863#endif
864 fIndexPool.appendIndices(indexCount,
865 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000866 &poolState.fPoolIndexBuffer,
867 &poolState.fPoolStartIndex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000868 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000869}
870
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000871void GrInOrderDrawBuffer::releaseVertexArray() {
872 // When the client provides an array as the vertex source we handled it
873 // by copying their array into reserved space.
874 this->GrInOrderDrawBuffer::releaseReservedVertexSpace();
875}
876
877void GrInOrderDrawBuffer::releaseIndexArray() {
878 // When the client provides an array as the index source we handled it
879 // by copying their array into reserved space.
880 this->GrInOrderDrawBuffer::releaseReservedIndexSpace();
881}
882
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000883void GrInOrderDrawBuffer::geometrySourceWillPush() {
884 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
885 poolState.fUsedPoolVertexBytes = 0;
886 poolState.fUsedPoolIndexBytes = 0;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000887#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000888 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
889 poolState.fPoolStartVertex = ~0;
890 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
891 poolState.fPoolStartIndex = ~0;
892#endif
893}
894
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000895void GrInOrderDrawBuffer::geometrySourceWillPop(
896 const GeometrySrcState& restoredState) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000897 SkASSERT(fGeoPoolStateStack.count() > 1);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000898 fGeoPoolStateStack.pop_back();
899 GeometryPoolState& poolState = fGeoPoolStateStack.back();
900 // we have to assume that any slack we had in our vertex/index data
901 // is now unreleasable because data may have been appended later in the
902 // pool.
903 if (kReserved_GeometrySrcType == restoredState.fVertexSrc ||
904 kArray_GeometrySrcType == restoredState.fVertexSrc) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000905 poolState.fUsedPoolVertexBytes = restoredState.fVertexSize * restoredState.fVertexCount;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000906 }
907 if (kReserved_GeometrySrcType == restoredState.fIndexSrc ||
908 kArray_GeometrySrcType == restoredState.fIndexSrc) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000909 poolState.fUsedPoolIndexBytes = sizeof(uint16_t) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000910 restoredState.fIndexCount;
911 }
912}
913
bsalomon838f62d2014-08-05 07:15:57 -0700914void GrInOrderDrawBuffer::recordStateIfNecessary() {
915 if (fStates.empty()) {
916 fStates.push_back() = this->getDrawState();
917 this->addToCmdBuffer(kSetState_Cmd);
918 return;
919 }
920 const GrDrawState& curr = this->getDrawState();
921 GrDrawState& prev = fStates.back();
922 switch (GrDrawState::CombineIfPossible(prev, curr)) {
923 case GrDrawState::kIncompatible_CombinedState:
924 fStates.push_back() = this->getDrawState();
925 this->addToCmdBuffer(kSetState_Cmd);
926 break;
927 case GrDrawState::kA_CombinedState:
928 case GrDrawState::kAOrB_CombinedState: // Treat the same as kA.
929 break;
930 case GrDrawState::kB_CombinedState:
931 prev = curr;
932 break;
933 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000934}
935
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000936bool GrInOrderDrawBuffer::needsNewClip() const {
bsalomon@google.com358e4272013-01-10 14:40:28 +0000937 if (this->getDrawState().isClipState()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000938 if (fClipSet &&
bsalomon@google.com358e4272013-01-10 14:40:28 +0000939 (fClips.empty() ||
bsalomonf0480b12014-07-02 12:11:24 -0700940 fClips.back().fStack != *this->getClip()->fClipStack ||
941 fClips.back().fOrigin != this->getClip()->fOrigin)) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000942 return true;
943 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000944 }
945 return false;
946}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000947
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000948void GrInOrderDrawBuffer::addToCmdBuffer(uint8_t cmd) {
949 SkASSERT(!cmd_has_trace_marker(cmd));
950 const GrTraceMarkerSet& activeTraceMarkers = this->getActiveTraceMarkers();
951 if (activeTraceMarkers.count() > 0) {
952 fCmds.push_back(add_trace_bit(cmd));
953 fGpuCmdMarkers.push_back(activeTraceMarkers);
954 } else {
955 fCmds.push_back(cmd);
956 }
957}
958
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000959void GrInOrderDrawBuffer::recordClip() {
bsalomonf0480b12014-07-02 12:11:24 -0700960 fClips.push_back().fStack = *this->getClip()->fClipStack;
961 fClips.back().fOrigin = this->getClip()->fOrigin;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000962 fClipSet = false;
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000963 this->addToCmdBuffer(kSetClip_Cmd);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000964}
965
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000966GrInOrderDrawBuffer::DrawRecord* GrInOrderDrawBuffer::recordDraw(const DrawInfo& info) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000967 this->addToCmdBuffer(kDraw_Cmd);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000968 return &fDraws.push_back(info);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000969}
970
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000971GrInOrderDrawBuffer::StencilPath* GrInOrderDrawBuffer::recordStencilPath() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000972 this->addToCmdBuffer(kStencilPath_Cmd);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000973 return &fStencilPaths.push_back();
974}
975
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000976GrInOrderDrawBuffer::DrawPath* GrInOrderDrawBuffer::recordDrawPath() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000977 this->addToCmdBuffer(kDrawPath_Cmd);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000978 return &fDrawPath.push_back();
979}
980
981GrInOrderDrawBuffer::DrawPaths* GrInOrderDrawBuffer::recordDrawPaths() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000982 this->addToCmdBuffer(kDrawPaths_Cmd);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000983 return &fDrawPaths.push_back();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000984}
985
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000986GrInOrderDrawBuffer::Clear* GrInOrderDrawBuffer::recordClear() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000987 this->addToCmdBuffer(kClear_Cmd);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000988 return &fClears.push_back();
reed@google.comac10a2d2010-12-22 21:39:39 +0000989}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000990
bsalomon@google.com116ad842013-04-09 15:38:19 +0000991GrInOrderDrawBuffer::CopySurface* GrInOrderDrawBuffer::recordCopySurface() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000992 this->addToCmdBuffer(kCopySurface_Cmd);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000993 return &fCopySurfaces.push_back();
994}
995
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000996void GrInOrderDrawBuffer::clipWillBeSet(const GrClipData* newClipData) {
997 INHERITED::clipWillBeSet(newClipData);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000998 fClipSet = true;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000999 fClipProxyState = kUnknown_ClipProxyState;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +00001000}