DRM(10) -- VBlank

Overview 一篇关于垂直同步 V-Sync 解释的文章:https://daily.elepover.com/2021/03/27/vsync/index.html 当 GPU 渲染的速度 > 显示器刷新的速度时,GPU 在显示器还来不及完成渲染完一帧时,就切换了 framebuffer,会导致出现撕裂现象。 帧率大于显示器刷新率时,启用垂直同步。 帧率小于显示器刷新率时,禁用垂直同步。 为了支持 vblank,底层 drvier 需要调用 drm_vblank_init() 初始化,另外需要实现 drm_crtc_funcs.enable_vblank 和 drm_crtc_funcs.disable_vblank 两个回调函数,并且在 vblank 中断中调用 drm_crtc_handle_vblank()。 数据结构 每个 crtc 对应一个 struct drm_vblank_crtc 结构体。 struct drm_vblank_crtc { struct drm_device *dev; wait_queue_head_t queue; struct timer_list disable_timer; seqlock_t seqlock; atomic64_t count; ktime_t time; atomic_t refcount; u32 last; u32 max_vblank_count; unsigned int inmodeset; unsigned int pipe; int framedur_ns; int linedur_ns; struct drm_display_mode hwmode; bool enabled; struct kthread_worker *worker; struct list_head pending_work; wait_queue_head_t work_wait_queue; }; pipe: 表示第几个 crtc,在 drm_vblank_init 中初始化。...

2024-08-28 · 3 min

DRM(2) -- KMS Core

Kernel Mode Setting (KMS) driver 通过 drmm_mode_config_init() 来初始化 mode setting. Overview KMS pipeline: 一个 crtc 可以有多个 encoder, 一个 encoder 对应一个 connector, 并且中间可以接任意个 bridge. 通常对于有 bridge 的 driver, encoder 层不需要任何代码。 Data Structure and api struct drm_mode_config_funcs mode setting functions, 该结构体需要在底层 driver 中初始化。 struct drm_mode_config_funcs { struct drm_framebuffer *(*fb_create)(struct drm_device *dev, struct drm_file *file_priv, const struct drm_mode_fb_cmd2 *mode_cmd); const struct drm_format_info *(*get_format_info)(const struct drm_mode_fb_cmd2 *mode_cmd); enum drm_mode_status (*mode_valid)(struct drm_device *dev, const struct drm_display_mode *mode); int (*atomic_check)(struct drm_device *dev, struct drm_atomic_state *state); int (*atomic_commit)(struct drm_device *dev,struct drm_atomic_state *state, bool nonblock); }; fb_create: 创建 framebuffer 的回调函数,在 userspace 调用 drmModeAddFB2() 之后会调用到。....

2024-08-28 · 3 min

DRM(6) -- Plane

在扫描输出过程中,一个平面(plane)代表一个图像源,CRTC 可以对平面进行混合(blend)或叠加(overlaid)显示。plane 从 drm_framebuffer 获取输入数据。平面本身定义了图像的裁剪(cropping)和缩放(scaling)方式,以及它在 display pipeline 可见区域中的位置。平面还可以具有额外的属性来指定平面像素的定位和混合方式,比如旋转(rotation)或 Z 轴位置(Z-position)。所有这些属性都存储在 drm_plane_state 中。 plane 由 struct drm_plane 表示,使用drm_universal_plane_init() 初始化。 每个 plane 都有一个类型,参考 enum drm_plane_type。 每个 CRTC 都必须要有一个 primary plane。 Data structure and api struct drm_plane { struct drm_device *dev; struct list_head head; char *name; struct drm_modeset_lock mutex; struct drm_mode_object base; uint32_t possible_crtcs; uint32_t *format_types; unsigned int format_count; uint64_t *modifiers; unsigned int modifier_count; const struct drm_plane_funcs *funcs; struct drm_object_properties properties; enum drm_plane_type type; unsigned index; const struct drm_plane_helper_funcs *helper_private; struct drm_plane_state *state; struct drm_property *alpha_property; struct drm_property *zpos_property; struct drm_property *rotation_property; struct drm_property *blend_mode_property; struct drm_property *color_encoding_property; struct drm_property *color_range_property; struct drm_property *scaling_filter_property; struct drm_property *hotspot_x_property; struct drm_property *hotspot_y_property; }; possible_crtcs: 该 plane 支持的 crtc 类型,在 plane 初始化时传入。...

2024-08-28 · 4 min

DRM(5) -- FrameBuffer

Framebuffer 帧缓冲区是抽象的内存对象,提供了扫描到 CRTC 的像素源。应用程序通过 IOCTLDRM_IOCTL_MODE_ADDFB(2)来创建 framebuffer,会返回一个不透明句柄,该句柄可以传递给 KMS CRTC、来控制 plane configuration 和 page flip 功能。 framebuffer 依赖底层的内存管理器来分配内存。创建 framebuffer 时,app 需要通过struct drm_mode_fb_cmd2传入一个 memory handle。 Data structures and apis struct drm_framebuffer { struct drm_device *dev; struct list_head head; struct drm_mode_object base; const struct drm_format_info *format; const struct drm_framebuffer_funcs *funcs; unsigned int pitches[DRM_FORMAT_MAX_PLANES]; unsigned int offsets[DRM_FORMAT_MAX_PLANES]; uint64_t modifier; unsigned int width; unsigned int height; int flags; struct list_head filp_head; struct drm_gem_object *obj[DRM_FORMAT_MAX_PLANES]; }; head: fb 链表节点。...

2024-08-28 · 4 min

DRM -- Graphic Introduction

Reference https://docs.kernel.org/gpu/index.html introduction 中提供了一些关于 DRM 的讲座和 slides 材料。 https://bootlin.com/doc/training/graphics/graphics-slides.pdf Basic Theory and Concepts About Graphics YUV 数据格式 采样 YUV 格式,由一个 Y 的亮度分量(Luma)和 U(蓝色投影 Cb)和 V(红色投影 Cr)的色度分量(Chroma)表示。 4:4:4 表示不降低色度(UV)通道的采样率。每个 Y 分量对应一组 UV 分量。 4:2:2 表示 2:1 水平下采样,没有垂直下采样。每两个 Y 分量共享一组 UV 分量。 4:2:0 表示 2:1 水平下采样,同时 2:1 垂直下采样。每四个 Y 分量共享一组 UV 分量。 4:1:1 表示 4:1 水平下采样,没有垂直下采样。每四个 Y 分量共享一组 UV 分量。 BT.601 中规定的 YUV 和 RGB 的转换公式: \(R=Y+1.140*V-0.7\) \(G=Y-0.343*U-0.711*V+0.526\) \(B=Y+1.765*U-0.883\) \(Y=0.299*R+0.587*G+0.114*B\) \(U=-0.169*R-0.331*G+0.500*B\) \(V=0.500*R-0.439*G-0.081\*B\)...

2024-08-20 · 1 min