当前位置: 首页 > 工具软件 > Plane > 使用案例 >

Linux内核4.14版本——drm框架分析(5)——plane分析

谭建章
2023-12-01

目录

1. struct drm_plane结构体

1.1 uint32_t possible_crtcs

1.2 uint32_t *format_types

1.3 unsigned int format_count

1.4  uint64_t *modifiers

1.5 unsigned int modifier_count

1.6  struct drm_crtc *crtc

1.7 struct drm_framebuffer *fb

1.8 struct drm_framebuffer *old_fb

1.9 struct drm_plane_state *state

2. API

2.1 drm_universal_plane_init

3. func

3.1 struct drm_plane_funcs

3.2 struct drm_plane_helper_funcs


      本文主要介绍plane的一些知识。

1. struct drm_plane结构体

/**
 * struct drm_plane - central DRM plane control structure
 * @dev: DRM device this plane belongs to
 * @head: for list management
 * @name: human readable name, can be overwritten by the driver
 * @base: base mode object
 * @possible_crtcs: pipes this plane can be bound to
 * @format_types: array of formats supported by this plane
 * @format_count: number of formats supported
 * @format_default: driver hasn't supplied supported formats for the plane
 * @crtc: currently bound CRTC
 * @fb: currently bound fb
 * @old_fb: Temporary tracking of the old fb while a modeset is ongoing. Used by
 * 	drm_mode_set_config_internal() to implement correct refcounting.
 * @funcs: helper functions
 * @properties: property tracking for this plane
 * @type: type of plane (overlay, primary, cursor)
 * @zpos_property: zpos property for this plane
 * @rotation_property: rotation property for this plane
 * @helper_private: mid-layer private data
 */
struct drm_plane {
	struct drm_device *dev;
	struct list_head head;

	char *name;

	/**
	 * @mutex:
	 *
	 * Protects modeset plane state, together with the &drm_crtc.mutex of
	 * CRTC this plane is linked to (when active, getting activated or
	 * getting disabled).
	 *
	 * For atomic drivers specifically this protects @state.
	 */
	struct drm_modeset_lock mutex;

	struct drm_mode_object base;

	uint32_t possible_crtcs;
	uint32_t *format_types;
	unsigned int format_count;
	bool format_default;

	uint64_t *modifiers;
	unsigned int modifier_count;

	struct drm_crtc *crtc;
	struct drm_framebuffer *fb;

	struct drm_framebuffer *old_fb;

	const struct drm_plane_funcs *funcs;

	struct drm_object_properties properties;

	enum drm_plane_type type;

	/**
	 * @index: Position inside the mode_config.list, can be used as an array
	 * index. It is invariant over the lifetime of the plane.
	 */
	unsigned index;

	const struct drm_plane_helper_funcs *helper_private;

	/**
	 * @state:
	 *
	 * Current atomic state for this plane.
	 *
	 * This is protected by @mutex. Note that nonblocking atomic commits
	 * access the current plane state without taking locks. Either by going
	 * through the &struct drm_atomic_state pointers, see
	 * for_each_plane_in_state(), for_each_oldnew_plane_in_state(),
	 * for_each_old_plane_in_state() and for_each_new_plane_in_state(). Or
	 * through careful ordering of atomic commit operations as implemented
	 * in the atomic helpers, see &struct drm_crtc_commit.
	 */
	struct drm_plane_state *state;

	struct drm_property *zpos_property;
	struct drm_property *rotation_property;
};

1.1 uint32_t possible_crtcs

        改plane绑定的crtc。

1.2 uint32_t *format_types

        plane支持的fb像素format类型数组, format类型如DRM_FORMAT_ARGB8888

1.3 unsigned int format_count

        plane支持的fb像素format类型数组大小

1.4  uint64_t *modifiers

        modifier数组,其存放的值DRM_FORMAT_MOD_LINEAR/DRM_FORMAT_MOD_X_TILED等

1.5 unsigned int modifier_count

        modifier数组大小

1.6  struct drm_crtc *crtc

        no-atomic drivers用来标识当前绑定的crtc。 对于atomic driver,该值应该为null并使用 &drm_plane_state.crtc替代

1.7 struct drm_framebuffer *fb

        no-atomic drivers用来标识当前绑定的fb。 对于atomic driver,该值应该为null并使用 &drm_plane_state.fb替代

