vi.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <array>
  6. #include <cstring>
  7. #include <memory>
  8. #include <optional>
  9. #include <type_traits>
  10. #include <utility>
  11. #include "common/alignment.h"
  12. #include "common/assert.h"
  13. #include "common/common_funcs.h"
  14. #include "common/logging/log.h"
  15. #include "common/math_util.h"
  16. #include "common/settings.h"
  17. #include "common/swap.h"
  18. #include "core/core_timing.h"
  19. #include "core/hle/ipc_helpers.h"
  20. #include "core/hle/kernel/k_readable_event.h"
  21. #include "core/hle/kernel/k_thread.h"
  22. #include "core/hle/kernel/k_writable_event.h"
  23. #include "core/hle/service/nvdrv/nvdata.h"
  24. #include "core/hle/service/nvdrv/nvdrv.h"
  25. #include "core/hle/service/nvflinger/buffer_queue.h"
  26. #include "core/hle/service/nvflinger/nvflinger.h"
  27. #include "core/hle/service/service.h"
  28. #include "core/hle/service/vi/vi.h"
  29. #include "core/hle/service/vi/vi_m.h"
  30. #include "core/hle/service/vi/vi_s.h"
  31. #include "core/hle/service/vi/vi_u.h"
  32. namespace Service::VI {
  33. constexpr ResultCode ERR_OPERATION_FAILED{ErrorModule::VI, 1};
  34. constexpr ResultCode ERR_PERMISSION_DENIED{ErrorModule::VI, 5};
  35. constexpr ResultCode ERR_UNSUPPORTED{ErrorModule::VI, 6};
  36. constexpr ResultCode ERR_NOT_FOUND{ErrorModule::VI, 7};
  37. struct DisplayInfo {
  38. /// The name of this particular display.
  39. char display_name[0x40]{"Default"};
  40. /// Whether or not the display has a limited number of layers.
  41. u8 has_limited_layers{1};
  42. INSERT_PADDING_BYTES(7);
  43. /// Indicates the total amount of layers supported by the display.
  44. /// @note This is only valid if has_limited_layers is set.
  45. u64 max_layers{1};
  46. /// Maximum width in pixels.
  47. u64 width{1920};
  48. /// Maximum height in pixels.
  49. u64 height{1080};
  50. };
  51. static_assert(sizeof(DisplayInfo) == 0x60, "DisplayInfo has wrong size");
  52. class Parcel {
  53. public:
  54. // This default size was chosen arbitrarily.
  55. static constexpr std::size_t DefaultBufferSize = 0x40;
  56. Parcel() : buffer(DefaultBufferSize) {}
  57. explicit Parcel(std::vector<u8> data) : buffer(std::move(data)) {}
  58. virtual ~Parcel() = default;
  59. template <typename T>
  60. T Read() {
  61. static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable.");
  62. ASSERT(read_index + sizeof(T) <= buffer.size());
  63. T val;
  64. std::memcpy(&val, buffer.data() + read_index, sizeof(T));
  65. read_index += sizeof(T);
  66. read_index = Common::AlignUp(read_index, 4);
  67. return val;
  68. }
  69. template <typename T>
  70. T ReadUnaligned() {
  71. static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable.");
  72. ASSERT(read_index + sizeof(T) <= buffer.size());
  73. T val;
  74. std::memcpy(&val, buffer.data() + read_index, sizeof(T));
  75. read_index += sizeof(T);
  76. return val;
  77. }
  78. std::vector<u8> ReadBlock(std::size_t length) {
  79. ASSERT(read_index + length <= buffer.size());
  80. const u8* const begin = buffer.data() + read_index;
  81. const u8* const end = begin + length;
  82. std::vector<u8> data(begin, end);
  83. read_index += length;
  84. read_index = Common::AlignUp(read_index, 4);
  85. return data;
  86. }
  87. std::u16string ReadInterfaceToken() {
  88. [[maybe_unused]] const u32 unknown = Read<u32_le>();
  89. const u32 length = Read<u32_le>();
  90. std::u16string token{};
  91. for (u32 ch = 0; ch < length + 1; ++ch) {
  92. token.push_back(ReadUnaligned<u16_le>());
  93. }
  94. read_index = Common::AlignUp(read_index, 4);
  95. return token;
  96. }
  97. template <typename T>
  98. void Write(const T& val) {
  99. static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable.");
  100. if (buffer.size() < write_index + sizeof(T)) {
  101. buffer.resize(buffer.size() + sizeof(T) + DefaultBufferSize);
  102. }
  103. std::memcpy(buffer.data() + write_index, &val, sizeof(T));
  104. write_index += sizeof(T);
  105. write_index = Common::AlignUp(write_index, 4);
  106. }
  107. template <typename T>
  108. void WriteObject(const T& val) {
  109. static_assert(std::is_trivially_copyable_v<T>, "T must be trivially copyable.");
  110. const u32_le size = static_cast<u32>(sizeof(val));
  111. Write(size);
  112. // TODO(Subv): Support file descriptors.
  113. Write<u32_le>(0); // Fd count.
  114. Write(val);
  115. }
  116. void Deserialize() {
  117. ASSERT(buffer.size() > sizeof(Header));
  118. Header header{};
  119. std::memcpy(&header, buffer.data(), sizeof(Header));
  120. read_index = header.data_offset;
  121. DeserializeData();
  122. }
  123. std::vector<u8> Serialize() {
  124. ASSERT(read_index == 0);
  125. write_index = sizeof(Header);
  126. SerializeData();
  127. Header header{};
  128. header.data_size = static_cast<u32_le>(write_index - sizeof(Header));
  129. header.data_offset = sizeof(Header);
  130. header.objects_size = 4;
  131. header.objects_offset = static_cast<u32>(sizeof(Header) + header.data_size);
  132. std::memcpy(buffer.data(), &header, sizeof(Header));
  133. return buffer;
  134. }
  135. protected:
  136. virtual void SerializeData() {}
  137. virtual void DeserializeData() {}
  138. private:
  139. struct Header {
  140. u32_le data_size;
  141. u32_le data_offset;
  142. u32_le objects_size;
  143. u32_le objects_offset;
  144. };
  145. static_assert(sizeof(Header) == 16, "ParcelHeader has wrong size");
  146. std::vector<u8> buffer;
  147. std::size_t read_index = 0;
  148. std::size_t write_index = 0;
  149. };
  150. class NativeWindow : public Parcel {
  151. public:
  152. explicit NativeWindow(u32 id) {
  153. data.id = id;
  154. }
  155. ~NativeWindow() override = default;
  156. protected:
  157. void SerializeData() override {
  158. Write(data);
  159. }
  160. private:
  161. struct Data {
  162. u32_le magic = 2;
  163. u32_le process_id = 1;
  164. u32_le id;
  165. INSERT_PADDING_WORDS(3);
  166. std::array<u8, 8> dispdrv = {'d', 'i', 's', 'p', 'd', 'r', 'v', '\0'};
  167. INSERT_PADDING_WORDS(2);
  168. };
  169. static_assert(sizeof(Data) == 0x28, "ParcelData has wrong size");
  170. Data data{};
  171. };
  172. class IGBPConnectRequestParcel : public Parcel {
  173. public:
  174. explicit IGBPConnectRequestParcel(std::vector<u8> buffer_) : Parcel(std::move(buffer_)) {
  175. Deserialize();
  176. }
  177. void DeserializeData() override {
  178. [[maybe_unused]] const std::u16string token = ReadInterfaceToken();
  179. data = Read<Data>();
  180. }
  181. struct Data {
  182. u32_le unk;
  183. u32_le api;
  184. u32_le producer_controlled_by_app;
  185. };
  186. Data data;
  187. };
  188. class IGBPConnectResponseParcel : public Parcel {
  189. public:
  190. explicit IGBPConnectResponseParcel(u32 width, u32 height) {
  191. data.width = width;
  192. data.height = height;
  193. }
  194. ~IGBPConnectResponseParcel() override = default;
  195. protected:
  196. void SerializeData() override {
  197. Write(data);
  198. }
  199. private:
  200. struct Data {
  201. u32_le width;
  202. u32_le height;
  203. u32_le transform_hint;
  204. u32_le num_pending_buffers;
  205. u32_le status;
  206. };
  207. static_assert(sizeof(Data) == 20, "ParcelData has wrong size");
  208. Data data{};
  209. };
  210. /// Represents a parcel containing one int '0' as its data
  211. /// Used by DetachBuffer and Disconnect
  212. class IGBPEmptyResponseParcel : public Parcel {
  213. protected:
  214. void SerializeData() override {
  215. Write(data);
  216. }
  217. private:
  218. struct Data {
  219. u32_le unk_0{};
  220. };
  221. Data data{};
  222. };
  223. class IGBPSetPreallocatedBufferRequestParcel : public Parcel {
  224. public:
  225. explicit IGBPSetPreallocatedBufferRequestParcel(std::vector<u8> buffer_)
  226. : Parcel(std::move(buffer_)) {
  227. Deserialize();
  228. }
  229. void DeserializeData() override {
  230. [[maybe_unused]] const std::u16string token = ReadInterfaceToken();
  231. data = Read<Data>();
  232. if (data.contains_object != 0) {
  233. buffer_container = Read<BufferContainer>();
  234. }
  235. }
  236. struct Data {
  237. u32_le slot;
  238. u32_le contains_object;
  239. };
  240. struct BufferContainer {
  241. u32_le graphic_buffer_length;
  242. INSERT_PADDING_WORDS(1);
  243. NVFlinger::IGBPBuffer buffer{};
  244. };
  245. Data data{};
  246. BufferContainer buffer_container{};
  247. };
  248. class IGBPSetPreallocatedBufferResponseParcel : public Parcel {
  249. protected:
  250. void SerializeData() override {
  251. // TODO(Subv): Find out what this means
  252. Write<u32>(0);
  253. }
  254. };
  255. class IGBPCancelBufferRequestParcel : public Parcel {
  256. public:
  257. explicit IGBPCancelBufferRequestParcel(std::vector<u8> buffer_) : Parcel(std::move(buffer_)) {
  258. Deserialize();
  259. }
  260. void DeserializeData() override {
  261. [[maybe_unused]] const std::u16string token = ReadInterfaceToken();
  262. data = Read<Data>();
  263. }
  264. struct Data {
  265. u32_le slot;
  266. Service::Nvidia::MultiFence multi_fence;
  267. };
  268. Data data;
  269. };
  270. class IGBPCancelBufferResponseParcel : public Parcel {
  271. protected:
  272. void SerializeData() override {
  273. Write<u32>(0); // Success
  274. }
  275. };
  276. class IGBPDequeueBufferRequestParcel : public Parcel {
  277. public:
  278. explicit IGBPDequeueBufferRequestParcel(std::vector<u8> buffer_) : Parcel(std::move(buffer_)) {
  279. Deserialize();
  280. }
  281. void DeserializeData() override {
  282. [[maybe_unused]] const std::u16string token = ReadInterfaceToken();
  283. data = Read<Data>();
  284. }
  285. struct Data {
  286. u32_le pixel_format;
  287. u32_le width;
  288. u32_le height;
  289. u32_le get_frame_timestamps;
  290. u32_le usage;
  291. };
  292. Data data;
  293. };
  294. class IGBPDequeueBufferResponseParcel : public Parcel {
  295. public:
  296. explicit IGBPDequeueBufferResponseParcel(u32 slot_, Nvidia::MultiFence& multi_fence_)
  297. : slot(slot_), multi_fence(multi_fence_) {}
  298. protected:
  299. void SerializeData() override {
  300. Write(slot);
  301. Write<u32_le>(1);
  302. WriteObject(multi_fence);
  303. Write<u32_le>(0);
  304. }
  305. u32_le slot;
  306. Service::Nvidia::MultiFence multi_fence;
  307. };
  308. class IGBPRequestBufferRequestParcel : public Parcel {
  309. public:
  310. explicit IGBPRequestBufferRequestParcel(std::vector<u8> buffer_) : Parcel(std::move(buffer_)) {
  311. Deserialize();
  312. }
  313. void DeserializeData() override {
  314. [[maybe_unused]] const std::u16string token = ReadInterfaceToken();
  315. slot = Read<u32_le>();
  316. }
  317. u32_le slot;
  318. };
  319. class IGBPRequestBufferResponseParcel : public Parcel {
  320. public:
  321. explicit IGBPRequestBufferResponseParcel(NVFlinger::IGBPBuffer buffer_) : buffer(buffer_) {}
  322. ~IGBPRequestBufferResponseParcel() override = default;
  323. protected:
  324. void SerializeData() override {
  325. // TODO(Subv): Figure out what this value means, writing non-zero here will make libnx
  326. // try to read an IGBPBuffer object from the parcel.
  327. Write<u32_le>(1);
  328. WriteObject(buffer);
  329. Write<u32_le>(0);
  330. }
  331. NVFlinger::IGBPBuffer buffer;
  332. };
  333. class IGBPQueueBufferRequestParcel : public Parcel {
  334. public:
  335. explicit IGBPQueueBufferRequestParcel(std::vector<u8> buffer_) : Parcel(std::move(buffer_)) {
  336. Deserialize();
  337. }
  338. void DeserializeData() override {
  339. [[maybe_unused]] const std::u16string token = ReadInterfaceToken();
  340. data = Read<Data>();
  341. }
  342. struct Data {
  343. u32_le slot;
  344. INSERT_PADDING_WORDS(3);
  345. u32_le timestamp;
  346. s32_le is_auto_timestamp;
  347. s32_le crop_top;
  348. s32_le crop_left;
  349. s32_le crop_right;
  350. s32_le crop_bottom;
  351. s32_le scaling_mode;
  352. NVFlinger::BufferQueue::BufferTransformFlags transform;
  353. u32_le sticky_transform;
  354. INSERT_PADDING_WORDS(1);
  355. u32_le swap_interval;
  356. Service::Nvidia::MultiFence multi_fence;
  357. Common::Rectangle<int> GetCropRect() const {
  358. return {crop_left, crop_top, crop_right, crop_bottom};
  359. }
  360. };
  361. static_assert(sizeof(Data) == 96, "ParcelData has wrong size");
  362. Data data;
  363. };
  364. class IGBPQueueBufferResponseParcel : public Parcel {
  365. public:
  366. explicit IGBPQueueBufferResponseParcel(u32 width, u32 height) {
  367. data.width = width;
  368. data.height = height;
  369. }
  370. ~IGBPQueueBufferResponseParcel() override = default;
  371. protected:
  372. void SerializeData() override {
  373. Write(data);
  374. }
  375. private:
  376. struct Data {
  377. u32_le width;
  378. u32_le height;
  379. u32_le transform_hint;
  380. u32_le num_pending_buffers;
  381. u32_le status;
  382. };
  383. static_assert(sizeof(Data) == 20, "ParcelData has wrong size");
  384. Data data{};
  385. };
  386. class IGBPQueryRequestParcel : public Parcel {
  387. public:
  388. explicit IGBPQueryRequestParcel(std::vector<u8> buffer_) : Parcel(std::move(buffer_)) {
  389. Deserialize();
  390. }
  391. void DeserializeData() override {
  392. [[maybe_unused]] const std::u16string token = ReadInterfaceToken();
  393. type = Read<u32_le>();
  394. }
  395. u32 type;
  396. };
  397. class IGBPQueryResponseParcel : public Parcel {
  398. public:
  399. explicit IGBPQueryResponseParcel(u32 value_) : value{value_} {}
  400. ~IGBPQueryResponseParcel() override = default;
  401. protected:
  402. void SerializeData() override {
  403. Write(value);
  404. }
  405. private:
  406. u32_le value;
  407. };
  408. class IHOSBinderDriver final : public ServiceFramework<IHOSBinderDriver> {
  409. public:
  410. explicit IHOSBinderDriver(Core::System& system_, NVFlinger::NVFlinger& nv_flinger_)
  411. : ServiceFramework{system_, "IHOSBinderDriver"}, nv_flinger(nv_flinger_) {
  412. static const FunctionInfo functions[] = {
  413. {0, &IHOSBinderDriver::TransactParcel, "TransactParcel"},
  414. {1, &IHOSBinderDriver::AdjustRefcount, "AdjustRefcount"},
  415. {2, &IHOSBinderDriver::GetNativeHandle, "GetNativeHandle"},
  416. {3, &IHOSBinderDriver::TransactParcel, "TransactParcelAuto"},
  417. };
  418. RegisterHandlers(functions);
  419. }
  420. private:
  421. enum class TransactionId {
  422. RequestBuffer = 1,
  423. SetBufferCount = 2,
  424. DequeueBuffer = 3,
  425. DetachBuffer = 4,
  426. DetachNextBuffer = 5,
  427. AttachBuffer = 6,
  428. QueueBuffer = 7,
  429. CancelBuffer = 8,
  430. Query = 9,
  431. Connect = 10,
  432. Disconnect = 11,
  433. AllocateBuffers = 13,
  434. SetPreallocatedBuffer = 14
  435. };
  436. void TransactParcel(Kernel::HLERequestContext& ctx) {
  437. IPC::RequestParser rp{ctx};
  438. const u32 id = rp.Pop<u32>();
  439. const auto transaction = static_cast<TransactionId>(rp.Pop<u32>());
  440. const u32 flags = rp.Pop<u32>();
  441. LOG_DEBUG(Service_VI, "called. id=0x{:08X} transaction={:X}, flags=0x{:08X}", id,
  442. transaction, flags);
  443. auto& buffer_queue = *nv_flinger.FindBufferQueue(id);
  444. switch (transaction) {
  445. case TransactionId::Connect: {
  446. IGBPConnectRequestParcel request{ctx.ReadBuffer()};
  447. IGBPConnectResponseParcel response{
  448. static_cast<u32>(static_cast<u32>(DisplayResolution::UndockedWidth) *
  449. Settings::values.resolution_factor.GetValue()),
  450. static_cast<u32>(static_cast<u32>(DisplayResolution::UndockedHeight) *
  451. Settings::values.resolution_factor.GetValue())};
  452. buffer_queue.Connect();
  453. ctx.WriteBuffer(response.Serialize());
  454. break;
  455. }
  456. case TransactionId::SetPreallocatedBuffer: {
  457. IGBPSetPreallocatedBufferRequestParcel request{ctx.ReadBuffer()};
  458. buffer_queue.SetPreallocatedBuffer(request.data.slot, request.buffer_container.buffer);
  459. IGBPSetPreallocatedBufferResponseParcel response{};
  460. ctx.WriteBuffer(response.Serialize());
  461. break;
  462. }
  463. case TransactionId::DequeueBuffer: {
  464. IGBPDequeueBufferRequestParcel request{ctx.ReadBuffer()};
  465. const u32 width{request.data.width};
  466. const u32 height{request.data.height};
  467. do {
  468. if (auto result = buffer_queue.DequeueBuffer(width, height); result) {
  469. // Buffer is available
  470. IGBPDequeueBufferResponseParcel response{result->first, *result->second};
  471. ctx.WriteBuffer(response.Serialize());
  472. break;
  473. }
  474. } while (buffer_queue.IsConnected());
  475. break;
  476. }
  477. case TransactionId::RequestBuffer: {
  478. IGBPRequestBufferRequestParcel request{ctx.ReadBuffer()};
  479. auto& buffer = buffer_queue.RequestBuffer(request.slot);
  480. IGBPRequestBufferResponseParcel response{buffer};
  481. ctx.WriteBuffer(response.Serialize());
  482. break;
  483. }
  484. case TransactionId::QueueBuffer: {
  485. IGBPQueueBufferRequestParcel request{ctx.ReadBuffer()};
  486. buffer_queue.QueueBuffer(request.data.slot, request.data.transform,
  487. request.data.GetCropRect(), request.data.swap_interval,
  488. request.data.multi_fence);
  489. IGBPQueueBufferResponseParcel response{1280, 720};
  490. ctx.WriteBuffer(response.Serialize());
  491. break;
  492. }
  493. case TransactionId::Query: {
  494. IGBPQueryRequestParcel request{ctx.ReadBuffer()};
  495. const u32 value =
  496. buffer_queue.Query(static_cast<NVFlinger::BufferQueue::QueryType>(request.type));
  497. IGBPQueryResponseParcel response{value};
  498. ctx.WriteBuffer(response.Serialize());
  499. break;
  500. }
  501. case TransactionId::CancelBuffer: {
  502. IGBPCancelBufferRequestParcel request{ctx.ReadBuffer()};
  503. buffer_queue.CancelBuffer(request.data.slot, request.data.multi_fence);
  504. IGBPCancelBufferResponseParcel response{};
  505. ctx.WriteBuffer(response.Serialize());
  506. break;
  507. }
  508. case TransactionId::Disconnect: {
  509. LOG_WARNING(Service_VI, "(STUBBED) called, transaction=Disconnect");
  510. const auto buffer = ctx.ReadBuffer();
  511. buffer_queue.Disconnect();
  512. IGBPEmptyResponseParcel response{};
  513. ctx.WriteBuffer(response.Serialize());
  514. break;
  515. }
  516. case TransactionId::DetachBuffer: {
  517. const auto buffer = ctx.ReadBuffer();
  518. IGBPEmptyResponseParcel response{};
  519. ctx.WriteBuffer(response.Serialize());
  520. break;
  521. }
  522. case TransactionId::SetBufferCount: {
  523. LOG_WARNING(Service_VI, "(STUBBED) called, transaction=SetBufferCount");
  524. [[maybe_unused]] const auto buffer = ctx.ReadBuffer();
  525. IGBPEmptyResponseParcel response{};
  526. ctx.WriteBuffer(response.Serialize());
  527. break;
  528. }
  529. default:
  530. ASSERT_MSG(false, "Unimplemented");
  531. }
  532. IPC::ResponseBuilder rb{ctx, 2};
  533. rb.Push(ResultSuccess);
  534. }
  535. void AdjustRefcount(Kernel::HLERequestContext& ctx) {
  536. IPC::RequestParser rp{ctx};
  537. const u32 id = rp.Pop<u32>();
  538. const s32 addval = rp.PopRaw<s32>();
  539. const u32 type = rp.Pop<u32>();
  540. LOG_WARNING(Service_VI, "(STUBBED) called id={}, addval={:08X}, type={:08X}", id, addval,
  541. type);
  542. IPC::ResponseBuilder rb{ctx, 2};
  543. rb.Push(ResultSuccess);
  544. }
  545. void GetNativeHandle(Kernel::HLERequestContext& ctx) {
  546. IPC::RequestParser rp{ctx};
  547. const u32 id = rp.Pop<u32>();
  548. const u32 unknown = rp.Pop<u32>();
  549. LOG_WARNING(Service_VI, "(STUBBED) called id={}, unknown={:08X}", id, unknown);
  550. // TODO(Subv): Find out what this actually is.
  551. IPC::ResponseBuilder rb{ctx, 2, 1};
  552. rb.Push(ResultSuccess);
  553. rb.PushCopyObjects(nv_flinger.FindBufferQueue(id)->GetBufferWaitEvent());
  554. }
  555. NVFlinger::NVFlinger& nv_flinger;
  556. };
  557. class ISystemDisplayService final : public ServiceFramework<ISystemDisplayService> {
  558. public:
  559. explicit ISystemDisplayService(Core::System& system_)
  560. : ServiceFramework{system_, "ISystemDisplayService"} {
  561. static const FunctionInfo functions[] = {
  562. {1200, nullptr, "GetZOrderCountMin"},
  563. {1202, nullptr, "GetZOrderCountMax"},
  564. {1203, nullptr, "GetDisplayLogicalResolution"},
  565. {1204, nullptr, "SetDisplayMagnification"},
  566. {2201, nullptr, "SetLayerPosition"},
  567. {2203, nullptr, "SetLayerSize"},
  568. {2204, nullptr, "GetLayerZ"},
  569. {2205, &ISystemDisplayService::SetLayerZ, "SetLayerZ"},
  570. {2207, &ISystemDisplayService::SetLayerVisibility, "SetLayerVisibility"},
  571. {2209, nullptr, "SetLayerAlpha"},
  572. {2210, nullptr, "SetLayerPositionAndSize"},
  573. {2312, nullptr, "CreateStrayLayer"},
  574. {2400, nullptr, "OpenIndirectLayer"},
  575. {2401, nullptr, "CloseIndirectLayer"},
  576. {2402, nullptr, "FlipIndirectLayer"},
  577. {3000, nullptr, "ListDisplayModes"},
  578. {3001, nullptr, "ListDisplayRgbRanges"},
  579. {3002, nullptr, "ListDisplayContentTypes"},
  580. {3200, &ISystemDisplayService::GetDisplayMode, "GetDisplayMode"},
  581. {3201, nullptr, "SetDisplayMode"},
  582. {3202, nullptr, "GetDisplayUnderscan"},
  583. {3203, nullptr, "SetDisplayUnderscan"},
  584. {3204, nullptr, "GetDisplayContentType"},
  585. {3205, nullptr, "SetDisplayContentType"},
  586. {3206, nullptr, "GetDisplayRgbRange"},
  587. {3207, nullptr, "SetDisplayRgbRange"},
  588. {3208, nullptr, "GetDisplayCmuMode"},
  589. {3209, nullptr, "SetDisplayCmuMode"},
  590. {3210, nullptr, "GetDisplayContrastRatio"},
  591. {3211, nullptr, "SetDisplayContrastRatio"},
  592. {3214, nullptr, "GetDisplayGamma"},
  593. {3215, nullptr, "SetDisplayGamma"},
  594. {3216, nullptr, "GetDisplayCmuLuma"},
  595. {3217, nullptr, "SetDisplayCmuLuma"},
  596. {3218, nullptr, "SetDisplayCrcMode"},
  597. {6013, nullptr, "GetLayerPresentationSubmissionTimestamps"},
  598. {8225, nullptr, "GetSharedBufferMemoryHandleId"},
  599. {8250, nullptr, "OpenSharedLayer"},
  600. {8251, nullptr, "CloseSharedLayer"},
  601. {8252, nullptr, "ConnectSharedLayer"},
  602. {8253, nullptr, "DisconnectSharedLayer"},
  603. {8254, nullptr, "AcquireSharedFrameBuffer"},
  604. {8255, nullptr, "PresentSharedFrameBuffer"},
  605. {8256, nullptr, "GetSharedFrameBufferAcquirableEvent"},
  606. {8257, nullptr, "FillSharedFrameBufferColor"},
  607. {8258, nullptr, "CancelSharedFrameBuffer"},
  608. {9000, nullptr, "GetDp2hdmiController"},
  609. };
  610. RegisterHandlers(functions);
  611. }
  612. private:
  613. void SetLayerZ(Kernel::HLERequestContext& ctx) {
  614. IPC::RequestParser rp{ctx};
  615. const u64 layer_id = rp.Pop<u64>();
  616. const u64 z_value = rp.Pop<u64>();
  617. LOG_WARNING(Service_VI, "(STUBBED) called. layer_id=0x{:016X}, z_value=0x{:016X}", layer_id,
  618. z_value);
  619. IPC::ResponseBuilder rb{ctx, 2};
  620. rb.Push(ResultSuccess);
  621. }
  622. // This function currently does nothing but return a success error code in
  623. // the vi library itself, so do the same thing, but log out the passed in values.
  624. void SetLayerVisibility(Kernel::HLERequestContext& ctx) {
  625. IPC::RequestParser rp{ctx};
  626. const u64 layer_id = rp.Pop<u64>();
  627. const bool visibility = rp.Pop<bool>();
  628. LOG_DEBUG(Service_VI, "called, layer_id=0x{:08X}, visibility={}", layer_id, visibility);
  629. IPC::ResponseBuilder rb{ctx, 2};
  630. rb.Push(ResultSuccess);
  631. }
  632. void GetDisplayMode(Kernel::HLERequestContext& ctx) {
  633. LOG_WARNING(Service_VI, "(STUBBED) called");
  634. IPC::ResponseBuilder rb{ctx, 6};
  635. rb.Push(ResultSuccess);
  636. if (Settings::values.use_docked_mode.GetValue()) {
  637. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::DockedWidth) *
  638. static_cast<u32>(Settings::values.resolution_factor.GetValue()));
  639. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::DockedHeight) *
  640. static_cast<u32>(Settings::values.resolution_factor.GetValue()));
  641. } else {
  642. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::UndockedWidth) *
  643. static_cast<u32>(Settings::values.resolution_factor.GetValue()));
  644. rb.Push(static_cast<u32>(Service::VI::DisplayResolution::UndockedHeight) *
  645. static_cast<u32>(Settings::values.resolution_factor.GetValue()));
  646. }
  647. rb.PushRaw<float>(60.0f); // This wouldn't seem to be correct for 30 fps games.
  648. rb.Push<u32>(0);
  649. }
  650. };
  651. class IManagerDisplayService final : public ServiceFramework<IManagerDisplayService> {
  652. public:
  653. explicit IManagerDisplayService(Core::System& system_, NVFlinger::NVFlinger& nv_flinger_)
  654. : ServiceFramework{system_, "IManagerDisplayService"}, nv_flinger{nv_flinger_} {
  655. // clang-format off
  656. static const FunctionInfo functions[] = {
  657. {200, nullptr, "AllocateProcessHeapBlock"},
  658. {201, nullptr, "FreeProcessHeapBlock"},
  659. {1020, &IManagerDisplayService::CloseDisplay, "CloseDisplay"},
  660. {1102, nullptr, "GetDisplayResolution"},
  661. {2010, &IManagerDisplayService::CreateManagedLayer, "CreateManagedLayer"},
  662. {2011, nullptr, "DestroyManagedLayer"},
  663. {2012, nullptr, "CreateStrayLayer"},
  664. {2050, nullptr, "CreateIndirectLayer"},
  665. {2051, nullptr, "DestroyIndirectLayer"},
  666. {2052, nullptr, "CreateIndirectProducerEndPoint"},
  667. {2053, nullptr, "DestroyIndirectProducerEndPoint"},
  668. {2054, nullptr, "CreateIndirectConsumerEndPoint"},
  669. {2055, nullptr, "DestroyIndirectConsumerEndPoint"},
  670. {2300, nullptr, "AcquireLayerTexturePresentingEvent"},
  671. {2301, nullptr, "ReleaseLayerTexturePresentingEvent"},
  672. {2302, nullptr, "GetDisplayHotplugEvent"},
  673. {2303, nullptr, "GetDisplayModeChangedEvent"},
  674. {2402, nullptr, "GetDisplayHotplugState"},
  675. {2501, nullptr, "GetCompositorErrorInfo"},
  676. {2601, nullptr, "GetDisplayErrorEvent"},
  677. {2701, nullptr, "GetDisplayFatalErrorEvent"},
  678. {4201, nullptr, "SetDisplayAlpha"},
  679. {4203, nullptr, "SetDisplayLayerStack"},
  680. {4205, nullptr, "SetDisplayPowerState"},
  681. {4206, nullptr, "SetDefaultDisplay"},
  682. {4207, nullptr, "ResetDisplayPanel"},
  683. {4208, nullptr, "SetDisplayFatalErrorEnabled"},
  684. {4209, nullptr, "IsDisplayPanelOn"},
  685. {4300, nullptr, "GetInternalPanelId"},
  686. {6000, &IManagerDisplayService::AddToLayerStack, "AddToLayerStack"},
  687. {6001, nullptr, "RemoveFromLayerStack"},
  688. {6002, &IManagerDisplayService::SetLayerVisibility, "SetLayerVisibility"},
  689. {6003, nullptr, "SetLayerConfig"},
  690. {6004, nullptr, "AttachLayerPresentationTracer"},
  691. {6005, nullptr, "DetachLayerPresentationTracer"},
  692. {6006, nullptr, "StartLayerPresentationRecording"},
  693. {6007, nullptr, "StopLayerPresentationRecording"},
  694. {6008, nullptr, "StartLayerPresentationFenceWait"},
  695. {6009, nullptr, "StopLayerPresentationFenceWait"},
  696. {6010, nullptr, "GetLayerPresentationAllFencesExpiredEvent"},
  697. {6011, nullptr, "EnableLayerAutoClearTransitionBuffer"},
  698. {6012, nullptr, "DisableLayerAutoClearTransitionBuffer"},
  699. {7000, nullptr, "SetContentVisibility"},
  700. {8000, nullptr, "SetConductorLayer"},
  701. {8001, nullptr, "SetTimestampTracking"},
  702. {8100, nullptr, "SetIndirectProducerFlipOffset"},
  703. {8200, nullptr, "CreateSharedBufferStaticStorage"},
  704. {8201, nullptr, "CreateSharedBufferTransferMemory"},
  705. {8202, nullptr, "DestroySharedBuffer"},
  706. {8203, nullptr, "BindSharedLowLevelLayerToManagedLayer"},
  707. {8204, nullptr, "BindSharedLowLevelLayerToIndirectLayer"},
  708. {8207, nullptr, "UnbindSharedLowLevelLayer"},
  709. {8208, nullptr, "ConnectSharedLowLevelLayerToSharedBuffer"},
  710. {8209, nullptr, "DisconnectSharedLowLevelLayerFromSharedBuffer"},
  711. {8210, nullptr, "CreateSharedLayer"},
  712. {8211, nullptr, "DestroySharedLayer"},
  713. {8216, nullptr, "AttachSharedLayerToLowLevelLayer"},
  714. {8217, nullptr, "ForceDetachSharedLayerFromLowLevelLayer"},
  715. {8218, nullptr, "StartDetachSharedLayerFromLowLevelLayer"},
  716. {8219, nullptr, "FinishDetachSharedLayerFromLowLevelLayer"},
  717. {8220, nullptr, "GetSharedLayerDetachReadyEvent"},
  718. {8221, nullptr, "GetSharedLowLevelLayerSynchronizedEvent"},
  719. {8222, nullptr, "CheckSharedLowLevelLayerSynchronized"},
  720. {8223, nullptr, "RegisterSharedBufferImporterAruid"},
  721. {8224, nullptr, "UnregisterSharedBufferImporterAruid"},
  722. {8227, nullptr, "CreateSharedBufferProcessHeap"},
  723. {8228, nullptr, "GetSharedLayerLayerStacks"},
  724. {8229, nullptr, "SetSharedLayerLayerStacks"},
  725. {8291, nullptr, "PresentDetachedSharedFrameBufferToLowLevelLayer"},
  726. {8292, nullptr, "FillDetachedSharedFrameBufferColor"},
  727. {8293, nullptr, "GetDetachedSharedFrameBufferImage"},
  728. {8294, nullptr, "SetDetachedSharedFrameBufferImage"},
  729. {8295, nullptr, "CopyDetachedSharedFrameBufferImage"},
  730. {8296, nullptr, "SetDetachedSharedFrameBufferSubImage"},
  731. {8297, nullptr, "GetSharedFrameBufferContentParameter"},
  732. {8298, nullptr, "ExpandStartupLogoOnSharedFrameBuffer"},
  733. };
  734. // clang-format on
  735. RegisterHandlers(functions);
  736. }
  737. private:
  738. void CloseDisplay(Kernel::HLERequestContext& ctx) {
  739. IPC::RequestParser rp{ctx};
  740. const u64 display = rp.Pop<u64>();
  741. LOG_WARNING(Service_VI, "(STUBBED) called. display=0x{:016X}", display);
  742. IPC::ResponseBuilder rb{ctx, 2};
  743. rb.Push(ResultSuccess);
  744. }
  745. void CreateManagedLayer(Kernel::HLERequestContext& ctx) {
  746. IPC::RequestParser rp{ctx};
  747. const u32 unknown = rp.Pop<u32>();
  748. rp.Skip(1, false);
  749. const u64 display = rp.Pop<u64>();
  750. const u64 aruid = rp.Pop<u64>();
  751. LOG_WARNING(Service_VI,
  752. "(STUBBED) called. unknown=0x{:08X}, display=0x{:016X}, aruid=0x{:016X}",
  753. unknown, display, aruid);
  754. const auto layer_id = nv_flinger.CreateLayer(display);
  755. if (!layer_id) {
  756. LOG_ERROR(Service_VI, "Layer not found! display=0x{:016X}", display);
  757. IPC::ResponseBuilder rb{ctx, 2};
  758. rb.Push(ERR_NOT_FOUND);
  759. return;
  760. }
  761. IPC::ResponseBuilder rb{ctx, 4};
  762. rb.Push(ResultSuccess);
  763. rb.Push(*layer_id);
  764. }
  765. void AddToLayerStack(Kernel::HLERequestContext& ctx) {
  766. IPC::RequestParser rp{ctx};
  767. const u32 stack = rp.Pop<u32>();
  768. const u64 layer_id = rp.Pop<u64>();
  769. LOG_WARNING(Service_VI, "(STUBBED) called. stack=0x{:08X}, layer_id=0x{:016X}", stack,
  770. layer_id);
  771. IPC::ResponseBuilder rb{ctx, 2};
  772. rb.Push(ResultSuccess);
  773. }
  774. void SetLayerVisibility(Kernel::HLERequestContext& ctx) {
  775. IPC::RequestParser rp{ctx};
  776. const u64 layer_id = rp.Pop<u64>();
  777. const bool visibility = rp.Pop<bool>();
  778. LOG_WARNING(Service_VI, "(STUBBED) called, layer_id=0x{:X}, visibility={}", layer_id,
  779. visibility);
  780. IPC::ResponseBuilder rb{ctx, 2};
  781. rb.Push(ResultSuccess);
  782. }
  783. NVFlinger::NVFlinger& nv_flinger;
  784. };
  785. class IApplicationDisplayService final : public ServiceFramework<IApplicationDisplayService> {
  786. public:
  787. explicit IApplicationDisplayService(Core::System& system_, NVFlinger::NVFlinger& nv_flinger_);
  788. private:
  789. enum class ConvertedScaleMode : u64 {
  790. Freeze = 0,
  791. ScaleToWindow = 1,
  792. ScaleAndCrop = 2,
  793. None = 3,
  794. PreserveAspectRatio = 4,
  795. };
  796. enum class NintendoScaleMode : u32 {
  797. None = 0,
  798. Freeze = 1,
  799. ScaleToWindow = 2,
  800. ScaleAndCrop = 3,
  801. PreserveAspectRatio = 4,
  802. };
  803. void GetRelayService(Kernel::HLERequestContext& ctx) {
  804. LOG_WARNING(Service_VI, "(STUBBED) called");
  805. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  806. rb.Push(ResultSuccess);
  807. rb.PushIpcInterface<IHOSBinderDriver>(system, nv_flinger);
  808. }
  809. void GetSystemDisplayService(Kernel::HLERequestContext& ctx) {
  810. LOG_WARNING(Service_VI, "(STUBBED) called");
  811. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  812. rb.Push(ResultSuccess);
  813. rb.PushIpcInterface<ISystemDisplayService>(system);
  814. }
  815. void GetManagerDisplayService(Kernel::HLERequestContext& ctx) {
  816. LOG_WARNING(Service_VI, "(STUBBED) called");
  817. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  818. rb.Push(ResultSuccess);
  819. rb.PushIpcInterface<IManagerDisplayService>(system, nv_flinger);
  820. }
  821. void GetIndirectDisplayTransactionService(Kernel::HLERequestContext& ctx) {
  822. LOG_WARNING(Service_VI, "(STUBBED) called");
  823. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  824. rb.Push(ResultSuccess);
  825. rb.PushIpcInterface<IHOSBinderDriver>(system, nv_flinger);
  826. }
  827. void OpenDisplay(Kernel::HLERequestContext& ctx) {
  828. LOG_WARNING(Service_VI, "(STUBBED) called");
  829. IPC::RequestParser rp{ctx};
  830. const auto name_buf = rp.PopRaw<std::array<char, 0x40>>();
  831. OpenDisplayImpl(ctx, std::string_view{name_buf.data(), name_buf.size()});
  832. }
  833. void OpenDefaultDisplay(Kernel::HLERequestContext& ctx) {
  834. LOG_DEBUG(Service_VI, "called");
  835. OpenDisplayImpl(ctx, "Default");
  836. }
  837. void OpenDisplayImpl(Kernel::HLERequestContext& ctx, std::string_view name) {
  838. const auto trim_pos = name.find('\0');
  839. if (trim_pos != std::string_view::npos) {
  840. name.remove_suffix(name.size() - trim_pos);
  841. }
  842. ASSERT_MSG(name == "Default", "Non-default displays aren't supported yet");
  843. const auto display_id = nv_flinger.OpenDisplay(name);
  844. if (!display_id) {
  845. LOG_ERROR(Service_VI, "Display not found! display_name={}", name);
  846. IPC::ResponseBuilder rb{ctx, 2};
  847. rb.Push(ERR_NOT_FOUND);
  848. return;
  849. }
  850. IPC::ResponseBuilder rb{ctx, 4};
  851. rb.Push(ResultSuccess);
  852. rb.Push<u64>(*display_id);
  853. }
  854. void CloseDisplay(Kernel::HLERequestContext& ctx) {
  855. IPC::RequestParser rp{ctx};
  856. const u64 display_id = rp.Pop<u64>();
  857. LOG_WARNING(Service_VI, "(STUBBED) called. display_id=0x{:016X}", display_id);
  858. IPC::ResponseBuilder rb{ctx, 2};
  859. rb.Push(ResultSuccess);
  860. }
  861. // This literally does nothing internally in the actual service itself,
  862. // and just returns a successful result code regardless of the input.
  863. void SetDisplayEnabled(Kernel::HLERequestContext& ctx) {
  864. LOG_DEBUG(Service_VI, "called.");
  865. IPC::ResponseBuilder rb{ctx, 2};
  866. rb.Push(ResultSuccess);
  867. }
  868. void GetDisplayResolution(Kernel::HLERequestContext& ctx) {
  869. IPC::RequestParser rp{ctx};
  870. const u64 display_id = rp.Pop<u64>();
  871. LOG_DEBUG(Service_VI, "called. display_id=0x{:016X}", display_id);
  872. IPC::ResponseBuilder rb{ctx, 6};
  873. rb.Push(ResultSuccess);
  874. // This only returns the fixed values of 1280x720 and makes no distinguishing
  875. // between docked and undocked dimensions. We take the liberty of applying
  876. // the resolution scaling factor here.
  877. rb.Push(static_cast<u64>(DisplayResolution::UndockedWidth) *
  878. static_cast<u32>(Settings::values.resolution_factor.GetValue()));
  879. rb.Push(static_cast<u64>(DisplayResolution::UndockedHeight) *
  880. static_cast<u32>(Settings::values.resolution_factor.GetValue()));
  881. }
  882. void SetLayerScalingMode(Kernel::HLERequestContext& ctx) {
  883. IPC::RequestParser rp{ctx};
  884. const auto scaling_mode = rp.PopEnum<NintendoScaleMode>();
  885. const u64 unknown = rp.Pop<u64>();
  886. LOG_DEBUG(Service_VI, "called. scaling_mode=0x{:08X}, unknown=0x{:016X}", scaling_mode,
  887. unknown);
  888. IPC::ResponseBuilder rb{ctx, 2};
  889. if (scaling_mode > NintendoScaleMode::PreserveAspectRatio) {
  890. LOG_ERROR(Service_VI, "Invalid scaling mode provided.");
  891. rb.Push(ERR_OPERATION_FAILED);
  892. return;
  893. }
  894. if (scaling_mode != NintendoScaleMode::ScaleToWindow &&
  895. scaling_mode != NintendoScaleMode::PreserveAspectRatio) {
  896. LOG_ERROR(Service_VI, "Unsupported scaling mode supplied.");
  897. rb.Push(ERR_UNSUPPORTED);
  898. return;
  899. }
  900. rb.Push(ResultSuccess);
  901. }
  902. void ListDisplays(Kernel::HLERequestContext& ctx) {
  903. LOG_WARNING(Service_VI, "(STUBBED) called");
  904. DisplayInfo display_info;
  905. display_info.width *= static_cast<u64>(Settings::values.resolution_factor.GetValue());
  906. display_info.height *= static_cast<u64>(Settings::values.resolution_factor.GetValue());
  907. ctx.WriteBuffer(&display_info, sizeof(DisplayInfo));
  908. IPC::ResponseBuilder rb{ctx, 4};
  909. rb.Push(ResultSuccess);
  910. rb.Push<u64>(1);
  911. }
  912. void OpenLayer(Kernel::HLERequestContext& ctx) {
  913. IPC::RequestParser rp{ctx};
  914. const auto name_buf = rp.PopRaw<std::array<u8, 0x40>>();
  915. const auto end = std::find(name_buf.begin(), name_buf.end(), '\0');
  916. const std::string display_name(name_buf.begin(), end);
  917. const u64 layer_id = rp.Pop<u64>();
  918. const u64 aruid = rp.Pop<u64>();
  919. LOG_DEBUG(Service_VI, "called. layer_id=0x{:016X}, aruid=0x{:016X}", layer_id, aruid);
  920. const auto display_id = nv_flinger.OpenDisplay(display_name);
  921. if (!display_id) {
  922. LOG_ERROR(Service_VI, "Layer not found! layer_id={}", layer_id);
  923. IPC::ResponseBuilder rb{ctx, 2};
  924. rb.Push(ERR_NOT_FOUND);
  925. return;
  926. }
  927. const auto buffer_queue_id = nv_flinger.FindBufferQueueId(*display_id, layer_id);
  928. if (!buffer_queue_id) {
  929. LOG_ERROR(Service_VI, "Buffer queue id not found! display_id={}", *display_id);
  930. IPC::ResponseBuilder rb{ctx, 2};
  931. rb.Push(ERR_NOT_FOUND);
  932. return;
  933. }
  934. NativeWindow native_window{*buffer_queue_id};
  935. const auto buffer_size = ctx.WriteBuffer(native_window.Serialize());
  936. IPC::ResponseBuilder rb{ctx, 4};
  937. rb.Push(ResultSuccess);
  938. rb.Push<u64>(buffer_size);
  939. }
  940. void CloseLayer(Kernel::HLERequestContext& ctx) {
  941. IPC::RequestParser rp{ctx};
  942. const auto layer_id{rp.Pop<u64>()};
  943. LOG_DEBUG(Service_VI, "called. layer_id=0x{:016X}", layer_id);
  944. nv_flinger.CloseLayer(layer_id);
  945. IPC::ResponseBuilder rb{ctx, 2};
  946. rb.Push(ResultSuccess);
  947. }
  948. void CreateStrayLayer(Kernel::HLERequestContext& ctx) {
  949. IPC::RequestParser rp{ctx};
  950. const u32 flags = rp.Pop<u32>();
  951. rp.Pop<u32>(); // padding
  952. const u64 display_id = rp.Pop<u64>();
  953. LOG_DEBUG(Service_VI, "called. flags=0x{:08X}, display_id=0x{:016X}", flags, display_id);
  954. // TODO(Subv): What's the difference between a Stray and a Managed layer?
  955. const auto layer_id = nv_flinger.CreateLayer(display_id);
  956. if (!layer_id) {
  957. LOG_ERROR(Service_VI, "Layer not found! display_id={}", display_id);
  958. IPC::ResponseBuilder rb{ctx, 2};
  959. rb.Push(ERR_NOT_FOUND);
  960. return;
  961. }
  962. const auto buffer_queue_id = nv_flinger.FindBufferQueueId(display_id, *layer_id);
  963. if (!buffer_queue_id) {
  964. LOG_ERROR(Service_VI, "Buffer queue id not found! display_id={}", display_id);
  965. IPC::ResponseBuilder rb{ctx, 2};
  966. rb.Push(ERR_NOT_FOUND);
  967. return;
  968. }
  969. NativeWindow native_window{*buffer_queue_id};
  970. const auto buffer_size = ctx.WriteBuffer(native_window.Serialize());
  971. IPC::ResponseBuilder rb{ctx, 6};
  972. rb.Push(ResultSuccess);
  973. rb.Push(*layer_id);
  974. rb.Push<u64>(buffer_size);
  975. }
  976. void DestroyStrayLayer(Kernel::HLERequestContext& ctx) {
  977. IPC::RequestParser rp{ctx};
  978. const u64 layer_id = rp.Pop<u64>();
  979. LOG_WARNING(Service_VI, "(STUBBED) called. layer_id=0x{:016X}", layer_id);
  980. IPC::ResponseBuilder rb{ctx, 2};
  981. rb.Push(ResultSuccess);
  982. }
  983. void GetDisplayVsyncEvent(Kernel::HLERequestContext& ctx) {
  984. IPC::RequestParser rp{ctx};
  985. const u64 display_id = rp.Pop<u64>();
  986. LOG_WARNING(Service_VI, "(STUBBED) called. display_id=0x{:016X}", display_id);
  987. const auto vsync_event = nv_flinger.FindVsyncEvent(display_id);
  988. if (!vsync_event) {
  989. LOG_ERROR(Service_VI, "Vsync event was not found for display_id={}", display_id);
  990. IPC::ResponseBuilder rb{ctx, 2};
  991. rb.Push(ERR_NOT_FOUND);
  992. return;
  993. }
  994. IPC::ResponseBuilder rb{ctx, 2, 1};
  995. rb.Push(ResultSuccess);
  996. rb.PushCopyObjects(vsync_event);
  997. }
  998. void ConvertScalingMode(Kernel::HLERequestContext& ctx) {
  999. IPC::RequestParser rp{ctx};
  1000. const auto mode = rp.PopEnum<NintendoScaleMode>();
  1001. LOG_DEBUG(Service_VI, "called mode={}", mode);
  1002. const auto converted_mode = ConvertScalingModeImpl(mode);
  1003. if (converted_mode.Succeeded()) {
  1004. IPC::ResponseBuilder rb{ctx, 4};
  1005. rb.Push(ResultSuccess);
  1006. rb.PushEnum(*converted_mode);
  1007. } else {
  1008. IPC::ResponseBuilder rb{ctx, 2};
  1009. rb.Push(converted_mode.Code());
  1010. }
  1011. }
  1012. void GetIndirectLayerImageMap(Kernel::HLERequestContext& ctx) {
  1013. IPC::RequestParser rp{ctx};
  1014. const auto width = rp.Pop<s64>();
  1015. const auto height = rp.Pop<s64>();
  1016. const auto indirect_layer_consumer_handle = rp.Pop<u64>();
  1017. const auto applet_resource_user_id = rp.Pop<u64>();
  1018. LOG_WARNING(Service_VI,
  1019. "(STUBBED) called, width={}, height={}, indirect_layer_consumer_handle={}, "
  1020. "applet_resource_user_id={}",
  1021. width, height, indirect_layer_consumer_handle, applet_resource_user_id);
  1022. std::vector<u8> out_buffer(0x46);
  1023. ctx.WriteBuffer(out_buffer);
  1024. // TODO: Figure out what these are
  1025. constexpr s64 unknown_result_1 = 0;
  1026. constexpr s64 unknown_result_2 = 0;
  1027. IPC::ResponseBuilder rb{ctx, 6};
  1028. rb.Push(unknown_result_1);
  1029. rb.Push(unknown_result_2);
  1030. rb.Push(ResultSuccess);
  1031. }
  1032. void GetIndirectLayerImageRequiredMemoryInfo(Kernel::HLERequestContext& ctx) {
  1033. IPC::RequestParser rp{ctx};
  1034. const auto width = rp.Pop<u64>();
  1035. const auto height = rp.Pop<u64>();
  1036. LOG_DEBUG(Service_VI, "called width={}, height={}", width, height);
  1037. constexpr u64 base_size = 0x20000;
  1038. constexpr u64 alignment = 0x1000;
  1039. const auto texture_size = width * height * 4;
  1040. const auto out_size = (texture_size + base_size - 1) / base_size * base_size;
  1041. IPC::ResponseBuilder rb{ctx, 6};
  1042. rb.Push(ResultSuccess);
  1043. rb.Push(out_size);
  1044. rb.Push(alignment);
  1045. }
  1046. static ResultVal<ConvertedScaleMode> ConvertScalingModeImpl(NintendoScaleMode mode) {
  1047. switch (mode) {
  1048. case NintendoScaleMode::None:
  1049. return MakeResult(ConvertedScaleMode::None);
  1050. case NintendoScaleMode::Freeze:
  1051. return MakeResult(ConvertedScaleMode::Freeze);
  1052. case NintendoScaleMode::ScaleToWindow:
  1053. return MakeResult(ConvertedScaleMode::ScaleToWindow);
  1054. case NintendoScaleMode::ScaleAndCrop:
  1055. return MakeResult(ConvertedScaleMode::ScaleAndCrop);
  1056. case NintendoScaleMode::PreserveAspectRatio:
  1057. return MakeResult(ConvertedScaleMode::PreserveAspectRatio);
  1058. default:
  1059. LOG_ERROR(Service_VI, "Invalid scaling mode specified, mode={}", mode);
  1060. return ERR_OPERATION_FAILED;
  1061. }
  1062. }
  1063. NVFlinger::NVFlinger& nv_flinger;
  1064. };
  1065. IApplicationDisplayService::IApplicationDisplayService(Core::System& system_,
  1066. NVFlinger::NVFlinger& nv_flinger_)
  1067. : ServiceFramework{system_, "IApplicationDisplayService"}, nv_flinger{nv_flinger_} {
  1068. static const FunctionInfo functions[] = {
  1069. {100, &IApplicationDisplayService::GetRelayService, "GetRelayService"},
  1070. {101, &IApplicationDisplayService::GetSystemDisplayService, "GetSystemDisplayService"},
  1071. {102, &IApplicationDisplayService::GetManagerDisplayService, "GetManagerDisplayService"},
  1072. {103, &IApplicationDisplayService::GetIndirectDisplayTransactionService,
  1073. "GetIndirectDisplayTransactionService"},
  1074. {1000, &IApplicationDisplayService::ListDisplays, "ListDisplays"},
  1075. {1010, &IApplicationDisplayService::OpenDisplay, "OpenDisplay"},
  1076. {1011, &IApplicationDisplayService::OpenDefaultDisplay, "OpenDefaultDisplay"},
  1077. {1020, &IApplicationDisplayService::CloseDisplay, "CloseDisplay"},
  1078. {1101, &IApplicationDisplayService::SetDisplayEnabled, "SetDisplayEnabled"},
  1079. {1102, &IApplicationDisplayService::GetDisplayResolution, "GetDisplayResolution"},
  1080. {2020, &IApplicationDisplayService::OpenLayer, "OpenLayer"},
  1081. {2021, &IApplicationDisplayService::CloseLayer, "CloseLayer"},
  1082. {2030, &IApplicationDisplayService::CreateStrayLayer, "CreateStrayLayer"},
  1083. {2031, &IApplicationDisplayService::DestroyStrayLayer, "DestroyStrayLayer"},
  1084. {2101, &IApplicationDisplayService::SetLayerScalingMode, "SetLayerScalingMode"},
  1085. {2102, &IApplicationDisplayService::ConvertScalingMode, "ConvertScalingMode"},
  1086. {2450, &IApplicationDisplayService::GetIndirectLayerImageMap, "GetIndirectLayerImageMap"},
  1087. {2451, nullptr, "GetIndirectLayerImageCropMap"},
  1088. {2460, &IApplicationDisplayService::GetIndirectLayerImageRequiredMemoryInfo,
  1089. "GetIndirectLayerImageRequiredMemoryInfo"},
  1090. {5202, &IApplicationDisplayService::GetDisplayVsyncEvent, "GetDisplayVsyncEvent"},
  1091. {5203, nullptr, "GetDisplayVsyncEventForDebug"},
  1092. };
  1093. RegisterHandlers(functions);
  1094. }
  1095. static bool IsValidServiceAccess(Permission permission, Policy policy) {
  1096. if (permission == Permission::User) {
  1097. return policy == Policy::User;
  1098. }
  1099. if (permission == Permission::System || permission == Permission::Manager) {
  1100. return policy == Policy::User || policy == Policy::Compositor;
  1101. }
  1102. return false;
  1103. }
  1104. void detail::GetDisplayServiceImpl(Kernel::HLERequestContext& ctx, Core::System& system,
  1105. NVFlinger::NVFlinger& nv_flinger, Permission permission) {
  1106. IPC::RequestParser rp{ctx};
  1107. const auto policy = rp.PopEnum<Policy>();
  1108. if (!IsValidServiceAccess(permission, policy)) {
  1109. LOG_ERROR(Service_VI, "Permission denied for policy {}", policy);
  1110. IPC::ResponseBuilder rb{ctx, 2};
  1111. rb.Push(ERR_PERMISSION_DENIED);
  1112. return;
  1113. }
  1114. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  1115. rb.Push(ResultSuccess);
  1116. rb.PushIpcInterface<IApplicationDisplayService>(system, nv_flinger);
  1117. }
  1118. void InstallInterfaces(SM::ServiceManager& service_manager, Core::System& system,
  1119. NVFlinger::NVFlinger& nv_flinger) {
  1120. std::make_shared<VI_M>(system, nv_flinger)->InstallAsService(service_manager);
  1121. std::make_shared<VI_S>(system, nv_flinger)->InstallAsService(service_manager);
  1122. std::make_shared<VI_U>(system, nv_flinger)->InstallAsService(service_manager);
  1123. }
  1124. } // namespace Service::VI