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