Merge branch 'deferred-cursor-move' into '0.17'

Draft: Deferred cursor move

See merge request wlroots/wlroots!4340
This commit is contained in:
Daniel Hill 2024-06-28 02:20:31 +00:00
commit 6746cf248e
6 changed files with 111 additions and 7 deletions

View file

@ -4,6 +4,8 @@
#include <stdint.h> #include <stdint.h>
#include <time.h> #include <time.h>
static const long NSEC_PER_SEC = 1000000000;
/** /**
* Get the current time, in milliseconds. * Get the current time, in milliseconds.
*/ */
@ -30,4 +32,6 @@ void timespec_from_nsec(struct timespec *r, int64_t nsec);
void timespec_sub(struct timespec *r, const struct timespec *a, void timespec_sub(struct timespec *r, const struct timespec *a,
const struct timespec *b); const struct timespec *b);
int32_t mhz_to_nsec(int32_t mhz);
#endif #endif

View file

@ -34,6 +34,8 @@ struct wlr_cursor {
struct wlr_cursor_state *state; struct wlr_cursor_state *state;
double x, y; double x, y;
int max_latency;
/** /**
* The interpretation of these signals is the responsibility of the * The interpretation of these signals is the responsibility of the
* compositor, but some helpers are provided for your benefit. If you * compositor, but some helpers are provided for your benefit. If you
@ -166,6 +168,8 @@ void wlr_cursor_set_xcursor(struct wlr_cursor *cur,
void wlr_cursor_set_surface(struct wlr_cursor *cur, struct wlr_surface *surface, void wlr_cursor_set_surface(struct wlr_cursor *cur, struct wlr_surface *surface,
int32_t hotspot_x, int32_t hotspot_y); int32_t hotspot_x, int32_t hotspot_y);
void wlr_cursor_set_max_latency(struct wlr_cursor *cur, int max_latency);
/** /**
* Attaches this input device to this cursor. The input device must be one of: * Attaches this input device to this cursor. The input device must be one of:
* *

View file

@ -43,9 +43,15 @@ struct wlr_output_cursor {
struct wlr_fbox src_box; struct wlr_fbox src_box;
enum wl_output_transform transform; enum wl_output_transform transform;
int32_t hotspot_x, hotspot_y; int32_t hotspot_x, hotspot_y;
// only when using a software cursor without a surface
struct wlr_texture *texture; struct wlr_texture *texture;
bool own_texture; bool own_texture;
struct wl_list link; struct wl_list link;
struct timespec last_presentation;
bool deferred;
double deferred_x, deferred_y;
int max_latency;
}; };
enum wlr_output_adaptive_sync_status { enum wlr_output_adaptive_sync_status {
@ -150,6 +156,8 @@ struct wlr_output {
enum wlr_output_adaptive_sync_status adaptive_sync_status; enum wlr_output_adaptive_sync_status adaptive_sync_status;
uint32_t render_format; uint32_t render_format;
int max_cursor_latency;
bool needs_frame; bool needs_frame;
// damage for cursors and fullscreen surface, in output-local coordinates // damage for cursors and fullscreen surface, in output-local coordinates
bool frame_pending; bool frame_pending;
@ -561,6 +569,13 @@ bool wlr_output_cursor_set_buffer(struct wlr_output_cursor *cursor,
struct wlr_buffer *buffer, int32_t hotspot_x, int32_t hotspot_y); struct wlr_buffer *buffer, int32_t hotspot_x, int32_t hotspot_y);
bool wlr_output_cursor_move(struct wlr_output_cursor *cursor, bool wlr_output_cursor_move(struct wlr_output_cursor *cursor,
double x, double y); double x, double y);
/**
* Call any_expired() before you check needs_frame.
* call all_deferred() whenever a new frame is needed.
* */
void wlr_output_cursor_move_expired(struct wlr_output_cursor *cursor, struct timespec *now);
void wlr_output_cursor_move_any_expired(struct wlr_output *output, struct timespec *now);
void wlr_output_cursor_move_all_deferred(struct wlr_output *output, struct timespec *now);
void wlr_output_cursor_destroy(struct wlr_output_cursor *cursor); void wlr_output_cursor_destroy(struct wlr_output_cursor *cursor);
/** /**

View file

@ -1,3 +1,4 @@
#define _POSIX_C_SOURCE 200809L
#include <assert.h> #include <assert.h>
#include <drm_fourcc.h> #include <drm_fourcc.h>
#include <stdlib.h> #include <stdlib.h>
@ -12,6 +13,9 @@
#include "types/wlr_buffer.h" #include "types/wlr_buffer.h"
#include "types/wlr_output.h" #include "types/wlr_output.h"
#include <time.h>
#include <util/time.h>
static bool output_set_hardware_cursor(struct wlr_output *output, static bool output_set_hardware_cursor(struct wlr_output *output,
struct wlr_buffer *buffer, int hotspot_x, int hotspot_y) { struct wlr_buffer *buffer, int hotspot_x, int hotspot_y) {
if (!output->impl->set_cursor) { if (!output->impl->set_cursor) {
@ -471,11 +475,10 @@ bool output_cursor_set_texture(struct wlr_output_cursor *cursor,
return true; return true;
} }
bool wlr_output_cursor_move(struct wlr_output_cursor *cursor, static bool output_cursor_move(struct wlr_output_cursor *cursor,
double x, double y) { double x, double y, struct timespec *now) {
// Scale coordinates for the output cursor->last_presentation = *now;
x *= cursor->output->scale; cursor->deferred = false;
y *= cursor->output->scale;
if (cursor->x == x && cursor->y == y) { if (cursor->x == x && cursor->y == y) {
return true; return true;
@ -504,6 +507,76 @@ bool wlr_output_cursor_move(struct wlr_output_cursor *cursor,
return cursor->output->impl->move_cursor(cursor->output, (int)x, (int)y); return cursor->output->impl->move_cursor(cursor->output, (int)x, (int)y);
} }
static bool output_cursor_move_should_defer(struct wlr_output_cursor *cursor,
struct timespec *now) {
int max_latency = cursor->max_latency;
if (!max_latency)
max_latency = cursor->output->max_cursor_latency;
if (!max_latency
|| !cursor->output->refresh // avoid divide by zero
|| cursor->output->adaptive_sync_status != WLR_OUTPUT_ADAPTIVE_SYNC_ENABLED)
return false;
struct timespec delta;
int32_t vrr_min = NSEC_PER_SEC / 30; // edid? enforce 30fps minimum for now.
timespec_sub(&delta, now, &cursor->last_presentation);
if (delta.tv_sec
|| delta.tv_nsec >= max_latency
|| delta.tv_nsec >= vrr_min)
return false;
return true;
}
bool wlr_output_cursor_move(struct wlr_output_cursor *cursor,
double x, double y) {
// Scale coordinates for the output
x *= cursor->output->scale;
y *= cursor->output->scale;
if (cursor->x == x && cursor->y == y) {
return true;
}
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
if (output_cursor_move_should_defer(cursor, &now)) {
cursor->deferred_x = x;
cursor->deferred_y = y;
cursor->deferred = true;
return true;
}
return output_cursor_move(cursor, x, y, &now);
}
void wlr_output_cursor_move_expired(struct wlr_output_cursor *cursor, struct timespec *now) {
if (cursor->deferred && !output_cursor_move_should_defer(cursor, now))
output_cursor_move(cursor, cursor->deferred_x, cursor->deferred_y, now);
}
void wlr_output_cursor_move_any_expired(struct wlr_output *output, struct timespec *now) {
struct wlr_output_cursor *cursor;
wl_list_for_each(cursor, &output->cursors, link) {
wlr_output_cursor_move_expired(cursor, now);
}
}
void wlr_output_cursor_move_all_deferred(struct wlr_output *output, struct timespec *now) {
struct wlr_output_cursor *cursor;
wl_list_for_each(cursor, &output->cursors, link) {
if (cursor->deferred)
output_cursor_move(cursor, cursor->deferred_x, cursor->deferred_y, now);
else {
// Should be on wlr_output?
cursor->last_presentation = *now;
};
}
}
struct wlr_output_cursor *wlr_output_cursor_create(struct wlr_output *output) { struct wlr_output_cursor *wlr_output_cursor_create(struct wlr_output *output) {
struct wlr_output_cursor *cursor = calloc(1, sizeof(*cursor)); struct wlr_output_cursor *cursor = calloc(1, sizeof(*cursor));
if (cursor == NULL) { if (cursor == NULL) {

View file

@ -288,6 +288,7 @@ static void cursor_warp_unchecked(struct wlr_cursor *cur,
struct wlr_cursor_output_cursor *output_cursor; struct wlr_cursor_output_cursor *output_cursor;
wl_list_for_each(output_cursor, &cur->state->output_cursors, link) { wl_list_for_each(output_cursor, &cur->state->output_cursors, link) {
output_cursor->output_cursor->max_latency = cur->max_latency;
output_cursor_move(output_cursor); output_cursor_move(output_cursor);
} }
} }
@ -670,6 +671,11 @@ void wlr_cursor_set_surface(struct wlr_cursor *cur, struct wlr_surface *surface,
cursor_update_outputs(cur); cursor_update_outputs(cur);
} }
void wlr_cursor_set_max_latency(struct wlr_cursor *cur, int max_latency) {
cur->max_latency = max_latency;
wlr_log(WLR_DEBUG, "setting max_latency %i", max_latency);
}
static void handle_pointer_motion(struct wl_listener *listener, void *data) { static void handle_pointer_motion(struct wl_listener *listener, void *data) {
struct wlr_pointer_motion_event *event = data; struct wlr_pointer_motion_event *event = data;
struct wlr_cursor_device *device = struct wlr_cursor_device *device =

View file

@ -4,8 +4,6 @@
#include "util/time.h" #include "util/time.h"
static const long NSEC_PER_SEC = 1000000000;
int64_t timespec_to_msec(const struct timespec *a) { int64_t timespec_to_msec(const struct timespec *a) {
return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000; return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000;
} }
@ -34,3 +32,7 @@ void timespec_sub(struct timespec *r, const struct timespec *a,
r->tv_nsec += NSEC_PER_SEC; r->tv_nsec += NSEC_PER_SEC;
} }
} }
int32_t mhz_to_nsec(int32_t mhz) {
return 1000000000000LL / mhz;
}