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