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"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorFilter.h"
14 #include "include/core/SkImageFilter.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPoint.h"
17 #include "include/core/SkRect.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkShader.h"
21 #include "include/core/SkSize.h"
22 #include "include/core/SkString.h"
23 #include "include/core/SkTileMode.h"
24 #include "include/core/SkTypes.h"
25 #include "include/effects/SkGradientShader.h"
26 #include "include/effects/SkImageFilters.h"
27
28 #include <math.h>
29 #include <utility>
30
make_shader0(int w,int h)31 static sk_sp<SkShader> make_shader0(int w, int h) {
32 SkPoint pts[] = { {0, 0}, {SkIntToScalar(w), SkIntToScalar(h)} };
33 SkColor colors[] = {
34 SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN,
35 SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE
36 };
37 return SkGradientShader::MakeLinear(pts, colors, nullptr, std::size(colors),
38 SkTileMode::kClamp);
39 }
make_bm0(SkBitmap * bm)40 static void make_bm0(SkBitmap* bm) {
41 int W = 120;
42 int H = 120;
43 bm->allocN32Pixels(W, H);
44 bm->eraseColor(SK_ColorTRANSPARENT);
45
46 SkCanvas canvas(*bm);
47 SkPaint paint;
48 paint.setShader(make_shader0(W, H));
49 canvas.drawPaint(paint);
50 }
make_shader1(int w,int h)51 static sk_sp<SkShader> make_shader1(int w, int h) {
52 SkScalar cx = SkIntToScalar(w)/2;
53 SkScalar cy = SkIntToScalar(h)/2;
54 SkColor colors[] = {
55 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
56 };
57 return SkGradientShader::MakeRadial(SkPoint::Make(cx, cy), cx, colors, nullptr,
58 std::size(colors), SkTileMode::kClamp);
59 }
make_bm1(SkBitmap * bm)60 static 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);
66 bm->eraseColor(SK_ColorTRANSPARENT);
67
68 SkCanvas canvas(*bm);
69 SkPaint paint;
70 paint.setShader(make_shader1(W, H));
71 paint.setAntiAlias(true);
72 canvas.drawCircle(cx, cy, cx, paint);
73 }
74
make_table0(uint8_t table[])75 static 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 }
make_table1(uint8_t table[])81 static void make_table1(uint8_t table[]) {
82 for (int i = 0; i < 256; ++i) {
83 table[i] = i * i / 255;
84 }
85 }
make_table2(uint8_t table[])86 static 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
make_null_cf()93 static sk_sp<SkColorFilter> make_null_cf() {
94 return nullptr;
95 }
96
make_cf0()97 static sk_sp<SkColorFilter> make_cf0() {
98 uint8_t table[256]; make_table0(table);
99 return SkColorFilters::Table(table);
100 }
make_cf1()101 static sk_sp<SkColorFilter> make_cf1() {
102 uint8_t table[256]; make_table1(table);
103 return SkColorFilters::Table(table);
104 }
make_cf2()105 static sk_sp<SkColorFilter> make_cf2() {
106 uint8_t table[256]; make_table2(table);
107 return SkColorFilters::Table(table);
108 }
make_cf3()109 static sk_sp<SkColorFilter> make_cf3() {
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
116 class TableColorFilterGM : public skiagm::GM {
117 public:
TableColorFilterGM()118 TableColorFilterGM() {}
119
120 protected:
getName() const121 SkString getName() const override { return SkString("tablecolorfilter"); }
122
getISize()123 SkISize getISize() override { return {700, 1650}; }
124
onDraw(SkCanvas * canvas)125 void onDraw(SkCanvas* canvas) override {
126 canvas->drawColor(0xFFDDDDDD);
127 canvas->translate(20, 20);
128
129 static sk_sp<SkColorFilter> (*gColorFilterMakers[])() = {
130 make_null_cf, make_cf0, make_cf1, make_cf2, make_cf3
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;
164 SkPaint paint;
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]());
181 sk_sp<SkImageFilter> imageFilter1(SkImageFilters::ColorFilter(
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]());
190 sk_sp<SkImageFilter> imageFilter2(SkImageFilters::ColorFilter(
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
203 private:
204 using INHERITED = GM;
205 };
206 DEF_GM( return new TableColorFilterGM; )
207
208 //////////////////////////////////////////////////////////////////////////////
209
210 class 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
219 public:
ComposeColorFilterGM(const SkColor colors[],const SkBlendMode modes[],const char * name)220 ComposeColorFilterGM(const SkColor colors[], const SkBlendMode modes[], const char* name)
221 : fColors(colors), fModes(modes), fName(name) {}
222
223 private:
getName() const224 SkString getName() const override { return SkString(fName); }
225
getISize()226 SkISize getISize() override { return {790, 790}; }
227
onDraw(SkCanvas * canvas)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
243 SkPaint paint;
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
281 const SkColor gColors0[] = { SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorYELLOW };
282 const SkBlendMode gModes0[] = {
283 SkBlendMode::kOverlay,
284 SkBlendMode::kDarken,
285 SkBlendMode::kColorBurn,
286 SkBlendMode::kExclusion,
287 };
288 DEF_GM( return new ComposeColorFilterGM(gColors0, gModes0, "colorcomposefilter_wacky"); )
289
290 const SkColor gColors1[] = { 0x80FF0000, 0x8000FF00, 0x800000FF };
291 const SkBlendMode gModes1[] = {
292 SkBlendMode::kSrcOver,
293 SkBlendMode::kXor,
294 SkBlendMode::kDstOut,
295 SkBlendMode::kSrcATop,
296 };
297 DEF_GM( return new ComposeColorFilterGM(gColors1, gModes1, "colorcomposefilter_alpha"); )
298