commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | #include "SkCanvas.h" |
| 9 | #include "SkColor.h" |
| 10 | #include "SkGradientShader.h" |
| 11 | #include "SkMatrix.h" |
| 12 | #include "SkPaint.h" |
| 13 | #include "SkPoint.h" |
| 14 | #include "SkRect.h" |
| 15 | #include "SkRefCnt.h" |
| 16 | #include "SkScalar.h" |
| 17 | #include "SkSize.h" |
| 18 | #include "SkString.h" |
| 19 | |
| 20 | #include "gm.h" |
| 21 | |
| 22 | static const SkColor gColors[] = { |
| 23 | SK_ColorRED, SK_ColorYELLOW |
| 24 | }; |
| 25 | |
commit-bot@chromium.org | 4567209 | 2013-06-12 23:36:50 +0000 | [diff] [blame] | 26 | // These annoying defines are necessary, because the only other alternative |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 27 | // is to use SkIntToScalar(...) everywhere. |
commit-bot@chromium.org | 4567209 | 2013-06-12 23:36:50 +0000 | [diff] [blame] | 28 | static const SkScalar sZero = 0; |
| 29 | static const SkScalar sHalf = SK_ScalarHalf; |
| 30 | static const SkScalar sOne = SK_Scalar1; |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 31 | |
commit-bot@chromium.org | 4567209 | 2013-06-12 23:36:50 +0000 | [diff] [blame] | 32 | // These arrays define the gradient stop points |
| 33 | // as x1, y1, x2, y2 per gradient to draw. |
| 34 | static const SkPoint linearPts[][2] = { |
| 35 | {{sZero, sZero}, {sOne, sZero}}, |
| 36 | {{sZero, sZero}, {sZero, sOne}}, |
| 37 | {{sOne, sZero}, {sZero, sZero}}, |
| 38 | {{sZero, sOne}, {sZero, sZero}}, |
| 39 | |
| 40 | {{sZero, sZero}, {sOne, sOne}}, |
| 41 | {{sOne, sOne}, {sZero, sZero}}, |
| 42 | {{sOne, sZero}, {sZero, sOne}}, |
| 43 | {{sZero, sOne}, {sOne, sZero}} |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
commit-bot@chromium.org | 1ac1cf6 | 2013-06-12 21:47:39 +0000 | [diff] [blame] | 46 | static const SkPoint radialPts[][2] = { |
commit-bot@chromium.org | 4567209 | 2013-06-12 23:36:50 +0000 | [diff] [blame] | 47 | {{sZero, sHalf}, {sOne, sHalf}}, |
| 48 | {{sHalf, sZero}, {sHalf, sOne}}, |
| 49 | {{sOne, sHalf}, {sZero, sHalf}}, |
| 50 | {{sHalf, sOne}, {sHalf, sZero}}, |
commit-bot@chromium.org | 1ac1cf6 | 2013-06-12 21:47:39 +0000 | [diff] [blame] | 51 | |
commit-bot@chromium.org | 4567209 | 2013-06-12 23:36:50 +0000 | [diff] [blame] | 52 | {{sZero, sZero}, {sOne, sOne}}, |
| 53 | {{sOne, sOne}, {sZero, sZero}}, |
| 54 | {{sOne, sZero}, {sZero, sOne}}, |
| 55 | {{sZero, sOne}, {sOne, sZero}} |
commit-bot@chromium.org | 1ac1cf6 | 2013-06-12 21:47:39 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
commit-bot@chromium.org | 4567209 | 2013-06-12 23:36:50 +0000 | [diff] [blame] | 58 | // These define the pixels allocated to each gradient image. |
| 59 | static const SkScalar TESTGRID_X = SkIntToScalar(200); |
| 60 | static const SkScalar TESTGRID_Y = SkIntToScalar(200); |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 61 | |
| 62 | static const int IMAGES_X = 4; // number of images per row |
| 63 | |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 64 | static SkShader* make_linear_gradient(const SkPoint pts[2], const SkMatrix& localMatrix) { |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 65 | return SkGradientShader::CreateLinear(pts, gColors, NULL, SK_ARRAY_COUNT(gColors), |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 66 | SkShader::kClamp_TileMode, 0, &localMatrix); |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 67 | } |
| 68 | |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 69 | static SkShader* make_radial_gradient(const SkPoint pts[2], const SkMatrix& localMatrix) { |
commit-bot@chromium.org | 1ac1cf6 | 2013-06-12 21:47:39 +0000 | [diff] [blame] | 70 | SkPoint center; |
| 71 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 72 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 73 | float radius = (center - pts[0]).length(); |
| 74 | return SkGradientShader::CreateRadial(center, radius, gColors, NULL, SK_ARRAY_COUNT(gColors), |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 75 | SkShader::kClamp_TileMode, 0, &localMatrix); |
commit-bot@chromium.org | 1ac1cf6 | 2013-06-12 21:47:39 +0000 | [diff] [blame] | 76 | } |
| 77 | |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 78 | static void draw_gradients(SkCanvas* canvas, |
| 79 | SkShader* (*makeShader)(const SkPoint[2], const SkMatrix&), |
commit-bot@chromium.org | 1ac1cf6 | 2013-06-12 21:47:39 +0000 | [diff] [blame] | 80 | const SkPoint ptsArray[][2], int numImages) { |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 81 | // Use some nice prime numbers for the rectangle and matrix with |
| 82 | // different scaling along the x and y axes (which is the bug this |
| 83 | // test addresses, where incorrect order of operations mixed up the axes) |
commit-bot@chromium.org | 4567209 | 2013-06-12 23:36:50 +0000 | [diff] [blame] | 84 | SkRect rectGrad = { |
| 85 | SkIntToScalar(43), SkIntToScalar(61), |
| 86 | SkIntToScalar(181), SkIntToScalar(167) }; |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 87 | SkMatrix shaderMat; |
| 88 | shaderMat.setScale(rectGrad.width(), rectGrad.height()); |
| 89 | shaderMat.postTranslate(rectGrad.left(), rectGrad.top()); |
| 90 | |
| 91 | canvas->save(); |
| 92 | for (int i = 0; i < numImages; i++) { |
| 93 | // Advance line downwards if necessary. |
| 94 | if (i % IMAGES_X == 0 && i != 0) { |
| 95 | canvas->restore(); |
| 96 | canvas->translate(0, TESTGRID_Y); |
| 97 | canvas->save(); |
| 98 | } |
| 99 | |
| 100 | // Setup shader and draw. |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 101 | SkAutoTUnref<SkShader> shader(makeShader(*ptsArray, shaderMat)); |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 102 | |
| 103 | SkPaint paint; |
| 104 | paint.setShader(shader); |
| 105 | canvas->drawRect(rectGrad, paint); |
| 106 | |
| 107 | // Advance to next position. |
| 108 | canvas->translate(TESTGRID_X, 0); |
| 109 | ptsArray++; |
| 110 | } |
| 111 | canvas->restore(); |
| 112 | } |
| 113 | |
| 114 | namespace skiagm { |
| 115 | |
| 116 | class GradientMatrixGM : public GM { |
| 117 | public: |
| 118 | GradientMatrixGM() { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 119 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 123 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 124 | SkString onShortName() override { |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 125 | return SkString("gradient_matrix"); |
| 126 | } |
| 127 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 128 | SkISize onISize() override { |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 129 | return SkISize::Make(800, 800); |
| 130 | } |
| 131 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 132 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 133 | draw_gradients(canvas, &make_linear_gradient, |
| 134 | linearPts, SK_ARRAY_COUNT(linearPts)); |
commit-bot@chromium.org | 1ac1cf6 | 2013-06-12 21:47:39 +0000 | [diff] [blame] | 135 | |
| 136 | canvas->translate(0, TESTGRID_Y); |
| 137 | |
| 138 | draw_gradients(canvas, &make_radial_gradient, |
| 139 | radialPts, SK_ARRAY_COUNT(radialPts)); |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | private: |
| 143 | typedef GM INHERITED; |
| 144 | }; |
| 145 | |
| 146 | DEF_GM( return new GradientMatrixGM; ) |
| 147 | } |