blob: 0bfe7c3907c3f70223c796ce133a2dc3c52d8308 [file] [log] [blame]
egdaniel723b0502015-09-15 09:31:40 -07001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkBlendMode.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040011#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 Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040023#include "include/core/SkTileMode.h"
24#include "include/core/SkTypeface.h"
25#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "include/effects/SkGradientShader.h"
27#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040028#include "tools/fonts/FontToolUtils.h"
egdaniel723b0502015-09-15 09:31:40 -070029
30namespace skiagm {
31
mtkleindbfd7ab2016-09-01 11:24:54 -070032constexpr int kColWidth = 180;
33constexpr int kNumCols = 4;
34constexpr int kWidth = kColWidth * kNumCols;
35constexpr int kHeight = 750;
egdaniel723b0502015-09-15 09:31:40 -070036
reed1a9b9642016-03-13 14:13:58 -070037static sk_sp<SkShader> make_shader(const SkRect& bounds) {
egdaniel723b0502015-09-15 09:31:40 -070038 const SkPoint pts[] = {
39 { bounds.left(), bounds.top() },
40 { bounds.right(), bounds.bottom() },
41 };
42 const SkColor colors[] = {
43 SK_ColorRED, SK_ColorGREEN,
44 };
Herb Derbyc37b3862022-06-21 09:49:17 -040045 return SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040046 SkTileMode::kRepeat);
egdaniel723b0502015-09-15 09:31:40 -070047}
48
49class LcdBlendGM : public skiagm::GM {
50public:
51 LcdBlendGM() {
52 const int kPointSize = 25;
53 fTextHeight = SkIntToScalar(kPointSize);
54 }
halcanary9d524f22016-03-29 09:03:52 -070055
egdaniel723b0502015-09-15 09:31:40 -070056protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000057 SkString getName() const override { return SkString("lcdblendmodes"); }
egdaniel27b63352015-09-15 13:13:50 -070058
59 void onOnceBeforeDraw() override {
Mike Kleinea3f0142019-03-20 11:12:10 -050060 fCheckerboard = ToolUtils::create_checkerboard_shader(SK_ColorBLACK, SK_ColorWHITE, 4);
egdaniel723b0502015-09-15 09:31:40 -070061 }
halcanary9d524f22016-03-29 09:03:52 -070062
Leandro Lovisolo8f023882023-08-15 21:13:52 +000063 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
halcanary9d524f22016-03-29 09:03:52 -070064
egdaniel723b0502015-09-15 09:31:40 -070065 void onDraw(SkCanvas* canvas) override {
egdaniel27b63352015-09-15 13:13:50 -070066 SkPaint p;
67 p.setAntiAlias(false);
68 p.setStyle(SkPaint::kFill_Style);
69 p.setShader(fCheckerboard);
70 SkRect r = SkRect::MakeWH(SkIntToScalar(kWidth), SkIntToScalar(kHeight));
71 canvas->drawRect(r, p);
72
73 SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
Ben Wagnerae4bb982020-09-24 14:49:00 -040074 SkSurfaceProps props = SkSurfaceProps(0, kRGB_H_SkPixelGeometry);
75 auto surface(ToolUtils::makeSurface(canvas, info, &props));
egdaniel27b63352015-09-15 13:13:50 -070076
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;
reed374772b2016-10-05 17:33:02 -070087 surfPaint.setBlendMode(SkBlendMode::kSrcOver);
Mike Reedb746b1f2021-01-06 08:43:51 -050088 surface->draw(canvas, 0, 0, SkSamplingOptions(), &surfPaint);
egdaniel723b0502015-09-15 09:31:40 -070089 }
90
91 void drawColumn(SkCanvas* canvas, SkColor backgroundColor, SkColor textColor, bool useGrad) {
Brian Osmand1e67e72017-03-15 12:19:37 -040092 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,
egdaniel723b0502015-09-15 09:31:40 -0700122 };
123 // Draw background rect
124 SkPaint backgroundPaint;
125 backgroundPaint.setColor(backgroundColor);
Mike Reed3661bc92017-02-22 13:21:42 -0500126 canvas->drawRect(SkRect::MakeIWH(kColWidth, kHeight), backgroundPaint);
egdaniel723b0502015-09-15 09:31:40 -0700127 SkScalar y = fTextHeight;
Herb Derbyc37b3862022-06-21 09:49:17 -0400128 for (size_t m = 0; m < std::size(gModes); m++) {
egdaniel723b0502015-09-15 09:31:40 -0700129 SkPaint paint;
130 paint.setColor(textColor);
Brian Osmand1e67e72017-03-15 12:19:37 -0400131 paint.setBlendMode(gModes[m]);
Kevin Lubicke836c3a2023-10-20 06:55:35 -0400132 SkFont font(ToolUtils::DefaultPortableTypeface(), fTextHeight);
Mike Reed1af9b482019-01-07 11:01:57 -0500133 font.setSubpixel(true);
134 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
egdaniel723b0502015-09-15 09:31:40 -0700135 if (useGrad) {
136 SkRect r;
137 r.setXYWH(0, y - fTextHeight, SkIntToScalar(kColWidth), fTextHeight);
reed1a9b9642016-03-13 14:13:58 -0700138 paint.setShader(make_shader(r));
egdaniel723b0502015-09-15 09:31:40 -0700139 }
Brian Osmand1e67e72017-03-15 12:19:37 -0400140 SkString string(SkBlendMode_Name(gModes[m]));
Mike Reed1af9b482019-01-07 11:01:57 -0500141 canvas->drawString(string, 0, y, font, paint);
egdaniel723b0502015-09-15 09:31:40 -0700142 y+=fTextHeight;
143 }
144 }
halcanary9d524f22016-03-29 09:03:52 -0700145
egdaniel723b0502015-09-15 09:31:40 -0700146private:
147 SkScalar fTextHeight;
reed8a21c9f2016-03-08 18:50:00 -0800148 sk_sp<SkShader> fCheckerboard;
John Stiles7571f9e2020-09-02 22:42:33 -0400149 using INHERITED = skiagm::GM;
egdaniel723b0502015-09-15 09:31:40 -0700150};
151
152//////////////////////////////////////////////////////////////////////////////
153
154DEF_GM( return new LcdBlendGM; )
John Stilesa6841be2020-08-06 14:11:56 -0400155} // namespace skiagm