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; |
| 79 | SkAutoTUnref<SkXfermode> xfermode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); |
| 80 | surfPaint.setXfermode(xfermode); |
| 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) { |
| 85 | const struct { |
| 86 | SkXfermode::Mode fMode; |
| 87 | const char* fLabel; |
| 88 | } gModes[] = { |
| 89 | { SkXfermode::kClear_Mode, "Clear" }, |
| 90 | { SkXfermode::kSrc_Mode, "Src" }, |
| 91 | { SkXfermode::kDst_Mode, "Dst" }, |
| 92 | { SkXfermode::kSrcOver_Mode, "SrcOver" }, |
| 93 | { SkXfermode::kDstOver_Mode, "DstOver" }, |
| 94 | { SkXfermode::kSrcIn_Mode, "SrcIn" }, |
| 95 | { SkXfermode::kDstIn_Mode, "DstIn" }, |
| 96 | { SkXfermode::kSrcOut_Mode, "SrcOut" }, |
| 97 | { SkXfermode::kDstOut_Mode, "DstOut" }, |
| 98 | { SkXfermode::kSrcATop_Mode, "SrcATop" }, |
| 99 | { SkXfermode::kDstATop_Mode, "DstATop" }, |
| 100 | { SkXfermode::kXor_Mode, "Xor" }, |
| 101 | { SkXfermode::kPlus_Mode, "Plus" }, |
| 102 | { SkXfermode::kModulate_Mode, "Modulate" }, |
| 103 | { SkXfermode::kScreen_Mode, "Screen" }, |
| 104 | { SkXfermode::kOverlay_Mode, "Overlay" }, |
| 105 | { SkXfermode::kDarken_Mode, "Darken" }, |
| 106 | { SkXfermode::kLighten_Mode, "Lighten" }, |
| 107 | { SkXfermode::kColorDodge_Mode, "ColorDodge" }, |
| 108 | { SkXfermode::kColorBurn_Mode, "ColorBurn" }, |
| 109 | { SkXfermode::kHardLight_Mode, "HardLight" }, |
| 110 | { SkXfermode::kSoftLight_Mode, "SoftLight" }, |
| 111 | { SkXfermode::kDifference_Mode, "Difference" }, |
| 112 | { SkXfermode::kExclusion_Mode, "Exclusion" }, |
| 113 | { SkXfermode::kMultiply_Mode, "Multiply" }, |
| 114 | { SkXfermode::kHue_Mode, "Hue" }, |
| 115 | { SkXfermode::kSaturation_Mode, "Saturation" }, |
| 116 | { SkXfermode::kColor_Mode, "Color" }, |
| 117 | { SkXfermode::kLuminosity_Mode, "Luminosity" }, |
| 118 | }; |
| 119 | // Draw background rect |
| 120 | SkPaint backgroundPaint; |
| 121 | backgroundPaint.setColor(backgroundColor); |
| 122 | canvas->drawRectCoords(0, 0, SkIntToScalar(kColWidth), SkIntToScalar(kHeight), |
| 123 | backgroundPaint); |
| 124 | SkScalar y = fTextHeight; |
| 125 | for (size_t m = 0; m < SK_ARRAY_COUNT(gModes); m++) { |
| 126 | SkAutoTUnref<SkXfermode> xfermode(SkXfermode::Create(gModes[m].fMode)); |
| 127 | SkPaint paint; |
| 128 | paint.setColor(textColor); |
| 129 | paint.setAntiAlias(true); |
| 130 | paint.setSubpixelText(true); |
| 131 | paint.setLCDRenderText(true); |
| 132 | paint.setTextSize(fTextHeight); |
| 133 | paint.setXfermode(xfermode); |
| 134 | sk_tool_utils::set_portable_typeface(&paint); |
| 135 | if (useGrad) { |
| 136 | SkRect r; |
| 137 | r.setXYWH(0, y - fTextHeight, SkIntToScalar(kColWidth), fTextHeight); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 138 | paint.setShader(make_shader(r)); |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 139 | } |
| 140 | SkString string(gModes[m].fLabel); |
| 141 | canvas->drawText(gModes[m].fLabel, string.size(), 0, y, paint); |
| 142 | y+=fTextHeight; |
| 143 | } |
| 144 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame^] | 145 | |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 146 | private: |
| 147 | SkScalar fTextHeight; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 148 | sk_sp<SkShader> fCheckerboard; |
egdaniel | 723b050 | 2015-09-15 09:31:40 -0700 | [diff] [blame] | 149 | typedef skiagm::GM INHERITED; |
| 150 | }; |
| 151 | |
| 152 | ////////////////////////////////////////////////////////////////////////////// |
| 153 | |
| 154 | DEF_GM( return new LcdBlendGM; ) |
| 155 | } |