compositor-drm: Extract drm_fb_addfb into a helper

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

Signed-off-by: Daniel Stone <daniels@collabora.com>
Reviewed-by: Pekka Paalanen <pekka.paalanen@collabora.co.uk>
Tested-by: Emre Ucan <eucan@de.adit-jv.com>
diff --git a/libweston/compositor-drm.c b/libweston/compositor-drm.c
index 628f7cb..780c341 100644
--- a/libweston/compositor-drm.c
+++ b/libweston/compositor-drm.c
@@ -309,7 +309,10 @@
 
 	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;
@@ -861,7 +864,7 @@
 		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);
@@ -877,6 +880,32 @@
 	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)
@@ -887,12 +916,10 @@
 	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);
@@ -918,30 +945,20 @@
 		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 (drm_fb_addfb(fb) != 0) {
+		weston_log("failed to create kms fb: %m\n");
+		goto err_bo;
 	}
 
-	if (ret)
-		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;
@@ -976,8 +993,6 @@
 		   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);
@@ -994,10 +1009,10 @@
 
 	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) {
@@ -1019,19 +1034,7 @@
 		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;
 	}
@@ -2045,8 +2048,10 @@
 	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,
@@ -4156,7 +4161,7 @@
 		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;
 	}
@@ -5957,7 +5962,7 @@
 		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] "
@@ -5966,7 +5971,7 @@
 	}
 
 	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);