1 /*
   2  * Copyright (c) 2003, 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 #include <jlong.h>
  27 
  28 #include "sun_java2d_opengl_GLXSurfaceData.h"
  29 
  30 #include "OGLRenderQueue.h"
  31 #include "GLXGraphicsConfig.h"
  32 #include "GLXSurfaceData.h"
  33 #include "awt_Component.h"
  34 #include "awt_GraphicsEnv.h"
  35 
  36 /**
  37  * The methods in this file implement the native windowing system specific
  38  * layer (GLX) for the OpenGL-based Java 2D pipeline.
  39  */
  40 
  41 #ifndef HEADLESS
  42 
  43 extern LockFunc       OGLSD_Lock;
  44 extern GetRasInfoFunc OGLSD_GetRasInfo;
  45 extern UnlockFunc     OGLSD_Unlock;
  46 extern DisposeFunc    OGLSD_Dispose;
  47 
  48 extern void
  49     OGLSD_SetNativeDimensions(JNIEnv *env, OGLSDOps *oglsdo, jint w, jint h);
  50 
  51 jboolean surfaceCreationFailed = JNI_FALSE;
  52 
  53 #endif /* !HEADLESS */
  54 
  55 JNIEXPORT void JNICALL
  56 Java_sun_java2d_opengl_GLXSurfaceData_initOps(JNIEnv *env, jobject glxsd,
  57                                               jobject peer, jlong aData)
  58 {
  59 #ifndef HEADLESS
  60     GLXSDOps *glxsdo = (GLXSDOps *)malloc(sizeof(GLXSDOps));
  61 
  62     if (glxsdo == NULL) {
  63         JNU_ThrowOutOfMemoryError(env, "creating native GLX ops");
  64         return;
  65     }
  66 
  67     OGLSDOps *oglsdo = (OGLSDOps *)SurfaceData_InitOps(env, glxsd,
  68                                                        sizeof(OGLSDOps));
  69     if (oglsdo == NULL) {
  70         free(glxsdo);
  71         JNU_ThrowOutOfMemoryError(env, "Initialization of SurfaceData failed.");
  72         return;
  73     }
  74 
  75     J2dTraceLn(J2D_TRACE_INFO, "GLXSurfaceData_initOps");
  76 
  77     oglsdo->privOps = glxsdo;
  78 
  79     oglsdo->sdOps.Lock       = OGLSD_Lock;
  80     oglsdo->sdOps.GetRasInfo = OGLSD_GetRasInfo;
  81     oglsdo->sdOps.Unlock     = OGLSD_Unlock;
  82     oglsdo->sdOps.Dispose    = OGLSD_Dispose;
  83 
  84     oglsdo->drawableType = OGLSD_UNDEFINED;
  85     oglsdo->activeBuffer = GL_FRONT;
  86     oglsdo->needsInit = JNI_TRUE;
  87 
  88     if (peer != NULL) {
  89         glxsdo->window = JNU_CallMethodByName(env, NULL, peer,
  90                                               "getContentWindow", "()J").j;
  91     } else {
  92         glxsdo->window = 0;
  93     }
  94     glxsdo->configData = (AwtGraphicsConfigDataPtr)jlong_to_ptr(aData);
  95     if (glxsdo->configData == NULL) {
  96         free(glxsdo);
  97         JNU_ThrowNullPointerException(env,
  98                                  "Native GraphicsConfig data block missing");
  99         return;
 100     }
 101 
 102     if (glxsdo->configData->glxInfo == NULL) {
 103         free(glxsdo);
 104         JNU_ThrowNullPointerException(env, "GLXGraphicsConfigInfo missing");
 105         return;
 106     }
 107 #endif /* HEADLESS */
 108 }
 109 
 110 #ifndef HEADLESS
 111 
 112 /**
 113  * This function disposes of any native windowing system resources associated
 114  * with this surface.
 115  */
 116 void
 117 OGLSD_DestroyOGLSurface(JNIEnv *env, OGLSDOps *oglsdo)
 118 {
 119     // X Window is free'd later by AWT code...
 120 }
 121 
 122 /**
 123  * Makes the given context current to its associated "scratch" surface.  If
 124  * the operation is successful, this method will return JNI_TRUE; otherwise,
 125  * returns JNI_FALSE.
 126  */
 127 static jboolean
 128 GLXSD_MakeCurrentToScratch(JNIEnv *env, OGLContext *oglc)
 129 {
 130     GLXCtxInfo *ctxInfo;
 131 
 132     J2dTraceLn(J2D_TRACE_INFO, "GLXSD_MakeCurrentToScratch");
 133 
 134     if (oglc == NULL) {
 135         J2dRlsTraceLn(J2D_TRACE_ERROR,
 136                       "GLXSD_MakeCurrentToScratch: context is null");
 137         return JNI_FALSE;
 138     }
 139 
 140     ctxInfo = (GLXCtxInfo *)oglc->ctxInfo;
 141     if (!j2d_glXMakeContextCurrent(awt_display,
 142                                    ctxInfo->scratchSurface,
 143                                    ctxInfo->scratchSurface,
 144                                    ctxInfo->context))
 145     {
 146         J2dRlsTraceLn(J2D_TRACE_ERROR,
 147                       "GLXSD_MakeCurrentToScratch: could not make current");
 148         return JNI_FALSE;
 149     }
 150 
 151     return JNI_TRUE;
 152 }
 153 
 154 /**
 155  * Returns a pointer (as a jlong) to the native GLXGraphicsConfigInfo
 156  * associated with the given OGLSDOps.  This method can be called from
 157  * shared code to retrieve the native GraphicsConfig data in a platform-
 158  * independent manner.
 159  */
 160 jlong
 161 OGLSD_GetNativeConfigInfo(OGLSDOps *oglsdo)
 162 {
 163     GLXSDOps *glxsdo;
 164 
 165     if (oglsdo == NULL) {
 166         J2dRlsTraceLn(J2D_TRACE_ERROR,
 167                       "OGLSD_GetNativeConfigInfo: ops are null");
 168         return 0L;
 169     }
 170 
 171     glxsdo = (GLXSDOps *)oglsdo->privOps;
 172     if (glxsdo == NULL) {
 173         J2dRlsTraceLn(J2D_TRACE_ERROR,
 174                       "OGLSD_GetNativeConfigInfo: glx ops are null");
 175         return 0L;
 176     }
 177 
 178     if (glxsdo->configData == NULL) {
 179         J2dRlsTraceLn(J2D_TRACE_ERROR,
 180                       "OGLSD_GetNativeConfigInfo: config data is null");
 181         return 0L;
 182     }
 183 
 184     return ptr_to_jlong(glxsdo->configData->glxInfo);
 185 }
 186 
 187 /**
 188  * Makes the given GraphicsConfig's context current to its associated
 189  * "scratch" surface.  If there is a problem making the context current,
 190  * this method will return NULL; otherwise, returns a pointer to the
 191  * OGLContext that is associated with the given GraphicsConfig.
 192  */
 193 OGLContext *
 194 OGLSD_SetScratchSurface(JNIEnv *env, jlong pConfigInfo)
 195 {
 196     GLXGraphicsConfigInfo *glxInfo =
 197         (GLXGraphicsConfigInfo *)jlong_to_ptr(pConfigInfo);
 198     OGLContext *oglc;
 199 
 200     J2dTraceLn(J2D_TRACE_INFO, "OGLSD_SetScratchContext");
 201 
 202     if (glxInfo == NULL) {
 203         J2dRlsTraceLn(J2D_TRACE_ERROR,
 204                       "OGLSD_SetScratchContext: glx config info is null");
 205         return NULL;
 206     }
 207 
 208     oglc = glxInfo->context;
 209     if (!GLXSD_MakeCurrentToScratch(env, oglc)) {
 210         return NULL;
 211     }
 212 
 213     if (OGLC_IS_CAP_PRESENT(oglc, CAPS_EXT_FBOBJECT)) {
 214         // the GL_EXT_framebuffer_object extension is present, so this call
 215         // will ensure that we are bound to the scratch pbuffer (and not
 216         // some other framebuffer object)
 217         j2d_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
 218     }
 219 
 220     return oglc;
 221 }
 222 
 223 /**
 224  * Makes a context current to the given source and destination
 225  * surfaces.  If there is a problem making the context current, this method
 226  * will return NULL; otherwise, returns a pointer to the OGLContext that is
 227  * associated with the destination surface.
 228  */
 229 OGLContext *
 230 OGLSD_MakeOGLContextCurrent(JNIEnv *env, OGLSDOps *srcOps, OGLSDOps *dstOps)
 231 {
 232     GLXSDOps *dstGLXOps = (GLXSDOps *)dstOps->privOps;
 233     OGLContext *oglc;
 234 
 235     J2dTraceLn(J2D_TRACE_INFO, "OGLSD_MakeOGLContextCurrent");
 236 
 237     oglc = dstGLXOps->configData->glxInfo->context;
 238     if (oglc == NULL) {
 239         J2dRlsTraceLn(J2D_TRACE_ERROR,
 240                       "OGLSD_MakeOGLContextCurrent: context is null");
 241         return NULL;
 242     }
 243 
 244     if (dstOps->drawableType == OGLSD_FBOBJECT) {
 245         OGLContext *currentContext = OGLRenderQueue_GetCurrentContext();
 246 
 247         // first make sure we have a current context (if the context isn't
 248         // already current to some drawable, we will make it current to
 249         // its scratch surface)
 250         if (oglc != currentContext) {
 251             if (!GLXSD_MakeCurrentToScratch(env, oglc)) {
 252                 return NULL;
 253             }
 254         }
 255 
 256         // now bind to the fbobject associated with the destination surface;
 257         // this means that all rendering will go into the fbobject destination
 258         // (note that we unbind the currently bound texture first; this is
 259         // recommended procedure when binding an fbobject)
 260         j2d_glBindTexture(dstOps->textureTarget, 0);
 261         j2d_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, dstOps->fbobjectID);
 262     } else {
 263         GLXSDOps *srcGLXOps = (GLXSDOps *)srcOps->privOps;
 264         GLXCtxInfo *ctxinfo = (GLXCtxInfo *)oglc->ctxInfo;
 265 
 266         // make the context current
 267         if (!j2d_glXMakeContextCurrent(awt_display,
 268                                        dstGLXOps->drawable,
 269                                        srcGLXOps->drawable,
 270                                        ctxinfo->context))
 271         {
 272             J2dRlsTraceLn(J2D_TRACE_ERROR,
 273                 "OGLSD_MakeOGLContextCurrent: could not make current");
 274             return NULL;
 275         }
 276 
 277         if (OGLC_IS_CAP_PRESENT(oglc, CAPS_EXT_FBOBJECT)) {
 278             // the GL_EXT_framebuffer_object extension is present, so we
 279             // must bind to the default (windowing system provided)
 280             // framebuffer
 281             j2d_glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
 282         }
 283     }
 284 
 285     return oglc;
 286 }
 287 
 288 /**
 289  * This function initializes a native window surface and caches the window
 290  * bounds in the given OGLSDOps.  Returns JNI_TRUE if the operation was
 291  * successful; JNI_FALSE otherwise.
 292  */
 293 jboolean
 294 OGLSD_InitOGLWindow(JNIEnv *env, OGLSDOps *oglsdo)
 295 {
 296     GLXSDOps *glxsdo;
 297     Window window;
 298     XWindowAttributes attr;
 299 
 300     J2dTraceLn(J2D_TRACE_INFO, "OGLSD_InitOGLWindow");
 301 
 302     if (oglsdo == NULL) {
 303         J2dRlsTraceLn(J2D_TRACE_ERROR,
 304                       "OGLSD_InitOGLWindow: ops are null");
 305         return JNI_FALSE;
 306     }
 307 
 308     glxsdo = (GLXSDOps *)oglsdo->privOps;
 309     if (glxsdo == NULL) {
 310         J2dRlsTraceLn(J2D_TRACE_ERROR,
 311                       "OGLSD_InitOGLWindow: glx ops are null");
 312         return JNI_FALSE;
 313     }
 314 
 315     window = glxsdo->window;
 316     if (window == 0) {
 317         J2dRlsTraceLn(J2D_TRACE_ERROR,
 318                       "OGLSD_InitOGLWindow: window is invalid");
 319         return JNI_FALSE;
 320     }
 321 
 322     XGetWindowAttributes(awt_display, window, &attr);
 323     oglsdo->width = attr.width;
 324     oglsdo->height = attr.height;
 325 
 326     oglsdo->drawableType = OGLSD_WINDOW;
 327     oglsdo->isOpaque = JNI_TRUE;
 328     oglsdo->xOffset = 0;
 329     oglsdo->yOffset = 0;
 330     glxsdo->drawable = window;
 331     glxsdo->xdrawable = window;
 332 
 333     J2dTraceLn2(J2D_TRACE_VERBOSE, "  created window: w=%d h=%d",
 334                 oglsdo->width, oglsdo->height);
 335 
 336     return JNI_TRUE;
 337 }
 338 
 339 static int
 340 GLXSD_BadAllocXErrHandler(Display *display, XErrorEvent *xerr)
 341 {
 342     if (xerr->error_code == BadAlloc) {
 343         surfaceCreationFailed = JNI_TRUE;
 344     }
 345     return 0;
 346 }
 347 
 348 void
 349 OGLSD_SwapBuffers(JNIEnv *env, jlong window)
 350 {
 351     J2dTraceLn(J2D_TRACE_INFO, "OGLSD_SwapBuffers");
 352 
 353     if (window == 0L) {
 354         J2dRlsTraceLn(J2D_TRACE_ERROR,
 355                       "OGLSD_SwapBuffers: window is null");
 356         return;
 357     }
 358 
 359     j2d_glXSwapBuffers(awt_display, (Window)window);
 360 }
 361 
 362 // needed by Mac OS X port, no-op on other platforms
 363 void
 364 OGLSD_Flush(JNIEnv *env)
 365 {
 366 }
 367 
 368 #endif /* !HEADLESS */