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