Implement gamma-correct blending
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Implement gamma-correct blending by decoding from sRGB to linear, and then using the color-management protocol to let the compositor handle the final encoding. Since linear colors require more bits to achieve the same precision as sRGB encoded colors, we use 16-bit surfaces. This has a couple of implications: * We require pixman >= 0.46, since that's where PIXMAN_a16b16g16r16 was added (which is what we need, since it maps to WL_SHM_FORMAT_ABGR161616). * Gamma-correct blending is *not* supported, at all, in cairo-enabled builds of fuzzel, since cairo doesn't have any 16-bit surfaces. Note: we _could_ use 10-bit surfaces, but: * While better than 8-bit, they don't really have enough precision (I believe we need at least 12 bits) * They only have 2 bits of alpha, which _really_ isn't enough. We also require fcft >= 3.3.1, to be able to use 16-bit surfaces for color glyphs (again, required to not loose color precision). Icons: * For PNGs, we can offload everything to libpng; that is, we can tell libpng to: a) decode from sRGB, and *keep* it decoded in the output buffer b) use 16-bit output c) do alpha pre-multiplication for us (unfortunately, I haven't been able to get it to do this while also re-encoding to sRGB, for when gamma-correct blending is disabled) * For SVGs, we do nothing when librsvg is used as backend (this happens automatically, since librsvg requires cairo, and since we always disable gamma-correct blending in cairo-enabled builds). With nanosvg there's unfortunately no way to tell it to decode to linear. The best we can do is take the rasterized image and convert to linear when nanosvg is done with it. Gamma-correct blending is disabled by default, and can be enabled with either the 'gamma-correct-blending=no|yes' option in fuzzel.ini, or the --gamma-correct command line option (assuming the compositor implements the necessary pieces of the color-management protocol). Closes #546
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
21 files+593 added−95 removed
- other:+593 added−95 removed
Provenance
- Commit
2d19826- Tree
7d20f49- Author
Daniel Eklöf <daniel@ekloef.se>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent
468ae06
21 files changed+593 added−95 removed
modified:completions/fish/fuzzel.fish+1 added
15 unmodified lines16 complete -c fuzzel -x -s f -l font -a "(fc-list : family | sed 's/,/\n/g' | sort | uniq)" -d "font name and style in fontconfig format (monospace)"17 complete -c fuzzel -l use-bold -d "allow fuzzel to use bold fonts"18 complete -c fuzzel -x -s o -l output -a "(__fish_complete_fuzzel_output)" -d "output (monitor) do display on (none)"19 complete -c fuzzel -x -s D -l dpi-aware -a "no yes auto" -d "scale fonts using the monitor's DPI (auto)"20+complete -c fuzzel -l gamma-correct -d "do gamma-correct blending"21 complete -c fuzzel -x -l icon-theme -a "(find /usr/share/icons -mindepth 1 -maxdepth 1 -type d -print0 | xargs -0 -n 1 basename | sort)" -d "icon theme name (hicolor)"22 complete -c fuzzel -s I -l no-icons -d "do not render any icons"23 complete -c fuzzel -l hide-before-typing -d "hide application list until something is typed"24 complete -c fuzzel -x -s F -l fields -a "filename name generic exec categories keywords comment" -d "comma separated list of XDG Desktop entry fields to match"59 unmodified linesmodified:completions/zsh/_fuzzel+1 added
9 unmodified lines10 '(-h --help)'{-h,--help}'[show help message and quit]' \11 '(-o --output)'{-o,--output}'[output (monitor) to display on (none)]:output:->outputs' \12 '(-f --font)'{-f,--font}'[font name and style in fontconfig format (monospace)]:font:->fonts' \13 '(-D --dpi-aware)'{-D,--dpi-aware}"[scale fonts using the monitor's DPI (auto)]:dpi_aware:(no yes auto)" \14+ '--gamma-correct[do gamma-correct blending]' \15 '--use-bold[allow fuzzel to use bold fonts]' \16 '--icon-theme[icon theme name (hicolor)]:theme:->icon_theme' \17 '(-I --no-icons)'{-I,--no-icons}'[do not render any icons]' \18 '--hide-before-typing[hide application list until something is typed]' \75 unmodified linesmodified:doc/fuzzel.1.scd+30 added
92 unmodified lines93 multiplied with the scaling factor.94 95 Default: _auto_96 97+*--gamma-correct*98+ Do gamma-correct blending in linear color space. This is how font99+ glyphs are supposed to be rendered, but since nearly no100+ applications or toolkits are doing it on Linux, the result may not101+ look like you are used to.102+ 103+ Compared to the default (disabled), bright glyphs on a dark104+ background will appear thicker, and dark glyphs on a light105+ background will appear thinner.106+ 107+ FreeType can limit the effect of the latter, with a technique108+ called stem darkening. It is only available for CFF fonts109+ (OpenType, .otf) and disabled by default (in FreeType). You can110+ enable it by setting the environment variable111+ *FREETYPE_PROPERTIES="cff:no-stem-darkening=0"* before starting112+ fuzzel.113+ 114+ Also be aware that many fonts have been developed on systems that115+ do not do gamma-correct blending, and may therefore look thicker116+ than intended when rendered with gamma-correct blending, since the117+ font designer set the font weight based on incorrect rendering.118+ 119+ In order to represent colors faithfully, higher precision image120+ buffers are required. For this reason, fuzzel uses 16-bit image121+ buffers (instead of 8-bit) when gamma-correct blending is enabled.122+ 123+ It is not possible to enable gamma-correct blending in cairo124+ enabled builds of fuzzel, since cairo does not support 16-bit125+ image buffers.126+127 *-p*,*--prompt*=_PROMPT_128 Prompt to use. Default: _> _.129 130 *--prompt-only*=_PROMPT_407 unmodified linesmodified:doc/fuzzel.ini.5.scd+30 added
104 unmodified lines105 multiplied with the scaling factor.106 107 Default: _auto_.108 109+*gamma-correct*110+ Boolean. When enabled, fuzzel will do gamma-correct blending in111+ linear color space. This is how font glyphs are supposed to be112+ rendered, but since nearly no applications or toolkits are doing113+ it on Linux, the result may not look like you are used to.114+ 115+ Compared to the default (disabled), bright glyphs on a dark116+ background will appear thicker, and dark glyphs on a light117+ background will appear thinner.118+ 119+ FreeType can limit the effect of the latter, with a technique120+ called stem darkening. It is only available for CFF fonts121+ (OpenType, .otf) and disabled by default (in FreeType). You can122+ enable it by setting the environment variable123+ *FREETYPE_PROPERTIES="cff:no-stem-darkening=0"* before starting124+ fuzzel.125+ 126+ Also be aware that many fonts have been developed on systems that127+ do not do gamma-correct blending, and may therefore look thicker128+ than intended when rendered with gamma-correct blending, since the129+ font designer set the font weight based on incorrect rendering.130+ 131+ In order to represent colors faithfully, higher precision image132+ buffers are required. For this reason, fuzzel uses 16-bit image133+ buffers (instead of 8-bit) when gamma-correct blending is enabled.134+ 135+ It is not possible to enable gamma-correct blending in cairo136+ enabled builds of fuzzel, since cairo does not support 16-bit137+ image buffers.138+139 *placeholder*140 Text to display as placeholder in the input box. Default: empty.141 142 *prompt*470 unmodified linesmodified:CHANGELOG.md+10 added
33 unmodified lines34 35 * Add support for fallback icons in dmenu mode using comma-separated36 values in the icon metadata ([#504][504]).37 * `--namespace` command line option ([#512][512]).38+* Gamma-correct blending. Disabled by default. Enable with the new39+ `--gamma-correct` command line option, or by setting40+ `gamma-correct-blending=yes` in `fuzzel.ini`. Note that41+ gamma-correct blending **is not supported in cairo enabled builds of42+ fuzzel**, due to the lack of 16-bit image buffers in cairo43+ ([#546][546]).44 45 [504]: https://codeberg.org/dnkl/fuzzel/pulls/50446 [512]: https://codeberg.org/dnkl/fuzzel/pulls/51247+[546]: https://codeberg.org/dnkl/fuzzel/issues/54648 49 50 ### Changed51 52 * Number of render workers is now automatically limited to the number53 of displayed entries.54+* wayland-protocols >= 1.41 is now required.55+* pixman >= 0.46.0 is now required.56+* fcft >= 3.3.1 is now required.57 58 59 ### Deprecated60 ### Removed920 unmodified linesmodified:config.c+4 added
779 unmodified lines780 }781 return true;782 }783 784+ else if (strcmp(key, "gamma-correct-blending") == 0)785+ return value_to_bool(ctx, &conf->gamma_correct);786+787 else if (strcmp(key, "render-workers") == 0)788 return value_to_uint16(ctx, 10, &conf->render_worker_count);789 790 else if (strcmp(key, "match-workers") == 0)816 unmodified lines1607 .launch_prefix = NULL,1608 .font = xstrdup("monospace"),1609 .use_bold = false,1610 .dpi_aware = DPI_AWARE_AUTO,1611+ .gamma_correct = false,1612 .render_worker_count = sysconf(_SC_NPROCESSORS_ONLN),1613 .match_worker_count = sysconf(_SC_NPROCESSORS_ONLN),1614 .icons_enabled = true,1615 .icon_theme = xstrdup("hicolor"),139 unmodified linesmodified:config.h+1 added
117 unmodified lines118 119 char *font;120 bool use_bold;121 enum dpi_aware dpi_aware;122+ bool gamma_correct;123 124 uint16_t render_worker_count;125 uint16_t match_worker_count;126 90 unmodified linesmodified:fuzzel.ini+1 added
30 unmodified lines31 # inner-pad=032 33 # image-size-ratio=0.534 35+# gamma-correct-blending=no36 # line-height=<use font metrics>37 # letter-spacing=038 39 # layer=overlay79 unmodified linesmodified:icon.c+4 added-4 removed
349 unmodified lines350 }351 352 #if defined(FUZZEL_ENABLE_PNG_LIBPNG)353 static bool354-icon_from_png_libpng(struct icon *icon, const char *file_name)354+icon_from_png_libpng(struct icon *icon, const char *file_name, bool gamma_correct)355 {356- pixman_image_t *png = png_load(file_name);356+ pixman_image_t *png = png_load(file_name, gamma_correct);357 if (png == NULL)358 return false;359 360 icon->type = ICON_PNG;2 unmodified lines363 }364 #endif365 366 bool367-icon_from_png(struct icon *icon, const char *name)367+icon_from_png(struct icon *icon, const char *name, bool gamma_correct)368 {369 #if defined(FUZZEL_ENABLE_PNG_LIBPNG)370- return icon_from_png_libpng(icon, name);370+ return icon_from_png_libpng(icon, name, gamma_correct);371 #else372 return false;373 #endif374 }437 unmodified linesmodified:icon.h+1 added-1 removed
33 unmodified lines34 bool icon_lookup_application_icons(35 icon_theme_list_t themes, int icon_size,36 struct application_list *applications);37 38-bool icon_from_png(struct icon *icon, const char *name);38+bool icon_from_png(struct icon *icon, const char *name, bool gamma_correct);39 bool icon_from_svg(struct icon *icon, const char *name);modified:main.c+12 added
753 unmodified lines754 #define OPT_COUNTER 296755 #define OPT_HIDE_WHEN_PROMPT_EMPTY 297756 #define OPT_DMENU_WITH_NTH 298757 #define OPT_DMENU_ACCEPT_NTH 299758+ #define OPT_GAMMA_CORRECT 300759 760 static const struct option longopts[] = {761 {"config", required_argument, 0, OPT_CONFIG},762 {"check-config", no_argument, 0, OPT_CHECK_CONFIG},2 unmodified lines765 {"output" , required_argument, 0, 'o'},766 {"font", required_argument, 0, 'f'},767 {"use-bold", no_argument, 0, OPT_USE_BOLD},768 {"dpi-aware", required_argument, 0, 'D'},769+ {"gamma-correct", no_argument, 0, OPT_GAMMA_CORRECT},770 {"icon-theme", required_argument, 0, OPT_ICON_THEME},771 {"no-icons", no_argument, 0, 'I'},772 {"hide-before-typing", no_argument, 0, OPT_HIDE_WHEN_PROMPT_EMPTY},773 {"fields", required_argument, 0, 'F'},75 unmodified lines849 struct {850 struct config conf;851 852 bool dpi_aware_set:1;853+ bool gamma_correct_set:1;854 bool match_fields_set:1;855 bool icons_disabled_set:1;856 bool hide_when_prompt_empty_set:1;857 bool anchor_set:1;112 unmodified lines970 }971 cmdline_overrides.dpi_aware_set = true;972 break;973 974+ case OPT_GAMMA_CORRECT:975+ cmdline_overrides.conf.gamma_correct = true;976+ cmdline_overrides.gamma_correct_set = true;977+ break;978+979 case 'i':980 /* Ignore (this flag means case insensitive search in981 * other fuzzel-like utilities) */982 break;799 unmodified lines1782 conf.icon_theme = xstrdup(cmdline_overrides.conf.icon_theme);1783 }1784 if (cmdline_overrides.dpi_aware_set)1785 conf.dpi_aware = cmdline_overrides.conf.dpi_aware;1786+ if (cmdline_overrides.gamma_correct_set)1787+ conf.gamma_correct = cmdline_overrides.conf.gamma_correct;1788 if (cmdline_overrides.match_fields_set)1789 conf.match_fields = cmdline_overrides.conf.match_fields;1790 if (cmdline_overrides.icons_disabled_set)1791 conf.icons_enabled = cmdline_overrides.conf.icons_enabled;210 unmodified lines2002 &conf, fdm, kb_manager, render, prompt, matches,2003 &font_reloaded, &ctx)) == NULL)2004 goto out;2005 2006+ render_initialize_colors(render, &conf, wayl_do_linear_blending(wayl));2007+2008 matches_set_wayland(matches, wayl);2009 ctx.wayl = wayl;2010 2011 /* Create thread that will populate the application list */89 unmodified linesmodified:meson.build+13 added-4 removed
54 unmodified lines55 56 math = cc.find_library('m')57 threads = [dependency('threads'), cc.find_library('stdthreads', required: false)]58 fontconfig = dependency('fontconfig')59-pixman = dependency('pixman-1')59+pixman = dependency('pixman-1', version: '>=0.46.0')60 cairo = dependency('cairo', required: get_option('enable-cairo'))61 libepoll = dependency('epoll-shim', required: false)62-wayland_protocols = dependency('wayland-protocols', version: '>=1.32')62+wayland_protocols = dependency('wayland-protocols', version: '>=1.41')63 wayland_client = dependency('wayland-client')64 wayland_cursor = dependency('wayland-cursor')65 xkb = dependency('xkbcommon')66 system_nanosvg = cc.find_library('nanosvg', required: get_option('system-nanosvg'))67 system_nanosvgrast = cc.find_library('nanosvgrast', required: get_option('system-nanosvg'))68 69 tllist = dependency('tllist', version: '>=1.0.1', fallback: 'tllist')70-fcft = dependency('fcft', version: ['>=3.0.0', '<4.0.0'], fallback: 'fcft')70+fcft = dependency('fcft', version: ['>=3.3.1', '<4.0.0'], fallback: 'fcft')71 72 if cairo.found()73 add_project_arguments('-DFUZZEL_ENABLE_CAIRO=1', language: 'c')74 endif50 unmodified lines125 wayland_protocols_datadir + '/unstable/tablet/tablet-unstable-v2.xml',126 wayland_protocols_datadir + '/stable/viewporter/viewporter.xml',127 wayland_protocols_datadir + '/staging/fractional-scale/fractional-scale-v1.xml',128 wayland_protocols_datadir / 'unstable/primary-selection/primary-selection-unstable-v1.xml',129+ wayland_protocols_datadir / 'staging/color-management/color-management-v1.xml',130 ]131 132 foreach prot : wl_proto_xml133 wl_proto_headers += custom_target(16 unmodified lines150 build_always_stale: true,151 output: 'version.h',152 command: [env, 'LC_ALL=C', generate_version_sh, meson.project_version(), '@CURRENT_SOURCE_DIR@', '@OUTPUT@'])153 154+python = find_program('python3', native: true)155+generate_srgb_funcs = files('srgb.py')156+srgb_funcs = custom_target(157+ 'generate_srgb_funcs',158+ output: ['srgb.c', 'srgb.h'],159+ command: [python, generate_srgb_funcs, '@OUTPUT0@', '@OUTPUT1@']160+)161+162 executable(163 'fuzzel',164 'application.c', 'application.h',165 'char32.c', 'char32.h',20 unmodified lines186 'wayland.c', 'wayland.h',187 'xdg.c', 'xdg.h',188 'xmalloc.c', 'xmalloc.h',189 'xsnprintf.c', 'xsnprintf.h',181- wl_proto_src + wl_proto_headers, version,190+ wl_proto_src + wl_proto_headers, version, srgb_funcs,191 dependencies: [math,192 threads,193 pixman,194 fontconfig,30 unmodified linesmodified:png-fuzzel.h+2 added-1 removed
1 #pragma once2 3 #if defined(FUZZEL_ENABLE_PNG_LIBPNG)4 5+#include <stdbool.h>6 #include <pixman.h>7 7-pixman_image_t *png_load(const char *path);8+pixman_image_t *png_load(const char *path, bool gamma_correct);9 10 #endif /* FUZZEL_ENABLE_PNG_LIBPNG */modified:png.c+63 added-28 removed
22 unmodified lines23 LOG_WARN("libpng: %s", warning_msg);24 }25 26 pixman_image_t *27-png_load(const char *path)27+png_load(const char *path, bool gamma_correct)28 {29 pixman_image_t *pix = NULL;30 31 FILE *fp = NULL;1 unmodified line33 png_infop info_ptr = NULL;34 png_bytepp row_pointers = NULL;35 uint8_t *image_data = NULL;36 37+ const pixman_format_code_t fmt_with_alpha =38+ gamma_correct ? PIXMAN_a16b16g16r16 : PIXMAN_a8b8g8r8;39+ const pixman_format_code_t fmt_without_alpha =40+ gamma_correct ? PIXMAN_a16b16g16r16 : PIXMAN_x8b8g8r8;41+42 /* open file and test for it being a png */43 if ((fp = fopen(path, "rbe")) == NULL) {44 //LOG_ERRNO("%s: failed to open", path);45 goto err;26 unmodified lines72 73 png_init_io(png_ptr, fp);74 png_set_sig_bytes(png_ptr, 8);75 76+ if (gamma_correct) {77+ /*78+ * 16-bit premultiplied linear pixels79+ */80+ png_set_alpha_mode(png_ptr, PNG_ALPHA_PREMULTIPLIED, PNG_DEFAULT_sRGB);81+ png_set_gamma(png_ptr, PNG_GAMMA_LINEAR, PNG_DEFAULT_sRGB);82+ png_set_expand_16(png_ptr);83+ } else {84+ /*85+ * 8-bit non-premultiplied sRGB pixels86+ *87+ * Setting alpha-mode == PREMULTIPLIED doesn't seem to work -88+ * may be that libpng doesn't re-encode to sRGB. So, tell89+ * libpng to generate non-premultiplied pixels, and then we90+ * premultiply them ourselves later91+ */92+ png_set_alpha_mode(png_ptr, PNG_ALPHA_PNG, PNG_DEFAULT_sRGB);93+ png_set_scale_16(png_ptr);94+ }95+96 /* Get meta data */97 png_read_info(png_ptr, info_ptr);98 int width = png_get_image_width(png_ptr, info_ptr);99 int height = png_get_image_height(png_ptr, info_ptr);4 unmodified lines104 LOG_DBG("%s: %dx%d@%hhubpp, %d channels", path, width, height, bit_depth, channels);105 106 png_set_packing(png_ptr);107 png_set_interlace_handling(png_ptr);83- png_set_strip_16(png_ptr); /* "pack" 16-bit colors to 8-bit */84- png_set_bgr(png_ptr);108 109 /* pixman expects pre-multiplied alpha */110 111 /* Tell libpng to expand to RGB(A) when necessary, and tell pixman8 unmodified lines120 if (bit_depth < 8)121 png_set_expand_gray_1_2_4_to_8(png_ptr);122 123 png_set_gray_to_rgb(png_ptr);124+125+ if (color_type == PNG_COLOR_TYPE_GRAY)126+ png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER);127+101- format = color_type == PNG_COLOR_TYPE_GRAY ? PIXMAN_r8g8b8 : PIXMAN_a8r8g8b8;128+ format = color_type == PNG_COLOR_TYPE_GRAY ? fmt_without_alpha : fmt_with_alpha;129 break;130 131 case PNG_COLOR_TYPE_PALETTE:132 LOG_DBG("%d-bit colormap%s", bit_depth,133 png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ? "+tRNS" : "");134 135 png_set_palette_to_rgb(png_ptr);136+137 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {138 png_set_tRNS_to_alpha(png_ptr);111- format = PIXMAN_a8r8g8b8;112- } else113- format = PIXMAN_r8g8b8;139+ format = fmt_with_alpha;140+ } else {141+ png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER);142+ format = fmt_without_alpha;143+ }144 break;145 146 case PNG_COLOR_TYPE_RGB:147 LOG_DBG("RGB");118- format = PIXMAN_r8g8b8;148+ png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER);149+ format = fmt_without_alpha;150 break;151 152 case PNG_COLOR_TYPE_RGBA:153 LOG_DBG("RGBA");123- format = PIXMAN_a8r8g8b8;154+ format = fmt_with_alpha;155 break;156 157 default:158 LOG_ERR("unhandled PNG color type: %d", color_type);14 unmodified lines173 row_pointers[i] = &image_data[i * stride];174 175 png_read_image(png_ptr, row_pointers);176 146- /* pixman expects pre-multiplied alpha */147- if (format == PIXMAN_a8r8g8b8) {148- for (int i = 0; i < height; i++) {149- uint32_t *p = (uint32_t *)row_pointers[i];150- for (int j = 0; j < width; j++, p++) {151- uint8_t a = (*p >> 24) & 0xff;152- uint8_t r = (*p >> 16) & 0xff;153- uint8_t g = (*p >> 8) & 0xff;154- uint8_t b = (*p >> 0) & 0xff;177+ if (!gamma_correct) {178+ /* TODO: find a way to make libpng generate premultiplied,179+ sRGB encoded pixels */180 156- if (a == 0xff)157- continue;181+ if (format == PIXMAN_a8b8g8r8) {182+ for (int i = 0; i < height; i++) {183+ uint32_t *p = (uint32_t *)row_pointers[i];184+ for (int j = 0; j < width; j++, p++) {185+ uint8_t a = (*p >> 24) & 0xff;186+ uint8_t r = (*p >> 16) & 0xff;187+ uint8_t g = (*p >> 8) & 0xff;188+ uint8_t b = (*p >> 0) & 0xff;189 159- if (a == 0) {160- r = g = b = 0;161- } else {162- r = r * a / 0xff;163- g = g * a / 0xff;164- b = b * a / 0xff;165- }190+ if (a == 0xff)191+ continue;192 193+ if (a == 0) {194+ r = g = b = 0;195+ } else {196+ r = r * a / 0xff;197+ g = g * a / 0xff;198+ b = b * a / 0xff;199+ }200+167- *p = (uint32_t)a << 24 | r << 16 | g << 8 | b;201+ *p = (uint32_t)a << 24 | r << 16 | g << 8 | b;202+ }203 }204 }205 }206 16 unmodified linesmodified:render.c+105 added-49 removed
34 unmodified lines35 #define LOG_ENABLE_DBG 036 #include "log.h"37 #include "char32.h"38 #include "icon.h"39+#include "srgb.h"40 #include "stride.h"41 #include "xmalloc.h"42 #include "xsnprintf.h"43 19 unmodified lines63 float scale;64 float dpi;65 bool size_font_by_dpi;66 67+ bool gamma_correct;68 pixman_color_t pix_background_color;69 pixman_color_t pix_border_color;70 pixman_color_t pix_text_color;71 pixman_color_t pix_prompt_color;28 unmodified lines100 mtx_t *icon_lock;101 };102 103 static pixman_color_t102-rgba2pixman(struct rgba rgba)104+rgba2pixman(bool gamma_correct, struct rgba rgba)105 {104- uint16_t r = rgba.r * 65535.0;105- uint16_t g = rgba.g * 65535.0;106- uint16_t b = rgba.b * 65535.0;107- uint16_t a = rgba.a * 65535.0;108-109- return (pixman_color_t){110- .red = (uint32_t)r * a / 0xffff,111- .green = (uint32_t)g * a / 0xffff,112- .blue = (uint32_t)b * a / 0xffff,113- .alpha = a,106+ pixman_color_t pix_color = {107+ .alpha = rgba.a * 65535. + 0.5,108+ .red = gamma_correct ? srgb_decode_8_to_16(rgba.r * 255 + 0.5) : rgba.r * 65535. + 0.5,109+ .green = gamma_correct ? srgb_decode_8_to_16(rgba.g * 255 + 0.5) : rgba.g * 65535. + 0.5,110+ .blue = gamma_correct ? srgb_decode_8_to_16(rgba.b * 255 + 0.5) : rgba.b * 65535. + 0.5,111 };112+113+ pix_color.red = (uint32_t)pix_color.red * pix_color.alpha / 0xffff;114+ pix_color.green = (uint32_t)pix_color.green * pix_color.alpha / 0xffff;115+ pix_color.blue = (uint32_t)pix_color.blue * pix_color.alpha / 0xffff;116+117+ return pix_color;118 }119 120 static int121 pt_or_px_as_pixels(const struct render *render, const struct pt_or_px *pt_or_px)617 unmodified lines739 740 #if defined(FUZZEL_ENABLE_SVG_NANOSVG)741 static void742 render_svg_nanosvg(struct icon *icon, int x, int y, int size,740- pixman_image_t *pix, cairo_t *cairo)743+ pixman_image_t *pix, cairo_t *cairo, bool gamma_correct)744 {745 #if defined(FUZZEL_ENABLE_CAIRO)746 cairo_surface_flush(cairo_get_target(cairo));747 #endif14 unmodified lines762 763 if (rast == NULL)764 return;765 766+ /* For gamma-correct blending */767+ uint8_t *data_16bit = NULL;768+ pixman_image_t *img_16bit = NULL;769+ uint64_t *abgr16 = NULL;770+771 float scale = svg->width > svg->height ? size / svg->width : size / svg->height;772 765- uint8_t *data = xmalloc(size * size * 4);766- nsvgRasterize(rast, svg, 0, 0, scale, data, size, size, size * 4);773+ uint8_t *data_8bit = xmalloc(size * size * 4);774 775+ nsvgRasterize(rast, svg, 0, 0, scale, data_8bit, size, size, size * 4);776+777 img = pixman_image_create_bits_no_clear(769- PIXMAN_a8b8g8r8, size, size, (uint32_t *)data, size * 4);778+ PIXMAN_a8b8g8r8, size, size, (uint32_t *)data_8bit, size * 4);779 780+ if (gamma_correct) {781+ data_16bit = xmalloc(size * size * 8);782+ abgr16 = (uint64_t *)data_16bit;783+784+ img_16bit = pixman_image_create_bits_no_clear(785+ PIXMAN_a16b16g16r16, size, size, (uint32_t *)data_16bit,786+ size * 8);787+ }788+789 /* Nanosvg produces non-premultiplied ABGR, while pixman expects790 * premultiplied */791+773- for (uint32_t *abgr = (uint32_t *)data;774- abgr < (uint32_t *)(data + size * size * 4);792+ for (uint32_t *abgr = (uint32_t *)data_8bit;793+ abgr < (uint32_t *)(data_8bit + size * size * 4);794 abgr++)795 {777- uint8_t alpha = (*abgr >> 24) & 0xff;778- uint8_t blue = (*abgr >> 16) & 0xff;779- uint8_t green = (*abgr >> 8) & 0xff;780- uint8_t red = (*abgr >> 0) & 0xff;796+ uint16_t alpha = (*abgr >> 24) & 0xff;797+ uint16_t blue = (*abgr >> 16) & 0xff;798+ uint16_t green = (*abgr >> 8) & 0xff;799+ uint16_t red = (*abgr >> 0) & 0xff;800 782- if (alpha == 0xff)783- continue;801+ /*802+ * TODO: decode sRGB -> linear here803+ *804+ * We should decode to 16-bit, meaning we have to prepare805+ * a 16-bit buffer before looping the pixels.806+ */807 808+ if (gamma_correct) {809+ if (alpha == 0x00)810+ blue = green = red = 0x00;811+ else {812+ alpha |= alpha << 8; /* Alpha already linear, expand to 16-bit */813+ blue = srgb_decode_8_to_16(blue);814+ green = srgb_decode_8_to_16(green);815+ red = srgb_decode_8_to_16(red);816+817+ blue = blue * alpha / 0xffff;818+ green = green * alpha / 0xffff;819+ red = red * alpha / 0xffff;820+ }821+822+ *abgr16 = (uint64_t)alpha << 48 | (uint64_t)blue << 32 | (uint64_t)green << 16 | (uint64_t)red;823+ abgr16++;824+ } else {785- if (alpha == 0x00)786- blue = green = red = 0x00;787- else {788- blue = blue * alpha / 0xff;789- green = green * alpha / 0xff;790- red = red * alpha / 0xff;825+ if (alpha == 0x00)826+ blue = green = red = 0x00;827+ else {828+ blue = (uint8_t)(blue * alpha / 0xff);829+ green = (uint8_t)(green * alpha / 0xff);830+ red = (uint8_t)(red * alpha / 0xff);831+ }832+833+ *abgr = (uint32_t)alpha << 24 | blue << 16 | green << 8 | red;834 }835+ }836 793- *abgr = (uint32_t)alpha << 24 | blue << 16 | green << 8 | red;837+ if (gamma_correct) {838+ /* Replace default image buffer with 16-bit linear buffer */839+ pixman_image_unref(img);840+ free(data_8bit);841+ img = img_16bit;842 }843 844 nsvgDeleteRasterizer(rast);845+846 tll_push_back(icon->rasterized, ((struct rasterized){img, size}));847 }848 849 pixman_image_composite32(6 unmodified lines856 #endif /* FUZZEL_ENABLE_SVG_NANOSVG */857 858 static void859 render_svg(struct icon *icon, int x, int y, int size,811- pixman_image_t *pix, cairo_t *cairo)860+ pixman_image_t *pix, cairo_t *cairo, bool gamma_correct)861 {862 assert(icon->type == ICON_SVG);863 864 if (icon->svg == NULL) {4 unmodified lines869 870 #if defined(FUZZEL_ENABLE_SVG_LIBRSVG)871 render_svg_librsvg(icon, x, y, size, cairo);872 #elif defined(FUZZEL_ENABLE_SVG_NANOSVG)824- render_svg_nanosvg(icon, x, y, size, pix, cairo);873+ render_svg_nanosvg(icon, x, y, size, pix, cairo, gamma_correct);874 #endif875 }876 877 #if defined(FUZZEL_ENABLE_PNG_LIBPNG)78 unmodified lines956 #endif /* FUZZEL_ENABLE_PNG_LIBPNG */957 958 static void959 render_png(struct icon *icon, int x, int y, int size, pixman_image_t *pix,911- cairo_t *cairo)960+ cairo_t *cairo, bool gamma_correct)961 {962 assert(icon->type == ICON_PNG);963 964 if (icon->png == NULL) {916- if (!icon_from_png(icon, icon->path))965+ if (!icon_from_png(icon, icon->path, gamma_correct))966 return;967 LOG_DBG("%s", icon->path);968 }969 88 unmodified lines1058 if (render_icons &&1059 match->application->icon.type == ICON_SVG &&1060 img_y > list_end + render->row_height)1061 {1013- render_svg(&match->application->icon, img_x, img_y, size, pix, cairo);1062+ render_svg(&match->application->icon, img_x, img_y, size, pix, cairo, render->gamma_correct);1063 }1064 }1065 1066 if (render_icons) {6 unmodified lines1073 case ICON_NONE:1074 break;1075 1076 case ICON_PNG:1028- render_png(icon, img_x, img_y, size, pix, cairo);1077+ render_png(icon, img_x, img_y, size, pix, cairo, render->gamma_correct);1078 break;1079 1080 case ICON_SVG:1032- render_svg(icon, img_x, img_y, size, pix, cairo);1081+ render_svg(icon, img_x, img_y, size, pix, cairo, render->gamma_correct);1082 break;1083 }1084 }1085 204 unmodified lines1290 .conf = conf,1291 .icon_lock = icon_lock,1292 };1293 1245- render->pix_background_color = rgba2pixman(render->conf->colors.background);1246- render->pix_border_color = rgba2pixman(render->conf->colors.border);1247- render->pix_text_color = rgba2pixman(render->conf->colors.text);1248- render->pix_prompt_color = rgba2pixman(render->conf->colors.prompt);1249- render->pix_input_color = rgba2pixman(render->conf->colors.input);1250- render->pix_match_color = rgba2pixman(render->conf->colors.match);1251- render->pix_selection_color = rgba2pixman(render->conf->colors.selection);1252- render->pix_selection_text_color = rgba2pixman(render->conf->colors.selection_text);1253- render->pix_selection_match_color = rgba2pixman(render->conf->colors.selection_match);1254- render->pix_counter_color = rgba2pixman(render->conf->colors.counter);1255- render->pix_placeholder_color = rgba2pixman(render->conf->colors.placeholder);1256-1294 if (sem_init(&render->workers.start, 0, 0) < 0 ||1295 sem_init(&render->workers.done, 0, 0) < 0)1296 {1297 LOG_ERRNO("failed to instantiate render worker semaphores");42 unmodified lines1340 return NULL;1341 }1342 1343 void1344+render_initialize_colors(struct render *render, const struct config *conf,1345+ bool gamma_correct)1346+{1347+ render->gamma_correct = gamma_correct;1348+ render->pix_background_color = rgba2pixman(gamma_correct, conf->colors.background);1349+ render->pix_border_color = rgba2pixman(gamma_correct, conf->colors.border);1350+ render->pix_text_color = rgba2pixman(gamma_correct, conf->colors.text);1351+ render->pix_prompt_color = rgba2pixman(gamma_correct, conf->colors.prompt);1352+ render->pix_input_color = rgba2pixman(gamma_correct, conf->colors.input);1353+ render->pix_match_color = rgba2pixman(gamma_correct, conf->colors.match);1354+ render->pix_selection_color = rgba2pixman(gamma_correct, conf->colors.selection);1355+ render->pix_selection_text_color = rgba2pixman(gamma_correct, conf->colors.selection_text);1356+ render->pix_selection_match_color = rgba2pixman(gamma_correct, conf->colors.selection_match);1357+ render->pix_counter_color = rgba2pixman(gamma_correct, conf->colors.counter);1358+ render->pix_placeholder_color = rgba2pixman(gamma_correct, conf->colors.placeholder);1359+}1360+1361+1362+void1363 render_set_subpixel(struct render *render, enum fcft_subpixel subpixel)1364 {1365 render->subpixel = subpixel;1366 }158 unmodified linesmodified:render.h+3 added
12 unmodified lines13 struct render;14 struct render *render_init(const struct config *conf, mtx_t *icon_lock);15 void render_destroy(struct render *render);16 17+void render_initialize_colors(18+ struct render *render, const struct config *conf, bool gamma_correct);19+20 void render_set_subpixel(struct render *render, enum fcft_subpixel subpixel);21 bool render_set_font_and_update_sizes(22 struct render *render, struct fcft_font *font, struct fcft_font *font_bold,23 float scale, float dpi, bool size_font_by_dpi,18 unmodified linesmodified:shm.c+41 added-4 removed
90 unmodified lines91 tll(struct buffer_private *) bufs;92 struct wl_shm *shm;93 size_t pix_instances;94 bool scrollable;95+ pixman_format_code_t pix_fmt_with_alpha;96+ pixman_format_code_t pix_fmt_without_alpha;97+ enum wl_shm_format wl_shm_fmt_with_alpha;98+ enum wl_shm_format wl_shm_fmt_without_alpha;99 };100 101 static tll(struct buffer_private *) deferred;102 183 unmodified lines286 287 wl_buf = wl_shm_pool_create_buffer(288 pool->wl_pool, new_offset,289 buf->public.width, buf->public.height, buf->public.stride,286- buf->with_alpha ? WL_SHM_FORMAT_ARGB8888 : WL_SHM_FORMAT_XRGB8888);290+ buf->with_alpha291+ ? buf->chain->wl_shm_fmt_with_alpha292+ : buf->chain->wl_shm_fmt_without_alpha);293 294 if (wl_buf == NULL) {295 LOG_ERR("failed to create SHM buffer");296 goto err;1 unmodified line298 299 /* One pixman image for each worker thread (do we really need multiple?) */300 for (size_t i = 0; i < buf->public.pix_instances; i++) {301 pix[i] = pixman_image_create_bits_no_clear(296- buf->with_alpha ? PIXMAN_a8r8g8b8 : PIXMAN_x8r8g8b8,302+ buf->with_alpha303+ ? buf->chain->pix_fmt_with_alpha304+ : buf->chain->pix_fmt_without_alpha,305 buf->public.width, buf->public.height,306 (uint32_t *)mmapped, buf->public.stride);307 308 if (pix[i] == NULL) {90 unmodified lines399 400 size_t total_size = 0;401 for (size_t i = 0; i < count; i++) {402 stride[i] = stride_for_format_and_width(395- with_alpha ? PIXMAN_a8r8g8b8 : PIXMAN_x8r8g8b8, widths[i]);403+ with_alpha404+ ? chain->pix_fmt_with_alpha405+ : chain->pix_fmt_without_alpha,406+ widths[i]);407 sizes[i] = stride[i] * heights[i];408 total_size += sizes[i];409 }410 if (total_size == 0)618 unmodified lines1029 break;1030 }1031 }1032 1033+static enum wl_shm_format1034+pix_fmt_to_shm_fmt(pixman_format_code_t pix_fmt)1035+{1036+ switch (pix_fmt) {1037+ default: assert(false); break;1038+1039+ case PIXMAN_a8r8g8b8: return WL_SHM_FORMAT_ARGB8888;1040+ case PIXMAN_x8r8g8b8: return WL_SHM_FORMAT_XRGB8888;1041+ case PIXMAN_a8b8g8r8: return WL_SHM_FORMAT_ABGR8888;1042+ case PIXMAN_x8b8g8r8: return WL_SHM_FORMAT_XBGR8888;1043+ case PIXMAN_a2r10g10b10: return WL_SHM_FORMAT_ARGB2101010;1044+ case PIXMAN_x2r10g10b10: return WL_SHM_FORMAT_XRGB2101010;1045+ case PIXMAN_a2b10g10r10: return WL_SHM_FORMAT_ABGR2101010;1046+ case PIXMAN_x2b10g10r10: return WL_SHM_FORMAT_XBGR2101010;1047+ case PIXMAN_a16b16g16r16: return WL_SHM_FORMAT_ABGR16161616;1048+ }1049+1050+ return WL_SHM_FORMAT_ARGB8888;1051+}1052+1053 struct buffer_chain *1023-shm_chain_new(struct wl_shm *shm, bool scrollable, size_t pix_instances)1054+shm_chain_new(struct wl_shm *shm, bool scrollable, size_t pix_instances,1055+ pixman_format_code_t pix_fmt_with_alpha,1056+ pixman_format_code_t pix_fmt_without_alpha)1057 {1058 struct buffer_chain *chain = xmalloc(sizeof(*chain));1059 *chain = (struct buffer_chain){1060 .bufs = tll_init(),1061 .shm = shm,1062 .pix_instances = pix_instances,1063 .scrollable = scrollable,1064+ .pix_fmt_with_alpha = pix_fmt_with_alpha,1065+ .pix_fmt_without_alpha = pix_fmt_without_alpha,1066+ .wl_shm_fmt_with_alpha = pix_fmt_to_shm_fmt(pix_fmt_with_alpha),1067+ .wl_shm_fmt_without_alpha = pix_fmt_to_shm_fmt(pix_fmt_without_alpha),1068 };1069 return chain;1070 }1071 15 unmodified linesmodified:shm.h+3 added-1 removed
48 unmodified lines49 void shm_set_max_pool_size(off_t max_pool_size);50 51 struct buffer_chain;52 struct buffer_chain *shm_chain_new(53- struct wl_shm *shm, bool scrollable, size_t pix_instances);53+ struct wl_shm *shm, bool scrollable, size_t pix_instances,54+ pixman_format_code_t pix_fmt_with_alpha,55+ pixman_format_code_t pix_fmt_without_alpha);56 void shm_chain_free(struct buffer_chain *chain);57 58 /*59 * Returns a single buffer.39 unmodified linesadded:srgb.py+71 added
1+#!/usr/bin/env python32+3+import argparse4+import math5+import sys6+7+8+# Note: we use a pure gamma 2.2 function, rather than the piece-wise9+# sRGB transfer function, since that is what all compositors do.10+11+def srgb_to_linear(f: float) -> float:12+ assert(f >= 0 and f <= 1.0)13+ return math.pow(f, 2.2)14+15+16+def linear_to_srgb(f: float) -> float:17+ return math.pow(f, 1 / 2.2)18+19+20+def main():21+ parser = argparse.ArgumentParser()22+ parser.add_argument('c_output', type=argparse.FileType('w'))23+ parser.add_argument('h_output', type=argparse.FileType('w'))24+ opts = parser.parse_args()25+26+ linear_table: list[int] = []27+28+ for i in range(256):29+ linear_table.append(int(srgb_to_linear(float(i) / 255) * 65535 + 0.5))30+31+32+ opts.h_output.write("#pragma once\n")33+ opts.h_output.write("#include <stdint.h>\n")34+ opts.h_output.write("\n")35+ opts.h_output.write('/* 8-bit input, 16-bit output */\n')36+ opts.h_output.write("extern const uint16_t srgb_decode_8_to_16_table[256];")37+38+ opts.h_output.write('\n')39+ opts.h_output.write('static inline uint16_t\n')40+ opts.h_output.write('srgb_decode_8_to_16(uint8_t v)\n')41+ opts.h_output.write('{\n')42+ opts.h_output.write(' return srgb_decode_8_to_16_table[v];\n')43+ opts.h_output.write('}\n')44+45+ opts.h_output.write('\n')46+ opts.h_output.write('/* 8-bit input, 8-bit output */\n')47+ opts.h_output.write("extern const uint8_t srgb_decode_8_to_8_table[256];\n")48+49+ opts.h_output.write('\n')50+ opts.h_output.write('static inline uint8_t\n')51+ opts.h_output.write('srgb_decode_8_to_8(uint8_t v)\n')52+ opts.h_output.write('{\n')53+ opts.h_output.write(' return srgb_decode_8_to_8_table[v];\n')54+ opts.h_output.write('}\n')55+56+ opts.c_output.write('#include "srgb.h"\n')57+ opts.c_output.write('\n')58+59+ opts.c_output.write("const uint16_t srgb_decode_8_to_16_table[256] = {\n")60+ for i in range(256):61+ opts.c_output.write(f' {linear_table[i]},\n')62+ opts.c_output.write('};\n')63+64+ opts.c_output.write("const uint8_t srgb_decode_8_to_8_table[256] = {\n")65+ for i in range(256):66+ opts.c_output.write(f' {linear_table[i] >> 8},\n')67+ opts.c_output.write('};\n')68+69+70+if __name__ == '__main__':71+ sys.exit(main())modified:wayland.c+192 added-3 removed
21 unmodified lines22 #include <xkbcommon/xkbcommon-keysyms.h>23 #include <xkbcommon/xkbcommon-compose.h>24 #include <fontconfig/fontconfig.h>25 26+#include <color-management-v1.h>27 #include <cursor-shape-v1.h>28 #include <fractional-scale-v1.h>29 #include <primary-selection-unstable-v1.h>30 #include <viewporter.h>134 unmodified lines165 bool has_zwlr_layer_shell_kb_inter_on_demand;166 167 struct wp_viewport *viewport;168 struct wp_fractional_scale_v1 *fractional_scale;169+ struct wp_color_management_surface_v1 *color_management_surface;170 171 struct wl_shm *shm;172 struct zxdg_output_manager_v1 *xdg_output_manager;173 struct xdg_activation_v1 *xdg_activation_v1;1 unmodified line175 176 struct wl_data_device_manager *data_device_manager;177 struct zwp_primary_selection_device_manager_v1 *primary_selection_device_manager;178 179+ struct {180+ struct wp_color_manager_v1 *manager;181+ struct wp_image_description_v1 *img_description;182+ bool have_intent_perceptual;183+ bool have_feat_parametric;184+ bool have_tf_ext_linear;185+ bool have_primaries_srgb;186+ } color_management;187+188 tll(struct monitor) monitors;189 const struct monitor *monitor;190 191 tll(struct seat) seats;192 193 struct buffer_chain *chain;194+195+ bool shm_have_abgr161616;196+ bool shm_have_xbgr161616;197 };198 199 bool200 repeat_start(struct repeat *repeat, uint32_t key)184 unmodified lines385 386 static void387 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)388 {389+ struct wayland *wayl = data;390+391+ switch (format) {392+#if 0 /* Don't bother with 10bpc formats right now, since they have too few alpha bits */393+ case WL_SHM_FORMAT_XRGB2101010: wayl->shm_have_xrgb2101010 = true; break;394+ case WL_SHM_FORMAT_ARGB2101010: wayl->shm_have_argb2101010 = true; break;395+ case WL_SHM_FORMAT_XBGR2101010: wayl->shm_have_xbgr2101010 = true; break;396+ case WL_SHM_FORMAT_ABGR2101010: wayl->shm_have_abgr2101010 = true; break;397+#endif398+ case WL_SHM_FORMAT_XBGR16161616: wayl->shm_have_xbgr161616 = true; break;399+ case WL_SHM_FORMAT_ABGR16161616: wayl->shm_have_abgr161616 = true; break;400+ }401 }402 403 static const struct wl_shm_listener shm_listener = {404 .format = &shm_format,952 unmodified lines1357 1358 if (wayl->conf->use_bold)1359 strcat(bold_attrs, ":weight=bold");1360 1335- font = fcft_from_name(wayl->font_count, (const char **)names, attrs);1336- font_bold = fcft_from_name(wayl->font_count, (const char **)names, bold_attrs);1361+ struct fcft_font_options *opts = fcft_font_options_create();1362 1363+ opts->color_glyphs.srgb_decode = wayl_do_linear_blending(wayl);1364+ opts->color_glyphs.format = opts->color_glyphs.srgb_decode1365+ ? PIXMAN_a16b16g16r16 : PIXMAN_a8r8g8b8;1366+1367+ font = fcft_from_name2(wayl->font_count, (const char **)names, attrs, opts);1368+ font_bold = fcft_from_name2(wayl->font_count, (const char **)names, bold_attrs, opts);1369+1370+ fcft_font_options_destroy(opts);1371+1372 for (size_t i = 0; i < wayl->font_count; i++)1373 free(names[i]);1374 free(names);1375 234 unmodified lines1610 .name = xdg_output_handle_name,1611 .description = xdg_output_handle_description,1612 };1613 1614+static void1615+color_manager_create_image_description(struct wayland *wayl)1616+{1617+ if (!wayl->color_management.have_feat_parametric)1618+ return;1619+1620+ if (!wayl->color_management.have_primaries_srgb)1621+ return;1622+1623+ if (!wayl->color_management.have_tf_ext_linear)1624+ return;1625+1626+ struct wp_image_description_creator_params_v1 *params =1627+ wp_color_manager_v1_create_parametric_creator(wayl->color_management.manager);1628+1629+ wp_image_description_creator_params_v1_set_tf_named(1630+ params, WP_COLOR_MANAGER_V1_TRANSFER_FUNCTION_EXT_LINEAR);1631+1632+ wp_image_description_creator_params_v1_set_primaries_named(1633+ params, WP_COLOR_MANAGER_V1_PRIMARIES_SRGB);1634+1635+ wayl->color_management.img_description =1636+ wp_image_description_creator_params_v1_create(params);1637+}1638+1639+static void1640+color_manager_supported_intent(void *data,1641+ struct wp_color_manager_v1 *wp_color_manager_v1,1642+ uint32_t render_intent)1643+{1644+ struct wayland *wayl = data;1645+ if (render_intent == WP_COLOR_MANAGER_V1_RENDER_INTENT_PERCEPTUAL)1646+ wayl->color_management.have_intent_perceptual = true;1647+}1648+1649+static void1650+color_manager_supported_feature(void *data,1651+ struct wp_color_manager_v1 *wp_color_manager_v1,1652+ uint32_t feature)1653+{1654+ struct wayland *wayl = data;1655+1656+ if (feature == WP_COLOR_MANAGER_V1_FEATURE_PARAMETRIC)1657+ wayl->color_management.have_feat_parametric = true;1658+}1659+1660+static void1661+color_manager_supported_tf_named(void *data,1662+ struct wp_color_manager_v1 *wp_color_manager_v1,1663+ uint32_t tf)1664+{1665+ struct wayland *wayl = data;1666+ if (tf == WP_COLOR_MANAGER_V1_TRANSFER_FUNCTION_EXT_LINEAR)1667+ wayl->color_management.have_tf_ext_linear = true;1668+}1669+1670+static void1671+color_manager_supported_primaries_named(void *data,1672+ struct wp_color_manager_v1 *wp_color_manager_v1,1673+ uint32_t primaries)1674+{1675+ struct wayland *wayl = data;1676+ if (primaries == WP_COLOR_MANAGER_V1_PRIMARIES_SRGB)1677+ wayl->color_management.have_primaries_srgb = true;1678+}1679+1680+static void1681+color_manager_done(void *data,1682+ struct wp_color_manager_v1 *wp_color_manager_v1)1683+{1684+ struct wayland *wayl = data;1685+ color_manager_create_image_description(wayl);1686+}1687+1688+static const struct wp_color_manager_v1_listener color_manager_listener = {1689+ .supported_intent = &color_manager_supported_intent,1690+ .supported_feature = &color_manager_supported_feature,1691+ .supported_primaries_named = &color_manager_supported_primaries_named,1692+ .supported_tf_named = &color_manager_supported_tf_named,1693+ .done = &color_manager_done,1694+};1695+1696 static bool1697 verify_iface_version(const char *iface, uint32_t version, uint32_t wanted)1698 {1699 if (version >= wanted)276 unmodified lines1976 1977 tll_foreach(wayl->seats, it)1978 seat_add_primary_selection(&it->item);1979 }1980+1981+ else if (strcmp(interface, wp_color_manager_v1_interface.name) == 0) {1982+ const uint32_t required = 1;1983+ if (!verify_iface_version(interface, version, required))1984+ return;1985+1986+ wayl->color_management.manager = wl_registry_bind(1987+ wayl->registry, name, &wp_color_manager_v1_interface, required);1988+1989+ wp_color_manager_v1_add_listener(1990+ wayl->color_management.manager, &color_manager_listener, wayl);1991+ }1992+1993 }1994 1995 static void1996 monitor_destroy(struct monitor *mon)172 unmodified lines2169 }2170 2171 if (wayl->chain == NULL) {2172 wayl->chain =2044- shm_chain_new(wayl->shm, false, 1 + wayl->conf->render_worker_count);2173+ shm_chain_new(2174+ wayl->shm, false, 1 + wayl->conf->render_worker_count,2175+ wayl_do_linear_blending(wayl) && wayl->shm_have_abgr1616162176+ ? PIXMAN_a16b16g16r162177+ : PIXMAN_a8r8g8b8,2178+ wayl_do_linear_blending(wayl) && wayl->shm_have_xbgr1616162179+ ? PIXMAN_a16b16g16r16 /* No PIXMAN_x16b16g16r16 */2180+ : PIXMAN_x8r8g8b82181+ );2182 2183 if (wayl->chain == NULL)2184 abort();2185 }361 unmodified lines2547 wp_fractional_scale_v1_add_listener(2548 wayl->fractional_scale, &fractional_scale_listener, wayl);2549 }2550 2551+ if (conf->gamma_correct) {2552+#if !defined(FUZZEL_ENABLE_CAIRO)2553+ if (wayl->color_management.img_description != NULL) {2554+ assert(wayl->color_management.manager != NULL);2555+2556+ wayl->color_management_surface = wp_color_manager_v1_get_surface(2557+ wayl->color_management.manager, wayl->surface);2558+2559+ wp_color_management_surface_v1_set_image_description(2560+ wayl->color_management_surface, wayl->color_management.img_description,2561+ WP_COLOR_MANAGER_V1_RENDER_INTENT_PERCEPTUAL);2562+2563+ LOG_INFO("gamma-correct blending: enabled");2564+ } else {2565+ if (wayl->color_management.manager == NULL) {2566+ LOG_WARN(2567+ "gamma-corrected-blending: disabling; "2568+ "compositor does not implement the color-management protocol");2569+ } else {2570+ LOG_WARN(2571+ "gamma-corrected-blending: disabling; "2572+ "compositor does not implement all required color-management features");2573+ LOG_WARN("use e.g. 'wayland-info' and verify the compositor implements:");2574+ LOG_WARN(" - feature: parametric");2575+ LOG_WARN(" - render intent: perceptual");2576+ LOG_WARN(" - TF: ext_linear");2577+ LOG_WARN(" - primaries: sRGB");2578+ }2579+ }2580+#else2581+ LOG_WARN("gamma-correct-blending: disabling; not supported in cairo-enabled builds");2582+#endif2583+ } else2584+ LOG_INFO("gamma-correct blending: disabled");2585+2586 zwlr_layer_surface_v1_set_keyboard_interactivity(wayl->layer_surface,2587 wayl->has_zwlr_layer_shell_kb_inter_on_demand2588 ? wayl->conf->keyboard_focus2589 : ZWLR_LAYER_SURFACE_V1_KEYBOARD_INTERACTIVITY_EXCLUSIVE);43 unmodified lines2633 tll_foreach(wayl->monitors, it)2634 monitor_destroy(&it->item);2635 tll_free(wayl->monitors);2636 2637+ if (wayl->color_management_surface != NULL)2638+ wp_color_management_surface_v1_destroy(wayl->color_management_surface);2639+ if (wayl->color_management.img_description != NULL)2640+ wp_image_description_v1_destroy(wayl->color_management.img_description);2641+ if (wayl->color_management.manager != NULL)2642+ wp_color_manager_v1_destroy(wayl->color_management.manager);2643 if (wayl->data_device_manager != NULL)2644 wl_data_device_manager_destroy(wayl->data_device_manager);2645 if (wayl->primary_selection_device_manager != NULL)2646 zwp_primary_selection_device_manager_v1_destroy(wayl->primary_selection_device_manager);112 unmodified lines2759 {2760 matches_update_incremental(wayl->matches);2761 wayl_refresh(wayl);2762 }2763+2764+bool2765+wayl_do_linear_blending(const struct wayland *wayl)2766+{2767+#if !defined(FUZZEL_ENABLE_CAIRO)2768+ return wayl->conf->gamma_correct &&2769+ wayl->color_management.img_description != NULL;2770+#else2771+ return false;2772+#endif2773+}modified:wayland.h+5 added
1 #pragma once2 3+#include <stdbool.h>4+#include <stdint.h>5+6 #include <fcft/fcft.h>7 8 #include "clipboard.h"9 #include "config.h"75 unmodified lines85 bool wayl_update_cache(const struct wayland *wayl);86 87 void wayl_clipboard_data(struct wayland *wayl, char *data, size_t size);88 void wayl_clipboard_done(struct wayland *wayl);89+90+bool wayl_do_linear_blending(const struct wayland *wayl);