blob: 846465ec19d7cde3a34b53024077c90517b75c7f [file] [log] [blame]
fmenozzi17b64b22016-06-22 13:39:25 -07001/*
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 *
fmenozzi7a9f3762016-08-03 07:58:06 -070017 * | 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
fmenozzi17b64b22016-06-22 13:39:25 -070026 *
fmenozzi7a9f3762016-08-03 07:58:06 -070027 * The first three rows are cases covered by pre-hard-stop code; simple
28 * 2-color gradients, 3-color gradients with the middle color centered,
29 * and general gradients that are rendered from a texture atlas.
30 *
31 * The next four rows all deal with hard stop gradients. The fourth row
32 * is a generic hard stop gradient, while the three subsequent rows deal
33 * with special cases of hard stop gradients; centered hard stop gradients
34 * (with t-values 0, 0.5, 0.5, 1), and two edge cases (with t-values
35 * 0, 0, 1 and 0, 1, 1).
fmenozzi17b64b22016-06-22 13:39:25 -070036 */
37
38#include "gm.h"
39
40#include "SkGradientShader.h"
41
42const int WIDTH = 500;
43const int HEIGHT = 500;
44
fmenozzi7a9f3762016-08-03 07:58:06 -070045const int NUM_ROWS = 7;
fmenozzi17b64b22016-06-22 13:39:25 -070046const int NUM_COLS = 3;
47
48const int CELL_WIDTH = WIDTH / NUM_COLS;
49const int CELL_HEIGHT = HEIGHT / NUM_ROWS;
50
51const int PAD_WIDTH = 3;
52const int PAD_HEIGHT = 3;
53
54const int RECT_WIDTH = CELL_WIDTH - (2 * PAD_WIDTH);
55const int RECT_HEIGHT = CELL_HEIGHT - (2 * PAD_HEIGHT);
56
57static void shade_rect(SkCanvas* canvas, sk_sp<SkShader> shader, int cellRow, int cellCol) {
58 SkPaint paint;
59 paint.setShader(shader);
60
61 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(cellCol * CELL_WIDTH + PAD_WIDTH),
62 SkIntToScalar(cellRow * CELL_HEIGHT + PAD_HEIGHT),
63 SkIntToScalar(RECT_WIDTH),
64 SkIntToScalar(RECT_HEIGHT));
65
66 canvas->drawRect(rect, paint);
67}
68
69static void create_gradient_points(int cellRow, int cellCol, SkPoint points[2]) {
70 const int X_OFFSET = 30;
71
fmenozzi7a9f3762016-08-03 07:58:06 -070072 auto x0 = SkIntToScalar(cellCol * CELL_WIDTH + PAD_WIDTH + X_OFFSET);
73 auto x1 = SkIntToScalar((cellCol+1) * CELL_WIDTH - PAD_WIDTH - X_OFFSET);
74 auto y = SkIntToScalar(cellRow * CELL_HEIGHT + PAD_HEIGHT + RECT_HEIGHT/2);
fmenozzi17b64b22016-06-22 13:39:25 -070075
76 points[0] = SkPoint::Make(x0, y);
77 points[1] = SkPoint::Make(x1, y);
78}
79
80class HardstopGradientShaderGM : public skiagm::GM {
81public:
82 HardstopGradientShaderGM() {
83
84 }
85
86protected:
87 SkString onShortName() override {
88 return SkString("hardstop_gradients");
89 }
90
91 SkISize onISize() override {
92 return SkISize::Make(512, 512);
93 }
94
95 void onDraw(SkCanvas* canvas) override {
96 SkPoint points[2];
97
98 SkColor colors[] = {
99 SK_ColorRED,
100 SK_ColorGREEN,
101 SK_ColorBLUE,
102 SK_ColorYELLOW,
103 SK_ColorMAGENTA,
104 };
105
fmenozzi7a9f3762016-08-03 07:58:06 -0700106 SkScalar row3[] = {0.00f, 0.25f, 1.00f};
107 SkScalar row4[] = {0.00f, 0.25f, 0.50f, 0.50f, 1.00f};
108 SkScalar row5[] = {0.00f, 0.50f, 0.50f, 1.00f};
109 SkScalar row6[] = {0.00f, 0.00f, 1.00f};
110 SkScalar row7[] = {0.00f, 1.00f, 1.00f};
fmenozzi17b64b22016-06-22 13:39:25 -0700111
fmenozzi7a9f3762016-08-03 07:58:06 -0700112 SkScalar* positions[NUM_ROWS] = {
fmenozzi17b64b22016-06-22 13:39:25 -0700113 nullptr,
114 nullptr,
115 row3,
116 row4,
117 row5,
fmenozzi7a9f3762016-08-03 07:58:06 -0700118 row6,
119 row7,
fmenozzi17b64b22016-06-22 13:39:25 -0700120 };
121
fmenozzi7a9f3762016-08-03 07:58:06 -0700122 int numGradientColors[NUM_ROWS] = {
fmenozzi17b64b22016-06-22 13:39:25 -0700123 2,
124 3,
fmenozzi7a9f3762016-08-03 07:58:06 -0700125 3,
fmenozzi17b64b22016-06-22 13:39:25 -0700126 5,
fmenozzi7a9f3762016-08-03 07:58:06 -0700127 4,
128 3,
129 3,
fmenozzi17b64b22016-06-22 13:39:25 -0700130 };
131
fmenozzi7a9f3762016-08-03 07:58:06 -0700132 SkShader::TileMode tilemodes[NUM_COLS] = {
fmenozzi17b64b22016-06-22 13:39:25 -0700133 SkShader::kClamp_TileMode,
134 SkShader::kRepeat_TileMode,
135 SkShader::kMirror_TileMode,
136 };
137
138 for (int cellRow = 0; cellRow < NUM_ROWS; cellRow++) {
139 for (int cellCol = 0; cellCol < NUM_COLS; cellCol++) {
140 create_gradient_points(cellRow, cellCol, points);
141
142 auto shader = SkGradientShader::MakeLinear(
143 points,
144 colors,
145 positions[cellRow],
146 numGradientColors[cellRow],
147 tilemodes[cellCol],
148 0,
149 nullptr);
150
151 shade_rect(canvas, shader, cellRow, cellCol);
152 }
153 }
154 }
155
156private:
157 typedef skiagm::GM INHERITED;
158};
159
160DEF_GM(return new HardstopGradientShaderGM;)