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