senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 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.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkColorMatrixFilter.h" |
| 11 | #include "SkColorPriv.h" |
| 12 | #include "SkShader.h" |
| 13 | |
| 14 | #include "SkBlurImageFilter.h" |
| 15 | #include "SkColorFilterImageFilter.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 16 | #include "SkTDArray.h" |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 17 | |
| 18 | #define FILTER_WIDTH SkIntToScalar(30) |
| 19 | #define FILTER_HEIGHT SkIntToScalar(30) |
| 20 | #define MARGIN SkIntToScalar(10) |
| 21 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 22 | static sk_sp<SkColorFilter> cf_make_brightness(float brightness) { |
Mike Reed | df85c38 | 2017-02-14 10:59:19 -0500 | [diff] [blame] | 23 | SkScalar amount255 = brightness * 255; |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 24 | SkScalar matrix[20] = { |
| 25 | 1, 0, 0, 0, amount255, |
| 26 | 0, 1, 0, 0, amount255, |
| 27 | 0, 0, 1, 0, amount255, |
| 28 | 0, 0, 0, 1, 0 }; |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 29 | return SkColorFilter::MakeMatrixFilterRowMajor255(matrix); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 30 | } |
| 31 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 32 | static sk_sp<SkColorFilter> cf_make_grayscale() { |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 33 | SkScalar matrix[20]; |
| 34 | memset(matrix, 0, 20 * sizeof(SkScalar)); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 35 | matrix[0] = matrix[5] = matrix[10] = 0.2126f; |
| 36 | matrix[1] = matrix[6] = matrix[11] = 0.7152f; |
| 37 | matrix[2] = matrix[7] = matrix[12] = 0.0722f; |
| 38 | matrix[18] = 1.0f; |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 39 | return SkColorFilter::MakeMatrixFilterRowMajor255(matrix); |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 40 | } |
| 41 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 42 | static sk_sp<SkColorFilter> cf_make_colorize(SkColor color) { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 43 | return SkColorFilter::MakeModeFilter(color, SkBlendMode::kSrc); |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 44 | } |
| 45 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 46 | static void sk_gm_get_colorfilters(SkTArray<sk_sp<SkColorFilter>>* array) { |
| 47 | array->push_back(cf_make_brightness(0.5f)); |
| 48 | array->push_back(cf_make_grayscale()); |
| 49 | array->push_back(cf_make_colorize(SK_ColorBLUE)); |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame^] | 53 | |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 54 | #include "SkGradientShader.h" |
| 55 | #include "SkImage.h" |
| 56 | #include "Resources.h" |
| 57 | |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 58 | static sk_sp<SkShader> sh_make_lineargradient0() { |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 59 | const SkPoint pts[] = { { 0, 0 }, { 100, 100 } }; |
| 60 | const SkColor colors[] = { SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE }; |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 61 | return SkGradientShader::MakeLinear(pts, colors, nullptr, 3, SkShader::kRepeat_TileMode); |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 62 | } |
| 63 | |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 64 | static sk_sp<SkShader> sh_make_lineargradient1() { |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 65 | const SkPoint pts[] = { { 0, 0 }, { 100, 100 } }; |
| 66 | const SkColor colors[] = { SK_ColorRED, 0x0000FF00, SK_ColorBLUE }; |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 67 | return SkGradientShader::MakeLinear(pts, colors, nullptr, 3, SkShader::kRepeat_TileMode); |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 68 | } |
| 69 | |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 70 | static sk_sp<SkShader> sh_make_image() { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 71 | sk_sp<SkImage> image(GetResourceAsImage("images/mandrill_128.png")); |
bsalomon | dfe3e50 | 2016-01-21 06:44:08 -0800 | [diff] [blame] | 72 | if (!image) { |
| 73 | return nullptr; |
| 74 | } |
reed | 5671c5b | 2016-03-09 14:47:34 -0800 | [diff] [blame] | 75 | return image->makeShader(SkShader::kRepeat_TileMode, SkShader::kRepeat_TileMode); |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 76 | } |
| 77 | |
reed | 57fa241 | 2016-01-07 17:56:33 -0800 | [diff] [blame] | 78 | static void sk_gm_get_shaders(SkTDArray<SkShader*>* array) { |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 79 | if (auto shader = sh_make_lineargradient0()) { |
| 80 | *array->append() = shader.release(); |
bsalomon | dfe3e50 | 2016-01-21 06:44:08 -0800 | [diff] [blame] | 81 | } |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 82 | if (auto shader = sh_make_lineargradient1()) { |
| 83 | *array->append() = shader.release(); |
bsalomon | dfe3e50 | 2016-01-21 06:44:08 -0800 | [diff] [blame] | 84 | } |
reed | 2ad1aa6 | 2016-03-09 09:50:50 -0800 | [diff] [blame] | 85 | if (auto shader = sh_make_image()) { |
| 86 | *array->append() = shader.release(); |
bsalomon | dfe3e50 | 2016-01-21 06:44:08 -0800 | [diff] [blame] | 87 | } |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 91 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 92 | static sk_sp<SkImageFilter> make_blur(float amount, sk_sp<SkImageFilter> input) { |
| 93 | return SkBlurImageFilter::Make(amount, amount, std::move(input)); |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 94 | } |
| 95 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 96 | static sk_sp<SkImageFilter> make_brightness(float amount, sk_sp<SkImageFilter> input) { |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 97 | return SkColorFilterImageFilter::Make(cf_make_brightness(amount), std::move(input)); |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 98 | } |
| 99 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 100 | static sk_sp<SkImageFilter> make_grayscale(sk_sp<SkImageFilter> input) { |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 101 | return SkColorFilterImageFilter::Make(cf_make_grayscale(), std::move(input)); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 102 | } |
| 103 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 104 | static sk_sp<SkImageFilter> make_mode_blue(sk_sp<SkImageFilter> input) { |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 105 | return SkColorFilterImageFilter::Make(cf_make_colorize(SK_ColorBLUE), std::move(input)); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 106 | } |
| 107 | |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 108 | static void draw_clipped_rect(SkCanvas* canvas, |
| 109 | const SkRect& r, |
| 110 | const SkPaint& paint, |
| 111 | float outset = 0.0f) { |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 112 | canvas->save(); |
| 113 | SkRect clip(r); |
| 114 | clip.outset(outset, outset); |
| 115 | canvas->clipRect(clip); |
| 116 | canvas->drawRect(r, paint); |
| 117 | canvas->restore(); |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 118 | } |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 119 | |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 120 | DEF_SIMPLE_GM(colorfilterimagefilter, canvas, 400, 100){ |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 121 | SkRect r = SkRect::MakeWH(FILTER_WIDTH, FILTER_HEIGHT); |
| 122 | SkPaint paint; |
| 123 | paint.setColor(SK_ColorRED); |
| 124 | canvas->save(); |
| 125 | for (float brightness = -1.0f; brightness <= 1.0f; brightness += 0.2f) { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 126 | sk_sp<SkImageFilter> dim(make_brightness(-brightness, nullptr)); |
| 127 | sk_sp<SkImageFilter> bright(make_brightness(brightness, std::move(dim))); |
| 128 | paint.setImageFilter(std::move(bright)); |
| 129 | draw_clipped_rect(canvas, r, paint); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 130 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 131 | } |
| 132 | canvas->restore(); |
| 133 | canvas->translate(0, FILTER_HEIGHT + MARGIN); |
| 134 | { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 135 | sk_sp<SkImageFilter> brightness(make_brightness(0.9f, nullptr)); |
| 136 | sk_sp<SkImageFilter> grayscale(make_grayscale(std::move(brightness))); |
| 137 | paint.setImageFilter(std::move(grayscale)); |
| 138 | draw_clipped_rect(canvas, r, paint); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 139 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 140 | } |
| 141 | { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 142 | sk_sp<SkImageFilter> grayscale(make_grayscale(nullptr)); |
| 143 | sk_sp<SkImageFilter> brightness(make_brightness(0.9f, std::move(grayscale))); |
| 144 | paint.setImageFilter(std::move(brightness)); |
| 145 | draw_clipped_rect(canvas, r, paint); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 146 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 147 | } |
| 148 | { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 149 | sk_sp<SkImageFilter> blue(make_mode_blue(nullptr)); |
| 150 | sk_sp<SkImageFilter> brightness(make_brightness(1.0f, std::move(blue))); |
| 151 | paint.setImageFilter(std::move(brightness)); |
| 152 | draw_clipped_rect(canvas, r, paint); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 153 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 154 | } |
| 155 | { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 156 | sk_sp<SkImageFilter> brightness(make_brightness(1.0f, nullptr)); |
| 157 | sk_sp<SkImageFilter> blue(make_mode_blue(std::move(brightness))); |
| 158 | paint.setImageFilter(std::move(blue)); |
| 159 | draw_clipped_rect(canvas, r, paint); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 160 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 161 | } |
| 162 | { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 163 | sk_sp<SkImageFilter> blur(make_blur(3.0f, nullptr)); |
| 164 | sk_sp<SkImageFilter> brightness(make_brightness(0.5f, std::move(blur))); |
| 165 | paint.setImageFilter(std::move(brightness)); |
| 166 | draw_clipped_rect(canvas, r, paint, 3); |
senorblanco@chromium.org | 8d21f6c | 2012-10-12 19:14:06 +0000 | [diff] [blame] | 167 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 168 | } |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 169 | { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 170 | sk_sp<SkImageFilter> blue(make_mode_blue(nullptr)); |
| 171 | paint.setImageFilter(std::move(blue)); |
| 172 | draw_clipped_rect(canvas, r, paint, 5); |
senorblanco | 0abdf76 | 2015-08-20 11:10:41 -0700 | [diff] [blame] | 173 | canvas->translate(FILTER_WIDTH + MARGIN, 0); |
| 174 | } |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 175 | } |
halcanary | 287d22d | 2015-09-24 10:20:05 -0700 | [diff] [blame] | 176 | |
| 177 | DEF_SIMPLE_GM(colorfilterimagefilter_layer, canvas, 32, 32) { |
| 178 | SkAutoCanvasRestore autoCanvasRestore(canvas, false); |
| 179 | SkColorMatrix cm; |
| 180 | cm.setSaturation(0.0f); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 181 | sk_sp<SkColorFilter> cf(SkColorFilter::MakeMatrixFilterRowMajor255(cm.fMat)); |
halcanary | 287d22d | 2015-09-24 10:20:05 -0700 | [diff] [blame] | 182 | SkPaint p; |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 183 | p.setImageFilter(SkColorFilterImageFilter::Make(std::move(cf), nullptr)); |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 184 | canvas->saveLayer(nullptr, &p); |
halcanary | 287d22d | 2015-09-24 10:20:05 -0700 | [diff] [blame] | 185 | canvas->clear(SK_ColorRED); |
| 186 | } |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 187 | |
| 188 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 189 | |
Mike Reed | a03d407 | 2017-06-01 09:37:57 -0400 | [diff] [blame] | 190 | #include "SkGradientShader.h" |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame^] | 191 | |
reed | 57fa241 | 2016-01-07 17:56:33 -0800 | [diff] [blame] | 192 | template <typename T> class SkTRefArray : public SkTDArray<T> { |
| 193 | public: |
| 194 | ~SkTRefArray() { this->unrefAll(); } |
| 195 | }; |
| 196 | |
Mike Reed | a03d407 | 2017-06-01 09:37:57 -0400 | [diff] [blame] | 197 | DEF_SIMPLE_GM(colorfiltershader, canvas, 610, 610) { |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 198 | SkTArray<sk_sp<SkColorFilter>> filters; |
reed | 57fa241 | 2016-01-07 17:56:33 -0800 | [diff] [blame] | 199 | sk_gm_get_colorfilters(&filters); |
| 200 | |
| 201 | SkTRefArray<SkShader*> shaders; |
| 202 | sk_gm_get_shaders(&shaders); |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 203 | |
Mike Reed | a03d407 | 2017-06-01 09:37:57 -0400 | [diff] [blame] | 204 | const SkColor colors[] = { SK_ColorRED, SK_ColorBLUE }; |
| 205 | *shaders.append() = SkGradientShader::MakeTwoPointConical({0, 0}, 50, {0, 0}, 150, |
| 206 | colors, nullptr, 2, |
| 207 | SkShader::kClamp_TileMode).release(); |
| 208 | |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 209 | SkPaint paint; |
| 210 | SkRect r = SkRect::MakeWH(120, 120); |
| 211 | |
| 212 | canvas->translate(20, 20); |
| 213 | for (int y = 0; y < shaders.count(); ++y) { |
| 214 | SkShader* shader = shaders[y]; |
bsalomon | dfe3e50 | 2016-01-21 06:44:08 -0800 | [diff] [blame] | 215 | |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 216 | canvas->save(); |
| 217 | for (int x = -1; x < filters.count(); ++x) { |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 218 | sk_sp<SkColorFilter> filter = x >= 0 ? filters[x] : nullptr; |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 219 | |
reed | 150835e | 2016-03-10 06:36:49 -0800 | [diff] [blame] | 220 | paint.setShader(shader->makeWithColorFilter(filter)); |
reed | 3061af4 | 2016-01-07 15:47:29 -0800 | [diff] [blame] | 221 | canvas->drawRect(r, paint); |
| 222 | canvas->translate(150, 0); |
| 223 | } |
| 224 | canvas->restore(); |
| 225 | canvas->translate(0, 150); |
| 226 | } |
| 227 | } |
Mike Reed | 01b2b83 | 2017-06-09 10:51:52 -0400 | [diff] [blame] | 228 | |
| 229 | DEF_SIMPLE_GM(mixershader, canvas, 800, 700) { |
Hal Canary | c465d13 | 2017-12-08 10:21:31 -0500 | [diff] [blame] | 230 | auto shaderA = GetResourceAsImage("images/mandrill_128.png")->makeShader(SkShader::kClamp_TileMode, |
Mike Reed | 01b2b83 | 2017-06-09 10:51:52 -0400 | [diff] [blame] | 231 | SkShader::kClamp_TileMode); |
| 232 | const SkColor colors[] = { SK_ColorGREEN, 0 }; |
| 233 | auto shaderB = SkGradientShader::MakeRadial({60, 60}, 55, colors, nullptr, 2, |
| 234 | SkShader::kClamp_TileMode, |
| 235 | SkGradientShader::kInterpolateColorsInPremul_Flag, |
| 236 | nullptr); |
| 237 | const SkBlendMode modes[] = { |
| 238 | SkBlendMode::kSrc, SkBlendMode::kModulate, SkBlendMode::kColorBurn, SkBlendMode::kPlus, |
| 239 | SkBlendMode::kDstATop, |
| 240 | }; |
| 241 | SkPaint paint; |
| 242 | SkRect r = SkRect::MakeWH(120, 120); |
| 243 | |
| 244 | canvas->translate(10, 10); |
| 245 | for (auto mode : modes) { |
| 246 | canvas->save(); |
| 247 | const int count = 6; |
| 248 | for (int x = 0; x < count; ++x) { |
| 249 | const float t = x * 1.0f / (count - 1); |
| 250 | paint.setShader(SkShader::MakeCompose(shaderA, shaderB, mode, t)); |
| 251 | canvas->drawRect(r, paint); |
| 252 | canvas->translate(r.width() + 10, 0); |
| 253 | } |
| 254 | canvas->restore(); |
| 255 | canvas->translate(0, r.height() + 20); |
| 256 | } |
| 257 | } |