From 77b7e1e68295221c05184d89bc46adb0129c5ec4 Mon Sep 17 00:00:00 2001
From: Liam <byteslice@airmail.cc>
Date: Sun, 11 Feb 2024 19:44:17 -0500
Subject: [PATCH] am: rewrite IDisplayController

---
 src/core/CMakeLists.txt                       |   4 +-
 .../hle/service/am/display_controller.cpp     | 135 ------------------
 src/core/hle/service/am/display_controller.h  |  30 ----
 .../service/am/service/application_proxy.cpp  |   2 +-
 .../service/am/service/display_controller.cpp | 105 ++++++++++++++
 .../service/am/service/display_controller.h   |  36 +++++
 .../am/service/library_applet_proxy.cpp       |   2 +-
 .../am/service/system_applet_proxy.cpp        |   2 +-
 8 files changed, 146 insertions(+), 170 deletions(-)
 delete mode 100644 src/core/hle/service/am/display_controller.cpp
 delete mode 100644 src/core/hle/service/am/display_controller.h
 create mode 100644 src/core/hle/service/am/service/display_controller.cpp
 create mode 100644 src/core/hle/service/am/service/display_controller.h

diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index ad9831979..b5b1192b3 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -425,8 +425,6 @@ add_library(core STATIC
     hle/service/am/applet_message_queue.h
     hle/service/am/application_creator.cpp
     hle/service/am/application_creator.h
-    hle/service/am/display_controller.cpp
-    hle/service/am/display_controller.h
     hle/service/am/global_state_controller.cpp
     hle/service/am/global_state_controller.h
     hle/service/am/hid_registration.cpp
@@ -471,6 +469,8 @@ add_library(core STATIC
     hle/service/am/service/common_state_getter.h
     hle/service/am/service/debug_functions.cpp
     hle/service/am/service/debug_functions.h
+    hle/service/am/service/display_controller.cpp
+    hle/service/am/service/display_controller.h
     hle/service/am/service/library_applet_proxy.cpp
     hle/service/am/service/library_applet_proxy.h
     hle/service/am/service/system_applet_proxy.cpp
diff --git a/src/core/hle/service/am/display_controller.cpp b/src/core/hle/service/am/display_controller.cpp
deleted file mode 100644
index 4d6858348..000000000
--- a/src/core/hle/service/am/display_controller.cpp
+++ /dev/null
@@ -1,135 +0,0 @@
-// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
-// SPDX-License-Identifier: GPL-2.0-or-later
-
-#include "core/hle/service/am/applet.h"
-#include "core/hle/service/am/display_controller.h"
-#include "core/hle/service/ipc_helpers.h"
-
-namespace Service::AM {
-
-namespace {
-struct OutputParameters {
-    bool was_written;
-    s32 fbshare_layer_index;
-};
-
-static_assert(sizeof(OutputParameters) == 8, "OutputParameters has wrong size");
-} // namespace
-
-IDisplayController::IDisplayController(Core::System& system_, std::shared_ptr<Applet> applet_)
-    : ServiceFramework{system_, "IDisplayController"}, applet(std::move(applet_)) {
-    // clang-format off
-    static const FunctionInfo functions[] = {
-        {0, nullptr, "GetLastForegroundCaptureImage"},
-        {1, nullptr, "UpdateLastForegroundCaptureImage"},
-        {2, nullptr, "GetLastApplicationCaptureImage"},
-        {3, nullptr, "GetCallerAppletCaptureImage"},
-        {4, nullptr, "UpdateCallerAppletCaptureImage"},
-        {5, nullptr, "GetLastForegroundCaptureImageEx"},
-        {6, nullptr, "GetLastApplicationCaptureImageEx"},
-        {7, &IDisplayController::GetCallerAppletCaptureImageEx, "GetCallerAppletCaptureImageEx"},
-        {8, &IDisplayController::TakeScreenShotOfOwnLayer, "TakeScreenShotOfOwnLayer"},
-        {9, nullptr, "CopyBetweenCaptureBuffers"},
-        {10, nullptr, "AcquireLastApplicationCaptureBuffer"},
-        {11, nullptr, "ReleaseLastApplicationCaptureBuffer"},
-        {12, nullptr, "AcquireLastForegroundCaptureBuffer"},
-        {13, nullptr, "ReleaseLastForegroundCaptureBuffer"},
-        {14, nullptr, "AcquireCallerAppletCaptureBuffer"},
-        {15, nullptr, "ReleaseCallerAppletCaptureBuffer"},
-        {16, nullptr, "AcquireLastApplicationCaptureBufferEx"},
-        {17, nullptr, "AcquireLastForegroundCaptureBufferEx"},
-        {18, nullptr, "AcquireCallerAppletCaptureBufferEx"},
-        {20, nullptr, "ClearCaptureBuffer"},
-        {21, nullptr, "ClearAppletTransitionBuffer"},
-        {22, &IDisplayController::AcquireLastApplicationCaptureSharedBuffer, "AcquireLastApplicationCaptureSharedBuffer"},
-        {23, &IDisplayController::ReleaseLastApplicationCaptureSharedBuffer, "ReleaseLastApplicationCaptureSharedBuffer"},
-        {24, &IDisplayController::AcquireLastForegroundCaptureSharedBuffer, "AcquireLastForegroundCaptureSharedBuffer"},
-        {25, &IDisplayController::ReleaseLastForegroundCaptureSharedBuffer, "ReleaseLastForegroundCaptureSharedBuffer"},
-        {26, &IDisplayController::AcquireCallerAppletCaptureSharedBuffer, "AcquireCallerAppletCaptureSharedBuffer"},
-        {27, &IDisplayController::ReleaseCallerAppletCaptureSharedBuffer, "ReleaseCallerAppletCaptureSharedBuffer"},
-        {28, nullptr, "TakeScreenShotOfOwnLayerEx"},
-    };
-    // clang-format on
-
-    RegisterHandlers(functions);
-}
-
-IDisplayController::~IDisplayController() = default;
-
-void IDisplayController::GetCallerAppletCaptureImageEx(HLERequestContext& ctx) {
-    LOG_WARNING(Service_AM, "(STUBBED) called");
-
-    OutputParameters params{};
-    const auto res = applet->system_buffer_manager.WriteAppletCaptureBuffer(
-        &params.was_written, &params.fbshare_layer_index);
-
-    IPC::ResponseBuilder rb{ctx, 4};
-    rb.Push(res);
-    rb.PushRaw(params);
-}
-
-void IDisplayController::TakeScreenShotOfOwnLayer(HLERequestContext& ctx) {
-    LOG_WARNING(Service_AM, "(STUBBED) called");
-
-    IPC::ResponseBuilder rb{ctx, 2};
-    rb.Push(ResultSuccess);
-}
-
-void IDisplayController::AcquireLastApplicationCaptureSharedBuffer(HLERequestContext& ctx) {
-    LOG_WARNING(Service_AM, "(STUBBED) called");
-
-    OutputParameters params{};
-    const auto res = applet->system_buffer_manager.WriteAppletCaptureBuffer(
-        &params.was_written, &params.fbshare_layer_index);
-
-    IPC::ResponseBuilder rb{ctx, 4};
-    rb.Push(res);
-    rb.PushRaw(params);
-}
-
-void IDisplayController::ReleaseLastApplicationCaptureSharedBuffer(HLERequestContext& ctx) {
-    LOG_WARNING(Service_AM, "(STUBBED) called");
-
-    IPC::ResponseBuilder rb{ctx, 2};
-    rb.Push(ResultSuccess);
-}
-
-void IDisplayController::AcquireLastForegroundCaptureSharedBuffer(HLERequestContext& ctx) {
-    LOG_WARNING(Service_AM, "(STUBBED) called");
-
-    OutputParameters params{};
-    const auto res = applet->system_buffer_manager.WriteAppletCaptureBuffer(
-        &params.was_written, &params.fbshare_layer_index);
-
-    IPC::ResponseBuilder rb{ctx, 4};
-    rb.Push(res);
-    rb.PushRaw(params);
-}
-
-void IDisplayController::ReleaseLastForegroundCaptureSharedBuffer(HLERequestContext& ctx) {
-    LOG_WARNING(Service_AM, "(STUBBED) called");
-
-    IPC::ResponseBuilder rb{ctx, 2};
-    rb.Push(ResultSuccess);
-}
-
-void IDisplayController::AcquireCallerAppletCaptureSharedBuffer(HLERequestContext& ctx) {
-    LOG_WARNING(Service_AM, "(STUBBED) called");
-
-    OutputParameters params{};
-    const auto res = applet->system_buffer_manager.WriteAppletCaptureBuffer(
-        &params.was_written, &params.fbshare_layer_index);
-
-    IPC::ResponseBuilder rb{ctx, 4};
-    rb.Push(res);
-    rb.PushRaw(params);
-}
-
-void IDisplayController::ReleaseCallerAppletCaptureSharedBuffer(HLERequestContext& ctx) {
-    LOG_WARNING(Service_AM, "(STUBBED) called");
-
-    IPC::ResponseBuilder rb{ctx, 2};
-    rb.Push(ResultSuccess);
-}
-
-} // namespace Service::AM
diff --git a/src/core/hle/service/am/display_controller.h b/src/core/hle/service/am/display_controller.h
deleted file mode 100644
index 75172580c..000000000
--- a/src/core/hle/service/am/display_controller.h
+++ /dev/null
@@ -1,30 +0,0 @@
-// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
-// SPDX-License-Identifier: GPL-2.0-or-later
-
-#pragma once
-
-#include "core/hle/service/service.h"
-
-namespace Service::AM {
-
-struct Applet;
-
-class IDisplayController final : public ServiceFramework<IDisplayController> {
-public:
-    explicit IDisplayController(Core::System& system_, std::shared_ptr<Applet> applet_);
-    ~IDisplayController() override;
-
-private:
-    void GetCallerAppletCaptureImageEx(HLERequestContext& ctx);
-    void TakeScreenShotOfOwnLayer(HLERequestContext& ctx);
-    void AcquireLastForegroundCaptureSharedBuffer(HLERequestContext& ctx);
-    void ReleaseLastForegroundCaptureSharedBuffer(HLERequestContext& ctx);
-    void AcquireCallerAppletCaptureSharedBuffer(HLERequestContext& ctx);
-    void ReleaseCallerAppletCaptureSharedBuffer(HLERequestContext& ctx);
-    void AcquireLastApplicationCaptureSharedBuffer(HLERequestContext& ctx);
-    void ReleaseLastApplicationCaptureSharedBuffer(HLERequestContext& ctx);
-
-    const std::shared_ptr<Applet> applet;
-};
-
-} // namespace Service::AM
diff --git a/src/core/hle/service/am/service/application_proxy.cpp b/src/core/hle/service/am/service/application_proxy.cpp
index 6cd6df2d0..445f9b158 100644
--- a/src/core/hle/service/am/service/application_proxy.cpp
+++ b/src/core/hle/service/am/service/application_proxy.cpp
@@ -1,7 +1,6 @@
 // SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
 // SPDX-License-Identifier: GPL-2.0-or-later
 
-#include "core/hle/service/am/display_controller.h"
 #include "core/hle/service/am/library_applet_creator.h"
 #include "core/hle/service/am/library_applet_self_accessor.h"
 #include "core/hle/service/am/process_winding_controller.h"
@@ -12,6 +11,7 @@
 #include "core/hle/service/am/service/audio_controller.h"
 #include "core/hle/service/am/service/common_state_getter.h"
 #include "core/hle/service/am/service/debug_functions.h"
+#include "core/hle/service/am/service/display_controller.h"
 #include "core/hle/service/am/window_controller.h"
 #include "core/hle/service/cmif_serialization.h"
 
diff --git a/src/core/hle/service/am/service/display_controller.cpp b/src/core/hle/service/am/service/display_controller.cpp
new file mode 100644
index 000000000..249c73dfb
--- /dev/null
+++ b/src/core/hle/service/am/service/display_controller.cpp
@@ -0,0 +1,105 @@
+// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "core/hle/result.h"
+#include "core/hle/service/am/applet.h"
+#include "core/hle/service/am/service/display_controller.h"
+#include "core/hle/service/cmif_serialization.h"
+
+namespace Service::AM {
+
+IDisplayController::IDisplayController(Core::System& system_, std::shared_ptr<Applet> applet_)
+    : ServiceFramework{system_, "IDisplayController"}, applet(std::move(applet_)) {
+    // clang-format off
+    static const FunctionInfo functions[] = {
+        {0, nullptr, "GetLastForegroundCaptureImage"},
+        {1, nullptr, "UpdateLastForegroundCaptureImage"},
+        {2, nullptr, "GetLastApplicationCaptureImage"},
+        {3, nullptr, "GetCallerAppletCaptureImage"},
+        {4, nullptr, "UpdateCallerAppletCaptureImage"},
+        {5, nullptr, "GetLastForegroundCaptureImageEx"},
+        {6, nullptr, "GetLastApplicationCaptureImageEx"},
+        {7, D<&IDisplayController::GetCallerAppletCaptureImageEx>, "GetCallerAppletCaptureImageEx"},
+        {8, D<&IDisplayController::TakeScreenShotOfOwnLayer>, "TakeScreenShotOfOwnLayer"},
+        {9, nullptr, "CopyBetweenCaptureBuffers"},
+        {10, nullptr, "AcquireLastApplicationCaptureBuffer"},
+        {11, nullptr, "ReleaseLastApplicationCaptureBuffer"},
+        {12, nullptr, "AcquireLastForegroundCaptureBuffer"},
+        {13, nullptr, "ReleaseLastForegroundCaptureBuffer"},
+        {14, nullptr, "AcquireCallerAppletCaptureBuffer"},
+        {15, nullptr, "ReleaseCallerAppletCaptureBuffer"},
+        {16, nullptr, "AcquireLastApplicationCaptureBufferEx"},
+        {17, nullptr, "AcquireLastForegroundCaptureBufferEx"},
+        {18, nullptr, "AcquireCallerAppletCaptureBufferEx"},
+        {20, D<&IDisplayController::ClearCaptureBuffer>, "ClearCaptureBuffer"},
+        {21, nullptr, "ClearAppletTransitionBuffer"},
+        {22, D<&IDisplayController::AcquireLastApplicationCaptureSharedBuffer>, "AcquireLastApplicationCaptureSharedBuffer"},
+        {23, D<&IDisplayController::ReleaseLastApplicationCaptureSharedBuffer>, "ReleaseLastApplicationCaptureSharedBuffer"},
+        {24, D<&IDisplayController::AcquireLastForegroundCaptureSharedBuffer>, "AcquireLastForegroundCaptureSharedBuffer"},
+        {25, D<&IDisplayController::ReleaseLastForegroundCaptureSharedBuffer>, "ReleaseLastForegroundCaptureSharedBuffer"},
+        {26, D<&IDisplayController::AcquireCallerAppletCaptureSharedBuffer>, "AcquireCallerAppletCaptureSharedBuffer"},
+        {27, D<&IDisplayController::ReleaseCallerAppletCaptureSharedBuffer>, "ReleaseCallerAppletCaptureSharedBuffer"},
+        {28, nullptr, "TakeScreenShotOfOwnLayerEx"},
+    };
+    // clang-format on
+
+    RegisterHandlers(functions);
+}
+
+IDisplayController::~IDisplayController() = default;
+
+Result IDisplayController::GetCallerAppletCaptureImageEx(
+    Out<bool> out_was_written, OutBuffer<BufferAttr_HipcMapAlias> out_image_data) {
+    LOG_WARNING(Service_AM, "(STUBBED) called");
+    *out_was_written = true;
+    R_SUCCEED();
+}
+
+Result IDisplayController::TakeScreenShotOfOwnLayer(bool unknown0, s32 fbshare_layer_index) {
+    LOG_WARNING(Service_AM, "(STUBBED) called");
+    R_SUCCEED();
+}
+
+Result IDisplayController::ClearCaptureBuffer(bool unknown0, s32 fbshare_layer_index, u32 color) {
+    LOG_WARNING(Service_AM, "(STUBBED) called, unknown0={} fbshare_layer_index={} color={:#x}",
+                unknown0, fbshare_layer_index, color);
+    R_SUCCEED();
+}
+
+Result IDisplayController::AcquireLastForegroundCaptureSharedBuffer(
+    Out<bool> out_was_written, Out<s32> out_fbshare_layer_index) {
+    LOG_WARNING(Service_AM, "(STUBBED) called");
+    R_RETURN(applet->system_buffer_manager.WriteAppletCaptureBuffer(out_was_written,
+                                                                    out_fbshare_layer_index));
+}
+
+Result IDisplayController::ReleaseLastForegroundCaptureSharedBuffer() {
+    LOG_WARNING(Service_AM, "(STUBBED) called");
+    R_SUCCEED();
+}
+
+Result IDisplayController::AcquireCallerAppletCaptureSharedBuffer(
+    Out<bool> out_was_written, Out<s32> out_fbshare_layer_index) {
+    LOG_WARNING(Service_AM, "(STUBBED) called");
+    R_RETURN(applet->system_buffer_manager.WriteAppletCaptureBuffer(out_was_written,
+                                                                    out_fbshare_layer_index));
+}
+
+Result IDisplayController::ReleaseCallerAppletCaptureSharedBuffer() {
+    LOG_WARNING(Service_AM, "(STUBBED) called");
+    R_SUCCEED();
+}
+
+Result IDisplayController::AcquireLastApplicationCaptureSharedBuffer(
+    Out<bool> out_was_written, Out<s32> out_fbshare_layer_index) {
+    LOG_WARNING(Service_AM, "(STUBBED) called");
+    R_RETURN(applet->system_buffer_manager.WriteAppletCaptureBuffer(out_was_written,
+                                                                    out_fbshare_layer_index));
+}
+
+Result IDisplayController::ReleaseLastApplicationCaptureSharedBuffer() {
+    LOG_WARNING(Service_AM, "(STUBBED) called");
+    R_SUCCEED();
+}
+
+} // namespace Service::AM
diff --git a/src/core/hle/service/am/service/display_controller.h b/src/core/hle/service/am/service/display_controller.h
new file mode 100644
index 000000000..406fae21a
--- /dev/null
+++ b/src/core/hle/service/am/service/display_controller.h
@@ -0,0 +1,36 @@
+// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#include "core/hle/service/cmif_types.h"
+#include "core/hle/service/service.h"
+
+namespace Service::AM {
+
+struct Applet;
+
+class IDisplayController final : public ServiceFramework<IDisplayController> {
+public:
+    explicit IDisplayController(Core::System& system_, std::shared_ptr<Applet> applet_);
+    ~IDisplayController() override;
+
+private:
+    Result GetCallerAppletCaptureImageEx(Out<bool> out_was_written,
+                                         OutBuffer<BufferAttr_HipcMapAlias> out_image_data);
+    Result TakeScreenShotOfOwnLayer(bool unknown0, s32 fbshare_layer_index);
+    Result ClearCaptureBuffer(bool unknown0, s32 fbshare_layer_index, u32 color);
+    Result AcquireLastForegroundCaptureSharedBuffer(Out<bool> out_was_written,
+                                                    Out<s32> out_fbshare_layer_index);
+    Result ReleaseLastForegroundCaptureSharedBuffer();
+    Result AcquireCallerAppletCaptureSharedBuffer(Out<bool> out_was_written,
+                                                  Out<s32> out_fbshare_layer_index);
+    Result ReleaseCallerAppletCaptureSharedBuffer();
+    Result AcquireLastApplicationCaptureSharedBuffer(Out<bool> out_was_written,
+                                                     Out<s32> out_fbshare_layer_index);
+    Result ReleaseLastApplicationCaptureSharedBuffer();
+
+    const std::shared_ptr<Applet> applet;
+};
+
+} // namespace Service::AM
diff --git a/src/core/hle/service/am/service/library_applet_proxy.cpp b/src/core/hle/service/am/service/library_applet_proxy.cpp
index d609871fb..589d53c20 100644
--- a/src/core/hle/service/am/service/library_applet_proxy.cpp
+++ b/src/core/hle/service/am/service/library_applet_proxy.cpp
@@ -1,7 +1,6 @@
 // SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
 // SPDX-License-Identifier: GPL-2.0-or-later
 
-#include "core/hle/service/am/display_controller.h"
 #include "core/hle/service/am/global_state_controller.h"
 #include "core/hle/service/am/home_menu_functions.h"
 #include "core/hle/service/am/library_applet_creator.h"
@@ -12,6 +11,7 @@
 #include "core/hle/service/am/service/audio_controller.h"
 #include "core/hle/service/am/service/common_state_getter.h"
 #include "core/hle/service/am/service/debug_functions.h"
+#include "core/hle/service/am/service/display_controller.h"
 #include "core/hle/service/am/service/library_applet_proxy.h"
 #include "core/hle/service/am/window_controller.h"
 #include "core/hle/service/cmif_serialization.h"
diff --git a/src/core/hle/service/am/service/system_applet_proxy.cpp b/src/core/hle/service/am/service/system_applet_proxy.cpp
index d4ebe3ae0..4bfbd1067 100644
--- a/src/core/hle/service/am/service/system_applet_proxy.cpp
+++ b/src/core/hle/service/am/service/system_applet_proxy.cpp
@@ -2,7 +2,6 @@
 // SPDX-License-Identifier: GPL-2.0-or-later
 
 #include "core/hle/service/am/application_creator.h"
-#include "core/hle/service/am/display_controller.h"
 #include "core/hle/service/am/global_state_controller.h"
 #include "core/hle/service/am/home_menu_functions.h"
 #include "core/hle/service/am/library_applet_creator.h"
@@ -13,6 +12,7 @@
 #include "core/hle/service/am/service/audio_controller.h"
 #include "core/hle/service/am/service/common_state_getter.h"
 #include "core/hle/service/am/service/debug_functions.h"
+#include "core/hle/service/am/service/display_controller.h"
 #include "core/hle/service/am/service/system_applet_proxy.h"
 #include "core/hle/service/am/window_controller.h"
 #include "core/hle/service/cmif_serialization.h"