2018-03-15 14:33:58 +00:00
|
|
|
#include <assert.h>
|
2019-09-26 19:52:35 +00:00
|
|
|
#include <drm_fourcc.h>
|
2024-11-07 17:50:25 +00:00
|
|
|
#include <wlr/render/allocator.h>
|
2024-08-05 17:12:53 +00:00
|
|
|
#include <wlr/render/drm_syncobj.h>
|
2023-02-20 18:04:20 +00:00
|
|
|
#include <wlr/render/swapchain.h>
|
2018-03-19 22:16:29 +00:00
|
|
|
#include <wlr/render/wlr_renderer.h>
|
2018-02-12 20:29:23 +00:00
|
|
|
#include <wlr/util/log.h>
|
2017-09-30 07:52:58 +00:00
|
|
|
#include "backend/drm/drm.h"
|
2023-11-23 14:55:24 +00:00
|
|
|
#include "backend/drm/fb.h"
|
|
|
|
#include "backend/drm/renderer.h"
|
2024-01-31 20:37:07 +00:00
|
|
|
#include "backend/backend.h"
|
2020-11-25 15:24:07 +00:00
|
|
|
#include "render/drm_format_set.h"
|
2021-03-23 20:33:47 +00:00
|
|
|
#include "render/pixel_format.h"
|
2020-06-01 17:49:51 +00:00
|
|
|
#include "render/wlr_renderer.h"
|
2017-09-30 07:52:58 +00:00
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
bool init_drm_renderer(struct wlr_drm_backend *drm,
|
2020-12-19 10:34:28 +00:00
|
|
|
struct wlr_drm_renderer *renderer) {
|
2021-04-29 07:46:34 +00:00
|
|
|
renderer->wlr_rend = renderer_autocreate_with_drm_fd(drm->fd);
|
2017-10-01 09:44:24 +00:00
|
|
|
if (!renderer->wlr_rend) {
|
2021-04-29 07:38:10 +00:00
|
|
|
wlr_log(WLR_ERROR, "Failed to create renderer");
|
2021-07-12 15:30:45 +00:00
|
|
|
return false;
|
2017-10-01 09:44:24 +00:00
|
|
|
}
|
2017-10-01 06:22:47 +00:00
|
|
|
|
2024-11-07 17:50:25 +00:00
|
|
|
renderer->allocator = wlr_allocator_autocreate(&drm->backend, renderer->wlr_rend);
|
2020-06-01 17:49:51 +00:00
|
|
|
if (renderer->allocator == NULL) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to create allocator");
|
2021-07-12 15:30:45 +00:00
|
|
|
wlr_renderer_destroy(renderer->wlr_rend);
|
2024-11-07 22:10:39 +00:00
|
|
|
renderer->wlr_rend = NULL;
|
2021-07-12 15:30:45 +00:00
|
|
|
return false;
|
2020-06-01 17:49:51 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 07:52:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
void finish_drm_renderer(struct wlr_drm_renderer *renderer) {
|
2017-09-30 07:52:58 +00:00
|
|
|
if (!renderer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-15 18:32:13 +00:00
|
|
|
wlr_allocator_destroy(renderer->allocator);
|
2017-10-01 09:44:24 +00:00
|
|
|
wlr_renderer_destroy(renderer->wlr_rend);
|
2017-09-30 07:52:58 +00:00
|
|
|
}
|
|
|
|
|
2023-11-23 15:05:37 +00:00
|
|
|
void finish_drm_surface(struct wlr_drm_surface *surf) {
|
2022-05-25 13:55:41 +00:00
|
|
|
if (!surf || !surf->renderer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-05 17:12:53 +00:00
|
|
|
wlr_drm_syncobj_timeline_unref(surf->timeline);
|
2022-05-25 13:55:41 +00:00
|
|
|
wlr_swapchain_destroy(surf->swapchain);
|
|
|
|
|
2023-07-07 12:34:56 +00:00
|
|
|
*surf = (struct wlr_drm_surface){0};
|
2022-05-25 13:55:41 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 15:41:16 +00:00
|
|
|
bool init_drm_surface(struct wlr_drm_surface *surf,
|
2022-05-25 13:55:41 +00:00
|
|
|
struct wlr_drm_renderer *renderer, int width, int height,
|
2020-12-09 11:06:42 +00:00
|
|
|
const struct wlr_drm_format *drm_format) {
|
2022-05-26 07:38:48 +00:00
|
|
|
if (surf->swapchain != NULL && surf->swapchain->width == width &&
|
|
|
|
surf->swapchain->height == height) {
|
2017-09-30 07:52:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-05-25 13:55:41 +00:00
|
|
|
finish_drm_surface(surf);
|
2019-10-06 21:31:51 +00:00
|
|
|
|
2021-04-15 18:32:13 +00:00
|
|
|
surf->swapchain = wlr_swapchain_create(renderer->allocator, width, height,
|
|
|
|
drm_format);
|
2020-06-01 17:49:51 +00:00
|
|
|
if (surf->swapchain == NULL) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to create swapchain");
|
|
|
|
return false;
|
2017-09-30 07:52:58 +00:00
|
|
|
}
|
|
|
|
|
2024-08-05 17:12:53 +00:00
|
|
|
int drm_fd = wlr_renderer_get_drm_fd(renderer->wlr_rend);
|
|
|
|
if (renderer->wlr_rend->features.timeline && drm_fd >= 0) {
|
|
|
|
surf->timeline = wlr_drm_syncobj_timeline_create(drm_fd);
|
|
|
|
if (surf->timeline == NULL) {
|
|
|
|
finish_drm_surface(surf);
|
|
|
|
wlr_log(WLR_ERROR, "Failed to create DRM syncobj timeline");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 13:55:41 +00:00
|
|
|
surf->renderer = renderer;
|
2017-09-30 07:52:58 +00:00
|
|
|
|
2022-05-25 13:55:41 +00:00
|
|
|
return true;
|
2017-09-30 07:52:58 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 15:41:16 +00:00
|
|
|
struct wlr_buffer *drm_surface_blit(struct wlr_drm_surface *surf,
|
2024-08-05 17:12:53 +00:00
|
|
|
struct wlr_buffer *buffer,
|
|
|
|
struct wlr_drm_syncobj_timeline *wait_timeline, uint64_t wait_point) {
|
2020-12-15 11:21:40 +00:00
|
|
|
struct wlr_renderer *renderer = surf->renderer->wlr_rend;
|
|
|
|
|
2022-05-25 13:55:41 +00:00
|
|
|
if (surf->swapchain->width != buffer->width ||
|
|
|
|
surf->swapchain->height != buffer->height) {
|
2020-12-15 11:26:00 +00:00
|
|
|
wlr_log(WLR_ERROR, "Surface size doesn't match buffer size");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-04-12 17:45:51 +00:00
|
|
|
struct wlr_texture *tex = wlr_texture_from_buffer(renderer, buffer);
|
2020-12-15 11:21:40 +00:00
|
|
|
if (tex == NULL) {
|
2022-10-18 14:39:22 +00:00
|
|
|
wlr_log(WLR_ERROR, "Failed to import source buffer into multi-GPU renderer");
|
2020-12-15 11:21:40 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-08-20 22:33:59 +00:00
|
|
|
struct wlr_buffer *dst = wlr_swapchain_acquire(surf->swapchain);
|
2021-07-07 21:02:48 +00:00
|
|
|
if (!dst) {
|
2022-10-18 14:39:22 +00:00
|
|
|
wlr_log(WLR_ERROR, "Failed to acquire multi-GPU swapchain buffer");
|
2023-04-17 14:11:06 +00:00
|
|
|
goto error_tex;
|
2020-12-15 11:21:40 +00:00
|
|
|
}
|
|
|
|
|
2024-08-05 17:12:53 +00:00
|
|
|
surf->point++;
|
|
|
|
const struct wlr_buffer_pass_options pass_options = {
|
|
|
|
.signal_timeline = surf->timeline,
|
|
|
|
.signal_point = surf->point,
|
|
|
|
};
|
|
|
|
struct wlr_render_pass *pass = wlr_renderer_begin_buffer_pass(renderer, dst, &pass_options);
|
2023-04-17 14:11:06 +00:00
|
|
|
if (pass == NULL) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to begin render pass with multi-GPU destination buffer");
|
|
|
|
goto error_dst;
|
2021-07-07 21:02:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-17 14:11:06 +00:00
|
|
|
wlr_render_pass_add_texture(pass, &(struct wlr_render_texture_options){
|
|
|
|
.texture = tex,
|
2023-06-19 16:48:07 +00:00
|
|
|
.blend_mode = WLR_RENDER_BLEND_MODE_NONE,
|
2024-08-05 17:12:53 +00:00
|
|
|
.wait_timeline = wait_timeline,
|
|
|
|
.wait_point = wait_point,
|
2023-04-17 14:11:06 +00:00
|
|
|
});
|
|
|
|
if (!wlr_render_pass_submit(pass)) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to submit multi-GPU render pass");
|
|
|
|
goto error_dst;
|
|
|
|
}
|
2020-12-15 11:21:40 +00:00
|
|
|
|
|
|
|
wlr_texture_destroy(tex);
|
|
|
|
|
2021-07-07 21:02:48 +00:00
|
|
|
return dst;
|
2023-04-17 14:11:06 +00:00
|
|
|
|
|
|
|
error_dst:
|
|
|
|
wlr_buffer_unlock(dst);
|
|
|
|
error_tex:
|
|
|
|
wlr_texture_destroy(tex);
|
|
|
|
return NULL;
|
2020-12-15 11:21:40 +00:00
|
|
|
}
|
|
|
|
|
2023-05-04 23:24:44 +00:00
|
|
|
bool drm_plane_pick_render_format(struct wlr_drm_plane *plane,
|
|
|
|
struct wlr_drm_format *fmt, struct wlr_drm_renderer *renderer) {
|
2020-11-25 15:24:07 +00:00
|
|
|
const struct wlr_drm_format_set *render_formats =
|
2021-05-31 17:37:16 +00:00
|
|
|
wlr_renderer_get_render_formats(renderer->wlr_rend);
|
2020-11-25 15:24:07 +00:00
|
|
|
if (render_formats == NULL) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to get render formats");
|
2023-05-04 23:24:44 +00:00
|
|
|
return false;
|
2020-11-25 15:24:07 +00:00
|
|
|
}
|
2021-05-31 17:37:16 +00:00
|
|
|
|
|
|
|
const struct wlr_drm_format_set *plane_formats = &plane->formats;
|
|
|
|
|
2023-05-04 23:24:44 +00:00
|
|
|
uint32_t format = DRM_FORMAT_ARGB8888;
|
|
|
|
if (!wlr_drm_format_set_get(&plane->formats, format)) {
|
2021-05-31 17:37:16 +00:00
|
|
|
const struct wlr_pixel_format_info *format_info =
|
2023-05-04 23:24:44 +00:00
|
|
|
drm_get_pixel_format_info(format);
|
2021-05-31 17:37:16 +00:00
|
|
|
assert(format_info != NULL &&
|
|
|
|
format_info->opaque_substitute != DRM_FORMAT_INVALID);
|
2023-05-04 23:24:44 +00:00
|
|
|
format = format_info->opaque_substitute;
|
2021-05-31 17:37:16 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 15:24:07 +00:00
|
|
|
const struct wlr_drm_format *render_format =
|
2023-05-04 23:24:44 +00:00
|
|
|
wlr_drm_format_set_get(render_formats, format);
|
2020-11-25 15:24:07 +00:00
|
|
|
if (render_format == NULL) {
|
2023-05-04 23:24:44 +00:00
|
|
|
wlr_log(WLR_DEBUG, "Renderer doesn't support format 0x%"PRIX32, format);
|
|
|
|
return false;
|
2020-11-25 15:24:07 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 17:37:16 +00:00
|
|
|
const struct wlr_drm_format *plane_format =
|
2023-05-04 23:24:44 +00:00
|
|
|
wlr_drm_format_set_get(plane_formats, format);
|
2021-05-31 17:37:16 +00:00
|
|
|
if (plane_format == NULL) {
|
|
|
|
wlr_log(WLR_DEBUG, "Plane %"PRIu32" doesn't support format 0x%"PRIX32,
|
2023-05-04 23:24:44 +00:00
|
|
|
plane->id, format);
|
|
|
|
return false;
|
2020-11-25 14:30:23 +00:00
|
|
|
}
|
2019-10-22 16:41:47 +00:00
|
|
|
|
2023-05-04 23:24:44 +00:00
|
|
|
if (!wlr_drm_format_intersect(fmt, plane_format, render_format)) {
|
2021-05-31 17:37:16 +00:00
|
|
|
wlr_log(WLR_DEBUG, "Failed to intersect plane and render "
|
2023-05-04 23:24:44 +00:00
|
|
|
"modifiers for format 0x%"PRIX32, format);
|
|
|
|
return false;
|
2021-05-31 17:37:16 +00:00
|
|
|
}
|
|
|
|
|
2023-05-17 17:07:29 +00:00
|
|
|
if (fmt->len == 0) {
|
|
|
|
wlr_drm_format_finish(fmt);
|
|
|
|
wlr_log(WLR_DEBUG, "Failed to find matching plane and renderer modifiers");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-04 23:24:44 +00:00
|
|
|
return true;
|
2021-05-31 17:37:16 +00:00
|
|
|
}
|