[vlc-devel] [PATCH] Win32 screen: Fix cursor rendering and multi-display support

Salah-Eddin Shaban salah at videolan.org
Tue Feb 14 22:00:27 CET 2017


Close #5987, #5988, #6659, #6980, #8299, #8871, #8876, #8878
---
 modules/access/screen/screen.c | 71 +++++++++++++++++++++++++-----------------
 modules/access/screen/win32.c  | 52 ++++++++++++++++++++++++++++---
 2 files changed, 90 insertions(+), 33 deletions(-)

diff --git a/modules/access/screen/screen.c b/modules/access/screen/screen.c
index 66a1396..5dffd5f 100644
--- a/modules/access/screen/screen.c
+++ b/modules/access/screen/screen.c
@@ -160,13 +160,6 @@ static int Open( vlc_object_t *p_this )
     p_sys->i_left = var_CreateGetInteger( p_demux, "screen-left" );
     p_sys->i_width = var_CreateGetInteger( p_demux, "screen-width" );
     p_sys->i_height = var_CreateGetInteger( p_demux, "screen-height" );
-    if( p_sys->i_width > 0 && p_sys->i_height > 0 )
-        msg_Dbg( p_demux, "capturing subscreen top: %d, left: %d, "
-                          "width: %d, height: %d",
-                          p_sys->i_top,
-                          p_sys->i_left,
-                          p_sys->i_width,
-                          p_sys->i_height );
 #endif
 
 #ifdef SCREEN_DISPLAY_ID
@@ -185,29 +178,41 @@ static int Open( vlc_object_t *p_this )
              p_sys->fmt.video.i_bits_per_pixel );
 
 #ifdef SCREEN_SUBSCREEN
-    if( p_sys->i_width > 0 && p_sys->i_height > 0 )
+    if( p_sys->i_left >= p_sys->fmt.video.i_width
+     || p_sys->i_top >= p_sys->fmt.video.i_height )
     {
-        if( p_sys->i_left + p_sys->i_width > p_sys->fmt.video.i_width ||
-            p_sys->i_top + p_sys->i_height > p_sys->fmt.video.i_height )
-        {
-            msg_Err( p_demux, "subscreen region overflows the screen" );
-            free( p_sys );
-            return VLC_EGENERIC;
-        }
-        else
-        {
-            p_sys->i_screen_width = p_sys->fmt.video.i_width;
-            p_sys->i_screen_height = p_sys->fmt.video.i_height;
-            p_sys->fmt.video.i_visible_width =
-            p_sys->fmt.video.i_width = p_sys->i_width;
-            p_sys->fmt.video.i_visible_height =
-            p_sys->fmt.video.i_height = p_sys->i_height;
-            p_sys->b_follow_mouse = var_CreateGetBool( p_demux,
-                                                "screen-follow-mouse" );
-            if( p_sys->b_follow_mouse )
-                msg_Dbg( p_demux, "mouse following enabled" );
-        }
+        msg_Err( p_demux, "subscreen left/top out of range" );
+        free( p_sys );
+        return VLC_EGENERIC;
     }
+
+    if( p_sys->i_width == 0 )
+        p_sys->i_width = p_sys->fmt.video.i_width - p_sys->i_left;
+    if( p_sys->i_height == 0 )
+        p_sys->i_height = p_sys->fmt.video.i_height - p_sys->i_top;
+
+    if( p_sys->i_left + p_sys->i_width > p_sys->fmt.video.i_width ||
+        p_sys->i_top + p_sys->i_height > p_sys->fmt.video.i_height )
+    {
+        msg_Err( p_demux, "subscreen region overflows the screen" );
+        free( p_sys );
+        return VLC_EGENERIC;
+    }
+    else
+    {
+        p_sys->i_screen_width = p_sys->fmt.video.i_width;
+        p_sys->i_screen_height = p_sys->fmt.video.i_height;
+        p_sys->fmt.video.i_visible_width =
+        p_sys->fmt.video.i_width = p_sys->i_width;
+        p_sys->fmt.video.i_visible_height =
+        p_sys->fmt.video.i_height = p_sys->i_height;
+        p_sys->b_follow_mouse = var_CreateGetBool( p_demux, "screen-follow-mouse" );
+        if( p_sys->b_follow_mouse )
+            msg_Dbg( p_demux, "mouse following enabled" );
+    }
+
+    msg_Dbg( p_demux, "capturing subscreen top: %d, left: %d, width: %d, height: %d",
+             p_sys->i_top, p_sys->i_left, p_sys->i_width, p_sys->i_height );
 #endif
 
 #ifdef SCREEN_MOUSE
