Flutter Engine
The Flutter Engine
hardstop_gradients.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2016 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/*
9 * This GM presents a variety of different gradients with different
10 * tile modes. Each entry in the table is a rectangle with a linear
11 * gradient that spans from its left edge to its right edge. The rows
12 * in the table represent different color/position configurations,
13 * while the columns in the table represent different tile modes. In
14 * order to highlight the differences between tile modes, the gradient
15 * starts and ends at 30 pixel inset from either side of the rectangle.
16 *
17 * | Clamp Repeat Mirror
18 * _____________________________|___________________________________________
19 * 2-color | rect00 rect01 rect02
20 * 3-color even | rect10 rect11 rect12
21 * 3-color texture | rect20 rect21 rect22
22 * 5-color hard stop | rect30 rect31 rect32
23 * 4-color hard stop centered | rect40 rect41 rect42
24 * 3-color hard stop 001 | rect50 rect51 rect52
25 * 3-color hard stop 011 | rect60 rect61 rect62
26 * 4-color hard stop off-center | rect70 rect71 rect72
27 *
28 * The first three rows are cases covered by pre-hard-stop code; simple
29 * 2-color gradients, 3-color gradients with the middle color centered,
30 * and general gradients that are rendered from a texture atlas.
31 *
32 * The next four rows all deal with hard stop gradients. The fourth row
33 * is a generic hard stop gradient, while the three subsequent rows deal
34 * with special cases of hard stop gradients; centered hard stop gradients
35 * (with t-values 0, 0.5, 0.5, 1), and two edge cases (with t-values
36 * 0, 0, 1 and 0, 1, 1). The final row has a single off-center hard stop.
37 */
38
39#include "gm/gm.h"
44#include "include/core/SkRect.h"
48#include "include/core/SkSize.h"
52
53const int WIDTH = 500;
54const int HEIGHT = 500;
55
56const int NUM_ROWS = 8;
57const int NUM_COLS = 3;
58
61
62const int PAD_WIDTH = 3;
63const int PAD_HEIGHT = 3;
64
65const int RECT_WIDTH = CELL_WIDTH - (2 * PAD_WIDTH);
67
68static void shade_rect(SkCanvas* canvas, sk_sp<SkShader> shader, int cellRow, int cellCol) {
70 paint.setShader(shader);
71
76
77 canvas->drawRect(rect, paint);
78}
79
80static void create_gradient_points(int cellRow, int cellCol, SkPoint points[2]) {
81 const int X_OFFSET = 30;
82
83 auto x0 = SkIntToScalar(cellCol * CELL_WIDTH + PAD_WIDTH + X_OFFSET);
84 auto x1 = SkIntToScalar((cellCol+1) * CELL_WIDTH - PAD_WIDTH - X_OFFSET);
85 auto y = SkIntToScalar(cellRow * CELL_HEIGHT + PAD_HEIGHT + RECT_HEIGHT/2);
86
87 points[0] = SkPoint::Make(x0, y);
88 points[1] = SkPoint::Make(x1, y);
89}
90
92public:
94
95 }
96
97protected:
98 SkString getName() const override { return SkString("hardstop_gradients"); }
99
100 SkISize getISize() override { return SkISize::Make(512, 512); }
101
102 void onDraw(SkCanvas* canvas) override {
103 SkPoint points[2];
104
105 SkColor colors[] = {
111 };
112
113 SkScalar row3[] = {0.00f, 0.25f, 1.00f};
114 SkScalar row4[] = {0.00f, 0.25f, 0.50f, 0.50f, 1.00f};
115 SkScalar row5[] = {0.00f, 0.50f, 0.50f, 1.00f};
116 SkScalar row6[] = {0.00f, 0.00f, 1.00f};
117 SkScalar row7[] = {0.00f, 1.00f, 1.00f};
118 SkScalar row8[] = {0.00f, 0.30f, 0.30f, 1.00f};
119
120 SkScalar* positions[NUM_ROWS] = {
121 nullptr,
122 nullptr,
123 row3,
124 row4,
125 row5,
126 row6,
127 row7,
128 row8,
129 };
130
131 int numGradientColors[NUM_ROWS] = {
132 2,
133 3,
134 3,
135 5,
136 4,
137 3,
138 3,
139 4,
140 };
141
142 SkTileMode tilemodes[NUM_COLS] = {
146 };
147
148 for (int cellRow = 0; cellRow < NUM_ROWS; cellRow++) {
149 for (int cellCol = 0; cellCol < NUM_COLS; cellCol++) {
150 create_gradient_points(cellRow, cellCol, points);
151
152 auto shader = SkGradientShader::MakeLinear(
153 points,
154 colors,
155 positions[cellRow],
156 numGradientColors[cellRow],
157 tilemodes[cellCol],
158 0,
159 nullptr);
160
161 shade_rect(canvas, shader, cellRow, cellCol);
162 }
163 }
164 }
165
166private:
167 using INHERITED = skiagm::GM;
168};
169
static const int points[]
constexpr SkColor SK_ColorYELLOW
Definition: SkColor.h:139
constexpr SkColor SK_ColorMAGENTA
Definition: SkColor.h:147
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
#define SkIntToScalar(x)
Definition: SkScalar.h:57
SkTileMode
Definition: SkTileMode.h:13
SkString getName() const override
void onDraw(SkCanvas *canvas) override
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
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)
Definition: gm.h:110
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
#define DEF_GM(CODE)
Definition: gm.h:40
const int RECT_HEIGHT
const int CELL_WIDTH
const int PAD_WIDTH
const int PAD_HEIGHT
static void shade_rect(SkCanvas *canvas, sk_sp< SkShader > shader, int cellRow, int cellCol)
const int WIDTH
const int NUM_COLS
const int RECT_WIDTH
const int CELL_HEIGHT
static void create_gradient_points(int cellRow, int cellCol, SkPoint points[2])
const int NUM_ROWS
const int HEIGHT
double y
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
PODArray< SkColor > colors
Definition: SkRecords.h:276
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition: SkRect.h:659