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