if ((fb_info->fd = open("/dev/fb0", O_RDWR)) < 0) { printf("open fb error/n"); return -1; }
时间: 2024-04-26 21:25:06 浏览: 8
这行代码打开了 Linux 系统中的 /dev/fb0 设备文件,并将其返回值保存在 fb_info->fd 变量中。如果返回值小于 0,说明设备文件打开失败,程序在控制台输出 "open fb error" 然后返回 -1 表示出现错误。该设备文件代表了系统中的主 framebuffer 设备,通常是显卡的显存区域。接下来的代码将通过该设备文件访问显存中的像素数据。
相关问题
int fb_init(st_fb_info *fb_info) { struct fb_var_screeninfo vinfo; /* Open video memory */ if ((fb_info->fd = open("/dev/fb0", O_RDWR)) < 0) { printf("open fb error/n"); return -1; } /* Get variable display parameters */ if (ioctl(fb_info->fd, FBIOGET_VSCREENINFO, &vinfo)) { printf("Bad vscreeninfo ioctl/n"); return -2; } fb_info->width = vinfo.xres; fb_info->height = vinfo.yres; fb_info->bytes_per_pixel = vinfo.bits_per_pixel/8; fb_info->fbsize = fb_info->width * fb_info->height * fb_info->bytes_per_pixel; /* Map video memory */ if ((fb_info->fbbuf = mmap(0, fb_info->fbsize, PROT_READ | PROT_WRITE, MAP_SHARED, fb_info->fd, 0)) == (void *) -1) { return -3; } return 0; }
这是一个用于初始化 Linux framebuffer 的函数。Framebuffer 是指显卡中的一个内存区域,用于存储屏幕上的像素数据。程序首先打开 /dev/fb0 设备文件,然后通过 ioctl() 函数获取显卡的变量参数,如屏幕分辨率、像素位数等。接着,程序使用 mmap() 函数将显存映射到进程地址空间中的 fb_info->fbbuf 指针所指向的内存区域,使得程序可以直接访问显存中的像素数据。最后,函数返回 0 表示初始化成功,否则返回负数表示出现了错误。
使用drm的over_plane显示YUV420像素格式的示例C语言代码
以下是使用 DRM 和 Over Plane 显示 YUV420 像素格式的示例 C 代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#define WIDTH 640
#define HEIGHT 480
int main(int argc, char *argv[])
{
int fd;
drmModeCrtcPtr crtc;
drmModePlaneResPtr plane_resources;
drmModePlanePtr plane;
drmModeObjectPropertiesPtr props;
uint32_t crtc_id, plane_id, blob_id;
uint32_t format = DRM_FORMAT_YUV420;
uint32_t handles[3], pitches[3], offsets[3];
uint32_t fb_id, bo_handles[4], flags = 0;
uint8_t *buf;
int ret, i, j;
/* Open the DRM device */
fd = open("/dev/dri/card0", O_RDWR);
if (fd < 0) {
fprintf(stderr, "Failed to open DRM device: %s\n", strerror(errno));
return -1;
}
/* Get the CRTC information */
crtc = drmModeGetCrtc(fd, 0);
if (!crtc) {
fprintf(stderr, "Failed to get CRTC information: %s\n", strerror(errno));
close(fd);
return -1;
}
crtc_id = crtc->crtc_id;
/* Get the plane resources */
plane_resources = drmModeGetPlaneResources(fd);
if (!plane_resources) {
fprintf(stderr, "Failed to get plane resources: %s\n", strerror(errno));
drmModeFreeCrtc(crtc);
close(fd);
return -1;
}
/* Find the primary plane */
for (i = 0; i < plane_resources->count_planes; i++) {
plane = drmModeGetPlane(fd, plane_resources->planes[i]);
if (!plane) {
fprintf(stderr, "Failed to get plane information: %s\n", strerror(errno));
continue;
}
props = drmModeObjectGetProperties(fd, plane->plane_id, DRM_MODE_OBJECT_PLANE);
for (j = 0; j < props->count_props; j++) {
drmModePropertyPtr prop = drmModeGetProperty(fd, props->props[j]);
if (!strcmp(prop->name, "type") && props->prop_values[j] == DRM_PLANE_TYPE_PRIMARY) {
plane_id = plane->plane_id;
drmModeFreeProperty(prop);
break;
}
drmModeFreeProperty(prop);
}
drmModeFreeObjectProperties(props);
drmModeFreePlane(plane);
}
/* Create the buffer object */
buf = malloc(WIDTH * HEIGHT * 3 / 2);
if (!buf) {
fprintf(stderr, "Failed to allocate buffer: %s\n", strerror(errno));
drmModeFreePlaneResources(plane_resources);
drmModeFreeCrtc(crtc);
close(fd);
return -1;
}
memset(buf, 0x80, WIDTH * HEIGHT * 3 / 2);
ret = drmPrimeFDToHandle(fd, STDIN_FILENO, &handles[0]);
if (ret < 0) {
fprintf(stderr, "Failed to get handle for file descriptor: %s\n", strerror(errno));
free(buf);
drmModeFreePlaneResources(plane_resources);
drmModeFreeCrtc(crtc);
close(fd);
return -1;
}
/* Configure the buffer object */
pitches[0] = WIDTH;
pitches[1] = pitches[2] = WIDTH / 2;
offsets[0] = 0;
offsets[1] = WIDTH * HEIGHT;
offsets[2] = offsets[1] + WIDTH * HEIGHT / 4;
bo_handles[0] = handles[0];
bo_handles[1] = bo_handles[2] = bo_handles[3] = 0;
ret = drmModeAddFB2(fd, WIDTH, HEIGHT, format, bo_handles, pitches, offsets, &fb_id, flags);
if (ret < 0) {
fprintf(stderr, "Failed to add frame buffer: %s\n", strerror(errno));
free(buf);
drmModeFreePlaneResources(plane_resources);
drmModeFreeCrtc(crtc);
close(fd);
return -1;
}
/* Set the plane mode */
ret = drmModeSetPlane(fd, plane_id, crtc_id, fb_id, 0, 0, 0, WIDTH, HEIGHT, 0, 0, WIDTH << 16, HEIGHT << 16);
if (ret < 0) {
fprintf(stderr, "Failed to set plane mode: %s\n", strerror(errno));
drmModeRmFB(fd, fb_id);
free(buf);
drmModeFreePlaneResources(plane_resources);
drmModeFreeCrtc(crtc);
close(fd);
return -1;
}
/* Wait for input */
printf("Press any key to exit...\n");
getchar();
/* Clean up */
drmModeRmFB(fd, fb_id);
free(buf);
drmModeFreePlaneResources(plane_resources);
drmModeFreeCrtc(crtc);
close(fd);
return 0;
}
```
这是一个简单的示例代码,它打开 DRM 设备,获取 CRTC 信息,找到主平面,创建 YUV420 帧缓冲区对象,将帧缓冲区对象传递给主平面,然后等待输入并在退出时清除资源。请注意,此示例仅涵盖了 DRM 和 Over Plane 基础知识,实际应用需要更多的代码来实现更复杂的功能。