blob: 4931f47db7bbfe4636b21427905492cadc16eaa1 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#include "GrInOrderDrawBuffer.h"
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00009
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000010#include "GrBufferAllocPool.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000011#include "GrDrawTargetCaps.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000012#include "GrGpu.h"
13#include "GrIndexBuffer.h"
14#include "GrPath.h"
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000015#include "GrPoint.h"
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +000016#include "GrRenderTarget.h"
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000017#include "GrTemplates.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018#include "GrTexture.h"
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000019#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000020
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000021GrInOrderDrawBuffer::GrInOrderDrawBuffer(GrGpu* gpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +000022 GrVertexBufferAllocPool* vertexPool,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000023 GrIndexBufferAllocPool* indexPool)
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000024 : GrDrawTarget(gpu->getContext())
25 , fDstGpu(gpu)
bsalomon@google.com97805382012-03-13 14:32:07 +000026 , fClipSet(true)
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000027 , fClipProxyState(kUnknown_ClipProxyState)
robertphillips@google.com69705572012-03-21 19:46:50 +000028 , fVertexPool(*vertexPool)
29 , fIndexPool(*indexPool)
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000030 , fFlushing(false)
31 , fDrawID(0) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +000032
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000033 fDstGpu->ref();
bsalomon@google.combcce8922013-03-25 15:38:39 +000034 fCaps.reset(SkRef(fDstGpu->caps()));
bsalomon@google.com18c9c192011-09-22 21:01:31 +000035
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000036 SkASSERT(NULL != vertexPool);
37 SkASSERT(NULL != indexPool);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000038
39 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
40 poolState.fUsedPoolVertexBytes = 0;
41 poolState.fUsedPoolIndexBytes = 0;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000042#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000043 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
44 poolState.fPoolStartVertex = ~0;
45 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
46 poolState.fPoolStartIndex = ~0;
47#endif
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000048 this->reset();
reed@google.comac10a2d2010-12-22 21:39:39 +000049}
50
51GrInOrderDrawBuffer::~GrInOrderDrawBuffer() {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000052 this->reset();
bsalomon@google.com4a018bb2011-10-28 19:50:21 +000053 // This must be called by before the GrDrawTarget destructor
54 this->releaseGeometry();
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000055 fDstGpu->unref();
reed@google.comac10a2d2010-12-22 21:39:39 +000056}
57
bsalomon@google.com934c5702012-03-20 21:17:58 +000058////////////////////////////////////////////////////////////////////////////////
59
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000060namespace {
61void get_vertex_bounds(const void* vertices,
62 size_t vertexSize,
63 int vertexCount,
64 SkRect* bounds) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000065 SkASSERT(vertexSize >= sizeof(SkPoint));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000066 SkASSERT(vertexCount > 0);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000067 const SkPoint* point = static_cast<const SkPoint*>(vertices);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000068 bounds->fLeft = bounds->fRight = point->fX;
69 bounds->fTop = bounds->fBottom = point->fY;
70 for (int i = 1; i < vertexCount; ++i) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000071 point = reinterpret_cast<SkPoint*>(reinterpret_cast<intptr_t>(point) + vertexSize);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000072 bounds->growToInclude(point->fX, point->fY);
73 }
74}
bsalomon@google.com934c5702012-03-20 21:17:58 +000075}
76
robertphillips@google.com42903302013-04-20 12:26:07 +000077
78namespace {
79
80extern const GrVertexAttrib kRectPosColorUVAttribs[] = {
81 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000082 {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kColor_GrVertexAttribBinding},
83 {kVec2f_GrVertexAttribType, sizeof(SkPoint)+sizeof(GrColor),
robertphillips@google.com42903302013-04-20 12:26:07 +000084 kLocalCoord_GrVertexAttribBinding},
85};
86
87extern const GrVertexAttrib kRectPosUVAttribs[] = {
88 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000089 {kVec2f_GrVertexAttribType, sizeof(SkPoint), kLocalCoord_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +000090};
91
92static void set_vertex_attributes(GrDrawState* drawState,
93 bool hasColor, bool hasUVs,
94 int* colorOffset, int* localOffset) {
95 *colorOffset = -1;
96 *localOffset = -1;
97
98 // Using per-vertex colors allows batching across colors. (A lot of rects in a row differing
99 // only in color is a common occurrence in tables). However, having per-vertex colors disables
100 // blending optimizations because we don't know if the color will be solid or not. These
101 // optimizations help determine whether coverage and color can be blended correctly when
102 // dual-source blending isn't available. This comes into play when there is coverage. If colors
103 // were a stage it could take a hint that every vertex's color will be opaque.
104 if (hasColor && hasUVs) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000105 *colorOffset = sizeof(SkPoint);
106 *localOffset = sizeof(SkPoint) + sizeof(GrColor);
robertphillips@google.com42903302013-04-20 12:26:07 +0000107 drawState->setVertexAttribs<kRectPosColorUVAttribs>(3);
108 } else if (hasColor) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000109 *colorOffset = sizeof(SkPoint);
robertphillips@google.com42903302013-04-20 12:26:07 +0000110 drawState->setVertexAttribs<kRectPosColorUVAttribs>(2);
111 } else if (hasUVs) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000112 *localOffset = sizeof(SkPoint);
robertphillips@google.com42903302013-04-20 12:26:07 +0000113 drawState->setVertexAttribs<kRectPosUVAttribs>(2);
114 } else {
115 drawState->setVertexAttribs<kRectPosUVAttribs>(1);
116 }
117}
118
119};
120
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000121enum {
122 kTraceCmdBit = 0x80,
123 kCmdMask = 0x7f,
124};
125
126static uint8_t add_trace_bit(uint8_t cmd) {
127 return cmd | kTraceCmdBit;
128}
129
130static uint8_t strip_trace_bit(uint8_t cmd) {
131 return cmd & kCmdMask;
132}
133
134static bool cmd_has_trace_marker(uint8_t cmd) {
commit-bot@chromium.orge6984a82014-03-25 15:49:59 +0000135 return SkToBool(cmd & kTraceCmdBit);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000136}
137
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000138void GrInOrderDrawBuffer::onDrawRect(const SkRect& rect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000139 const SkMatrix* matrix,
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
169 SkMatrix combinedMatrix;
170 if (NULL != matrix) {
171 combinedMatrix = *matrix;
172 } else {
173 combinedMatrix.reset();
174 }
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000175 combinedMatrix.postConcat(drawState->getViewMatrix());
jvanverth@google.com39768252013-02-14 15:25:44 +0000176 // 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 +0000177 // modify that stage's matrix. Otherwise if the effect is generating its source rect from
178 // the vertex positions then we have to account for the view matrix change.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000179 GrDrawState::AutoViewMatrixRestore avmr;
180 if (!avmr.setIdentity(drawState)) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000181 return;
182 }
183
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000184 size_t vsize = drawState->getVertexSize();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000185
186 geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom, vsize);
187 combinedMatrix.mapPointsWithStride(geo.positions(), vsize, 4);
188
189 SkRect devBounds;
190 // since we already computed the dev verts, set the bounds hint. This will help us avoid
191 // unnecessary clipping in our onDraw().
192 get_vertex_bounds(geo.vertices(), vsize, 4, &devBounds);
193
bsalomon@google.comc7818882013-03-20 19:19:53 +0000194 if (localOffset >= 0) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000195 SkPoint* coords = GrTCast<SkPoint*>(GrTCast<intptr_t>(geo.vertices()) + localOffset);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000196 coords->setRectFan(localRect->fLeft, localRect->fTop,
197 localRect->fRight, localRect->fBottom,
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000198 vsize);
bsalomon@google.comc7818882013-03-20 19:19:53 +0000199 if (NULL != localMatrix) {
200 localMatrix->mapPointsWithStride(coords, vsize, 4);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000201 }
202 }
203
204 if (colorOffset >= 0) {
205 GrColor* vertColor = GrTCast<GrColor*>(GrTCast<intptr_t>(geo.vertices()) + colorOffset);
206 for (int i = 0; i < 4; ++i) {
207 *vertColor = color;
208 vertColor = (GrColor*) ((intptr_t) vertColor + vsize);
209 }
210 }
211
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000212 this->setIndexSourceToBuffer(this->getContext()->getQuadIndexBuffer());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000213 this->drawIndexedInstances(kTriangles_GrPrimitiveType, 1, 4, 6, &devBounds);
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000214
215 // to ensure that stashing the drawState ptr is valid
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000216 SkASSERT(this->drawState() == drawState);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000217}
218
219bool GrInOrderDrawBuffer::quickInsideClip(const SkRect& devBounds) {
220 if (!this->getDrawState().isClipState()) {
221 return true;
222 }
223 if (kUnknown_ClipProxyState == fClipProxyState) {
224 SkIRect rect;
225 bool iior;
226 this->getClip()->getConservativeBounds(this->getDrawState().getRenderTarget(), &rect, &iior);
227 if (iior) {
228 // The clip is a rect. We will remember that in fProxyClip. It is common for an edge (or
229 // all edges) of the clip to be at the edge of the RT. However, we get that clipping for
230 // free via the viewport. We don't want to think that clipping must be enabled in this
231 // case. So we extend the clip outward from the edge to avoid these false negatives.
232 fClipProxyState = kValid_ClipProxyState;
reed@google.com44699382013-10-31 17:28:30 +0000233 fClipProxy = SkRect::Make(rect);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000234
235 if (fClipProxy.fLeft <= 0) {
236 fClipProxy.fLeft = SK_ScalarMin;
237 }
238 if (fClipProxy.fTop <= 0) {
239 fClipProxy.fTop = SK_ScalarMin;
240 }
241 if (fClipProxy.fRight >= this->getDrawState().getRenderTarget()->width()) {
242 fClipProxy.fRight = SK_ScalarMax;
243 }
244 if (fClipProxy.fBottom >= this->getDrawState().getRenderTarget()->height()) {
245 fClipProxy.fBottom = SK_ScalarMax;
246 }
247 } else {
248 fClipProxyState = kInvalid_ClipProxyState;
249 }
250 }
251 if (kValid_ClipProxyState == fClipProxyState) {
252 return fClipProxy.contains(devBounds);
253 }
254 SkPoint originOffset = {SkIntToScalar(this->getClip()->fOrigin.fX),
255 SkIntToScalar(this->getClip()->fOrigin.fY)};
256 SkRect clipSpaceBounds = devBounds;
257 clipSpaceBounds.offset(originOffset);
258 return this->getClip()->fClipStack->quickContains(clipSpaceBounds);
259}
260
261int GrInOrderDrawBuffer::concatInstancedDraw(const DrawInfo& info) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000262 SkASSERT(info.isInstanced());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000263
bsalomon@google.com934c5702012-03-20 21:17:58 +0000264 const GeometrySrcState& geomSrc = this->getGeomSrc();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000265 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000266
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000267 // we only attempt to concat the case when reserved verts are used with a client-specified index
268 // buffer. To make this work with client-specified VBs we'd need to know if the VB was updated
269 // between draws.
270 if (kReserved_GeometrySrcType != geomSrc.fVertexSrc ||
271 kBuffer_GeometrySrcType != geomSrc.fIndexSrc) {
272 return 0;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000273 }
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000274 // Check if there is a draw info that is compatible that uses the same VB from the pool and
275 // the same IB
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000276 if (kDraw_Cmd != strip_trace_bit(fCmds.back())) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000277 return 0;
278 }
279
280 DrawRecord* draw = &fDraws.back();
281 GeometryPoolState& poolState = fGeoPoolStateStack.back();
282 const GrVertexBuffer* vertexBuffer = poolState.fPoolVertexBuffer;
283
284 if (!draw->isInstanced() ||
285 draw->verticesPerInstance() != info.verticesPerInstance() ||
286 draw->indicesPerInstance() != info.indicesPerInstance() ||
287 draw->fVertexBuffer != vertexBuffer ||
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000288 draw->fIndexBuffer != geomSrc.fIndexBuffer) {
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000289 return 0;
290 }
291 // info does not yet account for the offset from the start of the pool's VB while the previous
292 // draw record does.
293 int adjustedStartVertex = poolState.fPoolStartVertex + info.startVertex();
294 if (draw->startVertex() + draw->vertexCount() != adjustedStartVertex) {
295 return 0;
296 }
297
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000298 SkASSERT(poolState.fPoolStartVertex == draw->startVertex() + draw->vertexCount());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000299
300 // how many instances can be concat'ed onto draw given the size of the index buffer
301 int instancesToConcat = this->indexCountInCurrentSource() / info.indicesPerInstance();
302 instancesToConcat -= draw->instanceCount();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000303 instancesToConcat = SkTMin(instancesToConcat, info.instanceCount());
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000304
305 // update the amount of reserved vertex data actually referenced in draws
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000306 size_t vertexBytes = instancesToConcat * info.verticesPerInstance() *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000307 drawState.getVertexSize();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000308 poolState.fUsedPoolVertexBytes = SkTMax(poolState.fUsedPoolVertexBytes, vertexBytes);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000309
310 draw->adjustInstanceCount(instancesToConcat);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000311
312 // update last fGpuCmdMarkers to include any additional trace markers that have been added
313 if (this->getActiveTraceMarkers().count() > 0) {
314 if (cmd_has_trace_marker(fCmds.back())) {
315 fGpuCmdMarkers.back().addSet(this->getActiveTraceMarkers());
316 } else {
317 fGpuCmdMarkers.push_back(this->getActiveTraceMarkers());
318 fCmds.back() = add_trace_bit(fCmds.back());
319 }
320 }
321
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000322 return instancesToConcat;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000323}
324
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000325class AutoClipReenable {
326public:
327 AutoClipReenable() : fDrawState(NULL) {}
328 ~AutoClipReenable() {
329 if (NULL != fDrawState) {
330 fDrawState->enableState(GrDrawState::kClip_StateBit);
331 }
332 }
333 void set(GrDrawState* drawState) {
334 if (drawState->isClipState()) {
335 fDrawState = drawState;
336 drawState->disableState(GrDrawState::kClip_StateBit);
337 }
338 }
339private:
340 GrDrawState* fDrawState;
341};
342
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000343void GrInOrderDrawBuffer::onDraw(const DrawInfo& info) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000344
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000345 GeometryPoolState& poolState = fGeoPoolStateStack.back();
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000346 const GrDrawState& drawState = this->getDrawState();
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000347 AutoClipReenable acr;
348
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000349 if (drawState.isClipState() &&
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000350 NULL != info.getDevBounds() &&
351 this->quickInsideClip(*info.getDevBounds())) {
352 acr.set(this->drawState());
353 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000354
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000355 if (this->needsNewClip()) {
356 this->recordClip();
357 }
358 if (this->needsNewState()) {
359 this->recordState();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000360 }
361
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000362 DrawRecord* draw;
363 if (info.isInstanced()) {
364 int instancesConcated = this->concatInstancedDraw(info);
365 if (info.instanceCount() > instancesConcated) {
366 draw = this->recordDraw(info);
367 draw->adjustInstanceCount(-instancesConcated);
368 } else {
369 return;
370 }
371 } else {
372 draw = this->recordDraw(info);
373 }
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000374
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000375 switch (this->getGeomSrc().fVertexSrc) {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000376 case kBuffer_GeometrySrcType:
377 draw->fVertexBuffer = this->getGeomSrc().fVertexBuffer;
378 break;
379 case kReserved_GeometrySrcType: // fallthrough
380 case kArray_GeometrySrcType: {
skia.committer@gmail.comae683922013-02-06 07:01:54 +0000381 size_t vertexBytes = (info.vertexCount() + info.startVertex()) *
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000382 drawState.getVertexSize();
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000383 poolState.fUsedPoolVertexBytes = SkTMax(poolState.fUsedPoolVertexBytes, vertexBytes);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000384 draw->fVertexBuffer = poolState.fPoolVertexBuffer;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000385 draw->adjustStartVertex(poolState.fPoolStartVertex);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000386 break;
387 }
388 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000389 SkFAIL("unknown geom src type");
reed@google.comac10a2d2010-12-22 21:39:39 +0000390 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000391 draw->fVertexBuffer->ref();
reed@google.comac10a2d2010-12-22 21:39:39 +0000392
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000393 if (info.isIndexed()) {
394 switch (this->getGeomSrc().fIndexSrc) {
395 case kBuffer_GeometrySrcType:
396 draw->fIndexBuffer = this->getGeomSrc().fIndexBuffer;
397 break;
398 case kReserved_GeometrySrcType: // fallthrough
399 case kArray_GeometrySrcType: {
400 size_t indexBytes = (info.indexCount() + info.startIndex()) * sizeof(uint16_t);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000401 poolState.fUsedPoolIndexBytes = SkTMax(poolState.fUsedPoolIndexBytes, indexBytes);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000402 draw->fIndexBuffer = poolState.fPoolIndexBuffer;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000403 draw->adjustStartIndex(poolState.fPoolStartIndex);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000404 break;
405 }
406 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000407 SkFAIL("unknown geom src type");
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000408 }
409 draw->fIndexBuffer->ref();
410 } else {
411 draw->fIndexBuffer = NULL;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000412 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000413}
414
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000415GrInOrderDrawBuffer::StencilPath::StencilPath() {}
416GrInOrderDrawBuffer::DrawPath::DrawPath() {}
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000417GrInOrderDrawBuffer::DrawPaths::DrawPaths() {}
418GrInOrderDrawBuffer::DrawPaths::~DrawPaths() {
419 if (fTransforms) {
420 SkDELETE_ARRAY(fTransforms);
421 }
commit-bot@chromium.orgecc45362014-03-28 21:31:34 +0000422 for (int i = 0; i < fPathCount; ++i) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000423 fPaths[i]->unref();
424 }
425 SkDELETE_ARRAY(fPaths);
426}
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
commit-bot@chromium.orgecc45362014-03-28 21:31:34 +0000460void GrInOrderDrawBuffer::onDrawPaths(int pathCount, const GrPath** paths,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000461 const SkMatrix* transforms,
462 SkPath::FillType fill,
463 SkStrokeRec::Style stroke,
464 const GrDeviceCoordTexture* dstCopy) {
465 SkASSERT(pathCount);
466
467 if (this->needsNewClip()) {
468 this->recordClip();
469 }
470 if (this->needsNewState()) {
471 this->recordState();
472 }
473 DrawPaths* dp = this->recordDrawPaths();
474 dp->fPathCount = pathCount;
475 dp->fPaths = SkNEW_ARRAY(const GrPath*, pathCount);
476 memcpy(dp->fPaths, paths, sizeof(GrPath*) * pathCount);
commit-bot@chromium.orgecc45362014-03-28 21:31:34 +0000477 for (int i = 0; i < pathCount; ++i) {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000478 dp->fPaths[i]->ref();
479 }
480
481 dp->fTransforms = SkNEW_ARRAY(SkMatrix, pathCount);
482 memcpy(dp->fTransforms, transforms, sizeof(SkMatrix) * pathCount);
483
484 dp->fFill = fill;
485 dp->fStroke = stroke;
486
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();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000533 int numDraws = fDraws.count();
534 for (int d = 0; d < numDraws; ++d) {
535 // we always have a VB, but not always an IB
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000536 SkASSERT(NULL != fDraws[d].fVertexBuffer);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000537 fDraws[d].fVertexBuffer->unref();
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000538 SkSafeUnref(fDraws[d].fIndexBuffer);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000539 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000540 fCmds.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000541 fDraws.reset();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000542 fStencilPaths.reset();
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000543 fDrawPath.reset();
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000544 fDrawPaths.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000545 fStates.reset();
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000546 fClears.reset();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000547 fVertexPool.reset();
548 fIndexPool.reset();
reed@google.comac10a2d2010-12-22 21:39:39 +0000549 fClips.reset();
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000550 fClipOrigins.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
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000561 SkASSERT(kReserved_GeometrySrcType != this->getGeomSrc().fVertexSrc);
562 SkASSERT(kReserved_GeometrySrcType != this->getGeomSrc().fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000563
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000564 int numCmds = fCmds.count();
bsalomon@google.com358e4272013-01-10 14:40:28 +0000565 if (0 == numCmds) {
robertphillips@google.com1267fbd2013-07-03 18:37:27 +0000566 return;
reed@google.comac10a2d2010-12-22 21:39:39 +0000567 }
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000568
569 GrAutoTRestore<bool> flushRestore(&fFlushing);
570 fFlushing = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000571
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000572 fVertexPool.unmap();
573 fIndexPool.unmap();
reed@google.comac10a2d2010-12-22 21:39:39 +0000574
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000575 GrDrawTarget::AutoClipRestore acr(fDstGpu);
576 AutoGeometryAndStatePush agasp(fDstGpu, kPreserve_ASRInit);
bsalomon@google.comca432082013-01-23 19:53:46 +0000577
578 GrDrawState playbackState;
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000579 GrDrawState* prevDrawState = fDstGpu->drawState();
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000580 prevDrawState->ref();
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000581 fDstGpu->setDrawState(&playbackState);
reed@google.comac10a2d2010-12-22 21:39:39 +0000582
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000583 GrClipData clipData;
584
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000585 int currState = 0;
586 int currClip = 0;
587 int currClear = 0;
588 int currDraw = 0;
589 int currStencilPath = 0;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000590 int currDrawPath = 0;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000591 int currDrawPaths = 0;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000592 int currCopySurface = 0;
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000593 int currCmdMarker = 0;
reed@google.comac10a2d2010-12-22 21:39:39 +0000594
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000595 for (int c = 0; c < numCmds; ++c) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000596 GrGpuTraceMarker newMarker("", -1);
597 if (cmd_has_trace_marker(fCmds[c])) {
598 SkString traceString = fGpuCmdMarkers[currCmdMarker].toString();
599 newMarker.fMarker = traceString.c_str();
600 fDstGpu->addGpuTraceMarker(&newMarker);
601 ++currCmdMarker;
602 }
603 switch (strip_trace_bit(fCmds[c])) {
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000604 case kDraw_Cmd: {
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000605 const DrawRecord& draw = fDraws[currDraw];
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000606 fDstGpu->setVertexSourceToBuffer(draw.fVertexBuffer);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000607 if (draw.isIndexed()) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000608 fDstGpu->setIndexSourceToBuffer(draw.fIndexBuffer);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000609 }
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000610 fDstGpu->executeDraw(draw);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000611 ++currDraw;
612 break;
613 }
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000614 case kStencilPath_Cmd: {
615 const StencilPath& sp = fStencilPaths[currStencilPath];
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000616 fDstGpu->stencilPath(sp.fPath.get(), sp.fFill);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000617 ++currStencilPath;
618 break;
619 }
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000620 case kDrawPath_Cmd: {
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000621 const DrawPath& cp = fDrawPath[currDrawPath];
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000622 fDstGpu->executeDrawPath(cp.fPath.get(), cp.fFill,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000623 NULL != cp.fDstCopy.texture() ? &cp.fDstCopy : NULL);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000624 ++currDrawPath;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000625 break;
626 }
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000627 case kDrawPaths_Cmd: {
628 DrawPaths& dp = fDrawPaths[currDrawPaths];
629 const GrDeviceCoordTexture* dstCopy =
630 NULL != dp.fDstCopy.texture() ? &dp.fDstCopy : NULL;
631 fDstGpu->executeDrawPaths(dp.fPathCount, dp.fPaths,
632 dp.fTransforms, dp.fFill, dp.fStroke,
633 dstCopy);
634 ++currDrawPaths;
635 break;
636 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000637 case kSetState_Cmd:
bsalomon@google.comca432082013-01-23 19:53:46 +0000638 fStates[currState].restoreTo(&playbackState);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000639 ++currState;
640 break;
641 case kSetClip_Cmd:
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000642 clipData.fClipStack = &fClips[currClip];
643 clipData.fOrigin = fClipOrigins[currClip];
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000644 fDstGpu->setClip(&clipData);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000645 ++currClip;
646 break;
647 case kClear_Cmd:
bsalomon@google.comefa85aa2014-03-31 18:03:06 +0000648 if (GrColor_ILLEGAL == fClears[currClear].fColor) {
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000649 fDstGpu->discard(fClears[currClear].fRenderTarget);
650 } else {
651 fDstGpu->clear(&fClears[currClear].fRect,
652 fClears[currClear].fColor,
653 fClears[currClear].fCanIgnoreRect,
654 fClears[currClear].fRenderTarget);
655 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000656 ++currClear;
657 break;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000658 case kCopySurface_Cmd:
659 fDstGpu->copySurface(fCopySurfaces[currCopySurface].fDst.get(),
660 fCopySurfaces[currCopySurface].fSrc.get(),
661 fCopySurfaces[currCopySurface].fSrcRect,
662 fCopySurfaces[currCopySurface].fDstPoint);
663 ++currCopySurface;
664 break;
reed@google.comac10a2d2010-12-22 21:39:39 +0000665 }
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000666 if (cmd_has_trace_marker(fCmds[c])) {
667 fDstGpu->removeGpuTraceMarker(&newMarker);
668 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000669 }
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000670 // we should have consumed all the states, clips, etc.
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000671 SkASSERT(fStates.count() == currState);
672 SkASSERT(fClips.count() == currClip);
673 SkASSERT(fClipOrigins.count() == currClip);
674 SkASSERT(fClears.count() == currClear);
675 SkASSERT(fDraws.count() == currDraw);
676 SkASSERT(fCopySurfaces.count() == currCopySurface);
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000677 SkASSERT(fGpuCmdMarkers.count() == currCmdMarker);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000678
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000679 fDstGpu->setDrawState(prevDrawState);
bsalomon@google.coma5d056a2012-03-27 15:59:58 +0000680 prevDrawState->unref();
bsalomon@google.com55e4a202013-01-11 13:54:21 +0000681 this->reset();
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000682 ++fDrawID;
reed@google.comac10a2d2010-12-22 21:39:39 +0000683}
684
bsalomon@google.com116ad842013-04-09 15:38:19 +0000685bool GrInOrderDrawBuffer::onCopySurface(GrSurface* dst,
686 GrSurface* src,
687 const SkIRect& srcRect,
688 const SkIPoint& dstPoint) {
689 if (fDstGpu->canCopySurface(dst, src, srcRect, dstPoint)) {
690 CopySurface* cs = this->recordCopySurface();
691 cs->fDst.reset(SkRef(dst));
692 cs->fSrc.reset(SkRef(src));
693 cs->fSrcRect = srcRect;
694 cs->fDstPoint = dstPoint;
695 return true;
696 } else {
697 return false;
698 }
699}
700
701bool GrInOrderDrawBuffer::onCanCopySurface(GrSurface* dst,
702 GrSurface* src,
703 const SkIRect& srcRect,
704 const SkIPoint& dstPoint) {
705 return fDstGpu->canCopySurface(dst, src, srcRect, dstPoint);
706}
707
bsalomon@google.comeb851172013-04-15 13:51:00 +0000708void GrInOrderDrawBuffer::initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) {
709 fDstGpu->initCopySurfaceDstDesc(src, desc);
710}
711
robertphillips@google.com9528bdb2013-09-18 22:33:57 +0000712void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace(int vertexCount,
713 int indexCount) {
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000714 // We use geometryHints() to know whether to flush the draw buffer. We
715 // can't flush if we are inside an unbalanced pushGeometrySource.
716 // Moreover, flushing blows away vertex and index data that was
717 // previously reserved. So if the vertex or index data is pulled from
718 // reserved space and won't be released by this request then we can't
719 // flush.
720 bool insideGeoPush = fGeoPoolStateStack.count() > 1;
bsalomon@google.com97805382012-03-13 14:32:07 +0000721
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000722 bool unreleasedVertexSpace =
723 !vertexCount &&
724 kReserved_GeometrySrcType == this->getGeomSrc().fVertexSrc;
bsalomon@google.com97805382012-03-13 14:32:07 +0000725
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000726 bool unreleasedIndexSpace =
727 !indexCount &&
728 kReserved_GeometrySrcType == this->getGeomSrc().fIndexSrc;
bsalomon@google.com97805382012-03-13 14:32:07 +0000729
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000730 // we don't want to finalize any reserved geom on the target since
731 // we don't know that the client has finished writing to it.
732 bool targetHasReservedGeom = fDstGpu->hasReservedVerticesOrIndices();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000733
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000734 int vcount = vertexCount;
735 int icount = indexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000736
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000737 if (!insideGeoPush &&
738 !unreleasedVertexSpace &&
739 !unreleasedIndexSpace &&
740 !targetHasReservedGeom &&
741 this->geometryHints(&vcount, &icount)) {
bsalomon@google.com97805382012-03-13 14:32:07 +0000742
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000743 this->flush();
bsalomon@google.com97805382012-03-13 14:32:07 +0000744 }
745}
746
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000747bool GrInOrderDrawBuffer::geometryHints(int* vertexCount,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000748 int* indexCount) const {
749 // we will recommend a flush if the data could fit in a single
750 // preallocated buffer but none are left and it can't fit
751 // in the current buffer (which may not be prealloced).
reed@google.comac10a2d2010-12-22 21:39:39 +0000752 bool flush = false;
753 if (NULL != indexCount) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000754 int32_t currIndices = fIndexPool.currentBufferIndices();
755 if (*indexCount > currIndices &&
756 (!fIndexPool.preallocatedBuffersRemaining() &&
757 *indexCount <= fIndexPool.preallocatedBufferIndices())) {
758
759 flush = true;
760 }
761 *indexCount = currIndices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000762 }
763 if (NULL != vertexCount) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000764 size_t vertexSize = this->getDrawState().getVertexSize();
jvanverth@google.coma6338982013-01-31 21:34:25 +0000765 int32_t currVertices = fVertexPool.currentBufferVertices(vertexSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000766 if (*vertexCount > currVertices &&
767 (!fVertexPool.preallocatedBuffersRemaining() &&
jvanverth@google.coma6338982013-01-31 21:34:25 +0000768 *vertexCount <= fVertexPool.preallocatedBufferVertices(vertexSize))) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000769
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000770 flush = true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000771 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000772 *vertexCount = currVertices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000773 }
774 return flush;
775}
776
jvanverth@google.coma6338982013-01-31 21:34:25 +0000777bool GrInOrderDrawBuffer::onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000778 int vertexCount,
779 void** vertices) {
780 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000781 SkASSERT(vertexCount > 0);
782 SkASSERT(NULL != vertices);
783 SkASSERT(0 == poolState.fUsedPoolVertexBytes);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000784
jvanverth@google.coma6338982013-01-31 21:34:25 +0000785 *vertices = fVertexPool.makeSpace(vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000786 vertexCount,
787 &poolState.fPoolVertexBuffer,
788 &poolState.fPoolStartVertex);
789 return NULL != *vertices;
790}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000791
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000792bool GrInOrderDrawBuffer::onReserveIndexSpace(int indexCount, void** indices) {
793 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000794 SkASSERT(indexCount > 0);
795 SkASSERT(NULL != indices);
796 SkASSERT(0 == poolState.fUsedPoolIndexBytes);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000797
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000798 *indices = fIndexPool.makeSpace(indexCount,
799 &poolState.fPoolIndexBuffer,
800 &poolState.fPoolStartIndex);
801 return NULL != *indices;
reed@google.comac10a2d2010-12-22 21:39:39 +0000802}
803
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000804void GrInOrderDrawBuffer::releaseReservedVertexSpace() {
805 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000806 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000807
808 // If we get a release vertex space call then our current source should either be reserved
809 // or array (which we copied into reserved space).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000810 SkASSERT(kReserved_GeometrySrcType == geoSrc.fVertexSrc ||
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000811 kArray_GeometrySrcType == geoSrc.fVertexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000812
813 // When the caller reserved vertex buffer space we gave it back a pointer
814 // provided by the vertex buffer pool. At each draw we tracked the largest
815 // offset into the pool's pointer that was referenced. Now we return to the
816 // pool any portion at the tail of the allocation that no draw referenced.
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000817 size_t reservedVertexBytes = geoSrc.fVertexSize * geoSrc.fVertexCount;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000818 fVertexPool.putBack(reservedVertexBytes -
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000819 poolState.fUsedPoolVertexBytes);
820 poolState.fUsedPoolVertexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000821 poolState.fPoolVertexBuffer = NULL;
822 poolState.fPoolStartVertex = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000823}
824
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000825void GrInOrderDrawBuffer::releaseReservedIndexSpace() {
826 GeometryPoolState& poolState = fGeoPoolStateStack.back();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000827 const GeometrySrcState& geoSrc = this->getGeomSrc();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000828
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000829 // If we get a release index space call then our current source should either be reserved
830 // or array (which we copied into reserved space).
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000831 SkASSERT(kReserved_GeometrySrcType == geoSrc.fIndexSrc ||
bsalomon@google.comd57d71a2012-08-16 16:26:33 +0000832 kArray_GeometrySrcType == geoSrc.fIndexSrc);
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000833
834 // Similar to releaseReservedVertexSpace we return any unused portion at
835 // the tail
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000836 size_t reservedIndexBytes = sizeof(uint16_t) * geoSrc.fIndexCount;
837 fIndexPool.putBack(reservedIndexBytes - poolState.fUsedPoolIndexBytes);
838 poolState.fUsedPoolIndexBytes = 0;
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000839 poolState.fPoolIndexBuffer = NULL;
840 poolState.fPoolStartIndex = 0;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000841}
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000842
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000843void GrInOrderDrawBuffer::onSetVertexSourceToArray(const void* vertexArray,
844 int vertexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000845
846 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000847 SkASSERT(0 == poolState.fUsedPoolVertexBytes);
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000848#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000849 bool success =
850#endif
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000851 fVertexPool.appendVertices(this->getVertexSize(),
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000852 vertexCount,
853 vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000854 &poolState.fPoolVertexBuffer,
855 &poolState.fPoolStartVertex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000856 GR_DEBUGASSERT(success);
857}
858
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000859void GrInOrderDrawBuffer::onSetIndexSourceToArray(const void* indexArray,
860 int indexCount) {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000861 GeometryPoolState& poolState = fGeoPoolStateStack.back();
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000862 SkASSERT(0 == poolState.fUsedPoolIndexBytes);
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000863#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000864 bool success =
865#endif
866 fIndexPool.appendIndices(indexCount,
867 indexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000868 &poolState.fPoolIndexBuffer,
869 &poolState.fPoolStartIndex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000870 GR_DEBUGASSERT(success);
reed@google.comac10a2d2010-12-22 21:39:39 +0000871}
872
bsalomon@google.com3f5a95e2012-03-08 16:41:42 +0000873void GrInOrderDrawBuffer::releaseVertexArray() {
874 // When the client provides an array as the vertex source we handled it
875 // by copying their array into reserved space.
876 this->GrInOrderDrawBuffer::releaseReservedVertexSpace();
877}
878
879void GrInOrderDrawBuffer::releaseIndexArray() {
880 // When the client provides an array as the index source we handled it
881 // by copying their array into reserved space.
882 this->GrInOrderDrawBuffer::releaseReservedIndexSpace();
883}
884
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000885void GrInOrderDrawBuffer::geometrySourceWillPush() {
886 GeometryPoolState& poolState = fGeoPoolStateStack.push_back();
887 poolState.fUsedPoolVertexBytes = 0;
888 poolState.fUsedPoolIndexBytes = 0;
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000889#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000890 poolState.fPoolVertexBuffer = (GrVertexBuffer*)~0;
891 poolState.fPoolStartVertex = ~0;
892 poolState.fPoolIndexBuffer = (GrIndexBuffer*)~0;
893 poolState.fPoolStartIndex = ~0;
894#endif
895}
896
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000897void GrInOrderDrawBuffer::geometrySourceWillPop(
898 const GeometrySrcState& restoredState) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000899 SkASSERT(fGeoPoolStateStack.count() > 1);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000900 fGeoPoolStateStack.pop_back();
901 GeometryPoolState& poolState = fGeoPoolStateStack.back();
902 // we have to assume that any slack we had in our vertex/index data
903 // is now unreleasable because data may have been appended later in the
904 // pool.
905 if (kReserved_GeometrySrcType == restoredState.fVertexSrc ||
906 kArray_GeometrySrcType == restoredState.fVertexSrc) {
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000907 poolState.fUsedPoolVertexBytes = restoredState.fVertexSize * restoredState.fVertexCount;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000908 }
909 if (kReserved_GeometrySrcType == restoredState.fIndexSrc ||
910 kArray_GeometrySrcType == restoredState.fIndexSrc) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000911 poolState.fUsedPoolIndexBytes = sizeof(uint16_t) *
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000912 restoredState.fIndexCount;
913 }
914}
915
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000916bool GrInOrderDrawBuffer::needsNewState() const {
bsalomon@google.comca432082013-01-23 19:53:46 +0000917 return fStates.empty() || !fStates.back().isEqual(this->getDrawState());
reed@google.comac10a2d2010-12-22 21:39:39 +0000918}
919
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000920bool GrInOrderDrawBuffer::needsNewClip() const {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000921 SkASSERT(fClips.count() == fClipOrigins.count());
bsalomon@google.com358e4272013-01-10 14:40:28 +0000922 if (this->getDrawState().isClipState()) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000923 if (fClipSet &&
bsalomon@google.com358e4272013-01-10 14:40:28 +0000924 (fClips.empty() ||
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000925 fClips.back() != *this->getClip()->fClipStack ||
926 fClipOrigins.back() != this->getClip()->fOrigin)) {
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000927 return true;
928 }
reed@google.comac10a2d2010-12-22 21:39:39 +0000929 }
930 return false;
931}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000932
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000933void GrInOrderDrawBuffer::addToCmdBuffer(uint8_t cmd) {
934 SkASSERT(!cmd_has_trace_marker(cmd));
935 const GrTraceMarkerSet& activeTraceMarkers = this->getActiveTraceMarkers();
936 if (activeTraceMarkers.count() > 0) {
937 fCmds.push_back(add_trace_bit(cmd));
938 fGpuCmdMarkers.push_back(activeTraceMarkers);
939 } else {
940 fCmds.push_back(cmd);
941 }
942}
943
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000944void GrInOrderDrawBuffer::recordClip() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000945 fClips.push_back(*this->getClip()->fClipStack);
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000946 fClipOrigins.push_back() = this->getClip()->fOrigin;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000947 fClipSet = false;
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000948 this->addToCmdBuffer(kSetClip_Cmd);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000949}
950
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000951void GrInOrderDrawBuffer::recordState() {
bsalomon@google.comca432082013-01-23 19:53:46 +0000952 fStates.push_back().saveFrom(this->getDrawState());
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000953 this->addToCmdBuffer(kSetState_Cmd);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000954}
955
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000956GrInOrderDrawBuffer::DrawRecord* GrInOrderDrawBuffer::recordDraw(const DrawInfo& info) {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000957 this->addToCmdBuffer(kDraw_Cmd);
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000958 return &fDraws.push_back(info);
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000959}
960
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000961GrInOrderDrawBuffer::StencilPath* GrInOrderDrawBuffer::recordStencilPath() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000962 this->addToCmdBuffer(kStencilPath_Cmd);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000963 return &fStencilPaths.push_back();
964}
965
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000966GrInOrderDrawBuffer::DrawPath* GrInOrderDrawBuffer::recordDrawPath() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000967 this->addToCmdBuffer(kDrawPath_Cmd);
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000968 return &fDrawPath.push_back();
969}
970
971GrInOrderDrawBuffer::DrawPaths* GrInOrderDrawBuffer::recordDrawPaths() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000972 this->addToCmdBuffer(kDrawPaths_Cmd);
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000973 return &fDrawPaths.push_back();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000974}
975
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000976GrInOrderDrawBuffer::Clear* GrInOrderDrawBuffer::recordClear() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000977 this->addToCmdBuffer(kClear_Cmd);
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000978 return &fClears.push_back();
reed@google.comac10a2d2010-12-22 21:39:39 +0000979}
bsalomon@google.comd302f142011-03-03 13:54:13 +0000980
bsalomon@google.com116ad842013-04-09 15:38:19 +0000981GrInOrderDrawBuffer::CopySurface* GrInOrderDrawBuffer::recordCopySurface() {
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000982 this->addToCmdBuffer(kCopySurface_Cmd);
bsalomon@google.com116ad842013-04-09 15:38:19 +0000983 return &fCopySurfaces.push_back();
984}
985
986
robertphillips@google.combeb1af72012-07-26 18:52:16 +0000987void GrInOrderDrawBuffer::clipWillBeSet(const GrClipData* newClipData) {
988 INHERITED::clipWillBeSet(newClipData);
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000989 fClipSet = true;
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000990 fClipProxyState = kUnknown_ClipProxyState;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000991}