Skip to content
This repository has been archived by the owner on Nov 1, 2021. It is now read-only.

Commit

Permalink
WIP: add support for direct scan-out
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed Apr 9, 2019
1 parent b6d0de1 commit 783d63a
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 7 deletions.
47 changes: 47 additions & 0 deletions backend/drm/drm.c
Expand Up @@ -818,6 +818,52 @@ static bool drm_connector_schedule_frame(struct wlr_output *output) {
return true;
}

static bool drm_connector_set_dmabuf(struct wlr_output *output,
struct wlr_dmabuf_attributes *attribs) {
struct wlr_drm_connector *conn = get_drm_connector_from_output(output);
struct wlr_drm_backend *drm = get_drm_backend_from_backend(output->backend);
if (!drm->session->active) {
return false;
}

struct wlr_drm_crtc *crtc = conn->crtc;
if (!crtc) {
return false;
}

// TODO: check plane input formats

if (attribs->width != output->width || attribs->height != output->height) {
return false;
}

struct gbm_bo *bo = import_gbm_bo(&drm->renderer, attribs);
if (bo == NULL) {
wlr_log(WLR_ERROR, "import_gbm_bo failed");
return NULL;
}

uint32_t fb_id = get_fb_for_bo(bo, gbm_bo_get_format(bo));
if (fb_id == 0) {
wlr_log(WLR_ERROR, "get_fb_for_bo failed");
return false;
}

if (conn->pageflip_pending) {
wlr_log(WLR_ERROR, "Skipping pageflip on output '%s'", conn->output.name);
return false;
}

if (!drm->iface->crtc_pageflip(drm, conn, crtc, fb_id, NULL)) {
wlr_log(WLR_ERROR, "crtc_pageflip failed");
return false;
}

conn->pageflip_pending = true;
wlr_output_update_enabled(output, true);
return true;
}

