SDL: Using egl in sdl in android leads to crash

I want to create egl context using my sdl android app. I know I can create the built in SDL_GL_createcontext but it doesn’t work in my case. It only shows black screen on my attempt to embed mpv on android using SDL. That’s why I thought of using EGL. Down below is my code. However the sdl asserts the following. Am I missing something here? The code below works fine in SDL windows application using angle jwith just simple difference. I am using the stable version of SDL. image

#include <stdlib.h>
#include <stdio.h>

#include "SDL.h"
#include "SDL_syswm.h"

#include <EGL/egl.h>
#include <GLES2/gl2.h>

using namespace std;


int main(int argc, char* argv[])
{
    SDL_Window* window = 0;
    
    if (0 != SDL_Init(SDL_INIT_VIDEO))
    {
        fprintf(stderr, "\nUnable to initialize SDL: %s\n", SDL_GetError());
        return 1;
    }
    window = SDL_CreateWindow("Glad Sample",
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        800, 600, SDL_WINDOW_SHOWN |
        SDL_WINDOW_OPENGL);

  EGLint configAttribList[] =
    {
        EGL_RED_SIZE,       8,
        EGL_GREEN_SIZE,     8,
        EGL_BLUE_SIZE,      8,
        EGL_ALPHA_SIZE,     8 /*: EGL_DONT_CARE*/,
        EGL_DEPTH_SIZE,     EGL_DONT_CARE,
        EGL_STENCIL_SIZE,   EGL_DONT_CARE,
        EGL_SAMPLE_BUFFERS, 0,
        EGL_NONE
    };

    EGLint surfaceAttribList[] =
    {
        // EGL_CONTEXT_CLIENT_VERSION, 2,
         EGL_WIDTH, 800,
         EGL_HEIGHT, 600,
         EGL_NONE

    };
    EGLint attribListPbuffer[] = {
    EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
    EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
    EGL_BUFFER_SIZE, 32,
    EGL_DEPTH_SIZE, 24,
    EGL_STENCIL_SIZE, 8,
    EGL_NONE
    };

    EGLint numConfigs;
    EGLint majorVersion;
    EGLint minorVersion;
    EGLDisplay display;
    EGLContext context;
    EGLSurface surface;
    EGLConfig config;
    EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };

    SDL_SysWMinfo info;
    SDL_VERSION(&info.version); // initialize info structure with SDL version info
    SDL_GetWindowWMInfo(window, &info);   
    SDL_bool get_win_info = SDL_GetWindowWMInfo(window, &info);
    SDL_assert_release(get_win_info);
    EGLNativeWindowType hWnd = info.info.android.window;

    // Get Display 
    display = eglGetDisplay(EGL_DEFAULT_DISPLAY); // EGL_DEFAULT_DISPLAY 
    if (display == EGL_NO_DISPLAY)
    {
        return EGL_FALSE;
    }

    // Initialize EGL 
    if (!eglInitialize(display, &majorVersion, &minorVersion))
    {
        return EGL_FALSE;
    }

    // Get configs 
    if (!eglGetConfigs(display, NULL, 0, &numConfigs))
    {
        return EGL_FALSE;
    }

    // Choose config 
    if (!eglChooseConfig(display, configAttribList, &config, 1, &numConfigs))
    {
        return EGL_FALSE;
    }

    // Create a 
    surface = eglCreateWindowSurface(display, config,hWnd, surfaceAttribList);
    if (surface == EGL_NO_SURFACE)
    {
        return EGL_FALSE;
    }
    context = eglCreateContext(display, config, EGL_NO_CONTEXT, contextAttribs);
    if (context == EGL_NO_CONTEXT)
    {
        return EGL_FALSE;
    }

    // Make the context current 
    if (!eglMakeCurrent(display, surface, surface, context))
    {
        return EGL_FALSE;
    }
    
    SDL_Event event;

    bool running = true;
    while (running)
    {
        SDL_PollEvent(&event);
        switch (event.type) {
        case SDL_QUIT:
            running = false;
            break;
        case SDL_WINDOWEVENT:
            if (event.window.event == SDL_WINDOWEVENT_EXPOSED)
              //  redraw = 1;
            break;
        }
       
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        eglSwapBuffers(display, surface);

    }
    SDL_DestroyWindow(window);
    SDL_Quit();

    exit(0);
}

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 40 (16 by maintainers)

Most upvoted comments

All the others flags. You can hardcode in config.h

Ah okay, so the issue is actually that the window didn’t get made, so WMInfo is fine. The pixel format thing is where the Android stuff starts, so I’ll slowly back out of the room now…