2 * Copyright (C) 2012 Intel Corporation. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef GLXConfigSelector_h
27 #define GLXConfigSelector_h
29 #if USE(ACCELERATED_COMPOSITING) && USE(GLX)
31 #include "X11Helper.h"
32 #include <opengl/GLDefs.h>
36 static int clientAttributes[] = {
37 // The specification is a set key value pairs stored in a simple array.
39 static_cast<int>(GLX_VISUAL_ID), 0,
40 GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
41 GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_BIT_EXT,
42 GLX_BIND_TO_TEXTURE_RGBA_EXT, TRUE,
46 class GLXConfigSelector {
47 WTF_MAKE_NONCOPYABLE(GLXConfigSelector);
51 : m_pbufferFBConfig(0)
52 , m_surfaceContextFBConfig(0)
56 virtual ~GLXConfigSelector()
60 XVisualInfo* visualInfo()
62 if (!surfaceContextConfig())
65 return glXGetVisualFromFBConfig(X11Helper::nativeDisplay(), m_surfaceContextFBConfig);
68 GLXFBConfig pBufferContextConfig()
70 if (!m_pbufferFBConfig) {
71 static const int attributes[] = {
73 GLX_DRAWABLE_TYPE, GLX_PBUFFER_BIT,
74 GLX_RENDER_TYPE, GLX_RGBA_BIT,
78 GLX_DOUBLEBUFFER, GL_FALSE,
82 m_pbufferFBConfig = findMatchingConfig(attributes);
85 return m_pbufferFBConfig;
88 GLXFBConfig surfaceContextConfig()
90 if (!m_surfaceContextFBConfig)
91 createSurfaceConfig();
93 return m_surfaceContextFBConfig;
96 GLXFBConfig surfaceClientConfig(int depth, VisualID id)
98 clientAttributes[3] = static_cast<int>(id);
99 clientAttributes[8] = depth == 32 ? GLX_BIND_TO_TEXTURE_RGBA_EXT : GLX_BIND_TO_TEXTURE_RGB_EXT;
101 return findMatchingConfig(clientAttributes, depth, id);
106 m_pbufferFBConfig = 0;
107 m_surfaceContextFBConfig = 0;
111 void createSurfaceConfig()
113 static int attributes[] = {
115 GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
116 GLX_RENDER_TYPE, GLX_RGBA_BIT,
122 GLX_X_VISUAL_TYPE, GLX_TRUE_COLOR,
123 GLX_DOUBLEBUFFER, True,
127 m_surfaceContextFBConfig = findMatchingConfig(attributes);
130 GLXFBConfig findMatchingConfig(const int attributes[], int depth = 32, VisualID id = 0)
132 int numAvailableConfigs;
133 OwnPtrX11<GLXFBConfig> temp(glXChooseFBConfig(X11Helper::nativeDisplay(), DefaultScreen(X11Helper::nativeDisplay()), attributes, &numAvailableConfigs));
135 if (!numAvailableConfigs || !temp.get())
138 OwnPtrX11<XVisualInfo> scopedVisualInfo;
139 for (int i = 0; i < numAvailableConfigs; ++i) {
140 scopedVisualInfo = glXGetVisualFromFBConfig(X11Helper::nativeDisplay(), temp[i]);
141 if (!scopedVisualInfo.get())
144 if (id && scopedVisualInfo->depth == depth && scopedVisualInfo->visualid == id)
147 #if USE(GRAPHICS_SURFACE)
148 if (X11Helper::isXRenderExtensionSupported()) {
149 XRenderPictFormat* format = XRenderFindVisualFormat(X11Helper::nativeDisplay(), scopedVisualInfo->visual);
150 if (format && depth == 32 && format->direct.alphaMask > 0)
154 if (scopedVisualInfo->depth == depth)
158 // Did not find any visual supporting alpha, select the first available config.
162 GLXFBConfig m_pbufferFBConfig;
163 GLXFBConfig m_surfaceContextFBConfig;