[PATCH 2/5] drm/dumb-buffers: Fix size calculations and set default pitch and size

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Calculate the dumb-buffer scanline pitch with existing 4CC format
helpers and provide results to drivers. Fixes the overflow and size
tests. Drivers can further reuse the computed values.

The dumb-buffer overflow tests round up any given bits-per-pixel
value to a multiple of 8. So even one-bit formats, such as DRM_FORMAT_C1,
require 8 bits per pixel. While not common, low-end displays use such
formats; with a possibly considerable overallocation of memory.

There's also quite a bit of code duplication among DRM's memory
managers. Each calculates scanline pitch and buffer size from the given
arguments. But the implementations are inconsistent in how they treat
alignment and format support. Therefore, provide the already-calculated
values for pitch and size to memory managers. Later patches will update
each to make use of them.

The commit adds drm_mode_align_dumb(), a helper to align a given dumb
buffer's pitch and size. It also exports the function for memory managers
to use.

Signed-off-by: Thomas Zimmermann <tzimmermann@xxxxxxx>
---
 drivers/gpu/drm/drm_dumb_buffers.c | 83 +++++++++++++++++++++++-------
 include/drm/drm_dumb_buffers.h     | 12 +++++
 2 files changed, 77 insertions(+), 18 deletions(-)
 create mode 100644 include/drm/drm_dumb_buffers.h

diff --git a/drivers/gpu/drm/drm_dumb_buffers.c b/drivers/gpu/drm/drm_dumb_buffers.c
index 9916aaf5b3f2..0f87a4c426c1 100644
--- a/drivers/gpu/drm/drm_dumb_buffers.c
+++ b/drivers/gpu/drm/drm_dumb_buffers.c
@@ -25,6 +25,8 @@
 
 #include <drm/drm_device.h>
 #include <drm/drm_drv.h>
+#include <drm/drm_dumb_buffers.h>
+#include <drm/drm_fourcc.h>
 #include <drm/drm_gem.h>
 #include <drm/drm_mode.h>
 
@@ -57,41 +59,86 @@
  * a hardware-specific ioctl to allocate suitable buffer objects.
  */
 
+int drm_mode_align_dumb(struct drm_mode_create_dumb *args,
+			unsigned long pitch_align,
+			unsigned long size_align)
+{
+	u32 pitch = args->pitch;
+	u32 size;
+
+	if (!pitch)
+		return -EINVAL;
+
+	if (pitch_align)
+		pitch = roundup(pitch, pitch_align);
+
+	/* overflow checks for 32bit size calculations */
+	if (args->height > U32_MAX / pitch)
+		return -EINVAL;
+
+	if (!size_align)
+		size_align = PAGE_SIZE;
+	else if (!IS_ALIGNED(size_align, PAGE_SIZE))
+		return -EINVAL;
+
+	size = ALIGN(args->height * pitch, size_align);
+	if (!size)
+		return -EINVAL;
+
+	args->pitch = pitch;
+	args->size = size;
+
+	return 0;
+}
+EXPORT_SYMBOL(drm_mode_align_dumb);
+
 int drm_mode_create_dumb(struct drm_device *dev,
 			 struct drm_mode_create_dumb *args,
 			 struct drm_file *file_priv)
 {
-	u32 cpp, stride, size;
+	u32 fourcc;
+	const struct drm_format_info *info;
+	u64 pitch;
+	int ret;
 
 	if (!dev->driver->dumb_create)
 		return -ENOSYS;
 	if (!args->width || !args->height || !args->bpp)
 		return -EINVAL;
 
-	/* overflow checks for 32bit size calculations */
-	if (args->bpp > U32_MAX - 8)
-		return -EINVAL;
-	cpp = DIV_ROUND_UP(args->bpp, 8);
-	if (cpp > U32_MAX / args->width)
+	/*
+	 * The scanline pitch depends on the buffer width and the color
+	 * format. The latter is specified as a color-mode constant for
+	 * which we first have to find the corresponding color format.
+	 *
+	 * Different color formats can have the same color-mode constant.
+	 * For example XRGB8888 and BGRX8888 both have a color mode of 32.
+	 * It is possible to use different formats for dumb-buffer allocation
+	 * and rendering as long as all involved formats share the same
+	 * color-mode constant.
+	 */
+	fourcc = drm_driver_color_mode_format(dev, args->bpp);
+	if (fourcc == DRM_FORMAT_INVALID)
 		return -EINVAL;
-	stride = cpp * args->width;
-	if (args->height > U32_MAX / stride)
+	info = drm_format_info(fourcc);
+	if (!info)
 		return -EINVAL;
-
-	/* test for wrap-around */
-	size = args->height * stride;
-	if (PAGE_ALIGN(size) == 0)
+	pitch = drm_format_info_min_pitch(info, 0, args->width);
+	if (!pitch || pitch > U32_MAX)
 		return -EINVAL;
 
 	/*
-	 * handle, pitch and size are output parameters. Zero them out to
-	 * prevent drivers from accidentally using uninitialized data. Since
-	 * not all existing userspace is clearing these fields properly we
-	 * cannot reject IOCTL with garbage in them.
+	 * The fields handle, pitch and size are output parameters. Zero out
+	 * handle to prevent drivers from accidentally using uninitialized
+	 * data. Set default pitch and size to the values computed for the
+	 * overflow tests. Driver are free to override them, the default values
+	 * are what most drivers want.
 	 */
 	args->handle = 0;
-	args->pitch = 0;
-	args->size = 0;
+	args->pitch = pitch;
+	ret = drm_mode_align_dumb(args, 0, 0);
+	if (ret)
+		return ret;
 
 	return dev->driver->dumb_create(file_priv, dev, args);
 }
diff --git a/include/drm/drm_dumb_buffers.h b/include/drm/drm_dumb_buffers.h
new file mode 100644
index 000000000000..00172c5997d2
--- /dev/null
+++ b/include/drm/drm_dumb_buffers.h
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: MIT */
+
+#ifndef __DRM_DUMB_BUFFERS_H__
+#define __DRM_DUMB_BUFFERS_H__
+
+struct drm_mode_create_dumb;
+
+int drm_mode_align_dumb(struct drm_mode_create_dumb *args,
+			unsigned long pitch_align,
+			unsigned long size_align);
+
+#endif
-- 
2.47.0




[Index of Archives]     [Linux DRI Users]     [Linux Intel Graphics]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux