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