1 /*
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 "GrDrawPathOp.h"
9 #include "GrAppliedClip.h"
10 #include "GrRenderTargetContext.h"
11 #include "GrRenderTargetPriv.h"
12 #include "SkTemplates.h"
13
GrDrawPathOpBase(uint32_t classID,const SkMatrix & viewMatrix,GrPaint && paint,GrPathRendering::FillType fill,GrAA aa)14 GrDrawPathOpBase::GrDrawPathOpBase(uint32_t classID, const SkMatrix& viewMatrix, GrPaint&& paint,
15 GrPathRendering::FillType fill, GrAA aa)
16 : INHERITED(classID)
17 , fViewMatrix(viewMatrix)
18 , fProcessorSet(std::move(paint))
19 , fAnalysis(paint.getColor())
20 , fFillType(fill)
21 , fAA(aa) {}
22
dumpInfo() const23 SkString GrDrawPathOp::dumpInfo() const {
24 SkString string;
25 string.printf("PATH: 0x%p", fPath.get());
26 string.append(INHERITED::dumpInfo());
27 return string;
28 }
29
initPipeline(const GrOpFlushState & state,GrPipeline * pipeline)30 GrPipelineOptimizations GrDrawPathOpBase::initPipeline(const GrOpFlushState& state,
31 GrPipeline* pipeline) {
32 static constexpr GrUserStencilSettings kCoverPass{
33 GrUserStencilSettings::StaticInit<
34 0x0000,
35 GrUserStencilTest::kNotEqual,
36 0xffff,
37 GrUserStencilOp::kZero,
38 GrUserStencilOp::kKeep,
39 0xffff>()
40 };
41 GrPipeline::InitArgs args;
42 args.fProcessors = &this->processors();
43 args.fFlags = GrAA::kYes == fAA ? GrPipeline::kHWAntialias_Flag : 0;
44 args.fUserStencil = &kCoverPass;
45 args.fAppliedClip = state.drawOpArgs().fAppliedClip;
46 args.fRenderTarget = state.drawOpArgs().fRenderTarget;
47 args.fCaps = &state.caps();
48 args.fDstTexture = state.drawOpArgs().fDstTexture;
49 args.fAnalysis =
50 &this->doFragmentProcessorAnalysis(state.caps(), state.drawOpArgs().fAppliedClip);
51
52 return pipeline->init(args);
53 }
54
55 //////////////////////////////////////////////////////////////////////////////
56
init_stencil_pass_settings(const GrOpFlushState & flushState,GrPathRendering::FillType fillType,GrStencilSettings * stencil)57 void init_stencil_pass_settings(const GrOpFlushState& flushState,
58 GrPathRendering::FillType fillType, GrStencilSettings* stencil) {
59 const GrAppliedClip* appliedClip = flushState.drawOpArgs().fAppliedClip;
60 bool stencilClip = appliedClip && appliedClip->hasStencilClip();
61 stencil->reset(GrPathRendering::GetStencilPassSettings(fillType), stencilClip,
62 flushState.drawOpArgs().fRenderTarget->renderTargetPriv().numStencilBits());
63 }
64
65 //////////////////////////////////////////////////////////////////////////////
66
onExecute(GrOpFlushState * state)67 void GrDrawPathOp::onExecute(GrOpFlushState* state) {
68 GrColor color = this->color();
69 GrPipeline pipeline;
70 GrPipelineOptimizations optimizations = this->initPipeline(*state, &pipeline);
71 optimizations.getOverrideColorIfSet(&color);
72 sk_sp<GrPathProcessor> pathProc(GrPathProcessor::Create(color, this->viewMatrix()));
73
74 GrStencilSettings stencil;
75 init_stencil_pass_settings(*state, this->fillType(), &stencil);
76 state->gpu()->pathRendering()->drawPath(pipeline, *pathProc, stencil, fPath.get());
77 }
78
79 //////////////////////////////////////////////////////////////////////////////
80
dumpInfo() const81 SkString GrDrawPathRangeOp::dumpInfo() const {
82 SkString string;
83 string.printf("RANGE: 0x%p COUNTS: [", fPathRange.get());
84 for (DrawList::Iter iter(fDraws); iter.get(); iter.next()) {
85 string.appendf("%d, ", iter.get()->fInstanceData->count());
86 }
87 string.remove(string.size() - 2, 2);
88 string.append("]");
89 string.append(INHERITED::dumpInfo());
90 return string;
91 }
92
GrDrawPathRangeOp(const SkMatrix & viewMatrix,SkScalar scale,SkScalar x,SkScalar y,GrPaint && paint,GrPathRendering::FillType fill,GrAA aa,GrPathRange * range,const InstanceData * instanceData,const SkRect & bounds)93 GrDrawPathRangeOp::GrDrawPathRangeOp(const SkMatrix& viewMatrix, SkScalar scale, SkScalar x,
94 SkScalar y, GrPaint&& paint, GrPathRendering::FillType fill,
95 GrAA aa, GrPathRange* range, const InstanceData* instanceData,
96 const SkRect& bounds)
97 : INHERITED(ClassID(), viewMatrix, std::move(paint), fill, aa)
98 , fPathRange(range)
99 , fTotalPathCount(instanceData->count())
100 , fScale(scale) {
101 fDraws.addToHead()->set(instanceData, x, y);
102 this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
103 }
104
105 static void pre_translate_transform_values(const float* xforms,
106 GrPathRendering::PathTransformType type, int count,
107 SkScalar x, SkScalar y, float* dst);
108
onCombineIfPossible(GrOp * t,const GrCaps & caps)109 bool GrDrawPathRangeOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
110 GrDrawPathRangeOp* that = t->cast<GrDrawPathRangeOp>();
111 if (this->fPathRange.get() != that->fPathRange.get() ||
112 this->transformType() != that->transformType() || this->fScale != that->fScale ||
113 this->color() != that->color() || !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
114 return false;
115 }
116 if (this->processors() != that->processors()) {
117 return false;
118 }
119 switch (fDraws.head()->fInstanceData->transformType()) {
120 case GrPathRendering::kNone_PathTransformType:
121 if (this->fDraws.head()->fX != that->fDraws.head()->fX ||
122 this->fDraws.head()->fY != that->fDraws.head()->fY) {
123 return false;
124 }
125 break;
126 case GrPathRendering::kTranslateX_PathTransformType:
127 if (this->fDraws.head()->fY != that->fDraws.head()->fY) {
128 return false;
129 }
130 break;
131 case GrPathRendering::kTranslateY_PathTransformType:
132 if (this->fDraws.head()->fX != that->fDraws.head()->fX) {
133 return false;
134 }
135 break;
136 default:
137 break;
138 }
139 // TODO: Check some other things here. (winding, opaque, pathProc color, vm, ...)
140 // Try to combine this call with the previous DrawPaths. We do this by stenciling all the
141 // paths together and then covering them in a single pass. This is not equivalent to two
142 // separate draw calls, so we can only do it if there is no blending (no overlap would also
143 // work). Note that it's also possible for overlapping paths to cancel each other's winding
144 // numbers, and we only partially account for this by not allowing even/odd paths to be
145 // combined. (Glyphs in the same font tend to wind the same direction so it works out OK.)
146
147 if (GrPathRendering::kWinding_FillType != this->fillType() ||
148 GrPathRendering::kWinding_FillType != that->fillType()) {
149 return false;
150 }
151 // If we have non-clipping coverage processors we don't try to merge as its unclear whether it
152 // will be correct. We don't expect this to happen in practice.
153 if (this->processors().numCoverageFragmentProcessors()) {
154 return false;
155 }
156 bool opaque = this->fragmentProcessorAnalysis().isOutputColorOpaque();
157 if (!GrXPFactory::CanCombineOverlappedStencilAndCover(this->processors().xpFactory(), opaque)) {
158 return false;
159 }
160 fTotalPathCount += that->fTotalPathCount;
161 while (Draw* head = that->fDraws.head()) {
162 Draw* draw = fDraws.addToTail();
163 draw->fInstanceData.reset(head->fInstanceData.release());
164 draw->fX = head->fX;
165 draw->fY = head->fY;
166 that->fDraws.popHead();
167 }
168 this->joinBounds(*that);
169 return true;
170 }
171
onExecute(GrOpFlushState * state)172 void GrDrawPathRangeOp::onExecute(GrOpFlushState* state) {
173 const Draw& head = *fDraws.head();
174
175 SkMatrix drawMatrix(this->viewMatrix());
176 drawMatrix.preScale(fScale, fScale);
177 drawMatrix.preTranslate(head.fX, head.fY);
178
179 SkMatrix localMatrix;
180 localMatrix.setScale(fScale, fScale);
181 localMatrix.preTranslate(head.fX, head.fY);
182
183 sk_sp<GrPathProcessor> pathProc(
184 GrPathProcessor::Create(this->color(), drawMatrix, localMatrix));
185
186 GrPipeline pipeline;
187 this->initPipeline(*state, &pipeline);
188 GrStencilSettings stencil;
189 init_stencil_pass_settings(*state, this->fillType(), &stencil);
190 if (fDraws.count() == 1) {
191 const InstanceData& instances = *head.fInstanceData;
192 state->gpu()->pathRendering()->drawPaths(pipeline,
193 *pathProc,
194 stencil,
195 fPathRange.get(),
196 instances.indices(),
197 GrPathRange::kU16_PathIndexType,
198 instances.transformValues(),
199 instances.transformType(),
200 instances.count());
201 } else {
202 int floatsPerTransform = GrPathRendering::PathTransformSize(this->transformType());
203 SkAutoSTMalloc<4096, float> transformStorage(floatsPerTransform * fTotalPathCount);
204 SkAutoSTMalloc<2048, uint16_t> indexStorage(fTotalPathCount);
205 int idx = 0;
206 for (DrawList::Iter iter(fDraws); iter.get(); iter.next()) {
207 const Draw& draw = *iter.get();
208 const InstanceData& instances = *draw.fInstanceData;
209 memcpy(&indexStorage[idx], instances.indices(), instances.count() * sizeof(uint16_t));
210 pre_translate_transform_values(instances.transformValues(), this->transformType(),
211 instances.count(), draw.fX - head.fX, draw.fY - head.fY,
212 &transformStorage[floatsPerTransform * idx]);
213 idx += instances.count();
214
215 // TODO: Support mismatched transform types if we start using more types other than 2D.
216 SkASSERT(instances.transformType() == this->transformType());
217 }
218 SkASSERT(idx == fTotalPathCount);
219
220 state->gpu()->pathRendering()->drawPaths(pipeline,
221 *pathProc,
222 stencil,
223 fPathRange.get(),
224 indexStorage,
225 GrPathRange::kU16_PathIndexType,
226 transformStorage,
227 this->transformType(),
228 fTotalPathCount);
229 }
230 }
231
pre_translate_transform_values(const float * xforms,GrPathRendering::PathTransformType type,int count,SkScalar x,SkScalar y,float * dst)232 inline void pre_translate_transform_values(const float* xforms,
233 GrPathRendering::PathTransformType type, int count,
234 SkScalar x, SkScalar y, float* dst) {
235 if (0 == x && 0 == y) {
236 memcpy(dst, xforms, count * GrPathRendering::PathTransformSize(type) * sizeof(float));
237 return;
238 }
239 switch (type) {
240 case GrPathRendering::kNone_PathTransformType:
241 SkFAIL("Cannot pre-translate kNone_PathTransformType.");
242 break;
243 case GrPathRendering::kTranslateX_PathTransformType:
244 SkASSERT(0 == y);
245 for (int i = 0; i < count; i++) {
246 dst[i] = xforms[i] + x;
247 }
248 break;
249 case GrPathRendering::kTranslateY_PathTransformType:
250 SkASSERT(0 == x);
251 for (int i = 0; i < count; i++) {
252 dst[i] = xforms[i] + y;
253 }
254 break;
255 case GrPathRendering::kTranslate_PathTransformType:
256 for (int i = 0; i < 2 * count; i += 2) {
257 dst[i] = xforms[i] + x;
258 dst[i + 1] = xforms[i + 1] + y;
259 }
260 break;
261 case GrPathRendering::kAffine_PathTransformType:
262 for (int i = 0; i < 6 * count; i += 6) {
263 dst[i] = xforms[i];
264 dst[i + 1] = xforms[i + 1];
265 dst[i + 2] = xforms[i] * x + xforms[i + 1] * y + xforms[i + 2];
266 dst[i + 3] = xforms[i + 3];
267 dst[i + 4] = xforms[i + 4];
268 dst[i + 5] = xforms[i + 3] * x + xforms[i + 4] * y + xforms[i + 5];
269 }
270 break;
271 default:
272 SkFAIL("Unknown transform type.");
273 break;
274 }
275 }
276