blob: efaa31acf2639ad2d6f7369ebc156e060a993780 [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
reed@google.com83f7c652013-02-04 16:56:15 +000010#include "SkGradientShader.h"
11
reed1a9b9642016-03-13 14:13:58 -070012typedef sk_sp<SkShader> (*MakeShaderProc)(const SkColor[], int count, const SkSize&);
reed@google.com83f7c652013-02-04 16:56:15 +000013
reed1a9b9642016-03-13 14:13:58 -070014static sk_sp<SkShader> shader_linear(const SkColor colors[], int count, const SkSize& size) {
reed@google.com83f7c652013-02-04 16:56:15 +000015 SkPoint pts[] = { { 0, 0 }, { size.width(), size.height() } };
reed1a9b9642016-03-13 14:13:58 -070016 return SkGradientShader::MakeLinear(pts, colors, nullptr, count, SkShader::kClamp_TileMode);
reed@google.com83f7c652013-02-04 16:56:15 +000017}
18
reed1a9b9642016-03-13 14:13:58 -070019static sk_sp<SkShader> shader_radial(const SkColor colors[], int count, const SkSize& size) {
reed@google.com83f7c652013-02-04 16:56:15 +000020 SkPoint center = { size.width()/2, size.height()/2 };
reed1a9b9642016-03-13 14:13:58 -070021 return SkGradientShader::MakeRadial(center, size.width()/2, colors, nullptr, count,
22 SkShader::kClamp_TileMode);
reed@google.com83f7c652013-02-04 16:56:15 +000023}
24
reed1a9b9642016-03-13 14:13:58 -070025static sk_sp<SkShader> shader_conical(const SkColor colors[], int count, const SkSize& size) {
reed@google.com83f7c652013-02-04 16:56:15 +000026 SkPoint center = { size.width()/2, size.height()/2 };
reed1a9b9642016-03-13 14:13:58 -070027 return SkGradientShader::MakeTwoPointConical(center, size.width()/64, center, size.width()/2,
28 colors, nullptr, count, SkShader::kClamp_TileMode);
reed@google.com83f7c652013-02-04 16:56:15 +000029}
30
reed1a9b9642016-03-13 14:13:58 -070031static sk_sp<SkShader> shader_sweep(const SkColor colors[], int count, const SkSize& size) {
32 return SkGradientShader::MakeSweep(size.width()/2, size.height()/2, colors, nullptr, count);
reed@google.com83f7c652013-02-04 16:56:15 +000033}
34
35class ShallowGradientGM : public skiagm::GM {
36public:
fmalita063675b2015-10-12 10:41:48 -070037 ShallowGradientGM(MakeShaderProc proc, const char name[], bool dither)
38 : fProc(proc)
39 , fDither(dither) {
reed@google.com83f7c652013-02-04 16:56:15 +000040 fName.printf("shallow_gradient_%s", name);
41 }
42
43protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000044
mtklein36352bf2015-03-25 18:17:31 -070045 SkString onShortName() override {
reed@google.com83f7c652013-02-04 16:56:15 +000046 return fName;
47 }
48
mtklein36352bf2015-03-25 18:17:31 -070049 SkISize onISize() override {
reed@google.com83f7c652013-02-04 16:56:15 +000050 return SkISize::Make(800, 800);
51 }
52
mtklein36352bf2015-03-25 18:17:31 -070053 void onDraw(SkCanvas* canvas) override {
halcanary9d524f22016-03-29 09:03:52 -070054 const SkColor colors[] = { sk_tool_utils::color_to_565(0xFF555555),
caryclark12596012015-07-29 05:27:47 -070055 sk_tool_utils::color_to_565(0xFF444444) };
reed@google.com83f7c652013-02-04 16:56:15 +000056 const int colorCount = SK_ARRAY_COUNT(colors);
57
58 SkRect r = { 0, 0, this->width(), this->height() };
59 SkSize size = SkSize::Make(r.width(), r.height());
60
61 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -070062 paint.setShader(fProc(colors, colorCount, size));
fmalita063675b2015-10-12 10:41:48 -070063 paint.setDither(fDither);
reed@google.com83f7c652013-02-04 16:56:15 +000064 canvas->drawRect(r, paint);
65 }
66
67private:
68 MakeShaderProc fProc;
69 SkString fName;
fmalita063675b2015-10-12 10:41:48 -070070 bool fDither;
reed@google.com83f7c652013-02-04 16:56:15 +000071
72 typedef skiagm::GM INHERITED;
73};
74
75///////////////////////////////////////////////////////////////////////////////
76
fmalita063675b2015-10-12 10:41:48 -070077DEF_GM( return new ShallowGradientGM(shader_linear, "linear", true); )
78DEF_GM( return new ShallowGradientGM(shader_radial, "radial", true); )
79DEF_GM( return new ShallowGradientGM(shader_conical, "conical", true); )
80DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep", true); )
81
82DEF_GM( return new ShallowGradientGM(shader_linear, "linear_nodither", false); )
83DEF_GM( return new ShallowGradientGM(shader_radial, "radial_nodither", false); )
84DEF_GM( return new ShallowGradientGM(shader_conical, "conical_nodither", false); )
85DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep_nodither", false); )