Exemple #1
0
 private CVReturn HandleDisplayLinkOutputCallback(CVDisplayLink displayLink, ref CVTimeStamp inNow, ref CVTimeStamp inOutputTime, CVOptionFlags flagsIn, ref CVOptionFlags flagsOut)
 {
     try
     {
         if (_paused)
         {
             return(CVReturn.Success);
         }
         if (GraphicsDevice != null)
         {
             if (_resized)
             {
                 _resized = false;
                 MainSwapchain.Resize(_width, _height);
                 Resized?.Invoke();
             }
             Rendering?.Invoke();
         }
     }
     catch (Exception e)
     {
         Debug.WriteLine("Encountered an error while rendering: " + e);
         throw;
     }
     return(CVReturn.Success);
 }
Exemple #2
0
 public void DefaultConstructorTest()
 {
     TestRuntime.AssertNotVSTS();
     Assert.DoesNotThrow(() => {
         using var displayLink = new CVDisplayLink();
     });
 }
        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);
        }
Exemple #4
0
 public void GetTypeIdTest()
 {
     TestRuntime.AssertSystemVersion(ApplePlatform.MacOSX, 12, 0);
     Assert.DoesNotThrow(() => {
         CVDisplayLink.GetTypeId();
     }, "Throws");
 }
Exemple #5
0
 public void SetCurrentDisplayOpenGLTest()
 {
     TestRuntime.AssertNotVSTS();
     Assert.DoesNotThrow(() => {
         using var displayLink = new CVDisplayLink();
         displayLink.SetCurrentDisplay(CGDisplay.MainDisplayID);
     });
 }
Exemple #6
0
        protected override void EnableTimer()
        {
            this.link = Xamarin CoreAnimation.CADisplayLink.Create(() => this.SendSignals (-1));

            this.link.Start();
            // TODO: Does this work?
            //this.link.AddToRunLoop (NSRunLoop.Current, NSRunLoop.NSRunLoopCommonModes);
        }
Exemple #7
0
 public void GetCurrentDisplayTest()
 {
     TestRuntime.AssertNotVSTS();
     Assert.DoesNotThrow(() => {
         using var displayLink = new CVDisplayLink();
         Assert.AreEqual(CGDisplay.MainDisplayID, displayLink.GetCurrentDisplay());
     });
 }
Exemple #8
0
 public void CreateFromDisplayWrongIdTest()
 {
     TestRuntime.AssertSystemVersion(ApplePlatform.MacOSX, 12, 0);
     Assert.DoesNotThrow(() => {
         using var displayLink = CVDisplayLink.CreateFromDisplayId(UInt32.MaxValue);
         Assert.Null(displayLink, "null");
     }, "Throws");
 }
Exemple #9
0
 public void CreateFromDisplayIdValidIdTest()
 {
     TestRuntime.AssertSystemVersion(ApplePlatform.MacOSX, 12, 0);
     Assert.DoesNotThrow(() => {
         using var displayLink = CVDisplayLink.CreateFromDisplayId((uint)CGDisplay.MainDisplayID);
         Assert.NotNull(displayLink, "Not null");
         Assert.AreEqual(CGDisplay.MainDisplayID, displayLink.GetCurrentDisplay(), "DisplayId");
     }, "Throws");
 }
Exemple #10
0
        protected override void SetupRenderLoop(bool oneShot)
        {
            // only start if we haven't already
            if (displayLink != null)
            {
                return;
            }

            // bail out if we are requesting something that the view doesn't want to
            if (!oneShot && !Element.HasRenderLoop)
            {
                return;
            }

            // if this is a one shot request, don't bother with the display link
            if (oneShot)
            {
                var nativeView = Control;
                nativeView?.BeginInvokeOnMainThread(() =>
                {
                    if (nativeView.Handle != IntPtr.Zero)
                    {
                        nativeView.NeedsDisplay = true;
                    }
                });
                return;
            }

            // create the loop
            displayLink = new CVDisplayLink();
            displayLink.SetOutputCallback(delegate
            {
                var nativeView = Control;
                var formsView  = Element;

                // stop the render loop if this was a one-shot, or the views are disposed
                if (nativeView == null || formsView == null || nativeView.Handle == IntPtr.Zero || !formsView.HasRenderLoop)
                {
                    displayLink.Stop();
                    displayLink.Dispose();
                    displayLink = null;
                    return(CVReturn.Success);
                }

                // redraw the view
                nativeView?.BeginInvokeOnMainThread(() =>
                {
                    if (nativeView != null)
                    {
                        nativeView.NeedsDisplay = true;
                    }
                });

                return(CVReturn.Success);
            });
            displayLink.Start();
        }
Exemple #11
0
        public void CreateFromOpenGLMaskTest()
        {
            TestRuntime.AssertSystemVersion(ApplePlatform.MacOSX, 12, 0);
            var openGLMask = CGDisplay.GetOpenGLDisplayMask(CGDisplay.MainDisplayID);

            Assert.DoesNotThrow(() => {
                using var displayLink = CVDisplayLink.CreateFromOpenGLMask((uint)openGLMask);
                Assert.NotNull(displayLink, "Not null");
            }, "Throws");
        }
