backend-drm: simplify compile time array copy

In drm_fb_get_from_dmabuf() we have some compile time array copies, and
multiple static_assert() calls are needed (for safety). This makes the
code unpleasant to read.

Add ARRAY_COPY macro, to simplify the code.

Signed-off-by: Leandro Ribeiro <leandro.ribeiro@collabora.com>
dev
Leandro Ribeiro 3 years ago
parent bdb37b30b3
commit 3193ab5807
  1. 41
      libweston/backend-drm/fb.c
  2. 19
      shared/helpers.h

@ -267,30 +267,9 @@ drm_fb_get_from_dmabuf(struct linux_dmabuf_buffer *dmabuf,
fb->refcnt = 1; fb->refcnt = 1;
fb->type = BUFFER_DMABUF; fb->type = BUFFER_DMABUF;
static_assert(ARRAY_LENGTH(import_mod.fds) == ARRAY_COPY(import_mod.fds, dmabuf->attributes.fd);
ARRAY_LENGTH(dmabuf->attributes.fd), ARRAY_COPY(import_mod.strides, dmabuf->attributes.stride);
"GBM and linux_dmabuf FD size must match"); ARRAY_COPY(import_mod.offsets, dmabuf->attributes.offset);
static_assert(sizeof(import_mod.fds) == sizeof(dmabuf->attributes.fd),
"GBM and linux_dmabuf FD size must match");
memcpy(import_mod.fds, dmabuf->attributes.fd, sizeof(import_mod.fds));
static_assert(ARRAY_LENGTH(import_mod.strides) ==
ARRAY_LENGTH(dmabuf->attributes.stride),
"GBM and linux_dmabuf stride size must match");
static_assert(sizeof(import_mod.strides) ==
sizeof(dmabuf->attributes.stride),
"GBM and linux_dmabuf stride size must match");
memcpy(import_mod.strides, dmabuf->attributes.stride,
sizeof(import_mod.strides));
static_assert(ARRAY_LENGTH(import_mod.offsets) ==
ARRAY_LENGTH(dmabuf->attributes.offset),
"GBM and linux_dmabuf offset size must match");
static_assert(sizeof(import_mod.offsets) ==
sizeof(dmabuf->attributes.offset),
"GBM and linux_dmabuf offset size must match");
memcpy(import_mod.offsets, dmabuf->attributes.offset,
sizeof(import_mod.offsets));
fb->bo = gbm_bo_import(backend->gbm, GBM_BO_IMPORT_FD_MODIFIER, fb->bo = gbm_bo_import(backend->gbm, GBM_BO_IMPORT_FD_MODIFIER,
&import_mod, GBM_BO_USE_SCANOUT); &import_mod, GBM_BO_USE_SCANOUT);
@ -303,18 +282,8 @@ drm_fb_get_from_dmabuf(struct linux_dmabuf_buffer *dmabuf,
fb->size = 0; fb->size = 0;
fb->fd = backend->drm.fd; fb->fd = backend->drm.fd;
static_assert(ARRAY_LENGTH(fb->strides) == ARRAY_COPY(fb->strides, dmabuf->attributes.stride);
ARRAY_LENGTH(dmabuf->attributes.stride), ARRAY_COPY(fb->offsets, dmabuf->attributes.offset);
"drm_fb and dmabuf stride size must match");
static_assert(sizeof(fb->strides) == sizeof(dmabuf->attributes.stride),
"drm_fb and dmabuf stride size must match");
memcpy(fb->strides, dmabuf->attributes.stride, sizeof(fb->strides));
static_assert(ARRAY_LENGTH(fb->offsets) ==
ARRAY_LENGTH(dmabuf->attributes.offset),
"drm_fb and dmabuf offset size must match");
static_assert(sizeof(fb->offsets) == sizeof(dmabuf->attributes.offset),
"drm_fb and dmabuf offset size must match");
memcpy(fb->offsets, dmabuf->attributes.offset, sizeof(fb->offsets));
fb->format = pixel_format_get_info(dmabuf->attributes.format); fb->format = pixel_format_get_info(dmabuf->attributes.format);
if (!fb->format) { if (!fb->format) {

@ -41,6 +41,25 @@ extern "C" {
#define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0]) #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
#endif #endif
#define STRING(s) #s
/**
* Compile-time copy of hardcoded arrays.
*
* @param dst the array to copy to.
* @param src the source array.
*/
#ifndef ARRAY_COPY
#define ARRAY_COPY(dst, src) \
do { \
static_assert(ARRAY_LENGTH(src) == ARRAY_LENGTH(dst), \
"src and dst sizes must match"); \
static_assert(sizeof(src) == sizeof(dst), \
"src and dst sizes must match"); \
memcpy((src), (dst), sizeof(src)); \
} while (0)
#endif
/** /**
* Returns the smaller of two values. * Returns the smaller of two values.
* *

Loading…
Cancel
Save