Gnash  0.8.11dev
eglDevice.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 // Free Software Foundation, Inc
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 //
19 
20 #ifndef __EGL_DEVICE_H__
21 #define __EGL_DEVICE_H__ 1
22 
23 #ifdef HAVE_CONFIG_H
24 #include "gnashconfig.h"
25 #endif
26 
27 #include <boost/scoped_array.hpp>
28 #include <boost/scoped_ptr.hpp>
29 
30 #ifdef HAVE_X11_X_H
31 #include "x11/X11Device.h"
32 #endif
33 #include "GnashDevice.h"
34 
35 #ifdef HAVE_EGL_EGL_H
36 # include <EGL/egl.h>
37 #else
38 # error "This file needs EGL, which is part of OpenGL-ES"
39 #endif
40 
41 #include "Point2d.h"
42 
43 namespace gnash {
44 
45 namespace renderer {
46 
47 struct eglVertex {
48  eglVertex(float x, float y)
49  : _x(x), _y(y) { }
50 
51  eglVertex(const point& p)
52  : _x(p.x), _y(p.y) { }
53  float _x;
54  float _y;
55 };
56 
57 class EGLDevice : public GnashDevice
58 {
59  public:
60  typedef enum {LOW, MEDIUM, HIGH} quality_e;
61  EGLDevice();
62  EGLDevice(int argc, char *argv[]);
64 
65  virtual ~EGLDevice();
66 
67  dtype_t getType() { return EGL; };
68 
69  // Initialize EGL Device layer
70  bool initDevice(int argc, char *argv[]);
71 
72  // Initialize EGL Window layer
74 
75  // Utility methods not in the base class
77  const char *getErrorString(int error);
78 
79  size_t getStride() {
80  return getDepth() * getWidth();
81  };
82 
83  // Accessors for the settings needed by higher level code.
84  // Surface accessors
85  size_t getWidth() {
86  return getWidth(_eglSurface);
87  };
88 
89  size_t getHeight() {
90  return getHeight(_eglSurface);
91  }
92 
93  EGLint getDepth() {
94  EGLint value;
95  eglGetConfigAttrib(_eglDisplay, _eglConfig, EGL_DEPTH_SIZE, &value);
96  return value;
97  }
98 
99  int getRedSize() {
100  EGLint value;
101  if (_eglConfig && _eglDisplay) {
102  eglGetConfigAttrib(_eglDisplay, _eglConfig, EGL_RED_SIZE, &value);
103  }
104  return static_cast<int>(value);
105  };
106  int getGreenSize() {
107  EGLint value;
108  if (_eglConfig && _eglDisplay) {
109  eglGetConfigAttrib(_eglDisplay, _eglConfig, EGL_GREEN_SIZE, &value);
110  }
111  return static_cast<int>(value);
112  };
113  int getBlueSize() {
114  EGLint value;
115  if (_eglConfig && _eglDisplay) {
116  eglGetConfigAttrib(_eglDisplay, _eglConfig, EGL_BLUE_SIZE, &value);
117  }
118  return static_cast<int>(value);
119  };
120 
122  EGLint value;
123  if (_eglSurface && _eglDisplay) {
124  eglQuerySurface(_eglDisplay, _eglSurface, EGL_RENDER_BUFFER, &value);
125  }
126  if (value == EGL_SINGLE_BUFFER) {
127  return true;
128  }
129  return false;
130  }
133  }
134 
135  int getID() {
136  return static_cast<int>(getSurfaceID());
137  }
138 
140 
141  bool isNativeRender() {
142  EGLint value;
143  eglGetConfigAttrib(_eglDisplay, _eglConfig, EGL_NATIVE_RENDERABLE, &value);
144  return value;
145  }
146 
147  // Overload some of the base class methods to deal with Device specific
148  // data types.
149  bool bindClient(GnashDevice::rtype_t rtype);
150 
151  size_t getWidth(EGLSurface surface) {
152  EGLint value;
153  if (surface && _eglDisplay) {
154  eglQuerySurface(_eglDisplay, surface, EGL_WIDTH, &value);
155  }
156  return static_cast<size_t>(value);
157  };
158  size_t getHeight(EGLSurface surface) {
159  EGLint value;
160  if (surface && _eglDisplay) {
161  eglQuerySurface(_eglDisplay, surface, EGL_HEIGHT, &value);
162  }
163  return static_cast<size_t>(value);
164  }
165  bool isBufferDestroyed(EGLSurface surface) {
166  EGLint value;
167  eglQuerySurface(_eglDisplay, surface, EGL_SWAP_BEHAVIOR, &value);
168  if (value == EGL_BUFFER_DESTROYED) {
169  return true;
170  }
171  return false;
172  }
173 #ifdef BUILD_X11_DEVICE
174  EGLint getNativeVisual();
175 #endif
176 
178  bool checkEGLConfig(EGLConfig config);
179 
182  int queryEGLConfig(EGLDisplay display);
183 
184  // Debugging utilities
186  void printEGLConfig(EGLConfig config);
188  void printEGLContext(EGLContext context);
190  void printEGLSurface(EGLSurface surface);
191  void printEGLAttribs(const EGLint *attrib);
192 
193  // Create Pbuffers for offscreen rendering
194  EGLSurface createPbuffer(int width, int height);
195  EGLSurface createPbuffer(int width, int height, EGLClientBuffer buf, EGLenum type);
196  EGLSurface createPixmap(int width, int height, NativePixmapType buf);
197  size_t totalPbuffers() { return _pbuffers.size(); };
198  EGLSurface &operator[](int index) { if (!_pbuffers.empty()) { return _pbuffers[index]; }; };
199 
200  // Swapping Buffers makes the specified surface active on the display if
201  // EGL_RENDER_BUFFER is set to EGL_BACK_BUFFER. If it's set to
202  // EGL_SINGLE_BUFFER then this has no effect, as the display was drawn to
203  // directly.
204  // Swap to the default surface
205  bool swapBuffers() {
206  // GNASH_REPORT_FUNCTION;
207  if (!isSingleBuffered()) {
208  return eglSwapBuffers(_eglDisplay, _eglSurface);
209  }
210  return true;
211  }
212  bool copyPbuffers(size_t x) {
214  if (x < _pbuffers.size()) {
215  NativePixmapType pix;
216  if (!eglCopyBuffers(_eglDisplay, _pbuffers[x], pix)) {
217  log_error( "eglCopyBuffers() failed (error 0x%x)", eglGetError());
218  return false;
219  }
220  return true;
221  }
222  return false;
223  }
224  // Make one of the pbuffers the current one to draw into
226  if ((_eglDisplay != EGL_NO_DISPLAY) && (_eglSurface != EGL_NO_SURFACE)) {
227  if (!eglMakeCurrent(_eglDisplay, _eglSurface, _eglSurface, _eglContext)) {
228  log_error( "eglMakeCurrent() failed (error 0x%x)", eglGetError());
229  return false;
230  }
231  }
232  return false;
233  }
234 
235  bool makePbufferCurrent(size_t x) {
236  if (x < _pbuffers.size()) {
237  if ((_eglDisplay != EGL_NO_DISPLAY) && (_eglContext != EGL_NO_CONTEXT)) {
238  if (!eglMakeCurrent(_eglDisplay, _pbuffers[x], _pbuffers[x], _eglContext)) {
239  log_error( "eglMakeCurrent() failed (error 0x%x)", eglGetError());
240  return false;
241  }
242  return true;
243  }
244  }
245  return false;
246  }
247 
248  size_t getVerticalRes() {
249  EGLint value = 0;
250  if ((_eglDisplay != EGL_NO_DISPLAY) && (_eglSurface != EGL_NO_SURFACE)) {
251  eglQuerySurface(_eglDisplay, _eglSurface, EGL_VERTICAL_RESOLUTION, &value);
252  }
253  return static_cast<size_t>(value);
254  }
255  size_t getHorzRes() {
256  EGLint value = 0;
257  if ((_eglDisplay != EGL_NO_DISPLAY) && (_eglSurface != EGL_NO_SURFACE)) {
258  eglQuerySurface(_eglDisplay, _eglSurface, EGL_HORIZONTAL_RESOLUTION, &value);
259  }
260  return static_cast<size_t>(value);
261  }
262  bool isBackBuffered() {
263  EGLint value;
264  if ((_eglDisplay != EGL_NO_DISPLAY) && (_eglSurface != EGL_NO_SURFACE)) {
265  eglQuerySurface(_eglDisplay, _eglSurface, EGL_RENDER_BUFFER, &value);
266  if (value == EGL_BACK_BUFFER) {
267  return true;
268  }
269  return false;
270  }
271  return false;
272  }
273 
274  bool isMultiSample() {
275  EGLint value;
276  if ((_eglDisplay != EGL_NO_DISPLAY) && (_eglSurface != EGL_NO_SURFACE)) {
277  eglQuerySurface(_eglDisplay, _eglSurface, EGL_MULTISAMPLE_RESOLVE, &value);
278  if (value == EGL_MULTISAMPLE_RESOLVE_BOX) {
279  return true;
280  }
281  return false;
282  }
283  return false;
284  }
285 
286  EGLint getSurfaceID() {
287  EGLint value = -1;
288  if ((_eglDisplay != EGL_NO_DISPLAY) && (_eglSurface != EGL_NO_SURFACE)) {
289  eglQuerySurface(_eglDisplay, _eglSurface, EGL_CONFIG_ID, &value);
290  }
291  return value;
292  }
293 
294  // Context accessors
295  EGLint getContextID() {
296  EGLint value = -1;
297  if ((_eglDisplay != EGL_NO_DISPLAY) && (_eglContext != EGL_NO_CONTEXT)) {
298  eglQueryContext(_eglDisplay, _eglContext, EGL_CONFIG_ID, &value);
299  }
300  return value;
301  }
303  EGLint value;
304  if ((_eglDisplay != EGL_NO_DISPLAY) && (_eglContext != EGL_NO_CONTEXT)) {
305  eglQueryContext(_eglDisplay, _eglContext, EGL_RENDER_BUFFER, &value);
306  if (value == EGL_SINGLE_BUFFER) {
307  return true;
308  }
309  return false;
310  }
311  return false;
312  }
314  EGLint value;
315  if ((_eglDisplay != EGL_NO_DISPLAY) && (_eglContext != EGL_NO_CONTEXT)) {
316  eglQueryContext(_eglDisplay, _eglContext, EGL_RENDER_BUFFER, &value);
317  if (value == EGL_BACK_BUFFER) {
318  return true;
319  }
320  return false;
321  }
322  return false;
323  }
324 
325  // Config accessors
326  EGLint getSamples() {
327  EGLint value = -1;
328  if ((_eglDisplay != EGL_NO_DISPLAY) && (_eglConfig != 0)) {
329  eglGetConfigAttrib(_eglDisplay, _eglConfig, EGL_SAMPLES, &value);
330  }
331  return value;
332  }
333  EGLint getSampleBuffers() {
334  EGLint value = -1;
335  if ((_eglDisplay != EGL_NO_DISPLAY) && (_eglConfig != 0)) {
336  eglGetConfigAttrib(_eglDisplay, _eglConfig, EGL_SAMPLE_BUFFERS, &value);
337  }
338  return value;
339  }
341  EGLint value = -1;
342  if ((_eglDisplay != EGL_NO_DISPLAY) && (_eglConfig != 0)) {
343  eglGetConfigAttrib(_eglDisplay, _eglConfig, EGL_MAX_SWAP_INTERVAL, &value);
344  }
345  return value;
346  }
348  EGLint value = -1;
349  if ((_eglDisplay != EGL_NO_DISPLAY) && (_eglConfig != 0)) {
350  eglGetConfigAttrib(_eglDisplay, _eglConfig, EGL_MIN_SWAP_INTERVAL, &value);
351  }
352  return value;
353  }
354 
355  void setAttrib(int bpp);
356  static EGLint getRenderableTypes();
357 protected:
358  EGLConfig _eglConfig;
359  EGLContext _eglContext;
360  EGLSurface _eglSurface;
361  EGLDisplay _eglDisplay;
362  EGLNativeWindowType _nativeWindow;
363  EGLNativePixmapType _nativePixmap;
365  const EGLint *_attrib;
366  unsigned int _bpp;
367  std::vector<EGLSurface> _pbuffers;
368 };
369 
370 #define DUMP_CURRENT_SURFACE printEGLSurface(eglGetCurrentSurface(EGL_DRAW))
371 #define DUMP_CURRENT_CONTEXT printEGLContext(eglGetCurrentContext())
372 
373 } // namespace renderer
374 } // namespace gnash
375 
376 #endif // end of __EGL_DEVICE_H__
377 
378 // local Variables:
379 // mode: C++
380 // indent-tabs-mode: nil
381 // End:
bool isContextSingleBuffered()
Definition: eglDevice.h:302
dtype_t getType()
Definition: eglDevice.h:67
EGLint getSurfaceID()
Definition: eglDevice.h:286
void printEGLContext()
Definition: eglDevice.h:187
EGLint getDepth()
Get the depth of the device.
Definition: eglDevice.h:93
bool supportsRenderer(GnashDevice::rtype_t rtype)
Is the specified renderer supported by this hardware ?
Definition: eglDevice.cpp:293
bool isContextBackBuffered()
Definition: eglDevice.h:313
bool copyPbuffers(size_t x)
Definition: eglDevice.h:212
EGLContext _eglContext
Definition: eglDevice.h:359
Definition: GnashDevice.h:48
bool makePbufferCurrent(size_t x)
Definition: eglDevice.h:235
size_t totalPbuffers()
Definition: eglDevice.h:197
float _y
Definition: eglDevice.h:54
EGLNativePixmapType _nativePixmap
Definition: eglDevice.h:363
void setAttrib(int bpp)
Definition: eglDevice.cpp:116
size_t getHeight(EGLSurface surface)
Definition: eglDevice.h:158
EGLint getSampleBuffers()
Definition: eglDevice.h:333
EGLSurface createPbuffer(int width, int height)
Definition: eglDevice.cpp:767
const EGLint * _attrib
Definition: eglDevice.h:365
long native_window_t
Definition: GnashDevice.h:43
bool swapBuffers()
Definition: eglDevice.h:205
bool makePbufferCurrent()
Definition: eglDevice.h:225
EGLSurface & operator[](int index)
Definition: eglDevice.h:198
Definition: GnashDevice.h:39
dtype_t
The list of supported device types.
Definition: GnashDevice.h:48
Definition: eglDevice.h:47
int getGreenSize()
Get the size of the Green pixel.
Definition: eglDevice.h:106
EGLConfig _eglConfig
Definition: eglDevice.h:358
type
Definition: GnashKey.h:329
Definition: eglDevice.h:60
bool isSingleBuffered()
Is this device single buffered.
Definition: eglDevice.h:121
void printEGLSurface()
Definition: eglDevice.h:189
Definition: klash_part.cpp:329
2D Point class
Definition: Point2d.h:38
bool bindClient(GnashDevice::rtype_t rtype)
Definition: eglDevice.cpp:332
bool attachWindow(GnashDevice::native_window_t window)
Definition: eglDevice.cpp:376
size_t getWidth(EGLSurface surface)
Definition: eglDevice.h:151
eglVertex(const point &p)
Definition: eglDevice.h:51
void printEGLAttribs(const EGLint *attrib)
Definition: eglDevice.cpp:581
size_t getStride()
Query the system for all supported configs.
Definition: eglDevice.h:79
bool isBufferDestroyed(EGLSurface surface)
Definition: eglDevice.h:165
int getRedSize()
Get the size of the Red pixel.
Definition: eglDevice.h:99
bool isNativeRender()
Is this renderering natively.
Definition: eglDevice.h:141
EGLint getContextID()
Definition: eglDevice.h:295
quality_e _quality
Definition: eglDevice.h:364
Definition: klash_part.cpp:329
bool isMultiSample()
Definition: eglDevice.h:274
struct lirc_config * config
Definition: lirc_ext.cpp:43
int getBlueSize()
Get the size of the Blue pixel.
Definition: eglDevice.h:113
size_t getHeight()
Get the Height of the device.
Definition: eglDevice.h:89
Definition: eglDevice.h:60
rtype_t
The list of supported renders that use devices.
Definition: GnashDevice.h:46
bool isBackBuffered()
Definition: eglDevice.h:262
void printEGLConfig()
Definition: eglDevice.h:185
int queryEGLConfig()
Query the system for all supported configs.
Definition: eglDevice.h:181
EGLNativeWindowType _nativeWindow
Definition: eglDevice.h:362
std::vector< EGLSurface > _pbuffers
Definition: eglDevice.h:367
EGLint getMinSwapInterval()
Definition: eglDevice.h:347
boost::int32_t x
Definition: BitmapData_as.cpp:434
BitmapData_as::iterator pix
Definition: BitmapData_as.cpp:568
EGLint getSamples()
Definition: eglDevice.h:326
static EGLint getRenderableTypes()
Definition: eglDevice.cpp:830
Definition: eglDevice.h:57
eglVertex(float x, float y)
Definition: eglDevice.h:48
unsigned int _bpp
Definition: eglDevice.h:366
boost::int32_t y
Definition: BitmapData_as.cpp:435
EGLDisplay _eglDisplay
Definition: eglDevice.h:361
Definition: GnashKey.h:162
Definition: eglDevice.h:60
#define GNASH_REPORT_FUNCTION
Definition: log.h:438
bool initDevice(int argc, char *argv[])
Definition: eglDevice.cpp:169
quality_e
Definition: eglDevice.h:60
EGLSurface createPixmap(int width, int height, NativePixmapType buf)
Definition: eglDevice.cpp:808
const char * getErrorString(int error)
Return a string with the error code as text, instead of a numeric value.
Definition: eglDevice.cpp:436
bool isBufferDestroyed()
Are buffers destroyed ?
Definition: eglDevice.h:131
virtual ~EGLDevice()
Definition: eglDevice.cpp:131
EGLDevice()
Definition: eglDevice.cpp:50
int getID()
Get the window ID handle.
Definition: eglDevice.h:135
size_t getVerticalRes()
Definition: eglDevice.h:248
EGLint getMaxSwapInterval()
Definition: eglDevice.h:340
EGLSurface _eglSurface
Definition: eglDevice.h:360
size_t getHorzRes()
Definition: eglDevice.h:255
float _x
Definition: eglDevice.h:53
size_t getWidth()
Get the width of the device.
Definition: eglDevice.h:85
bool checkEGLConfig(EGLConfig config)
Check the requested EGl configuration against the current one.
Definition: eglDevice.cpp:475