-
-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use AsyncGlContext.Request to get GlContext
- Loading branch information
Showing
3 changed files
with
120 additions
and
52 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Unity/AsyncGlContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright (c) 2021 homuler | ||
// | ||
// Use of this source code is governed by an MIT-style | ||
// license that can be found in the LICENSE file or at | ||
// https://opensource.org/licenses/MIT. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.Threading; | ||
using UnityEngine; | ||
|
||
namespace Mediapipe.Unity | ||
{ | ||
public static class AsyncGlContext | ||
{ | ||
public static AsyncGlContextRequest Request(Action<AsyncGlContextRequest> callback) => new AsyncGlContextRequest(callback); | ||
} | ||
|
||
public class AsyncGlContextRequest | ||
{ | ||
private static int _Counter = 0; | ||
private static readonly GlobalInstanceTable<int, AsyncGlContextRequest> _InstanceTable = new GlobalInstanceTable<int, AsyncGlContextRequest>(5); | ||
|
||
private delegate void GLEventCallback(int eventId); | ||
|
||
private readonly int _id; | ||
private readonly Action<AsyncGlContextRequest> _callback; | ||
|
||
public IntPtr platformGlContext { get; private set; } | ||
public bool done { get; private set; } | ||
public Exception error { get; private set; } | ||
|
||
internal AsyncGlContextRequest(Action<AsyncGlContextRequest> callback) | ||
{ | ||
_id = Interlocked.Increment(ref _Counter); | ||
_callback = callback; | ||
_InstanceTable.Add(_id, this); | ||
|
||
GLEventCallback gLEventCallback = PluginCallback; | ||
var fp = Marshal.GetFunctionPointerForDelegate(gLEventCallback); | ||
|
||
GL.IssuePluginEvent(fp, _id); | ||
} | ||
|
||
[AOT.MonoPInvokeCallback(typeof(GLEventCallback))] | ||
private static void PluginCallback(int eventId) | ||
{ | ||
if (!_InstanceTable.TryGetValue(eventId, out var request)) | ||
{ | ||
Logger.LogWarning($"AsyncGlContextRequest with id {eventId} is not found, maybe already GCed"); | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
#if UNITY_ANDROID | ||
// Currently, it works only on Android | ||
request.platformGlContext = Egl.GetCurrentContext(); | ||
#endif | ||
|
||
request._callback?.Invoke(request); | ||
} | ||
catch (Exception e) | ||
{ | ||
request.error = e; | ||
} | ||
finally | ||
{ | ||
request.done = true; | ||
_InstanceTable.Remove(eventId); | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
Packages/com.github.homuler.mediapipe/Runtime/Scripts/Unity/AsyncGlContext.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters