diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/cocoa/api.h | 7 | ||||
| -rw-r--r-- | src/cocoa/window.m | 99 | ||||
| -rw-r--r-- | src/common/backend.c | 103 | ||||
| -rw-r--r-- | src/common/backend.h | 13 | ||||
| -rw-r--r-- | src/common/loop.c | 59 | ||||
| -rw-r--r-- | src/common/loop.h | 8 | ||||
| -rw-r--r-- | src/common/meson.build | 2 | ||||
| -rw-r--r-- | src/common/native.c | 35 | ||||
| -rw-r--r-- | src/common/timing.c | 7 | ||||
| -rw-r--r-- | src/common/timing.h | 28 | ||||
| -rw-r--r-- | src/wayland/api.h | 7 | ||||
| -rw-r--r-- | src/wayland/backend.c | 23 | ||||
| -rw-r--r-- | src/wayland/buffer.c | 8 | ||||
| -rw-r--r-- | src/wayland/internal.h | 2 | ||||
| -rw-r--r-- | src/wayland/popup.c | 17 | ||||
| -rw-r--r-- | src/wayland/surface.c | 27 | ||||
| -rw-r--r-- | src/x11/window.c | 39 |
17 files changed, 196 insertions, 288 deletions
diff --git a/src/cocoa/api.h b/src/cocoa/api.h index 2fea415..59372cf 100644 --- a/src/cocoa/api.h +++ b/src/cocoa/api.h @@ -9,7 +9,6 @@ struct cocoa_surface; struct cocoa_display *cocoa_display_open(struct yasl_loop *loop, const struct yasl_display_spec *spec); void cocoa_display_close(struct cocoa_display *d); uint32_t cocoa_display_caps(const struct cocoa_display *d); -void cocoa_display_dispatch(struct cocoa_display *d); int cocoa_display_dispatch_timeout(struct cocoa_display *d, int timeout_ms); void cocoa_display_flush(struct cocoa_display *d); int cocoa_display_modifiers(const struct cocoa_display *d); @@ -17,7 +16,8 @@ void cocoa_display_copy(struct cocoa_display *d, enum yasl_selection sel, const void cocoa_display_paste(struct cocoa_display *d, enum yasl_selection sel, struct cocoa_surface *target); bool cocoa_display_drag_start(struct cocoa_display *d, struct cocoa_surface *origin, const struct yasl_drag_image *image, const struct yasl_drag_listener *l, void *ud); void cocoa_display_drag_cancel(struct cocoa_display *d); -struct cocoa_surface *cocoa_surface_create_toplevel(struct cocoa_display *d, const struct yasl_surface_spec *spec); +struct cocoa_surface *cocoa_surface_create_toplevel(struct cocoa_display *d, + const struct yasl_surface_spec *spec, void *graphics_device); struct cocoa_surface *cocoa_surface_create_popup(struct cocoa_surface *parent, const struct yasl_popup_spec *spec); bool cocoa_surface_popup_reposition(struct cocoa_surface *popup, const struct yasl_popup_spec *spec); void cocoa_surface_destroy(struct cocoa_surface *s); @@ -27,8 +27,7 @@ void cocoa_surface_set_title(struct cocoa_surface *s, const char *title); void cocoa_surface_schedule_redraw(struct cocoa_surface *s); bool cocoa_surface_present(struct cocoa_surface *s, const struct yasl_framebuffer *fb, const struct yasl_damage_list *damage); bool cocoa_surface_input_poll(struct cocoa_surface *s, struct yasl_input_event *ev); -struct yasl_vector2i cocoa_surface_size(const struct cocoa_surface *s); -uint32_t cocoa_surface_scale_120(const struct cocoa_surface *s); +struct yasl_geometry cocoa_surface_geometry(const struct cocoa_surface *s); uint32_t cocoa_surface_state(const struct cocoa_surface *s); struct yasl_timing cocoa_surface_timing(const struct cocoa_surface *s); uint64_t cocoa_surface_frame_time_ns(struct cocoa_surface *s, uint64_t now); diff --git a/src/cocoa/window.m b/src/cocoa/window.m index fc69412..6e1e457 100644 --- a/src/cocoa/window.m +++ b/src/cocoa/window.m @@ -3,7 +3,8 @@ #import <QuartzCore/QuartzCore.h> #include "cocoa/api.h" -#include "yasl/timing.h" +#include "common/timing.h" +#include "common/loop.h" #include "cocoa/keycodes.h" #include <assert.h> @@ -70,6 +71,8 @@ struct cocoa_surface { uint32_t *pixels; size_t pixel_capacity; + id<CAMetalDrawable> drawable; + bool transaction; struct yasl_frame frame; struct yasl_damage_list damage; bool did_present; @@ -775,9 +778,7 @@ bool cocoa_surface_present(struct cocoa_surface *w, const struct yasl_framebuffe { if (!w || w->closed || !w->rendering || w->did_present || !fb || !damage || fb->pixels != w->frame.target.pixels || fb->size.x != w->size.x || - fb->size.y != w->size.y || fb->buffer != w->frame.target.buffer || - fb->transaction != w->frame.target.transaction || - !fb->native_drawable || fb->native_drawable != w->frame.target.native_drawable) + fb->size.y != w->size.y || !w->drawable) return false; if (w->software_queue) { if (fb->pixels != w->pixels || fb->size.x != w->size.x || fb->size.y != w->size.y) @@ -793,7 +794,7 @@ bool cocoa_surface_present(struct cocoa_surface *w, const struct yasl_framebuffe id<MTLCommandBuffer> command = [w->software_queue commandBuffer]; id<MTLBlitCommandEncoder> blit = [command blitCommandEncoder]; if (!blit) { [buffer release]; return false; } - id<CAMetalDrawable> drawable = (id<CAMetalDrawable>)fb->native_drawable; + id<CAMetalDrawable> drawable = w->drawable; [blit copyFromBuffer:buffer sourceOffset:0 sourceBytesPerRow:row_bytes sourceBytesPerImage:length sourceSize:MTLSizeMake(w->size.x, w->size.y, 1) toTexture:drawable.texture destinationSlice:0 destinationLevel:0 @@ -827,6 +828,8 @@ static void render_and_commit(struct cocoa_surface *w) w->frame_due = false; w->did_present = false; id<CAMetalDrawable> drawable = [w->layer nextDrawable]; + w->drawable = drawable; + w->transaction = w->layer.presentsWithTransaction; w->damage = (struct yasl_damage_list){0}; w->frame = (struct yasl_frame){ .surface = &w->base, @@ -834,9 +837,6 @@ static void render_and_commit(struct cocoa_surface *w) .generation = yasl_frame_begin(&w->base), .pixels = w->software_queue ? w->pixels : NULL, .size = w->size, - .damage = &w->damage, - .native_drawable = drawable, - .transaction = w->layer.presentsWithTransaction, }, }; if (drawable) @@ -845,7 +845,7 @@ static void render_and_commit(struct cocoa_surface *w) w->want_draw = true; w->frames_skipped++; } - w->frame.target.native_drawable = NULL; + w->drawable = nil; w->rendering = false; darwin_ctx.depth--; } @@ -864,6 +864,14 @@ int cocoa_display_dispatch_timeout(struct cocoa_display *d, int timeout_ms) assert([NSThread isMainThread]); if (!d || d->pumping) return -1; + for (int i = 0; i < d->count; i++) { + struct cocoa_surface *w = d->windows[i]; + if (w->input.count || w->input.reset_pending || + (w->want_draw && (!w->link || w->frame_due || w->unpaced))) { + timeout_ms = 0; + break; + } + } d->pumping = true; d->depth++; int count = 0; @@ -914,21 +922,6 @@ int cocoa_display_dispatch_timeout(struct cocoa_display *d, int timeout_ms) return count; } -void cocoa_display_dispatch(struct cocoa_display *d) -{ - if (!d) return; - bool immediate = false; - for (int i = 0; i < d->count; i++) { - struct cocoa_surface *w = d->windows[i]; - if (w->input.count > 0 || w->input.reset_pending || - (w->want_draw && (!w->link || w->frame_due || w->unpaced))) { - immediate = true; - break; - } - } - cocoa_display_dispatch_timeout(d, immediate ? 0 : -1); -} - void cocoa_display_flush(struct cocoa_display *d) { assert([NSThread isMainThread]); @@ -1061,7 +1054,7 @@ uint32_t cocoa_display_caps(const struct cocoa_display *d) } struct cocoa_surface *cocoa_surface_create_toplevel(struct cocoa_display *d, - const struct yasl_surface_spec *spec) + const struct yasl_surface_spec *spec, void *graphics_device) { assert([NSThread isMainThread]); if (!d || !d->open || !spec) @@ -1104,8 +1097,8 @@ struct cocoa_surface *cocoa_surface_create_toplevel(struct cocoa_display *d, w->view = [[YaslView alloc] initWithFrame:w->window.contentView.bounds]; w->window.contentView = w->view; w->layer = (CAMetalLayer *)w->view.layer; - if (spec->graphics_device) { - w->layer.device = (id<MTLDevice>)spec->graphics_device; + if (graphics_device) { + w->layer.device = (id<MTLDevice>)graphics_device; } else { id<MTLDevice> device = MTLCreateSystemDefaultDevice(); w->layer.device = device; @@ -1311,14 +1304,12 @@ int cocoa_display_modifiers(const struct cocoa_display *d) return (int)cocoa_mods(NSEvent.modifierFlags); } -struct yasl_vector2i cocoa_surface_size(const struct cocoa_surface *w) +struct yasl_geometry cocoa_surface_geometry(const struct cocoa_surface *w) { - return w ? w->size : (struct yasl_vector2i){0}; -} - -uint32_t cocoa_surface_scale_120(const struct cocoa_surface *w) -{ - return w ? w->scale_120 : YASL_SCALE_UNIT; + return (struct yasl_geometry){ + .size = w ? w->size : (struct yasl_vector2i){0}, + .scale_120 = w ? w->scale_120 : YASL_SCALE_UNIT, + }; } struct cocoa_surface *cocoa_surface_create_popup(struct cocoa_surface *parent, @@ -1367,12 +1358,12 @@ bool cocoa_surface_show_window_menu(struct cocoa_surface *s, struct yasl_vector2 void *cocoa_display_get_native_handle(struct cocoa_display *d) { - return d ? (void *)d : NULL; + return d ? (void *)NSApp : NULL; } void *cocoa_surface_get_native_handle(struct cocoa_surface *s) { - return s ? (void *)s->view : NULL; + return s ? (void *)s->window : NULL; } @@ -1431,11 +1422,6 @@ static uint32_t impl_display_caps(const struct yasl_display *d) return cocoa_display_caps((const struct cocoa_display *)d); } -static void impl_display_dispatch(struct yasl_display *d) -{ - cocoa_display_dispatch((struct cocoa_display *)d); -} - static int impl_display_dispatch_timeout(struct yasl_display *d, int timeout_ms) { return cocoa_display_dispatch_timeout((struct cocoa_display *)d, timeout_ms); @@ -1471,9 +1457,10 @@ static void impl_display_drag_cancel(struct yasl_display *d) cocoa_display_drag_cancel((struct cocoa_display *)d); } -static struct yasl_surface *impl_surface_create_toplevel(struct yasl_display *d, const struct yasl_surface_spec *spec) +static struct yasl_surface *impl_surface_create_toplevel(struct yasl_display *d, + const struct yasl_surface_spec *spec, void *graphics_device) { - struct cocoa_surface *result = cocoa_surface_create_toplevel((struct cocoa_display *)d, spec); + struct cocoa_surface *result = cocoa_surface_create_toplevel((struct cocoa_display *)d, spec, graphics_device); return result ? &result->base : NULL; } @@ -1524,14 +1511,9 @@ static bool impl_surface_input_poll(struct yasl_surface *s, struct yasl_input_ev return cocoa_surface_input_poll((struct cocoa_surface *)s, ev); } -static struct yasl_vector2i impl_surface_size(const struct yasl_surface *s) +static struct yasl_geometry impl_surface_geometry(const struct yasl_surface *s) { - return cocoa_surface_size((const struct cocoa_surface *)s); -} - -static uint32_t impl_surface_scale_120(const struct yasl_surface *s) -{ - return cocoa_surface_scale_120((const struct cocoa_surface *)s); + return cocoa_surface_geometry((const struct cocoa_surface *)s); } static uint32_t impl_surface_state(const struct yasl_surface *s) @@ -1609,12 +1591,23 @@ static void *impl_surface_get_native_handle(struct yasl_surface *s) return cocoa_surface_get_native_handle((struct cocoa_surface *)s); } +static bool impl_surface_get_native_frame(struct yasl_surface *s, + const struct yasl_framebuffer *target, struct yasl_native_frame *out) +{ + struct cocoa_surface *w = (struct cocoa_surface *)s; + if (w->closed || !w->rendering || w->did_present || !w->drawable || + target->pixels != w->frame.target.pixels || + target->size.x != w->size.x || target->size.y != w->size.y) + return false; + *out = (struct yasl_native_frame){(void *)w->drawable, w->transaction}; + return true; +} + const struct yasl_backend_ops yasl_cocoa_backend = { .type = YASL_BACKEND_COCOA, .display_open = impl_display_open, .display_close = impl_display_close, .display_caps = impl_display_caps, - .display_dispatch = impl_display_dispatch, .display_dispatch_timeout = impl_display_dispatch_timeout, .display_flush = impl_display_flush, .display_modifiers = impl_display_modifiers, @@ -1632,8 +1625,7 @@ const struct yasl_backend_ops yasl_cocoa_backend = { .surface_schedule_redraw = impl_surface_schedule_redraw, .surface_present = impl_surface_present, .surface_input_poll = impl_surface_input_poll, - .surface_size = impl_surface_size, - .surface_scale_120 = impl_surface_scale_120, + .surface_geometry = impl_surface_geometry, .surface_state = impl_surface_state, .surface_timing = impl_surface_timing, .surface_frame_time_ns = impl_surface_frame_time_ns, @@ -1649,4 +1641,5 @@ const struct yasl_backend_ops yasl_cocoa_backend = { .surface_place_under_pointer = impl_surface_place_under_pointer, .display_get_native_handle = impl_display_get_native_handle, .surface_get_native_handle = impl_surface_get_native_handle, + .surface_get_native_frame = impl_surface_get_native_frame, }; diff --git a/src/common/backend.c b/src/common/backend.c index 27ab297..ad4d908 100644 --- a/src/common/backend.c +++ b/src/common/backend.c @@ -78,22 +78,11 @@ uint32_t yasl_display_caps(const struct yasl_display *d) return d->ops->display_caps(d); } -void yasl_display_dispatch(struct yasl_display *d) -{ - if (!d || !d->ops->display_dispatch) - return; - d->ops->display_dispatch(d); -} - -int yasl_display_dispatch_timeout(struct yasl_display *d, int timeout_ms) +int yasl_display_dispatch(struct yasl_display *d, int timeout_ms) { if (!d) return -1; if (d->ops->display_dispatch_timeout) return d->ops->display_dispatch_timeout(d, timeout_ms); - if (d->ops->display_dispatch) { - d->ops->display_dispatch(d); - return 0; - } return -1; } @@ -147,7 +136,8 @@ void yasl_display_drag_cancel(struct yasl_display *d) d->ops->display_drag_cancel(d); } -struct yasl_surface *yasl_surface_create_toplevel(struct yasl_display *d, const struct yasl_surface_spec *spec) +struct yasl_surface *yasl_create_toplevel(struct yasl_display *d, + const struct yasl_surface_spec *spec, void *graphics_device) { if (!d || !d->ops->surface_create_toplevel || !spec || spec->size.x <= 0 || spec->size.y <= 0 || @@ -156,11 +146,17 @@ struct yasl_surface *yasl_surface_create_toplevel(struct yasl_display *d, const errno = EINVAL; return NULL; } - if (spec->graphics_device && !(yasl_display_caps(d) & YASL_CAP_NATIVE_GRAPHICS)) { + if (graphics_device && !(yasl_display_caps(d) & YASL_CAP_NATIVE_GRAPHICS)) { errno = ENOTSUP; return NULL; } - return d->ops->surface_create_toplevel(d, spec); + return d->ops->surface_create_toplevel(d, spec, graphics_device); +} + +struct yasl_surface *yasl_surface_create_toplevel(struct yasl_display *d, + const struct yasl_surface_spec *spec) +{ + return yasl_create_toplevel(d, spec, NULL); } struct yasl_surface *yasl_surface_create_popup(struct yasl_surface *parent, const struct yasl_popup_spec *spec) @@ -247,18 +243,11 @@ bool yasl_surface_input_poll(struct yasl_surface *s, struct yasl_input_event *ev return s->display->ops->surface_input_poll(s, ev); } -struct yasl_vector2i yasl_surface_size(const struct yasl_surface *s) -{ - if (!s || !s->display->ops->surface_size) - return (struct yasl_vector2i){0}; - return s->display->ops->surface_size(s); -} - -uint32_t yasl_surface_scale_120(const struct yasl_surface *s) +struct yasl_geometry yasl_surface_geometry(const struct yasl_surface *s) { - if (!s || !s->display->ops->surface_scale_120) - return YASL_SCALE_UNIT; - return s->display->ops->surface_scale_120(s); + if (!s || !s->display->ops->surface_geometry) + return (struct yasl_geometry){.scale_120 = YASL_SCALE_UNIT}; + return s->display->ops->surface_geometry(s); } uint32_t yasl_surface_state(const struct yasl_surface *s) @@ -356,20 +345,6 @@ bool yasl_surface_place_under_pointer(struct yasl_surface *s, struct yasl_vector return s->display->ops->surface_place_under_pointer(s, at); } -void *yasl_display_get_native_handle(struct yasl_display *d) -{ - if (!d || !d->ops->display_get_native_handle) - return 0; - return d->ops->display_get_native_handle(d); -} - -void *yasl_surface_get_native_handle(struct yasl_surface *s) -{ - if (!s || !s->display->ops->surface_get_native_handle) - return 0; - return s->display->ops->surface_get_native_handle(s); -} - void yasl_surface_clear_timers(struct yasl_surface *s) { if (!s || !s->timers) return; @@ -408,29 +383,6 @@ yasl_timer_id yasl_surface_add_timer(struct yasl_surface *s, uint64_t interval_n return id; } -yasl_timer_id yasl_surface_add_timer_ms(struct yasl_surface *s, uint32_t interval_ms, - yasl_timer_ms_cb cb, void *ud) -{ - if (!s || !s->display || !s->display->loop || !cb || interval_ms == 0) - return 0; - - yasl_timer_id id = yasl_loop_add_timer_ms(s->display->loop, interval_ms, cb, ud); - if (!id) return 0; - - if (s->timer_count >= s->timer_capacity) { - int new_cap = s->timer_capacity ? s->timer_capacity * 2 : 4; - yasl_timer_id *nt = realloc(s->timers, (size_t)new_cap * sizeof(*nt)); - if (!nt) { - yasl_loop_del_timer(s->display->loop, id); - return 0; - } - s->timers = nt; - s->timer_capacity = new_cap; - } - s->timers[s->timer_count++] = id; - return id; -} - bool yasl_surface_del_timer(struct yasl_surface *s, yasl_timer_id id) { if (!s || id == 0) return false; @@ -445,32 +397,7 @@ bool yasl_surface_del_timer(struct yasl_surface *s, yasl_timer_id id) return false; } -yasl_timer_id yasl_display_add_timer(struct yasl_display *d, uint64_t interval_ns, - yasl_timer_cb cb, void *ud) -{ - if (!d || !d->loop) return 0; - return yasl_loop_add_timer(d->loop, interval_ns, cb, ud); -} - -yasl_timer_id yasl_display_add_timer_ms(struct yasl_display *d, uint32_t interval_ms, - yasl_timer_ms_cb cb, void *ud) -{ - if (!d || !d->loop) return 0; - return yasl_loop_add_timer_ms(d->loop, interval_ms, cb, ud); -} - -bool yasl_display_del_timer(struct yasl_display *d, yasl_timer_id id) -{ - if (!d || !d->loop) return false; - return yasl_loop_del_timer(d->loop, id); -} - struct yasl_loop *yasl_display_loop(struct yasl_display *d) { return d ? d->loop : NULL; } - -struct yasl_loop *yasl_surface_loop(struct yasl_surface *s) -{ - return (s && s->display) ? s->display->loop : NULL; -} diff --git a/src/common/backend.h b/src/common/backend.h index b52a147..6ab7248 100644 --- a/src/common/backend.h +++ b/src/common/backend.h @@ -2,6 +2,8 @@ #define YASL_BACKEND_H #include "yasl/yasl.h" +#include "yasl/loop.h" +#include "yasl/native.h" struct yasl_backend_ops; @@ -21,6 +23,8 @@ struct yasl_surface { }; void yasl_surface_clear_timers(struct yasl_surface *s); +struct yasl_surface *yasl_create_toplevel(struct yasl_display *d, + const struct yasl_surface_spec *spec, void *graphics_device); static inline uint64_t yasl_frame_begin(struct yasl_surface *s) { @@ -33,7 +37,6 @@ struct yasl_backend_ops { struct yasl_display *(*display_open)(struct yasl_loop *loop, const struct yasl_display_spec *spec); void (*display_close)(struct yasl_display *d); uint32_t (*display_caps)(const struct yasl_display *d); - void (*display_dispatch)(struct yasl_display *d); int (*display_dispatch_timeout)(struct yasl_display *d, int timeout_ms); void (*display_flush)(struct yasl_display *d); int (*display_modifiers)(const struct yasl_display *d); @@ -41,7 +44,8 @@ struct yasl_backend_ops { void (*display_paste)(struct yasl_display *d, enum yasl_selection sel, struct yasl_surface *target); bool (*display_drag_start)(struct yasl_display *d, struct yasl_surface *origin, const struct yasl_drag_image *image, const struct yasl_drag_listener *l, void *ud); void (*display_drag_cancel)(struct yasl_display *d); - struct yasl_surface *(*surface_create_toplevel)(struct yasl_display *d, const struct yasl_surface_spec *spec); + struct yasl_surface *(*surface_create_toplevel)(struct yasl_display *d, + const struct yasl_surface_spec *spec, void *graphics_device); struct yasl_surface *(*surface_create_popup)(struct yasl_surface *parent, const struct yasl_popup_spec *spec); bool (*surface_popup_reposition)(struct yasl_surface *popup, const struct yasl_popup_spec *spec); void (*surface_destroy)(struct yasl_surface *s); @@ -51,8 +55,7 @@ struct yasl_backend_ops { void (*surface_schedule_redraw)(struct yasl_surface *s); bool (*surface_present)(struct yasl_surface *s, const struct yasl_framebuffer *fb, const struct yasl_damage_list *damage); bool (*surface_input_poll)(struct yasl_surface *s, struct yasl_input_event *ev); - struct yasl_vector2i (*surface_size)(const struct yasl_surface *s); - uint32_t (*surface_scale_120)(const struct yasl_surface *s); + struct yasl_geometry (*surface_geometry)(const struct yasl_surface *s); uint32_t (*surface_state)(const struct yasl_surface *s); struct yasl_timing (*surface_timing)(const struct yasl_surface *s); uint64_t (*surface_frame_time_ns)(struct yasl_surface *s, uint64_t now); @@ -68,6 +71,8 @@ struct yasl_backend_ops { bool (*surface_place_under_pointer)(struct yasl_surface *s, struct yasl_vector2i at); void *(*display_get_native_handle)(struct yasl_display *d); void *(*surface_get_native_handle)(struct yasl_surface *s); + bool (*surface_get_native_frame)(struct yasl_surface *s, + const struct yasl_framebuffer *target, struct yasl_native_frame *out); }; #if YASL_HAS_WAYLAND diff --git a/src/common/loop.c b/src/common/loop.c index a1f2434..3815e91 100644 --- a/src/common/loop.c +++ b/src/common/loop.c @@ -1,6 +1,6 @@ #define _GNU_SOURCE #define _POSIX_C_SOURCE 200809L -#include "yasl/yasl.h" +#include "loop.h" #include <errno.h> #include <fcntl.h> @@ -23,16 +23,11 @@ struct yasl_fd_source { }; struct yasl_timer_source { - union { - yasl_timer_cb ns; - yasl_timer_ms_cb ms; - void *raw; - } fn; + yasl_timer_cb fn; void *ud; yasl_timer_id id; uint64_t interval_ns; uint64_t deadline_ns; - bool ms_mode; bool deleted; }; @@ -251,44 +246,11 @@ yasl_timer_id yasl_loop_add_timer(struct yasl_loop *l, uint64_t interval_ns, uint64_t now = yasl_loop_now_ns(); l->timers[l->timer_count++] = (struct yasl_timer_source){ - .fn.ns = cb, + .fn = cb, .ud = ud, .id = id, .interval_ns = interval_ns, .deadline_ns = now + interval_ns, - .ms_mode = false, - .deleted = false, - }; - - yasl_loop_kick(l); - return id; -} - -yasl_timer_id yasl_loop_add_timer_ms(struct yasl_loop *l, uint32_t interval_ms, - yasl_timer_ms_cb cb, void *ud) -{ - if (!l || !cb || interval_ms == 0) return 0; - - if (l->timer_count >= l->timer_capacity) { - int new_cap = l->timer_capacity ? l->timer_capacity * 2 : 16; - struct yasl_timer_source *new_timers = realloc(l->timers, (size_t)new_cap * sizeof(*new_timers)); - if (!new_timers) return 0; - l->timers = new_timers; - l->timer_capacity = new_cap; - } - - uint32_t id = l->next_timer_id++; - if (l->next_timer_id == 0) l->next_timer_id = 1; - - uint64_t now = yasl_loop_now_ns(); - uint64_t interval_ns = (uint64_t)interval_ms * 1000000ull; - l->timers[l->timer_count++] = (struct yasl_timer_source){ - .fn.ms = cb, - .ud = ud, - .id = id, - .interval_ns = interval_ns, - .deadline_ns = now + interval_ns, - .ms_mode = true, .deleted = false, }; @@ -390,18 +352,10 @@ int yasl_loop_step(struct yasl_loop *l, int timeout_ms) yasl_timer_id tid = l->timers[i].id; uint64_t interval = l->timers[i].interval_ns; - bool is_ms = l->timers[i].ms_mode; void *ud = l->timers[i].ud; struct yasl_timer_source timer_src = l->timers[i]; - uint64_t next_interval = 0; - if (is_ms) { - uint32_t old_ms = (uint32_t)(interval / 1000000ull); - uint32_t res_ms = timer_src.fn.ms(ud, tid, old_ms); - next_interval = (uint64_t)res_ms * 1000000ull; - } else { - next_interval = timer_src.fn.ns(ud, tid, interval); - } + uint64_t next_interval = timer_src.fn(ud, tid, interval); handled++; for (int j = 0; j < l->timer_count; j++) { @@ -443,8 +397,3 @@ void yasl_loop_quit(struct yasl_loop *l) { if (l) l->running = false; } - -bool yasl_loop_is_running(const struct yasl_loop *l) -{ - return l ? l->running : false; -} diff --git a/src/common/loop.h b/src/common/loop.h new file mode 100644 index 0000000..579fa2c --- /dev/null +++ b/src/common/loop.h @@ -0,0 +1,8 @@ +#ifndef YASL_COMMON_LOOP_H +#define YASL_COMMON_LOOP_H + +#include "yasl/loop.h" + +int yasl_loop_next_timeout_ms(const struct yasl_loop *l); + +#endif /* YASL_COMMON_LOOP_H */ diff --git a/src/common/meson.build b/src/common/meson.build index 5cfb187..ddd0194 100644 --- a/src/common/meson.build +++ b/src/common/meson.build @@ -1 +1 @@ -sources += files('backend.c', 'input.c', 'loop.c', 'timing.c', 'utf8.c') +sources += files('backend.c', 'input.c', 'loop.c', 'native.c', 'timing.c', 'utf8.c') diff --git a/src/common/native.c b/src/common/native.c new file mode 100644 index 0000000..2d9c06f --- /dev/null +++ b/src/common/native.c @@ -0,0 +1,35 @@ +#include "backend.h" + +#include <errno.h> + +void *yasl_display_get_native_handle(struct yasl_display *d) +{ + return d && d->ops->display_get_native_handle ? + d->ops->display_get_native_handle(d) : NULL; +} + +void *yasl_surface_get_native_handle(struct yasl_surface *s) +{ + return s && s->display->ops->surface_get_native_handle ? + s->display->ops->surface_get_native_handle(s) : NULL; +} + +struct yasl_surface *yasl_surface_create_native(struct yasl_display *d, + const struct yasl_surface_spec *spec, void *graphics_device) +{ + if (!graphics_device) { + errno = EINVAL; + return NULL; + } + return yasl_create_toplevel(d, spec, graphics_device); +} + +bool yasl_surface_get_native_frame(struct yasl_surface *s, + const struct yasl_framebuffer *target, struct yasl_native_frame *out) +{ + if (!s || !target || !out || !target->generation || + target->generation != s->frame_generation || + !s->display->ops->surface_get_native_frame) + return false; + return s->display->ops->surface_get_native_frame(s, target, out); +} diff --git a/src/common/timing.c b/src/common/timing.c index 17f71b5..7a91327 100644 --- a/src/common/timing.c +++ b/src/common/timing.c @@ -1,4 +1,4 @@ -#include "yasl/timing.h" +#include "timing.h" uint64_t yasl_clock_extend_ms(uint32_t ms, uint64_t now_ns) { @@ -39,11 +39,6 @@ void yasl_clock_resize_stats(const struct yasl_frame_clock *c, out->minflt = c->resize_minflt; } -double yasl_clock_refresh_ms(const struct yasl_frame_clock *c) -{ - return c->refresh_ns / 1e6; -} - uint64_t yasl_clock_next_present_ns(const struct yasl_frame_clock *c) { uint64_t next = c->last_present_ns + (uint64_t)c->refresh_ns; diff --git a/src/common/timing.h b/src/common/timing.h new file mode 100644 index 0000000..a3c3c88 --- /dev/null +++ b/src/common/timing.h @@ -0,0 +1,28 @@ +#ifndef YASL_COMMON_TIMING_H +#define YASL_COMMON_TIMING_H + +#include "yasl/timing.h" +#include <stdbool.h> + +#define YASL_REFRESH_DEFAULT_NS 16666666.0 + +struct yasl_frame_clock { + uint64_t last_present_ns; + uint64_t resize_count; + uint64_t resize_sum_ns; + uint64_t resize_max_ns; + long resize_minflt; + uint64_t frame_time_ns; + double refresh_ns; + enum yasl_timing_source source; +}; + +uint64_t yasl_clock_extend_ms(uint32_t ms, uint64_t now_ns); +void yasl_clock_presented(struct yasl_frame_clock *c, uint64_t now, bool sample); +void yasl_clock_note_resize(struct yasl_frame_clock *c, uint64_t dt_ns); +void yasl_clock_resize_stats(const struct yasl_frame_clock *c, + struct yasl_resize_stats *out); +uint64_t yasl_clock_next_present_ns(const struct yasl_frame_clock *c); +uint64_t yasl_clock_frame_timestamp_ns(struct yasl_frame_clock *c, uint64_t now); + +#endif /* YASL_COMMON_TIMING_H */ diff --git a/src/wayland/api.h b/src/wayland/api.h index 8252041..87e896d 100644 --- a/src/wayland/api.h +++ b/src/wayland/api.h @@ -9,7 +9,6 @@ struct wayland_surface; struct wayland_display *wayland_display_open(struct yasl_loop *loop, const struct yasl_display_spec *spec); void wayland_display_close(struct wayland_display *d); uint32_t wayland_display_caps(const struct wayland_display *d); -void wayland_display_dispatch(struct wayland_display *d); int wayland_display_dispatch_timeout(struct wayland_display *d, int timeout_ms); void wayland_display_flush(struct wayland_display *d); int wayland_display_modifiers(const struct wayland_display *d); @@ -17,7 +16,8 @@ void wayland_display_copy(struct wayland_display *d, enum yasl_selection sel, co void wayland_display_paste(struct wayland_display *d, enum yasl_selection sel, struct wayland_surface *target); bool wayland_display_drag_start(struct wayland_display *d, struct wayland_surface *origin, const struct yasl_drag_image *image, const struct yasl_drag_listener *l, void *ud); void wayland_display_drag_cancel(struct wayland_display *d); -struct wayland_surface *wayland_surface_create_toplevel(struct wayland_display *d, const struct yasl_surface_spec *spec); +struct wayland_surface *wayland_surface_create_toplevel(struct wayland_display *d, + const struct yasl_surface_spec *spec, void *graphics_device); struct wayland_surface *wayland_surface_create_popup(struct wayland_surface *parent, const struct yasl_popup_spec *spec); bool wayland_surface_popup_reposition(struct wayland_surface *popup, const struct yasl_popup_spec *spec); void wayland_surface_destroy(struct wayland_surface *s); @@ -27,8 +27,7 @@ void wayland_surface_set_title(struct wayland_surface *s, const char *title); void wayland_surface_schedule_redraw(struct wayland_surface *s); bool wayland_surface_present(struct wayland_surface *s, const struct yasl_framebuffer *fb, const struct yasl_damage_list *damage); bool wayland_surface_input_poll(struct wayland_surface *s, struct yasl_input_event *ev); -struct yasl_vector2i wayland_surface_size(const struct wayland_surface *s); -uint32_t wayland_surface_scale_120(const struct wayland_surface *s); +struct yasl_geometry wayland_surface_geometry(const struct wayland_surface *s); uint32_t wayland_surface_state(const struct wayland_surface *s); struct yasl_timing wayland_surface_timing(const struct wayland_surface *s); uint64_t wayland_surface_frame_time_ns(struct wayland_surface *s, uint64_t now); diff --git a/src/wayland/backend.c b/src/wayland/backend.c index 31e412f..5c34def 100644 --- a/src/wayland/backend.c +++ b/src/wayland/backend.c @@ -16,11 +16,6 @@ static uint32_t impl_display_caps(const struct yasl_display *d) return wayland_display_caps((const struct wayland_display *)d); } -static void impl_display_dispatch(struct yasl_display *d) -{ - wayland_display_dispatch((struct wayland_display *)d); -} - static int impl_display_dispatch_timeout(struct yasl_display *d, int timeout_ms) { return wayland_display_dispatch_timeout((struct wayland_display *)d, timeout_ms); @@ -56,9 +51,10 @@ static void impl_display_drag_cancel(struct yasl_display *d) wayland_display_drag_cancel((struct wayland_display *)d); } -static struct yasl_surface *impl_surface_create_toplevel(struct yasl_display *d, const struct yasl_surface_spec *spec) +static struct yasl_surface *impl_surface_create_toplevel(struct yasl_display *d, + const struct yasl_surface_spec *spec, void *graphics_device) { - struct wayland_surface *result = wayland_surface_create_toplevel((struct wayland_display *)d, spec); + struct wayland_surface *result = wayland_surface_create_toplevel((struct wayland_display *)d, spec, graphics_device); return result ? &result->base : NULL; } @@ -109,14 +105,9 @@ static bool impl_surface_input_poll(struct yasl_surface *s, struct yasl_input_ev return wayland_surface_input_poll((struct wayland_surface *)s, ev); } -static struct yasl_vector2i impl_surface_size(const struct yasl_surface *s) -{ - return wayland_surface_size((const struct wayland_surface *)s); -} - -static uint32_t impl_surface_scale_120(const struct yasl_surface *s) +static struct yasl_geometry impl_surface_geometry(const struct yasl_surface *s) { - return wayland_surface_scale_120((const struct wayland_surface *)s); + return wayland_surface_geometry((const struct wayland_surface *)s); } static uint32_t impl_surface_state(const struct yasl_surface *s) @@ -199,7 +190,6 @@ const struct yasl_backend_ops yasl_wayland_backend = { .display_open = impl_display_open, .display_close = impl_display_close, .display_caps = impl_display_caps, - .display_dispatch = impl_display_dispatch, .display_dispatch_timeout = impl_display_dispatch_timeout, .display_flush = impl_display_flush, .display_modifiers = impl_display_modifiers, @@ -217,8 +207,7 @@ const struct yasl_backend_ops yasl_wayland_backend = { .surface_schedule_redraw = impl_surface_schedule_redraw, .surface_present = impl_surface_present, .surface_input_poll = impl_surface_input_poll, - .surface_size = impl_surface_size, - .surface_scale_120 = impl_surface_scale_120, + .surface_geometry = impl_surface_geometry, .surface_state = impl_surface_state, .surface_timing = impl_surface_timing, .surface_frame_time_ns = impl_surface_frame_time_ns, diff --git a/src/wayland/buffer.c b/src/wayland/buffer.c index 3f9cf84..a1486fa 100644 --- a/src/wayland/buffer.c +++ b/src/wayland/buffer.c @@ -446,8 +446,7 @@ bool wayland_surface_present(struct wayland_surface *w, const struct yasl_frameb { if (!w || w->closed || !w->frame_active || w->frame_presented || !fb || !damage || fb->pixels != w->frame.target.pixels || - fb->size.x != w->width || fb->size.y != w->height || - fb->buffer != w->cur || fb->native_drawable || fb->transaction) + fb->size.x != w->width || fb->size.y != w->height) return false; w->damage = *damage; @@ -491,11 +490,6 @@ void surface_draw_frame(struct wayland_surface *w) .generation = yasl_frame_begin(&w->base), .pixels = w->buffers[idx].pixels, .size = { .x = w->width, .y = w->height }, - .buffer = idx, - .cleared = w->parent != NULL, - .damage = &w->damage, - .native_drawable = NULL, - .transaction = false, }, }; w->frame_active = true; diff --git a/src/wayland/internal.h b/src/wayland/internal.h index ccfd2dc..249fc8d 100644 --- a/src/wayland/internal.h +++ b/src/wayland/internal.h @@ -4,7 +4,7 @@ #define _GNU_SOURCE #include "api.h" -#include "yasl/timing.h" +#include "common/timing.h" #include "clipboard.h" #include <wayland-client.h> diff --git a/src/wayland/popup.c b/src/wayland/popup.c index f0ef305..63bcba6 100644 --- a/src/wayland/popup.c +++ b/src/wayland/popup.c @@ -23,13 +23,18 @@ static const struct xdg_popup_listener popup_listener = { .repositioned = popup_repositioned, }; -static bool popup_spec_valid(const struct yasl_popup_spec *spec) +static bool popup_spec_valid(const struct wayland_surface *parent, + const struct yasl_popup_spec *spec) { - return spec && spec->size.x > 0 && spec->size.y > 0 && + if (!parent || !spec) return false; + int64_t x = (int64_t)spec->anchor.x - parent->content.x; + int64_t y = (int64_t)spec->anchor.y - parent->content.y; + return x >= INT_MIN && x <= INT_MAX && y >= INT_MIN && y <= INT_MAX && + spec->size.x > 0 && spec->size.y > 0 && spec->content_offset.x >= 0 && spec->content_offset.y >= 0 && spec->content_size.x >= 0 && spec->content_size.y >= 0 && - spec->content_offset.x + spec->content_size.x <= spec->size.x && - spec->content_offset.y + spec->content_size.y <= spec->size.y; + (int64_t)spec->content_offset.x + spec->content_size.x <= spec->size.x && + (int64_t)spec->content_offset.y + spec->content_size.y <= spec->size.y; } static bool popup_apply_spec(struct wayland_surface *w, const struct yasl_popup_spec *spec) @@ -125,7 +130,7 @@ struct wayland_surface *wayland_surface_create_popup(struct wayland_surface *par struct wayland_display *c = parent ? parent->conn : NULL; struct seat *seat = c ? conn_active_seat(c) : NULL; if (!c || c->lost || parent->closed || !parent->xdg_surface || !seat || - !seat->input_serial || !popup_spec_valid(spec)) + !seat->input_serial || !popup_spec_valid(parent, spec)) return NULL; struct wayland_surface *w = calloc(1, sizeof(*w)); @@ -170,7 +175,7 @@ err: bool wayland_surface_popup_reposition(struct wayland_surface *w, const struct yasl_popup_spec *spec) { if (!w || !w->popup || w->closed || !w->conn || w->conn->lost || - !popup_spec_valid(spec) || + !popup_spec_valid(w->parent, spec) || xdg_popup_get_version(w->popup) < XDG_POPUP_REPOSITION_SINCE_VERSION) return false; struct yasl_popup_spec old = { diff --git a/src/wayland/surface.c b/src/wayland/surface.c index 4f064e2..f291190 100644 --- a/src/wayland/surface.c +++ b/src/wayland/surface.c @@ -148,13 +148,13 @@ int wayland_display_dispatch_timeout(struct wayland_display *c, int timeout_ms) return -1; } - bool had_pending = display_arm(c); + display_arm(c); if (c->lost) { display_leave(c); return -1; } - if (had_pending && timeout_ms != 0 && display_has_immediate_work(c)) { + if (timeout_ms != 0 && display_has_immediate_work(c)) { timeout_ms = 0; } @@ -177,14 +177,6 @@ int wayland_display_dispatch_timeout(struct wayland_display *c, int timeout_ms) return n; } -void wayland_display_dispatch(struct wayland_display *c) -{ - if (unlikely(!c || c->lost)) - return; - int timeout = (display_has_immediate_work(c) || yasl_loop_is_running(c->loop)) ? 0 : -1; - wayland_display_dispatch_timeout(c, timeout); -} - void wayland_display_flush(struct wayland_display *c) { if (!c || c->lost) @@ -342,8 +334,9 @@ static bool spec_valid(const struct yasl_surface_spec *spec) } struct wayland_surface *wayland_surface_create_toplevel(struct wayland_display *c, - const struct yasl_surface_spec *spec) + const struct yasl_surface_spec *spec, void *graphics_device) { + (void)graphics_device; if (!c || c->lost || !spec_valid(spec)) return NULL; int width = spec->size.x, height = spec->size.y; @@ -457,14 +450,12 @@ void wayland_surface_schedule_redraw(struct wayland_surface *w) yasl_loop_kick(w->conn->loop); } -struct yasl_vector2i wayland_surface_size(const struct wayland_surface *w) +struct yasl_geometry wayland_surface_geometry(const struct wayland_surface *w) { - return w ? (struct yasl_vector2i){ .x = w->width, .y = w->height } : (struct yasl_vector2i){0}; -} - -uint32_t wayland_surface_scale_120(const struct wayland_surface *w) -{ - return w ? w->scale_120 : YASL_SCALE_UNIT; + return (struct yasl_geometry){ + .size = w ? (struct yasl_vector2i){w->width, w->height} : (struct yasl_vector2i){0}, + .scale_120 = w ? w->scale_120 : YASL_SCALE_UNIT, + }; } void wayland_surface_set_background_color(struct wayland_surface *w, uint32_t bg) diff --git a/src/x11/window.c b/src/x11/window.c index 2d9aff3..67276cf 100644 --- a/src/x11/window.c +++ b/src/x11/window.c @@ -1,6 +1,7 @@ #define _POSIX_C_SOURCE 200809L #include "common/backend.h" #include "common/input.h" +#include "common/timing.h" #include <X11/Xatom.h> #include <X11/XKBlib.h> @@ -218,8 +219,9 @@ static void net_message(struct x11_surface *s, Atom type, } static struct yasl_surface *create_toplevel(struct yasl_display *base, - const struct yasl_surface_spec *spec) + const struct yasl_surface_spec *spec, void *graphics_device) { + (void)graphics_device; if (!spec) return NULL; struct x11_display *d = (struct x11_display *)base; struct x11_surface *s = calloc(1, sizeof(*s)); @@ -481,7 +483,6 @@ static bool present(struct yasl_surface *base, const struct yasl_framebuffer *fb struct x11_surface *s = (struct x11_surface *)base; if (s->destroyed || !s->drawing || s->presented || !fb || !damage || fb->pixels != s->pixels || fb->size.x != s->size.x || fb->size.y != s->size.y || - fb->buffer != s->frame.target.buffer || fb->native_drawable || fb->transaction || damage->n < -1 || damage->n > YASL_DAMAGE_MAX) return false; struct x11_display *d = display_of(base); XImage *im = s->image; @@ -527,7 +528,7 @@ static void flush(struct yasl_display *base) s->frame = (struct yasl_frame){ .surface = &s->base, .target = {.generation = yasl_frame_begin(&s->base), - .pixels = s->pixels, .size = s->size, .damage = &s->damage}, + .pixels = s->pixels, .size = s->size}, }; s->cbs->draw(&s->base, &s->frame, s->ud); s->drawing = false; @@ -549,6 +550,12 @@ static int dispatch_timeout(struct yasl_display *base, int timeout_ms) struct x11_display *d = (struct x11_display *)base; if (unlikely(!d || !d->native)) return -1; dispatch(base); + for (struct x11_surface *s = d->surfaces; s; s = s->next) { + if (s->want_draw || s->input.count || s->input.reset_pending) { + timeout_ms = 0; + break; + } + } if (d->loop) { if (!XPending(d->native) && timeout_ms != 0) { yasl_loop_step(d->loop, timeout_ms); @@ -696,8 +703,10 @@ static void schedule_redraw(struct yasl_surface *base) } static struct yasl_display *surface_display(const struct yasl_surface *s) { return s->display; } -static struct yasl_vector2i size(const struct yasl_surface *s) { return ((const struct x11_surface *)s)->size; } -static uint32_t scale(const struct yasl_surface *s) { (void)s; return YASL_SCALE_UNIT; } +static struct yasl_geometry geometry(const struct yasl_surface *s) +{ + return (struct yasl_geometry){((const struct x11_surface *)s)->size, YASL_SCALE_UNIT}; +} static uint32_t state(const struct yasl_surface *s) { return ((const struct x11_surface *)s)->state; } static int mods(const struct yasl_display *d) { return (int)((const struct x11_display *)d)->mods; } static void *native_display(struct yasl_display *d) { return ((struct x11_display *)d)->native; } @@ -766,28 +775,11 @@ static bool interactive_move(struct yasl_surface *base) return true; } -static void display_dispatch(struct yasl_display *base) -{ - struct x11_display *d = (struct x11_display *)base; - if (unlikely(!d || !d->native)) return; - bool immediate = false; - for (struct x11_surface *s = d->surfaces; s; s = s->next) { - if (s->want_draw || s->input.count > 0 || s->input.reset_pending) { - immediate = true; - break; - } - } - if (d->loop && yasl_loop_is_running(d->loop)) - immediate = true; - dispatch_timeout(base, immediate ? 0 : -1); -} - const struct yasl_backend_ops yasl_x11_backend = { .type = YASL_BACKEND_X11, .display_open = display_open, .display_close = display_close, .display_caps = caps, - .display_dispatch = display_dispatch, .display_dispatch_timeout = dispatch_timeout, .display_flush = flush, .display_modifiers = mods, @@ -801,8 +793,7 @@ const struct yasl_backend_ops yasl_x11_backend = { .surface_schedule_redraw = schedule_redraw, .surface_present = present, .surface_input_poll = input_poll, - .surface_size = size, - .surface_scale_120 = scale, + .surface_geometry = geometry, .surface_state = state, .surface_timing = timing, .surface_frame_time_ns = frame_time, |
