y2r_u.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. // Copyright 2015 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <cstring>
  5. #include "common/logging/log.h"
  6. #include "core/hle/hle.h"
  7. #include "core/hle/kernel/event.h"
  8. #include "core/hle/service/y2r_u.h"
  9. #include "core/hw/y2r.h"
  10. #include "core/mem_map.h"
  11. #include "video_core/utils.h"
  12. #include "video_core/video_core.h"
  13. ////////////////////////////////////////////////////////////////////////////////////////////////////
  14. // Namespace Y2R_U
  15. namespace Y2R_U {
  16. struct ConversionParameters {
  17. InputFormat input_format;
  18. OutputFormat output_format;
  19. Rotation rotation;
  20. BlockAlignment block_alignment;
  21. u16 input_line_width;
  22. u16 input_lines;
  23. StandardCoefficient standard_coefficient;
  24. u8 reserved;
  25. u16 alpha;
  26. };
  27. static_assert(sizeof(ConversionParameters) == 12, "ConversionParameters struct has incorrect size");
  28. static Kernel::SharedPtr<Kernel::Event> completion_event;
  29. static ConversionConfiguration conversion;
  30. static const CoefficientSet standard_coefficients[4] = {
  31. {{ 0x100, 0x166, 0xB6, 0x58, 0x1C5, -0x166F, 0x10EE, -0x1C5B }}, // ITU_Rec601
  32. {{ 0x100, 0x193, 0x77, 0x2F, 0x1DB, -0x1933, 0xA7C, -0x1D51 }}, // ITU_Rec709
  33. {{ 0x12A, 0x198, 0xD0, 0x64, 0x204, -0x1BDE, 0x10F2, -0x229B }}, // ITU_Rec601_Scaling
  34. {{ 0x12A, 0x1CA, 0x88, 0x36, 0x21C, -0x1F04, 0x99C, -0x2421 }}, // ITU_Rec709_Scaling
  35. };
  36. ResultCode ConversionConfiguration::SetInputLineWidth(u16 width) {
  37. if (width == 0 || width > 1024 || width % 8 != 0) {
  38. return ResultCode(ErrorDescription::OutOfRange, ErrorModule::CAM,
  39. ErrorSummary::InvalidArgument, ErrorLevel::Usage); // 0xE0E053FD
  40. }
  41. // Note: The hardware uses the register value 0 to represent a width of 1024, so for a width of
  42. // 1024 the `camera` module would set the value 0 here, but we don't need to emulate this
  43. // internal detail.
  44. this->input_line_width = width;
  45. return RESULT_SUCCESS;
  46. }
  47. ResultCode ConversionConfiguration::SetInputLines(u16 lines) {
  48. if (lines == 0 || lines > 1024) {
  49. return ResultCode(ErrorDescription::OutOfRange, ErrorModule::CAM,
  50. ErrorSummary::InvalidArgument, ErrorLevel::Usage); // 0xE0E053FD
  51. }
  52. // Note: In what appears to be a bug, the `camera` module does not set the hardware register at
  53. // all if `lines` is 1024, so the conversion uses the last value that was set. The intention
  54. // was probably to set it to 0 like in SetInputLineWidth.
  55. if (lines != 1024) {
  56. this->input_lines = lines;
  57. }
  58. return RESULT_SUCCESS;
  59. }
  60. ResultCode ConversionConfiguration::SetStandardCoefficient(StandardCoefficient standard_coefficient) {
  61. size_t index = static_cast<size_t>(standard_coefficient);
  62. if (index >= 4) {
  63. return ResultCode(ErrorDescription::InvalidEnumValue, ErrorModule::CAM,
  64. ErrorSummary::InvalidArgument, ErrorLevel::Usage); // 0xE0E053ED
  65. }
  66. std::memcpy(coefficients.data(), standard_coefficients[index].data(), sizeof(coefficients));
  67. return RESULT_SUCCESS;
  68. }
  69. static void SetInputFormat(Service::Interface* self) {
  70. u32* cmd_buff = Kernel::GetCommandBuffer();
  71. conversion.input_format = static_cast<InputFormat>(cmd_buff[1]);
  72. LOG_DEBUG(Service_Y2R, "called input_format=%hhu", conversion.input_format);
  73. cmd_buff[1] = RESULT_SUCCESS.raw;
  74. }
  75. static void SetOutputFormat(Service::Interface* self) {
  76. u32* cmd_buff = Kernel::GetCommandBuffer();
  77. conversion.output_format = static_cast<OutputFormat>(cmd_buff[1]);
  78. LOG_DEBUG(Service_Y2R, "called output_format=%hhu", conversion.output_format);
  79. cmd_buff[1] = RESULT_SUCCESS.raw;
  80. }
  81. static void SetRotation(Service::Interface* self) {
  82. u32* cmd_buff = Kernel::GetCommandBuffer();
  83. conversion.rotation = static_cast<Rotation>(cmd_buff[1]);
  84. LOG_DEBUG(Service_Y2R, "called rotation=%hhu", conversion.rotation);
  85. cmd_buff[1] = RESULT_SUCCESS.raw;
  86. }
  87. static void SetBlockAlignment(Service::Interface* self) {
  88. u32* cmd_buff = Kernel::GetCommandBuffer();
  89. conversion.block_alignment = static_cast<BlockAlignment>(cmd_buff[1]);
  90. LOG_DEBUG(Service_Y2R, "called alignment=%hhu", conversion.block_alignment);
  91. cmd_buff[1] = RESULT_SUCCESS.raw;
  92. }
  93. static void SetTransferEndInterrupt(Service::Interface* self) {
  94. u32* cmd_buff = Kernel::GetCommandBuffer();
  95. cmd_buff[0] = 0x000D0040;
  96. cmd_buff[1] = RESULT_SUCCESS.raw;
  97. LOG_DEBUG(Service_Y2R, "(STUBBED) called");
  98. }
  99. /**
  100. * Y2R_U::GetTransferEndEvent service function
  101. * Outputs:
  102. * 1 : Result of function, 0 on success, otherwise error code
  103. * 3 : The handle of the completion event
  104. */
  105. static void GetTransferEndEvent(Service::Interface* self) {
  106. u32* cmd_buff = Kernel::GetCommandBuffer();
  107. cmd_buff[1] = RESULT_SUCCESS.raw;
  108. cmd_buff[3] = Kernel::g_handle_table.Create(completion_event).MoveFrom();
  109. LOG_DEBUG(Service_Y2R, "called");
  110. }
  111. static void SetSendingY(Service::Interface* self) {
  112. u32* cmd_buff = Kernel::GetCommandBuffer();
  113. conversion.src_Y.address = cmd_buff[1];
  114. conversion.src_Y.image_size = cmd_buff[2];
  115. conversion.src_Y.transfer_unit = cmd_buff[3];
  116. conversion.src_Y.gap = cmd_buff[4];
  117. u32 src_process_handle = cmd_buff[6];
  118. LOG_DEBUG(Service_Y2R, "called image_size=0x%08X, transfer_unit=%hu, transfer_stride=%hu, "
  119. "src_process_handle=0x%08X", conversion.src_Y.image_size,
  120. conversion.src_Y.transfer_unit, conversion.src_Y.gap, src_process_handle);
  121. cmd_buff[1] = RESULT_SUCCESS.raw;
  122. }
  123. static void SetSendingU(Service::Interface* self) {
  124. u32* cmd_buff = Kernel::GetCommandBuffer();
  125. conversion.src_U.address = cmd_buff[1];
  126. conversion.src_U.image_size = cmd_buff[2];
  127. conversion.src_U.transfer_unit = cmd_buff[3];
  128. conversion.src_U.gap = cmd_buff[4];
  129. u32 src_process_handle = cmd_buff[6];
  130. LOG_DEBUG(Service_Y2R, "called image_size=0x%08X, transfer_unit=%hu, transfer_stride=%hu, "
  131. "src_process_handle=0x%08X", conversion.src_U.image_size,
  132. conversion.src_U.transfer_unit, conversion.src_U.gap, src_process_handle);
  133. cmd_buff[1] = RESULT_SUCCESS.raw;
  134. }
  135. static void SetSendingV(Service::Interface* self) {
  136. u32* cmd_buff = Kernel::GetCommandBuffer();
  137. conversion.src_V.address = cmd_buff[1];
  138. conversion.src_V.image_size = cmd_buff[2];
  139. conversion.src_V.transfer_unit = cmd_buff[3];
  140. conversion.src_V.gap = cmd_buff[4];
  141. u32 src_process_handle = cmd_buff[6];
  142. LOG_DEBUG(Service_Y2R, "called image_size=0x%08X, transfer_unit=%hu, transfer_stride=%hu, "
  143. "src_process_handle=0x%08X", conversion.src_V.image_size,
  144. conversion.src_V.transfer_unit, conversion.src_V.gap, src_process_handle);
  145. cmd_buff[1] = RESULT_SUCCESS.raw;
  146. }
  147. static void SetSendingYUYV(Service::Interface* self) {
  148. u32* cmd_buff = Kernel::GetCommandBuffer();
  149. conversion.src_YUYV.address = cmd_buff[1];
  150. conversion.src_YUYV.image_size = cmd_buff[2];
  151. conversion.src_YUYV.transfer_unit = cmd_buff[3];
  152. conversion.src_YUYV.gap = cmd_buff[4];
  153. u32 src_process_handle = cmd_buff[6];
  154. LOG_DEBUG(Service_Y2R, "called image_size=0x%08X, transfer_unit=%hu, transfer_stride=%hu, "
  155. "src_process_handle=0x%08X", conversion.src_YUYV.image_size,
  156. conversion.src_YUYV.transfer_unit, conversion.src_YUYV.gap, src_process_handle);
  157. cmd_buff[1] = RESULT_SUCCESS.raw;
  158. }
  159. static void SetReceiving(Service::Interface* self) {
  160. u32* cmd_buff = Kernel::GetCommandBuffer();
  161. conversion.dst.address = cmd_buff[1];
  162. conversion.dst.image_size = cmd_buff[2];
  163. conversion.dst.transfer_unit = cmd_buff[3];
  164. conversion.dst.gap = cmd_buff[4];
  165. u32 dst_process_handle = cmd_buff[6];
  166. LOG_DEBUG(Service_Y2R, "called image_size=0x%08X, transfer_unit=%hu, transfer_stride=%hu, "
  167. "dst_process_handle=0x%08X", conversion.dst.image_size,
  168. conversion.dst.transfer_unit, conversion.dst.gap,
  169. dst_process_handle);
  170. cmd_buff[1] = RESULT_SUCCESS.raw;
  171. }
  172. static void SetInputLineWidth(Service::Interface* self) {
  173. u32* cmd_buff = Kernel::GetCommandBuffer();
  174. LOG_DEBUG(Service_Y2R, "called input_line_width=%u", cmd_buff[1]);
  175. cmd_buff[1] = conversion.SetInputLineWidth(cmd_buff[1]).raw;
  176. }
  177. static void SetInputLines(Service::Interface* self) {
  178. u32* cmd_buff = Kernel::GetCommandBuffer();
  179. LOG_DEBUG(Service_Y2R, "called input_line_number=%u", cmd_buff[1]);
  180. cmd_buff[1] = conversion.SetInputLines(cmd_buff[1]).raw;
  181. }
  182. static void SetCoefficient(Service::Interface* self) {
  183. u32* cmd_buff = Kernel::GetCommandBuffer();
  184. const u16* coefficients = reinterpret_cast<const u16*>(&cmd_buff[1]);
  185. std::memcpy(conversion.coefficients.data(), coefficients, sizeof(CoefficientSet));
  186. LOG_DEBUG(Service_Y2R, "called coefficients=[%hX, %hX, %hX, %hX, %hX, %hX, %hX, %hX]",
  187. coefficients[0], coefficients[1], coefficients[2], coefficients[3],
  188. coefficients[4], coefficients[5], coefficients[6], coefficients[7]);
  189. cmd_buff[1] = RESULT_SUCCESS.raw;
  190. }
  191. static void SetStandardCoefficient(Service::Interface* self) {
  192. u32* cmd_buff = Kernel::GetCommandBuffer();
  193. LOG_DEBUG(Service_Y2R, "called standard_coefficient=%u", cmd_buff[1]);
  194. cmd_buff[1] = conversion.SetStandardCoefficient((StandardCoefficient)cmd_buff[1]).raw;
  195. }
  196. static void SetAlpha(Service::Interface* self) {
  197. u32* cmd_buff = Kernel::GetCommandBuffer();
  198. conversion.alpha = cmd_buff[1];
  199. LOG_DEBUG(Service_Y2R, "called alpha=%hu", conversion.alpha);
  200. cmd_buff[1] = RESULT_SUCCESS.raw;
  201. }
  202. static void StartConversion(Service::Interface* self) {
  203. u32* cmd_buff = Kernel::GetCommandBuffer();
  204. HW::Y2R::PerformConversion(conversion);
  205. // dst_image_size would seem to be perfect for this, but it doesn't include the gap :(
  206. u32 total_output_size = conversion.input_lines *
  207. (conversion.dst.transfer_unit + conversion.dst.gap);
  208. VideoCore::g_renderer->hw_rasterizer->NotifyFlush(
  209. Memory::VirtualToPhysicalAddress(conversion.dst.address), total_output_size);
  210. LOG_DEBUG(Service_Y2R, "called");
  211. completion_event->Signal();
  212. cmd_buff[1] = RESULT_SUCCESS.raw;
  213. }
  214. static void StopConversion(Service::Interface* self) {
  215. u32* cmd_buff = Kernel::GetCommandBuffer();
  216. cmd_buff[0] = 0x00270040;
  217. cmd_buff[1] = RESULT_SUCCESS.raw;
  218. LOG_DEBUG(Service_Y2R, "called");
  219. }
  220. /**
  221. * Y2R_U::IsBusyConversion service function
  222. * Outputs:
  223. * 1 : Result of function, 0 on success, otherwise error code
  224. * 2 : 1 if there's a conversion running, otherwise 0.
  225. */
  226. static void IsBusyConversion(Service::Interface* self) {
  227. u32* cmd_buff = Kernel::GetCommandBuffer();
  228. cmd_buff[1] = RESULT_SUCCESS.raw;
  229. cmd_buff[2] = 0; // StartConversion always finishes immediately
  230. LOG_DEBUG(Service_Y2R, "called");
  231. }
  232. /**
  233. * Y2R_U::SetConversionParams service function
  234. */
  235. static void SetConversionParams(Service::Interface* self) {
  236. u32* cmd_buff = Kernel::GetCommandBuffer();
  237. auto params = reinterpret_cast<const ConversionParameters*>(&cmd_buff[1]);
  238. LOG_DEBUG(Service_Y2R,
  239. "called input_format=%hhu output_format=%hhu rotation=%hhu block_alignment=%hhu "
  240. "input_line_width=%hu input_lines=%hu standard_coefficient=%hhu "
  241. "reserved=%hhu alpha=%hX",
  242. params->input_format, params->output_format, params->rotation, params->block_alignment,
  243. params->input_line_width, params->input_lines, params->standard_coefficient,
  244. params->reserved, params->alpha);
  245. ResultCode result = RESULT_SUCCESS;
  246. conversion.input_format = params->input_format;
  247. conversion.output_format = params->output_format;
  248. conversion.rotation = params->rotation;
  249. conversion.block_alignment = params->block_alignment;
  250. result = conversion.SetInputLineWidth(params->input_line_width);
  251. if (result.IsError()) goto cleanup;
  252. result = conversion.SetInputLines(params->input_lines);
  253. if (result.IsError()) goto cleanup;
  254. result = conversion.SetStandardCoefficient(params->standard_coefficient);
  255. if (result.IsError()) goto cleanup;
  256. conversion.alpha = params->alpha;
  257. cleanup:
  258. cmd_buff[0] = 0x00290040; // TODO verify
  259. cmd_buff[1] = result.raw;
  260. }
  261. static void PingProcess(Service::Interface* self) {
  262. u32* cmd_buff = Kernel::GetCommandBuffer();
  263. cmd_buff[1] = RESULT_SUCCESS.raw;
  264. cmd_buff[2] = 0;
  265. LOG_WARNING(Service_Y2R, "(STUBBED) called");
  266. }
  267. static void DriverInitialize(Service::Interface* self) {
  268. u32* cmd_buff = Kernel::GetCommandBuffer();
  269. conversion.input_format = InputFormat::YUV422_Indiv8;
  270. conversion.output_format = OutputFormat::RGBA8;
  271. conversion.rotation = Rotation::None;
  272. conversion.block_alignment = BlockAlignment::Linear;
  273. conversion.coefficients.fill(0);
  274. conversion.SetInputLineWidth(1024);
  275. conversion.SetInputLines(1024);
  276. conversion.alpha = 0;
  277. ConversionBuffer zero_buffer = {};
  278. conversion.src_Y = zero_buffer;
  279. conversion.src_U = zero_buffer;
  280. conversion.src_V = zero_buffer;
  281. conversion.dst = zero_buffer;
  282. completion_event->Clear();
  283. cmd_buff[0] = 0x002B0040;
  284. cmd_buff[1] = RESULT_SUCCESS.raw;
  285. LOG_DEBUG(Service_Y2R, "called");
  286. }
  287. static void DriverFinalize(Service::Interface* self) {
  288. u32* cmd_buff = Kernel::GetCommandBuffer();
  289. cmd_buff[0] = 0x002C0040;
  290. cmd_buff[1] = RESULT_SUCCESS.raw;
  291. LOG_DEBUG(Service_Y2R, "called");
  292. }
  293. const Interface::FunctionInfo FunctionTable[] = {
  294. {0x00010040, SetInputFormat, "SetInputFormat"},
  295. {0x00030040, SetOutputFormat, "SetOutputFormat"},
  296. {0x00050040, SetRotation, "SetRotation"},
  297. {0x00070040, SetBlockAlignment, "SetBlockAlignment"},
  298. {0x000D0040, SetTransferEndInterrupt, "SetTransferEndInterrupt"},
  299. {0x000F0000, GetTransferEndEvent, "GetTransferEndEvent"},
  300. {0x00100102, SetSendingY, "SetSendingY"},
  301. {0x00110102, SetSendingU, "SetSendingU"},
  302. {0x00120102, SetSendingV, "SetSendingV"},
  303. {0x00130102, SetSendingYUYV, "SetSendingYUYV"},
  304. {0x00180102, SetReceiving, "SetReceiving"},
  305. {0x001A0040, SetInputLineWidth, "SetInputLineWidth"},
  306. {0x001C0040, SetInputLines, "SetInputLines"},
  307. {0x001E0100, SetCoefficient, "SetCoefficient"},
  308. {0x00200040, SetStandardCoefficient, "SetStandardCoefficient"},
  309. {0x00220040, SetAlpha, "SetAlpha"},
  310. {0x00260000, StartConversion, "StartConversion"},
  311. {0x00270000, StopConversion, "StopConversion"},
  312. {0x00280000, IsBusyConversion, "IsBusyConversion"},
  313. {0x002901C0, SetConversionParams, "SetConversionParams"},
  314. {0x002A0000, PingProcess, "PingProcess"},
  315. {0x002B0000, DriverInitialize, "DriverInitialize"},
  316. {0x002C0000, DriverFinalize, "DriverFinalize"},
  317. };
  318. ////////////////////////////////////////////////////////////////////////////////////////////////////
  319. // Interface class
  320. Interface::Interface() {
  321. completion_event = Kernel::Event::Create(RESETTYPE_ONESHOT, "Y2R:Completed");
  322. std::memset(&conversion, 0, sizeof(conversion));
  323. Register(FunctionTable);
  324. }
  325. } // namespace