presentation-time: use a surface addon

This commit is contained in:
Kirill Primak 2021-10-08 20:49:12 +03:00 committed by Simon Ser
parent 3dc99ed281
commit 6c3a71d9f6
2 changed files with 69 additions and 79 deletions

View File

@ -14,12 +14,13 @@
#include <time.h> #include <time.h>
#include <wayland-server-core.h> #include <wayland-server-core.h>
struct wlr_surface;
struct wlr_output; struct wlr_output;
struct wlr_output_event_present; struct wlr_output_event_present;
struct wlr_presentation { struct wlr_presentation {
struct wl_global *global; struct wl_global *global;
struct wl_list feedbacks; // wlr_presentation_feedback::link
clockid_t clock; clockid_t clock;
struct { struct {
@ -31,27 +32,15 @@ struct wlr_presentation {
struct wlr_presentation_feedback { struct wlr_presentation_feedback {
struct wlr_presentation *presentation; struct wlr_presentation *presentation;
struct wlr_surface *surface; // NULL if the surface has been destroyed
struct wl_list link; // wlr_presentation::feedbacks
struct wl_list resources; // wl_resource_get_link struct wl_list resources; // wl_resource_get_link
// The surface contents were committed.
bool committed;
// The surface contents were sampled by the compositor and are to be
// presented on the next flip. Can become true only after committed becomes
// true.
bool sampled;
bool presented;
// Only when the wlr_presentation_surface_sampled_on_output helper has been // Only when the wlr_presentation_surface_sampled_on_output helper has been
// called // called
struct wlr_output *output; struct wlr_output *output;
bool output_committed; bool output_committed;
uint32_t output_commit_seq; uint32_t output_commit_seq;
struct wl_listener surface_commit;
struct wl_listener surface_destroy;
struct wl_listener output_commit; struct wl_listener output_commit;
struct wl_listener output_present; struct wl_listener output_present;
struct wl_listener output_destroy; struct wl_listener output_destroy;

View File

@ -2,14 +2,27 @@
#include <assert.h> #include <assert.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <wlr/backend.h>
#include <wlr/types/wlr_presentation_time.h> #include <wlr/types/wlr_presentation_time.h>
#include <wlr/types/wlr_surface.h> #include <wlr/types/wlr_surface.h>
#include <wlr/backend.h> #include <wlr/util/addon.h>
#include "presentation-time-protocol.h" #include "presentation-time-protocol.h"
#include "util/signal.h" #include "util/signal.h"
#define PRESENTATION_VERSION 1 #define PRESENTATION_VERSION 1
struct wlr_presentation_surface_state {
struct wlr_presentation_feedback *feedback;
};
struct wlr_presentation_surface {
struct wlr_presentation_surface_state current, pending;
struct wlr_addon addon; // wlr_surface::addons
struct wl_listener surface_commit;
};
static void feedback_handle_resource_destroy(struct wl_resource *resource) { static void feedback_handle_resource_destroy(struct wl_resource *resource) {
wl_list_remove(wl_resource_get_link(resource)); wl_list_remove(wl_resource_get_link(resource));
} }
@ -43,41 +56,32 @@ static void feedback_resource_send_discarded(
wl_resource_destroy(feedback_resource); wl_resource_destroy(feedback_resource);
} }
static void feedback_handle_surface_commit(struct wl_listener *listener, static const struct wlr_addon_interface presentation_surface_addon_impl;
void *data) {
struct wlr_presentation_feedback *feedback =
wl_container_of(listener, feedback, surface_commit);
if (feedback->committed) { static void presentation_surface_addon_destroy(struct wlr_addon *addon) {
if (!feedback->sampled) { struct wlr_presentation_surface *p_surface =
// The content update has been superseded wl_container_of(addon, p_surface, addon);
wlr_presentation_feedback_destroy(feedback);
} wlr_presentation_feedback_destroy(p_surface->current.feedback);
} else { wlr_presentation_feedback_destroy(p_surface->pending.feedback);
feedback->committed = true;
} wl_list_remove(&p_surface->surface_commit.link);
free(p_surface);
} }
static void feedback_unset_surface(struct wlr_presentation_feedback *feedback) { static const struct wlr_addon_interface presentation_surface_addon_impl = {
if (feedback->surface == NULL) { .name = "wlr_presentation_surface",
return; .destroy = presentation_surface_addon_destroy,
} };
feedback->surface = NULL; static void presentation_surface_handle_surface_commit(
wl_list_remove(&feedback->surface_commit.link); struct wl_listener *listener, void *data) {
wl_list_remove(&feedback->surface_destroy.link); struct wlr_presentation_surface *p_surface =
} wl_container_of(listener, p_surface, surface_commit);
static void feedback_handle_surface_destroy(struct wl_listener *listener, wlr_presentation_feedback_destroy(p_surface->current.feedback);
void *data) { p_surface->current.feedback = p_surface->pending.feedback;
struct wlr_presentation_feedback *feedback = p_surface->pending.feedback = NULL;
wl_container_of(listener, feedback, surface_destroy);
if (feedback->sampled) {
// The compositor might have a handle on this feedback
feedback_unset_surface(feedback);
} else {
wlr_presentation_feedback_destroy(feedback);
}
} }
static const struct wp_presentation_interface presentation_impl; static const struct wp_presentation_interface presentation_impl;
@ -96,31 +100,35 @@ static void presentation_handle_feedback(struct wl_client *client,
presentation_from_resource(presentation_resource); presentation_from_resource(presentation_resource);
struct wlr_surface *surface = wlr_surface_from_resource(surface_resource); struct wlr_surface *surface = wlr_surface_from_resource(surface_resource);
bool found = false; struct wlr_addon *addon = wlr_addon_find(&surface->addons,
struct wlr_presentation_feedback *feedback; presentation, &presentation_surface_addon_impl);
wl_list_for_each(feedback, &presentation->feedbacks, link) { struct wlr_presentation_surface *p_surface = NULL;
if (feedback->surface == surface && !feedback->committed) { if (addon != NULL) {
found = true; p_surface = wl_container_of(addon, p_surface, addon);
break; } else {
p_surface = calloc(1, sizeof(*p_surface));
if (p_surface == NULL) {
wl_client_post_no_memory(client);
return;
} }
wlr_addon_init(&p_surface->addon, &surface->addons,
presentation, &presentation_surface_addon_impl);
p_surface->surface_commit.notify =
presentation_surface_handle_surface_commit;
wl_signal_add(&surface->events.commit,
&p_surface->surface_commit);
} }
if (!found) {
struct wlr_presentation_feedback *feedback = p_surface->pending.feedback;
if (feedback == NULL) {
feedback = calloc(1, sizeof(struct wlr_presentation_feedback)); feedback = calloc(1, sizeof(struct wlr_presentation_feedback));
if (feedback == NULL) { if (feedback == NULL) {
wl_client_post_no_memory(client); wl_client_post_no_memory(client);
return; return;
} }
feedback->surface = surface;
wl_list_init(&feedback->resources); wl_list_init(&feedback->resources);
p_surface->pending.feedback = feedback;
feedback->surface_commit.notify = feedback_handle_surface_commit;
wl_signal_add(&surface->events.commit, &feedback->surface_commit);
feedback->surface_destroy.notify = feedback_handle_surface_destroy;
wl_signal_add(&surface->events.destroy, &feedback->surface_destroy);
wl_list_insert(&presentation->feedbacks, &feedback->link);
} }
uint32_t version = wl_resource_get_version(presentation_resource); uint32_t version = wl_resource_get_version(presentation_resource);
@ -188,7 +196,6 @@ struct wlr_presentation *wlr_presentation_create(struct wl_display *display,
presentation->clock = wlr_backend_get_presentation_clock(backend); presentation->clock = wlr_backend_get_presentation_clock(backend);
wl_list_init(&presentation->feedbacks);
wl_signal_init(&presentation->events.destroy); wl_signal_init(&presentation->events.destroy);
presentation->display_destroy.notify = handle_display_destroy; presentation->display_destroy.notify = handle_display_destroy;
@ -204,21 +211,19 @@ void wlr_presentation_feedback_send_presented(
wl_resource_for_each_safe(resource, tmp, &feedback->resources) { wl_resource_for_each_safe(resource, tmp, &feedback->resources) {
feedback_resource_send_presented(resource, event); feedback_resource_send_presented(resource, event);
} }
feedback->presented = true;
} }
struct wlr_presentation_feedback *wlr_presentation_surface_sampled( struct wlr_presentation_feedback *wlr_presentation_surface_sampled(
struct wlr_presentation *presentation, struct wlr_surface *surface) { struct wlr_presentation *presentation, struct wlr_surface *surface) {
// TODO: maybe use a hashtable to optimize this function struct wlr_addon *addon = wlr_addon_find(&surface->addons,
struct wlr_presentation_feedback *feedback, *feedback_tmp; presentation, &presentation_surface_addon_impl);
wl_list_for_each_safe(feedback, feedback_tmp, if (addon != NULL) {
&presentation->feedbacks, link) { struct wlr_presentation_surface *p_surface =
if (feedback->surface == surface && feedback->committed && wl_container_of(addon, p_surface, addon);
!feedback->sampled) { struct wlr_presentation_feedback *sampled =
feedback->sampled = true; p_surface->current.feedback;
return feedback; p_surface->current.feedback = NULL;
} return sampled;
} }
return NULL; return NULL;
} }
@ -231,17 +236,13 @@ void wlr_presentation_feedback_destroy(
return; return;
} }
if (!feedback->presented) { struct wl_resource *resource, *tmp;
struct wl_resource *resource, *tmp; wl_resource_for_each_safe(resource, tmp, &feedback->resources) {
wl_resource_for_each_safe(resource, tmp, &feedback->resources) { feedback_resource_send_discarded(resource);
feedback_resource_send_discarded(resource);
}
} }
assert(wl_list_empty(&feedback->resources)); assert(wl_list_empty(&feedback->resources));
feedback_unset_surface(feedback);
feedback_unset_output(feedback); feedback_unset_output(feedback);
wl_list_remove(&feedback->link);
free(feedback); free(feedback);
} }