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 #include <jni.h>
  27 #include <stdlib.h>
  28 #include <assert.h>
  29 #include <stdio.h>
  30 #include <string.h>
  31 #include <math.h>
  32 
  33 #include "PrismES2Defs.h"
  34 #include "com_sun_prism_es2_IOSGLContext.h"
  35 
  36 /*
  37  * Class:     com_sun_prism_es2_IOSGLContext
  38  * Method:    nInitialize
  39  * Signature: (JJJ)J
  40  */
  41 JNIEXPORT jlong JNICALL Java_com_sun_prism_es2_IOSGLContext_nInitialize
  42 (JNIEnv *env, jclass class, jlong nativeDInfo, jlong nativePFInfo, jlong nativeShareCtxHandle,
  43  jboolean vSyncRequested)
  44 {
  45     const char *glVersion;
  46     const char *glVendor;
  47     const char *glRenderer;
  48     char *tmpVersionStr;
  49     int  versionNumbers[2];
  50     const char *glExtensions;
  51 
  52     jlong pixelFormat = 0;
  53     jlong win = 0;
  54     jlong context = 0;
  55     int viewNotReady;
  56     jboolean result;
  57     ContextInfo *ctxInfo = NULL;
  58     DrawableInfo* dInfo =  (DrawableInfo* )jlong_to_ptr(nativeDInfo);
  59     PixelFormatInfo* pfInfo =  (PixelFormatInfo* )jlong_to_ptr(nativePFInfo);
  60 
  61     if (dInfo == NULL) {
  62         return 0;
  63     }
  64 
  65     win = dInfo->win;
  66 
  67     context = (jlong) (intptr_t) createContext((void *) (intptr_t) nativeShareCtxHandle,
  68             (void *) (intptr_t) win,
  69             (void *) (intptr_t) pixelFormat, &viewNotReady);
  70 
  71     if (context == 0) {
  72         fprintf(stderr, "Fail in createContext");
  73         return 0;
  74     }
  75 
  76     result = makeCurrentContext((void *) (intptr_t) context);
  77     if (!result) {
  78         printAndReleaseResources(0, context,
  79                 "Fail in makeCurrentContext");
  80         return 0;
  81     }
  82 
  83     /* Get the OpenGL version */
  84     glVersion = (char *)glGetString(GL_VERSION);
  85     if (glVersion == NULL) {
  86         printAndReleaseResources(0, context, "glVersion == null");
  87         return 0;
  88     }
  89 
  90     /* find out the version, major and minor version number */
  91     tmpVersionStr = strdup(glVersion);
  92     extractVersionInfo(tmpVersionStr, versionNumbers);
  93     free(tmpVersionStr);
  94 
  95 
  96     fprintf(stderr, "GL_VERSION string = %s\n", glVersion);
  97     fprintf(stderr, "GL_VERSION (major.minor) = %d.%d\n",
  98             versionNumbers[0], versionNumbers[1]);
  99 
 100     fprintf(stderr, "CTXINFO vendor\n");
 101 
 102     /* Get the OpenGL vendor and renderer */
 103     glVendor = (const char *)glGetString(GL_VENDOR);
 104     if (glVendor == NULL) {
 105         glVendor = "<UNKNOWN>";
 106     }
 107     fprintf(stderr, "CTXINFO renderer\n");
 108     glRenderer = (const char *)glGetString(GL_RENDERER);
 109     if (glRenderer == NULL) {
 110         glRenderer = "<UNKNOWN>";
 111     }
 112     fprintf(stderr, "CTXINFO glExtensions\n");
 113     glExtensions = (const char *)glGetString(GL_EXTENSIONS);
 114     if (glExtensions == NULL) {
 115         printAndReleaseResources(0, context, "glExtensions == null");
 116         return 0;
 117     }
 118     fprintf(stderr, "CTXINFO GL_ARB_pixel_buffer_object\n");
 119 
 120     fprintf(stderr, "CTXINFO allocate the structure\n");
 121     /* allocate the structure */
 122     ctxInfo = (ContextInfo *)malloc(sizeof(ContextInfo));
 123 
 124     /* initialize the structure */
 125     initializeCtxInfo(ctxInfo);
 126     ctxInfo->versionStr = strdup(glVersion);
 127     ctxInfo->vendorStr = strdup(glVendor);
 128     ctxInfo->rendererStr = strdup(glRenderer);
 129     ctxInfo->glExtensionStr = strdup(glExtensions);
 130     ctxInfo->versionNumbers[0] = versionNumbers[0];
 131     ctxInfo->versionNumbers[1] = versionNumbers[1];
 132     ctxInfo->context = context;
 133 
 134     fprintf(stderr, "CTXINFO set function pointers\n");
 135     /* set function pointers */
 136     ctxInfo->glActiveTexture = (PFNGLACTIVETEXTUREPROC)
 137             getProcAddress("glActiveTexture");
 138     ctxInfo->glAttachShader = (PFNGLATTACHSHADERPROC)
 139             getProcAddress("glAttachShader");
 140     ctxInfo->glBindAttribLocation = (PFNGLBINDATTRIBLOCATIONPROC)
 141             getProcAddress("glBindAttribLocation");
 142     ctxInfo->glBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC)
 143             getProcAddress("glBindFramebuffer");
 144     ctxInfo->glBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC)
 145             getProcAddress("glBindRenderbuffer");
 146     ctxInfo->glCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSPROC)
 147             getProcAddress("glCheckFramebufferStatus");
 148     ctxInfo->glCreateProgram = (PFNGLCREATEPROGRAMPROC)
 149             getProcAddress("glCreateProgram");
 150     ctxInfo->glCreateShader = (PFNGLCREATESHADERPROC)
 151             getProcAddress("glCreateShader");
 152     ctxInfo->glCompileShader = (PFNGLCOMPILESHADERPROC)
 153             getProcAddress("glCompileShader");
 154     ctxInfo->glDeleteBuffers = (PFNGLDELETEBUFFERSPROC)
 155             getProcAddress("glDeleteBuffers");
 156     ctxInfo->glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC)
 157             getProcAddress("glDeleteFramebuffers");
 158     ctxInfo->glDeleteProgram = (PFNGLDELETEPROGRAMPROC)
 159             getProcAddress("glDeleteProgram");
 160     ctxInfo->glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSPROC)
 161             getProcAddress("glDeleteRenderbuffers");
 162     ctxInfo->glDeleteShader = (PFNGLDELETESHADERPROC)
 163             getProcAddress("glDeleteShader");
 164     ctxInfo->glDetachShader = (PFNGLDETACHSHADERPROC)
 165             getProcAddress("glDetachShader");
 166     ctxInfo->glDisableVertexAttribArray = (PFNGLDISABLEVERTEXATTRIBARRAYPROC)
 167             getProcAddress("glDisableVertexAttribArray");
 168     ctxInfo->glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)
 169             getProcAddress("glEnableVertexAttribArray");
 170     ctxInfo->glFramebufferRenderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFERPROC)
 171             getProcAddress("glFramebufferRenderbuffer");
 172     ctxInfo->glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC)
 173             getProcAddress("glFramebufferTexture2D");
 174     ctxInfo->glGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC)
 175             getProcAddress("glGenFramebuffers");
 176     ctxInfo->glGenRenderbuffers = (PFNGLGENRENDERBUFFERSPROC)
 177             getProcAddress("glGenRenderbuffers");
 178     ctxInfo->glGetProgramiv = (PFNGLGETPROGRAMIVPROC)
 179             getProcAddress("glGetProgramiv");
 180     ctxInfo->glGetShaderiv = (PFNGLGETSHADERIVPROC)
 181             getProcAddress("glGetShaderiv");
 182     ctxInfo->glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)
 183             getProcAddress("glGetUniformLocation");
 184     ctxInfo->glLinkProgram = (PFNGLLINKPROGRAMPROC)
 185             getProcAddress("glLinkProgram");
 186     ctxInfo->glRenderbufferStorage = (PFNGLRENDERBUFFERSTORAGEPROC)
 187             getProcAddress("glRenderbufferStorage");
 188     ctxInfo->glShaderSource = (PFNGLSHADERSOURCEPROC)
 189             getProcAddress("glShaderSource");
 190     ctxInfo->glUniform1f = (PFNGLUNIFORM1FPROC)
 191             getProcAddress("glUniform1f");
 192     ctxInfo->glUniform2f = (PFNGLUNIFORM2FPROC)
 193             getProcAddress("glUniform2f");
 194     ctxInfo->glUniform3f = (PFNGLUNIFORM3FPROC)
 195             getProcAddress("glUniform3f");
 196     ctxInfo->glUniform4f = (PFNGLUNIFORM4FPROC)
 197             getProcAddress("glUniform4f");
 198     ctxInfo->glUniform4fv = (PFNGLUNIFORM4FVPROC)
 199             getProcAddress("glUniform4fv");
 200     ctxInfo->glUniform1i = (PFNGLUNIFORM1IPROC)
 201             getProcAddress("glUniform1i");
 202     ctxInfo->glUniform2i = (PFNGLUNIFORM2IPROC)
 203             getProcAddress("glUniform2i");
 204     ctxInfo->glUniform3i = (PFNGLUNIFORM3IPROC)
 205             getProcAddress("glUniform3i");
 206     ctxInfo->glUniform4i = (PFNGLUNIFORM4IPROC)
 207             getProcAddress("glUniform4i");
 208     ctxInfo->glUniform4iv = (PFNGLUNIFORM4IVPROC)
 209             getProcAddress("glUniform4iv");
 210     ctxInfo->glUniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC)
 211             getProcAddress("glUniformMatrix4fv");
 212     ctxInfo->glUseProgram = (PFNGLUSEPROGRAMPROC)
 213             getProcAddress("glUseProgram");
 214     ctxInfo->glValidateProgram = (PFNGLVALIDATEPROGRAMPROC)
 215             getProcAddress("glValidateProgram");
 216     ctxInfo->glVertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC)
 217             getProcAddress("glVertexAttribPointer");
 218     ctxInfo->glGenBuffers = (PFNGLGENBUFFERSPROC)
 219             getProcAddress("glGenBuffers");
 220     ctxInfo->glBindBuffer = (PFNGLBINDBUFFERPROC)
 221             getProcAddress("glBindBuffer");
 222     ctxInfo->glBufferData = (PFNGLBUFFERDATAPROC)
 223             getProcAddress("glBufferData");
 224     ctxInfo->glBufferSubData = (PFNGLBUFFERSUBDATAPROC)
 225             getProcAddress("glBufferSubData");
 226     ctxInfo->glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)
 227             getProcAddress("glGetShaderInfoLog");
 228     ctxInfo->glGetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC)
 229             getProcAddress("glGetProgramInfoLog");
 230     ctxInfo->glTexImage2DMultisample = (PFNGLTEXIMAGE2DMULTISAMPLEPROC)
 231             getProcAddress("glTexImage2DMultisample");
 232     ctxInfo->glRenderbufferStorageMultisample = (PFNGLRENDERBUFFERSTORAGEMULTISAMPLEPROC)
 233             getProcAddress("glRenderbufferStorageMultisample");
 234     ctxInfo->glBlitFramebuffer = (PFNGLBLITFRAMEBUFFERPROC)
 235             getProcAddress("glBlitFramebuffer");
 236 
 237     // initialize platform states and properties to match
 238     // cached states and properties
 239     setSwapInterval((void *) jlong_to_ptr(ctxInfo->context), 0);
 240     ctxInfo->state.vSyncEnabled = JNI_FALSE;
 241     ctxInfo->vSyncRequested = vSyncRequested;
 242 
 243     initState(ctxInfo);
 244 
 245     return ptr_to_jlong(ctxInfo);
 246 }
 247 
 248 /*
 249  * Class:     com_sun_prism_es2_IOSGLContext
 250  * Method:    nGetNativeHandle
 251  * Signature: (J)J
 252  */
 253 JNIEXPORT jlong JNICALL Java_com_sun_prism_es2_IOSGLContext_nGetNativeHandle
 254 (JNIEnv *env, jclass class, jlong nativeCtxInfo)
 255 {
 256     ContextInfo* ctxInfo = (ContextInfo*) jlong_to_ptr(nativeCtxInfo);
 257     if (ctxInfo == NULL) {
 258         return 0;
 259     }
 260     return ctxInfo->context;
 261 }
 262 
 263 /*
 264  * Class:     com_sun_prism_es2_IOSGLContext
 265  * Method:    nMakeCurrent
 266  * Signature: (JJ)V
 267  */
 268 JNIEXPORT void JNICALL Java_com_sun_prism_es2_IOSGLContext_nMakeCurrent
 269 (JNIEnv *env, jclass class, jlong nativeCtxInfo, jlong nativeDInfo)
 270 {
 271     ContextInfo* ctxInfo = (ContextInfo*) jlong_to_ptr(nativeCtxInfo);
 272     DrawableInfo* dInfo =  (DrawableInfo* )jlong_to_ptr(nativeDInfo);
 273     int interval;
 274     jboolean vSyncNeeded;
 275 
 276     if ((ctxInfo == NULL) || (dInfo == NULL)) {
 277         return;
 278     }
 279 
 280     if (!makeCurrentContext((void *) (intptr_t) ctxInfo->context)) {
 281         fprintf(stderr, "Failed in makeCurrentContext\n");
 282     }
 283     vSyncNeeded = ctxInfo->vSyncRequested && dInfo->onScreen;
 284     if (vSyncNeeded == ctxInfo->state.vSyncEnabled) {
 285         return;
 286     }
 287     interval = (vSyncNeeded) ? 1 : 0;
 288     ctxInfo->state.vSyncEnabled = vSyncNeeded;
 289     setSwapInterval(ctxInfo->context, interval);
 290     fprintf(stderr, "setSwapInterval(%d)\n", interval);
 291 }