Debian Codegraph @

Source/Core/VideoCommon/Present.h

raw symbols SCIP
1 // Copyright 2023 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include "Common/Flag.h"
7 #include "Common/MathUtil.h"
8
9 #include "VideoCommon/OnScreenUIKeyMap.h"
10 #include "VideoCommon/TextureCacheBase.h"
11 #include "VideoCommon/TextureConfig.h"
12 #include "VideoCommon/VideoCommon.h"
13
14 #include <array>
15 #include <memory>
16 #include <mutex>
17 #include <tuple>
18
19 class AbstractTexture;
20 struct SurfaceInfo;
21 enum class DolphinKey;
22
23 namespace VideoCommon
24 {
25 class OnScreenUI;
26 class PostProcessing;
27
28 // Presenter is a class that deals with putting the final XFB on the screen.
29 // It also handles the ImGui UI and post-processing.
30 class Presenter
31 {
32 public:
33 using ClearColor = std::array<float, 4>;
34
35 Presenter();
36 virtual ~Presenter();
37
38 void ViSwap(u32 xfb_addr, u32 fb_width, u32 fb_stride, u32 fb_height, u64 ticks,
39 TimePoint presentation_time);
40 void ImmediateSwap(u32 xfb_addr, u32 fb_width, u32 fb_stride, u32 fb_height);
41
42 void SetNextSwapEstimatedTime(u64 ticks, TimePoint host_time);
43
44 void Present(PresentInfo* present_info = nullptr);
45 void ClearLastXfbId() { m_last_xfb_id = std::numeric_limits<u64>::max(); }
46
47 bool Initialize();
48
49 void ConfigChanged(u32 changed_bits);
50
51 // Window resolution (display resolution if fullscreen)
52 int GetBackbufferWidth() const { return m_backbuffer_width; }
53 int GetBackbufferHeight() const { return m_backbuffer_height; }
54 float GetBackbufferScale() const { return m_backbuffer_scale; }
55 u32 AutoIntegralScale() const;
56 AbstractTextureFormat GetBackbufferFormat() const { return m_backbuffer_format; }
57 void SetSuggestedWindowSize(int width, int height);
58 void SetBackbuffer(int backbuffer_width, int backbuffer_height);
59 void SetBackbuffer(SurfaceInfo info);
60 void OnBackbufferSet(bool size_changed, bool is_first_set);
61
62 void UpdateDrawRectangle();
63
64 // Get the amount of pixels the given rect should be cropped on all sides from custom cropping.
65 MathUtil::Rectangle<int> GetCustomCrop(const MathUtil::Rectangle<int>& rect) const;
66
67 // Crop the given rectangle by the custom cropping.
68 MathUtil::Rectangle<int> AdjustForCustomCrop(const MathUtil::Rectangle<int>& rect) const;
69
70 // Modify an aspect ratio by the aspect ratio change that custom cropping will apply.
71 float AdjustAspectRatioForCustomCrop(float input_aspect_ratio) const;
72
73 // Returns the target aspect ratio the XFB output should be drawn with.
74 float CalculateDrawAspectRatio(bool allow_stretch = true) const;
75
76 // Crops the target rectangle to the framebuffer dimensions, reducing the size of the source
77 // rectangle if it is greater. Works even if the source and target rectangles don't have a
78 // 1:1 pixel mapping, scaling as appropriate.
79 void AdjustRectanglesToFitBounds(MathUtil::Rectangle<int>* target_rect,
80 MathUtil::Rectangle<int>* source_rect, int fb_width,
81 int fb_height);
82
83 void ReleaseXFBContentLock();
84
85 // Draws the specified XFB buffer to the screen, performing any post-processing.
86 // Assumes that the backbuffer has already been bound and cleared.
87 virtual void RenderXFBToScreen(const MathUtil::Rectangle<int>& target_rc,
88 const AbstractTexture* source_texture,
89 const MathUtil::Rectangle<int>& source_rc);
90
91 VideoCommon::PostProcessing* GetPostProcessor() const { return m_post_processor.get(); }
92 // Final surface changing
93 // This is called when the surface is resized (WX) or the window changes (Android).
94 void ChangeSurface(void* new_surface_handle);
95 void ResizeSurface();
96 bool SurfaceResizedTestAndClear() { return m_surface_resized.TestAndClear(); }
97 bool SurfaceChangedTestAndClear() { return m_surface_changed.TestAndClear(); }
98 void* GetNewSurfaceHandle();
99
100 void SetKeyMap(const DolphinKeyMap& key_map);
101
102 void SetKey(u32 key, bool is_down, const char* chars);
103 void SetMousePos(float x, float y);
104 void SetMousePress(u32 button_mask);
105
106 int FrameCount() const { return m_frame_count; }
107
108 void DoState(PointerWrap& p);
109
110 const MathUtil::Rectangle<int>& GetTargetRectangle() const { return m_target_rectangle; }
111
112 private:
113 // Fetches the XFB texture from the texture cache.
114 // Returns true the contents have changed since last time
115 bool FetchXFB(u32 xfb_addr, u32 fb_width, u32 fb_stride, u32 fb_height, u64 ticks);
116
117 void ProcessFrameDumping(u64 ticks) const;
118
119 void OnBackBufferSizeChanged();
120
121 // Scales a raw XFB resolution to the target (display) aspect ratio,
122 // also accounting for crop and other minor adjustments
123 std::tuple<int, int> CalculateOutputDimensions(int width, int height,
124 bool allow_stretch = true) const;
125 std::tuple<float, float> ApplyStandardAspectCrop(float width, float height,
126 bool allow_stretch = true) const;
127 // Scales a raw XFB resolution to the target (display) aspect ratio
128 std::tuple<float, float> ScaleToDisplayAspectRatio(int width, int height,
129 bool allow_stretch = true) const;
130
131 // Use this to convert a single target rectangle to two stereo rectangles
132 std::tuple<MathUtil::Rectangle<int>, MathUtil::Rectangle<int>>
133 ConvertStereoRectangle(const MathUtil::Rectangle<int>& rc) const;
134
135 std::mutex m_swap_mutex;
136
137 // Backbuffer (window) size and render area
138 int m_backbuffer_width = 0;
139 int m_backbuffer_height = 0;
140 float m_backbuffer_scale = 1.0f;
141 AbstractTextureFormat m_backbuffer_format = AbstractTextureFormat::Undefined;
142
143 void* m_new_surface_handle = nullptr;
144 Common::Flag m_surface_changed;
145 Common::Flag m_surface_resized;
146
147 // The presentation rectangle.
148 // Width and height correspond to the final output resolution.
149 // Offsets imply black borders (if the window aspect ratio doesn't match the game's one).
150 MathUtil::Rectangle<int> m_target_rectangle = {};
151
152 u32 m_auto_resolution_scale = 1;
153
154 RcTcacheEntry m_xfb_entry;
155 // Internal resolution multiplier scaled XFB size
156 MathUtil::Rectangle<int> m_xfb_rect{0, 0, MAX_XFB_WIDTH, MAX_XFB_HEIGHT};
157
158 // Tracking of XFB textures so we don't render duplicate frames.
159 u64 m_last_xfb_id = std::numeric_limits<u64>::max();
160
161 // These will be set on the first call to SetSuggestedWindowSize.
162 int m_last_window_request_width = 0;
163 int m_last_window_request_height = 0;
164
165 std::unique_ptr<VideoCommon::PostProcessing> m_post_processor;
166 std::unique_ptr<VideoCommon::OnScreenUI> m_onscreen_ui;
167
168 u64 m_frame_count = 0;
169 u64 m_present_count = 0;
170
171 // XFB tracking
172 u64 m_last_xfb_ticks = 0;
173 u32 m_last_xfb_addr = 0;
174 // Native XFB width
175 u32 m_last_xfb_width = MAX_XFB_WIDTH;
176 u32 m_last_xfb_stride = 0;
177 // Native XFB height
178 u32 m_last_xfb_height = MAX_XFB_HEIGHT;
179
180 Common::EventHook m_config_changed;
181 Common::EventHook m_end_field_hook;
182
183 // Updates state for the SmoothEarlyPresentation setting if enabled.
184 // Returns the desired presentation time regardless.
185 TimePoint GetUpdatedPresentationTime(TimePoint intended_presentation_time);
186
187 // Used by the SmoothEarlyPresentation setting.
188 DT m_presentation_time_offset{};
189
190 // Calculated from the previous swap time and current refresh rate.
191 // Can be used for presentation of ImmediateXFB swaps which don't have timing information.
192 u64 m_next_swap_estimated_ticks = 0;
193 TimePoint m_next_swap_estimated_time{Clock::now()};
194
195 std::atomic_bool m_immediate_swap_happened_this_field{};
196 };
197
198 } // namespace VideoCommon
199
200 extern std::unique_ptr<VideoCommon::Presenter> g_presenter;