Skip to content

Rework device names #682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions va/android/va_android.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2007 Intel Corporation. All Rights Reserved.
* Copyright (c) 2023 Emil Velikov
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
Expand Down Expand Up @@ -61,38 +62,38 @@ static void va_DisplayContextDestroy(
free(pDisplayContext);
}

static VAStatus va_DisplayContextGetNumCandidates(
VADisplayContextP pDisplayContext,
int *num_candidates
static VAStatus va_DisplayContextConnect(
VADisplayContextP pDisplayContext
)
{
VADriverContextP const ctx = pDisplayContext->pDriverContext;
struct drm_state * drm_state = (struct drm_state *)ctx->drm_state;
struct drm_state * const drm_state = (struct drm_state *)ctx->drm_state;

memset(drm_state, 0, sizeof(*drm_state));
drm_state->fd = open(DEVICE_NAME, O_RDWR | O_CLOEXEC);

if (drm_state->fd < 0) {
fprintf(stderr, "Cannot open DRM device '%s': %d, %s\n",
DEVICE_NAME, errno, strerror(errno));
return VA_STATUS_ERROR_UNKNOWN;
}
drm_state->auth_type = VA_DRM_AUTH_CUSTOM;
return VA_DRM_GetNumCandidates(ctx, num_candidates);
return VA_STATUS_SUCCESS;
}

static VAStatus va_DisplayContextGetDriverNameByIndex(
static VAStatus
va_DisplayContextGetDriverNames(
VADisplayContextP pDisplayContext,
char **driver_name,
int candidate_index
char **drivers,
unsigned *num_drivers
)
{
VADriverContextP const ctx = pDisplayContext->pDriverContext;
VAStatus status = va_DisplayContextConnect(pDisplayContext);
if (status != VA_STATUS_SUCCESS)
return status;

return VA_DRM_GetDriverName(ctx, driver_name, candidate_index);
return VA_DRM_GetDriverNames(ctx, drivers, num_drivers);
}


VADisplay vaGetDisplay(
void *native_dpy /* implementation specific */
)
Expand All @@ -109,8 +110,7 @@ VADisplay vaGetDisplay(
return NULL;

pDisplayContext->vaDestroy = va_DisplayContextDestroy;
pDisplayContext->vaGetDriverNameByIndex = va_DisplayContextGetDriverNameByIndex;
pDisplayContext->vaGetNumCandidates = va_DisplayContextGetNumCandidates;
pDisplayContext->vaGetDriverNames = va_DisplayContextGetDriverNames;

pDriverContext = va_newDriverContext(pDisplayContext);
if (!pDriverContext) {
Expand Down
29 changes: 15 additions & 14 deletions va/drm/va_drm.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2012 Intel Corporation. All Rights Reserved.
* Copyright (c) 2023 Emil Velikov
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
Expand Down Expand Up @@ -41,19 +42,17 @@ va_DisplayContextDestroy(VADisplayContextP pDisplayContext)
free(pDisplayContext->pDriverContext);
free(pDisplayContext);
}
static VAStatus va_DisplayContextGetNumCandidates(
VADisplayContextP pDisplayContext,
int *num_candidates


static VAStatus va_DisplayContextConnect(
VADisplayContextP pDisplayContext
)
{
VADriverContextP const ctx = pDisplayContext->pDriverContext;
struct drm_state * const drm_state = ctx->drm_state;
VAStatus status = VA_STATUS_SUCCESS;
drm_magic_t magic;
int ret;
status = VA_DRM_GetNumCandidates(ctx, num_candidates);
if (status != VA_STATUS_SUCCESS)
return status;

/* Authentication is only needed for a legacy DRM device */
if (ctx->display_type != VA_DISPLAY_DRM_RENDERNODES) {
ret = drmGetMagic(drm_state->fd, &magic);
Expand All @@ -68,17 +67,20 @@ static VAStatus va_DisplayContextGetNumCandidates(
return VA_STATUS_SUCCESS;
}


static VAStatus
va_DisplayContextGetDriverNameByIndex(
va_DisplayContextGetDriverNames(
VADisplayContextP pDisplayContext,
char **driver_name_ptr,
int candidate_index
char **drivers,
unsigned *num_drivers
)
{

VADriverContextP const ctx = pDisplayContext->pDriverContext;
VAStatus status = va_DisplayContextConnect(pDisplayContext);
if (status != VA_STATUS_SUCCESS)
return status;

return VA_DRM_GetDriverName(ctx, driver_name_ptr, candidate_index);
return VA_DRM_GetDriverNames(ctx, drivers, num_drivers);
}

VADisplay
Expand All @@ -104,8 +106,7 @@ vaGetDisplayDRM(int fd)
goto error;

pDisplayContext->vaDestroy = va_DisplayContextDestroy;
pDisplayContext->vaGetNumCandidates = va_DisplayContextGetNumCandidates;
pDisplayContext->vaGetDriverNameByIndex = va_DisplayContextGetDriverNameByIndex;
pDisplayContext->vaGetDriverNames = va_DisplayContextGetDriverNames;

pDriverContext = va_newDriverContext(pDisplayContext);
if (!pDriverContext)
Expand Down
132 changes: 47 additions & 85 deletions va/drm/va_drm_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* va_drm_utils.c - VA/DRM Utilities
*
* Copyright (c) 2012 Intel Corporation. All Rights Reserved.
* Copyright (c) 2023 Emil Velikov
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
Expand Down Expand Up @@ -31,22 +32,7 @@
#include "va_drm_utils.h"
#include "va_drmcommon.h"

struct driver_name_map {
const char *key;
const char *name;
};

static const struct driver_name_map g_driver_name_map[] = {
{ "i915", "iHD" }, // Intel Media driver
{ "i915", "i965" }, // Intel OTC GenX driver
{ "pvrsrvkm", "pvr" }, // Intel UMG PVR driver
{ "radeon", "r600" }, // Mesa Gallium driver
{ "radeon", "radeonsi" }, // Mesa Gallium driver
{ "amdgpu", "radeonsi" }, // Mesa Gallium driver
{ "WSL", "d3d12" }, // Mesa Gallium driver
{ "nvidia-drm", "nvidia" }, // NVIDIA driver
{ NULL, NULL }
};
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))

static char *
va_DRM_GetDrmDriverName(int fd)
Expand All @@ -63,88 +49,64 @@ va_DRM_GetDrmDriverName(int fd)
return driver_name;
}

/* Returns the VA driver candidate num for the active display*/
/* Returns the VA driver names and how many they are, for the active display */
VAStatus
VA_DRM_GetNumCandidates(VADriverContextP ctx, int * num_candidates)
VA_DRM_GetDriverNames(VADriverContextP ctx, char **drivers, unsigned *num_drivers)
{
struct drm_state * const drm_state = ctx->drm_state;
int count = 0;
const struct driver_name_map *m = NULL;
char *driver_name;

if (!drm_state || drm_state->fd < 0)
return VA_STATUS_ERROR_INVALID_DISPLAY;

driver_name = va_DRM_GetDrmDriverName(drm_state->fd);
if (!driver_name)
return VA_STATUS_ERROR_UNKNOWN;

for (m = g_driver_name_map; m->key != NULL; m++) {
if (strcmp(m->key, driver_name) == 0) {
count ++;
}
}

free(driver_name);

/*
* If the drm driver name does not have a mapped vaapi driver name, then
* assume they have the same name.
*/
if (count == 0)
count = 1;

*num_candidates = count;
return VA_STATUS_SUCCESS;
}

/* Returns the VA driver name for the active display */
VAStatus
VA_DRM_GetDriverName(VADriverContextP ctx, char **driver_name_ptr, int candidate_index)
{
struct drm_state * const drm_state = ctx->drm_state;
const struct driver_name_map *m;
int current_index = 0;

*driver_name_ptr = NULL;

if (!drm_state || drm_state->fd < 0)
return VA_STATUS_ERROR_INVALID_DISPLAY;

*driver_name_ptr = va_DRM_GetDrmDriverName(drm_state->fd);

if (!*driver_name_ptr)
#define MAX_NAMES 2 // Adjust if needed

static const struct {
const char * const drm_driver;
const char * const va_driver[MAX_NAMES];
} map[] = {
{ "i915", { "iHD", "i965" } }, // Intel Media and OTC GenX
{ "pvrsrvkm", { "pvr" } }, // Intel UMG PVR
{ "radeon", { "r600", "radeonsi" } }, // Mesa Gallium
{ "amdgpu", { "radeonsi" } }, // Mesa Gallium
{ "WSL", { "d3d12" } }, // Mesa Gallium
{ "nvidia-drm", { "nvidia" } }, // Unofficial NVIDIA
};

const struct drm_state * const drm_state = ctx->drm_state;
char *drm_driver;
unsigned count = 0;

drm_driver = va_DRM_GetDrmDriverName(drm_state->fd);
if (!drm_driver)
return VA_STATUS_ERROR_UNKNOWN;

/* Map vgem to WSL2 for Windows subsystem for linux */
struct utsname sysinfo = {};
if (!strncmp(*driver_name_ptr, "vgem", 4) && uname(&sysinfo) >= 0 &&
if (!strncmp(drm_driver, "vgem", 4) && uname(&sysinfo) >= 0 &&
strstr(sysinfo.release, "WSL")) {
free(*driver_name_ptr);
*driver_name_ptr = strdup("WSL");
free(drm_driver);
drm_driver = strdup("WSL");
if (!drm_driver)
return VA_STATUS_ERROR_UNKNOWN;
}

for (m = g_driver_name_map; m->key != NULL; m++) {
if (strcmp(m->key, *driver_name_ptr) == 0) {
if (current_index == candidate_index) {
break;
for (unsigned i = 0; i < ARRAY_SIZE(map); i++) {
if (strcmp(map[i].drm_driver, drm_driver) == 0) {
const char * const *va_drivers = map[i].va_driver;

while (va_drivers[count]) {
if (count < MAX_NAMES && count < *num_drivers)
drivers[count] = strdup(va_drivers[count]);
count++;
}
current_index ++;
break;
}
}

/*
* If the drm driver name does not have a mapped vaapi driver name, then
* assume they have the same name.
*/
if (!m->name)
return VA_STATUS_SUCCESS;

/* Use the mapped vaapi driver name */
free(*driver_name_ptr);
*driver_name_ptr = strdup(m->name);
if (!*driver_name_ptr)
return VA_STATUS_ERROR_ALLOCATION_FAILED;
/* Fallback to the drm driver, if there's no va equivalent in the map. */
if (!count) {
drivers[count] = drm_driver;
count++;
} else {
free(drm_driver);
}

*num_drivers = count;

return VA_STATUS_SUCCESS;
}
24 changes: 2 additions & 22 deletions va/drm/va_drm_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,10 @@
#ifdef __cplusplus
extern "C" {
#endif

DLL_HIDDEN
VAStatus
VA_DRM_GetNumCandidates(VADriverContextP ctx, int * num_candidates);
/**
* \brief Returns the VA driver name for the active display.
*
* This functions returns a newly allocated buffer in @driver_name_ptr that
* contains the VA driver name for the active display. Active display means
* the display obtained with any of the vaGetDisplay*() functions.
*
* The VADriverContext.drm_state structure must be valid, i.e. allocated
* and containing an open DRM connection descriptor. The DRM connection
* does *not* need to be authenticated as it only performs a call to
* drmGetVersion().
*
* @param[in] ctx the pointer to a VADriverContext
* @param[out] driver_name_ptr the newly allocated buffer containing
* the VA driver name
* @return VA_STATUS_SUCCESS if operation is successful, or another
* #VAStatus value otherwise.
*/
DLL_HIDDEN
VAStatus
VA_DRM_GetDriverName(VADriverContextP ctx, char **driver_name_ptr, int candidate_index);
VA_DRM_GetDriverNames(VADriverContextP ctx, char **drivers, unsigned *num_drivers);

/**@}*/

Expand Down
Loading