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