summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Renderers/OpenGLViewRenderer.cs
blob: dda6ac50a024d73fa2a90aae2f0fd79d6f02d258 (plain)
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
´╗┐using System;
using System.ComponentModel;
using CoreVideo;

namespace Xamarin.Forms.Platform.MacOS
{
	// ReSharper disable once InconsistentNaming
	internal class OpenGLViewRenderer : ViewRenderer<OpenGLView, MacOSOpenGLView>
	{
		CVDisplayLink _displayLink;

		public void Display(object sender, EventArgs eventArgs)
		{
			if (Element.HasRenderLoop)
				return;
			SetupRenderLoop(true);
		}

		protected override void Dispose(bool disposing)
		{
			if (_displayLink != null)
			{
				_displayLink.Dispose();
				_displayLink = null;

				if (Element != null)
					((IOpenGlViewController)Element).DisplayRequested -= Display;
			}

			base.Dispose(disposing);
		}

		protected override void OnElementChanged(ElementChangedEventArgs<OpenGLView> e)
		{
			if (e.OldElement != null)
				((IOpenGlViewController)e.OldElement).DisplayRequested -= Display;

			if (e.NewElement != null)
			{
				//var context = new EAGLContext(EAGLRenderingAPI.OpenGLES2);
				//var glkView = new GLKView(RectangleF.Empty) { Context = context, DrawableDepthFormat = GLKViewDrawableDepthFormat.Format24, Delegate = new Delegate(e.NewElement) };
				var glkView = new MacOSOpenGLView();
				SetNativeControl(glkView);

				((IOpenGlViewController)e.NewElement).DisplayRequested += Display;

				SetupRenderLoop(false);
			}

			base.OnElementChanged(e);
		}

		protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			base.OnElementPropertyChanged(sender, e);

			if (e.PropertyName == OpenGLView.HasRenderLoopProperty.PropertyName)
				SetupRenderLoop(false);
		}

		void SetupRenderLoop(bool oneShot)
		{
			if (_displayLink != null)
				return;
			if (!oneShot && !Element.HasRenderLoop)
				return;

			_displayLink = new CVDisplayLink();

			//.Create(() =>
			//{
			//	var control = Control;
			//	var model = Element;
			//	if (control != null)
			//		control.Display();
			//	if (control == null || model == null || !model.HasRenderLoop)
			//	{
			//		_displayLink.Invalidate();
			//		_displayLink.Dispose();
			//		_displayLink = null;
			//	}
			//});
			//_displayLink.(NSRunLoop.Current, NSRunLoop.NSDefaultRunLoopMode);
		}

		//class Delegate : GLKViewDelegate
		//{
		//	readonly OpenGLView _model;

		//	public Delegate(OpenGLView model)
		//	{
		//		_model = model;
		//	}

		//	public override void DrawInRect(GLKView view, RectangleF rect)
		//	{
		//		var onDisplay = _model.OnDisplay;
		//		if (onDisplay == null)
		//			return;
		//		onDisplay(rect.ToRectangle());
		//	}
		//}
	}
}