Exemple #12
0
 public override void Stop()
 {
     if (link == null)
     {
         return;
     }
     link?.Stop();
     link?.Dispose();
     link = null;
 }
Exemple #13
0
 public override void Start()
 {
     if (link != null)
     {
         return;
     }
     link = new CVDisplayLink();
     link.SetOutputCallback(DisplayLinkOutputCallback);
     link.Start();
 }
Exemple #14
0
 public void CreateFromDisplayIdsTest()
 {
     TestRuntime.AssertSystemVersion(ApplePlatform.MacOSX, 12, 0);
     // we might not have more than one display, therefore we will use an array
     // with a single one, there is nothing in the docs that say that we cannot do that
     Assert.DoesNotThrow(() => {
         using var displayLink = CVDisplayLink.CreateFromDisplayIds(new [] { (uint)CGDisplay.MainDisplayID });
         Assert.NotNull(displayLink, "Not null");
     }, "Throws");
 }
Exemple #15
0
        private CVReturn HandleDisplayLinkOutputCallback(CVDisplayLink displayLink, ref CVTimeStamp inNow, ref CVTimeStamp inOutputTime, CVOptionFlags flagsIn, ref CVOptionFlags flagsOut)
        {
            if (_newRenderSize != null)
            {
                Callback.OnResize(Widget, new ResizeEventArgs(_newRenderSize.Value));
                _newRenderSize = null;
            }

            Callback.OnDraw(Widget, EventArgs.Empty);
            return(CVReturn.Success);
        }
Exemple #16
0
 protected override void DisableTimer()
 {
     if (this.link != null)
     {
         this.link.Stop();
         // TODO: Does this work?
         //this.link.RemoveFromRunLoop (NSRunLoop.Current, NSRunLoop.NSRunLoopCommonModes);
         this.link.Dispose();
     }
     this.link = null;
 }
Exemple #17
0
        void DeAllocate()
        {
            if (_displayLink == null)
            {
                return;
            }

            _displayLink.Stop();
            _displayLink.Dispose();
            _displayLink = null;
        }
Exemple #18
0
 public CVReturn DisplayLinkOutputCallback(CVDisplayLink displayLink, ref CVTimeStamp inNow,
                                           ref CVTimeStamp inOutputTime, CVOptionFlags flagsIn, ref CVOptionFlags flagsOut)
 {
     // There is no autorelease pool when this method is called because it will be called from a background thread
     // It's important to create one or you will leak objects
     // ReSharper disable once UnusedVariable
     using (var pool = new NSAutoreleasePool())
     {
         Device.BeginInvokeOnMainThread(() => SendSignals());
     }
     return(CVReturn.Success);
 }
Exemple #19
0
        protected override void Dispose(bool disposing)
        {
            // stop the render loop
            if (displayLink != null)
            {
                displayLink.Stop();
                displayLink.Dispose();
                displayLink = null;
            }

            base.Dispose(disposing);
        }
Exemple #20
0
        public void Startup()
        {
            DisplayTitle();

            displayLink = new CVDisplayLink();
            displayLink.SetOutputCallback(DisplayLinkOutputCallback);

            tickStopWatch = new Stopwatch();
            tickStopWatch.Start();

            displayLink.Start();
        }
        private void SetupDisplayLink()
        {
            // Create a display link capable of being used with all active displays
            displayLink = new CVDisplayLink();

            // Set the renderer output callback function
            displayLink.SetOutputCallback(MyDisplayLinkOutputCallback);

            // Set the display link for the current renderer
            CGLContext     cglContext     = openGLContext.CGLContext;
            CGLPixelFormat cglPixelFormat = PixelFormat.CGLPixelFormat;

            displayLink.SetCurrentDisplay(cglContext, cglPixelFormat);
        }
Exemple #22
0
        private void Control_Draw(object sender, EventArgs e)
        {
            Callback.OnInitializeBackend(Widget, new InitializeEventArgs(RenderSize));

            if (Widget.Backend == GraphicsBackend.Metal)
            {
                _displayLink = new CVDisplayLink();
                _displayLink.SetOutputCallback(HandleDisplayLinkOutputCallback);
                _displayLink.Start();
            }

            Control.Draw       -= Control_Draw;
            Widget.SizeChanged += Widget_SizeChanged;
        }
Exemple #23
0
        void SetupDisplayLink()
        {
            _displayLink = new CVDisplayLink();

            // Set the renderer output callback function
            SetOutputCallback(_displayLink, DisplayLinkOutputCallback);

            // Set the display link for the current renderer
            var cglContext     = OpenGLContext.CGLContext;
            var cglPixelFormat = PixelFormat.CGLPixelFormat;

            _displayLink.SetCurrentDisplay(cglContext, cglPixelFormat);
            _displayLink.Start();
        }
