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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/ops/GrStrokeRectOp.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" |
| 22 | #include "src/gpu/ops/GrFillRectOp.h" |
| 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. |
| 31 | inline static 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 | */ |
| 61 | static void init_nonaa_stroke_rect_strip(SkPoint verts[10], const SkRect& rect, SkScalar width) { |
| 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 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame] | 94 | void visitProxies(const VisitProxyFunc& 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, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 175 | GrAppliedClip&& clip, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame^] | 176 | const GrDstProxyView& dstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 177 | GrXferBarrierFlags renderPassXferBarriers, |
| 178 | GrLoadOp colorLoadOp) override { |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 179 | GrGeometryProcessor* gp; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 180 | { |
| 181 | using namespace GrDefaultGeoProcFactory; |
| 182 | Color color(fColor); |
| 183 | LocalCoords::Type localCoordsType = fHelper.usesLocalCoords() |
| 184 | ? LocalCoords::kUsePosition_Type |
| 185 | : LocalCoords::kUnused_Type; |
Brian Osman | f0aee74 | 2020-03-12 09:28:44 -0400 | [diff] [blame] | 186 | gp = GrDefaultGeoProcFactory::Make(arena, color, Coverage::kSolid_Type, localCoordsType, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 187 | fViewMatrix); |
| 188 | } |
| 189 | |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 190 | GrPrimitiveType primType = (fStrokeWidth > 0) ? GrPrimitiveType::kTriangleStrip |
| 191 | : GrPrimitiveType::kLineStrip; |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 192 | |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 193 | fProgramInfo = fHelper.createProgramInfo(caps, arena, writeView, std::move(clip), |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 194 | dstProxyView, gp, primType, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 195 | renderPassXferBarriers, colorLoadOp); |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 196 | } |
| 197 | |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 198 | void onPrepareDraws(Target* target) override { |
| 199 | if (!fProgramInfo) { |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 200 | this->createProgramInfo(target); |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 201 | } |
| 202 | |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 203 | size_t kVertexStride = fProgramInfo->geomProc().vertexStride(); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 204 | int vertexCount = kVertsPerHairlineRect; |
| 205 | if (fStrokeWidth > 0) { |
| 206 | vertexCount = kVertsPerStrokeRect; |
| 207 | } |
| 208 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 209 | sk_sp<const GrBuffer> vertexBuffer; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 210 | int firstVertex; |
| 211 | |
| 212 | void* verts = |
| 213 | target->makeVertexSpace(kVertexStride, vertexCount, &vertexBuffer, &firstVertex); |
| 214 | |
| 215 | if (!verts) { |
| 216 | SkDebugf("Could not allocate vertices\n"); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | SkPoint* vertex = reinterpret_cast<SkPoint*>(verts); |
| 221 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 222 | if (fStrokeWidth > 0) { |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 223 | init_nonaa_stroke_rect_strip(vertex, fRect, fStrokeWidth); |
| 224 | } else { |
| 225 | // hairline |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 226 | vertex[0].set(fRect.fLeft, fRect.fTop); |
| 227 | vertex[1].set(fRect.fRight, fRect.fTop); |
| 228 | vertex[2].set(fRect.fRight, fRect.fBottom); |
| 229 | vertex[3].set(fRect.fLeft, fRect.fBottom); |
| 230 | vertex[4].set(fRect.fLeft, fRect.fTop); |
| 231 | } |
| 232 | |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 233 | fMesh = target->allocMesh(); |
Chris Dalton | 37c7bdd | 2020-03-13 09:21:12 -0600 | [diff] [blame] | 234 | fMesh->set(std::move(vertexBuffer), vertexCount, firstVertex); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 238 | if (!fMesh) { |
| 239 | return; |
| 240 | } |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 241 | |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 242 | flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 243 | flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline()); |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 244 | flushState->drawMesh(*fMesh); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 245 | } |
| 246 | |
John Stiles | af36652 | 2020-08-13 09:57:34 -0400 | [diff] [blame] | 247 | #if GR_TEST_UTILS |
| 248 | SkString onDumpInfo() const override { |
| 249 | return SkStringPrintf("Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], " |
| 250 | "StrokeWidth: %.2f\n%s", |
| 251 | fColor.toBytes_RGBA(), fRect.fLeft, fRect.fTop, fRect.fRight, |
| 252 | fRect.fBottom, fStrokeWidth, fHelper.dumpInfo().c_str()); |
| 253 | } |
| 254 | #endif |
| 255 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 256 | // TODO: override onCombineIfPossible |
| 257 | |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 258 | Helper fHelper; |
| 259 | SkPMColor4f fColor; |
| 260 | SkMatrix fViewMatrix; |
| 261 | SkRect fRect; |
| 262 | SkScalar fStrokeWidth; |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 263 | GrSimpleMesh* fMesh = nullptr; |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 264 | GrProgramInfo* fProgramInfo = nullptr; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 265 | |
| 266 | const static int kVertsPerHairlineRect = 5; |
| 267 | const static int kVertsPerStrokeRect = 10; |
| 268 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 269 | using INHERITED = GrMeshDrawOp; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 273 | // AA Stroking |
| 274 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 275 | |
| 276 | GR_DECLARE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey); |
| 277 | GR_DECLARE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey); |
| 278 | |
| 279 | static void compute_aa_rects(SkRect* devOutside, SkRect* devOutsideAssist, SkRect* devInside, |
| 280 | bool* isDegenerate, const SkMatrix& viewMatrix, const SkRect& rect, |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 281 | SkScalar strokeWidth, bool miterStroke, SkVector* devHalfStrokeSize) { |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 282 | SkRect devRect; |
| 283 | viewMatrix.mapRect(&devRect, rect); |
| 284 | |
| 285 | SkVector devStrokeSize; |
| 286 | if (strokeWidth > 0) { |
| 287 | devStrokeSize.set(strokeWidth, strokeWidth); |
| 288 | viewMatrix.mapVectors(&devStrokeSize, 1); |
| 289 | devStrokeSize.setAbs(devStrokeSize); |
| 290 | } else { |
| 291 | devStrokeSize.set(SK_Scalar1, SK_Scalar1); |
| 292 | } |
| 293 | |
| 294 | const SkScalar dx = devStrokeSize.fX; |
| 295 | const SkScalar dy = devStrokeSize.fY; |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 296 | const SkScalar rx = SkScalarHalf(dx); |
| 297 | const SkScalar ry = SkScalarHalf(dy); |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 298 | |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 299 | devHalfStrokeSize->fX = rx; |
| 300 | devHalfStrokeSize->fY = ry; |
| 301 | |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 302 | *devOutside = devRect; |
| 303 | *devOutsideAssist = devRect; |
| 304 | *devInside = devRect; |
| 305 | |
| 306 | devOutside->outset(rx, ry); |
| 307 | devInside->inset(rx, ry); |
| 308 | |
| 309 | // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we |
| 310 | // make a degenerate inside rect to avoid double hitting. We will also jam all of the points |
| 311 | // together when we render these rects. |
| 312 | SkScalar spare; |
| 313 | { |
| 314 | SkScalar w = devRect.width() - dx; |
| 315 | SkScalar h = devRect.height() - dy; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 316 | spare = std::min(w, h); |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | *isDegenerate = spare <= 0; |
| 320 | if (*isDegenerate) { |
| 321 | devInside->fLeft = devInside->fRight = devRect.centerX(); |
| 322 | devInside->fTop = devInside->fBottom = devRect.centerY(); |
| 323 | } |
| 324 | |
| 325 | // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist) |
| 326 | // to draw the outside of the octagon. Because there are 8 vertices on the outer |
| 327 | // edge, while vertex number of inner edge is 4, the same as miter-stroke. |
| 328 | if (!miterStroke) { |
| 329 | devOutside->inset(0, ry); |
| 330 | devOutsideAssist->outset(0, ry); |
| 331 | } |
| 332 | } |
| 333 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 334 | static GrGeometryProcessor* create_aa_stroke_rect_gp(SkArenaAlloc* arena, |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 335 | bool tweakAlphaForCoverage, |
| 336 | const SkMatrix& viewMatrix, |
| 337 | bool usesLocalCoords, |
| 338 | bool wideColor) { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 339 | using namespace GrDefaultGeoProcFactory; |
| 340 | |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 341 | Coverage::Type coverageType = |
| 342 | tweakAlphaForCoverage ? Coverage::kSolid_Type : Coverage::kAttribute_Type; |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 343 | LocalCoords::Type localCoordsType = |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 344 | usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type; |
| 345 | Color::Type colorType = |
| 346 | wideColor ? Color::kPremulWideColorAttribute_Type: Color::kPremulGrColorAttribute_Type; |
| 347 | |
Brian Osman | f0aee74 | 2020-03-12 09:28:44 -0400 | [diff] [blame] | 348 | return MakeForDeviceSpace(arena, colorType, coverageType, localCoordsType, viewMatrix); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 351 | class AAStrokeRectOp final : public GrMeshDrawOp { |
| 352 | private: |
| 353 | using Helper = GrSimpleMeshDrawOpHelper; |
| 354 | |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 355 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 356 | DEFINE_OP_CLASS_ID |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 357 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 358 | static GrOp::Owner Make(GrRecordingContext* context, |
| 359 | GrPaint&& paint, |
| 360 | const SkMatrix& viewMatrix, |
| 361 | const SkRect& devOutside, |
| 362 | const SkRect& devInside, |
| 363 | const SkVector& devHalfStrokeSize) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 364 | return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 365 | devOutside, devInside, devHalfStrokeSize); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 366 | } |
| 367 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 368 | AAStrokeRectOp(GrProcessorSet* processorSet, const SkPMColor4f& color, |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 369 | const SkMatrix& viewMatrix, const SkRect& devOutside, const SkRect& devInside, |
| 370 | const SkVector& devHalfStrokeSize) |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 371 | : INHERITED(ClassID()) |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 372 | , fHelper(processorSet, GrAAType::kCoverage) |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 373 | , fViewMatrix(viewMatrix) { |
caryclark | d656200 | 2016-07-27 12:02:07 -0700 | [diff] [blame] | 374 | SkASSERT(!devOutside.isEmpty()); |
| 375 | SkASSERT(!devInside.isEmpty()); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 376 | |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 377 | fRects.emplace_back(RectInfo{color, devOutside, devOutside, devInside, devHalfStrokeSize, false}); |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 378 | this->setBounds(devOutside, HasAABloat::kYes, IsHairline::kNo); |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 379 | fMiterStroke = true; |
| 380 | } |
| 381 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 382 | static GrOp::Owner Make(GrRecordingContext* context, |
| 383 | GrPaint&& paint, |
| 384 | const SkMatrix& viewMatrix, |
| 385 | const SkRect& rect, |
| 386 | const SkStrokeRec& stroke) { |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 387 | bool isMiter; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 388 | if (!allowed_stroke(stroke, GrAA::kYes, &isMiter)) { |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 389 | return nullptr; |
| 390 | } |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 391 | return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, rect, |
| 392 | stroke, isMiter); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 393 | } |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 394 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 395 | AAStrokeRectOp(GrProcessorSet* processorSet, const SkPMColor4f& color, |
Brian Osman | 936fe7d | 2018-10-30 15:30:35 -0400 | [diff] [blame] | 396 | const SkMatrix& viewMatrix, const SkRect& rect, const SkStrokeRec& stroke, |
| 397 | bool isMiter) |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 398 | : INHERITED(ClassID()) |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 399 | , fHelper(processorSet, GrAAType::kCoverage) |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 400 | , fViewMatrix(viewMatrix) { |
| 401 | fMiterStroke = isMiter; |
| 402 | RectInfo& info = fRects.push_back(); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 403 | compute_aa_rects(&info.fDevOutside, &info.fDevOutsideAssist, &info.fDevInside, |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 404 | &info.fDegenerate, viewMatrix, rect, stroke.getWidth(), isMiter, |
| 405 | &info.fDevHalfStrokeSize); |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 406 | info.fColor = color; |
Brian Salomon | 510dd42 | 2017-03-16 12:15:22 -0400 | [diff] [blame] | 407 | if (isMiter) { |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 408 | this->setBounds(info.fDevOutside, HasAABloat::kYes, IsHairline::kNo); |
Brian Salomon | 510dd42 | 2017-03-16 12:15:22 -0400 | [diff] [blame] | 409 | } else { |
| 410 | // The outer polygon of the bevel stroke is an octagon specified by the points of a |
| 411 | // pair of overlapping rectangles where one is wide and the other is narrow. |
| 412 | SkRect bounds = info.fDevOutside; |
| 413 | bounds.joinPossiblyEmptyRect(info.fDevOutsideAssist); |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 414 | this->setBounds(bounds, HasAABloat::kYes, IsHairline::kNo); |
Brian Salomon | 510dd42 | 2017-03-16 12:15:22 -0400 | [diff] [blame] | 415 | } |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | const char* name() const override { return "AAStrokeRect"; } |
| 419 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame] | 420 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 421 | if (fProgramInfo) { |
Chris Dalton | be45742 | 2020-03-16 18:05:03 -0600 | [diff] [blame] | 422 | fProgramInfo->visitFPProxies(func); |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 423 | } else { |
| 424 | fHelper.visitProxies(func); |
| 425 | } |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 426 | } |
| 427 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 428 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
Brian Salomon | a0485d9 | 2017-06-14 19:08:01 -0400 | [diff] [blame] | 429 | |
Chris Dalton | 57ab06c | 2021-04-22 12:57:28 -0600 | [diff] [blame] | 430 | GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 431 | GrClampType clampType) override { |
| 432 | return fHelper.finalizeProcessors(caps, clip, clampType, |
| 433 | GrProcessorAnalysisCoverage::kSingleChannel, |
| 434 | &fRects.back().fColor, &fWideColor); |
Brian Salomon | a0485d9 | 2017-06-14 19:08:01 -0400 | [diff] [blame] | 435 | } |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 436 | |
| 437 | private: |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 438 | GrProgramInfo* programInfo() override { return fProgramInfo; } |
| 439 | |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 440 | void onCreateProgramInfo(const GrCaps*, |
| 441 | SkArenaAlloc*, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 442 | const GrSurfaceProxyView& writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 443 | GrAppliedClip&&, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame^] | 444 | const GrDstProxyView&, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 445 | GrXferBarrierFlags renderPassXferBarriers, |
| 446 | GrLoadOp colorLoadOp) override; |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 447 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 448 | void onPrepareDraws(Target*) override; |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 449 | void onExecute(GrOpFlushState*, const SkRect& chainBounds) override; |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 450 | |
John Stiles | af36652 | 2020-08-13 09:57:34 -0400 | [diff] [blame] | 451 | #if GR_TEST_UTILS |
| 452 | SkString onDumpInfo() const override { |
| 453 | SkString string; |
| 454 | for (const auto& info : fRects) { |
| 455 | string.appendf( |
| 456 | "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], " |
| 457 | "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], " |
| 458 | "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d", |
| 459 | info.fColor.toBytes_RGBA(), info.fDevOutside.fLeft, info.fDevOutside.fTop, |
| 460 | info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft, |
| 461 | info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight, |
| 462 | info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop, |
| 463 | info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate); |
| 464 | } |
| 465 | string += fHelper.dumpInfo(); |
| 466 | return string; |
| 467 | } |
| 468 | #endif |
| 469 | |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 470 | static const int kMiterIndexCnt = 3 * 24; |
| 471 | static const int kMiterVertexCnt = 16; |
| 472 | static const int kNumMiterRectsInIndexBuffer = 256; |
| 473 | |
| 474 | static const int kBevelIndexCnt = 48 + 36 + 24; |
| 475 | static const int kBevelVertexCnt = 24; |
| 476 | static const int kNumBevelRectsInIndexBuffer = 256; |
| 477 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 478 | static sk_sp<const GrGpuBuffer> GetIndexBuffer(GrResourceProvider*, bool miterStroke); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 479 | |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 480 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
| 481 | bool miterStroke() const { return fMiterStroke; } |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 482 | |
Herb Derby | e25c300 | 2020-10-27 15:57:27 -0400 | [diff] [blame] | 483 | CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps&) override; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 484 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 485 | void generateAAStrokeRectGeometry(GrVertexWriter& vertices, |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 486 | const SkPMColor4f& color, |
| 487 | bool wideColor, |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 488 | const SkRect& devOutside, |
| 489 | const SkRect& devOutsideAssist, |
| 490 | const SkRect& devInside, |
| 491 | bool miterStroke, |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 492 | bool degenerate, |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 493 | bool tweakAlphaForCoverage, |
| 494 | const SkVector& devHalfStrokeSize) const; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 495 | |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 496 | // TODO support AA rotated stroke rects by copying around view matrices |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 497 | struct RectInfo { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 498 | SkPMColor4f fColor; |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 499 | SkRect fDevOutside; |
| 500 | SkRect fDevOutsideAssist; |
| 501 | SkRect fDevInside; |
| 502 | SkVector fDevHalfStrokeSize; |
| 503 | bool fDegenerate; |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 504 | }; |
| 505 | |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 506 | Helper fHelper; |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 507 | SkSTArray<1, RectInfo, true> fRects; |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 508 | SkMatrix fViewMatrix; |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 509 | GrSimpleMesh* fMesh = nullptr; |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 510 | GrProgramInfo* fProgramInfo = nullptr; |
| 511 | bool fMiterStroke; |
| 512 | bool fWideColor; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 513 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 514 | using INHERITED = GrMeshDrawOp; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 515 | }; |
| 516 | |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 517 | void AAStrokeRectOp::onCreateProgramInfo(const GrCaps* caps, |
| 518 | SkArenaAlloc* arena, |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 519 | const GrSurfaceProxyView& writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 520 | GrAppliedClip&& appliedClip, |
John Stiles | 52cb1d0 | 2021-06-02 11:58:05 -0400 | [diff] [blame^] | 521 | const GrDstProxyView& dstProxyView, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 522 | GrXferBarrierFlags renderPassXferBarriers, |
| 523 | GrLoadOp colorLoadOp) { |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 524 | |
| 525 | GrGeometryProcessor* gp = create_aa_stroke_rect_gp(arena, |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 526 | fHelper.compatibleWithCoverageAsAlpha(), |
| 527 | this->viewMatrix(), |
| 528 | fHelper.usesLocalCoords(), |
| 529 | fWideColor); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 530 | if (!gp) { |
| 531 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 532 | return; |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 533 | } |
| 534 | |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 535 | fProgramInfo = fHelper.createProgramInfo(caps, |
| 536 | arena, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 537 | writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 538 | std::move(appliedClip), |
| 539 | dstProxyView, |
| 540 | gp, |
Greg Daniel | d358cbe | 2020-09-11 09:33:54 -0400 | [diff] [blame] | 541 | GrPrimitiveType::kTriangles, |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 542 | renderPassXferBarriers, |
| 543 | colorLoadOp); |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 544 | } |
| 545 | |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 546 | void AAStrokeRectOp::onPrepareDraws(Target* target) { |
| 547 | |
| 548 | if (!fProgramInfo) { |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 549 | this->createProgramInfo(target); |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 550 | if (!fProgramInfo) { |
| 551 | return; |
| 552 | } |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 553 | } |
| 554 | |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 555 | int innerVertexNum = 4; |
| 556 | int outerVertexNum = this->miterStroke() ? 4 : 8; |
| 557 | int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2; |
| 558 | int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt; |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 559 | int instanceCount = fRects.count(); |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 560 | int maxQuads = this->miterStroke() ? kNumMiterRectsInIndexBuffer : kNumBevelRectsInIndexBuffer; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 561 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 562 | sk_sp<const GrGpuBuffer> indexBuffer = |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 563 | GetIndexBuffer(target->resourceProvider(), this->miterStroke()); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 564 | if (!indexBuffer) { |
| 565 | SkDebugf("Could not allocate indices\n"); |
| 566 | return; |
| 567 | } |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 568 | PatternHelper helper(target, GrPrimitiveType::kTriangles, |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 569 | fProgramInfo->geomProc().vertexStride(), std::move(indexBuffer), |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 570 | verticesPerInstance, indicesPerInstance, instanceCount, maxQuads); |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 571 | GrVertexWriter vertices{ helper.vertices() }; |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 572 | if (!vertices.fPtr) { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 573 | SkDebugf("Could not allocate vertices\n"); |
| 574 | return; |
| 575 | } |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 576 | |
| 577 | for (int i = 0; i < instanceCount; i++) { |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 578 | const RectInfo& info = fRects[i]; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 579 | this->generateAAStrokeRectGeometry(vertices, |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 580 | info.fColor, |
| 581 | fWideColor, |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 582 | info.fDevOutside, |
| 583 | info.fDevOutsideAssist, |
| 584 | info.fDevInside, |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 585 | fMiterStroke, |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 586 | info.fDegenerate, |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 587 | fHelper.compatibleWithCoverageAsAlpha(), |
| 588 | info.fDevHalfStrokeSize); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 589 | } |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 590 | fMesh = helper.mesh(); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | void AAStrokeRectOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 594 | if (!fProgramInfo || !fMesh) { |
| 595 | return; |
| 596 | } |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 597 | |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 598 | flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
Robert Phillips | 787fd9d | 2021-03-22 14:48:09 -0400 | [diff] [blame] | 599 | flushState->bindTextures(fProgramInfo->geomProc(), nullptr, fProgramInfo->pipeline()); |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 600 | flushState->drawMesh(*fMesh); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 603 | sk_sp<const GrGpuBuffer> AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider, |
| 604 | bool miterStroke) { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 605 | if (miterStroke) { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 606 | // clang-format off |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 607 | static const uint16_t gMiterIndices[] = { |
| 608 | 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0, |
| 609 | 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0, |
| 610 | 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0, |
| 611 | 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0, |
| 612 | |
| 613 | 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4, |
| 614 | 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4, |
| 615 | 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4, |
| 616 | 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4, |
| 617 | |
| 618 | 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8, |
| 619 | 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8, |
| 620 | 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8, |
| 621 | 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8, |
| 622 | }; |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 623 | // clang-format on |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 624 | static_assert(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 625 | GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey); |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 626 | return resourceProvider->findOrCreatePatternedIndexBuffer( |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 627 | gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt, |
| 628 | gMiterIndexBufferKey); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 629 | } else { |
| 630 | /** |
| 631 | * As in miter-stroke, index = a + b, and a is the current index, b is the shift |
| 632 | * from the first index. The index layout: |
| 633 | * outer AA line: 0~3, 4~7 |
| 634 | * outer edge: 8~11, 12~15 |
| 635 | * inner edge: 16~19 |
| 636 | * inner AA line: 20~23 |
| 637 | * Following comes a bevel-stroke rect and its indices: |
| 638 | * |
| 639 | * 4 7 |
| 640 | * ********************************* |
| 641 | * * ______________________________ * |
| 642 | * * / 12 15 \ * |
| 643 | * * / \ * |
| 644 | * 0 * |8 16_____________________19 11 | * 3 |
| 645 | * * | | | | * |
| 646 | * * | | **************** | | * |
| 647 | * * | | * 20 23 * | | * |
| 648 | * * | | * * | | * |
| 649 | * * | | * 21 22 * | | * |
| 650 | * * | | **************** | | * |
| 651 | * * | |____________________| | * |
| 652 | * 1 * |9 17 18 10| * 2 |
| 653 | * * \ / * |
| 654 | * * \13 __________________________14/ * |
| 655 | * * * |
| 656 | * ********************************** |
| 657 | * 5 6 |
| 658 | */ |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 659 | // clang-format off |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 660 | static const uint16_t gBevelIndices[] = { |
| 661 | // Draw outer AA, from outer AA line to outer edge, shift is 0. |
| 662 | 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0, |
| 663 | 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0, |
| 664 | 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0, |
| 665 | 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0, |
| 666 | 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0, |
| 667 | 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0, |
| 668 | 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0, |
| 669 | 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0, |
| 670 | |
| 671 | // Draw the stroke, from outer edge to inner edge, shift is 8. |
| 672 | 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8, |
| 673 | 1 + 8, 5 + 8, 9 + 8, |
| 674 | 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8, |
| 675 | 6 + 8, 2 + 8, 10 + 8, |
| 676 | 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8, |
| 677 | 3 + 8, 7 + 8, 11 + 8, |
| 678 | 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8, |
| 679 | 4 + 8, 0 + 8, 8 + 8, |
| 680 | |
| 681 | // Draw the inner AA, from inner edge to inner AA line, shift is 16. |
| 682 | 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16, |
| 683 | 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16, |
| 684 | 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16, |
| 685 | 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16, |
| 686 | }; |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 687 | // clang-format on |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 688 | static_assert(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 689 | |
| 690 | GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey); |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 691 | return resourceProvider->findOrCreatePatternedIndexBuffer( |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 692 | gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt, |
| 693 | gBevelIndexBufferKey); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | |
Herb Derby | e25c300 | 2020-10-27 15:57:27 -0400 | [diff] [blame] | 697 | GrOp::CombineResult AAStrokeRectOp::onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) |
| 698 | { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 699 | AAStrokeRectOp* that = t->cast<AAStrokeRectOp>(); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 700 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 701 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 702 | return CombineResult::kCannotCombine; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 705 | // TODO combine across miterstroke changes |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 706 | if (this->miterStroke() != that->miterStroke()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 707 | return CombineResult::kCannotCombine; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | // 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] | 711 | // 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] | 712 | if (fHelper.usesLocalCoords() && |
| 713 | !SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix())) |
| 714 | { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 715 | return CombineResult::kCannotCombine; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 716 | } |
| 717 | |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 718 | fRects.push_back_n(that->fRects.count(), that->fRects.begin()); |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 719 | fWideColor |= that->fWideColor; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 720 | return CombineResult::kMerged; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 723 | // Compute the coverage for the inner two rects. |
| 724 | static float compute_inner_coverage(SkScalar maxDevHalfStrokeSize) { |
| 725 | if (maxDevHalfStrokeSize < SK_ScalarHalf) { |
| 726 | return 2.0f * maxDevHalfStrokeSize / (maxDevHalfStrokeSize + SK_ScalarHalf); |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 727 | } |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 728 | |
| 729 | return 1.0f; |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 732 | void AAStrokeRectOp::generateAAStrokeRectGeometry(GrVertexWriter& vertices, |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 733 | const SkPMColor4f& color, |
| 734 | bool wideColor, |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 735 | const SkRect& devOutside, |
| 736 | const SkRect& devOutsideAssist, |
| 737 | const SkRect& devInside, |
| 738 | bool miterStroke, |
| 739 | bool degenerate, |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 740 | bool tweakAlphaForCoverage, |
| 741 | const SkVector& devHalfStrokeSize) const { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 742 | // We create vertices for four nested rectangles. There are two ramps from 0 to full |
| 743 | // 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] | 744 | |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 745 | // The following code only really works if either devStrokeSize's fX and fY are |
| 746 | // equal (in which case innerCoverage is same for all sides of the rects) or |
| 747 | // if devStrokeSize's fX and fY are both greater than 1.0 (in which case |
| 748 | // innerCoverage will always be 1). The most problematic case is when one of |
| 749 | // fX and fY is greater than 1.0 and the other is less than 1.0. In this case |
| 750 | // the smaller side should have a partial coverage but the larger side will |
| 751 | // force the coverage to be 1.0. |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 752 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 753 | auto inset_fan = [](const SkRect& r, SkScalar dx, SkScalar dy) { |
| 754 | return GrVertexWriter::TriFanFromRect(r.makeInset(dx, dy)); |
| 755 | }; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 756 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 757 | auto maybe_coverage = [tweakAlphaForCoverage](float coverage) { |
| 758 | return GrVertexWriter::If(!tweakAlphaForCoverage, coverage); |
| 759 | }; |
| 760 | |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 761 | GrVertexColor outerColor(tweakAlphaForCoverage ? SK_PMColor4fTRANSPARENT : color, wideColor); |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 762 | |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 763 | // For device-space stroke widths less than one we can't inset more than the original |
| 764 | // device space stroke width if we want to keep the sizing of all the rects correct. |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 765 | const SkScalar insetX = std::min(SK_ScalarHalf, devHalfStrokeSize.fX); |
| 766 | const SkScalar insetY = std::min(SK_ScalarHalf, devHalfStrokeSize.fY); |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 767 | |
| 768 | // But, correspondingly, we always want to keep the AA picture frame one pixel wide. |
| 769 | const SkScalar outsetX = SK_Scalar1 - insetX; |
| 770 | const SkScalar outsetY = SK_Scalar1 - insetY; |
| 771 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 772 | // Outermost rect |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 773 | vertices.writeQuad(inset_fan(devOutside, -outsetX, -outsetY), |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 774 | outerColor, |
| 775 | maybe_coverage(0.0f)); |
| 776 | |
| 777 | if (!miterStroke) { |
| 778 | // Second outermost |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 779 | vertices.writeQuad(inset_fan(devOutsideAssist, -outsetX, -outsetY), |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 780 | outerColor, |
| 781 | maybe_coverage(0.0f)); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 782 | } |
| 783 | |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 784 | float innerCoverage = compute_inner_coverage(std::max(devHalfStrokeSize.fX, |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 785 | devHalfStrokeSize.fY)); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 786 | |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 787 | SkPMColor4f scaledColor = color * innerCoverage; |
| 788 | GrVertexColor innerColor(tweakAlphaForCoverage ? scaledColor : color, wideColor); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 789 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 790 | // Inner rect |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 791 | vertices.writeQuad(inset_fan(devOutside, insetX, insetY), |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 792 | innerColor, |
| 793 | maybe_coverage(innerCoverage)); |
| 794 | |
| 795 | if (!miterStroke) { |
| 796 | // Second inner |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 797 | vertices.writeQuad(inset_fan(devOutsideAssist, insetX, insetY), |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 798 | innerColor, |
| 799 | maybe_coverage(innerCoverage)); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 800 | } |
| 801 | |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 802 | if (!degenerate) { |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 803 | vertices.writeQuad(inset_fan(devInside, -insetX, -insetY), |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 804 | innerColor, |
| 805 | maybe_coverage(innerCoverage)); |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 806 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 807 | // The innermost rect has 0 coverage... |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 808 | vertices.writeQuad(inset_fan(devInside, outsetX, outsetY), |
Brian Salomon | 747b340 | 2019-09-16 17:25:01 -0400 | [diff] [blame] | 809 | outerColor, |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 810 | maybe_coverage(0.0f)); |
| 811 | } else { |
| 812 | // When the interior rect has become degenerate we smoosh to a single point |
| 813 | SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom); |
| 814 | |
| 815 | vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside), |
| 816 | innerColor, |
| 817 | maybe_coverage(innerCoverage)); |
| 818 | |
| 819 | // ... unless we are degenerate, in which case we must apply the scaled coverage |
| 820 | vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside), |
| 821 | innerColor, |
| 822 | maybe_coverage(innerCoverage)); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 823 | } |
| 824 | } |
| 825 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 826 | } // anonymous namespace |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 827 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 828 | namespace GrStrokeRectOp { |
| 829 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 830 | GrOp::Owner Make(GrRecordingContext* context, |
| 831 | GrPaint&& paint, |
| 832 | GrAAType aaType, |
| 833 | const SkMatrix& viewMatrix, |
| 834 | const SkRect& rect, |
| 835 | const SkStrokeRec& stroke) { |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 836 | if (aaType == GrAAType::kCoverage) { |
| 837 | // The AA op only supports axis-aligned rectangles |
| 838 | if (!viewMatrix.rectStaysRect()) { |
| 839 | return nullptr; |
| 840 | } |
| 841 | return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke); |
| 842 | } else { |
| 843 | return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke, aaType); |
| 844 | } |
| 845 | } |
| 846 | |
Herb Derby | c76d409 | 2020-10-07 16:46:15 -0400 | [diff] [blame] | 847 | GrOp::Owner MakeNested(GrRecordingContext* context, |
| 848 | GrPaint&& paint, |
| 849 | const SkMatrix& viewMatrix, |
| 850 | const SkRect rects[2]) { |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 851 | SkASSERT(viewMatrix.rectStaysRect()); |
| 852 | SkASSERT(!rects[0].isEmpty() && !rects[1].isEmpty()); |
| 853 | |
| 854 | SkRect devOutside, devInside; |
| 855 | viewMatrix.mapRect(&devOutside, rects[0]); |
| 856 | viewMatrix.mapRect(&devInside, rects[1]); |
| 857 | if (devInside.isEmpty()) { |
| 858 | if (devOutside.isEmpty()) { |
| 859 | return nullptr; |
| 860 | } |
Michael Ludwig | 6b45c5d | 2020-02-07 09:56:38 -0500 | [diff] [blame] | 861 | DrawQuad quad{GrQuad::MakeFromRect(rects[0], viewMatrix), GrQuad(rects[0]), |
| 862 | GrQuadAAFlags::kAll}; |
| 863 | return GrFillRectOp::Make(context, std::move(paint), GrAAType::kCoverage, &quad); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 864 | } |
| 865 | |
Robert Phillips | d5caeb8 | 2020-01-08 16:27:59 -0500 | [diff] [blame] | 866 | SkVector devHalfStrokeSize{ SkScalarHalf(devOutside.fRight - devInside.fRight), |
| 867 | SkScalarHalf(devOutside.fBottom - devInside.fBottom) }; |
| 868 | return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, devOutside, |
| 869 | devInside, devHalfStrokeSize); |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 872 | } // namespace GrStrokeRectOp |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 873 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 874 | #if GR_TEST_UTILS |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 875 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 876 | #include "src/gpu/GrDrawOpTest.h" |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 877 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 878 | GR_DRAW_OP_TEST_DEFINE(NonAAStrokeRectOp) { |
| 879 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 880 | SkRect rect = GrTest::TestRect(random); |
| 881 | SkScalar strokeWidth = random->nextBool() ? 0.0f : 2.0f; |
| 882 | SkPaint strokePaint; |
| 883 | strokePaint.setStrokeWidth(strokeWidth); |
| 884 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 885 | strokePaint.setStrokeJoin(SkPaint::kMiter_Join); |
| 886 | SkStrokeRec strokeRec(strokePaint); |
| 887 | GrAAType aaType = GrAAType::kNone; |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 888 | if (numSamples > 1) { |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 889 | aaType = random->nextBool() ? GrAAType::kMSAA : GrAAType::kNone; |
| 890 | } |
| 891 | return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, strokeRec, aaType); |
| 892 | } |
| 893 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 894 | GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp) { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 895 | bool miterStroke = random->nextBool(); |
| 896 | |
bsalomon | 40ef485 | 2016-05-02 13:22:13 -0700 | [diff] [blame] | 897 | // Create either a empty rect or a non-empty rect. |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 898 | SkRect rect = |
| 899 | 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] | 900 | SkScalar minDim = std::min(rect.width(), rect.height()); |
bsalomon | 40ef485 | 2016-05-02 13:22:13 -0700 | [diff] [blame] | 901 | SkScalar strokeWidth = random->nextUScalar1() * minDim; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 902 | |
bsalomon | 40ef485 | 2016-05-02 13:22:13 -0700 | [diff] [blame] | 903 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
| 904 | rec.setStrokeStyle(strokeWidth); |
| 905 | rec.setStrokeParams(SkPaint::kButt_Cap, |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 906 | miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f); |
bsalomon | 40ef485 | 2016-05-02 13:22:13 -0700 | [diff] [blame] | 907 | SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 908 | return AAStrokeRectOp::Make(context, std::move(paint), matrix, rect, rec); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | #endif |