vi.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <array>
  5. #include <cstring>
  6. #include <memory>
  7. #include <optional>
  8. #include <type_traits>
  9. #include <utility>
  10. #include "common/alignment.h"
  11. #include "common/assert.h"
  12. #include "common/common_funcs.h"
  13. #include "common/logging/log.h"
  14. #include "common/math_util.h"
  15. #include "common/settings.h"
  16. #include "common/swap.h"
  17. #include "core/core_timing.h"
  18. #include "core/hle/kernel/k_readable_event.h"
  19. #include "core/hle/kernel/k_thread.h"
  20. #include "core/hle/service/ipc_helpers.h"
  21. #include "core/hle/service/nvdrv/nvdata.h"
  22. #include "core/hle/service/nvnflinger/binder.h"
  23. #include "core/hle/service/nvnflinger/buffer_queue_producer.h"
  24. #include "core/hle/service/nvnflinger/hos_binder_driver_server.h"
  25. #include "core/hle/service/nvnflinger/nvnflinger.h"
  26. #include "core/hle/service/nvnflinger/parcel.h"
  27. #include "core/hle/service/server_manager.h"
  28. #include "core/hle/service/service.h"
  29. #include "core/hle/service/vi/vi.h"
  30. #include "core/hle/service/vi/vi_m.h"
  31. #include "core/hle/service/vi/vi_results.h"
  32. #include "core/hle/service/vi/vi_s.h"
  33. #include "core/hle/service/vi/vi_u.h"
  34. namespace Service::VI {
  35. struct DisplayInfo {
  36. /// The name of this particular display.
  37. char display_name[0x40]{"Default"};
  38. /// Whether or not the display has a limited number of layers.
  39. u8 has_limited_layers{1};
  40. INSERT_PADDING_BYTES(7);
  41. /// Indicates the total amount of layers supported by the display.
  42. /// @note This is only valid if has_limited_layers is set.
  43. u64 max_layers{1};
  44. /// Maximum width in pixels.
  45. u64 width{1920};
  46. /// Maximum height in pixels.
  47. u64 height{1080};
  48. };
  49. static_assert(sizeof(DisplayInfo) == 0x60, "DisplayInfo has wrong size");
  50. class NativeWindow final {
  51. public:
  52. constexpr explicit NativeWindow(u32 id_) : id{id_} {}
  53. constexpr explicit NativeWindow(const NativeWindow& other) = default;
  54. private:
  55. const u32 magic = 2;
  56. const u32 process_id = 1;
  57. const u64 id;
  58. INSERT_PADDING_WORDS(2);
  59. std::array<u8, 8> dispdrv = {'d', 'i', 's', 'p', 'd', 'r', 'v', '\0'};
  60. INSERT_PADDING_WORDS(2);
  61. };
  62. static_assert(sizeof(NativeWindow) == 0x28, "NativeWindow has wrong size");
  63. class IHOSBinderDriver final : public ServiceFramework<IHOSBinderDriver> {
  64. public:
  65. explicit IHOSBinderDriver(Core::System& system_, Nvnflinger::HosBinderDriverServer& server_)
  66. : ServiceFramework{system_, "IHOSBinderDriver"}, server(server_) {
  67. static const FunctionInfo functions[] = {
  68. {0, &IHOSBinderDriver::TransactParcel, "TransactParcel"},
  69. {1, &IHOSBinderDriver::AdjustRefcount, "AdjustRefcount"},
  70. {2, &IHOSBinderDriver::GetNativeHandle, "GetNativeHandle"},
  71. {3, &IHOSBinderDriver::TransactParcel, "TransactParcelAuto"},
  72. };
  73. RegisterHandlers(functions);
  74. }
  75. private:
  76. void TransactParcel(HLERequestContext& ctx) {
  77. IPC::RequestParser rp{ctx};
  78. const u32 id = rp.Pop<u32>();
  79. const auto transaction = static_cast<android::TransactionId>(rp.Pop<u32>());
  80. const u32 flags = rp.Pop<u32>();
  81. LOG_DEBUG(Service_VI, "called. id=0x{:08X} transaction={:X}, flags=0x{:08X}", id,
  82. transaction, flags);
  83. server.TryGetProducer(id)->Transact(ctx, transaction, flags);
  84. IPC::ResponseBuilder rb{ctx, 2};
  85. rb.Push(ResultSuccess);
  86. }
  87. void AdjustRefcount(HLERequestContext& ctx) {
  88. IPC::RequestParser rp{ctx};
  89. const u32 id = rp.Pop<u32>();
  90. const s32 addval = rp.PopRaw<s32>();
  91. const u32 type = rp.Pop<u32>();
  92. LOG_WARNING(Service_VI, "(STUBBED) called id={}, addval={:08X}, type={:08X}", id, addval,
  93. type);
  94. IPC::ResponseBuilder rb{ctx, 2};
  95. rb.Push(ResultSuccess);
  96. }
  97. void GetNativeHandle(HLERequestContext& ctx) {
  98. IPC::RequestParser rp{ctx};
  99. const u32 id = rp.Pop<u32>();
  100. const u32 unknown = rp.Pop<u32>();
  101. LOG_WARNING(Service_VI, "(STUBBED) called id={}, unknown={:08X}", id, unknown);
  102. IPC::ResponseBuilder rb{ctx, 2, 1};
  103. rb.Push(ResultSuccess);
  104. rb.PushCopyObjects(server.TryGetProducer(id)->GetNativeHandle());
  105. }
  106. private:
  107. Nvnflinger::HosBinderDriverServer& server;
  108. };
  109. class ISystemDisplayService final : public ServiceFramework<ISystemDisplayService> {
  110. public:
  111. explicit ISystemDisplayService(Core::System& system_)
  112. : ServiceFramework{system_, "ISystemDisplayService"} {
  113. static const FunctionInfo functions[] = {
  114. {1200, nullptr, "GetZOrderCountMin"},
  115. {1202, nullptr, "GetZOrderCountMax"},
  116. {1203, nullptr, "GetDisplayLogicalResolution"},
  117. {1204, nullptr, "SetDisplayMagnification"},
  118. {2201, nullptr, "SetLayerPosition"},
  119. {2203, nullptr, "SetLayerSize"},
  120. {2204, nullptr, "GetLayerZ"},
  121. {2205, &ISystemDisplayService::SetLayerZ, "SetLayerZ"},
  122. {2207, &ISystemDisplayService::SetLayerVisibility, "SetLayerVisibility"},
  123. {2209, nullptr, "SetLayerAlpha"},
  124. {2210, nullptr, "SetLayerPositionAndSize"},
  125. {2312, nullptr, "CreateStrayLayer"},
  126. {2400, nullptr, "OpenIndirectLayer"},
  127. {2401, nullptr, "CloseIndirectLayer"},
  128. {2402, nullptr, "FlipIndirectLayer"},
  129. {3000, nullptr, "ListDisplayModes"},
  130. {3001, nullptr, "ListDisplayRgbRanges"},
  131. {3002, nullptr, "ListDisplayContentTypes"},
  132. {3200, &ISystemDisplayService::GetDisplayMode, "GetDisplayMode"},
  133. {3201, nullptr, "SetDisplayMode"},
  134. {3202, nullptr, "GetDisplayUnderscan"},
  135. {3203, nullptr, "SetDisplayUnderscan"},
  136. {3204, nullptr, "GetDisplayContentType"},
  137. {3205, nullptr, "SetDisplayContentType"},
  138. {3206, nullptr, "GetDisplayRgbRange"},
  139. {3207, nullptr, "SetDisplayRgbRange"},
  140. {3208, nullptr, "GetDisplayCmuMode"},
  141. {3209, nullptr, "SetDisplayCmuMode"},
  142. {3210, nullptr, "GetDisplayContrastRatio"},
  143. {3211, nullptr, "SetDisplayContrastRatio"},
  144. {3214, nullptr, "GetDisplayGamma"},
  145. {3215, nullptr, "SetDisplayGamma"},
  146. {3216, nullptr, "GetDisplayCmuLuma"},
  147. {3217, nullptr, "SetDisplayCmuLuma"},
  148. {3218, nullptr, "SetDisplayCrcMode"},
  149. {6013, nullptr, "GetLayerPresentationSubmissionTimestamps"},
  150. {8225, nullptr, "GetSharedBufferMemoryHandleId"},
  151. {8250, nullptr, "OpenSharedLayer"},
  152. {8251, nullptr, "CloseSharedLayer"},
  153. {8252, nullptr, "ConnectSharedLayer"},
  154. {8253, nullptr, "DisconnectSharedLayer"},
  155. {8254, nullptr, "AcquireSharedFrameBuffer"},
  156. {8255, nullptr, "PresentSharedFrameBuffer"},
  157. {8256, nullptr, "GetSharedFrameBufferAcquirableEvent"},
  158. {8257, nullptr, "FillSharedFrameBufferColor"},
  159. {8258, nullptr, "CancelSharedFrameBuffer"},
  160. {9000, nullptr, "GetDp2hdmiController"},
  161. };
  162. RegisterHandlers(functions);
  163. }
  164. private:
  165. void SetLayerZ(HLERequestContext& ctx) {
  166. IPC::RequestParser rp{ctx};
  167. const u64 layer_id = rp.Pop<u64>();
  168. const u64 z_value = rp.Pop<u64>();
  169. LOG_WARNING(Service_VI, "(STUBBED) called. layer_id=0x{:016X}, z_value=0x{:016X}", layer_id,
  170. z_value);
  171. IPC::ResponseBuilder rb{ctx, 2};
  172. rb.Push(ResultSuccess);
  173. }
  174. // This function currently does nothing but return a success error code in
  175. // the vi library itself, so do the same thing, but log out the passed in values.
  176. void SetLayerVisibility(HLERequestContext& ctx) {
  177. IPC::RequestParser rp{ctx};
  178. const u64 layer_id = rp.Pop<u64>();
  179. const bool visibility = rp.Pop<bool>();
  180. LOG_DEBUG(Service_VI, "called, layer_id=0x{:08X}, visibility={}", layer_id, visibility);
  181. IPC::ResponseBuilder rb{ctx, 2};
  182. rb.Push(ResultSuccess);
  183. }
  184. void GetDisplayMode(HLERequestContext& ctx) {
  185. LOG_WARNING(Service_VI, "(STUBBED) called");
  186. IPC::ResponseBuilder rb{ctx, 6};
  187. rb.Push(ResultSuccess);
  188. if (Settings::values.use_docked_mode.GetValue() == Settings::ConsoleMode::Docked) {
  189. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::DockedWidth));
  190. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::DockedHeight));
  191. } else {
  192. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::UndockedWidth));
  193. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::UndockedHeight));
  194. }
  195. rb.PushRaw<float>(60.0f); // This wouldn't seem to be correct for 30 fps games.
  196. rb.Push<u32>(0);
  197. }
  198. };
  199. class IManagerDisplayService final : public ServiceFramework<IManagerDisplayService> {
  200. public:
  201. explicit IManagerDisplayService(Core::System& system_, Nvnflinger::Nvnflinger& nv_flinger_)
  202. : ServiceFramework{system_, "IManagerDisplayService"}, nv_flinger{nv_flinger_} {
  203. // clang-format off
  204. static const FunctionInfo functions[] = {
  205. {200, nullptr, "AllocateProcessHeapBlock"},
  206. {201, nullptr, "FreeProcessHeapBlock"},
  207. {1020, &IManagerDisplayService::CloseDisplay, "CloseDisplay"},
  208. {1102, nullptr, "GetDisplayResolution"},
  209. {2010, &IManagerDisplayService::CreateManagedLayer, "CreateManagedLayer"},
  210. {2011, nullptr, "DestroyManagedLayer"},
  211. {2012, nullptr, "CreateStrayLayer"},
  212. {2050, nullptr, "CreateIndirectLayer"},
  213. {2051, nullptr, "DestroyIndirectLayer"},
  214. {2052, nullptr, "CreateIndirectProducerEndPoint"},
  215. {2053, nullptr, "DestroyIndirectProducerEndPoint"},
  216. {2054, nullptr, "CreateIndirectConsumerEndPoint"},
  217. {2055, nullptr, "DestroyIndirectConsumerEndPoint"},
  218. {2060, nullptr, "CreateWatermarkCompositor"},
  219. {2062, nullptr, "SetWatermarkText"},
  220. {2063, nullptr, "SetWatermarkLayerStacks"},
  221. {2300, nullptr, "AcquireLayerTexturePresentingEvent"},
  222. {2301, nullptr, "ReleaseLayerTexturePresentingEvent"},
  223. {2302, nullptr, "GetDisplayHotplugEvent"},
  224. {2303, nullptr, "GetDisplayModeChangedEvent"},
  225. {2402, nullptr, "GetDisplayHotplugState"},
  226. {2501, nullptr, "GetCompositorErrorInfo"},
  227. {2601, nullptr, "GetDisplayErrorEvent"},
  228. {2701, nullptr, "GetDisplayFatalErrorEvent"},
  229. {4201, nullptr, "SetDisplayAlpha"},
  230. {4203, nullptr, "SetDisplayLayerStack"},
  231. {4205, nullptr, "SetDisplayPowerState"},
  232. {4206, nullptr, "SetDefaultDisplay"},
  233. {4207, nullptr, "ResetDisplayPanel"},
  234. {4208, nullptr, "SetDisplayFatalErrorEnabled"},
  235. {4209, nullptr, "IsDisplayPanelOn"},
  236. {4300, nullptr, "GetInternalPanelId"},
  237. {6000, &IManagerDisplayService::AddToLayerStack, "AddToLayerStack"},
  238. {6001, nullptr, "RemoveFromLayerStack"},
  239. {6002, &IManagerDisplayService::SetLayerVisibility, "SetLayerVisibility"},
  240. {6003, nullptr, "SetLayerConfig"},
  241. {6004, nullptr, "AttachLayerPresentationTracer"},
  242. {6005, nullptr, "DetachLayerPresentationTracer"},
  243. {6006, nullptr, "StartLayerPresentationRecording"},
  244. {6007, nullptr, "StopLayerPresentationRecording"},
  245. {6008, nullptr, "StartLayerPresentationFenceWait"},
  246. {6009, nullptr, "StopLayerPresentationFenceWait"},
  247. {6010, nullptr, "GetLayerPresentationAllFencesExpiredEvent"},
  248. {6011, nullptr, "EnableLayerAutoClearTransitionBuffer"},
  249. {6012, nullptr, "DisableLayerAutoClearTransitionBuffer"},
  250. {6013, nullptr, "SetLayerOpacity"},
  251. {6014, nullptr, "AttachLayerWatermarkCompositor"},
  252. {6015, nullptr, "DetachLayerWatermarkCompositor"},
  253. {7000, nullptr, "SetContentVisibility"},
  254. {8000, nullptr, "SetConductorLayer"},
  255. {8001, nullptr, "SetTimestampTracking"},
  256. {8100, nullptr, "SetIndirectProducerFlipOffset"},
  257. {8200, nullptr, "CreateSharedBufferStaticStorage"},
  258. {8201, nullptr, "CreateSharedBufferTransferMemory"},
  259. {8202, nullptr, "DestroySharedBuffer"},
  260. {8203, nullptr, "BindSharedLowLevelLayerToManagedLayer"},
  261. {8204, nullptr, "BindSharedLowLevelLayerToIndirectLayer"},
  262. {8207, nullptr, "UnbindSharedLowLevelLayer"},
  263. {8208, nullptr, "ConnectSharedLowLevelLayerToSharedBuffer"},
  264. {8209, nullptr, "DisconnectSharedLowLevelLayerFromSharedBuffer"},
  265. {8210, nullptr, "CreateSharedLayer"},
  266. {8211, nullptr, "DestroySharedLayer"},
  267. {8216, nullptr, "AttachSharedLayerToLowLevelLayer"},
  268. {8217, nullptr, "ForceDetachSharedLayerFromLowLevelLayer"},
  269. {8218, nullptr, "StartDetachSharedLayerFromLowLevelLayer"},
  270. {8219, nullptr, "FinishDetachSharedLayerFromLowLevelLayer"},
  271. {8220, nullptr, "GetSharedLayerDetachReadyEvent"},
  272. {8221, nullptr, "GetSharedLowLevelLayerSynchronizedEvent"},
  273. {8222, nullptr, "CheckSharedLowLevelLayerSynchronized"},
  274. {8223, nullptr, "RegisterSharedBufferImporterAruid"},
  275. {8224, nullptr, "UnregisterSharedBufferImporterAruid"},
  276. {8227, nullptr, "CreateSharedBufferProcessHeap"},
  277. {8228, nullptr, "GetSharedLayerLayerStacks"},
  278. {8229, nullptr, "SetSharedLayerLayerStacks"},
  279. {8291, nullptr, "PresentDetachedSharedFrameBufferToLowLevelLayer"},
  280. {8292, nullptr, "FillDetachedSharedFrameBufferColor"},
  281. {8293, nullptr, "GetDetachedSharedFrameBufferImage"},
  282. {8294, nullptr, "SetDetachedSharedFrameBufferImage"},
  283. {8295, nullptr, "CopyDetachedSharedFrameBufferImage"},
  284. {8296, nullptr, "SetDetachedSharedFrameBufferSubImage"},
  285. {8297, nullptr, "GetSharedFrameBufferContentParameter"},
  286. {8298, nullptr, "ExpandStartupLogoOnSharedFrameBuffer"},
  287. };
  288. // clang-format on
  289. RegisterHandlers(functions);
  290. }
  291. private:
  292. void CloseDisplay(HLERequestContext& ctx) {
  293. IPC::RequestParser rp{ctx};
  294. const u64 display = rp.Pop<u64>();
  295. const Result rc = nv_flinger.CloseDisplay(display) ? ResultSuccess : ResultUnknown;
  296. IPC::ResponseBuilder rb{ctx, 2};
  297. rb.Push(rc);
  298. }
  299. void CreateManagedLayer(HLERequestContext& ctx) {
  300. IPC::RequestParser rp{ctx};
  301. const u32 unknown = rp.Pop<u32>();
  302. rp.Skip(1, false);
  303. const u64 display = rp.Pop<u64>();
  304. const u64 aruid = rp.Pop<u64>();
  305. LOG_WARNING(Service_VI,
  306. "(STUBBED) called. unknown=0x{:08X}, display=0x{:016X}, aruid=0x{:016X}",
  307. unknown, display, aruid);
  308. const auto layer_id = nv_flinger.CreateLayer(display);
  309. if (!layer_id) {
  310. LOG_ERROR(Service_VI, "Layer not found! display=0x{:016X}", display);
  311. IPC::ResponseBuilder rb{ctx, 2};
  312. rb.Push(ResultNotFound);
  313. return;
  314. }
  315. IPC::ResponseBuilder rb{ctx, 4};
  316. rb.Push(ResultSuccess);
  317. rb.Push(*layer_id);
  318. }
  319. void AddToLayerStack(HLERequestContext& ctx) {
  320. IPC::RequestParser rp{ctx};
  321. const u32 stack = rp.Pop<u32>();
  322. const u64 layer_id = rp.Pop<u64>();
  323. LOG_WARNING(Service_VI, "(STUBBED) called. stack=0x{:08X}, layer_id=0x{:016X}", stack,
  324. layer_id);
  325. IPC::ResponseBuilder rb{ctx, 2};
  326. rb.Push(ResultSuccess);
  327. }
  328. void SetLayerVisibility(HLERequestContext& ctx) {
  329. IPC::RequestParser rp{ctx};
  330. const u64 layer_id = rp.Pop<u64>();
  331. const bool visibility = rp.Pop<bool>();
  332. LOG_WARNING(Service_VI, "(STUBBED) called, layer_id=0x{:X}, visibility={}", layer_id,
  333. visibility);
  334. IPC::ResponseBuilder rb{ctx, 2};
  335. rb.Push(ResultSuccess);
  336. }
  337. Nvnflinger::Nvnflinger& nv_flinger;
  338. };
  339. class IApplicationDisplayService final : public ServiceFramework<IApplicationDisplayService> {
  340. public:
  341. IApplicationDisplayService(Core::System& system_, Nvnflinger::Nvnflinger& nv_flinger_,
  342. Nvnflinger::HosBinderDriverServer& hos_binder_driver_server_)
  343. : ServiceFramework{system_, "IApplicationDisplayService"}, nv_flinger{nv_flinger_},
  344. hos_binder_driver_server{hos_binder_driver_server_} {
  345. static const FunctionInfo functions[] = {
  346. {100, &IApplicationDisplayService::GetRelayService, "GetRelayService"},
  347. {101, &IApplicationDisplayService::GetSystemDisplayService, "GetSystemDisplayService"},
  348. {102, &IApplicationDisplayService::GetManagerDisplayService,
  349. "GetManagerDisplayService"},
  350. {103, &IApplicationDisplayService::GetIndirectDisplayTransactionService,
  351. "GetIndirectDisplayTransactionService"},
  352. {1000, &IApplicationDisplayService::ListDisplays, "ListDisplays"},
  353. {1010, &IApplicationDisplayService::OpenDisplay, "OpenDisplay"},
  354. {1011, &IApplicationDisplayService::OpenDefaultDisplay, "OpenDefaultDisplay"},
  355. {1020, &IApplicationDisplayService::CloseDisplay, "CloseDisplay"},
  356. {1101, &IApplicationDisplayService::SetDisplayEnabled, "SetDisplayEnabled"},
  357. {1102, &IApplicationDisplayService::GetDisplayResolution, "GetDisplayResolution"},
  358. {2020, &IApplicationDisplayService::OpenLayer, "OpenLayer"},
  359. {2021, &IApplicationDisplayService::CloseLayer, "CloseLayer"},
  360. {2030, &IApplicationDisplayService::CreateStrayLayer, "CreateStrayLayer"},
  361. {2031, &IApplicationDisplayService::DestroyStrayLayer, "DestroyStrayLayer"},
  362. {2101, &IApplicationDisplayService::SetLayerScalingMode, "SetLayerScalingMode"},
  363. {2102, &IApplicationDisplayService::ConvertScalingMode, "ConvertScalingMode"},
  364. {2450, &IApplicationDisplayService::GetIndirectLayerImageMap,
  365. "GetIndirectLayerImageMap"},
  366. {2451, nullptr, "GetIndirectLayerImageCropMap"},
  367. {2460, &IApplicationDisplayService::GetIndirectLayerImageRequiredMemoryInfo,
  368. "GetIndirectLayerImageRequiredMemoryInfo"},
  369. {5202, &IApplicationDisplayService::GetDisplayVsyncEvent, "GetDisplayVsyncEvent"},
  370. {5203, nullptr, "GetDisplayVsyncEventForDebug"},
  371. };
  372. RegisterHandlers(functions);
  373. }
  374. private:
  375. enum class ConvertedScaleMode : u64 {
  376. Freeze = 0,
  377. ScaleToWindow = 1,
  378. ScaleAndCrop = 2,
  379. None = 3,
  380. PreserveAspectRatio = 4,
  381. };
  382. enum class NintendoScaleMode : u32 {
  383. None = 0,
  384. Freeze = 1,
  385. ScaleToWindow = 2,
  386. ScaleAndCrop = 3,
  387. PreserveAspectRatio = 4,
  388. };
  389. void GetRelayService(HLERequestContext& ctx) {
  390. LOG_WARNING(Service_VI, "(STUBBED) called");
  391. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  392. rb.Push(ResultSuccess);
  393. rb.PushIpcInterface<IHOSBinderDriver>(system, hos_binder_driver_server);
  394. }
  395. void GetSystemDisplayService(HLERequestContext& ctx) {
  396. LOG_WARNING(Service_VI, "(STUBBED) called");
  397. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  398. rb.Push(ResultSuccess);
  399. rb.PushIpcInterface<ISystemDisplayService>(system);
  400. }
  401. void GetManagerDisplayService(HLERequestContext& ctx) {
  402. LOG_WARNING(Service_VI, "(STUBBED) called");
  403. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  404. rb.Push(ResultSuccess);
  405. rb.PushIpcInterface<IManagerDisplayService>(system, nv_flinger);
  406. }
  407. void GetIndirectDisplayTransactionService(HLERequestContext& ctx) {
  408. LOG_WARNING(Service_VI, "(STUBBED) called");
  409. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  410. rb.Push(ResultSuccess);
  411. rb.PushIpcInterface<IHOSBinderDriver>(system, hos_binder_driver_server);
  412. }
  413. void OpenDisplay(HLERequestContext& ctx) {
  414. LOG_WARNING(Service_VI, "(STUBBED) called");
  415. IPC::RequestParser rp{ctx};
  416. const auto name_buf = rp.PopRaw<std::array<char, 0x40>>();
  417. OpenDisplayImpl(ctx, std::string_view{name_buf.data(), name_buf.size()});
  418. }
  419. void OpenDefaultDisplay(HLERequestContext& ctx) {
  420. LOG_DEBUG(Service_VI, "called");
  421. OpenDisplayImpl(ctx, "Default");
  422. }
  423. void OpenDisplayImpl(HLERequestContext& ctx, std::string_view name) {
  424. const auto trim_pos = name.find('\0');
  425. if (trim_pos != std::string_view::npos) {
  426. name.remove_suffix(name.size() - trim_pos);
  427. }
  428. ASSERT_MSG(name == "Default", "Non-default displays aren't supported yet");
  429. const auto display_id = nv_flinger.OpenDisplay(name);
  430. if (!display_id) {
  431. LOG_ERROR(Service_VI, "Display not found! display_name={}", name);
  432. IPC::ResponseBuilder rb{ctx, 2};
  433. rb.Push(ResultNotFound);
  434. return;
  435. }
  436. IPC::ResponseBuilder rb{ctx, 4};
  437. rb.Push(ResultSuccess);
  438. rb.Push<u64>(*display_id);
  439. }
  440. void CloseDisplay(HLERequestContext& ctx) {
  441. IPC::RequestParser rp{ctx};
  442. const u64 display_id = rp.Pop<u64>();
  443. const Result rc = nv_flinger.CloseDisplay(display_id) ? ResultSuccess : ResultUnknown;
  444. IPC::ResponseBuilder rb{ctx, 2};
  445. rb.Push(rc);
  446. }
  447. // This literally does nothing internally in the actual service itself,
  448. // and just returns a successful result code regardless of the input.
  449. void SetDisplayEnabled(HLERequestContext& ctx) {
  450. LOG_DEBUG(Service_VI, "called.");
  451. IPC::ResponseBuilder rb{ctx, 2};
  452. rb.Push(ResultSuccess);
  453. }
  454. void GetDisplayResolution(HLERequestContext& ctx) {
  455. IPC::RequestParser rp{ctx};
  456. const u64 display_id = rp.Pop<u64>();
  457. LOG_DEBUG(Service_VI, "called. display_id=0x{:016X}", display_id);
  458. IPC::ResponseBuilder rb{ctx, 6};
  459. rb.Push(ResultSuccess);
  460. // This only returns the fixed values of 1280x720 and makes no distinguishing
  461. // between docked and undocked dimensions. We take the liberty of applying
  462. // the resolution scaling factor here.
  463. rb.Push(static_cast<u64>(DisplayResolution::UndockedWidth));
  464. rb.Push(static_cast<u64>(DisplayResolution::UndockedHeight));
  465. }
  466. void SetLayerScalingMode(HLERequestContext& ctx) {
  467. IPC::RequestParser rp{ctx};
  468. const auto scaling_mode = rp.PopEnum<NintendoScaleMode>();
  469. const u64 unknown = rp.Pop<u64>();
  470. LOG_DEBUG(Service_VI, "called. scaling_mode=0x{:08X}, unknown=0x{:016X}", scaling_mode,
  471. unknown);
  472. IPC::ResponseBuilder rb{ctx, 2};
  473. if (scaling_mode > NintendoScaleMode::PreserveAspectRatio) {
  474. LOG_ERROR(Service_VI, "Invalid scaling mode provided.");
  475. rb.Push(ResultOperationFailed);
  476. return;
  477. }
  478. if (scaling_mode != NintendoScaleMode::ScaleToWindow &&
  479. scaling_mode != NintendoScaleMode::PreserveAspectRatio) {
  480. LOG_ERROR(Service_VI, "Unsupported scaling mode supplied.");
  481. rb.Push(ResultNotSupported);
  482. return;
  483. }
  484. rb.Push(ResultSuccess);
  485. }
  486. void ListDisplays(HLERequestContext& ctx) {
  487. LOG_WARNING(Service_VI, "(STUBBED) called");
  488. const DisplayInfo display_info;
  489. ctx.WriteBuffer(&display_info, sizeof(DisplayInfo));
  490. IPC::ResponseBuilder rb{ctx, 4};
  491. rb.Push(ResultSuccess);
  492. rb.Push<u64>(1);
  493. }
  494. void OpenLayer(HLERequestContext& ctx) {
  495. IPC::RequestParser rp{ctx};
  496. const auto name_buf = rp.PopRaw<std::array<u8, 0x40>>();
  497. const auto end = std::find(name_buf.begin(), name_buf.end(), '\0');
  498. const std::string display_name(name_buf.begin(), end);
  499. const u64 layer_id = rp.Pop<u64>();
  500. const u64 aruid = rp.Pop<u64>();
  501. LOG_DEBUG(Service_VI, "called. layer_id=0x{:016X}, aruid=0x{:016X}", layer_id, aruid);
  502. const auto display_id = nv_flinger.OpenDisplay(display_name);
  503. if (!display_id) {
  504. LOG_ERROR(Service_VI, "Layer not found! layer_id={}", layer_id);
  505. IPC::ResponseBuilder rb{ctx, 2};
  506. rb.Push(ResultNotFound);
  507. return;
  508. }
  509. const auto buffer_queue_id = nv_flinger.FindBufferQueueId(*display_id, layer_id);
  510. if (!buffer_queue_id) {
  511. LOG_ERROR(Service_VI, "Buffer queue id not found! display_id={}", *display_id);
  512. IPC::ResponseBuilder rb{ctx, 2};
  513. rb.Push(ResultNotFound);
  514. return;
  515. }
  516. android::OutputParcel parcel;
  517. parcel.WriteInterface(NativeWindow{*buffer_queue_id});
  518. const auto buffer_size = ctx.WriteBuffer(parcel.Serialize());
  519. IPC::ResponseBuilder rb{ctx, 4};
  520. rb.Push(ResultSuccess);
  521. rb.Push<u64>(buffer_size);
  522. }
  523. void CloseLayer(HLERequestContext& ctx) {
  524. IPC::RequestParser rp{ctx};
  525. const auto layer_id{rp.Pop<u64>()};
  526. LOG_DEBUG(Service_VI, "called. layer_id=0x{:016X}", layer_id);
  527. nv_flinger.CloseLayer(layer_id);
  528. IPC::ResponseBuilder rb{ctx, 2};
  529. rb.Push(ResultSuccess);
  530. }
  531. void CreateStrayLayer(HLERequestContext& ctx) {
  532. IPC::RequestParser rp{ctx};
  533. const u32 flags = rp.Pop<u32>();
  534. rp.Pop<u32>(); // padding
  535. const u64 display_id = rp.Pop<u64>();
  536. LOG_DEBUG(Service_VI, "called. flags=0x{:08X}, display_id=0x{:016X}", flags, display_id);
  537. // TODO(Subv): What's the difference between a Stray and a Managed layer?
  538. const auto layer_id = nv_flinger.CreateLayer(display_id);
  539. if (!layer_id) {
  540. LOG_ERROR(Service_VI, "Layer not found! display_id={}", display_id);
  541. IPC::ResponseBuilder rb{ctx, 2};
  542. rb.Push(ResultNotFound);
  543. return;
  544. }
  545. const auto buffer_queue_id = nv_flinger.FindBufferQueueId(display_id, *layer_id);
  546. if (!buffer_queue_id) {
  547. LOG_ERROR(Service_VI, "Buffer queue id not found! display_id={}", display_id);
  548. IPC::ResponseBuilder rb{ctx, 2};
  549. rb.Push(ResultNotFound);
  550. return;
  551. }
  552. android::OutputParcel parcel;
  553. parcel.WriteInterface(NativeWindow{*buffer_queue_id});
  554. const auto buffer_size = ctx.WriteBuffer(parcel.Serialize());
  555. IPC::ResponseBuilder rb{ctx, 6};
  556. rb.Push(ResultSuccess);
  557. rb.Push(*layer_id);
  558. rb.Push<u64>(buffer_size);
  559. }
  560. void DestroyStrayLayer(HLERequestContext& ctx) {
  561. IPC::RequestParser rp{ctx};
  562. const u64 layer_id = rp.Pop<u64>();
  563. LOG_WARNING(Service_VI, "(STUBBED) called. layer_id=0x{:016X}", layer_id);
  564. IPC::ResponseBuilder rb{ctx, 2};
  565. rb.Push(ResultSuccess);
  566. }
  567. void GetDisplayVsyncEvent(HLERequestContext& ctx) {
  568. IPC::RequestParser rp{ctx};
  569. const u64 display_id = rp.Pop<u64>();
  570. LOG_DEBUG(Service_VI, "called. display_id={}", display_id);
  571. Kernel::KReadableEvent* vsync_event{};
  572. const auto result = nv_flinger.FindVsyncEvent(&vsync_event, display_id);
  573. if (result != ResultSuccess) {
  574. if (result == ResultNotFound) {
  575. LOG_ERROR(Service_VI, "Vsync event was not found for display_id={}", display_id);
  576. }
  577. IPC::ResponseBuilder rb{ctx, 2};
  578. rb.Push(result);
  579. return;
  580. }
  581. IPC::ResponseBuilder rb{ctx, 2, 1};
  582. rb.Push(ResultSuccess);
  583. rb.PushCopyObjects(vsync_event);
  584. }
  585. void ConvertScalingMode(HLERequestContext& ctx) {
  586. IPC::RequestParser rp{ctx};
  587. const auto mode = rp.PopEnum<NintendoScaleMode>();
  588. LOG_DEBUG(Service_VI, "called mode={}", mode);
  589. ConvertedScaleMode converted_mode{};
  590. const auto result = ConvertScalingModeImpl(&converted_mode, mode);
  591. if (result == ResultSuccess) {
  592. IPC::ResponseBuilder rb{ctx, 4};
  593. rb.Push(ResultSuccess);
  594. rb.PushEnum(converted_mode);
  595. } else {
  596. IPC::ResponseBuilder rb{ctx, 2};
  597. rb.Push(result);
  598. }
  599. }
  600. void GetIndirectLayerImageMap(HLERequestContext& ctx) {
  601. IPC::RequestParser rp{ctx};
  602. const auto width = rp.Pop<s64>();
  603. const auto height = rp.Pop<s64>();
  604. const auto indirect_layer_consumer_handle = rp.Pop<u64>();
  605. const auto applet_resource_user_id = rp.Pop<u64>();
  606. LOG_WARNING(Service_VI,
  607. "(STUBBED) called, width={}, height={}, indirect_layer_consumer_handle={}, "
  608. "applet_resource_user_id={}",
  609. width, height, indirect_layer_consumer_handle, applet_resource_user_id);
  610. std::vector<u8> out_buffer(0x46);
  611. ctx.WriteBuffer(out_buffer);
  612. // TODO: Figure out what these are
  613. constexpr s64 unknown_result_1 = 0;
  614. constexpr s64 unknown_result_2 = 0;
  615. IPC::ResponseBuilder rb{ctx, 6};
  616. rb.Push(unknown_result_1);
  617. rb.Push(unknown_result_2);
  618. rb.Push(ResultSuccess);
  619. }
  620. void GetIndirectLayerImageRequiredMemoryInfo(HLERequestContext& ctx) {
  621. IPC::RequestParser rp{ctx};
  622. const auto width = rp.Pop<u64>();
  623. const auto height = rp.Pop<u64>();
  624. LOG_DEBUG(Service_VI, "called width={}, height={}", width, height);
  625. constexpr u64 base_size = 0x20000;
  626. constexpr u64 alignment = 0x1000;
  627. const auto texture_size = width * height * 4;
  628. const auto out_size = (texture_size + base_size - 1) / base_size * base_size;
  629. IPC::ResponseBuilder rb{ctx, 6};
  630. rb.Push(ResultSuccess);
  631. rb.Push(out_size);
  632. rb.Push(alignment);
  633. }
  634. static Result ConvertScalingModeImpl(ConvertedScaleMode* out_scaling_mode,
  635. NintendoScaleMode mode) {
  636. switch (mode) {
  637. case NintendoScaleMode::None:
  638. *out_scaling_mode = ConvertedScaleMode::None;
  639. return ResultSuccess;
  640. case NintendoScaleMode::Freeze:
  641. *out_scaling_mode = ConvertedScaleMode::Freeze;
  642. return ResultSuccess;
  643. case NintendoScaleMode::ScaleToWindow:
  644. *out_scaling_mode = ConvertedScaleMode::ScaleToWindow;
  645. return ResultSuccess;
  646. case NintendoScaleMode::ScaleAndCrop:
  647. *out_scaling_mode = ConvertedScaleMode::ScaleAndCrop;
  648. return ResultSuccess;
  649. case NintendoScaleMode::PreserveAspectRatio:
  650. *out_scaling_mode = ConvertedScaleMode::PreserveAspectRatio;
  651. return ResultSuccess;
  652. default:
  653. LOG_ERROR(Service_VI, "Invalid scaling mode specified, mode={}", mode);
  654. return ResultOperationFailed;
  655. }
  656. }
  657. Nvnflinger::Nvnflinger& nv_flinger;
  658. Nvnflinger::HosBinderDriverServer& hos_binder_driver_server;
  659. };
  660. static bool IsValidServiceAccess(Permission permission, Policy policy) {
  661. if (permission == Permission::User) {
  662. return policy == Policy::User;
  663. }
  664. if (permission == Permission::System || permission == Permission::Manager) {
  665. return policy == Policy::User || policy == Policy::Compositor;
  666. }
  667. return false;
  668. }
  669. void detail::GetDisplayServiceImpl(HLERequestContext& ctx, Core::System& system,
  670. Nvnflinger::Nvnflinger& nv_flinger,
  671. Nvnflinger::HosBinderDriverServer& hos_binder_driver_server,
  672. Permission permission) {
  673. IPC::RequestParser rp{ctx};
  674. const auto policy = rp.PopEnum<Policy>();
  675. if (!IsValidServiceAccess(permission, policy)) {
  676. LOG_ERROR(Service_VI, "Permission denied for policy {}", policy);
  677. IPC::ResponseBuilder rb{ctx, 2};
  678. rb.Push(ResultPermissionDenied);
  679. return;
  680. }
  681. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  682. rb.Push(ResultSuccess);
  683. rb.PushIpcInterface<IApplicationDisplayService>(system, nv_flinger, hos_binder_driver_server);
  684. }
  685. void LoopProcess(Core::System& system, Nvnflinger::Nvnflinger& nv_flinger,
  686. Nvnflinger::HosBinderDriverServer& hos_binder_driver_server) {
  687. auto server_manager = std::make_unique<ServerManager>(system);
  688. server_manager->RegisterNamedService(
  689. "vi:m", std::make_shared<VI_M>(system, nv_flinger, hos_binder_driver_server));
  690. server_manager->RegisterNamedService(
  691. "vi:s", std::make_shared<VI_S>(system, nv_flinger, hos_binder_driver_server));
  692. server_manager->RegisterNamedService(
  693. "vi:u", std::make_shared<VI_U>(system, nv_flinger, hos_binder_driver_server));
  694. ServerManager::RunServer(std::move(server_manager));
  695. }
  696. } // namespace Service::VI