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

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

Issue 1295773003: fill rect batch refactor into separate batches (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: myinor bug fix Created 5 years, 4 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
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 "GrBWFillRectBatch.h" 8 #include "GrBWFillRectBatch.h"
9 9
10 #include "GrBatchFlushState.h" 10 #include "GrBatchFlushState.h"
11 #include "GrColor.h" 11 #include "GrColor.h"
12 #include "GrDefaultGeoProcFactory.h" 12 #include "GrDefaultGeoProcFactory.h"
13 #include "GrPrimitiveProcessor.h" 13 #include "GrPrimitiveProcessor.h"
14 #include "GrResourceProvider.h"
15 #include "GrTInstanceBatch.h"
14 #include "GrQuad.h" 16 #include "GrQuad.h"
15 #include "GrVertexBatch.h" 17 #include "GrVertexBatch.h"
16 18
17 class GrBatchFlushState; 19 // Common functions
18 class SkMatrix; 20 class BWFillRectBatchBase {
19 struct SkRect; 21 public:
20 22 static const int kVertsPerInstance = 4;
21 class BWFillRectBatch : public GrVertexBatch { 23 static const int kIndicesPerInstance = 6;
24
25 static void InitInvariantOutputCoverage(GrInitInvariantOutput* out) {
26 out->setKnownSingleComponent(0xff);
27 }
28
29 static const GrIndexBuffer* GetIndexBuffer(GrResourceProvider* rp) {
30 return rp->refQuadIndexBuffer();
31 }
32
33 template <typename Geometry>
34 static void SetBounds(const Geometry& geo, SkRect* outBounds) {
35 geo.fViewMatrix.mapRect(outBounds, geo.fRect);
36 }
37 };
38
39 /** We always use per-vertex colors so that rects can be batched across color ch anges. Sometimes
40 we have explicit local coords and sometimes not. We *could* always provide explicit local
41 coords and just duplicate the positions when the caller hasn't provided a lo cal coord rect,
42 but we haven't seen a use case which frequently switches between local rect and no local
43 rect draws.
44
robertphillips 2015/08/21 17:20:56 color param ?
45 The color param is used to determine whether the opaque hint can be set on t he draw state.
46 The caller must populate the vertex colors itself.
47
48 The vertex attrib order is always pos, color, [local coords].
49 */
50 static const GrGeometryProcessor* create_gp(const SkMatrix& viewMatrix,
51 bool readsCoverage,
52 bool hasExplicitLocalCoords,
53 const SkMatrix* localMatrix) {
54 using namespace GrDefaultGeoProcFactory;
55 Color color(Color::kAttribute_Type);
56 Coverage coverage(readsCoverage ? Coverage::kSolid_Type : Coverage::kNone_Ty pe);
57
58 // if we have a local rect, then we apply the localMatrix directly to the lo calRect to
59 // generate vertex local coords
60 if (hasExplicitLocalCoords) {
61 LocalCoords localCoords(LocalCoords::kHasExplicit_Type);
62 return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, SkM atrix::I());
63 } else {
robertphillips 2015/08/21 17:20:56 localMatrix ? localMatrix : NULL ?
64 LocalCoords localCoords(LocalCoords::kUsePosition_Type, localMatrix ? lo calMatrix : NULL);
65 return GrDefaultGeoProcFactory::CreateForDeviceSpace(color, coverage, lo calCoords,
66 viewMatrix);
67 }
68 }
69
70 static void tesselate(intptr_t vertices,
71 size_t vertexStride,
72 GrColor color,
73 const SkMatrix& viewMatrix,
74 const SkRect& rect,
75 const SkRect* localRect,
76 const SkMatrix* localMatrix) {
77 SkPoint* positions = reinterpret_cast<SkPoint*>(vertices);
78
79 positions->setRectFan(rect.fLeft, rect.fTop,
80 rect.fRight, rect.fBottom, vertexStride);
81 viewMatrix.mapPointsWithStride(positions, vertexStride, BWFillRectBatchBase: :kVertsPerInstance);
82
83 // TODO we should only do this if local coords are being read
84 if (localRect) {
85 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor);
86 SkPoint* coords = reinterpret_cast<SkPoint*>(vertices + kLocalOffset);
87 coords->setRectFan(localRect->fLeft, localRect->fTop,
88 localRect->fRight, localRect->fBottom,
89 vertexStride);
90 if (localMatrix) {
91 localMatrix->mapPointsWithStride(coords, vertexStride,
92 BWFillRectBatchBase::kVertsPerInsta nce);
93 }
94 }
95
96 static const int kColorOffset = sizeof(SkPoint);
97 GrColor* vertColor = reinterpret_cast<GrColor*>(vertices + kColorOffset);
98 for (int j = 0; j < 4; ++j) {
99 *vertColor = color;
100 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride);
101 }
102 }
103
104 class BWFillRectBatchNoLocalMatrixImp : public BWFillRectBatchBase {
105 public:
106 struct Geometry {
107 SkMatrix fViewMatrix;
108 SkRect fRect;
109 GrColor fColor;
110 };
111
112 static const char* Name() { return "BWFillRectBatchNoLocalMatrix"; }
113
114 static bool CanCombine(const Geometry& mine, const Geometry& theirs,
115 const GrPipelineOptimizations& opts) {
116 // We apply the viewmatrix to the rect points on the cpu. However, if t he pipeline uses
117 // local coords then we won't be able to batch. We could actually uploa d the viewmatrix
118 // using vertex attributes in these cases, but haven't investigated that
119 return !opts.readsLocalCoords() || mine.fViewMatrix.cheapEqualTo(theirs. fViewMatrix);
120 }
121
122 static const GrGeometryProcessor* CreateGP(const Geometry& geo,
123 const GrPipelineOptimizations& op ts) {
124 const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCov erage(), false,
125 NULL);
126
127 SkASSERT(gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::Positi onColorAttr));
128 return gp;
129 }
130
131 static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry & geo,
132 const GrPipelineOptimizations& opts) {
133 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect , NULL, NULL);
134 }
135 };
136
137 class BWFillRectBatchLocalMatrixImp : public BWFillRectBatchBase {
138 public:
139 struct Geometry {
140 SkMatrix fViewMatrix;
141 SkMatrix fLocalMatrix;
142 SkRect fRect;
143 GrColor fColor;
144 };
145
146 static const char* Name() { return "BWFillRectBatchLocalMatrix"; }
147
148 static bool CanCombine(const Geometry& mine, const Geometry& theirs,
149 const GrPipelineOptimizations& opts) {
150 // if we read local coords then we have to have the same viewmatrix and localmatrix
151 return !opts.readsLocalCoords() ||
152 (mine.fViewMatrix.cheapEqualTo(theirs.fViewMatrix) &&
153 mine.fLocalMatrix.cheapEqualTo(theirs.fLocalMatrix));
154 }
155
156 static const GrGeometryProcessor* CreateGP(const Geometry& geo,
157 const GrPipelineOptimizations& op ts) {
158 const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCov erage(), false,
159 &geo.fLocalMatrix);
160
161 SkASSERT(gp->getVertexStride() == sizeof(GrDefaultGeoProcFactory::Positi onColorAttr));
162 return gp;
163 }
164
165 static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry & geo,
166 const GrPipelineOptimizations& opts) {
167 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect , NULL,
168 &geo.fLocalMatrix);
169 }
170 };
171
172 class BWFillRectBatchLocalRectImp : public BWFillRectBatchBase {
22 public: 173 public:
23 struct Geometry { 174 struct Geometry {
24 SkMatrix fViewMatrix; 175 SkMatrix fViewMatrix;
25 SkRect fRect; 176 SkRect fRect;
26 SkRect fLocalRect; 177 SkRect fLocalRect;
178 GrColor fColor;
179 };
180
181 static const char* Name() { return "BWFillRectBatchLocalRect"; }
182
183 static bool CanCombine(const Geometry& mine, const Geometry& theirs,
184 const GrPipelineOptimizations& opts) {
185 return true;
186 }
187
188 static const GrGeometryProcessor* CreateGP(const Geometry& geo,
189 const GrPipelineOptimizations& op ts) {
190 const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCov erage(), true,
191 NULL);
192
193 SkASSERT(gp->getVertexStride() ==
194 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr));
195 return gp;
196 }
197
198 static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry & geo,
199 const GrPipelineOptimizations& opts) {
200 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect , &geo.fLocalRect,
201 NULL);
202 }
203 };
204
205 class BWFillRectBatchLocalMatrixLocalRectImp : public BWFillRectBatchBase {
206 public:
207 struct Geometry {
208 SkMatrix fViewMatrix;
27 SkMatrix fLocalMatrix; 209 SkMatrix fLocalMatrix;
28 GrColor fColor; 210 SkRect fRect;
29 bool fHasLocalRect; 211 SkRect fLocalRect;
30 bool fHasLocalMatrix; 212 GrColor fColor;
31 }; 213 };
32 214
33 static GrDrawBatch* Create(const Geometry& geometry) { 215 static const char* Name() { return "BWFillRectBatchLocalMatrixLocalRect"; }
34 return SkNEW_ARGS(BWFillRectBatch, (geometry)); 216
35 } 217 static bool CanCombine(const Geometry& mine, const Geometry& theirs,
36 218 const GrPipelineOptimizations& opts) {
37 const char* name() const override { return "RectBatch"; }
38
39 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
40 // When this is called on a batch, there is only one geometry bundle
41 out->setKnownFourComponents(fGeoData[0].fColor);
42 }
43
44 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
45 out->setKnownSingleComponent(0xff);
46 }
47
48 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
49
50 private:
51 BWFillRectBatch(const Geometry& geometry) {
52 this->initClassID<BWFillRectBatch>();
53 fGeoData.push_back(geometry);
54
55 fBounds = geometry.fRect;
56 geometry.fViewMatrix.mapRect(&fBounds);
57 }
58
59 GrColor color() const { return fBatch.fColor; }
60 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
61 bool colorIgnored() const { return fBatch.fColorIgnored; }
62 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
63 const SkMatrix& localMatrix() const { return fGeoData[0].fLocalMatrix; }
64 bool hasLocalRect() const { return fGeoData[0].fHasLocalRect; }
65 bool hasLocalMatrix() const { return fGeoData[0].fHasLocalMatrix; }
66 bool coverageIgnored() const { return fBatch.fCoverageIgnored; }
67
68 void initBatchTracker(const GrPipelineOptimizations& init) override {
69 // Handle any color overrides
70 if (!init.readsColor()) {
71 fGeoData[0].fColor = GrColor_ILLEGAL;
72 }
73 init.getOverrideColorIfSet(&fGeoData[0].fColor);
74
75 // setup batch properties
76 fBatch.fColorIgnored = !init.readsColor();
77 fBatch.fColor = fGeoData[0].fColor;
78 fBatch.fUsesLocalCoords = init.readsLocalCoords();
79 fBatch.fCoverageIgnored = !init.readsCoverage();
80 }
81
82 void onPrepareDraws(Target* target) override {
83 SkAutoTUnref<const GrGeometryProcessor> gp(this->createRectGP());
84 if (!gp) {
85 SkDebugf("Could not create GrGeometryProcessor\n");
86 return;
87 }
88
89 target->initDraw(gp, this->pipeline());
90
91 int instanceCount = fGeoData.count();
92 size_t vertexStride = gp->getVertexStride();
93 SkASSERT(this->hasLocalRect() ?
94 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorLo calCoordAttr) :
95 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAt tr));
96 QuadHelper helper;
97 void* vertices = helper.init(target, vertexStride, instanceCount);
98
99 if (!vertices) {
100 return;
101 }
102
103 for (int i = 0; i < instanceCount; i++) {
104 const Geometry& geom = fGeoData[i];
105
106 intptr_t offset = reinterpret_cast<intptr_t>(vertices) +
107 kVerticesPerQuad * i * vertexStride;
108 SkPoint* positions = reinterpret_cast<SkPoint*>(offset);
109
110 positions->setRectFan(geom.fRect.fLeft, geom.fRect.fTop,
111 geom.fRect.fRight, geom.fRect.fBottom, vertexS tride);
112 geom.fViewMatrix.mapPointsWithStride(positions, vertexStride, kVerti cesPerQuad);
113
114 // TODO we should only do this if local coords are being read
115 if (geom.fHasLocalRect) {
116 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor );
117 SkPoint* coords = reinterpret_cast<SkPoint*>(offset + kLocalOffs et);
118 coords->setRectFan(geom.fLocalRect.fLeft, geom.fLocalRect.fTop,
119 geom.fLocalRect.fRight, geom.fLocalRect.fBott om,
120 vertexStride);
121 if (geom.fHasLocalMatrix) {
122 geom.fLocalMatrix.mapPointsWithStride(coords, vertexStride, kVerticesPerQuad);
123 }
124 }
125
126 static const int kColorOffset = sizeof(SkPoint);
127 GrColor* vertColor = reinterpret_cast<GrColor*>(offset + kColorOffse t);
128 for (int j = 0; j < 4; ++j) {
129 *vertColor = geom.fColor;
130 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride);
131 }
132 }
133
134 helper.recordDraw(target);
135 }
136
137 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
138 BWFillRectBatch* that = t->cast<BWFillRectBatch>();
139 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pi peline(),
140 that->bounds(), caps)) {
141 return false;
142 }
143
144 if (this->hasLocalRect() != that->hasLocalRect()) {
145 return false;
146 }
147
148 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
149 if (!this->hasLocalRect() && this->usesLocalCoords()) {
150 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
151 return false;
152 }
153
154 if (this->hasLocalMatrix() != that->hasLocalMatrix()) {
155 return false;
156 }
157
158 if (this->hasLocalMatrix() && !this->localMatrix().cheapEqualTo(that ->localMatrix())) {
159 return false;
160 }
161 }
162
163 if (this->color() != that->color()) {
164 fBatch.fColor = GrColor_ILLEGAL;
165 }
166 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()) ;
167 this->joinBounds(that->bounds());
168 return true; 219 return true;
169 } 220 }
170 221
171 222 static const GrGeometryProcessor* CreateGP(const Geometry& geo,
172 /** We always use per-vertex colors so that rects can be batched across colo r changes. Sometimes 223 const GrPipelineOptimizations& op ts) {
173 we have explicit local coords and sometimes not. We *could* always prov ide explicit local 224 const GrGeometryProcessor* gp = create_gp(geo.fViewMatrix, opts.readsCov erage(), true,
174 coords and just duplicate the positions when the caller hasn't provided a local coord rect, 225 NULL);
175 but we haven't seen a use case which frequently switches between local r ect and no local 226
176 rect draws. 227 SkASSERT(gp->getVertexStride() ==
177 228 sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr));
178 The color param is used to determine whether the opaque hint can be set on the draw state. 229 return gp;
179 The caller must populate the vertex colors itself. 230 }
180 231
181 The vertex attrib order is always pos, color, [local coords]. 232 static void Tesselate(intptr_t vertices, size_t vertexStride, const Geometry & geo,
182 */ 233 const GrPipelineOptimizations& opts) {
183 const GrGeometryProcessor* createRectGP() const { 234 tesselate(vertices, vertexStride, geo.fColor, geo.fViewMatrix, geo.fRect , &geo.fLocalRect,
184 using namespace GrDefaultGeoProcFactory; 235 &geo.fLocalMatrix);
185 Color color(Color::kAttribute_Type); 236 }
186 Coverage coverage(this->coverageIgnored() ? Coverage::kNone_Type : Cover age::kSolid_Type); 237 };
187 238
188 // if we have a local rect, then we apply the localMatrix directly to th e localRect to 239 typedef GrTInstanceBatch<BWFillRectBatchNoLocalMatrixImp> BWFillRectBatchSimple;
189 // generate vertex local coords 240 typedef GrTInstanceBatch<BWFillRectBatchLocalMatrixImp> BWFillRectBatchLocalMatr ix;
190 if (this->hasLocalRect()) { 241 typedef GrTInstanceBatch<BWFillRectBatchLocalRectImp> BWFillRectBatchLocalRect;
191 LocalCoords localCoords(LocalCoords::kHasExplicit_Type); 242 typedef GrTInstanceBatch<BWFillRectBatchLocalMatrixLocalRectImp> BWFillRectBatch LocalMatrixLocalRect;
192 return GrDefaultGeoProcFactory::Create(color, coverage, localCoords, SkMatrix::I());
193 } else {
194 LocalCoords localCoords(LocalCoords::kUsePosition_Type,
195 this->hasLocalMatrix() ? &this->localMatrix( ) : NULL);
196 return GrDefaultGeoProcFactory::CreateForDeviceSpace(color, coverage , localCoords,
197 this->viewMatri x());
198 }
199 }
200
201 struct BatchTracker {
202 GrColor fColor;
203 bool fUsesLocalCoords;
204 bool fColorIgnored;
205 bool fCoverageIgnored;
206 };
207
208 BatchTracker fBatch;
209 SkSTArray<1, Geometry, true> fGeoData;
210 };
211 243
212 namespace GrBWFillRectBatch { 244 namespace GrBWFillRectBatch {
213 GrDrawBatch* Create(GrColor color, 245 GrDrawBatch* Create(GrColor color,
214 const SkMatrix& viewMatrix, 246 const SkMatrix& viewMatrix,
215 const SkRect& rect, 247 const SkRect& rect,
216 const SkRect* localRect, 248 const SkRect* localRect,
217 const SkMatrix* localMatrix) { 249 const SkMatrix* localMatrix) {
218 BWFillRectBatch::Geometry geometry; 250 // TODO bubble these up as separate calls
219 geometry.fColor = color; 251 if (localRect && localMatrix) {
220 geometry.fViewMatrix = viewMatrix; 252 BWFillRectBatchLocalMatrixLocalRect* batch = BWFillRectBatchLocalMatrixL ocalRect::Create();
221 geometry.fRect = rect; 253 BWFillRectBatchLocalMatrixLocalRect::Geometry& geo = *batch->geometry();
222 254 geo.fColor = color;
223 if (localRect) { 255 geo.fViewMatrix = viewMatrix;
224 geometry.fHasLocalRect = true; 256 geo.fLocalMatrix = *localMatrix;
225 geometry.fLocalRect = *localRect; 257 geo.fRect = rect;
258 geo.fLocalRect = *localRect;
259 batch->init();
260 return batch;
261 } else if (localRect) {
262 BWFillRectBatchLocalRect* batch = BWFillRectBatchLocalRect::Create();
263 BWFillRectBatchLocalRect::Geometry& geo = *batch->geometry();
264 geo.fColor = color;
265 geo.fViewMatrix = viewMatrix;
266 geo.fRect = rect;
267 geo.fLocalRect = *localRect;
268 batch->init();
269 return batch;
270 } else if (localMatrix) {
271 BWFillRectBatchLocalMatrix* batch = BWFillRectBatchLocalMatrix::Create() ;
272 BWFillRectBatchLocalMatrix::Geometry& geo = *batch->geometry();
273 geo.fColor = color;
274 geo.fViewMatrix = viewMatrix;
275 geo.fLocalMatrix = *localMatrix;
276 geo.fRect = rect;
277 batch->init();
278 return batch;
226 } else { 279 } else {
227 geometry.fHasLocalRect = false; 280 BWFillRectBatchSimple* batch = BWFillRectBatchSimple::Create();
228 } 281 BWFillRectBatchSimple::Geometry& geo = *batch->geometry();
229 282 geo.fColor = color;
230 if (localMatrix) { 283 geo.fViewMatrix = viewMatrix;
231 geometry.fHasLocalMatrix = true; 284 geo.fRect = rect;
232 geometry.fLocalMatrix = *localMatrix; 285 batch->init();
233 } else { 286 return batch;
234 geometry.fHasLocalMatrix = false; 287 }
235 }
236
237 return BWFillRectBatch::Create(geometry);
238 } 288 }
239 }; 289 };
240 290
241 //////////////////////////////////////////////////////////////////////////////// /////////////////// 291 //////////////////////////////////////////////////////////////////////////////// ///////////////////
242 292
243 #ifdef GR_TEST_UTILS 293 #ifdef GR_TEST_UTILS
244 294
245 #include "GrBatchTest.h" 295 #include "GrBatchTest.h"
246 296
247 DRAW_BATCH_TEST_DEFINE(RectBatch) { 297 DRAW_BATCH_TEST_DEFINE(RectBatch) {
248 BWFillRectBatch::Geometry geometry; 298 GrColor color = GrRandomColor(random);
249 geometry.fColor = GrRandomColor(random); 299 SkRect rect = GrTest::TestRect(random);
300 SkRect localRect = GrTest::TestRect(random);
301 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
302 SkMatrix localMatrix = GrTest::TestMatrix(random);
250 303
251 geometry.fRect = GrTest::TestRect(random); 304 bool hasLocalRect = random->nextBool();
252 geometry.fHasLocalRect = random->nextBool(); 305 bool hasLocalMatrix = random->nextBool();
253 306 return GrBWFillRectBatch::Create(color, viewMatrix, rect, hasLocalRect ? &lo calRect : nullptr,
254 if (geometry.fHasLocalRect) { 307 hasLocalMatrix ? &localMatrix : nullptr);
255 geometry.fViewMatrix = GrTest::TestMatrixInvertible(random);
256 geometry.fLocalRect = GrTest::TestRect(random);
257 } else {
258 geometry.fViewMatrix = GrTest::TestMatrix(random);
259 }
260
261 geometry.fHasLocalMatrix = random->nextBool();
262 if (geometry.fHasLocalMatrix) {
263 geometry.fLocalMatrix = GrTest::TestMatrix(random);
264 }
265
266 return BWFillRectBatch::Create(geometry);
267 } 308 }
268 309
269 #endif 310 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698