render/pixel-format: add get_drm_{format,modifier}_description

We need to log information about DRM formats/modifiers in quite a
few places. Make this easier with these helper functions.
This commit is contained in:
Simon Ser 2023-05-10 14:27:53 +02:00
parent 18139f4d87
commit eb48d5e9e6
6 changed files with 68 additions and 32 deletions

View file

@ -1,6 +1,9 @@
#include <assert.h>
#include <drm_fourcc.h>
#include <stdio.h>
#include <stdlib.h>
#include <wlr/util/log.h>
#include <xf86drm.h>
#include "render/pixel_format.h"
static const struct wlr_pixel_format_info pixel_format_info[] = {
@ -211,3 +214,38 @@ bool pixel_format_info_check_stride(const struct wlr_pixel_format_info *fmt,
}
return true;
}
static char *format_str(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
int len = vsnprintf(NULL, 0, fmt, args);
va_end(args);
if (len < 0) {
return NULL;
}
char *str = malloc(len + 1);
if (str == NULL) {
return NULL;
}
va_start(args, fmt);
vsnprintf(str, len + 1, fmt, args);
va_end(args);
return str;
}
char *get_drm_format_description(uint32_t format) {
char *name = drmGetFormatName(format);
char *str = format_str("%s (0x%08"PRIX32")", name ? name : "<unknown>", format);
free(name);
return str;
}
char *get_drm_modifier_description(uint64_t modifier) {
char *name = drmGetFormatModifierName(modifier);
char *str = format_str("%s (0x%016"PRIX64")", name ? name : "<unknown>", modifier);
free(name);
return str;
}