static void drm_connector_destroy(struct wlr_output *output) {
struct wlr_drm_connector *conn = get_drm_connector_from_output(output);
drm_connector_cleanup(conn);
Expand All @@ -840,6 +886,7 @@ static const struct wlr_output_impl output_impl = {
.get_gamma_size = drm_connector_get_gamma_size,
.export_dmabuf = drm_connector_export_dmabuf,
.schedule_frame = drm_connector_schedule_frame,
.set_dmabuf = drm_connector_set_dmabuf,
};

bool wlr_output_is_drm(struct wlr_output *output) {
Expand Down
24 changes: 24 additions & 0 deletions backend/drm/renderer.c
Expand Up @@ -165,6 +165,30 @@ void post_drm_surface(struct wlr_drm_surface *surf) {
}
}

struct gbm_bo *import_gbm_bo(struct wlr_drm_renderer *renderer,
struct wlr_dmabuf_attributes *attribs) {
struct gbm_import_fd_modifier_data data = {
.width = attribs->width,
.height = attribs->height,
.format = attribs->format,
.num_fds = attribs->n_planes,
.modifier = attribs->modifier,
};

if ((size_t)attribs->n_planes > sizeof(data.fds) / sizeof(data.fds[0])) {
return NULL;
}

for (size_t i = 0; i < (size_t)attribs->n_planes; ++i) {
data.fds[i] = attribs->fd[i];
data.strides[i] = attribs->stride[i];
data.offsets[i] = attribs->offset[i];
}

return gbm_bo_import(renderer->gbm, GBM_BO_IMPORT_FD_MODIFIER,
&data, GBM_BO_USE_SCANOUT);
}

bool export_drm_bo(struct gbm_bo *bo, struct wlr_dmabuf_attributes *attribs) {
memset(attribs, 0, sizeof(struct wlr_dmabuf_attributes));

Expand Down
11 changes: 4 additions & 7 deletions backend/drm/util.c
Expand Up @@ -184,21 +184,18 @@ uint32_t get_fb_for_bo(struct gbm_bo *bo, uint32_t drm_format) {
return id;
}

assert(gbm_bo_get_format(bo) == GBM_FORMAT_ARGB8888);
assert(drm_format == DRM_FORMAT_ARGB8888 ||
drm_format == DRM_FORMAT_XRGB8888);

struct gbm_device *gbm = gbm_bo_get_device(bo);

int fd = gbm_device_get_fd(gbm);
uint32_t width = gbm_bo_get_width(bo);
uint32_t height = gbm_bo_get_height(bo);
uint32_t handles[4] = {gbm_bo_get_handle(bo).u32};
uint32_t pitches[4] = {gbm_bo_get_stride(bo)};
uint32_t strides[4] = {gbm_bo_get_stride(bo)};
uint32_t offsets[4] = {gbm_bo_get_offset(bo, 0)};
uint64_t modifiers[4] = {gbm_bo_get_modifier(bo)};

if (drmModeAddFB2(fd, width, height, drm_format,
handles, pitches, offsets, &id, 0)) {
if (drmModeAddFB2WithModifiers(fd, width, height, drm_format,
handles, strides, offsets, modifiers, &id, DRM_MODE_FB_MODIFIERS)) {
wlr_log_errno(WLR_ERROR, "Unable to add DRM framebuffer");
}

Expand Down
2 changes: 2 additions & 0 deletions include/backend/drm/renderer.h
Expand Up @@ -54,6 +54,8 @@ struct gbm_bo *get_drm_surface_front(struct wlr_drm_surface *surf);
void post_drm_surface(struct wlr_drm_surface *surf);
struct gbm_bo *copy_drm_surface_mgpu(struct wlr_drm_surface *dest,
struct gbm_bo *src);
struct gbm_bo *import_gbm_bo(struct wlr_drm_renderer *renderer,
struct wlr_dmabuf_attributes *attribs);
bool export_drm_bo(struct gbm_bo *bo, struct wlr_dmabuf_attributes *attribs);

#endif
2 changes: 2 additions & 0 deletions include/wlr/interfaces/wlr_output.h
Expand Up @@ -34,6 +34,8 @@ struct wlr_output_impl {
bool (*export_dmabuf)(struct wlr_output *output,
struct wlr_dmabuf_attributes *attribs);
bool (*schedule_frame)(struct wlr_output *output);
bool (*set_dmabuf)(struct wlr_output *output,
struct wlr_dmabuf_attributes *attribs);
};

void wlr_output_init(struct wlr_output *output, struct wlr_backend *backend,
Expand Down
2 changes: 2 additions & 0 deletions include/wlr/types/wlr_output.h
Expand Up @@ -212,6 +212,8 @@ bool wlr_output_preferred_read_format(struct wlr_output *output,
*/
bool wlr_output_swap_buffers(struct wlr_output *output, struct timespec *when,
pixman_region32_t *damage);
bool wlr_output_set_dmabuf(struct wlr_output *output,
struct wlr_dmabuf_attributes *attribs);
/**
* Manually schedules a `frame` event. If a `frame` event is already pending,
* it is a no-op.
Expand Down
16 changes: 16 additions & 0 deletions rootston/render.c
Expand Up @@ -6,6 +6,8 @@
#include <wlr/config.h>
#include <wlr/types/wlr_compositor.h>
#include <wlr/types/wlr_matrix.h>
#include <wlr/types/wlr_buffer.h>
#include <wlr/types/wlr_linux_dmabuf_v1.h>
#include <wlr/util/log.h>
#include <wlr/util/region.h>
#include "rootston/layers.h"
Expand Down Expand Up @@ -264,6 +266,20 @@ void output_render(struct roots_output *output) {
if (output->fullscreen_view != NULL) {
struct roots_view *view = output->fullscreen_view;

// TODO: check this is the only surface on screen
// TODO: don't rely on the resource
struct wl_resource *buffer_resource =
output->fullscreen_view->wlr_surface->buffer->resource;
if (wlr_dmabuf_v1_resource_is_buffer(buffer_resource)) {
wlr_log(WLR_ERROR, "YES it's a dmabuf");
struct wlr_dmabuf_v1_buffer *dmabuf_buffer =
wlr_dmabuf_v1_buffer_from_buffer_resource(buffer_resource);
if (wlr_output_set_dmabuf(wlr_output, &dmabuf_buffer->attributes)) {
wlr_log(WLR_ERROR, "YES scanned out!");
goto damage_finish;
}
}

render_view(output, view, &data);

// During normal rendering the xwayland window tree isn't traversed
Expand Down
24 changes: 24 additions & 0 deletions types/wlr_output.c
Expand Up @@ -432,6 +432,30 @@ bool wlr_output_swap_buffers(struct wlr_output *output, struct timespec *when,
return true;
}

bool wlr_output_set_dmabuf(struct wlr_output *output,
struct wlr_dmabuf_attributes *attribs) {
if (output->frame_pending) {
wlr_log(WLR_ERROR, "Tried to swap buffers when a frame is pending");
return false;
}
if (output->idle_frame != NULL) {
wl_event_source_remove(output->idle_frame);
output->idle_frame = NULL;
}

if (!output->impl->set_dmabuf) {
return false;
}
if (!output->impl->set_dmabuf(output, attribs)) {
return false;
}

output->frame_pending = true;
output->needs_swap = false;
pixman_region32_clear(&output->damage);
return true;
}

void wlr_output_send_frame(struct wlr_output *output) {
output->frame_pending = false;
wlr_signal_emit_safe(&output->events.frame, output);
Expand Down

0 comments on commit 783d63a

Please sign in to comment.