Flutter Engine
The Flutter Engine
lumafilter.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2013 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"
14#include "include/core/SkFont.h"
18#include "include/core/SkRect.h"
22#include "include/core/SkSize.h"
31#include "tools/DecodeUtils.h"
32#include "tools/Resources.h"
33#include "tools/ToolUtils.h"
35
36#include <string.h>
37
38static SkScalar kSize = 80;
39static SkScalar kInset = 10;
40static SkColor kColor1 = SkColorSetARGB(0xff, 0xff, 0xff, 0);
41static SkColor kColor2 = SkColorSetARGB(0xff, 0x82, 0xff, 0);
42
43static void draw_label(SkCanvas* canvas, const char* label,
44 const SkPoint& offset) {
47
48 size_t len = strlen(label);
49
50 SkScalar width = font.measureText(label, len, SkTextEncoding::kUTF8);
51 canvas->drawSimpleText(label, len, SkTextEncoding::kUTF8, offset.x() - width / 2, offset.y(),
52 font, SkPaint());
53}
54
55static void draw_scene(SkCanvas* canvas, const sk_sp<SkColorFilter>& filter, SkBlendMode mode,
56 const sk_sp<SkShader>& s1, const sk_sp<SkShader>& s2) {
58 paint.setAntiAlias(true);
60
61 c = bounds;
62 c.fRight = bounds.centerX();
63 paint.setARGB(0x20, 0, 0, 0xff);
64 canvas->drawRect(bounds, paint);
65
66 canvas->saveLayer(&bounds, nullptr);
67
68 r = bounds;
69 r.inset(kInset, 0);
70 paint.setShader(s1);
71 paint.setColor(s1 ? SK_ColorBLACK : SkColorSetA(kColor1, 0x80));
72 canvas->drawOval(r, paint);
73 if (!s1) {
74 canvas->save();
75 canvas->clipRect(c);
76 paint.setColor(kColor1);
77 canvas->drawOval(r, paint);
78 canvas->restore();
79 }
80
81 SkPaint xferPaint;
82 xferPaint.setBlendMode(mode);
83 canvas->saveLayer(&bounds, &xferPaint);
84
85 r = bounds;
86 r.inset(0, kInset);
87 paint.setShader(s2);
88 paint.setColor(s2 ? SK_ColorBLACK : SkColorSetA(kColor2, 0x80));
89 paint.setColorFilter(filter);
90 canvas->drawOval(r, paint);
91 if (!s2) {
92 canvas->save();
93 canvas->clipRect(c);
94 paint.setColor(kColor2);
95 canvas->drawOval(r, paint);
96 canvas->restore();
97 }
98
99 canvas->restore();
100 canvas->restore();
101}
102
103class LumaFilterGM : public skiagm::GM {
104protected:
105 void onOnceBeforeDraw() override {
106 SkColor g1Colors[] = { kColor1, SkColorSetA(kColor1, 0x20) };
107 SkColor g2Colors[] = { kColor2, SkColorSetA(kColor2, 0x20) };
108 SkPoint g1Points[] = { { 0, 0 }, { 0, 100 } };
109 SkPoint g2Points[] = { { 0, 0 }, { kSize, 0 } };
110 SkScalar pos[] = { 0.2f, 1.0f };
111
112 fFilter = SkLumaColorFilter::Make();
113 fGr1 = SkGradientShader::MakeLinear(g1Points, g1Colors, pos, std::size(g1Colors),
115 fGr2 = SkGradientShader::MakeLinear(g2Points, g2Colors, pos, std::size(g2Colors),
117 }
118
119 SkString getName() const override { return SkString("lumafilter"); }
120
121 SkISize getISize() override { return SkISize::Make(600, 420); }
122
123 void onDraw(SkCanvas* canvas) override {
124 SkBlendMode modes[] = {
131 };
132 struct {
133 const sk_sp<SkShader>& fShader1;
134 const sk_sp<SkShader>& fShader2;
135 } shaders[] = {
136 { nullptr, nullptr },
137 { nullptr, fGr2 },
138 { fGr1, nullptr },
139 { fGr1, fGr2 },
140 };
141
142 SkScalar gridStep = kSize + 2 * kInset;
143 for (size_t i = 0; i < std::size(modes); ++i) {
144 draw_label(canvas, SkBlendMode_Name(modes[i]),
145 SkPoint::Make(gridStep * (0.5f + i), 20));
146 }
147
148 for (size_t i = 0; i < std::size(shaders); ++i) {
149 canvas->save();
150 canvas->translate(kInset, gridStep * i + 30);
151 for (size_t m = 0; m < std::size(modes); ++m) {
152 draw_scene(canvas, fFilter, modes[m], shaders[i].fShader1,
153 shaders[i].fShader2);
154 canvas->translate(gridStep, 0);
155 }
156 canvas->restore();
157 }
158 }
159
160private:
161 sk_sp<SkColorFilter> fFilter;
162 sk_sp<SkShader> fGr1, fGr2;
163
164 using INHERITED = skiagm::GM;
165};
166
167//////////////////////////////////////////////////////////////////////////////
168
169DEF_GM(return new LumaFilterGM;)
170
171DEF_SIMPLE_GM(AlternateLuma, canvas, 384,128) {
172 sk_sp<SkImage> img = ToolUtils::GetResourceAsImage("images/mandrill_128.png");
173 if (!img) {
174 return;
175 }
176
177 // Normal luma colorfilter on the left.
179 paint.setColorFilter(SkLumaColorFilter::Make());
180 canvas->drawImage(img, 0,0, SkFilterMode::kNearest, &paint);
181 canvas->translate(128,0);
182
183 // Original image in the middle for reference.
184 canvas->drawImage(img, 0,0);
185 canvas->translate(128,0);
186
187 // Here, RGB holds CIE XYZ. Splatting the G (Y) channel should result in (near) greyscale.
189 "half4 main(half4 inColor) { return inColor.ggga; }"});
190 SkASSERT(effect && err.isEmpty());
191
192 sk_sp<SkColorFilter> filter = effect->makeColorFilter(SkData::MakeEmpty());
193 SkASSERT(filter);
194
196 paint.setColorFilter(SkColorFilterPriv::WithWorkingFormat(std::move(filter),
199 &unpremul));
200 canvas->drawImage(img, 0,0, SkFilterMode::kNearest, &paint);
201}
SkPoint pos
kUnpremul_SkAlphaType
SkAlphaType
Definition: SkAlphaType.h:26
#define SkASSERT(cond)
Definition: SkAssert.h:116
SK_API const char * SkBlendMode_Name(SkBlendMode blendMode)
SkBlendMode
Definition: SkBlendMode.h:38
@ kDstIn
r = d * sa
@ kSrcOver
r = s + (1-sa)*d
@ kSrcATop
r = s*da + d*(1-sa)
@ kDstATop
r = d*sa + s*(1-da)
@ kDstOver
r = d + (1-da)*s
@ kSrcIn
r = s * da
uint32_t SkColor
Definition: SkColor.h:37
static constexpr SkColor SkColorSetA(SkColor c, U8CPU a)
Definition: SkColor.h:82
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColor.h:49
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
@ kUTF8
uses bytes to represent UTF-8 or ASCII
void onOnceBeforeDraw() override
Definition: lumafilter.cpp:105
SkISize getISize() override
Definition: lumafilter.cpp:121
void onDraw(SkCanvas *canvas) override
Definition: lumafilter.cpp:123
SkString getName() const override
Definition: lumafilter.cpp:119
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition: SkCanvas.cpp:496
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void drawOval(const SkRect &oval, const SkPaint &paint)
Definition: SkCanvas.cpp:1698
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
Definition: SkCanvas.cpp:1361
void restore()
Definition: SkCanvas.cpp:461
void drawSimpleText(const void *text, size_t byteLength, SkTextEncoding encoding, SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.cpp:2413
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
int save()
Definition: SkCanvas.cpp:447
static sk_sp< SkColorFilter > WithWorkingFormat(sk_sp< SkColorFilter > child, const skcms_TransferFunction *tf, const skcms_Matrix3x3 *gamut, const SkAlphaType *at)
static sk_sp< SkData > MakeEmpty()
Definition: SkData.cpp:94
Definition: SkFont.h:35
@ kAlias
no transparent pixels on glyph edges
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 setBlendMode(SkBlendMode mode)
Definition: SkPaint.cpp:151
static Result MakeForColorFilter(SkString sksl, const Options &)
Definition: gm.h:110
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
#define DEF_GM(CODE)
Definition: gm.h:40
static SkColor kColor1
Definition: lumafilter.cpp:40
DEF_SIMPLE_GM(AlternateLuma, canvas, 384, 128)
Definition: lumafilter.cpp:171
static void draw_scene(SkCanvas *canvas, const sk_sp< SkColorFilter > &filter, SkBlendMode mode, const sk_sp< SkShader > &s1, const sk_sp< SkShader > &s2)
Definition: lumafilter.cpp:55
static SkScalar kInset
Definition: lumafilter.cpp:39
static SkScalar kSize
Definition: lumafilter.cpp:38
static SkColor kColor2
Definition: lumafilter.cpp:41
static void draw_label(SkCanvas *canvas, const char *label, const SkPoint &offset)
Definition: lumafilter.cpp:43
static constexpr skcms_Matrix3x3 kXYZ
Definition: SkColorSpace.h:99
static constexpr skcms_TransferFunction kLinear
Definition: SkColorSpace.h:51
Optional< SkRect > bounds
Definition: SkRecords.h:189
SkFont DefaultPortableFont()
sk_sp< SkImage > GetResourceAsImage(const char *resource)
Definition: DecodeUtils.h:25
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
font
Font Metadata and Metrics.
int32_t width
SeparatedVector2 offset
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static sk_sp< SkColorFilter > Make()
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
void inset(float dx, float dy)
Definition: SkRect.h:1060
SkScalar fRight
larger x-axis bounds
Definition: extension.cpp:16
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609