adf.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. * Copyright (C) 2013 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <dirent.h>
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <malloc.h>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <algorithm>
  25. #include <memory>
  26. #include <vector>
  27. #include <linux/limits.h>
  28. #include <sys/ioctl.h>
  29. #include <adf/adf.h>
  30. #define ADF_BASE_PATH "/dev/"
  31. static ssize_t adf_id_vector_to_array(const std::vector<adf_id_t> &in,
  32. adf_id_t **out)
  33. {
  34. auto size = sizeof(in[0]) * in.size();
  35. // We can't use new[] since the existing API says the caller should free()
  36. // the returned array
  37. auto ret = static_cast<adf_id_t *>(malloc(size));
  38. if (!ret)
  39. return -ENOMEM;
  40. std::copy(in.begin(), in.end(), ret);
  41. *out = ret;
  42. return in.size();
  43. }
  44. static ssize_t adf_find_nodes(const char *pattern, adf_id_t **ids_out)
  45. {
  46. struct dirent *dirent;
  47. std::unique_ptr<DIR, decltype(&closedir)>
  48. dir{opendir(ADF_BASE_PATH), closedir};
  49. if (!dir)
  50. return -errno;
  51. std::vector<adf_id_t> ids;
  52. errno = 0;
  53. while ((dirent = readdir(dir.get()))) {
  54. adf_id_t id;
  55. int matched = sscanf(dirent->d_name, pattern, &id);
  56. if (matched < 0)
  57. return -errno;
  58. else if (matched == 1)
  59. ids.push_back(id);
  60. }
  61. if (errno)
  62. return -errno;
  63. return adf_id_vector_to_array(ids, ids_out);
  64. }
  65. ssize_t adf_devices(adf_id_t **ids)
  66. {
  67. return adf_find_nodes("adf%u", ids);
  68. }
  69. int adf_device_open(adf_id_t id, int flags, struct adf_device *dev)
  70. {
  71. char filename[64];
  72. dev->id = id;
  73. snprintf(filename, sizeof(filename), ADF_BASE_PATH "adf%u", id);
  74. dev->fd = open(filename, flags);
  75. if (dev->fd < 0)
  76. return -errno;
  77. return 0;
  78. }
  79. void adf_device_close(struct adf_device *dev)
  80. {
  81. if (dev->fd >= 0)
  82. close(dev->fd);
  83. }
  84. int adf_get_device_data(struct adf_device *dev, struct adf_device_data *data)
  85. {
  86. int err;
  87. int ret = 0;
  88. memset(data, 0, sizeof(*data));
  89. err = ioctl(dev->fd, ADF_GET_DEVICE_DATA, data);
  90. if (err < 0)
  91. return -ENOMEM;
  92. if (data->n_attachments)
  93. data->attachments = new adf_attachment_config[data->n_attachments];
  94. if (data->n_allowed_attachments)
  95. data->allowed_attachments =
  96. new adf_attachment_config[data->n_allowed_attachments];
  97. if (data->custom_data_size)
  98. data->custom_data = new char[data->custom_data_size];
  99. err = ioctl(dev->fd, ADF_GET_DEVICE_DATA, data);
  100. if (err < 0) {
  101. ret = -errno;
  102. adf_free_device_data(data);
  103. }
  104. return ret;
  105. }
  106. void adf_free_device_data(struct adf_device_data *data)
  107. {
  108. delete [] data->attachments;
  109. data->attachments = nullptr;
  110. delete [] data->allowed_attachments;
  111. data->allowed_attachments = nullptr;
  112. delete [] static_cast<char *>(data->custom_data);
  113. data->custom_data = nullptr;
  114. }
  115. int adf_device_post(struct adf_device *dev,
  116. adf_id_t *interfaces, size_t n_interfaces,
  117. struct adf_buffer_config *bufs, size_t n_bufs,
  118. void *custom_data, size_t custom_data_size)
  119. {
  120. int err;
  121. struct adf_post_config data;
  122. memset(&data, 0, sizeof(data));
  123. data.interfaces = interfaces;
  124. data.n_interfaces = n_interfaces;
  125. data.bufs = bufs;
  126. data.n_bufs = n_bufs;
  127. data.custom_data = custom_data;
  128. data.custom_data_size = custom_data_size;
  129. err = ioctl(dev->fd, ADF_POST_CONFIG, &data);
  130. if (err < 0)
  131. return -errno;
  132. return (int)data.complete_fence;
  133. }
  134. int adf_device_post_v2(struct adf_device *dev,
  135. adf_id_t *interfaces, __u32 n_interfaces,
  136. struct adf_buffer_config *bufs, __u32 n_bufs,
  137. void *custom_data, __u64 custom_data_size,
  138. enum adf_complete_fence_type complete_fence_type,
  139. int *complete_fence)
  140. {
  141. int err;
  142. struct adf_post_config_v2 data;
  143. memset(&data, 0, sizeof(data));
  144. data.interfaces = (uintptr_t)interfaces;
  145. data.n_interfaces = n_interfaces;
  146. data.bufs = (uintptr_t)bufs;
  147. data.n_bufs = n_bufs;
  148. data.custom_data = (uintptr_t)custom_data;
  149. data.custom_data_size = custom_data_size;
  150. data.complete_fence_type = complete_fence_type;
  151. err = ioctl(dev->fd, ADF_POST_CONFIG_V2, &data);
  152. if (err < 0)
  153. return -errno;
  154. if (complete_fence)
  155. *complete_fence = data.complete_fence;
  156. else if (data.complete_fence >= 0)
  157. close(data.complete_fence);
  158. return 0;
  159. }
  160. static int adf_device_attachment(struct adf_device *dev,
  161. adf_id_t overlay_engine, adf_id_t interface, bool attach)
  162. {
  163. int err;
  164. struct adf_attachment_config data;
  165. memset(&data, 0, sizeof(data));
  166. data.overlay_engine = overlay_engine;
  167. data.interface = interface;
  168. err = ioctl(dev->fd, attach ? ADF_ATTACH : ADF_DETACH, &data);
  169. if (err < 0)
  170. return -errno;
  171. return 0;
  172. }
  173. int adf_device_attach(struct adf_device *dev, adf_id_t overlay_engine,
  174. adf_id_t interface)
  175. {
  176. return adf_device_attachment(dev, overlay_engine, interface, true);
  177. }
  178. int adf_device_detach(struct adf_device *dev, adf_id_t overlay_engine,
  179. adf_id_t interface)
  180. {
  181. return adf_device_attachment(dev, overlay_engine, interface, false);
  182. }
  183. ssize_t adf_interfaces(struct adf_device *dev, adf_id_t **interfaces)
  184. {
  185. char pattern[64];
  186. snprintf(pattern, sizeof(pattern), "adf-interface%u.%%u", dev->id);
  187. return adf_find_nodes(pattern, interfaces);
  188. }
  189. ssize_t adf_interfaces_for_overlay_engine(struct adf_device *dev,
  190. adf_id_t overlay_engine, adf_id_t **interfaces)
  191. {
  192. struct adf_device_data data;
  193. auto err = adf_get_device_data(dev, &data);
  194. if (err < 0)
  195. return err;
  196. std::vector<adf_id_t> ids;
  197. if (data.allowed_attachments != nullptr)
  198. for (size_t i = 0; i < data.n_allowed_attachments; i++)
  199. if (data.allowed_attachments[i].overlay_engine == overlay_engine)
  200. ids.push_back(data.allowed_attachments[i].interface);
  201. adf_free_device_data(&data);
  202. return adf_id_vector_to_array(ids, interfaces);
  203. }
  204. static ssize_t adf_interfaces_filter(struct adf_device *dev,
  205. adf_id_t *in, size_t n_in, adf_id_t **out,
  206. bool (*filter)(struct adf_interface_data *data, __u32 match),
  207. __u32 match)
  208. {
  209. std::vector<adf_id_t> ids;
  210. for (size_t i = 0; i < n_in; i++) {
  211. int fd = adf_interface_open(dev, in[i], O_RDONLY);
  212. if (fd < 0)
  213. return fd;
  214. struct adf_interface_data data;
  215. auto ret = adf_get_interface_data(fd, &data);
  216. close(fd);
  217. if (ret < 0)
  218. return ret;
  219. if (filter(&data, match))
  220. ids.push_back(in[i]);
  221. }
  222. return adf_id_vector_to_array(ids, out);
  223. }
  224. static bool adf_interface_type_filter(struct adf_interface_data *data,
  225. __u32 type)
  226. {
  227. return data->type == (enum adf_interface_type)type;
  228. }
  229. ssize_t adf_interfaces_filter_by_type(struct adf_device *dev,
  230. enum adf_interface_type type,
  231. adf_id_t *in, size_t n_in, adf_id_t **out)
  232. {
  233. return adf_interfaces_filter(dev, in, n_in, out, adf_interface_type_filter,
  234. type);
  235. }
  236. static bool adf_interface_flags_filter(struct adf_interface_data *data,
  237. __u32 flag)
  238. {
  239. return !!(data->flags & flag);
  240. }
  241. ssize_t adf_interfaces_filter_by_flag(struct adf_device *dev, __u32 flag,
  242. adf_id_t *in, size_t n_in, adf_id_t **out)
  243. {
  244. return adf_interfaces_filter(dev, in, n_in, out, adf_interface_flags_filter,
  245. flag);
  246. }
  247. int adf_interface_open(struct adf_device *dev, adf_id_t id, int flags)
  248. {
  249. char filename[64];
  250. snprintf(filename, sizeof(filename), ADF_BASE_PATH "adf-interface%u.%u",
  251. dev->id, id);
  252. int fd = open(filename, flags);
  253. if (fd < 0)
  254. return -errno;
  255. return fd;
  256. }
  257. int adf_get_interface_data(int fd, struct adf_interface_data *data)
  258. {
  259. int err;
  260. int ret = 0;
  261. memset(data, 0, sizeof(*data));
  262. err = ioctl(fd, ADF_GET_INTERFACE_DATA, data);
  263. if (err < 0)
  264. return -errno;
  265. if (data->n_available_modes)
  266. data->available_modes = new drm_mode_modeinfo[data->n_available_modes];
  267. if (data->custom_data_size)
  268. data->custom_data = new char[data->custom_data_size];
  269. err = ioctl(fd, ADF_GET_INTERFACE_DATA, data);
  270. if (err < 0) {
  271. ret = -errno;
  272. adf_free_interface_data(data);
  273. }
  274. return ret;
  275. }
  276. void adf_free_interface_data(struct adf_interface_data *data)
  277. {
  278. delete [] data->available_modes;
  279. delete [] static_cast<char *>(data->custom_data);
  280. }
  281. int adf_interface_blank(int fd, __u8 mode)
  282. {
  283. int err = ioctl(fd, ADF_BLANK, mode);
  284. if (err < 0)
  285. return -errno;
  286. return 0;
  287. }
  288. int adf_interface_set_mode(int fd, struct drm_mode_modeinfo *mode)
  289. {
  290. int err = ioctl(fd, ADF_SET_MODE, mode);
  291. if (err < 0)
  292. return -errno;
  293. return 0;
  294. }
  295. int adf_interface_simple_buffer_alloc(int fd, __u32 w, __u32 h,
  296. __u32 format, __u32 *offset, __u32 *pitch)
  297. {
  298. int err;
  299. struct adf_simple_buffer_alloc data;
  300. memset(&data, 0, sizeof(data));
  301. data.w = w;
  302. data.h = h;
  303. data.format = format;
  304. err = ioctl(fd, ADF_SIMPLE_BUFFER_ALLOC, &data);
  305. if (err < 0)
  306. return -errno;
  307. *offset = data.offset;
  308. *pitch = data.pitch;
  309. return (int)data.fd;
  310. }
  311. static void adf_interface_simple_post_config_buf(struct adf_buffer_config *buf,
  312. __u32 overlay_engine, __u32 w, __u32 h, __u32 format, int buf_fd,
  313. __u32 offset, __u32 pitch, int acquire_fence)
  314. {
  315. buf->overlay_engine = overlay_engine;
  316. buf->w = w;
  317. buf->h = h;
  318. buf->format = format;
  319. buf->fd[0] = buf_fd;
  320. buf->offset[0] = offset;
  321. buf->pitch[0] = pitch;
  322. buf->n_planes = 1;
  323. buf->acquire_fence = acquire_fence;
  324. }
  325. int adf_interface_simple_post(int fd, __u32 overlay_engine,
  326. __u32 w, __u32 h, __u32 format, int buf_fd, __u32 offset,
  327. __u32 pitch, int acquire_fence)
  328. {
  329. int ret;
  330. struct adf_simple_post_config data;
  331. memset(&data, 0, sizeof(data));
  332. adf_interface_simple_post_config_buf(&data.buf, overlay_engine, w, h, format,
  333. buf_fd, offset, pitch, acquire_fence);
  334. ret = ioctl(fd, ADF_SIMPLE_POST_CONFIG, &data);
  335. if (ret < 0)
  336. return -errno;
  337. return (int)data.complete_fence;
  338. }
  339. int adf_interface_simple_post_v2(int fd, adf_id_t overlay_engine,
  340. __u32 w, __u32 h, __u32 format, int buf_fd, __u32 offset,
  341. __u32 pitch, int acquire_fence,
  342. enum adf_complete_fence_type complete_fence_type,
  343. int *complete_fence)
  344. {
  345. int ret;
  346. struct adf_simple_post_config_v2 data;
  347. memset(&data, 0, sizeof(data));
  348. adf_interface_simple_post_config_buf(&data.buf, overlay_engine, w, h, format,
  349. buf_fd, offset, pitch, acquire_fence);
  350. data.complete_fence_type = complete_fence_type;
  351. ret = ioctl(fd, ADF_SIMPLE_POST_CONFIG_V2, &data);
  352. if (ret < 0)
  353. return -errno;
  354. if (complete_fence)
  355. *complete_fence = data.complete_fence;
  356. else if (data.complete_fence >= 0)
  357. close(data.complete_fence);
  358. return 0;
  359. }
  360. ssize_t adf_overlay_engines(struct adf_device *dev, adf_id_t **overlay_engines)
  361. {
  362. char pattern[64];
  363. snprintf(pattern, sizeof(pattern), "adf-overlay-engine%u.%%u", dev->id);
  364. return adf_find_nodes(pattern, overlay_engines);
  365. }
  366. ssize_t adf_overlay_engines_for_interface(struct adf_device *dev,
  367. adf_id_t interface, adf_id_t **overlay_engines)
  368. {
  369. struct adf_device_data data;
  370. auto err = adf_get_device_data(dev, &data);
  371. if (err < 0)
  372. return err;
  373. std::vector<adf_id_t> ids;
  374. if (data.allowed_attachments != nullptr)
  375. for (size_t i = 0; i < data.n_allowed_attachments; i++)
  376. if (data.allowed_attachments[i].interface == interface)
  377. ids.push_back(data.allowed_attachments[i].overlay_engine);
  378. return adf_id_vector_to_array(ids, overlay_engines);
  379. }
  380. static ssize_t adf_overlay_engines_filter(struct adf_device *dev,
  381. adf_id_t *in, size_t n_in, adf_id_t **out,
  382. bool (*filter)(struct adf_overlay_engine_data *data, void *cookie),
  383. void *cookie)
  384. {
  385. std::vector<adf_id_t> ids;
  386. size_t i;
  387. for (i = 0; i < n_in; i++) {
  388. int fd = adf_overlay_engine_open(dev, in[i], O_RDONLY);
  389. if (fd < 0)
  390. return fd;
  391. struct adf_overlay_engine_data data;
  392. auto ret = adf_get_overlay_engine_data(fd, &data);
  393. close(fd);
  394. if (ret < 0)
  395. return ret;
  396. if (filter(&data, cookie))
  397. ids.push_back(in[i]);
  398. }
  399. return adf_id_vector_to_array(ids, out);
  400. }
  401. struct format_filter_cookie {
  402. const __u32 *formats;
  403. size_t n_formats;
  404. };
  405. static bool adf_overlay_engine_format_filter(
  406. struct adf_overlay_engine_data *data, void *cookie)
  407. {
  408. auto c = static_cast<format_filter_cookie *>(cookie);
  409. size_t i;
  410. for (i = 0; i < data->n_supported_formats; i++) {
  411. size_t j;
  412. for (j = 0; j < c->n_formats; j++)
  413. if (data->supported_formats[i] == c->formats[j])
  414. return true;
  415. }
  416. return false;
  417. }
  418. ssize_t adf_overlay_engines_filter_by_format(struct adf_device *dev,
  419. const __u32 *formats, size_t n_formats, adf_id_t *in, size_t n_in,
  420. adf_id_t **out)
  421. {
  422. struct format_filter_cookie cookie = { formats, n_formats };
  423. return adf_overlay_engines_filter(dev, in, n_in, out,
  424. adf_overlay_engine_format_filter, &cookie);
  425. }
  426. int adf_overlay_engine_open(struct adf_device *dev, adf_id_t id, int flags)
  427. {
  428. char filename[64];
  429. snprintf(filename, sizeof(filename),
  430. ADF_BASE_PATH "adf-overlay-engine%u.%u", dev->id, id);
  431. int fd = open(filename, flags);
  432. if (fd < 0)
  433. return -errno;
  434. return fd;
  435. }
  436. int adf_get_overlay_engine_data(int fd, struct adf_overlay_engine_data *data)
  437. {
  438. int err;
  439. int ret = 0;
  440. memset(data, 0, sizeof(*data));
  441. err = ioctl(fd, ADF_GET_OVERLAY_ENGINE_DATA, data);
  442. if (err < 0)
  443. return -errno;
  444. if (data->n_supported_formats)
  445. data->supported_formats = new __u32[data->n_supported_formats];
  446. if (data->custom_data_size)
  447. data->custom_data = new char[data->custom_data_size];
  448. err = ioctl(fd, ADF_GET_OVERLAY_ENGINE_DATA, data);
  449. if (err < 0) {
  450. ret = -errno;
  451. adf_free_overlay_engine_data(data);
  452. }
  453. return ret;
  454. }
  455. void adf_free_overlay_engine_data(struct adf_overlay_engine_data *data)
  456. {
  457. delete [] data->supported_formats;
  458. data->supported_formats = nullptr;
  459. delete [] static_cast<char *>(data->custom_data);
  460. data->custom_data = nullptr;
  461. }
  462. bool adf_overlay_engine_supports_format(int fd, __u32 format)
  463. {
  464. struct adf_overlay_engine_data data;
  465. bool ret = false;
  466. size_t i;
  467. int err = adf_get_overlay_engine_data(fd, &data);
  468. if (err < 0)
  469. return false;
  470. if (data.supported_formats != nullptr) {
  471. for (i = 0; i < data.n_supported_formats; i++) {
  472. if (data.supported_formats[i] == format) {
  473. ret = true;
  474. break;
  475. }
  476. }
  477. }
  478. adf_free_overlay_engine_data(&data);
  479. return ret;
  480. }
  481. int adf_set_event(int fd, enum adf_event_type type, bool enabled)
  482. {
  483. struct adf_set_event data;
  484. data.type = type;
  485. data.enabled = enabled;
  486. int err = ioctl(fd, ADF_SET_EVENT, &data);
  487. if (err < 0)
  488. return -errno;
  489. return 0;
  490. }
  491. int adf_read_event(int fd, struct adf_event **event)
  492. {
  493. struct adf_event header;
  494. struct event_with_data {
  495. struct adf_event base;
  496. uint8_t data[0];
  497. };
  498. using unique_event = std::unique_ptr<event_with_data, decltype(&free)>;
  499. size_t data_size;
  500. int err = read(fd, &header, sizeof(header));
  501. if (err < 0)
  502. return -errno;
  503. if ((size_t)err < sizeof(header))
  504. return -EIO;
  505. if (header.length < sizeof(header))
  506. return -EIO;
  507. // Again, we can't use new[] since the existing API says the caller should
  508. // free() the returned event
  509. auto event_ptr = static_cast<event_with_data *>(malloc(header.length));
  510. unique_event event_ret{event_ptr, free};
  511. if (!event_ret)
  512. return -ENOMEM;
  513. data_size = header.length - sizeof(header);
  514. memcpy(event_ret.get(), &header, sizeof(header));
  515. ssize_t read_size = read(fd, &event_ret->data, data_size);
  516. if (read_size < 0)
  517. return -errno;
  518. if ((size_t)read_size < data_size)
  519. return -EIO;
  520. *event = &event_ret.release()->base;
  521. return 0;
  522. }
  523. void adf_format_str(__u32 format, char buf[ADF_FORMAT_STR_SIZE])
  524. {
  525. buf[0] = format & 0xFF;
  526. buf[1] = (format >> 8) & 0xFF;
  527. buf[2] = (format >> 16) & 0xFF;
  528. buf[3] = (format >> 24) & 0xFF;
  529. buf[4] = '\0';
  530. }
  531. static bool adf_find_simple_post_overlay_engine(struct adf_device *dev,
  532. const __u32 *formats, size_t n_formats,
  533. adf_id_t interface, adf_id_t *overlay_engine)
  534. {
  535. adf_id_t *engs = nullptr;
  536. ssize_t n_engs = adf_overlay_engines_for_interface(dev, interface, &engs);
  537. if (engs == nullptr)
  538. return false;
  539. adf_id_t *filtered_engs = nullptr;
  540. ssize_t n_filtered_engs = adf_overlay_engines_filter_by_format(dev,
  541. formats, n_formats, engs, n_engs, &filtered_engs);
  542. free(engs);
  543. if (filtered_engs == nullptr)
  544. return false;
  545. *overlay_engine = filtered_engs[0];
  546. free(filtered_engs);
  547. return true;
  548. }
  549. static const __u32 any_rgb_format[] = {
  550. DRM_FORMAT_C8,
  551. DRM_FORMAT_RGB332,
  552. DRM_FORMAT_BGR233,
  553. DRM_FORMAT_XRGB1555,
  554. DRM_FORMAT_XBGR1555,
  555. DRM_FORMAT_RGBX5551,
  556. DRM_FORMAT_BGRX5551,
  557. DRM_FORMAT_ARGB1555,
  558. DRM_FORMAT_ABGR1555,
  559. DRM_FORMAT_RGBA5551,
  560. DRM_FORMAT_BGRA5551,
  561. DRM_FORMAT_RGB565,
  562. DRM_FORMAT_BGR565,
  563. DRM_FORMAT_RGB888,
  564. DRM_FORMAT_BGR888,
  565. DRM_FORMAT_XRGB8888,
  566. DRM_FORMAT_XBGR8888,
  567. DRM_FORMAT_RGBX8888,
  568. DRM_FORMAT_BGRX8888,
  569. DRM_FORMAT_XRGB2101010,
  570. DRM_FORMAT_XBGR2101010,
  571. DRM_FORMAT_RGBX1010102,
  572. DRM_FORMAT_BGRX1010102,
  573. DRM_FORMAT_ARGB2101010,
  574. DRM_FORMAT_ABGR2101010,
  575. DRM_FORMAT_RGBA1010102,
  576. DRM_FORMAT_BGRA1010102,
  577. DRM_FORMAT_ARGB8888,
  578. DRM_FORMAT_ABGR8888,
  579. DRM_FORMAT_RGBA8888,
  580. DRM_FORMAT_BGRA8888,
  581. };
  582. int adf_find_simple_post_configuration(struct adf_device *dev,
  583. const __u32 *formats, size_t n_formats,
  584. adf_id_t *interface, adf_id_t *overlay_engine)
  585. {
  586. adf_id_t *intfs = NULL;
  587. ssize_t n_intfs = adf_interfaces(dev, &intfs);
  588. if (n_intfs < 0)
  589. return n_intfs;
  590. else if (!intfs)
  591. return -ENODEV;
  592. adf_id_t *primary_intfs = nullptr;
  593. ssize_t n_primary_intfs = adf_interfaces_filter_by_flag(dev,
  594. ADF_INTF_FLAG_PRIMARY, intfs, n_intfs, &primary_intfs);
  595. free(intfs);
  596. if (n_primary_intfs < 0)
  597. return n_primary_intfs;
  598. else if (!primary_intfs)
  599. return -ENODEV;
  600. if (!formats) {
  601. formats = any_rgb_format;
  602. n_formats = sizeof(any_rgb_format) / sizeof(any_rgb_format[0]);
  603. }
  604. bool found = false;
  605. ssize_t i = 0;
  606. for (i = 0; i < n_primary_intfs; i++) {
  607. found = adf_find_simple_post_overlay_engine(dev, formats, n_formats,
  608. primary_intfs[i], overlay_engine);
  609. if (found) {
  610. *interface = primary_intfs[i];
  611. break;
  612. }
  613. }
  614. free(primary_intfs);
  615. if (!found)
  616. return -ENODEV;
  617. return 0;
  618. }