blob: df6f7d6564dda5b096e928cac6aae78cfc21477b [file] [log] [blame]
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +00001/*
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.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000010#include "SkCanvas.h"
cwallezc12b74d2015-01-26 07:45:53 -080011#include "SkColorFilterImageFilter.h"
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000012#include "SkGradientShader.h"
13#include "SkTableColorFilter.h"
14
reed1a9b9642016-03-13 14:13:58 -070015static sk_sp<SkShader> make_shader0(int w, int h) {
reeddb873d82015-03-01 19:53:47 -080016 SkPoint pts[] = { {0, 0}, {SkIntToScalar(w), SkIntToScalar(h)} };
17 SkColor colors[] = {
18 SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN,
19 SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE
20 };
reed1a9b9642016-03-13 14:13:58 -070021 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
22 SkShader::kClamp_TileMode);
reeddb873d82015-03-01 19:53:47 -080023}
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000024static void make_bm0(SkBitmap* bm) {
25 int W = 120;
26 int H = 120;
reed@google.comeb9a46c2014-01-25 16:46:20 +000027 bm->allocN32Pixels(W, H);
junov@google.comdbfac8a2012-12-06 21:47:40 +000028 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comd6176b02012-08-23 18:14:13 +000029
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000030 SkCanvas canvas(*bm);
31 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -070032 paint.setShader(make_shader0(W, H));
reed5bd055c2015-03-01 19:16:38 -080033 canvas.drawPaint(paint);
reedb675a732015-03-01 18:00:47 -080034}
reed1a9b9642016-03-13 14:13:58 -070035static sk_sp<SkShader> make_shader1(int w, int h) {
reeddb873d82015-03-01 19:53:47 -080036 SkScalar cx = SkIntToScalar(w)/2;
37 SkScalar cy = SkIntToScalar(h)/2;
38 SkColor colors[] = {
39 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
40 };
reed1a9b9642016-03-13 14:13:58 -070041 return SkGradientShader::MakeRadial(SkPoint::Make(cx, cy), cx, colors, nullptr,
42 SK_ARRAY_COUNT(colors), SkShader::kClamp_TileMode);
reeddb873d82015-03-01 19:53:47 -080043}
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000044static void make_bm1(SkBitmap* bm) {
45 int W = 120;
46 int H = 120;
reeddb873d82015-03-01 19:53:47 -080047 SkScalar cx = SkIntToScalar(W)/2;
48 SkScalar cy = SkIntToScalar(H)/2;
reed@google.comeb9a46c2014-01-25 16:46:20 +000049 bm->allocN32Pixels(W, H);
junov@google.comdbfac8a2012-12-06 21:47:40 +000050 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comd6176b02012-08-23 18:14:13 +000051
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000052 SkCanvas canvas(*bm);
53 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -070054 paint.setShader(make_shader1(W, H));
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000055 paint.setAntiAlias(true);
56 canvas.drawCircle(cx, cy, cx, paint);
57}
58
59static void make_table0(uint8_t table[]) {
60 for (int i = 0; i < 256; ++i) {
61 int n = i >> 5;
62 table[i] = (n << 5) | (n << 2) | (n >> 1);
63 }
64}
65static void make_table1(uint8_t table[]) {
66 for (int i = 0; i < 256; ++i) {
67 table[i] = i * i / 255;
68 }
69}
70static void make_table2(uint8_t table[]) {
71 for (int i = 0; i < 256; ++i) {
72 float fi = i / 255.0f;
robertphillips@google.com6853e802012-04-16 15:50:18 +000073 table[i] = static_cast<uint8_t>(sqrtf(fi) * 255);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000074 }
75}
76
reedd053ce92016-03-22 10:17:23 -070077static sk_sp<SkColorFilter> make_null_cf() {
halcanary96fcdcc2015-08-27 07:41:13 -070078 return nullptr;
cwallezc12b74d2015-01-26 07:45:53 -080079}
80
reedd053ce92016-03-22 10:17:23 -070081static sk_sp<SkColorFilter> make_cf0() {
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000082 uint8_t table[256]; make_table0(table);
reedd053ce92016-03-22 10:17:23 -070083 return SkTableColorFilter::Make(table);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000084}
reedd053ce92016-03-22 10:17:23 -070085static sk_sp<SkColorFilter> make_cf1() {
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000086 uint8_t table[256]; make_table1(table);
reedd053ce92016-03-22 10:17:23 -070087 return SkTableColorFilter::Make(table);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000088}
reedd053ce92016-03-22 10:17:23 -070089static sk_sp<SkColorFilter> make_cf2() {
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000090 uint8_t table[256]; make_table2(table);
reedd053ce92016-03-22 10:17:23 -070091 return SkTableColorFilter::Make(table);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000092}
reedd053ce92016-03-22 10:17:23 -070093static sk_sp<SkColorFilter> make_cf3() {
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000094 uint8_t table0[256]; make_table0(table0);
95 uint8_t table1[256]; make_table1(table1);
96 uint8_t table2[256]; make_table2(table2);
reedd053ce92016-03-22 10:17:23 -070097 return SkTableColorFilter::MakeARGB(nullptr, table0, table1, table2);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000098}
99
100class TableColorFilterGM : public skiagm::GM {
101public:
102 TableColorFilterGM() {}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000103
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000104protected:
105 virtual SkString onShortName() {
106 return SkString("tablecolorfilter");
107 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000108
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000109 virtual SkISize onISize() {
cwallezc12b74d2015-01-26 07:45:53 -0800110 return SkISize::Make(700, 1650);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000111 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000112
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000113 virtual void onDraw(SkCanvas* canvas) {
caryclark12596012015-07-29 05:27:47 -0700114 canvas->drawColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000115 canvas->translate(20, 20);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000116
cwallezc12b74d2015-01-26 07:45:53 -0800117
reedd053ce92016-03-22 10:17:23 -0700118 static sk_sp<SkColorFilter> (*gColorFilterMakers[])() = {
119 make_null_cf, make_cf0, make_cf1, make_cf2, make_cf3
120 };
cwallezc12b74d2015-01-26 07:45:53 -0800121 static void (*gBitmapMakers[])(SkBitmap*) = { make_bm0, make_bm1 };
122
123 // This test will be done once for each bitmap with the results stacked vertically.
124 // For a single bitmap the resulting image will be the following:
125 // - A first line with the original bitmap, followed by the image drawn once
126 // with each of the N color filters
127 // - N lines of the bitmap drawn N times, this will cover all N*N combinations of
128 // pair of color filters in order to test the collpsing of consecutive table
129 // color filters.
130 //
131 // Here is a graphical representation of the result for 2 bitmaps and 2 filters
132 // with the number corresponding to the number of filters the bitmap goes through:
133 //
134 // --bitmap1
135 // 011
136 // 22
137 // 22
138 // --bitmap2
139 // 011
140 // 22
141 // 22
142
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000143 SkScalar x = 0, y = 0;
cwallezc12b74d2015-01-26 07:45:53 -0800144 for (size_t bitmapMaker = 0; bitmapMaker < SK_ARRAY_COUNT(gBitmapMakers); ++bitmapMaker) {
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000145 SkBitmap bm;
cwallezc12b74d2015-01-26 07:45:53 -0800146 gBitmapMakers[bitmapMaker](&bm);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000147
cwallezc12b74d2015-01-26 07:45:53 -0800148 SkScalar xOffset = SkScalar(bm.width() * 9 / 8);
149 SkScalar yOffset = SkScalar(bm.height() * 9 / 8);
150
151 // Draw the first element of the first line
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000152 x = 0;
cwallezc12b74d2015-01-26 07:45:53 -0800153 SkPaint paint;
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000154 canvas->drawBitmap(bm, x, y, &paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000155
cwallezc12b74d2015-01-26 07:45:53 -0800156 // Draws the rest of the first line for this bitmap
157 // each draw being at xOffset of the previous one
158 for (unsigned i = 1; i < SK_ARRAY_COUNT(gColorFilterMakers); ++i) {
159 x += xOffset;
reedd053ce92016-03-22 10:17:23 -0700160 paint.setColorFilter(gColorFilterMakers[i]());
cwallezc12b74d2015-01-26 07:45:53 -0800161 canvas->drawBitmap(bm, x, y, &paint);
162 }
163
halcanary96fcdcc2015-08-27 07:41:13 -0700164 paint.setColorFilter(nullptr);
cwallezc12b74d2015-01-26 07:45:53 -0800165
166 for (unsigned i = 0; i < SK_ARRAY_COUNT(gColorFilterMakers); ++i) {
robertphillips5605b562016-04-05 11:50:42 -0700167 sk_sp<SkColorFilter> colorFilter1(gColorFilterMakers[i]());
168 sk_sp<SkImageFilter> imageFilter1(SkColorFilterImageFilter::Make(
169 std::move(colorFilter1), nullptr));
cwallezc12b74d2015-01-26 07:45:53 -0800170
171 // Move down to the next line and draw it
172 // each draw being at xOffset of the previous one
173 y += yOffset;
174 x = 0;
175 for (unsigned j = 1; j < SK_ARRAY_COUNT(gColorFilterMakers); ++j) {
robertphillips5605b562016-04-05 11:50:42 -0700176 sk_sp<SkColorFilter> colorFilter2(gColorFilterMakers[j]());
177 sk_sp<SkImageFilter> imageFilter2(SkColorFilterImageFilter::Make(
178 std::move(colorFilter2), imageFilter1, nullptr));
179 paint.setImageFilter(std::move(imageFilter2));
cwallezc12b74d2015-01-26 07:45:53 -0800180 canvas->drawBitmap(bm, x, y, &paint);
181 x += xOffset;
182 }
183 }
184
185 // Move down one line to the beginning of the block for next bitmap
186 y += yOffset;
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000187 }
188 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000189
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000190private:
191 typedef GM INHERITED;
192};
reeddb873d82015-03-01 19:53:47 -0800193DEF_GM( return new TableColorFilterGM; )
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000194
195//////////////////////////////////////////////////////////////////////////////
196
reeddb873d82015-03-01 19:53:47 -0800197class ComposeColorFilterGM : public skiagm::GM {
reedcff10b22015-03-03 06:41:45 -0800198 enum {
199 COLOR_COUNT = 3,
200 MODE_COUNT = 4,
201 };
Mike Reed7d954ad2016-10-28 15:42:34 -0400202 const SkColor* fColors;
203 const SkBlendMode* fModes;
204 SkString fName;
reedcff10b22015-03-03 06:41:45 -0800205
reeddb873d82015-03-01 19:53:47 -0800206public:
Mike Reed7d954ad2016-10-28 15:42:34 -0400207 ComposeColorFilterGM(const SkColor colors[], const SkBlendMode modes[],
reedcff10b22015-03-03 06:41:45 -0800208 const char suffix[])
209 : fColors(colors), fModes(modes)
210 {
211 fName.printf("colorcomposefilter_%s", suffix);
212 }
halcanary9d524f22016-03-29 09:03:52 -0700213
reeddb873d82015-03-01 19:53:47 -0800214protected:
215 virtual SkString onShortName() {
reedcff10b22015-03-03 06:41:45 -0800216 return fName;
reeddb873d82015-03-01 19:53:47 -0800217 }
halcanary9d524f22016-03-29 09:03:52 -0700218
reeddb873d82015-03-01 19:53:47 -0800219 virtual SkISize onISize() {
reedcff10b22015-03-03 06:41:45 -0800220 return SkISize::Make(790, 790);
reeddb873d82015-03-01 19:53:47 -0800221 }
222
223 virtual void onDraw(SkCanvas* canvas) {
224 SkBitmap bm;
225 make_bm1(&bm);
226
caryclark12596012015-07-29 05:27:47 -0700227 canvas->drawColor(sk_tool_utils::color_to_565(0xFFDDDDDD));
reeddb873d82015-03-01 19:53:47 -0800228
reedcff10b22015-03-03 06:41:45 -0800229 const int MODES = MODE_COUNT * COLOR_COUNT;
reedd053ce92016-03-22 10:17:23 -0700230 sk_sp<SkColorFilter> filters[MODES];
reeddb873d82015-03-01 19:53:47 -0800231 int index = 0;
reedcff10b22015-03-03 06:41:45 -0800232 for (int i = 0; i < MODE_COUNT; ++i) {
233 for (int j = 0; j < COLOR_COUNT; ++j) {
reedd053ce92016-03-22 10:17:23 -0700234 filters[index++] = SkColorFilter::MakeModeFilter(fColors[j], fModes[i]);
reeddb873d82015-03-01 19:53:47 -0800235 }
236 }
237
238 SkPaint paint;
reed1a9b9642016-03-13 14:13:58 -0700239 paint.setShader(make_shader1(50, 50));
reeddb873d82015-03-01 19:53:47 -0800240 SkRect r = SkRect::MakeWH(50, 50);
241 const SkScalar spacer = 10;
242
243 canvas->translate(spacer, spacer);
244
reedcff10b22015-03-03 06:41:45 -0800245 canvas->drawRect(r, paint); // orig
246
247 for (int i = 0; i < MODES; ++i) {
248 paint.setColorFilter(filters[i]);
249
reeddb873d82015-03-01 19:53:47 -0800250 canvas->save();
reedcff10b22015-03-03 06:41:45 -0800251 canvas->translate((i + 1) * (r.width() + spacer), 0);
252 canvas->drawRect(r, paint);
253 canvas->restore();
254
255 canvas->save();
256 canvas->translate(0, (i + 1) * (r.width() + spacer));
257 canvas->drawRect(r, paint);
258 canvas->restore();
259 }
260
261 canvas->translate(r.width() + spacer, r.width() + spacer);
262
263 for (int y = 0; y < MODES; ++y) {
264 canvas->save();
265 for (int x = 0; x < MODES; ++x) {
reedd053ce92016-03-22 10:17:23 -0700266 paint.setColorFilter(SkColorFilter::MakeComposeFilter(filters[y], filters[x]));
reeddb873d82015-03-01 19:53:47 -0800267 canvas->drawRect(r, paint);
268 canvas->translate(r.width() + spacer, 0);
269 }
270 canvas->restore();
271 canvas->translate(0, r.height() + spacer);
272 }
273 }
halcanary9d524f22016-03-29 09:03:52 -0700274
reeddb873d82015-03-01 19:53:47 -0800275private:
276 typedef GM INHERITED;
277};
reeddb873d82015-03-01 19:53:47 -0800278
reedcff10b22015-03-03 06:41:45 -0800279const SkColor gColors0[] = { SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorYELLOW };
Mike Reed7d954ad2016-10-28 15:42:34 -0400280const SkBlendMode gModes0[] = {
281 SkBlendMode::kOverlay,
282 SkBlendMode::kDarken,
283 SkBlendMode::kColorBurn,
284 SkBlendMode::kExclusion,
reedcff10b22015-03-03 06:41:45 -0800285};
286DEF_GM( return new ComposeColorFilterGM(gColors0, gModes0, "wacky"); )
287
288const SkColor gColors1[] = { 0x80FF0000, 0x8000FF00, 0x800000FF };
Mike Reed7d954ad2016-10-28 15:42:34 -0400289const SkBlendMode gModes1[] = {
290 SkBlendMode::kSrcOver,
291 SkBlendMode::kXor,
292 SkBlendMode::kDstOut,
293 SkBlendMode::kSrcATop,
reedcff10b22015-03-03 06:41:45 -0800294};
295DEF_GM( return new ComposeColorFilterGM(gColors1, gModes1, "alpha"); )