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