joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 8 | #include "GrDefaultGeoProcFactory.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 9 | #include "GrOpFlushState.h" |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 10 | #include "GrRectOpFactory.h" |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 11 | #include "GrResourceKey.h" |
| 12 | #include "GrResourceProvider.h" |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 13 | #include "GrSimpleMeshDrawOpHelper.h" |
Cary Clark | 74f623d | 2017-11-06 20:02:02 -0500 | [diff] [blame] | 14 | #include "SkPointPriv.h" |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 15 | #include "SkStrokeRec.h" |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 16 | |
| 17 | GR_DECLARE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey); |
| 18 | GR_DECLARE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey); |
| 19 | |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 20 | static void set_inset_fan(SkPoint* pts, size_t stride, const SkRect& r, SkScalar dx, SkScalar dy) { |
Cary Clark | 74f623d | 2017-11-06 20:02:02 -0500 | [diff] [blame] | 21 | SkPointPriv::SetRectFan(pts, r.fLeft + dx, r.fTop + dy, r.fRight - dx, r.fBottom - dy, stride); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 22 | } |
| 23 | |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 24 | // We support all hairlines, bevels, and miters, but not round joins. Also, check whether the miter |
| 25 | // limit makes a miter join effectively beveled. |
| 26 | inline static bool allowed_stroke(const SkStrokeRec& stroke, bool* isMiter) { |
| 27 | SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style || |
| 28 | stroke.getStyle() == SkStrokeRec::kHairline_Style); |
| 29 | // For hairlines, make bevel and round joins appear the same as mitered ones. |
| 30 | if (!stroke.getWidth()) { |
| 31 | *isMiter = true; |
| 32 | return true; |
| 33 | } |
| 34 | if (stroke.getJoin() == SkPaint::kBevel_Join) { |
| 35 | *isMiter = false; |
| 36 | return true; |
| 37 | } |
| 38 | if (stroke.getJoin() == SkPaint::kMiter_Join) { |
| 39 | *isMiter = stroke.getMiter() >= SK_ScalarSqrt2; |
| 40 | return true; |
| 41 | } |
| 42 | return false; |
| 43 | } |
| 44 | |
| 45 | static void compute_rects(SkRect* devOutside, SkRect* devOutsideAssist, SkRect* devInside, |
| 46 | bool* isDegenerate, const SkMatrix& viewMatrix, const SkRect& rect, |
| 47 | SkScalar strokeWidth, bool miterStroke) { |
| 48 | SkRect devRect; |
| 49 | viewMatrix.mapRect(&devRect, rect); |
| 50 | |
| 51 | SkVector devStrokeSize; |
| 52 | if (strokeWidth > 0) { |
| 53 | devStrokeSize.set(strokeWidth, strokeWidth); |
| 54 | viewMatrix.mapVectors(&devStrokeSize, 1); |
| 55 | devStrokeSize.setAbs(devStrokeSize); |
| 56 | } else { |
| 57 | devStrokeSize.set(SK_Scalar1, SK_Scalar1); |
| 58 | } |
| 59 | |
| 60 | const SkScalar dx = devStrokeSize.fX; |
| 61 | const SkScalar dy = devStrokeSize.fY; |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 62 | const SkScalar rx = SkScalarHalf(dx); |
| 63 | const SkScalar ry = SkScalarHalf(dy); |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 64 | |
| 65 | *devOutside = devRect; |
| 66 | *devOutsideAssist = devRect; |
| 67 | *devInside = devRect; |
| 68 | |
| 69 | devOutside->outset(rx, ry); |
| 70 | devInside->inset(rx, ry); |
| 71 | |
| 72 | // If we have a degenerate stroking rect(ie the stroke is larger than inner rect) then we |
| 73 | // make a degenerate inside rect to avoid double hitting. We will also jam all of the points |
| 74 | // together when we render these rects. |
| 75 | SkScalar spare; |
| 76 | { |
| 77 | SkScalar w = devRect.width() - dx; |
| 78 | SkScalar h = devRect.height() - dy; |
| 79 | spare = SkTMin(w, h); |
| 80 | } |
| 81 | |
| 82 | *isDegenerate = spare <= 0; |
| 83 | if (*isDegenerate) { |
| 84 | devInside->fLeft = devInside->fRight = devRect.centerX(); |
| 85 | devInside->fTop = devInside->fBottom = devRect.centerY(); |
| 86 | } |
| 87 | |
| 88 | // For bevel-stroke, use 2 SkRect instances(devOutside and devOutsideAssist) |
| 89 | // to draw the outside of the octagon. Because there are 8 vertices on the outer |
| 90 | // edge, while vertex number of inner edge is 4, the same as miter-stroke. |
| 91 | if (!miterStroke) { |
| 92 | devOutside->inset(0, ry); |
| 93 | devOutsideAssist->outset(0, ry); |
| 94 | } |
| 95 | } |
| 96 | |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 97 | static sk_sp<GrGeometryProcessor> create_stroke_rect_gp(const GrShaderCaps* shaderCaps, |
| 98 | bool tweakAlphaForCoverage, |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 99 | const SkMatrix& viewMatrix, |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 100 | bool usesLocalCoords) { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 101 | using namespace GrDefaultGeoProcFactory; |
| 102 | |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 103 | Coverage::Type coverageType; |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 104 | if (tweakAlphaForCoverage) { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 105 | coverageType = Coverage::kSolid_Type; |
| 106 | } else { |
| 107 | coverageType = Coverage::kAttribute_Type; |
| 108 | } |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 109 | LocalCoords::Type localCoordsType = |
| 110 | usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type; |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 111 | return MakeForDeviceSpace(shaderCaps, |
| 112 | Color::kPremulGrColorAttribute_Type, |
| 113 | coverageType, |
| 114 | localCoordsType, |
Brian Salomon | 3de0aee | 2017-01-29 09:34:17 -0500 | [diff] [blame] | 115 | viewMatrix); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 118 | namespace { |
| 119 | |
| 120 | class AAStrokeRectOp final : public GrMeshDrawOp { |
| 121 | private: |
| 122 | using Helper = GrSimpleMeshDrawOpHelper; |
| 123 | |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 124 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 125 | DEFINE_OP_CLASS_ID |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 126 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 127 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 128 | GrPaint&& paint, |
| 129 | const SkMatrix& viewMatrix, |
| 130 | const SkRect& devOutside, |
| 131 | const SkRect& devInside) { |
| 132 | return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, |
| 133 | devOutside, devInside); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 136 | AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, |
Brian Osman | 936fe7d | 2018-10-30 15:30:35 -0400 | [diff] [blame] | 137 | const SkMatrix& viewMatrix, const SkRect& devOutside, const SkRect& devInside) |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 138 | : INHERITED(ClassID()) |
| 139 | , fHelper(helperArgs, GrAAType::kCoverage) |
| 140 | , fViewMatrix(viewMatrix) { |
caryclark | d656200 | 2016-07-27 12:02:07 -0700 | [diff] [blame] | 141 | SkASSERT(!devOutside.isEmpty()); |
| 142 | SkASSERT(!devInside.isEmpty()); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 143 | |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 144 | fRects.emplace_back(RectInfo{color, devOutside, devOutside, devInside, false}); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 145 | this->setBounds(devOutside, HasAABloat::kYes, IsZeroArea::kNo); |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 146 | fMiterStroke = true; |
| 147 | } |
| 148 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 149 | static std::unique_ptr<GrDrawOp> Make(GrContext* context, |
| 150 | GrPaint&& paint, |
| 151 | const SkMatrix& viewMatrix, |
| 152 | const SkRect& rect, |
| 153 | const SkStrokeRec& stroke) { |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 154 | bool isMiter; |
| 155 | if (!allowed_stroke(stroke, &isMiter)) { |
| 156 | return nullptr; |
| 157 | } |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 158 | return Helper::FactoryHelper<AAStrokeRectOp>(context, std::move(paint), viewMatrix, rect, |
| 159 | stroke, isMiter); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 160 | } |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 161 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 162 | AAStrokeRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, |
Brian Osman | 936fe7d | 2018-10-30 15:30:35 -0400 | [diff] [blame] | 163 | const SkMatrix& viewMatrix, const SkRect& rect, const SkStrokeRec& stroke, |
| 164 | bool isMiter) |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 165 | : INHERITED(ClassID()) |
| 166 | , fHelper(helperArgs, GrAAType::kCoverage) |
| 167 | , fViewMatrix(viewMatrix) { |
| 168 | fMiterStroke = isMiter; |
| 169 | RectInfo& info = fRects.push_back(); |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 170 | compute_rects(&info.fDevOutside, &info.fDevOutsideAssist, &info.fDevInside, |
| 171 | &info.fDegenerate, viewMatrix, rect, stroke.getWidth(), isMiter); |
| 172 | info.fColor = color; |
Brian Salomon | 510dd42 | 2017-03-16 12:15:22 -0400 | [diff] [blame] | 173 | if (isMiter) { |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 174 | this->setBounds(info.fDevOutside, HasAABloat::kYes, IsZeroArea::kNo); |
Brian Salomon | 510dd42 | 2017-03-16 12:15:22 -0400 | [diff] [blame] | 175 | } else { |
| 176 | // The outer polygon of the bevel stroke is an octagon specified by the points of a |
| 177 | // pair of overlapping rectangles where one is wide and the other is narrow. |
| 178 | SkRect bounds = info.fDevOutside; |
| 179 | bounds.joinPossiblyEmptyRect(info.fDevOutsideAssist); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 180 | this->setBounds(bounds, HasAABloat::kYes, IsZeroArea::kNo); |
Brian Salomon | 510dd42 | 2017-03-16 12:15:22 -0400 | [diff] [blame] | 181 | } |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | const char* name() const override { return "AAStrokeRect"; } |
| 185 | |
Brian Salomon | 7d94bb5 | 2018-10-12 14:37:19 -0400 | [diff] [blame] | 186 | void visitProxies(const VisitProxyFunc& func, VisitorType) const override { |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 187 | fHelper.visitProxies(func); |
| 188 | } |
| 189 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame^] | 190 | #ifdef SK_DEBUG |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 191 | SkString dumpInfo() const override { |
| 192 | SkString string; |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 193 | for (const auto& info : fRects) { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 194 | string.appendf( |
| 195 | "Color: 0x%08x, ORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], " |
| 196 | "AssistORect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], " |
| 197 | "IRect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], Degen: %d", |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 198 | info.fColor.toBytes_RGBA(), info.fDevOutside.fLeft, info.fDevOutside.fTop, |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 199 | info.fDevOutside.fRight, info.fDevOutside.fBottom, info.fDevOutsideAssist.fLeft, |
| 200 | info.fDevOutsideAssist.fTop, info.fDevOutsideAssist.fRight, |
| 201 | info.fDevOutsideAssist.fBottom, info.fDevInside.fLeft, info.fDevInside.fTop, |
| 202 | info.fDevInside.fRight, info.fDevInside.fBottom, info.fDegenerate); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 203 | } |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 204 | string += fHelper.dumpInfo(); |
| 205 | string += INHERITED::dumpInfo(); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 206 | return string; |
| 207 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame^] | 208 | #endif |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 209 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 210 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
Brian Salomon | a0485d9 | 2017-06-14 19:08:01 -0400 | [diff] [blame] | 211 | |
Brian Osman | 532b3f9 | 2018-07-11 10:02:07 -0400 | [diff] [blame] | 212 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override { |
| 213 | return fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kSingleChannel, |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 214 | &fRects.back().fColor); |
Brian Salomon | a0485d9 | 2017-06-14 19:08:01 -0400 | [diff] [blame] | 215 | } |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 216 | |
| 217 | private: |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 218 | void onPrepareDraws(Target*) override; |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 219 | |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 220 | static const int kMiterIndexCnt = 3 * 24; |
| 221 | static const int kMiterVertexCnt = 16; |
| 222 | static const int kNumMiterRectsInIndexBuffer = 256; |
| 223 | |
| 224 | static const int kBevelIndexCnt = 48 + 36 + 24; |
| 225 | static const int kBevelVertexCnt = 24; |
| 226 | static const int kNumBevelRectsInIndexBuffer = 256; |
| 227 | |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 228 | static sk_sp<const GrBuffer> GetIndexBuffer(GrResourceProvider*, bool miterStroke); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 229 | |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 230 | const SkMatrix& viewMatrix() const { return fViewMatrix; } |
| 231 | bool miterStroke() const { return fMiterStroke; } |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 232 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 233 | CombineResult onCombineIfPossible(GrOp* t, const GrCaps&) override; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 234 | |
| 235 | void generateAAStrokeRectGeometry(void* vertices, |
| 236 | size_t offset, |
| 237 | size_t vertexStride, |
| 238 | int outerVertexNum, |
| 239 | int innerVertexNum, |
| 240 | GrColor color, |
| 241 | const SkRect& devOutside, |
| 242 | const SkRect& devOutsideAssist, |
| 243 | const SkRect& devInside, |
| 244 | bool miterStroke, |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 245 | bool degenerate, |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 246 | bool tweakAlphaForCoverage) const; |
| 247 | |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 248 | // TODO support AA rotated stroke rects by copying around view matrices |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 249 | struct RectInfo { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 250 | SkPMColor4f fColor; |
bsalomon | 8b7a9e1 | 2016-07-06 13:06:22 -0700 | [diff] [blame] | 251 | SkRect fDevOutside; |
| 252 | SkRect fDevOutsideAssist; |
| 253 | SkRect fDevInside; |
| 254 | bool fDegenerate; |
| 255 | }; |
| 256 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 257 | Helper fHelper; |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 258 | SkSTArray<1, RectInfo, true> fRects; |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 259 | SkMatrix fViewMatrix; |
| 260 | bool fMiterStroke; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 261 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 262 | typedef GrMeshDrawOp INHERITED; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 263 | }; |
| 264 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 265 | } // anonymous namespace |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 266 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 267 | void AAStrokeRectOp::onPrepareDraws(Target* target) { |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 268 | sk_sp<GrGeometryProcessor> gp(create_stroke_rect_gp(target->caps().shaderCaps(), |
| 269 | fHelper.compatibleWithAlphaAsCoverage(), |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 270 | this->viewMatrix(), |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 271 | fHelper.usesLocalCoords())); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 272 | if (!gp) { |
| 273 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
| 274 | return; |
| 275 | } |
| 276 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 277 | size_t vertexStride = fHelper.compatibleWithAlphaAsCoverage() |
| 278 | ? sizeof(GrDefaultGeoProcFactory::PositionColorAttr) |
| 279 | : sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 280 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 281 | SkASSERT(vertexStride == gp->debugOnly_vertexStride()); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 282 | int innerVertexNum = 4; |
| 283 | int outerVertexNum = this->miterStroke() ? 4 : 8; |
| 284 | int verticesPerInstance = (outerVertexNum + innerVertexNum) * 2; |
| 285 | int indicesPerInstance = this->miterStroke() ? kMiterIndexCnt : kBevelIndexCnt; |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 286 | int instanceCount = fRects.count(); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 287 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 288 | sk_sp<const GrBuffer> indexBuffer = |
| 289 | GetIndexBuffer(target->resourceProvider(), this->miterStroke()); |
| 290 | PatternHelper helper(target, GrPrimitiveType::kTriangles, vertexStride, indexBuffer.get(), |
| 291 | verticesPerInstance, indicesPerInstance, instanceCount); |
| 292 | void* vertices = helper.vertices(); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 293 | if (!vertices || !indexBuffer) { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 294 | SkDebugf("Could not allocate vertices\n"); |
| 295 | return; |
| 296 | } |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 297 | |
| 298 | for (int i = 0; i < instanceCount; i++) { |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 299 | const RectInfo& info = fRects[i]; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 300 | this->generateAAStrokeRectGeometry(vertices, |
| 301 | i * verticesPerInstance * vertexStride, |
| 302 | vertexStride, |
| 303 | outerVertexNum, |
| 304 | innerVertexNum, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 305 | info.fColor.toBytes_RGBA(), // TODO4F |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 306 | info.fDevOutside, |
| 307 | info.fDevOutsideAssist, |
| 308 | info.fDevInside, |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 309 | fMiterStroke, |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 310 | info.fDegenerate, |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 311 | fHelper.compatibleWithAlphaAsCoverage()); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 312 | } |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 313 | auto pipe = fHelper.makePipeline(target); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 314 | helper.recordDraw(target, std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 315 | } |
| 316 | |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 317 | sk_sp<const GrBuffer> AAStrokeRectOp::GetIndexBuffer(GrResourceProvider* resourceProvider, |
| 318 | bool miterStroke) { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 319 | if (miterStroke) { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 320 | // clang-format off |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 321 | static const uint16_t gMiterIndices[] = { |
| 322 | 0 + 0, 1 + 0, 5 + 0, 5 + 0, 4 + 0, 0 + 0, |
| 323 | 1 + 0, 2 + 0, 6 + 0, 6 + 0, 5 + 0, 1 + 0, |
| 324 | 2 + 0, 3 + 0, 7 + 0, 7 + 0, 6 + 0, 2 + 0, |
| 325 | 3 + 0, 0 + 0, 4 + 0, 4 + 0, 7 + 0, 3 + 0, |
| 326 | |
| 327 | 0 + 4, 1 + 4, 5 + 4, 5 + 4, 4 + 4, 0 + 4, |
| 328 | 1 + 4, 2 + 4, 6 + 4, 6 + 4, 5 + 4, 1 + 4, |
| 329 | 2 + 4, 3 + 4, 7 + 4, 7 + 4, 6 + 4, 2 + 4, |
| 330 | 3 + 4, 0 + 4, 4 + 4, 4 + 4, 7 + 4, 3 + 4, |
| 331 | |
| 332 | 0 + 8, 1 + 8, 5 + 8, 5 + 8, 4 + 8, 0 + 8, |
| 333 | 1 + 8, 2 + 8, 6 + 8, 6 + 8, 5 + 8, 1 + 8, |
| 334 | 2 + 8, 3 + 8, 7 + 8, 7 + 8, 6 + 8, 2 + 8, |
| 335 | 3 + 8, 0 + 8, 4 + 8, 4 + 8, 7 + 8, 3 + 8, |
| 336 | }; |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 337 | // clang-format on |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 338 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(gMiterIndices) == kMiterIndexCnt); |
| 339 | GR_DEFINE_STATIC_UNIQUE_KEY(gMiterIndexBufferKey); |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 340 | return resourceProvider->findOrCreatePatternedIndexBuffer( |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 341 | gMiterIndices, kMiterIndexCnt, kNumMiterRectsInIndexBuffer, kMiterVertexCnt, |
| 342 | gMiterIndexBufferKey); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 343 | } else { |
| 344 | /** |
| 345 | * As in miter-stroke, index = a + b, and a is the current index, b is the shift |
| 346 | * from the first index. The index layout: |
| 347 | * outer AA line: 0~3, 4~7 |
| 348 | * outer edge: 8~11, 12~15 |
| 349 | * inner edge: 16~19 |
| 350 | * inner AA line: 20~23 |
| 351 | * Following comes a bevel-stroke rect and its indices: |
| 352 | * |
| 353 | * 4 7 |
| 354 | * ********************************* |
| 355 | * * ______________________________ * |
| 356 | * * / 12 15 \ * |
| 357 | * * / \ * |
| 358 | * 0 * |8 16_____________________19 11 | * 3 |
| 359 | * * | | | | * |
| 360 | * * | | **************** | | * |
| 361 | * * | | * 20 23 * | | * |
| 362 | * * | | * * | | * |
| 363 | * * | | * 21 22 * | | * |
| 364 | * * | | **************** | | * |
| 365 | * * | |____________________| | * |
| 366 | * 1 * |9 17 18 10| * 2 |
| 367 | * * \ / * |
| 368 | * * \13 __________________________14/ * |
| 369 | * * * |
| 370 | * ********************************** |
| 371 | * 5 6 |
| 372 | */ |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 373 | // clang-format off |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 374 | static const uint16_t gBevelIndices[] = { |
| 375 | // Draw outer AA, from outer AA line to outer edge, shift is 0. |
| 376 | 0 + 0, 1 + 0, 9 + 0, 9 + 0, 8 + 0, 0 + 0, |
| 377 | 1 + 0, 5 + 0, 13 + 0, 13 + 0, 9 + 0, 1 + 0, |
| 378 | 5 + 0, 6 + 0, 14 + 0, 14 + 0, 13 + 0, 5 + 0, |
| 379 | 6 + 0, 2 + 0, 10 + 0, 10 + 0, 14 + 0, 6 + 0, |
| 380 | 2 + 0, 3 + 0, 11 + 0, 11 + 0, 10 + 0, 2 + 0, |
| 381 | 3 + 0, 7 + 0, 15 + 0, 15 + 0, 11 + 0, 3 + 0, |
| 382 | 7 + 0, 4 + 0, 12 + 0, 12 + 0, 15 + 0, 7 + 0, |
| 383 | 4 + 0, 0 + 0, 8 + 0, 8 + 0, 12 + 0, 4 + 0, |
| 384 | |
| 385 | // Draw the stroke, from outer edge to inner edge, shift is 8. |
| 386 | 0 + 8, 1 + 8, 9 + 8, 9 + 8, 8 + 8, 0 + 8, |
| 387 | 1 + 8, 5 + 8, 9 + 8, |
| 388 | 5 + 8, 6 + 8, 10 + 8, 10 + 8, 9 + 8, 5 + 8, |
| 389 | 6 + 8, 2 + 8, 10 + 8, |
| 390 | 2 + 8, 3 + 8, 11 + 8, 11 + 8, 10 + 8, 2 + 8, |
| 391 | 3 + 8, 7 + 8, 11 + 8, |
| 392 | 7 + 8, 4 + 8, 8 + 8, 8 + 8, 11 + 8, 7 + 8, |
| 393 | 4 + 8, 0 + 8, 8 + 8, |
| 394 | |
| 395 | // Draw the inner AA, from inner edge to inner AA line, shift is 16. |
| 396 | 0 + 16, 1 + 16, 5 + 16, 5 + 16, 4 + 16, 0 + 16, |
| 397 | 1 + 16, 2 + 16, 6 + 16, 6 + 16, 5 + 16, 1 + 16, |
| 398 | 2 + 16, 3 + 16, 7 + 16, 7 + 16, 6 + 16, 2 + 16, |
| 399 | 3 + 16, 0 + 16, 4 + 16, 4 + 16, 7 + 16, 3 + 16, |
| 400 | }; |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 401 | // clang-format on |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 402 | GR_STATIC_ASSERT(SK_ARRAY_COUNT(gBevelIndices) == kBevelIndexCnt); |
| 403 | |
| 404 | GR_DEFINE_STATIC_UNIQUE_KEY(gBevelIndexBufferKey); |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 405 | return resourceProvider->findOrCreatePatternedIndexBuffer( |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 406 | gBevelIndices, kBevelIndexCnt, kNumBevelRectsInIndexBuffer, kBevelVertexCnt, |
| 407 | gBevelIndexBufferKey); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 411 | GrOp::CombineResult AAStrokeRectOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 412 | AAStrokeRectOp* that = t->cast<AAStrokeRectOp>(); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 413 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 414 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 415 | return CombineResult::kCannotCombine; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 418 | // TODO combine across miterstroke changes |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 419 | if (this->miterStroke() != that->miterStroke()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 420 | return CombineResult::kCannotCombine; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | // 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] | 424 | // local coords then we won't be able to combine. TODO: Upload local coords as an attribute. |
| 425 | if (fHelper.usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 426 | return CombineResult::kCannotCombine; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Brian Salomon | 8c5bad3 | 2016-12-20 14:43:36 -0500 | [diff] [blame] | 429 | fRects.push_back_n(that->fRects.count(), that->fRects.begin()); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 430 | return CombineResult::kMerged; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 431 | } |
| 432 | |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 433 | static void setup_scale(int* scale, SkScalar inset) { |
| 434 | if (inset < SK_ScalarHalf) { |
| 435 | *scale = SkScalarFloorToInt(512.0f * inset / (inset + SK_ScalarHalf)); |
| 436 | SkASSERT(*scale >= 0 && *scale <= 255); |
| 437 | } else { |
| 438 | *scale = 0xff; |
| 439 | } |
| 440 | } |
| 441 | |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 442 | void AAStrokeRectOp::generateAAStrokeRectGeometry(void* vertices, |
| 443 | size_t offset, |
| 444 | size_t vertexStride, |
| 445 | int outerVertexNum, |
| 446 | int innerVertexNum, |
| 447 | GrColor color, |
| 448 | const SkRect& devOutside, |
| 449 | const SkRect& devOutsideAssist, |
| 450 | const SkRect& devInside, |
| 451 | bool miterStroke, |
| 452 | bool degenerate, |
| 453 | bool tweakAlphaForCoverage) const { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 454 | intptr_t verts = reinterpret_cast<intptr_t>(vertices) + offset; |
| 455 | |
| 456 | // We create vertices for four nested rectangles. There are two ramps from 0 to full |
| 457 | // coverage, one on the exterior of the stroke and the other on the interior. |
| 458 | // The following pointers refer to the four rects, from outermost to innermost. |
| 459 | SkPoint* fan0Pos = reinterpret_cast<SkPoint*>(verts); |
| 460 | SkPoint* fan1Pos = reinterpret_cast<SkPoint*>(verts + outerVertexNum * vertexStride); |
| 461 | SkPoint* fan2Pos = reinterpret_cast<SkPoint*>(verts + 2 * outerVertexNum * vertexStride); |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 462 | SkPoint* fan3Pos = reinterpret_cast<SkPoint*>( |
| 463 | verts + (2 * outerVertexNum + innerVertexNum) * vertexStride); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 464 | |
| 465 | #ifndef SK_IGNORE_THIN_STROKED_RECT_FIX |
| 466 | // TODO: this only really works if the X & Y margins are the same all around |
| 467 | // the rect (or if they are all >= 1.0). |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 468 | SkScalar inset; |
| 469 | if (!degenerate) { |
| 470 | inset = SkMinScalar(SK_Scalar1, devOutside.fRight - devInside.fRight); |
| 471 | inset = SkMinScalar(inset, devInside.fLeft - devOutside.fLeft); |
| 472 | inset = SkMinScalar(inset, devInside.fTop - devOutside.fTop); |
| 473 | if (miterStroke) { |
| 474 | inset = SK_ScalarHalf * SkMinScalar(inset, devOutside.fBottom - devInside.fBottom); |
| 475 | } else { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 476 | inset = SK_ScalarHalf * |
| 477 | SkMinScalar(inset, devOutsideAssist.fBottom - devInside.fBottom); |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 478 | } |
| 479 | SkASSERT(inset >= 0); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 480 | } else { |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 481 | // TODO use real devRect here |
| 482 | inset = SkMinScalar(devOutside.width(), SK_Scalar1); |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 483 | inset = SK_ScalarHalf * |
| 484 | SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height())); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 485 | } |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 486 | #else |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 487 | SkScalar inset; |
| 488 | if (!degenerate) { |
| 489 | inset = SK_ScalarHalf; |
| 490 | } else { |
| 491 | // TODO use real devRect here |
| 492 | inset = SkMinScalar(devOutside.width(), SK_Scalar1); |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 493 | inset = SK_ScalarHalf * |
| 494 | SkMinScalar(inset, SkTMax(devOutside.height(), devOutsideAssist.height())); |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 495 | } |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 496 | #endif |
| 497 | |
| 498 | if (miterStroke) { |
| 499 | // outermost |
| 500 | set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf); |
| 501 | // inner two |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 502 | set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset); |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 503 | if (!degenerate) { |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 504 | set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset); |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 505 | // innermost |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 506 | set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf); |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 507 | } else { |
| 508 | // When the interior rect has become degenerate we smoosh to a single point |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 509 | SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom); |
Cary Clark | 74f623d | 2017-11-06 20:02:02 -0500 | [diff] [blame] | 510 | SkPointPriv::SetRectFan(fan2Pos, devInside.fLeft, devInside.fTop, devInside.fRight, |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 511 | devInside.fBottom, vertexStride); |
Cary Clark | 74f623d | 2017-11-06 20:02:02 -0500 | [diff] [blame] | 512 | SkPointPriv::SetRectFan(fan3Pos, devInside.fLeft, devInside.fTop, devInside.fRight, |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 513 | devInside.fBottom, vertexStride); |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 514 | } |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 515 | } else { |
| 516 | SkPoint* fan0AssistPos = reinterpret_cast<SkPoint*>(verts + 4 * vertexStride); |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 517 | SkPoint* fan1AssistPos = |
| 518 | reinterpret_cast<SkPoint*>(verts + (outerVertexNum + 4) * vertexStride); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 519 | // outermost |
| 520 | set_inset_fan(fan0Pos, vertexStride, devOutside, -SK_ScalarHalf, -SK_ScalarHalf); |
| 521 | set_inset_fan(fan0AssistPos, vertexStride, devOutsideAssist, -SK_ScalarHalf, |
| 522 | -SK_ScalarHalf); |
| 523 | // outer one of the inner two |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 524 | set_inset_fan(fan1Pos, vertexStride, devOutside, inset, inset); |
| 525 | set_inset_fan(fan1AssistPos, vertexStride, devOutsideAssist, inset, inset); |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 526 | if (!degenerate) { |
| 527 | // inner one of the inner two |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 528 | set_inset_fan(fan2Pos, vertexStride, devInside, -inset, -inset); |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 529 | // innermost |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 530 | set_inset_fan(fan3Pos, vertexStride, devInside, SK_ScalarHalf, SK_ScalarHalf); |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 531 | } else { |
| 532 | // When the interior rect has become degenerate we smoosh to a single point |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 533 | SkASSERT(devInside.fLeft == devInside.fRight && devInside.fTop == devInside.fBottom); |
Cary Clark | 74f623d | 2017-11-06 20:02:02 -0500 | [diff] [blame] | 534 | SkPointPriv::SetRectFan(fan2Pos, devInside.fLeft, devInside.fTop, devInside.fRight, |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 535 | devInside.fBottom, vertexStride); |
Cary Clark | 74f623d | 2017-11-06 20:02:02 -0500 | [diff] [blame] | 536 | SkPointPriv::SetRectFan(fan3Pos, devInside.fLeft, devInside.fTop, devInside.fRight, |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 537 | devInside.fBottom, vertexStride); |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 538 | } |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | // Make verts point to vertex color and then set all the color and coverage vertex attrs |
| 542 | // values. The outermost rect has 0 coverage |
| 543 | verts += sizeof(SkPoint); |
| 544 | for (int i = 0; i < outerVertexNum; ++i) { |
| 545 | if (tweakAlphaForCoverage) { |
| 546 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = 0; |
| 547 | } else { |
| 548 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; |
| 549 | *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = 0; |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | // scale is the coverage for the the inner two rects. |
| 554 | int scale; |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 555 | setup_scale(&scale, inset); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 556 | |
| 557 | float innerCoverage = GrNormalizeByteToFloat(scale); |
| 558 | GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale); |
| 559 | |
| 560 | verts += outerVertexNum * vertexStride; |
| 561 | for (int i = 0; i < outerVertexNum + innerVertexNum; ++i) { |
| 562 | if (tweakAlphaForCoverage) { |
| 563 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor; |
| 564 | } else { |
| 565 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 566 | *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 570 | // The innermost rect has 0 coverage, unless we are degenerate, in which case we must apply the |
| 571 | // scaled coverage |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 572 | verts += (outerVertexNum + innerVertexNum) * vertexStride; |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 573 | if (!degenerate) { |
| 574 | innerCoverage = 0; |
| 575 | scaledColor = 0; |
| 576 | } |
| 577 | |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 578 | for (int i = 0; i < innerVertexNum; ++i) { |
| 579 | if (tweakAlphaForCoverage) { |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 580 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 581 | } else { |
| 582 | *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color; |
joshualitt | 11edad9 | 2015-09-22 10:32:28 -0700 | [diff] [blame] | 583 | *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) = innerCoverage; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 588 | namespace GrRectOpFactory { |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 589 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 590 | std::unique_ptr<GrDrawOp> MakeAAFillNestedRects(GrContext* context, |
| 591 | GrPaint&& paint, |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 592 | const SkMatrix& viewMatrix, |
| 593 | const SkRect rects[2]) { |
| 594 | SkASSERT(viewMatrix.rectStaysRect()); |
| 595 | SkASSERT(!rects[0].isEmpty() && !rects[1].isEmpty()); |
| 596 | |
| 597 | SkRect devOutside, devInside; |
| 598 | viewMatrix.mapRect(&devOutside, rects[0]); |
| 599 | viewMatrix.mapRect(&devInside, rects[1]); |
| 600 | if (devInside.isEmpty()) { |
| 601 | if (devOutside.isEmpty()) { |
| 602 | return nullptr; |
| 603 | } |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 604 | return MakeAAFill(context, std::move(paint), viewMatrix, rects[0]); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 605 | } |
| 606 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 607 | return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, devOutside, devInside); |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 610 | std::unique_ptr<GrDrawOp> MakeAAStroke(GrContext* context, |
| 611 | GrPaint&& paint, |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 612 | const SkMatrix& viewMatrix, |
| 613 | const SkRect& rect, |
| 614 | const SkStrokeRec& stroke) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 615 | return AAStrokeRectOp::Make(context, std::move(paint), viewMatrix, rect, stroke); |
joshualitt | 10cae83 | 2015-09-22 12:50:33 -0700 | [diff] [blame] | 616 | } |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 617 | |
| 618 | } // namespace GrRectOpFactory |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 619 | |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 620 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 621 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 622 | #if GR_TEST_UTILS |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 623 | |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 624 | #include "GrDrawOpTest.h" |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 625 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 626 | GR_DRAW_OP_TEST_DEFINE(AAStrokeRectOp) { |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 627 | bool miterStroke = random->nextBool(); |
| 628 | |
bsalomon | 40ef485 | 2016-05-02 13:22:13 -0700 | [diff] [blame] | 629 | // Create either a empty rect or a non-empty rect. |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 630 | SkRect rect = |
| 631 | 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] | 632 | SkScalar minDim = SkMinScalar(rect.width(), rect.height()); |
| 633 | SkScalar strokeWidth = random->nextUScalar1() * minDim; |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 634 | |
bsalomon | 40ef485 | 2016-05-02 13:22:13 -0700 | [diff] [blame] | 635 | SkStrokeRec rec(SkStrokeRec::kFill_InitStyle); |
| 636 | rec.setStrokeStyle(strokeWidth); |
| 637 | rec.setStrokeParams(SkPaint::kButt_Cap, |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 638 | miterStroke ? SkPaint::kMiter_Join : SkPaint::kBevel_Join, 1.f); |
bsalomon | 40ef485 | 2016-05-02 13:22:13 -0700 | [diff] [blame] | 639 | SkMatrix matrix = GrTest::TestMatrixRectStaysRect(random); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 640 | return GrRectOpFactory::MakeAAStroke(context, std::move(paint), matrix, rect, rec); |
joshualitt | 9ff6425 | 2015-08-10 09:03:51 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | #endif |