compiled fixes for builds without PCH
[wxWidgets.git] / src / dfb / wrapdfb.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/dfb/wrapdfb.cpp
3 // Purpose: wx wrappers for DirectFB interfaces
4 // Author: Vaclav Slavik
5 // Created: 2006-09-04
6 // RCS-ID: $Id$
7 // Copyright: (c) 2006 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #ifndef WX_PRECOMP
19 #include "wx/intl.h"
20 #include "wx/log.h"
21 #endif
22
23 #include "wx/dfb/wrapdfb.h"
24
25 //-----------------------------------------------------------------------------
26 // wxDfbCheckReturn
27 //-----------------------------------------------------------------------------
28
29 bool wxDfbCheckReturn(DFBResult code)
30 {
31 switch ( code )
32 {
33 case DFB_OK:
34 return true;
35
36 // these are programming errors, assert:
37 #define DFB_ASSERT(code) \
38 case code: \
39 wxFAIL_MSG( _T("DirectFB error: ") _T(#code) ); \
40 return false \
41
42 DFB_ASSERT(DFB_DEAD);
43 DFB_ASSERT(DFB_UNSUPPORTED);
44 DFB_ASSERT(DFB_UNIMPLEMENTED);
45 DFB_ASSERT(DFB_INVARG);
46 DFB_ASSERT(DFB_NOIMPL);
47 DFB_ASSERT(DFB_MISSINGFONT);
48 DFB_ASSERT(DFB_THIZNULL);
49 DFB_ASSERT(DFB_INVAREA);
50 DFB_ASSERT(DFB_DESTROYED);
51 DFB_ASSERT(DFB_NOSUCHMETHOD);
52 DFB_ASSERT(DFB_NOSUCHINSTANCE);
53 DFB_ASSERT(DFB_VERSIONMISMATCH);
54
55 #undef DFB_ASSERT
56
57 // these are not errors, but valid return codes:
58 case DFB_INTERRUPTED:
59 case DFB_BUFFEREMPTY:
60 return true;
61
62 default:
63 // FIXME: should handle the errors individually
64 wxLogError(_("DirectFB error %d occured."), (int)code);
65 return false;
66 }
67 }
68
69 //-----------------------------------------------------------------------------
70 // wxDfbPtrBase
71 //-----------------------------------------------------------------------------
72
73 /* static */
74 void wxDfbPtrBase::DoAddRef(wxDfbWrapperBase *ptr)
75 {
76 ptr->AddRef();
77 }
78
79 void wxDfbPtrBase::DoRelease(wxDfbWrapperBase *ptr)
80 {
81 ptr->Release();
82 }
83
84 //-----------------------------------------------------------------------------
85 // wxIDirectFB
86 //-----------------------------------------------------------------------------
87
88 wxIDirectFBPtr wxIDirectFB::ms_ptr;
89
90 /* static */
91 void wxIDirectFB::CreateDirectFB()
92 {
93 IDirectFB *dfb;
94 if ( wxDfbCheckReturn(DirectFBCreate(&dfb)) )
95 ms_ptr = new wxIDirectFB(dfb);
96 }
97
98 /* static */
99 void wxIDirectFB::CleanUp()
100 {
101 ms_ptr.Reset();
102 }
103
104 wxIDirectFBSurfacePtr wxIDirectFB::GetPrimarySurface()
105 {
106 DFBSurfaceDescription desc;
107 desc.flags = DSDESC_CAPS;
108 desc.caps = DSCAPS_PRIMARY;
109 return CreateSurface(&desc);
110 }
111
112 //-----------------------------------------------------------------------------
113 // wxIDirectFBSurface
114 //-----------------------------------------------------------------------------
115
116 int wxIDirectFBSurface::GetDepth()
117 {
118 DFBSurfacePixelFormat format = DSPF_UNKNOWN;
119
120 if ( !GetPixelFormat(&format) )
121 return -1;
122
123 return DFB_BITS_PER_PIXEL(format);
124 }
125
126 wxIDirectFBSurfacePtr
127 wxIDirectFBSurface::CreateCompatible(const wxSize& sz, int flags)
128 {
129 wxSize size(sz);
130 if ( size == wxDefaultSize )
131 {
132 if ( !GetSize(&size.x, &size.y) )
133 return NULL;
134 }
135
136 wxCHECK_MSG( size.x > 0 && size.y > 0, NULL, _T("invalid size") );
137
138 DFBSurfaceDescription desc;
139 desc.flags = (DFBSurfaceDescriptionFlags)(
140 DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT);
141 GetCapabilities(&desc.caps);
142 GetPixelFormat(&desc.pixelformat);
143 desc.width = size.x;
144 desc.height = size.y;
145
146 // filter out caps that don't make sense for a new compatible surface:
147 int caps = desc.caps;
148 caps &= ~DSCAPS_PRIMARY;
149 caps &= ~DSCAPS_SUBSURFACE;
150 if ( flags & CreateCompatible_NoBackBuffer )
151 {
152 caps &= ~DSCAPS_DOUBLE;
153 caps &= ~DSCAPS_TRIPLE;
154 }
155 desc.caps = (DFBSurfaceCapabilities)caps;
156
157 wxIDirectFBSurfacePtr snew(wxIDirectFB::Get()->CreateSurface(&desc));
158 if ( !snew )
159 return NULL;
160
161 if ( desc.pixelformat == DSPF_LUT8 )
162 {
163 wxIDirectFBPalettePtr pal(GetPalette());
164 if ( pal )
165 {
166 if ( !snew->SetPalette(pal) )
167 return NULL;
168 }
169 }
170
171 return snew;
172 }
173
174 wxIDirectFBSurfacePtr wxIDirectFBSurface::Clone()
175 {
176 wxIDirectFBSurfacePtr snew(CreateCompatible());
177 if ( !snew )
178 return NULL;
179
180 if ( !snew->SetBlittingFlags(DSBLIT_NOFX) )
181 return NULL;
182
183 if ( !snew->Blit(GetRaw(), NULL, 0, 0) )
184 return NULL;
185
186 return snew;
187 }
188
189 bool wxIDirectFBSurface::Flip(const DFBRegion *region, int flags)
190 {
191 return Check(m_ptr->Flip(m_ptr, region, (DFBSurfaceFlipFlags)flags));
192 }
193
194 bool wxIDirectFBSurface::FlipToFront(const DFBRegion *region)
195 {
196 // Blit to the front buffer instead of exchanging front and back ones.
197 // Always doing this ensures that back and front buffer have same content
198 // and so painting to the back buffer will never lose any previous
199 // drawings:
200 return Flip(region, DSFLIP_BLIT);
201 }
202
203 //-----------------------------------------------------------------------------
204 // wxIDirectFBDisplayLayer
205 //-----------------------------------------------------------------------------
206
207 wxVideoMode wxIDirectFBDisplayLayer::GetVideoMode()
208 {
209 DFBDisplayLayerConfig cfg;
210 if ( !GetConfiguration(&cfg) )
211 return wxVideoMode(); // invalid
212
213 if ( !((cfg.flags & DLCONF_WIDTH) &&
214 (cfg.flags & DLCONF_HEIGHT) &&
215 (cfg.flags & DLCONF_PIXELFORMAT)) )
216 return wxVideoMode(); // invalid
217
218 return wxVideoMode
219 (
220 cfg.width,
221 cfg.height,
222 DFB_BITS_PER_PIXEL(cfg.pixelformat)
223 );
224 }