blob: 9935d51c8411ed9bd24eba4522464a7be807319a [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/SkGr.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040016#include "src/gpu/geometry/GrQuad.h"
Michael Ludwigd17e05a2019-06-04 09:10:34 -040017#include "src/gpu/geometry/GrQuadList.h"
Michael Ludwig0f809022019-06-04 09:14:37 -040018#include "src/gpu/geometry/GrQuadUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h"
20#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
21#include "src/gpu/glsl/GrGLSLVarying.h"
22#include "src/gpu/ops/GrMeshDrawOp.h"
23#include "src/gpu/ops/GrQuadPerEdgeAA.h"
24#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
Michael Ludwig69858532018-11-28 15:34:34 -050025
26namespace {
27
28using VertexSpec = GrQuadPerEdgeAA::VertexSpec;
29using ColorType = GrQuadPerEdgeAA::ColorType;
30
Michael Ludwigc96fc372019-01-08 15:46:15 -050031#ifdef SK_DEBUG
Michael Ludwigde4c58c2019-06-04 09:12:59 -040032static SkString dump_quad_info(int index, const GrQuad& deviceQuad,
33 const GrQuad& localQuad, const SkPMColor4f& color,
Michael Ludwigc96fc372019-01-08 15:46:15 -050034 GrQuadAAFlags aaFlags) {
35 SkString str;
36 str.appendf("%d: Color: [%.2f, %.2f, %.2f, %.2f], Edge AA: l%u_t%u_r%u_b%u, \n"
37 " device quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), "
38 "(%.2f, %.2f, %.2f)],\n"
39 " local quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), "
40 "(%.2f, %.2f, %.2f)]\n",
41 index, color.fR, color.fG, color.fB, color.fA,
42 (uint32_t) (aaFlags & GrQuadAAFlags::kLeft),
43 (uint32_t) (aaFlags & GrQuadAAFlags::kTop),
44 (uint32_t) (aaFlags & GrQuadAAFlags::kRight),
45 (uint32_t) (aaFlags & GrQuadAAFlags::kBottom),
46 deviceQuad.x(0), deviceQuad.y(0), deviceQuad.w(0),
47 deviceQuad.x(1), deviceQuad.y(1), deviceQuad.w(1),
48 deviceQuad.x(2), deviceQuad.y(2), deviceQuad.w(2),
49 deviceQuad.x(3), deviceQuad.y(3), deviceQuad.w(3),
50 localQuad.x(0), localQuad.y(0), localQuad.w(0),
51 localQuad.x(1), localQuad.y(1), localQuad.w(1),
52 localQuad.x(2), localQuad.y(2), localQuad.w(2),
53 localQuad.x(3), localQuad.y(3), localQuad.w(3));
54 return str;
55}
56#endif
Michael Ludwig69858532018-11-28 15:34:34 -050057
Michael Ludwig69858532018-11-28 15:34:34 -050058class FillRectOp final : public GrMeshDrawOp {
59private:
60 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
61
62public:
Robert Phillipsb97da532019-02-12 15:24:12 -050063 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwig69858532018-11-28 15:34:34 -050064 GrPaint&& paint,
65 GrAAType aaType,
66 GrQuadAAFlags edgeAA,
67 const GrUserStencilSettings* stencilSettings,
Michael Ludwigde4c58c2019-06-04 09:12:59 -040068 const GrQuad& deviceQuad,
69 const GrQuad& localQuad) {
Michael Ludwig69858532018-11-28 15:34:34 -050070 // Clean up deviations between aaType and edgeAA
Michael Ludwig0f809022019-06-04 09:14:37 -040071 GrQuadUtils::ResolveAAType(aaType, edgeAA, deviceQuad, &aaType, &edgeAA);
Michael Ludwigdcd48212019-01-08 15:28:57 -050072 return Helper::FactoryHelper<FillRectOp>(context, std::move(paint), aaType, edgeAA,
Michael Ludwig41f395d2019-05-23 13:59:45 -040073 stencilSettings, deviceQuad, localQuad);
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 Ludwigde4c58c2019-06-04 09:12:59 -040080 const GrQuad& deviceQuad, const GrQuad& localQuad)
Michael Ludwig69858532018-11-28 15:34:34 -050081 : INHERITED(ClassID())
Brian Osman8fa7ab42019-03-18 10:22:42 -040082 , fHelper(args, aaType, stencil) {
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 Ludwig41f395d2019-05-23 13:59:45 -040085 fDeviceQuads.push_back(deviceQuad, { 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.
Michael Ludwig41f395d2019-05-23 13:59:45 -040091 fLocalQuads.push_back(localQuad);
Michael Ludwigdcd48212019-01-08 15:28:57 -050092 }
Michael Ludwig41f395d2019-05-23 13:59:45 -040093 this->setBounds(deviceQuad.bounds(), HasAABloat(aaType == GrAAType::kCoverage),
94 IsZeroArea::kNo);
Michael Ludwig69858532018-11-28 15:34:34 -050095 }
96
97 const char* name() const override { return "FillRectOp"; }
98
Chris Dalton1706cbf2019-05-21 19:35:29 -060099 void visitProxies(const VisitProxyFunc& func) const override {
Michael Ludwig69858532018-11-28 15:34:34 -0500100 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 Ludwigde4c58c2019-06-04 09:12:59 -0400110 GrQuad device, local;
Michael Ludwigc96fc372019-01-08 15:46:15 -0500111 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
Brian Osman5ced0bf2019-03-15 10:15:29 -0400124 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
125 GrFSAAType fsaaType, GrClampType clampType) override {
Michael Ludwig69858532018-11-28 15:34:34 -0500126 // Initialize aggregate color analysis with the first quad's color (which always exists)
Michael Ludwigc96fc372019-01-08 15:46:15 -0500127 SkASSERT(this->quadCount() > 0);
128 GrProcessorAnalysisColor quadColors(fDeviceQuads.metadata(0).fColor);
Michael Ludwig69858532018-11-28 15:34:34 -0500129 // Then combine the colors of any additional quads (e.g. from MakeSet)
Michael Ludwigc96fc372019-01-08 15:46:15 -0500130 for (int i = 1; i < this->quadCount(); ++i) {
131 quadColors = GrProcessorAnalysisColor::Combine(quadColors,
132 fDeviceQuads.metadata(i).fColor);
Michael Ludwigca91e1f2018-12-10 10:44:44 -0500133 if (quadColors.isUnknown()) {
134 // No point in accumulating additional starting colors, combining cannot make it
135 // less unknown.
136 break;
137 }
Michael Ludwig69858532018-11-28 15:34:34 -0500138 }
Michael Ludwig72ab3462018-12-10 12:43:36 -0500139
140 // If the AA type is coverage, it will be a single value per pixel; if it's not coverage AA
141 // then the coverage is always 1.0, so specify kNone for more optimal blending.
142 GrProcessorAnalysisCoverage coverage = fHelper.aaType() == GrAAType::kCoverage ?
143 GrProcessorAnalysisCoverage::kSingleChannel :
144 GrProcessorAnalysisCoverage::kNone;
Brian Osman5ced0bf2019-03-15 10:15:29 -0400145 auto result = fHelper.finalizeProcessors(
146 caps, clip, fsaaType, clampType, coverage, &quadColors);
Michael Ludwig69858532018-11-28 15:34:34 -0500147 // If there is a constant color after analysis, that means all of the quads should be set
148 // to the same color (even if they started out with different colors).
149 SkPMColor4f colorOverride;
150 if (quadColors.isConstant(&colorOverride)) {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400151 fColorType = GrQuadPerEdgeAA::MinColorType(colorOverride, clampType, caps);
Michael Ludwigc96fc372019-01-08 15:46:15 -0500152 for (int i = 0; i < this->quadCount(); ++i) {
153 fDeviceQuads.metadata(i).fColor = colorOverride;
Michael Ludwig69858532018-11-28 15:34:34 -0500154 }
Brian Osman8fa7ab42019-03-18 10:22:42 -0400155 } else {
156 // Otherwise compute the color type needed as the max over all quads.
157 fColorType = ColorType::kNone;
158 for (int i = 0; i < this->quadCount(); ++i) {
159 SkPMColor4f* color = &fDeviceQuads.metadata(i).fColor;
160 fColorType = SkTMax(fColorType,
161 GrQuadPerEdgeAA::MinColorType(*color, clampType, caps));
162 }
Michael Ludwig69858532018-11-28 15:34:34 -0500163 }
Brian Salomon41f9c3c2019-03-25 11:06:12 -0400164 // Most SkShaders' FPs multiply their calculated color by the paint color or alpha. We want
165 // to use ColorType::kNone to optimize out that multiply. However, if there are no color
166 // FPs then were really writing a special shader for white rectangles and not saving any
167 // multiples. So in that case use bytes to avoid the extra shader (and possibly work around
168 // an ANGLE issue: crbug.com/942565).
169 if (fColorType == ColorType::kNone && !result.hasColorFragmentProcessor()) {
170 fColorType = ColorType::kByte;
171 }
Michael Ludwig69858532018-11-28 15:34:34 -0500172
173 return result;
174 }
175
176 FixedFunctionFlags fixedFunctionFlags() const override {
177 // Since the AA type of the whole primitive is kept consistent with the per edge AA flags
178 // the helper's fixed function flags are appropriate.
179 return fHelper.fixedFunctionFlags();
180 }
181
182 DEFINE_OP_CLASS_ID
183
184private:
185 // For GrFillRectOp::MakeSet's use of addQuad
Robert Phillipsb97da532019-02-12 15:24:12 -0500186 friend std::unique_ptr<GrDrawOp> GrFillRectOp::MakeSet(
187 GrRecordingContext*,
188 GrPaint&&,
189 GrAAType, const SkMatrix& viewMatrix,
Michael Ludwig69858532018-11-28 15:34:34 -0500190 const GrRenderTargetContext::QuadSetEntry quads[], int quadCount,
Robert Phillipsb97da532019-02-12 15:24:12 -0500191 const GrUserStencilSettings*);
Michael Ludwig69858532018-11-28 15:34:34 -0500192
Michael Ludwigc96fc372019-01-08 15:46:15 -0500193 void onPrepareDraws(Target* target) override {
Michael Ludwig69858532018-11-28 15:34:34 -0500194 TRACE_EVENT0("skia", TRACE_FUNC);
195
196 using Domain = GrQuadPerEdgeAA::Domain;
197 static constexpr SkRect kEmptyDomain = SkRect::MakeEmpty();
198
Brian Salomon1d835422019-03-13 16:11:44 -0400199 VertexSpec vertexSpec(fDeviceQuads.quadType(), fColorType, fLocalQuads.quadType(),
200 fHelper.usesLocalCoords(), Domain::kNo, fHelper.aaType(),
Brian Osman605c6d52019-03-15 12:10:35 -0400201 fHelper.compatibleWithCoverageAsAlpha());
Michael Ludwigdcd48212019-01-08 15:28:57 -0500202 // Make sure that if the op thought it was a solid color, the vertex spec does not use
203 // local coords.
204 SkASSERT(!fHelper.isTrivial() || !fHelper.usesLocalCoords());
Michael Ludwig69858532018-11-28 15:34:34 -0500205
Michael Ludwig467994d2018-12-03 14:58:31 +0000206 sk_sp<GrGeometryProcessor> gp = GrQuadPerEdgeAA::MakeProcessor(vertexSpec);
Michael Ludwig69858532018-11-28 15:34:34 -0500207 size_t vertexSize = gp->vertexStride();
208
Brian Salomon12d22642019-01-29 14:38:50 -0500209 sk_sp<const GrBuffer> vbuffer;
Michael Ludwig69858532018-11-28 15:34:34 -0500210 int vertexOffsetInBuffer = 0;
211
212 // Fill the allocated vertex data
Michael Ludwig93aeba02018-12-21 09:50:31 -0500213 void* vdata = target->makeVertexSpace(
Michael Ludwigc96fc372019-01-08 15:46:15 -0500214 vertexSize, this->quadCount() * vertexSpec.verticesPerQuad(),
Michael Ludwig93aeba02018-12-21 09:50:31 -0500215 &vbuffer, &vertexOffsetInBuffer);
Michael Ludwig69858532018-11-28 15:34:34 -0500216 if (!vdata) {
217 SkDebugf("Could not allocate vertices\n");
218 return;
219 }
220
221 // vertices pointer advances through vdata based on Tessellate's return value
222 void* vertices = vdata;
Michael Ludwigdcd48212019-01-08 15:28:57 -0500223 if (fHelper.isTrivial()) {
224 SkASSERT(fLocalQuads.count() == 0); // No local coords, so send an ignored dummy quad
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400225 static const GrQuad kIgnoredLocal(SkRect::MakeEmpty());
Michael Ludwige9c57d32019-02-13 13:39:39 -0500226
Michael Ludwigdcd48212019-01-08 15:28:57 -0500227 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, kIgnoredLocal, kEmptyDomain, info.fAAFlags);
231 }
232 } else {
233 SkASSERT(fLocalQuads.count() == fDeviceQuads.count());
234 for (int i = 0; i < this->quadCount(); ++i) {
235 const ColorAndAA& info = fDeviceQuads.metadata(i);
236 vertices = GrQuadPerEdgeAA::Tessellate(vertices, vertexSpec, fDeviceQuads[i],
237 info.fColor, fLocalQuads[i], kEmptyDomain, info.fAAFlags);
238 }
Michael Ludwig69858532018-11-28 15:34:34 -0500239 }
240
241 // Configure the mesh for the vertex data
Michael Ludwig93aeba02018-12-21 09:50:31 -0500242 GrMesh* mesh = target->allocMeshes(1);
Michael Ludwigc96fc372019-01-08 15:46:15 -0500243 if (!GrQuadPerEdgeAA::ConfigureMeshIndices(target, mesh, vertexSpec, this->quadCount())) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500244 SkDebugf("Could not allocate indices\n");
245 return;
Michael Ludwig69858532018-11-28 15:34:34 -0500246 }
Brian Salomon12d22642019-01-29 14:38:50 -0500247 mesh->setVertexData(std::move(vbuffer), vertexOffsetInBuffer);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700248 target->recordDraw(std::move(gp), mesh);
249 }
Michael Ludwig69858532018-11-28 15:34:34 -0500250
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700251 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
252 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
253 }
Michael Ludwig69858532018-11-28 15:34:34 -0500254
255 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
256 TRACE_EVENT0("skia", TRACE_FUNC);
257 const auto* that = t->cast<FillRectOp>();
258
Michael Ludwig93aeba02018-12-21 09:50:31 -0500259 if ((fHelper.aaType() == GrAAType::kCoverage ||
260 that->fHelper.aaType() == GrAAType::kCoverage) &&
Michael Ludwigc96fc372019-01-08 15:46:15 -0500261 this->quadCount() + that->quadCount() > GrQuadPerEdgeAA::kNumAAQuadsInIndexBuffer) {
Michael Ludwig93aeba02018-12-21 09:50:31 -0500262 // This limit on batch size seems to help on Adreno devices
263 return CombineResult::kCannotCombine;
264 }
265
Michael Ludwigc96fc372019-01-08 15:46:15 -0500266 // Unlike most users of the draw op helper, this op can merge none-aa and coverage-aa draw
267 // ops together, so pass true as the last argument.
Michael Ludwig69858532018-11-28 15:34:34 -0500268 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds(), true)) {
269 return CombineResult::kCannotCombine;
270 }
271
Michael Ludwigdcd48212019-01-08 15:28:57 -0500272 // If the paints were compatible, the trivial/solid-color state should be the same
273 SkASSERT(fHelper.isTrivial() == that->fHelper.isTrivial());
Michael Ludwig69858532018-11-28 15:34:34 -0500274
Michael Ludwigdcd48212019-01-08 15:28:57 -0500275 // If the processor sets are compatible, the two ops are always compatible; it just needs to
276 // adjust the state of the op to be the more general quad and aa types of the two ops and
277 // then concatenate the per-quad data.
Brian Salomon1d835422019-03-13 16:11:44 -0400278 fColorType = SkTMax(fColorType, that->fColorType);
Michael Ludwig69858532018-11-28 15:34:34 -0500279
280 // The helper stores the aa type, but isCompatible(with true arg) allows the two ops' aa
281 // types to be none and coverage, in which case this op's aa type must be lifted to coverage
282 // so that quads with no aa edges can be batched with quads that have some/all edges aa'ed.
283 if (fHelper.aaType() == GrAAType::kNone && that->fHelper.aaType() == GrAAType::kCoverage) {
284 fHelper.setAAType(GrAAType::kCoverage);
285 }
286
Michael Ludwigc96fc372019-01-08 15:46:15 -0500287 fDeviceQuads.concat(that->fDeviceQuads);
Michael Ludwigdcd48212019-01-08 15:28:57 -0500288 if (!fHelper.isTrivial()) {
289 fLocalQuads.concat(that->fLocalQuads);
290 }
Michael Ludwig69858532018-11-28 15:34:34 -0500291 return CombineResult::kMerged;
292 }
293
294 // Similar to onCombineIfPossible, but adds a quad assuming its op would have been compatible.
295 // But since it's avoiding the op list management, it must update the op's bounds. This is only
296 // used with quad sets, which uses the same view matrix for each quad so this assumes that the
297 // device quad type of the new quad is the same as the op's.
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400298 void addQuad(const GrQuad& deviceQuad, const GrQuad& localQuad,
Michael Ludwig41f395d2019-05-23 13:59:45 -0400299 const SkPMColor4f& color, GrQuadAAFlags edgeAA, GrAAType aaType) {
Michael Ludwig69858532018-11-28 15:34:34 -0500300 // The new quad's aa type should be the same as the first quad's or none, except when the
301 // first quad's aa type was already downgraded to none, in which case the stored type must
302 // be lifted to back to the requested type.
303 if (aaType != fHelper.aaType()) {
304 if (aaType != GrAAType::kNone) {
305 // Original quad was downgraded to non-aa, lift back up to this quad's required type
306 SkASSERT(fHelper.aaType() == GrAAType::kNone);
307 fHelper.setAAType(aaType);
308 }
309 // else the new quad could have been downgraded but the other quads can't be, so don't
310 // reset the op's accumulated aa type.
311 }
312
Michael Ludwig69858532018-11-28 15:34:34 -0500313 // Update the bounds and add the quad to this op's storage
314 SkRect newBounds = this->bounds();
Michael Ludwig41f395d2019-05-23 13:59:45 -0400315 newBounds.joinPossiblyEmptyRect(deviceQuad.bounds());
Michael Ludwig69858532018-11-28 15:34:34 -0500316 this->setBounds(newBounds, HasAABloat(fHelper.aaType() == GrAAType::kCoverage),
317 IsZeroArea::kNo);
Michael Ludwig41f395d2019-05-23 13:59:45 -0400318 fDeviceQuads.push_back(deviceQuad, { color, edgeAA });
Michael Ludwigdcd48212019-01-08 15:28:57 -0500319 if (!fHelper.isTrivial()) {
Michael Ludwig41f395d2019-05-23 13:59:45 -0400320 fLocalQuads.push_back(localQuad);
Michael Ludwigdcd48212019-01-08 15:28:57 -0500321 }
Michael Ludwig69858532018-11-28 15:34:34 -0500322 }
323
Michael Ludwigc96fc372019-01-08 15:46:15 -0500324 int quadCount() const {
325 // Sanity check that the parallel arrays for quad properties all have the same size
Michael Ludwigdcd48212019-01-08 15:28:57 -0500326 SkASSERT(fDeviceQuads.count() == fLocalQuads.count() ||
327 (fLocalQuads.count() == 0 && fHelper.isTrivial()));
Michael Ludwigc96fc372019-01-08 15:46:15 -0500328 return fDeviceQuads.count();
329 }
330
331 struct ColorAndAA {
332 SkPMColor4f fColor;
333 GrQuadAAFlags fAAFlags;
334 };
Michael Ludwig69858532018-11-28 15:34:34 -0500335
336 Helper fHelper;
Michael Ludwigc96fc372019-01-08 15:46:15 -0500337 GrTQuadList<ColorAndAA> fDeviceQuads;
Michael Ludwigdcd48212019-01-08 15:28:57 -0500338 // No metadata attached to the local quads; this list is empty when local coords are not needed.
Michael Ludwigc96fc372019-01-08 15:46:15 -0500339 GrQuadList fLocalQuads;
Michael Ludwig69858532018-11-28 15:34:34 -0500340
Brian Salomon1d835422019-03-13 16:11:44 -0400341 ColorType fColorType;
Michael Ludwig69858532018-11-28 15:34:34 -0500342
Michael Ludwig69858532018-11-28 15:34:34 -0500343 typedef GrMeshDrawOp INHERITED;
344};
345
346} // anonymous namespace
347
348namespace GrFillRectOp {
349
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400350std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
351 GrPaint&& paint,
352 GrAAType aaType,
353 GrQuadAAFlags aaFlags,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400354 const GrQuad& deviceQuad,
355 const GrQuad& localQuad,
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400356 const GrUserStencilSettings* stencil) {
Michael Ludwigd9f917b2019-05-24 12:57:55 -0400357 return FillRectOp::Make(context, std::move(paint), aaType, aaFlags, stencil,
358 deviceQuad, localQuad);
359}
360
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400361std::unique_ptr<GrDrawOp> MakeNonAARect(GrRecordingContext* context,
362 GrPaint&& paint,
363 const SkMatrix& view,
364 const SkRect& rect,
365 const GrUserStencilSettings* stencil) {
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400366 return FillRectOp::Make(context, std::move(paint), GrAAType::kNone, GrQuadAAFlags::kNone,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400367 stencil, GrQuad::MakeFromRect(rect, view), GrQuad(rect));
Michael Ludwig009b92e2019-02-15 16:03:53 -0500368}
369
Robert Phillipsb97da532019-02-12 15:24:12 -0500370std::unique_ptr<GrDrawOp> MakeSet(GrRecordingContext* context,
Michael Ludwig69858532018-11-28 15:34:34 -0500371 GrPaint&& paint,
372 GrAAType aaType,
373 const SkMatrix& viewMatrix,
374 const GrRenderTargetContext::QuadSetEntry quads[],
375 int cnt,
376 const GrUserStencilSettings* stencilSettings) {
377 // First make a draw op for the first quad in the set
378 SkASSERT(cnt > 0);
Michael Ludwig69858532018-11-28 15:34:34 -0500379
380 paint.setColor4f(quads[0].fColor);
381 std::unique_ptr<GrDrawOp> op = FillRectOp::Make(context, std::move(paint), aaType,
Michael Ludwige9c57d32019-02-13 13:39:39 -0500382 quads[0].fAAFlags, stencilSettings,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400383 GrQuad::MakeFromRect(quads[0].fRect, viewMatrix),
384 GrQuad::MakeFromRect(quads[0].fRect, quads[0].fLocalMatrix));
Michael Ludwig69858532018-11-28 15:34:34 -0500385 auto* fillRects = op->cast<FillRectOp>();
386
387 // Accumulate remaining quads similar to onCombineIfPossible() without creating an op
388 for (int i = 1; i < cnt; ++i) {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400389 GrQuad deviceQuad = GrQuad::MakeFromRect(quads[i].fRect, viewMatrix);
Michael Ludwig69858532018-11-28 15:34:34 -0500390
391 GrAAType resolvedAA;
392 GrQuadAAFlags resolvedEdgeFlags;
Michael Ludwig0f809022019-06-04 09:14:37 -0400393 GrQuadUtils::ResolveAAType(aaType, quads[i].fAAFlags, deviceQuad,
394 &resolvedAA, &resolvedEdgeFlags);
Michael Ludwig69858532018-11-28 15:34:34 -0500395
Michael Ludwige9c57d32019-02-13 13:39:39 -0500396 fillRects->addQuad(deviceQuad,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400397 GrQuad::MakeFromRect(quads[i].fRect, quads[i].fLocalMatrix),
Michael Ludwig41f395d2019-05-23 13:59:45 -0400398 quads[i].fColor, resolvedEdgeFlags,resolvedAA);
Michael Ludwig69858532018-11-28 15:34:34 -0500399 }
400
401 return op;
402}
403
404} // namespace GrFillRectOp
405
406#if GR_TEST_UTILS
407
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500408#include "src/gpu/GrDrawOpTest.h"
409#include "src/gpu/SkGr.h"
Michael Ludwig69858532018-11-28 15:34:34 -0500410
411GR_DRAW_OP_TEST_DEFINE(FillRectOp) {
412 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
413 SkRect rect = GrTest::TestRect(random);
414
415 GrAAType aaType = GrAAType::kNone;
416 if (random->nextBool()) {
417 aaType = (fsaaType == GrFSAAType::kUnifiedMSAA) ? GrAAType::kMSAA : GrAAType::kCoverage;
418 }
419 const GrUserStencilSettings* stencil = random->nextBool() ? nullptr
420 : GrGetRandomStencil(random, context);
421
422 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
423 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
424 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
425 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
426 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
427
428 if (random->nextBool()) {
429 if (random->nextBool()) {
430 if (random->nextBool()) {
431 // Local matrix with a set op
432 uint32_t extraQuadCt = random->nextRangeU(1, 4);
433 SkTArray<GrRenderTargetContext::QuadSetEntry> quads(extraQuadCt + 1);
434 quads.push_back(
435 {rect, SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU())),
436 GrTest::TestMatrixInvertible(random), aaFlags});
437 for (uint32_t i = 0; i < extraQuadCt; ++i) {
438 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
439 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
440 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
441 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
442 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
443
444 quads.push_back(
445 {GrTest::TestRect(random),
446 SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU())),
447 GrTest::TestMatrixInvertible(random), aaFlags});
448 }
449
450 return GrFillRectOp::MakeSet(context, std::move(paint), aaType, viewMatrix,
451 quads.begin(), quads.count(), stencil);
452 } else {
453 // Single local matrix
454 SkMatrix localMatrix = GrTest::TestMatrixInvertible(random);
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400455 return GrFillRectOp::Make(context, std::move(paint), aaType, aaFlags,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400456 GrQuad::MakeFromRect(rect, viewMatrix),
457 GrQuad::MakeFromRect(rect, localMatrix), stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500458 }
459 } else {
460 // Pass local rect directly
461 SkRect localRect = GrTest::TestRect(random);
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400462 return GrFillRectOp::Make(context, std::move(paint), aaType, aaFlags,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400463 GrQuad::MakeFromRect(rect, viewMatrix),
464 GrQuad(localRect), stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500465 }
466 } else {
467 // The simplest constructor
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400468 return GrFillRectOp::Make(context, std::move(paint), aaType, aaFlags,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400469 GrQuad::MakeFromRect(rect, viewMatrix),
470 GrQuad(rect), stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500471 }
472}
473
474#endif