reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/effects/SkGradientShader.h" |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 21 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 22 | typedef sk_sp<SkShader> (*MakeShaderProc)(const SkColor[], int count, const SkSize&); |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 23 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 24 | static sk_sp<SkShader> shader_linear(const SkColor colors[], int count, const SkSize& size) { |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 25 | SkPoint pts[] = { { 0, 0 }, { size.width(), size.height() } }; |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 26 | return SkGradientShader::MakeLinear(pts, colors, nullptr, count, SkTileMode::kClamp); |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 27 | } |
| 28 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 29 | static sk_sp<SkShader> shader_radial(const SkColor colors[], int count, const SkSize& size) { |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 30 | SkPoint center = { size.width()/2, size.height()/2 }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 31 | return SkGradientShader::MakeRadial(center, size.width()/2, colors, nullptr, count, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 32 | SkTileMode::kClamp); |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 33 | } |
| 34 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 35 | static sk_sp<SkShader> shader_conical(const SkColor colors[], int count, const SkSize& size) { |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 36 | SkPoint center = { size.width()/2, size.height()/2 }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 37 | return SkGradientShader::MakeTwoPointConical(center, size.width()/64, center, size.width()/2, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 38 | colors, nullptr, count, SkTileMode::kClamp); |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 39 | } |
| 40 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 41 | static 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.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | class ShallowGradientGM : public skiagm::GM { |
| 46 | public: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 47 | ShallowGradientGM(MakeShaderProc proc, const char name[], bool dither) |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 48 | : fProc(proc), fName(name), fDither(dither) {} |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 49 | |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 50 | private: |
| 51 | MakeShaderProc fProc; |
| 52 | const char* fName; |
| 53 | bool fDither; |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 54 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 55 | SkString onShortName() override { |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 56 | return SkStringPrintf("shallow_gradient_%s%s", fName, fDither ? "" : "_nodither"); |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 59 | SkISize onISize() override { return {800, 800}; } |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 60 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 61 | void onDraw(SkCanvas* canvas) override { |
Mike Klein | cbd83bb | 2018-08-16 09:32:46 -0400 | [diff] [blame] | 62 | const SkColor colors[] = { 0xFF555555, 0xFF444444 }; |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 63 | 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; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 69 | paint.setShader(fProc(colors, colorCount, size)); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 70 | paint.setDither(fDither); |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 71 | canvas->drawRect(r, paint); |
| 72 | } |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /////////////////////////////////////////////////////////////////////////////// |
| 76 | |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 77 | #define M(PROC, DITHER) DEF_GM( return new ShallowGradientGM(shader_ ## PROC, #PROC, DITHER); ) |
| 78 | M(linear, true) |
| 79 | M(radial, true) |
| 80 | M(conical, true) |
| 81 | M(sweep, true) |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 82 | |
Hal Canary | 594fe85 | 2019-07-18 13:35:49 -0400 | [diff] [blame] | 83 | M(linear, false) |
| 84 | M(radial, false) |
| 85 | M(conical, false) |
| 86 | M(sweep, false) |
| 87 | #undef M |