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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame^] | 8 | #include "src/gpu/ops/GrFillRectOp.h" |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame^] | 10 | #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" |
| 15 | #include "src/gpu/GrQuad.h" |
| 16 | #include "src/gpu/SkGr.h" |
| 17 | #include "src/gpu/glsl/GrGLSLColorSpaceXformHelper.h" |
| 18 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 19 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 20 | #include "src/gpu/ops/GrMeshDrawOp.h" |
| 21 | #include "src/gpu/ops/GrQuadPerEdgeAA.h" |
| 22 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 23 | |
| 24 | namespace { |
| 25 | |
| 26 | using VertexSpec = GrQuadPerEdgeAA::VertexSpec; |
| 27 | using ColorType = GrQuadPerEdgeAA::ColorType; |
| 28 | |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 29 | #ifdef SK_DEBUG |
| 30 | static SkString dump_quad_info(int index, const GrPerspQuad& deviceQuad, |
| 31 | const GrPerspQuad& localQuad, const SkPMColor4f& color, |
| 32 | GrQuadAAFlags aaFlags) { |
| 33 | SkString str; |
| 34 | str.appendf("%d: Color: [%.2f, %.2f, %.2f, %.2f], Edge AA: l%u_t%u_r%u_b%u, \n" |
| 35 | " device quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), " |
| 36 | "(%.2f, %.2f, %.2f)],\n" |
| 37 | " local quad: [(%.2f, %2.f, %.2f), (%.2f, %.2f, %.2f), (%.2f, %.2f, %.2f), " |
| 38 | "(%.2f, %.2f, %.2f)]\n", |
| 39 | index, color.fR, color.fG, color.fB, color.fA, |
| 40 | (uint32_t) (aaFlags & GrQuadAAFlags::kLeft), |
| 41 | (uint32_t) (aaFlags & GrQuadAAFlags::kTop), |
| 42 | (uint32_t) (aaFlags & GrQuadAAFlags::kRight), |
| 43 | (uint32_t) (aaFlags & GrQuadAAFlags::kBottom), |
| 44 | deviceQuad.x(0), deviceQuad.y(0), deviceQuad.w(0), |
| 45 | deviceQuad.x(1), deviceQuad.y(1), deviceQuad.w(1), |
| 46 | deviceQuad.x(2), deviceQuad.y(2), deviceQuad.w(2), |
| 47 | deviceQuad.x(3), deviceQuad.y(3), deviceQuad.w(3), |
| 48 | localQuad.x(0), localQuad.y(0), localQuad.w(0), |
| 49 | localQuad.x(1), localQuad.y(1), localQuad.w(1), |
| 50 | localQuad.x(2), localQuad.y(2), localQuad.w(2), |
| 51 | localQuad.x(3), localQuad.y(3), localQuad.w(3)); |
| 52 | return str; |
| 53 | } |
| 54 | #endif |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 55 | |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 56 | class FillRectOp final : public GrMeshDrawOp { |
| 57 | private: |
| 58 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 59 | |
| 60 | public: |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 61 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 62 | GrPaint&& paint, |
| 63 | GrAAType aaType, |
| 64 | GrQuadAAFlags edgeAA, |
| 65 | const GrUserStencilSettings* stencilSettings, |
| 66 | const GrPerspQuad& deviceQuad, |
| 67 | GrQuadType deviceQuadType, |
| 68 | const GrPerspQuad& localQuad, |
| 69 | GrQuadType localQuadType) { |
| 70 | // Clean up deviations between aaType and edgeAA |
| 71 | GrResolveAATypeForQuad(aaType, edgeAA, deviceQuad, deviceQuadType, &aaType, &edgeAA); |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 72 | return Helper::FactoryHelper<FillRectOp>(context, std::move(paint), aaType, edgeAA, |
| 73 | stencilSettings, deviceQuad, deviceQuadType, localQuad, localQuadType); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 76 | // 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 Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 80 | const GrPerspQuad& deviceQuad, GrQuadType deviceQuadType, |
| 81 | const GrPerspQuad& localQuad, GrQuadType localQuadType) |
| 82 | : INHERITED(ClassID()) |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 83 | , fHelper(args, aaType, stencil) { |
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 Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 152 | fColorType = GrQuadPerEdgeAA::MinColorType(colorOverride, clampType, caps); |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 153 | for (int i = 0; i < this->quadCount(); ++i) { |
| 154 | fDeviceQuads.metadata(i).fColor = colorOverride; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 155 | } |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 156 | } else { |
| 157 | // Otherwise compute the color type needed as the max over all quads. |
| 158 | fColorType = ColorType::kNone; |
| 159 | for (int i = 0; i < this->quadCount(); ++i) { |
| 160 | SkPMColor4f* color = &fDeviceQuads.metadata(i).fColor; |
| 161 | fColorType = SkTMax(fColorType, |
| 162 | GrQuadPerEdgeAA::MinColorType(*color, clampType, caps)); |
| 163 | } |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 164 | } |
Brian Salomon | 41f9c3c | 2019-03-25 11:06:12 -0400 | [diff] [blame] | 165 | // Most SkShaders' FPs multiply their calculated color by the paint color or alpha. We want |
| 166 | // to use ColorType::kNone to optimize out that multiply. However, if there are no color |
| 167 | // FPs then were really writing a special shader for white rectangles and not saving any |
| 168 | // multiples. So in that case use bytes to avoid the extra shader (and possibly work around |
| 169 | // an ANGLE issue: crbug.com/942565). |
| 170 | if (fColorType == ColorType::kNone && !result.hasColorFragmentProcessor()) { |
| 171 | fColorType = ColorType::kByte; |
| 172 | } |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 173 | |
| 174 | return result; |
| 175 | } |
| 176 | |
| 177 | FixedFunctionFlags fixedFunctionFlags() const override { |
| 178 | // Since the AA type of the whole primitive is kept consistent with the per edge AA flags |
| 179 | // the helper's fixed function flags are appropriate. |
| 180 | return fHelper.fixedFunctionFlags(); |
| 181 | } |
| 182 | |
| 183 | DEFINE_OP_CLASS_ID |
| 184 | |
| 185 | private: |
| 186 | // For GrFillRectOp::MakeSet's use of addQuad |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 187 | friend std::unique_ptr<GrDrawOp> GrFillRectOp::MakeSet( |
| 188 | GrRecordingContext*, |
| 189 | GrPaint&&, |
| 190 | GrAAType, const SkMatrix& viewMatrix, |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 191 | const GrRenderTargetContext::QuadSetEntry quads[], int quadCount, |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 192 | const GrUserStencilSettings*); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 193 | |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 194 | void onPrepareDraws(Target* target) override { |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 195 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 196 | |
| 197 | using Domain = GrQuadPerEdgeAA::Domain; |
| 198 | static constexpr SkRect kEmptyDomain = SkRect::MakeEmpty(); |
| 199 | |
Brian Salomon | 1d83542 | 2019-03-13 16:11:44 -0400 | [diff] [blame] | 200 | VertexSpec vertexSpec(fDeviceQuads.quadType(), fColorType, fLocalQuads.quadType(), |
| 201 | fHelper.usesLocalCoords(), Domain::kNo, fHelper.aaType(), |
Brian Osman | 605c6d5 | 2019-03-15 12:10:35 -0400 | [diff] [blame] | 202 | fHelper.compatibleWithCoverageAsAlpha()); |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 203 | // Make sure that if the op thought it was a solid color, the vertex spec does not use |
| 204 | // local coords. |
| 205 | SkASSERT(!fHelper.isTrivial() || !fHelper.usesLocalCoords()); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 206 | |
Michael Ludwig | 467994d | 2018-12-03 14:58:31 +0000 | [diff] [blame] | 207 | sk_sp<GrGeometryProcessor> gp = GrQuadPerEdgeAA::MakeProcessor(vertexSpec); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 208 | size_t vertexSize = gp->vertexStride(); |
| 209 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 210 | sk_sp<const GrBuffer> vbuffer; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 211 | int vertexOffsetInBuffer = 0; |
| 212 | |
| 213 | // Fill the allocated vertex data |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 214 | void* vdata = target->makeVertexSpace( |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 215 | vertexSize, this->quadCount() * vertexSpec.verticesPerQuad(), |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 216 | &vbuffer, &vertexOffsetInBuffer); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 217 | if (!vdata) { |
| 218 | SkDebugf("Could not allocate vertices\n"); |
| 219 | return; |
| 220 | } |
| 221 | |
| 222 | // vertices pointer advances through vdata based on Tessellate's return value |
| 223 | void* vertices = vdata; |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 224 | if (fHelper.isTrivial()) { |
| 225 | 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] | 226 | static const GrPerspQuad kIgnoredLocal(SkRect::MakeEmpty()); |
| 227 | |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 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, kIgnoredLocal, kEmptyDomain, info.fAAFlags); |
| 232 | } |
| 233 | } else { |
| 234 | SkASSERT(fLocalQuads.count() == fDeviceQuads.count()); |
| 235 | for (int i = 0; i < this->quadCount(); ++i) { |
| 236 | const ColorAndAA& info = fDeviceQuads.metadata(i); |
| 237 | vertices = GrQuadPerEdgeAA::Tessellate(vertices, vertexSpec, fDeviceQuads[i], |
| 238 | info.fColor, fLocalQuads[i], kEmptyDomain, info.fAAFlags); |
| 239 | } |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | // Configure the mesh for the vertex data |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 243 | GrMesh* mesh = target->allocMeshes(1); |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 244 | if (!GrQuadPerEdgeAA::ConfigureMeshIndices(target, mesh, vertexSpec, this->quadCount())) { |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 245 | SkDebugf("Could not allocate indices\n"); |
| 246 | return; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 247 | } |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 248 | mesh->setVertexData(std::move(vbuffer), vertexOffsetInBuffer); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 249 | target->recordDraw(std::move(gp), mesh); |
| 250 | } |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 251 | |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 252 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
| 253 | fHelper.executeDrawsAndUploads(this, flushState, chainBounds); |
| 254 | } |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 255 | |
| 256 | CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
| 257 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 258 | const auto* that = t->cast<FillRectOp>(); |
| 259 | |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 260 | if ((fHelper.aaType() == GrAAType::kCoverage || |
| 261 | that->fHelper.aaType() == GrAAType::kCoverage) && |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 262 | this->quadCount() + that->quadCount() > GrQuadPerEdgeAA::kNumAAQuadsInIndexBuffer) { |
Michael Ludwig | 93aeba0 | 2018-12-21 09:50:31 -0500 | [diff] [blame] | 263 | // This limit on batch size seems to help on Adreno devices |
| 264 | return CombineResult::kCannotCombine; |
| 265 | } |
| 266 | |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 267 | // Unlike most users of the draw op helper, this op can merge none-aa and coverage-aa draw |
| 268 | // ops together, so pass true as the last argument. |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 269 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds(), true)) { |
| 270 | return CombineResult::kCannotCombine; |
| 271 | } |
| 272 | |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 273 | // If the paints were compatible, the trivial/solid-color state should be the same |
| 274 | SkASSERT(fHelper.isTrivial() == that->fHelper.isTrivial()); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 275 | |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 276 | // If the processor sets are compatible, the two ops are always compatible; it just needs to |
| 277 | // adjust the state of the op to be the more general quad and aa types of the two ops and |
| 278 | // then concatenate the per-quad data. |
Brian Salomon | 1d83542 | 2019-03-13 16:11:44 -0400 | [diff] [blame] | 279 | fColorType = SkTMax(fColorType, that->fColorType); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 280 | |
| 281 | // The helper stores the aa type, but isCompatible(with true arg) allows the two ops' aa |
| 282 | // types to be none and coverage, in which case this op's aa type must be lifted to coverage |
| 283 | // so that quads with no aa edges can be batched with quads that have some/all edges aa'ed. |
| 284 | if (fHelper.aaType() == GrAAType::kNone && that->fHelper.aaType() == GrAAType::kCoverage) { |
| 285 | fHelper.setAAType(GrAAType::kCoverage); |
| 286 | } |
| 287 | |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 288 | fDeviceQuads.concat(that->fDeviceQuads); |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 289 | if (!fHelper.isTrivial()) { |
| 290 | fLocalQuads.concat(that->fLocalQuads); |
| 291 | } |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 292 | 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. |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 299 | void addQuad(const GrPerspQuad& deviceQuad, const GrPerspQuad& localQuad, |
| 300 | GrQuadType localQuadType, const SkPMColor4f& color, GrQuadAAFlags edgeAA, |
| 301 | GrAAType aaType) { |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 302 | // The new quad's aa type should be the same as the first quad's or none, except when the |
| 303 | // first quad's aa type was already downgraded to none, in which case the stored type must |
| 304 | // be lifted to back to the requested type. |
| 305 | if (aaType != fHelper.aaType()) { |
| 306 | if (aaType != GrAAType::kNone) { |
| 307 | // Original quad was downgraded to non-aa, lift back up to this quad's required type |
| 308 | SkASSERT(fHelper.aaType() == GrAAType::kNone); |
| 309 | fHelper.setAAType(aaType); |
| 310 | } |
| 311 | // else the new quad could have been downgraded but the other quads can't be, so don't |
| 312 | // reset the op's accumulated aa type. |
| 313 | } |
| 314 | |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 315 | // Update the bounds and add the quad to this op's storage |
| 316 | SkRect newBounds = this->bounds(); |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 317 | newBounds.joinPossiblyEmptyRect(deviceQuad.bounds(fDeviceQuads.quadType())); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 318 | this->setBounds(newBounds, HasAABloat(fHelper.aaType() == GrAAType::kCoverage), |
| 319 | IsZeroArea::kNo); |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 320 | fDeviceQuads.push_back(deviceQuad, fDeviceQuads.quadType(), { color, edgeAA }); |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 321 | if (!fHelper.isTrivial()) { |
| 322 | fLocalQuads.push_back(localQuad, localQuadType); |
| 323 | } |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 324 | } |
| 325 | |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 326 | int quadCount() const { |
| 327 | // 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] | 328 | SkASSERT(fDeviceQuads.count() == fLocalQuads.count() || |
| 329 | (fLocalQuads.count() == 0 && fHelper.isTrivial())); |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 330 | return fDeviceQuads.count(); |
| 331 | } |
| 332 | |
| 333 | struct ColorAndAA { |
| 334 | SkPMColor4f fColor; |
| 335 | GrQuadAAFlags fAAFlags; |
| 336 | }; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 337 | |
| 338 | Helper fHelper; |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 339 | GrTQuadList<ColorAndAA> fDeviceQuads; |
Michael Ludwig | dcd4821 | 2019-01-08 15:28:57 -0500 | [diff] [blame] | 340 | // 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] | 341 | GrQuadList fLocalQuads; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 342 | |
Brian Salomon | 1d83542 | 2019-03-13 16:11:44 -0400 | [diff] [blame] | 343 | ColorType fColorType; |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 344 | |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 345 | typedef GrMeshDrawOp INHERITED; |
| 346 | }; |
| 347 | |
| 348 | } // anonymous namespace |
| 349 | |
| 350 | namespace GrFillRectOp { |
| 351 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 352 | std::unique_ptr<GrDrawOp> MakePerEdge(GrRecordingContext* context, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 353 | GrPaint&& paint, |
| 354 | GrAAType aaType, |
| 355 | GrQuadAAFlags edgeAA, |
| 356 | const SkMatrix& viewMatrix, |
| 357 | const SkRect& rect, |
| 358 | const GrUserStencilSettings* stencilSettings) { |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 359 | GrQuadType dstQuadType = GrQuadTypeForTransformedRect(viewMatrix); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 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(rect), GrQuadType::kRect); |
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> MakePerEdgeWithLocalMatrix(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 SkMatrix& localMatrix, |
| 371 | const SkRect& rect, |
| 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 | GrQuadType localQuadType = GrQuadTypeForTransformedRect(localMatrix); |
| 375 | return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings, |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 376 | GrPerspQuad::MakeFromRect(rect, viewMatrix), dstQuadType, |
| 377 | GrPerspQuad::MakeFromRect(rect, localMatrix), localQuadType); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 378 | } |
| 379 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 380 | std::unique_ptr<GrDrawOp> MakePerEdgeWithLocalRect(GrRecordingContext* context, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 381 | GrPaint&& paint, |
| 382 | GrAAType aaType, |
| 383 | GrQuadAAFlags edgeAA, |
| 384 | const SkMatrix& viewMatrix, |
| 385 | const SkRect& rect, |
| 386 | const SkRect& localRect, |
| 387 | const GrUserStencilSettings* stencilSettings) { |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 388 | GrQuadType dstQuadType = GrQuadTypeForTransformedRect(viewMatrix); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 389 | return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings, |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 390 | GrPerspQuad::MakeFromRect(rect, viewMatrix), dstQuadType, |
| 391 | GrPerspQuad(localRect), GrQuadType::kRect); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 392 | } |
| 393 | |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 394 | std::unique_ptr<GrDrawOp> MakePerEdgeQuad(GrRecordingContext* context, |
| 395 | GrPaint&& paint, |
| 396 | GrAAType aaType, |
| 397 | GrQuadAAFlags edgeAA, |
| 398 | const SkMatrix& viewMatrix, |
| 399 | const SkPoint quad[4], |
| 400 | const SkPoint localQuad[4], |
| 401 | const GrUserStencilSettings* stencilSettings) { |
Michael Ludwig | 97b9442 | 2019-04-09 10:42:39 -0400 | [diff] [blame] | 402 | GrQuadType deviceType = GrQuadTypeForPoints(quad, viewMatrix); |
| 403 | GrQuadType localType = GrQuadTypeForPoints(localQuad ? localQuad : quad, SkMatrix::I()); |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 404 | return FillRectOp::Make(context, std::move(paint), aaType, edgeAA, stencilSettings, |
| 405 | GrPerspQuad::MakeFromSkQuad(quad, viewMatrix), deviceType, |
| 406 | GrPerspQuad::MakeFromSkQuad(localQuad ? localQuad : quad, |
Michael Ludwig | 97b9442 | 2019-04-09 10:42:39 -0400 | [diff] [blame] | 407 | SkMatrix::I()), localType); |
Michael Ludwig | 009b92e | 2019-02-15 16:03:53 -0500 | [diff] [blame] | 408 | } |
| 409 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 410 | std::unique_ptr<GrDrawOp> MakeSet(GrRecordingContext* context, |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 411 | GrPaint&& paint, |
| 412 | GrAAType aaType, |
| 413 | const SkMatrix& viewMatrix, |
| 414 | const GrRenderTargetContext::QuadSetEntry quads[], |
| 415 | int cnt, |
| 416 | const GrUserStencilSettings* stencilSettings) { |
| 417 | // First make a draw op for the first quad in the set |
| 418 | SkASSERT(cnt > 0); |
| 419 | GrQuadType deviceQuadType = GrQuadTypeForTransformedRect(viewMatrix); |
| 420 | |
| 421 | paint.setColor4f(quads[0].fColor); |
| 422 | std::unique_ptr<GrDrawOp> op = FillRectOp::Make(context, std::move(paint), aaType, |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 423 | quads[0].fAAFlags, stencilSettings, |
| 424 | GrPerspQuad::MakeFromRect(quads[0].fRect, viewMatrix), deviceQuadType, |
| 425 | GrPerspQuad::MakeFromRect(quads[0].fRect, quads[0].fLocalMatrix), |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 426 | GrQuadTypeForTransformedRect(quads[0].fLocalMatrix)); |
| 427 | auto* fillRects = op->cast<FillRectOp>(); |
| 428 | |
| 429 | // Accumulate remaining quads similar to onCombineIfPossible() without creating an op |
| 430 | for (int i = 1; i < cnt; ++i) { |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 431 | GrPerspQuad deviceQuad = GrPerspQuad::MakeFromRect(quads[i].fRect, viewMatrix); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 432 | |
| 433 | GrAAType resolvedAA; |
| 434 | GrQuadAAFlags resolvedEdgeFlags; |
| 435 | GrResolveAATypeForQuad(aaType, quads[i].fAAFlags, deviceQuad, deviceQuadType, |
| 436 | &resolvedAA, &resolvedEdgeFlags); |
| 437 | |
Michael Ludwig | e9c57d3 | 2019-02-13 13:39:39 -0500 | [diff] [blame] | 438 | fillRects->addQuad(deviceQuad, |
| 439 | GrPerspQuad::MakeFromRect(quads[i].fRect, quads[i].fLocalMatrix), |
Michael Ludwig | c96fc37 | 2019-01-08 15:46:15 -0500 | [diff] [blame] | 440 | GrQuadTypeForTransformedRect(quads[i].fLocalMatrix), quads[i].fColor, |
| 441 | resolvedEdgeFlags,resolvedAA); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | return op; |
| 445 | } |
| 446 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 447 | std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 448 | GrPaint&& paint, |
| 449 | GrAAType aaType, |
| 450 | const SkMatrix& viewMatrix, |
| 451 | const SkRect& rect, |
| 452 | const GrUserStencilSettings* stencil) { |
| 453 | return MakePerEdge(context, std::move(paint), aaType, |
| 454 | aaType == GrAAType::kCoverage ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone, |
| 455 | viewMatrix, rect, stencil); |
| 456 | } |
| 457 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 458 | std::unique_ptr<GrDrawOp> MakeWithLocalMatrix(GrRecordingContext* context, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 459 | GrPaint&& paint, |
| 460 | GrAAType aaType, |
| 461 | const SkMatrix& viewMatrix, |
| 462 | const SkMatrix& localMatrix, |
| 463 | const SkRect& rect, |
| 464 | const GrUserStencilSettings* stencil) { |
| 465 | return MakePerEdgeWithLocalMatrix(context, std::move(paint), aaType, |
| 466 | aaType == GrAAType::kCoverage ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone, |
| 467 | viewMatrix, localMatrix, rect, stencil); |
| 468 | } |
| 469 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 470 | std::unique_ptr<GrDrawOp> MakeWithLocalRect(GrRecordingContext* context, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 471 | GrPaint&& paint, |
| 472 | GrAAType aaType, |
| 473 | const SkMatrix& viewMatrix, |
| 474 | const SkRect& rect, |
| 475 | const SkRect& localRect, |
| 476 | const GrUserStencilSettings* stencil) { |
| 477 | return MakePerEdgeWithLocalRect(context, std::move(paint), aaType, |
| 478 | aaType == GrAAType::kCoverage ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone, |
| 479 | viewMatrix, rect, localRect, stencil); |
| 480 | } |
| 481 | |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 482 | } // namespace GrFillRectOp |
| 483 | |
| 484 | #if GR_TEST_UTILS |
| 485 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame^] | 486 | #include "src/gpu/GrDrawOpTest.h" |
| 487 | #include "src/gpu/SkGr.h" |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 488 | |
| 489 | GR_DRAW_OP_TEST_DEFINE(FillRectOp) { |
| 490 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 491 | SkRect rect = GrTest::TestRect(random); |
| 492 | |
| 493 | GrAAType aaType = GrAAType::kNone; |
| 494 | if (random->nextBool()) { |
| 495 | aaType = (fsaaType == GrFSAAType::kUnifiedMSAA) ? GrAAType::kMSAA : GrAAType::kCoverage; |
| 496 | } |
| 497 | const GrUserStencilSettings* stencil = random->nextBool() ? nullptr |
| 498 | : GrGetRandomStencil(random, context); |
| 499 | |
| 500 | GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone; |
| 501 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone; |
| 502 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone; |
| 503 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone; |
| 504 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone; |
| 505 | |
| 506 | if (random->nextBool()) { |
| 507 | if (random->nextBool()) { |
| 508 | if (random->nextBool()) { |
| 509 | // Local matrix with a set op |
| 510 | uint32_t extraQuadCt = random->nextRangeU(1, 4); |
| 511 | SkTArray<GrRenderTargetContext::QuadSetEntry> quads(extraQuadCt + 1); |
| 512 | quads.push_back( |
| 513 | {rect, SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU())), |
| 514 | GrTest::TestMatrixInvertible(random), aaFlags}); |
| 515 | for (uint32_t i = 0; i < extraQuadCt; ++i) { |
| 516 | GrQuadAAFlags aaFlags = GrQuadAAFlags::kNone; |
| 517 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kLeft : GrQuadAAFlags::kNone; |
| 518 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kTop : GrQuadAAFlags::kNone; |
| 519 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kRight : GrQuadAAFlags::kNone; |
| 520 | aaFlags |= random->nextBool() ? GrQuadAAFlags::kBottom : GrQuadAAFlags::kNone; |
| 521 | |
| 522 | quads.push_back( |
| 523 | {GrTest::TestRect(random), |
| 524 | SkPMColor4f::FromBytes_RGBA(SkColorToPremulGrColor(random->nextU())), |
| 525 | GrTest::TestMatrixInvertible(random), aaFlags}); |
| 526 | } |
| 527 | |
| 528 | return GrFillRectOp::MakeSet(context, std::move(paint), aaType, viewMatrix, |
| 529 | quads.begin(), quads.count(), stencil); |
| 530 | } else { |
| 531 | // Single local matrix |
| 532 | SkMatrix localMatrix = GrTest::TestMatrixInvertible(random); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 533 | return GrFillRectOp::MakePerEdgeWithLocalMatrix(context, std::move(paint), aaType, |
| 534 | aaFlags, viewMatrix, localMatrix, |
| 535 | rect, stencil); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 536 | } |
| 537 | } else { |
| 538 | // Pass local rect directly |
| 539 | SkRect localRect = GrTest::TestRect(random); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 540 | return GrFillRectOp::MakePerEdgeWithLocalRect(context, std::move(paint), aaType, |
| 541 | aaFlags, viewMatrix, rect, localRect, |
| 542 | stencil); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 543 | } |
| 544 | } else { |
| 545 | // The simplest constructor |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 546 | return GrFillRectOp::MakePerEdge(context, std::move(paint), aaType, aaFlags, viewMatrix, |
| 547 | rect, stencil); |
Michael Ludwig | 6985853 | 2018-11-28 15:34:34 -0500 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
| 551 | #endif |