blob: b0fa5f0c0733ad1e70781978e532a76d94a0abcf [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"
Robert Phillips50d7d6f2020-03-04 11:12:24 -050015#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/SkGr.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040017#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig425eb452019-06-27 10:13:27 -040018#include "src/gpu/geometry/GrQuadBuffer.h"
Michael Ludwig0f809022019-06-04 09:14:37 -040019#include "src/gpu/geometry/GrQuadUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h"
21#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
22#include "src/gpu/glsl/GrGLSLVarying.h"
23#include "src/gpu/ops/GrMeshDrawOp.h"
24#include "src/gpu/ops/GrQuadPerEdgeAA.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050025#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
Michael Ludwig69858532018-11-28 15:34:34 -050026
27namespace {
28
29using VertexSpec = GrQuadPerEdgeAA::VertexSpec;
30using ColorType = GrQuadPerEdgeAA::ColorType;
31
John Stiles8d9bf642020-08-12 15:07:45 -040032#if GR_TEST_UTILS
Michael Ludwig704d5402019-11-25 09:43:37 -050033static SkString dump_quad_info(int index, const GrQuad* deviceQuad,
34 const GrQuad* localQuad, const SkPMColor4f& color,
Michael Ludwigc96fc372019-01-08 15:46:15 -050035 GrQuadAAFlags aaFlags) {
Michael Ludwig704d5402019-11-25 09:43:37 -050036 GrQuad safeLocal = localQuad ? *localQuad : GrQuad();
Michael Ludwigc96fc372019-01-08 15:46:15 -050037 SkString str;
38 str.appendf("%d: Color: [%.2f, %.2f, %.2f, %.2f], Edge AA: l%u_t%u_r%u_b%u, \n"
39 " device quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), "
40 "(%.2f, %.2f, %.2f)],\n"
41 " local quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), "
42 "(%.2f, %.2f, %.2f)]\n",
43 index, color.fR, color.fG, color.fB, color.fA,
44 (uint32_t) (aaFlags & GrQuadAAFlags::kLeft),
45 (uint32_t) (aaFlags & GrQuadAAFlags::kTop),
46 (uint32_t) (aaFlags & GrQuadAAFlags::kRight),
47 (uint32_t) (aaFlags & GrQuadAAFlags::kBottom),
Michael Ludwig704d5402019-11-25 09:43:37 -050048 deviceQuad->x(0), deviceQuad->y(0), deviceQuad->w(0),
49 deviceQuad->x(1), deviceQuad->y(1), deviceQuad->w(1),
50 deviceQuad->x(2), deviceQuad->y(2), deviceQuad->w(2),
51 deviceQuad->x(3), deviceQuad->y(3), deviceQuad->w(3),
52 safeLocal.x(0), safeLocal.y(0), safeLocal.w(0),
53 safeLocal.x(1), safeLocal.y(1), safeLocal.w(1),
54 safeLocal.x(2), safeLocal.y(2), safeLocal.w(2),
55 safeLocal.x(3), safeLocal.y(3), safeLocal.w(3));
Michael Ludwigc96fc372019-01-08 15:46:15 -050056 return str;
57}
58#endif
Michael Ludwig69858532018-11-28 15:34:34 -050059
Michael Ludwig69858532018-11-28 15:34:34 -050060class FillRectOp final : public GrMeshDrawOp {
61private:
62 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
63
64public:
Herb Derbyc76d4092020-10-07 16:46:15 -040065 static GrOp::Owner Make(GrRecordingContext* context,
66 GrPaint&& paint,
67 GrAAType aaType,
68 DrawQuad* quad,
69 const GrUserStencilSettings* stencilSettings,
70 Helper::InputFlags inputFlags) {
Michael Ludwig69858532018-11-28 15:34:34 -050071 // Clean up deviations between aaType and edgeAA
Michael Ludwig6b45c5d2020-02-07 09:56:38 -050072 GrQuadUtils::ResolveAAType(aaType, quad->fEdgeFlags, quad->fDevice,
73 &aaType, &quad->fEdgeFlags);
74 return Helper::FactoryHelper<FillRectOp>(context, std::move(paint), aaType, quad,
Chris Daltonc3b67eb2020-02-10 21:09:58 -070075 stencilSettings, inputFlags);
Michael Ludwig69858532018-11-28 15:34:34 -050076 }
77
Michael Ludwigdcd48212019-01-08 15:28:57 -050078 // aaType is passed to Helper in the initializer list, so incongruities between aaType and
79 // edgeFlags must be resolved prior to calling this constructor.
Herb Derbyc76d4092020-10-07 16:46:15 -040080 FillRectOp(GrProcessorSet* processorSet, SkPMColor4f paintColor, GrAAType aaType,
Chris Daltonc3b67eb2020-02-10 21:09:58 -070081 DrawQuad* quad, const GrUserStencilSettings* stencil, Helper::InputFlags inputFlags)
Michael Ludwig69858532018-11-28 15:34:34 -050082 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -040083 , fHelper(processorSet, aaType, stencil, inputFlags)
Michael Ludwig425eb452019-06-27 10:13:27 -040084 , fQuads(1, !fHelper.isTrivial()) {
Michael Ludwig949ceb22020-02-07 10:14:45 -050085 // Set bounds before clipping so we don't have to worry about unioning the bounds of
86 // the two potential quads (GrQuad::bounds() is perspective-safe).
87 this->setBounds(quad->fDevice.bounds(), HasAABloat(aaType == GrAAType::kCoverage),
88 IsHairline::kNo);
89
90 DrawQuad extra;
Michael Ludwig465864c2020-02-10 09:30:04 -050091 // Only clip when there's anti-aliasing. When non-aa, the GPU clips just fine and there's
92 // no inset/outset math that requires w > 0.
93 int count = quad->fEdgeFlags != GrQuadAAFlags::kNone ? GrQuadUtils::ClipToW0(quad, &extra)
94 : 1;
Michael Ludwig949ceb22020-02-07 10:14:45 -050095 if (count == 0) {
96 // We can't discard the op at this point, but disable AA flags so it won't go through
97 // inset/outset processing
98 quad->fEdgeFlags = GrQuadAAFlags::kNone;
99 count = 1;
100 }
101
Michael Ludwig425eb452019-06-27 10:13:27 -0400102 // Conservatively keep track of the local coordinates; it may be that the paint doesn't
103 // need them after analysis is finished. If the paint is known to be solid up front they
104 // can be skipped entirely.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500105 fQuads.append(quad->fDevice, {paintColor, quad->fEdgeFlags},
106 fHelper.isTrivial() ? nullptr : &quad->fLocal);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500107 if (count > 1) {
108 fQuads.append(extra.fDevice, { paintColor, extra.fEdgeFlags },
109 fHelper.isTrivial() ? nullptr : &extra.fLocal);
110 }
Michael Ludwig69858532018-11-28 15:34:34 -0500111 }
112
113 const char* name() const override { return "FillRectOp"; }
114
Chris Dalton1706cbf2019-05-21 19:35:29 -0600115 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500116 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600117 fProgramInfo->visitFPProxies(func);
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500118 } else {
119 return fHelper.visitProxies(func);
120 }
Michael Ludwig69858532018-11-28 15:34:34 -0500121 }
122
Chris Dalton6ce447a2019-06-23 18:07:38 -0600123 GrProcessorSet::Analysis finalize(
124 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
125 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 Ludwig425eb452019-06-27 10:13:27 -0400127 auto iter = fQuads.metadata();
128 SkAssertResult(iter.next());
129 GrProcessorAnalysisColor quadColors(iter->fColor);
Michael Ludwig69858532018-11-28 15:34:34 -0500130 // Then combine the colors of any additional quads (e.g. from MakeSet)
Michael Ludwig425eb452019-06-27 10:13:27 -0400131 while(iter.next()) {
132 quadColors = GrProcessorAnalysisColor::Combine(quadColors, iter->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.
Robert Phillips360ec182020-03-26 13:29:50 -0400142 auto coverage = fHelper.aaType() == GrAAType::kCoverage
143 ? GrProcessorAnalysisCoverage::kSingleChannel
144 : GrProcessorAnalysisCoverage::kNone;
Brian Osman5ced0bf2019-03-15 10:15:29 -0400145 auto result = fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600146 caps, clip, hasMixedSampledCoverage, 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).
Michael Ludwig425eb452019-06-27 10:13:27 -0400149 iter = fQuads.metadata();
Michael Ludwig69858532018-11-28 15:34:34 -0500150 SkPMColor4f colorOverride;
151 if (quadColors.isConstant(&colorOverride)) {
Brian Osman2715bf52019-12-06 14:38:47 -0500152 fColorType = GrQuadPerEdgeAA::MinColorType(colorOverride);
Michael Ludwig425eb452019-06-27 10:13:27 -0400153 while(iter.next()) {
154 iter->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;
Michael Ludwig425eb452019-06-27 10:13:27 -0400159 while(iter.next()) {
Brian Osman788b9162020-02-07 10:36:46 -0500160 fColorType = std::max(fColorType, GrQuadPerEdgeAA::MinColorType(iter->fColor));
Brian Osman8fa7ab42019-03-18 10:22:42 -0400161 }
Michael Ludwig69858532018-11-28 15:34:34 -0500162 }
Brian Salomon41f9c3c2019-03-25 11:06:12 -0400163 // Most SkShaders' FPs multiply their calculated color by the paint color or alpha. We want
164 // to use ColorType::kNone to optimize out that multiply. However, if there are no color
165 // FPs then were really writing a special shader for white rectangles and not saving any
166 // multiples. So in that case use bytes to avoid the extra shader (and possibly work around
167 // an ANGLE issue: crbug.com/942565).
168 if (fColorType == ColorType::kNone && !result.hasColorFragmentProcessor()) {
169 fColorType = ColorType::kByte;
170 }
Michael Ludwig69858532018-11-28 15:34:34 -0500171
172 return result;
173 }
174
175 FixedFunctionFlags fixedFunctionFlags() const override {
176 // Since the AA type of the whole primitive is kept consistent with the per edge AA flags
177 // the helper's fixed function flags are appropriate.
178 return fHelper.fixedFunctionFlags();
179 }
180
181 DEFINE_OP_CLASS_ID
182
183private:
Robert Phillips438d9862019-11-14 12:46:05 -0500184 friend class ::GrFillRectOp; // for access to addQuad
185
186#if GR_TEST_UTILS
187 int numQuads() const final { return fQuads.count(); }
188#endif
Michael Ludwig69858532018-11-28 15:34:34 -0500189
Robert Phillips93209052019-12-03 15:42:35 -0500190 VertexSpec vertexSpec() const {
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400191 auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(fHelper.aaType(),
192 fQuads.count());
193
Robert Phillips93209052019-12-03 15:42:35 -0500194 return VertexSpec(fQuads.deviceQuadType(), fColorType, fQuads.localQuadType(),
Brian Salomon2432d062020-04-16 20:48:09 -0400195 fHelper.usesLocalCoords(), GrQuadPerEdgeAA::Subset::kNo,
Robert Phillips93209052019-12-03 15:42:35 -0500196 fHelper.aaType(),
197 fHelper.compatibleWithCoverageAsAlpha(), indexBufferOption);
198 }
199
Robert Phillips2669a7b2020-03-12 12:07:19 -0400200 GrProgramInfo* programInfo() override {
Robert Phillips2669a7b2020-03-12 12:07:19 -0400201 return fProgramInfo;
202 }
203
Robert Phillips4133dc42020-03-11 15:55:55 -0400204 void onCreateProgramInfo(const GrCaps* caps,
205 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500206 const GrSurfaceProxyView& writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400207 GrAppliedClip&& appliedClip,
Greg Danield358cbe2020-09-11 09:33:54 -0400208 const GrXferProcessor::DstProxyView& dstProxyView,
209 GrXferBarrierFlags renderPassXferBarriers) override {
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500210 const VertexSpec vertexSpec = this->vertexSpec();
211
212 GrGeometryProcessor* gp = GrQuadPerEdgeAA::MakeProcessor(arena, vertexSpec);
213 SkASSERT(gp->vertexStride() == vertexSpec.vertexSize());
214
Brian Salomon8afde5f2020-04-01 16:22:00 -0400215 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400216 std::move(appliedClip),
217 dstProxyView, gp,
Greg Danield358cbe2020-09-11 09:33:54 -0400218 vertexSpec.primitiveType(),
219 renderPassXferBarriers);
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500220 }
221
Robert Phillips473a8482020-10-20 10:44:15 -0400222 void onPrePrepareDraws(GrRecordingContext* rContext,
Adlai Hollere2296f72020-11-19 13:41:26 -0500223 const GrSurfaceProxyView& writeView,
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500224 GrAppliedClip* clip,
Greg Danield358cbe2020-09-11 09:33:54 -0400225 const GrXferProcessor::DstProxyView& dstProxyView,
226 GrXferBarrierFlags renderPassXferBarriers) override {
Robert Phillips93209052019-12-03 15:42:35 -0500227 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
228
229 SkASSERT(!fPrePreparedVertices);
230
Robert Phillips473a8482020-10-20 10:44:15 -0400231 INHERITED::onPrePrepareDraws(rContext, writeView, clip, dstProxyView,
232 renderPassXferBarriers);
Robert Phillips93209052019-12-03 15:42:35 -0500233
Robert Phillips473a8482020-10-20 10:44:15 -0400234 SkArenaAlloc* arena = rContext->priv().recordTimeAllocator();
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500235
Robert Phillips93209052019-12-03 15:42:35 -0500236 const VertexSpec vertexSpec = this->vertexSpec();
237
238 const int totalNumVertices = fQuads.count() * vertexSpec.verticesPerQuad();
239 const size_t totalVertexSizeInBytes = vertexSpec.vertexSize() * totalNumVertices;
240
241 fPrePreparedVertices = arena->makeArrayDefault<char>(totalVertexSizeInBytes);
242
243 this->tessellate(vertexSpec, fPrePreparedVertices);
244 }
245
246 void tessellate(const VertexSpec& vertexSpec, char* dst) const {
247 static constexpr SkRect kEmptyDomain = SkRect::MakeEmpty();
248
249 GrQuadPerEdgeAA::Tessellator tessellator(vertexSpec, dst);
250 auto iter = fQuads.iterator();
251 while (iter.next()) {
252 // All entries should have local coords, or no entries should have local coords,
253 // matching !helper.isTrivial() (which is more conservative than helper.usesLocalCoords)
254 SkASSERT(iter.isLocalValid() != fHelper.isTrivial());
255 auto info = iter.metadata();
256 tessellator.append(iter.deviceQuad(), iter.localQuad(),
257 info.fColor, kEmptyDomain, info.fAAFlags);
258 }
259 }
260
261 void onPrepareDraws(Target* target) override {
262 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
263
264 const VertexSpec vertexSpec = this->vertexSpec();
265
Michael Ludwigdcd48212019-01-08 15:28:57 -0500266 // Make sure that if the op thought it was a solid color, the vertex spec does not use
267 // local coords.
268 SkASSERT(!fHelper.isTrivial() || !fHelper.usesLocalCoords());
Michael Ludwig69858532018-11-28 15:34:34 -0500269
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400270 const int totalNumVertices = fQuads.count() * vertexSpec.verticesPerQuad();
271
Michael Ludwig69858532018-11-28 15:34:34 -0500272 // Fill the allocated vertex data
Robert Phillips93209052019-12-03 15:42:35 -0500273 void* vdata = target->makeVertexSpace(vertexSpec.vertexSize(), totalNumVertices,
Chris Daltondbb833b2020-03-17 12:15:46 -0600274 &fVertexBuffer, &fBaseVertex);
Michael Ludwig69858532018-11-28 15:34:34 -0500275 if (!vdata) {
276 SkDebugf("Could not allocate vertices\n");
277 return;
278 }
279
Robert Phillips93209052019-12-03 15:42:35 -0500280 if (fPrePreparedVertices) {
281 const size_t totalVertexSizeInBytes = vertexSpec.vertexSize() * totalNumVertices;
282
283 memcpy(vdata, fPrePreparedVertices, totalVertexSizeInBytes);
284 } else {
285 this->tessellate(vertexSpec, (char*) vdata);
Michael Ludwig69858532018-11-28 15:34:34 -0500286 }
287
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400288 if (vertexSpec.needsIndexBuffer()) {
Chris Daltondbb833b2020-03-17 12:15:46 -0600289 fIndexBuffer = GrQuadPerEdgeAA::GetIndexBuffer(target, vertexSpec.indexBufferOption());
290 if (!fIndexBuffer) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400291 SkDebugf("Could not allocate indices\n");
292 return;
293 }
294 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700295 }
Michael Ludwig69858532018-11-28 15:34:34 -0500296
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700297 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600298 if (!fVertexBuffer) {
299 return;
300 }
301
302 const VertexSpec vertexSpec = this->vertexSpec();
303
304 if (vertexSpec.needsIndexBuffer() && !fIndexBuffer) {
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500305 return;
306 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500307
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500308 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400309 this->createProgramInfo(flushState);
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500310 }
311
Chris Daltondbb833b2020-03-17 12:15:46 -0600312 const int totalNumVertices = fQuads.count() * vertexSpec.verticesPerQuad();
313
Chris Dalton765ed362020-03-16 17:34:44 -0600314 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
Greg Daniel426274b2020-07-20 11:37:38 -0400315 flushState->bindBuffers(std::move(fIndexBuffer), nullptr, std::move(fVertexBuffer));
Chris Dalton765ed362020-03-16 17:34:44 -0600316 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
Chris Daltondbb833b2020-03-17 12:15:46 -0600317 GrQuadPerEdgeAA::IssueDraw(flushState->caps(), flushState->opsRenderPass(), vertexSpec, 0,
318 fQuads.count(), totalNumVertices, fBaseVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700319 }
Michael Ludwig69858532018-11-28 15:34:34 -0500320
Herb Derbye25c3002020-10-27 15:57:27 -0400321 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomon5f394272019-07-02 14:07:49 -0400322 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Michael Ludwig69858532018-11-28 15:34:34 -0500323 const auto* that = t->cast<FillRectOp>();
324
Robert Phillipsb69001f2019-10-29 12:16:35 -0400325 bool upgradeToCoverageAAOnMerge = false;
326 if (fHelper.aaType() != that->fHelper.aaType()) {
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400327 if (!CanUpgradeAAOnMerge(fHelper.aaType(), that->fHelper.aaType())) {
Robert Phillipsb69001f2019-10-29 12:16:35 -0400328 return CombineResult::kCannotCombine;
329 }
330 upgradeToCoverageAAOnMerge = true;
331 }
332
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400333 if (CombinedQuadCountWillOverflow(fHelper.aaType(), upgradeToCoverageAAOnMerge,
334 fQuads.count() + that->fQuads.count())) {
335 return CombineResult::kCannotCombine;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500336 }
337
Michael Ludwigc96fc372019-01-08 15:46:15 -0500338 // Unlike most users of the draw op helper, this op can merge none-aa and coverage-aa draw
339 // ops together, so pass true as the last argument.
Michael Ludwig69858532018-11-28 15:34:34 -0500340 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds(), true)) {
341 return CombineResult::kCannotCombine;
342 }
343
Michael Ludwigdcd48212019-01-08 15:28:57 -0500344 // If the paints were compatible, the trivial/solid-color state should be the same
345 SkASSERT(fHelper.isTrivial() == that->fHelper.isTrivial());
Michael Ludwig69858532018-11-28 15:34:34 -0500346
Michael Ludwigdcd48212019-01-08 15:28:57 -0500347 // If the processor sets are compatible, the two ops are always compatible; it just needs to
348 // adjust the state of the op to be the more general quad and aa types of the two ops and
349 // then concatenate the per-quad data.
Brian Osman788b9162020-02-07 10:36:46 -0500350 fColorType = std::max(fColorType, that->fColorType);
Michael Ludwig69858532018-11-28 15:34:34 -0500351
352 // The helper stores the aa type, but isCompatible(with true arg) allows the two ops' aa
353 // types to be none and coverage, in which case this op's aa type must be lifted to coverage
354 // so that quads with no aa edges can be batched with quads that have some/all edges aa'ed.
Robert Phillipsb69001f2019-10-29 12:16:35 -0400355 if (upgradeToCoverageAAOnMerge) {
Michael Ludwig69858532018-11-28 15:34:34 -0500356 fHelper.setAAType(GrAAType::kCoverage);
357 }
358
Michael Ludwig425eb452019-06-27 10:13:27 -0400359 fQuads.concat(that->fQuads);
Michael Ludwig69858532018-11-28 15:34:34 -0500360 return CombineResult::kMerged;
361 }
362
John Stilesaf366522020-08-13 09:57:34 -0400363#if GR_TEST_UTILS
364 SkString onDumpInfo() const override {
365 SkString str = SkStringPrintf("# draws: %u\n", fQuads.count());
366 str.appendf("Device quad type: %u, local quad type: %u\n",
367 (uint32_t) fQuads.deviceQuadType(), (uint32_t) fQuads.localQuadType());
368 str += fHelper.dumpInfo();
369 int i = 0;
370 auto iter = fQuads.iterator();
371 while(iter.next()) {
372 const ColorAndAA& info = iter.metadata();
373 str += dump_quad_info(i, iter.deviceQuad(), iter.localQuad(),
374 info.fColor, info.fAAFlags);
375 i++;
376 }
377 return str;
378 }
379#endif
380
Michael Ludwig949ceb22020-02-07 10:14:45 -0500381 bool canAddQuads(int numQuads, GrAAType aaType) {
Michael Ludwig69858532018-11-28 15:34:34 -0500382 // The new quad's aa type should be the same as the first quad's or none, except when the
383 // first quad's aa type was already downgraded to none, in which case the stored type must
384 // be lifted to back to the requested type.
Michael Ludwig949ceb22020-02-07 10:14:45 -0500385 int quadCount = fQuads.count() + numQuads;
Robert Phillips438d9862019-11-14 12:46:05 -0500386 if (aaType != fHelper.aaType() && aaType != GrAAType::kNone) {
Michael Ludwig949ceb22020-02-07 10:14:45 -0500387 auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(aaType, quadCount);
388 if (quadCount > GrQuadPerEdgeAA::QuadLimit(indexBufferOption)) {
Robert Phillips438d9862019-11-14 12:46:05 -0500389 // Promoting to the new aaType would've caused an overflow of the indexBuffer
390 // limit
391 return false;
Michael Ludwig69858532018-11-28 15:34:34 -0500392 }
Robert Phillips438d9862019-11-14 12:46:05 -0500393
394 // Original quad was downgraded to non-aa, lift back up to this quad's required type
395 SkASSERT(fHelper.aaType() == GrAAType::kNone);
396 fHelper.setAAType(aaType);
397 } else {
398 auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(fHelper.aaType(),
Michael Ludwig949ceb22020-02-07 10:14:45 -0500399 quadCount);
400 if (quadCount > GrQuadPerEdgeAA::QuadLimit(indexBufferOption)) {
Robert Phillips438d9862019-11-14 12:46:05 -0500401 return false; // This op can't grow any more
402 }
Michael Ludwig69858532018-11-28 15:34:34 -0500403 }
404
Michael Ludwig949ceb22020-02-07 10:14:45 -0500405 return true;
406 }
407
408 // Similar to onCombineIfPossible, but adds a quad assuming its op would have been compatible.
409 // But since it's avoiding the op list management, it must update the op's bounds.
410 bool addQuad(DrawQuad* quad, const SkPMColor4f& color, GrAAType aaType) {
Michael Ludwig69858532018-11-28 15:34:34 -0500411 SkRect newBounds = this->bounds();
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500412 newBounds.joinPossiblyEmptyRect(quad->fDevice.bounds());
Michael Ludwig949ceb22020-02-07 10:14:45 -0500413
414 DrawQuad extra;
Michael Ludwig465864c2020-02-10 09:30:04 -0500415 int count = quad->fEdgeFlags != GrQuadAAFlags::kNone ? GrQuadUtils::ClipToW0(quad, &extra)
416 : 1;
Michael Ludwig949ceb22020-02-07 10:14:45 -0500417 if (count == 0 ) {
418 // Just skip the append (trivial success)
419 return true;
420 } else if (!this->canAddQuads(count, aaType)) {
421 // Not enough room in the index buffer for the AA type
422 return false;
423 } else {
424 // Can actually add the 1 or 2 quads representing the draw
425 fQuads.append(quad->fDevice, { color, quad->fEdgeFlags },
426 fHelper.isTrivial() ? nullptr : &quad->fLocal);
427 if (count > 1) {
428 fQuads.append(extra.fDevice, { color, extra.fEdgeFlags },
429 fHelper.isTrivial() ? nullptr : &extra.fLocal);
430 }
431 // Update the bounds
432 this->setBounds(newBounds, HasAABloat(fHelper.aaType() == GrAAType::kCoverage),
433 IsHairline::kNo);
434 return true;
435 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500436 }
437
438 struct ColorAndAA {
439 SkPMColor4f fColor;
440 GrQuadAAFlags fAAFlags;
441 };
Michael Ludwig69858532018-11-28 15:34:34 -0500442
443 Helper fHelper;
Michael Ludwig425eb452019-06-27 10:13:27 -0400444 GrQuadBuffer<ColorAndAA> fQuads;
Robert Phillips93209052019-12-03 15:42:35 -0500445 char* fPrePreparedVertices = nullptr;
Michael Ludwig69858532018-11-28 15:34:34 -0500446
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500447 GrProgramInfo* fProgramInfo = nullptr;
448 ColorType fColorType;
Michael Ludwig69858532018-11-28 15:34:34 -0500449
Chris Daltondbb833b2020-03-17 12:15:46 -0600450 sk_sp<const GrBuffer> fVertexBuffer;
451 sk_sp<const GrBuffer> fIndexBuffer;
452 int fBaseVertex;
453
John Stiles7571f9e2020-09-02 22:42:33 -0400454 using INHERITED = GrMeshDrawOp;
Michael Ludwig69858532018-11-28 15:34:34 -0500455};
456
457} // anonymous namespace
458
Herb Derbyc76d4092020-10-07 16:46:15 -0400459GrOp::Owner GrFillRectOp::Make(GrRecordingContext* context,
460 GrPaint&& paint,
461 GrAAType aaType,
462 DrawQuad* quad,
463 const GrUserStencilSettings* stencil,
464 InputFlags inputFlags) {
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700465 return FillRectOp::Make(context, std::move(paint), aaType, std::move(quad), stencil,
466 inputFlags);
Michael Ludwigd9f917b2019-05-24 12:57:55 -0400467}
468
Herb Derbyc76d4092020-10-07 16:46:15 -0400469GrOp::Owner GrFillRectOp::MakeNonAARect(GrRecordingContext* context,
470 GrPaint&& paint,
471 const SkMatrix& view,
472 const SkRect& rect,
473 const GrUserStencilSettings* stencil) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500474 DrawQuad quad{GrQuad::MakeFromRect(rect, view), GrQuad(rect), GrQuadAAFlags::kNone};
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700475 return FillRectOp::Make(context, std::move(paint), GrAAType::kNone, &quad, stencil,
476 InputFlags::kNone);
Michael Ludwig009b92e2019-02-15 16:03:53 -0500477}
478
Herb Derbyc76d4092020-10-07 16:46:15 -0400479GrOp::Owner GrFillRectOp::MakeOp(GrRecordingContext* context,
480 GrPaint&& paint,
481 GrAAType aaType,
482 const SkMatrix& viewMatrix,
483 const GrRenderTargetContext::QuadSetEntry quads[],
484 int cnt,
485 const GrUserStencilSettings* stencilSettings,
486 int* numConsumed) {
Michael Ludwig69858532018-11-28 15:34:34 -0500487 // First make a draw op for the first quad in the set
488 SkASSERT(cnt > 0);
Michael Ludwig69858532018-11-28 15:34:34 -0500489
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500490 DrawQuad quad{GrQuad::MakeFromRect(quads[0].fRect, viewMatrix),
491 GrQuad::MakeFromRect(quads[0].fRect, quads[0].fLocalMatrix),
492 quads[0].fAAFlags};
Michael Ludwig69858532018-11-28 15:34:34 -0500493 paint.setColor4f(quads[0].fColor);
Herb Derbyc76d4092020-10-07 16:46:15 -0400494 GrOp::Owner op = FillRectOp::Make(context, std::move(paint), aaType,
495 &quad, stencilSettings, InputFlags::kNone);
Robert Phillips438d9862019-11-14 12:46:05 -0500496 FillRectOp* fillRects = op->cast<FillRectOp>();
Michael Ludwig69858532018-11-28 15:34:34 -0500497
Robert Phillips438d9862019-11-14 12:46:05 -0500498 *numConsumed = 1;
Michael Ludwig69858532018-11-28 15:34:34 -0500499 // Accumulate remaining quads similar to onCombineIfPossible() without creating an op
500 for (int i = 1; i < cnt; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500501 quad = {GrQuad::MakeFromRect(quads[i].fRect, viewMatrix),
502 GrQuad::MakeFromRect(quads[i].fRect, quads[i].fLocalMatrix),
503 quads[i].fAAFlags};
Michael Ludwig69858532018-11-28 15:34:34 -0500504
505 GrAAType resolvedAA;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500506 GrQuadUtils::ResolveAAType(aaType, quads[i].fAAFlags, quad.fDevice,
507 &resolvedAA, &quad.fEdgeFlags);
Michael Ludwig69858532018-11-28 15:34:34 -0500508
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500509 if (!fillRects->addQuad(&quad, quads[i].fColor, resolvedAA)) {
Robert Phillips438d9862019-11-14 12:46:05 -0500510 break;
511 }
512
513 (*numConsumed)++;
Michael Ludwig69858532018-11-28 15:34:34 -0500514 }
515
516 return op;
517}
518
Robert Phillips438d9862019-11-14 12:46:05 -0500519void GrFillRectOp::AddFillRectOps(GrRenderTargetContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400520 const GrClip* clip,
Robert Phillips438d9862019-11-14 12:46:05 -0500521 GrRecordingContext* context,
522 GrPaint&& paint,
523 GrAAType aaType,
524 const SkMatrix& viewMatrix,
525 const GrRenderTargetContext::QuadSetEntry quads[],
526 int cnt,
527 const GrUserStencilSettings* stencilSettings) {
528
529 int offset = 0;
530 int numLeft = cnt;
531 while (numLeft) {
532 int numConsumed = 0;
533
Herb Derbyc76d4092020-10-07 16:46:15 -0400534 GrOp::Owner op = MakeOp(context, GrPaint::Clone(paint), aaType, viewMatrix,
535 &quads[offset], numLeft, stencilSettings,
536 &numConsumed);
Robert Phillips438d9862019-11-14 12:46:05 -0500537
538 offset += numConsumed;
539 numLeft -= numConsumed;
540
541 rtc->addDrawOp(clip, std::move(op));
542 }
543
544 SkASSERT(offset == cnt);
545}
Michael Ludwig69858532018-11-28 15:34:34 -0500546
547#if GR_TEST_UTILS
548
Robert Phillips438d9862019-11-14 12:46:05 -0500549uint32_t GrFillRectOp::ClassID() {
550 return FillRectOp::ClassID();
551}
552
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500553#include "src/gpu/GrDrawOpTest.h"
554#include "src/gpu/SkGr.h"
Michael Ludwig69858532018-11-28 15:34:34 -0500555
556GR_DRAW_OP_TEST_DEFINE(FillRectOp) {
557 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
558 SkRect rect = GrTest::TestRect(random);
559
560 GrAAType aaType = GrAAType::kNone;
561 if (random->nextBool()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600562 aaType = (numSamples > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Michael Ludwig69858532018-11-28 15:34:34 -0500563 }
564 const GrUserStencilSettings* stencil = random->nextBool() ? nullptr
565 : GrGetRandomStencil(random, context);
566
567 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
568 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
569 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
570 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
571 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
572
573 if (random->nextBool()) {
574 if (random->nextBool()) {
Robert Phillips438d9862019-11-14 12:46:05 -0500575 // Single local matrix
576 SkMatrix localMatrix = GrTest::TestMatrixInvertible(random);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500577 DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix),
578 GrQuad::MakeFromRect(rect, localMatrix), aaFlags};
579 return GrFillRectOp::Make(context, std::move(paint), aaType, &quad, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500580 } else {
581 // Pass local rect directly
582 SkRect localRect = GrTest::TestRect(random);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500583 DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix),
584 GrQuad(localRect), aaFlags};
585 return GrFillRectOp::Make(context, std::move(paint), aaType, &quad, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500586 }
587 } else {
588 // The simplest constructor
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500589 DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix), GrQuad(rect), aaFlags};
590 return GrFillRectOp::Make(context, std::move(paint), aaType, &quad, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500591 }
592}
593
594#endif