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
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
|
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "GrShadowRRectOp.h"
#include "GrBatchTest.h"
#include "GrOpFlushState.h"
#include "GrResourceProvider.h"
#include "GrStyle.h"
#include "effects/GrShadowGeoProc.h"
///////////////////////////////////////////////////////////////////////////////
// We have two possible cases for geometry for a circle:
// In the case of a normal fill, we draw geometry for the circle as an octagon.
static const uint16_t gFillCircleIndices[] = {
// enter the octagon
// clang-format off
0, 1, 8, 1, 2, 8,
2, 3, 8, 3, 4, 8,
4, 5, 8, 5, 6, 8,
6, 7, 8, 7, 0, 8,
// clang-format on
};
// For stroked circles, we use two nested octagons.
static const uint16_t gStrokeCircleIndices[] = {
// enter the octagon
// clang-format off
0, 1, 9, 0, 9, 8,
1, 2, 10, 1, 10, 9,
2, 3, 11, 2, 11, 10,
3, 4, 12, 3, 12, 11,
4, 5, 13, 4, 13, 12,
5, 6, 14, 5, 14, 13,
6, 7, 15, 6, 15, 14,
7, 0, 8, 7, 8, 15,
// clang-format on
};
static const int kIndicesPerFillCircle = SK_ARRAY_COUNT(gFillCircleIndices);
static const int kIndicesPerStrokeCircle = SK_ARRAY_COUNT(gStrokeCircleIndices);
static const int kVertsPerStrokeCircle = 16;
static const int kVertsPerFillCircle = 9;
static int circle_type_to_vert_count(bool stroked) {
return stroked ? kVertsPerStrokeCircle : kVertsPerFillCircle;
}
static int circle_type_to_index_count(bool stroked) {
return stroked ? kIndicesPerStrokeCircle : kIndicesPerFillCircle;
}
static const uint16_t* circle_type_to_indices(bool stroked) {
return stroked ? gStrokeCircleIndices : gFillCircleIndices;
}
///////////////////////////////////////////////////////////////////////////////
class ShadowCircleOp final : public GrMeshDrawOp {
public:
DEFINE_OP_CLASS_ID
static sk_sp<GrDrawOp> Make(GrColor color, const SkMatrix& viewMatrix, SkPoint center,
SkScalar radius, SkScalar blurRadius, const GrStyle& style) {
SkASSERT(viewMatrix.isSimilarity());
const SkStrokeRec& stroke = style.strokeRec();
if (style.hasPathEffect()) {
return nullptr;
}
SkStrokeRec::Style recStyle = stroke.getStyle();
viewMatrix.mapPoints(¢er, 1);
radius = viewMatrix.mapRadius(radius);
SkScalar strokeWidth = viewMatrix.mapRadius(stroke.getWidth());
bool isStrokeOnly =
SkStrokeRec::kStroke_Style == recStyle || SkStrokeRec::kHairline_Style == recStyle;
bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == recStyle;
SkScalar innerRadius = -SK_ScalarHalf;
SkScalar outerRadius = radius;
SkScalar halfWidth = 0;
if (hasStroke) {
if (SkScalarNearlyZero(strokeWidth)) {
halfWidth = SK_ScalarHalf;
} else {
halfWidth = SkScalarHalf(strokeWidth);
}
outerRadius += halfWidth;
if (isStrokeOnly) {
innerRadius = radius - halfWidth;
}
}
// TODO: still needed?
// The radii are outset for two reasons. First, it allows the shader to simply perform
// simpler computation because the computed alpha is zero, rather than 50%, at the radius.
// Second, the outer radius is used to compute the verts of the bounding box that is
// rendered and the outset ensures the box will cover all partially covered by the circle.
outerRadius += SK_ScalarHalf;
innerRadius -= SK_ScalarHalf;
bool stroked = isStrokeOnly && innerRadius > 0.0f;
sk_sp<ShadowCircleOp> op(new ShadowCircleOp());
op->fViewMatrixIfUsingLocalCoords = viewMatrix;
SkRect devBounds = SkRect::MakeLTRB(center.fX - outerRadius, center.fY - outerRadius,
center.fX + outerRadius, center.fY + outerRadius);
op->fGeoData.emplace_back(
Geometry{color, outerRadius, innerRadius, blurRadius, devBounds, stroked});
// Use the original radius and stroke radius for the bounds so that it does not include the
// AA bloat.
radius += halfWidth;
op->setBounds(
{center.fX - radius, center.fY - radius, center.fX + radius, center.fY + radius},
HasAABloat::kNo, IsZeroArea::kNo);
op->fVertCount = circle_type_to_vert_count(stroked);
op->fIndexCount = circle_type_to_index_count(stroked);
return std::move(op);
}
const char* name() const override { return "ShadowCircleOp"; }
SkString dumpInfo() const override {
SkString string;
for (int i = 0; i < fGeoData.count(); ++i) {
string.appendf(
"Color: 0x%08x Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f], "
"OuterRad: %.2f, InnerRad: %.2f, BlurRad: %.2f\n",
fGeoData[i].fColor, fGeoData[i].fDevBounds.fLeft, fGeoData[i].fDevBounds.fTop,
fGeoData[i].fDevBounds.fRight, fGeoData[i].fDevBounds.fBottom,
fGeoData[i].fOuterRadius, fGeoData[i].fInnerRadius, fGeoData[i].fBlurRadius);
}
string.append(DumpPipelineInfo(*this->pipeline()));
string.append(INHERITED::dumpInfo());
return string;
}
void computePipelineOptimizations(GrInitInvariantOutput* color,
GrInitInvariantOutput* coverage,
GrBatchToXPOverrides* overrides) const override {
// When this is called on a batch, there is only one geometry bundle
color->setKnownFourComponents(fGeoData[0].fColor);
coverage->setUnknownSingleComponent();
}
private:
ShadowCircleOp() : INHERITED(ClassID()) {}
void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
// Handle any overrides that affect our GP.
overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
if (!overrides.readsLocalCoords()) {
fViewMatrixIfUsingLocalCoords.reset();
}
}
void onPrepareDraws(Target* target) const override {
SkMatrix localMatrix;
if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) {
return;
}
// Setup geometry processor
sk_sp<GrGeometryProcessor> gp(GrRRectShadowGeoProc::Make(localMatrix));
struct CircleVertex {
SkPoint fPos;
GrColor fColor;
SkPoint fOffset;
SkScalar fOuterRadius;
SkScalar fBlurRadius;
};
int instanceCount = fGeoData.count();
size_t vertexStride = gp->getVertexStride();
SkASSERT(vertexStride == sizeof(CircleVertex));
const GrBuffer* vertexBuffer;
int firstVertex;
char* vertices = (char*)target->makeVertexSpace(vertexStride, fVertCount, &vertexBuffer,
&firstVertex);
if (!vertices) {
SkDebugf("Could not allocate vertices\n");
return;
}
const GrBuffer* indexBuffer = nullptr;
int firstIndex = 0;
uint16_t* indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex);
if (!indices) {
SkDebugf("Could not allocate indices\n");
return;
}
int currStartVertex = 0;
for (int i = 0; i < instanceCount; i++) {
const Geometry& geom = fGeoData[i];
GrColor color = geom.fColor;
SkScalar outerRadius = geom.fOuterRadius;
SkScalar innerRadius = geom.fInnerRadius;
SkScalar blurRadius = geom.fBlurRadius;
const SkRect& bounds = geom.fDevBounds;
CircleVertex* ov0 = reinterpret_cast<CircleVertex*>(vertices + 0 * vertexStride);
CircleVertex* ov1 = reinterpret_cast<CircleVertex*>(vertices + 1 * vertexStride);
CircleVertex* ov2 = reinterpret_cast<CircleVertex*>(vertices + 2 * vertexStride);
CircleVertex* ov3 = reinterpret_cast<CircleVertex*>(vertices + 3 * vertexStride);
CircleVertex* ov4 = reinterpret_cast<CircleVertex*>(vertices + 4 * vertexStride);
CircleVertex* ov5 = reinterpret_cast<CircleVertex*>(vertices + 5 * vertexStride);
CircleVertex* ov6 = reinterpret_cast<CircleVertex*>(vertices + 6 * vertexStride);
CircleVertex* ov7 = reinterpret_cast<CircleVertex*>(vertices + 7 * vertexStride);
// The inner radius in the vertex data must be specified in normalized space.
innerRadius = innerRadius / outerRadius;
SkPoint center = SkPoint::Make(bounds.centerX(), bounds.centerY());
SkScalar halfWidth = 0.5f * bounds.width();
SkScalar octOffset = 0.41421356237f; // sqrt(2) - 1
ov0->fPos = center + SkPoint::Make(-octOffset * halfWidth, -halfWidth);
ov0->fColor = color;
ov0->fOffset = SkPoint::Make(-octOffset, -1);
ov0->fOuterRadius = outerRadius;
ov0->fBlurRadius = blurRadius;
ov1->fPos = center + SkPoint::Make(octOffset * halfWidth, -halfWidth);
ov1->fColor = color;
ov1->fOffset = SkPoint::Make(octOffset, -1);
ov1->fOuterRadius = outerRadius;
ov1->fBlurRadius = blurRadius;
ov2->fPos = center + SkPoint::Make(halfWidth, -octOffset * halfWidth);
ov2->fColor = color;
ov2->fOffset = SkPoint::Make(1, -octOffset);
ov2->fOuterRadius = outerRadius;
ov2->fBlurRadius = blurRadius;
ov3->fPos = center + SkPoint::Make(halfWidth, octOffset * halfWidth);
ov3->fColor = color;
ov3->fOffset = SkPoint::Make(1, octOffset);
ov3->fOuterRadius = outerRadius;
ov3->fBlurRadius = blurRadius;
ov4->fPos = center + SkPoint::Make(octOffset * halfWidth, halfWidth);
ov4->fColor = color;
ov4->fOffset = SkPoint::Make(octOffset, 1);
ov4->fOuterRadius = outerRadius;
ov4->fBlurRadius = blurRadius;
ov5->fPos = center + SkPoint::Make(-octOffset * halfWidth, halfWidth);
ov5->fColor = color;
ov5->fOffset = SkPoint::Make(-octOffset, 1);
ov5->fOuterRadius = outerRadius;
ov5->fBlurRadius = blurRadius;
ov6->fPos = center + SkPoint::Make(-halfWidth, octOffset * halfWidth);
ov6->fColor = color;
ov6->fOffset = SkPoint::Make(-1, octOffset);
ov6->fOuterRadius = outerRadius;
ov6->fBlurRadius = blurRadius;
ov7->fPos = center + SkPoint::Make(-halfWidth, -octOffset * halfWidth);
ov7->fColor = color;
ov7->fOffset = SkPoint::Make(-1, -octOffset);
ov7->fOuterRadius = outerRadius;
ov7->fBlurRadius = blurRadius;
if (geom.fStroked) {
// compute the inner ring
CircleVertex* iv0 = reinterpret_cast<CircleVertex*>(vertices + 8 * vertexStride);
CircleVertex* iv1 = reinterpret_cast<CircleVertex*>(vertices + 9 * vertexStride);
CircleVertex* iv2 = reinterpret_cast<CircleVertex*>(vertices + 10 * vertexStride);
CircleVertex* iv3 = reinterpret_cast<CircleVertex*>(vertices + 11 * vertexStride);
CircleVertex* iv4 = reinterpret_cast<CircleVertex*>(vertices + 12 * vertexStride);
CircleVertex* iv5 = reinterpret_cast<CircleVertex*>(vertices + 13 * vertexStride);
CircleVertex* iv6 = reinterpret_cast<CircleVertex*>(vertices + 14 * vertexStride);
CircleVertex* iv7 = reinterpret_cast<CircleVertex*>(vertices + 15 * vertexStride);
// cosine and sine of pi/8
SkScalar c = 0.923579533f;
SkScalar s = 0.382683432f;
SkScalar r = geom.fInnerRadius;
iv0->fPos = center + SkPoint::Make(-s * r, -c * r);
iv0->fColor = color;
iv0->fOffset = SkPoint::Make(-s * innerRadius, -c * innerRadius);
iv0->fOuterRadius = outerRadius;
iv0->fBlurRadius = blurRadius;
iv1->fPos = center + SkPoint::Make(s * r, -c * r);
iv1->fColor = color;
iv1->fOffset = SkPoint::Make(s * innerRadius, -c * innerRadius);
iv1->fOuterRadius = outerRadius;
iv1->fBlurRadius = blurRadius;
iv2->fPos = center + SkPoint::Make(c * r, -s * r);
iv2->fColor = color;
iv2->fOffset = SkPoint::Make(c * innerRadius, -s * innerRadius);
iv2->fOuterRadius = outerRadius;
iv2->fBlurRadius = blurRadius;
iv3->fPos = center + SkPoint::Make(c * r, s * r);
iv3->fColor = color;
iv3->fOffset = SkPoint::Make(c * innerRadius, s * innerRadius);
iv3->fOuterRadius = outerRadius;
iv3->fBlurRadius = blurRadius;
iv4->fPos = center + SkPoint::Make(s * r, c * r);
iv4->fColor = color;
iv4->fOffset = SkPoint::Make(s * innerRadius, c * innerRadius);
iv4->fOuterRadius = outerRadius;
iv4->fBlurRadius = blurRadius;
iv5->fPos = center + SkPoint::Make(-s * r, c * r);
iv5->fColor = color;
iv5->fOffset = SkPoint::Make(-s * innerRadius, c * innerRadius);
iv5->fOuterRadius = outerRadius;
iv5->fBlurRadius = blurRadius;
iv6->fPos = center + SkPoint::Make(-c * r, s * r);
iv6->fColor = color;
iv6->fOffset = SkPoint::Make(-c * innerRadius, s * innerRadius);
iv6->fOuterRadius = outerRadius;
iv6->fBlurRadius = blurRadius;
iv7->fPos = center + SkPoint::Make(-c * r, -s * r);
iv7->fColor = color;
iv7->fOffset = SkPoint::Make(-c * innerRadius, -s * innerRadius);
iv7->fOuterRadius = outerRadius;
iv7->fBlurRadius = blurRadius;
} else {
// filled
CircleVertex* iv = reinterpret_cast<CircleVertex*>(vertices + 8 * vertexStride);
iv->fPos = center;
iv->fColor = color;
iv->fOffset = SkPoint::Make(0, 0);
iv->fOuterRadius = outerRadius;
iv->fBlurRadius = blurRadius;
}
const uint16_t* primIndices = circle_type_to_indices(geom.fStroked);
const int primIndexCount = circle_type_to_index_count(geom.fStroked);
for (int i = 0; i < primIndexCount; ++i) {
*indices++ = primIndices[i] + currStartVertex;
}
currStartVertex += circle_type_to_vert_count(geom.fStroked);
vertices += circle_type_to_vert_count(geom.fStroked) * vertexStride;
}
GrMesh mesh;
mesh.initIndexed(kTriangles_GrPrimitiveType, vertexBuffer, indexBuffer, firstVertex,
firstIndex, fVertCount, fIndexCount);
target->draw(gp.get(), mesh);
}
bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
ShadowCircleOp* that = t->cast<ShadowCircleOp>();
if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
that->bounds(), caps)) {
return false;
}
if (!fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) {
return false;
}
fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
this->joinBounds(*that);
fVertCount += that->fVertCount;
fIndexCount += that->fIndexCount;
return true;
}
struct Geometry {
GrColor fColor;
SkScalar fOuterRadius;
SkScalar fInnerRadius;
SkScalar fBlurRadius;
SkRect fDevBounds;
bool fStroked;
};
SkSTArray<1, Geometry, true> fGeoData;
SkMatrix fViewMatrixIfUsingLocalCoords;
int fVertCount;
int fIndexCount;
typedef GrMeshDrawOp INHERITED;
};
///////////////////////////////////////////////////////////////////////////////////////////////////
// We have two possible cases for geometry for a shadow roundrect.
//
// In the case of a normal stroke, we draw the roundrect as a 9-patch without the center quad.
// ____________
// |_|________|_|
// | | | |
// | | | |
// | | | |
// |_|________|_|
// |_|________|_|
//
// In the case where the stroke width is greater than twice the corner radius (overstroke),
// we add additional geometry to mark out the rectangle in the center. The shared vertices
// are duplicated so we can set a different outer radius for the fill calculation.
// ____________
// |_|________|_|
// | |\ ____ /| |
// | | | | | |
// | | |____| | |
// |_|/______\|_|
// |_|________|_|
//
// For filled rrects we reuse the overstroke geometry but make the inner rect degenerate
// (either a point or a horizontal or vertical line).
static const uint16_t gOverstrokeRRectIndices[] = {
// clang-format off
// corners
0, 1, 5, 0, 5, 4,
2, 3, 7, 2, 7, 6,
8, 9, 13, 8, 13, 12,
10, 11, 15, 10, 15, 14,
// edges
1, 2, 6, 1, 6, 5,
4, 5, 9, 4, 9, 8,
6, 7, 11, 6, 11, 10,
9, 10, 14, 9, 14, 13,
// overstroke quads
// we place this at the end so that we can skip these indices when rendering as stroked
16, 17, 19, 16, 19, 18,
19, 17, 23, 19, 23, 21,
21, 23, 22, 21, 22, 20,
22, 16, 18, 22, 18, 20,
// clang-format on
};
// standard stroke indices start at the same place, but will skip the overstroke "ring"
static const uint16_t* gStrokeRRectIndices = gOverstrokeRRectIndices;
// overstroke count
static const int kIndicesPerOverstrokeRRect = SK_ARRAY_COUNT(gOverstrokeRRectIndices);
// simple stroke count skips overstroke indices
static const int kIndicesPerStrokeRRect = kIndicesPerOverstrokeRRect - 6 * 4 + 6;
static const int kVertsPerStrokeRRect = 16;
static const int kVertsPerOverstrokeRRect = 24;
enum RRectType {
kFill_RRectType,
kStroke_RRectType,
kOverstroke_RRectType,
};
static int rrect_type_to_vert_count(RRectType type) {
switch (type) {
case kFill_RRectType:
return kVertsPerOverstrokeRRect;
case kStroke_RRectType:
return kVertsPerStrokeRRect;
case kOverstroke_RRectType:
return kVertsPerOverstrokeRRect;
}
SkFAIL("Invalid type");
return 0;
}
static int rrect_type_to_index_count(RRectType type) {
switch (type) {
case kFill_RRectType:
return kIndicesPerOverstrokeRRect;
case kStroke_RRectType:
return kIndicesPerStrokeRRect;
case kOverstroke_RRectType:
return kIndicesPerOverstrokeRRect;
}
SkFAIL("Invalid type");
return 0;
}
static const uint16_t* rrect_type_to_indices(RRectType type) {
switch (type) {
case kFill_RRectType:
return gOverstrokeRRectIndices;
case kStroke_RRectType:
return gStrokeRRectIndices;
case kOverstroke_RRectType:
return gOverstrokeRRectIndices;
}
SkFAIL("Invalid type");
return nullptr;
}
// For distance computations in the interior of filled rrects we:
//
// add a interior degenerate (point or line) rect
// each vertex of that rect gets -outerRad as its radius
// this makes the computation of the distance to the outer edge be negative
// negative values are caught and then handled differently in the GP's onEmitCode
// each vertex is also given the normalized x & y distance from the interior rect's edge
// the GP takes the min of those depths +1 to get the normalized distance to the outer edge
class ShadowCircularRRectOp final : public GrMeshDrawOp {
public:
DEFINE_OP_CLASS_ID
// A devStrokeWidth <= 0 indicates a fill only. If devStrokeWidth > 0 then strokeOnly indicates
// whether the rrect is only stroked or stroked and filled.
ShadowCircularRRectOp(GrColor color, const SkMatrix& viewMatrix, const SkRect& devRect,
float devRadius, float blurRadius, float devStrokeWidth, bool strokeOnly)
: INHERITED(ClassID()), fViewMatrixIfUsingLocalCoords(viewMatrix) {
SkRect bounds = devRect;
SkASSERT(!(devStrokeWidth <= 0 && strokeOnly));
SkScalar innerRadius = 0.0f;
SkScalar outerRadius = devRadius;
SkScalar halfWidth = 0;
RRectType type = kFill_RRectType;
if (devStrokeWidth > 0) {
if (SkScalarNearlyZero(devStrokeWidth)) {
halfWidth = SK_ScalarHalf;
} else {
halfWidth = SkScalarHalf(devStrokeWidth);
}
if (strokeOnly) {
// Outset stroke by 1/4 pixel
devStrokeWidth += 0.25f;
// If stroke is greater than width or height, this is still a fill
// Otherwise we compute stroke params
if (devStrokeWidth <= devRect.width() && devStrokeWidth <= devRect.height()) {
innerRadius = devRadius - halfWidth;
type = (innerRadius >= 0) ? kStroke_RRectType : kOverstroke_RRectType;
}
}
outerRadius += halfWidth;
bounds.outset(halfWidth, halfWidth);
}
// TODO: still needed?
// The radii are outset for two reasons. First, it allows the shader to simply perform
// simpler computation because the computed alpha is zero, rather than 50%, at the radius.
// Second, the outer radius is used to compute the verts of the bounding box that is
// rendered and the outset ensures the box will cover all partially covered by the rrect
// corners.
outerRadius += SK_ScalarHalf;
innerRadius -= SK_ScalarHalf;
this->setBounds(bounds, HasAABloat::kYes, IsZeroArea::kNo);
// Expand the rect for aa to generate correct vertices.
bounds.outset(SK_ScalarHalf, SK_ScalarHalf);
fGeoData.emplace_back(Geometry{color, outerRadius, innerRadius, blurRadius, bounds, type});
fVertCount = rrect_type_to_vert_count(type);
fIndexCount = rrect_type_to_index_count(type);
}
const char* name() const override { return "ShadowCircularRRectOp"; }
SkString dumpInfo() const override {
SkString string;
for (int i = 0; i < fGeoData.count(); ++i) {
string.appendf(
"Color: 0x%08x Rect [L: %.2f, T: %.2f, R: %.2f, B: %.2f],"
"OuterRad: %.2f, InnerRad: %.2f, BlurRad: %.2f\n",
fGeoData[i].fColor, fGeoData[i].fDevBounds.fLeft, fGeoData[i].fDevBounds.fTop,
fGeoData[i].fDevBounds.fRight, fGeoData[i].fDevBounds.fBottom,
fGeoData[i].fOuterRadius, fGeoData[i].fInnerRadius, fGeoData[i].fBlurRadius);
}
string.append(DumpPipelineInfo(*this->pipeline()));
string.append(INHERITED::dumpInfo());
return string;
}
void computePipelineOptimizations(GrInitInvariantOutput* color,
GrInitInvariantOutput* coverage,
GrBatchToXPOverrides* overrides) const override {
// When this is called on a batch, there is only one geometry bundle
color->setKnownFourComponents(fGeoData[0].fColor);
coverage->setUnknownSingleComponent();
}
private:
void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
// Handle any overrides that affect our GP.
overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
if (!overrides.readsLocalCoords()) {
fViewMatrixIfUsingLocalCoords.reset();
}
}
struct CircleVertex {
SkPoint fPos;
GrColor fColor;
SkPoint fOffset;
SkScalar fOuterRadius;
SkScalar fBlurRadius;
};
static void FillInOverstrokeVerts(CircleVertex** verts, const SkRect& bounds, SkScalar smInset,
SkScalar bigInset, SkScalar xOffset, SkScalar outerRadius,
GrColor color, SkScalar blurRadius) {
SkASSERT(smInset < bigInset);
// TL
(*verts)->fPos = SkPoint::Make(bounds.fLeft + smInset, bounds.fTop + smInset);
(*verts)->fColor = color;
(*verts)->fOffset = SkPoint::Make(xOffset, 0);
(*verts)->fOuterRadius = outerRadius;
(*verts)->fBlurRadius = blurRadius;
(*verts)++;
// TR
(*verts)->fPos = SkPoint::Make(bounds.fRight - smInset, bounds.fTop + smInset);
(*verts)->fColor = color;
(*verts)->fOffset = SkPoint::Make(xOffset, 0);
(*verts)->fOuterRadius = outerRadius;
(*verts)->fBlurRadius = blurRadius;
(*verts)++;
(*verts)->fPos = SkPoint::Make(bounds.fLeft + bigInset, bounds.fTop + bigInset);
(*verts)->fColor = color;
(*verts)->fOffset = SkPoint::Make(0, 0);
(*verts)->fOuterRadius = outerRadius;
(*verts)->fBlurRadius = blurRadius;
(*verts)++;
(*verts)->fPos = SkPoint::Make(bounds.fRight - bigInset, bounds.fTop + bigInset);
(*verts)->fColor = color;
(*verts)->fOffset = SkPoint::Make(0, 0);
(*verts)->fOuterRadius = outerRadius;
(*verts)->fBlurRadius = blurRadius;
(*verts)++;
(*verts)->fPos = SkPoint::Make(bounds.fLeft + bigInset, bounds.fBottom - bigInset);
(*verts)->fColor = color;
(*verts)->fOffset = SkPoint::Make(0, 0);
(*verts)->fOuterRadius = outerRadius;
(*verts)->fBlurRadius = blurRadius;
(*verts)++;
(*verts)->fPos = SkPoint::Make(bounds.fRight - bigInset, bounds.fBottom - bigInset);
(*verts)->fColor = color;
(*verts)->fOffset = SkPoint::Make(0, 0);
(*verts)->fOuterRadius = outerRadius;
(*verts)->fBlurRadius = blurRadius;
(*verts)++;
// BL
(*verts)->fPos = SkPoint::Make(bounds.fLeft + smInset, bounds.fBottom - smInset);
(*verts)->fColor = color;
(*verts)->fOffset = SkPoint::Make(xOffset, 0);
(*verts)->fOuterRadius = outerRadius;
(*verts)->fBlurRadius = blurRadius;
(*verts)++;
// BR
(*verts)->fPos = SkPoint::Make(bounds.fRight - smInset, bounds.fBottom - smInset);
(*verts)->fColor = color;
(*verts)->fOffset = SkPoint::Make(xOffset, 0);
(*verts)->fOuterRadius = outerRadius;
(*verts)->fBlurRadius = blurRadius;
(*verts)++;
}
void onPrepareDraws(Target* target) const override {
// Invert the view matrix as a local matrix (if any other processors require coords).
SkMatrix localMatrix;
if (!fViewMatrixIfUsingLocalCoords.invert(&localMatrix)) {
return;
}
// Setup geometry processor
sk_sp<GrGeometryProcessor> gp(GrRRectShadowGeoProc::Make(localMatrix));
int instanceCount = fGeoData.count();
size_t vertexStride = gp->getVertexStride();
SkASSERT(sizeof(CircleVertex) == vertexStride);
const GrBuffer* vertexBuffer;
int firstVertex;
CircleVertex* verts = (CircleVertex*)target->makeVertexSpace(vertexStride, fVertCount,
&vertexBuffer, &firstVertex);
if (!verts) {
SkDebugf("Could not allocate vertices\n");
return;
}
const GrBuffer* indexBuffer = nullptr;
int firstIndex = 0;
uint16_t* indices = target->makeIndexSpace(fIndexCount, &indexBuffer, &firstIndex);
if (!indices) {
SkDebugf("Could not allocate indices\n");
return;
}
int currStartVertex = 0;
for (int i = 0; i < instanceCount; i++) {
const Geometry& args = fGeoData[i];
GrColor color = args.fColor;
SkScalar outerRadius = args.fOuterRadius;
const SkRect& bounds = args.fDevBounds;
SkScalar yCoords[4] = {bounds.fTop, bounds.fTop + outerRadius,
bounds.fBottom - outerRadius, bounds.fBottom};
SkScalar yOuterRadii[4] = {-1, 0, 0, 1};
// The inner radius in the vertex data must be specified in normalized space.
// For fills, specifying -1/outerRadius guarantees an alpha of 1.0 at the inner radius.
SkScalar blurRadius = args.fBlurRadius;
for (int i = 0; i < 4; ++i) {
verts->fPos = SkPoint::Make(bounds.fLeft, yCoords[i]);
verts->fColor = color;
verts->fOffset = SkPoint::Make(-1, yOuterRadii[i]);
verts->fOuterRadius = outerRadius;
verts->fBlurRadius = blurRadius;
verts++;
verts->fPos = SkPoint::Make(bounds.fLeft + outerRadius, yCoords[i]);
verts->fColor = color;
verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
verts->fOuterRadius = outerRadius;
verts->fBlurRadius = blurRadius;
verts++;
verts->fPos = SkPoint::Make(bounds.fRight - outerRadius, yCoords[i]);
verts->fColor = color;
verts->fOffset = SkPoint::Make(0, yOuterRadii[i]);
verts->fOuterRadius = outerRadius;
verts->fBlurRadius = blurRadius;
verts++;
verts->fPos = SkPoint::Make(bounds.fRight, yCoords[i]);
verts->fColor = color;
verts->fOffset = SkPoint::Make(1, yOuterRadii[i]);
verts->fOuterRadius = outerRadius;
verts->fBlurRadius = blurRadius;
verts++;
}
// Add the additional vertices for overstroked rrects.
// Effectively this is an additional stroked rrect, with its
// outer radius = outerRadius - innerRadius, and inner radius = 0.
// This will give us correct AA in the center and the correct
// distance to the outer edge.
//
// Also, the outer offset is a constant vector pointing to the right, which
// guarantees that the distance value along the outer rectangle is constant.
if (kOverstroke_RRectType == args.fType) {
SkASSERT(args.fInnerRadius <= 0.0f);
SkScalar overstrokeOuterRadius = outerRadius - args.fInnerRadius;
// this is the normalized distance from the outer rectangle of this
// geometry to the outer edge
SkScalar maxOffset = -args.fInnerRadius / overstrokeOuterRadius;
FillInOverstrokeVerts(&verts, bounds, outerRadius, overstrokeOuterRadius, maxOffset,
overstrokeOuterRadius, color, blurRadius);
}
if (kFill_RRectType == args.fType) {
SkScalar halfMinDim = 0.5f * SkTMin(bounds.width(), bounds.height());
SkScalar xOffset = 1.0f - outerRadius / halfMinDim;
FillInOverstrokeVerts(&verts, bounds, outerRadius, halfMinDim, xOffset, halfMinDim,
color, blurRadius);
}
const uint16_t* primIndices = rrect_type_to_indices(args.fType);
const int primIndexCount = rrect_type_to_index_count(args.fType);
for (int i = 0; i < primIndexCount; ++i) {
*indices++ = primIndices[i] + currStartVertex;
}
currStartVertex += rrect_type_to_vert_count(args.fType);
}
GrMesh mesh;
mesh.initIndexed(kTriangles_GrPrimitiveType, vertexBuffer, indexBuffer, firstVertex,
firstIndex, fVertCount, fIndexCount);
target->draw(gp.get(), mesh);
}
bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
ShadowCircularRRectOp* that = t->cast<ShadowCircularRRectOp>();
if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
that->bounds(), caps)) {
return false;
}
if (!fViewMatrixIfUsingLocalCoords.cheapEqualTo(that->fViewMatrixIfUsingLocalCoords)) {
return false;
}
fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
this->joinBounds(*that);
fVertCount += that->fVertCount;
fIndexCount += that->fIndexCount;
return true;
}
struct Geometry {
GrColor fColor;
SkScalar fOuterRadius;
SkScalar fInnerRadius;
SkScalar fBlurRadius;
SkRect fDevBounds;
RRectType fType;
};
SkSTArray<1, Geometry, true> fGeoData;
SkMatrix fViewMatrixIfUsingLocalCoords;
int fVertCount;
int fIndexCount;
typedef GrMeshDrawOp INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
sk_sp<GrDrawOp> make_shadow_circle_batch(GrColor color,
const SkMatrix& viewMatrix,
const SkRect& oval,
SkScalar blurRadius,
const SkStrokeRec& stroke,
const GrShaderCaps* shaderCaps) {
// we can only draw circles
SkScalar width = oval.width();
SkASSERT(SkScalarNearlyEqual(width, oval.height()) && viewMatrix.isSimilarity());
SkPoint center = {oval.centerX(), oval.centerY()};
return ShadowCircleOp::Make(color, viewMatrix, center, width / 2.f, blurRadius,
GrStyle(stroke, nullptr));
}
static sk_sp<GrDrawOp> make_shadow_rrect_batch(GrColor color,
const SkMatrix& viewMatrix,
const SkRRect& rrect,
SkScalar blurRadius,
const SkStrokeRec& stroke) {
SkASSERT(viewMatrix.rectStaysRect());
SkASSERT(rrect.isSimple());
SkASSERT(!rrect.isOval());
// Shadow rrect batchs only handle simple circular rrects
// do any matrix crunching before we reset the draw state for device coords
const SkRect& rrectBounds = rrect.getBounds();
SkRect bounds;
viewMatrix.mapRect(&bounds, rrectBounds);
SkVector radii = rrect.getSimpleRadii();
SkScalar xRadius = SkScalarAbs(viewMatrix[SkMatrix::kMScaleX] * radii.fX +
viewMatrix[SkMatrix::kMSkewY] * radii.fY);
SkScalar yRadius = SkScalarAbs(viewMatrix[SkMatrix::kMSkewX] * radii.fX +
viewMatrix[SkMatrix::kMScaleY] * radii.fY);
SkASSERT(SkScalarNearlyEqual(xRadius, yRadius));
SkStrokeRec::Style style = stroke.getStyle();
// Do (potentially) anisotropic mapping of stroke. Use -1s to indicate fill-only draws.
SkVector scaledStroke = {-1, -1};
SkScalar strokeWidth = stroke.getWidth();
bool isStrokeOnly =
SkStrokeRec::kStroke_Style == style || SkStrokeRec::kHairline_Style == style;
bool hasStroke = isStrokeOnly || SkStrokeRec::kStrokeAndFill_Style == style;
if (hasStroke) {
if (SkStrokeRec::kHairline_Style == style) {
scaledStroke.set(1, 1);
} else {
scaledStroke.fX = SkScalarAbs(
strokeWidth * (viewMatrix[SkMatrix::kMScaleX] + viewMatrix[SkMatrix::kMSkewY]));
scaledStroke.fY = SkScalarAbs(
strokeWidth * (viewMatrix[SkMatrix::kMSkewX] + viewMatrix[SkMatrix::kMScaleY]));
}
// we don't handle anisotropic strokes
if (!SkScalarNearlyEqual(scaledStroke.fX, scaledStroke.fY)) {
return nullptr;
}
}
// The way the effect interpolates the offset-to-ellipse/circle-center attribute only works on
// the interior of the rrect if the radii are >= 0.5. Otherwise, the inner rect of the nine-
// patch will have fractional coverage. This only matters when the interior is actually filled.
// We could consider falling back to rect rendering here, since a tiny radius is
// indistinguishable from a square corner.
if (!isStrokeOnly && (SK_ScalarHalf > xRadius || SK_ScalarHalf > yRadius)) {
return nullptr;
}
return sk_sp<GrDrawOp>(new ShadowCircularRRectOp(color, viewMatrix, bounds, xRadius, blurRadius,
scaledStroke.fX, isStrokeOnly));
}
namespace GrShadowRRectOp {
sk_sp<GrDrawOp> Make(GrColor color,
const SkMatrix& viewMatrix,
const SkRRect& rrect,
const SkScalar blurRadius,
const SkStrokeRec& stroke,
const GrShaderCaps* shaderCaps) {
if (rrect.isOval()) {
return make_shadow_circle_batch(color, viewMatrix, rrect.getBounds(), blurRadius, stroke,
shaderCaps);
}
if (!viewMatrix.rectStaysRect() || !rrect.isSimple()) {
return nullptr;
}
return make_shadow_rrect_batch(color, viewMatrix, rrect, blurRadius, stroke);
}
}
///////////////////////////////////////////////////////////////////////////////
#ifdef GR_TEST_UTILS
DRAW_BATCH_TEST_DEFINE(ShadowCircleOp) {
do {
SkScalar rotate = random->nextSScalar1() * 360.f;
SkScalar translateX = random->nextSScalar1() * 1000.f;
SkScalar translateY = random->nextSScalar1() * 1000.f;
SkScalar scale = random->nextSScalar1() * 100.f;
SkMatrix viewMatrix;
viewMatrix.setRotate(rotate);
viewMatrix.postTranslate(translateX, translateY);
viewMatrix.postScale(scale, scale);
GrColor color = GrRandomColor(random);
SkRect circle = GrTest::TestSquare(random);
SkPoint center = {circle.centerX(), circle.centerY()};
SkScalar radius = circle.width() / 2.f;
SkStrokeRec stroke = GrTest::TestStrokeRec(random);
SkScalar blurRadius = random->nextSScalar1() * 72.f;
sk_sp<GrDrawOp> op = ShadowCircleOp::Make(color, viewMatrix, center, radius, blurRadius,
GrStyle(stroke, nullptr));
if (op) {
return op.release();
}
} while (true);
}
DRAW_BATCH_TEST_DEFINE(ShadowRRectOp) {
SkMatrix viewMatrix = GrTest::TestMatrixRectStaysRect(random);
GrColor color = GrRandomColor(random);
const SkRRect& rrect = GrTest::TestRRectSimple(random);
SkScalar blurRadius = random->nextSScalar1() * 72.f;
return make_shadow_rrect_batch(color, viewMatrix, rrect, blurRadius,
GrTest::TestStrokeRec(random))
.release();
}
#endif
|