aboutsummaryrefslogtreecommitdiff
path: root/render/drm_format_set.c
blob: dc57ad9bc46f5b36767a92c07cebf614d38fd535 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <assert.h>
#include <drm_fourcc.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <wlr/render/drm_format_set.h>
#include <wlr/util/log.h>
#include "render/drm_format_set.h"

void wlr_drm_format_finish(struct wlr_drm_format *format) {
	if (!format) {
		return;
	}

	free(format->modifiers);
}

void wlr_drm_format_set_finish(struct wlr_drm_format_set *set) {
	for (size_t i = 0; i < set->len; ++i) {
		wlr_drm_format_finish(&set->formats[i]);
	}
	free(set->formats);

	set->len = 0;
	set->capacity = 0;
	set->formats = NULL;
}

static struct wlr_drm_format *format_set_get(const struct wlr_drm_format_set *set,
		uint32_t format) {
	for (size_t i = 0; i < set->len; ++i) {
		if (set->formats[i].format == format) {
			return &set->formats[i];
		}
	}

	return NULL;
}

const struct wlr_drm_format *wlr_drm_format_set_get(
		const struct wlr_drm_format_set *set, uint32_t format) {
	return format_set_get(set, format);
}

bool wlr_drm_format_set_has(const struct wlr_drm_format_set *set,
		uint32_t format, uint64_t modifier) {
	const struct wlr_drm_format *fmt = wlr_drm_format_set_get(set, format);
	if (!fmt) {
		return false;
	}
	return wlr_drm_format_has(fmt, modifier);
}

bool wlr_drm_format_set_add(struct wlr_drm_format_set *set, uint32_t format,
		uint64_t modifier) {
	assert(format != DRM_FORMAT_INVALID);

	struct wlr_drm_format *existing = format_set_get(set, format);
	if (existing) {
		return wlr_drm_format_add(existing, modifier);
	}

	struct wlr_drm_format fmt;
	wlr_drm_format_init(&fmt, format);
	if (!wlr_drm_format_add(&fmt, modifier)) {
		wlr_drm_format_finish(&fmt);
		return false;
	}

	if (set->len == set->capacity) {
		size_t capacity = set->capacity ? set->capacity * 2 : 4;

		struct wlr_drm_format *fmts = realloc(set->formats, sizeof(*fmts) * capacity);
		if (!fmts) {
			wlr_log_errno(WLR_ERROR, "Allocation failed");
			wlr_drm_format_finish(&fmt);
			return false;
		}

		set->capacity = capacity;
		set->formats = fmts;
	}

	set->formats[set->len++] = fmt;
	return true;
}

void wlr_drm_format_init(struct wlr_drm_format *fmt, uint32_t format) {
	*fmt = (struct wlr_drm_format){
		.format = format,
	};
}

bool wlr_drm_format_has(const struct wlr_drm_format *fmt, uint64_t modifier) {
	for (size_t i = 0; i < fmt->len; ++i) {
		if (fmt->modifiers[i] == modifier) {
			return true;
		}
	}
	return false;
}

bool wlr_drm_format_add(struct wlr_drm_format *fmt, uint64_t modifier) {
	if (wlr_drm_format_has(fmt, modifier)) {
		return true;
	}

	if (fmt->len == fmt->capacity) {
		size_t capacity = fmt->capacity ? fmt->capacity * 2 : 4;

		uint64_t *new_modifiers = realloc(fmt->modifiers, sizeof(*fmt->modifiers) * capacity);
		if (!new_modifiers) {
			wlr_log_errno(WLR_ERROR, "Allocation failed");
			return false;
		}

		fmt->capacity = capacity;
		fmt->modifiers = new_modifiers;
	}

	fmt->modifiers[fmt->len++] = modifier;
	return true;
}

bool wlr_drm_format_copy(struct wlr_drm_format *dst, const struct wlr_drm_format *src) {
	assert(src->len <= src->capacity);

	uint64_t *modifiers = malloc(sizeof(*modifiers) * src->len);
	if (!modifiers) {
		return false;
	}

	memcpy(modifiers, src->modifiers, sizeof(*modifiers) * src->len);

	wlr_drm_format_finish(dst);
	dst->capacity = src->len;
	dst->len = src->len;
	dst->format = src->format;
	dst->modifiers = modifiers;
	return true;
}

