[PATCH v16 11/23] compositor-drm: Extract drm_fb_addfb into a helper

Daniel Stone daniels at collabora.com
Thu Jul 5 17:16:38 UTC 2018


We currently do the same thing in two places, and will soon have a
third.

Signed-off-by: Daniel Stone <daniels at collabora.com>
Reviewed-by: Pekka Paalanen <pekka.paalanen at collabora.co.uk>
Tested-by: Emre Ucan <eucan at de.adit-jv.com>
---
 libweston/compositor-drm.c | 91 ++++++++++++++++++++------------------
 1 file changed, 48 insertions(+), 43 deletions(-)

diff --git a/libweston/compositor-drm.c b/libweston/compositor-drm.c
index 51e5b4262..658e91a96 100644
--- a/libweston/compositor-drm.c
+++ b/libweston/compositor-drm.c
@@ -294,7 +294,10 @@ struct drm_fb {
 
 	int refcnt;
 
-	uint32_t fb_id, stride, handle, size;
+	uint32_t fb_id, size;
+	uint32_t handles[4];
+	uint32_t strides[4];
+	uint32_t offsets[4];
 	const struct pixel_format_info *format;
 	int width, height;
 	int fd;
@@ -838,7 +841,7 @@ drm_fb_destroy_dumb(struct drm_fb *fb)
 		munmap(fb->map, fb->size);
 
 	memset(&destroy_arg, 0, sizeof(destroy_arg));
-	destroy_arg.handle = fb->handle;
+	destroy_arg.handle = fb->handles[0];
 	drmIoctl(fb->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_arg);
 
 	drm_fb_destroy(fb);
@@ -854,6 +857,32 @@ drm_fb_destroy_gbm(struct gbm_bo *bo, void *data)
 	drm_fb_destroy(fb);
 }
 
+static int
+drm_fb_addfb(struct drm_fb *fb)
+{
+	int ret;
+
+	ret = drmModeAddFB2(fb->fd, fb->width, fb->height, fb->format->format,
+			    fb->handles, fb->strides, fb->offsets, &fb->fb_id,
+			    0);
+	if (ret == 0)
+		return 0;
+
+	/* Legacy AddFB can't always infer the format from depth/bpp alone, so
+	 * check if our format is one of the lucky ones. */
+	if (!fb->format->depth || !fb->format->bpp)
+		return ret;
+
+	/* Cannot fall back to AddFB for multi-planar formats either. */
+	if (fb->handles[1] || fb->handles[2] || fb->handles[3])
+		return ret;
+
+	ret = drmModeAddFB(fb->fd, fb->width, fb->height,
+			   fb->format->depth, fb->format->bpp,
+			   fb->strides[0], fb->handles[0], &fb->fb_id);
+	return ret;
+}
+
 static struct drm_fb *
 drm_fb_create_dumb(struct drm_backend *b, int width, int height,
 		   uint32_t format)
