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