render.c
This file
Path
render.c
1,890 lines · 60.0 KiB · C
Last changed by
render: fix crash: don't assume render->selection_corners has been initializedDaniel Eklöfbefore adoption6f40720
History of this fileTake it
1#include "render.h"23#include <limits.h>4#include <pthread.h>5#include <semaphore.h>6#include <signal.h>7#include <stdio.h>8#include <stdlib.h>9#include <string.h>10#include <threads.h>11#include <uchar.h>12#include <sys/time.h>1314#include "column.h"15#include "macros.h"16#if HAS_INCLUDE(<pthread_np.h>)17#include <pthread_np.h>18#define pthread_setname_np(thread, name) (pthread_set_name_np(thread, name), 0)19#elif defined(__NetBSD__)20#define pthread_setname_np(thread, name) pthread_setname_np(thread, "%s", (void *)name)21#endif2223#if defined(FUZZEL_ENABLE_CAIRO)24#include <cairo.h>25#else26#define cairo_t void27#endif2829#include <fcft/fcft.h>3031#if defined(FUZZEL_ENABLE_SVG_NANOSVG)32 #include <nanosvg/nanosvgrast.h>33#endif3435#define LOG_MODULE "render"36#define LOG_ENABLE_DBG 037#include "log.h"38#include "char32.h"39#include "icon.h"40#include "srgb.h"41#include "stride.h"42#include "xmalloc.h"43#include "xsnprintf.h"4445#define min(x, y) ((x) < (y) ? (x) : (y))46#define max(x, y) ((x) > (y) ? (x) : (y))4748struct render;49struct thread_context {50 struct render *render;51 int my_id;52};5354struct render {55 const struct config *conf;56 struct fcft_font *font;57 struct fcft_font *font_bold;58 enum fcft_subpixel subpixel;5960 /* Cached fcft text runs */61 struct fcft_text_run *prompt_text_run;62 struct fcft_text_run *placeholder_text_run;6364 /* Cached selection corners */65 pixman_image_t *selection_corners;6667 float scale;68 float dpi;69 bool size_font_by_dpi;7071 bool gamma_correct;72 pixman_color_t pix_background_color;73 pixman_color_t pix_border_color;74 pixman_color_t pix_text_color;75 pixman_color_t pix_prompt_color;76 pixman_color_t pix_input_color;77 pixman_color_t pix_match_color;78 pixman_color_t pix_selection_color;79 pixman_color_t pix_selection_text_color;80 pixman_color_t pix_selection_match_color;81 pixman_color_t pix_counter_color;82 pixman_color_t pix_placeholder_color;8384 unsigned x_margin;85 unsigned y_margin;86 unsigned inner_pad;87 unsigned border_size;88 unsigned border_radius;89 unsigned selection_border_radius;90 unsigned row_height;91 unsigned icon_height;9293 unsigned input_glyph_offset; /* At which glyph to start rendering input */9495 struct {96 uint16_t count;97 sem_t start;98 sem_t done;99 mtx_t lock;100 tll(int) queue;101 thrd_t *threads;102103 const struct matches *matches;104 struct buffer *buf;105 bool render_icons;106 } workers;107108 mtx_t *icon_lock;109};110111static pixman_color_t112rgba2pixman(bool gamma_correct, struct rgba rgba)113{114 pixman_color_t pix_color = {115 .alpha = rgba.a * 65535. + 0.5,116 .red = gamma_correct ? srgb_decode_8_to_16(rgba.r * 255 + 0.5) : rgba.r * 65535. + 0.5,117 .green = gamma_correct ? srgb_decode_8_to_16(rgba.g * 255 + 0.5) : rgba.g * 65535. + 0.5,118 .blue = gamma_correct ? srgb_decode_8_to_16(rgba.b * 255 + 0.5) : rgba.b * 65535. + 0.5,119 };120121 pix_color.red = (uint32_t)pix_color.red * pix_color.alpha / 0xffff;122 pix_color.green = (uint32_t)pix_color.green * pix_color.alpha / 0xffff;123 pix_color.blue = (uint32_t)pix_color.blue * pix_color.alpha / 0xffff;124125 return pix_color;126}127128static int129pt_or_px_as_pixels(const struct render *render, const struct pt_or_px *pt_or_px)130{131 double scale = !render->size_font_by_dpi ? render->scale : 1.;132 double dpi = render->size_font_by_dpi ? render->dpi : 96.;133134 return pt_or_px->px == 0135 ? round(pt_or_px->pt * scale * dpi / 72.)136 : pt_or_px->px;137}138139static pixman_region32_t140rounded_rectangle_region(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t radius)141{142143 int rect_count = ( radius + radius ) + 1;144 pixman_box32_t rects[rect_count];145146 for (int i = 0; i <= radius; i++) {147 uint16_t ydist = radius - i;148 uint16_t curve = sqrt(radius * radius - ydist * ydist);149150 rects[i] = (pixman_box32_t) {151 x + radius - curve,152 y + i,153 x + width - radius + curve,154 y + i + 1155 };156157 rects[radius + i] = (pixman_box32_t) {158 x + radius - curve,159 y + height - i,160 x + width - radius + curve,161 y + height - i + 1162 };163 }164165 rects[(radius * 2)] = (pixman_box32_t){166 x,167 y + radius,168 x + width,169 y + height + 1 - radius170 };171172 pixman_region32_t region;173 pixman_region32_init_rects(®ion, rects, rect_count);174 return region;175}176177static inline void178fill_region32(pixman_op_t op, pixman_image_t* dest,179 const pixman_color_t* color, pixman_region32_t* region)180{181 int rectc;182 pixman_box32_t *rects = pixman_region32_rectangles(region, &rectc);183 pixman_image_fill_boxes(op, dest, color, rectc, rects);184185}186187static inline void188fill_rounded_rectangle(pixman_op_t op, pixman_image_t* dest,189 const pixman_color_t* color, int16_t x, int16_t y,190 uint16_t width, uint16_t height, uint16_t radius)191{192 pixman_region32_t region = rounded_rectangle_region(x, y, width, height, radius);193 fill_region32(op, dest, color, ®ion);194 pixman_region32_fini(®ion);195}196197static void198render_rounded_rectangle(pixman_image_t* dest, pixman_color_t* background,199 pixman_color_t* border, unsigned int radius, unsigned bw,200 int16_t x, int16_t y, uint16_t width, uint16_t height)201{202 if (radius == 0) {203 pixman_image_fill_rectangles(204 PIXMAN_OP_SRC, dest, background,205 1, &(pixman_rectangle16_t){206 x + bw, y + bw, width - 2 * bw, height - 2 * bw});207208 pixman_image_fill_rectangles(209 PIXMAN_OP_SRC, dest, border,210 4, (pixman_rectangle16_t[]){211 {x, y, width, bw}, /* top */212 {x, y + bw, bw, height - 2 * bw}, /* left */213 {x + width - bw, y + bw, bw, height - 2 * bw}, /* right */214 {x, y + height - bw, width, bw} /* bottom */215 });216 } else {217 const int msaa_scale = 2;218 const double brd_sz_scaled = bw * msaa_scale;219 const double brd_rad_scaled = radius * msaa_scale;220 int w = width * msaa_scale;221 int h = height * msaa_scale;222 int bg_rad = brd_rad_scaled * (1.0 - (float)brd_sz_scaled / (float)brd_rad_scaled);223224 pixman_image_t *bg_img;225 if (msaa_scale != 1) {226 bg_img = pixman_image_create_bits(227 pixman_image_get_format(dest), w, h, NULL, w*4);228 } else {229 bg_img = dest;230 }231232 /* Border */233 fill_rounded_rectangle(234 PIXMAN_OP_SRC, bg_img, border, 0, 0, w, h, brd_rad_scaled);235236 /* Background */237 fill_rounded_rectangle(238 PIXMAN_OP_SRC, bg_img, background, brd_sz_scaled, brd_sz_scaled,239 w-(brd_sz_scaled*2),240 h-(brd_sz_scaled*2),241 bg_rad);242243 if (msaa_scale != 1) {244 pixman_f_transform_t ftrans;245 pixman_transform_t trans;246 pixman_f_transform_init_scale(&ftrans, msaa_scale, msaa_scale);247 pixman_transform_from_pixman_f_transform(&trans, &ftrans);248 pixman_image_set_transform(bg_img, &trans);249 pixman_image_set_filter(bg_img, PIXMAN_FILTER_BILINEAR, NULL, 0);250251 pixman_image_composite32(252 PIXMAN_OP_OVER, bg_img, NULL, dest, 0, 0, 0, 0, x, y,253 width, height);254 pixman_image_unref(bg_img);255 }256 }257}258259void260render_background(const struct render *render, struct buffer *buf)261{262 unsigned bw = render->border_size;263264 pixman_color_t bg = render->pix_background_color;265 pixman_color_t border_color = render->pix_border_color;266267 if (buf->age == 0) {268 /* Each sub-part of the window erases itself */269 return;270 }271272 /* Limit radius if the margins are very small, to prevent e.g. the273 selection "box" from overlapping the corners */274 const unsigned int radius =275 min(render->border_radius,276 max(render->x_margin,277 render->y_margin));278279 render_rounded_rectangle(buf->pix[0], &bg, &border_color, radius, bw,280 0, 0, buf->width, buf->height);281}282283static void284render_glyph(pixman_image_t *pix, const struct fcft_glyph *glyph, int x, int y,285 const pixman_color_t *color)286{287 if (pixman_image_get_format(glyph->pix) == PIXMAN_a8r8g8b8) {288 /* Glyph surface is a pre-rendered image (typically a color emoji...) */289 pixman_image_composite32(290 PIXMAN_OP_OVER, glyph->pix, NULL, pix, 0, 0, 0, 0,291 x + glyph->x, y - glyph->y,292 glyph->width, glyph->height);293 } else {294 /* Glyph surface is an alpha mask */295 pixman_image_t *src = pixman_image_create_solid_fill(color);296 pixman_image_composite32(297 PIXMAN_OP_OVER, src, glyph->pix, pix, 0, 0, 0, 0,298 x + glyph->x, y - glyph->y,299 glyph->width, glyph->height);300 pixman_image_unref(src);301 }302}303304static int305render_baseline(const struct render *render)306{307 const struct fcft_font *font = render->font;308 const int line_height = render->row_height;309 const int font_height = font->ascent + font->descent;310311 /*312 * Center glyph on the line *if* using a custom line height,313 * otherwise the baseline is simply 'descent' pixels above the314 * bottom of the cell315 */316 const int glyph_top_y =317 render->conf->line_height.px >= 0 && line_height >= font_height318 ? round((line_height - font_height) / 2.)319 : 0;320321 return line_height - glyph_top_y - font->descent;322}323324static int325render_match_count(const struct render *render, struct buffer *buf,326 const struct prompt *prompt, const struct matches *matches)327{328 struct fcft_font *font = render->font;329 assert(font != NULL);330331 const struct config *conf = render->conf;332333 const enum fcft_subpixel subpixel =334 conf->colors.background.a == 1. && conf->colors.selection.a == 1.335 ? render->subpixel336 : FCFT_SUBPIXEL_NONE;337338 size_t total_count = matches_get_application_visible_count(matches);339 size_t match_count = matches_get_total_count(matches);340 const char32_t *ptext = prompt_text(prompt);341342 char text[64];343 size_t count = (ptext[0] == U'\0') ? total_count : match_count;344 size_t chars = xsnprintf(text, sizeof(text), "%zu/%zu", count, total_count);345346 /* fcft wants UTF-32. Since we only use ASCII... */347 uint32_t wtext[64];348 for (size_t i = 0; i < chars; i++)349 wtext[i] = (uint32_t)(unsigned char)text[i];350351 int width = -1;352 int x = buf->width - render->border_size - render->x_margin;353 int y = render->border_size + render->y_margin +354 (render->row_height + font->height) / 2 - font->descent;355356 if (fcft_capabilities() & FCFT_CAPABILITY_TEXT_RUN_SHAPING) {357 struct fcft_text_run *run = fcft_rasterize_text_run_utf32(358 font, chars, wtext, subpixel);359360 if (run != NULL) {361 width = 0;362363 for (size_t i = 0; i < run->count; i++) {364 const struct fcft_glyph *glyph = run->glyphs[i];365 width += glyph->advance.x;366 }367368 x -= width;369 for (size_t i = 0; i < run->count; i++) {370 const struct fcft_glyph *glyph = run->glyphs[i];371 render_glyph(buf->pix[0], glyph, x, y, &render->pix_counter_color);372 x += glyph->advance.x;373 }374375 fcft_text_run_destroy(run);376 return width;377 }378 }379380 const struct fcft_glyph **glyphs = xmalloc(chars * sizeof(glyphs[0]));381 long *x_kern = xmalloc(chars * sizeof(x_kern[0]));382 char32_t prev = 0;383384 width = 0;385 for (size_t i = 0; i < (size_t)chars; i++) {386 x_kern[i] = 0;387388 const struct fcft_glyph *glyph =389 fcft_rasterize_char_utf32(font, wtext[i], subpixel);390 glyphs[i] = glyph;391392 if (glyph == NULL)393 continue;394395 if (i > 0) {396 if (fcft_kerning(font, prev, wtext[i], &x_kern[i], NULL))397 width += x_kern[i];398 }399 width += glyph->advance.x;400 prev = wtext[i];401 }402403 x -= width;404405 for (size_t i = 0; i < (size_t)chars; i++) {406 const struct fcft_glyph *glyph = glyphs[i];407 if (glyph == NULL)408 continue;409410 x += x_kern[i];411 render_glyph(buf->pix[0], glyph, x, y, &render->pix_counter_color);412 x += glyph->advance.x;413 prev = wtext[i];414 }415416 free(x_kern);417 free(glyphs);418 return width;419}420421static void422render_cursor(const struct render *render, int x, int baseline, pixman_image_t *pix)423{424 struct fcft_font *font = render->font;425426 if (true) {427 /* Bar cursor */428 const int height = min(font->ascent + font->descent, render->row_height);429430 pixman_image_fill_rectangles(431 PIXMAN_OP_SRC, pix, &render->pix_input_color,432 1, &(pixman_rectangle16_t){433 x,434 baseline + render->font->descent - height,435 font->underline.thickness,436 height});437 } else {438 /* TODO: future: underline cursor */439 pixman_image_fill_rectangles(440 PIXMAN_OP_SRC, pix, &render->pix_input_color,441 1, &(pixman_rectangle16_t){442 x, baseline - font->underline.position,443 font->max_advance.x,444 font->underline.thickness});445 }446}447448static void449adjust_input_glyph_offset(struct render *render, const struct prompt *prompt,450 size_t count,451 const struct fcft_glyph *glyphs[static count],452 long kerning[static count],453 int start_x, int max_x)454{455 struct fcft_font *font = render->font;456 const size_t cursor_location = prompt_cursor(prompt);457458 if (cursor_location == 0)459 render->input_glyph_offset = 0;460461 /* Clamp to input string's length */462 render->input_glyph_offset = min(render->input_glyph_offset, count - 1);463464 /* If cursor position is before the current offset, adjust the465 offset backward */466 for (size_t i = 0; i < render->input_glyph_offset; i++) {467 if (cursor_location == i + 1) {468 render->input_glyph_offset = i + 1;469 break;470 }471 }472473 /* If cursor position is after the current visible portion, adjust the offset forward */474 int pos = start_x;475476 for (size_t i = render->input_glyph_offset; i < count; i++) {477478 const struct fcft_glyph *glyph = glyphs[i];479 if (glyph == NULL)480 continue;481482 const int pixels_needed = max(glyph->x + glyph->width, glyph->advance.x);483484 /* Cursor is after this glyph? */485 if (cursor_location == i + 1) {486 /* If current glyph doesn't fit, adjust offset so that it does */487 if (pos + kerning[i] + pixels_needed > max_x) {488 int needed = (pos + kerning[i] + pixels_needed) - max_x;489490 /* Font may be variable width, meaning we may have to scroll multiple characters */491 while (needed > 0 && render->input_glyph_offset + 1 < count) {492 long scrolled_kerning;493 fcft_kerning(494 font,495 glyphs[render->input_glyph_offset]->cp,496 glyphs[render->input_glyph_offset + 1]->cp,497 &scrolled_kerning, NULL);498499 const int gained =500 scrolled_kerning +501 glyphs[render->input_glyph_offset]->advance.x;502503 needed -= gained;504 pos -= gained;505 render->input_glyph_offset++;506 }507 }508 }509510 pos += kerning[i] + glyph->advance.x;511 }512513 /* Pull in glyphs from the left, if there's space left */514 while (render->input_glyph_offset > 0 && max_x > pos) {515 int total_width = start_x;516517 for (size_t i = render->input_glyph_offset; i < count; i++) {518 const struct fcft_glyph *g = glyphs[i];519 if (g == NULL)520 continue;521522 const int w = max(g->width, g->advance.x);523524 if (total_width + kerning[i] + w > max_x) {525 break;526 }527528 total_width += kerning[i] + (i + 1 == count ? w : g->advance.x);529 }530531 const struct fcft_glyph *glyph = glyphs[render->input_glyph_offset - 1];532 const int pixels_needed = max(glyph->x + glyph->width, glyph->advance.x);533534 assert(render->input_glyph_offset > 0);535 assert(render->input_glyph_offset < count);536537 const long kern = kerning[render->input_glyph_offset];538 const int available = max_x - total_width;539 if (kern + pixels_needed > available)540 break;541542 render->input_glyph_offset--;543 pos += kern + glyph->advance.x;544 }545}546547void548render_prompt(struct render *render, struct buffer *buf,549 const struct prompt *prompt, const struct matches *matches)550{551 struct fcft_font *font = render->font;552 assert(font != NULL);553554 const struct config *conf = render->conf;555556 const char32_t *pprompt = prompt_prompt(prompt);557 size_t prompt_len = c32len(pprompt);558 const size_t cursor_location = prompt_cursor(prompt);559560 const char32_t *ptext = prompt_text(prompt);561 size_t text_len = c32len(ptext);562 bool use_placeholder = text_len == 0;563564 const enum fcft_subpixel subpixel =565 (render->conf->colors.background.a == 1. &&566 render->conf->colors.selection.a == 1.)567 ? render->subpixel : FCFT_SUBPIXEL_NONE;568569 const struct fcft_glyph *input_glyphs[text_len];570 long input_kerning[text_len];571572 for (size_t i = 0; i < text_len; i++) {573 input_glyphs[i] = fcft_rasterize_char_utf32(font, ptext[i], subpixel);574 if (i == 0)575 input_kerning[0] = 0;576 else577 fcft_kerning(font, ptext[i - 1], ptext[i], &input_kerning[i], NULL);578 }579580 if (use_placeholder) {581 ptext = prompt_placeholder(prompt);582 text_len = c32len(ptext);583 }584585 int x = render->border_size + render->x_margin;586 int y = render->border_size + render->y_margin + render_baseline(render);587588 /* Erase background */589 pixman_color_t bg = render->pix_background_color;590 //pixman_color_t bg = (pixman_color_t){0xffff, 0, 0, 0xffff};591 pixman_image_fill_rectangles(592 PIXMAN_OP_SRC, buf->pix[0], &bg, 1,593 &(pixman_rectangle16_t){594 render->border_size + render->x_margin - render->x_margin / 3,595 render->border_size + render->y_margin,596 buf->width - 2 * (render->border_size + render->x_margin - render->x_margin / 3),597 render->row_height});598599#if 0600 bg = (pixman_color_t){0, 0xffff, 0, 0xffff};601 pixman_image_fill_rectangles(602 PIXMAN_OP_SRC, buf->pix[0], &bg, 1,603 &(pixman_rectangle16_t){604 render->border_size + render->x_margin,605 render->border_size + render->y_margin,606 buf->width - 2 * (render->border_size + render->x_margin),607 render->row_height});608#endif609610 int stats_width = conf->match_counter611 ? render_match_count(render, buf, prompt, matches)612 : 0;613614 const int max_x =615 buf->width - render->border_size - render->x_margin - stats_width;616617 struct fcft_text_run *prompt_run = render->prompt_text_run;618 struct fcft_text_run *input_run =619 use_placeholder ? render->placeholder_text_run : NULL;620621 if (fcft_capabilities() & FCFT_CAPABILITY_TEXT_RUN_SHAPING) {622 if (prompt_run == NULL) {623 prompt_run = fcft_rasterize_text_run_utf32(624 font, prompt_len, pprompt, subpixel);625 }626 if (input_run == NULL && use_placeholder) {627 input_run = fcft_rasterize_text_run_utf32(628 font, text_len, ptext, subpixel);629 }630 }631632 if (prompt_run != NULL) {633 for (size_t i = 0; i < prompt_run->count; i++) {634 const struct fcft_glyph *glyph = prompt_run->glyphs[i];635 const int pixels_needed = max(glyph->x + glyph->width, glyph->advance.x);636637 if (x + pixels_needed > max_x)638 goto out;639640 render_glyph(buf->pix[0], glyph, x, y, &render->pix_prompt_color);641 x += glyph->advance.x;642 }643 } else {644 char32_t prev = 0;645 for (size_t i = 0; i < prompt_len; i++) {646 const char32_t wc = pprompt[i];647 const struct fcft_glyph *glyph = fcft_rasterize_char_utf32(font, wc, subpixel);648649 if (glyph == NULL) {650 prev = wc;651 continue;652 }653654 long x_kern;655 fcft_kerning(font, prev, wc, &x_kern, NULL);656657 x += x_kern;658659 const int pixels_needed = max(glyph->x + glyph->width, glyph->advance.x);660 if (x + pixels_needed > max_x)661 goto out;662663 render_glyph(664 buf->pix[0], glyph, x, y, &render->pix_prompt_color);665 x += glyph->advance.x;666 x += pt_or_px_as_pixels(render, &render->conf->letter_spacing);667668 prev = wc;669 }670 }671672 /*673 * Adjust glyph offset (i.e. where in the input string we start674 * rendering), to ensure the portion containing the cursor is675 * always visible.676 */677 adjust_input_glyph_offset(678 render, prompt, !use_placeholder ? text_len : 0,679 input_glyphs, input_kerning, x, max_x);680681 /* Cursor, if right after the prompt. In all other cases, the682 * cursor will be rendered by the loop below */683 if (cursor_location == render->input_glyph_offset684 || (conf->password_mode.enabled &&685 conf->password_mode.character == U'\0'))686 {687 render_cursor(render, x, y, buf->pix[0]);688 }689690 if (input_run != NULL && !(conf->password_mode.enabled && !use_placeholder)) {691 /* We only shape the placeholder string */692 assert(use_placeholder);693694 for (size_t i = render->input_glyph_offset; i < input_run->count; i++) {695 const struct fcft_glyph *glyph = input_run->glyphs[i];696 const int pixels_needed = max(glyph->x + glyph->width, glyph->advance.x);697698 if (x + pixels_needed > max_x) {699 goto out;700 }701702 render_glyph(buf->pix[0], glyph, x, y,703 use_placeholder704 ? &render->pix_placeholder_color705 : &render->pix_input_color);706 x += glyph->advance.x;707708 /* Cursor */709 const int cur_cluster = input_run->cluster[i];710 const int next_cluster = i + 1 < input_run->count711 ? input_run->cluster[i + 1] : INT_MAX;712713 if (cursor_location > cur_cluster &&714 cursor_location <= next_cluster)715 {716 render_cursor(render, x, y, buf->pix[0]);717 }718 }719 } else {720 char32_t prev = 0;721722 for (size_t i = render->input_glyph_offset; i < text_len; i++) {723 if (conf->password_mode.enabled &&724 conf->password_mode.character == U'\0' &&725 !use_placeholder)726 {727 continue;728 }729730 const struct fcft_glyph *glyph = input_glyphs[i];731732 if (glyph == NULL) {733 prev = 0;734 continue;735 }736737 long x_kern;738 fcft_kerning(font, prev, glyph->cp, &x_kern, NULL);739740 x += x_kern;741742 const int pixels_needed = max(glyph->x + glyph->width, glyph->advance.x);743744 if (x + pixels_needed > max_x)745 goto out;746747 render_glyph(748 buf->pix[0], glyph, x, y,749 use_placeholder750 ? &render->pix_placeholder_color751 : &render->pix_input_color);752 x += glyph->advance.x;753 x += pt_or_px_as_pixels(render, &render->conf->letter_spacing);754755 /* Cursor */756 if (cursor_location > 0 && cursor_location - 1 == i)757 render_cursor(render, x, y, buf->pix[0]);758759 prev = glyph->cp;760 }761 }762763out:764 if (render->prompt_text_run == NULL)765 render->prompt_text_run = prompt_run;766 else if (prompt_run != render->prompt_text_run)767 fcft_text_run_destroy(prompt_run);768 if (use_placeholder && render->placeholder_text_run == NULL)769 render->placeholder_text_run = input_run;770 else if (!use_placeholder || input_run != render->placeholder_text_run)771 fcft_text_run_destroy(input_run);772}773774static void775render_match_text(pixman_image_t *pix, double *_x, double _y, double max_x,776 const char32_t *text, size_t match_count,777 const struct match_substring matches[static match_count],778 struct fcft_font *font, enum fcft_subpixel subpixel,779 int letter_spacing, int tabs,780 pixman_color_t regular_color, pixman_color_t match_color,781 struct fcft_text_run **run)782{783 int x = *_x;784 int y = _y;785786 const struct fcft_glyph **glyphs = NULL;787 int *clusters = NULL;788 long *kern = NULL;789 size_t count = 0;790791 if (*run == NULL &&792 (fcft_capabilities() & FCFT_CAPABILITY_TEXT_RUN_SHAPING))793 {794 *run = fcft_rasterize_text_run_utf32(795 font, c32len(text), text, subpixel);796 }797798 if (*run != NULL) {799 glyphs = (*run)->glyphs;800 clusters = (*run)->cluster;801 count = (*run)->count;802 } else {803 count = c32len(text);804 glyphs = xmalloc(count * sizeof(glyphs[0]));805 clusters = xmalloc(count * sizeof(clusters[0]));806 kern = xmalloc(count * sizeof(kern[0]));807808 for (size_t i = 0; i < count; i++) {809 const struct fcft_glyph *glyph = fcft_rasterize_char_utf32(810 font, text[i], subpixel);811 if (glyph == NULL) {812 glyphs[i] = NULL;813 continue;814 }815816 if (i > 0)817 fcft_kerning(font, text[i - 1], text[i], &kern[i], NULL);818 else819 kern[i] = 0;820821 glyphs[i] = glyph;822 clusters[i] = i;823 }824 }825826 for (size_t i = 0; i < count; i++) {827 if (text[clusters[i]] == U'\t') {828 const struct fcft_glyph *space =829 fcft_rasterize_char_utf32(font, U' ', subpixel);830831 if (space != NULL) {832 const size_t chars_to_next_tab_stop = tabs == 0833 ? 0834 : tabs - (clusters[i] % tabs);835 x += chars_to_next_tab_stop * space->advance.x;836 }837838 continue;839 }840841 bool is_match = false;842 for (size_t j = 0; j < match_count; j++) {843 const struct match_substring *match = &matches[j];844 assert(match->start >= 0);845846 if (clusters[i] >= match->start &&847 clusters[i] < match->start + match->len)848 {849 is_match = true;850 break;851 }852 }853854 if (x + (kern != NULL ? kern[i] : 0) + glyphs[i]->advance.x > max_x) {855 const struct fcft_glyph *ellipses =856 fcft_rasterize_char_utf32(font, U'…', subpixel);857858 if (ellipses != NULL)859 render_glyph(pix, ellipses, x, y, ®ular_color);860861 break;862 }863864 x += kern != NULL ? kern[i] : 0;865 render_glyph(pix, glyphs[i], x, y, is_match ? &match_color : ®ular_color);866 x += glyphs[i]->advance.x + letter_spacing;867 y += glyphs[i]->advance.y;868 }869870 if (*run == NULL) {871 free(kern);872 free(clusters);873 free(glyphs);874 }875876 *_x = x;877}878879#if defined(FUZZEL_ENABLE_SVG_LIBRSVG)880static void881render_svg_librsvg(const struct icon *icon, int x, int y, int size,882 cairo_t *cairo)883{884 RsvgHandle *svg = icon->svg;885886 cairo_save(cairo);887 cairo_set_operator(cairo, CAIRO_OPERATOR_ATOP);888889 #if LIBRSVG_CHECK_VERSION(2, 46, 0)890 if (cairo_status(cairo) == CAIRO_STATUS_SUCCESS) {891 const RsvgRectangle viewport = {892 .x = x,893 .y = y,894 .width = size,895 .height = size,896 };897898 cairo_rectangle(cairo, x, y, size, size);899 cairo_clip(cairo);900901 rsvg_handle_render_document(svg, cairo, &viewport, NULL);902 }903 #else904 RsvgDimensionData dim;905 rsvg_handle_get_dimensions(svg, &dim);906907 const double scale_x = size / dim.width;908 const double scale_y = size / dim.height;909 const double scale = scale_x < scale_y ? scale_x : scale_y;910911 const double height = dim.height * scale;912 const double width = dim.width * scale;913914 cairo_rectangle(cairo, x, y, height, width);915 cairo_clip(cairo);916917 /* Translate + scale. Note: order matters! */918 cairo_translate(cairo, x, y);919 cairo_scale(cairo, scale, scale);920921 if (cairo_status(cairo) == CAIRO_STATUS_SUCCESS)922 rsvg_handle_render_cairo(svg, cairo);923 #endif924 cairo_restore(cairo);925}926#endif /* FUZZEL_ENABLE_SVG_LIBRSVG */927928#if defined(FUZZEL_ENABLE_SVG_NANOSVG)929static void930render_svg_nanosvg(struct icon *icon, int x, int y, int size,931 pixman_image_t *pix, cairo_t *cairo, bool gamma_correct)932{933#if defined(FUZZEL_ENABLE_CAIRO)934 cairo_surface_flush(cairo_get_target(cairo));935#endif936937 pixman_image_t *img = NULL;938939 /* Look for a cached image, at the correct size */940 tll_foreach(icon->rasterized, it) {941 if (it->item.size == size) {942 img = it->item.pix;943 break;944 }945 }946947 if (img == NULL) {948 NSVGimage *svg = icon->svg;949 struct NSVGrasterizer *rast = nsvgCreateRasterizer();950951 if (rast == NULL)952 return;953954 /* For gamma-correct blending */955 uint8_t *data_16bit = NULL;956 pixman_image_t *img_16bit = NULL;957 uint64_t *abgr16 = NULL;958959 float scale = svg->width > svg->height ? size / svg->width : size / svg->height;960961 const int width = roundf(svg->width * scale);962 const int height = roundf(svg->height * scale);963964 uint8_t *data_8bit = xmalloc(width * height * 4);965966 nsvgRasterize(rast, svg, 0, 0, scale, data_8bit, width, height, width * 4);967968 img = pixman_image_create_bits_no_clear(969 PIXMAN_a8b8g8r8, width, height, (uint32_t *)data_8bit, width * 4);970971 if (gamma_correct) {972 data_16bit = xmalloc(width * height * 8);973 abgr16 = (uint64_t *)data_16bit;974975 img_16bit = pixman_image_create_bits_no_clear(976 PIXMAN_a16b16g16r16, width, height, (uint32_t *)data_16bit,977 width * 8);978 }979980 /* Nanosvg produces non-premultiplied ABGR, while pixman expects981 * premultiplied */982983 for (uint32_t *abgr = (uint32_t *)data_8bit;984 abgr < (uint32_t *)(data_8bit + width * height * 4);985 abgr++)986 {987 uint16_t alpha = (*abgr >> 24) & 0xff;988 uint16_t blue = (*abgr >> 16) & 0xff;989 uint16_t green = (*abgr >> 8) & 0xff;990 uint16_t red = (*abgr >> 0) & 0xff;991992 /*993 * TODO: decode sRGB -> linear here994 *995 * We should decode to 16-bit, meaning we have to prepare996 * a 16-bit buffer before looping the pixels.997 */998999 if (gamma_correct) {1000 if (alpha == 0x00)1001 blue = green = red = 0x00;1002 else {1003 alpha |= alpha << 8; /* Alpha already linear, expand to 16-bit */1004 blue = srgb_decode_8_to_16(blue);1005 green = srgb_decode_8_to_16(green);1006 red = srgb_decode_8_to_16(red);10071008 blue = blue * alpha / 0xffff;1009 green = green * alpha / 0xffff;1010 red = red * alpha / 0xffff;1011 }10121013 *abgr16 = (uint64_t)alpha << 48 | (uint64_t)blue << 32 | (uint64_t)green << 16 | (uint64_t)red;1014 abgr16++;1015 } else {1016 if (alpha == 0x00)1017 blue = green = red = 0x00;1018 else {1019 blue = (uint8_t)(blue * alpha / 0xff);1020 green = (uint8_t)(green * alpha / 0xff);1021 red = (uint8_t)(red * alpha / 0xff);1022 }10231024 *abgr = (uint32_t)alpha << 24 | blue << 16 | green << 8 | red;1025 }1026 }10271028 if (gamma_correct) {1029 /* Replace default image buffer with 16-bit linear buffer */1030 pixman_image_unref(img);1031 free(data_8bit);1032 img = img_16bit;1033 }10341035 nsvgDeleteRasterizer(rast);10361037 tll_push_back(icon->rasterized, ((struct rasterized){img, size}));1038 }10391040 const int w = pixman_image_get_width(img);1041 const int h = pixman_image_get_height(img);10421043 pixman_image_composite32(1044 PIXMAN_OP_OVER, img, NULL, pix, 0, 0, 0, 0,1045 x + (size - w) / 2,1046 y + (size - h) / 2,1047 w, h);10481049#if defined(FUZZEL_ENABLE_CAIRO)1050 cairo_surface_mark_dirty(cairo_get_target(cairo));1051#endif1052}1053#endif /* FUZZEL_ENABLE_SVG_NANOSVG */10541055static void1056render_svg(struct icon *icon, int x, int y, int size,1057 pixman_image_t *pix, cairo_t *cairo, bool gamma_correct,1058 bool print_timing_info)1059{1060 assert(icon->type == ICON_SVG);10611062 if (icon->svg == NULL) {1063 struct timeval start, stop, diff;1064 gettimeofday(&start, NULL);10651066 if (!icon_from_svg(icon, icon->path))1067 return;10681069 if (print_timing_info) {1070 gettimeofday(&stop, NULL);1071 timersub(&stop, &start, &diff);10721073 LOG_WARN("%s loaded in %llus %lluµs",1074 icon->path,1075 (unsigned long long)diff.tv_sec,1076 (unsigned long long)diff.tv_usec);1077 }10781079 LOG_DBG("%s", icon->path);1080 }10811082 struct timeval start, stop, diff;1083 gettimeofday(&start, NULL);10841085#if defined(FUZZEL_ENABLE_SVG_LIBRSVG)1086 render_svg_librsvg(icon, x, y, size, cairo);1087#elif defined(FUZZEL_ENABLE_SVG_NANOSVG)1088 render_svg_nanosvg(icon, x, y, size, pix, cairo, gamma_correct);1089#endif10901091 if (print_timing_info) {1092 gettimeofday(&stop, NULL);1093 timersub(&stop, &start, &diff);10941095 LOG_WARN("%s rendered in %llus %lluµs",1096 icon->path,1097 (unsigned long long)diff.tv_sec,1098 (unsigned long long)diff.tv_usec);1099 }1100}11011102#if defined(FUZZEL_ENABLE_PNG_LIBPNG)1103static void1104render_png_libpng(struct icon *icon, int x, int y, int size,1105 pixman_image_t *pix, cairo_t *cairo,1106 enum scaling_filter scaling_filter)1107{1108#if defined(FUZZEL_ENABLE_CAIRO)1109 cairo_surface_flush(cairo_get_target(cairo));1110#endif11111112 pixman_image_t *png = icon->png;1113 pixman_format_code_t fmt = pixman_image_get_format(png);1114 int height = pixman_image_get_height(png);1115 int width = pixman_image_get_width(png);11161117 if (height > size || width > size) {1118 double scale = (double)size / (height > width ? height : width);11191120 pixman_f_transform_t _scale_transform;1121 pixman_f_transform_init_scale(&_scale_transform, 1. / scale, 1. / scale);11221123 pixman_transform_t scale_transform;1124 pixman_transform_from_pixman_f_transform(1125 &scale_transform, &_scale_transform);1126 pixman_image_set_transform(png, &scale_transform);11271128 const int largest_side = max(width, height);11291130 if (largest_side >= 1024) {1131 if (!icon->png_size_warned) {1132 LOG_WARN(1133 "%s: PNG is too large (%dx%d); "1134 "downscaling using a less precise filter (fast)",1135 icon->path, width, height);1136 icon->png_size_warned = true;1137 }11381139 pixman_image_set_filter(png, PIXMAN_FILTER_FAST, NULL, 0);1140 }11411142 else {11431144 bool slow_scaling_filter = false;11451146 switch (scaling_filter) {1147 case SCALING_FILTER_NONE:1148 case SCALING_FILTER_NEAREST:1149 case SCALING_FILTER_BILINEAR:1150 case SCALING_FILTER_BOX:1151 slow_scaling_filter = false;1152 break;11531154 case SCALING_FILTER_CUBIC:1155 case SCALING_FILTER_LANCZOS3:1156 case SCALING_FILTER_LINEAR:1157 case SCALING_FILTER_LANCZOS2:1158 case SCALING_FILTER_LANCZOS3_STRETCHED:1159 slow_scaling_filter = true;1160 break;1161 }11621163 if (slow_scaling_filter && largest_side >= 256) {1164 if (!icon->png_size_warned) {1165 LOG_WARN(1166 "%s: PNG is too large (%dx%d); "1167 "downscaling using a less precise filter (box)",1168 icon->path, width, height);1169 icon->png_size_warned = true;1170 }11711172 scaling_filter = SCALING_FILTER_BOX;1173 }11741175 switch (scaling_filter) {1176 case SCALING_FILTER_NONE:1177 break;11781179 /*1180 * "simple" filters1181 */11821183 case SCALING_FILTER_NEAREST:1184 pixman_image_set_filter(png, PIXMAN_FILTER_NEAREST, NULL, 0);1185 break;11861187 case SCALING_FILTER_BILINEAR:1188 pixman_image_set_filter(png, PIXMAN_FILTER_BILINEAR, NULL, 0);1189 break;11901191 /*1192 * Separable convolution filters1193 */1194 case SCALING_FILTER_CUBIC:1195 case SCALING_FILTER_LANCZOS3:1196 case SCALING_FILTER_BOX:1197 case SCALING_FILTER_LINEAR:1198 case SCALING_FILTER_LANCZOS2:1199 case SCALING_FILTER_LANCZOS3_STRETCHED: {12001201 pixman_kernel_t kernel;12021203 switch (scaling_filter) {1204 case SCALING_FILTER_CUBIC: kernel = PIXMAN_KERNEL_CUBIC; break;1205 case SCALING_FILTER_LANCZOS3: kernel = PIXMAN_KERNEL_LANCZOS3; break;1206 case SCALING_FILTER_BOX: kernel = PIXMAN_KERNEL_BOX; break;1207 case SCALING_FILTER_LINEAR: kernel = PIXMAN_KERNEL_LINEAR; break;1208 case SCALING_FILTER_LANCZOS2: kernel = PIXMAN_KERNEL_LANCZOS2; break;1209 case SCALING_FILTER_LANCZOS3_STRETCHED: kernel = PIXMAN_KERNEL_LANCZOS3_STRETCHED; break;1210 default: assert(false); kernel = PIXMAN_KERNEL_CUBIC; break;1211 }12121213 int param_count = 0;1214 pixman_fixed_t *params = pixman_filter_create_separable_convolution(1215 ¶m_count,1216 pixman_double_to_fixed(1. / scale),1217 pixman_double_to_fixed(1. / scale),1218 kernel, kernel,1219 kernel, kernel,1220 pixman_int_to_fixed(1),1221 pixman_int_to_fixed(1));12221223 if (params != NULL || param_count == 0) {1224 pixman_image_set_filter(1225 png, PIXMAN_FILTER_SEPARABLE_CONVOLUTION,1226 params, param_count);1227 }12281229 free(params);1230 break;1231 }1232 }1233 }12341235 width *= scale;1236 height *= scale;12371238 int stride = stride_for_format_and_width(fmt, width);1239 uint8_t *data = xmalloc(height * stride);1240 pixman_image_t *scaled_png = pixman_image_create_bits_no_clear(1241 fmt, width, height, (uint32_t *)data, stride);1242 pixman_image_composite32(1243 PIXMAN_OP_SRC, png, NULL, scaled_png, 0, 0, 0, 0, 0, 0, width, height);12441245 free(pixman_image_get_data(png));1246 pixman_image_unref(png);12471248 png = scaled_png;1249 icon->png = png;1250 }12511252 pixman_image_composite32(1253 PIXMAN_OP_OVER, png, NULL, pix, 0, 0, 0, 0,1254 x + (size - width) / 2,1255 y + (size - height) / 2,1256 width, height);12571258#if defined(FUZZEL_ENABLE_CAIRO)1259 cairo_surface_mark_dirty(cairo_get_target(cairo));1260#endif1261}1262#endif /* FUZZEL_ENABLE_PNG_LIBPNG */12631264static void1265render_png(struct icon *icon, int x, int y, int size, pixman_image_t *pix,1266 cairo_t *cairo, bool gamma_correct, bool print_timing_info,1267 enum scaling_filter scaling_filter)1268{1269 assert(icon->type == ICON_PNG);12701271 if (icon->png == NULL) {1272 struct timeval start, stop, diff;1273 gettimeofday(&start, NULL);12741275 if (!icon_from_png(icon, icon->path, gamma_correct))1276 return;12771278 if (print_timing_info) {1279 gettimeofday(&stop, NULL);1280 timersub(&stop, &start, &diff);1281 LOG_WARN("%s loaded in %llus %lluµs",1282 icon->path,1283 (unsigned long long)diff.tv_sec,1284 (unsigned long long)diff.tv_usec);1285 }12861287 LOG_DBG("%s", icon->path);1288 }12891290 struct timeval start, stop, diff;1291 gettimeofday(&start, NULL);12921293#if defined(FUZZEL_ENABLE_PNG_LIBPNG)1294 render_png_libpng(icon, x, y, size, pix, cairo, scaling_filter);1295#endif12961297 if (print_timing_info) {1298 gettimeofday(&stop, NULL);1299 timersub(&stop, &start, &diff);1300 LOG_WARN("%s rendered in %llus %lluµs",1301 icon->path,1302 (unsigned long long)diff.tv_sec,1303 (unsigned long long)diff.tv_usec);1304 }1305}13061307static int1308first_row_y(const struct render *render)1309{1310 return (render->border_size +1311 render->y_margin +1312 (render->conf->hide_prompt ? 0 : render->row_height) +1313 (render->conf->hide_prompt ? 0 : render->inner_pad));1314}13151316static void1317render_match_entry_background(const struct render *render,1318 int idx, int row_count,1319 pixman_image_t *pix, int width)1320{1321 pixman_color_t bg = render->pix_background_color;13221323 const int sel_margin = render->x_margin / 3;13241325 const int x = render->border_size + render->x_margin - sel_margin;1326 const int y = first_row_y(render) + idx * render->row_height;1327 const int w = width - 2 * (render->border_size + render->x_margin - sel_margin);1328 const int h = row_count * render->row_height;13291330 pixman_image_fill_rectangles(1331 PIXMAN_OP_SRC, pix, &bg, 1, &(pixman_rectangle16_t){x, y, w, h});1332}13331334static void1335render_selected_match_entry_background(struct render *render,1336 int idx, pixman_image_t *pix, int width)1337{1338 pixman_color_t bg = render->pix_selection_color;13391340 const int sel_margin = render->x_margin / 3;13411342 const int x = render->border_size + render->x_margin - sel_margin;1343 const int y = first_row_y(render) + idx * render->row_height;1344 const int w = width - 2 * (render->border_size + render->x_margin - sel_margin);1345 const int h = 1 * render->row_height;13461347 // limit radius to half of height, any larger and it causes weird shapes1348 // also limit it when horizontal padding is small, to prevent icon pop out1349 const unsigned int radius = min(1350 min(render->selection_border_radius, h / 2), render->x_margin);13511352 if (render->selection_corners == NULL) {1353 render->selection_corners = pixman_image_create_bits(1354 pixman_image_get_format(pix), w, h, NULL, w * 4);1355 render_rounded_rectangle(render->selection_corners, &bg, &bg, radius, 0, 0, 0, w, h);1356 }1357 pixman_image_composite(1358 PIXMAN_OP_OVER, render->selection_corners, NULL, pix,1359 0, 0, 0, 0, x, y, w, h);1360}13611362static void1363render_one_match_entry(struct render *render, const struct matches *matches,1364 const struct match *match, bool render_icons,1365 int idx, bool is_selected, int width, int height,1366 pixman_image_t *pix, cairo_t *cairo)1367{1368 const enum fcft_subpixel subpixel =1369 (render->conf->colors.background.a == 1. &&1370 render->conf->colors.selection.a == 1.)1371 ? render->subpixel : FCFT_SUBPIXEL_NONE;13721373 struct fcft_font *font =1374 is_selected ? render->font_bold : render->font;1375 const struct fcft_glyph *ellipses =1376 fcft_rasterize_char_utf32(font, U'…', subpixel);13771378 const int first_row = first_row_y(render);1379 double cur_x = render->border_size + render->x_margin;1380 double max_x = width - render->border_size - render->x_margin;13811382 render_match_entry_background(render, idx, 1, pix, width);13831384 if (is_selected) {1385 render_selected_match_entry_background(render, idx, pix, width);13861387 /* If currently selected item has a scalable icon, and if1388 * there's "enough" free space, render a large representation1389 * of the icon */13901391 const double ratio = render->conf->image_size_ratio;1392 const double size = min(height * ratio, width * ratio);1393 const double img_x = (width - size) / 2.;1394 const double img_y_bottom = max(height - first_row, 0.);1395 const double img_y = max(img_y_bottom - size, 0.);1396 const size_t match_count = matches_get_count(matches);1397 const double list_end = first_row + match_count * render->row_height;13981399 LOG_DBG("img_y=%f, list_end=%f", img_y, list_end);14001401 if (render_icons &&1402 match->application->icon.type == ICON_SVG &&1403 img_y > list_end + render->row_height)1404 {1405 render_svg(&match->application->icon, img_x, img_y, size, pix, cairo,1406 render->gamma_correct, render->conf->print_timing_info);1407 }1408 }14091410 if (render_icons) {1411 struct icon *icon = &match->application->icon;1412 const int size = render->icon_height;1413 const int img_x = cur_x;1414 const int img_y = first_row + idx * render->row_height + (render->row_height - size) / 2;14151416 switch (icon->type) {1417 case ICON_NONE:1418 break;14191420 case ICON_PNG:1421 render_png(icon, img_x, img_y, size, pix, cairo,1422 render->gamma_correct, render->conf->print_timing_info,1423 render->conf->png_scaling_filter);1424 break;14251426 case ICON_SVG:1427 render_svg(icon, img_x, img_y, size, pix, cairo,1428 render->gamma_correct, render->conf->print_timing_info);1429 break;1430 }1431 }14321433 const struct fcft_glyph *space =1434 fcft_rasterize_char_utf32(render->font, U' ', subpixel);14351436 cur_x +=1437 (render->conf->icons_enabled && matches_have_icons(matches)1438 ? (render->row_height +1439 (space != NULL ? space->advance.x : render->font->max_advance.x))1440 : 0) +1441 pt_or_px_as_pixels(render, &render->conf->letter_spacing);14421443 /* Replace newlines in title, with spaces (basic support for1444 * multiline entries) */1445 if (match->application->render_title == NULL) {1446 char32_t *newline = c32chr(match->application->title, U'\n');14471448 if (newline != NULL) {1449 char32_t *render_title = xc32dup(match->application->title);14501451 newline = render_title + (newline - match->application->title);1452 *newline = U' ';14531454 while ((newline = c32chr(newline, U'\n')) != NULL)1455 *newline = U' ';14561457 match->application->render_title = render_title;1458 } else {1459 /* No newlines, use title as-is */1460 match->application->render_title = match->application->title;1461 }1462 }14631464 const int y = first_row + render_baseline(render) + idx * render->row_height;14651466 /* Application title */1467 render_match_text(1468 pix, &cur_x, y, max_x - (ellipses != NULL ? ellipses->width : 0),1469 match->application->render_title, match->pos_count, match->pos,1470 font, subpixel,1471 pt_or_px_as_pixels(render, &render->conf->letter_spacing),1472 render->conf->tabs,1473 (is_selected1474 ? render->pix_selection_text_color1475 : render->pix_text_color),1476 (is_selected1477 ? render->pix_selection_match_color1478 : render->pix_match_color),1479 (is_selected1480 ? &match->application->shaped_bold1481 : &match->application->shaped));1482}14831484void1485render_match_list(struct render *render, struct buffer *buf,1486 const struct prompt *prompt, const struct matches *matches)1487{1488 const size_t match_count = matches_get_count(matches);1489 const size_t selected = matches_get_match_index(matches);14901491 assert(match_count == 0 || selected < match_count);14921493 bool render_icons = mtx_trylock(render->icon_lock) == thrd_success;14941495 if (render->workers.count > 0) {1496 mtx_lock(&render->workers.lock);1497 render->workers.matches = matches;1498 render->workers.buf = buf;1499 render->workers.render_icons= render_icons;15001501 for (size_t i = 0; i < render->workers.count; i++)1502 sem_post(&render->workers.start);15031504 assert(tll_length(render->workers.queue) == 0);1505 }15061507 /* Erase background of the "empty" area, after the last match */1508 const size_t effective_lines = matches_max_matches_per_page(matches);1509 render_match_entry_background(1510 render, match_count, effective_lines - match_count,1511 buf->pix[0], buf->width);15121513 for (size_t i = 0; i < match_count; i++) {1514 if (render->workers.count == 0) {1515 const struct match *match = matches_get(matches, i);1516 render_one_match_entry(1517 render, matches, match, render_icons, i, i == selected,1518 buf->width, buf->height, buf->pix[0],1519#if defined(FUZZEL_ENABLE_CAIRO)1520 buf->cairo[0]1521#else1522 NULL1523#endif1524 );1525 } else {1526 tll_push_back(render->workers.queue, i);1527 }1528 }15291530 if (render->workers.count > 0) {1531 for (size_t i = 0; i < render->workers.count; i++)1532 tll_push_back(render->workers.queue, -1);1533 mtx_unlock(&render->workers.lock);15341535 for (size_t i = 0; i < render->workers.count; i++)1536 sem_wait(&render->workers.done);15371538 render->workers.matches = NULL;1539 render->workers.buf = NULL;1540 render->workers.render_icons = false;1541 }15421543 if (render_icons)1544 mtx_unlock(render->icon_lock);1545}15461547/* THREAD */1548static int1549render_worker_thread(void *_ctx)1550{1551 struct thread_context *ctx = _ctx;1552 struct render *render = ctx->render;1553 const int my_id = ctx->my_id;1554 free(ctx);15551556 sigset_t mask;1557 sigfillset(&mask);1558 pthread_sigmask(SIG_SETMASK, &mask, NULL);15591560 char proc_title[16];1561 xsnprintf(proc_title, sizeof(proc_title), "fuzzel:rend:%d", my_id);15621563 if (pthread_setname_np(pthread_self(), proc_title) < 0)1564 LOG_ERRNO("render worker %d: failed to set process title", my_id);15651566 sem_t *start = &render->workers.start;1567 sem_t *done = &render->workers.done;1568 mtx_t *lock = &render->workers.lock;15691570 while (true) {1571 sem_wait(start);15721573 const struct matches *matches = render->workers.matches;1574 struct buffer *buf = render->workers.buf;1575 const bool render_icons = render->workers.render_icons;1576 const size_t selected = matches != NULL1577 ? matches_get_match_index(matches)1578 : 0;15791580 bool frame_done = false;15811582 while (!frame_done) {1583 mtx_lock(lock);1584 assert(tll_length(render->workers.queue) > 0);15851586 int row_no = tll_pop_front(render->workers.queue);1587 mtx_unlock(lock);15881589 switch (row_no) {1590 default: {1591 assert(buf != NULL);15921593 const struct match *match = matches_get(matches, row_no);1594 render_one_match_entry(1595 render, matches, match, render_icons,1596 row_no, row_no == selected, buf->width, buf->height,1597 buf->pix[my_id],1598#if defined(FUZZEL_ENABLE_CAIRO)1599 buf->cairo[my_id]1600#else1601 NULL1602#endif1603 );16041605 break;1606 }16071608 case -1:1609 frame_done = true;1610 sem_post(done);1611 break;16121613 case -2:1614 return 0;1615 }1616 }1617 }16181619 return -1;1620}16211622struct render *1623render_init(const struct config *conf, mtx_t *icon_lock)1624{1625 struct render *render = xcalloc(1, sizeof(*render));1626 *render = (struct render){1627 .conf = conf,1628 .icon_lock = icon_lock,1629 };16301631 if (sem_init(&render->workers.start, 0, 0) < 0 ||1632 sem_init(&render->workers.done, 0, 0) < 0)1633 {1634 LOG_ERRNO("failed to instantiate render worker semaphores");1635 goto err_free_render;1636 }16371638 int err;1639 if ((err = mtx_init(&render->workers.lock, mtx_plain)) != thrd_success) {1640 LOG_ERR("failed to instantiate render worker mutex: %d", err);1641 goto err_free_semaphores;1642 }16431644 const size_t num_workers = min(conf->render_worker_count, conf->lines);16451646 render->workers.threads = xcalloc(1647 num_workers, sizeof(render->workers.threads[0]));16481649 for (size_t i = 0; i < num_workers; i++) {1650 struct thread_context *ctx = xmalloc(sizeof(*ctx));1651 *ctx = (struct thread_context){1652 .render = render,1653 .my_id = 1 + i,1654 };16551656 int ret = thrd_create(1657 &render->workers.threads[i], &render_worker_thread, ctx);16581659 if (ret != thrd_success) {1660 LOG_ERR("failed to create render worker thread: %d", ret);1661 render->workers.threads[i] = 0;1662 goto err_free_semaphores_and_lock;1663 }16641665 render->workers.count++;1666 }16671668 LOG_INFO("using %hu render worker threads", render->workers.count);16691670 return render;16711672err_free_semaphores_and_lock:1673 mtx_destroy(&render->workers.lock);1674err_free_semaphores:1675 sem_destroy(&render->workers.start);1676 sem_destroy(&render->workers.done);1677err_free_render:1678 free(render);1679 return NULL;1680}16811682void1683render_initialize_colors(struct render *render, const struct config *conf,1684 bool gamma_correct)1685{1686 render->gamma_correct = gamma_correct;1687 render->pix_background_color = rgba2pixman(gamma_correct, conf->colors.background);1688 render->pix_border_color = rgba2pixman(gamma_correct, conf->colors.border);1689 render->pix_text_color = rgba2pixman(gamma_correct, conf->colors.text);1690 render->pix_prompt_color = rgba2pixman(gamma_correct, conf->colors.prompt);1691 render->pix_input_color = rgba2pixman(gamma_correct, conf->colors.input);1692 render->pix_match_color = rgba2pixman(gamma_correct, conf->colors.match);1693 render->pix_selection_color = rgba2pixman(gamma_correct, conf->colors.selection);1694 render->pix_selection_text_color = rgba2pixman(gamma_correct, conf->colors.selection_text);1695 render->pix_selection_match_color = rgba2pixman(gamma_correct, conf->colors.selection_match);1696 render->pix_counter_color = rgba2pixman(gamma_correct, conf->colors.counter);1697 render->pix_placeholder_color = rgba2pixman(gamma_correct, conf->colors.placeholder);1698}16991700void1701render_set_subpixel(struct render *render, enum fcft_subpixel subpixel)1702{1703 render->subpixel = subpixel;1704}17051706void1707render_resized(struct render *render, int *new_width, int *new_height)1708{1709 struct fcft_font *font = render->font;1710 const float scale = render->scale;17111712 assert(font != NULL);1713 assert(render->font_bold != NULL);17141715 const struct fcft_glyph *W = fcft_rasterize_char_utf32(1716 font, U'o', render->subpixel);17171718 const unsigned x_margin = render->conf->pad.x * scale;1719 const unsigned y_margin = render->conf->pad.y * scale;1720 const unsigned inner_pad = render->conf->lines > 01721 ? render->conf->pad.inner * scale1722 : 0;17231724 const unsigned border_size = render->conf->border.size * scale;1725 const unsigned border_radius = render->conf->border.radius * scale;1726 const unsigned selection_border_radius = render->conf->selection_border.radius * scale;17271728 const unsigned row_height = render->conf->line_height.px >= 01729 ? pt_or_px_as_pixels(render, &render->conf->line_height)1730 : max(font->height, font->ascent + font->descent);17311732 const unsigned icon_height = max(0, row_height - font->descent);17331734 const unsigned height =1735 border_size + /* Top border */1736 y_margin +1737 (render->conf->hide_prompt ? 0 : row_height) + /* The prompt (hidden if hide_prompt) */1738 (render->conf->hide_prompt ? 0 : inner_pad) + /* Padding between prompt and matches (only if prompt shown) */1739 render->conf->lines * row_height + /* Matches */1740 y_margin +1741 border_size; /* Bottom border */17421743 const unsigned width =1744 border_size + /* Top border */1745 x_margin +1746 (max((W->advance.x + pt_or_px_as_pixels(1747 render, &render->conf->letter_spacing)),1748 0)1749 * render->conf->chars) +1750 x_margin +1751 border_size;17521753 LOG_DBG("x-margin: %d, y-margin: %d, border-size: %d, border-radius: %d, "1754 "row-height: %d, icon-height: %d, height: %d, width: %d, scale: %f",1755 x_margin, y_margin, border_size, border_radius,1756 row_height, icon_height, height, width, scale);17571758 render->y_margin = y_margin;1759 render->x_margin = x_margin;1760 render->inner_pad = inner_pad;1761 render->border_size = border_size;1762 render->border_radius = border_radius;1763 render->selection_border_radius = selection_border_radius;1764 render->row_height = row_height;1765 render->icon_height = icon_height;17661767 if (new_width != NULL)1768 *new_width = width;1769 if (new_height != NULL)1770 *new_height = height;17711772 /* invalidate cached corners since the size could have changed */1773 if (render->selection_corners != NULL) {1774 pixman_image_unref(render->selection_corners);1775 render->selection_corners = NULL;1776 }1777}17781779bool1780render_set_font_and_update_sizes(struct render *render, struct fcft_font *font,1781 struct fcft_font *font_bold,1782 float scale, float dpi, bool size_font_by_dpi,1783 int *new_width, int *new_height)1784{1785 if (font != NULL) {1786 fcft_destroy(render->font);1787 render->font = font;1788 } else {1789 assert(render->font != NULL);1790 font = render->font;1791 }17921793 if (font_bold != NULL) {1794 fcft_destroy(render->font_bold);1795 render->font_bold = font_bold;1796 } else {1797 assert(render->font_bold != NULL);1798 font_bold = render->font_bold;1799 }18001801 render->scale = scale;1802 render->dpi = dpi;1803 render->size_font_by_dpi = size_font_by_dpi;18041805 render_resized(render, new_width, new_height);1806 return true;1807}18081809int1810render_icon_size(const struct render *render)1811{1812 return render->icon_height;1813}18141815void1816render_destroy(struct render *render)1817{1818 if (render == NULL)1819 return;18201821 mtx_lock(&render->workers.lock);1822 {1823 assert(tll_length(render->workers.queue) == 0);18241825 for (size_t i = 0; i < render->workers.count; i++) {1826 assert(render->workers.threads[i] != 0);1827 sem_post(&render->workers.start);1828 tll_push_back(render->workers.queue, -2);1829 }1830 }1831 mtx_unlock(&render->workers.lock);18321833 for (size_t i = 0; i < render->workers.count; i++) {1834 assert(render->workers.threads[i] != 0);1835 thrd_join(render->workers.threads[i], NULL);1836 }18371838 free(render->workers.threads);1839 mtx_destroy(&render->workers.lock);1840 sem_destroy(&render->workers.start);1841 sem_destroy(&render->workers.done);1842 assert(tll_length(render->workers.queue) == 0);1843 tll_free(render->workers.queue);18441845 fcft_text_run_destroy(render->prompt_text_run);1846 fcft_text_run_destroy(render->placeholder_text_run);18471848 if (render->selection_corners != NULL)1849 pixman_image_unref(render->selection_corners);18501851 fcft_destroy(render->font);1852 fcft_destroy(render->font_bold);1853 free(render);1854}18551856ssize_t1857render_get_row_num(const struct render *render, int width, int x, int y,1858 const struct matches *matches)1859{1860 const int y_margin = render->y_margin;1861 const int inner_pad = render->inner_pad;1862 const int border_size = render->border_size;1863 const int row_height = render->row_height;18641865 const int min_x = render->border_size + render->x_margin - render->x_margin / 3;1866 const int max_x = width - (min_x);18671868 const int first_row = 1 * border_size + y_margin +1869 (render->conf->hide_prompt ? 0 : row_height) +1870 (render->conf->hide_prompt ? 0 : inner_pad);18711872 const size_t match_count = matches_get_count(matches);1873 const size_t last_row = first_row + match_count * row_height;18741875 ssize_t row = -1;18761877 if (y >= first_row && y < last_row && x >= min_x && x < max_x)1878 row = (y - first_row) / row_height;18791880 return row;1881}18821883void1884render_flush_text_run_cache(struct render *render)1885{1886 fcft_text_run_destroy(render->prompt_text_run);1887 fcft_text_run_destroy(render->placeholder_text_run);1888 render->prompt_text_run = NULL;1889 render->placeholder_text_run = NULL;1890}