blob: bdf1a2eac9d01d2d4cf9860c86090fe51b83f859 [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
10#include "GrGeometryProcessor.h"
11#include "GrMeshDrawOp.h"
12#include "GrPaint.h"
13#include "GrQuad.h"
14#include "GrQuadPerEdgeAA.h"
15#include "GrSimpleMeshDrawOpHelper.h"
16#include "SkMatrix.h"
17#include "SkRect.h"
18#include "glsl/GrGLSLColorSpaceXformHelper.h"
19#include "glsl/GrGLSLGeometryProcessor.h"
20#include "glsl/GrGLSLVarying.h"
21
22namespace {
23
24using VertexSpec = GrQuadPerEdgeAA::VertexSpec;
25using ColorType = GrQuadPerEdgeAA::ColorType;
26
Michael Ludwigc96fc372019-01-08 15:46:15 -050027#ifdef SK_DEBUG
28static SkString dump_quad_info(int index, const GrPerspQuad& deviceQuad,
29 const GrPerspQuad& localQuad, const SkPMColor4f& color,
30 GrQuadAAFlags aaFlags) {
31 SkString str;
32 str.appendf("%d: Color: [%.2f, %.2f, %.2f, %.2f], Edge AA: l%u_t%u_r%u_b%u, \n"
33 " device quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), "
34 "(%.2f, %.2f, %.2f)],\n"
35 " local quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), "
36 "(%.2f, %.2f, %.2f)]\n",
37 index, color.fR, color.fG, color.fB, color.fA,
38 (uint32_t) (aaFlags & GrQuadAAFlags::kLeft),
39 (uint32_t) (aaFlags & GrQuadAAFlags::kTop),
40 (uint32_t) (aaFlags & GrQuadAAFlags::kRight),
41 (uint32_t) (aaFlags & GrQuadAAFlags::kBottom),
42 deviceQuad.x(0), deviceQuad.y(0), deviceQuad.w(0),
43 deviceQuad.x(1), deviceQuad.y(1), deviceQuad.w(1),
44 deviceQuad.x(2), deviceQuad.y(2), deviceQuad.w(2),
45 deviceQuad.x(3), deviceQuad.y(3), deviceQuad.w(3),
46 localQuad.x(0), localQuad.y(0), localQuad.w(0),
47 localQuad.x(1), localQuad.y(1), localQuad.w(1),
48 localQuad.x(2), localQuad.y(2), localQuad.w(2),
49 localQuad.x(3), localQuad.y(3), localQuad.w(3));
50 return str;
51}
52#endif
Michael Ludwig69858532018-11-28 15:34:34 -050053
Michael Ludwig69858532018-11-28 15:34:34 -050054class FillRectOp final : public GrMeshDrawOp {
55private:
56 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
57
58public:
Robert Phillipsb97da532019-02-12 15:24:12 -050059 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwig69858532018-11-28 15:34:34 -050060 GrPaint&& paint,
61 GrAAType aaType,
62 GrQuadAAFlags edgeAA,
63 const GrUserStencilSettings* stencilSettings,
64 const GrPerspQuad& deviceQuad,
65 GrQuadType deviceQuadType,
66 const GrPerspQuad& localQuad,
67 GrQuadType localQuadType) {
68 // Clean up deviations between aaType and edgeAA
69 GrResolveAATypeForQuad(aaType, edgeAA, deviceQuad, deviceQuadType, &aaType, &edgeAA);
Michael Ludwigdcd48212019-01-08 15:28:57 -050070 return Helper::FactoryHelper<FillRectOp>(context, std::move(paint), aaType, edgeAA,
71 stencilSettings, deviceQuad, deviceQuadType, localQuad, localQuadType);
Michael Ludwig69858532018-11-28 15:34:34 -050072 }
73
Michael Ludwigdcd48212019-01-08 15:28:57 -050074 // aaType is passed to Helper in the initializer list, so incongruities between aaType and
75 // edgeFlags must be resolved prior to calling this constructor.
76 FillRectOp(Helper::MakeArgs args, SkPMColor4f paintColor, GrAAType aaType,
77 GrQuadAAFlags edgeFlags, const GrUserStencilSettings* stencil,
Michael Ludwig69858532018-11-28 15:34:34 -050078 const GrPerspQuad& deviceQuad, GrQuadType deviceQuadType,
79 const GrPerspQuad& localQuad, GrQuadType localQuadType)
80 : INHERITED(ClassID())
Michael Ludwigdcd48212019-01-08 15:28:57 -050081 , fHelper(args, aaType, stencil)
82 , fWideColor(!SkPMColor4fFitsInBytes(paintColor)) {
Michael Ludwig69858532018-11-28 15:34:34 -050083 // The color stored with the quad is the clear color if a scissor-clear is decided upon
84 // when executing the op.
Michael Ludwigc96fc372019-01-08 15:46:15 -050085 fDeviceQuads.push_back(deviceQuad, deviceQuadType, { paintColor, edgeFlags });
Michael Ludwigdcd48212019-01-08 15:28:57 -050086
87 if (!fHelper.isTrivial()) {
88 // Conservatively keep track of the local coordinates; it may be that the paint doesn't
89 // need them after analysis is finished. If the paint is known to be solid up front they
90 // can be skipped entirely.
91 fLocalQuads.push_back(localQuad, localQuadType);
92 }
Michael Ludwigc96fc372019-01-08 15:46:15 -050093 this->setBounds(deviceQuad.bounds(deviceQuadType),
94 HasAABloat(aaType == GrAAType::kCoverage), IsZeroArea::kNo);
Michael Ludwig69858532018-11-28 15:34:34 -050095 }
96
97 const char* name() const override { return "FillRectOp"; }
98
99 void visitProxies(const VisitProxyFunc& func, VisitorType) const override {
100 return fHelper.visitProxies(func);
101 }
102
103#ifdef SK_DEBUG
104 SkString dumpInfo() const override {
105 SkString str;
Michael Ludwigc96fc372019-01-08 15:46:15 -0500106 str.appendf("# draws: %u\n", this->quadCount());
Michael Ludwig69858532018-11-28 15:34:34 -0500107 str.appendf("Device quad type: %u, local quad type: %u\n",
Michael Ludwigc96fc372019-01-08 15:46:15 -0500108 (uint32_t) fDeviceQuads.quadType(), (uint32_t) fLocalQuads.quadType());
Michael Ludwig69858532018-11-28 15:34:34 -0500109 str += fHelper.dumpInfo();
Michael Ludwigc96fc372019-01-08 15:46:15 -0500110 GrPerspQuad device, local;
111 for (int i = 0; i < this->quadCount(); i++) {
112 device = fDeviceQuads[i];
113 const ColorAndAA& info = fDeviceQuads.metadata(i);
Michael Ludwigdcd48212019-01-08 15:28:57 -0500114 if (!fHelper.isTrivial()) {
115 local = fLocalQuads[i];
116 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500117 str += dump_quad_info(i, device, local, info.fColor, info.fAAFlags);
Michael Ludwig69858532018-11-28 15:34:34 -0500118 }
119 str += INHERITED::dumpInfo();
120 return str;
121 }
122#endif
123
Chris Dalton4b62aed2019-01-15 11:53:00 -0700124 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
Michael Ludwig69858532018-11-28 15:34:34 -0500125 // Initialize aggregate color analysis with the first quad's color (which always exists)
Michael Ludwigc96fc372019-01-08 15:46:15 -0500126 SkASSERT(this->quadCount() > 0);
127 GrProcessorAnalysisColor quadColors(fDeviceQuads.metadata(0).fColor);
Michael Ludwig69858532018-11-28 15:34:34 -0500128 // Then combine the colors of any additional quads (e.g. from MakeSet)
Michael Ludwigc96fc372019-01-08 15:46:15 -0500129 for (int i = 1; i < this->quadCount(); ++i) {
130 quadColors = GrProcessorAnalysisColor::Combine(quadColors,
131 fDeviceQuads.metadata(i).fColor);
Michael Ludwigca91e1f2018-12-10 10:44:44 -0500132 if (quadColors.isUnknown()) {
133 // No point in accumulating additional starting colors, combining cannot make it
134 // less unknown.
135 break;
136 }
Michael Ludwig69858532018-11-28 15:34:34 -0500137 }
Michael Ludwig72ab3462018-12-10 12:43:36 -0500138
139 // If the AA type is coverage, it will be a single value per pixel; if it's not coverage AA
140 // then the coverage is always 1.0, so specify kNone for more optimal blending.
141 GrProcessorAnalysisCoverage coverage = fHelper.aaType() == GrAAType::kCoverage ?
142 GrProcessorAnalysisCoverage::kSingleChannel :
143 GrProcessorAnalysisCoverage::kNone;
Chris Dalton4b62aed2019-01-15 11:53:00 -0700144 auto result = fHelper.finalizeProcessors(caps, clip, coverage, &quadColors);
Michael Ludwig69858532018-11-28 15:34:34 -0500145 // If there is a constant color after analysis, that means all of the quads should be set
146 // to the same color (even if they started out with different colors).
147 SkPMColor4f colorOverride;
148 if (quadColors.isConstant(&colorOverride)) {
Michael Ludwigc96fc372019-01-08 15:46:15 -0500149 for (int i = 0; i < this->quadCount(); ++i) {
150 fDeviceQuads.metadata(i).fColor = colorOverride;
Michael Ludwig69858532018-11-28 15:34:34 -0500151 }
152 }
153
154 return result;
155 }
156
157 FixedFunctionFlags fixedFunctionFlags() const override {
158 // Since the AA type of the whole primitive is kept consistent with the per edge AA flags
159 // the helper's fixed function flags are appropriate.
160 return fHelper.fixedFunctionFlags();
161 }
162
163 DEFINE_OP_CLASS_ID
164
165private:
166 // For GrFillRectOp::MakeSet's use of addQuad
Robert Phillipsb97da532019-02-12 15:24:12 -0500167 friend std::unique_ptr<GrDrawOp> GrFillRectOp::MakeSet(
168 GrRecordingContext*,
169 GrPaint&&,
170 GrAAType, const SkMatrix& viewMatrix,
Michael Ludwig69858532018-11-28 15:34:34 -0500171 const GrRenderTargetContext::QuadSetEntry quads[], int quadCount,
Robert Phillipsb97da532019-02-12 15:24:12 -0500172 const GrUserStencilSettings*);
Michael Ludwig69858532018-11-28 15:34:34 -0500173
Michael Ludwigc96fc372019-01-08 15:46:15 -0500174 void onPrepareDraws(Target* target) override {
Michael Ludwig69858532018-11-28 15:34:34 -0500175 TRACE_EVENT0("skia", TRACE_FUNC);
176
177 using Domain = GrQuadPerEdgeAA::Domain;
178 static constexpr SkRect kEmptyDomain = SkRect::MakeEmpty();
179
Michael Ludwigc96fc372019-01-08 15:46:15 -0500180 VertexSpec vertexSpec(fDeviceQuads.quadType(),
Michael Ludwig69858532018-11-28 15:34:34 -0500181 fWideColor ? ColorType::kHalf : ColorType::kByte,
Michael Ludwigc96fc372019-01-08 15:46:15 -0500182 fLocalQuads.quadType(), fHelper.usesLocalCoords(), Domain::kNo,
Michael Ludwig93aeba02018-12-21 09:50:31 -0500183 fHelper.aaType(), fHelper.compatibleWithAlphaAsCoverage());
Michael Ludwigdcd48212019-01-08 15:28:57 -0500184 // Make sure that if the op thought it was a solid color, the vertex spec does not use
185 // local coords.
186 SkASSERT(!fHelper.isTrivial() || !fHelper.usesLocalCoords());
Michael Ludwig69858532018-11-28 15:34:34 -0500187
Michael Ludwig467994d2018-12-03 14:58:31 +0000188 sk_sp<GrGeometryProcessor> gp = GrQuadPerEdgeAA::MakeProcessor(vertexSpec);
Michael Ludwig69858532018-11-28 15:34:34 -0500189 size_t vertexSize = gp->vertexStride();
190
Brian Salomon12d22642019-01-29 14:38:50 -0500191 sk_sp<const GrBuffer> vbuffer;
Michael Ludwig69858532018-11-28 15:34:34 -0500192 int vertexOffsetInBuffer = 0;
193
194 // Fill the allocated vertex data
Michael Ludwig93aeba02018-12-21 09:50:31 -0500195 void* vdata = target->makeVertexSpace(
Michael Ludwigc96fc372019-01-08 15:46:15 -0500196 vertexSize, this->quadCount() * vertexSpec.verticesPerQuad(),
Michael Ludwig93aeba02018-12-21 09:50:31 -0500197 &vbuffer, &vertexOffsetInBuffer);
Michael Ludwig69858532018-11-28 15:34:34 -0500198 if (!vdata) {
199 SkDebugf("Could not allocate vertices\n");
200 return;
201 }
202
203 // vertices pointer advances through vdata based on Tessellate's return value
204 void* vertices = vdata;
Michael Ludwigdcd48212019-01-08 15:28:57 -0500205 if (fHelper.isTrivial()) {
206 SkASSERT(fLocalQuads.count() == 0); // No local coords, so send an ignored dummy quad
207 static const GrPerspQuad kIgnoredLocal(SkRect::MakeEmpty(), SkMatrix::I());
208 for (int i = 0; i < this->quadCount(); ++i) {
209 const ColorAndAA& info = fDeviceQuads.metadata(i);
210 vertices = GrQuadPerEdgeAA::Tessellate(vertices, vertexSpec, fDeviceQuads[i],
211 info.fColor, kIgnoredLocal, kEmptyDomain, info.fAAFlags);
212 }
213 } else {
214 SkASSERT(fLocalQuads.count() == fDeviceQuads.count());
215 for (int i = 0; i < this->quadCount(); ++i) {
216 const ColorAndAA& info = fDeviceQuads.metadata(i);
217 vertices = GrQuadPerEdgeAA::Tessellate(vertices, vertexSpec, fDeviceQuads[i],
218 info.fColor, fLocalQuads[i], kEmptyDomain, info.fAAFlags);
219 }
Michael Ludwig69858532018-11-28 15:34:34 -0500220 }
221
222 // Configure the mesh for the vertex data
Michael Ludwig93aeba02018-12-21 09:50:31 -0500223 GrMesh* mesh = target->allocMeshes(1);
Michael Ludwigc96fc372019-01-08 15:46:15 -0500224 if (!GrQuadPerEdgeAA::ConfigureMeshIndices(target, mesh, vertexSpec, this->quadCount())) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500225 SkDebugf("Could not allocate indices\n");
226 return;
Michael Ludwig69858532018-11-28 15:34:34 -0500227 }
Brian Salomon12d22642019-01-29 14:38:50 -0500228 mesh->setVertexData(std::move(vbuffer), vertexOffsetInBuffer);
Michael Ludwig69858532018-11-28 15:34:34 -0500229
230 auto pipe = fHelper.makePipeline(target);
231 target->draw(std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState, mesh);
232 }
233
234 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
235 TRACE_EVENT0("skia", TRACE_FUNC);
236 const auto* that = t->cast<FillRectOp>();
237
Michael Ludwig93aeba02018-12-21 09:50:31 -0500238 if ((fHelper.aaType() == GrAAType::kCoverage ||
239 that->fHelper.aaType() == GrAAType::kCoverage) &&
Michael Ludwigc96fc372019-01-08 15:46:15 -0500240 this->quadCount() + that->quadCount() > GrQuadPerEdgeAA::kNumAAQuadsInIndexBuffer) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500241 // This limit on batch size seems to help on Adreno devices
242 return CombineResult::kCannotCombine;
243 }
244
Michael Ludwigc96fc372019-01-08 15:46:15 -0500245 // Unlike most users of the draw op helper, this op can merge none-aa and coverage-aa draw
246 // ops together, so pass true as the last argument.
Michael Ludwig69858532018-11-28 15:34:34 -0500247 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds(), true)) {
248 return CombineResult::kCannotCombine;
249 }
250
Michael Ludwigdcd48212019-01-08 15:28:57 -0500251 // If the paints were compatible, the trivial/solid-color state should be the same
252 SkASSERT(fHelper.isTrivial() == that->fHelper.isTrivial());
Michael Ludwig69858532018-11-28 15:34:34 -0500253
Michael Ludwigdcd48212019-01-08 15:28:57 -0500254 // If the processor sets are compatible, the two ops are always compatible; it just needs to
255 // adjust the state of the op to be the more general quad and aa types of the two ops and
256 // then concatenate the per-quad data.
Michael Ludwig69858532018-11-28 15:34:34 -0500257 fWideColor |= that->fWideColor;
258
259 // The helper stores the aa type, but isCompatible(with true arg) allows the two ops' aa
260 // types to be none and coverage, in which case this op's aa type must be lifted to coverage
261 // so that quads with no aa edges can be batched with quads that have some/all edges aa'ed.
262 if (fHelper.aaType() == GrAAType::kNone && that->fHelper.aaType() == GrAAType::kCoverage) {
263 fHelper.setAAType(GrAAType::kCoverage);
264 }
265
Michael Ludwigc96fc372019-01-08 15:46:15 -0500266 fDeviceQuads.concat(that->fDeviceQuads);
Michael Ludwigdcd48212019-01-08 15:28:57 -0500267 if (!fHelper.isTrivial()) {
268 fLocalQuads.concat(that->fLocalQuads);
269 }
Michael Ludwig69858532018-11-28 15:34:34 -0500270 return CombineResult::kMerged;
271 }
272
273 // Similar to onCombineIfPossible, but adds a quad assuming its op would have been compatible.
274 // But since it's avoiding the op list management, it must update the op's bounds. This is only
275 // used with quad sets, which uses the same view matrix for each quad so this assumes that the
276 // device quad type of the new quad is the same as the op's.
Michael Ludwigc96fc372019-01-08 15:46:15 -0500277 void addQuad(const GrPerspQuad& deviceQuad, const GrPerspQuad& localQuad,
278 GrQuadType localQuadType, const SkPMColor4f& color, GrQuadAAFlags edgeAA,
279 GrAAType aaType) {
280 SkASSERT(deviceQuad.quadType() <= fDeviceQuads.quadType());
Michael Ludwig69858532018-11-28 15:34:34 -0500281
282 // The new quad's aa type should be the same as the first quad's or none, except when the
283 // first quad's aa type was already downgraded to none, in which case the stored type must
284 // be lifted to back to the requested type.
285 if (aaType != fHelper.aaType()) {
286 if (aaType != GrAAType::kNone) {
287 // Original quad was downgraded to non-aa, lift back up to this quad's required type
288 SkASSERT(fHelper.aaType() == GrAAType::kNone);
289 fHelper.setAAType(aaType);
290 }
291 // else the new quad could have been downgraded but the other quads can't be, so don't
292 // reset the op's accumulated aa type.
293 }
294
Michael Ludwig69858532018-11-28 15:34:34 -0500295 // clear compatible won't need to be updated, since device quad type and paint is the same,
296 // but this quad has a new color, so maybe update wide color
Michael Ludwigc96fc372019-01-08 15:46:15 -0500297 fWideColor |= !SkPMColor4fFitsInBytes(color);
Michael Ludwig69858532018-11-28 15:34:34 -0500298
299 // Update the bounds and add the quad to this op's storage
300 SkRect newBounds = this->bounds();
Michael Ludwigc96fc372019-01-08 15:46:15 -0500301 newBounds.joinPossiblyEmptyRect(deviceQuad.bounds(fDeviceQuads.quadType()));
Michael Ludwig69858532018-11-28 15:34:34 -0500302 this->setBounds(newBounds, HasAABloat(fHelper.aaType() == GrAAType::kCoverage),
303 IsZeroArea::kNo);
Michael Ludwigc96fc372019-01-08 15:46:15 -0500304 fDeviceQuads.push_back(deviceQuad, fDeviceQuads.quadType(), { color, edgeAA });
Michael Ludwigdcd48212019-01-08 15:28:57 -0500305 if (!fHelper.isTrivial()) {
306 fLocalQuads.push_back(localQuad, localQuadType);
307 }
Michael Ludwig69858532018-11-28 15:34:34 -0500308 }
309
Michael Ludwigc96fc372019-01-08 15:46:15 -0500310 int quadCount() const {
311 // Sanity check that the parallel arrays for quad properties all have the same size
Michael Ludwigdcd48212019-01-08 15:28:57 -0500312 SkASSERT(fDeviceQuads.count() == fLocalQuads.count() ||
313 (fLocalQuads.count() == 0 && fHelper.isTrivial()));
Michael Ludwigc96fc372019-01-08 15:46:15 -0500314 return fDeviceQuads.count();
315 }
316
317 struct ColorAndAA {
318 SkPMColor4f fColor;
319 GrQuadAAFlags fAAFlags;
320 };
Michael Ludwig69858532018-11-28 15:34:34 -0500321
322 Helper fHelper;
Michael Ludwigc96fc372019-01-08 15:46:15 -0500323 GrTQuadList<ColorAndAA> fDeviceQuads;
Michael Ludwigdcd48212019-01-08 15:28:57 -0500324 // No metadata attached to the local quads; this list is empty when local coords are not needed.
Michael Ludwigc96fc372019-01-08 15:46:15 -0500325 GrQuadList fLocalQuads;
Michael Ludwig69858532018-11-28 15:34:34 -0500326
Michael Ludwig69858532018-11-28 15:34:34 -0500327 unsigned fWideColor: 1;
328
Michael Ludwig69858532018-11-28 15:34:34 -0500329 typedef GrMeshDrawOp INHERITED;
330};
331
332} // anonymous namespace
333
334namespace GrFillRectOp {
335
Robert Phillipsb97da532019-02-12 15:24:12 -0500336std::unique_ptr<GrDrawOp> MakePerEdge(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500337 GrPaint&& paint,
338 GrAAType aaType,
339 GrQuadAAFlags edgeAA,
340 const SkMatrix& viewMatrix,
341 const SkRect& rect,
342 const GrUserStencilSettings* stencilSettings) {
Michael Ludwig69858532018-11-28 15:34:34 -0500343 return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings,
344 GrPerspQuad(rect, viewMatrix), GrQuadTypeForTransformedRect(viewMatrix),
345 GrPerspQuad(rect, SkMatrix::I()), GrQuadType::kRect);
346}
347
Robert Phillipsb97da532019-02-12 15:24:12 -0500348std::unique_ptr<GrDrawOp> MakePerEdgeWithLocalMatrix(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500349 GrPaint&& paint,
350 GrAAType aaType,
351 GrQuadAAFlags edgeAA,
352 const SkMatrix& viewMatrix,
353 const SkMatrix& localMatrix,
354 const SkRect& rect,
355 const GrUserStencilSettings* stencilSettings) {
Michael Ludwig69858532018-11-28 15:34:34 -0500356 GrQuadType localQuadType = GrQuadTypeForTransformedRect(localMatrix);
357 return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings,
358 GrPerspQuad(rect, viewMatrix), GrQuadTypeForTransformedRect(viewMatrix),
359 GrPerspQuad(rect, localMatrix), localQuadType);
360}
361
Robert Phillipsb97da532019-02-12 15:24:12 -0500362std::unique_ptr<GrDrawOp> MakePerEdgeWithLocalRect(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500363 GrPaint&& paint,
364 GrAAType aaType,
365 GrQuadAAFlags edgeAA,
366 const SkMatrix& viewMatrix,
367 const SkRect& rect,
368 const SkRect& localRect,
369 const GrUserStencilSettings* stencilSettings) {
Michael Ludwig69858532018-11-28 15:34:34 -0500370 return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings,
371 GrPerspQuad(rect, viewMatrix), GrQuadTypeForTransformedRect(viewMatrix),
372 GrPerspQuad(localRect, SkMatrix::I()), GrQuadType::kRect);
373}
374
Robert Phillipsb97da532019-02-12 15:24:12 -0500375std::unique_ptr<GrDrawOp> MakeSet(GrRecordingContext* context,
Michael Ludwig69858532018-11-28 15:34:34 -0500376 GrPaint&& paint,
377 GrAAType aaType,
378 const SkMatrix& viewMatrix,
379 const GrRenderTargetContext::QuadSetEntry quads[],
380 int cnt,
381 const GrUserStencilSettings* stencilSettings) {
382 // First make a draw op for the first quad in the set
383 SkASSERT(cnt > 0);
384 GrQuadType deviceQuadType = GrQuadTypeForTransformedRect(viewMatrix);
385
386 paint.setColor4f(quads[0].fColor);
387 std::unique_ptr<GrDrawOp> op = FillRectOp::Make(context, std::move(paint), aaType,
388 quads[0].fAAFlags, stencilSettings, GrPerspQuad(quads[0].fRect, viewMatrix),
389 deviceQuadType, GrPerspQuad(quads[0].fRect, quads[0].fLocalMatrix),
390 GrQuadTypeForTransformedRect(quads[0].fLocalMatrix));
391 auto* fillRects = op->cast<FillRectOp>();
392
393 // Accumulate remaining quads similar to onCombineIfPossible() without creating an op
394 for (int i = 1; i < cnt; ++i) {
395 GrPerspQuad deviceQuad(quads[i].fRect, viewMatrix);
396
397 GrAAType resolvedAA;
398 GrQuadAAFlags resolvedEdgeFlags;
399 GrResolveAATypeForQuad(aaType, quads[i].fAAFlags, deviceQuad, deviceQuadType,
400 &resolvedAA, &resolvedEdgeFlags);
401
Michael Ludwigc96fc372019-01-08 15:46:15 -0500402 fillRects->addQuad(deviceQuad, GrPerspQuad(quads[i].fRect, quads[i].fLocalMatrix),
403 GrQuadTypeForTransformedRect(quads[i].fLocalMatrix), quads[i].fColor,
404 resolvedEdgeFlags,resolvedAA);
Michael Ludwig69858532018-11-28 15:34:34 -0500405 }
406
407 return op;
408}
409
Robert Phillipsb97da532019-02-12 15:24:12 -0500410std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500411 GrPaint&& paint,
412 GrAAType aaType,
413 const SkMatrix& viewMatrix,
414 const SkRect& rect,
415 const GrUserStencilSettings* stencil) {
416 return MakePerEdge(context, std::move(paint), aaType,
417 aaType == GrAAType::kCoverage ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone,
418 viewMatrix, rect, stencil);
419}
420
Robert Phillipsb97da532019-02-12 15:24:12 -0500421std::unique_ptr<GrDrawOp> MakeWithLocalMatrix(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500422 GrPaint&& paint,
423 GrAAType aaType,
424 const SkMatrix& viewMatrix,
425 const SkMatrix& localMatrix,
426 const SkRect& rect,
427 const GrUserStencilSettings* stencil) {
428 return MakePerEdgeWithLocalMatrix(context, std::move(paint), aaType,
429 aaType == GrAAType::kCoverage ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone,
430 viewMatrix, localMatrix, rect, stencil);
431}
432
Robert Phillipsb97da532019-02-12 15:24:12 -0500433std::unique_ptr<GrDrawOp> MakeWithLocalRect(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500434 GrPaint&& paint,
435 GrAAType aaType,
436 const SkMatrix& viewMatrix,
437 const SkRect& rect,
438 const SkRect& localRect,
439 const GrUserStencilSettings* stencil) {
440 return MakePerEdgeWithLocalRect(context, std::move(paint), aaType,
441 aaType == GrAAType::kCoverage ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone,
442 viewMatrix, rect, localRect, stencil);
443}
444
Michael Ludwig69858532018-11-28 15:34:34 -0500445} // namespace GrFillRectOp
446
447#if GR_TEST_UTILS
448
449#include "GrDrawOpTest.h"
450#include "SkGr.h"
451
452GR_DRAW_OP_TEST_DEFINE(FillRectOp) {
453 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
454 SkRect rect = GrTest::TestRect(random);
455
456 GrAAType aaType = GrAAType::kNone;
457 if (random->nextBool()) {
458 aaType = (fsaaType == GrFSAAType::kUnifiedMSAA) ? GrAAType::kMSAA : GrAAType::kCoverage;
459 }
460 const GrUserStencilSettings* stencil = random->nextBool() ? nullptr
461 : GrGetRandomStencil(random, context);
462
463 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
464 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
465 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
466 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
467 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
468
469 if (random->nextBool()) {
470 if (random->nextBool()) {
471 if (random->nextBool()) {
472 // Local matrix with a set op
473 uint32_t extraQuadCt = random->nextRangeU(1, 4);
474 SkTArray<GrRenderTargetContext::QuadSetEntry> quads(extraQuadCt + 1);
475 quads.push_back(
476 {rect, SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU())),
477 GrTest::TestMatrixInvertible(random), aaFlags});
478 for (uint32_t i = 0; i < extraQuadCt; ++i) {
479 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
480 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
481 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
482 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
483 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
484
485 quads.push_back(
486 {GrTest::TestRect(random),
487 SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU())),
488 GrTest::TestMatrixInvertible(random), aaFlags});
489 }
490
491 return GrFillRectOp::MakeSet(context, std::move(paint), aaType, viewMatrix,
492 quads.begin(), quads.count(), stencil);
493 } else {
494 // Single local matrix
495 SkMatrix localMatrix = GrTest::TestMatrixInvertible(random);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500496 return GrFillRectOp::MakePerEdgeWithLocalMatrix(context, std::move(paint), aaType,
497 aaFlags, viewMatrix, localMatrix,
498 rect, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500499 }
500 } else {
501 // Pass local rect directly
502 SkRect localRect = GrTest::TestRect(random);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500503 return GrFillRectOp::MakePerEdgeWithLocalRect(context, std::move(paint), aaType,
504 aaFlags, viewMatrix, rect, localRect,
505 stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500506 }
507 } else {
508 // The simplest constructor
Michael Ludwig72ab3462018-12-10 12:43:36 -0500509 return GrFillRectOp::MakePerEdge(context, std::move(paint), aaType, aaFlags, viewMatrix,
510 rect, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500511 }
512}
513
514#endif