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 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 8 | #include "gm/gm.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkMatrix.h" |
| 12 | #include "include/core/SkPaint.h" |
| 13 | #include "include/core/SkPoint.h" |
| 14 | #include "include/core/SkRect.h" |
| 15 | #include "include/core/SkRefCnt.h" |
| 16 | #include "include/core/SkScalar.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 17 | #include "include/core/SkShader.h" |
| 18 | #include "include/core/SkTileMode.h" |
| 19 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/effects/SkGradientShader.h" |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 21 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 22 | constexpr SkColor gColors[] = { |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 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. |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 28 | constexpr SkScalar sZero = 0; |
| 29 | constexpr SkScalar sHalf = SK_ScalarHalf; |
| 30 | constexpr 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. |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 34 | constexpr SkPoint linearPts[][2] = { |
commit-bot@chromium.org | 4567209 | 2013-06-12 23:36:50 +0000 | [diff] [blame] | 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 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 46 | constexpr 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. |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 59 | constexpr SkScalar TESTGRID_X = SkIntToScalar(200); |
| 60 | constexpr SkScalar TESTGRID_Y = SkIntToScalar(200); |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 61 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 62 | constexpr int IMAGES_X = 4; // number of images per row |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 63 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 64 | static sk_sp<SkShader> make_linear_gradient(const SkPoint pts[2], const SkMatrix& localMatrix) { |
| 65 | return SkGradientShader::MakeLinear(pts, gColors, nullptr, SK_ARRAY_COUNT(gColors), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 66 | SkTileMode::kClamp, 0, &localMatrix); |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 67 | } |
| 68 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 69 | static sk_sp<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(); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 74 | return SkGradientShader::MakeRadial(center, radius, gColors, nullptr, SK_ARRAY_COUNT(gColors), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 75 | SkTileMode::kClamp, 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, |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 79 | sk_sp<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 | |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 100 | SkPaint paint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 101 | paint.setShader(makeShader(*ptsArray, shaderMat)); |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 102 | canvas->drawRect(rectGrad, paint); |
| 103 | |
| 104 | // Advance to next position. |
| 105 | canvas->translate(TESTGRID_X, 0); |
| 106 | ptsArray++; |
| 107 | } |
| 108 | canvas->restore(); |
| 109 | } |
| 110 | |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 111 | DEF_SIMPLE_GM_BG(gradient_matrix, canvas, 800, 800, 0xFFDDDDDD) { |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 112 | draw_gradients(canvas, &make_linear_gradient, |
| 113 | linearPts, SK_ARRAY_COUNT(linearPts)); |
commit-bot@chromium.org | 1ac1cf6 | 2013-06-12 21:47:39 +0000 | [diff] [blame] | 114 | |
| 115 | canvas->translate(0, TESTGRID_Y); |
| 116 | |
| 117 | draw_gradients(canvas, &make_radial_gradient, |
| 118 | radialPts, SK_ARRAY_COUNT(radialPts)); |
commit-bot@chromium.org | ace2269 | 2013-06-12 21:33:02 +0000 | [diff] [blame] | 119 | } |