Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkFont.h" |
| 13 | #include "include/core/SkPaint.h" |
| 14 | #include "include/core/SkTypeface.h" |
| 15 | #include "include/core/SkTypes.h" |
Mike Reed | bd1db41 | 2020-03-21 10:27:53 -0400 | [diff] [blame] | 16 | #include "tools/Resources.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "tools/ToolUtils.h" |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 18 | |
| 19 | // Hue, Saturation, Color, and Luminosity blend modes are oddballs. |
| 20 | // They nominally convert their inputs to unpremul, then to HSL, then |
| 21 | // mix-and-match H,S,and L from Src and Dst, then convert back, then premul. |
| 22 | // |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 23 | // In practice that's slow, so instead we pick the color with the correct |
| 24 | // Hue, and then (approximately) apply the other's Saturation and/or Luminosity. |
| 25 | // This isn't just an optimization... it's how the modes are specified. |
| 26 | // |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 27 | // Each mode's name describes the Src H,S,L components to keep, taking the |
| 28 | // others from Dst, where Color == Hue + Saturation. Color and Luminosity |
| 29 | // are each other's complements; Hue and Saturation have no complement. |
| 30 | // |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 31 | // All these modes were originally designed to operate on gamma-encoded values, |
| 32 | // and that's what everyone's used to seeing. It's unclear wehther they make |
| 33 | // any sense in a gamma-correct world. They certainly won't look at all similar. |
| 34 | // |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 35 | // We have had many inconsistent implementations of these modes. |
| 36 | // This GM tries to demonstrate unambigously how they should work. |
| 37 | // |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 38 | // To go along with our inconsistent implementations, there are two slightly |
| 39 | // inconsistent specifications of how to perform these blends, |
| 40 | // web: https://www.w3.org/TR/compositing-1/#blendingnonseparable |
| 41 | // KHR: https://www.khronos.org/registry/OpenGL/extensions/KHR/KHR_blend_equation_advanced.txt |
| 42 | // It looks like these are meant to be identical, but they disagree on at least ClipColor(). |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 43 | // |
Mike Klein | ba8ca0a | 2017-05-07 00:25:16 -0400 | [diff] [blame] | 44 | // I think the KHR version is just wrong... it produces values >1. So we use the web version. |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 45 | |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 46 | static float min(float r, float g, float b) { return std::min(r, std::min(g, b)); } |
| 47 | static float max(float r, float g, float b) { return std::max(r, std::max(g, b)); } |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 48 | |
| 49 | static float sat(float r, float g, float b) { return max(r,g,b) - min(r,g,b); } |
| 50 | static float lum(float r, float g, float b) { return r*0.30f + g*0.59f + b*0.11f; } |
| 51 | |
| 52 | // The two SetSat() routines in the specs look different, but they're logically equivalent. |
| 53 | // Both map the minimum channel to 0, maximum to s, and scale the middle proportionately. |
| 54 | // The KHR version has done a better job at simplifying its math, so we use it here. |
| 55 | static void set_sat(float* r, float* g, float* b, float s) { |
Mike Klein | bc63fd4 | 2017-05-06 23:52:28 -0400 | [diff] [blame] | 56 | float mn = min(*r,*g,*b), |
| 57 | mx = max(*r,*g,*b); |
| 58 | auto channel = [=](float c) { |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 59 | return mx == mn ? 0 |
| 60 | : (c - mn) * s / (mx - mn); |
| 61 | }; |
| 62 | *r = channel(*r); |
| 63 | *g = channel(*g); |
| 64 | *b = channel(*b); |
| 65 | } |
Mike Klein | ba8ca0a | 2017-05-07 00:25:16 -0400 | [diff] [blame] | 66 | static void clip_color(float* r, float* g, float* b) { |
Mike Klein | bc63fd4 | 2017-05-06 23:52:28 -0400 | [diff] [blame] | 67 | float l = lum(*r,*g,*b), |
| 68 | mn = min(*r,*g,*b), |
| 69 | mx = max(*r,*g,*b); |
| 70 | auto clip = [=](float c) { |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 71 | if (mn < 0) { c = l + (c - l) * ( l) / (l - mn); } |
Mike Klein | ba8ca0a | 2017-05-07 00:25:16 -0400 | [diff] [blame] | 72 | if (mx > 1) { c = l + (c - l) * (1 - l) / (mx - l); } |
| 73 | SkASSERT(-0.0001f < c); // This may end up very slightly negative... |
| 74 | SkASSERT( c <= 1); |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 75 | return c; |
| 76 | }; |
| 77 | *r = clip(*r); |
| 78 | *g = clip(*g); |
| 79 | *b = clip(*b); |
| 80 | } |
Mike Klein | ba8ca0a | 2017-05-07 00:25:16 -0400 | [diff] [blame] | 81 | static void set_lum(float* r, float* g, float* b, float l) { |
| 82 | float diff = l - lum(*r,*g,*b); |
| 83 | *r += diff; |
| 84 | *g += diff; |
| 85 | *b += diff; |
| 86 | clip_color(r,g,b); |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | |
| 90 | static void hue(float dr, float dg, float db, |
| 91 | float* sr, float* sg, float* sb) { |
| 92 | // Hue of Src, Saturation and Luminosity of Dst. |
| 93 | float R = *sr, |
| 94 | G = *sg, |
| 95 | B = *sb; |
| 96 | set_sat(&R,&G,&B, sat(dr,dg,db)); |
| 97 | set_lum(&R,&G,&B, lum(dr,dg,db)); |
| 98 | *sr = R; |
| 99 | *sg = G; |
| 100 | *sb = B; |
| 101 | } |
| 102 | |
| 103 | static void saturation(float dr, float dg, float db, |
| 104 | float* sr, float* sg, float* sb) { |
| 105 | // Saturation of Src, Hue and Luminosity of Dst |
| 106 | float R = dr, |
| 107 | G = dg, |
| 108 | B = db; |
| 109 | set_sat(&R,&G,&B, sat(*sr,*sg,*sb)); |
Mike Klein | ba8ca0a | 2017-05-07 00:25:16 -0400 | [diff] [blame] | 110 | set_lum(&R,&G,&B, lum( dr, dg, db)); // This may seem redundant, but it is not. |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 111 | *sr = R; |
| 112 | *sg = G; |
| 113 | *sb = B; |
| 114 | } |
| 115 | |
| 116 | static void color(float dr, float dg, float db, |
| 117 | float* sr, float* sg, float* sb) { |
| 118 | // Hue and Saturation of Src, Luminosity of Dst. |
| 119 | float R = *sr, |
| 120 | G = *sg, |
| 121 | B = *sb; |
| 122 | set_lum(&R,&G,&B, lum(dr,dg,db)); |
| 123 | *sr = R; |
| 124 | *sg = G; |
| 125 | *sb = B; |
| 126 | } |
| 127 | |
| 128 | static void luminosity(float dr, float dg, float db, |
| 129 | float* sr, float* sg, float* sb) { |
| 130 | // Luminosity of Src, Hue and Saturation of Dst. |
| 131 | float R = dr, |
| 132 | G = dg, |
| 133 | B = db; |
| 134 | set_lum(&R,&G,&B, lum(*sr,*sg,*sb)); |
| 135 | *sr = R; |
| 136 | *sg = G; |
| 137 | *sb = B; |
| 138 | } |
| 139 | |
| 140 | static SkColor blend(SkColor dst, SkColor src, |
Brian Osman | fbc6d7f | 2018-08-15 11:16:06 -0400 | [diff] [blame] | 141 | void (*mode)(float,float,float, float*,float*,float*)) { |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 142 | |
| 143 | SkASSERT(SkColorGetA(dst) == 0xff |
| 144 | && SkColorGetA(src) == 0xff); // Not fundamental, just simplifying for this GM. |
| 145 | |
Brian Osman | fbc6d7f | 2018-08-15 11:16:06 -0400 | [diff] [blame] | 146 | SkColor4f d = SkColor4f::FromColor(dst), |
| 147 | s = SkColor4f::FromColor(src); |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 148 | |
Mike Klein | f484ac6 | 2017-05-06 15:44:38 -0400 | [diff] [blame] | 149 | mode( d.fR, d.fG, d.fB, |
| 150 | &s.fR, &s.fG, &s.fB); |
Mike Klein | f484ac6 | 2017-05-06 15:44:38 -0400 | [diff] [blame] | 151 | |
Mike Klein | f484ac6 | 2017-05-06 15:44:38 -0400 | [diff] [blame] | 152 | return s.toSkColor(); |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 153 | } |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 154 | |
| 155 | DEF_SIMPLE_GM(hsl, canvas, 600, 100) { |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 156 | SkPaint paint; |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 157 | SkFont font(ToolUtils::create_portable_typeface()); |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 158 | |
Mike Klein | ba8ca0a | 2017-05-07 00:25:16 -0400 | [diff] [blame] | 159 | const char* comment = "HSL blend modes are correct when you see no circles in the squares."; |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 160 | canvas->drawString(comment, 10,10, font, paint); |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 161 | |
| 162 | // Just to keep things reaaaal simple, we'll only use opaque colors. |
| 163 | SkPaint bg, fg; |
| 164 | bg.setColor(0xff00ff00); // Fully-saturated bright green, H = 120°, S = 100%, L = 50%. |
| 165 | fg.setColor(0xff7f3f7f); // Partly-saturated dim magenta, H = 300°, S = ~33%, L = ~37%. |
| 166 | |
| 167 | struct { |
| 168 | SkBlendMode mode; |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 169 | void (*reference)(float,float,float, float*,float*,float*); |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 170 | } tests[] = { |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 171 | { SkBlendMode::kSrc, nullptr }, |
| 172 | { SkBlendMode::kDst, nullptr }, |
| 173 | { SkBlendMode::kHue, hue }, |
| 174 | { SkBlendMode::kSaturation, saturation }, |
| 175 | { SkBlendMode::kColor, color }, |
| 176 | { SkBlendMode::kLuminosity, luminosity }, |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 177 | }; |
| 178 | for (auto test : tests) { |
| 179 | canvas->drawRect({20,20,80,80}, bg); |
| 180 | |
| 181 | fg.setBlendMode(test.mode); |
| 182 | canvas->drawRect({20,20,80,80}, fg); |
| 183 | |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 184 | if (test.reference) { |
Mike Klein | ba8ca0a | 2017-05-07 00:25:16 -0400 | [diff] [blame] | 185 | SkPaint ref; |
Brian Osman | fbc6d7f | 2018-08-15 11:16:06 -0400 | [diff] [blame] | 186 | ref.setColor(blend(bg.getColor(), fg.getColor(), test.reference)); |
Mike Klein | ba8ca0a | 2017-05-07 00:25:16 -0400 | [diff] [blame] | 187 | canvas->drawCircle(50,50, 20, ref); |
Mike Klein | 5b4e277 | 2017-05-06 14:01:22 -0400 | [diff] [blame] | 188 | } |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 189 | |
Hal Canary | df2d27e | 2019-01-08 09:38:02 -0500 | [diff] [blame] | 190 | canvas->drawString(SkBlendMode_Name(test.mode), 20, 90, font, paint); |
Mike Klein | 6bfe3f5 | 2017-05-05 13:49:00 -0400 | [diff] [blame] | 191 | |
| 192 | canvas->translate(100,0); |
| 193 | } |
| 194 | } |
Mike Reed | bd1db41 | 2020-03-21 10:27:53 -0400 | [diff] [blame] | 195 | |
| 196 | #include "include/effects/SkGradientShader.h" |
| 197 | |
| 198 | // Trying to match sample images on https://www.w3.org/TR/compositing-1/#blendingnonseparable |
| 199 | // |
| 200 | static sk_sp<SkShader> make_grad(SkScalar width) { |
| 201 | SkColor colors[] = { |
| 202 | 0xFF00CCCC, 0xFF0000CC, 0xFFCC00CC, 0xFFCC0000, 0xFFCCCC00, 0xFF00CC00, |
| 203 | }; |
| 204 | SkPoint pts[] = {{0, 0}, {width, 0}}; |
| 205 | |
| 206 | return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
| 207 | SkTileMode::kClamp); |
| 208 | } |
| 209 | |
| 210 | DEF_SIMPLE_GM(HSL_duck, canvas, 1110, 620) { |
Mike Reed | 6e9b179 | 2020-03-25 07:51:24 -0400 | [diff] [blame] | 211 | auto src = GetResourceAsImage("images/ducky.png"); |
Mike Reed | bd1db41 | 2020-03-21 10:27:53 -0400 | [diff] [blame] | 212 | auto dst = make_grad(src->width()); |
| 213 | SkRect r = SkRect::MakeIWH(src->width(), src->height()); |
| 214 | |
| 215 | canvas->translate(10, 50); |
| 216 | canvas->scale(0.5f, 0.5f); |
| 217 | |
| 218 | const struct { |
| 219 | SkBlendMode fMode; |
| 220 | const char* fName; |
| 221 | } recs[] = { |
| 222 | { SkBlendMode::kHue, "Hue" }, |
| 223 | { SkBlendMode::kSaturation, "Saturation" }, |
| 224 | { SkBlendMode::kColor, "Color" }, |
| 225 | { SkBlendMode::kLuminosity, "Luminosity" }, |
| 226 | }; |
| 227 | |
| 228 | SkFont font; |
| 229 | font.setSize(40); |
| 230 | font.setEdging(SkFont::Edging::kAntiAlias); |
| 231 | |
| 232 | canvas->save(); |
| 233 | for (auto [_, name] : recs) { |
| 234 | canvas->drawSimpleText(name, strlen(name), SkTextEncoding::kUTF8, 150, -20, |
| 235 | font, SkPaint()); |
| 236 | canvas->translate(r.width() + 10, 0); |
| 237 | } |
| 238 | canvas->restore(); |
| 239 | |
| 240 | for (SkScalar src_a : {1.0f, 0.5f}) { |
| 241 | canvas->save(); |
| 242 | for (auto [mode, _] : recs) { |
| 243 | SkPaint p; |
| 244 | p.setShader(dst); |
| 245 | canvas->drawRect(r, p); // bg |
| 246 | |
| 247 | p.setShader(nullptr); |
| 248 | p.setBlendMode(mode); |
| 249 | p.setAlphaf(src_a); |
| 250 | canvas->drawImageRect(src, r, &p); |
| 251 | |
| 252 | canvas->translate(r.width() + 10, 0); |
| 253 | } |
| 254 | SkString str; |
| 255 | str.printf("alpha %g", src_a); |
| 256 | canvas->drawSimpleText(str.c_str(), str.size(), SkTextEncoding::kUTF8, 10, r.height()/2, |
| 257 | font, SkPaint()); |
| 258 | |
| 259 | canvas->restore(); |
| 260 | canvas->translate(0, r.height() + 10); |
| 261 | } |
| 262 | } |