在 fb_var_screeninfo 中设置 yres_virtual 时出现无效参数错误

2023-12-04

我正在尝试为 Linux 创建一个直接写入帧缓冲区 /dev/fb0 的应用程序。为了使其成为双缓冲,我尝试使虚拟屏幕成为屏幕大小的两倍。这是我写的程序:

struct fb_var_screeninfo screeninfo_var;
struct fb_fix_screeninfo screeninfo_fixed;
unsigned int* screenbuffer;

void gfx_init()
{
    fb0 = open("/dev/fb0", O_RDWR);
    if(fb0 == 0)
        error("Could not open framebuffer located in /dev/fb0!");

    if (ioctl(fb0, FBIOGET_FSCREENINFO, &screeninfo_fixed) == -1)
        error("Could not retrive fixed screen info!");

    if (ioctl(fb0, FBIOGET_VSCREENINFO, &screeninfo_var) == -1)
        error("Could not retrive variable screen info!");

    screeninfo_var.xres_virtual = screeninfo_var.xres;
    screeninfo_var.yres_virtual = screeninfo_var.yres * 2;
    screeninfo_var.width = screeninfo_var.xres;
    screeninfo_var.height = screeninfo_var.yres;
    screeninfo_var.xoffset = 0;
    screeninfo_var.yoffset = 0;

    if (ioctl(fb0, FBIOPUT_VSCREENINFO, &screeninfo_var) == -1)
        error("Could not set variable screen info!");

    info("Detected monitor of %ix%i pixels using %i bit colors.",screeninfo_var.xres, screeninfo_var.yres, screeninfo_var.bits_per_pixel);

    screenbuffersize = screeninfo_var.xres_virtual * screeninfo_var.yres_virtual * screeninfo_var.bits_per_pixel/8;
    screenbuffer = (unsigned int *)mmap(0, screenbuffersize, PROT_READ | PROT_WRITE, MAP_SHARED, fb0, 0);
    if( (long)screenbuffer == 0 || (long)screenbuffer == -1 )
        error("Failed to map framebuffer to device memory!");
}

该程序失败于ioctl(fb0, FBIOPUT_VSCREENINFO, &screeninfo_var)报告错误无效参数。拆线时screeninfo_var.yres_virtual = screeninfo_var.yres * 2;它运行良好(但没有双缓冲)。

有人看到我在这里做错了什么吗?


为了避免将来的麻烦,有一种方法可以在 Linux 上使用低级图形(例如 /dev/fb0)正确地进行双缓冲。但是根据这个线程:通过创建一个大小为原始帧缓冲区两倍的虚拟帧缓冲区,不可能真正对帧缓冲区进行双倍缓冲(我读到,树莓派可能是此规则的例外,因为它由不同的驱动程序支持)。

在 Linux 上使用低级图形进行双缓冲的正确方法是通过 libdrm(或 /dev/dri/card0)。我在这里遵循了一个非常好的例子:https://github.com/dvdhrm/docs/blob/master/drm-howto/modeset-vsync.c。将 /dev/fb0 代码转换为 /dev/dri/card0 代码并不难。

无论如何,我希望我已经帮助了将来可能遇到此问题的人。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 fb_var_screeninfo 中设置 yres_virtual 时出现无效参数错误 的相关文章

随机推荐