blob: 4741cbf0ba1cc698b553726452d30d0ac343ef7f [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"
egdaniel723b0502015-09-15 09:31:40 -070028
29namespace skiagm {
30
mtkleindbfd7ab2016-09-01 11:24:54 -070031constexpr int kColWidth = 180;
32constexpr int kNumCols = 4;
33constexpr int kWidth = kColWidth * kNumCols;
34constexpr int kHeight = 750;
egdaniel723b0502015-09-15 09:31:40 -070035
reed1a9b9642016-03-13 14:13:58 -070036static sk_sp<SkShader> make_shader(const SkRect& bounds) {
egdaniel723b0502015-09-15 09:31:40 -070037 const SkPoint pts[] = {
38 { bounds.left(), bounds.top() },
39 { bounds.right(), bounds.bottom() },
40 };
41 const SkColor colors[] = {
42 SK_ColorRED, SK_ColorGREEN,
43 };
reed1a9b9642016-03-13 14:13:58 -070044 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
Mike Reedfae8fce2019-04-03 10:27:45 -040045 SkTileMode::kRepeat);
egdaniel723b0502015-09-15 09:31:40 -070046}
47
48class LcdBlendGM : public skiagm::GM {
49public:
50 LcdBlendGM() {
51 const int kPointSize = 25;
52 fTextHeight = SkIntToScalar(kPointSize);
53 }
halcanary9d524f22016-03-29 09:03:52 -070054
egdaniel723b0502015-09-15 09:31:40 -070055protected:
56 SkString onShortName() override {
egdaniel27b63352015-09-15 13:13:50 -070057 return SkString("lcdblendmodes");
58 }
59
60 void onOnceBeforeDraw() override {
Mike Kleinea3f0142019-03-20 11:12:10 -050061 fCheckerboard = ToolUtils::create_checkerboard_shader(SK_ColorBLACK, SK_ColorWHITE, 4);
egdaniel723b0502015-09-15 09:31:40 -070062 }
halcanary9d524f22016-03-29 09:03:52 -070063
egdaniel723b0502015-09-15 09:31:40 -070064 SkISize onISize() override { return SkISize::Make(kWidth, kHeight); }
halcanary9d524f22016-03-29 09:03:52 -070065
egdaniel723b0502015-09-15 09:31:40 -070066 void onDraw(SkCanvas* canvas) override {
egdaniel27b63352015-09-15 13:13:50 -070067 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 Kleinea3f0142019-03-20 11:12:10 -050075 auto surface(ToolUtils::makeSurface(canvas, info));
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);
egdaniel27b63352015-09-15 13:13:50 -070088 surface->draw(canvas, 0, 0, &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;
128 for (size_t m = 0; m < SK_ARRAY_COUNT(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]);
Mike Kleinea3f0142019-03-20 11:12:10 -0500132 SkFont font(ToolUtils::create_portable_typeface(), 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;
egdaniel723b0502015-09-15 09:31:40 -0700149 typedef skiagm::GM INHERITED;
150};
151
152//////////////////////////////////////////////////////////////////////////////
153
154DEF_GM( return new LcdBlendGM; )
155}