2
0

drm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /*
  2. * Copyright (C) 2018 Paul Kocialkowski <paul.kocialkowski@bootlin.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <errno.h>
  18. #include <fcntl.h>
  19. #include <stdbool.h>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <sys/ioctl.h>
  25. #include <sys/mman.h>
  26. #include <sys/stat.h>
  27. #include <sys/types.h>
  28. #include <unistd.h>
  29. #include <drm_fourcc.h>
  30. #include <xf86drm.h>
  31. #include <xf86drmMode.h>
  32. #include "v4l2-request-test.h"
  33. static int create_dumb_buffer(int drm_fd, unsigned int width,
  34. unsigned int height, unsigned int bpp,
  35. struct gem_buffer *buffer)
  36. {
  37. struct drm_mode_create_dumb create_dumb;
  38. int rc;
  39. memset(&create_dumb, 0, sizeof(create_dumb));
  40. create_dumb.width = width;
  41. create_dumb.height = height;
  42. create_dumb.bpp = bpp;
  43. rc = drmIoctl(drm_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
  44. if (rc < 0) {
  45. fprintf(stderr, "Unable to create dumb buffer: %s\n",
  46. strerror(errno));
  47. return -1;
  48. }
  49. buffer->size = create_dumb.size;
  50. buffer->pitches[0] = create_dumb.pitch;
  51. buffer->offsets[0] = 0;
  52. buffer->handles[0] = create_dumb.handle;
  53. return 0;
  54. }
  55. static int create_imported_buffer(int drm_fd, int *import_fds,
  56. unsigned int import_fds_count,
  57. unsigned int *offsets, unsigned int *pitches,
  58. struct gem_buffer *buffer)
  59. {
  60. uint32_t handles[4];
  61. unsigned int i;
  62. int rc;
  63. memset(buffer->handles, 0, sizeof(buffer->handles));
  64. memset(buffer->pitches, 0, sizeof(buffer->pitches));
  65. memset(buffer->offsets, 0, sizeof(buffer->offsets));
  66. for (i = 0; i < import_fds_count; i++) {
  67. rc = drmPrimeFDToHandle(drm_fd, import_fds[i], &handles[i]);
  68. if (rc < 0) {
  69. fprintf(stderr, "Unable to create imported buffer: %s\n",
  70. strerror(errno));
  71. return -1;
  72. }
  73. }
  74. for (i = 0; i < buffer->planes_count; i++) {
  75. buffer->handles[i] = import_fds_count == 1 ? handles[0] :
  76. handles[i];
  77. buffer->pitches[i] = pitches[i];
  78. buffer->offsets[i] = offsets[i];
  79. }
  80. return 0;
  81. }
  82. static int destroy_buffer(int drm_fd, struct gem_buffer *buffer)
  83. {
  84. struct drm_mode_destroy_dumb destroy_dumb;
  85. int rc;
  86. if (buffer == NULL)
  87. return -1;
  88. memset(&destroy_dumb, 0, sizeof(destroy_dumb));
  89. destroy_dumb.handle = buffer->handles[0];
  90. rc = drmIoctl(drm_fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
  91. if (rc < 0) {
  92. fprintf(stderr, "Unable to destroy buffer: %s\n",
  93. strerror(errno));
  94. return -1;
  95. }
  96. return 0;
  97. }
  98. static int close_buffer(int drm_fd, struct gem_buffer *buffer)
  99. {
  100. struct drm_gem_close gem_close;
  101. int rc;
  102. memset(&gem_close, 0, sizeof(gem_close));
  103. gem_close.handle = buffer->handles[0];
  104. rc = drmIoctl(drm_fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
  105. if (rc < 0) {
  106. fprintf(stderr, "Unable to close buffer: %s\n",
  107. strerror(errno));
  108. return -1;
  109. }
  110. return 0;
  111. }
  112. static int map_buffer(int drm_fd, struct gem_buffer *buffer)
  113. {
  114. struct drm_mode_map_dumb map_dumb;
  115. void *data;
  116. int rc;
  117. if (buffer == NULL)
  118. return -1;
  119. memset(&map_dumb, 0, sizeof(map_dumb));
  120. map_dumb.handle = buffer->handles[0];
  121. rc = drmIoctl(drm_fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
  122. if (rc < 0) {
  123. fprintf(stderr, "Unable to map buffer: %s\n", strerror(errno));
  124. return -1;
  125. }
  126. data = mmap(0, buffer->size, PROT_READ | PROT_WRITE, MAP_SHARED, drm_fd,
  127. map_dumb.offset);
  128. if (data == MAP_FAILED) {
  129. fprintf(stderr, "Unable to mmap buffer: %s\n", strerror(errno));
  130. return -1;
  131. }
  132. buffer->data = data;
  133. return 0;
  134. }
  135. static int unmap_buffer(int drm_fd, struct gem_buffer *buffer)
  136. {
  137. if (buffer == NULL)
  138. return -1;
  139. munmap(buffer->data, buffer->size);
  140. return 0;
  141. }
  142. static int add_framebuffer(int drm_fd, struct gem_buffer *buffer,
  143. unsigned int width, unsigned int height,
  144. unsigned int format, uint64_t modifier)
  145. {
  146. uint64_t modifiers[4] = { 0, 0, 0, 0 };
  147. uint32_t flags = 0;
  148. unsigned int id;
  149. unsigned int i;
  150. int rc;
  151. for (i = 0; i < buffer->planes_count; i++) {
  152. if (buffer->handles[i] != 0 &&
  153. modifier != DRM_FORMAT_MOD_NONE) {
  154. flags |= DRM_MODE_FB_MODIFIERS;
  155. modifiers[i] = modifier;
  156. }
  157. }
  158. rc = drmModeAddFB2WithModifiers(drm_fd, width, height, format,
  159. buffer->handles, buffer->pitches,
  160. buffer->offsets, modifiers, &id, flags);
  161. if (rc < 0) {
  162. fprintf(stderr, "Unable to add framebuffer for plane: %s\n",
  163. strerror(errno));
  164. return -1;
  165. }
  166. buffer->framebuffer_id = id;
  167. return 0;
  168. }
  169. static int discover_properties(int drm_fd, int connector_id, int crtc_id,
  170. int plane_id, struct display_properties_ids *ids)
  171. {
  172. drmModeObjectPropertiesPtr properties = NULL;
  173. drmModePropertyPtr property = NULL;
  174. struct {
  175. uint32_t object_type;
  176. uint32_t object_id;
  177. char *name;
  178. uint32_t *value;
  179. } glue[] = {
  180. { DRM_MODE_OBJECT_CONNECTOR, connector_id, "CRTC_ID", &ids->connector_crtc_id },
  181. { DRM_MODE_OBJECT_CRTC, crtc_id, "MODE_ID", &ids->crtc_mode_id },
  182. { DRM_MODE_OBJECT_CRTC, crtc_id, "ACTIVE", &ids->crtc_active },
  183. { DRM_MODE_OBJECT_PLANE, plane_id, "FB_ID", &ids->plane_fb_id },
  184. { DRM_MODE_OBJECT_PLANE, plane_id, "CRTC_ID", &ids->plane_crtc_id },
  185. { DRM_MODE_OBJECT_PLANE, plane_id, "SRC_X", &ids->plane_src_x },
  186. { DRM_MODE_OBJECT_PLANE, plane_id, "SRC_Y", &ids->plane_src_y },
  187. { DRM_MODE_OBJECT_PLANE, plane_id, "SRC_W", &ids->plane_src_w },
  188. { DRM_MODE_OBJECT_PLANE, plane_id, "SRC_H", &ids->plane_src_h },
  189. { DRM_MODE_OBJECT_PLANE, plane_id, "CRTC_X", &ids->plane_crtc_x },
  190. { DRM_MODE_OBJECT_PLANE, plane_id, "CRTC_Y", &ids->plane_crtc_y },
  191. { DRM_MODE_OBJECT_PLANE, plane_id, "CRTC_W", &ids->plane_crtc_w },
  192. { DRM_MODE_OBJECT_PLANE, plane_id, "CRTC_H", &ids->plane_crtc_h },
  193. { DRM_MODE_OBJECT_PLANE, plane_id, "zpos", &ids->plane_zpos },
  194. };
  195. unsigned int i, j;
  196. int rc;
  197. for (i = 0; i < ARRAY_SIZE(glue); i++) {
  198. properties = drmModeObjectGetProperties(drm_fd,
  199. glue[i].object_id,
  200. glue[i].object_type);
  201. if (properties == NULL) {
  202. fprintf(stderr, "Unable to get DRM properties: %s\n",
  203. strerror(errno));
  204. goto error;
  205. }
  206. for (j = 0; j < properties->count_props; j++) {
  207. property = drmModeGetProperty(drm_fd,
  208. properties->props[j]);
  209. if (property == NULL) {
  210. fprintf(stderr, "Unable to get DRM property: %s\n",
  211. strerror(errno));
  212. goto error;
  213. }
  214. if (strcmp(property->name, glue[i].name) == 0) {
  215. *glue[i].value = property->prop_id;
  216. break;
  217. }
  218. drmModeFreeProperty(property);
  219. property = NULL;
  220. }
  221. if (j == properties->count_props) {
  222. fprintf(stderr, "Unable to find property for %s\n",
  223. glue[i].name);
  224. goto error;
  225. }
  226. drmModeFreeProperty(property);
  227. property = NULL;
  228. drmModeFreeObjectProperties(properties);
  229. properties = NULL;
  230. }
  231. rc = 0;
  232. goto complete;
  233. error:
  234. rc = -1;
  235. complete:
  236. if (property != NULL)
  237. drmModeFreeProperty(property);
  238. if (properties != NULL)
  239. drmModeFreeObjectProperties(properties);
  240. return rc;
  241. }
  242. static int commit_atomic_mode(int drm_fd, unsigned int connector_id,
  243. unsigned int crtc_id, unsigned int plane_id,
  244. struct display_properties_ids *ids,
  245. unsigned int framebuffer_id, unsigned int width,
  246. unsigned int height, unsigned int x,
  247. unsigned int y, unsigned int scaled_width,
  248. unsigned int scaled_height, unsigned int zpos)
  249. {
  250. drmModeAtomicReqPtr request;
  251. uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
  252. int rc;
  253. request = drmModeAtomicAlloc();
  254. drmModeAtomicAddProperty(request, plane_id, ids->plane_fb_id,
  255. framebuffer_id);
  256. drmModeAtomicAddProperty(request, plane_id, ids->plane_crtc_id,
  257. crtc_id);
  258. drmModeAtomicAddProperty(request, plane_id, ids->plane_src_x, 0);
  259. drmModeAtomicAddProperty(request, plane_id, ids->plane_src_y, 0);
  260. drmModeAtomicAddProperty(request, plane_id, ids->plane_src_w,
  261. width << 16);
  262. drmModeAtomicAddProperty(request, plane_id, ids->plane_src_h,
  263. height << 16);
  264. drmModeAtomicAddProperty(request, plane_id, ids->plane_crtc_x, x);
  265. drmModeAtomicAddProperty(request, plane_id, ids->plane_crtc_y, y);
  266. drmModeAtomicAddProperty(request, plane_id, ids->plane_crtc_w,
  267. scaled_width);
  268. drmModeAtomicAddProperty(request, plane_id, ids->plane_crtc_h,
  269. scaled_height);
  270. drmModeAtomicAddProperty(request, plane_id, ids->plane_zpos, zpos);
  271. rc = drmModeAtomicCommit(drm_fd, request, flags, NULL);
  272. if (rc < 0) {
  273. fprintf(stderr, "Unable to commit atomic mode: %s\n",
  274. strerror(errno));
  275. goto error;
  276. }
  277. rc = 0;
  278. goto complete;
  279. error:
  280. rc = -1;
  281. complete:
  282. drmModeAtomicFree(request);
  283. return rc;
  284. }
  285. static int page_flip(int drm_fd, unsigned int crtc_id, unsigned int plane_id,
  286. struct display_properties_ids *ids,
  287. unsigned int framebuffer_id)
  288. {
  289. drmModeAtomicReqPtr request;
  290. uint32_t flags = DRM_MODE_ATOMIC_ALLOW_MODESET;
  291. int rc;
  292. request = drmModeAtomicAlloc();
  293. drmModeAtomicAddProperty(request, plane_id, ids->plane_fb_id,
  294. framebuffer_id);
  295. drmModeAtomicAddProperty(request, plane_id, ids->plane_crtc_id,
  296. crtc_id);
  297. rc = drmModeAtomicCommit(drm_fd, request, flags, NULL);
  298. if (rc < 0) {
  299. fprintf(stderr, "Unable to flip page: %s\n", strerror(errno));
  300. goto error;
  301. }
  302. rc = 0;
  303. goto complete;
  304. error:
  305. rc = -1;
  306. complete:
  307. drmModeAtomicFree(request);
  308. return rc;
  309. }
  310. static int select_connector_encoder(int drm_fd, unsigned int *connector_id,
  311. unsigned int *encoder_id)
  312. {
  313. drmModeResPtr ressources = NULL;
  314. drmModeConnectorPtr connector = NULL;
  315. unsigned int i;
  316. int rc;
  317. ressources = drmModeGetResources(drm_fd);
  318. if (ressources == NULL) {
  319. fprintf(stderr, "Unable to get DRM ressources: %s\n",
  320. strerror(errno));
  321. goto error;
  322. }
  323. for (i = 0; i < ressources->count_connectors; i++) {
  324. connector = drmModeGetConnector(drm_fd,
  325. ressources->connectors[i]);
  326. if (connector == NULL) {
  327. fprintf(stderr, "Unable to get DRM connector %d: %s\n",
  328. ressources->connectors[i], strerror(errno));
  329. goto error;
  330. }
  331. if (connector->connection == DRM_MODE_CONNECTED)
  332. break;
  333. drmModeFreeConnector(connector);
  334. connector = NULL;
  335. }
  336. if (connector == NULL || i == ressources->count_connectors) {
  337. fprintf(stderr, "Unable to find any connected connector\n");
  338. goto error;
  339. }
  340. if (connector_id != NULL)
  341. *connector_id = connector->connector_id;
  342. if (encoder_id != NULL)
  343. *encoder_id = connector->encoder_id;
  344. rc = 0;
  345. goto complete;
  346. error:
  347. rc = -1;
  348. complete:
  349. if (connector != NULL)
  350. drmModeFreeConnector(connector);
  351. if (ressources != NULL)
  352. drmModeFreeResources(ressources);
  353. return rc;
  354. }
  355. static int select_crtc(int drm_fd, unsigned int encoder_id,
  356. unsigned int *crtc_id, drmModeModeInfoPtr mode)
  357. {
  358. drmModeEncoderPtr encoder = NULL;
  359. drmModeCrtcPtr crtc = NULL;
  360. int rc;
  361. encoder = drmModeGetEncoder(drm_fd, encoder_id);
  362. if (encoder == NULL) {
  363. fprintf(stderr, "Unable to get DRM encoder: %s\n",
  364. strerror(errno));
  365. goto error;
  366. }
  367. if (crtc_id != NULL)
  368. *crtc_id = encoder->crtc_id;
  369. crtc = drmModeGetCrtc(drm_fd, encoder->crtc_id);
  370. if (crtc == NULL) {
  371. fprintf(stderr, "Unable to get CRTC mode: %s\n",
  372. strerror(errno));
  373. goto error;
  374. }
  375. if (!crtc->mode_valid) {
  376. fprintf(stderr, "Unable to get valid mode for CRTC %d\n",
  377. crtc_id);
  378. goto error;
  379. }
  380. if (mode != NULL)
  381. memcpy(mode, &crtc->mode, sizeof(drmModeModeInfo));
  382. rc = 0;
  383. goto complete;
  384. error:
  385. rc = -1;
  386. complete:
  387. if (encoder != NULL)
  388. drmModeFreeEncoder(encoder);
  389. if (crtc != NULL)
  390. drmModeFreeCrtc(crtc);
  391. return rc;
  392. }
  393. static int select_plane(int drm_fd, unsigned int crtc_id, unsigned int format,
  394. unsigned int *plane_id, unsigned int *zpos)
  395. {
  396. drmModeResPtr ressources = NULL;
  397. drmModePlaneResPtr plane_ressources = NULL;
  398. drmModePlanePtr plane = NULL;
  399. drmModeObjectPropertiesPtr properties = NULL;
  400. drmModePropertyPtr property = NULL;
  401. unsigned int zpos_primary = 0;
  402. unsigned int zpos_value;
  403. unsigned int crtc_index;
  404. unsigned int type;
  405. unsigned int i, j;
  406. bool format_found;
  407. int rc;
  408. ressources = drmModeGetResources(drm_fd);
  409. if (ressources == NULL) {
  410. fprintf(stderr, "Unable to get DRM ressources: %s\n",
  411. strerror(errno));
  412. goto error;
  413. }
  414. for (i = 0; i < ressources->count_crtcs; i++) {
  415. if (ressources->crtcs[i] == crtc_id) {
  416. crtc_index = i;
  417. break;
  418. }
  419. }
  420. if (i == ressources->count_crtcs) {
  421. fprintf(stderr, "Unable to find CRTC index for CRTC %d\n",
  422. crtc_id);
  423. goto error;
  424. }
  425. plane_ressources = drmModeGetPlaneResources(drm_fd);
  426. if (plane_ressources == NULL) {
  427. fprintf(stderr, "Unable to get DRM plane ressources: %s\n",
  428. strerror(errno));
  429. goto error;
  430. }
  431. for (i = 0; i < plane_ressources->count_planes; i++) {
  432. plane = drmModeGetPlane(drm_fd, plane_ressources->planes[i]);
  433. if (plane == NULL) {
  434. fprintf(stderr, "Unable to get DRM plane %d: %s\n",
  435. plane_ressources->planes[i], strerror(errno));
  436. goto error;
  437. }
  438. if ((plane->possible_crtcs & (1 << crtc_index)) == 0) {
  439. drmModeFreePlane(plane);
  440. plane = NULL;
  441. continue;
  442. }
  443. properties = drmModeObjectGetProperties(drm_fd,
  444. plane_ressources->planes[i],
  445. DRM_MODE_OBJECT_PLANE);
  446. if (properties == NULL) {
  447. fprintf(stderr,
  448. "Unable to get DRM plane %d properties: %s\n",
  449. plane_ressources->planes[i], strerror(errno));
  450. goto error;
  451. }
  452. zpos_value = zpos_primary;
  453. for (j = 0; j < properties->count_props; j++) {
  454. property = drmModeGetProperty(drm_fd,
  455. properties->props[j]);
  456. if (property == NULL) {
  457. fprintf(stderr,
  458. "Unable to get DRM plane %d property: %s\n",
  459. plane_ressources->planes[i],
  460. strerror(errno));
  461. goto error;
  462. }
  463. if (strcmp(property->name, "type") == 0) {
  464. type = properties->prop_values[j];
  465. break;
  466. } else if (strcmp(property->name, "zpos") == 0) {
  467. zpos_value = properties->prop_values[j];
  468. break;
  469. }
  470. drmModeFreeProperty(property);
  471. property = NULL;
  472. }
  473. if (j == properties->count_props) {
  474. fprintf(stderr,
  475. "Unable to find plane %d type property\n",
  476. plane_ressources->planes[i]);
  477. goto error;
  478. }
  479. if (property != NULL) {
  480. drmModeFreeProperty(property);
  481. property = NULL;
  482. }
  483. drmModeFreeObjectProperties(properties);
  484. properties = NULL;
  485. if (type == DRM_PLANE_TYPE_PRIMARY)
  486. zpos_primary = zpos_value;
  487. if (type != DRM_PLANE_TYPE_OVERLAY)
  488. continue;
  489. format_found = false;
  490. for (j = 0; j < plane->count_formats; j++)
  491. if (plane->formats[j] == format)
  492. format_found = true;
  493. if (format_found)
  494. break;
  495. drmModeFreePlane(plane);
  496. plane = NULL;
  497. }
  498. if (plane == NULL || i == plane_ressources->count_planes) {
  499. fprintf(stderr, "Unable to find any plane for CRTC %d\n",
  500. crtc_id);
  501. goto error;
  502. }
  503. if (plane_id != NULL)
  504. *plane_id = plane->plane_id;
  505. if (zpos != NULL) {
  506. if (zpos_value <= zpos_primary)
  507. zpos_value = zpos_primary + 1;
  508. *zpos = zpos_value;
  509. }
  510. rc = 0;
  511. goto complete;
  512. error:
  513. rc = -1;
  514. complete:
  515. if (property != NULL)
  516. drmModeFreeProperty(property);
  517. if (properties != NULL)
  518. drmModeFreeObjectProperties(properties);
  519. if (plane != NULL)
  520. drmModeFreePlane(plane);
  521. if (ressources != NULL)
  522. drmModeFreeResources(ressources);
  523. return rc;
  524. }
  525. int display_engine_start(int drm_fd, unsigned int width, unsigned int height,
  526. struct format_description *format,
  527. struct video_buffer *video_buffers, unsigned int count,
  528. struct gem_buffer **buffers,
  529. struct display_setup *setup)
  530. {
  531. struct video_buffer *video_buffer;
  532. struct gem_buffer *buffer;
  533. unsigned int crtc_width, crtc_height;
  534. unsigned int scaled_width, scaled_height;
  535. unsigned int x, y;
  536. unsigned int i, j;
  537. unsigned int zpos;
  538. unsigned int export_fds_count;
  539. unsigned int connector_id;
  540. unsigned int encoder_id;
  541. unsigned int crtc_id;
  542. unsigned int plane_id;
  543. bool use_dmabuf = true;
  544. drmModeModeInfo mode;
  545. int rc;
  546. rc = drmSetClientCap(drm_fd, DRM_CLIENT_CAP_ATOMIC, 1);
  547. if (rc < 0) {
  548. fprintf(stderr, "Unable to set DRM atomic capability: %s\n",
  549. strerror(errno));
  550. return -1;
  551. }
  552. rc = drmSetClientCap(drm_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1);
  553. if (rc < 0) {
  554. fprintf(stderr,
  555. "Unable to set DRM universal planes capability: %s\n",
  556. strerror(errno));
  557. return -1;
  558. }
  559. rc = select_connector_encoder(drm_fd, &connector_id, &encoder_id);
  560. if (rc < 0) {
  561. fprintf(stderr, "Unable to select DRM connector/encoder\n");
  562. return -1;
  563. }
  564. rc = select_crtc(drm_fd, encoder_id, &crtc_id, &mode);
  565. if (rc < 0) {
  566. fprintf(stderr, "Unable to selec DRM CRTC\n");
  567. return -1;
  568. }
  569. rc = select_plane(drm_fd, crtc_id, format->drm_format, &plane_id,
  570. &zpos);
  571. if (rc < 0) {
  572. fprintf(stderr, "Unable to select DRM plane for CRTC %d\n",
  573. crtc_id);
  574. return -1;
  575. }
  576. crtc_width = mode.hdisplay;
  577. crtc_height = mode.vdisplay;
  578. memset(setup, 0, sizeof(*setup));
  579. rc = discover_properties(drm_fd, connector_id, crtc_id, plane_id,
  580. &setup->properties_ids);
  581. if (rc < 0) {
  582. fprintf(stderr, "Unable to discover DRM properties\n");
  583. return -1;
  584. }
  585. /*
  586. * Check for DMABUF support first and use as many (imported) gem buffers
  587. * as video buffers. Otherwise, fallback to 2 dedicated GEM buffers.
  588. */
  589. for (i = 0; i < count; i++) {
  590. video_buffer = &video_buffers[i];
  591. export_fds_count = video_buffer->destination_buffers_count;
  592. for (j = 0; j < export_fds_count; j++) {
  593. if (video_buffer->export_fds[j] < 0) {
  594. use_dmabuf = false;
  595. break;
  596. }
  597. }
  598. }
  599. /* Use double-buffering without DMABUF. */
  600. if (!use_dmabuf)
  601. count = 2;
  602. *buffers = malloc(count * sizeof(**buffers));
  603. memset(*buffers, 0, count * sizeof(**buffers));
  604. for (i = 0; i < count; i++) {
  605. buffer = &((*buffers)[i]);
  606. video_buffer = &video_buffers[i];
  607. export_fds_count = video_buffer->destination_buffers_count;
  608. buffer->planes_count = format->planes_count;
  609. if (use_dmabuf)
  610. rc = create_imported_buffer(drm_fd,
  611. video_buffer->export_fds,
  612. export_fds_count,
  613. video_buffer->destination_offsets,
  614. video_buffer->destination_bytesperlines,
  615. buffer);
  616. else
  617. rc = create_dumb_buffer(drm_fd, width, height,
  618. format->bpp, buffer);
  619. if (rc < 0) {
  620. fprintf(stderr,
  621. "Unable to create or import DRM buffer\n");
  622. return -1;
  623. }
  624. rc = add_framebuffer(drm_fd, buffer, width, height,
  625. format->drm_format, format->drm_modifier);
  626. if (rc < 0) {
  627. fprintf(stderr, "Unable to add DRM framebuffer\n");
  628. return -1;
  629. }
  630. if (!use_dmabuf) {
  631. rc = map_buffer(drm_fd, buffer);
  632. if (rc < 0) {
  633. fprintf(stderr, "Unable to map DRM buffer\n");
  634. return -1;
  635. }
  636. }
  637. }
  638. scaled_height = (height * crtc_width) / width;
  639. if (scaled_height > crtc_height) {
  640. /* Scale to CRTC height. */
  641. scaled_width = (width * crtc_height) / height;
  642. scaled_height = crtc_height;
  643. } else {
  644. /* Scale to CRTC width. */
  645. scaled_width = crtc_width;
  646. }
  647. x = (crtc_width - scaled_width) / 2;
  648. y = (crtc_height - scaled_height) / 2;
  649. if (scaled_width != width || scaled_height != height)
  650. printf("Scaling video from %dx%d to %dx%d+%d+%d\n", width,
  651. height, scaled_width, scaled_height, x, y);
  652. buffer = &((*buffers)[0]);
  653. rc = commit_atomic_mode(drm_fd, connector_id, crtc_id, plane_id,
  654. &setup->properties_ids, buffer->framebuffer_id,
  655. width, height, x, y, scaled_width,
  656. scaled_height, zpos);
  657. if (rc < 0) {
  658. fprintf(stderr, "Unable to commit initial plane\n");
  659. return -1;
  660. }
  661. setup->connector_id = connector_id;
  662. setup->encoder_id = encoder_id;
  663. setup->crtc_id = crtc_id;
  664. setup->plane_id = plane_id;
  665. setup->width = width;
  666. setup->height = height;
  667. setup->scaled_width = scaled_width;
  668. setup->scaled_height = scaled_height;
  669. setup->x = x;
  670. setup->y = y;
  671. setup->buffers_count = count;
  672. setup->use_dmabuf = use_dmabuf;
  673. return 0;
  674. }
  675. int display_engine_stop(int drm_fd, struct gem_buffer *buffers,
  676. struct display_setup *setup)
  677. {
  678. struct gem_buffer *buffer;
  679. unsigned int i;
  680. int rc;
  681. if (buffers == NULL || setup == NULL)
  682. return -1;
  683. for (i = 0; i < setup->buffers_count; i++) {
  684. buffer = &buffers[i];
  685. if (setup->use_dmabuf) {
  686. rc = close_buffer(drm_fd, buffer);
  687. if (rc < 0) {
  688. fprintf(stderr, "Unable to close buffer %d\n", i);
  689. return -1;
  690. }
  691. } else {
  692. rc = unmap_buffer(drm_fd, buffer);
  693. if (rc < 0) {
  694. fprintf(stderr, "Unable to unmap buffer %d\n", i);
  695. return -1;
  696. }
  697. rc = destroy_buffer(drm_fd, buffer);
  698. if (rc < 0) {
  699. fprintf(stderr, "Unable to destroy buffer %d\n", i);
  700. return -1;
  701. }
  702. }
  703. }
  704. return 0;
  705. }
  706. int display_engine_show(int drm_fd, unsigned int index,
  707. struct video_buffer *video_buffers,
  708. struct gem_buffer *buffers, struct display_setup *setup)
  709. {
  710. struct video_buffer *video_buffer;
  711. struct gem_buffer *buffer;
  712. unsigned int i;
  713. int rc;
  714. if (buffers == NULL || setup == NULL)
  715. return -1;
  716. video_buffer = &video_buffers[index];
  717. buffer = &buffers[index];
  718. if (!setup->use_dmabuf) {
  719. for (i = 0; i < buffer->planes_count; i++)
  720. memcpy((unsigned char *)buffer->data +
  721. buffer->offsets[i],
  722. video_buffer->destination_data[i],
  723. video_buffer->destination_sizes[i]);
  724. buffer = index % 2 == 0 ? &buffers[0] : &buffers[1];
  725. }
  726. rc = page_flip(drm_fd, setup->crtc_id, setup->plane_id,
  727. &setup->properties_ids, buffer->framebuffer_id);
  728. if (rc < 0) {
  729. fprintf(stderr, "Unable to flip page to framebuffer %d\n",
  730. buffer->framebuffer_id);
  731. return -1;
  732. }
  733. return 0;
  734. }