Skip to content

context creation in android fails to be set as current #512

@mgood7123

Description

@mgood7123

variables

int init_EGL = false;
bool
        init_eglGetDisplay = false,
        init_eglInitialize = false,
        init_eglChooseConfig = false,
        init_eglCreateWindowSurface = false,
        init_eglCreatePbufferSurface = false,
        init_eglCreateContext = false,
        init_eglMakeCurrent = false,
        init_debug = false;
const GLint
        *configuration_attributes = nullptr,
        *context_attributes = nullptr,
        *surface_attributes = nullptr;
EGLint
        eglMajVers = 0,
        eglMinVers = 0,
        number_of_configurations = 0;
EGLNativeDisplayType display_id = EGL_DEFAULT_DISPLAY;
EGLDisplay display = EGL_NO_DISPLAY;
EGLConfig configuration = 0;
EGLContext
        context = EGL_NO_CONTEXT,
        shared_context = EGL_NO_CONTEXT;
EGLSurface surface = EGL_NO_SURFACE;
GLint surface_type = 0;
EGLNativeWindowType native_window = 0;
GLint
        width = 0,
        height = 0;

struct ContextContainer {
    Containers::Optional<Platform::GLContext> _context{InPlaceInit, NoCreate};
};

Containers::Pointer<ContextContainer> magnumContext = nullptr;

initialization and destruction code

