1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #ifndef _Prism_es2_defs_h_
  27 #define _Prism_es2_defs_h_
  28 
  29 #if defined(SOLARIS) || defined(LINUX) || defined(ANDROID_NDK) /* SOLARIS || LINUX */
  30 #define GLX_GLEXT_PROTOTYPES
  31 #define GLX_GLXEXT_PROTOTYPES
  32 #define UNIX
  33 #if defined(IS_EGLX11) || defined(IS_EGLFB)
  34 #define IS_EGL
  35 #else
  36 #define IS_GLX
  37 #endif
  38 
  39 #include <limits.h>
  40 
  41 #ifndef ANDROID_NDK
  42 #include <X11/X.h>
  43 #include <X11/Xlib.h>
  44 #include <X11/Xutil.h>
  45 #include <GL/glx.h>
  46 #endif
  47 
  48 #include <GL/gl.h>
  49 #include <GL/glext.h>
  50 
  51 #ifndef __USE_GNU
  52 #define __USE_GNU
  53 #endif
  54 #include <dlfcn.h>
  55 
  56 #define jlong_to_ptr(value) ((void*)((long)value))
  57 #define ptr_to_jlong(value) (jlong)((long)(value))
  58 
  59 /* Max lenght of value, attr pair plus a None */
  60 #define  MAX_GLX_ATTRS_LENGTH 50
  61 
  62 #ifdef IS_EGL
  63 #include <EGL/egl.h>
  64 #include "eglfb/wrapped_egl.h"
  65 #endif
  66 
  67 #endif /* SOLARIS || LINUX */
  68 
  69 #ifdef __APPLE__
  70 #include <TargetConditionals.h>
  71 
  72 #define jlong_to_ptr(value) (intptr_t)(value)
  73 #define ptr_to_jlong(value) (jlong)((intptr_t)(value))
  74 
  75 #if TARGET_OS_IPHONE /* iOS */
  76 #include <GL/gl.h>
  77 #include <GL/glext.h>
  78 #define IS_EGL
  79 
  80 #elif TARGET_OS_MAC /* MacOSX */
  81 /* Include the OpenGL headers */
  82 #include <GL/gl.h>
  83 #include <GL/glext.h>
  84 #include <OpenGL/OpenGL.h>
  85 #include <OpenGL/CGLTypes.h>
  86 #include "macosx-window-system.h"
  87 
  88 /* Max length of value, attr pair plus a None */
  89 #define MAX_PF_ATTRS_LENGTH 50
  90 #endif /* MacOSX */
  91 #endif /* __APPLE__ */
  92 
  93 #ifdef WIN32 /* WIN32 */
  94 #include <windows.h>
  95 #include <GL/gl.h>
  96 #include <GL/wglext.h>
  97 #include <GL/glext.h>
  98 
  99 #ifndef _WIN32_WINNT
 100 #define _WIN32_WINNT 0x0500
 101 #endif
 102 #ifndef _WIN64
 103 typedef int intptr_t;
 104 #endif
 105 
 106 #define jlong_to_ptr(value) (intptr_t)(value)
 107 #define ptr_to_jlong(value) (jlong)((intptr_t)(value))
 108 #endif /* WIN32 */
 109 
 110 /* platform independent .h files generated by javah */
 111 #include "com_sun_prism_es2_GLContext.h"
 112 #include "com_sun_prism_es2_GLDrawable.h"
 113 #include "com_sun_prism_es2_GLFactory.h"
 114 #include "com_sun_prism_es2_GLPixelFormat.h"
 115 #include "com_sun_prism_es2_GLPixelFormat_Attributes.h"
 116 
 117 /*
 118  * These match the constants in GLPixelFormat
 119  */
 120 #define RED_SIZE        com_sun_prism_es2_GLPixelFormat_Attributes_RED_SIZE
 121 #define GREEN_SIZE      com_sun_prism_es2_GLPixelFormat_Attributes_GREEN_SIZE
 122 #define BLUE_SIZE       com_sun_prism_es2_GLPixelFormat_Attributes_BLUE_SIZE
 123 #define ALPHA_SIZE      com_sun_prism_es2_GLPixelFormat_Attributes_ALPHA_SIZE
 124 #define DEPTH_SIZE      com_sun_prism_es2_GLPixelFormat_Attributes_DEPTH_SIZE
 125 #define DOUBLEBUFFER    com_sun_prism_es2_GLPixelFormat_Attributes_DOUBLEBUFFER
 126 #define ONSCREEN        com_sun_prism_es2_GLPixelFormat_Attributes_ONSCREEN
 127 #define NUM_ITEMS       com_sun_prism_es2_GLPixelFormat_Attributes_NUM_ITEMS
 128 
 129 #ifdef ANDROID_NDK
 130 
 131 #include "com_sun_prism_es2_GLPixelFormat_Attributes.h"
 132 
 133 typedef EGLNativeDisplayType Display;
 134 typedef EGLNativeWindowType  Window;
 135 typedef EGLConfig            GLXFBConfig;
 136 typedef unsigned long        Colormap;
 137 typedef unsigned long        PFNGLXSWAPINTERVALSGIPROC;
 138 
 139 #include <android/log.h>
 140 #include <string.h>
 141 #define TAG "javafx"
 142 #define printf(...) ((void)__android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__))
 143 #define fprintf(IGNORE, ...) ((void)__android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__))
 144 #endif
 145 
 146 /* Typedef for pixelformat properties struct */
 147 typedef struct PixelFormatInfoRec PixelFormatInfo;
 148 
 149 /* define the structure to hold the resources and proerties of pixelformat */
 150 struct PixelFormatInfoRec {
 151 #ifdef WIN32 /* WIN32 */
 152     int pixelFormat;
 153     HWND dummyHwnd;
 154     HDC dummyHdc;
 155     LPCTSTR dummySzAppName;
 156 #endif /* WIN32 */
 157 
 158 #ifdef UNIX /* LINUX || SOLARIS */
 159     Display *display;
 160     GLXFBConfig fbConfig;
 161     Window dummyWin;
 162     Colormap dummyCmap;
 163 #endif
 164 
 165 #ifdef MACOSX /* MACOSX */
 166     jlong pixelFormat;
 167 #endif
 168 };
 169 
 170 /* Typedef for drawable properties struct */
 171 typedef struct DrawableInfoRec DrawableInfo;
 172 
 173 /* define the structure to hold the resources and proerties of drawable */
 174 struct DrawableInfoRec {
 175     jboolean onScreen;
 176 
 177 #ifdef WIN32 /* WIN32 */
 178     HDC hdc;
 179     HWND hwnd;
 180 #endif /* WIN32 */
 181 
 182 #ifdef UNIX /* LINUX || SOLARIS */
 183 #ifdef IS_EGL
 184     EGLDisplay *egldisplay;
 185     EGLSurface eglsurface;
 186 #endif
 187 #ifndef IS_EGLFB
 188     Display *display;
 189     Window win;
 190 #endif
 191 #endif
 192 
 193 #ifdef __APPLE__
 194     jlong win;
 195 #endif /* __APPLE__ */
 196 };
 197 
 198 /* Typedef for state properties struct */
 199 typedef struct StateInfoRec StateInfo;
 200 
 201 /* define the structure to hold the states of context */
 202 struct StateInfoRec {
 203     /* For state caching */
 204     jboolean depthWritesEnabled;
 205     jboolean scissorEnabled;
 206     GLclampf clearColor[4];
 207     jboolean vSyncEnabled;
 208 
 209     /* For 3d state caching */
 210     GLboolean cullEnable;
 211     GLenum cullMode;
 212     GLenum fillMode;
 213 
 214     /* Currently bound fbo */
 215     GLuint fbo;
 216 };
 217 
 218 /* Typedef for context properties struct */
 219 typedef struct ContextInfoRec ContextInfo;
 220 
 221 /* define the structure to hold the properties of graphics context */
 222 struct ContextInfoRec {
 223 #ifdef WIN32 /* WIN32 */
 224     HGLRC hglrc;
 225 #endif /* WIN32 */
 226 
 227 #ifdef UNIX /* LINUX || SOLARIS */
 228     Display *display;
 229 #ifdef IS_EGL
 230     EGLContext context;
 231     EGLDisplay *egldisplay;
 232     EGLSurface eglsurface;
 233 #else
 234      GLXContext context;
 235 #endif
 236 
 237 #if defined(IS_GLX) || defined( IS_EGLX11)
 238     /*
 239      * display screen and visualID are cached
 240      * for Factory to pass to Glass
 241      */
 242     int screen;
 243     int visualID;
 244 #endif
 245 #endif
 246 
 247 #ifdef __APPLE__
 248     jlong context;
 249 #endif /* __APPLE__ */
 250 
 251     /* version and extension info */
 252     char *versionStr;
 253     char *vendorStr;
 254     char *rendererStr;
 255     char *glExtensionStr;
 256     int versionNumbers[2];
 257 
 258     /* platform specific extension string and function pointers */
 259 #ifdef WIN32 /* WIN32 */
 260     char *wglExtensionStr;
 261     PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT;
 262 #endif /* WIN32 */
 263 
 264 #ifdef UNIX /* LINUX || SOLARIS */
 265     char *glxExtensionStr;
 266     PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalSGI;
 267 #endif /* LINUX || SOLARIS */
 268 
 269     /* gl function pointers */
 270     PFNGLACTIVETEXTUREPROC glActiveTexture;
 271     PFNGLATTACHSHADERPROC glAttachShader;
 272     PFNGLBINDATTRIBLOCATIONPROC glBindAttribLocation;
 273     PFNGLBINDFRAMEBUFFERPROC glBindFramebuffer;
 274     PFNGLBINDRENDERBUFFERPROC glBindRenderbuffer;
 275     PFNGLCHECKFRAMEBUFFERSTATUSPROC glCheckFramebufferStatus;
 276     PFNGLCOMPILESHADERPROC glCompileShader;
 277     PFNGLCREATEPROGRAMPROC glCreateProgram;
 278     PFNGLCREATESHADERPROC glCreateShader;
 279     PFNGLDELETEBUFFERSPROC glDeleteBuffers;
 280     PFNGLDELETEFRAMEBUFFERSPROC glDeleteFramebuffers;
 281     PFNGLDELETEPROGRAMPROC glDeleteProgram;
 282     PFNGLDELETESHADERPROC glDeleteShader;
 283     PFNGLDELETERENDERBUFFERSPROC glDeleteRenderbuffers;
 284     PFNGLDETACHSHADERPROC glDetachShader;
 285     PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray;
 286     PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray;
 287     PFNGLFRAMEBUFFERRENDERBUFFERPROC glFramebufferRenderbuffer;
 288     PFNGLFRAMEBUFFERTEXTURE2DPROC glFramebufferTexture2D;
 289     PFNGLGENFRAMEBUFFERSPROC glGenFramebuffers;
 290     PFNGLGENRENDERBUFFERSPROC glGenRenderbuffers;
 291     PFNGLGETPROGRAMIVPROC glGetProgramiv;
 292     PFNGLGETSHADERIVPROC glGetShaderiv;
 293     PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation;
 294     PFNGLLINKPROGRAMPROC glLinkProgram;
 295     PFNGLRENDERBUFFERSTORAGEPROC glRenderbufferStorage;
 296     PFNGLSHADERSOURCEPROC glShaderSource;
 297     PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
 298     PFNGLBUFFERSUBDATAPROC glBufferSubData;
 299     PFNGLUNIFORM1FPROC glUniform1f;
 300     PFNGLUNIFORM2FPROC glUniform2f;
 301     PFNGLUNIFORM3FPROC glUniform3f;
 302     PFNGLUNIFORM4FPROC glUniform4f;
 303     PFNGLUNIFORM4FVPROC glUniform4fv;
 304     PFNGLUNIFORM1IPROC glUniform1i;
 305     PFNGLUNIFORM2IPROC glUniform2i;
 306     PFNGLUNIFORM3IPROC glUniform3i;
 307     PFNGLUNIFORM4IPROC glUniform4i;
 308     PFNGLUNIFORM4IVPROC glUniform4iv;
 309     PFNGLUNIFORMMATRIX4FVPROC glUniformMatrix4fv;
 310     PFNGLUSEPROGRAMPROC glUseProgram;
 311     PFNGLVALIDATEPROGRAMPROC glValidateProgram;
 312     PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer;
 313 
 314     PFNGLGENBUFFERSPROC glGenBuffers;
 315     PFNGLBINDBUFFERPROC glBindBuffer;
 316     PFNGLBUFFERDATAPROC glBufferData;
 317     PFNGLTEXIMAGE2DMULTISAMPLEPROC glTexImage2DMultisample;
 318     PFNGLRENDERBUFFERSTORAGEMULTISAMPLEPROC glRenderbufferStorageMultisample;
 319     PFNGLBLITFRAMEBUFFERPROC glBlitFramebuffer;
 320 
 321     /* For state caching */
 322     StateInfo state;
 323 
 324     /* this pointers represent cached values of glVertexAttribPointer values */
 325     /* they should be properly updated in case of glVertexAttribPointer call */
 326     /* see setVertexAttributePointers */
 327     float *vbFloatData;
 328     char  *vbByteData;
 329     jboolean gl2;
 330 
 331     /* Caching properties passed down from Java */
 332     jboolean vSyncRequested;
 333 };
 334 
 335 // extern declarations for core functions
 336 extern int isExtensionSupported(const char *allExtensions, const char *extension);
 337 extern void extractVersionInfo(char *versionStr, int *numbers);
 338 extern void initializeCtxInfo(ContextInfo *ctxInfo);
 339 extern void initializePixelFormatInfo(PixelFormatInfo *pfInfo);
 340 extern void initState(ContextInfo *ctxInfo);
 341 extern void deletePixelFormatInfo(PixelFormatInfo *pfInfo);
 342 
 343 /* Define 3D Primitive data type */
 344 /* define constants and structures to hold the resources of 3D primitive and rendering
 345  * attributes */
 346 #define VC_3D_INDEX 0
 347 #define TC_3D_INDEX 1
 348 #define NC_3D_INDEX 2
 349 #define VC_3D_SIZE 3  /* x, y, z */
 350 #define TC_3D_SIZE 2  /* tu, tv */
 351 #define NC_3D_SIZE 4  /* nx, ny, nz, nw */
 352 #define VERT_3D_SIZE (VC_3D_SIZE + TC_3D_SIZE + NC_3D_SIZE)
 353 #define VERT_3D_STRIDE (sizeof(GLfloat) * VERT_3D_SIZE)
 354 
 355 #define MESH_VERTEXBUFFER 0
 356 #define MESH_INDEXBUFFER 1
 357 #define MESH_MAX_BUFFERS 2
 358 
 359 typedef struct MeshInfoRec MeshInfo;
 360 struct MeshInfoRec {
 361     // vboIDArray[MESH_VERTEXBUFFER] used to store interleave points and tex. coords.
 362     // vboIDArray[MESH_INDEXBUFFER] used to store element indices
 363     GLuint vboIDArray[MESH_MAX_BUFFERS];
 364     GLuint indexBufferSize;
 365     GLenum indexBufferType;
 366 };
 367 
 368 typedef struct PhongMaterialInfoRec PhongMaterialInfo;
 369 struct PhongMaterialInfoRec {
 370    GLfloat diffuseColor[4]; // in the order of rgba
 371    GLuint maps[4];
 372 };
 373 
 374 typedef struct MeshViewInfoRec MeshViewInfo;
 375 struct MeshViewInfoRec {
 376     MeshInfo *meshInfo;
 377     PhongMaterialInfo *phongMaterialInfo;
 378     GLfloat ambientLightColor[3];
 379     GLuint pointLightIndex;
 380     GLfloat pointLightWeight;
 381     GLfloat pointLightPosition[3];
 382     GLfloat pointLightColor[3];
 383     GLboolean cullEnable;
 384     GLenum cullMode;
 385     GLenum fillMode;
 386 };
 387 
 388 /*
 389  * General purpose assertion macro
 390  */
 391 #define PRISMES2_ASSERT(expr) \
 392     if (!(expr)) { \
 393         fprintf(stderr, \
 394             "\nAssertion failed in module '%s' at line %d\n", \
 395             __FILE__, __LINE__); \
 396         fprintf(stderr, "\t%s\n\n", #expr);     \
 397     }
 398 
 399 #endif /* _Prism_es2_defs_h_ */