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