blob: c1baa2f1c652002ae9920092292239a59ece1070 [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 *
17 * | Clamp Repeat Mirror
18 * _____________________|___________________________________________
19 * 2-color | rect00 rect01 rect02
20 * 3-color | rect10 rect11 rect12
21 * 5-color hard stop | rect20 rect21 rect22
22 * 5-color edge case 1 | rect30 rect31 rect32
23 * 5-color edge case 2 | rect40 rect41 rect42
24 *
25 * The LAST two t-values in edge case 1 are the same, while the FIRST
26 * two t-values in edge case 2 are the same.
27 */
28
29#include "gm.h"
30
31#include "SkGradientShader.h"
32
33const int WIDTH = 500;
34const int HEIGHT = 500;
35
36const int NUM_ROWS = 5;
37const int NUM_COLS = 3;
38
39const int CELL_WIDTH = WIDTH / NUM_COLS;
40const int CELL_HEIGHT = HEIGHT / NUM_ROWS;
41
42const int PAD_WIDTH = 3;
43const int PAD_HEIGHT = 3;
44
45const int RECT_WIDTH = CELL_WIDTH - (2 * PAD_WIDTH);
46const int RECT_HEIGHT = CELL_HEIGHT - (2 * PAD_HEIGHT);
47
48static void shade_rect(SkCanvas* canvas, sk_sp<SkShader> shader, int cellRow, int cellCol) {
49 SkPaint paint;
50 paint.setShader(shader);
51
52 SkRect rect = SkRect::MakeXYWH(SkIntToScalar(cellCol * CELL_WIDTH + PAD_WIDTH),
53 SkIntToScalar(cellRow * CELL_HEIGHT + PAD_HEIGHT),
54 SkIntToScalar(RECT_WIDTH),
55 SkIntToScalar(RECT_HEIGHT));
56
57 canvas->drawRect(rect, paint);
58}
59
60static void create_gradient_points(int cellRow, int cellCol, SkPoint points[2]) {
61 const int X_OFFSET = 30;
62
63 auto x0 = SkIntToScalar(cellCol * CELL_WIDTH + PAD_WIDTH + X_OFFSET);
64 auto x1 = SkIntToScalar((cellCol+1) * CELL_WIDTH - PAD_WIDTH - X_OFFSET);
65 auto y = SkIntToScalar(cellRow * CELL_HEIGHT + PAD_HEIGHT + RECT_HEIGHT/2);
66
67 points[0] = SkPoint::Make(x0, y);
68 points[1] = SkPoint::Make(x1, y);
69}
70
71class HardstopGradientShaderGM : public skiagm::GM {
72public:
73 HardstopGradientShaderGM() {
74
75 }
76
77protected:
78 SkString onShortName() override {
79 return SkString("hardstop_gradients");
80 }
81
82 SkISize onISize() override {
83 return SkISize::Make(512, 512);
84 }
85
86 void onDraw(SkCanvas* canvas) override {
87 SkPoint points[2];
88
89 SkColor colors[] = {
90 SK_ColorRED,
91 SK_ColorGREEN,
92 SK_ColorBLUE,
93 SK_ColorYELLOW,
94 SK_ColorMAGENTA,
95 };
96
97 SkScalar row3[] = {0.00f, 0.25f, 0.50f, 0.50f, 1.00f};
98 SkScalar row4[] = {0.00f, 0.25f, 0.50f, 1.00f, 1.00f};
99 SkScalar row5[] = {0.00f, 0.00f, 0.50f, 0.50f, 1.00f};
100
101 SkScalar* positions[] = {
102 nullptr,
103 nullptr,
104 row3,
105 row4,
106 row5,
107 };
108
109 int numGradientColors[] = {
110 2,
111 3,
112 5,
113 5,
114 5,
115 };
116
117 SkShader::TileMode tilemodes[] = {
118 SkShader::kClamp_TileMode,
119 SkShader::kRepeat_TileMode,
120 SkShader::kMirror_TileMode,
121 };
122
123 for (int cellRow = 0; cellRow < NUM_ROWS; cellRow++) {
124 for (int cellCol = 0; cellCol < NUM_COLS; cellCol++) {
125 create_gradient_points(cellRow, cellCol, points);
126
127 auto shader = SkGradientShader::MakeLinear(
128 points,
129 colors,
130 positions[cellRow],
131 numGradientColors[cellRow],
132 tilemodes[cellCol],
133 0,
134 nullptr);
135
136 shade_rect(canvas, shader, cellRow, cellCol);
137 }
138 }
139 }
140
141private:
142 typedef skiagm::GM INHERITED;
143};
144
145DEF_GM(return new HardstopGradientShaderGM;)