diff options
| author | Manuel Stoeckl <code@mstoeckl.com> | 2023-01-14 14:51:47 -0500 |
|---|---|---|
| committer | Simon Ser <contact@emersion.fr> | 2023-01-27 12:19:32 +0100 |
| commit | 10ab8b274020a9323413d093c45f13490929fb81 (patch) | |
| tree | 9846d987bbb572177fe08b051c5217a89ac4ef1b /render.c | |
| parent | b4e3a2b0fdda3fcbc40e19c94bb7a7961aa7653c (diff) | |
Estimate buffer size in advance for render_frame
The size of the surface used to draw the indicator depends on
the extents of the text being drawn on and under the indicator.
This commit refactors the `render_frame` function so that the surface
size is computed before drawing; before, `render_frame` drew onto a
buffer, estimated the size the buffer should have had, and recursively
called itself to try again with the estimated size, if necessary. This
was done because Cairo's methods to estimate font and text size
require that a cairo_t context object and an associated cairo_surface
already have been set up. Since the surface size depends on the text
size, the natural way to use Cairo would have a circular dependency.
In order to compute sizes _before_ the buffer is created, this commit
adds a 1x1 surface and a matching `test_cairo` context which is set to
the same font and drawing parameters as the buffer that will be
created. Font/text extent measurements should give the same results as
for the final buffer.
Diffstat (limited to 'render.c')
| -rw-r--r-- | render.c | 192 |
1 files changed, 106 insertions, 86 deletions
@@ -68,17 +68,114 @@ void render_frame_background(struct swaylock_surface *surface) { wl_surface_commit(surface->surface); } +static void configure_font_drawing(cairo_t *cairo, struct swaylock_state *state, + enum wl_output_subpixel subpixel, int arc_radius) { + cairo_font_options_t *fo = cairo_font_options_create(); + cairo_font_options_set_hint_style(fo, CAIRO_HINT_STYLE_FULL); + cairo_font_options_set_antialias(fo, CAIRO_ANTIALIAS_SUBPIXEL); + cairo_font_options_set_subpixel_order(fo, to_cairo_subpixel_order(subpixel)); + + cairo_set_font_options(cairo, fo); + cairo_select_font_face(cairo, state->args.font, + CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); + if (state->args.font_size > 0) { + cairo_set_font_size(cairo, state->args.font_size); + } else { + cairo_set_font_size(cairo, arc_radius / 3.0f); + } + cairo_font_options_destroy(fo); +} + void render_frame(struct swaylock_surface *surface) { struct swaylock_state *state = surface->state; + // First, compute the text that will be drawn, if any, since this + // determines the size/positioning of the surface + + char attempts[4]; // like i3lock: count no more than 999 + char *text = NULL; + const char *layout_text = NULL; + + if (state->args.show_indicator) { + switch (state->auth_state) { + case AUTH_STATE_VALIDATING: + text = "Verifying"; + break; + case AUTH_STATE_INVALID: + text = "Wrong"; + break; + case AUTH_STATE_CLEAR: + text = "Cleared"; + break; + case AUTH_STATE_INPUT: + case AUTH_STATE_INPUT_NOP: + case AUTH_STATE_BACKSPACE: + // Caps Lock has higher priority + if (state->xkb.caps_lock && state->args.show_caps_lock_text) { + text = "Caps Lock"; + } else if (state->args.show_failed_attempts && + state->failed_attempts > 0) { + if (state->failed_attempts > 999) { + text = "999+"; + } else { + snprintf(attempts, sizeof(attempts), "%d", state->failed_attempts); + text = attempts; + } + } + + xkb_layout_index_t num_layout = xkb_keymap_num_layouts(state->xkb.keymap); + if (!state->args.hide_keyboard_layout && + (state->args.show_keyboard_layout || num_layout > 1)) { + xkb_layout_index_t curr_layout = 0; + + // advance to the first active layout (if any) + while (curr_layout < num_layout && + xkb_state_layout_index_is_active(state->xkb.state, + curr_layout, XKB_STATE_LAYOUT_EFFECTIVE) != 1) { + ++curr_layout; + } + // will handle invalid index if none are active + layout_text = xkb_keymap_layout_get_name(state->xkb.keymap, curr_layout); + } + break; + default: + break; + } + } + + // Compute the size of the buffer needed int arc_radius = state->args.radius * surface->scale; int arc_thickness = state->args.thickness * surface->scale; int buffer_diameter = (arc_radius + arc_thickness) * 2; + int buffer_width = buffer_diameter; + int buffer_height = buffer_diameter; - int buffer_width = surface->indicator_width; - int buffer_height = surface->indicator_height; - int new_width = buffer_diameter; - int new_height = buffer_diameter; + if (text || layout_text) { + cairo_set_antialias(state->test_cairo, CAIRO_ANTIALIAS_BEST); + configure_font_drawing(state->test_cairo, state, surface->subpixel, arc_radius); + + if (text) { + cairo_text_extents_t extents; + cairo_text_extents(state->test_cairo, text, &extents); + if (buffer_width < extents.width) { + buffer_width = extents.width; + } + } + if (layout_text) { + cairo_text_extents_t extents; + cairo_font_extents_t fe; + double box_padding = 4.0 * surface->scale; + cairo_text_extents(state->test_cairo, layout_text, &extents); + cairo_font_extents(state->test_cairo, &fe); + buffer_height += fe.height + 2 * box_padding; + if (buffer_width < extents.width + 2 * box_padding) { + buffer_width = extents.width + 2 * box_padding; + } + } + } + // Ensure buffer size is multiple of buffer scale - required by protocol + buffer_height += surface->scale - (buffer_height % surface->scale); + buffer_width += surface->scale - (buffer_width % surface->scale); int subsurf_xpos; int subsurf_ypos; @@ -100,22 +197,16 @@ void render_frame(struct swaylock_surface *surface) { (state->args.radius + state->args.thickness); } - wl_subsurface_set_position(surface->subsurface, subsurf_xpos, subsurf_ypos); - struct pool_buffer *buffer = get_next_buffer(state->shm, surface->indicator_buffers, buffer_width, buffer_height); if (buffer == NULL) { return; } + // Render the buffer cairo_t *cairo = buffer->cairo; cairo_set_antialias(cairo, CAIRO_ANTIALIAS_BEST); - cairo_font_options_t *fo = cairo_font_options_create(); - cairo_font_options_set_hint_style(fo, CAIRO_HINT_STYLE_FULL); - cairo_font_options_set_antialias(fo, CAIRO_ANTIALIAS_SUBPIXEL); - cairo_font_options_set_subpixel_order(fo, to_cairo_subpixel_order(surface->subpixel)); - cairo_set_font_options(cairo, fo); - cairo_font_options_destroy(fo); + cairo_identity_matrix(cairo); // Clear @@ -146,61 +237,8 @@ void render_frame(struct swaylock_surface *surface) { cairo_stroke(cairo); // Draw a message - char *text = NULL; - const char *layout_text = NULL; - char attempts[4]; // like i3lock: count no more than 999 + configure_font_drawing(cairo, state, surface->subpixel, arc_radius); set_color_for_state(cairo, state, &state->args.colors.text); - cairo_select_font_face(cairo, state->args.font, - CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL); - if (state->args.font_size > 0) { - cairo_set_font_size(cairo, state->args.font_size); - } else { - cairo_set_font_size(cairo, arc_radius / 3.0f); - } - switch (state->auth_state) { - case AUTH_STATE_VALIDATING: - text = "Verifying"; - break; - case AUTH_STATE_INVALID: - text = "Wrong"; - break; - case AUTH_STATE_CLEAR: - text = "Cleared"; - break; - case AUTH_STATE_INPUT: - case AUTH_STATE_INPUT_NOP: - case AUTH_STATE_BACKSPACE: - // Caps Lock has higher priority - if (state->xkb.caps_lock && state->args.show_caps_lock_text) { - text = "Caps Lock"; - } else if (state->args.show_failed_attempts && - state->failed_attempts > 0) { - if (state->failed_attempts > 999) { - text = "999+"; - } else { - snprintf(attempts, sizeof(attempts), "%d", state->failed_attempts); - text = attempts; - } - } - - xkb_layout_index_t num_layout = xkb_keymap_num_layouts(state->xkb.keymap); - if (!state->args.hide_keyboard_layout && - (state->args.show_keyboard_layout || num_layout > 1)) { - xkb_layout_index_t curr_layout = 0; - - // advance to the first active layout (if any) - while (curr_layout < num_layout && - xkb_state_layout_index_is_active(state->xkb.state, - curr_layout, XKB_STATE_LAYOUT_EFFECTIVE) != 1) { - ++curr_layout; - } - // will handle invalid index if none are active - layout_text = xkb_keymap_layout_get_name(state->xkb.keymap, curr_layout); - } - break; - default: - break; - } if (text) { cairo_text_extents_t extents; @@ -217,10 +255,6 @@ void render_frame(struct swaylock_surface *surface) { cairo_show_text(cairo, text); cairo_close_path(cairo); cairo_new_sub_path(cairo); - - if (new_width < extents.width) { - new_width = extents.width; - } } // Typing indicator: Highlight random part on keypress @@ -300,25 +334,11 @@ void render_frame(struct swaylock_surface *surface) { cairo_set_source_u32(cairo, state->args.colors.layout_text); cairo_show_text(cairo, layout_text); cairo_new_sub_path(cairo); - - new_height += fe.height + 2 * box_padding; - if (new_width < extents.width + 2 * box_padding) { - new_width = extents.width + 2 * box_padding; - } } } - // Ensure buffer size is multiple of buffer scale - required by protocol - new_height += surface->scale - (new_height % surface->scale); - new_width += surface->scale - (new_width % surface->scale); - - if (buffer_width != new_width || buffer_height != new_height) { - destroy_buffer(buffer); - surface->indicator_width = new_width; - surface->indicator_height = new_height; - render_frame(surface); - return; - } + // Send Wayland requests + wl_subsurface_set_position(surface->subsurface, subsurf_xpos, subsurf_ypos); wl_surface_set_buffer_scale(surface->child, surface->scale); wl_surface_attach(surface->child, buffer->buffer, 0, 0); |
