blob: 4649edf047118a03fc521fc7ec2ba5ddcdf30c52 [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
8#include "GrNonAAStrokeRectBatch.h"
9
joshualitt3566d442015-09-18 07:12:55 -070010#include "GrBatchFlushState.h"
Brian Salomondad29232016-12-01 16:40:24 -050011#include "GrBatchTest.h"
joshualitt3566d442015-09-18 07:12:55 -070012#include "GrColor.h"
13#include "GrDefaultGeoProcFactory.h"
Brian Salomondad29232016-12-01 16:40:24 -050014#include "GrMeshDrawOp.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
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
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 Salomondad29232016-12-01 16:40:24 -050048class NonAAStrokeRectBatch : 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;
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
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 Salomon9afd3712016-12-01 10:59:09 -050073 static GrDrawOp* Create(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 }
78 NonAAStrokeRectBatch* batch = new NonAAStrokeRectBatch();
79 batch->fColor = color;
80 batch->fViewMatrix = viewMatrix;
81 batch->fRect = rect;
joshualitt144c3c82015-11-30 12:30:13 -080082 // Sort the rect for hairlines
bsalomon28ef3962016-07-06 18:56:04 -070083 batch->fRect.sort();
84 batch->fStrokeWidth = stroke.getWidth();
joshualittaa37a962015-09-18 13:03:25 -070085
bsalomon28ef3962016-07-06 18:56:04 -070086 SkScalar rad = SkScalarHalf(batch->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);
101 batch->setBounds(bounds, HasAABloat::kNo, IsZeroArea::kNo);
102 } else {
103 batch->setTransformedBounds(bounds, batch->fViewMatrix, HasAABloat ::kNo,
104 IsZeroArea::kNo);
joshualittaa37a962015-09-18 13:03:25 -0700105 }
bsalomon28ef3962016-07-06 18:56:04 -0700106 return batch;
joshualittaa37a962015-09-18 13:03:25 -0700107 }
108
bsalomon28ef3962016-07-06 18:56:04 -0700109private:
110 NonAAStrokeRectBatch() : INHERITED(ClassID()) {}
111
joshualitt144c3c82015-11-30 12:30:13 -0800112 void onPrepareDraws(Target* target) const override {
bungeman06ca8ec2016-06-09 08:01:03 -0700113 sk_sp<GrGeometryProcessor> gp;
joshualitt3566d442015-09-18 07:12:55 -0700114 {
115 using namespace GrDefaultGeoProcFactory;
bsalomon28ef3962016-07-06 18:56:04 -0700116 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);
joshualitt3566d442015-09-18 07:12:55 -0700122 }
123
joshualitt3566d442015-09-18 07:12:55 -0700124 size_t vertexStride = gp->getVertexStride();
125
126 SkASSERT(vertexStride == sizeof(GrDefaultGeoProcFactory::PositionAttr));
127
joshualitt3566d442015-09-18 07:12:55 -0700128 int vertexCount = kVertsPerHairlineRect;
bsalomon28ef3962016-07-06 18:56:04 -0700129 if (fStrokeWidth > 0) {
joshualitt3566d442015-09-18 07:12:55 -0700130 vertexCount = kVertsPerStrokeRect;
131 }
132
cdalton397536c2016-03-25 12:15:03 -0700133 const GrBuffer* vertexBuffer;
joshualitt3566d442015-09-18 07:12:55 -0700134 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;
bsalomon28ef3962016-07-06 18:56:04 -0700147 if (fStrokeWidth > 0) {
joshualitt3566d442015-09-18 07:12:55 -0700148 primType = kTriangleStrip_GrPrimitiveType;
bsalomon28ef3962016-07-06 18:56:04 -0700149 init_stroke_rect_strip(vertex, fRect, fStrokeWidth);
joshualitt3566d442015-09-18 07:12:55 -0700150 } else {
151 // hairline
152 primType = kLineStrip_GrPrimitiveType;
bsalomon28ef3962016-07-06 18:56:04 -0700153 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);
joshualitt3566d442015-09-18 07:12:55 -0700158 }
159
egdaniel0e1853c2016-03-17 11:35:45 -0700160 GrMesh mesh;
161 mesh.init(primType, vertexBuffer, firstVertex, vertexCount);
bungeman06ca8ec2016-06-09 08:01:03 -0700162 target->draw(gp.get(), mesh);
joshualitt3566d442015-09-18 07:12:55 -0700163 }
164
ethannicholasff210322015-11-24 12:10:10 -0800165 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
bsalomon28ef3962016-07-06 18:56:04 -0700166 overrides.getOverrideColorIfSet(&fColor);
167 fOverrides = overrides;
joshualitt3566d442015-09-18 07:12:55 -0700168 }
169
Brian Salomon25a88092016-12-01 09:36:50 -0500170 bool onCombineIfPossible(GrOp* t, const GrCaps&) override {
joshualitt3566d442015-09-18 07:12:55 -0700171 // NonAA stroke rects cannot batch right now
172 // TODO make these batchable
173 return false;
174 }
175
bsalomon28ef3962016-07-06 18:56:04 -0700176 GrColor fColor;
177 SkMatrix fViewMatrix;
178 SkRect fRect;
179 SkScalar fStrokeWidth;
180
181 GrXPOverridesForBatch fOverrides;
joshualitt3566d442015-09-18 07:12:55 -0700182
183 const static int kVertsPerHairlineRect = 5;
184 const static int kVertsPerStrokeRect = 10;
185
Brian Salomondad29232016-12-01 16:40:24 -0500186 typedef GrMeshDrawOp INHERITED;
joshualitt3566d442015-09-18 07:12:55 -0700187};
188
189namespace GrNonAAStrokeRectBatch {
190
Brian Salomon9afd3712016-12-01 10:59:09 -0500191GrDrawOp* Create(GrColor color,
192 const SkMatrix& viewMatrix,
193 const SkRect& rect,
194 const SkStrokeRec& stroke,
195 bool snapToPixelCenters) {
bsalomon28ef3962016-07-06 18:56:04 -0700196 return NonAAStrokeRectBatch::Create(color, viewMatrix, rect, stroke, snapToPixelCenters);
joshualittaa37a962015-09-18 13:03:25 -0700197}
198
bsalomon28ef3962016-07-06 18:56:04 -0700199}
joshualitt3566d442015-09-18 07:12:55 -0700200
201#ifdef GR_TEST_UTILS
202
203DRAW_BATCH_TEST_DEFINE(NonAAStrokeRectBatch) {
204 SkMatrix viewMatrix = GrTest::TestMatrix(random);
205 GrColor color = GrRandomColor(random);
206 SkRect rect = GrTest::TestRect(random);
bsalomona7d85ba2016-07-06 11:54:59 -0700207 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());
joshualitt3566d442015-09-18 07:12:55 -0700214}
215
216#endif