2016-06-18 14:46:12 +02:00
|
|
|
/*************************************************************************/
|
|
|
|
/* gl_context_egl.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2016-06-18 14:46:12 +02:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2014-10-03 05:10:51 +02:00
|
|
|
#include "gl_context_egl.h"
|
|
|
|
|
2014-12-20 19:30:06 +01:00
|
|
|
#include "EGL/eglext.h"
|
|
|
|
|
2014-10-03 05:10:51 +02:00
|
|
|
using namespace Platform;
|
|
|
|
|
|
|
|
void ContextEGL::release_current() {
|
|
|
|
|
|
|
|
eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, mEglContext);
|
|
|
|
};
|
|
|
|
|
|
|
|
void ContextEGL::make_current() {
|
|
|
|
|
|
|
|
eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext);
|
|
|
|
};
|
|
|
|
|
|
|
|
int ContextEGL::get_window_width() {
|
|
|
|
|
|
|
|
return width;
|
|
|
|
};
|
|
|
|
|
|
|
|
int ContextEGL::get_window_height() {
|
|
|
|
|
|
|
|
return height;
|
|
|
|
};
|
|
|
|
|
2014-12-20 19:30:06 +01:00
|
|
|
void ContextEGL::reset() {
|
|
|
|
|
|
|
|
cleanup();
|
|
|
|
|
|
|
|
window = CoreWindow::GetForCurrentThread();
|
|
|
|
initialize();
|
|
|
|
};
|
|
|
|
|
2014-10-03 05:10:51 +02:00
|
|
|
void ContextEGL::swap_buffers() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (eglSwapBuffers(mEglDisplay, mEglSurface) != EGL_TRUE) {
|
2014-10-03 05:10:51 +02:00
|
|
|
cleanup();
|
|
|
|
|
|
|
|
window = CoreWindow::GetForCurrentThread();
|
|
|
|
initialize();
|
|
|
|
|
|
|
|
// tell rasterizer to reload textures and stuff?
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Error ContextEGL::initialize() {
|
|
|
|
|
|
|
|
EGLint configAttribList[] = {
|
|
|
|
EGL_RED_SIZE, 8,
|
|
|
|
EGL_GREEN_SIZE, 8,
|
|
|
|
EGL_BLUE_SIZE, 8,
|
|
|
|
EGL_ALPHA_SIZE, 8,
|
|
|
|
EGL_DEPTH_SIZE, 8,
|
|
|
|
EGL_STENCIL_SIZE, 8,
|
|
|
|
EGL_SAMPLE_BUFFERS, 0,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
EGLint surfaceAttribList[] = {
|
|
|
|
EGL_NONE, EGL_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
EGLint numConfigs = 0;
|
|
|
|
EGLint majorVersion = 1;
|
|
|
|
EGLint minorVersion = 0;
|
|
|
|
EGLDisplay display = EGL_NO_DISPLAY;
|
|
|
|
EGLContext context = EGL_NO_CONTEXT;
|
|
|
|
EGLSurface surface = EGL_NO_SURFACE;
|
|
|
|
EGLConfig config = nullptr;
|
|
|
|
EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
2014-12-20 19:30:06 +01:00
|
|
|
const EGLint displayAttributes[] =
|
2017-03-19 00:36:26 +01:00
|
|
|
{
|
|
|
|
EGL_PLATFORM_ANGLE_TYPE_ANGLE, EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
|
|
|
|
EGL_PLATFORM_ANGLE_MAX_VERSION_MAJOR_ANGLE, 9,
|
|
|
|
EGL_PLATFORM_ANGLE_MAX_VERSION_MINOR_ANGLE, 3,
|
|
|
|
EGL_NONE,
|
|
|
|
};
|
2014-12-20 19:30:06 +01:00
|
|
|
|
|
|
|
PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT = reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(eglGetProcAddress("eglGetPlatformDisplayEXT"));
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (!eglGetPlatformDisplayEXT) {
|
2014-12-20 19:30:06 +01:00
|
|
|
throw Exception::CreateException(E_FAIL, L"Failed to get function eglGetPlatformDisplayEXT");
|
|
|
|
}
|
|
|
|
|
|
|
|
display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, displayAttributes);
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (display == EGL_NO_DISPLAY) {
|
2014-10-03 05:10:51 +02:00
|
|
|
throw Exception::CreateException(E_FAIL, L"Failed to get default EGL display");
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (eglInitialize(display, &majorVersion, &minorVersion) == EGL_FALSE) {
|
2014-10-03 05:10:51 +02:00
|
|
|
throw Exception::CreateException(E_FAIL, L"Failed to initialize EGL");
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (eglGetConfigs(display, NULL, 0, &numConfigs) == EGL_FALSE) {
|
2014-10-03 05:10:51 +02:00
|
|
|
throw Exception::CreateException(E_FAIL, L"Failed to get EGLConfig count");
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (eglChooseConfig(display, configAttribList, &config, 1, &numConfigs) == EGL_FALSE) {
|
2014-10-03 05:10:51 +02:00
|
|
|
throw Exception::CreateException(E_FAIL, L"Failed to choose first EGLConfig count");
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
surface = eglCreateWindowSurface(display, config, reinterpret_cast<IInspectable *>(window), surfaceAttribList);
|
|
|
|
if (surface == EGL_NO_SURFACE) {
|
2014-10-03 05:10:51 +02:00
|
|
|
throw Exception::CreateException(E_FAIL, L"Failed to create EGL fullscreen surface");
|
|
|
|
}
|
|
|
|
|
|
|
|
context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
|
2017-03-19 00:36:26 +01:00
|
|
|
if (context == EGL_NO_CONTEXT) {
|
2014-10-03 05:10:51 +02:00
|
|
|
throw Exception::CreateException(E_FAIL, L"Failed to create EGL context");
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
|
2014-10-03 05:10:51 +02:00
|
|
|
throw Exception::CreateException(E_FAIL, L"Failed to make fullscreen EGLSurface current");
|
|
|
|
}
|
|
|
|
} catch (...) {
|
|
|
|
return FAILED;
|
|
|
|
};
|
|
|
|
|
|
|
|
mEglDisplay = display;
|
|
|
|
mEglSurface = surface;
|
|
|
|
mEglContext = context;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
eglQuerySurface(display, surface, EGL_WIDTH, &width);
|
|
|
|
eglQuerySurface(display, surface, EGL_HEIGHT, &height);
|
2014-10-03 05:10:51 +02:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
};
|
|
|
|
|
|
|
|
void ContextEGL::cleanup() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (mEglDisplay != EGL_NO_DISPLAY && mEglSurface != EGL_NO_SURFACE) {
|
2014-10-03 05:10:51 +02:00
|
|
|
eglDestroySurface(mEglDisplay, mEglSurface);
|
|
|
|
mEglSurface = EGL_NO_SURFACE;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (mEglDisplay != EGL_NO_DISPLAY && mEglContext != EGL_NO_CONTEXT) {
|
2014-10-03 05:10:51 +02:00
|
|
|
eglDestroyContext(mEglDisplay, mEglContext);
|
|
|
|
mEglContext = EGL_NO_CONTEXT;
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (mEglDisplay != EGL_NO_DISPLAY) {
|
2014-10-03 05:10:51 +02:00
|
|
|
eglTerminate(mEglDisplay);
|
|
|
|
mEglDisplay = EGL_NO_DISPLAY;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ContextEGL::ContextEGL(CoreWindow ^ p_window)
|
|
|
|
: mEglDisplay(EGL_NO_DISPLAY),
|
|
|
|
mEglContext(EGL_NO_CONTEXT),
|
|
|
|
mEglSurface(EGL_NO_SURFACE) {
|
2014-10-03 05:10:51 +02:00
|
|
|
|
|
|
|
window = p_window;
|
|
|
|
};
|
|
|
|
|
|
|
|
ContextEGL::~ContextEGL() {
|
|
|
|
|
|
|
|
cleanup();
|
|
|
|
};
|