2019-07-09 23:02:00 +03:00
|
|
|
/*
|
|
|
|
* Copyright © 2008-2011 Kristian Høgsberg
|
|
|
|
* Copyright © 2011 Intel Corporation
|
|
|
|
* Copyright © 2017, 2018 Collabora, Ltd.
|
|
|
|
* Copyright © 2017, 2018 General Electric Company
|
|
|
|
* Copyright (c) 2018 DisplayLink (UK) Ltd.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
* next paragraph) shall be included in all copies or substantial
|
|
|
|
* portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
#include "drm-internal.h"
|
|
|
|
#include "pixman-renderer.h"
|
|
|
|
#include "pixel-formats.h"
|
|
|
|
#include "renderer-gl/gl-renderer.h"
|
|
|
|
#include "shared/weston-egl-ext.h"
|
|
|
|
#include "linux-dmabuf.h"
|
|
|
|
#include "linux-explicit-synchronization.h"
|
|
|
|
|
|
|
|
static struct gbm_device *
|
|
|
|
create_gbm_device(int fd)
|
|
|
|
{
|
|
|
|
struct gbm_device *gbm;
|
|
|
|
|
|
|
|
/* GBM will load a dri driver, but even though they need symbols from
|
|
|
|
* libglapi, in some version of Mesa they are not linked to it. Since
|
|
|
|
* only the gl-renderer module links to it, the call above won't make
|
|
|
|
* these symbols globally available, and loading the DRI driver fails.
|
|
|
|
* Workaround this by dlopen()'ing libglapi with RTLD_GLOBAL. */
|
|
|
|
dlopen("libglapi.so.0", RTLD_LAZY | RTLD_GLOBAL);
|
|
|
|
|
|
|
|
gbm = gbm_create_device(fd);
|
|
|
|
|
|
|
|
return gbm;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* When initializing EGL, if the preferred buffer format isn't available
|
|
|
|
* we may be able to substitute an ARGB format for an XRGB one.
|
|
|
|
*
|
2023-01-26 19:33:05 +03:00
|
|
|
* This returns NULL if substitution isn't possible. The caller is responsible
|
|
|
|
* for checking for NULL before calling gl_renderer->create().
|
2019-07-09 23:02:00 +03:00
|
|
|
*
|
|
|
|
* This works around https://bugs.freedesktop.org/show_bug.cgi?id=89689
|
|
|
|
* but it's entirely possible we'll see this again on other implementations.
|
|
|
|
*/
|
2023-01-26 19:33:05 +03:00
|
|
|
static const struct pixel_format_info *
|
2023-01-27 19:08:00 +03:00
|
|
|
fallback_format_for(const struct pixel_format_info *format)
|
2019-07-09 23:02:00 +03:00
|
|
|
{
|
2023-01-27 19:08:00 +03:00
|
|
|
return pixel_format_get_info_by_opaque_substitute(format->format);
|
2019-07-09 23:02:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
drm_backend_create_gl_renderer(struct drm_backend *b)
|
|
|
|
{
|
2023-01-26 19:33:05 +03:00
|
|
|
const struct pixel_format_info *format[3] = {
|
2023-01-27 19:08:00 +03:00
|
|
|
b->format,
|
|
|
|
fallback_format_for(b->format),
|
2023-01-26 19:33:05 +03:00
|
|
|
NULL,
|
2019-07-09 23:02:00 +03:00
|
|
|
};
|
2020-03-06 16:04:18 +03:00
|
|
|
struct gl_renderer_display_options options = {
|
|
|
|
.egl_platform = EGL_PLATFORM_GBM_KHR,
|
|
|
|
.egl_native_display = b->gbm,
|
|
|
|
.egl_surface_type = EGL_WINDOW_BIT,
|
2023-01-26 19:33:05 +03:00
|
|
|
.formats = format,
|
|
|
|
.formats_count = 2,
|
2020-03-06 16:04:18 +03:00
|
|
|
};
|
2019-07-09 23:02:00 +03:00
|
|
|
|
|
|
|
if (format[1])
|
2023-01-26 19:33:05 +03:00
|
|
|
options.formats_count = 3;
|
2020-03-06 16:04:18 +03:00
|
|
|
|
2022-12-14 18:29:29 +03:00
|
|
|
return weston_compositor_init_renderer(b->compositor,
|
|
|
|
WESTON_RENDERER_GL,
|
|
|
|
&options.base);
|
2019-07-09 23:02:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
init_egl(struct drm_backend *b)
|
|
|
|
{
|
2021-11-29 16:21:40 +03:00
|
|
|
struct drm_device *device = b->drm;
|
2019-07-09 23:02:00 +03:00
|
|
|
|
2021-11-29 16:21:40 +03:00
|
|
|
b->gbm = create_gbm_device(device->drm.fd);
|
2019-07-09 23:02:00 +03:00
|
|
|
if (!b->gbm)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (drm_backend_create_gl_renderer(b) < 0) {
|
|
|
|
gbm_device_destroy(b->gbm);
|
2021-12-28 14:23:48 +03:00
|
|
|
b->gbm = NULL;
|
2019-07-09 23:02:00 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void drm_output_fini_cursor_egl(struct drm_output *output)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
|
2021-11-26 16:56:34 +03:00
|
|
|
/* This cursor does not have a GBM device */
|
|
|
|
if (output->gbm_cursor_fb[i] && !output->gbm_cursor_fb[i]->bo)
|
|
|
|
output->gbm_cursor_fb[i]->type = BUFFER_PIXMAN_DUMB;
|
2019-07-09 23:02:00 +03:00
|
|
|
drm_fb_unref(output->gbm_cursor_fb[i]);
|
|
|
|
output->gbm_cursor_fb[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
drm_output_init_cursor_egl(struct drm_output *output, struct drm_backend *b)
|
|
|
|
{
|
2021-11-03 18:36:17 +03:00
|
|
|
struct drm_device *device = output->device;
|
2019-07-09 23:02:00 +03:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
/* No point creating cursors if we don't have a plane for them. */
|
|
|
|
if (!output->cursor_plane)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_LENGTH(output->gbm_cursor_fb); i++) {
|
|
|
|
struct gbm_bo *bo;
|
|
|
|
|
2021-11-26 16:56:34 +03:00
|
|
|
if (gbm_device_get_fd(b->gbm) != output->device->drm.fd) {
|
|
|
|
output->gbm_cursor_fb[i] =
|
|
|
|
drm_fb_create_dumb(output->device,
|
|
|
|
device->cursor_width,
|
|
|
|
device->cursor_height,
|
|
|
|
DRM_FORMAT_ARGB8888);
|
|
|
|
/* Override buffer type, since we know it is a cursor */
|
|
|
|
output->gbm_cursor_fb[i]->type = BUFFER_CURSOR;
|
|
|
|
output->gbm_cursor_handle[i] =
|
|
|
|
output->gbm_cursor_fb[i]->handles[0];
|
|
|
|
} else {
|
|
|
|
bo = gbm_bo_create(b->gbm, device->cursor_width, device->cursor_height,
|
|
|
|
GBM_FORMAT_ARGB8888,
|
|
|
|
GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE);
|
|
|
|
if (!bo)
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
output->gbm_cursor_fb[i] =
|
|
|
|
drm_fb_get_from_bo(bo, device, false, BUFFER_CURSOR);
|
|
|
|
if (!output->gbm_cursor_fb[i]) {
|
|
|
|
gbm_bo_destroy(bo);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
output->gbm_cursor_handle[i] = gbm_bo_get_handle(bo).s32;
|
2019-07-09 23:02:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
weston_log("cursor buffers unavailable, using gl cursors\n");
|
2021-11-29 16:21:40 +03:00
|
|
|
device->cursors_are_broken = true;
|
2019-07-09 23:02:00 +03:00
|
|
|
drm_output_fini_cursor_egl(output);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-04-20 22:28:26 +03:00
|
|
|
static void
|
|
|
|
create_gbm_surface(struct gbm_device *gbm, struct drm_output *output)
|
2019-07-09 23:02:00 +03:00
|
|
|
{
|
2021-04-20 22:28:26 +03:00
|
|
|
struct weston_mode *mode = output->base.current_mode;
|
|
|
|
struct drm_plane *plane = output->scanout_plane;
|
2021-02-01 21:46:33 +03:00
|
|
|
struct weston_drm_format *fmt;
|
|
|
|
const uint64_t *modifiers;
|
|
|
|
unsigned int num_modifiers;
|
|
|
|
|
2021-04-20 22:28:26 +03:00
|
|
|
fmt = weston_drm_format_array_find_format(&plane->formats,
|
2023-01-27 19:08:00 +03:00
|
|
|
output->format->format);
|
2021-02-01 21:46:33 +03:00
|
|
|
if (!fmt) {
|
2023-01-27 19:08:00 +03:00
|
|
|
weston_log("format %s not supported by output %s\n",
|
|
|
|
output->format->drm_format_name,
|
|
|
|
output->base.name);
|
2021-04-20 22:28:26 +03:00
|
|
|
return;
|
2019-07-09 23:02:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_GBM_MODIFIERS
|
2021-04-21 17:44:53 +03:00
|
|
|
if (!weston_drm_format_has_modifier(fmt, DRM_FORMAT_MOD_INVALID)) {
|
|
|
|
modifiers = weston_drm_format_get_modifiers(fmt, &num_modifiers);
|
2019-07-09 23:02:00 +03:00
|
|
|
output->gbm_surface =
|
2021-04-20 22:28:26 +03:00
|
|
|
gbm_surface_create_with_modifiers(gbm,
|
|
|
|
mode->width, mode->height,
|
2023-01-27 19:08:00 +03:00
|
|
|
output->format->format,
|
2021-04-20 22:28:26 +03:00
|
|
|
modifiers, num_modifiers);
|
2019-07-09 23:02:00 +03:00
|
|
|
}
|
2021-04-20 22:28:26 +03:00
|
|
|
#endif
|
2019-07-09 23:02:00 +03:00
|
|
|
|
2021-11-11 17:18:51 +03:00
|
|
|
/*
|
|
|
|
* If we cannot use modifiers to allocate the GBM surface and the GBM
|
|
|
|
* device differs from the KMS display device (because we are rendering
|
|
|
|
* on a different GPU), we have to use linear buffers to make sure that
|
|
|
|
* the allocated GBM surface is correctly displayed on the KMS device.
|
|
|
|
*/
|
|
|
|
if (gbm_device_get_fd(gbm) != output->device->drm.fd)
|
|
|
|
output->gbm_bo_flags |= GBM_BO_USE_LINEAR;
|
|
|
|
|
2021-04-21 00:03:30 +03:00
|
|
|
/* We may allocate with no modifiers in the following situations:
|
|
|
|
*
|
|
|
|
* 1. old GBM version, so HAVE_GBM_MODIFIERS is false;
|
|
|
|
* 2. the KMS driver does not support modifiers;
|
|
|
|
* 3. if allocating with modifiers failed, what can happen when the KMS
|
|
|
|
* display device supports modifiers but the GBM driver does not,
|
|
|
|
* e.g. the old i915 Mesa driver.
|
|
|
|
*/
|
2019-07-09 23:02:00 +03:00
|
|
|
if (!output->gbm_surface)
|
2021-04-20 22:28:26 +03:00
|
|
|
output->gbm_surface = gbm_surface_create(gbm,
|
|
|
|
mode->width, mode->height,
|
2023-01-27 19:08:00 +03:00
|
|
|
output->format->format,
|
2021-04-20 22:28:26 +03:00
|
|
|
output->gbm_bo_flags);
|
|
|
|
}
|
2019-07-09 23:02:00 +03:00
|
|
|
|
2021-04-20 22:28:26 +03:00
|
|
|
/* Init output state that depends on gl or gbm */
|
|
|
|
int
|
|
|
|
drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
|
|
|
|
{
|
2022-12-14 18:29:29 +03:00
|
|
|
const struct weston_renderer *renderer = b->compositor->renderer;
|
2022-07-26 17:22:04 +03:00
|
|
|
const struct weston_mode *mode = output->base.current_mode;
|
2023-01-26 19:33:05 +03:00
|
|
|
const struct pixel_format_info *format[2] = {
|
2023-01-27 19:08:00 +03:00
|
|
|
output->format,
|
|
|
|
fallback_format_for(output->format),
|
2021-04-20 22:28:26 +03:00
|
|
|
};
|
|
|
|
struct gl_renderer_output_options options = {
|
2023-01-26 19:33:05 +03:00
|
|
|
.formats = format,
|
|
|
|
.formats_count = 1,
|
2022-07-26 17:22:04 +03:00
|
|
|
.area.x = 0,
|
|
|
|
.area.y = 0,
|
|
|
|
.area.width = mode->width,
|
|
|
|
.area.height = mode->height,
|
|
|
|
.fb_size.width = mode->width,
|
|
|
|
.fb_size.height = mode->height,
|
2021-04-20 22:28:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
assert(output->gbm_surface == NULL);
|
|
|
|
create_gbm_surface(b->gbm, output);
|
2019-07-09 23:02:00 +03:00
|
|
|
if (!output->gbm_surface) {
|
|
|
|
weston_log("failed to create gbm surface\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-01-26 19:33:05 +03:00
|
|
|
if (options.formats[1])
|
|
|
|
options.formats_count = 2;
|
2020-03-06 16:04:18 +03:00
|
|
|
options.window_for_legacy = (EGLNativeWindowType) output->gbm_surface;
|
|
|
|
options.window_for_platform = output->gbm_surface;
|
2022-12-14 18:29:29 +03:00
|
|
|
if (renderer->gl->output_window_create(&output->base, &options) < 0) {
|
2019-07-09 23:02:00 +03:00
|
|
|
weston_log("failed to create gl renderer output state\n");
|
|
|
|
gbm_surface_destroy(output->gbm_surface);
|
|
|
|
output->gbm_surface = NULL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
drm_output_init_cursor_egl(output, b);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
drm_output_fini_egl(struct drm_output *output)
|
|
|
|
{
|
2023-01-14 01:07:01 +03:00
|
|
|
struct drm_backend *b = output->backend;
|
2022-12-14 18:29:29 +03:00
|
|
|
const struct weston_renderer *renderer = b->compositor->renderer;
|
2019-07-09 23:02:00 +03:00
|
|
|
|
|
|
|
/* Destroying the GBM surface will destroy all our GBM buffers,
|
|
|
|
* regardless of refcount. Ensure we destroy them here. */
|
2023-05-02 01:38:22 +03:00
|
|
|
if (!b->compositor->shutting_down &&
|
2019-07-09 23:02:00 +03:00
|
|
|
output->scanout_plane->state_cur->fb &&
|
|
|
|
output->scanout_plane->state_cur->fb->type == BUFFER_GBM_SURFACE) {
|
2020-05-18 15:22:49 +03:00
|
|
|
drm_plane_reset_state(output->scanout_plane);
|
2019-07-09 23:02:00 +03:00
|
|
|
}
|
|
|
|
|
2022-12-14 18:29:29 +03:00
|
|
|
renderer->gl->output_destroy(&output->base);
|
2019-07-09 23:02:00 +03:00
|
|
|
gbm_surface_destroy(output->gbm_surface);
|
|
|
|
output->gbm_surface = NULL;
|
|
|
|
drm_output_fini_cursor_egl(output);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct drm_fb *
|
|
|
|
drm_output_render_gl(struct drm_output_state *state, pixman_region32_t *damage)
|
|
|
|
{
|
|
|
|
struct drm_output *output = state->output;
|
2021-11-03 18:36:17 +03:00
|
|
|
struct drm_device *device = output->device;
|
2019-07-09 23:02:00 +03:00
|
|
|
struct gbm_bo *bo;
|
|
|
|
struct drm_fb *ret;
|
|
|
|
|
|
|
|
output->base.compositor->renderer->repaint_output(&output->base,
|
2023-01-12 01:01:15 +03:00
|
|
|
damage, NULL);
|
2019-07-09 23:02:00 +03:00
|
|
|
|
|
|
|
bo = gbm_surface_lock_front_buffer(output->gbm_surface);
|
|
|
|
if (!bo) {
|
|
|
|
weston_log("failed to lock front buffer: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The renderer always produces an opaque image. */
|
2021-11-03 12:16:10 +03:00
|
|
|
ret = drm_fb_get_from_bo(bo, device, true, BUFFER_GBM_SURFACE);
|
2019-07-09 23:02:00 +03:00
|
|
|
if (!ret) {
|
|
|
|
weston_log("failed to get drm_fb for bo\n");
|
|
|
|
gbm_surface_release_buffer(output->gbm_surface, bo);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ret->gbm_surface = output->gbm_surface;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|