xbmc
RendererBase.h
1 /*
2  * Copyright (C) 2017-2019 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 #pragma once
9 
10 #include "VideoRenderers/ColorManager.h"
11 #include "VideoRenderers/DebugInfo.h"
12 #include "VideoRenderers/RenderInfo.h"
13 #include "VideoRenderers/VideoShaders/WinVideoFilter.h"
14 #include "cores/VideoSettings.h"
15 #include "guilib/D3DResource.h"
16 
17 #include <vector>
18 
19 #include <d3d11_4.h>
20 #include <dxgi1_5.h>
21 extern "C" {
22 #include <libavutil/mastering_display_metadata.h>
23 #include <libavutil/pixdesc.h>
24 }
25 
26 struct VideoPicture;
27 class CVideoBuffer;
28 
29 namespace win
30 {
31  namespace helpers
32  {
33  template<typename T>
34  bool contains(std::vector<T> vector, T item)
35  {
36  return find(vector.begin(), vector.end(), item) != vector.end();
37  }
38  }
39 }
40 
41 enum RenderMethod
42 {
43  RENDER_INVALID = 0,
44  RENDER_DXVA = 1,
45  RENDER_PS = 2,
46  RENDER_SW = 3
47 };
48 
49 enum class HDR_TYPE
50 {
51  HDR_INVALID = -1,
52  HDR_NONE_SDR = 0,
53  HDR_HDR10 = 1,
54  HDR_HLG = 2
55 };
56 
58 {
59 public:
60  virtual ~CRenderBuffer() = default;
61 
62  unsigned GetWidth() const { return m_widthTex; }
63  unsigned GetHeight() const { return m_heightTex; }
64  bool IsLoaded() { return m_bLoaded; }
65 
66  virtual void AppendPicture(const VideoPicture& picture);
67  virtual void ReleasePicture();
68  virtual bool UploadBuffer() { return false; }
69  virtual HRESULT GetResource(ID3D11Resource** ppResource, unsigned* index) const;
70 
71  // implementation specified
72  virtual bool GetDataPlanes(uint8_t*(&planes)[3], int(&strides)[3]) { return false; }
73  virtual unsigned GetViewCount() const { return 0; }
74  virtual ID3D11View* GetView(unsigned viewIdx) { return nullptr; }
75 
76  AVPixelFormat av_format;
77  CVideoBuffer* videoBuffer = nullptr;
78  unsigned int pictureFlags = 0;
79  AVColorPrimaries primaries = AVCOL_PRI_BT709;
80  AVColorSpace color_space = AVCOL_SPC_BT709;
81  AVColorTransferCharacteristic color_transfer = AVCOL_TRC_BT709;
82  bool full_range = false;
83  int bits = 8;
84  uint8_t texBits = 8;
85  AVPixelFormat pixelFormat = AV_PIX_FMT_NONE; // source pixel format
86  bool hasDisplayMetadata = false;
87  bool hasLightMetadata = false;
88  AVMasteringDisplayMetadata displayMetadata = {};
89  AVContentLightMetadata lightMetadata = {};
90  std::string stereoMode;
91  uint64_t frameIdx = 0;
92 
93 protected:
94  CRenderBuffer(AVPixelFormat av_pix_format, unsigned width, unsigned height);
95  void QueueCopyFromGPU();
96 
97  // video buffer size
98  unsigned int m_width;
99  unsigned int m_height;
100  // real texture size
101  unsigned int m_widthTex;
102  unsigned int m_heightTex;
103  // copy from GPU mem
104  Microsoft::WRL::ComPtr<ID3D11Texture2D> m_staging;
105  D3D11_TEXTURE2D_DESC m_sDesc{};
106  bool m_bPending = false;
107  bool m_bLoaded = false;
108 };
109 
111 {
112 public:
113  virtual ~CRendererBase();
114 
115  virtual CRenderInfo GetRenderInfo();
116  virtual bool Configure(const VideoPicture &picture, float fps, unsigned int orientation);
117  virtual bool Supports(ESCALINGMETHOD method) const = 0;
118  virtual bool WantsDoublePass() { return false; }
119  virtual bool NeedBuffer(int idx) { return false; }
120 
121  void AddVideoPicture(const VideoPicture &picture, int index);
122  void Render(int index, int index2, CD3DTexture& target, const CRect& sourceRect,
123  const CRect& destRect, const CRect& viewRect, unsigned flags);
124  void Render(CD3DTexture& target, const CRect& sourceRect, const CRect& destRect,
125  const CRect& viewRect, unsigned flags = 0);
126 
127  void ManageTextures();
128  int NextBuffer() const;
129  void ReleaseBuffer(int idx);
130  bool Flush(bool saveBuffers);
131  void SetBufferSize(int numBuffers) { m_iBuffersRequired = numBuffers; }
132 
133  DEBUG_INFO_VIDEO GetDebugInfo(int idx);
134 
135  static DXGI_FORMAT GetDXGIFormat(const VideoPicture &picture);
136  static DXGI_FORMAT GetDXGIFormat(CVideoBuffer* videoBuffer);
137  static AVPixelFormat GetAVFormat(DXGI_FORMAT dxgi_format);
138  static DXGI_HDR_METADATA_HDR10 GetDXGIHDR10MetaData(CRenderBuffer* rb);
139 
140 protected:
141  explicit CRendererBase(CVideoSettings& videoSettings);
142 
143  bool CreateIntermediateTarget(unsigned int width,
144  unsigned int height,
145  bool dynamic = false,
146  DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN);
147  void OnCMSConfigChanged(AVColorPrimaries srcPrimaries);
148  void ReorderDrawPoints(const CRect& destRect, CPoint(&rotatedPoints)[4]) const;
149  bool CreateRenderBuffer(int index);
150  void DeleteRenderBuffer(int index);
151 
152  void ProcessHDR(CRenderBuffer* rb);
158  static bool IntendToRenderAsHDR(const VideoPicture& picture);
164  {
165  return m_HdrType == HDR_TYPE::HDR_HDR10 || m_HdrType == HDR_TYPE::HDR_HLG;
166  }
167 
168  virtual void RenderImpl(CD3DTexture& target, CRect& sourceRect, CPoint (&destPoints)[4], uint32_t flags) = 0;
169  virtual void FinalOutput(CD3DTexture& source, CD3DTexture& target, const CRect& sourceRect, const CPoint(&destPoints)[4]);
170 
171  virtual CRenderBuffer* CreateBuffer() = 0;
172  virtual void UpdateVideoFilters();
173  virtual void CheckVideoParameters();
174  virtual void OnViewSizeChanged() {}
175  virtual void OnOutputReset() {}
176  virtual std::string GetRenderMethodDebugInfo() const { return {}; }
177 
178  bool m_toneMapping = false;
179  bool m_useDithering = false;
180  bool m_cmsOn = false;
181  bool m_lutIsLoading = false;
182  bool m_useHLGtoPQ = false;
183  ETONEMAPMETHOD m_toneMapMethod = VS_TONEMAPMETHOD_OFF;
184 
185  int m_iBufferIndex = 0;
186  int m_iNumBuffers = 0;
187  int m_iBuffersRequired = 0;
188  int m_ditherDepth = 0;
189  int m_cmsToken = -1;
190  int m_lutSize = 0;
191  unsigned m_sourceWidth = 0;
192  unsigned m_sourceHeight = 0;
193  unsigned m_viewWidth = 0;
194  unsigned m_viewHeight = 0;
195  unsigned m_renderOrientation = 0;
196  float m_fps = 0.0f;
197  uint64_t m_frameIdx = 0;
198 
199  AVPixelFormat m_format = AV_PIX_FMT_NONE;
200  CD3DTexture m_IntermediateTarget;
201  std::shared_ptr<COutputShader> m_outputShader;
202  std::unique_ptr<CColorManager> m_colorManager;
203  Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_pLUTView;
204  CVideoSettings& m_videoSettings;
205  std::map<int, CRenderBuffer*> m_renderBuffers;
206 
207  DXGI_HDR_METADATA_HDR10 m_lastHdr10 = {};
208  HDR_TYPE m_HdrType = HDR_TYPE::HDR_INVALID;
209  bool m_AutoSwitchHDR = false;
210  bool m_initialHdrEnabled = false;
211  std::string m_renderMethodName;
212 };
Definition: D3DResource.h:92
Definition: VideoBuffer.h:85
Definition: RendererBase.h:57
Definition: RenderInfo.h:19
bool ActualRenderAsHDR()
Call after rendering has started to find out if the output is configured as SDR or HDR...
Definition: RendererBase.h:163
Definition: DebugInfo.h:21
Definition: CharsetConverter.cpp:16
Definition: DVDVideoCodec.h:36
Definition: VideoSettings.h:194
Definition: RendererBase.h:110