1/*
2 * Copyright 2018 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 gradients meant to test the correctness of the analytic unrolled
10 * binary gradient colorizer, which can handle arbitrary gradients with 1 to 8 interpolation
11 * intervals. These intervals can be either hardstops or smooth color transitions.
12 *
13 * It produces an image similar to that of GM_hardstop_gradients, but is arranged as follows:
14 *
15 *            | Clamp          |
16 *            |________________|
17 *            | M1  M2  M3  M4 |
18 * ___________|________________|
19 *      1     |
20 *      2     |
21 *      3     |
22 *      4     |
23 *      5     |
24 *      6     |
25 *      7     |
26 *      8     |
27 * The M-modes are different ways of interlveaving hardstops with smooth transitions:
28 *   - M1 = All smooth transitions
29 *   - M2 = All hard stops
30 *   - M5 = Alternating smooth then hard
31 *   - M6 = Alternating hard then smooth
32 *
33 * Only clamping is tested since this is focused more on within the interpolation region behavior,
34 * compared to overall behavior.
35 */
36
37#include "gm/gm.h"
38#include "include/core/SkCanvas.h"
39#include "include/core/SkColor.h"
40#include "include/core/SkPaint.h"
41#include "include/core/SkPoint.h"
42#include "include/core/SkRect.h"
43#include "include/core/SkRefCnt.h"
44#include "include/core/SkScalar.h"
45#include "include/core/SkShader.h"
46#include "include/core/SkSize.h"
47#include "include/core/SkString.h"
48#include "include/core/SkTileMode.h"
49#include "include/core/SkTypes.h"
50#include "include/effects/SkGradientShader.h"
51#include "include/private/SkTemplates.h"
52
53// All positions must be divided by the target interval count, which will produce the expected
54// normalized position array for that interval number (assuming an appropriate color count is
55// provided).
56const int M1_POSITIONS[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
57const int M2_POSITIONS[] = { 0, 1,1, 2,2, 3,3, 4,4, 5,5, 6,6, 7,7, 8 };
58const int M3_POSITIONS[] = { 0, 1, 2,2, 3, 4,4, 5, 6,6, 7, 8 };
59const int M4_POSITIONS[] = { 0, 1,1, 2, 3,3, 4, 5,5, 6, 7,7, 8 };
60
61// Color count = index of first occurrence of interval count value in Mx_POSITIONS array.
62const int INT1_COLOR_COUNTS[] = { 2, 2, 2, 2 };
63const int INT2_COLOR_COUNTS[] = { 3, 4, 3, 4 };
64const int INT3_COLOR_COUNTS[] = { 4, 6, 5, 5 };
65const int INT4_COLOR_COUNTS[] = { 5, 8, 6, 7 };
66const int INT5_COLOR_COUNTS[] = { 6, 10, 8, 8 };
67const int INT6_COLOR_COUNTS[] = { 7, 12, 9, 10 };
68const int INT7_COLOR_COUNTS[] = { 8, 14, 11, 11 };
69const int INT8_COLOR_COUNTS[] = { 9, 16, 12, 13 };
70
71// Cycle through defined colors for positions 0 through 8.
72const SkColor COLORS[] = {
73    SK_ColorDKGRAY,
74    SK_ColorRED,
75    SK_ColorYELLOW,
76    SK_ColorGREEN,
77    SK_ColorCYAN,
78    SK_ColorBLUE,
79    SK_ColorMAGENTA,
80    SK_ColorBLACK,
81    SK_ColorLTGRAY
82};
83
84const int* INTERVAL_COLOR_COUNTS[] = {
85    INT1_COLOR_COUNTS,
86    INT2_COLOR_COUNTS,
87    INT3_COLOR_COUNTS,
88    INT4_COLOR_COUNTS,
89    INT5_COLOR_COUNTS,
90    INT6_COLOR_COUNTS,
91    INT7_COLOR_COUNTS,
92    INT8_COLOR_COUNTS
93};
94const int COLOR_COUNT = SK_ARRAY_COUNT(COLORS);
95
96const int* M_POSITIONS[] = {
97    M1_POSITIONS,
98    M2_POSITIONS,
99    M3_POSITIONS,
100    M4_POSITIONS
101};
102
103const int WIDTH  = 500;
104const int HEIGHT = 500;
105
106const int NUM_ROWS = 8;
107const int NUM_COLS = 4;
108
109const int CELL_WIDTH  = WIDTH  / NUM_COLS;
110const int CELL_HEIGHT = HEIGHT / NUM_ROWS;
111
112const int PAD_WIDTH  = 3;
113const int PAD_HEIGHT = 3;
114
115const int RECT_WIDTH  = CELL_WIDTH  - (2 * PAD_WIDTH);
116const int RECT_HEIGHT = CELL_HEIGHT - (2 * PAD_HEIGHT);
117
118static void shade_rect(SkCanvas* canvas, sk_sp<SkShader> shader, int cellRow, int cellCol) {
119    SkPaint paint;
120    paint.setShader(shader);
121
122    canvas->save();
123    canvas->translate(SkIntToScalar(cellCol * CELL_WIDTH + PAD_WIDTH),
124                      SkIntToScalar(cellRow * CELL_HEIGHT + PAD_HEIGHT));
125
126    const SkRect rect = SkRect::MakeWH(SkIntToScalar(RECT_WIDTH), SkIntToScalar(RECT_HEIGHT));
127    canvas->drawRect(rect, paint);
128    canvas->restore();
129}
130
131class AnalyticGradientShaderGM : public skiagm::GM {
132public:
133    AnalyticGradientShaderGM() {
134
135    }
136
137protected:
138    SkString onShortName() override {
139        return SkString("analytic_gradients");
140    }
141
142    SkISize onISize() override {
143        return SkISize::Make(1024, 512);
144    }
145
146    void onDraw(SkCanvas* canvas) override {
147        const SkPoint points[2] = { SkPoint::Make(0, 0), SkPoint::Make(RECT_WIDTH, 0.0) };
148
149        for (int cellRow = 0; cellRow < NUM_ROWS; cellRow++) {
150            // Each interval has 4 different color counts, one per mode
151            const int* colorCounts = INTERVAL_COLOR_COUNTS[cellRow]; // Has len = 4
152
153            for (int cellCol = 0; cellCol < NUM_COLS; cellCol++) {
154                // create_gradient_points(cellRow, cellCol, points);
155
156                // Get the color count dependent on interval and mode
157                int colorCount = colorCounts[cellCol];
158                // Get the positions given the mode
159                const int* layout = M_POSITIONS[cellCol];
160
161                // Collect positions and colors specific to the interval+mode normalizing the
162                // position based on the interval count (== cellRow+1)
163                SkAutoSTMalloc<4, SkColor> colors(colorCount);
164                SkAutoSTMalloc<4, SkScalar> positions(colorCount);
165                int j = 0;
166                for (int i = 0; i < colorCount; i++) {
167                    positions[i] = SkIntToScalar(layout[i]) / (cellRow + 1);
168                    colors[i] = COLORS[j % COLOR_COUNT];
169                    j++;
170                }
171
172                auto shader = SkGradientShader::MakeLinear(
173                                points,
174                                colors.get(),
175                                positions.get(),
176                                colorCount,
177                                SkTileMode::kClamp,
178                                0,
179                                nullptr);
180
181                shade_rect(canvas, shader, cellRow, cellCol);
182            }
183        }
184    }
185
186private:
187    using INHERITED = skiagm::GM;
188};
189
190DEF_GM(return new AnalyticGradientShaderGM;)
191