master
h 49 lines 1.57 KB
Raw
1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Pixman stride and buffer size helpers.
4 * Expects PIXMAN_FORMAT_BPP() and pixman_format_code_t to be
5 * already defined (by either <pixman.h> or "pixman-minimal.h").
6 */
7
8 #ifndef QEMU_PIXMAN_HELPERS_H
9 #define QEMU_PIXMAN_HELPERS_H
10
11 /*
12 * Compute the row stride for a pixman image, aligned to sizeof(uint32_t),
13 * as pixman does. Returns -1 on integer overflow.
14 */
15 static inline int qemu_pixman_stride(pixman_format_code_t format, int width)
16 {
17 int stride;
18
19 if (unlikely(__builtin_mul_overflow(width, PIXMAN_FORMAT_BPP(format),
20 &stride)) ||
21 unlikely(__builtin_add_overflow(stride, 31, &stride))) {
22 return -1;
23 }
24 return (stride / 32) * sizeof(uint32_t);
25 }
26
27 /*
28 * Compute stride and buffer size for a pixman image.
29 * If *rowstride_bytes is 0, compute it from format and width
30 * (aligned to sizeof(uint32_t), as pixman does).
31 * Returns false on integer overflow.
32 */
33 static inline bool qemu_pixman_image_calc_size(pixman_format_code_t format,
34 int width, int height,
35 int *rowstride_bytes,
36 size_t *buf_size)
37 {
38 if (!*rowstride_bytes) {
39 *rowstride_bytes = qemu_pixman_stride(format, width);
40 if (*rowstride_bytes < 0) {
41 return false;
42 }
43 }
44
45 return likely(!__builtin_mul_overflow((size_t)height,
46 (size_t)*rowstride_bytes, buf_size));
47 }
48
49 #endif /* QEMU_PIXMAN_HELPERS_H */