Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
emptyshader.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2024 Google LLC
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#include "gm/gm.h"
9
14
15namespace skiagm {
16
17namespace {
18
19sk_sp<SkShader> empty(SkRect r) { return sk_make_sp<SkEmptyShader>(); }
20
21sk_sp<SkShader> degen_sweep(SkRect r) {
22 // A too small angle between start and end falls back to an empty shader
23 const float startAngle = 0.0f;
24 const float endAngle = nextafter(startAngle, 360.0f);
26
28 colors,
29 /* pos= */ nullptr,
30 std::size(colors),
32 startAngle, endAngle,
33 /* flags= */ 0,
34 /* localMatrix= */ nullptr);
35}
36
37sk_sp<SkShader> degen_linear(SkRect r) {
38 // Having the two positions be the same causes a fallback to an empty shader
39 const SkPoint pts[2] = { r.center(), r.center() };
41
43 colors,
44 /* pos= */ nullptr,
45 std::size(colors),
47}
48
49sk_sp<SkShader> degen_radial(SkRect r) {
51
52 // Having a radius of 0.0 causes a fallback to an empty shader
54 /* radius= */ 0.0f,
55 colors,
56 /* pos= */ nullptr,
57 std::size(colors),
59}
60
61sk_sp<SkShader> degen_conical(SkRect r) {
63
64 // Having the start and end radii be the same causes a fallback to an empty shader
65 return SkGradientShader::MakeTwoPointConical(r.center(), /* startRadius= */ 0.0f,
66 r.center(), /* endRadius= */ 0.0f,
67 colors,
68 /* pos= */ nullptr,
69 std::size(colors),
71}
72
73} // anonymous namespace
74
75class EmptyShaderGM : public GM {
76public:
78 this->setBGColor(0xFFCCCCCC);
79 }
80
81protected:
82 SkString getName() const override { return SkString("emptyshader"); }
83
84 SkISize getISize() override { return SkISize::Make(128, 88); }
85
86 void onDraw(SkCanvas* canvas) override {
87 SkPaint stroke;
89
90 int left = kPad, top = kPad;
91 for (auto f : { empty, degen_sweep, degen_linear, degen_radial, degen_conical }) {
92 SkRect r = SkRect::MakeXYWH(left, top, kSize, kSize);
93
94 SkPaint p;
95 p.setColor(SK_ColorBLUE);
96 p.setShader(f(r));
97
98 canvas->drawRect(r, p);
99 canvas->drawRect(r, stroke);
100
101 left += kSize + kPad;
102 if (left >= this->getISize().width()) {
103 left = kPad;
104 top += kSize + kPad;
105 }
106 }
107 }
108
109private:
110 static constexpr int kPad = 8;
111 static constexpr int kSize = 32;
112};
113
114//////////////////////////////////////////////////////////////////////////////
115
116DEF_GM(return new EmptyShaderGM;)
117
118} // namespace skiagm
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
static bool left(const SkPoint &p0, const SkPoint &p1)
constexpr int kPad
void drawRect(const SkRect &rect, const SkPaint &paint)
static sk_sp< SkShader > MakeTwoPointConical(const SkPoint &start, SkScalar startRadius, const SkPoint &end, SkScalar endRadius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkShader > MakeSweep(SkScalar cx, SkScalar cy, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, SkScalar startAngle, SkScalar endAngle, uint32_t flags, const SkMatrix *localMatrix)
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
void setStyle(Style style)
Definition SkPaint.cpp:105
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
void onDraw(SkCanvas *canvas) override
SkString getName() const override
SkISize getISize() override
SkScalar width()
Definition gm.h:159
void setBGColor(SkColor)
Definition gm.cpp:159
EMSCRIPTEN_KEEPALIVE void empty()
#define DEF_GM(CODE)
Definition gm.h:40
SkScalar startAngle
Definition SkRecords.h:250
PODArray< SkColor > colors
Definition SkRecords.h:276
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
constexpr float centerX() const
Definition SkRect.h:776
constexpr float centerY() const
Definition SkRect.h:785
constexpr SkPoint center() const
Definition SkRect.h:792