Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
modecolorfilters.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2012 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"
16#include "include/core/SkRect.h"
20#include "include/core/SkSize.h"
25#include "tools/ToolUtils.h"
26
27#define WIDTH 512
28#define HEIGHT 1024
29
30namespace skiagm {
31
32// Using gradients because GPU doesn't currently have an implementation of SkColorShader (duh!)
34 constexpr SkPoint kPts[] = {{0, 0}, {1, 1}};
35 SkColor colors[] = {color, color};
36
37 return SkGradientShader::MakeLinear(kPts, colors, nullptr, 2, SkTileMode::kClamp);
38}
39
41 return make_color_shader(SkColorSetARGB(0xFF, 0x42, 0x82, 0x21));
42}
43
45 return make_color_shader(SkColorSetARGB(0x80, 0x10, 0x70, 0x20));
46}
47
51
52// draws a background behind each test rect to see transparency
53static sk_sp<SkShader> make_bg_shader(int checkSize) {
54 SkBitmap bmp;
55 bmp.allocN32Pixels(2 * checkSize, 2 * checkSize);
56 SkCanvas canvas(bmp);
57 canvas.clear(ToolUtils::color_to_565(0xFF800000));
59 paint.setColor(ToolUtils::color_to_565(0xFF000080));
60 SkRect rect0 = SkRect::MakeXYWH(0, 0,
61 SkIntToScalar(checkSize), SkIntToScalar(checkSize));
62 SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(checkSize), SkIntToScalar(checkSize),
63 SkIntToScalar(checkSize), SkIntToScalar(checkSize));
64 canvas.drawRect(rect1, paint);
65 canvas.drawRect(rect0, paint);
67}
68
69class ModeColorFilterGM : public GM {
70public:
72 this->setBGColor(0xFF303030);
73 }
74
75protected:
76 SkString getName() const override { return SkString("modecolorfilters"); }
77
78 SkISize getISize() override { return SkISize::Make(WIDTH, HEIGHT); }
79
80 void onDraw(SkCanvas* canvas) override {
81 // size of rect for each test case
82 constexpr int kRectWidth = 20;
83 constexpr int kRectHeight = 20;
84
85 constexpr int kCheckSize = 10;
86
87 if (!fBmpShader) {
88 fBmpShader = make_bg_shader(kCheckSize);
89 }
90 SkPaint bgPaint;
91 bgPaint.setShader(fBmpShader);
93
94 sk_sp<SkShader> shaders[] = {
95 nullptr, // use a paint color instead of a shader
99 };
100
101 // used without shader
102 SkColor colors[] = {
103 SkColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF),
104 SkColorSetARGB(0xFF, 0x00, 0x00, 0x00),
105 SkColorSetARGB(0x00, 0x00, 0x00, 0x00),
106 SkColorSetARGB(0xFF, 0x10, 0x20, 0x42),
107 SkColorSetARGB(0xA0, 0x20, 0x30, 0x90),
108 };
109
110 // used with shaders
111 SkColor alphas[] = {0xFFFFFFFF, 0x80808080};
112
113 const SkBlendMode modes[] = { // currently just doing the Modes expressible as Coeffs
128 };
129
131 int idx = 0;
132 const int kRectsPerRow = std::max(this->getISize().fWidth / kRectWidth, 1);
133 for (size_t cfm = 0; cfm < std::size(modes); ++cfm) {
134 for (size_t cfc = 0; cfc < std::size(colors); ++cfc) {
135 paint.setColorFilter(SkColorFilters::Blend(colors[cfc], modes[cfm]));
136 for (size_t s = 0; s < std::size(shaders); ++s) {
137 paint.setShader(shaders[s]);
138 bool hasShader = nullptr == paint.getShader();
139 int paintColorCnt = hasShader ? std::size(alphas) : std::size(colors);
140 SkColor* paintColors = hasShader ? alphas : colors;
141 for (int pc = 0; pc < paintColorCnt; ++pc) {
142 paint.setColor(paintColors[pc]);
143 SkScalar x = SkIntToScalar(idx % kRectsPerRow);
144 SkScalar y = SkIntToScalar(idx / kRectsPerRow);
145 SkRect rect = SkRect::MakeXYWH(x * kRectWidth, y * kRectHeight,
146 SkIntToScalar(kRectWidth),
148 canvas->saveLayer(&rect, nullptr);
149 canvas->drawRect(rect, bgPaint);
150 canvas->drawRect(rect, paint);
151 canvas->restore();
152 ++idx;
153 }
154 }
155 }
156 }
157 }
158
159private:
160 sk_sp<SkShader> fBmpShader;
161 using INHERITED = GM;
162};
163
164//////////////////////////////////////////////////////////////////////////////
165
166DEF_GM( return new ModeColorFilterGM; )
167
168} // namespace skiagm
SkColor4f color
SkBlendMode
Definition SkBlendMode.h:38
@ kSrcOut
r = s * (1-da)
@ kPlus
r = min(s + d, 1)
@ kDstIn
r = d * sa
@ kModulate
r = s*d
@ kSrcOver
r = s + (1-sa)*d
@ kXor
r = s*(1-da) + d*(1-sa)
@ kSrcATop
r = s*da + d*(1-sa)
@ kDstATop
r = d*sa + s*(1-da)
@ kDstOver
r = d + (1-da)*s
@ kDstOut
r = d * (1-sa)
@ kSrcIn
r = s * da
@ kClear
r = 0
uint32_t SkColor
Definition SkColor.h:37
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition SkColor.h:49
#define SkIntToScalar(x)
Definition SkScalar.h:57
#define WIDTH
#define HEIGHT
static sk_sp< SkShader > make_bg_shader()
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition SkBitmap.cpp:669
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition SkBitmap.cpp:232
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition SkCanvas.cpp:500
void drawRect(const SkRect &rect, const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
void clear(SkColor color)
Definition SkCanvas.h:1199
static sk_sp< SkColorFilter > Blend(const SkColor4f &c, sk_sp< SkColorSpace >, SkBlendMode mode)
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 setShader(sk_sp< SkShader > shader)
void setBlendMode(SkBlendMode mode)
Definition SkPaint.cpp:151
void setBGColor(SkColor)
Definition gm.cpp:159
SkString getName() const override
void onDraw(SkCanvas *canvas) override
const Paint & paint
float SkScalar
Definition extension.cpp:12
struct MyStruct s
#define DEF_GM(CODE)
Definition gm.h:40
const int kRectHeight
double y
double x
SkColor color_to_565(SkColor color)
static constexpr SkPoint kPts[kPtsCount]
static sk_sp< SkShader > make_solid_shader()
static sk_sp< SkShader > make_trans_black_shader()
static sk_sp< SkShader > make_transparent_shader()
static sk_sp< SkShader > make_color_shader(SkColor color)
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659