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" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkPath.h" |
| 11 | #include "SkGradientShader.h" |
| 12 | #include "SkTypeface.h" |
| 13 | |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 14 | static sk_sp<SkShader> make_heatGradient(const SkPoint pts[2]) { |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 15 | const SkColor bw[] = { SK_ColorBLACK, SK_ColorWHITE }; |
| 16 | |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 17 | return SkGradientShader::MakeLinear(pts, bw, nullptr, SK_ARRAY_COUNT(bw), |
| 18 | SkShader::kClamp_TileMode); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | static bool setFont(SkPaint* paint, const char name[]) { |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame^] | 22 | paint->setTypeface(SkTypeface::MakeFromName(name, SkFontStyle())); |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 23 | return SkToBool(paint->getTypeface()); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 24 | } |
| 25 | |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 26 | /** |
| 27 | Test a set of clipping problems discovered while writing blitAntiRect, |
| 28 | and test all the code paths through the clipping blitters. |
| 29 | Each region should show as a blue center surrounded by a 2px green |
| 30 | border, with no red. |
| 31 | */ |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 32 | |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 33 | #define HEIGHT 480 |
| 34 | |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 35 | class GammaTextGM : public skiagm::GM { |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 36 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 37 | SkString onShortName() override { |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 38 | SkString name("gammatext"); |
| 39 | name.append(sk_tool_utils::major_platform_os_name()); |
| 40 | return name; |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 41 | } |
| 42 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 43 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 44 | return SkISize::Make(1024, HEIGHT); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static void drawGrad(SkCanvas* canvas) { |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 48 | const SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 49 | |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 50 | canvas->clear(SK_ColorRED); |
| 51 | SkPaint paint; |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 52 | paint.setShader(make_heatGradient(pts)); |
vandebo@chromium.org | c39c867 | 2012-04-17 21:46:18 +0000 | [diff] [blame] | 53 | SkRect r = { 0, 0, SkIntToScalar(1024), SkIntToScalar(HEIGHT) }; |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 54 | canvas->drawRect(r, paint); |
| 55 | } |
| 56 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 57 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 58 | drawGrad(canvas); |
| 59 | |
| 60 | const SkColor fg[] = { |
| 61 | 0xFFFFFFFF, |
| 62 | 0xFFFFFF00, 0xFFFF00FF, 0xFF00FFFF, |
| 63 | 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, |
| 64 | 0xFF000000, |
| 65 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 66 | |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 67 | const char* text = "Hamburgefons"; |
| 68 | size_t len = strlen(text); |
| 69 | |
| 70 | SkPaint paint; |
caryclark | 3721355 | 2015-07-24 11:08:01 -0700 | [diff] [blame] | 71 | setFont(&paint, sk_tool_utils::platform_font_name("serif")); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 72 | paint.setTextSize(SkIntToScalar(16)); |
| 73 | paint.setAntiAlias(true); |
| 74 | paint.setLCDRenderText(true); |
| 75 | |
vandebo@chromium.org | c39c867 | 2012-04-17 21:46:18 +0000 | [diff] [blame] | 76 | SkScalar x = SkIntToScalar(10); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 77 | for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { |
| 78 | paint.setColor(fg[i]); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 79 | |
vandebo@chromium.org | c39c867 | 2012-04-17 21:46:18 +0000 | [diff] [blame] | 80 | SkScalar y = SkIntToScalar(40); |
| 81 | SkScalar stopy = SkIntToScalar(HEIGHT); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 82 | while (y < stopy) { |
reed | 04f7e14 | 2014-11-13 12:46:24 -0800 | [diff] [blame] | 83 | canvas->drawText(text, len, x, y, paint); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 84 | y += paint.getTextSize() * 2; |
| 85 | } |
| 86 | x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg); |
| 87 | } |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | private: |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 91 | typedef skiagm::GM INHERITED; |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
bungeman | 67b21a7 | 2015-02-19 07:45:14 -0800 | [diff] [blame] | 94 | DEF_GM( return new GammaTextGM; ) |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 95 | |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 96 | ////////////////////////////////////////////////////////////////////////////// |
| 97 | |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 98 | static sk_sp<SkShader> make_gradient(SkColor c) { |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 99 | const SkPoint pts[] = { { 0, 0 }, { 240, 0 } }; |
| 100 | SkColor colors[2]; |
| 101 | colors[0] = c; |
| 102 | colors[1] = SkColorSetA(c, 0); |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 103 | return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 104 | } |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 105 | |
| 106 | static void set_face(SkPaint* paint) { |
mboc | ee6a991 | 2016-05-31 11:42:36 -0700 | [diff] [blame^] | 107 | paint->setTypeface(SkTypeface::MakeFromName("serif", |
| 108 | SkFontStyle::FromOldStyle(SkTypeface::kItalic))); |
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; ) |