blob: 82e9fee683c48ff27e925f6655b876a5dd7ac9ed [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).
Michael Ludwig575c9212021-07-13 11:09:52 -040087 bool hairline = GrQuadUtils::WillUseHairline(quad->fDevice, aaType, quad->fEdgeFlags);
Michael Ludwig949ceb22020-02-07 10:14:45 -050088 this->setBounds(quad->fDevice.bounds(), HasAABloat(aaType == GrAAType::kCoverage),
Michael Ludwig575c9212021-07-13 11:09:52 -040089 hairline ? IsHairline::kYes : IsHairline::kNo);
Michael Ludwig949ceb22020-02-07 10:14:45 -050090 DrawQuad extra;
Michael Ludwig65299902021-05-13 12:02:23 -040091 // Always crop to W>0 to remain consistent with GrQuad::bounds()
92 int count = GrQuadUtils::ClipToW0(quad, &extra);
Michael Ludwig949ceb22020-02-07 10:14:45 -050093 if (count == 0) {
94 // We can't discard the op at this point, but disable AA flags so it won't go through
95 // inset/outset processing
96 quad->fEdgeFlags = GrQuadAAFlags::kNone;
97 count = 1;
98 }
99
Michael Ludwig425eb452019-06-27 10:13:27 -0400100 // Conservatively keep track of the local coordinates; it may be that the paint doesn't
101 // need them after analysis is finished. If the paint is known to be solid up front they
102 // can be skipped entirely.
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500103 fQuads.append(quad->fDevice, {paintColor, quad->fEdgeFlags},
104 fHelper.isTrivial() ? nullptr : &quad->fLocal);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500105 if (count > 1) {
106 fQuads.append(extra.fDevice, { paintColor, extra.fEdgeFlags },
107 fHelper.isTrivial() ? nullptr : &extra.fLocal);
108 }
Michael Ludwig69858532018-11-28 15:34:34 -0500109 }
110
111 const char* name() const override { return "FillRectOp"; }
112
Robert Phillips294723d2021-06-17 09:23:58 -0400113 void visitProxies(const GrVisitProxyFunc& func) const override {
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500114 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600115 fProgramInfo->visitFPProxies(func);
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500116 } else {
117 return fHelper.visitProxies(func);
118 }
Michael Ludwig69858532018-11-28 15:34:34 -0500119 }
120
Chris Dalton57ab06c2021-04-22 12:57:28 -0600121 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
122 GrClampType clampType) override {
Michael Ludwig69858532018-11-28 15:34:34 -0500123 // Initialize aggregate color analysis with the first quad's color (which always exists)
Michael Ludwig425eb452019-06-27 10:13:27 -0400124 auto iter = fQuads.metadata();
125 SkAssertResult(iter.next());
126 GrProcessorAnalysisColor quadColors(iter->fColor);
Michael Ludwig69858532018-11-28 15:34:34 -0500127 // Then combine the colors of any additional quads (e.g. from MakeSet)
Michael Ludwig425eb452019-06-27 10:13:27 -0400128 while(iter.next()) {
129 quadColors = GrProcessorAnalysisColor::Combine(quadColors, iter->fColor);
Michael Ludwigca91e1f2018-12-10 10:44:44 -0500130 if (quadColors.isUnknown()) {
131 // No point in accumulating additional starting colors, combining cannot make it
132 // less unknown.
133 break;
134 }
Michael Ludwig69858532018-11-28 15:34:34 -0500135 }
Michael Ludwig72ab3462018-12-10 12:43:36 -0500136
137 // If the AA type is coverage, it will be a single value per pixel; if it's not coverage AA
138 // then the coverage is always 1.0, so specify kNone for more optimal blending.
Robert Phillips360ec182020-03-26 13:29:50 -0400139 auto coverage = fHelper.aaType() == GrAAType::kCoverage
140 ? GrProcessorAnalysisCoverage::kSingleChannel
141 : GrProcessorAnalysisCoverage::kNone;
Chris Dalton57ab06c2021-04-22 12:57:28 -0600142 auto result = fHelper.finalizeProcessors(caps, clip, clampType, coverage, &quadColors);
Michael Ludwig69858532018-11-28 15:34:34 -0500143 // If there is a constant color after analysis, that means all of the quads should be set
144 // to the same color (even if they started out with different colors).
Michael Ludwig425eb452019-06-27 10:13:27 -0400145 iter = fQuads.metadata();
Michael Ludwig69858532018-11-28 15:34:34 -0500146 SkPMColor4f colorOverride;
147 if (quadColors.isConstant(&colorOverride)) {
Brian Osman2715bf52019-12-06 14:38:47 -0500148 fColorType = GrQuadPerEdgeAA::MinColorType(colorOverride);
Michael Ludwig425eb452019-06-27 10:13:27 -0400149 while(iter.next()) {
150 iter->fColor = colorOverride;
Michael Ludwig69858532018-11-28 15:34:34 -0500151 }
Brian Osman8fa7ab42019-03-18 10:22:42 -0400152 } else {
153 // Otherwise compute the color type needed as the max over all quads.
154 fColorType = ColorType::kNone;
Michael Ludwig425eb452019-06-27 10:13:27 -0400155 while(iter.next()) {
Brian Osman788b9162020-02-07 10:36:46 -0500156 fColorType = std::max(fColorType, GrQuadPerEdgeAA::MinColorType(iter->fColor));
Brian Osman8fa7ab42019-03-18 10:22:42 -0400157 }
Michael Ludwig69858532018-11-28 15:34:34 -0500158 }
Brian Salomon41f9c3c2019-03-25 11:06:12 -0400159 // Most SkShaders' FPs multiply their calculated color by the paint color or alpha. We want
160 // to use ColorType::kNone to optimize out that multiply. However, if there are no color
161 // FPs then were really writing a special shader for white rectangles and not saving any
162 // multiples. So in that case use bytes to avoid the extra shader (and possibly work around
163 // an ANGLE issue: crbug.com/942565).
164 if (fColorType == ColorType::kNone && !result.hasColorFragmentProcessor()) {
165 fColorType = ColorType::kByte;
166 }
Michael Ludwig69858532018-11-28 15:34:34 -0500167
168 return result;
169 }
170
171 FixedFunctionFlags fixedFunctionFlags() const override {
172 // Since the AA type of the whole primitive is kept consistent with the per edge AA flags
173 // the helper's fixed function flags are appropriate.
174 return fHelper.fixedFunctionFlags();
175 }
176
177 DEFINE_OP_CLASS_ID
178
179private:
Robert Phillips438d9862019-11-14 12:46:05 -0500180 friend class ::GrFillRectOp; // for access to addQuad
181
182#if GR_TEST_UTILS
183 int numQuads() const final { return fQuads.count(); }
184#endif
Michael Ludwig69858532018-11-28 15:34:34 -0500185
Brian Salomonb8c4add2021-06-28 09:20:44 -0400186 VertexSpec vertexSpec() const {
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400187 auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(fHelper.aaType(),
Brian Salomonb8c4add2021-06-28 09:20:44 -0400188 fQuads.count());
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400189
Robert Phillips93209052019-12-03 15:42:35 -0500190 return VertexSpec(fQuads.deviceQuadType(), fColorType, fQuads.localQuadType(),
Brian Salomon2432d062020-04-16 20:48:09 -0400191 fHelper.usesLocalCoords(), GrQuadPerEdgeAA::Subset::kNo,
Robert Phillips93209052019-12-03 15:42:35 -0500192 fHelper.aaType(),
193 fHelper.compatibleWithCoverageAsAlpha(), indexBufferOption);
194 }
195
Robert Phillips2669a7b2020-03-12 12:07:19 -0400196 GrProgramInfo* programInfo() override {
Robert Phillips2669a7b2020-03-12 12:07:19 -0400197 return fProgramInfo;
198 }
199
Robert Phillips4133dc42020-03-11 15:55:55 -0400200 void onCreateProgramInfo(const GrCaps* caps,
201 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500202 const GrSurfaceProxyView& writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400203 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -0400204 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500205 GrXferBarrierFlags renderPassXferBarriers,
206 GrLoadOp colorLoadOp) override {
Brian Salomonb8c4add2021-06-28 09:20:44 -0400207 const VertexSpec vertexSpec = this->vertexSpec();
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500208
209 GrGeometryProcessor* gp = GrQuadPerEdgeAA::MakeProcessor(arena, vertexSpec);
210 SkASSERT(gp->vertexStride() == vertexSpec.vertexSize());
211
Brian Salomon8afde5f2020-04-01 16:22:00 -0400212 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400213 std::move(appliedClip),
214 dstProxyView, gp,
Greg Danield358cbe2020-09-11 09:33:54 -0400215 vertexSpec.primitiveType(),
Greg Daniel42dbca52020-11-20 10:22:43 -0500216 renderPassXferBarriers, colorLoadOp);
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500217 }
218
Robert Phillips473a8482020-10-20 10:44:15 -0400219 void onPrePrepareDraws(GrRecordingContext* rContext,
Adlai Hollere2296f72020-11-19 13:41:26 -0500220 const GrSurfaceProxyView& writeView,
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500221 GrAppliedClip* clip,
John Stiles52cb1d02021-06-02 11:58:05 -0400222 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500223 GrXferBarrierFlags renderPassXferBarriers,
224 GrLoadOp colorLoadOp) override {
Robert Phillips93209052019-12-03 15:42:35 -0500225 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
226
227 SkASSERT(!fPrePreparedVertices);
228
Robert Phillips473a8482020-10-20 10:44:15 -0400229 INHERITED::onPrePrepareDraws(rContext, writeView, clip, dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500230 renderPassXferBarriers, colorLoadOp);
Robert Phillips93209052019-12-03 15:42:35 -0500231
Robert Phillips473a8482020-10-20 10:44:15 -0400232 SkArenaAlloc* arena = rContext->priv().recordTimeAllocator();
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500233
Brian Salomonb8c4add2021-06-28 09:20:44 -0400234 const VertexSpec vertexSpec = this->vertexSpec();
Robert Phillips93209052019-12-03 15:42:35 -0500235
236 const int totalNumVertices = fQuads.count() * vertexSpec.verticesPerQuad();
237 const size_t totalVertexSizeInBytes = vertexSpec.vertexSize() * totalNumVertices;
238
239 fPrePreparedVertices = arena->makeArrayDefault<char>(totalVertexSizeInBytes);
240
241 this->tessellate(vertexSpec, fPrePreparedVertices);
242 }
243
244 void tessellate(const VertexSpec& vertexSpec, char* dst) const {
245 static constexpr SkRect kEmptyDomain = SkRect::MakeEmpty();
246
247 GrQuadPerEdgeAA::Tessellator tessellator(vertexSpec, dst);
248 auto iter = fQuads.iterator();
249 while (iter.next()) {
250 // All entries should have local coords, or no entries should have local coords,
251 // matching !helper.isTrivial() (which is more conservative than helper.usesLocalCoords)
252 SkASSERT(iter.isLocalValid() != fHelper.isTrivial());
253 auto info = iter.metadata();
254 tessellator.append(iter.deviceQuad(), iter.localQuad(),
255 info.fColor, kEmptyDomain, info.fAAFlags);
256 }
257 }
258
Robert Phillips71143952021-06-17 14:55:07 -0400259 void onPrepareDraws(GrMeshDrawTarget* target) override {
Robert Phillips93209052019-12-03 15:42:35 -0500260 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
261
Brian Salomonb8c4add2021-06-28 09:20:44 -0400262 const VertexSpec vertexSpec = this->vertexSpec();
Robert Phillips93209052019-12-03 15:42:35 -0500263
Michael Ludwigdcd48212019-01-08 15:28:57 -0500264 // Make sure that if the op thought it was a solid color, the vertex spec does not use
265 // local coords.
266 SkASSERT(!fHelper.isTrivial() || !fHelper.usesLocalCoords());
Michael Ludwig69858532018-11-28 15:34:34 -0500267
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400268 const int totalNumVertices = fQuads.count() * vertexSpec.verticesPerQuad();
269
Michael Ludwig69858532018-11-28 15:34:34 -0500270 // Fill the allocated vertex data
Robert Phillips93209052019-12-03 15:42:35 -0500271 void* vdata = target->makeVertexSpace(vertexSpec.vertexSize(), totalNumVertices,
Chris Daltondbb833b2020-03-17 12:15:46 -0600272 &fVertexBuffer, &fBaseVertex);
Michael Ludwig69858532018-11-28 15:34:34 -0500273 if (!vdata) {
274 SkDebugf("Could not allocate vertices\n");
275 return;
276 }
277
Robert Phillips93209052019-12-03 15:42:35 -0500278 if (fPrePreparedVertices) {
279 const size_t totalVertexSizeInBytes = vertexSpec.vertexSize() * totalNumVertices;
280
281 memcpy(vdata, fPrePreparedVertices, totalVertexSizeInBytes);
282 } else {
283 this->tessellate(vertexSpec, (char*) vdata);
Michael Ludwig69858532018-11-28 15:34:34 -0500284 }
285
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400286 if (vertexSpec.needsIndexBuffer()) {
Chris Daltondbb833b2020-03-17 12:15:46 -0600287 fIndexBuffer = GrQuadPerEdgeAA::GetIndexBuffer(target, vertexSpec.indexBufferOption());
288 if (!fIndexBuffer) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400289 SkDebugf("Could not allocate indices\n");
290 return;
291 }
292 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700293 }
Michael Ludwig69858532018-11-28 15:34:34 -0500294
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700295 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600296 if (!fVertexBuffer) {
297 return;
298 }
299
Brian Salomonb8c4add2021-06-28 09:20:44 -0400300 const VertexSpec vertexSpec = this->vertexSpec();
Chris Daltondbb833b2020-03-17 12:15:46 -0600301
302 if (vertexSpec.needsIndexBuffer() && !fIndexBuffer) {
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500303 return;
304 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500305
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500306 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400307 this->createProgramInfo(flushState);
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500308 }
309
Chris Daltondbb833b2020-03-17 12:15:46 -0600310 const int totalNumVertices = fQuads.count() * vertexSpec.verticesPerQuad();
311
Chris Dalton765ed362020-03-16 17:34:44 -0600312 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
Greg Daniel426274b2020-07-20 11:37:38 -0400313 flushState->bindBuffers(std::move(fIndexBuffer), nullptr, std::move(fVertexBuffer));
Robert Phillips787fd9d2021-03-22 14:48:09 -0400314 flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
Chris Daltondbb833b2020-03-17 12:15:46 -0600315 GrQuadPerEdgeAA::IssueDraw(flushState->caps(), flushState->opsRenderPass(), vertexSpec, 0,
316 fQuads.count(), totalNumVertices, fBaseVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700317 }
Michael Ludwig69858532018-11-28 15:34:34 -0500318
Herb Derbye25c3002020-10-27 15:57:27 -0400319 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomon5f394272019-07-02 14:07:49 -0400320 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Michael Ludwig69858532018-11-28 15:34:34 -0500321 const auto* that = t->cast<FillRectOp>();
322
Robert Phillipsb69001f2019-10-29 12:16:35 -0400323 bool upgradeToCoverageAAOnMerge = false;
324 if (fHelper.aaType() != that->fHelper.aaType()) {
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400325 if (!CanUpgradeAAOnMerge(fHelper.aaType(), that->fHelper.aaType())) {
Robert Phillipsb69001f2019-10-29 12:16:35 -0400326 return CombineResult::kCannotCombine;
327 }
328 upgradeToCoverageAAOnMerge = true;
329 }
330
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400331 if (CombinedQuadCountWillOverflow(fHelper.aaType(), upgradeToCoverageAAOnMerge,
332 fQuads.count() + that->fQuads.count())) {
333 return CombineResult::kCannotCombine;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500334 }
335
Michael Ludwigc96fc372019-01-08 15:46:15 -0500336 // Unlike most users of the draw op helper, this op can merge none-aa and coverage-aa draw
337 // ops together, so pass true as the last argument.
Michael Ludwig69858532018-11-28 15:34:34 -0500338 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds(), true)) {
339 return CombineResult::kCannotCombine;
340 }
341
Michael Ludwigdcd48212019-01-08 15:28:57 -0500342 // If the paints were compatible, the trivial/solid-color state should be the same
343 SkASSERT(fHelper.isTrivial() == that->fHelper.isTrivial());
Michael Ludwig69858532018-11-28 15:34:34 -0500344
Michael Ludwigdcd48212019-01-08 15:28:57 -0500345 // If the processor sets are compatible, the two ops are always compatible; it just needs to
346 // adjust the state of the op to be the more general quad and aa types of the two ops and
347 // then concatenate the per-quad data.
Brian Osman788b9162020-02-07 10:36:46 -0500348 fColorType = std::max(fColorType, that->fColorType);
Michael Ludwig69858532018-11-28 15:34:34 -0500349
350 // The helper stores the aa type, but isCompatible(with true arg) allows the two ops' aa
351 // types to be none and coverage, in which case this op's aa type must be lifted to coverage
352 // 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 -0400353 if (upgradeToCoverageAAOnMerge) {
Michael Ludwig69858532018-11-28 15:34:34 -0500354 fHelper.setAAType(GrAAType::kCoverage);
355 }
356
Michael Ludwig425eb452019-06-27 10:13:27 -0400357 fQuads.concat(that->fQuads);
Michael Ludwig69858532018-11-28 15:34:34 -0500358 return CombineResult::kMerged;
359 }
360
John Stilesaf366522020-08-13 09:57:34 -0400361#if GR_TEST_UTILS
362 SkString onDumpInfo() const override {
363 SkString str = SkStringPrintf("# draws: %u\n", fQuads.count());
364 str.appendf("Device quad type: %u, local quad type: %u\n",
365 (uint32_t) fQuads.deviceQuadType(), (uint32_t) fQuads.localQuadType());
366 str += fHelper.dumpInfo();
367 int i = 0;
368 auto iter = fQuads.iterator();
369 while(iter.next()) {
370 const ColorAndAA& info = iter.metadata();
371 str += dump_quad_info(i, iter.deviceQuad(), iter.localQuad(),
372 info.fColor, info.fAAFlags);
373 i++;
374 }
375 return str;
376 }
377#endif
378
Brian Salomonb8c4add2021-06-28 09:20:44 -0400379 bool canAddQuads(int numQuads, GrAAType aaType) {
Michael Ludwig69858532018-11-28 15:34:34 -0500380 // The new quad's aa type should be the same as the first quad's or none, except when the
381 // first quad's aa type was already downgraded to none, in which case the stored type must
382 // be lifted to back to the requested type.
Michael Ludwig949ceb22020-02-07 10:14:45 -0500383 int quadCount = fQuads.count() + numQuads;
Robert Phillips438d9862019-11-14 12:46:05 -0500384 if (aaType != fHelper.aaType() && aaType != GrAAType::kNone) {
Brian Salomonb8c4add2021-06-28 09:20:44 -0400385 auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(aaType, quadCount);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500386 if (quadCount > GrQuadPerEdgeAA::QuadLimit(indexBufferOption)) {
Robert Phillips438d9862019-11-14 12:46:05 -0500387 // Promoting to the new aaType would've caused an overflow of the indexBuffer
388 // limit
389 return false;
Michael Ludwig69858532018-11-28 15:34:34 -0500390 }
Robert Phillips438d9862019-11-14 12:46:05 -0500391
392 // Original quad was downgraded to non-aa, lift back up to this quad's required type
393 SkASSERT(fHelper.aaType() == GrAAType::kNone);
394 fHelper.setAAType(aaType);
395 } else {
396 auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(fHelper.aaType(),
Brian Salomonb8c4add2021-06-28 09:20:44 -0400397 quadCount);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500398 if (quadCount > GrQuadPerEdgeAA::QuadLimit(indexBufferOption)) {
Robert Phillips438d9862019-11-14 12:46:05 -0500399 return false; // This op can't grow any more
400 }
Michael Ludwig69858532018-11-28 15:34:34 -0500401 }
402
Michael Ludwig949ceb22020-02-07 10:14:45 -0500403 return true;
404 }
405
406 // Similar to onCombineIfPossible, but adds a quad assuming its op would have been compatible.
407 // But since it's avoiding the op list management, it must update the op's bounds.
Brian Salomonb8c4add2021-06-28 09:20:44 -0400408 bool addQuad(DrawQuad* quad, const SkPMColor4f& color, GrAAType aaType) {
Michael Ludwig69858532018-11-28 15:34:34 -0500409 SkRect newBounds = this->bounds();
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500410 newBounds.joinPossiblyEmptyRect(quad->fDevice.bounds());
Michael Ludwig949ceb22020-02-07 10:14:45 -0500411
412 DrawQuad extra;
Michael Ludwig465864c2020-02-10 09:30:04 -0500413 int count = quad->fEdgeFlags != GrQuadAAFlags::kNone ? GrQuadUtils::ClipToW0(quad, &extra)
414 : 1;
Michael Ludwig949ceb22020-02-07 10:14:45 -0500415 if (count == 0 ) {
416 // Just skip the append (trivial success)
417 return true;
Brian Salomonb8c4add2021-06-28 09:20:44 -0400418 } else if (!this->canAddQuads(count, aaType)) {
Michael Ludwig949ceb22020-02-07 10:14:45 -0500419 // Not enough room in the index buffer for the AA type
420 return false;
421 } else {
422 // Can actually add the 1 or 2 quads representing the draw
423 fQuads.append(quad->fDevice, { color, quad->fEdgeFlags },
424 fHelper.isTrivial() ? nullptr : &quad->fLocal);
425 if (count > 1) {
426 fQuads.append(extra.fDevice, { color, extra.fEdgeFlags },
427 fHelper.isTrivial() ? nullptr : &extra.fLocal);
428 }
429 // Update the bounds
430 this->setBounds(newBounds, HasAABloat(fHelper.aaType() == GrAAType::kCoverage),
431 IsHairline::kNo);
432 return true;
433 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500434 }
435
436 struct ColorAndAA {
437 SkPMColor4f fColor;
438 GrQuadAAFlags fAAFlags;
439 };
Michael Ludwig69858532018-11-28 15:34:34 -0500440
441 Helper fHelper;
Michael Ludwig425eb452019-06-27 10:13:27 -0400442 GrQuadBuffer<ColorAndAA> fQuads;
Robert Phillips93209052019-12-03 15:42:35 -0500443 char* fPrePreparedVertices = nullptr;
Michael Ludwig69858532018-11-28 15:34:34 -0500444
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500445 GrProgramInfo* fProgramInfo = nullptr;
446 ColorType fColorType;
Michael Ludwig69858532018-11-28 15:34:34 -0500447
Chris Daltondbb833b2020-03-17 12:15:46 -0600448 sk_sp<const GrBuffer> fVertexBuffer;
449 sk_sp<const GrBuffer> fIndexBuffer;
450 int fBaseVertex;
451
John Stiles7571f9e2020-09-02 22:42:33 -0400452 using INHERITED = GrMeshDrawOp;
Michael Ludwig69858532018-11-28 15:34:34 -0500453};
454
455} // anonymous namespace
456
Herb Derbyc76d4092020-10-07 16:46:15 -0400457GrOp::Owner GrFillRectOp::Make(GrRecordingContext* context,
458 GrPaint&& paint,
459 GrAAType aaType,
460 DrawQuad* quad,
461 const GrUserStencilSettings* stencil,
462 InputFlags inputFlags) {
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700463 return FillRectOp::Make(context, std::move(paint), aaType, std::move(quad), stencil,
464 inputFlags);
Michael Ludwigd9f917b2019-05-24 12:57:55 -0400465}
466
Herb Derbyc76d4092020-10-07 16:46:15 -0400467GrOp::Owner GrFillRectOp::MakeNonAARect(GrRecordingContext* context,
468 GrPaint&& paint,
469 const SkMatrix& view,
470 const SkRect& rect,
471 const GrUserStencilSettings* stencil) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500472 DrawQuad quad{GrQuad::MakeFromRect(rect, view), GrQuad(rect), GrQuadAAFlags::kNone};
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700473 return FillRectOp::Make(context, std::move(paint), GrAAType::kNone, &quad, stencil,
474 InputFlags::kNone);
Michael Ludwig009b92e2019-02-15 16:03:53 -0500475}
476
Herb Derbyc76d4092020-10-07 16:46:15 -0400477GrOp::Owner GrFillRectOp::MakeOp(GrRecordingContext* context,
478 GrPaint&& paint,
479 GrAAType aaType,
480 const SkMatrix& viewMatrix,
Brian Salomoneebe7352020-12-09 16:37:04 -0500481 const GrSurfaceDrawContext::QuadSetEntry quads[],
Herb Derbyc76d4092020-10-07 16:46:15 -0400482 int cnt,
483 const GrUserStencilSettings* stencilSettings,
484 int* numConsumed) {
Michael Ludwig69858532018-11-28 15:34:34 -0500485 // First make a draw op for the first quad in the set
486 SkASSERT(cnt > 0);
Michael Ludwig69858532018-11-28 15:34:34 -0500487
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500488 DrawQuad quad{GrQuad::MakeFromRect(quads[0].fRect, viewMatrix),
489 GrQuad::MakeFromRect(quads[0].fRect, quads[0].fLocalMatrix),
490 quads[0].fAAFlags};
Michael Ludwig69858532018-11-28 15:34:34 -0500491 paint.setColor4f(quads[0].fColor);
Herb Derbyc76d4092020-10-07 16:46:15 -0400492 GrOp::Owner op = FillRectOp::Make(context, std::move(paint), aaType,
493 &quad, stencilSettings, InputFlags::kNone);
Robert Phillips438d9862019-11-14 12:46:05 -0500494 FillRectOp* fillRects = op->cast<FillRectOp>();
Michael Ludwig69858532018-11-28 15:34:34 -0500495
Robert Phillips438d9862019-11-14 12:46:05 -0500496 *numConsumed = 1;
Michael Ludwig69858532018-11-28 15:34:34 -0500497 // Accumulate remaining quads similar to onCombineIfPossible() without creating an op
498 for (int i = 1; i < cnt; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500499 quad = {GrQuad::MakeFromRect(quads[i].fRect, viewMatrix),
500 GrQuad::MakeFromRect(quads[i].fRect, quads[i].fLocalMatrix),
501 quads[i].fAAFlags};
Michael Ludwig69858532018-11-28 15:34:34 -0500502
503 GrAAType resolvedAA;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500504 GrQuadUtils::ResolveAAType(aaType, quads[i].fAAFlags, quad.fDevice,
505 &resolvedAA, &quad.fEdgeFlags);
Michael Ludwig69858532018-11-28 15:34:34 -0500506
Brian Salomonb8c4add2021-06-28 09:20:44 -0400507 if (!fillRects->addQuad(&quad, quads[i].fColor, resolvedAA)) {
Robert Phillips438d9862019-11-14 12:46:05 -0500508 break;
509 }
510
511 (*numConsumed)++;
Michael Ludwig69858532018-11-28 15:34:34 -0500512 }
513
514 return op;
515}
516
Brian Salomoneebe7352020-12-09 16:37:04 -0500517void GrFillRectOp::AddFillRectOps(GrSurfaceDrawContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400518 const GrClip* clip,
Robert Phillips438d9862019-11-14 12:46:05 -0500519 GrRecordingContext* context,
520 GrPaint&& paint,
521 GrAAType aaType,
522 const SkMatrix& viewMatrix,
Brian Salomoneebe7352020-12-09 16:37:04 -0500523 const GrSurfaceDrawContext::QuadSetEntry quads[],
Robert Phillips438d9862019-11-14 12:46:05 -0500524 int cnt,
525 const GrUserStencilSettings* stencilSettings) {
526
527 int offset = 0;
528 int numLeft = cnt;
529 while (numLeft) {
530 int numConsumed = 0;
531
Herb Derbyc76d4092020-10-07 16:46:15 -0400532 GrOp::Owner op = MakeOp(context, GrPaint::Clone(paint), aaType, viewMatrix,
533 &quads[offset], numLeft, stencilSettings,
534 &numConsumed);
Robert Phillips438d9862019-11-14 12:46:05 -0500535
536 offset += numConsumed;
537 numLeft -= numConsumed;
538
539 rtc->addDrawOp(clip, std::move(op));
540 }
541
542 SkASSERT(offset == cnt);
543}
Michael Ludwig69858532018-11-28 15:34:34 -0500544
545#if GR_TEST_UTILS
546
Robert Phillips438d9862019-11-14 12:46:05 -0500547uint32_t GrFillRectOp::ClassID() {
548 return FillRectOp::ClassID();
549}
550
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500551#include "src/gpu/GrDrawOpTest.h"
552#include "src/gpu/SkGr.h"
Michael Ludwig69858532018-11-28 15:34:34 -0500553
554GR_DRAW_OP_TEST_DEFINE(FillRectOp) {
555 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
556 SkRect rect = GrTest::TestRect(random);
557
558 GrAAType aaType = GrAAType::kNone;
559 if (random->nextBool()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600560 aaType = (numSamples > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Michael Ludwig69858532018-11-28 15:34:34 -0500561 }
562 const GrUserStencilSettings* stencil = random->nextBool() ? nullptr
563 : GrGetRandomStencil(random, context);
564
565 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
566 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
567 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
568 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
569 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
570
571 if (random->nextBool()) {
572 if (random->nextBool()) {
Robert Phillips438d9862019-11-14 12:46:05 -0500573 // Single local matrix
574 SkMatrix localMatrix = GrTest::TestMatrixInvertible(random);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500575 DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix),
576 GrQuad::MakeFromRect(rect, localMatrix), aaFlags};
577 return GrFillRectOp::Make(context, std::move(paint), aaType, &quad, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500578 } else {
579 // Pass local rect directly
580 SkRect localRect = GrTest::TestRect(random);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500581 DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix),
582 GrQuad(localRect), aaFlags};
583 return GrFillRectOp::Make(context, std::move(paint), aaType, &quad, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500584 }
585 } else {
586 // The simplest constructor
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500587 DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix), GrQuad(rect), aaFlags};
588 return GrFillRectOp::Make(context, std::move(paint), aaType, &quad, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500589 }
590}
591
592#endif