loading_screen.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <unordered_map>
  4. #include <QBuffer>
  5. #include <QByteArray>
  6. #include <QGraphicsOpacityEffect>
  7. #include <QIODevice>
  8. #include <QImage>
  9. #include <QPainter>
  10. #include <QPixmap>
  11. #include <QPropertyAnimation>
  12. #include <QStyleOption>
  13. #include "core/frontend/framebuffer_layout.h"
  14. #include "core/loader/loader.h"
  15. #include "ui_loading_screen.h"
  16. #include "video_core/rasterizer_interface.h"
  17. #include "yuzu/loading_screen.h"
  18. // Mingw seems to not have QMovie at all. If QMovie is missing then use a single frame instead of an
  19. // showing the full animation
  20. #if !YUZU_QT_MOVIE_MISSING
  21. #include <QMovie>
  22. #endif
  23. constexpr char PROGRESSBAR_STYLE_PREPARE[] = R"(
  24. QProgressBar {}
  25. QProgressBar::chunk {})";
  26. constexpr char PROGRESSBAR_STYLE_BUILD[] = R"(
  27. QProgressBar {
  28. background-color: black;
  29. border: 2px solid white;
  30. border-radius: 4px;
  31. padding: 2px;
  32. }
  33. QProgressBar::chunk {
  34. background-color: #ff3c28;
  35. width: 1px;
  36. })";
  37. constexpr char PROGRESSBAR_STYLE_COMPLETE[] = R"(
  38. QProgressBar {
  39. background-color: #0ab9e6;
  40. border: 2px solid white;
  41. border-radius: 4px;
  42. padding: 2px;
  43. }
  44. QProgressBar::chunk {
  45. background-color: #ff3c28;
  46. })";
  47. LoadingScreen::LoadingScreen(QWidget* parent)
  48. : QWidget(parent), ui(std::make_unique<Ui::LoadingScreen>()),
  49. previous_stage(VideoCore::LoadCallbackStage::Complete) {
  50. ui->setupUi(this);
  51. setMinimumSize(Layout::MinimumSize::Width, Layout::MinimumSize::Height);
  52. // Create a fade out effect to hide this loading screen widget.
  53. // When fading opacity, it will fade to the parent widgets background color, which is why we
  54. // create an internal widget named fade_widget that we use the effect on, while keeping the
  55. // loading screen widget's background color black. This way we can create a fade to black effect
  56. opacity_effect = new QGraphicsOpacityEffect(this);
  57. opacity_effect->setOpacity(1);
  58. ui->fade_parent->setGraphicsEffect(opacity_effect);
  59. fadeout_animation = std::make_unique<QPropertyAnimation>(opacity_effect, "opacity");
  60. fadeout_animation->setDuration(500);
  61. fadeout_animation->setStartValue(1);
  62. fadeout_animation->setEndValue(0);
  63. fadeout_animation->setEasingCurve(QEasingCurve::OutBack);
  64. // After the fade completes, hide the widget and reset the opacity
  65. connect(fadeout_animation.get(), &QPropertyAnimation::finished, [this] {
  66. hide();
  67. opacity_effect->setOpacity(1);
  68. emit Hidden();
  69. });
  70. connect(this, &LoadingScreen::LoadProgress, this, &LoadingScreen::OnLoadProgress,
  71. Qt::QueuedConnection);
  72. qRegisterMetaType<VideoCore::LoadCallbackStage>();
  73. stage_translations = {
  74. {VideoCore::LoadCallbackStage::Prepare, tr("Loading...")},
  75. {VideoCore::LoadCallbackStage::Build, tr("Loading Shaders %1 / %2")},
  76. {VideoCore::LoadCallbackStage::Complete, tr("Launching...")},
  77. };
  78. progressbar_style = {
  79. {VideoCore::LoadCallbackStage::Prepare, PROGRESSBAR_STYLE_PREPARE},
  80. {VideoCore::LoadCallbackStage::Build, PROGRESSBAR_STYLE_BUILD},
  81. {VideoCore::LoadCallbackStage::Complete, PROGRESSBAR_STYLE_COMPLETE},
  82. };
  83. }
  84. LoadingScreen::~LoadingScreen() = default;
  85. void LoadingScreen::Prepare(Loader::AppLoader& loader) {
  86. std::vector<u8> buffer;
  87. if (loader.ReadBanner(buffer) == Loader::ResultStatus::Success) {
  88. #ifdef YUZU_QT_MOVIE_MISSING
  89. QPixmap map;
  90. map.loadFromData(buffer.data(), buffer.size());
  91. ui->banner->setPixmap(map);
  92. #else
  93. backing_mem = std::make_unique<QByteArray>(reinterpret_cast<char*>(buffer.data()),
  94. static_cast<int>(buffer.size()));
  95. backing_buf = std::make_unique<QBuffer>(backing_mem.get());
  96. backing_buf->open(QIODevice::ReadOnly);
  97. animation = std::make_unique<QMovie>(backing_buf.get(), QByteArray());
  98. animation->start();
  99. ui->banner->setMovie(animation.get());
  100. #endif
  101. buffer.clear();
  102. }
  103. if (loader.ReadLogo(buffer) == Loader::ResultStatus::Success) {
  104. QPixmap map;
  105. map.loadFromData(buffer.data(), static_cast<uint>(buffer.size()));
  106. ui->logo->setPixmap(map);
  107. }
  108. slow_shader_compile_start = false;
  109. OnLoadProgress(VideoCore::LoadCallbackStage::Prepare, 0, 0);
  110. }
  111. void LoadingScreen::OnLoadComplete() {
  112. fadeout_animation->start(QPropertyAnimation::KeepWhenStopped);
  113. }
  114. void LoadingScreen::OnLoadProgress(VideoCore::LoadCallbackStage stage, std::size_t value,
  115. std::size_t total) {
  116. using namespace std::chrono;
  117. const auto now = steady_clock::now();
  118. // reset the timer if the stage changes
  119. if (stage != previous_stage) {
  120. ui->progress_bar->setStyleSheet(QString::fromUtf8(progressbar_style[stage]));
  121. // Hide the progress bar during the prepare stage
  122. if (stage == VideoCore::LoadCallbackStage::Prepare) {
  123. ui->progress_bar->hide();
  124. } else {
  125. ui->progress_bar->show();
  126. }
  127. previous_stage = stage;
  128. // reset back to fast shader compiling since the stage changed
  129. slow_shader_compile_start = false;
  130. }
  131. // update the max of the progress bar if the number of shaders change
  132. if (total != previous_total) {
  133. ui->progress_bar->setMaximum(static_cast<int>(total));
  134. previous_total = total;
  135. }
  136. // Reset the progress bar ranges if compilation is done
  137. if (stage == VideoCore::LoadCallbackStage::Complete) {
  138. ui->progress_bar->setRange(0, 0);
  139. }
  140. QString estimate;
  141. // If theres a drastic slowdown in the rate, then display an estimate
  142. if (now - previous_time > milliseconds{50} || slow_shader_compile_start) {
  143. if (!slow_shader_compile_start) {
  144. slow_shader_start = steady_clock::now();
  145. slow_shader_compile_start = true;
  146. slow_shader_first_value = value;
  147. }
  148. // only calculate an estimate time after a second has passed since stage change
  149. const auto diff = duration_cast<milliseconds>(now - slow_shader_start);
  150. if (diff > seconds{1}) {
  151. const auto eta_mseconds =
  152. static_cast<long>(static_cast<double>(total - slow_shader_first_value) /
  153. (value - slow_shader_first_value) * diff.count());
  154. estimate =
  155. tr("Estimated Time %1")
  156. .arg(QTime(0, 0, 0, 0)
  157. .addMSecs(std::max<long>(eta_mseconds - diff.count() + 1000, 1000))
  158. .toString(QStringLiteral("mm:ss")));
  159. }
  160. }
  161. // update labels and progress bar
  162. if (stage == VideoCore::LoadCallbackStage::Build) {
  163. ui->stage->setText(stage_translations[stage].arg(value).arg(total));
  164. } else {
  165. ui->stage->setText(stage_translations[stage]);
  166. }
  167. ui->value->setText(estimate);
  168. ui->progress_bar->setValue(static_cast<int>(value));
  169. previous_time = now;
  170. }
  171. void LoadingScreen::paintEvent(QPaintEvent* event) {
  172. QStyleOption opt;
  173. opt.initFrom(this);
  174. QPainter p(this);
  175. style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
  176. QWidget::paintEvent(event);
  177. }
  178. void LoadingScreen::Clear() {
  179. #ifndef YUZU_QT_MOVIE_MISSING
  180. animation.reset();
  181. backing_buf.reset();
  182. backing_mem.reset();
  183. #endif
  184. }