kmsvnc/drm.c

392 lines
13 KiB
C
Raw Normal View History

2023-04-27 19:35:18 +08:00
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include "drm.h"
2023-04-29 02:05:13 +08:00
#include "va.h"
2023-04-27 19:35:18 +08:00
extern struct kmsvnc_data *kmsvnc;
static void convert_copy(const char *in, int width, int height, char *buff) {
memcpy(buff, in, width * height * 4);
}
2023-04-29 02:05:13 +08:00
static void convert_vaapi(const char *in, int width, int height, char *buff) {
va_hwframe_to_vaapi();
}
2023-04-27 19:35:18 +08:00
static void convert_bgrx_to_rgb(const char *in, int width, int height, char *buff)
{
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
buff[(y * width + x) * 4] = in[(y * width + x) * 4 + 2];
buff[(y * width + x) * 4 + 1] = in[(y * width + x) * 4 + 1];
buff[(y * width + x) * 4 + 2] = in[(y * width + x) * 4];
}
}
}
static char *kms_convert_buf = NULL;
static size_t kms_convert_buf_len = 0;
static char *kms_cpy_tmp_buf = NULL;
static size_t kms_cpy_tmp_buf_len = 0;
2023-04-27 23:42:41 +08:00
static inline void convert_x_tiled(const int tilex, const int tiley, const char *in, int width, int height, char *buff)
2023-04-27 19:35:18 +08:00
{
2023-04-27 23:42:41 +08:00
if (width % tilex)
2023-04-27 19:35:18 +08:00
{
return;
}
2023-04-27 23:42:41 +08:00
if (height % tiley)
2023-04-27 19:35:18 +08:00
{
2023-04-27 23:42:41 +08:00
int sno = (width / tilex) + (height / tiley) * (width / tilex);
int ord = (width % tilex) + (height % tiley) * tilex;
int max_offset = sno * tilex * tiley + ord;
2023-04-27 19:35:18 +08:00
if (kms_cpy_tmp_buf_len < max_offset * 4 + 4)
{
if (kms_cpy_tmp_buf)
free(kms_convert_buf);
kms_cpy_tmp_buf = malloc(max_offset * 4 + 4);
kms_cpy_tmp_buf_len = max_offset * 4 + 4;
}
memcpy(kms_cpy_tmp_buf, in, max_offset * 4 + 4);
in = (const char *)kms_cpy_tmp_buf;
}
if (kms_convert_buf_len < width * height * 4)
{
if (kms_convert_buf)
free(kms_convert_buf);
kms_convert_buf = malloc(width * height * 4);
kms_convert_buf_len = width * height * 4;
}
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
2023-04-27 23:42:41 +08:00
int sno = (x / tilex) + (y / tiley) * (width / tilex);
int ord = (x % tilex) + (y % tiley) * tilex;
int offset = sno * tilex * tiley + ord;
2023-04-27 19:35:18 +08:00
memcpy(kms_convert_buf + (x + y * width) * 4, in + offset * 4, 4);
}
}
convert_bgrx_to_rgb(kms_convert_buf, width, height, buff);
}
2023-04-27 23:42:41 +08:00
void convert_nvidia_x_tiled_kmsbuf(const char *in, int width, int height, char *buff)
2023-04-27 19:35:18 +08:00
{
2023-04-27 23:42:41 +08:00
convert_x_tiled(16, 128, in, width, height, buff);
2023-04-27 19:35:18 +08:00
}
2023-04-27 23:42:41 +08:00
void convert_intel_x_tiled_kmsbuf(const char *in, int width, int height, char *buff)
2023-04-27 19:35:18 +08:00
{
2023-04-27 23:42:41 +08:00
convert_x_tiled(128, 8, in, width, height, buff);
2023-04-27 19:35:18 +08:00
}
static inline void drm_sync(int drmfd, uint64_t flags)
{
struct dma_buf_sync sync = {
.flags = flags,
};
2023-04-29 02:05:13 +08:00
DRM_R_IOCTL_MAY(drmfd, DMA_BUF_IOCTL_SYNC, &sync);
2023-04-27 19:35:18 +08:00
}
void drm_sync_start(int drmfd)
{
drm_sync(drmfd, DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ);
}
void drm_sync_end(int drmfd)
{
drm_sync(drmfd, DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ);
}
void drm_sync_noop(int drmfd)
{
}
void drm_cleanup() {
if (kmsvnc->drm) {
if (kmsvnc->drm->drm_ver) {
drmFreeVersion(kmsvnc->drm->drm_ver);
kmsvnc->drm->drm_ver = NULL;
}
if (kmsvnc->drm->plane) {
drmModeFreePlane(kmsvnc->drm->plane);
kmsvnc->drm->plane = NULL;
}
if (kmsvnc->drm->mfb) {
2023-04-27 23:42:41 +08:00
drmModeFreeFB2(kmsvnc->drm->mfb);
2023-04-27 19:35:18 +08:00
kmsvnc->drm->mfb = NULL;
}
if (kmsvnc->drm->mapped) {
munmap(kmsvnc->drm->mapped, kmsvnc->drm->mmap_size);
kmsvnc->drm->mapped = NULL;
}
if (kmsvnc->drm->prime_fd > 0) {
close(kmsvnc->drm->prime_fd);
kmsvnc->drm->prime_fd = 0;
}
if (kmsvnc->drm->drm_fd > 0) {
close(kmsvnc->drm->drm_fd);
kmsvnc->drm->drm_fd = 0;
}
2023-04-28 14:13:46 +08:00
if (kmsvnc->drm->plane_res) {
drmModeFreePlaneResources(kmsvnc->drm->plane_res);
kmsvnc->drm->plane_res = NULL;
}
2023-04-27 19:35:18 +08:00
free(kmsvnc->drm);
kmsvnc->drm = NULL;
}
}
int drm_open() {
struct kmsvnc_drm_data *drm = malloc(sizeof(struct kmsvnc_drm_data));
memset(drm, 0, sizeof(struct kmsvnc_drm_data));
kmsvnc->drm = drm;
drm->drm_fd = open(kmsvnc->card, O_RDONLY);
if (drm->drm_fd < 0)
{
2023-04-29 02:05:13 +08:00
KMSVNC_FATAL("card %s open failed: %s\n", kmsvnc->card, strerror(errno));
2023-04-27 19:35:18 +08:00
}
drm->drm_ver = drmGetVersion(drm->drm_fd);
2023-04-27 23:42:41 +08:00
printf("drm driver is %s\n", drm->drm_ver->name);
2023-04-27 19:35:18 +08:00
int err = drmSetClientCap(drm->drm_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
if (err < 0)
{
perror("Failed to set universal planes capability: primary planes will not be usable");
}
2023-04-28 14:13:46 +08:00
if (kmsvnc->source_plane > 0)
2023-04-27 19:35:18 +08:00
{
2023-04-28 14:13:46 +08:00
drm->plane = drmModeGetPlane(drm->drm_fd, kmsvnc->source_plane);
2023-04-27 19:35:18 +08:00
if (!drm->plane)
2023-04-29 02:05:13 +08:00
KMSVNC_FATAL("Failed to get plane %d: %s\n", kmsvnc->source_plane, strerror(errno));
2023-04-27 19:35:18 +08:00
if (drm->plane->fb_id == 0)
2023-04-28 14:13:46 +08:00
fprintf(stderr, "Place %d does not have an attached framebuffer\n", kmsvnc->source_plane);
2023-04-27 19:35:18 +08:00
}
else
{
drm->plane_res = drmModeGetPlaneResources(drm->drm_fd);
if (!drm->plane_res)
2023-04-29 02:05:13 +08:00
KMSVNC_FATAL("Failed to get plane resources: %s\n", strerror(errno));
2023-04-27 19:35:18 +08:00
int i;
for (i = 0; i < drm->plane_res->count_planes; i++)
{
drm->plane = drmModeGetPlane(drm->drm_fd, drm->plane_res->planes[i]);
if (!drm->plane)
{
fprintf(stderr, "Failed to get plane %u: %s\n", drm->plane_res->planes[i], strerror(errno));
continue;
}
printf("Plane %u CRTC %u FB %u\n", drm->plane->plane_id, drm->plane->crtc_id, drm->plane->fb_id);
2023-04-28 14:13:46 +08:00
if ((kmsvnc->source_crtc != 0 && drm->plane->crtc_id != kmsvnc->source_crtc) || drm->plane->fb_id == 0)
2023-04-27 19:35:18 +08:00
{
// Either not connected to the target source CRTC
// or not active.
drmModeFreePlane(drm->plane);
drm->plane = NULL;
continue;
}
break;
}
if (i == drm->plane_res->count_planes)
{
2023-04-28 14:13:46 +08:00
if (kmsvnc->source_crtc != 0)
2023-04-27 19:35:18 +08:00
{
2023-04-29 02:05:13 +08:00
KMSVNC_FATAL("No usable planes found on CRTC %d\n", kmsvnc->source_crtc);
2023-04-27 19:35:18 +08:00
}
else
{
2023-04-29 02:05:13 +08:00
KMSVNC_FATAL("No usable planes found\n");
2023-04-27 19:35:18 +08:00
}
}
printf("Using plane %u to locate framebuffers\n", drm->plane->plane_id);
}
uint32_t plane_id = drm->plane->plane_id;
2023-04-27 23:42:41 +08:00
drm->mfb = drmModeGetFB2(drm->drm_fd, drm->plane->fb_id);
if (!drm->mfb) {
2023-04-29 02:05:13 +08:00
KMSVNC_FATAL("Failed to get framebuffer %u: %s\n", drm->plane->fb_id, strerror(errno));
2023-04-27 19:35:18 +08:00
}
2023-04-27 23:42:41 +08:00
printf("Template framebuffer is %u: %ux%u fourcc:%u mod:%u flags:%u\n", drm->mfb->fb_id, drm->mfb->width, drm->mfb->height, drm->mfb->pixel_format, drm->mfb->modifier, drm->mfb->flags);
printf("handles %u %u %u %u\n", drm->mfb->handles[0], drm->mfb->handles[1], drm->mfb->handles[2], drm->mfb->handles[3]);
printf("offsets %u %u %u %u\n", drm->mfb->offsets[0], drm->mfb->offsets[1], drm->mfb->offsets[2], drm->mfb->offsets[3]);
printf("pitches %u %u %u %u\n", drm->mfb->pitches[0], drm->mfb->pitches[1], drm->mfb->pitches[2], drm->mfb->pitches[3]);
printf("format %s, modifier %s:%s\n", drmGetFormatName(drm->mfb->pixel_format), drmGetFormatModifierVendor(drm->mfb->modifier), drmGetFormatModifierName(drm->mfb->modifier));
if (
drm->mfb->pixel_format != KMSVNC_FOURCC_TO_INT('X', 'R', '2', '4') &&
drm->mfb->pixel_format != KMSVNC_FOURCC_TO_INT('A', 'R', '2', '4')
)
2023-04-27 19:35:18 +08:00
{
2023-04-29 02:05:13 +08:00
KMSVNC_FATAL("Unsupported pixfmt\n");
2023-04-27 19:35:18 +08:00
}
2023-04-27 23:42:41 +08:00
if (!drm->mfb->handles[0])
2023-04-27 19:35:18 +08:00
{
2023-04-29 02:05:13 +08:00
KMSVNC_FATAL("No handle set on framebuffer: maybe you need some additional capabilities?\n");
2023-04-27 19:35:18 +08:00
}
drm->mmap_fd = drm->drm_fd;
drm->mmap_size = drm->mfb->width * drm->mfb->height * BYTES_PER_PIXEL;
drm->funcs = malloc(sizeof(struct kmsvnc_drm_funcs));
drm->funcs->convert = convert_bgrx_to_rgb;
drm->funcs->sync_start = drm_sync_noop;
drm->funcs->sync_end = drm_sync_noop;
if (drm_vendors()) return 1;
return 0;
}
static int drm_kmsbuf_prime() {
struct kmsvnc_drm_data *drm = kmsvnc->drm;
2023-04-27 23:42:41 +08:00
int err = drmPrimeHandleToFD(drm->drm_fd, drm->mfb->handles[0], O_RDWR, &drm->prime_fd);
2023-04-27 19:35:18 +08:00
if (err < 0 || drm->prime_fd < 0)
{
2023-04-29 02:05:13 +08:00
KMSVNC_FATAL("Failed to get PRIME fd from framebuffer handle");
2023-04-27 19:35:18 +08:00
}
drm->funcs->sync_start = &drm_sync_start;
drm->funcs->sync_end = &drm_sync_end;
drm->mmap_fd = drm->prime_fd;
return 0;
}
2023-04-29 02:05:13 +08:00
static int drm_kmsbuf_prime_vaapi() {
struct kmsvnc_drm_data *drm = kmsvnc->drm;
int err = drmPrimeHandleToFD(drm->drm_fd, drm->mfb->handles[0], O_RDWR, &drm->prime_fd);
if (err < 0 || drm->prime_fd < 0)
{
KMSVNC_FATAL("Failed to get PRIME fd from framebuffer handle");
}
if (va_init()) return 1;
drm->funcs->sync_start = &drm_sync_start;
drm->funcs->sync_end = &drm_sync_end;
drm->mmap_fd = drm->prime_fd;
2023-04-29 02:21:39 +08:00
drm->mapped = kmsvnc->va->imgbuf;
2023-04-29 02:05:13 +08:00
return 0;
}
2023-04-27 19:35:18 +08:00
static int drm_kmsbuf_dumb() {
struct kmsvnc_drm_data *drm = kmsvnc->drm;
struct drm_gem_flink flink;
2023-04-27 23:42:41 +08:00
flink.handle = drm->mfb->handles[0];
2023-04-27 19:35:18 +08:00
DRM_IOCTL_MUST(drm->drm_fd, DRM_IOCTL_GEM_FLINK, &flink);
struct drm_gem_open open_arg;
open_arg.name = flink.name;
DRM_IOCTL_MUST(drm->drm_fd, DRM_IOCTL_GEM_OPEN, &open_arg);
struct drm_mode_map_dumb mreq;
memset(&mreq, 0, sizeof(mreq));
mreq.handle = open_arg.handle;
DRM_IOCTL_MUST(drm->drm_fd, DRM_IOCTL_MODE_MAP_DUMB, &mreq);
drm->mmap_size = open_arg.size;
drm->mmap_offset = mreq.offset;
return 0;
}
int drm_vendors() {
struct kmsvnc_drm_data *drm = kmsvnc->drm;
char *driver_name;
if (kmsvnc->force_driver) {
printf("using %s instead of %s\n", kmsvnc->force_driver, drm->drm_ver->name);
driver_name = kmsvnc->force_driver;
}
else {
driver_name = drm->drm_ver->name;
}
if (strcmp(driver_name, "i915") == 0)
{
2023-04-27 23:42:41 +08:00
drm->funcs->convert = &convert_intel_x_tiled_kmsbuf;
2023-04-29 02:05:13 +08:00
drm->funcs->convert = &convert_vaapi;
if (drm_kmsbuf_prime_vaapi()) return 1;
2023-04-27 19:35:18 +08:00
}
else if (strcmp(driver_name, "amdgpu") == 0)
{
struct drm_gem_flink flink;
2023-04-27 23:42:41 +08:00
flink.handle = drm->mfb->handles[0];
2023-04-27 19:35:18 +08:00
DRM_IOCTL_MUST(drm->drm_fd, DRM_IOCTL_GEM_FLINK, &flink);
struct drm_gem_open open_arg;
open_arg.name = flink.name;
DRM_IOCTL_MUST(drm->drm_fd, DRM_IOCTL_GEM_OPEN, &open_arg);
union drm_amdgpu_gem_mmap mmap_arg;
memset(&mmap_arg, 0, sizeof(mmap_arg));
mmap_arg.in.handle = open_arg.handle;
DRM_IOCTL_MUST(drm->drm_fd, DRM_IOCTL_AMDGPU_GEM_MMAP, &mmap_arg);
drm->mmap_size = open_arg.size;
drm->mmap_offset = mmap_arg.out.addr_ptr;
}
else if (strcmp(driver_name, "nvidia-drm") == 0)
{
// quirky and slow
2023-04-27 23:42:41 +08:00
drm->funcs->convert = &convert_nvidia_x_tiled_kmsbuf;
2023-04-27 19:35:18 +08:00
if (drm_kmsbuf_dumb()) return 1;
}
else if (strcmp(driver_name, "vmwgfx") == 0 ||
strcmp(driver_name, "vboxvideo") == 0 ||
strcmp(driver_name, "virtio_gpu") == 0
)
{
// virgl does not work
if (drm_kmsbuf_dumb()) return 1;
}
else if (strcmp(driver_name, "test-prime") == 0)
{
if (drm_kmsbuf_prime()) return 1;
}
else if (strcmp(driver_name, "test-map-dumb") == 0)
{
if (drm_kmsbuf_dumb()) return 1;
}
2023-04-27 19:51:16 +08:00
else if (strcmp(driver_name, "test-i915-gem") == 0)
{
struct drm_gem_flink flink;
2023-04-27 23:42:41 +08:00
flink.handle = drm->mfb->handles[0];
2023-04-27 19:51:16 +08:00
DRM_IOCTL_MUST(drm->drm_fd, DRM_IOCTL_GEM_FLINK, &flink);
struct drm_gem_open open_arg;
open_arg.name = flink.name;
DRM_IOCTL_MUST(drm->drm_fd, DRM_IOCTL_GEM_OPEN, &open_arg);
struct drm_i915_gem_mmap_gtt mmap_arg;
mmap_arg.handle = open_arg.handle;
DRM_IOCTL_MUST(drm->drm_fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &mmap_arg);
drm->mmap_size = open_arg.size;
drm->mmap_offset = mmap_arg.offset;
}
2023-04-27 19:35:18 +08:00
else
{
fprintf(stderr, "Untested drm driver, use at your own risk!\n");
if (drm_kmsbuf_dumb()) return 1;
}
if (!drm->mapped)
{
printf("mapping with size = %d, offset = %d, fd = %d\n", drm->mmap_size, drm->mmap_offset, drm->mmap_fd);
drm->mapped = mmap(NULL, drm->mmap_size, PROT_READ, MAP_SHARED, drm->mmap_fd, drm->mmap_offset);
if (drm->mapped == MAP_FAILED)
{
2023-04-29 02:05:13 +08:00
KMSVNC_FATAL("Failed to mmap: %s\n", strerror(errno));
2023-04-27 19:35:18 +08:00
}
}
return 0;
}