blob: fc959f36debbbbb7086c17f2c5aa8bf08e9b8715 [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)
Hal Canary594fe852019-07-18 13:35:49 -040048 : fProc(proc), fName(name), fDither(dither) {}
reed@google.com83f7c652013-02-04 16:56:15 +000049
Hal Canary594fe852019-07-18 13:35:49 -040050private:
51 MakeShaderProc fProc;
52 const char* fName;
53 bool fDither;
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000054
mtklein36352bf2015-03-25 18:17:31 -070055 SkString onShortName() override {
Hal Canary594fe852019-07-18 13:35:49 -040056 return SkStringPrintf("shallow_gradient_%s%s", fName, fDither ? "" : "_nodither");
reed@google.com83f7c652013-02-04 16:56:15 +000057 }
58
Hal Canary594fe852019-07-18 13:35:49 -040059 SkISize onISize() override { return {800, 800}; }
reed@google.com83f7c652013-02-04 16:56:15 +000060
mtklein36352bf2015-03-25 18:17:31 -070061 void onDraw(SkCanvas* canvas) override {
Mike Kleincbd83bb2018-08-16 09:32:46 -040062 const SkColor colors[] = { 0xFF555555, 0xFF444444 };
reed@google.com83f7c652013-02-04 16:56:15 +000063 const int colorCount = SK_ARRAY_COUNT(colors);
64
65 SkRect r = { 0, 0, this->width(), this->height() };
66 SkSize size = SkSize::Make(r.width(), r.height());
67
68 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -070069 paint.setShader(fProc(colors, colorCount, size));
fmalita063675b2015-10-12 10:41:48 -070070 paint.setDither(fDither);
reed@google.com83f7c652013-02-04 16:56:15 +000071 canvas->drawRect(r, paint);
72 }
reed@google.com83f7c652013-02-04 16:56:15 +000073};
74
75///////////////////////////////////////////////////////////////////////////////
76
Hal Canary594fe852019-07-18 13:35:49 -040077#define M(PROC, DITHER) DEF_GM( return new ShallowGradientGM(shader_ ## PROC, #PROC, DITHER); )
78M(linear, true)
79M(radial, true)
80M(conical, true)
81M(sweep, true)
fmalita063675b2015-10-12 10:41:48 -070082
Hal Canary594fe852019-07-18 13:35:49 -040083M(linear, false)
84M(radial, false)
85M(conical, false)
86M(sweep, false)
87#undef M