-
Notifications
You must be signed in to change notification settings - Fork 1
/
Frame.cs
133 lines (113 loc) · 4.49 KB
/
Frame.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
using System;
using System.Threading;
using System.Threading.Tasks;
namespace VVVV.DX11.ImagePlayer
{
class Frame : IDisposable
{
string filename;
IDecoder decoder;
public bool WaitForFrame { get; set; }
public SlimDX.Direct3D11.Texture2DDescription Description { get; private set; }
public bool Loaded { get; private set; }
public double LoadTime { get; private set; }
public double SwapTime { get; private set; }
Task LoadTask;
readonly System.Diagnostics.Stopwatch sw;
readonly CancellationTokenSource cts;
readonly RefCounter RefCounter;
readonly VVVV.Core.Logging.ILogger FLogger;
public Frame(string name, IDecoder decoder, VVVV.Core.Logging.ILogger logger)
{
WaitForFrame = true;
Loaded = false;
filename = name;
this.decoder = decoder;
sw = new System.Diagnostics.Stopwatch();
cts = new CancellationTokenSource();
RefCounter = new RefCounter();
FLogger = logger;
}
public void LoadAsync()
{
LoadTask = Task.Factory.StartNew(() => { Load(cts.Token); }, cts.Token, TaskCreationOptions.PreferFairness, TaskScheduler.Default);
LoadTask.ContinueWith((prev) => { LogExceptions(prev, "while reading"); }, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.Default);
var disposeTask = LoadTask.ContinueWith(prev => { DisposeAsync(); }, CancellationToken.None, TaskContinuationOptions.OnlyOnCanceled, TaskScheduler.Default);
disposeTask.ContinueWith((prev) => { LogExceptions(prev, "while disposing"); }, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.Default);
}
private void LogExceptions(Task previous, string topic)
{
foreach (var e in previous.Exception.InnerExceptions)
FLogger.Log(VVVV.Core.Logging.LogType.Debug, e.GetType().ToString() + topic + ": " + e.Message);
}
private unsafe void Load(CancellationToken token)
{
sw.Start();
try
{
token.ThrowIfCancellationRequested();
decoder.Load(filename, token);
Description = decoder.Description;
Loaded = true;
}
//catch (OperationCanceledException) { }
finally
{
LoadTime = sw.Elapsed.TotalMilliseconds;
}
}
public FeralTic.DX11.Resources.DX11ResourceTexture2D SetSRV(FeralTic.DX11.Resources.DX11ResourceTexture2D texture, FeralTic.DX11.DX11RenderContext context)
{
if (texture == null)
texture = new FeralTic.DX11.Resources.DX11ResourceTexture2D(context);
if (texture.Meta != this.filename && (WaitForFrame || this.Loaded))
{
sw.Restart();
var handle = RefCounter.Use();
if (!this.Loaded)
{
try
{
LoadTask.Wait();
}
catch (AggregateException) // .Wait can surface the exception
{
handle.Dispose();
LogExceptions(LoadTask, "while waiting");
}
}
if (this.Loaded)
{
texture.SetBySRV(decoder.SRV, handle);
SwapTime = sw.Elapsed.TotalMilliseconds;
}
}
return texture;
}
void DisposeAsync()
{
while (!RefCounter.Free)
{
// we're not in a hurry when disposing, give time to other tasks to finish
Thread.Sleep(3);
}
decoder.Dispose();
cts.Dispose();
}
public void Dispose()
{
if (LoadTask != null)
{
if (!Loaded && LoadTask.Status != TaskStatus.Faulted && LoadTask.Status != TaskStatus.Canceled)
cts.Cancel();
else
Task.Factory.StartNew(() => DisposeAsync(), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
}
else
{
decoder.Dispose();
cts.Dispose();
}
}
}
}