reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkFont.h" |
| 12 | #include "include/core/SkFontStyle.h" |
| 13 | #include "include/core/SkPaint.h" |
| 14 | #include "include/core/SkPoint.h" |
| 15 | #include "include/core/SkRect.h" |
| 16 | #include "include/core/SkRefCnt.h" |
| 17 | #include "include/core/SkScalar.h" |
| 18 | #include "include/core/SkShader.h" |
| 19 | #include "include/core/SkSize.h" |
| 20 | #include "include/core/SkString.h" |
| 21 | #include "include/core/SkTileMode.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "include/core/SkTypeface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 23 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "include/effects/SkGradientShader.h" |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 25 | |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 26 | static sk_sp<SkShader> make_heatGradient(const SkPoint pts[2]) { |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 27 | const SkColor bw[] = { SK_ColorBLACK, SK_ColorWHITE }; |
| 28 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 29 | return SkGradientShader::MakeLinear(pts, bw, nullptr, SK_ARRAY_COUNT(bw), SkTileMode::kClamp); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 30 | } |
| 31 | |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 32 | /** |
| 33 | Test a set of clipping problems discovered while writing blitAntiRect, |
| 34 | and test all the code paths through the clipping blitters. |
| 35 | Each region should show as a blue center surrounded by a 2px green |
| 36 | border, with no red. |
| 37 | */ |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 38 | |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 39 | #define HEIGHT 480 |
| 40 | |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 41 | class GammaTextGM : public skiagm::GM { |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 42 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 43 | SkString onShortName() override { |
Mike Klein | bea1f94 | 2019-03-08 11:11:55 -0600 | [diff] [blame] | 44 | return SkString("gammatext"); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 45 | } |
| 46 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 47 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 48 | return SkISize::Make(1024, HEIGHT); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static void drawGrad(SkCanvas* canvas) { |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 52 | const SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 53 | |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 54 | canvas->clear(SK_ColorRED); |
| 55 | SkPaint paint; |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 56 | paint.setShader(make_heatGradient(pts)); |
vandebo@chromium.org | c39c867 | 2012-04-17 21:46:18 +0000 | [diff] [blame] | 57 | SkRect r = { 0, 0, SkIntToScalar(1024), SkIntToScalar(HEIGHT) }; |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 58 | canvas->drawRect(r, paint); |
| 59 | } |
| 60 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 61 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 62 | drawGrad(canvas); |
| 63 | |
| 64 | const SkColor fg[] = { |
| 65 | 0xFFFFFFFF, |
| 66 | 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF, |
| 67 | 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, |
| 68 | 0xFF000000, |
| 69 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 70 | |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 71 | const char* text = "Hamburgefons"; |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 72 | |
| 73 | SkPaint paint; |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 74 | SkFont font(nullptr, 16); |
| 75 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 76 | |
vandebo@chromium.org | c39c867 | 2012-04-17 21:46:18 +0000 | [diff] [blame] | 77 | SkScalar x = SkIntToScalar(10); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 78 | for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { |
| 79 | paint.setColor(fg[i]); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 80 | |
vandebo@chromium.org | c39c867 | 2012-04-17 21:46:18 +0000 | [diff] [blame] | 81 | SkScalar y = SkIntToScalar(40); |
| 82 | SkScalar stopy = SkIntToScalar(HEIGHT); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 83 | while (y < stopy) { |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 84 | canvas->drawString(text, x, y, font, paint); |
| 85 | y += font.getSize() * 2; |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 86 | } |
| 87 | x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg); |
| 88 | } |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | private: |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 92 | typedef skiagm::GM INHERITED; |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
bungeman | 67b21a7 | 2015-02-19 07:45:14 -0800 | [diff] [blame] | 95 | DEF_GM( return new GammaTextGM; ) |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 96 | |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 97 | ////////////////////////////////////////////////////////////////////////////// |
| 98 | |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 99 | static sk_sp<SkShader> make_gradient(SkColor c) { |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 100 | const SkPoint pts[] = { { 0, 0 }, { 240, 0 } }; |
| 101 | SkColor colors[2]; |
| 102 | colors[0] = c; |
| 103 | colors[1] = SkColorSetA(c, 0); |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 104 | return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkTileMode::kClamp); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 105 | } |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 106 | |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 107 | static void draw_pair(SkCanvas* canvas, const SkFont& font, SkColor color, |
| 108 | const sk_sp<SkShader>& shader) { |
| 109 | static const char text[] = "Now is the time for all good"; |
| 110 | SkPaint paint; |
| 111 | paint.setColor(color); |
| 112 | canvas->drawString(text, 10, 20, font, paint); |
Mike Reed | c8bea7d | 2019-04-09 13:55:36 -0400 | [diff] [blame] | 113 | paint.setShader(SkShaders::Color(paint.getColor())); |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 114 | canvas->drawString(text, 10, 40, font, paint); |
| 115 | paint.setShader(shader); |
| 116 | canvas->drawString(text, 10, 60, font, paint); |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | class GammaShaderTextGM : public skiagm::GM { |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 120 | sk_sp<SkShader> fShaders[3]; |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 121 | SkColor fColors[3]; |
| 122 | |
| 123 | public: |
| 124 | GammaShaderTextGM() { |
| 125 | const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE }; |
| 126 | for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 127 | fColors[i] = colors[i]; |
| 128 | } |
| 129 | } |
| 130 | |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 131 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 132 | SkString onShortName() override { |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 133 | return SkString("gammagradienttext"); |
| 134 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 135 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 136 | SkISize onISize() override { |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 137 | return SkISize::Make(300, 300); |
| 138 | } |
| 139 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 140 | void onOnceBeforeDraw() override { |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 141 | for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { |
| 142 | fShaders[i] = make_gradient(fColors[i]); |
| 143 | } |
| 144 | } |
| 145 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 146 | void onDraw(SkCanvas* canvas) override { |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 147 | SkPaint paint; |
Hal Canary | 5880791 | 2019-01-08 14:07:50 -0500 | [diff] [blame] | 148 | paint.setAntiAlias(true); |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 149 | SkFont font(SkTypeface::MakeFromName("serif", SkFontStyle::Italic()), 18); |
| 150 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 151 | |
| 152 | for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { |
Hal Canary | 6ac0df8 | 2019-01-07 16:01:22 -0500 | [diff] [blame] | 153 | draw_pair(canvas, font, fColors[i], fShaders[i]); |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 154 | canvas->translate(0, 80); |
| 155 | } |
| 156 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 157 | |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 158 | private: |
| 159 | typedef skiagm::GM INHERITED; |
| 160 | }; |
| 161 | |
| 162 | DEF_GM( return new GammaShaderTextGM; ) |