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

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

Issue 1460873002: Add stroking support to distance field path renderer (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix Valgrind issue Created 5 years 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 | « src/gpu/batches/GrAADistanceFieldPathRenderer.h ('k') | 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 /* 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
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 = 72; 41 static const int kMediumMIP = 73;
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
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
88 if (!args.fShaderCaps->shaderDerivativeSupport() || !args.fAntiAlias || 87 if (!args.fShaderCaps->shaderDerivativeSupport() || !args.fAntiAlias ||
89 args.fPath->isInverseFillType() || args.fPath->isVolatile() || 88 SkStrokeRec::kHairline_Style == args.fStroke->getStyle() ||
90 !args.fStroke->isFillStyle()) { 89 args.fPath->isInverseFillType() || args.fPath->isVolatile()) {
91 return false; 90 return false;
92 } 91 }
93 92
94 // currently don't support perspective 93 // currently don't support perspective
95 if (args.fViewMatrix->hasPerspective()) { 94 if (args.fViewMatrix->hasPerspective()) {
96 return false; 95 return false;
97 } 96 }
98 97
99 // only support paths smaller than 64x64, scaled to less than 256x256 98 // only support paths with bounds within kMediumMIP by kMediumMIP,
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 return maxDim < 64.f && maxDim * maxScale < 256.f; 104 // Approximate stroked size by adding the maximum of the stroke width or 2x the miter limit
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;
105 } 114 }
106 115
107 //////////////////////////////////////////////////////////////////////////////// 116 ////////////////////////////////////////////////////////////////////////////////
108 117
109 // padding around path bounds to allow for antialiased pixels 118 // padding around path bounds to allow for antialiased pixels
110 static const SkScalar kAntiAliasPad = 1.0f; 119 static const SkScalar kAntiAliasPad = 1.0f;
111 120
112 class AADistanceFieldPathBatch : public GrVertexBatch { 121 class AADistanceFieldPathBatch : public GrVertexBatch {
113 public: 122 public:
114 DEFINE_BATCH_CLASS_ID 123 DEFINE_BATCH_CLASS_ID
115 124
116 typedef GrAADistanceFieldPathRenderer::PathData PathData; 125 typedef GrAADistanceFieldPathRenderer::PathData PathData;
117 typedef SkTDynamicHash<PathData, PathData::Key> PathCache; 126 typedef SkTDynamicHash<PathData, PathData::Key> PathCache;
118 typedef GrAADistanceFieldPathRenderer::PathDataList PathDataList; 127 typedef GrAADistanceFieldPathRenderer::PathDataList PathDataList;
119 128
120 struct Geometry { 129 struct Geometry {
121 Geometry(const SkStrokeRec& stroke) : fStroke(stroke) {} 130 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 }
122 SkPath fPath; 140 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;
123 SkStrokeRec fStroke; 144 SkStrokeRec fStroke;
124 bool fAntiAlias; 145 bool fAntiAlias;
125 PathData* fPathData; 146 PathData* fPathData;
126 }; 147 };
127 148
128 static GrDrawBatch* Create(const Geometry& geometry, GrColor color, const Sk Matrix& viewMatrix, 149 static GrDrawBatch* Create(const Geometry& geometry, GrColor color, const Sk Matrix& viewMatrix,
129 GrBatchAtlas* atlas, PathCache* pathCache, PathDa taList* pathList) { 150 GrBatchAtlas* atlas, PathCache* pathCache, PathDa taList* pathList) {
130 return new AADistanceFieldPathBatch(geometry, color, viewMatrix, atlas, pathCache, 151 return new AADistanceFieldPathBatch(geometry, color, viewMatrix, atlas, pathCache,
131 pathList); 152 pathList);
132 } 153 }
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 uint32_t desiredDimension; 239 uint32_t desiredDimension;
219 if (size <= kSmallMIP) { 240 if (size <= kSmallMIP) {
220 desiredDimension = kSmallMIP; 241 desiredDimension = kSmallMIP;
221 } else if (size <= kMediumMIP) { 242 } else if (size <= kMediumMIP) {
222 desiredDimension = kMediumMIP; 243 desiredDimension = kMediumMIP;
223 } else { 244 } else {
224 desiredDimension = kLargeMIP; 245 desiredDimension = kLargeMIP;
225 } 246 }
226 247
227 // check to see if path is cached 248 // check to see if path is cached
228 // TODO: handle stroked vs. filled version of same path 249 PathData::Key key(args.fGenID, desiredDimension, args.fStroke);
229 PathData::Key key = { args.fPath.getGenerationID(), desiredDimension };
230 args.fPathData = fPathCache->find(key); 250 args.fPathData = fPathCache->find(key);
231 if (nullptr == args.fPathData || !atlas->hasID(args.fPathData->fID)) { 251 if (nullptr == args.fPathData || !atlas->hasID(args.fPathData->fID)) {
232 // Remove the stale cache entry 252 // Remove the stale cache entry
233 if (args.fPathData) { 253 if (args.fPathData) {
234 fPathCache->remove(args.fPathData->fKey); 254 fPathCache->remove(args.fPathData->fKey);
235 fPathList->remove(args.fPathData); 255 fPathList->remove(args.fPathData);
236 delete args.fPathData; 256 delete args.fPathData;
237 } 257 }
238 SkScalar scale = desiredDimension/maxDim; 258 SkScalar scale = desiredDimension/maxDim;
239 args.fPathData = new PathData; 259 args.fPathData = new PathData;
240 if (!this->addPathToAtlas(target, 260 if (!this->addPathToAtlas(target,
241 dfProcessor, 261 dfProcessor,
242 this->pipeline(), 262 this->pipeline(),
243 &flushInfo, 263 &flushInfo,
244 atlas, 264 atlas,
245 args.fPathData, 265 args.fPathData,
246 args.fPath, 266 args.fPath,
267 args.fGenID,
247 args.fStroke, 268 args.fStroke,
248 args.fAntiAlias, 269 args.fAntiAlias,
249 desiredDimension, 270 desiredDimension,
250 scale)) { 271 scale)) {
251 SkDebugf("Can't rasterize path\n"); 272 SkDebugf("Can't rasterize path\n");
252 return; 273 return;
253 } 274 }
254 } 275 }
255 276
256 atlas->setLastUseToken(args.fPathData->fID, target->currentToken()); 277 atlas->setLastUseToken(args.fPathData->fID, target->currentToken());
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 viewMatrix.mapRect(&fBounds); 315 viewMatrix.mapRect(&fBounds);
295 } 316 }
296 317
297 bool addPathToAtlas(GrVertexBatch::Target* target, 318 bool addPathToAtlas(GrVertexBatch::Target* target,
298 const GrGeometryProcessor* dfProcessor, 319 const GrGeometryProcessor* dfProcessor,
299 const GrPipeline* pipeline, 320 const GrPipeline* pipeline,
300 FlushInfo* flushInfo, 321 FlushInfo* flushInfo,
301 GrBatchAtlas* atlas, 322 GrBatchAtlas* atlas,
302 PathData* pathData, 323 PathData* pathData,
303 const SkPath& path, 324 const SkPath& path,
304 const SkStrokeRec& 325 uint32_t genID,
305 stroke, bool antiAlias, 326 const SkStrokeRec& stroke,
327 bool antiAlias,
306 uint32_t dimension, 328 uint32_t dimension,
307 SkScalar scale) { 329 SkScalar scale) {
308 const SkRect& bounds = path.getBounds(); 330 const SkRect& bounds = path.getBounds();
309 331
310 // generate bounding rect for bitmap draw 332 // generate bounding rect for bitmap draw
311 SkRect scaledBounds = bounds; 333 SkRect scaledBounds = bounds;
312 // scale to mip level size 334 // scale to mip level size
313 scaledBounds.fLeft *= scale; 335 scaledBounds.fLeft *= scale;
314 scaledBounds.fTop *= scale; 336 scaledBounds.fTop *= scale;
315 scaledBounds.fRight *= scale; 337 scaledBounds.fRight *= scale;
(...skipping 10 matching lines...) Expand all
326 // move origin to upper left corner 348 // move origin to upper left corner
327 devPathBounds.offsetTo(0,0); 349 devPathBounds.offsetTo(0,0);
328 350
329 // draw path to bitmap 351 // draw path to bitmap
330 SkMatrix drawMatrix; 352 SkMatrix drawMatrix;
331 drawMatrix.setTranslate(-bounds.left(), -bounds.top()); 353 drawMatrix.setTranslate(-bounds.left(), -bounds.top());
332 drawMatrix.postScale(scale, scale); 354 drawMatrix.postScale(scale, scale);
333 drawMatrix.postTranslate(kAntiAliasPad, kAntiAliasPad); 355 drawMatrix.postTranslate(kAntiAliasPad, kAntiAliasPad);
334 356
335 // setup bitmap backing 357 // setup bitmap backing
336 // Now translate so the bound's UL corner is at the origin 358 SkASSERT(devPathBounds.fLeft == 0);
337 drawMatrix.postTranslate(-devPathBounds.fLeft * SK_Scalar1, 359 SkASSERT(devPathBounds.fTop == 0);
338 -devPathBounds.fTop * SK_Scalar1);
339 SkIRect pathBounds = SkIRect::MakeWH(devPathBounds.width(),
340 devPathBounds.height());
341
342 SkAutoPixmapStorage dst; 360 SkAutoPixmapStorage dst;
343 if (!dst.tryAlloc(SkImageInfo::MakeA8(pathBounds.width(), 361 if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
344 pathBounds.height()))) { 362 devPathBounds.height()))) {
345 return false; 363 return false;
346 } 364 }
347 sk_bzero(dst.writable_addr(), dst.getSafeSize()); 365 sk_bzero(dst.writable_addr(), dst.getSafeSize());
348 366
349 // rasterize path 367 // rasterize path
350 SkPaint paint; 368 SkPaint paint;
351 if (stroke.isHairlineStyle()) { 369 paint.setStyle(SkPaint::kFill_Style);
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 }
364 paint.setAntiAlias(antiAlias); 370 paint.setAntiAlias(antiAlias);
365 371
366 SkDraw draw; 372 SkDraw draw;
367 sk_bzero(&draw, sizeof(draw)); 373 sk_bzero(&draw, sizeof(draw));
368 374
369 SkRasterClip rasterClip; 375 SkRasterClip rasterClip;
370 rasterClip.setRect(pathBounds); 376 rasterClip.setRect(devPathBounds);
371 draw.fRC = &rasterClip; 377 draw.fRC = &rasterClip;
372 draw.fClip = &rasterClip.bwRgn(); 378 draw.fClip = &rasterClip.bwRgn();
373 draw.fMatrix = &drawMatrix; 379 draw.fMatrix = &drawMatrix;
374 draw.fDst = dst; 380 draw.fDst = dst;
375 381
376 draw.drawPathCoverage(path, paint); 382 draw.drawPathCoverage(path, paint);
377 383
378 // generate signed distance field 384 // generate signed distance field
379 devPathBounds.outset(SK_DistanceFieldPad, SK_DistanceFieldPad); 385 devPathBounds.outset(SK_DistanceFieldPad, SK_DistanceFieldPad);
380 int width = devPathBounds.width(); 386 int width = devPathBounds.width();
(...skipping 15 matching lines...) Expand all
396 this->flush(target, flushInfo); 402 this->flush(target, flushInfo);
397 target->initDraw(dfProcessor, pipeline); 403 target->initDraw(dfProcessor, pipeline);
398 404
399 SkDEBUGCODE(success =) atlas->addToAtlas(&id, target, width, height, 405 SkDEBUGCODE(success =) atlas->addToAtlas(&id, target, width, height,
400 dfStorage.get(), &atlasLoca tion); 406 dfStorage.get(), &atlasLoca tion);
401 SkASSERT(success); 407 SkASSERT(success);
402 408
403 } 409 }
404 410
405 // add to cache 411 // add to cache
406 pathData->fKey.fGenID = path.getGenerationID(); 412 pathData->fKey = PathData::Key(genID, dimension, stroke);
407 pathData->fKey.fDimension = dimension;
408 pathData->fScale = scale; 413 pathData->fScale = scale;
409 pathData->fID = id; 414 pathData->fID = id;
410 // change the scaled rect to match the size of the inset distance field 415 // change the scaled rect to match the size of the inset distance field
411 scaledBounds.fRight = scaledBounds.fLeft + 416 scaledBounds.fRight = scaledBounds.fLeft +
412 SkIntToScalar(devPathBounds.width() - 2*SK_DistanceFieldInset); 417 SkIntToScalar(devPathBounds.width() - 2*SK_DistanceFieldInset);
413 scaledBounds.fBottom = scaledBounds.fTop + 418 scaledBounds.fBottom = scaledBounds.fTop +
414 SkIntToScalar(devPathBounds.height() - 2*SK_DistanceFieldInset); 419 SkIntToScalar(devPathBounds.height() - 2*SK_DistanceFieldInset);
415 // shift the origin to the correct place relative to the distance field 420 // shift the origin to the correct place relative to the distance field
416 // need to also restore the fractional translation 421 // need to also restore the fractional translation
417 scaledBounds.offset(-SkIntToScalar(SK_DistanceFieldInset) - kAntiAliasPa d + dx, 422 scaledBounds.offset(-SkIntToScalar(SK_DistanceFieldInset) - kAntiAliasPa d + dx,
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 ATLAS_TEXTURE_WIDTH, ATLAS_ TEXTURE_HEIGHT, 541 ATLAS_TEXTURE_WIDTH, ATLAS_ TEXTURE_HEIGHT,
537 NUM_PLOTS_X, NUM_PLOTS_Y, 542 NUM_PLOTS_X, NUM_PLOTS_Y,
538 &GrAADistanceFieldPathRende rer::HandleEviction, 543 &GrAADistanceFieldPathRende rer::HandleEviction,
539 (void*)this); 544 (void*)this);
540 if (!fAtlas) { 545 if (!fAtlas) {
541 return false; 546 return false;
542 } 547 }
543 } 548 }
544 549
545 AADistanceFieldPathBatch::Geometry geometry(*args.fStroke); 550 AADistanceFieldPathBatch::Geometry geometry(*args.fStroke);
546 geometry.fPath = *args.fPath; 551 if (SkStrokeRec::kFill_Style == args.fStroke->getStyle()) {
552 geometry.fPath = *args.fPath;
553 } else {
554 args.fStroke->applyToPath(&geometry.fPath, *args.fPath);
555 }
547 geometry.fAntiAlias = args.fAntiAlias; 556 geometry.fAntiAlias = args.fAntiAlias;
548 557 // Note: this is the generation ID of the _original_ path. When a new path i s
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
549 SkAutoTUnref<GrDrawBatch> batch(AADistanceFieldPathBatch::Create(geometry, a rgs.fColor, 562 SkAutoTUnref<GrDrawBatch> batch(AADistanceFieldPathBatch::Create(geometry, a rgs.fColor,
550 *args.fView Matrix, fAtlas, 563 *args.fView Matrix, fAtlas,
551 &fPathCache , &fPathList)); 564 &fPathCache , &fPathList));
552 args.fTarget->drawBatch(*args.fPipelineBuilder, batch); 565 args.fTarget->drawBatch(*args.fPipelineBuilder, batch);
553 566
554 return true; 567 return true;
555 } 568 }
556 569
557 //////////////////////////////////////////////////////////////////////////////// /////////////////// 570 //////////////////////////////////////////////////////////////////////////////// ///////////////////
558 571
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
613 &PathTestStruct::HandleEvic tion, 626 &PathTestStruct::HandleEvic tion,
614 (void*)&gTestStruct); 627 (void*)&gTestStruct);
615 } 628 }
616 629
617 SkMatrix viewMatrix = GrTest::TestMatrix(random); 630 SkMatrix viewMatrix = GrTest::TestMatrix(random);
618 GrColor color = GrRandomColor(random); 631 GrColor color = GrRandomColor(random);
619 632
620 AADistanceFieldPathBatch::Geometry geometry(GrTest::TestStrokeRec(random)); 633 AADistanceFieldPathBatch::Geometry geometry(GrTest::TestStrokeRec(random));
621 geometry.fPath = GrTest::TestPath(random); 634 geometry.fPath = GrTest::TestPath(random);
622 geometry.fAntiAlias = random->nextBool(); 635 geometry.fAntiAlias = random->nextBool();
636 geometry.fGenID = random->nextU();
623 637
624 return AADistanceFieldPathBatch::Create(geometry, color, viewMatrix, 638 return AADistanceFieldPathBatch::Create(geometry, color, viewMatrix,
625 gTestStruct.fAtlas, 639 gTestStruct.fAtlas,
626 &gTestStruct.fPathCache, 640 &gTestStruct.fPathCache,
627 &gTestStruct.fPathList); 641 &gTestStruct.fPathList);
628 } 642 }
629 643
630 #endif 644 #endif
OLDNEW
« no previous file with comments | « src/gpu/batches/GrAADistanceFieldPathRenderer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698