From a30668b06ecd641bbf9b04dd620b389b371337ec Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Wed, 25 Sep 2019 15:06:46 +0300 Subject: [PATCH] example: refactor dumb FB helper This is necessary for the upcoming composer example. --- example/common.c | 51 +++++++++++++++++++++++++++++++++--------------- example/common.h | 14 +++++++++++-- example/simple.c | 14 +++++++------ 3 files changed, 55 insertions(+), 24 deletions(-) diff --git a/example/common.c b/example/common.c index 00e07ea..00b5cc1 100644 --- a/example/common.c +++ b/example/common.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -47,13 +48,13 @@ void disable_all_crtcs_except(int drm_fd, drmModeRes *drm_res, uint32_t crtc_id) } } -uint32_t create_argb_fb(int drm_fd, uint32_t width, uint32_t height, - uint32_t color, bool with_alpha) +bool dumb_fb_init(struct dumb_fb *fb, int drm_fd, uint32_t format, + uint32_t width, uint32_t height) { int ret; uint32_t fb_id; - uint32_t *data; - size_t i; + + assert(format == DRM_FORMAT_ARGB8888 || format == DRM_FORMAT_XRGB8888); struct drm_mode_create_dumb create = { .width = width, @@ -63,36 +64,54 @@ uint32_t create_argb_fb(int drm_fd, uint32_t width, uint32_t height, }; ret = drmIoctl(drm_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create); if (ret < 0) { - return 0; + return false; } - uint32_t fmt = with_alpha ? DRM_FORMAT_ARGB8888 : DRM_FORMAT_XRGB8888; uint32_t handles[4] = { create.handle }; uint32_t strides[4] = { create.pitch }; uint32_t offsets[4] = { 0 }; - ret = drmModeAddFB2(drm_fd, width, height, fmt, handles, strides, + ret = drmModeAddFB2(drm_fd, width, height, format, handles, strides, offsets, &fb_id, 0); if (ret < 0) { - return 0; + return false; } - struct drm_mode_map_dumb map = { .handle = create.handle }; + fb->width = width; + fb->height = height; + fb->stride = create.pitch; + fb->size = create.size; + fb->handle = create.handle; + fb->id = fb_id; + return true; +} + +void *dumb_fb_map(struct dumb_fb *fb, int drm_fd) +{ + int ret; + + struct drm_mode_map_dumb map = { .handle = fb->handle }; ret = drmIoctl(drm_fd, DRM_IOCTL_MODE_MAP_DUMB, &map); if (ret < 0) { - return 0; + return MAP_FAILED; } - data = mmap(0, create.size, PROT_READ | PROT_WRITE, MAP_SHARED, drm_fd, + return mmap(0, fb->size, PROT_READ | PROT_WRITE, MAP_SHARED, drm_fd, map.offset); +} + +void dumb_fb_fill(struct dumb_fb *fb, int drm_fd, uint32_t color) +{ + uint32_t *data; + size_t i; + + data = dumb_fb_map(fb, drm_fd); if (data == MAP_FAILED) { - return 0; + return; } - for (i = 0; i < create.size / sizeof(uint32_t); i++) { + for (i = 0; i < fb->size / sizeof(uint32_t); i++) { data[i] = color; } - munmap(data, create.size); - return fb_id; + munmap(data, fb->size); } - diff --git a/example/common.h b/example/common.h index c264d36..8d4249b 100644 --- a/example/common.h +++ b/example/common.h @@ -5,11 +5,21 @@ #include #include +struct dumb_fb { + uint32_t format; + uint32_t width, height, stride, size; + uint32_t handle; + uint32_t id; +}; + drmModeConnector *pick_connector(int drm_fd, drmModeRes *drm_res); drmModeCrtc *pick_crtc(int drm_fd, drmModeRes *drm_res, drmModeConnector *connector); void disable_all_crtcs_except(int drm_fd, drmModeRes *drm_res, uint32_t crtc_id); -uint32_t create_argb_fb(int drm_fd, uint32_t width, uint32_t height, - uint32_t color, bool with_alpha); + +bool dumb_fb_init(struct dumb_fb *fb, int drm_fd, uint32_t format, + uint32_t width, uint32_t height); +void *dumb_fb_map(struct dumb_fb *fb, int drm_fd); +void dumb_fb_fill(struct dumb_fb *fb, int drm_fd, uint32_t color); #endif diff --git a/example/simple.c b/example/simple.c index fded603..60e03c0 100644 --- a/example/simple.c +++ b/example/simple.c @@ -1,4 +1,5 @@ #define _POSIX_C_SOURCE 200809L +#include #include #include #include @@ -21,20 +22,21 @@ static struct liftoff_layer *add_layer(int drm_fd, struct liftoff_output *output bool with_alpha) { static size_t color_idx = 0; - uint32_t fb_id; + struct dumb_fb fb = {0}; struct liftoff_layer *layer; - fb_id = create_argb_fb(drm_fd, width, height, colors[color_idx], - with_alpha); - if (fb_id == 0) { + uint32_t format = with_alpha ? DRM_FORMAT_ARGB8888 : DRM_FORMAT_XRGB8888; + if (!dumb_fb_init(&fb, drm_fd, format, width, height)) { fprintf(stderr, "failed to create framebuffer\n"); return NULL; } - printf("Created FB %d with size %dx%d\n", fb_id, width, height); + printf("Created FB %d with size %dx%d\n", fb.id, width, height); + + dumb_fb_fill(&fb, drm_fd, colors[color_idx]); color_idx = (color_idx + 1) % (sizeof(colors) / sizeof(colors[0])); layer = liftoff_layer_create(output); - liftoff_layer_set_property(layer, "FB_ID", fb_id); + liftoff_layer_set_property(layer, "FB_ID", fb.id); liftoff_layer_set_property(layer, "CRTC_X", x); liftoff_layer_set_property(layer, "CRTC_Y", y); liftoff_layer_set_property(layer, "CRTC_W", width);