@@ -36,6 +36,9 @@ public sealed unsafe class ImGuiEGLRenderer : IImGuiRenderer
3636 /// <summary> 静态安装入口(供原生宿主调用) </summary>
3737 public static bool Install ( ) => ( s_instance = new ImGuiEGLRenderer ( ) ) . InstallInstance ( ) ;
3838
39+ internal static bool Install ( AndroidImGuiStartup startup ) =>
40+ ( s_instance = new ImGuiEGLRenderer { _startup = startup } ) . InstallInstance ( ) ;
41+
3942 /// <summary> 静态 OnRender 事件(Install 之前订阅会缓存) </summary>
4043 private static Action ? s_pendingOnRender ;
4144
@@ -58,6 +61,7 @@ public static event Action OnRender
5861 }
5962
6063 private bool _initialized ;
64+ private AndroidImGuiStartup ? _startup ;
6165 private Action _onRender = ( ) => { } ;
6266
6367 event Action IImGuiRenderer . OnRender
@@ -74,18 +78,16 @@ event Action IImGuiRenderer.OnRender
7478
7579 private bool InstallInstance ( )
7680 {
77- HookHelper . Instance = new DobbyHook ( ) ;
78- EglHooks . InstallHooks ( ) ;
79- EglHooks . OnEglSwapBuffers = OnSwapBuffers ;
80-
81-
82-
81+ _startup ??= new AndroidImGuiStartup ( ) ;
8382 // 回放 Install 之前缓存的静态事件订阅
8483 if ( s_pendingOnRender != null )
8584 {
8685 _onRender += s_pendingOnRender ;
8786 s_pendingOnRender = null ;
8887 }
88+ HookHelper . Instance = new DobbyHook ( ) ;
89+ EglHooks . InstallHooks ( ) ;
90+ EglHooks . OnEglSwapBuffers = OnSwapBuffers ;
8991 return true ;
9092 }
9193
@@ -111,7 +113,9 @@ public static int OnSwapBuffers(IntPtr display, IntPtr surface)
111113
112114 if ( ! self . _initialized )
113115 {
114- self . InitImGui ( display , surface ) ;
116+ self . InitImGui ( ) ;
117+ if ( ! self . _initialized )
118+ return EglHooks . HookEglSwapBuffersOriginal ( display , surface ) ;
115119 }
116120
117121 // 使用官方 backend 的 NewFrame
@@ -132,7 +136,9 @@ public static int OnSwapBuffers(IntPtr display, IntPtr surface)
132136 StArray . ModManager . Android . Native . AndroidJavaInputBridge . UpdateCaptureState ( ) ;
133137 // 渲染
134138 ImGui . Render ( ) ;
135- ImGuiImplOpenGL3 . RenderDrawData ( ( IntPtr ) ImGui . GetDrawData ( ) . NativePtr ) ;
139+ var drawData = ImGui . GetDrawData ( ) ;
140+ if ( drawData . CmdListsCount != 0 )
141+ ImGuiImplOpenGL3 . RenderDrawData ( ( IntPtr ) drawData . NativePtr ) ;
136142
137143 // 渲染后检查 surface 是否已被废弃
138144 var err = Egl . GetError ( ) ;
@@ -149,21 +155,12 @@ public static int OnSwapBuffers(IntPtr display, IntPtr surface)
149155 return EglHooks . HookEglSwapBuffersOriginal ( display , surface ) ;
150156 }
151157
152- private void InitImGui ( IntPtr display , IntPtr surface )
158+ private void InitImGui ( )
153159 {
154- if ( _initialized ) return ;
155- // 从 EGL surface 获取 ANativeWindow
156- if ( ! Egl . QuerySurface ( display , surface , Egl . WIDTH , out var width ) ||
157- ! Egl . QuerySurface ( display , surface , Egl . HEIGHT , out var height ) )
158- {
159- Logger . Error ( nameof ( ImGuiEGLRenderer ) , "Failed to query EGL surface for initialization" ) ;
160+ if ( ! _startup ! . TryInitializeContext ( ) )
160161 return ;
161- }
162- // 创建 ImGui 上下文 + 加载嵌入式字体 msyh + FA 图标(共享接口)
163- ( ( IImGuiRenderer ) this ) . InitImGui ( ) ;
164- ImGuiInputHandler . IsInitialized = true ;
162+
165163 var io = ImGui . GetIO ( ) ;
166- io . ConfigFlags |= ImGuiConfigFlags . NavEnableKeyboard ;
167164 io . FontGlobalScale = 3.0f ;
168165
169166 // 设置样式
@@ -179,6 +176,7 @@ private void InitImGui(IntPtr display, IntPtr surface)
179176 }
180177
181178 ImGuiImplOpenGL3 . Init ( ) ;
179+ ImGuiInputHandler . IsInitialized = true ;
182180 ImGuiInputHandler . InstallInputHooks ( ) ;
183181 _initialized = true ;
184182 }
0 commit comments