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