bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +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/SkBitmap.h" |
| 10 | #include "include/core/SkBlendMode.h" |
| 11 | #include "include/core/SkCanvas.h" |
| 12 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkColorFilter.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #include "include/core/SkPaint.h" |
| 15 | #include "include/core/SkPoint.h" |
| 16 | #include "include/core/SkRect.h" |
| 17 | #include "include/core/SkRefCnt.h" |
| 18 | #include "include/core/SkScalar.h" |
| 19 | #include "include/core/SkShader.h" |
| 20 | #include "include/core/SkSize.h" |
| 21 | #include "include/core/SkString.h" |
| 22 | #include "include/core/SkTileMode.h" |
| 23 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "include/effects/SkGradientShader.h" |
| 25 | #include "tools/ToolUtils.h" |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 26 | |
| 27 | #define WIDTH 512 |
| 28 | #define HEIGHT 1024 |
| 29 | |
| 30 | namespace skiagm { |
| 31 | |
| 32 | // Using gradients because GPU doesn't currently have an implementation of SkColorShader (duh!) |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 33 | static sk_sp<SkShader> make_color_shader(SkColor color) { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 34 | constexpr SkPoint kPts[] = {{0, 0}, {1, 1}}; |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 35 | SkColor colors[] = {color, color}; |
| 36 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 37 | return SkGradientShader::MakeLinear(kPts, colors, nullptr, 2, SkTileMode::kClamp); |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 38 | } |
| 39 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 40 | static sk_sp<SkShader> make_solid_shader() { |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 41 | return make_color_shader(SkColorSetARGB(0xFF, 0x42, 0x82, 0x21)); |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 42 | } |
| 43 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 44 | static sk_sp<SkShader> make_transparent_shader() { |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 45 | return make_color_shader(SkColorSetARGB(0x80, 0x10, 0x70, 0x20)); |
| 46 | } |
| 47 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 48 | static sk_sp<SkShader> make_trans_black_shader() { |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 49 | return make_color_shader(0x0); |
| 50 | } |
| 51 | |
| 52 | // draws a background behind each test rect to see transparency |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 53 | static sk_sp<SkShader> make_bg_shader(int checkSize) { |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 54 | SkBitmap bmp; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 55 | bmp.allocN32Pixels(2 * checkSize, 2 * checkSize); |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 56 | SkCanvas canvas(bmp); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 57 | canvas.clear(ToolUtils::color_to_565(0xFF800000)); |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 58 | SkPaint paint; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 59 | paint.setColor(ToolUtils::color_to_565(0xFF000080)); |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 60 | SkRect rect0 = SkRect::MakeXYWH(0, 0, |
| 61 | SkIntToScalar(checkSize), SkIntToScalar(checkSize)); |
| 62 | SkRect rect1 = SkRect::MakeXYWH(SkIntToScalar(checkSize), SkIntToScalar(checkSize), |
| 63 | SkIntToScalar(checkSize), SkIntToScalar(checkSize)); |
| 64 | canvas.drawRect(rect1, paint); |
| 65 | canvas.drawRect(rect0, paint); |
Mike Reed | 82abece | 2020-12-12 09:51:11 -0500 | [diff] [blame] | 66 | return bmp.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, SkSamplingOptions()); |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | class ModeColorFilterGM : public GM { |
| 70 | public: |
| 71 | ModeColorFilterGM() { |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 72 | this->setBGColor(0xFF303030); |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | protected: |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 76 | SkString onShortName() override { |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 77 | return SkString("modecolorfilters"); |
| 78 | } |
| 79 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 80 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 81 | return SkISize::Make(WIDTH, HEIGHT); |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 82 | } |
| 83 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 84 | void onDraw(SkCanvas* canvas) override { |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 85 | // size of rect for each test case |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 86 | constexpr int kRectWidth = 20; |
| 87 | constexpr int kRectHeight = 20; |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 88 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 89 | constexpr int kCheckSize = 10; |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 90 | |
| 91 | if (!fBmpShader) { |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 92 | fBmpShader = make_bg_shader(kCheckSize); |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 93 | } |
| 94 | SkPaint bgPaint; |
| 95 | bgPaint.setShader(fBmpShader); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 96 | bgPaint.setBlendMode(SkBlendMode::kSrc); |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 97 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 98 | sk_sp<SkShader> shaders[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 99 | nullptr, // use a paint color instead of a shader |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 100 | make_solid_shader(), |
| 101 | make_transparent_shader(), |
| 102 | make_trans_black_shader(), |
| 103 | }; |
| 104 | |
| 105 | // used without shader |
| 106 | SkColor colors[] = { |
| 107 | SkColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF), |
| 108 | SkColorSetARGB(0xFF, 0x00, 0x00, 0x00), |
| 109 | SkColorSetARGB(0x00, 0x00, 0x00, 0x00), |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 110 | SkColorSetARGB(0xFF, 0x10, 0x20, 0x42), |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 111 | SkColorSetARGB(0xA0, 0x20, 0x30, 0x90), |
| 112 | }; |
| 113 | |
| 114 | // used with shaders |
| 115 | SkColor alphas[] = {0xFFFFFFFF, 0x80808080}; |
skia.committer@gmail.com | 24c29d9 | 2012-10-20 02:01:23 +0000 | [diff] [blame] | 116 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 117 | const SkBlendMode modes[] = { // currently just doing the Modes expressible as Coeffs |
| 118 | SkBlendMode::kClear, |
| 119 | SkBlendMode::kSrc, |
| 120 | SkBlendMode::kDst, |
| 121 | SkBlendMode::kSrcOver, |
| 122 | SkBlendMode::kDstOver, |
| 123 | SkBlendMode::kSrcIn, |
| 124 | SkBlendMode::kDstIn, |
| 125 | SkBlendMode::kSrcOut, |
| 126 | SkBlendMode::kDstOut, |
| 127 | SkBlendMode::kSrcATop, |
| 128 | SkBlendMode::kDstATop, |
| 129 | SkBlendMode::kXor, |
| 130 | SkBlendMode::kPlus, |
| 131 | SkBlendMode::kModulate, |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | SkPaint paint; |
| 135 | int idx = 0; |
Brian Osman | 7f36405 | 2020-02-06 11:25:43 -0500 | [diff] [blame] | 136 | const int kRectsPerRow = std::max(this->getISize().fWidth / kRectWidth, 1); |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 137 | for (size_t cfm = 0; cfm < SK_ARRAY_COUNT(modes); ++cfm) { |
robertphillips@google.com | 93f0332 | 2012-12-03 17:35:19 +0000 | [diff] [blame] | 138 | for (size_t cfc = 0; cfc < SK_ARRAY_COUNT(colors); ++cfc) { |
Mike Reed | b286bc2 | 2019-04-08 16:23:20 -0400 | [diff] [blame] | 139 | paint.setColorFilter(SkColorFilters::Blend(colors[cfc], modes[cfm])); |
robertphillips@google.com | 93f0332 | 2012-12-03 17:35:19 +0000 | [diff] [blame] | 140 | for (size_t s = 0; s < SK_ARRAY_COUNT(shaders); ++s) { |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 141 | paint.setShader(shaders[s]); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 142 | bool hasShader = nullptr == paint.getShader(); |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 143 | int paintColorCnt = hasShader ? SK_ARRAY_COUNT(alphas) : SK_ARRAY_COUNT(colors); |
| 144 | SkColor* paintColors = hasShader ? alphas : colors; |
| 145 | for (int pc = 0; pc < paintColorCnt; ++pc) { |
| 146 | paint.setColor(paintColors[pc]); |
| 147 | SkScalar x = SkIntToScalar(idx % kRectsPerRow); |
| 148 | SkScalar y = SkIntToScalar(idx / kRectsPerRow); |
| 149 | SkRect rect = SkRect::MakeXYWH(x * kRectWidth, y * kRectHeight, |
skia.committer@gmail.com | 306ab9d | 2012-12-13 02:01:33 +0000 | [diff] [blame] | 150 | SkIntToScalar(kRectWidth), |
robertphillips@google.com | ca47aae | 2012-12-12 15:58:25 +0000 | [diff] [blame] | 151 | SkIntToScalar(kRectHeight)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 152 | canvas->saveLayer(&rect, nullptr); |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 153 | canvas->drawRect(rect, bgPaint); |
| 154 | canvas->drawRect(rect, paint); |
vandebo@chromium.org | 8d8d9a5 | 2013-03-14 17:24:16 +0000 | [diff] [blame] | 155 | canvas->restore(); |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 156 | ++idx; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | private: |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 164 | sk_sp<SkShader> fBmpShader; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 165 | using INHERITED = GM; |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | ////////////////////////////////////////////////////////////////////////////// |
| 169 | |
Hal Canary | e964c18 | 2019-01-23 10:22:01 -0500 | [diff] [blame] | 170 | DEF_GM( return new ModeColorFilterGM; ) |
bsalomon@google.com | c6126c1 | 2012-10-19 19:26:05 +0000 | [diff] [blame] | 171 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 172 | } // namespace skiagm |