blob: 764b1a50342c710da213046e2cd0c66ea5b1cf15 [file] [log] [blame]
reed@google.com83f7c652013-02-04 16:56:15 +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 "gm.h"
9#include "SkGradientShader.h"
10
11typedef SkShader* (*MakeShaderProc)(const SkColor[], int count, const SkSize&);
12
13static SkShader* shader_linear(const SkColor colors[], int count, const SkSize& size) {
14 SkPoint pts[] = { { 0, 0 }, { size.width(), size.height() } };
halcanary96fcdcc2015-08-27 07:41:13 -070015 return SkGradientShader::CreateLinear(pts, colors, nullptr, count,
reed@google.com83f7c652013-02-04 16:56:15 +000016 SkShader::kClamp_TileMode);
17}
18
19static SkShader* shader_radial(const SkColor colors[], int count, const SkSize& size) {
20 SkPoint center = { size.width()/2, size.height()/2 };
halcanary96fcdcc2015-08-27 07:41:13 -070021 return SkGradientShader::CreateRadial(center, size.width()/2, colors, nullptr, count,
reed@google.com83f7c652013-02-04 16:56:15 +000022 SkShader::kClamp_TileMode);
23}
24
25static SkShader* shader_conical(const SkColor colors[], int count, const SkSize& size) {
26 SkPoint center = { size.width()/2, size.height()/2 };
27 return SkGradientShader::CreateTwoPointConical(center, size.width()/64,
28 center, size.width()/2,
halcanary96fcdcc2015-08-27 07:41:13 -070029 colors, nullptr, count,
reed@google.com83f7c652013-02-04 16:56:15 +000030 SkShader::kClamp_TileMode);
31}
32
33static SkShader* shader_sweep(const SkColor colors[], int count, const SkSize& size) {
34 return SkGradientShader::CreateSweep(size.width()/2, size.height()/2,
halcanary96fcdcc2015-08-27 07:41:13 -070035 colors, nullptr, count);
reed@google.com83f7c652013-02-04 16:56:15 +000036}
37
38class ShallowGradientGM : public skiagm::GM {
39public:
fmalita063675b2015-10-12 10:41:48 -070040 ShallowGradientGM(MakeShaderProc proc, const char name[], bool dither)
41 : fProc(proc)
42 , fDither(dither) {
reed@google.com83f7c652013-02-04 16:56:15 +000043 fName.printf("shallow_gradient_%s", name);
44 }
45
46protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000047
mtklein36352bf2015-03-25 18:17:31 -070048 SkString onShortName() override {
reed@google.com83f7c652013-02-04 16:56:15 +000049 return fName;
50 }
51
mtklein36352bf2015-03-25 18:17:31 -070052 SkISize onISize() override {
reed@google.com83f7c652013-02-04 16:56:15 +000053 return SkISize::Make(800, 800);
54 }
55
mtklein36352bf2015-03-25 18:17:31 -070056 void onDraw(SkCanvas* canvas) override {
caryclark12596012015-07-29 05:27:47 -070057 const SkColor colors[] = { sk_tool_utils::color_to_565(0xFF555555),
58 sk_tool_utils::color_to_565(0xFF444444) };
reed@google.com83f7c652013-02-04 16:56:15 +000059 const int colorCount = SK_ARRAY_COUNT(colors);
60
61 SkRect r = { 0, 0, this->width(), this->height() };
62 SkSize size = SkSize::Make(r.width(), r.height());
63
64 SkPaint paint;
robertphillips@google.comb62ce702013-03-27 21:31:21 +000065 paint.setShader(fProc(colors, colorCount, size))->unref();
fmalita063675b2015-10-12 10:41:48 -070066 paint.setDither(fDither);
reed@google.com83f7c652013-02-04 16:56:15 +000067 canvas->drawRect(r, paint);
68 }
69
70private:
71 MakeShaderProc fProc;
72 SkString fName;
fmalita063675b2015-10-12 10:41:48 -070073 bool fDither;
reed@google.com83f7c652013-02-04 16:56:15 +000074
75 typedef skiagm::GM INHERITED;
76};
77
78///////////////////////////////////////////////////////////////////////////////
79
fmalita063675b2015-10-12 10:41:48 -070080DEF_GM( return new ShallowGradientGM(shader_linear, "linear", true); )
81DEF_GM( return new ShallowGradientGM(shader_radial, "radial", true); )
82DEF_GM( return new ShallowGradientGM(shader_conical, "conical", true); )
83DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep", true); )
84
85DEF_GM( return new ShallowGradientGM(shader_linear, "linear_nodither", false); )
86DEF_GM( return new ShallowGradientGM(shader_radial, "radial_nodither", false); )
87DEF_GM( return new ShallowGradientGM(shader_conical, "conical_nodither", false); )
88DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep_nodither", false); )