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
:
private void Update()
{
if (_camMonitorInstance == null)
{
// it is important to call this in update method. Single Threaded Rendering will run in UnityMain Thread
InitAndroidSurface(camWidth, camHeight);
}
else
{
_camMonitorInstance.Call("updateSurfaceTexture");
}
}
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.
private void initSurface() {
unityContext = EGL14.eglGetCurrentContext();
unityDisplay = EGL14.eglGetCurrentDisplay();
unityDrawSurface = EGL14.eglGetCurrentSurface(EGL14.EGL_DRAW);
unityReadSurface = EGL14.eglGetCurrentSurface(EGL14.EGL_READ);
if (unityContext == EGL14.EGL_NO_CONTEXT) {
Log.e(TAG, "UnityEGLContext is invalid -> Most probably wrong thread");
}
EGL14.eglMakeCurrent(unityDisplay, unityDrawSurface, unityReadSurface, unityContext);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, _unityTextureId);
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameterf(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
_surfaceTexture = new SurfaceTexture(_unityTextureId);
_surfaceTexture.setDefaultBufferSize(_textureWidth, _textureHeight);
_surface = new Surface(_surfaceTexture);
_surfaceTexture.setOnFrameAvailableListener(this);
}
UPDATE V1:
Everything works in emulators but not on real devices (Nougat 7.1 API level 25 and Android10 API level 29).
UPDATE V2, verbose logs:
2021-04-07 15:13:59.944 17317-17317/? I/m.MyCompany.MyGame: Late-enabling -Xcheck:jni
2021-04-07 15:13:59.982 17317-17317/? E/m.MyCompany.MyGame: Unknown bits set in runtime_flags: 0x8000
2021-04-07 15:13:59.984 17317-17317/? I/m.MyCompany.MyGame: Reinit property: dalvik.vm.checkjni= false
2021-04-07 15:13:59.999 17317-17317/? W/re-initialized>: type=1400 audit(0.0:6900): avc: denied { read } for pid=17317 name="u:object_r:mmi_prop:s0" dev="tmpfs" ino=1830 scontext=u:r:untrusted_app:s0:c245,c256,c512,c768 tcontext=u:object_r:mmi_prop:s0 tclass=file permissive=0
2021-04-07 15:14:00.004 17317-17317/? E/libc: Access denied finding property "runtime.mmitest.isrunning"
2021-04-07 15:14:00.008 17317-17317/? D/ActivityThread: Attach thread to application
2021-04-07 15:14:00.270 17317-17317/com.MyCompany.MyGame I/m.MyCompany.MyGame: QarthPatchMonintor::Init
2021-04-07 15:14:00.270 17317-17317/com.MyCompany.MyGame I/m.MyCompany.MyGame: QarthPatchMonintor::StartWatch
2021-04-07 15:14:00.270 17317-17317/com.MyCompany.MyGame I/m.MyCompany.MyGame: QarthPatchMonintor::WatchPackage: /data/hotpatch/fwkhotpatch/
2021-04-07 15:14:00.270 17317-17317/com.MyCompany.MyGame I/m.MyCompany.MyGame: QarthPatchMonintor::CheckAndWatchPatch: /data/hotpatch/fwkhotpatch/com.MyCompany.MyGame
2021-04-07 15:14:00.270 17317-17317/com.MyCompany.MyGame I/m.MyCompany.MyGame: QarthPatchMonintor::CheckAndWatchPatch: /data/hotpatch/fwkhotpatch/all
2021-04-07 15:14:00.270 17317-17317/com.MyCompany.MyGame I/m.MyCompany.MyGame: QarthPatchMonintor::Run
2021-04-07 15:14:00.270 17317-17353/com.MyCompany.MyGame I/m.MyCompany.MyGame: QarthPatchMonintor::Reading
2021-04-07 15:14:00.270 17317-17353/com.MyCompany.MyGame I/m.MyCompany.MyGame: QarthPatchMonintor::CheckNotifyEvent
2021-04-07 15:14:00.270 17317-17353/com.MyCompany.MyGame I/m.MyCompany.MyGame: QarthPatchMonintor::CheckNotifyEvent before read
2021-04-07 15:14:00.273 17317-17336/com.MyCompany.MyGame I/HwApiCacheMangerEx: apicache path=/storage/emulated/0 state=mounted key=com.MyCompany.MyGame#10245#256
2021-04-07 15:14:00.274 17317-17336/com.MyCompany.MyGame I/HwApiCacheMangerEx: apicache path=/storage/emulated/0 state=mounted key=com.MyCompany.MyGame#10245#0
2021-04-07 15:14:00.284 17317-17336/com.MyCompany.MyGame I/AwareBitmapCacher: init processName:com.MyCompany.MyGame pid=17317 uid=10245
2021-04-07 15:14:00.296 17317-17354/com.MyCompany.MyGame E/AwareLog: AtomicFileUtils: readFileLines file not exist: android.util.AtomicFile@4f00392
2021-04-07 15:14:00.381 17317-17317/com.MyCompany.MyGame V/ActivityThread: callActivityOnCreate
2021-04-07 15:14:00.424 17317-17317/com.MyCompany.MyGame I/Mono: JNI_OnLoad called
2021-04-07 15:14:00.442 17317-17317/com.MyCompany.MyGame V/HwWidgetFactory: : successes to get AllImpl object and return....
2021-04-07 15:14:00.472 17317-17317/com.MyCompany.MyGame W/m.MyCompany.MyGame: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2021-04-07 15:14:00.473 17317-17317/com.MyCompany.MyGame W/m.MyCompany.MyGame: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2021-04-07 15:14:00.480 17317-17317/com.MyCompany.MyGame D/ActivityThread: add activity client record, r= ActivityRecord{79b4c4c token=android.os.BinderProxy@f9fc86d {com.MyCompany.MyGame/com.unity3d.player.UnityPlayerActivity}} token= android.os.BinderProxy@f9fc86d
2021-04-07 15:14:00.487 17317-17317/com.MyCompany.MyGame V/AudioManager: getStreamVolume streamType: 3 volume: 2
2021-04-07 15:14:00.513 17317-17360/com.MyCompany.MyGame D/HiTouch_PressGestureDetector: onAttached, package=com.MyCompany.MyGame, windowType=1, mHiTouchRestricted=false
2021-04-07 15:14:00.535 17317-17317/com.MyCompany.MyGame W/Gralloc3: mapper 3.x is not supported
2021-04-07 15:14:00.537 17317-17317/com.MyCompany.MyGame E/ion: ioctl c0044901 failed with code -1: Not a typewriter
2021-04-07 15:14:00.547 17317-17317/com.MyCompany.MyGame I/HwViewRootImpl: removeInvalidNode jank list is null
2021-04-07 15:14:00.573 17317-17357/com.MyCompany.MyGame I/Unity: MemoryManager: Using 'Dynamic Heap' Allocator.
2021-04-07 15:14:00.576 17317-17357/com.MyCompany.MyGame W/m.MyCompany.MyGame: Accessing hidden method Ljava/lang/invoke/MethodHandles$Lookup;-><init>(Ljava/lang/Class;I)V (greylist, reflection, allowed)
2021-04-07 15:14:00.579 17317-17317/com.MyCompany.MyGame W/UnityMain: type=1400 audit(0.0:6901): avc: granted { write } for pid=17317 name="com.MyCompany.MyGame" dev="sdcardfs" ino=22707 scontext=u:r:untrusted_app:s0:c245,c256,c512,c768 tcontext=u:object_r:sdcardfs:s0 tclass=dir
2021-04-07 15:14:00.579 17317-17317/com.MyCompany.MyGame W/UnityMain: type=1400 audit(0.0:6902): avc: granted { add_name } for pid=17317 name="7a32170c1f93ca2e897b288e8dae6264" scontext=u:r:untrusted_app:s0:c245,c256,c512,c768 tcontext=u:object_r:sdcardfs:s0 tclass=dir
2021-04-07 15:14:00.579 17317-17317/com.MyCompany.MyGame W/UnityMain: type=1400 audit(0.0:6903): avc: granted { create } for pid=17317 name="7a32170c1f93ca2e897b288e8dae6264" scontext=u:r:untrusted_app:s0:c245,c256,c512,c768 tcontext=u:object_r:sdcardfs:s0:c245,c256,c512,c768 tclass=file
2021-04-07 15:14:00.579 17317-17317/com.MyCompany.MyGame W/UnityMain: type=1400 audit(0.0:6904): avc: granted { read write open } for pid=17317 path="/storage/emulated/0/Android/data/com.MyCompany.MyGame/7a32170c1f93ca2e897b288e8dae6264" dev="sdcardfs" ino=41792 scontext=u:r:untrusted_app:s0:c245,c256,c512,c768 tcontext=u:object_r:sdcardfs:s0 tclass=file
2021-04-07 15:14:00.579 17317-17317/com.MyCompany.MyGame W/UnityMain: type=1400 audit(0.0:6905): avc: granted { write } for pid=17317 name="com.MyCompany.MyGame" dev="sdcardfs" ino=22707 scontext=u:r:untrusted_app:s0:c245,c256,c512,c768 tcontext=u:object_r:sdcardfs:s0 tclass=dir
2021-04-07 15:14:00.579 17317-17317/com.MyCompany.MyGame W/UnityMain: type=1400 audit(0.0:6906): avc: granted { write } for pid=17317 name="com.MyCompany.MyGame" dev="sdcardfs" ino=22707 scontext=u:r:untrusted_app:s0:c245,c256,c512,c768 tcontext=u:object_r:sdcardfs:s0 tclass=dir
2021-04-07 15:14:00.579 17317-17317/com.MyCompany.MyGame W/UnityMain: type=1400 audit(0.0:6907): avc: granted { remove_name } for pid=17317 name="7a32170c1f93ca2e897b288e8dae6264" dev="sdcardfs" ino=41792 scontext=u:r:untrusted_app:s0:c245,c256,c512,c768 tcontext=u:object_r:sdcardfs:s0 tclass=dir
2021-04-07 15:14:00.579 17317-17317/com.MyCompany.MyGame W/UnityMain: type=1400 audit(0.0:6908): avc: granted { rename } for pid=17317 name="7a32170c1f93ca2e897b288e8dae6264" dev="sdcardfs" ino=41792 scontext=u:r:untrusted_app:s0:c245,c256,c512,c768 tcontext=u:object_r:sdcardfs:s0 tclass=file
2021-04-07 15:14:00.598 17317-17357/com.MyCompany.MyGame I/Unity: SystemInfo CPU = ARM64 FP ASIMD AES, Cores = 8, Memory = 3680mb
2021-04-07 15:14:00.598 17317-17357/com.MyCompany.MyGame I/Unity: SystemInfo ARM big.LITTLE configuration: 4 big (mask: 0xf0), 4 little (mask: 0xf)
2021-04-07 15:14:00.599 17317-17357/com.MyCompany.MyGame I/Unity: ApplicationInfo com.MyCompany.MyGame version 0.0.1 build 691c9dd8-c42e-4505-93a5-a598f89c82d0
2021-04-07 15:14:00.599 17317-17357/com.MyCompany.MyGame I/Unity: Built from '2020.3/release' branch, Version '2020.3.2f1 (8fd9074bf66c)', Build type 'Release', Scripting Backend 'mono', CPU 'armeabi-v7a', Stripping 'Disabled'
2021-04-07 15:14:00.689 17317-17357/com.MyCompany.MyGame I/Unity: Company Name: MyCompany
2021-04-07 15:14:00.689 17317-17357/com.MyCompany.MyGame I/Unity: Product Name: MyGame
2021-04-07 15:14:00.699 17317-17357/com.MyCompany.MyGame I/iGraphics: [0020080c] pn: com.MyCompany.MyGame, p: 17317
2021-04-07 15:14:00.699 17317-17357/com.MyCompany.MyGame I/iGraphics: [0030050c] PV 10
2021-04-07 15:14:00.699 17317-17357/com.MyCompany.MyGame I/iGraphics: [0030050c] no element 'igfx'
2021-04-07 15:14:00.700 17317-17357/com.MyCompany.MyGame I/iGraphics: [0030050c] LoadCloudParams lib file success.
2021-04-07 15:14:00.701 17317-17357/com.MyCompany.MyGame E/Parcel: Reading a NULL string not supported here.
2021-04-07 15:14:00.710 17317-17357/com.MyCompany.MyGame D/AwareLog: Game opt is enable
2021-04-07 15:14:00.714 17317-17357/com.MyCompany.MyGame D/mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
2021-04-07 15:14:00.720 17317-17357/com.MyCompany.MyGame D/Unity: GL_EXT_debug_marker GL_ARM_rgba8 GL_ARM_mali_shader_binary GL_OES_depth24 GL_OES_depth_texture GL_OES_depth_texture_cube_map GL_OES_packed_depth_stencil GL_OES_rgb8_rgba8 GL_EXT_read_format_bgra GL_OES_compressed_paletted_texture GL_OES_compressed_ETC1_RGB8_texture GL_OES_standard_derivatives GL_OES_EGL_image GL_OES_EGL_image_external GL_OES_EGL_image_external_essl3 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_EXT_texture_format_BGRA8888 GL_EXT_texture_rg GL_EXT_texture_type_2_10_10_10_REV GL_OES_fbo_render_mipmap GL_OES_element_index_uint GL_EXT_shadow_samplers 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_EXT_texture_compression_astc_decode_mode GL_EXT_texture_compression_astc_decode_mode_rgb9e5 GL_KHR_debug GL_EXT_occlusion_query_boolean GL_EXT_disjoint_timer_query GL_EXT_blend_minmax GL_EXT_discard_framebuffer
2021-04-07 15:14:00.720 17317-17357/com.MyCompany.MyGame D/Unity: GL_OES_get_program_binary GL_OES_texture_3D GL_EXT_texture_storage GL_EXT_multisampled_render_to_texture GL_EXT_multisampled_render_to_texture2 GL_OES_surfaceless_context GL_OES_texture_stencil8 GL_EXT_shader_pixel_local_storage GL_ARM_shader_framebuffer_fetch GL_ARM_shader_framebuffer_fetch_depth_stencil GL_ARM_mali_program_binary GL_EXT_sRGB GL_EXT_sRGB_write_control GL_EXT_texture_sRGB_decode GL_EXT_texture_sRGB_R8 GL_EXT_texture_sRGB_RG8 GL_KHR_blend_equation_advanced GL_KHR_blend_equation_advanced_coherent GL_OES_texture_storage_multisample_2d_array GL_OES_shader_image_atomic GL_EXT_robustness GL_EXT_draw_buffers_indexed GL_OES_draw_buffers_indexed GL_EXT_texture_border_clamp GL_OES_texture_border_clamp GL_EXT_texture_cube_map_array GL_OES_texture_cube_map_array GL_OES_sample_variables GL_OES_sample_shading GL_OES_shader_multisample_interpolation GL_EXT_shader_io_blocks GL_OES_shader_io_blocks GL_EXT_tessellation_shader GL_OES_tessellation_shader GL_EXT_primitive_bounding_box GL_OES_primitive_bounding_
2021-04-07 15:14:00.720 17317-17357/com.MyCompany.MyGame D/Unity: box GL_EXT_geometry_shader GL_OES_geometry_shader GL_ANDROID_extension_pack_es31a GL_EXT_gpu_shader5 GL_OES_gpu_shader5 GL_EXT_texture_buffer GL_OES_texture_buffer GL_EXT_copy_image GL_OES_copy_image GL_EXT_shader_non_constant_global_initializers GL_EXT_color_buffer_half_float GL_EXT_color_buffer_float GL_EXT_YUV_target GL_OVR_multiview GL_OVR_multiview2 GL_OVR_multiview_multisampled_render_to_texture GL_KHR_robustness GL_KHR_robust_buffer_access_behavior GL_EXT_draw_elements_base_vertex GL_OES_draw_elements_base_vertex GL_EXT_protected_textures GL_EXT_buffer_storage GL_EXT_external_buffer GL_EXT_EGL_image_array
2021-04-07 15:14:00.734 17317-17401/com.MyCompany.MyGame I/SwappyDisplayManager: Starting looper thread
2021-04-07 15:14:00.764 17317-17357/com.MyCompany.MyGame I/libOpenSLES: Emulating old channel mask behavior (ignoring positional mask 0x3, using default mask 0x3 based on channel count of 2)
2021-04-07 15:14:00.774 17317-17357/com.MyCompany.MyGame W/AudioTrack: createTrack_l(0): AUDIO_OUTPUT_FLAG_FAST denied by server; frameCount 0 -> 1924
2021-04-07 15:14:00.940 17317-17357/com.MyCompany.MyGame I/MONO: Getting locale
2021-04-07 15:14:00.940 17317-17357/com.MyCompany.MyGame I/Mono: Locale en-MD
2021-04-07 15:14:01.184 17317-17357/com.MyCompany.MyGame V/AudioManager: getStreamVolume streamType: 3 volume: 2
2021-04-07 15:14:01.193 17317-17357/com.MyCompany.MyGame V/MediaRouter: Selecting route: RouteInfo{ name=Phone, description=null, status=null, category=RouteCategory{ name=System types=ROUTE_TYPE_LIVE_AUDIO ROUTE_TYPE_LIVE_VIDEO groupable=false }, supportedTypes=ROUTE_TYPE_LIVE_AUDIO ROUTE_TYPE_LIVE_VIDEO , presentationDisplay=null }
2021-04-07 15:14:01.587 17317-17317/com.MyCompany.MyGame W/UnityMain: type=1400 audit(0.0:6933): avc: granted { read } for pid=17317 name="8ed5ebc4840489a57fabfcc302306155" dev="sdcardfs" ino=41785 scontext=u:r:untrusted_app:s0:c245,c256,c512,c768 tcontext=u:object_r:sdcardfs:s0 tclass=file
2021-04-07 15:14:01.982 17317-17330/com.MyCompany.MyGame I/m.MyCompany.MyGame: GcSupervisor: module enable:1
2021-04-07 15:14:03.533 17317-17357/com.MyCompany.MyGame V/AudioManager: getStreamVolume streamType: 3 volume: 2
2021-04-07 15:14:03.606 17317-17357/com.MyCompany.MyGame D/SensorManager: 0xb4e264c0 addFd fd=129
2021-04-07 15:14:03.703 17317-17357/com.MyCompany.MyGame W/Gralloc3: allocator 3.x is not supported
2021-04-07 15:14:04.031 17317-17317/com.MyCompany.MyGame W/UnityMain: type=1400 audit(0.0:6935): avc: granted { read } for pid=17317 name="7bd3b4937d9eddcae16dbb191d1a86db" dev="sdcardfs" ino=40438 scontext=u:r:untrusted_app:s0:c245,c256,c512,c768 tcontext=u:object_r:sdcardfs:s0 tclass=file
2021-04-07 15:14:04.031 17317-17317/com.MyCompany.MyGame W/UnityMain: type=1400 audit(0.0:6936): avc: granted { read open } for pid=17317 path="/storage/emulated/0/Android/data/com.MyCompany.MyGame/cache/UnityShaderCache/7bd3b4937d9eddcae16dbb191d1a86db" dev="sdcardfs" ino=40438 scontext=u:r:untrusted_app:s0:c245,c256,c512,c768 tcontext=u:object_r:sdcardfs:s0 tclass=file
2021-04-07 15:14:04.035 17317-17317/com.MyCompany.MyGame W/UnityMain: type=1400 audit(0.0:6939): avc: granted { read } for pid=17317 name="bf713628390495c6ec745eec555d4a09" dev="sdcardfs" ino=41718 scontext=u:r:untrusted_app:s0:c245,c256,c512,c768 tcontext=u:object_r:sdcardfs:s0 tclass=file
2021-04-07 15:14:04.068 17317-17357/com.MyCompany.MyGame E/MyCompany: Called from UnityMain
2021-04-07 15:14:04.068 17317-17357/com.MyCompany.MyGame W/OpenGLRenderer: [SurfaceTexture-34-17317-0] bindTextureImage: clearing GL error: 0x502
2021-04-07 15:14:04.069 17317-17357/com.MyCompany.MyGame E/OpenGLRenderer: [SurfaceTexture-34-17317-0] bindTextureImage: error binding external image: 0x502
2021-04-07 15:14:04.138 17317-17357/com.MyCompany.MyGame E/Unity: AndroidJavaException: java.lang.RuntimeException: Error during updateTexImage (see logcat for details)
java.lang.RuntimeException: Error during updateTexImage (see logcat for details)
at android.graphics.SurfaceTexture.nativeUpdateTexImage(Native Method)
at android.graphics.SurfaceTexture.updateTexImage(SurfaceTexture.java:248)
at com.unity3d.player.CamMonitor.updateSurfaceTexture(CamMonitor.java:108)
at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
at com.unity3d.player.UnityPlayer.access$300(Unknown Source:0)
at com.unity3d.player.UnityPlayer$e$1.handleMessage(Unknown Source:95)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:213)
at com.unity3d.player.UnityPlayer$e.run(Unknown Source:20)
at UnityEngine.AndroidJNISafe.CheckException () [0x00096] in <2def9ed1a9834118bfbf55b64b4d3069>:0
at UnityEngine.AndroidJNISafe.CallVoidMethod (System.IntPtr obj, System.IntPtr methodID, UnityEngine.jvalue[] args) [0x0000f] in <2def9ed1a9834118bfbf55
2021-04-07 15:14:04.144 17317-17357/com.MyCompany.MyGame E/MyCompany: Called from UnityMain
It was occurring because Unity was using Vulkan instead of OpenGLES all along. Disabling the Automatic Graphics API in the Player settings and dragging OpenGLES2 on top fixes the issue.