[dx11] CProcessorHD: Get rig of unneeded std::map.
authorAnton Fedchin <afedchin@ruswizards.com>
Mon, 27 Jul 2015 14:21:34 +0000 (17:21 +0300)
committerAnton Fedchin <afedchin@ruswizards.com>
Fri, 21 Aug 2015 06:49:39 +0000 (09:49 +0300)
xbmc/cores/VideoRenderers/DXVAHD.cpp
xbmc/cores/VideoRenderers/DXVAHD.h

index 1ca33db..fb21b70 100644 (file)
@@ -59,7 +59,6 @@ CProcessorHD::CProcessorHD()
   g_Windowing.Register(this);
 
   m_context = nullptr;
-  m_mappedResource.clear();
   m_width = 0;
   m_height = 0;
 }
@@ -84,12 +83,6 @@ void CProcessorHD::Close()
   SAFE_RELEASE(m_pEnumerator);
   SAFE_RELEASE(m_pVideoProcessor);
   SAFE_RELEASE(m_context);
-  std::map<ID3D11VideoProcessorInputView*, ID3D11Texture2D*>::iterator it = m_mappedResource.begin();
-  for (; it != m_mappedResource.end(); ++it)
-  {
-    if (it->second) it->second->Release();
-  }
-  m_mappedResource.clear();
 }
 
 bool CProcessorHD::UpdateSize(const DXVA2_VideoDesc& dsc)
@@ -125,6 +118,7 @@ bool CProcessorHD::PreInit()
     return false;
   }
 
+  memset(&m_texDesc, 0, sizeof(D3D11_TEXTURE2D_DESC));
   return true;
 }
 
