blob: 94142a68b015b1b8adca5cb5b034a3b02017fdd3 [file] [log] [blame]
Michael Ludwig69858532018-11-28 15:34:34 -05001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/ops/GrFillRectOp.h"
Michael Ludwig69858532018-11-28 15:34:34 -05009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkMatrix.h"
11#include "include/core/SkRect.h"
12#include "src/gpu/GrCaps.h"
13#include "src/gpu/GrGeometryProcessor.h"
14#include "src/gpu/GrPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/SkGr.h"
Michael Ludwigfd4f4df2019-05-29 09:51:09 -040016#include "src/gpu/geometry/GrQuad.h"
Michael Ludwig425eb452019-06-27 10:13:27 -040017#include "src/gpu/geometry/GrQuadBuffer.h"
Michael Ludwig0f809022019-06-04 09:14:37 -040018#include "src/gpu/geometry/GrQuadUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h"
20#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
21#include "src/gpu/glsl/GrGLSLVarying.h"
22#include "src/gpu/ops/GrMeshDrawOp.h"
23#include "src/gpu/ops/GrQuadPerEdgeAA.h"
24#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
Michael Ludwig69858532018-11-28 15:34:34 -050025
26namespace {
27
28using VertexSpec = GrQuadPerEdgeAA::VertexSpec;
29using ColorType = GrQuadPerEdgeAA::ColorType;
30
Michael Ludwigc96fc372019-01-08 15:46:15 -050031#ifdef SK_DEBUG
Michael Ludwigde4c58c2019-06-04 09:12:59 -040032static SkString dump_quad_info(int index, const GrQuad& deviceQuad,
33 const GrQuad& localQuad, const SkPMColor4f& color,
Michael Ludwigc96fc372019-01-08 15:46:15 -050034 GrQuadAAFlags aaFlags) {
35 SkString str;
36 str.appendf("%d: Color: [%.2f, %.2f, %.2f, %.2f], Edge AA: l%u_t%u_r%u_b%u, \n"
37 " device quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), "
38 "(%.2f, %.2f, %.2f)],\n"
39 " local quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), "
40 "(%.2f, %.2f, %.2f)]\n",
41 index, color.fR, color.fG, color.fB, color.fA,
42 (uint32_t) (aaFlags & GrQuadAAFlags::kLeft),
43 (uint32_t) (aaFlags & GrQuadAAFlags::kTop),
44 (uint32_t) (aaFlags & GrQuadAAFlags::kRight),
45 (uint32_t) (aaFlags & GrQuadAAFlags::kBottom),
46 deviceQuad.x(0), deviceQuad.y(0), deviceQuad.w(0),
47 deviceQuad.x(1), deviceQuad.y(1), deviceQuad.w(1),
48 deviceQuad.x(2), deviceQuad.y(2), deviceQuad.w(2),
49 deviceQuad.x(3), deviceQuad.y(3), deviceQuad.w(3),
50 localQuad.x(0), localQuad.y(0), localQuad.w(0),
51 localQuad.x(1), localQuad.y(1), localQuad.w(1),
52 localQuad.x(2), localQuad.y(2), localQuad.w(2),
53 localQuad.x(3), localQuad.y(3), localQuad.w(3));
54 return str;
55}
56#endif
Michael Ludwig69858532018-11-28 15:34:34 -050057
Michael Ludwig69858532018-11-28 15:34:34 -050058class FillRectOp final : public GrMeshDrawOp {
59private:
60 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
61
62public:
Robert Phillipsb97da532019-02-12 15:24:12 -050063 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Michael Ludwig69858532018-11-28 15:34:34 -050064 GrPaint&& paint,
65 GrAAType aaType,
66 GrQuadAAFlags edgeAA,
67 const GrUserStencilSettings* stencilSettings,
Michael Ludwigde4c58c2019-06-04 09:12:59 -040068 const GrQuad& deviceQuad,
69 const GrQuad& localQuad) {
Michael Ludwig69858532018-11-28 15:34:34 -050070 // Clean up deviations between aaType and edgeAA
Michael Ludwig0f809022019-06-04 09:14:37 -040071 GrQuadUtils::ResolveAAType(aaType, edgeAA, deviceQuad, &aaType, &edgeAA);
Michael Ludwigdcd48212019-01-08 15:28:57 -050072 return Helper::FactoryHelper<FillRectOp>(context, std::move(paint), aaType, edgeAA,
Michael Ludwig41f395d2019-05-23 13:59:45 -040073 stencilSettings, deviceQuad, localQuad);
Michael Ludwig69858532018-11-28 15:34:34 -050074 }
75
Michael Ludwigdcd48212019-01-08 15:28:57 -050076 // aaType is passed to Helper in the initializer list, so incongruities between aaType and
77 // edgeFlags must be resolved prior to calling this constructor.
78 FillRectOp(Helper::MakeArgs args, SkPMColor4f paintColor, GrAAType aaType,
79 GrQuadAAFlags edgeFlags, const GrUserStencilSettings* stencil,
Michael Ludwigde4c58c2019-06-04 09:12:59 -040080 const GrQuad& deviceQuad, const GrQuad& localQuad)
Michael Ludwig69858532018-11-28 15:34:34 -050081 : INHERITED(ClassID())
Michael Ludwig425eb452019-06-27 10:13:27 -040082 , fHelper(args, aaType, stencil)
83 , fQuads(1, !fHelper.isTrivial()) {
84 // Conservatively keep track of the local coordinates; it may be that the paint doesn't
85 // need them after analysis is finished. If the paint is known to be solid up front they
86 // can be skipped entirely.
87 fQuads.append(deviceQuad, { paintColor, edgeFlags },
88 fHelper.isTrivial() ? nullptr : &localQuad);
Michael Ludwig41f395d2019-05-23 13:59:45 -040089 this->setBounds(deviceQuad.bounds(), HasAABloat(aaType == GrAAType::kCoverage),
Greg Daniel5faf4742019-10-01 15:14:44 -040090 IsHairline::kNo);
Michael Ludwig69858532018-11-28 15:34:34 -050091 }
92
93 const char* name() const override { return "FillRectOp"; }
94
Chris Dalton1706cbf2019-05-21 19:35:29 -060095 void visitProxies(const VisitProxyFunc& func) const override {
Michael Ludwig69858532018-11-28 15:34:34 -050096 return fHelper.visitProxies(func);
97 }
98
99#ifdef SK_DEBUG
100 SkString dumpInfo() const override {
101 SkString str;
Michael Ludwig425eb452019-06-27 10:13:27 -0400102 str.appendf("# draws: %u\n", fQuads.count());
Michael Ludwig69858532018-11-28 15:34:34 -0500103 str.appendf("Device quad type: %u, local quad type: %u\n",
Michael Ludwig425eb452019-06-27 10:13:27 -0400104 (uint32_t) fQuads.deviceQuadType(), (uint32_t) fQuads.localQuadType());
Michael Ludwig69858532018-11-28 15:34:34 -0500105 str += fHelper.dumpInfo();
Michael Ludwig425eb452019-06-27 10:13:27 -0400106 int i = 0;
107 auto iter = fQuads.iterator();
108 while(iter.next()) {
109 const ColorAndAA& info = iter.metadata();
110 str += dump_quad_info(i, iter.deviceQuad(), iter.localQuad(),
111 info.fColor, info.fAAFlags);
112 i++;
Michael Ludwig69858532018-11-28 15:34:34 -0500113 }
114 str += INHERITED::dumpInfo();
115 return str;
116 }
117#endif
118
Chris Dalton6ce447a2019-06-23 18:07:38 -0600119 GrProcessorSet::Analysis finalize(
120 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
121 GrClampType clampType) override {
Michael Ludwig69858532018-11-28 15:34:34 -0500122 // Initialize aggregate color analysis with the first quad's color (which always exists)
Michael Ludwig425eb452019-06-27 10:13:27 -0400123 auto iter = fQuads.metadata();
124 SkAssertResult(iter.next());
125 GrProcessorAnalysisColor quadColors(iter->fColor);
Michael Ludwig69858532018-11-28 15:34:34 -0500126 // Then combine the colors of any additional quads (e.g. from MakeSet)
Michael Ludwig425eb452019-06-27 10:13:27 -0400127 while(iter.next()) {
128 quadColors = GrProcessorAnalysisColor::Combine(quadColors, iter->fColor);
Michael Ludwigca91e1f2018-12-10 10:44:44 -0500129 if (quadColors.isUnknown()) {
130 // No point in accumulating additional starting colors, combining cannot make it
131 // less unknown.
132 break;
133 }
Michael Ludwig69858532018-11-28 15:34:34 -0500134 }
Michael Ludwig72ab3462018-12-10 12:43:36 -0500135
136 // If the AA type is coverage, it will be a single value per pixel; if it's not coverage AA
137 // then the coverage is always 1.0, so specify kNone for more optimal blending.
138 GrProcessorAnalysisCoverage coverage = fHelper.aaType() == GrAAType::kCoverage ?
139 GrProcessorAnalysisCoverage::kSingleChannel :
140 GrProcessorAnalysisCoverage::kNone;
Brian Osman5ced0bf2019-03-15 10:15:29 -0400141 auto result = fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600142 caps, clip, hasMixedSampledCoverage, 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 Osman8fa7ab42019-03-18 10:22:42 -0400148 fColorType = GrQuadPerEdgeAA::MinColorType(colorOverride, clampType, caps);
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 Osman8fa7ab42019-03-18 10:22:42 -0400156 fColorType = SkTMax(fColorType,
Michael Ludwig425eb452019-06-27 10:13:27 -0400157 GrQuadPerEdgeAA::MinColorType(iter->fColor, clampType, caps));
Brian Osman8fa7ab42019-03-18 10:22:42 -0400158 }
Michael Ludwig69858532018-11-28 15:34:34 -0500159 }
Brian Salomon41f9c3c2019-03-25 11:06:12 -0400160 // Most SkShaders' FPs multiply their calculated color by the paint color or alpha. We want
161 // to use ColorType::kNone to optimize out that multiply. However, if there are no color
162 // FPs then were really writing a special shader for white rectangles and not saving any
163 // multiples. So in that case use bytes to avoid the extra shader (and possibly work around
164 // an ANGLE issue: crbug.com/942565).
165 if (fColorType == ColorType::kNone && !result.hasColorFragmentProcessor()) {
166 fColorType = ColorType::kByte;
167 }
Michael Ludwig69858532018-11-28 15:34:34 -0500168
169 return result;
170 }
171
172 FixedFunctionFlags fixedFunctionFlags() const override {
173 // Since the AA type of the whole primitive is kept consistent with the per edge AA flags
174 // the helper's fixed function flags are appropriate.
175 return fHelper.fixedFunctionFlags();
176 }
177
178 DEFINE_OP_CLASS_ID
179
180private:
Robert Phillips438d9862019-11-14 12:46:05 -0500181 friend class ::GrFillRectOp; // for access to addQuad
182
183#if GR_TEST_UTILS
184 int numQuads() const final { return fQuads.count(); }
185#endif
Michael Ludwig69858532018-11-28 15:34:34 -0500186
Michael Ludwigc96fc372019-01-08 15:46:15 -0500187 void onPrepareDraws(Target* target) override {
Brian Salomon5f394272019-07-02 14:07:49 -0400188 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Michael Ludwig69858532018-11-28 15:34:34 -0500189
190 using Domain = GrQuadPerEdgeAA::Domain;
191 static constexpr SkRect kEmptyDomain = SkRect::MakeEmpty();
192
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400193 auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(fHelper.aaType(),
194 fQuads.count());
195
Michael Ludwig425eb452019-06-27 10:13:27 -0400196 VertexSpec vertexSpec(fQuads.deviceQuadType(), fColorType, fQuads.localQuadType(),
Brian Salomon1d835422019-03-13 16:11:44 -0400197 fHelper.usesLocalCoords(), Domain::kNo, fHelper.aaType(),
Robert Phillipsc554dcf2019-10-28 11:43:55 -0400198 fHelper.compatibleWithCoverageAsAlpha(), indexBufferOption);
Michael Ludwigdcd48212019-01-08 15:28:57 -0500199 // Make sure that if the op thought it was a solid color, the vertex spec does not use
200 // local coords.
201 SkASSERT(!fHelper.isTrivial() || !fHelper.usesLocalCoords());
Michael Ludwig69858532018-11-28 15:34:34 -0500202
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500203 GrGeometryProcessor* gp = GrQuadPerEdgeAA::MakeProcessor(target->allocator(), vertexSpec);
Michael Ludwig69858532018-11-28 15:34:34 -0500204 size_t vertexSize = gp->vertexStride();
205
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400206 sk_sp<const GrBuffer> vertexBuffer;
Michael Ludwig69858532018-11-28 15:34:34 -0500207 int vertexOffsetInBuffer = 0;
208
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400209 const int totalNumVertices = fQuads.count() * vertexSpec.verticesPerQuad();
210
Michael Ludwig69858532018-11-28 15:34:34 -0500211 // Fill the allocated vertex data
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400212 void* vdata = target->makeVertexSpace(vertexSize, totalNumVertices,
213 &vertexBuffer, &vertexOffsetInBuffer);
Michael Ludwig69858532018-11-28 15:34:34 -0500214 if (!vdata) {
215 SkDebugf("Could not allocate vertices\n");
216 return;
217 }
218
219 // vertices pointer advances through vdata based on Tessellate's return value
220 void* vertices = vdata;
Michael Ludwig73dbea62019-11-19 14:55:36 -0500221 GrQuadPerEdgeAA::Tessellator tessellator(vertexSpec);
Michael Ludwig425eb452019-06-27 10:13:27 -0400222 auto iter = fQuads.iterator();
223 while(iter.next()) {
224 // All entries should have local coords, or no entries should have local coords,
225 // matching !helper.isTrivial() (which is more conservative than helper.usesLocalCoords)
226 SkASSERT(iter.isLocalValid() != fHelper.isTrivial());
227 auto info = iter.metadata();
Michael Ludwig73dbea62019-11-19 14:55:36 -0500228 vertices = tessellator.append(vertices, iter.deviceQuad(), iter.localQuad(),
229 info.fColor, kEmptyDomain, info.fAAFlags);
Michael Ludwig69858532018-11-28 15:34:34 -0500230 }
231
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400232 sk_sp<const GrBuffer> indexBuffer;
233 if (vertexSpec.needsIndexBuffer()) {
234 indexBuffer = GrQuadPerEdgeAA::GetIndexBuffer(target, vertexSpec.indexBufferOption());
235 if (!indexBuffer) {
236 SkDebugf("Could not allocate indices\n");
237 return;
238 }
239 }
240
Michael Ludwig69858532018-11-28 15:34:34 -0500241 // Configure the mesh for the vertex data
Michael Ludwig93aeba02018-12-21 09:50:31 -0500242 GrMesh* mesh = target->allocMeshes(1);
Robert Phillipsfd0c3b52019-11-01 08:44:42 -0400243 GrQuadPerEdgeAA::ConfigureMesh(mesh, vertexSpec, 0, fQuads.count(), totalNumVertices,
244 std::move(vertexBuffer), std::move(indexBuffer),
245 vertexOffsetInBuffer);
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500246 target->recordDraw(gp, mesh, 1, vertexSpec.primitiveType());
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700247 }
Michael Ludwig69858532018-11-28 15:34:34 -0500248
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700249 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
250 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
251 }
Michael Ludwig69858532018-11-28 15:34:34 -0500252
253 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomon5f394272019-07-02 14:07:49 -0400254 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
Michael Ludwig69858532018-11-28 15:34:34 -0500255 const auto* that = t->cast<FillRectOp>();
256
Robert Phillipsb69001f2019-10-29 12:16:35 -0400257 bool upgradeToCoverageAAOnMerge = false;
258 if (fHelper.aaType() != that->fHelper.aaType()) {
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400259 if (!CanUpgradeAAOnMerge(fHelper.aaType(), that->fHelper.aaType())) {
Robert Phillipsb69001f2019-10-29 12:16:35 -0400260 return CombineResult::kCannotCombine;
261 }
262 upgradeToCoverageAAOnMerge = true;
263 }
264
Robert Phillipsbbd459d2019-10-29 14:40:03 -0400265 if (CombinedQuadCountWillOverflow(fHelper.aaType(), upgradeToCoverageAAOnMerge,
266 fQuads.count() + that->fQuads.count())) {
267 return CombineResult::kCannotCombine;
Michael Ludwig93aeba02018-12-21 09:50:31 -0500268 }
269
Michael Ludwigc96fc372019-01-08 15:46:15 -0500270 // Unlike most users of the draw op helper, this op can merge none-aa and coverage-aa draw
271 // ops together, so pass true as the last argument.
Michael Ludwig69858532018-11-28 15:34:34 -0500272 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds(), true)) {
273 return CombineResult::kCannotCombine;
274 }
275
Michael Ludwigdcd48212019-01-08 15:28:57 -0500276 // If the paints were compatible, the trivial/solid-color state should be the same
277 SkASSERT(fHelper.isTrivial() == that->fHelper.isTrivial());
Michael Ludwig69858532018-11-28 15:34:34 -0500278
Michael Ludwigdcd48212019-01-08 15:28:57 -0500279 // If the processor sets are compatible, the two ops are always compatible; it just needs to
280 // adjust the state of the op to be the more general quad and aa types of the two ops and
281 // then concatenate the per-quad data.
Brian Salomon1d835422019-03-13 16:11:44 -0400282 fColorType = SkTMax(fColorType, that->fColorType);
Michael Ludwig69858532018-11-28 15:34:34 -0500283
284 // The helper stores the aa type, but isCompatible(with true arg) allows the two ops' aa
285 // types to be none and coverage, in which case this op's aa type must be lifted to coverage
286 // 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 -0400287 if (upgradeToCoverageAAOnMerge) {
Michael Ludwig69858532018-11-28 15:34:34 -0500288 fHelper.setAAType(GrAAType::kCoverage);
289 }
290
Michael Ludwig425eb452019-06-27 10:13:27 -0400291 fQuads.concat(that->fQuads);
Michael Ludwig69858532018-11-28 15:34:34 -0500292 return CombineResult::kMerged;
293 }
294
295 // Similar to onCombineIfPossible, but adds a quad assuming its op would have been compatible.
296 // But since it's avoiding the op list management, it must update the op's bounds. This is only
297 // used with quad sets, which uses the same view matrix for each quad so this assumes that the
298 // device quad type of the new quad is the same as the op's.
Robert Phillips438d9862019-11-14 12:46:05 -0500299 bool addQuad(const GrQuad& deviceQuad, const GrQuad& localQuad,
Michael Ludwig41f395d2019-05-23 13:59:45 -0400300 const SkPMColor4f& color, GrQuadAAFlags edgeAA, GrAAType aaType) {
Michael Ludwig69858532018-11-28 15:34:34 -0500301 // The new quad's aa type should be the same as the first quad's or none, except when the
302 // first quad's aa type was already downgraded to none, in which case the stored type must
303 // be lifted to back to the requested type.
Robert Phillips438d9862019-11-14 12:46:05 -0500304 if (aaType != fHelper.aaType() && aaType != GrAAType::kNone) {
305 auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(aaType,
306 fQuads.count()+1);
307 if (fQuads.count()+1 > GrQuadPerEdgeAA::QuadLimit(indexBufferOption)) {
308 // Promoting to the new aaType would've caused an overflow of the indexBuffer
309 // limit
310 return false;
Michael Ludwig69858532018-11-28 15:34:34 -0500311 }
Robert Phillips438d9862019-11-14 12:46:05 -0500312
313 // Original quad was downgraded to non-aa, lift back up to this quad's required type
314 SkASSERT(fHelper.aaType() == GrAAType::kNone);
315 fHelper.setAAType(aaType);
316 } else {
317 auto indexBufferOption = GrQuadPerEdgeAA::CalcIndexBufferOption(fHelper.aaType(),
318 fQuads.count()+1);
319 if (fQuads.count()+1 > GrQuadPerEdgeAA::QuadLimit(indexBufferOption)) {
320 return false; // This op can't grow any more
321 }
Michael Ludwig69858532018-11-28 15:34:34 -0500322 }
323
Michael Ludwig69858532018-11-28 15:34:34 -0500324 // Update the bounds and add the quad to this op's storage
325 SkRect newBounds = this->bounds();
Michael Ludwig41f395d2019-05-23 13:59:45 -0400326 newBounds.joinPossiblyEmptyRect(deviceQuad.bounds());
Michael Ludwig69858532018-11-28 15:34:34 -0500327 this->setBounds(newBounds, HasAABloat(fHelper.aaType() == GrAAType::kCoverage),
Greg Daniel5faf4742019-10-01 15:14:44 -0400328 IsHairline::kNo);
Michael Ludwig425eb452019-06-27 10:13:27 -0400329 fQuads.append(deviceQuad, { color, edgeAA }, fHelper.isTrivial() ? nullptr : &localQuad);
Robert Phillips438d9862019-11-14 12:46:05 -0500330 return true;
Michael Ludwigc96fc372019-01-08 15:46:15 -0500331 }
332
333 struct ColorAndAA {
334 SkPMColor4f fColor;
335 GrQuadAAFlags fAAFlags;
336 };
Michael Ludwig69858532018-11-28 15:34:34 -0500337
338 Helper fHelper;
Michael Ludwig425eb452019-06-27 10:13:27 -0400339 GrQuadBuffer<ColorAndAA> fQuads;
Michael Ludwig69858532018-11-28 15:34:34 -0500340
Brian Salomon1d835422019-03-13 16:11:44 -0400341 ColorType fColorType;
Michael Ludwig69858532018-11-28 15:34:34 -0500342
Michael Ludwig69858532018-11-28 15:34:34 -0500343 typedef GrMeshDrawOp INHERITED;
344};
345
346} // anonymous namespace
347
Robert Phillips438d9862019-11-14 12:46:05 -0500348std::unique_ptr<GrDrawOp> GrFillRectOp::Make(GrRecordingContext* context,
349 GrPaint&& paint,
350 GrAAType aaType,
351 GrQuadAAFlags aaFlags,
352 const GrQuad& deviceQuad,
353 const GrQuad& localQuad,
354 const GrUserStencilSettings* stencil) {
Michael Ludwigd9f917b2019-05-24 12:57:55 -0400355 return FillRectOp::Make(context, std::move(paint), aaType, aaFlags, stencil,
356 deviceQuad, localQuad);
357}
358
Robert Phillips438d9862019-11-14 12:46:05 -0500359std::unique_ptr<GrDrawOp> GrFillRectOp::MakeNonAARect(GrRecordingContext* context,
360 GrPaint&& paint,
361 const SkMatrix& view,
362 const SkRect& rect,
363 const GrUserStencilSettings* stencil) {
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400364 return FillRectOp::Make(context, std::move(paint), GrAAType::kNone, GrQuadAAFlags::kNone,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400365 stencil, GrQuad::MakeFromRect(rect, view), GrQuad(rect));
Michael Ludwig009b92e2019-02-15 16:03:53 -0500366}
367
Robert Phillips438d9862019-11-14 12:46:05 -0500368std::unique_ptr<GrDrawOp> GrFillRectOp::MakeOp(GrRecordingContext* context,
369 GrPaint&& paint,
370 GrAAType aaType,
371 const SkMatrix& viewMatrix,
372 const GrRenderTargetContext::QuadSetEntry quads[],
373 int cnt,
374 const GrUserStencilSettings* stencilSettings,
375 int* numConsumed) {
Michael Ludwig69858532018-11-28 15:34:34 -0500376 // First make a draw op for the first quad in the set
377 SkASSERT(cnt > 0);
Michael Ludwig69858532018-11-28 15:34:34 -0500378
379 paint.setColor4f(quads[0].fColor);
Robert Phillips438d9862019-11-14 12:46:05 -0500380 std::unique_ptr<GrDrawOp> op = FillRectOp::Make(
381 context, std::move(paint), aaType,
Michael Ludwige9c57d32019-02-13 13:39:39 -0500382 quads[0].fAAFlags, stencilSettings,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400383 GrQuad::MakeFromRect(quads[0].fRect, viewMatrix),
384 GrQuad::MakeFromRect(quads[0].fRect, quads[0].fLocalMatrix));
Robert Phillips438d9862019-11-14 12:46:05 -0500385 FillRectOp* fillRects = op->cast<FillRectOp>();
Michael Ludwig69858532018-11-28 15:34:34 -0500386
Robert Phillips438d9862019-11-14 12:46:05 -0500387 *numConsumed = 1;
Michael Ludwig69858532018-11-28 15:34:34 -0500388 // Accumulate remaining quads similar to onCombineIfPossible() without creating an op
389 for (int i = 1; i < cnt; ++i) {
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400390 GrQuad deviceQuad = GrQuad::MakeFromRect(quads[i].fRect, viewMatrix);
Michael Ludwig69858532018-11-28 15:34:34 -0500391
392 GrAAType resolvedAA;
393 GrQuadAAFlags resolvedEdgeFlags;
Michael Ludwig0f809022019-06-04 09:14:37 -0400394 GrQuadUtils::ResolveAAType(aaType, quads[i].fAAFlags, deviceQuad,
395 &resolvedAA, &resolvedEdgeFlags);
Michael Ludwig69858532018-11-28 15:34:34 -0500396
Robert Phillips438d9862019-11-14 12:46:05 -0500397 if (!fillRects->addQuad(deviceQuad,
398 GrQuad::MakeFromRect(quads[i].fRect, quads[i].fLocalMatrix),
399 quads[i].fColor, resolvedEdgeFlags, resolvedAA)) {
400 break;
401 }
402
403 (*numConsumed)++;
Michael Ludwig69858532018-11-28 15:34:34 -0500404 }
405
406 return op;
407}
408
Robert Phillips438d9862019-11-14 12:46:05 -0500409void GrFillRectOp::AddFillRectOps(GrRenderTargetContext* rtc,
410 const GrClip& clip,
411 GrRecordingContext* context,
412 GrPaint&& paint,
413 GrAAType aaType,
414 const SkMatrix& viewMatrix,
415 const GrRenderTargetContext::QuadSetEntry quads[],
416 int cnt,
417 const GrUserStencilSettings* stencilSettings) {
418
419 int offset = 0;
420 int numLeft = cnt;
421 while (numLeft) {
422 int numConsumed = 0;
423
424 std::unique_ptr<GrDrawOp> op = MakeOp(context, GrPaint::Clone(paint), aaType, viewMatrix,
425 &quads[offset], numLeft, stencilSettings,
426 &numConsumed);
427
428 offset += numConsumed;
429 numLeft -= numConsumed;
430
431 rtc->addDrawOp(clip, std::move(op));
432 }
433
434 SkASSERT(offset == cnt);
435}
Michael Ludwig69858532018-11-28 15:34:34 -0500436
437#if GR_TEST_UTILS
438
Robert Phillips438d9862019-11-14 12:46:05 -0500439uint32_t GrFillRectOp::ClassID() {
440 return FillRectOp::ClassID();
441}
442
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500443#include "src/gpu/GrDrawOpTest.h"
444#include "src/gpu/SkGr.h"
Michael Ludwig69858532018-11-28 15:34:34 -0500445
446GR_DRAW_OP_TEST_DEFINE(FillRectOp) {
447 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
448 SkRect rect = GrTest::TestRect(random);
449
450 GrAAType aaType = GrAAType::kNone;
451 if (random->nextBool()) {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600452 aaType = (numSamples > 1) ? GrAAType::kMSAA : GrAAType::kCoverage;
Michael Ludwig69858532018-11-28 15:34:34 -0500453 }
454 const GrUserStencilSettings* stencil = random->nextBool() ? nullptr
455 : GrGetRandomStencil(random, context);
456
457 GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone;
458 aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone;
459 aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone;
460 aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone;
461 aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone;
462
463 if (random->nextBool()) {
464 if (random->nextBool()) {
Robert Phillips438d9862019-11-14 12:46:05 -0500465 // Single local matrix
466 SkMatrix localMatrix = GrTest::TestMatrixInvertible(random);
467 return GrFillRectOp::Make(context, std::move(paint), aaType, aaFlags,
468 GrQuad::MakeFromRect(rect, viewMatrix),
469 GrQuad::MakeFromRect(rect, localMatrix), stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500470 } else {
471 // Pass local rect directly
472 SkRect localRect = GrTest::TestRect(random);
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400473 return GrFillRectOp::Make(context, std::move(paint), aaType, aaFlags,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400474 GrQuad::MakeFromRect(rect, viewMatrix),
475 GrQuad(localRect), stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500476 }
477 } else {
478 // The simplest constructor
Michael Ludwig4a0cf502019-05-30 12:54:09 -0400479 return GrFillRectOp::Make(context, std::move(paint), aaType, aaFlags,
Michael Ludwigde4c58c2019-06-04 09:12:59 -0400480 GrQuad::MakeFromRect(rect, viewMatrix),
481 GrQuad(rect), stencil);
Michael Ludwig69858532018-11-28 15:34:34 -0500482 }
483}
484
485#endif