blob: d3c924d36847b2ca76b242bdc39b667ba35c4919 [file] [log] [blame]
robertphillips193ea932015-03-03 12:40:49 -08001/*
2 * Copyright 2015 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.
6 */
7
8#include "GrTargetCommands.h"
9
10#include "GrColor.h"
11#include "GrDefaultGeoProcFactory.h"
12#include "GrInOrderDrawBuffer.h"
13#include "GrTemplates.h"
14#include "SkPoint.h"
15
16void GrTargetCommands::closeBatch() {
17 if (fDrawBatch) {
18 fBatchTarget.resetNumberOfDraws();
19 fDrawBatch->execute(NULL, fPrevState);
20 fDrawBatch->fBatch->setNumberOfDraws(fBatchTarget.numberOfDraws());
21 fDrawBatch = NULL;
22 }
23}
24
25static bool path_fill_type_is_winding(const GrStencilSettings& pathStencilSettings) {
26 static const GrStencilSettings::Face pathFace = GrStencilSettings::kFront_Face;
27 bool isWinding = kInvert_StencilOp != pathStencilSettings.passOp(pathFace);
28 if (isWinding) {
29 // Double check that it is in fact winding.
30 SkASSERT(kIncClamp_StencilOp == pathStencilSettings.passOp(pathFace));
31 SkASSERT(kIncClamp_StencilOp == pathStencilSettings.failOp(pathFace));
32 SkASSERT(0x1 != pathStencilSettings.writeMask(pathFace));
33 SkASSERT(!pathStencilSettings.isTwoSided());
34 }
35 return isWinding;
36}
37
38int GrTargetCommands::concatInstancedDraw(GrInOrderDrawBuffer* iodb,
39 const GrDrawTarget::DrawInfo& info) {
40 SkASSERT(!fCmdBuffer.empty());
41 SkASSERT(info.isInstanced());
42
43 const GrIndexBuffer* ib;
44 if (!iodb->canConcatToIndexBuffer(&ib)) {
45 return 0;
46 }
47
48 // Check if there is a draw info that is compatible that uses the same VB from the pool and
49 // the same IB
robertphillipsbca3c9f2015-03-05 09:17:17 -080050 if (Cmd::kDraw_CmdType != fCmdBuffer.back().type()) {
robertphillips193ea932015-03-03 12:40:49 -080051 return 0;
52 }
53
54 Draw* draw = static_cast<Draw*>(&fCmdBuffer.back());
55
56 if (!draw->fInfo.isInstanced() ||
57 draw->fInfo.primitiveType() != info.primitiveType() ||
58 draw->fInfo.verticesPerInstance() != info.verticesPerInstance() ||
59 draw->fInfo.indicesPerInstance() != info.indicesPerInstance() ||
60 draw->fInfo.vertexBuffer() != info.vertexBuffer() ||
61 draw->fInfo.indexBuffer() != ib) {
62 return 0;
63 }
64 if (draw->fInfo.startVertex() + draw->fInfo.vertexCount() != info.startVertex()) {
65 return 0;
66 }
67
68 // how many instances can be concat'ed onto draw given the size of the index buffer
69 int instancesToConcat = iodb->indexCountInCurrentSource() / info.indicesPerInstance();
70 instancesToConcat -= draw->fInfo.instanceCount();
71 instancesToConcat = SkTMin(instancesToConcat, info.instanceCount());
72
73 draw->fInfo.adjustInstanceCount(instancesToConcat);
74
75 // update last fGpuCmdMarkers to include any additional trace markers that have been added
76 iodb->recordTraceMarkersIfNecessary(draw);
77 return instancesToConcat;
78}
79
80GrTargetCommands::Cmd* GrTargetCommands::recordDraw(
81 GrInOrderDrawBuffer* iodb,
82 const GrGeometryProcessor* gp,
83 const GrDrawTarget::DrawInfo& info,
84 const GrDrawTarget::PipelineInfo& pipelineInfo) {
joshualitt0dcb8e32015-04-27 12:03:05 -070085#ifdef USE_BITMAP_TEXTBLOBS
86 SkFAIL("Non-batch no longer supported\n");
87#endif
robertphillips193ea932015-03-03 12:40:49 -080088 SkASSERT(info.vertexBuffer() && (!info.isIndexed() || info.indexBuffer()));
joshualitt0dcb8e32015-04-27 12:03:05 -070089 CLOSE_BATCH
robertphillips193ea932015-03-03 12:40:49 -080090
91 if (!this->setupPipelineAndShouldDraw(iodb, gp, pipelineInfo)) {
92 return NULL;
93 }
94
95 Draw* draw;
96 if (info.isInstanced()) {
97 int instancesConcated = this->concatInstancedDraw(iodb, info);
98 if (info.instanceCount() > instancesConcated) {
99 draw = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, Draw, (info));
100 draw->fInfo.adjustInstanceCount(-instancesConcated);
101 } else {
102 return NULL;
103 }
104 } else {
105 draw = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, Draw, (info));
106 }
107
108 return draw;
109}
110
111GrTargetCommands::Cmd* GrTargetCommands::recordDrawBatch(
112 GrInOrderDrawBuffer* iodb,
113 GrBatch* batch,
114 const GrDrawTarget::PipelineInfo& pipelineInfo) {
115 if (!this->setupPipelineAndShouldDraw(iodb, batch, pipelineInfo)) {
116 return NULL;
117 }
118
119 // Check if there is a Batch Draw we can batch with
robertphillipsbca3c9f2015-03-05 09:17:17 -0800120 if (Cmd::kDrawBatch_CmdType != fCmdBuffer.back().type() || !fDrawBatch) {
robertphillips193ea932015-03-03 12:40:49 -0800121 fDrawBatch = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, DrawBatch, (batch, &fBatchTarget));
122 return fDrawBatch;
123 }
124
125 SkASSERT(&fCmdBuffer.back() == fDrawBatch);
126 if (!fDrawBatch->fBatch->combineIfPossible(batch)) {
joshualitt0dcb8e32015-04-27 12:03:05 -0700127 CLOSE_BATCH
robertphillips193ea932015-03-03 12:40:49 -0800128 fDrawBatch = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, DrawBatch, (batch, &fBatchTarget));
129 }
130
131 return fDrawBatch;
132}
133
134GrTargetCommands::Cmd* GrTargetCommands::recordStencilPath(
135 GrInOrderDrawBuffer* iodb,
136 const GrPipelineBuilder& pipelineBuilder,
137 const GrPathProcessor* pathProc,
138 const GrPath* path,
139 const GrScissorState& scissorState,
140 const GrStencilSettings& stencilSettings) {
joshualitt0dcb8e32015-04-27 12:03:05 -0700141 CLOSE_BATCH
robertphillips193ea932015-03-03 12:40:49 -0800142
143 StencilPath* sp = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, StencilPath,
144 (path, pipelineBuilder.getRenderTarget()));
145
146 sp->fScissor = scissorState;
147 sp->fUseHWAA = pipelineBuilder.isHWAntialias();
148 sp->fViewMatrix = pathProc->viewMatrix();
149 sp->fStencil = stencilSettings;
150 return sp;
151}
152
153GrTargetCommands::Cmd* GrTargetCommands::recordDrawPath(
154 GrInOrderDrawBuffer* iodb,
155 const GrPathProcessor* pathProc,
156 const GrPath* path,
157 const GrStencilSettings& stencilSettings,
158 const GrDrawTarget::PipelineInfo& pipelineInfo) {
joshualitt0dcb8e32015-04-27 12:03:05 -0700159 CLOSE_BATCH
robertphillips193ea932015-03-03 12:40:49 -0800160
161 // TODO: Only compare the subset of GrPipelineBuilder relevant to path covering?
162 if (!this->setupPipelineAndShouldDraw(iodb, pathProc, pipelineInfo)) {
163 return NULL;
164 }
165 DrawPath* dp = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, DrawPath, (path));
166 dp->fStencilSettings = stencilSettings;
167 return dp;
168}
169
170GrTargetCommands::Cmd* GrTargetCommands::recordDrawPaths(
171 GrInOrderDrawBuffer* iodb,
172 const GrPathProcessor* pathProc,
173 const GrPathRange* pathRange,
174 const void* indexValues,
175 GrDrawTarget::PathIndexType indexType,
176 const float transformValues[],
177 GrDrawTarget::PathTransformType transformType,
178 int count,
179 const GrStencilSettings& stencilSettings,
180 const GrDrawTarget::PipelineInfo& pipelineInfo) {
181 SkASSERT(pathRange);
182 SkASSERT(indexValues);
183 SkASSERT(transformValues);
joshualitt0dcb8e32015-04-27 12:03:05 -0700184 CLOSE_BATCH
robertphillips193ea932015-03-03 12:40:49 -0800185
186 if (!this->setupPipelineAndShouldDraw(iodb, pathProc, pipelineInfo)) {
187 return NULL;
188 }
189
190 char* savedIndices;
191 float* savedTransforms;
192
193 iodb->appendIndicesAndTransforms(indexValues, indexType,
194 transformValues, transformType,
195 count, &savedIndices, &savedTransforms);
196
robertphillipsbca3c9f2015-03-05 09:17:17 -0800197 if (Cmd::kDrawPaths_CmdType == fCmdBuffer.back().type()) {
robertphillips193ea932015-03-03 12:40:49 -0800198 // The previous command was also DrawPaths. Try to collapse this call into the one
199 // before. Note that stenciling all the paths at once, then covering, may not be
200 // equivalent to two separate draw calls if there is overlap. Blending won't work,
201 // and the combined calls may also cancel each other's winding numbers in some
202 // places. For now the winding numbers are only an issue if the fill is even/odd,
203 // because DrawPaths is currently only used for glyphs, and glyphs in the same
204 // font tend to all wind in the same direction.
205 DrawPaths* previous = static_cast<DrawPaths*>(&fCmdBuffer.back());
206 if (pathRange == previous->pathRange() &&
207 indexType == previous->fIndexType &&
208 transformType == previous->fTransformType &&
209 stencilSettings == previous->fStencilSettings &&
210 path_fill_type_is_winding(stencilSettings) &&
211 !pipelineInfo.willBlendWithDst(pathProc)) {
212 const int indexBytes = GrPathRange::PathIndexSizeInBytes(indexType);
213 const int xformSize = GrPathRendering::PathTransformSize(transformType);
214 if (&previous->fIndices[previous->fCount*indexBytes] == savedIndices &&
215 (0 == xformSize ||
216 &previous->fTransforms[previous->fCount*xformSize] == savedTransforms)) {
217 // Fold this DrawPaths call into the one previous.
218 previous->fCount += count;
219 return NULL;
220 }
221 }
222 }
223
224 DrawPaths* dp = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, DrawPaths, (pathRange));
225 dp->fIndices = savedIndices;
226 dp->fIndexType = indexType;
227 dp->fTransforms = savedTransforms;
228 dp->fTransformType = transformType;
229 dp->fCount = count;
230 dp->fStencilSettings = stencilSettings;
231 return dp;
232}
233
234GrTargetCommands::Cmd* GrTargetCommands::recordClear(GrInOrderDrawBuffer* iodb,
235 const SkIRect* rect,
236 GrColor color,
237 bool canIgnoreRect,
238 GrRenderTarget* renderTarget) {
239 SkASSERT(renderTarget);
joshualitt0dcb8e32015-04-27 12:03:05 -0700240 CLOSE_BATCH
robertphillips193ea932015-03-03 12:40:49 -0800241
242 SkIRect r;
243 if (NULL == rect) {
244 // We could do something smart and remove previous draws and clears to
245 // the current render target. If we get that smart we have to make sure
246 // those draws aren't read before this clear (render-to-texture).
247 r.setLTRB(0, 0, renderTarget->width(), renderTarget->height());
248 rect = &r;
249 }
250 Clear* clr = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, Clear, (renderTarget));
251 GrColorIsPMAssert(color);
252 clr->fColor = color;
253 clr->fRect = *rect;
254 clr->fCanIgnoreRect = canIgnoreRect;
255 return clr;
256}
257
258GrTargetCommands::Cmd* GrTargetCommands::recordClearStencilClip(GrInOrderDrawBuffer* iodb,
259 const SkIRect& rect,
260 bool insideClip,
261 GrRenderTarget* renderTarget) {
262 SkASSERT(renderTarget);
joshualitt0dcb8e32015-04-27 12:03:05 -0700263 CLOSE_BATCH
robertphillips193ea932015-03-03 12:40:49 -0800264
265 ClearStencilClip* clr = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, ClearStencilClip, (renderTarget));
266 clr->fRect = rect;
267 clr->fInsideClip = insideClip;
268 return clr;
269}
270
271GrTargetCommands::Cmd* GrTargetCommands::recordDiscard(GrInOrderDrawBuffer* iodb,
272 GrRenderTarget* renderTarget) {
273 SkASSERT(renderTarget);
joshualitt0dcb8e32015-04-27 12:03:05 -0700274 CLOSE_BATCH
robertphillips193ea932015-03-03 12:40:49 -0800275
276 Clear* clr = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, Clear, (renderTarget));
277 clr->fColor = GrColor_ILLEGAL;
278 return clr;
279}
280
281void GrTargetCommands::reset() {
282 fCmdBuffer.reset();
283 fPrevState = NULL;
284 fDrawBatch = NULL;
285}
286
287void GrTargetCommands::flush(GrInOrderDrawBuffer* iodb) {
288 if (fCmdBuffer.empty()) {
289 return;
290 }
291
robertphillips193ea932015-03-03 12:40:49 -0800292 // TODO this is temporary while batch is being rolled out
joshualitt0dcb8e32015-04-27 12:03:05 -0700293 CLOSE_BATCH
robertphillips193ea932015-03-03 12:40:49 -0800294
robertphillipsbca3c9f2015-03-05 09:17:17 -0800295 // Updated every time we find a set state cmd to reflect the current state in the playback
296 // stream.
297 SetState* currentState = NULL;
robertphillips193ea932015-03-03 12:40:49 -0800298
joshualitt385e26e2015-04-27 11:42:30 -0700299 GrGpu* gpu = iodb->getGpu();
300
joshualitt0dcb8e32015-04-27 12:03:05 -0700301#ifdef USE_BITMAP_TEXTBLOBS
302 // Loop over all batches and generate geometry
303 CmdBuffer::Iter genIter(fCmdBuffer);
304 while (genIter.next()) {
305 if (Cmd::kDrawBatch_CmdType == genIter->type()) {
306 DrawBatch* db = reinterpret_cast<DrawBatch*>(genIter.get());
307 fBatchTarget.resetNumberOfDraws();
308 db->execute(NULL, currentState);
309 db->fBatch->setNumberOfDraws(fBatchTarget.numberOfDraws());
310 } else if (Cmd::kSetState_CmdType == genIter->type()) {
311 SetState* ss = reinterpret_cast<SetState*>(genIter.get());
312
313 ss->execute(gpu, currentState);
314 currentState = ss;
315 }
316 }
317#endif
318
319 iodb->getVertexAllocPool()->unmap();
320 iodb->getIndexAllocPool()->unmap();
321 fBatchTarget.preFlush();
322
323 CmdBuffer::Iter iter(fCmdBuffer);
324
robertphillips193ea932015-03-03 12:40:49 -0800325 while (iter.next()) {
robertphillips193ea932015-03-03 12:40:49 -0800326 GrGpuTraceMarker newMarker("", -1);
327 SkString traceString;
328 if (iter->isTraced()) {
robertphillipsbca3c9f2015-03-05 09:17:17 -0800329 traceString = iodb->getCmdString(iter->markerID());
robertphillips193ea932015-03-03 12:40:49 -0800330 newMarker.fMarker = traceString.c_str();
331 gpu->addGpuTraceMarker(&newMarker);
robertphillips193ea932015-03-03 12:40:49 -0800332 }
333
334 // TODO temporary hack
robertphillipsbca3c9f2015-03-05 09:17:17 -0800335 if (Cmd::kDrawBatch_CmdType == iter->type()) {
robertphillips193ea932015-03-03 12:40:49 -0800336 DrawBatch* db = reinterpret_cast<DrawBatch*>(iter.get());
337 fBatchTarget.flushNext(db->fBatch->numberOfDraws());
robertphillipsd14101e2015-03-05 08:55:28 -0800338
339 if (iter->isTraced()) {
340 gpu->removeGpuTraceMarker(&newMarker);
341 }
robertphillips193ea932015-03-03 12:40:49 -0800342 continue;
343 }
344
robertphillipsbca3c9f2015-03-05 09:17:17 -0800345 if (Cmd::kSetState_CmdType == iter->type()) {
joshualitt0dcb8e32015-04-27 12:03:05 -0700346#ifndef USE_BITMAP_TEXTBLOBS
robertphillips193ea932015-03-03 12:40:49 -0800347 SetState* ss = reinterpret_cast<SetState*>(iter.get());
348
robertphillipsbca3c9f2015-03-05 09:17:17 -0800349 ss->execute(gpu, currentState);
robertphillips193ea932015-03-03 12:40:49 -0800350 currentState = ss;
joshualitt0dcb8e32015-04-27 12:03:05 -0700351#else
352 // TODO this is just until NVPR is in batch
353 SetState* ss = reinterpret_cast<SetState*>(iter.get());
354
355 if (ss->fPrimitiveProcessor) {
356 ss->execute(gpu, currentState);
357 }
358 currentState = ss;
359#endif
360
robertphillips193ea932015-03-03 12:40:49 -0800361 } else {
362 iter->execute(gpu, currentState);
363 }
364
365 if (iter->isTraced()) {
366 gpu->removeGpuTraceMarker(&newMarker);
367 }
368 }
369
370 // TODO see copious notes about hack
371 fBatchTarget.postFlush();
372}
373
374void GrTargetCommands::Draw::execute(GrGpu* gpu, const SetState* state) {
375 SkASSERT(state);
376 DrawArgs args(state->fPrimitiveProcessor.get(), state->getPipeline(), &state->fDesc,
377 &state->fBatchTracker);
378 gpu->draw(args, fInfo);
379}
380
381void GrTargetCommands::StencilPath::execute(GrGpu* gpu, const SetState*) {
382 GrGpu::StencilPathState state;
383 state.fRenderTarget = fRenderTarget.get();
384 state.fScissor = &fScissor;
385 state.fStencil = &fStencil;
386 state.fUseHWAA = fUseHWAA;
387 state.fViewMatrix = &fViewMatrix;
388
389 gpu->stencilPath(this->path(), state);
390}
391
392void GrTargetCommands::DrawPath::execute(GrGpu* gpu, const SetState* state) {
393 SkASSERT(state);
394 DrawArgs args(state->fPrimitiveProcessor.get(), state->getPipeline(), &state->fDesc,
395 &state->fBatchTracker);
396 gpu->drawPath(args, this->path(), fStencilSettings);
397}
398
399void GrTargetCommands::DrawPaths::execute(GrGpu* gpu, const SetState* state) {
400 SkASSERT(state);
401 DrawArgs args(state->fPrimitiveProcessor.get(), state->getPipeline(), &state->fDesc,
402 &state->fBatchTracker);
403 gpu->drawPaths(args, this->pathRange(),
404 fIndices, fIndexType,
405 fTransforms, fTransformType,
406 fCount, fStencilSettings);
407}
408
409void GrTargetCommands::DrawBatch::execute(GrGpu*, const SetState* state) {
410 SkASSERT(state);
411 fBatch->generateGeometry(fBatchTarget, state->getPipeline());
412}
413
robertphillipsbca3c9f2015-03-05 09:17:17 -0800414void GrTargetCommands::SetState::execute(GrGpu* gpu, const SetState*) {
415 // TODO sometimes we have a prim proc, othertimes we have a GrBatch. Eventually we
416 // will only have GrBatch and we can delete this
417 if (fPrimitiveProcessor) {
418 gpu->buildProgramDesc(&fDesc, *fPrimitiveProcessor, *getPipeline(), fBatchTracker);
419 }
420}
robertphillips193ea932015-03-03 12:40:49 -0800421
422void GrTargetCommands::Clear::execute(GrGpu* gpu, const SetState*) {
423 if (GrColor_ILLEGAL == fColor) {
424 gpu->discard(this->renderTarget());
425 } else {
426 gpu->clear(&fRect, fColor, fCanIgnoreRect, this->renderTarget());
427 }
428}
429
430void GrTargetCommands::ClearStencilClip::execute(GrGpu* gpu, const SetState*) {
431 gpu->clearStencilClip(fRect, fInsideClip, this->renderTarget());
432}
433
434void GrTargetCommands::CopySurface::execute(GrGpu* gpu, const SetState*) {
435 gpu->copySurface(this->dst(), this->src(), fSrcRect, fDstPoint);
436}
437
438GrTargetCommands::Cmd* GrTargetCommands::recordCopySurface(GrInOrderDrawBuffer* iodb,
439 GrSurface* dst,
440 GrSurface* src,
441 const SkIRect& srcRect,
442 const SkIPoint& dstPoint) {
443 if (iodb->getGpu()->canCopySurface(dst, src, srcRect, dstPoint)) {
joshualitt0dcb8e32015-04-27 12:03:05 -0700444 CLOSE_BATCH
robertphillips193ea932015-03-03 12:40:49 -0800445 CopySurface* cs = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, CopySurface, (dst, src));
446 cs->fSrcRect = srcRect;
447 cs->fDstPoint = dstPoint;
448 return cs;
449 }
450 return NULL;
451}
452
453bool GrTargetCommands::setupPipelineAndShouldDraw(GrInOrderDrawBuffer* iodb,
454 const GrPrimitiveProcessor* primProc,
455 const GrDrawTarget::PipelineInfo& pipelineInfo) {
456 SetState* ss = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, SetState, (primProc));
457 iodb->setupPipeline(pipelineInfo, ss->pipelineLocation());
458
459 if (ss->getPipeline()->mustSkip()) {
460 fCmdBuffer.pop_back();
461 return false;
462 }
463
464 ss->fPrimitiveProcessor->initBatchTracker(&ss->fBatchTracker,
465 ss->getPipeline()->getInitBatchTracker());
466
467 if (fPrevState && fPrevState->fPrimitiveProcessor.get() &&
468 fPrevState->fPrimitiveProcessor->canMakeEqual(fPrevState->fBatchTracker,
469 *ss->fPrimitiveProcessor,
470 ss->fBatchTracker) &&
471 fPrevState->getPipeline()->isEqual(*ss->getPipeline())) {
472 fCmdBuffer.pop_back();
473 } else {
474 fPrevState = ss;
475 iodb->recordTraceMarkersIfNecessary(ss);
476 }
477 return true;
478}
479
480bool GrTargetCommands::setupPipelineAndShouldDraw(GrInOrderDrawBuffer* iodb,
481 GrBatch* batch,
482 const GrDrawTarget::PipelineInfo& pipelineInfo) {
483 SetState* ss = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, SetState, ());
484 iodb->setupPipeline(pipelineInfo, ss->pipelineLocation());
485
486 if (ss->getPipeline()->mustSkip()) {
487 fCmdBuffer.pop_back();
488 return false;
489 }
490
491 batch->initBatchTracker(ss->getPipeline()->getInitBatchTracker());
492
493 if (fPrevState && !fPrevState->fPrimitiveProcessor.get() &&
494 fPrevState->getPipeline()->isEqual(*ss->getPipeline())) {
495 fCmdBuffer.pop_back();
496 } else {
joshualitt0dcb8e32015-04-27 12:03:05 -0700497 CLOSE_BATCH
robertphillips193ea932015-03-03 12:40:49 -0800498 fPrevState = ss;
499 iodb->recordTraceMarkersIfNecessary(ss);
500 }
501 return true;
502}
503