Changeset 207615 in webkit


Ignore:
Timestamp:
Oct 20, 2016 5:37:53 AM (8 years ago)
Author:
Carlos Garcia Campos
Message:

[GTK] Avoid including egl.h headers in internal headers
https://bugs.webkit.org/show_bug.cgi?id=163722

Reviewed by Žan Doberšek.

egl.h includes eglplatform.h that decides the native types for the platform at compile time. However, we support
to build with X11 and Wayland at the same time and decide what to use at runtime. Currently GLContext.h includes
eglplatform.h after wayland-egl.h if Wayland is enabled. That means that the wayland native types are used by
default from all cpp files including GLContext.h. It currently works in X11 because we cast the value anyway and
for example EGLNativeWindowType is a pointer in Wayland that can be casted to unsigned long in X11 to represent
the X Window. This is very fragile in any case, we should avoid adding egl headers in our headers and only
include it in cpp files. But we also need to ensure we don't use X11 and Wayland in the same cpp file.

  • PlatformGTK.cmake:
  • platform/graphics/GLContext.cpp:

(WebCore::GLContext::createContextForWindow):

  • platform/graphics/GLContext.h:
  • platform/graphics/egl/GLContextEGL.cpp:

(WebCore::GLContextEGL::createWindowContext):
(WebCore::GLContextEGL::createContext):
(WebCore::GLContextEGL::~GLContextEGL):

  • platform/graphics/egl/GLContextEGL.h:
  • platform/graphics/egl/GLContextEGLWayland.cpp: Added.

(WebCore::GLContextEGL::GLContextEGL):
(WebCore::GLContextEGL::createWindowSurfaceWayland):
(WebCore::GLContextEGL::createWaylandContext):
(WebCore::GLContextEGL::destroyWaylandWindow):

  • platform/graphics/egl/GLContextEGLX11.cpp: Added.

(WebCore::GLContextEGL::GLContextEGL):
(WebCore::GLContextEGL::createWindowSurfaceX11):
(WebCore::GLContextEGL::createPixmapContext):

  • platform/graphics/glx/GLContextGLX.cpp:

(WebCore::GLContextGLX::createWindowContext):
(WebCore::GLContextGLX::createContext):
(WebCore::GLContextGLX::GLContextGLX):

  • platform/graphics/glx/GLContextGLX.h:
  • platform/graphics/wayland/PlatformDisplayWayland.cpp:
  • platform/graphics/x11/PlatformDisplayX11.cpp:
