blob: c69bca1a5c51d4bafb3affa449fb8d2a52e9266c [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"
Robert Phillipse40495d2021-07-20 09:40:13 -040014#include "src/gpu/GrOpsTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrPaint.h"
Robert Phillips50d7d6f2020-03-04 11:12:24 -050016#include "src/gpu/GrProgramInfo.h"
Robert Phillipse40495d2021-07-20 09:40:13 -040017#include "src/gpu/GrSurfaceDrawContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/SkGr.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040019#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig425eb452019-06-27 10:13:27 -040020#include "src/gpu/geometry/GrQuadBuffer.h"
Michael Ludwig0f809022019-06-04 09:14:37 -040021#include "src/gpu/geometry/GrQuadUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h"
23#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
24#include "src/gpu/glsl/GrGLSLVarying.h"
25#include "src/gpu/ops/GrMeshDrawOp.h"
26#include "src/gpu/ops/GrQuadPerEdgeAA.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050027#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
Michael Ludwig69858532018-11-28 15:34:34 -050028
29namespace {
30
31using VertexSpec = GrQuadPerEdgeAA::VertexSpec;
32using ColorType = GrQuadPerEdgeAA::ColorType;
33
John Stiles8d9bf642020-08-12 15:07:45 -040034#if GR_TEST_UTILS
Michael Ludwig704d5402019-11-25 09:43:37 -050035static SkString dump_quad_info(int index, const GrQuad* deviceQuad,
36 const GrQuad* localQuad, const SkPMColor4f& color,
Michael Ludwigc96fc372019-01-08 15:46:15 -050037 GrQuadAAFlags aaFlags) {
Michael Ludwig704d5402019-11-25 09:43:37 -050038 GrQuad safeLocal = localQuad ? *localQuad : GrQuad();
Michael Ludwigc96fc372019-01-08 15:46:15 -050039 SkString str;
40 str.appendf("%d: Color: [%.2f, %.2f, %.2f, %.2f], Edge AA: l%u_t%u_r%u_b%u, \n"
41 " device quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), "
42 "(%.2f, %.2f, %.2f)],\n"
43 " local quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), "
44 "(%.2f, %.2f, %.2f)]\n",
45 index, color.fR, color.fG, color.fB, color.fA,
46 (uint32_t) (aaFlags & GrQuadAAFlags::kLeft),
47 (uint32_t) (aaFlags & GrQuadAAFlags::kTop),
48 (uint32_t) (aaFlags & GrQuadAAFlags::kRight),
49 (uint32_t) (aaFlags & GrQuadAAFlags::kBottom),
Michael Ludwig704d5402019-11-25 09:43:37 -050050 deviceQuad->x(0), deviceQuad->y(0), deviceQuad->w(0),
51 deviceQuad->x(1), deviceQuad->y(1), deviceQuad->w(1),
52 deviceQuad->x(2), deviceQuad->y(2), deviceQuad->w(2),
53 deviceQuad->x(3), deviceQuad->y(3), deviceQuad->w(3),
54 safeLocal.x(0), safeLocal.y(0), safeLocal.w(0),
55 safeLocal.x(1), safeLocal.y(1), safeLocal.w(1),
56 safeLocal.x(2), safeLocal.y(2), safeLocal.w(2),
57 safeLocal.x(3), safeLocal.y(3), safeLocal.w(3));
Michael Ludwigc96fc372019-01-08 15:46:15 -050058 return str;
59}
60#endif
Michael Ludwig69858532018-11-28 15:34:34 -050061
Michael Ludwig69858532018-11-28 15:34:34 -050062class FillRectOp final : public GrMeshDrawOp {
63private:
64 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
65
66public:
Herb Derbyc76d4092020-10-07 16:46:15 -040067 static GrOp::Owner Make(GrRecordingContext* context,
68 GrPaint&& paint,
69 GrAAType aaType,
70 DrawQuad* quad,
71 const GrUserStencilSettings* stencilSettings,
72 Helper::InputFlags inputFlags) {
Michael Ludwig69858532018-11-28 15:34:34 -050073 // Clean up deviations between aaType and edgeAA
Michael Ludwig6b45c5d2020-02-07 09:56:38 -050074 GrQuadUtils::ResolveAAType(aaType, quad->fEdgeFlags, quad->fDevice,
75 &aaType, &quad->fEdgeFlags);
76 return Helper::FactoryHelper<FillRectOp>(context, std::move(paint), aaType, quad,
Chris Daltonc3b67eb2020-02-10 21:09:58 -070077 stencilSettings, inputFlags);
Michael Ludwig69858532018-11-28 15:34:34 -050078 }
79
Michael Ludwigdcd48212019-01-08 15:28:57 -050080 // aaType is passed to Helper in the initializer list, so incongruities between aaType and
81 // edgeFlags must be resolved prior to calling this constructor.
Herb Derbyc76d4092020-10-07 16:46:15 -040082 FillRectOp(GrProcessorSet* processorSet, SkPMColor4f paintColor, GrAAType aaType,
Chris Daltonc3b67eb2020-02-10 21:09:58 -070083 DrawQuad* quad, const GrUserStencilSettings* stencil, Helper::InputFlags inputFlags)
Michael Ludwig69858532018-11-28 15:34:34 -050084 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -040085 , fHelper(processorSet, aaType, stencil, inputFlags)
Michael Ludwig425eb452019-06-27 10:13:27 -040086 , fQuads(1, !fHelper.isTrivial()) {
Michael Ludwig949ceb22020-02-07 10:14:45 -050087 // Set bounds before clipping so we don't have to worry about unioning the bounds of
88 // the two potential quads (GrQuad::bounds() is perspective-safe).
Michael Ludwig575c9212021-07-13 11:09:52 -040089 bool hairline = GrQuadUtils::WillUseHairline(quad->fDevice, aaType, quad->fEdgeFlags);
Michael Ludwig949ceb22020-02-07 10:14:45 -050090 this->setBounds(quad->fDevice.bounds(), HasAABloat(aaType == GrAAType::kCoverage),
Michael Ludwig575c9212021-07-13 11:09:52 -040091 hairline ? IsHairline::kYes : IsHairline::kNo);
Michael Ludwig949ceb22020-02-07 10:14:45 -050092 DrawQuad extra;
Michael Ludwig65299902021-05-13 12:02:23 -040093 // Always crop to W>0 to remain consistent with GrQuad::bounds()
94 int count = GrQuadUtils::ClipToW0(quad, &extra);
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
Robert Phillips294723d2021-06-17 09:23:58 -0400115 void visitProxies(const GrVisitProxyFunc& 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 Dalton57ab06c2021-04-22 12:57:28 -0600123 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
124 GrClampType clampType) override {
Michael Ludwig69858532018-11-28 15:34:34 -0500125 // Initialize aggregate color analysis with the first quad's color (which always exists)
Michael Ludwig425eb452019-06-27 10:13:27 -0400126 auto iter = fQuads.metadata();
127 SkAssertResult(iter.next());
128 GrProcessorAnalysisColor quadColors(iter->fColor);
Michael Ludwig69858532018-11-28 15:34:34 -0500129 // Then combine the colors of any additional quads (e.g. from MakeSet)
Michael Ludwig425eb452019-06-27 10:13:27 -0400130 while(iter.next()) {
131 quadColors = GrProcessorAnalysisColor::Combine(quadColors, iter->fColor);
Michael Ludwigca91e1f2018-12-10 10:44:44 -0500132 if (quadColors.isUnknown()) {
133 // No point in accumulating additional starting colors, combining cannot make it
134 // less unknown.
135 break;
136 }
Michael Ludwig69858532018-11-28 15:34:34 -0500137 }
Michael Ludwig72ab3462018-12-10 12:43:36 -0500138
139 // If the AA type is coverage, it will be a single value per pixel; if it's not coverage AA
140 // then the coverage is always 1.0, so specify kNone for more optimal blending.
Robert Phillips360ec182020-03-26 13:29:50 -0400141 auto coverage = fHelper.aaType() == GrAAType::kCoverage
142 ? GrProcessorAnalysisCoverage::kSingleChannel
143 : GrProcessorAnalysisCoverage::kNone;
Chris Dalton57ab06c2021-04-22 12:57:28 -0600144 auto result = fHelper.finalizeProcessors(caps, clip, clampType, coverage, &quadColors);
Michael Ludwig69858532018-11-28 15:34:34 -0500145 // If there is a constant color after analysis, that means all of the quads should be set
146 // to the same color (even if they started out with different colors).
Michael Ludwig425eb452019-06-27 10:13:27 -0400147 iter = fQuads.metadata();
Michael Ludwig69858532018-11-28 15:34:34 -0500148 SkPMColor4f colorOverride;
149 if (quadColors.isConstant(&colorOverride)) {
Brian Osman2715bf52019-12-06 14:38:47 -0500150 fColorType = GrQuadPerEdgeAA::MinColorType(colorOverride);
Michael Ludwig425eb452019-06-27 10:13:27 -0400151 while(iter.next()) {
152 iter->fColor = colorOverride;
Michael Ludwig69858532018-11-28 15:34:34 -0500153 }
Brian Osman8fa7ab42019-03-18 10:22:42 -0400154 } else {
155 // Otherwise compute the color type needed as the max over all quads.
156 fColorType = ColorType::kNone;
Michael Ludwig425eb452019-06-27 10:13:27 -0400157 while(iter.next()) {
Brian Osman788b9162020-02-07 10:36:46 -0500158 fColorType = std::max(fColorType, GrQuadPerEdgeAA::MinColorType(iter->fColor));
Brian Osman8fa7ab42019-03-18 10:22:42 -0400159 }
Michael Ludwig69858532018-11-28 15:34:34 -0500160 }
Brian Salomon41f9c3c2019-03-25 11:06:12 -0400161 // Most SkShaders' FPs multiply their calculated color by the paint color or alpha. We want
162 // to use ColorType::kNone to optimize out that multiply. However, if there are no color
163 // FPs then were really writing a special shader for white rectangles and not saving any
164 // multiples. So in that case use bytes to avoid the extra shader (and possibly work around
165 // an ANGLE issue: crbug.com/942565).
166 if (fColorType == ColorType::kNone && !result.hasColorFragmentProcessor()) {
167 fColorType = ColorType::kByte;
168 }
Michael Ludwig69858532018-11-28 15:34:34 -0500169
170 return result;
171 }
172
173 FixedFunctionFlags fixedFunctionFlags() const override {
174 // Since the AA type of the whole primitive is kept consistent with the per edge AA flags
175 // the helper's fixed function flags are appropriate.
176 return fHelper.fixedFunctionFlags();
177 }
178
179 DEFINE_OP_CLASS_ID
180
181private:
Robert Phillips438d9862019-11-14 12:46:05 -0500182 friend class ::GrFillRectOp; // for access to addQuad
183
184#if GR_TEST_UTILS
185 int numQuads() const final { return fQuads.count(); }
186#endif
Michael Ludwig69858532018-11-28 15:34:34 -0500187
Brian Salomonb8c4add2021-06-28 09:20:44 -0400188 VertexSpec vertexSpec() const {
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400189 auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(fHelper.aaType(),
Brian Salomonb8c4add2021-06-28 09:20:44 -0400190 fQuads.count());
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400191
Robert Phillips93209052019-12-03 15:42:35 -0500192 return VertexSpec(fQuads.deviceQuadType(), fColorType, fQuads.localQuadType(),
Brian Salomon2432d062020-04-16 20:48:09 -0400193 fHelper.usesLocalCoords(), GrQuadPerEdgeAA::Subset::kNo,
Robert Phillips93209052019-12-03 15:42:35 -0500194 fHelper.aaType(),
195 fHelper.compatibleWithCoverageAsAlpha(), indexBufferOption);
196 }
197
Robert Phillips2669a7b2020-03-12 12:07:19 -0400198 GrProgramInfo* programInfo() override {
Robert Phillips2669a7b2020-03-12 12:07:19 -0400199 return fProgramInfo;
200 }
201
Robert Phillips4133dc42020-03-11 15:55:55 -0400202 void onCreateProgramInfo(const GrCaps* caps,
203 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -0500204 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -0600205 bool usesMSAASurface,
Robert Phillips4133dc42020-03-11 15:55:55 -0400206 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -0400207 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500208 GrXferBarrierFlags renderPassXferBarriers,
209 GrLoadOp colorLoadOp) override {
Brian Salomonb8c4add2021-06-28 09:20:44 -0400210 const VertexSpec vertexSpec = this->vertexSpec();
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500211
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(),
Greg Daniel42dbca52020-11-20 10:22:43 -0500219 renderPassXferBarriers, colorLoadOp);
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,
John Stiles52cb1d02021-06-02 11:58:05 -0400225 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500226 GrXferBarrierFlags renderPassXferBarriers,
227 GrLoadOp colorLoadOp) override {
Robert Phillips93209052019-12-03 15:42:35 -0500228 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
229
230 SkASSERT(!fPrePreparedVertices);
231
Robert Phillips473a8482020-10-20 10:44:15 -0400232 INHERITED::onPrePrepareDraws(rContext, writeView, clip, dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -0500233 renderPassXferBarriers, colorLoadOp);
Robert Phillips93209052019-12-03 15:42:35 -0500234
Robert Phillips473a8482020-10-20 10:44:15 -0400235 SkArenaAlloc* arena = rContext->priv().recordTimeAllocator();
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500236
Brian Salomonb8c4add2021-06-28 09:20:44 -0400237 const VertexSpec vertexSpec = this->vertexSpec();
Robert Phillips93209052019-12-03 15:42:35 -0500238
239 const int totalNumVertices = fQuads.count() * vertexSpec.verticesPerQuad();
240 const size_t totalVertexSizeInBytes = vertexSpec.vertexSize() * totalNumVertices;
241
242 fPrePreparedVertices = arena->makeArrayDefault<char>(totalVertexSizeInBytes);
243
244 this->tessellate(vertexSpec, fPrePreparedVertices);
245 }
246
247 void tessellate(const VertexSpec& vertexSpec, char* dst) const {
248 static constexpr SkRect kEmptyDomain = SkRect::MakeEmpty();
249
250 GrQuadPerEdgeAA::Tessellator tessellator(vertexSpec, dst);
251 auto iter = fQuads.iterator();
252 while (iter.next()) {
253 // All entries should have local coords, or no entries should have local coords,
254 // matching !helper.isTrivial() (which is more conservative than helper.usesLocalCoords)
255 SkASSERT(iter.isLocalValid() != fHelper.isTrivial());
256 auto info = iter.metadata();
257 tessellator.append(iter.deviceQuad(), iter.localQuad(),
258 info.fColor, kEmptyDomain, info.fAAFlags);
259 }
260 }
261
Robert Phillips71143952021-06-17 14:55:07 -0400262 void onPrepareDraws(GrMeshDrawTarget* target) override {
Robert Phillips93209052019-12-03 15:42:35 -0500263 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
264
Brian Salomonb8c4add2021-06-28 09:20:44 -0400265 const VertexSpec vertexSpec = this->vertexSpec();
Robert Phillips93209052019-12-03 15:42:35 -0500266
Michael Ludwigdcd48212019-01-08 15:28:57 -0500267 // Make sure that if the op thought it was a solid color, the vertex spec does not use
268 // local coords.
269 SkASSERT(!fHelper.isTrivial() || !fHelper.usesLocalCoords());
Michael Ludwig69858532018-11-28 15:34:34 -0500270
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400271 const int totalNumVertices = fQuads.count() * vertexSpec.verticesPerQuad();
272
Michael Ludwig69858532018-11-28 15:34:34 -0500273 // Fill the allocated vertex data
Robert Phillips93209052019-12-03 15:42:35 -0500274 void* vdata = target->makeVertexSpace(vertexSpec.vertexSize(), totalNumVertices,
Chris Daltondbb833b2020-03-17 12:15:46 -0600275 &fVertexBuffer, &fBaseVertex);
Michael Ludwig69858532018-11-28 15:34:34 -0500276 if (!vdata) {
277 SkDebugf("Could not allocate vertices\n");
278 return;
279 }
280
Robert Phillips93209052019-12-03 15:42:35 -0500281 if (fPrePreparedVertices) {
282 const size_t totalVertexSizeInBytes = vertexSpec.vertexSize() * totalNumVertices;
283
284 memcpy(vdata, fPrePreparedVertices, totalVertexSizeInBytes);
285 } else {
286 this->tessellate(vertexSpec, (char*) vdata);
Michael Ludwig69858532018-11-28 15:34:34 -0500287 }
288
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400289 if (vertexSpec.needsIndexBuffer()) {
Chris Daltondbb833b2020-03-17 12:15:46 -0600290 fIndexBuffer = GrQuadPerEdgeAA::GetIndexBuffer(target, vertexSpec.indexBufferOption());
291 if (!fIndexBuffer) {
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400292 SkDebugf("Could not allocate indices\n");
293 return;
294 }
295 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700296 }
Michael Ludwig69858532018-11-28 15:34:34 -0500297
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700298 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Chris Daltondbb833b2020-03-17 12:15:46 -0600299 if (!fVertexBuffer) {
300 return;
301 }
302
Brian Salomonb8c4add2021-06-28 09:20:44 -0400303 const VertexSpec vertexSpec = this->vertexSpec();
Chris Daltondbb833b2020-03-17 12:15:46 -0600304
305 if (vertexSpec.needsIndexBuffer() && !fIndexBuffer) {
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500306 return;
307 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500308
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500309 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400310 this->createProgramInfo(flushState);
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500311 }
312
Chris Daltondbb833b2020-03-17 12:15:46 -0600313 const int totalNumVertices = fQuads.count() * vertexSpec.verticesPerQuad();
314
Chris Dalton765ed362020-03-16 17:34:44 -0600315 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
Greg Daniel426274b2020-07-20 11:37:38 -0400316 flushState->bindBuffers(std::move(fIndexBuffer), nullptr, std::move(fVertexBuffer));
Robert Phillips787fd9d2021-03-22 14:48:09 -0400317 flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline());
Chris Daltondbb833b2020-03-17 12:15:46 -0600318 GrQuadPerEdgeAA::IssueDraw(flushState->caps(), flushState->opsRenderPass(), vertexSpec, 0,
319 fQuads.count(), totalNumVertices, fBaseVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700320 }
Michael Ludwig69858532018-11-28 15:34:34 -0500321
Herb Derbye25c3002020-10-27 15:57:27 -0400322 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomon5f394272019-07-02 14:07:49 -0400323 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Michael Ludwig69858532018-11-28 15:34:34 -0500324 const auto* that = t->cast<FillRectOp>();
325
Robert Phillipsb69001f2019-10-29 12:16:35 -0400326 bool upgradeToCoverageAAOnMerge = false;
327 if (fHelper.aaType() != that->fHelper.aaType()) {
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400328 if (!CanUpgradeAAOnMerge(fHelper.aaType(), that->fHelper.aaType())) {
Robert Phillipsb69001f2019-10-29 12:16:35 -0400329 return CombineResult::kCannotCombine;
330 }
331 upgradeToCoverageAAOnMerge = true;
332 }
333
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400334 if (CombinedQuadCountWillOverflow(fHelper.aaType(), upgradeToCoverageAAOnMerge,
335 fQuads.count() + that->fQuads.count())) {
336 return CombineResult::kCannotCombine;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500337 }
338
Michael Ludwigc96fc372019-01-08 15:46:15 -0500339 // Unlike most users of the draw op helper, this op can merge none-aa and coverage-aa draw
340 // ops together, so pass true as the last argument.
Michael Ludwig69858532018-11-28 15:34:34 -0500341 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds(), true)) {
342 return CombineResult::kCannotCombine;
343 }
344
Michael Ludwigdcd48212019-01-08 15:28:57 -0500345 // If the paints were compatible, the trivial/solid-color state should be the same
346 SkASSERT(fHelper.isTrivial() == that->fHelper.isTrivial());
Michael Ludwig69858532018-11-28 15:34:34 -0500347
Michael Ludwigdcd48212019-01-08 15:28:57 -0500348 // If the processor sets are compatible, the two ops are always compatible; it just needs to
349 // adjust the state of the op to be the more general quad and aa types of the two ops and
350 // then concatenate the per-quad data.
Brian Osman788b9162020-02-07 10:36:46 -0500351 fColorType = std::max(fColorType, that->fColorType);
Michael Ludwig69858532018-11-28 15:34:34 -0500352
353 // The helper stores the aa type, but isCompatible(with true arg) allows the two ops' aa
354 // types to be none and coverage, in which case this op's aa type must be lifted to coverage
355 // 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 -0400356 if (upgradeToCoverageAAOnMerge) {
Michael Ludwig69858532018-11-28 15:34:34 -0500357 fHelper.setAAType(GrAAType::kCoverage);
358 }
359
Michael Ludwig425eb452019-06-27 10:13:27 -0400360 fQuads.concat(that->fQuads);
Michael Ludwig69858532018-11-28 15:34:34 -0500361 return CombineResult::kMerged;
362 }
363
John Stilesaf366522020-08-13 09:57:34 -0400364#if GR_TEST_UTILS
365 SkString onDumpInfo() const override {
366 SkString str = SkStringPrintf("# draws: %u\n", fQuads.count());
367 str.appendf("Device quad type: %u, local quad type: %u\n",
368 (uint32_t) fQuads.deviceQuadType(), (uint32_t) fQuads.localQuadType());
369 str += fHelper.dumpInfo();
370 int i = 0;
371 auto iter = fQuads.iterator();
372 while(iter.next()) {
373 const ColorAndAA& info = iter.metadata();
374 str += dump_quad_info(i, iter.deviceQuad(), iter.localQuad(),
375 info.fColor, info.fAAFlags);
376 i++;
377 }
378 return str;
379 }
380#endif
381
Brian Salomonb8c4add2021-06-28 09:20:44 -0400382 bool canAddQuads(int numQuads, GrAAType aaType) {
Michael Ludwig69858532018-11-28 15:34:34 -0500383 // The new quad's aa type should be the same as the first quad's or none, except when the
384 // first quad's aa type was already downgraded to none, in which case the stored type must
385 // be lifted to back to the requested type.
Michael Ludwig949ceb22020-02-07 10:14:45 -0500386 int quadCount = fQuads.count() + numQuads;
Robert Phillips438d9862019-11-14 12:46:05 -0500387 if (aaType != fHelper.aaType() && aaType != GrAAType::kNone) {
Brian Salomonb8c4add2021-06-28 09:20:44 -0400388 auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(aaType, quadCount);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500389 if (quadCount > GrQuadPerEdgeAA::QuadLimit(indexBufferOption)) {
Robert Phillips438d9862019-11-14 12:46:05 -0500390 // Promoting to the new aaType would've caused an overflow of the indexBuffer
391 // limit
392 return false;
Michael Ludwig69858532018-11-28 15:34:34 -0500393 }
Robert Phillips438d9862019-11-14 12:46:05 -0500394
395 // Original quad was downgraded to non-aa, lift back up to this quad's required type
396 SkASSERT(fHelper.aaType() == GrAAType::kNone);
397 fHelper.setAAType(aaType);
398 } else {
399 auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(fHelper.aaType(),
Brian Salomonb8c4add2021-06-28 09:20:44 -0400400 quadCount);
Michael Ludwig949ceb22020-02-07 10:14:45 -0500401 if (quadCount > GrQuadPerEdgeAA::QuadLimit(indexBufferOption)) {
Robert Phillips438d9862019-11-14 12:46:05 -0500402 return false; // This op can't grow any more
403 }
Michael Ludwig69858532018-11-28 15:34:34 -0500404 }
405
Michael Ludwig949ceb22020-02-07 10:14:45 -0500406 return true;
407 }
408
409 // Similar to onCombineIfPossible, but adds a quad assuming its op would have been compatible.
410 // But since it's avoiding the op list management, it must update the op's bounds.
Brian Salomonb8c4add2021-06-28 09:20:44 -0400411 bool addQuad(DrawQuad* quad, const SkPMColor4f& color, GrAAType aaType) {
Michael Ludwig69858532018-11-28 15:34:34 -0500412 SkRect newBounds = this->bounds();
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500413 newBounds.joinPossiblyEmptyRect(quad->fDevice.bounds());
Michael Ludwig949ceb22020-02-07 10:14:45 -0500414
415 DrawQuad extra;
Michael Ludwig465864c2020-02-10 09:30:04 -0500416 int count = quad->fEdgeFlags != GrQuadAAFlags::kNone ? GrQuadUtils::ClipToW0(quad, &extra)
417 : 1;
Michael Ludwig949ceb22020-02-07 10:14:45 -0500418 if (count == 0 ) {
419 // Just skip the append (trivial success)
420 return true;
Brian Salomonb8c4add2021-06-28 09:20:44 -0400421 } else if (!this->canAddQuads(count, aaType)) {
Michael Ludwig949ceb22020-02-07 10:14:45 -0500422 // Not enough room in the index buffer for the AA type
423 return false;
424 } else {
425 // Can actually add the 1 or 2 quads representing the draw
426 fQuads.append(quad->fDevice, { color, quad->fEdgeFlags },
427 fHelper.isTrivial() ? nullptr : &quad->fLocal);
428 if (count > 1) {
429 fQuads.append(extra.fDevice, { color, extra.fEdgeFlags },
430 fHelper.isTrivial() ? nullptr : &extra.fLocal);
431 }
432 // Update the bounds
433 this->setBounds(newBounds, HasAABloat(fHelper.aaType() == GrAAType::kCoverage),
434 IsHairline::kNo);
435 return true;
436 }
Michael Ludwigc96fc372019-01-08 15:46:15 -0500437 }
438
439 struct ColorAndAA {
440 SkPMColor4f fColor;
441 GrQuadAAFlags fAAFlags;
442 };
Michael Ludwig69858532018-11-28 15:34:34 -0500443
444 Helper fHelper;
Michael Ludwig425eb452019-06-27 10:13:27 -0400445 GrQuadBuffer<ColorAndAA> fQuads;
Robert Phillips93209052019-12-03 15:42:35 -0500446 char* fPrePreparedVertices = nullptr;
Michael Ludwig69858532018-11-28 15:34:34 -0500447
Robert Phillips50d7d6f2020-03-04 11:12:24 -0500448 GrProgramInfo* fProgramInfo = nullptr;
449 ColorType fColorType;
Michael Ludwig69858532018-11-28 15:34:34 -0500450
Chris Daltondbb833b2020-03-17 12:15:46 -0600451 sk_sp<const GrBuffer> fVertexBuffer;
452 sk_sp<const GrBuffer> fIndexBuffer;
453 int fBaseVertex;
454
John Stiles7571f9e2020-09-02 22:42:33 -0400455 using INHERITED = GrMeshDrawOp;
Michael Ludwig69858532018-11-28 15:34:34 -0500456};
457
458} // anonymous namespace
459
Herb Derbyc76d4092020-10-07 16:46:15 -0400460GrOp::Owner GrFillRectOp::Make(GrRecordingContext* context,
461 GrPaint&& paint,
462 GrAAType aaType,
463 DrawQuad* quad,
464 const GrUserStencilSettings* stencil,
465 InputFlags inputFlags) {
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700466 return FillRectOp::Make(context, std::move(paint), aaType, std::move(quad), stencil,
467 inputFlags);
Michael Ludwigd9f917b2019-05-24 12:57:55 -0400468}
469
Herb Derbyc76d4092020-10-07 16:46:15 -0400470GrOp::Owner GrFillRectOp::MakeNonAARect(GrRecordingContext* context,
471 GrPaint&& paint,
472 const SkMatrix& view,
473 const SkRect& rect,
474 const GrUserStencilSettings* stencil) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500475 DrawQuad quad{GrQuad::MakeFromRect(rect, view), GrQuad(rect), GrQuadAAFlags::kNone};
Chris Daltonc3b67eb2020-02-10 21:09:58 -0700476 return FillRectOp::Make(context, std::move(paint), GrAAType::kNone, &quad, stencil,
477 InputFlags::kNone);
Michael Ludwig009b92e2019-02-15 16:03:53 -0500478}
479
Herb Derbyc76d4092020-10-07 16:46:15 -0400480GrOp::Owner GrFillRectOp::MakeOp(GrRecordingContext* context,
481 GrPaint&& paint,
482 GrAAType aaType,
483 const SkMatrix& viewMatrix,
Robert Phillipse40495d2021-07-20 09:40:13 -0400484 const GrQuadSetEntry quads[],
Herb Derbyc76d4092020-10-07 16:46:15 -0400485 int cnt,
486 const GrUserStencilSettings* stencilSettings,
487 int* numConsumed) {
Michael Ludwig69858532018-11-28 15:34:34 -0500488 // First make a draw op for the first quad in the set
489 SkASSERT(cnt > 0);
Michael Ludwig69858532018-11-28 15:34:34 -0500490
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500491 DrawQuad quad{GrQuad::MakeFromRect(quads[0].fRect, viewMatrix),
492 GrQuad::MakeFromRect(quads[0].fRect, quads[0].fLocalMatrix),
493 quads[0].fAAFlags};
Michael Ludwig69858532018-11-28 15:34:34 -0500494 paint.setColor4f(quads[0].fColor);
Herb Derbyc76d4092020-10-07 16:46:15 -0400495 GrOp::Owner op = FillRectOp::Make(context, std::move(paint), aaType,
496 &quad, stencilSettings, InputFlags::kNone);
Robert Phillips438d9862019-11-14 12:46:05 -0500497 FillRectOp* fillRects = op->cast<FillRectOp>();
Michael Ludwig69858532018-11-28 15:34:34 -0500498
Robert Phillips438d9862019-11-14 12:46:05 -0500499 *numConsumed = 1;
Michael Ludwig69858532018-11-28 15:34:34 -0500500 // Accumulate remaining quads similar to onCombineIfPossible() without creating an op
501 for (int i = 1; i < cnt; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500502 quad = {GrQuad::MakeFromRect(quads[i].fRect, viewMatrix),
503 GrQuad::MakeFromRect(quads[i].fRect, quads[i].fLocalMatrix),
504 quads[i].fAAFlags};
Michael Ludwig69858532018-11-28 15:34:34 -0500505
506 GrAAType resolvedAA;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500507 GrQuadUtils::ResolveAAType(aaType, quads[i].fAAFlags, quad.fDevice,
508 &resolvedAA, &quad.fEdgeFlags);
Michael Ludwig69858532018-11-28 15:34:34 -0500509
Brian Salomonb8c4add2021-06-28 09:20:44 -0400510 if (!fillRects->addQuad(&quad, quads[i].fColor, resolvedAA)) {
Robert Phillips438d9862019-11-14 12:46:05 -0500511 break;
512 }
513
514 (*numConsumed)++;
Michael Ludwig69858532018-11-28 15:34:34 -0500515 }
516
517 return op;
518}
519
Brian Salomoneebe7352020-12-09 16:37:04 -0500520void GrFillRectOp::AddFillRectOps(GrSurfaceDrawContext* rtc,
Michael Ludwig7c12e282020-05-29 09:54:07 -0400521 const GrClip* clip,
Robert Phillips438d9862019-11-14 12:46:05 -0500522 GrRecordingContext* context,
523 GrPaint&& paint,
524 GrAAType aaType,
525 const SkMatrix& viewMatrix,
Robert Phillipse40495d2021-07-20 09:40:13 -0400526 const GrQuadSetEntry quads[],
Robert Phillips438d9862019-11-14 12:46:05 -0500527 int cnt,
528 const GrUserStencilSettings* stencilSettings) {
529
530 int offset = 0;
531 int numLeft = cnt;
532 while (numLeft) {
533 int numConsumed = 0;
534
Herb Derbyc76d4092020-10-07 16:46:15 -0400535 GrOp::Owner op = MakeOp(context, GrPaint::Clone(paint), aaType, viewMatrix,
536 &quads[offset], numLeft, stencilSettings,
537 &numConsumed);
Robert Phillips438d9862019-11-14 12:46:05 -0500538
539 offset += numConsumed;
540 numLeft -= numConsumed;
541
542 rtc->addDrawOp(clip, std::move(op));
543 }
544
545 SkASSERT(offset == cnt);
546}
Michael Ludwig69858532018-11-28 15:34:34 -0500547
548#if GR_TEST_UTILS
549
Robert Phillips438d9862019-11-14 12:46:05 -0500550uint32_t GrFillRectOp::ClassID() {
551 return FillRectOp::ClassID();
552}
553
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500554#include "src/gpu/GrDrawOpTest.h"
555#include "src/gpu/SkGr.h"
Michael Ludwig69858532018-11-28 15:34:34 -0500556
557GR_DRAW_OP_TEST_DEFINE(FillRectOp) {
558 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
559 SkRect rect = GrTest::TestRect(random);
560
561 GrAAType aaType = GrAAType::kNone;
562 if (random->nextBool()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600563 aaType = (numSamples > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Michael Ludwig69858532018-11-28 15:34:34 -0500564 }
565 const GrUserStencilSettings* stencil = random->nextBool() ? nullptr
566 : GrGetRandomStencil(random, context);
567
568 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
569 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
570 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
571 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
572 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
573
574 if (random->nextBool()) {
575 if (random->nextBool()) {
Robert Phillips438d9862019-11-14 12:46:05 -0500576 // Single local matrix
577 SkMatrix localMatrix = GrTest::TestMatrixInvertible(random);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500578 DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix),
579 GrQuad::MakeFromRect(rect, localMatrix), aaFlags};
580 return GrFillRectOp::Make(context, std::move(paint), aaType, &quad, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500581 } else {
582 // Pass local rect directly
583 SkRect localRect = GrTest::TestRect(random);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500584 DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix),
585 GrQuad(localRect), aaFlags};
586 return GrFillRectOp::Make(context, std::move(paint), aaType, &quad, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500587 }
588 } else {
589 // The simplest constructor
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500590 DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix), GrQuad(rect), aaFlags};
591 return GrFillRectOp::Make(context, std::move(paint), aaType, &quad, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500592 }
593}
594
595#endif