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