1 /*
   2  * Copyright (c) 2012, 2015, 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 "eglWrapper/eglWrapper.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 #include "ios/ios-window-system.h"
  79 #define IS_EGL
  80 
  81 #elif TARGET_OS_MAC /* MacOSX */
  82 /* Include the OpenGL headers */
  83 #include <GL/gl.h>
  84 #include <GL/glext.h>
  85 #include <OpenGL/OpenGL.h>
  86 #include <OpenGL/CGLTypes.h>
  87 #include "macosx-window-system.h"
  88 
  89 /* Max length of value, attr pair plus a None */
  90 #define MAX_PF_ATTRS_LENGTH 50
  91 #endif /* MacOSX */
  92 #endif /* __APPLE__ */
  93 
  94 #ifdef WIN32 /* WIN32 */
  95 #include <windows.h>
  96 #include <GL/gl.h>
  97 #include <GL/wglext.h>
  98 #include <GL/glext.h>
  99 
 100 #ifndef _WIN32_WINNT
 101 #define _WIN32_WINNT 0x0500
 102 #endif
 103 #ifndef _WIN64
 104 typedef int intptr_t;
 105 #endif
 106 
 107 #define jlong_to_ptr(value) (intptr_t)(value)
 108 #define ptr_to_jlong(value) (jlong)((intptr_t)(value))
 109 #endif /* WIN32 */
 110 
 111 /* platform independent .h files generated by javah */
 112 #include "com_sun_prism_es2_GLContext.h"
 113 #include "com_sun_prism_es2_GLDrawable.h"
 114 #include "com_sun_prism_es2_GLFactory.h"
 115 #include "com_sun_prism_es2_GLPixelFormat.h"
 116 #include "com_sun_prism_es2_GLPixelFormat_Attributes.h"
 117 
 118 /*
 119  * These match the constants in GLPixelFormat
 120  */
 121 #define RED_SIZE        com_sun_prism_es2_GLPixelFormat_Attributes_RED_SIZE
 122 #define GREEN_SIZE      com_sun_prism_es2_GLPixelFormat_Attributes_GREEN_SIZE
 123 #define BLUE_SIZE       com_sun_prism_es2_GLPixelFormat_Attributes_BLUE_SIZE
 124 #define ALPHA_SIZE      com_sun_prism_es2_GLPixelFormat_Attributes_ALPHA_SIZE
 125 #define DEPTH_SIZE      com_sun_prism_es2_GLPixelFormat_Attributes_DEPTH_SIZE
 126 #define DOUBLEBUFFER    com_sun_prism_es2_GLPixelFormat_Attributes_DOUBLEBUFFER
 127 #define ONSCREEN        com_sun_prism_es2_GLPixelFormat_Attributes_ONSCREEN
 128 #define NUM_ITEMS       com_sun_prism_es2_GLPixelFormat_Attributes_NUM_ITEMS
 129 
 130 #ifdef ANDROID_NDK
 131 
 132 #include "com_sun_prism_es2_GLPixelFormat_Attributes.h"
 133 
 134 typedef EGLNativeDisplayType Display;
 135 typedef EGLNativeWindowType  Window;
 136 typedef EGLConfig            GLXFBConfig;
 137 typedef unsigned long        Colormap;
 138 typedef unsigned long        PFNGLXSWAPINTERVALSGIPROC;
 139 
 140 #include <android/log.h>
 141 #include <string.h>
 142 #define TAG "javafx"
 143 #define printf(...) ((void)__android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__))
 144 #define fprintf(IGNORE, ...) ((void)__android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__))
 145 #endif
 146 
 147 /* Typedef for pixelformat properties struct */
 148 typedef struct PixelFormatInfoRec PixelFormatInfo;
 149 
 150 /* define the structure to hold the resources and proerties of pixelformat */
 151 struct PixelFormatInfoRec {
 152 #ifdef WIN32 /* WIN32 */
 153     int pixelFormat;
 154     HWND dummyHwnd;
 155     HDC dummyHdc;
 156     LPCTSTR dummySzAppName;
 157 #endif /* WIN32 */
 158 
 159 #ifdef UNIX /* LINUX || SOLARIS */
 160     Display *display;
 161     GLXFBConfig fbConfig;
 162     Window dummyWin;
 163     Colormap dummyCmap;
 164 #endif
 165 
 166 #ifdef MACOSX /* MACOSX */
 167     jlong pixelFormat;
 168 #endif
 169 };
 170 
 171 /* Typedef for drawable properties struct */
 172 typedef struct DrawableInfoRec DrawableInfo;
 173 
 174 /* define the structure to hold the resources and proerties of drawable */
 175 struct DrawableInfoRec {
 176     jboolean onScreen;
 177 
 178 #ifdef WIN32 /* WIN32 */
 179     HDC hdc;
 180     HWND hwnd;
 181 #endif /* WIN32 */
 182 
 183 #ifdef UNIX /* LINUX || SOLARIS */
 184 #ifdef IS_EGL
 185     EGLDisplay *egldisplay;
 186     EGLSurface eglsurface;
 187 #endif
 188 #ifndef IS_EGLFB
 189     Display *display;
 190     Window win;
 191 #endif
 192 #endif
 193 
 194 #ifdef __APPLE__
 195     jlong win;
 196 #endif /* __APPLE__ */
 197 };
 198 
 199 /* Typedef for state properties struct */
 200 typedef struct StateInfoRec StateInfo;
 201 
 202 /* define the structure to hold the states of context */
 203 struct StateInfoRec {
 204     /* For state caching */
 205     jboolean depthWritesEnabled;
 206     jboolean scissorEnabled;
 207     GLclampf clearColor[4];
 208     jboolean vSyncEnabled;
 209 
 210     /* For 3d state caching */
 211     GLboolean cullEnable;
 212     GLenum cullMode;
 213     GLenum fillMode;
 214 
 215     /* Currently bound fbo */
 216     GLuint fbo;
 217 };
 218 
 219 /* Typedef for context properties struct */
 220 typedef struct ContextInfoRec ContextInfo;
 221 
 222 /* define the structure to hold the properties of graphics context */
 223 struct ContextInfoRec {
 224 #ifdef WIN32 /* WIN32 */
 225     HGLRC hglrc;
 226 #endif /* WIN32 */
 227 
 228 #ifdef UNIX /* LINUX || SOLARIS */
 229     Display *display;
 230 #ifdef IS_EGL
 231     EGLContext context;
 232     EGLDisplay *egldisplay;
 233     EGLSurface eglsurface;
 234 #else
 235      GLXContext context;
 236 #endif
 237 
 238 #if defined(IS_GLX) || defined( IS_EGLX11)
 239     /*
 240      * display screen and visualID are cached
 241      * for Factory to pass to Glass
 242      */
 243     int screen;
 244     int visualID;
 245 #endif
 246 #endif
 247 
 248 #ifdef __APPLE__
 249     jlong context;
 250 #endif /* __APPLE__ */
 251 
 252     /* version and extension info */
 253     char *versionStr;
 254     char *vendorStr;
 255     char *rendererStr;
 256     char *glExtensionStr;
 257     int versionNumbers[2];
 258 
 259     /* platform specific extension string and function pointers */
 260 #ifdef WIN32 /* WIN32 */
 261     char *wglExtensionStr;
 262     PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT;
 263 #endif /* WIN32 */
 264 
 265 #ifdef UNIX /* LINUX || SOLARIS */
 266     char *glxExtensionStr;
 267     PFNGLXSWAPINTERVALSGIPROC glXSwapIntervalSGI;
 268 #endif /* LINUX || SOLARIS */
 269 
 270     /* gl function pointers */
 271     PFNGLACTIVETEXTUREPROC glActiveTexture;
 272     PFNGLATTACHSHADERPROC glAttachShader;
 273     PFNGLBINDATTRIBLOCATIONPROC glBindAttribLocation;
 274     PFNGLBINDFRAMEBUFFERPROC glBindFramebuffer;
 275     PFNGLBINDRENDERBUFFERPROC glBindRenderbuffer;
 276     PFNGLCHECKFRAMEBUFFERSTATUSPROC glCheckFramebufferStatus;
 277     PFNGLCOMPILESHADERPROC glCompileShader;
 278     PFNGLCREATEPROGRAMPROC glCreateProgram;
 279     PFNGLCREATESHADERPROC glCreateShader;
 280     PFNGLDELETEBUFFERSPROC glDeleteBuffers;
 281     PFNGLDELETEFRAMEBUFFERSPROC glDeleteFramebuffers;
 282     PFNGLDELETEPROGRAMPROC glDeleteProgram;
 283     PFNGLDELETESHADERPROC glDeleteShader;
 284     PFNGLDELETERENDERBUFFERSPROC glDeleteRenderbuffers;
 285     PFNGLDETACHSHADERPROC glDetachShader;
 286     PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray;
 287     PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray;
 288     PFNGLFRAMEBUFFERRENDERBUFFERPROC glFramebufferRenderbuffer;
 289     PFNGLFRAMEBUFFERTEXTURE2DPROC glFramebufferTexture2D;
 290     PFNGLGENFRAMEBUFFERSPROC glGenFramebuffers;
 291     PFNGLGENRENDERBUFFERSPROC glGenRenderbuffers;
 292     PFNGLGETPROGRAMIVPROC glGetProgramiv;
 293     PFNGLGETSHADERIVPROC glGetShaderiv;
 294     PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation;
 295     PFNGLLINKPROGRAMPROC glLinkProgram;
 296     PFNGLRENDERBUFFERSTORAGEPROC glRenderbufferStorage;
 297     PFNGLSHADERSOURCEPROC glShaderSource;
 298     PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
 299     PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog;
 300     PFNGLBUFFERSUBDATAPROC glBufferSubData;
 301     PFNGLUNIFORM1FPROC glUniform1f;
 302     PFNGLUNIFORM2FPROC glUniform2f;
 303     PFNGLUNIFORM3FPROC glUniform3f;
 304     PFNGLUNIFORM4FPROC glUniform4f;
 305     PFNGLUNIFORM4FVPROC glUniform4fv;
 306     PFNGLUNIFORM1IPROC glUniform1i;
 307     PFNGLUNIFORM2IPROC glUniform2i;
 308     PFNGLUNIFORM3IPROC glUniform3i;
 309     PFNGLUNIFORM4IPROC glUniform4i;
 310     PFNGLUNIFORM4IVPROC glUniform4iv;
 311     PFNGLUNIFORMMATRIX4FVPROC glUniformMatrix4fv;
 312     PFNGLUSEPROGRAMPROC glUseProgram;
 313     PFNGLVALIDATEPROGRAMPROC glValidateProgram;
 314     PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer;
 315 
 316     PFNGLGENBUFFERSPROC glGenBuffers;
 317     PFNGLBINDBUFFERPROC glBindBuffer;
 318     PFNGLBUFFERDATAPROC glBufferData;
 319     PFNGLTEXIMAGE2DMULTISAMPLEPROC glTexImage2DMultisample;
 320     PFNGLRENDERBUFFERSTORAGEMULTISAMPLEPROC glRenderbufferStorageMultisample;
 321     PFNGLBLITFRAMEBUFFERPROC glBlitFramebuffer;
 322 
 323     /* For state caching */
 324     StateInfo state;
 325 
 326     /* this pointers represent cached values of glVertexAttribPointer values */
 327     /* they should be properly updated in case of glVertexAttribPointer call */
 328     /* see setVertexAttributePointers */
 329     float *vbFloatData;
 330     char  *vbByteData;
 331     jboolean gl2;
 332 
 333     /* Caching properties passed down from Java */
 334     jboolean vSyncRequested;
 335 };
 336 
 337 // extern declarations for core functions
 338 extern int isExtensionSupported(const char *allExtensions, const char *extension);
 339 extern void extractVersionInfo(char *versionStr, int *numbers);
 340 extern void initializeCtxInfo(ContextInfo *ctxInfo);
 341 extern void initializePixelFormatInfo(PixelFormatInfo *pfInfo);
 342 extern void initState(ContextInfo *ctxInfo);
 343 extern void deletePixelFormatInfo(PixelFormatInfo *pfInfo);
 344 
 345 /* Define 3D Primitive data type */
 346 /* define constants and structures to hold the resources of 3D primitive and rendering
 347  * attributes */
 348 #define VC_3D_INDEX 0
 349 #define TC_3D_INDEX 1
 350 #define NC_3D_INDEX 2
 351 #define VC_3D_SIZE 3  /* x, y, z */
 352 #define TC_3D_SIZE 2  /* tu, tv */
 353 #define NC_3D_SIZE 4  /* nx, ny, nz, nw */
 354 #define VERT_3D_SIZE (VC_3D_SIZE + TC_3D_SIZE + NC_3D_SIZE)
 355 #define VERT_3D_STRIDE (sizeof(GLfloat) * VERT_3D_SIZE)
 356 
 357 #define MESH_VERTEXBUFFER 0
 358 #define MESH_INDEXBUFFER 1
 359 #define MESH_MAX_BUFFERS 2
 360 
 361 typedef struct MeshInfoRec MeshInfo;
 362 struct MeshInfoRec {
 363     // vboIDArray[MESH_VERTEXBUFFER] used to store interleave points and tex. coords.
 364     // vboIDArray[MESH_INDEXBUFFER] used to store element indices
 365     GLuint vboIDArray[MESH_MAX_BUFFERS];
 366     GLuint indexBufferSize;
 367     GLenum indexBufferType;
 368 };
 369 
 370 typedef struct PhongMaterialInfoRec PhongMaterialInfo;
 371 struct PhongMaterialInfoRec {
 372    GLfloat diffuseColor[4]; // in the order of rgba
 373    GLuint maps[4];
 374 };
 375 
 376 typedef struct MeshViewInfoRec MeshViewInfo;
 377 struct MeshViewInfoRec {
 378     MeshInfo *meshInfo;
 379     PhongMaterialInfo *phongMaterialInfo;
 380     GLfloat ambientLightColor[3];
 381     GLuint pointLightIndex;
 382     GLfloat pointLightWeight;
 383     GLfloat pointLightPosition[3];
 384     GLfloat pointLightColor[3];
 385     GLboolean cullEnable;
 386     GLenum cullMode;
 387     GLenum fillMode;
 388 };
 389 
 390 /*
 391  * General purpose assertion macro
 392  */
 393 #define PRISMES2_ASSERT(expr) \
 394     if (!(expr)) { \
 395         fprintf(stderr, \
 396             "\nAssertion failed in module '%s' at line %d\n", \
 397             __FILE__, __LINE__); \
 398         fprintf(stderr, "\t%s\n\n", #expr);     \
 399     }
 400 
 401 #endif /* _Prism_es2_defs_h_ */