Exemple #24
0
        // Private Callback function for CVDisplayLink
        private CVReturn MyDisplayLinkOutputCallback(CVDisplayLink displayLink, ref CVTimeStamp inNow, ref CVTimeStamp inOutputTime, CVOptionFlags flagsIn, ref CVOptionFlags flagsOut)
        {
            //CVReturn result = GetFrameForTime (inOutputTime);
            CVReturn result = CVReturn.Error;

            // There is no autorelease pool when this method is called because it will be called from a background thread
            // It's important to create one or you will leak objects
            using (NSAutoreleasePool pool = new NSAutoreleasePool()) {
                // Update the animation
                RenderScene();
                result = CVReturn.Success;
            }

            return(result);
        }
Exemple #25
0
        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 Dispose(bool disposing)
        {
            if (_displayLink != null)
            {
                _displayLink.Dispose();
                _displayLink = null;

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

            base.Dispose(disposing);
        }
Exemple #27
0
        public void TryTranslateTimeValidTest()
        {
            TestRuntime.AssertNotVSTS();
            TestRuntime.AssertSystemVersion(ApplePlatform.MacOSX, 12, 0);
            var outTime = new CVTimeStamp {
                Version = 0,
                Flags   = (1L << 0) | (1L << 1),               // kCVTimeStampVideoTimeValid | kCVTimeStampHostTimeValid
            };

            using var displayLink = new CVDisplayLink();
            // it has to be running else you will get a crash
            if (displayLink.Start() == 0)
            {
                displayLink.GetCurrentTime(out var timeStamp);
                Assert.True(displayLink.TryTranslateTime(timeStamp, ref outTime));
                displayLink.Stop();
            }
        }
        partial void DoEnableRenderLoop(bool enable)
        {
            // stop the render loop
            if (!enable)
            {
                if (displayLink != null)
                {
                    displayLink.Stop();
                    displayLink.Dispose();
                    displayLink = null;
                }
                return;
            }

            // only start if we haven't already
            if (displayLink != null)
            {
                return;
            }

            // create the loop
            displayLink = new CVDisplayLink();
            displayLink.SetOutputCallback(delegate
            {
                // redraw the view
                glView?.BeginInvokeOnMainThread(() =>
                {
                    if (glView != null)
                    {
                        glView.NeedsDisplay = true;
                    }
                });

                // stop the render loop if it has been disabled or the views are disposed
                if (glView == null || !EnableRenderLoop)
                {
                    DoEnableRenderLoop(false);
                }

                return(CVReturn.Success);
            });
            displayLink.Start();
        }
Exemple #29
0
        public static double GetRefreshRate(uint display)
        {
            var result = CVDisplayLinkCreateWithCGDisplay(display, out var handle);

            if (result != 0 || handle == IntPtr.Zero)
            {
                return(0.0);
            }

            using var dl = new CVDisplayLink(handle);

            var period = dl.NominalOutputVideoRefreshPeriod;

            if (((CVTimeFlags)period.Flags).HasFlag(CVTimeFlags.IsIndefinite) || period.TimeValue == 0)
            {
                return(0.0);
            }

            return(period.TimeScale / (double)period.TimeValue);
        }
Exemple #30
0
        public override void ViewDidMoveToWindow()
        {
            base.ViewDidMoveToWindow();

            var swapchainSource      = SwapchainSource.CreateNSView(Handle);
            var swapchainDescription = new SwapchainDescription(swapchainSource, (uint)Frame.Width, (uint)Frame.Height, null, true, true);

            if (_backend == GraphicsBackend.Metal)
            {
                GraphicsDevice = GraphicsDevice.CreateMetal(_deviceOptions);
            }

            MainSwapchain = GraphicsDevice.ResourceFactory.CreateSwapchain(swapchainDescription);

            DeviceReady?.Invoke();

            _displayLink = new CVDisplayLink();
            _displayLink.SetOutputCallback(HandleDisplayLinkOutputCallback);
            _displayLink.Start();
        }
Exemple #31
0
		public CVReturn MyDisplayLinkOutputCallback (CVDisplayLink displayLink, ref CVTimeStamp inNow, ref CVTimeStamp inOutputTime, CVOptionFlags flagsIn, ref CVOptionFlags flagsOut)
		{
			CVReturn result = GetFrameForTime (inOutputTime);

			return result;
		}
Exemple #32
0
		private void SetupDisplayLink ()
		{
			// Create a display link capable of being used with all active displays
			displayLink = new CVDisplayLink ();

			// Set the renderer output callback function
			displayLink.SetOutputCallback (MyDisplayLinkOutputCallback);

			// Set the display link for the current renderer
			CGLContext cglContext = openGLContext.CGLContext;
			CGLPixelFormat cglPixelFormat = PixelFormat.CGLPixelFormat;
			displayLink.SetCurrentDisplay (cglContext, cglPixelFormat);

		}