Home | History | Annotate | Download | only in framebuffer
      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <stdint.h>
      5 #include <sys/types.h>
      6 
      7 #include <unistd.h>
      8 #include <fcntl.h>
      9 #include <math.h>
     10 #include <time.h>
     11 #include <errno.h>
     12 
     13 #include <sys/resource.h>
     14 #include <sys/syscall.h>
     15 #include <sys/mman.h>
     16 
     17 #include <linux/fb.h>
     18 
     19 int64_t systemTime()
     20 {
     21     struct timespec t;
     22     t.tv_sec = t.tv_nsec = 0;
     23     clock_gettime(CLOCK_MONOTONIC, &t);
     24     return (int64_t)(t.tv_sec)*1000000000LL + t.tv_nsec;
     25 }
     26 
     27 int main(int argc, char** argv)
     28 {
     29     char const * const device_template[] = {
     30             "/dev/graphics/fb%u",
     31             "/dev/fb%u",
     32             0 };
     33     int fd = -1;
     34     int i=0;
     35     int j=0;
     36     char name[64];
     37     while ((fd==-1) && device_template[i]) {
     38         snprintf(name, 64, device_template[i], 0);
     39         fd = open(name, O_RDWR, 0);
     40         i++;
     41     }
     42     if (fd < 0)
     43         return -errno;
     44 
     45     struct fb_fix_screeninfo finfo;
     46     if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
     47         return -errno;
     48 
     49     struct fb_var_screeninfo info;
     50     if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
     51         return -errno;
     52 
     53     info.reserved[0] = 0;
     54     info.reserved[1] = 0;
     55     info.reserved[2] = 0;
     56     info.xoffset = 0;
     57     info.yoffset = 0;
     58     info.bits_per_pixel = 16;
     59     info.activate = FB_ACTIVATE_NOW;
     60 
     61     if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
     62         printf("FBIOPUT_VSCREENINFO failed (%d x %d)\n",
     63                 info.xres_virtual, info.yres_virtual);
     64         return 0;
     65     }
     66 
     67     if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1)
     68         return -errno;
     69 
     70     int refreshRate = 1000000000000000LLU /
     71             (
     72           (uint64_t)( info.upper_margin + info.lower_margin + info.yres )
     73                   * ( info.left_margin  + info.right_margin + info.xres )
     74                   * info.pixclock
     75             );
     76 
     77     float xdpi = (info.xres * 25.4f) / info.width;
     78     float ydpi = (info.yres * 25.4f) / info.height;
     79     float fps  = refreshRate / 1000.0f;
     80 
     81     printf( "using (fd=%d)\n"
     82             "id           = %s\n"
     83             "xres         = %d px\n"
     84             "yres         = %d px\n"
     85             "xres_virtual = %d px\n"
     86             "yres_virtual = %d px\n"
     87             "bpp          = %d\n"
     88             "r            = %2u:%u\n"
     89             "g            = %2u:%u\n"
     90             "b            = %2u:%u\n",
     91                 fd,
     92                 finfo.id,
     93                 info.xres,
     94                 info.yres,
     95                 info.xres_virtual,
     96                 info.yres_virtual,
     97                 info.bits_per_pixel,
     98                 info.red.offset, info.red.length,
     99                 info.green.offset, info.green.length,
    100                 info.blue.offset, info.blue.length
    101         );
    102 
    103     printf( "width        = %d mm (%f dpi)\n"
    104             "height       = %d mm (%f dpi)\n"
    105             "refresh rate = %.2f Hz\n",
    106                 info.width,  xdpi,
    107                 info.height, ydpi,
    108                 fps
    109         );
    110 
    111     printf("upper_margin=%d, lower_margin=%d, left_margin=%d, right_margin=%d, pixclock=%d, finfo.smem_len=%d\n",
    112             info.upper_margin, info.lower_margin, info.left_margin, info.right_margin, info.pixclock, finfo.smem_len);
    113 
    114     if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1)
    115         return -errno;
    116 
    117     if (finfo.smem_len <= 0)
    118         return -errno;
    119 
    120     /*
    121      * Open and map the display.
    122      */
    123 
    124     uint16_t* buffer  = (uint16_t*) mmap(
    125             0, finfo.smem_len,
    126             PROT_READ | PROT_WRITE,
    127             MAP_SHARED,
    128             fd, 0);
    129 
    130     if (buffer == MAP_FAILED)
    131         return -errno;
    132 
    133     // at least for now, always clear the fb
    134     memset(buffer, 0, finfo.smem_len);
    135     memset(buffer, 0xff, 320*(info.yres_virtual/2)*2);
    136 
    137     int l,t,w,h;
    138     l=0;
    139     t=0;
    140     w=320;
    141     h=480;
    142     info.reserved[0] = 0x54445055; // "UPDT";
    143     info.reserved[1] = (uint16_t)l | ((uint32_t)t << 16);
    144     info.reserved[2] = (uint16_t)(l+w) | ((uint32_t)(t+h) << 16);
    145 
    146     int err;
    147     int c = 0;
    148     int64_t time = systemTime();
    149     while (1) {
    150 
    151         info.activate = FB_ACTIVATE_VBL;
    152         info.yoffset = 0;
    153         ioctl(fd, FBIOPUT_VSCREENINFO, &info);
    154 
    155         info.activate = FB_ACTIVATE_VBL;
    156         info.yoffset = info.yres_virtual/2;
    157         err = ioctl(fd, FBIOPUT_VSCREENINFO, &info);
    158 
    159         c+=2;
    160         if (c==60*2) {
    161             int64_t now = systemTime();
    162             time = now - time;
    163             printf("refresh rate = %f Hz\n", (c*1000000000.0 / (double)time));
    164             c = 0;
    165             time = now;
    166         }
    167     }
    168     return 0;
    169 }
    170