1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
/*
* Copyright (C) 2013 Intel Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "X11Helper.h"
namespace WebCore {
// Used for handling XError.
static bool validOperation = true;
static int handleXPixmapCreationError(Display*, XErrorEvent* event)
{
if (event->error_code == BadMatch || event->error_code == BadWindow || event->error_code == BadAlloc) {
validOperation = false;
switch (event->error_code) {
case BadMatch:
LOG_ERROR("BadMatch.");
break;
case BadWindow:
LOG_ERROR("BadWindow.");
break;
case BadAlloc:
LOG_ERROR("BadAlloc.");
break;
default:
break;
}
}
return 0;
}
struct DisplayConnection {
DisplayConnection()
{
m_display = XOpenDisplay(0);
if (!m_display)
LOG_ERROR("Failed to make connection with X");
}
~DisplayConnection()
{
XCloseDisplay(m_display);
}
Display* display() { return m_display; }
private:
Display* m_display;
};
struct OffScreenRootWindow {
OffScreenRootWindow()
{
m_window = 0;
Display* dpy = X11Helper::nativeDisplay();
if (!dpy)
return;
XSetWindowAttributes attributes;
attributes.override_redirect = true;
m_window = XCreateSimpleWindow(dpy, XDefaultRootWindow(dpy), -1, -1, 1, 1, 0, BlackPixel(dpy, 0), WhitePixel(dpy, 0));
// From http://tronche.com/gui/x/xlib/window/attributes/
XChangeWindowAttributes(dpy, m_window, CWOverrideRedirect, &attributes);
XMapWindow(dpy, m_window);
if (!m_window)
LOG_ERROR("Failed to create offscreen root window.");
}
~OffScreenRootWindow()
{
if (!X11Helper::nativeDisplay())
return;
if (m_window) {
XUnmapWindow(X11Helper::nativeDisplay(), m_window);
XDestroyWindow(X11Helper::nativeDisplay(), m_window);
m_window = 0;
}
}
Window rootWindow()
{
return m_window;
}
private:
Window m_window;
};
ScopedXPixmapCreationErrorHandler::ScopedXPixmapCreationErrorHandler()
{
// XSync must be called to ensure that current errors are handled by the original handler.
XSync(X11Helper::nativeDisplay(), false);
m_previousErrorHandler = XSetErrorHandler(handleXPixmapCreationError);
}
ScopedXPixmapCreationErrorHandler::~ScopedXPixmapCreationErrorHandler()
{
// Restore the original handler.
XSetErrorHandler(m_previousErrorHandler);
}
bool ScopedXPixmapCreationErrorHandler::isValidOperation() const
{
validOperation = true;
// XSync is needed to catch possible errors as they are generated asynchronously.
XSync(X11Helper::nativeDisplay(), false);
return validOperation;
}
void X11Helper::resizeWindow(const IntRect& newRect, const uint32_t windowId)
{
XResizeWindow(nativeDisplay(), windowId, newRect.width(), newRect.height());
XFlush(nativeDisplay());
}
void X11Helper::createPixmap(Pixmap* handleId, const XVisualInfo& visualInfo, const IntSize& size)
{
Display* display = nativeDisplay();
if (!display)
return;
if (!visualInfo.visual) {
LOG_ERROR("Failed to find valid XVisual.");
return;
}
Window xWindow = offscreenRootWindow();
if (!xWindow) {
LOG_ERROR("Failed to create offscreen root window.");
return;
}
Pixmap tempHandleId = XCreatePixmap(display, xWindow, size.width(), size.height(), visualInfo.depth);
if (!tempHandleId) {
LOG_ERROR("Failed to create offscreen pixmap.");
return;
}
*handleId = tempHandleId;
XSync(X11Helper::nativeDisplay(), false);
}
void X11Helper::destroyPixmap(const uint32_t pixmapId)
{
if (!pixmapId)
return;
Display* display = nativeDisplay();
if (!display)
return;
XFreePixmap(display, pixmapId);
XSync(X11Helper::nativeDisplay(), false);
}
void X11Helper::createOffScreenWindow(uint32_t* handleId, const XVisualInfo& visInfo, const IntSize& size)
{
#if USE(GRAPHICS_SURFACE)
Display* display = nativeDisplay();
if (!display)
return;
if (!visInfo.visual) {
LOG_ERROR("Failed to find valid XVisual.");
return;
}
Window xWindow = offscreenRootWindow();
if (!xWindow)
return;
Colormap cmap = XCreateColormap(display, xWindow, visInfo.visual, AllocNone);
XSetWindowAttributes attribute;
attribute.background_pixel = WhitePixel(display, 0);
attribute.border_pixel = BlackPixel(display, 0);
attribute.colormap = cmap;
#if USE(GLX)
attribute.event_mask = ResizeRedirectMask;
#endif
uint32_t tempHandleId = XCreateWindow(display, xWindow, 0, 0, size.width(), size.height(), 0, visInfo.depth, InputOutput, visInfo.visual, CWBackPixel | CWBorderPixel | CWColormap, &attribute);
if (!tempHandleId) {
LOG_ERROR("Failed to create offscreen window.");
return;
}
XSetWindowBackgroundPixmap(display, tempHandleId, 0);
#if USE(GLX)
XCompositeRedirectWindow(display, tempHandleId, CompositeRedirectManual);
#endif
XMapWindow(display, tempHandleId);
*handleId = tempHandleId;
#else
UNUSED_PARAM(handleId);
UNUSED_PARAM(visInfo);
UNUSED_PARAM(size);
#endif
}
#if USE(EGL)
void X11Helper::createOffScreenWindow(uint32_t* handleId, const EGLint id, bool supportsAlpha, const IntSize& size)
{
#if USE(GRAPHICS_SURFACE)
VisualID visualId = static_cast<VisualID>(id);
if (!visualId)
return;
// EGL has suggested a visual id, so get the rest of the visual info for that id.
XVisualInfo visualInfoTemplate;
memset(&visualInfoTemplate, 0, sizeof(XVisualInfo));
visualInfoTemplate.visualid = visualId;
int matchingCount = 0;
OwnPtrX11<XVisualInfo> matchingVisuals(XGetVisualInfo(nativeDisplay(), VisualIDMask, &visualInfoTemplate, &matchingCount));
XVisualInfo* foundVisual = 0;
if (matchingVisuals) {
for (int i = 0; i< matchingCount; i++) {
XVisualInfo* temp = &matchingVisuals[i];
int matchingdepth = supportsAlpha ? 32 : 24;
if (temp->visualid == visualId && temp->depth == matchingdepth) {
foundVisual = temp;
break;
}
}
if (foundVisual)
createOffScreenWindow(handleId, *foundVisual, size);
}
#else
UNUSED_PARAM(handleId);
UNUSED_PARAM(id);
UNUSED_PARAM(size);
#endif
}
void X11Helper::createPixmap(Pixmap* handleId, const EGLint id, bool hasAlpha, const IntSize& size)
{
VisualID visualId = static_cast<VisualID>(id);
if (!visualId)
return;
// EGL has suggested a visual id, so get the rest of the visual info for that id.
XVisualInfo visualInfoTemplate;
memset(&visualInfoTemplate, 0, sizeof(XVisualInfo));
visualInfoTemplate.visualid = visualId;
int matchingCount = 0;
OwnPtrX11<XVisualInfo> matchingVisuals(XGetVisualInfo(nativeDisplay(), VisualIDMask, &visualInfoTemplate, &matchingCount));
XVisualInfo* foundVisual = 0;
int requiredDepth = hasAlpha ? 32 : 24;
if (matchingVisuals) {
for (int i = 0; i< matchingCount; i++) {
XVisualInfo* temp = &matchingVisuals[i];
if (temp->visualid == visualId && temp->depth == requiredDepth) {
foundVisual = temp;
break;
}
}
if (foundVisual)
createPixmap(handleId, *foundVisual, size);
}
}
#endif
void X11Helper::destroyWindow(const uint32_t windowId)
{
if (!windowId)
return;
Display* display = nativeDisplay();
if (!display)
return;
XDestroyWindow(display, windowId);
}
bool X11Helper::isXRenderExtensionSupported()
{
static bool queryDone = false;
static bool supportsXRenderExtension = false;
if (!queryDone) {
queryDone = true;
#if USE(GRAPHICS_SURFACE) && USE(GLX)
Display* display = nativeDisplay();
if (display) {
int eventBasep, errorBasep;
supportsXRenderExtension = XRenderQueryExtension(display, &eventBasep, &errorBasep);
}
#endif
}
return supportsXRenderExtension;
}
Display* X11Helper::nativeDisplay()
{
// Display connection will only be broken at program shutdown.
static DisplayConnection displayConnection;
return displayConnection.display();
}
Window X11Helper::offscreenRootWindow()
{
static OffScreenRootWindow offscreenWindow;
return offscreenWindow.rootWindow();
}
}
|