OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2014 Google Inc. | 3 * Copyright 2014 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "GrAADistanceFieldPathRenderer.h" | 9 #include "GrAADistanceFieldPathRenderer.h" |
10 | 10 |
11 #include "GrBatch.h" | 11 #include "GrBatch.h" |
12 #include "GrBatchTarget.h" | 12 #include "GrBatchTarget.h" |
13 #include "GrBufferAllocPool.h" | 13 #include "GrBufferAllocPool.h" |
14 #include "GrContext.h" | 14 #include "GrContext.h" |
15 #include "GrPipelineBuilder.h" | 15 #include "GrPipelineBuilder.h" |
| 16 #include "GrResourceProvider.h" |
16 #include "GrSurfacePriv.h" | 17 #include "GrSurfacePriv.h" |
17 #include "GrSWMaskHelper.h" | 18 #include "GrSWMaskHelper.h" |
18 #include "GrResourceProvider.h" | |
19 #include "GrTexturePriv.h" | 19 #include "GrTexturePriv.h" |
20 #include "GrVertexBuffer.h" | 20 #include "GrVertexBuffer.h" |
21 #include "effects/GrDistanceFieldGeoProc.h" | 21 #include "effects/GrDistanceFieldGeoProc.h" |
22 | 22 |
23 #include "SkDistanceFieldGen.h" | 23 #include "SkDistanceFieldGen.h" |
24 #include "SkRTConf.h" | 24 #include "SkRTConf.h" |
25 | 25 |
26 #define ATLAS_TEXTURE_WIDTH 1024 | 26 #define ATLAS_TEXTURE_WIDTH 1024 |
27 #define ATLAS_TEXTURE_HEIGHT 2048 | 27 #define ATLAS_TEXTURE_HEIGHT 2048 |
28 #define PLOT_WIDTH 256 | 28 #define PLOT_WIDTH 256 |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 } else if (GrColor_ILLEGAL != init.fOverrideColor) { | 162 } else if (GrColor_ILLEGAL != init.fOverrideColor) { |
163 fBatch.fColor = init.fOverrideColor; | 163 fBatch.fColor = init.fOverrideColor; |
164 } | 164 } |
165 | 165 |
166 // setup batch properties | 166 // setup batch properties |
167 fBatch.fColorIgnored = init.fColorIgnored; | 167 fBatch.fColorIgnored = init.fColorIgnored; |
168 fBatch.fUsesLocalCoords = init.fUsesLocalCoords; | 168 fBatch.fUsesLocalCoords = init.fUsesLocalCoords; |
169 fBatch.fCoverageIgnored = init.fCoverageIgnored; | 169 fBatch.fCoverageIgnored = init.fCoverageIgnored; |
170 } | 170 } |
171 | 171 |
| 172 struct FlushInfo { |
| 173 SkAutoTUnref<const GrVertexBuffer> fVertexBuffer; |
| 174 SkAutoTUnref<const GrIndexBuffer> fIndexBuffer; |
| 175 int fVertexOffset; |
| 176 int fInstancesToFlush; |
| 177 }; |
| 178 |
172 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline
) override { | 179 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline
) override { |
173 int instanceCount = fGeoData.count(); | 180 int instanceCount = fGeoData.count(); |
174 | 181 |
175 SkMatrix invert; | 182 SkMatrix invert; |
176 if (this->usesLocalCoords() && !this->viewMatrix().invert(&invert)) { | 183 if (this->usesLocalCoords() && !this->viewMatrix().invert(&invert)) { |
177 SkDebugf("Could not invert viewmatrix\n"); | 184 SkDebugf("Could not invert viewmatrix\n"); |
178 return; | 185 return; |
179 } | 186 } |
180 | 187 |
181 uint32_t flags = 0; | 188 uint32_t flags = 0; |
182 flags |= this->viewMatrix().isSimilarity() ? kSimilarity_DistanceFieldEf
fectFlag : 0; | 189 flags |= this->viewMatrix().isSimilarity() ? kSimilarity_DistanceFieldEf
fectFlag : 0; |
183 | 190 |
184 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBil
erp_FilterMode); | 191 GrTextureParams params(SkShader::kRepeat_TileMode, GrTextureParams::kBil
erp_FilterMode); |
185 | 192 |
186 // Setup GrGeometryProcessor | 193 // Setup GrGeometryProcessor |
187 GrBatchAtlas* atlas = fAtlas; | 194 GrBatchAtlas* atlas = fAtlas; |
188 SkAutoTUnref<GrGeometryProcessor> dfProcessor( | 195 SkAutoTUnref<GrGeometryProcessor> dfProcessor( |
189 GrDistanceFieldPathGeoProc::Create(this->color(), | 196 GrDistanceFieldPathGeoProc::Create(this->color(), |
190 this->viewMatrix(), | 197 this->viewMatrix(), |
191 atlas->getTexture(), | 198 atlas->getTexture(), |
192 params, | 199 params, |
193 flags, | 200 flags, |
194 false)); | 201 false)); |
195 | 202 |
196 this->initDraw(batchTarget, dfProcessor, pipeline); | 203 this->initDraw(batchTarget, dfProcessor, pipeline); |
197 | 204 |
198 static const int kVertsPerQuad = 4; | 205 FlushInfo flushInfo; |
199 static const int kIndicesPerQuad = 6; | |
200 | |
201 SkAutoTUnref<const GrIndexBuffer> indexBuffer( | |
202 batchTarget->resourceProvider()->refQuadIndexBuffer()); | |
203 | 206 |
204 // allocate vertices | 207 // allocate vertices |
205 size_t vertexStride = dfProcessor->getVertexStride(); | 208 size_t vertexStride = dfProcessor->getVertexStride(); |
206 SkASSERT(vertexStride == 2 * sizeof(SkPoint)); | 209 SkASSERT(vertexStride == 2 * sizeof(SkPoint)); |
| 210 |
207 const GrVertexBuffer* vertexBuffer; | 211 const GrVertexBuffer* vertexBuffer; |
208 int vertexCount = kIndicesPerQuad * instanceCount; | |
209 int firstVertex; | |
210 | |
211 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride, | 212 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride, |
212 vertexCount, | 213 kVerticesPerQuad *
instanceCount, |
213 &vertexBuffer, | 214 &vertexBuffer, |
214 &firstVertex); | 215 &flushInfo.fVertex
Offset); |
215 | 216 flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer)); |
216 if (!vertices || !indexBuffer) { | 217 flushInfo.fIndexBuffer.reset(batchTarget->resourceProvider()->refQuadInd
exBuffer()); |
| 218 if (!vertices || !flushInfo.fIndexBuffer) { |
217 SkDebugf("Could not allocate vertices\n"); | 219 SkDebugf("Could not allocate vertices\n"); |
218 return; | 220 return; |
219 } | 221 } |
220 | 222 |
221 // We may have to flush while uploading path data to the atlas, so we se
t up the draw here | 223 flushInfo.fInstancesToFlush = 0; |
222 int maxInstancesPerDraw = indexBuffer->maxQuads(); | |
223 | |
224 GrDrawTarget::DrawInfo drawInfo; | |
225 drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType); | |
226 drawInfo.setStartVertex(0); | |
227 drawInfo.setStartIndex(0); | |
228 drawInfo.setVerticesPerInstance(kVertsPerQuad); | |
229 drawInfo.setIndicesPerInstance(kIndicesPerQuad); | |
230 drawInfo.adjustStartVertex(firstVertex); | |
231 drawInfo.setVertexBuffer(vertexBuffer); | |
232 drawInfo.setIndexBuffer(indexBuffer); | |
233 | |
234 int instancesToFlush = 0; | |
235 for (int i = 0; i < instanceCount; i++) { | 224 for (int i = 0; i < instanceCount; i++) { |
236 Geometry& args = fGeoData[i]; | 225 Geometry& args = fGeoData[i]; |
237 | 226 |
238 // get mip level | 227 // get mip level |
239 SkScalar maxScale = this->viewMatrix().getMaxScale(); | 228 SkScalar maxScale = this->viewMatrix().getMaxScale(); |
240 const SkRect& bounds = args.fPath.getBounds(); | 229 const SkRect& bounds = args.fPath.getBounds(); |
241 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height()); | 230 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height()); |
242 SkScalar size = maxScale * maxDim; | 231 SkScalar size = maxScale * maxDim; |
243 uint32_t desiredDimension; | 232 uint32_t desiredDimension; |
244 if (size <= kSmallMIP) { | 233 if (size <= kSmallMIP) { |
(...skipping 13 matching lines...) Expand all Loading... |
258 if (args.fPathData) { | 247 if (args.fPathData) { |
259 fPathCache->remove(args.fPathData->fKey); | 248 fPathCache->remove(args.fPathData->fKey); |
260 fPathList->remove(args.fPathData); | 249 fPathList->remove(args.fPathData); |
261 SkDELETE(args.fPathData); | 250 SkDELETE(args.fPathData); |
262 } | 251 } |
263 SkScalar scale = desiredDimension/maxDim; | 252 SkScalar scale = desiredDimension/maxDim; |
264 args.fPathData = SkNEW(PathData); | 253 args.fPathData = SkNEW(PathData); |
265 if (!this->addPathToAtlas(batchTarget, | 254 if (!this->addPathToAtlas(batchTarget, |
266 dfProcessor, | 255 dfProcessor, |
267 pipeline, | 256 pipeline, |
268 &drawInfo, | 257 &flushInfo, |
269 &instancesToFlush, | |
270 maxInstancesPerDraw, | |
271 atlas, | 258 atlas, |
272 args.fPathData, | 259 args.fPathData, |
273 args.fPath, | 260 args.fPath, |
274 args.fStroke, | 261 args.fStroke, |
275 args.fAntiAlias, | 262 args.fAntiAlias, |
276 desiredDimension, | 263 desiredDimension, |
277 scale)) { | 264 scale)) { |
278 SkDebugf("Can't rasterize path\n"); | 265 SkDebugf("Can't rasterize path\n"); |
279 return; | 266 return; |
280 } | 267 } |
281 } | 268 } |
282 | 269 |
283 atlas->setLastUseToken(args.fPathData->fID, batchTarget->currentToke
n()); | 270 atlas->setLastUseToken(args.fPathData->fID, batchTarget->currentToke
n()); |
284 | 271 |
285 // Now set vertices | 272 // Now set vertices |
286 intptr_t offset = reinterpret_cast<intptr_t>(vertices); | 273 intptr_t offset = reinterpret_cast<intptr_t>(vertices); |
287 offset += i * kVertsPerQuad * vertexStride; | 274 offset += i * kVerticesPerQuad * vertexStride; |
288 SkPoint* positions = reinterpret_cast<SkPoint*>(offset); | 275 SkPoint* positions = reinterpret_cast<SkPoint*>(offset); |
289 this->drawPath(batchTarget, | 276 this->writePathVertices(batchTarget, |
290 atlas, | 277 atlas, |
291 pipeline, | 278 pipeline, |
292 dfProcessor, | 279 dfProcessor, |
293 positions, | 280 positions, |
294 vertexStride, | 281 vertexStride, |
295 this->viewMatrix(), | 282 this->viewMatrix(), |
296 args.fPath, | 283 args.fPath, |
297 args.fPathData); | 284 args.fPathData); |
298 instancesToFlush++; | 285 flushInfo.fInstancesToFlush++; |
299 } | 286 } |
300 | 287 |
301 this->flush(batchTarget, &drawInfo, instancesToFlush, maxInstancesPerDra
w); | 288 this->flush(batchTarget, &flushInfo); |
302 } | 289 } |
303 | 290 |
304 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } | 291 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; } |
305 | 292 |
306 private: | 293 private: |
307 AADistanceFieldPathBatch(const Geometry& geometry, GrColor color, const SkMa
trix& viewMatrix, | 294 AADistanceFieldPathBatch(const Geometry& geometry, GrColor color, const SkMa
trix& viewMatrix, |
308 GrBatchAtlas* atlas, | 295 GrBatchAtlas* atlas, |
309 PathCache* pathCache, PathDataList* pathList) { | 296 PathCache* pathCache, PathDataList* pathList) { |
310 this->initClassID<AADistanceFieldPathBatch>(); | 297 this->initClassID<AADistanceFieldPathBatch>(); |
311 fBatch.fColor = color; | 298 fBatch.fColor = color; |
312 fBatch.fViewMatrix = viewMatrix; | 299 fBatch.fViewMatrix = viewMatrix; |
313 fGeoData.push_back(geometry); | 300 fGeoData.push_back(geometry); |
314 fGeoData.back().fPathData = NULL; | 301 fGeoData.back().fPathData = NULL; |
315 | 302 |
316 fAtlas = atlas; | 303 fAtlas = atlas; |
317 fPathCache = pathCache; | 304 fPathCache = pathCache; |
318 fPathList = pathList; | 305 fPathList = pathList; |
319 | 306 |
320 // Compute bounds | 307 // Compute bounds |
321 fBounds = geometry.fPath.getBounds(); | 308 fBounds = geometry.fPath.getBounds(); |
322 viewMatrix.mapRect(&fBounds); | 309 viewMatrix.mapRect(&fBounds); |
323 } | 310 } |
324 | 311 |
325 bool addPathToAtlas(GrBatchTarget* batchTarget, | 312 bool addPathToAtlas(GrBatchTarget* batchTarget, |
326 const GrGeometryProcessor* dfProcessor, | 313 const GrGeometryProcessor* dfProcessor, |
327 const GrPipeline* pipeline, | 314 const GrPipeline* pipeline, |
328 GrDrawTarget::DrawInfo* drawInfo, | 315 FlushInfo* flushInfo, |
329 int* instancesToFlush, | |
330 int maxInstancesPerDraw, | |
331 GrBatchAtlas* atlas, | 316 GrBatchAtlas* atlas, |
332 PathData* pathData, | 317 PathData* pathData, |
333 const SkPath& path, | 318 const SkPath& path, |
334 const SkStrokeRec& | 319 const SkStrokeRec& |
335 stroke, bool antiAlias, | 320 stroke, bool antiAlias, |
336 uint32_t dimension, | 321 uint32_t dimension, |
337 SkScalar scale) { | 322 SkScalar scale) { |
338 const SkRect& bounds = path.getBounds(); | 323 const SkRect& bounds = path.getBounds(); |
339 | 324 |
340 // generate bounding rect for bitmap draw | 325 // generate bounding rect for bitmap draw |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 (const unsigned char*)bmp.getPixe
ls(), | 407 (const unsigned char*)bmp.getPixe
ls(), |
423 bmp.width(), bmp.height(), bmp.ro
wBytes()); | 408 bmp.width(), bmp.height(), bmp.ro
wBytes()); |
424 } | 409 } |
425 | 410 |
426 // add to atlas | 411 // add to atlas |
427 SkIPoint16 atlasLocation; | 412 SkIPoint16 atlasLocation; |
428 GrBatchAtlas::AtlasID id; | 413 GrBatchAtlas::AtlasID id; |
429 bool success = atlas->addToAtlas(&id, batchTarget, width, height, dfStor
age.get(), | 414 bool success = atlas->addToAtlas(&id, batchTarget, width, height, dfStor
age.get(), |
430 &atlasLocation); | 415 &atlasLocation); |
431 if (!success) { | 416 if (!success) { |
432 this->flush(batchTarget, drawInfo, *instancesToFlush, maxInstancesPe
rDraw); | 417 this->flush(batchTarget, flushInfo); |
433 this->initDraw(batchTarget, dfProcessor, pipeline); | 418 this->initDraw(batchTarget, dfProcessor, pipeline); |
434 *instancesToFlush = 0; | |
435 | 419 |
436 SkDEBUGCODE(success =) atlas->addToAtlas(&id, batchTarget, width, he
ight, | 420 SkDEBUGCODE(success =) atlas->addToAtlas(&id, batchTarget, width, he
ight, |
437 dfStorage.get(), &atlasLoca
tion); | 421 dfStorage.get(), &atlasLoca
tion); |
438 SkASSERT(success); | 422 SkASSERT(success); |
439 | 423 |
440 } | 424 } |
441 | 425 |
442 // add to cache | 426 // add to cache |
443 pathData->fKey.fGenID = path.getGenerationID(); | 427 pathData->fKey.fGenID = path.getGenerationID(); |
444 pathData->fKey.fDimension = dimension; | 428 pathData->fKey.fDimension = dimension; |
(...skipping 15 matching lines...) Expand all Loading... |
460 pathData->fAtlasLocation = atlasLocation; | 444 pathData->fAtlasLocation = atlasLocation; |
461 | 445 |
462 fPathCache->add(pathData); | 446 fPathCache->add(pathData); |
463 fPathList->addToTail(pathData); | 447 fPathList->addToTail(pathData); |
464 #ifdef DF_PATH_TRACKING | 448 #ifdef DF_PATH_TRACKING |
465 ++g_NumCachedPaths; | 449 ++g_NumCachedPaths; |
466 #endif | 450 #endif |
467 return true; | 451 return true; |
468 } | 452 } |
469 | 453 |
470 void drawPath(GrBatchTarget* target, | 454 void writePathVertices(GrBatchTarget* target, |
471 GrBatchAtlas* atlas, | 455 GrBatchAtlas* atlas, |
472 const GrPipeline* pipeline, | 456 const GrPipeline* pipeline, |
473 const GrGeometryProcessor* gp, | 457 const GrGeometryProcessor* gp, |
474 SkPoint* positions, | 458 SkPoint* positions, |
475 size_t vertexStride, | 459 size_t vertexStride, |
476 const SkMatrix& viewMatrix, | 460 const SkMatrix& viewMatrix, |
477 const SkPath& path, | 461 const SkPath& path, |
478 const PathData* pathData) { | 462 const PathData* pathData) { |
479 GrTexture* texture = atlas->getTexture(); | 463 GrTexture* texture = atlas->getTexture(); |
480 | 464 |
481 SkScalar dx = pathData->fBounds.fLeft; | 465 SkScalar dx = pathData->fBounds.fLeft; |
482 SkScalar dy = pathData->fBounds.fTop; | 466 SkScalar dy = pathData->fBounds.fTop; |
483 SkScalar width = pathData->fBounds.width(); | 467 SkScalar width = pathData->fBounds.width(); |
484 SkScalar height = pathData->fBounds.height(); | 468 SkScalar height = pathData->fBounds.height(); |
485 | 469 |
486 SkScalar invScale = 1.0f / pathData->fScale; | 470 SkScalar invScale = 1.0f / pathData->fScale; |
487 dx *= invScale; | 471 dx *= invScale; |
488 dy *= invScale; | 472 dy *= invScale; |
(...skipping 26 matching lines...) Expand all Loading... |
515 | 499 |
516 // TODO remove this when batch is everywhere | 500 // TODO remove this when batch is everywhere |
517 GrPipelineInfo init; | 501 GrPipelineInfo init; |
518 init.fColorIgnored = fBatch.fColorIgnored; | 502 init.fColorIgnored = fBatch.fColorIgnored; |
519 init.fOverrideColor = GrColor_ILLEGAL; | 503 init.fOverrideColor = GrColor_ILLEGAL; |
520 init.fCoverageIgnored = fBatch.fCoverageIgnored; | 504 init.fCoverageIgnored = fBatch.fCoverageIgnored; |
521 init.fUsesLocalCoords = this->usesLocalCoords(); | 505 init.fUsesLocalCoords = this->usesLocalCoords(); |
522 dfProcessor->initBatchTracker(batchTarget->currentBatchTracker(), init); | 506 dfProcessor->initBatchTracker(batchTarget->currentBatchTracker(), init); |
523 } | 507 } |
524 | 508 |
525 void flush(GrBatchTarget* batchTarget, | 509 void flush(GrBatchTarget* batchTarget, FlushInfo* flushInfo) { |
526 GrDrawTarget::DrawInfo* drawInfo, | 510 GrDrawTarget::DrawInfo drawInfo; |
527 int instanceCount, | 511 int instancesToFlush = flushInfo->fInstancesToFlush; |
528 int maxInstancesPerDraw) { | 512 int maxInstancesPerDraw = flushInfo->fIndexBuffer->maxQuads(); |
529 while (instanceCount) { | 513 drawInfo.initInstanced(kTriangles_GrPrimitiveType, flushInfo->fVertexBuf
fer, |
530 drawInfo->setInstanceCount(SkTMin(instanceCount, maxInstancesPerDraw
)); | 514 flushInfo->fIndexBuffer, flushInfo->fVertexOffset, kVerticesPerQuad, |
531 drawInfo->setVertexCount(drawInfo->instanceCount() * drawInfo->verti
cesPerInstance()); | 515 kIndicesPerQuad, &instancesToFlush, maxInstancesPerDraw); |
532 drawInfo->setIndexCount(drawInfo->instanceCount() * drawInfo->indice
sPerInstance()); | 516 do { |
533 | 517 batchTarget->draw(drawInfo); |
534 batchTarget->draw(*drawInfo); | 518 } while (drawInfo.nextInstances(&instancesToFlush, maxInstancesPerDraw))
; |
535 | 519 flushInfo->fVertexOffset += kVerticesPerQuad * flushInfo->fInstancesToFl
ush; |
536 drawInfo->setStartVertex(drawInfo->startVertex() + drawInfo->vertexC
ount()); | 520 flushInfo->fInstancesToFlush = 0; |
537 instanceCount -= drawInfo->instanceCount(); | |
538 } | |
539 } | 521 } |
540 | 522 |
541 GrColor color() const { return fBatch.fColor; } | 523 GrColor color() const { return fBatch.fColor; } |
542 const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; } | 524 const SkMatrix& viewMatrix() const { return fBatch.fViewMatrix; } |
543 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } | 525 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
544 | 526 |
545 bool onCombineIfPossible(GrBatch* t) override { | 527 bool onCombineIfPossible(GrBatch* t) override { |
546 AADistanceFieldPathBatch* that = t->cast<AADistanceFieldPathBatch>(); | 528 AADistanceFieldPathBatch* that = t->cast<AADistanceFieldPathBatch>(); |
547 | 529 |
548 // TODO we could actually probably do a bunch of this work on the CPU, i
e map viewMatrix, | 530 // TODO we could actually probably do a bunch of this work on the CPU, i
e map viewMatrix, |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
614 geometry.fPath = path; | 596 geometry.fPath = path; |
615 geometry.fAntiAlias = antiAlias; | 597 geometry.fAntiAlias = antiAlias; |
616 | 598 |
617 SkAutoTUnref<GrBatch> batch(AADistanceFieldPathBatch::Create(geometry, color
, viewMatrix, | 599 SkAutoTUnref<GrBatch> batch(AADistanceFieldPathBatch::Create(geometry, color
, viewMatrix, |
618 fAtlas, &fPathC
ache, &fPathList)); | 600 fAtlas, &fPathC
ache, &fPathList)); |
619 target->drawBatch(pipelineBuilder, batch); | 601 target->drawBatch(pipelineBuilder, batch); |
620 | 602 |
621 return true; | 603 return true; |
622 } | 604 } |
623 | 605 |
OLD | NEW |