blob: ece0791f9a6aab9a4e3320e6b0675c5bf36a805a [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ops/GrFillRectOp.h"
Michael Ludwig69858532018-11-28 15:34:34 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkMatrix.h"
11#include "include/core/SkRect.h"
12#include "src/gpu/GrCaps.h"
13#include "src/gpu/GrGeometryProcessor.h"
14#include "src/gpu/GrPaint.h"
15#include "src/gpu/GrQuad.h"
16#include "src/gpu/SkGr.h"
17#include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h"
18#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
19#include "src/gpu/glsl/GrGLSLVarying.h"
20#include "src/gpu/ops/GrMeshDrawOp.h"
21#include "src/gpu/ops/GrQuadPerEdgeAA.h"
22#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
Michael Ludwig69858532018-11-28 15:34:34 -050023
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 }
Brian Salomon41f9c3c2019-03-25 11:06:12 -0400165 // Most SkShaders' FPs multiply their calculated color by the paint color or alpha. We want
166 // to use ColorType::kNone to optimize out that multiply. However, if there are no color
167 // FPs then were really writing a special shader for white rectangles and not saving any
168 // multiples. So in that case use bytes to avoid the extra shader (and possibly work around
169 // an ANGLE issue: crbug.com/942565).
170 if (fColorType == ColorType::kNone && !result.hasColorFragmentProcessor()) {
171 fColorType = ColorType::kByte;
172 }
Michael Ludwig69858532018-11-28 15:34:34 -0500173
174 return result;
175 }
176
177 FixedFunctionFlags fixedFunctionFlags() const override {
178 // Since the AA type of the whole primitive is kept consistent with the per edge AA flags
179 // the helper's fixed function flags are appropriate.
180 return fHelper.fixedFunctionFlags();
181 }
182
183 DEFINE_OP_CLASS_ID
184
185private:
186 // For GrFillRectOp::MakeSet's use of addQuad
Robert Phillipsb97da532019-02-12 15:24:12 -0500187 friend std::unique_ptr<GrDrawOp> GrFillRectOp::MakeSet(
188 GrRecordingContext*,
189 GrPaint&&,
190 GrAAType, const SkMatrix& viewMatrix,
Michael Ludwig69858532018-11-28 15:34:34 -0500191 const GrRenderTargetContext::QuadSetEntry quads[], int quadCount,
Robert Phillipsb97da532019-02-12 15:24:12 -0500192 const GrUserStencilSettings*);
Michael Ludwig69858532018-11-28 15:34:34 -0500193
Michael Ludwigc96fc372019-01-08 15:46:15 -0500194 void onPrepareDraws(Target* target) override {
Michael Ludwig69858532018-11-28 15:34:34 -0500195 TRACE_EVENT0("skia", TRACE_FUNC);
196
197 using Domain = GrQuadPerEdgeAA::Domain;
198 static constexpr SkRect kEmptyDomain = SkRect::MakeEmpty();
199
Brian Salomon1d835422019-03-13 16:11:44 -0400200 VertexSpec vertexSpec(fDeviceQuads.quadType(), fColorType, fLocalQuads.quadType(),
201 fHelper.usesLocalCoords(), Domain::kNo, fHelper.aaType(),
Brian Osman605c6d52019-03-15 12:10:35 -0400202 fHelper.compatibleWithCoverageAsAlpha());
Michael Ludwigdcd48212019-01-08 15:28:57 -0500203 // Make sure that if the op thought it was a solid color, the vertex spec does not use
204 // local coords.
205 SkASSERT(!fHelper.isTrivial() || !fHelper.usesLocalCoords());
Michael Ludwig69858532018-11-28 15:34:34 -0500206
Michael Ludwig467994d2018-12-03 14:58:31 +0000207 sk_sp<GrGeometryProcessor> gp = GrQuadPerEdgeAA::MakeProcessor(vertexSpec);
Michael Ludwig69858532018-11-28 15:34:34 -0500208 size_t vertexSize = gp->vertexStride();
209
Brian Salomon12d22642019-01-29 14:38:50 -0500210 sk_sp<const GrBuffer> vbuffer;
Michael Ludwig69858532018-11-28 15:34:34 -0500211 int vertexOffsetInBuffer = 0;
212
213 // Fill the allocated vertex data
Michael Ludwig93aeba02018-12-21 09:50:31 -0500214 void* vdata = target->makeVertexSpace(
Michael Ludwigc96fc372019-01-08 15:46:15 -0500215 vertexSize, this->quadCount() * vertexSpec.verticesPerQuad(),
Michael Ludwig93aeba02018-12-21 09:50:31 -0500216 &vbuffer, &vertexOffsetInBuffer);
Michael Ludwig69858532018-11-28 15:34:34 -0500217 if (!vdata) {
218 SkDebugf("Could not allocate vertices\n");
219 return;
220 }
221
222 // vertices pointer advances through vdata based on Tessellate's return value
223 void* vertices = vdata;
Michael Ludwigdcd48212019-01-08 15:28:57 -0500224 if (fHelper.isTrivial()) {
225 SkASSERT(fLocalQuads.count() == 0); // No local coords, so send an ignored dummy quad
Michael Ludwige9c57d32019-02-13 13:39:39 -0500226 static const GrPerspQuad kIgnoredLocal(SkRect::MakeEmpty());
227
Michael Ludwigdcd48212019-01-08 15:28:57 -0500228 for (int i = 0; i < this->quadCount(); ++i) {
229 const ColorAndAA& info = fDeviceQuads.metadata(i);
230 vertices = GrQuadPerEdgeAA::Tessellate(vertices, vertexSpec, fDeviceQuads[i],
231 info.fColor, kIgnoredLocal, kEmptyDomain, info.fAAFlags);
232 }
233 } else {
234 SkASSERT(fLocalQuads.count() == fDeviceQuads.count());
235 for (int i = 0; i < this->quadCount(); ++i) {
236 const ColorAndAA& info = fDeviceQuads.metadata(i);
237 vertices = GrQuadPerEdgeAA::Tessellate(vertices, vertexSpec, fDeviceQuads[i],
238 info.fColor, fLocalQuads[i], kEmptyDomain, info.fAAFlags);
239 }
Michael Ludwig69858532018-11-28 15:34:34 -0500240 }
241
242 // Configure the mesh for the vertex data
Michael Ludwig93aeba02018-12-21 09:50:31 -0500243 GrMesh* mesh = target->allocMeshes(1);
Michael Ludwigc96fc372019-01-08 15:46:15 -0500244 if (!GrQuadPerEdgeAA::ConfigureMeshIndices(target, mesh, vertexSpec, this->quadCount())) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500245 SkDebugf("Could not allocate indices\n");
246 return;
Michael Ludwig69858532018-11-28 15:34:34 -0500247 }
Brian Salomon12d22642019-01-29 14:38:50 -0500248 mesh->setVertexData(std::move(vbuffer), vertexOffsetInBuffer);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700249 target->recordDraw(std::move(gp), mesh);
250 }
Michael Ludwig69858532018-11-28 15:34:34 -0500251
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700252 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
253 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
254 }
Michael Ludwig69858532018-11-28 15:34:34 -0500255
256 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
257 TRACE_EVENT0("skia", TRACE_FUNC);
258 const auto* that = t->cast<FillRectOp>();
259
Michael Ludwig93aeba02018-12-21 09:50:31 -0500260 if ((fHelper.aaType() == GrAAType::kCoverage ||
261 that->fHelper.aaType() == GrAAType::kCoverage) &&
Michael Ludwigc96fc372019-01-08 15:46:15 -0500262 this->quadCount() + that->quadCount() > GrQuadPerEdgeAA::kNumAAQuadsInIndexBuffer) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500263 // This limit on batch size seems to help on Adreno devices
264 return CombineResult::kCannotCombine;
265 }
266
Michael Ludwigc96fc372019-01-08 15:46:15 -0500267 // Unlike most users of the draw op helper, this op can merge none-aa and coverage-aa draw
268 // ops together, so pass true as the last argument.
Michael Ludwig69858532018-11-28 15:34:34 -0500269 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds(), true)) {
270 return CombineResult::kCannotCombine;
271 }
272
Michael Ludwigdcd48212019-01-08 15:28:57 -0500273 // If the paints were compatible, the trivial/solid-color state should be the same
274 SkASSERT(fHelper.isTrivial() == that->fHelper.isTrivial());
Michael Ludwig69858532018-11-28 15:34:34 -0500275
Michael Ludwigdcd48212019-01-08 15:28:57 -0500276 // If the processor sets are compatible, the two ops are always compatible; it just needs to
277 // adjust the state of the op to be the more general quad and aa types of the two ops and
278 // then concatenate the per-quad data.
Brian Salomon1d835422019-03-13 16:11:44 -0400279 fColorType = SkTMax(fColorType, that->fColorType);
Michael Ludwig69858532018-11-28 15:34:34 -0500280
281 // The helper stores the aa type, but isCompatible(with true arg) allows the two ops' aa
282 // types to be none and coverage, in which case this op's aa type must be lifted to coverage
283 // so that quads with no aa edges can be batched with quads that have some/all edges aa'ed.
284 if (fHelper.aaType() == GrAAType::kNone && that->fHelper.aaType() == GrAAType::kCoverage) {
285 fHelper.setAAType(GrAAType::kCoverage);
286 }
287
Michael Ludwigc96fc372019-01-08 15:46:15 -0500288 fDeviceQuads.concat(that->fDeviceQuads);
Michael Ludwigdcd48212019-01-08 15:28:57 -0500289 if (!fHelper.isTrivial()) {
290 fLocalQuads.concat(that->fLocalQuads);
291 }
Michael Ludwig69858532018-11-28 15:34:34 -0500292 return CombineResult::kMerged;
293 }
294
295 // Similar to onCombineIfPossible, but adds a quad assuming its op would have been compatible.
296 // But since it's avoiding the op list management, it must update the op's bounds. This is only
297 // used with quad sets, which uses the same view matrix for each quad so this assumes that the
298 // device quad type of the new quad is the same as the op's.
Michael Ludwigc96fc372019-01-08 15:46:15 -0500299 void addQuad(const GrPerspQuad& deviceQuad, const GrPerspQuad& localQuad,
300 GrQuadType localQuadType, const SkPMColor4f& color, GrQuadAAFlags edgeAA,
301 GrAAType aaType) {
Michael Ludwig69858532018-11-28 15:34:34 -0500302 // The new quad's aa type should be the same as the first quad's or none, except when the
303 // first quad's aa type was already downgraded to none, in which case the stored type must
304 // be lifted to back to the requested type.
305 if (aaType != fHelper.aaType()) {
306 if (aaType != GrAAType::kNone) {
307 // Original quad was downgraded to non-aa, lift back up to this quad's required type
308 SkASSERT(fHelper.aaType() == GrAAType::kNone);
309 fHelper.setAAType(aaType);
310 }
311 // else the new quad could have been downgraded but the other quads can't be, so don't
312 // reset the op's accumulated aa type.
313 }
314
Michael Ludwig69858532018-11-28 15:34:34 -0500315 // Update the bounds and add the quad to this op's storage
316 SkRect newBounds = this->bounds();
Michael Ludwigc96fc372019-01-08 15:46:15 -0500317 newBounds.joinPossiblyEmptyRect(deviceQuad.bounds(fDeviceQuads.quadType()));
Michael Ludwig69858532018-11-28 15:34:34 -0500318 this->setBounds(newBounds, HasAABloat(fHelper.aaType() == GrAAType::kCoverage),
319 IsZeroArea::kNo);
Michael Ludwigc96fc372019-01-08 15:46:15 -0500320 fDeviceQuads.push_back(deviceQuad, fDeviceQuads.quadType(), { color, edgeAA });
Michael Ludwigdcd48212019-01-08 15:28:57 -0500321 if (!fHelper.isTrivial()) {
322 fLocalQuads.push_back(localQuad, localQuadType);
323 }
Michael Ludwig69858532018-11-28 15:34:34 -0500324 }
325
Michael Ludwigc96fc372019-01-08 15:46:15 -0500326 int quadCount() const {
327 // Sanity check that the parallel arrays for quad properties all have the same size
Michael Ludwigdcd48212019-01-08 15:28:57 -0500328 SkASSERT(fDeviceQuads.count() == fLocalQuads.count() ||
329 (fLocalQuads.count() == 0 && fHelper.isTrivial()));
Michael Ludwigc96fc372019-01-08 15:46:15 -0500330 return fDeviceQuads.count();
331 }
332
333 struct ColorAndAA {
334 SkPMColor4f fColor;
335 GrQuadAAFlags fAAFlags;
336 };
Michael Ludwig69858532018-11-28 15:34:34 -0500337
338 Helper fHelper;
Michael Ludwigc96fc372019-01-08 15:46:15 -0500339 GrTQuadList<ColorAndAA> fDeviceQuads;
Michael Ludwigdcd48212019-01-08 15:28:57 -0500340 // No metadata attached to the local quads; this list is empty when local coords are not needed.
Michael Ludwigc96fc372019-01-08 15:46:15 -0500341 GrQuadList fLocalQuads;
Michael Ludwig69858532018-11-28 15:34:34 -0500342
Brian Salomon1d835422019-03-13 16:11:44 -0400343 ColorType fColorType;
Michael Ludwig69858532018-11-28 15:34:34 -0500344
Michael Ludwig69858532018-11-28 15:34:34 -0500345 typedef GrMeshDrawOp INHERITED;
346};
347
348} // anonymous namespace
349
350namespace GrFillRectOp {
351
Robert Phillipsb97da532019-02-12 15:24:12 -0500352std::unique_ptr<GrDrawOp> MakePerEdge(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500353 GrPaint&& paint,
354 GrAAType aaType,
355 GrQuadAAFlags edgeAA,
356 const SkMatrix& viewMatrix,
357 const SkRect& rect,
358 const GrUserStencilSettings* stencilSettings) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500359 GrQuadType dstQuadType = GrQuadTypeForTransformedRect(viewMatrix);
Michael Ludwig69858532018-11-28 15:34:34 -0500360 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(rect), GrQuadType::kRect);
Michael Ludwig69858532018-11-28 15:34:34 -0500363}
364
Robert Phillipsb97da532019-02-12 15:24:12 -0500365std::unique_ptr<GrDrawOp> MakePerEdgeWithLocalMatrix(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500366 GrPaint&& paint,
367 GrAAType aaType,
368 GrQuadAAFlags edgeAA,
369 const SkMatrix& viewMatrix,
370 const SkMatrix& localMatrix,
371 const SkRect& rect,
372 const GrUserStencilSettings* stencilSettings) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500373 GrQuadType dstQuadType = GrQuadTypeForTransformedRect(viewMatrix);
Michael Ludwig69858532018-11-28 15:34:34 -0500374 GrQuadType localQuadType = GrQuadTypeForTransformedRect(localMatrix);
375 return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings,
Michael Ludwige9c57d32019-02-13 13:39:39 -0500376 GrPerspQuad::MakeFromRect(rect, viewMatrix), dstQuadType,
377 GrPerspQuad::MakeFromRect(rect, localMatrix), localQuadType);
Michael Ludwig69858532018-11-28 15:34:34 -0500378}
379
Robert Phillipsb97da532019-02-12 15:24:12 -0500380std::unique_ptr<GrDrawOp> MakePerEdgeWithLocalRect(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500381 GrPaint&& paint,
382 GrAAType aaType,
383 GrQuadAAFlags edgeAA,
384 const SkMatrix& viewMatrix,
385 const SkRect& rect,
386 const SkRect& localRect,
387 const GrUserStencilSettings* stencilSettings) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500388 GrQuadType dstQuadType = GrQuadTypeForTransformedRect(viewMatrix);
Michael Ludwig69858532018-11-28 15:34:34 -0500389 return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings,
Michael Ludwige9c57d32019-02-13 13:39:39 -0500390 GrPerspQuad::MakeFromRect(rect, viewMatrix), dstQuadType,
391 GrPerspQuad(localRect), GrQuadType::kRect);
Michael Ludwig69858532018-11-28 15:34:34 -0500392}
393
Michael Ludwig009b92e2019-02-15 16:03:53 -0500394std::unique_ptr<GrDrawOp> MakePerEdgeQuad(GrRecordingContext* context,
395 GrPaint&& paint,
396 GrAAType aaType,
397 GrQuadAAFlags edgeAA,
398 const SkMatrix& viewMatrix,
399 const SkPoint quad[4],
400 const SkPoint localQuad[4],
401 const GrUserStencilSettings* stencilSettings) {
Michael Ludwig97b94422019-04-09 10:42:39 -0400402 GrQuadType deviceType = GrQuadTypeForPoints(quad, viewMatrix);
403 GrQuadType localType = GrQuadTypeForPoints(localQuad ? localQuad : quad, SkMatrix::I());
Michael Ludwig009b92e2019-02-15 16:03:53 -0500404 return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings,
405 GrPerspQuad::MakeFromSkQuad(quad, viewMatrix), deviceType,
406 GrPerspQuad::MakeFromSkQuad(localQuad ? localQuad : quad,
Michael Ludwig97b94422019-04-09 10:42:39 -0400407 SkMatrix::I()), localType);
Michael Ludwig009b92e2019-02-15 16:03:53 -0500408}
409
Robert Phillipsb97da532019-02-12 15:24:12 -0500410std::unique_ptr<GrDrawOp> MakeSet(GrRecordingContext* context,
Michael Ludwig69858532018-11-28 15:34:34 -0500411 GrPaint&& paint,
412 GrAAType aaType,
413 const SkMatrix& viewMatrix,
414 const GrRenderTargetContext::QuadSetEntry quads[],
415 int cnt,
416 const GrUserStencilSettings* stencilSettings) {
417 // First make a draw op for the first quad in the set
418 SkASSERT(cnt > 0);
419 GrQuadType deviceQuadType = GrQuadTypeForTransformedRect(viewMatrix);
420
421 paint.setColor4f(quads[0].fColor);
422 std::unique_ptr<GrDrawOp> op = FillRectOp::Make(context, std::move(paint), aaType,
Michael Ludwige9c57d32019-02-13 13:39:39 -0500423 quads[0].fAAFlags, stencilSettings,
424 GrPerspQuad::MakeFromRect(quads[0].fRect, viewMatrix), deviceQuadType,
425 GrPerspQuad::MakeFromRect(quads[0].fRect, quads[0].fLocalMatrix),
Michael Ludwig69858532018-11-28 15:34:34 -0500426 GrQuadTypeForTransformedRect(quads[0].fLocalMatrix));
427 auto* fillRects = op->cast<FillRectOp>();
428
429 // Accumulate remaining quads similar to onCombineIfPossible() without creating an op
430 for (int i = 1; i < cnt; ++i) {
Michael Ludwige9c57d32019-02-13 13:39:39 -0500431 GrPerspQuad deviceQuad = GrPerspQuad::MakeFromRect(quads[i].fRect, viewMatrix);
Michael Ludwig69858532018-11-28 15:34:34 -0500432
433 GrAAType resolvedAA;
434 GrQuadAAFlags resolvedEdgeFlags;
435 GrResolveAATypeForQuad(aaType, quads[i].fAAFlags, deviceQuad, deviceQuadType,
436 &resolvedAA, &resolvedEdgeFlags);
437
Michael Ludwige9c57d32019-02-13 13:39:39 -0500438 fillRects->addQuad(deviceQuad,
439 GrPerspQuad::MakeFromRect(quads[i].fRect, quads[i].fLocalMatrix),
Michael Ludwigc96fc372019-01-08 15:46:15 -0500440 GrQuadTypeForTransformedRect(quads[i].fLocalMatrix), quads[i].fColor,
441 resolvedEdgeFlags,resolvedAA);
Michael Ludwig69858532018-11-28 15:34:34 -0500442 }
443
444 return op;
445}
446
Robert Phillipsb97da532019-02-12 15:24:12 -0500447std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500448 GrPaint&& paint,
449 GrAAType aaType,
450 const SkMatrix& viewMatrix,
451 const SkRect& rect,
452 const GrUserStencilSettings* stencil) {
453 return MakePerEdge(context, std::move(paint), aaType,
454 aaType == GrAAType::kCoverage ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone,
455 viewMatrix, rect, stencil);
456}
457
Robert Phillipsb97da532019-02-12 15:24:12 -0500458std::unique_ptr<GrDrawOp> MakeWithLocalMatrix(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500459 GrPaint&& paint,
460 GrAAType aaType,
461 const SkMatrix& viewMatrix,
462 const SkMatrix& localMatrix,
463 const SkRect& rect,
464 const GrUserStencilSettings* stencil) {
465 return MakePerEdgeWithLocalMatrix(context, std::move(paint), aaType,
466 aaType == GrAAType::kCoverage ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone,
467 viewMatrix, localMatrix, rect, stencil);
468}
469
Robert Phillipsb97da532019-02-12 15:24:12 -0500470std::unique_ptr<GrDrawOp> MakeWithLocalRect(GrRecordingContext* context,
Michael Ludwig72ab3462018-12-10 12:43:36 -0500471 GrPaint&& paint,
472 GrAAType aaType,
473 const SkMatrix& viewMatrix,
474 const SkRect& rect,
475 const SkRect& localRect,
476 const GrUserStencilSettings* stencil) {
477 return MakePerEdgeWithLocalRect(context, std::move(paint), aaType,
478 aaType == GrAAType::kCoverage ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone,
479 viewMatrix, rect, localRect, stencil);
480}
481
Michael Ludwig69858532018-11-28 15:34:34 -0500482} // namespace GrFillRectOp
483
484#if GR_TEST_UTILS
485
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500486#include "src/gpu/GrDrawOpTest.h"
487#include "src/gpu/SkGr.h"
Michael Ludwig69858532018-11-28 15:34:34 -0500488
489GR_DRAW_OP_TEST_DEFINE(FillRectOp) {
490 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
491 SkRect rect = GrTest::TestRect(random);
492
493 GrAAType aaType = GrAAType::kNone;
494 if (random->nextBool()) {
495 aaType = (fsaaType == GrFSAAType::kUnifiedMSAA) ? GrAAType::kMSAA : GrAAType::kCoverage;
496 }
497 const GrUserStencilSettings* stencil = random->nextBool() ? nullptr
498 : GrGetRandomStencil(random, context);
499
500 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
501 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
502 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
503 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
504 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
505
506 if (random->nextBool()) {
507 if (random->nextBool()) {
508 if (random->nextBool()) {
509 // Local matrix with a set op
510 uint32_t extraQuadCt = random->nextRangeU(1, 4);
511 SkTArray<GrRenderTargetContext::QuadSetEntry> quads(extraQuadCt + 1);
512 quads.push_back(
513 {rect, SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU())),
514 GrTest::TestMatrixInvertible(random), aaFlags});
515 for (uint32_t i = 0; i < extraQuadCt; ++i) {
516 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
517 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
518 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
519 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
520 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
521
522 quads.push_back(
523 {GrTest::TestRect(random),
524 SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU())),
525 GrTest::TestMatrixInvertible(random), aaFlags});
526 }
527
528 return GrFillRectOp::MakeSet(context, std::move(paint), aaType, viewMatrix,
529 quads.begin(), quads.count(), stencil);
530 } else {
531 // Single local matrix
532 SkMatrix localMatrix = GrTest::TestMatrixInvertible(random);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500533 return GrFillRectOp::MakePerEdgeWithLocalMatrix(context, std::move(paint), aaType,
534 aaFlags, viewMatrix, localMatrix,
535 rect, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500536 }
537 } else {
538 // Pass local rect directly
539 SkRect localRect = GrTest::TestRect(random);
Michael Ludwig72ab3462018-12-10 12:43:36 -0500540 return GrFillRectOp::MakePerEdgeWithLocalRect(context, std::move(paint), aaType,
541 aaFlags, viewMatrix, rect, localRect,
542 stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500543 }
544 } else {
545 // The simplest constructor
Michael Ludwig72ab3462018-12-10 12:43:36 -0500546 return GrFillRectOp::MakePerEdge(context, std::move(paint), aaType, aaFlags, viewMatrix,
547 rect, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500548 }
549}
550
551#endif