Debian Codegraph @

gm/convexpolyeffect.cpp

raw
1 /*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 // This test only works with the Ganesh backend.
9
10 #include "gm/gm.h"
11 #include "include/core/SkBlendMode.h"
12 #include "include/core/SkCanvas.h"
13 #include "include/core/SkMatrix.h"
14 #include "include/core/SkPaint.h"
15 #include "include/core/SkPath.h"
16 #include "include/core/SkPathBuilder.h"
17 #include "include/core/SkPoint.h"
18 #include "include/core/SkRect.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkSize.h"
21 #include "include/core/SkString.h"
22 #include "include/private/gpu/ganesh/GrTypesPriv.h"
23 #include "src/core/SkCanvasPriv.h"
24 #include "src/core/SkPathPriv.h"
25 #include "src/gpu/ganesh/GrCanvas.h"
26 #include "src/gpu/ganesh/GrFragmentProcessor.h"
27 #include "src/gpu/ganesh/GrPaint.h"
28 #include "src/gpu/ganesh/SurfaceDrawContext.h"
29 #include "src/gpu/ganesh/effects/GrConvexPolyEffect.h"
30 #include "src/gpu/ganesh/effects/GrPorterDuffXferProcessor.h"
31 #include "tools/ganesh/TestOps.h"
32
33 #include <memory>
34 #include <utility>
35
36 class GrAppliedClip;
37
38 namespace skiagm {
39
40 /**
41 * This GM directly exercises a GrProcessor that draws convex polygons.
42 */
43 class ConvexPolyEffect : public GpuGM {
44 public:
45 ConvexPolyEffect() {
46 this->setBGColor(0xFFFFFFFF);
47 }
48
49 protected:
50 SkString getName() const override { return SkString("convex_poly_effect"); }
51
52 SkISize getISize() override { return SkISize::Make(720, 550); }
53
54 void onOnceBeforeDraw() override {
55 SkPathBuilder tri;
56 tri.moveTo(5.f, 5.f)
57 .lineTo(100.f, 20.f)
58 .lineTo(15.f, 100.f);
59
60 fPaths.push_back(tri.snapshot());
61 fPaths.push_back(SkPathPriv::ReversePath(fPaths.back()));
62
63 tri.close();
64 fPaths.push_back(tri.detach());
65
66 SkPathBuilder ngon;
67 constexpr SkScalar kRadius = 50.f;
68 const SkPoint center = { kRadius, kRadius };
69 for (int i = 0; i < GrConvexPolyEffect::kMaxEdges; ++i) {
70 SkScalar angle = 2 * SK_ScalarPI * i / GrConvexPolyEffect::kMaxEdges;
71 SkPoint point = { SkScalarCos(angle), SkScalarSin(angle) };
72 point.scale(kRadius);
73 point = center + point;
74 if (0 == i) {
75 ngon.moveTo(point);
76 } else {
77 ngon.lineTo(point);
78 }
79 }
80
81 SkPath path = ngon.detach();
82 fPaths.push_back(path);
83 fPaths.push_back(path.makeTransform(SkMatrix::Scale(1.1f, 0.4f)));
84
85 fPaths.push_back(SkPath::Line({5.f, 5.f}, {6.f, 6.f}));
86 }
87
88 DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override {
89 auto sdc = skgpu::ganesh::TopDeviceSurfaceDrawContext(canvas);
90 if (!sdc) {
91 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
92 return DrawResult::kSkip;
93 }
94
95 SkScalar y = 0;
96 static constexpr SkScalar kDX = 12.f;
97 static constexpr SkScalar kOutset = 5.f;
98
99 for (const SkPath& path : fPaths) {
100 SkScalar x = 0;
101
102 for (int et = 0; et < kGrClipEdgeTypeCnt; ++et) {
103 const SkMatrix m = SkMatrix::Translate(x, y);
104 SkPath p = path.makeTransform(m);
105
106 GrClipEdgeType edgeType = (GrClipEdgeType) et;
107 auto [success, fp] = GrConvexPolyEffect::Make(/*inputFP=*/nullptr, edgeType, p);
108 if (!success) {
109 continue;
110 }
111
112 GrPaint grPaint;
113 grPaint.setColor4f({ 0, 0, 0, 1.f });
114 grPaint.setXPFactory(GrPorterDuffXPFactory::Get(SkBlendMode::kSrc));
115 grPaint.setCoverageFragmentProcessor(std::move(fp));
116 auto rect = p.getBounds().makeOutset(kOutset, kOutset);
117 auto op = sk_gpu_test::test_ops::MakeRect(rContext, std::move(grPaint), rect);
118 sdc->addDrawOp(std::move(op));
119
120 x += SkScalarCeilToScalar(path.getBounds().width() + kDX);
121 }
122
123 // Draw AA and non AA paths using normal API for reference.
124 canvas->save();
125 canvas->translate(x, y);
126 SkPaint paint;
127 canvas->drawPath(path, paint);
128 canvas->translate(path.getBounds().width() + 10.f, 0);
129 paint.setAntiAlias(true);
130 canvas->drawPath(path, paint);
131 canvas->restore();
132
133 y += SkScalarCeilToScalar(path.getBounds().height() + 20.f);
134 }
135
136 return DrawResult::kOk;
137 }
138
139 private:
140 std::vector<SkPath> fPaths;
141
142 using INHERITED = GM;
143 };
144
145 DEF_GM(return new ConvexPolyEffect;)
146
147 } // namespace skiagm