initialize self.list
[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         default:
138                 gDC::exec(o);
139                 break;
140         }
141 }
142
143 void gFBDC::setAlpha(int a)
144 {
145         alpha=a;
146
147         calcRamp();
148         setPalette();
149 }
150
151 void gFBDC::setBrightness(int b)
152 {
153         brightness=b;
154
155         calcRamp();
156         setPalette();
157 }
158
159 void gFBDC::setGamma(int g)
160 {
161         gamma=g;
162
163         calcRamp();
164         setPalette();
165 }
166
167 void gFBDC::setResolution(int xres, int yres)
168 {
169         if ((m_xres == xres) && (m_yres == yres))
170                 return;
171
172         m_xres = xres; m_yres = yres;
173
174         fb->SetMode(m_xres, m_yres, 32);
175
176         for (int y=0; y<m_yres; y++)    // make whole screen transparent
177                 memset(fb->lfb+y*fb->Stride(), 0x00, fb->Stride());
178
179         surface.type = 0;
180         surface.x = m_xres;
181         surface.y = m_yres;
182         surface.bpp = 32;
183         surface.bypp = 4;
184         surface.stride = fb->Stride();
185         surface.data = fb->lfb;
186         surface.offset = 0;
187
188         surface.data_phys = 50*1024*1024; // FIXME
189
190         int fb_size = surface.stride * surface.y;
191
192         if (fb->getNumPages() > 1)
193         {
194                 m_enable_double_buffering = 1;
195                 surface_back.type = 0;
196                 surface_back.x = m_xres;
197                 surface_back.y = m_yres;
198                 surface_back.bpp = 32;
199                 surface_back.bypp = 4;
200                 surface_back.stride = fb->Stride();
201                 surface_back.offset = surface.y;
202                 surface_back.data = fb->lfb + fb_size;
203                 surface_back.data_phys = surface.data_phys + fb_size;
204
205                 fb_size *= 2;
206         } else
207                 m_enable_double_buffering = 0;
208
209         eDebug("%dkB available for acceleration surfaces.", (fb->Available() - fb_size)/1024);
210         eDebug("resolution: %d x %d x %d (stride: %d)", surface.x, surface.y, surface.bpp, fb->Stride());
211
212         if (gAccel::getInstance())
213                 gAccel::getInstance()->setAccelMemorySpace(fb->lfb + fb_size, surface.data_phys + fb_size, fb->Available() - fb_size);
214
215         if (!surface.clut.data)
216         {
217                 surface.clut.colors = 256;
218                 surface.clut.data = new gRGB[surface.clut.colors];
219                 memset(surface.clut.data, 0, sizeof(*surface.clut.data)*surface.clut.colors);
220         }
221
222         surface_back.clut = surface.clut;
223
224         m_pixmap = new gPixmap(&surface);
225 }
226
227 void gFBDC::saveSettings()
228 {
229 }
230
231 void gFBDC::reloadSettings()
232 {
233         alpha=255;
234         gamma=128;
235         brightness=128;
236
237         calcRamp();
238         setPalette();
239 }
240
241 // eAutoInitPtr<gFBDC> init_gFBDC(eAutoInitNumbers::graphic-1, "GFBDC");
242 #ifndef SDLDC
243 eAutoInitPtr<gFBDC> init_gFBDC(eAutoInitNumbers::graphic-1, "GFBDC");
244 #endif