HRESULT DisplayerImpl::CreateDeviceResources() { UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT; HRESULT hr = CreateD3D11Device(nullptr, D3D_DRIVER_TYPE_HARDWARE, creationFlags, &mD3DDevice, &mD3DDeviceContext, &mCurrentFeatureLevel); if (FAILED(hr)) { // fallback to software wrap device hr = CreateD3D11Device(nullptr, D3D_DRIVER_TYPE_WARP, creationFlags, &mD3DDevice, &mD3DDeviceContext, &mCurrentFeatureLevel); } if (FAILED(hr)) return hr; hr = mD3DDevice.As(&mDxgiDevice); if (FAILED(hr)) return hr; hr = mD2DFactory->CreateDevice(mDxgiDevice.Get(), &mD2DDevice); if (FAILED(hr)) return hr; hr = mD2DDevice->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &mDeviceContext); if (FAILED(hr)) return hr; hr = mD2DDevice->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &mLendContext); if (FAILED(hr)) return hr; return hr; }
// Creates the application window, the d3d device and DirectComposition device and visual tree // before entering the message loop. HRESULT CApplication::BeforeEnteringMessageLoop() { HRESULT hr = CreateApplicationWindow(); if (SUCCEEDED(hr)) { hr = CreateD3D11Device(); } if (SUCCEEDED(hr)) { hr = CreateD2D1Factory(); } if (SUCCEEDED(hr)) { hr = CreateD2D1Device(); } if (SUCCEEDED(hr)) { hr = CreateDCompositionDevice(); } if (SUCCEEDED(hr)) { hr = CreateDCompositionVisualTree(); } return hr; }
void Render() { if (!m_renderTarget) { auto d3dDevice = CreateD3D11Device(); m_renderTarget = CreateRenderTarget(m_d2dFactory, d3dDevice); m_swapChain = CreateSwapChainForHwnd(d3dDevice, __super::m_hWnd); CreateDeviceSwapChainBitmap(m_swapChain, m_renderTarget); static_cast<T *>(this)->CreateDeviceResources(); static_cast<T *>(this)->CreateDeviceSizeResources(); } m_renderTarget->BeginDraw(); // NOTE: no fallback is provided in this base class so all derived classes must implement this static_cast<T *>(this)->Draw(); m_renderTarget->EndDraw(); auto const hr = m_swapChain->Present(1, 0); if (S_OK != hr && DXGI_STATUS_OCCLUDED != hr) { ReleaseDevices(); } }
HRESULT CApplication::BeforeEnteringMessageLoop() { HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE); if (SUCCEEDED(hr)) { hr = CreateApplicationWindow(); } if (SUCCEEDED(hr)) { hr = CreateD3D11Device(); } if (SUCCEEDED(hr)) { hr = CreateD2D1Factory(); } if (SUCCEEDED(hr)) { hr = CreateD2D1Device(); } if (SUCCEEDED(hr)) { hr = CreateWICFactory(); } if (SUCCEEDED(hr)) { hr = CreateAnimationManager(); } if (SUCCEEDED(hr)) { hr = CreateAnimationTransitionLibrary(); } if (SUCCEEDED(hr)) { hr = CreateAnimationVariables(); } if (SUCCEEDED(hr)) { hr = CreateDCompositionDevice(); } if (SUCCEEDED(hr)) { hr = CreateDCompositionRenderTarget(); } if (SUCCEEDED(hr)) { hr = CreateDCompositionVisualTree(); } if (SUCCEEDED(hr)) { hr = AttachDCompositionVisualTreeToRenderTarget(); } if (SUCCEEDED(hr)) { hr = _device->Commit(); } return hr; }
STDMETHODIMP CDecD3D11::PostConnect(IPin *pPin) { DbgLog((LOG_TRACE, 10, L"CDecD3D11::PostConnect()")); HRESULT hr = S_OK; ID3D11DecoderConfiguration *pD3D11DecoderConfiguration = nullptr; hr = pPin->QueryInterface(&pD3D11DecoderConfiguration); if (FAILED(hr)) { DbgLog((LOG_ERROR, 10, L"-> ID3D11DecoderConfiguration not available, using fallback mode")); } // Release old D3D resources, we're about to re-init m_pCallback->ReleaseAllDXVAResources(); // free the decoder to force a re-init down the line SafeRelease(&m_pDecoder); // and the old device av_buffer_unref(&m_pDevCtx); // device id UINT nDevice = m_pSettings->GetHWAccelDeviceIndex(HWAccel_D3D11, nullptr); // in automatic mode use the device the renderer gives us if (nDevice == LAVHWACCEL_DEVICE_DEFAULT && pD3D11DecoderConfiguration) { nDevice = pD3D11DecoderConfiguration->GetD3D11AdapterIndex(); } else { // if a device is specified manually, fallback to copy-back and use the selected device SafeRelease(&pD3D11DecoderConfiguration); // use the configured device if (nDevice == LAVHWACCEL_DEVICE_DEFAULT) nDevice = 0; } // create the device ID3D11Device *pD3D11Device = nullptr; hr = CreateD3D11Device(nDevice, &pD3D11Device, &m_AdapterDesc); if (FAILED(hr)) { goto fail; } // allocate and fill device context m_pDevCtx = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_D3D11VA); AVD3D11VADeviceContext *pDeviceContext = (AVD3D11VADeviceContext *)((AVHWDeviceContext *)m_pDevCtx->data)->hwctx; pDeviceContext->device = pD3D11Device; // finalize the context int ret = av_hwdevice_ctx_init(m_pDevCtx); if (ret < 0) { av_buffer_unref(&m_pDevCtx); goto fail; } // check if the connection supports native mode if (pD3D11DecoderConfiguration) { CMediaType mt = m_pCallback->GetOutputMediaType(); if ((m_SurfaceFormat == DXGI_FORMAT_NV12 && mt.subtype != MEDIASUBTYPE_NV12) || (m_SurfaceFormat == DXGI_FORMAT_P010 && mt.subtype != MEDIASUBTYPE_P010) || (m_SurfaceFormat == DXGI_FORMAT_P016 && mt.subtype != MEDIASUBTYPE_P016)) { DbgLog((LOG_ERROR, 10, L"-> Connection is not the appropriate pixel format for D3D11 Native")); SafeRelease(&pD3D11DecoderConfiguration); } } // verify hardware support { GUID guidConversion = GUID_NULL; hr = FindVideoServiceConversion(m_pAVCtx->codec_id, m_pAVCtx->profile, m_SurfaceFormat, &guidConversion); if (FAILED(hr)) { goto fail; } // get decoder configuration D3D11_VIDEO_DECODER_DESC desc = { 0 }; desc.Guid = guidConversion; desc.OutputFormat = m_SurfaceFormat; desc.SampleWidth = m_pAVCtx->coded_width; desc.SampleHeight = m_pAVCtx->coded_height; D3D11_VIDEO_DECODER_CONFIG decoder_config = { 0 }; hr = FindDecoderConfiguration(&desc, &decoder_config); if (FAILED(hr)) { goto fail; } // test creating a texture D3D11_TEXTURE2D_DESC texDesc = { 0 }; texDesc.Width = m_pAVCtx->coded_width; texDesc.Height = m_pAVCtx->coded_height; texDesc.MipLevels = 1; texDesc.ArraySize = 10; texDesc.Format = m_SurfaceFormat; texDesc.SampleDesc.Count = 1; texDesc.Usage = D3D11_USAGE_DEFAULT; texDesc.BindFlags = D3D11_BIND_DECODER | D3D11_BIND_SHADER_RESOURCE; texDesc.MiscFlags = D3D11_RESOURCE_MISC_SHARED; ID3D11Texture2D *pTexture2D = nullptr; hr = pD3D11Device->CreateTexture2D(&texDesc, nullptr, &pTexture2D); if (FAILED(hr)) { goto fail; } SafeRelease(&pTexture2D); } // Notice the connected pin that we're sending D3D11 textures if (pD3D11DecoderConfiguration) { hr = pD3D11DecoderConfiguration->ActivateD3D11Decoding(pDeviceContext->device, pDeviceContext->device_context, pDeviceContext->lock_ctx, 0); SafeRelease(&pD3D11DecoderConfiguration); m_bReadBackFallback = FAILED(hr); } else { m_bReadBackFallback = true; } return S_OK; fail: SafeRelease(&pD3D11DecoderConfiguration); return E_FAIL; }