source: trunk/examples/qws/framebuffer/main.c@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 16.9 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <stdlib.h>
42#include <unistd.h>
43#include <stdio.h>
44#include <fcntl.h>
45#include <linux/fb.h>
46#include <linux/kd.h>
47#include <sys/mman.h>
48#include <sys/ioctl.h>
49#include <sys/time.h>
50#include <string.h>
51#include <errno.h>
52
53struct fb_var_screeninfo vinfo;
54struct fb_fix_screeninfo finfo;
55char *frameBuffer = 0;
56int fbFd = 0;
57int ttyFd = 0;
58
59void printFixedInfo()
60{
61 printf("Fixed screen info:\n"
62 "\tid: %s\n"
63 "\tsmem_start: 0x%lx\n"
64 "\tsmem_len: %d\n"
65 "\ttype: %d\n"
66 "\ttype_aux: %d\n"
67 "\tvisual: %d\n"
68 "\txpanstep: %d\n"
69 "\typanstep: %d\n"
70 "\tywrapstep: %d\n"
71 "\tline_length: %d\n"
72 "\tmmio_start: 0x%lx\n"
73 "\tmmio_len: %d\n"
74 "\taccel: %d\n"
75 "\n",
76 finfo.id, finfo.smem_start, finfo.smem_len, finfo.type,
77 finfo.type_aux, finfo.visual, finfo.xpanstep, finfo.ypanstep,
78 finfo.ywrapstep, finfo.line_length, finfo.mmio_start,
79 finfo.mmio_len, finfo.accel);
80}
81
82void printVariableInfo()
83{
84 printf("Variable screen info:\n"
85 "\txres: %d\n"
86 "\tyres: %d\n"
87 "\txres_virtual: %d\n"
88 "\tyres_virtual: %d\n"
89 "\tyoffset: %d\n"
90 "\txoffset: %d\n"
91 "\tbits_per_pixel: %d\n"
92 "\tgrayscale: %d\n"
93 "\tred: offset: %2d, length: %2d, msb_right: %2d\n"
94 "\tgreen: offset: %2d, length: %2d, msb_right: %2d\n"
95 "\tblue: offset: %2d, length: %2d, msb_right: %2d\n"
96 "\ttransp: offset: %2d, length: %2d, msb_right: %2d\n"
97 "\tnonstd: %d\n"
98 "\tactivate: %d\n"
99 "\theight: %d\n"
100 "\twidth: %d\n"
101 "\taccel_flags: 0x%x\n"
102 "\tpixclock: %d\n"
103 "\tleft_margin: %d\n"
104 "\tright_margin: %d\n"
105 "\tupper_margin: %d\n"
106 "\tlower_margin: %d\n"
107 "\thsync_len: %d\n"
108 "\tvsync_len: %d\n"
109 "\tsync: %d\n"
110 "\tvmode: %d\n"
111 "\n",
112 vinfo.xres, vinfo.yres, vinfo.xres_virtual, vinfo.yres_virtual,
113 vinfo.xoffset, vinfo.yoffset, vinfo.bits_per_pixel, vinfo.grayscale,
114 vinfo.red.offset, vinfo.red.length, vinfo.red.msb_right,
115 vinfo.green.offset, vinfo.green.length, vinfo.green.msb_right,
116 vinfo.blue.offset, vinfo.blue.length, vinfo.blue.msb_right,
117 vinfo.transp.offset, vinfo.transp.length, vinfo.transp.msb_right,
118 vinfo.nonstd, vinfo.activate, vinfo.height, vinfo.width,
119 vinfo.accel_flags, vinfo.pixclock, vinfo.left_margin,
120 vinfo.right_margin, vinfo.upper_margin, vinfo.lower_margin,
121 vinfo.hsync_len, vinfo.vsync_len, vinfo.sync, vinfo.vmode);
122}
123
124long switchToGraphicsMode()
125{
126 const char *const devs[] = {"/dev/tty0", "/dev/tty", "/dev/console", 0};
127 const char * const *dev;
128 long oldMode = KD_TEXT;
129
130 for (dev = devs; *dev; ++dev) {
131 ttyFd = open(*dev, O_RDWR);
132 if (ttyFd != -1)
133 break;
134 printf("Opening tty device %s failed: %s\n", *dev, strerror(errno));
135 }
136
137 ioctl(ttyFd, KDGETMODE, &oldMode);
138 if (oldMode == KD_GRAPHICS) {
139 printf("Was in graphics mode already. Skipping\n");
140 return oldMode;
141 }
142 int ret = ioctl(ttyFd, KDSETMODE, KD_GRAPHICS);
143 if (ret == -1) {
144 printf("Switch to graphics mode failed: %s\n", strerror(errno));
145 return oldMode;
146 }
147
148 printf("Successfully switched to graphics mode.\n\n");
149
150 return oldMode;
151}
152
153void restoreTextMode(long oldMode)
154{
155 if (ttyFd == -1)
156 return;
157
158 ioctl(ttyFd, KDSETMODE, oldMode);
159 close(ttyFd);
160}
161
162struct fb_cmap oldPalette;
163struct fb_cmap palette;
164int paletteSize = 0;
165
166void initPalette_16()
167{
168 if (finfo.type == FB_TYPE_PACKED_PIXELS) {
169 // We'll setup a grayscale map for 4bpp linear
170 int val = 0;
171 int i;
172 for (i = 0; i < 16; ++i) {
173 palette.red[i] = (val << 8) | val;
174 palette.green[i] = (val << 8) | val;
175 palette.blue[i] = (val << 8) | val;
176 val += 17;
177 }
178 return;
179 }
180
181 // Default 16 colour palette
182 unsigned char reds[16] = { 0x00, 0x7F, 0xBF, 0xFF, 0xFF, 0xA2,
183 0x00, 0xFF, 0xFF, 0x00, 0x7F, 0x7F,
184 0x00, 0x00, 0x00, 0x82 };
185 unsigned char greens[16] = { 0x00, 0x7F, 0xBF, 0xFF, 0x00, 0xC5,
186 0x00, 0xFF, 0x00, 0xFF, 0x00, 0x00,
187 0x00, 0x7F, 0x7F, 0x7F };
188 unsigned char blues[16] = { 0x00, 0x7F, 0xBF, 0xFF, 0x00, 0x11,
189 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0x7F,
190 0x7F, 0x7F, 0x00, 0x00 };
191
192 int i;
193 for (i = 0; i < 16; ++i) {
194 palette.red[i] = ((reds[i]) << 8) | reds[i];
195 palette.green[i] = ((greens[i]) << 8) | greens[i];
196 palette.blue[i] = ((blues[i]) << 8) | blues[i];
197 palette.transp[i] = 0;
198 }
199}
200
201void initPalette_256()
202{
203 if (vinfo.grayscale) {
204 int i;
205 for (i = 0; i < 256; ++i) {
206 unsigned short c = (i << 8) | i;
207 palette.red[i] = c;
208 palette.green[i] = c;
209 palette.blue[i] = c;
210 palette.transp[i] = 0;
211 }
212 return;
213 }
214
215 // 6x6x6 216 color cube
216 int i = 0;
217 int ir, ig, ib;
218 for (ir = 0x0; ir <= 0xff; ir += 0x33) {
219 for (ig = 0x0; ig <= 0xff; ig += 0x33) {
220 for (ib = 0x0; ib <= 0xff; ib += 0x33) {
221 palette.red[i] = (ir << 8)|ir;
222 palette.green[i] = (ig << 8)|ig;
223 palette.blue[i] = (ib << 8)|ib;
224 palette.transp[i] = 0;
225 ++i;
226 }
227 }
228 }
229}
230
231void initPalette()
232{
233 switch (vinfo.bits_per_pixel) {
234 case 8: paletteSize = 256; break;
235 case 4: paletteSize = 16; break;
236 default: break;
237 }
238
239 if (!paletteSize)
240 return; /* not using a palette */
241
242 /* read old palette */
243 oldPalette.start = 0;
244 oldPalette.len = paletteSize;
245 oldPalette.red = (unsigned short*)malloc(sizeof(unsigned short)*paletteSize);
246 oldPalette.green = (unsigned short*)malloc(sizeof(unsigned short)*paletteSize);
247 oldPalette.blue=(unsigned short*)malloc(sizeof(unsigned short)*paletteSize);
248 oldPalette.transp=(unsigned short*)malloc(sizeof(unsigned short)*paletteSize);
249 if (ioctl(ttyFd, FBIOGETCMAP, &oldPalette) == -1)
250 perror("initPalette: error reading palette");
251
252 /* create new palette */
253 palette.start = 0;
254 palette.len = paletteSize;
255 palette.red = (unsigned short*)malloc(sizeof(unsigned short)*paletteSize);
256 palette.green = (unsigned short*)malloc(sizeof(unsigned short)*paletteSize);
257 palette.blue = (unsigned short*)malloc(sizeof(unsigned short)*paletteSize);
258 palette.transp = (unsigned short*)malloc(sizeof(unsigned short)*paletteSize);
259 switch (paletteSize) {
260 case 16: initPalette_16(); break;
261 case 256: initPalette_256(); break;
262 default: break;
263 }
264
265 /* set new palette */
266 if (ioctl(ttyFd, FBIOPUTCMAP, &palette) == -1)
267 perror("initPalette: error setting palette");
268}
269
270void resetPalette()
271{
272 if (paletteSize == 0)
273 return;
274
275 if (ioctl(ttyFd, FBIOPUTCMAP, &oldPalette) == -1)
276 perror("resetPalette");
277
278 free(oldPalette.red);
279 free(oldPalette.green);
280 free(oldPalette.blue);
281 free(oldPalette.transp);
282
283 free(palette.red);
284 free(palette.green);
285 free(palette.blue);
286 free(palette.transp);
287}
288
289void drawRect_rgb32(int x0, int y0, int width, int height, int color)
290{
291 const int bytesPerPixel = 4;
292 const int stride = finfo.line_length / bytesPerPixel;
293
294 int *dest = (int*)(frameBuffer)
295 + (y0 + vinfo.yoffset) * stride
296 + (x0 + vinfo.xoffset);
297
298 int x, y;
299 for (y = 0; y < height; ++y) {
300 for (x = 0; x < width; ++x) {
301 dest[x] = color;
302 }
303 dest += stride;
304 }
305}
306
307void drawRect_rgb18(int x0, int y0, int width, int height, int color)
308{
309 const int bytesPerPixel = 3;
310 const int stride = finfo.line_length - width * bytesPerPixel;
311 const int red = (color & 0xff0000) >> 16;
312 const int green = (color & 0xff00) >> 8;
313 const int blue = (color & 0xff);
314 const unsigned int packed = (blue >> 2) |
315 ((green >> 2) << 6) |
316 ((red >> 2) << 12);
317 const char color18[3] = { packed & 0xff,
318 (packed & 0xff00) >> 8,
319 (packed & 0xff0000) >> 16 };
320
321 char *dest = (char*)(frameBuffer)
322 + (y0 + vinfo.yoffset) * stride
323 + (x0 + vinfo.xoffset);
324
325 int x, y;
326 for (y = 0; y < height; ++y) {
327 for (x = 0; x < width; ++x) {
328 *dest++ = color18[0];
329 *dest++ = color18[1];
330 *dest++ = color18[2];
331 }
332 dest += stride;
333 }
334}
335
336void drawRect_rgb16(int x0, int y0, int width, int height, int color)
337{
338 const int bytesPerPixel = 2;
339 const int stride = finfo.line_length / bytesPerPixel;
340 const int red = (color & 0xff0000) >> (16 + 3);
341 const int green = (color & 0xff00) >> (8 + 2);
342 const int blue = (color & 0xff) >> 3;
343 const short color16 = blue | (green << 5) | (red << (5 + 6));
344
345 short *dest = (short*)(frameBuffer)
346 + (y0 + vinfo.yoffset) * stride
347 + (x0 + vinfo.xoffset);
348
349 int x, y;
350 for (y = 0; y < height; ++y) {
351 for (x = 0; x < width; ++x) {
352 dest[x] = color16;
353 }
354 dest += stride;
355 }
356}
357
358void drawRect_rgb15(int x0, int y0, int width, int height, int color)
359{
360 const int bytesPerPixel = 2;
361 const int stride = finfo.line_length / bytesPerPixel;
362 const int red = (color & 0xff0000) >> (16 + 3);
363 const int green = (color & 0xff00) >> (8 + 3);
364 const int blue = (color & 0xff) >> 3;
365 const short color15 = blue | (green << 5) | (red << (5 + 5));
366
367 short *dest = (short*)(frameBuffer)
368 + (y0 + vinfo.yoffset) * stride
369 + (x0 + vinfo.xoffset);
370
371 int x, y;
372 for (y = 0; y < height; ++y) {
373 for (x = 0; x < width; ++x) {
374 dest[x] = color15;
375 }
376 dest += stride;
377 }
378}
379
380void drawRect_palette(int x0, int y0, int width, int height, int color)
381{
382 const int bytesPerPixel = 1;
383 const int stride = finfo.line_length / bytesPerPixel;
384 const unsigned char color8 = color;
385
386 unsigned char *dest = (unsigned char*)(frameBuffer)
387 + (y0 + vinfo.yoffset) * stride
388 + (x0 + vinfo.xoffset);
389
390 int x, y;
391 for (y = 0; y < height; ++y) {
392 for (x = 0; x < width; ++x) {
393 dest[x] = color8;
394 }
395 dest += stride;
396 }
397}
398
399void drawRect(int x0, int y0, int width, int height, int color)
400{
401 switch (vinfo.bits_per_pixel) {
402 case 32:
403 drawRect_rgb32(x0, y0, width, height, color);
404 break;
405 case 18:
406 drawRect_rgb18(x0, y0, width, height, color);
407 break;
408 case 16:
409 drawRect_rgb16(x0, y0, width, height, color);
410 break;
411 case 15:
412 drawRect_rgb15(x0, y0, width, height, color);
413 break;
414 case 8:
415 drawRect_palette(x0, y0, width, height, color);
416 break;
417 case 4:
418 drawRect_palette(x0, y0, width, height, color);
419 break;
420 default:
421 printf("Warning: drawRect() not implemented for color depth %i\n",
422 vinfo.bits_per_pixel);
423 break;
424 }
425}
426
427#define PERFORMANCE_RUN_COUNT 5
428void performSpeedTest(void* fb, int fbSize)
429{
430 int i, j, run;
431
432 struct timeval startTime, endTime;
433 unsigned long long results[PERFORMANCE_RUN_COUNT];
434 unsigned long long average;
435
436 unsigned int* testImage;
437
438 unsigned int randData[17] = {
439 0x3A428472, 0x724B84D3, 0x26B898AB, 0x7D980E3C, 0x5345A084,
440 0x6779B66B, 0x791EE4B4, 0x6E8EE3CC, 0x63AF504A, 0x18A21B33,
441 0x0E26EB73, 0x022F708E, 0x1740F3B0, 0x7E2C699D, 0x0E8A570B,
442 0x5F2C22FB, 0x6A742130
443 };
444
445 printf("Frame Buffer Performance test...\n");
446
447 for (run=0; run<PERFORMANCE_RUN_COUNT; ++run) {
448
449 /* Generate test image with random(ish) data: */
450 testImage = (unsigned int*) malloc(fbSize);
451 j = run;
452 for (i=0; i < (int)(fbSize / sizeof(int)); ++i) {
453 testImage[i] = randData[j];
454 j++;
455 if (j >= 17)
456 j = 0;
457 }
458
459 gettimeofday(&startTime, NULL);
460 memcpy(fb, testImage, fbSize);
461 gettimeofday(&endTime, NULL);
462
463 long secsDiff = endTime.tv_sec - startTime.tv_sec;
464 results[run] = secsDiff * 1000000 + (endTime.tv_usec - startTime.tv_usec);
465
466 free(testImage);
467 }
468
469
470 average = 0;
471 for (i=0; i<PERFORMANCE_RUN_COUNT; ++i)
472 average += results[i];
473 average = average / PERFORMANCE_RUN_COUNT;
474
475 printf(" Average: %llu usecs\n", average);
476 printf(" Bandwidth: %.03f MByte/Sec\n", (fbSize / 1048576.0) / ((double)average / 1000000.0));
477 printf(" Max. FPS: %.03f fps\n\n", 1000000.0 / (double)average);
478
479 /* Clear the framebuffer back to black again: */
480 memset(fb, 0, fbSize);
481}
482
483int main(int argc, char **argv)
484{
485 long int screensize = 0;
486 int doGraphicsMode = 1;
487 long oldKdMode = KD_TEXT;
488 const char *devfile = "/dev/fb0";
489 int nextArg = 1;
490
491 if (nextArg < argc) {
492 if (strncmp("nographicsmodeswitch", argv[nextArg],
493 strlen("nographicsmodeswitch")) == 0)
494 {
495 ++nextArg;
496 doGraphicsMode = 0;
497 }
498 }
499 if (nextArg < argc)
500 devfile = argv[nextArg++];
501
502 /* Open the file for reading and writing */
503 fbFd = open(devfile, O_RDWR);
504 if (fbFd == -1) {
505 perror("Error: cannot open framebuffer device");
506 exit(1);
507 }
508 printf("The framebuffer device was opened successfully.\n\n");
509
510 /* Get fixed screen information */
511 if (ioctl(fbFd, FBIOGET_FSCREENINFO, &finfo) == -1) {
512 perror("Error reading fixed information");
513 exit(2);
514 }
515
516 printFixedInfo();
517
518 /* Figure out the size of the screen in bytes */
519 screensize = finfo.smem_len;
520
521 /* Map the device to memory */
522 frameBuffer = (char *)mmap(0, screensize,
523 PROT_READ | PROT_WRITE, MAP_SHARED,
524 fbFd, 0);
525 if (frameBuffer == MAP_FAILED) {
526 perror("Error: Failed to map framebuffer device to memory");
527 exit(4);
528 }
529 printf("The framebuffer device was mapped to memory successfully.\n"
530 "\n");
531
532 if (doGraphicsMode)
533 oldKdMode = switchToGraphicsMode();
534
535 /* Get variable screen information */
536 if (ioctl(fbFd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
537 perror("Error reading variable information");
538 exit(3);
539 }
540
541 printVariableInfo();
542
543 performSpeedTest(frameBuffer, screensize);
544
545 initPalette();
546
547 if (paletteSize == 0) {
548 printf("Will draw 3 rectangles on the screen,\n"
549 "they should be colored red, green and blue (in that order).\n");
550 drawRect(vinfo.xres / 8, vinfo.yres / 8,
551 vinfo.xres / 4, vinfo.yres / 4,
552 0xffff0000);
553 drawRect(vinfo.xres * 3 / 8, vinfo.yres * 3 / 8,
554 vinfo.xres / 4, vinfo.yres / 4,
555 0xff00ff00);
556 drawRect(vinfo.xres * 5 / 8, vinfo.yres * 5 / 8,
557 vinfo.xres / 4, vinfo.yres / 4,
558 0xff0000ff);
559 } else {
560 printf("Will rectangles from the 16 first entries in the color palette"
561 " on the screen\n");
562 int y;
563 int x;
564 for (y = 0; y < 4; ++y) {
565 for (x = 0; x < 4; ++x) {
566 drawRect(vinfo.xres / 4 * x, vinfo.yres / 4 * y,
567 vinfo.xres / 4, vinfo.yres / 4,
568 4 * y + x);
569 }
570 }
571 }
572
573 sleep(5);
574
575 resetPalette();
576
577 printf(" Done.\n");
578
579 if (doGraphicsMode)
580 restoreTextMode(oldKdMode);
581
582 munmap(frameBuffer, screensize);
583 close(fbFd);
584 return 0;
585}
Note: See TracBrowser for help on using the repository browser.