From 43f4f479a6eade9ccdb216327c12a27c9f78feae Mon Sep 17 00:00:00 2001
From: sebastiii <sebastiii@team-mediaportal.com>
Date: Sat, 2 Apr 2016 12:15:27 +0200
Subject: [PATCH] MP1-4781 : Fix freeze when using EVR. MP1-4781 : WIP madVR
 v5.

---
 TvEngine3/TVLibrary/TvPlugin/TvPlugin/TVHome.cs   | 12 ++++-
 mediaportal/Core/Player/BDPlayer.cs               | 15 ++++--
 mediaportal/Core/Player/BaseStreamBufferPlayer.cs | 44 ++++++++--------
 mediaportal/Core/Player/BaseTSReaderPlayer.cs     | 59 ++++++++++++---------
 mediaportal/Core/Player/DVDPlayer.cs              | 15 ++++--
 mediaportal/Core/Player/PlaneScene.cs             | 32 +++++++-----
 mediaportal/Core/Player/RTSPPlayer.cs             |  3 +-
 mediaportal/Core/Player/VMR9.cs                   | 13 +++--
 mediaportal/Core/Player/VideoPlayerVMR7.cs        | 19 ++++---
 mediaportal/Core/Player/g_player.cs               | 26 +++++++---
 mediaportal/Core/guilib/GUIActionGroup.cs         | 31 +++++------
 mediaportal/Core/guilib/GUIWindow.cs              | 63 ++++++++++++++++++-----
 mediaportal/Core/guilib/GraphicContext.cs         | 33 ++++++++----
 mediaportal/Dialogs/Dialogs/GUIDialogFile.cs      | 14 ++++-
 mediaportal/Dialogs/Dialogs/GUIDialogWindow.cs    | 11 +++-
 mediaportal/MediaPortal.Application/D3D.cs        | 46 ++++++++++-------
 16 files changed, 290 insertions(+), 146 deletions(-)

diff --git a/TvEngine3/TVLibrary/TvPlugin/TvPlugin/TVHome.cs b/TvEngine3/TVLibrary/TvPlugin/TvPlugin/TVHome.cs
index f8d244f..bf4989c 100644
--- a/TvEngine3/TVLibrary/TvPlugin/TvPlugin/TVHome.cs
+++ b/TvEngine3/TVLibrary/TvPlugin/TvPlugin/TVHome.cs
@@ -1643,10 +1643,17 @@ namespace TvPlugin
 
       if (!_playbackStopped)
       {
-        _mainThreadContext.Send(delegate
+        if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
         {
           g_Player.ShowFullScreenWindow();
-        }, null);
+        }
+        else
+        {
+          GUIWindow._mainThreadContext.Send(delegate
+          {
+            g_Player.ShowFullScreenWindow();
+          }, null);
+        }
       }
     }
 
@@ -3197,6 +3204,7 @@ namespace TvPlugin
       {
         //MediaPortal.GUI.Library.Log.Debug("TvHome.OnBlackImageRendered()");
         _waitForBlackScreen.Set();
+        OnVideoReceived();
       }
     }
 