1.8 struct drm_framebuffer *old_fb

       对于non-atomic drivers, old_fb用于在modeset操作时跟踪老的fb atomic drivers下,该值为Null

1.9 struct drm_plane_state *state

        表示plane的各种状态,如其绑定的crtc/fb等,用于atomic操作

2. API

2.1 drm_universal_plane_init

int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
			     uint32_t possible_crtcs,
			     const struct drm_plane_funcs *funcs,
			     const uint32_t *formats, unsigned int format_count,
			     const uint64_t *format_modifiers,
			     enum drm_plane_type type,
			     const char *name, ...)
{
	struct drm_mode_config *config = &dev->mode_config;
	unsigned int format_modifier_count = 0;
	int ret;

	ret = drm_mode_object_add(dev, &plane->base, DRM_MODE_OBJECT_PLANE);  // (1)
	if (ret)
		return ret;

	drm_modeset_lock_init(&plane->mutex);

	plane->base.properties = &plane->properties;                  // (2)
	plane->dev = dev;
	plane->funcs = funcs;
	plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
					    GFP_KERNEL);
	if (!plane->format_types) {
		DRM_DEBUG_KMS("out of memory when allocating plane\n");
		drm_mode_object_unregister(dev, &plane->base);
		return -ENOMEM;
	}

	/*
	 * First driver to need more than 64 formats needs to fix this. Each
	 * format is encoded as a bit and the current code only supports a u64.
	 */
	if (WARN_ON(format_count > 64))
		return -EINVAL;

	if (format_modifiers) {
		const uint64_t *temp_modifiers = format_modifiers;
		while (*temp_modifiers++ != DRM_FORMAT_MOD_INVALID)
			format_modifier_count++;
	}

	if (format_modifier_count)
		config->allow_fb_modifiers = true;

	plane->modifier_count = format_modifier_count;
	plane->modifiers = kmalloc_array(format_modifier_count,
					 sizeof(format_modifiers[0]),
					 GFP_KERNEL);

	if (format_modifier_count && !plane->modifiers) {
		DRM_DEBUG_KMS("out of memory when allocating plane\n");
		kfree(plane->format_types);
		drm_mode_object_unregister(dev, &plane->base);
		return -ENOMEM;
	}

	if (name) {
		va_list ap;

		va_start(ap, name);
		plane->name = kvasprintf(GFP_KERNEL, name, ap);
		va_end(ap);
	} else {
		plane->name = kasprintf(GFP_KERNEL, "plane-%d",
					drm_num_planes(dev));
	}
	if (!plane->name) {
		kfree(plane->format_types);
		kfree(plane->modifiers);
		drm_mode_object_unregister(dev, &plane->base);
		return -ENOMEM;
	}

	memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
	plane->format_count = format_count;
	memcpy(plane->modifiers, format_modifiers,
	       format_modifier_count * sizeof(format_modifiers[0]));
	plane->possible_crtcs = possible_crtcs;
	plane->type = type;

	list_add_tail(&plane->head, &config->plane_list);
	plane->index = config->num_total_plane++;
	if (plane->type == DRM_PLANE_TYPE_OVERLAY)
		config->num_overlay_plane++;

	drm_object_attach_property(&plane->base,
				   config->plane_type_property,
				   plane->type);

	if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {              // (3)
		drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
		drm_object_attach_property(&plane->base, config->prop_in_fence_fd, -1);
		drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
		drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
		drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
		drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
		drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
		drm_object_attach_property(&plane->base, config->prop_src_x, 0);
		drm_object_attach_property(&plane->base, config->prop_src_y, 0);
		drm_object_attach_property(&plane->base, config->prop_src_w, 0);
		drm_object_attach_property(&plane->base, config->prop_src_h, 0);
	}

	if (config->allow_fb_modifiers)
		create_in_format_blob(dev, plane);

	return 0;
}

        (1)创建一个类型为DRM_MODE_OBJECT_PLANE的结构体。

        (2~3)得到properties,并设置。

3. func

3.1 struct drm_plane_funcs

/**
 * struct drm_plane_funcs - driver plane control functions
 */
struct drm_plane_funcs {
 
