2021-10-22 23:10:47 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <drm_fourcc.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <wlr/interfaces/wlr_output.h>
|
|
|
|
#include <wlr/render/interface.h>
|
2023-02-20 19:04:20 +01:00
|
|
|
#include <wlr/render/swapchain.h>
|
2021-10-22 23:10:47 +02:00
|
|
|
#include <wlr/util/log.h>
|
2022-09-07 10:49:25 +02:00
|
|
|
#include <xf86drm.h>
|
2021-10-22 23:10:47 +02:00
|
|
|
#include "backend/backend.h"
|
|
|
|
#include "render/allocator/allocator.h"
|
|
|
|
#include "render/drm_format_set.h"
|
|
|
|
#include "render/wlr_renderer.h"
|
2021-11-11 05:20:10 +01:00
|
|
|
#include "render/pixel_format.h"
|
2021-10-22 23:10:47 +02:00
|
|
|
#include "types/wlr_output.h"
|
|
|
|
|
2021-11-05 12:26:38 +01:00
|
|
|
bool wlr_output_init_render(struct wlr_output *output,
|
|
|
|
struct wlr_allocator *allocator, struct wlr_renderer *renderer) {
|
2022-12-01 10:57:53 +01:00
|
|
|
assert(allocator != NULL && renderer != NULL);
|
|
|
|
assert(output->back_buffer == NULL);
|
2021-11-05 12:26:38 +01:00
|
|
|
|
|
|
|
uint32_t backend_caps = backend_get_buffer_caps(output->backend);
|
|
|
|
uint32_t renderer_caps = renderer_get_render_buffer_caps(renderer);
|
|
|
|
|
|
|
|
if (!(backend_caps & allocator->buffer_caps)) {
|
|
|
|
wlr_log(WLR_ERROR, "output backend and allocator buffer capabilities "
|
|
|
|
"don't match");
|
|
|
|
return false;
|
2021-11-19 15:24:07 +01:00
|
|
|
} else if (!(renderer_caps & allocator->buffer_caps)) {
|
|
|
|
wlr_log(WLR_ERROR, "renderer and allocator buffer capabilities "
|
2021-11-05 12:26:38 +01:00
|
|
|
"don't match");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-12-01 10:57:53 +01:00
|
|
|
wlr_swapchain_destroy(output->swapchain);
|
|
|
|
output->swapchain = NULL;
|
|
|
|
|
|
|
|
wlr_swapchain_destroy(output->cursor_swapchain);
|
|
|
|
output->cursor_swapchain = NULL;
|
|
|
|
|
2021-11-05 12:26:38 +01:00
|
|
|
output->allocator = allocator;
|
|
|
|
output->renderer = renderer;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-20 11:32:17 +02:00
|
|
|
void output_clear_back_buffer(struct wlr_output *output) {
|
|
|
|
if (output->back_buffer == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wlr_renderer *renderer = output->renderer;
|
|
|
|
assert(renderer != NULL);
|
|
|
|
|
|
|
|
renderer_bind_buffer(renderer, NULL);
|
|
|
|
|
|
|
|
wlr_buffer_unlock(output->back_buffer);
|
|
|
|
output->back_buffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wlr_output_attach_render(struct wlr_output *output, int *buffer_age) {
|
2021-10-22 23:10:47 +02:00
|
|
|
assert(output->back_buffer == NULL);
|
|
|
|
|
2023-06-20 11:32:17 +02:00
|
|
|
if (!wlr_output_configure_primary_swapchain(output, &output->pending,
|
2023-02-22 19:04:02 +01:00
|
|
|
&output->swapchain)) {
|
2021-10-22 23:10:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-05 12:26:38 +01:00
|
|
|
struct wlr_renderer *renderer = output->renderer;
|
2021-10-22 23:10:47 +02:00
|
|
|
assert(renderer != NULL);
|
|
|
|
|
|
|
|
struct wlr_buffer *buffer =
|
|
|
|
wlr_swapchain_acquire(output->swapchain, buffer_age);
|
|
|
|
if (buffer == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!renderer_bind_buffer(renderer, buffer)) {
|
|
|
|
wlr_buffer_unlock(buffer);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
output->back_buffer = buffer;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-06-20 11:32:17 +02:00
|
|
|
static struct wlr_buffer *output_acquire_empty_buffer(struct wlr_output *output,
|
2022-06-03 00:15:42 +02:00
|
|
|
const struct wlr_output_state *state) {
|
2022-05-24 19:43:56 +02:00
|
|
|
assert(!(state->committed & WLR_OUTPUT_STATE_BUFFER));
|
2021-10-22 23:10:47 +02:00
|
|
|
|
2023-06-20 11:32:17 +02:00
|
|
|
// wlr_output_configure_primary_swapchain() function will call
|
|
|
|
// wlr_output_test_state(), which can call us again. This is dangerous: we
|
|
|
|
// risk infinite recursion. However, a buffer will always be supplied in
|
|
|
|
// wlr_output_test_state(), which will prevent us from being called.
|
|
|
|
if (!wlr_output_configure_primary_swapchain(output, state,
|
|
|
|
&output->swapchain)) {
|
2021-10-22 23:10:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-06-20 11:32:17 +02:00
|
|
|
struct wlr_buffer *buffer = wlr_swapchain_acquire(output->swapchain, NULL);
|
|
|
|
if (buffer == NULL) {
|
2022-01-12 18:37:09 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-10-22 23:10:47 +02:00
|
|
|
|
2023-06-20 11:32:17 +02:00
|
|
|
struct wlr_render_pass *pass =
|
|
|
|
wlr_renderer_begin_buffer_pass(output->renderer, buffer, NULL);
|
|
|
|
if (pass == NULL) {
|
|
|
|
wlr_buffer_unlock(buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
wlr_render_pass_add_rect(pass, &(struct wlr_render_rect_options){
|
|
|
|
.color = { 0, 0, 0, 0 },
|
|
|
|
.blend_mode = WLR_RENDER_BLEND_MODE_NONE,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!wlr_render_pass_submit(pass)) {
|
|
|
|
wlr_buffer_unlock(buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer;
|
2021-10-22 23:10:47 +02:00
|
|
|
}
|
|
|
|
|
2023-06-20 11:32:17 +02:00
|
|
|
// This function may attach a new, empty buffer if necessary.
|
2022-06-03 00:15:42 +02:00
|
|
|
// If so, the new_back_buffer out parameter will be set to true.
|
2022-05-24 19:43:56 +02:00
|
|
|
bool output_ensure_buffer(struct wlr_output *output,
|
2023-06-20 11:32:17 +02:00
|
|
|
struct wlr_output_state *state, bool *new_buffer) {
|
|
|
|
assert(*new_buffer == false);
|
2022-06-03 00:15:42 +02:00
|
|
|
|
2022-10-07 14:15:27 +02:00
|
|
|
// If we already have a buffer, we don't need to allocate a new one
|
|
|
|
if (state->committed & WLR_OUTPUT_STATE_BUFFER) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-07 14:19:26 +02:00
|
|
|
// If the compositor hasn't called wlr_output_init_render(), they will use
|
|
|
|
// their own logic to attach buffers
|
|
|
|
if (output->renderer == NULL) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-02-01 12:31:04 +01:00
|
|
|
bool enabled = output->enabled;
|
|
|
|
if (state->committed & WLR_OUTPUT_STATE_ENABLED) {
|
|
|
|
enabled = state->enabled;
|
|
|
|
}
|
|
|
|
|
2021-10-22 23:10:47 +02:00
|
|
|
// If we're lighting up an output or changing its mode, make sure to
|
|
|
|
// provide a new buffer
|
|
|
|
bool needs_new_buffer = false;
|
2022-05-24 19:43:56 +02:00
|
|
|
if ((state->committed & WLR_OUTPUT_STATE_ENABLED) && state->enabled) {
|
2021-10-22 23:10:47 +02:00
|
|
|
needs_new_buffer = true;
|
|
|
|
}
|
2022-05-24 19:43:56 +02:00
|
|
|
if (state->committed & WLR_OUTPUT_STATE_MODE) {
|
2021-10-22 23:10:47 +02:00
|
|
|
needs_new_buffer = true;
|
|
|
|
}
|
2022-05-24 19:43:56 +02:00
|
|
|
if (state->committed & WLR_OUTPUT_STATE_RENDER_FORMAT) {
|
2021-11-11 05:20:10 +01:00
|
|
|
needs_new_buffer = true;
|
|
|
|
}
|
2023-02-01 12:31:04 +01:00
|
|
|
if (state->allow_artifacts && output->commit_seq == 0 && enabled) {
|
2022-10-07 13:51:01 +02:00
|
|
|
// On first commit, require a new buffer if the compositor called a
|
|
|
|
// mode-setting function, even if the mode won't change. This makes it
|
|
|
|
// so the swapchain is created now.
|
|
|
|
needs_new_buffer = true;
|
2021-10-22 23:10:47 +02:00
|
|
|
}
|
2022-10-07 13:51:01 +02:00
|
|
|
if (!needs_new_buffer) {
|
2021-10-22 23:10:47 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
wlr_log(WLR_DEBUG, "Attaching empty buffer to output for modeset");
|
2023-06-20 11:32:17 +02:00
|
|
|
struct wlr_buffer *buffer = output_acquire_empty_buffer(output, state);
|
|
|
|
if (buffer == NULL) {
|
2022-06-03 00:15:42 +02:00
|
|
|
return false;
|
2021-10-22 23:10:47 +02:00
|
|
|
}
|
2022-06-03 00:15:42 +02:00
|
|
|
|
2023-06-20 11:32:17 +02:00
|
|
|
*new_buffer = true;
|
|
|
|
wlr_output_state_set_buffer(state, buffer);
|
|
|
|
wlr_buffer_unlock(buffer);
|
2023-02-22 19:04:02 +01:00
|
|
|
return true;
|
2021-10-22 23:10:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void wlr_output_lock_attach_render(struct wlr_output *output, bool lock) {
|
|
|
|
if (lock) {
|
|
|
|
++output->attach_render_locks;
|
|
|
|
} else {
|
|
|
|
assert(output->attach_render_locks > 0);
|
|
|
|
--output->attach_render_locks;
|
|
|
|
}
|
|
|
|
wlr_log(WLR_DEBUG, "%s direct scan-out on output '%s' (locks: %d)",
|
|
|
|
lock ? "Disabling" : "Enabling", output->name,
|
|
|
|
output->attach_render_locks);
|
|
|
|
}
|
|
|
|
|
2023-05-05 01:24:44 +02:00
|
|
|
bool output_pick_format(struct wlr_output *output,
|
2021-11-11 04:54:04 +01:00
|
|
|
const struct wlr_drm_format_set *display_formats,
|
2023-05-05 01:24:44 +02:00
|
|
|
struct wlr_drm_format *format, uint32_t fmt) {
|
2021-11-05 12:26:38 +01:00
|
|
|
struct wlr_renderer *renderer = output->renderer;
|
|
|
|
struct wlr_allocator *allocator = output->allocator;
|
2021-10-22 23:10:47 +02:00
|
|
|
assert(renderer != NULL && allocator != NULL);
|
|
|
|
|
|
|
|
const struct wlr_drm_format_set *render_formats =
|
|
|
|
wlr_renderer_get_render_formats(renderer);
|
|
|
|
if (render_formats == NULL) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to get render formats");
|
2023-05-05 01:24:44 +02:00
|
|
|
return false;
|
2021-10-22 23:10:47 +02:00
|
|
|
}
|
|
|
|
|
2021-11-11 04:54:04 +01:00
|
|
|
const struct wlr_drm_format *render_format =
|
|
|
|
wlr_drm_format_set_get(render_formats, fmt);
|
|
|
|
if (render_format == NULL) {
|
|
|
|
wlr_log(WLR_DEBUG, "Renderer doesn't support format 0x%"PRIX32, fmt);
|
2023-05-05 01:24:44 +02:00
|
|
|
return false;
|
2021-11-11 04:54:04 +01:00
|
|
|
}
|
2021-10-22 23:10:47 +02:00
|
|
|
|
2021-11-11 04:54:04 +01:00
|
|
|
if (display_formats != NULL) {
|
|
|
|
const struct wlr_drm_format *display_format =
|
|
|
|
wlr_drm_format_set_get(display_formats, fmt);
|
|
|
|
if (display_format == NULL) {
|
|
|
|
wlr_log(WLR_DEBUG, "Output doesn't support format 0x%"PRIX32, fmt);
|
2023-05-05 01:24:44 +02:00
|
|
|
return false;
|
2021-10-22 23:10:47 +02:00
|
|
|
}
|
2023-05-05 01:24:44 +02:00
|
|
|
if (!wlr_drm_format_intersect(format, display_format, render_format)) {
|
|
|
|
wlr_log(WLR_DEBUG, "Failed to intersect display and render "
|
|
|
|
"modifiers for format 0x%"PRIX32 " on output %s",
|
|
|
|
fmt, output->name);
|
2023-05-10 22:00:22 +02:00
|
|
|
return false;
|
|
|
|
}
|
2023-05-05 01:24:44 +02:00
|
|
|
} else {
|
2021-11-11 04:54:04 +01:00
|
|
|
// The output can display any format
|
2023-05-05 01:24:44 +02:00
|
|
|
if (!wlr_drm_format_copy(format, render_format)) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-22 23:10:47 +02:00
|
|
|
}
|
|
|
|
|
2023-05-18 07:26:30 +02:00
|
|
|
if (format->len == 0) {
|
|
|
|
wlr_drm_format_finish(format);
|
|
|
|
wlr_log(WLR_DEBUG, "Failed to pick output format");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-05 01:24:44 +02:00
|
|
|
return true;
|
2021-10-22 23:10:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t wlr_output_preferred_read_format(struct wlr_output *output) {
|
2021-11-05 12:26:38 +01:00
|
|
|
struct wlr_renderer *renderer = output->renderer;
|
|
|
|
assert(renderer != NULL);
|
|
|
|
|
2021-10-22 23:10:47 +02:00
|
|
|
if (!renderer->impl->preferred_read_format || !renderer->impl->read_pixels) {
|
|
|
|
return DRM_FORMAT_INVALID;
|
|
|
|
}
|
|
|
|
|
2023-06-20 11:32:17 +02:00
|
|
|
if (!wlr_output_attach_render(output, NULL)) {
|
2021-10-22 23:10:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t fmt = renderer->impl->preferred_read_format(renderer);
|
|
|
|
|
|
|
|
output_clear_back_buffer(output);
|
|
|
|
|
|
|
|
return fmt;
|
|
|
|
}
|
2023-04-17 15:33:27 +02:00
|
|
|
|
|
|
|
struct wlr_render_pass *wlr_output_begin_render_pass(struct wlr_output *output,
|
2023-06-02 11:25:07 +02:00
|
|
|
struct wlr_output_state *state, int *buffer_age, struct wlr_render_timer *timer) {
|
2023-04-17 15:33:27 +02:00
|
|
|
if (!wlr_output_configure_primary_swapchain(output, state, &output->swapchain)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wlr_buffer *buffer = wlr_swapchain_acquire(output->swapchain, buffer_age);
|
|
|
|
if (buffer == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wlr_renderer *renderer = output->renderer;
|
|
|
|
assert(renderer != NULL);
|
2023-06-02 11:25:07 +02:00
|
|
|
struct wlr_render_pass *pass = wlr_renderer_begin_buffer_pass(renderer, buffer,
|
|
|
|
&(struct wlr_buffer_pass_options){
|
|
|
|
.timer = timer,
|
|
|
|
});
|
2023-04-17 15:33:27 +02:00
|
|
|
if (pass == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
wlr_output_state_set_buffer(state, buffer);
|
|
|
|
wlr_buffer_unlock(buffer);
|
|
|
|
return pass;
|
|
|
|
}
|