Merge branch 'pixman'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Closes #10
authored by Daniel Eklöf · committed by Daniel Eklöf · parents e899892, edabc3e · Browse files · 11 files·+129 added−73 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
11 files+129 added−73 removed
- other:+129 added−73 removed
Provenance
- Commit
477141e- Tree
007524b- Author
Daniel Eklöf <daniel@ekloef.se>- Committer
Daniel Eklöf <daniel@ekloef.se>- Parent 1
e899892- Parent 2
edabc3e
11 files changed+129 added−73 removed
modified:application.h+2 added-3 removed
1 unmodified line2 3 #include <stdbool.h>4 #include <pixman.h>5 6-#include <cairo.h>7-#ifdef FUZZEL_ENABLE_SVG8-#include <librsvg/rsvg.h>6+#if defined(FUZZEL_ENABLE_SVG)7+ #include <librsvg/rsvg.h>8 #endif9 10 #include "prompt.h"11 37 unmodified linesmodified:CHANGELOG.md+4 added
12 unmodified lines13141516171819202122232429 unmodified lines12 unmodified lines disabled. Default: `auto`.* meson option `-Denable-png=[auto|enabled|disabled]`. When disabled, _libpng_ is no longer a dependency and PNG icons are disabled. Default: `auto`.* meson option `-Denable-cairo=auto|enabled|disabled`. When disabled, fuzzel will not be able to draw rounded corners, nor support SVGs (regardless of what `-Denable-svg` is set to) (https://codeberg.org/dnkl/fuzzel/issues/10).
### Deprecated### Removed29 unmodified linesmodified:main.c+2 added
435 unmodified lines436 fdm_destroy(fdm);437 applications_destroy(apps);438 icon_themes_destroy(themes);439 440+#if defined(FUZZEL_ENABLE_CAIRO)441 cairo_debug_reset_static_data();442+#endif443 log_deinit();444 return ret;445 }modified:meson_options.txt+3 added-2 removed
1-option('enable-png', type: 'feature')2-option('enable-svg', type: 'feature')1+option('enable-cairo', type: 'feature', description: 'use cairo; required for rounded corners and SVG icons')2+option('enable-png', type: 'feature', description: 'enables support for PNG icons')3+option('enable-svg', type: 'feature', description: 'enables support for SVG icons (note: also requires cairo)')modified:meson.build+14 added-7 removed
46 unmodified lines47 endif48 49 math = cc.find_library('m')50 pixman = dependency('pixman-1')51-cairo = dependency('cairo')52-cairo_ft = dependency('cairo-ft')51+cairo = dependency('cairo', required: get_option('enable-cairo'))52 libpng = dependency('libpng', required: get_option('enable-png'))53+if cairo.found()54-librsvg = dependency('librsvg-2.0', required: get_option('enable-svg'))54+ librsvg = dependency('librsvg-2.0', required: get_option('enable-svg'))55+else56+ librsvg = declare_dependency()57+endif58 wayland_protocols = dependency('wayland-protocols')59 wayland_client = dependency('wayland-client')60 wayland_cursor = dependency('wayland-cursor')61 xkb = dependency('xkbcommon')62 60-if (libpng.found())63+if cairo.found()64+ add_project_arguments('-DFUZZEL_ENABLE_CAIRO=1', language: 'c')65+endif66+67+if libpng.found()68 add_project_arguments('-DFUZZEL_ENABLE_PNG=1', language: 'c')69 endif70 64-if librsvg.found()71+if librsvg.found() and cairo.found()72 add_project_arguments('-DFUZZEL_ENABLE_SVG=1', language: 'c')73 endif74 75 tllist = dependency('tllist', version: '>=1.0.1', fallback: 'tllist')51 unmodified lines127 wl_proto_src + wl_proto_headers, version,128 dependencies: [math,129 pixman,130 cairo,124- cairo_ft,131 libpng,132 librsvg,133 wayland_client,134 wayland_cursor,10 unmodified lines145 subdir('doc')146 147 summary(148 {149+ 'Cairo': cairo.found(),150 'PNG icons': libpng.found(),144- 'SVG icons': librsvg.found(),151+ 'SVG icons': librsvg.found() and cairo.found(),152 },153 bool_yn: true154 )modified:README.md+10 added-1 removed
71 unmodified lines727374757676777879808182838485868788899091929394959620 unmodified lines71 unmodified lines
Second, configure the build (if you intend to install it globally, youmight also want `--prefix=/usr`):```shmeson --buildtype=release -Denable-png=disabled|enabled|auto -Denable-svg=disabled|enabled|auto ../..meson --buildtype=release \ -Denable-cairo=disabled|enabled|auto \ -Denable-png=disabled|enabled|auto \ -Denable-svg=disabled|enabled|auto \ ../..```
`-Denable-{png,svg}` can be used to force-enable or force-disable pngand/or svg support. The default is `auto`, which means enable if allrequired libraries are available. PNGs require _libpng_, and SVGsrequire _cairo_ and _librsvg_.
`-Denable-cairo` can be used to force-enable or force-disable cairosupport. When disabled, fuzzel will not be able to draw roundedcorners, nor will it support SVGs (regardless of what `-Denable-svg`is set to).
Three, build it:```shninja```20 unmodified linesmodified:render.c+69 added-55 removed
21 unmodified lines22 unsigned border_size;23 unsigned row_height;24 };25 26+static pixman_color_t27+rgba2pixman(struct rgba rgba)28+{29+ uint16_t r = rgba.r * 65535.0;30+ uint16_t g = rgba.g * 65535.0;31+ uint16_t b = rgba.b * 65535.0;32+ uint16_t a = rgba.a * 65535.0;33+34+ return (pixman_color_t){35+ .red = r * a / 0xffff,36+ .green = g * a / 0xffff,37+ .blue = b * a / 0xffff,38+ .alpha = a,39+ };40+}41+42 void43 render_background(const struct render *render, struct buffer *buf)44 {29- /*30- * Lines in cairo are *between* pixels.31- *32- * To get a sharp 1px line, we need to draw it with33- * line-width=2.34- *35- * Thus, we need to draw the path offset:ed with half that36- * (=actual border width).37- */38- const double b = render->border_size;39- const double w = max(buf->width - 2 * b, 0.);40- const double h = max(buf->height - 2 * b, 0.);45+ bool use_pixman =46+#if defined(FUZZEL_ENABLE_CAIRO)47+ render->options.border_radius == 048+#else49+ true50+#endif51+ ;52 42- if (w == 0. || h == 0.)43- return;44-45- if (render->options.border_radius == 0) {46- cairo_rectangle(buf->cairo, b, b, w, h);53+ if (use_pixman) {54+ pixman_color_t bg = rgba2pixman(render->options.background_color);55+ pixman_image_fill_rectangles(56+ PIXMAN_OP_SRC, buf->pix, &bg, 1, &(pixman_rectangle16_t){0, 0, buf->width, buf->height}57+ );58 } else {59+#if defined(FUZZEL_ENABLE_CAIRO)60+ /*61+ * Lines in cairo are *between* pixels.62+ *63+ * To get a sharp 1px line, we need to draw it with64+ * line-width=2.65+ *66+ * Thus, we need to draw the path offset:ed with half that67+ * (=actual border width).68+ */69+ const double b = render->border_size;70+ const double w = max(buf->width - 2 * b, 0.);71+ const double h = max(buf->height - 2 * b, 0.);72+73 const double from_degree = M_PI / 180;74 const double radius = render->options.border_radius;75 51-76 /* Path describing an arc:ed rectangle */77 cairo_arc(buf->cairo, b + w - radius, b + h - radius, radius,78 0.0 * from_degree, 90.0 * from_degree);79 cairo_arc(buf->cairo, b + radius, b + h - radius, radius,2 unmodified lines82 180.0 * from_degree, 270.0 * from_degree);83 cairo_arc(buf->cairo, b + w - radius, b + radius, radius,84 270.0 * from_degree, 360.0 * from_degree);85 cairo_close_path(buf->cairo);62- }86 64- /* Border */65- const struct rgba *bc = &render->options.border_color;66- cairo_set_operator(buf->cairo, CAIRO_OPERATOR_SOURCE);67- cairo_set_line_width(buf->cairo, 2 * b);68- cairo_set_source_rgba(buf->cairo, bc->r, bc->g, bc->b, bc->a);69- cairo_stroke_preserve(buf->cairo);87+ /* Border */88+ const struct rgba *bc = &render->options.border_color;89+ cairo_set_operator(buf->cairo, CAIRO_OPERATOR_SOURCE);90+ cairo_set_line_width(buf->cairo, 2 * b);91+ cairo_set_source_rgba(buf->cairo, bc->r, bc->g, bc->b, bc->a);92+ cairo_stroke_preserve(buf->cairo);93 71- /* Background */72- const struct rgba *bg = &render->options.background_color;73- cairo_set_source_rgba(buf->cairo, bg->r, bg->g, bg->b, bg->a);74- cairo_fill(buf->cairo);94+ /* Background */95+ const struct rgba *bg = &render->options.background_color;96+ cairo_set_source_rgba(buf->cairo, bg->r, bg->g, bg->b, bg->a);97+ cairo_fill(buf->cairo);98+#else99+ assert(false);100+#endif101+ }102 }103 77-static pixman_color_t78-rgba2pixman(struct rgba rgba)79-{80- uint16_t r = rgba.r * 65535.0;81- uint16_t g = rgba.g * 65535.0;82- uint16_t b = rgba.b * 65535.0;83- uint16_t a = rgba.a * 65535.0;84-85- return (pixman_color_t){86- .red = r * a / 0xffff,87- .green = g * a / 0xffff,88- .blue = b * a / 0xffff,89- .alpha = a,90- };91-}92-104 static void105 render_glyph(pixman_image_t *pix, const struct fcft_glyph *glyph, int x, int y, const pixman_color_t *color)106 {107 if (pixman_image_get_format(glyph->pix) == PIXMAN_a8r8g8b8) {168 unmodified lines276 }277 #endif /* FUZZEL_ENABLE_SVG */278 }279 269- /* Hightlight selected entry */270- const struct rgba *sc = &render->options.selection_color;271- cairo_set_source_rgba(buf->cairo, sc->r, sc->g, sc->b, sc->a);272- cairo_set_operator(buf->cairo, CAIRO_OPERATOR_SOURCE);273- cairo_rectangle(buf->cairo,274- x_margin - sel_margin,275- first_row + i * row_height,276- buf->width - 2 * (x_margin - sel_margin),277- row_height);278- cairo_fill(buf->cairo);280+ pixman_color_t sc = rgba2pixman(render->options.selection_color);281+ pixman_image_fill_rectangles(282+ PIXMAN_OP_SRC, buf->pix, &sc, 1,283+ &(pixman_rectangle16_t){284+ x_margin - sel_margin,285+ first_row + i * row_height,286+ buf->width - 2 * (x_margin - sel_margin),287+ row_height}288+ );289 }290 291 double cur_x = border_size + x_margin;292 struct icon *icon = &match->application->icon;3 unmodified lines296 break;297 298 case ICON_PNG: {299 #if defined(FUZZEL_ENABLE_PNG)300+ #if defined(FUZZEL_ENABLE_CAIRO)301 cairo_surface_flush(buf->cairo_surface);302+ #endif303 304 pixman_image_t *png = icon->png.pix;305 int height = pixman_image_get_height(png);306 int width = pixman_image_get_width(png);42 unmodified lines349 0, 0, 0, 0,350 cur_x, first_row + i * row_height + (row_height - height) / 2,351 width, height);352 353+ #if defined(FUZZEL_ENABLE_CAIRO)354 cairo_surface_mark_dirty(buf->cairo_surface);355+ #endif356 #endif /* FUZZEL_ENABLE_PNG */357 break;358 }359 123 unmodified linesmodified:render.h-1 removed
1 #pragma once2 3 #include <pixman.h>4-#include <cairo.h>4 5 #include <fcft/fcft.h>6 7 #include "shm.h"42 unmodified linesmodified:shm.c+17 added-3 removed
9 unmodified lines10 #include <tllist.h>11 12 #define LOG_MODULE "shm"13 #include "log.h"14+#include "stride.h"15 16 static tll(struct buffer) buffers;17 18 static void41 unmodified lines60 struct wl_shm_pool *pool = NULL;61 struct wl_buffer *buf = NULL;62 63 pixman_image_t *pix = NULL;64+65+#if defined(FUZZEL_ENABLE_CAIRO)66 cairo_surface_t *cairo_surface = NULL;67 cairo_t *cairo = NULL;68+#endif69 70 /* Backing memory for SHM */71 pool_fd = memfd_create("fuzzel-wayland-shm-buffer-pool", MFD_CLOEXEC);72 if (pool_fd == -1) {1 unmodified line74 goto err;75 }76 77 /* Total size */74- const uint32_t stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);78+ const uint32_t stride = stride_for_format_and_width(PIXMAN_a8r8g8b8, width);79 size = stride * height;80 if (ftruncate(pool_fd, size) == -1) {81 LOG_ERRNO("failed to truncate SHM pool");82 goto err;28 unmodified lines111 LOG_ERR("failed to create pixman image");112 goto err;113 }114 115+#if defined(FUZZEL_ENABLE_CAIRO)116 /* Create a cairo surface around the mmapped memory */117 cairo_surface = cairo_image_surface_create_for_data(118 mmapped, CAIRO_FORMAT_ARGB32, width, height, stride);119 if (cairo_surface_status(cairo_surface) != CAIRO_STATUS_SUCCESS) {7 unmodified lines127 LOG_ERR("failed to create cairo context: %s",128 cairo_status_to_string(cairo_status(cairo)));129 goto err;130 }131+#endif132 133 /* Push to list of available buffers, but marked as 'busy' */134 tll_push_back(135 buffers,5 unmodified lines141 .size = size,142 .mmapped = mmapped,143 .wl_buf = buf,144 .pix = pix,145+146+#if defined(FUZZEL_ENABLE_CAIRO)147 .cairo_surface = cairo_surface,140- .cairo = cairo}141- )148+ .cairo = cairo149+#endif150+ })151 );152 153 struct buffer *ret = &tll_back(buffers);154 wl_buffer_add_listener(ret->wl_buf, &buffer_listener, ret);155 return ret;156 157 err:158+#if defined(FUZZEL_ENABLE_CAIRO)159 if (cairo != NULL)160 cairo_destroy(cairo);161 if (cairo_surface != NULL)162 cairo_surface_destroy(cairo_surface);163+#endif164 if (pix != NULL)165 pixman_image_unref(pix);166 if (buf != NULL)167 wl_buffer_destroy(buf);12 unmodified lines180 {181 tll_foreach(buffers, it) {182 struct buffer *buf = &it->item;183 184+#if defined(FUZZEL_ENABLE_CAIRO)185 cairo_destroy(buf->cairo);186 cairo_surface_destroy(buf->cairo_surface);187+#endif188+189 pixman_image_unref(buf->pix);190 wl_buffer_destroy(buf->wl_buf);191 munmap(buf->mmapped, buf->size);192 3 unmodified linesmodified:shm.h+6 added-1 removed
2 unmodified lines3 #include <stdbool.h>4 #include <stddef.h>5 6 #include <pixman.h>7+#if defined(FUZZEL_ENABLE_CAIRO)7-#include <cairo.h>8+ #include <cairo.h>9+#endif10 #include <wayland-client.h>11 12 struct buffer {13 int width;6 unmodified lines20 21 struct wl_buffer *wl_buf;22 23 pixman_image_t *pix;24+25+#if defined(FUZZEL_ENABLE_CAIRO)26 cairo_surface_t *cairo_surface;27 cairo_t *cairo;28+#endif29 };30 31 struct buffer *shm_get_buffer(struct wl_shm *shm, int width, int height);32 void shm_fini(void);modified:wayland.c+2 added
1299 unmodified lines1300 /* Window content */1301 render_prompt(wayl->render, buf, wayl->prompt);1302 render_match_list(wayl->render, buf, wayl->prompt, wayl->matches);1303 1304+#if defined(FUZZEL_ENABLE_CAIRO)1305 cairo_surface_flush(buf->cairo_surface);1306+#endif1307 1308 if (wayl->frame_is_scheduled) {1309 /* There's already a frame being drawn - delay current frame1310 * (overwriting any previous pending frame) */264 unmodified lines