OLD | NEW |
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 "GrDefaultGeoProcFactory.h" | 10 #include "GrDefaultGeoProcFactory.h" |
11 #include "GrResourceProvider.h" | |
12 #include "GrTemplates.h" | 11 #include "GrTemplates.h" |
13 | 12 |
14 GrInOrderDrawBuffer::GrInOrderDrawBuffer(GrContext* context, | 13 GrInOrderDrawBuffer::GrInOrderDrawBuffer(GrContext* context, |
15 GrVertexBufferAllocPool* vertexPool, | 14 GrVertexBufferAllocPool* vertexPool, |
16 GrIndexBufferAllocPool* indexPool) | 15 GrIndexBufferAllocPool* indexPool) |
17 : INHERITED(context, vertexPool, indexPool) | 16 : INHERITED(context, vertexPool, indexPool) |
18 , fCommands(context->getGpu(), vertexPool, indexPool) | 17 , fCommands(context->getGpu(), vertexPool, indexPool) |
19 , fPathIndexBuffer(kPathIdxBufferMinReserve * sizeof(char)/4) | 18 , fPathIndexBuffer(kPathIdxBufferMinReserve * sizeof(char)/4) |
20 , fPathTransformBuffer(kPathXformBufferMinReserve * sizeof(float)/4) | 19 , fPathTransformBuffer(kPathXformBufferMinReserve * sizeof(float)/4) |
21 , fPipelineBuffer(kPipelineBufferMinReserve) | 20 , fPipelineBuffer(kPipelineBufferMinReserve) |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 // TODO this is hacky, but the only way we have to initialize the GP is
to use the | 124 // TODO this is hacky, but the only way we have to initialize the GP is
to use the |
126 // GrPipelineInfo struct so we can generate the correct shader. Once we
have GrBatch | 125 // GrPipelineInfo struct so we can generate the correct shader. Once we
have GrBatch |
127 // everywhere we can remove this nastiness | 126 // everywhere we can remove this nastiness |
128 GrPipelineInfo init; | 127 GrPipelineInfo init; |
129 init.fColorIgnored = fBatch.fColorIgnored; | 128 init.fColorIgnored = fBatch.fColorIgnored; |
130 init.fOverrideColor = GrColor_ILLEGAL; | 129 init.fOverrideColor = GrColor_ILLEGAL; |
131 init.fCoverageIgnored = fBatch.fCoverageIgnored; | 130 init.fCoverageIgnored = fBatch.fCoverageIgnored; |
132 init.fUsesLocalCoords = this->usesLocalCoords(); | 131 init.fUsesLocalCoords = this->usesLocalCoords(); |
133 gp->initBatchTracker(batchTarget->currentBatchTracker(), init); | 132 gp->initBatchTracker(batchTarget->currentBatchTracker(), init); |
134 | 133 |
| 134 int instanceCount = fGeoData.count(); |
135 size_t vertexStride = gp->getVertexStride(); | 135 size_t vertexStride = gp->getVertexStride(); |
136 | |
137 SkASSERT(hasExplicitLocalCoords ? | 136 SkASSERT(hasExplicitLocalCoords ? |
138 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorLo
calCoordAttr) : | 137 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorLo
calCoordAttr) : |
139 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAt
tr)); | 138 vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAt
tr)); |
| 139 QuadHelper helper; |
| 140 void* vertices = helper.init(batchTarget, vertexStride, instanceCount); |
140 | 141 |
141 int instanceCount = fGeoData.count(); | 142 if (!vertices) { |
142 SkAutoTUnref<const GrIndexBuffer> indexBuffer( | |
143 batchTarget->resourceProvider()->refQuadIndexBuffer()); | |
144 | |
145 int vertexCount = kVertsPerRect * instanceCount; | |
146 const GrVertexBuffer* vertexBuffer; | |
147 int firstVertex; | |
148 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride, | |
149 vertexCount, | |
150 &vertexBuffer, | |
151 &firstVertex); | |
152 | |
153 if (!vertices || !indexBuffer) { | |
154 SkDebugf("Could not allocate buffers\n"); | |
155 return; | 143 return; |
156 } | 144 } |
157 | 145 |
| 146 |
158 for (int i = 0; i < instanceCount; i++) { | 147 for (int i = 0; i < instanceCount; i++) { |
159 const Geometry& args = fGeoData[i]; | 148 const Geometry& geom = fGeoData[i]; |
160 | 149 |
161 intptr_t offset = GrTCast<intptr_t>(vertices) + kVertsPerRect * i *
vertexStride; | 150 intptr_t offset = GrTCast<intptr_t>(vertices) + kVerticesPerQuad * i
* vertexStride; |
162 SkPoint* positions = GrTCast<SkPoint*>(offset); | 151 SkPoint* positions = GrTCast<SkPoint*>(offset); |
163 | 152 |
164 positions->setRectFan(args.fRect.fLeft, args.fRect.fTop, | 153 positions->setRectFan(geom.fRect.fLeft, geom.fRect.fTop, |
165 args.fRect.fRight, args.fRect.fBottom, vertexS
tride); | 154 geom.fRect.fRight, geom.fRect.fBottom, vertexS
tride); |
166 args.fViewMatrix.mapPointsWithStride(positions, vertexStride, kVerts
PerRect); | 155 geom.fViewMatrix.mapPointsWithStride(positions, vertexStride, kVerti
cesPerQuad); |
167 | 156 |
168 if (args.fHasLocalRect) { | 157 if (geom.fHasLocalRect) { |
169 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor
); | 158 static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor
); |
170 SkPoint* coords = GrTCast<SkPoint*>(offset + kLocalOffset); | 159 SkPoint* coords = GrTCast<SkPoint*>(offset + kLocalOffset); |
171 coords->setRectFan(args.fLocalRect.fLeft, args.fLocalRect.fTop, | 160 coords->setRectFan(geom.fLocalRect.fLeft, geom.fLocalRect.fTop, |
172 args.fLocalRect.fRight, args.fLocalRect.fBott
om, | 161 geom.fLocalRect.fRight, geom.fLocalRect.fBott
om, |
173 vertexStride); | 162 vertexStride); |
174 if (args.fHasLocalMatrix) { | 163 if (geom.fHasLocalMatrix) { |
175 args.fLocalMatrix.mapPointsWithStride(coords, vertexStride,
kVertsPerRect); | 164 geom.fLocalMatrix.mapPointsWithStride(coords, vertexStride,
kVerticesPerQuad); |
176 } | 165 } |
177 } | 166 } |
178 | 167 |
179 static const int kColorOffset = sizeof(SkPoint); | 168 static const int kColorOffset = sizeof(SkPoint); |
180 GrColor* vertColor = GrTCast<GrColor*>(offset + kColorOffset); | 169 GrColor* vertColor = GrTCast<GrColor*>(offset + kColorOffset); |
181 for (int j = 0; j < 4; ++j) { | 170 for (int j = 0; j < 4; ++j) { |
182 *vertColor = args.fColor; | 171 *vertColor = geom.fColor; |
183 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride); | 172 vertColor = (GrColor*) ((intptr_t) vertColor + vertexStride); |
184 } | 173 } |
185 } | 174 } |
186 | 175 |
187 GrDrawTarget::DrawInfo drawInfo; | 176 helper.issueDraws(batchTarget); |
188 drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType); | |
189 drawInfo.setStartVertex(0); | |
190 drawInfo.setStartIndex(0); | |
191 drawInfo.setVerticesPerInstance(kVertsPerRect); | |
192 drawInfo.setIndicesPerInstance(kIndicesPerRect); | |
193 drawInfo.adjustStartVertex(firstVertex); | |
194 drawInfo.setVertexBuffer(vertexBuffer); | |
195 drawInfo.setIndexBuffer(indexBuffer); | |
196 | |
197 int maxInstancesPerDraw = indexBuffer->maxQuads(); | |
198 while (instanceCount) { | |
199 drawInfo.setInstanceCount(SkTMin(instanceCount, maxInstancesPerDraw)
); | |
200 drawInfo.setVertexCount(drawInfo.instanceCount() * drawInfo.vertices
PerInstance()); | |
201 drawInfo.setIndexCount(drawInfo.instanceCount() * drawInfo.indicesPe
rInstance()); | |
202 | |
203 batchTarget->draw(drawInfo); | |
204 | |
205 drawInfo.setStartVertex(drawInfo.startVertex() + drawInfo.vertexCoun
t()); | |
206 instanceCount -= drawInfo.instanceCount(); | |
207 } | |
208 } | 177 } |
209 | 178 |
210 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } | 179 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
211 | 180 |
212 private: | 181 private: |
213 RectBatch(const Geometry& geometry) { | 182 RectBatch(const Geometry& geometry) { |
214 this->initClassID<RectBatch>(); | 183 this->initClassID<RectBatch>(); |
215 fGeoData.push_back(geometry); | 184 fGeoData.push_back(geometry); |
216 | 185 |
217 fBounds = geometry.fRect; | 186 fBounds = geometry.fRect; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 return true; | 225 return true; |
257 } | 226 } |
258 | 227 |
259 struct BatchTracker { | 228 struct BatchTracker { |
260 GrColor fColor; | 229 GrColor fColor; |
261 bool fUsesLocalCoords; | 230 bool fUsesLocalCoords; |
262 bool fColorIgnored; | 231 bool fColorIgnored; |
263 bool fCoverageIgnored; | 232 bool fCoverageIgnored; |
264 }; | 233 }; |
265 | 234 |
266 const static int kVertsPerRect = 4; | |
267 const static int kIndicesPerRect = 6; | |
268 | |
269 BatchTracker fBatch; | 235 BatchTracker fBatch; |
270 SkSTArray<1, Geometry, true> fGeoData; | 236 SkSTArray<1, Geometry, true> fGeoData; |
271 }; | 237 }; |
272 | 238 |
273 void GrInOrderDrawBuffer::onDrawRect(GrPipelineBuilder* pipelineBuilder, | 239 void GrInOrderDrawBuffer::onDrawRect(GrPipelineBuilder* pipelineBuilder, |
274 GrColor color, | 240 GrColor color, |
275 const SkMatrix& viewMatrix, | 241 const SkMatrix& viewMatrix, |
276 const SkRect& rect, | 242 const SkRect& rect, |
277 const SkRect* localRect, | 243 const SkRect* localRect, |
278 const SkMatrix* localMatrix) { | 244 const SkMatrix* localMatrix) { |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
464 if (fPrevState && !fPrevState->fPrimitiveProcessor.get() && | 430 if (fPrevState && !fPrevState->fPrimitiveProcessor.get() && |
465 fPrevState->getPipeline()->isEqual(*state->getPipeline())) { | 431 fPrevState->getPipeline()->isEqual(*state->getPipeline())) { |
466 this->unallocState(state); | 432 this->unallocState(state); |
467 } else { | 433 } else { |
468 fPrevState.reset(state); | 434 fPrevState.reset(state); |
469 } | 435 } |
470 | 436 |
471 fCommands.recordXferBarrierIfNecessary(*fPrevState->getPipeline(), this); | 437 fCommands.recordXferBarrierIfNecessary(*fPrevState->getPipeline(), this); |
472 return fPrevState; | 438 return fPrevState; |
473 } | 439 } |
OLD | NEW |