Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(127)

Side by Side Diff: src/gpu/batches/GrNonAAStrokeRectBatch.cpp

Issue 1353683003: Move StrokeRectBatches into .cpp files (Closed) Base URL: https://skia.googlesource.com/skia.git@statics
Patch Set: tweaks Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
10 #include "GrBatchTest.h"
11 #include "GrBatchFlushState.h"
12 #include "GrColor.h"
13 #include "GrDefaultGeoProcFactory.h"
14 #include "GrVertexBatch.h"
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, SkScal ar width) {
23 const SkScalar rad = SkScalarHalf(width);
24 // TODO we should be able to enable this assert, but we'd have to filter the se 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
40 class NonAAStrokeRectBatch : public GrVertexBatch {
41 public:
42 DEFINE_BATCH_CLASS_ID
43
44 struct Geometry {
45 SkMatrix fViewMatrix;
46 SkRect fRect;
47 SkScalar fStrokeWidth;
48 GrColor fColor;
49 };
50
51 static GrDrawBatch* Create(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect,
52 SkScalar strokeWidth, bool snapToPixelCenters) {
53 return new NonAAStrokeRectBatch(color, viewMatrix, rect, strokeWidth, sn apToPixelCenters);
54 }
55
56 const char* name() const override { return "GrStrokeRectBatch"; }
57
58 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
59 // When this is called on a batch, there is only one geometry bundle
60 out->setKnownFourComponents(fGeoData[0].fColor);
61 }
62
63 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
64 out->setKnownSingleComponent(0xff);
65 }
66
67 private:
68 void onPrepareDraws(Target* target) override {
69 SkAutoTUnref<const GrGeometryProcessor> gp;
70 {
71 using namespace GrDefaultGeoProcFactory;
72 Color color(this->color());
73 Coverage coverage(this->coverageIgnored() ? Coverage::kSolid_Type :
74 Coverage::kNone_Type);
75 LocalCoords localCoords(this->usesLocalCoords() ? LocalCoords::kUseP osition_Type :
76 LocalCoords::kUn used_Type);
77 gp.reset(GrDefaultGeoProcFactory::Create(color, coverage, localCoord s,
78 this->viewMatrix()));
79 }
80
81 target->initDraw(gp, this->pipeline());
82
83 size_t vertexStride = gp->getVertexStride();
84
85 SkASSERT(vertexStride == sizeof(GrDefaultGeoProcFactory::PositionAttr));
86
87 Geometry& args = fGeoData[0];
88
89 int vertexCount = kVertsPerHairlineRect;
90 if (args.fStrokeWidth > 0) {
91 vertexCount = kVertsPerStrokeRect;
92 }
93
94 const GrVertexBuffer* vertexBuffer;
95 int firstVertex;
96
97 void* verts = target->makeVertexSpace(vertexStride, vertexCount, &vertex Buffer,
98 &firstVertex);
99
100 if (!verts) {
101 SkDebugf("Could not allocate vertices\n");
102 return;
103 }
104
105 SkPoint* vertex = reinterpret_cast<SkPoint*>(verts);
106
107 GrPrimitiveType primType;
108
109 if (args.fStrokeWidth > 0) {;
110 primType = kTriangleStrip_GrPrimitiveType;
111 args.fRect.sort();
112 init_stroke_rect_strip(vertex, args.fRect, args.fStrokeWidth);
113 } else {
114 // hairline
115 primType = kLineStrip_GrPrimitiveType;
116 vertex[0].set(args.fRect.fLeft, args.fRect.fTop);
117 vertex[1].set(args.fRect.fRight, args.fRect.fTop);
118 vertex[2].set(args.fRect.fRight, args.fRect.fBottom);
119 vertex[3].set(args.fRect.fLeft, args.fRect.fBottom);
120 vertex[4].set(args.fRect.fLeft, args.fRect.fTop);
121 }
122
123 GrVertices vertices;
124 vertices.init(primType, vertexBuffer, firstVertex, vertexCount);
125 target->draw(vertices);
126 }
127 void initBatchTracker(const GrPipelineOptimizations& opt) override {
128 // Handle any color overrides
129 if (!opt.readsColor()) {
130 fGeoData[0].fColor = GrColor_ILLEGAL;
131 }
132 opt.getOverrideColorIfSet(&fGeoData[0].fColor);
133
134 // setup batch properties
135 fBatch.fColorIgnored = !opt.readsColor();
136 fBatch.fColor = fGeoData[0].fColor;
137 fBatch.fUsesLocalCoords = opt.readsLocalCoords();
138 fBatch.fCoverageIgnored = !opt.readsCoverage();
139 }
140
141 NonAAStrokeRectBatch(GrColor color, const SkMatrix& viewMatrix, const SkRect & rect,
142 SkScalar strokeWidth, bool snapToPixelCenters)
143 : INHERITED(ClassID()) {
144 Geometry& geometry = fGeoData.push_back();
145 geometry.fViewMatrix = viewMatrix;
146 geometry.fRect = rect;
147 geometry.fStrokeWidth = strokeWidth;
148 geometry.fColor = color;
149
150 fBatch.fHairline = geometry.fStrokeWidth == 0;
151
152 fGeoData.push_back(geometry);
153
154 // setup bounds
155 fBounds = geometry.fRect;
156 SkScalar rad = SkScalarHalf(geometry.fStrokeWidth);
157 fBounds.outset(rad, rad);
158 geometry.fViewMatrix.mapRect(&fBounds);
159
160 // If our caller snaps to pixel centers then we have to round out the bo unds
161 if (snapToPixelCenters) {
162 fBounds.roundOut();
163 }
164 }
165
166 GrColor color() const { return fBatch.fColor; }
167 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
168 bool colorIgnored() const { return fBatch.fColorIgnored; }
169 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
170 bool hairline() const { return fBatch.fHairline; }
171 bool coverageIgnored() const { return fBatch.fCoverageIgnored; }
172
173 bool onCombineIfPossible(GrBatch* t, const GrCaps&) override {
174 // if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *t->pi peline(),
175 // t->bounds(), caps)) {
176 // return false;
177 // }
178 // GrStrokeRectBatch* that = t->cast<StrokeRectBatch>();
179
180 // NonAA stroke rects cannot batch right now
181 // TODO make these batchable
182 return false;
183 }
184
185 struct BatchTracker {
186 GrColor fColor;
187 bool fUsesLocalCoords;
188 bool fColorIgnored;
189 bool fCoverageIgnored;
190 bool fHairline;
191 };
192
193 const static int kVertsPerHairlineRect = 5;
194 const static int kVertsPerStrokeRect = 10;
195
196 BatchTracker fBatch;
197 SkSTArray<1, Geometry, true> fGeoData;
198
199 typedef GrVertexBatch INHERITED;
200 };
201
202 namespace GrNonAAStrokeRectBatch {
203
204 GrDrawBatch* Create(GrColor color,
205 const SkMatrix& viewMatrix,
206 const SkRect& rect,
207 SkScalar strokeWidth,
208 bool snapToPixelCenters) {
209 return NonAAStrokeRectBatch::Create(color, viewMatrix, rect, strokeWidth, sn apToPixelCenters);
210 }
211
212 };
213
214 #ifdef GR_TEST_UTILS
215
216 DRAW_BATCH_TEST_DEFINE(NonAAStrokeRectBatch) {
217 SkMatrix viewMatrix = GrTest::TestMatrix(random);
218 GrColor color = GrRandomColor(random);
219 SkRect rect = GrTest::TestRect(random);
220 SkScalar strokeWidth = random->nextBool() ? 0.0f : 1.0f;
221
222 return NonAAStrokeRectBatch::Create(color, viewMatrix, rect, strokeWidth, ra ndom->nextBool());
223 }
224
225 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698