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