blob: 84dda9071c6f5218a72e75ff080f1810da0f57a2 [file] [log] [blame]
Michael Ludwig69858532018-11-28 15:34:34 -05001/*
2 * Copyright 2018 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 "GrFillRectOp.h"
9
Brian Salomonf4191392019-03-14 15:46:20 -040010#include "GrCaps.h"
Michael Ludwig69858532018-11-28 15:34:34 -050011#include "GrGeometryProcessor.h"
12#include "GrMeshDrawOp.h"
13#include "GrPaint.h"
14#include "GrQuad.h"
15#include "GrQuadPerEdgeAA.h"
16#include "GrSimpleMeshDrawOpHelper.h"
Brian Osman8fa7ab42019-03-18 10:22:42 -040017#include "SkGr.h"
Michael Ludwig69858532018-11-28 15:34:34 -050018#include "SkMatrix.h"
19#include "SkRect.h"
20#include "glsl/GrGLSLColorSpaceXformHelper.h"
21#include "glsl/GrGLSLGeometryProcessor.h"
22#include "glsl/GrGLSLVarying.h"
23
24namespace {
25
26using VertexSpec = GrQuadPerEdgeAA::VertexSpec;
27using ColorType = GrQuadPerEdgeAA::ColorType;
28
Michael Ludwigc96fc372019-01-08 15:46:15 -050029#ifdef SK_DEBUG
30static SkString dump_quad_info(int index, const GrPerspQuad& deviceQuad,
31 const GrPerspQuad& localQuad, const SkPMColor4f& color,
32 GrQuadAAFlags aaFlags) {
33 SkString str;
34 str.appendf("%d: Color: [%.2f, %.2f, %.2f, %.2f], Edge AA: l%u_t%u_r%u_b%u, \n"
35 " device quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), "
36 "(%.2f, %.2f, %.2f)],\n"
37 " local quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), "
38 "(%.2f, %.2f, %.2f)]\n",
39 index, color.fR, color.fG, color.fB, color.fA,
40 (uint32_t) (aaFlags & GrQuadAAFlags::kLeft),
41 (uint32_t) (aaFlags & GrQuadAAFlags::kTop),
42 (uint32_t) (aaFlags & GrQuadAAFlags::kRight),
43 (uint32_t) (aaFlags & GrQuadAAFlags::kBottom),
44 deviceQuad.x(0), deviceQuad.y(0), deviceQuad.w(0),
45 deviceQuad.x(1), deviceQuad.y(1), deviceQuad.w(1),
46 deviceQuad.x(2), deviceQuad.y(2), deviceQuad.w(2),
47 deviceQuad.x(3), deviceQuad.y(3), deviceQuad.w(3),
48 localQuad.x(0), localQuad.y(0), localQuad.w(0),
49 localQuad.x(1), localQuad.y(1), localQuad.w(1),
50 localQuad.x(2), localQuad.y(2), localQuad.w(2),
51 localQuad.x(3), localQuad.y(3), localQuad.w(3));
52 return str;
53}
54#endif
Michael Ludwig69858532018-11-28 15:34:34 -050055
Michael Ludwig69858532018-11-28 15:34:34 -050056class FillRectOp final : public GrMeshDrawOp {
57private:
58 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
59
60public:
Robert Phillipsb97da532019-02-12 15:24:12 -050061 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwig69858532018-11-28 15:34:34 -050062 GrPaint&& paint,
63 GrAAType aaType,
64 GrQuadAAFlags edgeAA,
65 const GrUserStencilSettings* stencilSettings,
66 const GrPerspQuad& deviceQuad,
67 GrQuadType deviceQuadType,
68 const GrPerspQuad& localQuad,
69 GrQuadType localQuadType) {
70 // Clean up deviations between aaType and edgeAA
71 GrResolveAATypeForQuad(aaType, edgeAA, deviceQuad, deviceQuadType, &aaType, &edgeAA);
Michael Ludwigdcd48212019-01-08 15:28:57 -050072 return Helper::FactoryHelper<FillRectOp>(context, std::move(paint), aaType, edgeAA,
73 stencilSettings, deviceQuad, deviceQuadType, localQuad, localQuadType);
Michael Ludwig69858532018-11-28 15:34:34 -050074 }
75
Michael Ludwigdcd48212019-01-08 15:28:57 -050076 // aaType is passed to Helper in the initializer list, so incongruities between aaType and
77 // edgeFlags must be resolved prior to calling this constructor.
78 FillRectOp(Helper::MakeArgs args, SkPMColor4f paintColor, GrAAType aaType,
79 GrQuadAAFlags edgeFlags, const GrUserStencilSettings* stencil,
Michael Ludwig69858532018-11-28 15:34:34 -050080 const GrPerspQuad& deviceQuad, GrQuadType deviceQuadType,
81 const GrPerspQuad& localQuad, GrQuadType localQuadType)
82 : INHERITED(ClassID())
Brian Osman8fa7ab42019-03-18 10:22:42 -040083 , fHelper(args, aaType, stencil) {
Michael Ludwig69858532018-11-28 15:34:34 -050084 // The color stored with the quad is the clear color if a scissor-clear is decided upon
85 // when executing the op.
Michael Ludwigc96fc372019-01-08 15:46:15 -050086 fDeviceQuads.push_back(deviceQuad, deviceQuadType, { paintColor, edgeFlags });
Michael Ludwigdcd48212019-01-08 15:28:57 -050087
88 if (!fHelper.isTrivial()) {
89 // Conservatively keep track of the local coordinates; it may be that the paint doesn't
90 // need them after analysis is finished. If the paint is known to be solid up front they
91 // can be skipped entirely.
92 fLocalQuads.push_back(localQuad, localQuadType);
93 }
Michael Ludwigc96fc372019-01-08 15:46:15 -050094 this->setBounds(deviceQuad.bounds(deviceQuadType),
95 HasAABloat(aaType == GrAAType::kCoverage), IsZeroArea::kNo);
Michael Ludwig69858532018-11-28 15:34:34 -050096 }
97
98 const char* name() const override { return "FillRectOp"; }
99
100 void visitProxies(const VisitProxyFunc& func, VisitorType) const override {
101 return fHelper.visitProxies(func);
102 }
103
104#ifdef SK_DEBUG
105 SkString dumpInfo() const override {
106 SkString str;
Michael Ludwigc96fc372019-01-08 15:46:15 -0500107 str.appendf("# draws: %u\n", this->quadCount());
Michael Ludwig69858532018-11-28 15:34:34 -0500108 str.appendf("Device quad type: %u, local quad type: %u\n",
Michael Ludwigc96fc372019-01-08 15:46:15 -0500109 (uint32_t) fDeviceQuads.quadType(), (uint32_t) fLocalQuads.quadType());
Michael Ludwig69858532018-11-28 15:34:34 -0500110 str += fHelper.dumpInfo();
Michael Ludwigc96fc372019-01-08 15:46:15 -0500111 GrPerspQuad device, local;
112 for (int i = 0; i < this->quadCount(); i++) {
113 device = fDeviceQuads[i];
114 const ColorAndAA& info = fDeviceQuads.metadata(i);
Michael Ludwigdcd48212019-01-08 15:28:57 -0500115 if (!fHelper.isTrivial()) {
116 local = fLocalQuads[i];
117 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500118 str += dump_quad_info(i, device, local, info.fColor, info.fAAFlags);
Michael Ludwig69858532018-11-28 15:34:34 -0500119 }
120 str += INHERITED::dumpInfo();
121 return str;
122 }
123#endif
124
Brian Osman5ced0bf2019-03-15 10:15:29 -0400125 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
126 GrFSAAType fsaaType, GrClampType clampType) override {
Michael Ludwig69858532018-11-28 15:34:34 -0500127 // Initialize aggregate color analysis with the first quad's color (which always exists)
Michael Ludwigc96fc372019-01-08 15:46:15 -0500128 SkASSERT(this->quadCount() > 0);
129 GrProcessorAnalysisColor quadColors(fDeviceQuads.metadata(0).fColor);
Michael Ludwig69858532018-11-28 15:34:34 -0500130 // Then combine the colors of any additional quads (e.g. from MakeSet)
Michael Ludwigc96fc372019-01-08 15:46:15 -0500131 for (int i = 1; i < this->quadCount(); ++i) {
132 quadColors = GrProcessorAnalysisColor::Combine(quadColors,
133 fDeviceQuads.metadata(i).fColor);
Michael Ludwigca91e1f2018-12-10 10:44:44 -0500134 if (quadColors.isUnknown()) {
135 // No point in accumulating additional starting colors, combining cannot make it
136 // less unknown.
137 break;
138 }
Michael Ludwig69858532018-11-28 15:34:34 -0500139 }
Michael Ludwig72ab3462018-12-10 12:43:36 -0500140
141 // If the AA type is coverage, it will be a single value per pixel; if it's not coverage AA
142 // then the coverage is always 1.0, so specify kNone for more optimal blending.
143 GrProcessorAnalysisCoverage coverage = fHelper.aaType() == GrAAType::kCoverage ?
144 GrProcessorAnalysisCoverage::kSingleChannel :
145 GrProcessorAnalysisCoverage::kNone;
Brian Osman5ced0bf2019-03-15 10:15:29 -0400146 auto result = fHelper.finalizeProcessors(
147 caps, clip, fsaaType, clampType, coverage, &quadColors);
Michael Ludwig69858532018-11-28 15:34:34 -0500148 // If there is a constant color after analysis, that means all of the quads should be set
149 // to the same color (even if they started out with different colors).
150 SkPMColor4f colorOverride;
151 if (quadColors.isConstant(&colorOverride)) {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400152 fColorType = GrQuadPerEdgeAA::MinColorType(colorOverride, clampType, caps);
Michael Ludwigc96fc372019-01-08 15:46:15 -0500153 for (int i = 0; i < this->quadCount(); ++i) {
154 fDeviceQuads.metadata(i).fColor = colorOverride;
Michael Ludwig69858532018-11-28 15:34:34 -0500155 }
Brian Osman8fa7ab42019-03-18 10:22:42 -0400156 } else {
157 // Otherwise compute the color type needed as the max over all quads.
158 fColorType = ColorType::kNone;
159 for (int i = 0; i < this->quadCount(); ++i) {
160 SkPMColor4f* color = &fDeviceQuads.metadata(i).fColor;
161 fColorType = SkTMax(fColorType,
162 GrQuadPerEdgeAA::MinColorType(*color, clampType, caps));
163 }
Michael Ludwig69858532018-11-28 15:34:34 -0500164 }
165
166 return result;
167 }
168
169 FixedFunctionFlags fixedFunctionFlags() const override {
170 // Since the AA type of the whole primitive is kept consistent with the per edge AA flags
171 // the helper's fixed function flags are appropriate.
172 return fHelper.fixedFunctionFlags();
173 }
174
175 DEFINE_OP_CLASS_ID
176
177private:
178 // For GrFillRectOp::MakeSet's use of addQuad
Robert Phillipsb97da532019-02-12 15:24:12 -0500179 friend std::unique_ptr<GrDrawOp> GrFillRectOp::MakeSet(
180 GrRecordingContext*,
181 GrPaint&&,
182 GrAAType, const SkMatrix& viewMatrix,
Michael Ludwig69858532018-11-28 15:34:34 -0500183 const GrRenderTargetContext::QuadSetEntry quads[], int quadCount,
Robert Phillipsb97da532019-02-12 15:24:12 -0500184 const GrUserStencilSettings*);
Michael Ludwig69858532018-11-28 15:34:34 -0500185
Michael Ludwigc96fc372019-01-08 15:46:15 -0500186 void onPrepareDraws(Target* target) override {
Michael Ludwig69858532018-11-28 15:34:34 -0500187 TRACE_EVENT0("skia", TRACE_FUNC);
188
189 using Domain = GrQuadPerEdgeAA::Domain;
190 static constexpr SkRect kEmptyDomain = SkRect::MakeEmpty();
191
Brian Salomon1d835422019-03-13 16:11:44 -0400192 VertexSpec vertexSpec(fDeviceQuads.quadType(), fColorType, fLocalQuads.quadType(),
193 fHelper.usesLocalCoords(), Domain::kNo, fHelper.aaType(),
Brian Osman605c6d52019-03-15 12:10:35 -0400194 fHelper.compatibleWithCoverageAsAlpha());
Michael Ludwigdcd48212019-01-08 15:28:57 -0500195 // Make sure that if the op thought it was a solid color, the vertex spec does not use
196 // local coords.
197 SkASSERT(!fHelper.isTrivial() || !fHelper.usesLocalCoords());
Michael Ludwig69858532018-11-28 15:34:34 -0500198
Michael Ludwig467994d2018-12-03 14:58:31 +0000199 sk_sp<GrGeometryProcessor> gp = GrQuadPerEdgeAA::MakeProcessor(vertexSpec);
Michael Ludwig69858532018-11-28 15:34:34 -0500200 size_t vertexSize = gp->vertexStride();
201
Brian Salomon12d22642019-01-29 14:38:50 -0500202 sk_sp<const GrBuffer> vbuffer;
Michael Ludwig69858532018-11-28 15:34:34 -0500203 int vertexOffsetInBuffer = 0;
204
205 // Fill the allocated vertex data
Michael Ludwig93aeba02018-12-21 09:50:31 -0500206 void* vdata = target->makeVertexSpace(
Michael Ludwigc96fc372019-01-08 15:46:15 -0500207 vertexSize, this->quadCount() * vertexSpec.verticesPerQuad(),
Michael Ludwig93aeba02018-12-21 09:50:31 -0500208 &vbuffer, &vertexOffsetInBuffer);
Michael Ludwig69858532018-11-28 15:34:34 -0500209 if (!vdata) {
210 SkDebugf("Could not allocate vertices\n");
211 return;
212 }
213
214 // vertices pointer advances through vdata based on Tessellate's return value
215 void* vertices = vdata;
Michael Ludwigdcd48212019-01-08 15:28:57 -0500216 if (fHelper.isTrivial()) {
217 SkASSERT(fLocalQuads.count() == 0); // No local coords, so send an ignored dummy quad
Michael Ludwige9c57d32019-02-13 13:39:39 -0500218 static const GrPerspQuad kIgnoredLocal(SkRect::MakeEmpty());
219
Michael Ludwigdcd48212019-01-08 15:28:57 -0500220 for (int i = 0; i < this->quadCount(); ++i) {
221 const ColorAndAA& info = fDeviceQuads.metadata(i);
222 vertices = GrQuadPerEdgeAA::Tessellate(vertices, vertexSpec, fDeviceQuads[i],
223 info.fColor, kIgnoredLocal, kEmptyDomain, info.fAAFlags);
224 }
225 } else {
226 SkASSERT(fLocalQuads.count() == fDeviceQuads.count());
227 for (int i = 0; i < this->quadCount(); ++i) {
228 const ColorAndAA& info = fDeviceQuads.metadata(i);
229 vertices = GrQuadPerEdgeAA::Tessellate(vertices, vertexSpec, fDeviceQuads[i],
230 info.fColor, fLocalQuads[i], kEmptyDomain, info.fAAFlags);
231 }
Michael Ludwig69858532018-11-28 15:34:34 -0500232 }
233
234 // Configure the mesh for the vertex data
Michael Ludwig93aeba02018-12-21 09:50:31 -0500235 GrMesh* mesh = target->allocMeshes(1);
Michael Ludwigc96fc372019-01-08 15:46:15 -0500236 if (!GrQuadPerEdgeAA::ConfigureMeshIndices(target, mesh, vertexSpec, this->quadCount())) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500237 SkDebugf("Could not allocate indices\n");
238 return;
Michael Ludwig69858532018-11-28 15:34:34 -0500239 }
Brian Salomon12d22642019-01-29 14:38:50 -0500240 mesh->setVertexData(std::move(vbuffer), vertexOffsetInBuffer);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700241 target->recordDraw(std::move(gp), mesh);
242 }
Michael Ludwig69858532018-11-28 15:34:34 -0500243
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700244 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
245 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
246 }
Michael Ludwig69858532018-11-28 15:34:34 -0500247
248 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
249 TRACE_EVENT0("skia", TRACE_FUNC);
250 const auto* that = t->cast<FillRectOp>();
251
Michael Ludwig93aeba02018-12-21 09:50:31 -0500252 if ((fHelper.aaType() == GrAAType::kCoverage ||
253 that->fHelper.aaType() == GrAAType::kCoverage) &&
Michael Ludwigc96fc372019-01-08 15:46:15 -0500254 this->quadCount() + that->quadCount() > GrQuadPerEdgeAA::kNumAAQuadsInIndexBuffer) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500255 // This limit on batch size seems to help on Adreno devices
256 return CombineResult::kCannotCombine;
257 }
258
Michael Ludwigc96fc372019-01-08 15:46:15 -0500259 // Unlike most users of the draw op helper, this op can merge none-aa and coverage-aa draw
260 // ops together, so pass true as the last argument.
Michael Ludwig69858532018-11-28 15:34:34 -0500261 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds(), true)) {
262 return CombineResult::kCannotCombine;
263 }
264
Michael Ludwigdcd48212019-01-08 15:28:57 -0500265 // If the paints were compatible, the trivial/solid-color state should be the same
266 SkASSERT(fHelper.isTrivial() == that->fHelper.isTrivial());
Michael Ludwig69858532018-11-28 15:34:34 -0500267
Michael Ludwigdcd48212019-01-08 15:28:57 -0500268 // If the processor sets are compatible, the two ops are always compatible; it just needs to
269 // adjust the state of the op to be the more general quad and aa types of the two ops and
270 // then concatenate the per-quad data.
Brian Salomon1d835422019-03-13 16:11:44 -0400271 fColorType = SkTMax(fColorType, that->fColorType);
Michael Ludwig69858532018-11-28 15:34:34 -0500272
273 // The helper stores the aa type, but isCompatible(with true arg) allows the two ops' aa
274 // types to be none and coverage, in which case this op's aa type must be lifted to coverage
275 // so that quads with no aa edges can be batched with quads that have some/all edges aa'ed.
276 if (fHelper.aaType() == GrAAType::kNone && that->fHelper.aaType() == GrAAType::kCoverage) {
277 fHelper.setAAType(GrAAType::kCoverage);
278 }
279
Michael Ludwigc96fc372019-01-08 15:46:15 -0500280 fDeviceQuads.concat(that->fDeviceQuads);
Michael Ludwigdcd48212019-01-08 15:28:57 -0500281 if (!fHelper.isTrivial()) {
282 fLocalQuads.concat(that->fLocalQuads);
283 }
Michael Ludwig69858532018-11-28 15:34:34 -0500284 return CombineResult::kMerged;
285 }
286
287 // Similar to onCombineIfPossible, but adds a quad assuming its op would have been compatible.
288 // But since it's avoiding the op list management, it must update the op's bounds. This is only
289 // used with quad sets, which uses the same view matrix for each quad so this assumes that the
290 // device quad type of the new quad is the same as the op's.
Michael Ludwigc96fc372019-01-08 15:46:15 -0500291 void addQuad(const GrPerspQuad& deviceQuad, const GrPerspQuad& localQuad,
292 GrQuadType localQuadType, const SkPMColor4f& color, GrQuadAAFlags edgeAA,
293 GrAAType aaType) {
294 SkASSERT(deviceQuad.quadType() <= fDeviceQuads.quadType());
Michael Ludwig69858532018-11-28 15:34:34 -0500295
296 // The new quad's aa type should be the same as the first quad's or none, except when the
297 // first quad's aa type was already downgraded to none, in which case the stored type must
298 // be lifted to back to the requested type.
299 if (aaType != fHelper.aaType()) {
300 if (aaType != GrAAType::kNone) {
301 // Original quad was downgraded to non-aa, lift back up to this quad's required type
302 SkASSERT(fHelper.aaType() == GrAAType::kNone);
303 fHelper.setAAType(aaType);
304 }
305 // else the new quad could have been downgraded but the other quads can't be, so don't
306 // reset the op's accumulated aa type.
307 }
308
Michael Ludwig69858532018-11-28 15:34:34 -0500309 // Update the bounds and add the quad to this op's storage
310 SkRect newBounds = this->bounds();
Michael Ludwigc96fc372019-01-08 15:46:15 -0500311 newBounds.joinPossiblyEmptyRect(deviceQuad.bounds(fDeviceQuads.quadType()));
Michael Ludwig69858532018-11-28 15:34:34 -0500312 this->setBounds(newBounds, HasAABloat(fHelper.aaType() == GrAAType::kCoverage),
313 IsZeroArea::kNo);
Michael Ludwigc96fc372019-01-08 15:46:15 -0500314 fDeviceQuads.push_back(deviceQuad, fDeviceQuads.quadType(), { color, edgeAA });
Michael Ludwigdcd48212019-01-08 15:28:57 -0500315 if (!fHelper.isTrivial()) {
316 fLocalQuads.push_back(localQuad, localQuadType);
317 }
Michael Ludwig69858532018-11-28 15:34:34 -0500318 }
319
Michael Ludwigc96fc372019-01-08 15:46:15 -0500320 int quadCount() const {
321 // Sanity check that the parallel arrays for quad properties all have the same size
Michael Ludwigdcd48212019-01-08 15:28:57 -0500322 SkASSERT(fDeviceQuads.count() == fLocalQuads.count() ||
323 (fLocalQuads.count() == 0 && fHelper.isTrivial()));
Michael Ludwigc96fc372019-01-08 15:46:15 -0500324 return fDeviceQuads.count();
325 }
326
327 struct ColorAndAA {
328 SkPMColor4f fColor;
329 GrQuadAAFlags fAAFlags;
330 };
Michael Ludwig69858532018-11-28 15:34:34 -0500331
332 Helper fHelper;
Michael Ludwigc96fc372019-01-08 15:46:15 -0500333 GrTQuadList<ColorAndAA> fDeviceQuads;
Michael Ludwigdcd48212019-01-08 15:28:57 -0500334 // No metadata attached to the local quads; this list is empty when local coords are not needed.
Michael Ludwigc96fc372019-01-08 15:46:15 -0500335 GrQuadList fLocalQuads;
Michael Ludwig69858532018-11-28 15:34:34 -0500336
Brian Salomon1d835422019-03-13 16:11:44 -0400337 ColorType fColorType;
Michael Ludwig69858532018-11-28 15:34:34 -0500338
Michael Ludwig69858532018-11-28 15:34:34 -0500339 typedef GrMeshDrawOp INHERITED;
340};
341
342} // anonymous namespace
343
344namespace GrFillRectOp {
345
Robert Phillipsb97da532019-02-12 15:24:12 -0500346std::unique_ptr<GrDrawOp> MakePerEdge(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500347 GrPaint&& paint,
348 GrAAType aaType,
349 GrQuadAAFlags edgeAA,
350 const SkMatrix& viewMatrix,
351 const SkRect& rect,
352 const GrUserStencilSettings* stencilSettings) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500353 GrQuadType dstQuadType = GrQuadTypeForTransformedRect(viewMatrix);
Michael Ludwig69858532018-11-28 15:34:34 -0500354 return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings,
Michael Ludwige9c57d32019-02-13 13:39:39 -0500355 GrPerspQuad::MakeFromRect(rect, viewMatrix), dstQuadType,
356 GrPerspQuad(rect), GrQuadType::kRect);
Michael Ludwig69858532018-11-28 15:34:34 -0500357}
358
Robert Phillipsb97da532019-02-12 15:24:12 -0500359std::unique_ptr<GrDrawOp> MakePerEdgeWithLocalMatrix(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500360 GrPaint&& paint,
361 GrAAType aaType,
362 GrQuadAAFlags edgeAA,
363 const SkMatrix& viewMatrix,
364 const SkMatrix& localMatrix,
365 const SkRect& rect,
366 const GrUserStencilSettings* stencilSettings) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500367 GrQuadType dstQuadType = GrQuadTypeForTransformedRect(viewMatrix);
Michael Ludwig69858532018-11-28 15:34:34 -0500368 GrQuadType localQuadType = GrQuadTypeForTransformedRect(localMatrix);
369 return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings,
Michael Ludwige9c57d32019-02-13 13:39:39 -0500370 GrPerspQuad::MakeFromRect(rect, viewMatrix), dstQuadType,
371 GrPerspQuad::MakeFromRect(rect, localMatrix), localQuadType);
Michael Ludwig69858532018-11-28 15:34:34 -0500372}
373
Robert Phillipsb97da532019-02-12 15:24:12 -0500374std::unique_ptr<GrDrawOp> MakePerEdgeWithLocalRect(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500375 GrPaint&& paint,
376 GrAAType aaType,
377 GrQuadAAFlags edgeAA,
378 const SkMatrix& viewMatrix,
379 const SkRect& rect,
380 const SkRect& localRect,
381 const GrUserStencilSettings* stencilSettings) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500382 GrQuadType dstQuadType = GrQuadTypeForTransformedRect(viewMatrix);
Michael Ludwig69858532018-11-28 15:34:34 -0500383 return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings,
Michael Ludwige9c57d32019-02-13 13:39:39 -0500384 GrPerspQuad::MakeFromRect(rect, viewMatrix), dstQuadType,
385 GrPerspQuad(localRect), GrQuadType::kRect);
Michael Ludwig69858532018-11-28 15:34:34 -0500386}
387
Michael Ludwig009b92e2019-02-15 16:03:53 -0500388std::unique_ptr<GrDrawOp> MakePerEdgeQuad(GrRecordingContext* context,
389 GrPaint&& paint,
390 GrAAType aaType,
391 GrQuadAAFlags edgeAA,
392 const SkMatrix& viewMatrix,
393 const SkPoint quad[4],
394 const SkPoint localQuad[4],
395 const GrUserStencilSettings* stencilSettings) {
396 // With arbitrary quads, the quad types are limited to kStandard or kPerspective (unless we
397 // analyzed the points, but callers have more knowledge and should've just use the appropriate
398 // factory, so assume they can't be rectilinear or simpler)
399 GrQuadType deviceType = viewMatrix.hasPerspective() ? GrQuadType::kPerspective
400 : GrQuadType::kStandard;
401 return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings,
402 GrPerspQuad::MakeFromSkQuad(quad, viewMatrix), deviceType,
403 GrPerspQuad::MakeFromSkQuad(localQuad ? localQuad : quad,
404 SkMatrix::I()), GrQuadType::kStandard);
405}
406
Robert Phillipsb97da532019-02-12 15:24:12 -0500407std::unique_ptr<GrDrawOp> MakeSet(GrRecordingContext* context,
Michael Ludwig69858532018-11-28 15:34:34 -0500408 GrPaint&& paint,
409 GrAAType aaType,
410 const SkMatrix& viewMatrix,
411 const GrRenderTargetContext::QuadSetEntry quads[],
412 int cnt,
413 const GrUserStencilSettings* stencilSettings) {
414 // First make a draw op for the first quad in the set
415 SkASSERT(cnt > 0);
416 GrQuadType deviceQuadType = GrQuadTypeForTransformedRect(viewMatrix);
417
418 paint.setColor4f(quads[0].fColor);
419 std::unique_ptr<GrDrawOp> op = FillRectOp::Make(context, std::move(paint), aaType,
Michael Ludwige9c57d32019-02-13 13:39:39 -0500420 quads[0].fAAFlags, stencilSettings,
421 GrPerspQuad::MakeFromRect(quads[0].fRect, viewMatrix), deviceQuadType,
422 GrPerspQuad::MakeFromRect(quads[0].fRect, quads[0].fLocalMatrix),
Michael Ludwig69858532018-11-28 15:34:34 -0500423 GrQuadTypeForTransformedRect(quads[0].fLocalMatrix));
424 auto* fillRects = op->cast<FillRectOp>();
425
426 // Accumulate remaining quads similar to onCombineIfPossible() without creating an op
427 for (int i = 1; i < cnt; ++i) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500428 GrPerspQuad deviceQuad = GrPerspQuad::MakeFromRect(quads[i].fRect, viewMatrix);
Michael Ludwig69858532018-11-28 15:34:34 -0500429
430 GrAAType resolvedAA;
431 GrQuadAAFlags resolvedEdgeFlags;
432 GrResolveAATypeForQuad(aaType, quads[i].fAAFlags, deviceQuad, deviceQuadType,
433 &resolvedAA, &resolvedEdgeFlags);
434
Michael Ludwige9c57d32019-02-13 13:39:39 -0500435 fillRects->addQuad(deviceQuad,
436 GrPerspQuad::MakeFromRect(quads[i].fRect, quads[i].fLocalMatrix),
Michael Ludwigc96fc372019-01-08 15:46:15 -0500437 GrQuadTypeForTransformedRect(quads[i].fLocalMatrix), quads[i].fColor,
438 resolvedEdgeFlags,resolvedAA);
Michael Ludwig69858532018-11-28 15:34:34 -0500439 }
440
441 return op;
442}
443
Robert Phillipsb97da532019-02-12 15:24:12 -0500444std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500445 GrPaint&& paint,
446 GrAAType aaType,
447 const SkMatrix& viewMatrix,
448 const SkRect& rect,
449 const GrUserStencilSettings* stencil) {
450 return MakePerEdge(context, std::move(paint), aaType,
451 aaType == GrAAType::kCoverage ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone,
452 viewMatrix, rect, stencil);
453}
454
Robert Phillipsb97da532019-02-12 15:24:12 -0500455std::unique_ptr<GrDrawOp> MakeWithLocalMatrix(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500456 GrPaint&& paint,
457 GrAAType aaType,
458 const SkMatrix& viewMatrix,
459 const SkMatrix& localMatrix,
460 const SkRect& rect,
461 const GrUserStencilSettings* stencil) {
462 return MakePerEdgeWithLocalMatrix(context, std::move(paint), aaType,
463 aaType == GrAAType::kCoverage ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone,
464 viewMatrix, localMatrix, rect, stencil);
465}
466
Robert Phillipsb97da532019-02-12 15:24:12 -0500467std::unique_ptr<GrDrawOp> MakeWithLocalRect(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500468 GrPaint&& paint,
469 GrAAType aaType,
470 const SkMatrix& viewMatrix,
471 const SkRect& rect,
472 const SkRect& localRect,
473 const GrUserStencilSettings* stencil) {
474 return MakePerEdgeWithLocalRect(context, std::move(paint), aaType,
475 aaType == GrAAType::kCoverage ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone,
476 viewMatrix, rect, localRect, stencil);
477}
478
Michael Ludwig69858532018-11-28 15:34:34 -0500479} // namespace GrFillRectOp
480
481#if GR_TEST_UTILS
482
483#include "GrDrawOpTest.h"
484#include "SkGr.h"
485
486GR_DRAW_OP_TEST_DEFINE(FillRectOp) {
487 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
488 SkRect rect = GrTest::TestRect(random);
489
490 GrAAType aaType = GrAAType::kNone;
491 if (random->nextBool()) {
492 aaType = (fsaaType == GrFSAAType::kUnifiedMSAA) ? GrAAType::kMSAA : GrAAType::kCoverage;
493 }
494 const GrUserStencilSettings* stencil = random->nextBool() ? nullptr
495 : GrGetRandomStencil(random, context);
496
497 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
498 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
499 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
500 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
501 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
502
503 if (random->nextBool()) {
504 if (random->nextBool()) {
505 if (random->nextBool()) {
506 // Local matrix with a set op
507 uint32_t extraQuadCt = random->nextRangeU(1, 4);
508 SkTArray<GrRenderTargetContext::QuadSetEntry> quads(extraQuadCt + 1);
509 quads.push_back(
510 {rect, SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU())),
511 GrTest::TestMatrixInvertible(random), aaFlags});
512 for (uint32_t i = 0; i < extraQuadCt; ++i) {
513 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
514 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
515 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
516 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
517 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
518
519 quads.push_back(
520 {GrTest::TestRect(random),
521 SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU())),
522 GrTest::TestMatrixInvertible(random), aaFlags});
523 }
524
525 return GrFillRectOp::MakeSet(context, std::move(paint), aaType, viewMatrix,
526 quads.begin(), quads.count(), stencil);
527 } else {
528 // Single local matrix
529 SkMatrix localMatrix = GrTest::TestMatrixInvertible(random);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500530 return GrFillRectOp::MakePerEdgeWithLocalMatrix(context, std::move(paint), aaType,
531 aaFlags, viewMatrix, localMatrix,
532 rect, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500533 }
534 } else {
535 // Pass local rect directly
536 SkRect localRect = GrTest::TestRect(random);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500537 return GrFillRectOp::MakePerEdgeWithLocalRect(context, std::move(paint), aaType,
538 aaFlags, viewMatrix, rect, localRect,
539 stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500540 }
541 } else {
542 // The simplest constructor
Michael Ludwig72ab3462018-12-10 12:43:36 -0500543 return GrFillRectOp::MakePerEdge(context, std::move(paint), aaType, aaFlags, viewMatrix,
544 rect, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500545 }
546}
547
548#endif