Skip to content

Commit a1921d4

Browse files
committed
Use single_pixel_buffer for solid backgrounds
1 parent 69f399f commit a1921d4

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

include/swaylock.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "pool-buffer.h"
99
#include "seat.h"
1010
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
11+
#include "viewporter-client-protocol.h"
12+
#include "single-pixel-buffer-v1-client-protocol.h"
1113

1214
enum auth_state {
1315
AUTH_STATE_IDLE,
@@ -78,6 +80,9 @@ struct swaylock_state {
7880
struct wl_display *display;
7981
struct wl_compositor *compositor;
8082
struct wl_subcompositor *subcompositor;
83+
struct wp_viewporter *viewporter;
84+
struct wl_buffer *background_buffer;
85+
struct wp_single_pixel_buffer_manager_v1 *single_pixel_buffer_manager;
8186
struct zwlr_layer_shell_v1 *layer_shell;
8287
struct zwlr_input_inhibit_manager_v1 *input_inhibit_manager;
8388
struct wl_shm *shm;
@@ -100,11 +105,13 @@ struct swaylock_surface {
100105
uint32_t output_global_name;
101106
struct wl_surface *surface;
102107
struct wl_surface *child; // surface made into subsurface
108+
struct wp_viewport *viewport;
103109
struct wl_subsurface *subsurface;
104110
struct zwlr_layer_surface_v1 *layer_surface;
105111
struct ext_session_lock_surface_v1 *ext_session_lock_surface_v1;
106112
struct pool_buffer buffers[2];
107113
struct pool_buffer indicator_buffers[2];
114+
struct wl_buffer *backgound_buffer;
108115
bool frame_pending, dirty;
109116
uint32_t width, height;
110117
uint32_t indicator_width, indicator_height;

main.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ static void destroy_surface(struct swaylock_surface *surface) {
109109
destroy_buffer(&surface->buffers[1]);
110110
destroy_buffer(&surface->indicator_buffers[0]);
111111
destroy_buffer(&surface->indicator_buffers[1]);
112+
wp_viewport_destroy(surface->viewport);
112113
wl_output_destroy(surface->output);
113114
free(surface);
114115
}
@@ -173,6 +174,11 @@ static void create_surface(struct swaylock_surface *surface) {
173174
wl_region_destroy(region);
174175
}
175176

177+
if (state->viewporter) {
178+
surface->viewport = wp_viewporter_get_viewport(
179+
state->viewporter, surface->surface);
180+
}
181+
176182
if (!state->ext_session_lock_v1) {
177183
wl_surface_commit(surface->surface);
178184
}
@@ -373,6 +379,13 @@ static void handle_global(void *data, struct wl_registry *registry,
373379
} else if (strcmp(interface, ext_session_lock_manager_v1_interface.name) == 0) {
374380
state->ext_session_lock_manager_v1 = wl_registry_bind(registry, name,
375381
&ext_session_lock_manager_v1_interface, 1);
382+
} else if (strcmp(interface, wp_viewporter_interface.name) == 0) {
383+
state->viewporter = wl_registry_bind(registry, name,
384+
&wp_viewporter_interface, 1);
385+
} else if (strcmp(interface,
386+
wp_single_pixel_buffer_manager_v1_interface.name) == 0) {
387+
state->single_pixel_buffer_manager = wl_registry_bind(registry, name,
388+
&wp_single_pixel_buffer_manager_v1_interface, 1);
376389
}
377390
}
378391

@@ -1265,6 +1278,20 @@ int main(int argc, char **argv) {
12651278
return 1;
12661279
}
12671280

1281+
if (state.single_pixel_buffer_manager && state.viewporter) {
1282+
uint8_t r8 = (state.args.colors.background >> 24) & 0xFF;
1283+
uint8_t g8 = (state.args.colors.background >> 16) & 0xFF;
1284+
uint8_t b8 = (state.args.colors.background >> 8) & 0xFF;
1285+
uint8_t a8 = (state.args.colors.background >> 0) & 0xFF;
1286+
uint32_t f = 0xFFFFFFFF / 0xFF; // division result is an integer
1287+
uint32_t r32 = r8 * f;
1288+
uint32_t g32 = g8 * f;
1289+
uint32_t b32 = b8 * f;
1290+
uint32_t a32 = a8 * f;
1291+
state.background_buffer = wp_single_pixel_buffer_manager_v1_create_u32_rgba_buffer(
1292+
state.single_pixel_buffer_manager, r32, g32, b32, a32);
1293+
}
1294+
12681295
struct swaylock_surface *surface;
12691296
wl_list_for_each(surface, &state.surfaces, link) {
12701297
create_surface(surface);
@@ -1298,6 +1325,7 @@ int main(int argc, char **argv) {
12981325
wl_display_flush(state.display);
12991326
}
13001327

1328+
wl_buffer_destroy(state.background_buffer);
13011329
free(state.args.font);
13021330
return 0;
13031331
}

meson.build

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ if is_freebsd
3737
endif
3838

3939
wayland_client = dependency('wayland-client', version: '>=1.20.0')
40-
wayland_protos = dependency('wayland-protocols', version: '>=1.25', fallback: 'wayland-protocols')
40+
wayland_protos = dependency('wayland-protocols', version: '>=1.26', fallback: 'wayland-protocols')
4141
# use native version of wayland-scanner when cross-compiling
4242
# meson does this too: https://github.com/mesonbuild/meson/blob/c649a2b8c59c9f49affca9bd89c126bfa0f54449/mesonbuild/modules/unstable_wayland.py#L48-L53
4343
wayland_scanner = dependency('wayland-scanner', version: '>=1.15.0', native: true)
@@ -84,6 +84,8 @@ client_protos_headers = []
8484
client_protocols = [
8585
wl_protocol_dir / 'stable/xdg-shell/xdg-shell.xml',
8686
wl_protocol_dir / 'staging/ext-session-lock/ext-session-lock-v1.xml',
87+
wl_protocol_dir / 'stable/viewporter/viewporter.xml',
88+
wl_protocol_dir / 'staging/single-pixel-buffer/single-pixel-buffer-v1.xml',
8789
'wlr-layer-shell-unstable-v1.xml',
8890
'wlr-input-inhibitor-unstable-v1.xml',
8991
]

render.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ void render_frame_background(struct swaylock_surface *surface) {
4141
return; // not yet configured
4242
}
4343

44+
if (surface->viewport && state->background_buffer &&
45+
(!surface->image || state->args.mode == BACKGROUND_MODE_SOLID_COLOR)) {
46+
// no need to carry around shm buffers if we are going to use the single
47+
// pixel buffer
48+
destroy_buffer(&surface->buffers[0]);
49+
destroy_buffer(&surface->buffers[1]);
50+
51+
wl_surface_set_buffer_scale(surface->surface, 1);
52+
wl_surface_attach(surface->surface, state->background_buffer, 0, 0);
53+
wl_surface_damage_buffer(surface->surface, 0, 0, INT32_MAX, INT32_MAX);
54+
55+
wl_surface_commit(surface->surface);
56+
return;
57+
}
58+
59+
if (surface->viewport) {
60+
wp_viewport_set_destination(surface->viewport, -1, -1);
61+
}
62+
4463
struct pool_buffer *buffer = get_next_buffer(state->shm,
4564
surface->buffers, buffer_width, buffer_height);
4665
if (buffer == NULL) {

0 commit comments

Comments
 (0)