blob: c56c4ad2cd723d8fc7c04fb5a8db3ddab6b3466b [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)
reed1b55a962015-09-17 20:16:13 -070060 : INHERITED(ClassID(), viewMatrix, color)
joshualittf2384692015-09-10 11:00:51 -070061 , fDraws(4)
62 , fLocalMatrix(localMatrix) {
bsalomon1fcc01c2015-09-09 09:48:06 -070063 SkDEBUGCODE(pathRangeDraw->fUsedInBatch = true;)
bsalomon1fcc01c2015-09-09 09:48:06 -070064 fDraws.addToHead(SkRef(pathRangeDraw));
65 fTotalPathCount = pathRangeDraw->count();
66 // 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>();
75 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();
119 state->gpu()->pathRendering()->drawPaths(args, draw.range(), draw.indices(),
120 GrPathRange::kU16_PathIndexType, draw.transforms(), draw.transformType(),
121 draw.count());
122 return;
123 }
124
125 const GrPathRange* range = (*fDraws.head())->range();
126 GrPathRendering::PathTransformType transformType = (*fDraws.head())->transformType();
127 int floatsPerTransform = GrPathRendering::PathTransformSize(transformType);
128 SkAutoSTMalloc<512, float> transformStorage(floatsPerTransform * fTotalPathCount);
129 SkAutoSTMalloc<256, uint16_t> indexStorage(fTotalPathCount);
130 uint16_t* indices = indexStorage.get();
131 float* transforms = transformStorage.get();
132 for (DrawList::Iter iter(fDraws); iter.get(); iter.next()) {
133 SkASSERT((*iter.get())->transformType() == transformType);
134 SkASSERT((*iter.get())->range() == range);
135 int cnt = (*iter.get())->count();
136 memcpy(indices, (*iter.get())->indices(), cnt * sizeof(uint16_t));
137 indices += cnt;
138 memcpy(transforms, (*iter.get())->transforms(), cnt * floatsPerTransform * sizeof(float));
139 transforms += cnt * floatsPerTransform;
140 }
141 SkASSERT(indices - indexStorage.get() == fTotalPathCount);
142 state->gpu()->pathRendering()->drawPaths(args, range, indexStorage.get(),
143 GrPathRange::kU16_PathIndexType, transformStorage.get(), transformType,
144 fTotalPathCount);
145}