Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
fillrect_gradient.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2021 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/*
9 * This GM creates the same gradients as the Chromium test fillrect_gradient:
10 * http://osscs/chromium/chromium/src/+/main:third_party/blink/web_tests/fast/canvas/fillrect_gradient.html
11 */
12
13#include "gm/gm.h"
18#include "include/core/SkRect.h"
22#include "include/core/SkSize.h"
26
27#include <vector>
28
29const int kCellSize = 50;
30const int kNumColumns = 2;
31const int kNumRows = 9;
32const int kPadSize = 10;
33
35public:
37
38protected:
39 struct GradientStop {
40 float pos;
42 };
43
44 SkString getName() const override { return SkString("fillrect_gradient"); }
45
46 SkISize getISize() override {
49 }
50
51 void drawGradient(SkCanvas* canvas, std::initializer_list<GradientStop> stops) {
52 std::vector<SkColor> colors;
53 std::vector<SkScalar> positions;
54 colors.reserve(stops.size());
55 positions.reserve(stops.size());
56
57 for (const GradientStop& stop : stops) {
58 colors.push_back(stop.color);
59 positions.push_back(stop.pos);
60 }
61
62 static constexpr SkPoint points[] = {
65 };
66
67 // Draw the gradient linearly.
69 colors.data(),
70 positions.data(),
71 colors.size(),
74 paint.setShader(shader);
76
77 canvas->save();
78 canvas->translate(kCellSize + kPadSize, 0);
79
80 // Draw the gradient radially.
82 kCellSize / 2,
83 colors.data(),
84 positions.data(),
85 colors.size(),
87 paint.setShader(shader);
89
90 canvas->restore();
91 canvas->translate(0, kCellSize + kPadSize);
92 }
93
94 void onDraw(SkCanvas* canvas) override {
95 // Simple gradient: Green to white
96 this->drawGradient(canvas, {{0.0f, SK_ColorGREEN}, {1.0f, SK_ColorWHITE}});
97
98 // Multiple sections: Green to white to red
99 this->drawGradient(canvas,
100 {{0.0f, SK_ColorGREEN}, {0.5f, SK_ColorWHITE}, {1.0f, SK_ColorRED}});
101
102 // No stops at 0.0 or 1.0: Larger green to white to larger red
103 this->drawGradient(canvas,
104 {{0.4f, SK_ColorGREEN}, {0.5f, SK_ColorWHITE}, {0.6f, SK_ColorRED}});
105
106 // Only one stop, at zero: Solid red
107 this->drawGradient(canvas, {{0.0f, SK_ColorRED}});
108
109 // Only one stop, at 1.0: Solid red
110 this->drawGradient(canvas, {{1.0f, SK_ColorRED}});
111
112 // Only one stop, in the middle: Solid red
113 this->drawGradient(canvas, {{0.5f, SK_ColorRED}});
114
115 // Disjoint gradients (multiple stops at the same offset)
116 // Blue to white in the top (inner) half, red to yellow in the bottom (outer) half
117 this->drawGradient(canvas,
118 {{0.0f, SK_ColorBLUE},
119 {0.5f, SK_ColorWHITE},
120 {0.5f, SK_ColorRED},
121 {1.0f, SK_ColorYELLOW}});
122
123 // Ignored stops: Blue to white, red to yellow (same as previous)
124 this->drawGradient(canvas,
125 {{0.0f, SK_ColorBLUE},
126 {0.5f, SK_ColorWHITE},
127 {0.5f, SK_ColorGRAY},
128 {0.5f, SK_ColorCYAN},
129 {0.5f, SK_ColorRED},
130 {1.0f, SK_ColorYELLOW}});
131
132 // Unsorted stops: Blue to white, red to yellow
133 // Unlike Chrome, we don't sort the stops, so this renders differently than the prior cell.
134 this->drawGradient(canvas,
135 {{0.5f, SK_ColorWHITE},
136 {0.5f, SK_ColorGRAY},
137 {1.0f, SK_ColorYELLOW},
138 {0.5f, SK_ColorCYAN},
139 {0.5f, SK_ColorRED},
140 {0.0f, SK_ColorBLUE}});
141 }
142
143private:
144 using INHERITED = skiagm::GM;
145};
146
147DEF_GM(return new FillrectGradientGM;)
static const int points[]
constexpr SkColor SK_ColorYELLOW
Definition SkColor.h:139
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorCYAN
Definition SkColor.h:143
constexpr SkColor SK_ColorGRAY
Definition SkColor.h:113
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
void drawGradient(SkCanvas *canvas, std::initializer_list< GradientStop > stops)
SkISize getISize() override
void onDraw(SkCanvas *canvas) override
SkString getName() const override
void drawRect(const SkRect &rect, const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
int save()
Definition SkCanvas.cpp:451
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)
const Paint & paint
const int kNumRows
const int kNumColumns
const int kPadSize
const int kCellSize
#define DEF_GM(CODE)
Definition gm.h:40
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static constexpr SkPoint Make(float x, float y)
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659