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