2022-09-17 14:37:52 +02:00
|
|
|
#include <assert.h>
|
2021-03-15 12:29:29 -04:00
|
|
|
#include <drm_fourcc.h>
|
2022-09-17 14:37:52 +02:00
|
|
|
#include <wlr/util/log.h>
|
2021-03-15 12:29:29 -04:00
|
|
|
#include "render/pixel_format.h"
|
|
|
|
|
|
|
|
|
|
const struct wlr_pixel_format_info *drm_get_pixel_format_info(uint32_t fmt) {
|
2026-02-19 15:54:43 +01:00
|
|
|
for (size_t i = 0; i < pixel_format_info_len; ++i) {
|
2021-03-15 12:29:29 -04:00
|
|
|
if (pixel_format_info[i].drm_format == fmt) {
|
|
|
|
|
return &pixel_format_info[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2021-03-15 12:35:47 -04:00
|
|
|
|
|
|
|
|
uint32_t convert_wl_shm_format_to_drm(enum wl_shm_format fmt) {
|
|
|
|
|
switch (fmt) {
|
|
|
|
|
case WL_SHM_FORMAT_XRGB8888:
|
|
|
|
|
return DRM_FORMAT_XRGB8888;
|
|
|
|
|
case WL_SHM_FORMAT_ARGB8888:
|
|
|
|
|
return DRM_FORMAT_ARGB8888;
|
|
|
|
|
default:
|
|
|
|
|
return (uint32_t)fmt;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum wl_shm_format convert_drm_format_to_wl_shm(uint32_t fmt) {
|
|
|
|
|
switch (fmt) {
|
|
|
|
|
case DRM_FORMAT_XRGB8888:
|
|
|
|
|
return WL_SHM_FORMAT_XRGB8888;
|
|
|
|
|
case DRM_FORMAT_ARGB8888:
|
|
|
|
|
return WL_SHM_FORMAT_ARGB8888;
|
|
|
|
|
default:
|
|
|
|
|
return (enum wl_shm_format)fmt;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-17 14:37:52 +02:00
|
|
|
|
2023-05-08 22:17:26 +02:00
|
|
|
uint32_t pixel_format_info_pixels_per_block(const struct wlr_pixel_format_info *info) {
|
2026-02-19 15:54:43 +01:00
|
|
|
return info->block_width * info->block_height;
|
2023-05-08 22:17:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int32_t div_round_up(int32_t dividend, int32_t divisor) {
|
|
|
|
|
int32_t quotient = dividend / divisor;
|
|
|
|
|
if (dividend % divisor != 0) {
|
|
|
|
|
quotient++;
|
|
|
|
|
}
|
|
|
|
|
return quotient;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t pixel_format_info_min_stride(const struct wlr_pixel_format_info *fmt, int32_t width) {
|
|
|
|
|
int32_t pixels_per_block = (int32_t)pixel_format_info_pixels_per_block(fmt);
|
|
|
|
|
int32_t bytes_per_block = (int32_t)fmt->bytes_per_block;
|
|
|
|
|
if (width > INT32_MAX / bytes_per_block) {
|
|
|
|
|
wlr_log(WLR_DEBUG, "Invalid width %d (overflow)", width);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return div_round_up(width * bytes_per_block, pixels_per_block);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-17 14:37:52 +02:00
|
|
|
bool pixel_format_info_check_stride(const struct wlr_pixel_format_info *fmt,
|
|
|
|
|
int32_t stride, int32_t width) {
|
2023-05-08 22:17:26 +02:00
|
|
|
int32_t bytes_per_block = (int32_t)fmt->bytes_per_block;
|
|
|
|
|
if (stride % bytes_per_block != 0) {
|
2022-09-17 14:37:52 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Invalid stride %d (incompatible with %d "
|
2023-05-08 22:17:26 +02:00
|
|
|
"bytes-per-block)", stride, bytes_per_block);
|
2022-09-17 14:37:52 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2023-05-08 22:17:26 +02:00
|
|
|
|
|
|
|
|
int32_t min_stride = pixel_format_info_min_stride(fmt, width);
|
|
|
|
|
if (min_stride <= 0) {
|
|
|
|
|
return false;
|
|
|
|
|
} else if (stride < min_stride) {
|
2022-09-17 14:37:52 +02:00
|
|
|
wlr_log(WLR_DEBUG, "Invalid stride %d (too small for %d "
|
2023-05-08 22:17:26 +02:00
|
|
|
"bytes-per-block and width %d)", stride, bytes_per_block, width);
|
2022-09-17 14:37:52 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2023-05-08 22:17:26 +02:00
|
|
|
|
2022-09-17 14:37:52 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
2024-01-31 12:30:16 -05:00
|
|
|
|
|
|
|
|
bool pixel_format_has_alpha(uint32_t fmt) {
|
2026-02-19 15:54:43 +01:00
|
|
|
return !pixel_format_is_opaque(fmt);
|
2026-01-31 15:00:57 +04:00
|
|
|
}
|