Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 8 | #include "GrFillRectOp.h" |
| 9 | |
Brian Salomon | f419139 | 2019-03-14 15:46:20 -0400 | [diff] [blame] | 10 | #include "GrCaps.h" |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 11 | #include "GrGeometryProcessor.h" |
| 12 | #include "GrMeshDrawOp.h" |
| 13 | #include "GrPaint.h" |
| 14 | #include "GrQuad.h" |
| 15 | #include "GrQuadPerEdgeAA.h" |
| 16 | #include "GrSimpleMeshDrawOpHelper.h" |
| 17 | #include "SkMatrix.h" |
| 18 | #include "SkRect.h" |
| 19 | #include "glsl/GrGLSLColorSpaceXformHelper.h" |
| 20 | #include "glsl/GrGLSLGeometryProcessor.h" |
| 21 | #include "glsl/GrGLSLVarying.h" |
| 22 | |
| 23 | namespace { |
| 24 | |
| 25 | using VertexSpec = GrQuadPerEdgeAA::VertexSpec; |
| 26 | using ColorType = GrQuadPerEdgeAA::ColorType; |
| 27 | |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 28 | #ifdef SK_DEBUG |
| 29 | static SkString dump_quad_info(int index, const GrPerspQuad& deviceQuad, |
| 30 | const GrPerspQuad& localQuad, const SkPMColor4f& color, |
| 31 | GrQuadAAFlags aaFlags) { |
| 32 | SkString str; |
| 33 | str.appendf("%d: Color: [%.2f, %.2f, %.2f, %.2f], Edge AA: l%u_t%u_r%u_b%u, \n" |
| 34 | " device quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), " |
| 35 | "(%.2f, %.2f, %.2f)],\n" |
| 36 | " local quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), " |
| 37 | "(%.2f, %.2f, %.2f)]\n", |
| 38 | index, color.fR, color.fG, color.fB, color.fA, |
| 39 | (uint32_t) (aaFlags & GrQuadAAFlags::kLeft), |
| 40 | (uint32_t) (aaFlags & GrQuadAAFlags::kTop), |
| 41 | (uint32_t) (aaFlags & GrQuadAAFlags::kRight), |
| 42 | (uint32_t) (aaFlags & GrQuadAAFlags::kBottom), |
| 43 | deviceQuad.x(0), deviceQuad.y(0), deviceQuad.w(0), |
| 44 | deviceQuad.x(1), deviceQuad.y(1), deviceQuad.w(1), |
| 45 | deviceQuad.x(2), deviceQuad.y(2), deviceQuad.w(2), |
| 46 | deviceQuad.x(3), deviceQuad.y(3), deviceQuad.w(3), |
| 47 | localQuad.x(0), localQuad.y(0), localQuad.w(0), |
| 48 | localQuad.x(1), localQuad.y(1), localQuad.w(1), |
| 49 | localQuad.x(2), localQuad.y(2), localQuad.w(2), |
| 50 | localQuad.x(3), localQuad.y(3), localQuad.w(3)); |
| 51 | return str; |
| 52 | } |
| 53 | #endif |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 54 | |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 55 | class FillRectOp final : public GrMeshDrawOp { |
| 56 | private: |
| 57 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 58 | |
| 59 | public: |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 60 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 61 | GrPaint&& paint, |
| 62 | GrAAType aaType, |
| 63 | GrQuadAAFlags edgeAA, |
| 64 | const GrUserStencilSettings* stencilSettings, |
| 65 | const GrPerspQuad& deviceQuad, |
| 66 | GrQuadType deviceQuadType, |
| 67 | const GrPerspQuad& localQuad, |
| 68 | GrQuadType localQuadType) { |
| 69 | // Clean up deviations between aaType and edgeAA |
| 70 | GrResolveAATypeForQuad(aaType, edgeAA, deviceQuad, deviceQuadType, &aaType, &edgeAA); |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 71 | return Helper::FactoryHelper<FillRectOp>(context, std::move(paint), aaType, edgeAA, |
| 72 | stencilSettings, deviceQuad, deviceQuadType, localQuad, localQuadType); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 73 | } |
| 74 | |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 75 | // aaType is passed to Helper in the initializer list, so incongruities between aaType and |
| 76 | // edgeFlags must be resolved prior to calling this constructor. |
| 77 | FillRectOp(Helper::MakeArgs args, SkPMColor4f paintColor, GrAAType aaType, |
| 78 | GrQuadAAFlags edgeFlags, const GrUserStencilSettings* stencil, |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 79 | const GrPerspQuad& deviceQuad, GrQuadType deviceQuadType, |
| 80 | const GrPerspQuad& localQuad, GrQuadType localQuadType) |
| 81 | : INHERITED(ClassID()) |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 82 | , fHelper(args, aaType, stencil) |
Brian Salomon | 1d83542 | 2019-03-13 16:11:44 -0400 | [diff] [blame] | 83 | , fColorType(GrQuadPerEdgeAA::MinColorType(paintColor)) { |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 84 | // The color stored with the quad is the clear color if a scissor-clear is decided upon |
| 85 | // when executing the op. |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 86 | fDeviceQuads.push_back(deviceQuad, deviceQuadType, { paintColor, edgeFlags }); |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 87 | |
| 88 | if (!fHelper.isTrivial()) { |
| 89 | // Conservatively keep track of the local coordinates; it may be that the paint doesn't |
| 90 | // need them after analysis is finished. If the paint is known to be solid up front they |
| 91 | // can be skipped entirely. |
| 92 | fLocalQuads.push_back(localQuad, localQuadType); |
| 93 | } |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 94 | this->setBounds(deviceQuad.bounds(deviceQuadType), |
| 95 | HasAABloat(aaType == GrAAType::kCoverage), IsZeroArea::kNo); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | const char* name() const override { return "FillRectOp"; } |
| 99 | |
| 100 | void visitProxies(const VisitProxyFunc& func, VisitorType) const override { |
| 101 | return fHelper.visitProxies(func); |
| 102 | } |
| 103 | |
| 104 | #ifdef SK_DEBUG |
| 105 | SkString dumpInfo() const override { |
| 106 | SkString str; |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 107 | str.appendf("# draws: %u\n", this->quadCount()); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 108 | str.appendf("Device quad type: %u, local quad type: %u\n", |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 109 | (uint32_t) fDeviceQuads.quadType(), (uint32_t) fLocalQuads.quadType()); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 110 | str += fHelper.dumpInfo(); |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 111 | GrPerspQuad device, local; |
| 112 | for (int i = 0; i < this->quadCount(); i++) { |
| 113 | device = fDeviceQuads[i]; |
| 114 | const ColorAndAA& info = fDeviceQuads.metadata(i); |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 115 | if (!fHelper.isTrivial()) { |
| 116 | local = fLocalQuads[i]; |
| 117 | } |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 118 | str += dump_quad_info(i, device, local, info.fColor, info.fAAFlags); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 119 | } |
| 120 | str += INHERITED::dumpInfo(); |
| 121 | return str; |
| 122 | } |
| 123 | #endif |
| 124 | |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame^] | 125 | GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 126 | GrFSAAType fsaaType, GrClampType clampType) override { |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 127 | // Initialize aggregate color analysis with the first quad's color (which always exists) |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 128 | SkASSERT(this->quadCount() > 0); |
| 129 | GrProcessorAnalysisColor quadColors(fDeviceQuads.metadata(0).fColor); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 130 | // Then combine the colors of any additional quads (e.g. from MakeSet) |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 131 | for (int i = 1; i < this->quadCount(); ++i) { |
| 132 | quadColors = GrProcessorAnalysisColor::Combine(quadColors, |
| 133 | fDeviceQuads.metadata(i).fColor); |
Michael Ludwig | ca91e1f | 2018-12-10 10:44:44 -0500 | [diff] [blame] | 134 | if (quadColors.isUnknown()) { |
| 135 | // No point in accumulating additional starting colors, combining cannot make it |
| 136 | // less unknown. |
| 137 | break; |
| 138 | } |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 139 | } |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 140 | |
| 141 | // If the AA type is coverage, it will be a single value per pixel; if it's not coverage AA |
| 142 | // then the coverage is always 1.0, so specify kNone for more optimal blending. |
| 143 | GrProcessorAnalysisCoverage coverage = fHelper.aaType() == GrAAType::kCoverage ? |
| 144 | GrProcessorAnalysisCoverage::kSingleChannel : |
| 145 | GrProcessorAnalysisCoverage::kNone; |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame^] | 146 | auto result = fHelper.finalizeProcessors( |
| 147 | caps, clip, fsaaType, clampType, coverage, &quadColors); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 148 | // If there is a constant color after analysis, that means all of the quads should be set |
| 149 | // to the same color (even if they started out with different colors). |
| 150 | SkPMColor4f colorOverride; |
| 151 | if (quadColors.isConstant(&colorOverride)) { |
Brian Salomon | f419139 | 2019-03-14 15:46:20 -0400 | [diff] [blame] | 152 | // TODO: Unified strategy for handling wide color outputs from processor analysis. |
| 153 | // skbug.com/8871 |
| 154 | fColorType = GrQuadPerEdgeAA::MinColorType(colorOverride); |
| 155 | if (fColorType == ColorType::kHalf && !caps.halfFloatVertexAttributeSupport()) { |
| 156 | fColorType = ColorType::kByte; |
| 157 | colorOverride = {SkTPin(colorOverride.fR, 0.0f, 1.0f), |
| 158 | SkTPin(colorOverride.fG, 0.0f, 1.0f), |
| 159 | SkTPin(colorOverride.fB, 0.0f, 1.0f), |
| 160 | colorOverride.fA}; |
| 161 | } |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 162 | for (int i = 0; i < this->quadCount(); ++i) { |
| 163 | fDeviceQuads.metadata(i).fColor = colorOverride; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
| 167 | return result; |
| 168 | } |
| 169 | |
| 170 | FixedFunctionFlags fixedFunctionFlags() const override { |
| 171 | // Since the AA type of the whole primitive is kept consistent with the per edge AA flags |
| 172 | // the helper's fixed function flags are appropriate. |
| 173 | return fHelper.fixedFunctionFlags(); |
| 174 | } |
| 175 | |
| 176 | DEFINE_OP_CLASS_ID |
| 177 | |
| 178 | private: |
| 179 | // For GrFillRectOp::MakeSet's use of addQuad |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 180 | friend std::unique_ptr<GrDrawOp> GrFillRectOp::MakeSet( |
| 181 | GrRecordingContext*, |
| 182 | GrPaint&&, |
| 183 | GrAAType, const SkMatrix& viewMatrix, |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 184 | const GrRenderTargetContext::QuadSetEntry quads[], int quadCount, |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 185 | const GrUserStencilSettings*); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 186 | |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 187 | void onPrepareDraws(Target* target) override { |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 188 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 189 | |
| 190 | using Domain = GrQuadPerEdgeAA::Domain; |
| 191 | static constexpr SkRect kEmptyDomain = SkRect::MakeEmpty(); |
| 192 | |
Brian Salomon | 1d83542 | 2019-03-13 16:11:44 -0400 | [diff] [blame] | 193 | VertexSpec vertexSpec(fDeviceQuads.quadType(), fColorType, fLocalQuads.quadType(), |
| 194 | fHelper.usesLocalCoords(), Domain::kNo, fHelper.aaType(), |
| 195 | fHelper.compatibleWithAlphaAsCoverage()); |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 196 | // Make sure that if the op thought it was a solid color, the vertex spec does not use |
| 197 | // local coords. |
| 198 | SkASSERT(!fHelper.isTrivial() || !fHelper.usesLocalCoords()); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 199 | |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 200 | sk_sp<GrGeometryProcessor> gp = GrQuadPerEdgeAA::MakeProcessor(vertexSpec); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 201 | size_t vertexSize = gp->vertexStride(); |
| 202 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 203 | sk_sp<const GrBuffer> vbuffer; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 204 | int vertexOffsetInBuffer = 0; |
| 205 | |
| 206 | // Fill the allocated vertex data |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 207 | void* vdata = target->makeVertexSpace( |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 208 | vertexSize, this->quadCount() * vertexSpec.verticesPerQuad(), |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 209 | &vbuffer, &vertexOffsetInBuffer); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 210 | if (!vdata) { |
| 211 | SkDebugf("Could not allocate vertices\n"); |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | // vertices pointer advances through vdata based on Tessellate's return value |
| 216 | void* vertices = vdata; |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 217 | if (fHelper.isTrivial()) { |
| 218 | SkASSERT(fLocalQuads.count() == 0); // No local coords, so send an ignored dummy quad |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 219 | static const GrPerspQuad kIgnoredLocal(SkRect::MakeEmpty()); |
| 220 | |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 221 | for (int i = 0; i < this->quadCount(); ++i) { |
| 222 | const ColorAndAA& info = fDeviceQuads.metadata(i); |
| 223 | vertices = GrQuadPerEdgeAA::Tessellate(vertices, vertexSpec, fDeviceQuads[i], |
| 224 | info.fColor, kIgnoredLocal, kEmptyDomain, info.fAAFlags); |
| 225 | } |
| 226 | } else { |
| 227 | SkASSERT(fLocalQuads.count() == fDeviceQuads.count()); |
| 228 | for (int i = 0; i < this->quadCount(); ++i) { |
| 229 | const ColorAndAA& info = fDeviceQuads.metadata(i); |
| 230 | vertices = GrQuadPerEdgeAA::Tessellate(vertices, vertexSpec, fDeviceQuads[i], |
| 231 | info.fColor, fLocalQuads[i], kEmptyDomain, info.fAAFlags); |
| 232 | } |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // Configure the mesh for the vertex data |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 236 | GrMesh* mesh = target->allocMeshes(1); |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 237 | if (!GrQuadPerEdgeAA::ConfigureMeshIndices(target, mesh, vertexSpec, this->quadCount())) { |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 238 | SkDebugf("Could not allocate indices\n"); |
| 239 | return; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 240 | } |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 241 | mesh->setVertexData(std::move(vbuffer), vertexOffsetInBuffer); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 242 | target->recordDraw(std::move(gp), mesh); |
| 243 | } |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 244 | |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 245 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
| 246 | fHelper.executeDrawsAndUploads(this, flushState, chainBounds); |
| 247 | } |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 248 | |
| 249 | CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
| 250 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 251 | const auto* that = t->cast<FillRectOp>(); |
| 252 | |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 253 | if ((fHelper.aaType() == GrAAType::kCoverage || |
| 254 | that->fHelper.aaType() == GrAAType::kCoverage) && |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 255 | this->quadCount() + that->quadCount() > GrQuadPerEdgeAA::kNumAAQuadsInIndexBuffer) { |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 256 | // This limit on batch size seems to help on Adreno devices |
| 257 | return CombineResult::kCannotCombine; |
| 258 | } |
| 259 | |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 260 | // Unlike most users of the draw op helper, this op can merge none-aa and coverage-aa draw |
| 261 | // ops together, so pass true as the last argument. |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 262 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds(), true)) { |
| 263 | return CombineResult::kCannotCombine; |
| 264 | } |
| 265 | |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 266 | // If the paints were compatible, the trivial/solid-color state should be the same |
| 267 | SkASSERT(fHelper.isTrivial() == that->fHelper.isTrivial()); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 268 | |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 269 | // If the processor sets are compatible, the two ops are always compatible; it just needs to |
| 270 | // adjust the state of the op to be the more general quad and aa types of the two ops and |
| 271 | // then concatenate the per-quad data. |
Brian Salomon | 1d83542 | 2019-03-13 16:11:44 -0400 | [diff] [blame] | 272 | fColorType = SkTMax(fColorType, that->fColorType); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 273 | |
| 274 | // The helper stores the aa type, but isCompatible(with true arg) allows the two ops' aa |
| 275 | // types to be none and coverage, in which case this op's aa type must be lifted to coverage |
| 276 | // so that quads with no aa edges can be batched with quads that have some/all edges aa'ed. |
| 277 | if (fHelper.aaType() == GrAAType::kNone && that->fHelper.aaType() == GrAAType::kCoverage) { |
| 278 | fHelper.setAAType(GrAAType::kCoverage); |
| 279 | } |
| 280 | |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 281 | fDeviceQuads.concat(that->fDeviceQuads); |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 282 | if (!fHelper.isTrivial()) { |
| 283 | fLocalQuads.concat(that->fLocalQuads); |
| 284 | } |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 285 | return CombineResult::kMerged; |
| 286 | } |
| 287 | |
| 288 | // Similar to onCombineIfPossible, but adds a quad assuming its op would have been compatible. |
| 289 | // But since it's avoiding the op list management, it must update the op's bounds. This is only |
| 290 | // used with quad sets, which uses the same view matrix for each quad so this assumes that the |
| 291 | // device quad type of the new quad is the same as the op's. |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 292 | void addQuad(const GrPerspQuad& deviceQuad, const GrPerspQuad& localQuad, |
| 293 | GrQuadType localQuadType, const SkPMColor4f& color, GrQuadAAFlags edgeAA, |
| 294 | GrAAType aaType) { |
| 295 | SkASSERT(deviceQuad.quadType() <= fDeviceQuads.quadType()); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 296 | |
| 297 | // The new quad's aa type should be the same as the first quad's or none, except when the |
| 298 | // first quad's aa type was already downgraded to none, in which case the stored type must |
| 299 | // be lifted to back to the requested type. |
| 300 | if (aaType != fHelper.aaType()) { |
| 301 | if (aaType != GrAAType::kNone) { |
| 302 | // Original quad was downgraded to non-aa, lift back up to this quad's required type |
| 303 | SkASSERT(fHelper.aaType() == GrAAType::kNone); |
| 304 | fHelper.setAAType(aaType); |
| 305 | } |
| 306 | // else the new quad could have been downgraded but the other quads can't be, so don't |
| 307 | // reset the op's accumulated aa type. |
| 308 | } |
| 309 | |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 310 | // clear compatible won't need to be updated, since device quad type and paint is the same, |
Brian Salomon | 1d83542 | 2019-03-13 16:11:44 -0400 | [diff] [blame] | 311 | // but this quad has a new color, so maybe update color type |
| 312 | fColorType = SkTMax(fColorType, GrQuadPerEdgeAA::MinColorType(color)); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 313 | |
| 314 | // Update the bounds and add the quad to this op's storage |
| 315 | SkRect newBounds = this->bounds(); |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 316 | newBounds.joinPossiblyEmptyRect(deviceQuad.bounds(fDeviceQuads.quadType())); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 317 | this->setBounds(newBounds, HasAABloat(fHelper.aaType() == GrAAType::kCoverage), |
| 318 | IsZeroArea::kNo); |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 319 | fDeviceQuads.push_back(deviceQuad, fDeviceQuads.quadType(), { color, edgeAA }); |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 320 | if (!fHelper.isTrivial()) { |
| 321 | fLocalQuads.push_back(localQuad, localQuadType); |
| 322 | } |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 323 | } |
| 324 | |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 325 | int quadCount() const { |
| 326 | // Sanity check that the parallel arrays for quad properties all have the same size |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 327 | SkASSERT(fDeviceQuads.count() == fLocalQuads.count() || |
| 328 | (fLocalQuads.count() == 0 && fHelper.isTrivial())); |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 329 | return fDeviceQuads.count(); |
| 330 | } |
| 331 | |
| 332 | struct ColorAndAA { |
| 333 | SkPMColor4f fColor; |
| 334 | GrQuadAAFlags fAAFlags; |
| 335 | }; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 336 | |
| 337 | Helper fHelper; |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 338 | GrTQuadList<ColorAndAA> fDeviceQuads; |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 339 | // No metadata attached to the local quads; this list is empty when local coords are not needed. |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 340 | GrQuadList fLocalQuads; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 341 | |
Brian Salomon | 1d83542 | 2019-03-13 16:11:44 -0400 | [diff] [blame] | 342 | ColorType fColorType; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 343 | |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 344 | typedef GrMeshDrawOp INHERITED; |
| 345 | }; |
| 346 | |
| 347 | } // anonymous namespace |
| 348 | |
| 349 | namespace GrFillRectOp { |
| 350 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 351 | std::unique_ptr<GrDrawOp> MakePerEdge(GrRecordingContext* context, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 352 | GrPaint&& paint, |
| 353 | GrAAType aaType, |
| 354 | GrQuadAAFlags edgeAA, |
| 355 | const SkMatrix& viewMatrix, |
| 356 | const SkRect& rect, |
| 357 | const GrUserStencilSettings* stencilSettings) { |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 358 | GrQuadType dstQuadType = GrQuadTypeForTransformedRect(viewMatrix); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 359 | return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings, |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 360 | GrPerspQuad::MakeFromRect(rect, viewMatrix), dstQuadType, |
| 361 | GrPerspQuad(rect), GrQuadType::kRect); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 362 | } |
| 363 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 364 | std::unique_ptr<GrDrawOp> MakePerEdgeWithLocalMatrix(GrRecordingContext* context, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 365 | GrPaint&& paint, |
| 366 | GrAAType aaType, |
| 367 | GrQuadAAFlags edgeAA, |
| 368 | const SkMatrix& viewMatrix, |
| 369 | const SkMatrix& localMatrix, |
| 370 | const SkRect& rect, |
| 371 | const GrUserStencilSettings* stencilSettings) { |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 372 | GrQuadType dstQuadType = GrQuadTypeForTransformedRect(viewMatrix); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 373 | GrQuadType localQuadType = GrQuadTypeForTransformedRect(localMatrix); |
| 374 | return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings, |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 375 | GrPerspQuad::MakeFromRect(rect, viewMatrix), dstQuadType, |
| 376 | GrPerspQuad::MakeFromRect(rect, localMatrix), localQuadType); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 377 | } |
| 378 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 379 | std::unique_ptr<GrDrawOp> MakePerEdgeWithLocalRect(GrRecordingContext* context, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 380 | GrPaint&& paint, |
| 381 | GrAAType aaType, |
| 382 | GrQuadAAFlags edgeAA, |
| 383 | const SkMatrix& viewMatrix, |
| 384 | const SkRect& rect, |
| 385 | const SkRect& localRect, |
| 386 | const GrUserStencilSettings* stencilSettings) { |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 387 | GrQuadType dstQuadType = GrQuadTypeForTransformedRect(viewMatrix); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 388 | return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings, |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 389 | GrPerspQuad::MakeFromRect(rect, viewMatrix), dstQuadType, |
| 390 | GrPerspQuad(localRect), GrQuadType::kRect); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 391 | } |
| 392 | |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 393 | std::unique_ptr<GrDrawOp> MakePerEdgeQuad(GrRecordingContext* context, |
| 394 | GrPaint&& paint, |
| 395 | GrAAType aaType, |
| 396 | GrQuadAAFlags edgeAA, |
| 397 | const SkMatrix& viewMatrix, |
| 398 | const SkPoint quad[4], |
| 399 | const SkPoint localQuad[4], |
| 400 | const GrUserStencilSettings* stencilSettings) { |
| 401 | // With arbitrary quads, the quad types are limited to kStandard or kPerspective (unless we |
| 402 | // analyzed the points, but callers have more knowledge and should've just use the appropriate |
| 403 | // factory, so assume they can't be rectilinear or simpler) |
| 404 | GrQuadType deviceType = viewMatrix.hasPerspective() ? GrQuadType::kPerspective |
| 405 | : GrQuadType::kStandard; |
| 406 | return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings, |
| 407 | GrPerspQuad::MakeFromSkQuad(quad, viewMatrix), deviceType, |
| 408 | GrPerspQuad::MakeFromSkQuad(localQuad ? localQuad : quad, |
| 409 | SkMatrix::I()), GrQuadType::kStandard); |
| 410 | } |
| 411 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 412 | std::unique_ptr<GrDrawOp> MakeSet(GrRecordingContext* context, |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 413 | GrPaint&& paint, |
| 414 | GrAAType aaType, |
| 415 | const SkMatrix& viewMatrix, |
| 416 | const GrRenderTargetContext::QuadSetEntry quads[], |
| 417 | int cnt, |
| 418 | const GrUserStencilSettings* stencilSettings) { |
| 419 | // First make a draw op for the first quad in the set |
| 420 | SkASSERT(cnt > 0); |
| 421 | GrQuadType deviceQuadType = GrQuadTypeForTransformedRect(viewMatrix); |
| 422 | |
| 423 | paint.setColor4f(quads[0].fColor); |
| 424 | std::unique_ptr<GrDrawOp> op = FillRectOp::Make(context, std::move(paint), aaType, |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 425 | quads[0].fAAFlags, stencilSettings, |
| 426 | GrPerspQuad::MakeFromRect(quads[0].fRect, viewMatrix), deviceQuadType, |
| 427 | GrPerspQuad::MakeFromRect(quads[0].fRect, quads[0].fLocalMatrix), |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 428 | GrQuadTypeForTransformedRect(quads[0].fLocalMatrix)); |
| 429 | auto* fillRects = op->cast<FillRectOp>(); |
| 430 | |
| 431 | // Accumulate remaining quads similar to onCombineIfPossible() without creating an op |
| 432 | for (int i = 1; i < cnt; ++i) { |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 433 | GrPerspQuad deviceQuad = GrPerspQuad::MakeFromRect(quads[i].fRect, viewMatrix); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 434 | |
| 435 | GrAAType resolvedAA; |
| 436 | GrQuadAAFlags resolvedEdgeFlags; |
| 437 | GrResolveAATypeForQuad(aaType, quads[i].fAAFlags, deviceQuad, deviceQuadType, |
| 438 | &resolvedAA, &resolvedEdgeFlags); |
| 439 | |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 440 | fillRects->addQuad(deviceQuad, |
| 441 | GrPerspQuad::MakeFromRect(quads[i].fRect, quads[i].fLocalMatrix), |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 442 | GrQuadTypeForTransformedRect(quads[i].fLocalMatrix), quads[i].fColor, |
| 443 | resolvedEdgeFlags,resolvedAA); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | return op; |
| 447 | } |
| 448 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 449 | std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 450 | GrPaint&& paint, |
| 451 | GrAAType aaType, |
| 452 | const SkMatrix& viewMatrix, |
| 453 | const SkRect& rect, |
| 454 | const GrUserStencilSettings* stencil) { |
| 455 | return MakePerEdge(context, std::move(paint), aaType, |
| 456 | aaType == GrAAType::kCoverage ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone, |
| 457 | viewMatrix, rect, stencil); |
| 458 | } |
| 459 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 460 | std::unique_ptr<GrDrawOp> MakeWithLocalMatrix(GrRecordingContext* context, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 461 | GrPaint&& paint, |
| 462 | GrAAType aaType, |
| 463 | const SkMatrix& viewMatrix, |
| 464 | const SkMatrix& localMatrix, |
| 465 | const SkRect& rect, |
| 466 | const GrUserStencilSettings* stencil) { |
| 467 | return MakePerEdgeWithLocalMatrix(context, std::move(paint), aaType, |
| 468 | aaType == GrAAType::kCoverage ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone, |
| 469 | viewMatrix, localMatrix, rect, stencil); |
| 470 | } |
| 471 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 472 | std::unique_ptr<GrDrawOp> MakeWithLocalRect(GrRecordingContext* context, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 473 | GrPaint&& paint, |
| 474 | GrAAType aaType, |
| 475 | const SkMatrix& viewMatrix, |
| 476 | const SkRect& rect, |
| 477 | const SkRect& localRect, |
| 478 | const GrUserStencilSettings* stencil) { |
| 479 | return MakePerEdgeWithLocalRect(context, std::move(paint), aaType, |
| 480 | aaType == GrAAType::kCoverage ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone, |
| 481 | viewMatrix, rect, localRect, stencil); |
| 482 | } |
| 483 | |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 484 | } // namespace GrFillRectOp |
| 485 | |
| 486 | #if GR_TEST_UTILS |
| 487 | |
| 488 | #include "GrDrawOpTest.h" |
| 489 | #include "SkGr.h" |
| 490 | |
| 491 | GR_DRAW_OP_TEST_DEFINE(FillRectOp) { |
| 492 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 493 | SkRect rect = GrTest::TestRect(random); |
| 494 | |
| 495 | GrAAType aaType = GrAAType::kNone; |
| 496 | if (random->nextBool()) { |
| 497 | aaType = (fsaaType == GrFSAAType::kUnifiedMSAA) ? GrAAType::kMSAA : GrAAType::kCoverage; |
| 498 | } |
| 499 | const GrUserStencilSettings* stencil = random->nextBool() ? nullptr |
| 500 | : GrGetRandomStencil(random, context); |
| 501 | |
| 502 | GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone; |
| 503 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone; |
| 504 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone; |
| 505 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone; |
| 506 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone; |
| 507 | |
| 508 | if (random->nextBool()) { |
| 509 | if (random->nextBool()) { |
| 510 | if (random->nextBool()) { |
| 511 | // Local matrix with a set op |
| 512 | uint32_t extraQuadCt = random->nextRangeU(1, 4); |
| 513 | SkTArray<GrRenderTargetContext::QuadSetEntry> quads(extraQuadCt + 1); |
| 514 | quads.push_back( |
| 515 | {rect, SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU())), |
| 516 | GrTest::TestMatrixInvertible(random), aaFlags}); |
| 517 | for (uint32_t i = 0; i < extraQuadCt; ++i) { |
| 518 | GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone; |
| 519 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone; |
| 520 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone; |
| 521 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone; |
| 522 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone; |
| 523 | |
| 524 | quads.push_back( |
| 525 | {GrTest::TestRect(random), |
| 526 | SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU())), |
| 527 | GrTest::TestMatrixInvertible(random), aaFlags}); |
| 528 | } |
| 529 | |
| 530 | return GrFillRectOp::MakeSet(context, std::move(paint), aaType, viewMatrix, |
| 531 | quads.begin(), quads.count(), stencil); |
| 532 | } else { |
| 533 | // Single local matrix |
| 534 | SkMatrix localMatrix = GrTest::TestMatrixInvertible(random); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 535 | return GrFillRectOp::MakePerEdgeWithLocalMatrix(context, std::move(paint), aaType, |
| 536 | aaFlags, viewMatrix, localMatrix, |
| 537 | rect, stencil); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 538 | } |
| 539 | } else { |
| 540 | // Pass local rect directly |
| 541 | SkRect localRect = GrTest::TestRect(random); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 542 | return GrFillRectOp::MakePerEdgeWithLocalRect(context, std::move(paint), aaType, |
| 543 | aaFlags, viewMatrix, rect, localRect, |
| 544 | stencil); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 545 | } |
| 546 | } else { |
| 547 | // The simplest constructor |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 548 | return GrFillRectOp::MakePerEdge(context, std::move(paint), aaType, aaFlags, viewMatrix, |
| 549 | rect, stencil); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
| 553 | #endif |