    /*使能配置plane的crtc和framebuffer.src_x/src_y/src_w/src_h指定fb的源区域
     *crtc_x/crtc_y/crtc_w/crtc_h指定其显示在crtc上的目标区域
     *atomic操作使用drm_atomic_helper_update_plane实现
    */
	int (*update_plane)(struct drm_plane *plane,
			    struct drm_crtc *crtc, struct drm_framebuffer *fb,
			    int crtc_x, int crtc_y,
			    unsigned int crtc_w, unsigned int crtc_h,
			    uint32_t src_x, uint32_t src_y,
			    uint32_t src_w, uint32_t src_h,
			    struct drm_modeset_acquire_ctx *ctx);
 
    /*DRM_IOCTL_MODE_SETPLANE IOCTL调用时,fb id参数,如果为0,就会调用该接口
     *关闭plane, atomic modeset使用drm_atomic_helper_disable_plane()实现该hook
     */
	int (*disable_plane)(struct drm_plane *plane,
			     struct drm_modeset_acquire_ctx *ctx);
 
 
    //该接口仅在driver卸载的时候通过drm_mode_config_cleanup()调用来清空plane资源
	void (*destroy)(struct drm_plane *plane);
 
    /*reset plane的软硬件状态, 通过drm_mode_config_reset接口调用
     *atomic drivers 使用drm_atomic_helper_plane_reset()实现该hook*/
	void (*reset)(struct drm_plane *plane);
 
    /*设置属性的legacy入口, atomic drivers不使用*/
	int (*set_property)(struct drm_plane *plane,
			    struct drm_property *property, uint64_t val);
 
 
    /*复制plane state, atomic driver必须要有该接口
     *drm_atomic_get_plane_state会调用到该hook*/
	struct drm_plane_state *(*atomic_duplicate_state)(struct drm_plane *plane);
 
    //销毁plane state
	void (*atomic_destroy_state)(struct drm_plane *plane,
				     struct drm_plane_state *state);
 
	
    /*设置驱动自定义的属性, 通过drm_atomic_plane_set_property调用*/
	int (*atomic_set_property)(struct drm_plane *plane,
				   struct drm_plane_state *state,
				   struct drm_property *property,
				   uint64_t val);
 
    //获取驱动自定义的属性, 通过drm_atomic_plane_get_property调用
	int (*atomic_get_property)(struct drm_plane *plane,
				   const struct drm_plane_state *state,
				   struct drm_property *property,
				   uint64_t *val);
	
    //drm_dev_register接口调用后,调用该hook注册额外的用户接口
	int (*late_register)(struct drm_plane *plane);
 
    //drm_dev_unregister()前调用,unregister用户空间接口
	void (*early_unregister)(struct drm_plane *plane);
 
 
    //打印plane state, drm_atomic_plane_print_state()会调用
	void (*atomic_print_state)(struct drm_printer *p,
				   const struct drm_plane_state *state);
 
    //检查format和modifier是否有效
	bool (*format_mod_supported)(struct drm_plane *plane, uint32_t format,
				     uint64_t modifier);
};

3.2 struct drm_plane_helper_funcs

 
struct drm_plane_helper_funcs {
	
    //准备fb,主要包括设置fb fence,  映射fb虚拟地址等
	int (*prepare_fb)(struct drm_plane *plane,
			  struct drm_plane_state *new_state);
 
    //和prepare_fb是相反的操作,比如解除fb虚拟地址的映射
	void (*cleanup_fb)(struct drm_plane *plane,
			   struct drm_plane_state *old_state);
 
    //可选的检查plane属性的约束项
	int (*atomic_check)(struct drm_plane *plane,
			    struct drm_atomic_state *state);
 
    /*更新plane的属性状态到软硬件中, 这个接口才是真正更新参数*/
	void (*atomic_update)(struct drm_plane *plane,
			      struct drm_atomic_state *state);
    //disable plane 非必需,不做介绍
	void (*atomic_disable)(struct drm_plane *plane,
			       struct drm_atomic_state *state);
 
    //异步检查 后续了解
	int (*atomic_async_check)(struct drm_plane *plane,
				  struct drm_atomic_state *state);
    //异步更新,后续了解
	void (*atomic_async_update)(struct drm_plane *plane,
				    struct drm_atomic_state *state);
};

 类似资料: