|
Line 0
Source/WebCore/platform/graphics/surfaces/glx/EGLXWindowResources.h_sec1
|
|
|
1 |
/* |
| 2 |
* Copyright (C) 2012 Intel Corporation. All rights reserved. |
| 3 |
* |
| 4 |
* Redistribution and use in source and binary forms, with or without |
| 5 |
* modification, are permitted provided that the following conditions |
| 6 |
* are met: |
| 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. |
| 12 |
* |
| 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. |
| 24 |
*/ |
| 25 |
|
| 26 |
#ifndef EGLXWindowResources_h |
| 27 |
#define EGLXWindowResources_h |
| 28 |
|
| 29 |
#include "X11WindowResources.h" |
| 30 |
|
| 31 |
#if USE(ACCELERATED_COMPOSITING) && USE(EGL) |
| 32 |
|
| 33 |
#include <opengl/GLDefs.h> |
| 34 |
|
| 35 |
namespace WebCore { |
| 36 |
|
| 37 |
class SharedEGLResources : public SharedX11Resources { |
| 38 |
|
| 39 |
public: |
| 40 |
static PassRefPtr<SharedEGLResources> create() |
| 41 |
{ |
| 42 |
if (!m_staticSharedResource) |
| 43 |
m_staticSharedResource = new SharedEGLResources(); |
| 44 |
else |
| 45 |
m_staticSharedResource->ref(); |
| 46 |
|
| 47 |
return adoptRef(m_staticSharedResource); |
| 48 |
} |
| 49 |
|
| 50 |
PlatformDisplay nativeDisplay() |
| 51 |
{ |
| 52 |
if (m_eglDisplay == EGL_NO_DISPLAY) { |
| 53 |
EGLBoolean success; |
| 54 |
|
| 55 |
m_eglDisplay = eglGetDisplay(reinterpret_cast<EGLNativeDisplayType>(SharedX11Resources::x11Display())); |
| 56 |
|
| 57 |
if (m_eglDisplay == EGL_NO_DISPLAY) { |
| 58 |
LOG_ERROR("EGLDisplay Initialization failed.\n"); |
| 59 |
return m_eglDisplay; |
| 60 |
} |
| 61 |
|
| 62 |
success = eglInitialize(m_eglDisplay, 0, 0); |
| 63 |
|
| 64 |
if (success != EGL_TRUE) { |
| 65 |
LOG_ERROR("EGLInitialization failed.\n"); |
| 66 |
cleanup(); |
| 67 |
return m_eglDisplay; |
| 68 |
} |
| 69 |
|
| 70 |
success = eglBindAPI(eglAPIVersion); |
| 71 |
|
| 72 |
if (success != EGL_TRUE) { |
| 73 |
LOG_ERROR("failed to set EGL API(%d).\n", eglGetError()); |
| 74 |
cleanup(); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return m_eglDisplay; |
| 79 |
} |
| 80 |
|
| 81 |
virtual EGLConfig pBufferContextConfig() |
| 82 |
{ |
| 83 |
if (!m_pbufferfbConfig) { |
| 84 |
EGLint attributeList[] = { |
| 85 |
#if USE(OPENGL_ES_2) |
| 86 |
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 87 |
#else |
| 88 |
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, |
| 89 |
#endif |
| 90 |
EGL_RED_SIZE, 8, |
| 91 |
EGL_GREEN_SIZE, 8, |
| 92 |
EGL_BLUE_SIZE, 8, |
| 93 |
EGL_STENCIL_SIZE, 8, |
| 94 |
EGL_ALPHA_SIZE, 8, |
| 95 |
EGL_SURFACE_TYPE, EGL_PIXMAP_BIT, |
| 96 |
EGL_NONE |
| 97 |
}; |
| 98 |
m_pbufferfbConfig = createConfig(attributeList); |
| 99 |
} |
| 100 |
return m_pbufferfbConfig; |
| 101 |
} |
| 102 |
|
| 103 |
virtual EGLConfig surfaceContextConfig() |
| 104 |
{ |
| 105 |
if (!m_surfaceContextfbConfig) { |
| 106 |
EGLint attributeList[] = { |
| 107 |
EGL_LEVEL, 0, |
| 108 |
#if USE(OPENGL_ES_2) |
| 109 |
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 110 |
#else |
| 111 |
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, |
| 112 |
#endif |
| 113 |
EGL_RED_SIZE, 8, |
| 114 |
EGL_GREEN_SIZE, 8, |
| 115 |
EGL_BLUE_SIZE, 8, |
| 116 |
EGL_STENCIL_SIZE, 8, |
| 117 |
EGL_ALPHA_SIZE, 8, |
| 118 |
EGL_DEPTH_SIZE, 8, |
| 119 |
EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 120 |
EGL_NONE |
| 121 |
}; |
| 122 |
m_surfaceContextfbConfig = createConfig(attributeList); |
| 123 |
} |
| 124 |
|
| 125 |
return m_surfaceContextfbConfig; |
| 126 |
} |
| 127 |
|
| 128 |
private: |
| 129 |
SharedEGLResources() |
| 130 |
: SharedX11Resources() |
| 131 |
{ |
| 132 |
m_eglDisplay = EGL_NO_DISPLAY; |
| 133 |
m_surfaceContextfbConfig = 0; |
| 134 |
m_pbufferfbConfig = 0; |
| 135 |
} |
| 136 |
|
| 137 |
void cleanup() |
| 138 |
{ |
| 139 |
eglMakeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 140 |
eglTerminate(m_eglDisplay); |
| 141 |
m_eglDisplay = EGL_NO_DISPLAY; |
| 142 |
} |
| 143 |
|
| 144 |
EGLConfig createConfig(const int attributes[]) |
| 145 |
{ |
| 146 |
if (m_eglDisplay == EGL_NO_DISPLAY) { |
| 147 |
// Try to initialize Display if already not done. |
| 148 |
nativeDisplay(); |
| 149 |
|
| 150 |
if (m_eglDisplay == EGL_NO_DISPLAY) |
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
int numConfigs; |
| 155 |
EGLConfig rightConfig = 0; |
| 156 |
|
| 157 |
if (!eglChooseConfig(m_eglDisplay, attributes, &rightConfig, 1, &numConfigs)) { |
| 158 |
LOG_ERROR("Failed to get supported configirations.\n"); |
| 159 |
cleanup(); |
| 160 |
} |
| 161 |
|
| 162 |
if (numConfigs != 1) { |
| 163 |
LOG_ERROR("EGLChooseConfig didn't return exactly one config but (%d).\n", numConfigs); |
| 164 |
cleanup(); |
| 165 |
} |
| 166 |
|
| 167 |
VisualID visualId = 0; |
| 168 |
EGLint eglValue = 0; |
| 169 |
|
| 170 |
// Check if EGL provides us with a valid visual id. |
| 171 |
eglGetConfigAttrib(m_eglDisplay, rightConfig, EGL_NATIVE_VISUAL_ID, &eglValue); |
| 172 |
visualId = static_cast<VisualID>(eglValue); |
| 173 |
if (visualId) { |
| 174 |
// EGL has suggested a visual id, so get the rest of the visual info for that id. |
| 175 |
XVisualInfo visualInfoTemplate; |
| 176 |
memset(&visualInfoTemplate, 0, sizeof(XVisualInfo)); |
| 177 |
visualInfoTemplate.visualid = visualId; |
| 178 |
|
| 179 |
XVisualInfo *chosenVisualInfo; |
| 180 |
int matchingCount = 0; |
| 181 |
chosenVisualInfo = XGetVisualInfo(x11Display(), VisualIDMask, &visualInfoTemplate, &matchingCount); |
| 182 |
if (chosenVisualInfo) { |
| 183 |
#if USE(GRAPHICS_SURFACE) |
| 184 |
if (m_supportsXRenderExtension) { |
| 185 |
XRenderPictFormat* format = XRenderFindVisualFormat(x11Display(), chosenVisualInfo->visual); |
| 186 |
if (format && format->direct.alphaMask > 0) |
| 187 |
m_VisualInfo = chosenVisualInfo; |
| 188 |
} |
| 189 |
#endif |
| 190 |
if (!m_VisualInfo && chosenVisualInfo->depth == 32) |
| 191 |
m_VisualInfo = chosenVisualInfo; |
| 192 |
} |
| 193 |
|
| 194 |
if (!m_VisualInfo) { |
| 195 |
XVisualInfo visualInfoTemplate; |
| 196 |
memset(&visualInfoTemplate, 0, sizeof(XVisualInfo)); |
| 197 |
XVisualInfo *matchingVisuals; |
| 198 |
int matchingCount = 0; |
| 199 |
|
| 200 |
visualInfoTemplate.depth = chosenVisualInfo->depth; |
| 201 |
matchingVisuals = XGetVisualInfo(x11Display(), VisualDepthMask, &visualInfoTemplate, &matchingCount); |
| 202 |
|
| 203 |
if (matchingVisuals) { |
| 204 |
m_VisualInfo = &matchingVisuals[0]; |
| 205 |
XFree(matchingVisuals); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if (!m_VisualInfo) { |
| 211 |
LOG_ERROR("Failed to get Visual Info.\n"); |
| 212 |
cleanup(); |
| 213 |
} |
| 214 |
|
| 215 |
return rightConfig; |
| 216 |
} |
| 217 |
|
| 218 |
virtual ~SharedEGLResources() |
| 219 |
{ |
| 220 |
if (m_eglDisplay == EGL_NO_DISPLAY) |
| 221 |
return; |
| 222 |
|
| 223 |
if (m_surfaceContextfbConfig) |
| 224 |
m_surfaceContextfbConfig = 0; |
| 225 |
|
| 226 |
eglTerminate(m_eglDisplay); |
| 227 |
m_eglDisplay = EGL_NO_DISPLAY; |
| 228 |
} |
| 229 |
|
| 230 |
protected: |
| 231 |
EGLDisplay m_eglDisplay; |
| 232 |
EGLConfig m_pbufferfbConfig; |
| 233 |
EGLConfig m_surfaceContextfbConfig; |
| 234 |
}; |
| 235 |
|
| 236 |
} |
| 237 |
|
| 238 |
#endif |
| 239 |
|
| 240 |
#endif |