Skip to content

Commit

Permalink
Mac: Only create GLArea when actually using OpenGL
Browse files Browse the repository at this point in the history
- Use net48/net5.0 for test projects
  • Loading branch information
cwensley committed May 30, 2021
1 parent 34a0c32 commit 9d787a9
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 38 deletions.
10 changes: 5 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.Gtk/Debug/net5.0/linux-x64/TestEtoVeldrid.Gtk.dll"
},
"windows": {
"program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.Wpf/Debug/net5.0/win-x64/TestEtoVeldrid.Wpf.dll"
"program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.Wpf/Debug/net5.0-windows/win-x64/TestEtoVeldrid.Wpf.dll"
},
"console": "internalConsole",
"stopAtEntry": false
Expand All @@ -29,11 +29,11 @@
"preLaunchTask": "build",
"program": "",
"osx": {
"program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.Mac64/Debug/net472/osx-x64/TestEtoVeldrid.Mac64.app/Contents/MacOS/TestEtoVeldrid.Mac64",
"program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.Mac64/Debug/net48/osx-x64/TestEtoVeldrid.Mac64.app/Contents/MacOS/TestEtoVeldrid.Mac64",
"useRuntime": false
},
"linux": {
"program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.Gtk/Debug/net472/linux-x64/TestEtoVeldrid.Gtk.exe"
"program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.Gtk/Debug/net48/linux-x64/TestEtoVeldrid.Gtk.exe"
},
"console": "internalConsole"
},
Expand All @@ -44,7 +44,7 @@
"preLaunchTask": "build",
"program": "",
"windows": {
"program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.Wpf/Debug/net472/win-x64/TestEtoVeldrid.Wpf.exe"
"program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.Wpf/Debug/net48/win-x64/TestEtoVeldrid.Wpf.exe"
},
"console": "internalConsole"
},
Expand All @@ -55,7 +55,7 @@
"preLaunchTask": "build",
"program": "",
"windows": {
"program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.WinForms/Debug/net472/win-x64/TestEtoVeldrid.WinForms.exe"
"program": "${workspaceFolder}/artifacts/bin/TestEtoVeldrid.WinForms/Debug/net48/win-x64/TestEtoVeldrid.WinForms.exe"
},
"console": "internalConsole"
},
Expand Down
73 changes: 49 additions & 24 deletions src/Eto.Veldrid.Gtk/GtkVeldridSurfaceHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,18 @@

