/**************************************************************************
*
* Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
* Copyright 2009-2010 Chia-I Wu <[email protected]>
* Copyright 2010-2011 LunarG, Inc.
* 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 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.
*
**************************************************************************/
/**
* 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 "eglcontext.h"
#include "egldisplay.h"
#include "egltypedefs.h"
#include "eglcurrent.h"
#include "egldriver.h"
#include "eglsurface.h"
#include "eglconfig.h"
#include "eglscreen.h"
#include "eglmode.h"
#include "eglimage.h"
#include "eglsync.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)
#define _EGL_CHECK_SYNC(disp, s, ret, drv) \
_EGL_CHECK_OBJECT(disp, Sync, s, 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;
}
static INLINE _EGLDriver *
_eglCheckSync(_EGLDisplay *disp, _EGLSync *s, const char *msg)
{
_EGLDriver *drv = _eglCheckDisplay(disp, msg);
if (!drv)
return NULL;
if (!s) {
_eglError(EGL_BAD_PARAMETER, 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)
{
_EGLPlatformType plat = _