diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
commit | 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c (patch) | |
tree | 46dcd36c86e7fbc6e5df36deb463b33e9967a6f7 /Source/WebCore/platform/graphics/x11 | |
parent | 32761a6cee1d0dee366b885b7b9c777e67885688 (diff) | |
download | WebKitGtk-tarball-master.tar.gz |
webkitgtk-2.16.5HEADwebkitgtk-2.16.5master
Diffstat (limited to 'Source/WebCore/platform/graphics/x11')
7 files changed, 631 insertions, 0 deletions
diff --git a/Source/WebCore/platform/graphics/x11/PlatformDisplayX11.cpp b/Source/WebCore/platform/graphics/x11/PlatformDisplayX11.cpp new file mode 100644 index 000000000..5aefe6d51 --- /dev/null +++ b/Source/WebCore/platform/graphics/x11/PlatformDisplayX11.cpp @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2015 Igalia S.L + * + * 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 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 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 "PlatformDisplayX11.h" + +#include "GLContext.h" + +#if PLATFORM(X11) +#include <X11/Xlib.h> +#include <X11/extensions/Xcomposite.h> +#if PLATFORM(GTK) +#include <X11/extensions/Xdamage.h> +#endif + +#if USE(EGL) +#include <EGL/egl.h> +#include <EGL/eglext.h> +#endif + +namespace WebCore { + +std::unique_ptr<PlatformDisplay> PlatformDisplayX11::create() +{ + Display* display = XOpenDisplay(getenv("DISPLAY")); + if (!display) + return nullptr; + + return std::make_unique<PlatformDisplayX11>(display, NativeDisplayOwned::Yes); +} + +PlatformDisplayX11::PlatformDisplayX11(Display* display, NativeDisplayOwned displayOwned) + : PlatformDisplay(displayOwned) + , m_display(display) +{ +} + +PlatformDisplayX11::~PlatformDisplayX11() +{ +#if USE(EGL) || USE(GLX) + // Clear the sharing context before releasing the display. + m_sharingGLContext = nullptr; +#endif + if (m_nativeDisplayOwned == NativeDisplayOwned::Yes) + XCloseDisplay(m_display); +} + +#if USE(EGL) +void PlatformDisplayX11::initializeEGLDisplay() +{ +#if defined(EGL_KHR_platform_x11) + const char* extensions = eglQueryString(nullptr, EGL_EXTENSIONS); + if (GLContext::isExtensionSupported(extensions, "EGL_KHR_platform_base")) { + if (auto* getPlatformDisplay = reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(eglGetProcAddress("eglGetPlatformDisplay"))) + m_eglDisplay = getPlatformDisplay(EGL_PLATFORM_X11_KHR, m_display, nullptr); + } else if (GLContext::isExtensionSupported(extensions, "EGL_EXT_platform_base")) { + if (auto* getPlatformDisplay = reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(eglGetProcAddress("eglGetPlatformDisplayEXT"))) + m_eglDisplay = getPlatformDisplay(EGL_PLATFORM_X11_KHR, m_display, nullptr); + } else +#endif + m_eglDisplay = eglGetDisplay(m_display); + + PlatformDisplay::initializeEGLDisplay(); +} +#endif // USE(EGL) + +bool PlatformDisplayX11::supportsXComposite() const +{ + if (!m_supportsXComposite) { + if (m_display) { + int eventBase, errorBase; + m_supportsXComposite = XCompositeQueryExtension(m_display, &eventBase, &errorBase); + } else + m_supportsXComposite = false; + } + return m_supportsXComposite.value(); +} + +bool PlatformDisplayX11::supportsXDamage(std::optional<int>& damageEventBase, std::optional<int>& damageErrorBase) const +{ + if (!m_supportsXDamage) { + m_supportsXDamage = false; +#if PLATFORM(GTK) + if (m_display) { + int eventBase, errorBase; + m_supportsXDamage = XDamageQueryExtension(m_display, &eventBase, &errorBase); + if (m_supportsXDamage.value()) { + m_damageEventBase = eventBase; + m_damageErrorBase = errorBase; + } + } +#endif + } + + damageEventBase = m_damageEventBase; + damageErrorBase = m_damageErrorBase; + return m_supportsXDamage.value(); +} + +} // namespace WebCore + +#endif // PLATFORM(X11) + diff --git a/Source/WebCore/platform/graphics/x11/PlatformDisplayX11.h b/Source/WebCore/platform/graphics/x11/PlatformDisplayX11.h new file mode 100644 index 000000000..311744576 --- /dev/null +++ b/Source/WebCore/platform/graphics/x11/PlatformDisplayX11.h @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2015 Igalia S.L + * + * 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 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 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. + */ + +#ifndef PlatformDisplayX11_h +#define PlatformDisplayX11_h + +#if PLATFORM(X11) + +#include "PlatformDisplay.h" +#include <wtf/Optional.h> + +typedef struct _XDisplay Display; + +namespace WebCore { + +class PlatformDisplayX11 final : public PlatformDisplay { +public: + static std::unique_ptr<PlatformDisplay> create(); + PlatformDisplayX11(Display*, NativeDisplayOwned = NativeDisplayOwned::No); + virtual ~PlatformDisplayX11(); + + Display* native() const { return m_display; } + bool supportsXComposite() const; + bool supportsXDamage(std::optional<int>& damageEventBase, std::optional<int>& damageErrorBase) const; + +private: + Type type() const override { return PlatformDisplay::Type::X11; } + +#if USE(EGL) + void initializeEGLDisplay() override; +#endif + + Display* m_display { nullptr }; + mutable std::optional<bool> m_supportsXComposite; + mutable std::optional<bool> m_supportsXDamage; + mutable std::optional<int> m_damageEventBase; + mutable std::optional<int> m_damageErrorBase; +}; + +} // namespace WebCore + +SPECIALIZE_TYPE_TRAITS_PLATFORM_DISPLAY(PlatformDisplayX11, X11) + +#endif // PLATFORM(X11) + +#endif // PlatformDisplayX11 diff --git a/Source/WebCore/platform/graphics/x11/XErrorTrapper.cpp b/Source/WebCore/platform/graphics/x11/XErrorTrapper.cpp new file mode 100644 index 000000000..6fd4d49df --- /dev/null +++ b/Source/WebCore/platform/graphics/x11/XErrorTrapper.cpp @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2016 Igalia S.L + * + * 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 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 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 "XErrorTrapper.h" + +#if PLATFORM(X11) +#include <sys/types.h> +#include <unistd.h> +#include <wtf/HashMap.h> +#include <wtf/NeverDestroyed.h> + +namespace WebCore { + +static HashMap<Display*, Vector<XErrorTrapper*>>& xErrorTrappersMap() +{ + static NeverDestroyed<HashMap<Display*, Vector<XErrorTrapper*>>> trappersMap; + return trappersMap; +} + +XErrorTrapper::XErrorTrapper(Display* display, Policy policy, Vector<unsigned char>&& expectedErrors) + : m_display(display) + , m_policy(policy) + , m_expectedErrors(WTFMove(expectedErrors)) +{ + xErrorTrappersMap().add(m_display, Vector<XErrorTrapper*>()).iterator->value.append(this); + m_previousErrorHandler = XSetErrorHandler([](Display* display, XErrorEvent* event) -> int { + auto iterator = xErrorTrappersMap().find(display); + if (iterator == xErrorTrappersMap().end()) + return 0; + + ASSERT(!iterator->value.isEmpty()); + iterator->value.last()->errorEvent(event); + return 0; + }); +} + +XErrorTrapper::~XErrorTrapper() +{ + XSync(m_display, False); + auto iterator = xErrorTrappersMap().find(m_display); + ASSERT(iterator != xErrorTrappersMap().end()); + auto* trapper = iterator->value.takeLast(); + ASSERT_UNUSED(trapper, trapper == this); + if (iterator->value.isEmpty()) + xErrorTrappersMap().remove(iterator); + + XSetErrorHandler(m_previousErrorHandler); +} + +unsigned char XErrorTrapper::errorCode() const +{ + XSync(m_display, False); + return m_errorCode; +} + +void XErrorTrapper::errorEvent(XErrorEvent* event) +{ + m_errorCode = event->error_code; + if (m_policy == Policy::Ignore) + return; + + if (m_expectedErrors.contains(m_errorCode)) + return; + + static const char errorFormatString[] = "The program with pid %d received an X Window System error.\n" + "The error was '%s'.\n" + " (Details: serial %ld error_code %d request_code %d minor_code %d)\n"; + char errorMessage[64]; + XGetErrorText(m_display, m_errorCode, errorMessage, 63); + WTFLogAlways(errorFormatString, getpid(), errorMessage, event->serial, event->error_code, event->request_code, event->minor_code); + + if (m_policy == Policy::Crash) + CRASH(); +} + +} // namespace WebCore + +#endif // PLATFORM(X11) diff --git a/Source/WebCore/platform/graphics/x11/XErrorTrapper.h b/Source/WebCore/platform/graphics/x11/XErrorTrapper.h new file mode 100644 index 000000000..5d53b6c33 --- /dev/null +++ b/Source/WebCore/platform/graphics/x11/XErrorTrapper.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2016 Igalia S.L + * + * 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 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 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. + */ + +#pragma once + +#if PLATFORM(X11) +#include <X11/Xlib.h> +#include <wtf/Vector.h> + +namespace WebCore { + +class XErrorTrapper { +public: + enum class Policy { Ignore, Warn, Crash }; + XErrorTrapper(Display*, Policy = Policy::Ignore, Vector<unsigned char>&& expectedErrors = { }); + ~XErrorTrapper(); + + unsigned char errorCode() const; + +private: + void errorEvent(XErrorEvent*); + + Display* m_display { nullptr }; + Policy m_policy { Policy::Ignore }; + Vector<unsigned char> m_expectedErrors; + XErrorHandler m_previousErrorHandler { nullptr }; + unsigned char m_errorCode { 0 }; +}; + +} // namespace WebCore + +#endif // PLATFORM(X11) diff --git a/Source/WebCore/platform/graphics/x11/XUniquePtr.h b/Source/WebCore/platform/graphics/x11/XUniquePtr.h new file mode 100644 index 000000000..37a18c29d --- /dev/null +++ b/Source/WebCore/platform/graphics/x11/XUniquePtr.h @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2015 Igalia S.L + * + * 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 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 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. + */ + +#ifndef XUniquePtr_h +#define XUniquePtr_h + +#if PLATFORM(X11) +#include "PlatformDisplayX11.h" +#include <X11/Xutil.h> + +#if USE(GLX) +typedef struct __GLXcontextRec* GLXContext; +extern "C" void glXDestroyContext(Display*, GLXContext); +#endif + +namespace WebCore { + +template<typename T> +struct XPtrDeleter { + void operator()(T* ptr) const + { + XFree(ptr); + } +}; + +template<typename T> +using XUniquePtr = std::unique_ptr<T, XPtrDeleter<T>>; + +template<> struct XPtrDeleter<XImage> { + void operator() (XImage* ptr) const + { + XDestroyImage(ptr); + } +}; + +template<> struct XPtrDeleter<_XGC> { + void operator() (_XGC* ptr) const + { + XFreeGC(downcast<PlatformDisplayX11>(PlatformDisplay::sharedDisplay()).native(), ptr); + } +}; +// Give a name to this to avoid having to use the internal struct name. +using XUniqueGC = XUniquePtr<_XGC>; + +#if USE(GLX) +template<> struct XPtrDeleter<__GLXcontextRec> { + void operator() (__GLXcontextRec* ptr) + { + glXDestroyContext(downcast<PlatformDisplayX11>(PlatformDisplay::sharedDisplay()).native(), ptr); + } +}; +// Give a name to this to avoid having to use the internal struct name. +using XUniqueGLXContext = XUniquePtr<__GLXcontextRec>; +#endif + +} // namespace WebCore + +#endif // PLATFORM(X11) + +#endif // XUniquePtr_h diff --git a/Source/WebCore/platform/graphics/x11/XUniqueResource.cpp b/Source/WebCore/platform/graphics/x11/XUniqueResource.cpp new file mode 100644 index 000000000..f300ee4f5 --- /dev/null +++ b/Source/WebCore/platform/graphics/x11/XUniqueResource.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2015 Igalia S.L + * + * 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 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 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 "XUniqueResource.h" + +#if PLATFORM(X11) +#include "PlatformDisplayX11.h" +#include <X11/Xlib.h> +#include <X11/Xutil.h> + +#if PLATFORM(GTK) +#include <X11/extensions/Xdamage.h> +#endif + +#if USE(GLX) +#include <GL/glx.h> +#endif + +namespace WebCore { + +static inline Display* sharedDisplay() +{ + return downcast<PlatformDisplayX11>(PlatformDisplay::sharedDisplay()).native(); +} + +template<> void XUniqueResource<XResource::Colormap>::deleteXResource(unsigned long resource) +{ + if (resource) + XFreeColormap(sharedDisplay(), resource); +} + +#if PLATFORM(GTK) +template<> void XUniqueResource<XResource::Damage>::deleteXResource(unsigned long resource) +{ + if (resource) + XDamageDestroy(sharedDisplay(), resource); +} +#endif + +template<> void XUniqueResource<XResource::Pixmap>::deleteXResource(unsigned long resource) +{ + if (resource) + XFreePixmap(sharedDisplay(), resource); +} + +template<> void XUniqueResource<XResource::Window>::deleteXResource(unsigned long resource) +{ + if (resource) + XDestroyWindow(sharedDisplay(), resource); +} + +#if USE(GLX) +template<> void XUniqueResource<XResource::GLXPbuffer>::deleteXResource(unsigned long resource) +{ + if (resource) + glXDestroyPbuffer(sharedDisplay(), resource); +} + +template<> void XUniqueResource<XResource::GLXPixmap>::deleteXResource(unsigned long resource) +{ + if (resource) + glXDestroyGLXPixmap(sharedDisplay(), resource); +} +#endif // USE(GLX) + +} // namespace WebCore + +#endif // PLATFORM(X11) diff --git a/Source/WebCore/platform/graphics/x11/XUniqueResource.h b/Source/WebCore/platform/graphics/x11/XUniqueResource.h new file mode 100644 index 000000000..0da8b0c9c --- /dev/null +++ b/Source/WebCore/platform/graphics/x11/XUniqueResource.h @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2015 Igalia S.L + * + * 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 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 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. + */ + +#ifndef XUniqueResource_h +#define XUniqueResource_h + +#if PLATFORM(X11) + +#if USE(GLX) +typedef unsigned long GLXPbuffer; +typedef unsigned long GLXPixmap; +#endif + +namespace WebCore { + +enum class XResource { + Colormap, +#if PLATFORM(GTK) + Damage, +#endif + Pixmap, + Window, +#if USE(GLX) + GLXPbuffer, + GLXPixmap, +#endif +}; + +template <XResource T> class XUniqueResource { +public: + XUniqueResource() + { + } + + XUniqueResource(unsigned long resource) + : m_resource(resource) + { + } + + XUniqueResource(XUniqueResource&& uniqueResource) + : m_resource(uniqueResource.release()) + { + } + + XUniqueResource& operator=(XUniqueResource&& uniqueResource) + { + reset(uniqueResource.release()); + return *this; + } + + ~XUniqueResource() + { + reset(); + } + + unsigned long get() const { return m_resource; } + unsigned long release() { return std::exchange(m_resource, 0); } + + void reset(unsigned long resource = 0) + { + std::swap(m_resource, resource); + deleteXResource(resource); + } + + explicit operator bool() const { return m_resource; } + +private: + static void deleteXResource(unsigned long resource); + + unsigned long m_resource { 0 }; +}; + +using XUniqueColormap = XUniqueResource<XResource::Colormap>; +#if PLATFORM(GTK) +using XUniqueDamage = XUniqueResource<XResource::Damage>; +#endif +using XUniquePixmap = XUniqueResource<XResource::Pixmap>; +using XUniqueWindow = XUniqueResource<XResource::Window>; +#if USE(GLX) +using XUniqueGLXPbuffer = XUniqueResource<XResource::GLXPbuffer>; +using XUniqueGLXPixmap = XUniqueResource<XResource::GLXPixmap>; +#endif + +} // namespace WebCore + +#endif // PLATFORM(X11) + +#endif // XUniqueResource_h |