blob: 3acdb00958c4afe03f1eb15cc08c07e833fbd980 [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
Robert Phillipsfbf02142021-09-01 16:31:34 -04008#include "src/gpu/ops/FillRectOp.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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/SkGr.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040018#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig425eb452019-06-27 10:13:27 -040019#include "src/gpu/geometry/GrQuadBuffer.h"
Michael Ludwig0f809022019-06-04 09:14:37 -040020#include "src/gpu/geometry/GrQuadUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#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"
Robert Phillips4dca8312021-07-28 15:13:20 -040026#include "src/gpu/v1/SurfaceDrawContext_v1.h"
Michael Ludwig69858532018-11-28 15:34:34 -050027
28namespace {
29
30using VertexSpec = GrQuadPerEdgeAA::VertexSpec;
31using ColorType = GrQuadPerEdgeAA::ColorType;
32
John Stiles8d9bf642020-08-12 15:07:45 -040033#if GR_TEST_UTILS
Robert Phillipsfbf02142021-09-01 16:31:34 -040034SkString dump_quad_info(int index, const GrQuad* deviceQuad,
35 const GrQuad* localQuad, const SkPMColor4f& color,
36 GrQuadAAFlags aaFlags) {
Michael Ludwig704d5402019-11-25 09:43:37 -050037 GrQuad safeLocal = localQuad ? *localQuad : GrQuad();
Michael Ludwigc96fc372019-01-08 15:46:15 -050038 SkString str;
39 str.appendf("%d: Color: [%.2f, %.2f, %.2f, %.2f], Edge AA: l%u_t%u_r%u_b%u, \n"
40 " device quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), "
41 "(%.2f, %.2f, %.2f)],\n"
42 " local quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), "
43 "(%.2f, %.2f, %.2f)]\n",
44 index, color.fR, color.fG, color.fB, color.fA,
45 (uint32_t) (aaFlags & GrQuadAAFlags::kLeft),
46 (uint32_t) (aaFlags & GrQuadAAFlags::kTop),
47 (uint32_t) (aaFlags & GrQuadAAFlags::kRight),
48 (uint32_t) (aaFlags & GrQuadAAFlags::kBottom),
Michael Ludwig704d5402019-11-25 09:43:37 -050049 deviceQuad->x(0), deviceQuad->y(0), deviceQuad->w(0),
50 deviceQuad->x(1), deviceQuad->y(1), deviceQuad->w(1),
51 deviceQuad->x(2), deviceQuad->y(2), deviceQuad->w(2),
52 deviceQuad->x(3), deviceQuad->y(3), deviceQuad->w(3),
53 safeLocal.x(0), safeLocal.y(0), safeLocal.w(0),
54 safeLocal.x(1), safeLocal.y(1), safeLocal.w(1),
55 safeLocal.x(2), safeLocal.y(2), safeLocal.w(2),
56 safeLocal.x(3), safeLocal.y(3), safeLocal.w(3));
Michael Ludwigc96fc372019-01-08 15:46:15 -050057 return str;
58}
59#endif
Michael Ludwig69858532018-11-28 15:34:34 -050060
Robert Phillipsfbf02142021-09-01 16:31:34 -040061class FillRectOpImpl final : public GrMeshDrawOp {
Michael Ludwig69858532018-11-28 15:34:34 -050062private:
63 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
64
65public:
Herb Derbyc76d4092020-10-07 16:46:15 -040066 static GrOp::Owner Make(GrRecordingContext* context,
67 GrPaint&& paint,
68 GrAAType aaType,
69 DrawQuad* quad,
70 const GrUserStencilSettings* stencilSettings,
71 Helper::InputFlags inputFlags) {
Michael Ludwig69858532018-11-28 15:34:34 -050072 // Clean up deviations between aaType and edgeAA
Michael Ludwig6b45c5d2020-02-07 09:56:38 -050073 GrQuadUtils::ResolveAAType(aaType, quad->fEdgeFlags, quad->fDevice,
74 &aaType, &quad->fEdgeFlags);
Robert Phillipsfbf02142021-09-01 16:31:34 -040075 return Helper::FactoryHelper<FillRectOpImpl>(context, std::move(paint), aaType, quad,
76 stencilSettings, inputFlags);
Michael Ludwig69858532018-11-28 15:34:34 -050077 }
78
Michael Ludwigdcd48212019-01-08 15:28:57 -050079 // aaType is passed to Helper in the initializer list, so incongruities between aaType and
80 // edgeFlags must be resolved prior to calling this constructor.
Robert Phillipsfbf02142021-09-01 16:31:34 -040081 FillRectOpImpl(GrProcessorSet* processorSet, SkPMColor4f paintColor, GrAAType aaType,
82 DrawQuad* quad, const GrUserStencilSettings* stencil,
83 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 Phillipsfbf02142021-09-01 16:31:34 -0400182 friend class skgpu::v1::FillRectOp; // for access to addQuad
Robert Phillips438d9862019-11-14 12:46:05 -0500183
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
Chris Dalton2a26c502021-08-26 10:05:11 -0600215 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView, usesMSAASurface,
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);
Robert Phillipsfbf02142021-09-01 16:31:34 -0400324 auto that = t->cast<FillRectOpImpl>();
Michael Ludwig69858532018-11-28 15:34:34 -0500325
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
Robert Phillipsfbf02142021-09-01 16:31:34 -0400460namespace skgpu::v1 {
461
462GrOp::Owner FillRectOp::Make(GrRecordingContext* context,
463 GrPaint&& paint,
464 GrAAType aaType,
465 DrawQuad* quad,
466 const GrUserStencilSettings* stencil,
467 InputFlags inputFlags) {
468 return FillRectOpImpl::Make(context, std::move(paint), aaType, std::move(quad), stencil,
469 inputFlags);
470}
471
472GrOp::Owner FillRectOp::MakeNonAARect(GrRecordingContext* context,
473 GrPaint&& paint,
474 const SkMatrix& view,
475 const SkRect& rect,
476 const GrUserStencilSettings* stencil) {
477 DrawQuad quad{GrQuad::MakeFromRect(rect, view), GrQuad(rect), GrQuadAAFlags::kNone};
478 return FillRectOpImpl::Make(context, std::move(paint), GrAAType::kNone, &quad, stencil,
479 InputFlags::kNone);
480}
481
482GrOp::Owner FillRectOp::MakeOp(GrRecordingContext* context,
Herb Derbyc76d4092020-10-07 16:46:15 -0400483 GrPaint&& paint,
484 GrAAType aaType,
Robert Phillipsfbf02142021-09-01 16:31:34 -0400485 const SkMatrix& viewMatrix,
486 const GrQuadSetEntry quads[],
487 int cnt,
488 const GrUserStencilSettings* stencilSettings,
489 int* numConsumed) {
Michael Ludwig69858532018-11-28 15:34:34 -0500490 // First make a draw op for the first quad in the set
491 SkASSERT(cnt > 0);
Michael Ludwig69858532018-11-28 15:34:34 -0500492
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500493 DrawQuad quad{GrQuad::MakeFromRect(quads[0].fRect, viewMatrix),
494 GrQuad::MakeFromRect(quads[0].fRect, quads[0].fLocalMatrix),
495 quads[0].fAAFlags};
Michael Ludwig69858532018-11-28 15:34:34 -0500496 paint.setColor4f(quads[0].fColor);
Herb Derbyc76d4092020-10-07 16:46:15 -0400497 GrOp::Owner op = FillRectOp::Make(context, std::move(paint), aaType,
498 &quad, stencilSettings, InputFlags::kNone);
Robert Phillipsfbf02142021-09-01 16:31:34 -0400499 auto fillRects = op->cast<FillRectOpImpl>();
Michael Ludwig69858532018-11-28 15:34:34 -0500500
Robert Phillips438d9862019-11-14 12:46:05 -0500501 *numConsumed = 1;
Michael Ludwig69858532018-11-28 15:34:34 -0500502 // Accumulate remaining quads similar to onCombineIfPossible() without creating an op
503 for (int i = 1; i < cnt; ++i) {
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500504 quad = {GrQuad::MakeFromRect(quads[i].fRect, viewMatrix),
505 GrQuad::MakeFromRect(quads[i].fRect, quads[i].fLocalMatrix),
506 quads[i].fAAFlags};
Michael Ludwig69858532018-11-28 15:34:34 -0500507
508 GrAAType resolvedAA;
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500509 GrQuadUtils::ResolveAAType(aaType, quads[i].fAAFlags, quad.fDevice,
510 &resolvedAA, &quad.fEdgeFlags);
Michael Ludwig69858532018-11-28 15:34:34 -0500511
Brian Salomonb8c4add2021-06-28 09:20:44 -0400512 if (!fillRects->addQuad(&quad, quads[i].fColor, resolvedAA)) {
Robert Phillips438d9862019-11-14 12:46:05 -0500513 break;
514 }
515
516 (*numConsumed)++;
Michael Ludwig69858532018-11-28 15:34:34 -0500517 }
518
519 return op;
520}
521
Robert Phillipsfbf02142021-09-01 16:31:34 -0400522void FillRectOp::AddFillRectOps(skgpu::v1::SurfaceDrawContext* sdc,
523 const GrClip* clip,
524 GrRecordingContext* context,
525 GrPaint&& paint,
526 GrAAType aaType,
527 const SkMatrix& viewMatrix,
528 const GrQuadSetEntry quads[],
529 int cnt,
530 const GrUserStencilSettings* stencilSettings) {
Robert Phillips438d9862019-11-14 12:46:05 -0500531
532 int offset = 0;
533 int numLeft = cnt;
534 while (numLeft) {
535 int numConsumed = 0;
536
Herb Derbyc76d4092020-10-07 16:46:15 -0400537 GrOp::Owner op = MakeOp(context, GrPaint::Clone(paint), aaType, viewMatrix,
538 &quads[offset], numLeft, stencilSettings,
539 &numConsumed);
Robert Phillips438d9862019-11-14 12:46:05 -0500540
541 offset += numConsumed;
542 numLeft -= numConsumed;
543
Robert Phillips4dca8312021-07-28 15:13:20 -0400544 sdc->addDrawOp(clip, std::move(op));
Robert Phillips438d9862019-11-14 12:46:05 -0500545 }
546
547 SkASSERT(offset == cnt);
548}
Robert Phillipsfbf02142021-09-01 16:31:34 -0400549
550} // namespace skgpu::v1
Michael Ludwig69858532018-11-28 15:34:34 -0500551
552#if GR_TEST_UTILS
553
Robert Phillipsfbf02142021-09-01 16:31:34 -0400554uint32_t skgpu::v1::FillRectOp::ClassID() {
555 return FillRectOpImpl::ClassID();
Robert Phillips438d9862019-11-14 12:46:05 -0500556}
557
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500558#include "src/gpu/GrDrawOpTest.h"
559#include "src/gpu/SkGr.h"
Michael Ludwig69858532018-11-28 15:34:34 -0500560
561GR_DRAW_OP_TEST_DEFINE(FillRectOp) {
562 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
563 SkRect rect = GrTest::TestRect(random);
564
565 GrAAType aaType = GrAAType::kNone;
566 if (random->nextBool()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600567 aaType = (numSamples > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Michael Ludwig69858532018-11-28 15:34:34 -0500568 }
569 const GrUserStencilSettings* stencil = random->nextBool() ? nullptr
570 : GrGetRandomStencil(random, context);
571
572 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
573 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
574 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
575 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
576 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
577
578 if (random->nextBool()) {
579 if (random->nextBool()) {
Robert Phillips438d9862019-11-14 12:46:05 -0500580 // Single local matrix
581 SkMatrix localMatrix = GrTest::TestMatrixInvertible(random);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500582 DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix),
583 GrQuad::MakeFromRect(rect, localMatrix), aaFlags};
Robert Phillipsfbf02142021-09-01 16:31:34 -0400584 return skgpu::v1::FillRectOp::Make(context, std::move(paint), aaType, &quad, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500585 } else {
586 // Pass local rect directly
587 SkRect localRect = GrTest::TestRect(random);
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500588 DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix),
589 GrQuad(localRect), aaFlags};
Robert Phillipsfbf02142021-09-01 16:31:34 -0400590 return skgpu::v1::FillRectOp::Make(context, std::move(paint), aaType, &quad, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500591 }
592 } else {
593 // The simplest constructor
Michael Ludwig6b45c5d2020-02-07 09:56:38 -0500594 DrawQuad quad = {GrQuad::MakeFromRect(rect, viewMatrix), GrQuad(rect), aaFlags};
Robert Phillipsfbf02142021-09-01 16:31:34 -0400595 return skgpu::v1::FillRectOp::Make(context, std::move(paint), aaType, &quad, stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500596 }
597}
598
599#endif