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