limin.tian | 79bf2b1 | 2023-02-24 10:28:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 Amlogic, Inc. All rights reserved. |
Ao Xu | 6747bbd | 2020-09-28 20:02:09 +0800 | [diff] [blame] | 3 | * |
limin.tian | 79bf2b1 | 2023-02-24 10:28:26 +0000 | [diff] [blame] | 4 | * This source code is subject to the terms and conditions defined in the |
| 5 | * file 'LICENSE' which is part of this source code package. |
| 6 | * |
| 7 | * Description: |
Ao Xu | 6747bbd | 2020-09-28 20:02:09 +0800 | [diff] [blame] | 8 | */ |
limin.tian | 79bf2b1 | 2023-02-24 10:28:26 +0000 | [diff] [blame] | 9 | |
wenlong.zhang | e389b37 | 2021-12-16 16:42:08 +0800 | [diff] [blame] | 10 | #include <poll.h> |
| 11 | #include <sys/ioctl.h> |
| 12 | #include "dma-buf.h" |
Ao Xu | 6747bbd | 2020-09-28 20:02:09 +0800 | [diff] [blame] | 13 | #include "meson_drm_util.h" |
| 14 | |
| 15 | struct drm_display *drm_kms_init(void); |
| 16 | |
| 17 | struct drm_display *drm_display_init(void) |
| 18 | { |
| 19 | return drm_kms_init(); |
| 20 | } |
| 21 | |
| 22 | void drm_destroy_display(struct drm_display *disp) |
| 23 | { |
| 24 | disp->destroy_display(disp); |
| 25 | } |
| 26 | |
| 27 | void drm_display_register_done_cb(struct drm_display *disp, void *func, void *priv) |
| 28 | { |
| 29 | disp->display_done_callback = func; |
| 30 | disp->priv = priv; |
| 31 | } |
| 32 | |
| 33 | void drm_display_register_res_cb(struct drm_display *disp, void *func, void *priv) |
| 34 | { |
| 35 | disp->resolution_change_callback = func; |
| 36 | disp->reso_priv = priv; |
| 37 | } |
| 38 | |
wenlong.zhang | 4c011d4 | 2022-06-09 10:59:38 +0800 | [diff] [blame] | 39 | int drm_set_alloc_only_flag(struct drm_display *disp, int flag) |
| 40 | { |
| 41 | disp->alloc_only = flag; |
| 42 | } |
| 43 | |
Ao Xu | 6747bbd | 2020-09-28 20:02:09 +0800 | [diff] [blame] | 44 | int drm_alloc_bufs(struct drm_display *disp, int num, struct drm_buf_metadata *info) |
| 45 | { |
| 46 | int ret; |
| 47 | ret = disp->alloc_bufs(disp, num, info); |
| 48 | |
| 49 | return ret; |
| 50 | } |
| 51 | |
| 52 | int drm_free_bufs(struct drm_display *disp) |
| 53 | { |
| 54 | int ret; |
| 55 | ret = disp->free_bufs(disp); |
| 56 | |
| 57 | return ret; |
| 58 | } |
| 59 | |
| 60 | struct drm_buf *drm_alloc_buf(struct drm_display *disp, struct drm_buf_metadata *info) |
| 61 | { |
| 62 | struct drm_buf *buf = NULL; |
| 63 | |
| 64 | buf = disp->alloc_buf(disp, info); |
| 65 | |
| 66 | return buf; |
| 67 | } |
| 68 | |
Ao Xu | 7055424 | 2020-10-15 15:19:48 +0800 | [diff] [blame] | 69 | struct drm_buf *drm_import_buf(struct drm_display *disp, struct drm_buf_import *info) |
| 70 | { |
| 71 | struct drm_buf *buf = NULL; |
| 72 | |
| 73 | buf = disp->import_buf(disp, info); |
| 74 | |
| 75 | return buf; |
| 76 | } |
| 77 | |
Ao Xu | 6747bbd | 2020-09-28 20:02:09 +0800 | [diff] [blame] | 78 | int drm_free_buf(struct drm_buf *buf) |
| 79 | { |
| 80 | int ret; |
| 81 | struct drm_display *disp = buf->disp; |
| 82 | ret = disp->free_buf(buf); |
| 83 | |
| 84 | return ret; |
| 85 | } |
| 86 | |
| 87 | int drm_post_buf(struct drm_display *disp, struct drm_buf *buf) |
| 88 | { |
| 89 | return disp->post_buf(disp, buf); |
| 90 | } |
wenlong.zhang | e389b37 | 2021-12-16 16:42:08 +0800 | [diff] [blame] | 91 | |
| 92 | int drm_waitvideoFence( int dmabuffd ) |
| 93 | { |
| 94 | struct dma_buf_export_sync_file dma_fence; |
| 95 | int rc = -1; |
| 96 | |
| 97 | if (dmabuffd <= 0) |
| 98 | return; |
| 99 | |
| 100 | memset (&dma_fence, 0, sizeof(dma_fence)); |
| 101 | dma_fence.flags |= DMA_BUF_SYNC_READ; |
| 102 | rc = ioctl(dmabuffd, DMA_BUF_IOCTL_EXPORT_SYNC_FILE, &dma_fence); |
| 103 | if (!rc && dma_fence.fd >= 0 ) |
| 104 | { |
| 105 | struct pollfd pfd; |
| 106 | |
| 107 | pfd.fd= dma_fence.fd; |
| 108 | pfd.events= POLLIN; |
| 109 | pfd.revents= 0; |
| 110 | |
| 111 | for ( ; ; ) { |
| 112 | rc= poll( &pfd, 1, 3000); |
| 113 | if ( (rc == -1) && ((errno == EINTR) || (errno == EAGAIN)) ) |
| 114 | { |
| 115 | continue; |
| 116 | } |
| 117 | else if ( rc <= 0 ) |
| 118 | { |
| 119 | if ( rc == 0 ) errno= ETIME; |
| 120 | fprintf(stderr, "wait out video fence failed: fd %d errno %d\n", dma_fence.fd, errno); |
| 121 | } |
| 122 | else if(pfd.revents & (POLLNVAL | POLLERR)) { |
| 123 | fprintf(stderr, "waiting on video fence fd %d, revents error\n", dma_fence.fd); |
| 124 | } |
| 125 | break; |
| 126 | } |
| 127 | close( dma_fence.fd ); |
| 128 | dma_fence.fd= -1; |
| 129 | } |
| 130 | |
| 131 | return rc; |
| 132 | } |