aboutsummaryrefslogtreecommitdiff
path: root/render/pixel_format.c
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2022-09-17 14:37:52 +0200
committerSimon Zeni <simon@bl4ckb0ne.ca>2022-11-15 16:30:00 +0000
commit6e88eeadebe5bed0210158124a3c498a8f8a47cb (patch)
tree83edd18ab44ea8fb34a4e8ad3a6c09d9c488a17f /render/pixel_format.c
parent8cfd44980baae8fdc3eb64461378cadd3362878b (diff)
render/pixel_format: import pixel_format_info_check_stride()
We'll use this function from wlr_shm too. Add some assertions, use int32_t (since the wire protocol uses that, and we don't want to use 16-bit integers on exotic systems) and switch the stride check to be overflow-safe.
Diffstat (limited to 'render/pixel_format.c')
-rw-r--r--render/pixel_format.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/render/pixel_format.c b/render/pixel_format.c
index 586d2a09..330a59cc 100644
--- a/render/pixel_format.c
+++ b/render/pixel_format.c
@@ -1,4 +1,6 @@
+#include <assert.h>
#include <drm_fourcc.h>
+#include <wlr/util/log.h>
#include "render/pixel_format.h"
static const struct wlr_pixel_format_info pixel_format_info[] = {
@@ -176,3 +178,20 @@ enum wl_shm_format convert_drm_format_to_wl_shm(uint32_t fmt) {
return (enum wl_shm_format)fmt;
}
}
+
+bool pixel_format_info_check_stride(const struct wlr_pixel_format_info *fmt,
+ int32_t stride, int32_t width) {
+ assert(fmt->bpp > 0 && fmt->bpp % 8 == 0);
+ int32_t bytes_per_pixel = (int32_t)(fmt->bpp / 8);
+ if (stride % bytes_per_pixel != 0) {
+ wlr_log(WLR_DEBUG, "Invalid stride %d (incompatible with %d "
+ "bytes-per-pixel)", stride, bytes_per_pixel);
+ return false;
+ }
+ if (stride / bytes_per_pixel < width) {
+ wlr_log(WLR_DEBUG, "Invalid stride %d (too small for %d "
+ "bytes-per-pixel and width %d)", stride, bytes_per_pixel, width);
+ return false;
+ }
+ return true;
+}