dont show display setup in menu, when no display is available
[enigma2.git] / lib / gdi / gfbdc.cpp
1 #include <lib/gdi/gfbdc.h>
2
3 #include <lib/base/init.h>
4 #include <lib/base/init_num.h>
5
6 #include <lib/gdi/accel.h>
7
8 #include <time.h>
9
10 gFBDC *gFBDC::instance;
11
12 ePtr<gFBDC> NewgFBDCPtr(void)
13 {
14         ePtr<gFBDC> ptr;
15         gFBDC::getInstance(ptr);
16         return ptr;
17 }
18
19 gFBDC::gFBDC()
20 {
21         instance=this;
22         fb=new fbClass;
23
24         if (!fb->Available())
25                 eFatal("no framebuffer available");
26
27         surface.clut.data = 0;
28         setResolution(720, 576); // default res
29
30         reloadSettings();
31 }
32
33 gFBDC::~gFBDC()
34 {
35         delete fb;
36         delete[] surface.clut.data;
37         instance=0;
38 }
39
40 void gFBDC::calcRamp()
41 {
42 #if 0
43         float fgamma=gamma ? gamma : 1;
44         fgamma/=10.0;
45         fgamma=1/log(fgamma);
46         for (int i=0; i<256; i++)
47         {
48                 float raw=i/255.0; // IIH, float.
49                 float corr=pow(raw, fgamma) * 256.0;
50
51                 int d=corr * (float)(256-brightness) / 256 + brightness;
52                 if (d < 0)
53                         d=0;
54                 if (d > 255)
55                         d=255;
56                 ramp[i]=d;
57
58                 rampalpha[i]=i*alpha/256;
59         }
60 #endif
61         for (int i=0; i<256; i++)
62         {
63                 int d;
64                 d=i;
65                 d=(d-128)*(gamma+64)/(128+64)+128;
66                 d+=brightness-128; // brightness correction
67                 if (d<0)
68                         d=0;
69                 if (d>255)
70                         d=255;
71                 ramp[i]=d;
72
73                 rampalpha[i]=i*alpha/256;
74         }
75
76         rampalpha[255]=255; // transparent BLEIBT bitte so.
77 }
78
79 void gFBDC::setPalette()
80 {
81         if (!surface.clut.data)
82                 return;
83
84         for (int i=0; i<256; ++i)
85         {
86                 fb->CMAP()->red[i]=ramp[surface.clut.data[i].r]<<8;
87                 fb->CMAP()->green[i]=ramp[surface.clut.data[i].g]<<8;
88                 fb->CMAP()->blue[i]=ramp[surface.clut.data[i].b]<<8;
89                 fb->CMAP()->transp[i]=rampalpha[surface.clut.data[i].a]<<8;
90         }
91         fb->PutCMAP();
92 }
93
94 void gFBDC::exec(gOpcode *o)
95 {
96         switch (o->opcode)
97         {
98         case gOpcode::setPalette:
99         {
100                 gDC::exec(o);
101                 setPalette();
102                 break;
103         }
104         case gOpcode::flip:
105         {
106                 if (m_enable_double_buffering)
107                 {
108                         gSurface s(surface);
109                         surface = surface_back;
110                         surface_back = s;
111
112                         fb->setOffset(surface_back.offset);
113                 }
114                 break;
115         }
116         case gOpcode::waitVSync:
117         {
118                 static timeval l;
119                 static int t;
120                 timeval now;
121
122                 if (t == 1000)
123                 {
124                         gettimeofday(&now, 0);
125
126                         int diff = (now.tv_sec - l.tv_sec) * 1000 + (now.tv_usec - l.tv_usec) / 1000;
127                         eDebug("%d ms latency (%d fps)", diff, t * 1000 / (diff ? diff : 1));
128                         l = now;
129                         t = 0;
130                 }
131
132                 ++t;
133
134                 fb->waitVSync();
135                 break;
136         }
137         case gOpcode::flush:
138                 fb->blit();
139                 break;
140         default:
141                 gDC::exec(o);
142                 break;
143         }
144 }
145
146 void gFBDC::setAlpha(int a)
147 {
148         alpha=a;
149
150         calcRamp();
151         setPalette();
152 }
153
154 void gFBDC::setBrightness(int b)
155 {
156         brightness=b;
157
158         calcRamp();
159         setPalette();
160 }
161
162 void gFBDC::setGamma(int g)
163 {
164         gamma=g;
165
166         calcRamp();
167         setPalette();
168 }
169
170 void gFBDC::setResolution(int xres, int yres)
171 {
172         if ((m_xres == xres) && (m_yres == yres))
173                 return;
174
175         m_xres = xres; m_yres = yres;
176
177         fb->SetMode(m_xres, m_yres, 32);
178
179         for (int y=0; y<m_yres; y++)    // make whole screen transparent
180                 memset(fb->lfb+y*fb->Stride(), 0x00, fb->Stride());
181
182         surface.type = 0;
183         surface.x = m_xres;
184         surface.y = m_yres;
185         surface.bpp = 32;
186         surface.bypp = 4;
187         surface.stride = fb->Stride();
188         surface.data = fb->lfb;
189         surface.offset = 0;
190
191         surface.data_phys = 50*1024*1024; // FIXME
192
193         int fb_size = surface.stride * surface.y;
194
195         if (fb->getNumPages() > 1)
196         {
197                 m_enable_double_buffering = 1;
198                 surface_back.type = 0;
199                 surface_back.x = m_xres;
200                 surface_back.y = m_yres;
201                 surface_back.bpp = 32;
202                 surface_back.bypp = 4;
203                 surface_back.stride = fb->Stride();
204                 surface_back.offset = surface.y;
205                 surface_back.data = fb->lfb + fb_size;
206                 surface_back.data_phys = surface.data_phys + fb_size;
207
208                 fb_size *= 2;
209         } else
210                 m_enable_double_buffering = 0;
211
212         eDebug("%dkB available for acceleration surfaces.", (fb->Available() - fb_size)/1024);
213         eDebug("resolution: %d x %d x %d (stride: %d)", surface.x, surface.y, surface.bpp, fb->Stride());
214
215         if (gAccel::getInstance())
216                 gAccel::getInstance()->setAccelMemorySpace(fb->lfb + fb_size, surface.data_phys + fb_size, fb->Available() - fb_size);
217
218         if (!surface.clut.data)
219         {
220                 surface.clut.colors = 256;
221                 surface.clut.data = new gRGB[surface.clut.colors];
222                 memset(surface.clut.data, 0, sizeof(*surface.clut.data)*surface.clut.colors);
223         }
224
225         surface_back.clut = surface.clut;
226
227         m_pixmap = new gPixmap(&surface);
228 }
229
230 void gFBDC::saveSettings()
231 {
232 }
233
234 void gFBDC::reloadSettings()
235 {
236         alpha=255;
237         gamma=128;
238         brightness=128;
239
240         calcRamp();
241         setPalette();
242 }
243
244 // eAutoInitPtr<gFBDC> init_gFBDC(eAutoInitNumbers::graphic-1, "GFBDC");
245 #ifndef SDLDC
246 eAutoInitPtr<gFBDC> init_gFBDC(eAutoInitNumbers::graphic-1, "GFBDC");
247 #endif