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 <stdlib.h>
  27 #include <assert.h>
  28 #include <stdio.h>
  29 #include <string.h>
  30 #include <math.h>
  31 
  32 #include <EGL/egl.h>
  33 #include <GL/gl.h>
  34 #include "eglUtils.h"
  35 
  36 #include "com_sun_prism_es2_EGLFBGLContext.h"
  37 
  38 extern void initializeDrawableInfo(DrawableInfo *dInfo);
  39 extern void deleteDrawableInfo(DrawableInfo *dInfo);
  40 
  41 /*
  42  * Class:     com_sun_prism_es2_EGLFBGLDrawable
  43  * Method:    nCreateDrawable
  44  * Signature: (JJ)J
  45  */
  46 JNIEXPORT jlong JNICALL Java_com_sun_prism_es2_EGLFBGLDrawable_nCreateDrawable
  47 (JNIEnv *env, jclass jeglfbDrawable, jlong nativeWindow, jlong nativePFInfo) {
  48     DrawableInfo *dInfo = NULL;
  49     PixelFormatInfo *pfInfo = (PixelFormatInfo *) jlong_to_ptr(nativePFInfo);
  50     if (pfInfo == NULL) {
  51         fprintf(stderr, "nCreateDrawable: PixelFormatInfo null\n");
  52         return 0;
  53     }
  54     /* allocate the structure */
  55     dInfo = (DrawableInfo *) malloc(sizeof(DrawableInfo));
  56     if (dInfo == NULL) {
  57         fprintf(stderr, "nCreateDrawable: Failed in malloc\n");
  58         return 0;
  59     }
  60 
  61     /* initialize the structure */
  62     initializeDrawableInfo(dInfo);
  63     dInfo->egldisplay = eglGetDisplay(getNativeDisplayType());
  64     dInfo->eglsurface = getSharedWindowSurface(dInfo->egldisplay,
  65                                                pfInfo->fbConfig,
  66                                                jlong_to_ptr(nativeWindow));
  67     dInfo->onScreen = JNI_TRUE;
  68 
  69     return ptr_to_jlong(dInfo);
  70 }
  71 
  72 /*
  73  * Class:     com_sun_prism_es2_EGLFBGLDrawable
  74  * Method:    nGetDummyDrawable
  75  * Signature: (J)J
  76  */
  77 JNIEXPORT jlong JNICALL Java_com_sun_prism_es2_EGLFBGLDrawable_nGetDummyDrawable
  78 (JNIEnv *env, jclass jeglfbDrawable, jlong nativePFInfo) {
  79     DrawableInfo *dInfo = NULL;
  80     PixelFormatInfo *pfInfo = (PixelFormatInfo *) jlong_to_ptr(nativePFInfo);
  81     if (pfInfo == NULL) {
  82         fprintf(stderr, " GetDummyDrawable, PixelFormatInfo is null\n");
  83         return 0;
  84     }
  85 
  86     /* allocate the structure */
  87     dInfo = (DrawableInfo *) malloc(sizeof(DrawableInfo));
  88     if (dInfo == NULL) {
  89         fprintf(stderr, "nGetDummyDrawable: Failed in malloc\n");
  90         return 0;
  91     }
  92 
  93     /* initialize the structure */
  94     initializeDrawableInfo(dInfo);
  95     dInfo->egldisplay =
  96         eglGetDisplay(getNativeDisplayType());
  97     dInfo->onScreen = JNI_FALSE;
  98     dInfo->eglsurface = getDummyWindowSurface(pfInfo->display,
  99                                               pfInfo->fbConfig);
 100 
 101     return ptr_to_jlong(dInfo);
 102 }
 103 
 104 /*
 105  * Class:     com_sun_prism_es2_EGLFBGLDrawable
 106  * Method:    nSwapBuffers
 107  * Signature: (J)Z
 108  */
 109 JNIEXPORT jboolean JNICALL Java_com_sun_prism_es2_EGLFBGLDrawable_nSwapBuffers
 110 (JNIEnv *env, jclass jeglfbDrawable, jlong nativeDInfo) {
 111     int value;
 112 
 113     DrawableInfo *dInfo = (DrawableInfo *) jlong_to_ptr(nativeDInfo);
 114     if (dInfo == NULL) {
 115         return JNI_FALSE;
 116     }
 117     if (!eglSwapBuffers(dInfo->egldisplay, dInfo->eglsurface)) {
 118         fprintf(stderr, "eglSwapBuffers failed; eglGetError %d\n", eglGetError());
 119     }
 120     return JNI_TRUE;
 121 }
 122