AtlatestRepositorysigil-ui
1
/* Included by wayland.c: protocol objects never escape the owning connection. */2
#define DMABUF_TABLE_BYTES (1024u * 1024)3
#define DMABUF_TRANCHES 64u4
#define DMABUF_FORMATS 4096u6
typedef struct { uint32_t fourcc; uint64_t modifier; } DmabufFormat;7
typedef struct {8
dev_t device;9
uint32_t flags;10
size_t count;11
DmabufFormat *formats;12
} DmabufTranche;14
struct SuiWaylandDmabufFeedback {15
unsigned references;16
uint64_t generation;17
dev_t main_device;18
size_t count;19
DmabufTranche tranches[DMABUF_TRANCHES];20
};22
struct DmabufFeedbackState {23
struct zwp_linux_dmabuf_feedback_v1 *object;24
SuiWaylandDmabufFeedback *active, *building;25
DmabufFormat *table;26
size_t table_count, total_formats;27
int have_table, main_seen, target_seen, flags_seen, formats_seen, invalid, status;28
uint64_t generation;29
DmabufTranche current;30
};32
SuiWaylandDmabufFeedback *sui_wayland_dmabuf_feedback_ref(SuiWaylandDmabufFeedback *feedback)33
{34
if (feedback) feedback->references++;35
return feedback;36
}38
void sui_wayland_dmabuf_feedback_unref(SuiWaylandDmabufFeedback *feedback)39
{40
if (!feedback || --feedback->references) return;41
for (size_t i = 0; i < feedback->count; i++) free(feedback->tranches[i].formats);42
free(feedback);43
}45
static void dmabuf_builder_reset(DmabufFeedbackState *state)46
{47
sui_wayland_dmabuf_feedback_unref(state->building);48
state->building = NULL;49
free(state->current.formats);50
memset(&state->current, 0, sizeof state->current);51
state->total_formats = 0;52
state->main_seen = state->target_seen = state->flags_seen = state->formats_seen = state->invalid = 0;53
}55
static int dmabuf_builder(DmabufFeedbackState *state)56
{57
if (state->invalid) return 0;58
if (!state->building) {59
state->building = calloc(1, sizeof *state->building);60
if (!state->building) { state->invalid = 1; return 0; }61
state->building->references = 1;62
}63
return 1;64
}66
/* Sort an audit copy so duplicate validation stays O(n log n) while the67
* immutable snapshot retains compositor preference and wire ordering. */68
typedef struct { dev_t device; uint32_t flags; DmabufFormat format; } DmabufPair;69
static int dmabuf_pair_compare(const void *left, const void *right)70
{71
const DmabufPair *a = left, *b = right;72
#define COMPARE(field) if (a->field != b->field) return a->field < b->field ? -1 : 173
COMPARE(device);74
COMPARE(flags);75
COMPARE(format.fourcc);76
COMPARE(format.modifier);77
#undef COMPARE78
return 0;79
}81
static int dmabuf_unique(DmabufFeedbackState *state)82
{83
if (!state->total_formats) return 1;84
DmabufPair *pairs = malloc(state->total_formats * sizeof *pairs);85
if (!pairs) return 0;86
size_t used = 0;87
for (size_t i = 0; i < state->building->count; i++) {88
DmabufTranche *tranche = &state->building->tranches[i];89
for (size_t j = 0; j < tranche->count; j++)90
pairs[used++] = (DmabufPair){tranche->device, tranche->flags, tranche->formats[j]};91
}92
qsort(pairs, used, sizeof *pairs, dmabuf_pair_compare);93
int unique = 1;94
for (size_t i = 1; i < used; i++)95
if (!dmabuf_pair_compare(&pairs[i - 1], &pairs[i])) { unique = 0; break; }96
free(pairs);97
return unique;98
}100
static void dmabuf_done(void *data, struct zwp_linux_dmabuf_feedback_v1 *object)101
{102
DmabufFeedbackState *state = data;103
state->generation++;104
int valid = !state->invalid && state->building && state->have_table && state->main_seen &&105
state->building->count && !state->target_seen && !state->flags_seen && !state->formats_seen &&106
dmabuf_unique(state);107
sui_wayland_dmabuf_feedback_unref(state->active);108
state->active = NULL;109
if (valid) {110
state->active = state->building;111
state->building = NULL;112
state->active->generation = state->generation;113
state->status = SUI_DMABUF_READY;114
} else state->status = SUI_DMABUF_INVALID;115
dmabuf_builder_reset(state);116
}118
static void dmabuf_format_table(void *data, struct zwp_linux_dmabuf_feedback_v1 *object,119
int32_t fd, uint32_t size)120
{121
DmabufFeedbackState *state = data;122
struct stat info;123
void *mapped = MAP_FAILED;124
DmabufFormat *table = NULL;125
int valid = size <= DMABUF_TABLE_BYTES && size % 16 == 0 &&126
fstat(fd, &info) == 0 && info.st_size >= (off_t)size;127
if (valid && size) {128
mapped = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);129
table = malloc((size / 16) * sizeof *table);130
valid = mapped != MAP_FAILED && table;131
if (valid) {132
for (size_t i = 0; i < size / 16; i++) {133
memcpy(&table[i].fourcc, (const uint8_t *)mapped + i * 16, 4);134
memcpy(&table[i].modifier, (const uint8_t *)mapped + i * 16 + 8, 8);135
}136
}137
}138
if (mapped != MAP_FAILED) munmap(mapped, size);139
close(fd);140
free(state->table);141
state->table = valid ? table : NULL;142
state->table_count = valid ? size / 16 : 0;143
state->have_table = valid;144
if (!valid) { free(table); state->invalid = 1; }145
}147
static int dmabuf_device(struct wl_array *array, dev_t *device)148
{149
if (array->size != sizeof *device || !array->data) return 0;150
memcpy(device, array->data, sizeof *device);151
return 1;152
}154
static void dmabuf_main_device(void *data, struct zwp_linux_dmabuf_feedback_v1 *object,155
struct wl_array *device)156
{157
DmabufFeedbackState *state = data;158
if (!dmabuf_builder(state)) return;159
if (state->main_seen || state->building->count || state->target_seen ||160
!dmabuf_device(device, &state->building->main_device)) state->invalid = 1;161
state->main_seen = 1;162
}164
static void dmabuf_target_device(void *data, struct zwp_linux_dmabuf_feedback_v1 *object,165
struct wl_array *device)166
{167
DmabufFeedbackState *state = data;168
if (!dmabuf_builder(state)) return;169
if (!state->main_seen || state->target_seen ||170
!dmabuf_device(device, &state->current.device)) state->invalid = 1;171
state->target_seen = 1;172
}174
static void dmabuf_tranche_flags(void *data, struct zwp_linux_dmabuf_feedback_v1 *object, uint32_t flags)175
{176
DmabufFeedbackState *state = data;177
if (!dmabuf_builder(state)) return;178
if (!state->target_seen || state->flags_seen) state->invalid = 1;179
state->current.flags = flags;180
state->flags_seen = 1;181
}183
static void dmabuf_tranche_formats(void *data, struct zwp_linux_dmabuf_feedback_v1 *object,184
struct wl_array *indices)185
{186
DmabufFeedbackState *state = data;187
if (!dmabuf_builder(state)) return;188
if (!state->target_seen || !state->have_table || indices->size % 2 ||189
(indices->size && !indices->data) || indices->size / 2 > DMABUF_FORMATS - state->total_formats) {190
state->invalid = 1;191
return;192
}193
size_t count = indices->size / 2;194
if (count) {195
DmabufFormat *formats = realloc(state->current.formats,196
(state->current.count + count) * sizeof *formats);197
if (!formats) { state->invalid = 1; return; }198
state->current.formats = formats;199
for (size_t i = 0; i < count; i++) {200
uint16_t index;201
memcpy(&index, (const uint8_t *)indices->data + i * 2, 2);202
if (index >= state->table_count) { state->invalid = 1; return; }203
formats[state->current.count + i] = state->table[index];204
}205
}206
state->current.count += count;207
state->total_formats += count;208
state->formats_seen = 1;209
}211
static void dmabuf_tranche_done(void *data, struct zwp_linux_dmabuf_feedback_v1 *object)212
{213
DmabufFeedbackState *state = data;214
if (!dmabuf_builder(state)) return;215
if (!state->target_seen || !state->flags_seen || !state->formats_seen ||216
state->building->count >= DMABUF_TRANCHES) { state->invalid = 1; return; }217
state->building->tranches[state->building->count++] = state->current;218
memset(&state->current, 0, sizeof state->current);219
state->target_seen = state->flags_seen = state->formats_seen = 0;220
}222
static const struct zwp_linux_dmabuf_feedback_v1_listener dmabuf_feedback_listener = {223
.done = dmabuf_done, .format_table = dmabuf_format_table, .main_device = dmabuf_main_device,224
.tranche_done = dmabuf_tranche_done, .tranche_target_device = dmabuf_target_device,225
.tranche_formats = dmabuf_tranche_formats, .tranche_flags = dmabuf_tranche_flags226
};228
static void dmabuf_feedback_revoke(Wl *surface)229
{230
DmabufFeedbackState *state = surface->dmabuf_feedback;231
if (!state) return;232
if (state->object) zwp_linux_dmabuf_feedback_v1_destroy(state->object);233
state->object = NULL;234
dmabuf_builder_reset(state);235
free(state->table);236
state->table = NULL;237
state->table_count = 0;238
state->have_table = 0;239
sui_wayland_dmabuf_feedback_unref(state->active);240
state->active = NULL;241
state->status = SUI_DMABUF_UNAVAILABLE;242
state->generation++;243
}245
static void dmabuf_feedback_revoke_tree(Wl *surface)246
{247
dmabuf_feedback_revoke(surface);248
for (Wl *child = surface->children; child; child = child->sibling) dmabuf_feedback_revoke_tree(child);249
}251
static void dmabuf_feedback_close(Wl *surface)252
{253
dmabuf_feedback_revoke(surface);254
free(surface->dmabuf_feedback);255
surface->dmabuf_feedback = NULL;256
}258
int sui_wayland_dmabuf_feedback_enable(SuiWaylandSurface *surface)259
{260
if (!surface || !surface->display || !surface->surface || surface->dead || surface->closed) return 0;261
Wl *root = surface;262
while (root->parent) root = root->parent;263
if (!root->dmabuf_global || root->dmabuf_version < 4 || root->dead || root->closed) return 0;264
if (surface->dmabuf_feedback && surface->dmabuf_feedback->object) return 1;265
if (!surface->dmabuf_feedback) surface->dmabuf_feedback = calloc(1, sizeof *surface->dmabuf_feedback);266
DmabufFeedbackState *state = surface->dmabuf_feedback;267
if (!state) return 0;268
if (!root->dmabuf)269
root->dmabuf = wl_registry_bind(root->registry, root->dmabuf_global, &zwp_linux_dmabuf_v1_interface, 4);270
if (!root->dmabuf) return 0;271
state->object = zwp_linux_dmabuf_v1_get_surface_feedback(root->dmabuf, surface->surface);272
if (!state->object) return 0;273
if (zwp_linux_dmabuf_feedback_v1_add_listener(state->object, &dmabuf_feedback_listener, state) < 0) {274
dmabuf_feedback_revoke(surface);275
return 0;276
}277
state->status = SUI_DMABUF_PENDING;278
return 1;279
}281
int sui_wayland_dmabuf_feedback_status(SuiWaylandSurface *surface, uint64_t *generation)282
{283
DmabufFeedbackState *state = surface ? surface->dmabuf_feedback : NULL;284
if (generation) *generation = state ? state->generation : 0;285
if (!surface || !surface->display || surface->dead || surface->closed || !state) return SUI_DMABUF_UNAVAILABLE;286
Wl *root = surface;287
while (root->parent) root = root->parent;288
if (!root->display || root->dead || root->closed) return SUI_DMABUF_UNAVAILABLE;289
return state->status;290
}292
SuiWaylandDmabufFeedback *sui_wayland_dmabuf_feedback_acquire(SuiWaylandSurface *surface)293
{294
return sui_wayland_dmabuf_feedback_status(surface, NULL) == SUI_DMABUF_READY ?295
sui_wayland_dmabuf_feedback_ref(surface->dmabuf_feedback->active) : NULL;296
}298
uint64_t sui_wayland_dmabuf_feedback_generation(const SuiWaylandDmabufFeedback *feedback)299
{ return feedback ? feedback->generation : 0; }300
dev_t sui_wayland_dmabuf_feedback_main_device(const SuiWaylandDmabufFeedback *feedback)301
{ return feedback ? feedback->main_device : 0; }302
size_t sui_wayland_dmabuf_feedback_tranche_count(const SuiWaylandDmabufFeedback *feedback)303
{ return feedback ? feedback->count : 0; }305
int sui_wayland_dmabuf_feedback_tranche(const SuiWaylandDmabufFeedback *feedback,306
size_t tranche, dev_t *device, uint32_t *flags, size_t *formats)307
{308
if (!feedback || tranche >= feedback->count) return 0;309
const DmabufTranche *entry = &feedback->tranches[tranche];310
if (device) *device = entry->device;311
if (flags) *flags = entry->flags;312
if (formats) *formats = entry->count;313
return 1;314
}316
int sui_wayland_dmabuf_feedback_format(const SuiWaylandDmabufFeedback *feedback,317
size_t tranche, size_t index, uint32_t *fourcc, uint64_t *modifier)318
{319
if (!feedback || tranche >= feedback->count || index >= feedback->tranches[tranche].count) return 0;320
const DmabufFormat *entry = &feedback->tranches[tranche].formats[index];321
if (fourcc) *fourcc = entry->fourcc;322
if (modifier) *modifier = entry->modifier;323
return 1;324
}