Skip to content

Commit ecf7e2d

Browse files
committed
Test cleanup, etc
1 parent 21b8eb2 commit ecf7e2d

6 files changed

Lines changed: 96 additions & 15 deletions

File tree

OVRSharp.Graphics.DirectX/DirectXCompositor.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,45 @@ namespace OVRSharp.Graphics.DirectX
1010
{
1111
public class DirectXCompositor : ICompositorAPI
1212
{
13+
/// <summary>
14+
/// Global, static instance of <see cref="DirectXCompositor"/>.
15+
/// </summary>
16+
public static DirectXCompositor Instance
17+
{
18+
get
19+
{
20+
if (_instance == null)
21+
_instance = new DirectXCompositor();
22+
23+
return _instance;
24+
}
25+
}
26+
27+
private static DirectXCompositor _instance = null;
28+
1329
private readonly Device device;
1430

31+
/// <summary>
32+
/// A DirectX-based implementation of <see cref="ICompositorAPI"/>.<br/><br/>
33+
///
34+
/// Ideally, there should ever only be one instance of this class
35+
/// at once. You can provide <see cref="Instance"/> to anything that depends
36+
/// on <see cref="ICompositorAPI"/> to ensure that this is the case.
37+
/// </summary>
1538
public DirectXCompositor()
1639
{
1740
device = new Device(SharpDX.Direct3D.DriverType.Hardware, DeviceCreationFlags.Debug);
1841
}
1942

43+
/// <summary>
44+
/// <inheritdoc/><br/><br/>
45+
///
46+
/// <strong>Warning:</strong> this is a pretty slow method.
47+
/// It's fine to use this for one-off captures, but if you require
48+
/// something like a constant stream of the headset view, I recommend
49+
/// digging into a lower-level implementation.
50+
/// </summary>
51+
/// <inheritdoc/>
2052
public Bitmap GetMirrorImage(EVREye eye = EVREye.Eye_Left)
2153
{
2254
var srvPtr = IntPtr.Zero;
@@ -46,7 +78,7 @@ public Bitmap GetMirrorImage(EVREye eye = EVREye.Eye_Left)
4678
Usage = ResourceUsage.Staging
4779
}))
4880
{
49-
// Copy texture to CPU so we can read from it
81+
// Copy texture to RAM so CPU can read from it
5082
device.ImmediateContext.CopyResource(tex, cpuTex);
5183
OpenVR.Compositor.ReleaseMirrorTextureD3D11(srvPtr);
5284

OVRSharp/Application.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public enum ApplicationType
2020

2121
/// <summary>
2222
/// The application will not start SteamVR. If it is not already running
23-
/// the call with VR_Init will fail with VRInitError_Init_NoServerForBackgroundApp.
23+
/// the call with VR_Init will fail with <see cref="EVRInitError.Init_NoServerForBackgroundApp"/>.
2424
/// </summary>
2525
Background = EVRApplicationType.VRApplication_Background,
2626

OVRSharp/Graphics/ICompositorAPI.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,20 @@
33

44
namespace OVRSharp.Graphics
55
{
6+
/// <summary>
7+
/// An interface for graphics-related compositor API methods.<br/><br/>
8+
///
9+
/// You can find implementations for different graphics APIs on NuGet as
10+
/// OVRSharp.Graphics.DirectX and OVRSharp.Graphics.OpenGL. Anyone else is also
11+
/// free to implement their own version of this for other graphics APIs and
12+
/// publish them.
13+
/// </summary>
614
public interface ICompositorAPI
715
{
16+
/// <summary>
17+
/// Capture a screenshot of the headset view.
18+
/// </summary>
19+
/// <param name="eye">The eye to capture.</param>
820
Bitmap GetMirrorImage(EVREye eye = EVREye.Eye_Left);
921
}
1022
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
using NUnit.Framework;
12
using OVRSharp.Tests.Graphics;
23

34
namespace OVRSharp.Graphics.DirectX.Tests
45
{
5-
public class DirectXCompositorTests : CompositorTests<DirectXCompositor> { }
6+
[TestFixture]
7+
public class DirectXCompositorTests : CompositorTests<DirectXCompositor>
8+
{
9+
protected override DirectXCompositor InstantiateCompositorAPI() => DirectXCompositor.Instance;
10+
}
611
}
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
using FluentAssertions;
2-
using NUnit.Framework;
1+
using NUnit.Framework;
32
using OVRSharp.Tests.Graphics;
4-
using Valve.VR;
53

64
namespace OVRSharp.Graphics.OpenGL.Tests
75
{
8-
public class OpenGLCompositorTests : CompositorTests<OpenGLCompositor> { }
6+
[TestFixture]
7+
public class OpenGLCompositorTests : CompositorTests<OpenGLCompositor>
8+
{
9+
protected override OpenGLCompositor InstantiateCompositorAPI()
10+
{
11+
return new OpenGLCompositor();
12+
}
13+
}
914
}

tests/OVRSharp.Tests/Graphics/CompositorTests.cs

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,50 @@
11
using FluentAssertions;
22
using NUnit.Framework;
33
using OVRSharp.Graphics;
4+
using OVRSharp.Exceptions;
45
using Valve.VR;
56

67
namespace OVRSharp.Tests.Graphics
78
{
8-
public class CompositorTests<T> where T : ICompositorAPI, new()
9+
/// <summary>
10+
/// Tests to run against <see cref="ICompositorAPI"/> implementations.
11+
/// </summary>
12+
/// <typeparam name="T">Your implementation of <see cref="ICompositorAPI"/> to test.</typeparam>
13+
public abstract class CompositorTests<T> where T : ICompositorAPI
914
{
1015
private Application app;
1116
private ICompositorAPI compositor;
1217

18+
/// <summary>
19+
/// Instantiates the instance of your <see cref="ICompositorAPI"/>
20+
/// that will be used for testing.
21+
/// </summary>
22+
protected abstract T InstantiateCompositorAPI();
23+
1324
[OneTimeSetUp]
1425
public void Setup()
1526
{
16-
app = new Application(Application.ApplicationType.Background);
17-
compositor = new T();
27+
try
28+
{
29+
app = new Application(Application.ApplicationType.Background);
30+
}
31+
catch(OpenVRSystemException<EVRInitError> e)
32+
{
33+
switch(e.Error)
34+
{
35+
case EVRInitError.Init_InstallationNotFound:
36+
case EVRInitError.Init_VRClientDLLNotFound:
37+
Assert.Ignore("OpenVR runtime not found; skipping integration tests.");
38+
break;
39+
case EVRInitError.Init_NoServerForBackgroundApp:
40+
Assert.Ignore("OpenVR runtime not running; skipping integration tests.");
41+
break;
42+
default:
43+
throw;
44+
}
45+
}
46+
47+
compositor = InstantiateCompositorAPI();
1848
}
1949

2050
[Test]
@@ -29,16 +59,13 @@ public void ShouldGetMirrorTextureSuccessfully(EVREye eye)
2959

3060
// This test is mostly here to make sure we are deallocating resources properly.
3161
[Test]
32-
public void ShouldWithstandRapidCalls()
62+
public void ShouldWorkWhenCalledRapidly()
3363
{
34-
Assert.Ignore();
35-
for (var i = 0; i < 1000; i++)
64+
for (var i = 0; i < 100; i++)
3665
{
3766
var bitmap = compositor.GetMirrorImage(EVREye.Eye_Left);
3867
bitmap.Height.Should().BeGreaterThan(0);
3968
bitmap.Width.Should().BeGreaterThan(0);
40-
41-
bitmap.Dispose();
4269
}
4370
}
4471
}

0 commit comments

Comments
 (0)