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