2010-12-05 21:38:12 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2010-06-15 10:55:08 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Matt Woodrow <mwoodrow@mozilla.com>
|
|
|
|
* Bas Schouten <bschouten@mozilla.com>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GTK2
|
|
|
|
#include <gdk/gdk.h>
|
|
|
|
#include <gdk/gdkx.h>
|
|
|
|
#define GET_NATIVE_WINDOW(aWidget) GDK_WINDOW_XID((GdkWindow *) aWidget->GetNativeData(NS_NATIVE_WINDOW))
|
|
|
|
#elif defined(MOZ_WIDGET_QT)
|
|
|
|
#include <QWidget>
|
|
|
|
#include <QX11Info>
|
|
|
|
#define GET_NATIVE_WINDOW(aWidget) static_cast<QWidget*>(aWidget->GetNativeData(NS_NATIVE_SHELLWIDGET))->handle()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <X11/Xlib.h>
|
|
|
|
#include <X11/Xutil.h>
|
|
|
|
|
2010-06-24 07:52:48 +04:00
|
|
|
#include "mozilla/X11Util.h"
|
|
|
|
|
2011-01-11 00:14:14 +03:00
|
|
|
#include "prenv.h"
|
2010-06-15 10:55:08 +04:00
|
|
|
#include "GLContextProvider.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsIWidget.h"
|
|
|
|
#include "GLXLibrary.h"
|
2010-07-19 09:01:14 +04:00
|
|
|
#include "gfxXlibSurface.h"
|
2010-07-01 20:30:38 +04:00
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "gfxImageSurface.h"
|
|
|
|
#include "gfxPlatform.h"
|
2010-11-10 18:49:52 +03:00
|
|
|
#include "GLContext.h"
|
2011-04-08 03:58:08 +04:00
|
|
|
#include "gfxUtils.h"
|
2010-06-15 10:55:08 +04:00
|
|
|
|
2011-03-02 23:50:36 +03:00
|
|
|
#include "gfxCrashReporterUtils.h"
|
|
|
|
|
2010-06-15 10:55:08 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gl {
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
static PRBool gIsATI = PR_FALSE;
|
|
|
|
static PRBool gIsChromium = PR_FALSE;
|
2010-11-10 18:49:52 +03:00
|
|
|
static int gGLXMajorVersion = 0, gGLXMinorVersion = 0;
|
|
|
|
|
|
|
|
// Check that we have at least version aMajor.aMinor .
|
|
|
|
static inline bool
|
|
|
|
GLXVersionCheck(int aMajor, int aMinor)
|
|
|
|
{
|
|
|
|
return aMajor < gGLXMajorVersion ||
|
|
|
|
(aMajor == gGLXMajorVersion && aMinor <= gGLXMinorVersion);
|
|
|
|
}
|
2010-07-19 09:01:14 +04:00
|
|
|
|
2010-11-10 18:49:52 +03:00
|
|
|
static inline bool
|
|
|
|
HasExtension(const char* aExtensions, const char* aRequiredExtension)
|
|
|
|
{
|
|
|
|
return GLContext::ListHasExtension(
|
|
|
|
reinterpret_cast<const GLubyte*>(aExtensions), aRequiredExtension);
|
|
|
|
}
|
|
|
|
|
2010-06-15 10:55:08 +04:00
|
|
|
PRBool
|
|
|
|
GLXLibrary::EnsureInitialized()
|
|
|
|
{
|
|
|
|
if (mInitialized) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-11-10 18:49:52 +03:00
|
|
|
// Don't repeatedly try to initialize.
|
|
|
|
if (mTriedInitializing) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
mTriedInitializing = PR_TRUE;
|
|
|
|
|
2010-06-15 10:55:08 +04:00
|
|
|
if (!mOGLLibrary) {
|
2011-03-02 23:50:36 +03:00
|
|
|
// see e.g. bug 608526: it is intrinsically interesting to know whether we have dynamically linked to libGL.so.1
|
|
|
|
// because at least the NVIDIA implementation requires an executable stack, which causes mprotect calls,
|
|
|
|
// which trigger glibc bug http://sourceware.org/bugzilla/show_bug.cgi?id=12225
|
|
|
|
const char *libGLfilename = "libGL.so.1";
|
|
|
|
ScopedGfxFeatureReporter reporter(libGLfilename);
|
|
|
|
mOGLLibrary = PR_LoadLibrary(libGLfilename);
|
2010-06-15 10:55:08 +04:00
|
|
|
if (!mOGLLibrary) {
|
2011-03-02 23:50:36 +03:00
|
|
|
NS_WARNING("Couldn't load OpenGL shared library.");
|
|
|
|
return PR_FALSE;
|
2010-06-15 10:55:08 +04:00
|
|
|
}
|
2011-03-02 23:50:36 +03:00
|
|
|
reporter.SetSuccessful();
|
2010-06-15 10:55:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
LibrarySymbolLoader::SymLoadStruct symbols[] = {
|
2010-11-10 18:49:52 +03:00
|
|
|
/* functions that were in GLX 1.0 */
|
2010-11-10 18:49:52 +03:00
|
|
|
{ (PRFuncPtr*) &xDestroyContext, { "glXDestroyContext", NULL } },
|
2010-06-15 10:55:08 +04:00
|
|
|
{ (PRFuncPtr*) &xMakeCurrent, { "glXMakeCurrent", NULL } },
|
2010-06-19 03:17:00 +04:00
|
|
|
{ (PRFuncPtr*) &xSwapBuffers, { "glXSwapBuffers", NULL } },
|
2010-11-10 18:49:52 +03:00
|
|
|
{ (PRFuncPtr*) &xQueryVersion, { "glXQueryVersion", NULL } },
|
2010-11-10 18:49:52 +03:00
|
|
|
{ (PRFuncPtr*) &xGetCurrentContext, { "glXGetCurrentContext", NULL } },
|
2011-04-08 03:58:07 +04:00
|
|
|
{ (PRFuncPtr*) &xWaitGL, { "glXWaitGL", NULL } },
|
2010-11-10 18:49:52 +03:00
|
|
|
/* functions introduced in GLX 1.1 */
|
2010-11-10 18:49:52 +03:00
|
|
|
{ (PRFuncPtr*) &xQueryExtensionsString, { "glXQueryExtensionsString", NULL } },
|
2011-03-25 06:38:59 +03:00
|
|
|
{ (PRFuncPtr*) &xGetClientString, { "glXGetClientString", NULL } },
|
2010-06-19 03:17:00 +04:00
|
|
|
{ (PRFuncPtr*) &xQueryServerString, { "glXQueryServerString", NULL } },
|
2010-11-10 18:49:52 +03:00
|
|
|
{ NULL, { NULL } }
|
|
|
|
};
|
|
|
|
|
|
|
|
LibrarySymbolLoader::SymLoadStruct symbols13[] = {
|
|
|
|
/* functions introduced in GLX 1.3 */
|
|
|
|
{ (PRFuncPtr*) &xChooseFBConfig, { "glXChooseFBConfig", NULL } },
|
|
|
|
{ (PRFuncPtr*) &xGetFBConfigAttrib, { "glXGetFBConfigAttrib", NULL } },
|
|
|
|
// WARNING: xGetFBConfigs not set in symbols13_ext
|
|
|
|
{ (PRFuncPtr*) &xGetFBConfigs, { "glXGetFBConfigs", NULL } },
|
|
|
|
{ (PRFuncPtr*) &xGetVisualFromFBConfig, { "glXGetVisualFromFBConfig", NULL } },
|
|
|
|
// WARNING: symbols13_ext sets xCreateGLXPixmapWithConfig instead
|
2010-07-19 09:01:14 +04:00
|
|
|
{ (PRFuncPtr*) &xCreatePixmap, { "glXCreatePixmap", NULL } },
|
|
|
|
{ (PRFuncPtr*) &xDestroyPixmap, { "glXDestroyPixmap", NULL } },
|
2010-11-10 18:49:52 +03:00
|
|
|
{ (PRFuncPtr*) &xCreateNewContext, { "glXCreateNewContext", NULL } },
|
|
|
|
{ NULL, { NULL } }
|
|
|
|
};
|
|
|
|
|
|
|
|
LibrarySymbolLoader::SymLoadStruct symbols13_ext[] = {
|
|
|
|
/* extension equivalents for functions introduced in GLX 1.3 */
|
|
|
|
// GLX_SGIX_fbconfig extension
|
|
|
|
{ (PRFuncPtr*) &xChooseFBConfig, { "glXChooseFBConfigSGIX", NULL } },
|
|
|
|
{ (PRFuncPtr*) &xGetFBConfigAttrib, { "glXGetFBConfigAttribSGIX", NULL } },
|
|
|
|
// WARNING: no xGetFBConfigs equivalent in extensions
|
|
|
|
{ (PRFuncPtr*) &xGetVisualFromFBConfig, { "glXGetVisualFromFBConfig", NULL } },
|
|
|
|
// WARNING: different from symbols13:
|
|
|
|
{ (PRFuncPtr*) &xCreateGLXPixmapWithConfig, { "glXCreateGLXPixmapWithConfigSGIX", NULL } },
|
|
|
|
{ (PRFuncPtr*) &xDestroyPixmap, { "glXDestroyGLXPixmap", NULL } }, // not from ext
|
|
|
|
{ (PRFuncPtr*) &xCreateNewContext, { "glXCreateContextWithConfigSGIX", NULL } },
|
|
|
|
{ NULL, { NULL } }
|
|
|
|
};
|
|
|
|
|
|
|
|
LibrarySymbolLoader::SymLoadStruct symbols14[] = {
|
|
|
|
/* functions introduced in GLX 1.4 */
|
|
|
|
{ (PRFuncPtr*) &xGetProcAddress, { "glXGetProcAddress", NULL } },
|
|
|
|
{ NULL, { NULL } }
|
|
|
|
};
|
|
|
|
|
|
|
|
LibrarySymbolLoader::SymLoadStruct symbols14_ext[] = {
|
|
|
|
/* extension equivalents for functions introduced in GLX 1.4 */
|
|
|
|
// GLX_ARB_get_proc_address extension
|
|
|
|
{ (PRFuncPtr*) &xGetProcAddress, { "glXGetProcAddressARB", NULL } },
|
2010-06-15 10:55:08 +04:00
|
|
|
{ NULL, { NULL } }
|
|
|
|
};
|
|
|
|
|
2011-04-08 03:58:07 +04:00
|
|
|
LibrarySymbolLoader::SymLoadStruct symbols_texturefrompixmap[] = {
|
|
|
|
{ (PRFuncPtr*) &xBindTexImage, { "glXBindTexImageEXT", NULL } },
|
|
|
|
{ (PRFuncPtr*) &xReleaseTexImage, { "glXReleaseTexImageEXT", NULL } },
|
|
|
|
{ NULL, { NULL } }
|
|
|
|
};
|
|
|
|
|
2010-06-15 10:55:08 +04:00
|
|
|
if (!LibrarySymbolLoader::LoadSymbols(mOGLLibrary, &symbols[0])) {
|
|
|
|
NS_WARNING("Couldn't find required entry point in OpenGL shared library");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-11-10 18:49:52 +03:00
|
|
|
Display *display = DefaultXDisplay();
|
2011-03-25 06:38:59 +03:00
|
|
|
PRBool ignoreBlacklist = PR_GetEnv("MOZ_GLX_IGNORE_BLACKLIST") != nsnull;
|
|
|
|
if (!ignoreBlacklist) {
|
|
|
|
// ATI's libGL (at least the one provided with 11.2 drivers) segfaults
|
|
|
|
// when querying server info if the server does not have the
|
|
|
|
// ATIFGLEXTENSION extension.
|
|
|
|
const char *clientVendor = xGetClientString(display, GLX_VENDOR);
|
|
|
|
if (clientVendor && strcmp(clientVendor, "ATI") == 0) {
|
|
|
|
printf("[GLX] The ATI proprietary libGL.so.1 is currently "
|
|
|
|
"blacklisted to avoid crashes that happen in some "
|
|
|
|
"situations. If you would like to bypass this, set the "
|
|
|
|
"MOZ_GLX_IGNORE_BLACKLIST environment variable.\n");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-10 18:49:52 +03:00
|
|
|
int screen = DefaultScreen(display);
|
2011-04-08 08:55:10 +04:00
|
|
|
const char *serverVendor = NULL;
|
|
|
|
const char *serverVersionStr = NULL;
|
|
|
|
const char *extensionsStr = NULL;
|
2011-02-12 02:11:30 +03:00
|
|
|
|
2011-03-25 06:38:59 +03:00
|
|
|
// This scope is covered by a ScopedXErrorHandler to catch X errors in GLX
|
|
|
|
// calls. See bug 632867 comment 3: Mesa versions up to 7.10 cause a
|
|
|
|
// BadLength error during the first GLX call that communicates with the
|
|
|
|
// server when the server GLX version < 1.3.
|
2011-02-12 02:11:30 +03:00
|
|
|
{
|
|
|
|
ScopedXErrorHandler xErrorHandler;
|
2011-02-12 02:11:30 +03:00
|
|
|
|
2011-02-12 02:11:30 +03:00
|
|
|
if (!xQueryVersion(display, &gGLXMajorVersion, &gGLXMinorVersion)) {
|
|
|
|
gGLXMajorVersion = 0;
|
|
|
|
gGLXMinorVersion = 0;
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2011-03-25 06:38:59 +03:00
|
|
|
serverVendor = xQueryServerString(display, screen, GLX_VENDOR);
|
2011-02-12 02:11:30 +03:00
|
|
|
serverVersionStr = xQueryServerString(display, screen, GLX_VERSION);
|
|
|
|
|
|
|
|
if (!GLXVersionCheck(1, 1))
|
|
|
|
// Not possible to query for extensions.
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
extensionsStr = xQueryExtensionsString(display, screen);
|
2010-07-19 09:01:14 +04:00
|
|
|
|
2011-02-12 02:11:30 +03:00
|
|
|
if (xErrorHandler.GetError())
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2010-11-10 18:49:52 +03:00
|
|
|
|
|
|
|
LibrarySymbolLoader::SymLoadStruct *sym13;
|
2010-11-10 18:49:52 +03:00
|
|
|
if (!GLXVersionCheck(1, 3)) {
|
2010-11-10 18:49:52 +03:00
|
|
|
// Even if we don't have 1.3, we might have equivalent extensions
|
|
|
|
// (as on the Intel X server).
|
|
|
|
if (!HasExtension(extensionsStr, "GLX_SGIX_fbconfig")) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
sym13 = symbols13_ext;
|
|
|
|
} else {
|
|
|
|
sym13 = symbols13;
|
|
|
|
}
|
|
|
|
if (!LibrarySymbolLoader::LoadSymbols(mOGLLibrary, sym13)) {
|
|
|
|
NS_WARNING("Couldn't find required entry point in OpenGL shared library");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
LibrarySymbolLoader::SymLoadStruct *sym14;
|
2010-11-10 18:49:52 +03:00
|
|
|
if (!GLXVersionCheck(1, 4)) {
|
2010-11-10 18:49:52 +03:00
|
|
|
// Even if we don't have 1.4, we might have equivalent extensions
|
|
|
|
// (as on the Intel X server).
|
|
|
|
if (!HasExtension(extensionsStr, "GLX_ARB_get_proc_address")) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
sym14 = symbols14_ext;
|
|
|
|
} else {
|
|
|
|
sym14 = symbols14;
|
|
|
|
}
|
|
|
|
if (!LibrarySymbolLoader::LoadSymbols(mOGLLibrary, sym14)) {
|
|
|
|
NS_WARNING("Couldn't find required entry point in OpenGL shared library");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2011-04-08 03:58:07 +04:00
|
|
|
if (HasExtension(extensionsStr, "GLX_EXT_texture_from_pixmap") &&
|
|
|
|
LibrarySymbolLoader::LoadSymbols(mOGLLibrary, symbols_texturefrompixmap))
|
|
|
|
{
|
|
|
|
mHasTextureFromPixmap = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2011-03-25 06:38:59 +03:00
|
|
|
gIsATI = serverVendor && DoesVendorStringMatch(serverVendor, "ATI");
|
|
|
|
gIsChromium = (serverVendor &&
|
|
|
|
DoesVendorStringMatch(serverVendor, "Chromium")) ||
|
|
|
|
(serverVersionStr &&
|
|
|
|
DoesVendorStringMatch(serverVersionStr, "Chromium"));
|
2010-07-19 09:01:14 +04:00
|
|
|
|
2010-06-15 10:55:08 +04:00
|
|
|
mInitialized = PR_TRUE;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2011-04-08 03:58:07 +04:00
|
|
|
GLXPixmap
|
|
|
|
GLXLibrary::CreatePixmap(gfxASurface* aSurface)
|
|
|
|
{
|
|
|
|
if (aSurface->GetType() != gfxASurface::SurfaceTypeXlib || !mHasTextureFromPixmap) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!EnsureInitialized()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int attribs[] = { GLX_DOUBLEBUFFER, False,
|
|
|
|
GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
|
|
|
|
GLX_BIND_TO_TEXTURE_RGBA_EXT, True,
|
|
|
|
None };
|
|
|
|
|
|
|
|
int numFormats;
|
|
|
|
Display *display = DefaultXDisplay();
|
|
|
|
int xscreen = DefaultScreen(display);
|
|
|
|
|
|
|
|
ScopedXFree<GLXFBConfig> cfg(xChooseFBConfig(display,
|
|
|
|
xscreen,
|
|
|
|
attribs,
|
|
|
|
&numFormats));
|
|
|
|
if (!cfg) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
NS_ABORT_IF_FALSE(numFormats > 0,
|
|
|
|
"glXChooseFBConfig() failed to match our requested format and violated its spec (!)");
|
|
|
|
|
|
|
|
gfxXlibSurface *xs = static_cast<gfxXlibSurface*>(aSurface);
|
|
|
|
|
|
|
|
int pixmapAttribs[] = { GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
|
|
|
|
GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGBA_EXT,
|
|
|
|
None};
|
|
|
|
|
|
|
|
GLXPixmap glxpixmap = xCreatePixmap(display,
|
|
|
|
cfg[0],
|
|
|
|
xs->XDrawable(),
|
|
|
|
pixmapAttribs);
|
|
|
|
|
|
|
|
return glxpixmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GLXLibrary::DestroyPixmap(GLXPixmap aPixmap)
|
|
|
|
{
|
|
|
|
if (!mHasTextureFromPixmap) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Display *display = DefaultXDisplay();
|
|
|
|
xDestroyPixmap(display, aPixmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GLXLibrary::BindTexImage(GLXPixmap aPixmap)
|
|
|
|
{
|
|
|
|
if (!mHasTextureFromPixmap) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Display *display = DefaultXDisplay();
|
|
|
|
// Make sure all X drawing to the surface has finished before binding to a texture.
|
|
|
|
XSync(DefaultXDisplay(), False);
|
|
|
|
xBindTexImage(display, aPixmap, GLX_FRONT_LEFT_EXT, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GLXLibrary::ReleaseTexImage(GLXPixmap aPixmap)
|
|
|
|
{
|
|
|
|
if (!mHasTextureFromPixmap) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Display *display = DefaultXDisplay();
|
|
|
|
xReleaseTexImage(display, aPixmap, GLX_FRONT_LEFT_EXT);
|
|
|
|
}
|
|
|
|
|
2010-06-15 10:55:08 +04:00
|
|
|
GLXLibrary sGLXLibrary;
|
|
|
|
|
|
|
|
class GLContextGLX : public GLContext
|
|
|
|
{
|
|
|
|
public:
|
2010-06-24 07:52:48 +04:00
|
|
|
static already_AddRefed<GLContextGLX>
|
2010-07-19 09:01:14 +04:00
|
|
|
CreateGLContext(const ContextFormat& format,
|
|
|
|
Display *display,
|
|
|
|
GLXDrawable drawable,
|
|
|
|
GLXFBConfig cfg,
|
|
|
|
XVisualInfo *vinfo,
|
|
|
|
GLContextGLX *shareContext,
|
|
|
|
PRBool deleteDrawable,
|
|
|
|
gfxXlibSurface *pixmap = nsnull)
|
2010-06-15 10:55:08 +04:00
|
|
|
{
|
2010-06-19 03:17:00 +04:00
|
|
|
int db = 0, err;
|
|
|
|
err = sGLXLibrary.xGetFBConfigAttrib(display, cfg,
|
|
|
|
GLX_DOUBLEBUFFER, &db);
|
|
|
|
if (GLX_BAD_ATTRIBUTE != err) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("[GLX] FBConfig is %sdouble-buffered\n", db ? "" : "not ");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
GLXContext context;
|
2010-12-06 14:34:34 +03:00
|
|
|
nsRefPtr<GLContextGLX> glContext;
|
2011-02-12 02:11:30 +03:00
|
|
|
bool error;
|
2010-12-06 14:34:34 +03:00
|
|
|
|
|
|
|
ScopedXErrorHandler xErrorHandler;
|
2010-07-19 09:01:14 +04:00
|
|
|
|
|
|
|
TRY_AGAIN_NO_SHARING:
|
|
|
|
|
2011-02-12 02:11:30 +03:00
|
|
|
error = false;
|
|
|
|
|
2010-11-10 18:49:52 +03:00
|
|
|
context = sGLXLibrary.xCreateNewContext(display,
|
|
|
|
cfg,
|
|
|
|
GLX_RGBA_TYPE,
|
|
|
|
shareContext ? shareContext->mContext : NULL,
|
|
|
|
True);
|
2010-06-15 10:55:08 +04:00
|
|
|
|
2010-12-06 14:34:34 +03:00
|
|
|
if (context) {
|
|
|
|
glContext = new GLContextGLX(format,
|
|
|
|
shareContext,
|
|
|
|
display,
|
|
|
|
drawable,
|
|
|
|
context,
|
|
|
|
deleteDrawable,
|
|
|
|
db,
|
|
|
|
pixmap);
|
|
|
|
if (!glContext->Init())
|
|
|
|
error = true;
|
|
|
|
} else {
|
|
|
|
error = true;
|
|
|
|
}
|
2010-06-15 10:55:08 +04:00
|
|
|
|
2011-02-12 02:11:30 +03:00
|
|
|
error |= xErrorHandler.SyncAndGetError(display);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
if (shareContext) {
|
2010-07-19 09:01:14 +04:00
|
|
|
shareContext = nsnull;
|
|
|
|
goto TRY_AGAIN_NO_SHARING;
|
|
|
|
}
|
2010-06-15 10:55:08 +04:00
|
|
|
|
2011-02-12 02:11:30 +03:00
|
|
|
NS_WARNING("Failed to create GLXContext!");
|
|
|
|
glContext = nsnull; // note: this must be done while the graceful X error handler is set,
|
|
|
|
// because glxMakeCurrent can give a GLXBadDrawable error
|
2010-06-15 10:55:08 +04:00
|
|
|
}
|
|
|
|
|
2010-06-24 07:52:48 +04:00
|
|
|
return glContext.forget();
|
2010-06-15 10:55:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
~GLContextGLX()
|
|
|
|
{
|
2010-09-21 23:41:24 +04:00
|
|
|
MarkDestroyed();
|
2010-08-07 09:09:18 +04:00
|
|
|
|
2010-11-10 18:49:52 +03:00
|
|
|
sGLXLibrary.xDestroyContext(mDisplay, mContext);
|
2010-07-19 09:01:14 +04:00
|
|
|
|
|
|
|
if (mDeleteDrawable) {
|
|
|
|
sGLXLibrary.xDestroyPixmap(mDisplay, mDrawable);
|
2010-06-15 10:55:08 +04:00
|
|
|
}
|
2010-07-19 09:01:14 +04:00
|
|
|
}
|
2010-06-15 10:55:08 +04:00
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
GLContextType GetContextType() {
|
|
|
|
return ContextTypeGLX;
|
2010-06-15 10:55:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool Init()
|
|
|
|
{
|
|
|
|
MakeCurrent();
|
|
|
|
SetupLookupFunction();
|
|
|
|
if (!InitWithPrefix("gl", PR_TRUE)) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return IsExtensionSupported("GL_EXT_framebuffer_object");
|
|
|
|
}
|
|
|
|
|
2010-10-13 01:01:59 +04:00
|
|
|
PRBool MakeCurrentImpl(PRBool aForce = PR_FALSE)
|
2010-06-15 10:55:08 +04:00
|
|
|
{
|
2010-09-20 21:19:30 +04:00
|
|
|
PRBool succeeded = PR_TRUE;
|
|
|
|
|
|
|
|
// With the ATI FGLRX driver, glxMakeCurrent is very slow even when the context doesn't change.
|
|
|
|
// (This is not the case with other drivers such as NVIDIA).
|
|
|
|
// So avoid calling it more than necessary. Since GLX documentation says that:
|
|
|
|
// "glXGetCurrentContext returns client-side information.
|
|
|
|
// It does not make a round trip to the server."
|
|
|
|
// I assume that it's not worth using our own TLS slot here.
|
|
|
|
if (aForce || sGLXLibrary.xGetCurrentContext() != mContext) {
|
|
|
|
succeeded = sGLXLibrary.xMakeCurrent(mDisplay, mDrawable, mContext);
|
|
|
|
NS_ASSERTION(succeeded, "Failed to make GL context current!");
|
|
|
|
}
|
|
|
|
|
2010-06-15 10:55:08 +04:00
|
|
|
return succeeded;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool SetupLookupFunction()
|
|
|
|
{
|
|
|
|
mLookupFunc = (PlatformLookupFunction)sGLXLibrary.xGetProcAddress;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *GetNativeData(NativeDataType aType)
|
|
|
|
{
|
|
|
|
switch(aType) {
|
|
|
|
case NativeGLContext:
|
|
|
|
return mContext;
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
case NativeThebesSurface:
|
|
|
|
return mPixmap;
|
2010-06-15 10:55:08 +04:00
|
|
|
|
|
|
|
default:
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
PRBool IsDoubleBuffered()
|
|
|
|
{
|
|
|
|
return mDoubleBuffered;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool SwapBuffers()
|
2010-06-19 03:17:00 +04:00
|
|
|
{
|
2010-07-19 09:01:14 +04:00
|
|
|
if (!mDoubleBuffered)
|
2010-06-19 03:17:00 +04:00
|
|
|
return PR_FALSE;
|
2010-07-19 09:01:14 +04:00
|
|
|
sGLXLibrary.xSwapBuffers(mDisplay, mDrawable);
|
2011-04-08 03:58:07 +04:00
|
|
|
sGLXLibrary.xWaitGL();
|
2010-06-19 03:17:00 +04:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2011-04-08 03:58:08 +04:00
|
|
|
PRBool TextureImageSupportsGetBackingSurface()
|
|
|
|
{
|
|
|
|
return sGLXLibrary.HasTextureFromPixmap();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual already_AddRefed<TextureImage>
|
|
|
|
CreateTextureImage(const nsIntSize& aSize,
|
|
|
|
TextureImage::ContentType aContentType,
|
|
|
|
GLenum aWrapMode,
|
|
|
|
PRBool aUseNearestFilter = PR_FALSE);
|
|
|
|
|
2010-06-15 10:55:08 +04:00
|
|
|
private:
|
2010-07-19 09:01:14 +04:00
|
|
|
friend class GLContextProviderGLX;
|
|
|
|
|
|
|
|
GLContextGLX(const ContextFormat& aFormat,
|
|
|
|
GLContext *aShareContext,
|
|
|
|
Display *aDisplay,
|
|
|
|
GLXDrawable aDrawable,
|
|
|
|
GLXContext aContext,
|
|
|
|
PRBool aDeleteDrawable,
|
|
|
|
PRBool aDoubleBuffered,
|
|
|
|
gfxXlibSurface *aPixmap)
|
|
|
|
: GLContext(aFormat, aDeleteDrawable ? PR_TRUE : PR_FALSE, aShareContext),
|
|
|
|
mContext(aContext),
|
|
|
|
mDisplay(aDisplay),
|
|
|
|
mDrawable(aDrawable),
|
|
|
|
mDeleteDrawable(aDeleteDrawable),
|
|
|
|
mDoubleBuffered(aDoubleBuffered),
|
|
|
|
mPixmap(aPixmap)
|
|
|
|
{ }
|
2010-06-15 10:55:08 +04:00
|
|
|
|
|
|
|
GLXContext mContext;
|
|
|
|
Display *mDisplay;
|
2010-07-19 09:01:14 +04:00
|
|
|
GLXDrawable mDrawable;
|
|
|
|
PRPackedBool mDeleteDrawable;
|
|
|
|
PRPackedBool mDoubleBuffered;
|
|
|
|
|
|
|
|
nsRefPtr<gfxXlibSurface> mPixmap;
|
2010-06-15 10:55:08 +04:00
|
|
|
};
|
|
|
|
|
2011-04-08 03:58:08 +04:00
|
|
|
class TextureImageGLX : public TextureImage
|
|
|
|
{
|
|
|
|
friend already_AddRefed<TextureImage>
|
|
|
|
GLContextGLX::CreateTextureImage(const nsIntSize&,
|
|
|
|
ContentType,
|
|
|
|
GLenum,
|
|
|
|
PRBool);
|
|
|
|
|
|
|
|
public:
|
|
|
|
virtual ~TextureImageGLX()
|
|
|
|
{
|
|
|
|
mGLContext->MakeCurrent();
|
|
|
|
mGLContext->fDeleteTextures(1, &mTexture);
|
|
|
|
sGLXLibrary.DestroyPixmap(mPixmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual gfxASurface* BeginUpdate(nsIntRegion& aRegion)
|
|
|
|
{
|
|
|
|
mInUpdate = PR_TRUE;
|
|
|
|
return mUpdateSurface;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void EndUpdate()
|
|
|
|
{
|
|
|
|
mInUpdate = PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool DirectUpdate(gfxASurface* aSurface, const nsIntRegion& aRegion)
|
|
|
|
{
|
|
|
|
nsRefPtr<gfxContext> ctx = new gfxContext(mUpdateSurface);
|
|
|
|
gfxUtils::ClipToRegion(ctx, aRegion);
|
|
|
|
ctx->SetSource(aSurface);
|
|
|
|
ctx->SetOperator(gfxContext::OPERATOR_SOURCE);
|
|
|
|
ctx->Paint();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void BindTexture(GLenum aTextureUnit)
|
|
|
|
{
|
|
|
|
mGLContext->fActiveTexture(aTextureUnit);
|
|
|
|
mGLContext->fBindTexture(LOCAL_GL_TEXTURE_2D, Texture());
|
|
|
|
sGLXLibrary.BindTexImage(mPixmap);
|
2011-04-12 11:36:13 +04:00
|
|
|
mGLContext->fActiveTexture(LOCAL_GL_TEXTURE0);
|
2011-04-08 03:58:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void ReleaseTexture()
|
|
|
|
{
|
|
|
|
sGLXLibrary.ReleaseTexImage(mPixmap);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual already_AddRefed<gfxASurface> GetBackingSurface()
|
|
|
|
{
|
|
|
|
NS_ADDREF(mUpdateSurface);
|
|
|
|
return mUpdateSurface.get();
|
|
|
|
}
|
|
|
|
|
2011-04-19 02:50:54 +04:00
|
|
|
virtual PRBool InUpdate() const { return mInUpdate; }
|
2011-04-08 03:58:08 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
TextureImageGLX(GLuint aTexture,
|
|
|
|
const nsIntSize& aSize,
|
|
|
|
GLenum aWrapMode,
|
|
|
|
ContentType aContentType,
|
|
|
|
GLContext* aContext,
|
|
|
|
gfxASurface* aSurface,
|
|
|
|
GLXPixmap aPixmap)
|
|
|
|
: TextureImage(aTexture, aSize, aWrapMode, aContentType)
|
|
|
|
, mGLContext(aContext)
|
|
|
|
, mUpdateSurface(aSurface)
|
|
|
|
, mPixmap(aPixmap)
|
|
|
|
, mInUpdate(PR_FALSE)
|
|
|
|
{
|
|
|
|
if (aSurface->GetContentType() == gfxASurface::CONTENT_COLOR_ALPHA) {
|
|
|
|
mShaderType = gl::RGBALayerProgramType;
|
|
|
|
} else {
|
|
|
|
mShaderType = gl::RGBXLayerProgramType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GLContext* mGLContext;
|
|
|
|
nsRefPtr<gfxASurface> mUpdateSurface;
|
|
|
|
GLXPixmap mPixmap;
|
|
|
|
PRPackedBool mInUpdate;
|
|
|
|
};
|
|
|
|
|
|
|
|
already_AddRefed<TextureImage>
|
|
|
|
GLContextGLX::CreateTextureImage(const nsIntSize& aSize,
|
|
|
|
TextureImage::ContentType aContentType,
|
|
|
|
GLenum aWrapMode,
|
|
|
|
PRBool aUseNearestFilter)
|
|
|
|
{
|
|
|
|
if (!TextureImageSupportsGetBackingSurface()) {
|
|
|
|
return GLContext::CreateTextureImage(aSize,
|
|
|
|
aContentType,
|
|
|
|
aWrapMode,
|
|
|
|
aUseNearestFilter);
|
|
|
|
}
|
|
|
|
|
|
|
|
Display *display = DefaultXDisplay();
|
|
|
|
int xscreen = DefaultScreen(display);
|
|
|
|
gfxASurface::gfxImageFormat imageFormat = gfxASurface::FormatFromContent(aContentType);
|
|
|
|
|
|
|
|
XRenderPictFormat* xrenderFormat =
|
|
|
|
gfxXlibSurface::FindRenderFormat(display, imageFormat);
|
|
|
|
NS_ASSERTION(xrenderFormat, "Could not find a render format for our display!");
|
|
|
|
|
|
|
|
|
|
|
|
nsRefPtr<gfxXlibSurface> surface =
|
|
|
|
gfxXlibSurface::Create(ScreenOfDisplay(display, xscreen),
|
|
|
|
xrenderFormat,
|
|
|
|
gfxIntSize(aSize.width, aSize.height));
|
|
|
|
NS_ASSERTION(surface, "Failed to create xlib surface!");
|
|
|
|
|
|
|
|
if (aContentType == gfxASurface::CONTENT_COLOR_ALPHA) {
|
|
|
|
nsRefPtr<gfxContext> ctx = new gfxContext(surface);
|
|
|
|
ctx->SetOperator(gfxContext::OPERATOR_CLEAR);
|
|
|
|
ctx->Paint();
|
|
|
|
}
|
|
|
|
|
|
|
|
MakeCurrent();
|
|
|
|
GLXPixmap pixmap = sGLXLibrary.CreatePixmap(surface);
|
|
|
|
NS_ASSERTION(pixmap, "Failed to create pixmap!");
|
|
|
|
|
|
|
|
GLuint texture;
|
|
|
|
fGenTextures(1, &texture);
|
|
|
|
|
|
|
|
fActiveTexture(LOCAL_GL_TEXTURE0);
|
|
|
|
fBindTexture(LOCAL_GL_TEXTURE_2D, texture);
|
|
|
|
|
|
|
|
nsRefPtr<TextureImageGLX> teximage =
|
|
|
|
new TextureImageGLX(texture, aSize, aWrapMode, aContentType, this, surface, pixmap);
|
|
|
|
|
|
|
|
GLint texfilter = aUseNearestFilter ? LOCAL_GL_NEAREST : LOCAL_GL_LINEAR;
|
|
|
|
fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_MIN_FILTER, texfilter);
|
|
|
|
fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_MAG_FILTER, texfilter);
|
|
|
|
fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_WRAP_S, aWrapMode);
|
|
|
|
fTexParameteri(LOCAL_GL_TEXTURE_2D, LOCAL_GL_TEXTURE_WRAP_T, aWrapMode);
|
|
|
|
|
|
|
|
return teximage.forget();
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
static GLContextGLX *
|
|
|
|
GetGlobalContextGLX()
|
|
|
|
{
|
|
|
|
return static_cast<GLContextGLX*>(GLContextProviderGLX::GetGlobalContext());
|
|
|
|
}
|
|
|
|
|
|
|
|
static PRBool
|
|
|
|
AreCompatibleVisuals(XVisualInfo *one, XVisualInfo *two)
|
2010-06-19 03:17:00 +04:00
|
|
|
{
|
|
|
|
if (one->c_class != two->c_class) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (one->depth != two->depth) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (one->red_mask != two->red_mask ||
|
|
|
|
one->green_mask != two->green_mask ||
|
|
|
|
one->blue_mask != two->blue_mask) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (one->bits_per_rgb != two->bits_per_rgb) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-06-15 10:55:08 +04:00
|
|
|
already_AddRefed<GLContext>
|
2010-07-19 09:01:14 +04:00
|
|
|
GLContextProviderGLX::CreateForWindow(nsIWidget *aWidget)
|
2010-06-15 10:55:08 +04:00
|
|
|
{
|
2010-06-19 03:17:00 +04:00
|
|
|
if (!sGLXLibrary.EnsureInitialized()) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Currently, we take whatever Visual the window already has, and
|
|
|
|
// try to create an fbconfig for that visual. This isn't
|
|
|
|
// necessarily what we want in the long run; an fbconfig may not
|
|
|
|
// be available for the existing visual, or if it is, the GL
|
|
|
|
// performance might be suboptimal. But using the existing visual
|
|
|
|
// is a relatively safe intermediate step.
|
|
|
|
|
|
|
|
Display *display = (Display*)aWidget->GetNativeData(NS_NATIVE_DISPLAY);
|
|
|
|
int xscreen = DefaultScreen(display);
|
|
|
|
Window window = GET_NATIVE_WINDOW(aWidget);
|
|
|
|
|
|
|
|
int numConfigs;
|
2010-06-24 07:52:48 +04:00
|
|
|
ScopedXFree<GLXFBConfig> cfgs;
|
2010-11-10 18:49:52 +03:00
|
|
|
if (gIsATI || !GLXVersionCheck(1, 3)) {
|
2010-06-19 03:17:00 +04:00
|
|
|
const int attribs[] = {
|
|
|
|
GLX_DOUBLEBUFFER, False,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
cfgs = sGLXLibrary.xChooseFBConfig(display,
|
|
|
|
xscreen,
|
|
|
|
attribs,
|
|
|
|
&numConfigs);
|
|
|
|
} else {
|
|
|
|
cfgs = sGLXLibrary.xGetFBConfigs(display,
|
|
|
|
xscreen,
|
|
|
|
&numConfigs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cfgs) {
|
|
|
|
NS_WARNING("[GLX] glXGetFBConfigs() failed");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
NS_ASSERTION(numConfigs > 0, "No FBConfigs found!");
|
|
|
|
|
|
|
|
// XXX the visual ID is almost certainly the GLX_FBCONFIG_ID, so
|
|
|
|
// we could probably do this first and replace the glXGetFBConfigs
|
|
|
|
// with glXChooseConfigs. Docs are sparklingly clear as always.
|
|
|
|
XWindowAttributes widgetAttrs;
|
|
|
|
if (!XGetWindowAttributes(display, window, &widgetAttrs)) {
|
|
|
|
NS_WARNING("[GLX] XGetWindowAttributes() failed");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
const VisualID widgetVisualID = XVisualIDFromVisual(widgetAttrs.visual);
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("[GLX] widget has VisualID 0x%lx\n", widgetVisualID);
|
|
|
|
#endif
|
|
|
|
|
2010-06-24 07:52:48 +04:00
|
|
|
ScopedXFree<XVisualInfo> vi;
|
2010-07-19 09:01:14 +04:00
|
|
|
if (gIsATI) {
|
2010-06-19 03:17:00 +04:00
|
|
|
XVisualInfo vinfo_template;
|
|
|
|
int nvisuals;
|
|
|
|
vinfo_template.visual = widgetAttrs.visual;
|
|
|
|
vinfo_template.visualid = XVisualIDFromVisual(vinfo_template.visual);
|
|
|
|
vinfo_template.depth = widgetAttrs.depth;
|
|
|
|
vinfo_template.screen = xscreen;
|
|
|
|
vi = XGetVisualInfo(display, VisualIDMask|VisualDepthMask|VisualScreenMask,
|
|
|
|
&vinfo_template, &nvisuals);
|
|
|
|
NS_ASSERTION(vi && nvisuals == 1, "Could not locate unique matching XVisualInfo for Visual");
|
|
|
|
}
|
|
|
|
|
|
|
|
int matchIndex = -1;
|
2010-07-19 09:01:14 +04:00
|
|
|
ScopedXFree<XVisualInfo> vinfo;
|
|
|
|
|
2010-06-19 03:17:00 +04:00
|
|
|
for (int i = 0; i < numConfigs; i++) {
|
2010-07-19 09:01:14 +04:00
|
|
|
vinfo = sGLXLibrary.xGetVisualFromFBConfig(display, cfgs[i]);
|
|
|
|
if (!vinfo) {
|
2010-06-23 04:24:52 +04:00
|
|
|
continue;
|
|
|
|
}
|
2010-07-19 09:01:14 +04:00
|
|
|
if (gIsATI) {
|
|
|
|
if (AreCompatibleVisuals(vi, vinfo)) {
|
2010-06-19 03:17:00 +04:00
|
|
|
matchIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
2010-07-19 09:01:14 +04:00
|
|
|
if (widgetVisualID == vinfo->visualid) {
|
2010-06-19 03:17:00 +04:00
|
|
|
matchIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matchIndex == -1) {
|
|
|
|
NS_WARNING("[GLX] Couldn't find a FBConfig matching widget visual");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
GLContextGLX *shareContext = GetGlobalContextGLX();
|
|
|
|
|
|
|
|
nsRefPtr<GLContextGLX> glContext = GLContextGLX::CreateGLContext(ContextFormat(ContextFormat::BasicRGB24),
|
|
|
|
display,
|
2010-06-19 03:17:00 +04:00
|
|
|
window,
|
|
|
|
cfgs[matchIndex],
|
2010-07-19 09:01:14 +04:00
|
|
|
vinfo,
|
|
|
|
shareContext,
|
2010-06-19 03:17:00 +04:00
|
|
|
PR_FALSE);
|
2010-06-24 07:52:48 +04:00
|
|
|
return glContext.forget();
|
2010-06-15 10:55:08 +04:00
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
static already_AddRefed<GLContextGLX>
|
|
|
|
CreateOffscreenPixmapContext(const gfxIntSize& aSize,
|
|
|
|
const ContextFormat& aFormat,
|
|
|
|
PRBool aShare)
|
|
|
|
{
|
|
|
|
if (!sGLXLibrary.EnsureInitialized()) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
Display *display = DefaultXDisplay();
|
|
|
|
int xscreen = DefaultScreen(display);
|
|
|
|
|
|
|
|
int attribs[] = {
|
|
|
|
GLX_DOUBLEBUFFER, False,
|
|
|
|
GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
|
|
|
|
GLX_X_RENDERABLE, True,
|
|
|
|
GLX_RED_SIZE, 1,
|
|
|
|
GLX_GREEN_SIZE, 1,
|
|
|
|
GLX_BLUE_SIZE, 1,
|
|
|
|
GLX_ALPHA_SIZE, 0,
|
|
|
|
GLX_DEPTH_SIZE, 0,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
int numConfigs = 0;
|
|
|
|
|
|
|
|
ScopedXFree<GLXFBConfig> cfgs;
|
|
|
|
cfgs = sGLXLibrary.xChooseFBConfig(display,
|
|
|
|
xscreen,
|
|
|
|
attribs,
|
|
|
|
&numConfigs);
|
|
|
|
if (!cfgs) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(numConfigs > 0,
|
|
|
|
"glXChooseFBConfig() failed to match our requested format and violated its spec (!)");
|
|
|
|
|
|
|
|
ScopedXFree<XVisualInfo> vinfo;
|
2010-12-05 21:38:12 +03:00
|
|
|
int chosenIndex = 0;
|
2010-07-19 09:01:14 +04:00
|
|
|
|
|
|
|
for (int i = 0; i < numConfigs; ++i) {
|
|
|
|
int dtype, visid;
|
|
|
|
|
|
|
|
if (sGLXLibrary.xGetFBConfigAttrib(display, cfgs[i], GLX_DRAWABLE_TYPE, &dtype) != Success
|
|
|
|
|| !(dtype & GLX_PIXMAP_BIT))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (sGLXLibrary.xGetFBConfigAttrib(display, cfgs[i], GLX_VISUAL_ID, &visid) != Success
|
|
|
|
|| visid == 0)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
vinfo = sGLXLibrary.xGetVisualFromFBConfig(display, cfgs[i]);
|
|
|
|
|
|
|
|
if (vinfo) {
|
|
|
|
chosenIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!vinfo) {
|
|
|
|
NS_WARNING("glXChooseFBConfig() didn't give us any configs with visuals!");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-12-06 14:34:34 +03:00
|
|
|
ScopedXErrorHandler xErrorHandler;
|
2011-01-07 13:54:19 +03:00
|
|
|
GLXPixmap glxpixmap = 0;
|
2010-12-06 14:34:34 +03:00
|
|
|
bool error = false;
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
nsRefPtr<gfxXlibSurface> xsurface = gfxXlibSurface::Create(DefaultScreenOfDisplay(display),
|
|
|
|
vinfo->visual,
|
|
|
|
gfxIntSize(16, 16));
|
|
|
|
if (xsurface->CairoStatus() != 0) {
|
2010-12-06 14:34:34 +03:00
|
|
|
error = true;
|
|
|
|
goto DONE_CREATING_PIXMAP;
|
2010-07-19 09:01:14 +04:00
|
|
|
}
|
|
|
|
|
2010-11-10 18:49:52 +03:00
|
|
|
// Handle slightly different signature between glXCreatePixmap and
|
|
|
|
// its pre-GLX-1.3 extension equivalent (though given the ABI, we
|
|
|
|
// might not need to).
|
2010-11-10 18:49:52 +03:00
|
|
|
if (GLXVersionCheck(1, 3)) {
|
2010-11-10 18:49:52 +03:00
|
|
|
glxpixmap = sGLXLibrary.xCreatePixmap(display,
|
|
|
|
cfgs[chosenIndex],
|
|
|
|
xsurface->XDrawable(),
|
|
|
|
NULL);
|
|
|
|
} else {
|
|
|
|
glxpixmap = sGLXLibrary.xCreateGLXPixmapWithConfig(display,
|
|
|
|
cfgs[chosenIndex],
|
|
|
|
xsurface->
|
|
|
|
XDrawable());
|
|
|
|
}
|
2010-07-19 09:01:14 +04:00
|
|
|
if (glxpixmap == 0) {
|
2010-12-06 14:34:34 +03:00
|
|
|
error = true;
|
2010-07-19 09:01:14 +04:00
|
|
|
}
|
|
|
|
|
2010-12-06 14:34:34 +03:00
|
|
|
DONE_CREATING_PIXMAP:
|
2010-07-19 09:01:14 +04:00
|
|
|
|
2010-12-06 14:34:34 +03:00
|
|
|
nsRefPtr<GLContextGLX> glContext;
|
|
|
|
bool serverError = xErrorHandler.SyncAndGetError(display);
|
|
|
|
|
|
|
|
if (!error && // earlier recorded error
|
|
|
|
!serverError)
|
|
|
|
{
|
|
|
|
glContext = GLContextGLX::CreateGLContext(
|
|
|
|
aFormat,
|
|
|
|
display,
|
|
|
|
glxpixmap,
|
|
|
|
cfgs[chosenIndex],
|
|
|
|
vinfo,
|
|
|
|
aShare ? GetGlobalContextGLX() : nsnull,
|
|
|
|
PR_TRUE,
|
|
|
|
xsurface);
|
|
|
|
}
|
2010-07-19 09:01:14 +04:00
|
|
|
|
|
|
|
return glContext.forget();
|
|
|
|
}
|
|
|
|
|
2010-06-15 10:55:08 +04:00
|
|
|
already_AddRefed<GLContext>
|
2010-07-19 09:01:14 +04:00
|
|
|
GLContextProviderGLX::CreateOffscreen(const gfxIntSize& aSize,
|
|
|
|
const ContextFormat& aFormat)
|
|
|
|
{
|
|
|
|
|
|
|
|
nsRefPtr<GLContextGLX> glContext =
|
|
|
|
CreateOffscreenPixmapContext(aSize, aFormat, PR_TRUE);
|
|
|
|
|
|
|
|
if (!glContext) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!glContext->GetSharedContext()) {
|
|
|
|
// no point in returning anything if sharing failed, we can't
|
|
|
|
// render from this
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!glContext->ResizeOffscreenFBO(aSize)) {
|
|
|
|
// we weren't able to create the initial
|
|
|
|
// offscreen FBO, so this is dead
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
return glContext.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<GLContext>
|
|
|
|
GLContextProviderGLX::CreateForNativePixmapSurface(gfxASurface *aSurface)
|
2010-06-15 10:55:08 +04:00
|
|
|
{
|
|
|
|
if (!sGLXLibrary.EnsureInitialized()) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
if (aSurface->GetType() != gfxASurface::SurfaceTypeXlib) {
|
|
|
|
NS_WARNING("GLContextProviderGLX::CreateForNativePixmapSurface called with non-Xlib surface");
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoTArray<int, 20> attribs;
|
2010-06-15 10:55:08 +04:00
|
|
|
|
|
|
|
#define A1_(_x) do { attribs.AppendElement(_x); } while(0)
|
|
|
|
#define A2_(_x,_y) do { \
|
|
|
|
attribs.AppendElement(_x); \
|
|
|
|
attribs.AppendElement(_y); \
|
|
|
|
} while(0)
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
A2_(GLX_DOUBLEBUFFER, False);
|
|
|
|
A2_(GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT);
|
|
|
|
A1_(0);
|
|
|
|
|
2010-06-15 10:55:08 +04:00
|
|
|
int numFormats;
|
2010-06-24 07:52:48 +04:00
|
|
|
Display *display = DefaultXDisplay();
|
2010-06-15 10:55:08 +04:00
|
|
|
int xscreen = DefaultScreen(display);
|
|
|
|
|
2010-06-24 07:52:48 +04:00
|
|
|
ScopedXFree<GLXFBConfig> cfg(sGLXLibrary.xChooseFBConfig(display,
|
|
|
|
xscreen,
|
|
|
|
attribs.Elements(),
|
|
|
|
&numFormats));
|
2010-06-15 10:55:08 +04:00
|
|
|
if (!cfg) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
2010-06-24 07:52:48 +04:00
|
|
|
NS_ASSERTION(numFormats > 0,
|
|
|
|
"glXChooseFBConfig() failed to match our requested format and violated its spec (!)");
|
2010-07-19 09:01:14 +04:00
|
|
|
|
|
|
|
gfxXlibSurface *xs = static_cast<gfxXlibSurface*>(aSurface);
|
|
|
|
|
|
|
|
GLXPixmap glxpixmap = sGLXLibrary.xCreatePixmap(display,
|
2010-06-15 10:55:08 +04:00
|
|
|
cfg[0],
|
2010-07-19 09:01:14 +04:00
|
|
|
xs->XDrawable(),
|
|
|
|
NULL);
|
2010-06-15 10:55:08 +04:00
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
nsRefPtr<GLContextGLX> glContext = GLContextGLX::CreateGLContext(ContextFormat(ContextFormat::BasicRGB24),
|
|
|
|
display,
|
|
|
|
glxpixmap,
|
|
|
|
cfg[0],
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
PR_FALSE,
|
|
|
|
xs);
|
2010-06-15 10:55:08 +04:00
|
|
|
|
2010-06-24 07:52:48 +04:00
|
|
|
return glContext.forget();
|
2010-06-15 10:55:08 +04:00
|
|
|
}
|
|
|
|
|
2010-07-19 09:01:14 +04:00
|
|
|
static nsRefPtr<GLContext> gGlobalContext;
|
|
|
|
|
|
|
|
GLContext *
|
|
|
|
GLContextProviderGLX::GetGlobalContext()
|
2010-06-23 13:24:31 +04:00
|
|
|
{
|
2010-07-19 09:01:14 +04:00
|
|
|
static bool triedToCreateContext = false;
|
|
|
|
if (!triedToCreateContext && !gGlobalContext) {
|
|
|
|
triedToCreateContext = true;
|
|
|
|
gGlobalContext = CreateOffscreenPixmapContext(gfxIntSize(1, 1),
|
|
|
|
ContextFormat(ContextFormat::BasicRGB24),
|
|
|
|
PR_FALSE);
|
2010-08-07 09:09:18 +04:00
|
|
|
if (gGlobalContext)
|
|
|
|
gGlobalContext->SetIsGlobalSharedContext(PR_TRUE);
|
2010-07-19 09:01:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return gGlobalContext;
|
2010-06-23 13:24:31 +04:00
|
|
|
}
|
|
|
|
|
2010-07-20 08:05:42 +04:00
|
|
|
void
|
|
|
|
GLContextProviderGLX::Shutdown()
|
|
|
|
{
|
|
|
|
gGlobalContext = nsnull;
|
|
|
|
}
|
|
|
|
|
2010-06-15 10:55:08 +04:00
|
|
|
} /* namespace gl */
|
|
|
|
} /* namespace mozilla */
|