hostsupport/hostopengles20/src/include/common.h
branchbug235_bringup_0
changeset 55 09263774e342
parent 54 067180f57b12
child 56 40cc73c24bf8
equal deleted inserted replaced
54:067180f57b12 55:09263774e342
     1 /* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     2  *
       
     3  * Permission is hereby granted, free of charge, to any person obtaining a
       
     4  * copy of this software and associated documentation files (the "Software"),
       
     5  * to deal in the Software without restriction, including without limitation
       
     6  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
       
     7  * and/or sell copies of the Software, and to permit persons to whom the
       
     8  * Software is furnished to do so, subject to the following conditions:
       
     9  *
       
    10  * The above copyright notice and this permission notice shall be included
       
    11  * in all copies or substantial portions of the Software.
       
    12  *
       
    13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
       
    14  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
       
    15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
       
    16  * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
       
    17  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
       
    18  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
       
    19  *
       
    20  * Initial Contributors:
       
    21  * Nokia Corporation - initial contribution.
       
    22  *
       
    23  * Contributors:
       
    24  *
       
    25  * Description:
       
    26  *
       
    27  */
       
    28 
       
    29 #ifndef COMMON_H_
       
    30 #define COMMON_H_
       
    31 
       
    32 #include <stdio.h>
       
    33 #include <string.h>
       
    34 #include <assert.h>
       
    35 
       
    36 // Correct defines for platform detection.
       
    37 #if(CONFIG_X11 == 1)
       
    38 #	ifndef __unix__
       
    39 #		define __unix__
       
    40 #	endif
       
    41 #elif(CONFIG_OFFSCREEN == 1)
       
    42 #	define __offscreen__
       
    43 #elif(CONFIG_WIN32 == 1)
       
    44 #	define __win32__
       
    45 #else
       
    46 #	error "Backend needs to be enabled!"
       
    47 #endif // else
       
    48 
       
    49 // DLL-mangling.
       
    50 #ifdef _WIN32
       
    51 #	define DGLES2_EXPORT __declspec(dllexport)
       
    52 #	define DGLES2_IMPORT __declspec(dllimport)
       
    53 #	define DGLES2_CALL	 __stdcall
       
    54 #else
       
    55 #	define DGLES2_EXPORT
       
    56 #	define DGLES2_IMPORT extern
       
    57 #	define DGLES2_CALL
       
    58 #endif
       
    59 
       
    60 // For proper imports and exports.
       
    61 #if(defined BUILD_EGL)
       
    62 #	define EGLAPI       extern
       
    63 #	define EGLAPI_BUILD DGLES2_EXPORT
       
    64 #elif(defined BUILD_GLES2)
       
    65 #	define GL_APICALL       DGLES2_EXPORT
       
    66 #	define GL_APICALL_BUILD DGLES2_EXPORT
       
    67 #else
       
    68 #	error "Only to be used with EGL or GLES!"
       
    69 #endif
       
    70 
       
    71 // The actual standard headers.
       
    72 #include "EGL/egl.h"
       
    73 #include "GLES2/gl2.h"
       
    74 #include "GLES2/gl2ext.h"
       
    75 
       
    76 // For malloc
       
    77 #ifdef __APPLE__
       
    78 #	include <stdlib.h>
       
    79 #else
       
    80 #	include <malloc.h>
       
    81 #endif
       
    82 
       
    83 // Debug location aids.
       
    84 #ifdef _MSC_VER
       
    85 #	define __PRETTY_FUNCTION__ __FUNCSIG__
       
    86 #	ifdef _DEBUG
       
    87 #		define CONFIG_DEBUG 1
       
    88 #	endif
       
    89 #else
       
    90 #   if !defined(NDEBUG)
       
    91 #       define CONFIG_DEBUG 1
       
    92 #   endif
       
    93 #endif
       
    94 
       
    95 #define COMMON_STAMP_FMT "%s:%d(%s)"
       
    96 #define COMMON_STAMP_ARGS ,(strchr(__FILE__, '/')?"":__FILE__), __LINE__, __PRETTY_FUNCTION__
       
    97 
       
    98 #define DUMMY() \
       
    99 	fprintf(stderr, "\x1b[41mDUMMY\x1b[0m " COMMON_STAMP_FMT ": Unimplemented!\n" COMMON_STAMP_ARGS)
       
   100 #define STUB_ONCE(format, ...) \
       
   101 	{ \
       
   102 		static int once = 1; \
       
   103 		if(once) \
       
   104 		{ \
       
   105 			fprintf(stderr, "\x1b[43mSTUB ONCE\x1b[0m " COMMON_STAMP_FMT ": " format COMMON_STAMP_ARGS, __VA_ARGS__); \
       
   106 			once = 0; \
       
   107 		} \
       
   108 	} (void) 0
       
   109 #define STUB(format, ...) \
       
   110 	fprintf(stderr, "STUB " COMMON_STAMP_FMT ": " format COMMON_STAMP_ARGS, __VA_ARGS__)
       
   111 
       
   112 #if(CONFIG_DEBUG == 1 && !defined NDEBUG)
       
   113 #	define Dprintf(format, ...) fprintf(stderr, "DEBUG " COMMON_STAMP_FMT ": " format  COMMON_STAMP_ARGS, __VA_ARGS__)
       
   114 #else // NDEBUG
       
   115 #	define Dprintf(format, ...) (void)0
       
   116 #endif // !NDEBUG
       
   117 
       
   118 #if(CONFIG_DEBUG == 1 && !defined NDEBUG)
       
   119 #	define DGLES2_ASSERT assert
       
   120 #else
       
   121 #	define DGLES2_ASSERT
       
   122 #endif
       
   123 
       
   124 // Context acquisition
       
   125 #define DGLES2_NO_RETVAL ;
       
   126 
       
   127 #define DGLES2_ENTER_RET(retval) \
       
   128 	DGLContext* ctx; \
       
   129 	{ \
       
   130 		dglGetLock(); \
       
   131 		ctx = deglGetCurrentContext(); \
       
   132 		if(!ctx) \
       
   133 		{ \
       
   134 			dglReleaseLock(); \
       
   135 			return retval; \
       
   136 		} \
       
   137 		if(!ctx->initialized) \
       
   138 		{ \
       
   139 			if(!DGLContext_initialize(ctx)) \
       
   140 			{ \
       
   141 				dglReleaseLock(); \
       
   142 				return retval; \
       
   143 			} \
       
   144 		} \
       
   145 	} (void) 0
       
   146 
       
   147 // Check the host error API function calls so that the error flag
       
   148 // in the wrapper always contains the latest error.
       
   149 #define DGLES2_LEAVE_RET(retval) \
       
   150 	{ \
       
   151 		DGLContext_getHostError(ctx); \
       
   152 		dglReleaseLock(); \
       
   153 		return retval; \
       
   154 	} (void) 0
       
   155 
       
   156 // Error checking is not necessary when DGLContext_getHostError was already
       
   157 // called at the end of the function body. Also, the EGL<->GLES interface
       
   158 // functions should preserve the error flag, so this macro should be used there.
       
   159 #define DGLES2_LEAVE_NO_ERROR_CHECK_RET(retval) \
       
   160 	{ \
       
   161 		dglReleaseLock(); \
       
   162 		return retval; \
       
   163 	} (void) 0
       
   164 
       
   165 #define DGLES2_ENTER() DGLES2_ENTER_RET(DGLES2_NO_RETVAL)
       
   166 #define DGLES2_LEAVE() DGLES2_LEAVE_RET(DGLES2_NO_RETVAL)
       
   167 #define DGLES2_LEAVE_NO_ERROR_CHECK() DGLES2_LEAVE_NO_ERROR_CHECK_RET(DGLES2_NO_RETVAL)
       
   168 
       
   169 // Surface locking
       
   170 #define DGLES2_LOCK_SURFACE_RET(read, draw, retval) \
       
   171 	do \
       
   172 	{ \
       
   173 		if(!deglLockSurface(read, draw)) \
       
   174 		{ \
       
   175 			DGLES2_LEAVE_NO_ERROR_CHECK_RET(retval); \
       
   176 		} \
       
   177 	} while(0)
       
   178 
       
   179 #define DGLES2_LOCK_DRAW_SURFACE_RET(retval) DGLES2_LOCK_SURFACE_RET(0, 1, retval)
       
   180 #define DGLES2_LOCK_READ_SURFACE_RET(retval) DGLES2_LOCK_SURFACE_RET(1, 0, retval)
       
   181 
       
   182 #define DGLES2_UNLOCK_SURFACE_RET(retval) \
       
   183 	do \
       
   184 	{ \
       
   185 		if(!deglUnlockSurface()) \
       
   186 		{ \
       
   187 			DGLES2_LEAVE_RET(retval); \
       
   188 		} \
       
   189 	} while(0)
       
   190 
       
   191 #define DGLES2_UNLOCK_SURFACE() DGLES2_UNLOCK_SURFACE_RET(DGLES2_NO_RETVAL)
       
   192 
       
   193 #define DGLES2_BEGIN_DRAWING_RET(retval) \
       
   194 	do \
       
   195 	{ \
       
   196 		if(ctx->framebuffer_binding == 0) \
       
   197 		{ \
       
   198 			/* Using default framebuffer. */ \
       
   199 			DGLES2_LOCK_DRAW_SURFACE_RET(retval); \
       
   200 		} \
       
   201 	} while(0)
       
   202 
       
   203 #define DGLES2_BEGIN_DRAWING() DGLES2_BEGIN_DRAWING_RET(DGLES2_NO_RETVAL)
       
   204 
       
   205 #define DGLES2_END_DRAWING_RET(retval) \
       
   206 	do \
       
   207 	{ \
       
   208 		if(ctx->framebuffer_binding == 0) \
       
   209 		{ \
       
   210 			/* Using default framebuffer. */ \
       
   211 			DGLES2_UNLOCK_SURFACE_RET(retval); \
       
   212 		} \
       
   213 		else \
       
   214 		{ \
       
   215 			/* Using user-created framebuffer. */ \
       
   216 			DGLContext_updateFBOAttachmentSiblings(ctx); \
       
   217 		} \
       
   218 	} while(0)
       
   219 
       
   220 #define DGLES2_END_DRAWING() DGLES2_END_DRAWING_RET(DGLES2_NO_RETVAL)
       
   221 
       
   222 #define DGLES2_BEGIN_READING_RET(retval) \
       
   223 	do \
       
   224 	{ \
       
   225 		if(ctx->framebuffer_binding == 0) \
       
   226 		{ \
       
   227 			/* Using default framebuffer. */ \
       
   228 			DGLES2_LOCK_READ_SURFACE_RET(retval); \
       
   229 		} \
       
   230 	} while(0)
       
   231 
       
   232 #define DGLES2_BEGIN_READING() DGLES2_BEGIN_READING_RET(DGLES2_NO_RETVAL)
       
   233 
       
   234 #define DGLES2_END_READING_RET(retval) \
       
   235 	do \
       
   236 	{ \
       
   237 		if(ctx->framebuffer_binding == 0) \
       
   238 		{ \
       
   239 			/* Using default framebuffer. */ \
       
   240 			DGLES2_UNLOCK_SURFACE_RET(retval); \
       
   241 		} \
       
   242 	} while(0)
       
   243 
       
   244 #define DGLES2_END_READING() DGLES2_END_READING_RET(DGLES2_NO_RETVAL)
       
   245 
       
   246 // GL error handling
       
   247 #define DGLES2_ERROR_IF_RET(cond, error_, retval) \
       
   248 	{ \
       
   249 		if(cond) \
       
   250 		{ \
       
   251 			/* Clear the host GL error flag(s). */ \
       
   252 			while(ctx->hgl.GetError() != GL_NO_ERROR); \
       
   253 			DGLContext_setError(ctx, error_); \
       
   254 			dglReleaseLock(); \
       
   255 			return retval; \
       
   256 		} \
       
   257 	} (void) 0
       
   258 
       
   259 #define DGLES2_ERROR_IF(cond, error) DGLES2_ERROR_IF_RET(cond, error, DGLES2_NO_RETVAL)
       
   260 #define DGLES2_ERROR_RET(error, retval) DGLES2_ERROR_IF_RET(1, error, retval)
       
   261 #define DGLES2_ERROR(error) DGLES2_ERROR_IF(1, error)
       
   262 
       
   263 #endif /* COMMON_H_ */