QSurfaceFormat qglx_reduceSurfaceFormat(const QSurfaceFormat &format, bool *reduced) { QSurfaceFormat retFormat = format; *reduced = true; if (retFormat.redBufferSize() > 1) { retFormat.setRedBufferSize(1); } else if (retFormat.greenBufferSize() > 1) { retFormat.setGreenBufferSize(1); } else if (retFormat.blueBufferSize() > 1) { retFormat.setBlueBufferSize(1); } else if (retFormat.samples() > 1) { retFormat.setSamples(qMin(retFormat.samples() / 2, 16)); } else if (retFormat.stereo()) { retFormat.setStereo(false); }else if (retFormat.stencilBufferSize() > 0) { retFormat.setStencilBufferSize(0); }else if (retFormat.hasAlpha()) { retFormat.setAlphaBufferSize(0); }else if (retFormat.depthBufferSize() > 0) { retFormat.setDepthBufferSize(0); }else if (retFormat.swapBehavior() != QSurfaceFormat::SingleBuffer) { retFormat.setSwapBehavior(QSurfaceFormat::SingleBuffer); }else{ *reduced = false; } return retFormat; }
GLXFBConfig qglx_findConfig(Display *display, int screen , const QSurfaceFormat &format, int drawableBit) { bool reduced = true; GLXFBConfig chosenConfig = 0; QSurfaceFormat reducedFormat = format; while (!chosenConfig && reduced) { QVector<int> spec = qglx_buildSpec(reducedFormat, drawableBit); int confcount = 0; GLXFBConfig *configs; configs = glXChooseFBConfig(display, screen,spec.constData(),&confcount); if (confcount) { for (int i = 0; i < confcount; i++) { chosenConfig = configs[i]; // Make sure we try to get an ARGB visual if the format asked for an alpha: if (reducedFormat.hasAlpha()) { int alphaSize; glXGetFBConfigAttrib(display,configs[i],GLX_ALPHA_SIZE,&alphaSize); if (alphaSize > 0) { XVisualInfo *visual = glXGetVisualFromFBConfig(display, chosenConfig); bool hasAlpha = false; #if !defined(QT_NO_XRENDER) XRenderPictFormat *pictFormat = XRenderFindVisualFormat(display, visual->visual); hasAlpha = pictFormat->direct.alphaMask > 0; #else hasAlpha = visual->depth == 32; #endif XFree(visual); if (hasAlpha) break; } } else { break; // Just choose the first in the list if there's no alpha requested } } XFree(configs); } if (!chosenConfig) reducedFormat = qglx_reduceSurfaceFormat(reducedFormat,&reduced); } return chosenConfig; }
QVector<int> qglx_buildSpec(const QSurfaceFormat &format, int drawableBit) { QVector<int> spec(48); int i = 0; spec[i++] = GLX_LEVEL; spec[i++] = 0; spec[i++] = GLX_DRAWABLE_TYPE; spec[i++] = drawableBit; spec[i++] = GLX_RENDER_TYPE; spec[i++] = GLX_RGBA_BIT; spec[i++] = GLX_RED_SIZE; spec[i++] = (format.redBufferSize() == -1) ? 1 : format.redBufferSize(); spec[i++] = GLX_GREEN_SIZE; spec[i++] = (format.greenBufferSize() == -1) ? 1 : format.greenBufferSize(); spec[i++] = GLX_BLUE_SIZE; spec[i++] = (format.blueBufferSize() == -1) ? 1 : format.blueBufferSize(); if (format.hasAlpha()) { spec[i++] = GLX_ALPHA_SIZE; spec[i++] = format.alphaBufferSize(); } spec[i++] = GLX_DOUBLEBUFFER; spec[i++] = format.swapBehavior() != QSurfaceFormat::SingleBuffer ? True : False; spec[i++] = GLX_STEREO; spec[i++] = format.stereo() ? True : False; if (format.depthBufferSize() > 0) { spec[i++] = GLX_DEPTH_SIZE; spec[i++] = format.depthBufferSize(); } if (format.stencilBufferSize() > 0) { spec[i++] = GLX_STENCIL_SIZE; spec[i++] = (format.stencilBufferSize() == -1) ? 1 : format.stencilBufferSize(); } if (format.samples() > 1) { spec[i++] = GLX_SAMPLE_BUFFERS_ARB; spec[i++] = 1; spec[i++] = GLX_SAMPLES_ARB; spec[i++] = format.samples(); } spec[i++] = XNone; return spec; }
GLXFBConfig qglx_findConfig(Display *display, int screen , const QSurfaceFormat &format, int drawableBit) { // Allow forcing LIBGL_ALWAYS_SOFTWARE for Qt 5 applications only. // This is most useful with drivers that only support OpenGL 1. // We need OpenGL 2, but the user probably doesn't want // LIBGL_ALWAYS_SOFTWARE in OpenGL 1 apps. static bool checkedForceSoftwareOpenGL = false; static bool forceSoftwareOpenGL = false; if (!checkedForceSoftwareOpenGL) { // If LIBGL_ALWAYS_SOFTWARE is already set, don't mess with it. // We want to unset LIBGL_ALWAYS_SOFTWARE at the end so it does not // get inherited by other processes, of course only if it wasn't // already set before. if (!qEnvironmentVariableIsEmpty("QT_XCB_FORCE_SOFTWARE_OPENGL") && !qEnvironmentVariableIsSet("LIBGL_ALWAYS_SOFTWARE")) forceSoftwareOpenGL = true; checkedForceSoftwareOpenGL = true; } if (forceSoftwareOpenGL) qputenv("LIBGL_ALWAYS_SOFTWARE", QByteArrayLiteral("1")); bool reduced = true; GLXFBConfig chosenConfig = 0; QSurfaceFormat reducedFormat = format; while (!chosenConfig && reduced) { QVector<int> spec = qglx_buildSpec(reducedFormat, drawableBit); int confcount = 0; GLXFBConfig *configs; configs = glXChooseFBConfig(display, screen,spec.constData(),&confcount); if (confcount) { for (int i = 0; i < confcount; i++) { chosenConfig = configs[i]; // Make sure we try to get an ARGB visual if the format asked for an alpha: if (reducedFormat.hasAlpha()) { int alphaSize; glXGetFBConfigAttrib(display,configs[i],GLX_ALPHA_SIZE,&alphaSize); if (alphaSize > 0) { XVisualInfo *visual = glXGetVisualFromFBConfig(display, chosenConfig); bool hasAlpha = false; #if !defined(QT_NO_XRENDER) XRenderPictFormat *pictFormat = XRenderFindVisualFormat(display, visual->visual); hasAlpha = pictFormat->direct.alphaMask > 0; #else hasAlpha = visual->depth == 32; #endif XFree(visual); if (hasAlpha) break; } } else { break; // Just choose the first in the list if there's no alpha requested } } XFree(configs); } if (!chosenConfig) reducedFormat = qglx_reduceSurfaceFormat(reducedFormat,&reduced); } // unset LIBGL_ALWAYS_SOFTWARE now so other processes don't inherit it if (forceSoftwareOpenGL) qunsetenv("LIBGL_ALWAYS_SOFTWARE"); return chosenConfig; }