[vlc-commits] [Git][videolan/vlc][master] direct3d11: use d3d11_quad_t as a class

Hugo Beauzée-Luyssen (@chouquette) gitlab at videolan.org
Thu Feb 24 17:30:38 UTC 2022



Hugo Beauzée-Luyssen pushed to branch master at VideoLAN / VLC


Commits:
b15b63e7 by Steve Lhomme at 2022-02-24T16:20:19+00:00
direct3d11: use d3d11_quad_t as a class

It contains ComPtr, it should not be allocated with calloc()!

Also fixes a structure leak for each region quad.

- - - - -


5 changed files:

- modules/video_output/win32/d3d11_quad.cpp
- modules/video_output/win32/d3d11_quad.h
- modules/video_output/win32/d3d11_shaders.cpp
- modules/video_output/win32/d3d11_shaders.h
- modules/video_output/win32/direct3d11.cpp


Changes:

=====================================
modules/video_output/win32/d3d11_quad.cpp
=====================================
@@ -124,14 +124,18 @@ fail:
     return false;
 }
 
-void D3D11_ReleaseQuad(d3d11_quad_t *quad)
+void d3d11_quad_t::Reset()
 {
-    quad->pPixelShaderConstants.Reset();
-    quad->vertexBuffer.Reset();
-    quad->indexBuffer.Reset();
-    quad->viewpointShaderConstant.Reset();
-    D3D11_ReleaseQuadPixelShader(quad);
-    ReleaseD3D11PictureSys(&quad->picSys);
+    pPixelShaderConstants.Reset();
+    vertexBuffer.Reset();
+    indexBuffer.Reset();
+    viewpointShaderConstant.Reset();
+    for (size_t i=0; i<ARRAY_SIZE(d3dpixelShader); i++)
+    {
+        d3dpixelShader[i].Reset();
+        SamplerStates[i].Reset();
+    }
+    ReleaseD3D11PictureSys(&picSys);
 }
 
 #undef D3D11_UpdateQuadPosition
@@ -272,7 +276,7 @@ int D3D11_AllocateQuad(vlc_object_t *o, d3d11_device_t *d3d_dev,
     return VLC_SUCCESS;
 
 error:
-    D3D11_ReleaseQuad(quad);
+    quad->Reset();
     return VLC_EGENERIC;
 }
 
@@ -294,26 +298,26 @@ int D3D11_SetupQuad(vlc_object_t *o, d3d11_device_t *d3d_dev, const video_format
     return VLC_SUCCESS;
 }
 
