You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
3286 lines
83 KiB
3286 lines
83 KiB
15 years ago
|
/*
|
||
13 years ago
|
* Copyright © 2008-2011 Kristian Høgsberg
|
||
|
* Copyright © 2011 Intel Corporation
|
||
15 years ago
|
*
|
||
10 years ago
|
* 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:
|
||
15 years ago
|
*
|
||
10 years ago
|
* 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.
|
||
15 years ago
|
*/
|
||
|
|
||
12 years ago
|
#include "config.h"
|
||
14 years ago
|
|
||
13 years ago
|
#include <errno.h>
|
||
15 years ago
|
#include <stdlib.h>
|
||
12 years ago
|
#include <ctype.h>
|
||
15 years ago
|
#include <string.h>
|
||
|
#include <fcntl.h>
|
||
|
#include <unistd.h>
|
||
13 years ago
|
#include <linux/input.h>
|
||
11 years ago
|
#include <linux/vt.h>
|
||
13 years ago
|
#include <assert.h>
|
||
12 years ago
|
#include <sys/mman.h>
|
||
11 years ago
|
#include <dlfcn.h>
|
||
12 years ago
|
#include <time.h>
|
||
15 years ago
|
|
||
14 years ago
|
#include <xf86drm.h>
|
||
|
#include <xf86drmMode.h>
|
||
13 years ago
|
#include <drm_fourcc.h>
|
||
14 years ago
|
|
||
14 years ago
|
#include <gbm.h>
|
||
13 years ago
|
#include <libudev.h>
|
||
14 years ago
|
|
||
9 years ago
|
#include "compositor.h"
|
||
|
#include "compositor-drm.h"
|
||
10 years ago
|
#include "shared/helpers.h"
|
||
10 years ago
|
#include "shared/timespec-util.h"
|
||
12 years ago
|
#include "gl-renderer.h"
|
||
12 years ago
|
#include "pixman-renderer.h"
|
||
9 years ago
|
#include "libbacklight.h"
|
||
10 years ago
|
#include "libinput-seat.h"
|
||
13 years ago
|
#include "launcher-util.h"
|
||
11 years ago
|
#include "vaapi-recorder.h"
|
||
9 years ago
|
#include "presentation-time-server-protocol.h"
|
||
11 years ago
|
#include "linux-dmabuf.h"
|
||
15 years ago
|
|
||
12 years ago
|
#ifndef DRM_CAP_TIMESTAMP_MONOTONIC
|
||
|
#define DRM_CAP_TIMESTAMP_MONOTONIC 0x6
|
||
|
#endif
|
||
|
|
||
10 years ago
|
#ifndef DRM_CAP_CURSOR_WIDTH
|
||
|
#define DRM_CAP_CURSOR_WIDTH 0x8
|
||
|
#endif
|
||
|
|
||
|
#ifndef DRM_CAP_CURSOR_HEIGHT
|
||
|
#define DRM_CAP_CURSOR_HEIGHT 0x9
|
||
|
#endif
|
||
|
|
||
|
#ifndef GBM_BO_USE_CURSOR
|
||
|
#define GBM_BO_USE_CURSOR GBM_BO_USE_CURSOR_64X64
|
||
|
#endif
|
||
|
|
||
10 years ago
|
struct drm_backend {
|
||
|
struct weston_backend base;
|
||
|
struct weston_compositor *compositor;
|
||
15 years ago
|
|
||
|
struct udev *udev;
|
||
|
struct wl_event_source *drm_source;
|
||
|
|
||
14 years ago
|
struct udev_monitor *udev_monitor;
|
||
|
struct wl_event_source *udev_drm_source;
|
||
|
|
||
14 years ago
|
struct {
|
||
13 years ago
|
int id;
|
||
14 years ago
|
int fd;
|
||
11 years ago
|
char *filename;
|
||
14 years ago
|
} drm;
|
||
14 years ago
|
struct gbm_device *gbm;
|
||
13 years ago
|
uint32_t *crtcs;
|
||
|
int num_crtcs;
|
||
14 years ago
|
uint32_t crtc_allocator;
|
||
14 years ago
|
uint32_t connector_allocator;
|
||
11 years ago
|
struct wl_listener session_listener;
|
||
9 years ago
|
uint32_t gbm_format;
|
||
13 years ago
|
|
||
12 years ago
|
/* we need these parameters in order to not fail drmModeAddFB2()
|
||
|
* due to out of bounds dimensions, and then mistakenly set
|
||
|
* sprites_are_broken:
|
||
|
*/
|
||
12 years ago
|
uint32_t min_width, max_width;
|
||
|
uint32_t min_height, max_height;
|
||
|
int no_addfb2;
|
||
12 years ago
|
|
||
13 years ago
|
struct wl_list sprite_list;
|
||
13 years ago
|
int sprites_are_broken;
|
||
12 years ago
|
int sprites_hidden;
|
||
13 years ago
|
|
||
12 years ago
|
int cursors_are_broken;
|
||
|
|
||
12 years ago
|
int use_pixman;
|
||
|
|
||
13 years ago
|
uint32_t prev_state;
|
||
12 years ago
|
|
||
12 years ago
|
struct udev_input input;
|
||
10 years ago
|
|
||
10 years ago
|
int32_t cursor_width;
|
||
|
int32_t cursor_height;
|
||
9 years ago
|
|
||
|
/** Callback used to configure the outputs.
|
||
|
*
|
||
|
* This function will be called by the backend when a new DRM
|
||
|
* output needs to be configured.
|
||
|
*/
|
||
|
enum weston_drm_backend_output_mode
|
||
|
(*configure_output)(struct weston_compositor *compositor,
|
||
|
bool use_current_mode,
|
||
|
const char *name,
|
||
|
struct weston_drm_backend_output_config *output_config);
|
||
|
bool use_current_mode;
|
||
15 years ago
|
};
|
||
|
|
||
14 years ago
|
struct drm_mode {
|
||
13 years ago
|
struct weston_mode base;
|
||
14 years ago
|
drmModeModeInfo mode_info;
|
||
|
};
|
||
|
|
||
13 years ago
|
struct drm_fb {
|
||
12 years ago
|
uint32_t fb_id, stride, handle, size;
|
||
|
int fd;
|
||
13 years ago
|
int is_client_buffer;
|
||
12 years ago
|
struct weston_buffer_reference buffer_ref;
|
||
12 years ago
|
|
||
|
/* Used by gbm fbs */
|
||
|
struct gbm_bo *bo;
|
||
|
|
||
|
/* Used by dumb fbs */
|
||
|
void *map;
|
||
13 years ago
|
};
|
||
|
|
||
12 years ago
|
struct drm_edid {
|
||
|
char eisa_id[13];
|
||
|
char monitor_name[13];
|
||
|
char pnp_id[5];
|
||
|
char serial_number[13];
|
||
|
};
|
||
|
|
||
15 years ago
|
struct drm_output {
|
||
13 years ago
|
struct weston_output base;
|
||
15 years ago
|
|
||
|
uint32_t crtc_id;
|
||
12 years ago
|
int pipe;
|
||
15 years ago
|
uint32_t connector_id;
|
||
13 years ago
|
drmModeCrtcPtr original_crtc;
|
||
12 years ago
|
struct drm_edid edid;
|
||
12 years ago
|
drmModePropertyPtr dpms_prop;
|
||
9 years ago
|
uint32_t gbm_format;
|
||
13 years ago
|
|
||
10 years ago
|
enum dpms_enum dpms;
|
||
|
|
||
13 years ago
|
int vblank_pending;
|
||
|
int page_flip_pending;
|
||
11 years ago
|
int destroy_pending;
|
||
13 years ago
|
|
||
9 years ago
|
struct gbm_surface *gbm_surface;
|
||
|
struct gbm_bo *gbm_cursor_bo[2];
|
||
13 years ago
|
struct weston_plane cursor_plane;
|
||
|
struct weston_plane fb_plane;
|
||
11 years ago
|
struct weston_view *cursor_view;
|
||
13 years ago
|
int current_cursor;
|
||
13 years ago
|
struct drm_fb *current, *next;
|
||
13 years ago
|
struct backlight *backlight;
|
||
12 years ago
|
|
||
|
struct drm_fb *dumb[2];
|
||
|
pixman_image_t *image[2];
|
||
|
int current_image;
|
||
|
pixman_region32_t previous_damage;
|
||
11 years ago
|
|
||
|
struct vaapi_recorder *recorder;
|
||
|
struct wl_listener recorder_frame_listener;
|
||
15 years ago
|
};
|
||
|
|
||
13 years ago
|
/*
|
||
|
* An output has a primary display plane plus zero or more sprites for
|
||
|
* blending display contents.
|
||
|
*/
|
||
|
struct drm_sprite {
|
||
|
struct wl_list link;
|
||
|
|
||
13 years ago
|
struct weston_plane plane;
|
||
13 years ago
|
|
||
12 years ago
|
struct drm_fb *current, *next;
|
||
13 years ago
|
struct drm_output *output;
|
||
10 years ago
|
struct drm_backend *backend;
|
||
13 years ago
|
|
||
|
uint32_t possible_crtcs;
|
||
|
uint32_t plane_id;
|
||
|
uint32_t count_formats;
|
||
|
|
||
|
int32_t src_x, src_y;
|
||
|
uint32_t src_w, src_h;
|
||
|
uint32_t dest_x, dest_y;
|
||
|
uint32_t dest_w, dest_h;
|
||
|
|
||
|
uint32_t formats[];
|
||
|
};
|
||
|
|
||
11 years ago
|
static struct gl_renderer_interface *gl_renderer;
|
||
|
|
||
12 years ago
|
static const char default_seat[] = "seat0";
|
||
13 years ago
|
|
||
13 years ago
|
static void
|
||
|
drm_output_set_cursor(struct drm_output *output);
|
||
|
|
||
10 years ago
|
static void
|
||
|
drm_output_update_msc(struct drm_output *output, unsigned int seq);
|
||
|
|
||
13 years ago
|
static int
|
||
10 years ago
|
drm_sprite_crtc_supported(struct drm_output *output, uint32_t supported)
|
||
13 years ago
|
{
|
||
10 years ago
|
struct weston_compositor *ec = output->base.compositor;
|
||
10 years ago
|
struct drm_backend *b =(struct drm_backend *)ec->backend;
|
||
13 years ago
|
int crtc;
|
||
|
|
||
10 years ago
|
for (crtc = 0; crtc < b->num_crtcs; crtc++) {
|
||
|
if (b->crtcs[crtc] != output->crtc_id)
|
||
13 years ago
|
continue;
|
||
|
|
||
|
if (supported & (1 << crtc))
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
13 years ago
|
static void
|
||
|
drm_fb_destroy_callback(struct gbm_bo *bo, void *data)
|
||
|
{
|
||
|
struct drm_fb *fb = data;
|
||
|
struct gbm_device *gbm = gbm_bo_get_device(bo);
|
||
|
|
||
|
if (fb->fb_id)
|
||
|
drmModeRmFB(gbm_device_get_fd(gbm), fb->fb_id);
|
||
|
|
||
12 years ago
|
weston_buffer_reference(&fb->buffer_ref, NULL);
|
||
13 years ago
|
|
||
|
free(data);
|
||
|
}
|
||
|
|
||
12 years ago
|
static struct drm_fb *
|
||
9 years ago
|
drm_fb_create_dumb(struct drm_backend *b, unsigned width, unsigned height,
|
||
|
uint32_t format)
|
||
12 years ago
|
{
|
||
|
struct drm_fb *fb;
|
||
|
int ret;
|
||
9 years ago
|
uint32_t bpp, depth;
|
||
12 years ago
|
|
||
|
struct drm_mode_create_dumb create_arg;
|
||
|
struct drm_mode_destroy_dumb destroy_arg;
|
||
|
struct drm_mode_map_dumb map_arg;
|
||
|
|
||
11 years ago
|
fb = zalloc(sizeof *fb);
|
||
12 years ago
|
if (!fb)
|
||
|
return NULL;
|
||
|
|
||
9 years ago
|
switch (format) {
|
||
|
case GBM_FORMAT_XRGB8888:
|
||
|
bpp = 32;
|
||
|
depth = 24;
|
||
|
break;
|
||
|
case GBM_FORMAT_RGB565:
|
||
|
bpp = depth = 16;
|
||
|
break;
|
||
|
default:
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
11 years ago
|
memset(&create_arg, 0, sizeof create_arg);
|
||
9 years ago
|
create_arg.bpp = bpp;
|
||
12 years ago
|
create_arg.width = width;
|
||
|
create_arg.height = height;
|
||
|
|
||
10 years ago
|
ret = drmIoctl(b->drm.fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_arg);
|
||
12 years ago
|
if (ret)
|
||
|
goto err_fb;
|
||
|
|
||
|
fb->handle = create_arg.handle;
|
||
|
fb->stride = create_arg.pitch;
|
||
|
fb->size = create_arg.size;
|
||
10 years ago
|
fb->fd = b->drm.fd;
|
||
12 years ago
|
|
||
9 years ago
|
ret = -1;
|
||
|
|
||
|
if (!b->no_addfb2) {
|
||
|
uint32_t handles[4], pitches[4], offsets[4];
|
||
|
|
||
|
handles[0] = fb->handle;
|
||
|
pitches[0] = fb->stride;
|
||
|
offsets[0] = 0;
|
||
|
|
||
|
ret = drmModeAddFB2(b->drm.fd, width, height,
|
||
|
format, handles, pitches, offsets,
|
||
|
&fb->fb_id, 0);
|
||
|
if (ret) {
|
||
|
weston_log("addfb2 failed: %m\n");
|
||
|
b->no_addfb2 = 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (ret) {
|
||
|
ret = drmModeAddFB(b->drm.fd, width, height, depth, bpp,
|
||
|
fb->stride, fb->handle, &fb->fb_id);
|
||
|
}
|
||
|
|
||
12 years ago
|
if (ret)
|
||
|
goto err_bo;
|
||
|
|
||
11 years ago
|
memset(&map_arg, 0, sizeof map_arg);
|
||
12 years ago
|
map_arg.handle = fb->handle;
|
||
12 years ago
|
ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
|
||
12 years ago
|
if (ret)
|
||
|
goto err_add_fb;
|
||
|
|
||
9 years ago
|
fb->map = mmap(NULL, fb->size, PROT_WRITE,
|
||
10 years ago
|
MAP_SHARED, b->drm.fd, map_arg.offset);
|
||
12 years ago
|
if (fb->map == MAP_FAILED)
|
||
|
goto err_add_fb;
|
||
|
|
||
|
return fb;
|
||
|
|
||
|
err_add_fb:
|
||
10 years ago
|
drmModeRmFB(b->drm.fd, fb->fb_id);
|
||
12 years ago
|
err_bo:
|
||
|
memset(&destroy_arg, 0, sizeof(destroy_arg));
|
||
|
destroy_arg.handle = create_arg.handle;
|
||
10 years ago
|
drmIoctl(b->drm.fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
|
||
12 years ago
|
err_fb:
|
||
|
free(fb);
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
drm_fb_destroy_dumb(struct drm_fb *fb)
|
||
|
{
|
||
|
struct drm_mode_destroy_dumb destroy_arg;
|
||
|
|
||
|
if (!fb->map)
|
||
|
return;
|
||
|
|
||
|
if (fb->fb_id)
|
||
|
drmModeRmFB(fb->fd, fb->fb_id);
|
||
|
|
||
|
weston_buffer_reference(&fb->buffer_ref, NULL);
|
||
|
|
||
|
munmap(fb->map, fb->size);
|
||
|
|
||
|
memset(&destroy_arg, 0, sizeof(destroy_arg));
|
||
|
destroy_arg.handle = fb->handle;
|
||
|
drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
|
||
|
|
||
|
free(fb);
|
||
|
}
|
||
|
|
||
13 years ago
|
static struct drm_fb *
|
||
12 years ago
|
drm_fb_get_from_bo(struct gbm_bo *bo,
|
||
10 years ago
|
struct drm_backend *backend, uint32_t format)
|
||
13 years ago
|
{
|
||
|
struct drm_fb *fb = gbm_bo_get_user_data(bo);
|
||
12 years ago
|
uint32_t width, height;
|
||
12 years ago
|
uint32_t handles[4], pitches[4], offsets[4];
|
||
13 years ago
|
int ret;
|
||
|
|
||
|
if (fb)
|
||
|
return fb;
|
||
|
|
||
10 years ago
|
fb = zalloc(sizeof *fb);
|
||
|
if (fb == NULL)
|
||
12 years ago
|
return NULL;
|
||
13 years ago
|
|
||
|
fb->bo = bo;
|
||
|
|
||
|
width = gbm_bo_get_width(bo);
|
||
|
height = gbm_bo_get_height(bo);
|
||
12 years ago
|
fb->stride = gbm_bo_get_stride(bo);
|
||
|
fb->handle = gbm_bo_get_handle(bo).u32;
|
||
|
fb->size = fb->stride * height;
|
||
10 years ago
|
fb->fd = backend->drm.fd;
|
||
13 years ago
|
|
||
10 years ago
|
if (backend->min_width > width || width > backend->max_width ||
|
||
|
backend->min_height > height ||
|
||
|
height > backend->max_height) {
|
||
12 years ago
|
weston_log("bo geometry out of bounds\n");
|
||
|
goto err_free;
|
||
|
}
|
||
|
|
||
|
ret = -1;
|
||
|
|
||
10 years ago
|
if (format && !backend->no_addfb2) {
|
||
12 years ago
|
handles[0] = fb->handle;
|
||
|
pitches[0] = fb->stride;
|
||
12 years ago
|
offsets[0] = 0;
|
||
|
|
||
10 years ago
|
ret = drmModeAddFB2(backend->drm.fd, width, height,
|
||
12 years ago
|
format, handles, pitches, offsets,
|
||
|
&fb->fb_id, 0);
|
||
|
if (ret) {
|
||
|
weston_log("addfb2 failed: %m\n");
|
||
10 years ago
|
backend->no_addfb2 = 1;
|
||
|
backend->sprites_are_broken = 1;
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
|
if (ret)
|
||
10 years ago
|
ret = drmModeAddFB(backend->drm.fd, width, height, 24, 32,
|
||
12 years ago
|
fb->stride, fb->handle, &fb->fb_id);
|
||
12 years ago
|
|
||
13 years ago
|
if (ret) {
|
||
13 years ago
|
weston_log("failed to create kms fb: %m\n");
|
||
12 years ago
|
goto err_free;
|
||
13 years ago
|
}
|
||
|
|
||
|
gbm_bo_set_user_data(bo, fb, drm_fb_destroy_callback);
|
||
|
|
||
|
return fb;
|
||
12 years ago
|
|
||
|
err_free:
|
||
|
free(fb);
|
||
|
return NULL;
|
||
13 years ago
|
}
|
||
|
|
||
12 years ago
|
static void
|
||
12 years ago
|
drm_fb_set_buffer(struct drm_fb *fb, struct weston_buffer *buffer)
|
||
12 years ago
|
{
|
||
12 years ago
|
assert(fb->buffer_ref.buffer == NULL);
|
||
12 years ago
|
|
||
|
fb->is_client_buffer = 1;
|
||
|
|
||
12 years ago
|
weston_buffer_reference(&fb->buffer_ref, buffer);
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
static void
|
||
|
drm_output_release_fb(struct drm_output *output, struct drm_fb *fb)
|
||
|
{
|
||
|
if (!fb)
|
||
|
return;
|
||
|
|
||
12 years ago
|
if (fb->map &&
|
||
|
(fb != output->dumb[0] && fb != output->dumb[1])) {
|
||
12 years ago
|
drm_fb_destroy_dumb(fb);
|
||
|
} else if (fb->bo) {
|
||
12 years ago
|
if (fb->is_client_buffer)
|
||
|
gbm_bo_destroy(fb->bo);
|
||
|
else
|
||
9 years ago
|
gbm_surface_release_buffer(output->gbm_surface,
|
||
11 years ago
|
fb->bo);
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
static uint32_t
|
||
12 years ago
|
drm_output_check_scanout_format(struct drm_output *output,
|
||
|
struct weston_surface *es, struct gbm_bo *bo)
|
||
|
{
|
||
|
uint32_t format;
|
||
|
pixman_region32_t r;
|
||
|
|
||
|
format = gbm_bo_get_format(bo);
|
||
|
|
||
11 years ago
|
if (format == GBM_FORMAT_ARGB8888) {
|
||
|
/* We can scanout an ARGB buffer if the surface's
|
||
|
* opaque region covers the whole output, but we have
|
||
|
* to use XRGB as the KMS format code. */
|
||
11 years ago
|
pixman_region32_init_rect(&r, 0, 0,
|
||
|
output->base.width,
|
||
|
output->base.height);
|
||
|
pixman_region32_subtract(&r, &r, &es->opaque);
|
||
12 years ago
|
|
||
|
if (!pixman_region32_not_empty(&r))
|
||
12 years ago
|
format = GBM_FORMAT_XRGB8888;
|
||
12 years ago
|
|
||
|
pixman_region32_fini(&r);
|
||
11 years ago
|
}
|
||
12 years ago
|
|
||
9 years ago
|
if (output->gbm_format == format)
|
||
12 years ago
|
return format;
|
||
11 years ago
|
|
||
|
return 0;
|
||
12 years ago
|
}
|
||
|
|
||
13 years ago
|
static struct weston_plane *
|
||
10 years ago
|
drm_output_prepare_scanout_view(struct drm_output *output,
|
||
11 years ago
|
struct weston_view *ev)
|
||
13 years ago
|
{
|
||
10 years ago
|
struct drm_backend *b =
|
||
|
(struct drm_backend *)output->base.compositor->backend;
|
||
11 years ago
|
struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
|
||
11 years ago
|
struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
|
||
13 years ago
|
struct gbm_bo *bo;
|
||
12 years ago
|
uint32_t format;
|
||
13 years ago
|
|
||
11 years ago
|
if (ev->geometry.x != output->base.x ||
|
||
|
ev->geometry.y != output->base.y ||
|
||
10 years ago
|
buffer == NULL || b->gbm == NULL ||
|
||
11 years ago
|
buffer->width != output->base.current_mode->width ||
|
||
|
buffer->height != output->base.current_mode->height ||
|
||
11 years ago
|
output->base.transform != viewport->buffer.transform ||
|
||
11 years ago
|
ev->transform.enabled)
|
||
13 years ago
|
return NULL;
|
||
13 years ago
|
|
||
10 years ago
|
if (ev->geometry.scissor_enabled)
|
||
|
return NULL;
|
||
|
|
||
10 years ago
|
bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
|
||
11 years ago
|
buffer->resource, GBM_BO_USE_SCANOUT);
|
||
13 years ago
|
|
||
12 years ago
|
/* Unable to use the buffer for scanout */
|
||
|
if (!bo)
|
||
|
return NULL;
|
||
|
|
||
11 years ago
|
format = drm_output_check_scanout_format(output, ev->surface, bo);
|
||
12 years ago
|
if (format == 0) {
|
||
13 years ago
|
gbm_bo_destroy(bo);
|
||
13 years ago
|
return NULL;
|
||
13 years ago
|
}
|
||
|
|
||
10 years ago
|
output->next = drm_fb_get_from_bo(bo, b, format);
|
||
13 years ago
|
if (!output->next) {
|
||
|
gbm_bo_destroy(bo);
|
||
13 years ago
|
return NULL;
|
||
13 years ago
|
}
|
||
13 years ago
|
|
||
12 years ago
|
drm_fb_set_buffer(output->next, buffer);
|
||
13 years ago
|
|
||
13 years ago
|
return &output->fb_plane;
|
||
13 years ago
|
}
|
||
|
|
||
13 years ago
|
static void
|
||
12 years ago
|
drm_output_render_gl(struct drm_output *output, pixman_region32_t *damage)
|
||
13 years ago
|
{
|
||
10 years ago
|
struct drm_backend *b =
|
||
|
(struct drm_backend *)output->base.compositor->backend;
|
||
13 years ago
|
struct gbm_bo *bo;
|
||
13 years ago
|
|
||
10 years ago
|
output->base.compositor->renderer->repaint_output(&output->base,
|
||
|
damage);
|
||
13 years ago
|
|
||
9 years ago
|
bo = gbm_surface_lock_front_buffer(output->gbm_surface);
|
||
13 years ago
|
if (!bo) {
|
||
13 years ago
|
weston_log("failed to lock front buffer: %m\n");
|
||
13 years ago
|
return;
|
||
|
}
|
||
13 years ago
|
|
||
9 years ago
|
output->next = drm_fb_get_from_bo(bo, b, output->gbm_format);
|
||
13 years ago
|
if (!output->next) {
|
||
13 years ago
|
weston_log("failed to get drm_fb for bo\n");
|
||
9 years ago
|
gbm_surface_release_buffer(output->gbm_surface, bo);
|
||
13 years ago
|
return;
|
||
|
}
|
||
13 years ago
|
}
|
||
|
|
||
12 years ago
|
static void
|
||
|
drm_output_render_pixman(struct drm_output *output, pixman_region32_t *damage)
|
||
|
{
|
||
|
struct weston_compositor *ec = output->base.compositor;
|
||
|
pixman_region32_t total_damage, previous_damage;
|
||
|
|
||
|
pixman_region32_init(&total_damage);
|
||
|
pixman_region32_init(&previous_damage);
|
||
|
|
||
|
pixman_region32_copy(&previous_damage, damage);
|
||
|
|
||
|
pixman_region32_union(&total_damage, damage, &output->previous_damage);
|
||
|
pixman_region32_copy(&output->previous_damage, &previous_damage);
|
||
|
|
||
|
output->current_image ^= 1;
|
||
|
|
||
|
output->next = output->dumb[output->current_image];
|
||
|
pixman_renderer_output_set_buffer(&output->base,
|
||
|
output->image[output->current_image]);
|
||
|
|
||
|
ec->renderer->repaint_output(&output->base, &total_damage);
|
||
|
|
||
|
pixman_region32_fini(&total_damage);
|
||
|
pixman_region32_fini(&previous_damage);
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
drm_output_render(struct drm_output *output, pixman_region32_t *damage)
|
||
|
{
|
||
10 years ago
|
struct weston_compositor *c = output->base.compositor;
|
||
|
struct drm_backend *b = (struct drm_backend *)c->backend;
|
||
12 years ago
|
|
||
10 years ago
|
if (b->use_pixman)
|
||
12 years ago
|
drm_output_render_pixman(output, damage);
|
||
|
else
|
||
|
drm_output_render_gl(output, damage);
|
||
|
|
||
10 years ago
|
pixman_region32_subtract(&c->primary_plane.damage,
|
||
|
&c->primary_plane.damage, damage);
|
||
12 years ago
|
}
|
||
|
|
||
12 years ago
|
static void
|
||
|
drm_output_set_gamma(struct weston_output *output_base,
|
||
|
uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b)
|
||
|
{
|
||
|
int rc;
|
||
|
struct drm_output *output = (struct drm_output *) output_base;
|
||
10 years ago
|
struct drm_backend *backend =
|
||
|
(struct drm_backend *) output->base.compositor->backend;
|
||
12 years ago
|
|
||
|
/* check */
|
||
|
if (output_base->gamma_size != size)
|
||
|
return;
|
||
|
if (!output->original_crtc)
|
||
|
return;
|
||
|
|
||
10 years ago
|
rc = drmModeCrtcSetGamma(backend->drm.fd,
|
||
12 years ago
|
output->crtc_id,
|
||
|
size, r, g, b);
|
||
|
if (rc)
|
||
|
weston_log("set gamma failed: %m\n");
|
||
|
}
|
||
|
|
||
10 years ago
|
/* Determine the type of vblank synchronization to use for the output.
|
||
9 years ago
|
*
|
||
10 years ago
|
* The pipe parameter indicates which CRTC is in use. Knowing this, we
|
||
|
* can determine which vblank sequence type to use for it. Traditional
|
||
|
* cards had only two CRTCs, with CRTC 0 using no special flags, and
|
||
|
* CRTC 1 using DRM_VBLANK_SECONDARY. The first bit of the pipe
|
||
|
* parameter indicates this.
|
||
9 years ago
|
*
|
||
10 years ago
|
* Bits 1-5 of the pipe parameter are 5 bit wide pipe number between
|
||
|
* 0-31. If this is non-zero it indicates we're dealing with a
|
||
|
* multi-gpu situation and we need to calculate the vblank sync
|
||
|
* using DRM_BLANK_HIGH_CRTC_MASK.
|
||
|
*/
|
||
10 years ago
|
static unsigned int
|
||
|
drm_waitvblank_pipe(struct drm_output *output)
|
||
10 years ago
|
{
|
||
|
if (output->pipe > 1)
|
||
|
return (output->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT) &
|
||
|
DRM_VBLANK_HIGH_CRTC_MASK;
|
||
|
else if (output->pipe > 0)
|
||
|
return DRM_VBLANK_SECONDARY;
|
||
|
else
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
11 years ago
|
static int
|
||
13 years ago
|
drm_output_repaint(struct weston_output *output_base,
|
||
12 years ago
|
pixman_region32_t *damage)
|
||
14 years ago
|
{
|
||
|
struct drm_output *output = (struct drm_output *) output_base;
|
||
10 years ago
|
struct drm_backend *backend =
|
||
|
(struct drm_backend *)output->base.compositor->backend;
|
||
13 years ago
|
struct drm_sprite *s;
|
||
13 years ago
|
struct drm_mode *mode;
|
||
13 years ago
|
int ret = 0;
|
||
14 years ago
|
|
||
11 years ago
|
if (output->destroy_pending)
|
||
11 years ago
|
return -1;
|
||
11 years ago
|
|
||
13 years ago
|
if (!output->next)
|
||
12 years ago
|
drm_output_render(output, damage);
|
||
13 years ago
|
if (!output->next)
|
||
11 years ago
|
return -1;
|
||
15 years ago
|
|
||
11 years ago
|
mode = container_of(output->base.current_mode, struct drm_mode, base);
|
||
11 years ago
|
if (!output->current ||
|
||
|
output->current->stride != output->next->stride) {
|
||
10 years ago
|
ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
|
||
13 years ago
|
output->next->fb_id, 0, 0,
|
||
13 years ago
|
&output->connector_id, 1,
|
||
|
&mode->mode_info);
|
||
|
if (ret) {
|
||
13 years ago
|
weston_log("set mode failed: %m\n");
|
||
11 years ago
|
goto err_pageflip;
|
||
13 years ago
|
}
|
||
12 years ago
|
output_base->set_dpms(output_base, WESTON_DPMS_ON);
|
||
14 years ago
|
}
|
||
|
|
||
10 years ago
|
if (drmModePageFlip(backend->drm.fd, output->crtc_id,
|
||
13 years ago
|
output->next->fb_id,
|
||
13 years ago
|
DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
|
||
13 years ago
|
weston_log("queueing pageflip failed: %m\n");
|
||
11 years ago
|
goto err_pageflip;
|
||
13 years ago
|
}
|
||
14 years ago
|
|
||
13 years ago
|
output->page_flip_pending = 1;
|
||
|
|
||
13 years ago
|
drm_output_set_cursor(output);
|
||
|
|
||
13 years ago
|
/*
|
||
|
* Now, update all the sprite surfaces
|
||
|
*/
|
||
10 years ago
|
wl_list_for_each(s, &backend->sprite_list, link) {
|
||
12 years ago
|
uint32_t flags = 0, fb_id = 0;
|
||
13 years ago
|
drmVBlank vbl = {
|
||
|
.request.type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
|
||
|
.request.sequence = 1,
|
||
|
};
|
||
|
|
||
12 years ago
|
if ((!s->current && !s->next) ||
|
||
10 years ago
|
!drm_sprite_crtc_supported(output, s->possible_crtcs))
|
||
13 years ago
|
continue;
|
||
|
|
||
10 years ago
|
if (s->next && !backend->sprites_hidden)
|
||
12 years ago
|
fb_id = s->next->fb_id;
|
||
|
|
||
10 years ago
|
ret = drmModeSetPlane(backend->drm.fd, s->plane_id,
|
||
12 years ago
|
output->crtc_id, fb_id, flags,
|
||
13 years ago
|
s->dest_x, s->dest_y,
|
||
|
s->dest_w, s->dest_h,
|
||
|
s->src_x, s->src_y,
|
||
|
s->src_w, s->src_h);
|
||
|
if (ret)
|
||
13 years ago
|
weston_log("setplane failed: %d: %s\n",
|
||
13 years ago
|
ret, strerror(errno));
|
||
|
|
||
10 years ago
|
vbl.request.type |= drm_waitvblank_pipe(output);
|
||
12 years ago
|
|
||
13 years ago
|
/*
|
||
|
* Queue a vblank signal so we know when the surface
|
||
|
* becomes active on the display or has been replaced.
|
||
|
*/
|
||
|
vbl.request.signal = (unsigned long)s;
|
||
10 years ago
|
ret = drmWaitVBlank(backend->drm.fd, &vbl);
|
||
13 years ago
|
if (ret) {
|
||
13 years ago
|
weston_log("vblank event request failed: %d: %s\n",
|
||
13 years ago
|
ret, strerror(errno));
|
||
|
}
|
||
13 years ago
|
|
||
|
s->output = output;
|
||
|
output->vblank_pending = 1;
|
||
13 years ago
|
}
|
||
|
|
||
11 years ago
|
return 0;
|
||
|
|
||
|
err_pageflip:
|
||
11 years ago
|
output->cursor_view = NULL;
|
||
11 years ago
|
if (output->next) {
|
||
|
drm_output_release_fb(output, output->next);
|
||
|
output->next = NULL;
|
||
|
}
|
||
|
|
||
|
return -1;
|
||
15 years ago
|
}
|
||
|
|
||
12 years ago
|
static void
|
||
|
drm_output_start_repaint_loop(struct weston_output *output_base)
|
||
|
{
|
||
|
struct drm_output *output = (struct drm_output *) output_base;
|
||
10 years ago
|
struct drm_backend *backend = (struct drm_backend *)
|
||
|
output_base->compositor->backend;
|
||
12 years ago
|
uint32_t fb_id;
|
||
10 years ago
|
struct timespec ts, tnow;
|
||
|
struct timespec vbl2now;
|
||
|
int64_t refresh_nsec;
|
||
|
int ret;
|
||
|
drmVBlank vbl = {
|
||
|
.request.type = DRM_VBLANK_RELATIVE,
|
||
|
.request.sequence = 0,
|
||
|
.request.signal = 0,
|
||
|
};
|
||
12 years ago
|
|
||
11 years ago
|
if (output->destroy_pending)
|
||
|
return;
|
||
|
|
||
12 years ago
|
if (!output->current) {
|
||
|
/* We can't page flip if there's no mode set */
|
||
11 years ago
|
goto finish_frame;
|
||
12 years ago
|
}
|
||
|
|
||
10 years ago
|
/* Try to get current msc and timestamp via instant query */
|
||
|
vbl.request.type |= drm_waitvblank_pipe(output);
|
||
|
ret = drmWaitVBlank(backend->drm.fd, &vbl);
|
||
|
|
||
|
/* Error ret or zero timestamp means failure to get valid timestamp */
|
||
|
if ((ret == 0) && (vbl.reply.tval_sec > 0 || vbl.reply.tval_usec > 0)) {
|
||
|
ts.tv_sec = vbl.reply.tval_sec;
|
||
|
ts.tv_nsec = vbl.reply.tval_usec * 1000;
|
||
|
|
||
|
/* Valid timestamp for most recent vblank - not stale?
|
||
|
* Stale ts could happen on Linux 3.17+, so make sure it
|
||
|
* is not older than 1 refresh duration since now.
|
||
|
*/
|
||
|
weston_compositor_read_presentation_clock(backend->compositor,
|
||
|
&tnow);
|
||
|
timespec_sub(&vbl2now, &tnow, &ts);
|
||
|
refresh_nsec =
|
||
|
millihz_to_nsec(output->base.current_mode->refresh);
|
||
|
if (timespec_to_nsec(&vbl2now) < refresh_nsec) {
|
||
|
drm_output_update_msc(output, vbl.reply.sequence);
|
||
|
weston_output_finish_frame(output_base, &ts,
|
||
9 years ago
|
WP_PRESENTATION_FEEDBACK_INVALID);
|
||
10 years ago
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* Immediate query didn't provide valid timestamp.
|
||
|
* Use pageflip fallback.
|
||
|
*/
|
||
12 years ago
|
fb_id = output->current->fb_id;
|
||
12 years ago
|
|
||
10 years ago
|
if (drmModePageFlip(backend->drm.fd, output->crtc_id, fb_id,
|
||
12 years ago
|
DRM_MODE_PAGE_FLIP_EVENT, output) < 0) {
|
||
|
weston_log("queueing pageflip failed: %m\n");
|
||
11 years ago
|
goto finish_frame;
|
||
12 years ago
|
}
|
||
11 years ago
|
|
||
|
return;
|
||
|
|
||
|
finish_frame:
|
||
|
/* if we cannot page-flip, immediately finish frame */
|
||
10 years ago
|
weston_compositor_read_presentation_clock(output_base->compositor, &ts);
|
||
10 years ago
|
weston_output_finish_frame(output_base, &ts,
|
||
9 years ago
|
WP_PRESENTATION_FEEDBACK_INVALID);
|
||
12 years ago
|
}
|
||
|
|
||
10 years ago
|
static void
|
||
|
drm_output_update_msc(struct drm_output *output, unsigned int seq)
|
||
|
{
|
||
|
uint64_t msc_hi = output->base.msc >> 32;
|
||
|
|
||
|
if (seq < (output->base.msc & 0xffffffff))
|
||
|
msc_hi++;
|
||
|
|
||
|
output->base.msc = (msc_hi << 32) + seq;
|
||
|
}
|
||
|
|
||
13 years ago
|
static void
|
||
|
vblank_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
|
||
|
void *data)
|
||
|
{
|
||
|
struct drm_sprite *s = (struct drm_sprite *)data;
|
||
13 years ago
|
struct drm_output *output = s->output;
|
||
10 years ago
|
struct timespec ts;
|
||
9 years ago
|
uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
|
||
|
WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
|
||
13 years ago
|
|
||
10 years ago
|
drm_output_update_msc(output, frame);
|
||
13 years ago
|
output->vblank_pending = 0;
|
||
13 years ago
|
|
||
12 years ago
|
drm_output_release_fb(output, s->current);
|
||
12 years ago
|
s->current = s->next;
|
||
|
s->next = NULL;
|
||
13 years ago
|
|
||
|
if (!output->page_flip_pending) {
|
||
10 years ago
|
ts.tv_sec = sec;
|
||
|
ts.tv_nsec = usec * 1000;
|
||
10 years ago
|
weston_output_finish_frame(&output->base, &ts, flags);
|
||
13 years ago
|
}
|
||
13 years ago
|
}
|
||
|
|
||
11 years ago
|
static void
|
||
|
drm_output_destroy(struct weston_output *output_base);
|
||
|
|
||
15 years ago
|
static void
|
||
|
page_flip_handler(int fd, unsigned int frame,
|
||
|
unsigned int sec, unsigned int usec, void *data)
|
||
|
{
|
||
14 years ago
|
struct drm_output *output = (struct drm_output *) data;
|
||
10 years ago
|
struct timespec ts;
|
||
9 years ago
|
uint32_t flags = WP_PRESENTATION_FEEDBACK_KIND_VSYNC |
|
||
|
WP_PRESENTATION_FEEDBACK_KIND_HW_COMPLETION |
|
||
|
WP_PRESENTATION_FEEDBACK_KIND_HW_CLOCK;
|
||
15 years ago
|
|
||
10 years ago
|
drm_output_update_msc(output, frame);
|
||
|
|
||
12 years ago
|
/* We don't set page_flip_pending on start_repaint_loop, in that case
|
||
|
* we just want to page flip to the current buffer to get an accurate
|
||
|
* timestamp */
|
||
|
if (output->page_flip_pending) {
|
||
|
drm_output_release_fb(output, output->current);
|
||
|
output->current = output->next;
|
||
|
output->next = NULL;
|
||
|
}
|
||
13 years ago
|
|
||
12 years ago
|
output->page_flip_pending = 0;
|
||
13 years ago
|
|
||
11 years ago
|
if (output->destroy_pending)
|
||
|
drm_output_destroy(&output->base);
|
||
|
else if (!output->vblank_pending) {
|
||
10 years ago
|
ts.tv_sec = sec;
|
||
|
ts.tv_nsec = usec * 1000;
|
||
10 years ago
|
weston_output_finish_frame(&output->base, &ts, flags);
|
||
11 years ago
|
|
||
|
/* We can't call this from frame_notify, because the output's
|
||
|
* repaint needed flag is cleared just after that */
|
||
|
if (output->recorder)
|
||
|
weston_output_schedule_repaint(&output->base);
|
||
13 years ago
|
}
|
||
14 years ago
|
}
|
||
|
|
||
12 years ago
|
static uint32_t
|
||
|
drm_output_check_sprite_format(struct drm_sprite *s,
|
||
11 years ago
|
struct weston_view *ev, struct gbm_bo *bo)
|
||
13 years ago
|
{
|
||
12 years ago
|
uint32_t i, format;
|
||
|
|
||
|
format = gbm_bo_get_format(bo);
|
||
|
|
||
|
if (format == GBM_FORMAT_ARGB8888) {
|
||
|
pixman_region32_t r;
|
||
|
|
||
12 years ago
|
pixman_region32_init_rect(&r, 0, 0,
|
||
11 years ago
|
ev->surface->width,
|
||
|
ev->surface->height);
|
||
11 years ago
|
pixman_region32_subtract(&r, &r, &ev->surface->opaque);
|
||
12 years ago
|
|
||
|
if (!pixman_region32_not_empty(&r))
|
||
|
format = GBM_FORMAT_XRGB8888;
|
||
|
|
||
|
pixman_region32_fini(&r);
|
||
|
}
|
||
13 years ago
|
|
||
|
for (i = 0; i < s->count_formats; i++)
|
||
|
if (s->formats[i] == format)
|
||
12 years ago
|
return format;
|
||
13 years ago
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static int
|
||
11 years ago
|
drm_view_transform_supported(struct weston_view *ev)
|
||
13 years ago
|
{
|
||
11 years ago
|
return !ev->transform.enabled ||
|
||
|
(ev->transform.matrix.type < WESTON_MATRIX_TRANSFORM_ROTATE);
|
||
13 years ago
|
}
|
||
|
|
||
13 years ago
|
static struct weston_plane *
|
||
10 years ago
|
drm_output_prepare_overlay_view(struct drm_output *output,
|
||
11 years ago
|
struct weston_view *ev)
|
||
13 years ago
|
{
|
||
10 years ago
|
struct weston_compositor *ec = output->base.compositor;
|
||
10 years ago
|
struct drm_backend *b = (struct drm_backend *)ec->backend;
|
||
11 years ago
|
struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
|
||
11 years ago
|
struct wl_resource *buffer_resource;
|
||
13 years ago
|
struct drm_sprite *s;
|
||
11 years ago
|
struct linux_dmabuf_buffer *dmabuf;
|
||
13 years ago
|
int found = 0;
|
||
|
struct gbm_bo *bo;
|
||
|
pixman_region32_t dest_rect, src_rect;
|
||
12 years ago
|
pixman_box32_t *box, tbox;
|
||
13 years ago
|
uint32_t format;
|
||
13 years ago
|
wl_fixed_t sx1, sy1, sx2, sy2;
|
||
13 years ago
|
|
||
10 years ago
|
if (b->gbm == NULL)
|
||
12 years ago
|
return NULL;
|
||
|
|
||
10 years ago
|
if (viewport->buffer.transform != output->base.transform)
|
||
12 years ago
|
return NULL;
|
||
|
|
||
10 years ago
|
if (viewport->buffer.scale != output->base.current_scale)
|
||
12 years ago
|
return NULL;
|
||
|
|
||
10 years ago
|
if (b->sprites_are_broken)
|
||
13 years ago
|
return NULL;
|
||
13 years ago
|
|
||
10 years ago
|
if (ev->output_mask != (1u << output->base.id))
|
||
13 years ago
|
return NULL;
|
||
13 years ago
|
|
||
11 years ago
|
if (ev->surface->buffer_ref.buffer == NULL)
|
||
13 years ago
|
return NULL;
|
||
11 years ago
|
buffer_resource = ev->surface->buffer_ref.buffer->resource;
|
||
13 years ago
|
|
||
11 years ago
|
if (ev->alpha != 1.0f)
|
||
12 years ago
|
return NULL;
|
||
|
|
||
11 years ago
|
if (wl_shm_buffer_get(buffer_resource))
|
||
12 years ago
|
return NULL;
|
||
|
|
||
11 years ago
|
if (!drm_view_transform_supported(ev))
|
||
13 years ago
|
return NULL;
|
||
13 years ago
|
|
||
10 years ago
|
wl_list_for_each(s, &b->sprite_list, link) {
|
||
10 years ago
|
if (!drm_sprite_crtc_supported(output, s->possible_crtcs))
|
||
13 years ago
|
continue;
|
||
|
|
||
12 years ago
|
if (!s->next) {
|
||
13 years ago
|
found = 1;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* No sprites available */
|
||
|
if (!found)
|
||
13 years ago
|
return NULL;
|
||
13 years ago
|
|
||
11 years ago
|
if ((dmabuf = linux_dmabuf_buffer_get(buffer_resource))) {
|
||
9 years ago
|
#ifdef HAVE_GBM_FD_IMPORT
|
||
11 years ago
|
/* XXX: TODO:
|
||
|
*
|
||
|
* Use AddFB2 directly, do not go via GBM.
|
||
|
* Add support for multiplanar formats.
|
||
|
* Both require refactoring in the DRM-backend to
|
||
|
* support a mix of gbm_bos and drmfbs.
|
||
|
*/
|
||
|
struct gbm_import_fd_data gbm_dmabuf = {
|
||
9 years ago
|
.fd = dmabuf->attributes.fd[0],
|
||
|
.width = dmabuf->attributes.width,
|
||
|
.height = dmabuf->attributes.height,
|
||
|
.stride = dmabuf->attributes.stride[0],
|
||
|
.format = dmabuf->attributes.format
|
||
11 years ago
|
};
|
||
|
|
||
9 years ago
|
if (dmabuf->attributes.n_planes != 1 || dmabuf->attributes.offset[0] != 0)
|
||
11 years ago
|
return NULL;
|
||
|
|
||
|
bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD, &gbm_dmabuf,
|
||
|
GBM_BO_USE_SCANOUT);
|
||
9 years ago
|
#else
|
||
|
return NULL;
|
||
|
#endif
|
||
11 years ago
|
} else {
|
||
|
bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
|
||
|
buffer_resource, GBM_BO_USE_SCANOUT);
|
||
|
}
|
||
13 years ago
|
if (!bo)
|
||
13 years ago
|
return NULL;
|
||
13 years ago
|
|
||
11 years ago
|
format = drm_output_check_sprite_format(s, ev, bo);
|
||
12 years ago
|
if (format == 0) {
|
||
12 years ago
|
gbm_bo_destroy(bo);
|
||
13 years ago
|
return NULL;
|
||
12 years ago
|
}
|
||
13 years ago
|
|
||
10 years ago
|
s->next = drm_fb_get_from_bo(bo, b, format);
|
||
12 years ago
|
if (!s->next) {
|
||
|
gbm_bo_destroy(bo);
|
||
13 years ago
|
return NULL;
|
||
13 years ago
|
}
|
||
|
|
||
11 years ago
|
drm_fb_set_buffer(s->next, ev->surface->buffer_ref.buffer);
|
||
13 years ago
|
|
||
11 years ago
|
box = pixman_region32_extents(&ev->transform.boundingbox);
|
||
13 years ago
|
s->plane.x = box->x1;
|
||
|
s->plane.y = box->y1;
|
||
|
|
||
13 years ago
|
/*
|
||
|
* Calculate the source & dest rects properly based on actual
|
||
10 years ago
|
* position (note the caller has called weston_view_update_transform()
|
||
13 years ago
|
* for us already).
|
||
|
*/
|
||
|
pixman_region32_init(&dest_rect);
|
||
11 years ago
|
pixman_region32_intersect(&dest_rect, &ev->transform.boundingbox,
|
||
10 years ago
|
&output->base.region);
|
||
|
pixman_region32_translate(&dest_rect, -output->base.x, -output->base.y);
|
||
13 years ago
|
box = pixman_region32_extents(&dest_rect);
|
||
10 years ago
|
tbox = weston_transformed_rect(output->base.width,
|
||
|
output->base.height,
|
||
|
output->base.transform,
|
||
|
output->base.current_scale,
|
||
12 years ago
|
*box);
|
||
12 years ago
|
s->dest_x = tbox.x1;
|
||
|
s->dest_y = tbox.y1;
|
||
|
s->dest_w = tbox.x2 - tbox.x1;
|
||
|
s->dest_h = tbox.y2 - tbox.y1;
|
||
13 years ago
|
pixman_region32_fini(&dest_rect);
|
||
|
|
||
|
pixman_region32_init(&src_rect);
|
||
11 years ago
|
pixman_region32_intersect(&src_rect, &ev->transform.boundingbox,
|
||
10 years ago
|
&output->base.region);
|
||
13 years ago
|
box = pixman_region32_extents(&src_rect);
|
||
13 years ago
|
|
||
11 years ago
|
weston_view_from_global_fixed(ev,
|
||
|
wl_fixed_from_int(box->x1),
|
||
|
wl_fixed_from_int(box->y1),
|
||
|
&sx1, &sy1);
|
||
|
weston_view_from_global_fixed(ev,
|
||
|
wl_fixed_from_int(box->x2),
|
||
|
wl_fixed_from_int(box->y2),
|
||
|
&sx2, &sy2);
|
||
13 years ago
|
|
||
|
if (sx1 < 0)
|
||
|
sx1 = 0;
|
||
|
if (sy1 < 0)
|
||
|
sy1 = 0;
|
||
11 years ago
|
if (sx2 > wl_fixed_from_int(ev->surface->width))
|
||
|
sx2 = wl_fixed_from_int(ev->surface->width);
|
||
|
if (sy2 > wl_fixed_from_int(ev->surface->height))
|
||
|
sy2 = wl_fixed_from_int(ev->surface->height);
|
||
13 years ago
|
|
||
12 years ago
|
tbox.x1 = sx1;
|
||
|
tbox.y1 = sy1;
|
||
|
tbox.x2 = sx2;
|
||
|
tbox.y2 = sy2;
|
||
|
|
||
11 years ago
|
tbox = weston_transformed_rect(wl_fixed_from_int(ev->surface->width),
|
||
|
wl_fixed_from_int(ev->surface->height),
|
||
11 years ago
|
viewport->buffer.transform,
|
||
|
viewport->buffer.scale,
|
||
11 years ago
|
tbox);
|
||
12 years ago
|
|
||
|
s->src_x = tbox.x1 << 8;
|
||
|
s->src_y = tbox.y1 << 8;
|
||
|
s->src_w = (tbox.x2 - tbox.x1) << 8;
|
||
|
s->src_h = (tbox.y2 - tbox.y1) << 8;
|
||
13 years ago
|
pixman_region32_fini(&src_rect);
|
||
|
|
||
13 years ago
|
return &s->plane;
|
||
13 years ago
|
}
|
||
|
|
||
13 years ago
|
static struct weston_plane *
|
||
10 years ago
|
drm_output_prepare_cursor_view(struct drm_output *output,
|
||
11 years ago
|
struct weston_view *ev)
|
||
13 years ago
|
{
|
||
10 years ago
|
struct drm_backend *b =
|
||
|
(struct drm_backend *)output->base.compositor->backend;
|
||
11 years ago
|
struct weston_buffer_viewport *viewport = &ev->surface->buffer_viewport;
|
||
9 years ago
|
struct wl_shm_buffer *shmbuf;
|
||
13 years ago
|
|
||
9 years ago
|
if (ev->transform.enabled &&
|
||
|
(ev->transform.matrix.type > WESTON_MATRIX_TRANSFORM_TRANSLATE))
|
||
|
return NULL;
|
||
10 years ago
|
if (b->gbm == NULL)
|
||
12 years ago
|
return NULL;
|
||
12 years ago
|
if (output->base.transform != WL_OUTPUT_TRANSFORM_NORMAL)
|
||
|
return NULL;
|
||
10 years ago
|
if (viewport->buffer.scale != output->base.current_scale)
|
||
11 years ago
|
return NULL;
|
||
11 years ago
|
if (output->cursor_view)
|
||
13 years ago
|
return NULL;
|
||
10 years ago
|
if (ev->output_mask != (1u << output->base.id))
|
||
13 years ago
|
return NULL;
|
||
10 years ago
|
if (b->cursors_are_broken)
|
||
12 years ago
|
return NULL;
|
||
10 years ago
|
if (ev->geometry.scissor_enabled)
|
||
|
return NULL;
|
||
9 years ago
|
if (ev->surface->buffer_ref.buffer == NULL)
|
||
|
return NULL;
|
||
|
shmbuf = wl_shm_buffer_get(ev->surface->buffer_ref.buffer->resource);
|
||
|
if (!shmbuf)
|
||
|
return NULL;
|
||
|
if (wl_shm_buffer_get_format(shmbuf) != WL_SHM_FORMAT_ARGB8888)
|
||
|
return NULL;
|
||
|
if (ev->surface->width > b->cursor_width ||
|
||
10 years ago
|
ev->surface->height > b->cursor_height)
|
||
13 years ago
|
return NULL;
|
||
|
|
||
11 years ago
|
output->cursor_view = ev;
|
||
13 years ago
|
|
||
|
return &output->cursor_plane;
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Update the image for the current cursor surface
|
||
|
*
|
||
|
* @param b DRM backend structure
|
||
|
* @param bo GBM buffer object to write into
|
||
|
* @param ev View to use for cursor image
|
||
|
*/
|
||
|
static void
|
||
|
cursor_bo_update(struct drm_backend *b, struct gbm_bo *bo,
|
||
|
struct weston_view *ev)
|
||
|
{
|
||
|
struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
|
||
|
uint32_t buf[b->cursor_width * b->cursor_height];
|
||
|
int32_t stride;
|
||
|
uint8_t *s;
|
||
|
int i;
|
||
|
|
||
|
assert(buffer && buffer->shm_buffer);
|
||
|
assert(buffer->shm_buffer == wl_shm_buffer_get(buffer->resource));
|
||
|
assert(ev->surface->width <= b->cursor_width);
|
||
|
assert(ev->surface->height <= b->cursor_height);
|
||
|
|
||
|
memset(buf, 0, sizeof buf);
|
||
|
stride = wl_shm_buffer_get_stride(buffer->shm_buffer);
|
||
|
s = wl_shm_buffer_get_data(buffer->shm_buffer);
|
||
|
|
||
|
wl_shm_buffer_begin_access(buffer->shm_buffer);
|
||
|
for (i = 0; i < ev->surface->height; i++)
|
||
|
memcpy(buf + i * b->cursor_width,
|
||
|
s + i * stride,
|
||
|
ev->surface->width * 4);
|
||
|
wl_shm_buffer_end_access(buffer->shm_buffer);
|
||
|
|
||
|
if (gbm_bo_write(bo, buf, sizeof buf) < 0)
|
||
|
weston_log("failed update cursor: %m\n");
|
||
|
}
|
||
|
|
||
13 years ago
|
static void
|
||
|
drm_output_set_cursor(struct drm_output *output)
|
||
|
{
|
||
11 years ago
|
struct weston_view *ev = output->cursor_view;
|
||
11 years ago
|
struct weston_buffer *buffer;
|
||
10 years ago
|
struct drm_backend *b =
|
||
|
(struct drm_backend *) output->base.compositor->backend;
|
||
11 years ago
|
EGLint handle;
|
||
13 years ago
|
struct gbm_bo *bo;
|
||
9 years ago
|
float x, y;
|
||
13 years ago
|
|
||
11 years ago
|
output->cursor_view = NULL;
|
||
|
if (ev == NULL) {
|
||
10 years ago
|
drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
|
||
9 years ago
|
output->cursor_plane.x = INT32_MIN;
|
||
|
output->cursor_plane.y = INT32_MIN;
|
||
13 years ago
|
return;
|
||
|
}
|
||
13 years ago
|
|
||
11 years ago
|
buffer = ev->surface->buffer_ref.buffer;
|
||
|
|
||
|
if (buffer &&
|
||
12 years ago
|
pixman_region32_not_empty(&output->cursor_plane.damage)) {
|
||
13 years ago
|
pixman_region32_fini(&output->cursor_plane.damage);
|
||
|
pixman_region32_init(&output->cursor_plane.damage);
|
||
13 years ago
|
output->current_cursor ^= 1;
|
||
9 years ago
|
bo = output->gbm_cursor_bo[output->current_cursor];
|
||
13 years ago
|
|
||
11 years ago
|
cursor_bo_update(b, bo, ev);
|
||
13 years ago
|
handle = gbm_bo_get_handle(bo).s32;
|
||
10 years ago
|
if (drmModeSetCursor(b->drm.fd, output->crtc_id, handle,
|
||
|
b->cursor_width, b->cursor_height)) {
|
||
13 years ago
|
weston_log("failed to set cursor: %m\n");
|
||
10 years ago
|
b->cursors_are_broken = 1;
|
||
12 years ago
|
}
|
||
13 years ago
|
}
|
||
13 years ago
|
|
||
9 years ago
|
weston_view_to_global_float(ev, 0, 0, &x, &y);
|
||
9 years ago
|
|
||
|
/* From global to output space, output transform is guaranteed to be
|
||
|
* NORMAL by drm_output_prepare_cursor_view().
|
||
|
*/
|
||
|
x = (x - output->base.x) * output->base.current_scale;
|
||
|
y = (y - output->base.y) * output->base.current_scale;
|
||
|
|
||
13 years ago
|
if (output->cursor_plane.x != x || output->cursor_plane.y != y) {
|
||
10 years ago
|
if (drmModeMoveCursor(b->drm.fd, output->crtc_id, x, y)) {
|
||
13 years ago
|
weston_log("failed to move cursor: %m\n");
|
||
10 years ago
|
b->cursors_are_broken = 1;
|
||
12 years ago
|
}
|
||
|
|
||
13 years ago
|
output->cursor_plane.x = x;
|
||
|
output->cursor_plane.y = y;
|
||
13 years ago
|
}
|
||
13 years ago
|
}
|
||
|
|
||
13 years ago
|
static void
|
||
10 years ago
|
drm_assign_planes(struct weston_output *output_base)
|
||
13 years ago
|
{
|
||
10 years ago
|
struct drm_backend *b =
|
||
|
(struct drm_backend *)output_base->compositor->backend;
|
||
10 years ago
|
struct drm_output *output = (struct drm_output *)output_base;
|
||
11 years ago
|
struct weston_view *ev, *next;
|
||
13 years ago
|
pixman_region32_t overlap, surface_overlap;
|
||
13 years ago
|
struct weston_plane *primary, *next_plane;
|
||
13 years ago
|
|
||
|
/*
|
||
|
* Find a surface for each sprite in the output using some heuristics:
|
||
|
* 1) size
|
||
|
* 2) frequency of update
|
||
|
* 3) opacity (though some hw might support alpha blending)
|
||
|
* 4) clipping (this can be fixed with color keys)
|
||
|
*
|
||
|
* The idea is to save on blitting since this should save power.
|
||
|
* If we can get a large video surface on the sprite for example,
|
||
|
* the main display surface may not need to update at all, and
|
||
|
* the client buffer can be used directly for the sprite surface
|
||
|
* as we do for flipping full screen surfaces.
|
||
|
*/
|
||
|
pixman_region32_init(&overlap);
|
||
10 years ago
|
primary = &output_base->compositor->primary_plane;
|
||
11 years ago
|
|
||
10 years ago
|
wl_list_for_each_safe(ev, next, &output_base->compositor->view_list, link) {
|
||
11 years ago
|
struct weston_surface *es = ev->surface;
|
||
|
|
||
|
/* Test whether this buffer can ever go into a plane:
|
||
|
* non-shm, or small enough to be a cursor.
|
||
|
*
|
||
|
* Also, keep a reference when using the pixman renderer.
|
||
|
* That makes it possible to do a seamless switch to the GL
|
||
|
* renderer and since the pixman renderer keeps a reference
|
||
|
* to the buffer anyway, there is no side effects.
|
||
12 years ago
|
*/
|
||
10 years ago
|
if (b->use_pixman ||
|
||
11 years ago
|
(es->buffer_ref.buffer &&
|
||
|
(!wl_shm_buffer_get(es->buffer_ref.buffer->resource) ||
|
||
9 years ago
|
(ev->surface->width <= b->cursor_width &&
|
||
|
ev->surface->height <= b->cursor_height))))
|
||
10 years ago
|
es->keep_buffer = true;
|
||
11 years ago
|
else
|
||
10 years ago
|
es->keep_buffer = false;
|
||
12 years ago
|
|
||
13 years ago
|
pixman_region32_init(&surface_overlap);
|
||
|
pixman_region32_intersect(&surface_overlap, &overlap,
|
||
11 years ago
|
&ev->transform.boundingbox);
|
||
13 years ago
|
|
||
13 years ago
|
next_plane = NULL;
|
||
13 years ago
|
if (pixman_region32_not_empty(&surface_overlap))
|
||
13 years ago
|
next_plane = primary;
|
||
|
if (next_plane == NULL)
|
||
11 years ago
|
next_plane = drm_output_prepare_cursor_view(output, ev);
|
||
13 years ago
|
if (next_plane == NULL)
|
||
11 years ago
|
next_plane = drm_output_prepare_scanout_view(output, ev);
|
||
13 years ago
|
if (next_plane == NULL)
|
||
11 years ago
|
next_plane = drm_output_prepare_overlay_view(output, ev);
|
||
13 years ago
|
if (next_plane == NULL)
|
||
|
next_plane = primary;
|
||
10 years ago
|
|
||
11 years ago
|
weston_view_move_to_plane(ev, next_plane);
|
||
10 years ago
|
|
||
13 years ago
|
if (next_plane == primary)
|
||
13 years ago
|
pixman_region32_union(&overlap, &overlap,
|
||
11 years ago
|
&ev->transform.boundingbox);
|
||
13 years ago
|
|
||
10 years ago
|
if (next_plane == primary ||
|
||
|
next_plane == &output->cursor_plane) {
|
||
|
/* cursor plane involves a copy */
|
||
|
ev->psf_flags = 0;
|
||
|
} else {
|
||
|
/* All other planes are a direct scanout of a
|
||
|
* single client buffer.
|
||
|
*/
|
||
9 years ago
|
ev->psf_flags = WP_PRESENTATION_FEEDBACK_KIND_ZERO_COPY;
|
||
10 years ago
|
}
|
||
|
|
||
13 years ago
|
pixman_region32_fini(&surface_overlap);
|
||
|
}
|
||
|
pixman_region32_fini(&overlap);
|
||
|
}
|
||
|
|
||
12 years ago
|
static void
|
||
|
drm_output_fini_pixman(struct drm_output *output);
|
||
|
|
||
13 years ago
|
static void
|
||
13 years ago
|
drm_output_destroy(struct weston_output *output_base)
|
||
13 years ago
|
{
|
||
|
struct drm_output *output = (struct drm_output *) output_base;
|
||
10 years ago
|
struct drm_backend *b =
|
||
|
(struct drm_backend *)output->base.compositor->backend;
|
||
13 years ago
|
drmModeCrtcPtr origcrtc = output->original_crtc;
|
||
|
|
||
11 years ago
|
if (output->page_flip_pending) {
|
||
|
output->destroy_pending = 1;
|
||
|
weston_log("destroy output while page flip pending\n");
|
||
|
return;
|
||
|
}
|
||
|
|
||
13 years ago
|
if (output->backlight)
|
||
|
backlight_destroy(output->backlight);
|
||
|
|
||
12 years ago
|
drmModeFreeProperty(output->dpms_prop);
|
||
|
|
||
13 years ago
|
/* Turn off hardware cursor */
|
||
10 years ago
|
drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
|
||
13 years ago
|
|
||
|
/* Restore original CRTC state */
|
||
10 years ago
|
drmModeSetCrtc(b->drm.fd, origcrtc->crtc_id, origcrtc->buffer_id,
|
||
13 years ago
|
origcrtc->x, origcrtc->y,
|
||
|
&output->connector_id, 1, &origcrtc->mode);
|
||
13 years ago
|
drmModeFreeCrtc(origcrtc);
|
||
|
|
||
10 years ago
|
b->crtc_allocator &= ~(1 << output->crtc_id);
|
||
|
b->connector_allocator &= ~(1 << output->connector_id);
|
||
13 years ago
|
|
||
10 years ago
|
if (b->use_pixman) {
|
||
12 years ago
|
drm_output_fini_pixman(output);
|
||
|
} else {
|
||
11 years ago
|
gl_renderer->output_destroy(output_base);
|
||
9 years ago
|
gbm_surface_destroy(output->gbm_surface);
|
||
12 years ago
|
}
|
||
13 years ago
|
|
||
13 years ago
|
weston_plane_release(&output->fb_plane);
|
||
|
weston_plane_release(&output->cursor_plane);
|
||
|
|
||
13 years ago
|
weston_output_destroy(&output->base);
|
||
13 years ago
|
|
||
13 years ago
|
free(output);
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Find the closest-matching mode for a given target
|
||
|
*
|
||
|
* Given a target mode, find the most suitable mode amongst the output's
|
||
|
* current mode list to use, preferring the current mode if possible, to
|
||
|
* avoid an expensive mode switch.
|
||
|
*
|
||
|
* @param output DRM output
|
||
|
* @param target_mode Mode to attempt to match
|
||
|
* @returns Pointer to a mode from the output's mode list
|
||
|
*/
|
||
13 years ago
|
static struct drm_mode *
|
||
|
choose_mode (struct drm_output *output, struct weston_mode *target_mode)
|
||
|
{
|
||
|
struct drm_mode *tmp_mode = NULL, *mode;
|
||
|
|
||
11 years ago
|
if (output->base.current_mode->width == target_mode->width &&
|
||
|
output->base.current_mode->height == target_mode->height &&
|
||
|
(output->base.current_mode->refresh == target_mode->refresh ||
|
||
13 years ago
|
target_mode->refresh == 0))
|
||
11 years ago
|
return (struct drm_mode *)output->base.current_mode;
|
||
13 years ago
|
|
||
|
wl_list_for_each(mode, &output->base.mode_list, base.link) {
|
||
|
if (mode->mode_info.hdisplay == target_mode->width &&
|
||
|
mode->mode_info.vdisplay == target_mode->height) {
|
||
10 years ago
|
if (mode->base.refresh == target_mode->refresh ||
|
||
|
target_mode->refresh == 0) {
|
||
13 years ago
|
return mode;
|
||
10 years ago
|
} else if (!tmp_mode)
|
||
13 years ago
|
tmp_mode = mode;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return tmp_mode;
|
||
|
}
|
||
|
|
||
12 years ago
|
static int
|
||
10 years ago
|
drm_output_init_egl(struct drm_output *output, struct drm_backend *b);
|
||
12 years ago
|
static int
|
||
10 years ago
|
drm_output_init_pixman(struct drm_output *output, struct drm_backend *b);
|
||
12 years ago
|
|
||
13 years ago
|
static int
|
||
|
drm_output_switch_mode(struct weston_output *output_base, struct weston_mode *mode)
|
||
|
{
|
||
|
struct drm_output *output;
|
||
|
struct drm_mode *drm_mode;
|
||
10 years ago
|
struct drm_backend *b;
|
||
13 years ago
|
|
||
|
if (output_base == NULL) {
|
||
13 years ago
|
weston_log("output is NULL.\n");
|
||
13 years ago
|
return -1;
|
||
|
}
|
||
|
|
||
|
if (mode == NULL) {
|
||
13 years ago
|
weston_log("mode is NULL.\n");
|
||
13 years ago
|
return -1;
|
||
|
}
|
||
|
|
||
10 years ago
|
b = (struct drm_backend *)output_base->compositor->backend;
|
||
13 years ago
|
output = (struct drm_output *)output_base;
|
||
|
drm_mode = choose_mode (output, mode);
|
||
|
|
||
|
if (!drm_mode) {
|
||
13 years ago
|
weston_log("%s, invalid resolution:%dx%d\n", __func__, mode->width, mode->height);
|
||
13 years ago
|
return -1;
|
||
|
}
|
||
|
|
||
11 years ago
|
if (&drm_mode->base == output->base.current_mode)
|
||
12 years ago
|
return 0;
|
||
13 years ago
|
|
||
11 years ago
|
output->base.current_mode->flags = 0;
|
||
13 years ago
|
|
||
11 years ago
|
output->base.current_mode = &drm_mode->base;
|
||
|
output->base.current_mode->flags =
|
||
12 years ago
|
WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
|
||
13 years ago
|
|
||
|
/* reset rendering stuff. */
|
||
12 years ago
|
drm_output_release_fb(output, output->current);
|
||
|
drm_output_release_fb(output, output->next);
|
||
|
output->current = output->next = NULL;
|
||
13 years ago
|
|
||
10 years ago
|
if (b->use_pixman) {
|
||
12 years ago
|
drm_output_fini_pixman(output);
|
||
10 years ago
|
if (drm_output_init_pixman(output, b) < 0) {
|
||
12 years ago
|
weston_log("failed to init output pixman state with "
|
||
|
"new mode\n");
|
||
|
return -1;
|
||
|
}
|
||
|
} else {
|
||
11 years ago
|
gl_renderer->output_destroy(&output->base);
|
||
9 years ago
|
gbm_surface_destroy(output->gbm_surface);
|
||
13 years ago
|
|
||
10 years ago
|
if (drm_output_init_egl(output, b) < 0) {
|
||
12 years ago
|
weston_log("failed to init output egl state with "
|
||
|
"new mode");
|
||
|
return -1;
|
||
|
}
|
||
12 years ago
|
}
|
||
13 years ago
|
|
||
12 years ago
|
return 0;
|
||
13 years ago
|
}
|
||
|
|
||
14 years ago
|
static int
|
||
15 years ago
|
on_drm_input(int fd, uint32_t mask, void *data)
|
||
|
{
|
||
|
drmEventContext evctx;
|
||
|
|
||
|
memset(&evctx, 0, sizeof evctx);
|
||
|
evctx.version = DRM_EVENT_CONTEXT_VERSION;
|
||
|
evctx.page_flip_handler = page_flip_handler;
|
||
13 years ago
|
evctx.vblank_handler = vblank_handler;
|
||
15 years ago
|
drmHandleEvent(fd, &evctx);
|
||
14 years ago
|
|
||
|
return 1;
|
||
15 years ago
|
}
|
||
|
|
||
|
static int
|
||
10 years ago
|
init_drm(struct drm_backend *b, struct udev_device *device)
|
||
15 years ago
|
{
|
||
13 years ago
|
const char *filename, *sysnum;
|
||
12 years ago
|
uint64_t cap;
|
||
|
int fd, ret;
|
||
10 years ago
|
clockid_t clk_id;
|
||
13 years ago
|
|
||
13 years ago
|
sysnum = udev_device_get_sysnum(device);
|
||
|
if (sysnum)
|
||
10 years ago
|
b->drm.id = atoi(sysnum);
|
||
|
if (!sysnum || b->drm.id < 0) {
|
||
13 years ago
|
weston_log("cannot get device sysnum\n");
|
||
13 years ago
|
return -1;
|
||
|
}
|
||
|
|
||
15 years ago
|
filename = udev_device_get_devnode(device);
|
||
10 years ago
|
fd = weston_launcher_open(b->compositor->launcher, filename, O_RDWR);
|
||
15 years ago
|
if (fd < 0) {
|
||
15 years ago
|
/* Probably permissions error */
|
||
13 years ago
|
weston_log("couldn't open %s, skipping\n",
|
||
15 years ago
|
udev_device_get_devnode(device));
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
13 years ago
|
weston_log("using %s\n", filename);
|
||
|
|
||
10 years ago
|
b->drm.fd = fd;
|
||
|
b->drm.filename = strdup(filename);
|
||
12 years ago
|
|
||
12 years ago
|
ret = drmGetCap(fd, DRM_CAP_TIMESTAMP_MONOTONIC, &cap);
|
||
|
if (ret == 0 && cap == 1)
|
||
10 years ago
|
clk_id = CLOCK_MONOTONIC;
|
||
12 years ago
|
else
|
||
10 years ago
|
clk_id = CLOCK_REALTIME;
|
||
|
|
||
10 years ago
|
if (weston_compositor_set_presentation_clock(b->compositor, clk_id) < 0) {
|
||
10 years ago
|
weston_log("Error: failed to set presentation clock %d.\n",
|
||
|
clk_id);
|
||
|
return -1;
|
||
|
}
|
||
12 years ago
|
|
||
10 years ago
|
ret = drmGetCap(fd, DRM_CAP_CURSOR_WIDTH, &cap);
|
||
|
if (ret == 0)
|
||
10 years ago
|
b->cursor_width = cap;
|
||
10 years ago
|
else
|
||
10 years ago
|
b->cursor_width = 64;
|
||
10 years ago
|
|
||
|
ret = drmGetCap(fd, DRM_CAP_CURSOR_HEIGHT, &cap);
|
||
|
if (ret == 0)
|
||
10 years ago
|
b->cursor_height = cap;
|
||
10 years ago
|
else
|
||
10 years ago
|
b->cursor_height = 64;
|
||
10 years ago
|
|
||
12 years ago
|
return 0;
|
||
|
}
|
||
|
|
||
11 years ago
|
static struct gbm_device *
|
||
|
create_gbm_device(int fd)
|
||
12 years ago
|
{
|
||
11 years ago
|
struct gbm_device *gbm;
|
||
11 years ago
|
|
||
11 years ago
|
gl_renderer = weston_load_module("gl-renderer.so",
|
||
|
"gl_renderer_interface");
|
||
|
if (!gl_renderer)
|
||
11 years ago
|
return NULL;
|
||
11 years ago
|
|
||
|
/* 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);
|
||
|
|
||
11 years ago
|
gbm = gbm_create_device(fd);
|
||
15 years ago
|
|
||
11 years ago
|
return gbm;
|
||
|
}
|
||
|
|
||
10 years ago
|
/* When initializing EGL, if the preferred buffer format isn't available
|
||
9 years ago
|
* we may be able to substitute an ARGB format for an XRGB one.
|
||
10 years ago
|
*
|
||
|
* This returns 0 if substitution isn't possible, but 0 might be a
|
||
|
* legitimate format for other EGL platforms, so the caller is
|
||
|
* responsible for checking for 0 before calling gl_renderer->create().
|
||
|
*
|
||
|
* 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.
|
||
|
*/
|
||
11 years ago
|
static int
|
||
10 years ago
|
fallback_format_for(uint32_t format)
|
||
11 years ago
|
{
|
||
10 years ago
|
switch (format) {
|
||
|
case GBM_FORMAT_XRGB8888:
|
||
|
return GBM_FORMAT_ARGB8888;
|
||
|
case GBM_FORMAT_XRGB2101010:
|
||
|
return GBM_FORMAT_ARGB2101010;
|
||
|
default:
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
12 years ago
|
|
||
10 years ago
|
static int
|
||
10 years ago
|
drm_backend_create_gl_renderer(struct drm_backend *b)
|
||
10 years ago
|
{
|
||
9 years ago
|
EGLint format[3] = {
|
||
9 years ago
|
b->gbm_format,
|
||
|
fallback_format_for(b->gbm_format),
|
||
9 years ago
|
0,
|
||
10 years ago
|
};
|
||
9 years ago
|
int n_formats = 2;
|
||
10 years ago
|
|
||
|
if (format[1])
|
||
9 years ago
|
n_formats = 3;
|
||
10 years ago
|
if (gl_renderer->create(b->compositor,
|
||
10 years ago
|
EGL_PLATFORM_GBM_KHR,
|
||
10 years ago
|
(void *)b->gbm,
|
||
10 years ago
|
gl_renderer->opaque_attribs,
|
||
|
format,
|
||
|
n_formats) < 0) {
|
||
11 years ago
|
return -1;
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static int
|
||
10 years ago
|
init_egl(struct drm_backend *b)
|
||
11 years ago
|
{
|
||
10 years ago
|
b->gbm = create_gbm_device(b->drm.fd);
|
||
11 years ago
|
|
||
10 years ago
|
if (!b->gbm)
|
||
11 years ago
|
return -1;
|
||
|
|
||
10 years ago
|
if (drm_backend_create_gl_renderer(b) < 0) {
|
||
|
gbm_device_destroy(b->gbm);
|
||
13 years ago
|
return -1;
|
||
|
}
|
||
|
|
||
15 years ago
|
return 0;
|
||
|
}
|
||
|
|
||
12 years ago
|
static int
|
||
10 years ago
|
init_pixman(struct drm_backend *b)
|
||
12 years ago
|
{
|
||
10 years ago
|
return pixman_renderer_init(b->compositor);
|
||
12 years ago
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Add a mode to output's mode list
|
||
|
*
|
||
|
* Copy the supplied DRM mode into a Weston mode structure, and add it to the
|
||
|
* output's mode list.
|
||
|
*
|
||
|
* @param output DRM output to add mode to
|
||
|
* @param info DRM mode structure to add
|
||
|
* @returns Newly-allocated Weston/DRM mode structure
|
||
|
*/
|
||
12 years ago
|
static struct drm_mode *
|
||
11 years ago
|
drm_output_add_mode(struct drm_output *output, const drmModeModeInfo *info)
|
||
14 years ago
|
{
|
||
|
struct drm_mode *mode;
|
||
13 years ago
|
uint64_t refresh;
|
||
14 years ago
|
|
||
|
mode = malloc(sizeof *mode);
|
||
|
if (mode == NULL)
|
||
12 years ago
|
return NULL;
|
||
14 years ago
|
|
||
|
mode->base.flags = 0;
|
||
12 years ago
|
mode->base.width = info->hdisplay;
|
||
|
mode->base.height = info->vdisplay;
|
||
13 years ago
|
|
||
|
/* Calculate higher precision (mHz) refresh rate */
|
||
|
refresh = (info->clock * 1000000LL / info->htotal +
|
||
|
info->vtotal / 2) / info->vtotal;
|
||
|
|
||
|
if (info->flags & DRM_MODE_FLAG_INTERLACE)
|
||
|
refresh *= 2;
|
||
|
if (info->flags & DRM_MODE_FLAG_DBLSCAN)
|
||
|
refresh /= 2;
|
||
|
if (info->vscan > 1)
|
||
|
refresh /= info->vscan;
|
||
|
|
||
|
mode->base.refresh = refresh;
|
||
14 years ago
|
mode->mode_info = *info;
|
||
13 years ago
|
|
||
|
if (info->type & DRM_MODE_TYPE_PREFERRED)
|
||
|
mode->base.flags |= WL_OUTPUT_MODE_PREFERRED;
|
||
|
|
||
14 years ago
|
wl_list_insert(output->base.mode_list.prev, &mode->base.link);
|
||
|
|
||
12 years ago
|
return mode;
|
||
14 years ago
|
}
|
||
|
|
||
|
static int
|
||
|
drm_subpixel_to_wayland(int drm_value)
|
||
|
{
|
||
|
switch (drm_value) {
|
||
|
default:
|
||
|
case DRM_MODE_SUBPIXEL_UNKNOWN:
|
||
|
return WL_OUTPUT_SUBPIXEL_UNKNOWN;
|
||
|
case DRM_MODE_SUBPIXEL_NONE:
|
||
|
return WL_OUTPUT_SUBPIXEL_NONE;
|
||
|
case DRM_MODE_SUBPIXEL_HORIZONTAL_RGB:
|
||
|
return WL_OUTPUT_SUBPIXEL_HORIZONTAL_RGB;
|
||
|
case DRM_MODE_SUBPIXEL_HORIZONTAL_BGR:
|
||
|
return WL_OUTPUT_SUBPIXEL_HORIZONTAL_BGR;
|
||
|
case DRM_MODE_SUBPIXEL_VERTICAL_RGB:
|
||
|
return WL_OUTPUT_SUBPIXEL_VERTICAL_RGB;
|
||
|
case DRM_MODE_SUBPIXEL_VERTICAL_BGR:
|
||
|
return WL_OUTPUT_SUBPIXEL_VERTICAL_BGR;
|
||
|
}
|
||
|
}
|
||
|
|
||
13 years ago
|
/* returns a value between 0-255 range, where higher is brighter */
|
||
13 years ago
|
static uint32_t
|
||
|
drm_get_backlight(struct drm_output *output)
|
||
|
{
|
||
|
long brightness, max_brightness, norm;
|
||
|
|
||
|
brightness = backlight_get_brightness(output->backlight);
|
||
|
max_brightness = backlight_get_max_brightness(output->backlight);
|
||
|
|
||
13 years ago
|
/* convert it on a scale of 0 to 255 */
|
||
|
norm = (brightness * 255)/(max_brightness);
|
||
13 years ago
|
|
||
|
return (uint32_t) norm;
|
||
|
}
|
||
|
|
||
13 years ago
|
/* values accepted are between 0-255 range */
|
||
13 years ago
|
static void
|
||
|
drm_set_backlight(struct weston_output *output_base, uint32_t value)
|
||
|
{
|
||
|
struct drm_output *output = (struct drm_output *) output_base;
|
||
|
long max_brightness, new_brightness;
|
||
|
|
||
|
if (!output->backlight)
|
||
|
return;
|
||
|
|
||
13 years ago
|
if (value > 255)
|
||
13 years ago
|
return;
|
||
|
|
||
|
max_brightness = backlight_get_max_brightness(output->backlight);
|
||
|
|
||
|
/* get denormalized value */
|
||
13 years ago
|
new_brightness = (value * max_brightness) / 255;
|
||
13 years ago
|
|
||
|
backlight_set_brightness(output->backlight, new_brightness);
|
||
|
}
|
||
|
|
||
|
static drmModePropertyPtr
|
||
|
drm_get_prop(int fd, drmModeConnectorPtr connector, const char *name)
|
||
|
{
|
||
|
drmModePropertyPtr props;
|
||
|
int i;
|
||
|
|
||
|
for (i = 0; i < connector->count_props; i++) {
|
||
|
props = drmModeGetProperty(fd, connector->props[i]);
|
||
|
if (!props)
|
||
|
continue;
|
||
|
|
||
|
if (!strcmp(props->name, name))
|
||
|
return props;
|
||
|
|
||
|
drmModeFreeProperty(props);
|
||
|
}
|
||
|
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
|
||
|
{
|
||
|
struct drm_output *output = (struct drm_output *) output_base;
|
||
|
struct weston_compositor *ec = output_base->compositor;
|
||
10 years ago
|
struct drm_backend *b = (struct drm_backend *)ec->backend;
|
||
10 years ago
|
int ret;
|
||
13 years ago
|
|
||
12 years ago
|
if (!output->dpms_prop)
|
||
13 years ago
|
return;
|
||
|
|
||
10 years ago
|
ret = drmModeConnectorSetProperty(b->drm.fd, output->connector_id,
|
||
|
output->dpms_prop->prop_id, level);
|
||
|
if (ret) {
|
||
|
weston_log("DRM: DPMS: failed property set for %s\n",
|
||
|
output->base.name);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
output->dpms = level;
|
||
13 years ago
|
}
|
||
|
|
||
11 years ago
|
static const char * const connector_type_names[] = {
|
||
9 years ago
|
[DRM_MODE_CONNECTOR_Unknown] = "Unknown",
|
||
|
[DRM_MODE_CONNECTOR_VGA] = "VGA",
|
||
|
[DRM_MODE_CONNECTOR_DVII] = "DVI-I",
|
||
|
[DRM_MODE_CONNECTOR_DVID] = "DVI-D",
|
||
|
[DRM_MODE_CONNECTOR_DVIA] = "DVI-A",
|
||
|
[DRM_MODE_CONNECTOR_Composite] = "Composite",
|
||
|
[DRM_MODE_CONNECTOR_SVIDEO] = "SVIDEO",
|
||
|
[DRM_MODE_CONNECTOR_LVDS] = "LVDS",
|
||
|
[DRM_MODE_CONNECTOR_Component] = "Component",
|
||
|
[DRM_MODE_CONNECTOR_9PinDIN] = "DIN",
|
||
|
[DRM_MODE_CONNECTOR_DisplayPort] = "DP",
|
||
|
[DRM_MODE_CONNECTOR_HDMIA] = "HDMI-A",
|
||
|
[DRM_MODE_CONNECTOR_HDMIB] = "HDMI-B",
|
||
|
[DRM_MODE_CONNECTOR_TV] = "TV",
|
||
|
[DRM_MODE_CONNECTOR_eDP] = "eDP",
|
||
9 years ago
|
#ifdef DRM_MODE_CONNECTOR_DSI
|
||
9 years ago
|
[DRM_MODE_CONNECTOR_VIRTUAL] = "Virtual",
|
||
|
[DRM_MODE_CONNECTOR_DSI] = "DSI",
|
||
9 years ago
|
#endif
|
||
13 years ago
|
};
|
||
|
|
||
11 years ago
|
static char *
|
||
|
make_connector_name(const drmModeConnector *con)
|
||
|
{
|
||
|
char name[32];
|
||
9 years ago
|
const char *type_name = NULL;
|
||
11 years ago
|
|
||
|
if (con->connector_type < ARRAY_LENGTH(connector_type_names))
|
||
|
type_name = connector_type_names[con->connector_type];
|
||
9 years ago
|
|
||
|
if (!type_name)
|
||
|
type_name = "UNNAMED";
|
||
|
|
||
|
snprintf(name, sizeof name, "%s-%d", type_name, con->connector_type_id);
|
||
11 years ago
|
|
||
|
return strdup(name);
|
||
|
}
|
||
|
|
||
13 years ago
|
static int
|
||
10 years ago
|
find_crtc_for_connector(struct drm_backend *b,
|
||
13 years ago
|
drmModeRes *resources, drmModeConnector *connector)
|
||
|
{
|
||
|
drmModeEncoder *encoder;
|
||
|
uint32_t possible_crtcs;
|
||
|
int i, j;
|
||
|
|
||
|
for (j = 0; j < connector->count_encoders; j++) {
|
||
10 years ago
|
encoder = drmModeGetEncoder(b->drm.fd, connector->encoders[j]);
|
||
13 years ago
|
if (encoder == NULL) {
|
||
|
weston_log("Failed to get encoder.\n");
|
||
|
return -1;
|
||
|
}
|
||
|
possible_crtcs = encoder->possible_crtcs;
|
||
|
drmModeFreeEncoder(encoder);
|
||
|
|
||
|
for (i = 0; i < resources->count_crtcs; i++) {
|
||
|
if (possible_crtcs & (1 << i) &&
|
||
10 years ago
|
!(b->crtc_allocator & (1 << resources->crtcs[i])))
|
||
13 years ago
|
return i;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
12 years ago
|
/* Init output state that depends on gl or gbm */
|
||
|
static int
|
||
10 years ago
|
drm_output_init_egl(struct drm_output *output, struct drm_backend *b)
|
||
12 years ago
|
{
|
||
10 years ago
|
EGLint format[2] = {
|
||
9 years ago
|
output->gbm_format,
|
||
|
fallback_format_for(output->gbm_format),
|
||
10 years ago
|
};
|
||
|
int i, flags, n_formats = 1;
|
||
12 years ago
|
|
||
9 years ago
|
output->gbm_surface = gbm_surface_create(b->gbm,
|
||
11 years ago
|
output->base.current_mode->width,
|
||
|
output->base.current_mode->height,
|
||
10 years ago
|
format[0],
|
||
12 years ago
|
GBM_BO_USE_SCANOUT |
|
||
|
GBM_BO_USE_RENDERING);
|
||
9 years ago
|
if (!output->gbm_surface) {
|
||
12 years ago
|
weston_log("failed to create gbm surface\n");
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
10 years ago
|
if (format[1])
|
||
|
n_formats = 2;
|
||
10 years ago
|
if (gl_renderer->output_create(&output->base,
|
||
9 years ago
|
(EGLNativeWindowType)output->gbm_surface,
|
||
|
output->gbm_surface,
|
||
11 years ago
|
gl_renderer->opaque_attribs,
|
||
10 years ago
|
format,
|
||
|
n_formats) < 0) {
|
||
12 years ago
|
weston_log("failed to create gl renderer output state\n");
|
||
9 years ago
|
gbm_surface_destroy(output->gbm_surface);
|
||
12 years ago
|
return -1;
|
||
|
}
|
||
|
|
||
10 years ago
|
flags = GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE;
|
||
12 years ago
|
|
||
|
for (i = 0; i < 2; i++) {
|
||
9 years ago
|
if (output->gbm_cursor_bo[i])
|
||
12 years ago
|
continue;
|
||
|
|
||
9 years ago
|
output->gbm_cursor_bo[i] =
|
||
10 years ago
|
gbm_bo_create(b->gbm, b->cursor_width, b->cursor_height,
|
||
10 years ago
|
GBM_FORMAT_ARGB8888, flags);
|
||
12 years ago
|
}
|
||
|
|
||
9 years ago
|
if (output->gbm_cursor_bo[0] == NULL || output->gbm_cursor_bo[1] == NULL) {
|
||
12 years ago
|
weston_log("cursor buffers unavailable, using gl cursors\n");
|
||
10 years ago
|
b->cursors_are_broken = 1;
|
||
12 years ago
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
12 years ago
|
static int
|
||
10 years ago
|
drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
|
||
12 years ago
|
{
|
||
11 years ago
|
int w = output->base.current_mode->width;
|
||
|
int h = output->base.current_mode->height;
|
||
9 years ago
|
uint32_t format = output->gbm_format;
|
||
|
uint32_t pixman_format;
|
||
12 years ago
|
unsigned int i;
|
||
|
|
||
9 years ago
|
switch (format) {
|
||
|
case GBM_FORMAT_XRGB8888:
|
||
|
pixman_format = PIXMAN_x8r8g8b8;
|
||
|
break;
|
||
|
case GBM_FORMAT_RGB565:
|
||
|
pixman_format = PIXMAN_r5g6b5;
|
||
|
break;
|
||
|
default:
|
||
|
weston_log("Unsupported pixman format 0x%x\n", format);
|
||
|
return -1;
|
||
|
}
|
||
12 years ago
|
|
||
9 years ago
|
/* FIXME error checking */
|
||
12 years ago
|
for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
|
||
9 years ago
|
output->dumb[i] = drm_fb_create_dumb(b, w, h, format);
|
||
12 years ago
|
if (!output->dumb[i])
|
||
|
goto err;
|
||
|
|
||
|
output->image[i] =
|
||
9 years ago
|
pixman_image_create_bits(pixman_format, w, h,
|
||
12 years ago
|
output->dumb[i]->map,
|
||
|
output->dumb[i]->stride);
|
||
|
if (!output->image[i])
|
||
|
goto err;
|
||
|
}
|
||
|
|
||
|
if (pixman_renderer_output_create(&output->base) < 0)
|
||
|
goto err;
|
||
|
|
||
|
pixman_region32_init_rect(&output->previous_damage,
|
||
12 years ago
|
output->base.x, output->base.y, output->base.width, output->base.height);
|
||
12 years ago
|
|
||
|
return 0;
|
||
|
|
||
|
err:
|
||
|
for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
|
||
|
if (output->dumb[i])
|
||
|
drm_fb_destroy_dumb(output->dumb[i]);
|
||
|
if (output->image[i])
|
||
|
pixman_image_unref(output->image[i]);
|
||
|
|
||
|
output->dumb[i] = NULL;
|
||
|
output->image[i] = NULL;
|
||
|
}
|
||
|
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
static void
|
||
|
drm_output_fini_pixman(struct drm_output *output)
|
||
|
{
|
||
|
unsigned int i;
|
||
|
|
||
|
pixman_renderer_output_destroy(&output->base);
|
||
|
pixman_region32_fini(&output->previous_damage);
|
||
|
|
||
|
for (i = 0; i < ARRAY_LENGTH(output->dumb); i++) {
|
||
|
drm_fb_destroy_dumb(output->dumb[i]);
|
||
|
pixman_image_unref(output->image[i]);
|
||
|
output->dumb[i] = NULL;
|
||
|
output->image[i] = NULL;
|
||
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
static void
|
||
|
edid_parse_string(const uint8_t *data, char text[])
|
||
|
{
|
||
|
int i;
|
||
|
int replaced = 0;
|
||
|
|
||
|
/* this is always 12 bytes, but we can't guarantee it's null
|
||
|
* terminated or not junk. */
|
||
|
strncpy(text, (const char *) data, 12);
|
||
|
|
||
9 years ago
|
/* guarantee our new string is null-terminated */
|
||
|
text[12] = '\0';
|
||
|
|
||
12 years ago
|
/* remove insane chars */
|
||
|
for (i = 0; text[i] != '\0'; i++) {
|
||
|
if (text[i] == '\n' ||
|
||
|
text[i] == '\r') {
|
||
|
text[i] = '\0';
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* ensure string is printable */
|
||
|
for (i = 0; text[i] != '\0'; i++) {
|
||
|
if (!isprint(text[i])) {
|
||
|
text[i] = '-';
|
||
|
replaced++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* if the string is random junk, ignore the string */
|
||
|
if (replaced > 4)
|
||
|
text[0] = '\0';
|
||
|
}
|
||
|
|
||
|
#define EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING 0xfe
|
||
|
#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME 0xfc
|
||
|
#define EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER 0xff
|
||
|
#define EDID_OFFSET_DATA_BLOCKS 0x36
|
||
|
#define EDID_OFFSET_LAST_BLOCK 0x6c
|
||
|
#define EDID_OFFSET_PNPID 0x08
|
||
|
#define EDID_OFFSET_SERIAL 0x0c
|
||
|
|
||
|
static int
|
||
|
edid_parse(struct drm_edid *edid, const uint8_t *data, size_t length)
|
||
|
{
|
||
|
int i;
|
||
|
uint32_t serial_number;
|
||
|
|
||
|
/* check header */
|
||
|
if (length < 128)
|
||
|
return -1;
|
||
|
if (data[0] != 0x00 || data[1] != 0xff)
|
||
|
return -1;
|
||
|
|
||
|
/* decode the PNP ID from three 5 bit words packed into 2 bytes
|
||
|
* /--08--\/--09--\
|
||
|
* 7654321076543210
|
||
|
* |\---/\---/\---/
|
||
|
* R C1 C2 C3 */
|
||
|
edid->pnp_id[0] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x7c) / 4) - 1;
|
||
|
edid->pnp_id[1] = 'A' + ((data[EDID_OFFSET_PNPID + 0] & 0x3) * 8) + ((data[EDID_OFFSET_PNPID + 1] & 0xe0) / 32) - 1;
|
||
|
edid->pnp_id[2] = 'A' + (data[EDID_OFFSET_PNPID + 1] & 0x1f) - 1;
|
||
|
edid->pnp_id[3] = '\0';
|
||
|
|
||
|
/* maybe there isn't a ASCII serial number descriptor, so use this instead */
|
||
|
serial_number = (uint32_t) data[EDID_OFFSET_SERIAL + 0];
|
||
|
serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 1] * 0x100;
|
||
|
serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 2] * 0x10000;
|
||
|
serial_number += (uint32_t) data[EDID_OFFSET_SERIAL + 3] * 0x1000000;
|
||
|
if (serial_number > 0)
|
||
|
sprintf(edid->serial_number, "%lu", (unsigned long) serial_number);
|
||
|
|
||
|
/* parse EDID data */
|
||
|
for (i = EDID_OFFSET_DATA_BLOCKS;
|
||
|
i <= EDID_OFFSET_LAST_BLOCK;
|
||
|
i += 18) {
|
||
|
/* ignore pixel clock data */
|
||
|
if (data[i] != 0)
|
||
|
continue;
|
||
|
if (data[i+2] != 0)
|
||
|
continue;
|
||
|
|
||
|
/* any useful blocks? */
|
||
|
if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_NAME) {
|
||
|
edid_parse_string(&data[i+5],
|
||
|
edid->monitor_name);
|
||
|
} else if (data[i+3] == EDID_DESCRIPTOR_DISPLAY_PRODUCT_SERIAL_NUMBER) {
|
||
|
edid_parse_string(&data[i+5],
|
||
|
edid->serial_number);
|
||
|
} else if (data[i+3] == EDID_DESCRIPTOR_ALPHANUMERIC_DATA_STRING) {
|
||
|
edid_parse_string(&data[i+5],
|
||
|
edid->eisa_id);
|
||
|
}
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static void
|
||
10 years ago
|
find_and_parse_output_edid(struct drm_backend *b,
|
||
12 years ago
|
struct drm_output *output,
|
||
|
drmModeConnector *connector)
|
||
|
{
|
||
|
drmModePropertyBlobPtr edid_blob = NULL;
|
||
|
drmModePropertyPtr property;
|
||
|
int i;
|
||
|
int rc;
|
||
|
|
||
|
for (i = 0; i < connector->count_props && !edid_blob; i++) {
|
||
10 years ago
|
property = drmModeGetProperty(b->drm.fd, connector->props[i]);
|
||
12 years ago
|
if (!property)
|
||
|
continue;
|
||
|
if ((property->flags & DRM_MODE_PROP_BLOB) &&
|
||
|
!strcmp(property->name, "EDID")) {
|
||
10 years ago
|
edid_blob = drmModeGetPropertyBlob(b->drm.fd,
|
||
12 years ago
|
connector->prop_values[i]);
|
||
|
}
|
||
|
drmModeFreeProperty(property);
|
||
|
}
|
||
|
if (!edid_blob)
|
||
|
return;
|
||
|
|
||
|
rc = edid_parse(&output->edid,
|
||
|
edid_blob->data,
|
||
|
edid_blob->length);
|
||
|
if (!rc) {
|
||
|
weston_log("EDID data '%s', '%s', '%s'\n",
|
||
|
output->edid.pnp_id,
|
||
|
output->edid.monitor_name,
|
||
|
output->edid.serial_number);
|
||
|
if (output->edid.pnp_id[0] != '\0')
|
||
|
output->base.make = output->edid.pnp_id;
|
||
|
if (output->edid.monitor_name[0] != '\0')
|
||
|
output->base.model = output->edid.monitor_name;
|
||
|
if (output->edid.serial_number[0] != '\0')
|
||
|
output->base.serial_number = output->edid.serial_number;
|
||
|
}
|
||
|
drmModeFreePropertyBlob(edid_blob);
|
||
|
}
|
||
|
|
||
12 years ago
|
|
||
|
|
||
|
static int
|
||
|
parse_modeline(const char *s, drmModeModeInfo *mode)
|
||
|
{
|
||
|
char hsync[16];
|
||
|
char vsync[16];
|
||
|
float fclock;
|
||
|
|
||
|
mode->type = DRM_MODE_TYPE_USERDEF;
|
||
|
mode->hskew = 0;
|
||
|
mode->vscan = 0;
|
||
|
mode->vrefresh = 0;
|
||
|
mode->flags = 0;
|
||
|
|
||
12 years ago
|
if (sscanf(s, "%f %hd %hd %hd %hd %hd %hd %hd %hd %15s %15s",
|
||
12 years ago
|
&fclock,
|
||
|
&mode->hdisplay,
|
||
|
&mode->hsync_start,
|
||
|
&mode->hsync_end,
|
||
|
&mode->htotal,
|
||
|
&mode->vdisplay,
|
||
|
&mode->vsync_start,
|
||
|
&mode->vsync_end,
|
||
|
&mode->vtotal, hsync, vsync) != 11)
|
||
|
return -1;
|
||
|
|
||
|
mode->clock = fclock * 1000;
|
||
|
if (strcmp(hsync, "+hsync") == 0)
|
||
|
mode->flags |= DRM_MODE_FLAG_PHSYNC;
|
||
|
else if (strcmp(hsync, "-hsync") == 0)
|
||
|
mode->flags |= DRM_MODE_FLAG_NHSYNC;
|
||
|
else
|
||
|
return -1;
|
||
|
|
||
|
if (strcmp(vsync, "+vsync") == 0)
|
||
|
mode->flags |= DRM_MODE_FLAG_PVSYNC;
|
||
|
else if (strcmp(vsync, "-vsync") == 0)
|
||
|
mode->flags |= DRM_MODE_FLAG_NVSYNC;
|
||
|
else
|
||
|
return -1;
|
||
|
|
||
9 years ago
|
snprintf(mode->name, sizeof mode->name, "%dx%d@%.3f",
|
||
|
mode->hdisplay, mode->vdisplay, fclock);
|
||
|
|
||
12 years ago
|
return 0;
|
||
|
}
|
||
|
|
||
12 years ago
|
static void
|
||
10 years ago
|
setup_output_seat_constraint(struct drm_backend *b,
|
||
12 years ago
|
struct weston_output *output,
|
||
|
const char *s)
|
||
|
{
|
||
|
if (strcmp(s, "") != 0) {
|
||
9 years ago
|
struct weston_pointer *pointer;
|
||
12 years ago
|
struct udev_seat *seat;
|
||
|
|
||
10 years ago
|
seat = udev_seat_get_named(&b->input, s);
|
||
10 years ago
|
if (!seat)
|
||
|
return;
|
||
|
|
||
|
seat->base.output = output;
|
||
12 years ago
|
|
||
9 years ago
|
pointer = weston_seat_get_pointer(&seat->base);
|
||
|
if (pointer)
|
||
|
weston_pointer_clamp(pointer,
|
||
|
&pointer->x,
|
||
|
&pointer->y);
|
||
12 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
static int
|
||
9 years ago
|
parse_gbm_format(const char *s, uint32_t default_value, uint32_t *gbm_format)
|
||
11 years ago
|
{
|
||
|
int ret = 0;
|
||
|
|
||
|
if (s == NULL)
|
||
9 years ago
|
*gbm_format = default_value;
|
||
11 years ago
|
else if (strcmp(s, "xrgb8888") == 0)
|
||
9 years ago
|
*gbm_format = GBM_FORMAT_XRGB8888;
|
||
11 years ago
|
else if (strcmp(s, "rgb565") == 0)
|
||
9 years ago
|
*gbm_format = GBM_FORMAT_RGB565;
|
||
11 years ago
|
else if (strcmp(s, "xrgb2101010") == 0)
|
||
9 years ago
|
*gbm_format = GBM_FORMAT_XRGB2101010;
|
||
11 years ago
|
else {
|
||
|
weston_log("fatal: unrecognized pixel format: %s\n", s);
|
||
|
ret = -1;
|
||
|
}
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Choose suitable mode for an output
|
||
|
*
|
||
|
* Find the most suitable mode to use for initial setup (or reconfiguration on
|
||
|
* hotplug etc) for a DRM output.
|
||
|
*
|
||
|
* @param output DRM output to choose mode for
|
||
|
* @param kind Strategy and preference to use when choosing mode
|
||
|
* @param width Desired width for this output
|
||
|
* @param height Desired height for this output
|
||
|
* @param current_mode Mode currently being displayed on this output
|
||
|
* @param modeline Manually-entered mode (may be NULL)
|
||
|
* @returns A mode from the output's mode list, or NULL if none available
|
||
|
*/
|
||
|
static struct drm_mode *
|
||
9 years ago
|
drm_output_choose_initial_mode(struct drm_backend *backend,
|
||
|
struct drm_output *output,
|
||
|
enum weston_drm_backend_output_mode mode,
|
||
|
struct weston_drm_backend_output_config *config,
|
||
|
const drmModeModeInfo *current_mode)
|
||
11 years ago
|
{
|
||
|
struct drm_mode *preferred = NULL;
|
||
|
struct drm_mode *current = NULL;
|
||
|
struct drm_mode *configured = NULL;
|
||
|
struct drm_mode *best = NULL;
|
||
|
struct drm_mode *drm_mode;
|
||
9 years ago
|
drmModeModeInfo modeline;
|
||
|
int32_t width = 0;
|
||
|
int32_t height = 0;
|
||
|
|
||
|
if (mode == WESTON_DRM_BACKEND_OUTPUT_PREFERRED && config->modeline) {
|
||
|
if (sscanf(config->modeline, "%dx%d", &width, &height) != 2) {
|
||
|
width = -1;
|
||
|
|
||
|
if (parse_modeline(config->modeline, &modeline) == 0) {
|
||
|
configured = drm_output_add_mode(output, &modeline);
|
||
|
if (!configured)
|
||
|
return NULL;
|
||
|
} else {
|
||
|
weston_log("Invalid modeline \"%s\" for output %s\n",
|
||
|
config->modeline, output->base.name);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
11 years ago
|
|
||
|
wl_list_for_each_reverse(drm_mode, &output->base.mode_list, base.link) {
|
||
9 years ago
|
if (width == drm_mode->base.width &&
|
||
11 years ago
|
height == drm_mode->base.height)
|
||
|
configured = drm_mode;
|
||
|
|
||
9 years ago
|
if (memcmp(current_mode, &drm_mode->mode_info,
|
||
11 years ago
|
sizeof *current_mode) == 0)
|
||
|
current = drm_mode;
|
||
|
|
||
|
if (drm_mode->base.flags & WL_OUTPUT_MODE_PREFERRED)
|
||
|
preferred = drm_mode;
|
||
|
|
||
|
best = drm_mode;
|
||
|
}
|
||
|
|
||
|
if (current == NULL && current_mode->clock != 0) {
|
||
|
current = drm_output_add_mode(output, current_mode);
|
||
|
if (!current)
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (mode == WESTON_DRM_BACKEND_OUTPUT_CURRENT)
|
||
11 years ago
|
configured = current;
|
||
|
|
||
|
if (configured)
|
||
|
return configured;
|
||
|
|
||
|
if (preferred)
|
||
|
return preferred;
|
||
|
|
||
|
if (current)
|
||
|
return current;
|
||
|
|
||
|
if (best)
|
||
|
return best;
|
||
|
|
||
|
weston_log("no available modes for %s\n", output->base.name);
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
11 years ago
|
static int
|
||
|
connector_get_current_mode(drmModeConnector *connector, int drm_fd,
|
||
|
drmModeModeInfo *mode)
|
||
|
{
|
||
|
drmModeEncoder *encoder;
|
||
|
drmModeCrtc *crtc;
|
||
|
|
||
|
/* Get the current mode on the crtc that's currently driving
|
||
|
* this connector. */
|
||
|
encoder = drmModeGetEncoder(drm_fd, connector->encoder_id);
|
||
|
memset(mode, 0, sizeof *mode);
|
||
|
if (encoder != NULL) {
|
||
|
crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
|
||
|
drmModeFreeEncoder(encoder);
|
||
|
if (crtc == NULL)
|
||
|
return -1;
|
||
|
if (crtc->mode_valid)
|
||
|
*mode = crtc->mode;
|
||
|
drmModeFreeCrtc(crtc);
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Create and configure a Weston output structure
|
||
|
*
|
||
|
* Given a DRM connector, create a matching drm_output structure and add it
|
||
|
* to Weston's output list.
|
||
|
*
|
||
11 years ago
|
* @param b Weston backend structure structure
|
||
11 years ago
|
* @param resources DRM resources for this device
|
||
|
* @param connector DRM connector to use for this new output
|
||
|
* @param x Horizontal offset to use into global co-ordinate space
|
||
|
* @param y Vertical offset to use into global co-ordinate space
|
||
|
* @param drm_device udev device pointer
|
||
|
* @returns 0 on success, or -1 on failure
|
||
|
*/
|
||
15 years ago
|
static int
|
||
10 years ago
|
create_output_for_connector(struct drm_backend *b,
|
||
15 years ago
|
drmModeRes *resources,
|
||
14 years ago
|
drmModeConnector *connector,
|
||
13 years ago
|
int x, int y, struct udev_device *drm_device)
|
||
15 years ago
|
{
|
||
15 years ago
|
struct drm_output *output;
|
||
11 years ago
|
struct drm_mode *drm_mode, *next, *current;
|
||
12 years ago
|
struct weston_mode *m;
|
||
9 years ago
|
|
||
|
drmModeModeInfo crtc_mode;
|
||
|
int i;
|
||
|
enum weston_drm_backend_output_mode mode;
|
||
|
struct weston_drm_backend_output_config config = {{ 0 }};
|
||
15 years ago
|
|
||
10 years ago
|
i = find_crtc_for_connector(b, resources, connector);
|
||
13 years ago
|
if (i < 0) {
|
||
|
weston_log("No usable crtc/encoder pair for connector.\n");
|
||
15 years ago
|
return -1;
|
||
|
}
|
||
|
|
||
11 years ago
|
output = zalloc(sizeof *output);
|
||
13 years ago
|
if (output == NULL)
|
||
14 years ago
|
return -1;
|
||
|
|
||
|
output->base.subpixel = drm_subpixel_to_wayland(connector->subpixel);
|
||
11 years ago
|
output->base.name = make_connector_name(connector);
|
||
14 years ago
|
output->base.make = "unknown";
|
||
|
output->base.model = "unknown";
|
||
12 years ago
|
output->base.serial_number = "unknown";
|
||
14 years ago
|
wl_list_init(&output->base.mode_list);
|
||
15 years ago
|
|
||
9 years ago
|
mode = b->configure_output(b->compositor, b->use_current_mode,
|
||
|
output->base.name, &config);
|
||
|
if (parse_gbm_format(config.gbm_format, b->gbm_format, &output->gbm_format) == -1)
|
||
9 years ago
|
output->gbm_format = b->gbm_format;
|
||
11 years ago
|
|
||
9 years ago
|
setup_output_seat_constraint(b, &output->base,
|
||
|
config.seat ? config.seat : "");
|
||
|
free(config.seat);
|
||
12 years ago
|
|
||
15 years ago
|
output->crtc_id = resources->crtcs[i];
|
||
12 years ago
|
output->pipe = i;
|
||
10 years ago
|
b->crtc_allocator |= (1 << output->crtc_id);
|
||
15 years ago
|
output->connector_id = connector->connector_id;
|
||
10 years ago
|
b->connector_allocator |= (1 << output->connector_id);
|
||
14 years ago
|
|
||
10 years ago
|
output->original_crtc = drmModeGetCrtc(b->drm.fd, output->crtc_id);
|
||
|
output->dpms_prop = drm_get_prop(b->drm.fd, connector, "DPMS");
|
||
13 years ago
|
|
||
11 years ago
|
if (connector_get_current_mode(connector, b->drm.fd, &crtc_mode) < 0)
|
||
|
goto err_free;
|
||
13 years ago
|
|
||
13 years ago
|
for (i = 0; i < connector->count_modes; i++) {
|
||
12 years ago
|
drm_mode = drm_output_add_mode(output, &connector->modes[i]);
|
||
12 years ago
|
if (!drm_mode)
|
||
13 years ago
|
goto err_free;
|
||
|
}
|
||
13 years ago
|
|
||
9 years ago
|
if (mode == WESTON_DRM_BACKEND_OUTPUT_OFF) {
|
||
12 years ago
|
weston_log("Disabling output %s\n", output->base.name);
|
||
10 years ago
|
drmModeSetCrtc(b->drm.fd, output->crtc_id,
|
||
12 years ago
|
0, 0, 0, 0, 0, NULL);
|
||
13 years ago
|
goto err_free;
|
||
|
}
|
||
|
|
||
9 years ago
|
current = drm_output_choose_initial_mode(b, output, mode, &config,
|
||
|
&crtc_mode);
|
||
11 years ago
|
if (!current)
|
||
13 years ago
|
goto err_free;
|
||
11 years ago
|
output->base.current_mode = ¤t->base;
|
||
11 years ago
|
output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
|
||
13 years ago
|
|
||
10 years ago
|
weston_output_init(&output->base, b->compositor, x, y,
|
||
12 years ago
|
connector->mmWidth, connector->mmHeight,
|
||
9 years ago
|
config.base.transform, config.base.scale);
|
||
12 years ago
|
|
||
10 years ago
|
if (b->use_pixman) {
|
||
|
if (drm_output_init_pixman(output, b) < 0) {
|
||
12 years ago
|
weston_log("Failed to init output pixman state\n");
|
||
|
goto err_output;
|
||
|
}
|
||
10 years ago
|
} else if (drm_output_init_egl(output, b) < 0) {
|
||
12 years ago
|
weston_log("Failed to init output gl state\n");
|
||
12 years ago
|
goto err_output;
|
||
12 years ago
|
}
|
||
13 years ago
|
|
||
13 years ago
|
output->backlight = backlight_init(drm_device,
|
||
|
connector->connector_type);
|
||
|
if (output->backlight) {
|
||
12 years ago
|
weston_log("Initialized backlight, device %s\n",
|
||
|
output->backlight->path);
|
||
13 years ago
|
output->base.set_backlight = drm_set_backlight;
|
||
|
output->base.backlight_current = drm_get_backlight(output);
|
||
12 years ago
|
} else {
|
||
|
weston_log("Failed to initialize backlight\n");
|
||
13 years ago
|
}
|
||
|
|
||
10 years ago
|
weston_compositor_add_output(b->compositor, &output->base);
|
||
13 years ago
|
|
||
10 years ago
|
find_and_parse_output_edid(b, output, connector);
|
||
12 years ago
|
if (connector->connector_type == DRM_MODE_CONNECTOR_LVDS)
|
||
|
output->base.connection_internal = 1;
|
||
12 years ago
|
|
||
12 years ago
|
output->base.start_repaint_loop = drm_output_start_repaint_loop;
|
||
13 years ago
|
output->base.repaint = drm_output_repaint;
|
||
13 years ago
|
output->base.destroy = drm_output_destroy;
|
||
13 years ago
|
output->base.assign_planes = drm_assign_planes;
|
||
13 years ago
|
output->base.set_dpms = drm_set_dpms;
|
||
13 years ago
|
output->base.switch_mode = drm_output_switch_mode;
|
||
14 years ago
|
|
||
12 years ago
|
output->base.gamma_size = output->original_crtc->gamma_size;
|
||
|
output->base.set_gamma = drm_output_set_gamma;
|
||
|
|
||
9 years ago
|
weston_plane_init(&output->cursor_plane, b->compositor,
|
||
|
INT32_MIN, INT32_MIN);
|
||
10 years ago
|
weston_plane_init(&output->fb_plane, b->compositor, 0, 0);
|
||
13 years ago
|
|
||
10 years ago
|
weston_compositor_stack_plane(b->compositor, &output->cursor_plane, NULL);
|
||
|
weston_compositor_stack_plane(b->compositor, &output->fb_plane,
|
||
|
&b->compositor->primary_plane);
|
||
12 years ago
|
|
||
13 years ago
|
weston_log("Output %s, (connector %d, crtc %d)\n",
|
||
12 years ago
|
output->base.name, output->connector_id, output->crtc_id);
|
||
13 years ago
|
wl_list_for_each(m, &output->base.mode_list, link)
|
||
11 years ago
|
weston_log_continue(STAMP_SPACE "mode %dx%d@%.1f%s%s%s\n",
|
||
13 years ago
|
m->width, m->height, m->refresh / 1000.0,
|
||
|
m->flags & WL_OUTPUT_MODE_PREFERRED ?
|
||
|
", preferred" : "",
|
||
|
m->flags & WL_OUTPUT_MODE_CURRENT ?
|
||
|
", current" : "",
|
||
|
connector->count_modes == 0 ?
|
||
|
", built-in" : "");
|
||
13 years ago
|
|
||
10 years ago
|
/* Set native_ fields, so weston_output_mode_switch_to_native() works */
|
||
|
output->base.native_mode = output->base.current_mode;
|
||
|
output->base.native_scale = output->base.current_scale;
|
||
|
|
||
15 years ago
|
return 0;
|
||
13 years ago
|
|
||
12 years ago
|
err_output:
|
||
|
weston_output_destroy(&output->base);
|
||
13 years ago
|
err_free:
|
||
|
wl_list_for_each_safe(drm_mode, next, &output->base.mode_list,
|
||
|
base.link) {
|
||
|
wl_list_remove(&drm_mode->base.link);
|
||
|
free(drm_mode);
|
||
|
}
|
||
|
|
||
|
drmModeFreeCrtc(output->original_crtc);
|
||
10 years ago
|
b->crtc_allocator &= ~(1 << output->crtc_id);
|
||
|
b->connector_allocator &= ~(1 << output->connector_id);
|
||
13 years ago
|
free(output);
|
||
9 years ago
|
free(config.modeline);
|
||
13 years ago
|
|
||
13 years ago
|
return -1;
|
||
15 years ago
|
}
|
||
|
|
||
13 years ago
|
static void
|
||
10 years ago
|
create_sprites(struct drm_backend *b)
|
||
13 years ago
|
{
|
||
|
struct drm_sprite *sprite;
|
||
|
drmModePlaneRes *plane_res;
|
||
|
drmModePlane *plane;
|
||
13 years ago
|
uint32_t i;
|
||
13 years ago
|
|
||
10 years ago
|
plane_res = drmModeGetPlaneResources(b->drm.fd);
|
||
13 years ago
|
if (!plane_res) {
|
||
13 years ago
|
weston_log("failed to get plane resources: %s\n",
|
||
13 years ago
|
strerror(errno));
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
for (i = 0; i < plane_res->count_planes; i++) {
|
||
10 years ago
|
plane = drmModeGetPlane(b->drm.fd, plane_res->planes[i]);
|
||
13 years ago
|
if (!plane)
|
||
|
continue;
|
||
|
|
||
11 years ago
|
sprite = zalloc(sizeof(*sprite) + ((sizeof(uint32_t)) *
|
||
13 years ago
|
plane->count_formats));
|
||
|
if (!sprite) {
|
||
13 years ago
|
weston_log("%s: out of memory\n",
|
||
13 years ago
|
__func__);
|
||
11 years ago
|
drmModeFreePlane(plane);
|
||
13 years ago
|
continue;
|
||
|
}
|
||
|
|
||
|
sprite->possible_crtcs = plane->possible_crtcs;
|
||
|
sprite->plane_id = plane->plane_id;
|
||
12 years ago
|
sprite->current = NULL;
|
||
|
sprite->next = NULL;
|
||
10 years ago
|
sprite->backend = b;
|
||
13 years ago
|
sprite->count_formats = plane->count_formats;
|
||
|
memcpy(sprite->formats, plane->formats,
|
||
13 years ago
|
plane->count_formats * sizeof(plane->formats[0]));
|
||
13 years ago
|
drmModeFreePlane(plane);
|
||
10 years ago
|
weston_plane_init(&sprite->plane, b->compositor, 0, 0);
|
||
|
weston_compositor_stack_plane(b->compositor, &sprite->plane,
|
||
|
&b->compositor->primary_plane);
|
||
13 years ago
|
|
||
10 years ago
|
wl_list_insert(&b->sprite_list, &sprite->link);
|
||
13 years ago
|
}
|
||
|
|
||
12 years ago
|
drmModeFreePlaneResources(plane_res);
|
||
13 years ago
|
}
|
||
|
|
||
13 years ago
|
static void
|
||
10 years ago
|
destroy_sprites(struct drm_backend *backend)
|
||
13 years ago
|
{
|
||
|
struct drm_sprite *sprite, *next;
|
||
|
struct drm_output *output;
|
||
|
|
||
10 years ago
|
output = container_of(backend->compositor->output_list.next,
|
||
13 years ago
|
struct drm_output, base.link);
|
||
|
|
||
10 years ago
|
wl_list_for_each_safe(sprite, next, &backend->sprite_list, link) {
|
||
|
drmModeSetPlane(backend->drm.fd,
|
||
13 years ago
|
sprite->plane_id,
|
||
|
output->crtc_id, 0, 0,
|
||
|
0, 0, 0, 0, 0, 0, 0, 0);
|
||
12 years ago
|
drm_output_release_fb(output, sprite->current);
|
||
|
drm_output_release_fb(output, sprite->next);
|
||
13 years ago
|
weston_plane_release(&sprite->plane);
|
||
13 years ago
|
free(sprite);
|
||
|
}
|
||
|
}
|
||
13 years ago
|
|
||
15 years ago
|
static int
|
||
10 years ago
|
create_outputs(struct drm_backend *b, uint32_t option_connector,
|
||
13 years ago
|
struct udev_device *drm_device)
|
||
15 years ago
|
{
|
||
|
drmModeConnector *connector;
|
||
|
drmModeRes *resources;
|
||
|
int i;
|
||
14 years ago
|
int x = 0, y = 0;
|
||
15 years ago
|
|
||
10 years ago
|
resources = drmModeGetResources(b->drm.fd);
|
||
15 years ago
|
if (!resources) {
|
||
13 years ago
|
weston_log("drmModeGetResources failed\n");
|
||
15 years ago
|
return -1;
|
||
|
}
|
||
|
|
||
10 years ago
|
b->crtcs = calloc(resources->count_crtcs, sizeof(uint32_t));
|
||
|
if (!b->crtcs) {
|
||
13 years ago
|
drmModeFreeResources(resources);
|
||
13 years ago
|
return -1;
|
||
13 years ago
|
}
|
||
13 years ago
|
|
||
10 years ago
|
b->min_width = resources->min_width;
|
||
|
b->max_width = resources->max_width;
|
||
|
b->min_height = resources->min_height;
|
||
|
b->max_height = resources->max_height;
|
||
12 years ago
|
|
||
10 years ago
|
b->num_crtcs = resources->count_crtcs;
|
||
|
memcpy(b->crtcs, resources->crtcs, sizeof(uint32_t) * b->num_crtcs);
|
||
13 years ago
|
|
||
15 years ago
|
for (i = 0; i < resources->count_connectors; i++) {
|
||
10 years ago
|
connector = drmModeGetConnector(b->drm.fd,
|
||
13 years ago
|
resources->connectors[i]);
|
||
15 years ago
|
if (connector == NULL)
|
||
|
continue;
|
||
|
|
||
|
if (connector->connection == DRM_MODE_CONNECTED &&
|
||
|
(option_connector == 0 ||
|
||
14 years ago
|
connector->connector_id == option_connector)) {
|
||
10 years ago
|
if (create_output_for_connector(b, resources,
|
||
13 years ago
|
connector, x, y,
|
||
|
drm_device) < 0) {
|
||
13 years ago
|
drmModeFreeConnector(connector);
|
||
|
continue;
|
||
|
}
|
||
15 years ago
|
|
||
10 years ago
|
x += container_of(b->compositor->output_list.prev,
|
||
13 years ago
|
struct weston_output,
|
||
12 years ago
|
link)->width;
|
||
14 years ago
|
}
|
||
14 years ago
|
|
||
15 years ago
|
drmModeFreeConnector(connector);
|
||
|
}
|
||
|
|
||
10 years ago
|
if (wl_list_empty(&b->compositor->output_list)) {
|
||
13 years ago
|
weston_log("No currently active connector found.\n");
|
||
13 years ago
|
drmModeFreeResources(resources);
|
||
15 years ago
|
return -1;
|
||
|
}
|
||
|
|
||
|
drmModeFreeResources(resources);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
14 years ago
|
static void
|
||
10 years ago
|
update_outputs(struct drm_backend *b, struct udev_device *drm_device)
|
||
14 years ago
|
{
|
||
|
drmModeConnector *connector;
|
||
|
drmModeRes *resources;
|
||
|
struct drm_output *output, *next;
|
||
|
int x = 0, y = 0;
|
||
|
uint32_t connected = 0, disconnects = 0;
|
||
|
int i;
|
||
|
|
||
10 years ago
|
resources = drmModeGetResources(b->drm.fd);
|
||
14 years ago
|
if (!resources) {
|
||
13 years ago
|
weston_log("drmModeGetResources failed\n");
|
||
14 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
/* collect new connects */
|
||
|
for (i = 0; i < resources->count_connectors; i++) {
|
||
13 years ago
|
int connector_id = resources->connectors[i];
|
||
|
|
||
10 years ago
|
connector = drmModeGetConnector(b->drm.fd, connector_id);
|
||
13 years ago
|
if (connector == NULL)
|
||
|
continue;
|
||
|
|
||
|
if (connector->connection != DRM_MODE_CONNECTED) {
|
||
|
drmModeFreeConnector(connector);
|
||
14 years ago
|
continue;
|
||
13 years ago
|
}
|
||
14 years ago
|
|
||
13 years ago
|
connected |= (1 << connector_id);
|
||
|
|
||
10 years ago
|
if (!(b->connector_allocator & (1 << connector_id))) {
|
||
13 years ago
|
struct weston_output *last =
|
||
10 years ago
|
container_of(b->compositor->output_list.prev,
|
||
13 years ago
|
struct weston_output, link);
|
||
14 years ago
|
|
||
|
/* XXX: not yet needed, we die with 0 outputs */
|
||
10 years ago
|
if (!wl_list_empty(&b->compositor->output_list))
|
||
12 years ago
|
x = last->x + last->width;
|
||
14 years ago
|
else
|
||
|
x = 0;
|
||
|
y = 0;
|
||
10 years ago
|
create_output_for_connector(b, resources,
|
||
13 years ago
|
connector, x, y,
|
||
|
drm_device);
|
||
13 years ago
|
weston_log("connector %d connected\n", connector_id);
|
||
13 years ago
|
|
||
14 years ago
|
}
|
||
|
drmModeFreeConnector(connector);
|
||
|
}
|
||
|
drmModeFreeResources(resources);
|
||
|
|
||
10 years ago
|
disconnects = b->connector_allocator & ~connected;
|
||
14 years ago
|
if (disconnects) {
|
||
10 years ago
|
wl_list_for_each_safe(output, next, &b->compositor->output_list,
|
||
14 years ago
|
base.link) {
|
||
|
if (disconnects & (1 << output->connector_id)) {
|
||
|
disconnects &= ~(1 << output->connector_id);
|
||
13 years ago
|
weston_log("connector %d disconnected\n",
|
||
14 years ago
|
output->connector_id);
|
||
13 years ago
|
drm_output_destroy(&output->base);
|
||
14 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
/* FIXME: handle zero outputs, without terminating */
|
||
10 years ago
|
if (b->connector_allocator == 0)
|
||
10 years ago
|
weston_compositor_exit(b->compositor);
|
||
14 years ago
|
}
|
||
|
|
||
|
static int
|
||
10 years ago
|
udev_event_is_hotplug(struct drm_backend *b, struct udev_device *device)
|
||
14 years ago
|
{
|
||
13 years ago
|
const char *sysnum;
|
||
13 years ago
|
const char *val;
|
||
13 years ago
|
|
||
|
sysnum = udev_device_get_sysnum(device);
|
||
10 years ago
|
if (!sysnum || atoi(sysnum) != b->drm.id)
|
||
13 years ago
|
return 0;
|
||
13 years ago
|
|
||
13 years ago
|
val = udev_device_get_property_value(device, "HOTPLUG");
|
||
|
if (!val)
|
||
14 years ago
|
return 0;
|
||
|
|
||
13 years ago
|
return strcmp(val, "1") == 0;
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
static int
|
||
14 years ago
|
udev_drm_event(int fd, uint32_t mask, void *data)
|
||
|
{
|
||
10 years ago
|
struct drm_backend *b = data;
|
||
14 years ago
|
struct udev_device *event;
|
||
|
|
||
10 years ago
|
event = udev_monitor_receive_device(b->udev_monitor);
|
||
13 years ago
|
|
||
10 years ago
|
if (udev_event_is_hotplug(b, event))
|
||
|
update_outputs(b, event);
|
||
14 years ago
|
|
||
|
udev_device_unref(event);
|
||
14 years ago
|
|
||
|
return 1;
|
||
14 years ago
|
}
|
||
|
|
||
13 years ago
|
static void
|
||
|
drm_restore(struct weston_compositor *ec)
|
||
|
{
|
||
11 years ago
|
weston_launcher_restore(ec->launcher);
|
||
13 years ago
|
}
|
||
|
|
||
14 years ago
|
static void
|
||
13 years ago
|
drm_destroy(struct weston_compositor *ec)
|
||
14 years ago
|
{
|
||
10 years ago
|
struct drm_backend *b = (struct drm_backend *) ec->backend;
|
||
14 years ago
|
|
||
10 years ago
|
udev_input_destroy(&b->input);
|
||
13 years ago
|
|
||
10 years ago
|
wl_event_source_remove(b->udev_drm_source);
|
||
|
wl_event_source_remove(b->drm_source);
|
||
13 years ago
|
|
||
10 years ago
|
destroy_sprites(b);
|
||
12 years ago
|
|
||
11 years ago
|
weston_compositor_shutdown(ec);
|
||
|
|
||
10 years ago
|
if (b->gbm)
|
||
|
gbm_device_destroy(b->gbm);
|
||
12 years ago
|
|
||
10 years ago
|
weston_launcher_destroy(ec->launcher);
|
||
14 years ago
|
|
||
10 years ago
|
close(b->drm.fd);
|
||
|
free(b);
|
||
14 years ago
|
}
|
||
|
|
||
13 years ago
|
static void
|
||
10 years ago
|
drm_backend_set_modes(struct drm_backend *backend)
|
||
13 years ago
|
{
|
||
|
struct drm_output *output;
|
||
|
struct drm_mode *drm_mode;
|
||
|
int ret;
|
||
|
|
||
10 years ago
|
wl_list_for_each(output, &backend->compositor->output_list, base.link) {
|
||
12 years ago
|
if (!output->current) {
|
||
|
/* If something that would cause the output to
|
||
|
* switch mode happened while in another vt, we
|
||
|
* might not have a current drm_fb. In that case,
|
||
|
* schedule a repaint and let drm_output_repaint
|
||
|
* handle setting the mode. */
|
||
|
weston_output_schedule_repaint(&output->base);
|
||
|
continue;
|
||
|
}
|
||
|
|
||
11 years ago
|
drm_mode = (struct drm_mode *) output->base.current_mode;
|
||
10 years ago
|
ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
|
||
13 years ago
|
output->current->fb_id, 0, 0,
|
||
13 years ago
|
&output->connector_id, 1,
|
||
|
&drm_mode->mode_info);
|
||
|
if (ret < 0) {
|
||
13 years ago
|
weston_log(
|
||
13 years ago
|
"failed to set mode %dx%d for output at %d,%d: %m\n",
|
||
10 years ago
|
drm_mode->base.width, drm_mode->base.height,
|
||
13 years ago
|
output->base.x, output->base.y);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
static void
|
||
11 years ago
|
session_notify(struct wl_listener *listener, void *data)
|
||
14 years ago
|
{
|
||
11 years ago
|
struct weston_compositor *compositor = data;
|
||
10 years ago
|
struct drm_backend *b = (struct drm_backend *)compositor->backend;
|
||
13 years ago
|
struct drm_sprite *sprite;
|
||
13 years ago
|
struct drm_output *output;
|
||
14 years ago
|
|
||
10 years ago
|
if (compositor->session_active) {
|
||
11 years ago
|
weston_log("activating session\n");
|
||
10 years ago
|
compositor->state = b->prev_state;
|
||
|
drm_backend_set_modes(b);
|
||
13 years ago
|
weston_compositor_damage_all(compositor);
|
||
10 years ago
|
udev_input_enable(&b->input);
|
||
11 years ago
|
} else {
|
||
|
weston_log("deactivating session\n");
|
||
10 years ago
|
udev_input_disable(&b->input);
|
||
13 years ago
|
|
||
10 years ago
|
b->prev_state = compositor->state;
|
||
12 years ago
|
weston_compositor_offscreen(compositor);
|
||
14 years ago
|
|
||
13 years ago
|
/* If we have a repaint scheduled (either from a
|
||
|
* pending pageflip or the idle handler), make sure we
|
||
|
* cancel that so we don't try to pageflip when we're
|
||
12 years ago
|
* vt switched away. The OFFSCREEN state will prevent
|
||
13 years ago
|
* further attemps at repainting. When we switch
|
||
|
* back, we schedule a repaint, which will process
|
||
|
* pending frame callbacks. */
|
||
|
|
||
10 years ago
|
wl_list_for_each(output, &compositor->output_list, base.link) {
|
||
13 years ago
|
output->base.repaint_needed = 0;
|
||
10 years ago
|
drmModeSetCursor(b->drm.fd, output->crtc_id, 0, 0, 0);
|
||
13 years ago
|
}
|
||
|
|
||
10 years ago
|
output = container_of(compositor->output_list.next,
|
||
13 years ago
|
struct drm_output, base.link);
|
||
13 years ago
|
|
||
10 years ago
|
wl_list_for_each(sprite, &b->sprite_list, link)
|
||
|
drmModeSetPlane(b->drm.fd,
|
||
13 years ago
|
sprite->plane_id,
|
||
13 years ago
|
output->crtc_id, 0, 0,
|
||
13 years ago
|
0, 0, 0, 0, 0, 0, 0, 0);
|
||
14 years ago
|
};
|
||
|
}
|
||
|
|
||
12 years ago
|
/*
|
||
|
* Find primary GPU
|
||
|
* Some systems may have multiple DRM devices attached to a single seat. This
|
||
|
* function loops over all devices and tries to find a PCI device with the
|
||
|
* boot_vga sysfs attribute set to 1.
|
||
|
* If no such device is found, the first DRM device reported by udev is used.
|
||
|
*/
|
||
|
static struct udev_device*
|
||
10 years ago
|
find_primary_gpu(struct drm_backend *b, const char *seat)
|
||
12 years ago
|
{
|
||
|
struct udev_enumerate *e;
|
||
|
struct udev_list_entry *entry;
|
||
|
const char *path, *device_seat, *id;
|
||
|
struct udev_device *device, *drm_device, *pci;
|
||
|
|
||
10 years ago
|
e = udev_enumerate_new(b->udev);
|
||
12 years ago
|
udev_enumerate_add_match_subsystem(e, "drm");
|
||
|
udev_enumerate_add_match_sysname(e, "card[0-9]*");
|
||
|
|
||
|
udev_enumerate_scan_devices(e);
|
||
|
drm_device = NULL;
|
||
|
udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
|
||
|
path = udev_list_entry_get_name(entry);
|
||
10 years ago
|
device = udev_device_new_from_syspath(b->udev, path);
|
||
12 years ago
|
if (!device)
|
||
|
continue;
|
||
|
device_seat = udev_device_get_property_value(device, "ID_SEAT");
|
||
|
if (!device_seat)
|
||
|
device_seat = default_seat;
|
||
|
if (strcmp(device_seat, seat)) {
|
||
|
udev_device_unref(device);
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
pci = udev_device_get_parent_with_subsystem_devtype(device,
|
||
|
"pci", NULL);
|
||
|
if (pci) {
|
||
|
id = udev_device_get_sysattr_value(pci, "boot_vga");
|
||
|
if (id && !strcmp(id, "1")) {
|
||
|
if (drm_device)
|
||
|
udev_device_unref(drm_device);
|
||
|
drm_device = device;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (!drm_device)
|
||
|
drm_device = device;
|
||
|
else
|
||
|
udev_device_unref(device);
|
||
|
}
|
||
|
|
||
|
udev_enumerate_unref(e);
|
||
|
return drm_device;
|
||
|
}
|
||
|
|
||
12 years ago
|
static void
|
||
10 years ago
|
planes_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
|
||
|
void *data)
|
||
12 years ago
|
{
|
||
10 years ago
|
struct drm_backend *b = data;
|
||
12 years ago
|
|
||
12 years ago
|
switch (key) {
|
||
|
case KEY_C:
|
||
10 years ago
|
b->cursors_are_broken ^= 1;
|
||
12 years ago
|
break;
|
||
|
case KEY_V:
|
||
10 years ago
|
b->sprites_are_broken ^= 1;
|
||
12 years ago
|
break;
|
||
|
case KEY_O:
|
||
10 years ago
|
b->sprites_hidden ^= 1;
|
||
12 years ago
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
12 years ago
|
}
|
||
|
|
||
11 years ago
|
#ifdef BUILD_VAAPI_RECORDER
|
||
11 years ago
|
static void
|
||
|
recorder_destroy(struct drm_output *output)
|
||
|
{
|
||
|
vaapi_recorder_destroy(output->recorder);
|
||
|
output->recorder = NULL;
|
||
|
|
||
|
output->base.disable_planes--;
|
||
|
|
||
|
wl_list_remove(&output->recorder_frame_listener.link);
|
||
|
weston_log("[libva recorder] done\n");
|
||
|
}
|
||
|
|
||
11 years ago
|
static void
|
||
|
recorder_frame_notify(struct wl_listener *listener, void *data)
|
||
|
{
|
||
|
struct drm_output *output;
|
||
10 years ago
|
struct drm_backend *b;
|
||
11 years ago
|
int fd, ret;
|
||
|
|
||
|
output = container_of(listener, struct drm_output,
|
||
|
recorder_frame_listener);
|
||
10 years ago
|
b = (struct drm_backend *)output->base.compositor->backend;
|
||
11 years ago
|
|
||
|
if (!output->recorder)
|
||
|
return;
|
||
|
|
||
10 years ago
|
ret = drmPrimeHandleToFD(b->drm.fd, output->current->handle,
|
||
11 years ago
|
DRM_CLOEXEC, &fd);
|
||
|
if (ret) {
|
||
|
weston_log("[libva recorder] "
|
||
|
"failed to create prime fd for front buffer\n");
|
||
|
return;
|
||
|
}
|
||
|
|
||
11 years ago
|
ret = vaapi_recorder_frame(output->recorder, fd,
|
||
|
output->current->stride);
|
||
|
if (ret < 0) {
|
||
|
weston_log("[libva recorder] aborted: %m\n");
|
||
|
recorder_destroy(output);
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
static void *
|
||
10 years ago
|
create_recorder(struct drm_backend *b, int width, int height,
|
||
11 years ago
|
const char *filename)
|
||
|
{
|
||
|
int fd;
|
||
|
drm_magic_t magic;
|
||
|
|
||
10 years ago
|
fd = open(b->drm.filename, O_RDWR | O_CLOEXEC);
|
||
11 years ago
|
if (fd < 0)
|
||
|
return NULL;
|
||
|
|
||
|
drmGetMagic(fd, &magic);
|
||
10 years ago
|
drmAuthMagic(b->drm.fd, magic);
|
||
11 years ago
|
|
||
|
return vaapi_recorder_create(fd, width, height, filename);
|
||
|
}
|
||
|
|
||
|
static void
|
||
10 years ago
|
recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
|
||
11 years ago
|
void *data)
|
||
|
{
|
||
10 years ago
|
struct drm_backend *b = data;
|
||
11 years ago
|
struct drm_output *output;
|
||
|
int width, height;
|
||
|
|
||
10 years ago
|
output = container_of(b->compositor->output_list.next,
|
||
11 years ago
|
struct drm_output, base.link);
|
||
|
|
||
|
if (!output->recorder) {
|
||
9 years ago
|
if (output->gbm_format != GBM_FORMAT_XRGB8888) {
|
||
11 years ago
|
weston_log("failed to start vaapi recorder: "
|
||
|
"output format not supported\n");
|
||
|
return;
|
||
|
}
|
||
|
|
||
11 years ago
|
width = output->base.current_mode->width;
|
||
|
height = output->base.current_mode->height;
|
||
11 years ago
|
|
||
|
output->recorder =
|
||
10 years ago
|
create_recorder(b, width, height, "capture.h264");
|
||
11 years ago
|
if (!output->recorder) {
|
||
|
weston_log("failed to create vaapi recorder\n");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
output->base.disable_planes++;
|
||
|
|
||
|
output->recorder_frame_listener.notify = recorder_frame_notify;
|
||
|
wl_signal_add(&output->base.frame_signal,
|
||
|
&output->recorder_frame_listener);
|
||
|
|
||
|
weston_output_schedule_repaint(&output->base);
|
||
|
|
||
|
weston_log("[libva recorder] initialized\n");
|
||
|
} else {
|
||
11 years ago
|
recorder_destroy(output);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
#else
|
||
|
static void
|
||
10 years ago
|
recorder_binding(struct weston_keyboard *keyboard, uint32_t time, uint32_t key,
|
||
11 years ago
|
void *data)
|
||
|
{
|
||
|
weston_log("Compiled without libva support\n");
|
||
|
}
|
||
|
#endif
|
||
|
|
||
11 years ago
|
static void
|
||
10 years ago
|
switch_to_gl_renderer(struct drm_backend *b)
|
||
11 years ago
|
{
|
||
|
struct drm_output *output;
|
||
11 years ago
|
bool dmabuf_support_inited;
|
||
11 years ago
|
|
||
10 years ago
|
if (!b->use_pixman)
|
||
11 years ago
|
return;
|
||
|
|
||
11 years ago
|
dmabuf_support_inited = !!b->compositor->renderer->import_dmabuf;
|
||
|
|
||
11 years ago
|
weston_log("Switching to GL renderer\n");
|
||
|
|
||
10 years ago
|
b->gbm = create_gbm_device(b->drm.fd);
|
||
|
if (!b->gbm) {
|
||
11 years ago
|
weston_log("Failed to create gbm device. "
|
||
|
"Aborting renderer switch\n");
|
||
|
return;
|
||
|
}
|
||
|
|
||
10 years ago
|
wl_list_for_each(output, &b->compositor->output_list, base.link)
|
||
11 years ago
|
pixman_renderer_output_destroy(&output->base);
|
||
|
|
||
10 years ago
|
b->compositor->renderer->destroy(b->compositor);
|
||
11 years ago
|
|
||
10 years ago
|
if (drm_backend_create_gl_renderer(b) < 0) {
|
||
|
gbm_device_destroy(b->gbm);
|
||
11 years ago
|
weston_log("Failed to create GL renderer. Quitting.\n");
|
||
|
/* FIXME: we need a function to shutdown cleanly */
|
||
|
assert(0);
|
||
|
}
|
||
|
|
||
10 years ago
|
wl_list_for_each(output, &b->compositor->output_list, base.link)
|
||
|
drm_output_init_egl(output, b);
|
||
11 years ago
|
|
||
10 years ago
|
b->use_pixman = 0;
|
||
11 years ago
|
|
||
|
if (!dmabuf_support_inited && b->compositor->renderer->import_dmabuf) {
|
||
|
if (linux_dmabuf_setup(b->compositor) < 0)
|
||
|
weston_log("Error: initializing dmabuf "
|
||
|
"support failed.\n");
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
static void
|
||
10 years ago
|
renderer_switch_binding(struct weston_keyboard *keyboard, uint32_t time,
|
||
|
uint32_t key, void *data)
|
||
11 years ago
|
{
|
||
10 years ago
|
struct drm_backend *b =
|
||
|
(struct drm_backend *) keyboard->seat->compositor;
|
||
11 years ago
|
|
||
10 years ago
|
switch_to_gl_renderer(b);
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
static struct drm_backend *
|
||
|
drm_backend_create(struct weston_compositor *compositor,
|
||
9 years ago
|
struct weston_drm_backend_config *config)
|
||
15 years ago
|
{
|
||
10 years ago
|
struct drm_backend *b;
|
||
12 years ago
|
struct udev_device *drm_device;
|
||
15 years ago
|
struct wl_event_loop *loop;
|
||
12 years ago
|
const char *path;
|
||
9 years ago
|
const char *seat_id = default_seat;
|
||
15 years ago
|
|
||
13 years ago
|
weston_log("initializing drm backend\n");
|
||
|
|
||
10 years ago
|
b = zalloc(sizeof *b);
|
||
|
if (b == NULL)
|
||
15 years ago
|
return NULL;
|
||
13 years ago
|
|
||
10 years ago
|
/*
|
||
|
* KMS support for hardware planes cannot properly synchronize
|
||
|
* without nuclear page flip. Without nuclear/atomic, hw plane
|
||
|
* and cursor plane updates would either tear or cause extra
|
||
|
* waits for vblanks which means dropping the compositor framerate
|
||
9 years ago
|
* to a fraction. For cursors, it's not so bad, so they are
|
||
|
* enabled.
|
||
10 years ago
|
*
|
||
|
* These can be enabled again when nuclear/atomic support lands.
|
||
|
*/
|
||
10 years ago
|
b->sprites_are_broken = 1;
|
||
|
b->compositor = compositor;
|
||
9 years ago
|
b->use_pixman = config->use_pixman;
|
||
|
b->configure_output = config->configure_output;
|
||
|
b->use_current_mode = config->use_current_mode;
|
||
11 years ago
|
|
||
9 years ago
|
if (parse_gbm_format(config->gbm_format, GBM_FORMAT_XRGB8888, &b->gbm_format) < 0)
|
||
|
goto err_compositor;
|
||
11 years ago
|
|
||
9 years ago
|
if (config->seat_id)
|
||
|
seat_id = config->seat_id;
|
||
12 years ago
|
|
||
12 years ago
|
/* Check if we run drm-backend using weston-launch */
|
||
9 years ago
|
compositor->launcher = weston_launcher_connect(compositor, config->tty,
|
||
|
seat_id, true);
|
||
10 years ago
|
if (compositor->launcher == NULL) {
|
||
12 years ago
|
weston_log("fatal: drm backend should be run "
|
||
12 years ago
|
"using weston-launch binary or as root\n");
|
||
12 years ago
|
goto err_compositor;
|
||
|
}
|
||
|
|
||
10 years ago
|
b->udev = udev_new();
|
||
|
if (b->udev == NULL) {
|
||
13 years ago
|
weston_log("failed to initialize udev context\n");
|
||
11 years ago
|
goto err_launcher;
|
||
15 years ago
|
}
|
||
|
|
||
10 years ago
|
b->session_listener.notify = session_notify;
|
||
|
wl_signal_add(&compositor->session_signal, &b->session_listener);
|
||
13 years ago
|
|
||
9 years ago
|
drm_device = find_primary_gpu(b, seat_id);
|
||
14 years ago
|
if (drm_device == NULL) {
|
||
13 years ago
|
weston_log("no drm device found\n");
|
||
11 years ago
|
goto err_udev;
|
||
15 years ago
|
}
|
||
12 years ago
|
path = udev_device_get_syspath(drm_device);
|
||
15 years ago
|
|
||
10 years ago
|
if (init_drm(b, drm_device) < 0) {
|
||
12 years ago
|
weston_log("failed to initialize kms\n");
|
||
|
goto err_udev_dev;
|
||
|
}
|
||
|
|
||
10 years ago
|
if (b->use_pixman) {
|
||
|
if (init_pixman(b) < 0) {
|
||
12 years ago
|
weston_log("failed to initialize pixman renderer\n");
|
||
|
goto err_udev_dev;
|
||
|
}
|
||
|
} else {
|
||
10 years ago
|
if (init_egl(b) < 0) {
|
||
12 years ago
|
weston_log("failed to initialize egl\n");
|
||
|
goto err_udev_dev;
|
||
|
}
|
||
15 years ago
|
}
|
||
14 years ago
|
|
||
10 years ago
|
b->base.destroy = drm_destroy;
|
||
|
b->base.restore = drm_restore;
|
||
14 years ago
|
|
||
10 years ago
|
b->prev_state = WESTON_COMPOSITOR_ACTIVE;
|
||
13 years ago
|
|
||
9 years ago
|
weston_setup_vt_switch_bindings(compositor);
|
||
13 years ago
|
|
||
10 years ago
|
wl_list_init(&b->sprite_list);
|
||
|
create_sprites(b);
|
||
13 years ago
|
|
||
10 years ago
|
if (udev_input_init(&b->input,
|
||
9 years ago
|
compositor, b->udev, seat_id,
|
||
|
config->configure_device) < 0) {
|
||
11 years ago
|
weston_log("failed to create input devices\n");
|
||
|
goto err_sprite;
|
||
|
}
|
||
|
|
||
9 years ago
|
if (create_outputs(b, config->connector, drm_device) < 0) {
|
||
13 years ago
|
weston_log("failed to create output for %s\n", path);
|
||
11 years ago
|
goto err_udev_input;
|
||
15 years ago
|
}
|
||
|
|
||
11 years ago
|
/* A this point we have some idea of whether or not we have a working
|
||
|
* cursor plane. */
|
||
10 years ago
|
if (!b->cursors_are_broken)
|
||
|
compositor->capabilities |= WESTON_CAP_CURSOR_PLANE;
|
||
11 years ago
|
|
||
13 years ago
|
path = NULL;
|
||
|
|
||
10 years ago
|
loop = wl_display_get_event_loop(compositor->wl_display);
|
||
|
b->drm_source =
|
||
|
wl_event_loop_add_fd(loop, b->drm.fd,
|
||
|
WL_EVENT_READABLE, on_drm_input, b);
|
||
15 years ago
|
|
||
10 years ago
|
b->udev_monitor = udev_monitor_new_from_netlink(b->udev, "udev");
|
||
|
if (b->udev_monitor == NULL) {
|
||
13 years ago
|
weston_log("failed to intialize udev monitor\n");
|
||
13 years ago
|
goto err_drm_source;
|
||
14 years ago
|
}
|
||
10 years ago
|
udev_monitor_filter_add_match_subsystem_devtype(b->udev_monitor,
|
||
14 years ago
|
"drm", NULL);
|
||
10 years ago
|
b->udev_drm_source =
|
||
13 years ago
|
wl_event_loop_add_fd(loop,
|
||
10 years ago
|
udev_monitor_get_fd(b->udev_monitor),
|
||
|
WL_EVENT_READABLE, udev_drm_event, b);
|
||
14 years ago
|
|
||
10 years ago
|
if (udev_monitor_enable_receiving(b->udev_monitor) < 0) {
|
||
13 years ago
|
weston_log("failed to enable udev-monitor receiving\n");
|
||
13 years ago
|
goto err_udev_monitor;
|
||
14 years ago
|
}
|
||
|
|
||
13 years ago
|
udev_device_unref(drm_device);
|
||
|
|
||
10 years ago
|
weston_compositor_add_debug_binding(compositor, KEY_O,
|
||
|
planes_binding, b);
|
||
|
weston_compositor_add_debug_binding(compositor, KEY_C,
|
||
|
planes_binding, b);
|
||
|
weston_compositor_add_debug_binding(compositor, KEY_V,
|
||
|
planes_binding, b);
|
||
|
weston_compositor_add_debug_binding(compositor, KEY_Q,
|
||
|
recorder_binding, b);
|
||
|
weston_compositor_add_debug_binding(compositor, KEY_W,
|
||
|
renderer_switch_binding, b);
|
||
12 years ago
|
|
||
11 years ago
|
if (compositor->renderer->import_dmabuf) {
|
||
|
if (linux_dmabuf_setup(compositor) < 0)
|
||
|
weston_log("Error: initializing dmabuf "
|
||
|
"support failed.\n");
|
||
|
}
|
||
|
|
||
10 years ago
|
compositor->backend = &b->base;
|
||
11 years ago
|
|
||
10 years ago
|
return b;
|
||
13 years ago
|
|
||
|
err_udev_monitor:
|
||
10 years ago
|
wl_event_source_remove(b->udev_drm_source);
|
||
|
udev_monitor_unref(b->udev_monitor);
|
||
13 years ago
|
err_drm_source:
|
||
10 years ago
|
wl_event_source_remove(b->drm_source);
|
||
11 years ago
|
err_udev_input:
|
||
10 years ago
|
udev_input_destroy(&b->input);
|
||
12 years ago
|
err_sprite:
|
||
9 years ago
|
if (b->gbm)
|
||
|
gbm_device_destroy(b->gbm);
|
||
10 years ago
|
destroy_sprites(b);
|
||
13 years ago
|
err_udev_dev:
|
||
|
udev_device_unref(drm_device);
|
||
11 years ago
|
err_launcher:
|
||
10 years ago
|
weston_launcher_destroy(compositor->launcher);
|
||
13 years ago
|
err_udev:
|
||
10 years ago
|
udev_unref(b->udev);
|
||
13 years ago
|
err_compositor:
|
||
10 years ago
|
weston_compositor_shutdown(compositor);
|
||
|
free(b);
|
||
13 years ago
|
return NULL;
|
||
15 years ago
|
}
|
||
14 years ago
|
|
||
9 years ago
|
static void
|
||
|
config_init_to_defaults(struct weston_drm_backend_config *config)
|
||
|
{
|
||
|
}
|
||
|
|
||
10 years ago
|
WL_EXPORT int
|
||
9 years ago
|
backend_init(struct weston_compositor *compositor,
|
||
9 years ago
|
struct weston_backend_config *config_base)
|
||
14 years ago
|
{
|
||
10 years ago
|
struct drm_backend *b;
|
||
9 years ago
|
struct weston_drm_backend_config config = {{ 0, }};
|
||
13 years ago
|
|
||
9 years ago
|
if (config_base == NULL ||
|
||
|
config_base->struct_version != WESTON_DRM_BACKEND_CONFIG_VERSION ||
|
||
|
config_base->struct_size > sizeof(struct weston_drm_backend_config)) {
|
||
|
weston_log("drm backend config structure is invalid\n");
|
||
|
return -1;
|
||
|
}
|
||
11 years ago
|
|
||
9 years ago
|
config_init_to_defaults(&config);
|
||
|
memcpy(&config, config_base, config_base->struct_size);
|
||
14 years ago
|
|
||
9 years ago
|
b = drm_backend_create(compositor, &config);
|
||
10 years ago
|
if (b == NULL)
|
||
|
return -1;
|
||
9 years ago
|
|
||
10 years ago
|
return 0;
|
||
14 years ago
|
}
|