joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 1 | /* |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 2 | * Copyright 2018 Google Inc. |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 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 | |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 8 | #include "src/gpu/ops/StrokeRectOp.h" |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkStrokeRec.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/private/GrResourceKey.h" |
| 12 | #include "include/utils/SkRandom.h" |
Michael Ludwig | fbe2859 | 2020-06-26 16:02:15 -0400 | [diff] [blame] | 13 | #include "src/core/SkMatrixPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrCaps.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/GrDefaultGeoProcFactory.h" |
| 17 | #include "src/gpu/GrDrawOpTest.h" |
| 18 | #include "src/gpu/GrOpFlushState.h" |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrProgramInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/gpu/GrResourceProvider.h" |
| 21 | #include "src/gpu/GrVertexWriter.h" |
Robert Phillips | fbf0214 | 2021-09-01 16:31:34 -0400 | [diff] [blame] | 22 | #include "src/gpu/ops/FillRectOp.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/gpu/ops/GrMeshDrawOp.h" |
| 24 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 25 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 26 | namespace { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 27 | |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 28 | // We support all hairlines, bevels, and miters, but not round joins. Also, check whether the miter |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 29 | // limit makes a miter join effectively beveled. If the miter is effectively beveled, it is only |
| 30 | // supported when using an AA stroke. |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 31 | inline bool allowed_stroke(const SkStrokeRec& stroke, GrAA aa, bool* isMiter) { |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 32 | SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style || |
| 33 | stroke.getStyle() == SkStrokeRec::kHairline_Style); |
| 34 | // For hairlines, make bevel and round joins appear the same as mitered ones. |
| 35 | if (!stroke.getWidth()) { |
| 36 | *isMiter = true; |
| 37 | return true; |
| 38 | } |
| 39 | if (stroke.getJoin() == SkPaint::kBevel_Join) { |
| 40 | *isMiter = false; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 41 | return aa == GrAA::kYes; // bevel only supported with AA |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 42 | } |
| 43 | if (stroke.getJoin() == SkPaint::kMiter_Join) { |
| 44 | *isMiter = stroke.getMiter() >= SK_ScalarSqrt2; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 45 | // Supported under non-AA only if it remains mitered |
| 46 | return aa == GrAA::kYes || *isMiter; |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 47 | } |
| 48 | return false; |
| 49 | } |
| 50 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 51 | |
| 52 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 53 | // Non-AA Stroking |
| 54 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 55 | |
| 56 | /* create a triangle strip that strokes the specified rect. There are 8 |
| 57 | unique vertices, but we repeat the last 2 to close up. Alternatively we |
| 58 | could use an indices array, and then only send 8 verts, but not sure that |
| 59 | would be faster. |
| 60 | */ |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 61 | void init_nonaa_stroke_rect_strip(SkPoint verts[10], const SkRect& rect, SkScalar width) { |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 62 | const SkScalar rad = SkScalarHalf(width); |
| 63 | |
| 64 | verts[0].set(rect.fLeft + rad, rect.fTop + rad); |
| 65 | verts[1].set(rect.fLeft - rad, rect.fTop - rad); |
| 66 | verts[2].set(rect.fRight - rad, rect.fTop + rad); |
| 67 | verts[3].set(rect.fRight + rad, rect.fTop - rad); |
| 68 | verts[4].set(rect.fRight - rad, rect.fBottom - rad); |
| 69 | verts[5].set(rect.fRight + rad, rect.fBottom + rad); |
| 70 | verts[6].set(rect.fLeft + rad, rect.fBottom - rad); |
| 71 | verts[7].set(rect.fLeft - rad, rect.fBottom + rad); |
| 72 | verts[8] = verts[0]; |
| 73 | verts[9] = verts[1]; |
| 74 | |
| 75 | // TODO: we should be catching this higher up the call stack and just draw a single |
| 76 | // non-AA rect |
| 77 | if (2*rad >= rect.width()) { |
| 78 | verts[0].fX = verts[2].fX = verts[4].fX = verts[6].fX = verts[8].fX = rect.centerX(); |
| 79 | } |
| 80 | if (2*rad >= rect.height()) { |
| 81 | verts[0].fY = verts[2].fY = verts[4].fY = verts[6].fY = verts[8].fY = rect.centerY(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | class NonAAStrokeRectOp final : public GrMeshDrawOp { |
| 86 | private: |
| 87 | using Helper = GrSimpleMeshDrawOpHelper; |
| 88 | |
| 89 | public: |
| 90 | DEFINE_OP_CLASS_ID |
| 91 | |
| 92 | const char* name() const override { return "NonAAStrokeRectOp"; } |
| 93 | |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 94 | void visitProxies(const GrVisitProxyFunc& func) const override { |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 95 | if (fProgramInfo) { |
Chris Dalton | be45742 | 2020-03-16 18:05:03 -0600 | [diff] [blame] | 96 | fProgramInfo->visitFPProxies(func); |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 97 | } else { |
| 98 | fHelper.visitProxies(func); |
| 99 | } |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 100 | } |
| 101 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 102 | static GrOp::Owner Make(GrRecordingContext* context, |
| 103 | GrPaint&& paint, |
| 104 | const SkMatrix& viewMatrix, |
| 105 | const SkRect& rect, |
| 106 | const SkStrokeRec& stroke, |
| 107 | GrAAType aaType) { |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 108 | bool isMiter; |
| 109 | if (!allowed_stroke(stroke, GrAA::kNo, &isMiter)) { |
| 110 | return nullptr; |
| 111 | } |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 112 | Helper::InputFlags inputFlags = Helper::InputFlags::kNone; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 113 | // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of |
| 114 | // hairline rects. We jam all the vertices to pixel centers to avoid this, but not |
| 115 | // when MSAA is enabled because it can cause ugly artifacts. |
| 116 | if (stroke.getStyle() == SkStrokeRec::kHairline_Style && aaType != GrAAType::kMSAA) { |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 117 | inputFlags |= Helper::InputFlags::kSnapVerticesToPixelCenters; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 118 | } |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 119 | return Helper::FactoryHelper<NonAAStrokeRectOp>(context, std::move(paint), inputFlags, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 120 | viewMatrix, rect, |
| 121 | stroke, aaType); |
| 122 | } |
| 123 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 124 | NonAAStrokeRectOp(GrProcessorSet* processorSet, const SkPMColor4f& color, |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 125 | Helper::InputFlags inputFlags, const SkMatrix& viewMatrix, const SkRect& rect, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 126 | const SkStrokeRec& stroke, GrAAType aaType) |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 127 | : INHERITED(ClassID()) |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 128 | , fHelper(processorSet, aaType, inputFlags) { |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 129 | fColor = color; |
| 130 | fViewMatrix = viewMatrix; |
| 131 | fRect = rect; |
| 132 | // Sort the rect for hairlines |
| 133 | fRect.sort(); |
| 134 | fStrokeWidth = stroke.getWidth(); |
| 135 | |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 136 | SkScalar rad = SkScalarHalf(fStrokeWidth); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 137 | SkRect bounds = rect; |
| 138 | bounds.outset(rad, rad); |
| 139 | |
| 140 | // If our caller snaps to pixel centers then we have to round out the bounds |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 141 | if (inputFlags & Helper::InputFlags::kSnapVerticesToPixelCenters) { |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 142 | SkASSERT(!fStrokeWidth || aaType == GrAAType::kNone); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 143 | viewMatrix.mapRect(&bounds); |
| 144 | // We want to be consistent with how we snap non-aa lines. To match what we do in |
| 145 | // GrGLSLVertexShaderBuilder, we first floor all the vertex values and then add half a |
| 146 | // pixel to force us to pixel centers. |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 147 | bounds.setLTRB(SkScalarFloorToScalar(bounds.fLeft), |
| 148 | SkScalarFloorToScalar(bounds.fTop), |
| 149 | SkScalarFloorToScalar(bounds.fRight), |
| 150 | SkScalarFloorToScalar(bounds.fBottom)); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 151 | bounds.offset(0.5f, 0.5f); |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 152 | this->setBounds(bounds, HasAABloat::kNo, IsHairline::kNo); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 153 | } else { |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 154 | HasAABloat aaBloat = (aaType == GrAAType::kNone) ? HasAABloat ::kNo : HasAABloat::kYes; |
| 155 | this->setTransformedBounds(bounds, fViewMatrix, aaBloat, |
| 156 | fStrokeWidth ? IsHairline::kNo : IsHairline::kYes); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 161 | |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 162 | GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 163 | GrClampType clampType) override { |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 164 | // This Op uses uniform (not vertex) color, so doesn't need to track wide color. |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 165 | return fHelper.finalizeProcessors(caps, clip, clampType, GrProcessorAnalysisCoverage::kNone, |
| 166 | &fColor, nullptr); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | private: |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 170 | GrProgramInfo* programInfo() override { return fProgramInfo; } |
| 171 | |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 172 | void onCreateProgramInfo(const GrCaps* caps, |
| 173 | SkArenaAlloc* arena, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 174 | const GrSurfaceProxyView& writeView, |
Chris Dalton | 6aaf00f | 2021-07-13 13:26:39 -0600 | [diff] [blame] | 175 | bool usesMSAASurface, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 176 | GrAppliedClip&& clip, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame] | 177 | const GrDstProxyView& dstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 178 | GrXferBarrierFlags renderPassXferBarriers, |
| 179 | GrLoadOp colorLoadOp) override { |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 180 | GrGeometryProcessor* gp; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 181 | { |
| 182 | using namespace GrDefaultGeoProcFactory; |
| 183 | Color color(fColor); |
| 184 | LocalCoords::Type localCoordsType = fHelper.usesLocalCoords() |
| 185 | ? LocalCoords::kUsePosition_Type |
| 186 | : LocalCoords::kUnused_Type; |
Brian Osman | f0aee74 | 2020-03-12 09:28:44 -0400 | [diff] [blame] | 187 | gp = GrDefaultGeoProcFactory::Make(arena, color, Coverage::kSolid_Type, localCoordsType, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 188 | fViewMatrix); |
| 189 | } |
| 190 | |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 191 | GrPrimitiveType primType = (fStrokeWidth > 0) ? GrPrimitiveType::kTriangleStrip |
| 192 | : GrPrimitiveType::kLineStrip; |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 193 | |
Chris Dalton | 2a26c50 | 2021-08-26 10:05:11 -0600 | [diff] [blame] | 194 | fProgramInfo = fHelper.createProgramInfo(caps, arena, writeView, usesMSAASurface, |
| 195 | std::move(clip), dstProxyView, gp, primType, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 196 | renderPassXferBarriers, colorLoadOp); |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 197 | } |
| 198 | |
Robert Phillips | 7114395 | 2021-06-17 14:55:07 -0400 | [diff] [blame] | 199 | void onPrepareDraws(GrMeshDrawTarget* target) override { |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 200 | if (!fProgramInfo) { |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 201 | this->createProgramInfo(target); |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 202 | } |
| 203 | |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 204 | size_t kVertexStride = fProgramInfo->geomProc().vertexStride(); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 205 | int vertexCount = kVertsPerHairlineRect; |
| 206 | if (fStrokeWidth > 0) { |
| 207 | vertexCount = kVertsPerStrokeRect; |
| 208 | } |
| 209 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 210 | sk_sp<const GrBuffer> vertexBuffer; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 211 | int firstVertex; |
| 212 | |
| 213 | void* verts = |
| 214 | target->makeVertexSpace(kVertexStride, vertexCount, &vertexBuffer, &firstVertex); |
| 215 | |
| 216 | if (!verts) { |
| 217 | SkDebugf("Could not allocate vertices\n"); |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | SkPoint* vertex = reinterpret_cast<SkPoint*>(verts); |
| 222 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 223 | if (fStrokeWidth > 0) { |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 224 | init_nonaa_stroke_rect_strip(vertex, fRect, fStrokeWidth); |
| 225 | } else { |
| 226 | // hairline |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 227 | vertex[0].set(fRect.fLeft, fRect.fTop); |
| 228 | vertex[1].set(fRect.fRight, fRect.fTop); |
| 229 | vertex[2].set(fRect.fRight, fRect.fBottom); |
| 230 | vertex[3].set(fRect.fLeft, fRect.fBottom); |
| 231 | vertex[4].set(fRect.fLeft, fRect.fTop); |
| 232 | } |
| 233 | |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 234 | fMesh = target->allocMesh(); |
Chris Dalton | 37c7bdd | 2020-03-13 09:21:12 -0600 | [diff] [blame] | 235 | fMesh->set(std::move(vertexBuffer), vertexCount, firstVertex); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 239 | if (!fMesh) { |
| 240 | return; |
| 241 | } |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 242 | |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 243 | flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 244 | flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline()); |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 245 | flushState->drawMesh(*fMesh); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 246 | } |
| 247 | |
John Stiles | af36652 | 2020-08-13 09:57:34 -0400 | [diff] [blame] | 248 | #if GR_TEST_UTILS |
| 249 | SkString onDumpInfo() const override { |
| 250 | return SkStringPrintf("Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], " |
| 251 | "StrokeWidth: %.2f\n%s", |
| 252 | fColor.toBytes_RGBA(), fRect.fLeft, fRect.fTop, fRect.fRight, |
| 253 | fRect.fBottom, fStrokeWidth, fHelper.dumpInfo().c_str()); |
| 254 | } |
| 255 | #endif |
| 256 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 257 | // TODO: override onCombineIfPossible |
| 258 | |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 259 | Helper fHelper; |
| 260 | SkPMColor4f fColor; |
| 261 | SkMatrix fViewMatrix; |
| 262 | SkRect fRect; |
| 263 | SkScalar fStrokeWidth; |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 264 | GrSimpleMesh* fMesh = nullptr; |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 265 | GrProgramInfo* fProgramInfo = nullptr; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 266 | |
| 267 | const static int kVertsPerHairlineRect = 5; |
| 268 | const static int kVertsPerStrokeRect = 10; |
| 269 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 270 | using INHERITED = GrMeshDrawOp; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 271 | }; |
| 272 | |
| 273 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 274 | // AA Stroking |
| 275 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 276 | |
| 277 | GR_DECLARE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey); |
| 278 | GR_DECLARE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey); |
| 279 | |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 280 | bool stroke_dev_half_size_supported(SkVector devHalfStrokeSize) { |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 281 | // Since the horizontal and vertical strokes share internal corners, the coverage value at that |
| 282 | // corner needs to be equal for the horizontal and vertical strokes both. |
| 283 | // |
| 284 | // The inner coverage values will be equal if the horizontal and vertical stroke widths are |
| 285 | // equal (in which case innerCoverage is same for all sides of the rects) or if the horizontal |
| 286 | // and vertical stroke widths are both greater than 1 (in which case innerCoverage will always |
Michael Ludwig | 144926d | 2021-09-10 09:34:47 -0400 | [diff] [blame] | 287 | // be 1). In actuality we allow them to be nearly-equal since differing by < 1/1000 will not be |
| 288 | // visually detectable when the shape is already less than 1px in thickness. |
| 289 | return SkScalarNearlyEqual(devHalfStrokeSize.fX, devHalfStrokeSize.fY) || |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 290 | std::min(devHalfStrokeSize.fX, devHalfStrokeSize.fY) >= .5f; |
| 291 | } |
| 292 | |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 293 | bool compute_aa_rects(const GrCaps& caps, |
| 294 | SkRect* devOutside, |
| 295 | SkRect* devOutsideAssist, |
| 296 | SkRect* devInside, |
| 297 | bool* isDegenerate, |
| 298 | const SkMatrix& viewMatrix, |
| 299 | const SkRect& rect, |
| 300 | SkScalar strokeWidth, |
| 301 | bool miterStroke, |
| 302 | SkVector* devHalfStrokeSize) { |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 303 | SkVector devStrokeSize; |
| 304 | if (strokeWidth > 0) { |
| 305 | devStrokeSize.set(strokeWidth, strokeWidth); |
| 306 | viewMatrix.mapVectors(&devStrokeSize, 1); |
| 307 | devStrokeSize.setAbs(devStrokeSize); |
| 308 | } else { |
| 309 | devStrokeSize.set(SK_Scalar1, SK_Scalar1); |
| 310 | } |
| 311 | |
| 312 | const SkScalar dx = devStrokeSize.fX; |
| 313 | const SkScalar dy = devStrokeSize.fY; |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 314 | const SkScalar rx = SkScalarHalf(dx); |
| 315 | const SkScalar ry = SkScalarHalf(dy); |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 316 | |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 317 | devHalfStrokeSize->fX = rx; |
| 318 | devHalfStrokeSize->fY = ry; |
| 319 | |
Chris Dalton | 4f48910 | 2021-07-14 14:44:28 -0600 | [diff] [blame] | 320 | SkRect devRect; |
| 321 | viewMatrix.mapRect(&devRect, rect); |
| 322 | |
| 323 | // Clip our draw rect 1 full stroke width plus bloat outside the viewport. This avoids |
| 324 | // interpolation precision issues with very large coordinates. |
Chris Dalton | 0196a72 | 2021-07-14 18:59:59 -0600 | [diff] [blame] | 325 | const float m = caps.maxRenderTargetSize(); |
Chris Dalton | 4f48910 | 2021-07-14 14:44:28 -0600 | [diff] [blame] | 326 | const SkRect visibilityBounds = SkRect::MakeWH(m, m).makeOutset(dx + 1, dy + 1); |
| 327 | if (!devRect.intersect(visibilityBounds)) { |
| 328 | return false; |
| 329 | } |
| 330 | |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 331 | *devOutside = devRect; |
| 332 | *devOutsideAssist = devRect; |
| 333 | *devInside = devRect; |
| 334 | |
| 335 | devOutside->outset(rx, ry); |
| 336 | devInside->inset(rx, ry); |
| 337 | |
| 338 | // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we |
| 339 | // make a degenerate inside rect to avoid double hitting. We will also jam all of the points |
| 340 | // together when we render these rects. |
| 341 | SkScalar spare; |
| 342 | { |
| 343 | SkScalar w = devRect.width() - dx; |
| 344 | SkScalar h = devRect.height() - dy; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 345 | spare = std::min(w, h); |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | *isDegenerate = spare <= 0; |
| 349 | if (*isDegenerate) { |
| 350 | devInside->fLeft = devInside->fRight = devRect.centerX(); |
| 351 | devInside->fTop = devInside->fBottom = devRect.centerY(); |
| 352 | } |
| 353 | |
| 354 | // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist) |
| 355 | // to draw the outside of the octagon. Because there are 8 vertices on the outer |
| 356 | // edge, while vertex number of inner edge is 4, the same as miter-stroke. |
| 357 | if (!miterStroke) { |
| 358 | devOutside->inset(0, ry); |
| 359 | devOutsideAssist->outset(0, ry); |
| 360 | } |
Chris Dalton | 4f48910 | 2021-07-14 14:44:28 -0600 | [diff] [blame] | 361 | |
| 362 | return true; |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 365 | GrGeometryProcessor* create_aa_stroke_rect_gp(SkArenaAlloc* arena, |
| 366 | bool usesMSAASurface, |
| 367 | bool tweakAlphaForCoverage, |
| 368 | const SkMatrix& viewMatrix, |
| 369 | bool usesLocalCoords, |
| 370 | bool wideColor) { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 371 | using namespace GrDefaultGeoProcFactory; |
| 372 | |
Chris Dalton | 97f8590 | 2021-07-14 03:16:22 -0600 | [diff] [blame] | 373 | // When MSAA is enabled, we have to extend our AA bloats and interpolate coverage values outside |
| 374 | // 0..1. We tell the gp in this case that coverage is an unclamped attribute so it will call |
| 375 | // saturate(coverage) in the fragment shader. |
| 376 | Coverage::Type coverageType = usesMSAASurface ? Coverage::kAttributeUnclamped_Type |
| 377 | : (!tweakAlphaForCoverage ? Coverage::kAttribute_Type |
| 378 | : Coverage::kSolid_Type); |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 379 | LocalCoords::Type localCoordsType = |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 380 | usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type; |
| 381 | Color::Type colorType = |
| 382 | wideColor ? Color::kPremulWideColorAttribute_Type: Color::kPremulGrColorAttribute_Type; |
| 383 | |
Brian Osman | f0aee74 | 2020-03-12 09:28:44 -0400 | [diff] [blame] | 384 | return MakeForDeviceSpace(arena, colorType, coverageType, localCoordsType, viewMatrix); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 387 | class AAStrokeRectOp final : public GrMeshDrawOp { |
| 388 | private: |
| 389 | using Helper = GrSimpleMeshDrawOpHelper; |
| 390 | |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 391 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 392 | DEFINE_OP_CLASS_ID |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 393 | |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 394 | // TODO support AA rotated stroke rects by copying around view matrices |
| 395 | struct RectInfo { |
| 396 | SkPMColor4f fColor; |
| 397 | SkRect fDevOutside; |
| 398 | SkRect fDevOutsideAssist; |
| 399 | SkRect fDevInside; |
| 400 | SkVector fDevHalfStrokeSize; |
| 401 | bool fDegenerate; |
| 402 | }; |
| 403 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 404 | static GrOp::Owner Make(GrRecordingContext* context, |
| 405 | GrPaint&& paint, |
| 406 | const SkMatrix& viewMatrix, |
| 407 | const SkRect& devOutside, |
| 408 | const SkRect& devInside, |
| 409 | const SkVector& devHalfStrokeSize) { |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 410 | if (!viewMatrix.rectStaysRect()) { |
| 411 | // The AA op only supports axis-aligned rectangles |
| 412 | return nullptr; |
| 413 | } |
| 414 | if (!stroke_dev_half_size_supported(devHalfStrokeSize)) { |
| 415 | return nullptr; |
| 416 | } |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 417 | return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 418 | devOutside, devInside, devHalfStrokeSize); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 419 | } |
| 420 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 421 | AAStrokeRectOp(GrProcessorSet* processorSet, const SkPMColor4f& color, |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 422 | const SkMatrix& viewMatrix, const SkRect& devOutside, const SkRect& devInside, |
| 423 | const SkVector& devHalfStrokeSize) |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 424 | : INHERITED(ClassID()) |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 425 | , fHelper(processorSet, GrAAType::kCoverage) |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 426 | , fViewMatrix(viewMatrix) { |
caryclark | d656200 | 2016-07-27 12:02:07 -0700 | [diff] [blame] | 427 | SkASSERT(!devOutside.isEmpty()); |
| 428 | SkASSERT(!devInside.isEmpty()); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 429 | |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 430 | fRects.emplace_back(RectInfo{color, devOutside, devOutside, devInside, devHalfStrokeSize, false}); |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 431 | this->setBounds(devOutside, HasAABloat::kYes, IsHairline::kNo); |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 432 | fMiterStroke = true; |
| 433 | } |
| 434 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 435 | static GrOp::Owner Make(GrRecordingContext* context, |
| 436 | GrPaint&& paint, |
| 437 | const SkMatrix& viewMatrix, |
| 438 | const SkRect& rect, |
| 439 | const SkStrokeRec& stroke) { |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 440 | if (!viewMatrix.rectStaysRect()) { |
| 441 | // The AA op only supports axis-aligned rectangles |
| 442 | return nullptr; |
| 443 | } |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 444 | bool isMiter; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 445 | if (!allowed_stroke(stroke, GrAA::kYes, &isMiter)) { |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 446 | return nullptr; |
| 447 | } |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 448 | RectInfo info; |
Chris Dalton | 4f48910 | 2021-07-14 14:44:28 -0600 | [diff] [blame] | 449 | if (!compute_aa_rects(*context->priv().caps(), |
| 450 | &info.fDevOutside, |
| 451 | &info.fDevOutsideAssist, |
| 452 | &info.fDevInside, |
| 453 | &info.fDegenerate, |
| 454 | viewMatrix, |
| 455 | rect, |
| 456 | stroke.getWidth(), |
| 457 | isMiter, |
| 458 | &info.fDevHalfStrokeSize)) { |
| 459 | return nullptr; |
| 460 | } |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 461 | if (!stroke_dev_half_size_supported(info.fDevHalfStrokeSize)) { |
| 462 | return nullptr; |
| 463 | } |
| 464 | return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, info, |
| 465 | isMiter); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 466 | } |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 467 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 468 | AAStrokeRectOp(GrProcessorSet* processorSet, const SkPMColor4f& color, |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 469 | const SkMatrix& viewMatrix, const RectInfo& infoExceptColor, bool isMiter) |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 470 | : INHERITED(ClassID()) |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 471 | , fHelper(processorSet, GrAAType::kCoverage) |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 472 | , fViewMatrix(viewMatrix) { |
| 473 | fMiterStroke = isMiter; |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 474 | RectInfo& info = fRects.push_back(infoExceptColor); |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 475 | info.fColor = color; |
Brian Salomon | 510dd42 | 2017-03-16 12:15:22 -0400 | [diff] [blame] | 476 | if (isMiter) { |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 477 | this->setBounds(info.fDevOutside, HasAABloat::kYes, IsHairline::kNo); |
Brian Salomon | 510dd42 | 2017-03-16 12:15:22 -0400 | [diff] [blame] | 478 | } else { |
| 479 | // The outer polygon of the bevel stroke is an octagon specified by the points of a |
| 480 | // pair of overlapping rectangles where one is wide and the other is narrow. |
| 481 | SkRect bounds = info.fDevOutside; |
| 482 | bounds.joinPossiblyEmptyRect(info.fDevOutsideAssist); |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 483 | this->setBounds(bounds, HasAABloat::kYes, IsHairline::kNo); |
Brian Salomon | 510dd42 | 2017-03-16 12:15:22 -0400 | [diff] [blame] | 484 | } |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | const char* name() const override { return "AAStrokeRect"; } |
| 488 | |
Robert Phillips | 294723d | 2021-06-17 09:23:58 -0400 | [diff] [blame] | 489 | void visitProxies(const GrVisitProxyFunc& func) const override { |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 490 | if (fProgramInfo) { |
Chris Dalton | be45742 | 2020-03-16 18:05:03 -0600 | [diff] [blame] | 491 | fProgramInfo->visitFPProxies(func); |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 492 | } else { |
| 493 | fHelper.visitProxies(func); |
| 494 | } |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 495 | } |
| 496 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 497 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
Brian Salomon | a0485d9 | 2017-06-14 19:08:01 -0400 | [diff] [blame] | 498 | |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 499 | GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 500 | GrClampType clampType) override { |
| 501 | return fHelper.finalizeProcessors(caps, clip, clampType, |
| 502 | GrProcessorAnalysisCoverage::kSingleChannel, |
| 503 | &fRects.back().fColor, &fWideColor); |
Brian Salomon | a0485d9 | 2017-06-14 19:08:01 -0400 | [diff] [blame] | 504 | } |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 505 | |
| 506 | private: |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 507 | GrProgramInfo* programInfo() override { return fProgramInfo; } |
| 508 | |
Chris Dalton | 97f8590 | 2021-07-14 03:16:22 -0600 | [diff] [blame] | 509 | bool compatibleWithCoverageAsAlpha(bool usesMSAASurface) const { |
| 510 | // When MSAA is enabled, we have to extend our AA bloats and interpolate coverage values |
| 511 | // outside 0..1. This makes us incompatible with coverage as alpha. |
| 512 | return !usesMSAASurface && fHelper.compatibleWithCoverageAsAlpha(); |
| 513 | } |
| 514 | |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 515 | void onCreateProgramInfo(const GrCaps*, |
| 516 | SkArenaAlloc*, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 517 | const GrSurfaceProxyView& writeView, |
Chris Dalton | 6aaf00f | 2021-07-13 13:26:39 -0600 | [diff] [blame] | 518 | bool usesMSAASurface, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 519 | GrAppliedClip&&, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame] | 520 | const GrDstProxyView&, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 521 | GrXferBarrierFlags renderPassXferBarriers, |
| 522 | GrLoadOp colorLoadOp) override; |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 523 | |
Robert Phillips | 7114395 | 2021-06-17 14:55:07 -0400 | [diff] [blame] | 524 | void onPrepareDraws(GrMeshDrawTarget*) override; |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 525 | void onExecute(GrOpFlushState*, const SkRect& chainBounds) override; |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 526 | |
John Stiles | af36652 | 2020-08-13 09:57:34 -0400 | [diff] [blame] | 527 | #if GR_TEST_UTILS |
| 528 | SkString onDumpInfo() const override { |
| 529 | SkString string; |
| 530 | for (const auto& info : fRects) { |
| 531 | string.appendf( |
| 532 | "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], " |
| 533 | "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], " |
| 534 | "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d", |
| 535 | info.fColor.toBytes_RGBA(), info.fDevOutside.fLeft, info.fDevOutside.fTop, |
| 536 | info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft, |
| 537 | info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight, |
| 538 | info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop, |
| 539 | info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate); |
| 540 | } |
| 541 | string += fHelper.dumpInfo(); |
| 542 | return string; |
| 543 | } |
| 544 | #endif |
| 545 | |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 546 | static const int kMiterIndexCnt = 3 * 24; |
| 547 | static const int kMiterVertexCnt = 16; |
| 548 | static const int kNumMiterRectsInIndexBuffer = 256; |
| 549 | |
| 550 | static const int kBevelIndexCnt = 48 + 36 + 24; |
| 551 | static const int kBevelVertexCnt = 24; |
| 552 | static const int kNumBevelRectsInIndexBuffer = 256; |
| 553 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 554 | static sk_sp<const GrGpuBuffer> GetIndexBuffer(GrResourceProvider*, bool miterStroke); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 555 | |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 556 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
| 557 | bool miterStroke() const { return fMiterStroke; } |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 558 | |
Herb Derby | e25c300 | 2020-10-27 15:57:27 -0400 | [diff] [blame] | 559 | CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps&) override; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 560 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 561 | void generateAAStrokeRectGeometry(GrVertexWriter& vertices, |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 562 | const SkPMColor4f& color, |
| 563 | bool wideColor, |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 564 | const SkRect& devOutside, |
| 565 | const SkRect& devOutsideAssist, |
| 566 | const SkRect& devInside, |
| 567 | bool miterStroke, |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 568 | bool degenerate, |
Chris Dalton | 97f8590 | 2021-07-14 03:16:22 -0600 | [diff] [blame] | 569 | const SkVector& devHalfStrokeSize, |
| 570 | bool usesMSAASurface) const; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 571 | |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 572 | Helper fHelper; |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 573 | SkSTArray<1, RectInfo, true> fRects; |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 574 | SkMatrix fViewMatrix; |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 575 | GrSimpleMesh* fMesh = nullptr; |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 576 | GrProgramInfo* fProgramInfo = nullptr; |
| 577 | bool fMiterStroke; |
| 578 | bool fWideColor; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 579 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 580 | using INHERITED = GrMeshDrawOp; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 581 | }; |
| 582 | |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 583 | void AAStrokeRectOp::onCreateProgramInfo(const GrCaps* caps, |
| 584 | SkArenaAlloc* arena, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 585 | const GrSurfaceProxyView& writeView, |
Chris Dalton | 6aaf00f | 2021-07-13 13:26:39 -0600 | [diff] [blame] | 586 | bool usesMSAASurface, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 587 | GrAppliedClip&& appliedClip, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame] | 588 | const GrDstProxyView& dstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 589 | GrXferBarrierFlags renderPassXferBarriers, |
| 590 | GrLoadOp colorLoadOp) { |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 591 | |
Chris Dalton | 97f8590 | 2021-07-14 03:16:22 -0600 | [diff] [blame] | 592 | GrGeometryProcessor* gp = create_aa_stroke_rect_gp( |
| 593 | arena, |
| 594 | usesMSAASurface, |
| 595 | this->compatibleWithCoverageAsAlpha(usesMSAASurface), |
| 596 | this->viewMatrix(), |
| 597 | fHelper.usesLocalCoords(), |
| 598 | fWideColor); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 599 | if (!gp) { |
| 600 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 601 | return; |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 602 | } |
| 603 | |
Chris Dalton | eb0195e | 2021-08-18 21:39:02 -0600 | [diff] [blame] | 604 | fProgramInfo = fHelper.createProgramInfo(caps, |
| 605 | arena, |
| 606 | writeView, |
Chris Dalton | 2a26c50 | 2021-08-26 10:05:11 -0600 | [diff] [blame] | 607 | usesMSAASurface, |
Chris Dalton | eb0195e | 2021-08-18 21:39:02 -0600 | [diff] [blame] | 608 | std::move(appliedClip), |
| 609 | dstProxyView, |
| 610 | gp, |
| 611 | GrPrimitiveType::kTriangles, |
| 612 | renderPassXferBarriers, |
| 613 | colorLoadOp); |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 614 | } |
| 615 | |
Robert Phillips | 7114395 | 2021-06-17 14:55:07 -0400 | [diff] [blame] | 616 | void AAStrokeRectOp::onPrepareDraws(GrMeshDrawTarget* target) { |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 617 | |
| 618 | if (!fProgramInfo) { |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 619 | this->createProgramInfo(target); |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 620 | if (!fProgramInfo) { |
| 621 | return; |
| 622 | } |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 623 | } |
| 624 | |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 625 | int innerVertexNum = 4; |
| 626 | int outerVertexNum = this->miterStroke() ? 4 : 8; |
| 627 | int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2; |
| 628 | int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt; |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 629 | int instanceCount = fRects.count(); |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 630 | int maxQuads = this->miterStroke() ? kNumMiterRectsInIndexBuffer : kNumBevelRectsInIndexBuffer; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 631 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 632 | sk_sp<const GrGpuBuffer> indexBuffer = |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 633 | GetIndexBuffer(target->resourceProvider(), this->miterStroke()); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 634 | if (!indexBuffer) { |
| 635 | SkDebugf("Could not allocate indices\n"); |
| 636 | return; |
| 637 | } |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 638 | PatternHelper helper(target, GrPrimitiveType::kTriangles, |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 639 | fProgramInfo->geomProc().vertexStride(), std::move(indexBuffer), |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 640 | verticesPerInstance, indicesPerInstance, instanceCount, maxQuads); |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 641 | GrVertexWriter vertices{ helper.vertices() }; |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 642 | if (!vertices.fPtr) { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 643 | SkDebugf("Could not allocate vertices\n"); |
| 644 | return; |
| 645 | } |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 646 | |
| 647 | for (int i = 0; i < instanceCount; i++) { |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 648 | const RectInfo& info = fRects[i]; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 649 | this->generateAAStrokeRectGeometry(vertices, |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 650 | info.fColor, |
| 651 | fWideColor, |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 652 | info.fDevOutside, |
| 653 | info.fDevOutsideAssist, |
| 654 | info.fDevInside, |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 655 | fMiterStroke, |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 656 | info.fDegenerate, |
Chris Dalton | 97f8590 | 2021-07-14 03:16:22 -0600 | [diff] [blame] | 657 | info.fDevHalfStrokeSize, |
| 658 | target->usesMSAASurface()); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 659 | } |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 660 | fMesh = helper.mesh(); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | void AAStrokeRectOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 664 | if (!fProgramInfo || !fMesh) { |
| 665 | return; |
| 666 | } |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 667 | |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 668 | flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 669 | flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline()); |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 670 | flushState->drawMesh(*fMesh); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 673 | sk_sp<const GrGpuBuffer> AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider, |
| 674 | bool miterStroke) { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 675 | if (miterStroke) { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 676 | // clang-format off |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 677 | static const uint16_t gMiterIndices[] = { |
| 678 | 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0, |
| 679 | 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0, |
| 680 | 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0, |
| 681 | 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0, |
| 682 | |
| 683 | 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4, |
| 684 | 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4, |
| 685 | 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4, |
| 686 | 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4, |
| 687 | |
| 688 | 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8, |
| 689 | 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8, |
| 690 | 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8, |
| 691 | 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8, |
| 692 | }; |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 693 | // clang-format on |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 694 | static_assert(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 695 | GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey); |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 696 | return resourceProvider->findOrCreatePatternedIndexBuffer( |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 697 | gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt, |
| 698 | gMiterIndexBufferKey); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 699 | } else { |
| 700 | /** |
| 701 | * As in miter-stroke, index = a + b, and a is the current index, b is the shift |
| 702 | * from the first index. The index layout: |
| 703 | * outer AA line: 0~3, 4~7 |
| 704 | * outer edge: 8~11, 12~15 |
| 705 | * inner edge: 16~19 |
| 706 | * inner AA line: 20~23 |
| 707 | * Following comes a bevel-stroke rect and its indices: |
| 708 | * |
| 709 | * 4 7 |
| 710 | * ********************************* |
| 711 | * * ______________________________ * |
| 712 | * * / 12 15 \ * |
| 713 | * * / \ * |
| 714 | * 0 * |8 16_____________________19 11 | * 3 |
| 715 | * * | | | | * |
| 716 | * * | | **************** | | * |
| 717 | * * | | * 20 23 * | | * |
| 718 | * * | | * * | | * |
| 719 | * * | | * 21 22 * | | * |
| 720 | * * | | **************** | | * |
| 721 | * * | |____________________| | * |
| 722 | * 1 * |9 17 18 10| * 2 |
| 723 | * * \ / * |
| 724 | * * \13 __________________________14/ * |
| 725 | * * * |
| 726 | * ********************************** |
| 727 | * 5 6 |
| 728 | */ |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 729 | // clang-format off |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 730 | static const uint16_t gBevelIndices[] = { |
| 731 | // Draw outer AA, from outer AA line to outer edge, shift is 0. |
| 732 | 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0, |
| 733 | 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0, |
| 734 | 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0, |
| 735 | 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0, |
| 736 | 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0, |
| 737 | 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0, |
| 738 | 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0, |
| 739 | 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0, |
| 740 | |
| 741 | // Draw the stroke, from outer edge to inner edge, shift is 8. |
| 742 | 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8, |
| 743 | 1 + 8, 5 + 8, 9 + 8, |
| 744 | 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8, |
| 745 | 6 + 8, 2 + 8, 10 + 8, |
| 746 | 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8, |
| 747 | 3 + 8, 7 + 8, 11 + 8, |
| 748 | 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8, |
| 749 | 4 + 8, 0 + 8, 8 + 8, |
| 750 | |
| 751 | // Draw the inner AA, from inner edge to inner AA line, shift is 16. |
| 752 | 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16, |
| 753 | 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16, |
| 754 | 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16, |
| 755 | 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16, |
| 756 | }; |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 757 | // clang-format on |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 758 | static_assert(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 759 | |
| 760 | GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey); |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 761 | return resourceProvider->findOrCreatePatternedIndexBuffer( |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 762 | gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt, |
| 763 | gBevelIndexBufferKey); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | |
Herb Derby | e25c300 | 2020-10-27 15:57:27 -0400 | [diff] [blame] | 767 | GrOp::CombineResult AAStrokeRectOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) |
| 768 | { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 769 | AAStrokeRectOp* that = t->cast<AAStrokeRectOp>(); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 770 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 771 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 772 | return CombineResult::kCannotCombine; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 773 | } |
| 774 | |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 775 | // TODO combine across miterstroke changes |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 776 | if (this->miterStroke() != that->miterStroke()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 777 | return CombineResult::kCannotCombine; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | // We apply the viewmatrix to the rect points on the cpu. However, if the pipeline uses |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 781 | // local coords then we won't be able to combine. TODO: Upload local coords as an attribute. |
Mike Reed | 2c38315 | 2019-12-18 16:47:47 -0500 | [diff] [blame] | 782 | if (fHelper.usesLocalCoords() && |
| 783 | !SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix())) |
| 784 | { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 785 | return CombineResult::kCannotCombine; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 786 | } |
| 787 | |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 788 | fRects.push_back_n(that->fRects.count(), that->fRects.begin()); |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 789 | fWideColor |= that->fWideColor; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 790 | return CombineResult::kMerged; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 791 | } |
| 792 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 793 | void AAStrokeRectOp::generateAAStrokeRectGeometry(GrVertexWriter& vertices, |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 794 | const SkPMColor4f& color, |
| 795 | bool wideColor, |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 796 | const SkRect& devOutside, |
| 797 | const SkRect& devOutsideAssist, |
| 798 | const SkRect& devInside, |
| 799 | bool miterStroke, |
| 800 | bool degenerate, |
Chris Dalton | 97f8590 | 2021-07-14 03:16:22 -0600 | [diff] [blame] | 801 | const SkVector& devHalfStrokeSize, |
| 802 | bool usesMSAASurface) const { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 803 | // We create vertices for four nested rectangles. There are two ramps from 0 to full |
| 804 | // coverage, one on the exterior of the stroke and the other on the interior. |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 805 | |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 806 | // The following code only works if either devStrokeSize's fX and fY are |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 807 | // equal (in which case innerCoverage is same for all sides of the rects) or |
| 808 | // if devStrokeSize's fX and fY are both greater than 1.0 (in which case |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 809 | // innerCoverage will always be 1). |
| 810 | SkASSERT(stroke_dev_half_size_supported(devHalfStrokeSize)); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 811 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 812 | auto inset_fan = [](const SkRect& r, SkScalar dx, SkScalar dy) { |
| 813 | return GrVertexWriter::TriFanFromRect(r.makeInset(dx, dy)); |
| 814 | }; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 815 | |
Chris Dalton | 97f8590 | 2021-07-14 03:16:22 -0600 | [diff] [blame] | 816 | bool tweakAlphaForCoverage = this->compatibleWithCoverageAsAlpha(usesMSAASurface); |
| 817 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 818 | auto maybe_coverage = [tweakAlphaForCoverage](float coverage) { |
| 819 | return GrVertexWriter::If(!tweakAlphaForCoverage, coverage); |
| 820 | }; |
| 821 | |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 822 | // How much do we inset toward the inside of the strokes? |
Michael Ludwig | 144926d | 2021-09-10 09:34:47 -0400 | [diff] [blame] | 823 | float inset = std::min(0.5f, std::min(devHalfStrokeSize.fX, devHalfStrokeSize.fY)); |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 824 | float innerCoverage = 1; |
Michael Ludwig | 144926d | 2021-09-10 09:34:47 -0400 | [diff] [blame] | 825 | if (inset < 0.5f) { |
| 826 | // Stroke is subpixel, so reduce the coverage to simulate the narrower strokes. |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 827 | innerCoverage = 2 * inset / (inset + .5f); |
| 828 | } |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 829 | |
| 830 | // How much do we outset away from the outside of the strokes? |
| 831 | // We always want to keep the AA picture frame one pixel wide. |
| 832 | float outset = 1 - inset; |
| 833 | float outerCoverage = 0; |
Chris Dalton | 97f8590 | 2021-07-14 03:16:22 -0600 | [diff] [blame] | 834 | |
| 835 | // How much do we outset away from the interior side of the stroke (toward the center)? |
| 836 | float interiorOutset = outset; |
| 837 | float interiorCoverage = outerCoverage; |
| 838 | |
| 839 | if (usesMSAASurface) { |
| 840 | // Since we're using MSAA, extend our outsets to ensure any pixel with partial coverage has |
| 841 | // a full sample mask. |
| 842 | constexpr float msaaExtraBloat = SK_ScalarSqrt2 - .5f; |
| 843 | outset += msaaExtraBloat; |
| 844 | outerCoverage -= msaaExtraBloat; |
| 845 | |
| 846 | float insetExtraBloat = |
| 847 | std::min(inset + msaaExtraBloat, |
| 848 | std::min(devHalfStrokeSize.fX, devHalfStrokeSize.fY)) - inset; |
| 849 | inset += insetExtraBloat; |
| 850 | innerCoverage += insetExtraBloat; |
| 851 | |
| 852 | float interiorExtraBloat = |
| 853 | std::min(interiorOutset + msaaExtraBloat, |
| 854 | std::min(devInside.width(), devInside.height()) / 2) - interiorOutset; |
| 855 | interiorOutset += interiorExtraBloat; |
| 856 | interiorCoverage -= interiorExtraBloat; |
| 857 | } |
| 858 | |
| 859 | GrVertexColor innerColor(tweakAlphaForCoverage ? color * innerCoverage : color, wideColor); |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 860 | GrVertexColor outerColor(tweakAlphaForCoverage ? SK_PMColor4fTRANSPARENT : color, wideColor); |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 861 | |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 862 | // Exterior outset rect (away from stroke). |
| 863 | vertices.writeQuad(inset_fan(devOutside, -outset, -outset), |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 864 | outerColor, |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 865 | maybe_coverage(outerCoverage)); |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 866 | |
| 867 | if (!miterStroke) { |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 868 | // Second exterior outset. |
| 869 | vertices.writeQuad(inset_fan(devOutsideAssist, -outset, -outset), |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 870 | outerColor, |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 871 | maybe_coverage(outerCoverage)); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 874 | // Exterior inset rect (toward stroke). |
| 875 | vertices.writeQuad(inset_fan(devOutside, inset, inset), |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 876 | innerColor, |
| 877 | maybe_coverage(innerCoverage)); |
| 878 | |
| 879 | if (!miterStroke) { |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 880 | // Second exterior inset. |
| 881 | vertices.writeQuad(inset_fan(devOutsideAssist, inset, inset), |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 882 | innerColor, |
| 883 | maybe_coverage(innerCoverage)); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 884 | } |
| 885 | |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 886 | if (!degenerate) { |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 887 | // Interior inset rect (toward stroke). |
| 888 | vertices.writeQuad(inset_fan(devInside, -inset, -inset), |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 889 | innerColor, |
| 890 | maybe_coverage(innerCoverage)); |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 891 | |
Chris Dalton | 3ee0dc6 | 2021-07-14 01:36:49 -0600 | [diff] [blame] | 892 | // Interior outset rect (away from stroke, toward center of rect). |
Chris Dalton | 97f8590 | 2021-07-14 03:16:22 -0600 | [diff] [blame] | 893 | SkRect interiorAABoundary = devInside.makeInset(interiorOutset, interiorOutset); |
Chris Dalton | bdc2cd0 | 2021-07-14 01:54:38 -0600 | [diff] [blame] | 894 | float coverageBackset = 0; // Adds back coverage when the interior AA edges cross. |
| 895 | if (interiorAABoundary.fLeft > interiorAABoundary.fRight) { |
Chris Dalton | 97f8590 | 2021-07-14 03:16:22 -0600 | [diff] [blame] | 896 | coverageBackset = |
| 897 | (interiorAABoundary.fLeft - interiorAABoundary.fRight) / (interiorOutset * 2); |
Chris Dalton | bdc2cd0 | 2021-07-14 01:54:38 -0600 | [diff] [blame] | 898 | interiorAABoundary.fLeft = interiorAABoundary.fRight = interiorAABoundary.centerX(); |
| 899 | } |
| 900 | if (interiorAABoundary.fTop > interiorAABoundary.fBottom) { |
| 901 | coverageBackset = std::max( |
Chris Dalton | 97f8590 | 2021-07-14 03:16:22 -0600 | [diff] [blame] | 902 | (interiorAABoundary.fTop - interiorAABoundary.fBottom) / (interiorOutset * 2), |
Chris Dalton | bdc2cd0 | 2021-07-14 01:54:38 -0600 | [diff] [blame] | 903 | coverageBackset); |
| 904 | interiorAABoundary.fTop = interiorAABoundary.fBottom = interiorAABoundary.centerY(); |
| 905 | } |
| 906 | if (coverageBackset > 0) { |
| 907 | // The interior edges crossed. Lerp back toward innerCoverage, which is what this op |
| 908 | // will draw in the degenerate case. This gives a smooth transition into the degenerate |
| 909 | // case. |
| 910 | interiorCoverage += interiorCoverage * (1 - coverageBackset) + |
| 911 | innerCoverage * coverageBackset; |
| 912 | } |
| 913 | GrVertexColor interiorColor(tweakAlphaForCoverage ? color * interiorCoverage : color, |
| 914 | wideColor); |
| 915 | vertices.writeQuad(GrVertexWriter::TriFanFromRect(interiorAABoundary), |
| 916 | interiorColor, |
| 917 | maybe_coverage(interiorCoverage)); |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 918 | } else { |
| 919 | // When the interior rect has become degenerate we smoosh to a single point |
| 920 | SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom); |
| 921 | |
| 922 | vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside), |
| 923 | innerColor, |
| 924 | maybe_coverage(innerCoverage)); |
| 925 | |
| 926 | // ... unless we are degenerate, in which case we must apply the scaled coverage |
| 927 | vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside), |
| 928 | innerColor, |
| 929 | maybe_coverage(innerCoverage)); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 933 | } // anonymous namespace |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 934 | |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 935 | namespace skgpu::v1::StrokeRectOp { |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 936 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 937 | GrOp::Owner Make(GrRecordingContext* context, |
| 938 | GrPaint&& paint, |
| 939 | GrAAType aaType, |
| 940 | const SkMatrix& viewMatrix, |
| 941 | const SkRect& rect, |
| 942 | const SkStrokeRec& stroke) { |
Brian Salomon | b18a947 | 2021-06-23 10:03:07 -0400 | [diff] [blame] | 943 | SkASSERT(!context->priv().caps()->reducedShaderMode()); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 944 | if (aaType == GrAAType::kCoverage) { |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 945 | return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke); |
| 946 | } else { |
| 947 | return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke, aaType); |
| 948 | } |
| 949 | } |
| 950 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 951 | GrOp::Owner MakeNested(GrRecordingContext* context, |
| 952 | GrPaint&& paint, |
| 953 | const SkMatrix& viewMatrix, |
| 954 | const SkRect rects[2]) { |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 955 | SkASSERT(viewMatrix.rectStaysRect()); |
| 956 | SkASSERT(!rects[0].isEmpty() && !rects[1].isEmpty()); |
| 957 | |
Chris Dalton | 0196a72 | 2021-07-14 18:59:59 -0600 | [diff] [blame] | 958 | SkRect devOutside = viewMatrix.mapRect(rects[0]); |
| 959 | SkRect devInside = viewMatrix.mapRect(rects[1]); |
| 960 | float dx = devOutside.fRight - devInside.fRight; |
| 961 | float dy = devOutside.fBottom - devInside.fBottom; |
| 962 | |
Chris Dalton | 4f48910 | 2021-07-14 14:44:28 -0600 | [diff] [blame] | 963 | // Clips our draw rects 1 full pixel outside the viewport. This avoids interpolation precision |
| 964 | // issues with very large coordinates. |
Chris Dalton | 0196a72 | 2021-07-14 18:59:59 -0600 | [diff] [blame] | 965 | const float m = context->priv().caps()->maxRenderTargetSize(); |
Chris Dalton | 4f48910 | 2021-07-14 14:44:28 -0600 | [diff] [blame] | 966 | const SkRect visibilityBounds = SkRect::MakeWH(m, m).makeOutset(1, 1); |
| 967 | |
Chris Dalton | 0196a72 | 2021-07-14 18:59:59 -0600 | [diff] [blame] | 968 | if (!devOutside.intersect(visibilityBounds.makeOutset(dx, dy))) { |
Chris Dalton | 4f48910 | 2021-07-14 14:44:28 -0600 | [diff] [blame] | 969 | return nullptr; |
| 970 | } |
| 971 | |
Chris Dalton | 4f48910 | 2021-07-14 14:44:28 -0600 | [diff] [blame] | 972 | if (devInside.isEmpty() || !devInside.intersect(visibilityBounds)) { |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 973 | if (devOutside.isEmpty()) { |
| 974 | return nullptr; |
| 975 | } |
Michael Ludwig | 6b45c5d | 2020-02-07 09:56:38 -0500 | [diff] [blame] | 976 | DrawQuad quad{GrQuad::MakeFromRect(rects[0], viewMatrix), GrQuad(rects[0]), |
| 977 | GrQuadAAFlags::kAll}; |
Robert Phillips | fbf0214 | 2021-09-01 16:31:34 -0400 | [diff] [blame] | 978 | return FillRectOp::Make(context, std::move(paint), GrAAType::kCoverage, &quad); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 979 | } |
| 980 | |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 981 | return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, devOutside, |
Chris Dalton | 0196a72 | 2021-07-14 18:59:59 -0600 | [diff] [blame] | 982 | devInside, SkVector{dx, dy} * .5f); |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 983 | } |
| 984 | |
Robert Phillips | cadd5db | 2021-08-30 11:37:18 -0400 | [diff] [blame] | 985 | } // namespace skgpu::v1::StrokeRectOp |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 986 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 987 | #if GR_TEST_UTILS |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 988 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 989 | #include "src/gpu/GrDrawOpTest.h" |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 990 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 991 | GR_DRAW_OP_TEST_DEFINE(NonAAStrokeRectOp) { |
| 992 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 993 | SkRect rect = GrTest::TestRect(random); |
| 994 | SkScalar strokeWidth = random->nextBool() ? 0.0f : 2.0f; |
| 995 | SkPaint strokePaint; |
| 996 | strokePaint.setStrokeWidth(strokeWidth); |
| 997 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 998 | strokePaint.setStrokeJoin(SkPaint::kMiter_Join); |
| 999 | SkStrokeRec strokeRec(strokePaint); |
| 1000 | GrAAType aaType = GrAAType::kNone; |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 1001 | if (numSamples > 1) { |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 1002 | aaType = random->nextBool() ? GrAAType::kMSAA : GrAAType::kNone; |
| 1003 | } |
| 1004 | return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, strokeRec, aaType); |
| 1005 | } |
| 1006 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 1007 | GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp) { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 1008 | bool miterStroke = random->nextBool(); |
| 1009 | |
bsalomon | 40ef485 | 2016-05-02 13:22:13 -0700 | [diff] [blame] | 1010 | // Create either a empty rect or a non-empty rect. |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 1011 | SkRect rect = |
| 1012 | random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0); |
Brian Osman | 116b33e | 2020-02-05 13:34:09 -0500 | [diff] [blame] | 1013 | SkScalar minDim = std::min(rect.width(), rect.height()); |
bsalomon | 40ef485 | 2016-05-02 13:22:13 -0700 | [diff] [blame] | 1014 | SkScalar strokeWidth = random->nextUScalar1() * minDim; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 1015 | |
bsalomon | 40ef485 | 2016-05-02 13:22:13 -0700 | [diff] [blame] | 1016 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
| 1017 | rec.setStrokeStyle(strokeWidth); |
| 1018 | rec.setStrokeParams(SkPaint::kButt_Cap, |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 1019 | miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f); |
bsalomon | 40ef485 | 2016-05-02 13:22:13 -0700 | [diff] [blame] | 1020 | SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 1021 | return AAStrokeRectOp::Make(context, std::move(paint), matrix, rect, rec); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | #endif |