[vlc-devel] [PATCH 3/5] doc: D3D11 callbacks: set the rendering size when the window size changes

Steve Lhomme robux4 at ycbcr.xyz
Mon May 27 13:06:48 CEST 2019


---
 doc/libvlc/d3d11_player.cpp | 51 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 50 insertions(+), 1 deletion(-)

diff --git a/doc/libvlc/d3d11_player.cpp b/doc/libvlc/d3d11_player.cpp
index a287afbb4e..9f05080a01 100644
--- a/doc/libvlc/d3d11_player.cpp
+++ b/doc/libvlc/d3d11_player.cpp
@@ -39,6 +39,11 @@ struct render_context
     ID3D11Texture2D          *texture;
     ID3D11ShaderResourceView *textureShaderInput;
     ID3D11RenderTargetView   *textureRenderTarget;
+
+    CRITICAL_SECTION sizeLock; // the ReportSize callback cannot be called during/after the Cleanup_cb is called
+    unsigned width, height;
+    void (*ReportSize)(void *ReportOpaque, unsigned width, unsigned height);
+    void *ReportOpaque;
 };
 
 static bool UpdateOutput_cb( void *opaque, const libvlc_video_direct3d_cfg_t *cfg, libvlc_video_output_cfg_t *out )
@@ -185,12 +190,28 @@ static bool Setup_cb( void **opaque, const libvlc_video_direct3d_device_cfg_t *c
 {
     struct render_context *ctx = static_cast<struct render_context *>(*opaque);
     out->device_context = ctx->d3dctx;
+
+    EnterCriticalSection(&ctx->sizeLock);
+    ctx->ReportSize = cfg->report_size_change;
+    ctx->ReportOpaque = cfg->report_opaque;
+
+    if (ctx->ReportSize != nullptr)
+    {
+        /* report our initial size */
+        ctx->ReportSize(ctx->ReportOpaque, ctx->width, ctx->height);
+    }
+    LeaveCriticalSection(&ctx->sizeLock);
+
     return true;
 }
 
 static void Cleanup_cb( void *opaque )
 {
     // here we can release all things Direct3D11 for good (if playing only one file)
+    struct render_context *ctx = static_cast<struct render_context *>( opaque );
+    EnterCriticalSection(&ctx->sizeLock);
+    ctx->ReportSize = nullptr;
+    LeaveCriticalSection(&ctx->sizeLock);
 }
 
 static const char *shaderStr = "\
@@ -378,8 +399,33 @@ static void release_direct3d(struct render_context *ctx)
 
 static LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
+    if( message == WM_CREATE )
+    {
+        /* Store p_mp for future use */
+        CREATESTRUCT *c = (CREATESTRUCT *)lParam;
+        SetWindowLongPtr( hWnd, GWLP_USERDATA, (LONG_PTR)c->lpCreateParams );
+        return 0;
+    }
+
+    LONG_PTR p_user_data = GetWindowLongPtr( hWnd, GWLP_USERDATA );
+    if( p_user_data == 0 )
+        return DefWindowProc(hWnd, message, wParam, lParam);
+    struct render_context *ctx = (struct render_context *)p_user_data;
+
     switch(message)
     {
+        case WM_SIZE:
+        {
+            /* tell libvlc that our size has changed */
+            ctx->width  = LOWORD(lParam) * (BORDER_RIGHT - BORDER_LEFT) / 2.0f; /* remove the orange part ! */
+            ctx->height = HIWORD(lParam) * (BORDER_TOP - BORDER_BOTTOM) / 2.0f;
+            EnterCriticalSection(&ctx->sizeLock);
+            if (ctx->ReportSize != nullptr)
+                ctx->ReportSize(ctx->ReportOpaque, ctx->width, ctx->height);
+            LeaveCriticalSection(&ctx->sizeLock);
+        }
+        break;
+
         case WM_DESTROY:
             PostQuitMessage(0);
             return 0;
@@ -417,6 +463,8 @@ int WINAPI WinMain(HINSTANCE hInstance,
     free( file_path );
     p_mp = libvlc_media_player_new_from_media( p_media );
 
+    InitializeCriticalSection(&Context.sizeLock);
+
     ZeroMemory(&wc, sizeof(WNDCLASSEX));
 
     wc.cbSize = sizeof(WNDCLASSEX);
@@ -441,7 +489,7 @@ int WINAPI WinMain(HINSTANCE hInstance,
                           NULL,
                           NULL,
                           hInstance,
-                          p_mp);
+                          &Context);
 
     ShowWindow(hWnd, nCmdShow);
 
@@ -476,6 +524,7 @@ int WINAPI WinMain(HINSTANCE hInstance,
     libvlc_media_release( p_media );
     libvlc_release( p_libvlc );
 
+    DeleteCriticalSection(&Context.sizeLock);
     release_direct3d(&Context);
 
     return msg.wParam;
-- 
2.17.1



More information about the vlc-devel mailing list