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" |
| 11 | #include "include/private/GrColor.h" |
| 12 | #include "include/private/GrResourceKey.h" |
| 13 | #include "include/utils/SkRandom.h" |
| 14 | #include "src/gpu/GrCaps.h" |
| 15 | #include "src/gpu/GrDefaultGeoProcFactory.h" |
| 16 | #include "src/gpu/GrDrawOpTest.h" |
| 17 | #include "src/gpu/GrOpFlushState.h" |
| 18 | #include "src/gpu/GrResourceProvider.h" |
| 19 | #include "src/gpu/GrVertexWriter.h" |
| 20 | #include "src/gpu/ops/GrFillRectOp.h" |
| 21 | #include "src/gpu/ops/GrMeshDrawOp.h" |
| 22 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 23 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 24 | namespace { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 25 | |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 26 | // 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] | 27 | // limit makes a miter join effectively beveled. If the miter is effectively beveled, it is only |
| 28 | // supported when using an AA stroke. |
| 29 | inline static bool allowed_stroke(const SkStrokeRec& stroke, GrAA aa, bool* isMiter) { |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 30 | SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style || |
| 31 | stroke.getStyle() == SkStrokeRec::kHairline_Style); |
| 32 | // For hairlines, make bevel and round joins appear the same as mitered ones. |
| 33 | if (!stroke.getWidth()) { |
| 34 | *isMiter = true; |
| 35 | return true; |
| 36 | } |
| 37 | if (stroke.getJoin() == SkPaint::kBevel_Join) { |
| 38 | *isMiter = false; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 39 | return aa == GrAA::kYes; // bevel only supported with AA |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 40 | } |
| 41 | if (stroke.getJoin() == SkPaint::kMiter_Join) { |
| 42 | *isMiter = stroke.getMiter() >= SK_ScalarSqrt2; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 43 | // Supported under non-AA only if it remains mitered |
| 44 | return aa == GrAA::kYes || *isMiter; |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 45 | } |
| 46 | return false; |
| 47 | } |
| 48 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 49 | |
| 50 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 51 | // Non-AA Stroking |
| 52 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 53 | |
| 54 | /* create a triangle strip that strokes the specified rect. There are 8 |
| 55 | unique vertices, but we repeat the last 2 to close up. Alternatively we |
| 56 | could use an indices array, and then only send 8 verts, but not sure that |
| 57 | would be faster. |
| 58 | */ |
| 59 | static void init_nonaa_stroke_rect_strip(SkPoint verts[10], const SkRect& rect, SkScalar width) { |
| 60 | const SkScalar rad = SkScalarHalf(width); |
| 61 | |
| 62 | verts[0].set(rect.fLeft + rad, rect.fTop + rad); |
| 63 | verts[1].set(rect.fLeft - rad, rect.fTop - rad); |
| 64 | verts[2].set(rect.fRight - rad, rect.fTop + rad); |
| 65 | verts[3].set(rect.fRight + rad, rect.fTop - rad); |
| 66 | verts[4].set(rect.fRight - rad, rect.fBottom - rad); |
| 67 | verts[5].set(rect.fRight + rad, rect.fBottom + rad); |
| 68 | verts[6].set(rect.fLeft + rad, rect.fBottom - rad); |
| 69 | verts[7].set(rect.fLeft - rad, rect.fBottom + rad); |
| 70 | verts[8] = verts[0]; |
| 71 | verts[9] = verts[1]; |
| 72 | |
| 73 | // TODO: we should be catching this higher up the call stack and just draw a single |
| 74 | // non-AA rect |
| 75 | if (2*rad >= rect.width()) { |
| 76 | verts[0].fX = verts[2].fX = verts[4].fX = verts[6].fX = verts[8].fX = rect.centerX(); |
| 77 | } |
| 78 | if (2*rad >= rect.height()) { |
| 79 | verts[0].fY = verts[2].fY = verts[4].fY = verts[6].fY = verts[8].fY = rect.centerY(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | class NonAAStrokeRectOp final : public GrMeshDrawOp { |
| 84 | private: |
| 85 | using Helper = GrSimpleMeshDrawOpHelper; |
| 86 | |
| 87 | public: |
| 88 | DEFINE_OP_CLASS_ID |
| 89 | |
| 90 | const char* name() const override { return "NonAAStrokeRectOp"; } |
| 91 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame] | 92 | void visitProxies(const VisitProxyFunc& func) const override { |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 93 | fHelper.visitProxies(func); |
| 94 | } |
| 95 | |
| 96 | #ifdef SK_DEBUG |
| 97 | SkString dumpInfo() const override { |
| 98 | SkString string; |
| 99 | string.appendf( |
| 100 | "Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], " |
| 101 | "StrokeWidth: %.2f\n", |
| 102 | fColor.toBytes_RGBA(), fRect.fLeft, fRect.fTop, fRect.fRight, fRect.fBottom, |
| 103 | fStrokeWidth); |
| 104 | string += fHelper.dumpInfo(); |
| 105 | string += INHERITED::dumpInfo(); |
| 106 | return string; |
| 107 | } |
| 108 | #endif |
| 109 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 110 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 111 | GrPaint&& paint, |
| 112 | const SkMatrix& viewMatrix, |
| 113 | const SkRect& rect, |
| 114 | const SkStrokeRec& stroke, |
| 115 | GrAAType aaType) { |
| 116 | bool isMiter; |
| 117 | if (!allowed_stroke(stroke, GrAA::kNo, &isMiter)) { |
| 118 | return nullptr; |
| 119 | } |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 120 | Helper::InputFlags inputFlags = Helper::InputFlags::kNone; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 121 | // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of |
| 122 | // hairline rects. We jam all the vertices to pixel centers to avoid this, but not |
| 123 | // when MSAA is enabled because it can cause ugly artifacts. |
| 124 | if (stroke.getStyle() == SkStrokeRec::kHairline_Style && aaType != GrAAType::kMSAA) { |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 125 | inputFlags |= Helper::InputFlags::kSnapVerticesToPixelCenters; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 126 | } |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 127 | return Helper::FactoryHelper<NonAAStrokeRectOp>(context, std::move(paint), inputFlags, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 128 | viewMatrix, rect, |
| 129 | stroke, aaType); |
| 130 | } |
| 131 | |
| 132 | NonAAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 133 | Helper::InputFlags inputFlags, const SkMatrix& viewMatrix, const SkRect& rect, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 134 | const SkStrokeRec& stroke, GrAAType aaType) |
Chris Dalton | baa1b35 | 2019-04-03 12:03:00 -0600 | [diff] [blame] | 135 | : INHERITED(ClassID()), fHelper(helperArgs, aaType, inputFlags) { |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 136 | fColor = color; |
| 137 | fViewMatrix = viewMatrix; |
| 138 | fRect = rect; |
| 139 | // Sort the rect for hairlines |
| 140 | fRect.sort(); |
| 141 | fStrokeWidth = stroke.getWidth(); |
| 142 | |
| 143 | SkScalar rad = SkScalarHalf(fStrokeWidth); |
| 144 | SkRect bounds = rect; |
| 145 | bounds.outset(rad, rad); |
| 146 | |
| 147 | // 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] | 148 | if (inputFlags & Helper::InputFlags::kSnapVerticesToPixelCenters) { |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 149 | viewMatrix.mapRect(&bounds); |
| 150 | // We want to be consistent with how we snap non-aa lines. To match what we do in |
| 151 | // GrGLSLVertexShaderBuilder, we first floor all the vertex values and then add half a |
| 152 | // pixel to force us to pixel centers. |
| 153 | bounds.set(SkScalarFloorToScalar(bounds.fLeft), |
| 154 | SkScalarFloorToScalar(bounds.fTop), |
| 155 | SkScalarFloorToScalar(bounds.fRight), |
| 156 | SkScalarFloorToScalar(bounds.fBottom)); |
| 157 | bounds.offset(0.5f, 0.5f); |
| 158 | this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo); |
| 159 | } else { |
| 160 | this->setTransformedBounds(bounds, fViewMatrix, HasAABloat::kNo, IsZeroArea::kNo); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 165 | |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 166 | GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 167 | GrFSAAType fsaaType, GrClampType clampType) override { |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 168 | // This Op uses uniform (not vertex) color, so doesn't need to track wide color. |
| 169 | return fHelper.finalizeProcessors(caps, clip, fsaaType, clampType, |
| 170 | GrProcessorAnalysisCoverage::kNone, &fColor, nullptr); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | private: |
| 174 | void onPrepareDraws(Target* target) override { |
| 175 | sk_sp<GrGeometryProcessor> gp; |
| 176 | { |
| 177 | using namespace GrDefaultGeoProcFactory; |
| 178 | Color color(fColor); |
| 179 | LocalCoords::Type localCoordsType = fHelper.usesLocalCoords() |
| 180 | ? LocalCoords::kUsePosition_Type |
| 181 | : LocalCoords::kUnused_Type; |
| 182 | gp = GrDefaultGeoProcFactory::Make(target->caps().shaderCaps(), color, |
| 183 | Coverage::kSolid_Type, localCoordsType, |
| 184 | fViewMatrix); |
| 185 | } |
| 186 | |
| 187 | size_t kVertexStride = gp->vertexStride(); |
| 188 | int vertexCount = kVertsPerHairlineRect; |
| 189 | if (fStrokeWidth > 0) { |
| 190 | vertexCount = kVertsPerStrokeRect; |
| 191 | } |
| 192 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 193 | sk_sp<const GrBuffer> vertexBuffer; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 194 | int firstVertex; |
| 195 | |
| 196 | void* verts = |
| 197 | target->makeVertexSpace(kVertexStride, vertexCount, &vertexBuffer, &firstVertex); |
| 198 | |
| 199 | if (!verts) { |
| 200 | SkDebugf("Could not allocate vertices\n"); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | SkPoint* vertex = reinterpret_cast<SkPoint*>(verts); |
| 205 | |
| 206 | GrPrimitiveType primType; |
| 207 | if (fStrokeWidth > 0) { |
| 208 | primType = GrPrimitiveType::kTriangleStrip; |
| 209 | init_nonaa_stroke_rect_strip(vertex, fRect, fStrokeWidth); |
| 210 | } else { |
| 211 | // hairline |
| 212 | primType = GrPrimitiveType::kLineStrip; |
| 213 | vertex[0].set(fRect.fLeft, fRect.fTop); |
| 214 | vertex[1].set(fRect.fRight, fRect.fTop); |
| 215 | vertex[2].set(fRect.fRight, fRect.fBottom); |
| 216 | vertex[3].set(fRect.fLeft, fRect.fBottom); |
| 217 | vertex[4].set(fRect.fLeft, fRect.fTop); |
| 218 | } |
| 219 | |
| 220 | GrMesh* mesh = target->allocMesh(primType); |
| 221 | mesh->setNonIndexedNonInstanced(vertexCount); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 222 | mesh->setVertexData(std::move(vertexBuffer), firstVertex); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 223 | target->recordDraw(std::move(gp), mesh); |
| 224 | } |
| 225 | |
| 226 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
| 227 | fHelper.executeDrawsAndUploads(this, flushState, chainBounds); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // TODO: override onCombineIfPossible |
| 231 | |
| 232 | Helper fHelper; |
| 233 | SkPMColor4f fColor; |
| 234 | SkMatrix fViewMatrix; |
| 235 | SkRect fRect; |
| 236 | SkScalar fStrokeWidth; |
| 237 | |
| 238 | const static int kVertsPerHairlineRect = 5; |
| 239 | const static int kVertsPerStrokeRect = 10; |
| 240 | |
| 241 | typedef GrMeshDrawOp INHERITED; |
| 242 | }; |
| 243 | |
| 244 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 245 | // AA Stroking |
| 246 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 247 | |
| 248 | GR_DECLARE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey); |
| 249 | GR_DECLARE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey); |
| 250 | |
| 251 | static void compute_aa_rects(SkRect* devOutside, SkRect* devOutsideAssist, SkRect* devInside, |
| 252 | bool* isDegenerate, const SkMatrix& viewMatrix, const SkRect& rect, |
| 253 | SkScalar strokeWidth, bool miterStroke) { |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 254 | SkRect devRect; |
| 255 | viewMatrix.mapRect(&devRect, rect); |
| 256 | |
| 257 | SkVector devStrokeSize; |
| 258 | if (strokeWidth > 0) { |
| 259 | devStrokeSize.set(strokeWidth, strokeWidth); |
| 260 | viewMatrix.mapVectors(&devStrokeSize, 1); |
| 261 | devStrokeSize.setAbs(devStrokeSize); |
| 262 | } else { |
| 263 | devStrokeSize.set(SK_Scalar1, SK_Scalar1); |
| 264 | } |
| 265 | |
| 266 | const SkScalar dx = devStrokeSize.fX; |
| 267 | const SkScalar dy = devStrokeSize.fY; |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 268 | const SkScalar rx = SkScalarHalf(dx); |
| 269 | const SkScalar ry = SkScalarHalf(dy); |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 270 | |
| 271 | *devOutside = devRect; |
| 272 | *devOutsideAssist = devRect; |
| 273 | *devInside = devRect; |
| 274 | |
| 275 | devOutside->outset(rx, ry); |
| 276 | devInside->inset(rx, ry); |
| 277 | |
| 278 | // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we |
| 279 | // make a degenerate inside rect to avoid double hitting. We will also jam all of the points |
| 280 | // together when we render these rects. |
| 281 | SkScalar spare; |
| 282 | { |
| 283 | SkScalar w = devRect.width() - dx; |
| 284 | SkScalar h = devRect.height() - dy; |
| 285 | spare = SkTMin(w, h); |
| 286 | } |
| 287 | |
| 288 | *isDegenerate = spare <= 0; |
| 289 | if (*isDegenerate) { |
| 290 | devInside->fLeft = devInside->fRight = devRect.centerX(); |
| 291 | devInside->fTop = devInside->fBottom = devRect.centerY(); |
| 292 | } |
| 293 | |
| 294 | // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist) |
| 295 | // to draw the outside of the octagon. Because there are 8 vertices on the outer |
| 296 | // edge, while vertex number of inner edge is 4, the same as miter-stroke. |
| 297 | if (!miterStroke) { |
| 298 | devOutside->inset(0, ry); |
| 299 | devOutsideAssist->outset(0, ry); |
| 300 | } |
| 301 | } |
| 302 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 303 | static sk_sp<GrGeometryProcessor> create_aa_stroke_rect_gp(const GrShaderCaps* shaderCaps, |
| 304 | bool tweakAlphaForCoverage, |
| 305 | const SkMatrix& viewMatrix, |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 306 | bool usesLocalCoords, |
| 307 | bool wideColor) { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 308 | using namespace GrDefaultGeoProcFactory; |
| 309 | |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 310 | Coverage::Type coverageType = |
| 311 | tweakAlphaForCoverage ? Coverage::kSolid_Type : Coverage::kAttribute_Type; |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 312 | LocalCoords::Type localCoordsType = |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 313 | usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type; |
| 314 | Color::Type colorType = |
| 315 | wideColor ? Color::kPremulWideColorAttribute_Type: Color::kPremulGrColorAttribute_Type; |
| 316 | |
| 317 | return MakeForDeviceSpace(shaderCaps, colorType, coverageType, localCoordsType, viewMatrix); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 320 | class AAStrokeRectOp final : public GrMeshDrawOp { |
| 321 | private: |
| 322 | using Helper = GrSimpleMeshDrawOpHelper; |
| 323 | |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 324 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 325 | DEFINE_OP_CLASS_ID |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 326 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 327 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 328 | GrPaint&& paint, |
| 329 | const SkMatrix& viewMatrix, |
| 330 | const SkRect& devOutside, |
| 331 | const SkRect& devInside) { |
| 332 | return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, |
| 333 | devOutside, devInside); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 334 | } |
| 335 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 336 | AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, |
Brian Osman | 936fe7d | 2018-10-30 15:30:35 -0400 | [diff] [blame] | 337 | const SkMatrix& viewMatrix, const SkRect& devOutside, const SkRect& devInside) |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 338 | : INHERITED(ClassID()) |
| 339 | , fHelper(helperArgs, GrAAType::kCoverage) |
| 340 | , fViewMatrix(viewMatrix) { |
caryclark | d656200 | 2016-07-27 12:02:07 -0700 | [diff] [blame] | 341 | SkASSERT(!devOutside.isEmpty()); |
| 342 | SkASSERT(!devInside.isEmpty()); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 343 | |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 344 | fRects.emplace_back(RectInfo{color, devOutside, devOutside, devInside, false}); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 345 | this->setBounds(devOutside, HasAABloat::kYes, IsZeroArea::kNo); |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 346 | fMiterStroke = true; |
| 347 | } |
| 348 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 349 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 350 | GrPaint&& paint, |
| 351 | const SkMatrix& viewMatrix, |
| 352 | const SkRect& rect, |
| 353 | const SkStrokeRec& stroke) { |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 354 | bool isMiter; |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 355 | if (!allowed_stroke(stroke, GrAA::kYes, &isMiter)) { |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 356 | return nullptr; |
| 357 | } |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 358 | return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, rect, |
| 359 | stroke, isMiter); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 360 | } |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 361 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 362 | AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, |
Brian Osman | 936fe7d | 2018-10-30 15:30:35 -0400 | [diff] [blame] | 363 | const SkMatrix& viewMatrix, const SkRect& rect, const SkStrokeRec& stroke, |
| 364 | bool isMiter) |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 365 | : INHERITED(ClassID()) |
| 366 | , fHelper(helperArgs, GrAAType::kCoverage) |
| 367 | , fViewMatrix(viewMatrix) { |
| 368 | fMiterStroke = isMiter; |
| 369 | RectInfo& info = fRects.push_back(); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 370 | compute_aa_rects(&info.fDevOutside, &info.fDevOutsideAssist, &info.fDevInside, |
| 371 | &info.fDegenerate, viewMatrix, rect, stroke.getWidth(), isMiter); |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 372 | info.fColor = color; |
Brian Salomon | 510dd42 | 2017-03-16 12:15:22 -0400 | [diff] [blame] | 373 | if (isMiter) { |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 374 | this->setBounds(info.fDevOutside, HasAABloat::kYes, IsZeroArea::kNo); |
Brian Salomon | 510dd42 | 2017-03-16 12:15:22 -0400 | [diff] [blame] | 375 | } else { |
| 376 | // The outer polygon of the bevel stroke is an octagon specified by the points of a |
| 377 | // pair of overlapping rectangles where one is wide and the other is narrow. |
| 378 | SkRect bounds = info.fDevOutside; |
| 379 | bounds.joinPossiblyEmptyRect(info.fDevOutsideAssist); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 380 | this->setBounds(bounds, HasAABloat::kYes, IsZeroArea::kNo); |
Brian Salomon | 510dd42 | 2017-03-16 12:15:22 -0400 | [diff] [blame] | 381 | } |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | const char* name() const override { return "AAStrokeRect"; } |
| 385 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame] | 386 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 387 | fHelper.visitProxies(func); |
| 388 | } |
| 389 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 390 | #ifdef SK_DEBUG |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 391 | SkString dumpInfo() const override { |
| 392 | SkString string; |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 393 | for (const auto& info : fRects) { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 394 | string.appendf( |
| 395 | "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], " |
| 396 | "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], " |
| 397 | "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d", |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 398 | info.fColor.toBytes_RGBA(), info.fDevOutside.fLeft, info.fDevOutside.fTop, |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 399 | info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft, |
| 400 | info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight, |
| 401 | info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop, |
| 402 | info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 403 | } |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 404 | string += fHelper.dumpInfo(); |
| 405 | string += INHERITED::dumpInfo(); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 406 | return string; |
| 407 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 408 | #endif |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 409 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 410 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
Brian Salomon | a0485d9 | 2017-06-14 19:08:01 -0400 | [diff] [blame] | 411 | |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 412 | GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip, |
| 413 | GrFSAAType fsaaType, GrClampType clampType) override { |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 414 | return fHelper.finalizeProcessors( |
Brian Osman | 5ced0bf | 2019-03-15 10:15:29 -0400 | [diff] [blame] | 415 | caps, clip, fsaaType, clampType, GrProcessorAnalysisCoverage::kSingleChannel, |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 416 | &fRects.back().fColor, &fWideColor); |
Brian Salomon | a0485d9 | 2017-06-14 19:08:01 -0400 | [diff] [blame] | 417 | } |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 418 | |
| 419 | private: |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 420 | void onPrepareDraws(Target*) override; |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 421 | void onExecute(GrOpFlushState*, const SkRect& chainBounds) override; |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 422 | |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 423 | static const int kMiterIndexCnt = 3 * 24; |
| 424 | static const int kMiterVertexCnt = 16; |
| 425 | static const int kNumMiterRectsInIndexBuffer = 256; |
| 426 | |
| 427 | static const int kBevelIndexCnt = 48 + 36 + 24; |
| 428 | static const int kBevelVertexCnt = 24; |
| 429 | static const int kNumBevelRectsInIndexBuffer = 256; |
| 430 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 431 | static sk_sp<const GrGpuBuffer> GetIndexBuffer(GrResourceProvider*, bool miterStroke); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 432 | |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 433 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
| 434 | bool miterStroke() const { return fMiterStroke; } |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 435 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 436 | CombineResult onCombineIfPossible(GrOp* t, const GrCaps&) override; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 437 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 438 | void generateAAStrokeRectGeometry(GrVertexWriter& vertices, |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 439 | const SkPMColor4f& color, |
| 440 | bool wideColor, |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 441 | const SkRect& devOutside, |
| 442 | const SkRect& devOutsideAssist, |
| 443 | const SkRect& devInside, |
| 444 | bool miterStroke, |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 445 | bool degenerate, |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 446 | bool tweakAlphaForCoverage) const; |
| 447 | |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 448 | // TODO support AA rotated stroke rects by copying around view matrices |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 449 | struct RectInfo { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 450 | SkPMColor4f fColor; |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 451 | SkRect fDevOutside; |
| 452 | SkRect fDevOutsideAssist; |
| 453 | SkRect fDevInside; |
| 454 | bool fDegenerate; |
| 455 | }; |
| 456 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 457 | Helper fHelper; |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 458 | SkSTArray<1, RectInfo, true> fRects; |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 459 | SkMatrix fViewMatrix; |
| 460 | bool fMiterStroke; |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 461 | bool fWideColor; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 462 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 463 | typedef GrMeshDrawOp INHERITED; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 464 | }; |
| 465 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 466 | void AAStrokeRectOp::onPrepareDraws(Target* target) { |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 467 | sk_sp<GrGeometryProcessor> gp(create_aa_stroke_rect_gp(target->caps().shaderCaps(), |
Brian Osman | 605c6d5 | 2019-03-15 12:10:35 -0400 | [diff] [blame] | 468 | fHelper.compatibleWithCoverageAsAlpha(), |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 469 | this->viewMatrix(), |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 470 | fHelper.usesLocalCoords(), |
| 471 | fWideColor)); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 472 | if (!gp) { |
| 473 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 474 | return; |
| 475 | } |
| 476 | |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 477 | int innerVertexNum = 4; |
| 478 | int outerVertexNum = this->miterStroke() ? 4 : 8; |
| 479 | int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2; |
| 480 | int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt; |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 481 | int instanceCount = fRects.count(); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 482 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 483 | sk_sp<const GrGpuBuffer> indexBuffer = |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 484 | GetIndexBuffer(target->resourceProvider(), this->miterStroke()); |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 485 | if (!indexBuffer) { |
| 486 | SkDebugf("Could not allocate indices\n"); |
| 487 | return; |
| 488 | } |
| 489 | PatternHelper helper(target, GrPrimitiveType::kTriangles, gp->vertexStride(), |
| 490 | std::move(indexBuffer), verticesPerInstance, indicesPerInstance, |
| 491 | instanceCount); |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 492 | GrVertexWriter vertices{ helper.vertices() }; |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 493 | if (!vertices.fPtr) { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 494 | SkDebugf("Could not allocate vertices\n"); |
| 495 | return; |
| 496 | } |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 497 | |
| 498 | for (int i = 0; i < instanceCount; i++) { |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 499 | const RectInfo& info = fRects[i]; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 500 | this->generateAAStrokeRectGeometry(vertices, |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 501 | info.fColor, |
| 502 | fWideColor, |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 503 | info.fDevOutside, |
| 504 | info.fDevOutsideAssist, |
| 505 | info.fDevInside, |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 506 | fMiterStroke, |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 507 | info.fDegenerate, |
Brian Osman | 605c6d5 | 2019-03-15 12:10:35 -0400 | [diff] [blame] | 508 | fHelper.compatibleWithCoverageAsAlpha()); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 509 | } |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 510 | helper.recordDraw(target, std::move(gp)); |
| 511 | } |
| 512 | |
| 513 | void AAStrokeRectOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
| 514 | fHelper.executeDrawsAndUploads(this, flushState, chainBounds); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 515 | } |
| 516 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 517 | sk_sp<const GrGpuBuffer> AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider, |
| 518 | bool miterStroke) { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 519 | if (miterStroke) { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 520 | // clang-format off |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 521 | static const uint16_t gMiterIndices[] = { |
| 522 | 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0, |
| 523 | 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0, |
| 524 | 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0, |
| 525 | 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0, |
| 526 | |
| 527 | 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4, |
| 528 | 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4, |
| 529 | 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4, |
| 530 | 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4, |
| 531 | |
| 532 | 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8, |
| 533 | 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8, |
| 534 | 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8, |
| 535 | 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8, |
| 536 | }; |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 537 | // clang-format on |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 538 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt); |
| 539 | GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey); |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 540 | return resourceProvider->findOrCreatePatternedIndexBuffer( |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 541 | gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt, |
| 542 | gMiterIndexBufferKey); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 543 | } else { |
| 544 | /** |
| 545 | * As in miter-stroke, index = a + b, and a is the current index, b is the shift |
| 546 | * from the first index. The index layout: |
| 547 | * outer AA line: 0~3, 4~7 |
| 548 | * outer edge: 8~11, 12~15 |
| 549 | * inner edge: 16~19 |
| 550 | * inner AA line: 20~23 |
| 551 | * Following comes a bevel-stroke rect and its indices: |
| 552 | * |
| 553 | * 4 7 |
| 554 | * ********************************* |
| 555 | * * ______________________________ * |
| 556 | * * / 12 15 \ * |
| 557 | * * / \ * |
| 558 | * 0 * |8 16_____________________19 11 | * 3 |
| 559 | * * | | | | * |
| 560 | * * | | **************** | | * |
| 561 | * * | | * 20 23 * | | * |
| 562 | * * | | * * | | * |
| 563 | * * | | * 21 22 * | | * |
| 564 | * * | | **************** | | * |
| 565 | * * | |____________________| | * |
| 566 | * 1 * |9 17 18 10| * 2 |
| 567 | * * \ / * |
| 568 | * * \13 __________________________14/ * |
| 569 | * * * |
| 570 | * ********************************** |
| 571 | * 5 6 |
| 572 | */ |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 573 | // clang-format off |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 574 | static const uint16_t gBevelIndices[] = { |
| 575 | // Draw outer AA, from outer AA line to outer edge, shift is 0. |
| 576 | 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0, |
| 577 | 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0, |
| 578 | 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0, |
| 579 | 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0, |
| 580 | 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0, |
| 581 | 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0, |
| 582 | 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0, |
| 583 | 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0, |
| 584 | |
| 585 | // Draw the stroke, from outer edge to inner edge, shift is 8. |
| 586 | 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8, |
| 587 | 1 + 8, 5 + 8, 9 + 8, |
| 588 | 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8, |
| 589 | 6 + 8, 2 + 8, 10 + 8, |
| 590 | 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8, |
| 591 | 3 + 8, 7 + 8, 11 + 8, |
| 592 | 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8, |
| 593 | 4 + 8, 0 + 8, 8 + 8, |
| 594 | |
| 595 | // Draw the inner AA, from inner edge to inner AA line, shift is 16. |
| 596 | 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16, |
| 597 | 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16, |
| 598 | 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16, |
| 599 | 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16, |
| 600 | }; |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 601 | // clang-format on |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 602 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt); |
| 603 | |
| 604 | GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey); |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 605 | return resourceProvider->findOrCreatePatternedIndexBuffer( |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 606 | gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt, |
| 607 | gBevelIndexBufferKey); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 611 | GrOp::CombineResult AAStrokeRectOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 612 | AAStrokeRectOp* that = t->cast<AAStrokeRectOp>(); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 613 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 614 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 615 | return CombineResult::kCannotCombine; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 616 | } |
| 617 | |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 618 | // TODO combine across miterstroke changes |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 619 | if (this->miterStroke() != that->miterStroke()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 620 | return CombineResult::kCannotCombine; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | // 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] | 624 | // local coords then we won't be able to combine. TODO: Upload local coords as an attribute. |
| 625 | if (fHelper.usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 626 | return CombineResult::kCannotCombine; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 627 | } |
| 628 | |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 629 | fRects.push_back_n(that->fRects.count(), that->fRects.begin()); |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 630 | fWideColor |= that->fWideColor; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 631 | return CombineResult::kMerged; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 632 | } |
| 633 | |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 634 | static void setup_scale(int* scale, SkScalar inset) { |
| 635 | if (inset < SK_ScalarHalf) { |
| 636 | *scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf)); |
| 637 | SkASSERT(*scale >= 0 && *scale <= 255); |
| 638 | } else { |
| 639 | *scale = 0xff; |
| 640 | } |
| 641 | } |
| 642 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 643 | void AAStrokeRectOp::generateAAStrokeRectGeometry(GrVertexWriter& vertices, |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 644 | const SkPMColor4f& color, |
| 645 | bool wideColor, |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 646 | const SkRect& devOutside, |
| 647 | const SkRect& devOutsideAssist, |
| 648 | const SkRect& devInside, |
| 649 | bool miterStroke, |
| 650 | bool degenerate, |
| 651 | bool tweakAlphaForCoverage) const { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 652 | // We create vertices for four nested rectangles. There are two ramps from 0 to full |
| 653 | // 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] | 654 | |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 655 | // TODO: this only really works if the X & Y margins are the same all around |
| 656 | // the rect (or if they are all >= 1.0). |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 657 | SkScalar inset; |
| 658 | if (!degenerate) { |
| 659 | inset = SkMinScalar(SK_Scalar1, devOutside.fRight - devInside.fRight); |
| 660 | inset = SkMinScalar(inset, devInside.fLeft - devOutside.fLeft); |
| 661 | inset = SkMinScalar(inset, devInside.fTop - devOutside.fTop); |
| 662 | if (miterStroke) { |
| 663 | inset = SK_ScalarHalf * SkMinScalar(inset, devOutside.fBottom - devInside.fBottom); |
| 664 | } else { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 665 | inset = SK_ScalarHalf * |
| 666 | SkMinScalar(inset, devOutsideAssist.fBottom - devInside.fBottom); |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 667 | } |
| 668 | SkASSERT(inset >= 0); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 669 | } else { |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 670 | // TODO use real devRect here |
| 671 | inset = SkMinScalar(devOutside.width(), SK_Scalar1); |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 672 | inset = SK_ScalarHalf * |
| 673 | SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height())); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 674 | } |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 675 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 676 | auto inset_fan = [](const SkRect& r, SkScalar dx, SkScalar dy) { |
| 677 | return GrVertexWriter::TriFanFromRect(r.makeInset(dx, dy)); |
| 678 | }; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 679 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 680 | auto maybe_coverage = [tweakAlphaForCoverage](float coverage) { |
| 681 | return GrVertexWriter::If(!tweakAlphaForCoverage, coverage); |
| 682 | }; |
| 683 | |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 684 | GrVertexColor outerColor(tweakAlphaForCoverage ? SK_PMColor4fTRANSPARENT : color, wideColor); |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 685 | |
| 686 | // Outermost rect |
| 687 | vertices.writeQuad(inset_fan(devOutside, -SK_ScalarHalf, -SK_ScalarHalf), |
| 688 | outerColor, |
| 689 | maybe_coverage(0.0f)); |
| 690 | |
| 691 | if (!miterStroke) { |
| 692 | // Second outermost |
| 693 | vertices.writeQuad(inset_fan(devOutsideAssist, -SK_ScalarHalf, -SK_ScalarHalf), |
| 694 | outerColor, |
| 695 | maybe_coverage(0.0f)); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | // scale is the coverage for the the inner two rects. |
| 699 | int scale; |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 700 | setup_scale(&scale, inset); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 701 | |
| 702 | float innerCoverage = GrNormalizeByteToFloat(scale); |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 703 | SkPMColor4f scaledColor = color * innerCoverage; |
| 704 | GrVertexColor innerColor(tweakAlphaForCoverage ? scaledColor : color, wideColor); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 705 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 706 | // Inner rect |
| 707 | vertices.writeQuad(inset_fan(devOutside, inset, inset), |
| 708 | innerColor, |
| 709 | maybe_coverage(innerCoverage)); |
| 710 | |
| 711 | if (!miterStroke) { |
| 712 | // Second inner |
| 713 | vertices.writeQuad(inset_fan(devOutsideAssist, inset, inset), |
| 714 | innerColor, |
| 715 | maybe_coverage(innerCoverage)); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 716 | } |
| 717 | |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 718 | if (!degenerate) { |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 719 | vertices.writeQuad(inset_fan(devInside, -inset, -inset), |
| 720 | innerColor, |
| 721 | maybe_coverage(innerCoverage)); |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 722 | |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 723 | // The innermost rect has 0 coverage... |
| 724 | vertices.writeQuad(inset_fan(devInside, SK_ScalarHalf, SK_ScalarHalf), |
Brian Osman | 2a4c4df | 2018-12-20 14:06:54 -0500 | [diff] [blame] | 725 | GrVertexColor(SK_PMColor4fTRANSPARENT, wideColor), |
Brian Osman | cfec9d5 | 2018-11-20 11:39:15 -0500 | [diff] [blame] | 726 | maybe_coverage(0.0f)); |
| 727 | } else { |
| 728 | // When the interior rect has become degenerate we smoosh to a single point |
| 729 | SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom); |
| 730 | |
| 731 | vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside), |
| 732 | innerColor, |
| 733 | maybe_coverage(innerCoverage)); |
| 734 | |
| 735 | // ... unless we are degenerate, in which case we must apply the scaled coverage |
| 736 | vertices.writeQuad(GrVertexWriter::TriFanFromRect(devInside), |
| 737 | innerColor, |
| 738 | maybe_coverage(innerCoverage)); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 739 | } |
| 740 | } |
| 741 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 742 | } // anonymous namespace |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 743 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 744 | namespace GrStrokeRectOp { |
| 745 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 746 | std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 747 | GrPaint&& paint, |
| 748 | GrAAType aaType, |
| 749 | const SkMatrix& viewMatrix, |
| 750 | const SkRect& rect, |
| 751 | const SkStrokeRec& stroke) { |
| 752 | if (aaType == GrAAType::kCoverage) { |
| 753 | // The AA op only supports axis-aligned rectangles |
| 754 | if (!viewMatrix.rectStaysRect()) { |
| 755 | return nullptr; |
| 756 | } |
| 757 | return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke); |
| 758 | } else { |
| 759 | return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke, aaType); |
| 760 | } |
| 761 | } |
| 762 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 763 | std::unique_ptr<GrDrawOp> MakeNested(GrRecordingContext* context, |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 764 | GrPaint&& paint, |
| 765 | const SkMatrix& viewMatrix, |
| 766 | const SkRect rects[2]) { |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 767 | SkASSERT(viewMatrix.rectStaysRect()); |
| 768 | SkASSERT(!rects[0].isEmpty() && !rects[1].isEmpty()); |
| 769 | |
| 770 | SkRect devOutside, devInside; |
| 771 | viewMatrix.mapRect(&devOutside, rects[0]); |
| 772 | viewMatrix.mapRect(&devInside, rects[1]); |
| 773 | if (devInside.isEmpty()) { |
| 774 | if (devOutside.isEmpty()) { |
| 775 | return nullptr; |
| 776 | } |
Michael Ludwig | aa1b6b3 | 2019-05-29 14:43:13 -0400 | [diff] [blame] | 777 | return GrFillRectOp::MakeGeneric(context, std::move(paint), GrAAType::kCoverage, |
| 778 | GrQuadAAFlags::kAll, |
| 779 | GrPerspQuad::MakeFromRect(rects[0], viewMatrix), |
| 780 | GrPerspQuad(rects[0])); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 781 | } |
| 782 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 783 | return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, devOutside, devInside); |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 784 | } |
| 785 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 786 | } // namespace GrStrokeRectOp |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 787 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 788 | #if GR_TEST_UTILS |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 789 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 790 | #include "src/gpu/GrDrawOpTest.h" |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 791 | |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 792 | GR_DRAW_OP_TEST_DEFINE(NonAAStrokeRectOp) { |
| 793 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 794 | SkRect rect = GrTest::TestRect(random); |
| 795 | SkScalar strokeWidth = random->nextBool() ? 0.0f : 2.0f; |
| 796 | SkPaint strokePaint; |
| 797 | strokePaint.setStrokeWidth(strokeWidth); |
| 798 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 799 | strokePaint.setStrokeJoin(SkPaint::kMiter_Join); |
| 800 | SkStrokeRec strokeRec(strokePaint); |
| 801 | GrAAType aaType = GrAAType::kNone; |
| 802 | if (fsaaType == GrFSAAType::kUnifiedMSAA) { |
| 803 | aaType = random->nextBool() ? GrAAType::kMSAA : GrAAType::kNone; |
| 804 | } |
| 805 | return NonAAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, strokeRec, aaType); |
| 806 | } |
| 807 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 808 | GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp) { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 809 | bool miterStroke = random->nextBool(); |
| 810 | |
bsalomon | 40ef485 | 2016-05-02 13:22:13 -0700 | [diff] [blame] | 811 | // Create either a empty rect or a non-empty rect. |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 812 | SkRect rect = |
| 813 | random->nextBool() ? SkRect::MakeXYWH(10, 10, 50, 40) : SkRect::MakeXYWH(6, 7, 0, 0); |
bsalomon | 40ef485 | 2016-05-02 13:22:13 -0700 | [diff] [blame] | 814 | SkScalar minDim = SkMinScalar(rect.width(), rect.height()); |
| 815 | SkScalar strokeWidth = random->nextUScalar1() * minDim; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 816 | |
bsalomon | 40ef485 | 2016-05-02 13:22:13 -0700 | [diff] [blame] | 817 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
| 818 | rec.setStrokeStyle(strokeWidth); |
| 819 | rec.setStrokeParams(SkPaint::kButt_Cap, |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 820 | miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f); |
bsalomon | 40ef485 | 2016-05-02 13:22:13 -0700 | [diff] [blame] | 821 | SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random); |
Michael Ludwig | 72ab346 | 2018-12-10 12:43:36 -0500 | [diff] [blame] | 822 | return AAStrokeRectOp::Make(context, std::move(paint), matrix, rect, rec); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | #endif |