if (javasurface != nullptr) {

    if (init_EGL) {
        LOG_MAGNUM_ERROR << "context already exists";
        return;
    }

    // obtain native window
    {
        native_window = ANativeWindow_fromSurface(jenv, javasurface);
    }

    // init attributes for on screen rendering
    {
        configuration_attributes = new EGLint[3]{EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
                                                 EGL_NONE};

        surface_attributes = new EGLint[13]{EGL_RENDERABLE_TYPE, EGL_OPENGL_ES3_BIT,
                                            EGL_BLUE_SIZE, 8, EGL_GREEN_SIZE, 8,
                                            EGL_RED_SIZE, 8, EGL_ALPHA_SIZE, 8,
                                            EGL_DEPTH_SIZE, 16, EGL_NONE};
        surface_type = EGL_WINDOW_BIT;
    }

    // define error checking
    {
        #define CHECK_EGL(name) \
        { \
            EGLint e = eglGetError(); \
            if (e == EGL_SUCCESS) { \
                LOG_MAGNUM_INFO << name << " succeeded"; \
            } else { \
                LOG_MAGNUM_ERROR << name << " returned error: " << \
                    Magnum::Platform::Implementation::eglErrorString(e); \
            } \
        }
        #define PTR(ptr) (ptr == 0 ? "(nullptr)" : ptr)
    }

    // initialize
    {
        LOG_MAGNUM_INFO << "Initializing";

        // bind api
        {
            EGLBoolean r = eglBindAPI(EGL_OPENGL_ES_API);
            CHECK_EGL("eglBindAPI");
            if (r == EGL_FALSE) return;
        }

        // initialize display
        {
            LOG_MAGNUM_INFO << "Initializing display";
            display = eglGetDisplay(display_id);
            CHECK_EGL("eglGetDisplay");
            if (display == EGL_NO_DISPLAY) return;

            init_eglGetDisplay = true;

            // initialize egl with display
            {
                EGLBoolean r = eglInitialize(display, &eglMajVers, &eglMinVers);
                CHECK_EGL("eglInitialize");
                if (r == EGL_FALSE) return;
                init_eglInitialize = true;
            }
            LOG_MAGNUM_INFO << "EGL initialized with version " << eglMajVers << "."
                            << eglMinVers;

            // print information about the display
            {
                LOG_MAGNUM_INFO << "EGL_CLIENT_APIS: "
                                << PTR(eglQueryString(display, EGL_CLIENT_APIS));
                LOG_MAGNUM_INFO << "EGL_VENDOR: " << PTR(eglQueryString(display, EGL_VENDOR));
                LOG_MAGNUM_INFO << "EGL_VERSION: " << PTR(eglQueryString(display, EGL_VERSION));
                LOG_MAGNUM_INFO << "EGL_EXTENSIONS: "
                                << PTR(eglQueryString(display, EGL_EXTENSIONS));
            }
            LOG_MAGNUM_INFO << "Initialized display";
        }

        // aqcuire configuration for context
        {
            LOG_MAGNUM_INFO << "Initializing configuration";
            EGLBoolean r = eglChooseConfig(display, configuration_attributes,
                                           &configuration, 1, &number_of_configurations);
            CHECK_EGL("eglChooseConfig");
            if (r == EGL_FALSE) return;
            init_eglChooseConfig = true;
            LOG_MAGNUM_INFO << "Initialized configuration";
        }

        // obtain buffer format
        GLint format;
        {
            EGLBoolean r = eglGetConfigAttrib(display, configuration, EGL_NATIVE_VISUAL_ID,
                                              &format);
            CHECK_EGL("eglGetConfigAttrib");
            if (r == EGL_FALSE) false;
        }

        // set native buffer geometry
        {
            width = ANativeWindow_getWidth(native_window);
            height = ANativeWindow_getHeight(native_window);
            if (ANativeWindow_setBuffersGeometry(native_window, width, height, format) != 0) {
                LOG_MAGNUM_ERROR << "Failed to set native buffer geometry";
                return;
            }
        }

        // initialize surface
        {
            surface = eglCreateWindowSurface(display, configuration, native_window, nullptr);
            CHECK_EGL("eglCreateWindowSurface");
            if (surface == EGL_NO_SURFACE) return;
            init_eglCreateWindowSurface = true;
        }

        // initialize context
        {
            context_attributes = new EGLint[3]{EGL_CONTEXT_CLIENT_VERSION, eglMajVers,
                                               EGL_NONE};
            context = eglCreateContext(display, configuration, shared_context,
                                       context_attributes);
            CHECK_EGL("eglCreateContext");
            if (context == EGL_NO_CONTEXT) return;
            init_eglCreateContext = true;
        }

        // switch to context
        {
            EGLBoolean r = eglMakeCurrent(display, surface, surface, context);
            CHECK_EGL("eglMakeCurrent");
            if (r == EGL_FALSE) return;
            init_eglMakeCurrent = true;

            // print information about the current context
            {
                LOG_MAGNUM_INFO << "GL_VENDOR: " << PTR((const char *) glGetString(GL_VENDOR));
                LOG_MAGNUM_INFO << "GL_RENDERER: "
                                << PTR((const char *) glGetString(GL_RENDERER));
                LOG_MAGNUM_INFO << "GL_VERSION: "
                                << PTR((const char *) glGetString(GL_VERSION));
                LOG_MAGNUM_INFO << "GL_SHADING_LANGUAGE_VERSION: "
                                << PTR((const char *) glGetString(GL_SHADING_LANGUAGE_VERSION));
                LOG_MAGNUM_INFO << "GL_EXTENSIONS: "
                                << PTR((const char *) glGetString(GL_EXTENSIONS));
            }
        }

        init_EGL = true;
    }

    LOG_MAGNUM_INFO << "initialized EGL";

    // create magnum context
    {
        magnumContext.reset(new ContextContainer());
        Platform::GLContext *context_ = magnumContext->_context.operator->();
        if (context_->tryCreate()) {
            LOG_MAGNUM_INFO << "created magnum context";

            // draw something
            {
                GL::defaultFramebuffer.clear(GL::FramebufferClear::Color);
                eglSwapBuffers(display, surface);
            }
        } else {
            LOG_MAGNUM_INFO << "failed to create magnum context";
        }
    }
} else {
    // destroy everything
    {
        if (magnumContext != nullptr) {
            delete magnumContext.operator->();
            magnumContext.reset(nullptr);
        }

        if (!init_EGL) return;

        if (init_eglMakeCurrent) {
            eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
            CHECK_EGL("eglMakeCurrent");
            init_eglMakeCurrent = false;
        }
        if (init_eglCreateContext) {
            eglDestroyContext(display, context);
            CHECK_EGL("eglDestroyContext");
            context = EGL_NO_CONTEXT;
            shared_context = EGL_NO_CONTEXT;
            init_eglCreateContext = false;
        }
        if (init_eglCreateWindowSurface || init_eglCreatePbufferSurface) {
            eglDestroySurface(display, surface);
            CHECK_EGL("eglDestroySurface");
            surface = EGL_NO_SURFACE;
            init_eglCreateWindowSurface = false;
            init_eglCreatePbufferSurface = false;
        }
        if (init_eglChooseConfig) {
            // TODO: figure how to undo init_eglChooseConfig
        }
        if (init_eglInitialize) {
            eglTerminate(display);
            CHECK_EGL("eglTerminate");
            init_eglInitialize = false;
        }
        if (init_eglGetDisplay) {
            display = EGL_NO_DISPLAY;
            init_eglGetDisplay = false;
        }
        if (configuration_attributes != nullptr) delete[] configuration_attributes;
        if (context_attributes != nullptr) delete[] context_attributes;
        if (surface_attributes != nullptr) delete[] surface_attributes;
        init_EGL = false;

        ANativeWindow_release(native_window);
        native_window = 0;
    }
}

