Flutter Engine
The Flutter Engine
highcontrastfilter.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2017 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
30#include <stdio.h>
31#include <string.h>
32
34
35static SkScalar kSize = 200;
36static SkColor kColor1 = SkColorSetARGB(0xff, 0xff, 0xff, 0);
37static SkColor kColor2 = SkColorSetARGB(0xff, 0x82, 0xff, 0);
38
39static void draw_label(SkCanvas* canvas, const SkHighContrastConfig& config) {
40 char labelBuffer[256];
41 const char* invertStr =
43 "InvBright" :
45 "InvLight" : "NoInvert"));
46
47 snprintf(labelBuffer, sizeof(labelBuffer), "%s%s contrast=%.1f",
48 config.fGrayscale ? "Gray " : "",
49 invertStr,
50 config.fContrast);
51
53 font.setSize(0.075f);
55
56 size_t len = strlen(labelBuffer);
57
58 SkScalar width = font.measureText(labelBuffer, len, SkTextEncoding::kUTF8);
59 canvas->drawSimpleText(labelBuffer, len, SkTextEncoding::kUTF8, 0.5f - width / 2, 0.16f, font, SkPaint());
60}
61
62static void draw_scene(SkCanvas* canvas, const SkHighContrastConfig& config) {
63 SkRect bounds = SkRect::MakeLTRB(0.0f, 0.0f, 1.0f, 1.0f);
64 SkPaint xferPaint;
66 canvas->saveLayer(&bounds, &xferPaint);
67
69 bounds = SkRect::MakeLTRB(0.1f, 0.2f, 0.9f, 0.4f);
70 paint.setARGB(0xff, 0x66, 0x11, 0x11);
71 canvas->drawRect(bounds, paint);
72
74 font.setSize(0.15f);
76
77 paint.setARGB(0xff, 0xbb, 0x77, 0x77);
78 canvas->drawString("A", 0.15f, 0.35f, font, paint);
79
80 bounds = SkRect::MakeLTRB(0.1f, 0.8f, 0.9f, 1.0f);
81 paint.setARGB(0xff, 0xcc, 0xcc, 0xff);
82 canvas->drawRect(bounds, paint);
83
84 paint.setARGB(0xff, 0x88, 0x88, 0xbb);
85 canvas->drawString("Z", 0.75f, 0.95f, font, paint);
86
87 bounds = SkRect::MakeLTRB(0.1f, 0.4f, 0.9f, 0.6f);
88 SkPoint pts[] = { { 0, 0 }, { 1, 0 } };
90 SkScalar pos[] = { 0.2f, 0.8f };
92 pts, colors, pos,
94 canvas->drawRect(bounds, paint);
95
96 bounds = SkRect::MakeLTRB(0.1f, 0.6f, 0.9f, 0.8f);
97 SkColor colors2[] = { SK_ColorGREEN, SK_ColorWHITE };
99 pts, colors2, pos,
100 std::size(colors2), SkTileMode::kClamp));
101 canvas->drawRect(bounds, paint);
102
103 canvas->restore();
104}
105
107protected:
108 void onOnceBeforeDraw() override {
109 SkColor g1Colors[] = { kColor1, SkColorSetA(kColor1, 0x20) };
110 SkColor g2Colors[] = { kColor2, SkColorSetA(kColor2, 0x20) };
111 SkPoint g1Points[] = { { 0, 0 }, { 0, 100 } };
112 SkPoint g2Points[] = { { 0, 0 }, { kSize, 0 } };
113 SkScalar pos[] = { 0.2f, 1.0f };
114
115 SkHighContrastConfig fConfig;
116 fFilter = SkHighContrastFilter::Make(fConfig);
118 g1Points, g1Colors, pos, std::size(g1Colors),
121 g2Points, g2Colors, pos, std::size(g2Colors),
123 }
124
125 SkString getName() const override { return SkString("highcontrastfilter"); }
126
127 SkISize getISize() override { return SkISize::Make(800, 420); }
128
129 void onDraw(SkCanvas* canvas) override {
131 { false, InvertStyle::kNoInvert, 0.0f },
132 { false, InvertStyle::kInvertBrightness, 0.0f },
133 { false, InvertStyle::kInvertLightness, 0.0f },
134 { false, InvertStyle::kInvertLightness, 0.2f },
135 { true, InvertStyle::kNoInvert, 0.0f },
136 { true, InvertStyle::kInvertBrightness, 0.0f },
137 { true, InvertStyle::kInvertLightness, 0.0f },
138 { true, InvertStyle::kInvertLightness, 0.2f },
139 };
140
141 for (size_t i = 0; i < std::size(configs); ++i) {
142 SkScalar x = kSize * (i % 4);
143 SkScalar y = kSize * (i / 4);
144 canvas->save();
145 canvas->translate(x, y);
146 canvas->scale(kSize, kSize);
147 draw_scene(canvas, configs[i]);
148 draw_label(canvas, configs[i]);
149 canvas->restore();
150 }
151 }
152
153private:
154 sk_sp<SkColorFilter> fFilter;
155 sk_sp<SkShader> fGr1, fGr2;
156
157 using INHERITED = skiagm::GM;
158};
159
160DEF_GM(return new HighContrastFilterGM;)
SkPoint pos
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
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
@ kUTF8
uses bytes to represent UTF-8 or ASCII
void onOnceBeforeDraw() override
SkString getName() const override
void onDraw(SkCanvas *canvas) override
SkISize getISize() override
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 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
void scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.h:1803
Definition: SkFont.h:35
@ kAntiAlias
may have transparent pixels on glyph edges
@ 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 setColorFilter(sk_sp< SkColorFilter > colorFilter)
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
static void draw_label(SkCanvas *canvas, const SkHighContrastConfig &config)
static void draw_scene(SkCanvas *canvas, const SkHighContrastConfig &config)
static SkScalar kSize
static SkColor kColor2
double y
double x
Optional< SkRect > bounds
Definition: SkRecords.h:189
PODArray< SkColor > colors
Definition: SkRecords.h:276
SkFont DefaultPortableFont()
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
static sk_sp< SkColorFilter > Make(const SkHighContrastConfig &config)
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition: SkRect.h:646