kodi
OverlayRenderer.h
1 /*
2  * Initial code sponsored by: Voddler Inc (voddler.com)
3  * Copyright (C) 2005-2018 Team Kodi
4  * This file is part of Kodi - https://kodi.tv
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  * See LICENSES/README.md for more information.
8  */
9 
10 #pragma once
11 
12 #include "BaseRenderer.h"
13 #include "cores/VideoPlayer/DVDCodecs/Overlay/DVDOverlay.h"
14 #include "cores/VideoPlayer/DVDSubtitles/SubtitlesStyle.h"
15 #include "settings/SubtitlesSettings.h"
16 #include "threads/CriticalSection.h"
17 #include "utils/Observer.h"
18 
19 #include <atomic>
20 #include <map>
21 #include <memory>
22 #include <vector>
23 
24 typedef struct ass_image ASS_Image;
25 
26 class CDVDOverlay;
27 class CDVDOverlayLibass;
28 class CDVDOverlayImage;
29 class CDVDOverlaySpu;
30 class CDVDOverlaySSA;
31 class CDVDOverlayText;
32 
33 namespace OVERLAY {
34 
35  struct SRenderState
36  {
37  float x;
38  float y;
39  float width;
40  float height;
41  };
42 
43  class COverlay
44  {
45  public:
46  static std::shared_ptr<COverlay> Create(const CDVDOverlayImage& o, CRect& rSource);
47  static std::shared_ptr<COverlay> Create(const CDVDOverlaySpu& o);
48  static std::shared_ptr<COverlay> Create(ASS_Image* images, float width, float height);
49 
50  COverlay();
51  virtual ~COverlay();
52 
53  virtual void Render(SRenderState& state) = 0;
54 
55  enum EType
56  {
57  TYPE_NONE,
58  TYPE_TEXTURE
59  } m_type;
60 
61  enum EAlign
62  {
63  ALIGN_SCREEN,
64  ALIGN_SCREEN_AR,
65  ALIGN_VIDEO,
66  ALIGN_SUBTITLE
67  } m_align;
68 
69  enum EPosition
70  {
71  POSITION_ABSOLUTE,
72  POSITION_ABSOLUTE_SCREEN,
73  POSITION_RELATIVE
74  } m_pos;
75 
76  float m_x{0};
77  float m_y{0};
78  float m_width{1.0f};
79  float m_height{1.0f};
80  float m_source_width{0}; // Video source width resolution used to calculate aspect ratio
81  float m_source_height{0}; // Video source height resolution used to calculate aspect ratio
82 
83  protected:
89  bool IsSquareResolution(float resRatio) { return resRatio > 1.22f && resRatio < 1.34f; }
90  };
91 
92  class CRenderer : public Observer
93  {
94  public:
95  CRenderer();
96  virtual ~CRenderer();
97 
98  // Implementation of Observer
99  void Notify(const Observable& obs, const ObservableMessage msg) override;
100 
101  void AddOverlay(std::shared_ptr<CDVDOverlay> o, double pts, int index);
102  virtual void Render(int idx);
103 
107  void UnInit();
108 
109  void Flush();
110 
114  void Reset();
115 
116  void Release(int idx);
117  bool HasOverlay(int idx);
118  void SetVideoRect(CRect &source, CRect &dest, CRect &view);
119  void SetStereoMode(const std::string &stereomode);
120 
127  void SetSubtitleVerticalPosition(const int value, bool save);
128 
129  protected:
133  void ResetSubtitlePosition();
134 
138  void OnViewChange();
139 
140  struct SElement
141  {
142  SElement() : overlay_dvd(NULL) { pts = 0.0; }
143  double pts;
144  std::shared_ptr<CDVDOverlay> overlay_dvd;
145  };
146 
147  void Render(COverlay* o);
148  std::shared_ptr<COverlay> Convert(CDVDOverlay& o, double pts);
156  std::shared_ptr<COverlay> ConvertLibass(
158  double pts,
159  bool updateStyle,
160  const std::shared_ptr<struct KODI::SUBTITLES::STYLE::style>& overlayStyle);
161 
162  void CreateSubtitlesStyle();
163 
164  void Release(std::vector<SElement>& list);
165  void ReleaseCache();
166  void ReleaseUnused();
167 
171  void LoadSettings();
172 
173  enum PositonResInfoState
174  {
175  POSRESINFO_UNSET = -1,
176  POSRESINFO_SAVE_CHANGES = -2,
177  };
178 
179  CCriticalSection m_section;
180  std::vector<SElement> m_buffers[NUM_BUFFERS];
181  std::map<unsigned int, std::shared_ptr<COverlay>> m_textureCache;
182  static unsigned int m_textureid;
183  CRect m_rv; // Frame size
184  CRect m_rs; // Source size
185  CRect m_rd; // Video size, may be influenced by video settings (e.g. zoom)
186  std::string m_stereomode;
187  // Current subtitle position
188  int m_subtitlePosition{0};
189  // Current subtitle position from resolution info,
190  // or PositonResInfoState enum values for deferred processing
191  int m_subtitlePosResInfo{POSRESINFO_UNSET};
192  int m_subtitleVerticalMargin{0};
193  bool m_saveSubtitlePosition{false}; // To save subtitle position permanently
194  KODI::SUBTITLES::HorizontalAlign m_subtitleHorizontalAlign{
195  KODI::SUBTITLES::HorizontalAlign::CENTER};
196  KODI::SUBTITLES::Align m_subtitleAlign{KODI::SUBTITLES::Align::BOTTOM_OUTSIDE};
197 
198  std::shared_ptr<struct KODI::SUBTITLES::STYLE::style> m_overlayStyle;
199  std::atomic<bool> m_isSettingsChanged{false};
200  };
201 }
Definition: DVDOverlayText.h:16
Definition: DVDOverlaySSA.h:16
Definition: OverlayRenderer.h:43
Definition: OverlayRenderer.h:92
Definition: OverlayRenderer.h:33
Definition: DVDOverlayImage.h:18
bool IsSquareResolution(float resRatio)
Given the resolution ratio determines if it is a 4/3 resolution.
Definition: OverlayRenderer.h:89
Definition: DVDOverlayLibass.h:16
Definition: Observer.h:31
Definition: OverlayRenderer.h:35
Definition: OverlayRenderer.h:140
Definition: DVDOverlaySpu.h:16
Definition: Observer.h:44
Definition: DVDOverlay.h:27