2023-02-21 18:12:05 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <drm_fourcc.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <wlr/render/allocator.h>
|
|
|
|
#include <wlr/render/swapchain.h>
|
|
|
|
#include <wlr/util/log.h>
|
|
|
|
#include <xf86drm.h>
|
|
|
|
|
|
|
|
#include "render/drm_format_set.h"
|
|
|
|
#include "types/wlr_output.h"
|
|
|
|
|
|
|
|
static struct wlr_swapchain *create_swapchain(struct wlr_output *output,
|
2023-05-26 13:42:17 +02:00
|
|
|
int width, int height, uint32_t render_format, bool allow_modifiers) {
|
2023-02-21 18:12:05 +01:00
|
|
|
struct wlr_allocator *allocator = output->allocator;
|
|
|
|
assert(output->allocator != NULL);
|
|
|
|
|
|
|
|
const struct wlr_drm_format_set *display_formats =
|
|
|
|
wlr_output_get_primary_formats(output, allocator->buffer_caps);
|
2023-05-05 01:24:44 +02:00
|
|
|
struct wlr_drm_format format = {0};
|
2023-05-26 13:42:17 +02:00
|
|
|
if (!output_pick_format(output, display_formats, &format, render_format)) {
|
2023-02-21 18:12:05 +01:00
|
|
|
wlr_log(WLR_ERROR, "Failed to pick primary buffer format for output '%s'",
|
|
|
|
output->name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-05-05 01:24:44 +02:00
|
|
|
char *format_name = drmGetFormatName(format.format);
|
2023-02-21 18:12:05 +01:00
|
|
|
wlr_log(WLR_DEBUG, "Choosing primary buffer format %s (0x%08"PRIX32") for output '%s'",
|
2023-05-05 01:24:44 +02:00
|
|
|
format_name ? format_name : "<unknown>", format.format, output->name);
|
2023-02-21 18:12:05 +01:00
|
|
|
free(format_name);
|
|
|
|
|
2023-05-05 01:24:44 +02:00
|
|
|
if (!allow_modifiers && (format.len != 1 || format.modifiers[0] != DRM_FORMAT_MOD_LINEAR)) {
|
|
|
|
if (!wlr_drm_format_has(&format, DRM_FORMAT_MOD_INVALID)) {
|
2023-02-21 18:12:05 +01:00
|
|
|
wlr_log(WLR_DEBUG, "Implicit modifiers not supported");
|
2023-05-05 01:24:44 +02:00
|
|
|
wlr_drm_format_finish(&format);
|
2023-02-21 18:12:05 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2023-05-05 01:24:44 +02:00
|
|
|
format.len = 0;
|
|
|
|
if (!wlr_drm_format_add(&format, DRM_FORMAT_MOD_INVALID)) {
|
|
|
|
wlr_log(WLR_DEBUG, "Failed to add implicit modifier to format");
|
|
|
|
wlr_drm_format_finish(&format);
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-02-21 18:12:05 +01:00
|
|
|
}
|
|
|
|
|
2023-05-05 01:24:44 +02:00
|
|
|
struct wlr_swapchain *swapchain = wlr_swapchain_create(allocator, width, height, &format);
|
|
|
|
wlr_drm_format_finish(&format);
|
2023-02-21 18:12:05 +01:00
|
|
|
return swapchain;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool test_swapchain(struct wlr_output *output,
|
|
|
|
struct wlr_swapchain *swapchain, const struct wlr_output_state *state) {
|
|
|
|
struct wlr_buffer *buffer = wlr_swapchain_acquire(swapchain, NULL);
|
|
|
|
if (buffer == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wlr_output_state copy = *state;
|
|
|
|
copy.committed |= WLR_OUTPUT_STATE_BUFFER;
|
|
|
|
copy.buffer = buffer;
|
|
|
|
bool ok = wlr_output_test_state(output, ©);
|
|
|
|
wlr_buffer_unlock(buffer);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wlr_output_configure_primary_swapchain(struct wlr_output *output,
|
|
|
|
const struct wlr_output_state *state, struct wlr_swapchain **swapchain_ptr) {
|
2023-06-22 15:48:35 +02:00
|
|
|
struct wlr_output_state empty_state;
|
2023-02-21 18:12:05 +01:00
|
|
|
if (state == NULL) {
|
2023-06-22 15:48:35 +02:00
|
|
|
wlr_output_state_init(&empty_state);
|
2023-02-21 18:12:05 +01:00
|
|
|
state = &empty_state;
|
|
|
|
}
|
|
|
|
|
|
|
|
int width, height;
|
|
|
|
output_pending_resolution(output, state, &width, &height);
|
|
|
|
|
2023-05-26 13:42:17 +02:00
|
|
|
uint32_t format = output->render_format;
|
|
|
|
if (state->committed & WLR_OUTPUT_STATE_RENDER_FORMAT) {
|
|
|
|
format = state->render_format;
|
|
|
|
}
|
|
|
|
|
2023-02-21 18:12:05 +01:00
|
|
|
// Re-use the existing swapchain if possible
|
|
|
|
struct wlr_swapchain *old_swapchain = *swapchain_ptr;
|
|
|
|
if (old_swapchain != NULL &&
|
|
|
|
old_swapchain->width == width && old_swapchain->height == height &&
|
2023-05-26 13:42:17 +02:00
|
|
|
old_swapchain->format.format == format) {
|
2023-02-21 18:12:05 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-05-26 13:42:17 +02:00
|
|
|
struct wlr_swapchain *swapchain = create_swapchain(output, width, height, format, true);
|
2023-02-21 18:12:05 +01:00
|
|
|
if (swapchain == NULL) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to create swapchain for output '%s'", output->name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
wlr_log(WLR_DEBUG, "Testing swapchain for output '%s'", output->name);
|
|
|
|
if (!test_swapchain(output, swapchain, state)) {
|
|
|
|
wlr_log(WLR_DEBUG, "Output test failed on '%s', retrying without modifiers",
|
|
|
|
output->name);
|
|
|
|
wlr_swapchain_destroy(swapchain);
|
2023-05-26 13:42:17 +02:00
|
|
|
swapchain = create_swapchain(output, width, height, format, false);
|
2023-02-21 18:12:05 +01:00
|
|
|
if (swapchain == NULL) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to create modifier-less swapchain for output '%s'",
|
|
|
|
output->name);
|
2023-06-22 13:55:45 +02:00
|
|
|
return false;
|
2023-02-21 18:12:05 +01:00
|
|
|
}
|
|
|
|
wlr_log(WLR_DEBUG, "Testing modifier-less swapchain for output '%s'", output->name);
|
|
|
|
if (!test_swapchain(output, swapchain, state)) {
|
|
|
|
wlr_log(WLR_ERROR, "Swapchain for output '%s' failed test", output->name);
|
|
|
|
wlr_swapchain_destroy(swapchain);
|
2023-06-22 13:55:45 +02:00
|
|
|
return false;
|
2023-02-21 18:12:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wlr_swapchain_destroy(*swapchain_ptr);
|
|
|
|
*swapchain_ptr = swapchain;
|
|
|
|
return true;
|
|
|
|
}
|