Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
tablecolorfilter.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#include <math.h>
29#include <utility>
30
31static sk_sp<SkShader> make_shader0(int w, int h) {
32 SkPoint pts[] = { {0, 0}, {SkIntToScalar(w), SkIntToScalar(h)} };
33 SkColor colors[] = {
36 };
37 return SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors),
39}
40static void make_bm0(SkBitmap* bm) {
41 int W = 120;
42 int H = 120;
43 bm->allocN32Pixels(W, H);
45
46 SkCanvas canvas(*bm);
48 paint.setShader(make_shader0(W, H));
49 canvas.drawPaint(paint);
50}
51static sk_sp<SkShader> make_shader1(int w, int h) {
52 SkScalar cx = SkIntToScalar(w)/2;
53 SkScalar cy = SkIntToScalar(h)/2;
54 SkColor colors[] = {
56 };
57 return SkGradientShader::MakeRadial(SkPoint::Make(cx, cy), cx, colors, nullptr,
58 std::size(colors), SkTileMode::kClamp);
59}
60static void make_bm1(SkBitmap* bm) {
61 int W = 120;
62 int H = 120;
63 SkScalar cx = SkIntToScalar(W)/2;
64 SkScalar cy = SkIntToScalar(H)/2;
65 bm->allocN32Pixels(W, H);
67
68 SkCanvas canvas(*bm);
70 paint.setShader(make_shader1(W, H));
71 paint.setAntiAlias(true);
72 canvas.drawCircle(cx, cy, cx, paint);
73}
74
75static void make_table0(uint8_t table[]) {
76 for (int i = 0; i < 256; ++i) {
77 int n = i >> 5;
78 table[i] = (n << 5) | (n << 2) | (n >> 1);
79 }
80}
81static void make_table1(uint8_t table[]) {
82 for (int i = 0; i < 256; ++i) {
83 table[i] = i * i / 255;
84 }
85}
86static void make_table2(uint8_t table[]) {
87 for (int i = 0; i < 256; ++i) {
88 float fi = i / 255.0f;
89 table[i] = static_cast<uint8_t>(sqrtf(fi) * 255);
90 }
91}
92
94 return nullptr;
95}
96
98 uint8_t table[256]; make_table0(table);
100}
102 uint8_t table[256]; make_table1(table);
104}
106 uint8_t table[256]; make_table2(table);
108}
110 uint8_t table0[256]; make_table0(table0);
111 uint8_t table1[256]; make_table1(table1);
112 uint8_t table2[256]; make_table2(table2);
113 return SkColorFilters::TableARGB(nullptr, table0, table1, table2);
114}
115
117public:
119
120protected:
121 SkString getName() const override { return SkString("tablecolorfilter"); }
122
123 SkISize getISize() override { return {700, 1650}; }
124
125 void onDraw(SkCanvas* canvas) override {
126 canvas->drawColor(0xFFDDDDDD);
127 canvas->translate(20, 20);
128
129 static sk_sp<SkColorFilter> (*gColorFilterMakers[])() = {
131 };
132 static void (*gBitmapMakers[])(SkBitmap*) = { make_bm0, make_bm1 };
133
134 // This test will be done once for each bitmap with the results stacked vertically.
135 // For a single bitmap the resulting image will be the following:
136 // - A first line with the original bitmap, followed by the image drawn once
137 // with each of the N color filters
138 // - N lines of the bitmap drawn N times, this will cover all N*N combinations of
139 // pair of color filters in order to test the collapsing of consecutive table
140 // color filters.
141 //
142 // Here is a graphical representation of the result for 2 bitmaps and 2 filters
143 // with the number corresponding to the number of filters the bitmap goes through:
144 //
145 // --bitmap1
146 // 011
147 // 22
148 // 22
149 // --bitmap2
150 // 011
151 // 22
152 // 22
153
154 SkScalar x = 0, y = 0;
155 for (size_t bitmapMaker = 0; bitmapMaker < std::size(gBitmapMakers); ++bitmapMaker) {
156 SkBitmap bm;
157 gBitmapMakers[bitmapMaker](&bm);
158
159 SkScalar xOffset = SkScalar(bm.width() * 9 / 8);
160 SkScalar yOffset = SkScalar(bm.height() * 9 / 8);
161
162 // Draw the first element of the first line
163 x = 0;
165 SkSamplingOptions sampling;
166
167 canvas->drawImage(bm.asImage(), x, y);
168
169 // Draws the rest of the first line for this bitmap
170 // each draw being at xOffset of the previous one
171 for (unsigned i = 1; i < std::size(gColorFilterMakers); ++i) {
172 x += xOffset;
173 paint.setColorFilter(gColorFilterMakers[i]());
174 canvas->drawImage(bm.asImage(), x, y, sampling, &paint);
175 }
176
177 paint.setColorFilter(nullptr);
178
179 for (unsigned i = 0; i < std::size(gColorFilterMakers); ++i) {
180 sk_sp<SkColorFilter> colorFilter1(gColorFilterMakers[i]());
182 std::move(colorFilter1), nullptr));
183
184 // Move down to the next line and draw it
185 // each draw being at xOffset of the previous one
186 y += yOffset;
187 x = 0;
188 for (unsigned j = 1; j < std::size(gColorFilterMakers); ++j) {
189 sk_sp<SkColorFilter> colorFilter2(gColorFilterMakers[j]());
191 std::move(colorFilter2), imageFilter1, nullptr));
192 paint.setImageFilter(std::move(imageFilter2));
193 canvas->drawImage(bm.asImage(), x, y, sampling, &paint);
194 x += xOffset;
195 }
196 }
197
198 // Move down one line to the beginning of the block for next bitmap
199 y += yOffset;
200 }
201 }
202
203private:
204 using INHERITED = GM;
205};
206DEF_GM( return new TableColorFilterGM; )
207
208//////////////////////////////////////////////////////////////////////////////
209
210class ComposeColorFilterGM : public skiagm::GM {
211 enum {
212 COLOR_COUNT = 3,
213 MODE_COUNT = 4,
214 };
215 const SkColor* fColors;
216 const SkBlendMode* fModes;
217 const char* fName;
218
219public:
220 ComposeColorFilterGM(const SkColor colors[], const SkBlendMode modes[], const char* name)
221 : fColors(colors), fModes(modes), fName(name) {}
222
223private:
224 SkString getName() const override { return SkString(fName); }
225
226 SkISize getISize() override { return {790, 790}; }
227
228 void onDraw(SkCanvas* canvas) override {
229 SkBitmap bm;
230 make_bm1(&bm);
231
232 canvas->drawColor(0xFFDDDDDD);
233
234 const int MODES = MODE_COUNT * COLOR_COUNT;
235 sk_sp<SkColorFilter> filters[MODES];
236 int index = 0;
237 for (int i = 0; i < MODE_COUNT; ++i) {
238 for (int j = 0; j < COLOR_COUNT; ++j) {
239 filters[index++] = SkColorFilters::Blend(fColors[j], fModes[i]);
240 }
241 }
242
244 paint.setShader(make_shader1(50, 50));
245 SkRect r = SkRect::MakeWH(50, 50);
246 const SkScalar spacer = 10;
247
248 canvas->translate(spacer, spacer);
249
250 canvas->drawRect(r, paint); // orig
251
252 for (int i = 0; i < MODES; ++i) {
253 paint.setColorFilter(filters[i]);
254
255 canvas->save();
256 canvas->translate((i + 1) * (r.width() + spacer), 0);
257 canvas->drawRect(r, paint);
258 canvas->restore();
259
260 canvas->save();
261 canvas->translate(0, (i + 1) * (r.width() + spacer));
262 canvas->drawRect(r, paint);
263 canvas->restore();
264 }
265
266 canvas->translate(r.width() + spacer, r.width() + spacer);
267
268 for (int y = 0; y < MODES; ++y) {
269 canvas->save();
270 for (int x = 0; x < MODES; ++x) {
271 paint.setColorFilter(filters[y]->makeComposed(filters[x]));
272 canvas->drawRect(r, paint);
273 canvas->translate(r.width() + spacer, 0);
274 }
275 canvas->restore();
276 canvas->translate(0, r.height() + spacer);
277 }
278 }
279};
280
288DEF_GM( return new ComposeColorFilterGM(gColors0, gModes0, "colorcomposefilter_wacky"); )
289
290const SkColor gColors1[] = { 0x80FF0000, 0x8000FF00, 0x800000FF };
297DEF_GM( return new ComposeColorFilterGM(gColors1, gModes1, "colorcomposefilter_alpha"); )
const char * fName
SkBlendMode
Definition SkBlendMode.h:38
@ kExclusion
rc = s + d - two(s*d), ra = kSrcOver
@ kColorBurn
darken destination to reflect source
@ kSrcOver
r = s + (1-sa)*d
@ kXor
r = s*(1-da) + d*(1-sa)
@ kOverlay
multiply or screen, depending on destination
@ kSrcATop
r = s*da + d*(1-sa)
@ kDstOut
r = d * (1-sa)
@ kDarken
rc = s + d - max(s*da, d*sa), ra = kSrcOver
constexpr SkColor SK_ColorYELLOW
Definition SkColor.h:139
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_ColorTRANSPARENT
Definition SkColor.h:99
constexpr SkColor SK_ColorBLUE
Definition SkColor.h:135
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
SI F table(const skcms_Curve *curve, F v)
#define W
Definition aaa.cpp:17
const int COLOR_COUNT
SkISize getISize() override
ComposeColorFilterGM(const SkColor colors[], const SkBlendMode modes[], const char *name)
SkString getName() const override
void onDraw(SkCanvas *canvas) override
sk_sp< SkImage > asImage() const
Definition SkBitmap.cpp:645
int width() const
Definition SkBitmap.h:149
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition SkBitmap.cpp:232
int height() const
Definition SkBitmap.h:158
void eraseColor(SkColor4f) const
Definition SkBitmap.cpp:442
void drawRect(const SkRect &rect, const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
void drawColor(SkColor color, SkBlendMode mode=SkBlendMode::kSrcOver)
Definition SkCanvas.h:1182
void drawPaint(const SkPaint &paint)
int save()
Definition SkCanvas.cpp:451
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
void drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint &paint)
static sk_sp< SkColorFilter > Blend(const SkColor4f &c, sk_sp< SkColorSpace >, SkBlendMode mode)
static sk_sp< SkColorFilter > Table(const uint8_t table[256])
static sk_sp< SkColorFilter > TableARGB(const uint8_t tableA[256], const uint8_t tableR[256], const uint8_t tableG[256], const uint8_t tableB[256])
static sk_sp< SkShader > MakeRadial(const SkPoint &center, SkScalar radius, const SkColor colors[], const SkScalar pos[], int count, SkTileMode mode, uint32_t flags=0, const SkMatrix *localMatrix=nullptr)
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)
static sk_sp< SkImageFilter > ColorFilter(sk_sp< SkColorFilter > cf, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
SkISize getISize() override
SkString getName() const override
void onDraw(SkCanvas *canvas) override
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition gm.cpp:81
const Paint & paint
float SkScalar
Definition extension.cpp:12
const char * name
Definition fuchsia.cc:50
#define DEF_GM(CODE)
Definition gm.h:40
double y
double x
SkScalar w
SkScalar h
Definition SkMD5.cpp:130
static constexpr SkPoint Make(float x, float y)
constexpr float height() const
Definition SkRect.h:769
constexpr float width() const
Definition SkRect.h:762
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
static void make_table0(uint8_t table[])
const SkColor gColors1[]
static void make_table2(uint8_t table[])
static sk_sp< SkColorFilter > make_null_cf()
static sk_sp< SkShader > make_shader1(int w, int h)
static sk_sp< SkShader > make_shader0(int w, int h)
static sk_sp< SkColorFilter > make_cf0()
static void make_bm0(SkBitmap *bm)
static sk_sp< SkColorFilter > make_cf1()
const SkBlendMode gModes0[]
static void make_table1(uint8_t table[])
static void make_bm1(SkBitmap *bm)
static sk_sp< SkColorFilter > make_cf3()
const SkBlendMode gModes1[]
static sk_sp< SkColorFilter > make_cf2()
const SkColor gColors0[]