kodi
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 m_originalPrimaries = AVCOL_PRI_BT709;
80  AVColorPrimaries primaries = AVCOL_PRI_BT709;
81  AVColorSpace color_space = AVCOL_SPC_BT709;
82  AVColorTransferCharacteristic color_transfer = AVCOL_TRC_BT709;
83  bool full_range = false;
84  int bits = 8;
85  uint8_t texBits = 8;
86  AVPixelFormat pixelFormat = AV_PIX_FMT_NONE; // source pixel format
87  bool hasDisplayMetadata = false;
88  bool hasLightMetadata = false;
89  AVMasteringDisplayMetadata displayMetadata = {};
90  AVContentLightMetadata lightMetadata = {};
91  std::string stereoMode;
92  uint64_t frameIdx = 0;
93 
94 protected:
95  CRenderBuffer(AVPixelFormat av_pix_format, unsigned width, unsigned height);
96  void QueueCopyFromGPU();
97 
98  // video buffer size
99  unsigned int m_width;
100  unsigned int m_height;
101  // real texture size
102  unsigned int m_widthTex;
103  unsigned int m_heightTex;
104  // copy from GPU mem
105  Microsoft::WRL::ComPtr<ID3D11Texture2D> m_staging;
106  D3D11_TEXTURE2D_DESC m_sDesc{};
107  bool m_bPending = false;
108  bool m_bLoaded = false;
109 };
110 
112 {
113 public:
114  virtual ~CRendererBase();
115 
116  virtual CRenderInfo GetRenderInfo();
117  virtual bool Configure(const VideoPicture &picture, float fps, unsigned int orientation);
118  virtual bool Supports(ESCALINGMETHOD method) const = 0;
119  virtual bool Supports(ERENDERFEATURE feature) const;
120 
121  virtual bool WantsDoublePass() { return false; }
122  virtual bool NeedBuffer(int idx) { return false; }
123 
124  void AddVideoPicture(const VideoPicture &picture, int index);
125  void Render(int index, int index2, CD3DTexture& target, const CRect& sourceRect,
126  const CRect& destRect, const CRect& viewRect, unsigned flags);
127  void Render(CD3DTexture& target, const CRect& sourceRect, const CRect& destRect,
128  const CRect& viewRect, unsigned flags = 0);
129 
130  void ManageTextures();
131  int NextBuffer() const;
132  void ReleaseBuffer(int idx);
133  bool Flush(bool saveBuffers);
134  void SetBufferSize(int numBuffers) { m_iBuffersRequired = numBuffers; }
135 
136  DEBUG_INFO_VIDEO GetDebugInfo(int idx);
137 
138  static DXGI_FORMAT GetDXGIFormat(const VideoPicture &picture);
139  static DXGI_FORMAT GetDXGIFormat(CVideoBuffer* videoBuffer);
140  static AVPixelFormat GetAVFormat(DXGI_FORMAT dxgi_format);
141  static DXGI_HDR_METADATA_HDR10 GetDXGIHDR10MetaData(CRenderBuffer* rb);
142 
143 protected:
144  explicit CRendererBase(CVideoSettings& videoSettings);
145 
146  bool CreateIntermediateTarget(unsigned int width,
147  unsigned int height,
148  bool dynamic = false,
149  DXGI_FORMAT format = DXGI_FORMAT_UNKNOWN);
150  void OnCMSConfigChanged(AVColorPrimaries srcPrimaries);
151  void ReorderDrawPoints(const CRect& destRect, CPoint(&rotatedPoints)[4]) const;
152  bool CreateRenderBuffer(int index);
153  void DeleteRenderBuffer(int index);
154 
155  void ProcessHDR(CRenderBuffer* rb);
161  static bool IntendToRenderAsHDR(const VideoPicture& picture);
167  {
168  return m_HdrType == HDR_TYPE::HDR_HDR10 || m_HdrType == HDR_TYPE::HDR_HLG;
169  }
170 
171  virtual void RenderImpl(CD3DTexture& target, CRect& sourceRect, CPoint (&destPoints)[4], uint32_t flags) = 0;
172  virtual void FinalOutput(CD3DTexture& source, CD3DTexture& target, const CRect& sourceRect, const CPoint(&destPoints)[4]);
173 
174  virtual CRenderBuffer* CreateBuffer() = 0;
175  virtual void UpdateVideoFilters();
176  virtual void CheckVideoParameters();
177  virtual void OnViewSizeChanged() {}
178  virtual void OnOutputReset() {}
179  virtual std::string GetRenderMethodDebugInfo() const { return {}; }
180 
181  bool m_toneMapping = false;
182  bool m_useDithering = false;
183  bool m_cmsOn = false;
184  bool m_lutIsLoading = false;
185  bool m_useHLGtoPQ = false;
186  ETONEMAPMETHOD m_toneMapMethod = VS_TONEMAPMETHOD_OFF;
187 
188  int m_iBufferIndex = 0;
189  int m_iNumBuffers = 0;
190  int m_iBuffersRequired = 0;
191  int m_ditherDepth = 0;
192  int m_cmsToken = -1;
193  int m_lutSize = 0;
194  unsigned m_sourceWidth = 0;
195  unsigned m_sourceHeight = 0;
196  unsigned m_viewWidth = 0;
197  unsigned m_viewHeight = 0;
198  unsigned m_renderOrientation = 0;
199  float m_fps = 0.0f;
200  uint64_t m_frameIdx = 0;
201 
202  AVPixelFormat m_format = AV_PIX_FMT_NONE;
203  CD3DTexture m_IntermediateTarget;
204  std::shared_ptr<COutputShader> m_outputShader;
205  std::unique_ptr<CColorManager> m_colorManager;
206  Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> m_pLUTView;
207  CVideoSettings& m_videoSettings;
208  std::map<int, CRenderBuffer*> m_renderBuffers;
209 
210  DXGI_HDR_METADATA_HDR10 m_lastHdr10 = {};
211  HDR_TYPE m_HdrType = HDR_TYPE::HDR_INVALID;
212  bool m_AutoSwitchHDR = false;
213  bool m_initialHdrEnabled = false;
214  std::string m_renderMethodName;
215 };
Definition: D3DResource.h:90
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:166
Definition: DebugInfo.h:21
Definition: CharsetConverter.cpp:16
Definition: DVDVideoCodec.h:36
Definition: VideoSettings.h:194
Definition: RendererBase.h:111