-void D3D11_UpdateViewport(d3d11_quad_t *quad, const RECT *rect, const d3d_format_t *display)
+void d3d11_quad_t::UpdateViewport(const RECT *rect, const d3d_format_t *display)
 {
     LONG srcAreaWidth, srcAreaHeight;
 
     srcAreaWidth  = RECTWidth(*rect);
     srcAreaHeight = RECTHeight(*rect);
 
-    quad->cropViewport[0].TopLeftX = rect->left;
-    quad->cropViewport[0].TopLeftY = rect->top;
-    quad->cropViewport[0].Width    = srcAreaWidth;
-    quad->cropViewport[0].Height   = srcAreaHeight;
+    cropViewport[0].TopLeftX = rect->left;
+    cropViewport[0].TopLeftY = rect->top;
+    cropViewport[0].Width    = srcAreaWidth;
+    cropViewport[0].Height   = srcAreaHeight;
 
-    switch ( quad->generic.textureFormat->formatTexture )
+    switch ( generic.textureFormat->formatTexture )
     {
     case DXGI_FORMAT_NV12:
     case DXGI_FORMAT_P010:
-        quad->cropViewport[1].TopLeftX = rect->left / 2;
-        quad->cropViewport[1].TopLeftY = rect->top / 2;
-        quad->cropViewport[1].Width    = srcAreaWidth / 2;
-        quad->cropViewport[1].Height   = srcAreaHeight / 2;
+        cropViewport[1].TopLeftX = rect->left / 2;
+        cropViewport[1].TopLeftY = rect->top / 2;
+        cropViewport[1].Width    = srcAreaWidth / 2;
+        cropViewport[1].Height   = srcAreaHeight / 2;
         break;
     case DXGI_FORMAT_R8G8B8A8_UNORM:
     case DXGI_FORMAT_B8G8R8A8_UNORM:
@@ -328,29 +332,29 @@ void D3D11_UpdateViewport(d3d11_quad_t *quad, const RECT *rect, const d3d_format
         if ( display->formatTexture == DXGI_FORMAT_NV12 ||
              display->formatTexture == DXGI_FORMAT_P010 )
         {
-            quad->cropViewport[1].TopLeftX = rect->left / 2;
-            quad->cropViewport[1].TopLeftY = rect->top / 2;
-            quad->cropViewport[1].Width    = srcAreaWidth / 2;
-            quad->cropViewport[1].Height   = srcAreaHeight / 2;
+            cropViewport[1].TopLeftX = rect->left / 2;
+            cropViewport[1].TopLeftY = rect->top / 2;
+            cropViewport[1].Width    = srcAreaWidth / 2;
+            cropViewport[1].Height   = srcAreaHeight / 2;
         }
         break;
     case DXGI_FORMAT_UNKNOWN:
-        switch ( quad->generic.textureFormat->fourcc )
+        switch ( generic.textureFormat->fourcc )
         {
         case VLC_CODEC_YUVA:
             if ( display->formatTexture != DXGI_FORMAT_NV12 &&
                  display->formatTexture != DXGI_FORMAT_P010 )
             {
-                quad->cropViewport[1] = quad->cropViewport[0];
+                cropViewport[1] = cropViewport[0];
                 break;
             }
             /* fallthrough */
         case VLC_CODEC_I420_10L:
         case VLC_CODEC_I420:
-            quad->cropViewport[1].TopLeftX = quad->cropViewport[0].TopLeftX / 2;
-            quad->cropViewport[1].TopLeftY = quad->cropViewport[0].TopLeftY / 2;
-            quad->cropViewport[1].Width    = quad->cropViewport[0].Width / 2;
-            quad->cropViewport[1].Height   = quad->cropViewport[0].Height / 2;
+            cropViewport[1].TopLeftX = cropViewport[0].TopLeftX / 2;
+            cropViewport[1].TopLeftY = cropViewport[0].TopLeftY / 2;
+            cropViewport[1].Width    = cropViewport[0].Width / 2;
+            cropViewport[1].Height   = cropViewport[0].Height / 2;
             break;
         }
         break;


=====================================
modules/video_output/win32/d3d11_quad.h
=====================================
@@ -38,8 +38,6 @@ void D3D11_RenderQuad(d3d11_device_t *, d3d11_quad_t *, d3d11_vertex_shader_t *,
 int D3D11_AllocateQuad(vlc_object_t *, d3d11_device_t *, video_projection_mode_t, d3d11_quad_t *);
 #define D3D11_AllocateQuad(a,b,c,d)  D3D11_AllocateQuad(VLC_OBJECT(a),b,c,d)
 
-void D3D11_ReleaseQuad(d3d11_quad_t *);
-
 int D3D11_SetupQuad(vlc_object_t *, d3d11_device_t *, const video_format_t *, d3d11_quad_t *,
                     const display_info_t *);
 #define D3D11_SetupQuad(a,b,c,d,e)  D3D11_SetupQuad(VLC_OBJECT(a),b,c,d,e)
@@ -48,8 +46,6 @@ bool D3D11_UpdateQuadPosition( vlc_object_t *, d3d11_device_t *, d3d11_quad_t *,
                                const RECT *output, video_orientation_t );
 #define D3D11_UpdateQuadPosition(a,b,c,d,e)  D3D11_UpdateQuadPosition(VLC_OBJECT(a),b,c,d,e)
 
-void D3D11_UpdateViewport(d3d11_quad_t *, const RECT *, const d3d_format_t *display);
-
 void D3D11_UpdateQuadOpacity(vlc_object_t *, d3d11_device_t *, d3d11_quad_t *, float opacity);
 #define D3D11_UpdateQuadOpacity(a,b,c,d)  D3D11_UpdateQuadOpacity(VLC_OBJECT(a),b,c,d)
 


=====================================
modules/video_output/win32/d3d11_shaders.cpp
=====================================
@@ -91,15 +91,6 @@ HRESULT D3D11_SetQuadPixelShader(vlc_object_t *o, d3d11_device_t *d3d_dev,
     return hr;
 }
 
-void D3D11_ReleaseQuadPixelShader(d3d11_quad_t *quad)
-{
-    for (size_t i=0; i<ARRAY_SIZE(quad->d3dpixelShader); i++)
-    {
-        quad->d3dpixelShader[i].Reset();
-        quad->SamplerStates[i].Reset();
-    }
-}
-
 HRESULT D3D11_CreateRenderTargets( d3d11_device_t *d3d_dev, ID3D11Resource *texture,
                                    const d3d_format_t *cfg, ComPtr<ID3D11RenderTargetView> output[DXGI_MAX_RENDER_TARGET] )
 {


=====================================
modules/video_output/win32/d3d11_shaders.h
=====================================
@@ -39,8 +39,16 @@ struct d3d11_vertex_shader_t {
 /* A Quad is texture that can be displayed in a rectangle */
 struct d3d11_quad_t
 {
-    picture_sys_d3d11_t       picSys;
-    d3d_quad_t                generic;
+    ~d3d11_quad_t()
+    {
+        ReleaseD3D11PictureSys(&picSys);
+    }
+
+    void Reset();
+    void UpdateViewport(const RECT *, const d3d_format_t *display);
+
+    picture_sys_d3d11_t       picSys = {};
+    d3d_quad_t                generic = {};
     UINT                      resourceCount = 0;
     Microsoft::WRL::ComPtr<ID3D11Buffer> vertexBuffer;
     Microsoft::WRL::ComPtr<ID3D11Buffer> indexBuffer;
@@ -65,7 +73,6 @@ HRESULT D3D11_CompilePixelShaderBlob(vlc_object_t *, const d3d_shader_compiler_t
 HRESULT D3D11_SetQuadPixelShader(vlc_object_t *, d3d11_device_t *,
                                  bool sharp,
                                  d3d11_quad_t *quad, d3d_shader_blob pPSBlob[DXGI_MAX_RENDER_TARGET]);
-void D3D11_ReleaseQuadPixelShader(d3d11_quad_t *);
 
 HRESULT D3D11_CompileVertexShaderBlob(vlc_object_t *, const d3d_shader_compiler_t *,
                                       d3d11_device_t *, bool flat, d3d_shader_blob *);


=====================================
modules/video_output/win32/direct3d11.cpp
=====================================
@@ -265,7 +265,7 @@ static void UpdateSize(vout_display_t *vd)
     rect_dst.top    = sys->area.place.y;
     rect_dst.bottom = sys->area.place.y + sys->area.place.height;
 
-    D3D11_UpdateViewport( &sys->picQuad, &rect_dst, sys->display.pixelFormat );
+    sys->picQuad.UpdateViewport( &rect_dst, sys->display.pixelFormat );
 
     RECT source_rect;
     source_rect.left   = vd->source->i_x_offset;
@@ -1208,7 +1208,7 @@ static int Direct3D11CreateGenericResources(vout_display_t *vd)
     rect_dst.top    = sys->area.place.y;
     rect_dst.bottom = sys->area.place.y + sys->area.place.height;
 
-    D3D11_UpdateViewport( &sys->picQuad, &rect_dst, sys->display.pixelFormat );
+    sys->picQuad.UpdateViewport( &rect_dst, sys->display.pixelFormat );
 
 #ifndef NDEBUG
     msg_Dbg( vd, "picQuad position (%.02f,%.02f) %.02fx%.02f",
@@ -1227,10 +1227,10 @@ static void Direct3D11DestroyResources(vout_display_t *vd)
 {
     vout_display_sys_t *sys = static_cast<vout_display_sys_t *>(vd->sys);
 
-    D3D11_ReleaseQuad(&sys->picQuad);
+    sys->picQuad.Reset();
     Direct3D11DeleteRegions(sys->d3dregion_count, sys->d3dregions);
     sys->d3dregion_count = 0;
-    D3D11_ReleaseQuad(&sys->regionQuad);
+    sys->regionQuad.Reset();
 
     ReleaseD3D11PictureSys(&sys->stagingSys);
 
@@ -1262,7 +1262,8 @@ static void Direct3D11DeleteRegions(int count, picture_t **region)
 
 static void DestroyPictureQuad(picture_t *p_picture)
 {
-    D3D11_ReleaseQuad( (d3d11_quad_t *) p_picture->p_sys );
+    d3d11_quad_t *quad = static_cast<d3d11_quad_t *>(p_picture->p_sys);
+    delete quad;
 }
 
 static int Direct3D11MapSubpicture(vout_display_t *vd, int *subpicture_region_count,
@@ -1293,8 +1294,9 @@ static int Direct3D11MapSubpicture(vout_display_t *vd, int *subpicture_region_co
 
         for (int j = 0; j < sys->d3dregion_count; j++) {
             picture_t *cache = sys->d3dregions[j];
-            if (cache != NULL && ((d3d11_quad_t *) cache->p_sys)->picSys.texture[KNOWN_DXGI_INDEX]) {
-                ((d3d11_quad_t *) cache->p_sys)->picSys.texture[KNOWN_DXGI_INDEX]->GetDesc(&texDesc );
+            d3d11_quad_t *cache_quad = cache ? static_cast<d3d11_quad_t *>(cache->p_sys) : nullptr;
+            if (cache_quad != nullptr && cache_quad->picSys.texture[KNOWN_DXGI_INDEX]) {
+                cache_quad->picSys.texture[KNOWN_DXGI_INDEX]->GetDesc(&texDesc );
                 if (texDesc.Format == sys->regionQuad.generic.textureFormat->formatTexture &&
                     texDesc.Width  == r->p_picture->format.i_width &&
                     texDesc.Height == r->p_picture->format.i_height) {
@@ -1311,7 +1313,7 @@ static int Direct3D11MapSubpicture(vout_display_t *vd, int *subpicture_region_co
             quad = static_cast<d3d11_quad_t*>(quad_picture->p_sys);
         else
         {
-            d3d11_quad_t *d3dquad = static_cast<d3d11_quad_t *>(calloc(1, sizeof(*d3dquad)));
+            d3d11_quad_t *d3dquad = new (std::nothrow) d3d11_quad_t;
             if (unlikely(d3dquad==NULL)) {
                 continue;
             }
@@ -1322,7 +1324,7 @@ static int Direct3D11MapSubpicture(vout_display_t *vd, int *subpicture_region_co
                 for (int j=0; j<DXGI_MAX_SHADER_VIEW; j++)
                     if (d3dquad->picSys.texture[j])
                         d3dquad->picSys.texture[j]->Release();
-                free(d3dquad);
+                delete d3dquad;
                 continue;
             }
 
@@ -1331,7 +1333,7 @@ static int Direct3D11MapSubpicture(vout_display_t *vd, int *subpicture_region_co
                                            d3dquad->picSys.renderSrc)) {
                 msg_Err(vd, "Failed to create %dx%d shader view for OSD",
                         r->fmt.i_visible_width, r->fmt.i_visible_height);
-                free(d3dquad);
+                delete d3dquad;
                 continue;
             }
             d3dquad->generic.i_width  = r->fmt.i_width;
@@ -1343,7 +1345,7 @@ static int Direct3D11MapSubpicture(vout_display_t *vd, int *subpicture_region_co
             {
                 msg_Err(vd, "Failed to allocate %dx%d quad for OSD",
                              r->fmt.i_visible_width, r->fmt.i_visible_height);
-                free(d3dquad);
+                delete d3dquad;
                 continue;
             }
 
@@ -1351,7 +1353,7 @@ static int Direct3D11MapSubpicture(vout_display_t *vd, int *subpicture_region_co
             if (err != VLC_SUCCESS) {
                 msg_Err(vd, "Failed to setup %dx%d quad for OSD",
                         r->fmt.i_visible_width, r->fmt.i_visible_height);
-                free(d3dquad);
+                delete d3dquad;
                 continue;
             }
             const auto picres = [](picture_sys_d3d11_t * p_sys){
@@ -1364,7 +1366,7 @@ static int Direct3D11MapSubpicture(vout_display_t *vd, int *subpicture_region_co
             if ((*region)[i] == NULL) {
                 msg_Err(vd, "Failed to create %dx%d picture for OSD",
                         r->fmt.i_width, r->fmt.i_height);
-                D3D11_ReleaseQuad(d3dquad);
+                d3dquad->Reset();
                 continue;
             }
             quad_picture = (*region)[i];
@@ -1378,12 +1380,12 @@ static int Direct3D11MapSubpicture(vout_display_t *vd, int *subpicture_region_co
             }
         }
 
-        hr = sys->d3d_dev->d3dcontext->Map(((d3d11_quad_t *) quad_picture->p_sys)->picSys.resource[KNOWN_DXGI_INDEX], 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
+        hr = sys->d3d_dev->d3dcontext->Map(quad->picSys.resource[KNOWN_DXGI_INDEX], 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
         if( SUCCEEDED(hr) ) {
             err = picture_UpdatePlanes(quad_picture, static_cast<uint8_t*>(mappedResource.pData), mappedResource.RowPitch);
             if (err != VLC_SUCCESS) {
                 msg_Err(vd, "Failed to set the buffer on the SPU picture" );
-                sys->d3d_dev->d3dcontext->Unmap(((d3d11_quad_t *) quad_picture->p_sys)->picSys.resource[KNOWN_DXGI_INDEX], 0);
+                sys->d3d_dev->d3dcontext->Unmap(quad->picSys.resource[KNOWN_DXGI_INDEX], 0);
                 picture_Release(quad_picture);
                 if ((*region)[i] == quad_picture)
                     (*region)[i] = NULL;
@@ -1392,7 +1394,7 @@ static int Direct3D11MapSubpicture(vout_display_t *vd, int *subpicture_region_co
 
             picture_CopyPixels(quad_picture, r->p_picture);
 
-            sys->d3d_dev->d3dcontext->Unmap(((d3d11_quad_t *) quad_picture->p_sys)->picSys.resource[KNOWN_DXGI_INDEX], 0);
+            sys->d3d_dev->d3dcontext->Unmap(quad->picSys.resource[KNOWN_DXGI_INDEX], 0);
         } else {
             msg_Err(vd, "Failed to map the SPU texture (hr=0x%lX)", hr );
             picture_Release(quad_picture);
@@ -1432,7 +1434,7 @@ static int Direct3D11MapSubpicture(vout_display_t *vd, int *subpicture_region_co
         spuViewport.top    += sys->area.place.y;
         spuViewport.bottom += sys->area.place.y;
 
-        D3D11_UpdateViewport( quad, &spuViewport, sys->display.pixelFormat );
+        quad->UpdateViewport( &spuViewport, sys->display.pixelFormat );
 
         D3D11_UpdateQuadOpacity(vd, sys->d3d_dev, quad, r->i_alpha / 255.0f );
     }



View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/b15b63e7d807151ee40cc782b0161d827f0ecd07

-- 
View it on GitLab: https://code.videolan.org/videolan/vlc/-/commit/b15b63e7d807151ee40cc782b0161d827f0ecd07
You're receiving this email because of your account on code.videolan.org.


VideoLAN code repository instance


More information about the vlc-commits mailing list