blob: 8dcec2106ad53591b9434fe5f293ff57f0caa319 [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
Michael Ludwige9c57d32019-02-13 13:39:39 -0500207 static const GrPerspQuad kIgnoredLocal(SkRect::MakeEmpty());
208
Michael Ludwigdcd48212019-01-08 15:28:57 -0500209 for (int i = 0; i < this->quadCount(); ++i) {
210 const ColorAndAA& info = fDeviceQuads.metadata(i);
211 vertices = GrQuadPerEdgeAA::Tessellate(vertices, vertexSpec, fDeviceQuads[i],
212 info.fColor, kIgnoredLocal, kEmptyDomain, info.fAAFlags);
213 }
214 } else {
215 SkASSERT(fLocalQuads.count() == fDeviceQuads.count());
216 for (int i = 0; i < this->quadCount(); ++i) {
217 const ColorAndAA& info = fDeviceQuads.metadata(i);
218 vertices = GrQuadPerEdgeAA::Tessellate(vertices, vertexSpec, fDeviceQuads[i],
219 info.fColor, fLocalQuads[i], kEmptyDomain, info.fAAFlags);
220 }
Michael Ludwig69858532018-11-28 15:34:34 -0500221 }
222
223 // Configure the mesh for the vertex data
Michael Ludwig93aeba02018-12-21 09:50:31 -0500224 GrMesh* mesh = target->allocMeshes(1);
Michael Ludwigc96fc372019-01-08 15:46:15 -0500225 if (!GrQuadPerEdgeAA::ConfigureMeshIndices(target, mesh, vertexSpec, this->quadCount())) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500226 SkDebugf("Could not allocate indices\n");
227 return;
Michael Ludwig69858532018-11-28 15:34:34 -0500228 }
Brian Salomon12d22642019-01-29 14:38:50 -0500229 mesh->setVertexData(std::move(vbuffer), vertexOffsetInBuffer);
Michael Ludwig69858532018-11-28 15:34:34 -0500230
231 auto pipe = fHelper.makePipeline(target);
232 target->draw(std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState, mesh);
233 }
234
235 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
236 TRACE_EVENT0("skia", TRACE_FUNC);
237 const auto* that = t->cast<FillRectOp>();
238
Michael Ludwig93aeba02018-12-21 09:50:31 -0500239 if ((fHelper.aaType() == GrAAType::kCoverage ||
240 that->fHelper.aaType() == GrAAType::kCoverage) &&
Michael Ludwigc96fc372019-01-08 15:46:15 -0500241 this->quadCount() + that->quadCount() > GrQuadPerEdgeAA::kNumAAQuadsInIndexBuffer) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500242 // This limit on batch size seems to help on Adreno devices
243 return CombineResult::kCannotCombine;
244 }
245
Michael Ludwigc96fc372019-01-08 15:46:15 -0500246 // Unlike most users of the draw op helper, this op can merge none-aa and coverage-aa draw
247 // ops together, so pass true as the last argument.
Michael Ludwig69858532018-11-28 15:34:34 -0500248 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds(), true)) {
249 return CombineResult::kCannotCombine;
250 }
251
Michael Ludwigdcd48212019-01-08 15:28:57 -0500252 // If the paints were compatible, the trivial/solid-color state should be the same
253 SkASSERT(fHelper.isTrivial() == that->fHelper.isTrivial());
Michael Ludwig69858532018-11-28 15:34:34 -0500254
Michael Ludwigdcd48212019-01-08 15:28:57 -0500255 // If the processor sets are compatible, the two ops are always compatible; it just needs to
256 // adjust the state of the op to be the more general quad and aa types of the two ops and
257 // then concatenate the per-quad data.
Michael Ludwig69858532018-11-28 15:34:34 -0500258 fWideColor |= that->fWideColor;
259
260 // The helper stores the aa type, but isCompatible(with true arg) allows the two ops' aa
261 // types to be none and coverage, in which case this op's aa type must be lifted to coverage
262 // so that quads with no aa edges can be batched with quads that have some/all edges aa'ed.
263 if (fHelper.aaType() == GrAAType::kNone && that->fHelper.aaType() == GrAAType::kCoverage) {
264 fHelper.setAAType(GrAAType::kCoverage);
265 }
266
Michael Ludwigc96fc372019-01-08 15:46:15 -0500267 fDeviceQuads.concat(that->fDeviceQuads);
Michael Ludwigdcd48212019-01-08 15:28:57 -0500268 if (!fHelper.isTrivial()) {
269 fLocalQuads.concat(that->fLocalQuads);
270 }
Michael Ludwig69858532018-11-28 15:34:34 -0500271 return CombineResult::kMerged;
272 }
273
274 // Similar to onCombineIfPossible, but adds a quad assuming its op would have been compatible.
275 // But since it's avoiding the op list management, it must update the op's bounds. This is only
276 // used with quad sets, which uses the same view matrix for each quad so this assumes that the
277 // device quad type of the new quad is the same as the op's.
Michael Ludwigc96fc372019-01-08 15:46:15 -0500278 void addQuad(const GrPerspQuad& deviceQuad, const GrPerspQuad& localQuad,
279 GrQuadType localQuadType, const SkPMColor4f& color, GrQuadAAFlags edgeAA,
280 GrAAType aaType) {
281 SkASSERT(deviceQuad.quadType() <= fDeviceQuads.quadType());
Michael Ludwig69858532018-11-28 15:34:34 -0500282
283 // The new quad's aa type should be the same as the first quad's or none, except when the
284 // first quad's aa type was already downgraded to none, in which case the stored type must
285 // be lifted to back to the requested type.
286 if (aaType != fHelper.aaType()) {
287 if (aaType != GrAAType::kNone) {
288 // Original quad was downgraded to non-aa, lift back up to this quad's required type
289 SkASSERT(fHelper.aaType() == GrAAType::kNone);
290 fHelper.setAAType(aaType);
291 }
292 // else the new quad could have been downgraded but the other quads can't be, so don't
293 // reset the op's accumulated aa type.
294 }
295
Michael Ludwig69858532018-11-28 15:34:34 -0500296 // clear compatible won't need to be updated, since device quad type and paint is the same,
297 // but this quad has a new color, so maybe update wide color
Michael Ludwigc96fc372019-01-08 15:46:15 -0500298 fWideColor |= !SkPMColor4fFitsInBytes(color);
Michael Ludwig69858532018-11-28 15:34:34 -0500299
300 // Update the bounds and add the quad to this op's storage
301 SkRect newBounds = this->bounds();
Michael Ludwigc96fc372019-01-08 15:46:15 -0500302 newBounds.joinPossiblyEmptyRect(deviceQuad.bounds(fDeviceQuads.quadType()));
Michael Ludwig69858532018-11-28 15:34:34 -0500303 this->setBounds(newBounds, HasAABloat(fHelper.aaType() == GrAAType::kCoverage),
304 IsZeroArea::kNo);
Michael Ludwigc96fc372019-01-08 15:46:15 -0500305 fDeviceQuads.push_back(deviceQuad, fDeviceQuads.quadType(), { color, edgeAA });
Michael Ludwigdcd48212019-01-08 15:28:57 -0500306 if (!fHelper.isTrivial()) {
307 fLocalQuads.push_back(localQuad, localQuadType);
308 }
Michael Ludwig69858532018-11-28 15:34:34 -0500309 }
310
Michael Ludwigc96fc372019-01-08 15:46:15 -0500311 int quadCount() const {
312 // Sanity check that the parallel arrays for quad properties all have the same size
Michael Ludwigdcd48212019-01-08 15:28:57 -0500313 SkASSERT(fDeviceQuads.count() == fLocalQuads.count() ||
314 (fLocalQuads.count() == 0 && fHelper.isTrivial()));
Michael Ludwigc96fc372019-01-08 15:46:15 -0500315 return fDeviceQuads.count();
316 }
317
318 struct ColorAndAA {
319 SkPMColor4f fColor;
320 GrQuadAAFlags fAAFlags;
321 };
Michael Ludwig69858532018-11-28 15:34:34 -0500322
323 Helper fHelper;
Michael Ludwigc96fc372019-01-08 15:46:15 -0500324 GrTQuadList<ColorAndAA> fDeviceQuads;
Michael Ludwigdcd48212019-01-08 15:28:57 -0500325 // No metadata attached to the local quads; this list is empty when local coords are not needed.
Michael Ludwigc96fc372019-01-08 15:46:15 -0500326 GrQuadList fLocalQuads;
Michael Ludwig69858532018-11-28 15:34:34 -0500327
Michael Ludwig69858532018-11-28 15:34:34 -0500328 unsigned fWideColor: 1;
329
Michael Ludwig69858532018-11-28 15:34:34 -0500330 typedef GrMeshDrawOp INHERITED;
331};
332
333} // anonymous namespace
334
335namespace GrFillRectOp {
336
Robert Phillipsb97da532019-02-12 15:24:12 -0500337std::unique_ptr<GrDrawOp> MakePerEdge(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500338 GrPaint&& paint,
339 GrAAType aaType,
340 GrQuadAAFlags edgeAA,
341 const SkMatrix& viewMatrix,
342 const SkRect& rect,
343 const GrUserStencilSettings* stencilSettings) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500344 GrQuadType dstQuadType = GrQuadTypeForTransformedRect(viewMatrix);
Michael Ludwig69858532018-11-28 15:34:34 -0500345 return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings,
Michael Ludwige9c57d32019-02-13 13:39:39 -0500346 GrPerspQuad::MakeFromRect(rect, viewMatrix), dstQuadType,
347 GrPerspQuad(rect), GrQuadType::kRect);
Michael Ludwig69858532018-11-28 15:34:34 -0500348}
349
Robert Phillipsb97da532019-02-12 15:24:12 -0500350std::unique_ptr<GrDrawOp> MakePerEdgeWithLocalMatrix(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500351 GrPaint&& paint,
352 GrAAType aaType,
353 GrQuadAAFlags edgeAA,
354 const SkMatrix& viewMatrix,
355 const SkMatrix& localMatrix,
356 const SkRect& rect,
357 const GrUserStencilSettings* stencilSettings) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500358 GrQuadType dstQuadType = GrQuadTypeForTransformedRect(viewMatrix);
Michael Ludwig69858532018-11-28 15:34:34 -0500359 GrQuadType localQuadType = GrQuadTypeForTransformedRect(localMatrix);
360 return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings,
Michael Ludwige9c57d32019-02-13 13:39:39 -0500361 GrPerspQuad::MakeFromRect(rect, viewMatrix), dstQuadType,
362 GrPerspQuad::MakeFromRect(rect, localMatrix), localQuadType);
Michael Ludwig69858532018-11-28 15:34:34 -0500363}
364
Robert Phillipsb97da532019-02-12 15:24:12 -0500365std::unique_ptr<GrDrawOp> MakePerEdgeWithLocalRect(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500366 GrPaint&& paint,
367 GrAAType aaType,
368 GrQuadAAFlags edgeAA,
369 const SkMatrix& viewMatrix,
370 const SkRect& rect,
371 const SkRect& localRect,
372 const GrUserStencilSettings* stencilSettings) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500373 GrQuadType dstQuadType = GrQuadTypeForTransformedRect(viewMatrix);
Michael Ludwig69858532018-11-28 15:34:34 -0500374 return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings,
Michael Ludwige9c57d32019-02-13 13:39:39 -0500375 GrPerspQuad::MakeFromRect(rect, viewMatrix), dstQuadType,
376 GrPerspQuad(localRect), GrQuadType::kRect);
Michael Ludwig69858532018-11-28 15:34:34 -0500377}
378
Robert Phillipsb97da532019-02-12 15:24:12 -0500379std::unique_ptr<GrDrawOp> MakeSet(GrRecordingContext* context,
Michael Ludwig69858532018-11-28 15:34:34 -0500380 GrPaint&& paint,
381 GrAAType aaType,
382 const SkMatrix& viewMatrix,
383 const GrRenderTargetContext::QuadSetEntry quads[],
384 int cnt,
385 const GrUserStencilSettings* stencilSettings) {
386 // First make a draw op for the first quad in the set
387 SkASSERT(cnt > 0);
388 GrQuadType deviceQuadType = GrQuadTypeForTransformedRect(viewMatrix);
389
390 paint.setColor4f(quads[0].fColor);
391 std::unique_ptr<GrDrawOp> op = FillRectOp::Make(context, std::move(paint), aaType,
Michael Ludwige9c57d32019-02-13 13:39:39 -0500392 quads[0].fAAFlags, stencilSettings,
393 GrPerspQuad::MakeFromRect(quads[0].fRect, viewMatrix), deviceQuadType,
394 GrPerspQuad::MakeFromRect(quads[0].fRect, quads[0].fLocalMatrix),
Michael Ludwig69858532018-11-28 15:34:34 -0500395 GrQuadTypeForTransformedRect(quads[0].fLocalMatrix));
396 auto* fillRects = op->cast<FillRectOp>();
397
398 // Accumulate remaining quads similar to onCombineIfPossible() without creating an op
399 for (int i = 1; i < cnt; ++i) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500400 GrPerspQuad deviceQuad = GrPerspQuad::MakeFromRect(quads[i].fRect, viewMatrix);
Michael Ludwig69858532018-11-28 15:34:34 -0500401
402 GrAAType resolvedAA;
403 GrQuadAAFlags resolvedEdgeFlags;
404 GrResolveAATypeForQuad(aaType, quads[i].fAAFlags, deviceQuad, deviceQuadType,
405 &resolvedAA, &resolvedEdgeFlags);
406
Michael Ludwige9c57d32019-02-13 13:39:39 -0500407 fillRects->addQuad(deviceQuad,
408 GrPerspQuad::MakeFromRect(quads[i].fRect, quads[i].fLocalMatrix),
Michael Ludwigc96fc372019-01-08 15:46:15 -0500409 GrQuadTypeForTransformedRect(quads[i].fLocalMatrix), quads[i].fColor,
410 resolvedEdgeFlags,resolvedAA);
Michael Ludwig69858532018-11-28 15:34:34 -0500411 }
412
413 return op;
414}
415
Robert Phillipsb97da532019-02-12 15:24:12 -0500416std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500417 GrPaint&& paint,
418 GrAAType aaType,
419 const SkMatrix& viewMatrix,
420 const SkRect& rect,
421 const GrUserStencilSettings* stencil) {
422 return MakePerEdge(context, std::move(paint), aaType,
423 aaType == GrAAType::kCoverage ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone,
424 viewMatrix, rect, stencil);
425}
426
Robert Phillipsb97da532019-02-12 15:24:12 -0500427std::unique_ptr<GrDrawOp> MakeWithLocalMatrix(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500428 GrPaint&& paint,
429 GrAAType aaType,
430 const SkMatrix& viewMatrix,
431 const SkMatrix& localMatrix,
432 const SkRect& rect,
433 const GrUserStencilSettings* stencil) {
434 return MakePerEdgeWithLocalMatrix(context, std::move(paint), aaType,
435 aaType == GrAAType::kCoverage ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone,
436 viewMatrix, localMatrix, rect, stencil);
437}
438
Robert Phillipsb97da532019-02-12 15:24:12 -0500439std::unique_ptr<GrDrawOp> MakeWithLocalRect(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500440 GrPaint&& paint,
441 GrAAType aaType,
442 const SkMatrix& viewMatrix,
443 const SkRect& rect,
444 const SkRect& localRect,
445 const GrUserStencilSettings* stencil) {
446 return MakePerEdgeWithLocalRect(context, std::move(paint), aaType,
447 aaType == GrAAType::kCoverage ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone,
448 viewMatrix, rect, localRect, stencil);
449}
450
Michael Ludwig69858532018-11-28 15:34:34 -0500451} // namespace GrFillRectOp
452
453#if GR_TEST_UTILS
454
455#include "GrDrawOpTest.h"
456#include "SkGr.h"
457
458GR_DRAW_OP_TEST_DEFINE(FillRectOp) {
459 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
460 SkRect rect = GrTest::TestRect(random);
461
462 GrAAType aaType = GrAAType::kNone;
463 if (random->nextBool()) {
464 aaType = (fsaaType == GrFSAAType::kUnifiedMSAA) ? GrAAType::kMSAA : GrAAType::kCoverage;
465 }
466 const GrUserStencilSettings* stencil = random->nextBool() ? nullptr
467 : GrGetRandomStencil(random, context);
468
469 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
470 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
471 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
472 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
473 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
474
475 if (random->nextBool()) {
476 if (random->nextBool()) {
477 if (random->nextBool()) {
478 // Local matrix with a set op
479 uint32_t extraQuadCt = random->nextRangeU(1, 4);
480 SkTArray<GrRenderTargetContext::QuadSetEntry> quads(extraQuadCt + 1);
481 quads.push_back(
482 {rect, SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU())),
483 GrTest::TestMatrixInvertible(random), aaFlags});
484 for (uint32_t i = 0; i < extraQuadCt; ++i) {
485 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
486 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
487 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
488 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
489 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
490
491 quads.push_back(
492 {GrTest::TestRect(random),
493 SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU())),
494 GrTest::TestMatrixInvertible(random), aaFlags});
495 }
496
497 return GrFillRectOp::MakeSet(context, std::move(paint), aaType, viewMatrix,
498 quads.begin(), quads.count(), stencil);
499 } else {
500 // Single local matrix
501 SkMatrix localMatrix = GrTest::TestMatrixInvertible(random);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500502 return GrFillRectOp::MakePerEdgeWithLocalMatrix(context, std::move(paint), aaType,
503 aaFlags, viewMatrix, localMatrix,
504 rect, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500505 }
506 } else {
507 // Pass local rect directly
508 SkRect localRect = GrTest::TestRect(random);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500509 return GrFillRectOp::MakePerEdgeWithLocalRect(context, std::move(paint), aaType,
510 aaFlags, viewMatrix, rect, localRect,
511 stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500512 }
513 } else {
514 // The simplest constructor
Michael Ludwig72ab3462018-12-10 12:43:36 -0500515 return GrFillRectOp::MakePerEdge(context, std::move(paint), aaType, aaFlags, viewMatrix,
516 rect, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500517 }
518}
519
520#endif