OnScreenDisplay.cpp 6.7 KB

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