Flutter Engine
The Flutter Engine
convexpolyeffect.cpp
Go to the documentation of this file.
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 GPU backend.
9
10#include "gm/gm.h"
15#include "include/core/SkPath.h"
17#include "include/core/SkRect.h"
19#include "include/core/SkSize.h"
29#include "tools/gpu/TestOps.h"
30
31#include <memory>
32#include <utility>
33
34class GrAppliedClip;
35
36namespace skiagm {
37
38/**
39 * This GM directly exercises a GrProcessor that draws convex polygons.
40 */
41class ConvexPolyEffect : public GpuGM {
42public:
44 this->setBGColor(0xFFFFFFFF);
45 }
46
47protected:
48 SkString getName() const override { return SkString("convex_poly_effect"); }
49
50 SkISize getISize() override { return SkISize::Make(720, 550); }
51
52 void onOnceBeforeDraw() override {
53 SkPath tri;
54 tri.moveTo(5.f, 5.f);
55 tri.lineTo(100.f, 20.f);
56 tri.lineTo(15.f, 100.f);
57
58 fPaths.push_back(tri);
59 fPaths.emplace_back();
60 fPaths.back().reverseAddPath(tri);
61
62 tri.close();
63 fPaths.push_back(tri);
64
65 SkPath ngon;
66 constexpr SkScalar kRadius = 50.f;
67 const SkPoint center = { kRadius, kRadius };
68 for (int i = 0; i < GrConvexPolyEffect::kMaxEdges; ++i) {
70 SkPoint point = { SkScalarCos(angle), SkScalarSin(angle) };
71 point.scale(kRadius);
72 point = center + point;
73 if (0 == i) {
74 ngon.moveTo(point);
75 } else {
76 ngon.lineTo(point);
77 }
78 }
79
80 fPaths.push_back(ngon);
81 SkMatrix scaleM;
82 scaleM.setScale(1.1f, 0.4f);
83 ngon.transform(scaleM);
84 fPaths.push_back(ngon);
85
86 SkPath linePath;
87 linePath.moveTo(5.f, 5.f);
88 linePath.lineTo(6.f, 6.f);
89 fPaths.push_back(linePath);
90 }
91
92 DrawResult onDraw(GrRecordingContext* rContext, SkCanvas* canvas, SkString* errorMsg) override {
94 if (!sdc) {
95 *errorMsg = kErrorMsg_DrawSkippedGpuOnly;
96 return DrawResult::kSkip;
97 }
98
99 SkScalar y = 0;
100 static constexpr SkScalar kDX = 12.f;
101 static constexpr SkScalar kOutset = 5.f;
102
103 for (const SkPath& path : fPaths) {
104 SkScalar x = 0;
105
106 for (int et = 0; et < kGrClipEdgeTypeCnt; ++et) {
107 const SkMatrix m = SkMatrix::Translate(x, y);
108 SkPath p;
109 path.transform(m, &p);
110
111 GrClipEdgeType edgeType = (GrClipEdgeType) et;
112 auto [success, fp] = GrConvexPolyEffect::Make(/*inputFP=*/nullptr, edgeType, p);
113 if (!success) {
114 continue;
115 }
116
117 GrPaint grPaint;
118 grPaint.setColor4f({ 0, 0, 0, 1.f });
120 grPaint.setCoverageFragmentProcessor(std::move(fp));
121 auto rect = p.getBounds().makeOutset(kOutset, kOutset);
122 auto op = sk_gpu_test::test_ops::MakeRect(rContext, std::move(grPaint), rect);
123 sdc->addDrawOp(std::move(op));
124
125 x += SkScalarCeilToScalar(path.getBounds().width() + kDX);
126 }
127
128 // Draw AA and non AA paths using normal API for reference.
129 canvas->save();
130 canvas->translate(x, y);
132 canvas->drawPath(path, paint);
133 canvas->translate(path.getBounds().width() + 10.f, 0);
134 paint.setAntiAlias(true);
135 canvas->drawPath(path, paint);
136 canvas->restore();
137
138 y += SkScalarCeilToScalar(path.getBounds().height() + 20.f);
139 }
140
141 return DrawResult::kOk;
142 }
143
144private:
145 std::vector<SkPath> fPaths;
146
147 using INHERITED = GM;
148};
149
150DEF_GM(return new ConvexPolyEffect;)
151
152} // namespace skiagm
static const int kGrClipEdgeTypeCnt
Definition: GrTypesPriv.h:369
GrClipEdgeType
Definition: GrTypesPriv.h:361
#define SkScalarSin(radians)
Definition: SkScalar.h:45
#define SkScalarCos(radians)
Definition: SkScalar.h:46
#define SkScalarCeilToScalar(x)
Definition: SkScalar.h:31
#define SK_ScalarPI
Definition: SkScalar.h:21
static constexpr int kMaxEdges
static GrFPResult Make(std::unique_ptr< GrFragmentProcessor > inputFP, GrClipEdgeType edgeType, int n, const float edges[])
void setXPFactory(const GrXPFactory *xpFactory)
Definition: GrPaint.h:53
void setColor4f(const SkPMColor4f &color)
Definition: GrPaint.h:50
void setCoverageFragmentProcessor(std::unique_ptr< GrFragmentProcessor > fp)
Definition: GrPaint.h:75
static const GrXPFactory * Get(SkBlendMode blendMode)
void restore()
Definition: SkCanvas.cpp:461
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
int save()
Definition: SkCanvas.cpp:447
void drawPath(const SkPath &path, const SkPaint &paint)
Definition: SkCanvas.cpp:1747
static SkMatrix Translate(SkScalar dx, SkScalar dy)
Definition: SkMatrix.h:91
SkMatrix & setScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py)
Definition: SkMatrix.cpp:296
Definition: SkPath.h:59
SkPath & moveTo(SkScalar x, SkScalar y)
Definition: SkPath.cpp:688
SkPath & lineTo(SkScalar x, SkScalar y)
Definition: SkPath.cpp:728
SkPath & close()
Definition: SkPath.cpp:823
void transform(const SkMatrix &matrix, SkPath *dst, SkApplyPerspectiveClip pc=SkApplyPerspectiveClip::kYes) const
Definition: SkPath.cpp:1711
SkISize getISize() override
DrawResult onDraw(GrRecordingContext *rContext, SkCanvas *canvas, SkString *errorMsg) override
SkString getName() const override
void onOnceBeforeDraw() override
Definition: gm.h:110
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
double y
double x
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
const uint32_t fp
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition: switches.h:57
GrOp::Owner MakeRect(GrRecordingContext *context, GrPaint &&paint, const SkRect &drawRect, const SkRect &localRect, const SkMatrix &localM)
Definition: TestOps.cpp:227
SurfaceDrawContext * TopDeviceSurfaceDrawContext(const SkCanvas *canvas)
Definition: GrCanvas.cpp:20
DEF_GM(return F(C(clipbox), 0.0f, 0.0f, {})) DEF_GM(return F(C(clipbox)
DrawResult
Definition: gm.h:104
constexpr int kRadius
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
void scale(float scale, SkPoint *dst) const
Definition: SkPoint.cpp:17