joshualitt | 3566d44 | 2015-09-18 07:12:55 -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 | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 8 | #include "GrColor.h" |
| 9 | #include "GrDefaultGeoProcFactory.h" |
Brian Salomon | 5ec9def | 2016-12-20 15:34:05 -0500 | [diff] [blame] | 10 | #include "GrDrawOpTest.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 11 | #include "GrMeshDrawOp.h" |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 12 | #include "GrOpFlushState.h" |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 13 | #include "GrRectOpFactory.h" |
| 14 | #include "GrSimpleMeshDrawOpHelper.h" |
Brian Salomon | a0485d9 | 2017-06-14 19:08:01 -0400 | [diff] [blame] | 15 | #include "SkRandom.h" |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 16 | #include "SkStrokeRec.h" |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 17 | |
| 18 | /* create a triangle strip that strokes the specified rect. There are 8 |
| 19 | unique vertices, but we repeat the last 2 to close up. Alternatively we |
| 20 | could use an indices array, and then only send 8 verts, but not sure that |
| 21 | would be faster. |
| 22 | */ |
| 23 | static void init_stroke_rect_strip(SkPoint verts[10], const SkRect& rect, SkScalar width) { |
| 24 | const SkScalar rad = SkScalarHalf(width); |
| 25 | // TODO we should be able to enable this assert, but we'd have to filter these draws |
| 26 | // this is a bug |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 27 | // SkASSERT(rad < rect.width() / 2 && rad < rect.height() / 2); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 28 | |
| 29 | verts[0].set(rect.fLeft + rad, rect.fTop + rad); |
| 30 | verts[1].set(rect.fLeft - rad, rect.fTop - rad); |
| 31 | verts[2].set(rect.fRight - rad, rect.fTop + rad); |
| 32 | verts[3].set(rect.fRight + rad, rect.fTop - rad); |
| 33 | verts[4].set(rect.fRight - rad, rect.fBottom - rad); |
| 34 | verts[5].set(rect.fRight + rad, rect.fBottom + rad); |
| 35 | verts[6].set(rect.fLeft + rad, rect.fBottom - rad); |
| 36 | verts[7].set(rect.fLeft - rad, rect.fBottom + rad); |
| 37 | verts[8] = verts[0]; |
| 38 | verts[9] = verts[1]; |
| 39 | } |
| 40 | |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 41 | // Allow all hairlines and all miters, so long as the miter limit doesn't produce beveled corners. |
| 42 | inline static bool allowed_stroke(const SkStrokeRec& stroke) { |
| 43 | SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style || |
| 44 | stroke.getStyle() == SkStrokeRec::kHairline_Style); |
| 45 | return !stroke.getWidth() || |
| 46 | (stroke.getJoin() == SkPaint::kMiter_Join && stroke.getMiter() > SK_ScalarSqrt2); |
| 47 | } |
| 48 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 49 | namespace { |
| 50 | |
| 51 | class NonAAStrokeRectOp final : public GrMeshDrawOp { |
| 52 | private: |
| 53 | using Helper = GrSimpleMeshDrawOpHelper; |
| 54 | |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 55 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 56 | DEFINE_OP_CLASS_ID |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 57 | |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 58 | const char* name() const override { return "NonAAStrokeRectOp"; } |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 59 | |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 60 | SkString dumpInfo() const override { |
| 61 | SkString string; |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 62 | string.appendf( |
| 63 | "Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], " |
| 64 | "StrokeWidth: %.2f\n", |
| 65 | fColor, fRect.fLeft, fRect.fTop, fRect.fRight, fRect.fBottom, fStrokeWidth); |
Brian Salomon | 82dfd3d | 2017-06-14 12:30:35 -0400 | [diff] [blame] | 66 | string += fHelper.dumpInfo(); |
| 67 | string += INHERITED::dumpInfo(); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 68 | return string; |
| 69 | } |
| 70 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 71 | static std::unique_ptr<GrDrawOp> Make(GrPaint&& paint, const SkMatrix& viewMatrix, |
| 72 | const SkRect& rect, const SkStrokeRec& stroke, |
| 73 | GrAAType aaType) { |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 74 | if (!allowed_stroke(stroke)) { |
| 75 | return nullptr; |
| 76 | } |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 77 | Helper::Flags flags = Helper::Flags::kNone; |
| 78 | // Depending on sub-pixel coordinates and the particular GPU, we may lose a corner of |
| 79 | // hairline rects. We jam all the vertices to pixel centers to avoid this, but not |
| 80 | // when MSAA is enabled because it can cause ugly artifacts. |
| 81 | if (stroke.getStyle() == SkStrokeRec::kHairline_Style && aaType != GrAAType::kMSAA) { |
| 82 | flags |= Helper::Flags::kSnapVerticesToPixelCenters; |
| 83 | } |
| 84 | return Helper::FactoryHelper<NonAAStrokeRectOp>(std::move(paint), flags, viewMatrix, rect, |
| 85 | stroke, aaType); |
| 86 | } |
Brian Salomon | 1ec03f3 | 2017-06-14 09:49:26 -0400 | [diff] [blame] | 87 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 88 | NonAAStrokeRectOp(const Helper::MakeArgs& helperArgs, GrColor color, Helper::Flags flags, |
| 89 | const SkMatrix& viewMatrix, const SkRect& rect, const SkStrokeRec& stroke, |
| 90 | GrAAType aaType) |
| 91 | : INHERITED(ClassID()), fHelper(helperArgs, aaType, flags) { |
| 92 | fColor = color; |
| 93 | fViewMatrix = viewMatrix; |
| 94 | fRect = rect; |
| 95 | // Sort the rect for hairlines |
| 96 | fRect.sort(); |
| 97 | fStrokeWidth = stroke.getWidth(); |
| 98 | |
| 99 | SkScalar rad = SkScalarHalf(fStrokeWidth); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 100 | SkRect bounds = rect; |
| 101 | bounds.outset(rad, rad); |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 102 | |
| 103 | // If our caller snaps to pixel centers then we have to round out the bounds |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 104 | if (flags & Helper::Flags::kSnapVerticesToPixelCenters) { |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 105 | viewMatrix.mapRect(&bounds); |
egdaniel | 7e8cc21 | 2016-07-06 14:38:34 -0700 | [diff] [blame] | 106 | // We want to be consistent with how we snap non-aa lines. To match what we do in |
| 107 | // GrGLSLVertexShaderBuilder, we first floor all the vertex values and then add half a |
| 108 | // pixel to force us to pixel centers. |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 109 | bounds.set(SkScalarFloorToScalar(bounds.fLeft), |
| 110 | SkScalarFloorToScalar(bounds.fTop), |
| 111 | SkScalarFloorToScalar(bounds.fRight), |
| 112 | SkScalarFloorToScalar(bounds.fBottom)); |
| 113 | bounds.offset(0.5f, 0.5f); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 114 | this->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 115 | } else { |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 116 | this->setTransformedBounds(bounds, fViewMatrix, HasAABloat::kNo, IsZeroArea::kNo); |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 117 | } |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 121 | |
Brian Salomon | f86d37b | 2017-06-16 10:04:34 -0400 | [diff] [blame] | 122 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override { |
| 123 | return fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kNone, |
| 124 | &fColor); |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 125 | } |
| 126 | |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 127 | private: |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 128 | void onPrepareDraws(Target* target) const override { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 129 | sk_sp<GrGeometryProcessor> gp; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 130 | { |
| 131 | using namespace GrDefaultGeoProcFactory; |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 132 | Color color(fColor); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 133 | LocalCoords::Type localCoordsType = fHelper.usesLocalCoords() |
Brian Salomon | 8c852be | 2017-01-04 10:44:42 -0500 | [diff] [blame] | 134 | ? LocalCoords::kUsePosition_Type |
| 135 | : LocalCoords::kUnused_Type; |
| 136 | gp = GrDefaultGeoProcFactory::Make(color, Coverage::kSolid_Type, localCoordsType, |
| 137 | fViewMatrix); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 138 | } |
| 139 | |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 140 | size_t vertexStride = gp->getVertexStride(); |
| 141 | |
| 142 | SkASSERT(vertexStride == sizeof(GrDefaultGeoProcFactory::PositionAttr)); |
| 143 | |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 144 | int vertexCount = kVertsPerHairlineRect; |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 145 | if (fStrokeWidth > 0) { |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 146 | vertexCount = kVertsPerStrokeRect; |
| 147 | } |
| 148 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 149 | const GrBuffer* vertexBuffer; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 150 | int firstVertex; |
| 151 | |
Brian Salomon | 6a63904 | 2016-12-14 11:08:17 -0500 | [diff] [blame] | 152 | void* verts = |
| 153 | target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 154 | |
| 155 | if (!verts) { |
| 156 | SkDebugf("Could not allocate vertices\n"); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | SkPoint* vertex = reinterpret_cast<SkPoint*>(verts); |
| 161 | |
| 162 | GrPrimitiveType primType; |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 163 | if (fStrokeWidth > 0) { |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 164 | primType = GrPrimitiveType::kTriangleStrip; |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 165 | init_stroke_rect_strip(vertex, fRect, fStrokeWidth); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 166 | } else { |
| 167 | // hairline |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 168 | primType = GrPrimitiveType::kLineStrip; |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 169 | vertex[0].set(fRect.fLeft, fRect.fTop); |
| 170 | vertex[1].set(fRect.fRight, fRect.fTop); |
| 171 | vertex[2].set(fRect.fRight, fRect.fBottom); |
| 172 | vertex[3].set(fRect.fLeft, fRect.fBottom); |
| 173 | vertex[4].set(fRect.fLeft, fRect.fTop); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 176 | GrMesh mesh(primType); |
Chris Dalton | 1d61635 | 2017-05-31 12:51:23 -0600 | [diff] [blame] | 177 | mesh.setNonIndexedNonInstanced(vertexCount); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 178 | mesh.setVertexData(vertexBuffer, firstVertex); |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 179 | target->draw(gp.get(), fHelper.makePipeline(target), mesh); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 182 | bool onCombineIfPossible(GrOp* t, const GrCaps&) override { |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 183 | // NonAA stroke rects cannot combine right now |
| 184 | // TODO make these combinable. |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 185 | return false; |
| 186 | } |
| 187 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 188 | Helper fHelper; |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 189 | GrColor fColor; |
| 190 | SkMatrix fViewMatrix; |
| 191 | SkRect fRect; |
| 192 | SkScalar fStrokeWidth; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 193 | |
| 194 | const static int kVertsPerHairlineRect = 5; |
| 195 | const static int kVertsPerStrokeRect = 10; |
| 196 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 197 | typedef GrMeshDrawOp INHERITED; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 198 | }; |
| 199 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 200 | } // anonymous namespace |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 201 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 202 | namespace GrRectOpFactory { |
| 203 | std::unique_ptr<GrDrawOp> MakeNonAAStroke(GrPaint&& paint, |
| 204 | const SkMatrix& viewMatrix, |
| 205 | const SkRect& rect, |
| 206 | const SkStrokeRec& stroke, |
| 207 | GrAAType aaType) { |
| 208 | return NonAAStrokeRectOp::Make(std::move(paint), viewMatrix, rect, stroke, aaType); |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 209 | } |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 210 | } // namespace GrRectOpFactory |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 211 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 212 | #if GR_TEST_UTILS |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 213 | |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 214 | GR_DRAW_OP_TEST_DEFINE(NonAAStrokeRectOp) { |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 215 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 216 | SkRect rect = GrTest::TestRect(random); |
bsalomon | a7d85ba | 2016-07-06 11:54:59 -0700 | [diff] [blame] | 217 | SkScalar strokeWidth = random->nextBool() ? 0.0f : 2.0f; |
Brian Salomon | baaf439 | 2017-06-15 09:59:23 -0400 | [diff] [blame] | 218 | SkPaint strokePaint; |
| 219 | strokePaint.setStrokeWidth(strokeWidth); |
| 220 | strokePaint.setStyle(SkPaint::kStroke_Style); |
| 221 | strokePaint.setStrokeJoin(SkPaint::kMiter_Join); |
| 222 | SkStrokeRec strokeRec(strokePaint); |
| 223 | GrAAType aaType = GrAAType::kNone; |
| 224 | if (fsaaType == GrFSAAType::kUnifiedMSAA) { |
| 225 | aaType = random->nextBool() ? GrAAType::kMSAA : GrAAType::kNone; |
| 226 | } |
| 227 | return NonAAStrokeRectOp::Make(std::move(paint), viewMatrix, rect, strokeRec, aaType); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | #endif |