整理一篇Linux drm顯示系統的文章
這篇文章主要是回答一位同學的提問,當然也是做一次總結,我相信關注我號的很多人也有做LCD相關的驅動或者系統開發,即使不是專門做LCD,但是在開發過程中也難免會遇到這樣或者那樣的問題。
所以找了幾篇和drm不錯的文章分享給大家,Linux是一個模塊化非常明顯的系統,每個子系統又會有屬于自己的一些特性,學習的時候,最好也是分類學習比較好。
Linux 的 2 種顯示方案
包括:
- FBDEV: Framebuffer Device
- DRM/KMS: Direct Rendering Manager / Kernel Mode Setting
它們有什么區別?
- FBDEV:
- 傳統的顯示框架;
- 簡單,但是只能提供最基礎的顯示功能;
- 無法滿足當前上層應用和底層硬件的顯示需求;
- DRM/KMS:
- 目前主流的顯示方案;
- 為了適應當前日益更新的顯示硬件;
- 軟件上能支持更多高級的控制和特性;
簡單的說就是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的,可以找到一些更優秀的資源。