Mercurial > hg > Game > Cerium
annotate old/framebuffer/cp_fb.c @ 1251:6da91e7cbffb draft
improve create From COLLADA
author | Taiki TAIRA <e095767@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 11 Nov 2011 18:17:26 +0900 |
parents | 8fe33c0c2b8f |
children |
rev | line source |
---|---|
977
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 // cp_fb.c |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 // |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 // Copyright (c) 2006, Mike Acton <macton@cellperformance.com> |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 // |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6 // documentation files (the "Software"), to deal in the Software without restriction, including without |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 // limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8 // the Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 // conditions: |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 // |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 // The above copyright notice and this permission notice shall be included in all copies or substantial |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
12 // portions of the Software. |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
13 // |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 // EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
17 // AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18 // OR OTHER DEALINGS IN THE SOFTWARE. |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 // NOTES: |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
21 // From Geert Uytterhoeven 2007-01-26 04:50:44, |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 // http://patchwork.ozlabs.org/linuxppc/patch?id=9143 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23 // |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24 // "As the actual graphics hardware cannot be accessed directly by Linux, |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
25 // ps3fb uses a virtual frame buffer in main memory. The actual screen image is |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
26 // copied to graphics memory by the GPU on every vertical blank, by making a |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27 // hypervisor call." |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28 // |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
29 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
30 #include <stdio.h> |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
31 #include <stdlib.h> |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 #include <unistd.h> |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 #include <fcntl.h> |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34 #include <getopt.h> |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 #include <string.h> |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 #include <errno.h> |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 #include <stdint.h> |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 #include <sys/ioctl.h> |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39 #include <sys/mman.h> |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40 #include <linux/fb.h> |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
41 #include <sys/time.h> |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
42 #include <asm/ps3fb.h> |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
43 #include <linux/vt.h> |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
44 #include <linux/kd.h> |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
45 #include "cp_fb.h" |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
46 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
47 static inline const char* |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
48 select_error_str( int existing_error, const char* const existing_error_str, int new_error, const char* const new_error_str ) |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
49 { |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
50 // Only report the first error found - any error that follows is probably just a cascading effect. |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
51 const char* error_str = (char*)( (~(intptr_t)existing_error & (intptr_t)new_error & (intptr_t)new_error_str) |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
52 | ((intptr_t)existing_error & (intptr_t)existing_error_str) ); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
53 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
54 return (error_str); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
55 } |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
56 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
57 int |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
58 cp_fb_open( cp_fb* const restrict fb ) |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
59 { |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
60 const char* error_str = NULL; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
61 int error = 0; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
62 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
63 // Open framebuffer device |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
64 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
65 const int fb_fd = open( "/dev/fb0", O_RDWR ); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
66 const int open_fb_error = (fb_fd >> ((sizeof(int)<<3)-1)); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
67 const char* open_fb_error_str = "Could not open /dev/fb0. Check permissions."; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
68 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
69 error_str = select_error_str( error, error_str, open_fb_error, open_fb_error_str ); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
70 error = error | open_fb_error; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
71 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
72 // Check for vsync |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
73 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
74 struct fb_vblank vblank; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
75 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
76 const int get_vblank_error = ioctl(fb_fd, FBIOGET_VBLANK, (unsigned long)&vblank); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
77 const char* get_vblank_error_str = "Could not get vblank info (FBIOGET_VBLANK)"; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
78 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
79 error_str = select_error_str( error, error_str, get_vblank_error, get_vblank_error_str ); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
80 error = error | get_vblank_error; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
81 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
82 const int has_vsync = vblank.flags & FB_VBLANK_HAVE_VSYNC; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
83 const int has_vsync_error = (~(-has_vsync|has_vsync))>>((sizeof(int)<<3)-1); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
84 const char* has_vsync_error_str = "No vsync available (FB_VBLANK_HAVE_VSYNC)"; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
85 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
86 error_str = select_error_str( error, error_str, has_vsync_error, has_vsync_error_str ); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
87 error = error | has_vsync_error; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
88 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
89 // Get screen resolution and frame count |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
90 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
91 struct ps3fb_ioctl_res res; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
92 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
93 const int screeninfo_error = ioctl(fb_fd, PS3FB_IOCTL_SCREENINFO, (unsigned long)&res); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
94 const char* screeninfo_error_str = "Could not get screen info (PS3_IOCTL_SCREENINFO)"; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
95 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
96 error_str = select_error_str( error, error_str, screeninfo_error, screeninfo_error_str ); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
97 error = error | screeninfo_error; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
98 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
99 const int has_at_least_double_buffer = (res.num_frames - 2) >> ((sizeof(res.num_frames)<<3)-1); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
100 const int has_at_least_double_buffer_error = ~has_at_least_double_buffer; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
101 const char* has_at_least_double_buffer_error_str = "Could not get screen info (PS3_IOCTL_SCREENINFO)"; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
102 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
103 error_str = select_error_str( error, error_str, has_at_least_double_buffer_error, has_at_least_double_buffer_error_str ); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
104 error = error | has_at_least_double_buffer_error; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
105 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
106 const uint32_t bpp = 4; // This is fixed for PS3 fb, and there's not a test for it. |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
107 const uint32_t frame_size = res.xres * res.yres * bpp; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
108 const uint32_t double_buffer_frame_size = frame_size * 2; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
109 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
110 // const uint32_t frame_top_margin_size = res.xres * res.yoff * bpp; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
111 // const uint32_t frame_bottom_margin_size = frame_top_margin_size; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
112 // const uint32_t frame_size = frame_full_size; /* - ( frame_top_margin_size + frame_bottom_margin_size ); */ |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
113 // const uint32_t double_buffer_frame_size = frame_size * 2; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
114 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
115 const uintptr_t fb_addr = (uintptr_t)mmap(NULL, double_buffer_frame_size, PROT_READ|PROT_WRITE, MAP_SHARED, fb_fd, 0); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
116 const int fb_mmap_error = fb_addr >> ((sizeof(uintptr_t)<<3)-1); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
117 const char* fb_mmap_error_str = "Could not get mmap frame buffer"; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
118 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
119 error_str = select_error_str( error, error_str, fb_mmap_error, fb_mmap_error_str ); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
120 error = error | fb_mmap_error; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
121 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
122 // Take control of frame buffer from kernel |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
123 ioctl(fb_fd, PS3FB_IOCTL_ON, 0); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
124 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
125 // yoff is the number of lines that cannot be copied to the CRT before the vblank. For the most part this represents |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
126 // unusable frame buffer space. While it is possible to draw to the area if you draw in the opposite frame buffer's |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
127 // offset space, which will (due to poor draw timing by ps3fb) be the thing that is actually drawn, it's very |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
128 // difficult to work with in practice. So: |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
129 // |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
130 // (1) The y offset area will be treated as "off limits". |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
131 // (2) An equivalent border will be created at the bottom, so the frame looks balanced even though it is |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
132 // not entirely full screen. |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
133 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
134 // xoff is the number of lines that cannot be copied to the CRT before the hblank. |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
135 // Similar to the y offset space, the x offset space is displayed on the wrong (previous) line. So: |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
136 // |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
137 // (1) The x offset area will be treated as "off limits". |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
138 // (2) An equivalent border will be created at the right, so the frame looks balanced even though it is |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
139 // not entirely full screen. |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
140 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
141 uintptr_t draw_start_addr = fb_addr; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
142 uintptr_t draw_next_addr = draw_start_addr + ( res.yres * res.xres * bpp ); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
143 uintptr_t drawable_h = res.yres - ( 2 * res.yoff ); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
144 uintptr_t drawable_w = res.xres - ( 2 * res.xoff ); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
145 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
146 // xoff is the number of lines that cannot be copied to the CRT before the hblank. This area is much easier to use. |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
147 // Similar to the y offset space, the x offset space is displayed on the wrong (previous) line. So: |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
148 // In principle, it is possible to steal back the x offset space by shifting back the line address to the |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
149 // start of the border of the previous line. Like so: |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
150 // |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
151 // (1) One additional line will be taken from the height so the a complete horizontal line can be started |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
152 // early. |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
153 // (2) The frame buffer address returned in cp_fb will be offset by (xres-xoff) in order for the remaining |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
154 // space to represent a rectangular area of drawable memory. |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
155 // |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
156 // i.e. |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
157 // uintptr_t draw_start_addr = fb_addr + ( ( res.xres - res.xoff ) * bpp ); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
158 // uintptr_t draw_next_addr = draw_start_addr + ( res.yres * res.xres * bpp ); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
159 // uintptr_t drawable_h = res.yres - 1 - ( 2 * res.yoff ); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
160 // uintptr_t drawable_w = res.xres; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
161 // |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
162 // But I wouldn't recommend it, since on some CRTs the effect of this would be that the frame does not appear |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
163 // square. |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
164 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
165 fb->stride = res.xres; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
166 fb->w = drawable_w; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
167 fb->h = drawable_h; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
168 fb->fd = fb_fd; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
169 fb->start_addr = fb_addr; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
170 fb->size = double_buffer_frame_size; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
171 fb->draw_addr[0] = draw_start_addr; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
172 fb->draw_addr[1] = draw_next_addr; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
173 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
174 // Clear out the whole buffer. Any unused space is black. It's also convinient to start with a cleared frame |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
175 // buffer for the user. |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
176 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
177 memset((void*)fb_addr, 0x00, double_buffer_frame_size ); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
178 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
179 return (error); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
180 } |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
181 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
182 void |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
183 cp_fb_close( const cp_fb* const restrict fb ) |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
184 { |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
185 // Give frame buffer control back to the kernel |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
186 ioctl(fb->fd, PS3FB_IOCTL_OFF, 0); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
187 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
188 munmap( (void*)fb->start_addr, fb->size ); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
189 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
190 close(fb->fd); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
191 } |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
192 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
193 void |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
194 cp_fb_wait_vsync( cp_fb* const restrict fb ) |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
195 { |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
196 unsigned long crt = 0; |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
197 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
198 ioctl(fb->fd, FBIO_WAITFORVSYNC, &crt ); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
199 } |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
200 |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
201 void |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
202 cp_fb_flip( cp_fb* const restrict fb, unsigned long field_ndx ) |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
203 { |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
204 ioctl(fb->fd, PS3FB_IOCTL_FSEL, &field_ndx ); |
8fe33c0c2b8f
add frame buffer utitilties
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
205 } |