diff --git a/mediaportal/Core/Player/BDPlayer.cs b/mediaportal/Core/Player/BDPlayer.cs
index 6d08f44..f56b6a1 100644
--- a/mediaportal/Core/Player/BDPlayer.cs
+++ b/mediaportal/Core/Player/BDPlayer.cs
@@ -769,8 +769,11 @@ namespace MediaPortal.Player
         }
         if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
         {
-          Size client = GUIGraphicsContext.form.ClientSize;
-          _videoWin.SetWindowPosition(0, 0, client.Width, client.Height);
+          if (GUIGraphicsContext.InVmr9Render)
+          {
+            Size client = GUIGraphicsContext.form.ClientSize;
+            _videoWin.SetWindowPosition(0, 0, client.Width, client.Height);
+          }
         }
         else
         {
@@ -796,7 +799,10 @@ namespace MediaPortal.Player
 
         if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
         {
-          _basicVideo.SetDestinationPosition(destination.Left, destination.Top, destination.Width, destination.Height);
+          if (GUIGraphicsContext.InVmr9Render)
+          {
+            _basicVideo.SetDestinationPosition(destination.Left, destination.Top, destination.Width, destination.Height);
+          }
         }
         else
         {
@@ -1147,7 +1153,8 @@ namespace MediaPortal.Player
 
         if (_videoWin != null)
         {
-          if (GUIGraphicsContext.Overlay == false && GUIGraphicsContext.IsFullScreenVideo == false)
+          if (GUIGraphicsContext.Overlay == false && GUIGraphicsContext.IsFullScreenVideo == false &&
+              GUIGraphicsContext.VideoRenderer != GUIGraphicsContext.VideoRendererType.madVR)
           {
             if (_isVisible)
             {
diff --git a/mediaportal/Core/Player/BaseStreamBufferPlayer.cs b/mediaportal/Core/Player/BaseStreamBufferPlayer.cs
index 294ffda..034d567 100644
--- a/mediaportal/Core/Player/BaseStreamBufferPlayer.cs
+++ b/mediaportal/Core/Player/BaseStreamBufferPlayer.cs
@@ -476,31 +476,33 @@ namespace MediaPortal.Player
       }
       _lastPosition = CurrentPosition;
 
-
-      if (GUIGraphicsContext.VideoWindow.Width <= 10 && GUIGraphicsContext.IsFullScreenVideo == false)
+      if (GUIGraphicsContext.VideoRenderer != GUIGraphicsContext.VideoRendererType.madVR)
       {
-        _isVisible = false;
-      }
-      if (GUIGraphicsContext.BlankScreen)
-      {
-        _isVisible = false;
-      }
-      if (_isWindowVisible && !_isVisible)
-      {
-        _isWindowVisible = false;
-        //Log.Info("StreamBufferPlayer:hide window");
-        if (_videoWin != null)
+        if (GUIGraphicsContext.VideoWindow.Width <= 10 && GUIGraphicsContext.IsFullScreenVideo == false)
         {
-          _videoWin.put_Visible(OABool.False);
+          _isVisible = false;
         }
-      }
-      else if (!_isWindowVisible && _isVisible)
-      {
-        _isWindowVisible = true;
-        //Log.Info("StreamBufferPlayer:show window");
-        if (_videoWin != null)
+        if (GUIGraphicsContext.BlankScreen)
+        {
+          _isVisible = false;
+        }
+        if (_isWindowVisible && !_isVisible)
         {
-          _videoWin.put_Visible(OABool.True);
+          _isWindowVisible = false;
+          //Log.Info("StreamBufferPlayer:hide window");
+          if (_videoWin != null)
+          {
+            _videoWin.put_Visible(OABool.False);
+          }
+        }
+        else if (!_isWindowVisible && _isVisible)
+        {
+          _isWindowVisible = true;
+          //Log.Info("StreamBufferPlayer:show window");
+          if (_videoWin != null)
+          {
+            _videoWin.put_Visible(OABool.True);
+          }
         }
       }
 
diff --git a/mediaportal/Core/Player/BaseTSReaderPlayer.cs b/mediaportal/Core/Player/BaseTSReaderPlayer.cs
index 985e80a..a6dc370 100644
--- a/mediaportal/Core/Player/BaseTSReaderPlayer.cs
+++ b/mediaportal/Core/Player/BaseTSReaderPlayer.cs
@@ -548,12 +548,12 @@ namespace MediaPortal.Player
       _speedRate = 10000;
       _isLive = false;
       _duration = -1d;
-      if (strFile.ToLowerInvariant().IndexOf(".tsbuffer") >= 0)
+      if (strFile.ToLowerInvariant().IndexOf(".tsbuffer", StringComparison.Ordinal) >= 0)
       {
         Log.Info("TSReaderPlayer: live tv");
         _isLive = true;
       }
-      if (strFile.ToLowerInvariant().IndexOf("rtsp") >= 0)
+      if (strFile.ToLowerInvariant().IndexOf("rtsp", StringComparison.Ordinal) >= 0)
       {
         Log.Info("TSReaderPlayer: live tv");
         _isLive = true;
@@ -782,30 +782,33 @@ namespace MediaPortal.Player
       }
 
       _lastPosition = CurrentPosition;
-      if (GUIGraphicsContext.VideoWindow.Width <= 10 && GUIGraphicsContext.IsFullScreenVideo == false)
+      if (GUIGraphicsContext.VideoRenderer != GUIGraphicsContext.VideoRendererType.madVR)
       {
-        _isVisible = false;
-      }
-      if (GUIGraphicsContext.BlankScreen)
-      {
-        _isVisible = false;
-      }
-      if (_isWindowVisible && !_isVisible)
-      {
-        _isWindowVisible = false;
-        //Log.Info("TSReaderPlayer:hide window");
-        if (_videoWin != null)
+        if (GUIGraphicsContext.VideoWindow.Width <= 10 && GUIGraphicsContext.IsFullScreenVideo == false)
         {
-          _videoWin.put_Visible(OABool.False);
+          _isVisible = false;
         }
-      }
-      else if (!_isWindowVisible && _isVisible)
-      {
-        _isWindowVisible = true;
-        //Log.Info("TSReaderPlayer:show window");
-        if (_videoWin != null)
+        if (GUIGraphicsContext.BlankScreen)
+        {
+          _isVisible = false;
+        }
+        if (_isWindowVisible && !_isVisible)
+        {
+          _isWindowVisible = false;
+          //Log.Info("TSReaderPlayer:hide window");
+          if (_videoWin != null)
+          {
+            _videoWin.put_Visible(OABool.False);
+          }
+        }
+        else if (!_isWindowVisible && _isVisible)
         {
-          _videoWin.put_Visible(OABool.True);
+          _isWindowVisible = true;
+          //Log.Info("TSReaderPlayer:show window");
+          if (_videoWin != null)
+          {
+            _videoWin.put_Visible(OABool.True);
+          }
         }
       }
       OnProcess();
@@ -1450,8 +1453,11 @@ namespace MediaPortal.Player
         }
         if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
         {
-          Size client = GUIGraphicsContext.form.ClientSize;
-          _videoWin.SetWindowPosition(0, 0, client.Width, client.Height);
+          if (GUIGraphicsContext.InVmr9Render)
+          {
+            Size client = GUIGraphicsContext.form.ClientSize;
+            _videoWin.SetWindowPosition(0, 0, client.Width, client.Height);
+          }
         }
         else
         {
@@ -1477,7 +1483,10 @@ namespace MediaPortal.Player
 
         if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
         {
-          _basicVideo.SetDestinationPosition(rDest.Left, rDest.Top, rDest.Width, rDest.Height);
+          if (GUIGraphicsContext.InVmr9Render)
+          {
+            _basicVideo.SetDestinationPosition(rDest.Left, rDest.Top, rDest.Width, rDest.Height);
+          }
         }
         else
         {
diff --git a/mediaportal/Core/Player/DVDPlayer.cs b/mediaportal/Core/Player/DVDPlayer.cs
index 57f9917..3755109 100644
--- a/mediaportal/Core/Player/DVDPlayer.cs
+++ b/mediaportal/Core/Player/DVDPlayer.cs
@@ -1422,7 +1422,8 @@ namespace MediaPortal.Player
     {
       if (_videoWin != null)
       {
-        if (GUIGraphicsContext.Overlay == false && GUIGraphicsContext.IsFullScreenVideo == false)
+        if (GUIGraphicsContext.Overlay == false && GUIGraphicsContext.IsFullScreenVideo == false &&
+            GUIGraphicsContext.VideoRenderer != GUIGraphicsContext.VideoRendererType.madVR)
         {
           if (_visible)
           {
@@ -2129,8 +2130,11 @@ namespace MediaPortal.Player
         }
         if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
         {
-          Size client = GUIGraphicsContext.form.ClientSize;
-          _videoWin.SetWindowPosition(0, 0, client.Width, client.Height);
+          if (GUIGraphicsContext.InVmr9Render)
+          {
+            Size client = GUIGraphicsContext.form.ClientSize;
+            _videoWin.SetWindowPosition(0, 0, client.Width, client.Height);
+          }
         }
         else
         {
@@ -2156,7 +2160,10 @@ namespace MediaPortal.Player
 
         if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
         {
-          _basicVideo.SetDestinationPosition(destination.Left, destination.Top, destination.Width, destination.Height);
+          if (GUIGraphicsContext.InVmr9Render)
+          {
+            _basicVideo.SetDestinationPosition(destination.Left, destination.Top, destination.Width, destination.Height);
+          }
         }
         else
         {
diff --git a/mediaportal/Core/Player/PlaneScene.cs b/mediaportal/Core/Player/PlaneScene.cs
index 766b540..0fb9f9f 100644
--- a/mediaportal/Core/Player/PlaneScene.cs
+++ b/mediaportal/Core/Player/PlaneScene.cs
@@ -240,7 +240,13 @@ namespace MediaPortal.Player
         _blackImage = null;
       }
 
-      grabber.Clean();
+      if (grabber != null) 
+      {
+        lock (GUIGraphicsContext.RenderModeSwitch)
+        {
+          grabber.Clean();
+        }
+      }
       SubtitleRenderer.GetInstance().Clear();
 
       if (GUIGraphicsContext.LastFrames != null)
@@ -553,9 +559,9 @@ namespace MediaPortal.Player
             return 0;
           }
           _vmr9Util.FrameCounter++;
-          //			Log.Info("vmr9:present image()");
+          //Log.Info("vmr9:present image()");
           InternalPresentImage(width, height, arWidth, arHeight, false);
-          //			Log.Info("vmr9:present image() done");
+          //Log.Info("vmr9:present image() done");
         }
         catch (Exception ex)
         {
@@ -584,21 +590,21 @@ namespace MediaPortal.Player
         _vmr9Util.VideoAspectRatioX = arWidth;
         _vmr9Util.VideoAspectRatioY = arHeight;
         _arVideoWidth = arWidth;
-        _arVideoHeight = arHeight;
-
-        Size nativeSize = new Size(width, height);
-        _shouldRenderTexture = SetVideoWindow(nativeSize);
+        _arVideoHeight = arHeight;
+
+        Size nativeSize = new Size(width, height);
+        _shouldRenderTexture = SetVideoWindow(nativeSize);
       }
 
       Device device = GUIGraphicsContext.DX9Device;
 
       device.Clear(ClearFlags.Target, Color.FromArgb(0, 0, 0, 0), 1.0f, 0);
-      device.BeginScene();
-
-      if (layers == GUILayers.over)
-      {
-        SubtitleRenderer.GetInstance().Render();
-        BDOSDRenderer.GetInstance().Render();
+      device.BeginScene();
+
+      if (layers == GUILayers.over)
+      {
+        SubtitleRenderer.GetInstance().Render();
+        BDOSDRenderer.GetInstance().Render();
       }
 
       bool visible = false;
diff --git a/mediaportal/Core/Player/RTSPPlayer.cs b/mediaportal/Core/Player/RTSPPlayer.cs
index dd16090..af992ad 100644
--- a/mediaportal/Core/Player/RTSPPlayer.cs
+++ b/mediaportal/Core/Player/RTSPPlayer.cs
@@ -777,7 +777,8 @@ namespace MediaPortal.Player
         UpdateCurrentPosition();
         updateTimer = DateTime.Now;
         if (GUIGraphicsContext.BlankScreen ||
-            (GUIGraphicsContext.Overlay == false && GUIGraphicsContext.IsFullScreenVideo == false))
+            (GUIGraphicsContext.Overlay == false && GUIGraphicsContext.IsFullScreenVideo == false) &&
+            GUIGraphicsContext.VideoRenderer != GUIGraphicsContext.VideoRendererType.madVR)
         {
           if (m_bVisible)
           {
diff --git a/mediaportal/Core/Player/VMR9.cs b/mediaportal/Core/Player/VMR9.cs
index 6b1eb39..4b24eec 100644
--- a/mediaportal/Core/Player/VMR9.cs
+++ b/mediaportal/Core/Player/VMR9.cs
@@ -1229,14 +1229,17 @@ namespace MediaPortal.Player
       {
         EvrDeinit();
       }
-      else if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
-      {
-        MadDeinit();
-      }
+      else if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
+      {
+        GUIWindow._mainThreadContext.Send(delegate
+        {
+          MadDeinit();
+        }, null);
+      }
       else
       {
         Vmr9Deinit();
-      }
+      }
 
       DirectShowUtil.ReleaseComObject(_vmr9Filter);
       _vmr9Filter = null;
diff --git a/mediaportal/Core/Player/VideoPlayerVMR7.cs b/mediaportal/Core/Player/VideoPlayerVMR7.cs
index 5b56fd9..dc74d94 100644
--- a/mediaportal/Core/Player/VideoPlayerVMR7.cs
+++ b/mediaportal/Core/Player/VideoPlayerVMR7.cs
@@ -46,7 +46,7 @@ namespace MediaPortal.Player
     protected const string LAV_AUDIO = "LAV Audio Decoder";
     protected const string LAV_VIDEO = "LAV Video Decoder";
     protected const string FILE_SYNC_FILTER = "File Source (Async.)";
-    protected IGraphRebuildDelegate _IGraphRebuildDelegate = null;
+    protected IGraphRebuildDelegate _IGraphRebuildDelegate = null;
 
     protected struct FilterStreamInfos
     {
@@ -631,9 +631,12 @@ namespace MediaPortal.Player
           return;
         }
         if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
-        {
-          Size client = GUIGraphicsContext.form.ClientSize;
-          videoWin.SetWindowPosition(0, 0, client.Width, client.Height);
+        {
+          if (GUIGraphicsContext.InVmr9Render)
+          {
+            Size client = GUIGraphicsContext.form.ClientSize;
+            videoWin.SetWindowPosition(0, 0, client.Width, client.Height);
+          }
         }
         else
         {
@@ -659,7 +662,10 @@ namespace MediaPortal.Player
 
         if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
         {
-          basicVideo.SetDestinationPosition(rDest.Left, rDest.Top, rDest.Width, rDest.Height);
+          if (GUIGraphicsContext.InVmr9Render)
+          {
+            basicVideo.SetDestinationPosition(rDest.Left, rDest.Top, rDest.Width, rDest.Height);
+          }
         }
         else
         {
@@ -700,7 +706,8 @@ namespace MediaPortal.Player
           mediaPos.get_CurrentPosition(out m_dCurrentPos);
         }
         if (GUIGraphicsContext.BlankScreen ||
-            (GUIGraphicsContext.Overlay == false && GUIGraphicsContext.IsFullScreenVideo == false))
+            (GUIGraphicsContext.Overlay == false && GUIGraphicsContext.IsFullScreenVideo == false) &&
+            GUIGraphicsContext.VideoRenderer != GUIGraphicsContext.VideoRendererType.madVR)
         {
           if (m_bVisible)
           {
diff --git a/mediaportal/Core/Player/g_player.cs b/mediaportal/Core/Player/g_player.cs
index 89b562a..ec845e8 100644
--- a/mediaportal/Core/Player/g_player.cs
+++ b/mediaportal/Core/Player/g_player.cs
@@ -666,6 +666,12 @@ namespace MediaPortal.Player
           return;
         }
 
+        if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
+        {
+          // Stop VMR9
+          GUIGraphicsContext.InVmr9Render = false;
+        }
+
         GUIGraphicsContext.ShowBackground = true;
         if (!keepTimeShifting && !keepExclusiveModeOn)
         {
@@ -3044,9 +3050,8 @@ namespace MediaPortal.Player
 
     public static void Init()
     {
-      GUIGraphicsContext.OnVideoWindowChanged += new VideoWindowChangedHandler(OnVideoWindowChanged);
-      GUIGraphicsContext.OnGammaContrastBrightnessChanged +=
-        new VideoGammaContrastBrightnessHandler(OnGammaContrastBrightnessChanged);
+      GUIGraphicsContext.OnVideoWindowChanged += OnVideoWindowChanged;
+      GUIGraphicsContext.OnGammaContrastBrightnessChanged += OnGammaContrastBrightnessChanged;
     }
 
     private static void OnGammaContrastBrightnessChanged()
@@ -3097,12 +3102,19 @@ namespace MediaPortal.Player
       {
         inTV = true;
       }
-      Visible = (FullScreen || GUIGraphicsContext.Overlay ||
-                 windowId == (int)GUIWindow.Window.WINDOW_SCHEDULER || inTV);
-      GUIWindow._mainThreadContext.Post(delegate
+
+      if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
+      {
+        if (GUIGraphicsContext.InVmr9Render)
+        {
+          SetVideoWindow();
+        }
+      }
+      else
       {
+        Visible = (FullScreen || GUIGraphicsContext.Overlay || windowId == (int)GUIWindow.Window.WINDOW_SCHEDULER || inTV);
         SetVideoWindow();
-      }, null);
+      }
     }
 
     /// <summary>
diff --git a/mediaportal/Core/guilib/GUIActionGroup.cs b/mediaportal/Core/guilib/GUIActionGroup.cs
index b10257e..b5af02a 100644
--- a/mediaportal/Core/guilib/GUIActionGroup.cs
+++ b/mediaportal/Core/guilib/GUIActionGroup.cs
@@ -137,7 +137,17 @@ namespace MediaPortal.GUI.Library
       {
         _parentWin.Children.Remove(this);
         _parentWin.Children.Add(this);
-        _isWinOverlayAllowed = _parentWin.IsOverlayAllowed;
+        if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
+        {
+          if (GUIGraphicsContext.InVmr9Render)
+          {
+            GUIGraphicsContext.Overlay = true;
+          }
+        }
+        else
+        {
+          _isWinOverlayAllowed = _parentWin.IsOverlayAllowed;
+        }
       }
       SetDefaultControl();
       _buttonState = NextButtonStates.Activation;
@@ -275,30 +285,21 @@ namespace MediaPortal.GUI.Library
       {
         if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
         {
-          GUIGraphicsContext.Overlay = true;
+          if (GUIGraphicsContext.InVmr9Render)
+          {
+            GUIGraphicsContext.Overlay = true;
+          }
         }
         else if (GUIGraphicsContext.Overlay != _isOverlayAllowed)
         {
-          GUIGraphicsContext.Overlay = _isWinOverlayAllowed && _isOverlayAllowed;
+          GUIGraphicsContext.Overlay = _parentWin.IsOverlayAllowed = _isOverlayAllowed;
         }
         _imageFocused.Render(timePassed);
         GUIFontManager.Present();
       }
       else
       {
-        if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
-        {
-          GUIGraphicsContext.Overlay = true;
-        }
-        else if (GUIGraphicsContext.Overlay != _isWinOverlayAllowed)
-        {
-          GUIGraphicsContext.Overlay = _isWinOverlayAllowed;
-        }
         _imageNonFocused.Render(timePassed);
-        if (IsEffectAnimating(AnimationType.Hidden))
-        {
-          GUIFontManager.Present();
-        }
       }
       base.Render(timePassed);
     }
diff --git a/mediaportal/Core/guilib/GUIWindow.cs b/mediaportal/Core/guilib/GUIWindow.cs
index 7dd3d08..4c5d6f3 100644
--- a/mediaportal/Core/guilib/GUIWindow.cs
+++ b/mediaportal/Core/guilib/GUIWindow.cs
@@ -475,10 +475,17 @@ namespace MediaPortal.GUI.Library
       }
 
       // else load xml file now
-      GUIWindow._mainThreadContext.Send(delegate
+      if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR && GUIGraphicsContext.InVmr9Render)
       {
         LoadSkin();
-      }, null);
+      }
+      else
+      {
+        GUIWindow._mainThreadContext.Send(delegate
+        {
+          LoadSkin();
+        }, null);
+      }
 
       if (!_windowAllocated)
       {
@@ -1069,11 +1076,18 @@ namespace MediaPortal.GUI.Library
     protected virtual void OnPageLoad()
     {
       if (_isSkinLoaded && (_lastSkin != GUIGraphicsContext.Skin))
-      {
-        GUIWindow._mainThreadContext.Send(delegate
+      {
+        if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR && GUIGraphicsContext.InVmr9Render)
         {
           LoadSkin();
-        }, null);
+        }
+        else
+        {
+          GUIWindow._mainThreadContext.Send(delegate
+          {
+            LoadSkin();
+          }, null);
+        }
       }
 
       if (_rememberLastFocusedControl && _rememberLastFocusedControlId >= 0)
@@ -1200,12 +1214,19 @@ namespace MediaPortal.GUI.Library
           return;
         }
 
-        Dispose();
+        Dispose();
 
-        GUIWindow._mainThreadContext.Send(delegate
+        if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR && GUIGraphicsContext.InVmr9Render)
         {
           LoadSkin();
-        }, null);
+        }
+        else
+        {
+          GUIWindow._mainThreadContext.Send(delegate
+          {
+            LoadSkin();
+          }, null);
+        }
 
         HashSet<int> faultyControl = new HashSet<int>();
         // tell every control we're gonna alloc the resources next
@@ -1749,11 +1770,18 @@ namespace MediaPortal.GUI.Library
                 DoRestoreSkin();
               }
               else
-              {
-                GUIWindow._mainThreadContext.Send(delegate
+              {
+                if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR && GUIGraphicsContext.InVmr9Render)
                 {
                   LoadSkin();
-                }, null);
+                }
+                else
+                {
+                  GUIWindow._mainThreadContext.Send(delegate
+                  {
+                    LoadSkin();
+                  }, null);
+                }
 
                 if (!_windowAllocated)
                 {
@@ -1763,7 +1791,18 @@ namespace MediaPortal.GUI.Library
 
               InitControls();
               UpdateOverlayAllowed();
-              GUIGraphicsContext.Overlay = _isOverlayAllowed || GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR;
+
+              if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
+              {
+                if (GUIGraphicsContext.InVmr9Render)
+                {
+                  GUIGraphicsContext.Overlay = false;
+                }
+              }
+              else
+              {
+                GUIGraphicsContext.Overlay = _isOverlayAllowed;
+              }
 
               // set topbar autohide 
               switch (_autoHideTopbarType)
diff --git a/mediaportal/Core/guilib/GraphicContext.cs b/mediaportal/Core/guilib/GraphicContext.cs
index b11d7f6..abaa810 100644
--- a/mediaportal/Core/guilib/GraphicContext.cs
+++ b/mediaportal/Core/guilib/GraphicContext.cs
@@ -1005,7 +1005,10 @@ namespace MediaPortal.GUI.Library
     {
       if (_videoRendererType == VideoRendererType.madVR)
       {
-        ThreadPool.QueueUserWorkItem(o => NotifyVideoWindowChanged());
+        if (InVmr9Render && Thread.CurrentThread.Name == "MPMain")
+        {
+          ThreadPool.QueueUserWorkItem(o => NotifyVideoWindowChanged());
+        }
       }
       else if (OnVideoWindowChanged != null)
       {
@@ -1075,6 +1078,13 @@ namespace MediaPortal.GUI.Library
           {
             VideoWindowChanged();
           }
+          else if (VideoRenderer == VideoRendererType.madVR)
+          {
+            if (InVmr9Render)
+            {
+              VideoWindowChanged();
+            }
+          }
         }
       }
     }
@@ -1483,16 +1493,19 @@ namespace MediaPortal.GUI.Library
     {
       if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
       {
-        Size client = GUIGraphicsContext.form.ClientSize;
-        float ration = 1.0f;
-        
-        if (DX9Device != null && DX9Device.PresentationParameters != null)
-          ration = (float)client.Width / (float)DX9Device.PresentationParameters.BackBufferWidth;
+        if (GUIGraphicsContext.InVmr9Render)
+        {
+          Size client = GUIGraphicsContext.form.ClientSize;
+          float ration = 1.0f;
 
-        width = (int)((float)width * (float)ration);
-        height = (int)((float)height * (float)ration);
-        x *= ration;
-        y *= ration;
+          if (DX9Device != null && DX9Device.PresentationParameters != null)
+            ration = (float) client.Width/(float) DX9Device.PresentationParameters.BackBufferWidth;
+
+          width = (int) ((float) width*(float) ration);
+          height = (int) ((float) height*(float) ration);
+          x *= ration;
+          y *= ration;
+        }
       }
     }
 
diff --git a/mediaportal/Dialogs/Dialogs/GUIDialogFile.cs b/mediaportal/Dialogs/Dialogs/GUIDialogFile.cs
index bc7f1e8..7e2796b 100644
--- a/mediaportal/Dialogs/Dialogs/GUIDialogFile.cs
+++ b/mediaportal/Dialogs/Dialogs/GUIDialogFile.cs
@@ -177,8 +177,18 @@ namespace MediaPortal.Dialogs
         case GUIMessage.MessageType.GUI_MSG_WINDOW_INIT:
           {
             m_bDialogActive = true;
-            base.OnMessage(message);
-            GUIGraphicsContext.Overlay = base.IsOverlayAllowed;
+            base.OnMessage(message);
+            if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
+            {
+              if (GUIGraphicsContext.InVmr9Render)
+              {
+                GUIGraphicsContext.Overlay = true;
+              }
+            }
+            else
+            {
+              GUIGraphicsContext.Overlay = base.IsOverlayAllowed;
+            }
             m_pParentWindow = GUIWindowManager.GetWindow(m_dwParentWindowID);
             GUILayerManager.RegisterLayer(this, GUILayerManager.LayerType.Dialog);
           }
diff --git a/mediaportal/Dialogs/Dialogs/GUIDialogWindow.cs b/mediaportal/Dialogs/Dialogs/GUIDialogWindow.cs
index 5cbfe14..641c394 100644
--- a/mediaportal/Dialogs/Dialogs/GUIDialogWindow.cs
+++ b/mediaportal/Dialogs/Dialogs/GUIDialogWindow.cs
@@ -231,8 +231,17 @@ namespace MediaPortal.Dialogs
             _prevLayer = GUILayerManager.GetLayer(GUILayerManager.LayerType.Dialog);
             _prevOverlay = GUIGraphicsContext.Overlay;
 
-            if (GUIGraphicsContext.VideoRenderer != GUIGraphicsContext.VideoRendererType.madVR)
+            if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
+            {
+              if (GUIGraphicsContext.InVmr9Render)
+              {
+                GUIGraphicsContext.Overlay = true;
+              }
+            }
+            else
+            {
               GUIGraphicsContext.Overlay = base.IsOverlayAllowed;
+            }
 
             GUILayerManager.RegisterLayer(this, GUILayerManager.LayerType.Dialog);
 
diff --git a/mediaportal/MediaPortal.Application/D3D.cs b/mediaportal/MediaPortal.Application/D3D.cs
index d86de3e..2395cc5 100644
--- a/mediaportal/MediaPortal.Application/D3D.cs
+++ b/mediaportal/MediaPortal.Application/D3D.cs
@@ -856,29 +856,39 @@ namespace MediaPortal
       }
 
       // lock rendering loop and recreate the backbuffer for the current D3D device
-      RecreateSwapChain(true);
-
-      GUIGraphicsContext.CurrentState = GUIGraphicsContext.State.RUNNING;
-
-      if (RefreshRateChanger.RefreshRateChangePending && RefreshRateChanger.RefreshRateChangeStrFile.Length > 0)
+      if (GUIGraphicsContext.VideoRenderer == GUIGraphicsContext.VideoRendererType.madVR)
       {
-        RefreshRateChanger.RefreshRateChangePending = false;
-        if (RefreshRateChanger.RefreshRateChangeMediaType != RefreshRateChanger.MediaType.Unknown)
-        {
-          var t1 = (int)RefreshRateChanger.RefreshRateChangeMediaType;
-          var t2 = (g_Player.MediaType)t1;
-          g_Player.Play(RefreshRateChanger.RefreshRateChangeStrFile, t2);
-        }
-        else
+        if (GUIGraphicsContext.InVmr9Render)
         {
-          g_Player.Play(RefreshRateChanger.RefreshRateChangeStrFile);
+          RecreateSwapChain(false);
         }
-        if ((g_Player.HasVideo || g_Player.HasViz) && RefreshRateChanger.RefreshRateChangeFullscreenVideo)
+        GUIGraphicsContext.CurrentState = GUIGraphicsContext.State.RUNNING;
+      }
+      else
+      {
+        RecreateSwapChain(true);
+        GUIGraphicsContext.CurrentState = GUIGraphicsContext.State.RUNNING;
+
+        if (RefreshRateChanger.RefreshRateChangePending && RefreshRateChanger.RefreshRateChangeStrFile.Length > 0)
         {
-          g_Player.ShowFullScreenWindow();
+          RefreshRateChanger.RefreshRateChangePending = false;
+          if (RefreshRateChanger.RefreshRateChangeMediaType != RefreshRateChanger.MediaType.Unknown)
+          {
+            var t1 = (int) RefreshRateChanger.RefreshRateChangeMediaType;
+            var t2 = (g_Player.MediaType) t1;
+            g_Player.Play(RefreshRateChanger.RefreshRateChangeStrFile, t2);
+          }
+          else
+          {
+            g_Player.Play(RefreshRateChanger.RefreshRateChangeStrFile);
+          }
+          if ((g_Player.HasVideo || g_Player.HasViz) && RefreshRateChanger.RefreshRateChangeFullscreenVideo)
+          {
+            g_Player.ShowFullScreenWindow();
+          }
         }
-      }
-
+      }
+
       // enable handlers
       if (GUIGraphicsContext.DX9Device != null)
       {
-- 
2.6.3.windows.1