namespace Eto.Veldrid.Gtk
{
public class GtkVeldridSurfaceHandler : GtkControl<GLArea, VeldridSurface, VeldridSurface.ICallback>, VeldridSurface.IHandler, VeldridSurface.IOpenGL
public class GtkVeldridSurfaceHandler : GtkControl<EtoEventBox, VeldridSurface, VeldridSurface.ICallback>, VeldridSurface.IHandler, VeldridSurface.IOpenGL
{
GLArea glArea;
public Size RenderSize => Size.Round((SizeF)Widget.Size * Scale);

float Scale => Widget.ParentWindow?.Screen?.LogicalPixelSize ?? 1;

public override global::Gtk.Widget ContainerContentControl => glArea ?? base.ContainerContentControl;

public GtkVeldridSurfaceHandler()
{
Control = new GLArea();
Control.CanFocus = true;

// Veldrid technically supports as low as OpenGL 3.0, but the full
// complement of features is only available with 3.3 and higher.
Control.SetRequiredVersion(3, 3);

Control.HasDepthBuffer = true;
Control.HasStencilBuffer = true;

Control.Realized += Control_InitializeGraphicsBackend;
Control = new EtoEventBox { Handler = this };
}

public Swapchain CreateSwapchain()
Expand Down Expand Up @@ -64,24 +57,29 @@ public Swapchain CreateSwapchain()
return swapchain;
}

void Control_InitializeGraphicsBackend(object sender, EventArgs e)
void glArea_InitializeGraphicsBackend(object sender, EventArgs e)
{
Control.Context.MakeCurrent();
glArea.Context.MakeCurrent();
Callback.OnInitializeBackend(Widget, new InitializeEventArgs(RenderSize));

Control.Render += Control_Render;
Control.Resize += Control_Resize;
glArea.Render += glArea_Render;
glArea.Resize += glArea_Resize;
}

void Control_InitializeGraphicsBackend(object sender, EventArgs e)
{
Callback.OnInitializeBackend(Widget, new InitializeEventArgs(RenderSize));
}

bool skipDraw;

private void Control_Resize(object o, ResizeArgs args)
private void glArea_Resize(object o, ResizeArgs args)
{
skipDraw = false;
Callback.OnResize(Widget, new ResizeEventArgs(RenderSize));
}

void Control_Render(object o, RenderArgs args)
void glArea_Render(object o, RenderArgs args)
{
if (!skipDraw)
{
Expand All @@ -92,11 +90,11 @@ void Control_Render(object o, RenderArgs args)
}

// TODO: Figure this one out! The docstring for this property in Veldrid's OpenGLPlatformInfo is ambiguous.
IntPtr VeldridSurface.IOpenGL.OpenGLContextHandle => Control.Context.Handle;
IntPtr VeldridSurface.IOpenGL.OpenGLContextHandle => glArea?.Context.Handle ?? IntPtr.Zero;

IntPtr VeldridSurface.IOpenGL.GetProcAddress(string name) => X11Interop.glXGetProcAddress(name);

void VeldridSurface.IOpenGL.MakeCurrent(IntPtr context) => Control.MakeCurrent();
void VeldridSurface.IOpenGL.MakeCurrent(IntPtr context) => glArea?.MakeCurrent();

IntPtr VeldridSurface.IOpenGL.GetCurrentContext() => Gdk.GLContext.Current.Handle;

Expand All @@ -111,9 +109,9 @@ void VeldridSurface.IOpenGL.SwapBuffers()
// GLArea doesn't support drawing directly, so we queue a render but don't actually call OnDraw
if (skipDraw)
return;

skipDraw = true;
Control.QueueRender();
glArea?.QueueRender();
}

void VeldridSurface.IOpenGL.SetSyncToVerticalBlank(bool on)
Expand All @@ -131,13 +129,40 @@ void VeldridSurface.IOpenGL.ResizeSwapchain(uint width, uint height)
void Eto.Forms.Control.IHandler.Invalidate(Rectangle rect, bool invalidateChildren)
{
skipDraw = false;
Control.QueueRender();
glArea?.QueueRender();
}

void Eto.Forms.Control.IHandler.Invalidate(bool invalidateChildren)
{
skipDraw = false;
Control.QueueRender();
glArea?.QueueRender();
}


protected override void Initialize()
{
base.Initialize();

if (Widget.Backend == GraphicsBackend.OpenGL)
{
glArea = new GLArea();
glArea.CanFocus = true;

// Veldrid technically supports as low as OpenGL 3.0, but the full
// complement of features is only available with 3.3 and higher.
glArea.SetRequiredVersion(3, 3);

glArea.HasDepthBuffer = true;
glArea.HasStencilBuffer = true;
Control.Child = glArea;
glArea.Realized += glArea_InitializeGraphicsBackend;
}
else
{
Control.CanFocus = true;
Control.Realized += Control_InitializeGraphicsBackend;
}

}
}
}
7 changes: 5 additions & 2 deletions src/Eto.Veldrid/VeldridSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace Eto.Veldrid
[Handler(typeof(VeldridSurface.IHandler))]
public class VeldridSurface : Control
{
[AutoInitialize(false)]
public new interface IHandler : Control.IHandler
{
Size RenderSize { get; }
Expand Down Expand Up @@ -72,7 +73,7 @@ public interface IOpenGL

public GraphicsBackend Backend { get; private set; }
public GraphicsDevice GraphicsDevice { get; private set; }
public GraphicsDeviceOptions GraphicsDeviceOptions { get; private set; } = new GraphicsDeviceOptions();
public GraphicsDeviceOptions GraphicsDeviceOptions { get; private set; }
public Swapchain Swapchain { get; private set; }

public const string VeldridInitializedEvent = "VeldridSurface.VeldridInitialized";
Expand Down Expand Up @@ -100,13 +101,15 @@ public VeldridSurface()
{
}
public VeldridSurface(GraphicsBackend backend)
: this(backend, new GraphicsDeviceOptions())
{
Backend = backend;
}

public VeldridSurface(GraphicsBackend backend, GraphicsDeviceOptions gdOptions)
{
Backend = backend;
GraphicsDeviceOptions = gdOptions;
Initialize();
}

private static GraphicsBackend GetPreferredBackend()
Expand Down
6 changes: 3 additions & 3 deletions test/TestEtoVeldrid.Gtk/TestEtoVeldrid.Gtk.csproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFrameworks>net5.0;net472</TargetFrameworks>
<TargetFrameworks>net5.0;net48</TargetFrameworks>
<RuntimeIdentifier>linux-x64</RuntimeIdentifier>
</PropertyGroup>

Expand All @@ -20,5 +20,5 @@
<ProjectReference Include="..\TestEtoVeldrid\TestEtoVeldrid.csproj" />
<ProjectReference Include="..\..\src\Eto.Veldrid.Gtk\Eto.Veldrid.Gtk.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion test/TestEtoVeldrid.Mac/TestEtoVeldrid.Mac64.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>net48;net5.0</TargetFrameworks>
<DefineConstants>MONOMAC</DefineConstants>
<RuntimeIdentifier>osx-x64</RuntimeIdentifier>
</PropertyGroup>
Expand Down
3 changes: 2 additions & 1 deletion test/TestEtoVeldrid.WinForms/TestEtoVeldrid.WinForms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>net48</TargetFrameworks>
<TargetFrameworks Condition="$(HaveWindowsDesktopSdk) == 'True'">$(TargetFrameworks);net5.0-windows</TargetFrameworks>
<UseWindowsForms>true</UseWindowsForms>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>
Expand Down
5 changes: 3 additions & 2 deletions test/TestEtoVeldrid.Wpf/TestEtoVeldrid.Wpf.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<Project>

<PropertyGroup>
<HaveWindowsDesktopSdk Condition="$(HaveWindowsDesktopSdk) == '' and $(OS) == 'Windows_NT' and $([System.Version]::Parse($([MSBuild]::ValueOrDefault('$(VisualStudioVersion)', '1.0')))) &gt;= $([System.Version]::Parse('16.0'))">true</HaveWindowsDesktopSdk>
<HaveWindowsDesktopSdk Condition="$(HaveWindowsDesktopSdk) == '' and $(OS) == 'Windows_NT'">true</HaveWindowsDesktopSdk>
</PropertyGroup>

<Import Condition="'$(HaveWindowsDesktopSdk)' != 'true'" Sdk="Microsoft.NET.Sdk" Project="Sdk.props" />
<Import Condition="'$(HaveWindowsDesktopSdk)' == 'true'" Sdk="Microsoft.NET.Sdk.WindowsDesktop" Project="Sdk.props" />

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFrameworks>net472;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>net48</TargetFrameworks>
<TargetFrameworks Condition="$(HaveWindowsDesktopSdk) == 'True'">$(TargetFrameworks);net5.0-windows</TargetFrameworks>
<ApplicationManifest>app1.manifest</ApplicationManifest>
<UseWPF>true</UseWPF>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
Expand Down

0 comments on commit 9d787a9

Please sign in to comment.