blob: ad1de63b536c656eb563a9059db02ba47278cf3d [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 {
halcanary9d524f22016-03-29 09:03:52 -070053 const SkColor colors[] = { sk_tool_utils::color_to_565(0xFF555555),
caryclark12596012015-07-29 05:27:47 -070054 sk_tool_utils::color_to_565(0xFF444444) };
reed@google.com83f7c652013-02-04 16:56:15 +000055 const int colorCount = SK_ARRAY_COUNT(colors);
56
57 SkRect r = { 0, 0, this->width(), this->height() };
58 SkSize size = SkSize::Make(r.width(), r.height());
59
60 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -070061 paint.setShader(fProc(colors, colorCount, size));
fmalita063675b2015-10-12 10:41:48 -070062 paint.setDither(fDither);
reed@google.com83f7c652013-02-04 16:56:15 +000063 canvas->drawRect(r, paint);
64 }
65
66private:
67 MakeShaderProc fProc;
68 SkString fName;
fmalita063675b2015-10-12 10:41:48 -070069 bool fDither;
reed@google.com83f7c652013-02-04 16:56:15 +000070
71 typedef skiagm::GM INHERITED;
72};
73
74///////////////////////////////////////////////////////////////////////////////
75
fmalita063675b2015-10-12 10:41:48 -070076DEF_GM( return new ShallowGradientGM(shader_linear, "linear", true); )
77DEF_GM( return new ShallowGradientGM(shader_radial, "radial", true); )
78DEF_GM( return new ShallowGradientGM(shader_conical, "conical", true); )
79DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep", true); )
80
81DEF_GM( return new ShallowGradientGM(shader_linear, "linear_nodither", false); )
82DEF_GM( return new ShallowGradientGM(shader_radial, "radial_nodither", false); )
83DEF_GM( return new ShallowGradientGM(shader_conical, "conical_nodither", false); )
84DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep_nodither", false); )