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 | |
| 8 | #include "GrNonAAStrokeRectBatch.h" |
| 9 | |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 10 | #include "GrBatchFlushState.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame^] | 11 | #include "GrBatchTest.h" |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 12 | #include "GrColor.h" |
| 13 | #include "GrDefaultGeoProcFactory.h" |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame^] | 14 | #include "GrMeshDrawOp.h" |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 15 | #include "SkRandom.h" |
| 16 | |
| 17 | /* create a triangle strip that strokes the specified rect. There are 8 |
| 18 | unique vertices, but we repeat the last 2 to close up. Alternatively we |
| 19 | could use an indices array, and then only send 8 verts, but not sure that |
| 20 | would be faster. |
| 21 | */ |
| 22 | static void init_stroke_rect_strip(SkPoint verts[10], const SkRect& rect, SkScalar width) { |
| 23 | const SkScalar rad = SkScalarHalf(width); |
| 24 | // TODO we should be able to enable this assert, but we'd have to filter these draws |
| 25 | // this is a bug |
| 26 | //SkASSERT(rad < rect.width() / 2 && rad < rect.height() / 2); |
| 27 | |
| 28 | verts[0].set(rect.fLeft + rad, rect.fTop + rad); |
| 29 | verts[1].set(rect.fLeft - rad, rect.fTop - rad); |
| 30 | verts[2].set(rect.fRight - rad, rect.fTop + rad); |
| 31 | verts[3].set(rect.fRight + rad, rect.fTop - rad); |
| 32 | verts[4].set(rect.fRight - rad, rect.fBottom - rad); |
| 33 | verts[5].set(rect.fRight + rad, rect.fBottom + rad); |
| 34 | verts[6].set(rect.fLeft + rad, rect.fBottom - rad); |
| 35 | verts[7].set(rect.fLeft - rad, rect.fBottom + rad); |
| 36 | verts[8] = verts[0]; |
| 37 | verts[9] = verts[1]; |
| 38 | } |
| 39 | |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 40 | // Allow all hairlines and all miters, so long as the miter limit doesn't produce beveled corners. |
| 41 | inline static bool allowed_stroke(const SkStrokeRec& stroke) { |
| 42 | SkASSERT(stroke.getStyle() == SkStrokeRec::kStroke_Style || |
| 43 | stroke.getStyle() == SkStrokeRec::kHairline_Style); |
| 44 | return !stroke.getWidth() || |
| 45 | (stroke.getJoin() == SkPaint::kMiter_Join && stroke.getMiter() > SK_ScalarSqrt2); |
| 46 | } |
| 47 | |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame^] | 48 | class NonAAStrokeRectBatch : public GrMeshDrawOp { |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 49 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 50 | DEFINE_OP_CLASS_ID |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 51 | |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 52 | const char* name() const override { return "NonAAStrokeRectBatch"; } |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 53 | |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 54 | SkString dumpInfo() const override { |
| 55 | SkString string; |
| 56 | string.appendf("Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], " |
| 57 | "StrokeWidth: %.2f\n", |
| 58 | fColor, fRect.fLeft, fRect.fTop, fRect.fRight, fRect.fBottom, |
| 59 | fStrokeWidth); |
| 60 | string.append(DumpPipelineInfo(*this->pipeline())); |
| 61 | string.append(INHERITED::dumpInfo()); |
| 62 | return string; |
| 63 | } |
| 64 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 65 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 66 | GrInitInvariantOutput* coverage, |
| 67 | GrBatchToXPOverrides* overrides) const override { |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 68 | // When this is called on a batch, there is only one geometry bundle |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 69 | color->setKnownFourComponents(fColor); |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 70 | coverage->setKnownSingleComponent(0xff); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 73 | static GrDrawOp* Create(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect, |
| 74 | const SkStrokeRec& stroke, bool snapToPixelCenters) { |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 75 | if (!allowed_stroke(stroke)) { |
| 76 | return nullptr; |
| 77 | } |
| 78 | NonAAStrokeRectBatch* batch = new NonAAStrokeRectBatch(); |
| 79 | batch->fColor = color; |
| 80 | batch->fViewMatrix = viewMatrix; |
| 81 | batch->fRect = rect; |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 82 | // Sort the rect for hairlines |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 83 | batch->fRect.sort(); |
| 84 | batch->fStrokeWidth = stroke.getWidth(); |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 85 | |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 86 | SkScalar rad = SkScalarHalf(batch->fStrokeWidth); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 87 | SkRect bounds = rect; |
| 88 | bounds.outset(rad, rad); |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 89 | |
| 90 | // If our caller snaps to pixel centers then we have to round out the bounds |
| 91 | if (snapToPixelCenters) { |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 92 | viewMatrix.mapRect(&bounds); |
egdaniel | 7e8cc21 | 2016-07-06 14:38:34 -0700 | [diff] [blame] | 93 | // We want to be consistent with how we snap non-aa lines. To match what we do in |
| 94 | // GrGLSLVertexShaderBuilder, we first floor all the vertex values and then add half a |
| 95 | // pixel to force us to pixel centers. |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 96 | bounds.set(SkScalarFloorToScalar(bounds.fLeft), |
| 97 | SkScalarFloorToScalar(bounds.fTop), |
| 98 | SkScalarFloorToScalar(bounds.fRight), |
| 99 | SkScalarFloorToScalar(bounds.fBottom)); |
| 100 | bounds.offset(0.5f, 0.5f); |
| 101 | batch->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo); |
| 102 | } else { |
| 103 | batch->setTransformedBounds(bounds, batch->fViewMatrix, HasAABloat ::kNo, |
| 104 | IsZeroArea::kNo); |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 105 | } |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 106 | return batch; |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 107 | } |
| 108 | |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 109 | private: |
| 110 | NonAAStrokeRectBatch() : INHERITED(ClassID()) {} |
| 111 | |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 112 | void onPrepareDraws(Target* target) const override { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 113 | sk_sp<GrGeometryProcessor> gp; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 114 | { |
| 115 | using namespace GrDefaultGeoProcFactory; |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 116 | Color color(fColor); |
| 117 | Coverage coverage(fOverrides.readsCoverage() ? Coverage::kSolid_Type |
| 118 | : Coverage::kNone_Type); |
| 119 | LocalCoords localCoords(fOverrides.readsLocalCoords() ? LocalCoords::kUsePosition_Type : |
| 120 | LocalCoords::kUnused_Type); |
| 121 | gp = GrDefaultGeoProcFactory::Make(color, coverage, localCoords, fViewMatrix); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 122 | } |
| 123 | |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 124 | size_t vertexStride = gp->getVertexStride(); |
| 125 | |
| 126 | SkASSERT(vertexStride == sizeof(GrDefaultGeoProcFactory::PositionAttr)); |
| 127 | |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 128 | int vertexCount = kVertsPerHairlineRect; |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 129 | if (fStrokeWidth > 0) { |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 130 | vertexCount = kVertsPerStrokeRect; |
| 131 | } |
| 132 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 133 | const GrBuffer* vertexBuffer; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 134 | int firstVertex; |
| 135 | |
| 136 | void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, |
| 137 | &firstVertex); |
| 138 | |
| 139 | if (!verts) { |
| 140 | SkDebugf("Could not allocate vertices\n"); |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | SkPoint* vertex = reinterpret_cast<SkPoint*>(verts); |
| 145 | |
| 146 | GrPrimitiveType primType; |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 147 | if (fStrokeWidth > 0) { |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 148 | primType = kTriangleStrip_GrPrimitiveType; |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 149 | init_stroke_rect_strip(vertex, fRect, fStrokeWidth); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 150 | } else { |
| 151 | // hairline |
| 152 | primType = kLineStrip_GrPrimitiveType; |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 153 | vertex[0].set(fRect.fLeft, fRect.fTop); |
| 154 | vertex[1].set(fRect.fRight, fRect.fTop); |
| 155 | vertex[2].set(fRect.fRight, fRect.fBottom); |
| 156 | vertex[3].set(fRect.fLeft, fRect.fBottom); |
| 157 | vertex[4].set(fRect.fLeft, fRect.fTop); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 158 | } |
| 159 | |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 160 | GrMesh mesh; |
| 161 | mesh.init(primType, vertexBuffer, firstVertex, vertexCount); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 162 | target->draw(gp.get(), mesh); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 163 | } |
| 164 | |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 165 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 166 | overrides.getOverrideColorIfSet(&fColor); |
| 167 | fOverrides = overrides; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 170 | bool onCombineIfPossible(GrOp* t, const GrCaps&) override { |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 171 | // NonAA stroke rects cannot batch right now |
| 172 | // TODO make these batchable |
| 173 | return false; |
| 174 | } |
| 175 | |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 176 | GrColor fColor; |
| 177 | SkMatrix fViewMatrix; |
| 178 | SkRect fRect; |
| 179 | SkScalar fStrokeWidth; |
| 180 | |
| 181 | GrXPOverridesForBatch fOverrides; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 182 | |
| 183 | const static int kVertsPerHairlineRect = 5; |
| 184 | const static int kVertsPerStrokeRect = 10; |
| 185 | |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame^] | 186 | typedef GrMeshDrawOp INHERITED; |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | namespace GrNonAAStrokeRectBatch { |
| 190 | |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 191 | GrDrawOp* Create(GrColor color, |
| 192 | const SkMatrix& viewMatrix, |
| 193 | const SkRect& rect, |
| 194 | const SkStrokeRec& stroke, |
| 195 | bool snapToPixelCenters) { |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 196 | return NonAAStrokeRectBatch::Create(color, viewMatrix, rect, stroke, snapToPixelCenters); |
joshualitt | aa37a96 | 2015-09-18 13:03:25 -0700 | [diff] [blame] | 197 | } |
| 198 | |
bsalomon | 28ef396 | 2016-07-06 18:56:04 -0700 | [diff] [blame] | 199 | } |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 200 | |
| 201 | #ifdef GR_TEST_UTILS |
| 202 | |
| 203 | DRAW_BATCH_TEST_DEFINE(NonAAStrokeRectBatch) { |
| 204 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
| 205 | GrColor color = GrRandomColor(random); |
| 206 | SkRect rect = GrTest::TestRect(random); |
bsalomon | a7d85ba | 2016-07-06 11:54:59 -0700 | [diff] [blame] | 207 | SkScalar strokeWidth = random->nextBool() ? 0.0f : 2.0f; |
| 208 | SkPaint paint; |
| 209 | paint.setStrokeWidth(strokeWidth); |
| 210 | paint.setStyle(SkPaint::kStroke_Style); |
| 211 | paint.setStrokeJoin(SkPaint::kMiter_Join); |
| 212 | SkStrokeRec strokeRec(paint); |
| 213 | return GrNonAAStrokeRectBatch::Create(color, viewMatrix, rect, strokeRec, random->nextBool()); |
joshualitt | 3566d44 | 2015-09-18 07:12:55 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | #endif |