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