-
-
Notifications
You must be signed in to change notification settings - Fork 484
/
Copy pathDemoGraph.cs
198 lines (158 loc) · 5.82 KB
/
DemoGraph.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using Mediapipe;
using System;
using System.Collections.Generic;
using UnityEngine;
using Stopwatch = System.Diagnostics.Stopwatch;
public abstract class DemoGraph : MonoBehaviour, IDemoGraph<TextureFrame> {
[SerializeField] protected TextAsset gpuConfig = null;
[SerializeField] protected TextAsset cpuConfig = null;
[SerializeField] protected TextAsset androidConfig = null;
GameObject resourceManager;
protected const string inputStream = "input_video";
protected Stopwatch stopwatch;
protected static CalculatorGraph graph;
protected static GlCalculatorHelper gpuHelper;
protected static Timestamp currentTimestamp;
#if UNITY_ANDROID
static readonly object frameLock = new object();
static TextureFrame currentTextureFrame;
static IntPtr currentTextureName;
#endif
void OnEnable() {
resourceManager = GameObject.Find("ResourceManager");
}
protected virtual void OnDestroy() {
Stop();
if (graph != null) {
graph.Dispose();
graph = null;
}
gpuHelper = null;
if (stopwatch != null && stopwatch.IsRunning) {
stopwatch.Stop();
}
}
public virtual void Initialize() {
PrepareDependentAssets();
Debug.Log("Loaded dependent assets");
var config = GetConfig();
if (config == null) {
Debug.LogError("config is missing");
return;
}
graph = new CalculatorGraph(config.text);
stopwatch = new Stopwatch();
}
public void Initialize(GpuResources gpuResources, GlCalculatorHelper gpuHelper) {
DemoGraph.gpuHelper = gpuHelper;
this.Initialize();
graph?.SetGpuResources(gpuResources).AssertOk();
}
public abstract Status StartRun();
public virtual Status StartRun(Texture texture) {
stopwatch.Start();
return StartRun();
}
public virtual Status PushInput(TextureFrame textureFrame) {
currentTimestamp = GetCurrentTimestamp();
#if UNITY_ANDROID && !UNITY_EDITOR
if (IsGpuEnabled()) {
lock (frameLock) {
currentTextureFrame = textureFrame;
currentTextureName = textureFrame.GetNativeTexturePtr();
return gpuHelper.RunInGlContext(PushInputInGlContext);
}
}
#endif
var imageFrame = new ImageFrame(
ImageFormat.Format.SRGBA, textureFrame.width, textureFrame.height, 4 * textureFrame.width, textureFrame.GetRawNativeByteArray());
textureFrame.Release();
var packet = new ImageFramePacket(imageFrame, currentTimestamp);
return graph.AddPacketToInputStream(inputStream, packet);
}
#if UNITY_ANDROID
/// <remarks>
/// <see cref="currentTimestamp" />, <see cref="currentTextureFrame" /> and <see cref="currentTextureName" /> must be set before calling.
/// </remarks>
[AOT.MonoPInvokeCallback(typeof(GlCalculatorHelper.NativeGlStatusFunction))]
static IntPtr PushInputInGlContext() {
try {
var glContext = GlContext.GetCurrent();
var glTextureBuffer = new GlTextureBuffer((UInt32)currentTextureName, currentTextureFrame.width, currentTextureFrame.height,
currentTextureFrame.gpuBufferformat, currentTextureFrame.OnRelease, glContext);
var gpuBuffer = new GpuBuffer(glTextureBuffer);
// TODO: ensure the returned status won't be garbage collected prematurely.
return graph.AddPacketToInputStream(inputStream, new GpuBufferPacket(gpuBuffer, currentTimestamp)).mpPtr;
} catch (Exception e) {
return Status.FailedPrecondition(e.ToString()).mpPtr;
}
}
#endif
public abstract void RenderOutput(WebCamScreenController screenController, TextureFrame textureFrame);
public void Stop() {
if (graph == null) { return; }
using (var status = graph.CloseAllPacketSources()) {
if (!status.ok) {
Debug.LogError(status.ToString());
}
}
using (var status = graph.WaitUntilDone()) {
if (!status.ok) {
Debug.LogError(status.ToString());
}
}
}
/// <summary>
/// Fetch next value from <paramref name="poller" />.
/// Note that this method blocks the thread till the next value is fetched.
/// If the next value is empty, this method never returns.
/// </summary>
public T FetchNext<T>(OutputStreamPoller<T> poller, Packet<T> packet, string streamName = null, T failedValue = default(T)) {
if (!poller.Next(packet)) { // blocks
if (streamName != null) {
Debug.LogWarning($"Failed to fetch next packet from {streamName}");
}
return failedValue;
}
return packet.Get();
}
/// <summary>
/// Fetch next vector value from <paramref name="poller" />.
/// </summary>
/// <returns>
/// Fetched vector or an empty List when failed.
/// </returns>
/// <seealso cref="FetchNext" />
public List<T> FetchNextVector<T>(OutputStreamPoller<List<T>> poller, Packet<List<T>> packet, string streamName = null) {
var nextValue = FetchNext<List<T>>(poller, packet, streamName);
return nextValue == null ? new List<T>() : nextValue;
}
protected bool IsGpuEnabled() {
return gpuHelper != null;
}
protected TextAsset GetConfig() {
if (!IsGpuEnabled()) {
return cpuConfig;
}
#if UNITY_ANDROID && !UNITY_EDITOR
if (androidConfig != null) {
return androidConfig;
}
#endif
return gpuConfig;
}
protected Timestamp GetCurrentTimestamp() {
if (stopwatch == null || !stopwatch.IsRunning) {
return Timestamp.Unset();
}
var microseconds = (stopwatch.ElapsedTicks) / (TimeSpan.TicksPerMillisecond / 1000);
return new Timestamp(microseconds);
}
protected virtual void PrepareDependentAssets() {}
protected void PrepareDependentAsset(string assetName, string uniqueKey, bool overwrite = false) {
resourceManager.GetComponent<AssetLoader>().PrepareAsset(assetName, uniqueKey, overwrite);
}
protected void PrepareDependentAsset(string assetName, bool overwrite = false) {
PrepareDependentAsset(assetName, assetName, overwrite);
}
}