Skip to content

Commit

Permalink
backend/drm: use wlr_swapchain
Browse files Browse the repository at this point in the history
  • Loading branch information
emersion committed Jun 1, 2020
1 parent 82e6569 commit 740c15e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 48 deletions.
2 changes: 1 addition & 1 deletion backend/drm/drm.c
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ static bool drm_connector_set_cursor(struct wlr_output *output,
return false;
}

if (!plane->surf.gbm) {
if (!plane->surf.swapchain) {
int ret;
uint64_t w, h;
ret = drmGetCap(drm->fd, DRM_CAP_CURSOR_WIDTH, &w);
Expand Down
93 changes: 52 additions & 41 deletions backend/drm/renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
#include <wlr/types/wlr_matrix.h>
#include <wlr/util/log.h>
#include "backend/drm/drm.h"
#include "render/gbm_allocator.h"
#include "render/swapchain.h"

bool init_drm_renderer(struct wlr_drm_backend *drm,
struct wlr_drm_renderer *renderer, wlr_renderer_create_func_t create_renderer_func) {
// TODO: get rid of renderer->gbm
renderer->gbm = gbm_create_device(drm->fd);
if (!renderer->gbm) {
wlr_log(WLR_ERROR, "Failed to create GBM device");
Expand Down Expand Up @@ -44,9 +47,17 @@ bool init_drm_renderer(struct wlr_drm_backend *drm,
goto error_gbm;
}

renderer->allocator = wlr_gbm_allocator_create(drm->fd);
if (renderer->allocator == NULL) {
wlr_log(WLR_ERROR, "Failed to create allocator");
goto error_wlr_rend;
}

renderer->fd = drm->fd;
return true;

error_wlr_rend:
wlr_renderer_destroy(renderer->wlr_rend);
error_gbm:
gbm_device_destroy(renderer->gbm);
return false;
Expand All @@ -57,14 +68,15 @@ void finish_drm_renderer(struct wlr_drm_renderer *renderer) {
return;
}

wlr_allocator_destroy(&renderer->allocator->base);
wlr_renderer_destroy(renderer->wlr_rend);
wlr_egl_finish(&renderer->egl);
gbm_device_destroy(renderer->gbm);
}

static bool init_drm_surface(struct wlr_drm_surface *surf,
struct wlr_drm_renderer *renderer, uint32_t width, uint32_t height,
uint32_t format, struct wlr_drm_format_set *set, uint32_t flags) {
uint32_t format, const struct wlr_drm_format_set *set, uint32_t flags) {
if (surf->width == width && surf->height == height) {
return true;
}
Expand All @@ -73,61 +85,53 @@ static bool init_drm_surface(struct wlr_drm_surface *surf,
surf->width = width;
surf->height = height;

if (surf->gbm) {
gbm_surface_destroy(surf->gbm);
surf->gbm = NULL;
}
wlr_egl_destroy_surface(&surf->renderer->egl, surf->egl);

if (!(flags & GBM_BO_USE_LINEAR) && set != NULL) {
const struct wlr_drm_format *drm_format =
wlr_drm_format_set_get(set, format);
if (drm_format != NULL) {
surf->gbm = gbm_surface_create_with_modifiers(renderer->gbm,
width, height, format, drm_format->modifiers, drm_format->len);
}
}
wlr_buffer_unlock(surf->back_buffer);
surf->back_buffer = NULL;
wlr_swapchain_destroy(surf->swapchain);
surf->swapchain = NULL;

if (surf->gbm == NULL) {
surf->gbm = gbm_surface_create(renderer->gbm, width, height,
format, GBM_BO_USE_RENDERING | flags);
}
if (!surf->gbm) {
wlr_log_errno(WLR_ERROR, "Failed to create GBM surface");
goto error_zero;
const struct wlr_drm_format *drm_format = NULL;
const struct wlr_drm_format format_no_modifiers = { .format = format };
if (set != NULL) {
drm_format = wlr_drm_format_set_get(set, format);
} else {
drm_format = &format_no_modifiers;
}
// TODO: flags

surf->egl = wlr_egl_create_surface(&renderer->egl, surf->gbm);
if (surf->egl == EGL_NO_SURFACE) {
wlr_log(WLR_ERROR, "Failed to create EGL surface");
goto error_gbm;
surf->swapchain = wlr_swapchain_create(&renderer->allocator->base,
width, height, drm_format);
if (surf->swapchain == NULL) {
wlr_log(WLR_ERROR, "Failed to create swapchain");
memset(surf, 0, sizeof(*surf));
return false;
}

return true;

error_gbm:
gbm_surface_destroy(surf->gbm);
error_zero:
memset(surf, 0, sizeof(*surf));
return false;
}

static void finish_drm_surface(struct wlr_drm_surface *surf) {
if (!surf || !surf->renderer) {
return;
}

wlr_egl_destroy_surface(&surf->renderer->egl, surf->egl);
if (surf->gbm) {
gbm_surface_destroy(surf->gbm);
}
wlr_buffer_unlock(surf->back_buffer);
wlr_swapchain_destroy(surf->swapchain);

memset(surf, 0, sizeof(*surf));
}

bool drm_surface_make_current(struct wlr_drm_surface *surf,
int *buffer_damage) {
return wlr_egl_make_current(&surf->renderer->egl, surf->egl, buffer_damage);
assert(surf->back_buffer == NULL);
surf->back_buffer = wlr_swapchain_acquire(surf->swapchain);
if (surf->back_buffer == NULL) {
wlr_log(WLR_ERROR, "Failed to acquire swapchain buffer");
return false;
}

// TODO: glider_gl_renderer_begin
return false;
}

bool export_drm_bo(struct gbm_bo *bo, struct wlr_dmabuf_attributes *attribs) {
Expand Down Expand Up @@ -249,7 +253,8 @@ void drm_fb_clear(struct wlr_drm_fb *fb) {
assert(!fb->bo);
break;
case WLR_DRM_FB_TYPE_SURFACE:
gbm_surface_release_buffer(fb->surf->gbm, fb->bo);
wlr_buffer_unlock(fb->wlr_buf);
fb->wlr_buf = NULL;
break;
case WLR_DRM_FB_TYPE_WLR_BUFFER:
gbm_bo_destroy(fb->bo);
Expand All @@ -270,9 +275,13 @@ void drm_fb_clear(struct wlr_drm_fb *fb) {
}

bool drm_fb_lock_surface(struct wlr_drm_fb *fb, struct wlr_drm_surface *surf) {
assert(surf->back_buffer != NULL);

// TODO: glider_gl_renderer_end

drm_fb_clear(fb);

if (!wlr_egl_swap_buffers(&surf->renderer->egl, surf->egl, NULL)) {
/*if (!wlr_egl_swap_buffers(&surf->renderer->egl, surf->egl, NULL)) {
wlr_log(WLR_ERROR, "Failed to swap buffers");
return false;
}
Expand All @@ -281,10 +290,12 @@ bool drm_fb_lock_surface(struct wlr_drm_fb *fb, struct wlr_drm_surface *surf) {
if (!fb->bo) {
wlr_log(WLR_ERROR, "Failed to lock front buffer");
return false;
}
}*/

fb->type = WLR_DRM_FB_TYPE_SURFACE;
fb->surf = surf;
fb->wlr_buf = surf->back_buffer;
surf->back_buffer = NULL;

return true;
}

Expand Down
10 changes: 4 additions & 6 deletions include/backend/drm/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ struct wlr_drm_renderer {
uint32_t gbm_format;

struct wlr_renderer *wlr_rend;
struct wlr_gbm_allocator *allocator;
};

struct wlr_drm_surface {
Expand All @@ -28,8 +29,8 @@ struct wlr_drm_surface {
uint32_t width;
uint32_t height;

struct gbm_surface *gbm;
EGLSurface egl;
struct wlr_swapchain *swapchain;
struct wlr_buffer *back_buffer;
};

enum wlr_drm_fb_type {
Expand All @@ -45,10 +46,7 @@ struct wlr_drm_fb {
struct wlr_drm_surface *mgpu_surf;
struct gbm_bo *mgpu_bo;

union {
struct wlr_drm_surface *surf;
struct wlr_buffer *wlr_buf;
};
struct wlr_buffer *wlr_buf;
};

bool init_drm_renderer(struct wlr_drm_backend *drm,
Expand Down

0 comments on commit 740c15e

Please sign in to comment.