Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
lcdblendmodes.cpp
Go to the documentation of this file.
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#include "gm/gm.h"
12#include "include/core/SkFont.h"
16#include "include/core/SkRect.h"
20#include "include/core/SkSize.h"
27#include "tools/ToolUtils.h"
29
30namespace skiagm {
31
32constexpr int kColWidth = 180;
33constexpr int kNumCols = 4;
34constexpr int kWidth = kColWidth * kNumCols;
35constexpr int kHeight = 750;
36
37static sk_sp<SkShader> make_shader(const SkRect& bounds) {
38 const SkPoint pts[] = {
39 { bounds.left(), bounds.top() },
40 { bounds.right(), bounds.bottom() },
41 };
42 const SkColor colors[] = {
44 };
45 return SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors),
47}
48
49class LcdBlendGM : public skiagm::GM {
50public:
52 const int kPointSize = 25;
53 fTextHeight = SkIntToScalar(kPointSize);
54 }
55
56protected:
57 SkString getName() const override { return SkString("lcdblendmodes"); }
58
62
63 SkISize getISize() override { return SkISize::Make(kWidth, kHeight); }
64
65 void onDraw(SkCanvas* canvas) override {
66 SkPaint p;
67 p.setAntiAlias(false);
68 p.setStyle(SkPaint::kFill_Style);
69 p.setShader(fCheckerboard);
71 canvas->drawRect(r, p);
72
75 auto surface(ToolUtils::makeSurface(canvas, info, &props));
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;
88 surface->draw(canvas, 0, 0, SkSamplingOptions(), &surfPaint);
89 }
90
91 void drawColumn(SkCanvas* canvas, SkColor backgroundColor, SkColor textColor, bool useGrad) {
92 const SkBlendMode gModes[] = {
122 };
123 // Draw background rect
124 SkPaint backgroundPaint;
125 backgroundPaint.setColor(backgroundColor);
126 canvas->drawRect(SkRect::MakeIWH(kColWidth, kHeight), backgroundPaint);
127 SkScalar y = fTextHeight;
128 for (size_t m = 0; m < std::size(gModes); m++) {
130 paint.setColor(textColor);
131 paint.setBlendMode(gModes[m]);
132 SkFont font(ToolUtils::DefaultPortableTypeface(), fTextHeight);
133 font.setSubpixel(true);
135 if (useGrad) {
136 SkRect r;
137 r.setXYWH(0, y - fTextHeight, SkIntToScalar(kColWidth), fTextHeight);
138 paint.setShader(make_shader(r));
139 }
140 SkString string(SkBlendMode_Name(gModes[m]));
141 canvas->drawString(string, 0, y, font, paint);
142 y+=fTextHeight;
143 }
144 }
145
146private:
147 SkScalar fTextHeight;
148 sk_sp<SkShader> fCheckerboard;
149 using INHERITED = skiagm::GM;
150};
151
152//////////////////////////////////////////////////////////////////////////////
153
154DEF_GM( return new LcdBlendGM; )
155} // namespace skiagm
static sk_sp< SkShader > make_shader()
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition DM.cpp:213
SK_API const char * SkBlendMode_Name(SkBlendMode blendMode)
SkBlendMode
Definition SkBlendMode.h:38
@ kSrcOut
r = s * (1-da)
@ kExclusion
rc = s + d - two(s*d), ra = kSrcOver
@ kSaturation
saturation of source with hue and luminosity of destination
@ kColorBurn
darken destination to reflect source
@ kPlus
r = min(s + d, 1)
@ kLighten
rc = s + d - min(s*da, d*sa), ra = kSrcOver
@ kHue
hue of source with saturation and luminosity of destination
@ kDstIn
r = d * sa
@ kModulate
r = s*d
@ kMultiply
r = s*(1-da) + d*(1-sa) + s*d
@ kColorDodge
brighten destination to reflect source
@ kScreen
r = s + d - s*d
@ kSrcOver
r = s + (1-sa)*d
@ kXor
r = s*(1-da) + d*(1-sa)
@ kLuminosity
luminosity of source with hue and saturation of destination
@ kSoftLight
lighten or darken, depending on source
@ kDifference
rc = s + d - 2*(min(s*da, d*sa)), ra = kSrcOver
@ kOverlay
multiply or screen, depending on destination
@ kSrcATop
r = s*da + d*(1-sa)
@ kDstATop
r = d*sa + s*(1-da)
@ kDstOver
r = d + (1-da)*s
@ kColor
hue and saturation of source with luminosity of destination
@ kHardLight
multiply or screen, depending on source
@ kDstOut
r = d * (1-sa)
@ kDarken
rc = s + d - max(s*da, d*sa), ra = kSrcOver
@ kSrcIn
r = s * da
@ kClear
r = 0
constexpr SkColor SK_ColorMAGENTA
Definition SkColor.h:147
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorCYAN
Definition SkColor.h:143
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
constexpr SkColor SK_ColorGREEN
Definition SkColor.h:131
constexpr SkColor SK_ColorWHITE
Definition SkColor.h:122
#define SkIntToScalar(x)
Definition SkScalar.h:57
@ kRGB_H_SkPixelGeometry
constexpr SkBlendMode gModes[]
void drawRect(const SkRect &rect, const SkPaint &paint)
void translate(SkScalar dx, SkScalar dy)
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition SkCanvas.h:1803
@ kSubpixelAntiAlias
glyph positioned in pixel using transparency
static sk_sp< SkShader > MakeLinear(const SkPoint pts[2], const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
void setColor(SkColor color)
Definition SkPaint.cpp:119
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
void setBlendMode(SkBlendMode mode)
Definition SkPaint.cpp:151
SkString getName() const override
void onDraw(SkCanvas *canvas) override
void drawColumn(SkCanvas *canvas, SkColor backgroundColor, SkColor textColor, bool useGrad)
void onOnceBeforeDraw() override
SkISize getISize() override
const Paint & paint
VkSurfaceKHR surface
Definition main.cc:49
float SkScalar
Definition extension.cpp:12
#define DEF_GM(CODE)
Definition gm.h:40
double y
sk_sp< SkTypeface > DefaultPortableTypeface()
sk_sp< SkSurface > makeSurface(SkCanvas *canvas, const SkImageInfo &info, const SkSurfaceProps *props)
sk_sp< SkShader > create_checkerboard_shader(SkColor c1, SkColor c2, int size)
constexpr int kPointSize
constexpr int kWidth
constexpr int kColWidth
constexpr int kHeight
constexpr int kNumCols
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static SkImageInfo MakeN32Premul(int width, int height)
void setXYWH(float x, float y, float width, float height)
Definition SkRect.h:931
static SkRect MakeIWH(int w, int h)
Definition SkRect.h:623
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609