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 "../PrismES2Defs.h"
  32 
  33 #include <EGL/egl.h>
  34 
  35 #include "eglUtils.h"
  36 
  37 #include "com_sun_prism_es2_EGLFBGLPixelFormat.h"
  38 
  39 /*
  40  * Class:     com_sun_prism_es2_EGLFBGLPixelFormat
  41  * Method:    nCreatePixelFormat
  42  * Signature: (J[I)J
  43  */
  44 JNIEXPORT jlong JNICALL Java_com_sun_prism_es2_EGLFBGLPixelFormat_nCreatePixelFormat
  45 (JNIEnv *env, jclass jeglfbPixelFormat, jlong nativeScreen, jintArray attrArr) {
  46     int eglAttrs[MAX_GLX_ATTRS_LENGTH]; /* value, attr pair plus a None */
  47     jint *attrs;
  48     PixelFormatInfo *pfInfo = NULL;
  49 
  50     EGLConfig config;
  51     int numFBConfigs;
  52 
  53     if (attrArr == NULL) {
  54         return 0;
  55     }
  56     attrs = (*env)->GetIntArrayElements(env, attrArr, NULL);
  57     setEGLAttrs(attrs, eglAttrs);
  58     (*env)->ReleaseIntArrayElements(env, attrArr, attrs, JNI_ABORT);
  59 
  60     EGLDisplay egldisplay = eglGetDisplay(getNativeDisplayType());
  61     if (EGL_NO_DISPLAY == egldisplay) {
  62         fprintf(stderr, "eglGetDisplay returned EGL_NO_DISPLAY");
  63         return 0;
  64     }
  65 
  66     if (!eglInitialize(egldisplay, NULL, NULL)) {
  67         fprintf(stderr, "eglInitialize failed!");
  68         return 0;
  69     }
  70 
  71 #ifdef DEBUG
  72     printf("Requested EGL attributes:\n");
  73     printConfigAttrs(eglAttrs);
  74 #endif
  75 
  76     if (!eglChooseConfig(egldisplay, eglAttrs, &config, 1, &numFBConfigs)) {
  77         fprintf(stderr, "PixelFormat - Failed to get a FBconfig with requested attrs\n");
  78         //cleanup
  79         return 0;
  80     }
  81 #ifdef DEBUG
  82     printf("EGL: Using config\n");
  83     printConfig(egldisplay, config);
  84 #endif
  85 
  86     /* allocate the structure */
  87     pfInfo = (PixelFormatInfo *) malloc(sizeof(PixelFormatInfo));
  88     if (pfInfo == NULL) {
  89         fprintf(stderr, "nCreatePixelFormat: Failed in malloc\n");
  90         return 0;
  91     }
  92 
  93     /* initialize the structure */
  94     initializePixelFormatInfo(pfInfo);
  95     pfInfo->fbConfig = config;
  96 
  97     return ptr_to_jlong(pfInfo);
  98 }
  99