OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrNonAAStrokeRectBatch.h" | 8 #include "GrNonAAStrokeRectBatch.h" |
9 | 9 |
10 #include "GrBatchTest.h" | 10 #include "GrBatchTest.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 public: | 41 public: |
42 DEFINE_BATCH_CLASS_ID | 42 DEFINE_BATCH_CLASS_ID |
43 | 43 |
44 struct Geometry { | 44 struct Geometry { |
45 SkMatrix fViewMatrix; | 45 SkMatrix fViewMatrix; |
46 SkRect fRect; | 46 SkRect fRect; |
47 SkScalar fStrokeWidth; | 47 SkScalar fStrokeWidth; |
48 GrColor fColor; | 48 GrColor fColor; |
49 }; | 49 }; |
50 | 50 |
51 static GrDrawBatch* Create(GrColor color, const SkMatrix& viewMatrix, const
SkRect& rect, | 51 static NonAAStrokeRectBatch* Create() { |
52 SkScalar strokeWidth, bool snapToPixelCenters) { | 52 return new NonAAStrokeRectBatch; |
53 return new NonAAStrokeRectBatch(color, viewMatrix, rect, strokeWidth, sn
apToPixelCenters); | |
54 } | 53 } |
55 | 54 |
56 const char* name() const override { return "GrStrokeRectBatch"; } | 55 const char* name() const override { return "GrStrokeRectBatch"; } |
57 | 56 |
58 void getInvariantOutputColor(GrInitInvariantOutput* out) const override { | 57 void getInvariantOutputColor(GrInitInvariantOutput* out) const override { |
59 // When this is called on a batch, there is only one geometry bundle | 58 // When this is called on a batch, there is only one geometry bundle |
60 out->setKnownFourComponents(fGeoData[0].fColor); | 59 out->setKnownFourComponents(fGeoData[0].fColor); |
61 } | 60 } |
62 | 61 |
63 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { | 62 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override { |
64 out->setKnownSingleComponent(0xff); | 63 out->setKnownSingleComponent(0xff); |
65 } | 64 } |
66 | 65 |
| 66 void append(GrColor color, const SkMatrix& viewMatrix, const SkRect& rect, |
| 67 SkScalar strokeWidth) { |
| 68 Geometry& geometry = fGeoData.push_back(); |
| 69 geometry.fViewMatrix = viewMatrix; |
| 70 geometry.fRect = rect; |
| 71 geometry.fStrokeWidth = strokeWidth; |
| 72 geometry.fColor = color; |
| 73 } |
| 74 |
| 75 void appendAndUpdateBounds(GrColor color, const SkMatrix& viewMatrix, const
SkRect& rect, |
| 76 SkScalar strokeWidth, bool snapToPixelCenters) { |
| 77 this->append(color, viewMatrix, rect, strokeWidth); |
| 78 |
| 79 SkRect bounds; |
| 80 this->setupBounds(&bounds, fGeoData.back(), snapToPixelCenters); |
| 81 this->joinBounds(bounds); |
| 82 } |
| 83 |
| 84 void init(bool snapToPixelCenters) { |
| 85 const Geometry& geo = fGeoData[0]; |
| 86 fBatch.fHairline = geo.fStrokeWidth == 0; |
| 87 |
| 88 // setup bounds |
| 89 this->setupBounds(&fBounds, geo, snapToPixelCenters); |
| 90 } |
| 91 |
67 private: | 92 private: |
| 93 void setupBounds(SkRect* bounds, const Geometry& geo, bool snapToPixelCenter
s) { |
| 94 *bounds = geo.fRect; |
| 95 SkScalar rad = SkScalarHalf(geo.fStrokeWidth); |
| 96 bounds->outset(rad, rad); |
| 97 geo.fViewMatrix.mapRect(&fBounds); |
| 98 |
| 99 // If our caller snaps to pixel centers then we have to round out the bo
unds |
| 100 if (snapToPixelCenters) { |
| 101 bounds->roundOut(); |
| 102 } |
| 103 } |
| 104 |
68 void onPrepareDraws(Target* target) override { | 105 void onPrepareDraws(Target* target) override { |
69 SkAutoTUnref<const GrGeometryProcessor> gp; | 106 SkAutoTUnref<const GrGeometryProcessor> gp; |
70 { | 107 { |
71 using namespace GrDefaultGeoProcFactory; | 108 using namespace GrDefaultGeoProcFactory; |
72 Color color(this->color()); | 109 Color color(this->color()); |
73 Coverage coverage(this->coverageIgnored() ? Coverage::kSolid_Type : | 110 Coverage coverage(this->coverageIgnored() ? Coverage::kSolid_Type : |
74 Coverage::kNone_Type); | 111 Coverage::kNone_Type); |
75 LocalCoords localCoords(this->usesLocalCoords() ? LocalCoords::kUseP
osition_Type : | 112 LocalCoords localCoords(this->usesLocalCoords() ? LocalCoords::kUseP
osition_Type : |
76 LocalCoords::kUnus
ed_Type); | 113 LocalCoords::kUnus
ed_Type); |
77 gp.reset(GrDefaultGeoProcFactory::Create(color, coverage, localCoord
s, | 114 gp.reset(GrDefaultGeoProcFactory::Create(color, coverage, localCoord
s, |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 } | 169 } |
133 opt.getOverrideColorIfSet(&fGeoData[0].fColor); | 170 opt.getOverrideColorIfSet(&fGeoData[0].fColor); |
134 | 171 |
135 // setup batch properties | 172 // setup batch properties |
136 fBatch.fColorIgnored = !opt.readsColor(); | 173 fBatch.fColorIgnored = !opt.readsColor(); |
137 fBatch.fColor = fGeoData[0].fColor; | 174 fBatch.fColor = fGeoData[0].fColor; |
138 fBatch.fUsesLocalCoords = opt.readsLocalCoords(); | 175 fBatch.fUsesLocalCoords = opt.readsLocalCoords(); |
139 fBatch.fCoverageIgnored = !opt.readsCoverage(); | 176 fBatch.fCoverageIgnored = !opt.readsCoverage(); |
140 } | 177 } |
141 | 178 |
142 NonAAStrokeRectBatch(GrColor color, const SkMatrix& viewMatrix, const SkRect
& rect, | 179 NonAAStrokeRectBatch() : INHERITED(ClassID()) {} |
143 SkScalar strokeWidth, bool snapToPixelCenters) | |
144 : INHERITED(ClassID()) { | |
145 Geometry& geometry = fGeoData.push_back(); | |
146 geometry.fViewMatrix = viewMatrix; | |
147 geometry.fRect = rect; | |
148 geometry.fStrokeWidth = strokeWidth; | |
149 geometry.fColor = color; | |
150 | |
151 fBatch.fHairline = geometry.fStrokeWidth == 0; | |
152 | |
153 fGeoData.push_back(geometry); | |
154 | |
155 // setup bounds | |
156 fBounds = geometry.fRect; | |
157 SkScalar rad = SkScalarHalf(geometry.fStrokeWidth); | |
158 fBounds.outset(rad, rad); | |
159 geometry.fViewMatrix.mapRect(&fBounds); | |
160 | |
161 // If our caller snaps to pixel centers then we have to round out the bo
unds | |
162 if (snapToPixelCenters) { | |
163 fBounds.roundOut(); | |
164 } | |
165 } | |
166 | 180 |
167 GrColor color() const { return fBatch.fColor; } | 181 GrColor color() const { return fBatch.fColor; } |
168 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } | 182 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
169 bool colorIgnored() const { return fBatch.fColorIgnored; } | 183 bool colorIgnored() const { return fBatch.fColorIgnored; } |
170 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } | 184 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
171 bool hairline() const { return fBatch.fHairline; } | 185 bool hairline() const { return fBatch.fHairline; } |
172 bool coverageIgnored() const { return fBatch.fCoverageIgnored; } | 186 bool coverageIgnored() const { return fBatch.fCoverageIgnored; } |
173 | 187 |
174 bool onCombineIfPossible(GrBatch* t, const GrCaps&) override { | 188 bool onCombineIfPossible(GrBatch* t, const GrCaps&) override { |
175 // if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *t->pi
peline(), | 189 // if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *t->pi
peline(), |
(...skipping 24 matching lines...) Expand all Loading... |
200 typedef GrVertexBatch INHERITED; | 214 typedef GrVertexBatch INHERITED; |
201 }; | 215 }; |
202 | 216 |
203 namespace GrNonAAStrokeRectBatch { | 217 namespace GrNonAAStrokeRectBatch { |
204 | 218 |
205 GrDrawBatch* Create(GrColor color, | 219 GrDrawBatch* Create(GrColor color, |
206 const SkMatrix& viewMatrix, | 220 const SkMatrix& viewMatrix, |
207 const SkRect& rect, | 221 const SkRect& rect, |
208 SkScalar strokeWidth, | 222 SkScalar strokeWidth, |
209 bool snapToPixelCenters) { | 223 bool snapToPixelCenters) { |
210 return NonAAStrokeRectBatch::Create(color, viewMatrix, rect, strokeWidth, sn
apToPixelCenters); | 224 NonAAStrokeRectBatch* batch = NonAAStrokeRectBatch::Create(); |
| 225 batch->append(color, viewMatrix, rect, strokeWidth); |
| 226 batch->init(snapToPixelCenters); |
| 227 return batch; |
| 228 } |
| 229 |
| 230 void Append(GrBatch* origBatch, |
| 231 GrColor color, |
| 232 const SkMatrix& viewMatrix, |
| 233 const SkRect& rect, |
| 234 SkScalar strokeWidth, |
| 235 bool snapToPixelCenters) { |
| 236 NonAAStrokeRectBatch* batch = origBatch->cast<NonAAStrokeRectBatch>(); |
| 237 batch->appendAndUpdateBounds(color, viewMatrix, rect, strokeWidth, snapToPix
elCenters); |
211 } | 238 } |
212 | 239 |
213 }; | 240 }; |
214 | 241 |
215 #ifdef GR_TEST_UTILS | 242 #ifdef GR_TEST_UTILS |
216 | 243 |
217 DRAW_BATCH_TEST_DEFINE(NonAAStrokeRectBatch) { | 244 DRAW_BATCH_TEST_DEFINE(NonAAStrokeRectBatch) { |
218 SkMatrix viewMatrix = GrTest::TestMatrix(random); | 245 SkMatrix viewMatrix = GrTest::TestMatrix(random); |
219 GrColor color = GrRandomColor(random); | 246 GrColor color = GrRandomColor(random); |
220 SkRect rect = GrTest::TestRect(random); | 247 SkRect rect = GrTest::TestRect(random); |
221 SkScalar strokeWidth = random->nextBool() ? 0.0f : 1.0f; | 248 SkScalar strokeWidth = random->nextBool() ? 0.0f : 1.0f; |
222 | 249 |
223 return NonAAStrokeRectBatch::Create(color, viewMatrix, rect, strokeWidth, ra
ndom->nextBool()); | 250 return GrNonAAStrokeRectBatch::Create(color, viewMatrix, rect, strokeWidth,
random->nextBool()); |
224 } | 251 } |
225 | 252 |
226 #endif | 253 #endif |
OLD | NEW |