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