整理一篇Linux drm顯示系統(tǒng)的文章
這篇文章主要是回答一位同學(xué)的提問,當(dāng)然也是做一次總結(jié),我相信關(guān)注我號(hào)的很多人也有做LCD相關(guān)的驅(qū)動(dòng)或者系統(tǒng)開發(fā),即使不是專門做LCD,但是在開發(fā)過程中也難免會(huì)遇到這樣或者那樣的問題。
所以找了幾篇和drm不錯(cuò)的文章分享給大家,Linux是一個(gè)模塊化非常明顯的系統(tǒng),每個(gè)子系統(tǒng)又會(huì)有屬于自己的一些特性,學(xué)習(xí)的時(shí)候,最好也是分類學(xué)習(xí)比較好。
Linux 的 2 種顯示方案
包括:
- FBDEV: Framebuffer Device
- DRM/KMS: Direct Rendering Manager / Kernel Mode Setting
它們有什么區(qū)別?
- FBDEV:
- 傳統(tǒng)的顯示框架;
- 簡(jiǎn)單,但是只能提供最基礎(chǔ)的顯示功能;
- 無(wú)法滿足當(dāng)前上層應(yīng)用和底層硬件的顯示需求;
- DRM/KMS:
- 目前主流的顯示方案;
- 為了適應(yīng)當(dāng)前日益更新的顯示硬件;
- 軟件上能支持更多高級(jí)的控制和特性;
簡(jiǎn)單的說就是FBDEV已經(jīng)不滿足時(shí)代的發(fā)展需要,然后就出現(xiàn)了DRM這個(gè)東西,DRM,英文全稱 Direct Rendering Manager, 即 直接渲染管理器。它是為了解決多個(gè)程序?qū)?Video Card 資源的協(xié)同使用問題而產(chǎn)生的。它向用戶空間提供了一組 API,用以訪問操縱 GPU。
DRM是一個(gè)內(nèi)核級(jí)的設(shè)備驅(qū)動(dòng),可以編譯到內(nèi)核中也可以作為標(biāo)準(zhǔn)模塊進(jìn)行加載。DRM最初是在FreeBSD中出現(xiàn)的,后來被移植到Linux系統(tǒng)中,并成為L(zhǎng)inux系統(tǒng)的標(biāo)準(zhǔn)部分。
DRM可以直接訪問DRM clients的硬件。DRM驅(qū)動(dòng)用來處理DMA,內(nèi)存管理,資源鎖以及安全硬件訪問。為了同時(shí)支持多個(gè)3D應(yīng)用,3D圖形卡硬件必須作為一個(gè)共享資源,因此需要鎖來提供互斥訪問。DMA傳輸和AGP接口用來發(fā)送圖形操作的buffers到顯卡硬件,因此要防止客戶端越權(quán)訪問顯卡硬件。
Linux DRM層用來支持那些復(fù)雜的顯卡設(shè)備,這些顯卡設(shè)備通常都包含可編程的流水線,非常適合3D圖像加速。內(nèi)核中的DRM層,使得這些顯卡驅(qū)動(dòng)在進(jìn)行內(nèi)存管理,中斷處理和DMA操作中變得更容易,并且可以為上層應(yīng)用提供統(tǒng)一的接口。
FBDEV的測(cè)試程序
- /*
- * 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應(yīng)用測(cè)試程序
- int main(int argc, char **argv)
- {
- int fd;
- drmModeConnector *conn;
- drmModeRes *res;
- uint32_t conn_id;
- uint32_t crtc_id;
- // 1. 打開設(shè)備
- 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. 創(chuàng)建 framebuffer
- modeset_create_fb(fd, &buf);
- // 5. Sets a CRTC configuration,這之后就會(huì)開始在 crtc0 + connector0 pipeline 上進(jìn)行以 mode0 輸出顯示
- drmModeSetCrtc(fd, crtc_id, buf.fb_id, 0, 0, &conn_id, 1, &conn->modes[0]);
- getchar();
- // 6. cleanup
- ...
- return 0;
- }
DRM 相關(guān)的驅(qū)動(dòng)很復(fù)雜,我并不敢班門弄斧,如果大家只是想了解個(gè)大概,我覺得上面的文章應(yīng)該能夠滿足你們的需求,但是如果你們是專門做LCD的,可以找到一些更優(yōu)秀的資源。