bool wlr_drm_format_set_copy(struct wlr_drm_format_set *dst, const struct wlr_drm_format_set *src) {
	struct wlr_drm_format *formats = malloc(src->len * sizeof(formats[0]));
	if (formats == NULL) {
		return false;
	}

	struct wlr_drm_format_set out = {
		.len = 0,
		.capacity = src->len,
		.formats = formats,
	};

	size_t i;
	for (i = 0; i < src->len; i++) {
		out.formats[out.len] = (struct wlr_drm_format){0};
		if (!wlr_drm_format_copy(&out.formats[out.len], &src->formats[i])) {
			wlr_drm_format_set_finish(&out);
			return false;
		}

		out.len++;
	}

	*dst = out;

	return true;
}

bool wlr_drm_format_intersect(struct wlr_drm_format *dst,
		const struct wlr_drm_format *a, const struct wlr_drm_format *b) {
	assert(a->format == b->format);

	size_t capacity = a->len < b->len ? a->len : b->len;
	uint64_t *modifiers = malloc(sizeof(*modifiers) * capacity);
	if (!modifiers) {
		return false;
	}

	struct wlr_drm_format fmt = {
		.capacity = capacity,
		.len = 0,
		.modifiers = modifiers,
		.format = a->format,
	};

	for (size_t i = 0; i < a->len; i++) {
		for (size_t j = 0; j < b->len; j++) {
			if (a->modifiers[i] == b->modifiers[j]) {
				assert(fmt.len < fmt.capacity);
				fmt.modifiers[fmt.len++] = a->modifiers[i];
				break;
			}
		}
	}

	wlr_drm_format_finish(dst);
	*dst = fmt;
	return true;
}

bool wlr_drm_format_set_intersect(struct wlr_drm_format_set *dst,
		const struct wlr_drm_format_set *a, const struct wlr_drm_format_set *b) {
	struct wlr_drm_format_set out = {0};
	out.capacity = a->len < b->len ? a->len : b->len;
	out.formats = malloc(sizeof(*out.formats) * out.capacity);
	if (out.formats == NULL) {
		wlr_log_errno(WLR_ERROR, "Allocation failed");
		return false;
	}

	for (size_t i = 0; i < a->len; i++) {
		for (size_t j = 0; j < b->len; j++) {
			if (a->formats[i].format == b->formats[j].format) {
				// When the two formats have no common modifier, keep
				// intersecting the rest of the formats: they may be compatible
				// with each other
				out.formats[out.len] = (struct wlr_drm_format){0};
				if (!wlr_drm_format_intersect(&out.formats[out.len],
						&a->formats[i], &b->formats[j])) {
					wlr_drm_format_set_finish(&out);
					return false;
				}

				if (out.formats[out.len].len == 0) {
					wlr_drm_format_finish(&out.formats[out.len]);
				} else {
					out.len++;
				}

				break;
			}
		}
	}

	if (out.len == 0) {
		wlr_drm_format_set_finish(&out);
		return false;
	}

	wlr_drm_format_set_finish(dst);
	*dst = out;
	return true;
}

static bool drm_format_set_extend(struct wlr_drm_format_set *dst,
		const struct wlr_drm_format_set *src) {
	for (size_t i = 0; i < src->len; i++) {
		struct wlr_drm_format *format = &src->formats[i];
		for (size_t j = 0; j < format->len; j++) {
			if (!wlr_drm_format_set_add(dst, format->format, format->modifiers[j])) {
				wlr_log_errno(WLR_ERROR, "Adding format/modifier to set failed");
				return false;
			}
		}
	}

	return true;
}

bool wlr_drm_format_set_union(struct wlr_drm_format_set *dst,
		const struct wlr_drm_format_set *a, const struct wlr_drm_format_set *b) {
	struct wlr_drm_format_set out = {0};
	out.capacity = a->len + b->len;
	out.formats = malloc(sizeof(*out.formats) * out.capacity);
	if (out.formats == NULL) {
		wlr_log_errno(WLR_ERROR, "Allocation failed");
		return false;
	}

	// Add both a and b sets into out
	if (!drm_format_set_extend(&out, a) ||
		!drm_format_set_extend(&out, b)) {
		wlr_drm_format_set_finish(&out);
		return false;
	}

	wlr_drm_format_set_finish(dst);
	*dst = out;

	return true;
}