comparison Renderer/Engine/ps3fb/cp_fb.cc @ 0:04e28d8d3c6f

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