1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
|
/*
* Copyright 2014 Google Inc.
* Copyright 2017 ARM Ltd.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrSmallPathRenderer.h"
#include "GrBuffer.h"
#include "GrContext.h"
#include "GrDistanceFieldGenFromVector.h"
#include "GrDrawOpTest.h"
#include "GrResourceProvider.h"
#include "GrSimpleMeshDrawOpHelper.h"
#include "SkAutoMalloc.h"
#include "SkAutoPixmapStorage.h"
#include "SkDistanceFieldGen.h"
#include "SkRasterClip.h"
#include "effects/GrBitmapTextGeoProc.h"
#include "effects/GrDistanceFieldGeoProc.h"
#include "ops/GrMeshDrawOp.h"
#define ATLAS_TEXTURE_WIDTH 2048
#define ATLAS_TEXTURE_HEIGHT 2048
#define PLOT_WIDTH 512
#define PLOT_HEIGHT 256
#define NUM_PLOTS_X (ATLAS_TEXTURE_WIDTH / PLOT_WIDTH)
#define NUM_PLOTS_Y (ATLAS_TEXTURE_HEIGHT / PLOT_HEIGHT)
#ifdef DF_PATH_TRACKING
static int g_NumCachedShapes = 0;
static int g_NumFreedShapes = 0;
#endif
// mip levels
static const SkScalar kIdealMinMIP = 12;
static const SkScalar kMaxMIP = 162;
static const SkScalar kMaxDim = 73;
static const SkScalar kMinSize = SK_ScalarHalf;
static const SkScalar kMaxSize = 2*kMaxMIP;
// Callback to clear out internal path cache when eviction occurs
void GrSmallPathRenderer::HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) {
GrSmallPathRenderer* dfpr = (GrSmallPathRenderer*)pr;
// remove any paths that use this plot
ShapeDataList::Iter iter;
iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart);
ShapeData* shapeData;
while ((shapeData = iter.get())) {
iter.next();
if (id == shapeData->fID) {
dfpr->fShapeCache.remove(shapeData->fKey);
dfpr->fShapeList.remove(shapeData);
delete shapeData;
#ifdef DF_PATH_TRACKING
++g_NumFreedPaths;
#endif
}
}
}
////////////////////////////////////////////////////////////////////////////////
GrSmallPathRenderer::GrSmallPathRenderer() : fAtlas(nullptr) {}
GrSmallPathRenderer::~GrSmallPathRenderer() {
ShapeDataList::Iter iter;
iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
ShapeData* shapeData;
while ((shapeData = iter.get())) {
iter.next();
delete shapeData;
}
#ifdef DF_PATH_TRACKING
SkDebugf("Cached shapes: %d, freed shapes: %d\n", g_NumCachedShapes, g_NumFreedShapes);
#endif
}
////////////////////////////////////////////////////////////////////////////////
GrPathRenderer::CanDrawPath GrSmallPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
if (!args.fCaps->shaderCaps()->shaderDerivativeSupport()) {
return CanDrawPath::kNo;
}
// If the shape has no key then we won't get any reuse.
if (!args.fShape->hasUnstyledKey()) {
return CanDrawPath::kNo;
}
// This only supports filled paths, however, the caller may apply the style to make a filled
// path and try again.
if (!args.fShape->style().isSimpleFill()) {
return CanDrawPath::kNo;
}
// This does non-inverse coverage-based antialiased fills.
if (GrAAType::kCoverage != args.fAAType) {
return CanDrawPath::kNo;
}
// TODO: Support inverse fill
if (args.fShape->inverseFilled()) {
return CanDrawPath::kNo;
}
// currently don't support perspective
if (args.fViewMatrix->hasPerspective()) {
return CanDrawPath::kNo;
}
// Only support paths with bounds within kMaxDim by kMaxDim,
// scaled to have bounds within kMaxSize by kMaxSize.
// The goal is to accelerate rendering of lots of small paths that may be scaling.
SkScalar scaleFactors[2];
if (!args.fViewMatrix->getMinMaxScales(scaleFactors)) {
return CanDrawPath::kNo;
}
SkRect bounds = args.fShape->styledBounds();
SkScalar minDim = SkMinScalar(bounds.width(), bounds.height());
SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
SkScalar minSize = minDim * SkScalarAbs(scaleFactors[0]);
SkScalar maxSize = maxDim * SkScalarAbs(scaleFactors[1]);
if (maxDim > kMaxDim || kMinSize > minSize || maxSize > kMaxSize) {
return CanDrawPath::kNo;
}
return CanDrawPath::kYes;
}
////////////////////////////////////////////////////////////////////////////////
// padding around path bounds to allow for antialiased pixels
static const SkScalar kAntiAliasPad = 1.0f;
class GrSmallPathRenderer::SmallPathOp final : public GrMeshDrawOp {
private:
using Helper = GrSimpleMeshDrawOpHelperWithStencil;
public:
DEFINE_OP_CLASS_ID
using ShapeData = GrSmallPathRenderer::ShapeData;
using ShapeCache = SkTDynamicHash<ShapeData, ShapeData::Key>;
using ShapeDataList = GrSmallPathRenderer::ShapeDataList;
static std::unique_ptr<GrDrawOp> Make(GrPaint&& paint, const GrShape& shape,
const SkMatrix& viewMatrix, GrDrawOpAtlas* atlas,
ShapeCache* shapeCache, ShapeDataList* shapeList,
bool gammaCorrect,
const GrUserStencilSettings* stencilSettings) {
return Helper::FactoryHelper<SmallPathOp>(std::move(paint), shape, viewMatrix, atlas,
shapeCache, shapeList, gammaCorrect,
stencilSettings);
}
SmallPathOp(Helper::MakeArgs helperArgs, GrColor color, const GrShape& shape,
const SkMatrix& viewMatrix, GrDrawOpAtlas* atlas, ShapeCache* shapeCache,
ShapeDataList* shapeList, bool gammaCorrect,
const GrUserStencilSettings* stencilSettings)
: INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) {
SkASSERT(shape.hasUnstyledKey());
// Compute bounds
this->setTransformedBounds(shape.bounds(), viewMatrix, HasAABloat::kYes, IsZeroArea::kNo);
#if defined(SK_BUILD_FOR_ANDROID) && !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK)
fUsesDistanceField = true;
#else
// only use distance fields on desktop and Android framework to save space in the atlas
fUsesDistanceField = this->bounds().width() > kMaxMIP || this->bounds().height() > kMaxMIP;
#endif
fViewMatrix = viewMatrix;
SkVector translate = SkVector::Make(0, 0);
if (!fUsesDistanceField) {
// In this case we don't apply a view matrix, so we need to remove the non-subpixel
// translation and add it back when we generate the quad for the path
SkScalar translateX = viewMatrix.getTranslateX();
SkScalar translateY = viewMatrix.getTranslateY();
translate = SkVector::Make(SkScalarFloorToScalar(translateX),
SkScalarFloorToScalar(translateY));
// Only store the fractional part of the translation in the view matrix
fViewMatrix.setTranslateX(translateX - translate.fX);
fViewMatrix.setTranslateY(translateY - translate.fY);
}
fShapes.emplace_back(Entry{color, shape, translate});
fAtlas = atlas;
fShapeCache = shapeCache;
fShapeList = shapeList;
fGammaCorrect = gammaCorrect;
}
const char* name() const override { return "SmallPathOp"; }
SkString dumpInfo() const override {
SkString string;
for (const auto& geo : fShapes) {
string.appendf("Color: 0x%08x\n", geo.fColor);
}
string += fHelper.dumpInfo();
string += INHERITED::dumpInfo();
return string;
}
FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
return fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kSingleChannel,
&fShapes.front().fColor);
}
private:
struct FlushInfo {
sk_sp<const GrBuffer> fVertexBuffer;
sk_sp<const GrBuffer> fIndexBuffer;
sk_sp<GrGeometryProcessor> fGeometryProcessor;
const GrPipeline* fPipeline;
int fVertexOffset;
int fInstancesToFlush;
};
void onPrepareDraws(Target* target) override {
int instanceCount = fShapes.count();
const SkMatrix& ctm = this->viewMatrix();
FlushInfo flushInfo;
flushInfo.fPipeline = fHelper.makePipeline(target);
// Setup GrGeometryProcessor
GrDrawOpAtlas* atlas = fAtlas;
if (fUsesDistanceField) {
uint32_t flags = 0;
flags |= ctm.isScaleTranslate() ? kScaleOnly_DistanceFieldEffectFlag : 0;
flags |= ctm.isSimilarity() ? kSimilarity_DistanceFieldEffectFlag : 0;
flags |= fGammaCorrect ? kGammaCorrect_DistanceFieldEffectFlag : 0;
flushInfo.fGeometryProcessor = GrDistanceFieldPathGeoProc::Make(
this->color(), this->viewMatrix(), atlas->getProxy(),
GrSamplerState::ClampBilerp(), flags, fHelper.usesLocalCoords());
} else {
SkMatrix invert;
if (fHelper.usesLocalCoords()) {
if (!this->viewMatrix().invert(&invert)) {
SkDebugf("Could not invert viewmatrix\n");
return;
}
// for local coords, we need to add the translation back in that we removed
// from the stored view matrix
invert.preTranslate(-fShapes[0].fTranslate.fX, -fShapes[0].fTranslate.fY);
}
flushInfo.fGeometryProcessor = GrBitmapTextGeoProc::Make(
this->color(), atlas->getProxy(), GrSamplerState::ClampNearest(),
kA8_GrMaskFormat, invert, fHelper.usesLocalCoords());
}
// allocate vertices
size_t vertexStride = flushInfo.fGeometryProcessor->getVertexStride();
SkASSERT(vertexStride == sizeof(SkPoint) + sizeof(GrColor) + 2*sizeof(uint16_t));
const GrBuffer* vertexBuffer;
void* vertices = target->makeVertexSpace(vertexStride,
kVerticesPerQuad * instanceCount,
&vertexBuffer,
&flushInfo.fVertexOffset);
flushInfo.fVertexBuffer.reset(SkRef(vertexBuffer));
flushInfo.fIndexBuffer.reset(target->resourceProvider()->refQuadIndexBuffer());
if (!vertices || !flushInfo.fIndexBuffer) {
SkDebugf("Could not allocate vertices\n");
return;
}
flushInfo.fInstancesToFlush = 0;
// Pointer to the next set of vertices to write.
intptr_t offset = reinterpret_cast<intptr_t>(vertices);
for (int i = 0; i < instanceCount; i++) {
const Entry& args = fShapes[i];
ShapeData* shapeData;
if (fUsesDistanceField) {
// get mip level
SkScalar maxScale = SkScalarAbs(this->viewMatrix().getMaxScale());
const SkRect& bounds = args.fShape.bounds();
SkScalar maxDim = SkMaxScalar(bounds.width(), bounds.height());
// We try to create the DF at a 2^n scaled path resolution (1/2, 1, 2, 4, etc.)
// In the majority of cases this will yield a crisper rendering.
SkScalar mipScale = 1.0f;
// Our mipscale is the maxScale clamped to the next highest power of 2
if (maxScale <= SK_ScalarHalf) {
SkScalar log = SkScalarFloorToScalar(SkScalarLog2(SkScalarInvert(maxScale)));
mipScale = SkScalarPow(2, -log);
} else if (maxScale > SK_Scalar1) {
SkScalar log = SkScalarCeilToScalar(SkScalarLog2(maxScale));
mipScale = SkScalarPow(2, log);
}
SkASSERT(maxScale <= mipScale);
SkScalar mipSize = mipScale*SkScalarAbs(maxDim);
// For sizes less than kIdealMinMIP we want to use as large a distance field as we can
// so we can preserve as much detail as possible. However, we can't scale down more
// than a 1/4 of the size without artifacts. So the idea is that we pick the mipsize
// just bigger than the ideal, and then scale down until we are no more than 4x the
// original mipsize.
if (mipSize < kIdealMinMIP) {
SkScalar newMipSize = mipSize;
do {
newMipSize *= 2;
} while (newMipSize < kIdealMinMIP);
while (newMipSize > 4 * mipSize) {
newMipSize *= 0.25f;
}
mipSize = newMipSize;
}
SkScalar desiredDimension = SkTMin(mipSize, kMaxMIP);
// check to see if df path is cached
ShapeData::Key key(args.fShape, SkScalarCeilToInt(desiredDimension));
shapeData = fShapeCache->find(key);
if (nullptr == shapeData || !atlas->hasID(shapeData->fID)) {
// Remove the stale cache entry
if (shapeData) {
fShapeCache->remove(shapeData->fKey);
fShapeList->remove(shapeData);
delete shapeData;
}
SkScalar scale = desiredDimension / maxDim;
shapeData = new ShapeData;
if (!this->addDFPathToAtlas(target,
&flushInfo,
atlas,
shapeData,
args.fShape,
SkScalarCeilToInt(desiredDimension),
scale)) {
delete shapeData;
continue;
}
}
} else {
// check to see if bitmap path is cached
ShapeData::Key key(args.fShape, this->viewMatrix());
shapeData = fShapeCache->find(key);
if (nullptr == shapeData || !atlas->hasID(shapeData->fID)) {
// Remove the stale cache entry
if (shapeData) {
fShapeCache->remove(shapeData->fKey);
fShapeList->remove(shapeData);
delete shapeData;
}
shapeData = new ShapeData;
if (!this->addBMPathToAtlas(target,
&flushInfo,
atlas,
shapeData,
args.fShape,
this->viewMatrix())) {
delete shapeData;
continue;
}
}
}
atlas->setLastUseToken(shapeData->fID, target->nextDrawToken());
this->writePathVertices(target,
atlas,
offset,
args.fColor,
vertexStride,
args.fTranslate,
shapeData);
offset += kVerticesPerQuad * vertexStride;
flushInfo.fInstancesToFlush++;
}
this->flush(target, &flushInfo);
}
bool addDFPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape,
uint32_t dimension, SkScalar scale) const {
const SkRect& bounds = shape.bounds();
// generate bounding rect for bitmap draw
SkRect scaledBounds = bounds;
// scale to mip level size
scaledBounds.fLeft *= scale;
scaledBounds.fTop *= scale;
scaledBounds.fRight *= scale;
scaledBounds.fBottom *= scale;
// subtract out integer portion of origin
// (SDF created will be placed with fractional offset burnt in)
SkScalar dx = SkScalarFloorToScalar(scaledBounds.fLeft);
SkScalar dy = SkScalarFloorToScalar(scaledBounds.fTop);
scaledBounds.offset(-dx, -dy);
// get integer boundary
SkIRect devPathBounds;
scaledBounds.roundOut(&devPathBounds);
// pad to allow room for antialiasing
const int intPad = SkScalarCeilToInt(kAntiAliasPad);
// place devBounds at origin
int width = devPathBounds.width() + 2*intPad;
int height = devPathBounds.height() + 2*intPad;
devPathBounds = SkIRect::MakeWH(width, height);
SkScalar translateX = intPad - dx;
SkScalar translateY = intPad - dy;
// draw path to bitmap
SkMatrix drawMatrix;
drawMatrix.setScale(scale, scale);
drawMatrix.postTranslate(translateX, translateY);
SkASSERT(devPathBounds.fLeft == 0);
SkASSERT(devPathBounds.fTop == 0);
SkASSERT(devPathBounds.width() > 0);
SkASSERT(devPathBounds.height() > 0);
// setup signed distance field storage
SkIRect dfBounds = devPathBounds.makeOutset(SK_DistanceFieldPad, SK_DistanceFieldPad);
width = dfBounds.width();
height = dfBounds.height();
// TODO We should really generate this directly into the plot somehow
SkAutoSMalloc<1024> dfStorage(width * height * sizeof(unsigned char));
SkPath path;
shape.asPath(&path);
#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
// Generate signed distance field directly from SkPath
bool succeed = GrGenerateDistanceFieldFromPath((unsigned char*)dfStorage.get(),
path, drawMatrix,
width, height, width * sizeof(unsigned char));
if (!succeed) {
#endif
// setup bitmap backing
SkAutoPixmapStorage dst;
if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
devPathBounds.height()))) {
return false;
}
sk_bzero(dst.writable_addr(), dst.getSafeSize());
// rasterize path
SkPaint paint;
paint.setStyle(SkPaint::kFill_Style);
paint.setAntiAlias(true);
SkDraw draw;
sk_bzero(&draw, sizeof(draw));
SkRasterClip rasterClip;
rasterClip.setRect(devPathBounds);
draw.fRC = &rasterClip;
draw.fMatrix = &drawMatrix;
draw.fDst = dst;
draw.drawPathCoverage(path, paint);
// Generate signed distance field
SkGenerateDistanceFieldFromA8Image((unsigned char*)dfStorage.get(),
(const unsigned char*)dst.addr(),
dst.width(), dst.height(), dst.rowBytes());
#ifndef SK_USE_LEGACY_DISTANCE_FIELDS
}
#endif
// add to atlas
SkIPoint16 atlasLocation;
GrDrawOpAtlas::AtlasID id;
if (!atlas->addToAtlas(&id, target, width, height, dfStorage.get(), &atlasLocation)) {
this->flush(target, flushInfo);
if (!atlas->addToAtlas(&id, target, width, height, dfStorage.get(), &atlasLocation)) {
return false;
}
}
// add to cache
shapeData->fKey.set(shape, dimension);
shapeData->fID = id;
shapeData->fBounds = SkRect::Make(devPathBounds);
shapeData->fBounds.offset(-translateX, -translateY);
shapeData->fBounds.fLeft /= scale;
shapeData->fBounds.fTop /= scale;
shapeData->fBounds.fRight /= scale;
shapeData->fBounds.fBottom /= scale;
shapeData->fTextureCoords.set((atlasLocation.fX+SK_DistanceFieldPad) << 1,
(atlasLocation.fY+SK_DistanceFieldPad) << 1,
(atlasLocation.fX+SK_DistanceFieldPad+
devPathBounds.width()) << 1,
(atlasLocation.fY+SK_DistanceFieldPad+
devPathBounds.height()) << 1);
fShapeCache->add(shapeData);
fShapeList->addToTail(shapeData);
#ifdef DF_PATH_TRACKING
++g_NumCachedPaths;
#endif
return true;
}
bool addBMPathToAtlas(GrMeshDrawOp::Target* target, FlushInfo* flushInfo,
GrDrawOpAtlas* atlas, ShapeData* shapeData, const GrShape& shape,
const SkMatrix& ctm) const {
const SkRect& bounds = shape.bounds();
if (bounds.isEmpty()) {
return false;
}
SkMatrix drawMatrix(ctm);
drawMatrix.set(SkMatrix::kMTransX, SkScalarFraction(ctm.get(SkMatrix::kMTransX)));
drawMatrix.set(SkMatrix::kMTransY, SkScalarFraction(ctm.get(SkMatrix::kMTransY)));
SkRect shapeDevBounds;
drawMatrix.mapRect(&shapeDevBounds, bounds);
SkScalar dx = SkScalarFloorToScalar(shapeDevBounds.fLeft);
SkScalar dy = SkScalarFloorToScalar(shapeDevBounds.fTop);
// get integer boundary
SkIRect devPathBounds;
shapeDevBounds.roundOut(&devPathBounds);
// pad to allow room for antialiasing
const int intPad = SkScalarCeilToInt(kAntiAliasPad);
// place devBounds at origin
int width = devPathBounds.width() + 2 * intPad;
int height = devPathBounds.height() + 2 * intPad;
devPathBounds = SkIRect::MakeWH(width, height);
SkScalar translateX = intPad - dx;
SkScalar translateY = intPad - dy;
SkASSERT(devPathBounds.fLeft == 0);
SkASSERT(devPathBounds.fTop == 0);
SkASSERT(devPathBounds.width() > 0);
SkASSERT(devPathBounds.height() > 0);
SkPath path;
shape.asPath(&path);
// setup bitmap backing
SkAutoPixmapStorage dst;
if (!dst.tryAlloc(SkImageInfo::MakeA8(devPathBounds.width(),
devPathBounds.height()))) {
return false;
}
sk_bzero(dst.writable_addr(), dst.getSafeSize());
// rasterize path
SkPaint paint;
paint.setStyle(SkPaint::kFill_Style);
paint.setAntiAlias(true);
SkDraw draw;
sk_bzero(&draw, sizeof(draw));
SkRasterClip rasterClip;
rasterClip.setRect(devPathBounds);
draw.fRC = &rasterClip;
drawMatrix.postTranslate(translateX, translateY);
draw.fMatrix = &drawMatrix;
draw.fDst = dst;
draw.drawPathCoverage(path, paint);
// add to atlas
SkIPoint16 atlasLocation;
GrDrawOpAtlas::AtlasID id;
if (!atlas->addToAtlas(&id, target, dst.width(), dst.height(), dst.addr(),
&atlasLocation)) {
this->flush(target, flushInfo);
if (!atlas->addToAtlas(&id, target, dst.width(), dst.height(), dst.addr(),
&atlasLocation)) {
return false;
}
}
// add to cache
shapeData->fKey.set(shape, ctm);
shapeData->fID = id;
shapeData->fBounds = SkRect::Make(devPathBounds);
shapeData->fBounds.offset(-translateX, -translateY);
shapeData->fTextureCoords.set(atlasLocation.fX << 1, atlasLocation.fY << 1,
(atlasLocation.fX+width) << 1,
(atlasLocation.fY+height) << 1);
fShapeCache->add(shapeData);
fShapeList->addToTail(shapeData);
#ifdef DF_PATH_TRACKING
++g_NumCachedPaths;
#endif
return true;
}
void writePathVertices(GrDrawOp::Target* target,
GrDrawOpAtlas* atlas,
intptr_t offset,
GrColor color,
size_t vertexStride,
const SkVector& preTranslate,
const ShapeData* shapeData) const {
SkPoint* positions = reinterpret_cast<SkPoint*>(offset);
SkRect bounds = shapeData->fBounds;
// vertex positions
// TODO make the vertex attributes a struct
positions->setRectFan(bounds.left() + preTranslate.fX,
bounds.top() + preTranslate.fY,
bounds.right() + preTranslate.fX,
bounds.bottom() + preTranslate.fY,
vertexStride);
// colors
for (int i = 0; i < kVerticesPerQuad; i++) {
GrColor* colorPtr = (GrColor*)(offset + sizeof(SkPoint) + i * vertexStride);
*colorPtr = color;
}
// set up texture coordinates
uint16_t l = shapeData->fTextureCoords.fLeft;
uint16_t t = shapeData->fTextureCoords.fTop;
uint16_t r = shapeData->fTextureCoords.fRight;
uint16_t b = shapeData->fTextureCoords.fBottom;
// set vertex texture coords
intptr_t textureCoordOffset = offset + sizeof(SkPoint) + sizeof(GrColor);
uint16_t* textureCoords = (uint16_t*) textureCoordOffset;
textureCoords[0] = l;
textureCoords[1] = t;
textureCoordOffset += vertexStride;
textureCoords = (uint16_t*)textureCoordOffset;
textureCoords[0] = l;
textureCoords[1] = b;
textureCoordOffset += vertexStride;
textureCoords = (uint16_t*)textureCoordOffset;
textureCoords[0] = r;
textureCoords[1] = b;
textureCoordOffset += vertexStride;
textureCoords = (uint16_t*)textureCoordOffset;
textureCoords[0] = r;
textureCoords[1] = t;
}
void flush(GrMeshDrawOp::Target* target, FlushInfo* flushInfo) const {
if (flushInfo->fInstancesToFlush) {
GrMesh mesh(GrPrimitiveType::kTriangles);
int maxInstancesPerDraw =
static_cast<int>(flushInfo->fIndexBuffer->gpuMemorySize() / sizeof(uint16_t) / 6);
mesh.setIndexedPatterned(flushInfo->fIndexBuffer.get(), kIndicesPerQuad,
kVerticesPerQuad, flushInfo->fInstancesToFlush,
maxInstancesPerDraw);
mesh.setVertexData(flushInfo->fVertexBuffer.get(), flushInfo->fVertexOffset);
target->draw(flushInfo->fGeometryProcessor.get(), flushInfo->fPipeline, mesh);
flushInfo->fVertexOffset += kVerticesPerQuad * flushInfo->fInstancesToFlush;
flushInfo->fInstancesToFlush = 0;
}
}
GrColor color() const { return fShapes[0].fColor; }
const SkMatrix& viewMatrix() const { return fViewMatrix; }
bool usesDistanceField() const { return fUsesDistanceField; }
bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
SmallPathOp* that = t->cast<SmallPathOp>();
if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
return false;
}
if (this->usesDistanceField() != that->usesDistanceField()) {
return false;
}
// TODO We can position on the cpu for distance field paths
if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
return false;
}
if (!this->usesDistanceField() && fHelper.usesLocalCoords() &&
!this->fShapes[0].fTranslate.equalsWithinTolerance(that->fShapes[0].fTranslate)) {
return false;
}
fShapes.push_back_n(that->fShapes.count(), that->fShapes.begin());
this->joinBounds(*that);
return true;
}
SkMatrix fViewMatrix;
bool fUsesDistanceField;
struct Entry {
GrColor fColor;
GrShape fShape;
SkVector fTranslate;
};
SkSTArray<1, Entry> fShapes;
Helper fHelper;
GrDrawOpAtlas* fAtlas;
ShapeCache* fShapeCache;
ShapeDataList* fShapeList;
bool fGammaCorrect;
typedef GrMeshDrawOp INHERITED;
};
bool GrSmallPathRenderer::onDrawPath(const DrawPathArgs& args) {
GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
"GrSmallPathRenderer::onDrawPath");
// we've already bailed on inverse filled paths, so this is safe
SkASSERT(!args.fShape->isEmpty());
SkASSERT(args.fShape->hasUnstyledKey());
if (!fAtlas) {
fAtlas = GrDrawOpAtlas::Make(args.fContext,
kAlpha_8_GrPixelConfig,
ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
NUM_PLOTS_X, NUM_PLOTS_Y,
&GrSmallPathRenderer::HandleEviction,
(void*)this);
if (!fAtlas) {
return false;
}
}
std::unique_ptr<GrDrawOp> op = SmallPathOp::Make(
std::move(args.fPaint), *args.fShape, *args.fViewMatrix, fAtlas.get(), &fShapeCache,
&fShapeList, args.fGammaCorrect, args.fUserStencilSettings);
args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
#if GR_TEST_UTILS
struct GrSmallPathRenderer::PathTestStruct {
PathTestStruct() : fContextID(SK_InvalidGenID), fAtlas(nullptr) {}
~PathTestStruct() { this->reset(); }
void reset() {
ShapeDataList::Iter iter;
iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
ShapeData* shapeData;
while ((shapeData = iter.get())) {
iter.next();
fShapeList.remove(shapeData);
delete shapeData;
}
fAtlas = nullptr;
fShapeCache.reset();
}
static void HandleEviction(GrDrawOpAtlas::AtlasID id, void* pr) {
PathTestStruct* dfpr = (PathTestStruct*)pr;
// remove any paths that use this plot
ShapeDataList::Iter iter;
iter.init(dfpr->fShapeList, ShapeDataList::Iter::kHead_IterStart);
ShapeData* shapeData;
while ((shapeData = iter.get())) {
iter.next();
if (id == shapeData->fID) {
dfpr->fShapeCache.remove(shapeData->fKey);
dfpr->fShapeList.remove(shapeData);
delete shapeData;
}
}
}
uint32_t fContextID;
std::unique_ptr<GrDrawOpAtlas> fAtlas;
ShapeCache fShapeCache;
ShapeDataList fShapeList;
};
GR_DRAW_OP_TEST_DEFINE(SmallPathOp) {
using PathTestStruct = GrSmallPathRenderer::PathTestStruct;
static PathTestStruct gTestStruct;
if (context->uniqueID() != gTestStruct.fContextID) {
gTestStruct.fContextID = context->uniqueID();
gTestStruct.reset();
gTestStruct.fAtlas = GrDrawOpAtlas::Make(context, kAlpha_8_GrPixelConfig,
ATLAS_TEXTURE_WIDTH, ATLAS_TEXTURE_HEIGHT,
NUM_PLOTS_X, NUM_PLOTS_Y,
&PathTestStruct::HandleEviction,
(void*)&gTestStruct);
}
SkMatrix viewMatrix = GrTest::TestMatrix(random);
bool gammaCorrect = random->nextBool();
// This path renderer only allows fill styles.
GrShape shape(GrTest::TestPath(random), GrStyle::SimpleFill());
return GrSmallPathRenderer::SmallPathOp::Make(
std::move(paint), shape, viewMatrix, gTestStruct.fAtlas.get(), &gTestStruct.fShapeCache,
&gTestStruct.fShapeList, gammaCorrect, GrGetRandomStencil(random, context));
}
#endif
|