blob: 6b16be9f0132c57641d844f7b5f7e016d1f2847a [file] [log] [blame]
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +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#include "gm.h"
8#include "SkGradientShader.h"
9
10using namespace skiagm;
11
12struct GradData {
13 int fCount;
14 const SkColor* fColors;
15 const SkScalar* fPos;
16};
17
18static const SkColor gColors[] = {
19 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
20 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
21 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
22 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
23 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
24 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
25 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
26 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK,
27};
28
29//static const SkScalar gPos[] = { SK_Scalar1*999/2000, SK_Scalar1*1001/2000 };
30
31static const GradData gGradData[] = {
halcanary96fcdcc2015-08-27 07:41:13 -070032 { 40, gColors, nullptr },
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000033 // { 2, gColors, gPos },
halcanary96fcdcc2015-08-27 07:41:13 -070034 // { 2, gCol2, nullptr },
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000035};
36
reed1a9b9642016-03-13 14:13:58 -070037static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
38 return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm);
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000039}
40
reed1a9b9642016-03-13 14:13:58 -070041static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) {
42 const SkPoint pt{ SkScalarAve(pts[0].fX, pts[1].fX), SkScalarAve(pts[0].fY, pts[1].fY) };
43 return SkGradientShader::MakeRadial(pt, pt.fX, data.fColors, data.fPos, data.fCount, tm);
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000044}
45
reed1a9b9642016-03-13 14:13:58 -070046static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode) {
47 const SkPoint pt{ SkScalarAve(pts[0].fX, pts[1].fX), SkScalarAve(pts[0].fY, pts[1].fY) };
48 return SkGradientShader::MakeSweep(pt.fX, pt.fY, data.fColors, data.fPos, data.fCount);
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000049}
50
51
reed1a9b9642016-03-13 14:13:58 -070052typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData&, SkShader::TileMode);
commit-bot@chromium.org83f23d82014-05-22 12:27:41 +000053
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000054static const GradMaker gGradMakers[] = {
skia.committer@gmail.com9e1ec1a2013-07-10 07:00:58 +000055 MakeLinear, MakeRadial, MakeSweep,
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000056};
57
58///////////////////////////////////////////////////////////////////////////////
59
60class GradientsGM : public GM {
61public:
62 GradientsGM() {
caryclark65cdba62015-06-15 06:51:08 -070063 this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000064 }
65
66protected:
mtklein36352bf2015-03-25 18:17:31 -070067 SkString onShortName() override { return SkString("gradient_dirty_laundry"); }
68 SkISize onISize() override { return SkISize::Make(640, 615); }
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000069
mtklein36352bf2015-03-25 18:17:31 -070070 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000071 SkPoint pts[2] = { { 0, 0 },
72 { SkIntToScalar(100), SkIntToScalar(100) }
73 };
74 SkShader::TileMode tm = SkShader::kClamp_TileMode;
75 SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) };
76 SkPaint paint;
77 paint.setAntiAlias(true);
78
79 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
80 for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) {
81 canvas->save();
82 for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) {
reed1a9b9642016-03-13 14:13:58 -070083 paint.setShader(gGradMakers[j](pts, gGradData[i], tm));
commit-bot@chromium.orgb6865392013-07-09 19:43:29 +000084 canvas->drawRect(r, paint);
85 canvas->translate(0, SkIntToScalar(120));
86 }
87 canvas->restore();
88 canvas->translate(SkIntToScalar(120), 0);
89 }
90 }
91
92private:
93 typedef GM INHERITED;
94};
95
96///////////////////////////////////////////////////////////////////////////////
97
98static GM* MyFactory(void*) { return new GradientsGM; }
99static GMRegistry reg(MyFactory);