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