blob: f5f7c7c75ecb2ad3d8f7a5ca69bcdcb3c43c4468 [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
Chris Dalton6ce447a2019-06-23 18:07:38 -0600124 GrProcessorSet::Analysis finalize(
125 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
126 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(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600147 caps, clip, hasMixedSampledCoverage, 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 Ludwigde4c58c2019-06-04 09:12:59 -0400226 static const GrQuad kIgnoredLocal(SkRect::MakeEmpty());
Michael Ludwige9c57d32019-02-13 13:39:39 -0500227
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 Ludwigde4c58c2019-06-04 09:12:59 -0400299 void addQuad(const GrQuad& deviceQuad, const GrQuad& localQuad,
Michael Ludwig41f395d2019-05-23 13:59:45 -0400300 const SkPMColor4f& color, GrQuadAAFlags edgeAA, GrAAType aaType) {
Michael Ludwig69858532018-11-28 15:34:34 -0500301 // The new quad's aa type should be the same as the first quad's or none, except when the
302 // first quad's aa type was already downgraded to none, in which case the stored type must
303 // be lifted to back to the requested type.
304 if (aaType != fHelper.aaType()) {
305 if (aaType != GrAAType::kNone) {
306 // Original quad was downgraded to non-aa, lift back up to this quad's required type
307 SkASSERT(fHelper.aaType() == GrAAType::kNone);
308 fHelper.setAAType(aaType);
309 }
310 // else the new quad could have been downgraded but the other quads can't be, so don't
311 // reset the op's accumulated aa type.
312 }
313
Michael Ludwig69858532018-11-28 15:34:34 -0500314 // Update the bounds and add the quad to this op's storage
315 SkRect newBounds = this->bounds();
Michael Ludwig41f395d2019-05-23 13:59:45 -0400316 newBounds.joinPossiblyEmptyRect(deviceQuad.bounds());
Michael Ludwig69858532018-11-28 15:34:34 -0500317 this->setBounds(newBounds, HasAABloat(fHelper.aaType() == GrAAType::kCoverage),
318 IsZeroArea::kNo);
Michael Ludwig41f395d2019-05-23 13:59:45 -0400319 fDeviceQuads.push_back(deviceQuad, { color, edgeAA });
Michael Ludwigdcd48212019-01-08 15:28:57 -0500320 if (!fHelper.isTrivial()) {
Michael Ludwig41f395d2019-05-23 13:59:45 -0400321 fLocalQuads.push_back(localQuad);
Michael Ludwigdcd48212019-01-08 15:28:57 -0500322 }
Michael Ludwig69858532018-11-28 15:34:34 -0500323 }
324
Michael Ludwigc96fc372019-01-08 15:46:15 -0500325 int quadCount() const {
326 // Sanity check that the parallel arrays for quad properties all have the same size
Michael Ludwigdcd48212019-01-08 15:28:57 -0500327 SkASSERT(fDeviceQuads.count() == fLocalQuads.count() ||
328 (fLocalQuads.count() == 0 && fHelper.isTrivial()));
Michael Ludwigc96fc372019-01-08 15:46:15 -0500329 return fDeviceQuads.count();
330 }
331
332 struct ColorAndAA {
333 SkPMColor4f fColor;
334 GrQuadAAFlags fAAFlags;
335 };
Michael Ludwig69858532018-11-28 15:34:34 -0500336
337 Helper fHelper;
Michael Ludwigc96fc372019-01-08 15:46:15 -0500338 GrTQuadList<ColorAndAA> fDeviceQuads;
Michael Ludwigdcd48212019-01-08 15:28:57 -0500339 // No metadata attached to the local quads; this list is empty when local coords are not needed.
Michael Ludwigc96fc372019-01-08 15:46:15 -0500340 GrQuadList fLocalQuads;
Michael Ludwig69858532018-11-28 15:34:34 -0500341
Brian Salomon1d835422019-03-13 16:11:44 -0400342 ColorType fColorType;
Michael Ludwig69858532018-11-28 15:34:34 -0500343
Michael Ludwig69858532018-11-28 15:34:34 -0500344 typedef GrMeshDrawOp INHERITED;
345};
346
347} // anonymous namespace
348
349namespace GrFillRectOp {
350
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400351std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
352 GrPaint&& paint,
353 GrAAType aaType,
354 GrQuadAAFlags aaFlags,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400355 const GrQuad& deviceQuad,
356 const GrQuad& localQuad,
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400357 const GrUserStencilSettings* stencil) {
Michael Ludwigd9f917b2019-05-24 12:57:55 -0400358 return FillRectOp::Make(context, std::move(paint), aaType, aaFlags, stencil,
359 deviceQuad, localQuad);
360}
361
Michael Ludwigaa1b6b32019-05-29 14:43:13 -0400362std::unique_ptr<GrDrawOp> MakeNonAARect(GrRecordingContext* context,
363 GrPaint&& paint,
364 const SkMatrix& view,
365 const SkRect& rect,
366 const GrUserStencilSettings* stencil) {
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400367 return FillRectOp::Make(context, std::move(paint), GrAAType::kNone, GrQuadAAFlags::kNone,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400368 stencil, GrQuad::MakeFromRect(rect, view), GrQuad(rect));
Michael Ludwig009b92e2019-02-15 16:03:53 -0500369}
370
Robert Phillipsb97da532019-02-12 15:24:12 -0500371std::unique_ptr<GrDrawOp> MakeSet(GrRecordingContext* context,
Michael Ludwig69858532018-11-28 15:34:34 -0500372 GrPaint&& paint,
373 GrAAType aaType,
374 const SkMatrix& viewMatrix,
375 const GrRenderTargetContext::QuadSetEntry quads[],
376 int cnt,
377 const GrUserStencilSettings* stencilSettings) {
378 // First make a draw op for the first quad in the set
379 SkASSERT(cnt > 0);
Michael Ludwig69858532018-11-28 15:34:34 -0500380
381 paint.setColor4f(quads[0].fColor);
382 std::unique_ptr<GrDrawOp> op = FillRectOp::Make(context, std::move(paint), aaType,
Michael Ludwige9c57d32019-02-13 13:39:39 -0500383 quads[0].fAAFlags, stencilSettings,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400384 GrQuad::MakeFromRect(quads[0].fRect, viewMatrix),
385 GrQuad::MakeFromRect(quads[0].fRect, quads[0].fLocalMatrix));
Michael Ludwig69858532018-11-28 15:34:34 -0500386 auto* fillRects = op->cast<FillRectOp>();
387
388 // Accumulate remaining quads similar to onCombineIfPossible() without creating an op
389 for (int i = 1; i < cnt; ++i) {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400390 GrQuad deviceQuad = GrQuad::MakeFromRect(quads[i].fRect, viewMatrix);
Michael Ludwig69858532018-11-28 15:34:34 -0500391
392 GrAAType resolvedAA;
393 GrQuadAAFlags resolvedEdgeFlags;
Michael Ludwig0f809022019-06-04 09:14:37 -0400394 GrQuadUtils::ResolveAAType(aaType, quads[i].fAAFlags, deviceQuad,
395 &resolvedAA, &resolvedEdgeFlags);
Michael Ludwig69858532018-11-28 15:34:34 -0500396
Michael Ludwige9c57d32019-02-13 13:39:39 -0500397 fillRects->addQuad(deviceQuad,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400398 GrQuad::MakeFromRect(quads[i].fRect, quads[i].fLocalMatrix),
Michael Ludwig41f395d2019-05-23 13:59:45 -0400399 quads[i].fColor, resolvedEdgeFlags,resolvedAA);
Michael Ludwig69858532018-11-28 15:34:34 -0500400 }
401
402 return op;
403}
404
405} // namespace GrFillRectOp
406
407#if GR_TEST_UTILS
408
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500409#include "src/gpu/GrDrawOpTest.h"
410#include "src/gpu/SkGr.h"
Michael Ludwig69858532018-11-28 15:34:34 -0500411
412GR_DRAW_OP_TEST_DEFINE(FillRectOp) {
413 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
414 SkRect rect = GrTest::TestRect(random);
415
416 GrAAType aaType = GrAAType::kNone;
417 if (random->nextBool()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600418 aaType = (numSamples > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Michael Ludwig69858532018-11-28 15:34:34 -0500419 }
420 const GrUserStencilSettings* stencil = random->nextBool() ? nullptr
421 : GrGetRandomStencil(random, context);
422
423 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
424 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
425 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
426 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
427 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
428
429 if (random->nextBool()) {
430 if (random->nextBool()) {
431 if (random->nextBool()) {
432 // Local matrix with a set op
433 uint32_t extraQuadCt = random->nextRangeU(1, 4);
434 SkTArray<GrRenderTargetContext::QuadSetEntry> quads(extraQuadCt + 1);
435 quads.push_back(
436 {rect, SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU())),
437 GrTest::TestMatrixInvertible(random), aaFlags});
438 for (uint32_t i = 0; i < extraQuadCt; ++i) {
439 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
440 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
441 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
442 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
443 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
444
445 quads.push_back(
446 {GrTest::TestRect(random),
447 SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU())),
448 GrTest::TestMatrixInvertible(random), aaFlags});
449 }
450
451 return GrFillRectOp::MakeSet(context, std::move(paint), aaType, viewMatrix,
452 quads.begin(), quads.count(), stencil);
453 } else {
454 // Single local matrix
455 SkMatrix localMatrix = GrTest::TestMatrixInvertible(random);
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400456 return GrFillRectOp::Make(context, std::move(paint), aaType, aaFlags,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400457 GrQuad::MakeFromRect(rect, viewMatrix),
458 GrQuad::MakeFromRect(rect, localMatrix), stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500459 }
460 } else {
461 // Pass local rect directly
462 SkRect localRect = GrTest::TestRect(random);
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400463 return GrFillRectOp::Make(context, std::move(paint), aaType, aaFlags,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400464 GrQuad::MakeFromRect(rect, viewMatrix),
465 GrQuad(localRect), stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500466 }
467 } else {
468 // The simplest constructor
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400469 return GrFillRectOp::Make(context, std::move(paint), aaType, aaFlags,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400470 GrQuad::MakeFromRect(rect, viewMatrix),
471 GrQuad(rect), stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500472 }
473}
474
475#endif