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 uint64_t denominator = (uint64_t)( info.upper_margin + info.lower_margin + info.yres ) 71 * ( info.left_margin + info.right_margin + info.xres ) 72 * info.pixclock; 73 int refreshRate = denominator ? (1000000000000000LLU / denominator) : 0; 74 75 float xdpi = (info.xres * 25.4f) / info.width; 76 float ydpi = (info.yres * 25.4f) / info.height; 77 float fps = refreshRate / 1000.0f; 78 79 printf( "using (fd=%d)\n" 80 "id = %s\n" 81 "xres = %d px\n" 82 "yres = %d px\n" 83 "xres_virtual = %d px\n" 84 "yres_virtual = %d px\n" 85 "bpp = %d\n" 86 "r = %2u:%u\n" 87 "g = %2u:%u\n" 88 "b = %2u:%u\n", 89 fd, 90 finfo.id, 91 info.xres, 92 info.yres, 93 info.xres_virtual, 94 info.yres_virtual, 95 info.bits_per_pixel, 96 info.red.offset, info.red.length, 97 info.green.offset, info.green.length, 98 info.blue.offset, info.blue.length 99 ); 100 101 printf( "width = %d mm (%f dpi)\n" 102 "height = %d mm (%f dpi)\n" 103 "refresh rate = %.2f Hz\n", 104 info.width, xdpi, 105 info.height, ydpi, 106 fps 107 ); 108 109 printf("upper_margin=%d, lower_margin=%d, left_margin=%d, right_margin=%d, pixclock=%d, finfo.smem_len=%d\n", 110 info.upper_margin, info.lower_margin, info.left_margin, info.right_margin, info.pixclock, finfo.smem_len); 111 112 if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) 113 return -errno; 114 115 if (finfo.smem_len <= 0) 116 return -errno; 117 118 /* 119 * Open and map the display. 120 */ 121 122 uint16_t* buffer = (uint16_t*) mmap( 123 0, finfo.smem_len, 124 PROT_READ | PROT_WRITE, 125 MAP_SHARED, 126 fd, 0); 127 128 if (buffer == MAP_FAILED) 129 return -errno; 130 131 // at least for now, always clear the fb 132 memset(buffer, 0, finfo.smem_len); 133 memset(buffer, 0xff, 320*(info.yres_virtual/2)*2); 134 135 int l,t,w,h; 136 l=0; 137 t=0; 138 w=320; 139 h=480; 140 info.reserved[0] = 0x54445055; // "UPDT"; 141 info.reserved[1] = (uint16_t)l | ((uint32_t)t << 16); 142 info.reserved[2] = (uint16_t)(l+w) | ((uint32_t)(t+h) << 16); 143 144 int err; 145 int c = 0; 146 int64_t time = systemTime(); 147 while (1) { 148 149 info.activate = FB_ACTIVATE_VBL; 150 info.yoffset = 0; 151 ioctl(fd, FBIOPUT_VSCREENINFO, &info); 152 153 info.activate = FB_ACTIVATE_VBL; 154 info.yoffset = info.yres_virtual/2; 155 err = ioctl(fd, FBIOPUT_VSCREENINFO, &info); 156 157 c+=2; 158 if (c==60*2) { 159 int64_t now = systemTime(); 160 time = now - time; 161 printf("refresh rate = %f Hz\n", (c*1000000000.0 / (double)time)); 162 c = 0; 163 time = now; 164 } 165 } 166 return 0; 167 } 168