loading_screen.cpp 7.3 KB

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