From 5b1b43c68c7ae65c64a0fcb2c15bf403e5a2fa07 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Tue, 22 Dec 2020 17:07:29 +0100 Subject: [PATCH] backend/drm: make wlr_drm_plane.{pending,queued,current}_fb pointers This will be useful once we start re-using wlr_drm_fb. --- backend/drm/atomic.c | 2 +- backend/drm/drm.c | 36 +++++++++++++++++----------------- backend/drm/legacy.c | 4 ++-- backend/drm/renderer.c | 30 ++++++++++++++++------------ include/backend/drm/drm.h | 6 +++--- include/backend/drm/renderer.h | 8 ++++---- 6 files changed, 46 insertions(+), 40 deletions(-) diff --git a/backend/drm/atomic.c b/backend/drm/atomic.c index acce9e4d..a6a12278 100644 --- a/backend/drm/atomic.c +++ b/backend/drm/atomic.c @@ -134,7 +134,7 @@ static void set_plane_props(struct atomic *atom, struct wlr_drm_backend *drm, uint32_t id = plane->id; const union wlr_drm_plane_props *props = &plane->props; struct wlr_drm_fb *fb = plane_get_next_fb(plane); - if (!fb->id) { + if (fb == NULL) { wlr_log(WLR_ERROR, "Failed to acquire FB"); goto error; } diff --git a/backend/drm/drm.c b/backend/drm/drm.c index 1a8c2637..3340d424 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -324,9 +324,9 @@ static bool drm_connector_attach_render(struct wlr_output *output, static void drm_plane_set_committed(struct wlr_drm_plane *plane) { drm_fb_move(&plane->queued_fb, &plane->pending_fb); - struct wlr_buffer *queued = plane->queued_fb.wlr_buf; - if (queued != NULL) { - wlr_swapchain_set_buffer_submitted(plane->surf.swapchain, queued); + if (plane->queued_fb && plane->queued_fb->wlr_buf) { + wlr_swapchain_set_buffer_submitted(plane->surf.swapchain, + plane->queued_fb->wlr_buf); } } @@ -366,7 +366,7 @@ static bool drm_crtc_page_flip(struct wlr_drm_connector *conn) { } assert(crtc->pending.active); - assert(plane_get_next_fb(crtc->primary)->bo); + assert(plane_get_next_fb(crtc->primary)); if (!drm_crtc_commit(conn, DRM_MODE_PAGE_FLIP_EVENT)) { return false; } @@ -638,11 +638,11 @@ static bool drm_connector_export_dmabuf(struct wlr_output *output, return false; } - struct wlr_drm_fb *fb = &crtc->primary->queued_fb; - if (fb->wlr_buf == NULL) { - fb = &crtc->primary->current_fb; + struct wlr_drm_fb *fb = crtc->primary->queued_fb; + if (fb == NULL) { + fb = crtc->primary->current_fb; } - if (fb->wlr_buf == NULL) { + if (fb == NULL) { return false; } @@ -657,13 +657,13 @@ static bool drm_connector_export_dmabuf(struct wlr_output *output, } struct wlr_drm_fb *plane_get_next_fb(struct wlr_drm_plane *plane) { - if (plane->pending_fb.bo) { - return &plane->pending_fb; + if (plane->pending_fb) { + return plane->pending_fb; } - if (plane->queued_fb.bo) { - return &plane->queued_fb; + if (plane->queued_fb) { + return plane->queued_fb; } - return &plane->current_fb; + return plane->current_fb; } static bool drm_connector_pageflip_renderer(struct wlr_drm_connector *conn) { @@ -676,7 +676,7 @@ static bool drm_connector_pageflip_renderer(struct wlr_drm_connector *conn) { // drm_crtc_page_flip expects a FB to be available struct wlr_drm_plane *plane = crtc->primary; - if (!plane_get_next_fb(plane)->bo) { + if (!plane_get_next_fb(plane)) { if (!drm_surface_render_black_frame(&plane->surf)) { return false; } @@ -1466,10 +1466,10 @@ static void page_flip_handler(int fd, unsigned seq, } struct wlr_drm_plane *plane = conn->crtc->primary; - if (plane->queued_fb.bo) { + if (plane->queued_fb) { drm_fb_move(&plane->current_fb, &plane->queued_fb); } - if (conn->crtc->cursor && conn->crtc->cursor->queued_fb.bo) { + if (conn->crtc->cursor && conn->crtc->cursor->queued_fb) { drm_fb_move(&conn->crtc->cursor->current_fb, &conn->crtc->cursor->queued_fb); } @@ -1480,8 +1480,8 @@ static void page_flip_handler(int fd, unsigned seq, * data between the GPUs, even if we were using the direct scanout * interface. */ - if (!drm->parent && plane->current_fb.wlr_buf && - wlr_client_buffer_get(plane->current_fb.wlr_buf)) { + if (!drm->parent && plane->current_fb->wlr_buf && + wlr_client_buffer_get(plane->current_fb->wlr_buf)) { present_flags |= WLR_OUTPUT_PRESENT_ZERO_COPY; } diff --git a/backend/drm/legacy.c b/backend/drm/legacy.c index fa909fac..accb18ff 100644 --- a/backend/drm/legacy.c +++ b/backend/drm/legacy.c @@ -17,7 +17,7 @@ static bool legacy_crtc_commit(struct wlr_drm_backend *drm, uint32_t fb_id = 0; if (crtc->pending.active) { struct wlr_drm_fb *fb = plane_get_next_fb(crtc->primary); - if (!fb->id) { + if (fb == NULL) { wlr_log(WLR_ERROR, "%s: failed to acquire primary FB", conn->output.name); return false; @@ -76,7 +76,7 @@ static bool legacy_crtc_commit(struct wlr_drm_backend *drm, if (cursor != NULL && drm_connector_is_cursor_visible(conn)) { struct wlr_drm_fb *cursor_fb = plane_get_next_fb(cursor); - if (!cursor_fb->bo) { + if (cursor_fb == NULL) { wlr_drm_conn_log(conn, WLR_DEBUG, "Failed to acquire cursor FB"); return false; } diff --git a/backend/drm/renderer.c b/backend/drm/renderer.c index 4370aa77..a1cdfe86 100644 --- a/backend/drm/renderer.c +++ b/backend/drm/renderer.c @@ -291,12 +291,13 @@ bool drm_plane_init_surface(struct wlr_drm_plane *plane, return ok; } -void drm_fb_clear(struct wlr_drm_fb *fb) { - if (!fb->bo) { - assert(!fb->wlr_buf); +void drm_fb_clear(struct wlr_drm_fb **fb_ptr) { + if (*fb_ptr == NULL) { return; } + struct wlr_drm_fb *fb = *fb_ptr; + struct gbm_device *gbm = gbm_bo_get_device(fb->bo); if (drmModeRmFB(gbm_device_get_fd(gbm), fb->id) != 0) { wlr_log(WLR_ERROR, "drmModeRmFB failed"); @@ -305,11 +306,12 @@ void drm_fb_clear(struct wlr_drm_fb *fb) { gbm_bo_destroy(fb->bo); wlr_buffer_unlock(fb->wlr_buf); wlr_buffer_unlock(fb->mgpu_wlr_buf); + free(fb); - memset(fb, 0, sizeof(*fb)); + *fb_ptr = NULL; } -bool drm_fb_lock_surface(struct wlr_drm_fb *fb, struct wlr_drm_backend *drm, +bool drm_fb_lock_surface(struct wlr_drm_fb **fb_ptr, struct wlr_drm_backend *drm, struct wlr_drm_surface *surf, struct wlr_drm_surface *mgpu) { assert(surf->back_buffer != NULL); @@ -319,7 +321,7 @@ bool drm_fb_lock_surface(struct wlr_drm_fb *fb, struct wlr_drm_backend *drm, // making another context current. drm_surface_unset_current(surf); - bool ok = drm_fb_import(fb, drm, buffer, mgpu, NULL); + bool ok = drm_fb_import(fb_ptr, drm, buffer, mgpu, NULL); wlr_buffer_unlock(buffer); return ok; } @@ -361,10 +363,13 @@ static struct gbm_bo *get_bo_for_dmabuf(struct gbm_device *gbm, } } -bool drm_fb_import(struct wlr_drm_fb *fb, struct wlr_drm_backend *drm, +bool drm_fb_import(struct wlr_drm_fb **fb_ptr, struct wlr_drm_backend *drm, struct wlr_buffer *buf, struct wlr_drm_surface *mgpu, struct wlr_drm_format_set *set) { - drm_fb_clear(fb); + struct wlr_drm_fb *fb = calloc(1, sizeof(*fb)); + if (!fb) { + return false; + } fb->wlr_buf = wlr_buffer_lock(buf); @@ -410,6 +415,8 @@ bool drm_fb_import(struct wlr_drm_fb *fb, struct wlr_drm_backend *drm, goto error_get_fb_for_bo; } + drm_fb_move(fb_ptr, &fb); + return true; error_get_fb_for_bo: @@ -418,15 +425,14 @@ error_get_dmabuf: wlr_buffer_unlock(fb->mgpu_wlr_buf); error_mgpu_wlr_buf: wlr_buffer_unlock(fb->wlr_buf); - memset(fb, 0, sizeof(*fb)); + free(fb); return false; } -void drm_fb_move(struct wlr_drm_fb *new, struct wlr_drm_fb *old) { +void drm_fb_move(struct wlr_drm_fb **new, struct wlr_drm_fb **old) { drm_fb_clear(new); - *new = *old; - memset(old, 0, sizeof(*old)); + *old = NULL; } bool drm_surface_render_black_frame(struct wlr_drm_surface *surf) { diff --git a/include/backend/drm/drm.h b/include/backend/drm/drm.h index 3759ea35..23b06847 100644 --- a/include/backend/drm/drm.h +++ b/include/backend/drm/drm.h @@ -24,11 +24,11 @@ struct wlr_drm_plane { struct wlr_drm_surface mgpu_surf; /* Buffer to be submitted to the kernel on the next page-flip */ - struct wlr_drm_fb pending_fb; + struct wlr_drm_fb *pending_fb; /* Buffer submitted to the kernel, will be presented on next vblank */ - struct wlr_drm_fb queued_fb; + struct wlr_drm_fb *queued_fb; /* Buffer currently displayed on screen */ - struct wlr_drm_fb current_fb; + struct wlr_drm_fb *current_fb; struct wlr_drm_format_set formats; diff --git a/include/backend/drm/renderer.h b/include/backend/drm/renderer.h index d30d4150..71d278ee 100644 --- a/include/backend/drm/renderer.h +++ b/include/backend/drm/renderer.h @@ -45,14 +45,14 @@ void finish_drm_renderer(struct wlr_drm_renderer *renderer); bool drm_surface_make_current(struct wlr_drm_surface *surf, int *buffer_age); void drm_surface_unset_current(struct wlr_drm_surface *surf); -void drm_fb_clear(struct wlr_drm_fb *fb); -bool drm_fb_lock_surface(struct wlr_drm_fb *fb, struct wlr_drm_backend *drm, +bool drm_fb_lock_surface(struct wlr_drm_fb **fb, struct wlr_drm_backend *drm, struct wlr_drm_surface *surf, struct wlr_drm_surface *mgpu); -bool drm_fb_import(struct wlr_drm_fb *fb, struct wlr_drm_backend *drm, +bool drm_fb_import(struct wlr_drm_fb **fb, struct wlr_drm_backend *drm, struct wlr_buffer *buf, struct wlr_drm_surface *mgpu, struct wlr_drm_format_set *set); -void drm_fb_move(struct wlr_drm_fb *new, struct wlr_drm_fb *old); +void drm_fb_clear(struct wlr_drm_fb **fb); +void drm_fb_move(struct wlr_drm_fb **new, struct wlr_drm_fb **old); bool drm_surface_render_black_frame(struct wlr_drm_surface *surf);