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