Location:
trunk/Source/WebCore
Files:
2 added
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r207614 r207615  
     12016-10-20  Carlos Garcia Campos  <cgarcia@igalia.com>
     2
     3        [GTK] Avoid including egl.h headers in internal headers
     4        https://bugs.webkit.org/show_bug.cgi?id=163722
     5
     6        Reviewed by Žan Doberšek.
     7
     8        egl.h includes eglplatform.h that decides the native types for the platform at compile time. However, we support
     9        to build with X11 and Wayland at the same time and decide what to use at runtime. Currently GLContext.h includes
     10        eglplatform.h after wayland-egl.h if Wayland is enabled. That means that the wayland native types are used by
     11        default from all cpp files including GLContext.h. It currently works in X11 because we cast the value anyway and
     12        for example EGLNativeWindowType is a pointer in Wayland that can be casted to unsigned long in X11 to represent
     13        the X Window. This is very fragile in any case, we should avoid adding egl headers in our headers and only
     14        include it in cpp files. But we also need to ensure we don't use X11 and Wayland in the same cpp file.
     15
     16        * PlatformGTK.cmake:
     17        * platform/graphics/GLContext.cpp:
     18        (WebCore::GLContext::createContextForWindow):
     19        * platform/graphics/GLContext.h:
     20        * platform/graphics/egl/GLContextEGL.cpp:
     21        (WebCore::GLContextEGL::createWindowContext):
     22        (WebCore::GLContextEGL::createContext):
     23        (WebCore::GLContextEGL::~GLContextEGL):
     24        * platform/graphics/egl/GLContextEGL.h:
     25        * platform/graphics/egl/GLContextEGLWayland.cpp: Added.
     26        (WebCore::GLContextEGL::GLContextEGL):
     27        (WebCore::GLContextEGL::createWindowSurfaceWayland):
     28        (WebCore::GLContextEGL::createWaylandContext):
     29        (WebCore::GLContextEGL::destroyWaylandWindow):
     30        * platform/graphics/egl/GLContextEGLX11.cpp: Added.
     31        (WebCore::GLContextEGL::GLContextEGL):
     32        (WebCore::GLContextEGL::createWindowSurfaceX11):
     33        (WebCore::GLContextEGL::createPixmapContext):
     34        * platform/graphics/glx/GLContextGLX.cpp:
     35        (WebCore::GLContextGLX::createWindowContext):
     36        (WebCore::GLContextGLX::createContext):
     37        (WebCore::GLContextGLX::GLContextGLX):
     38        * platform/graphics/glx/GLContextGLX.h:
     39        * platform/graphics/wayland/PlatformDisplayWayland.cpp:
     40        * platform/graphics/x11/PlatformDisplayX11.cpp:
     41
    1422016-10-20  Carlos Garcia Campos  <cgarcia@igalia.com>
    243
  • trunk/Source/WebCore/PlatformGTK.cmake

    r207590 r207615  
    127127
    128128    platform/graphics/egl/GLContextEGL.cpp
     129    platform/graphics/egl/GLContextEGLWayland.cpp
     130    platform/graphics/egl/GLContextEGLX11.cpp
    129131
    130132    platform/graphics/freetype/FontCacheFreeType.cpp
  • trunk/Source/WebCore/platform/graphics/GLContext.cpp

    r207614 r207615  
    8585
    8686#if USE(GLX)
    87 #if PLATFORM(WAYLAND) // Building both X11 and Wayland targets
    88     XID GLXWindowHandle = reinterpret_cast<XID>(windowHandle);
    89 #else
    90     XID GLXWindowHandle = static_cast<XID>(windowHandle);
    91 #endif
    92     if (auto glxContext = GLContextGLX::createContext(GLXWindowHandle, display))
     87    if (auto glxContext = GLContextGLX::createContext(windowHandle, display))
    9388        return WTFMove(glxContext);
    9489#endif
  • trunk/Source/WebCore/platform/graphics/GLContext.h

    r207614 r207615  
    2727#if USE(EGL) && !PLATFORM(GTK)
    2828#include "eglplatform.h"
    29 typedef EGLNativeWindowType GLNativeWindowType;
    30 #elif PLATFORM(GTK) && PLATFORM(WAYLAND) && !defined(GTK_API_VERSION_2)
    31 #include <wayland-egl.h>
    32 #include <EGL/eglplatform.h>
    3329typedef EGLNativeWindowType GLNativeWindowType;
    3430#else
  • trunk/Source/WebCore/platform/graphics/egl/GLContextEGL.cpp

    r207614 r207615  
    2323
    2424#include "GraphicsContext3D.h"
     25#include "PlatformDisplay.h"
     26#include <EGL/egl.h>
    2527
    2628#if USE(CAIRO)
     
    3335#else
    3436#include "OpenGLShims.h"
    35 #endif
    36 
    37 #if PLATFORM(X11)
    38 #include "PlatformDisplayX11.h"
    39 #include "XErrorTrapper.h"
    40 #include "XUniquePtr.h"
    41 #include <X11/Xlib.h>
    42 #endif
    43 
    44 #if PLATFORM(WAYLAND)
    45 #include "PlatformDisplayWayland.h"
    46 #include <wayland-egl.h>
    4737#endif
    4838
     
    10494}
    10595
    106 std::unique_ptr<GLContextEGL> GLContextEGL::createWindowContext(EGLNativeWindowType window, PlatformDisplay& platformDisplay, EGLContext sharingContext)
     96std::unique_ptr<GLContextEGL> GLContextEGL::createWindowContext(GLNativeWindowType window, PlatformDisplay& platformDisplay, EGLContext sharingContext)
    10797{
    10898    EGLDisplay display = platformDisplay.eglDisplay();
     
    115105        return nullptr;
    116106
    117     EGLSurface surface = eglCreateWindowSurface(display, config, window, 0);
     107    EGLSurface surface = EGL_NO_SURFACE;
     108#if PLATFORM(GTK)
     109#if PLATFORM(X11)
     110    if (platformDisplay.type() == PlatformDisplay::Type::X11)
     111        surface = createWindowSurfaceX11(display, config, window);
     112#endif
     113#if PLATFORM(WAYLAND)
     114    if (platformDisplay.type() == PlatformDisplay::Type::Wayland)
     115        surface = createWindowSurfaceWayland(display, config, window);
     116#endif
     117#else
     118    surface = eglCreateWindowSurface(display, config, static_cast<EGLNativeWindowType>(window), nullptr);
     119#endif
    118120    if (surface == EGL_NO_SURFACE) {
    119121        eglDestroyContext(display, context);
     
    166168}
    167169
    168 #if PLATFORM(X11)
    169 std::unique_ptr<GLContextEGL> GLContextEGL::createPixmapContext(PlatformDisplay& platformDisplay, EGLContext sharingContext)
    170 {
    171     EGLDisplay display = platformDisplay.eglDisplay();
    172     EGLConfig config;
    173     if (!getEGLConfig(display, &config, PixmapSurface))
    174         return nullptr;
    175 
    176     EGLContext context = eglCreateContext(display, config, sharingContext, gContextAttributes);
    177     if (context == EGL_NO_CONTEXT)
    178         return nullptr;
    179 
    180     EGLint visualId;
    181     if (!eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &visualId)) {
    182         eglDestroyContext(display, context);
    183         return nullptr;
    184     }
    185 
    186     Display* x11Display = downcast<PlatformDisplayX11>(platformDisplay).native();
    187 
    188     XVisualInfo visualInfo;
    189     visualInfo.visualid = visualId;
    190     int numVisuals = 0;
    191     XUniquePtr<XVisualInfo> visualInfoList(XGetVisualInfo(x11Display, VisualIDMask, &visualInfo, &numVisuals));
    192     if (!visualInfoList || !numVisuals) {
    193         eglDestroyContext(display, context);
    194         return nullptr;
    195     }
    196 
    197     // We are using VisualIDMask so there must be only one.
    198     ASSERT(numVisuals == 1);
    199     XUniquePixmap pixmap = XCreatePixmap(x11Display, DefaultRootWindow(x11Display), 1, 1, visualInfoList.get()[0].depth);
    200     if (!pixmap) {
    201         eglDestroyContext(display, context);
    202         return nullptr;
    203     }
    204 
    205     // Some drivers fail to create the surface producing BadDrawable X error and the default XError handler normally aborts.
    206     // However, if the X error is ignored, eglCreatePixmapSurface() ends up returning a surface and we can continue creating
    207     // the context. Since this is an offscreen context, it doesn't matter if the pixmap used is not valid because we never do
    208     // swap buffers. So, we use a custom XError handler here that ignores BadDrawable errors and only warns about any other
    209     // errors without aborting in any case.
    210     XErrorTrapper trapper(x11Display, XErrorTrapper::Policy::Warn, { BadDrawable });
    211     EGLSurface surface = eglCreatePixmapSurface(display, config, reinterpret_cast<EGLNativePixmapType>(pixmap.get()), 0);
    212     if (surface == EGL_NO_SURFACE) {
    213         eglDestroyContext(display, context);
    214         return nullptr;
    215     }
    216 
    217     return std::unique_ptr<GLContextEGL>(new GLContextEGL(platformDisplay, context, surface, WTFMove(pixmap)));
    218 }
    219 #endif // PLATFORM(X11)
    220 
    221 #if PLATFORM(WAYLAND)
    222 std::unique_ptr<GLContextEGL> GLContextEGL::createWaylandContext(PlatformDisplay& platformDisplay, EGLContext sharingContext)
    223 {
    224     EGLDisplay display = platformDisplay.eglDisplay();
    225     EGLConfig config;
    226     if (!getEGLConfig(display, &config, WindowSurface))
    227         return nullptr;
    228 
    229     EGLContext context = eglCreateContext(display, config, sharingContext, gContextAttributes);
    230     if (context == EGL_NO_CONTEXT)
    231         return nullptr;
    232 
    233     WlUniquePtr<struct wl_surface> wlSurface(downcast<PlatformDisplayWayland>(platformDisplay).createSurface());
    234     if (!wlSurface) {
    235         eglDestroyContext(display, context);
    236         return nullptr;
    237     }
    238 
    239     EGLNativeWindowType window = wl_egl_window_create(wlSurface.get(), 1, 1);
    240     EGLSurface surface = eglCreateWindowSurface(display, config, window, 0);
    241     if (surface == EGL_NO_SURFACE) {
    242         eglDestroyContext(display, context);
    243         wl_egl_window_destroy(window);
    244         return nullptr;
    245     }
    246 
    247     return std::unique_ptr<GLContextEGL>(new GLContextEGL(platformDisplay, context, surface, WTFMove(wlSurface), window));
    248 }
    249 #endif
    250 
    251 std::unique_ptr<GLContextEGL> GLContextEGL::createContext(EGLNativeWindowType window, PlatformDisplay& platformDisplay)
     170std::unique_ptr<GLContextEGL> GLContextEGL::createContext(GLNativeWindowType window, PlatformDisplay& platformDisplay)
    252171{
    253172    if (platformDisplay.eglDisplay() == EGL_NO_DISPLAY)
     
    312231}
    313232
    314 #if PLATFORM(X11)
    315 GLContextEGL::GLContextEGL(PlatformDisplay& display, EGLContext context, EGLSurface surface, XUniquePixmap&& pixmap)
    316     : GLContext(display)
    317     , m_context(context)
    318     , m_surface(surface)
    319     , m_type(PixmapSurface)
    320     , m_pixmap(WTFMove(pixmap))
    321 {
    322 }
    323 #endif
    324 
    325 #if PLATFORM(WAYLAND)
    326 GLContextEGL::GLContextEGL(PlatformDisplay& display, EGLContext context, EGLSurface surface, WlUniquePtr<struct wl_surface>&& wlSurface, EGLNativeWindowType wlWindow)
    327     : GLContext(display)
    328     , m_context(context)
    329     , m_surface(surface)
    330     , m_type(WindowSurface)
    331     , m_wlSurface(WTFMove(wlSurface))
    332     , m_wlWindow(wlWindow)
    333 {
    334 }
    335 #endif
    336 
    337233GLContextEGL::~GLContextEGL()
    338234{
     
    353249
    354250#if PLATFORM(WAYLAND)
    355     if (m_wlWindow)
    356         wl_egl_window_destroy(m_wlWindow);
     251    destroyWaylandWindow();
    357252#endif
    358253}
  • trunk/Source/WebCore/platform/graphics/egl/GLContextEGL.h

    r205116 r207615  
    1818 */
    1919
    20 #ifndef GLContextEGL_h
    21 #define GLContextEGL_h
     20#pragma once
    2221
    2322#if USE(EGL)
    2423
    2524#include "GLContext.h"
    26 #include <EGL/egl.h>
    2725
    2826#if PLATFORM(X11)
     
    3230#if PLATFORM(WAYLAND)
    3331#include "WlUniquePtr.h"
     32struct wl_egl_window;
    3433#endif
     34
     35typedef void *EGLConfig;
     36typedef void *EGLContext;
     37typedef void *EGLDisplay;
     38typedef void *EGLSurface;
    3539
    3640namespace WebCore {
     
    3943    WTF_MAKE_NONCOPYABLE(GLContextEGL);
    4044public:
    41     static std::unique_ptr<GLContextEGL> createContext(EGLNativeWindowType, PlatformDisplay&);
     45    static std::unique_ptr<GLContextEGL> createContext(GLNativeWindowType, PlatformDisplay&);
    4246    static std::unique_ptr<GLContextEGL> createSharingContext(PlatformDisplay&);
    4347
    4448    virtual ~GLContextEGL();
    4549
     50private:
    4651    bool makeContextCurrent() override;
    4752    void swapBuffers() override;
     
    5964#endif
    6065
    61 private:
    6266    enum EGLSurfaceType { PbufferSurface, WindowSurface, PixmapSurface, Surfaceless };
    6367
     
    6771#endif
    6872#if PLATFORM(WAYLAND)
    69     GLContextEGL(PlatformDisplay&, EGLContext, EGLSurface, WlUniquePtr<struct wl_surface>&&, EGLNativeWindowType);
     73    GLContextEGL(PlatformDisplay&, EGLContext, EGLSurface, WlUniquePtr<struct wl_surface>&&, struct wl_egl_window*);
     74    void destroyWaylandWindow();
    7075#endif
    7176
    72     static std::unique_ptr<GLContextEGL> createWindowContext(EGLNativeWindowType, PlatformDisplay&, EGLContext sharingContext = EGL_NO_CONTEXT);
    73     static std::unique_ptr<GLContextEGL> createPbufferContext(PlatformDisplay&, EGLContext sharingContext = EGL_NO_CONTEXT);
    74     static std::unique_ptr<GLContextEGL> createSurfacelessContext(PlatformDisplay&, EGLContext sharingContext = EGL_NO_CONTEXT);
     77    static std::unique_ptr<GLContextEGL> createWindowContext(GLNativeWindowType, PlatformDisplay&, EGLContext sharingContext = nullptr);
     78    static std::unique_ptr<GLContextEGL> createPbufferContext(PlatformDisplay&, EGLContext sharingContext = nullptr);
     79    static std::unique_ptr<GLContextEGL> createSurfacelessContext(PlatformDisplay&, EGLContext sharingContext = nullptr);
    7580#if PLATFORM(X11)
    76     static std::unique_ptr<GLContextEGL> createPixmapContext(PlatformDisplay&, EGLContext sharingContext = EGL_NO_CONTEXT);
     81    static std::unique_ptr<GLContextEGL> createPixmapContext(PlatformDisplay&, EGLContext sharingContext = nullptr);
     82    static EGLSurface createWindowSurfaceX11(EGLDisplay, EGLConfig, GLNativeWindowType);
    7783#endif
    7884#if PLATFORM(WAYLAND)
    79     static std::unique_ptr<GLContextEGL> createWaylandContext(PlatformDisplay&, EGLContext sharingContext = EGL_NO_CONTEXT);
     85    static std::unique_ptr<GLContextEGL> createWaylandContext(PlatformDisplay&, EGLContext sharingContext = nullptr);
     86    static EGLSurface createWindowSurfaceWayland(EGLDisplay, EGLConfig, GLNativeWindowType);
    8087#endif
    8188
    8289    static bool getEGLConfig(EGLDisplay, EGLConfig*, EGLSurfaceType);
    8390
    84     EGLContext m_context { EGL_NO_CONTEXT };
    85     EGLSurface m_surface { EGL_NO_SURFACE };
     91    EGLContext m_context { nullptr };
     92    EGLSurface m_surface { nullptr };
    8693    EGLSurfaceType m_type;
    8794#if PLATFORM(X11)
     
    100107
    101108#endif // USE(EGL)
    102 
    103 #endif // GLContextEGL_h
  • trunk/Source/WebCore/platform/graphics/glx/GLContextGLX.cpp

    r207614 r207615  
    5353}
    5454
    55 std::unique_ptr<GLContextGLX> GLContextGLX::createWindowContext(XID window, PlatformDisplay& platformDisplay, GLXContext sharingContext)
     55std::unique_ptr<GLContextGLX> GLContextGLX::createWindowContext(GLNativeWindowType window, PlatformDisplay& platformDisplay, GLXContext sharingContext)
    5656{
    5757    Display* display = downcast<PlatformDisplayX11>(platformDisplay).native();
    5858    XWindowAttributes attributes;
    59     if (!XGetWindowAttributes(display, window, &attributes))
     59    if (!XGetWindowAttributes(display, static_cast<Window>(window), &attributes))
    6060        return nullptr;
    6161
     
    136136}
    137137
    138 std::unique_ptr<GLContextGLX> GLContextGLX::createContext(XID window, PlatformDisplay& platformDisplay)
     138std::unique_ptr<GLContextGLX> GLContextGLX::createContext(GLNativeWindowType window, PlatformDisplay& platformDisplay)
    139139{
    140140    GLXContext glxSharingContext = platformDisplay.sharingGLContext() ? static_cast<GLContextGLX*>(platformDisplay.sharingGLContext())->m_context.get() : nullptr;
     
    156156}
    157157
    158 GLContextGLX::GLContextGLX(PlatformDisplay& display, XUniqueGLXContext&& context, XID window)
     158GLContextGLX::GLContextGLX(PlatformDisplay& display, XUniqueGLXContext&& context, GLNativeWindowType window)
    159159    : GLContext(display)
    160160    , m_x11Display(downcast<PlatformDisplayX11>(m_display).native())
    161161    , m_context(WTFMove(context))
    162     , m_window(window)
     162    , m_window(static_cast<Window>(window))
    163163{
    164164}
  • trunk/Source/WebCore/platform/graphics/glx/GLContextGLX.h

    r205852 r207615  
    1818 */
    1919
    20 #ifndef GLContextGLX_h
    21 #define GLContextGLX_h
     20#pragma once
    2221
    2322#if USE(GLX)
     
    2827
    2928typedef unsigned char GLubyte;
    30 typedef unsigned long XID;
     29typedef unsigned long Window;
    3130typedef void* ContextKeyType;
    3231typedef struct _XDisplay Display;
     
    3736    WTF_MAKE_NONCOPYABLE(GLContextGLX);
    3837public:
    39     static std::unique_ptr<GLContextGLX> createContext(XID window, PlatformDisplay&);
     38    static std::unique_ptr<GLContextGLX> createContext(GLNativeWindowType, PlatformDisplay&);
    4039    static std::unique_ptr<GLContextGLX> createSharingContext(PlatformDisplay&);
    4140
    4241    virtual ~GLContextGLX();
    4342
     43private:
    4444    bool makeContextCurrent() override;
    4545    void swapBuffers() override;
     
    5555#endif
    5656
    57 private:
    58     GLContextGLX(PlatformDisplay&, XUniqueGLXContext&&, XID);
     57    GLContextGLX(PlatformDisplay&, XUniqueGLXContext&&, GLNativeWindowType);
    5958    GLContextGLX(PlatformDisplay&, XUniqueGLXContext&&, XUniqueGLXPbuffer&&);
    6059    GLContextGLX(PlatformDisplay&, XUniqueGLXContext&&, XUniquePixmap&&, XUniqueGLXPixmap&&);
    6160
    62     static std::unique_ptr<GLContextGLX> createWindowContext(XID window, PlatformDisplay&, GLXContext sharingContext = nullptr);
     61    static std::unique_ptr<GLContextGLX> createWindowContext(GLNativeWindowType, PlatformDisplay&, GLXContext sharingContext = nullptr);
    6362    static std::unique_ptr<GLContextGLX> createPbufferContext(PlatformDisplay&, GLXContext sharingContext = nullptr);
    6463    static std::unique_ptr<GLContextGLX> createPixmapContext(PlatformDisplay&, GLXContext sharingContext = nullptr);
     
    6665    Display* m_x11Display { nullptr };
    6766    XUniqueGLXContext m_context;
    68     XID m_window { 0 };
     67    Window m_window { 0 };
    6968    XUniqueGLXPbuffer m_pbuffer;
    7069    XUniquePixmap m_pixmap;
     
    7776#endif // USE(GLX)
    7877
    79 #endif // GLContextGLX_h
  • trunk/Source/WebCore/platform/graphics/wayland/PlatformDisplayWayland.cpp

    r205116 r207615  
    3131#include "GLContextEGL.h"
    3232#include <cstring>
     33// These includes need to be in this order because wayland-egl.h defines WL_EGL_PLATFORM
     34// and egl.h checks that to decide whether it's Wayland platform.
    3335#include <wayland-egl.h>
     36#include <EGL/egl.h>
    3437#include <wtf/Assertions.h>
    3538
  • trunk/Source/WebCore/platform/graphics/x11/PlatformDisplayX11.cpp

    r207403 r207615  
    2727#include "PlatformDisplayX11.h"
    2828
     29#include "GLContext.h"
     30
    2931#if PLATFORM(X11)
    3032#include <X11/Xlib.h>
     
    3638#if USE(EGL)
    3739#include <EGL/egl.h>
    38 #include <EGL/eglplatform.h>
    3940#endif
    40 
    41 // FIXME: this needs to be here, after eglplatform.h, to avoid EGLNativeDisplayType to be defined as wl_display.
    42 // Since we support Wayland and X11 to be built at the same time, but eglplatform.h defines are decided at compile time
    43 // we need to ensure we only include eglplatform.h from X11 or Wayland specific files.
    44 #include "GLContext.h"
    4541
    4642namespace WebCore {
Note: See TracChangeset for help on using the changeset viewer.