blob: 3e4c863e33abfa406a037bc1bdfac55045ab59b9 [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()));
22 state->gpu()->buildProgramDesc(&desc, *pathProc,
bsalomon1fcc01c2015-09-09 09:48:06 -070023 *this->pipeline(), *this->tracker());
joshualittf2384692015-09-10 11:00:51 -070024 GrPathRendering::DrawPathArgs args(pathProc, this->pipeline(),
bsalomon1fcc01c2015-09-09 09:48:06 -070025 &desc, this->tracker(), &this->stencilSettings());
26 state->gpu()->pathRendering()->drawPath(args, fPath.get());
27}
28
29GrDrawPathRangeBatch::~GrDrawPathRangeBatch() {
30 for (DrawList::Iter iter(fDraws); iter.get(); iter.next()) {
31 (*iter.get())->unref();
32 }
33}
34
35SkString GrDrawPathRangeBatch::dumpInfo() const {
36 SkString string;
37 string.printf("RANGE: 0x%p COUNTS: [", *fDraws.head());
38 for (DrawList::Iter iter(fDraws); iter.get(); iter.next()) {
39 string.appendf("%d ,", (*iter.get())->count());
40 }
41 string.remove(string.size() - 2, 2);
42 string.append("]");
43 return string;
44}
45
46bool GrDrawPathRangeBatch::isWinding() const {
47 static const GrStencilSettings::Face pathFace = GrStencilSettings::kFront_Face;
48 bool isWinding = kInvert_StencilOp != this->stencilSettings().passOp(pathFace);
49 if (isWinding) {
50 // Double check that it is in fact winding.
51 SkASSERT(kIncClamp_StencilOp == this->stencilSettings().passOp(pathFace));
52 SkASSERT(kIncClamp_StencilOp == this->stencilSettings().failOp(pathFace));
53 SkASSERT(0x1 != this->stencilSettings().writeMask(pathFace));
54 SkASSERT(!this->stencilSettings().isTwoSided());
55 }
56 return isWinding;
57}
58
joshualittf2384692015-09-10 11:00:51 -070059GrDrawPathRangeBatch::GrDrawPathRangeBatch(const SkMatrix& viewMatrix, const SkMatrix& localMatrix,
60 GrColor color, GrPathRangeDraw* pathRangeDraw)
61 : INHERITED(viewMatrix, color)
62 , fDraws(4)
63 , fLocalMatrix(localMatrix) {
bsalomon1fcc01c2015-09-09 09:48:06 -070064 SkDEBUGCODE(pathRangeDraw->fUsedInBatch = true;)
65 this->initClassID<GrDrawPathRangeBatch>();
66 fDraws.addToHead(SkRef(pathRangeDraw));
67 fTotalPathCount = pathRangeDraw->count();
68 // Don't compute a bounding box. For dst copy texture, we'll opt instead for it to just copy
69 // the entire dst. Realistically this is a moot point, because any context that supports
70 // NV_path_rendering will also support NV_blend_equation_advanced.
71 // For clipping we'll just skip any optimizations based on the bounds.
72 fBounds.setLargest();
73}
74
75bool GrDrawPathRangeBatch::onCombineIfPossible(GrBatch* t, const GrCaps& caps) {
76 GrDrawPathRangeBatch* that = t->cast<GrDrawPathRangeBatch>();
77 if (!GrPathRangeDraw::CanMerge(**this->fDraws.head(), **that->fDraws.head())) {
78 return false;
79 }
80 if (!GrPipeline::AreEqual(*this->pipeline(), *that->pipeline(), false)) {
81 return false;
82 }
joshualittf2384692015-09-10 11:00:51 -070083 if (this->color() != that->color() ||
84 !this->viewMatrix().cheapEqualTo(that->viewMatrix()) ||
85 !fLocalMatrix.cheapEqualTo(that->fLocalMatrix)) {
bsalomon1fcc01c2015-09-09 09:48:06 -070086 return false;
87 }
88 // TODO: Check some other things here. (winding, opaque, pathProc color, vm, ...)
89 // Try to combine this call with the previous DrawPaths. We do this by stenciling all the
90 // paths together and then covering them in a single pass. This is not equivalent to two
91 // separate draw calls, so we can only do it if there is no blending (no overlap would also
92 // work). Note that it's also possible for overlapping paths to cancel each other's winding
93 // numbers, and we only partially account for this by not allowing even/odd paths to be
94 // combined. (Glyphs in the same font tend to wind the same direction so it works out OK.)
95 if (!this->isWinding() ||
96 this->stencilSettings() != that->stencilSettings() ||
97 this->opts().willColorBlendWithDst()) {
98 return false;
99 }
100 SkASSERT(!that->opts().willColorBlendWithDst());
101 fTotalPathCount += that->fTotalPathCount;
102 while (GrPathRangeDraw** head = that->fDraws.head()) {
103 fDraws.addToTail(*head);
104 // We're stealing that's refs, so pop without unreffing.
105 that->fDraws.popHead();
106 }
107 return true;
108}
109
110void GrDrawPathRangeBatch::onDraw(GrBatchFlushState* state) {
111 GrProgramDesc desc;
joshualittf2384692015-09-10 11:00:51 -0700112 SkAutoTUnref<GrPathProcessor> pathProc(GrPathProcessor::Create(this->color(),
113 this->opts(),
114 this->viewMatrix(),
115 fLocalMatrix));
116 state->gpu()->buildProgramDesc(&desc, *pathProc, *this->pipeline(),
117 *this->tracker());
118 GrPathRendering::DrawPathArgs args(pathProc, this->pipeline(),
bsalomon1fcc01c2015-09-09 09:48:06 -0700119 &desc, this->tracker(), &this->stencilSettings());
120 if (fDraws.count() == 1) {
121 const GrPathRangeDraw& draw = **fDraws.head();
122 state->gpu()->pathRendering()->drawPaths(args, draw.range(), draw.indices(),
123 GrPathRange::kU16_PathIndexType, draw.transforms(), draw.transformType(),
124 draw.count());
125 return;
126 }
127
128 const GrPathRange* range = (*fDraws.head())->range();
129 GrPathRendering::PathTransformType transformType = (*fDraws.head())->transformType();
130 int floatsPerTransform = GrPathRendering::PathTransformSize(transformType);
131 SkAutoSTMalloc<512, float> transformStorage(floatsPerTransform * fTotalPathCount);
132 SkAutoSTMalloc<256, uint16_t> indexStorage(fTotalPathCount);
133 uint16_t* indices = indexStorage.get();
134 float* transforms = transformStorage.get();
135 for (DrawList::Iter iter(fDraws); iter.get(); iter.next()) {
136 SkASSERT((*iter.get())->transformType() == transformType);
137 SkASSERT((*iter.get())->range() == range);
138 int cnt = (*iter.get())->count();
139 memcpy(indices, (*iter.get())->indices(), cnt * sizeof(uint16_t));
140 indices += cnt;
141 memcpy(transforms, (*iter.get())->transforms(), cnt * floatsPerTransform * sizeof(float));
142 transforms += cnt * floatsPerTransform;
143 }
144 SkASSERT(indices - indexStorage.get() == fTotalPathCount);
145 state->gpu()->pathRendering()->drawPaths(args, range, indexStorage.get(),
146 GrPathRange::kU16_PathIndexType, transformStorage.get(), transformType,
147 fTotalPathCount);
148}