Debian Codegraph @

Source/Core/VideoCommon/OnScreenDisplay.cpp

raw symbols SCIP
1 // Copyright 2009 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/OnScreenDisplay.h"
5
6 #include <algorithm>
7 #include <atomic>
8 #include <map>
9 #include <mutex>
10 #include <string>
11
12 #include <fmt/format.h>
13 #include <imgui.h>
14
15 #include "Common/CommonTypes.h"
16 #include "Common/Config/Config.h"
17 #include "Common/Timer.h"
18
19 #include "Core/Config/MainSettings.h"
20
21 #include "VideoCommon/AbstractGfx.h"
22 #include "VideoCommon/AbstractTexture.h"
23 #include "VideoCommon/Assets/CustomTextureData.h"
24 #include "VideoCommon/TextureConfig.h"
25
26 namespace OSD
27 {
28 constexpr float LEFT_MARGIN = 10.0f; // Pixels to the left of OSD messages.
29 constexpr float TOP_MARGIN = 10.0f; // Pixels above the first OSD message.
30 constexpr float WINDOW_PADDING = 4.0f; // Pixels between subsequent OSD messages.
31 constexpr float MESSAGE_FADE_TIME = 1000.f; // Ms to fade OSD messages at the end of their life.
32 constexpr float MESSAGE_DROP_TIME = 5000.f; // Ms to drop OSD messages that has yet to ever render.
33
34 static std::atomic<int> s_obscured_pixels_left = 0;
35 static std::atomic<int> s_obscured_pixels_top = 0;
36
37 struct Message
38 {
39 Message() = default;
40 Message(std::string text_, u32 duration_, u32 color_,
41 const VideoCommon::CustomTextureData::ArraySlice::Level* icon_ = nullptr)
42 : text(std::move(text_)), duration(duration_), color(color_), icon(icon_)
43 {
44 timer.Start();
45 }
46 s64 TimeRemaining() const { return duration - timer.ElapsedMs(); }
47 std::string text;
48 Common::Timer timer;
49 u32 duration = 0;
50 bool ever_drawn = false;
51 bool should_discard = false;
52 u32 color = 0;
53 const VideoCommon::CustomTextureData::ArraySlice::Level* icon;
54 std::unique_ptr<AbstractTexture> texture;
55 };
56 static std::multimap<MessageType, Message> s_messages;
57 static std::mutex s_messages_mutex;
58
59 static ImVec4 ARGBToImVec4(const u32 argb)
60 {
61 return ImVec4(static_cast<float>((argb >> 16) & 0xFF) / 255.0f,
62 static_cast<float>((argb >> 8) & 0xFF) / 255.0f,
63 static_cast<float>((argb >> 0) & 0xFF) / 255.0f,
64 static_cast<float>((argb >> 24) & 0xFF) / 255.0f);
65 }
66
67 static float DrawMessage(int index, Message& msg, const ImVec2& position, int time_left)
68 {
69 // We have to provide a window name, and these shouldn't be duplicated.
70 // So instead, we generate a name based on the number of messages drawn.
71 const std::string window_name = fmt::format("osd_{}", index);
72
73 // The size must be reset, otherwise the length of old messages could influence new ones.
74 ImGui::SetNextWindowPos(position);
75 ImGui::SetNextWindowSize(ImVec2(0.0f, 0.0f));
76
77 // Gradually fade old messages away (except in their first frame)
78 const float fade_time = std::max(std::min(MESSAGE_FADE_TIME, (float)msg.duration), 1.f);
79 const float alpha = std::clamp(time_left / fade_time, 0.f, 1.f);
80 ImGui::PushStyleVar(ImGuiStyleVar_Alpha, msg.ever_drawn ? alpha : 1.0);
81
82 float window_height = 0.0f;
83 if (ImGui::Begin(window_name.c_str(), nullptr,
84 ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoInputs |
85 ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings |
86 ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoNav |
87 ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoFocusOnAppearing))
88 {
89 if (msg.icon)
90 {
91 if (!msg.texture)
92 {
93 const u32 width = msg.icon->width;
94 const u32 height = msg.icon->height;
95 TextureConfig tex_config(width, height, 1, 1, 1, AbstractTextureFormat::RGBA8, 0,
96 AbstractTextureType::Texture_2DArray);
97 msg.texture = g_gfx->CreateTexture(tex_config);
98 if (msg.texture)
99 {
100 msg.texture->Load(0, width, height, width, msg.icon->data.data(),
101 sizeof(u32) * width * height);
102 }
103 else
104 {
105 // don't try again next time
106 msg.icon = nullptr;
107 }
108 }
109
110 if (msg.texture)
111 {
112 ImGui::Image(*msg.texture.get(), ImVec2(static_cast<float>(msg.icon->width),
113 static_cast<float>(msg.icon->height)));
114 ImGui::SameLine();
115 }
116 }
117
118 // Use %s in case message contains %.
119 if (msg.text.size() > 0)
120 ImGui::TextColored(ARGBToImVec4(msg.color), "%s", msg.text.c_str());
121 window_height =
122 ImGui::GetWindowSize().y + (WINDOW_PADDING * ImGui::GetIO().DisplayFramebufferScale.y);
123 }
124
125 ImGui::End();
126 ImGui::PopStyleVar();
127
128 msg.ever_drawn = true;
129
130 return window_height;
131 }
132
133 void AddTypedMessage(MessageType type, std::string message, u32 ms, u32 argb,
134 const VideoCommon::CustomTextureData::ArraySlice::Level* icon)
135 {
136 std::lock_guard lock{s_messages_mutex};
137
138 // A message may hold a reference to a texture that can only be destroyed on the video thread, so
139 // only mark the old typed message (if any) for removal. It will be discarded on the next call to
140 // DrawMessages().
141 auto range = s_messages.equal_range(type);
142 for (auto it = range.first; it != range.second; ++it)
143 it->second.should_discard = true;
144
145 s_messages.emplace(type, Message(std::move(message), ms, argb, icon));
146 }
147
148 void AddMessage(std::string message, u32 ms, u32 argb,
149 const VideoCommon::CustomTextureData::ArraySlice::Level* icon)
150 {
151 std::lock_guard lock{s_messages_mutex};
152 s_messages.emplace(MessageType::Typeless, Message(std::move(message), ms, argb, icon));
153 }
154
155 void DrawMessages()
156 {
157 const bool draw_messages = Config::Get(Config::MAIN_OSD_MESSAGES);
158 const float current_x =
159 LEFT_MARGIN * ImGui::GetIO().DisplayFramebufferScale.x + s_obscured_pixels_left;
160 float current_y = TOP_MARGIN * ImGui::GetIO().DisplayFramebufferScale.y + s_obscured_pixels_top;
161 int index = 0;
162
163 std::lock_guard lock{s_messages_mutex};
164
165 for (auto it = s_messages.begin(); it != s_messages.end();)
166 {
167 Message& msg = it->second;
168 if (msg.should_discard)
169 {
170 it = s_messages.erase(it);
171 continue;
172 }
173
174 const s64 time_left = msg.TimeRemaining();
175
176 // Make sure we draw them at least once if they were printed with 0ms,
177 // unless enough time has expired, in that case, we drop them
178 if (time_left <= 0 && (msg.ever_drawn || -time_left >= MESSAGE_DROP_TIME))
179 {
180 it = s_messages.erase(it);
181 continue;
182 }
183 else
184 {
185 ++it;
186 }
187
188 if (draw_messages)
189 current_y += DrawMessage(index++, msg, ImVec2(current_x, current_y), time_left);
190 }
191 }
192
193 void ClearMessages()
194 {
195 std::lock_guard lock{s_messages_mutex};
196 s_messages.clear();
197 }
198
199 void SetObscuredPixelsLeft(int width)
200 {
201 s_obscured_pixels_left = width;
202 }
203
204 void SetObscuredPixelsTop(int height)
205 {
206 s_obscured_pixels_top = height;
207 }
208 } // namespace OSD