fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 Amlogic Corporation. |
| 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 <sys/types.h> |
| 17 | #include <sys/socket.h> |
| 18 | #include <unistd.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <errno.h> |
| 21 | #include <sys/epoll.h> |
| 22 | #include <string.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <wayland-client.h> |
| 25 | #include "xdg-shell-client-protocol.h" |
| 26 | #include "viewporter-client-protocol.h" |
| 27 | #include "linux-dmabuf-unstable-v1-client-protocol.h" |
| 28 | #include "fullscreen-shell-unstable-v1-client-protocol.h" |
| 29 | #include "wayland_display.h" |
| 30 | #include "wayland_buffer.h" |
| 31 | #include "Logger.h" |
| 32 | #include "wayland_videoformat.h" |
| 33 | #include "wayland_dma.h" |
| 34 | #include "wayland_shm.h" |
| 35 | #include "Utils.h" |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 36 | #include "ErrorCode.h" |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 37 | |
| 38 | #define TAG "rlib:wayland_buffer" |
| 39 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 40 | WaylandBuffer::WaylandBuffer(WaylandDisplay *display, int logCategory) |
| 41 | : mDisplay(display), |
| 42 | mLogCategory(logCategory) |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 43 | { |
| 44 | mRenderBuffer = NULL; |
| 45 | mWaylandWlWrap = NULL; |
| 46 | mUsedByCompositor = false; |
| 47 | mRedrawingPending = false; |
| 48 | mRealTime = -1; |
| 49 | mBufferFormat = VIDEO_FORMAT_UNKNOWN; |
| 50 | mFrameWidth = 0; |
| 51 | mFrameHeight = 0; |
fei.deng | a4abbd5 | 2024-07-11 19:17:50 +0800 | [diff] [blame] | 52 | TRACE(mLogCategory,"new WaylandBuffer:%p",this); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | WaylandBuffer::~WaylandBuffer() |
| 56 | { |
| 57 | /*if weston obtains the wl_buffer,we need |
| 58 | * notify user to release renderBuffer*/ |
| 59 | if (mRenderBuffer) { |
| 60 | mDisplay->handleBufferReleaseCallback(this); |
| 61 | mRenderBuffer = NULL; |
| 62 | } |
| 63 | if (mWaylandWlWrap) { |
| 64 | delete mWaylandWlWrap; |
| 65 | mWaylandWlWrap = NULL; |
| 66 | } |
fei.deng | a4abbd5 | 2024-07-11 19:17:50 +0800 | [diff] [blame] | 67 | TRACE(mLogCategory,"delete WaylandBuffer:%p",this); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void WaylandBuffer::bufferRelease (void *data, struct wl_buffer *wl_buffer) |
| 71 | { |
| 72 | WaylandBuffer* waylandBuffer = static_cast<WaylandBuffer*>(data); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 73 | TRACE(waylandBuffer->mLogCategory,"--wl_buffer:%p,renderBuffer:%p",wl_buffer,waylandBuffer->mRenderBuffer); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 74 | waylandBuffer->mUsedByCompositor = false; |
| 75 | //sometimes this callback be called twice |
| 76 | //this cause double free,so check mRenderBuffer |
| 77 | if (waylandBuffer->mRenderBuffer) { |
| 78 | waylandBuffer->mDisplay->handleBufferReleaseCallback(waylandBuffer); |
| 79 | waylandBuffer->mRenderBuffer = NULL; |
fei.deng | ae8c90a | 2024-06-27 13:39:53 +0800 | [diff] [blame] | 80 | waylandBuffer->mRealTime = -1; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 81 | } |
fei.deng | a4abbd5 | 2024-07-11 19:17:50 +0800 | [diff] [blame] | 82 | //release after buffer release callback |
| 83 | int fw = waylandBuffer->mDisplay->getFrameWidth(); |
| 84 | int fh = waylandBuffer->mDisplay->getFrameHeight(); |
| 85 | if ( (waylandBuffer->mFrameWidth != fw || waylandBuffer->mFrameHeight != fh) && |
| 86 | waylandBuffer->mWaylandWlWrap) { |
fei.deng | 6c42523 | 2024-07-19 16:15:31 +0800 | [diff] [blame^] | 87 | TRACE(waylandBuffer->mLogCategory,"resolution changed,delete WaylandBuffer:%p,WaylandDmaBuffer:%p",waylandBuffer,waylandBuffer->mWaylandWlWrap); |
fei.deng | a4abbd5 | 2024-07-11 19:17:50 +0800 | [diff] [blame] | 88 | delete waylandBuffer->mWaylandWlWrap; |
| 89 | waylandBuffer->mWaylandWlWrap = NULL; |
| 90 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void WaylandBuffer::bufferdroped (void *data, struct wl_buffer *wl_buffer) |
| 94 | { |
| 95 | WaylandBuffer* waylandBuffer = static_cast<WaylandBuffer*>(data); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 96 | WARNING(waylandBuffer->mLogCategory,"--droped wl_buffer:%p,renderBuffer:%p",wl_buffer,waylandBuffer->mRenderBuffer); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 97 | waylandBuffer->mUsedByCompositor = false; |
| 98 | |
| 99 | if (waylandBuffer->mRenderBuffer) { |
| 100 | waylandBuffer->mDisplay->handleFrameDropedCallback(waylandBuffer); |
| 101 | waylandBuffer->mDisplay->handleBufferReleaseCallback(waylandBuffer); |
| 102 | waylandBuffer->mRenderBuffer = NULL; |
| 103 | } |
| 104 | } |
| 105 | |
fei.deng | 640c3c9 | 2024-04-12 08:31:19 +0000 | [diff] [blame] | 106 | static const struct wl_buffer_listener buffer_with_drop_listener = { |
| 107 | WaylandBuffer::bufferRelease, |
| 108 | WaylandBuffer::bufferdroped, |
| 109 | }; |
| 110 | |
| 111 | static const struct wl_buffer_listener buffer_listener = { |
| 112 | WaylandBuffer::bufferRelease |
| 113 | }; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 114 | |
| 115 | /*if we commit buffers to weston too fast,it causes weston can't invoke this callback*/ |
| 116 | void WaylandBuffer::frameDisplayedCallback(void *data, struct wl_callback *callback, uint32_t time) |
| 117 | { |
| 118 | WaylandBuffer* waylandBuffer = static_cast<WaylandBuffer*>(data); |
| 119 | waylandBuffer->mDisplay->setRedrawingPending(false); |
| 120 | waylandBuffer->mLock.lock(); |
| 121 | bool redrawing = waylandBuffer->mRedrawingPending; |
| 122 | waylandBuffer->mRedrawingPending = false; |
| 123 | waylandBuffer->mLock.unlock(); |
| 124 | if (waylandBuffer->mRenderBuffer && redrawing) { |
fei.deng | dd910ef | 2024-06-07 10:25:30 +0800 | [diff] [blame] | 125 | TRACE(waylandBuffer->mLogCategory,"renderBuffer:%p, pts:%lld us",waylandBuffer->mRenderBuffer,waylandBuffer->mRenderBuffer->pts/1000); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 126 | waylandBuffer->mDisplay->handleFrameDisplayedCallback(waylandBuffer); |
| 127 | } |
| 128 | wl_callback_destroy (callback); |
| 129 | } |
| 130 | |
| 131 | static const struct wl_callback_listener frame_callback_listener = { |
| 132 | WaylandBuffer::frameDisplayedCallback |
| 133 | }; |
| 134 | |
| 135 | int WaylandBuffer::constructWlBuffer(RenderBuffer *buf) |
| 136 | { |
| 137 | struct wl_buffer * wlbuffer = NULL; |
fei.deng | 640c3c9 | 2024-04-12 08:31:19 +0000 | [diff] [blame] | 138 | WaylandDisplay::AmlConfigAPIList *amlConfigAPI = mDisplay->getAmlConfigAPIList(); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 139 | |
| 140 | mRenderBuffer = buf; |
| 141 | if (mWaylandWlWrap) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 142 | return NO_ERROR; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | if (buf->flag & BUFFER_FLAG_DMA_BUFFER) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 146 | WaylandDmaBuffer *waylanddma = new WaylandDmaBuffer(mDisplay, mLogCategory); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 147 | wlbuffer = waylanddma->constructWlBuffer(&buf->dma, mBufferFormat); |
| 148 | if (!wlbuffer) { |
| 149 | delete waylanddma; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 150 | ERROR(mLogCategory,"create wl_buffer fail"); |
| 151 | return ERROR_INVALID_OPERATION; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 152 | } |
| 153 | mWaylandWlWrap = waylanddma; |
| 154 | mFrameWidth = buf->dma.width; |
| 155 | mFrameHeight = buf->dma.height; |
fei.deng | a4abbd5 | 2024-07-11 19:17:50 +0800 | [diff] [blame] | 156 | TRACE(mLogCategory,"WaylandBuffer:%p,WaylandDmaBuffer:%p",this,mWaylandWlWrap); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | /*register buffer release listen*/ |
fei.deng | 640c3c9 | 2024-04-12 08:31:19 +0000 | [diff] [blame] | 160 | if (amlConfigAPI->enableDropFrame) { |
| 161 | wl_buffer_add_listener (wlbuffer, &buffer_with_drop_listener, this); |
| 162 | } else { |
| 163 | wl_buffer_add_listener (wlbuffer, &buffer_listener, this); |
| 164 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 165 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 166 | return NO_ERROR; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | struct wl_buffer *WaylandBuffer::getWlBuffer() |
| 170 | { |
| 171 | if (mWaylandWlWrap) { |
| 172 | return mWaylandWlWrap->getWlBuffer(); |
| 173 | } else { |
| 174 | return NULL; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | void WaylandBuffer::attach(struct wl_surface *surface) |
| 179 | { |
| 180 | struct wl_callback *callback; |
| 181 | struct wl_buffer *wlbuffer = NULL; |
| 182 | if (mUsedByCompositor) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 183 | DEBUG(mLogCategory,"buffer used by compositor"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | |
| 187 | //callback when this frame displayed |
| 188 | callback = wl_surface_frame (surface); |
| 189 | wl_callback_add_listener (callback, &frame_callback_listener, this); |
| 190 | mDisplay->setRedrawingPending(true); |
| 191 | |
| 192 | wlbuffer = getWlBuffer(); |
| 193 | if (wlbuffer) { |
| 194 | wl_surface_attach (surface, wlbuffer, 0, 0); |
| 195 | } |
| 196 | |
| 197 | mUsedByCompositor = true; |
| 198 | mRedrawingPending = true; |
fei.deng | a4abbd5 | 2024-07-11 19:17:50 +0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | bool WaylandBuffer::isFree() |
| 202 | { |
| 203 | if (!mWaylandWlWrap && !mRenderBuffer) { |
| 204 | return true; |
| 205 | } |
| 206 | return false; |
| 207 | }; |