output

06-05 18:11:30.673 30185 30185 I ViewRootImpl@7dde1ed[MainActivity]: [DrawPending] drawPending(1) 1 android.view.ViewRootImpl.reportNextDraw:9955 android.view.ViewRootImpl.performTraversals:3332 android.view.ViewRootImpl.doTraversal:2225
06-05 18:11:30.675  4155  4226 I SurfaceFlinger: id=5076 createSurf (0x0),2 flag=4, Bounds for - com.smallville7123.crafter/com.smallville7123.crafter.MainActivity@0#0
06-05 18:11:30.679  4155  4226 I SurfaceFlinger: id=5077 createSurf (1080x1989),4 flag=404, SurfaceView - com.smallville7123.crafter/com.smallville7123.crafter.MainActivity@9c052b3@0#0
06-05 18:11:30.680  4155  4226 I SurfaceFlinger: id=5078 createSurf (0x0),-1 flag=20404, Background for -SurfaceView - com.smallville7123.crafter/com.smallville7123.crafter.MainActivity@9c052b3@0#0
06-05 18:11:30.681 30185 30185 I SurfaceView: surfaceCreated 1 #8 com.smallville7123.crafter.NativeView$View{9c052b3 V.ED..... ......ID 0,0-1080,1989}
06-05 18:11:30.682 30185 30185 I SurfaceView: surfaceChanged (1080,1989) 1 #8 com.smallville7123.crafter.NativeView$View{9c052b3 V.ED..... ......ID 0,0-1080,1989}
06-05 18:11:30.682 30185 30185 D magnum  : Initializing
06-05 18:11:30.682 30185 30185 D magnum  : eglBindAPI succeeded
06-05 18:11:30.682 30185 30185 D magnum  : Initializing display
06-05 18:11:30.695 30185 30185 D magnum  : eglGetDisplay succeeded
06-05 18:11:30.695 30185 30185 D magnum  : eglInitialize succeeded
06-05 18:11:30.695 30185 30185 D magnum  : EGL initialized with version 1.4
06-05 18:11:30.695 30185 30185 D magnum  : EGL_CLIENT_APIS: OpenGL_ES
06-05 18:11:30.695 30185 30185 D magnum  : EGL_VENDOR: Android
06-05 18:11:30.695 30185 30185 D magnum  : EGL_VERSION: 1.4 Android META-EGL
06-05 18:11:30.695 30185 30185 D magnum  : EGL_EXTENSIONS: EGL_KHR_get_all_proc_addresses EGL_ANDROID_presentation_time EGL_KHR_swap_buffers_with_damage EGL_ANDROID_get_native_client_buffer EGL_ANDROID_front_buffer_auto_refresh EGL_ANDROID_get_frame_timestamps EGL_EXT_surface_SMPTE2086_metadata EGL_EXT_surface_CTA861_3_metadata EGL_EXT_gl_colorspace_scrgb EGL_EXT_gl_colorspace_scrgb_linear EGL_EXT_gl_colorspace_display_p3_linear EGL_EXT_gl_colorspace_display_p3 EGL_EXT_gl_colorspace_display_p3_passthrough EGL_EXT_gl_colorspace_bt2020_linear EGL_EXT_gl_colorspace_bt2020_pq EGL_KHR_image EGL_KHR_image_base EGL_EXT_image_gl_colorspace EGL_KHR_gl_colorspace EGL_KHR_gl_texture_2D_image EGL_KHR_gl_texture_cubemap_image EGL_KHR_gl_renderbuffer_image EGL_KHR_fence_sync EGL_KHR_create_context EGL_KHR_config_attribs EGL_KHR_surfaceless_context EGL_EXT_create_context_robustness EGL_ANDROID_image_native_buffer EGL_KHR_wait_sync EGL_ANDROID_recordable EGL_KHR_partial_update EGL_EXT_pixel_format_float EGL_KHR_mutable_render_buffer EGL_EXT_protected_content EGL_IMG_context_priority EGL_KHR_no_config_context
06-05 18:11:30.695 30185 30185 D magnum  : Initialized display
06-05 18:11:30.695 30185 30185 D magnum  : Initializing configuration
06-05 18:11:30.695 30185 30185 D magnum  : eglChooseConfig succeeded
06-05 18:11:30.695 30185 30185 D magnum  : Initialized configuration
06-05 18:11:30.695 30185 30185 D magnum  : eglGetConfigAttrib succeeded
06-05 18:11:30.698 30185 30185 D mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
06-05 18:11:30.698 30185 30185 D magnum  : eglCreateWindowSurface succeeded
06-05 18:11:30.699 30185 30300 D OpenGLRenderer: makeCurrent EglSurface : 0x0 -> 0x0
06-05 18:11:30.701 30185 30185 D magnum  : eglCreateContext succeeded
06-05 18:11:30.701 30185 30185 D magnum  : eglMakeCurrent succeeded
06-05 18:11:30.701 30185 30185 D magnum  : GL_VENDOR: ARM
06-05 18:11:30.701 30185 30185 D magnum  : GL_RENDERER: Mali-G71
06-05 18:11:30.701 30185 30185 D magnum  : GL_VERSION: OpenGL ES-CM 1.1 v1.r16p0-01rel0.###other-sha0123456789ABCDEF0###
06-05 18:11:30.701 30185 30185 D magnum  : GL_SHADING_LANGUAGE_VERSION: (nullptr)
06-05 18:11:30.701 30185 30185 D magnum  : GL_EXTENSIONS: GL_EXT_debug_marker GL_OES_byte_coordinates GL_OES_fixed_point GL_OES_single_precision GL_OES_matrix_get GL_OES_compressed_paletted_texture GL_OES_point_size_array GL_OES_point_sprite GL_OES_read_format GL_OES_compressed_ETC1_RGB8_texture GL_OES_depth24 GL_OES_stencil8 GL_OES_framebuffer_object GL_OES_packed_depth_stencil GL_OES_rgb8_rgba8 GL_EXT_read_format_bgra GL_OES_matrix_palette GL_OES_extended_matrix_palette GL_OES_draw_texture GL_OES_blend_equation_separate GL_OES_blend_func_separate GL_OES_blend_subtract GL_OES_stencil_wrap GL_OES_texture_mirrored_repeat GL_EXT_texture_format_BGRA8888 GL_OES_query_matrix GL_OES_EGL_image GL_OES_EGL_image_external GL_OES_EGL_sync GL_OES_texture_npot GL_OES_vertex_half_float GL_OES_required_internalformat GL_OES_vertex_array_object GL_OES_mapbuffer GL_OES_fbo_render_mipmap GL_OES_element_index_uint GL_ARM_rgba8 GL_EXT_blend_minmax GL_EXT_discard_framebuffer GL_EXT_texture_storage GL_OES_texture_compression_astc GL_KHR_texture_compression_astc_ldr GL_KHR_texture_compression_astc_hdr GL_KHR_texture_compression_astc_sliced_3d GL_OES_surfaceless_context GL_EXT_multisampled_render_to_texture GL_OES_texture_cube_map GL_KHR_debug GL_EXT_sRGB GL_EXT_robustness
06-05 18:11:30.701 30185 30185 D magnum  : initialized EGL
06-05 18:11:30.712 30185 30300 D mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
06-05 18:11:30.712 30185 30300 D OpenGLRenderer: eglCreateWindowSurface : 0x73cef25d00
06-05 18:11:30.718 30185 30185 D magnum  : failed to create magnum context

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions