properly request a video mode with an alpha channel
[enigma2.git] / lib / gdi / fb.cpp
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <sys/ioctl.h>
5 #include <unistd.h>
6 #include <sys/mman.h>
7 #include <memory.h>
8 #include <linux/kd.h>
9
10 #include <lib/gdi/fb.h>
11
12 #ifndef FBIO_WAITFORVSYNC
13 #define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32)
14 #endif
15
16 #ifndef FBIO_BLIT
17 #define FBIO_SET_MANUAL_BLIT _IOW('F', 0x21, __u8)
18 #define FBIO_BLIT 0x22
19 #endif
20
21 fbClass *fbClass::instance;
22
23 fbClass *fbClass::getInstance()
24 {
25         return instance;
26 }
27
28 fbClass::fbClass(const char *fb)
29 {
30         m_manual_blit=-1;
31         instance=this;
32         locked=0;
33         available=0;
34         cmap.start=0;
35         cmap.len=256;
36         cmap.red=red;
37         cmap.green=green;
38         cmap.blue=blue;
39         cmap.transp=trans;
40
41         fd=open(fb, O_RDWR);
42         if (fd<0)
43         {
44                 perror(fb);
45                 goto nolfb;
46         }
47
48
49         if (ioctl(fd, FBIOGET_VSCREENINFO, &screeninfo)<0)
50         {
51                 perror("FBIOGET_VSCREENINFO");
52                 goto nolfb;
53         }
54         
55         memcpy(&oldscreen, &screeninfo, sizeof(screeninfo));
56
57         fb_fix_screeninfo fix;
58         if (ioctl(fd, FBIOGET_FSCREENINFO, &fix)<0)
59         {
60                 perror("FBIOGET_FSCREENINFO");
61                 goto nolfb;
62         }
63
64         available=fix.smem_len;
65         eDebug("%dk video mem", available/1024);
66         lfb=(unsigned char*)mmap(0, available, PROT_WRITE|PROT_READ, MAP_SHARED, fd, 0);
67         if (!lfb)
68         {
69                 perror("mmap");
70                 goto nolfb;
71         }
72
73         showConsole(0);
74
75         enableManualBlit();
76         return;
77 nolfb:
78         lfb=0;
79         printf("framebuffer not available.\n");
80         return;
81 }
82
83 int fbClass::showConsole(int state)
84 {
85         int fd=open("/dev/vc/0", O_RDWR);
86         if(fd>=0)
87         {
88                 if(ioctl(fd, KDSETMODE, state?KD_TEXT:KD_GRAPHICS)<0)
89                 {
90                         eDebug("setting /dev/vc/0 status failed.");
91                 }
92                 close(fd);
93         }
94         return 0;
95 }
96
97 int fbClass::SetMode(unsigned int nxRes, unsigned int nyRes, unsigned int nbpp)
98 {
99         screeninfo.xres_virtual=screeninfo.xres=nxRes;
100         screeninfo.yres_virtual=(screeninfo.yres=nyRes)*2;
101         screeninfo.height=0;
102         screeninfo.width=0;
103         screeninfo.xoffset=screeninfo.yoffset=0;
104         screeninfo.bits_per_pixel=nbpp;
105
106         switch (nbpp) {
107         case 16:
108                 // ARGB 1555
109                 screeninfo.transp.offset = 15;
110                 screeninfo.transp.length = 1;
111                 screeninfo.red.offset = 10;
112                 screeninfo.red.length = 5;
113                 screeninfo.green.offset = 5;
114                 screeninfo.green.length = 5;
115                 screeninfo.blue.offset = 0;
116                 screeninfo.blue.length = 5;
117                 break;
118         case 32:
119                 // ARGB 8888
120                 screeninfo.transp.offset = 24;
121                 screeninfo.transp.length = 8;
122                 screeninfo.red.offset = 16;
123                 screeninfo.red.length = 8;
124                 screeninfo.green.offset = 8;
125                 screeninfo.green.length = 8;
126                 screeninfo.blue.offset = 0;
127                 screeninfo.blue.length = 8;
128                 break;
129         }
130
131         if (ioctl(fd, FBIOPUT_VSCREENINFO, &screeninfo)<0)
132         {
133                 // try single buffering
134                 screeninfo.yres_virtual=screeninfo.yres=nyRes;
135                 
136                 if (ioctl(fd, FBIOPUT_VSCREENINFO, &screeninfo)<0)
137                 {
138                         perror("FBIOPUT_VSCREENINFO");
139                         printf("fb failed\n");
140                         return -1;
141                 }
142                 eDebug(" - double buffering not available.");
143         } else
144                 eDebug(" - double buffering available!");
145         
146         m_number_of_pages = screeninfo.yres_virtual / nyRes;
147         
148         ioctl(fd, FBIOGET_VSCREENINFO, &screeninfo);
149         
150         if ((screeninfo.xres!=nxRes) && (screeninfo.yres!=nyRes) && (screeninfo.bits_per_pixel!=nbpp))
151         {
152                 eDebug("SetMode failed: wanted: %dx%dx%d, got %dx%dx%d",
153                         nxRes, nyRes, nbpp,
154                         screeninfo.xres, screeninfo.yres, screeninfo.bits_per_pixel);
155         }
156         xRes=screeninfo.xres;
157         yRes=screeninfo.yres;
158         bpp=screeninfo.bits_per_pixel;
159         fb_fix_screeninfo fix;
160         if (ioctl(fd, FBIOGET_FSCREENINFO, &fix)<0)
161         {
162                 perror("FBIOGET_FSCREENINFO");
163                 printf("fb failed\n");
164         }
165         stride=fix.line_length;
166         memset(lfb, 0, stride*yRes);
167         return 0;
168 }
169
170 int fbClass::setOffset(int off)
171 {
172         screeninfo.xoffset = 0;
173         screeninfo.yoffset = off;
174         return ioctl(fd, FBIOPAN_DISPLAY, &screeninfo);
175 }
176
177 int fbClass::waitVSync()
178 {
179         int c = 0;
180         return ioctl(fd, FBIO_WAITFORVSYNC, &c);
181 }
182
183 void fbClass::blit()
184 {
185         if (m_manual_blit == 1) {
186                 if (ioctl(fd, FBIO_BLIT) < 0)
187                         perror("FBIO_BLIT");
188         }
189 }
190
191 fbClass::~fbClass()
192 {
193         if (available)
194                 ioctl(fd, FBIOPUT_VSCREENINFO, &oldscreen);
195         if (lfb)
196                 munmap(lfb, available);
197         showConsole(1);
198         disableManualBlit();
199 }
200
201 int fbClass::PutCMAP()
202 {
203         return ioctl(fd, FBIOPUTCMAP, &cmap);
204 }
205
206 int fbClass::lock()
207 {
208         if (locked)
209                 return -1;
210         if (m_manual_blit == 1)
211         {
212                 locked = 2;
213                 disableManualBlit();
214         }
215         else
216                 locked = 1;
217         return fd;
218 }
219
220 void fbClass::unlock()
221 {
222         if (!locked)
223                 return;
224         if (locked == 2)  // re-enable manualBlit
225                 enableManualBlit();
226         locked=0;
227         SetMode(xRes, yRes, bpp);
228         PutCMAP();
229 }
230
231 void fbClass::enableManualBlit()
232 {
233         unsigned char tmp = 1;
234         if (ioctl(fd,FBIO_SET_MANUAL_BLIT, &tmp)<0)
235                 perror("FBIO_SET_MANUAL_BLIT");
236         else
237                 m_manual_blit = 1;
238 }
239
240 void fbClass::disableManualBlit()
241 {
242         unsigned char tmp = 0;
243         if (ioctl(fd,FBIO_SET_MANUAL_BLIT, &tmp)<0) 
244                 perror("FBIO_SET_MANUAL_BLIT");
245         else
246                 m_manual_blit = 0;
247 }
248