loading_screen.cpp 7.6 KB

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