fmenozzi | 17b64b2 | 2016-06-22 13:39:25 -0700 | [diff] [blame] | 1 | /* |
| 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 | * |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 17 | * | 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 |
| 26 | * 4-color hard stop off-center | rect70 rect71 rect72 |
fmenozzi | 17b64b2 | 2016-06-22 13:39:25 -0700 | [diff] [blame] | 27 | * |
fmenozzi | 7a9f376 | 2016-08-03 07:58:06 -0700 | [diff] [blame] | 28 | * The first three rows are cases covered by pre-hard-stop code; simple |
| 29 | * 2-color gradients, 3-color gradients with the middle color centered, |
| 30 | * and general gradients that are rendered from a texture atlas. |
| 31 | * |
| 32 | * The next four rows all deal with hard stop gradients. The fourth row |
| 33 | * is a generic hard stop gradient, while the three subsequent rows deal |
| 34 | * with special cases of hard stop gradients; centered hard stop gradients |
| 35 | * (with t-values 0, 0.5, 0.5, 1), and two edge cases (with t-values |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 36 | * 0, 0, 1 and 0, 1, 1). The final row has a single off-center hard stop. |
fmenozzi | 17b64b2 | 2016-06-22 13:39:25 -0700 | [diff] [blame] | 37 | */ |
| 38 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 39 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 40 | #include "include/core/SkCanvas.h" |
| 41 | #include "include/core/SkColor.h" |
| 42 | #include "include/core/SkPaint.h" |
| 43 | #include "include/core/SkPoint.h" |
| 44 | #include "include/core/SkRect.h" |
| 45 | #include "include/core/SkRefCnt.h" |
| 46 | #include "include/core/SkScalar.h" |
| 47 | #include "include/core/SkShader.h" |
| 48 | #include "include/core/SkSize.h" |
| 49 | #include "include/core/SkString.h" |
| 50 | #include "include/core/SkTileMode.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 51 | #include "include/effects/SkGradientShader.h" |
fmenozzi | 17b64b2 | 2016-06-22 13:39:25 -0700 | [diff] [blame] | 52 | |
| 53 | const int WIDTH = 500; |
| 54 | const int HEIGHT = 500; |
| 55 | |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 56 | const int NUM_ROWS = 8; |
fmenozzi | 17b64b2 | 2016-06-22 13:39:25 -0700 | [diff] [blame] | 57 | const int NUM_COLS = 3; |
| 58 | |
| 59 | const int CELL_WIDTH = WIDTH / NUM_COLS; |
| 60 | const int CELL_HEIGHT = HEIGHT / NUM_ROWS; |
| 61 | |
| 62 | const int PAD_WIDTH = 3; |
| 63 | const int PAD_HEIGHT = 3; |
| 64 | |
| 65 | const int RECT_WIDTH = CELL_WIDTH - (2 * PAD_WIDTH); |
| 66 | const int RECT_HEIGHT = CELL_HEIGHT - (2 * PAD_HEIGHT); |
| 67 | |
| 68 | static void shade_rect(SkCanvas* canvas, sk_sp<SkShader> shader, int cellRow, int cellCol) { |
| 69 | SkPaint paint; |
| 70 | paint.setShader(shader); |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 71 | |
fmenozzi | 17b64b2 | 2016-06-22 13:39:25 -0700 | [diff] [blame] | 72 | SkRect rect = SkRect::MakeXYWH(SkIntToScalar(cellCol * CELL_WIDTH + PAD_WIDTH), |
| 73 | SkIntToScalar(cellRow * CELL_HEIGHT + PAD_HEIGHT), |
| 74 | SkIntToScalar(RECT_WIDTH), |
| 75 | SkIntToScalar(RECT_HEIGHT)); |
| 76 | |
| 77 | canvas->drawRect(rect, paint); |
| 78 | } |
| 79 | |
| 80 | static void create_gradient_points(int cellRow, int cellCol, SkPoint points[2]) { |
| 81 | const int X_OFFSET = 30; |
| 82 | |
fmenozzi | 7a9f376 | 2016-08-03 07:58:06 -0700 | [diff] [blame] | 83 | auto x0 = SkIntToScalar(cellCol * CELL_WIDTH + PAD_WIDTH + X_OFFSET); |
| 84 | auto x1 = SkIntToScalar((cellCol+1) * CELL_WIDTH - PAD_WIDTH - X_OFFSET); |
| 85 | auto y = SkIntToScalar(cellRow * CELL_HEIGHT + PAD_HEIGHT + RECT_HEIGHT/2); |
fmenozzi | 17b64b2 | 2016-06-22 13:39:25 -0700 | [diff] [blame] | 86 | |
| 87 | points[0] = SkPoint::Make(x0, y); |
| 88 | points[1] = SkPoint::Make(x1, y); |
| 89 | } |
| 90 | |
| 91 | class HardstopGradientShaderGM : public skiagm::GM { |
| 92 | public: |
| 93 | HardstopGradientShaderGM() { |
| 94 | |
| 95 | } |
| 96 | |
| 97 | protected: |
| 98 | SkString onShortName() override { |
| 99 | return SkString("hardstop_gradients"); |
| 100 | } |
| 101 | |
| 102 | SkISize onISize() override { |
| 103 | return SkISize::Make(512, 512); |
| 104 | } |
| 105 | |
| 106 | void onDraw(SkCanvas* canvas) override { |
| 107 | SkPoint points[2]; |
| 108 | |
| 109 | SkColor colors[] = { |
| 110 | SK_ColorRED, |
| 111 | SK_ColorGREEN, |
| 112 | SK_ColorBLUE, |
| 113 | SK_ColorYELLOW, |
| 114 | SK_ColorMAGENTA, |
| 115 | }; |
| 116 | |
fmenozzi | 7a9f376 | 2016-08-03 07:58:06 -0700 | [diff] [blame] | 117 | SkScalar row3[] = {0.00f, 0.25f, 1.00f}; |
| 118 | SkScalar row4[] = {0.00f, 0.25f, 0.50f, 0.50f, 1.00f}; |
| 119 | SkScalar row5[] = {0.00f, 0.50f, 0.50f, 1.00f}; |
| 120 | SkScalar row6[] = {0.00f, 0.00f, 1.00f}; |
| 121 | SkScalar row7[] = {0.00f, 1.00f, 1.00f}; |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 122 | SkScalar row8[] = {0.00f, 0.30f, 0.30f, 1.00f}; |
fmenozzi | 17b64b2 | 2016-06-22 13:39:25 -0700 | [diff] [blame] | 123 | |
fmenozzi | 7a9f376 | 2016-08-03 07:58:06 -0700 | [diff] [blame] | 124 | SkScalar* positions[NUM_ROWS] = { |
fmenozzi | 17b64b2 | 2016-06-22 13:39:25 -0700 | [diff] [blame] | 125 | nullptr, |
| 126 | nullptr, |
| 127 | row3, |
| 128 | row4, |
| 129 | row5, |
fmenozzi | 7a9f376 | 2016-08-03 07:58:06 -0700 | [diff] [blame] | 130 | row6, |
| 131 | row7, |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 132 | row8, |
fmenozzi | 17b64b2 | 2016-06-22 13:39:25 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
fmenozzi | 7a9f376 | 2016-08-03 07:58:06 -0700 | [diff] [blame] | 135 | int numGradientColors[NUM_ROWS] = { |
fmenozzi | 17b64b2 | 2016-06-22 13:39:25 -0700 | [diff] [blame] | 136 | 2, |
| 137 | 3, |
fmenozzi | 7a9f376 | 2016-08-03 07:58:06 -0700 | [diff] [blame] | 138 | 3, |
fmenozzi | 17b64b2 | 2016-06-22 13:39:25 -0700 | [diff] [blame] | 139 | 5, |
fmenozzi | 7a9f376 | 2016-08-03 07:58:06 -0700 | [diff] [blame] | 140 | 4, |
| 141 | 3, |
| 142 | 3, |
Brian Salomon | 466ad99 | 2016-10-13 16:08:36 -0400 | [diff] [blame] | 143 | 4, |
fmenozzi | 17b64b2 | 2016-06-22 13:39:25 -0700 | [diff] [blame] | 144 | }; |
| 145 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 146 | SkTileMode tilemodes[NUM_COLS] = { |
| 147 | SkTileMode::kClamp, |
| 148 | SkTileMode::kRepeat, |
| 149 | SkTileMode::kMirror, |
fmenozzi | 17b64b2 | 2016-06-22 13:39:25 -0700 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | for (int cellRow = 0; cellRow < NUM_ROWS; cellRow++) { |
| 153 | for (int cellCol = 0; cellCol < NUM_COLS; cellCol++) { |
| 154 | create_gradient_points(cellRow, cellCol, points); |
| 155 | |
| 156 | auto shader = SkGradientShader::MakeLinear( |
| 157 | points, |
| 158 | colors, |
| 159 | positions[cellRow], |
| 160 | numGradientColors[cellRow], |
| 161 | tilemodes[cellCol], |
| 162 | 0, |
| 163 | nullptr); |
| 164 | |
| 165 | shade_rect(canvas, shader, cellRow, cellCol); |
| 166 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 167 | } |
fmenozzi | 17b64b2 | 2016-06-22 13:39:25 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | private: |
| 171 | typedef skiagm::GM INHERITED; |
| 172 | }; |
| 173 | |
| 174 | DEF_GM(return new HardstopGradientShaderGM;) |