Path: blob/master/src/java.desktop/unix/native/common/java2d/opengl/GLXGraphicsConfig.c
41159 views
/*1* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <stdlib.h>26#include <string.h>2728#include "sun_java2d_opengl_GLXGraphicsConfig.h"2930#include "jni.h"31#include "jlong.h"32#include "GLXGraphicsConfig.h"33#include "GLXSurfaceData.h"34#include "awt_GraphicsEnv.h"35#include "awt_util.h"3637#ifndef HEADLESS3839extern Bool usingXinerama;4041/**42* This is a globally shared context used when creating textures. When any43* new contexts are created, they specify this context as the "share list"44* context, which means any texture objects created when this shared context45* is current will be available to any other context.46*/47static GLXContext sharedContext = 0;4849/**50* Attempts to initialize GLX and the core OpenGL library. For this method51* to return JNI_TRUE, the following must be true:52* - libGL must be loaded successfully (via dlopen)53* - all function symbols from libGL must be available and loaded properly54* - the GLX extension must be available through X1155* - client GLX version must be >= 1.356* If any of these requirements are not met, this method will return57* JNI_FALSE, indicating there is no hope of using GLX/OpenGL for any58* GraphicsConfig in the environment.59*/60static jboolean61GLXGC_InitGLX()62{63int errorbase, eventbase;64const char *version;6566J2dRlsTraceLn(J2D_TRACE_INFO, "GLXGC_InitGLX");6768if (!OGLFuncs_OpenLibrary()) {69return JNI_FALSE;70}7172if (!OGLFuncs_InitPlatformFuncs() ||73!OGLFuncs_InitBaseFuncs() ||74!OGLFuncs_InitExtFuncs())75{76OGLFuncs_CloseLibrary();77return JNI_FALSE;78}7980if (!j2d_glXQueryExtension(awt_display, &errorbase, &eventbase)) {81J2dRlsTraceLn(J2D_TRACE_ERROR,82"GLXGC_InitGLX: GLX extension is not present");83OGLFuncs_CloseLibrary();84return JNI_FALSE;85}8687version = j2d_glXGetClientString(awt_display, GLX_VERSION);88if (version == NULL) {89J2dRlsTraceLn(J2D_TRACE_ERROR,90"GLXGC_InitGLX: could not query GLX version");91OGLFuncs_CloseLibrary();92return JNI_FALSE;93}9495// we now only verify that the client GLX version is >= 1.3 (if the96// server does not support GLX 1.3, then we will find that out later97// when we attempt to create a GLXFBConfig)98J2dRlsTraceLn1(J2D_TRACE_INFO,99"GLXGC_InitGLX: client GLX version=%s", version);100if (!((version[0] == '1' && version[2] >= '3') || (version[0] > '1'))) {101J2dRlsTraceLn(J2D_TRACE_ERROR,102"GLXGC_InitGLX: invalid GLX version; 1.3 is required");103OGLFuncs_CloseLibrary();104return JNI_FALSE;105}106107return JNI_TRUE;108}109110/**111* Returns JNI_TRUE if GLX is available for the current display. Note that112* this method will attempt to initialize GLX (and all the necessary function113* symbols) if it has not been already. The AWT_LOCK must be acquired before114* calling this method.115*/116jboolean117GLXGC_IsGLXAvailable()118{119static jboolean glxAvailable = JNI_FALSE;120static jboolean firstTime = JNI_TRUE;121122J2dTraceLn(J2D_TRACE_INFO, "GLXGC_IsGLXAvailable");123124if (firstTime) {125glxAvailable = GLXGC_InitGLX();126firstTime = JNI_FALSE;127}128129return glxAvailable;130}131132/**133* Disposes all memory and resources allocated for the given OGLContext.134*/135static void136GLXGC_DestroyOGLContext(OGLContext *oglc)137{138GLXCtxInfo *ctxinfo;139140J2dTraceLn(J2D_TRACE_INFO, "GLXGC_DestroyOGLContext");141142if (oglc == NULL) {143J2dRlsTraceLn(J2D_TRACE_ERROR,144"GLXGC_DestroyOGLContext: context is null");145return;146}147148// at this point, this context will be current to its scratch surface149// so the following GL/GLX operations should be safe...150151OGLContext_DestroyContextResources(oglc);152153ctxinfo = (GLXCtxInfo *)oglc->ctxInfo;154if (ctxinfo != NULL) {155// release the current context before we continue156j2d_glXMakeContextCurrent(awt_display, None, None, NULL);157158if (ctxinfo->context != 0) {159j2d_glXDestroyContext(awt_display, ctxinfo->context);160}161if (ctxinfo->scratchSurface != 0) {162j2d_glXDestroyPbuffer(awt_display, ctxinfo->scratchSurface);163}164165free(ctxinfo);166}167168free(oglc);169}170171/**172* Disposes all memory and resources associated with the given173* GLXGraphicsConfigInfo (including its native OGLContext data).174*/175void176OGLGC_DestroyOGLGraphicsConfig(jlong pConfigInfo)177{178GLXGraphicsConfigInfo *glxinfo =179(GLXGraphicsConfigInfo *)jlong_to_ptr(pConfigInfo);180181J2dTraceLn(J2D_TRACE_INFO, "OGLGC_DestroyOGLGraphicsConfig");182183if (glxinfo == NULL) {184J2dRlsTraceLn(J2D_TRACE_ERROR,185"OGLGC_DestroyOGLGraphicsConfig: info is null");186return;187}188189if (glxinfo->context != NULL) {190GLXGC_DestroyOGLContext(glxinfo->context);191}192193free(glxinfo);194}195196/**197* Attempts to create a new GLXFBConfig for the requested screen and visual.198* If visualid is 0, this method will iterate through all GLXFBConfigs (if199* any) that match the requested attributes and will attempt to find an200* fbconfig with a minimal combined depth+stencil buffer. Note that we201* currently only need depth capabilities (for shape clipping purposes), but202* glXChooseFBConfig() will often return a list of fbconfigs with the largest203* depth buffer (and stencil) sizes at the top of the list. Therefore, we204* scan through the whole list to find the most VRAM-efficient fbconfig.205* If visualid is non-zero, the GLXFBConfig associated with the given visual206* is chosen (assuming it meets the requested attributes). If there are no207* valid GLXFBConfigs available, this method returns 0.208*/209static GLXFBConfig210GLXGC_InitFBConfig(JNIEnv *env, jint screennum, VisualID visualid)211{212GLXFBConfig *fbconfigs;213GLXFBConfig chosenConfig = 0;214int nconfs, i;215int attrlist[] = {GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT | GLX_PBUFFER_BIT,216GLX_RENDER_TYPE, GLX_RGBA_BIT,217GLX_CONFIG_CAVEAT, GLX_NONE, // avoid "slow" configs218GLX_DEPTH_SIZE, 16, // anything >= 16 will work for us2190};220221// this is the initial minimum value for the combined depth+stencil size222// (we initialize it to some absurdly high value; realistic values will223// be much less than this number)224int minDepthPlusStencil = 512;225226J2dRlsTraceLn2(J2D_TRACE_INFO, "GLXGC_InitFBConfig: scn=%d vis=0x%x",227screennum, visualid);228229// find all fbconfigs for this screen with the provided attributes230fbconfigs = j2d_glXChooseFBConfig(awt_display, screennum,231attrlist, &nconfs);232233if ((fbconfigs == NULL) || (nconfs <= 0)) {234J2dRlsTraceLn(J2D_TRACE_ERROR,235"GLXGC_InitFBConfig: could not find any valid fbconfigs");236return 0;237}238239J2dRlsTraceLn(J2D_TRACE_VERBOSE, " candidate fbconfigs:");240241// iterate through the list of fbconfigs, looking for the one that matches242// the requested VisualID and supports RGBA rendering as well as the243// creation of windows and pbuffers244for (i = 0; i < nconfs; i++) {245XVisualInfo *xvi;246VisualID fbvisualid;247GLXFBConfig fbc = fbconfigs[i];248249// get VisualID from GLXFBConfig250xvi = j2d_glXGetVisualFromFBConfig(awt_display, fbc);251if (xvi == NULL) {252continue;253}254fbvisualid = xvi->visualid;255XFree(xvi);256257if (visualid == 0 || visualid == fbvisualid) {258int dtype, rtype, depth, stencil, db, alpha, gamma;259260// get GLX-specific attributes from GLXFBConfig261j2d_glXGetFBConfigAttrib(awt_display, fbc,262GLX_DRAWABLE_TYPE, &dtype);263j2d_glXGetFBConfigAttrib(awt_display, fbc,264GLX_RENDER_TYPE, &rtype);265j2d_glXGetFBConfigAttrib(awt_display, fbc,266GLX_DEPTH_SIZE, &depth);267j2d_glXGetFBConfigAttrib(awt_display, fbc,268GLX_STENCIL_SIZE, &stencil);269270// these attributes don't affect our decision, but they are271// interesting for trace logs, so we will query them anyway272j2d_glXGetFBConfigAttrib(awt_display, fbc,273GLX_DOUBLEBUFFER, &db);274j2d_glXGetFBConfigAttrib(awt_display, fbc,275GLX_ALPHA_SIZE, &alpha);276277J2dRlsTrace5(J2D_TRACE_VERBOSE,278"[V] id=0x%x db=%d alpha=%d depth=%d stencil=%d valid=",279fbvisualid, db, alpha, depth, stencil);280281if ((dtype & GLX_WINDOW_BIT) &&282(dtype & GLX_PBUFFER_BIT) &&283(rtype & GLX_RGBA_BIT) &&284(depth >= 16))285{286if (visualid == 0) {287// when visualid == 0, we loop through all configs288// looking for an fbconfig that has the smallest combined289// depth+stencil size (this keeps VRAM usage to a minimum)290if ((depth + stencil) < minDepthPlusStencil) {291J2dRlsTrace(J2D_TRACE_VERBOSE, "true\n");292minDepthPlusStencil = depth + stencil;293chosenConfig = fbc;294} else {295J2dRlsTrace(J2D_TRACE_VERBOSE,296"false (large depth)\n");297}298continue;299} else {300// in this case, visualid == fbvisualid, which means301// we've found a valid fbconfig corresponding to the302// requested VisualID, so break out of the loop303J2dRlsTrace(J2D_TRACE_VERBOSE, "true\n");304chosenConfig = fbc;305break;306}307} else {308J2dRlsTrace(J2D_TRACE_VERBOSE, "false (bad match)\n");309}310}311}312313// free the list of fbconfigs314XFree(fbconfigs);315316if (chosenConfig == 0) {317J2dRlsTraceLn(J2D_TRACE_ERROR,318"GLXGC_InitFBConfig: could not find an appropriate fbconfig");319return 0;320}321322return chosenConfig;323}324325/**326* Returns the X11 VisualID that corresponds to the best GLXFBConfig for the327* given screen. If no valid visual could be found, this method returns zero.328* Note that this method will attempt to initialize GLX (and all the329* necessary function symbols) if it has not been already. The AWT_LOCK330* must be acquired before calling this method.331*/332VisualID333GLXGC_FindBestVisual(JNIEnv *env, jint screen)334{335GLXFBConfig fbc;336XVisualInfo *xvi;337VisualID visualid;338339J2dRlsTraceLn1(J2D_TRACE_INFO, "GLXGC_FindBestVisual: scn=%d", screen);340341if (!GLXGC_IsGLXAvailable()) {342J2dRlsTraceLn(J2D_TRACE_ERROR,343"GLXGC_FindBestVisual: could not initialize GLX");344return 0;345}346347fbc = GLXGC_InitFBConfig(env, screen, 0);348if (fbc == 0) {349J2dRlsTraceLn(J2D_TRACE_ERROR,350"GLXGC_FindBestVisual: could not find best visual");351return 0;352}353354xvi = j2d_glXGetVisualFromFBConfig(awt_display, fbc);355if (xvi == NULL) {356J2dRlsTraceLn(J2D_TRACE_ERROR,357"GLXGC_FindBestVisual: could not get visual for fbconfig");358return 0;359}360361visualid = xvi->visualid;362XFree(xvi);363364J2dRlsTraceLn2(J2D_TRACE_INFO,365"GLXGC_FindBestVisual: chose 0x%x as the best visual for screen %d",366visualid, screen);367368return visualid;369}370371/**372* Creates a scratch pbuffer, which can be used to make a context current373* for extension queries, etc.374*/375static GLXPbuffer376GLXGC_InitScratchPbuffer(GLXFBConfig fbconfig)377{378int pbattrlist[] = {GLX_PBUFFER_WIDTH, 4,379GLX_PBUFFER_HEIGHT, 4,380GLX_PRESERVED_CONTENTS, GL_FALSE,3810};382383J2dTraceLn(J2D_TRACE_INFO, "GLXGC_InitScratchPbuffer");384385return j2d_glXCreatePbuffer(awt_display, fbconfig, pbattrlist);386}387388/**389* Initializes a new OGLContext, which includes the native GLXContext handle390* and some other important information such as the associated GLXFBConfig.391*/392static OGLContext *393GLXGC_InitOGLContext(GLXFBConfig fbconfig, GLXContext context,394GLXPbuffer scratch, jint caps)395{396OGLContext *oglc;397GLXCtxInfo *ctxinfo;398399J2dTraceLn(J2D_TRACE_INFO, "GLXGC_InitOGLContext");400401oglc = (OGLContext *)malloc(sizeof(OGLContext));402if (oglc == NULL) {403J2dRlsTraceLn(J2D_TRACE_ERROR,404"GLXGC_InitOGLContext: could not allocate memory for oglc");405return NULL;406}407408memset(oglc, 0, sizeof(OGLContext));409410ctxinfo = (GLXCtxInfo *)malloc(sizeof(GLXCtxInfo));411if (ctxinfo == NULL) {412J2dRlsTraceLn(J2D_TRACE_ERROR,413"GLXGC_InitOGLContext: could not allocate memory for ctxinfo");414free(oglc);415return NULL;416}417418ctxinfo->fbconfig = fbconfig;419ctxinfo->context = context;420ctxinfo->scratchSurface = scratch;421oglc->ctxInfo = ctxinfo;422oglc->caps = caps;423424return oglc;425}426427#endif /* !HEADLESS */428429/**430* Determines whether the GLX pipeline can be used for a given GraphicsConfig431* provided its screen number and visual ID. If the minimum requirements are432* met, the native GLXGraphicsConfigInfo structure is initialized for this433* GraphicsConfig with the necessary information (GLXFBConfig, etc.)434* and a pointer to this structure is returned as a jlong. If435* initialization fails at any point, zero is returned, indicating that GLX436* cannot be used for this GraphicsConfig (we should fallback on the existing437* X11 pipeline).438*/439JNIEXPORT jlong JNICALL440Java_sun_java2d_opengl_GLXGraphicsConfig_getGLXConfigInfo(JNIEnv *env,441jclass glxgc,442jint screennum,443jint visnum)444{445#ifndef HEADLESS446OGLContext *oglc;447GLXFBConfig fbconfig;448GLXContext context;449GLXPbuffer scratch;450GLXGraphicsConfigInfo *glxinfo;451jint caps = CAPS_EMPTY;452int db;453const unsigned char *versionstr;454455J2dRlsTraceLn(J2D_TRACE_INFO, "GLXGraphicsConfig_getGLXConfigInfo");456457if (usingXinerama) {458// when Xinerama is enabled, the screen ID needs to be 0459screennum = 0;460}461462fbconfig = GLXGC_InitFBConfig(env, screennum, (VisualID)visnum);463if (fbconfig == 0) {464J2dRlsTraceLn(J2D_TRACE_ERROR,465"GLXGraphicsConfig_getGLXConfigInfo: could not create fbconfig");466return 0L;467}468469if (sharedContext == 0) {470// create the one shared context471sharedContext = j2d_glXCreateNewContext(awt_display, fbconfig,472GLX_RGBA_TYPE, 0, GL_TRUE);473if (sharedContext == 0) {474J2dRlsTraceLn(J2D_TRACE_ERROR,475"GLXGraphicsConfig_getGLXConfigInfo: could not create shared context");476return 0L;477}478}479480// create the GLXContext for this GLXGraphicsConfig481context = j2d_glXCreateNewContext(awt_display, fbconfig,482GLX_RGBA_TYPE, sharedContext,483GL_TRUE);484if (context == 0) {485J2dRlsTraceLn(J2D_TRACE_ERROR,486"GLXGraphicsConfig_getGLXConfigInfo: could not create GLX context");487return 0L;488}489490// this is pretty sketchy, but it seems to be the easiest way to create491// some form of GLXDrawable using only the display and a GLXFBConfig492// (in order to make the context current for checking the version,493// extensions, etc)...494scratch = GLXGC_InitScratchPbuffer(fbconfig);495if (scratch == 0) {496J2dRlsTraceLn(J2D_TRACE_ERROR,497"GLXGraphicsConfig_getGLXConfigInfo: could not create scratch pbuffer");498j2d_glXDestroyContext(awt_display, context);499return 0L;500}501502// the context must be made current before we can query the503// version and extension strings504j2d_glXMakeContextCurrent(awt_display, scratch, scratch, context);505506versionstr = j2d_glGetString(GL_VERSION);507OGLContext_GetExtensionInfo(env, &caps);508509// destroy the temporary resources510j2d_glXMakeContextCurrent(awt_display, None, None, NULL);511512J2dRlsTraceLn1(J2D_TRACE_INFO,513"GLXGraphicsConfig_getGLXConfigInfo: OpenGL version=%s",514(versionstr == NULL) ? "null" : (char *)versionstr);515516if (!OGLContext_IsVersionSupported(versionstr)) {517J2dRlsTraceLn(J2D_TRACE_ERROR,518"GLXGraphicsConfig_getGLXConfigInfo: OpenGL 1.2 is required");519j2d_glXDestroyPbuffer(awt_display, scratch);520j2d_glXDestroyContext(awt_display, context);521return 0L;522}523524// get config-specific capabilities525j2d_glXGetFBConfigAttrib(awt_display, fbconfig, GLX_DOUBLEBUFFER, &db);526if (db) {527caps |= CAPS_DOUBLEBUFFERED;528}529530// initialize the OGLContext, which wraps the GLXFBConfig and GLXContext531oglc = GLXGC_InitOGLContext(fbconfig, context, scratch, caps);532if (oglc == NULL) {533J2dRlsTraceLn(J2D_TRACE_ERROR,534"GLXGraphicsConfig_getGLXConfigInfo: could not create oglc");535j2d_glXDestroyPbuffer(awt_display, scratch);536j2d_glXDestroyContext(awt_display, context);537return 0L;538}539540J2dTraceLn(J2D_TRACE_VERBOSE,541"GLXGraphicsConfig_getGLXConfigInfo: finished checking dependencies");542543// create the GLXGraphicsConfigInfo record for this config544glxinfo = (GLXGraphicsConfigInfo *)malloc(sizeof(GLXGraphicsConfigInfo));545if (glxinfo == NULL) {546J2dRlsTraceLn(J2D_TRACE_ERROR,547"GLXGraphicsConfig_getGLXConfigInfo: could not allocate memory for glxinfo");548GLXGC_DestroyOGLContext(oglc);549return 0L;550}551552glxinfo->screen = screennum;553glxinfo->visual = visnum;554glxinfo->context = oglc;555glxinfo->fbconfig = fbconfig;556557return ptr_to_jlong(glxinfo);558#else559return 0L;560#endif /* !HEADLESS */561}562563JNIEXPORT void JNICALL564Java_sun_java2d_opengl_GLXGraphicsConfig_initConfig(JNIEnv *env,565jobject glxgc,566jlong aData,567jlong configInfo)568{569#ifndef HEADLESS570GLXGraphicsConfigInfo *glxinfo;571AwtGraphicsConfigDataPtr configData =572(AwtGraphicsConfigDataPtr)jlong_to_ptr(aData);573574J2dTraceLn(J2D_TRACE_INFO, "GLXGraphicsConfig_initConfig");575576if (configData == NULL) {577JNU_ThrowNullPointerException(env, "Native GraphicsConfig missing");578return;579}580581glxinfo = (GLXGraphicsConfigInfo *)jlong_to_ptr(configInfo);582if (glxinfo == NULL) {583JNU_ThrowNullPointerException(env,584"GLXGraphicsConfigInfo data missing");585return;586}587588configData->glxInfo = glxinfo;589#endif /* !HEADLESS */590}591592JNIEXPORT jint JNICALL593Java_sun_java2d_opengl_GLXGraphicsConfig_getOGLCapabilities(JNIEnv *env,594jclass glxgc,595jlong configInfo)596{597#ifndef HEADLESS598GLXGraphicsConfigInfo *glxinfo =599(GLXGraphicsConfigInfo *)jlong_to_ptr(configInfo);600601J2dTraceLn(J2D_TRACE_INFO, "GLXGraphicsConfig_getOGLCapabilities");602603if (glxinfo == NULL || glxinfo->context == NULL) {604return CAPS_EMPTY;605}606607return glxinfo->context->caps;608#else609return CAPS_EMPTY;610#endif /* !HEADLESS */611}612613614