如何使用c语言在framebuffer中绘制图形..?

时间:2009-12-02 05:34:03

标签: c++ c linux framebuffer

我是这个linux framebuffer的新手,所以有人引导我在framebuffer中绘制线图。我有代码在turbo c中绘制图形但现在在linux中。所以请帮帮我。

谢谢你, 拉胡尔

3 个答案:

答案 0 :(得分:19)

open()中的正确文件上使用/dev(例如/dev/fb0),然后使用mmap()将其映射到内存中。如果您不知道如何使用它们,则联机帮助将有助于这些系统调用。

然后ioctl()中的某些<linux/fb.h>有一些结构和常量。像许多内核头文件一样,只需浏览文件就可以学到很多东西。

ioctl FBIOGET_VSCREENINFOstruct fb_var_screeninfo特别有趣。请注意,这有xresyres(分辨率)和bits_per_pixel。然后是FBIOGET_FSCREENINFOstruct fb_fix_screeninfo,其中包含typeline_length等更多信息。

因此(x,y)处的像素可能位于mmap_base_address + x * bits_per_pixel/8 + y * line_length。像素的确切格式取决于您通过ioctl检索的结构;决定如何阅读/写作是你的工作。

我已经用了一段时间了,所以我对更多细节有点朦胧......

这是一个快速而又脏的代码示例,只是为了说明它是如何完成的......我还没有对此进行测试。

#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/mman.h>

#include <linux/fb.h>

#include <unistd.h>
#include <fcntl.h>

#include <stdio.h>

int main()
{
   struct fb_var_screeninfo screen_info;
   struct fb_fix_screeninfo fixed_info;
   char *buffer = NULL;
   size_t buflen;
   int fd = -1;
   int r = 1;

   fd = open("/dev/fb0", O_RDWR);
   if (fd >= 0)
   {
      if (!ioctl(fd, FBIOGET_VSCREENINFO, &screen_info) &&
          !ioctl(fd, FBIOGET_FSCREENINFO, &fixed_info))
      {
         buflen = screen_info.yres_virtual * fixed_info.line_length;
         buffer = mmap(NULL,
                       buflen,
                       PROT_READ|PROT_WRITE,
                       MAP_SHARED,
                       fd,
                       0);
         if (buffer != MAP_FAILED)
         {
            /*
             * TODO: something interesting here.
             * "buffer" now points to screen pixels.
             * Each individual pixel might be at:
             *    buffer + x * screen_info.bits_per_pixel/8
             *           + y * fixed_info.line_length
             * Then you can write pixels at locations such as that.
             */

             r = 0;   /* Indicate success */
         }
         else
         {
            perror("mmap");
         }
      }
      else
      {
         perror("ioctl");
      }
   }
   else
   {
      perror("open");
   }

   /*
    * Clean up
    */
   if (buffer && buffer != MAP_FAILED)
      munmap(buffer, buflen);
   if (fd >= 0)
      close(fd);

   return r;
}

答案 1 :(得分:4)

作为asveikau's answer的替代方案,您可以使用DirectFB,这可能会大大简化您的工作。

答案 2 :(得分:0)

来自我对Rasbian的Synaptic:&#34; DirectFB是一个图形库,其设计考虑了嵌入式系统。它以最少的资源使用和开销提供最大的硬件加速性能。&#34;

无论如何,我还没有在帧缓冲区看到这项工作,但我希望它能像我做的大部分图片一样。你有一些线性地址空间,高度*宽度*字节每像素字节数。如果要写入特定的x,y位置,则该空间中的位置由(y * width * bytes / bytes)+(x * 3)给出。颜色是相邻的字节RGB(通常),因此可以获得红色像素的地址,绿色加1,蓝色加2。你malloc(高度*宽*每个像素的字节数)一个地址空间,写入它,然后选择你的libpng,libjpeg,libtiff将该缓冲区写入文件。如果你想把文字放在里面,你必须自己滚动,所以我从一个旧的libgif偷了一个。我已经达到了年龄和经验水平,自己更容易做到这一点,而不是学习其他人认为应该怎么做。我的图形如下: data taken as CSV from rtl_power

我一直在尝试使用http://raspberrycompote.blogspot.com/2014/04/low-level-graphics-on-raspberry-pi.html所述的帧缓冲,但有些错误。我在Pi 3上,它可能是为2014年的Pi 1而写的,而不是2017年。但Pi与传统的帧缓冲区不同,因为GPU运行该节目。使用此方法:http://elinux.org/RPi_Framebuffer