Chromium Code Reviews| Index: gm/hardstop_gradients.cpp |
| diff --git a/gm/hardstop_gradients.cpp b/gm/hardstop_gradients.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a04ba045752315d48390ed64583ff510310a323a |
| --- /dev/null |
| +++ b/gm/hardstop_gradients.cpp |
| @@ -0,0 +1,145 @@ |
| +/* |
| + * Copyright 2016 Google Inc. |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + */ |
| + |
| +/* |
| + * This GM presents a variety of different gradients with different |
| + * tile modes. Each entry in the table is a rectangle with a linear |
| + * gradient that spans from its left edge to its right edge. The rows |
| + * in the table represent different color/position configurations, |
| + * while the columns in the table represent different tile modes. In |
| + * order to highlight the differences between tile modes, the gradient |
| + * starts and ends at 30 pixel inset from either side of the rectangle. |
| + * |
| + * | Clamp Repeat Mirror |
| + * _____________________|___________________________________________ |
| + * 2-color | rect00 rect01 rect02 |
| + * 3-color | rect10 rect11 rect12 |
| + * 5-color hard stop | rect20 rect21 rect22 |
| + * 5-color edge case 1 | rect30 rect31 rect32 |
| + * 5-color edge case 2 | rect40 rect41 rect42 |
| + * |
| + * The LAST two t-values in edge case 1 are the same, while the FIRST |
| + * two t-values in edge case 2 are the same. |
| + */ |
| + |
| +#include "gm.h" |
| + |
| +#include "SkGradientShader.h" |
| + |
| +const int WIDTH = 500; |
| +const int HEIGHT = 500; |
| + |
| +const int NUM_ROWS = 5; |
| +const int NUM_COLS = 3; |
| + |
| +const int CELL_WIDTH = WIDTH / NUM_COLS; |
| +const int CELL_HEIGHT = HEIGHT / NUM_ROWS; |
| + |
| +const int PAD_WIDTH = 3; |
| +const int PAD_HEIGHT = 3; |
| + |
| +const int RECT_WIDTH = CELL_WIDTH - (2 * PAD_WIDTH); |
| +const int RECT_HEIGHT = CELL_HEIGHT - (2 * PAD_HEIGHT); |
| + |
| +static void shadeRect(SkCanvas* canvas, sk_sp<SkShader> shader, int cell_row, int cell_col) { |
| + SkPaint paint; |
| + paint.setShader(shader); |
| + |
| + SkRect rect = SkRect::MakeXYWH(cell_col * CELL_WIDTH + PAD_WIDTH, |
| + cell_row * CELL_HEIGHT + PAD_HEIGHT, |
| + RECT_WIDTH, |
| + RECT_HEIGHT); |
| + |
| + canvas->drawRect(rect, paint); |
| +} |
| + |
| +static void createGradientPoints(int cell_row, int cell_col, SkPoint points[2]) { |
| + const int X_OFFSET = 30; |
| + |
| + int x0 = cell_col * CELL_WIDTH + PAD_WIDTH + X_OFFSET; |
| + int x1 = (cell_col+1) * CELL_WIDTH - PAD_WIDTH - X_OFFSET; |
| + int y = cell_row * CELL_HEIGHT + PAD_HEIGHT + RECT_HEIGHT/2; |
| + |
| + points[0] = SkPoint::Make(x0, y); |
| + points[1] = SkPoint::Make(x1, y); |
| +} |
| + |
| +class HardstopGradientShaderGM : public skiagm::GM { |
| +public: |
| + HardstopGradientShaderGM() { |
| + |
| + } |
| + |
| +protected: |
| + SkString onShortName() override { |
| + return SkString("hardstop_gradients"); |
| + } |
| + |
| + SkISize onISize() override { |
| + return SkISize::Make(512, 512); |
| + } |
| + |
| + void onDraw(SkCanvas* canvas) { |
| + SkPoint points[2]; |
| + |
| + SkColor colors[] = { |
| + SK_ColorRED, |
| + SK_ColorGREEN, |
| + SK_ColorBLUE, |
| + SK_ColorYELLOW, |
| + SK_ColorMAGENTA, |
| + }; |
| + |
| + SkScalar row3[] = {0.00f, 0.25f, 0.50f, 0.50f, 1.00f}; |
| + SkScalar row4[] = {0.00f, 0.25f, 0.50f, 1.00f, 1.00f}; |
| + SkScalar row5[] = {0.00f, 0.00f, 0.50f, 0.50f, 1.00f}; |
| + |
| + SkScalar* positions[] = { |
| + nullptr, |
| + nullptr, |
| + row3, |
| + row4, |
| + row5, |
| + }; |
| + |
| + int num_gradient_colors[] = { |
| + 2, |
| + 3, |
| + 5, |
| + 5, |
| + 5, |
| + }; |
| + |
| + SkShader::TileMode tilemodes[] = { |
| + SkShader::kClamp_TileMode, |
| + SkShader::kRepeat_TileMode, |
| + SkShader::kMirror_TileMode, |
| + }; |
| + |
| + for (int cell_row = 0; cell_row < NUM_ROWS; cell_row++) { |
|
tomhudson
2016/06/22 15:54:06
Nit: revisit this function after reading https://s
|
| + for (int cell_col = 0; cell_col < NUM_COLS; cell_col++) { |
| + createGradientPoints(cell_row, cell_col, points); |
| + |
| + auto shader = SkGradientShader::MakeLinear( |
| + points, |
| + colors, |
| + positions[cell_row], |
| + num_gradient_colors[cell_row], |
| + tilemodes[cell_col], |
| + 0, |
| + nullptr); |
| + |
| + shadeRect(canvas, shader, cell_row, cell_col); |
| + } |
| + } |
| + } |
| + |
| +private: |
| + typedef skiagm::GM INHERITED; |
| +}; |
| + |
| +DEF_GM(return new HardstopGradientShaderGM;) |