blob: 0a4f03621a2df7a4a21cc2deab1d8d609628a9e9 [file] [log] [blame]
commit-bot@chromium.orgace22692013-06-12 21:33:02 +00001/*
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"
Mike Klein33d20552017-03-22 13:47:51 -040021#include "sk_tool_utils.h"
commit-bot@chromium.orgace22692013-06-12 21:33:02 +000022
mtkleindbfd7ab2016-09-01 11:24:54 -070023constexpr SkColor gColors[] = {
commit-bot@chromium.orgace22692013-06-12 21:33:02 +000024 SK_ColorRED, SK_ColorYELLOW
25};
26
commit-bot@chromium.org45672092013-06-12 23:36:50 +000027// These annoying defines are necessary, because the only other alternative
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +000028// is to use SkIntToScalar(...) everywhere.
mtkleindbfd7ab2016-09-01 11:24:54 -070029constexpr SkScalar sZero = 0;
30constexpr SkScalar sHalf = SK_ScalarHalf;
31constexpr SkScalar sOne = SK_Scalar1;
commit-bot@chromium.orgace22692013-06-12 21:33:02 +000032
commit-bot@chromium.org45672092013-06-12 23:36:50 +000033// These arrays define the gradient stop points
34// as x1, y1, x2, y2 per gradient to draw.
mtkleindbfd7ab2016-09-01 11:24:54 -070035constexpr SkPoint linearPts[][2] = {
commit-bot@chromium.org45672092013-06-12 23:36:50 +000036 {{sZero, sZero}, {sOne, sZero}},
37 {{sZero, sZero}, {sZero, sOne}},
38 {{sOne, sZero}, {sZero, sZero}},
39 {{sZero, sOne}, {sZero, sZero}},
40
41 {{sZero, sZero}, {sOne, sOne}},
42 {{sOne, sOne}, {sZero, sZero}},
43 {{sOne, sZero}, {sZero, sOne}},
44 {{sZero, sOne}, {sOne, sZero}}
commit-bot@chromium.orgace22692013-06-12 21:33:02 +000045};
46
mtkleindbfd7ab2016-09-01 11:24:54 -070047constexpr SkPoint radialPts[][2] = {
commit-bot@chromium.org45672092013-06-12 23:36:50 +000048 {{sZero, sHalf}, {sOne, sHalf}},
49 {{sHalf, sZero}, {sHalf, sOne}},
50 {{sOne, sHalf}, {sZero, sHalf}},
51 {{sHalf, sOne}, {sHalf, sZero}},
commit-bot@chromium.org1ac1cf62013-06-12 21:47:39 +000052
commit-bot@chromium.org45672092013-06-12 23:36:50 +000053 {{sZero, sZero}, {sOne, sOne}},
54 {{sOne, sOne}, {sZero, sZero}},
55 {{sOne, sZero}, {sZero, sOne}},
56 {{sZero, sOne}, {sOne, sZero}}
commit-bot@chromium.org1ac1cf62013-06-12 21:47:39 +000057};
58
commit-bot@chromium.org45672092013-06-12 23:36:50 +000059// These define the pixels allocated to each gradient image.
mtkleindbfd7ab2016-09-01 11:24:54 -070060constexpr SkScalar TESTGRID_X = SkIntToScalar(200);
61constexpr SkScalar TESTGRID_Y = SkIntToScalar(200);
commit-bot@chromium.orgace22692013-06-12 21:33:02 +000062
mtkleindbfd7ab2016-09-01 11:24:54 -070063constexpr int IMAGES_X = 4; // number of images per row
commit-bot@chromium.orgace22692013-06-12 21:33:02 +000064
reed1a9b9642016-03-13 14:13:58 -070065static sk_sp<SkShader> make_linear_gradient(const SkPoint pts[2], const SkMatrix& localMatrix) {
66 return SkGradientShader::MakeLinear(pts, gColors, nullptr, SK_ARRAY_COUNT(gColors),
67 SkShader::kClamp_TileMode, 0, &localMatrix);
commit-bot@chromium.orgace22692013-06-12 21:33:02 +000068}
69
reed1a9b9642016-03-13 14:13:58 -070070static sk_sp<SkShader> make_radial_gradient(const SkPoint pts[2], const SkMatrix& localMatrix) {
commit-bot@chromium.org1ac1cf62013-06-12 21:47:39 +000071 SkPoint center;
72 center.set(SkScalarAve(pts[0].fX, pts[1].fX),
73 SkScalarAve(pts[0].fY, pts[1].fY));
74 float radius = (center - pts[0]).length();
reed1a9b9642016-03-13 14:13:58 -070075 return SkGradientShader::MakeRadial(center, radius, gColors, nullptr, SK_ARRAY_COUNT(gColors),
76 SkShader::kClamp_TileMode, 0, &localMatrix);
commit-bot@chromium.org1ac1cf62013-06-12 21:47:39 +000077}
78
commit-bot@chromium.org9c9005a2014-04-28 14:55:39 +000079static void draw_gradients(SkCanvas* canvas,
reed1a9b9642016-03-13 14:13:58 -070080 sk_sp<SkShader> (*makeShader)(const SkPoint[2], const SkMatrix&),
commit-bot@chromium.org1ac1cf62013-06-12 21:47:39 +000081 const SkPoint ptsArray[][2], int numImages) {
commit-bot@chromium.orgace22692013-06-12 21:33:02 +000082 // Use some nice prime numbers for the rectangle and matrix with
83 // different scaling along the x and y axes (which is the bug this
84 // test addresses, where incorrect order of operations mixed up the axes)
commit-bot@chromium.org45672092013-06-12 23:36:50 +000085 SkRect rectGrad = {
86 SkIntToScalar(43), SkIntToScalar(61),
87 SkIntToScalar(181), SkIntToScalar(167) };
commit-bot@chromium.orgace22692013-06-12 21:33:02 +000088 SkMatrix shaderMat;
89 shaderMat.setScale(rectGrad.width(), rectGrad.height());
90 shaderMat.postTranslate(rectGrad.left(), rectGrad.top());
91
92 canvas->save();
93 for (int i = 0; i < numImages; i++) {
94 // Advance line downwards if necessary.
95 if (i % IMAGES_X == 0 && i != 0) {
96 canvas->restore();
97 canvas->translate(0, TESTGRID_Y);
98 canvas->save();
99 }
100
commit-bot@chromium.orgace22692013-06-12 21:33:02 +0000101 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -0700102 paint.setShader(makeShader(*ptsArray, shaderMat));
commit-bot@chromium.orgace22692013-06-12 21:33:02 +0000103 canvas->drawRect(rectGrad, paint);
104
105 // Advance to next position.
106 canvas->translate(TESTGRID_X, 0);
107 ptsArray++;
108 }
109 canvas->restore();
110}
111
halcanary2a243382015-09-09 08:16:41 -0700112DEF_SIMPLE_GM_BG(gradient_matrix, canvas, 800, 800,
113 sk_tool_utils::color_to_565(0xFFDDDDDD)) {
commit-bot@chromium.orgace22692013-06-12 21:33:02 +0000114 draw_gradients(canvas, &make_linear_gradient,
115 linearPts, SK_ARRAY_COUNT(linearPts));
commit-bot@chromium.org1ac1cf62013-06-12 21:47:39 +0000116
117 canvas->translate(0, TESTGRID_Y);
118
119 draw_gradients(canvas, &make_radial_gradient,
120 radialPts, SK_ARRAY_COUNT(radialPts));
commit-bot@chromium.orgace22692013-06-12 21:33:02 +0000121}