@@ -864,12 +893,10 @@ drm_fb_create_dumb(struct drm_backend *b, int width, int height,
 	struct drm_mode_create_dumb create_arg;
 	struct drm_mode_destroy_dumb destroy_arg;
 	struct drm_mode_map_dumb map_arg;
-	uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
 
 	fb = zalloc(sizeof *fb);
 	if (!fb)
 		return NULL;
-
 	fb->refcnt = 1;
 
 	fb->format = pixel_format_get_info(format);
@@ -895,30 +922,20 @@ drm_fb_create_dumb(struct drm_backend *b, int width, int height,
 		goto err_fb;
 
 	fb->type = BUFFER_PIXMAN_DUMB;
-	fb->handle = create_arg.handle;
-	fb->stride = create_arg.pitch;
+	fb->handles[0] = create_arg.handle;
+	fb->strides[0] = create_arg.pitch;
 	fb->size = create_arg.size;
 	fb->width = width;
 	fb->height = height;
 	fb->fd = b->drm.fd;
 
-	handles[0] = fb->handle;
-	pitches[0] = fb->stride;
-	offsets[0] = 0;
-
-	ret = drmModeAddFB2(b->drm.fd, width, height, fb->format->format,
-			    handles, pitches, offsets, &fb->fb_id, 0);
-	if (ret) {
-		ret = drmModeAddFB(b->drm.fd, width, height,
-				   fb->format->depth, fb->format->bpp,
-				   fb->stride, fb->handle, &fb->fb_id);
-	}
-
-	if (ret)
+	if (drm_fb_addfb(fb) != 0) {
+		weston_log("failed to create kms fb: %m\n");
 		goto err_bo;
+	}
 
 	memset(&map_arg, 0, sizeof map_arg);
-	map_arg.handle = fb->handle;
+	map_arg.handle = fb->handles[0];
 	ret = drmIoctl(fb->fd, DRM_IOCTL_MODE_MAP_DUMB, &map_arg);
 	if (ret)
 		goto err_add_fb;
@@ -953,8 +970,6 @@ drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
 		   bool is_opaque, enum drm_fb_type type)
 {
 	struct drm_fb *fb = gbm_bo_get_user_data(bo);
-	uint32_t handles[4] = { 0 }, pitches[4] = { 0 }, offsets[4] = { 0 };
-	int ret;
 
 	if (fb) {
 		assert(fb->type == type);
@@ -971,10 +986,10 @@ drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
 
 	fb->width = gbm_bo_get_width(bo);
 	fb->height = gbm_bo_get_height(bo);
-	fb->stride = gbm_bo_get_stride(bo);
-	fb->handle = gbm_bo_get_handle(bo).u32;
+	fb->strides[0] = gbm_bo_get_stride(bo);
+	fb->handles[0] = gbm_bo_get_handle(bo).u32;
 	fb->format = pixel_format_get_info(gbm_bo_get_format(bo));
-	fb->size = fb->stride * fb->height;
+	fb->size = fb->strides[0] * fb->height;
 	fb->fd = backend->drm.fd;
 
 	if (!fb->format) {
@@ -994,19 +1009,7 @@ drm_fb_get_from_bo(struct gbm_bo *bo, struct drm_backend *backend,
 		goto err_free;
 	}
 
-	handles[0] = fb->handle;
-	pitches[0] = fb->stride;
-	offsets[0] = 0;
-
-	ret = drmModeAddFB2(backend->drm.fd, fb->width, fb->height,
-			    fb->format->format, handles, pitches, offsets,
-			    &fb->fb_id, 0);
-	if (ret && fb->format->depth && fb->format->bpp)
-		ret = drmModeAddFB(backend->drm.fd, fb->width, fb->height,
-				   fb->format->depth, fb->format->bpp,
-				   fb->stride, fb->handle, &fb->fb_id);
-
-	if (ret) {
+	if (drm_fb_addfb(fb) != 0) {
 		weston_log("failed to create kms fb: %m\n");
 		goto err_free;
 	}
@@ -2010,8 +2013,10 @@ drm_output_apply_state_legacy(struct drm_output_state *state)
 	assert(scanout_state->dest_h == scanout_state->src_h >> 16);
 
 	mode = to_drm_mode(output->base.current_mode);
-	if (backend->state_invalid || !scanout_plane->state_cur->fb ||
-	    scanout_plane->state_cur->fb->stride != scanout_state->fb->stride) {
+	if (backend->state_invalid ||
+	    !scanout_plane->state_cur->fb ||
+	    scanout_plane->state_cur->fb->strides[0] !=
+	    scanout_state->fb->strides[0]) {
 		ret = drmModeSetCrtc(backend->drm.fd, output->crtc_id,
 				     scanout_state->fb->fb_id,
 				     0, 0,
@@ -4071,7 +4076,7 @@ drm_output_init_pixman(struct drm_output *output, struct drm_backend *b)
 		output->image[i] =
 			pixman_image_create_bits(pixman_format, w, h,
 						 output->dumb[i]->map,
-						 output->dumb[i]->stride);
+						 output->dumb[i]->strides[0]);
 		if (!output->image[i])
 			goto err;
 	}
@@ -5843,7 +5848,7 @@ recorder_frame_notify(struct wl_listener *listener, void *data)
 		return;
 
 	ret = drmPrimeHandleToFD(b->drm.fd,
-				 output->scanout_plane->state_cur->fb->handle,
+				 output->scanout_plane->state_cur->fb->handles[0],
 				 DRM_CLOEXEC, &fd);
 	if (ret) {
 		weston_log("[libva recorder] "
@@ -5852,7 +5857,7 @@ recorder_frame_notify(struct wl_listener *listener, void *data)
 	}
 
 	ret = vaapi_recorder_frame(output->recorder, fd,
-				   output->scanout_plane->state_cur->fb->stride);
+				   output->scanout_plane->state_cur->fb->strides[0]);
 	if (ret < 0) {
 		weston_log("[libva recorder] aborted: %m\n");
 		recorder_destroy(output);
-- 
2.17.1



More information about the wayland-devel mailing list