@@ -382,42 +376,38 @@ bool CProcessorHD::OpenProcessor()
 
 bool CProcessorHD::CreateSurfaces()
 {
+  HRESULT hr;
+  size_t idx;
   ID3D11Device* pD3DDevice = g_Windowing.Get3D11Device();
 
   // we cannot use texture array (like in decoder) for USAGE_DYNAMIC, so create separete textures
-  CD3D11_TEXTURE2D_DESC desc(m_textureFormat, (m_width + 15) & ~15, (m_height + 15) & ~15, 1, 1, D3D11_BIND_DECODER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
-  D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC pivd = {0};
-  pivd.FourCC = 0;
-  pivd.ViewDimension = D3D11_VPIV_DIMENSION_TEXTURE2D;
+  CD3D11_TEXTURE2D_DESC texDesc(m_textureFormat, FFALIGN(m_width, 16), FFALIGN(m_height, 16), 1, 1, D3D11_BIND_DECODER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
+  D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC pivd = { 0, D3D11_VPIV_DIMENSION_TEXTURE2D };
   pivd.Texture2D.ArraySlice = 0;
   pivd.Texture2D.MipSlice = 0;
 
-  ID3D11Texture2D* resource[32];
-  ID3D11VideoProcessorInputView* views[32];
-  memset(views, 0, 32 * sizeof(ID3D11VideoProcessorInputView*));
-  memset(resource, 0, 32 * sizeof(ID3D11Texture2D*));
-  bool needRelease = false;
-
+  ID3D11VideoProcessorInputView* views[32] = { 0 };
   CLog::Log(LOGDEBUG, "%s - Creating %d processor surfaces with format %d.", __FUNCTION__, m_size, m_textureFormat);
 
-  for (unsigned idx = 0; idx < m_size; idx++)
+  for (idx = 0; idx < m_size; idx++)
   {
-    if ( FAILED(pD3DDevice->CreateTexture2D(&desc, NULL, &resource[idx]))
-      || FAILED(m_pVideoDevice->CreateVideoProcessorInputView(resource[idx], m_pEnumerator, &pivd, &views[idx])))
-    {
-      SAFE_RELEASE(resource[idx]);
-      SAFE_RELEASE(views[idx]);
-      needRelease = true;
-    }
+    ID3D11Texture2D* pTexture = nullptr;
+    hr = pD3DDevice->CreateTexture2D(&texDesc, NULL, &pTexture);
+    if (FAILED(hr))
+      break;
+
+    hr = m_pVideoDevice->CreateVideoProcessorInputView(pTexture, m_pEnumerator, &pivd, &views[idx]);
+    SAFE_RELEASE(pTexture);
+    if (FAILED(hr))
+      break;
   }
 
-  if (needRelease) 
+  if (idx != m_size)
   {
+    // something goes wrong
     CLog::Log(LOGERROR, "%s - Failed to create processor surfaces.", __FUNCTION__);
-
     for (unsigned idx = 0; idx < m_size; idx++)
     {
-      SAFE_RELEASE(resource[idx]);
       SAFE_RELEASE(views[idx]);
     }
     return false;
@@ -426,9 +416,10 @@ bool CProcessorHD::CreateSurfaces()
   m_context = new CSurfaceContext();
   for (unsigned int i = 0; i < m_size; i++)
   {
-    m_mappedResource[views[i]] = resource[i];
     m_context->AddSurface(views[i]);
   }
+
+  m_texDesc = texDesc;
   return true;
 }
 
@@ -453,18 +444,17 @@ CRenderPicture *CProcessorHD::Convert(DVDVideoPicture* picture)
   }
 
   ID3D11VideoProcessorInputView* view = reinterpret_cast<ID3D11VideoProcessorInputView*>(pView);
-  ID3D11Texture2D* texture = m_mappedResource[view];
+
+  ID3D11Resource* pResource = nullptr;
+  view->GetResource(&pResource);
 
   D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC vpivd;
   view->GetDesc(&vpivd);
-  int subresource = D3D11CalcSubresource(0, vpivd.Texture2D.ArraySlice, 1);
-
-  D3D11_TEXTURE2D_DESC sDesc;
-  texture->GetDesc(&sDesc);
+  UINT subresource = D3D11CalcSubresource(0, vpivd.Texture2D.ArraySlice, 1);
 
   D3D11_MAPPED_SUBRESOURCE rectangle;
   ID3D11DeviceContext* pContext = g_Windowing.GetImmediateContext();
-  if (FAILED(pContext->Map(texture, subresource, D3D11_MAP_WRITE_DISCARD, 0, &rectangle)))
+  if (FAILED(pContext->Map(pResource, subresource, D3D11_MAP_WRITE_DISCARD, 0, &rectangle)))
   {
     CLog::Log(LOGERROR, "%s - could not lock rect", __FUNCTION__);
     m_context->ClearReference(view);
@@ -474,7 +464,7 @@ CRenderPicture *CProcessorHD::Convert(DVDVideoPicture* picture)
   if (picture->format == RENDER_FMT_YUV420P)
   {
     uint8_t*  pData = static_cast<uint8_t*>(rectangle.pData);
-    uint8_t*  dst[] = { pData, pData + sDesc.Height * rectangle.RowPitch };
+    uint8_t*  dst[] = { pData, pData + m_texDesc.Height * rectangle.RowPitch };
     int dstStride[] = { rectangle.RowPitch, rectangle.RowPitch };
     convert_yuv420_nv12(picture->data, picture->iLineSize, picture->iHeight, picture->iWidth, dst, dstStride);
   }
@@ -482,7 +472,7 @@ CRenderPicture *CProcessorHD::Convert(DVDVideoPicture* picture)
   {
     // TODO: Optimize this later using sse2/sse4
     uint16_t * d_y = static_cast<uint16_t*>(rectangle.pData);
-    uint16_t * d_uv = d_y + sDesc.Height * rectangle.RowPitch;
+    uint16_t * d_uv = d_y + m_texDesc.Height * rectangle.RowPitch;
     // Convert to NV12 - Luma
     for (size_t line = 0; line < picture->iHeight; ++line)
     {
@@ -505,7 +495,8 @@ CRenderPicture *CProcessorHD::Convert(DVDVideoPicture* picture)
       }
     }
   }
-  pContext->Unmap(texture, subresource);
+  pContext->Unmap(pResource, subresource);
+  SAFE_RELEASE(pResource);
 
   m_context->ClearReference(view);
   m_context->MarkRender(view);
index 609c7ee..154668f 100644 (file)
@@ -98,7 +98,7 @@ protected:
 
   unsigned int                    m_procIndex;
   D3D11_VIDEO_PROCESSOR_RATE_CONVERSION_CAPS m_rateCaps;
-  std::map<ID3D11VideoProcessorInputView*, ID3D11Texture2D*> m_mappedResource;
+  D3D11_TEXTURE2D_DESC            m_texDesc;
 };
 
 };