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 |
(...skipping 20 matching lines...) Expand all Loading... |
31 #define NUM_PLOTS_X (ATLAS_TEXTURE_WIDTH / PLOT_WIDTH) | 31 #define NUM_PLOTS_X (ATLAS_TEXTURE_WIDTH / PLOT_WIDTH) |
32 #define NUM_PLOTS_Y (ATLAS_TEXTURE_HEIGHT / PLOT_HEIGHT) | 32 #define NUM_PLOTS_Y (ATLAS_TEXTURE_HEIGHT / PLOT_HEIGHT) |
33 | 33 |
34 #ifdef DF_PATH_TRACKING | 34 #ifdef DF_PATH_TRACKING |
35 static int g_NumCachedPaths = 0; | 35 static int g_NumCachedPaths = 0; |
36 static int g_NumFreedPaths = 0; | 36 static int g_NumFreedPaths = 0; |
37 #endif | 37 #endif |
38 | 38 |
39 // mip levels | 39 // mip levels |
40 static const int kSmallMIP = 32; | 40 static const int kSmallMIP = 32; |
41 static const int kMediumMIP = 73; | 41 static const int kMediumMIP = 72; |
42 static const int kLargeMIP = 162; | 42 static const int kLargeMIP = 162; |
43 | 43 |
44 // Callback to clear out internal path cache when eviction occurs | 44 // Callback to clear out internal path cache when eviction occurs |
45 void GrAADistanceFieldPathRenderer::HandleEviction(GrBatchAtlas::AtlasID id, voi
d* pr) { | 45 void GrAADistanceFieldPathRenderer::HandleEviction(GrBatchAtlas::AtlasID id, voi
d* pr) { |
46 GrAADistanceFieldPathRenderer* dfpr = (GrAADistanceFieldPathRenderer*)pr; | 46 GrAADistanceFieldPathRenderer* dfpr = (GrAADistanceFieldPathRenderer*)pr; |
47 // remove any paths that use this plot | 47 // remove any paths that use this plot |
48 PathDataList::Iter iter; | 48 PathDataList::Iter iter; |
49 iter.init(dfpr->fPathList, PathDataList::Iter::kHead_IterStart); | 49 iter.init(dfpr->fPathList, PathDataList::Iter::kHead_IterStart); |
50 PathData* pathData; | 50 PathData* pathData; |
51 while ((pathData = iter.get())) { | 51 while ((pathData = iter.get())) { |
(...skipping 25 matching lines...) Expand all Loading... |
77 | 77 |
78 #ifdef DF_PATH_TRACKING | 78 #ifdef DF_PATH_TRACKING |
79 SkDebugf("Cached paths: %d, freed paths: %d\n", g_NumCachedPaths, g_NumFreed
Paths); | 79 SkDebugf("Cached paths: %d, freed paths: %d\n", g_NumCachedPaths, g_NumFreed
Paths); |
80 #endif | 80 #endif |
81 } | 81 } |
82 | 82 |
83 //////////////////////////////////////////////////////////////////////////////// | 83 //////////////////////////////////////////////////////////////////////////////// |
84 bool GrAADistanceFieldPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) c
onst { | 84 bool GrAADistanceFieldPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) c
onst { |
85 | 85 |
86 // TODO: Support inverse fill | 86 // TODO: Support inverse fill |
| 87 // TODO: Support strokes |
87 if (!args.fShaderCaps->shaderDerivativeSupport() || !args.fAntiAlias || | 88 if (!args.fShaderCaps->shaderDerivativeSupport() || !args.fAntiAlias || |
88 SkStrokeRec::kHairline_Style == args.fStroke->getStyle() || | 89 args.fPath->isInverseFillType() || args.fPath->isVolatile() || |
89 args.fPath->isInverseFillType() || args.fPath->isVolatile()) { | 90 !args.fStroke->isFillStyle()) { |
90 return false; | 91 return false; |
91 } | 92 } |
92 | 93 |
93 // currently don't support perspective | 94 // currently don't support perspective |
94 if (args.fViewMatrix->hasPerspective()) { | 95 if (args.fViewMatrix->hasPerspective()) { |
95 return false; | 96 return false; |
96 } | 97 } |
97 | 98 |
98 // only support paths with bounds within kMediumMIP by kMediumMIP, | 99 // only support paths smaller than 64x64, scaled to less than 256x256 |
99 // scaled to have bounds within 2.0f*kLargeMIP by 2.0f*kLargeMIP | |
100 // the goal is to accelerate rendering of lots of small paths that may be sc
aling | 100 // the goal is to accelerate rendering of lots of small paths that may be sc
aling |
101 SkScalar maxScale = args.fViewMatrix->getMaxScale(); | 101 SkScalar maxScale = args.fViewMatrix->getMaxScale(); |
102 const SkRect& bounds = args.fPath->getBounds(); | 102 const SkRect& bounds = args.fPath->getBounds(); |
103 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height()); | 103 SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height()); |
104 // Approximate stroked size by adding the maximum of the stroke width or 2x
the miter limit | 104 return maxDim < 64.f && maxDim * maxScale < 256.f; |
105 if (!args.fStroke->isFillStyle()) { | |
106 SkScalar extraWidth = args.fStroke->getWidth(); | |
107 if (SkPaint::kMiter_Join == args.fStroke->getJoin()) { | |
108 extraWidth = SkTMax(extraWidth, 2.0f*args.fStroke->getMiter()); | |
109 } | |
110 maxDim += extraWidth; | |
111 } | |
112 | |
113 return maxDim <= kMediumMIP && maxDim * maxScale <= 2.0f*kLargeMIP; | |
114 } | 105 } |
115 | 106 |
116 //////////////////////////////////////////////////////////////////////////////// | 107 //////////////////////////////////////////////////////////////////////////////// |
117 | 108 |
118 // padding around path bounds to allow for antialiased pixels | 109 // padding around path bounds to allow for antialiased pixels |
119 static const SkScalar kAntiAliasPad = 1.0f; | 110 static const SkScalar kAntiAliasPad = 1.0f; |
120 | 111 |
121 class AADistanceFieldPathBatch : public GrVertexBatch { | 112 class AADistanceFieldPathBatch : public GrVertexBatch { |
122 public: | 113 public: |
123 DEFINE_BATCH_CLASS_ID | 114 DEFINE_BATCH_CLASS_ID |
124 | 115 |
125 typedef GrAADistanceFieldPathRenderer::PathData PathData; | 116 typedef GrAADistanceFieldPathRenderer::PathData PathData; |
126 typedef SkTDynamicHash<PathData, PathData::Key> PathCache; | 117 typedef SkTDynamicHash<PathData, PathData::Key> PathCache; |
127 typedef GrAADistanceFieldPathRenderer::PathDataList PathDataList; | 118 typedef GrAADistanceFieldPathRenderer::PathDataList PathDataList; |
128 | 119 |
129 struct Geometry { | 120 struct Geometry { |
130 Geometry(const SkStrokeRec& stroke) : fStroke(stroke) { | 121 Geometry(const SkStrokeRec& stroke) : fStroke(stroke) {} |
131 if (!stroke.needToApply()) { | |
132 // purify unused values to ensure binary equality | |
133 fStroke.setStrokeParams(SkPaint::kDefault_Cap, SkPaint::kDefault
_Join, | |
134 SkIntToScalar(4)); | |
135 if (fStroke.getWidth() < 0) { | |
136 fStroke.setStrokeStyle(-1.0f); | |
137 } | |
138 } | |
139 } | |
140 SkPath fPath; | 122 SkPath fPath; |
141 // The unique ID of the path involved in this draw. This may be differen
t than the ID | |
142 // in fPath since that path may have resulted from a SkStrokeRec::applyT
oPath call. | |
143 uint32_t fGenID; | |
144 SkStrokeRec fStroke; | 123 SkStrokeRec fStroke; |
145 bool fAntiAlias; | 124 bool fAntiAlias; |
146 PathData* fPathData; | 125 PathData* fPathData; |
147 }; | 126 }; |
148 | 127 |
149 static GrDrawBatch* Create(const Geometry& geometry, GrColor color, const Sk
Matrix& viewMatrix, | 128 static GrDrawBatch* Create(const Geometry& geometry, GrColor color, const Sk
Matrix& viewMatrix, |
150 GrBatchAtlas* atlas, PathCache* pathCache, PathDa
taList* pathList) { | 129 GrBatchAtlas* atlas, PathCache* pathCache, PathDa
taList* pathList) { |
151 return new AADistanceFieldPathBatch(geometry, color, viewMatrix, atlas,
pathCache, | 130 return new AADistanceFieldPathBatch(geometry, color, viewMatrix, atlas,
pathCache, |
152 pathList); | 131 pathList); |
153 } | 132 } |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 uint32_t desiredDimension; | 218 uint32_t desiredDimension; |
240 if (size <= kSmallMIP) { | 219 if (size <= kSmallMIP) { |
241 desiredDimension = kSmallMIP; | 220 desiredDimension = kSmallMIP; |
242 } else if (size <= kMediumMIP) { | 221 } else if (size <= kMediumMIP) { |
243 desiredDimension = kMediumMIP; | 222 desiredDimension = kMediumMIP; |
244 } else { | 223 } else { |
245 desiredDimension = kLargeMIP; | 224 desiredDimension = kLargeMIP; |
246 } | 225 } |
247 | 226 |
248 // check to see if path is cached | 227 // check to see if path is cached |
249 PathData::Key key(args.fGenID, desiredDimension, args.fStroke); | 228 // TODO: handle stroked vs. filled version of same path |
| 229 PathData::Key key = { args.fPath.getGenerationID(), desiredDimension
}; |
250 args.fPathData = fPathCache->find(key); | 230 args.fPathData = fPathCache->find(key); |
251 if (nullptr == args.fPathData || !atlas->hasID(args.fPathData->fID))
{ | 231 if (nullptr == args.fPathData || !atlas->hasID(args.fPathData->fID))
{ |
252 // Remove the stale cache entry | 232 // Remove the stale cache entry |
253 if (args.fPathData) { | 233 if (args.fPathData) { |
254 fPathCache->remove(args.fPathData->fKey); | 234 fPathCache->remove(args.fPathData->fKey); |
255 fPathList->remove(args.fPathData); | 235 fPathList->remove(args.fPathData); |
256 delete args.fPathData; | 236 delete args.fPathData; |
257 } | 237 } |
258 SkScalar scale = desiredDimension/maxDim; | 238 SkScalar scale = desiredDimension/maxDim; |
259 args.fPathData = new PathData; | 239 args.fPathData = new PathData; |
260 if (!this->addPathToAtlas(target, | 240 if (!this->addPathToAtlas(target, |
261 dfProcessor, | 241 dfProcessor, |
262 this->pipeline(), | 242 this->pipeline(), |
263 &flushInfo, | 243 &flushInfo, |
264 atlas, | 244 atlas, |
265 args.fPathData, | 245 args.fPathData, |
266 args.fPath, | 246 args.fPath, |
267 args.fGenID, | |
268 args.fStroke, | 247 args.fStroke, |
269 args.fAntiAlias, | 248 args.fAntiAlias, |
270 desiredDimension, | 249 desiredDimension, |
271 scale)) { | 250 scale)) { |
272 SkDebugf("Can't rasterize path\n"); | 251 SkDebugf("Can't rasterize path\n"); |
273 return; | 252 return; |
274 } | 253 } |
275 } | 254 } |
276 | 255 |
277 atlas->setLastUseToken(args.fPathData->fID, target->currentToken()); | 256 atlas->setLastUseToken(args.fPathData->fID, target->currentToken()); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 viewMatrix.mapRect(&fBounds); | 294 viewMatrix.mapRect(&fBounds); |
316 } | 295 } |
317 | 296 |
318 bool addPathToAtlas(GrVertexBatch::Target* target, | 297 bool addPathToAtlas(GrVertexBatch::Target* target, |
319 const GrGeometryProcessor* dfProcessor, | 298 const GrGeometryProcessor* dfProcessor, |
320 const GrPipeline* pipeline, | 299 const GrPipeline* pipeline, |
321 FlushInfo* flushInfo, | 300 FlushInfo* flushInfo, |
322 GrBatchAtlas* atlas, | 301 GrBatchAtlas* atlas, |
323 PathData* pathData, | 302 PathData* pathData, |
324 const SkPath& path, | 303 const SkPath& path, |
325 uint32_t genID, | 304 const SkStrokeRec& |
326 const SkStrokeRec& stroke, | 305 stroke, bool antiAlias, |
327 bool antiAlias, | |
328 uint32_t dimension, | 306 uint32_t dimension, |
329 SkScalar scale) { | 307 SkScalar scale) { |
330 const SkRect& bounds = path.getBounds(); | 308 const SkRect& bounds = path.getBounds(); |
331 | 309 |
332 // generate bounding rect for bitmap draw | 310 // generate bounding rect for bitmap draw |
333 SkRect scaledBounds = bounds; | 311 SkRect scaledBounds = bounds; |
334 // scale to mip level size | 312 // scale to mip level size |
335 scaledBounds.fLeft *= scale; | 313 scaledBounds.fLeft *= scale; |
336 scaledBounds.fTop *= scale; | 314 scaledBounds.fTop *= scale; |
337 scaledBounds.fRight *= scale; | 315 scaledBounds.fRight *= scale; |
(...skipping 10 matching lines...) Expand all Loading... |
348 // move origin to upper left corner | 326 // move origin to upper left corner |
349 devPathBounds.offsetTo(0,0); | 327 devPathBounds.offsetTo(0,0); |
350 | 328 |
351 // draw path to bitmap | 329 // draw path to bitmap |
352 SkMatrix drawMatrix; | 330 SkMatrix drawMatrix; |
353 drawMatrix.setTranslate(-bounds.left(), -bounds.top()); | 331 drawMatrix.setTranslate(-bounds.left(), -bounds.top()); |
354 drawMatrix.postScale(scale, scale); | 332 drawMatrix.postScale(scale, scale); |
355 drawMatrix.postTranslate(kAntiAliasPad, kAntiAliasPad); | 333 drawMatrix.postTranslate(kAntiAliasPad, kAntiAliasPad); |
356 | 334 |
357 // setup bitmap backing | 335 // setup bitmap backing |
358 SkASSERT(devPathBounds.fLeft == 0); | 336 // Now translate so the bound's UL corner is at the origin |
359 SkASSERT(devPathBounds.fTop == 0); | 337 drawMatrix.postTranslate(-devPathBounds.fLeft * SK_Scalar1, |
| 338 -devPathBounds.fTop * SK_Scalar1); |
| 339 SkIRect pathBounds = SkIRect::MakeWH(devPathBounds.width(), |
| 340 devPathBounds.height()); |
| 341 |
360 SkAutoPixmapStorage dst; | 342 SkAutoPixmapStorage dst; |
361 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(), | 343 if (!dst.tryAlloc(SkImageInfo::MakeA8(pathBounds.width(), |
362 devPathBounds.height()))) { | 344 pathBounds.height()))) { |
363 return false; | 345 return false; |
364 } | 346 } |
365 sk_bzero(dst.writable_addr(), dst.getSafeSize()); | 347 sk_bzero(dst.writable_addr(), dst.getSafeSize()); |
366 | 348 |
367 // rasterize path | 349 // rasterize path |
368 SkPaint paint; | 350 SkPaint paint; |
369 paint.setStyle(SkPaint::kFill_Style); | 351 if (stroke.isHairlineStyle()) { |
| 352 paint.setStyle(SkPaint::kStroke_Style); |
| 353 paint.setStrokeWidth(SK_Scalar1); |
| 354 } else { |
| 355 if (stroke.isFillStyle()) { |
| 356 paint.setStyle(SkPaint::kFill_Style); |
| 357 } else { |
| 358 paint.setStyle(SkPaint::kStroke_Style); |
| 359 paint.setStrokeJoin(stroke.getJoin()); |
| 360 paint.setStrokeCap(stroke.getCap()); |
| 361 paint.setStrokeWidth(stroke.getWidth()); |
| 362 } |
| 363 } |
370 paint.setAntiAlias(antiAlias); | 364 paint.setAntiAlias(antiAlias); |
371 | 365 |
372 SkDraw draw; | 366 SkDraw draw; |
373 sk_bzero(&draw, sizeof(draw)); | 367 sk_bzero(&draw, sizeof(draw)); |
374 | 368 |
375 SkRasterClip rasterClip; | 369 SkRasterClip rasterClip; |
376 rasterClip.setRect(devPathBounds); | 370 rasterClip.setRect(pathBounds); |
377 draw.fRC = &rasterClip; | 371 draw.fRC = &rasterClip; |
378 draw.fClip = &rasterClip.bwRgn(); | 372 draw.fClip = &rasterClip.bwRgn(); |
379 draw.fMatrix = &drawMatrix; | 373 draw.fMatrix = &drawMatrix; |
380 draw.fDst = dst; | 374 draw.fDst = dst; |
381 | 375 |
382 draw.drawPathCoverage(path, paint); | 376 draw.drawPathCoverage(path, paint); |
383 | 377 |
384 // generate signed distance field | 378 // generate signed distance field |
385 devPathBounds.outset(SK_DistanceFieldPad, SK_DistanceFieldPad); | 379 devPathBounds.outset(SK_DistanceFieldPad, SK_DistanceFieldPad); |
386 int width = devPathBounds.width(); | 380 int width = devPathBounds.width(); |
(...skipping 15 matching lines...) Expand all Loading... |
402 this->flush(target, flushInfo); | 396 this->flush(target, flushInfo); |
403 target->initDraw(dfProcessor, pipeline); | 397 target->initDraw(dfProcessor, pipeline); |
404 | 398 |
405 SkDEBUGCODE(success =) atlas->addToAtlas(&id, target, width, height, | 399 SkDEBUGCODE(success =) atlas->addToAtlas(&id, target, width, height, |
406 dfStorage.get(), &atlasLoca
tion); | 400 dfStorage.get(), &atlasLoca
tion); |
407 SkASSERT(success); | 401 SkASSERT(success); |
408 | 402 |
409 } | 403 } |
410 | 404 |
411 // add to cache | 405 // add to cache |
412 pathData->fKey = PathData::Key(genID, dimension, stroke); | 406 pathData->fKey.fGenID = path.getGenerationID(); |
| 407 pathData->fKey.fDimension = dimension; |
413 pathData->fScale = scale; | 408 pathData->fScale = scale; |
414 pathData->fID = id; | 409 pathData->fID = id; |
415 // change the scaled rect to match the size of the inset distance field | 410 // change the scaled rect to match the size of the inset distance field |
416 scaledBounds.fRight = scaledBounds.fLeft + | 411 scaledBounds.fRight = scaledBounds.fLeft + |
417 SkIntToScalar(devPathBounds.width() - 2*SK_DistanceFieldInset); | 412 SkIntToScalar(devPathBounds.width() - 2*SK_DistanceFieldInset); |
418 scaledBounds.fBottom = scaledBounds.fTop + | 413 scaledBounds.fBottom = scaledBounds.fTop + |
419 SkIntToScalar(devPathBounds.height() - 2*SK_DistanceFieldInset); | 414 SkIntToScalar(devPathBounds.height() - 2*SK_DistanceFieldInset); |
420 // shift the origin to the correct place relative to the distance field | 415 // shift the origin to the correct place relative to the distance field |
421 // need to also restore the fractional translation | 416 // need to also restore the fractional translation |
422 scaledBounds.offset(-SkIntToScalar(SK_DistanceFieldInset) - kAntiAliasPa
d + dx, | 417 scaledBounds.offset(-SkIntToScalar(SK_DistanceFieldInset) - kAntiAliasPa
d + dx, |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
541 ATLAS_TEXTURE_WIDTH, ATLAS_
TEXTURE_HEIGHT, | 536 ATLAS_TEXTURE_WIDTH, ATLAS_
TEXTURE_HEIGHT, |
542 NUM_PLOTS_X, NUM_PLOTS_Y, | 537 NUM_PLOTS_X, NUM_PLOTS_Y, |
543 &GrAADistanceFieldPathRende
rer::HandleEviction, | 538 &GrAADistanceFieldPathRende
rer::HandleEviction, |
544 (void*)this); | 539 (void*)this); |
545 if (!fAtlas) { | 540 if (!fAtlas) { |
546 return false; | 541 return false; |
547 } | 542 } |
548 } | 543 } |
549 | 544 |
550 AADistanceFieldPathBatch::Geometry geometry(*args.fStroke); | 545 AADistanceFieldPathBatch::Geometry geometry(*args.fStroke); |
551 if (SkStrokeRec::kFill_Style == args.fStroke->getStyle()) { | 546 geometry.fPath = *args.fPath; |
552 geometry.fPath = *args.fPath; | |
553 } else { | |
554 args.fStroke->applyToPath(&geometry.fPath, *args.fPath); | |
555 } | |
556 geometry.fAntiAlias = args.fAntiAlias; | 547 geometry.fAntiAlias = args.fAntiAlias; |
557 // Note: this is the generation ID of the _original_ path. When a new path i
s | 548 |
558 // generated due to stroking it is important that the original path's id is
used | |
559 // for caching. | |
560 geometry.fGenID = args.fPath->getGenerationID(); | |
561 | |
562 SkAutoTUnref<GrDrawBatch> batch(AADistanceFieldPathBatch::Create(geometry, a
rgs.fColor, | 549 SkAutoTUnref<GrDrawBatch> batch(AADistanceFieldPathBatch::Create(geometry, a
rgs.fColor, |
563 *args.fView
Matrix, fAtlas, | 550 *args.fView
Matrix, fAtlas, |
564 &fPathCache
, &fPathList)); | 551 &fPathCache
, &fPathList)); |
565 args.fTarget->drawBatch(*args.fPipelineBuilder, batch); | 552 args.fTarget->drawBatch(*args.fPipelineBuilder, batch); |
566 | 553 |
567 return true; | 554 return true; |
568 } | 555 } |
569 | 556 |
570 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 557 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
571 | 558 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
634 geometry.fPath = GrTest::TestPath(random); | 621 geometry.fPath = GrTest::TestPath(random); |
635 geometry.fAntiAlias = random->nextBool(); | 622 geometry.fAntiAlias = random->nextBool(); |
636 | 623 |
637 return AADistanceFieldPathBatch::Create(geometry, color, viewMatrix, | 624 return AADistanceFieldPathBatch::Create(geometry, color, viewMatrix, |
638 gTestStruct.fAtlas, | 625 gTestStruct.fAtlas, |
639 &gTestStruct.fPathCache, | 626 &gTestStruct.fPathCache, |
640 &gTestStruct.fPathList); | 627 &gTestStruct.fPathList); |
641 } | 628 } |
642 | 629 |
643 #endif | 630 #endif |
OLD | NEW |