fatal: refactor for R_TRY

This commit is contained in:
Michael Scire 2019-06-17 16:41:03 -07:00
parent f9bf8923b1
commit 31fde233e1
30 changed files with 226 additions and 285 deletions

View file

@ -217,16 +217,10 @@ void FontManager::ConfigureFontFramebuffer(u16 *fb, u32 (*unswizzle_func)(u32, u
} }
Result FontManager::InitializeSharedFont() { Result FontManager::InitializeSharedFont() {
Result rc;
size_t total_fonts = 0; size_t total_fonts = 0;
if (R_FAILED((rc = plGetSharedFont(GetFatalConfig()->language_code, g_fonts, PlSharedFontType_Total, &total_fonts)))) { R_TRY(plGetSharedFont(GetFatalConfig()->language_code, g_fonts, PlSharedFontType_Total, &total_fonts));
return rc; R_TRY(plGetSharedFontByType(&g_font, PlSharedFontType_Standard));
}
if (R_FAILED((rc = plGetSharedFontByType(&g_font, PlSharedFontType_Standard)))) {
return rc;
}
g_ft_err = FT_Init_FreeType(&g_library); g_ft_err = FT_Init_FreeType(&g_library);
if (g_ft_err) return g_ft_err; if (g_ft_err) return g_ft_err;

View file

@ -18,29 +18,22 @@
#include "fatal_task_clock.hpp" #include "fatal_task_clock.hpp"
Result AdjustClockTask::AdjustClockForModule(PcvModule module, u32 hz) { Result AdjustClockTask::AdjustClockForModule(PcvModule module, u32 hz) {
Result rc;
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_800) { if (GetRuntimeFirmwareVersion() >= FirmwareVersion_800) {
/* On 8.0.0+, convert to module id + use clkrst API. */ /* On 8.0.0+, convert to module id + use clkrst API. */
PcvModuleId module_id; PcvModuleId module_id;
if (R_FAILED((rc = pcvGetModuleId(&module_id, module)))) { R_TRY(pcvGetModuleId(&module_id, module));
return rc;
}
ClkrstSession session; ClkrstSession session;
Result rc = clkrstOpenSession(&session, module_id, 3); R_TRY(clkrstOpenSession(&session, module_id, 3));
if (R_FAILED(rc)) {
return rc;
}
ON_SCOPE_EXIT { clkrstCloseSession(&session); }; ON_SCOPE_EXIT { clkrstCloseSession(&session); };
rc = clkrstSetClockRate(&session, hz); R_TRY(clkrstSetClockRate(&session, hz));
} else { } else {
/* On 1.0.0-7.0.1, use pcv API. */ /* On 1.0.0-7.0.1, use pcv API. */
rc = pcvSetClockRate(module, hz); R_TRY(pcvSetClockRate(module, hz));
} }
return rc; return ResultSuccess;
} }
Result AdjustClockTask::AdjustClock() { Result AdjustClockTask::AdjustClock() {
@ -48,21 +41,12 @@ Result AdjustClockTask::AdjustClock() {
constexpr u32 CPU_CLOCK_1020MHZ = 0x3CCBF700L; constexpr u32 CPU_CLOCK_1020MHZ = 0x3CCBF700L;
constexpr u32 GPU_CLOCK_307MHZ = 0x124F8000L; constexpr u32 GPU_CLOCK_307MHZ = 0x124F8000L;
constexpr u32 EMC_CLOCK_1331MHZ = 0x4F588000L; constexpr u32 EMC_CLOCK_1331MHZ = 0x4F588000L;
Result rc = ResultSuccess;
if (R_FAILED((rc = AdjustClockForModule(PcvModule_CpuBus, CPU_CLOCK_1020MHZ)))) { R_TRY(AdjustClockForModule(PcvModule_CpuBus, CPU_CLOCK_1020MHZ));
return rc; R_TRY(AdjustClockForModule(PcvModule_GPU, GPU_CLOCK_307MHZ));
} R_TRY(AdjustClockForModule(PcvModule_EMC, EMC_CLOCK_1331MHZ));
if (R_FAILED((rc = AdjustClockForModule(PcvModule_GPU, GPU_CLOCK_307MHZ)))) { return ResultSuccess;
return rc;
}
if (R_FAILED((rc = AdjustClockForModule(PcvModule_EMC, EMC_CLOCK_1331MHZ)))) {
return rc;
}
return rc;
} }
Result AdjustClockTask::Run() { Result AdjustClockTask::Run() {

View file

@ -43,92 +43,69 @@ u32 GetPixelOffset(uint32_t x, uint32_t y)
} }
Result ShowFatalTask::SetupDisplayInternal() { Result ShowFatalTask::SetupDisplayInternal() {
Result rc;
ViDisplay display; ViDisplay display;
/* Try to open the display. */ /* Try to open the display. */
if (R_FAILED((rc = viOpenDisplay("Internal", &display)))) { R_TRY_CATCH(viOpenDisplay("Internal", &display)) {
if (rc == ResultViNotFound) { R_CATCH(ResultViNotFound) {
return ResultSuccess; return ResultSuccess;
} else {
return rc;
} }
} } R_END_TRY_CATCH;
/* Guarantee we close the display. */ /* Guarantee we close the display. */
ON_SCOPE_EXIT { viCloseDisplay(&display); }; ON_SCOPE_EXIT { viCloseDisplay(&display); };
/* Turn on the screen. */ /* Turn on the screen. */
if (R_FAILED((rc = viSetDisplayPowerState(&display, ViPowerState_On)))) { R_TRY(viSetDisplayPowerState(&display, ViPowerState_On));
return rc;
}
/* Set alpha to 1.0f. */ /* Set alpha to 1.0f. */
if (R_FAILED((rc = viSetDisplayAlpha(&display, 1.0f)))) { R_TRY(viSetDisplayAlpha(&display, 1.0f));
return rc;
}
return rc; return ResultSuccess;
} }
Result ShowFatalTask::SetupDisplayExternal() { Result ShowFatalTask::SetupDisplayExternal() {
Result rc;
ViDisplay display; ViDisplay display;
/* Try to open the display. */ /* Try to open the display. */
if (R_FAILED((rc = viOpenDisplay("External", &display)))) { R_TRY_CATCH(viOpenDisplay("External", &display)) {
if (rc == ResultViNotFound) { R_CATCH(ResultViNotFound) {
return ResultSuccess; return ResultSuccess;
} else {
return rc;
} }
} } R_END_TRY_CATCH;
/* Guarantee we close the display. */ /* Guarantee we close the display. */
ON_SCOPE_EXIT { viCloseDisplay(&display); }; ON_SCOPE_EXIT { viCloseDisplay(&display); };
/* Set alpha to 1.0f. */ /* Set alpha to 1.0f. */
if (R_FAILED((rc = viSetDisplayAlpha(&display, 1.0f)))) { R_TRY(viSetDisplayAlpha(&display, 1.0f));
return rc;
}
return rc; return ResultSuccess;
} }
Result ShowFatalTask::PrepareScreenForDrawing() { Result ShowFatalTask::PrepareScreenForDrawing() {
Result rc = ResultSuccess;
/* Connect to vi. */ /* Connect to vi. */
if (R_FAILED((rc = viInitialize(ViServiceType_Manager)))) { R_TRY(viInitialize(ViServiceType_Manager));
return rc;
}
/* Close other content. */ /* Close other content. */
viSetContentVisibility(false); viSetContentVisibility(false);
/* Setup the two displays. */ /* Setup the two displays. */
if (R_FAILED((rc = SetupDisplayInternal())) || R_FAILED((rc = SetupDisplayExternal()))) { R_TRY(SetupDisplayInternal());
return rc; R_TRY(SetupDisplayExternal());
}
/* Open the default display. */ /* Open the default display. */
if (R_FAILED((rc = viOpenDefaultDisplay(&this->display)))) { R_TRY(viOpenDefaultDisplay(&this->display));
return rc;
}
/* Reset the display magnification to its default value. */ /* Reset the display magnification to its default value. */
u32 display_width, display_height; u32 display_width, display_height;
if (R_FAILED((rc = viGetDisplayLogicalResolution(&this->display, &display_width, &display_height)))) { R_TRY(viGetDisplayLogicalResolution(&this->display, &display_width, &display_height));
return rc;
}
/* viSetDisplayMagnification was added in 3.0.0. */ /* viSetDisplayMagnification was added in 3.0.0. */
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_300) { if (GetRuntimeFirmwareVersion() >= FirmwareVersion_300) {
if (R_FAILED((rc = viSetDisplayMagnification(&this->display, 0, 0, display_width, display_height)))) { R_TRY(viSetDisplayMagnification(&this->display, 0, 0, display_width, display_height));
return rc;
}
} }
/* Create layer to draw to. */ /* Create layer to draw to. */
if (R_FAILED((rc = viCreateLayer(&this->display, &this->layer)))) { R_TRY(viCreateLayer(&this->display, &this->layer));
return rc;
}
/* Setup the layer. */ /* Setup the layer. */
{ {
@ -144,47 +121,36 @@ Result ShowFatalTask::PrepareScreenForDrawing() {
const float layer_y = static_cast<float>((display_height - layer_height) / 2); const float layer_y = static_cast<float>((display_height - layer_height) / 2);
u64 layer_z; u64 layer_z;
if (R_FAILED((rc = viSetLayerSize(&this->layer, layer_width, layer_height)))) { R_TRY(viSetLayerSize(&this->layer, layer_width, layer_height));
return rc;
}
/* Set the layer's Z at display maximum, to be above everything else .*/ /* Set the layer's Z at display maximum, to be above everything else .*/
/* NOTE: Fatal hardcodes 100 here. */ /* NOTE: Fatal hardcodes 100 here. */
if (R_SUCCEEDED((rc = viGetDisplayMaximumZ(&this->display, &layer_z)))) { if (R_SUCCEEDED(viGetDisplayMaximumZ(&this->display, &layer_z))) {
if (R_FAILED((rc = viSetLayerZ(&this->layer, layer_z)))) { R_TRY(viSetLayerZ(&this->layer, layer_z));
return rc;
}
} }
/* Center the layer in the screen. */ /* Center the layer in the screen. */
if (R_FAILED((rc = viSetLayerPosition(&this->layer, layer_x, layer_y)))) { R_TRY(viSetLayerPosition(&this->layer, layer_x, layer_y));
return rc;
}
/* Create framebuffer. */ /* Create framebuffer. */
if (R_FAILED(rc = nwindowCreateFromLayer(&this->win, &this->layer))) { R_TRY(nwindowCreateFromLayer(&this->win, &this->layer));
return rc; R_TRY(framebufferCreate(&this->fb, &this->win, raw_width, raw_height, PIXEL_FORMAT_RGB_565, 1));
}
if (R_FAILED(rc = framebufferCreate(&this->fb, &this->win, raw_width, raw_height, PIXEL_FORMAT_RGB_565, 1))) {
return rc;
}
} }
return ResultSuccess;
return rc;
} }
Result ShowFatalTask::ShowFatal() { Result ShowFatalTask::ShowFatal() {
Result rc = ResultSuccess;
const FatalConfig *config = GetFatalConfig(); const FatalConfig *config = GetFatalConfig();
/* Prepare screen for drawing. */
DoWithSmSession([&]() { DoWithSmSession([&]() {
rc = PrepareScreenForDrawing(); Result rc = PrepareScreenForDrawing();
if (R_FAILED(rc)) {
*(volatile u32 *)(0xCAFEBABE) = rc;
std::abort();
}
}); });
if (R_FAILED(rc)) {
*(volatile u32 *)(0xCAFEBABE) = rc;
return rc;
}
/* Dequeue a buffer. */ /* Dequeue a buffer. */
u16 *tiled_buf = reinterpret_cast<u16 *>(framebufferBegin(&this->fb, NULL)); u16 *tiled_buf = reinterpret_cast<u16 *>(framebufferBegin(&this->fb, NULL));
@ -407,7 +373,7 @@ Result ShowFatalTask::ShowFatal() {
/* Enqueue the buffer. */ /* Enqueue the buffer. */
framebufferEnd(&fb); framebufferEnd(&fb);
return rc; return ResultSuccess;
} }
Result ShowFatalTask::Run() { Result ShowFatalTask::Run() {

View file

@ -41,7 +41,6 @@ Result ThrowFatalForSelf(u32 error) {
} }
Result ThrowFatalImpl(u32 error, u64 pid, FatalType policy, FatalCpuContext *cpu_ctx) { Result ThrowFatalImpl(u32 error, u64 pid, FatalType policy, FatalCpuContext *cpu_ctx) {
Result rc = ResultSuccess;
FatalThrowContext ctx = {0}; FatalThrowContext ctx = {0};
ctx.error_code = error; ctx.error_code = error;
if (cpu_ctx != nullptr) { if (cpu_ctx != nullptr) {
@ -91,9 +90,7 @@ Result ThrowFatalImpl(u32 error, u64 pid, FatalType policy, FatalCpuContext *cpu
case FatalType_ErrorScreen: case FatalType_ErrorScreen:
{ {
/* Ensure we only throw once. */ /* Ensure we only throw once. */
if (R_FAILED((rc = SetThrown()))) { R_TRY(SetThrown());
return rc;
}
/* Signal that fatal is about to happen. */ /* Signal that fatal is about to happen. */
GetEventManager()->SignalEvents(); GetEventManager()->SignalEvents();