@@ -350,6 +355,16 @@ void RenderCursor( demux_t *p_demux, int i_x, int i_y,
     demux_sys_t *p_sys = p_demux->p_sys;
     if( !p_sys->dst.i_planes )
         picture_Setup( &p_sys->dst, &p_sys->fmt.video );
+
+    if( !p_sys->dst.i_planes )
+        return;
+
+#ifdef _WIN32
+    /* Bitmaps here created by CreateDIBSection: stride rounded up to the nearest DWORD */
+    p_sys->dst.p[ 0 ].i_pitch = p_sys->dst.p[ 0 ].i_visible_pitch =
+        ( ( ( ( p_sys->fmt.video.i_width * p_sys->fmt.video.i_bits_per_pixel ) + 31 ) & ~31 ) >> 3 );
+#endif
+
     if( !p_sys->p_blend )
     {
         p_sys->p_blend = vlc_object_create( p_demux, sizeof(filter_t) );
diff --git a/modules/access/screen/win32.c b/modules/access/screen/win32.c
index d53f1a1..df3c051 100644
--- a/modules/access/screen/win32.c
+++ b/modules/access/screen/win32.c
@@ -39,12 +39,44 @@ struct screen_data_t
     HDC hdc_dst;
     BITMAPINFO bmi;
     HGDIOBJ hgdi_backup;
+    POINT ptl;             /* Coordinates of the primary display's top left, when the origin
+                            * is taken to be the top left of the entire virtual screen */
 
     int i_fragment_size;
     int i_fragment;
     block_t *p_block;
 };
 
+/*
+ * In screen coordinates the origin is the upper-left corner of the primary
+ * display, and points can have negative x/y when other displays are located
+ * to the left/top of the primary.
+ *
+ * Windows may supply these coordinates in physical or logical units
+ * depending on the version of Windows and the DPI awareness of the application.
+ * I have noticed that even different interfaces of VLC (qt, rc...) can lead
+ * to differences in DPI awareness. The choice of physical vs logical seems
+ * to be universal though (it applies to everything we use, from GetCursorPos
+ * to GetSystemMetrics and BitBlt) so we don't have to worry about anything.
+ *
+ * The only issue here is that it can be confusing to users when setting e.g.
+ * subscreen position and dimensions. This however can be controlled by
+ * disabling display scaling in the compatibility settings of the VLC executable.
+ */
+static inline void FromScreenCoordinates( demux_t *p_demux, POINT *p_point )
+{
+    screen_data_t *p_data = p_demux->p_sys->p_data;
+    p_point->x += p_data->ptl.x;
+    p_point->y += p_data->ptl.y;
+}
+
+static inline void ToScreenCoordinates( demux_t *p_demux, POINT *p_point )
+{
+    screen_data_t *p_data = p_demux->p_sys->p_data;
+    p_point->x -= p_data->ptl.x;
+    p_point->y -= p_data->ptl.y;
+}
+
 int screen_InitCapture( demux_t *p_demux )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
@@ -95,9 +127,9 @@ int screen_InitCapture( demux_t *p_demux )
 
     es_format_Init( &p_sys->fmt, VIDEO_ES, i_chroma );
     p_sys->fmt.video.i_visible_width  =
-    p_sys->fmt.video.i_width          = GetDeviceCaps( p_data->hdc_src, HORZRES );
+    p_sys->fmt.video.i_width          = GetSystemMetrics( SM_CXVIRTUALSCREEN );
     p_sys->fmt.video.i_visible_height =
-    p_sys->fmt.video.i_height         = GetDeviceCaps( p_data->hdc_src, VERTRES );
+    p_sys->fmt.video.i_height         = GetSystemMetrics( SM_CYVIRTUALSCREEN );
     p_sys->fmt.video.i_bits_per_pixel = i_bits_per_pixel;
     p_sys->fmt.video.i_sar_num = p_sys->fmt.video.i_sar_den = 1;
     p_sys->fmt.video.i_chroma         = i_chroma;
@@ -124,6 +156,9 @@ int screen_InitCapture( demux_t *p_demux )
         break;
     }
 
+    p_data->ptl.x = - GetSystemMetrics( SM_XVIRTUALSCREEN );
+    p_data->ptl.y = - GetSystemMetrics( SM_YVIRTUALSCREEN );
+
     return VLC_SUCCESS;
 }
 
@@ -219,8 +254,9 @@ static block_t *CaptureBlockNew( demux_t *p_demux )
         goto error;
 
     /* Fill all fields */
-    i_buffer = (p_sys->fmt.video.i_bits_per_pixel + 7) / 8 *
-        p_sys->fmt.video.i_width * p_sys->fmt.video.i_height;
+    int i_stride =
+        ( ( ( ( p_sys->fmt.video.i_width * p_sys->fmt.video.i_bits_per_pixel ) + 31 ) & ~31 ) >> 3 );
+    i_buffer = i_stride * p_sys->fmt.video.i_height;
     block_Init( &p_block->self, p_buffer, i_buffer );
     p_block->self.pf_release = CaptureBlockRelease;
     p_block->hbmp            = hbmp;
@@ -250,13 +286,17 @@ block_t *screen_Capture( demux_t *p_demux )
     {
         POINT pos;
         GetCursorPos( &pos );
+        FromScreenCoordinates( p_demux, &pos );
         FollowMouse( p_sys, pos.x, pos.y );
     }
 
+    POINT top_left = { p_sys->i_left, p_sys->i_top };
+    ToScreenCoordinates( p_demux, &top_left );
+
     if( !BitBlt( p_data->hdc_dst, 0,
                  p_data->i_fragment * p_data->i_fragment_size,
                  p_sys->fmt.video.i_width, p_data->i_fragment_size,
-                 p_data->hdc_src, p_sys->i_left, p_sys->i_top +
+                 p_data->hdc_src, top_left.x, top_left.y +
                  p_data->i_fragment * p_data->i_fragment_size,
                  SRCCOPY | CAPTUREBLT ) )
     {
@@ -276,7 +316,9 @@ block_t *screen_Capture( demux_t *p_demux )
         if( p_sys->p_mouse )
         {
             POINT pos;
+
             GetCursorPos( &pos );
+            FromScreenCoordinates( p_demux, &pos );
             RenderCursor( p_demux, pos.x, pos.y,
                           p_block->p_buffer );
         }
-- 
2.6.6



More information about the vlc-devel mailing list