xbmc
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;
71  void GetAdapterDesc(DXGI_ADAPTER_DESC *desc) const;
72  void GetDisplayMode(DXGI_MODE_DESC *mode) const;
73 
74  D3D11_VIEWPORT GetScreenViewport() const { return m_screenViewport; }
75  void SetViewPort(D3D11_VIEWPORT& viewPort) const;
76 
77  void ReleaseBackBuffer();
78  void CreateBackBuffer();
79  void ResizeBuffers();
80 
81  bool SetFullScreen(bool fullscreen, RESOLUTION_INFO& res);
82 
83  // Apply display settings changes
84  void ApplyDisplaySettings();
85 
86  // HDR display support
87  HDR_STATUS ToggleHDR();
88  void SetHdrMetaData(DXGI_HDR_METADATA_HDR10& hdr10) const;
89  void SetHdrColorSpace(const DXGI_COLOR_SPACE_TYPE colorSpace);
90  bool IsHDROutput() const { return m_IsHDROutput; }
91  bool IsTransferPQ() const { return m_IsTransferPQ; }
92 
93  // DX resources registration
94  void Register(ID3DResource *resource);
95  void Unregister(ID3DResource *resource);
96 
97  void FinishCommandList(bool bExecute = true) const;
98  void ClearDepthStencil() const;
99  void ClearRenderTarget(ID3D11RenderTargetView* pRTView, float color[4]) const;
100  void RegisterDeviceNotify(IDeviceNotify* deviceNotify);
101 
102  bool IsStereoAvailable() const;
103  bool IsStereoEnabled() const { return m_stereoEnabled; }
104  void SetStereoIdx(byte idx) { m_backBufferTex.SetViewIdx(idx); }
105 
106  void SetMonitor(HMONITOR monitor);
107  HMONITOR GetMonitor() const;
108 #if defined(TARGET_WINDOWS_DESKTOP)
109  void SetWindow(HWND window);
110 #elif defined(TARGET_WINDOWS_STORE)
111  void Trim() const;
112  void SetWindow(const winrt::Windows::UI::Core::CoreWindow& window);
113  void SetWindowPos(winrt::Windows::Foundation::Rect rect);
114 #endif // TARGET_WINDOWS_STORE
115  bool IsNV12SharedTexturesSupported() const { return m_NV12SharedTexturesSupport; }
116  bool IsDXVA2SharedDecoderSurfaces() const { return m_DXVA2SharedDecoderSurfaces; }
117 
118  // Gets debug info from swapchain
119  DEBUG_INFO_RENDER GetDebugInfo() const;
120 
121  private:
122  class CBackBuffer : public CD3DTexture
123  {
124  public:
125  CBackBuffer() : CD3DTexture() {}
126  void SetViewIdx(unsigned idx) { m_viewIdx = idx; }
127  bool Acquire(ID3D11Texture2D* pTexture);
128  };
129 
130  HRESULT CreateSwapChain(DXGI_SWAP_CHAIN_DESC1 &desc, DXGI_SWAP_CHAIN_FULLSCREEN_DESC &fsDesc, IDXGISwapChain1 **ppSwapChain) const;
131  void DestroySwapChain();
132  void CreateDeviceIndependentResources();
133  void CreateDeviceResources();
134  void CreateWindowSizeDependentResources();
135  void UpdateRenderTargetSize();
136  void OnDeviceLost(bool removed);
137  void OnDeviceRestored();
138  void HandleOutputChange(const std::function<bool(DXGI_OUTPUT_DESC)>& cmpFunc);
139  bool CreateFactory();
140  void CheckNV12SharedTexturesSupport();
141  VideoDriverInfo GetVideoDriverVersion();
142  void CheckDXVA2SharedDecoderSurfaces();
143 
144  HWND m_window{ nullptr };
145 #if defined(TARGET_WINDOWS_STORE)
146  winrt::Windows::UI::Core::CoreWindow m_coreWindow = nullptr;
147 #endif
148  Microsoft::WRL::ComPtr<IDXGIFactory2> m_dxgiFactory;
149  Microsoft::WRL::ComPtr<IDXGIAdapter1> m_adapter;
150  Microsoft::WRL::ComPtr<IDXGIOutput1> m_output;
151 
152  Microsoft::WRL::ComPtr<ID3D11Device1> m_d3dDevice;
153  Microsoft::WRL::ComPtr<ID3D11DeviceContext1> m_d3dContext;
154  Microsoft::WRL::ComPtr<ID3D11DeviceContext1> m_deferrContext;
155  Microsoft::WRL::ComPtr<IDXGISwapChain1> m_swapChain;
156 #ifdef _DEBUG
157  Microsoft::WRL::ComPtr<ID3D11Debug> m_d3dDebug;
158 #endif
159 
160  CBackBuffer m_backBufferTex;
161  Microsoft::WRL::ComPtr<ID3D11DepthStencilView> m_d3dDepthStencilView;
162  D3D11_VIEWPORT m_screenViewport;
163 
164  // Cached device properties.
165  D3D_FEATURE_LEVEL m_d3dFeatureLevel;
166  winrt::Windows::Foundation::Size m_outputSize;
167  winrt::Windows::Foundation::Size m_logicalSize;
168  float m_dpi;
169 
170  // 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.
171  float m_effectiveDpi;
172  // The IDeviceNotify can be held directly as it owns the DeviceResources.
173  IDeviceNotify* m_deviceNotify;
174 
175  // scritical section
176  Concurrency::critical_section m_criticalSection;
177  Concurrency::critical_section m_resourceSection;
178  std::vector<ID3DResource*> m_resources;
179 
180  bool m_stereoEnabled;
181  bool m_bDeviceCreated;
182  bool m_IsHDROutput;
183  bool m_IsTransferPQ;
184  bool m_NV12SharedTexturesSupport{false};
185  bool m_DXVA2SharedDecoderSurfaces{false};
186  };
187 }
Definition: D3DResource.h:90
Definition: DeviceResources.h:27
Provide info of a resolution.
Definition: Resolution.h:66
Definition: DebugInfo.h:29
Definition: D3DResource.h:36
Definition: DeviceResources.h:36
Definition: WIN32Util.h:23