评论

收藏

[Linux] 整理一篇Linux drm显示系统的文章

服务系统 服务系统 发布于:2021-07-21 21:44 | 阅读数:327 | 评论:0

DSC0000.jpg

这篇文章主要是回答一位同学的提问,当然也是做一次总结,我相信关注我号的很多人也有做LCD相关的驱动或者系统开发,即使不是专门做LCD,但是在开发过程中也难免会遇到这样或者那样的问题。
所以找了几篇和drm不错的文章分享给大家,Linux是一个模块化非常明显的系统,每个子系统又会有属于自己的一些特性,学习的时候,最好也是分类学习比较好。
Linux 的 2 种显示方案
包括:
  • FBDEV: Framebuffer Device
  • DRM/KMS: Direct Rendering Manager / Kernel Mode Setting
它们有什么区别?
  • FBDEV:
    • 传统的显示框架;
    • 简单,但是只能提供最基础的显示功能;
    • 无法满足当前上层应用和底层硬件的显示需求;
  • DRM/KMS:
    • 目前主流的显示方案;
    • 为了适应当前日益更新的显示硬件;
    • 软件上能支持更多高级的控制和特性;

DSC0001.png

简单的说就是FBDEV已经不满足时代的发展需要,然后就出现了DRM这个东西,DRM,英文全称 Direct Rendering Manager, 即 直接渲染管理器。它是为了解决多个程序对 Video Card 资源的协同使用问题而产生的。它向用户空间提供了一组 API,用以访问操纵 GPU。
DRM是一个内核级的设备驱动,可以编译到内核中也可以作为标准模块进行加载。DRM最初是在FreeBSD中出现的,后来被移植到Linux系统中,并成为Linux系统的标准部分。
DRM可以直接访问DRM clients的硬件。DRM驱动用来处理DMA,内存管理,资源锁以及安全硬件访问。为了同时支持多个3D应用,3D图形卡硬件必须作为一个共享资源,因此需要锁来提供互斥访问。DMA传输和AGP接口用来发送图形操作的buffers到显卡硬件,因此要防止客户端越权访问显卡硬件。
Linux DRM层用来支持那些复杂的显卡设备,这些显卡设备通常都包含可编程的流水线,非常适合3D图像加速。内核中的DRM层,使得这些显卡驱动在进行内存管理,中断处理和DMA操作中变得更容易,并且可以为上层应用提供统一的接口。
FBDEV的测试程序
/*  
* Copyright (C) 2011 The Android Open Source Project  
*  
* Licensed under the Apache License, Version 2.0 (the "License");  
* you may not use this file except in compliance with the License.  
* You may obtain a copy of the License at  
*  
*    http://www.apache.org/licenses/LICENSE-2.0  
*  
* Unless required by applicable law or agreed to in writing, software  
* distributed under the License is distributed on an "AS IS" BASIS,  
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
* See the License for the specific language governing permissions and  
* limitations under the License.  
*/  
#include <stdint.h>  
#include <sys/types.h>  
#include <fcntl.h>  
#include <sys/ioctl.h>  
#include <linux/fb.h>  
#include <errno.h>  
#include <string.h>  
#include <stdio.h>  
#ifndef FBIO_WAITFORVSYNC  
#define FBIO_WAITFORVSYNC   _IOW('F', 0x20, __u32)  
#endif  
int main(int argc, char** argv) {  
  int fd = open("/dev/graphics/fb0", O_RDWR);  
  if (fd >= 0) {  
    do {  
      uint32_t crt = 0;  
       int err = ioctl(fd, FBIO_WAITFORVSYNC, &crt);  
       if (err < 0) {  
         printf("FBIO_WAITFORVSYNC error: %s\n", strerror(errno));  
         break;  
       }  
    } while(1);  
    close(fd);  
  }  
  return 0;  
}
DRM应用测试程序
int main(int argc, char **argv)  
{  
int fd;  
drmModeConnector *conn;  
drmModeRes *res;  
uint32_t conn_id;  
uint32_t crtc_id;   
  // 1. 打开设备  
fd = open("/dev/dri/card0", O_RDWR | O_CLOEXEC);  
  // 2. 获得 crtc 和 connector 的 id  
res = drmModeGetResources(fd);  
crtc_id = res->crtcs[0];  
conn_id = res->connectors[0];  
  // 3. 获得 connector  
conn = drmModeGetConnector(fd, conn_id);  
buf.width = conn->modes[0].hdisplay;  
buf.height = conn->modes[0].vdisplay;  
  // 4. 创建 framebuffer  
modeset_create_fb(fd, &buf);  
  // 5. Sets a CRTC configuration,这之后就会开始在 crtc0 + connector0 pipeline 上进行以 mode0 输出显示  
drmModeSetCrtc(fd, crtc_id, buf.fb_id, 0, 0, &conn_id, 1, &conn->modes[0]);  
getchar();  
// 6. cleanup  
...  
return 0;  
}

DRM 相关的驱动很复杂,我并不敢班门弄斧,如果大家只是想了解个大概,我觉得上面的文章应该能够满足你们的需求,但是如果你们是专门做LCD的,可以找到一些更优秀的资源。
原文链接:https://mp.weixin.qq.com/s/T987fYbDE5d3WYuYTkjtFw

关注下面的标签,发现更多相似文章