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