Flutter Engine
The Flutter Engine
colormatrix.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2011 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"
17#include "include/core/SkRect.h"
21#include "include/core/SkSize.h"
27
28#define WIDTH 500
29#define HEIGHT 160
30
32 paint->setColorFilter(SkColorFilters::Matrix(matrix));
33}
34
35static void set_array(SkPaint* paint, const float array[]) {
36 paint->setColorFilter(SkColorFilters::Matrix(array));
37}
38
39class ColorMatrixGM : public skiagm::GM {
40public:
42 this->setBGColor(0xFF808080);
43 }
44
45protected:
46 SkString getName() const override { return SkString("colormatrix"); }
47
48 SkISize getISize() override { return SkISize::Make(WIDTH, HEIGHT); }
49
50 void onOnceBeforeDraw() override {
51 fSolidImg = CreateSolidBitmap(64, 64);
52 fTransparentImg = CreateTransparentBitmap(64, 64);
53 }
54
56 SkBitmap bm;
58 SkCanvas canvas(bm);
59 canvas.clear(0x0);
60 for (int y = 0; y < height; ++y) {
61 for (int x = 0; x < width; ++x) {
63 paint.setColor(SkColorSetARGB(255, x * 255 / width, y * 255 / height, 0));
66 }
67 }
68 return bm.asImage();
69 }
70
71 // creates a bitmap with shades of transparent gray.
73 SkBitmap bm;
75 SkCanvas canvas(bm);
76 canvas.clear(0x0);
77
78 SkPoint pts[] = {{0, 0}, {SkIntToScalar(width), SkIntToScalar(height)}};
79 SkColor colors[] = {0x00000000, 0xFFFFFFFF};
81 paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
84 return bm.asImage();
85 }
86
87 void onDraw(SkCanvas* canvas) override {
90
91 paint.setBlendMode(SkBlendMode::kSrc);
92 const SkImage* bmps[] = { fSolidImg.get(), fTransparentImg.get() };
93
94 for (size_t i = 0; i < std::size(bmps); ++i) {
95 matrix.setIdentity();
97 canvas->drawImage(bmps[i], 0, 0, SkSamplingOptions(), &paint);
98
99 ///////////////////////////////////////////////
100
101 matrix.setSaturation(0.0f);
103 canvas->drawImage(bmps[i], 80, 0, SkSamplingOptions(), &paint);
104
105 matrix.setSaturation(0.5f);
107 canvas->drawImage(bmps[i], 160, 0, SkSamplingOptions(), &paint);
108
109 matrix.setSaturation(1.0f);
111 canvas->drawImage(bmps[i], 240, 0, SkSamplingOptions(), &paint);
112
113 matrix.setSaturation(2.0f);
115 canvas->drawImage(bmps[i], 320, 0, SkSamplingOptions(), &paint);
116
117 ///////////////////////////////////////////////
118
119 // Move red into alpha, set color to white
120 float data[20] = {
121 0, 0, 0, 0, 1,
122 0, 0, 0, 0, 1,
123 0, 0, 0, 0, 1,
124 1, 0, 0, 0, 0,
125 };
126
128 canvas->drawImage(bmps[i], 400, 0, SkSamplingOptions(), &paint);
129 ///////////////////////////////////////////////
130 canvas->translate(0, 80);
131 }
132 }
133
134private:
135 sk_sp<SkImage> fSolidImg;
136 sk_sp<SkImage> fTransparentImg;
137
138 using INHERITED = skiagm::GM;
139};
140DEF_GM( return new ColorMatrixGM; )
uint32_t SkColor
Definition: SkColor.h:37
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColor.h:49
#define SK_Scalar1
Definition: SkScalar.h:18
#define SkIntToScalar(x)
Definition: SkScalar.h:57
SkISize getISize() override
Definition: colormatrix.cpp:48
void onDraw(SkCanvas *canvas) override
Definition: colormatrix.cpp:87
static sk_sp< SkImage > CreateSolidBitmap(int width, int height)
Definition: colormatrix.cpp:55
void onOnceBeforeDraw() override
Definition: colormatrix.cpp:50
static sk_sp< SkImage > CreateTransparentBitmap(int width, int height)
Definition: colormatrix.cpp:72
SkString getName() const override
Definition: colormatrix.cpp:46
sk_sp< SkImage > asImage() const
Definition: SkBitmap.cpp:645
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition: SkBitmap.cpp:232
void drawRect(const SkRect &rect, const SkPaint &paint)
Definition: SkCanvas.cpp:1673
void translate(SkScalar dx, SkScalar dy)
Definition: SkCanvas.cpp:1278
void clear(SkColor color)
Definition: SkCanvas.h:1199
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
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)
T * get() const
Definition: SkRefCnt.h:303
Definition: gm.h:110
SkScalar width()
Definition: gm.h:159
SkScalar height()
Definition: gm.h:162
void setBGColor(SkColor)
Definition: gm.cpp:159
const Paint & paint
Definition: color_source.cc:38
#define WIDTH
Definition: colormatrix.cpp:28
static void set_array(SkPaint *paint, const float array[])
Definition: colormatrix.cpp:35
static void set_color_matrix(SkPaint *paint, const SkColorMatrix &matrix)
Definition: colormatrix.cpp:31
#define HEIGHT
Definition: colormatrix.cpp:29
#define DEF_GM(CODE)
Definition: gm.h:40
double y
double x
unsigned useCenter Optional< SkMatrix > matrix
Definition: SkRecords.h:258
PODArray< SkColor > colors
Definition: SkRecords.h:276
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
SkSamplingOptions(SkFilterMode::kLinear))
Definition: SkSize.h:16
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
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63