summaryrefslogtreecommitdiff
path: root/src/egl
diff options
context:
space:
mode:
Diffstat (limited to 'src/egl')
-rw-r--r--src/egl/Makefile31
-rw-r--r--src/egl/docs/EGL_MESA_screen_surface564
-rw-r--r--src/egl/drivers/Makefile31
-rw-r--r--src/egl/drivers/Makefile.template52
-rw-r--r--src/egl/drivers/dri/Makefile69
-rw-r--r--src/egl/drivers/dri/egldri.c1205
-rw-r--r--src/egl/drivers/dri/egldri.h116
-rw-r--r--src/egl/drivers/dri2/Makefile18
-rw-r--r--src/egl/drivers/dri2/egl_dri2.c1474
-rw-r--r--src/egl/drivers/glx/Makefile16
-rw-r--r--src/egl/drivers/glx/egl_glx.c941
-rw-r--r--src/egl/main/Makefile121
-rw-r--r--src/egl/main/README.txt71
-rw-r--r--src/egl/main/SConscript49
-rw-r--r--src/egl/main/egl.pc.in12
-rw-r--r--src/egl/main/eglapi.c1282
-rw-r--r--src/egl/main/eglapi.h146
-rw-r--r--src/egl/main/eglcompiler.h94
-rw-r--r--src/egl/main/eglconfig.c848
-rw-r--r--src/egl/main/eglconfig.h172
-rw-r--r--src/egl/main/eglconfigutil.c128
-rw-r--r--src/egl/main/eglconfigutil.h19
-rw-r--r--src/egl/main/eglcontext.c424
-rw-r--r--src/egl/main/eglcontext.h137
-rw-r--r--src/egl/main/eglcurrent.c316
-rw-r--r--src/egl/main/eglcurrent.h88
-rw-r--r--src/egl/main/egldefines.h46
-rw-r--r--src/egl/main/egldisplay.c221
-rw-r--r--src/egl/main/egldisplay.h156
-rw-r--r--src/egl/main/egldriver.c699
-rw-r--r--src/egl/main/egldriver.h108
-rw-r--r--src/egl/main/eglglobals.c53
-rw-r--r--src/egl/main/eglglobals.h37
-rw-r--r--src/egl/main/eglimage.c90
-rw-r--r--src/egl/main/eglimage.h96
-rw-r--r--src/egl/main/egllog.c176
-rw-r--r--src/egl/main/egllog.h29
-rw-r--r--src/egl/main/eglmisc.c180
-rw-r--r--src/egl/main/eglmisc.h48
-rw-r--r--src/egl/main/eglmode.c352
-rw-r--r--src/egl/main/eglmode.h57
-rw-r--r--src/egl/main/eglmutex.h52
-rw-r--r--src/egl/main/eglscreen.c265
-rw-r--r--src/egl/main/eglscreen.h89
-rw-r--r--src/egl/main/eglstring.c24
-rw-r--r--src/egl/main/eglstring.h18
-rw-r--r--src/egl/main/eglsurface.c556
-rw-r--r--src/egl/main/eglsurface.h184
-rw-r--r--src/egl/main/egltypedefs.h35
49 files changed, 11995 insertions, 0 deletions
diff --git a/src/egl/Makefile b/src/egl/Makefile
new file mode 100644
index 0000000000..5b09e178c6
--- /dev/null
+++ b/src/egl/Makefile
@@ -0,0 +1,31 @@
+# src/egl/Makefile
+
+TOP = ../..
+include $(TOP)/configs/current
+
+SUBDIRS = main drivers
+
+
+default: subdirs
+
+
+subdirs:
+ @for dir in $(SUBDIRS) ; do \
+ if [ -d $$dir ] ; then \
+ (cd $$dir && $(MAKE)) || exit 1 ; \
+ fi \
+ done
+
+install:
+ @for dir in $(SUBDIRS) ; do \
+ if [ -d $$dir ] ; then \
+ (cd $$dir && $(MAKE) install) || exit 1 ; \
+ fi \
+ done
+
+clean:
+ -@for dir in $(SUBDIRS) ; do \
+ if [ -d $$dir ] ; then \
+ (cd $$dir && $(MAKE) clean) ; \
+ fi \
+ done
diff --git a/src/egl/docs/EGL_MESA_screen_surface b/src/egl/docs/EGL_MESA_screen_surface
new file mode 100644
index 0000000000..6beb4ce88e
--- /dev/null
+++ b/src/egl/docs/EGL_MESA_screen_surface
@@ -0,0 +1,564 @@
+Name
+
+ MESA_screen_surface
+
+Name Strings
+
+ EGL_MESA_screen_surface
+
+Contact
+
+ Brian Paul
+
+ To discuss, join the dri-egl@lists.freedesktop.org list.
+
+Status
+
+ Preliminary - totally subject to change.
+
+Version
+
+ 11 (27 January 2006)
+
+Number
+
+ TBD
+
+Dependencies
+
+ EGL 1.0 or later.
+
+Overview
+
+ EGL 1.1 supports three types of drawing surfaces:
+ * Window surfaces
+ * Pixmap surfaces
+ * Pbuffer surfaces
+ This extension defines a fourth type of drawing surface:
+ * Screen surface
+
+ A screen surface is a surface for which the (front) color buffer can
+ be directly displayed (i.e. scanned out) on a monitor (such as a flat
+ panel or CRT). In particular the color buffer memory will be allocated
+ at a location in VRAM (and in a suitable format) which can be displayed
+ by the graphics hardware.
+
+ Note that the width and height of the screen surface need not exactly
+ match the monitor's current resolution. For example, while the monitor
+ may be configured to to show 1024x768 pixels, the associated screen
+ surface may be larger, such as 1200x1000. The "screen origin" attribute
+ will specify which region of the screen surface which is visible on the
+ monitor. The screen surface can be scrolled by changing this origin.
+
+ This extension also defines functions for controlling the monitor's
+ display mode (width, height, refresh rate, etc), and specifing which
+ screen surface is to be displayed on a monitor.
+
+ The new EGLModeMESA type and related functions are very similar to the
+ EGLConfig type and related functions. The user may get a list of
+ supported modes for a screen and specify the mode to be used when
+ displaying a screen surface.
+
+
+Issues
+
+ 1. Should EGL_INTERLACE be a supported mode attribute?
+
+ Arguments against:
+
+ No, this should be provided by another extension which would
+ also provide the mechanisms needed to play back interlaced video
+ material correctly on hardware that supports it.
+ This extension should prefer non-interlaced modes. [M. Danzer]
+
+ Arguments for:
+
+ An interlaced display can be of use without considering video
+ material. Being able to query whether a screen is operating in
+ interlaced mode can be used by applications to control their
+ drawing. For example: avoid drawing 1-pixel-wide horizontal lines
+ if screen is interlaced. [B. Paul]
+
+ Resolution: Defer for future extension?
+
+
+ 2. Should EGL_REFRESH_RATE be a supported mode attribute?
+
+ Arguments for:
+
+ Yes, it's been shown that applications and/or users need to select
+ modes by this. [M. Danzer]
+
+ Many examples have been given in which it's desirable to let the
+ user choose from a variety of refresh rates without having to
+ restart/reconfigure. [B. Paul]
+
+ Arguments against:
+
+ TBD.
+
+ Resolution: Yes.
+
+
+ 3. Exactly how should the list of modes returned by eglChooseConfigMESA
+ be sorted?
+
+ Current method is described in the text below. Subject to change.
+
+ Alternately, leave the sorting order undefined so that each
+ implementation can return the modes in order of "most desirable"
+ to "least desirable" which may depend on the display technology
+ (CRT vs LCD, etc) or other factors.
+
+
+ 4. How should screen blanking be supported? Note that a screen can be
+ disabled or turned off by calling eglShowSurface(dpy, scrn,
+ EGL_NO_SURFACE, EGL_NO_MODE_MESA). But what about power-save mode?
+
+ I would defer this to other extensions that depend on this one.
+ I can imagine people wanting different semantics not just in
+ relation to the power management API being exposed (DPMS or whatever)
+ but also relating to what events can trigger EGL_CONTEXT_LOST. Also
+ I'm not sure whether power management commands are properly operations
+ on the Display or on a screen surface. [A. Jackson]
+
+
+ 5. Should the EGL_PHYSICAL_SIZE_EGL query be kept? The size information
+ isn't always reliable (consider video projectors) but can still be
+ used to determine the pixel aspect ratio.
+
+ Resolution: Omit. The EGL 1.2 specification includes queries for
+ the display resolution and pixel aspect ratio.
+
+
+ 6. Should detailed mode timing information be exposed by this API?
+
+ Probably not. Instead, offer that information in a layered extension.
+
+
+ 7. How should the notion of a screen's "native" mode be expressed?
+ For example, LCD panels have a native resolution and refresh rate
+ that looks best but other sub-optimal resolutions may be supported.
+
+ The mode attribute EGL_OPTIMAL_MESA will be set for modes which
+ best match the screen. [M. Danzer]
+
+
+ 8. Should eglQueryModeStringMESA() be included? This function returns
+ a human-readable string which corresponds to an EGLMode.
+
+ Arguments for:
+
+ A mode name such as "HDTV-720P" might mean more to users than
+ "1280x720@60Hz" if the later were generated via code.
+
+ Arguments against:
+
+ There's no standard syntax for the strings. May cause more
+ trouble than it's worth.
+
+ Postpone for future extension. [A. Jackson]
+
+ Latest discussion leaning toward omitting this function.
+
+
+ 9. Should we use "Get" or "Query" for functions which return state?
+ The EGL 1.x specification doesn't seem to be totally consistent
+ in this regard, but "Query" is used more often.
+
+ Use "Get" for mode-related queries (as for EGLConfigs) but "Query"
+ for everything else.
+
+
+ 10. What should be the default size for screen surfaces?
+
+ For Pbuffer surfaces the default width and height are zero.
+ We'll do the same for screen surfaces. Since there's no function
+ to resize surfaces it's useless to have a 0x0 screen, but this isn't
+ a situation that'll normally be encountered.
+
+
+ 11. Should there be a function for resizing a screen surface?
+
+ Suppose one wants to change the screen's size in the EGL application.
+ Also suppose there's a hardware restriction such that only one screen
+ surface can exist at a time (either for lack of memory or because of
+ memory layout restrictions).
+
+ The basic idea is that the currently displayed screen surface must
+ be deallocated before a new one can be created. Perhaps a resize
+ function would work better?
+
+
+ 12. How should sub-pixel LCD color information be made available?
+ What about the display's gamma value?
+
+ Perhaps expose as additional read-only mode attributes.
+
+ Perhaps postpone for a layered extension.
+
+
+ 13. What happens if the user attempts to delete a screen surface that
+ is currently being shown?
+
+ Spec currently says that's illegal and that an error (TBD) will be
+ generated.
+
+
+ 14. What if the physical screen size can't be determined? Should
+ a query of EGL_PHYSICAL_SIZE_MESA return [0,0]?
+
+ Obsolete: EGL_PHYSICAL_SIZE_MESA not used.
+
+
+ 15. Suppose the device's number of RAMDACs is different from the
+ number of output ports. For example, a graphics card with
+ two RAMDACs but three ports (VGA, DVI, TV).
+
+ Address this in a follow-on extension. [Matthias Hopf]
+
+
+ 16. How should we deal with on-the-fly device changes? For example,
+ the monitor being unplugged and replaced by another with different
+ characteristics?
+
+ A HAL event could be received via DBUS in the application [J. Smirl,
+ A. Jackson].
+
+ Should there be an EGL mechanism for detecting this? Maybe an
+ EGL_SCREEN_LOST error (similar to EGL_CONTEXT_LOST) can be recorded
+ when there's a screen change. At least then the application can
+ poll to detect this situation.
+
+ Maybe leave that to a future extension.
+
+ See also the EGL_SCREEN_COUNT_MESA query.
+
+
+ 17. What if pixel-accurate panning is not supported (see
+ eglScreenPositionMESA)? [M. Danzer]
+
+ Is this a common problem? Can we ignore it for now?
+
+
+ 18. Should eglShowSurfaceMESA be renamed to eglShowScreenSurfaceMESA?
+
+ Probably.
+
+
+
+New Procedures and Functions
+
+ EGLBoolean eglChooseModeMESA(EGLDisplay dpy, EGLScreenMESA screen,
+ const EGLint *attrib_list,
+ EGLModeMESA *modes, EGLint modes_size,
+ EGLint *num_modes)
+
+ EGLBoolean eglGetModesMESA(EGLDisplay dpy, EGLScreenMESA screen,
+ EGLModeMESA *modes, EGLint modes_size,
+ EGLint *num_modes)
+
+ EGLBoolean eglGetModeAttribMESA(EGLDisplay dpy, EGLModeMESA mode,
+ EGLint attrib, EGLint *value)
+
+
+ EGLBoolean eglGetScreensMESA(EGLDisplay dpy, EGLScreenMESA *screens,
+ EGLint screens_size, EGLint *num_screens)
+
+ EGLSurface eglCreateScreenSurfaceMESA(EGLDisplay dpy, EGLConfig config,
+ const EGLint *attrib_list)
+
+ EGLBoolean eglShowSurfaceMESA(EGLDisplay dpy, EGLScreenMESA screen,
+ EGLSurface surface, EGLModeMESA mode)
+
+ EGLBoolean eglScreenPositionMESA(EGLDisplay dpy, EGLScreenMESA screen,
+ EGLint x, EGLint y)
+
+
+ EGLBoolean eglQueryScreenMESA(EGLDisplay dpy, EGLScreenMESA screen,
+ EGLint attrib, EGLint *value);
+
+ EGLBoolean eglQueryScreenSurfaceMESA(EGLDisplay dpy, EGLScreenMESA screen,
+ EGLSurface *surface)
+
+ EGLBoolean eglQueryScreenModeMESA(EGLDisplay dpy, EGLScreenMESA screen,
+ EGLModeMESA *mode)
+
+ const char *eglQueryModeStringMESA(EGLDisplay dpy, EGLMode mode);
+
+
+New Types
+
+ EGLModeMESA
+ EGLScreenMESA
+
+New Tokens
+
+ New error codes:
+
+ EGL_BAD_SCREEN_MESA
+ EGL_BAD_MODE_MESA
+
+ Screen-related tokens:
+
+ EGL_SCREEN_COUNT_MESA
+ EGL_SCREEN_POSITION_MESA
+ EGL_SCREEN_BIT_MESA
+ EGL_SCREEN_POSITION_GRANULARITY_MESA
+
+ Mode-related tokens:
+
+ EGL_MODE_ID_MESA
+ EGL_REFRESH_RATE_MESA
+ EGL_INTERLACED_MESA
+ EGL_OPTIMAL_MESA
+ EGL_NO_MODE_MESA
+
+
+Additions to Chapter X of the EGL 1.1 Specification
+
+ [XXX this all has to be rewritten to fit into the EGL specification
+ and match the conventions of an EGL extension. For now, just list
+ all the functions with brief descriptions.]
+
+
+ EGLBoolean eglChooseModeMESA(EGLDisplay dpy, const EGLScreenMESA screen,
+ EGLint *attrib_list, EGLModeMESA *modes,
+ EGLint modes_size, EGLint *num_modes)
+
+ Like eglChooseConfig, returns a list of EGLModes which match the given
+ attribute list. This does not set the screen's current display mode.
+ The attribute list is a list of token/value pairs terminated with
+ EGL_NONE. Supported attributes include:
+
+ Name Description
+ --------------------- ---------------------------------------------
+ EGL_WIDTH Mode width (resolution)
+ EGL_HEIGHT Mode height (resolution)
+ EGL_REFRESH_RATE_MESA The mode's refresh rate, multiplied by 1000
+ EGL_INTERLACED_MESA 1 indicates an interlaced mode, 0 otherwise
+ EGL_OPTIMAL_MESA Set if the most is especially optimal for the
+ screen (ex. for particular LCD resolutions)
+
+ Any other token will generate the error EGL_BAD_ATTRIBUTE.
+
+ The list of modes returned by eglChooseModeMESA will be sorted
+ according to the following criteria. See the discussion of table 3.3
+ in the EGL specification for more information.
+
+ Selection Sort Sort
+ Attribute Default Criteria Order Priority
+ -------------------- -------------- ----------- ------ --------
+ EGL_OPTIMAL_MESA EGL_DONT_CARE Exact 1,0 1
+ EGL_INTERLACED_MESA EGL_DONT_CARE Exact 0,1 2
+ EGL_REFRESH_RATE EGL_DONT_CARE AtLeast Larger 3
+ EGL_WIDTH EGL_DONT_CARE AtLeast Larger 4
+ EGL_HEIGHT EGL_DONT_CARE AtLeast Larger 5
+ EGL_MODE_ID_MESA EGL_DONT_CARE Exact Smaller 6
+
+
+ EGLBoolean eglGetModesMESA(EGLDisplay dpy, EGLScreenMESA screen,
+ EGLModeMESA *modes, EGLint modes_size,
+ EGLint *num_modes)
+
+ Like eglGetConfigs, returns a list of all modes supported by the
+ given screen. The returned modes will be sorted in the same manner
+ as for eglChooseModeMESA().
+
+
+
+ EGLBoolean eglGetModeAttribMESA(EGLDisplay dpy, EGLModeMESA mode,
+ EGLint attrib, EGLint *value)
+
+ Used to query mode attributes. The following attributes are supported:
+
+ Name Return value description
+ --------------------- ----------------------------------------------
+ EGL_OPTIMAL_MESA 1 indicates an optimal mode, 0 otherwise
+ EGL_INTERLACED_MESA 1 indicates an interlaced mode, 0 otherwise
+ EGL_REFRESH_RATE_MESA The mode's refresh rate, multiplied by 1000
+ EGL_WIDTH Mode width (resolution)
+ EGL_HEIGHT Mode height (resolution)
+ EGL_MODE_ID_MESA A unique small integer identifier for the mode
+
+ Any other token will generate the error EGL_BAD_ATTRIBUTE.
+
+
+
+ EGLBoolean eglGetScreensMESA(EGLDisplay dpy, EGLScreenMESA *screens,
+ EGLint screens_size, EGLint *num_screens)
+
+ This function returns an array of all available screen handles.
+ <screens_size> is the maximum number of screens to return in the
+ <screens> array. <num_screens> will return the number of screen handles
+ placed in the array, even if <screens> is NULL.
+
+ The number of screens and the availability of each may change over
+ time (hot-plugging). Screen handles will not be reused. When a
+ screen handle becomes invalid, function calls which reference an
+ invalid handle will generate EGL_BAD_SCREEN_MESA.
+
+ The first screen handle returned will be considered to be the primary
+ one.
+
+
+
+ EGLSurface eglCreateScreenSurfaceMESA(EGLDisplay dpy, EGLConfig config,
+ const EGLint *attrib_list)
+
+ Create a surface that can be displayed on a screen. <attrib_list> is
+ an array of token/value pairs terminated with EGL_NONE. Valid tokens
+ include:
+
+ Name Description
+ ---------------- --------------------------------
+ EGL_WIDTH desired surface width in pixels
+ EGL_HEIGHT desired surface height in pixels
+
+ Any other token will generate the error EGL_BAD_ATTRIBUTE.
+ The default width and height are zero.
+
+
+
+ EGLBoolean eglShowSurfaceMESA(EGLDisplay dpy, EGLScreenMESA screen,
+ EGLSurface surface, EGLModeMESA mode)
+
+ This function causes a screen to show the given surface (or more
+ precisely, the surface's front color buffer) with the given mode.
+
+ If the surface is in any way incompatible with the mode, the error
+ EGL_BAD_MATCH will be generated, EGL_FALSE will be returned, and the
+ previous screen state will remain in effect. This might occur when
+ the bandwidth of the video-out subsystem is exceeded, or if the mode
+ specifies a width or height that's greater than the width or height
+ of the surface.
+
+ To disable a screen, the values EGL_NO_SURFACE and EGL_NO_MODE_MESA
+ be passed as the <surface> and <mode> parameters.
+
+ The values of EGL_SCREEN_POSITION_MESA are clamped to the new valid
+ range computed from the screen size and surface size. If the new
+ surface is EGL_NO_SURFACE, EGL_SCREEN_POSITION_MESA is set to [0, 0].
+
+
+ Attempting to delete a screen surface which is currently being
+ displayed will result in the error EGL_BAD_ACCESS being generated.
+
+
+
+ EGLBoolean eglScreenPositionMESA(EGLDisplay dpy, EGLScreenMESA screen,
+ EGLint x, EGLint y)
+
+ Specifies the origin of the screen's view into the surface, if the
+ surface is larger than the screen. Valid values for x and y are
+ [0, surfaceWidth - screenWidth] and [0, surfaceHeight - screenHeight],
+ respectively.
+
+ The x and y values are also constrained to be integer multiples of the
+ EGL_SCREEN_POSITION_GRANULARITY_MESA values.
+
+
+
+
+ EGLBoolean eglQueryScreenMESA(EGLDisplay dpy, EGLScreenMESA screen,
+ EGLint attrib, EGLint *value);
+
+ Used to query screen attributes. <attrib> may be one of the following:
+
+ Name Return value description
+ ------------------------ ---------------------------------------------
+ EGL_SCREEN_POSITION_MESA x, y position of the screen's origin with
+ respect to the surface. If no surface is
+ attached to the screen, [0, 0] is returned.
+ EGL_SCREEN_POSITION_GRANULARITY_MESA
+ Returns the granularity, in pixels, for
+ which the screen position is constrained.
+
+ Any other token will generate the error EGL_BAD_ATTRIBUTE.
+
+
+
+
+ EGLBoolean eglQueryScreenSurfaceMESA(EGLDisplay dpy, EGLScreenMESA screen,
+ EGLSurface *surface)
+
+ Returns the surface currently displayed on the given screen. <surface>
+ may be EGL_NO_SURFACE if the screen isn't currently showing any surface.
+
+
+
+
+ EGLBoolean eglQueryScreenModeMESA(EGLDisplay dpy, EGLScreenMESA screen,
+ EGLModeMESA *mode)
+
+ Returns the given screen's current display mode. The mode may be
+ EGL_NO_MODE_MESA if the screen is currently disabled.
+
+
+
+ const char *eglQueryModeStringMESA(EGLDisplay dpy, EGLModeMESA mode);
+
+ Returns a human-readable string for the given mode. The string is a
+ zero-terminated C string which the user should not attempt to free.
+ There is no standard syntax for mode strings. Applications should
+ not directly rely on mode strings.
+
+
+
+Version History
+
+ 1. 15 March 2005 - BrianP
+ Initial version
+
+ 2. 16 March 2005 - BrianP
+ Removed EGL_DEPTH_MESA
+ Added EGL_PHYSICAL_WIDTH_MESA, EGL_PHYSICAL_HEIGHT_MESA queries
+ Added EGL_OPTIMAL_MESA for width/height/refresh rate selection
+ Added possible eglQueryModeStringMESA() function
+ More details of the new functions explained.
+
+ 3. 18 March 2005 - BrianP
+ Added screen_number to eglChooseModeMESA().
+ Fix off by one mistake in value range for ORIGIN attributes
+ Added Issues section
+
+ 4. 21 March 2005 - BrianP
+ Removed eglScreenAttribsMESA().
+ Added eglScreenPositionMESA() to set screen origin.
+ Replaced EGL_SCREEN_X/Y_OFFSET_MESA with EGL_SCREEN_POSITION_MESA.
+ Replaced EGL_PHYSICAL_WIDTH/HEIGHT_MESA with EGL_PHYSICAL_SIZE_MESA.
+ Use EGL_OPTIMAL_MESA as a new mode attribute. (Michel Danzer)
+ Added a few more issues.
+
+ 5. 6 April 2005 - BrianP
+ More language for eglGetModeStringMESA().
+ Added issues 10, 11, 12, 13, 14.
+ Updated issue 3 discussion about mode sorting.
+
+ 6. 22 April 2005 - BrianP
+ Fixed "LDC" typo.
+ Added issues 15, 16.
+ Changed dependency on EGL 1.1 to EGL 1.0
+ s/EGL_NUM_SCREENS_MESA/EGL_SCREEN_COUNT_MESA/
+ Added eglQueryDisplayMESA() to New Functions section.
+ Clarified language for the EGL_SCREEN_COUNT_MESA query.
+
+ 7. 29 April 2005 - BrianP
+ Added EGLScreenMESA type and eglGetScreensMESA() function. [J. Smirl].
+ Replaced EGLint screen_number parameters with EGLScreenMESA screen.
+ Added issue 17 (pixel-accurate panning)
+
+ 8. 2 May 2005 - BrianP
+ Removed eglQueryDisplayMESA.
+ Fixed a few more EGLint -> EGLScreenMESA changes.
+
+ 9. 20 May 2005 - BrianP
+ Fixed a few typos.
+ Updated some open issues text.
+
+ 10. 10 August 2005 - BrianP
+ Added EGL_SCREEN_POSITION_GRANULARITY_MESA.
+
+ 11. 27 January 2006 - BrianP
+ EGL_PHYSICAL_SIZE_MESA removed since EGL 1.2 has a similar feature.
+
diff --git a/src/egl/drivers/Makefile b/src/egl/drivers/Makefile
new file mode 100644
index 0000000000..dde4ee2255
--- /dev/null
+++ b/src/egl/drivers/Makefile
@@ -0,0 +1,31 @@
+# src/egl/drivers/Makefile
+
+TOP = ../../..
+include $(TOP)/configs/current
+
+SUBDIRS = $(EGL_DRIVERS_DIRS)
+
+
+default: subdirs
+
+
+subdirs:
+ @for dir in $(SUBDIRS) ; do \
+ if [ -d $$dir ] ; then \
+ (cd $$dir ; $(MAKE)) || exit 1 ; \
+ fi \
+ done
+
+install:
+ @ for dir in $(SUBDIRS) ; do \
+ if [ -d $$dir ] ; then \
+ (cd $$dir ; $(MAKE) install) || exit 1 ; \
+ fi \
+ done
+
+clean:
+ @for dir in $(SUBDIRS) ; do \
+ if [ -d $$dir ] ; then \
+ (cd $$dir ; $(MAKE) clean) ; \
+ fi \
+ done
diff --git a/src/egl/drivers/Makefile.template b/src/egl/drivers/Makefile.template
new file mode 100644
index 0000000000..08e82c65e9
--- /dev/null
+++ b/src/egl/drivers/Makefile.template
@@ -0,0 +1,52 @@
+# src/egl/drivers/Makefile.template
+#
+# Drivers should define
+#
+# EGL_DRIVER, the driver name
+# EGL_SOURCES, the driver sources
+# EGL_INCLUDES, the include pathes
+# EGL_CFLAGS, additional CFLAGS
+# EGL_LIBS, additional LIBS
+#
+# before including this template.
+#
+
+
+EGL_DRIVER_PATH = $(TOP)/$(LIB_DIR)/egl/$(EGL_DRIVER)
+EGL_OBJECTS = $(EGL_SOURCES:.c=.o)
+
+
+default: depend $(EGL_DRIVER_PATH)
+
+$(EGL_DRIVER_PATH): $(EGL_DRIVER)
+ @$(INSTALL) -d $(TOP)/$(LIB_DIR)/egl
+ $(INSTALL) $< $(TOP)/$(LIB_DIR)/egl
+
+$(EGL_DRIVER): $(EGL_OBJECTS) Makefile $(TOP)/src/egl/drivers/Makefile.template
+ @$(MKLIB) -o $(EGL_DRIVER) -noprefix \
+ -linker '$(CC)' -ldflags '$(LDFLAGS)' \
+ -L$(TOP)/$(LIB_DIR) $(MKLIB_OPTIONS) \
+ $(EGL_OBJECTS) $(EGL_LIBS) -l$(EGL_LIB)
+
+.c.o:
+ $(CC) -c $(EGL_INCLUDES) $(CFLAGS) $(EGL_CFLAGS) $< -o $@
+
+
+install: $(EGL_DRIVER_PATH)
+ $(INSTALL) -d $(DESTDIR)$(EGL_DRIVER_INSTALL_DIR)
+ $(MINSTALL) $(EGL_DRIVER_PATH) $(DESTDIR)$(EGL_DRIVER_INSTALL_DIR)
+
+clean:
+ rm -f $(EGL_DRIVER)
+ rm -f $(EGL_OBJECTS)
+ rm -f depend depend.bak
+
+depend: $(EGL_SOURCES)
+ @ echo "running $(MKDEP)"
+ @ rm -f depend
+ @ touch depend
+ $(MKDEP) $(MKDEP_OPTIONS) $(EGL_INCLUDES) $(EGL_SOURCES) \
+ >/dev/null 2>/dev/null
+
+sinclude depend
+# DO NOT DELETE
diff --git a/src/egl/drivers/dri/Makefile b/src/egl/drivers/dri/Makefile
new file mode 100644
index 0000000000..c3aacff1cf
--- /dev/null
+++ b/src/egl/drivers/dri/Makefile
@@ -0,0 +1,69 @@
+# src/egl/drivers/dri/Makefile
+
+TOP = ../../../..
+include $(TOP)/configs/current
+
+
+### Include directories
+INCLUDE_DIRS = \
+ -I. \
+ -I/usr/include \
+ $(shell pkg-config --cflags-only-I libdrm) \
+ -I$(TOP)/include \
+ -I$(TOP)/include/GL/internal \
+ -I$(TOP)/src/mapi \
+ -I$(TOP)/src/mesa \
+ -I$(TOP)/src/mesa/main \
+ -I$(TOP)/src/mesa/math \
+ -I$(TOP)/src/mesa/transform \
+ -I$(TOP)/src/mesa/shader \
+ -I$(TOP)/src/mesa/swrast \
+ -I$(TOP)/src/mesa/swrast_setup \
+ -I$(TOP)/src/egl/main \
+ -I$(TOP)/src/mesa/drivers/dri/common
+
+
+HEADERS = egldri.h
+
+SOURCES = egldri.c
+
+OBJECTS = $(SOURCES:.c=.o)
+
+DRM_LIB = `pkg-config --libs libdrm`
+
+
+.c.o:
+ $(CC) -c $(INCLUDE_DIRS) $(CFLAGS) $< -o $@
+
+
+
+default: depend library Makefile
+
+
+# EGLdri Library
+library: $(TOP)/$(LIB_DIR)/libEGLdri.so
+
+$(TOP)/$(LIB_DIR)/libEGLdri.so: $(OBJECTS)
+ $(MKLIB) -o EGLdri -linker '$(CC)' -ldflags '$(LDFLAGS)' \
+ -major 1 -minor 0 \
+ -install $(TOP)/$(LIB_DIR) -ldl $(OBJECTS) $(LIBS)
+
+install:
+ $(INSTALL) -d $(DESTDIR)$(INSTALL_LIB_DIR)
+ $(MINSTALL) $(TOP)/$(LIB_DIR)/libEGLdri.so $(DESTDIR)$(INSTALL_LIB_DIR)
+
+clean:
+ -rm -f *.o
+ -rm -f *.so
+ -rm -f depend depend.bak
+
+depend: $(SOURCES) $(HEADERS)
+ @ echo "running $(MKDEP)"
+ @ rm -f depend
+ @ touch depend
+ $(MKDEP) $(MKDEP_OPTIONS) $(DEFINES) $(INCLUDE_DIRS) \
+ $(SOURCES) $(HEADERS) >/dev/null 2>/dev/null
+
+include depend
+# DO NOT DELETE
+
diff --git a/src/egl/drivers/dri/egldri.c b/src/egl/drivers/dri/egldri.c
new file mode 100644
index 0000000000..6a8bf89985
--- /dev/null
+++ b/src/egl/drivers/dri/egldri.c
@@ -0,0 +1,1205 @@
+/**
+ * Generic EGL driver for DRI. This is basically an "adaptor" driver
+ * that allows libEGL to load/use regular DRI drivers.
+ *
+ * This file contains all the code needed to interface DRI-based drivers
+ * with libEGL.
+ *
+ * There's a lot of dependencies on fbdev and the /sys/ filesystem.
+ */
+
+
+#include <dirent.h>
+#include <stdio.h>
+#include <string.h>
+#include <linux/fb.h>
+#include <assert.h>
+#include <dlfcn.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/time.h>
+
+#include "egldriver.h"
+#include "egldisplay.h"
+#include "eglcontext.h"
+#include "eglconfig.h"
+#include "eglconfigutil.h"
+#include "eglsurface.h"
+#include "eglscreen.h"
+#include "eglglobals.h"
+#include "egllog.h"
+#include "eglmode.h"
+
+#include "egldri.h"
+
+const char *sysfs = "/sys/class";
+
+static const int empty_attribute_list[1] = { None };
+
+
+
+/**
+ * Given a card number, return the name of the DRI driver to use.
+ * This generally means reading the contents of
+ * /sys/class/drm/cardX/dri_library_name, where X is the card number
+ */
+static EGLBoolean
+driver_name_from_card_number(int card, char *driverName, int maxDriverName)
+{
+ char path[2000];
+ FILE *f;
+ int length;
+
+ snprintf(path, sizeof(path), "%s/drm/card%d/dri_library_name", sysfs, card);
+
+ f = fopen(path, "r");
+ if (!f)
+ return EGL_FALSE;
+
+ fgets(driverName, maxDriverName, f);
+ fclose(f);
+
+ if ((length = strlen(driverName)) > 1) {
+ /* remove the trailing newline from sysfs */
+ driverName[length - 1] = '\0';
+ strncat(driverName, "_dri", maxDriverName);
+ return EGL_TRUE;
+ }
+ else {
+ return EGL_FALSE;
+ }
+}
+
+
+
+/**
+ * The bootstrap function.
+ * Return a new driDriver object and plug in API functions.
+ * This function, in turn, loads a specific DRI driver (ex: r200_dri.so).
+ */
+_EGLDriver *
+_eglMain(_EGLDisplay *dpy, const char *args)
+{
+#if 1
+ const int card = args ? atoi(args) : 0;
+ _EGLDriver *driver = NULL;
+ char driverName[1000];
+
+ if (!driver_name_from_card_number(card, driverName, sizeof(driverName))) {
+ _eglLog(_EGL_WARNING,
+ "Unable to determine driver name for card %d\n", card);
+ return NULL;
+ }
+
+ _eglLog(_EGL_DEBUG, "Driver name: %s\n", driverName);
+
+ driver = _eglOpenDriver(dpy, driverName, args);
+
+ return driver;
+
+#else
+
+ int length;
+ char path[NAME_MAX];
+ struct dirent *dirent;
+#if 1
+ FILE *file;
+#endif
+ DIR *dir;
+ _EGLDriver *driver = NULL;;
+
+ snprintf(path, sizeof(path), "%s/drm", sysfs);
+ if (!(dir = opendir(path))) {
+ _eglLog(_EGL_WARNING, "%s DRM devices not found.", path);
+ return EGL_FALSE;
+ }
+
+ /* loop over dir entries looking for cardX where "X" is in the
+ * dpy->DriverName ":X" string.
+ */
+ while ((dirent = readdir(dir))) {
+
+ if (strncmp(&dirent->d_name[0], "card", 4) != 0)
+ continue;
+ if (strcmp(&dirent->d_name[4], &driverName[1]) != 0)
+ continue;
+
+ snprintf(path, sizeof(path), "%s/drm/card%s/dri_library_name",
+ sysfs, &driverName[1]);
+ _eglLog(_EGL_INFO, "Opening %s", path);
+#if 1
+ file = fopen(path, "r");
+ if (!file) {
+ _eglLog(_EGL_WARNING, "Failed to open %s", path);
+ return NULL;
+ }
+ fgets(path, sizeof(path), file);
+ fclose(file);
+#else
+ strcpy(path, "r200\n");
+#endif
+ if ((length = strlen(path)) > 0)
+ path[length - 1] = '\0'; /* remove the trailing newline from sysfs */
+ strncat(path, "_dri", sizeof(path));
+
+ driver = _eglOpenDriver(dpy, path);
+
+ break;
+ }
+ closedir(dir);
+
+ return driver;
+#endif
+}
+
+
+/**
+ * Called by eglCreateContext via drv->API.CreateContext().
+ */
+static EGLContext
+_eglDRICreateContext(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
+ EGLContext share_list, const EGLint *attrib_list)
+{
+ driDisplay *disp = Lookup_driDisplay(dpy);
+ driContext *c, *share;
+ void *sharePriv;
+ _EGLConfig *conf;
+ __GLcontextModes visMode;
+
+ c = (driContext *) calloc(1, sizeof(*c));
+ if (!c)
+ return EGL_NO_CONTEXT;
+
+ conf = _eglLookupConfig(drv, dpy, config);
+ assert(conf);
+
+ if (!_eglInitContext(drv, &c->Base, conf, attrib_list)) {
+ free(c);
+ return EGL_NO_CONTEXT;
+ }
+
+ if (share_list != EGL_NO_CONTEXT) {
+ _EGLContext *shareCtx = _eglLookupContext(share_list);
+ if (!shareCtx) {
+ _eglError(EGL_BAD_CONTEXT, "eglCreateContext(share_list)");
+ return EGL_FALSE;
+ }
+ }
+ share = Lookup_driContext(share_list);
+ if (share)
+ sharePriv = share->driContext.private;
+ else
+ sharePriv = NULL;
+
+ _eglConfigToContextModesRec(conf, &visMode);
+
+ c->driContext.private = disp->driScreen.createNewContext(disp, &visMode,
+ GLX_WINDOW_BIT, sharePriv, &c->driContext);
+ if (!c->driContext.private) {
+ free(c);
+ return EGL_FALSE;
+ }
+
+ /* link to display */
+ _eglLinkContext(&c->Base, &disp->Base);
+
+ return _eglGetContextHandle(&c->Base);
+}
+
+
+static EGLBoolean
+_eglDRIMakeCurrent(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw,
+ EGLSurface read, EGLContext context)
+{
+ driDisplay *disp = Lookup_driDisplay(dpy);
+ driContext *ctx = Lookup_driContext(context);
+ EGLBoolean b;
+ __DRIid drawBuf = (__DRIid) draw;
+ __DRIid readBuf = (__DRIid) read;
+
+ b = _eglMakeCurrent(drv, dpy, draw, read, context);
+ if (!b)
+ return EGL_FALSE;
+
+ if (ctx) {
+ ctx->driContext.bindContext(disp, 0, drawBuf, readBuf, &ctx->driContext);
+ }
+ else {
+ /* what's this??? */
+ /* _mesa_make_current( NULL, NULL, NULL );*/
+ }
+ return EGL_TRUE;
+}
+
+
+static EGLSurface
+_eglDRICreatePbufferSurface(_EGLDriver *drv, EGLDisplay dpy, EGLConfig config,
+ const EGLint *attrib_list)
+{
+ driSurface *surf;
+ _EGLConfig *conf;
+
+ conf = _eglLookupConfig(drv, dpy, config);
+ assert(conf);
+
+ surf = (driSurface *) calloc(1, sizeof(*surf));
+ if (!surf) {
+ return EGL_NO_SURFACE;
+ }
+
+ if (!_eglInitSurface(drv, &surf->Base, EGL_PBUFFER_BIT,
+ conf, attrib_list)) {
+ free(surf);
+ return EGL_NO_SURFACE;
+ }
+
+ /* create software-based pbuffer */
+ {
+#if 0
+ GLcontext *ctx = NULL; /* this _should_ be OK */
+#endif
+ __GLcontextModes visMode;
+ _EGLConfig *conf = _eglLookupConfig(drv, dpy, config);
+ assert(conf); /* bad config should be caught earlier */
+ _eglConfigToContextModesRec(conf, &visMode);
+
+#if 0
+ surf->mesa_framebuffer = _mesa_create_framebuffer(&visMode);
+ _mesa_add_soft_renderbuffers(surf->mesa_framebuffer,
+ GL_TRUE, /* color bufs */
+ visMode.haveDepthBuffer,
+ visMode.haveStencilBuffer,
+ visMode.haveAccumBuffer,
+ GL_FALSE, /* alpha */
+ GL_FALSE /* aux */ );
+
+ /* set pbuffer/framebuffer size */
+ _mesa_resize_framebuffer(ctx, surf->mesa_framebuffer,
+ surf->Base.Width, surf->Base.Height);
+#endif
+ }
+
+ _eglLinkSurface(&surf->Base, _eglLookupDisplay(dpy));
+
+ return surf->Base.Handle;
+}
+
+
+static EGLBoolean
+_eglDRIDestroySurface(_EGLDriver *drv, EGLDisplay dpy, EGLSurface surface)
+{
+ driDisplay *disp = Lookup_driDisplay(dpy);
+ driSurface *fs = Lookup_driSurface(surface);
+
+ _eglUnlinkSurface(&fs->Base);
+
+ fs->drawable.destroyDrawable(disp, fs->drawable.private);
+
+ if (!_eglIsSurfaceBound(&fs->Base))
+ free(fs);
+ return EGL_TRUE;
+}
+
+
+static EGLBoolean
+_eglDRIDestroyContext(_EGLDriver *drv, EGLDisplay dpy, EGLContext context)
+{
+ driDisplay *disp = Lookup_driDisplay(dpy);
+ driContext *fc = Lookup_driContext(context);
+
+ _eglUnlinkContext(&fc->Base);
+
+ fc->driContext.destroyContext(disp, 0, fc->driContext.private);
+
+ if (!_eglIsContextBound(&fc->Base))
+ free(fc);
+ return EGL_TRUE;
+}
+
+
+/**
+ * Create a drawing surface which can be directly displayed on a screen.
+ */
+static EGLSurface
+_eglDRICreateScreenSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy, EGLConfig cfg,
+ const EGLint *attrib_list)
+{
+ _EGLConfig *config = _eglLookupConfig(drv, dpy, cfg);
+ driDisplay *disp = Lookup_driDisplay(dpy);
+ driSurface *surface;
+ __GLcontextModes visMode;
+ __DRIid drawBuf;
+
+ surface = (driSurface *) calloc(1, sizeof(*surface));
+ if (!surface) {
+ return EGL_NO_SURFACE;
+ }
+
+ /* init base class, do error checking, etc. */
+ if (!_eglInitSurface(drv, &surface->Base, EGL_SCREEN_BIT_MESA,
+ config, attrib_list)) {
+ free(surface);
+ return EGL_NO_SURFACE;
+ }
+
+ _eglLinkSurface(&surface->Base &disp->Base);
+
+
+ /*
+ * XXX this is where we should allocate video memory for the surface!
+ */
+
+
+ /* convert EGLConfig to GLvisual */
+ _eglConfigToContextModesRec(config, &visMode);
+
+ drawBuf = (__DRIid) _eglGetSurfaceHandle(&surface->Base);
+
+ /* Create a new DRI drawable */
+ if (!disp->driScreen.createNewDrawable(disp, &visMode, drawBuf,
+ &surface->drawable, GLX_WINDOW_BIT,
+ empty_attribute_list)) {
+ _eglUnlinkSurface(&surface->Base);
+ free(surface);
+ return EGL_NO_SURFACE;
+ }
+
+ return surface->Base.Handle;
+}
+
+
+/**
+ * Set the fbdev colormap to a simple linear ramp.
+ */
+static void
+_eglDRILoadColormap(driScreen *scrn)
+{
+ char path[ NAME_MAX ];
+ char *buffer;
+ int i, fd;
+
+ /* cmap attribute uses 256 lines of 16 bytes.
+ * Allocate one extra char for the \0 added by sprintf()
+ */
+ if ( !( buffer = malloc( 256 * 16 + 1 ) ) ) {
+ _eglLog(_EGL_WARNING, "Out of memory in _eglDRILoadColormap");
+ return;
+ }
+
+ /* cmap attribute uses 256 lines of 16 bytes */
+ for ( i = 0; i < 256; i++ ) {
+ int c = (i << 8) | i; /* expand to 16-bit value */
+ sprintf(&buffer[i * 16], "%02x%c%04x%04x%04x\n", i, ' ', c, c, c);
+ }
+
+ snprintf(path, sizeof(path), "%s/graphics/%s/color_map", sysfs, scrn->fb);
+ if ( !( fd = open( path, O_RDWR ) ) ) {
+ _eglLog(_EGL_WARNING, "Unable to open %s to set colormap", path);
+ return;
+ }
+ write( fd, buffer, 256 * 16 );
+ close( fd );
+
+ free( buffer );
+}
+
+
+/**
+ * Show the given surface on the named screen.
+ * If surface is EGL_NO_SURFACE, disable the screen's output.
+ * Called via eglShowSurfaceMESA().
+ */
+EGLBoolean
+_eglDRIShowScreenSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy,
+ EGLScreenMESA screen,
+ EGLSurface surface, EGLModeMESA m)
+{
+ driDisplay *display = Lookup_driDisplay(dpy);
+ driScreen *scrn = Lookup_driScreen(dpy, screen);
+ driSurface *surf = Lookup_driSurface(surface);
+ _EGLMode *mode = _eglLookupMode(dpy, m);
+ FILE *file;
+ char fname[NAME_MAX], buffer[1000];
+ int temp;
+
+ _eglLog(_EGL_DEBUG, "Enter _eglDRIShowScreenSurface");
+
+ /* This will check that surface, screen, and mode are valid.
+ * Also, it checks that the surface is large enough for the mode, etc.
+ */
+ if (!_eglShowScreenSurfaceMESA(drv, dpy, screen, surface, m))
+ return EGL_FALSE;
+
+ assert(surface == EGL_NO_SURFACE || surf);
+ assert(m == EGL_NO_MODE_MESA || mode);
+ assert(scrn);
+
+ /*
+ * Blank/unblank screen depending on if m == EGL_NO_MODE_MESA
+ */
+ snprintf(fname, sizeof(fname), "%s/graphics/%s/blank", sysfs, scrn->fb);
+ file = fopen(fname, "r+");
+ if (!file) {
+ _eglLog(_EGL_WARNING, "kernel patch?? chown all fb sysfs attrib to allow"
+ " write - %s\n", fname);
+ return EGL_FALSE;
+ }
+ snprintf(buffer, sizeof(buffer), "%d",
+ (m == EGL_NO_MODE_MESA ? VESA_POWERDOWN : VESA_VSYNC_SUSPEND));
+ fputs(buffer, file);
+ fclose(file);
+
+ if (m == EGL_NO_MODE_MESA) {
+ /* all done! */
+ return EGL_TRUE;
+ }
+
+ _eglLog(_EGL_INFO, "Setting display mode to %d x %d, %d bpp",
+ mode->Width, mode->Height, display->bpp);
+
+ /*
+ * Set the display mode
+ */
+ snprintf(fname, sizeof(fname), "%s/graphics/%s/mode", sysfs, scrn->fb);
+ file = fopen(fname, "r+");
+ if (!file) {
+ _eglLog(_EGL_WARNING, "Failed to open %s to set mode", fname);
+ return EGL_FALSE;
+ }
+ /* note: nothing happens without the \n! */
+ snprintf(buffer, sizeof(buffer), "%s\n", mode->Name);
+ fputs(buffer, file);
+ fclose(file);
+ _eglLog(_EGL_INFO, "Set mode to %s in %s", mode->Name, fname);
+
+ /*
+ * Set display bpp
+ */
+ snprintf(fname, sizeof(fname), "%s/graphics/%s/bits_per_pixel",
+ sysfs, scrn->fb);
+ file = fopen(fname, "r+");
+ if (!file) {
+ _eglLog(_EGL_WARNING, "Failed to open %s to set bpp", fname);
+ return EGL_FALSE;
+ }
+ display->bpp = GET_CONFIG_ATTRIB(surf->Base.Config, EGL_BUFFER_SIZE);
+ display->cpp = display->bpp / 8;
+ snprintf(buffer, sizeof(buffer), "%d", display->bpp);
+ fputs(buffer, file);
+ fclose(file);
+
+ /*
+ * Unblank display
+ */
+ snprintf(fname, sizeof(fname), "%s/graphics/%s/blank", sysfs, scrn->fb);
+ file = fopen(fname, "r+");
+ if (!file) {
+ _eglLog(_EGL_WARNING, "Failed to open %s", fname);
+ return EGL_FALSE;
+ }
+ snprintf(buffer, sizeof(buffer), "%d", VESA_NO_BLANKING);
+ fputs(buffer, file);
+ fclose(file);
+
+ /*
+ * Set fbdev buffer virtual size to surface's size.
+ */
+ snprintf(fname, sizeof(fname), "%s/graphics/%s/virtual_size", sysfs, scrn->fb);
+ file = fopen(fname, "r+");
+ snprintf(buffer, sizeof(buffer), "%d,%d", surf->Base.Width, surf->Base.Height);
+ fputs(buffer, file);
+ rewind(file);
+ fgets(buffer, sizeof(buffer), file);
+ sscanf(buffer, "%d,%d", &display->virtualWidth, &display->virtualHeight);
+ fclose(file);
+
+ /*
+ * round up pitch as needed
+ */
+ temp = display->virtualWidth;
+ switch (display->bpp / 8) {
+ case 1: temp = (display->virtualWidth + 127) & ~127; break;
+ case 2: temp = (display->virtualWidth + 31) & ~31; break;
+ case 3:
+ case 4: temp = (display->virtualWidth + 15) & ~15; break;
+ default:
+ _eglLog(_EGL_WARNING, "Bad display->bpp = %d in _eglDRIShowScreenSurface");
+ }
+ display->virtualWidth = temp;
+
+ /*
+ * sanity check
+ */
+ if (surf->Base.Width < display->virtualWidth ||
+ surf->Base.Height < display->virtualHeight) {
+ /* this case _should_ have been caught at the top of this function */
+ _eglLog(_EGL_WARNING, "too small of surface in _eglDRIShowScreenSurfaceMESA "
+ "%d x %d < %d x %d",
+ surf->Base.Width,
+ surf->Base.Height,
+ display->virtualWidth,
+ display->virtualHeight);
+ /*
+ return EGL_FALSE;
+ */
+ }
+
+ /* This used to be done in the _eglDRICreateScreens routine. */
+ _eglDRILoadColormap(scrn);
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Called by eglSwapBuffers via the drv->API.SwapBuffers() pointer.
+ *
+ * If the backbuffer is on a videocard, this is extraordinarily slow!
+ */
+static EGLBoolean
+_eglDRISwapBuffers(_EGLDriver *drv, EGLDisplay dpy, EGLSurface draw)
+{
+ driSurface *drawable = Lookup_driSurface(draw);
+
+ /* this does error checking */
+ if (!_eglSwapBuffers(drv, dpy, draw))
+ return EGL_FALSE;
+
+ drawable->drawable.swapBuffers(NULL, drawable->drawable.private);
+
+ return EGL_TRUE;
+}
+
+
+EGLBoolean
+_eglDRIGetDisplayInfo(driDisplay *dpy)
+{
+ char path[ NAME_MAX ];
+ FILE *file;
+ int i, rc;
+ drmSetVersion sv;
+ drm_magic_t magic;
+
+ snprintf( path, sizeof( path ), "%s/graphics/fb%d/device/device", sysfs, dpy->minor );
+ file = fopen( path, "r" );
+ if (!file) {
+ _eglLog(_EGL_WARNING, "Unable to open %s", path);
+ return EGL_FALSE;
+ }
+ fgets( path, sizeof( path ), file );
+ sscanf( path, "%x", &dpy->chipset );
+ fclose( file );
+
+ sprintf(path, DRM_DEV_NAME, DRM_DIR_NAME, dpy->minor);
+ if ( ( dpy->drmFD = open(path, O_RDWR, 0) ) < 0 ) {
+ _eglLog(_EGL_WARNING, "drmOpen failed.");
+ return EGL_FALSE;
+ }
+
+ /* Set the interface version, asking for 1.2 */
+ sv.drm_di_major = 1;
+ sv.drm_di_minor = 2;
+ sv.drm_dd_major = -1;
+ if ((rc = drmSetInterfaceVersion(dpy->drmFD, &sv)))
+ return EGL_FALSE;
+
+ /* self authorize */
+ if (drmGetMagic(dpy->drmFD, &magic))
+ return EGL_FALSE;
+ if (drmAuthMagic(dpy->drmFD, magic))
+ return EGL_FALSE;
+
+ /* Map framebuffer and SAREA */
+ for (i = 0; ; i++) {
+ drm_handle_t handle, offset;
+ drmSize size;
+ drmMapType type;
+ drmMapFlags flags;
+ int mtrr;
+
+ if (drmGetMap(dpy->drmFD, i, &offset, &size, &type, &flags,
+ &handle, &mtrr))
+ break;
+
+ if (type == DRM_FRAME_BUFFER) {
+ rc = drmMap( dpy->drmFD, offset, size, (drmAddressPtr) &dpy->pFB);
+ if (rc < 0) {
+ _eglLog(_EGL_WARNING, "drmMap DRM_FAME_BUFFER failed");
+ return EGL_FALSE;
+ }
+ dpy->fbSize = size;
+ _eglLog(_EGL_INFO, "Found framebuffer size: %d", dpy->fbSize);
+ }
+ else if (type == DRM_SHM) {
+ rc = drmMap(dpy->drmFD, offset, size, (drmAddressPtr) &dpy->pSAREA);
+ if (rc < 0 ) {
+ _eglLog(_EGL_WARNING, "drmMap DRM_SHM failed.");
+ return EGL_FALSE;
+ }
+ dpy->SAREASize = SAREA_MAX;
+ _eglLog(_EGL_DEBUG, "mapped SAREA 0x%08lx to %p, size %d",
+ (unsigned long) offset, dpy->pSAREA, dpy->SAREASize );
+ }
+ }
+
+ if (!dpy->pFB) {
+ _eglLog(_EGL_WARNING, "failed to map framebuffer");
+ return EGL_FALSE;
+ }
+
+ if (!dpy->pSAREA) {
+ /* if this happens, make sure you're using the most recent DRM modules */
+ _eglLog(_EGL_WARNING, "failed to map SAREA");
+ return EGL_FALSE;
+ }
+
+ memset( dpy->pSAREA, 0, dpy->SAREASize );
+
+ return EGL_TRUE;
+}
+
+
+ /* Return the DRI per screen structure */
+static __DRIscreen *
+__eglFindDRIScreen(__DRInativeDisplay *ndpy, int scrn)
+{
+ driDisplay *disp = (driDisplay *)ndpy;
+ return &disp->driScreen;
+}
+
+
+static GLboolean
+__eglCreateContextWithConfig(__DRInativeDisplay* ndpy, int screen,
+ int configID, void* context,
+ drm_context_t * hHWContext)
+{
+ __DRIscreen *pDRIScreen;
+ __DRIscreen *psp;
+
+ pDRIScreen = __eglFindDRIScreen(ndpy, screen);
+ if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
+ return GL_FALSE;
+ }
+ psp = (__DRIscreen *) pDRIScreen->private;
+ if (psp->fd) {
+ if (drmCreateContext(psp->fd, hHWContext)) {
+ _eglLog(_EGL_WARNING, "drmCreateContext failed.");
+ return GL_FALSE;
+ }
+ *(void**)context = (void*) *hHWContext;
+ }
+#if 0
+ __DRIscreen *pDRIScreen;
+ __DRIscreen *psp;
+
+ pDRIScreen = __glXFindDRIScreen(dpy, screen);
+ if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
+ return GL_FALSE;
+ }
+
+ psp = (__DRIscreen *) pDRIScreen->private;
+
+ if (psp->fd) {
+ if (drmCreateContext(psp->fd, hHWContext)) {
+ _eglLog(_EGL_WARNING, "drmCreateContext failed.");
+ return GL_FALSE;
+ }
+ *(void**)contextID = (void*) *hHWContext;
+ }
+#endif
+ return GL_TRUE;
+}
+
+
+static GLboolean
+__eglDestroyContext( __DRInativeDisplay * ndpy, int screen, __DRIid context )
+{
+ __DRIscreen *pDRIScreen;
+ __DRIscreen *psp;
+
+ pDRIScreen = __eglFindDRIScreen(ndpy, screen);
+ if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
+ return GL_FALSE;
+ }
+ psp = (__DRIscreen *) pDRIScreen->private;
+ if (psp->fd)
+ drmDestroyContext(psp->fd, context);
+
+ return GL_TRUE;
+}
+
+
+static GLboolean
+__eglCreateDrawable(__DRInativeDisplay * ndpy, int screen,
+ __DRIid drawable, drm_drawable_t * hHWDrawable)
+{
+ __DRIscreen *pDRIScreen;
+ __DRIscreen *psp;
+
+ pDRIScreen = __eglFindDRIScreen(ndpy, screen);
+ if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
+ return GL_FALSE;
+ }
+ psp = (__DRIscreen *) pDRIScreen->private;
+ if (psp->fd) {
+ if (drmCreateDrawable(psp->fd, hHWDrawable)) {
+ _eglLog(_EGL_WARNING, "drmCreateDrawable failed.");
+ return GL_FALSE;
+ }
+ }
+ return GL_TRUE;
+}
+
+
+static GLboolean
+__eglDestroyDrawable( __DRInativeDisplay * ndpy, int screen, __DRIid drawable )
+{
+ __DRIscreen *pDRIScreen;
+ __DRIscreen *psp;
+
+ pDRIScreen = __eglFindDRIScreen(ndpy, screen);
+ if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
+ return GL_FALSE;
+ }
+ psp = (__DRIscreen *) pDRIScreen->private;
+ if (psp->fd)
+ drmDestroyDrawable(psp->fd, drawable);
+
+ return GL_TRUE;
+}
+
+static GLboolean
+__eglGetDrawableInfo(__DRInativeDisplay * ndpy, int screen, __DRIid drawable,
+ unsigned int* index, unsigned int* stamp,
+ int* X, int* Y, int* W, int* H,
+ int* numClipRects, drm_clip_rect_t ** pClipRects,
+ int* backX, int* backY,
+ int* numBackClipRects, drm_clip_rect_t ** pBackClipRects )
+{
+ __DRIscreen *pDRIScreen;
+ __DRIscreen *psp;
+ driSurface *surf = Lookup_driSurface((EGLSurface) drawable);
+
+ pDRIScreen = __eglFindDRIScreen(ndpy, screen);
+
+ if ( (pDRIScreen == NULL) || (pDRIScreen->private == NULL) ) {
+ return GL_FALSE;
+ }
+ psp = (__DRIscreen *) pDRIScreen->private;
+ *X = 0;
+ *Y = 0;
+ *W = surf->Base.Width;
+ *H = surf->Base.Height;
+
+ *backX = 0;
+ *backY = 0;
+ *numBackClipRects = 0;
+ *pBackClipRects = NULL;
+
+ *numClipRects = 1;
+ *pClipRects = malloc(sizeof(**pClipRects));
+ **pClipRects = (drm_clip_rect_t){0, 0, surf->Base.Width, surf->Base.Height};
+
+ psp->pSAREA->drawableTable[0].stamp = 1;
+ *stamp = 1;
+#if 0
+ GLXDrawable drawable = (GLXDrawable) draw;
+ drm_clip_rect_t * cliprect;
+ Display* display = (Display*)dpy;
+ __DRIcontext *pcp = (__DRIcontext *)CurrentContext->driContext.private;
+ if (drawable == 0) {
+ return GL_FALSE;
+ }
+
+ cliprect = (drm_clip_rect_t*) malloc(sizeof(drm_clip_rect_t));
+ cliprect->x1 = drawable->x;
+ cliprect->y1 = drawable->y;
+ cliprect->x2 = drawable->x + drawable->w;
+ cliprect->y2 = drawable->y + drawable->h;
+
+ /* the drawable index is by client id */
+ *index = display->clientID;
+
+ *stamp = pcp->driScreenPriv->pSAREA->drawableTable[display->clientID].stamp;
+ *x = drawable->x;
+ *y = drawable->y;
+ *width = drawable->w;
+ *height = drawable->h;
+ *numClipRects = 1;
+ *pClipRects = cliprect;
+
+ *backX = drawable->x;
+ *backY = drawable->y;
+ *numBackClipRects = 0;
+ *pBackClipRects = 0;
+#endif
+ return GL_TRUE;
+}
+
+
+/**
+ * Implement \c __DRIinterfaceMethods::getProcAddress.
+ */
+static __DRIfuncPtr
+get_proc_address(const char * proc_name)
+{
+ return NULL;
+}
+
+
+/**
+ * Destroy a linked list of \c __GLcontextModes structures created by
+ * \c _gl_context_modes_create.
+ *
+ * \param modes Linked list of structures to be destroyed. All structres
+ * in the list will be freed.
+ */
+static void
+__egl_context_modes_destroy(__GLcontextModes *modes)
+{
+ while ( modes != NULL ) {
+ __GLcontextModes * const next = modes->next;
+
+ free( modes );
+ modes = next;
+ }
+}
+
+
+/**
+ * Allocate a linked list of \c __GLcontextModes structures. The fields of
+ * each structure will be initialized to "reasonable" default values. In
+ * most cases this is the default value defined by table 3.4 of the GLX
+ * 1.3 specification. This means that most values are either initialized to
+ * zero or \c GLX_DONT_CARE (which is -1). As support for additional
+ * extensions is added, the new values will be initialized to appropriate
+ * values from the extension specification.
+ *
+ * \param count Number of structures to allocate.
+ * \param minimum_size Minimum size of a structure to allocate. This allows
+ * for differences in the version of the
+ * \c __GLcontextModes stucture used in libGL and in a
+ * DRI-based driver.
+ * \returns A pointer to the first element in a linked list of \c count
+ * stuctures on success, or \c NULL on failure.
+ *
+ * \warning Use of \c minimum_size does \b not guarantee binary compatibility.
+ * The fundamental assumption is that if the \c minimum_size
+ * specified by the driver and the size of the \c __GLcontextModes
+ * structure in libGL is the same, then the meaning of each byte in
+ * the structure is the same in both places. \b Be \b careful!
+ * Basically this means that fields have to be added in libGL and
+ * then propagated to drivers. Drivers should \b never arbitrarilly
+ * extend the \c __GLcontextModes data-structure.
+ */
+static __GLcontextModes *
+__egl_context_modes_create(unsigned count, size_t minimum_size)
+{
+ const size_t size = (minimum_size > sizeof( __GLcontextModes ))
+ ? minimum_size : sizeof( __GLcontextModes );
+ __GLcontextModes * base = NULL;
+ __GLcontextModes ** next;
+ unsigned i;
+
+ next = & base;
+ for ( i = 0 ; i < count ; i++ ) {
+ *next = (__GLcontextModes *) malloc( size );
+ if ( *next == NULL ) {
+ __egl_context_modes_destroy( base );
+ base = NULL;
+ break;
+ }
+
+ (void) memset( *next, 0, size );
+ (*next)->visualID = GLX_DONT_CARE;
+ (*next)->visualType = GLX_DONT_CARE;
+ (*next)->visualRating = GLX_NONE;
+ (*next)->transparentPixel = GLX_NONE;
+ (*next)->transparentRed = GLX_DONT_CARE;
+ (*next)->transparentGreen = GLX_DONT_CARE;
+ (*next)->transparentBlue = GLX_DONT_CARE;
+ (*next)->transparentAlpha = GLX_DONT_CARE;
+ (*next)->transparentIndex = GLX_DONT_CARE;
+ (*next)->xRenderable = GLX_DONT_CARE;
+ (*next)->fbconfigID = GLX_DONT_CARE;
+ (*next)->swapMethod = GLX_SWAP_UNDEFINED_OML;
+
+ next = & ((*next)->next);
+ }
+
+ return base;
+}
+
+
+static GLboolean
+__eglWindowExists(__DRInativeDisplay *dpy, __DRIid draw)
+{
+ return EGL_TRUE;
+}
+
+
+/**
+ * Get the unadjusted system time (UST). Currently, the UST is measured in
+ * microseconds since Epoc. The actual resolution of the UST may vary from
+ * system to system, and the units may vary from release to release.
+ * Drivers should not call this function directly. They should instead use
+ * \c glXGetProcAddress to obtain a pointer to the function.
+ *
+ * \param ust Location to store the 64-bit UST
+ * \returns Zero on success or a negative errno value on failure.
+ *
+ * \sa glXGetProcAddress, PFNGLXGETUSTPROC
+ *
+ * \since Internal API version 20030317.
+ */
+static int
+__eglGetUST(int64_t *ust)
+{
+ struct timeval tv;
+
+ if ( ust == NULL ) {
+ return -EFAULT;
+ }
+
+ if ( gettimeofday( & tv, NULL ) == 0 ) {
+ ust[0] = (tv.tv_sec * 1000000) + tv.tv_usec;
+ return 0;
+ }
+ else {
+ return -errno;
+ }
+}
+
+/**
+ * Determine the refresh rate of the specified drawable and display.
+ *
+ * \param dpy Display whose refresh rate is to be determined.
+ * \param drawable Drawable whose refresh rate is to be determined.
+ * \param numerator Numerator of the refresh rate.
+ * \param demoninator Denominator of the refresh rate.
+ * \return If the refresh rate for the specified display and drawable could
+ * be calculated, True is returned. Otherwise False is returned.
+ *
+ * \note This function is implemented entirely client-side. A lot of other
+ * functionality is required to export GLX_OML_sync_control, so on
+ * XFree86 this function can be called for direct-rendering contexts
+ * when GLX_OML_sync_control appears in the client extension string.
+ */
+static GLboolean
+__eglGetMSCRate(__DRInativeDisplay * dpy, __DRIid drawable,
+ int32_t * numerator, int32_t * denominator)
+{
+ return EGL_TRUE;
+}
+
+
+/**
+ * Table of functions exported by the loader to the driver.
+ */
+static const __DRIinterfaceMethods interface_methods = {
+ get_proc_address,
+
+ __egl_context_modes_create,
+ __egl_context_modes_destroy,
+
+ __eglFindDRIScreen,
+ __eglWindowExists,
+
+ __eglCreateContextWithConfig,
+ __eglDestroyContext,
+
+ __eglCreateDrawable,
+ __eglDestroyDrawable,
+ __eglGetDrawableInfo,
+
+ __eglGetUST,
+ __eglGetMSCRate,
+};
+
+
+static int
+__glXGetInternalVersion(void)
+{
+ return 20050725;
+}
+
+static const char createNewScreenName[] = "__driCreateNewScreen_20050727";
+
+
+/**
+ * Do per-display initialization.
+ */
+EGLBoolean
+_eglDRICreateDisplay(driDisplay *dpy, __DRIframebuffer *framebuffer)
+{
+ PFNCREATENEWSCREENFUNC createNewScreen;
+ int api_ver = __glXGetInternalVersion();
+ __DRIversion ddx_version;
+ __DRIversion dri_version;
+ __DRIversion drm_version;
+ drmVersionPtr version;
+
+ version = drmGetVersion( dpy->drmFD );
+ if ( version ) {
+ drm_version.major = version->version_major;
+ drm_version.minor = version->version_minor;
+ drm_version.patch = version->version_patchlevel;
+ drmFreeVersion( version );
+ }
+ else {
+ drm_version.major = -1;
+ drm_version.minor = -1;
+ drm_version.patch = -1;
+ }
+
+ /*
+ * Get device name (like "tdfx") and the ddx version numbers.
+ * We'll check the version in each DRI driver's "createScreen"
+ * function.
+ */
+ ddx_version.major = 4;
+ ddx_version.minor = 0;
+ ddx_version.patch = 0;
+
+ /*
+ * Get the DRI X extension version.
+ */
+ dri_version.major = 4;
+ dri_version.minor = 0;
+ dri_version.patch = 0;
+
+ createNewScreen = ( PFNCREATENEWSCREENFUNC ) dlsym( dpy->Base.Driver->LibHandle, createNewScreenName );
+ if ( !createNewScreen ) {
+ _eglLog(_EGL_WARNING, "Couldn't find %s function in the driver.",
+ createNewScreenName );
+ return EGL_FALSE;
+ }
+
+ dpy->driScreen.private = createNewScreen( dpy, 0, &dpy->driScreen, NULL,
+ &ddx_version, &dri_version,
+ &drm_version, framebuffer,
+ dpy->pSAREA, dpy->drmFD,
+ api_ver,
+ & interface_methods,
+ NULL);
+ if (!dpy->driScreen.private) {
+ _eglLog(_EGL_WARNING, "egldri.c: DRI create new screen failed");
+ return EGL_FALSE;
+ }
+
+ DRM_UNLOCK( dpy->drmFD, dpy->pSAREA, dpy->serverContext );
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Create all the EGL screens for the given display.
+ */
+EGLBoolean
+_eglDRICreateScreens(driDisplay *dpy)
+{
+ const int numScreens = 1; /* XXX fix this someday */
+ int i;
+
+ for (i = 0; i < numScreens; i++) {
+ char path[ NAME_MAX ];
+ FILE *file;
+ driScreen *s;
+
+ /* Create a screen */
+ if ( !( s = ( driScreen * ) calloc( 1, sizeof( *s ) ) ) )
+ return EGL_FALSE;
+
+ snprintf( s->fb, NAME_MAX, "fb%d", dpy->minor );
+ _eglInitScreen( &s->Base );
+
+ _eglAddScreen( &dpy->Base, &s->Base );
+
+ /* Create the screen's mode list */
+ snprintf( path, sizeof( path ), "%s/graphics/%s/modes", sysfs, s->fb );
+ file = fopen( path, "r" );
+ while ( fgets( path, sizeof( path ), file ) ) {
+ unsigned int x, y, r;
+ char c;
+ path[ strlen( path ) - 1 ] = '\0'; /* strip off \n from sysfs */
+ sscanf( path, "%c:%ux%u-%u", &c, &x, &y, &r );
+ _eglAddNewMode( &s->Base, x, y, r * 1000, path );
+ }
+ fclose( file );
+
+ /*
+ * NOTE: we used to set the colormap here, but that didn't work reliably.
+ * Some entries near the start of the table would get corrupted by later
+ * mode changes.
+ */
+ }
+
+ return EGL_TRUE;
+}
+
+
+EGLBoolean
+_eglDRIInitialize(_EGLDriver *drv, EGLDisplay dpy,
+ EGLint *major, EGLint *minor)
+{
+ _EGLDisplay *disp = _eglLookupDisplay(dpy);
+ driDisplay *display;
+ const char *driverName = (const char *) disp->NativeDisplay;
+
+ assert(disp);
+
+ /* Create new driDisplay object to replace the _EGLDisplay that was
+ * previously created.
+ */
+ display = calloc(1, sizeof(*display));
+ display->Base = *disp;
+ _eglSaveDisplay(&display->Base);
+ free(disp);
+
+ *major = 1;
+ *minor = 0;
+
+ sscanf(driverName + 1, "%d", &display->minor);
+
+ drv->Initialized = EGL_TRUE;
+ return EGL_TRUE;
+}
+
+
+static EGLBoolean
+_eglDRITerminate(_EGLDriver *drv, EGLDisplay dpy)
+{
+ driDisplay *display = Lookup_driDisplay(dpy);
+ _eglCleanupDisplay(&display->Base);/*rename that function*/
+ free(display);
+ free(drv);
+ return EGL_TRUE;
+}
+
+
+/**
+ * Plug in the DRI-specific functions into the driver's dispatch table.
+ * Also, enable some EGL extensions.
+ */
+void
+_eglDRIInitDriverFallbacks(_EGLDriver *drv)
+{
+ _eglInitDriverFallbacks(drv);
+
+ drv->API.Initialize = _eglDRIInitialize;
+ drv->API.Terminate = _eglDRITerminate;
+ drv->API.CreateContext = _eglDRICreateContext;
+ drv->API.MakeCurrent = _eglDRIMakeCurrent;
+ drv->API.CreatePbufferSurface = _eglDRICreatePbufferSurface;
+ drv->API.DestroySurface = _eglDRIDestroySurface;
+ drv->API.DestroyContext = _eglDRIDestroyContext;
+ drv->API.CreateScreenSurfaceMESA = _eglDRICreateScreenSurfaceMESA;
+ drv->API.ShowScreenSurfaceMESA = _eglDRIShowScreenSurfaceMESA;
+ drv->API.SwapBuffers = _eglDRISwapBuffers;
+
+ /* enable supported extensions */
+ drv->Extensions.MESA_screen_surface = EGL_TRUE;
+ drv->Extensions.MESA_copy_context = EGL_TRUE;
+}
diff --git a/src/egl/drivers/dri/egldri.h b/src/egl/drivers/dri/egldri.h
new file mode 100644
index 0000000000..54a9a4ea26
--- /dev/null
+++ b/src/egl/drivers/dri/egldri.h
@@ -0,0 +1,116 @@
+#ifndef EGLDRI_INCLUDED
+#define EGLDRI_INCLUDED
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+#include "egldisplay.h"
+#include "eglscreen.h"
+#include "eglsurface.h"
+#include "eglcontext.h"
+
+#include "dri_util.h"
+#include "drm_sarea.h"
+
+/**
+ * dri display-specific driver class derived from _EGLDisplay
+ */
+typedef struct dri_display
+{
+ _EGLDisplay Base; /**< base class */
+ void *pFB;
+ int drmFD; /**< \brief DRM device file descriptor */
+ int minor;
+ unsigned long hFrameBuffer;
+
+ int virtualWidth;
+ int virtualHeight;
+ int fbSize;
+ int bpp;
+ int cpp;
+ int card_type;
+ int SAREASize;
+ drm_sarea_t *pSAREA;
+ unsigned int serverContext; /**< \brief DRM context only active on server */
+ unsigned long FBStart; /**< \brief physical address of the framebuffer */
+ void *driverClientMsg;
+ int driverClientMsgSize;
+ unsigned chipset;
+ void *driverPrivate;
+ drm_magic_t magic;
+
+ __DRIscreen driScreen;
+
+} driDisplay;
+
+
+/**
+ * dri driver-specific screen class derived from _EGLScreen
+ */
+typedef struct dri_screen
+{
+ _EGLScreen Base;
+ char fb[NAME_MAX]; /** the screen name, like "fb0" */
+} driScreen;
+
+
+/**
+ * dri driver-specific surface class derived from _EGLSurface
+ */
+typedef struct dri_surface
+{
+ _EGLSurface Base; /* base class/object */
+ __DRIdrawable drawable;
+} driSurface;
+
+
+/**
+ * dri driver-specific context class derived from _EGLContext
+ */
+typedef struct dri_context
+{
+ _EGLContext Base; /* base class/object */
+ __DRIcontext driContext; /**< \brief context dependent methods */
+} driContext;
+
+
+
+static inline driDisplay *
+Lookup_driDisplay(EGLDisplay dpy)
+{
+ _EGLDisplay *d = _eglLookupDisplay(dpy);
+ return (driDisplay *) d;
+}
+
+
+static inline driScreen *
+Lookup_driScreen(EGLDisplay dpy, EGLScreenMESA screen)
+{
+ _EGLScreen *s = _eglLookupScreen(dpy, screen);
+ return (driScreen *) s;
+}
+
+
+static inline driContext *
+Lookup_driContext(EGLContext ctx)
+{
+ _EGLContext *c = _eglLookupContext(ctx);
+ return (driContext *) c;
+}
+
+
+static inline driSurface *
+Lookup_driSurface(EGLSurface surf)
+{
+ _EGLSurface *s = _eglLookupSurface(surf);
+ return (driSurface *) s;
+}
+
+extern void _eglDRIInitDriverFallbacks(_EGLDriver *drv);
+extern EGLBoolean _eglDRIShowScreenSurfaceMESA(_EGLDriver *drv, EGLDisplay dpy, EGLScreenMESA screen, EGLSurface surface, EGLModeMESA m);
+extern EGLBoolean _eglDRIInitialize(_EGLDriver *drv, EGLDisplay dpy, EGLint *major, EGLint *minor);
+extern EGLBoolean _eglDRIGetDisplayInfo(driDisplay *dpy);
+extern EGLBoolean _eglDRICreateDisplay(driDisplay *dpy, __DRIframebuffer *framebuffer);
+extern EGLBoolean _eglDRICreateScreens(driDisplay *dpy);
+
+#endif /* EGLDRI_INCLUDED */
diff --git a/src/egl/drivers/dri2/Makefile b/src/egl/drivers/dri2/Makefile
new file mode 100644
index 0000000000..4e760aec4c
--- /dev/null
+++ b/src/egl/drivers/dri2/Makefile
@@ -0,0 +1,18 @@
+# src/egl/drivers/dri2/Makefile
+
+TOP = ../../../..
+include $(TOP)/configs/current
+
+EGL_DRIVER = egl_dri2.so
+EGL_SOURCES = egl_dri2.c
+
+EGL_INCLUDES = \
+ -I$(TOP)/include \
+ -I$(TOP)/src/egl/main \
+ -I$(TOP)/src/mapi \
+ -DDEFAULT_DRIVER_DIR=\"$(DRI_DRIVER_SEARCH_DIR)\" \
+ $(EGL_DRI2_CFLAGS)
+
+EGL_LIBS = $(EGL_DRI2_LIBS)
+
+include ../Makefile.template
diff --git a/src/egl/drivers/dri2/egl_dri2.c b/src/egl/drivers/dri2/egl_dri2.c
new file mode 100644
index 0000000000..aa384cb117
--- /dev/null
+++ b/src/egl/drivers/dri2/egl_dri2.c
@@ -0,0 +1,1474 @@
+/*
+ * Copyright © 2010 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Authors:
+ * Kristian Høgsberg <krh@bitplanet.net>
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <limits.h>
+#include <dlfcn.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
+#include <xf86drm.h>
+#include <GL/gl.h>
+#include <GL/internal/dri_interface.h>
+#include <xcb/xcb.h>
+#include <xcb/dri2.h>
+#include <xcb/xfixes.h>
+#include <X11/Xlib-xcb.h>
+
+#include <glapi/glapi.h>
+#include "eglconfig.h"
+#include "eglcontext.h"
+#include "egldisplay.h"
+#include "egldriver.h"
+#include "eglcurrent.h"
+#include "egllog.h"
+#include "eglsurface.h"
+#include "eglimage.h"
+
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
+
+struct dri2_egl_driver
+{
+ _EGLDriver base;
+
+ void (*glFlush)(void);
+};
+
+struct dri2_egl_display
+{
+ xcb_connection_t *conn;
+ int dri2_major;
+ int dri2_minor;
+ __DRIscreen *dri_screen;
+ const __DRIconfig **driver_configs;
+ void *driver;
+ __DRIcoreExtension *core;
+ __DRIdri2Extension *dri2;
+ __DRI2flushExtension *flush;
+ __DRItexBufferExtension *tex_buffer;
+ __DRIimageExtension *image;
+ int fd;
+
+ char *device_name;
+ char *driver_name;
+
+ __DRIdri2LoaderExtension loader_extension;
+ __DRIimageLookupExtension image_lookup_extension;
+ const __DRIextension *extensions[3];
+};
+
+struct dri2_egl_context
+{
+ _EGLContext base;
+ __DRIcontext *dri_context;
+};
+
+struct dri2_egl_surface
+{
+ _EGLSurface base;
+ __DRIdrawable *dri_drawable;
+ xcb_drawable_t drawable;
+ __DRIbuffer buffers[5];
+ int buffer_count;
+ xcb_xfixes_region_t region;
+ int have_fake_front;
+ int swap_interval;
+};
+
+struct dri2_egl_config
+{
+ _EGLConfig base;
+ const __DRIconfig *dri_config;
+};
+
+struct dri2_egl_image
+{
+ _EGLImage base;
+ __DRIimage *dri_image;
+};
+
+/* standard typecasts */
+_EGL_DRIVER_STANDARD_TYPECASTS(dri2_egl)
+_EGL_DRIVER_TYPECAST(dri2_egl_image, _EGLImage, obj)
+
+EGLint dri2_to_egl_attribute_map[] = {
+ 0,
+ EGL_BUFFER_SIZE, /* __DRI_ATTRIB_BUFFER_SIZE */
+ EGL_LEVEL, /* __DRI_ATTRIB_LEVEL */
+ EGL_RED_SIZE, /* __DRI_ATTRIB_RED_SIZE */
+ EGL_GREEN_SIZE, /* __DRI_ATTRIB_GREEN_SIZE */
+ EGL_BLUE_SIZE, /* __DRI_ATTRIB_BLUE_SIZE */
+ EGL_LUMINANCE_SIZE, /* __DRI_ATTRIB_LUMINANCE_SIZE */
+ EGL_ALPHA_SIZE, /* __DRI_ATTRIB_ALPHA_SIZE */
+ 0, /* __DRI_ATTRIB_ALPHA_MASK_SIZE */
+ EGL_DEPTH_SIZE, /* __DRI_ATTRIB_DEPTH_SIZE */
+ EGL_STENCIL_SIZE, /* __DRI_ATTRIB_STENCIL_SIZE */
+ 0, /* __DRI_ATTRIB_ACCUM_RED_SIZE */
+ 0, /* __DRI_ATTRIB_ACCUM_GREEN_SIZE */
+ 0, /* __DRI_ATTRIB_ACCUM_BLUE_SIZE */
+ 0, /* __DRI_ATTRIB_ACCUM_ALPHA_SIZE */
+ EGL_SAMPLE_BUFFERS, /* __DRI_ATTRIB_SAMPLE_BUFFERS */
+ EGL_SAMPLES, /* __DRI_ATTRIB_SAMPLES */
+ 0, /* __DRI_ATTRIB_RENDER_TYPE, */
+ 0, /* __DRI_ATTRIB_CONFIG_CAVEAT */
+ 0, /* __DRI_ATTRIB_CONFORMANT */
+ 0, /* __DRI_ATTRIB_DOUBLE_BUFFER */
+ 0, /* __DRI_ATTRIB_STEREO */
+ 0, /* __DRI_ATTRIB_AUX_BUFFERS */
+ 0, /* __DRI_ATTRIB_TRANSPARENT_TYPE */
+ 0, /* __DRI_ATTRIB_TRANSPARENT_INDEX_VALUE */
+ 0, /* __DRI_ATTRIB_TRANSPARENT_RED_VALUE */
+ 0, /* __DRI_ATTRIB_TRANSPARENT_GREEN_VALUE */
+ 0, /* __DRI_ATTRIB_TRANSPARENT_BLUE_VALUE */
+ 0, /* __DRI_ATTRIB_TRANSPARENT_ALPHA_VALUE */
+ 0, /* __DRI_ATTRIB_FLOAT_MODE */
+ 0, /* __DRI_ATTRIB_RED_MASK */
+ 0, /* __DRI_ATTRIB_GREEN_MASK */
+ 0, /* __DRI_ATTRIB_BLUE_MASK */
+ 0, /* __DRI_ATTRIB_ALPHA_MASK */
+ EGL_MAX_PBUFFER_WIDTH, /* __DRI_ATTRIB_MAX_PBUFFER_WIDTH */
+ EGL_MAX_PBUFFER_HEIGHT, /* __DRI_ATTRIB_MAX_PBUFFER_HEIGHT */
+ EGL_MAX_PBUFFER_PIXELS, /* __DRI_ATTRIB_MAX_PBUFFER_PIXELS */
+ 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_WIDTH */
+ 0, /* __DRI_ATTRIB_OPTIMAL_PBUFFER_HEIGHT */
+ 0, /* __DRI_ATTRIB_VISUAL_SELECT_GROUP */
+ 0, /* __DRI_ATTRIB_SWAP_METHOD */
+ EGL_MAX_SWAP_INTERVAL, /* __DRI_ATTRIB_MAX_SWAP_INTERVAL */
+ EGL_MIN_SWAP_INTERVAL, /* __DRI_ATTRIB_MIN_SWAP_INTERVAL */
+ 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGB */
+ 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA */
+ 0, /* __DRI_ATTRIB_BIND_TO_MIPMAP_TEXTURE */
+ 0, /* __DRI_ATTRIB_BIND_TO_TEXTURE_TARGETS */
+ EGL_Y_INVERTED_NOK, /* __DRI_ATTRIB_YINVERTED */
+};
+
+static void
+dri2_add_config(_EGLDisplay *disp, const __DRIconfig *dri_config, int id,
+ int depth, xcb_visualtype_t *visual)
+{
+ struct dri2_egl_config *conf;
+ struct dri2_egl_display *dri2_dpy;
+ _EGLConfig base;
+ unsigned int attrib, value, double_buffer;
+ EGLint key, bind_to_texture_rgb, bind_to_texture_rgba;
+ int i;
+
+ dri2_dpy = disp->DriverData;
+ _eglInitConfig(&base, disp, id);
+
+ i = 0;
+ while (dri2_dpy->core->indexConfigAttrib(dri_config, i++, &attrib, &value)) {
+ switch (attrib) {
+ case __DRI_ATTRIB_RENDER_TYPE:
+ if (value & __DRI_ATTRIB_RGBA_BIT)
+ value = EGL_RGB_BUFFER;
+ else if (value & __DRI_ATTRIB_LUMINANCE_BIT)
+ value = EGL_LUMINANCE_BUFFER;
+ else
+ /* not valid */;
+ _eglSetConfigKey(&base, EGL_COLOR_BUFFER_TYPE, value);
+ break;
+
+ case __DRI_ATTRIB_CONFIG_CAVEAT:
+ if (value & __DRI_ATTRIB_NON_CONFORMANT_CONFIG)
+ value = EGL_NON_CONFORMANT_CONFIG;
+ else if (value & __DRI_ATTRIB_SLOW_BIT)
+ value = EGL_SLOW_CONFIG;
+ else
+ value = EGL_NONE;
+ _eglSetConfigKey(&base, EGL_CONFIG_CAVEAT, value);
+ break;
+
+ case __DRI_ATTRIB_BIND_TO_TEXTURE_RGB:
+ bind_to_texture_rgb = value;
+ break;
+
+ case __DRI_ATTRIB_BIND_TO_TEXTURE_RGBA:
+ bind_to_texture_rgba = value;
+ break;
+
+ case __DRI_ATTRIB_DOUBLE_BUFFER:
+ double_buffer = value;
+ break;
+
+ default:
+ key = dri2_to_egl_attribute_map[attrib];
+ if (key != 0)
+ _eglSetConfigKey(&base, key, value);
+ break;
+ }
+ }
+
+ /* In EGL, double buffer or not isn't a config attribute. Pixmaps
+ * surfaces are always single buffered, pbuffer surfaces are always
+ * back buffers and windows can be either, selected by passing an
+ * attribute at window surface construction time. To support this
+ * we ignore all double buffer configs and manipulate the buffer we
+ * return in the getBuffer callback to get the behaviour we want. */
+
+ if (double_buffer)
+ return;
+
+ if (visual != NULL) {
+ if (depth != _eglGetConfigKey(&base, EGL_BUFFER_SIZE))
+ return;
+
+ _eglSetConfigKey(&base, EGL_SURFACE_TYPE,
+ EGL_WINDOW_BIT | EGL_PIXMAP_BIT | EGL_PBUFFER_BIT |
+ EGL_SWAP_BEHAVIOR_PRESERVED_BIT);
+
+ _eglSetConfigKey(&base, EGL_NATIVE_VISUAL_ID, visual->visual_id);
+ _eglSetConfigKey(&base, EGL_NATIVE_VISUAL_TYPE, visual->_class);
+ } else {
+ _eglSetConfigKey(&base, EGL_SURFACE_TYPE,
+ EGL_PIXMAP_BIT | EGL_PBUFFER_BIT);
+ }
+
+ _eglSetConfigKey(&base, EGL_NATIVE_RENDERABLE, EGL_TRUE);
+ _eglSetConfigKey(&base, EGL_BIND_TO_TEXTURE_RGB, bind_to_texture_rgb);
+ if (_eglGetConfigKey(&base, EGL_ALPHA_SIZE) > 0)
+ _eglSetConfigKey(&base,
+ EGL_BIND_TO_TEXTURE_RGBA, bind_to_texture_rgba);
+
+ _eglSetConfigKey(&base, EGL_RENDERABLE_TYPE, disp->ClientAPIsMask);
+ _eglSetConfigKey(&base, EGL_CONFORMANT, disp->ClientAPIsMask);
+
+ if (!_eglValidateConfig(&base, EGL_FALSE)) {
+ _eglLog(_EGL_DEBUG, "DRI2: failed to validate config %d", id);
+ return;
+ }
+
+ conf = malloc(sizeof *conf);
+ if (conf != NULL) {
+ memcpy(&conf->base, &base, sizeof base);
+ conf->dri_config = dri_config;
+ _eglAddConfig(disp, &conf->base);
+ }
+}
+
+/**
+ * Process list of buffer received from the server
+ *
+ * Processes the list of buffers received in a reply from the server to either
+ * \c DRI2GetBuffers or \c DRI2GetBuffersWithFormat.
+ */
+static void
+dri2_process_buffers(struct dri2_egl_surface *dri2_surf,
+ xcb_dri2_dri2_buffer_t *buffers, unsigned count)
+{
+ struct dri2_egl_display *dri2_dpy =
+ dri2_egl_display(dri2_surf->base.Resource.Display);
+ xcb_rectangle_t rectangle;
+ int i;
+
+ dri2_surf->buffer_count = count;
+ dri2_surf->have_fake_front = 0;
+
+ /* This assumes the DRI2 buffer attachment tokens matches the
+ * __DRIbuffer tokens. */
+ for (i = 0; i < count; i++) {
+ dri2_surf->buffers[i].attachment = buffers[i].attachment;
+ dri2_surf->buffers[i].name = buffers[i].name;
+ dri2_surf->buffers[i].pitch = buffers[i].pitch;
+ dri2_surf->buffers[i].cpp = buffers[i].cpp;
+ dri2_surf->buffers[i].flags = buffers[i].flags;
+
+ /* We only use the DRI drivers single buffer configs. This
+ * means that if we try to render to a window, DRI2 will give us
+ * the fake front buffer, which we'll use as a back buffer.
+ * Note that EGL doesn't require that several clients rendering
+ * to the same window must see the same aux buffers. */
+ if (dri2_surf->buffers[i].attachment == __DRI_BUFFER_FAKE_FRONT_LEFT)
+ dri2_surf->have_fake_front = 1;
+ }
+
+ if (dri2_surf->region != XCB_NONE)
+ xcb_xfixes_destroy_region(dri2_dpy->conn, dri2_surf->region);
+
+ rectangle.x = 0;
+ rectangle.y = 0;
+ rectangle.width = dri2_surf->base.Width;
+ rectangle.height = dri2_surf->base.Height;
+ dri2_surf->region = xcb_generate_id(dri2_dpy->conn);
+ xcb_xfixes_create_region(dri2_dpy->conn, dri2_surf->region, 1, &rectangle);
+}
+
+static __DRIbuffer *
+dri2_get_buffers(__DRIdrawable * driDrawable,
+ int *width, int *height,
+ unsigned int *attachments, int count,
+ int *out_count, void *loaderPrivate)
+{
+ struct dri2_egl_surface *dri2_surf = loaderPrivate;
+ struct dri2_egl_display *dri2_dpy =
+ dri2_egl_display(dri2_surf->base.Resource.Display);
+ xcb_dri2_dri2_buffer_t *buffers;
+ xcb_dri2_get_buffers_reply_t *reply;
+ xcb_dri2_get_buffers_cookie_t cookie;
+
+ cookie = xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
+ dri2_surf->drawable,
+ count, count, attachments);
+ reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn, cookie, NULL);
+ buffers = xcb_dri2_get_buffers_buffers (reply);
+ if (buffers == NULL)
+ return NULL;
+
+ *out_count = reply->count;
+ dri2_surf->base.Width = *width = reply->width;
+ dri2_surf->base.Height = *height = reply->height;
+ dri2_process_buffers(dri2_surf, buffers, *out_count);
+
+ free(reply);
+
+ return dri2_surf->buffers;
+}
+
+static void
+dri2_flush_front_buffer(__DRIdrawable * driDrawable, void *loaderPrivate)
+{
+ /* FIXME: Does EGL support front buffer rendering at all? */
+
+#if 0
+ struct dri2_egl_surface *dri2_surf = loaderPrivate;
+
+ dri2WaitGL(dri2_surf);
+#endif
+}
+
+static __DRIimage *
+dri2_lookup_egl_image(__DRIcontext *context, void *image, void *data)
+{
+ struct dri2_egl_context *dri2_ctx = data;
+ _EGLDisplay *disp = dri2_ctx->base.Resource.Display;
+ struct dri2_egl_image *dri2_img;
+ _EGLImage *img;
+
+ img = _eglLookupImage(image, disp);
+ if (img == NULL) {
+ _eglError(EGL_BAD_PARAMETER, "dri2_lookup_egl_image");
+ return NULL;
+ }
+
+ dri2_img = dri2_egl_image(image);
+
+ return dri2_img->dri_image;
+}
+
+static __DRIbuffer *
+dri2_get_buffers_with_format(__DRIdrawable * driDrawable,
+ int *width, int *height,
+ unsigned int *attachments, int count,
+ int *out_count, void *loaderPrivate)
+{
+ struct dri2_egl_surface *dri2_surf = loaderPrivate;
+ struct dri2_egl_display *dri2_dpy =
+ dri2_egl_display(dri2_surf->base.Resource.Display);
+ xcb_dri2_dri2_buffer_t *buffers;
+ xcb_dri2_get_buffers_with_format_reply_t *reply;
+ xcb_dri2_get_buffers_with_format_cookie_t cookie;
+ xcb_dri2_attach_format_t *format_attachments;
+
+ format_attachments = (xcb_dri2_attach_format_t *) attachments;
+ cookie = xcb_dri2_get_buffers_with_format_unchecked (dri2_dpy->conn,
+ dri2_surf->drawable,
+ count, count,
+ format_attachments);
+
+ reply = xcb_dri2_get_buffers_with_format_reply (dri2_dpy->conn,
+ cookie, NULL);
+ if (reply == NULL)
+ return NULL;
+
+ buffers = xcb_dri2_get_buffers_with_format_buffers (reply);
+ dri2_surf->base.Width = *width = reply->width;
+ dri2_surf->base.Height = *height = reply->height;
+ *out_count = reply->count;
+ dri2_process_buffers(dri2_surf, buffers, *out_count);
+
+ free(reply);
+
+ return dri2_surf->buffers;
+}
+
+static const char dri_driver_path[] = DEFAULT_DRIVER_DIR;
+
+struct dri2_extension_match {
+ const char *name;
+ int version;
+ int offset;
+};
+
+static struct dri2_extension_match dri2_driver_extensions[] = {
+ { __DRI_CORE, 1, offsetof(struct dri2_egl_display, core) },
+ { __DRI_DRI2, 1, offsetof(struct dri2_egl_display, dri2) },
+ { NULL }
+};
+
+static struct dri2_extension_match dri2_core_extensions[] = {
+ { __DRI2_FLUSH, 1, offsetof(struct dri2_egl_display, flush) },
+ { __DRI_TEX_BUFFER, 2, offsetof(struct dri2_egl_display, tex_buffer) },
+ { __DRI_IMAGE, 1, offsetof(struct dri2_egl_display, image) },
+ { NULL }
+};
+
+static EGLBoolean
+dri2_bind_extensions(struct dri2_egl_display *dri2_dpy,
+ struct dri2_extension_match *matches,
+ const __DRIextension **extensions)
+{
+ int i, j, ret = EGL_TRUE;
+ void *field;
+
+ for (i = 0; extensions[i]; i++) {
+ _eglLog(_EGL_DEBUG, "DRI2: found extension `%s'", extensions[i]->name);
+ for (j = 0; matches[j].name; j++) {
+ if (strcmp(extensions[i]->name, matches[j].name) == 0 &&
+ extensions[i]->version >= matches[j].version) {
+ field = ((char *) dri2_dpy + matches[j].offset);
+ *(const __DRIextension **) field = extensions[i];
+ _eglLog(_EGL_INFO, "DRI2: found extension %s version %d",
+ extensions[i]->name, extensions[i]->version);
+ }
+ }
+ }
+
+ for (j = 0; matches[j].name; j++) {
+ field = ((char *) dri2_dpy + matches[j].offset);
+ if (*(const __DRIextension **) field == NULL) {
+ _eglLog(_EGL_FATAL, "DRI2: did not find extension %s version %d",
+ matches[j].name, matches[j].version);
+ ret = EGL_FALSE;
+ }
+ }
+
+ return ret;
+}
+
+static char *
+dri2_strndup(const char *s, int length)
+{
+ char *d;
+
+ d = malloc(length + 1);
+ if (d == NULL)
+ return NULL;
+
+ memcpy(d, s, length);
+ d[length] = '\0';
+
+ return d;
+}
+
+static EGLBoolean
+dri2_connect(struct dri2_egl_display *dri2_dpy)
+{
+ xcb_xfixes_query_version_reply_t *xfixes_query;
+ xcb_xfixes_query_version_cookie_t xfixes_query_cookie;
+ xcb_dri2_query_version_reply_t *dri2_query;
+ xcb_dri2_query_version_cookie_t dri2_query_cookie;
+ xcb_dri2_connect_reply_t *connect;
+ xcb_dri2_connect_cookie_t connect_cookie;
+ xcb_generic_error_t *error;
+ xcb_screen_iterator_t s;
+
+ xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_xfixes_id);
+ xcb_prefetch_extension_data (dri2_dpy->conn, &xcb_dri2_id);
+
+ xfixes_query_cookie = xcb_xfixes_query_version(dri2_dpy->conn,
+ XCB_XFIXES_MAJOR_VERSION,
+ XCB_XFIXES_MINOR_VERSION);
+
+ dri2_query_cookie = xcb_dri2_query_version (dri2_dpy->conn,
+ XCB_DRI2_MAJOR_VERSION,
+ XCB_DRI2_MINOR_VERSION);
+
+ s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
+ connect_cookie = xcb_dri2_connect_unchecked (dri2_dpy->conn,
+ s.data->root,
+ XCB_DRI2_DRIVER_TYPE_DRI);
+
+ xfixes_query =
+ xcb_xfixes_query_version_reply (dri2_dpy->conn,
+ xfixes_query_cookie, &error);
+ if (xfixes_query == NULL ||
+ error != NULL || xfixes_query->major_version < 2) {
+ _eglLog(_EGL_FATAL, "DRI2: failed to query xfixes version");
+ free(error);
+ return EGL_FALSE;
+ }
+ free(xfixes_query);
+
+ dri2_query =
+ xcb_dri2_query_version_reply (dri2_dpy->conn, dri2_query_cookie, &error);
+ if (dri2_query == NULL || error != NULL) {
+ _eglLog(_EGL_FATAL, "DRI2: failed to query version");
+ free(error);
+ return EGL_FALSE;
+ }
+ dri2_dpy->dri2_major = dri2_query->major_version;
+ dri2_dpy->dri2_minor = dri2_query->minor_version;
+ free(dri2_query);
+
+ connect = xcb_dri2_connect_reply (dri2_dpy->conn, connect_cookie, NULL);
+ if (connect == NULL ||
+ connect->driver_name_length + connect->device_name_length == 0) {
+ _eglLog(_EGL_FATAL, "DRI2: failed to authenticate");
+ return EGL_FALSE;
+ }
+
+ dri2_dpy->device_name =
+ dri2_strndup(xcb_dri2_connect_device_name (connect),
+ xcb_dri2_connect_device_name_length (connect));
+
+ dri2_dpy->driver_name =
+ dri2_strndup(xcb_dri2_connect_driver_name (connect),
+ xcb_dri2_connect_driver_name_length (connect));
+
+ if (dri2_dpy->device_name == NULL || dri2_dpy->driver_name == NULL) {
+ free(dri2_dpy->device_name);
+ free(dri2_dpy->driver_name);
+ free(connect);
+ return EGL_FALSE;
+ }
+ free(connect);
+
+ return EGL_TRUE;
+}
+
+static EGLBoolean
+dri2_authenticate(struct dri2_egl_display *dri2_dpy)
+{
+ xcb_dri2_authenticate_reply_t *authenticate;
+ xcb_dri2_authenticate_cookie_t authenticate_cookie;
+ xcb_screen_iterator_t s;
+ drm_magic_t magic;
+
+ if (drmGetMagic(dri2_dpy->fd, &magic)) {
+ _eglLog(_EGL_FATAL, "DRI2: failed to get drm magic");
+ return EGL_FALSE;
+ }
+
+ s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
+ authenticate_cookie =
+ xcb_dri2_authenticate_unchecked(dri2_dpy->conn, s.data->root, magic);
+ authenticate =
+ xcb_dri2_authenticate_reply(dri2_dpy->conn, authenticate_cookie, NULL);
+ if (authenticate == NULL || !authenticate->authenticated) {
+ _eglLog(_EGL_FATAL, "DRI2: failed to authenticate");
+ free(authenticate);
+ return EGL_FALSE;
+ }
+
+ free(authenticate);
+
+ return EGL_TRUE;
+}
+
+static EGLBoolean
+dri2_add_configs_for_visuals(struct dri2_egl_display *dri2_dpy,
+ _EGLDisplay *disp)
+{
+ xcb_screen_iterator_t s;
+ xcb_depth_iterator_t d;
+ xcb_visualtype_t *visuals;
+ int i, j, id;
+
+ s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
+ d = xcb_screen_allowed_depths_iterator(s.data);
+ id = 1;
+ while (d.rem > 0) {
+ EGLBoolean class_added[6] = { 0, };
+
+ visuals = xcb_depth_visuals(d.data);
+ for (i = 0; i < xcb_depth_visuals_length(d.data); i++) {
+ if (class_added[visuals[i]._class])
+ continue;
+
+ class_added[visuals[i]._class] = EGL_TRUE;
+ for (j = 0; dri2_dpy->driver_configs[j]; j++)
+ dri2_add_config(disp, dri2_dpy->driver_configs[j],
+ id++, d.data->depth, &visuals[i]);
+ }
+
+ xcb_depth_next(&d);
+ }
+
+ if (!disp->NumConfigs) {
+ _eglLog(_EGL_WARNING, "DRI2: failed to create any config");
+ return EGL_FALSE;
+ }
+
+ return EGL_TRUE;
+}
+
+static EGLBoolean
+dri2_load_driver(_EGLDisplay *disp)
+{
+ struct dri2_egl_display *dri2_dpy = disp->DriverData;
+ const __DRIextension **extensions;
+ char path[PATH_MAX], *search_paths, *p, *next, *end;
+
+ search_paths = NULL;
+ if (geteuid() == getuid()) {
+ /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
+ search_paths = getenv("LIBGL_DRIVERS_PATH");
+ }
+ if (search_paths == NULL)
+ search_paths = DEFAULT_DRIVER_DIR;
+
+ dri2_dpy->driver = NULL;
+ end = search_paths + strlen(search_paths);
+ for (p = search_paths; p < end && dri2_dpy->driver == NULL; p = next + 1) {
+ int len;
+ next = strchr(p, ':');
+ if (next == NULL)
+ next = end;
+
+ len = next - p;
+#if GLX_USE_TLS
+ snprintf(path, sizeof path,
+ "%.*s/tls/%s_dri.so", len, p, dri2_dpy->driver_name);
+ dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
+#endif
+ if (dri2_dpy->driver == NULL) {
+ snprintf(path, sizeof path,
+ "%.*s/%s_dri.so", len, p, dri2_dpy->driver_name);
+ dri2_dpy->driver = dlopen(path, RTLD_NOW | RTLD_GLOBAL);
+ if (dri2_dpy->driver == NULL)
+ _eglLog(_EGL_DEBUG, "failed to open %s: %s\n", path, dlerror());
+ }
+ }
+
+ if (dri2_dpy->driver == NULL) {
+ _eglLog(_EGL_WARNING,
+ "DRI2: failed to open any driver (search paths %s)",
+ search_paths);
+ return EGL_FALSE;
+ }
+
+ _eglLog(_EGL_DEBUG, "DRI2: dlopen(%s)", path);
+ extensions = dlsym(dri2_dpy->driver, __DRI_DRIVER_EXTENSIONS);
+ if (extensions == NULL) {
+ _eglLog(_EGL_WARNING,
+ "DRI2: driver exports no extensions (%s)", dlerror());
+ dlclose(dri2_dpy->driver);
+ return EGL_FALSE;
+ }
+
+ if (!dri2_bind_extensions(dri2_dpy, dri2_driver_extensions, extensions)) {
+ dlclose(dri2_dpy->driver);
+ return EGL_FALSE;
+ }
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Called via eglInitialize(), GLX_drv->API.Initialize().
+ */
+static EGLBoolean
+dri2_initialize(_EGLDriver *drv, _EGLDisplay *disp,
+ EGLint *major, EGLint *minor)
+{
+ const __DRIextension **extensions;
+ struct dri2_egl_display *dri2_dpy;
+ unsigned int api_mask;
+
+ dri2_dpy = malloc(sizeof *dri2_dpy);
+ if (!dri2_dpy)
+ return _eglError(EGL_BAD_ALLOC, "eglInitialize");
+
+ disp->DriverData = (void *) dri2_dpy;
+ if (disp->NativeDisplay == NULL) {
+ dri2_dpy->conn = xcb_connect(0, 0);
+ } else {
+ dri2_dpy->conn = XGetXCBConnection(disp->NativeDisplay);
+ }
+
+ if (xcb_connection_has_error(dri2_dpy->conn)) {
+ _eglLog(_EGL_WARNING, "DRI2: xcb_connect failed");
+ goto cleanup_dpy;
+ }
+
+ if (dri2_dpy->conn) {
+ if (!dri2_connect(dri2_dpy))
+ goto cleanup_conn;
+ }
+
+ if (!dri2_load_driver(disp))
+ goto cleanup_conn;
+
+ dri2_dpy->fd = open(dri2_dpy->device_name, O_RDWR);
+ if (dri2_dpy->fd == -1) {
+ _eglLog(_EGL_WARNING,
+ "DRI2: could not open %s (%s)", dri2_dpy->device_name,
+ strerror(errno));
+ goto cleanup_driver;
+ }
+
+ if (dri2_dpy->conn) {
+ if (!dri2_authenticate(dri2_dpy))
+ goto cleanup_fd;
+ }
+
+ if (dri2_dpy->dri2_minor >= 1) {
+ dri2_dpy->loader_extension.base.name = __DRI_DRI2_LOADER;
+ dri2_dpy->loader_extension.base.version = 3;
+ dri2_dpy->loader_extension.getBuffers = dri2_get_buffers;
+ dri2_dpy->loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
+ dri2_dpy->loader_extension.getBuffersWithFormat =
+ dri2_get_buffers_with_format;
+ } else {
+ dri2_dpy->loader_extension.base.name = __DRI_DRI2_LOADER;
+ dri2_dpy->loader_extension.base.version = 2;
+ dri2_dpy->loader_extension.getBuffers = dri2_get_buffers;
+ dri2_dpy->loader_extension.flushFrontBuffer = dri2_flush_front_buffer;
+ dri2_dpy->loader_extension.getBuffersWithFormat = NULL;
+ }
+
+ dri2_dpy->image_lookup_extension.base.name = __DRI_IMAGE_LOOKUP;
+ dri2_dpy->image_lookup_extension.base.version = 1;
+ dri2_dpy->image_lookup_extension.lookupEGLImage = dri2_lookup_egl_image;
+
+ dri2_dpy->extensions[0] = &dri2_dpy->loader_extension.base;
+ dri2_dpy->extensions[1] = &dri2_dpy->image_lookup_extension.base;
+ dri2_dpy->extensions[2] = NULL;
+
+ dri2_dpy->dri_screen =
+ dri2_dpy->dri2->createNewScreen(0, dri2_dpy->fd, dri2_dpy->extensions,
+ &dri2_dpy->driver_configs, dri2_dpy);
+
+ if (dri2_dpy->dri_screen == NULL) {
+ _eglLog(_EGL_WARNING, "DRI2: failed to create dri screen");
+ goto cleanup_fd;
+ }
+
+ extensions = dri2_dpy->core->getExtensions(dri2_dpy->dri_screen);
+ if (!dri2_bind_extensions(dri2_dpy, dri2_core_extensions, extensions))
+ goto cleanup_dri_screen;
+
+ if (dri2_dpy->dri2->base.version >= 2)
+ api_mask = dri2_dpy->dri2->getAPIMask(dri2_dpy->dri_screen);
+ else
+ api_mask = __DRI_API_OPENGL;
+
+ disp->ClientAPIsMask = 0;
+ if (api_mask & (1 <<__DRI_API_OPENGL))
+ disp->ClientAPIsMask |= EGL_OPENGL_BIT;
+ if (api_mask & (1 <<__DRI_API_GLES))
+ disp->ClientAPIsMask |= EGL_OPENGL_ES_BIT;
+ if (api_mask & (1 << __DRI_API_GLES2))
+ disp->ClientAPIsMask |= EGL_OPENGL_ES2_BIT;
+
+ if (dri2_dpy->conn) {
+ if (!dri2_add_configs_for_visuals(dri2_dpy, disp))
+ goto cleanup_configs;
+ }
+
+ disp->Extensions.KHR_image_base = EGL_TRUE;
+ disp->Extensions.KHR_image_pixmap = EGL_TRUE;
+ disp->Extensions.KHR_gl_renderbuffer_image = EGL_TRUE;
+ disp->Extensions.KHR_gl_texture_2D_image = EGL_TRUE;
+ disp->Extensions.NOK_swap_region = EGL_TRUE;
+ disp->Extensions.NOK_texture_from_pixmap = EGL_TRUE;
+
+ /* we're supporting EGL 1.4 */
+ *major = 1;
+ *minor = 4;
+
+ return EGL_TRUE;
+
+ cleanup_configs:
+ _eglCleanupDisplay(disp);
+ cleanup_dri_screen:
+ dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
+ cleanup_fd:
+ close(dri2_dpy->fd);
+ cleanup_driver:
+ dlclose(dri2_dpy->driver);
+ cleanup_conn:
+ if (disp->NativeDisplay == NULL)
+ xcb_disconnect(dri2_dpy->conn);
+ cleanup_dpy:
+ free(dri2_dpy);
+
+ return EGL_FALSE;
+}
+
+/**
+ * Called via eglTerminate(), drv->API.Terminate().
+ */
+static EGLBoolean
+dri2_terminate(_EGLDriver *drv, _EGLDisplay *disp)
+{
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+
+ _eglReleaseDisplayResources(drv, disp);
+ _eglCleanupDisplay(disp);
+
+ dri2_dpy->core->destroyScreen(dri2_dpy->dri_screen);
+ close(dri2_dpy->fd);
+ dlclose(dri2_dpy->driver);
+ if (disp->NativeDisplay == NULL)
+ xcb_disconnect(dri2_dpy->conn);
+ free(dri2_dpy);
+ disp->DriverData = NULL;
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Called via eglCreateContext(), drv->API.CreateContext().
+ */
+static _EGLContext *
+dri2_create_context(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
+ _EGLContext *share_list, const EGLint *attrib_list)
+{
+ struct dri2_egl_context *dri2_ctx;
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+ struct dri2_egl_context *dri2_ctx_shared = dri2_egl_context(share_list);
+ struct dri2_egl_config *dri2_config = dri2_egl_config(conf);
+ int api;
+
+ dri2_ctx = malloc(sizeof *dri2_ctx);
+ if (!dri2_ctx) {
+ _eglError(EGL_BAD_ALLOC, "eglCreateContext");
+ return NULL;
+ }
+
+ if (!_eglInitContext(&dri2_ctx->base, disp, conf, attrib_list))
+ goto cleanup;
+
+ switch (dri2_ctx->base.ClientAPI) {
+ case EGL_OPENGL_ES_API:
+ switch (dri2_ctx->base.ClientVersion) {
+ case 1:
+ api = __DRI_API_GLES;
+ break;
+ case 2:
+ api = __DRI_API_GLES2;
+ break;
+ default:
+ _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
+ return NULL;
+ }
+ break;
+ case EGL_OPENGL_API:
+ api = __DRI_API_OPENGL;
+ break;
+ default:
+ _eglError(EGL_BAD_PARAMETER, "eglCreateContext");
+ return NULL;
+ }
+
+ if (dri2_dpy->dri2->base.version >= 2) {
+ dri2_ctx->dri_context =
+ dri2_dpy->dri2->createNewContextForAPI(dri2_dpy->dri_screen,
+ api,
+ dri2_config->dri_config,
+ dri2_ctx_shared ?
+ dri2_ctx_shared->dri_context : NULL,
+ dri2_ctx);
+ } else if (api == __DRI_API_OPENGL) {
+ dri2_ctx->dri_context =
+ dri2_dpy->dri2->createNewContext(dri2_dpy->dri_screen,
+ dri2_config->dri_config,
+ dri2_ctx_shared ?
+ dri2_ctx_shared->dri_context : NULL,
+ dri2_ctx);
+ } else {
+ /* fail */
+ }
+
+ if (!dri2_ctx->dri_context)
+ goto cleanup;
+
+ return &dri2_ctx->base;
+
+ cleanup:
+ free(dri2_ctx);
+ return NULL;
+}
+
+static EGLBoolean
+dri2_destroy_surface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
+{
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+ struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
+
+ if (_eglIsSurfaceBound(surf))
+ return EGL_TRUE;
+
+ (*dri2_dpy->core->destroyDrawable)(dri2_surf->dri_drawable);
+
+ xcb_dri2_destroy_drawable (dri2_dpy->conn, dri2_surf->drawable);
+
+ if (surf->Type == EGL_PBUFFER_BIT)
+ xcb_free_pixmap (dri2_dpy->conn, dri2_surf->drawable);
+
+ free(surf);
+
+ return EGL_TRUE;
+}
+
+/**
+ * Called via eglMakeCurrent(), drv->API.MakeCurrent().
+ */
+static EGLBoolean
+dri2_make_current(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf,
+ _EGLSurface *rsurf, _EGLContext *ctx)
+{
+ struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+ struct dri2_egl_surface *dri2_dsurf = dri2_egl_surface(dsurf);
+ struct dri2_egl_surface *dri2_rsurf = dri2_egl_surface(rsurf);
+ struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
+ __DRIdrawable *ddraw, *rdraw;
+ __DRIcontext *cctx;
+
+ /* bind the new context and return the "orphaned" one */
+ if (!_eglBindContext(&ctx, &dsurf, &rsurf))
+ return EGL_FALSE;
+
+ /* flush before context switch */
+ if (ctx && dri2_drv->glFlush)
+ dri2_drv->glFlush();
+
+ ddraw = (dri2_dsurf) ? dri2_dsurf->dri_drawable : NULL;
+ rdraw = (dri2_rsurf) ? dri2_rsurf->dri_drawable : NULL;
+ cctx = (dri2_ctx) ? dri2_ctx->dri_context : NULL;
+
+ if ((cctx == NULL && ddraw == NULL && rdraw == NULL) ||
+ dri2_dpy->core->bindContext(cctx, ddraw, rdraw)) {
+ if (dsurf && !_eglIsSurfaceLinked(dsurf))
+ dri2_destroy_surface(drv, disp, dsurf);
+ if (rsurf && rsurf != dsurf && !_eglIsSurfaceLinked(dsurf))
+ dri2_destroy_surface(drv, disp, rsurf);
+ if (ctx != NULL && !_eglIsContextLinked(ctx))
+ dri2_dpy->core->unbindContext(dri2_egl_context(ctx)->dri_context);
+
+ return EGL_TRUE;
+ } else {
+ _eglBindContext(&ctx, &dsurf, &rsurf);
+
+ return EGL_FALSE;
+ }
+}
+
+/**
+ * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
+ */
+static _EGLSurface *
+dri2_create_surface(_EGLDriver *drv, _EGLDisplay *disp, EGLint type,
+ _EGLConfig *conf, EGLNativeWindowType window,
+ const EGLint *attrib_list)
+{
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+ struct dri2_egl_config *dri2_conf = dri2_egl_config(conf);
+ struct dri2_egl_surface *dri2_surf;
+ xcb_get_geometry_cookie_t cookie;
+ xcb_get_geometry_reply_t *reply;
+ xcb_screen_iterator_t s;
+ xcb_generic_error_t *error;
+
+ dri2_surf = malloc(sizeof *dri2_surf);
+ if (!dri2_surf) {
+ _eglError(EGL_BAD_ALLOC, "dri2_create_surface");
+ return NULL;
+ }
+
+ if (!_eglInitSurface(&dri2_surf->base, disp, type, conf, attrib_list))
+ goto cleanup_surf;
+
+ dri2_surf->region = XCB_NONE;
+ if (type == EGL_PBUFFER_BIT) {
+ dri2_surf->drawable = xcb_generate_id(dri2_dpy->conn);
+ s = xcb_setup_roots_iterator(xcb_get_setup(dri2_dpy->conn));
+ xcb_create_pixmap(dri2_dpy->conn,
+ _eglGetConfigKey(conf, EGL_BUFFER_SIZE),
+ dri2_surf->drawable, s.data->root,
+ dri2_surf->base.Width, dri2_surf->base.Height);
+ } else {
+ dri2_surf->drawable = window;
+ }
+
+ dri2_surf->dri_drawable =
+ (*dri2_dpy->dri2->createNewDrawable) (dri2_dpy->dri_screen,
+ dri2_conf->dri_config, dri2_surf);
+ if (dri2_surf->dri_drawable == NULL) {
+ _eglError(EGL_BAD_ALLOC, "dri2->createNewDrawable");
+ goto cleanup_pixmap;
+ }
+
+ xcb_dri2_create_drawable (dri2_dpy->conn, dri2_surf->drawable);
+
+ if (type != EGL_PBUFFER_BIT) {
+ cookie = xcb_get_geometry (dri2_dpy->conn, dri2_surf->drawable);
+ reply = xcb_get_geometry_reply (dri2_dpy->conn, cookie, &error);
+ if (reply == NULL || error != NULL) {
+ _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
+ free(error);
+ goto cleanup_dri_drawable;
+ }
+
+ dri2_surf->base.Width = reply->width;
+ dri2_surf->base.Height = reply->height;
+ free(reply);
+ }
+
+ return &dri2_surf->base;
+
+ cleanup_dri_drawable:
+ dri2_dpy->core->destroyDrawable(dri2_surf->dri_drawable);
+ cleanup_pixmap:
+ if (type == EGL_PBUFFER_BIT)
+ xcb_free_pixmap(dri2_dpy->conn, dri2_surf->drawable);
+ cleanup_surf:
+ free(dri2_surf);
+
+ return NULL;
+}
+
+/**
+ * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
+ */
+static _EGLSurface *
+dri2_create_window_surface(_EGLDriver *drv, _EGLDisplay *disp,
+ _EGLConfig *conf, EGLNativeWindowType window,
+ const EGLint *attrib_list)
+{
+ return dri2_create_surface(drv, disp, EGL_WINDOW_BIT, conf,
+ window, attrib_list);
+}
+
+static _EGLSurface *
+dri2_create_pixmap_surface(_EGLDriver *drv, _EGLDisplay *disp,
+ _EGLConfig *conf, EGLNativePixmapType pixmap,
+ const EGLint *attrib_list)
+{
+ return dri2_create_surface(drv, disp, EGL_PIXMAP_BIT, conf,
+ pixmap, attrib_list);
+}
+
+static _EGLSurface *
+dri2_create_pbuffer_surface(_EGLDriver *drv, _EGLDisplay *disp,
+ _EGLConfig *conf, const EGLint *attrib_list)
+{
+ return dri2_create_surface(drv, disp, EGL_PBUFFER_BIT, conf,
+ XCB_WINDOW_NONE, attrib_list);
+}
+
+static EGLBoolean
+dri2_copy_region(_EGLDriver *drv, _EGLDisplay *disp,
+ _EGLSurface *draw, xcb_xfixes_region_t region)
+{
+ struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+ struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
+ _EGLContext *ctx;
+ xcb_dri2_copy_region_cookie_t cookie;
+
+ if (dri2_drv->glFlush) {
+ ctx = _eglGetCurrentContext();
+ if (ctx && ctx->DrawSurface == &dri2_surf->base)
+ dri2_drv->glFlush();
+ }
+
+ (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
+
+#if 0
+ /* FIXME: Add support for dri swapbuffers, that'll give us swap
+ * interval and page flipping (at least for fullscreen windows) as
+ * well as the page flip event. Unless surface->SwapBehavior is
+ * EGL_BUFFER_PRESERVED. */
+#if __DRI2_FLUSH_VERSION >= 2
+ if (pdraw->psc->f)
+ (*pdraw->psc->f->flushInvalidate)(pdraw->driDrawable);
+#endif
+#endif
+
+ if (!dri2_surf->have_fake_front)
+ return EGL_TRUE;
+
+ cookie = xcb_dri2_copy_region_unchecked(dri2_dpy->conn,
+ dri2_surf->drawable,
+ region,
+ XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT,
+ XCB_DRI2_ATTACHMENT_BUFFER_FAKE_FRONT_LEFT);
+ free(xcb_dri2_copy_region_reply(dri2_dpy->conn, cookie, NULL));
+
+ return EGL_TRUE;
+}
+
+static EGLBoolean
+dri2_swap_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
+{
+ struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
+
+ return dri2_copy_region(drv, disp, draw, dri2_surf->region);
+}
+
+static EGLBoolean
+dri2_swap_buffers_region(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw,
+ EGLint numRects, const EGLint *rects)
+{
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+ struct dri2_egl_surface *dri2_surf = dri2_egl_surface(draw);
+ EGLBoolean ret;
+ xcb_xfixes_region_t region;
+ xcb_rectangle_t rectangles[16];
+ int i;
+
+ if (numRects > ARRAY_SIZE(rectangles))
+ return dri2_copy_region(drv, disp, draw, dri2_surf->region);
+
+ /* FIXME: Invert y here? */
+ for (i = 0; i < numRects; i++) {
+ rectangles[i].x = rects[i * 4];
+ rectangles[i].y = rects[i * 4 + 1];
+ rectangles[i].width = rects[i * 4 + 2];
+ rectangles[i].height = rects[i * 4 + 3];
+ }
+
+ region = xcb_generate_id(dri2_dpy->conn);
+ xcb_xfixes_create_region(dri2_dpy->conn, region, numRects, rectangles);
+ ret = dri2_copy_region(drv, disp, draw, region);
+ xcb_xfixes_destroy_region(dri2_dpy->conn, region);
+
+ return ret;
+}
+
+/*
+ * Called from eglGetProcAddress() via drv->API.GetProcAddress().
+ */
+static _EGLProc
+dri2_get_proc_address(_EGLDriver *drv, const char *procname)
+{
+ /* FIXME: Do we need to support lookup of EGL symbols too? */
+
+ return (_EGLProc) _glapi_get_proc_address(procname);
+}
+
+static EGLBoolean
+dri2_wait_client(_EGLDriver *drv, _EGLDisplay *disp, _EGLContext *ctx)
+{
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+ struct dri2_egl_surface *dri2_surf = dri2_egl_surface(ctx->DrawSurface);
+
+ /* FIXME: If EGL allows frontbuffer rendering for window surfaces,
+ * we need to copy fake to real here.*/
+
+ (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
+
+ return EGL_TRUE;
+}
+
+static EGLBoolean
+dri2_wait_native(_EGLDriver *drv, _EGLDisplay *disp, EGLint engine)
+{
+ if (engine != EGL_CORE_NATIVE_ENGINE)
+ return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
+ /* glXWaitX(); */
+
+ return EGL_TRUE;
+}
+
+static void
+dri2_unload(_EGLDriver *drv)
+{
+ struct dri2_egl_driver *dri2_drv = dri2_egl_driver(drv);
+ free(dri2_drv);
+}
+
+static EGLBoolean
+dri2_copy_buffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf,
+ EGLNativePixmapType target)
+{
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+ struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
+ xcb_gcontext_t gc;
+
+ (*dri2_dpy->flush->flush)(dri2_surf->dri_drawable);
+
+ gc = xcb_generate_id(dri2_dpy->conn);
+ xcb_create_gc(dri2_dpy->conn, gc, target, 0, NULL);
+ xcb_copy_area(dri2_dpy->conn,
+ dri2_surf->drawable,
+ target,
+ gc,
+ 0, 0,
+ 0, 0,
+ dri2_surf->base.Width,
+ dri2_surf->base.Height);
+ xcb_free_gc(dri2_dpy->conn, gc);
+
+ return EGL_TRUE;
+}
+
+static EGLBoolean
+dri2_bind_tex_image(_EGLDriver *drv,
+ _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
+{
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+ struct dri2_egl_surface *dri2_surf = dri2_egl_surface(surf);
+ struct dri2_egl_context *dri2_ctx;
+ _EGLContext *ctx;
+ GLint format, target;
+
+ ctx = _eglGetCurrentContext();
+ dri2_ctx = dri2_egl_context(ctx);
+
+ if (!_eglBindTexImage(drv, disp, surf, buffer))
+ return EGL_FALSE;
+
+ switch (dri2_surf->base.TextureFormat) {
+ case EGL_TEXTURE_RGB:
+ format = __DRI_TEXTURE_FORMAT_RGB;
+ break;
+ case EGL_TEXTURE_RGBA:
+ format = __DRI_TEXTURE_FORMAT_RGBA;
+ break;
+ default:
+ assert(0);
+ }
+
+ switch (dri2_surf->base.TextureTarget) {
+ case EGL_TEXTURE_2D:
+ target = GL_TEXTURE_2D;
+ break;
+ default:
+ assert(0);
+ }
+
+ (*dri2_dpy->tex_buffer->setTexBuffer2)(dri2_ctx->dri_context,
+ target, format,
+ dri2_surf->dri_drawable);
+
+ return EGL_TRUE;
+}
+
+static EGLBoolean
+dri2_release_tex_image(_EGLDriver *drv,
+ _EGLDisplay *disp, _EGLSurface *surf, EGLint buffer)
+{
+ return EGL_TRUE;
+}
+
+static _EGLImage *
+dri2_create_image_khr_pixmap(_EGLDisplay *disp, _EGLContext *ctx,
+ EGLClientBuffer buffer, const EGLint *attr_list)
+{
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+ struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
+ struct dri2_egl_image *dri2_img;
+ unsigned int attachments[1];
+ xcb_drawable_t drawable;
+ xcb_dri2_get_buffers_cookie_t buffers_cookie;
+ xcb_dri2_get_buffers_reply_t *buffers_reply;
+ xcb_dri2_dri2_buffer_t *buffers;
+ xcb_get_geometry_cookie_t geometry_cookie;
+ xcb_get_geometry_reply_t *geometry_reply;
+ xcb_generic_error_t *error;
+ int stride, format;
+
+ drawable = (xcb_drawable_t) buffer;
+ xcb_dri2_create_drawable (dri2_dpy->conn, drawable);
+ attachments[0] = XCB_DRI2_ATTACHMENT_BUFFER_FRONT_LEFT;
+ buffers_cookie =
+ xcb_dri2_get_buffers_unchecked (dri2_dpy->conn,
+ drawable, 1, 1, attachments);
+ geometry_cookie = xcb_get_geometry (dri2_dpy->conn, drawable);
+ buffers_reply = xcb_dri2_get_buffers_reply (dri2_dpy->conn,
+ buffers_cookie, NULL);
+ buffers = xcb_dri2_get_buffers_buffers (buffers_reply);
+ if (buffers == NULL) {
+ return NULL;
+ }
+
+ geometry_reply = xcb_get_geometry_reply (dri2_dpy->conn,
+ geometry_cookie, &error);
+ if (geometry_reply == NULL || error != NULL) {
+ _eglError(EGL_BAD_ALLOC, "xcb_get_geometry");
+ free(error);
+ free(buffers_reply);
+ }
+
+ switch (geometry_reply->depth) {
+ case 16:
+ format = __DRI_IMAGE_FORMAT_RGB565;
+ break;
+ case 24:
+ format = __DRI_IMAGE_FORMAT_XRGB8888;
+ break;
+ case 32:
+ format = __DRI_IMAGE_FORMAT_ARGB8888;
+ break;
+ default:
+ _eglError(EGL_BAD_PARAMETER,
+ "dri2_create_image_khr: unsupported pixmap depth");
+ free(buffers_reply);
+ free(geometry_reply);
+ return NULL;
+ }
+
+ dri2_img = malloc(sizeof *dri2_img);
+ if (!dri2_img) {
+ free(buffers_reply);
+ free(geometry_reply);
+ _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
+ return EGL_NO_IMAGE_KHR;
+ }
+
+ if (!_eglInitImage(&dri2_img->base, disp, attr_list)) {
+ free(buffers_reply);
+ free(geometry_reply);
+ return EGL_NO_IMAGE_KHR;
+ }
+
+ stride = buffers[0].pitch / buffers[0].cpp;
+ dri2_img->dri_image =
+ dri2_dpy->image->createImageFromName(dri2_ctx->dri_context,
+ buffers_reply->width,
+ buffers_reply->height,
+ format,
+ buffers[0].name,
+ stride,
+ dri2_img);
+
+ free(buffers_reply);
+ free(geometry_reply);
+
+ return &dri2_img->base;
+}
+
+static _EGLImage *
+dri2_create_image_khr_renderbuffer(_EGLDisplay *disp, _EGLContext *ctx,
+ EGLClientBuffer buffer,
+ const EGLint *attr_list)
+{
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+ struct dri2_egl_context *dri2_ctx = dri2_egl_context(ctx);
+ struct dri2_egl_image *dri2_img;
+ GLuint renderbuffer = (GLuint) buffer;
+
+ if (renderbuffer == 0) {
+ _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
+ return EGL_NO_IMAGE_KHR;
+ }
+
+ dri2_img = malloc(sizeof *dri2_img);
+ if (!dri2_img) {
+ _eglError(EGL_BAD_ALLOC, "dri2_create_image_khr");
+ return EGL_NO_IMAGE_KHR;
+ }
+
+ if (!_eglInitImage(&dri2_img->base, disp, attr_list))
+ return EGL_NO_IMAGE_KHR;
+
+ dri2_img->dri_image =
+ dri2_dpy->image->createImageFromRenderbuffer(dri2_ctx->dri_context,
+ renderbuffer,
+ dri2_img);
+
+ return &dri2_img->base;
+}
+
+static _EGLImage *
+dri2_create_image_khr(_EGLDriver *drv, _EGLDisplay *disp,
+ _EGLContext *ctx, EGLenum target,
+ EGLClientBuffer buffer, const EGLint *attr_list)
+{
+ switch (target) {
+ case EGL_NATIVE_PIXMAP_KHR:
+ return dri2_create_image_khr_pixmap(disp, ctx, buffer, attr_list);
+ case EGL_GL_RENDERBUFFER_KHR:
+ return dri2_create_image_khr_renderbuffer(disp, ctx, buffer, attr_list);
+ default:
+ _eglError(EGL_BAD_PARAMETER, "dri2_create_image_khr");
+ return EGL_NO_IMAGE_KHR;
+ }
+}
+
+static EGLBoolean
+dri2_destroy_image_khr(_EGLDriver *drv, _EGLDisplay *disp, _EGLImage *image)
+{
+ struct dri2_egl_display *dri2_dpy = dri2_egl_display(disp);
+ struct dri2_egl_image *dri2_img = dri2_egl_image(image);
+
+ dri2_dpy->image->destroyImage(dri2_img->dri_image);
+ free(dri2_img);
+
+ return EGL_TRUE;
+}
+
+/**
+ * This is the main entrypoint into the driver, called by libEGL.
+ * Create a new _EGLDriver object and init its dispatch table.
+ */
+_EGLDriver *
+_eglMain(const char *args)
+{
+ struct dri2_egl_driver *dri2_drv;
+
+ dri2_drv = malloc(sizeof *dri2_drv);
+ if (!dri2_drv)
+ return NULL;
+
+ memset(dri2_drv, 0, sizeof *dri2_drv);
+ _eglInitDriverFallbacks(&dri2_drv->base);
+ dri2_drv->base.API.Initialize = dri2_initialize;
+ dri2_drv->base.API.Terminate = dri2_terminate;
+ dri2_drv->base.API.CreateContext = dri2_create_context;
+ dri2_drv->base.API.MakeCurrent = dri2_make_current;
+ dri2_drv->base.API.CreateWindowSurface = dri2_create_window_surface;
+ dri2_drv->base.API.CreatePixmapSurface = dri2_create_pixmap_surface;
+ dri2_drv->base.API.CreatePbufferSurface = dri2_create_pbuffer_surface;
+ dri2_drv->base.API.DestroySurface = dri2_destroy_surface;
+ dri2_drv->base.API.SwapBuffers = dri2_swap_buffers;
+ dri2_drv->base.API.GetProcAddress = dri2_get_proc_address;
+ dri2_drv->base.API.WaitClient = dri2_wait_client;
+ dri2_drv->base.API.WaitNative = dri2_wait_native;
+ dri2_drv->base.API.CopyBuffers = dri2_copy_buffers;
+ dri2_drv->base.API.BindTexImage = dri2_bind_tex_image;
+ dri2_drv->base.API.ReleaseTexImage = dri2_release_tex_image;
+ dri2_drv->base.API.CreateImageKHR = dri2_create_image_khr;
+ dri2_drv->base.API.DestroyImageKHR = dri2_destroy_image_khr;
+ dri2_drv->base.API.SwapBuffersRegionNOK = dri2_swap_buffers_region;
+
+ dri2_drv->base.Name = "DRI2";
+ dri2_drv->base.Unload = dri2_unload;
+
+ dri2_drv->glFlush =
+ (void (*)(void)) dri2_get_proc_address(&dri2_drv->base, "glFlush");
+
+ return &dri2_drv->base;
+}
diff --git a/src/egl/drivers/glx/Makefile b/src/egl/drivers/glx/Makefile
new file mode 100644
index 0000000000..d976b91a74
--- /dev/null
+++ b/src/egl/drivers/glx/Makefile
@@ -0,0 +1,16 @@
+# src/egl/drivers/glx/Makefile
+
+TOP = ../../../..
+include $(TOP)/configs/current
+
+EGL_DRIVER = egl_glx.so
+EGL_SOURCES = egl_glx.c
+
+EGL_INCLUDES = \
+ -I$(TOP)/include \
+ -I$(TOP)/src/egl/main
+
+EGL_CFLAGS = $(X11_CFLAGS)
+EGL_LIBS = $(X11_LIBS) -lGL
+
+include ../Makefile.template
diff --git a/src/egl/drivers/glx/egl_glx.c b/src/egl/drivers/glx/egl_glx.c
new file mode 100644
index 0000000000..e08ef5f222
--- /dev/null
+++ b/src/egl/drivers/glx/egl_glx.c
@@ -0,0 +1,941 @@
+/**************************************************************************
+ *
+ * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+/**
+ * This is an EGL driver that wraps GLX. This gives the benefit of being
+ * completely agnostic of the direct rendering implementation.
+ *
+ * Authors: Alan Hourihane <alanh@tungstengraphics.com>
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <X11/Xlib.h>
+#include <GL/glx.h>
+
+#include "eglconfigutil.h"
+#include "eglconfig.h"
+#include "eglcontext.h"
+#include "egldefines.h"
+#include "egldisplay.h"
+#include "egldriver.h"
+#include "eglcurrent.h"
+#include "egllog.h"
+#include "eglsurface.h"
+
+#define CALLOC_STRUCT(T) (struct T *) calloc(1, sizeof(struct T))
+
+#ifndef GLX_VERSION_1_4
+#error "GL/glx.h must be equal to or greater than GLX 1.4"
+#endif
+
+/** subclass of _EGLDriver */
+struct GLX_egl_driver
+{
+ _EGLDriver Base; /**< base class */
+};
+
+
+/** driver data of _EGLDisplay */
+struct GLX_egl_display
+{
+ Display *dpy;
+ XVisualInfo *visuals;
+ GLXFBConfig *fbconfigs;
+
+ int glx_maj, glx_min;
+
+ const char *extensions;
+ EGLBoolean have_1_3;
+ EGLBoolean have_make_current_read;
+ EGLBoolean have_fbconfig;
+ EGLBoolean have_pbuffer;
+
+ /* GLX_SGIX_pbuffer */
+ PFNGLXCREATEGLXPBUFFERSGIXPROC glXCreateGLXPbufferSGIX;
+ PFNGLXDESTROYGLXPBUFFERSGIXPROC glXDestroyGLXPbufferSGIX;
+
+ /* workaround quirks of different GLX implementations */
+ EGLBoolean single_buffered_quirk;
+ EGLBoolean glx_window_quirk;
+
+};
+
+
+/** subclass of _EGLContext */
+struct GLX_egl_context
+{
+ _EGLContext Base; /**< base class */
+
+ GLXContext context;
+};
+
+
+/** subclass of _EGLSurface */
+struct GLX_egl_surface
+{
+ _EGLSurface Base; /**< base class */
+
+ Drawable drawable;
+ GLXDrawable glx_drawable;
+
+ void (*destroy)(Display *, GLXDrawable);
+};
+
+
+/** subclass of _EGLConfig */
+struct GLX_egl_config
+{
+ _EGLConfig Base; /**< base class */
+ EGLBoolean double_buffered;
+ int index;
+};
+
+/* standard typecasts */
+_EGL_DRIVER_STANDARD_TYPECASTS(GLX_egl)
+
+static int
+GLX_egl_config_index(_EGLConfig *conf)
+{
+ struct GLX_egl_config *GLX_conf = GLX_egl_config(conf);
+ return GLX_conf->index;
+}
+
+
+#define MAP_ATTRIB(attr, memb) \
+ { attr, offsetof(__GLcontextModes, memb) }
+
+
+static const struct {
+ int attr;
+ int offset;
+} fbconfig_attributes[] = {
+ /* table 3.1 of GLX 1.4 */
+ MAP_ATTRIB(GLX_FBCONFIG_ID, fbconfigID),
+ MAP_ATTRIB(GLX_BUFFER_SIZE, rgbBits),
+ MAP_ATTRIB(GLX_LEVEL, level),
+ MAP_ATTRIB(GLX_DOUBLEBUFFER, doubleBufferMode),
+ MAP_ATTRIB(GLX_STEREO, stereoMode),
+ MAP_ATTRIB(GLX_AUX_BUFFERS, numAuxBuffers),
+ MAP_ATTRIB(GLX_RED_SIZE, redBits),
+ MAP_ATTRIB(GLX_GREEN_SIZE, greenBits),
+ MAP_ATTRIB(GLX_BLUE_SIZE, blueBits),
+ MAP_ATTRIB(GLX_ALPHA_SIZE, alphaBits),
+ MAP_ATTRIB(GLX_DEPTH_SIZE, depthBits),
+ MAP_ATTRIB(GLX_STENCIL_SIZE, stencilBits),
+ MAP_ATTRIB(GLX_ACCUM_RED_SIZE, accumRedBits),
+ MAP_ATTRIB(GLX_ACCUM_GREEN_SIZE, accumGreenBits),
+ MAP_ATTRIB(GLX_ACCUM_BLUE_SIZE, accumBlueBits),
+ MAP_ATTRIB(GLX_ACCUM_ALPHA_SIZE, accumAlphaBits),
+ MAP_ATTRIB(GLX_SAMPLE_BUFFERS, sampleBuffers),
+ MAP_ATTRIB(GLX_SAMPLES, samples),
+ MAP_ATTRIB(GLX_RENDER_TYPE, renderType),
+ MAP_ATTRIB(GLX_DRAWABLE_TYPE, drawableType),
+ MAP_ATTRIB(GLX_X_RENDERABLE, xRenderable),
+ MAP_ATTRIB(GLX_X_VISUAL_TYPE, visualType),
+ MAP_ATTRIB(GLX_CONFIG_CAVEAT, visualRating),
+ MAP_ATTRIB(GLX_TRANSPARENT_TYPE, transparentPixel),
+ MAP_ATTRIB(GLX_TRANSPARENT_INDEX_VALUE, transparentIndex),
+ MAP_ATTRIB(GLX_TRANSPARENT_RED_VALUE, transparentRed),
+ MAP_ATTRIB(GLX_TRANSPARENT_GREEN_VALUE, transparentGreen),
+ MAP_ATTRIB(GLX_TRANSPARENT_BLUE_VALUE, transparentBlue),
+ MAP_ATTRIB(GLX_TRANSPARENT_ALPHA_VALUE, transparentAlpha),
+ MAP_ATTRIB(GLX_MAX_PBUFFER_WIDTH, maxPbufferWidth),
+ MAP_ATTRIB(GLX_MAX_PBUFFER_HEIGHT, maxPbufferHeight),
+ MAP_ATTRIB(GLX_MAX_PBUFFER_PIXELS, maxPbufferPixels),
+ MAP_ATTRIB(GLX_VISUAL_ID, visualID),
+};
+
+
+static EGLBoolean
+convert_fbconfig(Display *dpy, GLXFBConfig fbconfig,
+ struct GLX_egl_config *GLX_conf)
+{
+ __GLcontextModes mode;
+ int err = 0, attr, val, i;
+
+ memset(&mode, 0, sizeof(mode));
+
+ for (i = 0; i < ARRAY_SIZE(fbconfig_attributes); i++) {
+ int offset = fbconfig_attributes[i].offset;
+ attr = fbconfig_attributes[i].attr;
+ err = glXGetFBConfigAttrib(dpy, fbconfig, attr, &val);
+ if (err) {
+ if (err == GLX_BAD_ATTRIBUTE) {
+ err = 0;
+ continue;
+ }
+ break;
+ }
+ *((int *) ((char *) &mode + offset)) = val;
+ }
+ if (err)
+ return EGL_FALSE;
+
+ /* must have rgba bit */
+ if (!(mode.renderType & GLX_RGBA_BIT))
+ return EGL_FALSE;
+
+ /* pixmap and pbuffer surfaces must be single-buffered in EGL */
+ if (mode.doubleBufferMode) {
+ mode.drawableType &= ~(GLX_PIXMAP_BIT | GLX_PBUFFER_BIT);
+ if (!mode.drawableType)
+ return EGL_FALSE;
+ }
+
+ mode.rgbMode = GL_TRUE;
+ mode.haveAccumBuffer = (mode.accumRedBits +
+ mode.accumGreenBits +
+ mode.accumBlueBits +
+ mode.accumAlphaBits > 0);
+ mode.haveDepthBuffer = (mode.depthBits > 0);
+ mode.haveStencilBuffer = (mode.stencilBits > 0);
+
+ GLX_conf->double_buffered = (mode.doubleBufferMode != 0);
+ return _eglConfigFromContextModesRec(&GLX_conf->Base, &mode,
+ EGL_OPENGL_BIT, EGL_OPENGL_BIT);
+}
+
+
+static const struct {
+ int attr;
+ int offset;
+} visual_attributes[] = {
+ /* table 3.7 of GLX 1.4 */
+ /* no GLX_USE_GL */
+ MAP_ATTRIB(GLX_BUFFER_SIZE, rgbBits),
+ MAP_ATTRIB(GLX_LEVEL, level),
+ MAP_ATTRIB(GLX_RGBA, rgbMode),
+ MAP_ATTRIB(GLX_DOUBLEBUFFER, doubleBufferMode),
+ MAP_ATTRIB(GLX_STEREO, stereoMode),
+ MAP_ATTRIB(GLX_AUX_BUFFERS, numAuxBuffers),
+ MAP_ATTRIB(GLX_RED_SIZE, redBits),
+ MAP_ATTRIB(GLX_GREEN_SIZE, greenBits),
+ MAP_ATTRIB(GLX_BLUE_SIZE, blueBits),
+ MAP_ATTRIB(GLX_ALPHA_SIZE, alphaBits),
+ MAP_ATTRIB(GLX_DEPTH_SIZE, depthBits),
+ MAP_ATTRIB(GLX_STENCIL_SIZE, stencilBits),
+ MAP_ATTRIB(GLX_ACCUM_RED_SIZE, accumRedBits),
+ MAP_ATTRIB(GLX_ACCUM_GREEN_SIZE, accumGreenBits),
+ MAP_ATTRIB(GLX_ACCUM_BLUE_SIZE, accumBlueBits),
+ MAP_ATTRIB(GLX_ACCUM_ALPHA_SIZE, accumAlphaBits),
+ MAP_ATTRIB(GLX_SAMPLE_BUFFERS, sampleBuffers),
+ MAP_ATTRIB(GLX_SAMPLES, samples),
+ MAP_ATTRIB(GLX_FBCONFIG_ID, fbconfigID),
+ /* GLX_EXT_visual_rating */
+ MAP_ATTRIB(GLX_VISUAL_CAVEAT_EXT, visualRating),
+};
+
+
+static int
+get_visual_type(const XVisualInfo *vis)
+{
+ int klass;
+
+#if defined(__cplusplus) || defined(c_plusplus)
+ klass = vis->c_class;
+#else
+ klass = vis->class;
+#endif
+
+ switch (klass) {
+ case TrueColor:
+ return GLX_TRUE_COLOR;
+ case DirectColor:
+ return GLX_DIRECT_COLOR;
+ case PseudoColor:
+ return GLX_PSEUDO_COLOR;
+ case StaticColor:
+ return GLX_STATIC_COLOR;
+ case GrayScale:
+ return GLX_GRAY_SCALE;
+ case StaticGray:
+ return GLX_STATIC_GRAY;
+ default:
+ return GLX_NONE;
+ }
+}
+
+
+static EGLBoolean
+convert_visual(Display *dpy, XVisualInfo *vinfo,
+ struct GLX_egl_config *GLX_conf)
+{
+ __GLcontextModes mode;
+ int err, attr, val, i;
+
+ /* the visual must support OpenGL */
+ err = glXGetConfig(dpy, vinfo, GLX_USE_GL, &val);
+ if (err || !val)
+ return EGL_FALSE;
+
+ memset(&mode, 0, sizeof(mode));
+
+ for (i = 0; i < ARRAY_SIZE(visual_attributes); i++) {
+ int offset = visual_attributes[i].offset;
+ attr = visual_attributes[i].attr;
+ err = glXGetConfig(dpy, vinfo, attr, &val);
+ if (err) {
+ if (err == GLX_BAD_ATTRIBUTE) {
+ err = 0;
+ continue;
+ }
+ break;
+ }
+ *((int *) ((char *) &mode + offset)) = val;
+ }
+ if (err)
+ return EGL_FALSE;
+
+ /* must be RGB mode */
+ if (!mode.rgbMode)
+ return EGL_FALSE;
+
+ mode.visualID = vinfo->visualid;
+ mode.visualType = get_visual_type(vinfo);
+ mode.redMask = vinfo->red_mask;
+ mode.greenMask = vinfo->green_mask;
+ mode.blueMask = vinfo->blue_mask;
+
+ mode.drawableType = GLX_WINDOW_BIT;
+ /* pixmap surfaces must be single-buffered in EGL */
+ if (!mode.doubleBufferMode)
+ mode.drawableType |= GLX_PIXMAP_BIT;
+
+ mode.renderType = GLX_RGBA_BIT;
+ mode.xRenderable = GL_TRUE;
+ mode.haveAccumBuffer = (mode.accumRedBits +
+ mode.accumGreenBits +
+ mode.accumBlueBits +
+ mode.accumAlphaBits > 0);
+ mode.haveDepthBuffer = (mode.depthBits > 0);
+ mode.haveStencilBuffer = (mode.stencilBits > 0);
+
+ GLX_conf->double_buffered = (mode.doubleBufferMode != 0);
+ return _eglConfigFromContextModesRec(&GLX_conf->Base, &mode,
+ EGL_OPENGL_BIT, EGL_OPENGL_BIT);
+}
+
+
+static void
+fix_config(struct GLX_egl_display *GLX_dpy, struct GLX_egl_config *GLX_conf)
+{
+ _EGLConfig *conf = &GLX_conf->Base;
+ EGLint surface_type, r, g, b, a;
+
+ surface_type = GET_CONFIG_ATTRIB(conf, EGL_SURFACE_TYPE);
+ if (!GLX_conf->double_buffered && GLX_dpy->single_buffered_quirk) {
+ /* some GLX impls do not like single-buffered window surface */
+ surface_type &= ~EGL_WINDOW_BIT;
+ /* pbuffer bit is usually not set */
+ if (GLX_dpy->have_pbuffer)
+ surface_type |= EGL_PBUFFER_BIT;
+ SET_CONFIG_ATTRIB(conf, EGL_SURFACE_TYPE, surface_type);
+ }
+
+ /* no visual attribs unless window bit is set */
+ if (!(surface_type & EGL_WINDOW_BIT)) {
+ SET_CONFIG_ATTRIB(conf, EGL_NATIVE_VISUAL_ID, 0);
+ SET_CONFIG_ATTRIB(conf, EGL_NATIVE_VISUAL_TYPE, EGL_NONE);
+ }
+
+ /* make sure buffer size is set correctly */
+ r = GET_CONFIG_ATTRIB(conf, EGL_RED_SIZE);
+ g = GET_CONFIG_ATTRIB(conf, EGL_GREEN_SIZE);
+ b = GET_CONFIG_ATTRIB(conf, EGL_BLUE_SIZE);
+ a = GET_CONFIG_ATTRIB(conf, EGL_ALPHA_SIZE);
+ SET_CONFIG_ATTRIB(conf, EGL_BUFFER_SIZE, r + g + b + a);
+}
+
+
+static EGLBoolean
+create_configs(_EGLDisplay *dpy, struct GLX_egl_display *GLX_dpy,
+ EGLint screen)
+{
+ EGLint num_configs = 0, i;
+ EGLint id = 1;
+
+ if (GLX_dpy->have_fbconfig) {
+ GLX_dpy->fbconfigs = glXGetFBConfigs(GLX_dpy->dpy, screen, &num_configs);
+ }
+ else {
+ XVisualInfo vinfo_template;
+ long mask;
+
+ vinfo_template.screen = screen;
+ mask = VisualScreenMask;
+ GLX_dpy->visuals = XGetVisualInfo(GLX_dpy->dpy, mask, &vinfo_template,
+ &num_configs);
+ }
+
+ if (!num_configs)
+ return EGL_FALSE;
+
+ for (i = 0; i < num_configs; i++) {
+ struct GLX_egl_config *GLX_conf, template;
+ EGLBoolean ok;
+
+ memset(&template, 0, sizeof(template));
+ _eglInitConfig(&template.Base, dpy, id);
+ if (GLX_dpy->have_fbconfig)
+ ok = convert_fbconfig(GLX_dpy->dpy, GLX_dpy->fbconfigs[i], &template);
+ else
+ ok = convert_visual(GLX_dpy->dpy, &GLX_dpy->visuals[i], &template);
+ if (!ok)
+ continue;
+
+ fix_config(GLX_dpy, &template);
+ if (!_eglValidateConfig(&template.Base, EGL_FALSE)) {
+ _eglLog(_EGL_DEBUG, "GLX: failed to validate config %d", i);
+ continue;
+ }
+
+ GLX_conf = CALLOC_STRUCT(GLX_egl_config);
+ if (GLX_conf) {
+ memcpy(GLX_conf, &template, sizeof(template));
+ GLX_conf->index = i;
+
+ _eglAddConfig(dpy, &GLX_conf->Base);
+ id++;
+ }
+ }
+
+ return EGL_TRUE;
+}
+
+
+static void
+check_extensions(struct GLX_egl_display *GLX_dpy, EGLint screen)
+{
+ GLX_dpy->extensions =
+ glXQueryExtensionsString(GLX_dpy->dpy, screen);
+ if (GLX_dpy->extensions) {
+ /* glXGetProcAddress is assumed */
+
+ if (strstr(GLX_dpy->extensions, "GLX_SGI_make_current_read")) {
+ /* GLX 1.3 entry points are used */
+ GLX_dpy->have_make_current_read = EGL_TRUE;
+ }
+
+ if (strstr(GLX_dpy->extensions, "GLX_SGIX_fbconfig")) {
+ /* GLX 1.3 entry points are used */
+ GLX_dpy->have_fbconfig = EGL_TRUE;
+ }
+
+ if (strstr(GLX_dpy->extensions, "GLX_SGIX_pbuffer")) {
+ GLX_dpy->glXCreateGLXPbufferSGIX = (PFNGLXCREATEGLXPBUFFERSGIXPROC)
+ glXGetProcAddress((const GLubyte *) "glXCreateGLXPbufferSGIX");
+ GLX_dpy->glXDestroyGLXPbufferSGIX = (PFNGLXDESTROYGLXPBUFFERSGIXPROC)
+ glXGetProcAddress((const GLubyte *) "glXDestroyGLXPbufferSGIX");
+
+ if (GLX_dpy->glXCreateGLXPbufferSGIX &&
+ GLX_dpy->glXDestroyGLXPbufferSGIX &&
+ GLX_dpy->have_fbconfig)
+ GLX_dpy->have_pbuffer = EGL_TRUE;
+ }
+ }
+
+ if (GLX_dpy->glx_maj == 1 && GLX_dpy->glx_min >= 3) {
+ GLX_dpy->have_1_3 = EGL_TRUE;
+ GLX_dpy->have_make_current_read = EGL_TRUE;
+ GLX_dpy->have_fbconfig = EGL_TRUE;
+ GLX_dpy->have_pbuffer = EGL_TRUE;
+ }
+}
+
+
+static void
+check_quirks(struct GLX_egl_display *GLX_dpy, EGLint screen)
+{
+ const char *vendor;
+
+ GLX_dpy->single_buffered_quirk = EGL_TRUE;
+ GLX_dpy->glx_window_quirk = EGL_TRUE;
+
+ vendor = glXGetClientString(GLX_dpy->dpy, GLX_VENDOR);
+ if (vendor && strstr(vendor, "NVIDIA")) {
+ vendor = glXQueryServerString(GLX_dpy->dpy, screen, GLX_VENDOR);
+ if (vendor && strstr(vendor, "NVIDIA")) {
+ _eglLog(_EGL_DEBUG, "disable quirks");
+ GLX_dpy->single_buffered_quirk = EGL_FALSE;
+ GLX_dpy->glx_window_quirk = EGL_FALSE;
+ }
+ }
+}
+
+
+/**
+ * Called via eglInitialize(), GLX_drv->API.Initialize().
+ */
+static EGLBoolean
+GLX_eglInitialize(_EGLDriver *drv, _EGLDisplay *disp,
+ EGLint *major, EGLint *minor)
+{
+ struct GLX_egl_display *GLX_dpy;
+
+ GLX_dpy = CALLOC_STRUCT(GLX_egl_display);
+ if (!GLX_dpy)
+ return _eglError(EGL_BAD_ALLOC, "eglInitialize");
+
+ GLX_dpy->dpy = (Display *) disp->NativeDisplay;
+ if (!GLX_dpy->dpy) {
+ GLX_dpy->dpy = XOpenDisplay(NULL);
+ if (!GLX_dpy->dpy) {
+ _eglLog(_EGL_WARNING, "GLX: XOpenDisplay failed");
+ free(GLX_dpy);
+ return EGL_FALSE;
+ }
+ }
+
+ if (!glXQueryVersion(GLX_dpy->dpy, &GLX_dpy->glx_maj, &GLX_dpy->glx_min)) {
+ _eglLog(_EGL_WARNING, "GLX: glXQueryVersion failed");
+ if (!disp->NativeDisplay)
+ XCloseDisplay(GLX_dpy->dpy);
+ free(GLX_dpy);
+ return EGL_FALSE;
+ }
+
+ check_extensions(GLX_dpy, DefaultScreen(GLX_dpy->dpy));
+ check_quirks(GLX_dpy, DefaultScreen(GLX_dpy->dpy));
+
+ create_configs(disp, GLX_dpy, DefaultScreen(GLX_dpy->dpy));
+ if (!disp->NumConfigs) {
+ _eglLog(_EGL_WARNING, "GLX: failed to create any config");
+ if (!disp->NativeDisplay)
+ XCloseDisplay(GLX_dpy->dpy);
+ free(GLX_dpy);
+ return EGL_FALSE;
+ }
+
+ disp->DriverData = (void *) GLX_dpy;
+ disp->ClientAPIsMask = EGL_OPENGL_BIT;
+
+ /* we're supporting EGL 1.4 */
+ *major = 1;
+ *minor = 4;
+
+ return EGL_TRUE;
+}
+
+/**
+ * Called via eglTerminate(), drv->API.Terminate().
+ */
+static EGLBoolean
+GLX_eglTerminate(_EGLDriver *drv, _EGLDisplay *disp)
+{
+ struct GLX_egl_display *GLX_dpy = GLX_egl_display(disp);
+
+ _eglReleaseDisplayResources(drv, disp);
+ _eglCleanupDisplay(disp);
+
+ if (GLX_dpy->visuals)
+ XFree(GLX_dpy->visuals);
+ if (GLX_dpy->fbconfigs)
+ XFree(GLX_dpy->fbconfigs);
+
+ if (!disp->NativeDisplay)
+ XCloseDisplay(GLX_dpy->dpy);
+ free(GLX_dpy);
+
+ disp->DriverData = NULL;
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Called via eglCreateContext(), drv->API.CreateContext().
+ */
+static _EGLContext *
+GLX_eglCreateContext(_EGLDriver *drv, _EGLDisplay *disp, _EGLConfig *conf,
+ _EGLContext *share_list, const EGLint *attrib_list)
+{
+ struct GLX_egl_context *GLX_ctx = CALLOC_STRUCT(GLX_egl_context);
+ struct GLX_egl_display *GLX_dpy = GLX_egl_display(disp);
+ struct GLX_egl_context *GLX_ctx_shared = GLX_egl_context(share_list);
+
+ if (!GLX_ctx) {
+ _eglError(EGL_BAD_ALLOC, "eglCreateContext");
+ return NULL;
+ }
+
+ if (!_eglInitContext(&GLX_ctx->Base, disp, conf, attrib_list)) {
+ free(GLX_ctx);
+ return NULL;
+ }
+
+ if (GLX_dpy->have_fbconfig)
+ GLX_ctx->context =
+ glXCreateNewContext(GLX_dpy->dpy,
+ GLX_dpy->fbconfigs[GLX_egl_config_index(conf)],
+ GLX_RGBA_TYPE,
+ GLX_ctx_shared ? GLX_ctx_shared->context : NULL,
+ GL_TRUE);
+ else
+ GLX_ctx->context =
+ glXCreateContext(GLX_dpy->dpy,
+ &GLX_dpy->visuals[GLX_egl_config_index(conf)],
+ GLX_ctx_shared ? GLX_ctx_shared->context : NULL,
+ GL_TRUE);
+ if (!GLX_ctx->context) {
+ free(GLX_ctx);
+ return NULL;
+ }
+
+ return &GLX_ctx->Base;
+}
+
+
+/**
+ * Destroy a surface. The display is allowed to be uninitialized.
+ */
+static void
+destroy_surface(_EGLDisplay *disp, _EGLSurface *surf)
+{
+ struct GLX_egl_surface *GLX_surf = GLX_egl_surface(surf);
+
+ if (GLX_surf->destroy)
+ GLX_surf->destroy(disp->NativeDisplay, GLX_surf->glx_drawable);
+
+ free(GLX_surf);
+}
+
+
+/**
+ * Called via eglMakeCurrent(), drv->API.MakeCurrent().
+ */
+static EGLBoolean
+GLX_eglMakeCurrent(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *dsurf,
+ _EGLSurface *rsurf, _EGLContext *ctx)
+{
+ struct GLX_egl_display *GLX_dpy = GLX_egl_display(disp);
+ struct GLX_egl_surface *GLX_dsurf = GLX_egl_surface(dsurf);
+ struct GLX_egl_surface *GLX_rsurf = GLX_egl_surface(rsurf);
+ struct GLX_egl_context *GLX_ctx = GLX_egl_context(ctx);
+ GLXDrawable ddraw, rdraw;
+ GLXContext cctx;
+ EGLBoolean ret = EGL_FALSE;
+
+ /* bind the new context and return the "orphaned" one */
+ if (!_eglBindContext(&ctx, &dsurf, &rsurf))
+ return EGL_FALSE;
+
+ ddraw = (GLX_dsurf) ? GLX_dsurf->glx_drawable : None;
+ rdraw = (GLX_rsurf) ? GLX_rsurf->glx_drawable : None;
+ cctx = (GLX_ctx) ? GLX_ctx->context : NULL;
+
+ if (GLX_dpy->have_make_current_read)
+ ret = glXMakeContextCurrent(GLX_dpy->dpy, ddraw, rdraw, cctx);
+ else if (ddraw == rdraw)
+ ret = glXMakeCurrent(GLX_dpy->dpy, ddraw, cctx);
+
+ if (ret) {
+ if (dsurf && !_eglIsSurfaceLinked(dsurf))
+ destroy_surface(disp, dsurf);
+ if (rsurf && rsurf != dsurf && !_eglIsSurfaceLinked(rsurf))
+ destroy_surface(disp, rsurf);
+ }
+ else {
+ _eglBindContext(&ctx, &dsurf, &rsurf);
+ }
+
+ return ret;
+}
+
+/** Get size of given window */
+static Status
+get_drawable_size(Display *dpy, Drawable d, uint *width, uint *height)
+{
+ Window root;
+ Status stat;
+ int xpos, ypos;
+ unsigned int w, h, bw, depth;
+ stat = XGetGeometry(dpy, d, &root, &xpos, &ypos, &w, &h, &bw, &depth);
+ *width = w;
+ *height = h;
+ return stat;
+}
+
+/**
+ * Called via eglCreateWindowSurface(), drv->API.CreateWindowSurface().
+ */
+static _EGLSurface *
+GLX_eglCreateWindowSurface(_EGLDriver *drv, _EGLDisplay *disp,
+ _EGLConfig *conf, EGLNativeWindowType window,
+ const EGLint *attrib_list)
+{
+ struct GLX_egl_display *GLX_dpy = GLX_egl_display(disp);
+ struct GLX_egl_surface *GLX_surf;
+ uint width, height;
+
+ GLX_surf = CALLOC_STRUCT(GLX_egl_surface);
+ if (!GLX_surf) {
+ _eglError(EGL_BAD_ALLOC, "eglCreateWindowSurface");
+ return NULL;
+ }
+
+ if (!_eglInitSurface(&GLX_surf->Base, disp, EGL_WINDOW_BIT,
+ conf, attrib_list)) {
+ free(GLX_surf);
+ return NULL;
+ }
+
+ GLX_surf->drawable = window;
+
+ if (GLX_dpy->have_1_3 && !GLX_dpy->glx_window_quirk)
+ GLX_surf->glx_drawable =
+ glXCreateWindow(GLX_dpy->dpy,
+ GLX_dpy->fbconfigs[GLX_egl_config_index(conf)],
+ GLX_surf->drawable, NULL);
+ else
+ GLX_surf->glx_drawable = GLX_surf->drawable;
+
+ if (!GLX_surf->glx_drawable) {
+ free(GLX_surf);
+ return NULL;
+ }
+
+ if (GLX_dpy->have_1_3 && !GLX_dpy->glx_window_quirk)
+ GLX_surf->destroy = glXDestroyWindow;
+
+ get_drawable_size(GLX_dpy->dpy, window, &width, &height);
+ GLX_surf->Base.Width = width;
+ GLX_surf->Base.Height = height;
+
+ return &GLX_surf->Base;
+}
+
+static _EGLSurface *
+GLX_eglCreatePixmapSurface(_EGLDriver *drv, _EGLDisplay *disp,
+ _EGLConfig *conf, EGLNativePixmapType pixmap,
+ const EGLint *attrib_list)
+{
+ struct GLX_egl_display *GLX_dpy = GLX_egl_display(disp);
+ struct GLX_egl_surface *GLX_surf;
+ uint width, height;
+
+ GLX_surf = CALLOC_STRUCT(GLX_egl_surface);
+ if (!GLX_surf) {
+ _eglError(EGL_BAD_ALLOC, "eglCreatePixmapSurface");
+ return NULL;
+ }
+
+ if (!_eglInitSurface(&GLX_surf->Base, disp, EGL_PIXMAP_BIT,
+ conf, attrib_list)) {
+ free(GLX_surf);
+ return NULL;
+ }
+
+ GLX_surf->drawable = pixmap;
+
+ if (GLX_dpy->have_1_3) {
+ GLX_surf->glx_drawable =
+ glXCreatePixmap(GLX_dpy->dpy,
+ GLX_dpy->fbconfigs[GLX_egl_config_index(conf)],
+ GLX_surf->drawable, NULL);
+ }
+ else if (GLX_dpy->have_fbconfig) {
+ GLXFBConfig fbconfig = GLX_dpy->fbconfigs[GLX_egl_config_index(conf)];
+ XVisualInfo *vinfo = glXGetVisualFromFBConfig(GLX_dpy->dpy, fbconfig);
+ if (vinfo) {
+ GLX_surf->glx_drawable =
+ glXCreateGLXPixmap(GLX_dpy->dpy, vinfo, GLX_surf->drawable);
+ XFree(vinfo);
+ }
+ }
+ else {
+ GLX_surf->glx_drawable =
+ glXCreateGLXPixmap(GLX_dpy->dpy,
+ &GLX_dpy->visuals[GLX_egl_config_index(conf)],
+ GLX_surf->drawable);
+ }
+
+ if (!GLX_surf->glx_drawable) {
+ free(GLX_surf);
+ return NULL;
+ }
+
+ GLX_surf->destroy = (GLX_dpy->have_1_3) ?
+ glXDestroyPixmap : glXDestroyGLXPixmap;
+
+ get_drawable_size(GLX_dpy->dpy, pixmap, &width, &height);
+ GLX_surf->Base.Width = width;
+ GLX_surf->Base.Height = height;
+
+ return &GLX_surf->Base;
+}
+
+static _EGLSurface *
+GLX_eglCreatePbufferSurface(_EGLDriver *drv, _EGLDisplay *disp,
+ _EGLConfig *conf, const EGLint *attrib_list)
+{
+ struct GLX_egl_display *GLX_dpy = GLX_egl_display(disp);
+ struct GLX_egl_surface *GLX_surf;
+ int attribs[5];
+ int i;
+
+ GLX_surf = CALLOC_STRUCT(GLX_egl_surface);
+ if (!GLX_surf) {
+ _eglError(EGL_BAD_ALLOC, "eglCreatePbufferSurface");
+ return NULL;
+ }
+
+ if (!_eglInitSurface(&GLX_surf->Base, disp, EGL_PBUFFER_BIT,
+ conf, attrib_list)) {
+ free(GLX_surf);
+ return NULL;
+ }
+
+ i = 0;
+ attribs[i] = None;
+
+ GLX_surf->drawable = None;
+
+ if (GLX_dpy->have_1_3) {
+ /* put geometry in attribs */
+ if (GLX_surf->Base.Width) {
+ attribs[i++] = GLX_PBUFFER_WIDTH;
+ attribs[i++] = GLX_surf->Base.Width;
+ }
+ if (GLX_surf->Base.Height) {
+ attribs[i++] = GLX_PBUFFER_HEIGHT;
+ attribs[i++] = GLX_surf->Base.Height;
+ }
+ attribs[i] = None;
+
+ GLX_surf->glx_drawable =
+ glXCreatePbuffer(GLX_dpy->dpy,
+ GLX_dpy->fbconfigs[GLX_egl_config_index(conf)],
+ attribs);
+ }
+ else if (GLX_dpy->have_pbuffer) {
+ GLX_surf->glx_drawable = GLX_dpy->glXCreateGLXPbufferSGIX(
+ GLX_dpy->dpy,
+ GLX_dpy->fbconfigs[GLX_egl_config_index(conf)],
+ GLX_surf->Base.Width,
+ GLX_surf->Base.Height,
+ attribs);
+ }
+
+ if (!GLX_surf->glx_drawable) {
+ free(GLX_surf);
+ return NULL;
+ }
+
+ GLX_surf->destroy = (GLX_dpy->have_1_3) ?
+ glXDestroyPbuffer : GLX_dpy->glXDestroyGLXPbufferSGIX;
+
+ return &GLX_surf->Base;
+}
+
+
+static EGLBoolean
+GLX_eglDestroySurface(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf)
+{
+ if (!_eglIsSurfaceBound(surf))
+ destroy_surface(disp, surf);
+
+ return EGL_TRUE;
+}
+
+
+static EGLBoolean
+GLX_eglSwapBuffers(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *draw)
+{
+ struct GLX_egl_display *GLX_dpy = GLX_egl_display(disp);
+ struct GLX_egl_surface *GLX_surf = GLX_egl_surface(draw);
+
+ glXSwapBuffers(GLX_dpy->dpy, GLX_surf->glx_drawable);
+
+ return EGL_TRUE;
+}
+
+/*
+ * Called from eglGetProcAddress() via drv->API.GetProcAddress().
+ */
+static _EGLProc
+GLX_eglGetProcAddress(_EGLDriver *drv, const char *procname)
+{
+ return (_EGLProc) glXGetProcAddress((const GLubyte *) procname);
+}
+
+static EGLBoolean
+GLX_eglWaitClient(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx)
+{
+ glXWaitGL();
+ return EGL_TRUE;
+}
+
+static EGLBoolean
+GLX_eglWaitNative(_EGLDriver *drv, _EGLDisplay *dpy, EGLint engine)
+{
+ if (engine != EGL_CORE_NATIVE_ENGINE)
+ return _eglError(EGL_BAD_PARAMETER, "eglWaitNative");
+ glXWaitX();
+ return EGL_TRUE;
+}
+
+static void
+GLX_Unload(_EGLDriver *drv)
+{
+ struct GLX_egl_driver *GLX_drv = GLX_egl_driver(drv);
+ free(GLX_drv);
+}
+
+
+/**
+ * This is the main entrypoint into the driver, called by libEGL.
+ * Create a new _EGLDriver object and init its dispatch table.
+ */
+_EGLDriver *
+_eglMain(const char *args)
+{
+ struct GLX_egl_driver *GLX_drv = CALLOC_STRUCT(GLX_egl_driver);
+
+ if (!GLX_drv)
+ return NULL;
+
+ _eglInitDriverFallbacks(&GLX_drv->Base);
+ GLX_drv->Base.API.Initialize = GLX_eglInitialize;
+ GLX_drv->Base.API.Terminate = GLX_eglTerminate;
+ GLX_drv->Base.API.CreateContext = GLX_eglCreateContext;
+ GLX_drv->Base.API.MakeCurrent = GLX_eglMakeCurrent;
+ GLX_drv->Base.API.CreateWindowSurface = GLX_eglCreateWindowSurface;
+ GLX_drv->Base.API.CreatePixmapSurface = GLX_eglCreatePixmapSurface;
+ GLX_drv->Base.API.CreatePbufferSurface = GLX_eglCreatePbufferSurface;
+ GLX_drv->Base.API.DestroySurface = GLX_eglDestroySurface;
+ GLX_drv->Base.API.SwapBuffers = GLX_eglSwapBuffers;
+ GLX_drv->Base.API.GetProcAddress = GLX_eglGetProcAddress;
+ GLX_drv->Base.API.WaitClient = GLX_eglWaitClient;
+ GLX_drv->Base.API.WaitNative = GLX_eglWaitNative;
+
+ GLX_drv->Base.Name = "GLX";
+ GLX_drv->Base.Unload = GLX_Unload;
+
+ return &GLX_drv->Base;
+}
diff --git a/src/egl/main/Makefile b/src/egl/main/Makefile
new file mode 100644
index 0000000000..d9e9e4fcd6
--- /dev/null
+++ b/src/egl/main/Makefile
@@ -0,0 +1,121 @@
+# src/egl/main/Makefile
+
+TOP = ../../..
+include $(TOP)/configs/current
+
+
+EGL_MAJOR = 1
+EGL_MINOR = 0
+
+INCLUDE_DIRS = -I$(TOP)/include
+
+HEADERS = \
+ eglcompiler.h \
+ eglconfig.h \
+ eglconfigutil.h \
+ eglcontext.h \
+ eglcurrent.h \
+ egldefines.h \
+ egldisplay.h \
+ egldriver.h \
+ eglglobals.h \
+ eglimage.h \
+ egllog.h \
+ eglmisc.h \
+ eglmode.h \
+ eglmutex.h \
+ eglscreen.h \
+ eglstring.h \
+ eglsurface.h
+
+SOURCES = \
+ eglapi.c \
+ eglconfig.c \
+ eglconfigutil.c \
+ eglcontext.c \
+ eglcurrent.c \
+ egldisplay.c \
+ egldriver.c \
+ eglglobals.c \
+ eglimage.c \
+ egllog.c \
+ eglmisc.c \
+ eglmode.c \
+ eglscreen.c \
+ eglstring.c \
+ eglsurface.c
+
+OBJECTS = $(SOURCES:.c=.o)
+
+
+# use dl*() to load drivers
+LOCAL_CFLAGS = -D_EGL_OS_UNIX=1
+
+EGL_DEFAULT_PLATFORM = $(firstword $(EGL_PLATFORMS))
+
+LOCAL_CFLAGS += \
+ -D_EGL_DEFAULT_PLATFORM=\"$(EGL_DEFAULT_PLATFORM)\" \
+ -D_EGL_DRIVER_SEARCH_DIR=\"$(EGL_DRIVER_INSTALL_DIR)\"
+
+.c.o:
+ $(CC) -c $(INCLUDE_DIRS) $(CFLAGS) $(LOCAL_CFLAGS) $< -o $@
+
+
+
+default: depend library
+
+
+# EGL Library
+library: $(TOP)/$(LIB_DIR)/$(EGL_LIB_NAME)
+
+$(TOP)/$(LIB_DIR)/$(EGL_LIB_NAME): $(OBJECTS)
+ $(MKLIB) -o $(EGL_LIB) -linker '$(CC)' -ldflags '$(LDFLAGS)' \
+ -major $(EGL_MAJOR) -minor $(EGL_MINOR) \
+ -install $(TOP)/$(LIB_DIR) $(MKLIB_OPTIONS) \
+ $(EGL_LIB_DEPS) $(OBJECTS)
+
+install-headers:
+ $(INSTALL) -d $(DESTDIR)$(INSTALL_INC_DIR)/KHR
+ $(INSTALL) -m 644 $(TOP)/include/KHR/*.h \
+ $(DESTDIR)$(INSTALL_INC_DIR)/KHR
+ $(INSTALL) -d $(DESTDIR)$(INSTALL_INC_DIR)/EGL
+ $(INSTALL) -m 644 $(TOP)/include/EGL/*.h \
+ $(DESTDIR)$(INSTALL_INC_DIR)/EGL
+
+PKG_CONFIG_DIR = $(INSTALL_LIB_DIR)/pkgconfig
+
+gl_pcedit = sed \
+ -e 's,@INSTALL_DIR@,$(INSTALL_DIR),' \
+ -e 's,@INSTALL_LIB_DIR@,$(INSTALL_LIB_DIR),' \
+ -e 's,@INSTALL_INC_DIR@,$(INSTALL_INC_DIR),' \
+ -e 's,@VERSION@,$(MESA_MAJOR).$(MESA_MINOR).$(MESA_TINY),' \
+ -e 's,@EGL_PC_REQ_PRIV@,$(EGL_PC_REQ_PRIV),' \
+ -e 's,@EGL_PC_LIB_PRIV@,$(EGL_PC_LIB_PRIV),' \
+ -e 's,@EGL_PC_CFLAGS@,$(EGL_PC_CFLAGS),' \
+ -e 's,@EGL_LIB@,$(EGL_LIB),'
+
+egl.pc: egl.pc.in
+ $(gl_pcedit) $< > $@
+
+install: default install-headers egl.pc
+ $(INSTALL) -d $(DESTDIR)$(INSTALL_LIB_DIR)
+ $(MINSTALL) $(TOP)/$(LIB_DIR)/$(EGL_LIB_GLOB) \
+ $(DESTDIR)$(INSTALL_LIB_DIR)
+ $(INSTALL) -d $(DESTDIR)$(PKG_CONFIG_DIR)
+ $(INSTALL) -m 644 egl.pc $(DESTDIR)$(PKG_CONFIG_DIR)
+
+clean:
+ -rm -f *.o
+ -rm -f depend depend.bak
+
+
+depend: $(SOURCES) $(HEADERS)
+ @ echo "running $(MKDEP)"
+ @ rm -f depend
+ @ touch depend
+ $(MKDEP) $(MKDEP_OPTIONS) $(DEFINES) $(INCLUDE_DIRS) \
+ $(SOURCES) $(HEADERS) > /dev/null 2>/dev/null
+
+
+-include depend
+# DO NOT DELETE
diff --git a/src/egl/main/README.txt b/src/egl/main/README.txt
new file mode 100644
index 0000000000..b3d253dd13
--- /dev/null
+++ b/src/egl/main/README.txt
@@ -0,0 +1,71 @@
+
+
+Notes about the EGL library:
+
+
+The EGL code here basically consists of two things:
+
+1. An EGL API dispatcher. This directly routes all the eglFooBar() API
+ calls into driver-specific functions.
+
+2. Fallbacks for EGL API functions. A driver _could_ implement all the
+ EGL API calls from scratch. But in many cases, the fallbacks provided
+ in libEGL (such as eglChooseConfig()) will do the job.
+
+
+
+Bootstrapping:
+
+When the apps calls eglOpenDisplay() a device driver is selected and loaded
+(look for dlsym() or LoadLibrary() in egldriver.c).
+
+The driver's _eglMain() function is then called. This driver function
+allocates, initializes and returns a new _EGLDriver object (usually a
+subclass of that type).
+
+As part of initialization, the dispatch table in _EGLDriver->API must be
+populated with all the EGL entrypoints. Typically, _eglInitDriverFallbacks()
+can be used to plug in default/fallback functions. Some functions like
+driver->API.Initialize and driver->API.Terminate _must_ be implemented
+with driver-specific code (no default/fallback function is possible).
+
+
+A bit later, the app will call eglInitialize(). This will get routed
+to the driver->API.Initialize() function. Any additional driver
+initialization that wasn't done in _eglMain() should be done at this
+point. Typically, this will involve setting up visual configs, etc.
+
+
+
+Special Functions:
+
+Certain EGL functions _must_ be implemented by the driver. This includes:
+
+eglCreateContext
+eglCreateWindowSurface
+eglCreatePixmapSurface
+eglCreatePBufferSurface
+eglMakeCurrent
+eglSwapBuffers
+
+Most of the EGLConfig-related functions can be implemented with the
+defaults/fallbacks. Same thing for the eglGet/Query functions.
+
+
+
+
+Teardown:
+
+When eglTerminate() is called, the driver->API.Terminate() function is
+called. The driver should clean up after itself. eglTerminate() will
+then close/unload the driver (shared library).
+
+
+
+
+Subclassing:
+
+The internal libEGL data structures such as _EGLDisplay, _EGLContext,
+_EGLSurface, etc should be considered base classes from which drivers
+will derive subclasses.
+
diff --git a/src/egl/main/SConscript b/src/egl/main/SConscript
new file mode 100644
index 0000000000..a331c9711e
--- /dev/null
+++ b/src/egl/main/SConscript
@@ -0,0 +1,49 @@
+#######################################################################
+# SConscript for EGL
+
+
+Import('*')
+
+if env['platform'] != 'winddk':
+
+ env = env.Clone()
+
+ env.Append(CPPDEFINES = [
+ '_EGL_DEFAULT_DISPLAY=\\"gdi\\"',
+ '_EGL_DRIVER_SEARCH_DIR=\\"\\"',
+ '_EGL_OS_WINDOWS',
+ 'KHRONOS_DLL_EXPORTS',
+ ])
+
+ env.Append(CPPPATH = [
+ '#/include',
+ ])
+
+ egl_sources = [
+ 'eglapi.c',
+ 'eglconfig.c',
+ 'eglconfigutil.c',
+ 'eglcontext.c',
+ 'eglcurrent.c',
+ 'egldisplay.c',
+ 'egldriver.c',
+ 'eglglobals.c',
+ 'eglimage.c',
+ 'egllog.c',
+ 'eglmisc.c',
+ 'eglmode.c',
+ 'eglscreen.c',
+ 'eglstring.c',
+ 'eglsurface.c',
+ ]
+
+ egl = env.SharedLibrary(
+ target = 'libEGL',
+ source = egl_sources,
+ )
+
+ env.InstallSharedLibrary(egl, version=(1, 4, 0))
+
+ egl = [env.FindIxes(egl, 'LIBPREFIX', 'LIBSUFFIX')]
+
+ Export('egl')
diff --git a/src/egl/main/egl.pc.in b/src/egl/main/egl.pc.in
new file mode 100644
index 0000000000..2855a4980a
--- /dev/null
+++ b/src/egl/main/egl.pc.in
@@ -0,0 +1,12 @@
+prefix=@INSTALL_DIR@
+exec_prefix=${prefix}
+libdir=@INSTALL_LIB_DIR@
+includedir=@INSTALL_INC_DIR@
+
+Name: egl
+Description: Mesa EGL library
+Requires.private: @EGL_PC_REQ_PRIV@
+Version: @VERSION@
+Libs: -L${libdir} -l@EGL_LIB@
+Libs.private: @EGL_PC_LIB_PRIV@
+Cflags: -I${includedir} @EGL_PC_CFLAGS@
diff --git a/src/egl/main/eglapi.c b/src/egl/main/eglapi.c
new file mode 100644
index 0000000000..9912043e06
--- /dev/null
+++ b/src/egl/main/eglapi.c
@@ -0,0 +1,1282 @@
+/**
+ * Public EGL API entrypoints
+ *
+ * Generally, we use the EGLDisplay parameter as a key to lookup the
+ * appropriate device driver handle, then jump though the driver's
+ * dispatch table to handle the function.
+ *
+ * That allows us the option of supporting multiple, simultaneous,
+ * heterogeneous hardware devices in the future.
+ *
+ * The EGLDisplay, EGLConfig, EGLContext and EGLSurface types are
+ * opaque handles. Internal objects are linked to a display to
+ * create the handles.
+ *
+ * For each public API entry point, the opaque handles are looked up
+ * before being dispatched to the drivers. When it fails to look up
+ * a handle, one of
+ *
+ * EGL_BAD_DISPLAY
+ * EGL_BAD_CONFIG
+ * EGL_BAD_CONTEXT
+ * EGL_BAD_SURFACE
+ * EGL_BAD_SCREEN_MESA
+ * EGL_BAD_MODE_MESA
+ *
+ * is generated and the driver function is not called. An
+ * uninitialized EGLDisplay has no driver associated with it. When
+ * such display is detected,
+ *
+ * EGL_NOT_INITIALIZED
+ *
+ * is generated.
+ *
+ * Some of the entry points use current display, context, or surface
+ * implicitly. For such entry points, the implicit objects are also
+ * checked before calling the driver function. Other than the
+ * errors listed above,
+ *
+ * EGL_BAD_CURRENT_SURFACE
+ *
+ * may also be generated.
+ *
+ * Notes on naming conventions:
+ *
+ * eglFooBar - public EGL function
+ * EGL_FOO_BAR - public EGL token
+ * EGLDatatype - public EGL datatype
+ *
+ * _eglFooBar - private EGL function
+ * _EGLDatatype - private EGL datatype, typedef'd struct
+ * _egl_struct - private EGL struct, non-typedef'd
+ *
+ */
+
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "eglstring.h"
+#include "eglcontext.h"
+#include "egldisplay.h"
+#include "egltypedefs.h"
+#include "eglglobals.h"
+#include "eglcurrent.h"
+#include "egldriver.h"
+#include "eglsurface.h"
+#include "eglconfig.h"
+#include "eglscreen.h"
+#include "eglmode.h"
+#include "eglimage.h"
+
+
+/**
+ * Macros to help return an API entrypoint.
+ *
+ * These macros will unlock the display and record the error code.
+ */
+#define RETURN_EGL_ERROR(disp, err, ret) \
+ do { \
+ if (disp) \
+ _eglUnlockDisplay(disp); \
+ /* EGL error codes are non-zero */ \
+ if (err) \
+ _eglError(err, __FUNCTION__); \
+ return ret; \
+ } while (0)
+
+#define RETURN_EGL_SUCCESS(disp, ret) \
+ RETURN_EGL_ERROR(disp, EGL_SUCCESS, ret)
+
+/* record EGL_SUCCESS only when ret evaluates to true */
+#define RETURN_EGL_EVAL(disp, ret) \
+ RETURN_EGL_ERROR(disp, (ret) ? EGL_SUCCESS : 0, ret)
+
+
+/*
+ * A bunch of macros and checks to simplify error checking.
+ */
+
+#define _EGL_CHECK_DISPLAY(disp, ret, drv) \
+ do { \
+ drv = _eglCheckDisplay(disp, __FUNCTION__); \
+ if (!drv) \
+ RETURN_EGL_ERROR(disp, 0, ret); \
+ } while (0)
+
+#define _EGL_CHECK_OBJECT(disp, type, obj, ret, drv) \
+ do { \
+ drv = _eglCheck ## type(disp, obj, __FUNCTION__); \
+ if (!drv) \
+ RETURN_EGL_ERROR(disp, 0, ret); \
+ } while (0)
+
+#define _EGL_CHECK_SURFACE(disp, surf, ret, drv) \
+ _EGL_CHECK_OBJECT(disp, Surface, surf, ret, drv)
+
+#define _EGL_CHECK_CONTEXT(disp, context, ret, drv) \
+ _EGL_CHECK_OBJECT(disp, Context, context, ret, drv)
+
+#define _EGL_CHECK_CONFIG(disp, conf, ret, drv) \
+ _EGL_CHECK_OBJECT(disp, Config, conf, ret, drv)
+
+#define _EGL_CHECK_SCREEN(disp, scrn, ret, drv) \
+ _EGL_CHECK_OBJECT(disp, Screen, scrn, ret, drv)
+
+#define _EGL_CHECK_MODE(disp, m, ret, drv) \
+ _EGL_CHECK_OBJECT(disp, Mode, m, ret, drv)
+
+
+
+static INLINE _EGLDriver *
+_eglCheckDisplay(_EGLDisplay *disp, const char *msg)
+{
+ if (!disp) {
+ _eglError(EGL_BAD_DISPLAY, msg);
+ return NULL;
+ }
+ if (!disp->Initialized) {
+ _eglError(EGL_NOT_INITIALIZED, msg);
+ return NULL;
+ }
+ return disp->Driver;
+}
+
+
+static INLINE _EGLDriver *
+_eglCheckSurface(_EGLDisplay *disp, _EGLSurface *surf, const char *msg)
+{
+ _EGLDriver *drv = _eglCheckDisplay(disp, msg);
+ if (!drv)
+ return NULL;
+ if (!surf) {
+ _eglError(EGL_BAD_SURFACE, msg);
+ return NULL;
+ }
+ return drv;
+}
+
+
+static INLINE _EGLDriver *
+_eglCheckContext(_EGLDisplay *disp, _EGLContext *context, const char *msg)
+{
+ _EGLDriver *drv = _eglCheckDisplay(disp, msg);
+ if (!drv)
+ return NULL;
+ if (!context) {
+ _eglError(EGL_BAD_CONTEXT, msg);
+ return NULL;
+ }
+ return drv;
+}
+
+
+static INLINE _EGLDriver *
+_eglCheckConfig(_EGLDisplay *disp, _EGLConfig *conf, const char *msg)
+{
+ _EGLDriver *drv = _eglCheckDisplay(disp, msg);
+ if (!drv)
+ return NULL;
+ if (!conf) {
+ _eglError(EGL_BAD_CONFIG, msg);
+ return NULL;
+ }
+ return drv;
+}
+
+
+#ifdef EGL_MESA_screen_surface
+
+
+static INLINE _EGLDriver *
+_eglCheckScreen(_EGLDisplay *disp, _EGLScreen *scrn, const char *msg)
+{
+ _EGLDriver *drv = _eglCheckDisplay(disp, msg);
+ if (!drv)
+ return NULL;
+ if (!scrn) {
+ _eglError(EGL_BAD_SCREEN_MESA, msg);
+ return NULL;
+ }
+ return drv;
+}
+
+
+static INLINE _EGLDriver *
+_eglCheckMode(_EGLDisplay *disp, _EGLMode *m, const char *msg)
+{
+ _EGLDriver *drv = _eglCheckDisplay(disp, msg);
+ if (!drv)
+ return NULL;
+ if (!m) {
+ _eglError(EGL_BAD_MODE_MESA, msg);
+ return NULL;
+ }
+ return drv;
+}
+
+
+#endif /* EGL_MESA_screen_surface */
+
+
+/**
+ * Lookup and lock a display.
+ */
+static INLINE _EGLDisplay *
+_eglLockDisplay(EGLDisplay display)
+{
+ _EGLDisplay *dpy = _eglLookupDisplay(display);
+ if (dpy)
+ _eglLockMutex(&dpy->Mutex);
+ return dpy;
+}
+
+
+/**
+ * Unlock a display.
+ */
+static INLINE void
+_eglUnlockDisplay(_EGLDisplay *dpy)
+{
+ _eglUnlockMutex(&dpy->Mutex);
+}
+
+
+/**
+ * This is typically the first EGL function that an application calls.
+ * It associates a private _EGLDisplay object to the native display.
+ */
+EGLDisplay EGLAPIENTRY
+eglGetDisplay(EGLNativeDisplayType nativeDisplay)
+{
+ _EGLDisplay *dpy = _eglFindDisplay(nativeDisplay);
+ return _eglGetDisplayHandle(dpy);
+}
+
+
+/**
+ * This is typically the second EGL function that an application calls.
+ * Here we load/initialize the actual hardware driver.
+ */
+EGLBoolean EGLAPIENTRY
+eglInitialize(EGLDisplay dpy, EGLint *major, EGLint *minor)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ EGLint major_int = 0, minor_int = 0;
+
+ if (!disp)
+ RETURN_EGL_ERROR(NULL, EGL_BAD_DISPLAY, EGL_FALSE);
+
+ if (!disp->Initialized) {
+ _EGLDriver *drv = disp->Driver;
+
+ if (!drv) {
+ _eglPreloadDrivers();
+ drv = _eglMatchDriver(disp);
+ /* Initialize the particular display now */
+ if (drv && !drv->API.Initialize(drv, disp, &major_int, &minor_int))
+ RETURN_EGL_ERROR(disp, EGL_NOT_INITIALIZED, EGL_FALSE);
+ }
+ if (!drv)
+ /* Load and initialize the first default driver that works */
+ drv = _eglLoadDefaultDriver(disp, &major_int, &minor_int);
+ if (!drv)
+ RETURN_EGL_ERROR(disp, EGL_NOT_INITIALIZED, EGL_FALSE);
+
+ disp->APImajor = major_int;
+ disp->APIminor = minor_int;
+ _eglsnprintf(disp->Version, sizeof(disp->Version),
+ "%d.%d (%s)", major_int, minor_int, drv->Name);
+
+ /* limit to APIs supported by core */
+ disp->ClientAPIsMask &= _EGL_API_ALL_BITS;
+
+ disp->Driver = drv;
+ disp->Initialized = EGL_TRUE;
+ } else {
+ major_int = disp->APImajor;
+ minor_int = disp->APIminor;
+ }
+
+ /* Update applications version of major and minor if not NULL */
+ if ((major != NULL) && (minor != NULL)) {
+ *major = major_int;
+ *minor = minor_int;
+ }
+
+ RETURN_EGL_SUCCESS(disp, EGL_TRUE);
+}
+
+
+EGLBoolean EGLAPIENTRY
+eglTerminate(EGLDisplay dpy)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+
+ if (!disp)
+ RETURN_EGL_ERROR(NULL, EGL_BAD_DISPLAY, EGL_FALSE);
+
+ if (disp->Initialized) {
+ _EGLDriver *drv = disp->Driver;
+
+ drv->API.Terminate(drv, disp);
+ /* do not reset disp->Driver */
+ disp->Initialized = EGL_FALSE;
+ }
+
+ RETURN_EGL_SUCCESS(disp, EGL_TRUE);
+}
+
+
+const char * EGLAPIENTRY
+eglQueryString(EGLDisplay dpy, EGLint name)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLDriver *drv;
+ const char *ret;
+
+ _EGL_CHECK_DISPLAY(disp, NULL, drv);
+ ret = drv->API.QueryString(drv, disp, name);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean EGLAPIENTRY
+eglGetConfigs(EGLDisplay dpy, EGLConfig *configs,
+ EGLint config_size, EGLint *num_config)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
+ ret = drv->API.GetConfigs(drv, disp, configs, config_size, num_config);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean EGLAPIENTRY
+eglChooseConfig(EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs,
+ EGLint config_size, EGLint *num_config)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
+ ret = drv->API.ChooseConfig(drv, disp, attrib_list, configs,
+ config_size, num_config);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean EGLAPIENTRY
+eglGetConfigAttrib(EGLDisplay dpy, EGLConfig config,
+ EGLint attribute, EGLint *value)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLConfig *conf = _eglLookupConfig(config, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_CONFIG(disp, conf, EGL_FALSE, drv);
+ ret = drv->API.GetConfigAttrib(drv, disp, conf, attribute, value);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLContext EGLAPIENTRY
+eglCreateContext(EGLDisplay dpy, EGLConfig config, EGLContext share_list,
+ const EGLint *attrib_list)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLConfig *conf = _eglLookupConfig(config, disp);
+ _EGLContext *share = _eglLookupContext(share_list, disp);
+ _EGLDriver *drv;
+ _EGLContext *context;
+ EGLContext ret;
+
+ _EGL_CHECK_CONFIG(disp, conf, EGL_NO_CONTEXT, drv);
+ if (!share && share_list != EGL_NO_CONTEXT)
+ RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_NO_CONTEXT);
+
+ context = drv->API.CreateContext(drv, disp, conf, share, attrib_list);
+ ret = (context) ? _eglLinkContext(context, disp) : EGL_NO_CONTEXT;
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean EGLAPIENTRY
+eglDestroyContext(EGLDisplay dpy, EGLContext ctx)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLContext *context = _eglLookupContext(ctx, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_CONTEXT(disp, context, EGL_FALSE, drv);
+ _eglUnlinkContext(context);
+ ret = drv->API.DestroyContext(drv, disp, context);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean EGLAPIENTRY
+eglMakeCurrent(EGLDisplay dpy, EGLSurface draw, EGLSurface read,
+ EGLContext ctx)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLContext *context = _eglLookupContext(ctx, disp);
+ _EGLSurface *draw_surf = _eglLookupSurface(draw, disp);
+ _EGLSurface *read_surf = _eglLookupSurface(read, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ if (!disp)
+ RETURN_EGL_ERROR(disp, EGL_BAD_DISPLAY, EGL_FALSE);
+ drv = disp->Driver;
+
+ /* display is allowed to be uninitialized under certain condition */
+ if (!disp->Initialized) {
+ if (draw != EGL_NO_SURFACE || read != EGL_NO_SURFACE ||
+ ctx != EGL_NO_CONTEXT)
+ RETURN_EGL_ERROR(disp, EGL_BAD_DISPLAY, EGL_FALSE);
+ }
+ if (!drv)
+ RETURN_EGL_SUCCESS(disp, EGL_TRUE);
+
+ if (!context && ctx != EGL_NO_CONTEXT)
+ RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_FALSE);
+ if ((!draw_surf && draw != EGL_NO_SURFACE) ||
+ (!read_surf && read != EGL_NO_SURFACE))
+ RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
+
+ ret = drv->API.MakeCurrent(drv, disp, draw_surf, read_surf, context);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean EGLAPIENTRY
+eglQueryContext(EGLDisplay dpy, EGLContext ctx,
+ EGLint attribute, EGLint *value)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLContext *context = _eglLookupContext(ctx, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_CONTEXT(disp, context, EGL_FALSE, drv);
+ ret = drv->API.QueryContext(drv, disp, context, attribute, value);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLSurface EGLAPIENTRY
+eglCreateWindowSurface(EGLDisplay dpy, EGLConfig config,
+ EGLNativeWindowType window, const EGLint *attrib_list)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLConfig *conf = _eglLookupConfig(config, disp);
+ _EGLDriver *drv;
+ _EGLSurface *surf;
+ EGLSurface ret;
+
+ _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
+
+ surf = drv->API.CreateWindowSurface(drv, disp, conf, window, attrib_list);
+ ret = (surf) ? _eglLinkSurface(surf, disp) : EGL_NO_SURFACE;
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLSurface EGLAPIENTRY
+eglCreatePixmapSurface(EGLDisplay dpy, EGLConfig config,
+ EGLNativePixmapType pixmap, const EGLint *attrib_list)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLConfig *conf = _eglLookupConfig(config, disp);
+ _EGLDriver *drv;
+ _EGLSurface *surf;
+ EGLSurface ret;
+
+ _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
+
+ surf = drv->API.CreatePixmapSurface(drv, disp, conf, pixmap, attrib_list);
+ ret = (surf) ? _eglLinkSurface(surf, disp) : EGL_NO_SURFACE;
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLSurface EGLAPIENTRY
+eglCreatePbufferSurface(EGLDisplay dpy, EGLConfig config,
+ const EGLint *attrib_list)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLConfig *conf = _eglLookupConfig(config, disp);
+ _EGLDriver *drv;
+ _EGLSurface *surf;
+ EGLSurface ret;
+
+ _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
+
+ surf = drv->API.CreatePbufferSurface(drv, disp, conf, attrib_list);
+ ret = (surf) ? _eglLinkSurface(surf, disp) : EGL_NO_SURFACE;
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean EGLAPIENTRY
+eglDestroySurface(EGLDisplay dpy, EGLSurface surface)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLSurface *surf = _eglLookupSurface(surface, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
+ _eglUnlinkSurface(surf);
+ ret = drv->API.DestroySurface(drv, disp, surf);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+EGLBoolean EGLAPIENTRY
+eglQuerySurface(EGLDisplay dpy, EGLSurface surface,
+ EGLint attribute, EGLint *value)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLSurface *surf = _eglLookupSurface(surface, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
+ ret = drv->API.QuerySurface(drv, disp, surf, attribute, value);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+EGLBoolean EGLAPIENTRY
+eglSurfaceAttrib(EGLDisplay dpy, EGLSurface surface,
+ EGLint attribute, EGLint value)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLSurface *surf = _eglLookupSurface(surface, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
+ ret = drv->API.SurfaceAttrib(drv, disp, surf, attribute, value);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean EGLAPIENTRY
+eglBindTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLSurface *surf = _eglLookupSurface(surface, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
+ ret = drv->API.BindTexImage(drv, disp, surf, buffer);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean EGLAPIENTRY
+eglReleaseTexImage(EGLDisplay dpy, EGLSurface surface, EGLint buffer)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLSurface *surf = _eglLookupSurface(surface, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
+ ret = drv->API.ReleaseTexImage(drv, disp, surf, buffer);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean EGLAPIENTRY
+eglSwapInterval(EGLDisplay dpy, EGLint interval)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLContext *ctx = _eglGetCurrentContext();
+ _EGLSurface *surf;
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
+
+ if (!ctx || !_eglIsContextLinked(ctx) || ctx->Resource.Display != disp)
+ RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_FALSE);
+
+ surf = ctx->DrawSurface;
+ if (!_eglIsSurfaceLinked(surf))
+ RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
+
+ ret = drv->API.SwapInterval(drv, disp, surf, interval);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean EGLAPIENTRY
+eglSwapBuffers(EGLDisplay dpy, EGLSurface surface)
+{
+ _EGLContext *ctx = _eglGetCurrentContext();
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLSurface *surf = _eglLookupSurface(surface, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
+
+ /* surface must be bound to current context in EGL 1.4 */
+ if (!ctx || !_eglIsContextLinked(ctx) || surf != ctx->DrawSurface)
+ RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
+
+ ret = drv->API.SwapBuffers(drv, disp, surf);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean EGLAPIENTRY
+eglCopyBuffers(EGLDisplay dpy, EGLSurface surface, EGLNativePixmapType target)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLSurface *surf = _eglLookupSurface(surface, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
+ ret = drv->API.CopyBuffers(drv, disp, surf, target);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean EGLAPIENTRY
+eglWaitClient(void)
+{
+ _EGLContext *ctx = _eglGetCurrentContext();
+ _EGLDisplay *disp;
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ if (!ctx)
+ RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
+
+ disp = ctx->Resource.Display;
+ _eglLockMutex(&disp->Mutex);
+
+ /* let bad current context imply bad current surface */
+ if (!_eglIsContextLinked(ctx) || !_eglIsSurfaceLinked(ctx->DrawSurface))
+ RETURN_EGL_ERROR(disp, EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
+
+ /* a valid current context implies an initialized current display */
+ assert(disp->Initialized);
+ drv = disp->Driver;
+ ret = drv->API.WaitClient(drv, disp, ctx);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean EGLAPIENTRY
+eglWaitGL(void)
+{
+#ifdef EGL_VERSION_1_2
+ _EGLThreadInfo *t = _eglGetCurrentThread();
+ EGLint api_index = t->CurrentAPIIndex;
+ EGLint es_index = _eglConvertApiToIndex(EGL_OPENGL_ES_API);
+ EGLBoolean ret;
+
+ if (api_index != es_index && _eglIsCurrentThreadDummy())
+ RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, EGL_FALSE);
+
+ t->CurrentAPIIndex = es_index;
+ ret = eglWaitClient();
+ t->CurrentAPIIndex = api_index;
+ return ret;
+#else
+ return eglWaitClient();
+#endif
+}
+
+
+EGLBoolean EGLAPIENTRY
+eglWaitNative(EGLint engine)
+{
+ _EGLContext *ctx = _eglGetCurrentContext();
+ _EGLDisplay *disp;
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ if (!ctx)
+ RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
+
+ disp = ctx->Resource.Display;
+ _eglLockMutex(&disp->Mutex);
+
+ /* let bad current context imply bad current surface */
+ if (!_eglIsContextLinked(ctx) || !_eglIsSurfaceLinked(ctx->DrawSurface))
+ RETURN_EGL_ERROR(disp, EGL_BAD_CURRENT_SURFACE, EGL_FALSE);
+
+ /* a valid current context implies an initialized current display */
+ assert(disp->Initialized);
+ drv = disp->Driver;
+ ret = drv->API.WaitNative(drv, disp, engine);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLDisplay EGLAPIENTRY
+eglGetCurrentDisplay(void)
+{
+ _EGLContext *ctx = _eglGetCurrentContext();
+ EGLDisplay ret;
+
+ ret = (ctx) ? _eglGetDisplayHandle(ctx->Resource.Display) : EGL_NO_DISPLAY;
+
+ RETURN_EGL_SUCCESS(NULL, ret);
+}
+
+
+EGLContext EGLAPIENTRY
+eglGetCurrentContext(void)
+{
+ _EGLContext *ctx = _eglGetCurrentContext();
+ EGLContext ret;
+
+ ret = _eglGetContextHandle(ctx);
+
+ RETURN_EGL_SUCCESS(NULL, ret);
+}
+
+
+EGLSurface EGLAPIENTRY
+eglGetCurrentSurface(EGLint readdraw)
+{
+ _EGLContext *ctx = _eglGetCurrentContext();
+ EGLint err = EGL_SUCCESS;
+ _EGLSurface *surf;
+ EGLSurface ret;
+
+ if (!ctx)
+ RETURN_EGL_SUCCESS(NULL, EGL_NO_SURFACE);
+
+ switch (readdraw) {
+ case EGL_DRAW:
+ surf = ctx->DrawSurface;
+ break;
+ case EGL_READ:
+ surf = ctx->ReadSurface;
+ break;
+ default:
+ surf = NULL;
+ err = EGL_BAD_PARAMETER;
+ break;
+ }
+
+ ret = _eglGetSurfaceHandle(surf);
+
+ RETURN_EGL_ERROR(NULL, err, ret);
+}
+
+
+EGLint EGLAPIENTRY
+eglGetError(void)
+{
+ _EGLThreadInfo *t = _eglGetCurrentThread();
+ EGLint e = t->LastError;
+ if (!_eglIsCurrentThreadDummy())
+ t->LastError = EGL_SUCCESS;
+ return e;
+}
+
+
+__eglMustCastToProperFunctionPointerType EGLAPIENTRY
+eglGetProcAddress(const char *procname)
+{
+ static const struct {
+ const char *name;
+ _EGLProc function;
+ } egl_functions[] = {
+ /* extensions only */
+#ifdef EGL_MESA_screen_surface
+ { "eglChooseModeMESA", (_EGLProc) eglChooseModeMESA },
+ { "eglGetModesMESA", (_EGLProc) eglGetModesMESA },
+ { "eglGetModeAttribMESA", (_EGLProc) eglGetModeAttribMESA },
+ { "eglCopyContextMESA", (_EGLProc) eglCopyContextMESA },
+ { "eglGetScreensMESA", (_EGLProc) eglGetScreensMESA },
+ { "eglCreateScreenSurfaceMESA", (_EGLProc) eglCreateScreenSurfaceMESA },
+ { "eglShowScreenSurfaceMESA", (_EGLProc) eglShowScreenSurfaceMESA },
+ { "eglScreenPositionMESA", (_EGLProc) eglScreenPositionMESA },
+ { "eglQueryScreenMESA", (_EGLProc) eglQueryScreenMESA },
+ { "eglQueryScreenSurfaceMESA", (_EGLProc) eglQueryScreenSurfaceMESA },
+ { "eglQueryScreenModeMESA", (_EGLProc) eglQueryScreenModeMESA },
+ { "eglQueryModeStringMESA", (_EGLProc) eglQueryModeStringMESA },
+#endif /* EGL_MESA_screen_surface */
+#ifdef EGL_KHR_image_base
+ { "eglCreateImageKHR", (_EGLProc) eglCreateImageKHR },
+ { "eglDestroyImageKHR", (_EGLProc) eglDestroyImageKHR },
+#endif /* EGL_KHR_image_base */
+#ifdef EGL_NOK_swap_region
+ { "eglSwapBuffersRegionNOK", (_EGLProc) eglSwapBuffersRegionNOK },
+#endif
+ { NULL, NULL }
+ };
+ EGLint i;
+ _EGLProc ret;
+
+ if (!procname)
+ RETURN_EGL_SUCCESS(NULL, NULL);
+
+ ret = NULL;
+ if (strncmp(procname, "egl", 3) == 0) {
+ for (i = 0; egl_functions[i].name; i++) {
+ if (strcmp(egl_functions[i].name, procname) == 0) {
+ ret = egl_functions[i].function;
+ break;
+ }
+ }
+ }
+ if (ret)
+ RETURN_EGL_SUCCESS(NULL, ret);
+
+ _eglPreloadDrivers();
+
+ /* now loop over drivers to query their procs */
+ for (i = 0; i < _eglGlobal.NumDrivers; i++) {
+ _EGLDriver *drv = _eglGlobal.Drivers[i];
+ ret = drv->API.GetProcAddress(drv, procname);
+ if (ret)
+ break;
+ }
+
+ RETURN_EGL_SUCCESS(NULL, ret);
+}
+
+
+#ifdef EGL_MESA_screen_surface
+
+
+/*
+ * EGL_MESA_screen extension
+ */
+
+EGLBoolean EGLAPIENTRY
+eglChooseModeMESA(EGLDisplay dpy, EGLScreenMESA screen,
+ const EGLint *attrib_list, EGLModeMESA *modes,
+ EGLint modes_size, EGLint *num_modes)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLScreen *scrn = _eglLookupScreen(screen, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_SCREEN(disp, scrn, EGL_FALSE, drv);
+ ret = drv->API.ChooseModeMESA(drv, disp, scrn, attrib_list,
+ modes, modes_size, num_modes);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean EGLAPIENTRY
+eglGetModesMESA(EGLDisplay dpy, EGLScreenMESA screen, EGLModeMESA *modes,
+ EGLint mode_size, EGLint *num_mode)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLScreen *scrn = _eglLookupScreen(screen, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_SCREEN(disp, scrn, EGL_FALSE, drv);
+ ret = drv->API.GetModesMESA(drv, disp, scrn, modes, mode_size, num_mode);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean EGLAPIENTRY
+eglGetModeAttribMESA(EGLDisplay dpy, EGLModeMESA mode,
+ EGLint attribute, EGLint *value)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLMode *m = _eglLookupMode(mode, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_MODE(disp, m, EGL_FALSE, drv);
+ ret = drv->API.GetModeAttribMESA(drv, disp, m, attribute, value);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean EGLAPIENTRY
+eglCopyContextMESA(EGLDisplay dpy, EGLContext source, EGLContext dest,
+ EGLint mask)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLContext *source_context = _eglLookupContext(source, disp);
+ _EGLContext *dest_context = _eglLookupContext(dest, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_CONTEXT(disp, source_context, EGL_FALSE, drv);
+ if (!dest_context)
+ RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_FALSE);
+
+ ret = drv->API.CopyContextMESA(drv, disp,
+ source_context, dest_context, mask);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean
+eglGetScreensMESA(EGLDisplay dpy, EGLScreenMESA *screens,
+ EGLint max_screens, EGLint *num_screens)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
+ ret = drv->API.GetScreensMESA(drv, disp, screens, max_screens, num_screens);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLSurface
+eglCreateScreenSurfaceMESA(EGLDisplay dpy, EGLConfig config,
+ const EGLint *attrib_list)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLConfig *conf = _eglLookupConfig(config, disp);
+ _EGLDriver *drv;
+ _EGLSurface *surf;
+ EGLSurface ret;
+
+ _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
+
+ surf = drv->API.CreateScreenSurfaceMESA(drv, disp, conf, attrib_list);
+ ret = (surf) ? _eglLinkSurface(surf, disp) : EGL_NO_SURFACE;
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean
+eglShowScreenSurfaceMESA(EGLDisplay dpy, EGLint screen,
+ EGLSurface surface, EGLModeMESA mode)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLScreen *scrn = _eglLookupScreen((EGLScreenMESA) screen, disp);
+ _EGLSurface *surf = _eglLookupSurface(surface, disp);
+ _EGLMode *m = _eglLookupMode(mode, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_SCREEN(disp, scrn, EGL_FALSE, drv);
+ if (!surf && surface != EGL_NO_SURFACE)
+ RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
+ if (!m && mode != EGL_NO_MODE_MESA)
+ RETURN_EGL_ERROR(disp, EGL_BAD_MODE_MESA, EGL_FALSE);
+
+ ret = drv->API.ShowScreenSurfaceMESA(drv, disp, scrn, surf, m);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean
+eglScreenPositionMESA(EGLDisplay dpy, EGLScreenMESA screen, EGLint x, EGLint y)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLScreen *scrn = _eglLookupScreen(screen, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_SCREEN(disp, scrn, EGL_FALSE, drv);
+ ret = drv->API.ScreenPositionMESA(drv, disp, scrn, x, y);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean
+eglQueryScreenMESA(EGLDisplay dpy, EGLScreenMESA screen,
+ EGLint attribute, EGLint *value)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLScreen *scrn = _eglLookupScreen(screen, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_SCREEN(disp, scrn, EGL_FALSE, drv);
+ ret = drv->API.QueryScreenMESA(drv, disp, scrn, attribute, value);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean
+eglQueryScreenSurfaceMESA(EGLDisplay dpy, EGLScreenMESA screen,
+ EGLSurface *surface)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLScreen *scrn = _eglLookupScreen((EGLScreenMESA) screen, disp);
+ _EGLDriver *drv;
+ _EGLSurface *surf;
+ EGLBoolean ret;
+
+ _EGL_CHECK_SCREEN(disp, scrn, EGL_FALSE, drv);
+ ret = drv->API.QueryScreenSurfaceMESA(drv, disp, scrn, &surf);
+ if (ret && surface)
+ *surface = _eglGetSurfaceHandle(surf);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean
+eglQueryScreenModeMESA(EGLDisplay dpy, EGLScreenMESA screen, EGLModeMESA *mode)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLScreen *scrn = _eglLookupScreen((EGLScreenMESA) screen, disp);
+ _EGLDriver *drv;
+ _EGLMode *m;
+ EGLBoolean ret;
+
+ _EGL_CHECK_SCREEN(disp, scrn, EGL_FALSE, drv);
+ ret = drv->API.QueryScreenModeMESA(drv, disp, scrn, &m);
+ if (ret && mode)
+ *mode = m->Handle;
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+const char *
+eglQueryModeStringMESA(EGLDisplay dpy, EGLModeMESA mode)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLMode *m = _eglLookupMode(mode, disp);
+ _EGLDriver *drv;
+ const char *ret;
+
+ _EGL_CHECK_MODE(disp, m, NULL, drv);
+ ret = drv->API.QueryModeStringMESA(drv, disp, m);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+#endif /* EGL_MESA_screen_surface */
+
+
+/**
+ ** EGL 1.2
+ **/
+
+#ifdef EGL_VERSION_1_2
+
+
+/**
+ * Specify the client API to use for subsequent calls including:
+ * eglCreateContext()
+ * eglGetCurrentContext()
+ * eglGetCurrentDisplay()
+ * eglGetCurrentSurface()
+ * eglMakeCurrent(when the ctx parameter is EGL NO CONTEXT)
+ * eglWaitClient()
+ * eglWaitNative()
+ * See section 3.7 "Rendering Context" in the EGL specification for details.
+ */
+EGLBoolean
+eglBindAPI(EGLenum api)
+{
+ _EGLThreadInfo *t = _eglGetCurrentThread();
+
+ if (_eglIsCurrentThreadDummy())
+ RETURN_EGL_ERROR(NULL, EGL_BAD_ALLOC, EGL_FALSE);
+
+ if (!_eglIsApiValid(api))
+ RETURN_EGL_ERROR(NULL, EGL_BAD_PARAMETER, EGL_FALSE);
+
+ t->CurrentAPIIndex = _eglConvertApiToIndex(api);
+
+ RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
+}
+
+
+/**
+ * Return the last value set with eglBindAPI().
+ */
+EGLenum
+eglQueryAPI(void)
+{
+ _EGLThreadInfo *t = _eglGetCurrentThread();
+ EGLenum ret;
+
+ /* returns one of EGL_OPENGL_API, EGL_OPENGL_ES_API or EGL_OPENVG_API */
+ ret = _eglConvertApiFromIndex(t->CurrentAPIIndex);
+
+ RETURN_EGL_SUCCESS(NULL, ret);
+}
+
+
+EGLSurface
+eglCreatePbufferFromClientBuffer(EGLDisplay dpy, EGLenum buftype,
+ EGLClientBuffer buffer, EGLConfig config,
+ const EGLint *attrib_list)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLConfig *conf = _eglLookupConfig(config, disp);
+ _EGLDriver *drv;
+ _EGLSurface *surf;
+ EGLSurface ret;
+
+ _EGL_CHECK_CONFIG(disp, conf, EGL_NO_SURFACE, drv);
+
+ surf = drv->API.CreatePbufferFromClientBuffer(drv, disp, buftype, buffer,
+ conf, attrib_list);
+ ret = (surf) ? _eglLinkSurface(surf, disp) : EGL_NO_SURFACE;
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean
+eglReleaseThread(void)
+{
+ /* unbind current contexts */
+ if (!_eglIsCurrentThreadDummy()) {
+ _EGLThreadInfo *t = _eglGetCurrentThread();
+ EGLint api_index = t->CurrentAPIIndex;
+ EGLint i;
+
+ for (i = 0; i < _EGL_API_NUM_APIS; i++) {
+ _EGLContext *ctx = t->CurrentContexts[i];
+ if (ctx) {
+ _EGLDisplay *disp = ctx->Resource.Display;
+ _EGLDriver *drv;
+
+ t->CurrentAPIIndex = i;
+
+ _eglLockMutex(&disp->Mutex);
+ drv = disp->Driver;
+ (void) drv->API.MakeCurrent(drv, disp, NULL, NULL, NULL);
+ _eglUnlockMutex(&disp->Mutex);
+ }
+ }
+
+ t->CurrentAPIIndex = api_index;
+ }
+
+ _eglDestroyCurrentThread();
+
+ RETURN_EGL_SUCCESS(NULL, EGL_TRUE);
+}
+
+
+#endif /* EGL_VERSION_1_2 */
+
+
+#ifdef EGL_KHR_image_base
+
+
+EGLImageKHR
+eglCreateImageKHR(EGLDisplay dpy, EGLContext ctx, EGLenum target,
+ EGLClientBuffer buffer, const EGLint *attr_list)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLContext *context = _eglLookupContext(ctx, disp);
+ _EGLDriver *drv;
+ _EGLImage *img;
+ EGLImageKHR ret;
+
+ _EGL_CHECK_DISPLAY(disp, EGL_NO_IMAGE_KHR, drv);
+ if (!context && ctx != EGL_NO_CONTEXT)
+ RETURN_EGL_ERROR(disp, EGL_BAD_CONTEXT, EGL_NO_IMAGE_KHR);
+
+ img = drv->API.CreateImageKHR(drv,
+ disp, context, target, buffer, attr_list);
+ ret = (img) ? _eglLinkImage(img, disp) : EGL_NO_IMAGE_KHR;
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+EGLBoolean
+eglDestroyImageKHR(EGLDisplay dpy, EGLImageKHR image)
+{
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLImage *img = _eglLookupImage(image, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_DISPLAY(disp, EGL_FALSE, drv);
+ if (!img)
+ RETURN_EGL_ERROR(disp, EGL_BAD_PARAMETER, EGL_FALSE);
+
+ _eglUnlinkImage(img);
+ ret = drv->API.DestroyImageKHR(drv, disp, img);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+
+#endif /* EGL_KHR_image_base */
+
+
+#ifdef EGL_NOK_swap_region
+
+EGLBoolean
+eglSwapBuffersRegionNOK(EGLDisplay dpy, EGLSurface surface,
+ EGLint numRects, const EGLint *rects)
+{
+ _EGLContext *ctx = _eglGetCurrentContext();
+ _EGLDisplay *disp = _eglLockDisplay(dpy);
+ _EGLSurface *surf = _eglLookupSurface(surface, disp);
+ _EGLDriver *drv;
+ EGLBoolean ret;
+
+ _EGL_CHECK_SURFACE(disp, surf, EGL_FALSE, drv);
+
+ /* surface must be bound to current context in EGL 1.4 */
+ if (!ctx || !_eglIsContextLinked(ctx) || surf != ctx->DrawSurface)
+ RETURN_EGL_ERROR(disp, EGL_BAD_SURFACE, EGL_FALSE);
+
+ if (drv->API.SwapBuffersRegionNOK)
+ ret = drv->API.SwapBuffersRegionNOK(drv, disp, surf, numRects, rects);
+ else
+ ret = drv->API.SwapBuffers(drv, disp, surf);
+
+ RETURN_EGL_EVAL(disp, ret);
+}
+
+#endif /* EGL_NOK_swap_region */
diff --git a/src/egl/main/eglapi.h b/src/egl/main/eglapi.h
new file mode 100644
index 0000000000..d8c8b49a49
--- /dev/null
+++ b/src/egl/main/eglapi.h
@@ -0,0 +1,146 @@
+#ifndef EGLAPI_INCLUDED
+#define EGLAPI_INCLUDED
+
+/**
+ * A generic function ptr type
+ */
+typedef void (*_EGLProc)(void);
+
+
+/**
+ * Typedefs for all EGL API entrypoint functions.
+ */
+
+/* driver funcs */
+typedef EGLBoolean (*Initialize_t)(_EGLDriver *, _EGLDisplay *dpy, EGLint *major, EGLint *minor);
+typedef EGLBoolean (*Terminate_t)(_EGLDriver *, _EGLDisplay *dpy);
+
+/* config funcs */
+typedef EGLBoolean (*GetConfigs_t)(_EGLDriver *drv, _EGLDisplay *dpy, EGLConfig *configs, EGLint config_size, EGLint *num_config);
+typedef EGLBoolean (*ChooseConfig_t)(_EGLDriver *drv, _EGLDisplay *dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config);
+typedef EGLBoolean (*GetConfigAttrib_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *config, EGLint attribute, EGLint *value);
+
+/* context funcs */
+typedef _EGLContext *(*CreateContext_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *config, _EGLContext *share_list, const EGLint *attrib_list);
+typedef EGLBoolean (*DestroyContext_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx);
+/* this is the only function (other than Initialize) that may be called with an uninitialized display */
+typedef EGLBoolean (*MakeCurrent_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *draw, _EGLSurface *read, _EGLContext *ctx);
+typedef EGLBoolean (*QueryContext_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx, EGLint attribute, EGLint *value);
+
+/* surface funcs */
+typedef _EGLSurface *(*CreateWindowSurface_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *config, EGLNativeWindowType window, const EGLint *attrib_list);
+typedef _EGLSurface *(*CreatePixmapSurface_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *config, EGLNativePixmapType pixmap, const EGLint *attrib_list);
+typedef _EGLSurface *(*CreatePbufferSurface_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *config, const EGLint *attrib_list);
+typedef EGLBoolean (*DestroySurface_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surface);
+typedef EGLBoolean (*QuerySurface_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surface, EGLint attribute, EGLint *value);
+typedef EGLBoolean (*SurfaceAttrib_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surface, EGLint attribute, EGLint value);
+typedef EGLBoolean (*BindTexImage_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surface, EGLint buffer);
+typedef EGLBoolean (*ReleaseTexImage_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surface, EGLint buffer);
+typedef EGLBoolean (*SwapInterval_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf, EGLint interval);
+typedef EGLBoolean (*SwapBuffers_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *draw);
+typedef EGLBoolean (*CopyBuffers_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surface, EGLNativePixmapType target);
+
+/* misc funcs */
+typedef const char *(*QueryString_t)(_EGLDriver *drv, _EGLDisplay *dpy, EGLint name);
+typedef EGLBoolean (*WaitClient_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx);
+typedef EGLBoolean (*WaitNative_t)(_EGLDriver *drv, _EGLDisplay *dpy, EGLint engine);
+
+/* this function may be called from multiple threads at the same time */
+typedef _EGLProc (*GetProcAddress_t)(_EGLDriver *drv, const char *procname);
+
+
+
+#ifdef EGL_MESA_screen_surface
+typedef EGLBoolean (*ChooseModeMESA_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *screen, const EGLint *attrib_list, EGLModeMESA *modes, EGLint modes_size, EGLint *num_modes);
+typedef EGLBoolean (*GetModesMESA_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *screen, EGLModeMESA *modes, EGLint mode_size, EGLint *num_mode);
+typedef EGLBoolean (*GetModeAttribMESA_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLMode *mode, EGLint attribute, EGLint *value);
+typedef EGLBoolean (*CopyContextMESA_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *source, _EGLContext *dest, EGLint mask);
+typedef EGLBoolean (*GetScreensMESA_t)(_EGLDriver *drv, _EGLDisplay *dpy, EGLScreenMESA *screens, EGLint max_screens, EGLint *num_screens);
+typedef _EGLSurface *(*CreateScreenSurfaceMESA_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *config, const EGLint *attrib_list);
+typedef EGLBoolean (*ShowScreenSurfaceMESA_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *screen, _EGLSurface *surface, _EGLMode *mode);
+typedef EGLBoolean (*ScreenPositionMESA_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *screen, EGLint x, EGLint y);
+typedef EGLBoolean (*QueryScreenMESA_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *screen, EGLint attribute, EGLint *value);
+typedef EGLBoolean (*QueryScreenSurfaceMESA_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *screen, _EGLSurface **surface);
+typedef EGLBoolean (*QueryScreenModeMESA_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *screen, _EGLMode **mode);
+typedef const char * (*QueryModeStringMESA_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLMode *mode);
+#endif /* EGL_MESA_screen_surface */
+
+
+#ifdef EGL_VERSION_1_2
+typedef _EGLSurface *(*CreatePbufferFromClientBuffer_t)(_EGLDriver *drv, _EGLDisplay *dpy, EGLenum buftype, EGLClientBuffer buffer, _EGLConfig *config, const EGLint *attrib_list);
+#endif /* EGL_VERSION_1_2 */
+
+
+#ifdef EGL_KHR_image_base
+typedef _EGLImage *(*CreateImageKHR_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx, EGLenum target, EGLClientBuffer buffer, const EGLint *attr_list);
+typedef EGLBoolean (*DestroyImageKHR_t)(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *image);
+#endif /* EGL_KHR_image_base */
+
+#ifdef EGL_NOK_swap_region
+typedef EGLBoolean (*SwapBuffersRegionNOK_t)(_EGLDriver *drv, _EGLDisplay *disp, _EGLSurface *surf, EGLint numRects, const EGLint *rects);
+#endif
+
+/**
+ * The API dispatcher jumps through these functions
+ */
+struct _egl_api
+{
+ Initialize_t Initialize;
+ Terminate_t Terminate;
+
+ GetConfigs_t GetConfigs;
+ ChooseConfig_t ChooseConfig;
+ GetConfigAttrib_t GetConfigAttrib;
+
+ CreateContext_t CreateContext;
+ DestroyContext_t DestroyContext;
+ MakeCurrent_t MakeCurrent;
+ QueryContext_t QueryContext;
+
+ CreateWindowSurface_t CreateWindowSurface;
+ CreatePixmapSurface_t CreatePixmapSurface;
+ CreatePbufferSurface_t CreatePbufferSurface;
+ DestroySurface_t DestroySurface;
+ QuerySurface_t QuerySurface;
+ SurfaceAttrib_t SurfaceAttrib;
+ BindTexImage_t BindTexImage;
+ ReleaseTexImage_t ReleaseTexImage;
+ SwapInterval_t SwapInterval;
+ SwapBuffers_t SwapBuffers;
+ CopyBuffers_t CopyBuffers;
+
+ QueryString_t QueryString;
+ WaitClient_t WaitClient;
+ WaitNative_t WaitNative;
+ GetProcAddress_t GetProcAddress;
+
+#ifdef EGL_MESA_screen_surface
+ ChooseModeMESA_t ChooseModeMESA;
+ GetModesMESA_t GetModesMESA;
+ GetModeAttribMESA_t GetModeAttribMESA;
+ CopyContextMESA_t CopyContextMESA;
+ GetScreensMESA_t GetScreensMESA;
+ CreateScreenSurfaceMESA_t CreateScreenSurfaceMESA;
+ ShowScreenSurfaceMESA_t ShowScreenSurfaceMESA;
+ ScreenPositionMESA_t ScreenPositionMESA;
+ QueryScreenMESA_t QueryScreenMESA;
+ QueryScreenSurfaceMESA_t QueryScreenSurfaceMESA;
+ QueryScreenModeMESA_t QueryScreenModeMESA;
+ QueryModeStringMESA_t QueryModeStringMESA;
+#endif /* EGL_MESA_screen_surface */
+
+#ifdef EGL_VERSION_1_2
+ CreatePbufferFromClientBuffer_t CreatePbufferFromClientBuffer;
+#endif
+
+#ifdef EGL_KHR_image_base
+ CreateImageKHR_t CreateImageKHR;
+ DestroyImageKHR_t DestroyImageKHR;
+#endif /* EGL_KHR_image_base */
+
+#ifdef EGL_NOK_swap_region
+ SwapBuffersRegionNOK_t SwapBuffersRegionNOK;
+#endif
+};
+
+#endif /* EGLAPI_INCLUDED */
diff --git a/src/egl/main/eglcompiler.h b/src/egl/main/eglcompiler.h
new file mode 100644
index 0000000000..90c75e84b2
--- /dev/null
+++ b/src/egl/main/eglcompiler.h
@@ -0,0 +1,94 @@
+#ifndef EGLCOMPILER_INCLUDED
+#define EGLCOMPILER_INCLUDED
+
+
+/**
+ * Get standard integer types
+ */
+#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)
+# include <stdint.h>
+#elif defined(_MSC_VER)
+ typedef __int8 int8_t;
+ typedef unsigned __int8 uint8_t;
+ typedef __int16 int16_t;
+ typedef unsigned __int16 uint16_t;
+ typedef __int32 int32_t;
+ typedef unsigned __int32 uint32_t;
+ typedef __int64 int64_t;
+ typedef unsigned __int64 uint64_t;
+
+# if defined(_WIN64)
+ typedef __int64 intptr_t;
+ typedef unsigned __int64 uintptr_t;
+# else
+ typedef __int32 intptr_t;
+ typedef unsigned __int32 uintptr_t;
+# endif
+
+# define INT64_C(__val) __val##i64
+# define UINT64_C(__val) __val##ui64
+#else
+/* hope the best instead of adding a bunch of ifdef's */
+# include <stdint.h>
+#endif
+
+
+/**
+ * Function inlining
+ */
+#if defined(__GNUC__)
+# define INLINE __inline__
+#elif defined(__MSC__)
+# define INLINE __inline
+#elif defined(_MSC_VER)
+# define INLINE __inline
+#elif defined(__ICL)
+# define INLINE __inline
+#elif defined(__INTEL_COMPILER)
+# define INLINE inline
+#elif defined(__WATCOMC__) && (__WATCOMC__ >= 1100)
+# define INLINE __inline
+#elif defined(__SUNPRO_C) && defined(__C99FEATURES__)
+# define INLINE inline
+# define __inline inline
+# define __inline__ inline
+#elif (__STDC_VERSION__ >= 199901L) /* C99 */
+# define INLINE inline
+#else
+# define INLINE
+#endif
+
+
+/**
+ * Function visibility
+ */
+#ifndef PUBLIC
+# if defined(__GNUC__) || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
+# define PUBLIC __attribute__((visibility("default")))
+# elif defined(_MSC_VER)
+# define PUBLIC __declspec(dllexport)
+# else
+# define PUBLIC
+# endif
+#endif
+
+/**
+ * The __FUNCTION__ gcc variable is generally only used for debugging.
+ * If we're not using gcc, define __FUNCTION__ as a cpp symbol here.
+ * Don't define it if using a newer Windows compiler.
+ */
+#ifndef __FUNCTION__
+# if defined(__VMS)
+# define __FUNCTION__ "VMS$NL:"
+# elif (!defined __GNUC__) && (!defined __xlC__) && \
+ (!defined(_MSC_VER) || _MSC_VER < 1300)
+# if (__STDC_VERSION__ >= 199901L) /* C99 */ || \
+ (defined(__SUNPRO_C) && defined(__C99FEATURES__))
+# define __FUNCTION__ __func__
+# else
+# define __FUNCTION__ "<unknown>"
+# endif
+# endif
+#endif
+
+#endif /* EGLCOMPILER_INCLUDED */
diff --git a/src/egl/main/eglconfig.c b/src/egl/main/eglconfig.c
new file mode 100644
index 0000000000..fa947d7688
--- /dev/null
+++ b/src/egl/main/eglconfig.c
@@ -0,0 +1,848 @@
+/**
+ * EGL Configuration (pixel format) functions.
+ */
+
+
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+#include "eglconfig.h"
+#include "egldisplay.h"
+#include "eglcurrent.h"
+#include "egllog.h"
+
+
+#define MIN2(A, B) (((A) < (B)) ? (A) : (B))
+
+
+/**
+ * Init the given _EGLconfig to default values.
+ * \param id the configuration's ID.
+ *
+ * Note that id must be positive for the config to be valid.
+ * It is also recommended that when there are N configs, their
+ * IDs are from 1 to N respectively.
+ */
+void
+_eglInitConfig(_EGLConfig *config, _EGLDisplay *dpy, EGLint id)
+{
+ memset(config, 0, sizeof(*config));
+
+ config->Display = dpy;
+
+ /* some attributes take non-zero default values */
+ SET_CONFIG_ATTRIB(config, EGL_CONFIG_ID, id);
+ SET_CONFIG_ATTRIB(config, EGL_CONFIG_CAVEAT, EGL_NONE);
+ SET_CONFIG_ATTRIB(config, EGL_TRANSPARENT_TYPE, EGL_NONE);
+ SET_CONFIG_ATTRIB(config, EGL_NATIVE_VISUAL_TYPE, EGL_NONE);
+#ifdef EGL_VERSION_1_2
+ SET_CONFIG_ATTRIB(config, EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER);
+#endif /* EGL_VERSION_1_2 */
+}
+
+
+/**
+ * Link a config to a display and return the handle of the link.
+ * The handle can be passed to client directly.
+ *
+ * Note that we just save the ptr to the config (we don't copy the config).
+ */
+EGLConfig
+_eglAddConfig(_EGLDisplay *dpy, _EGLConfig *conf)
+{
+ _EGLConfig **configs;
+
+ /* sanity check */
+ assert(GET_CONFIG_ATTRIB(conf, EGL_CONFIG_ID) > 0);
+
+ configs = dpy->Configs;
+ if (dpy->NumConfigs >= dpy->MaxConfigs) {
+ EGLint new_size = dpy->MaxConfigs + 16;
+ assert(dpy->NumConfigs < new_size);
+
+ configs = realloc(dpy->Configs, new_size * sizeof(dpy->Configs[0]));
+ if (!configs)
+ return (EGLConfig) NULL;
+
+ dpy->Configs = configs;
+ dpy->MaxConfigs = new_size;
+ }
+
+ conf->Display = dpy;
+ dpy->Configs[dpy->NumConfigs++] = conf;
+
+ return (EGLConfig) conf;
+}
+
+
+EGLBoolean
+_eglCheckConfigHandle(EGLConfig config, _EGLDisplay *dpy)
+{
+ EGLint num_configs = (dpy) ? dpy->NumConfigs : 0;
+ EGLint i;
+
+ for (i = 0; i < num_configs; i++) {
+ _EGLConfig *conf = dpy->Configs[i];
+ if (conf == (_EGLConfig *) config) {
+ assert(conf->Display == dpy);
+ break;
+ }
+ }
+ return (i < num_configs);
+}
+
+
+enum {
+ /* types */
+ ATTRIB_TYPE_INTEGER,
+ ATTRIB_TYPE_BOOLEAN,
+ ATTRIB_TYPE_BITMASK,
+ ATTRIB_TYPE_ENUM,
+ ATTRIB_TYPE_PSEUDO, /* non-queryable */
+ ATTRIB_TYPE_PLATFORM, /* platform-dependent */
+ /* criteria */
+ ATTRIB_CRITERION_EXACT,
+ ATTRIB_CRITERION_ATLEAST,
+ ATTRIB_CRITERION_MASK,
+ ATTRIB_CRITERION_SPECIAL,
+ ATTRIB_CRITERION_IGNORE
+};
+
+
+/* EGL spec Table 3.1 and 3.4 */
+static const struct {
+ EGLint attr;
+ EGLint type;
+ EGLint criterion;
+ EGLint default_value;
+} _eglValidationTable[] =
+{
+ { EGL_BUFFER_SIZE, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_ATLEAST,
+ 0 },
+ { EGL_RED_SIZE, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_ATLEAST,
+ 0 },
+ { EGL_GREEN_SIZE, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_ATLEAST,
+ 0 },
+ { EGL_BLUE_SIZE, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_ATLEAST,
+ 0 },
+ { EGL_LUMINANCE_SIZE, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_ATLEAST,
+ 0 },
+ { EGL_ALPHA_SIZE, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_ATLEAST,
+ 0 },
+ { EGL_ALPHA_MASK_SIZE, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_ATLEAST,
+ 0 },
+ { EGL_BIND_TO_TEXTURE_RGB, ATTRIB_TYPE_BOOLEAN,
+ ATTRIB_CRITERION_EXACT,
+ EGL_DONT_CARE },
+ { EGL_BIND_TO_TEXTURE_RGBA, ATTRIB_TYPE_BOOLEAN,
+ ATTRIB_CRITERION_EXACT,
+ EGL_DONT_CARE },
+ { EGL_COLOR_BUFFER_TYPE, ATTRIB_TYPE_ENUM,
+ ATTRIB_CRITERION_EXACT,
+ EGL_RGB_BUFFER },
+ { EGL_CONFIG_CAVEAT, ATTRIB_TYPE_ENUM,
+ ATTRIB_CRITERION_EXACT,
+ EGL_DONT_CARE },
+ { EGL_CONFIG_ID, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_EXACT,
+ EGL_DONT_CARE },
+ { EGL_CONFORMANT, ATTRIB_TYPE_BITMASK,
+ ATTRIB_CRITERION_MASK,
+ 0 },
+ { EGL_DEPTH_SIZE, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_ATLEAST,
+ 0 },
+ { EGL_LEVEL, ATTRIB_TYPE_PLATFORM,
+ ATTRIB_CRITERION_EXACT,
+ 0 },
+ { EGL_MAX_PBUFFER_WIDTH, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_IGNORE,
+ 0 },
+ { EGL_MAX_PBUFFER_HEIGHT, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_IGNORE,
+ 0 },
+ { EGL_MAX_PBUFFER_PIXELS, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_IGNORE,
+ 0 },
+ { EGL_MAX_SWAP_INTERVAL, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_EXACT,
+ EGL_DONT_CARE },
+ { EGL_MIN_SWAP_INTERVAL, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_EXACT,
+ EGL_DONT_CARE },
+ { EGL_NATIVE_RENDERABLE, ATTRIB_TYPE_BOOLEAN,
+ ATTRIB_CRITERION_EXACT,
+ EGL_DONT_CARE },
+ { EGL_NATIVE_VISUAL_ID, ATTRIB_TYPE_PLATFORM,
+ ATTRIB_CRITERION_IGNORE,
+ 0 },
+ { EGL_NATIVE_VISUAL_TYPE, ATTRIB_TYPE_PLATFORM,
+ ATTRIB_CRITERION_EXACT,
+ EGL_DONT_CARE },
+ { EGL_RENDERABLE_TYPE, ATTRIB_TYPE_BITMASK,
+ ATTRIB_CRITERION_MASK,
+ EGL_OPENGL_ES_BIT },
+ { EGL_SAMPLE_BUFFERS, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_ATLEAST,
+ 0 },
+ { EGL_SAMPLES, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_ATLEAST,
+ 0 },
+ { EGL_STENCIL_SIZE, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_ATLEAST,
+ 0 },
+ { EGL_SURFACE_TYPE, ATTRIB_TYPE_BITMASK,
+ ATTRIB_CRITERION_MASK,
+ EGL_WINDOW_BIT },
+ { EGL_TRANSPARENT_TYPE, ATTRIB_TYPE_ENUM,
+ ATTRIB_CRITERION_EXACT,
+ EGL_NONE },
+ { EGL_TRANSPARENT_RED_VALUE, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_EXACT,
+ EGL_DONT_CARE },
+ { EGL_TRANSPARENT_GREEN_VALUE, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_EXACT,
+ EGL_DONT_CARE },
+ { EGL_TRANSPARENT_BLUE_VALUE, ATTRIB_TYPE_INTEGER,
+ ATTRIB_CRITERION_EXACT,
+ EGL_DONT_CARE },
+ /* these are not real attributes */
+ { EGL_MATCH_NATIVE_PIXMAP, ATTRIB_TYPE_PSEUDO,
+ ATTRIB_CRITERION_SPECIAL,
+ EGL_NONE },
+ /* there is a gap before EGL_SAMPLES */
+ { 0x3030, ATTRIB_TYPE_PSEUDO,
+ ATTRIB_CRITERION_IGNORE,
+ 0 },
+ { EGL_NONE, ATTRIB_TYPE_PSEUDO,
+ ATTRIB_CRITERION_IGNORE,
+ 0 },
+
+ { EGL_Y_INVERTED_NOK, ATTRIB_TYPE_BOOLEAN,
+ ATTRIB_CRITERION_EXACT,
+ EGL_DONT_CARE },
+
+};
+
+
+/**
+ * Return true if a config is valid. When for_matching is true,
+ * EGL_DONT_CARE is accepted as a valid attribute value, and checks
+ * for conflicting attribute values are skipped.
+ *
+ * Note that some attributes are platform-dependent and are not
+ * checked.
+ */
+EGLBoolean
+_eglValidateConfig(const _EGLConfig *conf, EGLBoolean for_matching)
+{
+ EGLint i, attr, val;
+ EGLBoolean valid = EGL_TRUE;
+ EGLint red_size = 0, green_size = 0, blue_size = 0, luminance_size = 0;
+ EGLint alpha_size = 0, buffer_size = 0;
+
+ /* all attributes should have been listed */
+ assert(ARRAY_SIZE(_eglValidationTable) == _EGL_CONFIG_NUM_ATTRIBS);
+
+ /* check attributes by their types */
+ for (i = 0; i < ARRAY_SIZE(_eglValidationTable); i++) {
+ EGLint mask;
+
+ attr = _eglValidationTable[i].attr;
+ val = GET_CONFIG_ATTRIB(conf, attr);
+
+ switch (_eglValidationTable[i].type) {
+ case ATTRIB_TYPE_INTEGER:
+ switch (attr) {
+ case EGL_CONFIG_ID:
+ /* config id must be positive */
+ if (val <= 0)
+ valid = EGL_FALSE;
+ break;
+ case EGL_SAMPLE_BUFFERS:
+ /* there can be at most 1 sample buffer */
+ if (val > 1)
+ valid = EGL_FALSE;
+ break;
+ case EGL_RED_SIZE:
+ red_size = val;
+ break;
+ case EGL_GREEN_SIZE:
+ green_size = val;
+ break;
+ case EGL_BLUE_SIZE:
+ blue_size = val;
+ break;
+ case EGL_LUMINANCE_SIZE:
+ luminance_size = val;
+ break;
+ case EGL_ALPHA_SIZE:
+ alpha_size = val;
+ break;
+ case EGL_BUFFER_SIZE:
+ buffer_size = val;
+ break;
+ }
+ if (val < 0)
+ valid = EGL_FALSE;
+ break;
+ case ATTRIB_TYPE_BOOLEAN:
+ if (val != EGL_TRUE && val != EGL_FALSE)
+ valid = EGL_FALSE;
+ break;
+ case ATTRIB_TYPE_ENUM:
+ switch (attr) {
+ case EGL_CONFIG_CAVEAT:
+ if (val != EGL_NONE && val != EGL_SLOW_CONFIG &&
+ val != EGL_NON_CONFORMANT_CONFIG)
+ valid = EGL_FALSE;
+ break;
+ case EGL_TRANSPARENT_TYPE:
+ if (val != EGL_NONE && val != EGL_TRANSPARENT_RGB)
+ valid = EGL_FALSE;
+ break;
+ case EGL_COLOR_BUFFER_TYPE:
+ if (val != EGL_RGB_BUFFER && val != EGL_LUMINANCE_BUFFER)
+ valid = EGL_FALSE;
+ break;
+ default:
+ assert(0);
+ break;
+ }
+ break;
+ case ATTRIB_TYPE_BITMASK:
+ switch (attr) {
+ case EGL_SURFACE_TYPE:
+ mask = EGL_PBUFFER_BIT |
+ EGL_PIXMAP_BIT |
+ EGL_WINDOW_BIT |
+ EGL_VG_COLORSPACE_LINEAR_BIT |
+ EGL_VG_ALPHA_FORMAT_PRE_BIT |
+ EGL_MULTISAMPLE_RESOLVE_BOX_BIT |
+ EGL_SWAP_BEHAVIOR_PRESERVED_BIT;
+ if (conf->Display->Extensions.MESA_screen_surface)
+ mask |= EGL_SCREEN_BIT_MESA;
+ break;
+ case EGL_RENDERABLE_TYPE:
+ case EGL_CONFORMANT:
+ mask = EGL_OPENGL_ES_BIT |
+ EGL_OPENVG_BIT |
+ EGL_OPENGL_ES2_BIT |
+ EGL_OPENGL_BIT;
+ break;
+ default:
+ assert(0);
+ break;
+ }
+ if (val & ~mask)
+ valid = EGL_FALSE;
+ break;
+ case ATTRIB_TYPE_PLATFORM:
+ /* unable to check platform-dependent attributes here */
+ break;
+ case ATTRIB_TYPE_PSEUDO:
+ /* pseudo attributes should not be set */
+ if (val != 0)
+ valid = EGL_FALSE;
+ break;
+ default:
+ assert(0);
+ break;
+ }
+
+ if (!valid && for_matching) {
+ /* accept EGL_DONT_CARE as a valid value */
+ if (val == EGL_DONT_CARE)
+ valid = EGL_TRUE;
+ if (_eglValidationTable[i].criterion == ATTRIB_CRITERION_SPECIAL)
+ valid = EGL_TRUE;
+ }
+ if (!valid) {
+ _eglLog(_EGL_DEBUG,
+ "attribute 0x%04x has an invalid value 0x%x", attr, val);
+ break;
+ }
+ }
+
+ /* any invalid attribute value should have been catched */
+ if (!valid || for_matching)
+ return valid;
+
+ /* now check for conflicting attribute values */
+
+ switch (GET_CONFIG_ATTRIB(conf, EGL_COLOR_BUFFER_TYPE)) {
+ case EGL_RGB_BUFFER:
+ if (luminance_size)
+ valid = EGL_FALSE;
+ if (red_size + green_size + blue_size + alpha_size != buffer_size)
+ valid = EGL_FALSE;
+ break;
+ case EGL_LUMINANCE_BUFFER:
+ if (red_size || green_size || blue_size)
+ valid = EGL_FALSE;
+ if (luminance_size + alpha_size != buffer_size)
+ valid = EGL_FALSE;
+ break;
+ }
+ if (!valid) {
+ _eglLog(_EGL_DEBUG, "conflicting color buffer type and channel sizes");
+ return EGL_FALSE;
+ }
+
+ val = GET_CONFIG_ATTRIB(conf, EGL_SAMPLE_BUFFERS);
+ if (!val && GET_CONFIG_ATTRIB(conf, EGL_SAMPLES))
+ valid = EGL_FALSE;
+ if (!valid) {
+ _eglLog(_EGL_DEBUG, "conflicting samples and sample buffers");
+ return EGL_FALSE;
+ }
+
+ val = GET_CONFIG_ATTRIB(conf, EGL_SURFACE_TYPE);
+ if (!(val & EGL_WINDOW_BIT)) {
+ if (GET_CONFIG_ATTRIB(conf, EGL_NATIVE_VISUAL_ID) != 0 ||
+ GET_CONFIG_ATTRIB(conf, EGL_NATIVE_VISUAL_TYPE) != EGL_NONE)
+ valid = EGL_FALSE;
+ }
+ if (!(val & EGL_PBUFFER_BIT)) {
+ if (GET_CONFIG_ATTRIB(conf, EGL_BIND_TO_TEXTURE_RGB) ||
+ GET_CONFIG_ATTRIB(conf, EGL_BIND_TO_TEXTURE_RGBA))
+ valid = EGL_FALSE;
+ }
+ if (!valid) {
+ _eglLog(_EGL_DEBUG, "conflicting surface type and native visual/texture binding");
+ return EGL_FALSE;
+ }
+
+ return valid;
+}
+
+
+/**
+ * Return true if a config matches the criteria. This and
+ * _eglParseConfigAttribList together implement the algorithm
+ * described in "Selection of EGLConfigs".
+ *
+ * Note that attributes that are special (currently, only
+ * EGL_MATCH_NATIVE_PIXMAP) are ignored.
+ */
+EGLBoolean
+_eglMatchConfig(const _EGLConfig *conf, const _EGLConfig *criteria)
+{
+ EGLint attr, val, i;
+ EGLBoolean matched = EGL_TRUE;
+
+ for (i = 0; i < ARRAY_SIZE(_eglValidationTable); i++) {
+ EGLint cmp;
+ if (_eglValidationTable[i].criterion == ATTRIB_CRITERION_IGNORE)
+ continue;
+
+ attr = _eglValidationTable[i].attr;
+ cmp = GET_CONFIG_ATTRIB(criteria, attr);
+ if (cmp == EGL_DONT_CARE)
+ continue;
+
+ val = GET_CONFIG_ATTRIB(conf, attr);
+ switch (_eglValidationTable[i].criterion) {
+ case ATTRIB_CRITERION_EXACT:
+ if (val != cmp)
+ matched = EGL_FALSE;
+ break;
+ case ATTRIB_CRITERION_ATLEAST:
+ if (val < cmp)
+ matched = EGL_FALSE;
+ break;
+ case ATTRIB_CRITERION_MASK:
+ if ((val & cmp) != cmp)
+ matched = EGL_FALSE;
+ break;
+ case ATTRIB_CRITERION_SPECIAL:
+ /* ignored here */
+ break;
+ default:
+ assert(0);
+ break;
+ }
+
+ if (!matched) {
+#ifdef DEBUG
+ _eglLog(_EGL_DEBUG,
+ "the value (0x%x) of attribute 0x%04x did not meet the criteria (0x%x)",
+ val, attr, cmp);
+#endif
+ break;
+ }
+ }
+
+ return matched;
+}
+
+static INLINE EGLBoolean
+_eglIsConfigAttribValid(_EGLConfig *conf, EGLint attr)
+{
+ if (_eglIndexConfig(conf, attr) < 0)
+ return EGL_FALSE;
+
+ /* there are some holes in the range */
+ switch (attr) {
+ case 0x3030 /* a gap before EGL_SAMPLES */:
+ case EGL_NONE:
+#ifdef EGL_VERSION_1_4
+ case EGL_MATCH_NATIVE_PIXMAP:
+#endif
+ return EGL_FALSE;
+ case EGL_Y_INVERTED_NOK:
+ return conf->Display->Extensions.NOK_texture_from_pixmap;
+ default:
+ break;
+ }
+
+ return EGL_TRUE;
+}
+
+/**
+ * Initialize a criteria config from the given attribute list.
+ * Return EGL_FALSE if any of the attribute is invalid.
+ */
+EGLBoolean
+_eglParseConfigAttribList(_EGLConfig *conf, const EGLint *attrib_list)
+{
+ EGLint attr, val, i;
+ EGLint config_id = 0, level = 0;
+ EGLBoolean has_native_visual_type = EGL_FALSE;
+ EGLBoolean has_transparent_color = EGL_FALSE;
+
+ /* reset to default values */
+ for (i = 0; i < ARRAY_SIZE(_eglValidationTable); i++) {
+ attr = _eglValidationTable[i].attr;
+ val = _eglValidationTable[i].default_value;
+ SET_CONFIG_ATTRIB(conf, attr, val);
+ }
+
+ /* parse the list */
+ for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i += 2) {
+ attr = attrib_list[i];
+ val = attrib_list[i + 1];
+
+ if (!_eglIsConfigAttribValid(conf, attr))
+ return EGL_FALSE;
+
+ SET_CONFIG_ATTRIB(conf, attr, val);
+
+ /* rememeber some attributes for post-processing */
+ switch (attr) {
+ case EGL_CONFIG_ID:
+ config_id = val;
+ break;
+ case EGL_LEVEL:
+ level = val;
+ break;
+ case EGL_NATIVE_VISUAL_TYPE:
+ has_native_visual_type = EGL_TRUE;
+ break;
+ case EGL_TRANSPARENT_RED_VALUE:
+ case EGL_TRANSPARENT_GREEN_VALUE:
+ case EGL_TRANSPARENT_BLUE_VALUE:
+ has_transparent_color = EGL_TRUE;
+ break;
+ default:
+ break;
+ }
+ }
+
+ if (!_eglValidateConfig(conf, EGL_TRUE))
+ return EGL_FALSE;
+
+ /* the spec says that EGL_LEVEL cannot be EGL_DONT_CARE */
+ if (level == EGL_DONT_CARE)
+ return EGL_FALSE;
+
+ /* ignore other attributes when EGL_CONFIG_ID is given */
+ if (config_id > 0) {
+ _eglResetConfigKeys(conf, EGL_DONT_CARE);
+ SET_CONFIG_ATTRIB(conf, EGL_CONFIG_ID, config_id);
+ }
+ else {
+ if (has_native_visual_type) {
+ val = GET_CONFIG_ATTRIB(conf, EGL_SURFACE_TYPE);
+ if (!(val & EGL_WINDOW_BIT))
+ SET_CONFIG_ATTRIB(conf, EGL_NATIVE_VISUAL_TYPE, EGL_DONT_CARE);
+ }
+
+ if (has_transparent_color) {
+ val = GET_CONFIG_ATTRIB(conf, EGL_TRANSPARENT_TYPE);
+ if (val == EGL_NONE) {
+ SET_CONFIG_ATTRIB(conf, EGL_TRANSPARENT_RED_VALUE,
+ EGL_DONT_CARE);
+ SET_CONFIG_ATTRIB(conf, EGL_TRANSPARENT_GREEN_VALUE,
+ EGL_DONT_CARE);
+ SET_CONFIG_ATTRIB(conf, EGL_TRANSPARENT_BLUE_VALUE,
+ EGL_DONT_CARE);
+ }
+ }
+ }
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Decide the ordering of conf1 and conf2, under the given criteria.
+ * When compare_id is true, this implements the algorithm described
+ * in "Sorting of EGLConfigs". When compare_id is false,
+ * EGL_CONFIG_ID is not compared.
+ *
+ * It returns a negative integer if conf1 is considered to come
+ * before conf2; a positive integer if conf2 is considered to come
+ * before conf1; zero if the ordering cannot be decided.
+ *
+ * Note that EGL_NATIVE_VISUAL_TYPE is platform-dependent and is
+ * ignored here.
+ */
+EGLint
+_eglCompareConfigs(const _EGLConfig *conf1, const _EGLConfig *conf2,
+ const _EGLConfig *criteria, EGLBoolean compare_id)
+{
+ const EGLint compare_attribs[] = {
+ EGL_BUFFER_SIZE,
+ EGL_SAMPLE_BUFFERS,
+ EGL_SAMPLES,
+ EGL_DEPTH_SIZE,
+ EGL_STENCIL_SIZE,
+ EGL_ALPHA_MASK_SIZE,
+ };
+ EGLint val1, val2;
+ EGLBoolean rgb_buffer;
+ EGLint i;
+
+ if (conf1 == conf2)
+ return 0;
+
+ /* the enum values have the desired ordering */
+ assert(EGL_NONE < EGL_SLOW_CONFIG);
+ assert(EGL_SLOW_CONFIG < EGL_NON_CONFORMANT_CONFIG);
+ val1 = GET_CONFIG_ATTRIB(conf1, EGL_CONFIG_CAVEAT);
+ val2 = GET_CONFIG_ATTRIB(conf2, EGL_CONFIG_CAVEAT);
+ if (val1 != val2)
+ return (val1 - val2);
+
+ /* the enum values have the desired ordering */
+ assert(EGL_RGB_BUFFER < EGL_LUMINANCE_BUFFER);
+ val1 = GET_CONFIG_ATTRIB(conf1, EGL_COLOR_BUFFER_TYPE);
+ val2 = GET_CONFIG_ATTRIB(conf2, EGL_COLOR_BUFFER_TYPE);
+ if (val1 != val2)
+ return (val1 - val2);
+ rgb_buffer = (val1 == EGL_RGB_BUFFER);
+
+ if (criteria) {
+ val1 = val2 = 0;
+ if (rgb_buffer) {
+ if (GET_CONFIG_ATTRIB(criteria, EGL_RED_SIZE) > 0) {
+ val1 += GET_CONFIG_ATTRIB(conf1, EGL_RED_SIZE);
+ val2 += GET_CONFIG_ATTRIB(conf2, EGL_RED_SIZE);
+ }
+ if (GET_CONFIG_ATTRIB(criteria, EGL_GREEN_SIZE) > 0) {
+ val1 += GET_CONFIG_ATTRIB(conf1, EGL_GREEN_SIZE);
+ val2 += GET_CONFIG_ATTRIB(conf2, EGL_GREEN_SIZE);
+ }
+ if (GET_CONFIG_ATTRIB(criteria, EGL_BLUE_SIZE) > 0) {
+ val1 += GET_CONFIG_ATTRIB(conf1, EGL_BLUE_SIZE);
+ val2 += GET_CONFIG_ATTRIB(conf2, EGL_BLUE_SIZE);
+ }
+ }
+ else {
+ if (GET_CONFIG_ATTRIB(criteria, EGL_LUMINANCE_SIZE) > 0) {
+ val1 += GET_CONFIG_ATTRIB(conf1, EGL_LUMINANCE_SIZE);
+ val2 += GET_CONFIG_ATTRIB(conf2, EGL_LUMINANCE_SIZE);
+ }
+ }
+ if (GET_CONFIG_ATTRIB(criteria, EGL_ALPHA_SIZE) > 0) {
+ val1 += GET_CONFIG_ATTRIB(conf1, EGL_ALPHA_SIZE);
+ val2 += GET_CONFIG_ATTRIB(conf2, EGL_ALPHA_SIZE);
+ }
+ }
+ else {
+ /* assume the default criteria, which gives no specific ordering */
+ val1 = val2 = 0;
+ }
+
+ /* for color bits, larger one is preferred */
+ if (val1 != val2)
+ return (val2 - val1);
+
+ for (i = 0; i < ARRAY_SIZE(compare_attribs); i++) {
+ val1 = GET_CONFIG_ATTRIB(conf1, compare_attribs[i]);
+ val2 = GET_CONFIG_ATTRIB(conf2, compare_attribs[i]);
+ if (val1 != val2)
+ return (val1 - val2);
+ }
+
+ /* EGL_NATIVE_VISUAL_TYPE cannot be compared here */
+
+ if (compare_id) {
+ val1 = GET_CONFIG_ATTRIB(conf1, EGL_CONFIG_ID);
+ val2 = GET_CONFIG_ATTRIB(conf2, EGL_CONFIG_ID);
+ assert(val1 != val2);
+ }
+ else {
+ val1 = val2 = 0;
+ }
+
+ return (val1 - val2);
+}
+
+
+static INLINE
+void _eglSwapConfigs(const _EGLConfig **conf1, const _EGLConfig **conf2)
+{
+ const _EGLConfig *tmp = *conf1;
+ *conf1 = *conf2;
+ *conf2 = tmp;
+}
+
+
+/**
+ * Quick sort an array of configs. This differs from the standard
+ * qsort() in that the compare function accepts an additional
+ * argument.
+ */
+void
+_eglSortConfigs(const _EGLConfig **configs, EGLint count,
+ EGLint (*compare)(const _EGLConfig *, const _EGLConfig *,
+ void *),
+ void *priv_data)
+{
+ const EGLint pivot = 0;
+ EGLint i, j;
+
+ if (count <= 1)
+ return;
+
+ _eglSwapConfigs(&configs[pivot], &configs[count / 2]);
+ i = 1;
+ j = count - 1;
+ do {
+ while (i < count && compare(configs[i], configs[pivot], priv_data) < 0)
+ i++;
+ while (compare(configs[j], configs[pivot], priv_data) > 0)
+ j--;
+ if (i < j) {
+ _eglSwapConfigs(&configs[i], &configs[j]);
+ i++;
+ j--;
+ }
+ else if (i == j) {
+ i++;
+ j--;
+ break;
+ }
+ } while (i <= j);
+ _eglSwapConfigs(&configs[pivot], &configs[j]);
+
+ _eglSortConfigs(configs, j, compare, priv_data);
+ _eglSortConfigs(configs + i, count - i, compare, priv_data);
+}
+
+
+static int
+_eglFallbackCompare(const _EGLConfig *conf1, const _EGLConfig *conf2,
+ void *priv_data)
+{
+ const _EGLConfig *criteria = (const _EGLConfig *) priv_data;
+ return _eglCompareConfigs(conf1, conf2, criteria, EGL_TRUE);
+}
+
+
+/**
+ * Typical fallback routine for eglChooseConfig
+ */
+EGLBoolean
+_eglChooseConfig(_EGLDriver *drv, _EGLDisplay *disp, const EGLint *attrib_list,
+ EGLConfig *configs, EGLint config_size, EGLint *num_configs)
+{
+ _EGLConfig **configList, criteria;
+ EGLint i, count;
+
+ if (!num_configs)
+ return _eglError(EGL_BAD_PARAMETER, "eglChooseConfigs");
+
+ _eglInitConfig(&criteria, disp, 0);
+ if (!_eglParseConfigAttribList(&criteria, attrib_list))
+ return _eglError(EGL_BAD_ATTRIBUTE, "eglChooseConfig");
+
+ /* allocate array of config pointers */
+ configList = (_EGLConfig **)
+ malloc(disp->NumConfigs * sizeof(_EGLConfig *));
+ if (!configList)
+ return _eglError(EGL_BAD_ALLOC, "eglChooseConfig(out of memory)");
+
+ /* perform selection of configs */
+ count = 0;
+ for (i = 0; i < disp->NumConfigs; i++) {
+ if (_eglMatchConfig(disp->Configs[i], &criteria))
+ configList[count++] = disp->Configs[i];
+ }
+
+ /* perform sorting of configs */
+ if (configs && count) {
+ _eglSortConfigs((const _EGLConfig **) configList, count,
+ _eglFallbackCompare, (void *) &criteria);
+ count = MIN2(count, config_size);
+ for (i = 0; i < count; i++)
+ configs[i] = _eglGetConfigHandle(configList[i]);
+ }
+
+ free(configList);
+
+ *num_configs = count;
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Fallback for eglGetConfigAttrib.
+ */
+EGLBoolean
+_eglGetConfigAttrib(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
+ EGLint attribute, EGLint *value)
+{
+ if (!_eglIsConfigAttribValid(conf, attribute))
+ return _eglError(EGL_BAD_ATTRIBUTE, "eglGetConfigAttrib");
+ if (!value)
+ return _eglError(EGL_BAD_PARAMETER, "eglGetConfigAttrib");
+
+ *value = GET_CONFIG_ATTRIB(conf, attribute);
+ return EGL_TRUE;
+}
+
+
+/**
+ * Fallback for eglGetConfigs.
+ */
+EGLBoolean
+_eglGetConfigs(_EGLDriver *drv, _EGLDisplay *disp, EGLConfig *configs,
+ EGLint config_size, EGLint *num_config)
+{
+ if (!num_config)
+ return _eglError(EGL_BAD_PARAMETER, "eglGetConfigs");
+
+ if (configs) {
+ EGLint i;
+ *num_config = MIN2(disp->NumConfigs, config_size);
+ for (i = 0; i < *num_config; i++)
+ configs[i] = _eglGetConfigHandle(disp->Configs[i]);
+ }
+ else {
+ /* just return total number of supported configs */
+ *num_config = disp->NumConfigs;
+ }
+
+ return EGL_TRUE;
+}
diff --git a/src/egl/main/eglconfig.h b/src/egl/main/eglconfig.h
new file mode 100644
index 0000000000..ca63c40d3d
--- /dev/null
+++ b/src/egl/main/eglconfig.h
@@ -0,0 +1,172 @@
+#ifndef EGLCONFIG_INCLUDED
+#define EGLCONFIG_INCLUDED
+
+
+#include <assert.h>
+#include "egltypedefs.h"
+
+
+#define _EGL_CONFIG_FIRST_ATTRIB EGL_BUFFER_SIZE
+#define _EGL_CONFIG_LAST_ATTRIB EGL_CONFORMANT
+#define _EGL_CONFIG_NUM_CONTIGUOUS_ATTRIBS \
+ (_EGL_CONFIG_LAST_ATTRIB - _EGL_CONFIG_FIRST_ATTRIB + 1)
+
+/* Attributes outside the contiguous block:
+ *
+ * EGL_Y_INVERTED_NOK
+ */
+#define _EGL_CONFIG_FIRST_EXTRA_ATTRIB _EGL_CONFIG_NUM_CONTIGUOUS_ATTRIBS
+#define _EGL_CONFIG_NUM_EXTRA_ATTRIBS 1
+
+#define _EGL_CONFIG_NUM_ATTRIBS \
+ _EGL_CONFIG_NUM_CONTIGUOUS_ATTRIBS + _EGL_CONFIG_NUM_EXTRA_ATTRIBS
+
+
+struct _egl_config
+{
+ _EGLDisplay *Display;
+ EGLint Storage[_EGL_CONFIG_NUM_ATTRIBS];
+};
+
+
+/**
+ * Macros for source level compatibility.
+ */
+#define SET_CONFIG_ATTRIB(CONF, ATTR, VAL) _eglSetConfigKey(CONF, ATTR, VAL)
+#define GET_CONFIG_ATTRIB(CONF, ATTR) _eglGetConfigKey(CONF, ATTR)
+
+
+/**
+ * Given a key, return an index into the storage of the config.
+ * Return -1 if the key is invalid.
+ */
+static INLINE EGLint
+_eglIndexConfig(const _EGLConfig *conf, EGLint key)
+{
+ (void) conf;
+ if (key >= _EGL_CONFIG_FIRST_ATTRIB &&
+ key < _EGL_CONFIG_FIRST_ATTRIB + _EGL_CONFIG_NUM_CONTIGUOUS_ATTRIBS)
+ return key - _EGL_CONFIG_FIRST_ATTRIB;
+
+ switch (key) {
+ case EGL_Y_INVERTED_NOK:
+ return _EGL_CONFIG_FIRST_EXTRA_ATTRIB;
+ default:
+ return -1;
+ }
+}
+
+
+/**
+ * Reset all keys in the config to a given value.
+ */
+static INLINE void
+_eglResetConfigKeys(_EGLConfig *conf, EGLint val)
+{
+ EGLint i;
+ for (i = 0; i < _EGL_CONFIG_NUM_ATTRIBS; i++)
+ conf->Storage[i] = val;
+}
+
+
+/**
+ * Update a config for a given key.
+ *
+ * Note that a valid key is not necessarily a valid attribute. There are gaps
+ * in the attribute enums. The separation is to catch application errors.
+ * Drivers should never set a key that is an invalid attribute.
+ */
+static INLINE void
+_eglSetConfigKey(_EGLConfig *conf, EGLint key, EGLint val)
+{
+ EGLint idx = _eglIndexConfig(conf, key);
+ assert(idx >= 0);
+ conf->Storage[idx] = val;
+}
+
+
+/**
+ * Return the value for a given key.
+ */
+static INLINE EGLint
+_eglGetConfigKey(const _EGLConfig *conf, EGLint key)
+{
+ EGLint idx = _eglIndexConfig(conf, key);
+ assert(idx >= 0);
+ return conf->Storage[idx];
+}
+
+
+PUBLIC void
+_eglInitConfig(_EGLConfig *config, _EGLDisplay *dpy, EGLint id);
+
+
+PUBLIC EGLConfig
+_eglAddConfig(_EGLDisplay *dpy, _EGLConfig *conf);
+
+
+extern EGLBoolean
+_eglCheckConfigHandle(EGLConfig config, _EGLDisplay *dpy);
+
+
+/**
+ * Lookup a handle to find the linked config.
+ * Return NULL if the handle has no corresponding linked config.
+ */
+static INLINE _EGLConfig *
+_eglLookupConfig(EGLConfig config, _EGLDisplay *dpy)
+{
+ _EGLConfig *conf = (_EGLConfig *) config;
+ if (!_eglCheckConfigHandle(config, dpy))
+ conf = NULL;
+ return conf;
+}
+
+
+/**
+ * Return the handle of a linked config, or NULL.
+ */
+static INLINE EGLConfig
+_eglGetConfigHandle(_EGLConfig *conf)
+{
+ return (EGLConfig) ((conf && conf->Display) ? conf : NULL);
+}
+
+
+PUBLIC EGLBoolean
+_eglValidateConfig(const _EGLConfig *conf, EGLBoolean for_matching);
+
+
+PUBLIC EGLBoolean
+_eglMatchConfig(const _EGLConfig *conf, const _EGLConfig *criteria);
+
+
+PUBLIC EGLBoolean
+_eglParseConfigAttribList(_EGLConfig *conf, const EGLint *attrib_list);
+
+
+PUBLIC EGLint
+_eglCompareConfigs(const _EGLConfig *conf1, const _EGLConfig *conf2,
+ const _EGLConfig *criteria, EGLBoolean compare_id);
+
+
+PUBLIC void
+_eglSortConfigs(const _EGLConfig **configs, EGLint count,
+ EGLint (*compare)(const _EGLConfig *, const _EGLConfig *,
+ void *),
+ void *priv_data);
+
+
+extern EGLBoolean
+_eglChooseConfig(_EGLDriver *drv, _EGLDisplay *dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config);
+
+
+extern EGLBoolean
+_eglGetConfigAttrib(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, EGLint attribute, EGLint *value);
+
+
+extern EGLBoolean
+_eglGetConfigs(_EGLDriver *drv, _EGLDisplay *dpy, EGLConfig *configs, EGLint config_size, EGLint *num_config);
+
+
+#endif /* EGLCONFIG_INCLUDED */
diff --git a/src/egl/main/eglconfigutil.c b/src/egl/main/eglconfigutil.c
new file mode 100644
index 0000000000..e416b190f0
--- /dev/null
+++ b/src/egl/main/eglconfigutil.c
@@ -0,0 +1,128 @@
+/**
+ * Extra utility functions related to EGL configs.
+ */
+
+
+#include <stdlib.h>
+#include <string.h>
+#include "eglconfigutil.h"
+
+
+/**
+ * Convert an _EGLConfig to a __GLcontextModes object.
+ * NOTE: This routine may be incomplete - we're only making sure that
+ * the fields needed by Mesa (for _mesa_create_context/framebuffer) are
+ * set correctly.
+ */
+void
+_eglConfigToContextModesRec(const _EGLConfig *config, __GLcontextModes *mode)
+{
+ memset(mode, 0, sizeof(*mode));
+
+ mode->rgbMode = GL_TRUE; /* no color index */
+ mode->colorIndexMode = GL_FALSE;
+ mode->doubleBufferMode = GL_TRUE; /* always DB for now */
+ mode->stereoMode = GL_FALSE;
+
+ mode->redBits = GET_CONFIG_ATTRIB(config, EGL_RED_SIZE);
+ mode->greenBits = GET_CONFIG_ATTRIB(config, EGL_GREEN_SIZE);
+ mode->blueBits = GET_CONFIG_ATTRIB(config, EGL_BLUE_SIZE);
+ mode->alphaBits = GET_CONFIG_ATTRIB(config, EGL_ALPHA_SIZE);
+ mode->rgbBits = GET_CONFIG_ATTRIB(config, EGL_BUFFER_SIZE);
+
+ /* no rgba masks - fix? */
+
+ mode->depthBits = GET_CONFIG_ATTRIB(config, EGL_DEPTH_SIZE);
+ mode->haveDepthBuffer = mode->depthBits > 0;
+
+ mode->stencilBits = GET_CONFIG_ATTRIB(config, EGL_STENCIL_SIZE);
+ mode->haveStencilBuffer = mode->stencilBits > 0;
+
+ /* no accum */
+
+ mode->level = GET_CONFIG_ATTRIB(config, EGL_LEVEL);
+ mode->samples = GET_CONFIG_ATTRIB(config, EGL_SAMPLES);
+ mode->sampleBuffers = GET_CONFIG_ATTRIB(config, EGL_SAMPLE_BUFFERS);
+
+ /* surface type - not really needed */
+ mode->visualType = GLX_TRUE_COLOR;
+ mode->renderType = GLX_RGBA_BIT;
+}
+
+
+/**
+ * Convert a __GLcontextModes object to an _EGLConfig.
+ */
+EGLBoolean
+_eglConfigFromContextModesRec(_EGLConfig *conf, const __GLcontextModes *m,
+ EGLint conformant, EGLint renderable_type)
+{
+ EGLint config_caveat, surface_type;
+
+ /* must be RGBA */
+ if (!m->rgbMode || !(m->renderType & GLX_RGBA_BIT))
+ return EGL_FALSE;
+
+ config_caveat = EGL_NONE;
+ if (m->visualRating == GLX_SLOW_CONFIG)
+ config_caveat = EGL_SLOW_CONFIG;
+
+ if (m->visualRating == GLX_NON_CONFORMANT_CONFIG)
+ conformant &= ~EGL_OPENGL_BIT;
+ if (!(conformant & EGL_OPENGL_ES_BIT))
+ config_caveat = EGL_NON_CONFORMANT_CONFIG;
+
+ surface_type = 0;
+ if (m->drawableType & GLX_WINDOW_BIT)
+ surface_type |= EGL_WINDOW_BIT;
+ if (m->drawableType & GLX_PIXMAP_BIT)
+ surface_type |= EGL_PIXMAP_BIT;
+ if (m->drawableType & GLX_PBUFFER_BIT)
+ surface_type |= EGL_PBUFFER_BIT;
+
+ SET_CONFIG_ATTRIB(conf, EGL_BUFFER_SIZE, m->rgbBits);
+ SET_CONFIG_ATTRIB(conf, EGL_RED_SIZE, m->redBits);
+ SET_CONFIG_ATTRIB(conf, EGL_GREEN_SIZE, m->greenBits);
+ SET_CONFIG_ATTRIB(conf, EGL_BLUE_SIZE, m->blueBits);
+ SET_CONFIG_ATTRIB(conf, EGL_ALPHA_SIZE, m->alphaBits);
+
+ SET_CONFIG_ATTRIB(conf, EGL_BIND_TO_TEXTURE_RGB, m->bindToTextureRgb);
+ SET_CONFIG_ATTRIB(conf, EGL_BIND_TO_TEXTURE_RGBA, m->bindToTextureRgba);
+ SET_CONFIG_ATTRIB(conf, EGL_COLOR_BUFFER_TYPE, EGL_RGB_BUFFER);
+ SET_CONFIG_ATTRIB(conf, EGL_CONFIG_CAVEAT, config_caveat);
+
+ SET_CONFIG_ATTRIB(conf, EGL_CONFORMANT, conformant);
+ SET_CONFIG_ATTRIB(conf, EGL_DEPTH_SIZE, m->depthBits);
+ SET_CONFIG_ATTRIB(conf, EGL_LEVEL, m->level);
+ SET_CONFIG_ATTRIB(conf, EGL_MAX_PBUFFER_WIDTH, m->maxPbufferWidth);
+ SET_CONFIG_ATTRIB(conf, EGL_MAX_PBUFFER_HEIGHT, m->maxPbufferHeight);
+ SET_CONFIG_ATTRIB(conf, EGL_MAX_PBUFFER_PIXELS, m->maxPbufferPixels);
+
+ SET_CONFIG_ATTRIB(conf, EGL_NATIVE_RENDERABLE, m->xRenderable);
+ SET_CONFIG_ATTRIB(conf, EGL_NATIVE_VISUAL_ID, m->visualID);
+
+ if (m->visualType != GLX_NONE)
+ SET_CONFIG_ATTRIB(conf, EGL_NATIVE_VISUAL_TYPE, m->visualType);
+ else
+ SET_CONFIG_ATTRIB(conf, EGL_NATIVE_VISUAL_TYPE, EGL_NONE);
+
+ SET_CONFIG_ATTRIB(conf, EGL_RENDERABLE_TYPE, renderable_type);
+ SET_CONFIG_ATTRIB(conf, EGL_SAMPLE_BUFFERS, m->sampleBuffers);
+ SET_CONFIG_ATTRIB(conf, EGL_SAMPLES, m->samples);
+ SET_CONFIG_ATTRIB(conf, EGL_STENCIL_SIZE, m->stencilBits);
+
+ SET_CONFIG_ATTRIB(conf, EGL_SURFACE_TYPE, surface_type);
+
+ /* what to do with GLX_TRANSPARENT_INDEX? */
+ if (m->transparentPixel == GLX_TRANSPARENT_RGB) {
+ SET_CONFIG_ATTRIB(conf, EGL_TRANSPARENT_TYPE, EGL_TRANSPARENT_RGB);
+ SET_CONFIG_ATTRIB(conf, EGL_TRANSPARENT_RED_VALUE, m->transparentRed);
+ SET_CONFIG_ATTRIB(conf, EGL_TRANSPARENT_GREEN_VALUE, m->transparentGreen);
+ SET_CONFIG_ATTRIB(conf, EGL_TRANSPARENT_BLUE_VALUE, m->transparentBlue);
+ }
+ else {
+ SET_CONFIG_ATTRIB(conf, EGL_TRANSPARENT_TYPE, EGL_NONE);
+ }
+
+ return EGL_TRUE;
+}
diff --git a/src/egl/main/eglconfigutil.h b/src/egl/main/eglconfigutil.h
new file mode 100644
index 0000000000..c6f4819960
--- /dev/null
+++ b/src/egl/main/eglconfigutil.h
@@ -0,0 +1,19 @@
+#ifndef EGLCONFIGUTIL_INCLUDED
+#define EGLCONFIGUTIL_INCLUDED
+
+
+#include "GL/gl.h"
+#include "GL/internal/glcore.h"
+#include "eglconfig.h"
+
+
+PUBLIC void
+_eglConfigToContextModesRec(const _EGLConfig *config, __GLcontextModes *mode);
+
+
+PUBLIC EGLBoolean
+_eglConfigFromContextModesRec(_EGLConfig *conf, const __GLcontextModes *m,
+ EGLint conformant, EGLint renderable_type);
+
+
+#endif /* EGLCONFIGUTIL_INCLUDED */
diff --git a/src/egl/main/eglcontext.c b/src/egl/main/eglcontext.c
new file mode 100644
index 0000000000..d5a1e79a99
--- /dev/null
+++ b/src/egl/main/eglcontext.c
@@ -0,0 +1,424 @@
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include "eglconfig.h"
+#include "eglcontext.h"
+#include "egldisplay.h"
+#include "eglcurrent.h"
+#include "eglsurface.h"
+#include "egllog.h"
+
+
+/**
+ * Return the API bit (one of EGL_xxx_BIT) of the context.
+ */
+static EGLint
+_eglGetContextAPIBit(_EGLContext *ctx)
+{
+ EGLint bit = 0;
+
+ switch (ctx->ClientAPI) {
+ case EGL_OPENGL_ES_API:
+ switch (ctx->ClientVersion) {
+ case 1:
+ bit = EGL_OPENGL_ES_BIT;
+ break;
+ case 2:
+ bit = EGL_OPENGL_ES2_BIT;
+ break;
+ default:
+ break;
+ }
+ break;
+ case EGL_OPENVG_API:
+ bit = EGL_OPENVG_BIT;
+ break;
+ case EGL_OPENGL_API:
+ bit = EGL_OPENGL_BIT;
+ break;
+ default:
+ break;
+ }
+
+ return bit;
+}
+
+
+/**
+ * Parse the list of context attributes and return the proper error code.
+ */
+static EGLint
+_eglParseContextAttribList(_EGLContext *ctx, const EGLint *attrib_list)
+{
+ EGLenum api = ctx->ClientAPI;
+ EGLint i, err = EGL_SUCCESS;
+
+ if (!attrib_list)
+ return EGL_SUCCESS;
+
+ for (i = 0; attrib_list[i] != EGL_NONE; i++) {
+ EGLint attr = attrib_list[i++];
+ EGLint val = attrib_list[i];
+
+ switch (attr) {
+ case EGL_CONTEXT_CLIENT_VERSION:
+ if (api != EGL_OPENGL_ES_API) {
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+ if (val != 1 && val != 2) {
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+ ctx->ClientVersion = val;
+ break;
+ default:
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+
+ if (err != EGL_SUCCESS) {
+ _eglLog(_EGL_DEBUG, "bad context attribute 0x%04x", attr);
+ break;
+ }
+ }
+
+ if (err == EGL_SUCCESS) {
+ EGLint renderable_type, api_bit;
+
+ renderable_type = GET_CONFIG_ATTRIB(ctx->Config, EGL_RENDERABLE_TYPE);
+ api_bit = _eglGetContextAPIBit(ctx);
+ if (!(renderable_type & api_bit))
+ err = EGL_BAD_CONFIG;
+ }
+
+ return err;
+}
+
+
+/**
+ * Initialize the given _EGLContext object to defaults and/or the values
+ * in the attrib_list.
+ */
+EGLBoolean
+_eglInitContext(_EGLContext *ctx, _EGLDisplay *dpy, _EGLConfig *conf,
+ const EGLint *attrib_list)
+{
+ const EGLenum api = eglQueryAPI();
+ EGLint err;
+
+ if (api == EGL_NONE) {
+ _eglError(EGL_BAD_MATCH, "eglCreateContext(no client API)");
+ return EGL_FALSE;
+ }
+
+ memset(ctx, 0, sizeof(_EGLContext));
+ ctx->Resource.Display = dpy;
+ ctx->ClientAPI = api;
+ ctx->Config = conf;
+ ctx->WindowRenderBuffer = EGL_NONE;
+
+ ctx->ClientVersion = 1; /* the default, per EGL spec */
+
+ err = _eglParseContextAttribList(ctx, attrib_list);
+ if (err != EGL_SUCCESS)
+ return _eglError(err, "eglCreateContext");
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Just a placeholder/demo function. Real driver will never use this!
+ */
+_EGLContext *
+_eglCreateContext(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
+ _EGLContext *share_list, const EGLint *attrib_list)
+{
+ return NULL;
+}
+
+
+/**
+ * Default fallback routine - drivers should usually override this.
+ */
+EGLBoolean
+_eglDestroyContext(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx)
+{
+ if (!_eglIsContextBound(ctx))
+ free(ctx);
+ return EGL_TRUE;
+}
+
+
+#ifdef EGL_VERSION_1_2
+static EGLint
+_eglQueryContextRenderBuffer(_EGLContext *ctx)
+{
+ _EGLSurface *surf = ctx->DrawSurface;
+ EGLint rb;
+
+ if (!surf)
+ return EGL_NONE;
+ if (surf->Type == EGL_WINDOW_BIT && ctx->WindowRenderBuffer != EGL_NONE)
+ rb = ctx->WindowRenderBuffer;
+ else
+ rb = surf->RenderBuffer;
+ return rb;
+}
+#endif /* EGL_VERSION_1_2 */
+
+
+EGLBoolean
+_eglQueryContext(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *c,
+ EGLint attribute, EGLint *value)
+{
+ (void) drv;
+ (void) dpy;
+
+ if (!value)
+ return _eglError(EGL_BAD_PARAMETER, "eglQueryContext");
+
+ switch (attribute) {
+ case EGL_CONFIG_ID:
+ *value = GET_CONFIG_ATTRIB(c->Config, EGL_CONFIG_ID);
+ break;
+ case EGL_CONTEXT_CLIENT_VERSION:
+ *value = c->ClientVersion;
+ break;
+#ifdef EGL_VERSION_1_2
+ case EGL_CONTEXT_CLIENT_TYPE:
+ *value = c->ClientAPI;
+ break;
+ case EGL_RENDER_BUFFER:
+ *value = _eglQueryContextRenderBuffer(c);
+ break;
+#endif /* EGL_VERSION_1_2 */
+ default:
+ return _eglError(EGL_BAD_ATTRIBUTE, "eglQueryContext");
+ }
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Bind the context to the surfaces. Return the surfaces that are "orphaned".
+ * That is, when the context is not NULL, return the surfaces it previously
+ * bound to; when the context is NULL, the same surfaces are returned.
+ */
+static void
+_eglBindContextToSurfaces(_EGLContext *newCtx,
+ _EGLSurface **draw, _EGLSurface **read)
+{
+ _EGLSurface *newDraw = *draw, *newRead = *read;
+ _EGLContext *oldCtx;
+
+ /*
+ * The goal is to bind a newCtx to newDraw. Since newDraw may already have
+ * a binding context (oldCtx), and newCtx may already be bound to another
+ * surface (oldDraw), the old bindings are broken first and the new one is
+ * created.
+ */
+ oldCtx = newDraw->CurrentContext;
+ if (newCtx != oldCtx) {
+ if (oldCtx) {
+ assert(oldCtx->DrawSurface == newDraw);
+ oldCtx->DrawSurface = NULL;
+ }
+
+ if (newCtx) {
+ _EGLSurface *oldDraw = newCtx->DrawSurface;
+ if (oldDraw)
+ oldDraw->CurrentContext = NULL;
+
+ newCtx->DrawSurface = newDraw;
+ *draw = oldDraw;
+ }
+
+ newDraw->CurrentContext = newCtx;
+ }
+
+ /* likewise */
+ if (newRead != newDraw)
+ oldCtx = newRead->CurrentContext;
+ if (newCtx != oldCtx) {
+ if (oldCtx) {
+ assert(oldCtx->ReadSurface == newRead);
+ oldCtx->ReadSurface = NULL;
+ }
+
+ if (newCtx) {
+ _EGLSurface *oldRead = newCtx->ReadSurface;
+ if (oldRead)
+ oldRead->CurrentContext = NULL;
+
+ newCtx->ReadSurface = newRead;
+ *read = oldRead;
+ }
+
+ newRead->CurrentContext = newCtx;
+ }
+}
+
+
+/**
+ * Bind the context to the thread and return the previous context.
+ *
+ * Note that the context may be NULL.
+ */
+static _EGLContext *
+_eglBindContextToThread(_EGLContext *ctx, _EGLThreadInfo *t)
+{
+ EGLint apiIndex;
+ _EGLContext *oldCtx;
+
+ apiIndex = (ctx) ?
+ _eglConvertApiToIndex(ctx->ClientAPI) : t->CurrentAPIIndex;
+
+ oldCtx = t->CurrentContexts[apiIndex];
+ if (ctx != oldCtx) {
+ if (oldCtx)
+ oldCtx->Binding = NULL;
+ if (ctx)
+ ctx->Binding = t;
+
+ t->CurrentContexts[apiIndex] = ctx;
+ }
+
+ return oldCtx;
+}
+
+
+/**
+ * Return true if the given context and surfaces can be made current.
+ */
+static EGLBoolean
+_eglCheckMakeCurrent(_EGLContext *ctx, _EGLSurface *draw, _EGLSurface *read)
+{
+ _EGLThreadInfo *t = _eglGetCurrentThread();
+ EGLint conflict_api;
+
+ if (_eglIsCurrentThreadDummy())
+ return _eglError(EGL_BAD_ALLOC, "eglMakeCurrent");
+
+ /* this is easy */
+ if (!ctx) {
+ if (draw || read)
+ return _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
+ return EGL_TRUE;
+ }
+
+ /* ctx/draw/read must be all given */
+ if (draw == NULL || read == NULL)
+ return _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
+
+ /* context stealing from another thread is not allowed */
+ if (ctx->Binding && ctx->Binding != t)
+ return _eglError(EGL_BAD_ACCESS, "eglMakeCurrent");
+
+ /*
+ * The spec says
+ *
+ * "If ctx is current to some other thread, or if either draw or read are
+ * bound to contexts in another thread, an EGL_BAD_ACCESS error is
+ * generated."
+ *
+ * But it also says
+ *
+ * "at most one context may be bound to a particular surface at a given
+ * time"
+ *
+ * The latter is more restrictive so we can check only the latter case.
+ */
+ if ((draw->CurrentContext && draw->CurrentContext != ctx) ||
+ (read->CurrentContext && read->CurrentContext != ctx))
+ return _eglError(EGL_BAD_ACCESS, "eglMakeCurrent");
+
+ /* simply require the configs to be equal */
+ if (draw->Config != ctx->Config || read->Config != ctx->Config)
+ return _eglError(EGL_BAD_MATCH, "eglMakeCurrent");
+
+ switch (ctx->ClientAPI) {
+#ifdef EGL_VERSION_1_4
+ /* OpenGL and OpenGL ES are conflicting */
+ case EGL_OPENGL_ES_API:
+ conflict_api = EGL_OPENGL_API;
+ break;
+ case EGL_OPENGL_API:
+ conflict_api = EGL_OPENGL_ES_API;
+ break;
+#endif
+ default:
+ conflict_api = -1;
+ break;
+ }
+
+ if (conflict_api >= 0 && _eglGetAPIContext(conflict_api))
+ return _eglError(EGL_BAD_ACCESS, "eglMakeCurrent");
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Bind the context to the current thread and given surfaces. Return the
+ * previously bound context and the surfaces it bound to. Each argument is
+ * both input and output.
+ */
+EGLBoolean
+_eglBindContext(_EGLContext **ctx, _EGLSurface **draw, _EGLSurface **read)
+{
+ _EGLThreadInfo *t = _eglGetCurrentThread();
+ _EGLContext *newCtx = *ctx, *oldCtx;
+
+ if (!_eglCheckMakeCurrent(newCtx, *draw, *read))
+ return EGL_FALSE;
+
+ /* bind the new context */
+ oldCtx = _eglBindContextToThread(newCtx, t);
+
+ if (newCtx)
+ _eglBindContextToSurfaces(newCtx, draw, read);
+
+ /* unbind the old context from its binding surfaces */
+ if (oldCtx && oldCtx != newCtx) {
+ assert(!*draw && !*read);
+
+ *draw = oldCtx->DrawSurface;
+ *read = oldCtx->ReadSurface;
+ assert(*draw && *read);
+
+ _eglBindContextToSurfaces(NULL, draw, read);
+ }
+
+ *ctx = oldCtx;
+ /* draw and read have been updated in _eglBindContextToSurfaces */
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Just a placeholder/demo function. Drivers should override this.
+ */
+EGLBoolean
+_eglMakeCurrent(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *draw,
+ _EGLSurface *read, _EGLContext *ctx)
+{
+ return EGL_FALSE;
+}
+
+
+/**
+ * This is defined by the EGL_MESA_copy_context extension.
+ */
+EGLBoolean
+_eglCopyContextMESA(_EGLDriver *drv, EGLDisplay dpy, EGLContext source,
+ EGLContext dest, EGLint mask)
+{
+ /* This function will always have to be overridden/implemented in the
+ * device driver. If the driver is based on Mesa, use _mesa_copy_context().
+ */
+ return EGL_FALSE;
+}
diff --git a/src/egl/main/eglcontext.h b/src/egl/main/eglcontext.h
new file mode 100644
index 0000000000..cfe92dd9f5
--- /dev/null
+++ b/src/egl/main/eglcontext.h
@@ -0,0 +1,137 @@
+#ifndef EGLCONTEXT_INCLUDED
+#define EGLCONTEXT_INCLUDED
+
+
+#include "egltypedefs.h"
+#include "egldisplay.h"
+
+
+/**
+ * "Base" class for device driver contexts.
+ */
+struct _egl_context
+{
+ /* A context is a display resource */
+ _EGLResource Resource;
+
+ /* The bound status of the context */
+ _EGLThreadInfo *Binding;
+ _EGLSurface *DrawSurface;
+ _EGLSurface *ReadSurface;
+
+ _EGLConfig *Config;
+
+ EGLint ClientAPI; /**< EGL_OPENGL_ES_API, EGL_OPENGL_API, EGL_OPENVG_API */
+ EGLint ClientVersion; /**< 1 = OpenGLES 1.x, 2 = OpenGLES 2.x */
+
+ /* The real render buffer when a window surface is bound */
+ EGLint WindowRenderBuffer;
+};
+
+
+PUBLIC EGLBoolean
+_eglInitContext(_EGLContext *ctx, _EGLDisplay *dpy,
+ _EGLConfig *config, const EGLint *attrib_list);
+
+
+extern _EGLContext *
+_eglCreateContext(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, _EGLContext *share_list, const EGLint *attrib_list);
+
+
+extern EGLBoolean
+_eglDestroyContext(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx);
+
+
+extern EGLBoolean
+_eglQueryContext(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx, EGLint attribute, EGLint *value);
+
+
+PUBLIC EGLBoolean
+_eglBindContext(_EGLContext **ctx, _EGLSurface **draw, _EGLSurface **read);
+
+
+extern EGLBoolean
+_eglMakeCurrent(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *draw, _EGLSurface *read, _EGLContext *ctx);
+
+
+extern EGLBoolean
+_eglCopyContextMESA(_EGLDriver *drv, EGLDisplay dpy, EGLContext source, EGLContext dest, EGLint mask);
+
+
+/**
+ * Return true if the context is bound to a thread.
+ *
+ * The binding is considered a reference to the context. Drivers should not
+ * destroy a context when it is bound.
+ */
+static INLINE EGLBoolean
+_eglIsContextBound(_EGLContext *ctx)
+{
+ return (ctx->Binding != NULL);
+}
+
+
+/**
+ * Link a context to a display and return the handle of the link.
+ * The handle can be passed to client directly.
+ */
+static INLINE EGLContext
+_eglLinkContext(_EGLContext *ctx, _EGLDisplay *dpy)
+{
+ _eglLinkResource(&ctx->Resource, _EGL_RESOURCE_CONTEXT, dpy);
+ return (EGLContext) ctx;
+}
+
+
+/**
+ * Unlink a linked context from its display.
+ * Accessing an unlinked context should generate EGL_BAD_CONTEXT error.
+ */
+static INLINE void
+_eglUnlinkContext(_EGLContext *ctx)
+{
+ _eglUnlinkResource(&ctx->Resource, _EGL_RESOURCE_CONTEXT);
+}
+
+
+/**
+ * Lookup a handle to find the linked context.
+ * Return NULL if the handle has no corresponding linked context.
+ */
+static INLINE _EGLContext *
+_eglLookupContext(EGLContext context, _EGLDisplay *dpy)
+{
+ _EGLContext *ctx = (_EGLContext *) context;
+ if (!dpy || !_eglCheckResource((void *) ctx, _EGL_RESOURCE_CONTEXT, dpy))
+ ctx = NULL;
+ return ctx;
+}
+
+
+/**
+ * Return the handle of a linked context, or EGL_NO_CONTEXT.
+ */
+static INLINE EGLContext
+_eglGetContextHandle(_EGLContext *ctx)
+{
+ _EGLResource *res = (_EGLResource *) ctx;
+ return (res && _eglIsResourceLinked(res)) ?
+ (EGLContext) ctx : EGL_NO_CONTEXT;
+}
+
+
+/**
+ * Return true if the context is linked to a display.
+ *
+ * The link is considered a reference to the context (the display is owning the
+ * context). Drivers should not destroy a context when it is linked.
+ */
+static INLINE EGLBoolean
+_eglIsContextLinked(_EGLContext *ctx)
+{
+ _EGLResource *res = (_EGLResource *) ctx;
+ return (res && _eglIsResourceLinked(res));
+}
+
+
+#endif /* EGLCONTEXT_INCLUDED */
diff --git a/src/egl/main/eglcurrent.c b/src/egl/main/eglcurrent.c
new file mode 100644
index 0000000000..c697bf796d
--- /dev/null
+++ b/src/egl/main/eglcurrent.c
@@ -0,0 +1,316 @@
+#include <stdlib.h>
+#include <string.h>
+#include "eglglobals.h"
+#include "egllog.h"
+#include "eglmutex.h"
+#include "eglcurrent.h"
+
+
+/* This should be kept in sync with _eglInitThreadInfo() */
+#define _EGL_THREAD_INFO_INITIALIZER \
+ { EGL_SUCCESS, { NULL }, 0 }
+
+/* a fallback thread info to guarantee that every thread always has one */
+static _EGLThreadInfo dummy_thread = _EGL_THREAD_INFO_INITIALIZER;
+
+
+#ifdef GLX_USE_TLS
+static __thread const _EGLThreadInfo *_egl_TSD
+ __attribute__ ((tls_model("initial-exec")));
+
+static INLINE void _eglSetTSD(const _EGLThreadInfo *t)
+{
+ _egl_TSD = t;
+}
+
+static INLINE _EGLThreadInfo *_eglGetTSD(void)
+{
+ return (_EGLThreadInfo *) _egl_TSD;
+}
+
+static INLINE void _eglFiniTSD(void)
+{
+}
+
+static INLINE EGLBoolean _eglInitTSD(void (*dtor)(_EGLThreadInfo *))
+{
+ /* TODO destroy TSD */
+ (void) dtor;
+ (void) _eglFiniTSD;
+ return EGL_TRUE;
+}
+
+#elif PTHREADS
+#include <pthread.h>
+
+static _EGL_DECLARE_MUTEX(_egl_TSDMutex);
+static EGLBoolean _egl_TSDInitialized;
+static pthread_key_t _egl_TSD;
+static void (*_egl_FreeTSD)(_EGLThreadInfo *);
+
+static INLINE void _eglSetTSD(const _EGLThreadInfo *t)
+{
+ pthread_setspecific(_egl_TSD, (const void *) t);
+}
+
+static INLINE _EGLThreadInfo *_eglGetTSD(void)
+{
+ return (_EGLThreadInfo *) pthread_getspecific(_egl_TSD);
+}
+
+static INLINE void _eglFiniTSD(void)
+{
+ _eglLockMutex(&_egl_TSDMutex);
+ if (_egl_TSDInitialized) {
+ _EGLThreadInfo *t = _eglGetTSD();
+
+ _egl_TSDInitialized = EGL_FALSE;
+ if (t && _egl_FreeTSD)
+ _egl_FreeTSD((void *) t);
+ pthread_key_delete(_egl_TSD);
+ }
+ _eglUnlockMutex(&_egl_TSDMutex);
+}
+
+static INLINE EGLBoolean _eglInitTSD(void (*dtor)(_EGLThreadInfo *))
+{
+ if (!_egl_TSDInitialized) {
+ _eglLockMutex(&_egl_TSDMutex);
+
+ /* check again after acquiring lock */
+ if (!_egl_TSDInitialized) {
+ if (pthread_key_create(&_egl_TSD, (void (*)(void *)) dtor) != 0) {
+ _eglUnlockMutex(&_egl_TSDMutex);
+ return EGL_FALSE;
+ }
+ _egl_FreeTSD = dtor;
+ _eglAddAtExitCall(_eglFiniTSD);
+ _egl_TSDInitialized = EGL_TRUE;
+ }
+
+ _eglUnlockMutex(&_egl_TSDMutex);
+ }
+
+ return EGL_TRUE;
+}
+
+#else /* PTHREADS */
+static const _EGLThreadInfo *_egl_TSD;
+static void (*_egl_FreeTSD)(_EGLThreadInfo *);
+
+static INLINE void _eglSetTSD(const _EGLThreadInfo *t)
+{
+ _egl_TSD = t;
+}
+
+static INLINE _EGLThreadInfo *_eglGetTSD(void)
+{
+ return (_EGLThreadInfo *) _egl_TSD;
+}
+
+static INLINE void _eglFiniTSD(void)
+{
+ if (_egl_FreeTSD && _egl_TSD)
+ _egl_FreeTSD((_EGLThreadInfo *) _egl_TSD);
+}
+
+static INLINE EGLBoolean _eglInitTSD(void (*dtor)(_EGLThreadInfo *))
+{
+ if (!_egl_FreeTSD && dtor) {
+ _egl_FreeTSD = dtor;
+ _eglAddAtExitCall(_eglFiniTSD);
+ }
+ return EGL_TRUE;
+}
+
+#endif /* !PTHREADS */
+
+
+static void
+_eglInitThreadInfo(_EGLThreadInfo *t)
+{
+ memset(t, 0, sizeof(*t));
+ t->LastError = EGL_SUCCESS;
+ /* default, per EGL spec */
+ t->CurrentAPIIndex = _eglConvertApiToIndex(EGL_OPENGL_ES_API);
+}
+
+
+/**
+ * Allocate and init a new _EGLThreadInfo object.
+ */
+static _EGLThreadInfo *
+_eglCreateThreadInfo(void)
+{
+ _EGLThreadInfo *t = (_EGLThreadInfo *) calloc(1, sizeof(_EGLThreadInfo));
+ if (t)
+ _eglInitThreadInfo(t);
+ else
+ t = &dummy_thread;
+ return t;
+}
+
+
+/**
+ * Delete/free a _EGLThreadInfo object.
+ */
+static void
+_eglDestroyThreadInfo(_EGLThreadInfo *t)
+{
+ if (t != &dummy_thread)
+ free(t);
+}
+
+
+/**
+ * Make sure TSD is initialized and return current value.
+ */
+static INLINE _EGLThreadInfo *
+_eglCheckedGetTSD(void)
+{
+ if (_eglInitTSD(&_eglDestroyThreadInfo) != EGL_TRUE) {
+ _eglLog(_EGL_FATAL, "failed to initialize \"current\" system");
+ return NULL;
+ }
+
+ return _eglGetTSD();
+}
+
+
+/**
+ * Return the calling thread's thread info.
+ * If the calling thread nevers calls this function before, or if its thread
+ * info was destroyed, a new one is created. This function never returns NULL.
+ * In the case allocation fails, a dummy one is returned. See also
+ * _eglIsCurrentThreadDummy.
+ */
+_EGLThreadInfo *
+_eglGetCurrentThread(void)
+{
+ _EGLThreadInfo *t = _eglCheckedGetTSD();
+ if (!t) {
+ t = _eglCreateThreadInfo();
+ _eglSetTSD(t);
+ }
+
+ return t;
+}
+
+
+/**
+ * Destroy the calling thread's thread info.
+ */
+void
+_eglDestroyCurrentThread(void)
+{
+ _EGLThreadInfo *t = _eglCheckedGetTSD();
+ if (t) {
+ _eglDestroyThreadInfo(t);
+ _eglSetTSD(NULL);
+ }
+}
+
+
+/**
+ * Return true if the calling thread's thread info is dummy.
+ * A dummy thread info is shared by all threads and should not be modified.
+ * Functions like eglBindAPI or eglMakeCurrent should check for dummy-ness
+ * before updating the thread info.
+ */
+EGLBoolean
+_eglIsCurrentThreadDummy(void)
+{
+ _EGLThreadInfo *t = _eglCheckedGetTSD();
+ return (!t || t == &dummy_thread);
+}
+
+
+/**
+ * Return the currently bound context of the given API, or NULL.
+ */
+PUBLIC _EGLContext *
+_eglGetAPIContext(EGLenum api)
+{
+ _EGLThreadInfo *t = _eglGetCurrentThread();
+ return t->CurrentContexts[_eglConvertApiToIndex(api)];
+}
+
+
+/**
+ * Return the currently bound context of the current API, or NULL.
+ */
+_EGLContext *
+_eglGetCurrentContext(void)
+{
+ _EGLThreadInfo *t = _eglGetCurrentThread();
+ return t->CurrentContexts[t->CurrentAPIIndex];
+}
+
+
+/**
+ * Record EGL error code and return EGL_FALSE.
+ */
+EGLBoolean
+_eglError(EGLint errCode, const char *msg)
+{
+ _EGLThreadInfo *t = _eglGetCurrentThread();
+
+ if (t == &dummy_thread)
+ return EGL_FALSE;
+
+ t->LastError = errCode;
+
+ if (errCode != EGL_SUCCESS) {
+ const char *s;
+
+ switch (errCode) {
+ case EGL_BAD_ACCESS:
+ s = "EGL_BAD_ACCESS";
+ break;
+ case EGL_BAD_ALLOC:
+ s = "EGL_BAD_ALLOC";
+ break;
+ case EGL_BAD_ATTRIBUTE:
+ s = "EGL_BAD_ATTRIBUTE";
+ break;
+ case EGL_BAD_CONFIG:
+ s = "EGL_BAD_CONFIG";
+ break;
+ case EGL_BAD_CONTEXT:
+ s = "EGL_BAD_CONTEXT";
+ break;
+ case EGL_BAD_CURRENT_SURFACE:
+ s = "EGL_BAD_CURRENT_SURFACE";
+ break;
+ case EGL_BAD_DISPLAY:
+ s = "EGL_BAD_DISPLAY";
+ break;
+ case EGL_BAD_MATCH:
+ s = "EGL_BAD_MATCH";
+ break;
+ case EGL_BAD_NATIVE_PIXMAP:
+ s = "EGL_BAD_NATIVE_PIXMAP";
+ break;
+ case EGL_BAD_NATIVE_WINDOW:
+ s = "EGL_BAD_NATIVE_WINDOW";
+ break;
+ case EGL_BAD_PARAMETER:
+ s = "EGL_BAD_PARAMETER";
+ break;
+ case EGL_BAD_SURFACE:
+ s = "EGL_BAD_SURFACE";
+ break;
+ case EGL_BAD_SCREEN_MESA:
+ s = "EGL_BAD_SCREEN_MESA";
+ break;
+ case EGL_BAD_MODE_MESA:
+ s = "EGL_BAD_MODE_MESA";
+ break;
+ default:
+ s = "other";
+ }
+ _eglLog(_EGL_DEBUG, "EGL user error 0x%x (%s) in %s\n", errCode, s, msg);
+ }
+
+ return EGL_FALSE;
+}
diff --git a/src/egl/main/eglcurrent.h b/src/egl/main/eglcurrent.h
new file mode 100644
index 0000000000..e5c94ce60a
--- /dev/null
+++ b/src/egl/main/eglcurrent.h
@@ -0,0 +1,88 @@
+#ifndef EGLCURRENT_INCLUDED
+#define EGLCURRENT_INCLUDED
+
+
+#include "egltypedefs.h"
+
+
+#define _EGL_API_ALL_BITS \
+ (EGL_OPENGL_ES_BIT | \
+ EGL_OPENVG_BIT | \
+ EGL_OPENGL_ES2_BIT | \
+ EGL_OPENGL_BIT)
+
+
+#define _EGL_API_FIRST_API EGL_OPENGL_ES_API
+#define _EGL_API_LAST_API EGL_OPENGL_API
+#define _EGL_API_NUM_APIS (_EGL_API_LAST_API - _EGL_API_FIRST_API + 1)
+
+
+/**
+ * Per-thread info
+ */
+struct _egl_thread_info
+{
+ EGLint LastError;
+ _EGLContext *CurrentContexts[_EGL_API_NUM_APIS];
+ /* use index for fast access to current context */
+ EGLint CurrentAPIIndex;
+};
+
+
+/**
+ * Return true if a client API enum is recognized.
+ */
+static INLINE EGLBoolean
+_eglIsApiValid(EGLenum api)
+{
+ return (api >= _EGL_API_FIRST_API && api <= _EGL_API_LAST_API);
+}
+
+
+/**
+ * Convert a client API enum to an index, for use by thread info.
+ * The client API enum is assumed to be valid.
+ */
+static INLINE EGLint
+_eglConvertApiToIndex(EGLenum api)
+{
+ return api - _EGL_API_FIRST_API;
+}
+
+
+/**
+ * Convert an index, used by thread info, to a client API enum.
+ * The index is assumed to be valid.
+ */
+static INLINE EGLenum
+_eglConvertApiFromIndex(EGLint idx)
+{
+ return _EGL_API_FIRST_API + idx;
+}
+
+
+PUBLIC _EGLThreadInfo *
+_eglGetCurrentThread(void);
+
+
+extern void
+_eglDestroyCurrentThread(void);
+
+
+extern EGLBoolean
+_eglIsCurrentThreadDummy(void);
+
+
+PUBLIC _EGLContext *
+_eglGetAPIContext(EGLenum api);
+
+
+PUBLIC _EGLContext *
+_eglGetCurrentContext(void);
+
+
+PUBLIC EGLBoolean
+_eglError(EGLint errCode, const char *msg);
+
+
+#endif /* EGLCURRENT_INCLUDED */
diff --git a/src/egl/main/egldefines.h b/src/egl/main/egldefines.h
new file mode 100644
index 0000000000..4ecd4c1420
--- /dev/null
+++ b/src/egl/main/egldefines.h
@@ -0,0 +1,46 @@
+/**************************************************************************
+ *
+ * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+
+/**
+ * Internal EGL defines
+ */
+
+
+#ifndef EGLDEFINES_INCLUDED
+#define EGLDEFINES_INCLUDED
+
+
+#define _EGL_MAX_EXTENSIONS_LEN 1000
+
+#define _EGL_VENDOR_STRING "Mesa Project"
+
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
+
+
+#endif /* EGLDEFINES_INCLUDED */
diff --git a/src/egl/main/egldisplay.c b/src/egl/main/egldisplay.c
new file mode 100644
index 0000000000..5dc5fd9719
--- /dev/null
+++ b/src/egl/main/egldisplay.c
@@ -0,0 +1,221 @@
+/**
+ * Functions related to EGLDisplay.
+ */
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include "eglcontext.h"
+#include "eglsurface.h"
+#include "egldisplay.h"
+#include "egldriver.h"
+#include "eglglobals.h"
+#include "eglmutex.h"
+#include "egllog.h"
+
+
+/**
+ * Finish display management.
+ */
+void
+_eglFiniDisplay(void)
+{
+ _EGLDisplay *dpyList, *dpy;
+
+ /* atexit function is called with global mutex locked */
+ dpyList = _eglGlobal.DisplayList;
+ while (dpyList) {
+ EGLint i;
+
+ /* pop list head */
+ dpy = dpyList;
+ dpyList = dpyList->Next;
+
+ for (i = 0; i < _EGL_NUM_RESOURCES; i++) {
+ if (dpy->ResourceLists[i]) {
+ _eglLog(_EGL_DEBUG, "Display %p is destroyed with resources", dpy);
+ break;
+ }
+ }
+
+ free(dpy);
+ }
+ _eglGlobal.DisplayList = NULL;
+}
+
+
+/**
+ * Find the display corresponding to the specified native display, or create a
+ * new one.
+ */
+_EGLDisplay *
+_eglFindDisplay(EGLNativeDisplayType nativeDisplay)
+{
+ _EGLDisplay *dpy;
+
+ _eglLockMutex(_eglGlobal.Mutex);
+
+ /* search the display list first */
+ dpy = _eglGlobal.DisplayList;
+ while (dpy) {
+ if (dpy->NativeDisplay == nativeDisplay)
+ break;
+ dpy = dpy->Next;
+ }
+
+ /* create a new display */
+ if (!dpy) {
+ dpy = (_EGLDisplay *) calloc(1, sizeof(_EGLDisplay));
+ if (dpy) {
+ _eglInitMutex(&dpy->Mutex);
+ dpy->NativeDisplay = nativeDisplay;
+
+ /* add to the display list */
+ dpy->Next = _eglGlobal.DisplayList;
+ _eglGlobal.DisplayList = dpy;
+ }
+ }
+
+ _eglUnlockMutex(_eglGlobal.Mutex);
+
+ return dpy;
+}
+
+
+/**
+ * Destroy the contexts and surfaces that are linked to the display.
+ */
+void
+_eglReleaseDisplayResources(_EGLDriver *drv, _EGLDisplay *display)
+{
+ _EGLResource *list;
+
+ list = display->ResourceLists[_EGL_RESOURCE_CONTEXT];
+ while (list) {
+ _EGLContext *ctx = (_EGLContext *) list;
+ list = list->Next;
+
+ _eglUnlinkContext(ctx);
+ drv->API.DestroyContext(drv, display, ctx);
+ }
+ assert(!display->ResourceLists[_EGL_RESOURCE_CONTEXT]);
+
+ list = display->ResourceLists[_EGL_RESOURCE_SURFACE];
+ while (list) {
+ _EGLSurface *surf = (_EGLSurface *) list;
+ list = list->Next;
+
+ _eglUnlinkSurface(surf);
+ drv->API.DestroySurface(drv, display, surf);
+ }
+ assert(!display->ResourceLists[_EGL_RESOURCE_SURFACE]);
+}
+
+
+/**
+ * Free all the data hanging of an _EGLDisplay object, but not
+ * the object itself.
+ */
+void
+_eglCleanupDisplay(_EGLDisplay *disp)
+{
+ EGLint i;
+
+ if (disp->Configs) {
+ for (i = 0; i < disp->NumConfigs; i++)
+ free(disp->Configs[i]);
+ free(disp->Configs);
+ disp->Configs = NULL;
+ disp->NumConfigs = 0;
+ disp->MaxConfigs = 0;
+ }
+
+ /* XXX incomplete */
+}
+
+
+/**
+ * Return EGL_TRUE if the given handle is a valid handle to a display.
+ */
+EGLBoolean
+_eglCheckDisplayHandle(EGLDisplay dpy)
+{
+ _EGLDisplay *cur;
+
+ _eglLockMutex(_eglGlobal.Mutex);
+ cur = _eglGlobal.DisplayList;
+ while (cur) {
+ if (cur == (_EGLDisplay *) dpy)
+ break;
+ cur = cur->Next;
+ }
+ _eglUnlockMutex(_eglGlobal.Mutex);
+ return (cur != NULL);
+}
+
+
+/**
+ * Return EGL_TRUE if the given resource is valid. That is, the display does
+ * own the resource.
+ */
+EGLBoolean
+_eglCheckResource(void *res, _EGLResourceType type, _EGLDisplay *dpy)
+{
+ _EGLResource *list = dpy->ResourceLists[type];
+
+ if (!res)
+ return EGL_FALSE;
+
+ while (list) {
+ if (res == (void *) list) {
+ assert(list->Display == dpy);
+ break;
+ }
+ list = list->Next;
+ }
+
+ return (list != NULL);
+}
+
+
+/**
+ * Link a resource to a display.
+ */
+void
+_eglLinkResource(_EGLResource *res, _EGLResourceType type, _EGLDisplay *dpy)
+{
+ assert(!res->Display || res->Display == dpy);
+
+ res->Display = dpy;
+ res->IsLinked = EGL_TRUE;
+ res->Next = dpy->ResourceLists[type];
+ dpy->ResourceLists[type] = res;
+}
+
+
+/**
+ * Unlink a linked resource from its display.
+ */
+void
+_eglUnlinkResource(_EGLResource *res, _EGLResourceType type)
+{
+ _EGLResource *prev;
+
+ prev = res->Display->ResourceLists[type];
+ if (prev != res) {
+ while (prev) {
+ if (prev->Next == res)
+ break;
+ prev = prev->Next;
+ }
+ assert(prev);
+ prev->Next = res->Next;
+ }
+ else {
+ res->Display->ResourceLists[type] = res->Next;
+ }
+
+ res->Next = NULL;
+ /* do not reset res->Display */
+ res->IsLinked = EGL_FALSE;
+}
diff --git a/src/egl/main/egldisplay.h b/src/egl/main/egldisplay.h
new file mode 100644
index 0000000000..42e305f91a
--- /dev/null
+++ b/src/egl/main/egldisplay.h
@@ -0,0 +1,156 @@
+#ifndef EGLDISPLAY_INCLUDED
+#define EGLDISPLAY_INCLUDED
+
+
+#include "egltypedefs.h"
+#include "egldefines.h"
+#include "eglmutex.h"
+
+
+enum _egl_resource_type {
+ _EGL_RESOURCE_CONTEXT,
+ _EGL_RESOURCE_SURFACE,
+ _EGL_RESOURCE_IMAGE,
+
+ _EGL_NUM_RESOURCES
+};
+/* this cannot and need not go into egltypedefs.h */
+typedef enum _egl_resource_type _EGLResourceType;
+
+
+/**
+ * A resource of a display.
+ */
+struct _egl_resource
+{
+ /* which display the resource belongs to */
+ _EGLDisplay *Display;
+ EGLBoolean IsLinked;
+
+ /* used to link resources of the same type */
+ _EGLResource *Next;
+};
+
+
+/**
+ * Optional EGL extensions info.
+ */
+struct _egl_extensions
+{
+ EGLBoolean MESA_screen_surface;
+ EGLBoolean MESA_copy_context;
+ EGLBoolean KHR_image_base;
+ EGLBoolean KHR_image_pixmap;
+ EGLBoolean KHR_vg_parent_image;
+ EGLBoolean KHR_gl_texture_2D_image;
+ EGLBoolean KHR_gl_texture_cubemap_image;
+ EGLBoolean KHR_gl_texture_3D_image;
+ EGLBoolean KHR_gl_renderbuffer_image;
+ EGLBoolean NOK_swap_region;
+ EGLBoolean NOK_texture_from_pixmap;
+
+ char String[_EGL_MAX_EXTENSIONS_LEN];
+};
+
+
+struct _egl_display
+{
+ /* used to link displays */
+ _EGLDisplay *Next;
+
+ _EGLMutex Mutex;
+
+ EGLNativeDisplayType NativeDisplay;
+
+ EGLBoolean Initialized; /**< True if the display is initialized */
+ _EGLDriver *Driver;
+ void *DriverData; /* private to driver */
+
+ int APImajor, APIminor; /**< as returned by eglInitialize() */
+ char Version[1000]; /**< initialized from APImajor/minor, DriverName */
+
+ /** Bitmask of supported APIs (EGL_xx_BIT) set by the driver during init */
+ EGLint ClientAPIsMask;
+ char ClientAPIs[1000]; /**< updated by eglQueryString */
+
+ _EGLExtensions Extensions;
+
+ EGLint NumScreens;
+ _EGLScreen **Screens; /* array [NumScreens] */
+
+ EGLint MaxConfigs;
+ EGLint NumConfigs;
+ _EGLConfig **Configs; /* array [NumConfigs] of ptr to _EGLConfig */
+
+ /* lists of resources */
+ _EGLResource *ResourceLists[_EGL_NUM_RESOURCES];
+};
+
+
+extern void
+_eglFiniDisplay(void);
+
+
+extern _EGLDisplay *
+_eglFindDisplay(EGLNativeDisplayType displayName);
+
+
+PUBLIC void
+_eglReleaseDisplayResources(_EGLDriver *drv, _EGLDisplay *dpy);
+
+
+PUBLIC void
+_eglCleanupDisplay(_EGLDisplay *disp);
+
+
+extern EGLBoolean
+_eglCheckDisplayHandle(EGLDisplay dpy);
+
+
+PUBLIC EGLBoolean
+_eglCheckResource(void *res, _EGLResourceType type, _EGLDisplay *dpy);
+
+
+/**
+ * Lookup a handle to find the linked display.
+ * Return NULL if the handle has no corresponding linked display.
+ */
+static INLINE _EGLDisplay *
+_eglLookupDisplay(EGLDisplay display)
+{
+ _EGLDisplay *dpy = (_EGLDisplay *) display;
+ if (!_eglCheckDisplayHandle(display))
+ dpy = NULL;
+ return dpy;
+}
+
+
+/**
+ * Return the handle of a linked display, or EGL_NO_DISPLAY.
+ */
+static INLINE EGLDisplay
+_eglGetDisplayHandle(_EGLDisplay *dpy)
+{
+ return (EGLDisplay) ((dpy) ? dpy : EGL_NO_DISPLAY);
+}
+
+
+extern void
+_eglLinkResource(_EGLResource *res, _EGLResourceType type, _EGLDisplay *dpy);
+
+
+extern void
+_eglUnlinkResource(_EGLResource *res, _EGLResourceType type);
+
+
+/**
+ * Return true if the resource is linked.
+ */
+static INLINE EGLBoolean
+_eglIsResourceLinked(_EGLResource *res)
+{
+ return res->IsLinked;
+}
+
+
+#endif /* EGLDISPLAY_INCLUDED */
diff --git a/src/egl/main/egldriver.c b/src/egl/main/egldriver.c
new file mode 100644
index 0000000000..f56214472e
--- /dev/null
+++ b/src/egl/main/egldriver.c
@@ -0,0 +1,699 @@
+/**
+ * Functions for choosing and opening/loading device drivers.
+ */
+
+
+#include <assert.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "eglstring.h"
+#include "eglconfig.h"
+#include "eglcontext.h"
+#include "egldefines.h"
+#include "egldisplay.h"
+#include "egldriver.h"
+#include "eglglobals.h"
+#include "egllog.h"
+#include "eglmisc.h"
+#include "eglmode.h"
+#include "eglscreen.h"
+#include "eglstring.h"
+#include "eglsurface.h"
+#include "eglimage.h"
+
+#if defined(_EGL_OS_UNIX)
+#include <dlfcn.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <unistd.h>
+#endif
+
+
+/**
+ * Wrappers for dlopen/dlclose()
+ */
+#if defined(_EGL_OS_WINDOWS)
+
+
+/* XXX Need to decide how to do dynamic name lookup on Windows */
+static const char *DefaultDriverNames[] = {
+ "egl_gdi_swrast"
+};
+
+typedef HMODULE lib_handle;
+
+static HMODULE
+open_library(const char *filename)
+{
+ return LoadLibrary(filename);
+}
+
+static void
+close_library(HMODULE lib)
+{
+ FreeLibrary(lib);
+}
+
+
+static const char *
+library_suffix(void)
+{
+ return ".dll";
+}
+
+
+#elif defined(_EGL_OS_UNIX)
+
+
+static const char *DefaultDriverNames[] = {
+ "egl_dri2",
+ "egl_glx"
+};
+
+typedef void * lib_handle;
+
+static void *
+open_library(const char *filename)
+{
+ return dlopen(filename, RTLD_LAZY);
+}
+
+static void
+close_library(void *lib)
+{
+ dlclose(lib);
+}
+
+
+static const char *
+library_suffix(void)
+{
+ return ".so";
+}
+
+
+#endif
+
+
+#define NUM_PROBE_CACHE_SLOTS 8
+static struct {
+ EGLint keys[NUM_PROBE_CACHE_SLOTS];
+ const void *values[NUM_PROBE_CACHE_SLOTS];
+} _eglProbeCache;
+
+
+/**
+ * Open the named driver and find its bootstrap function: _eglMain().
+ */
+static _EGLMain_t
+_eglOpenLibrary(const char *driverPath, lib_handle *handle)
+{
+ lib_handle lib;
+ _EGLMain_t mainFunc = NULL;
+ const char *error = "unknown error";
+
+ assert(driverPath);
+
+ _eglLog(_EGL_DEBUG, "dlopen(%s)", driverPath);
+ lib = open_library(driverPath);
+
+#if defined(_EGL_OS_WINDOWS)
+ /* XXX untested */
+ if (lib)
+ mainFunc = (_EGLMain_t) GetProcAddress(lib, "_eglMain");
+#elif defined(_EGL_OS_UNIX)
+ if (lib) {
+ union {
+ _EGLMain_t func;
+ void *ptr;
+ } tmp = { NULL };
+ /* direct cast gives a warning when compiled with -pedantic */
+ tmp.ptr = dlsym(lib, "_eglMain");
+ mainFunc = tmp.func;
+ if (!mainFunc)
+ error = dlerror();
+ }
+ else {
+ error = dlerror();
+ }
+#endif
+
+ if (!lib) {
+ _eglLog(_EGL_WARNING, "Could not open driver %s (%s)",
+ driverPath, error);
+ if (!getenv("EGL_DRIVER"))
+ _eglLog(_EGL_WARNING,
+ "The driver can be overridden by setting EGL_DRIVER");
+ return NULL;
+ }
+
+ if (!mainFunc) {
+ _eglLog(_EGL_WARNING, "_eglMain not found in %s (%s)",
+ driverPath, error);
+ if (lib)
+ close_library(lib);
+ return NULL;
+ }
+
+ *handle = lib;
+ return mainFunc;
+}
+
+
+/**
+ * Load the named driver.
+ */
+static _EGLDriver *
+_eglLoadDriver(const char *path, const char *args)
+{
+ _EGLMain_t mainFunc;
+ lib_handle lib;
+ _EGLDriver *drv = NULL;
+
+ mainFunc = _eglOpenLibrary(path, &lib);
+ if (!mainFunc)
+ return NULL;
+
+ drv = mainFunc(args);
+ if (!drv) {
+ if (lib)
+ close_library(lib);
+ return NULL;
+ }
+
+ if (!drv->Name) {
+ _eglLog(_EGL_WARNING, "Driver loaded from %s has no name", path);
+ drv->Name = "UNNAMED";
+ }
+
+ drv->Path = _eglstrdup(path);
+ drv->Args = (args) ? _eglstrdup(args) : NULL;
+ if (!drv->Path || (args && !drv->Args)) {
+ if (drv->Path)
+ free((char *) drv->Path);
+ if (drv->Args)
+ free((char *) drv->Args);
+ drv->Unload(drv);
+ if (lib)
+ close_library(lib);
+ return NULL;
+ }
+
+ drv->LibHandle = lib;
+
+ return drv;
+}
+
+
+/**
+ * Match a display to a preloaded driver.
+ *
+ * The matching is done by finding the driver with the highest score.
+ */
+_EGLDriver *
+_eglMatchDriver(_EGLDisplay *dpy)
+{
+ _EGLDriver *best_drv = NULL;
+ EGLint best_score = -1, i;
+
+ /*
+ * this function is called after preloading and the drivers never change
+ * after preloading.
+ */
+ for (i = 0; i < _eglGlobal.NumDrivers; i++) {
+ _EGLDriver *drv = _eglGlobal.Drivers[i];
+ EGLint score;
+
+ score = (drv->Probe) ? drv->Probe(drv, dpy) : 0;
+ if (score > best_score) {
+ if (best_drv) {
+ _eglLog(_EGL_DEBUG, "driver %s has higher score than %s",
+ drv->Name, best_drv->Name);
+ }
+
+ best_drv = drv;
+ best_score = score;
+ /* perfect match */
+ if (score >= 100)
+ break;
+ }
+ }
+
+ return best_drv;
+}
+
+
+/**
+ * A loader function for use with _eglPreloadForEach. The loader data is the
+ * filename of the driver. This function stops on the first valid driver.
+ */
+static EGLBoolean
+_eglLoaderFile(const char *dir, size_t len, void *loader_data)
+{
+ _EGLDriver *drv;
+ char path[1024];
+ const char *filename = (const char *) loader_data;
+ size_t flen = strlen(filename);
+
+ /* make a full path */
+ if (len + flen + 2 > sizeof(path))
+ return EGL_TRUE;
+ if (len) {
+ memcpy(path, dir, len);
+ path[len++] = '/';
+ }
+ memcpy(path + len, filename, flen);
+ len += flen;
+ path[len] = '\0';
+
+ if (library_suffix() == NULL || strstr(path, library_suffix()))
+ drv = _eglLoadDriver(path, NULL);
+ else {
+ const char *suffix = library_suffix();
+ size_t slen = strlen(suffix);
+ const char *p;
+ EGLBoolean need_suffix;
+
+ p = filename + flen - slen;
+ need_suffix = (p < filename || strcmp(p, suffix) != 0);
+ if (need_suffix && len + slen + 1 <= sizeof(path)) {
+ strcpy(path + len, suffix);
+ drv = _eglLoadDriver(path, NULL);
+ } else {
+ drv = NULL;
+ }
+ }
+ if (!drv)
+ return EGL_TRUE;
+
+ /* remember the driver and stop */
+ _eglGlobal.Drivers[_eglGlobal.NumDrivers++] = drv;
+ return EGL_FALSE;
+}
+
+
+/**
+ * A loader function for use with _eglPreloadForEach. The loader data is the
+ * pattern (prefix) of the files to look for.
+ */
+static EGLBoolean
+_eglLoaderPattern(const char *dir, size_t len, void *loader_data)
+{
+#if defined(_EGL_OS_UNIX)
+ const char *prefix, *suffix;
+ size_t prefix_len, suffix_len;
+ DIR *dirp;
+ struct dirent *dirent;
+ char path[1024];
+
+ if (len + 2 > sizeof(path))
+ return EGL_TRUE;
+ if (len) {
+ memcpy(path, dir, len);
+ path[len++] = '/';
+ }
+ path[len] = '\0';
+
+ dirp = opendir(path);
+ if (!dirp)
+ return EGL_TRUE;
+
+ prefix = (const char *) loader_data;
+ prefix_len = strlen(prefix);
+ suffix = library_suffix();
+ suffix_len = (suffix) ? strlen(suffix) : 0;
+
+ while ((dirent = readdir(dirp))) {
+ _EGLDriver *drv;
+ size_t dirent_len = strlen(dirent->d_name);
+ const char *p;
+
+ /* match the prefix */
+ if (strncmp(dirent->d_name, prefix, prefix_len) != 0)
+ continue;
+ /* match the suffix */
+ if (suffix) {
+ p = dirent->d_name + dirent_len - suffix_len;
+ if (p < dirent->d_name || strcmp(p, suffix) != 0)
+ continue;
+ }
+
+ /* make a full path and load the driver */
+ if (len + dirent_len + 1 <= sizeof(path)) {
+ strcpy(path + len, dirent->d_name);
+ drv = _eglLoadDriver(path, NULL);
+ if (drv)
+ _eglGlobal.Drivers[_eglGlobal.NumDrivers++] = drv;
+ }
+ }
+
+ closedir(dirp);
+
+ return EGL_TRUE;
+#else /* _EGL_OS_UNIX */
+ /* stop immediately */
+ return EGL_FALSE;
+#endif
+}
+
+
+/**
+ * Run the preload function on each driver directory and return the number of
+ * drivers loaded.
+ *
+ * The process may end prematurely if the callback function returns false.
+ */
+static EGLint
+_eglPreloadForEach(const char *search_path,
+ EGLBoolean (*loader)(const char *, size_t, void *),
+ void *loader_data)
+{
+ const char *cur, *next;
+ size_t len;
+ EGLint num_drivers = _eglGlobal.NumDrivers;
+
+ cur = search_path;
+ while (cur) {
+ next = strchr(cur, ':');
+ len = (next) ? next - cur : strlen(cur);
+
+ if (!loader(cur, len, loader_data))
+ break;
+
+ cur = (next) ? next + 1 : NULL;
+ }
+
+ return (_eglGlobal.NumDrivers - num_drivers);
+}
+
+
+/**
+ * Return a list of colon-separated driver directories.
+ */
+static const char *
+_eglGetSearchPath(void)
+{
+ static const char *search_path;
+
+#if defined(_EGL_OS_UNIX) || defined(_EGL_OS_WINDOWS)
+ if (!search_path) {
+ static char buffer[1024];
+ const char *p;
+ int ret;
+
+ p = getenv("EGL_DRIVERS_PATH");
+#if defined(_EGL_OS_UNIX)
+ if (p && (geteuid() != getuid() || getegid() != getgid())) {
+ _eglLog(_EGL_DEBUG,
+ "ignore EGL_DRIVERS_PATH for setuid/setgid binaries");
+ p = NULL;
+ }
+#endif /* _EGL_OS_UNIX */
+
+ if (p) {
+ ret = _eglsnprintf(buffer, sizeof(buffer),
+ "%s:%s", p, _EGL_DRIVER_SEARCH_DIR);
+ if (ret > 0 && ret < sizeof(buffer))
+ search_path = buffer;
+ }
+ }
+ if (!search_path)
+ search_path = _EGL_DRIVER_SEARCH_DIR;
+#else
+ search_path = "";
+#endif
+
+ return search_path;
+}
+
+
+/**
+ * Preload a user driver.
+ *
+ * A user driver can be specified by EGL_DRIVER.
+ */
+static EGLBoolean
+_eglPreloadUserDriver(void)
+{
+ const char *search_path = _eglGetSearchPath();
+ char *env;
+
+ env = getenv("EGL_DRIVER");
+#if defined(_EGL_OS_UNIX)
+ if (env && strchr(env, '/')) {
+ search_path = "";
+ if ((geteuid() != getuid() || getegid() != getgid())) {
+ _eglLog(_EGL_DEBUG,
+ "ignore EGL_DRIVER for setuid/setgid binaries");
+ env = NULL;
+ }
+ }
+#endif /* _EGL_OS_UNIX */
+ if (!env)
+ return EGL_FALSE;
+
+ if (!_eglPreloadForEach(search_path, _eglLoaderFile, (void *) env)) {
+ _eglLog(_EGL_WARNING, "EGL_DRIVER is set to an invalid driver");
+ return EGL_FALSE;
+ }
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Preload platform drivers.
+ *
+ * Platform drivers are a set of drivers that support a certain window system.
+ * The window system may be specified by EGL_PLATFORM.
+ *
+ * FIXME This makes libEGL a memory hog if an user driver is not specified and
+ * there are many platform drivers.
+ */
+static EGLBoolean
+_eglPreloadPlatformDrivers(void)
+{
+ const char *dpy;
+ char prefix[32];
+ int ret;
+
+ dpy = getenv("EGL_PLATFORM");
+ /* try deprecated env variable */
+ if (!dpy || !dpy[0])
+ dpy = getenv("EGL_DISPLAY");
+ if (!dpy || !dpy[0])
+ dpy = _EGL_DEFAULT_PLATFORM;
+ if (!dpy || !dpy[0])
+ return EGL_FALSE;
+
+ ret = _eglsnprintf(prefix, sizeof(prefix), "egl_%s_", dpy);
+ if (ret < 0 || ret >= sizeof(prefix))
+ return EGL_FALSE;
+
+ return (_eglPreloadForEach(_eglGetSearchPath(),
+ _eglLoaderPattern, (void *) prefix) > 0);
+}
+
+
+/**
+ * Preload drivers.
+ *
+ * This function loads the driver modules and creates the corresponding
+ * _EGLDriver objects.
+ */
+EGLBoolean
+_eglPreloadDrivers(void)
+{
+ EGLBoolean loaded;
+
+ /* protect the preloading process */
+ _eglLockMutex(_eglGlobal.Mutex);
+
+ /* already preloaded */
+ if (_eglGlobal.NumDrivers) {
+ _eglUnlockMutex(_eglGlobal.Mutex);
+ return EGL_TRUE;
+ }
+
+ loaded = (_eglPreloadUserDriver() ||
+ _eglPreloadPlatformDrivers());
+
+ _eglUnlockMutex(_eglGlobal.Mutex);
+
+ return loaded;
+}
+
+/**
+ * Unload preloaded drivers.
+ */
+void
+_eglUnloadDrivers(void)
+{
+ EGLint i;
+
+ /* this is called at atexit time */
+ for (i = 0; i < _eglGlobal.NumDrivers; i++) {
+ _EGLDriver *drv = _eglGlobal.Drivers[i];
+ lib_handle handle = drv->LibHandle;
+
+ if (drv->Path)
+ free((char *) drv->Path);
+ if (drv->Args)
+ free((char *) drv->Args);
+
+ /* destroy driver */
+ if (drv->Unload)
+ drv->Unload(drv);
+
+ if (handle)
+ close_library(handle);
+ _eglGlobal.Drivers[i] = NULL;
+ }
+
+ _eglGlobal.NumDrivers = 0;
+}
+
+_EGLDriver *
+_eglLoadDefaultDriver(EGLDisplay dpy, EGLint *major, EGLint *minor)
+{
+ _EGLDriver *drv = NULL;
+ int i;
+
+ _eglLockMutex(_eglGlobal.Mutex);
+
+ for (i = 0; i < ARRAY_SIZE(DefaultDriverNames); i++) {
+ _eglPreloadForEach(_eglGetSearchPath(),
+ _eglLoaderFile, (void *) DefaultDriverNames[i]);
+ if (_eglGlobal.NumDrivers == 0)
+ continue;
+ drv = _eglGlobal.Drivers[0];
+ if (drv->API.Initialize(drv, dpy, major, minor))
+ break;
+ _eglUnloadDrivers();
+ }
+
+ _eglUnlockMutex(_eglGlobal.Mutex);
+
+ return _eglGlobal.NumDrivers > 0 ? drv : NULL;
+}
+
+
+/**
+ * Plug all the available fallback routines into the given driver's
+ * dispatch table.
+ */
+void
+_eglInitDriverFallbacks(_EGLDriver *drv)
+{
+ /* If a pointer is set to NULL, then the device driver _really_ has
+ * to implement it.
+ */
+ drv->API.Initialize = NULL;
+ drv->API.Terminate = NULL;
+
+ drv->API.GetConfigs = _eglGetConfigs;
+ drv->API.ChooseConfig = _eglChooseConfig;
+ drv->API.GetConfigAttrib = _eglGetConfigAttrib;
+
+ drv->API.CreateContext = _eglCreateContext;
+ drv->API.DestroyContext = _eglDestroyContext;
+ drv->API.MakeCurrent = _eglMakeCurrent;
+ drv->API.QueryContext = _eglQueryContext;
+
+ drv->API.CreateWindowSurface = _eglCreateWindowSurface;
+ drv->API.CreatePixmapSurface = _eglCreatePixmapSurface;
+ drv->API.CreatePbufferSurface = _eglCreatePbufferSurface;
+ drv->API.DestroySurface = _eglDestroySurface;
+ drv->API.QuerySurface = _eglQuerySurface;
+ drv->API.SurfaceAttrib = _eglSurfaceAttrib;
+ drv->API.BindTexImage = _eglBindTexImage;
+ drv->API.ReleaseTexImage = _eglReleaseTexImage;
+ drv->API.SwapInterval = _eglSwapInterval;
+ drv->API.SwapBuffers = _eglSwapBuffers;
+ drv->API.CopyBuffers = _eglCopyBuffers;
+
+ drv->API.QueryString = _eglQueryString;
+ drv->API.WaitClient = _eglWaitClient;
+ drv->API.WaitNative = _eglWaitNative;
+
+#ifdef EGL_MESA_screen_surface
+ drv->API.ChooseModeMESA = _eglChooseModeMESA;
+ drv->API.GetModesMESA = _eglGetModesMESA;
+ drv->API.GetModeAttribMESA = _eglGetModeAttribMESA;
+ drv->API.GetScreensMESA = _eglGetScreensMESA;
+ drv->API.CreateScreenSurfaceMESA = _eglCreateScreenSurfaceMESA;
+ drv->API.ShowScreenSurfaceMESA = _eglShowScreenSurfaceMESA;
+ drv->API.ScreenPositionMESA = _eglScreenPositionMESA;
+ drv->API.QueryScreenMESA = _eglQueryScreenMESA;
+ drv->API.QueryScreenSurfaceMESA = _eglQueryScreenSurfaceMESA;
+ drv->API.QueryScreenModeMESA = _eglQueryScreenModeMESA;
+ drv->API.QueryModeStringMESA = _eglQueryModeStringMESA;
+#endif /* EGL_MESA_screen_surface */
+
+#ifdef EGL_VERSION_1_2
+ drv->API.CreatePbufferFromClientBuffer = _eglCreatePbufferFromClientBuffer;
+#endif /* EGL_VERSION_1_2 */
+
+#ifdef EGL_KHR_image_base
+ drv->API.CreateImageKHR = _eglCreateImageKHR;
+ drv->API.DestroyImageKHR = _eglDestroyImageKHR;
+#endif /* EGL_KHR_image_base */
+}
+
+
+/**
+ * Invoke a callback function on each EGL search path.
+ *
+ * The first argument of the callback function is the name of the search path.
+ * The second argument is the length of the name.
+ */
+void
+_eglSearchPathForEach(EGLBoolean (*callback)(const char *, size_t, void *),
+ void *callback_data)
+{
+ const char *search_path = _eglGetSearchPath();
+ _eglPreloadForEach(search_path, callback, callback_data);
+}
+
+
+/**
+ * Set the probe cache at the given key.
+ *
+ * A key, instead of a _EGLDriver, is used to allow the probe cache to be share
+ * by multiple drivers.
+ */
+void
+_eglSetProbeCache(EGLint key, const void *val)
+{
+ EGLint idx;
+
+ for (idx = 0; idx < NUM_PROBE_CACHE_SLOTS; idx++) {
+ if (!_eglProbeCache.keys[idx] || _eglProbeCache.keys[idx] == key)
+ break;
+ }
+ assert(key > 0);
+ assert(idx < NUM_PROBE_CACHE_SLOTS);
+
+ _eglProbeCache.keys[idx] = key;
+ _eglProbeCache.values[idx] = val;
+}
+
+
+/**
+ * Return the probe cache at the given key.
+ */
+const void *
+_eglGetProbeCache(EGLint key)
+{
+ EGLint idx;
+
+ for (idx = 0; idx < NUM_PROBE_CACHE_SLOTS; idx++) {
+ if (!_eglProbeCache.keys[idx] || _eglProbeCache.keys[idx] == key)
+ break;
+ }
+
+ return (idx < NUM_PROBE_CACHE_SLOTS && _eglProbeCache.keys[idx] == key) ?
+ _eglProbeCache.values[idx] : NULL;
+}
diff --git a/src/egl/main/egldriver.h b/src/egl/main/egldriver.h
new file mode 100644
index 0000000000..8b34c43b92
--- /dev/null
+++ b/src/egl/main/egldriver.h
@@ -0,0 +1,108 @@
+#ifndef EGLDRIVER_INCLUDED
+#define EGLDRIVER_INCLUDED
+
+
+#include "egltypedefs.h"
+#include "eglapi.h"
+
+
+/**
+ * Define an inline driver typecast function.
+ *
+ * Note that this macro defines a function and should not be ended with a
+ * semicolon when used.
+ */
+#define _EGL_DRIVER_TYPECAST(drvtype, egltype, code) \
+ static INLINE struct drvtype *drvtype(const egltype *obj) \
+ { return (struct drvtype *) code; }
+
+
+/**
+ * Define the driver typecast functions for _EGLDriver, _EGLDisplay,
+ * _EGLContext, _EGLSurface, and _EGLConfig.
+ *
+ * Note that this macro defines several functions and should not be ended with
+ * a semicolon when used.
+ */
+#define _EGL_DRIVER_STANDARD_TYPECASTS(drvname) \
+ _EGL_DRIVER_TYPECAST(drvname ## _driver, _EGLDriver, obj) \
+ /* note that this is not a direct cast */ \
+ _EGL_DRIVER_TYPECAST(drvname ## _display, _EGLDisplay, obj->DriverData) \
+ _EGL_DRIVER_TYPECAST(drvname ## _context, _EGLContext, obj) \
+ _EGL_DRIVER_TYPECAST(drvname ## _surface, _EGLSurface, obj) \
+ _EGL_DRIVER_TYPECAST(drvname ## _config, _EGLConfig, obj)
+
+
+typedef _EGLDriver *(*_EGLMain_t)(const char *args);
+
+
+/**
+ * Base class for device drivers.
+ */
+struct _egl_driver
+{
+ void *LibHandle; /**< dlopen handle */
+ const char *Path; /**< path to this driver */
+ const char *Args; /**< args to load this driver */
+
+ const char *Name; /**< name of this driver */
+
+ /**
+ * Probe a display and return a score.
+ *
+ * Roughly,
+ * 50 means the driver supports the display;
+ * 90 means the driver can accelerate the display;
+ * 100 means a perfect match.
+ */
+ EGLint (*Probe)(_EGLDriver *drv, _EGLDisplay *dpy);
+
+ /**
+ * Release the driver resource.
+ *
+ * It is called before dlclose().
+ */
+ void (*Unload)(_EGLDriver *drv);
+
+ _EGLAPI API; /**< EGL API dispatch table */
+};
+
+
+PUBLIC _EGLDriver *
+_eglMain(const char *args);
+
+
+extern _EGLDriver *
+_eglMatchDriver(_EGLDisplay *dpy);
+
+
+extern EGLBoolean
+_eglPreloadDrivers(void);
+
+
+extern void
+_eglUnloadDrivers(void);
+
+
+extern _EGLDriver *
+_eglLoadDefaultDriver(EGLDisplay dpy, EGLint *major, EGLint *minor);
+
+
+PUBLIC void
+_eglInitDriverFallbacks(_EGLDriver *drv);
+
+
+PUBLIC void
+_eglSearchPathForEach(EGLBoolean (*callback)(const char *, size_t, void *),
+ void *callback_data);
+
+
+PUBLIC void
+_eglSetProbeCache(EGLint key, const void *val);
+
+
+PUBLIC const void *
+_eglGetProbeCache(EGLint key);
+
+
+#endif /* EGLDRIVER_INCLUDED */
diff --git a/src/egl/main/eglglobals.c b/src/egl/main/eglglobals.c
new file mode 100644
index 0000000000..e63819e08a
--- /dev/null
+++ b/src/egl/main/eglglobals.c
@@ -0,0 +1,53 @@
+#include <stdlib.h>
+#include <assert.h>
+#include "eglglobals.h"
+#include "egldisplay.h"
+#include "egldriver.h"
+#include "eglmutex.h"
+
+
+static _EGL_DECLARE_MUTEX(_eglGlobalMutex);
+struct _egl_global _eglGlobal =
+{
+ &_eglGlobalMutex, /* Mutex */
+ NULL, /* DisplayList */
+ 1, /* FreeScreenHandle */
+ 0, /* NumDrivers */
+ { NULL }, /* Drivers */
+ 2, /* NumAtExitCalls */
+ {
+ /* default AtExitCalls, called in reverse order */
+ _eglUnloadDrivers, /* always called last */
+ _eglFiniDisplay
+ },
+};
+
+
+static void
+_eglAtExit(void)
+{
+ EGLint i;
+ for (i = _eglGlobal.NumAtExitCalls - 1; i >= 0; i--)
+ _eglGlobal.AtExitCalls[i]();
+}
+
+
+void
+_eglAddAtExitCall(void (*func)(void))
+{
+ if (func) {
+ static EGLBoolean registered = EGL_FALSE;
+
+ _eglLockMutex(_eglGlobal.Mutex);
+
+ if (!registered) {
+ atexit(_eglAtExit);
+ registered = EGL_TRUE;
+ }
+
+ assert(_eglGlobal.NumAtExitCalls < ARRAY_SIZE(_eglGlobal.AtExitCalls));
+ _eglGlobal.AtExitCalls[_eglGlobal.NumAtExitCalls++] = func;
+
+ _eglUnlockMutex(_eglGlobal.Mutex);
+ }
+}
diff --git a/src/egl/main/eglglobals.h b/src/egl/main/eglglobals.h
new file mode 100644
index 0000000000..4368898020
--- /dev/null
+++ b/src/egl/main/eglglobals.h
@@ -0,0 +1,37 @@
+#ifndef EGLGLOBALS_INCLUDED
+#define EGLGLOBALS_INCLUDED
+
+
+#include "egltypedefs.h"
+#include "eglmutex.h"
+
+
+/**
+ * Global library data
+ */
+struct _egl_global
+{
+ _EGLMutex *Mutex;
+
+ /* the list of all displays */
+ _EGLDisplay *DisplayList;
+
+ EGLScreenMESA FreeScreenHandle;
+
+ /* these never change after preloading */
+ EGLint NumDrivers;
+ _EGLDriver *Drivers[10];
+
+ EGLint NumAtExitCalls;
+ void (*AtExitCalls[10])(void);
+};
+
+
+extern struct _egl_global _eglGlobal;
+
+
+extern void
+_eglAddAtExitCall(void (*func)(void));
+
+
+#endif /* EGLGLOBALS_INCLUDED */
diff --git a/src/egl/main/eglimage.c b/src/egl/main/eglimage.c
new file mode 100644
index 0000000000..5732ef35ec
--- /dev/null
+++ b/src/egl/main/eglimage.c
@@ -0,0 +1,90 @@
+#include <assert.h>
+#include <string.h>
+
+#include "eglimage.h"
+#include "eglcurrent.h"
+#include "egllog.h"
+
+
+#ifdef EGL_KHR_image_base
+
+
+/**
+ * Parse the list of image attributes and return the proper error code.
+ */
+static EGLint
+_eglParseImageAttribList(_EGLImage *img, const EGLint *attrib_list)
+{
+ EGLint i, err = EGL_SUCCESS;
+
+ if (!attrib_list)
+ return EGL_SUCCESS;
+
+ for (i = 0; attrib_list[i] != EGL_NONE; i++) {
+ EGLint attr = attrib_list[i++];
+ EGLint val = attrib_list[i];
+
+ switch (attr) {
+ case EGL_IMAGE_PRESERVED_KHR:
+ img->Preserved = val;
+ break;
+ case EGL_GL_TEXTURE_LEVEL_KHR:
+ img->GLTextureLevel = val;
+ break;
+ case EGL_GL_TEXTURE_ZOFFSET_KHR:
+ img->GLTextureZOffset = val;
+ break;
+ default:
+ /* unknown attrs are ignored */
+ break;
+ }
+
+ if (err != EGL_SUCCESS) {
+ _eglLog(_EGL_DEBUG, "bad image attribute 0x%04x", attr);
+ break;
+ }
+ }
+
+ return err;
+}
+
+
+EGLBoolean
+_eglInitImage(_EGLImage *img, _EGLDisplay *dpy, const EGLint *attrib_list)
+{
+ EGLint err;
+
+ memset(img, 0, sizeof(_EGLImage));
+ img->Resource.Display = dpy;
+
+ img->Preserved = EGL_FALSE;
+ img->GLTextureLevel = 0;
+ img->GLTextureZOffset = 0;
+
+ err = _eglParseImageAttribList(img, attrib_list);
+ if (err != EGL_SUCCESS)
+ return _eglError(err, "eglCreateImageKHR");
+
+ return EGL_TRUE;
+}
+
+
+_EGLImage *
+_eglCreateImageKHR(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx,
+ EGLenum target, EGLClientBuffer buffer,
+ const EGLint *attr_list)
+{
+ /* driver should override this function */
+ return NULL;
+}
+
+
+EGLBoolean
+_eglDestroyImageKHR(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *image)
+{
+ /* driver should override this function */
+ return EGL_FALSE;
+}
+
+
+#endif /* EGL_KHR_image_base */
diff --git a/src/egl/main/eglimage.h b/src/egl/main/eglimage.h
new file mode 100644
index 0000000000..2c0fb16d1d
--- /dev/null
+++ b/src/egl/main/eglimage.h
@@ -0,0 +1,96 @@
+#ifndef EGLIMAGE_INCLUDED
+#define EGLIMAGE_INCLUDED
+
+
+#include "egltypedefs.h"
+#include "egldisplay.h"
+
+
+/**
+ * "Base" class for device driver images.
+ */
+struct _egl_image
+{
+ /* An image is a display resource */
+ _EGLResource Resource;
+
+ EGLBoolean Preserved;
+ EGLint GLTextureLevel;
+ EGLint GLTextureZOffset;
+};
+
+
+PUBLIC EGLBoolean
+_eglInitImage(_EGLImage *img, _EGLDisplay *dpy, const EGLint *attrib_list);
+
+
+extern _EGLImage *
+_eglCreateImageKHR(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx,
+ EGLenum target, EGLClientBuffer buffer, const EGLint *attr_list);
+
+
+extern EGLBoolean
+_eglDestroyImageKHR(_EGLDriver *drv, _EGLDisplay *dpy, _EGLImage *image);
+
+
+/**
+ * Link an image to a display and return the handle of the link.
+ * The handle can be passed to client directly.
+ */
+static INLINE EGLImageKHR
+_eglLinkImage(_EGLImage *img, _EGLDisplay *dpy)
+{
+ _eglLinkResource(&img->Resource, _EGL_RESOURCE_IMAGE, dpy);
+ return (EGLImageKHR) img;
+}
+
+
+/**
+ * Unlink a linked image from its display.
+ * Accessing an unlinked image should generate EGL_BAD_PARAMETER error.
+ */
+static INLINE void
+_eglUnlinkImage(_EGLImage *img)
+{
+ _eglUnlinkResource(&img->Resource, _EGL_RESOURCE_IMAGE);
+}
+
+
+/**
+ * Lookup a handle to find the linked image.
+ * Return NULL if the handle has no corresponding linked image.
+ */
+static INLINE _EGLImage *
+_eglLookupImage(EGLImageKHR image, _EGLDisplay *dpy)
+{
+ _EGLImage *img = (_EGLImage *) image;
+ if (!dpy || !_eglCheckResource((void *) img, _EGL_RESOURCE_IMAGE, dpy))
+ img = NULL;
+ return img;
+}
+
+
+/**
+ * Return the handle of a linked image, or EGL_NO_IMAGE_KHR.
+ */
+static INLINE EGLImageKHR
+_eglGetImageHandle(_EGLImage *img)
+{
+ _EGLResource *res = (_EGLResource *) img;
+ return (res && _eglIsResourceLinked(res)) ?
+ (EGLImageKHR) img : EGL_NO_IMAGE_KHR;
+}
+
+
+/**
+ * Return true if the image is linked to a display.
+ */
+static INLINE EGLBoolean
+_eglIsImageLinked(_EGLImage *img)
+{
+ _EGLResource *res = (_EGLResource *) img;
+ return (res && _eglIsResourceLinked(res));
+}
+
+
+#endif /* EGLIMAGE_INCLUDED */
diff --git a/src/egl/main/egllog.c b/src/egl/main/egllog.c
new file mode 100644
index 0000000000..8f3bae2243
--- /dev/null
+++ b/src/egl/main/egllog.c
@@ -0,0 +1,176 @@
+/**
+ * Logging facility for debug/info messages.
+ * _EGL_FATAL messages are printed to stderr
+ * The EGL_LOG_LEVEL var controls the output of other warning/info/debug msgs.
+ */
+
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "egllog.h"
+#include "eglstring.h"
+#include "eglmutex.h"
+
+#define MAXSTRING 1000
+#define FALLBACK_LOG_LEVEL _EGL_WARNING
+
+
+static struct {
+ _EGLMutex mutex;
+
+ EGLBoolean initialized;
+ EGLint level;
+ _EGLLogProc logger;
+ EGLint num_messages;
+} logging = {
+ _EGL_MUTEX_INITIALIZER,
+ EGL_FALSE,
+ FALLBACK_LOG_LEVEL,
+ NULL,
+ 0
+};
+
+static const char *level_strings[] = {
+ /* the order is important */
+ "fatal",
+ "warning",
+ "info",
+ "debug",
+ NULL
+};
+
+
+/**
+ * Set the function to be called when there is a message to log.
+ * Note that the function will be called with an internal lock held.
+ * Recursive logging is not allowed.
+ */
+void
+_eglSetLogProc(_EGLLogProc logger)
+{
+ EGLint num_messages = 0;
+
+ _eglLockMutex(&logging.mutex);
+
+ if (logging.logger != logger) {
+ logging.logger = logger;
+
+ num_messages = logging.num_messages;
+ logging.num_messages = 0;
+ }
+
+ _eglUnlockMutex(&logging.mutex);
+
+ if (num_messages)
+ _eglLog(_EGL_DEBUG,
+ "New logger installed. "
+ "Messages before the new logger might not be available.");
+}
+
+
+/**
+ * Set the log reporting level.
+ */
+void
+_eglSetLogLevel(EGLint level)
+{
+ switch (level) {
+ case _EGL_FATAL:
+ case _EGL_WARNING:
+ case _EGL_INFO:
+ case _EGL_DEBUG:
+ _eglLockMutex(&logging.mutex);
+ logging.level = level;
+ _eglUnlockMutex(&logging.mutex);
+ break;
+ default:
+ break;
+ }
+}
+
+
+/**
+ * The default logger. It prints the message to stderr.
+ */
+static void
+_eglDefaultLogger(EGLint level, const char *msg)
+{
+ fprintf(stderr, "libEGL %s: %s\n", level_strings[level], msg);
+}
+
+
+/**
+ * Initialize the logging facility.
+ */
+static void
+_eglInitLogger(void)
+{
+ const char *log_env;
+ EGLint i, level = -1;
+
+ if (logging.initialized)
+ return;
+
+ log_env = getenv("EGL_LOG_LEVEL");
+ if (log_env) {
+ for (i = 0; level_strings[i]; i++) {
+ if (_eglstrcasecmp(log_env, level_strings[i]) == 0) {
+ level = i;
+ break;
+ }
+ }
+ }
+ else {
+ level = FALLBACK_LOG_LEVEL;
+ }
+
+ logging.logger = _eglDefaultLogger;
+ logging.level = (level >= 0) ? level : FALLBACK_LOG_LEVEL;
+ logging.initialized = EGL_TRUE;
+
+ /* it is fine to call _eglLog now */
+ if (log_env && level < 0) {
+ _eglLog(_EGL_WARNING,
+ "Unrecognized EGL_LOG_LEVEL environment variable value. "
+ "Expected one of \"fatal\", \"warning\", \"info\", \"debug\". "
+ "Got \"%s\". Falling back to \"%s\".",
+ log_env, level_strings[FALLBACK_LOG_LEVEL]);
+ }
+}
+
+
+/**
+ * Log a message with message logger.
+ * \param level one of _EGL_FATAL, _EGL_WARNING, _EGL_INFO, _EGL_DEBUG.
+ */
+void
+_eglLog(EGLint level, const char *fmtStr, ...)
+{
+ va_list args;
+ char msg[MAXSTRING];
+
+ /* one-time initialization; a little race here is fine */
+ if (!logging.initialized)
+ _eglInitLogger();
+ if (level > logging.level || level < 0)
+ return;
+
+ _eglLockMutex(&logging.mutex);
+
+ if (logging.logger) {
+ va_start(args, fmtStr);
+ vsnprintf(msg, MAXSTRING, fmtStr, args);
+ va_end(args);
+
+ logging.logger(level, msg);
+ logging.num_messages++;
+ }
+
+ _eglUnlockMutex(&logging.mutex);
+
+ if (level == _EGL_FATAL)
+ exit(1); /* or abort()? */
+}
diff --git a/src/egl/main/egllog.h b/src/egl/main/egllog.h
new file mode 100644
index 0000000000..03bef2670f
--- /dev/null
+++ b/src/egl/main/egllog.h
@@ -0,0 +1,29 @@
+#ifndef EGLLOG_INCLUDED
+#define EGLLOG_INCLUDED
+
+
+#include "egltypedefs.h"
+
+
+#define _EGL_FATAL 0 /* unrecoverable error */
+#define _EGL_WARNING 1 /* recoverable error/problem */
+#define _EGL_INFO 2 /* just useful info */
+#define _EGL_DEBUG 3 /* useful info for debugging */
+
+
+typedef void (*_EGLLogProc)(EGLint level, const char *msg);
+
+
+PUBLIC void
+_eglSetLogProc(_EGLLogProc logger);
+
+
+PUBLIC void
+_eglSetLogLevel(EGLint level);
+
+
+PUBLIC void
+_eglLog(EGLint level, const char *fmtStr, ...);
+
+
+#endif /* EGLLOG_INCLUDED */
diff --git a/src/egl/main/eglmisc.c b/src/egl/main/eglmisc.c
new file mode 100644
index 0000000000..4652969659
--- /dev/null
+++ b/src/egl/main/eglmisc.c
@@ -0,0 +1,180 @@
+/**************************************************************************
+ *
+ * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+/**
+ * Small/misc EGL functions
+ */
+
+
+#include <assert.h>
+#include <string.h>
+#include "eglcurrent.h"
+#include "eglmisc.h"
+#include "egldisplay.h"
+
+
+/**
+ * Copy the extension into the string and update the string pointer.
+ */
+static EGLint
+_eglAppendExtension(char **str, const char *ext)
+{
+ char *s = *str;
+ size_t len = strlen(ext);
+
+ if (s) {
+ memcpy(s, ext, len);
+ s[len++] = ' ';
+ s[len] = '\0';
+
+ *str += len;
+ }
+ else {
+ len++;
+ }
+
+ return (EGLint) len;
+}
+
+
+/**
+ * Examine the individual extension enable/disable flags and recompute
+ * the driver's Extensions string.
+ */
+static void
+_eglUpdateExtensionsString(_EGLDisplay *dpy)
+{
+#define _EGL_CHECK_EXTENSION(ext) \
+ do { \
+ if (dpy->Extensions.ext) { \
+ _eglAppendExtension(&exts, "EGL_" #ext); \
+ assert(exts <= dpy->Extensions.String + _EGL_MAX_EXTENSIONS_LEN); \
+ } \
+ } while (0)
+
+ char *exts = dpy->Extensions.String;
+
+ if (exts[0])
+ return;
+
+ _EGL_CHECK_EXTENSION(MESA_screen_surface);
+ _EGL_CHECK_EXTENSION(MESA_copy_context);
+
+ _EGL_CHECK_EXTENSION(KHR_image_base);
+ _EGL_CHECK_EXTENSION(KHR_image_pixmap);
+ if (dpy->Extensions.KHR_image_base && dpy->Extensions.KHR_image_pixmap)
+ _eglAppendExtension(&exts, "EGL_KHR_image");
+
+ _EGL_CHECK_EXTENSION(KHR_vg_parent_image);
+ _EGL_CHECK_EXTENSION(KHR_gl_texture_2D_image);
+ _EGL_CHECK_EXTENSION(KHR_gl_texture_cubemap_image);
+ _EGL_CHECK_EXTENSION(KHR_gl_texture_3D_image);
+ _EGL_CHECK_EXTENSION(KHR_gl_renderbuffer_image);
+
+ _EGL_CHECK_EXTENSION(NOK_swap_region);
+ _EGL_CHECK_EXTENSION(NOK_texture_from_pixmap);
+#undef _EGL_CHECK_EXTENSION
+}
+
+
+static void
+_eglUpdateAPIsString(_EGLDisplay *dpy)
+{
+ char *apis = dpy->ClientAPIs;
+
+ if (apis[0] || !dpy->ClientAPIsMask)
+ return;
+
+ if (dpy->ClientAPIsMask & EGL_OPENGL_BIT)
+ strcat(apis, "OpenGL ");
+
+ if (dpy->ClientAPIsMask & EGL_OPENGL_ES_BIT)
+ strcat(apis, "OpenGL_ES ");
+
+ if (dpy->ClientAPIsMask & EGL_OPENGL_ES2_BIT)
+ strcat(apis, "OpenGL_ES2 ");
+
+ if (dpy->ClientAPIsMask & EGL_OPENVG_BIT)
+ strcat(apis, "OpenVG ");
+
+ assert(strlen(apis) < sizeof(dpy->ClientAPIs));
+}
+
+
+const char *
+_eglQueryString(_EGLDriver *drv, _EGLDisplay *dpy, EGLint name)
+{
+ (void) drv;
+ (void) dpy;
+ switch (name) {
+ case EGL_VENDOR:
+ return _EGL_VENDOR_STRING;
+ case EGL_VERSION:
+ return dpy->Version;
+ case EGL_EXTENSIONS:
+ _eglUpdateExtensionsString(dpy);
+ return dpy->Extensions.String;
+#ifdef EGL_VERSION_1_2
+ case EGL_CLIENT_APIS:
+ _eglUpdateAPIsString(dpy);
+ return dpy->ClientAPIs;
+#endif
+ default:
+ _eglError(EGL_BAD_PARAMETER, "eglQueryString");
+ return NULL;
+ }
+}
+
+
+EGLBoolean
+_eglWaitClient(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx)
+{
+ /* just a placeholder */
+ (void) drv;
+ (void) dpy;
+ (void) ctx;
+ return EGL_TRUE;
+}
+
+
+EGLBoolean
+_eglWaitNative(_EGLDriver *drv, _EGLDisplay *dpy, EGLint engine)
+{
+ /* just a placeholder */
+ (void) drv;
+ (void) dpy;
+ switch (engine) {
+ case EGL_CORE_NATIVE_ENGINE:
+ break;
+ default:
+ _eglError(EGL_BAD_PARAMETER, "eglWaitNative(engine)");
+ return EGL_FALSE;
+ }
+
+ return EGL_TRUE;
+}
diff --git a/src/egl/main/eglmisc.h b/src/egl/main/eglmisc.h
new file mode 100644
index 0000000000..5e6a2d41df
--- /dev/null
+++ b/src/egl/main/eglmisc.h
@@ -0,0 +1,48 @@
+/**************************************************************************
+ *
+ * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+
+#ifndef EGLMISC_INCLUDED
+#define EGLMISC_INCLUDED
+
+
+#include "egltypedefs.h"
+
+
+extern const char *
+_eglQueryString(_EGLDriver *drv, _EGLDisplay *dpy, EGLint name);
+
+
+extern EGLBoolean
+_eglWaitClient(_EGLDriver *drv, _EGLDisplay *dpy, _EGLContext *ctx);
+
+
+extern EGLBoolean
+_eglWaitNative(_EGLDriver *drv, _EGLDisplay *dpy, EGLint engine);
+
+
+#endif /* EGLMISC_INCLUDED */
diff --git a/src/egl/main/eglmode.c b/src/egl/main/eglmode.c
new file mode 100644
index 0000000000..66446c0495
--- /dev/null
+++ b/src/egl/main/eglmode.c
@@ -0,0 +1,352 @@
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "egldisplay.h"
+#include "egldriver.h"
+#include "eglmode.h"
+#include "eglcurrent.h"
+#include "eglscreen.h"
+#include "eglstring.h"
+
+
+#define MIN2(A, B) (((A) < (B)) ? (A) : (B))
+
+
+/**
+ * Given an EGLModeMESA handle, return the corresponding _EGLMode object
+ * or null if non-existant.
+ */
+_EGLMode *
+_eglLookupMode(EGLModeMESA mode, _EGLDisplay *disp)
+{
+ EGLint scrnum;
+
+ /* loop over all screens on the display */
+ for (scrnum = 0; scrnum < disp->NumScreens; scrnum++) {
+ const _EGLScreen *scrn = disp->Screens[scrnum];
+ EGLint i;
+ /* search list of modes for handle */
+ for (i = 0; i < scrn->NumModes; i++) {
+ if (scrn->Modes[i].Handle == mode) {
+ return scrn->Modes + i;
+ }
+ }
+ }
+
+ return NULL;
+}
+
+
+/**
+ * Add a new mode with the given attributes (width, height, depth, refreshRate)
+ * to the given screen.
+ * Assign a new mode ID/handle to the mode as well.
+ * \return pointer to the new _EGLMode
+ */
+_EGLMode *
+_eglAddNewMode(_EGLScreen *screen, EGLint width, EGLint height,
+ EGLint refreshRate, const char *name)
+{
+ EGLint n;
+ _EGLMode *newModes;
+
+ assert(screen);
+ assert(width > 0);
+ assert(height > 0);
+ assert(refreshRate > 0);
+
+ n = screen->NumModes;
+ newModes = (_EGLMode *) realloc(screen->Modes, (n+1) * sizeof(_EGLMode));
+ if (newModes) {
+ screen->Modes = newModes;
+ screen->Modes[n].Handle = n + 1;
+ screen->Modes[n].Width = width;
+ screen->Modes[n].Height = height;
+ screen->Modes[n].RefreshRate = refreshRate;
+ screen->Modes[n].Optimal = EGL_FALSE;
+ screen->Modes[n].Interlaced = EGL_FALSE;
+ screen->Modes[n].Name = _eglstrdup(name);
+ screen->NumModes++;
+ return screen->Modes + n;
+ }
+ else {
+ return NULL;
+ }
+}
+
+
+
+/**
+ * Parse the attrib_list to fill in the fields of the given _eglMode
+ * Return EGL_FALSE if any errors, EGL_TRUE otherwise.
+ */
+static EGLBoolean
+_eglParseModeAttribs(_EGLMode *mode, const EGLint *attrib_list)
+{
+ EGLint i;
+
+ /* init all attribs to EGL_DONT_CARE */
+ mode->Handle = EGL_DONT_CARE;
+ mode->Width = EGL_DONT_CARE;
+ mode->Height = EGL_DONT_CARE;
+ mode->RefreshRate = EGL_DONT_CARE;
+ mode->Optimal = EGL_DONT_CARE;
+ mode->Interlaced = EGL_DONT_CARE;
+ mode->Name = NULL;
+
+ for (i = 0; attrib_list && attrib_list[i] != EGL_NONE; i++) {
+ switch (attrib_list[i]) {
+ case EGL_MODE_ID_MESA:
+ mode->Handle = attrib_list[++i];
+ if (mode->Handle <= 0) {
+ _eglError(EGL_BAD_PARAMETER, "eglChooseModeMESA(handle)");
+ return EGL_FALSE;
+ }
+ break;
+ case EGL_WIDTH:
+ mode->Width = attrib_list[++i];
+ if (mode->Width <= 0) {
+ _eglError(EGL_BAD_PARAMETER, "eglChooseModeMESA(width)");
+ return EGL_FALSE;
+ }
+ break;
+ case EGL_HEIGHT:
+ mode->Height = attrib_list[++i];
+ if (mode->Height <= 0) {
+ _eglError(EGL_BAD_PARAMETER, "eglChooseModeMESA(height)");
+ return EGL_FALSE;
+ }
+ break;
+ case EGL_REFRESH_RATE_MESA:
+ mode->RefreshRate = attrib_list[++i];
+ if (mode->RefreshRate <= 0) {
+ _eglError(EGL_BAD_PARAMETER, "eglChooseModeMESA(refresh rate)");
+ return EGL_FALSE;
+ }
+ break;
+ case EGL_INTERLACED_MESA:
+ mode->Interlaced = attrib_list[++i];
+ if (mode->Interlaced != EGL_TRUE && mode->Interlaced != EGL_FALSE) {
+ _eglError(EGL_BAD_PARAMETER, "eglChooseModeMESA(interlaced)");
+ return EGL_FALSE;
+ }
+ break;
+ case EGL_OPTIMAL_MESA:
+ mode->Optimal = attrib_list[++i];
+ if (mode->Optimal != EGL_TRUE && mode->Optimal != EGL_FALSE) {
+ _eglError(EGL_BAD_PARAMETER, "eglChooseModeMESA(optimal)");
+ return EGL_FALSE;
+ }
+ break;
+ default:
+ _eglError(EGL_BAD_ATTRIBUTE, "eglChooseModeMESA");
+ return EGL_FALSE;
+ }
+ }
+ return EGL_TRUE;
+}
+
+
+/**
+ * Determine if the candidate mode's attributes are at least as good
+ * as the minimal mode's.
+ * \return EGL_TRUE if qualifies, EGL_FALSE otherwise
+ */
+static EGLBoolean
+_eglModeQualifies(const _EGLMode *c, const _EGLMode *min)
+{
+ if (min->Handle != EGL_DONT_CARE && c->Handle != min->Handle)
+ return EGL_FALSE;
+ if (min->Width != EGL_DONT_CARE && c->Width < min->Width)
+ return EGL_FALSE;
+ if (min->Height != EGL_DONT_CARE && c->Height < min->Height)
+ return EGL_FALSE;
+ if (min->RefreshRate != EGL_DONT_CARE && c->RefreshRate < min->RefreshRate)
+ return EGL_FALSE;
+ if (min->Optimal != EGL_DONT_CARE && c->Optimal != min->Optimal)
+ return EGL_FALSE;
+ if (min->Interlaced != EGL_DONT_CARE && c->Interlaced != min->Interlaced)
+ return EGL_FALSE;
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Return value of given mode attribute, or -1 if bad attrib.
+ */
+static EGLint
+getModeAttrib(const _EGLMode *m, EGLint attrib)
+{
+ switch (attrib) {
+ case EGL_MODE_ID_MESA:
+ return m->Handle;
+ case EGL_WIDTH:
+ return m->Width;
+ case EGL_HEIGHT:
+ return m->Height;
+ case EGL_REFRESH_RATE_MESA:
+ return m->RefreshRate;
+ case EGL_OPTIMAL_MESA:
+ return m->Optimal;
+ case EGL_INTERLACED_MESA:
+ return m->Interlaced;
+ default:
+ return -1;
+ }
+}
+
+
+#define SMALLER 1
+#define LARGER 2
+
+struct sort_info {
+ EGLint Attrib;
+ EGLint Order; /* SMALLER or LARGER */
+};
+
+/* the order of these entries is the priority */
+static struct sort_info SortInfo[] = {
+ { EGL_OPTIMAL_MESA, LARGER },
+ { EGL_INTERLACED_MESA, SMALLER },
+ { EGL_WIDTH, LARGER },
+ { EGL_HEIGHT, LARGER },
+ { EGL_REFRESH_RATE_MESA, LARGER },
+ { EGL_MODE_ID_MESA, SMALLER },
+ { 0, 0 }
+};
+
+
+/**
+ * Compare modes 'a' and 'b' and return -1 if a belongs before b, or 1 if a
+ * belongs after b, or 0 if they're equal.
+ * Used by qsort().
+ */
+static int
+_eglCompareModes(const void *a, const void *b)
+{
+ const _EGLMode *aMode = *((const _EGLMode **) a);
+ const _EGLMode *bMode = *((const _EGLMode **) b);
+ EGLint i;
+
+ for (i = 0; SortInfo[i].Attrib; i++) {
+ const EGLint aVal = getModeAttrib(aMode, SortInfo[i].Attrib);
+ const EGLint bVal = getModeAttrib(bMode, SortInfo[i].Attrib);
+ if (aVal == bVal) {
+ /* a tie */
+ continue;
+ }
+ else if (SortInfo[i].Order == SMALLER) {
+ return (aVal < bVal) ? -1 : 1;
+ }
+ else if (SortInfo[i].Order == LARGER) {
+ return (aVal > bVal) ? -1 : 1;
+ }
+ }
+
+ /* all attributes identical */
+ return 0;
+}
+
+
+/**
+ * Search for EGLModes which match the given attribute list.
+ * Called via eglChooseModeMESA API function.
+ */
+EGLBoolean
+_eglChooseModeMESA(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *scrn,
+ const EGLint *attrib_list, EGLModeMESA *modes,
+ EGLint modes_size, EGLint *num_modes)
+{
+ _EGLMode **modeList, min;
+ EGLint i, count;
+
+ if (!_eglParseModeAttribs(&min, attrib_list)) {
+ /* error code will have been recorded */
+ return EGL_FALSE;
+ }
+
+ /* allocate array of mode pointers */
+ modeList = (_EGLMode **) malloc(modes_size * sizeof(_EGLMode *));
+ if (!modeList) {
+ _eglError(EGL_BAD_MODE_MESA, "eglChooseModeMESA(out of memory)");
+ return EGL_FALSE;
+ }
+
+ /* make array of pointers to qualifying modes */
+ for (i = count = 0; i < scrn->NumModes && count < modes_size; i++) {
+ if (_eglModeQualifies(scrn->Modes + i, &min)) {
+ modeList[count++] = scrn->Modes + i;
+ }
+ }
+
+ /* sort array of pointers */
+ qsort(modeList, count, sizeof(_EGLMode *), _eglCompareModes);
+
+ /* copy mode handles to output array */
+ for (i = 0; i < count; i++) {
+ modes[i] = modeList[i]->Handle;
+ }
+
+ free(modeList);
+
+ *num_modes = count;
+
+ return EGL_TRUE;
+}
+
+
+
+/**
+ * Return all possible modes for the given screen. No sorting of results.
+ * Called via eglGetModesMESA() API function.
+ */
+EGLBoolean
+_eglGetModesMESA(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *scrn,
+ EGLModeMESA *modes, EGLint modes_size, EGLint *num_modes)
+{
+ if (modes) {
+ EGLint i;
+ *num_modes = MIN2(scrn->NumModes, modes_size);
+ for (i = 0; i < *num_modes; i++) {
+ modes[i] = scrn->Modes[i].Handle;
+ }
+ }
+ else {
+ /* just return total number of supported modes */
+ *num_modes = scrn->NumModes;
+ }
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Query an attribute of a mode.
+ */
+EGLBoolean
+_eglGetModeAttribMESA(_EGLDriver *drv, _EGLDisplay *dpy,
+ _EGLMode *m, EGLint attribute, EGLint *value)
+{
+ EGLint v;
+
+ v = getModeAttrib(m, attribute);
+ if (v < 0) {
+ _eglError(EGL_BAD_ATTRIBUTE, "eglGetModeAttribMESA");
+ return EGL_FALSE;
+ }
+ *value = v;
+ return EGL_TRUE;
+}
+
+
+/**
+ * Return human-readable string for given mode.
+ * This is the default function called by eglQueryModeStringMESA().
+ */
+const char *
+_eglQueryModeStringMESA(_EGLDriver *drv, _EGLDisplay *dpy, _EGLMode *m)
+{
+ return m->Name;
+}
diff --git a/src/egl/main/eglmode.h b/src/egl/main/eglmode.h
new file mode 100644
index 0000000000..a089a5e194
--- /dev/null
+++ b/src/egl/main/eglmode.h
@@ -0,0 +1,57 @@
+#ifndef EGLMODE_INCLUDED
+#define EGLMODE_INCLUDED
+
+#include "egltypedefs.h"
+
+
+#define EGL_NO_MODE_MESA 0
+
+
+/**
+ * Data structure which corresponds to an EGLModeMESA.
+ */
+struct _egl_mode
+{
+ EGLModeMESA Handle; /* the public/opaque handle which names this mode */
+ EGLint Width, Height; /* size in pixels */
+ EGLint RefreshRate; /* rate * 1000.0 */
+ EGLint Optimal;
+ EGLint Interlaced;
+ const char *Name;
+
+ /* Other possible attributes */
+ /* interlaced */
+ /* external sync */
+};
+
+
+extern _EGLMode *
+_eglLookupMode(EGLModeMESA mode, _EGLDisplay *dpy);
+
+
+PUBLIC _EGLMode *
+_eglAddNewMode(_EGLScreen *screen, EGLint width, EGLint height,
+ EGLint refreshRate, const char *name);
+
+
+extern EGLBoolean
+_eglChooseModeMESA(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *scrn,
+ const EGLint *attrib_list, EGLModeMESA *modes,
+ EGLint modes_size, EGLint *num_modes);
+
+
+extern EGLBoolean
+_eglGetModesMESA(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *scrn,
+ EGLModeMESA *modes, EGLint modes_size, EGLint *num_modes);
+
+
+extern EGLBoolean
+_eglGetModeAttribMESA(_EGLDriver *drv, _EGLDisplay *dpy, _EGLMode *m,
+ EGLint attribute, EGLint *value);
+
+
+extern const char *
+_eglQueryModeStringMESA(_EGLDriver *drv, _EGLDisplay *dpy, _EGLMode *m);
+
+
+#endif /* EGLMODE_INCLUDED */
diff --git a/src/egl/main/eglmutex.h b/src/egl/main/eglmutex.h
new file mode 100644
index 0000000000..29faba0f24
--- /dev/null
+++ b/src/egl/main/eglmutex.h
@@ -0,0 +1,52 @@
+#ifndef EGLMUTEX_INCLUDED
+#define EGLMUTEX_INCLUDED
+
+#include "eglcompiler.h"
+
+#ifdef PTHREADS
+#include <pthread.h>
+
+typedef pthread_mutex_t _EGLMutex;
+
+static INLINE void _eglInitMutex(_EGLMutex *m)
+{
+ pthread_mutex_init(m, NULL);
+}
+
+static INLINE void
+_eglDestroyMutex(_EGLMutex *m)
+{
+ pthread_mutex_destroy(m);
+}
+
+static INLINE void
+_eglLockMutex(_EGLMutex *m)
+{
+ pthread_mutex_lock(m);
+}
+
+static INLINE void
+_eglUnlockMutex(_EGLMutex *m)
+{
+ pthread_mutex_unlock(m);
+}
+
+#define _EGL_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
+#define _EGL_DECLARE_MUTEX(m) \
+ _EGLMutex m = _EGL_MUTEX_INITIALIZER
+
+#else
+
+typedef int _EGLMutex;
+static INLINE void _eglInitMutex(_EGLMutex *m) { (void) m; }
+static INLINE void _eglDestroyMutex(_EGLMutex *m) { (void) m; }
+static INLINE void _eglLockMutex(_EGLMutex *m) { (void) m; }
+static INLINE void _eglUnlockMutex(_EGLMutex *m) { (void) m; }
+
+#define _EGL_MUTEX_INITIALIZER 0
+#define _EGL_DECLARE_MUTEX(m) \
+ _EGLMutex m = _EGL_MUTEX_INITIALIZER
+
+#endif
+
+#endif /* EGLMUTEX_INCLUDED */
diff --git a/src/egl/main/eglscreen.c b/src/egl/main/eglscreen.c
new file mode 100644
index 0000000000..c47afd6abd
--- /dev/null
+++ b/src/egl/main/eglscreen.c
@@ -0,0 +1,265 @@
+/*
+ * Ideas for screen management extension to EGL.
+ *
+ * Each EGLDisplay has one or more screens (CRTs, Flat Panels, etc).
+ * The screens' handles can be obtained with eglGetScreensMESA().
+ *
+ * A new kind of EGLSurface is possible- one which can be directly scanned
+ * out on a screen. Such a surface is created with eglCreateScreenSurface().
+ *
+ * To actually display a screen surface on a screen, the eglShowSurface()
+ * function is called.
+ */
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "egldisplay.h"
+#include "eglglobals.h"
+#include "eglcurrent.h"
+#include "eglmode.h"
+#include "eglconfig.h"
+#include "eglsurface.h"
+#include "eglscreen.h"
+#include "eglmutex.h"
+
+
+/**
+ * Return a new screen handle/ID.
+ * NOTE: we never reuse these!
+ */
+static EGLScreenMESA
+_eglAllocScreenHandle(void)
+{
+ EGLScreenMESA s;
+
+ _eglLockMutex(_eglGlobal.Mutex);
+ s = _eglGlobal.FreeScreenHandle++;
+ _eglUnlockMutex(_eglGlobal.Mutex);
+
+ return s;
+}
+
+
+/**
+ * Initialize an _EGLScreen object to default values.
+ */
+void
+_eglInitScreen(_EGLScreen *screen)
+{
+ memset(screen, 0, sizeof(_EGLScreen));
+ screen->StepX = 1;
+ screen->StepY = 1;
+}
+
+
+/**
+ * Given a public screen handle, return the internal _EGLScreen object.
+ */
+_EGLScreen *
+_eglLookupScreen(EGLScreenMESA screen, _EGLDisplay *display)
+{
+ EGLint i;
+
+ for (i = 0; i < display->NumScreens; i++) {
+ if (display->Screens[i]->Handle == screen)
+ return display->Screens[i];
+ }
+ return NULL;
+}
+
+
+/**
+ * Add the given _EGLScreen to the display's list of screens.
+ */
+void
+_eglAddScreen(_EGLDisplay *display, _EGLScreen *screen)
+{
+ EGLint n;
+
+ assert(display);
+ assert(screen);
+
+ screen->Handle = _eglAllocScreenHandle();
+ n = display->NumScreens;
+ display->Screens = realloc(display->Screens, (n+1) * sizeof(_EGLScreen *));
+ display->Screens[n] = screen;
+ display->NumScreens++;
+}
+
+
+
+EGLBoolean
+_eglGetScreensMESA(_EGLDriver *drv, _EGLDisplay *display, EGLScreenMESA *screens,
+ EGLint max_screens, EGLint *num_screens)
+{
+ EGLint n;
+
+ if (display->NumScreens > max_screens) {
+ n = max_screens;
+ }
+ else {
+ n = display->NumScreens;
+ }
+
+ if (screens) {
+ EGLint i;
+ for (i = 0; i < n; i++)
+ screens[i] = display->Screens[i]->Handle;
+ }
+ if (num_screens)
+ *num_screens = n;
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Drivers should do a proper implementation.
+ */
+_EGLSurface *
+_eglCreateScreenSurfaceMESA(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
+ const EGLint *attrib_list)
+{
+ return NULL;
+}
+
+
+/**
+ * Show the given surface on the named screen.
+ * If surface is EGL_NO_SURFACE, disable the screen's output.
+ *
+ * This is just a placeholder function; drivers will always override
+ * this with code that _really_ shows the surface.
+ */
+EGLBoolean
+_eglShowScreenSurfaceMESA(_EGLDriver *drv, _EGLDisplay *dpy,
+ _EGLScreen *scrn, _EGLSurface *surf,
+ _EGLMode *mode)
+{
+ if (!surf) {
+ scrn->CurrentSurface = NULL;
+ }
+ else {
+ if (surf->Type != EGL_SCREEN_BIT_MESA) {
+ _eglError(EGL_BAD_SURFACE, "eglShowSurfaceMESA");
+ return EGL_FALSE;
+ }
+ if (surf->Width < mode->Width || surf->Height < mode->Height) {
+ _eglError(EGL_BAD_SURFACE,
+ "eglShowSurfaceMESA(surface smaller than screen size)");
+ return EGL_FALSE;
+ }
+
+ scrn->CurrentSurface = surf;
+ scrn->CurrentMode = mode;
+ }
+ return EGL_TRUE;
+}
+
+
+/**
+ * Set a screen's current display mode.
+ * Note: mode = EGL_NO_MODE is valid (turns off the screen)
+ *
+ * This is just a placeholder function; drivers will always override
+ * this with code that _really_ sets the mode.
+ */
+EGLBoolean
+_eglScreenModeMESA(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *scrn,
+ _EGLMode *m)
+{
+ scrn->CurrentMode = m;
+ return EGL_TRUE;
+}
+
+
+/**
+ * Set a screen's surface origin.
+ */
+EGLBoolean
+_eglScreenPositionMESA(_EGLDriver *drv, _EGLDisplay *dpy,
+ _EGLScreen *scrn, EGLint x, EGLint y)
+{
+ scrn->OriginX = x;
+ scrn->OriginY = y;
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Query a screen's current surface.
+ */
+EGLBoolean
+_eglQueryScreenSurfaceMESA(_EGLDriver *drv, _EGLDisplay *dpy,
+ _EGLScreen *scrn, _EGLSurface **surf)
+{
+ *surf = scrn->CurrentSurface;
+ return EGL_TRUE;
+}
+
+
+/**
+ * Query a screen's current mode.
+ */
+EGLBoolean
+_eglQueryScreenModeMESA(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *scrn,
+ _EGLMode **m)
+{
+ *m = scrn->CurrentMode;
+ return EGL_TRUE;
+}
+
+
+EGLBoolean
+_eglQueryScreenMESA(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *scrn,
+ EGLint attribute, EGLint *value)
+{
+ switch (attribute) {
+ case EGL_SCREEN_POSITION_MESA:
+ value[0] = scrn->OriginX;
+ value[1] = scrn->OriginY;
+ break;
+ case EGL_SCREEN_POSITION_GRANULARITY_MESA:
+ value[0] = scrn->StepX;
+ value[1] = scrn->StepY;
+ break;
+ default:
+ _eglError(EGL_BAD_ATTRIBUTE, "eglQueryScreenMESA");
+ return EGL_FALSE;
+ }
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Delete the modes associated with given screen.
+ */
+void
+_eglDestroyScreenModes(_EGLScreen *scrn)
+{
+ EGLint i;
+ for (i = 0; i < scrn->NumModes; i++) {
+ if (scrn->Modes[i].Name)
+ free((char *) scrn->Modes[i].Name); /* cast away const */
+ }
+ if (scrn->Modes)
+ free(scrn->Modes);
+ scrn->Modes = NULL;
+ scrn->NumModes = 0;
+}
+
+
+/**
+ * Default fallback routine - drivers should usually override this.
+ */
+void
+_eglDestroyScreen(_EGLScreen *scrn)
+{
+ _eglDestroyScreenModes(scrn);
+ free(scrn);
+}
+
diff --git a/src/egl/main/eglscreen.h b/src/egl/main/eglscreen.h
new file mode 100644
index 0000000000..0fd71f71fc
--- /dev/null
+++ b/src/egl/main/eglscreen.h
@@ -0,0 +1,89 @@
+#ifndef EGLSCREEN_INCLUDED
+#define EGLSCREEN_INCLUDED
+
+
+#include "egltypedefs.h"
+
+
+/**
+ * Per-screen information.
+ * Note that an EGL screen doesn't have a size. A screen may be set to
+ * one of several display modes (width/height/scanrate). The screen
+ * then displays a drawing surface. The drawing surface must be at least
+ * as large as the display mode's resolution. If it's larger, the
+ * OriginX and OriginY fields control what part of the surface is visible
+ * on the screen.
+ */
+struct _egl_screen
+{
+ EGLScreenMESA Handle; /* The public/opaque handle which names this object */
+
+ _EGLMode *CurrentMode;
+ _EGLSurface *CurrentSurface;
+
+ EGLint OriginX, OriginY; /**< Origin of scan-out region w.r.t. surface */
+ EGLint StepX, StepY; /**< Screen position/origin granularity */
+
+ EGLint NumModes;
+ _EGLMode *Modes; /**< array [NumModes] */
+};
+
+
+PUBLIC void
+_eglInitScreen(_EGLScreen *screen);
+
+
+extern _EGLScreen *
+_eglLookupScreen(EGLScreenMESA screen, _EGLDisplay *dpy);
+
+
+PUBLIC void
+_eglAddScreen(_EGLDisplay *display, _EGLScreen *screen);
+
+
+extern EGLBoolean
+_eglGetScreensMESA(_EGLDriver *drv, _EGLDisplay *dpy, EGLScreenMESA *screens, EGLint max_screens, EGLint *num_screens);
+
+
+extern _EGLSurface *
+_eglCreateScreenSurfaceMESA(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, const EGLint *attrib_list);
+
+
+extern EGLBoolean
+_eglShowScreenSurfaceMESA(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *scrn, _EGLSurface *surf, _EGLMode *m);
+
+
+extern EGLBoolean
+_eglScreenModeMESA(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *scrn, _EGLMode *m);
+
+
+extern EGLBoolean
+_eglScreenPositionMESA(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *scrn, EGLint x, EGLint y);
+
+
+extern EGLBoolean
+_eglQueryDisplayMESA(_EGLDriver *drv, _EGLDisplay *dpy, EGLint attribute, EGLint *value);
+
+
+extern EGLBoolean
+_eglQueryScreenSurfaceMESA(_EGLDriver *drv, _EGLDisplay *dpy,
+ _EGLScreen *scrn, _EGLSurface **surface);
+
+
+extern EGLBoolean
+_eglQueryScreenModeMESA(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *scrn, _EGLMode **m);
+
+
+extern EGLBoolean
+_eglQueryScreenMESA(_EGLDriver *drv, _EGLDisplay *dpy, _EGLScreen *scrn, EGLint attribute, EGLint *value);
+
+
+extern void
+_eglDestroyScreenModes(_EGLScreen *scrn);
+
+
+PUBLIC void
+_eglDestroyScreen(_EGLScreen *scrn);
+
+
+#endif /* EGLSCREEN_INCLUDED */
diff --git a/src/egl/main/eglstring.c b/src/egl/main/eglstring.c
new file mode 100644
index 0000000000..e4ab19136f
--- /dev/null
+++ b/src/egl/main/eglstring.c
@@ -0,0 +1,24 @@
+/**
+ * String utils.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include "eglstring.h"
+
+
+char *
+_eglstrdup(const char *s)
+{
+ if (s) {
+ size_t l = strlen(s);
+ char *s2 = malloc(l + 1);
+ if (s2)
+ strcpy(s2, s);
+ return s2;
+ }
+ return NULL;
+}
+
+
+
diff --git a/src/egl/main/eglstring.h b/src/egl/main/eglstring.h
new file mode 100644
index 0000000000..f1d559b24a
--- /dev/null
+++ b/src/egl/main/eglstring.h
@@ -0,0 +1,18 @@
+#ifndef EGLSTRING_INCLUDED
+#define EGLSTRING_INCLUDED
+
+#include <string.h>
+
+#ifdef _EGL_OS_WINDOWS
+#define _eglstrcasecmp _stricmp
+#define _eglsnprintf _snprintf
+#else
+#define _eglstrcasecmp strcasecmp
+#define _eglsnprintf snprintf
+#endif
+
+extern char *
+_eglstrdup(const char *s);
+
+
+#endif /* EGLSTRING_INCLUDED */
diff --git a/src/egl/main/eglsurface.c b/src/egl/main/eglsurface.c
new file mode 100644
index 0000000000..d46bdb0672
--- /dev/null
+++ b/src/egl/main/eglsurface.c
@@ -0,0 +1,556 @@
+/**
+ * Surface-related functions.
+ */
+
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include "egldisplay.h"
+#include "eglcontext.h"
+#include "eglconfig.h"
+#include "eglcurrent.h"
+#include "egllog.h"
+#include "eglsurface.h"
+
+
+static void
+_eglClampSwapInterval(_EGLSurface *surf, EGLint interval)
+{
+ EGLint bound = GET_CONFIG_ATTRIB(surf->Config, EGL_MAX_SWAP_INTERVAL);
+ if (interval >= bound) {
+ interval = bound;
+ }
+ else {
+ bound = GET_CONFIG_ATTRIB(surf->Config, EGL_MIN_SWAP_INTERVAL);
+ if (interval < bound)
+ interval = bound;
+ }
+ surf->SwapInterval = interval;
+}
+
+
+/**
+ * Parse the list of surface attributes and return the proper error code.
+ */
+static EGLint
+_eglParseSurfaceAttribList(_EGLSurface *surf, const EGLint *attrib_list)
+{
+ _EGLDisplay *dpy = surf->Resource.Display;
+ EGLint type = surf->Type;
+ EGLint texture_type = EGL_PBUFFER_BIT;
+ EGLint i, err = EGL_SUCCESS;
+
+ if (!attrib_list)
+ return EGL_SUCCESS;
+
+ if (dpy->Extensions.NOK_texture_from_pixmap)
+ texture_type |= EGL_PIXMAP_BIT;
+
+ for (i = 0; attrib_list[i] != EGL_NONE; i++) {
+ EGLint attr = attrib_list[i++];
+ EGLint val = attrib_list[i];
+
+ switch (attr) {
+ /* common (except for screen surfaces) attributes */
+ case EGL_VG_COLORSPACE:
+ if (type == EGL_SCREEN_BIT_MESA) {
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+ switch (val) {
+ case EGL_VG_COLORSPACE_sRGB:
+ case EGL_VG_COLORSPACE_LINEAR:
+ break;
+ default:
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+ if (err != EGL_SUCCESS)
+ break;
+ surf->VGColorspace = val;
+ break;
+ case EGL_VG_ALPHA_FORMAT:
+ if (type == EGL_SCREEN_BIT_MESA) {
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+ switch (val) {
+ case EGL_VG_ALPHA_FORMAT_NONPRE:
+ case EGL_VG_ALPHA_FORMAT_PRE:
+ break;
+ default:
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+ if (err != EGL_SUCCESS)
+ break;
+ surf->VGAlphaFormat = val;
+ break;
+ /* window surface attributes */
+ case EGL_RENDER_BUFFER:
+ if (type != EGL_WINDOW_BIT) {
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+ if (val != EGL_BACK_BUFFER && val != EGL_SINGLE_BUFFER) {
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+ surf->RenderBuffer = val;
+ break;
+ /* pbuffer surface attributes */
+ case EGL_WIDTH:
+ if (type != EGL_PBUFFER_BIT && type != EGL_SCREEN_BIT_MESA) {
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+ if (val < 0) {
+ err = EGL_BAD_PARAMETER;
+ break;
+ }
+ surf->Width = val;
+ break;
+ case EGL_HEIGHT:
+ if (type != EGL_PBUFFER_BIT && type != EGL_SCREEN_BIT_MESA) {
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+ if (val < 0) {
+ err = EGL_BAD_PARAMETER;
+ break;
+ }
+ surf->Height = val;
+ break;
+ case EGL_LARGEST_PBUFFER:
+ if (type != EGL_PBUFFER_BIT) {
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+ surf->LargestPbuffer = !!val;
+ break;
+ case EGL_TEXTURE_FORMAT:
+ if (!(type & texture_type)) {
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+ switch (val) {
+ case EGL_TEXTURE_RGB:
+ case EGL_TEXTURE_RGBA:
+ case EGL_NO_TEXTURE:
+ break;
+ default:
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+ if (err != EGL_SUCCESS)
+ break;
+ surf->TextureFormat = val;
+ break;
+ case EGL_TEXTURE_TARGET:
+ if (!(type & texture_type)) {
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+ switch (val) {
+ case EGL_TEXTURE_2D:
+ case EGL_NO_TEXTURE:
+ break;
+ default:
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+ if (err != EGL_SUCCESS)
+ break;
+ surf->TextureTarget = val;
+ break;
+ case EGL_MIPMAP_TEXTURE:
+ if (!(type & texture_type)) {
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+ surf->MipmapTexture = !!val;
+ break;
+ /* no pixmap surface specific attributes */
+ default:
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+
+ if (err != EGL_SUCCESS) {
+ _eglLog(_EGL_WARNING, "bad surface attribute 0x%04x", attr);
+ break;
+ }
+ }
+
+ return err;
+}
+
+
+/**
+ * Do error check on parameters and initialize the given _EGLSurface object.
+ * \return EGL_TRUE if no errors, EGL_FALSE otherwise.
+ */
+EGLBoolean
+_eglInitSurface(_EGLSurface *surf, _EGLDisplay *dpy, EGLint type,
+ _EGLConfig *conf, const EGLint *attrib_list)
+{
+ const char *func;
+ EGLint renderBuffer = EGL_BACK_BUFFER;
+ EGLint err;
+
+ switch (type) {
+ case EGL_WINDOW_BIT:
+ func = "eglCreateWindowSurface";
+ break;
+ case EGL_PIXMAP_BIT:
+ func = "eglCreatePixmapSurface";
+ renderBuffer = EGL_SINGLE_BUFFER;
+ break;
+ case EGL_PBUFFER_BIT:
+ func = "eglCreatePBufferSurface";
+ break;
+ case EGL_SCREEN_BIT_MESA:
+ func = "eglCreateScreenSurface";
+ renderBuffer = EGL_SINGLE_BUFFER; /* XXX correct? */
+ break;
+ default:
+ _eglLog(_EGL_WARNING, "Bad type in _eglInitSurface");
+ return EGL_FALSE;
+ }
+
+ if ((GET_CONFIG_ATTRIB(conf, EGL_SURFACE_TYPE) & type) == 0) {
+ /* The config can't be used to create a surface of this type */
+ _eglError(EGL_BAD_CONFIG, func);
+ return EGL_FALSE;
+ }
+
+ memset(surf, 0, sizeof(_EGLSurface));
+ surf->Resource.Display = dpy;
+ surf->Type = type;
+ surf->Config = conf;
+
+ surf->Width = 0;
+ surf->Height = 0;
+ surf->TextureFormat = EGL_NO_TEXTURE;
+ surf->TextureTarget = EGL_NO_TEXTURE;
+ surf->MipmapTexture = EGL_FALSE;
+ surf->LargestPbuffer = EGL_FALSE;
+ surf->RenderBuffer = renderBuffer;
+ surf->VGAlphaFormat = EGL_VG_ALPHA_FORMAT_NONPRE;
+ surf->VGColorspace = EGL_VG_COLORSPACE_sRGB;
+
+ surf->MipmapLevel = 0;
+ surf->MultisampleResolve = EGL_MULTISAMPLE_RESOLVE_DEFAULT;
+ surf->SwapBehavior = EGL_BUFFER_DESTROYED;
+
+ surf->HorizontalResolution = EGL_UNKNOWN;
+ surf->VerticalResolution = EGL_UNKNOWN;
+ surf->AspectRatio = EGL_UNKNOWN;
+
+ /* the default swap interval is 1 */
+ _eglClampSwapInterval(surf, 1);
+
+ err = _eglParseSurfaceAttribList(surf, attrib_list);
+ if (err != EGL_SUCCESS)
+ return _eglError(err, func);
+
+ return EGL_TRUE;
+}
+
+
+EGLBoolean
+_eglSwapBuffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
+{
+ /* Drivers have to do the actual buffer swap. */
+ return EGL_TRUE;
+}
+
+
+EGLBoolean
+_eglCopyBuffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
+ EGLNativePixmapType target)
+{
+ /* copy surface to native pixmap */
+ /* All implementation burdon for this is in the device driver */
+ return EGL_FALSE;
+}
+
+
+EGLBoolean
+_eglQuerySurface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surface,
+ EGLint attribute, EGLint *value)
+{
+ switch (attribute) {
+ case EGL_WIDTH:
+ *value = surface->Width;
+ break;
+ case EGL_HEIGHT:
+ *value = surface->Height;
+ break;
+ case EGL_CONFIG_ID:
+ *value = GET_CONFIG_ATTRIB(surface->Config, EGL_CONFIG_ID);
+ break;
+ case EGL_LARGEST_PBUFFER:
+ *value = surface->LargestPbuffer;
+ break;
+ case EGL_TEXTURE_FORMAT:
+ /* texture attributes: only for pbuffers, no error otherwise */
+ if (surface->Type == EGL_PBUFFER_BIT)
+ *value = surface->TextureFormat;
+ break;
+ case EGL_TEXTURE_TARGET:
+ if (surface->Type == EGL_PBUFFER_BIT)
+ *value = surface->TextureTarget;
+ break;
+ case EGL_MIPMAP_TEXTURE:
+ if (surface->Type == EGL_PBUFFER_BIT)
+ *value = surface->MipmapTexture;
+ break;
+ case EGL_MIPMAP_LEVEL:
+ if (surface->Type == EGL_PBUFFER_BIT)
+ *value = surface->MipmapLevel;
+ break;
+ case EGL_SWAP_BEHAVIOR:
+ *value = surface->SwapBehavior;
+ break;
+ case EGL_RENDER_BUFFER:
+ *value = surface->RenderBuffer;
+ break;
+ case EGL_PIXEL_ASPECT_RATIO:
+ *value = surface->AspectRatio;
+ break;
+ case EGL_HORIZONTAL_RESOLUTION:
+ *value = surface->HorizontalResolution;
+ break;
+ case EGL_VERTICAL_RESOLUTION:
+ *value = surface->VerticalResolution;
+ break;
+ case EGL_MULTISAMPLE_RESOLVE:
+ *value = surface->MultisampleResolve;
+ break;
+ case EGL_VG_ALPHA_FORMAT:
+ *value = surface->VGAlphaFormat;
+ break;
+ case EGL_VG_COLORSPACE:
+ *value = surface->VGColorspace;
+ break;
+ default:
+ _eglError(EGL_BAD_ATTRIBUTE, "eglQuerySurface");
+ return EGL_FALSE;
+ }
+
+ return EGL_TRUE;
+}
+
+
+/**
+ * Drivers should do a proper implementation.
+ */
+_EGLSurface *
+_eglCreateWindowSurface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
+ EGLNativeWindowType window, const EGLint *attrib_list)
+{
+ return NULL;
+}
+
+
+/**
+ * Drivers should do a proper implementation.
+ */
+_EGLSurface *
+_eglCreatePixmapSurface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
+ EGLNativePixmapType pixmap, const EGLint *attrib_list)
+{
+ return NULL;
+}
+
+
+/**
+ * Drivers should do a proper implementation.
+ */
+_EGLSurface *
+_eglCreatePbufferSurface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf,
+ const EGLint *attrib_list)
+{
+ return NULL;
+}
+
+
+/**
+ * Default fallback routine - drivers should usually override this.
+ */
+EGLBoolean
+_eglDestroySurface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf)
+{
+ if (!_eglIsSurfaceBound(surf))
+ free(surf);
+ return EGL_TRUE;
+}
+
+
+/**
+ * Default fallback routine - drivers might override this.
+ */
+EGLBoolean
+_eglSurfaceAttrib(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surface,
+ EGLint attribute, EGLint value)
+{
+ EGLint confval;
+ EGLint err = EGL_SUCCESS;
+
+ switch (attribute) {
+ case EGL_MIPMAP_LEVEL:
+ confval = GET_CONFIG_ATTRIB(surface->Config, EGL_RENDERABLE_TYPE);
+ if (!(confval & (EGL_OPENGL_ES_BIT | EGL_OPENGL_ES2_BIT))) {
+ err = EGL_BAD_PARAMETER;
+ break;
+ }
+ surface->MipmapLevel = value;
+ break;
+ case EGL_MULTISAMPLE_RESOLVE:
+ switch (value) {
+ case EGL_MULTISAMPLE_RESOLVE_DEFAULT:
+ break;
+ case EGL_MULTISAMPLE_RESOLVE_BOX:
+ confval = GET_CONFIG_ATTRIB(surface->Config, EGL_SURFACE_TYPE);
+ if (!(confval & EGL_MULTISAMPLE_RESOLVE_BOX_BIT))
+ err = EGL_BAD_MATCH;
+ break;
+ default:
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+ if (err != EGL_SUCCESS)
+ break;
+ surface->MultisampleResolve = value;
+ break;
+ case EGL_SWAP_BEHAVIOR:
+ switch (value) {
+ case EGL_BUFFER_DESTROYED:
+ break;
+ case EGL_BUFFER_PRESERVED:
+ confval = GET_CONFIG_ATTRIB(surface->Config, EGL_SURFACE_TYPE);
+ if (!(confval & EGL_SWAP_BEHAVIOR_PRESERVED_BIT))
+ err = EGL_BAD_MATCH;
+ break;
+ default:
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+ if (err != EGL_SUCCESS)
+ break;
+ surface->SwapBehavior = value;
+ break;
+ default:
+ err = EGL_BAD_ATTRIBUTE;
+ break;
+ }
+
+ if (err != EGL_SUCCESS)
+ return _eglError(err, "eglSurfaceAttrib");
+ return EGL_TRUE;
+}
+
+
+EGLBoolean
+_eglBindTexImage(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surface,
+ EGLint buffer)
+{
+ EGLint texture_type = EGL_PBUFFER_BIT;
+
+ /* Just do basic error checking and return success/fail.
+ * Drivers must implement the real stuff.
+ */
+
+ if (dpy->Extensions.NOK_texture_from_pixmap)
+ texture_type |= EGL_PIXMAP_BIT;
+
+ if (!(surface->Type & texture_type)) {
+ _eglError(EGL_BAD_SURFACE, "eglBindTexImage");
+ return EGL_FALSE;
+ }
+
+ if (surface->TextureFormat == EGL_NO_TEXTURE) {
+ _eglError(EGL_BAD_MATCH, "eglBindTexImage");
+ return EGL_FALSE;
+ }
+
+ if (surface->TextureTarget == EGL_NO_TEXTURE) {
+ _eglError(EGL_BAD_MATCH, "eglBindTexImage");
+ return EGL_FALSE;
+ }
+
+ if (buffer != EGL_BACK_BUFFER) {
+ _eglError(EGL_BAD_PARAMETER, "eglBindTexImage");
+ return EGL_FALSE;
+ }
+
+ surface->BoundToTexture = EGL_TRUE;
+
+ return EGL_TRUE;
+}
+
+
+EGLBoolean
+_eglReleaseTexImage(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surface,
+ EGLint buffer)
+{
+ /* Just do basic error checking and return success/fail.
+ * Drivers must implement the real stuff.
+ */
+
+ if (surface->Type != EGL_PBUFFER_BIT) {
+ _eglError(EGL_BAD_SURFACE, "eglBindTexImage");
+ return EGL_FALSE;
+ }
+
+ if (surface->TextureFormat == EGL_NO_TEXTURE) {
+ _eglError(EGL_BAD_MATCH, "eglBindTexImage");
+ return EGL_FALSE;
+ }
+
+ if (buffer != EGL_BACK_BUFFER) {
+ _eglError(EGL_BAD_PARAMETER, "eglReleaseTexImage");
+ return EGL_FALSE;
+ }
+
+ if (!surface->BoundToTexture) {
+ _eglError(EGL_BAD_SURFACE, "eglReleaseTexImage");
+ return EGL_FALSE;
+ }
+
+ surface->BoundToTexture = EGL_FALSE;
+
+ return EGL_TRUE;
+}
+
+
+EGLBoolean
+_eglSwapInterval(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf,
+ EGLint interval)
+{
+ _eglClampSwapInterval(surf, interval);
+ return EGL_TRUE;
+}
+
+
+#ifdef EGL_VERSION_1_2
+
+/**
+ * Example function - drivers should do a proper implementation.
+ */
+_EGLSurface *
+_eglCreatePbufferFromClientBuffer(_EGLDriver *drv, _EGLDisplay *dpy,
+ EGLenum buftype, EGLClientBuffer buffer,
+ _EGLConfig *conf, const EGLint *attrib_list)
+{
+ if (buftype != EGL_OPENVG_IMAGE) {
+ _eglError(EGL_BAD_PARAMETER, "eglCreatePbufferFromClientBuffer");
+ return NULL;
+ }
+
+ return NULL;
+}
+
+#endif /* EGL_VERSION_1_2 */
diff --git a/src/egl/main/eglsurface.h b/src/egl/main/eglsurface.h
new file mode 100644
index 0000000000..8f520dcdf6
--- /dev/null
+++ b/src/egl/main/eglsurface.h
@@ -0,0 +1,184 @@
+#ifndef EGLSURFACE_INCLUDED
+#define EGLSURFACE_INCLUDED
+
+
+#include "egltypedefs.h"
+#include "egldisplay.h"
+
+
+/**
+ * "Base" class for device driver surfaces.
+ */
+struct _egl_surface
+{
+ /* A surface is a display resource */
+ _EGLResource Resource;
+
+ /* The context that is currently bound to the surface */
+ _EGLContext *CurrentContext;
+
+ _EGLConfig *Config;
+
+ EGLint Type; /* one of EGL_WINDOW_BIT, EGL_PIXMAP_BIT or EGL_PBUFFER_BIT */
+
+ /* attributes set by attribute list */
+ EGLint Width, Height;
+ EGLenum TextureFormat;
+ EGLenum TextureTarget;
+ EGLBoolean MipmapTexture;
+ EGLBoolean LargestPbuffer;
+ EGLenum RenderBuffer;
+ EGLenum VGAlphaFormat;
+ EGLenum VGColorspace;
+
+ /* attributes set by eglSurfaceAttrib */
+ EGLint MipmapLevel;
+ EGLenum MultisampleResolve;
+ EGLenum SwapBehavior;
+
+ EGLint HorizontalResolution, VerticalResolution;
+ EGLint AspectRatio;
+
+ EGLint SwapInterval;
+
+ /* True if the surface is bound to an OpenGL ES texture */
+ EGLBoolean BoundToTexture;
+};
+
+
+PUBLIC EGLBoolean
+_eglInitSurface(_EGLSurface *surf, _EGLDisplay *dpy, EGLint type,
+ _EGLConfig *config, const EGLint *attrib_list);
+
+
+extern EGLBoolean
+_eglSwapBuffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf);
+
+
+extern EGLBoolean
+_eglCopyBuffers(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf, EGLNativePixmapType target);
+
+
+extern EGLBoolean
+_eglQuerySurface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf, EGLint attribute, EGLint *value);
+
+
+extern _EGLSurface *
+_eglCreateWindowSurface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, EGLNativeWindowType window, const EGLint *attrib_list);
+
+
+extern _EGLSurface *
+_eglCreatePixmapSurface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, EGLNativePixmapType pixmap, const EGLint *attrib_list);
+
+
+extern _EGLSurface *
+_eglCreatePbufferSurface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLConfig *conf, const EGLint *attrib_list);
+
+
+extern EGLBoolean
+_eglDestroySurface(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf);
+
+
+extern EGLBoolean
+_eglSurfaceAttrib(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf, EGLint attribute, EGLint value);
+
+
+PUBLIC extern EGLBoolean
+_eglBindTexImage(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf, EGLint buffer);
+
+
+extern EGLBoolean
+_eglReleaseTexImage(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf, EGLint buffer);
+
+
+extern EGLBoolean
+_eglSwapInterval(_EGLDriver *drv, _EGLDisplay *dpy, _EGLSurface *surf, EGLint interval);
+
+
+#ifdef EGL_VERSION_1_2
+
+extern _EGLSurface *
+_eglCreatePbufferFromClientBuffer(_EGLDriver *drv, _EGLDisplay *dpy,
+ EGLenum buftype, EGLClientBuffer buffer,
+ _EGLConfig *conf, const EGLint *attrib_list);
+
+#endif /* EGL_VERSION_1_2 */
+
+
+/**
+ * Return true if there is a context bound to the surface.
+ *
+ * The binding is considered a reference to the surface. Drivers should not
+ * destroy a surface when it is bound.
+ */
+static INLINE EGLBoolean
+_eglIsSurfaceBound(_EGLSurface *surf)
+{
+ return (surf->CurrentContext != NULL);
+}
+
+
+/**
+ * Link a surface to a display and return the handle of the link.
+ * The handle can be passed to client directly.
+ */
+static INLINE EGLSurface
+_eglLinkSurface(_EGLSurface *surf, _EGLDisplay *dpy)
+{
+ _eglLinkResource(&surf->Resource, _EGL_RESOURCE_SURFACE, dpy);
+ return (EGLSurface) surf;
+}
+
+
+/**
+ * Unlink a linked surface from its display.
+ * Accessing an unlinked surface should generate EGL_BAD_SURFACE error.
+ */
+static INLINE void
+_eglUnlinkSurface(_EGLSurface *surf)
+{
+ _eglUnlinkResource(&surf->Resource, _EGL_RESOURCE_SURFACE);
+}
+
+
+/**
+ * Lookup a handle to find the linked surface.
+ * Return NULL if the handle has no corresponding linked surface.
+ */
+static INLINE _EGLSurface *
+_eglLookupSurface(EGLSurface surface, _EGLDisplay *dpy)
+{
+ _EGLSurface *surf = (_EGLSurface *) surface;
+ if (!dpy || !_eglCheckResource((void *) surf, _EGL_RESOURCE_SURFACE, dpy))
+ surf = NULL;
+ return surf;
+}
+
+
+/**
+ * Return the handle of a linked surface, or EGL_NO_SURFACE.
+ */
+static INLINE EGLSurface
+_eglGetSurfaceHandle(_EGLSurface *surf)
+{
+ _EGLResource *res = (_EGLResource *) surf;
+ return (res && _eglIsResourceLinked(res)) ?
+ (EGLSurface) surf : EGL_NO_SURFACE;
+}
+
+
+/**
+ * Return true if the surface is linked to a display.
+ *
+ * The link is considered a reference to the surface (the display is owning the
+ * surface). Drivers should not destroy a surface when it is linked.
+ */
+static INLINE EGLBoolean
+_eglIsSurfaceLinked(_EGLSurface *surf)
+{
+ _EGLResource *res = (_EGLResource *) surf;
+ return (res && _eglIsResourceLinked(res));
+}
+
+
+#endif /* EGLSURFACE_INCLUDED */
diff --git a/src/egl/main/egltypedefs.h b/src/egl/main/egltypedefs.h
new file mode 100644
index 0000000000..166b133909
--- /dev/null
+++ b/src/egl/main/egltypedefs.h
@@ -0,0 +1,35 @@
+#ifndef EGLTYPEDEFS_INCLUDED
+#define EGLTYPEDEFS_INCLUDED
+
+#define EGL_EGLEXT_PROTOTYPES
+
+#include <EGL/egl.h>
+#include <EGL/eglext.h>
+
+#include "eglcompiler.h"
+
+typedef struct _egl_api _EGLAPI;
+
+typedef struct _egl_config _EGLConfig;
+
+typedef struct _egl_context _EGLContext;
+
+typedef struct _egl_display _EGLDisplay;
+
+typedef struct _egl_driver _EGLDriver;
+
+typedef struct _egl_extensions _EGLExtensions;
+
+typedef struct _egl_image _EGLImage;
+
+typedef struct _egl_mode _EGLMode;
+
+typedef struct _egl_resource _EGLResource;
+
+typedef struct _egl_screen _EGLScreen;
+
+typedef struct _egl_surface _EGLSurface;
+
+typedef struct _egl_thread_info _EGLThreadInfo;
+
+#endif /* EGLTYPEDEFS_INCLUDED */