blob: 43bee6a832623ff83d56c5a1ae532c349e890112 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
11#include "include/core/SkPaint.h"
12#include "include/core/SkPoint.h"
13#include "include/core/SkRect.h"
14#include "include/core/SkRefCnt.h"
15#include "include/core/SkShader.h"
16#include "include/core/SkSize.h"
17#include "include/core/SkString.h"
18#include "include/core/SkTileMode.h"
19#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "include/effects/SkGradientShader.h"
reed@google.com83f7c652013-02-04 16:56:15 +000021
reed1a9b9642016-03-13 14:13:58 -070022typedef sk_sp<SkShader> (*MakeShaderProc)(const SkColor[], int count, const SkSize&);
reed@google.com83f7c652013-02-04 16:56:15 +000023
reed1a9b9642016-03-13 14:13:58 -070024static sk_sp<SkShader> shader_linear(const SkColor colors[], int count, const SkSize& size) {
reed@google.com83f7c652013-02-04 16:56:15 +000025 SkPoint pts[] = { { 0, 0 }, { size.width(), size.height() } };
Mike Reedfae8fce2019-04-03 10:27:45 -040026 return SkGradientShader::MakeLinear(pts, colors, nullptr, count, SkTileMode::kClamp);
reed@google.com83f7c652013-02-04 16:56:15 +000027}
28
reed1a9b9642016-03-13 14:13:58 -070029static sk_sp<SkShader> shader_radial(const SkColor colors[], int count, const SkSize& size) {
reed@google.com83f7c652013-02-04 16:56:15 +000030 SkPoint center = { size.width()/2, size.height()/2 };
reed1a9b9642016-03-13 14:13:58 -070031 return SkGradientShader::MakeRadial(center, size.width()/2, colors, nullptr, count,
Mike Reedfae8fce2019-04-03 10:27:45 -040032 SkTileMode::kClamp);
reed@google.com83f7c652013-02-04 16:56:15 +000033}
34
reed1a9b9642016-03-13 14:13:58 -070035static sk_sp<SkShader> shader_conical(const SkColor colors[], int count, const SkSize& size) {
reed@google.com83f7c652013-02-04 16:56:15 +000036 SkPoint center = { size.width()/2, size.height()/2 };
reed1a9b9642016-03-13 14:13:58 -070037 return SkGradientShader::MakeTwoPointConical(center, size.width()/64, center, size.width()/2,
Mike Reedfae8fce2019-04-03 10:27:45 -040038 colors, nullptr, count, SkTileMode::kClamp);
reed@google.com83f7c652013-02-04 16:56:15 +000039}
40
reed1a9b9642016-03-13 14:13:58 -070041static sk_sp<SkShader> shader_sweep(const SkColor colors[], int count, const SkSize& size) {
42 return SkGradientShader::MakeSweep(size.width()/2, size.height()/2, colors, nullptr, count);
reed@google.com83f7c652013-02-04 16:56:15 +000043}
44
45class ShallowGradientGM : public skiagm::GM {
46public:
fmalita063675b2015-10-12 10:41:48 -070047 ShallowGradientGM(MakeShaderProc proc, const char name[], bool dither)
48 : fProc(proc)
49 , fDither(dither) {
reed@google.com83f7c652013-02-04 16:56:15 +000050 fName.printf("shallow_gradient_%s", name);
51 }
52
53protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000054
mtklein36352bf2015-03-25 18:17:31 -070055 SkString onShortName() override {
reed@google.com83f7c652013-02-04 16:56:15 +000056 return fName;
57 }
58
mtklein36352bf2015-03-25 18:17:31 -070059 SkISize onISize() override {
reed@google.com83f7c652013-02-04 16:56:15 +000060 return SkISize::Make(800, 800);
61 }
62
mtklein36352bf2015-03-25 18:17:31 -070063 void onDraw(SkCanvas* canvas) override {
Mike Kleincbd83bb2018-08-16 09:32:46 -040064 const SkColor colors[] = { 0xFF555555, 0xFF444444 };
reed@google.com83f7c652013-02-04 16:56:15 +000065 const int colorCount = SK_ARRAY_COUNT(colors);
66
67 SkRect r = { 0, 0, this->width(), this->height() };
68 SkSize size = SkSize::Make(r.width(), r.height());
69
70 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -070071 paint.setShader(fProc(colors, colorCount, size));
fmalita063675b2015-10-12 10:41:48 -070072 paint.setDither(fDither);
reed@google.com83f7c652013-02-04 16:56:15 +000073 canvas->drawRect(r, paint);
74 }
75
76private:
77 MakeShaderProc fProc;
78 SkString fName;
fmalita063675b2015-10-12 10:41:48 -070079 bool fDither;
reed@google.com83f7c652013-02-04 16:56:15 +000080
81 typedef skiagm::GM INHERITED;
82};
83
84///////////////////////////////////////////////////////////////////////////////
85
fmalita063675b2015-10-12 10:41:48 -070086DEF_GM( return new ShallowGradientGM(shader_linear, "linear", true); )
87DEF_GM( return new ShallowGradientGM(shader_radial, "radial", true); )
88DEF_GM( return new ShallowGradientGM(shader_conical, "conical", true); )
89DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep", true); )
90
91DEF_GM( return new ShallowGradientGM(shader_linear, "linear_nodither", false); )
92DEF_GM( return new ShallowGradientGM(shader_radial, "radial_nodither", false); )
93DEF_GM( return new ShallowGradientGM(shader_conical, "conical_nodither", false); )
94DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep_nodither", false); )