blob: b74cd502c83109a90447a9a0ad1022c9e09907fd [file] [log] [blame]
joshualitt3566d442015-09-18 07:12:55 -07001/*
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
Brian Salomon6a639042016-12-14 11:08:17 -05008#include "GrNonAAStrokeRectOp.h"
joshualitt3566d442015-09-18 07:12:55 -07009
Brian Salomondad29232016-12-01 16:40:24 -050010#include "GrBatchTest.h"
joshualitt3566d442015-09-18 07:12:55 -070011#include "GrColor.h"
12#include "GrDefaultGeoProcFactory.h"
Brian Salomondad29232016-12-01 16:40:24 -050013#include "GrMeshDrawOp.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050014#include "GrOpFlushState.h"
joshualitt3566d442015-09-18 07:12:55 -070015#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 */
22static 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
Brian Salomon6a639042016-12-14 11:08:17 -050026 // SkASSERT(rad < rect.width() / 2 && rad < rect.height() / 2);
joshualitt3566d442015-09-18 07:12:55 -070027
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
bsalomon28ef3962016-07-06 18:56:04 -070040// Allow all hairlines and all miters, so long as the miter limit doesn't produce beveled corners.
41inline 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 Salomon6a639042016-12-14 11:08:17 -050048class NonAAStrokeRectOp final : public GrMeshDrawOp {
joshualitt3566d442015-09-18 07:12:55 -070049public:
Brian Salomon25a88092016-12-01 09:36:50 -050050 DEFINE_OP_CLASS_ID
joshualitt3566d442015-09-18 07:12:55 -070051
bsalomon28ef3962016-07-06 18:56:04 -070052 const char* name() const override { return "NonAAStrokeRectBatch"; }
joshualitt3566d442015-09-18 07:12:55 -070053
Brian Salomon7c3e7182016-12-01 09:35:30 -050054 SkString dumpInfo() const override {
55 SkString string;
Brian Salomon6a639042016-12-14 11:08:17 -050056 string.appendf(
57 "Color: 0x%08x, Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
58 "StrokeWidth: %.2f\n",
59 fColor, fRect.fLeft, fRect.fTop, fRect.fRight, fRect.fBottom, fStrokeWidth);
Brian Salomon7c3e7182016-12-01 09:35:30 -050060 string.append(DumpPipelineInfo(*this->pipeline()));
61 string.append(INHERITED::dumpInfo());
62 return string;
63 }
64
halcanary9d524f22016-03-29 09:03:52 -070065 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -080066 GrInitInvariantOutput* coverage,
67 GrBatchToXPOverrides* overrides) const override {
joshualitt3566d442015-09-18 07:12:55 -070068 // When this is called on a batch, there is only one geometry bundle
bsalomon28ef3962016-07-06 18:56:04 -070069 color->setKnownFourComponents(fColor);
ethannicholasff210322015-11-24 12:10:10 -080070 coverage->setKnownSingleComponent(0xff);
joshualitt3566d442015-09-18 07:12:55 -070071 }
72
Brian Salomon6a639042016-12-14 11:08:17 -050073 static sk_sp<GrDrawOp> Make(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect,
74 const SkStrokeRec& stroke, bool snapToPixelCenters) {
bsalomon28ef3962016-07-06 18:56:04 -070075 if (!allowed_stroke(stroke)) {
76 return nullptr;
77 }
Brian Salomon6a639042016-12-14 11:08:17 -050078 NonAAStrokeRectOp* op = new NonAAStrokeRectOp();
79 op->fColor = color;
80 op->fViewMatrix = viewMatrix;
81 op->fRect = rect;
joshualitt144c3c82015-11-30 12:30:13 -080082 // Sort the rect for hairlines
Brian Salomon6a639042016-12-14 11:08:17 -050083 op->fRect.sort();
84 op->fStrokeWidth = stroke.getWidth();
joshualittaa37a962015-09-18 13:03:25 -070085
Brian Salomon6a639042016-12-14 11:08:17 -050086 SkScalar rad = SkScalarHalf(op->fStrokeWidth);
bsalomon88cf17d2016-07-08 06:40:56 -070087 SkRect bounds = rect;
88 bounds.outset(rad, rad);
joshualittaa37a962015-09-18 13:03:25 -070089
90 // If our caller snaps to pixel centers then we have to round out the bounds
91 if (snapToPixelCenters) {
bsalomon88cf17d2016-07-08 06:40:56 -070092 viewMatrix.mapRect(&bounds);
egdaniel7e8cc212016-07-06 14:38:34 -070093 // 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.
bsalomon88cf17d2016-07-08 06:40:56 -070096 bounds.set(SkScalarFloorToScalar(bounds.fLeft),
97 SkScalarFloorToScalar(bounds.fTop),
98 SkScalarFloorToScalar(bounds.fRight),
99 SkScalarFloorToScalar(bounds.fBottom));
100 bounds.offset(0.5f, 0.5f);
Brian Salomon6a639042016-12-14 11:08:17 -0500101 op->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
bsalomon88cf17d2016-07-08 06:40:56 -0700102 } else {
Brian Salomon6a639042016-12-14 11:08:17 -0500103 op->setTransformedBounds(bounds, op->fViewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
joshualittaa37a962015-09-18 13:03:25 -0700104 }
Brian Salomon6a639042016-12-14 11:08:17 -0500105 return sk_sp<GrDrawOp>(op);
joshualittaa37a962015-09-18 13:03:25 -0700106 }
107
bsalomon28ef3962016-07-06 18:56:04 -0700108private:
Brian Salomon6a639042016-12-14 11:08:17 -0500109 NonAAStrokeRectOp() : INHERITED(ClassID()) {}
bsalomon28ef3962016-07-06 18:56:04 -0700110
joshualitt144c3c82015-11-30 12:30:13 -0800111 void onPrepareDraws(Target* target) const override {
bungeman06ca8ec2016-06-09 08:01:03 -0700112 sk_sp<GrGeometryProcessor> gp;
joshualitt3566d442015-09-18 07:12:55 -0700113 {
114 using namespace GrDefaultGeoProcFactory;
bsalomon28ef3962016-07-06 18:56:04 -0700115 Color color(fColor);
116 Coverage coverage(fOverrides.readsCoverage() ? Coverage::kSolid_Type
117 : Coverage::kNone_Type);
Brian Salomon6a639042016-12-14 11:08:17 -0500118 LocalCoords localCoords(fOverrides.readsLocalCoords() ? LocalCoords::kUsePosition_Type
119 : LocalCoords::kUnused_Type);
bsalomon28ef3962016-07-06 18:56:04 -0700120 gp = GrDefaultGeoProcFactory::Make(color, coverage, localCoords, fViewMatrix);
joshualitt3566d442015-09-18 07:12:55 -0700121 }
122
joshualitt3566d442015-09-18 07:12:55 -0700123 size_t vertexStride = gp->getVertexStride();
124
125 SkASSERT(vertexStride == sizeof(GrDefaultGeoProcFactory::PositionAttr));
126
joshualitt3566d442015-09-18 07:12:55 -0700127 int vertexCount = kVertsPerHairlineRect;
bsalomon28ef3962016-07-06 18:56:04 -0700128 if (fStrokeWidth > 0) {
joshualitt3566d442015-09-18 07:12:55 -0700129 vertexCount = kVertsPerStrokeRect;
130 }
131
cdalton397536c2016-03-25 12:15:03 -0700132 const GrBuffer* vertexBuffer;
joshualitt3566d442015-09-18 07:12:55 -0700133 int firstVertex;
134
Brian Salomon6a639042016-12-14 11:08:17 -0500135 void* verts =
136 target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex);
joshualitt3566d442015-09-18 07:12:55 -0700137
138 if (!verts) {
139 SkDebugf("Could not allocate vertices\n");
140 return;
141 }
142
143 SkPoint* vertex = reinterpret_cast<SkPoint*>(verts);
144
145 GrPrimitiveType primType;
bsalomon28ef3962016-07-06 18:56:04 -0700146 if (fStrokeWidth > 0) {
joshualitt3566d442015-09-18 07:12:55 -0700147 primType = kTriangleStrip_GrPrimitiveType;
bsalomon28ef3962016-07-06 18:56:04 -0700148 init_stroke_rect_strip(vertex, fRect, fStrokeWidth);
joshualitt3566d442015-09-18 07:12:55 -0700149 } else {
150 // hairline
151 primType = kLineStrip_GrPrimitiveType;
bsalomon28ef3962016-07-06 18:56:04 -0700152 vertex[0].set(fRect.fLeft, fRect.fTop);
153 vertex[1].set(fRect.fRight, fRect.fTop);
154 vertex[2].set(fRect.fRight, fRect.fBottom);
155 vertex[3].set(fRect.fLeft, fRect.fBottom);
156 vertex[4].set(fRect.fLeft, fRect.fTop);
joshualitt3566d442015-09-18 07:12:55 -0700157 }
158
egdaniel0e1853c2016-03-17 11:35:45 -0700159 GrMesh mesh;
160 mesh.init(primType, vertexBuffer, firstVertex, vertexCount);
bungeman06ca8ec2016-06-09 08:01:03 -0700161 target->draw(gp.get(), mesh);
joshualitt3566d442015-09-18 07:12:55 -0700162 }
163
ethannicholasff210322015-11-24 12:10:10 -0800164 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomon28ef3962016-07-06 18:56:04 -0700165 overrides.getOverrideColorIfSet(&fColor);
166 fOverrides = overrides;
joshualitt3566d442015-09-18 07:12:55 -0700167 }
168
Brian Salomon25a88092016-12-01 09:36:50 -0500169 bool onCombineIfPossible(GrOp* t, const GrCaps&) override {
joshualitt3566d442015-09-18 07:12:55 -0700170 // NonAA stroke rects cannot batch right now
171 // TODO make these batchable
172 return false;
173 }
174
bsalomon28ef3962016-07-06 18:56:04 -0700175 GrColor fColor;
176 SkMatrix fViewMatrix;
177 SkRect fRect;
178 SkScalar fStrokeWidth;
179
180 GrXPOverridesForBatch fOverrides;
joshualitt3566d442015-09-18 07:12:55 -0700181
182 const static int kVertsPerHairlineRect = 5;
183 const static int kVertsPerStrokeRect = 10;
184
Brian Salomondad29232016-12-01 16:40:24 -0500185 typedef GrMeshDrawOp INHERITED;
joshualitt3566d442015-09-18 07:12:55 -0700186};
187
Brian Salomon6a639042016-12-14 11:08:17 -0500188namespace GrNonAAStrokeRectOp {
joshualitt3566d442015-09-18 07:12:55 -0700189
Brian Salomon6a639042016-12-14 11:08:17 -0500190sk_sp<GrDrawOp> Make(GrColor color,
191 const SkMatrix& viewMatrix,
192 const SkRect& rect,
193 const SkStrokeRec& stroke,
194 bool snapToPixelCenters) {
195 return NonAAStrokeRectOp::Make(color, viewMatrix, rect, stroke, snapToPixelCenters);
joshualittaa37a962015-09-18 13:03:25 -0700196}
bsalomon28ef3962016-07-06 18:56:04 -0700197}
joshualitt3566d442015-09-18 07:12:55 -0700198
199#ifdef GR_TEST_UTILS
200
Brian Salomon6a639042016-12-14 11:08:17 -0500201DRAW_BATCH_TEST_DEFINE(NonAAStrokeRectOp) {
joshualitt3566d442015-09-18 07:12:55 -0700202 SkMatrix viewMatrix = GrTest::TestMatrix(random);
203 GrColor color = GrRandomColor(random);
204 SkRect rect = GrTest::TestRect(random);
bsalomona7d85ba2016-07-06 11:54:59 -0700205 SkScalar strokeWidth = random->nextBool() ? 0.0f : 2.0f;
206 SkPaint paint;
207 paint.setStrokeWidth(strokeWidth);
208 paint.setStyle(SkPaint::kStroke_Style);
209 paint.setStrokeJoin(SkPaint::kMiter_Join);
210 SkStrokeRec strokeRec(paint);
Brian Salomon6a639042016-12-14 11:08:17 -0500211 return GrNonAAStrokeRectOp::Make(color, viewMatrix, rect, strokeRec, random->nextBool())
212 .release();
joshualitt3566d442015-09-18 07:12:55 -0700213}
214
215#endif