kodi
GraphicContext.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 "Resolution.h"
12 #include "rendering/RenderSystem.h"
13 #include "threads/CriticalSection.h"
14 #include "utils/ColorUtils.h"
15 #include "utils/Geometry.h" // for CRect/CPoint
16 #include "utils/TransformMatrix.h" // for the members m_guiTransform etc.
17 
18 #include <map>
19 #include <stack>
20 #include <string>
21 #include <vector>
22 
23 // required by clients
24 #include "ServiceBroker.h"
25 #include "WinSystem.h"
26 
27 #define D3DPRESENTFLAG_INTERLACED 1
28 #define D3DPRESENTFLAG_WIDESCREEN 2
29 #define D3DPRESENTFLAG_PROGRESSIVE 4
30 #define D3DPRESENTFLAG_MODE3DSBS 8
31 #define D3DPRESENTFLAG_MODE3DTB 16
32 
33 /* what types are important for mode setting */
34 #define D3DPRESENTFLAG_MODEMASK ( D3DPRESENTFLAG_INTERLACED \
35  | D3DPRESENTFLAG_MODE3DSBS \
36  | D3DPRESENTFLAG_MODE3DTB )
37 
38 enum VIEW_TYPE { VIEW_TYPE_NONE = 0,
39  VIEW_TYPE_LIST,
40  VIEW_TYPE_ICON,
41  VIEW_TYPE_BIG_LIST,
42  VIEW_TYPE_BIG_ICON,
43  VIEW_TYPE_WIDE,
44  VIEW_TYPE_BIG_WIDE,
45  VIEW_TYPE_WRAP,
46  VIEW_TYPE_BIG_WRAP,
47  VIEW_TYPE_INFO,
48  VIEW_TYPE_BIG_INFO,
49  VIEW_TYPE_AUTO,
50  VIEW_TYPE_MAX };
51 
52 enum AdjustRefreshRate
53 {
54  ADJUST_REFRESHRATE_OFF = 0,
55  ADJUST_REFRESHRATE_ALWAYS,
56  ADJUST_REFRESHRATE_ON_STARTSTOP,
57  ADJUST_REFRESHRATE_ON_START,
58 };
59 
60 class CGraphicContext : public CCriticalSection
61 {
62 public:
63  CGraphicContext(void);
64  virtual ~CGraphicContext();
65 
66  // methods related to windowing
67  float GetFPS() const;
68  void SetFPS(float fps);
69  float GetDisplayLatency() const;
70  bool IsFullScreenRoot() const;
71  void ToggleFullScreen();
72  void SetFullScreenVideo(bool bOnOff);
73  bool IsFullScreenVideo() const;
74  bool IsValidResolution(RESOLUTION res);
75  void SetVideoResolution(RESOLUTION res, bool forceUpdate);
76  void ApplyModeChange(RESOLUTION res);
77  void ApplyWindowResize(int newWidth, int newHeight);
78  RESOLUTION GetVideoResolution() const;
79  const RESOLUTION_INFO GetResInfo() const;
80  const RESOLUTION_INFO GetResInfo(RESOLUTION res) const;
81  void SetResInfo(RESOLUTION res, const RESOLUTION_INFO& info);
82 
83  void Flip(bool rendered, bool videoLayer);
84 
85  // gfx context interface
86  int GetWidth() const;
87  int GetHeight() const;
88  bool SetViewPort(float fx, float fy , float fwidth, float fheight, bool intersectPrevious = false);
89  void RestoreViewPort();
90  void SetScissors(const CRect &rect);
91  void ResetScissors();
92  const CRect &GetScissors() const;
93  const CRect GetViewWindow() const;
94  void SetViewWindow(float left, float top, float right, float bottom);
95  bool IsCalibrating() const;
96  void SetCalibrating(bool bOnOff);
97  void ResetOverscan(RESOLUTION res, OVERSCAN &overscan);
98  void ResetOverscan(RESOLUTION_INFO &resinfo);
99  void ResetScreenParameters(RESOLUTION res);
100  void CaptureStateBlock();
101  void ApplyStateBlock();
102  void Clear(UTILS::COLOR::Color color = 0);
103  void GetAllowedResolutions(std::vector<RESOLUTION> &res);
104 
105  /* \brief Get UI scaling information from a given resolution to the screen resolution.
106  Takes account of overscan and UI zooming.
107  \param res the resolution to scale from.
108  \param scaleX [out] the scaling amount in the X direction.
109  \param scaleY [out] the scaling amount in the Y direction.
110  \param matrix [out] if non-NULL, a suitable transformation from res to screen resolution is set.
111  */
112  void GetGUIScaling(const RESOLUTION_INFO &res, float &scaleX, float &scaleY, TransformMatrix *matrix = NULL);
113  void SetRenderingResolution(const RESOLUTION_INFO &res, bool needsScaling);
114  void SetScalingResolution(const RESOLUTION_INFO &res, bool needsScaling);
115  float GetScalingPixelRatio() const;
116  void InvertFinalCoords(float &x, float &y) const;
117  float ScaleFinalXCoord(float x, float y) const;
118  float ScaleFinalYCoord(float x, float y) const;
119  float ScaleFinalZCoord(float x, float y) const;
120  void ScaleFinalCoords(float &x, float &y, float &z) const;
121  bool RectIsAngled(float x1, float y1, float x2, float y2) const;
122  const TransformMatrix &GetGUIMatrix() const;
123  float GetGUIScaleX() const;
124  float GetGUIScaleY() const;
125  UTILS::COLOR::Color MergeAlpha(UTILS::COLOR::Color color) const;
126  UTILS::COLOR::Color MergeColor(UTILS::COLOR::Color color) const;
127  void SetOrigin(float x, float y);
128  void RestoreOrigin();
129  void SetCameraPosition(const CPoint &camera);
130  void SetStereoView(RENDER_STEREO_VIEW view);
131  RENDER_STEREO_VIEW GetStereoView() { return m_stereoView; }
132  void SetStereoMode(RENDER_STEREO_MODE mode) { m_nextStereoMode = mode; }
133  RENDER_STEREO_MODE GetStereoMode() { return m_stereoMode; }
134  void RestoreCameraPosition();
135  void SetStereoFactor(float factor);
136  void RestoreStereoFactor();
162  bool SetClipRegion(float x, float y, float w, float h);
163  void RestoreClipRegion();
164  void ClipRect(CRect &vertex, CRect &texture, CRect *diffuse = NULL);
165  CRect GetClipRegion();
166  void AddGUITransform();
167  TransformMatrix AddTransform(const TransformMatrix &matrix);
168  void SetTransform(const TransformMatrix &matrix);
169  void SetTransform(const TransformMatrix &matrix, float scaleX, float scaleY);
170  void RemoveTransform();
171 
172  /* modifies final coordinates according to stereo mode if needed */
173  CRect StereoCorrection(const CRect &rect) const;
174  CPoint StereoCorrection(const CPoint &point) const;
175 
176  CRect GenerateAABB(const CRect &rect) const;
177 
178  //@todo move those somewhere else
179  const std::string& GetMediaDir() const;
180  void SetMediaDir(const std::string& strMediaDir);
181 
182  void SetTransferPQ(bool PQ) { m_isTransferPQ = PQ; }
183  bool IsTransferPQ() const { return m_isTransferPQ; }
184 
185 protected:
186 
187  void UpdateCameraPosition(const CPoint &camera, const float &factor);
188  void SetVideoResolutionInternal(RESOLUTION res, bool forceUpdate);
189  void ApplyVideoResolution(RESOLUTION res);
190  void UpdateInternalStateWithResolution(RESOLUTION res);
191 
192  int m_iScreenHeight = 576;
193  int m_iScreenWidth = 720;
194  std::string m_strMediaDir;
195  CRect m_videoRect;
196  bool m_bFullScreenRoot = true;
197  bool m_bFullScreenVideo = false;
198  bool m_bCalibrating = false;
199  RESOLUTION m_Resolution = RES_INVALID;
200  float m_fFPSOverride = 0.0f;
201 
202  RESOLUTION_INFO m_windowResolution;
203  std::stack<CPoint> m_cameras;
204  std::stack<CPoint> m_origins;
205  std::stack<CRect> m_clipRegions;
206  std::stack<float> m_stereoFactors;
207  std::stack<CRect> m_viewStack;
208  CRect m_scissors;
209 
211  {
212  public:
213  UITransform() : matrix() {}
214  UITransform(const TransformMatrix& m, const float sX = 1.0f, const float sY = 1.0f)
215  : matrix(m), scaleX(sX), scaleY(sY)
216  {
217  }
218  void Reset()
219  {
220  matrix.Reset();
221  scaleX = scaleY = 1.0f;
222  }
223 
224  TransformMatrix matrix;
225  float scaleX = 1.0f;
226  float scaleY = 1.0f;
227  };
228 
229  UITransform m_guiTransform;
230  UITransform m_finalTransform;
231  std::stack<UITransform> m_transforms;
232  RENDER_STEREO_VIEW m_stereoView = RENDER_STEREO_VIEW_OFF;
233  RENDER_STEREO_MODE m_stereoMode = RENDER_STEREO_MODE_OFF;
234  RENDER_STEREO_MODE m_nextStereoMode = RENDER_STEREO_MODE_OFF;
235 
236  bool m_isTransferPQ{false};
237 };
void SetRenderingResolution(const RESOLUTION_INFO &res, bool needsScaling)
Sets scaling up for rendering.
Definition: GraphicContext.cpp:733
Provide info of a resolution.
Definition: Resolution.h:66
Definition: TransformMatrix.h:25
Definition: Resolution.h:26
void SetScalingResolution(const RESOLUTION_INFO &res, bool needsScaling)
Sets scaling up for skin loading etc.
Definition: GraphicContext.cpp:708
Definition: GraphicContext.h:210
bool SetClipRegion(float x, float y, float w, float h)
Set a region in which to clip all rendering Anything that is rendered after setting a clip region wil...
Definition: GraphicContext.cpp:53
Definition: GraphicContext.h:60