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 rc;
size_t total_fonts = 0;
if (R_FAILED((rc = plGetSharedFont(GetFatalConfig()->language_code, g_fonts, PlSharedFontType_Total, &total_fonts)))) {
return rc;
}
if (R_FAILED((rc = plGetSharedFontByType(&g_font, PlSharedFontType_Standard)))) {
return rc;
}
R_TRY(plGetSharedFont(GetFatalConfig()->language_code, g_fonts, PlSharedFontType_Total, &total_fonts));
R_TRY(plGetSharedFontByType(&g_font, PlSharedFontType_Standard));
g_ft_err = FT_Init_FreeType(&g_library);
if (g_ft_err) return g_ft_err;

View file

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

View file

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