2
* Copyright (C) 2018 Metrological Group B.V.
3
* Copyright (C) 2018, 2019 Igalia S.L.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* 1. Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* 2. Redistributions in binary form must reproduce the above
12
* copyright notice, this list of conditions and the following
13
* disclaimer in the documentation and/or other materials provided
14
* with the distribution.
16
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
#include "NicosiaGC3DANGLELayer.h"
32
#if USE(NICOSIA) && USE(TEXTURE_MAPPER)
36
#define EGL_EGL_PROTOTYPES 0
37
// Skip the inclusion of ANGLE's explicit context entry points for now.
38
#define GL_ANGLE_explicit_context
39
#define GL_ANGLE_explicit_context_gles1
40
typedef void* GLeglContext;
41
#include <ANGLE/egl.h>
42
#include <ANGLE/eglext.h>
43
#include <ANGLE/eglext_angle.h>
44
#include <ANGLE/entry_points_egl.h>
45
#include <ANGLE/entry_points_gles_2_0_autogen.h>
46
#include <ANGLE/entry_points_gles_ext_autogen.h>
47
#include <ANGLE/gl2ext.h>
48
#include <ANGLE/gl2ext_angle.h>
52
using namespace WebCore;
54
const char* GC3DANGLELayer::ANGLEContext::errorString(int statusCode)
56
static_assert(sizeof(int) >= sizeof(EGLint), "EGLint must not be wider than int");
58
#define CASE_RETURN_STRING(name) case name: return #name
59
// https://www.khronos.org/registry/EGL/sdk/docs/man/html/eglGetError.xhtml
60
CASE_RETURN_STRING(EGL_SUCCESS);
61
CASE_RETURN_STRING(EGL_NOT_INITIALIZED);
62
CASE_RETURN_STRING(EGL_BAD_ACCESS);
63
CASE_RETURN_STRING(EGL_BAD_ALLOC);
64
CASE_RETURN_STRING(EGL_BAD_ATTRIBUTE);
65
CASE_RETURN_STRING(EGL_BAD_CONTEXT);
66
CASE_RETURN_STRING(EGL_BAD_CONFIG);
67
CASE_RETURN_STRING(EGL_BAD_CURRENT_SURFACE);
68
CASE_RETURN_STRING(EGL_BAD_DISPLAY);
69
CASE_RETURN_STRING(EGL_BAD_SURFACE);
70
CASE_RETURN_STRING(EGL_BAD_MATCH);
71
CASE_RETURN_STRING(EGL_BAD_PARAMETER);
72
CASE_RETURN_STRING(EGL_BAD_NATIVE_PIXMAP);
73
CASE_RETURN_STRING(EGL_BAD_NATIVE_WINDOW);
74
CASE_RETURN_STRING(EGL_CONTEXT_LOST);
75
#undef CASE_RETURN_STRING
76
default: return "Unknown EGL error";
80
const char* GC3DANGLELayer::ANGLEContext::lastErrorString()
82
return errorString(EGL_GetError());
85
std::unique_ptr<GC3DANGLELayer::ANGLEContext> GC3DANGLELayer::ANGLEContext::createContext()
87
EGLDisplay display = EGL_GetDisplay(EGL_DEFAULT_DISPLAY);
88
if (display == EGL_NO_DISPLAY)
91
EGLint majorVersion, minorVersion;
92
if (EGL_Initialize(display, &majorVersion, &minorVersion) == EGL_FALSE) {
93
LOG(WebGL, "EGLDisplay Initialization failed.");
96
LOG(WebGL, "ANGLE initialised Major: %d Minor: %d", majorVersion, minorVersion);
98
const char* displayExtensions = EGL_QueryString(display, EGL_EXTENSIONS);
99
LOG(WebGL, "Extensions: %s", displayExtensions);
102
EGLint configAttributes[] = {
103
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
110
EGLint numberConfigsReturned = 0;
111
EGL_ChooseConfig(display, configAttributes, &config, 1, &numberConfigsReturned);
112
if (numberConfigsReturned != 1) {
113
LOG(WebGL, "EGLConfig Initialization failed.");
116
LOG(WebGL, "Got EGLConfig");
118
EGL_BindAPI(EGL_OPENGL_ES_API);
119
if (EGL_GetError() != EGL_SUCCESS) {
120
LOG(WebGL, "Unable to bind to OPENGL_ES_API");
124
std::vector<EGLint> contextAttributes;
125
contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
126
contextAttributes.push_back(2);
127
contextAttributes.push_back(EGL_CONTEXT_WEBGL_COMPATIBILITY_ANGLE);
128
contextAttributes.push_back(EGL_TRUE);
129
contextAttributes.push_back(EGL_EXTENSIONS_ENABLED_ANGLE);
130
contextAttributes.push_back(EGL_TRUE);
131
if (strstr(displayExtensions, "EGL_ANGLE_power_preference")) {
132
contextAttributes.push_back(EGL_POWER_PREFERENCE_ANGLE);
133
// EGL_LOW_POWER_ANGLE is the default. Change to
134
// EGL_HIGH_POWER_ANGLE if desired.
135
contextAttributes.push_back(EGL_LOW_POWER_ANGLE);
137
contextAttributes.push_back(EGL_NONE);
139
EGLContext context = EGL_CreateContext(display, config, EGL_NO_CONTEXT, contextAttributes.data());
140
if (context == EGL_NO_CONTEXT) {
141
LOG(WebGL, "EGLContext Initialization failed.");
144
LOG(WebGL, "Got EGLContext");
146
return std::unique_ptr<ANGLEContext>(new ANGLEContext(display, context, EGL_NO_SURFACE));
149
GC3DANGLELayer::ANGLEContext::ANGLEContext(EGLDisplay display, EGLContext context, EGLSurface surface)
156
GC3DANGLELayer::ANGLEContext::~ANGLEContext()
159
gl::BindFramebuffer(GL_FRAMEBUFFER, 0);
160
EGL_MakeCurrent(m_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
161
EGL_DestroyContext(m_display, m_context);
165
EGL_DestroySurface(m_display, m_surface);
168
bool GC3DANGLELayer::ANGLEContext::makeContextCurrent()
172
if (EGL_GetCurrentContext() != m_context)
173
return EGL_MakeCurrent(m_display, m_surface, m_surface, m_context);
177
PlatformGraphicsContextGL GC3DANGLELayer::ANGLEContext::platformContext() const
182
GC3DANGLELayer::GC3DANGLELayer(GraphicsContextGLOpenGL& context, GraphicsContextGLOpenGL::Destination destination)
185
switch (destination) {
186
case GraphicsContextGLOpenGL::Destination::Offscreen:
187
m_angleContext = ANGLEContext::createContext();
189
case GraphicsContextGLOpenGL::Destination::DirectlyToHostWindow:
190
ASSERT_NOT_REACHED();
195
GC3DANGLELayer::~GC3DANGLELayer()
199
bool GC3DANGLELayer::makeContextCurrent()
201
ASSERT(m_angleContext);
202
return m_angleContext->makeContextCurrent();
206
PlatformGraphicsContextGL GC3DANGLELayer::platformContext() const
208
ASSERT(m_angleContext);
209
return m_angleContext->platformContext();
212
} // namespace Nicosia
214
#endif // USE(NICOSIA) && USE(TEXTURE_MAPPER)