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 | |
| 17 | return SkGradientShader::CreateLinear(pts, bw, NULL, |
| 18 | SK_ARRAY_COUNT(bw), |
| 19 | SkShader::kClamp_TileMode); |
| 20 | } |
| 21 | |
| 22 | static bool setFont(SkPaint* paint, const char name[]) { |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [diff] [blame] | 23 | SkTypeface* tf = sk_tool_utils::create_portable_typeface(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: |
reed | 04f7e14 | 2014-11-13 12:46:24 -0800 | [diff] [blame] | 42 | SkString onShortName() SK_OVERRIDE { |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 43 | return SkString("gammatext"); |
| 44 | } |
| 45 | |
reed | 04f7e14 | 2014-11-13 12:46:24 -0800 | [diff] [blame] | 46 | SkISize onISize() SK_OVERRIDE { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 47 | return SkISize::Make(1024, HEIGHT); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static void drawGrad(SkCanvas* canvas) { |
vandebo@chromium.org | c39c867 | 2012-04-17 21:46:18 +0000 | [diff] [blame] | 51 | SkPoint pts[] = { { 0, 0 }, { 0, SkIntToScalar(HEIGHT) } }; |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 52 | SkShader* s = make_heatGradient(pts); |
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; |
| 56 | paint.setShader(s)->unref(); |
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 | |
reed | 04f7e14 | 2014-11-13 12:46:24 -0800 | [diff] [blame] | 61 | void onDraw(SkCanvas* canvas) SK_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"; |
| 72 | size_t len = strlen(text); |
| 73 | |
| 74 | SkPaint paint; |
| 75 | setFont(&paint, "Times"); |
| 76 | paint.setTextSize(SkIntToScalar(16)); |
| 77 | paint.setAntiAlias(true); |
| 78 | paint.setLCDRenderText(true); |
| 79 | |
vandebo@chromium.org | c39c867 | 2012-04-17 21:46:18 +0000 | [diff] [blame] | 80 | SkScalar x = SkIntToScalar(10); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 81 | for (size_t i = 0; i < SK_ARRAY_COUNT(fg); ++i) { |
| 82 | paint.setColor(fg[i]); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 83 | |
vandebo@chromium.org | c39c867 | 2012-04-17 21:46:18 +0000 | [diff] [blame] | 84 | SkScalar y = SkIntToScalar(40); |
| 85 | SkScalar stopy = SkIntToScalar(HEIGHT); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 86 | while (y < stopy) { |
reed | 04f7e14 | 2014-11-13 12:46:24 -0800 | [diff] [blame] | 87 | canvas->drawText(text, len, x, y, paint); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 88 | y += paint.getTextSize() * 2; |
| 89 | } |
| 90 | x += SkIntToScalar(1024) / SK_ARRAY_COUNT(fg); |
| 91 | } |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | private: |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 95 | typedef skiagm::GM INHERITED; |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
reed | 64c0488 | 2014-11-14 14:39:29 -0800 | [diff] [blame] | 98 | // TEMP disable to see if nothing else flakes <reed> |
| 99 | //DEF_GM( return new GammaTextGM; ) |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 100 | |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 101 | ////////////////////////////////////////////////////////////////////////////// |
| 102 | |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 103 | static SkShader* make_gradient(SkColor c) { |
| 104 | const SkPoint pts[] = { { 0, 0 }, { 240, 0 } }; |
| 105 | SkColor colors[2]; |
| 106 | colors[0] = c; |
| 107 | colors[1] = SkColorSetA(c, 0); |
| 108 | return SkGradientShader::CreateLinear(pts, colors, NULL, 2, SkShader::kClamp_TileMode); |
reed@google.com | ed3ee64 | 2012-02-14 16:12:49 +0000 | [diff] [blame] | 109 | } |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 110 | |
| 111 | static void set_face(SkPaint* paint) { |
| 112 | SkTypeface* face = SkTypeface::CreateFromName("serif", SkTypeface::kItalic); |
| 113 | SkSafeUnref(paint->setTypeface(face)); |
| 114 | } |
| 115 | |
| 116 | static void draw_pair(SkCanvas* canvas, SkPaint* paint, SkShader* shader) { |
| 117 | const char text[] = "Now is the time for all good"; |
| 118 | const size_t len = strlen(text); |
| 119 | |
| 120 | paint->setShader(NULL); |
| 121 | canvas->drawText(text, len, 10, 20, *paint); |
| 122 | paint->setShader(SkShader::CreateColorShader(paint->getColor()))->unref(); |
| 123 | canvas->drawText(text, len, 10, 40, *paint); |
| 124 | paint->setShader(shader); |
| 125 | canvas->drawText(text, len, 10, 60, *paint); |
| 126 | } |
| 127 | |
| 128 | class GammaShaderTextGM : public skiagm::GM { |
| 129 | SkShader* fShaders[3]; |
| 130 | SkColor fColors[3]; |
| 131 | |
| 132 | public: |
| 133 | GammaShaderTextGM() { |
| 134 | const SkColor colors[] = { SK_ColorBLACK, SK_ColorRED, SK_ColorBLUE }; |
| 135 | for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { |
| 136 | fShaders[i] = NULL; |
| 137 | fColors[i] = colors[i]; |
| 138 | } |
| 139 | } |
| 140 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 141 | ~GammaShaderTextGM() SK_OVERRIDE { |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 142 | for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { |
| 143 | SkSafeUnref(fShaders[i]); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | protected: |
reed | 04f7e14 | 2014-11-13 12:46:24 -0800 | [diff] [blame] | 148 | SkString onShortName() SK_OVERRIDE { |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 149 | return SkString("gammagradienttext"); |
| 150 | } |
| 151 | |
reed | 04f7e14 | 2014-11-13 12:46:24 -0800 | [diff] [blame] | 152 | SkISize onISize() SK_OVERRIDE { |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 153 | return SkISize::Make(300, 300); |
| 154 | } |
| 155 | |
reed | 04f7e14 | 2014-11-13 12:46:24 -0800 | [diff] [blame] | 156 | void onOnceBeforeDraw() SK_OVERRIDE { |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 157 | for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { |
| 158 | fShaders[i] = make_gradient(fColors[i]); |
| 159 | } |
| 160 | } |
| 161 | |
reed | 04f7e14 | 2014-11-13 12:46:24 -0800 | [diff] [blame] | 162 | void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
reed | 8367b8c | 2014-08-22 08:30:20 -0700 | [diff] [blame] | 163 | SkPaint paint; |
| 164 | paint.setAntiAlias(true); |
| 165 | paint.setLCDRenderText(true); |
| 166 | paint.setTextSize(18); |
| 167 | set_face(&paint); |
| 168 | |
| 169 | for (size_t i = 0; i < SK_ARRAY_COUNT(fShaders); ++i) { |
| 170 | paint.setColor(fColors[i]); |
| 171 | draw_pair(canvas, &paint, fShaders[i]); |
| 172 | canvas->translate(0, 80); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | private: |
| 177 | typedef skiagm::GM INHERITED; |
| 178 | }; |
| 179 | |
| 180 | DEF_GM( return new GammaShaderTextGM; ) |
| 181 | |