Commit a42992a4 authored by Emil Velikov's avatar Emil Velikov Committed by Adam Jackson
Browse files

dri3: rework format/modifier caching

Cut down the unnecessary malloc/memcpy/free by utilising the explicit
copy provided by the client.

But above all: do so, after ensuring we get valid data from the

Fixes: cef12efc

 ("glamor: Implement GetSupportedModifiers")
Cc: Louis-Francis Ratté-Boulianne <>
Cc: Daniel Stone <>
Reviewed-by: Adam Jackson's avatarAdam Jackson <>
Signed-off-by: Emil Velikov's avatarEmil Velikov <>
parent 71a069fd
......@@ -159,8 +159,10 @@ cache_formats_and_modifiers(ScreenPtr screen)
dri3_screen_priv_ptr ds = dri3_screen_priv(screen);
const dri3_screen_info_rec *info = ds->info;
CARD32 *formats = NULL;
CARD64 *modifiers = NULL;
CARD32 num_formats;
CARD32 *formats;
CARD32 num_modifiers;
CARD64 *modifiers;
int i;
if (ds->formats_cached)
......@@ -176,34 +178,36 @@ cache_formats_and_modifiers(ScreenPtr screen)
return Success;
(*info->get_formats) (screen, &ds->num_formats, &formats);
ds->formats = calloc(ds->num_formats, sizeof(dri3_dmabuf_format_rec));
if (!info->get_formats(screen, &num_formats, &formats))
return BadAlloc;
if (!num_formats) {
ds->num_formats = 0;
ds->formats_cached = TRUE;
return Success;
ds->formats = calloc(num_formats, sizeof(dri3_dmabuf_format_rec));
if (!ds->formats)
return BadAlloc;
for (i = 0; i < ds->num_formats; i++) {
for (i = 0; i < num_formats; i++) {
dri3_dmabuf_format_ptr iter = &ds->formats[i];
if (!info->get_modifiers(screen, formats[i],
if (!num_modifiers)
iter->format = formats[i];
(*info->get_modifiers) (screen, formats[i],
iter->modifiers = malloc(iter->num_modifiers * sizeof(CARD64));
if (iter->modifiers == NULL)
goto error;
memcpy(iter->modifiers, modifiers,
iter->num_modifiers * sizeof(CARD64));
goto done;
iter->num_modifiers = 0;
iter->num_modifiers = num_modifiers;
iter->modifiers = modifiers;
ds->num_formats = i;
ds->formats_cached = TRUE;
return Success;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment