loading_screen.cpp 7.3 KB

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