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" |
| 14 | #include "SkCanvas.h" |
| 15 | #include "SkGradientShader.h" |
egdaniel | 27b6335 | 2015-09-15 13:13:50 -0700 | [diff] [blame] | 16 | #include "SkSurface.h" |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 17 | |
| 18 | namespace skiagm { |
| 19 | |
| 20 | static const int kColWidth = 180; |
| 21 | static const int kNumCols = 4; |
| 22 | static const int kWidth = kColWidth * kNumCols; |
| 23 | static const int kHeight = 750; |
| 24 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 25 | static sk_sp<SkShader> make_shader(const SkRect& bounds) { |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 26 | const SkPoint pts[] = { |
| 27 | { bounds.left(), bounds.top() }, |
| 28 | { bounds.right(), bounds.bottom() }, |
| 29 | }; |
| 30 | const SkColor colors[] = { |
| 31 | SK_ColorRED, SK_ColorGREEN, |
| 32 | }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 33 | return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
| 34 | SkShader::kRepeat_TileMode); |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | class LcdBlendGM : public skiagm::GM { |
| 38 | public: |
| 39 | LcdBlendGM() { |
| 40 | const int kPointSize = 25; |
| 41 | fTextHeight = SkIntToScalar(kPointSize); |
| 42 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 43 | |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 44 | protected: |
| 45 | SkString onShortName() override { |
egdaniel | 27b6335 | 2015-09-15 13:13:50 -0700 | [diff] [blame] | 46 | return SkString("lcdblendmodes"); |
| 47 | } |
| 48 | |
| 49 | void onOnceBeforeDraw() override { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 50 | fCheckerboard = sk_tool_utils::create_checkerboard_shader(SK_ColorBLACK, SK_ColorWHITE, 4); |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 51 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 52 | |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 53 | SkISize onISize() override { return SkISize::Make(kWidth, kHeight); } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 54 | |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 55 | void onDraw(SkCanvas* canvas) override { |
egdaniel | 27b6335 | 2015-09-15 13:13:50 -0700 | [diff] [blame] | 56 | SkPaint p; |
| 57 | p.setAntiAlias(false); |
| 58 | p.setStyle(SkPaint::kFill_Style); |
| 59 | p.setShader(fCheckerboard); |
| 60 | SkRect r = SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight)); |
| 61 | canvas->drawRect(r, p); |
| 62 | |
| 63 | SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 64 | auto surface(canvas->makeSurface(info)); |
egdaniel | 27b6335 | 2015-09-15 13:13:50 -0700 | [diff] [blame] | 65 | if (nullptr == surface) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 66 | surface = SkSurface::MakeRaster(info); |
egdaniel | 27b6335 | 2015-09-15 13:13:50 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | SkCanvas* surfCanvas = surface->getCanvas(); |
| 70 | this->drawColumn(surfCanvas, SK_ColorBLACK, SK_ColorWHITE, false); |
| 71 | surfCanvas->translate(SkIntToScalar(kColWidth), 0); |
| 72 | this->drawColumn(surfCanvas, SK_ColorWHITE, SK_ColorBLACK, false); |
| 73 | surfCanvas->translate(SkIntToScalar(kColWidth), 0); |
| 74 | this->drawColumn(surfCanvas, SK_ColorGREEN, SK_ColorMAGENTA, false); |
| 75 | surfCanvas->translate(SkIntToScalar(kColWidth), 0); |
| 76 | this->drawColumn(surfCanvas, SK_ColorCYAN, SK_ColorMAGENTA, true); |
| 77 | |
| 78 | SkPaint surfPaint; |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 79 | surfPaint.setXfermode(SkXfermode::Make(SkXfermode::kSrcOver_Mode)); |
egdaniel | 27b6335 | 2015-09-15 13:13:50 -0700 | [diff] [blame] | 80 | surface->draw(canvas, 0, 0, &surfPaint); |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void drawColumn(SkCanvas* canvas, SkColor backgroundColor, SkColor textColor, bool useGrad) { |
| 84 | const struct { |
| 85 | SkXfermode::Mode fMode; |
| 86 | const char* fLabel; |
| 87 | } gModes[] = { |
| 88 | { SkXfermode::kClear_Mode, "Clear" }, |
| 89 | { SkXfermode::kSrc_Mode, "Src" }, |
| 90 | { SkXfermode::kDst_Mode, "Dst" }, |
| 91 | { SkXfermode::kSrcOver_Mode, "SrcOver" }, |
| 92 | { SkXfermode::kDstOver_Mode, "DstOver" }, |
| 93 | { SkXfermode::kSrcIn_Mode, "SrcIn" }, |
| 94 | { SkXfermode::kDstIn_Mode, "DstIn" }, |
| 95 | { SkXfermode::kSrcOut_Mode, "SrcOut" }, |
| 96 | { SkXfermode::kDstOut_Mode, "DstOut" }, |
| 97 | { SkXfermode::kSrcATop_Mode, "SrcATop" }, |
| 98 | { SkXfermode::kDstATop_Mode, "DstATop" }, |
| 99 | { SkXfermode::kXor_Mode, "Xor" }, |
| 100 | { SkXfermode::kPlus_Mode, "Plus" }, |
| 101 | { SkXfermode::kModulate_Mode, "Modulate" }, |
| 102 | { SkXfermode::kScreen_Mode, "Screen" }, |
| 103 | { SkXfermode::kOverlay_Mode, "Overlay" }, |
| 104 | { SkXfermode::kDarken_Mode, "Darken" }, |
| 105 | { SkXfermode::kLighten_Mode, "Lighten" }, |
| 106 | { SkXfermode::kColorDodge_Mode, "ColorDodge" }, |
| 107 | { SkXfermode::kColorBurn_Mode, "ColorBurn" }, |
| 108 | { SkXfermode::kHardLight_Mode, "HardLight" }, |
| 109 | { SkXfermode::kSoftLight_Mode, "SoftLight" }, |
| 110 | { SkXfermode::kDifference_Mode, "Difference" }, |
| 111 | { SkXfermode::kExclusion_Mode, "Exclusion" }, |
| 112 | { SkXfermode::kMultiply_Mode, "Multiply" }, |
| 113 | { SkXfermode::kHue_Mode, "Hue" }, |
| 114 | { SkXfermode::kSaturation_Mode, "Saturation" }, |
| 115 | { SkXfermode::kColor_Mode, "Color" }, |
| 116 | { SkXfermode::kLuminosity_Mode, "Luminosity" }, |
| 117 | }; |
| 118 | // Draw background rect |
| 119 | SkPaint backgroundPaint; |
| 120 | backgroundPaint.setColor(backgroundColor); |
| 121 | canvas->drawRectCoords(0, 0, SkIntToScalar(kColWidth), SkIntToScalar(kHeight), |
| 122 | backgroundPaint); |
| 123 | SkScalar y = fTextHeight; |
| 124 | for (size_t m = 0; m < SK_ARRAY_COUNT(gModes); m++) { |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 125 | SkPaint paint; |
| 126 | paint.setColor(textColor); |
| 127 | paint.setAntiAlias(true); |
| 128 | paint.setSubpixelText(true); |
| 129 | paint.setLCDRenderText(true); |
| 130 | paint.setTextSize(fTextHeight); |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 131 | paint.setXfermode(SkXfermode::Make(gModes[m].fMode)); |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 132 | sk_tool_utils::set_portable_typeface(&paint); |
| 133 | if (useGrad) { |
| 134 | SkRect r; |
| 135 | r.setXYWH(0, y - fTextHeight, SkIntToScalar(kColWidth), fTextHeight); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 136 | paint.setShader(make_shader(r)); |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 137 | } |
| 138 | SkString string(gModes[m].fLabel); |
| 139 | canvas->drawText(gModes[m].fLabel, string.size(), 0, y, paint); |
| 140 | y+=fTextHeight; |
| 141 | } |
| 142 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 143 | |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 144 | private: |
| 145 | SkScalar fTextHeight; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 146 | sk_sp<SkShader> fCheckerboard; |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 147 | typedef skiagm::GM INHERITED; |
| 148 | }; |
| 149 | |
| 150 | ////////////////////////////////////////////////////////////////////////////// |
| 151 | |
| 152 | DEF_GM( return new LcdBlendGM; ) |
| 153 | } |