9
9
using UnityEngine ;
10
10
using UnityEngine . Rendering ;
11
11
12
- #if UNITY_ANDROID
13
- using System . Runtime . InteropServices ;
14
- #endif
15
-
16
12
namespace Mediapipe . Unity
17
13
{
18
14
public static class GpuManager
@@ -22,10 +18,7 @@ public static class GpuManager
22
18
private delegate void PluginCallback ( int eventId ) ;
23
19
24
20
private static readonly object _SetupLock = new object ( ) ;
25
- #pragma warning disable IDE0044
26
- private static IntPtr _CurrentContext = IntPtr . Zero ;
27
- #pragma warning restore IDE0044
28
- private static bool _IsContextInitialized = false ;
21
+ private static IntPtr _PlatformGlContext = IntPtr . Zero ;
29
22
30
23
public static GpuResources GpuResources { get ; private set ; }
31
24
public static GlCalculatorHelper GlCalculatorHelper { get ; private set ; }
@@ -50,48 +43,21 @@ public static IEnumerator Initialize()
50
43
yield break ;
51
44
}
52
45
53
- #if UNITY_ANDROID
54
- _IsContextInitialized = SystemInfo . graphicsDeviceType != GraphicsDeviceType . OpenGLES3 ;
55
- if ( ! _IsContextInitialized )
56
- {
57
- PluginCallback callback = GetCurrentContext ;
58
-
59
- var fp = Marshal . GetFunctionPointerForDelegate ( callback ) ;
60
- GL . IssuePluginEvent ( fp , 1 ) ;
61
- }
62
- #else
63
- _IsContextInitialized = true ;
64
- #endif
65
-
66
- var count = 100 ;
67
- yield return new WaitUntil ( ( ) =>
68
- {
69
- return -- count < 0 || _IsContextInitialized ;
70
- } ) ;
71
-
72
- if ( ! _IsContextInitialized )
73
- {
74
- Logger . LogError ( _TAG , "Failed to get GlContext" ) ;
75
- yield break ;
76
- }
77
-
78
- #if UNITY_ANDROID
79
- if ( _CurrentContext == IntPtr . Zero )
80
- {
81
- Logger . LogWarning ( _TAG , "EGL context is not found, so MediaPipe won't share their EGL contexts with Unity" ) ;
82
- }
83
- else
46
+ if ( SystemInfo . graphicsDeviceType == GraphicsDeviceType . OpenGLES3 )
84
47
{
85
- Logger . LogVerbose ( _TAG , $ "EGL context is found: { _CurrentContext } ") ;
48
+ var req = AsyncGlContext . Request ( OnGetEglContext ) ;
49
+ yield return new WaitUntil ( ( ) => req . done ) ;
50
+
51
+ if ( req . error != null )
52
+ {
53
+ Logger . LogException ( req . error ) ;
54
+ yield break ;
55
+ }
86
56
}
87
- #endif
88
57
89
58
try
90
59
{
91
- Logger . LogInfo ( _TAG , "Initializing GpuResources..." ) ;
92
- GpuResources = GpuResources . Create ( _CurrentContext ) ;
93
-
94
- Logger . LogInfo ( _TAG , "Initializing GlCalculatorHelper..." ) ;
60
+ GpuResources = GpuResources . Create ( _PlatformGlContext ) ;
95
61
GlCalculatorHelper = new GlCalculatorHelper ( ) ;
96
62
GlCalculatorHelper . InitializeForTest ( GpuResources ) ;
97
63
@@ -133,13 +99,30 @@ public static void Shutdown()
133
99
IsInitialized = false ;
134
100
}
135
101
136
- // Currently, it works only on Android
137
- #if UNITY_ANDROID
138
- [ AOT . MonoPInvokeCallback ( typeof ( PluginCallback ) ) ]
139
- private static void GetCurrentContext ( int eventId ) {
140
- _CurrentContext = Egl . GetCurrentContext ( ) ;
141
- _IsContextInitialized = true ;
102
+ public static void ResetGpuResources ( IntPtr platformGlContext )
103
+ {
104
+ if ( ! IsInitialized )
105
+ {
106
+ throw new InvalidOperationException ( "GpuManager is not initialized" ) ;
107
+ }
108
+ GpuResources ? . Dispose ( ) ;
109
+
110
+ GpuResources = new GpuResources ( platformGlContext ) ;
111
+ GlCalculatorHelper . InitializeForTest ( GpuResources ) ;
112
+ }
113
+
114
+ public static GlContext GetGlContext ( ) => GlCalculatorHelper ? . GetGlContext ( ) ;
115
+
116
+ private static void OnGetEglContext ( AsyncGlContextRequest request )
117
+ {
118
+ if ( request . platformGlContext == IntPtr . Zero )
119
+ {
120
+ Logger . LogWarning ( _TAG , "EGL context is not found, so MediaPipe won't share their EGL contexts with Unity" ) ;
121
+ return ;
122
+ }
123
+ Logger . LogVerbose ( _TAG , $ "EGL context is found: { request . platformGlContext } ") ;
124
+
125
+ _PlatformGlContext = request . platformGlContext ;
142
126
}
143
- #endif
144
127
}
145
128
}
0 commit comments