blob: a47e665607f4bcc0e3e2a8831f1d405b517b0393 [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,
59 GrColor color, GrPathRangeDraw* pathRangeDraw)
60 : INHERITED(viewMatrix, color)
61 , fDraws(4)
62 , fLocalMatrix(localMatrix) {
bsalomon1fcc01c2015-09-09 09:48:06 -070063 SkDEBUGCODE(pathRangeDraw->fUsedInBatch = true;)
64 this->initClassID<GrDrawPathRangeBatch>();
65 fDraws.addToHead(SkRef(pathRangeDraw));
66 fTotalPathCount = pathRangeDraw->count();
67 // Don't compute a bounding box. For dst copy texture, we'll opt instead for it to just copy
68 // the entire dst. Realistically this is a moot point, because any context that supports
69 // NV_path_rendering will also support NV_blend_equation_advanced.
70 // For clipping we'll just skip any optimizations based on the bounds.
71 fBounds.setLargest();
72}
73
74bool GrDrawPathRangeBatch::onCombineIfPossible(GrBatch* t, const GrCaps& caps) {
75 GrDrawPathRangeBatch* that = t->cast<GrDrawPathRangeBatch>();
76 if (!GrPathRangeDraw::CanMerge(**this->fDraws.head(), **that->fDraws.head())) {
77 return false;
78 }
79 if (!GrPipeline::AreEqual(*this->pipeline(), *that->pipeline(), false)) {
80 return false;
81 }
joshualittf2384692015-09-10 11:00:51 -070082 if (this->color() != that->color() ||
83 !this->viewMatrix().cheapEqualTo(that->viewMatrix()) ||
84 !fLocalMatrix.cheapEqualTo(that->fLocalMatrix)) {
bsalomon1fcc01c2015-09-09 09:48:06 -070085 return false;
86 }
87 // TODO: Check some other things here. (winding, opaque, pathProc color, vm, ...)
88 // Try to combine this call with the previous DrawPaths. We do this by stenciling all the
89 // paths together and then covering them in a single pass. This is not equivalent to two
90 // separate draw calls, so we can only do it if there is no blending (no overlap would also
91 // work). Note that it's also possible for overlapping paths to cancel each other's winding
92 // numbers, and we only partially account for this by not allowing even/odd paths to be
93 // combined. (Glyphs in the same font tend to wind the same direction so it works out OK.)
94 if (!this->isWinding() ||
95 this->stencilSettings() != that->stencilSettings() ||
96 this->opts().willColorBlendWithDst()) {
97 return false;
98 }
99 SkASSERT(!that->opts().willColorBlendWithDst());
100 fTotalPathCount += that->fTotalPathCount;
101 while (GrPathRangeDraw** head = that->fDraws.head()) {
102 fDraws.addToTail(*head);
103 // We're stealing that's refs, so pop without unreffing.
104 that->fDraws.popHead();
105 }
106 return true;
107}
108
109void GrDrawPathRangeBatch::onDraw(GrBatchFlushState* state) {
110 GrProgramDesc desc;
joshualittf2384692015-09-10 11:00:51 -0700111 SkAutoTUnref<GrPathProcessor> pathProc(GrPathProcessor::Create(this->color(),
112 this->opts(),
113 this->viewMatrix(),
114 fLocalMatrix));
joshualitt465283c2015-09-11 08:19:35 -0700115 state->gpu()->buildProgramDesc(&desc, *pathProc, *this->pipeline());
joshualittf2384692015-09-10 11:00:51 -0700116 GrPathRendering::DrawPathArgs args(pathProc, this->pipeline(),
joshualitt465283c2015-09-11 08:19:35 -0700117 &desc, &this->stencilSettings());
bsalomon1fcc01c2015-09-09 09:48:06 -0700118 if (fDraws.count() == 1) {
119 const GrPathRangeDraw& draw = **fDraws.head();
120 state->gpu()->pathRendering()->drawPaths(args, draw.range(), draw.indices(),
121 GrPathRange::kU16_PathIndexType, draw.transforms(), draw.transformType(),
122 draw.count());
123 return;
124 }
125
126 const GrPathRange* range = (*fDraws.head())->range();
127 GrPathRendering::PathTransformType transformType = (*fDraws.head())->transformType();
128 int floatsPerTransform = GrPathRendering::PathTransformSize(transformType);
129 SkAutoSTMalloc<512, float> transformStorage(floatsPerTransform * fTotalPathCount);
130 SkAutoSTMalloc<256, uint16_t> indexStorage(fTotalPathCount);
131 uint16_t* indices = indexStorage.get();
132 float* transforms = transformStorage.get();
133 for (DrawList::Iter iter(fDraws); iter.get(); iter.next()) {
134 SkASSERT((*iter.get())->transformType() == transformType);
135 SkASSERT((*iter.get())->range() == range);
136 int cnt = (*iter.get())->count();
137 memcpy(indices, (*iter.get())->indices(), cnt * sizeof(uint16_t));
138 indices += cnt;
139 memcpy(transforms, (*iter.get())->transforms(), cnt * floatsPerTransform * sizeof(float));
140 transforms += cnt * floatsPerTransform;
141 }
142 SkASSERT(indices - indexStorage.get() == fTotalPathCount);
143 state->gpu()->pathRendering()->drawPaths(args, range, indexStorage.get(),
144 GrPathRange::kU16_PathIndexType, transformStorage.get(), transformType,
145 fTotalPathCount);
146}