kodi
DeviceResources.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "DirectXHelper.h"
12 #include "HDRStatus.h"
13 #include "guilib/D3DResource.h"
14 
15 #include <functional>
16 #include <memory>
17 
18 #include <concrt.h>
19 #include <dxgi1_5.h>
20 #include <wrl.h>
21 #include <wrl/client.h>
22 
23 struct RESOLUTION_INFO;
24 struct DEBUG_INFO_RENDER;
25 struct VideoDriverInfo;
26 
27 namespace DX
28 {
29  interface IDeviceNotify
30  {
31  virtual void OnDXDeviceLost() = 0;
32  virtual void OnDXDeviceRestored() = 0;
33  };
34 
35  // Controls all the DirectX device resources.
37  {
38  public:
39  static std::shared_ptr<DX::DeviceResources> Get();
40 
42  virtual ~DeviceResources();
43  void Release();
44 
45  void ValidateDevice();
46  void HandleDeviceLost(bool removed);
47  bool Begin();
48  void Present();
49 
50  // The size of the render target, in pixels.
51  winrt::Windows::Foundation::Size GetOutputSize() const { return m_outputSize; }
52  // The size of the render target, in dips.
53  winrt::Windows::Foundation::Size GetLogicalSize() const { return m_logicalSize; }
54  void SetLogicalSize(float width, float height);
55  float GetDpi() const { return m_effectiveDpi; }
56  void SetDpi(float dpi);
57 
58  // D3D Accessors.
59  bool HasValidDevice() const { return m_bDeviceCreated; }
60  ID3D11Device1* GetD3DDevice() const { return m_d3dDevice.Get(); }
61  ID3D11DeviceContext1* GetD3DContext() const { return m_deferrContext.Get(); }
62  ID3D11DeviceContext1* GetImmediateContext() const { return m_d3dContext.Get(); }
63  IDXGISwapChain1* GetSwapChain() const { return m_swapChain.Get(); }
64  IDXGIFactory2* GetIDXGIFactory2() const { return m_dxgiFactory.Get(); }
65  IDXGIAdapter1* GetAdapter() const { return m_adapter.Get(); }
66  ID3D11DepthStencilView* GetDSV() const { return m_d3dDepthStencilView.Get(); }
67  D3D_FEATURE_LEVEL GetDeviceFeatureLevel() const { return m_d3dFeatureLevel; }
68  CD3DTexture& GetBackBuffer() { return m_backBufferTex; }
69 
70  void GetOutput(IDXGIOutput** ppOutput) const;
77  void GetCachedOutputAndDesc(IDXGIOutput** output, DXGI_OUTPUT_DESC* outputDesc) const;
78  DXGI_ADAPTER_DESC GetAdapterDesc() const;
79  void GetDisplayMode(DXGI_MODE_DESC *mode) const;
80 
81  D3D11_VIEWPORT GetScreenViewport() const { return m_screenViewport; }
82  void SetViewPort(D3D11_VIEWPORT& viewPort) const;
83 
84  void ReleaseBackBuffer();
85  void CreateBackBuffer();
86  void ResizeBuffers();
87 
88  bool SetFullScreen(bool fullscreen, RESOLUTION_INFO& res);
89 
90  // Apply display settings changes
91  void ApplyDisplaySettings();
92 
93  // HDR display support
94  HDR_STATUS ToggleHDR();
95  void SetHdrMetaData(DXGI_HDR_METADATA_HDR10& hdr10) const;
96  void SetHdrColorSpace(const DXGI_COLOR_SPACE_TYPE colorSpace);
97  bool IsHDROutput() const { return m_IsHDROutput; }
98  bool IsTransferPQ() const { return m_IsTransferPQ; }
99 
100  // DX resources registration
101  void Register(ID3DResource *resource);
102  void Unregister(ID3DResource *resource);
103 
104  void FinishCommandList(bool bExecute = true) const;
105  void ClearDepthStencil() const;
106  void ClearRenderTarget(ID3D11RenderTargetView* pRTView, float color[4]) const;
107  void RegisterDeviceNotify(IDeviceNotify* deviceNotify);
108 
109  bool IsStereoAvailable() const;
110  bool IsStereoEnabled() const { return m_stereoEnabled; }
111  void SetStereoIdx(byte idx) { m_backBufferTex.SetViewIdx(idx); }
112 
113  void SetMonitor(HMONITOR monitor);
114  HMONITOR GetMonitor() const;
115 #if defined(TARGET_WINDOWS_DESKTOP)
116  void SetWindow(HWND window);
117 #elif defined(TARGET_WINDOWS_STORE)
118  void Trim() const;
119  void SetWindow(const winrt::Windows::UI::Core::CoreWindow& window);
120  void SetWindowPos(winrt::Windows::Foundation::Rect rect);
121 #endif // TARGET_WINDOWS_STORE
122  bool IsNV12SharedTexturesSupported() const { return m_NV12SharedTexturesSupport; }
123  bool IsDXVA2SharedDecoderSurfaces() const { return m_DXVA2SharedDecoderSurfaces; }
124  bool IsSuperResolutionSupported() const { return m_DXVASuperResolutionSupport; }
125  bool UseFence() const { return m_DXVA2UseFence; }
126 
127  // Gets debug info from swapchain
128  DEBUG_INFO_RENDER GetDebugInfo() const;
129  std::vector<DXGI_COLOR_SPACE_TYPE> GetSwapChainColorSpaces() const;
130  bool SetMultithreadProtected(bool enabled) const;
131  bool IsGCNOrOlder() const;
132 
133  private:
134  class CBackBuffer : public CD3DTexture
135  {
136  public:
137  CBackBuffer() : CD3DTexture() {}
138  void SetViewIdx(unsigned idx) { m_viewIdx = idx; }
139  bool Acquire(ID3D11Texture2D* pTexture);
140  };
141 
142  HRESULT CreateSwapChain(DXGI_SWAP_CHAIN_DESC1 &desc, DXGI_SWAP_CHAIN_FULLSCREEN_DESC &fsDesc, IDXGISwapChain1 **ppSwapChain) const;
143  void DestroySwapChain();
144  void CreateDeviceIndependentResources();
145  void CreateDeviceResources();
146  void CreateWindowSizeDependentResources();
147  void UpdateRenderTargetSize();
148  void OnDeviceLost(bool removed);
149  void OnDeviceRestored();
150  void HandleOutputChange(const std::function<bool(DXGI_OUTPUT_DESC)>& cmpFunc);
151  bool CreateFactory();
152  void CheckNV12SharedTexturesSupport();
153  VideoDriverInfo GetVideoDriverVersion();
154  void CheckDXVA2SharedDecoderSurfaces();
155 
156  HWND m_window{ nullptr };
157 #if defined(TARGET_WINDOWS_STORE)
158  winrt::Windows::UI::Core::CoreWindow m_coreWindow = nullptr;
159 #endif
160  Microsoft::WRL::ComPtr<IDXGIFactory2> m_dxgiFactory;
161  Microsoft::WRL::ComPtr<IDXGIAdapter1> m_adapter;
162  Microsoft::WRL::ComPtr<IDXGIOutput1> m_output;
163  DXGI_OUTPUT_DESC m_outputDesc{};
164 
165  Microsoft::WRL::ComPtr<ID3D11Device1> m_d3dDevice;
166  Microsoft::WRL::ComPtr<ID3D11DeviceContext1> m_d3dContext;
167  Microsoft::WRL::ComPtr<ID3D11DeviceContext1> m_deferrContext;
168  Microsoft::WRL::ComPtr<IDXGISwapChain1> m_swapChain;
169 #ifdef _DEBUG
170  Microsoft::WRL::ComPtr<ID3D11Debug> m_d3dDebug;
171 #endif
172 
173  CBackBuffer m_backBufferTex;
174  Microsoft::WRL::ComPtr<ID3D11DepthStencilView> m_d3dDepthStencilView;
175  D3D11_VIEWPORT m_screenViewport;
176 
177  // Cached device properties.
178  D3D_FEATURE_LEVEL m_d3dFeatureLevel;
179  winrt::Windows::Foundation::Size m_outputSize;
180  winrt::Windows::Foundation::Size m_logicalSize;
181  float m_dpi;
182 
183  // This is the DPI that will be reported back to the app. It takes into account whether the app supports high resolution screens or not.
184  float m_effectiveDpi;
185  // The IDeviceNotify can be held directly as it owns the DeviceResources.
186  IDeviceNotify* m_deviceNotify;
187 
188  // scritical section
189  Concurrency::critical_section m_criticalSection;
190  Concurrency::critical_section m_resourceSection;
191  std::vector<ID3DResource*> m_resources;
192 
193  bool m_stereoEnabled;
194  bool m_bDeviceCreated;
195  bool m_IsHDROutput;
196  bool m_IsTransferPQ;
197  bool m_NV12SharedTexturesSupport{false};
198  bool m_DXVA2SharedDecoderSurfaces{false};
199  bool m_DXVASuperResolutionSupport{false};
200  bool m_DXVA2UseFence{false};
201  };
202 }
Definition: D3DResource.h:90
Definition: DeviceResources.h:27
Provide info of a resolution.
Definition: Resolution.h:66
Definition: DebugInfo.h:30
Definition: D3DResource.h:36
Definition: DeviceResources.h:36
Definition: WIN32Util.h:23