您尚未登录。

楼主 # 2023-11-27 11:14:48

ubuntu
会员
注册时间: 2020-03-30
已发帖子: 276
积分: 254

一个简单的 linux fb0 双缓冲切换demo

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <linux/fb.h>

#define WIDTH 480
#define HEIGHT 480
#define BYTES_PER_PIXEL 4

int main() {
    int fb = open("/dev/fb0", O_RDWR);
    if (fb == -1) {
        perror("Unable to open framebuffer");
        return 1;
    }

    struct fb_var_screeninfo vinfo;

    ioctl(fb, FBIOGET_VSCREENINFO, &vinfo);

    printf("vinfo.xres=%d, vinfo.yres=%d\n", vinfo.xres, vinfo.yres);
    printf("vinfo.xres_virtual=%d, vinfo.yres_virtual=%d\n", vinfo.xres_virtual, vinfo.yres_virtual);

    ioctl(fb, FBIOGET_VSCREENINFO, &vinfo);

    unsigned char *fbp = mmap(0, vinfo.yres_virtual * vinfo.xres * BYTES_PER_PIXEL, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0);

    // 填充第一个缓冲区
    for (int y = 0; y < vinfo.yres; y++)
        for (int x = 0; x < vinfo.xres; x++)
            *((unsigned long*)(fbp + 4*x + 4*y*vinfo.xres)) = 0xFF00FFFF;

    // 填充第二个缓冲区
    for (int y = vinfo.yres; y < 2*vinfo.yres; y++)
        for (int x = 0; x < vinfo.xres; x++)
            *((unsigned long*)(fbp + 4*x + 4*y*vinfo.xres)) = 0xFFFF00FF;



    int i = 0;
    while(1)
    {
        // 切换到第二个缓冲区
        vinfo.yoffset = (i++%2)*HEIGHT;
        printf("vinfo.yoffset = %d\n", vinfo.yoffset);

        if(0 != ioctl(fb, FBIOPAN_DISPLAY, &vinfo))
        {
                printf("fb iopan display error.\n");
        }

        sleep(1);
    }


    munmap(fbp, vinfo.yres_virtual * vinfo.xres * BYTES_PER_PIXEL);
    close(fb);

    return 0;
}

离线

楼主 #1 2023-11-27 11:18:19

ubuntu
会员
注册时间: 2020-03-30
已发帖子: 276
积分: 254

Re: 一个简单的 linux fb0 双缓冲切换demo

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn