blob: d2336ab57f1be6bdc51c8247045e1acbb8281c92 [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
reed1a9b9642016-03-13 14:13:58 -070011typedef sk_sp<SkShader> (*MakeShaderProc)(const SkColor[], int count, const SkSize&);
reed@google.com83f7c652013-02-04 16:56:15 +000012
reed1a9b9642016-03-13 14:13:58 -070013static sk_sp<SkShader> shader_linear(const SkColor colors[], int count, const SkSize& size) {
reed@google.com83f7c652013-02-04 16:56:15 +000014 SkPoint pts[] = { { 0, 0 }, { size.width(), size.height() } };
reed1a9b9642016-03-13 14:13:58 -070015 return SkGradientShader::MakeLinear(pts, colors, nullptr, count, SkShader::kClamp_TileMode);
reed@google.com83f7c652013-02-04 16:56:15 +000016}
17
reed1a9b9642016-03-13 14:13:58 -070018static sk_sp<SkShader> shader_radial(const SkColor colors[], int count, const SkSize& size) {
reed@google.com83f7c652013-02-04 16:56:15 +000019 SkPoint center = { size.width()/2, size.height()/2 };
reed1a9b9642016-03-13 14:13:58 -070020 return SkGradientShader::MakeRadial(center, size.width()/2, colors, nullptr, count,
21 SkShader::kClamp_TileMode);
reed@google.com83f7c652013-02-04 16:56:15 +000022}
23
reed1a9b9642016-03-13 14:13:58 -070024static sk_sp<SkShader> shader_conical(const SkColor colors[], int count, const SkSize& size) {
reed@google.com83f7c652013-02-04 16:56:15 +000025 SkPoint center = { size.width()/2, size.height()/2 };
reed1a9b9642016-03-13 14:13:58 -070026 return SkGradientShader::MakeTwoPointConical(center, size.width()/64, center, size.width()/2,
27 colors, nullptr, count, SkShader::kClamp_TileMode);
reed@google.com83f7c652013-02-04 16:56:15 +000028}
29
reed1a9b9642016-03-13 14:13:58 -070030static sk_sp<SkShader> shader_sweep(const SkColor colors[], int count, const SkSize& size) {
31 return SkGradientShader::MakeSweep(size.width()/2, size.height()/2, colors, nullptr, count);
reed@google.com83f7c652013-02-04 16:56:15 +000032}
33
34class ShallowGradientGM : public skiagm::GM {
35public:
fmalita063675b2015-10-12 10:41:48 -070036 ShallowGradientGM(MakeShaderProc proc, const char name[], bool dither)
37 : fProc(proc)
38 , fDither(dither) {
reed@google.com83f7c652013-02-04 16:56:15 +000039 fName.printf("shallow_gradient_%s", name);
40 }
41
42protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000043
mtklein36352bf2015-03-25 18:17:31 -070044 SkString onShortName() override {
reed@google.com83f7c652013-02-04 16:56:15 +000045 return fName;
46 }
47
mtklein36352bf2015-03-25 18:17:31 -070048 SkISize onISize() override {
reed@google.com83f7c652013-02-04 16:56:15 +000049 return SkISize::Make(800, 800);
50 }
51
mtklein36352bf2015-03-25 18:17:31 -070052 void onDraw(SkCanvas* canvas) override {
Mike Kleincbd83bb2018-08-16 09:32:46 -040053 const SkColor colors[] = { 0xFF555555, 0xFF444444 };
reed@google.com83f7c652013-02-04 16:56:15 +000054 const int colorCount = SK_ARRAY_COUNT(colors);
55
56 SkRect r = { 0, 0, this->width(), this->height() };
57 SkSize size = SkSize::Make(r.width(), r.height());
58
59 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -070060 paint.setShader(fProc(colors, colorCount, size));
fmalita063675b2015-10-12 10:41:48 -070061 paint.setDither(fDither);
reed@google.com83f7c652013-02-04 16:56:15 +000062 canvas->drawRect(r, paint);
63 }
64
65private:
66 MakeShaderProc fProc;
67 SkString fName;
fmalita063675b2015-10-12 10:41:48 -070068 bool fDither;
reed@google.com83f7c652013-02-04 16:56:15 +000069
70 typedef skiagm::GM INHERITED;
71};
72
73///////////////////////////////////////////////////////////////////////////////
74
fmalita063675b2015-10-12 10:41:48 -070075DEF_GM( return new ShallowGradientGM(shader_linear, "linear", true); )
76DEF_GM( return new ShallowGradientGM(shader_radial, "radial", true); )
77DEF_GM( return new ShallowGradientGM(shader_conical, "conical", true); )
78DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep", true); )
79
80DEF_GM( return new ShallowGradientGM(shader_linear, "linear_nodither", false); )
81DEF_GM( return new ShallowGradientGM(shader_radial, "radial_nodither", false); )
82DEF_GM( return new ShallowGradientGM(shader_conical, "conical_nodither", false); )
83DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep_nodither", false); )