[WPE] libepoxy headers can use EGL_CAST, which might not be defined by eglplatform.h
[WebKit-https.git] / Source / WebCore / platform / graphics / egl / GLContextEGLWPE.cpp
1 /*
2  * Copyright (C) 2017 Igalia, S.L.
3  *
4  *  This library is free software; you can redistribute it and/or
5  *  modify it under the terms of the GNU Lesser General Public
6  *  License as published by the Free Software Foundation; either
7  *  version 2 of the License, or (at your option) any later version.
8  *
9  *  This library is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *  Lesser General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Lesser General Public
15  *  License along with this library; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include "config.h"
20 #include "GLContextEGL.h"
21
22 #if USE(EGL) && PLATFORM(WPE)
23
24 #include "PlatformDisplayWPE.h"
25 // FIXME: For now default to the GBM EGL platform, but this should really be
26 // somehow deducible from the build configuration.
27 #define __GBM__ 1
28 #if USE(LIBEPOXY)
29 #include "EpoxyEGL.h"
30 #else
31 #include <EGL/egl.h>
32 #endif
33 #include <wpe/renderer-backend-egl.h>
34
35 namespace WebCore {
36
37 GLContextEGL::GLContextEGL(PlatformDisplay& display, EGLContext context, EGLSurface surface, struct wpe_renderer_backend_egl_offscreen_target* target)
38     : GLContext(display)
39     , m_context(context)
40     , m_surface(surface)
41     , m_type(WindowSurface)
42     , m_wpeTarget(target)
43 {
44 }
45
46 EGLSurface GLContextEGL::createWindowSurfaceWPE(EGLDisplay display, EGLConfig config, GLNativeWindowType window)
47 {
48     return eglCreateWindowSurface(display, config, reinterpret_cast<EGLNativeWindowType>(window), nullptr);
49 }
50
51 std::unique_ptr<GLContextEGL> GLContextEGL::createWPEContext(PlatformDisplay& platformDisplay, EGLContext sharingContext)
52 {
53     EGLDisplay display = platformDisplay.eglDisplay();
54     EGLConfig config;
55     if (!getEGLConfig(display, &config, WindowSurface))
56         return nullptr;
57
58     static const EGLint contextAttributes[] = {
59 #if USE(OPENGL_ES_2)
60         EGL_CONTEXT_CLIENT_VERSION, 2,
61 #endif
62         EGL_NONE
63     };
64
65     EGLContext context = eglCreateContext(display, config, sharingContext, contextAttributes);
66     if (context == EGL_NO_CONTEXT)
67         return nullptr;
68
69     auto* target = wpe_renderer_backend_egl_offscreen_target_create();
70     wpe_renderer_backend_egl_offscreen_target_initialize(target, downcast<PlatformDisplayWPE>(platformDisplay).backend());
71     EGLNativeWindowType window = wpe_renderer_backend_egl_offscreen_target_get_native_window(target);
72     if (!window) {
73         wpe_renderer_backend_egl_offscreen_target_destroy(target);
74         return nullptr;
75     }
76
77     EGLSurface surface = eglCreateWindowSurface(display, config, static_cast<EGLNativeWindowType>(window), nullptr);
78     if (surface == EGL_NO_SURFACE) {
79         eglDestroyContext(display, context);
80         wpe_renderer_backend_egl_offscreen_target_destroy(target);
81         return nullptr;
82     }
83
84     return std::unique_ptr<GLContextEGL>(new GLContextEGL(platformDisplay, context, surface, target));
85 }
86
87 void GLContextEGL::destroyWPETarget()
88 {
89     if (m_wpeTarget)
90         wpe_renderer_backend_egl_offscreen_target_destroy(m_wpeTarget);
91 }
92
93 } // namespace WebCore
94
95 #endif // USE(EGL) && PLATFORM(WPE)