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 <string> |
| 17 | #include "wayland_display.h" |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 18 | #include "ErrorCode.h" |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 19 | #include "Logger.h" |
| 20 | #include "wayland_plugin.h" |
| 21 | #include "wayland_videoformat.h" |
| 22 | #include "wayland_shm.h" |
| 23 | #include "wayland_dma.h" |
| 24 | #include "wayland_buffer.h" |
| 25 | |
| 26 | #ifndef MAX |
| 27 | # define MAX(a,b) ((a) > (b)? (a) : (b)) |
| 28 | # define MIN(a,b) ((a) < (b)? (a) : (b)) |
| 29 | #endif |
| 30 | |
| 31 | #define UNUSED_PARAM(x) ((void)(x)) |
fei.deng | 2695083 | 2023-11-09 03:00:23 +0000 | [diff] [blame] | 32 | #define INVALID_OUTPUT_INDEX (-1) |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 33 | |
| 34 | #define TAG "rlib:wayland_display" |
| 35 | |
| 36 | void WaylandDisplay::dmabuf_modifiers(void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf, |
| 37 | uint32_t format, uint32_t modifier_hi, uint32_t modifier_lo) |
| 38 | { |
| 39 | WaylandDisplay *self = static_cast<WaylandDisplay *>(data); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 40 | Tls::Mutex::Autolock _l(self->mMutex); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 41 | if (wl_dmabuf_format_to_video_format (format) != VIDEO_FORMAT_UNKNOWN) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 42 | TRACE(self->mLogCategory,"regist dmabuffer format:%d (%s) hi:%x,lo:%x",format,print_dmabuf_format_name(format),modifier_hi,modifier_lo); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 43 | uint64_t modifier = ((uint64_t)modifier_hi << 32) | modifier_lo; |
| 44 | auto item = self->mDmaBufferFormats.find(format); |
| 45 | if (item == self->mDmaBufferFormats.end()) { |
| 46 | std::pair<uint32_t ,uint64_t> item(format, modifier); |
| 47 | self->mDmaBufferFormats.insert(item); |
| 48 | } else { //found format |
| 49 | item->second = modifier; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | WaylandDisplay::dmaBufferFormat (void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf, |
| 56 | uint32_t format) |
| 57 | { |
| 58 | #if 0 |
| 59 | WaylandDisplay *self = static_cast<WaylandDisplay *>(data); |
| 60 | |
| 61 | if (wl_dmabuf_format_to_video_format (format) != VIDEO_FORMAT_UNKNOWN) { |
| 62 | TRACE(mLogCategory,"regist dmabuffer format:%d : %s",format); |
| 63 | //self->mDmaBufferFormats.push_back(format); |
| 64 | } |
| 65 | #endif |
| 66 | /* XXX: deprecated */ |
| 67 | } |
| 68 | |
| 69 | static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = { |
| 70 | WaylandDisplay::dmaBufferFormat, |
| 71 | WaylandDisplay::dmabuf_modifiers |
| 72 | }; |
| 73 | |
| 74 | static void |
| 75 | handle_xdg_wm_base_ping (void *user_data, struct xdg_wm_base *xdg_wm_base, |
| 76 | uint32_t serial) |
| 77 | { |
| 78 | xdg_wm_base_pong (xdg_wm_base, serial); |
| 79 | } |
| 80 | |
| 81 | static const struct xdg_wm_base_listener xdg_wm_base_listener = { |
| 82 | handle_xdg_wm_base_ping |
| 83 | }; |
| 84 | |
| 85 | void WaylandDisplay::shmFormat(void *data, struct wl_shm *wl_shm, uint32_t format) |
| 86 | { |
| 87 | WaylandDisplay *self = static_cast<WaylandDisplay *>(data); |
| 88 | self->mShmFormats.push_back(format); |
| 89 | } |
| 90 | |
| 91 | static const struct wl_shm_listener shm_listener = { |
| 92 | WaylandDisplay::shmFormat |
| 93 | }; |
| 94 | |
| 95 | void WaylandDisplay::outputHandleGeometry( void *data, |
| 96 | struct wl_output *output, |
| 97 | int x, |
| 98 | int y, |
| 99 | int physicalWidth, |
| 100 | int physicalHeight, |
| 101 | int subPixel, |
| 102 | const char *make, |
| 103 | const char *model, |
| 104 | int transform ) |
| 105 | { |
| 106 | UNUSED_PARAM(make); |
| 107 | UNUSED_PARAM(model); |
| 108 | |
| 109 | WaylandDisplay *self = static_cast<WaylandDisplay *>(data); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 110 | DEBUG(self->mLogCategory,"wl_output %p x:%d,y:%d,physicalWidth:%d,physicalHeight:%d,subPixel:%d,trans:%d", |
| 111 | output,x, y,physicalWidth, physicalHeight,subPixel,transform); |
| 112 | Tls::Mutex::Autolock _l(self->mMutex); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 113 | for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) { |
| 114 | if (output == self->mOutput[i].wlOutput) { |
| 115 | self->mOutput[i].offsetX = x; |
| 116 | self->mOutput[i].offsetY = y; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void WaylandDisplay::outputHandleMode( void *data, |
| 122 | struct wl_output *output, |
| 123 | uint32_t flags, |
| 124 | int width, |
| 125 | int height, |
| 126 | int refreshRate ) |
| 127 | { |
| 128 | WaylandDisplay *self = static_cast<WaylandDisplay *>(data); |
| 129 | |
| 130 | if ( flags & WL_OUTPUT_MODE_CURRENT ) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 131 | Tls::Mutex::Autolock _l(self->mMutex); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 132 | for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) { |
| 133 | if (output == self->mOutput[i].wlOutput) { |
| 134 | self->mOutput[i].width = width; |
| 135 | self->mOutput[i].height = height; |
| 136 | self->mOutput[i].refreshRate = refreshRate; |
| 137 | } |
| 138 | } |
fei.deng | 2695083 | 2023-11-09 03:00:23 +0000 | [diff] [blame] | 139 | |
| 140 | DEBUG(self->mLogCategory,"wl_output: %p (%dx%d) refreshrate:%d,select output index %d",output, width, height,refreshRate,self->mSelectOutputIndex); |
| 141 | if (self->mCurrentDisplayOutput->width > 0 && |
| 142 | self->mCurrentDisplayOutput->height > 0) { |
| 143 | self->updateDisplayOutput(); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void WaylandDisplay::outputHandleDone( void *data, |
| 149 | struct wl_output *output ) |
| 150 | { |
fei.deng | f1f5fc3 | 2023-12-06 06:22:20 +0000 | [diff] [blame] | 151 | WaylandDisplay *self = static_cast<WaylandDisplay *>(data); |
| 152 | DEBUG(self->mLogCategory,"wl_output: %p",output); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 153 | UNUSED_PARAM(data); |
| 154 | UNUSED_PARAM(output); |
| 155 | } |
| 156 | |
| 157 | void WaylandDisplay::outputHandleScale( void *data, |
| 158 | struct wl_output *output, |
| 159 | int32_t scale ) |
| 160 | { |
fei.deng | f1f5fc3 | 2023-12-06 06:22:20 +0000 | [diff] [blame] | 161 | WaylandDisplay *self = static_cast<WaylandDisplay *>(data); |
| 162 | DEBUG(self->mLogCategory,"wl_output: %p scale %d",output, scale); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 163 | UNUSED_PARAM(data); |
| 164 | UNUSED_PARAM(output); |
| 165 | UNUSED_PARAM(scale); |
| 166 | } |
| 167 | |
fei.deng | f1f5fc3 | 2023-12-06 06:22:20 +0000 | [diff] [blame] | 168 | void WaylandDisplay::outputHandleCrtcIndex( void *data, |
| 169 | struct wl_output *output, |
| 170 | int32_t index ) |
| 171 | { |
| 172 | WaylandDisplay *self = static_cast<WaylandDisplay *>(data); |
| 173 | DEBUG(self->mLogCategory,"wl_output: %p crtc index %d",output, index); |
| 174 | Tls::Mutex::Autolock _l(self->mMutex); |
| 175 | for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) { |
| 176 | if (output == self->mOutput[i].wlOutput) { |
| 177 | self->mOutput[i].crtcIndex = index; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
fei.deng | 640c3c9 | 2024-04-12 08:31:19 +0000 | [diff] [blame] | 182 | //aml weston always add crtcIndex in callbacks. |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 183 | static const struct wl_output_listener outputListener = { |
| 184 | WaylandDisplay::outputHandleGeometry, |
| 185 | WaylandDisplay::outputHandleMode, |
| 186 | WaylandDisplay::outputHandleDone, |
fei.deng | f1f5fc3 | 2023-12-06 06:22:20 +0000 | [diff] [blame] | 187 | WaylandDisplay::outputHandleScale, |
| 188 | WaylandDisplay::outputHandleCrtcIndex, |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | void WaylandDisplay::pointerHandleEnter(void *data, struct wl_pointer *pointer, |
| 192 | uint32_t serial, struct wl_surface *surface, |
| 193 | wl_fixed_t sx, wl_fixed_t sy) |
| 194 | { |
| 195 | WaylandDisplay *self = static_cast<WaylandDisplay *>(data); |
| 196 | |
| 197 | if (self->mFullScreen) { |
| 198 | wl_pointer_set_cursor(pointer, serial, NULL, 0, 0); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void WaylandDisplay::pointerHandleLeave(void *data, struct wl_pointer *pointer, |
| 203 | uint32_t serial, struct wl_surface *surface) |
| 204 | { |
| 205 | UNUSED_PARAM(data); |
| 206 | UNUSED_PARAM(pointer); |
| 207 | UNUSED_PARAM(serial); |
| 208 | UNUSED_PARAM(surface); |
| 209 | } |
| 210 | |
| 211 | void WaylandDisplay::pointerHandleMotion(void *data, struct wl_pointer *pointer, |
| 212 | uint32_t time, wl_fixed_t sx, wl_fixed_t sy) |
| 213 | { |
| 214 | WaylandDisplay *self = static_cast<WaylandDisplay *>(data); |
| 215 | int x = wl_fixed_to_int(sx); |
| 216 | int y = wl_fixed_to_int(sy); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 217 | DEBUG(self->mLogCategory,"pointer motion fixed[%d, %d] to-int: x[%d] y[%d]\n", sx, sy, x, y); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void WaylandDisplay::pointerHandleButton(void *data, struct wl_pointer *wl_pointer, |
| 221 | uint32_t serial, uint32_t time, uint32_t button, uint32_t state) |
| 222 | { |
| 223 | UNUSED_PARAM(data); |
| 224 | UNUSED_PARAM(wl_pointer); |
| 225 | UNUSED_PARAM(time); |
| 226 | UNUSED_PARAM(serial); |
| 227 | UNUSED_PARAM(button); |
| 228 | UNUSED_PARAM(state); |
| 229 | } |
| 230 | |
| 231 | void WaylandDisplay::pointerHandleAxis(void *data, struct wl_pointer *wl_pointer, |
| 232 | uint32_t time, uint32_t axis, wl_fixed_t value) |
| 233 | { |
| 234 | UNUSED_PARAM(data); |
| 235 | UNUSED_PARAM(wl_pointer); |
| 236 | UNUSED_PARAM(time); |
| 237 | UNUSED_PARAM(axis); |
| 238 | UNUSED_PARAM(value); |
| 239 | } |
| 240 | |
| 241 | static const struct wl_pointer_listener pointer_listener = { |
| 242 | WaylandDisplay::pointerHandleEnter, |
| 243 | WaylandDisplay::pointerHandleLeave, |
| 244 | WaylandDisplay::pointerHandleMotion, |
| 245 | WaylandDisplay::pointerHandleButton, |
| 246 | WaylandDisplay::pointerHandleAxis, |
| 247 | }; |
| 248 | |
| 249 | void WaylandDisplay::touchHandleDown(void *data, struct wl_touch *wl_touch, |
| 250 | uint32_t serial, uint32_t time, struct wl_surface *surface, |
| 251 | int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) |
| 252 | { |
| 253 | WaylandDisplay *self = static_cast<WaylandDisplay *>(data); |
| 254 | } |
| 255 | |
| 256 | void WaylandDisplay::touchHandleUp(void *data, struct wl_touch *wl_touch, |
| 257 | uint32_t serial, uint32_t time, int32_t id) |
| 258 | { |
| 259 | UNUSED_PARAM(data); |
| 260 | UNUSED_PARAM(wl_touch); |
| 261 | UNUSED_PARAM(serial); |
| 262 | UNUSED_PARAM(time); |
| 263 | UNUSED_PARAM(id); |
| 264 | } |
| 265 | |
| 266 | void WaylandDisplay::touchHandleMotion(void *data, struct wl_touch *wl_touch, |
| 267 | uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) |
| 268 | { |
| 269 | UNUSED_PARAM(data); |
| 270 | UNUSED_PARAM(wl_touch); |
| 271 | UNUSED_PARAM(time); |
| 272 | UNUSED_PARAM(id); |
| 273 | UNUSED_PARAM(x_w); |
| 274 | UNUSED_PARAM(y_w); |
| 275 | } |
| 276 | |
| 277 | void WaylandDisplay::touchHandleFrame(void *data, struct wl_touch *wl_touch) |
| 278 | { |
| 279 | UNUSED_PARAM(data); |
| 280 | UNUSED_PARAM(wl_touch); |
| 281 | } |
| 282 | |
| 283 | void WaylandDisplay::touchHandleCancel(void *data, struct wl_touch *wl_touch) |
| 284 | { |
| 285 | UNUSED_PARAM(data); |
| 286 | UNUSED_PARAM(wl_touch); |
| 287 | } |
| 288 | |
| 289 | static const struct wl_touch_listener touch_listener = { |
| 290 | WaylandDisplay::touchHandleDown, |
| 291 | WaylandDisplay::touchHandleUp, |
| 292 | WaylandDisplay::touchHandleMotion, |
| 293 | WaylandDisplay::touchHandleFrame, |
| 294 | WaylandDisplay::touchHandleCancel, |
| 295 | }; |
| 296 | |
| 297 | void WaylandDisplay::keyboardHandleKeymap(void *data, struct wl_keyboard *keyboard, |
| 298 | uint32_t format, int fd, uint32_t size) |
| 299 | { |
| 300 | UNUSED_PARAM(data); |
| 301 | UNUSED_PARAM(keyboard); |
| 302 | UNUSED_PARAM(format); |
| 303 | UNUSED_PARAM(fd); |
| 304 | UNUSED_PARAM(size); |
| 305 | } |
| 306 | |
| 307 | void WaylandDisplay::keyboardHandleEnter(void *data, struct wl_keyboard *keyboard, |
| 308 | uint32_t serial, struct wl_surface *surface, struct wl_array *keys) |
| 309 | { |
| 310 | UNUSED_PARAM(data); |
| 311 | UNUSED_PARAM(keyboard); |
| 312 | UNUSED_PARAM(serial); |
| 313 | UNUSED_PARAM(surface); |
| 314 | UNUSED_PARAM(keys); |
| 315 | } |
| 316 | |
| 317 | void WaylandDisplay::keyboardHandleLeave(void *data, struct wl_keyboard *keyboard, |
| 318 | uint32_t serial, struct wl_surface *surface) |
| 319 | { |
| 320 | UNUSED_PARAM(data); |
| 321 | UNUSED_PARAM(keyboard); |
| 322 | UNUSED_PARAM(serial); |
| 323 | UNUSED_PARAM(surface); |
| 324 | } |
| 325 | |
| 326 | void WaylandDisplay::keyboardHandleKey(void *data, struct wl_keyboard *keyboard, |
| 327 | uint32_t serial, uint32_t time, uint32_t key, uint32_t state) |
| 328 | { |
| 329 | UNUSED_PARAM(data); |
| 330 | UNUSED_PARAM(keyboard); |
| 331 | UNUSED_PARAM(serial); |
| 332 | UNUSED_PARAM(time); |
| 333 | UNUSED_PARAM(key); |
| 334 | UNUSED_PARAM(state); |
| 335 | } |
| 336 | |
| 337 | void WaylandDisplay::keyboardHandleModifiers(void *data, struct wl_keyboard *keyboard, |
| 338 | uint32_t serial, uint32_t mods_depressed, |
| 339 | uint32_t mods_latched, uint32_t mods_locked, |
| 340 | uint32_t group) |
| 341 | { |
| 342 | UNUSED_PARAM(data); |
| 343 | UNUSED_PARAM(keyboard); |
| 344 | UNUSED_PARAM(serial); |
| 345 | UNUSED_PARAM(mods_depressed); |
| 346 | UNUSED_PARAM(mods_latched); |
| 347 | UNUSED_PARAM(mods_locked); |
| 348 | UNUSED_PARAM(group); |
| 349 | } |
| 350 | |
| 351 | static const struct wl_keyboard_listener keyboard_listener = { |
| 352 | WaylandDisplay::keyboardHandleKeymap, |
| 353 | WaylandDisplay::keyboardHandleEnter, |
| 354 | WaylandDisplay::keyboardHandleLeave, |
| 355 | WaylandDisplay::keyboardHandleKey, |
| 356 | WaylandDisplay::keyboardHandleModifiers, |
| 357 | }; |
| 358 | |
| 359 | void WaylandDisplay::seatHandleCapabilities(void *data, struct wl_seat *seat, |
| 360 | uint32_t caps) |
| 361 | { |
| 362 | WaylandDisplay *self = static_cast<WaylandDisplay *>(data); |
| 363 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !self->mPointer) { |
| 364 | self->mPointer = wl_seat_get_pointer(seat); |
| 365 | wl_pointer_add_listener(self->mPointer, &pointer_listener, data); |
| 366 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && self->mPointer) { |
| 367 | wl_pointer_destroy(self->mPointer); |
| 368 | self->mPointer = NULL; |
| 369 | } |
| 370 | |
| 371 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !self->mKeyboard) { |
| 372 | self->mKeyboard = wl_seat_get_keyboard(seat); |
| 373 | wl_keyboard_add_listener(self->mKeyboard, &keyboard_listener, data); |
| 374 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && self->mKeyboard) { |
| 375 | wl_keyboard_destroy(self->mKeyboard); |
| 376 | self->mKeyboard = NULL; |
| 377 | } |
| 378 | |
| 379 | if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !self->mTouch) { |
| 380 | self->mTouch = wl_seat_get_touch(seat); |
| 381 | wl_touch_set_user_data(self->mTouch, data); |
| 382 | wl_touch_add_listener(self->mTouch, &touch_listener, data); |
| 383 | } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && self->mTouch) { |
| 384 | wl_touch_destroy(self->mTouch); |
| 385 | self->mTouch = NULL; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | static const struct wl_seat_listener seat_listener = { |
| 390 | WaylandDisplay::seatHandleCapabilities, |
| 391 | }; |
| 392 | |
| 393 | |
| 394 | void WaylandDisplay::handleXdgToplevelClose (void *data, struct xdg_toplevel *xdg_toplevel) |
| 395 | { |
| 396 | WaylandDisplay *self = static_cast<WaylandDisplay *>(data); |
| 397 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 398 | INFO(self->mLogCategory,"XDG toplevel got a close event."); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | void WaylandDisplay::handleXdgToplevelConfigure (void *data, struct xdg_toplevel *xdg_toplevel, |
| 402 | int32_t width, int32_t height, struct wl_array *states) |
| 403 | { |
| 404 | WaylandDisplay *self = static_cast<WaylandDisplay *>(data); |
| 405 | uint32_t *state; |
| 406 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 407 | INFO(self->mLogCategory, "XDG toplevel got a configure event, width:height [ %d, %d ].", width, height); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 408 | /* |
| 409 | wl_array_for_each (state, states) { |
| 410 | switch (*state) { |
| 411 | case XDG_TOPLEVEL_STATE_FULLSCREEN: |
| 412 | case XDG_TOPLEVEL_STATE_MAXIMIZED: |
| 413 | case XDG_TOPLEVEL_STATE_RESIZING: |
| 414 | case XDG_TOPLEVEL_STATE_ACTIVATED: |
| 415 | break; |
| 416 | } |
| 417 | } |
| 418 | */ |
| 419 | |
| 420 | if (width <= 0 || height <= 0) |
| 421 | return; |
| 422 | |
fei.deng | 2695083 | 2023-11-09 03:00:23 +0000 | [diff] [blame] | 423 | if (width == self->mCurrentDisplayOutput->width && height == self->mCurrentDisplayOutput->height && self->mUpdateRenderRectangle) { |
| 424 | self->mUpdateRenderRectangle = false; |
| 425 | self->setRenderRectangle(self->mCurrentDisplayOutput->offsetX, |
| 426 | self->mCurrentDisplayOutput->offsetY, |
| 427 | self->mCurrentDisplayOutput->width, |
| 428 | self->mCurrentDisplayOutput->height); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 429 | } else{ |
| 430 | self->setRenderRectangle(self->mRenderRect.x, self->mRenderRect.y, width, height); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | static const struct xdg_toplevel_listener xdg_toplevel_listener = { |
| 435 | WaylandDisplay::handleXdgToplevelConfigure, |
| 436 | WaylandDisplay::handleXdgToplevelClose, |
| 437 | }; |
| 438 | |
| 439 | void WaylandDisplay::handleXdgSurfaceConfigure (void *data, struct xdg_surface *xdg_surface, |
| 440 | uint32_t serial) |
| 441 | { |
| 442 | WaylandDisplay *self = static_cast<WaylandDisplay *>(data); |
| 443 | xdg_surface_ack_configure (xdg_surface, serial); |
| 444 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 445 | TRACE(self->mLogCategory,"handleXdgSurfaceConfigure"); |
| 446 | Tls::Mutex::Autolock _l(self->mConfigureMutex); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 447 | self->mXdgSurfaceConfigured = true; |
fei.deng | 2695083 | 2023-11-09 03:00:23 +0000 | [diff] [blame] | 448 | self->updateDisplayOutput(); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static const struct xdg_surface_listener xdg_surface_listener = { |
| 452 | WaylandDisplay::handleXdgSurfaceConfigure, |
| 453 | }; |
| 454 | |
fei.deng | 640c3c9 | 2024-04-12 08:31:19 +0000 | [diff] [blame] | 455 | void WaylandDisplay::amlConfigure(void *data, struct aml_config *config, const char *list) { |
| 456 | WaylandDisplay *self = static_cast<WaylandDisplay *>(data); |
| 457 | TRACE(self->mLogCategory,"aml_config:%s",list); |
| 458 | if (list && strlen(list) > 0) { |
| 459 | if (strstr(list, "set_video_plane")) { |
| 460 | TRACE(self->mLogCategory,"weston enable set_video_plane"); |
| 461 | self->mAmlConfigAPIList.enableSetVideoPlane = true; |
| 462 | } |
| 463 | if (strstr(list, "set_pts")) { |
| 464 | TRACE(self->mLogCategory,"weston enable set_pts"); |
| 465 | self->mAmlConfigAPIList.enableSetPts = true; |
| 466 | } |
| 467 | if (strstr(list, "drop")) { |
| 468 | TRACE(self->mLogCategory,"weston enable drop"); |
| 469 | self->mAmlConfigAPIList.enableDropFrame = true; |
| 470 | } |
| 471 | if (strstr(list, "keep_last_frame")) { |
| 472 | TRACE(self->mLogCategory,"weston enable keep_last_frame"); |
| 473 | self->mAmlConfigAPIList.enableKeepLastFrame = true; |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | static const struct aml_config_listener aml_config_listener = { |
| 479 | WaylandDisplay::amlConfigure, |
| 480 | }; |
| 481 | |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 482 | void |
| 483 | WaylandDisplay::registryHandleGlobal (void *data, struct wl_registry *registry, |
fei.deng | af9b07d | 2023-10-10 07:38:40 +0000 | [diff] [blame] | 484 | uint32_t name, const char *interface, uint32_t version) |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 485 | { |
| 486 | WaylandDisplay *self = static_cast<WaylandDisplay *>(data); |
fei.deng | af9b07d | 2023-10-10 07:38:40 +0000 | [diff] [blame] | 487 | TRACE(self->mLogCategory,"registryHandleGlobal,name:%u,interface:%s,version:%d",name,interface,version); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 488 | |
| 489 | if (strcmp (interface, "wl_compositor") == 0) { |
fei.deng | af9b07d | 2023-10-10 07:38:40 +0000 | [diff] [blame] | 490 | self->mCompositor = (struct wl_compositor *)wl_registry_bind (registry, name, &wl_compositor_interface, 1/*MIN (version, 3)*/); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 491 | } else if (strcmp (interface, "wl_subcompositor") == 0) { |
fei.deng | af9b07d | 2023-10-10 07:38:40 +0000 | [diff] [blame] | 492 | self->mSubCompositor = (struct wl_subcompositor *)wl_registry_bind (registry, name, &wl_subcompositor_interface, 1); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 493 | } else if (strcmp (interface, "xdg_wm_base") == 0) { |
fei.deng | af9b07d | 2023-10-10 07:38:40 +0000 | [diff] [blame] | 494 | self->mXdgWmBase = (struct xdg_wm_base *)wl_registry_bind (registry, name, &xdg_wm_base_interface, 1); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 495 | xdg_wm_base_add_listener (self->mXdgWmBase, &xdg_wm_base_listener, (void *)self); |
| 496 | } else if (strcmp (interface, "wl_shm") == 0) { |
fei.deng | af9b07d | 2023-10-10 07:38:40 +0000 | [diff] [blame] | 497 | self->mShm = (struct wl_shm *)wl_registry_bind (registry, name, &wl_shm_interface, 1); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 498 | wl_shm_add_listener (self->mShm, &shm_listener, self); |
| 499 | } else if (strcmp (interface, "zwp_fullscreen_shell_v1") == 0) { |
fei.deng | af9b07d | 2023-10-10 07:38:40 +0000 | [diff] [blame] | 500 | //self->mFullscreenShell = (struct zwp_fullscreen_shell_v1 *)wl_registry_bind (registry, name, |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 501 | // &zwp_fullscreen_shell_v1_interface, 1); |
| 502 | } else if (strcmp (interface, "wp_viewporter") == 0) { |
fei.deng | af9b07d | 2023-10-10 07:38:40 +0000 | [diff] [blame] | 503 | self->mViewporter = (struct wp_viewporter *)wl_registry_bind (registry, name, &wp_viewporter_interface, 1); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 504 | } else if (strcmp (interface, "zwp_linux_dmabuf_v1") == 0) { |
| 505 | if (version < 3) |
| 506 | return; |
fei.deng | af9b07d | 2023-10-10 07:38:40 +0000 | [diff] [blame] | 507 | self->mDmabuf = (struct zwp_linux_dmabuf_v1 *)wl_registry_bind (registry, name, &zwp_linux_dmabuf_v1_interface, 3); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 508 | zwp_linux_dmabuf_v1_add_listener (self->mDmabuf, &dmabuf_listener, (void *)self); |
| 509 | } else if (strcmp (interface, "wl_output") == 0) { |
fei.deng | 2695083 | 2023-11-09 03:00:23 +0000 | [diff] [blame] | 510 | int i = 0; |
| 511 | uint32_t oriName = self->mCurrentDisplayOutput->name; |
fei.deng | af9b07d | 2023-10-10 07:38:40 +0000 | [diff] [blame] | 512 | for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) { |
| 513 | if (self->mOutput[i].wlOutput == NULL) { |
| 514 | self->mOutput[i].name = name; |
| 515 | self->mOutput[i].wlOutput = (struct wl_output*)wl_registry_bind(registry, name, &wl_output_interface, version); |
fei.deng | 2695083 | 2023-11-09 03:00:23 +0000 | [diff] [blame] | 516 | TRACE(self->mLogCategory,"name:%u, wl_output:%p, select:%d",self->mOutput[i].name,self->mOutput[i].wlOutput,self->mSelectOutputIndex); |
fei.deng | af9b07d | 2023-10-10 07:38:40 +0000 | [diff] [blame] | 517 | wl_output_add_listener(self->mOutput[i].wlOutput, &outputListener, (void *)self); |
| 518 | if (i == 0) { //primary wl_output |
| 519 | self->mOutput[i].isPrimary = true; |
| 520 | } |
fei.deng | 2695083 | 2023-11-09 03:00:23 +0000 | [diff] [blame] | 521 | break; |
fei.deng | af9b07d | 2023-10-10 07:38:40 +0000 | [diff] [blame] | 522 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 523 | } |
fei.deng | 2695083 | 2023-11-09 03:00:23 +0000 | [diff] [blame] | 524 | if (i == DEFAULT_DISPLAY_OUTPUT_NUM) { |
| 525 | WARNING(self->mLogCategory,"Not enough free output"); |
| 526 | } |
| 527 | //select current wl_output |
| 528 | if (self->mSelectOutputIndex != INVALID_OUTPUT_INDEX && !self->isRunning()) { |
| 529 | TRACE(self->mLogCategory,"select %d output",self->mSelectOutputIndex); |
| 530 | self->mCurrentDisplayOutput = &self->mOutput[self->mSelectOutputIndex]; |
| 531 | } |
| 532 | //if user select a wrong output index, we using a suiteble wl_output |
| 533 | if (self->mCurrentDisplayOutput->wlOutput == NULL) { |
| 534 | WARNING(self->mLogCategory,"wl_output is null,we should find a suiteble output"); |
| 535 | for (i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) { |
| 536 | if (self->mOutput[i].wlOutput) { |
| 537 | self->mCurrentDisplayOutput = &self->mOutput[i]; |
| 538 | break; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | //if current wl_output update, we should update render rectangle |
| 543 | if (self->mCurrentDisplayOutput->name != oriName) { |
| 544 | self->mUpdateRenderRectangle = true; |
| 545 | } |
| 546 | //if wl_output plugin,active sending frame |
| 547 | self->setRedrawingPending(false); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 548 | } else if (strcmp(interface, "wl_seat") == 0) { |
fei.deng | af9b07d | 2023-10-10 07:38:40 +0000 | [diff] [blame] | 549 | //self->mSeat = (struct wl_seat *)wl_registry_bind(registry, name, &wl_seat_interface, 1); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 550 | //wl_seat_add_listener(self->mSeat, &seat_listener, (void *)self); |
fei.deng | 31f93f1 | 2024-02-27 07:52:10 +0000 | [diff] [blame] | 551 | } else if (strcmp(interface, "weston_direct_display_v1") == 0) { |
| 552 | self->mDirect_display = (struct weston_direct_display_v1 *)wl_registry_bind(registry,name, &weston_direct_display_v1_interface, 1); |
fei.deng | 640c3c9 | 2024-04-12 08:31:19 +0000 | [diff] [blame] | 553 | } else if (strcmp(interface, "aml_config") == 0) { |
| 554 | self->mAmlConfig = (struct aml_config*)wl_registry_bind(registry, name, &aml_config_interface, 1); |
| 555 | aml_config_add_listener(self->mAmlConfig, &aml_config_listener, (void *)self); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
| 559 | void |
| 560 | WaylandDisplay::registryHandleGlobalRemove (void *data, struct wl_registry *registry, uint32_t name) |
| 561 | { |
fei.deng | 2695083 | 2023-11-09 03:00:23 +0000 | [diff] [blame] | 562 | int i; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 563 | WaylandDisplay *self = static_cast<WaylandDisplay *>(data); |
fei.deng | af9b07d | 2023-10-10 07:38:40 +0000 | [diff] [blame] | 564 | /* check wl_output changed */ |
| 565 | DEBUG(self->mLogCategory,"wayland display remove registry handle global,name:%u",name); |
fei.deng | 2695083 | 2023-11-09 03:00:23 +0000 | [diff] [blame] | 566 | //if user selected wl_output removed, reset selected output index |
| 567 | if (self->mSelectOutputIndex != INVALID_OUTPUT_INDEX && |
| 568 | self->mOutput[self->mSelectOutputIndex].wlOutput && |
| 569 | self->mOutput[self->mSelectOutputIndex].name == name) { |
| 570 | self->mSelectOutputIndex = INVALID_OUTPUT_INDEX; |
| 571 | } |
| 572 | for (i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) { |
fei.deng | af9b07d | 2023-10-10 07:38:40 +0000 | [diff] [blame] | 573 | if (self->mOutput[i].name == name) { |
fei.deng | 2695083 | 2023-11-09 03:00:23 +0000 | [diff] [blame] | 574 | DEBUG(self->mLogCategory,"remove wl_output name:%u,wl_output:%p",name,self->mOutput[i].wlOutput); |
fei.deng | af9b07d | 2023-10-10 07:38:40 +0000 | [diff] [blame] | 575 | self->mOutput[i].name = 0; |
| 576 | self->mOutput[i].wlOutput = NULL; |
fei.deng | 2695083 | 2023-11-09 03:00:23 +0000 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | //if current output removed, select a suiteble output |
| 580 | if (self->mCurrentDisplayOutput->wlOutput == NULL) { |
| 581 | for (i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) { |
| 582 | if (self->mOutput[i].wlOutput) { |
| 583 | self->mCurrentDisplayOutput = &self->mOutput[i]; |
| 584 | self->mUpdateRenderRectangle = true; |
| 585 | } |
| 586 | } |
| 587 | //set new output rectangle |
| 588 | if (self->mUpdateRenderRectangle) { |
| 589 | self->mUpdateRenderRectangle = false; |
| 590 | self->setRenderRectangle(self->mCurrentDisplayOutput->offsetX, |
| 591 | self->mCurrentDisplayOutput->offsetY, |
| 592 | self->mCurrentDisplayOutput->width, |
| 593 | self->mCurrentDisplayOutput->height); |
fei.deng | af9b07d | 2023-10-10 07:38:40 +0000 | [diff] [blame] | 594 | } |
| 595 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | static const struct wl_registry_listener registry_listener = { |
| 599 | WaylandDisplay::registryHandleGlobal, |
| 600 | WaylandDisplay::registryHandleGlobalRemove |
| 601 | }; |
| 602 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 603 | WaylandDisplay::WaylandDisplay(WaylandPlugin *plugin, int logCategory) |
| 604 | :mBufferMutex("bufferMutex"), |
| 605 | mWaylandPlugin(plugin), |
| 606 | mLogCategory(logCategory) |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 607 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 608 | TRACE(mLogCategory,"construct WaylandDisplay"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 609 | mWlDisplay = NULL; |
| 610 | mWlDisplayWrapper = NULL; |
| 611 | mWlQueue = NULL; |
| 612 | mRegistry = NULL; |
| 613 | mCompositor = NULL; |
| 614 | mXdgWmBase = NULL; |
| 615 | mViewporter = NULL; |
| 616 | mDmabuf = NULL; |
| 617 | mShm = NULL; |
| 618 | mSeat = NULL; |
| 619 | mPointer = NULL; |
| 620 | mTouch = NULL; |
| 621 | mKeyboard = NULL; |
fei.deng | 31f93f1 | 2024-02-27 07:52:10 +0000 | [diff] [blame] | 622 | mDirect_display = NULL; |
fei.deng | 2695083 | 2023-11-09 03:00:23 +0000 | [diff] [blame] | 623 | mSelectOutputIndex = INVALID_OUTPUT_INDEX; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 624 | mPoll = new Tls::Poll(true); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 625 | //window |
| 626 | mVideoWidth = 0; |
| 627 | mVideoHeight = 0; |
| 628 | mVideoSurface = NULL; |
| 629 | mXdgSurface = NULL; |
| 630 | mXdgToplevel = NULL; |
| 631 | mAreaViewport = NULL; |
| 632 | mVideoViewport = NULL; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 633 | mAreaShmBuffer = NULL; |
| 634 | mCommitCnt = 0; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 635 | mAreaSurface = NULL; |
| 636 | mAreaSurfaceWrapper = NULL; |
| 637 | mVideoSurfaceWrapper = NULL; |
| 638 | mVideoSubSurface = NULL; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 639 | mPip = 0; |
fei.deng | 640c3c9 | 2024-04-12 08:31:19 +0000 | [diff] [blame] | 640 | mKeepLastFrame = 0; //default is off keep last frame |
fei.deng | 3287c08 | 2024-04-23 09:29:22 +0000 | [diff] [blame] | 641 | mSignalFirstFramePts = false; |
fei.deng | 4029e68 | 2024-06-26 17:06:31 +0800 | [diff] [blame] | 642 | mToSendKeepLastFrame = false; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | WaylandDisplay::~WaylandDisplay() |
| 646 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 647 | TRACE(mLogCategory,"desconstruct WaylandDisplay"); |
| 648 | if (mPoll) { |
| 649 | delete mPoll; |
| 650 | mPoll = NULL; |
| 651 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | char *WaylandDisplay::require_xdg_runtime_dir() |
| 655 | { |
| 656 | char *val = getenv("XDG_RUNTIME_DIR"); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 657 | INFO(mLogCategory,"XDG_RUNTIME_DIR=%s",val); |
fei.deng | dd910ef | 2024-06-07 10:25:30 +0800 | [diff] [blame] | 658 | //if not set XDG_RUNTIME_DIR,set default value |
| 659 | // if (!val) { |
| 660 | // val = const_cast<char *>("/run/user/0"); |
| 661 | // setenv("XDG_RUNTIME_DIR",val,0); |
| 662 | // WARNING(mLogCategory,"XDG_RUNTIME_DIR is not set,set default %s",val); |
| 663 | // } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 664 | |
| 665 | return val; |
| 666 | } |
| 667 | |
| 668 | int WaylandDisplay::openDisplay() |
| 669 | { |
fei.deng | dd910ef | 2024-06-07 10:25:30 +0800 | [diff] [blame] | 670 | mNoBorderUpdate = false; |
| 671 | mReCommitAreaSurface = false; |
| 672 | mXdgSurfaceConfigured = false; |
| 673 | mUpdateRenderRectangle = false; |
| 674 | memset(&mRenderRect, 0, sizeof(struct Rectangle)); |
| 675 | memset(&mVideoRect, 0, sizeof(struct Rectangle)); |
| 676 | memset(&mWindowRect, 0, sizeof(struct Rectangle)); |
| 677 | mFullScreen = true; //default is full screen |
| 678 | mAmlConfig = NULL; |
| 679 | //weston config private api |
| 680 | mAmlConfigAPIList.enableDropFrame = false; |
| 681 | mAmlConfigAPIList.enableKeepLastFrame = false; |
| 682 | mAmlConfigAPIList.enableSetPts = false; |
| 683 | mAmlConfigAPIList.enableSetVideoPlane = false; |
| 684 | mCurrentDisplayOutput = &mOutput[0]; |
| 685 | for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) { |
| 686 | mOutput[i].wlOutput = NULL; |
| 687 | mOutput[i].offsetX = 0; |
| 688 | mOutput[i].offsetY = 0; |
| 689 | mOutput[i].width = 0; |
| 690 | mOutput[i].height = 0; |
| 691 | mOutput[i].refreshRate = 0; |
| 692 | mOutput[i].isPrimary = false; |
| 693 | mOutput[i].name = 0; |
| 694 | mOutput[i].crtcIndex = 0; |
| 695 | } |
fei.deng | 4029e68 | 2024-06-26 17:06:31 +0800 | [diff] [blame] | 696 | /*if mKeepLastFrame had set but mToSendKeepLastFrame is false,maybe |
| 697 | playback is doing FF/FW action,so we keep set it on new connection*/ |
| 698 | if (!mToSendKeepLastFrame && mKeepLastFrame) { |
| 699 | mToSendKeepLastFrame = true; |
| 700 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 701 | char *name = require_xdg_runtime_dir(); |
| 702 | //DEBUG(mLogCategory,"name:%s",name); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 703 | DEBUG(mLogCategory,"openDisplay in"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 704 | mWlDisplay = wl_display_connect(NULL); |
| 705 | if (!mWlDisplay) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 706 | ERROR(mLogCategory,"Failed to connect to the wayland display, XDG_RUNTIME_DIR='%s'", |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 707 | name ? name : "NULL"); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 708 | return ERROR_OPEN_FAIL; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | mWlDisplayWrapper = (struct wl_display *)wl_proxy_create_wrapper ((void *)mWlDisplay); |
| 712 | mWlQueue = wl_display_create_queue (mWlDisplay); |
| 713 | wl_proxy_set_queue ((struct wl_proxy *)mWlDisplayWrapper, mWlQueue); |
| 714 | |
| 715 | mRegistry = wl_display_get_registry (mWlDisplayWrapper); |
| 716 | wl_registry_add_listener (mRegistry, ®istry_listener, (void *)this); |
| 717 | |
| 718 | /* we need exactly 2 roundtrips to discover global objects and their state */ |
| 719 | for (int i = 0; i < 2; i++) { |
| 720 | if (wl_display_roundtrip_queue (mWlDisplay, mWlQueue) < 0) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 721 | ERROR(mLogCategory,"Error communicating with the wayland display"); |
| 722 | return ERROR_OPEN_FAIL; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
| 726 | if (!mCompositor) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 727 | ERROR(mLogCategory,"Could not bind to wl_compositor. Either it is not implemented in " \ |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 728 | "the compositor, or the implemented version doesn't match"); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 729 | return ERROR_OPEN_FAIL; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | if (!mDmabuf) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 733 | ERROR(mLogCategory,"Could not bind to zwp_linux_dmabuf_v1"); |
| 734 | return ERROR_OPEN_FAIL; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | if (!mXdgWmBase) { |
| 738 | /* If wl_surface and wl_display are passed via GstContext |
| 739 | * wl_shell, xdg_shell and zwp_fullscreen_shell are not used. |
| 740 | * In this case is correct to continue. |
| 741 | */ |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 742 | ERROR(mLogCategory,"Could not bind to either wl_shell, xdg_wm_base or " |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 743 | "zwp_fullscreen_shell, video display may not work properly."); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 744 | return ERROR_OPEN_FAIL; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | //create window surface |
| 748 | createCommonWindowSurface(); |
| 749 | createXdgShellWindowSurface(); |
| 750 | |
| 751 | //config weston video plane |
fei.deng | 640c3c9 | 2024-04-12 08:31:19 +0000 | [diff] [blame] | 752 | if (mAmlConfigAPIList.enableSetVideoPlane) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 753 | INFO(mLogCategory,"set weston video plane:%d",mPip); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 754 | wl_surface_set_video_plane(mVideoSurfaceWrapper, mPip); |
| 755 | } |
| 756 | |
| 757 | //run wl display queue dispatch |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 758 | DEBUG(mLogCategory,"To run wl display dispatch queue"); |
fei.deng | dd910ef | 2024-06-07 10:25:30 +0800 | [diff] [blame] | 759 | if (mPoll) { |
| 760 | mPoll->setFlushing(false); |
| 761 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 762 | run("display queue"); |
| 763 | mRedrawingPending = false; |
| 764 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 765 | DEBUG(mLogCategory,"openDisplay out"); |
| 766 | return NO_ERROR; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | void WaylandDisplay::closeDisplay() |
| 770 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 771 | DEBUG(mLogCategory,"closeDisplay in"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 772 | |
| 773 | if (isRunning()) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 774 | TRACE(mLogCategory,"try stop dispatch thread"); |
| 775 | if (mPoll) { |
| 776 | mPoll->setFlushing(true); |
| 777 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 778 | requestExitAndWait(); |
| 779 | } |
| 780 | |
fei.deng | 4029e68 | 2024-06-26 17:06:31 +0800 | [diff] [blame] | 781 | //flush pending event to weston |
fei.deng | dd910ef | 2024-06-07 10:25:30 +0800 | [diff] [blame] | 782 | if (mWlDisplay) { |
| 783 | wl_display_flush (mWlDisplay); |
| 784 | } |
| 785 | |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 786 | //first destroy window surface |
| 787 | destroyWindowSurfaces(); |
| 788 | |
| 789 | if (mViewporter) { |
| 790 | wp_viewporter_destroy (mViewporter); |
| 791 | mViewporter = NULL; |
| 792 | } |
| 793 | |
| 794 | if (mDmabuf) { |
| 795 | zwp_linux_dmabuf_v1_destroy (mDmabuf); |
| 796 | mDmabuf = NULL; |
| 797 | } |
| 798 | |
| 799 | if (mXdgWmBase) { |
| 800 | xdg_wm_base_destroy (mXdgWmBase); |
| 801 | mXdgWmBase = NULL; |
| 802 | } |
| 803 | |
| 804 | if (mCompositor) { |
| 805 | wl_compositor_destroy (mCompositor); |
| 806 | mCompositor = NULL; |
| 807 | } |
| 808 | |
| 809 | if (mSubCompositor) { |
| 810 | wl_subcompositor_destroy (mSubCompositor); |
| 811 | mSubCompositor = NULL; |
| 812 | } |
| 813 | |
| 814 | if (mRegistry) { |
| 815 | wl_registry_destroy (mRegistry); |
| 816 | mRegistry= NULL; |
| 817 | } |
| 818 | |
| 819 | if (mWlDisplayWrapper) { |
| 820 | wl_proxy_wrapper_destroy (mWlDisplayWrapper); |
| 821 | mWlDisplayWrapper = NULL; |
| 822 | } |
| 823 | |
fei.deng | 640c3c9 | 2024-04-12 08:31:19 +0000 | [diff] [blame] | 824 | if (mAmlConfig) { |
| 825 | aml_config_destroy(mAmlConfig); |
| 826 | mAmlConfig = NULL; |
| 827 | } |
| 828 | |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 829 | if (mWlQueue) { |
| 830 | wl_event_queue_destroy (mWlQueue); |
| 831 | mWlQueue = NULL; |
| 832 | } |
| 833 | |
| 834 | if (mWlDisplay) { |
| 835 | wl_display_flush (mWlDisplay); |
| 836 | wl_display_disconnect (mWlDisplay); |
| 837 | mWlDisplay = NULL; |
| 838 | } |
| 839 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 840 | DEBUG(mLogCategory,"closeDisplay out"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | int WaylandDisplay::toDmaBufferFormat(RenderVideoFormat format, uint32_t *outDmaformat /*out param*/, uint64_t *outDmaformatModifiers /*out param*/) |
| 844 | { |
| 845 | if (!outDmaformat || !outDmaformatModifiers) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 846 | WARNING(mLogCategory,"NULL params"); |
| 847 | return ERROR_PARAM_NULL; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | *outDmaformat = 0; |
| 851 | *outDmaformatModifiers = 0; |
| 852 | |
| 853 | uint32_t dmaformat = video_format_to_wl_dmabuf_format (format); |
| 854 | if (dmaformat == -1) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 855 | ERROR(mLogCategory,"Error not found render video format:%d to wl dmabuf format",format); |
| 856 | return ERROR_NOT_FOUND; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 857 | } |
| 858 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 859 | TRACE(mLogCategory,"render video format:%d -> dmabuf format:%d",format,dmaformat); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 860 | *outDmaformat = (uint32_t)dmaformat; |
| 861 | |
| 862 | /*get dmaformat and modifiers*/ |
| 863 | auto item = mDmaBufferFormats.find(dmaformat); |
| 864 | if (item == mDmaBufferFormats.end()) { //not found |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 865 | WARNING(mLogCategory,"Not found dmabuf for render video format :%d",format); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 866 | *outDmaformatModifiers = 0; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 867 | return NO_ERROR; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | *outDmaformatModifiers = (uint64_t)item->second; |
| 871 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 872 | return NO_ERROR; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | int WaylandDisplay::toShmBufferFormat(RenderVideoFormat format, uint32_t *outformat) |
| 876 | { |
| 877 | if (!outformat) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 878 | WARNING(mLogCategory,"NULL params"); |
| 879 | return ERROR_PARAM_NULL; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | *outformat = 0; |
| 883 | |
| 884 | int shmformat = (int)video_format_to_wl_shm_format(format); |
| 885 | if (shmformat < 0) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 886 | ERROR(mLogCategory,"Error not found render video format:%d to wl shmbuf format",format); |
| 887 | return ERROR_NOT_FOUND; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | for (auto item = mShmFormats.begin(); item != mShmFormats.end(); ++item) { |
| 891 | uint32_t registFormat = (uint32_t)*item; |
| 892 | if (registFormat == (uint32_t)shmformat) { |
| 893 | *outformat = registFormat; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 894 | return NO_ERROR; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 898 | return ERROR_NOT_FOUND; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | void WaylandDisplay::setVideoBufferFormat(RenderVideoFormat format) |
| 902 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 903 | TRACE(mLogCategory,"set video buffer format: %d",format); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 904 | mBufferFormat = format; |
| 905 | }; |
| 906 | |
| 907 | void WaylandDisplay::setDisplayOutput(int output) |
| 908 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 909 | TRACE(mLogCategory,"select display output: %d",output); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 910 | if (output < 0 || output >= DEFAULT_DISPLAY_OUTPUT_NUM) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 911 | ERROR(mLogCategory, "display output index error,please set 0:primary or 1:extend,now:%d",output); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 912 | return; |
| 913 | } |
fei.deng | 2695083 | 2023-11-09 03:00:23 +0000 | [diff] [blame] | 914 | //only do select output before video playing |
| 915 | if (mSelectOutputIndex != output) { |
| 916 | mSelectOutputIndex = output; |
| 917 | // if (mOutput[output].wlOutput) { |
| 918 | // mCurrentDisplayOutput = &mOutput[output]; |
| 919 | // setRenderRectangle(mOutput[output].offsetX, mOutput[output].offsetY, |
| 920 | // mOutput[output].width, mOutput[output].height); |
| 921 | // } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | |
| 925 | int WaylandDisplay::getDisplayOutput() |
| 926 | { |
fei.deng | 2695083 | 2023-11-09 03:00:23 +0000 | [diff] [blame] | 927 | return mSelectOutputIndex == INVALID_OUTPUT_INDEX? 0: mSelectOutputIndex; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | void WaylandDisplay::setPip(int pip) |
| 931 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 932 | INFO(mLogCategory,"set pip:%d",pip); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 933 | mPip = pip; |
| 934 | } |
| 935 | |
fei.deng | 2695083 | 2023-11-09 03:00:23 +0000 | [diff] [blame] | 936 | void WaylandDisplay::updateDisplayOutput() |
| 937 | { |
| 938 | if (!mCurrentDisplayOutput->wlOutput || !mXdgToplevel || !mXdgSurface) |
| 939 | { |
| 940 | return; |
| 941 | } |
| 942 | if (mUpdateRenderRectangle) { |
| 943 | if (mFullScreen) { |
| 944 | DEBUG(mLogCategory,"unset full screen"); |
| 945 | xdg_toplevel_unset_fullscreen (mXdgToplevel); |
| 946 | } |
| 947 | |
| 948 | if (mXdgSurface) { |
| 949 | DEBUG(mLogCategory,"set geometry"); |
| 950 | xdg_surface_set_window_geometry(mXdgSurface, |
| 951 | mCurrentDisplayOutput->offsetX, |
| 952 | mCurrentDisplayOutput->offsetY, |
| 953 | mCurrentDisplayOutput->width, |
| 954 | mCurrentDisplayOutput->height); |
| 955 | } |
| 956 | |
| 957 | if (mFullScreen && mXdgToplevel) { |
| 958 | DEBUG(mLogCategory,"set full screen"); |
| 959 | xdg_toplevel_set_fullscreen (mXdgToplevel, mCurrentDisplayOutput->wlOutput); |
| 960 | } |
| 961 | setRenderRectangle(mCurrentDisplayOutput->offsetX, mCurrentDisplayOutput->offsetY, |
| 962 | mCurrentDisplayOutput->width, mCurrentDisplayOutput->height); |
| 963 | mUpdateRenderRectangle = false; |
| 964 | } |
| 965 | } |
| 966 | |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 967 | void WaylandDisplay::createCommonWindowSurface() |
| 968 | { |
| 969 | struct wl_region *region; |
| 970 | |
| 971 | mAreaSurface = wl_compositor_create_surface (mCompositor); |
| 972 | mVideoSurface = wl_compositor_create_surface (mCompositor); |
| 973 | mAreaSurfaceWrapper = (struct wl_surface *)wl_proxy_create_wrapper (mAreaSurface); |
| 974 | mVideoSurfaceWrapper = (struct wl_surface *)wl_proxy_create_wrapper (mVideoSurface); |
| 975 | |
| 976 | wl_proxy_set_queue ((struct wl_proxy *) mAreaSurfaceWrapper, mWlQueue); |
| 977 | wl_proxy_set_queue ((struct wl_proxy *) mVideoSurfaceWrapper, mWlQueue); |
| 978 | |
| 979 | /* embed video_surface in area_surface */ |
| 980 | mVideoSubSurface = wl_subcompositor_get_subsurface (mSubCompositor, mVideoSurface, mAreaSurface); |
| 981 | wl_subsurface_set_desync (mVideoSubSurface); |
| 982 | |
| 983 | if (mViewporter) { |
| 984 | mAreaViewport = wp_viewporter_get_viewport (mViewporter, mAreaSurface); |
| 985 | mVideoViewport = wp_viewporter_get_viewport (mViewporter, mVideoSurface); |
| 986 | } |
| 987 | |
| 988 | /* do not accept input */ |
| 989 | region = wl_compositor_create_region (mCompositor); |
| 990 | wl_surface_set_input_region (mAreaSurface, region); |
| 991 | wl_region_destroy (region); |
| 992 | |
| 993 | region = wl_compositor_create_region (mCompositor); |
| 994 | wl_surface_set_input_region (mVideoSurface, region); |
| 995 | wl_region_destroy (region); |
| 996 | } |
| 997 | |
| 998 | void WaylandDisplay::createXdgShellWindowSurface() |
| 999 | { |
| 1000 | /* Check which protocol we will use (in order of preference) */ |
| 1001 | if (mXdgWmBase) { |
| 1002 | /* First create the XDG surface */ |
| 1003 | mXdgSurface= xdg_wm_base_get_xdg_surface (mXdgWmBase, mAreaSurface); |
| 1004 | if (!mXdgSurface) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1005 | ERROR(mLogCategory,"Unable to get xdg_surface"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1006 | return; |
| 1007 | } |
| 1008 | xdg_surface_add_listener (mXdgSurface, &xdg_surface_listener,(void *)this); |
| 1009 | |
| 1010 | /* Then the toplevel */ |
| 1011 | mXdgToplevel= xdg_surface_get_toplevel (mXdgSurface); |
| 1012 | if (!mXdgSurface) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1013 | ERROR(mLogCategory,"Unable to get xdg_toplevel"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1014 | return; |
| 1015 | } |
| 1016 | xdg_toplevel_add_listener (mXdgToplevel, &xdg_toplevel_listener, this); |
| 1017 | |
| 1018 | /* Finally, commit the xdg_surface state as toplevel */ |
| 1019 | mXdgSurfaceConfigured = false; |
| 1020 | wl_surface_commit (mAreaSurface); |
| 1021 | wl_display_flush (mWlDisplay); |
| 1022 | /* we need exactly 3 roundtrips to discover global objects and their state */ |
| 1023 | for (int i = 0; i < 3; i++) { |
| 1024 | if (wl_display_roundtrip_queue(mWlDisplay, mWlQueue) < 0) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1025 | ERROR(mLogCategory,"Error communicating with the wayland display"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | if (mXdgSurfaceConfigured) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1030 | INFO(mLogCategory,"xdg surface had configured"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1031 | } else { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1032 | WARNING(mLogCategory,"xdg surface not configured"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | //full screen show |
fei.deng | 2695083 | 2023-11-09 03:00:23 +0000 | [diff] [blame] | 1036 | // if (mFullScreen && mCurrentDisplayOutput->wlOutput) { |
| 1037 | // //ensureFullscreen(mFullScreen); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1038 | // } |
| 1039 | } else { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1040 | ERROR(mLogCategory,"Unable to use xdg_wm_base "); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1041 | return; |
| 1042 | } |
| 1043 | } |
| 1044 | |
| 1045 | void WaylandDisplay::destroyWindowSurfaces() |
| 1046 | { |
| 1047 | if (mAreaShmBuffer) { |
| 1048 | delete mAreaShmBuffer; |
| 1049 | mAreaShmBuffer = NULL; |
| 1050 | } |
| 1051 | |
| 1052 | //clean all wayland buffers |
| 1053 | cleanAllWaylandBuffer(); |
| 1054 | |
| 1055 | if (mXdgToplevel) { |
| 1056 | xdg_toplevel_destroy (mXdgToplevel); |
| 1057 | mXdgToplevel = NULL; |
| 1058 | } |
| 1059 | |
| 1060 | if (mXdgSurface) { |
| 1061 | xdg_surface_destroy (mXdgSurface); |
| 1062 | mXdgSurface = NULL; |
| 1063 | } |
| 1064 | |
| 1065 | if (mVideoSurfaceWrapper) { |
| 1066 | wl_proxy_wrapper_destroy (mVideoSurfaceWrapper); |
| 1067 | mVideoSurfaceWrapper = NULL; |
| 1068 | } |
| 1069 | |
| 1070 | if (mVideoSubSurface) { |
| 1071 | wl_subsurface_destroy (mVideoSubSurface); |
| 1072 | mVideoSubSurface = NULL; |
| 1073 | } |
| 1074 | |
| 1075 | if (mVideoSurface) { |
| 1076 | wl_surface_destroy (mVideoSurface); |
| 1077 | mVideoSurface = NULL; |
| 1078 | } |
| 1079 | |
| 1080 | if (mAreaSurfaceWrapper) { |
| 1081 | wl_proxy_wrapper_destroy (mAreaSurfaceWrapper); |
| 1082 | mAreaSurfaceWrapper = NULL; |
| 1083 | } |
| 1084 | |
| 1085 | if (mAreaSurface) { |
| 1086 | wl_surface_destroy (mAreaSurface); |
| 1087 | mAreaSurface = NULL; |
| 1088 | mReCommitAreaSurface = false; |
| 1089 | } |
| 1090 | } |
| 1091 | |
| 1092 | void WaylandDisplay::ensureFullscreen(bool fullscreen) |
| 1093 | { |
| 1094 | if (mXdgWmBase) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1095 | DEBUG(mLogCategory,"full screen : %d",fullscreen); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1096 | if (fullscreen) { |
fei.deng | 2695083 | 2023-11-09 03:00:23 +0000 | [diff] [blame] | 1097 | xdg_toplevel_set_fullscreen (mXdgToplevel, mCurrentDisplayOutput->wlOutput); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1098 | } else { |
| 1099 | xdg_toplevel_unset_fullscreen (mXdgToplevel); |
| 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | void WaylandDisplay::setRenderRectangle(int x, int y, int w, int h) |
| 1105 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1106 | DEBUG(mLogCategory,"set render rect:x:%d,y:%d,w:%d,h:%d",x,y,w,h); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1107 | |
| 1108 | if (w <= 0 || h <= 0) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1109 | WARNING(mLogCategory, "wrong render width or height %dx%d",w,h); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1110 | return; |
| 1111 | } |
| 1112 | |
| 1113 | mRenderRect.x = x; |
| 1114 | mRenderRect.y = y; |
| 1115 | mRenderRect.w = w; |
| 1116 | mRenderRect.h = h; |
| 1117 | |
| 1118 | if (!mXdgSurfaceConfigured) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1119 | WARNING(mLogCategory,"Not configured xdg"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1120 | return; |
| 1121 | } |
| 1122 | |
| 1123 | if (mAreaViewport) { |
| 1124 | wp_viewport_set_destination (mAreaViewport, w, h); |
| 1125 | } |
| 1126 | |
| 1127 | updateBorders(); |
| 1128 | |
| 1129 | if (mVideoWidth != 0 && mVideoSurface) { |
| 1130 | wl_subsurface_set_sync (mVideoSubSurface); |
| 1131 | resizeVideoSurface(true); |
| 1132 | } |
| 1133 | |
| 1134 | wl_surface_damage (mAreaSurfaceWrapper, 0, 0, w, h); |
| 1135 | wl_surface_commit (mAreaSurfaceWrapper); |
| 1136 | |
| 1137 | if (mVideoWidth != 0) { |
| 1138 | wl_subsurface_set_desync (mVideoSubSurface); |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | |
| 1143 | void WaylandDisplay::setFrameSize(int w, int h) |
| 1144 | { |
| 1145 | mVideoWidth = w; |
| 1146 | mVideoHeight = h; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1147 | TRACE(mLogCategory,"frame w:%d,h:%d",mVideoWidth,mVideoHeight); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1148 | if (mRenderRect.w > 0 && mVideoSurface) { |
| 1149 | resizeVideoSurface(true); |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | void WaylandDisplay::setWindowSize(int x, int y, int w, int h) |
| 1154 | { |
| 1155 | mWindowRect.x = x; |
| 1156 | mWindowRect.y = y; |
| 1157 | mWindowRect.w = w; |
| 1158 | mWindowRect.h = h; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1159 | TRACE(mLogCategory,"window size:x:%d,y:%d,w:%d,h:%d",mWindowRect.x,mWindowRect.y,mWindowRect.w,mWindowRect.h); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1160 | if (mWindowRect.w > 0 && mVideoWidth > 0 && mVideoSurface) { |
| 1161 | //if had full screen, unset it and set window size |
| 1162 | if (mFullScreen) { |
| 1163 | mFullScreen = false; |
| 1164 | ensureFullscreen(mFullScreen); |
| 1165 | } |
| 1166 | resizeVideoSurface(true); |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | |
| 1171 | void WaylandDisplay::resizeVideoSurface(bool commit) |
| 1172 | { |
| 1173 | Rectangle src = {0,}; |
| 1174 | Rectangle dst = {0,}; |
| 1175 | Rectangle res; |
| 1176 | |
| 1177 | /* center the video_subsurface inside area_subsurface */ |
| 1178 | src.w = mVideoWidth; |
| 1179 | src.h = mVideoHeight; |
| 1180 | /*if had set the window size, we will scall |
| 1181 | video surface to this window size*/ |
| 1182 | if (mWindowRect.w > 0 && mWindowRect.h > 0) { |
| 1183 | dst.x = mWindowRect.x; |
| 1184 | dst.y = mWindowRect.y; |
| 1185 | dst.w = mWindowRect.w; |
| 1186 | dst.h = mWindowRect.h; |
| 1187 | if (mWindowRect.w > mRenderRect.w && mWindowRect.h > mRenderRect.h) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1188 | WARNING(mLogCategory,"Error window size:%dx%d, but render size:%dx%d,reset to render size", |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1189 | mWindowRect.w,mWindowRect.h,mRenderRect.w,mRenderRect.h); |
| 1190 | dst.x = mRenderRect.x; |
| 1191 | dst.y = mRenderRect.y; |
| 1192 | dst.w = mRenderRect.w; |
| 1193 | dst.h = mRenderRect.h; |
| 1194 | } |
| 1195 | //to do,we need set geometry? |
| 1196 | //if (mXdgSurface) { |
| 1197 | // xdg_surface_set_window_geometry(mXdgSurface, mWindowRect.x, mWindowRect.y, mWindowRect.w, mWindowRect.h); |
| 1198 | //} |
| 1199 | } else { //scal video to full screen |
| 1200 | dst.w = mRenderRect.w; |
| 1201 | dst.h = mRenderRect.h; |
| 1202 | } |
| 1203 | |
| 1204 | if (mViewporter) { |
| 1205 | videoCenterRect(src, dst, &res, true); |
| 1206 | } else { |
| 1207 | videoCenterRect(src, dst, &res, false); |
| 1208 | } |
| 1209 | |
| 1210 | wl_subsurface_set_position (mVideoSubSurface, res.x, res.y); |
| 1211 | |
| 1212 | if (commit) { |
| 1213 | wl_surface_damage (mVideoSurfaceWrapper, 0, 0, res.w, res.h); |
| 1214 | wl_surface_commit (mVideoSurfaceWrapper); |
| 1215 | } |
| 1216 | |
| 1217 | //top level setting |
| 1218 | if (mXdgToplevel) { |
| 1219 | struct wl_region *region; |
| 1220 | |
| 1221 | region = wl_compositor_create_region (mCompositor); |
| 1222 | wl_region_add (region, 0, 0, mRenderRect.w, mRenderRect.h); |
| 1223 | wl_surface_set_input_region (mAreaSurface, region); |
| 1224 | wl_region_destroy (region); |
| 1225 | } |
| 1226 | |
| 1227 | /* this is saved for use in wl_surface_damage */ |
| 1228 | mVideoRect.x = res.x; |
| 1229 | mVideoRect.y = res.y; |
| 1230 | mVideoRect.w = res.w; |
| 1231 | mVideoRect.h = res.h; |
| 1232 | |
| 1233 | //to scale video surface to full screen |
| 1234 | wp_viewport_set_destination(mVideoViewport, res.w, res.h); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1235 | TRACE(mLogCategory,"video rectangle,x:%d,y:%d,w:%d,h:%d",mVideoRect.x, mVideoRect.y, mVideoRect.w, mVideoRect.h); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | void WaylandDisplay::setOpaque() |
| 1239 | { |
| 1240 | struct wl_region *region; |
| 1241 | |
| 1242 | /* Set area opaque */ |
| 1243 | region = wl_compositor_create_region (mCompositor); |
| 1244 | wl_region_add (region, 0, 0, mRenderRect.w, mRenderRect.h); |
| 1245 | wl_surface_set_opaque_region (mAreaSurface, region); |
| 1246 | wl_region_destroy (region); |
| 1247 | } |
| 1248 | |
| 1249 | int WaylandDisplay::prepareFrameBuffer(RenderBuffer * buf) |
| 1250 | { |
| 1251 | WaylandBuffer *waylandBuf = NULL; |
| 1252 | int ret; |
| 1253 | |
fei.deng | dd910ef | 2024-06-07 10:25:30 +0800 | [diff] [blame] | 1254 | if (!mDmabuf) |
| 1255 | { |
| 1256 | ERROR(mLogCategory,"Error zwp_linux_dmabuf_v1"); |
| 1257 | return ERROR_UNKNOWN; |
| 1258 | } |
| 1259 | |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1260 | waylandBuf = findWaylandBuffer(buf); |
| 1261 | if (waylandBuf == NULL) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1262 | waylandBuf = new WaylandBuffer(this, mLogCategory); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1263 | waylandBuf->setBufferFormat(mBufferFormat); |
| 1264 | ret = waylandBuf->constructWlBuffer(buf); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1265 | if (ret != NO_ERROR) { |
| 1266 | WARNING(mLogCategory,"dmabufConstructWlBuffer fail,release waylandbuf"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1267 | //delete waylanBuf,WaylandBuffer object destruct will call release callback |
| 1268 | goto waylandbuf_fail; |
| 1269 | } else { |
| 1270 | addWaylandBuffer(buf, waylandBuf); |
| 1271 | } |
| 1272 | } |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1273 | return NO_ERROR; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1274 | waylandbuf_fail: |
| 1275 | //delete waylandbuf |
| 1276 | delete waylandBuf; |
| 1277 | waylandBuf = NULL; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1278 | return ERROR_UNKNOWN; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | void WaylandDisplay::displayFrameBuffer(RenderBuffer * buf, int64_t realDisplayTime) |
| 1282 | { |
| 1283 | WaylandBuffer *waylandBuf = NULL; |
| 1284 | struct wl_buffer * wlbuffer = NULL; |
| 1285 | int ret; |
| 1286 | |
| 1287 | if (!buf) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1288 | ERROR(mLogCategory,"Error input params, waylandbuffer is null"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1289 | return; |
| 1290 | } |
| 1291 | |
| 1292 | //must commit areasurface first, because weston xdg surface maybe timeout |
| 1293 | //this cause video is not display,commit can resume xdg surface |
| 1294 | if (!mReCommitAreaSurface) { |
| 1295 | mReCommitAreaSurface = true; |
| 1296 | wl_surface_commit (mAreaSurface); |
| 1297 | } |
| 1298 | |
fei.deng | dd910ef | 2024-06-07 10:25:30 +0800 | [diff] [blame] | 1299 | //TRACE(mLogCategory,"display renderBuffer:%p,PTS:%lld us,realtime:%lld",buf, buf->pts/1000, realDisplayTime); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1300 | |
| 1301 | if (buf->flag & BUFFER_FLAG_DMA_BUFFER) { |
| 1302 | if (buf->dma.width <=0 || buf->dma.height <=0) { |
| 1303 | buf->dma.width = mVideoWidth; |
| 1304 | buf->dma.height = mVideoHeight; |
| 1305 | } |
| 1306 | waylandBuf = findWaylandBuffer(buf); |
| 1307 | if (waylandBuf) { |
| 1308 | waylandBuf->setRenderRealTime(realDisplayTime); |
| 1309 | ret = waylandBuf->constructWlBuffer(buf); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1310 | if (ret != NO_ERROR) { |
| 1311 | WARNING(mLogCategory,"dmabufConstructWlBuffer fail,release waylandbuf"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1312 | //delete waylanBuf,WaylandBuffer object destruct will call release callback |
| 1313 | goto waylandbuf_fail; |
| 1314 | } |
| 1315 | } else { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1316 | ERROR(mLogCategory,"NOT found wayland buffer,please prepare buffer first"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1317 | goto waylandbuf_fail; |
| 1318 | } |
| 1319 | } |
| 1320 | |
| 1321 | if (waylandBuf) { |
| 1322 | wlbuffer = waylandBuf->getWlBuffer(); |
| 1323 | } |
fei.deng | 1cfb275 | 2023-10-26 08:01:25 +0000 | [diff] [blame] | 1324 | //if no wl_output, drop this buffer |
fei.deng | 2695083 | 2023-11-09 03:00:23 +0000 | [diff] [blame] | 1325 | if (mCurrentDisplayOutput->wlOutput == NULL) { |
fei.deng | 1cfb275 | 2023-10-26 08:01:25 +0000 | [diff] [blame] | 1326 | TRACE(mLogCategory,"No wl_output"); |
| 1327 | mWaylandPlugin->handleFrameDropped(buf); |
| 1328 | mWaylandPlugin->handleBufferRelease(buf); |
| 1329 | return; |
| 1330 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1331 | if (wlbuffer) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1332 | Tls::Mutex::Autolock _l(mRenderMutex); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1333 | ++mCommitCnt; |
| 1334 | uint32_t hiPts = realDisplayTime >> 32; |
| 1335 | uint32_t lowPts = realDisplayTime & 0xFFFFFFFF; |
| 1336 | //attach this wl_buffer to weston |
fei.deng | dd910ef | 2024-06-07 10:25:30 +0800 | [diff] [blame] | 1337 | TRACE(mLogCategory,"++attach,renderbuf:%p,wl_buffer:%p(0,0,%d,%d),pts:%lld us,commitCnt:%d",buf,wlbuffer,mVideoRect.w,mVideoRect.h,buf->pts/1000,mCommitCnt); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1338 | waylandBuf->attach(mVideoSurfaceWrapper); |
| 1339 | |
fei.deng | 640c3c9 | 2024-04-12 08:31:19 +0000 | [diff] [blame] | 1340 | if (mAmlConfigAPIList.enableSetPts) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1341 | TRACE(mLogCategory,"display time:%lld,hiPts:%u,lowPts:%u",realDisplayTime, hiPts, lowPts); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1342 | wl_surface_set_pts(mVideoSurfaceWrapper, hiPts, lowPts); |
| 1343 | } |
| 1344 | |
| 1345 | wl_surface_damage (mVideoSurfaceWrapper, 0, 0, mVideoRect.w, mVideoRect.h); |
| 1346 | wl_surface_commit (mVideoSurfaceWrapper); |
| 1347 | //insert this buffer to committed weston buffer manager |
fei.deng | ae8c90a | 2024-06-27 13:39:53 +0800 | [diff] [blame^] | 1348 | std::pair<int64_t, WaylandBuffer *> item(realDisplayTime, waylandBuf); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1349 | mCommittedBufferMap.insert(item); |
| 1350 | } else { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1351 | WARNING(mLogCategory,"wlbuffer is NULL"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1352 | /* clear both video and parent surfaces */ |
| 1353 | cleanSurface(); |
| 1354 | } |
| 1355 | |
| 1356 | wl_display_flush (mWlDisplay); |
fei.deng | 4029e68 | 2024-06-26 17:06:31 +0800 | [diff] [blame] | 1357 | //set keep last frame or not when after send first buffer to weston,1 keep last frame, 0 not |
| 1358 | if (mToSendKeepLastFrame) { |
| 1359 | setKeepLastFrame(mKeepLastFrame); |
| 1360 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1361 | |
| 1362 | return; |
| 1363 | waylandbuf_fail: |
| 1364 | //notify dropped |
| 1365 | mWaylandPlugin->handleFrameDropped(buf); |
| 1366 | //notify app release this buf |
| 1367 | mWaylandPlugin->handleBufferRelease(buf); |
| 1368 | //delete waylandbuf |
| 1369 | delete waylandBuf; |
| 1370 | waylandBuf = NULL; |
| 1371 | return; |
| 1372 | } |
| 1373 | |
| 1374 | void WaylandDisplay::handleBufferReleaseCallback(WaylandBuffer *buf) |
| 1375 | { |
| 1376 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1377 | Tls::Mutex::Autolock _l(mRenderMutex); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1378 | //remove buffer if this buffer is ready to release |
| 1379 | RenderBuffer *renderBuffer = buf->getRenderBuffer(); |
fei.deng | ae8c90a | 2024-06-27 13:39:53 +0800 | [diff] [blame^] | 1380 | auto item = mCommittedBufferMap.find(buf->getRenderRealTime()); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1381 | if (item != mCommittedBufferMap.end()) { |
fei.deng | dd910ef | 2024-06-07 10:25:30 +0800 | [diff] [blame] | 1382 | --mCommitCnt; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1383 | mCommittedBufferMap.erase(item); |
| 1384 | } else { |
fei.deng | dd910ef | 2024-06-07 10:25:30 +0800 | [diff] [blame] | 1385 | WARNING(mLogCategory,"Can't find WaylandBuffer pts:%lld us in buffer map",renderBuffer->pts/1000); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1386 | return; |
| 1387 | } |
| 1388 | } |
| 1389 | RenderBuffer *renderBuffer = buf->getRenderBuffer(); |
fei.deng | dd910ef | 2024-06-07 10:25:30 +0800 | [diff] [blame] | 1390 | TRACE(mLogCategory,"handle release renderBuffer :%p,priv:%p,PTS:%lld us,realtime:%lld us,commitCnt:%d",renderBuffer,renderBuffer->priv,renderBuffer->pts/1000,buf->getRenderRealTime(),mCommitCnt); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1391 | mWaylandPlugin->handleBufferRelease(renderBuffer); |
| 1392 | } |
| 1393 | |
| 1394 | void WaylandDisplay::handleFrameDisplayedCallback(WaylandBuffer *buf) |
| 1395 | { |
| 1396 | RenderBuffer *renderBuffer = buf->getRenderBuffer(); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1397 | TRACE(mLogCategory,"handle displayed renderBuffer :%p,PTS:%lld us,realtime:%lld us",renderBuffer,renderBuffer->pts/1000,buf->getRenderRealTime()); |
fei.deng | 3287c08 | 2024-04-23 09:29:22 +0000 | [diff] [blame] | 1398 | if (!mSignalFirstFramePts) { |
| 1399 | mSignalFirstFramePts = true; |
| 1400 | mWaylandPlugin->handleMsgNotify(MSG_FIRST_FRAME, (void*)&renderBuffer->pts); |
| 1401 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1402 | mWaylandPlugin->handleFrameDisplayed(renderBuffer); |
| 1403 | } |
| 1404 | |
| 1405 | void WaylandDisplay::handleFrameDropedCallback(WaylandBuffer *buf) |
| 1406 | { |
| 1407 | RenderBuffer *renderBuffer = buf->getRenderBuffer(); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1408 | TRACE(mLogCategory,"handle droped renderBuffer :%p,PTS:%lld us,realtime:%lld us",renderBuffer,renderBuffer->pts/1000,buf->getRenderRealTime()); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1409 | mWaylandPlugin->handleFrameDropped(renderBuffer); |
| 1410 | } |
| 1411 | |
| 1412 | |
| 1413 | void WaylandDisplay::readyToRun() |
| 1414 | { |
| 1415 | mFd = wl_display_get_fd (mWlDisplay); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1416 | if (mPoll) { |
| 1417 | mPoll->addFd(mFd); |
| 1418 | mPoll->setFdReadable(mFd, true); |
| 1419 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
fei.deng | dd910ef | 2024-06-07 10:25:30 +0800 | [diff] [blame] | 1422 | void WaylandDisplay::readyToExit() |
| 1423 | { |
| 1424 | if (mPoll && mFd >= 0) { |
| 1425 | mPoll->removeFd(mFd); |
| 1426 | } |
| 1427 | } |
| 1428 | |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1429 | bool WaylandDisplay::threadLoop() |
| 1430 | { |
| 1431 | int ret; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1432 | |
| 1433 | while (wl_display_prepare_read_queue (mWlDisplay, mWlQueue) != 0) { |
| 1434 | wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue); |
| 1435 | } |
| 1436 | |
| 1437 | wl_display_flush (mWlDisplay); |
| 1438 | |
| 1439 | /*poll timeout value must > 300 ms,otherwise zwp_linux_dmabuf will create failed, |
| 1440 | so do use -1 to wait for ever*/ |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1441 | ret = mPoll->wait(-1); //wait for ever |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1442 | if (ret < 0) { //poll error |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1443 | WARNING(mLogCategory,"poll error"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1444 | wl_display_cancel_read(mWlDisplay); |
| 1445 | return false; |
| 1446 | } else if (ret == 0) { //poll time out |
| 1447 | return true; //run loop |
| 1448 | } |
| 1449 | |
| 1450 | if (wl_display_read_events (mWlDisplay) == -1) { |
| 1451 | goto tag_error; |
| 1452 | } |
| 1453 | |
| 1454 | wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue); |
| 1455 | return true; |
| 1456 | tag_error: |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1457 | ERROR(mLogCategory,"Error communicating with the wayland server"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1458 | return false; |
| 1459 | } |
| 1460 | |
| 1461 | void WaylandDisplay::videoCenterRect(Rectangle src, Rectangle dst, Rectangle *result, bool scaling) |
| 1462 | { |
| 1463 | //if dst is a small window, we scale video to map window size,don't doing center |
| 1464 | if (mRenderRect.w != dst.w && mRenderRect.h != dst.h) { |
| 1465 | result->x = dst.x; |
| 1466 | result->y = dst.y; |
| 1467 | result->w = dst.w; |
| 1468 | result->h = dst.h; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1469 | TRACE(mLogCategory,"small window source is %dx%d dest is %dx%d, result is %dx%d with x,y %dx%d", |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1470 | src.w, src.h, dst.w, dst.h, result->w, result->h, result->x, result->y); |
| 1471 | return; |
| 1472 | } |
| 1473 | if (!scaling) { |
| 1474 | result->w = MIN (src.w, dst.w); |
| 1475 | result->h = MIN (src.h, dst.h); |
| 1476 | result->x = dst.x + (dst.w - result->w) / 2; |
| 1477 | result->y = dst.y + (dst.h - result->h) / 2; |
| 1478 | } else { |
| 1479 | double src_ratio, dst_ratio; |
| 1480 | |
| 1481 | src_ratio = (double) src.w / src.h; |
| 1482 | dst_ratio = (double) dst.w / dst.h; |
| 1483 | |
| 1484 | if (src_ratio > dst_ratio) { |
| 1485 | result->w = dst.w; |
| 1486 | result->h = dst.w / src_ratio; |
| 1487 | result->x = dst.x; |
| 1488 | result->y = dst.y + (dst.h - result->h) / 2; |
| 1489 | } else if (src_ratio < dst_ratio) { |
| 1490 | result->w = dst.h * src_ratio; |
| 1491 | result->h = dst.h; |
| 1492 | result->x = dst.x + (dst.w - result->w) / 2; |
| 1493 | result->y = dst.y; |
| 1494 | } else { |
| 1495 | result->x = dst.x; |
| 1496 | result->y = dst.y; |
| 1497 | result->w = dst.w; |
| 1498 | result->h = dst.h; |
| 1499 | } |
| 1500 | } |
| 1501 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1502 | TRACE(mLogCategory,"source is %dx%d dest is %dx%d, result is %dx%d with x,y %dx%d", |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1503 | src.w, src.h, dst.w, dst.h, result->w, result->h, result->x, result->y); |
| 1504 | } |
| 1505 | |
| 1506 | void WaylandDisplay::updateBorders() |
| 1507 | { |
| 1508 | int width,height; |
| 1509 | |
| 1510 | if (mNoBorderUpdate) |
| 1511 | return; |
| 1512 | |
| 1513 | if (mViewporter) { |
| 1514 | width = height = 1; |
| 1515 | mNoBorderUpdate = true; |
| 1516 | } else { |
| 1517 | width = mRenderRect.w; |
| 1518 | height = mRenderRect.h; |
| 1519 | } |
| 1520 | |
| 1521 | RenderVideoFormat format = VIDEO_FORMAT_BGRA; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1522 | mAreaShmBuffer = new WaylandShmBuffer(this, mLogCategory); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1523 | struct wl_buffer *wlbuf = mAreaShmBuffer->constructWlBuffer(width, height, format); |
| 1524 | if (wlbuf == NULL) { |
| 1525 | delete mAreaShmBuffer; |
| 1526 | mAreaShmBuffer = NULL; |
| 1527 | } |
| 1528 | |
| 1529 | wl_surface_attach(mAreaSurfaceWrapper, wlbuf, 0, 0); |
| 1530 | } |
| 1531 | |
| 1532 | std::size_t WaylandDisplay::calculateDmaBufferHash(RenderDmaBuffer &dmabuf) |
| 1533 | { |
| 1534 | std::string hashString(""); |
| 1535 | for (int i = 0; i < dmabuf.planeCnt; i++) { |
| 1536 | char hashtmp[1024]; |
| 1537 | snprintf (hashtmp, 1024, "%d%d%d%d%d%d", dmabuf.width,dmabuf.height,dmabuf.planeCnt, |
| 1538 | dmabuf.stride[i],dmabuf.offset[i],dmabuf.fd[i]); |
| 1539 | std::string tmp(hashtmp); |
| 1540 | hashString += tmp; |
| 1541 | } |
| 1542 | |
| 1543 | std::size_t hashval = std::hash<std::string>()(hashString); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1544 | //TRACE(mLogCategory,"hashstr:%s,val:%zu",hashString.c_str(),hashval); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1545 | return hashval; |
| 1546 | } |
| 1547 | |
| 1548 | void WaylandDisplay::addWaylandBuffer(RenderBuffer * buf, WaylandBuffer *waylandbuf) |
| 1549 | { |
| 1550 | if (buf->flag & BUFFER_FLAG_DMA_BUFFER) { |
| 1551 | std::size_t hashval = calculateDmaBufferHash(buf->dma); |
| 1552 | std::pair<std::size_t, WaylandBuffer *> item(hashval, waylandbuf); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1553 | //TRACE(mLogCategory,"fd:%d,w:%d,h:%d,%p,hash:%zu",buf->dma.fd[0],buf->dma.width,buf->dma.height,waylandbuf,hashval); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1554 | mWaylandBuffersMap.insert(item); |
| 1555 | } |
| 1556 | |
| 1557 | //clean invalid wayland buffer,if video resolution changed |
| 1558 | for (auto item = mWaylandBuffersMap.begin(); item != mWaylandBuffersMap.end(); ) { |
| 1559 | WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second; |
| 1560 | if ((waylandbuf->getFrameWidth() != buf->dma.width || |
| 1561 | waylandbuf->getFrameHeight() != buf->dma.height) && |
| 1562 | waylandbuf->isFree()) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1563 | TRACE(mLogCategory,"delete wayland buffer,width:%d(%d),height:%d(%d)", |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1564 | waylandbuf->getFrameWidth(),buf->dma.width, |
| 1565 | waylandbuf->getFrameHeight(),buf->dma.height); |
| 1566 | mWaylandBuffersMap.erase(item++); |
| 1567 | delete waylandbuf; |
| 1568 | } else { |
| 1569 | item++; |
| 1570 | } |
| 1571 | } |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1572 | TRACE(mLogCategory,"mWaylandBuffersMap size:%d",mWaylandBuffersMap.size()); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | WaylandBuffer* WaylandDisplay::findWaylandBuffer(RenderBuffer * buf) |
| 1576 | { |
| 1577 | std::size_t hashval = calculateDmaBufferHash(buf->dma); |
| 1578 | auto item = mWaylandBuffersMap.find(hashval); |
| 1579 | if (item == mWaylandBuffersMap.end()) { |
| 1580 | return NULL; |
| 1581 | } |
| 1582 | |
| 1583 | return (WaylandBuffer*) item->second; |
| 1584 | } |
| 1585 | |
| 1586 | void WaylandDisplay::cleanAllWaylandBuffer() |
| 1587 | { |
| 1588 | //free all obtain buff |
| 1589 | for (auto item = mWaylandBuffersMap.begin(); item != mWaylandBuffersMap.end(); ) { |
| 1590 | WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second; |
| 1591 | mWaylandBuffersMap.erase(item++); |
| 1592 | delete waylandbuf; |
| 1593 | } |
| 1594 | } |
| 1595 | |
| 1596 | void WaylandDisplay::flushBuffers() |
| 1597 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1598 | INFO(mLogCategory,"flushBuffers"); |
| 1599 | Tls::Mutex::Autolock _l(mRenderMutex); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1600 | for (auto item = mCommittedBufferMap.begin(); item != mCommittedBufferMap.end(); item++) { |
| 1601 | WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second; |
| 1602 | waylandbuf->forceRedrawing(); |
| 1603 | handleFrameDisplayedCallback(waylandbuf); |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | void WaylandDisplay::cleanSurface() |
| 1608 | { |
| 1609 | /* clear both video and parent surfaces */ |
| 1610 | wl_surface_attach (mVideoSurfaceWrapper, NULL, 0, 0); |
| 1611 | wl_surface_commit (mVideoSurfaceWrapper); |
| 1612 | wl_surface_attach (mAreaSurfaceWrapper, NULL, 0, 0); |
| 1613 | wl_surface_commit (mAreaSurfaceWrapper); |
fei.deng | 640c3c9 | 2024-04-12 08:31:19 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | void WaylandDisplay::setKeepLastFrame(int keep) |
| 1617 | { |
| 1618 | mKeepLastFrame = keep; |
| 1619 | if (mVideoSurfaceWrapper && mAmlConfigAPIList.enableKeepLastFrame) { |
| 1620 | INFO(mLogCategory,"keep last frame:%d",keep); |
| 1621 | wl_surface_keep_last_frame(mVideoSurfaceWrapper, keep); |
fei.deng | 4029e68 | 2024-06-26 17:06:31 +0800 | [diff] [blame] | 1622 | mToSendKeepLastFrame = false; |
| 1623 | return; |
fei.deng | 640c3c9 | 2024-04-12 08:31:19 +0000 | [diff] [blame] | 1624 | } |
fei.deng | 4029e68 | 2024-06-26 17:06:31 +0800 | [diff] [blame] | 1625 | mToSendKeepLastFrame = true; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1626 | } |