• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
10  * GrUnrolledBinaryGradientColorizer, 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.h"
38 
39 #include "SkGradientShader.h"
40 
41 // All positions must be divided by the target interval count, which will produce the expected
42 // normalized position array for that interval number (assuming an appropriate color count is
43 // provided).
44 const int M1_POSITIONS[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
45 const int M2_POSITIONS[] = { 0, 1,1, 2,2, 3,3, 4,4, 5,5, 6,6, 7,7, 8 };
46 const int M3_POSITIONS[] = { 0, 1, 2,2, 3, 4,4, 5, 6,6, 7, 8 };
47 const int M4_POSITIONS[] = { 0, 1,1, 2, 3,3, 4, 5,5, 6, 7,7, 8 };
48 
49 // Color count = index of first occurrence of interval count value in Mx_POSITIONS array.
50 const int INT1_COLOR_COUNTS[] = { 2, 2, 2, 2 };
51 const int INT2_COLOR_COUNTS[] = { 3, 4, 3, 4 };
52 const int INT3_COLOR_COUNTS[] = { 4, 6, 5, 5 };
53 const int INT4_COLOR_COUNTS[] = { 5, 8, 6, 7 };
54 const int INT5_COLOR_COUNTS[] = { 6, 10, 8, 8 };
55 const int INT6_COLOR_COUNTS[] = { 7, 12, 9, 10 };
56 const int INT7_COLOR_COUNTS[] = { 8, 14, 11, 11 };
57 const int INT8_COLOR_COUNTS[] = { 9, 16, 12, 13 };
58 
59 // Cycle through defined colors for positions 0 through 8.
60 const SkColor COLORS[] = {
61     SK_ColorDKGRAY,
62     SK_ColorRED,
63     SK_ColorYELLOW,
64     SK_ColorGREEN,
65     SK_ColorCYAN,
66     SK_ColorBLUE,
67     SK_ColorMAGENTA,
68     SK_ColorBLACK,
69     SK_ColorLTGRAY
70 };
71 
72 const int* INTERVAL_COLOR_COUNTS[] = {
73     INT1_COLOR_COUNTS,
74     INT2_COLOR_COUNTS,
75     INT3_COLOR_COUNTS,
76     INT4_COLOR_COUNTS,
77     INT5_COLOR_COUNTS,
78     INT6_COLOR_COUNTS,
79     INT7_COLOR_COUNTS,
80     INT8_COLOR_COUNTS
81 };
82 const int COLOR_COUNT = SK_ARRAY_COUNT(COLORS);
83 
84 const int* M_POSITIONS[] = {
85     M1_POSITIONS,
86     M2_POSITIONS,
87     M3_POSITIONS,
88     M4_POSITIONS
89 };
90 
91 const int WIDTH  = 500;
92 const int HEIGHT = 500;
93 
94 const int NUM_ROWS = 8;
95 const int NUM_COLS = 4;
96 
97 const int CELL_WIDTH  = WIDTH  / NUM_COLS;
98 const int CELL_HEIGHT = HEIGHT / NUM_ROWS;
99 
100 const int PAD_WIDTH  = 3;
101 const int PAD_HEIGHT = 3;
102 
103 const int RECT_WIDTH  = CELL_WIDTH  - (2 * PAD_WIDTH);
104 const int RECT_HEIGHT = CELL_HEIGHT - (2 * PAD_HEIGHT);
105 
shade_rect(SkCanvas * canvas,sk_sp<SkShader> shader,int cellRow,int cellCol)106 static void shade_rect(SkCanvas* canvas, sk_sp<SkShader> shader, int cellRow, int cellCol) {
107     SkPaint paint;
108     paint.setShader(shader);
109 
110     canvas->save();
111     canvas->translate(SkIntToScalar(cellCol * CELL_WIDTH + PAD_WIDTH),
112                       SkIntToScalar(cellRow * CELL_HEIGHT + PAD_HEIGHT));
113 
114     const SkRect rect = SkRect::MakeWH(SkIntToScalar(RECT_WIDTH), SkIntToScalar(RECT_HEIGHT));
115     canvas->drawRect(rect, paint);
116     canvas->restore();
117 }
118 
119 class AnalyticGradientShaderGM : public skiagm::GM {
120 public:
AnalyticGradientShaderGM()121     AnalyticGradientShaderGM() {
122 
123     }
124 
125 protected:
onShortName()126     SkString onShortName() override {
127         return SkString("analytic_gradients");
128     }
129 
onISize()130     SkISize onISize() override {
131         return SkISize::Make(1024, 512);
132     }
133 
onDraw(SkCanvas * canvas)134     void onDraw(SkCanvas* canvas) override {
135         const SkPoint points[2] = { SkPoint::Make(0, 0), SkPoint::Make(RECT_WIDTH, 0.0) };
136 
137         for (int cellRow = 0; cellRow < NUM_ROWS; cellRow++) {
138             // Each interval has 4 different color counts, one per mode
139             const int* colorCounts = INTERVAL_COLOR_COUNTS[cellRow]; // Has len = 4
140 
141             for (int cellCol = 0; cellCol < NUM_COLS; cellCol++) {
142                 // create_gradient_points(cellRow, cellCol, points);
143 
144                 // Get the color count dependent on interval and mode
145                 int colorCount = colorCounts[cellCol];
146                 // Get the positions given the mode
147                 const int* layout = M_POSITIONS[cellCol];
148 
149                 // Collect positions and colors specific to the interval+mode normalizing the
150                 // position based on the interval count (== cellRow+1)
151                 SkAutoSTMalloc<4, SkColor> colors(colorCount);
152                 SkAutoSTMalloc<4, SkScalar> positions(colorCount);
153                 int j = 0;
154                 for (int i = 0; i < colorCount; i++) {
155                     positions[i] = SkIntToScalar(layout[i]) / (cellRow + 1);
156                     colors[i] = COLORS[j % COLOR_COUNT];
157                     j++;
158                 }
159 
160                 auto shader = SkGradientShader::MakeLinear(
161                                 points,
162                                 colors.get(),
163                                 positions.get(),
164                                 colorCount,
165                                 SkShader::kClamp_TileMode,
166                                 0,
167                                 nullptr);
168 
169                 shade_rect(canvas, shader, cellRow, cellCol);
170             }
171         }
172     }
173 
174 private:
175     typedef skiagm::GM INHERITED;
176 };
177 
178 DEF_GM(return new AnalyticGradientShaderGM;)
179