backend/headless: switch to wlr_swapchain

This commit is contained in:
Simon Ser 2020-08-22 17:24:16 +02:00
parent eb8360bda3
commit 61f8cdfb9e
3 changed files with 83 additions and 84 deletions

View File

@ -1,12 +1,18 @@
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <drm_fourcc.h>
#include <stdlib.h>
#include <unistd.h>
#include <wlr/interfaces/wlr_input_device.h>
#include <wlr/interfaces/wlr_output.h>
#include <wlr/render/egl.h>
#include <wlr/render/wlr_renderer.h>
#include <wlr/render/gles2.h>
#include <wlr/util/log.h>
#include <xf86drm.h>
#include "backend/headless.h"
#include "render/drm_format_set.h"
#include "render/gbm_allocator.h"
#include "util/signal.h"
struct wlr_headless_backend *headless_backend_from_backend(
@ -62,6 +68,8 @@ static void backend_destroy(struct wlr_backend *wlr_backend) {
wlr_signal_emit_safe(&wlr_backend->events.destroy, backend);
free(backend->format);
wlr_allocator_destroy(backend->allocator);
if (backend->egl == &backend->priv_egl) {
wlr_renderer_destroy(backend->renderer);
wlr_egl_finish(&backend->priv_egl);
@ -104,18 +112,40 @@ static bool backend_init(struct wlr_headless_backend *backend,
backend->renderer = renderer;
backend->egl = wlr_gles2_renderer_get_egl(renderer);
if (wlr_gles2_renderer_check_ext(backend->renderer, "GL_OES_rgb8_rgba8") ||
wlr_gles2_renderer_check_ext(backend->renderer,
"GL_OES_required_internalformat") ||
wlr_gles2_renderer_check_ext(backend->renderer, "GL_ARM_rgba8")) {
backend->internal_format = GL_RGBA8_OES;
} else {
wlr_log(WLR_INFO, "GL_RGBA8_OES not supported, "
"falling back to GL_RGBA4 internal format "
"(performance may be affected)");
backend->internal_format = GL_RGBA4;
int fd = wlr_renderer_get_drm_fd(renderer);
if (fd < 0) {
wlr_log(WLR_ERROR, "Failed to get DRM device FD from renderer");
return false;
}
fd = dup(fd);
if (fd < 0) {
wlr_log_errno(WLR_ERROR, "dup failed");
return false;
}
struct wlr_gbm_allocator *alloc = wlr_gbm_allocator_create(fd);
if (alloc == NULL) {
wlr_log(WLR_ERROR, "Failed to create GBM allocator");
return false;
}
backend->allocator = &alloc->base;
const struct wlr_drm_format_set *formats =
wlr_renderer_get_dmabuf_formats(backend->renderer);
if (formats == NULL) {
wlr_log(WLR_ERROR, "Failed to get available DMA-BUF formats from renderer");
return false;
}
// TODO: filter modifiers with external_only=false
const struct wlr_drm_format *format =
wlr_drm_format_set_get(formats, DRM_FORMAT_XRGB8888);
if (format == NULL) {
wlr_log(WLR_ERROR, "Renderer doesn't support XRGB8888");
return false;
}
backend->format = wlr_drm_format_dup(format);
backend->display_destroy.notify = handle_display_destroy;
wl_display_add_destroy_listener(display, &backend->display_destroy);

View File

@ -1,12 +1,12 @@
#include <assert.h>
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#include <stdlib.h>
#include <stdio.h>
#include <wlr/interfaces/wlr_output.h>
#include <wlr/render/wlr_renderer.h>
#include <wlr/util/log.h>
#include "backend/headless.h"
#include "render/swapchain.h"
#include "render/wlr_renderer.h"
#include "util/signal.h"
static struct wlr_headless_output *headless_output_from_output(
@ -15,53 +15,6 @@ static struct wlr_headless_output *headless_output_from_output(
return (struct wlr_headless_output *)wlr_output;
}
static bool create_fbo(struct wlr_headless_output *output,
unsigned int width, unsigned int height) {
if (!wlr_egl_make_current(output->backend->egl, EGL_NO_SURFACE, NULL)) {
return false;
}
GLuint rbo;
glGenRenderbuffers(1, &rbo);
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
glRenderbufferStorage(GL_RENDERBUFFER, output->backend->internal_format,
width, height);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
GLuint fbo;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_RENDERBUFFER, rbo);
GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
wlr_egl_unset_current(output->backend->egl);
if (status != GL_FRAMEBUFFER_COMPLETE) {
wlr_log(WLR_ERROR, "Failed to create FBO");
return false;
}
output->fbo = fbo;
output->rbo = rbo;
return true;
}
static void destroy_fbo(struct wlr_headless_output *output) {
if (!wlr_egl_make_current(output->backend->egl, EGL_NO_SURFACE, NULL)) {
return;
}
glDeleteFramebuffers(1, &output->fbo);
glDeleteRenderbuffers(1, &output->rbo);
wlr_egl_unset_current(output->backend->egl);
output->fbo = 0;
output->rbo = 0;
}
static bool output_set_custom_mode(struct wlr_output *wlr_output, int32_t width,
int32_t height, int32_t refresh) {
struct wlr_headless_output *output =
@ -71,8 +24,10 @@ static bool output_set_custom_mode(struct wlr_output *wlr_output, int32_t width,
refresh = HEADLESS_DEFAULT_REFRESH;
}
destroy_fbo(output);
if (!create_fbo(output, width, height)) {
wlr_swapchain_destroy(output->swapchain);
output->swapchain = wlr_swapchain_create(output->backend->allocator,
width, height, output->backend->format);
if (!output->swapchain) {
wlr_output_destroy(wlr_output);
return false;
}
@ -88,15 +43,20 @@ static bool output_attach_render(struct wlr_output *wlr_output,
struct wlr_headless_output *output =
headless_output_from_output(wlr_output);
if (!wlr_egl_make_current(output->backend->egl, EGL_NO_SURFACE, NULL)) {
wlr_buffer_unlock(output->back_buffer);
output->back_buffer = wlr_swapchain_acquire(output->swapchain, buffer_age);
if (!output->back_buffer) {
return false;
}
glBindFramebuffer(GL_FRAMEBUFFER, output->fbo);
if (buffer_age != NULL) {
*buffer_age = 0; // We only have one buffer
if (!wlr_egl_make_current(output->backend->egl, EGL_NO_SURFACE, NULL)) {
return false;
}
if (!wlr_renderer_bind_buffer(output->backend->renderer,
output->back_buffer)) {
return false;
}
return true;
}
@ -131,10 +91,18 @@ static bool output_commit(struct wlr_output *wlr_output) {
}
if (wlr_output->pending.committed & WLR_OUTPUT_STATE_BUFFER) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
assert(output->back_buffer != NULL);
wlr_renderer_bind_buffer(output->backend->renderer, NULL);
wlr_egl_unset_current(output->backend->egl);
// Nothing needs to be done for FBOs
wlr_buffer_unlock(output->front_buffer);
output->front_buffer = output->back_buffer;
output->back_buffer = NULL;
wlr_swapchain_set_buffer_submitted(output->swapchain,
output->front_buffer);
wlr_output_send_present(wlr_output, NULL);
}
@ -145,8 +113,12 @@ static void output_rollback_render(struct wlr_output *wlr_output) {
struct wlr_headless_output *output =
headless_output_from_output(wlr_output);
assert(wlr_egl_is_current(output->backend->egl));
glBindFramebuffer(GL_FRAMEBUFFER, 0);
wlr_renderer_bind_buffer(output->backend->renderer, NULL);
wlr_egl_unset_current(output->backend->egl);
wlr_buffer_unlock(output->back_buffer);
output->back_buffer = NULL;
}
static void output_destroy(struct wlr_output *wlr_output) {
@ -154,7 +126,9 @@ static void output_destroy(struct wlr_output *wlr_output) {
headless_output_from_output(wlr_output);
wl_list_remove(&output->link);
wl_event_source_remove(output->frame_timer);
destroy_fbo(output);
wlr_swapchain_destroy(output->swapchain);
wlr_buffer_unlock(output->back_buffer);
wlr_buffer_unlock(output->front_buffer);
free(output);
}
@ -192,7 +166,9 @@ struct wlr_output *wlr_headless_add_output(struct wlr_backend *wlr_backend,
backend->display);
struct wlr_output *wlr_output = &output->wlr_output;
if (!create_fbo(output, width, height)) {
output->swapchain = wlr_swapchain_create(backend->allocator,
width, height, backend->format);
if (!output->swapchain) {
goto error;
}
@ -207,14 +183,6 @@ struct wlr_output *wlr_headless_add_output(struct wlr_backend *wlr_backend,
"Headless output %zd", backend->last_output_num);
wlr_output_set_description(wlr_output, description);
if (!output_attach_render(wlr_output, NULL)) {
goto error;
}
wlr_renderer_begin(backend->renderer, wlr_output->width, wlr_output->height);
wlr_renderer_clear(backend->renderer, (float[]){ 1.0, 1.0, 1.0, 1.0 });
wlr_renderer_end(backend->renderer);
struct wl_event_loop *ev = wl_display_get_event_loop(backend->display);
output->frame_timer = wl_event_loop_add_timer(ev, signal_frame, output);

View File

@ -3,7 +3,6 @@
#include <wlr/backend/headless.h>
#include <wlr/backend/interface.h>
#include <wlr/render/gles2.h>
#define HEADLESS_DEFAULT_REFRESH (60 * 1000) // 60 Hz
@ -12,6 +11,8 @@ struct wlr_headless_backend {
struct wlr_egl priv_egl; // may be uninitialized
struct wlr_egl *egl;
struct wlr_renderer *renderer;
struct wlr_allocator *allocator;
struct wlr_drm_format *format;
struct wl_display *display;
struct wl_list outputs;
size_t last_output_num;
@ -19,7 +20,6 @@ struct wlr_headless_backend {
struct wl_listener display_destroy;
struct wl_listener renderer_destroy;
bool started;
GLenum internal_format;
};
struct wlr_headless_output {
@ -28,7 +28,8 @@ struct wlr_headless_output {
struct wlr_headless_backend *backend;
struct wl_list link;
GLuint fbo, rbo;
struct wlr_swapchain *swapchain;
struct wlr_buffer *back_buffer, *front_buffer;
struct wl_event_source *frame_timer;
int frame_delay; // ms