123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- #include "gl_context_egl.h"
- #include "EGL/eglext.h"
- using Platform::Exception;
- 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;
- };
- void ContextEGL::reset() {
- cleanup();
- window = CoreWindow::GetForCurrentThread();
- initialize();
- };
- void ContextEGL::swap_buffers() {
- if (eglSwapBuffers(mEglDisplay, mEglSurface) != EGL_TRUE) {
- cleanup();
- window = CoreWindow::GetForCurrentThread();
- initialize();
-
- }
- };
- 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;
- if (driver == GLES_2_0) {
- minorVersion = 0;
- } else {
- minorVersion = 5;
- }
- EGLDisplay display = EGL_NO_DISPLAY;
- EGLContext context = EGL_NO_CONTEXT;
- EGLSurface surface = EGL_NO_SURFACE;
- EGLConfig config = nullptr;
- EGLint contextAttribs[3];
- if (driver == GLES_2_0) {
- contextAttribs[0] = EGL_CONTEXT_CLIENT_VERSION;
- contextAttribs[1] = 2;
- contextAttribs[2] = EGL_NONE;
- } else {
- contextAttribs[0] = EGL_CONTEXT_CLIENT_VERSION;
- contextAttribs[1] = 3;
- contextAttribs[2] = EGL_NONE;
- }
- try {
- const EGLint displayAttributes[] = {
-
-
-
- EGL_PLATFORM_ANGLE_TYPE_ANGLE,
- EGL_PLATFORM_ANGLE_TYPE_D3D11_ANGLE,
-
-
- EGL_ANGLE_DISPLAY_ALLOW_RENDER_TO_BACK_BUFFER,
- EGL_TRUE,
-
-
-
- EGL_PLATFORM_ANGLE_ENABLE_AUTOMATIC_TRIM_ANGLE,
- EGL_TRUE,
- EGL_NONE,
- };
- PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplayEXT = reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(eglGetProcAddress("eglGetPlatformDisplayEXT"));
- if (!eglGetPlatformDisplayEXT) {
- throw Exception::CreateException(E_FAIL, L"Failed to get function eglGetPlatformDisplayEXT");
- }
- display = eglGetPlatformDisplayEXT(EGL_PLATFORM_ANGLE_ANGLE, EGL_DEFAULT_DISPLAY, displayAttributes);
- if (display == EGL_NO_DISPLAY) {
- throw Exception::CreateException(E_FAIL, L"Failed to get default EGL display");
- }
- if (eglInitialize(display, &majorVersion, &minorVersion) == EGL_FALSE) {
- throw Exception::CreateException(E_FAIL, L"Failed to initialize EGL");
- }
- if (eglGetConfigs(display, NULL, 0, &numConfigs) == EGL_FALSE) {
- throw Exception::CreateException(E_FAIL, L"Failed to get EGLConfig count");
- }
- if (eglChooseConfig(display, configAttribList, &config, 1, &numConfigs) == EGL_FALSE) {
- throw Exception::CreateException(E_FAIL, L"Failed to choose first EGLConfig count");
- }
- surface = eglCreateWindowSurface(display, config, reinterpret_cast<IInspectable *>(window), surfaceAttribList);
- if (surface == EGL_NO_SURFACE) {
- throw Exception::CreateException(E_FAIL, L"Failed to create EGL fullscreen surface");
- }
- context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
- if (context == EGL_NO_CONTEXT) {
- throw Exception::CreateException(E_FAIL, L"Failed to create EGL context");
- }
- if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
- throw Exception::CreateException(E_FAIL, L"Failed to make fullscreen EGLSurface current");
- }
- } catch (...) {
- return FAILED;
- };
- mEglDisplay = display;
- mEglSurface = surface;
- mEglContext = context;
- eglQuerySurface(display, surface, EGL_WIDTH, &width);
- eglQuerySurface(display, surface, EGL_HEIGHT, &height);
- return OK;
- };
- void ContextEGL::cleanup() {
- if (mEglDisplay != EGL_NO_DISPLAY && mEglSurface != EGL_NO_SURFACE) {
- eglDestroySurface(mEglDisplay, mEglSurface);
- mEglSurface = EGL_NO_SURFACE;
- }
- if (mEglDisplay != EGL_NO_DISPLAY && mEglContext != EGL_NO_CONTEXT) {
- eglDestroyContext(mEglDisplay, mEglContext);
- mEglContext = EGL_NO_CONTEXT;
- }
- if (mEglDisplay != EGL_NO_DISPLAY) {
- eglTerminate(mEglDisplay);
- mEglDisplay = EGL_NO_DISPLAY;
- }
- };
- ContextEGL::ContextEGL(CoreWindow ^ p_window, Driver p_driver) :
- mEglDisplay(EGL_NO_DISPLAY),
- mEglContext(EGL_NO_CONTEXT),
- mEglSurface(EGL_NO_SURFACE),
- driver(p_driver),
- window(p_window) {}
- ContextEGL::~ContextEGL() {
- cleanup();
- };
|