Chris Dalton | 133944a | 2018-11-16 23:30:29 -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/GrFillRRectOp.h" |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 9 | |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 10 | #include "include/gpu/GrRecordingContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/core/SkRRectPriv.h" |
| 12 | #include "src/gpu/GrCaps.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrMemoryPool.h" |
| 14 | #include "src/gpu/GrOpFlushState.h" |
Greg Daniel | 2d41d0d | 2019-08-26 11:08:51 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrOpsRenderPass.h" |
Robert Phillips | 901aff0 | 2019-10-08 12:32:56 -0400 | [diff] [blame] | 16 | #include "src/gpu/GrProgramInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrRecordingContextPriv.h" |
Robert Phillips | 1a82a4e | 2021-07-01 10:27:44 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrResourceProvider.h" |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 19 | #include "src/gpu/GrVertexWriter.h" |
| 20 | #include "src/gpu/GrVx.h" |
Chris Dalton | ea46ef3 | 2021-07-12 15:09:06 -0600 | [diff] [blame] | 21 | #include "src/gpu/geometry/GrShape.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 23 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 24 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 25 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 26 | #include "src/gpu/ops/GrMeshDrawOp.h" |
Robert Phillips | ce97857 | 2020-02-28 11:56:44 -0500 | [diff] [blame] | 27 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 28 | |
| 29 | namespace { |
| 30 | |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 31 | class FillRRectOp : public GrMeshDrawOp { |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 32 | private: |
| 33 | using Helper = GrSimpleMeshDrawOpHelper; |
| 34 | |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 35 | public: |
| 36 | DEFINE_OP_CLASS_ID |
| 37 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 38 | static GrOp::Owner Make(GrRecordingContext*, |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 39 | SkArenaAlloc*, |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 40 | GrPaint&&, |
| 41 | const SkMatrix& viewMatrix, |
| 42 | const SkRRect&, |
Chris Dalton | 61d694d | 2021-03-22 00:00:52 -0600 | [diff] [blame] | 43 | const SkRect& localRect, |
Chris Dalton | 4f44734 | 2021-03-12 12:09:12 -0700 | [diff] [blame] | 44 | GrAA); |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 45 | |
| 46 | const char* name() const final { return "GrFillRRectOp"; } |
| 47 | |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 48 | FixedFunctionFlags fixedFunctionFlags() const final { return fHelper.fixedFunctionFlags(); } |
| 49 | |
Chris Dalton | ea46ef3 | 2021-07-12 15:09:06 -0600 | [diff] [blame] | 50 | ClipResult clipToShape(GrSurfaceDrawContext*, SkClipOp, const SkMatrix& clipMatrix, |
| 51 | const GrShape&, GrAA) override; |
| 52 | |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 53 | GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*, GrClampType) final; |
Herb Derby | e25c300 | 2020-10-27 15:57:27 -0400 | [diff] [blame] | 54 | CombineResult onCombineIfPossible(GrOp*, SkArenaAlloc*, const GrCaps&) final; |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 55 | |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 56 | void visitProxies(const GrVisitProxyFunc& func) const override { |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 57 | if (fProgramInfo) { |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 58 | fProgramInfo->visitFPProxies(func); |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 59 | } else { |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 60 | fHelper.visitProxies(func); |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Robert Phillips | 7114395 | 2021-06-17 14:55:07 -0400 | [diff] [blame] | 64 | void onPrepareDraws(GrMeshDrawTarget*) final; |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 65 | |
| 66 | void onExecute(GrOpFlushState*, const SkRect& chainBounds) final; |
| 67 | |
| 68 | private: |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 69 | friend class ::GrSimpleMeshDrawOpHelper; // for access to ctor |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 70 | friend class ::GrOp; // for access to ctor |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 71 | |
| 72 | enum class ProcessorFlags { |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 73 | kNone = 0, |
| 74 | kUseHWDerivatives = 1 << 0, |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 75 | kHasLocalCoords = 1 << 1, |
| 76 | kWideColor = 1 << 2, |
| 77 | kMSAAEnabled = 1 << 3, |
| 78 | kFakeNonAA = 1 << 4, |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 79 | }; |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 80 | constexpr static int kNumProcessorFlags = 5; |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 81 | |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 82 | GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(ProcessorFlags); |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 83 | |
| 84 | class Processor; |
| 85 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 86 | FillRRectOp(GrProcessorSet*, |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 87 | const SkPMColor4f& paintColor, |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 88 | SkArenaAlloc*, |
| 89 | const SkMatrix& viewMatrix, |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 90 | const SkRRect&, |
Chris Dalton | 61d694d | 2021-03-22 00:00:52 -0600 | [diff] [blame] | 91 | const SkRect& localRect, |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 92 | ProcessorFlags); |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 93 | |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 94 | GrProgramInfo* programInfo() final { return fProgramInfo; } |
| 95 | |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 96 | // Create a GrProgramInfo object in the provided arena |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 97 | void onCreateProgramInfo(const GrCaps*, |
| 98 | SkArenaAlloc*, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 99 | const GrSurfaceProxyView& writeView, |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 100 | GrAppliedClip&&, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame] | 101 | const GrDstProxyView&, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 102 | GrXferBarrierFlags renderPassXferBarriers, |
| 103 | GrLoadOp colorLoadOp) final; |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 104 | |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 105 | Helper fHelper; |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 106 | ProcessorFlags fProcessorFlags; |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 107 | |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 108 | struct Instance { |
| 109 | Instance(const SkMatrix& viewMatrix, const SkRRect& rrect, const SkRect& localRect, |
| 110 | const SkPMColor4f& color) |
| 111 | : fViewMatrix(viewMatrix), fRRect(rrect), fLocalRect(localRect), fColor(color) {} |
| 112 | SkMatrix fViewMatrix; |
| 113 | SkRRect fRRect; |
| 114 | SkRect fLocalRect; |
| 115 | SkPMColor4f fColor; |
| 116 | Instance* fNext = nullptr; |
| 117 | }; |
| 118 | |
| 119 | Instance* fHeadInstance; |
| 120 | Instance** fTailInstance; |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 121 | int fInstanceCount = 1; |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 122 | |
| 123 | sk_sp<const GrBuffer> fInstanceBuffer; |
| 124 | sk_sp<const GrBuffer> fVertexBuffer; |
| 125 | sk_sp<const GrBuffer> fIndexBuffer; |
| 126 | int fBaseInstance = 0; |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 127 | |
| 128 | // If this op is prePrepared the created programInfo will be stored here for use in |
| 129 | // onExecute. In the prePrepared case it will have been stored in the record-time arena. |
| 130 | GrProgramInfo* fProgramInfo = nullptr; |
| 131 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 132 | using INHERITED = GrMeshDrawOp; |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 133 | }; |
| 134 | |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 135 | GR_MAKE_BITFIELD_CLASS_OPS(FillRRectOp::ProcessorFlags) |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 136 | |
| 137 | // Hardware derivatives are not always accurate enough for highly elliptical corners. This method |
| 138 | // checks to make sure the corners will still all look good if we use HW derivatives. |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 139 | static bool can_use_hw_derivatives_with_coverage(const GrShaderCaps&, |
| 140 | const SkMatrix&, |
| 141 | const SkRRect&); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 142 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 143 | GrOp::Owner FillRRectOp::Make(GrRecordingContext* ctx, |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 144 | SkArenaAlloc* arena, |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 145 | GrPaint&& paint, |
| 146 | const SkMatrix& viewMatrix, |
| 147 | const SkRRect& rrect, |
Chris Dalton | 61d694d | 2021-03-22 00:00:52 -0600 | [diff] [blame] | 148 | const SkRect& localRect, |
Chris Dalton | 4f44734 | 2021-03-12 12:09:12 -0700 | [diff] [blame] | 149 | GrAA aa) { |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 150 | using Helper = GrSimpleMeshDrawOpHelper; |
| 151 | |
| 152 | const GrCaps* caps = ctx->priv().caps(); |
| 153 | |
Chris Dalton | a77cdee | 2020-04-03 14:50:43 -0600 | [diff] [blame] | 154 | if (!caps->drawInstancedSupport()) { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 155 | return nullptr; |
| 156 | } |
| 157 | |
Chris Dalton | 61d694d | 2021-03-22 00:00:52 -0600 | [diff] [blame] | 158 | // We transform into a normalized -1..+1 space to draw the round rect. If the boundaries are too |
| 159 | // large, the math can overflow. The caller can fall back on path rendering if this is the case. |
| 160 | if (std::max(rrect.height(), rrect.width()) >= 1e6f) { |
| 161 | return nullptr; |
| 162 | } |
| 163 | |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 164 | ProcessorFlags flags = ProcessorFlags::kNone; |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 165 | // TODO: Support perspective in a follow-on CL. This shouldn't be difficult, since we already |
| 166 | // use HW derivatives. The only trick will be adjusting the AA outset to account for |
| 167 | // perspective. (i.e., outset = 0.5 * z.) |
| 168 | if (viewMatrix.hasPerspective()) { |
| 169 | return nullptr; |
| 170 | } |
| 171 | if (can_use_hw_derivatives_with_coverage(*caps->shaderCaps(), viewMatrix, rrect)) { |
| 172 | // HW derivatives (more specifically, fwidth()) are consistently faster on all platforms in |
| 173 | // coverage mode. We use them as long as the approximation will be accurate enough. |
| 174 | flags |= ProcessorFlags::kUseHWDerivatives; |
| 175 | } |
Chris Dalton | 4f44734 | 2021-03-12 12:09:12 -0700 | [diff] [blame] | 176 | if (aa == GrAA::kNo) { |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 177 | flags |= ProcessorFlags::kFakeNonAA; |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 178 | } |
| 179 | |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 180 | return Helper::FactoryHelper<FillRRectOp>(ctx, std::move(paint), arena, viewMatrix, rrect, |
| 181 | localRect, flags); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 182 | } |
| 183 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 184 | FillRRectOp::FillRRectOp(GrProcessorSet* processorSet, |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 185 | const SkPMColor4f& paintColor, |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 186 | SkArenaAlloc* arena, |
| 187 | const SkMatrix& viewMatrix, |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 188 | const SkRRect& rrect, |
Chris Dalton | 61d694d | 2021-03-22 00:00:52 -0600 | [diff] [blame] | 189 | const SkRect& localRect, |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 190 | ProcessorFlags processorFlags) |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 191 | : INHERITED(ClassID()) |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 192 | , fHelper(processorSet, |
| 193 | (processorFlags & ProcessorFlags::kFakeNonAA) |
| 194 | ? GrAAType::kNone |
| 195 | : GrAAType::kCoverage) // Use analytic AA even if the RT is MSAA. |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 196 | , fProcessorFlags(processorFlags & ~(ProcessorFlags::kHasLocalCoords | |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 197 | ProcessorFlags::kWideColor | |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 198 | ProcessorFlags::kMSAAEnabled)) |
| 199 | , fHeadInstance(arena->make<Instance>(viewMatrix, rrect, localRect, paintColor)) |
| 200 | , fTailInstance(&fHeadInstance->fNext) { |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 201 | // FillRRectOp::Make fails if there is perspective. |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 202 | SkASSERT(!viewMatrix.hasPerspective()); |
| 203 | this->setBounds(viewMatrix.mapRect(rrect.getBounds()), |
| 204 | GrOp::HasAABloat(!(processorFlags & ProcessorFlags::kFakeNonAA)), |
Chris Dalton | 4f44734 | 2021-03-12 12:09:12 -0700 | [diff] [blame] | 205 | GrOp::IsHairline::kNo); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 206 | } |
| 207 | |
Chris Dalton | ea46ef3 | 2021-07-12 15:09:06 -0600 | [diff] [blame] | 208 | GrDrawOp::ClipResult FillRRectOp::clipToShape(GrSurfaceDrawContext* sdc, SkClipOp clipOp, |
| 209 | const SkMatrix& clipMatrix, const GrShape& shape, |
| 210 | GrAA aa) { |
| 211 | SkASSERT(fInstanceCount == 1); // This needs to be called before combining. |
| 212 | SkASSERT(fHeadInstance->fNext == nullptr); |
| 213 | |
| 214 | if ((shape.isRect() || shape.isRRect()) && |
| 215 | clipOp == SkClipOp::kIntersect && |
| 216 | (aa == GrAA::kNo) == (fProcessorFlags & ProcessorFlags::kFakeNonAA)) { |
| 217 | // The clip shape is a round rect. Attempt to map it to a round rect in "viewMatrix" space. |
| 218 | SkRRect clipRRect; |
| 219 | if (clipMatrix == fHeadInstance->fViewMatrix) { |
| 220 | if (shape.isRect()) { |
| 221 | clipRRect.setRect(shape.rect()); |
| 222 | } else { |
| 223 | clipRRect = shape.rrect(); |
| 224 | } |
| 225 | } else { |
| 226 | // Find a matrix that maps from "clipMatrix" space to "viewMatrix" space. |
| 227 | SkASSERT(!fHeadInstance->fViewMatrix.hasPerspective()); |
| 228 | if (clipMatrix.hasPerspective()) { |
| 229 | return ClipResult::kFail; |
| 230 | } |
| 231 | SkMatrix clipToView; |
| 232 | if (!fHeadInstance->fViewMatrix.invert(&clipToView)) { |
| 233 | return ClipResult::kClippedOut; |
| 234 | } |
| 235 | clipToView.preConcat(clipMatrix); |
| 236 | SkASSERT(!clipToView.hasPerspective()); |
| 237 | if (!SkScalarNearlyZero(clipToView.getSkewX()) || |
| 238 | !SkScalarNearlyZero(clipToView.getSkewY())) { |
| 239 | // A rect in "clipMatrix" space is not a rect in "viewMatrix" space. |
| 240 | return ClipResult::kFail; |
| 241 | } |
| 242 | clipToView.setSkewX(0); |
| 243 | clipToView.setSkewY(0); |
| 244 | SkASSERT(clipToView.rectStaysRect()); |
| 245 | |
| 246 | if (shape.isRect()) { |
| 247 | clipRRect.setRect(clipToView.mapRect(shape.rect())); |
| 248 | } else { |
| 249 | if (!shape.rrect().transform(clipToView, &clipRRect)) { |
| 250 | // Transforming the rrect failed. This shouldn't generally happen except in |
| 251 | // cases of fp32 overflow. |
| 252 | return ClipResult::kFail; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // Intersect our round rect with the clip shape. |
| 258 | SkRRect isectRRect; |
| 259 | if (fHeadInstance->fRRect.isRect() && clipRRect.isRect()) { |
| 260 | SkRect isectRect; |
| 261 | if (!isectRect.intersect(fHeadInstance->fRRect.rect(), clipRRect.rect())) { |
| 262 | return ClipResult::kClippedOut; |
| 263 | } |
| 264 | isectRRect.setRect(isectRect); |
| 265 | } else { |
| 266 | isectRRect = SkRRectPriv::ConservativeIntersect(fHeadInstance->fRRect, clipRRect); |
| 267 | if (isectRRect.isEmpty()) { |
| 268 | // The round rects did not intersect at all or the intersection was too complicated |
| 269 | // to compute quickly. |
| 270 | return ClipResult::kFail; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | // Update the local rect. |
| 275 | auto rect = skvx::bit_pun<grvx::float4>(fHeadInstance->fRRect.rect()); |
| 276 | auto local = skvx::bit_pun<grvx::float4>(fHeadInstance->fLocalRect); |
| 277 | auto isect = skvx::bit_pun<grvx::float4>(isectRRect.rect()); |
| 278 | auto rectToLocalSize = (local - skvx::shuffle<2,3,0,1>(local)) / |
| 279 | (rect - skvx::shuffle<2,3,0,1>(rect)); |
| 280 | fHeadInstance->fLocalRect = skvx::bit_pun<SkRect>((isect - rect) * rectToLocalSize + local); |
| 281 | |
| 282 | // Update the round rect. |
| 283 | fHeadInstance->fRRect = isectRRect; |
| 284 | return ClipResult::kClippedGeometrically; |
| 285 | } |
| 286 | |
| 287 | return ClipResult::kFail; |
| 288 | } |
| 289 | |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 290 | GrProcessorSet::Analysis FillRRectOp::finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 291 | GrClampType clampType) { |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 292 | SkASSERT(fInstanceCount == 1); |
| 293 | SkASSERT(fHeadInstance->fNext == nullptr); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 294 | |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 295 | bool isWideColor; |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 296 | auto analysis = fHelper.finalizeProcessors(caps, clip, clampType, |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 297 | GrProcessorAnalysisCoverage::kSingleChannel, |
| 298 | &fHeadInstance->fColor, &isWideColor); |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 299 | if (isWideColor) { |
| 300 | fProcessorFlags |= ProcessorFlags::kWideColor; |
Brian Osman | 5105d68 | 2019-02-13 16:06:14 -0500 | [diff] [blame] | 301 | } |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 302 | if (analysis.usesLocalCoords()) { |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 303 | fProcessorFlags |= ProcessorFlags::kHasLocalCoords; |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 304 | } |
Chris Dalton | 4b62aed | 2019-01-15 11:53:00 -0700 | [diff] [blame] | 305 | return analysis; |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 306 | } |
| 307 | |
Herb Derby | e25c300 | 2020-10-27 15:57:27 -0400 | [diff] [blame] | 308 | GrOp::CombineResult FillRRectOp::onCombineIfPossible(GrOp* op, SkArenaAlloc*, const GrCaps& caps) { |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 309 | const auto& that = *op->cast<FillRRectOp>(); |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 310 | if (!fHelper.isCompatible(that.fHelper, caps, this->bounds(), that.bounds()) || |
| 311 | fProcessorFlags != that.fProcessorFlags) { |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 312 | return CombineResult::kCannotCombine; |
| 313 | } |
| 314 | |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 315 | *fTailInstance = that.fHeadInstance; |
| 316 | fTailInstance = that.fTailInstance; |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 317 | fInstanceCount += that.fInstanceCount; |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 318 | return CombineResult::kMerged; |
| 319 | } |
| 320 | |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 321 | class FillRRectOp::Processor : public GrGeometryProcessor { |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 322 | public: |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 323 | static GrGeometryProcessor* Make(SkArenaAlloc* arena, GrAAType aaType, ProcessorFlags flags) { |
Mike Klein | f124108 | 2020-12-14 15:59:09 -0600 | [diff] [blame] | 324 | return arena->make([&](void* ptr) { |
| 325 | return new (ptr) Processor(aaType, flags); |
| 326 | }); |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 327 | } |
| 328 | |
Robert Phillips | 8053c97 | 2019-11-21 10:44:53 -0500 | [diff] [blame] | 329 | const char* name() const final { return "GrFillRRectOp::Processor"; } |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 330 | |
Robert Phillips | 8053c97 | 2019-11-21 10:44:53 -0500 | [diff] [blame] | 331 | void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const final { |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 332 | b->addBits(kNumProcessorFlags, (uint32_t)fFlags, "flags"); |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 333 | } |
| 334 | |
Robert Phillips | f10535f | 2021-03-23 09:30:45 -0400 | [diff] [blame] | 335 | GrGLSLGeometryProcessor* createGLSLInstance(const GrShaderCaps&) const final; |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 336 | |
| 337 | private: |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 338 | Processor(GrAAType aaType, ProcessorFlags flags) |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 339 | : INHERITED(kGrFillRRectOp_Processor_ClassID) |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 340 | , fFlags(flags) { |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 341 | this->setVertexAttributes(kVertexAttribs, SK_ARRAY_COUNT(kVertexAttribs)); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 342 | |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 343 | fInstanceAttribs.emplace_back("skew", kFloat4_GrVertexAttribType, kFloat4_GrSLType); |
| 344 | fInstanceAttribs.emplace_back("translate", kFloat2_GrVertexAttribType, kFloat2_GrSLType); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 345 | fInstanceAttribs.emplace_back("radii_x", kFloat4_GrVertexAttribType, kFloat4_GrSLType); |
| 346 | fInstanceAttribs.emplace_back("radii_y", kFloat4_GrVertexAttribType, kFloat4_GrSLType); |
| 347 | fColorAttrib = &fInstanceAttribs.push_back( |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 348 | MakeColorAttribute("color", (fFlags & ProcessorFlags::kWideColor))); |
| 349 | if (fFlags & ProcessorFlags::kHasLocalCoords) { |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 350 | fInstanceAttribs.emplace_back( |
| 351 | "local_rect", kFloat4_GrVertexAttribType, kFloat4_GrSLType); |
| 352 | } |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 353 | SkASSERT(fInstanceAttribs.count() <= kMaxInstanceAttribs); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 354 | this->setInstanceAttributes(fInstanceAttribs.begin(), fInstanceAttribs.count()); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 355 | } |
| 356 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 357 | static constexpr Attribute kVertexAttribs[] = { |
| 358 | {"radii_selector", kFloat4_GrVertexAttribType, kFloat4_GrSLType}, |
| 359 | {"corner_and_radius_outsets", kFloat4_GrVertexAttribType, kFloat4_GrSLType}, |
| 360 | // Coverage only. |
| 361 | {"aa_bloat_and_coverage", kFloat4_GrVertexAttribType, kFloat4_GrSLType}}; |
| 362 | |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 363 | const ProcessorFlags fFlags; |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 364 | |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 365 | constexpr static int kMaxInstanceAttribs = 6; |
| 366 | SkSTArray<kMaxInstanceAttribs, Attribute> fInstanceAttribs; |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 367 | const Attribute* fColorAttrib; |
| 368 | |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 369 | class Impl; |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 370 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 371 | using INHERITED = GrGeometryProcessor; |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 372 | }; |
| 373 | |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 374 | constexpr GrGeometryProcessor::Attribute FillRRectOp::Processor::kVertexAttribs[]; |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 375 | |
| 376 | // Our coverage geometry consists of an inset octagon with solid coverage, surrounded by linear |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 377 | // coverage ramps on the horizontal and vertical edges, and "arc coverage" pieces on the diagonal |
| 378 | // edges. The Vertex struct tells the shader where to place its vertex within a normalized |
| 379 | // ([l, t, r, b] = [-1, -1, +1, +1]) space, and how to calculate coverage. See onEmitCode. |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 380 | struct CoverageVertex { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 381 | std::array<float, 4> fRadiiSelector; |
| 382 | std::array<float, 2> fCorner; |
| 383 | std::array<float, 2> fRadiusOutset; |
| 384 | std::array<float, 2> fAABloatDirection; |
| 385 | float fCoverage; |
| 386 | float fIsLinearCoverage; |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 387 | }; |
| 388 | |
| 389 | // This is the offset (when multiplied by radii) from the corners of a bounding box to the vertices |
| 390 | // of its inscribed octagon. We draw the outside portion of arcs with quarter-octagons rather than |
| 391 | // rectangles. |
| 392 | static constexpr float kOctoOffset = 1/(1 + SK_ScalarRoot2Over2); |
| 393 | |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 394 | static constexpr CoverageVertex kVertexData[] = { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 395 | // Left inset edge. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 396 | {{{0,0,0,1}}, {{-1,+1}}, {{0,-1}}, {{+1,0}}, 1, 1}, |
| 397 | {{{1,0,0,0}}, {{-1,-1}}, {{0,+1}}, {{+1,0}}, 1, 1}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 398 | |
| 399 | // Top inset edge. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 400 | {{{1,0,0,0}}, {{-1,-1}}, {{+1,0}}, {{0,+1}}, 1, 1}, |
| 401 | {{{0,1,0,0}}, {{+1,-1}}, {{-1,0}}, {{0,+1}}, 1, 1}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 402 | |
| 403 | // Right inset edge. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 404 | {{{0,1,0,0}}, {{+1,-1}}, {{0,+1}}, {{-1,0}}, 1, 1}, |
| 405 | {{{0,0,1,0}}, {{+1,+1}}, {{0,-1}}, {{-1,0}}, 1, 1}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 406 | |
| 407 | // Bottom inset edge. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 408 | {{{0,0,1,0}}, {{+1,+1}}, {{-1,0}}, {{0,-1}}, 1, 1}, |
| 409 | {{{0,0,0,1}}, {{-1,+1}}, {{+1,0}}, {{0,-1}}, 1, 1}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 410 | |
| 411 | |
| 412 | // Left outset edge. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 413 | {{{0,0,0,1}}, {{-1,+1}}, {{0,-1}}, {{-1,0}}, 0, 1}, |
| 414 | {{{1,0,0,0}}, {{-1,-1}}, {{0,+1}}, {{-1,0}}, 0, 1}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 415 | |
| 416 | // Top outset edge. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 417 | {{{1,0,0,0}}, {{-1,-1}}, {{+1,0}}, {{0,-1}}, 0, 1}, |
| 418 | {{{0,1,0,0}}, {{+1,-1}}, {{-1,0}}, {{0,-1}}, 0, 1}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 419 | |
| 420 | // Right outset edge. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 421 | {{{0,1,0,0}}, {{+1,-1}}, {{0,+1}}, {{+1,0}}, 0, 1}, |
| 422 | {{{0,0,1,0}}, {{+1,+1}}, {{0,-1}}, {{+1,0}}, 0, 1}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 423 | |
| 424 | // Bottom outset edge. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 425 | {{{0,0,1,0}}, {{+1,+1}}, {{-1,0}}, {{0,+1}}, 0, 1}, |
| 426 | {{{0,0,0,1}}, {{-1,+1}}, {{+1,0}}, {{0,+1}}, 0, 1}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 427 | |
| 428 | |
| 429 | // Top-left corner. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 430 | {{{1,0,0,0}}, {{-1,-1}}, {{ 0,+1}}, {{-1, 0}}, 0, 0}, |
| 431 | {{{1,0,0,0}}, {{-1,-1}}, {{ 0,+1}}, {{+1, 0}}, 1, 0}, |
| 432 | {{{1,0,0,0}}, {{-1,-1}}, {{+1, 0}}, {{ 0,+1}}, 1, 0}, |
| 433 | {{{1,0,0,0}}, {{-1,-1}}, {{+1, 0}}, {{ 0,-1}}, 0, 0}, |
| 434 | {{{1,0,0,0}}, {{-1,-1}}, {{+kOctoOffset,0}}, {{-1,-1}}, 0, 0}, |
| 435 | {{{1,0,0,0}}, {{-1,-1}}, {{0,+kOctoOffset}}, {{-1,-1}}, 0, 0}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 436 | |
| 437 | // Top-right corner. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 438 | {{{0,1,0,0}}, {{+1,-1}}, {{-1, 0}}, {{ 0,-1}}, 0, 0}, |
| 439 | {{{0,1,0,0}}, {{+1,-1}}, {{-1, 0}}, {{ 0,+1}}, 1, 0}, |
| 440 | {{{0,1,0,0}}, {{+1,-1}}, {{ 0,+1}}, {{-1, 0}}, 1, 0}, |
| 441 | {{{0,1,0,0}}, {{+1,-1}}, {{ 0,+1}}, {{+1, 0}}, 0, 0}, |
| 442 | {{{0,1,0,0}}, {{+1,-1}}, {{0,+kOctoOffset}}, {{+1,-1}}, 0, 0}, |
| 443 | {{{0,1,0,0}}, {{+1,-1}}, {{-kOctoOffset,0}}, {{+1,-1}}, 0, 0}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 444 | |
| 445 | // Bottom-right corner. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 446 | {{{0,0,1,0}}, {{+1,+1}}, {{ 0,-1}}, {{+1, 0}}, 0, 0}, |
| 447 | {{{0,0,1,0}}, {{+1,+1}}, {{ 0,-1}}, {{-1, 0}}, 1, 0}, |
| 448 | {{{0,0,1,0}}, {{+1,+1}}, {{-1, 0}}, {{ 0,-1}}, 1, 0}, |
| 449 | {{{0,0,1,0}}, {{+1,+1}}, {{-1, 0}}, {{ 0,+1}}, 0, 0}, |
| 450 | {{{0,0,1,0}}, {{+1,+1}}, {{-kOctoOffset,0}}, {{+1,+1}}, 0, 0}, |
| 451 | {{{0,0,1,0}}, {{+1,+1}}, {{0,-kOctoOffset}}, {{+1,+1}}, 0, 0}, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 452 | |
| 453 | // Bottom-left corner. |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 454 | {{{0,0,0,1}}, {{-1,+1}}, {{+1, 0}}, {{ 0,+1}}, 0, 0}, |
| 455 | {{{0,0,0,1}}, {{-1,+1}}, {{+1, 0}}, {{ 0,-1}}, 1, 0}, |
| 456 | {{{0,0,0,1}}, {{-1,+1}}, {{ 0,-1}}, {{+1, 0}}, 1, 0}, |
| 457 | {{{0,0,0,1}}, {{-1,+1}}, {{ 0,-1}}, {{-1, 0}}, 0, 0}, |
Chris Dalton | 2d07e86 | 2018-11-26 12:30:47 -0700 | [diff] [blame] | 458 | {{{0,0,0,1}}, {{-1,+1}}, {{0,-kOctoOffset}}, {{-1,+1}}, 0, 0}, |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 459 | {{{0,0,0,1}}, {{-1,+1}}, {{+kOctoOffset,0}}, {{-1,+1}}, 0, 0}}; |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 460 | |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 461 | GR_DECLARE_STATIC_UNIQUE_KEY(gVertexBufferKey); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 462 | |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 463 | static constexpr uint16_t kIndexData[] = { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 464 | // Inset octagon (solid coverage). |
| 465 | 0, 1, 7, |
| 466 | 1, 2, 7, |
| 467 | 7, 2, 6, |
| 468 | 2, 3, 6, |
| 469 | 6, 3, 5, |
| 470 | 3, 4, 5, |
| 471 | |
| 472 | // AA borders (linear coverage). |
| 473 | 0, 1, 8, 1, 9, 8, |
| 474 | 2, 3, 10, 3, 11, 10, |
| 475 | 4, 5, 12, 5, 13, 12, |
| 476 | 6, 7, 14, 7, 15, 14, |
| 477 | |
| 478 | // Top-left arc. |
| 479 | 16, 17, 21, |
| 480 | 17, 21, 18, |
| 481 | 21, 18, 20, |
| 482 | 18, 20, 19, |
| 483 | |
| 484 | // Top-right arc. |
| 485 | 22, 23, 27, |
| 486 | 23, 27, 24, |
| 487 | 27, 24, 26, |
| 488 | 24, 26, 25, |
| 489 | |
| 490 | // Bottom-right arc. |
| 491 | 28, 29, 33, |
| 492 | 29, 33, 30, |
| 493 | 33, 30, 32, |
| 494 | 30, 32, 31, |
| 495 | |
| 496 | // Bottom-left arc. |
| 497 | 34, 35, 39, |
| 498 | 35, 39, 36, |
| 499 | 39, 36, 38, |
| 500 | 36, 38, 37}; |
| 501 | |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 502 | GR_DECLARE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 503 | |
Robert Phillips | 7114395 | 2021-06-17 14:55:07 -0400 | [diff] [blame] | 504 | void FillRRectOp::onPrepareDraws(GrMeshDrawTarget* target) { |
Chris Dalton | cc29a39 | 2021-07-12 15:16:29 -0600 | [diff] [blame] | 505 | if (target->usesMSAASurface()) { |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 506 | fProcessorFlags |= ProcessorFlags::kMSAAEnabled; |
| 507 | } |
| 508 | |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 509 | if (!fProgramInfo) { |
| 510 | this->createProgramInfo(target); |
| 511 | } |
| 512 | |
Chris Dalton | cc29a39 | 2021-07-12 15:16:29 -0600 | [diff] [blame] | 513 | // FIXME(skbug.com/12201): Our draw's MSAA state should match the render target, but DDL doesn't |
| 514 | // yet communicate DMSAA state to onPrePrepare. |
| 515 | SkASSERT(fProgramInfo->pipeline().isHWAntialiasState() == target->usesMSAASurface()); |
| 516 | |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 517 | size_t instanceStride = fProgramInfo->geomProc().instanceStride(); |
| 518 | |
| 519 | if (GrVertexWriter instanceWrter = target->makeVertexSpace(instanceStride, fInstanceCount, |
| 520 | &fInstanceBuffer, &fBaseInstance)) { |
| 521 | SkDEBUGCODE(auto end = instanceWrter.makeOffset(instanceStride * fInstanceCount)); |
| 522 | for (Instance* i = fHeadInstance; i; i = i->fNext) { |
| 523 | auto [l, t, r, b] = i->fRRect.rect(); |
| 524 | |
| 525 | // Produce a matrix that draws the round rect from normalized [-1, -1, +1, +1] space. |
| 526 | SkMatrix m; |
| 527 | // Unmap the normalized rect [-1, -1, +1, +1] back to [l, t, r, b]. |
| 528 | m.setScaleTranslate((r - l)/2, (b - t)/2, (l + r)/2, (t + b)/2); |
| 529 | // Map to device space. |
| 530 | m.postConcat(i->fViewMatrix); |
| 531 | |
| 532 | // Convert the radii to [-1, -1, +1, +1] space and write their attribs. |
| 533 | grvx::float4 radiiX, radiiY; |
| 534 | grvx::strided_load2(&SkRRectPriv::GetRadiiArray(i->fRRect)->fX, radiiX, radiiY); |
| 535 | radiiX *= 2 / (r - l); |
| 536 | radiiY *= 2 / (b - t); |
| 537 | |
| 538 | instanceWrter.write( |
| 539 | m.getScaleX(), m.getSkewX(), m.getSkewY(), m.getScaleY(), |
| 540 | m.getTranslateX(), m.getTranslateY(), |
| 541 | radiiX, |
| 542 | radiiY, |
| 543 | GrVertexColor(i->fColor, fProcessorFlags & ProcessorFlags::kWideColor), |
| 544 | GrVertexWriter::If(fProcessorFlags & ProcessorFlags::kHasLocalCoords, |
| 545 | i->fLocalRect)); |
| 546 | } |
| 547 | SkASSERT(instanceWrter == end); |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 548 | } |
| 549 | |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 550 | GR_DEFINE_STATIC_UNIQUE_KEY(gIndexBufferKey); |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 551 | |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 552 | fIndexBuffer = target->resourceProvider()->findOrMakeStaticBuffer(GrGpuBufferType::kIndex, |
| 553 | sizeof(kIndexData), |
| 554 | kIndexData, gIndexBufferKey); |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 555 | |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 556 | GR_DEFINE_STATIC_UNIQUE_KEY(gVertexBufferKey); |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 557 | |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 558 | fVertexBuffer = target->resourceProvider()->findOrMakeStaticBuffer(GrGpuBufferType::kVertex, |
| 559 | sizeof(kVertexData), |
| 560 | kVertexData, |
| 561 | gVertexBufferKey); |
Greg Daniel | f793de1 | 2019-09-05 13:23:23 -0400 | [diff] [blame] | 562 | } |
| 563 | |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 564 | class FillRRectOp::Processor::Impl : public GrGLSLGeometryProcessor { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 565 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
John Stiles | 4d7ac49 | 2021-03-09 20:16:43 -0500 | [diff] [blame] | 566 | GrGLSLVertexBuilder* v = args.fVertBuilder; |
| 567 | GrGLSLFPFragmentBuilder* f = args.fFragBuilder; |
| 568 | |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 569 | const auto& proc = args.fGeomProc.cast<Processor>(); |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 570 | bool useHWDerivatives = (proc.fFlags & ProcessorFlags::kUseHWDerivatives); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 571 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 572 | SkASSERT(proc.vertexStride() == sizeof(CoverageVertex)); |
| 573 | |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 574 | GrGLSLVaryingHandler* varyings = args.fVaryingHandler; |
| 575 | varyings->emitAttributes(proc); |
John Stiles | 4d7ac49 | 2021-03-09 20:16:43 -0500 | [diff] [blame] | 576 | f->codeAppendf("half4 %s;", args.fOutputColor); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 577 | varyings->addPassThroughAttribute(*proc.fColorAttrib, args.fOutputColor, |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 578 | GrGLSLVaryingHandler::Interpolation::kCanBeFlat); |
| 579 | |
| 580 | // Emit the vertex shader. |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 581 | // When MSAA is enabled, we need to make sure every sample gets lit up on pixels that have |
| 582 | // fractional coverage. We do this by making the ramp wider. |
| 583 | v->codeAppendf("float aa_bloat_multiplier = %i;", |
| 584 | (proc.fFlags & ProcessorFlags::kMSAAEnabled) |
| 585 | ? 2 // Outset an entire pixel (2 radii). |
| 586 | : (!(proc.fFlags & ProcessorFlags::kFakeNonAA)) |
| 587 | ? 1 // Outset one half pixel (1 radius). |
| 588 | : 0); // No AA bloat. |
| 589 | |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 590 | // Unpack vertex attribs. |
| 591 | v->codeAppend("float2 corner = corner_and_radius_outsets.xy;"); |
| 592 | v->codeAppend("float2 radius_outset = corner_and_radius_outsets.zw;"); |
| 593 | v->codeAppend("float2 aa_bloat_direction = aa_bloat_and_coverage.xy;"); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 594 | v->codeAppend("float is_linear_coverage = aa_bloat_and_coverage.w;"); |
| 595 | |
| 596 | // Find the amount to bloat each edge for AA (in source space). |
| 597 | v->codeAppend("float2 pixellength = inversesqrt(" |
| 598 | "float2(dot(skew.xz, skew.xz), dot(skew.yw, skew.yw)));"); |
| 599 | v->codeAppend("float4 normalized_axis_dirs = skew * pixellength.xyxy;"); |
| 600 | v->codeAppend("float2 axiswidths = (abs(normalized_axis_dirs.xy) + " |
| 601 | "abs(normalized_axis_dirs.zw));"); |
| 602 | v->codeAppend("float2 aa_bloatradius = axiswidths * pixellength * .5;"); |
| 603 | |
| 604 | // Identify our radii. |
Mike Reed | d3efa99 | 2018-11-28 13:13:15 +0000 | [diff] [blame] | 605 | v->codeAppend("float4 radii_and_neighbors = radii_selector" |
| 606 | "* float4x4(radii_x, radii_y, radii_x.yxwz, radii_y.wzyx);"); |
| 607 | v->codeAppend("float2 radii = radii_and_neighbors.xy;"); |
| 608 | v->codeAppend("float2 neighbor_radii = radii_and_neighbors.zw;"); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 609 | |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 610 | v->codeAppend("float coverage_multiplier = 1;"); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 611 | v->codeAppend("if (any(greaterThan(aa_bloatradius, float2(1)))) {"); |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 612 | // The rrect is more narrow than a half-pixel AA coverage ramp. We can't |
| 613 | // draw as-is or else opposite AA borders will overlap. Instead, fudge the |
| 614 | // size up to the width of a coverage ramp, and then reduce total coverage |
| 615 | // to make the rect appear more thin. |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 616 | v->codeAppend( "corner = max(abs(corner), aa_bloatradius) * sign(corner);"); |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 617 | v->codeAppend( "coverage_multiplier = 1 / (max(aa_bloatradius.x, 1) * " |
| 618 | "max(aa_bloatradius.y, 1));"); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 619 | // Set radii to zero to ensure we take the "linear coverage" codepath. |
| 620 | // (The "coverage" variable only has effect in the linear codepath.) |
| 621 | v->codeAppend( "radii = float2(0);"); |
| 622 | v->codeAppend("}"); |
| 623 | |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 624 | // Unpack coverage. |
| 625 | v->codeAppend("float coverage = aa_bloat_and_coverage.z;"); |
| 626 | if (proc.fFlags & ProcessorFlags::kMSAAEnabled) { |
| 627 | // MSAA has a wider ramp that goes from -.5 to 1.5 instead of 0 to 1. |
| 628 | v->codeAppendf("coverage = (coverage - .5) * aa_bloat_multiplier + .5;"); |
| 629 | } |
| 630 | |
| 631 | v->codeAppend("if (any(lessThan(radii, aa_bloatradius * 1.5))) {"); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 632 | // The radii are very small. Demote this arc to a sharp 90 degree corner. |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 633 | v->codeAppend( "radii = float2(0);"); |
| 634 | // Convert to a standard picture frame for an AA rect instead of the round |
| 635 | // rect geometry. |
| 636 | v->codeAppend( "aa_bloat_direction = sign(corner);"); |
| 637 | v->codeAppend( "if (coverage > .5) {"); // Are we an inset edge? |
| 638 | v->codeAppend( "aa_bloat_direction = -aa_bloat_direction;"); |
| 639 | v->codeAppend( "}"); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 640 | v->codeAppend( "is_linear_coverage = 1;"); |
| 641 | v->codeAppend("} else {"); |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 642 | // Don't let radii get smaller than a coverage ramp plus an extra half |
| 643 | // pixel for MSAA. Always use the same amount so we don't pop when |
| 644 | // switching between MSAA and coverage. |
| 645 | v->codeAppend( "radii = clamp(radii, pixellength * 1.5, 2 - pixellength * 1.5);"); |
| 646 | v->codeAppend( "neighbor_radii = clamp(neighbor_radii, pixellength * 1.5, " |
| 647 | "2 - pixellength * 1.5);"); |
Mike Reed | d3efa99 | 2018-11-28 13:13:15 +0000 | [diff] [blame] | 648 | // Don't let neighboring radii get closer together than 1/16 pixel. |
| 649 | v->codeAppend( "float2 spacing = 2 - radii - neighbor_radii;"); |
| 650 | v->codeAppend( "float2 extra_pad = max(pixellength * .0625 - spacing, float2(0));"); |
| 651 | v->codeAppend( "radii -= extra_pad * .5;"); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 652 | v->codeAppend("}"); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 653 | |
| 654 | // Find our vertex position, adjusted for radii and bloated for AA. Our rect is drawn in |
| 655 | // normalized [-1,-1,+1,+1] space. |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 656 | v->codeAppend("float2 aa_outset = " |
| 657 | "aa_bloat_direction * aa_bloatradius * aa_bloat_multiplier;"); |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 658 | v->codeAppend("float2 vertexpos = corner + radius_outset * radii + aa_outset;"); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 659 | |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 660 | v->codeAppend("if (coverage > .5) {"); // Are we an inset edge? |
| 661 | // Don't allow the aa insets to overlap. i.e., Don't let them inset past |
| 662 | // the center (x=y=0). Since we don't allow the rect to become thinner |
| 663 | // than 1px, this should only happen when using MSAA, where we inset by an |
| 664 | // entire pixel instead of half. |
| 665 | v->codeAppend( "if (aa_bloat_direction.x != 0 && vertexpos.x * corner.x < 0) {"); |
| 666 | v->codeAppend( "float backset = abs(vertexpos.x);"); |
| 667 | v->codeAppend( "vertexpos.x = 0;"); |
| 668 | v->codeAppend( "vertexpos.y += " |
| 669 | "backset * sign(corner.y) * pixellength.y/pixellength.x;"); |
| 670 | v->codeAppend( "coverage = (coverage - .5) * abs(corner.x) / " |
| 671 | "(abs(corner.x) + backset) + .5;"); |
| 672 | v->codeAppend( "}"); |
| 673 | v->codeAppend( "if (aa_bloat_direction.y != 0 && vertexpos.y * corner.y < 0) {"); |
| 674 | v->codeAppend( "float backset = abs(vertexpos.y);"); |
| 675 | v->codeAppend( "vertexpos.y = 0;"); |
| 676 | v->codeAppend( "vertexpos.x += " |
| 677 | "backset * sign(corner.x) * pixellength.x/pixellength.y;"); |
| 678 | v->codeAppend( "coverage = (coverage - .5) * abs(corner.y) / " |
| 679 | "(abs(corner.y) + backset) + .5;"); |
| 680 | v->codeAppend( "}"); |
| 681 | v->codeAppend("}"); |
| 682 | |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 683 | // Write positions |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 684 | GrShaderVar localCoord("", kFloat2_GrSLType); |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 685 | if (proc.fFlags & ProcessorFlags::kHasLocalCoords) { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 686 | v->codeAppend("float2 localcoord = (local_rect.xy * (1 - vertexpos) + " |
| 687 | "local_rect.zw * (1 + vertexpos)) * .5;"); |
Michael Ludwig | 553db62 | 2020-06-19 10:47:30 -0400 | [diff] [blame] | 688 | gpArgs->fLocalCoordVar.set(kFloat2_GrSLType, "localcoord"); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 689 | } |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 690 | |
| 691 | // Transform to device space. |
| 692 | v->codeAppend("float2x2 skewmatrix = float2x2(skew.xy, skew.zw);"); |
| 693 | v->codeAppend("float2 devcoord = vertexpos * skewmatrix + translate;"); |
| 694 | gpArgs->fPositionVar.set(kFloat2_GrSLType, "devcoord"); |
| 695 | |
| 696 | // Setup interpolants for coverage. |
| 697 | GrGLSLVarying arcCoord(useHWDerivatives ? kFloat2_GrSLType : kFloat4_GrSLType); |
| 698 | varyings->addVarying("arccoord", &arcCoord); |
| 699 | v->codeAppend("if (0 != is_linear_coverage) {"); |
| 700 | // We are a non-corner piece: Set x=0 to indicate built-in coverage, and |
| 701 | // interpolate linear coverage across y. |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 702 | v->codeAppendf( "%s.xy = float2(0, coverage * coverage_multiplier);", |
| 703 | arcCoord.vsOut()); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 704 | v->codeAppend("} else {"); |
Chris Dalton | aa71f0a | 2018-11-21 18:14:45 -0700 | [diff] [blame] | 705 | // Find the normalized arc coordinates for our corner ellipse. |
| 706 | // (i.e., the coordinate system where x^2 + y^2 == 1). |
| 707 | v->codeAppend( "float2 arccoord = 1 - abs(radius_outset) + aa_outset/radii * corner;"); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 708 | // We are a corner piece: Interpolate the arc coordinates for coverage. |
| 709 | // Emit x+1 to ensure no pixel in the arc has a x value of 0 (since x=0 |
| 710 | // instructs the fragment shader to use linear coverage). |
| 711 | v->codeAppendf( "%s.xy = float2(arccoord.x+1, arccoord.y);", arcCoord.vsOut()); |
| 712 | if (!useHWDerivatives) { |
| 713 | // The gradient is order-1: Interpolate it across arccoord.zw. |
| 714 | v->codeAppendf("float2x2 derivatives = inverse(skewmatrix);"); |
| 715 | v->codeAppendf("%s.zw = derivatives * (arccoord/radii * 2);", arcCoord.vsOut()); |
| 716 | } |
| 717 | v->codeAppend("}"); |
| 718 | |
| 719 | // Emit the fragment shader. |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 720 | f->codeAppendf("float x_plus_1=%s.x, y=%s.y;", arcCoord.fsIn(), arcCoord.fsIn()); |
| 721 | f->codeAppendf("half coverage;"); |
| 722 | f->codeAppendf("if (0 == x_plus_1) {"); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 723 | f->codeAppendf( "coverage = half(y);"); // We are a non-arc pixel (linear coverage). |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 724 | f->codeAppendf("} else {"); |
| 725 | f->codeAppendf( "float fn = x_plus_1 * (x_plus_1 - 2);"); // fn = (x+1)*(x-1) = x^2-1 |
| 726 | f->codeAppendf( "fn = fma(y,y, fn);"); // fn = x^2 + y^2 - 1 |
| 727 | if (useHWDerivatives) { |
| 728 | f->codeAppendf("float fnwidth = fwidth(fn);"); |
| 729 | } else { |
| 730 | // The gradient is interpolated across arccoord.zw. |
| 731 | f->codeAppendf("float gx=%s.z, gy=%s.w;", arcCoord.fsIn(), arcCoord.fsIn()); |
| 732 | f->codeAppendf("float fnwidth = abs(gx) + abs(gy);"); |
| 733 | } |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 734 | f->codeAppendf( "coverage = .5 - half(fn/fnwidth);"); |
| 735 | if (proc.fFlags & ProcessorFlags::kMSAAEnabled) { |
| 736 | // MSAA uses ramps larger than 1px, so we need to clamp in both branches. |
| 737 | f->codeAppendf("}"); |
| 738 | } |
| 739 | f->codeAppendf("coverage = clamp(coverage, 0, 1);"); |
| 740 | if (!(proc.fFlags & ProcessorFlags::kMSAAEnabled)) { |
| 741 | // When not using MSAA, we only need to clamp in the "arc" branch. |
| 742 | f->codeAppendf("}"); |
| 743 | } |
| 744 | if (proc.fFlags & ProcessorFlags::kFakeNonAA) { |
| 745 | f->codeAppendf("coverage = (coverage >= .5) ? 1 : 0;"); |
| 746 | } |
John Stiles | 4d7ac49 | 2021-03-09 20:16:43 -0500 | [diff] [blame] | 747 | f->codeAppendf("half4 %s = half4(coverage);", args.fOutputCoverage); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 748 | } |
| 749 | |
Brian Salomon | 5a32828 | 2021-04-14 10:32:25 -0400 | [diff] [blame] | 750 | void setData(const GrGLSLProgramDataManager&, |
| 751 | const GrShaderCaps&, |
| 752 | const GrGeometryProcessor&) override {} |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 753 | }; |
| 754 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 755 | |
Robert Phillips | f10535f | 2021-03-23 09:30:45 -0400 | [diff] [blame] | 756 | GrGLSLGeometryProcessor* FillRRectOp::Processor::createGLSLInstance(const GrShaderCaps&) const { |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 757 | return new Impl(); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 758 | } |
| 759 | |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 760 | void FillRRectOp::onCreateProgramInfo(const GrCaps* caps, |
| 761 | SkArenaAlloc* arena, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 762 | const GrSurfaceProxyView& writeView, |
Robert Phillips | cad8fba | 2020-03-20 15:39:29 -0400 | [diff] [blame] | 763 | GrAppliedClip&& appliedClip, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame] | 764 | const GrDstProxyView& dstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 765 | GrXferBarrierFlags renderPassXferBarriers, |
| 766 | GrLoadOp colorLoadOp) { |
Chris Dalton | cc29a39 | 2021-07-12 15:16:29 -0600 | [diff] [blame] | 767 | if (writeView.asRenderTargetProxy()->numSamples() > 1) { |
| 768 | fProcessorFlags |= ProcessorFlags::kMSAAEnabled; |
| 769 | } |
| 770 | auto extraPipelineFlags = (fProcessorFlags & ProcessorFlags::kMSAAEnabled) |
| 771 | ? GrPipeline::InputFlags::kHWAntialias |
| 772 | : GrPipeline::InputFlags::kNone; |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 773 | GrGeometryProcessor* gp = Processor::Make(arena, fHelper.aaType(), fProcessorFlags); |
Chris Dalton | cc29a39 | 2021-07-12 15:16:29 -0600 | [diff] [blame] | 774 | fProgramInfo = GrSimpleMeshDrawOpHelper::CreateProgramInfo( |
| 775 | caps, arena, writeView, std::move(appliedClip), dstProxyView, gp, |
| 776 | fHelper.detachProcessorSet(), GrPrimitiveType::kTriangles, renderPassXferBarriers, |
| 777 | colorLoadOp, fHelper.pipelineFlags() | extraPipelineFlags); |
Robert Phillips | 8053c97 | 2019-11-21 10:44:53 -0500 | [diff] [blame] | 778 | } |
| 779 | |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 780 | void FillRRectOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Robert Phillips | 8053c97 | 2019-11-21 10:44:53 -0500 | [diff] [blame] | 781 | if (!fInstanceBuffer || !fIndexBuffer || !fVertexBuffer) { |
| 782 | return; // Setup failed. |
| 783 | } |
| 784 | |
Chris Dalton | aa0e45c | 2020-03-16 10:05:11 -0600 | [diff] [blame] | 785 | flushState->bindPipelineAndScissorClip(*fProgramInfo, this->bounds()); |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 786 | flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline()); |
Greg Daniel | 426274b | 2020-07-20 11:37:38 -0400 | [diff] [blame] | 787 | flushState->bindBuffers(std::move(fIndexBuffer), std::move(fInstanceBuffer), |
| 788 | std::move(fVertexBuffer)); |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 789 | flushState->drawIndexedInstanced(SK_ARRAY_COUNT(kIndexData), 0, fInstanceCount, fBaseInstance, |
| 790 | 0); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | // Will the given corner look good if we use HW derivatives? |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 794 | static bool can_use_hw_derivatives_with_coverage(const Sk2f& devScale, const Sk2f& cornerRadii) { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 795 | Sk2f devRadii = devScale * cornerRadii; |
| 796 | if (devRadii[1] < devRadii[0]) { |
| 797 | devRadii = SkNx_shuffle<1,0>(devRadii); |
| 798 | } |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 799 | float minDevRadius = std::max(devRadii[0], 1.f); // Shader clamps radius at a minimum of 1. |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 800 | // Is the gradient smooth enough for this corner look ok if we use hardware derivatives? |
| 801 | // This threshold was arrived at subjevtively on an NVIDIA chip. |
| 802 | return minDevRadius * minDevRadius * 5 > devRadii[1]; |
| 803 | } |
| 804 | |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 805 | static bool can_use_hw_derivatives_with_coverage( |
| 806 | const Sk2f& devScale, const SkVector& cornerRadii) { |
| 807 | return can_use_hw_derivatives_with_coverage(devScale, Sk2f::Load(&cornerRadii)); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | // Will the given round rect look good if we use HW derivatives? |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 811 | static bool can_use_hw_derivatives_with_coverage( |
| 812 | const GrShaderCaps& shaderCaps, const SkMatrix& viewMatrix, const SkRRect& rrect) { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 813 | if (!shaderCaps.shaderDerivativeSupport()) { |
| 814 | return false; |
| 815 | } |
| 816 | |
| 817 | Sk2f x = Sk2f(viewMatrix.getScaleX(), viewMatrix.getSkewX()); |
| 818 | Sk2f y = Sk2f(viewMatrix.getSkewY(), viewMatrix.getScaleY()); |
| 819 | Sk2f devScale = (x*x + y*y).sqrt(); |
| 820 | switch (rrect.getType()) { |
| 821 | case SkRRect::kEmpty_Type: |
| 822 | case SkRRect::kRect_Type: |
| 823 | return true; |
| 824 | |
| 825 | case SkRRect::kOval_Type: |
| 826 | case SkRRect::kSimple_Type: |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 827 | return can_use_hw_derivatives_with_coverage(devScale, rrect.getSimpleRadii()); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 828 | |
| 829 | case SkRRect::kNinePatch_Type: { |
| 830 | Sk2f r0 = Sk2f::Load(SkRRectPriv::GetRadiiArray(rrect)); |
| 831 | Sk2f r1 = Sk2f::Load(SkRRectPriv::GetRadiiArray(rrect) + 2); |
| 832 | Sk2f minRadii = Sk2f::Min(r0, r1); |
| 833 | Sk2f maxRadii = Sk2f::Max(r0, r1); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 834 | return can_use_hw_derivatives_with_coverage(devScale, Sk2f(minRadii[0], maxRadii[1])) && |
| 835 | can_use_hw_derivatives_with_coverage(devScale, Sk2f(maxRadii[0], minRadii[1])); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | case SkRRect::kComplex_Type: { |
| 839 | for (int i = 0; i < 4; ++i) { |
| 840 | auto corner = static_cast<SkRRect::Corner>(i); |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 841 | if (!can_use_hw_derivatives_with_coverage(devScale, rrect.radii(corner))) { |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 842 | return false; |
| 843 | } |
| 844 | } |
| 845 | return true; |
| 846 | } |
| 847 | } |
Chris Dalton | 0dffbab | 2019-03-27 13:08:50 -0600 | [diff] [blame] | 848 | SK_ABORT("Invalid round rect type."); |
Chris Dalton | 133944a | 2018-11-16 23:30:29 -0500 | [diff] [blame] | 849 | } |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 850 | |
| 851 | } // anonymous namespace |
| 852 | |
| 853 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 854 | GrOp::Owner GrFillRRectOp::Make(GrRecordingContext* ctx, |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 855 | SkArenaAlloc* arena, |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 856 | GrPaint&& paint, |
| 857 | const SkMatrix& viewMatrix, |
| 858 | const SkRRect& rrect, |
Chris Dalton | 61d694d | 2021-03-22 00:00:52 -0600 | [diff] [blame] | 859 | const SkRect& localRect, |
Chris Dalton | 4f44734 | 2021-03-12 12:09:12 -0700 | [diff] [blame] | 860 | GrAA aa) { |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 861 | return FillRRectOp::Make(ctx, arena, std::move(paint), viewMatrix, rrect, localRect, aa); |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 862 | } |
| 863 | |
Chris Dalton | 61d694d | 2021-03-22 00:00:52 -0600 | [diff] [blame] | 864 | |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 865 | #if GR_TEST_UTILS |
| 866 | |
| 867 | #include "src/gpu/GrDrawOpTest.h" |
| 868 | |
| 869 | GR_DRAW_OP_TEST_DEFINE(FillRRectOp) { |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 870 | SkArenaAlloc arena(64 * sizeof(float)); |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 871 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
Chris Dalton | 4f44734 | 2021-03-12 12:09:12 -0700 | [diff] [blame] | 872 | GrAA aa = GrAA(random->nextBool()); |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 873 | |
| 874 | SkRect rect = GrTest::TestRect(random); |
| 875 | float w = rect.width(); |
| 876 | float h = rect.height(); |
| 877 | |
| 878 | SkRRect rrect; |
| 879 | // TODO: test out other rrect configurations |
| 880 | rrect.setNinePatch(rect, w / 3.0f, h / 4.0f, w / 5.0f, h / 6.0); |
| 881 | |
| 882 | return GrFillRRectOp::Make(context, |
Chris Dalton | 3c636f0 | 2021-07-08 14:50:57 -0600 | [diff] [blame] | 883 | &arena, |
Robert Phillips | 360ec18 | 2020-03-26 13:29:50 -0400 | [diff] [blame] | 884 | std::move(paint), |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 885 | viewMatrix, |
| 886 | rrect, |
Chris Dalton | 61d694d | 2021-03-22 00:00:52 -0600 | [diff] [blame] | 887 | rrect.rect(), |
Chris Dalton | 4f44734 | 2021-03-12 12:09:12 -0700 | [diff] [blame] | 888 | aa); |
Robert Phillips | 366176b | 2020-02-26 11:40:50 -0500 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | #endif |