forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLContext.cpp
640 lines (560 loc) · 22.9 KB
/
GLContext.cpp
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*
* Copyright (C) 2012,2023 Igalia, S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "GLContext.h"
#if USE(EGL)
#include "GraphicsContextGL.h"
#include "Logging.h"
#include <wtf/ThreadSpecific.h>
#include <wtf/Vector.h>
#include <wtf/text/StringToIntegerConversion.h>
#if USE(LIBEPOXY)
#include "EpoxyEGL.h"
#else
#include <EGL/egl.h>
#include <EGL/eglext.h>
#endif
#if USE(LIBEPOXY)
#include <epoxy/gl.h>
#elif USE(OPENGL_ES)
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#else
#include "OpenGLShims.h"
#endif
#if PLATFORM(X11)
#include "PlatformDisplayX11.h"
#include "XUniquePtr.h"
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#endif
namespace WebCore {
static ThreadSpecific<GLContext*>& currentContext()
{
static ThreadSpecific<GLContext*>* context;
static std::once_flag flag;
std::call_once(flag, [] {
context = new ThreadSpecific<GLContext*>();
});
return *context;
}
static bool initializeOpenGLShimsIfNeeded()
{
#if USE(OPENGL_ES) || USE(LIBEPOXY) || USE(ANGLE)
return true;
#else
static bool initialized = false;
static bool success = true;
if (!initialized) {
success = initializeOpenGLShims();
initialized = true;
}
return success;
#endif
}
#if USE(OPENGL_ES)
static const char* gEGLAPIName = "OpenGL ES";
static const EGLenum gEGLAPIVersion = EGL_OPENGL_ES_API;
#else
static const char* gEGLAPIName = "OpenGL";
static const EGLenum gEGLAPIVersion = EGL_OPENGL_API;
#endif
const char* GLContext::errorString(int statusCode)
{
static_assert(sizeof(int) >= sizeof(EGLint), "EGLint must not be wider than int");
switch (statusCode) {
#define CASE_RETURN_STRING(name) case name: return #name
// https://www.khronos.org/registry/EGL/sdk/docs/man/html/eglGetError.xhtml
CASE_RETURN_STRING(EGL_SUCCESS);
CASE_RETURN_STRING(EGL_NOT_INITIALIZED);
CASE_RETURN_STRING(EGL_BAD_ACCESS);
CASE_RETURN_STRING(EGL_BAD_ALLOC);
CASE_RETURN_STRING(EGL_BAD_ATTRIBUTE);
CASE_RETURN_STRING(EGL_BAD_CONTEXT);
CASE_RETURN_STRING(EGL_BAD_CONFIG);
CASE_RETURN_STRING(EGL_BAD_CURRENT_SURFACE);
CASE_RETURN_STRING(EGL_BAD_DISPLAY);
CASE_RETURN_STRING(EGL_BAD_SURFACE);
CASE_RETURN_STRING(EGL_BAD_MATCH);
CASE_RETURN_STRING(EGL_BAD_PARAMETER);
CASE_RETURN_STRING(EGL_BAD_NATIVE_PIXMAP);
CASE_RETURN_STRING(EGL_BAD_NATIVE_WINDOW);
CASE_RETURN_STRING(EGL_CONTEXT_LOST);
#undef CASE_RETURN_STRING
default: return "Unknown EGL error";
}
}
const char* GLContext::lastErrorString()
{
return errorString(eglGetError());
}
bool GLContext::getEGLConfig(PlatformDisplay& platformDisplay, EGLConfig* config, EGLSurfaceType surfaceType, Function<bool(int)>&& checkCompatibleVisuals)
{
#if !PLATFORM(X11)
UNUSED_PARAM(checkCompatibleVisuals);
#endif
std::array<EGLint, 4> rgbaSize = { 8, 8, 8, 8 };
if (const char* environmentVariable = getenv("WEBKIT_EGL_PIXEL_LAYOUT")) {
if (!strcmp(environmentVariable, "RGB565"))
rgbaSize = { 5, 6, 5, 0 };
else
WTFLogAlways("Unknown pixel layout %s, falling back to RGBA8888", environmentVariable);
}
EGLint attributeList[] = {
#if USE(OPENGL_ES)
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
#else
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
#endif
EGL_RED_SIZE, rgbaSize[0],
EGL_GREEN_SIZE, rgbaSize[1],
EGL_BLUE_SIZE, rgbaSize[2],
EGL_ALPHA_SIZE, rgbaSize[3],
EGL_STENCIL_SIZE, 8,
EGL_SURFACE_TYPE, EGL_NONE,
EGL_DEPTH_SIZE, 0,
EGL_NONE
};
switch (surfaceType) {
case GLContext::Surfaceless:
if (platformDisplay.type() == PlatformDisplay::Type::Surfaceless)
attributeList[13] = EGL_PBUFFER_BIT;
else
attributeList[13] = EGL_WINDOW_BIT;
break;
case GLContext::PbufferSurface:
attributeList[13] = EGL_PBUFFER_BIT;
break;
case GLContext::PixmapSurface:
attributeList[13] = EGL_PIXMAP_BIT;
break;
case GLContext::WindowSurface:
attributeList[13] = EGL_WINDOW_BIT;
break;
}
EGLDisplay display = platformDisplay.eglDisplay();
EGLint count;
if (!eglChooseConfig(display, attributeList, nullptr, 0, &count)) {
RELEASE_LOG_INFO(Compositing, "Cannot get count of available EGL configurations: %s.", lastErrorString());
return false;
}
EGLint numberConfigsReturned;
Vector<EGLConfig> configs(count);
if (!eglChooseConfig(display, attributeList, reinterpret_cast<EGLConfig*>(configs.data()), count, &numberConfigsReturned) || !numberConfigsReturned) {
RELEASE_LOG_INFO(Compositing, "Cannot get available EGL configurations: %s.", lastErrorString());
return false;
}
auto index = configs.findIf([&](EGLConfig value) {
EGLint redSize, greenSize, blueSize, alphaSize;
eglGetConfigAttrib(display, value, EGL_RED_SIZE, &redSize);
eglGetConfigAttrib(display, value, EGL_GREEN_SIZE, &greenSize);
eglGetConfigAttrib(display, value, EGL_BLUE_SIZE, &blueSize);
eglGetConfigAttrib(display, value, EGL_ALPHA_SIZE, &alphaSize);
#if PLATFORM(X11)
if (checkCompatibleVisuals) {
EGLint visualid;
if (!eglGetConfigAttrib(display, value, EGL_NATIVE_VISUAL_ID, &visualid))
return false;
if (!checkCompatibleVisuals(visualid))
return false;
}
#endif
return redSize == rgbaSize[0] && greenSize == rgbaSize[1]
&& blueSize == rgbaSize[2] && alphaSize == rgbaSize[3];
});
if (index != notFound) {
*config = configs[index];
return true;
}
RELEASE_LOG_INFO(Compositing, "Could not find suitable EGL configuration out of %zu checked.", configs.size());
return false;
}
std::unique_ptr<GLContext> GLContext::createWindowContext(GLNativeWindowType window, PlatformDisplay& platformDisplay, EGLContext sharingContext)
{
Function<bool(int)> checkCompatibleVisuals = nullptr;
#if PLATFORM(X11)
if (platformDisplay.type() == PlatformDisplay::Type::X11) {
Display* x11Display = downcast<PlatformDisplayX11>(platformDisplay).native();
XWindowAttributes attributes;
if (!XGetWindowAttributes(x11Display, static_cast<Window>(window), &attributes))
return nullptr;
auto visualInfoForID = [x11Display](VisualID visualID) -> XUniquePtr<XVisualInfo> {
XVisualInfo templateVisualInfo;
templateVisualInfo.visualid = visualID;
int visualInfoCount;
return XUniquePtr<XVisualInfo>(XGetVisualInfo(x11Display, VisualIDMask, &templateVisualInfo, &visualInfoCount));
};
XUniquePtr<XVisualInfo> visualInfo(visualInfoForID(XVisualIDFromVisual(attributes.visual)));
if (!visualInfo)
return nullptr;
checkCompatibleVisuals = [visualInfo = WTFMove(visualInfo), visualInfoForID = WTFMove(visualInfoForID)](unsigned long configVisualID) {
auto configVisualInfo = visualInfoForID(configVisualID);
return configVisualInfo
&& visualInfo->c_class == configVisualInfo->c_class
&& visualInfo->depth >= configVisualInfo->depth
&& visualInfo->red_mask == configVisualInfo->red_mask
&& visualInfo->green_mask == configVisualInfo->green_mask
&& visualInfo->blue_mask == configVisualInfo->blue_mask
&& visualInfo->colormap_size == configVisualInfo->colormap_size
&& visualInfo->bits_per_rgb == configVisualInfo->bits_per_rgb;
};
}
#endif
EGLConfig config;
if (!getEGLConfig(platformDisplay, &config, WindowSurface, WTFMove(checkCompatibleVisuals))) {
RELEASE_LOG_INFO(Compositing, "Cannot obtain EGL window context configuration: %s\n", lastErrorString());
return nullptr;
}
EGLContext context = createContextForEGLVersion(platformDisplay, config, sharingContext);
if (context == EGL_NO_CONTEXT) {
RELEASE_LOG_INFO(Compositing, "Cannot create EGL window context: %s\n", lastErrorString());
return nullptr;
}
EGLDisplay display = platformDisplay.eglDisplay();
EGLSurface surface = EGL_NO_SURFACE;
switch (platformDisplay.type()) {
#if PLATFORM(X11)
case PlatformDisplay::Type::X11:
surface = createWindowSurfaceX11(display, config, window);
break;
#endif
#if PLATFORM(WAYLAND)
case PlatformDisplay::Type::Wayland:
surface = createWindowSurfaceWayland(display, config, window);
break;
#endif
#if USE(WPE_RENDERER)
case PlatformDisplay::Type::WPE:
surface = createWindowSurfaceWPE(display, config, window);
break;
#endif // USE(WPE_RENDERER)
#if USE(GBM)
case PlatformDisplay::Type::GBM:
#endif
case PlatformDisplay::Type::Surfaceless:
RELEASE_ASSERT_NOT_REACHED();
}
if (surface == EGL_NO_SURFACE) {
RELEASE_LOG_INFO(Compositing, "Cannot create EGL window surface: %s. Retrying with fallback.", lastErrorString());
surface = eglCreateWindowSurface(display, config, static_cast<EGLNativeWindowType>(window), nullptr);
}
if (surface == EGL_NO_SURFACE) {
RELEASE_LOG_INFO(Compositing, "Cannot create EGL window surface: %s\n", lastErrorString());
eglDestroyContext(display, context);
return nullptr;
}
return makeUnique<GLContext>(platformDisplay, context, surface, config, WindowSurface);
}
std::unique_ptr<GLContext> GLContext::createPbufferContext(PlatformDisplay& platformDisplay, EGLContext sharingContext)
{
EGLConfig config;
if (!getEGLConfig(platformDisplay, &config, PbufferSurface)) {
RELEASE_LOG_INFO(Compositing, "Cannot obtain EGL Pbuffer configuration: %s\n", lastErrorString());
return nullptr;
}
EGLContext context = createContextForEGLVersion(platformDisplay, config, sharingContext);
if (context == EGL_NO_CONTEXT) {
RELEASE_LOG_INFO(Compositing, "Cannot create EGL Pbuffer context: %s\n", lastErrorString());
return nullptr;
}
EGLDisplay display = platformDisplay.eglDisplay();
static const int pbufferAttributes[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE };
EGLSurface surface = eglCreatePbufferSurface(display, config, pbufferAttributes);
if (surface == EGL_NO_SURFACE) {
RELEASE_LOG_INFO(Compositing, "Cannot create EGL Pbuffer surface: %s\n", lastErrorString());
eglDestroyContext(display, context);
return nullptr;
}
return makeUnique<GLContext>(platformDisplay, context, surface, config, PbufferSurface);
}
std::unique_ptr<GLContext> GLContext::createSurfacelessContext(PlatformDisplay& platformDisplay, EGLContext sharingContext)
{
EGLDisplay display = platformDisplay.eglDisplay();
if (display == EGL_NO_DISPLAY) {
RELEASE_LOG_INFO(Compositing, "Cannot create surfaceless EGL context: invalid display (last error: %s)\n", lastErrorString());
return nullptr;
}
const char* extensions = eglQueryString(display, EGL_EXTENSIONS);
if (!GLContext::isExtensionSupported(extensions, "EGL_KHR_surfaceless_context") && !GLContext::isExtensionSupported(extensions, "EGL_KHR_surfaceless_opengl")) {
RELEASE_LOG_INFO(Compositing, "Cannot create surfaceless EGL context: required extensions missing.");
return nullptr;
}
EGLConfig config;
if (!getEGLConfig(platformDisplay, &config, Surfaceless)) {
RELEASE_LOG_INFO(Compositing, "Cannot obtain EGL surfaceless configuration: %s\n", lastErrorString());
return nullptr;
}
EGLContext context = createContextForEGLVersion(platformDisplay, config, sharingContext);
if (context == EGL_NO_CONTEXT) {
RELEASE_LOG_INFO(Compositing, "Cannot create EGL surfaceless context: %s\n", lastErrorString());
return nullptr;
}
return makeUnique<GLContext>(platformDisplay, context, EGL_NO_SURFACE, config, Surfaceless);
}
std::unique_ptr<GLContext> GLContext::create(GLNativeWindowType window, PlatformDisplay& platformDisplay)
{
if (!initializeOpenGLShimsIfNeeded())
return nullptr;
if (platformDisplay.eglDisplay() == EGL_NO_DISPLAY) {
WTFLogAlways("Cannot create EGL context: invalid display (last error: %s)\n", lastErrorString());
return nullptr;
}
if (eglBindAPI(gEGLAPIVersion) == EGL_FALSE) {
WTFLogAlways("Cannot create EGL context: error binding %s API (%s)\n", gEGLAPIName, lastErrorString());
return nullptr;
}
EGLContext eglSharingContext = platformDisplay.sharingGLContext() ? static_cast<GLContext*>(platformDisplay.sharingGLContext())->m_context : EGL_NO_CONTEXT;
if (platformDisplay.type() == PlatformDisplay::Type::Surfaceless) {
auto context = createSurfacelessContext(platformDisplay, eglSharingContext);
if (!context)
WTFLogAlways("Could not create EGL surfaceless context: %s.", lastErrorString());
return context;
}
auto context = window ? createWindowContext(window, platformDisplay, eglSharingContext) : nullptr;
if (!context)
context = createSurfacelessContext(platformDisplay, eglSharingContext);
if (!context) {
switch (platformDisplay.type()) {
#if PLATFORM(X11)
case PlatformDisplay::Type::X11:
context = createPixmapContext(platformDisplay, eglSharingContext);
break;
#endif
#if PLATFORM(WAYLAND)
case PlatformDisplay::Type::Wayland:
context = createWaylandContext(platformDisplay, eglSharingContext);
break;
#endif
#if USE(WPE_RENDERER)
case PlatformDisplay::Type::WPE:
context = createWPEContext(platformDisplay, eglSharingContext);
break;
#endif
#if USE(GBM)
case PlatformDisplay::Type::GBM:
#endif
case PlatformDisplay::Type::Surfaceless:
RELEASE_ASSERT_NOT_REACHED();
}
}
if (!context) {
RELEASE_LOG_INFO(Compositing, "Could not create platform context: %s. Using Pbuffer as fallback.", lastErrorString());
context = createPbufferContext(platformDisplay, eglSharingContext);
if (!context)
RELEASE_LOG_INFO(Compositing, "Could not create Pbuffer context: %s.", lastErrorString());
}
if (!context)
WTFLogAlways("Could not create EGL context.");
return context;
}
std::unique_ptr<GLContext> GLContext::createOffscreen(PlatformDisplay& platformDisplay)
{
if (!initializeOpenGLShimsIfNeeded())
return nullptr;
return create(0, platformDisplay);
}
std::unique_ptr<GLContext> GLContext::createSharing(PlatformDisplay& platformDisplay)
{
if (!initializeOpenGLShimsIfNeeded())
return nullptr;
if (platformDisplay.eglDisplay() == EGL_NO_DISPLAY) {
WTFLogAlways("Cannot create EGL sharing context: invalid display (last error: %s)", lastErrorString());
return nullptr;
}
if (eglBindAPI(gEGLAPIVersion) == EGL_FALSE) {
WTFLogAlways("Cannot create EGL sharing context: error binding %s API (%s)\n", gEGLAPIName, lastErrorString());
return nullptr;
}
auto context = createSurfacelessContext(platformDisplay);
if (!context) {
switch (platformDisplay.type()) {
#if PLATFORM(X11)
case PlatformDisplay::Type::X11:
context = createPixmapContext(platformDisplay);
break;
#endif
#if PLATFORM(WAYLAND)
case PlatformDisplay::Type::Wayland:
context = createWaylandContext(platformDisplay);
break;
#endif
#if USE(WPE_RENDERER)
case PlatformDisplay::Type::WPE:
context = createWPEContext(platformDisplay);
break;
#endif
#if USE(GBM)
case PlatformDisplay::Type::GBM:
#endif
case PlatformDisplay::Type::Surfaceless:
break;
}
}
if (!context) {
RELEASE_LOG_INFO(Compositing, "Could not create platform context: %s. Using Pbuffer as fallback.", lastErrorString());
context = createPbufferContext(platformDisplay);
if (!context)
RELEASE_LOG_INFO(Compositing, "Could not create Pbuffer context: %s.", lastErrorString());
}
if (!context)
WTFLogAlways("Could not create EGL sharing context.");
return context;
}
GLContext::GLContext(PlatformDisplay& display, EGLContext context, EGLSurface surface, EGLConfig config, EGLSurfaceType type)
: m_display(display)
, m_context(context)
, m_surface(surface)
, m_config(config)
, m_type(type)
{
ASSERT(type != PixmapSurface);
ASSERT(type == Surfaceless || surface != EGL_NO_SURFACE);
RELEASE_ASSERT(m_display.eglDisplay() != EGL_NO_DISPLAY);
RELEASE_ASSERT(context != EGL_NO_CONTEXT);
}
GLContext::~GLContext()
{
EGLDisplay display = m_display.eglDisplay();
if (m_context) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroyContext(display, m_context);
}
if (m_surface)
eglDestroySurface(display, m_surface);
#if PLATFORM(WAYLAND)
destroyWaylandWindow();
#endif
#if USE(WPE_RENDERER)
destroyWPETarget();
#endif
if (this == *currentContext())
*currentContext() = nullptr;
}
EGLContext GLContext::createContextForEGLVersion(PlatformDisplay& platformDisplay, EGLConfig config, EGLContext sharingContext)
{
static EGLint contextAttributes[7];
static bool contextAttributesInitialized = false;
if (!contextAttributesInitialized) {
contextAttributesInitialized = true;
#if USE(OPENGL_ES)
// GLES case. Not much to do here besides requesting a GLES2 version.
contextAttributes[0] = EGL_CONTEXT_CLIENT_VERSION;
contextAttributes[1] = 2;
contextAttributes[2] = EGL_NONE;
#else
// OpenGL case. We want to request an OpenGL version >= 3.2 with a core profile. If that's not possible,
// we'll use whatever is available. In order to request a concrete version of OpenGL we need EGL version
// 1.5 or EGL version 1.4 with the extension EGL_KHR_create_context.
EGLContext context = EGL_NO_CONTEXT;
if (platformDisplay.eglCheckVersion(1, 5)) {
contextAttributes[0] = EGL_CONTEXT_MAJOR_VERSION;
contextAttributes[1] = 3;
contextAttributes[2] = EGL_CONTEXT_MINOR_VERSION;
contextAttributes[3] = 2;
contextAttributes[4] = EGL_CONTEXT_OPENGL_PROFILE_MASK;
contextAttributes[5] = EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT;
contextAttributes[6] = EGL_NONE;
// Try to create a context with this configuration.
context = eglCreateContext(platformDisplay.eglDisplay(), config, sharingContext, contextAttributes);
} else if (platformDisplay.eglCheckVersion(1, 4)) {
const char* extensions = eglQueryString(platformDisplay.eglDisplay(), EGL_EXTENSIONS);
if (GLContext::isExtensionSupported(extensions, "EGL_KHR_create_context")) {
contextAttributes[0] = EGL_CONTEXT_MAJOR_VERSION_KHR;
contextAttributes[1] = 3;
contextAttributes[2] = EGL_CONTEXT_MINOR_VERSION_KHR;
contextAttributes[3] = 2;
contextAttributes[4] = EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR;
contextAttributes[5] = EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR;
contextAttributes[6] = EGL_NONE;
// Try to create a context with this configuration.
context = eglCreateContext(platformDisplay.eglDisplay(), config, sharingContext, contextAttributes);
}
}
// If the context creation worked, just return it.
if (context != EGL_NO_CONTEXT)
return context;
// Legacy case: the required EGL version is not present, or we haven't been able to create a >= 3.2 OpenGL
// context, so just request whatever is available.
contextAttributes[0] = EGL_NONE;
#endif
}
return eglCreateContext(platformDisplay.eglDisplay(), config, sharingContext, contextAttributes);
}
bool GLContext::makeContextCurrent()
{
ASSERT(m_context);
*currentContext() = this;
if (eglGetCurrentContext() == m_context)
return true;
return eglMakeCurrent(m_display.eglDisplay(), m_surface, m_surface, m_context);
}
GLContext* GLContext::current()
{
return *currentContext();
}
void GLContext::swapBuffers()
{
if (m_type == Surfaceless)
return;
ASSERT(m_surface);
eglSwapBuffers(m_display.eglDisplay(), m_surface);
}
GCGLContext GLContext::platformContext() const
{
return m_context;
}
bool GLContext::isExtensionSupported(const char* extensionList, const char* extension)
{
if (!extensionList)
return false;
ASSERT(extension);
int extensionLen = strlen(extension);
const char* extensionListPtr = extensionList;
while ((extensionListPtr = strstr(extensionListPtr, extension))) {
if (extensionListPtr[extensionLen] == ' ' || extensionListPtr[extensionLen] == '\0')
return true;
extensionListPtr += extensionLen;
}
return false;
}
unsigned GLContext::version()
{
if (!m_version) {
// Version string can start with the version number (all versions except GLES 1 and 2) or with
// "OpenGL". Different fields inside the version string are separated by spaces.
auto versionString = String::fromLatin1(reinterpret_cast<const char*>(::glGetString(GL_VERSION)));
Vector<String> versionStringComponents = versionString.split(' ');
Vector<String> versionDigits;
if (versionStringComponents[0] == "OpenGL"_s) {
// If the version string starts with "OpenGL" it can be GLES 1 or 2. In GLES1 version string starts
// with "OpenGL ES-<profile> major.minor" and in GLES2 with "OpenGL ES major.minor". Version is the
// third component in both cases.
versionDigits = versionStringComponents[2].split('.');
} else {
// Version is the first component. The version number is always "major.minor" or
// "major.minor.release". Ignore the release number.
versionDigits = versionStringComponents[0].split('.');
}
m_version = parseIntegerAllowingTrailingJunk<unsigned>(versionDigits[0]).value_or(0) * 100
+ parseIntegerAllowingTrailingJunk<unsigned>(versionDigits[1]).value_or(0) * 10;
}
return m_version;
}
} // namespace WebCore
#endif // USE(EGL)