Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Unable to get OpenGL context from the Java side on Android

Discussion in 'General Graphics' started by Virtumonde, Apr 10, 2021.

  1. Virtumonde

    Virtumonde

    Joined:
    Apr 15, 2020
    Posts:
    84
    As per the tittle, I am unable to get the EGL14.eglGetCurrentContext() inside an Android Java class. More precisely, the returned context is equal to EGL14.EGL_NO_CONTEXT. My interpretation is that the code is called from the main Unity thread yet the code is not able to get the OpenGL context. Multithreaded rendering is disabled. The project is a Unity project exported to Android. This C# code calls the Java initSurface method from an Update function of a MonoBehavior:

    Code (CSharp):
    1. private void Update()
    2.     {
    3.         if (_camMonitorInstance == null)
    4.         {
    5.             // it is important to call this in update method. Single Threaded Rendering will run in UnityMain Thread
    6.             InitAndroidSurface(camWidth, camHeight);
    7.         }
    8.         else
    9.         {
    10.             _camMonitorInstance.Call("updateSurfaceTexture");
    11.         }
    12.     }
    This should ensure that it will be called from the UnityMain thread. I am very confident that the thread is UnityMain because in updateSurfaceTexture Thread.currentThread().getName().equals("UnityMain") is true.

    Code (Java):
    1. private void initSurface() {
    2.         unityContext = EGL14.eglGetCurrentContext();
    3.         unityDisplay = EGL14.eglGetCurrentDisplay();
    4.         unityDrawSurface = EGL14.eglGetCurrentSurface(EGL14.EGL_DRAW);
    5.         unityReadSurface = EGL14.eglGetCurrentSurface(EGL14.EGL_READ);
    6.  
    7.         if (unityContext == EGL14.EGL_NO_CONTEXT) {
    8.             Log.e(TAG, "UnityEGLContext is invalid -> Most probably wrong thread");
    9.         }
    10.  
    11.         EGL14.eglMakeCurrent(unityDisplay, unityDrawSurface, unityReadSurface, unityContext);
    12.  
    13.         GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
    14.         GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, _unityTextureId);
    15.         GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
    16.         GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
    17.         GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
    18.         GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
    19.         _surfaceTexture = new SurfaceTexture(_unityTextureId);
    20.         _surfaceTexture.setDefaultBufferSize(_textureWidth, _textureHeight);
    21.         _surface = new Surface(_surfaceTexture);
    22.         _surfaceTexture.setOnFrameAvailableListener(this);
    23.     }
     
  2. Virtumonde

    Virtumonde

    Joined:
    Apr 15, 2020
    Posts:
    84