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

Side by Side Diff: src/gpu/GrInOrderDrawBuffer.cpp

Issue 931293002: non-aa rects batch (Closed) Base URL: https://skia.googlesource.com/skia.git@dash
Patch Set: cleanup Created 5 years, 10 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2011 Google Inc. 2 * Copyright 2011 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 "GrInOrderDrawBuffer.h" 8 #include "GrInOrderDrawBuffer.h"
9 9
10 #include "GrBufferAllocPool.h" 10 #include "GrBufferAllocPool.h"
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 // allocated space, and leave enough for 50% growth over last time. 99 // allocated space, and leave enough for 50% growth over last time.
100 if (3 * buffer->count() < buffer->reserved() && buffer->reserved() > minRese rve) { 100 if (3 * buffer->count() < buffer->reserved() && buffer->reserved() > minRese rve) {
101 int reserve = SkTMax(minReserve, buffer->count() * 3 / 2); 101 int reserve = SkTMax(minReserve, buffer->count() * 3 / 2);
102 buffer->reset(); 102 buffer->reset();
103 buffer->setReserve(reserve); 103 buffer->setReserve(reserve);
104 } else { 104 } else {
105 buffer->rewind(); 105 buffer->rewind();
106 } 106 }
107 } 107 }
108 108
109 class RectBatch : public GrBatch {
110 public:
robertphillips 2015/02/20 17:29:38 That'a a lot of data!
111 struct Geometry {
112 GrColor fColor;
113 SkMatrix fViewMatrix;
114 SkRect fRect;
robertphillips 2015/02/20 17:29:38 Does reordering the bools yield better packing?
joshualitt 2015/02/20 17:49:07 I can try putting them at the end, that is a good
115 bool fHasLocalRect;
116 bool fHasLocalMatrix;
117 SkRect fLocalRect;
118 SkMatrix fLocalMatrix;
119 };
120
121 static GrBatch* Create(const Geometry& geometry) {
122 return SkNEW_ARGS(RectBatch, (geometry));
123 }
124
125 const char* name() const SK_OVERRIDE { return "RectBatch"; }
126
127 void getInvariantOutputColor(GrInitInvariantOutput* out) const SK_OVERRIDE {
128 // When this is called on a batch, there is only one geometry bundle
129 if (GrColorIsOpaque(fGeoData[0].fColor)) {
130 out->setUnknownOpaqueFourComponents();
131 } else {
132 out->setUnknownFourComponents();
133 }
134 }
135
136 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const SK_OVERRID E {
137 out->setUnknownSingleComponent();
138 }
139
140 void initBatchTracker(const GrPipelineInfo& init) SK_OVERRIDE {
141 // Handle any color overrides
142 if (init.fColorIgnored) {
143 fGeoData[0].fColor = GrColor_ILLEGAL;
144 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
145 fGeoData[0].fColor = init.fOverrideColor;
146 }
147
148 // setup batch properties
149 fBatch.fColorIgnored = init.fColorIgnored;
150 fBatch.fColor = fGeoData[0].fColor;
151 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
152 fBatch.fCoverageIgnored = init.fCoverageIgnored;
153 }
154
155 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline ) SK_OVERRIDE {
156 // Go to device coords to allow batching across matrix changes
157 SkMatrix invert = SkMatrix::I();
158
159 // if we have a local rect, then we apply the localMatrix directly to th e localRect to
160 // generate vertex local coords
161 bool hasExplicitLocalCoords = this->hasLocalRect();
162 if (!hasExplicitLocalCoords) {
163 if (!this->viewMatrix().isIdentity() && !this->viewMatrix().invert(& invert)) {
164 SkDebugf("Could not invert\n");
165 return;
166 }
167
168 if (this->hasLocalMatrix()) {
169 invert.preConcat(this->localMatrix());
170 }
171 }
172
173 SkAutoTUnref<const GrGeometryProcessor> gp(create_rect_gp(hasExplicitLoc alCoords,
174 this->color(),
175 &invert));
176
177 batchTarget->initDraw(gp, pipeline);
178
179 // TODO this is hacky, but the only way we have to initialize the GP is to use the
180 // GrPipelineInfo struct so we can generate the correct shader. Once we have GrBatch
181 // everywhere we can remove this nastiness
182 GrPipelineInfo init;
183 init.fColorIgnored = fBatch.fColorIgnored;
184 init.fOverrideColor = GrColor_ILLEGAL;
185 init.fCoverageIgnored = fBatch.fCoverageIgnored;
186 init.fUsesLocalCoords = this->usesLocalCoords();
187 gp->initBatchTracker(batchTarget->currentBatchTracker(), init);
188
189 size_t vertexStride = gp->getVertexStride();
190
191 SkASSERT(hasExplicitLocalCoords ?
192 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorLo calCoordAttr) :
193 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAt tr));
194
195 int instanceCount = fGeoData.count();
196 int vertexCount = kVertsPerRect * instanceCount;
197
198 const GrVertexBuffer* vertexBuffer;
199 int firstVertex;
200
201 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
202 vertexCount,
203 &vertexBuffer,
204 &firstVertex);
205
206 for (int i = 0; i < instanceCount; i++) {
207 const Geometry& args = fGeoData[i];
208
209 intptr_t offset = GrTCast<intptr_t>(vertices) + kVertsPerRect * i * vertexStride;
210 SkPoint* positions = GrTCast<SkPoint*>(offset);
211
212 positions->setRectFan(args.fRect.fLeft, args.fRect.fTop,
213 args.fRect.fRight, args.fRect.fBottom, vertexS tride);
214 args.fViewMatrix.mapPointsWithStride(positions, vertexStride, kVerts PerRect);
215
216 if (args.fHasLocalRect) {
217 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor );
218 SkPoint* coords = GrTCast<SkPoint*>(offset + kLocalOffset);
219 coords->setRectFan(args.fLocalRect.fLeft, args.fLocalRect.fTop,
220 args.fLocalRect.fRight, args.fLocalRect.fBott om,
221 vertexStride);
222 if (args.fHasLocalMatrix) {
223 args.fLocalMatrix.mapPointsWithStride(coords, vertexStride, kVertsPerRect);
224 }
225 }
226
227 static const int kColorOffset = sizeof(SkPoint);
228 GrColor* vertColor = GrTCast<GrColor*>(offset + kColorOffset);
229 for (int j = 0; j < 4; ++j) {
230 *vertColor = args.fColor;
231 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride);
232 }
233 }
234
235 const GrIndexBuffer* quadIndexBuffer = batchTarget->quadIndexBuffer();
236
237 GrDrawTarget::DrawInfo drawInfo;
238 drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType);
239 drawInfo.setStartVertex(0);
240 drawInfo.setStartIndex(0);
241 drawInfo.setVerticesPerInstance(kVertsPerRect);
242 drawInfo.setIndicesPerInstance(kIndicesPerRect);
243 drawInfo.adjustStartVertex(firstVertex);
244 drawInfo.setVertexBuffer(vertexBuffer);
245 drawInfo.setIndexBuffer(quadIndexBuffer);
246
247 int maxInstancesPerDraw = quadIndexBuffer->maxQuads();
248 while (instanceCount) {
249 drawInfo.setInstanceCount(SkTMin(instanceCount, maxInstancesPerDraw) );
250 drawInfo.setVertexCount(drawInfo.instanceCount() * drawInfo.vertices PerInstance());
251 drawInfo.setIndexCount(drawInfo.instanceCount() * drawInfo.indicesPe rInstance());
252
253 batchTarget->draw(drawInfo);
254
255 drawInfo.setStartVertex(drawInfo.startVertex() + drawInfo.vertexCoun t());
256 instanceCount -= drawInfo.instanceCount();
257 }
258 }
259
260 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
261
262 private:
263 RectBatch(const Geometry& geometry) {
264 this->initClassID<RectBatch>();
265 fGeoData.push_back(geometry);
266 }
267
268 GrColor color() const { return fBatch.fColor; }
269 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
270 bool colorIgnored() const { return fBatch.fColorIgnored; }
271 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
272 const SkMatrix& localMatrix() const { return fGeoData[0].fLocalMatrix; }
273 bool hasLocalRect() const { return fGeoData[0].fHasLocalRect; }
274 bool hasLocalMatrix() const { return fGeoData[0].fHasLocalMatrix; }
275
276 bool onCombineIfPossible(GrBatch* t) SK_OVERRIDE {
277 RectBatch* that = t->cast<RectBatch>();
278
279 if (this->hasLocalRect() != that->hasLocalRect()) {
280 return false;
281 }
282
283 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
284 if (!this->hasLocalRect() && this->usesLocalCoords()) {
robertphillips 2015/02/20 17:29:38 Do we need a view matrix for each one then?
joshualitt 2015/02/20 17:49:07 Well, I think the most common case by far is to no
285 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
286 return false;
287 }
288
289 if (this->hasLocalMatrix() != that->hasLocalMatrix()) {
290 return false;
291 }
292
robertphillips 2015/02/20 17:29:38 Ditto for local matrix?
293 if (this->hasLocalMatrix() && !this->localMatrix().cheapEqualTo(that ->localMatrix())) {
294 return false;
295 }
296 }
297
298 if (this->color() != that->color()) {
299 fBatch.fColor = GrColor_ILLEGAL;
300 }
301 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin()) ;
302 return true;
303 }
304
305 struct BatchTracker {
306 GrColor fColor;
307 bool fUsesLocalCoords;
308 bool fColorIgnored;
309 bool fCoverageIgnored;
310 };
311
312 const static int kVertsPerRect = 4;
313 const static int kIndicesPerRect = 6;
314
315 BatchTracker fBatch;
316 SkSTArray<1, Geometry, true> fGeoData;
317 };
318
109 void GrInOrderDrawBuffer::onDrawRect(GrPipelineBuilder* pipelineBuilder, 319 void GrInOrderDrawBuffer::onDrawRect(GrPipelineBuilder* pipelineBuilder,
110 GrColor color, 320 GrColor color,
111 const SkMatrix& viewMatrix, 321 const SkMatrix& viewMatrix,
112 const SkRect& rect, 322 const SkRect& rect,
113 const SkRect* localRect, 323 const SkRect* localRect,
114 const SkMatrix* localMatrix) { 324 const SkMatrix* localMatrix) {
115 GrPipelineBuilder::AutoRestoreEffects are(pipelineBuilder); 325 GrPipelineBuilder::AutoRestoreEffects are(pipelineBuilder);
116 326 RectBatch::Geometry geometry;
117 // Go to device coords to allow batching across matrix changes 327 geometry.fColor = color;
118 SkMatrix invert = SkMatrix::I(); 328 geometry.fViewMatrix = viewMatrix;
119 329 geometry.fRect = rect;
120 // if we have a local rect, then we apply the localMatrix directly to the lo calRect to generate
121 // vertex local coords
122 bool hasExplicitLocalCoords = SkToBool(localRect);
123 if (!hasExplicitLocalCoords) {
124 if (!viewMatrix.isIdentity() && !viewMatrix.invert(&invert)) {
125 SkDebugf("Could not invert\n");
126 return;
127 }
128
129 if (localMatrix) {
130 invert.preConcat(*localMatrix);
131 }
132 }
133
134 SkAutoTUnref<const GrGeometryProcessor> gp(create_rect_gp(hasExplicitLocalCo ords,
135 color,
136 &invert));
137
138 size_t vstride = gp->getVertexStride();
139 SkASSERT(vstride == sizeof(SkPoint) + sizeof(GrColor) + (SkToBool(localRect) ? sizeof(SkPoint) :
140 0));
141 AutoReleaseGeometry geo(this, 4, vstride, 0);
142 if (!geo.succeeded()) {
143 SkDebugf("Failed to get space for vertices!\n");
144 return;
145 }
146
147 geo.positions()->setRectFan(rect.fLeft, rect.fTop, rect.fRight, rect.fBottom , vstride);
148 viewMatrix.mapPointsWithStride(geo.positions(), vstride, 4);
149
150 // When the caller has provided an explicit source rect for a stage then we don't want to
151 // modify that stage's matrix. Otherwise if the effect is generating its sou rce rect from
152 // the vertex positions then we have to account for the view matrix
153 SkRect devBounds;
154
155 // since we already computed the dev verts, set the bounds hint. This will h elp us avoid
156 // unnecessary clipping in our onDraw().
157 get_vertex_bounds(geo.vertices(), vstride, 4, &devBounds);
158 330
159 if (localRect) { 331 if (localRect) {
160 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); 332 geometry.fHasLocalRect = true;
161 SkPoint* coords = GrTCast<SkPoint*>(GrTCast<intptr_t>(geo.vertices()) + kLocalOffset); 333 geometry.fLocalRect = *localRect;
162 coords->setRectFan(localRect->fLeft, localRect->fTop, 334 } else {
163 localRect->fRight, localRect->fBottom, 335 geometry.fHasLocalRect = false;
164 vstride); 336 }
165 if (localMatrix) { 337
166 localMatrix->mapPointsWithStride(coords, vstride, 4); 338 if (localMatrix) {
167 } 339 geometry.fHasLocalMatrix = true;
168 } 340 geometry.fLocalMatrix = *localMatrix;
169 341 } else {
170 static const int kColorOffset = sizeof(SkPoint); 342 geometry.fHasLocalMatrix = false;
171 GrColor* vertColor = GrTCast<GrColor*>(GrTCast<intptr_t>(geo.vertices()) + k ColorOffset); 343 }
172 for (int i = 0; i < 4; ++i) { 344
173 *vertColor = color; 345 SkAutoTUnref<GrBatch> batch(RectBatch::Create(geometry));
174 vertColor = (GrColor*) ((intptr_t) vertColor + vstride); 346
175 } 347 SkRect bounds = rect;
176 348 viewMatrix.mapRect(&bounds);
177 this->setIndexSourceToBuffer(this->getContext()->getQuadIndexBuffer()); 349 this->drawBatch(pipelineBuilder, batch, &bounds);
178 this->drawIndexedInstances(pipelineBuilder, gp, kTriangles_GrPrimitiveType, 1, 4, 6,
179 &devBounds);
180 } 350 }
181 351
182 int GrInOrderDrawBuffer::concatInstancedDraw(const DrawInfo& info) { 352 int GrInOrderDrawBuffer::concatInstancedDraw(const DrawInfo& info) {
183 SkASSERT(!fCmdBuffer.empty()); 353 SkASSERT(!fCmdBuffer.empty());
184 SkASSERT(info.isInstanced()); 354 SkASSERT(info.isInstanced());
185 355
186 const GeometrySrcState& geomSrc = this->getGeomSrc(); 356 const GeometrySrcState& geomSrc = this->getGeomSrc();
187 357
188 // we only attempt to concat the case when reserved verts are used with a cl ient-specified index 358 // we only attempt to concat the case when reserved verts are used with a cl ient-specified index
189 // buffer. To make this work with client-specified VBs we'd need to know if the VB was updated 359 // buffer. To make this work with client-specified VBs we'd need to know if the VB was updated
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 451
282 void GrInOrderDrawBuffer::onStencilPath(const GrPipelineBuilder& pipelineBuilder , 452 void GrInOrderDrawBuffer::onStencilPath(const GrPipelineBuilder& pipelineBuilder ,
283 const GrPathProcessor* pathProc, 453 const GrPathProcessor* pathProc,
284 const GrPath* path, 454 const GrPath* path,
285 const GrScissorState& scissorState, 455 const GrScissorState& scissorState,
286 const GrStencilSettings& stencilSettings ) { 456 const GrStencilSettings& stencilSettings ) {
287 this->closeBatch(); 457 this->closeBatch();
288 458
289 StencilPath* sp = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, StencilPath, 459 StencilPath* sp = GrNEW_APPEND_TO_RECORDER(fCmdBuffer, StencilPath,
290 (path, pipelineBuilder.getRenderT arget())); 460 (path, pipelineBuilder.getRenderT arget()));
461
291 sp->fScissor = scissorState; 462 sp->fScissor = scissorState;
292 sp->fUseHWAA = pipelineBuilder.isHWAntialias(); 463 sp->fUseHWAA = pipelineBuilder.isHWAntialias();
293 sp->fViewMatrix = pathProc->viewMatrix(); 464 sp->fViewMatrix = pathProc->viewMatrix();
294 sp->fStencil = stencilSettings; 465 sp->fStencil = stencilSettings;
295 this->recordTraceMarkersIfNecessary(); 466 this->recordTraceMarkersIfNecessary();
296 } 467 }
297 468
298 void GrInOrderDrawBuffer::onDrawPath(const GrPathProcessor* pathProc, 469 void GrInOrderDrawBuffer::onDrawPath(const GrPathProcessor* pathProc,
299 const GrPath* path, 470 const GrPath* path,
300 const GrStencilSettings& stencilSettings, 471 const GrStencilSettings& stencilSettings,
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
624 } 795 }
625 } 796 }
626 797
627 void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace(int vertexCount, 798 void GrInOrderDrawBuffer::willReserveVertexAndIndexSpace(int vertexCount,
628 size_t vertexStride, 799 size_t vertexStride,
629 int indexCount) { 800 int indexCount) {
630 this->closeBatch(); 801 this->closeBatch();
631 802
632 this->INHERITED::willReserveVertexAndIndexSpace(vertexCount, vertexStride, i ndexCount); 803 this->INHERITED::willReserveVertexAndIndexSpace(vertexCount, vertexStride, i ndexCount);
633 } 804 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698