egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 9 | /* |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 10 | * Tests text rendering with LCD and the various blend modes. |
| 11 | */ |
| 12 | |
| 13 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 14 | #include "sk_tool_utils.h" |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 15 | #include "SkCanvas.h" |
| 16 | #include "SkGradientShader.h" |
egdaniel | 27b6335 | 2015-09-15 13:13:50 -0700 | [diff] [blame] | 17 | #include "SkSurface.h" |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 18 | |
| 19 | namespace skiagm { |
| 20 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 21 | constexpr int kColWidth = 180; |
| 22 | constexpr int kNumCols = 4; |
| 23 | constexpr int kWidth = kColWidth * kNumCols; |
| 24 | constexpr int kHeight = 750; |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 25 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 26 | static sk_sp<SkShader> make_shader(const SkRect& bounds) { |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 27 | const SkPoint pts[] = { |
| 28 | { bounds.left(), bounds.top() }, |
| 29 | { bounds.right(), bounds.bottom() }, |
| 30 | }; |
| 31 | const SkColor colors[] = { |
| 32 | SK_ColorRED, SK_ColorGREEN, |
| 33 | }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 34 | return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
| 35 | SkShader::kRepeat_TileMode); |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | class LcdBlendGM : public skiagm::GM { |
| 39 | public: |
| 40 | LcdBlendGM() { |
| 41 | const int kPointSize = 25; |
| 42 | fTextHeight = SkIntToScalar(kPointSize); |
| 43 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 44 | |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 45 | protected: |
| 46 | SkString onShortName() override { |
egdaniel | 27b6335 | 2015-09-15 13:13:50 -0700 | [diff] [blame] | 47 | return SkString("lcdblendmodes"); |
| 48 | } |
| 49 | |
| 50 | void onOnceBeforeDraw() override { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 51 | fCheckerboard = sk_tool_utils::create_checkerboard_shader(SK_ColorBLACK, SK_ColorWHITE, 4); |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 52 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 53 | |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 54 | SkISize onISize() override { return SkISize::Make(kWidth, kHeight); } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 55 | |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 56 | void onDraw(SkCanvas* canvas) override { |
egdaniel | 27b6335 | 2015-09-15 13:13:50 -0700 | [diff] [blame] | 57 | SkPaint p; |
| 58 | p.setAntiAlias(false); |
| 59 | p.setStyle(SkPaint::kFill_Style); |
| 60 | p.setShader(fCheckerboard); |
| 61 | SkRect r = SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)); |
| 62 | canvas->drawRect(r, p); |
| 63 | |
| 64 | SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 65 | auto surface(canvas->makeSurface(info)); |
egdaniel | 27b6335 | 2015-09-15 13:13:50 -0700 | [diff] [blame] | 66 | if (nullptr == surface) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 67 | surface = SkSurface::MakeRaster(info); |
egdaniel | 27b6335 | 2015-09-15 13:13:50 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | SkCanvas* surfCanvas = surface->getCanvas(); |
| 71 | this->drawColumn(surfCanvas, SK_ColorBLACK, SK_ColorWHITE, false); |
| 72 | surfCanvas->translate(SkIntToScalar(kColWidth), 0); |
| 73 | this->drawColumn(surfCanvas, SK_ColorWHITE, SK_ColorBLACK, false); |
| 74 | surfCanvas->translate(SkIntToScalar(kColWidth), 0); |
| 75 | this->drawColumn(surfCanvas, SK_ColorGREEN, SK_ColorMAGENTA, false); |
| 76 | surfCanvas->translate(SkIntToScalar(kColWidth), 0); |
| 77 | this->drawColumn(surfCanvas, SK_ColorCYAN, SK_ColorMAGENTA, true); |
| 78 | |
| 79 | SkPaint surfPaint; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 80 | surfPaint.setBlendMode(SkBlendMode::kSrcOver); |
egdaniel | 27b6335 | 2015-09-15 13:13:50 -0700 | [diff] [blame] | 81 | surface->draw(canvas, 0, 0, &surfPaint); |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void drawColumn(SkCanvas* canvas, SkColor backgroundColor, SkColor textColor, bool useGrad) { |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 85 | const SkBlendMode gModes[] = { |
| 86 | SkBlendMode::kClear, |
| 87 | SkBlendMode::kSrc, |
| 88 | SkBlendMode::kDst, |
| 89 | SkBlendMode::kSrcOver, |
| 90 | SkBlendMode::kDstOver, |
| 91 | SkBlendMode::kSrcIn, |
| 92 | SkBlendMode::kDstIn, |
| 93 | SkBlendMode::kSrcOut, |
| 94 | SkBlendMode::kDstOut, |
| 95 | SkBlendMode::kSrcATop, |
| 96 | SkBlendMode::kDstATop, |
| 97 | SkBlendMode::kXor, |
| 98 | SkBlendMode::kPlus, |
| 99 | SkBlendMode::kModulate, |
| 100 | SkBlendMode::kScreen, |
| 101 | SkBlendMode::kOverlay, |
| 102 | SkBlendMode::kDarken, |
| 103 | SkBlendMode::kLighten, |
| 104 | SkBlendMode::kColorDodge, |
| 105 | SkBlendMode::kColorBurn, |
| 106 | SkBlendMode::kHardLight, |
| 107 | SkBlendMode::kSoftLight, |
| 108 | SkBlendMode::kDifference, |
| 109 | SkBlendMode::kExclusion, |
| 110 | SkBlendMode::kMultiply, |
| 111 | SkBlendMode::kHue, |
| 112 | SkBlendMode::kSaturation, |
| 113 | SkBlendMode::kColor, |
| 114 | SkBlendMode::kLuminosity, |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 115 | }; |
| 116 | // Draw background rect |
| 117 | SkPaint backgroundPaint; |
| 118 | backgroundPaint.setColor(backgroundColor); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 119 | canvas->drawRect(SkRect::MakeIWH(kColWidth, kHeight), backgroundPaint); |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 120 | SkScalar y = fTextHeight; |
| 121 | for (size_t m = 0; m < SK_ARRAY_COUNT(gModes); m++) { |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 122 | SkPaint paint; |
| 123 | paint.setColor(textColor); |
| 124 | paint.setAntiAlias(true); |
| 125 | paint.setSubpixelText(true); |
| 126 | paint.setLCDRenderText(true); |
| 127 | paint.setTextSize(fTextHeight); |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 128 | paint.setBlendMode(gModes[m]); |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 129 | sk_tool_utils::set_portable_typeface(&paint); |
| 130 | if (useGrad) { |
| 131 | SkRect r; |
| 132 | r.setXYWH(0, y - fTextHeight, SkIntToScalar(kColWidth), fTextHeight); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 133 | paint.setShader(make_shader(r)); |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 134 | } |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 135 | SkString string(SkBlendMode_Name(gModes[m])); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 136 | canvas->drawString(string, 0, y, paint); |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 137 | y+=fTextHeight; |
| 138 | } |
| 139 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 140 | |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 141 | private: |
| 142 | SkScalar fTextHeight; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 143 | sk_sp<SkShader> fCheckerboard; |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 144 | typedef skiagm::GM INHERITED; |
| 145 | }; |
| 146 | |
| 147 | ////////////////////////////////////////////////////////////////////////////// |
| 148 | |
| 149 | DEF_GM( return new LcdBlendGM; ) |
| 150 | } |