2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
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.
23 // required by clients
24 #include "ServiceBroker.h"
25 #include "WinSystem.h"
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
33 /* what types are important for mode setting */
34 #define D3DPRESENTFLAG_MODEMASK ( D3DPRESENTFLAG_INTERLACED \
35 | D3DPRESENTFLAG_MODE3DSBS \
36 | D3DPRESENTFLAG_MODE3DTB )
38 enum VIEW_TYPE
{ VIEW_TYPE_NONE
= 0,
52 enum AdjustRefreshRate
54 ADJUST_REFRESHRATE_OFF
= 0,
55 ADJUST_REFRESHRATE_ALWAYS
,
56 ADJUST_REFRESHRATE_ON_STARTSTOP
,
57 ADJUST_REFRESHRATE_ON_START
,
62 RENDER_ORDER_ALL_BACK_TO_FRONT
= 0,
63 RENDER_ORDER_BACK_TO_FRONT
,
64 RENDER_ORDER_FRONT_TO_BACK
,
67 class CGraphicContext
: public CCriticalSection
70 CGraphicContext(void);
71 virtual ~CGraphicContext();
73 // methods related to windowing
75 void SetFPS(float fps
);
76 float GetDisplayLatency() const;
77 bool IsFullScreenRoot() const;
78 void ToggleFullScreen();
79 void SetFullScreenVideo(bool bOnOff
);
80 bool IsFullScreenVideo() const;
81 bool IsValidResolution(RESOLUTION res
);
82 void SetVideoResolution(RESOLUTION res
, bool forceUpdate
);
83 void ApplyModeChange(RESOLUTION res
);
84 void ApplyWindowResize(int newWidth
, int newHeight
);
85 RESOLUTION
GetVideoResolution() const;
86 const RESOLUTION_INFO
GetResInfo() const;
87 const RESOLUTION_INFO
GetResInfo(RESOLUTION res
) const;
88 void SetResInfo(RESOLUTION res
, const RESOLUTION_INFO
& info
);
90 void Flip(bool rendered
, bool videoLayer
);
92 // gfx context interface
94 int GetHeight() const;
95 bool SetViewPort(float fx
, float fy
, float fwidth
, float fheight
, bool intersectPrevious
= false);
96 void RestoreViewPort();
97 void SetScissors(const CRect
&rect
);
99 const CRect
&GetScissors() const;
100 const CRect
GetViewWindow() const;
101 void SetViewWindow(float left
, float top
, float right
, float bottom
);
102 bool IsCalibrating() const;
103 void SetCalibrating(bool bOnOff
);
104 void ResetOverscan(RESOLUTION res
, OVERSCAN
&overscan
);
105 void ResetOverscan(RESOLUTION_INFO
&resinfo
);
106 void ResetScreenParameters(RESOLUTION res
);
107 void CaptureStateBlock();
108 void ApplyStateBlock();
109 /*! \brief Invalidates color buffer, clears the depth buffer (if used).
110 Will result in undefined color buffer values which will have to be
111 repainted. Has to be called at the beginning of a frame.
114 /*! \brief Clears the depth buffer (if used) and the color buffer. Guaranties
115 a defined color buffer value. Has to be called at the beginning of a frame.
116 \param color the specified color.
118 void Clear(UTILS::COLOR::Color color
);
119 void GetAllowedResolutions(std::vector
<RESOLUTION
> &res
);
120 /*! \brief Sets the direction of the current rendering pass.
121 \param renderOrder direction of the pass
123 void SetRenderOrder(RENDER_ORDER renderOrder
);
124 /*! \brief Returns the current render order mode
125 \returns RENDER_ORDER returns the mode
127 RENDER_ORDER
GetRenderOrder() { return m_renderOrder
; }
128 /*! \brief Resets the z-depth. Layer 0 and 1 are reserved as presentation (video) layer.
130 void ResetDepth() { m_layer
= 2; }
131 /*! \brief Reserve layers for the caller to use
132 \param addLayers number of layers needed
133 \returns uint32_t returns the absolute layer hight
135 uint32_t GetDepth(uint32_t addLayers
= 2);
137 /* \brief Get UI scaling information from a given resolution to the screen resolution.
138 Takes account of overscan and UI zooming.
139 \param res the resolution to scale from.
140 \param scaleX [out] the scaling amount in the X direction.
141 \param scaleY [out] the scaling amount in the Y direction.
142 \param matrix [out] if non-NULL, a suitable transformation from res to screen resolution is set.
144 void GetGUIScaling(const RESOLUTION_INFO
&res
, float &scaleX
, float &scaleY
, TransformMatrix
*matrix
= NULL
);
145 void SetRenderingResolution(const RESOLUTION_INFO
&res
, bool needsScaling
); ///< Sets scaling up for rendering
146 void SetScalingResolution(const RESOLUTION_INFO
&res
, bool needsScaling
); ///< Sets scaling up for skin loading etc.
147 float GetScalingPixelRatio() const;
148 void InvertFinalCoords(float &x
, float &y
) const;
149 float ScaleFinalXCoord(float x
, float y
) const;
150 float ScaleFinalYCoord(float x
, float y
) const;
151 float ScaleFinalZCoord(float x
, float y
) const;
152 void ScaleFinalCoords(float &x
, float &y
, float &z
) const;
153 bool RectIsAngled(float x1
, float y1
, float x2
, float y2
) const;
154 const TransformMatrix
&GetGUIMatrix() const;
155 float GetGUIScaleX() const;
156 float GetGUIScaleY() const;
157 UTILS::COLOR::Color
MergeAlpha(UTILS::COLOR::Color color
) const;
158 UTILS::COLOR::Color
MergeColor(UTILS::COLOR::Color color
) const;
159 void SetOrigin(float x
, float y
);
160 void RestoreOrigin();
161 void SetCameraPosition(const CPoint
&camera
);
162 void SetStereoView(RENDER_STEREO_VIEW view
);
163 RENDER_STEREO_VIEW
GetStereoView() { return m_stereoView
; }
164 void SetStereoMode(RENDER_STEREO_MODE mode
) { m_nextStereoMode
= mode
; }
165 RENDER_STEREO_MODE
GetStereoMode() { return m_stereoMode
; }
166 void RestoreCameraPosition();
167 void SetStereoFactor(float factor
);
168 void RestoreStereoFactor();
169 /*! \brief Gets the depth information of the current transform matrix
170 \param depthOffset adds an offset to the current depth
171 \returns float normalized -1 to 1
173 float GetTransformDepth(int32_t depthOffset
= 0);
174 /*! \brief Gets the (normalized) depth information
175 \param depth to be normalized
176 \returns float normalized -1 to 1
178 float GetNormalizedDepth(uint32_t depth
);
179 /*! \brief Set a region in which to clip all rendering
180 Anything that is rendered after setting a clip region will be clipped so that no part renders
181 outside of the clip region. Successive calls to SetClipRegion intersect the clip region, which
182 means the clip region may eventually become an empty set. In this case SetClipRegion returns false
183 to indicate that no rendering need be performed.
185 This call must be matched with a RestoreClipRegion call unless SetClipRegion returns false.
187 Usage should be of the form:
189 if (SetClipRegion(x, y, w, h))
197 \param x the left-most coordinate of the clip region
198 \param y the top-most coordinate of the clip region
199 \param w the width of the clip region
200 \param h the height of the clip region
201 \returns true if the region is set and the result is non-empty. Returns false if the resulting region is empty.
202 \sa RestoreClipRegion
204 bool SetClipRegion(float x
, float y
, float w
, float h
);
205 void RestoreClipRegion();
206 void ClipRect(CRect
&vertex
, CRect
&texture
, CRect
*diffuse
= NULL
);
207 CRect
GetClipRegion();
208 void AddGUITransform();
209 TransformMatrix
AddTransform(const TransformMatrix
&matrix
);
210 void SetTransform(const TransformMatrix
&matrix
);
211 void SetTransform(const TransformMatrix
&matrix
, float scaleX
, float scaleY
);
212 void RemoveTransform();
214 /* modifies final coordinates according to stereo mode if needed */
215 CRect
StereoCorrection(const CRect
&rect
) const;
216 CPoint
StereoCorrection(const CPoint
&point
) const;
218 CRect
GenerateAABB(const CRect
&rect
) const;
220 //@todo move those somewhere else
221 const std::string
& GetMediaDir() const;
222 void SetMediaDir(const std::string
& strMediaDir
);
224 void SetTransferPQ(bool PQ
) { m_isTransferPQ
= PQ
; }
225 bool IsTransferPQ() const { return m_isTransferPQ
; }
229 void UpdateCameraPosition(const CPoint
&camera
, const float &factor
);
230 void SetVideoResolutionInternal(RESOLUTION res
, bool forceUpdate
);
231 void ApplyVideoResolution(RESOLUTION res
);
232 void UpdateInternalStateWithResolution(RESOLUTION res
);
234 int m_iScreenHeight
= 576;
235 int m_iScreenWidth
= 720;
236 std::string m_strMediaDir
;
238 bool m_bFullScreenRoot
= true;
239 bool m_bFullScreenVideo
= false;
240 bool m_bCalibrating
= false;
241 RESOLUTION m_Resolution
= RES_INVALID
;
242 float m_fFPSOverride
= 0.0f
;
244 RESOLUTION_INFO m_windowResolution
;
245 std::stack
<CPoint
> m_cameras
;
246 std::stack
<CPoint
> m_origins
;
247 std::stack
<CRect
> m_clipRegions
;
248 std::stack
<float> m_stereoFactors
;
249 std::stack
<CRect
> m_viewStack
;
255 UITransform() : matrix() {}
256 UITransform(const TransformMatrix
& m
, const float sX
= 1.0f
, const float sY
= 1.0f
)
257 : matrix(m
), scaleX(sX
), scaleY(sY
)
263 scaleX
= scaleY
= 1.0f
;
266 TransformMatrix matrix
;
271 UITransform m_guiTransform
;
272 UITransform m_finalTransform
;
273 std::stack
<UITransform
> m_transforms
;
274 RENDER_STEREO_VIEW m_stereoView
= RENDER_STEREO_VIEW_OFF
;
275 RENDER_STEREO_MODE m_stereoMode
= RENDER_STEREO_MODE_OFF
;
276 RENDER_STEREO_MODE m_nextStereoMode
= RENDER_STEREO_MODE_OFF
;
278 bool m_isTransferPQ
{false};
279 RENDER_ORDER m_renderOrder
{RENDER_ORDER_ALL_BACK_TO_FRONT
};