blob: a829378e54f15d9ed82a8b5e0c5af2db8d5b3a82 [file] [log] [blame]
bsalomon1fcc01c2015-09-09 09:48:06 -07001/*
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 "GrDrawPathBatch.h"
9
10SkString GrDrawPathBatch::dumpInfo() const {
11 SkString string;
12 string.printf("PATH: 0x%p", fPath.get());
13 return string;
14}
15
16void GrDrawPathBatch::onDraw(GrBatchFlushState* state) {
17 GrProgramDesc desc;
joshualittf2384692015-09-10 11:00:51 -070018
19 SkAutoTUnref<GrPathProcessor> pathProc(GrPathProcessor::Create(this->color(),
20 this->opts(),
21 this->viewMatrix()));
joshualitt465283c2015-09-11 08:19:35 -070022 state->gpu()->buildProgramDesc(&desc, *pathProc, *this->pipeline());
joshualittf2384692015-09-10 11:00:51 -070023 GrPathRendering::DrawPathArgs args(pathProc, this->pipeline(),
joshualitt465283c2015-09-11 08:19:35 -070024 &desc, &this->stencilSettings());
bsalomon1fcc01c2015-09-09 09:48:06 -070025 state->gpu()->pathRendering()->drawPath(args, fPath.get());
26}
27
28GrDrawPathRangeBatch::~GrDrawPathRangeBatch() {
29 for (DrawList::Iter iter(fDraws); iter.get(); iter.next()) {
30 (*iter.get())->unref();
31 }
32}
33
34SkString GrDrawPathRangeBatch::dumpInfo() const {
35 SkString string;
36 string.printf("RANGE: 0x%p COUNTS: [", *fDraws.head());
37 for (DrawList::Iter iter(fDraws); iter.get(); iter.next()) {
38 string.appendf("%d ,", (*iter.get())->count());
39 }
40 string.remove(string.size() - 2, 2);
41 string.append("]");
42 return string;
43}
44
45bool GrDrawPathRangeBatch::isWinding() const {
46 static const GrStencilSettings::Face pathFace = GrStencilSettings::kFront_Face;
47 bool isWinding = kInvert_StencilOp != this->stencilSettings().passOp(pathFace);
48 if (isWinding) {
49 // Double check that it is in fact winding.
50 SkASSERT(kIncClamp_StencilOp == this->stencilSettings().passOp(pathFace));
51 SkASSERT(kIncClamp_StencilOp == this->stencilSettings().failOp(pathFace));
52 SkASSERT(0x1 != this->stencilSettings().writeMask(pathFace));
53 SkASSERT(!this->stencilSettings().isTwoSided());
54 }
55 return isWinding;
56}
57
joshualittf2384692015-09-10 11:00:51 -070058GrDrawPathRangeBatch::GrDrawPathRangeBatch(const SkMatrix& viewMatrix, const SkMatrix& localMatrix,
bsalomonbf074552015-11-23 14:25:19 -080059 GrColor color, GrPathRange* range, GrPathRangeDraw* draw,
60 const SkRect& bounds)
reed1b55a962015-09-17 20:16:13 -070061 : INHERITED(ClassID(), viewMatrix, color)
cdalton8585dd22015-10-08 08:04:09 -070062 , fPathRange(range)
joshualittf2384692015-09-10 11:00:51 -070063 , fLocalMatrix(localMatrix) {
cdalton8585dd22015-10-08 08:04:09 -070064 SkDEBUGCODE(draw->fUsedInBatch = true;)
65 fDraws.addToHead(SkRef(draw));
66 fTotalPathCount = draw->count();
bsalomonbf074552015-11-23 14:25:19 -080067 fBounds = bounds;
bsalomon1fcc01c2015-09-09 09:48:06 -070068}
69
70bool GrDrawPathRangeBatch::onCombineIfPossible(GrBatch* t, const GrCaps& caps) {
71 GrDrawPathRangeBatch* that = t->cast<GrDrawPathRangeBatch>();
cdalton8585dd22015-10-08 08:04:09 -070072 if (this->fPathRange.get() != that->fPathRange.get()) {
73 return false;
74 }
bsalomon1fcc01c2015-09-09 09:48:06 -070075 if (!GrPathRangeDraw::CanMerge(**this->fDraws.head(), **that->fDraws.head())) {
76 return false;
77 }
78 if (!GrPipeline::AreEqual(*this->pipeline(), *that->pipeline(), false)) {
79 return false;
80 }
joshualittf2384692015-09-10 11:00:51 -070081 if (this->color() != that->color() ||
82 !this->viewMatrix().cheapEqualTo(that->viewMatrix()) ||
83 !fLocalMatrix.cheapEqualTo(that->fLocalMatrix)) {
bsalomon1fcc01c2015-09-09 09:48:06 -070084 return false;
85 }
86 // TODO: Check some other things here. (winding, opaque, pathProc color, vm, ...)
87 // Try to combine this call with the previous DrawPaths. We do this by stenciling all the
88 // paths together and then covering them in a single pass. This is not equivalent to two
89 // separate draw calls, so we can only do it if there is no blending (no overlap would also
90 // work). Note that it's also possible for overlapping paths to cancel each other's winding
91 // numbers, and we only partially account for this by not allowing even/odd paths to be
92 // combined. (Glyphs in the same font tend to wind the same direction so it works out OK.)
93 if (!this->isWinding() ||
94 this->stencilSettings() != that->stencilSettings() ||
95 this->opts().willColorBlendWithDst()) {
96 return false;
97 }
98 SkASSERT(!that->opts().willColorBlendWithDst());
99 fTotalPathCount += that->fTotalPathCount;
100 while (GrPathRangeDraw** head = that->fDraws.head()) {
101 fDraws.addToTail(*head);
102 // We're stealing that's refs, so pop without unreffing.
103 that->fDraws.popHead();
104 }
105 return true;
106}
107
108void GrDrawPathRangeBatch::onDraw(GrBatchFlushState* state) {
109 GrProgramDesc desc;
joshualittf2384692015-09-10 11:00:51 -0700110 SkAutoTUnref<GrPathProcessor> pathProc(GrPathProcessor::Create(this->color(),
111 this->opts(),
112 this->viewMatrix(),
113 fLocalMatrix));
joshualitt465283c2015-09-11 08:19:35 -0700114 state->gpu()->buildProgramDesc(&desc, *pathProc, *this->pipeline());
joshualittf2384692015-09-10 11:00:51 -0700115 GrPathRendering::DrawPathArgs args(pathProc, this->pipeline(),
joshualitt465283c2015-09-11 08:19:35 -0700116 &desc, &this->stencilSettings());
bsalomon1fcc01c2015-09-09 09:48:06 -0700117 if (fDraws.count() == 1) {
118 const GrPathRangeDraw& draw = **fDraws.head();
cdalton8585dd22015-10-08 08:04:09 -0700119 state->gpu()->pathRendering()->drawPaths(args, fPathRange.get(), draw.indices(),
bsalomon1fcc01c2015-09-09 09:48:06 -0700120 GrPathRange::kU16_PathIndexType, draw.transforms(), draw.transformType(),
121 draw.count());
122 return;
123 }
124
bsalomon1fcc01c2015-09-09 09:48:06 -0700125 GrPathRendering::PathTransformType transformType = (*fDraws.head())->transformType();
126 int floatsPerTransform = GrPathRendering::PathTransformSize(transformType);
127 SkAutoSTMalloc<512, float> transformStorage(floatsPerTransform * fTotalPathCount);
128 SkAutoSTMalloc<256, uint16_t> indexStorage(fTotalPathCount);
129 uint16_t* indices = indexStorage.get();
130 float* transforms = transformStorage.get();
131 for (DrawList::Iter iter(fDraws); iter.get(); iter.next()) {
132 SkASSERT((*iter.get())->transformType() == transformType);
bsalomon1fcc01c2015-09-09 09:48:06 -0700133 int cnt = (*iter.get())->count();
134 memcpy(indices, (*iter.get())->indices(), cnt * sizeof(uint16_t));
135 indices += cnt;
136 memcpy(transforms, (*iter.get())->transforms(), cnt * floatsPerTransform * sizeof(float));
137 transforms += cnt * floatsPerTransform;
138 }
139 SkASSERT(indices - indexStorage.get() == fTotalPathCount);
cdalton8585dd22015-10-08 08:04:09 -0700140 state->gpu()->pathRendering()->drawPaths(args, fPathRange.get(), indexStorage.get(),
bsalomon1fcc01c2015-09-09 09:48:06 -0700141 GrPathRange::kU16_PathIndexType, transformStorage.get(), transformType,
142 fTotalPathCount);
143}