blob: 83924d40e03eae96406bc6580d555a3999332a89 [file] [log] [blame]
fei.dengf7a0cd32023-08-29 09:36:37 +00001/*
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.dengb9a1a572023-09-13 01:33:57 +000018#include "ErrorCode.h"
fei.dengf7a0cd32023-08-29 09:36:37 +000019#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.deng26950832023-11-09 03:00:23 +000032#define INVALID_OUTPUT_INDEX (-1)
fei.dengf7a0cd32023-08-29 09:36:37 +000033
34#define TAG "rlib:wayland_display"
35
36void 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.dengb9a1a572023-09-13 01:33:57 +000040 Tls::Mutex::Autolock _l(self->mMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +000041 if (wl_dmabuf_format_to_video_format (format) != VIDEO_FORMAT_UNKNOWN) {
fei.dengb9a1a572023-09-13 01:33:57 +000042 TRACE(self->mLogCategory,"regist dmabuffer format:%d (%s) hi:%x,lo:%x",format,print_dmabuf_format_name(format),modifier_hi,modifier_lo);
fei.dengf7a0cd32023-08-29 09:36:37 +000043 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
54void
55WaylandDisplay::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
69static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
70 WaylandDisplay::dmaBufferFormat,
71 WaylandDisplay::dmabuf_modifiers
72};
73
74static void
75handle_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
81static const struct xdg_wm_base_listener xdg_wm_base_listener = {
82 handle_xdg_wm_base_ping
83};
84
85void 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
91static const struct wl_shm_listener shm_listener = {
92 WaylandDisplay::shmFormat
93};
94
95void 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.dengb9a1a572023-09-13 01:33:57 +0000110 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.dengf7a0cd32023-08-29 09:36:37 +0000113 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
121void 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.dengb9a1a572023-09-13 01:33:57 +0000131 Tls::Mutex::Autolock _l(self->mMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +0000132 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.deng26950832023-11-09 03:00:23 +0000139
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.dengf7a0cd32023-08-29 09:36:37 +0000144 }
145 }
146}
147
148void WaylandDisplay::outputHandleDone( void *data,
149 struct wl_output *output )
150{
fei.dengf1f5fc32023-12-06 06:22:20 +0000151 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
152 DEBUG(self->mLogCategory,"wl_output: %p",output);
fei.dengf7a0cd32023-08-29 09:36:37 +0000153 UNUSED_PARAM(data);
154 UNUSED_PARAM(output);
155}
156
157void WaylandDisplay::outputHandleScale( void *data,
158 struct wl_output *output,
159 int32_t scale )
160{
fei.dengf1f5fc32023-12-06 06:22:20 +0000161 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
162 DEBUG(self->mLogCategory,"wl_output: %p scale %d",output, scale);
fei.dengf7a0cd32023-08-29 09:36:37 +0000163 UNUSED_PARAM(data);
164 UNUSED_PARAM(output);
165 UNUSED_PARAM(scale);
166}
167
fei.dengf1f5fc32023-12-06 06:22:20 +0000168void 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.deng640c3c92024-04-12 08:31:19 +0000182//aml weston always add crtcIndex in callbacks.
fei.dengf7a0cd32023-08-29 09:36:37 +0000183static const struct wl_output_listener outputListener = {
184 WaylandDisplay::outputHandleGeometry,
185 WaylandDisplay::outputHandleMode,
186 WaylandDisplay::outputHandleDone,
fei.dengf1f5fc32023-12-06 06:22:20 +0000187 WaylandDisplay::outputHandleScale,
188 WaylandDisplay::outputHandleCrtcIndex,
fei.dengf7a0cd32023-08-29 09:36:37 +0000189};
190
191void 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
202void 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
211void 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.dengb9a1a572023-09-13 01:33:57 +0000217 DEBUG(self->mLogCategory,"pointer motion fixed[%d, %d] to-int: x[%d] y[%d]\n", sx, sy, x, y);
fei.dengf7a0cd32023-08-29 09:36:37 +0000218}
219
220void 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
231void 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
241static const struct wl_pointer_listener pointer_listener = {
242 WaylandDisplay::pointerHandleEnter,
243 WaylandDisplay::pointerHandleLeave,
244 WaylandDisplay::pointerHandleMotion,
245 WaylandDisplay::pointerHandleButton,
246 WaylandDisplay::pointerHandleAxis,
247};
248
249void 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
256void 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
266void 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
277void WaylandDisplay::touchHandleFrame(void *data, struct wl_touch *wl_touch)
278{
279 UNUSED_PARAM(data);
280 UNUSED_PARAM(wl_touch);
281}
282
283void WaylandDisplay::touchHandleCancel(void *data, struct wl_touch *wl_touch)
284{
285 UNUSED_PARAM(data);
286 UNUSED_PARAM(wl_touch);
287}
288
289static const struct wl_touch_listener touch_listener = {
290 WaylandDisplay::touchHandleDown,
291 WaylandDisplay::touchHandleUp,
292 WaylandDisplay::touchHandleMotion,
293 WaylandDisplay::touchHandleFrame,
294 WaylandDisplay::touchHandleCancel,
295};
296
297void 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
307void 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
317void 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
326void 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
337void 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
351static const struct wl_keyboard_listener keyboard_listener = {
352 WaylandDisplay::keyboardHandleKeymap,
353 WaylandDisplay::keyboardHandleEnter,
354 WaylandDisplay::keyboardHandleLeave,
355 WaylandDisplay::keyboardHandleKey,
356 WaylandDisplay::keyboardHandleModifiers,
357};
358
359void 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
389static const struct wl_seat_listener seat_listener = {
390 WaylandDisplay::seatHandleCapabilities,
391};
392
393
394void WaylandDisplay::handleXdgToplevelClose (void *data, struct xdg_toplevel *xdg_toplevel)
395{
396 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
397
fei.dengb9a1a572023-09-13 01:33:57 +0000398 INFO(self->mLogCategory,"XDG toplevel got a close event.");
fei.dengf7a0cd32023-08-29 09:36:37 +0000399}
400
401void 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.dengb9a1a572023-09-13 01:33:57 +0000407 INFO(self->mLogCategory, "XDG toplevel got a configure event, width:height [ %d, %d ].", width, height);
fei.dengf7a0cd32023-08-29 09:36:37 +0000408 /*
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.deng26950832023-11-09 03:00:23 +0000423 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.dengf7a0cd32023-08-29 09:36:37 +0000429 } else{
430 self->setRenderRectangle(self->mRenderRect.x, self->mRenderRect.y, width, height);
431 }
432}
433
434static const struct xdg_toplevel_listener xdg_toplevel_listener = {
435 WaylandDisplay::handleXdgToplevelConfigure,
436 WaylandDisplay::handleXdgToplevelClose,
437};
438
439void 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.dengb9a1a572023-09-13 01:33:57 +0000445 TRACE(self->mLogCategory,"handleXdgSurfaceConfigure");
446 Tls::Mutex::Autolock _l(self->mConfigureMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +0000447 self->mXdgSurfaceConfigured = true;
fei.deng26950832023-11-09 03:00:23 +0000448 self->updateDisplayOutput();
fei.dengf7a0cd32023-08-29 09:36:37 +0000449}
450
451static const struct xdg_surface_listener xdg_surface_listener = {
452 WaylandDisplay::handleXdgSurfaceConfigure,
453};
454
fei.dengd0da4e22024-07-04 11:29:13 +0800455void WaylandDisplay::handleSurfaceDestroy(void *data, struct wl_callback *callback, uint32_t time)
456{
457 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
458 INFO(self->mLogCategory,"handle video surface destroy");
459 Tls::Mutex::Autolock _l(self->mMutex);
460 self->mCondition.signal();
461 wl_callback_destroy (callback);
462}
463
464static const struct wl_callback_listener surface_destroy_listener = {
465 WaylandDisplay::handleSurfaceDestroy,
466};
467
fei.deng640c3c92024-04-12 08:31:19 +0000468void WaylandDisplay::amlConfigure(void *data, struct aml_config *config, const char *list) {
469 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
470 TRACE(self->mLogCategory,"aml_config:%s",list);
471 if (list && strlen(list) > 0) {
472 if (strstr(list, "set_video_plane")) {
473 TRACE(self->mLogCategory,"weston enable set_video_plane");
474 self->mAmlConfigAPIList.enableSetVideoPlane = true;
475 }
476 if (strstr(list, "set_pts")) {
477 TRACE(self->mLogCategory,"weston enable set_pts");
478 self->mAmlConfigAPIList.enableSetPts = true;
479 }
480 if (strstr(list, "drop")) {
481 TRACE(self->mLogCategory,"weston enable drop");
482 self->mAmlConfigAPIList.enableDropFrame = true;
483 }
484 if (strstr(list, "keep_last_frame")) {
485 TRACE(self->mLogCategory,"weston enable keep_last_frame");
486 self->mAmlConfigAPIList.enableKeepLastFrame = true;
487 }
fei.dengd0da4e22024-07-04 11:29:13 +0800488 if (strstr(list, "surface_destroy_cb")) {
489 TRACE(self->mLogCategory,"weston enable surface_destroy_cb");
490 self->mAmlConfigAPIList.enableSurfaceDestroyCallback = true;
491 }
fei.deng640c3c92024-04-12 08:31:19 +0000492 }
493}
494
495static const struct aml_config_listener aml_config_listener = {
496 WaylandDisplay::amlConfigure,
497};
498
fei.dengf7a0cd32023-08-29 09:36:37 +0000499void
500WaylandDisplay::registryHandleGlobal (void *data, struct wl_registry *registry,
fei.dengaf9b07d2023-10-10 07:38:40 +0000501 uint32_t name, const char *interface, uint32_t version)
fei.dengf7a0cd32023-08-29 09:36:37 +0000502{
503 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
fei.dengaf9b07d2023-10-10 07:38:40 +0000504 TRACE(self->mLogCategory,"registryHandleGlobal,name:%u,interface:%s,version:%d",name,interface,version);
fei.dengf7a0cd32023-08-29 09:36:37 +0000505
506 if (strcmp (interface, "wl_compositor") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000507 self->mCompositor = (struct wl_compositor *)wl_registry_bind (registry, name, &wl_compositor_interface, 1/*MIN (version, 3)*/);
fei.dengf7a0cd32023-08-29 09:36:37 +0000508 } else if (strcmp (interface, "wl_subcompositor") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000509 self->mSubCompositor = (struct wl_subcompositor *)wl_registry_bind (registry, name, &wl_subcompositor_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000510 } else if (strcmp (interface, "xdg_wm_base") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000511 self->mXdgWmBase = (struct xdg_wm_base *)wl_registry_bind (registry, name, &xdg_wm_base_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000512 xdg_wm_base_add_listener (self->mXdgWmBase, &xdg_wm_base_listener, (void *)self);
513 } else if (strcmp (interface, "wl_shm") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000514 self->mShm = (struct wl_shm *)wl_registry_bind (registry, name, &wl_shm_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000515 wl_shm_add_listener (self->mShm, &shm_listener, self);
516 } else if (strcmp (interface, "zwp_fullscreen_shell_v1") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000517 //self->mFullscreenShell = (struct zwp_fullscreen_shell_v1 *)wl_registry_bind (registry, name,
fei.dengf7a0cd32023-08-29 09:36:37 +0000518 // &zwp_fullscreen_shell_v1_interface, 1);
519 } else if (strcmp (interface, "wp_viewporter") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000520 self->mViewporter = (struct wp_viewporter *)wl_registry_bind (registry, name, &wp_viewporter_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000521 } else if (strcmp (interface, "zwp_linux_dmabuf_v1") == 0) {
522 if (version < 3)
523 return;
fei.dengaf9b07d2023-10-10 07:38:40 +0000524 self->mDmabuf = (struct zwp_linux_dmabuf_v1 *)wl_registry_bind (registry, name, &zwp_linux_dmabuf_v1_interface, 3);
fei.dengf7a0cd32023-08-29 09:36:37 +0000525 zwp_linux_dmabuf_v1_add_listener (self->mDmabuf, &dmabuf_listener, (void *)self);
526 } else if (strcmp (interface, "wl_output") == 0) {
fei.deng26950832023-11-09 03:00:23 +0000527 int i = 0;
528 uint32_t oriName = self->mCurrentDisplayOutput->name;
fei.dengaf9b07d2023-10-10 07:38:40 +0000529 for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
530 if (self->mOutput[i].wlOutput == NULL) {
531 self->mOutput[i].name = name;
532 self->mOutput[i].wlOutput = (struct wl_output*)wl_registry_bind(registry, name, &wl_output_interface, version);
fei.deng26950832023-11-09 03:00:23 +0000533 TRACE(self->mLogCategory,"name:%u, wl_output:%p, select:%d",self->mOutput[i].name,self->mOutput[i].wlOutput,self->mSelectOutputIndex);
fei.dengaf9b07d2023-10-10 07:38:40 +0000534 wl_output_add_listener(self->mOutput[i].wlOutput, &outputListener, (void *)self);
535 if (i == 0) { //primary wl_output
536 self->mOutput[i].isPrimary = true;
537 }
fei.deng26950832023-11-09 03:00:23 +0000538 break;
fei.dengaf9b07d2023-10-10 07:38:40 +0000539 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000540 }
fei.deng26950832023-11-09 03:00:23 +0000541 if (i == DEFAULT_DISPLAY_OUTPUT_NUM) {
542 WARNING(self->mLogCategory,"Not enough free output");
543 }
544 //select current wl_output
545 if (self->mSelectOutputIndex != INVALID_OUTPUT_INDEX && !self->isRunning()) {
546 TRACE(self->mLogCategory,"select %d output",self->mSelectOutputIndex);
547 self->mCurrentDisplayOutput = &self->mOutput[self->mSelectOutputIndex];
548 }
549 //if user select a wrong output index, we using a suiteble wl_output
550 if (self->mCurrentDisplayOutput->wlOutput == NULL) {
551 WARNING(self->mLogCategory,"wl_output is null,we should find a suiteble output");
552 for (i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
553 if (self->mOutput[i].wlOutput) {
554 self->mCurrentDisplayOutput = &self->mOutput[i];
555 break;
556 }
557 }
558 }
559 //if current wl_output update, we should update render rectangle
560 if (self->mCurrentDisplayOutput->name != oriName) {
561 self->mUpdateRenderRectangle = true;
562 }
563 //if wl_output plugin,active sending frame
564 self->setRedrawingPending(false);
fei.dengf7a0cd32023-08-29 09:36:37 +0000565 } else if (strcmp(interface, "wl_seat") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000566 //self->mSeat = (struct wl_seat *)wl_registry_bind(registry, name, &wl_seat_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000567 //wl_seat_add_listener(self->mSeat, &seat_listener, (void *)self);
fei.deng31f93f12024-02-27 07:52:10 +0000568 } else if (strcmp(interface, "weston_direct_display_v1") == 0) {
569 self->mDirect_display = (struct weston_direct_display_v1 *)wl_registry_bind(registry,name, &weston_direct_display_v1_interface, 1);
fei.deng640c3c92024-04-12 08:31:19 +0000570 } else if (strcmp(interface, "aml_config") == 0) {
571 self->mAmlConfig = (struct aml_config*)wl_registry_bind(registry, name, &aml_config_interface, 1);
572 aml_config_add_listener(self->mAmlConfig, &aml_config_listener, (void *)self);
fei.dengf7a0cd32023-08-29 09:36:37 +0000573 }
574}
575
576void
577WaylandDisplay::registryHandleGlobalRemove (void *data, struct wl_registry *registry, uint32_t name)
578{
fei.deng26950832023-11-09 03:00:23 +0000579 int i;
fei.dengf7a0cd32023-08-29 09:36:37 +0000580 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
fei.dengaf9b07d2023-10-10 07:38:40 +0000581 /* check wl_output changed */
582 DEBUG(self->mLogCategory,"wayland display remove registry handle global,name:%u",name);
fei.deng26950832023-11-09 03:00:23 +0000583 //if user selected wl_output removed, reset selected output index
584 if (self->mSelectOutputIndex != INVALID_OUTPUT_INDEX &&
585 self->mOutput[self->mSelectOutputIndex].wlOutput &&
586 self->mOutput[self->mSelectOutputIndex].name == name) {
587 self->mSelectOutputIndex = INVALID_OUTPUT_INDEX;
588 }
589 for (i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000590 if (self->mOutput[i].name == name) {
fei.deng26950832023-11-09 03:00:23 +0000591 DEBUG(self->mLogCategory,"remove wl_output name:%u,wl_output:%p",name,self->mOutput[i].wlOutput);
fei.dengaf9b07d2023-10-10 07:38:40 +0000592 self->mOutput[i].name = 0;
593 self->mOutput[i].wlOutput = NULL;
fei.deng26950832023-11-09 03:00:23 +0000594 }
595 }
596 //if current output removed, select a suiteble output
597 if (self->mCurrentDisplayOutput->wlOutput == NULL) {
598 for (i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
599 if (self->mOutput[i].wlOutput) {
600 self->mCurrentDisplayOutput = &self->mOutput[i];
601 self->mUpdateRenderRectangle = true;
602 }
603 }
604 //set new output rectangle
605 if (self->mUpdateRenderRectangle) {
606 self->mUpdateRenderRectangle = false;
607 self->setRenderRectangle(self->mCurrentDisplayOutput->offsetX,
608 self->mCurrentDisplayOutput->offsetY,
609 self->mCurrentDisplayOutput->width,
610 self->mCurrentDisplayOutput->height);
fei.dengaf9b07d2023-10-10 07:38:40 +0000611 }
612 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000613}
614
615static const struct wl_registry_listener registry_listener = {
616 WaylandDisplay::registryHandleGlobal,
617 WaylandDisplay::registryHandleGlobalRemove
618};
619
fei.dengb9a1a572023-09-13 01:33:57 +0000620WaylandDisplay::WaylandDisplay(WaylandPlugin *plugin, int logCategory)
621 :mBufferMutex("bufferMutex"),
622 mWaylandPlugin(plugin),
623 mLogCategory(logCategory)
fei.dengf7a0cd32023-08-29 09:36:37 +0000624{
fei.dengb9a1a572023-09-13 01:33:57 +0000625 TRACE(mLogCategory,"construct WaylandDisplay");
fei.dengf7a0cd32023-08-29 09:36:37 +0000626 mWlDisplay = NULL;
627 mWlDisplayWrapper = NULL;
628 mWlQueue = NULL;
629 mRegistry = NULL;
630 mCompositor = NULL;
631 mXdgWmBase = NULL;
632 mViewporter = NULL;
633 mDmabuf = NULL;
634 mShm = NULL;
635 mSeat = NULL;
636 mPointer = NULL;
637 mTouch = NULL;
638 mKeyboard = NULL;
fei.deng31f93f12024-02-27 07:52:10 +0000639 mDirect_display = NULL;
fei.deng26950832023-11-09 03:00:23 +0000640 mSelectOutputIndex = INVALID_OUTPUT_INDEX;
fei.dengb9a1a572023-09-13 01:33:57 +0000641 mPoll = new Tls::Poll(true);
fei.dengf7a0cd32023-08-29 09:36:37 +0000642 //window
643 mVideoWidth = 0;
644 mVideoHeight = 0;
645 mVideoSurface = NULL;
646 mXdgSurface = NULL;
647 mXdgToplevel = NULL;
648 mAreaViewport = NULL;
649 mVideoViewport = NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000650 mAreaShmBuffer = NULL;
651 mCommitCnt = 0;
fei.dengf7a0cd32023-08-29 09:36:37 +0000652 mAreaSurface = NULL;
653 mAreaSurfaceWrapper = NULL;
654 mVideoSurfaceWrapper = NULL;
655 mVideoSubSurface = NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000656 mPip = 0;
fei.deng640c3c92024-04-12 08:31:19 +0000657 mKeepLastFrame = 0; //default is off keep last frame
fei.deng3287c082024-04-23 09:29:22 +0000658 mSignalFirstFramePts = false;
fei.deng4029e682024-06-26 17:06:31 +0800659 mToSendKeepLastFrame = false;
fei.dengf7a0cd32023-08-29 09:36:37 +0000660}
661
662WaylandDisplay::~WaylandDisplay()
663{
fei.dengb9a1a572023-09-13 01:33:57 +0000664 TRACE(mLogCategory,"desconstruct WaylandDisplay");
665 if (mPoll) {
666 delete mPoll;
667 mPoll = NULL;
668 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000669}
670
671char *WaylandDisplay::require_xdg_runtime_dir()
672{
673 char *val = getenv("XDG_RUNTIME_DIR");
fei.dengb9a1a572023-09-13 01:33:57 +0000674 INFO(mLogCategory,"XDG_RUNTIME_DIR=%s",val);
fei.dengdd910ef2024-06-07 10:25:30 +0800675 //if not set XDG_RUNTIME_DIR,set default value
676 // if (!val) {
677 // val = const_cast<char *>("/run/user/0");
678 // setenv("XDG_RUNTIME_DIR",val,0);
679 // WARNING(mLogCategory,"XDG_RUNTIME_DIR is not set,set default %s",val);
680 // }
fei.dengf7a0cd32023-08-29 09:36:37 +0000681
682 return val;
683}
684
685int WaylandDisplay::openDisplay()
686{
fei.dengdd910ef2024-06-07 10:25:30 +0800687 mNoBorderUpdate = false;
688 mReCommitAreaSurface = false;
689 mXdgSurfaceConfigured = false;
690 mUpdateRenderRectangle = false;
691 memset(&mRenderRect, 0, sizeof(struct Rectangle));
692 memset(&mVideoRect, 0, sizeof(struct Rectangle));
693 memset(&mWindowRect, 0, sizeof(struct Rectangle));
694 mFullScreen = true; //default is full screen
695 mAmlConfig = NULL;
696 //weston config private api
697 mAmlConfigAPIList.enableDropFrame = false;
698 mAmlConfigAPIList.enableKeepLastFrame = false;
699 mAmlConfigAPIList.enableSetPts = false;
700 mAmlConfigAPIList.enableSetVideoPlane = false;
fei.dengd0da4e22024-07-04 11:29:13 +0800701 mAmlConfigAPIList.enableSurfaceDestroyCallback = false;
fei.dengdd910ef2024-06-07 10:25:30 +0800702 mCurrentDisplayOutput = &mOutput[0];
703 for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
704 mOutput[i].wlOutput = NULL;
705 mOutput[i].offsetX = 0;
706 mOutput[i].offsetY = 0;
707 mOutput[i].width = 0;
708 mOutput[i].height = 0;
709 mOutput[i].refreshRate = 0;
710 mOutput[i].isPrimary = false;
711 mOutput[i].name = 0;
712 mOutput[i].crtcIndex = 0;
713 }
fei.deng4029e682024-06-26 17:06:31 +0800714 /*if mKeepLastFrame had set but mToSendKeepLastFrame is false,maybe
715 playback is doing FF/FW action,so we keep set it on new connection*/
716 if (!mToSendKeepLastFrame && mKeepLastFrame) {
717 mToSendKeepLastFrame = true;
718 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000719 char *name = require_xdg_runtime_dir();
720 //DEBUG(mLogCategory,"name:%s",name);
fei.dengb9a1a572023-09-13 01:33:57 +0000721 DEBUG(mLogCategory,"openDisplay in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000722 mWlDisplay = wl_display_connect(NULL);
723 if (!mWlDisplay) {
fei.dengb9a1a572023-09-13 01:33:57 +0000724 ERROR(mLogCategory,"Failed to connect to the wayland display, XDG_RUNTIME_DIR='%s'",
fei.dengf7a0cd32023-08-29 09:36:37 +0000725 name ? name : "NULL");
fei.dengb9a1a572023-09-13 01:33:57 +0000726 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000727 }
728
729 mWlDisplayWrapper = (struct wl_display *)wl_proxy_create_wrapper ((void *)mWlDisplay);
730 mWlQueue = wl_display_create_queue (mWlDisplay);
731 wl_proxy_set_queue ((struct wl_proxy *)mWlDisplayWrapper, mWlQueue);
732
733 mRegistry = wl_display_get_registry (mWlDisplayWrapper);
734 wl_registry_add_listener (mRegistry, &registry_listener, (void *)this);
735
736 /* we need exactly 2 roundtrips to discover global objects and their state */
737 for (int i = 0; i < 2; i++) {
738 if (wl_display_roundtrip_queue (mWlDisplay, mWlQueue) < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000739 ERROR(mLogCategory,"Error communicating with the wayland display");
740 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000741 }
742 }
743
744 if (!mCompositor) {
fei.dengb9a1a572023-09-13 01:33:57 +0000745 ERROR(mLogCategory,"Could not bind to wl_compositor. Either it is not implemented in " \
fei.dengf7a0cd32023-08-29 09:36:37 +0000746 "the compositor, or the implemented version doesn't match");
fei.dengb9a1a572023-09-13 01:33:57 +0000747 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000748 }
749
750 if (!mDmabuf) {
fei.dengb9a1a572023-09-13 01:33:57 +0000751 ERROR(mLogCategory,"Could not bind to zwp_linux_dmabuf_v1");
752 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000753 }
754
755 if (!mXdgWmBase) {
756 /* If wl_surface and wl_display are passed via GstContext
757 * wl_shell, xdg_shell and zwp_fullscreen_shell are not used.
758 * In this case is correct to continue.
759 */
fei.dengb9a1a572023-09-13 01:33:57 +0000760 ERROR(mLogCategory,"Could not bind to either wl_shell, xdg_wm_base or "
fei.dengf7a0cd32023-08-29 09:36:37 +0000761 "zwp_fullscreen_shell, video display may not work properly.");
fei.dengb9a1a572023-09-13 01:33:57 +0000762 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000763 }
764
765 //create window surface
766 createCommonWindowSurface();
767 createXdgShellWindowSurface();
768
769 //config weston video plane
fei.deng640c3c92024-04-12 08:31:19 +0000770 if (mAmlConfigAPIList.enableSetVideoPlane) {
fei.dengb9a1a572023-09-13 01:33:57 +0000771 INFO(mLogCategory,"set weston video plane:%d",mPip);
fei.dengf7a0cd32023-08-29 09:36:37 +0000772 wl_surface_set_video_plane(mVideoSurfaceWrapper, mPip);
773 }
774
775 //run wl display queue dispatch
fei.dengb9a1a572023-09-13 01:33:57 +0000776 DEBUG(mLogCategory,"To run wl display dispatch queue");
fei.dengdd910ef2024-06-07 10:25:30 +0800777 if (mPoll) {
778 mPoll->setFlushing(false);
779 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000780 run("display queue");
781 mRedrawingPending = false;
782
fei.dengb9a1a572023-09-13 01:33:57 +0000783 DEBUG(mLogCategory,"openDisplay out");
784 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000785}
786
787void WaylandDisplay::closeDisplay()
788{
fei.dengb9a1a572023-09-13 01:33:57 +0000789 DEBUG(mLogCategory,"closeDisplay in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000790
fei.dengd0da4e22024-07-04 11:29:13 +0800791 //first destroy window surface
792 destroyWindowSurfaces();
fei.dengf7a0cd32023-08-29 09:36:37 +0000793
fei.deng4029e682024-06-26 17:06:31 +0800794 //flush pending event to weston
fei.dengdd910ef2024-06-07 10:25:30 +0800795 if (mWlDisplay) {
796 wl_display_flush (mWlDisplay);
797 }
798
fei.dengd0da4e22024-07-04 11:29:13 +0800799 //wait video surface destroyed or 50ms timeout
800 if (mAmlConfigAPIList.enableSurfaceDestroyCallback) {
801 INFO(mLogCategory,"waiting surface_destroy_cb from weston");
802 Tls::Mutex::Autolock _l(mMutex);
803 if (ERROR_TIMED_OUT == mCondition.waitRelative(mMutex, 50/*microsecond*/)) {
804 WARNING(mLogCategory,"waited surface_destroy_cb timeout");
805 }
806 }
807
808 //we should receive all event from weston before stopped dispatch queue
809 if (isRunning()) {
810 TRACE(mLogCategory,"try stop dispatch thread");
811 if (mPoll) {
812 mPoll->setFlushing(true);
813 }
814 requestExitAndWait();
815 }
816 //after destroyed surface,then destroy buffers,otherwise maybe crash
817 if (mAreaShmBuffer) {
818 delete mAreaShmBuffer;
819 mAreaShmBuffer = NULL;
820 }
821
822 //clean all wayland buffers
823 cleanAllWaylandBuffer();
fei.dengf7a0cd32023-08-29 09:36:37 +0000824
825 if (mViewporter) {
826 wp_viewporter_destroy (mViewporter);
827 mViewporter = NULL;
828 }
829
830 if (mDmabuf) {
831 zwp_linux_dmabuf_v1_destroy (mDmabuf);
832 mDmabuf = NULL;
833 }
834
835 if (mXdgWmBase) {
836 xdg_wm_base_destroy (mXdgWmBase);
837 mXdgWmBase = NULL;
838 }
839
840 if (mCompositor) {
841 wl_compositor_destroy (mCompositor);
842 mCompositor = NULL;
843 }
844
845 if (mSubCompositor) {
846 wl_subcompositor_destroy (mSubCompositor);
847 mSubCompositor = NULL;
848 }
849
850 if (mRegistry) {
851 wl_registry_destroy (mRegistry);
852 mRegistry= NULL;
853 }
854
855 if (mWlDisplayWrapper) {
856 wl_proxy_wrapper_destroy (mWlDisplayWrapper);
857 mWlDisplayWrapper = NULL;
858 }
859
fei.deng640c3c92024-04-12 08:31:19 +0000860 if (mAmlConfig) {
861 aml_config_destroy(mAmlConfig);
862 mAmlConfig = NULL;
863 }
864
fei.dengf7a0cd32023-08-29 09:36:37 +0000865 if (mWlQueue) {
866 wl_event_queue_destroy (mWlQueue);
867 mWlQueue = NULL;
868 }
869
870 if (mWlDisplay) {
871 wl_display_flush (mWlDisplay);
872 wl_display_disconnect (mWlDisplay);
873 mWlDisplay = NULL;
874 }
875
fei.dengb9a1a572023-09-13 01:33:57 +0000876 DEBUG(mLogCategory,"closeDisplay out");
fei.dengf7a0cd32023-08-29 09:36:37 +0000877}
878
879int WaylandDisplay::toDmaBufferFormat(RenderVideoFormat format, uint32_t *outDmaformat /*out param*/, uint64_t *outDmaformatModifiers /*out param*/)
880{
881 if (!outDmaformat || !outDmaformatModifiers) {
fei.dengb9a1a572023-09-13 01:33:57 +0000882 WARNING(mLogCategory,"NULL params");
883 return ERROR_PARAM_NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000884 }
885
886 *outDmaformat = 0;
887 *outDmaformatModifiers = 0;
888
889 uint32_t dmaformat = video_format_to_wl_dmabuf_format (format);
890 if (dmaformat == -1) {
fei.dengb9a1a572023-09-13 01:33:57 +0000891 ERROR(mLogCategory,"Error not found render video format:%d to wl dmabuf format",format);
892 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000893 }
894
fei.dengb9a1a572023-09-13 01:33:57 +0000895 TRACE(mLogCategory,"render video format:%d -> dmabuf format:%d",format,dmaformat);
fei.dengf7a0cd32023-08-29 09:36:37 +0000896 *outDmaformat = (uint32_t)dmaformat;
897
898 /*get dmaformat and modifiers*/
899 auto item = mDmaBufferFormats.find(dmaformat);
900 if (item == mDmaBufferFormats.end()) { //not found
fei.dengb9a1a572023-09-13 01:33:57 +0000901 WARNING(mLogCategory,"Not found dmabuf for render video format :%d",format);
fei.dengf7a0cd32023-08-29 09:36:37 +0000902 *outDmaformatModifiers = 0;
fei.dengb9a1a572023-09-13 01:33:57 +0000903 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000904 }
905
906 *outDmaformatModifiers = (uint64_t)item->second;
907
fei.dengb9a1a572023-09-13 01:33:57 +0000908 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000909}
910
911int WaylandDisplay::toShmBufferFormat(RenderVideoFormat format, uint32_t *outformat)
912{
913 if (!outformat) {
fei.dengb9a1a572023-09-13 01:33:57 +0000914 WARNING(mLogCategory,"NULL params");
915 return ERROR_PARAM_NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000916 }
917
918 *outformat = 0;
919
920 int shmformat = (int)video_format_to_wl_shm_format(format);
921 if (shmformat < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000922 ERROR(mLogCategory,"Error not found render video format:%d to wl shmbuf format",format);
923 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000924 }
925
926 for (auto item = mShmFormats.begin(); item != mShmFormats.end(); ++item) {
927 uint32_t registFormat = (uint32_t)*item;
928 if (registFormat == (uint32_t)shmformat) {
929 *outformat = registFormat;
fei.dengb9a1a572023-09-13 01:33:57 +0000930 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000931 }
932 }
933
fei.dengb9a1a572023-09-13 01:33:57 +0000934 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000935}
936
937void WaylandDisplay::setVideoBufferFormat(RenderVideoFormat format)
938{
fei.dengb9a1a572023-09-13 01:33:57 +0000939 TRACE(mLogCategory,"set video buffer format: %d",format);
fei.dengf7a0cd32023-08-29 09:36:37 +0000940 mBufferFormat = format;
941};
942
943void WaylandDisplay::setDisplayOutput(int output)
944{
fei.dengb9a1a572023-09-13 01:33:57 +0000945 TRACE(mLogCategory,"select display output: %d",output);
fei.dengf7a0cd32023-08-29 09:36:37 +0000946 if (output < 0 || output >= DEFAULT_DISPLAY_OUTPUT_NUM) {
fei.dengb9a1a572023-09-13 01:33:57 +0000947 ERROR(mLogCategory, "display output index error,please set 0:primary or 1:extend,now:%d",output);
fei.dengf7a0cd32023-08-29 09:36:37 +0000948 return;
949 }
fei.deng26950832023-11-09 03:00:23 +0000950 //only do select output before video playing
951 if (mSelectOutputIndex != output) {
952 mSelectOutputIndex = output;
953 // if (mOutput[output].wlOutput) {
954 // mCurrentDisplayOutput = &mOutput[output];
955 // setRenderRectangle(mOutput[output].offsetX, mOutput[output].offsetY,
956 // mOutput[output].width, mOutput[output].height);
957 // }
fei.dengf7a0cd32023-08-29 09:36:37 +0000958 }
959}
960
961int WaylandDisplay::getDisplayOutput()
962{
fei.deng26950832023-11-09 03:00:23 +0000963 return mSelectOutputIndex == INVALID_OUTPUT_INDEX? 0: mSelectOutputIndex;
fei.dengf7a0cd32023-08-29 09:36:37 +0000964}
965
966void WaylandDisplay::setPip(int pip)
967{
fei.dengb9a1a572023-09-13 01:33:57 +0000968 INFO(mLogCategory,"set pip:%d",pip);
fei.dengf7a0cd32023-08-29 09:36:37 +0000969 mPip = pip;
970}
971
fei.deng26950832023-11-09 03:00:23 +0000972void WaylandDisplay::updateDisplayOutput()
973{
974 if (!mCurrentDisplayOutput->wlOutput || !mXdgToplevel || !mXdgSurface)
975 {
976 return;
977 }
978 if (mUpdateRenderRectangle) {
979 if (mFullScreen) {
980 DEBUG(mLogCategory,"unset full screen");
981 xdg_toplevel_unset_fullscreen (mXdgToplevel);
982 }
983
984 if (mXdgSurface) {
985 DEBUG(mLogCategory,"set geometry");
986 xdg_surface_set_window_geometry(mXdgSurface,
987 mCurrentDisplayOutput->offsetX,
988 mCurrentDisplayOutput->offsetY,
989 mCurrentDisplayOutput->width,
990 mCurrentDisplayOutput->height);
991 }
992
993 if (mFullScreen && mXdgToplevel) {
994 DEBUG(mLogCategory,"set full screen");
995 xdg_toplevel_set_fullscreen (mXdgToplevel, mCurrentDisplayOutput->wlOutput);
996 }
997 setRenderRectangle(mCurrentDisplayOutput->offsetX, mCurrentDisplayOutput->offsetY,
998 mCurrentDisplayOutput->width, mCurrentDisplayOutput->height);
999 mUpdateRenderRectangle = false;
1000 }
1001}
1002
fei.dengf7a0cd32023-08-29 09:36:37 +00001003void WaylandDisplay::createCommonWindowSurface()
1004{
1005 struct wl_region *region;
1006
1007 mAreaSurface = wl_compositor_create_surface (mCompositor);
1008 mVideoSurface = wl_compositor_create_surface (mCompositor);
1009 mAreaSurfaceWrapper = (struct wl_surface *)wl_proxy_create_wrapper (mAreaSurface);
1010 mVideoSurfaceWrapper = (struct wl_surface *)wl_proxy_create_wrapper (mVideoSurface);
1011
1012 wl_proxy_set_queue ((struct wl_proxy *) mAreaSurfaceWrapper, mWlQueue);
1013 wl_proxy_set_queue ((struct wl_proxy *) mVideoSurfaceWrapper, mWlQueue);
1014
1015 /* embed video_surface in area_surface */
1016 mVideoSubSurface = wl_subcompositor_get_subsurface (mSubCompositor, mVideoSurface, mAreaSurface);
1017 wl_subsurface_set_desync (mVideoSubSurface);
1018
fei.dengd0da4e22024-07-04 11:29:13 +08001019 //add video surface callback to weston if weston enable this feature
1020 if (mVideoSurface && mAmlConfigAPIList.enableSurfaceDestroyCallback) {
1021 struct wl_callback *surfaceDestroyCb = wl_surface_destroy_callback(mVideoSurface);
1022 wl_callback_add_listener(surfaceDestroyCb, &surface_destroy_listener, this);
1023 }
1024
fei.dengf7a0cd32023-08-29 09:36:37 +00001025 if (mViewporter) {
1026 mAreaViewport = wp_viewporter_get_viewport (mViewporter, mAreaSurface);
1027 mVideoViewport = wp_viewporter_get_viewport (mViewporter, mVideoSurface);
1028 }
1029
1030 /* do not accept input */
1031 region = wl_compositor_create_region (mCompositor);
1032 wl_surface_set_input_region (mAreaSurface, region);
1033 wl_region_destroy (region);
1034
1035 region = wl_compositor_create_region (mCompositor);
1036 wl_surface_set_input_region (mVideoSurface, region);
1037 wl_region_destroy (region);
1038}
1039
1040void WaylandDisplay::createXdgShellWindowSurface()
1041{
1042 /* Check which protocol we will use (in order of preference) */
1043 if (mXdgWmBase) {
1044 /* First create the XDG surface */
1045 mXdgSurface= xdg_wm_base_get_xdg_surface (mXdgWmBase, mAreaSurface);
1046 if (!mXdgSurface) {
fei.dengb9a1a572023-09-13 01:33:57 +00001047 ERROR(mLogCategory,"Unable to get xdg_surface");
fei.dengf7a0cd32023-08-29 09:36:37 +00001048 return;
1049 }
1050 xdg_surface_add_listener (mXdgSurface, &xdg_surface_listener,(void *)this);
1051
1052 /* Then the toplevel */
1053 mXdgToplevel= xdg_surface_get_toplevel (mXdgSurface);
1054 if (!mXdgSurface) {
fei.dengb9a1a572023-09-13 01:33:57 +00001055 ERROR(mLogCategory,"Unable to get xdg_toplevel");
fei.dengf7a0cd32023-08-29 09:36:37 +00001056 return;
1057 }
1058 xdg_toplevel_add_listener (mXdgToplevel, &xdg_toplevel_listener, this);
1059
1060 /* Finally, commit the xdg_surface state as toplevel */
1061 mXdgSurfaceConfigured = false;
1062 wl_surface_commit (mAreaSurface);
1063 wl_display_flush (mWlDisplay);
1064 /* we need exactly 3 roundtrips to discover global objects and their state */
1065 for (int i = 0; i < 3; i++) {
1066 if (wl_display_roundtrip_queue(mWlDisplay, mWlQueue) < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +00001067 ERROR(mLogCategory,"Error communicating with the wayland display");
fei.dengf7a0cd32023-08-29 09:36:37 +00001068 }
1069 }
1070
1071 if (mXdgSurfaceConfigured) {
fei.dengb9a1a572023-09-13 01:33:57 +00001072 INFO(mLogCategory,"xdg surface had configured");
fei.dengf7a0cd32023-08-29 09:36:37 +00001073 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001074 WARNING(mLogCategory,"xdg surface not configured");
fei.dengf7a0cd32023-08-29 09:36:37 +00001075 }
1076
1077 //full screen show
fei.deng26950832023-11-09 03:00:23 +00001078 // if (mFullScreen && mCurrentDisplayOutput->wlOutput) {
1079 // //ensureFullscreen(mFullScreen);
fei.dengf7a0cd32023-08-29 09:36:37 +00001080 // }
1081 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001082 ERROR(mLogCategory,"Unable to use xdg_wm_base ");
fei.dengf7a0cd32023-08-29 09:36:37 +00001083 return;
1084 }
1085}
1086
1087void WaylandDisplay::destroyWindowSurfaces()
1088{
fei.dengf7a0cd32023-08-29 09:36:37 +00001089 if (mXdgToplevel) {
1090 xdg_toplevel_destroy (mXdgToplevel);
1091 mXdgToplevel = NULL;
1092 }
1093
1094 if (mXdgSurface) {
1095 xdg_surface_destroy (mXdgSurface);
1096 mXdgSurface = NULL;
1097 }
1098
1099 if (mVideoSurfaceWrapper) {
1100 wl_proxy_wrapper_destroy (mVideoSurfaceWrapper);
1101 mVideoSurfaceWrapper = NULL;
1102 }
1103
1104 if (mVideoSubSurface) {
1105 wl_subsurface_destroy (mVideoSubSurface);
1106 mVideoSubSurface = NULL;
1107 }
1108
1109 if (mVideoSurface) {
1110 wl_surface_destroy (mVideoSurface);
1111 mVideoSurface = NULL;
1112 }
1113
1114 if (mAreaSurfaceWrapper) {
1115 wl_proxy_wrapper_destroy (mAreaSurfaceWrapper);
1116 mAreaSurfaceWrapper = NULL;
1117 }
1118
1119 if (mAreaSurface) {
1120 wl_surface_destroy (mAreaSurface);
1121 mAreaSurface = NULL;
1122 mReCommitAreaSurface = false;
1123 }
1124}
1125
1126void WaylandDisplay::ensureFullscreen(bool fullscreen)
1127{
1128 if (mXdgWmBase) {
fei.dengb9a1a572023-09-13 01:33:57 +00001129 DEBUG(mLogCategory,"full screen : %d",fullscreen);
fei.dengf7a0cd32023-08-29 09:36:37 +00001130 if (fullscreen) {
fei.deng26950832023-11-09 03:00:23 +00001131 xdg_toplevel_set_fullscreen (mXdgToplevel, mCurrentDisplayOutput->wlOutput);
fei.dengf7a0cd32023-08-29 09:36:37 +00001132 } else {
1133 xdg_toplevel_unset_fullscreen (mXdgToplevel);
1134 }
1135 }
1136}
1137
1138void WaylandDisplay::setRenderRectangle(int x, int y, int w, int h)
1139{
fei.dengb9a1a572023-09-13 01:33:57 +00001140 DEBUG(mLogCategory,"set render rect:x:%d,y:%d,w:%d,h:%d",x,y,w,h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001141
1142 if (w <= 0 || h <= 0) {
fei.dengb9a1a572023-09-13 01:33:57 +00001143 WARNING(mLogCategory, "wrong render width or height %dx%d",w,h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001144 return;
1145 }
1146
1147 mRenderRect.x = x;
1148 mRenderRect.y = y;
1149 mRenderRect.w = w;
1150 mRenderRect.h = h;
1151
1152 if (!mXdgSurfaceConfigured) {
fei.dengb9a1a572023-09-13 01:33:57 +00001153 WARNING(mLogCategory,"Not configured xdg");
fei.dengf7a0cd32023-08-29 09:36:37 +00001154 return;
1155 }
1156
1157 if (mAreaViewport) {
1158 wp_viewport_set_destination (mAreaViewport, w, h);
1159 }
1160
1161 updateBorders();
1162
1163 if (mVideoWidth != 0 && mVideoSurface) {
1164 wl_subsurface_set_sync (mVideoSubSurface);
1165 resizeVideoSurface(true);
1166 }
1167
1168 wl_surface_damage (mAreaSurfaceWrapper, 0, 0, w, h);
1169 wl_surface_commit (mAreaSurfaceWrapper);
1170
1171 if (mVideoWidth != 0) {
1172 wl_subsurface_set_desync (mVideoSubSurface);
1173 }
1174}
1175
1176
1177void WaylandDisplay::setFrameSize(int w, int h)
1178{
1179 mVideoWidth = w;
1180 mVideoHeight = h;
fei.dengb9a1a572023-09-13 01:33:57 +00001181 TRACE(mLogCategory,"frame w:%d,h:%d",mVideoWidth,mVideoHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +00001182 if (mRenderRect.w > 0 && mVideoSurface) {
1183 resizeVideoSurface(true);
1184 }
1185}
1186
1187void WaylandDisplay::setWindowSize(int x, int y, int w, int h)
1188{
1189 mWindowRect.x = x;
1190 mWindowRect.y = y;
1191 mWindowRect.w = w;
1192 mWindowRect.h = h;
fei.dengb9a1a572023-09-13 01:33:57 +00001193 TRACE(mLogCategory,"window size:x:%d,y:%d,w:%d,h:%d",mWindowRect.x,mWindowRect.y,mWindowRect.w,mWindowRect.h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001194 if (mWindowRect.w > 0 && mVideoWidth > 0 && mVideoSurface) {
1195 //if had full screen, unset it and set window size
1196 if (mFullScreen) {
1197 mFullScreen = false;
1198 ensureFullscreen(mFullScreen);
1199 }
1200 resizeVideoSurface(true);
1201 }
1202}
1203
1204
1205void WaylandDisplay::resizeVideoSurface(bool commit)
1206{
1207 Rectangle src = {0,};
1208 Rectangle dst = {0,};
1209 Rectangle res;
1210
1211 /* center the video_subsurface inside area_subsurface */
1212 src.w = mVideoWidth;
1213 src.h = mVideoHeight;
1214 /*if had set the window size, we will scall
1215 video surface to this window size*/
1216 if (mWindowRect.w > 0 && mWindowRect.h > 0) {
1217 dst.x = mWindowRect.x;
1218 dst.y = mWindowRect.y;
1219 dst.w = mWindowRect.w;
1220 dst.h = mWindowRect.h;
1221 if (mWindowRect.w > mRenderRect.w && mWindowRect.h > mRenderRect.h) {
fei.dengb9a1a572023-09-13 01:33:57 +00001222 WARNING(mLogCategory,"Error window size:%dx%d, but render size:%dx%d,reset to render size",
fei.dengf7a0cd32023-08-29 09:36:37 +00001223 mWindowRect.w,mWindowRect.h,mRenderRect.w,mRenderRect.h);
1224 dst.x = mRenderRect.x;
1225 dst.y = mRenderRect.y;
1226 dst.w = mRenderRect.w;
1227 dst.h = mRenderRect.h;
1228 }
1229 //to do,we need set geometry?
1230 //if (mXdgSurface) {
1231 // xdg_surface_set_window_geometry(mXdgSurface, mWindowRect.x, mWindowRect.y, mWindowRect.w, mWindowRect.h);
1232 //}
1233 } else { //scal video to full screen
1234 dst.w = mRenderRect.w;
1235 dst.h = mRenderRect.h;
1236 }
1237
1238 if (mViewporter) {
1239 videoCenterRect(src, dst, &res, true);
1240 } else {
1241 videoCenterRect(src, dst, &res, false);
1242 }
1243
1244 wl_subsurface_set_position (mVideoSubSurface, res.x, res.y);
1245
1246 if (commit) {
1247 wl_surface_damage (mVideoSurfaceWrapper, 0, 0, res.w, res.h);
1248 wl_surface_commit (mVideoSurfaceWrapper);
1249 }
1250
1251 //top level setting
1252 if (mXdgToplevel) {
1253 struct wl_region *region;
1254
1255 region = wl_compositor_create_region (mCompositor);
1256 wl_region_add (region, 0, 0, mRenderRect.w, mRenderRect.h);
1257 wl_surface_set_input_region (mAreaSurface, region);
1258 wl_region_destroy (region);
1259 }
1260
1261 /* this is saved for use in wl_surface_damage */
1262 mVideoRect.x = res.x;
1263 mVideoRect.y = res.y;
1264 mVideoRect.w = res.w;
1265 mVideoRect.h = res.h;
1266
1267 //to scale video surface to full screen
1268 wp_viewport_set_destination(mVideoViewport, res.w, res.h);
fei.dengb9a1a572023-09-13 01:33:57 +00001269 TRACE(mLogCategory,"video rectangle,x:%d,y:%d,w:%d,h:%d",mVideoRect.x, mVideoRect.y, mVideoRect.w, mVideoRect.h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001270}
1271
1272void WaylandDisplay::setOpaque()
1273{
1274 struct wl_region *region;
1275
1276 /* Set area opaque */
1277 region = wl_compositor_create_region (mCompositor);
1278 wl_region_add (region, 0, 0, mRenderRect.w, mRenderRect.h);
1279 wl_surface_set_opaque_region (mAreaSurface, region);
1280 wl_region_destroy (region);
1281}
1282
1283int WaylandDisplay::prepareFrameBuffer(RenderBuffer * buf)
1284{
1285 WaylandBuffer *waylandBuf = NULL;
1286 int ret;
1287
fei.dengdd910ef2024-06-07 10:25:30 +08001288 if (!mDmabuf)
1289 {
1290 ERROR(mLogCategory,"Error zwp_linux_dmabuf_v1");
1291 return ERROR_UNKNOWN;
1292 }
1293
fei.dengf7a0cd32023-08-29 09:36:37 +00001294 waylandBuf = findWaylandBuffer(buf);
1295 if (waylandBuf == NULL) {
fei.dengb9a1a572023-09-13 01:33:57 +00001296 waylandBuf = new WaylandBuffer(this, mLogCategory);
fei.dengf7a0cd32023-08-29 09:36:37 +00001297 waylandBuf->setBufferFormat(mBufferFormat);
1298 ret = waylandBuf->constructWlBuffer(buf);
fei.dengb9a1a572023-09-13 01:33:57 +00001299 if (ret != NO_ERROR) {
1300 WARNING(mLogCategory,"dmabufConstructWlBuffer fail,release waylandbuf");
fei.dengf7a0cd32023-08-29 09:36:37 +00001301 //delete waylanBuf,WaylandBuffer object destruct will call release callback
1302 goto waylandbuf_fail;
1303 } else {
1304 addWaylandBuffer(buf, waylandBuf);
1305 }
1306 }
fei.dengb9a1a572023-09-13 01:33:57 +00001307 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +00001308waylandbuf_fail:
1309 //delete waylandbuf
1310 delete waylandBuf;
1311 waylandBuf = NULL;
fei.dengb9a1a572023-09-13 01:33:57 +00001312 return ERROR_UNKNOWN;
fei.dengf7a0cd32023-08-29 09:36:37 +00001313}
1314
1315void WaylandDisplay::displayFrameBuffer(RenderBuffer * buf, int64_t realDisplayTime)
1316{
1317 WaylandBuffer *waylandBuf = NULL;
1318 struct wl_buffer * wlbuffer = NULL;
1319 int ret;
1320
1321 if (!buf) {
fei.dengb9a1a572023-09-13 01:33:57 +00001322 ERROR(mLogCategory,"Error input params, waylandbuffer is null");
fei.dengf7a0cd32023-08-29 09:36:37 +00001323 return;
1324 }
1325
1326 //must commit areasurface first, because weston xdg surface maybe timeout
1327 //this cause video is not display,commit can resume xdg surface
1328 if (!mReCommitAreaSurface) {
1329 mReCommitAreaSurface = true;
1330 wl_surface_commit (mAreaSurface);
1331 }
1332
fei.dengdd910ef2024-06-07 10:25:30 +08001333 //TRACE(mLogCategory,"display renderBuffer:%p,PTS:%lld us,realtime:%lld",buf, buf->pts/1000, realDisplayTime);
fei.dengf7a0cd32023-08-29 09:36:37 +00001334
1335 if (buf->flag & BUFFER_FLAG_DMA_BUFFER) {
1336 if (buf->dma.width <=0 || buf->dma.height <=0) {
1337 buf->dma.width = mVideoWidth;
1338 buf->dma.height = mVideoHeight;
1339 }
1340 waylandBuf = findWaylandBuffer(buf);
1341 if (waylandBuf) {
1342 waylandBuf->setRenderRealTime(realDisplayTime);
1343 ret = waylandBuf->constructWlBuffer(buf);
fei.dengb9a1a572023-09-13 01:33:57 +00001344 if (ret != NO_ERROR) {
1345 WARNING(mLogCategory,"dmabufConstructWlBuffer fail,release waylandbuf");
fei.dengf7a0cd32023-08-29 09:36:37 +00001346 //delete waylanBuf,WaylandBuffer object destruct will call release callback
1347 goto waylandbuf_fail;
1348 }
1349 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001350 ERROR(mLogCategory,"NOT found wayland buffer,please prepare buffer first");
fei.dengf7a0cd32023-08-29 09:36:37 +00001351 goto waylandbuf_fail;
1352 }
1353 }
1354
1355 if (waylandBuf) {
1356 wlbuffer = waylandBuf->getWlBuffer();
1357 }
fei.deng1cfb2752023-10-26 08:01:25 +00001358 //if no wl_output, drop this buffer
fei.deng26950832023-11-09 03:00:23 +00001359 if (mCurrentDisplayOutput->wlOutput == NULL) {
fei.deng1cfb2752023-10-26 08:01:25 +00001360 TRACE(mLogCategory,"No wl_output");
1361 mWaylandPlugin->handleFrameDropped(buf);
1362 mWaylandPlugin->handleBufferRelease(buf);
1363 return;
1364 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001365 if (wlbuffer) {
fei.dengb9a1a572023-09-13 01:33:57 +00001366 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001367 ++mCommitCnt;
1368 uint32_t hiPts = realDisplayTime >> 32;
1369 uint32_t lowPts = realDisplayTime & 0xFFFFFFFF;
1370 //attach this wl_buffer to weston
fei.dengdd910ef2024-06-07 10:25:30 +08001371 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.dengf7a0cd32023-08-29 09:36:37 +00001372 waylandBuf->attach(mVideoSurfaceWrapper);
1373
fei.deng640c3c92024-04-12 08:31:19 +00001374 if (mAmlConfigAPIList.enableSetPts) {
fei.dengb9a1a572023-09-13 01:33:57 +00001375 TRACE(mLogCategory,"display time:%lld,hiPts:%u,lowPts:%u",realDisplayTime, hiPts, lowPts);
fei.dengf7a0cd32023-08-29 09:36:37 +00001376 wl_surface_set_pts(mVideoSurfaceWrapper, hiPts, lowPts);
1377 }
1378
1379 wl_surface_damage (mVideoSurfaceWrapper, 0, 0, mVideoRect.w, mVideoRect.h);
1380 wl_surface_commit (mVideoSurfaceWrapper);
1381 //insert this buffer to committed weston buffer manager
fei.dengae8c90a2024-06-27 13:39:53 +08001382 std::pair<int64_t, WaylandBuffer *> item(realDisplayTime, waylandBuf);
fei.dengf7a0cd32023-08-29 09:36:37 +00001383 mCommittedBufferMap.insert(item);
1384 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001385 WARNING(mLogCategory,"wlbuffer is NULL");
fei.dengf7a0cd32023-08-29 09:36:37 +00001386 /* clear both video and parent surfaces */
1387 cleanSurface();
1388 }
1389
1390 wl_display_flush (mWlDisplay);
fei.deng4029e682024-06-26 17:06:31 +08001391 //set keep last frame or not when after send first buffer to weston,1 keep last frame, 0 not
1392 if (mToSendKeepLastFrame) {
1393 setKeepLastFrame(mKeepLastFrame);
1394 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001395
1396 return;
1397waylandbuf_fail:
1398 //notify dropped
1399 mWaylandPlugin->handleFrameDropped(buf);
1400 //notify app release this buf
1401 mWaylandPlugin->handleBufferRelease(buf);
1402 //delete waylandbuf
1403 delete waylandBuf;
1404 waylandBuf = NULL;
1405 return;
1406}
1407
1408void WaylandDisplay::handleBufferReleaseCallback(WaylandBuffer *buf)
1409{
1410 {
fei.dengb9a1a572023-09-13 01:33:57 +00001411 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001412 //remove buffer if this buffer is ready to release
1413 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengae8c90a2024-06-27 13:39:53 +08001414 auto item = mCommittedBufferMap.find(buf->getRenderRealTime());
fei.dengf7a0cd32023-08-29 09:36:37 +00001415 if (item != mCommittedBufferMap.end()) {
fei.dengdd910ef2024-06-07 10:25:30 +08001416 --mCommitCnt;
fei.dengf7a0cd32023-08-29 09:36:37 +00001417 mCommittedBufferMap.erase(item);
1418 } else {
fei.dengd0da4e22024-07-04 11:29:13 +08001419 TRACE(mLogCategory,"Can't find WaylandBuffer pts:%lld us in buffer map",renderBuffer->pts/1000);
fei.dengf7a0cd32023-08-29 09:36:37 +00001420 return;
1421 }
1422 }
1423 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengdd910ef2024-06-07 10:25:30 +08001424 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.dengf7a0cd32023-08-29 09:36:37 +00001425 mWaylandPlugin->handleBufferRelease(renderBuffer);
1426}
1427
1428void WaylandDisplay::handleFrameDisplayedCallback(WaylandBuffer *buf)
1429{
1430 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengb9a1a572023-09-13 01:33:57 +00001431 TRACE(mLogCategory,"handle displayed renderBuffer :%p,PTS:%lld us,realtime:%lld us",renderBuffer,renderBuffer->pts/1000,buf->getRenderRealTime());
fei.deng3287c082024-04-23 09:29:22 +00001432 if (!mSignalFirstFramePts) {
1433 mSignalFirstFramePts = true;
1434 mWaylandPlugin->handleMsgNotify(MSG_FIRST_FRAME, (void*)&renderBuffer->pts);
1435 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001436 mWaylandPlugin->handleFrameDisplayed(renderBuffer);
1437}
1438
1439void WaylandDisplay::handleFrameDropedCallback(WaylandBuffer *buf)
1440{
1441 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengb9a1a572023-09-13 01:33:57 +00001442 TRACE(mLogCategory,"handle droped renderBuffer :%p,PTS:%lld us,realtime:%lld us",renderBuffer,renderBuffer->pts/1000,buf->getRenderRealTime());
fei.dengf7a0cd32023-08-29 09:36:37 +00001443 mWaylandPlugin->handleFrameDropped(renderBuffer);
1444}
1445
1446
1447void WaylandDisplay::readyToRun()
1448{
1449 mFd = wl_display_get_fd (mWlDisplay);
fei.dengb9a1a572023-09-13 01:33:57 +00001450 if (mPoll) {
1451 mPoll->addFd(mFd);
1452 mPoll->setFdReadable(mFd, true);
1453 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001454}
1455
fei.dengdd910ef2024-06-07 10:25:30 +08001456void WaylandDisplay::readyToExit()
1457{
1458 if (mPoll && mFd >= 0) {
1459 mPoll->removeFd(mFd);
1460 }
1461}
1462
fei.dengf7a0cd32023-08-29 09:36:37 +00001463bool WaylandDisplay::threadLoop()
1464{
1465 int ret;
fei.dengf7a0cd32023-08-29 09:36:37 +00001466
1467 while (wl_display_prepare_read_queue (mWlDisplay, mWlQueue) != 0) {
1468 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1469 }
1470
1471 wl_display_flush (mWlDisplay);
1472
1473 /*poll timeout value must > 300 ms,otherwise zwp_linux_dmabuf will create failed,
1474 so do use -1 to wait for ever*/
fei.dengb9a1a572023-09-13 01:33:57 +00001475 ret = mPoll->wait(-1); //wait for ever
fei.dengf7a0cd32023-08-29 09:36:37 +00001476 if (ret < 0) { //poll error
fei.dengb9a1a572023-09-13 01:33:57 +00001477 WARNING(mLogCategory,"poll error");
fei.dengf7a0cd32023-08-29 09:36:37 +00001478 wl_display_cancel_read(mWlDisplay);
1479 return false;
1480 } else if (ret == 0) { //poll time out
1481 return true; //run loop
1482 }
1483
1484 if (wl_display_read_events (mWlDisplay) == -1) {
1485 goto tag_error;
1486 }
1487
1488 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1489 return true;
1490tag_error:
fei.dengb9a1a572023-09-13 01:33:57 +00001491 ERROR(mLogCategory,"Error communicating with the wayland server");
fei.dengf7a0cd32023-08-29 09:36:37 +00001492 return false;
1493}
1494
1495void WaylandDisplay::videoCenterRect(Rectangle src, Rectangle dst, Rectangle *result, bool scaling)
1496{
1497 //if dst is a small window, we scale video to map window size,don't doing center
1498 if (mRenderRect.w != dst.w && mRenderRect.h != dst.h) {
1499 result->x = dst.x;
1500 result->y = dst.y;
1501 result->w = dst.w;
1502 result->h = dst.h;
fei.dengb9a1a572023-09-13 01:33:57 +00001503 TRACE(mLogCategory,"small window source is %dx%d dest is %dx%d, result is %dx%d with x,y %dx%d",
fei.dengf7a0cd32023-08-29 09:36:37 +00001504 src.w, src.h, dst.w, dst.h, result->w, result->h, result->x, result->y);
1505 return;
1506 }
1507 if (!scaling) {
1508 result->w = MIN (src.w, dst.w);
1509 result->h = MIN (src.h, dst.h);
1510 result->x = dst.x + (dst.w - result->w) / 2;
1511 result->y = dst.y + (dst.h - result->h) / 2;
1512 } else {
1513 double src_ratio, dst_ratio;
1514
1515 src_ratio = (double) src.w / src.h;
1516 dst_ratio = (double) dst.w / dst.h;
1517
1518 if (src_ratio > dst_ratio) {
1519 result->w = dst.w;
1520 result->h = dst.w / src_ratio;
1521 result->x = dst.x;
1522 result->y = dst.y + (dst.h - result->h) / 2;
1523 } else if (src_ratio < dst_ratio) {
1524 result->w = dst.h * src_ratio;
1525 result->h = dst.h;
1526 result->x = dst.x + (dst.w - result->w) / 2;
1527 result->y = dst.y;
1528 } else {
1529 result->x = dst.x;
1530 result->y = dst.y;
1531 result->w = dst.w;
1532 result->h = dst.h;
1533 }
1534 }
1535
fei.dengb9a1a572023-09-13 01:33:57 +00001536 TRACE(mLogCategory,"source is %dx%d dest is %dx%d, result is %dx%d with x,y %dx%d",
fei.dengf7a0cd32023-08-29 09:36:37 +00001537 src.w, src.h, dst.w, dst.h, result->w, result->h, result->x, result->y);
1538}
1539
1540void WaylandDisplay::updateBorders()
1541{
1542 int width,height;
1543
1544 if (mNoBorderUpdate)
1545 return;
1546
1547 if (mViewporter) {
1548 width = height = 1;
1549 mNoBorderUpdate = true;
1550 } else {
1551 width = mRenderRect.w;
1552 height = mRenderRect.h;
1553 }
1554
1555 RenderVideoFormat format = VIDEO_FORMAT_BGRA;
fei.dengb9a1a572023-09-13 01:33:57 +00001556 mAreaShmBuffer = new WaylandShmBuffer(this, mLogCategory);
fei.dengf7a0cd32023-08-29 09:36:37 +00001557 struct wl_buffer *wlbuf = mAreaShmBuffer->constructWlBuffer(width, height, format);
1558 if (wlbuf == NULL) {
1559 delete mAreaShmBuffer;
1560 mAreaShmBuffer = NULL;
1561 }
1562
1563 wl_surface_attach(mAreaSurfaceWrapper, wlbuf, 0, 0);
1564}
1565
1566std::size_t WaylandDisplay::calculateDmaBufferHash(RenderDmaBuffer &dmabuf)
1567{
1568 std::string hashString("");
1569 for (int i = 0; i < dmabuf.planeCnt; i++) {
1570 char hashtmp[1024];
1571 snprintf (hashtmp, 1024, "%d%d%d%d%d%d", dmabuf.width,dmabuf.height,dmabuf.planeCnt,
1572 dmabuf.stride[i],dmabuf.offset[i],dmabuf.fd[i]);
1573 std::string tmp(hashtmp);
1574 hashString += tmp;
1575 }
1576
1577 std::size_t hashval = std::hash<std::string>()(hashString);
fei.dengb9a1a572023-09-13 01:33:57 +00001578 //TRACE(mLogCategory,"hashstr:%s,val:%zu",hashString.c_str(),hashval);
fei.dengf7a0cd32023-08-29 09:36:37 +00001579 return hashval;
1580}
1581
1582void WaylandDisplay::addWaylandBuffer(RenderBuffer * buf, WaylandBuffer *waylandbuf)
1583{
1584 if (buf->flag & BUFFER_FLAG_DMA_BUFFER) {
1585 std::size_t hashval = calculateDmaBufferHash(buf->dma);
1586 std::pair<std::size_t, WaylandBuffer *> item(hashval, waylandbuf);
fei.dengb9a1a572023-09-13 01:33:57 +00001587 //TRACE(mLogCategory,"fd:%d,w:%d,h:%d,%p,hash:%zu",buf->dma.fd[0],buf->dma.width,buf->dma.height,waylandbuf,hashval);
fei.dengf7a0cd32023-08-29 09:36:37 +00001588 mWaylandBuffersMap.insert(item);
1589 }
1590
1591 //clean invalid wayland buffer,if video resolution changed
1592 for (auto item = mWaylandBuffersMap.begin(); item != mWaylandBuffersMap.end(); ) {
1593 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1594 if ((waylandbuf->getFrameWidth() != buf->dma.width ||
1595 waylandbuf->getFrameHeight() != buf->dma.height) &&
1596 waylandbuf->isFree()) {
fei.dengb9a1a572023-09-13 01:33:57 +00001597 TRACE(mLogCategory,"delete wayland buffer,width:%d(%d),height:%d(%d)",
fei.dengf7a0cd32023-08-29 09:36:37 +00001598 waylandbuf->getFrameWidth(),buf->dma.width,
1599 waylandbuf->getFrameHeight(),buf->dma.height);
1600 mWaylandBuffersMap.erase(item++);
1601 delete waylandbuf;
1602 } else {
1603 item++;
1604 }
1605 }
fei.dengb9a1a572023-09-13 01:33:57 +00001606 TRACE(mLogCategory,"mWaylandBuffersMap size:%d",mWaylandBuffersMap.size());
fei.dengf7a0cd32023-08-29 09:36:37 +00001607}
1608
1609WaylandBuffer* WaylandDisplay::findWaylandBuffer(RenderBuffer * buf)
1610{
1611 std::size_t hashval = calculateDmaBufferHash(buf->dma);
1612 auto item = mWaylandBuffersMap.find(hashval);
1613 if (item == mWaylandBuffersMap.end()) {
1614 return NULL;
1615 }
1616
1617 return (WaylandBuffer*) item->second;
1618}
1619
1620void WaylandDisplay::cleanAllWaylandBuffer()
1621{
1622 //free all obtain buff
1623 for (auto item = mWaylandBuffersMap.begin(); item != mWaylandBuffersMap.end(); ) {
1624 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1625 mWaylandBuffersMap.erase(item++);
1626 delete waylandbuf;
1627 }
1628}
1629
1630void WaylandDisplay::flushBuffers()
1631{
fei.dengb9a1a572023-09-13 01:33:57 +00001632 INFO(mLogCategory,"flushBuffers");
1633 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001634 for (auto item = mCommittedBufferMap.begin(); item != mCommittedBufferMap.end(); item++) {
1635 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1636 waylandbuf->forceRedrawing();
1637 handleFrameDisplayedCallback(waylandbuf);
1638 }
1639}
1640
1641void WaylandDisplay::cleanSurface()
1642{
1643 /* clear both video and parent surfaces */
1644 wl_surface_attach (mVideoSurfaceWrapper, NULL, 0, 0);
1645 wl_surface_commit (mVideoSurfaceWrapper);
1646 wl_surface_attach (mAreaSurfaceWrapper, NULL, 0, 0);
1647 wl_surface_commit (mAreaSurfaceWrapper);
fei.deng640c3c92024-04-12 08:31:19 +00001648}
1649
1650void WaylandDisplay::setKeepLastFrame(int keep)
1651{
1652 mKeepLastFrame = keep;
1653 if (mVideoSurfaceWrapper && mAmlConfigAPIList.enableKeepLastFrame) {
1654 INFO(mLogCategory,"keep last frame:%d",keep);
1655 wl_surface_keep_last_frame(mVideoSurfaceWrapper, keep);
fei.deng4029e682024-06-26 17:06:31 +08001656 mToSendKeepLastFrame = false;
1657 return;
fei.deng640c3c92024-04-12 08:31:19 +00001658 }
fei.deng4029e682024-06-26 17:06:31 +08001659 mToSendKeepLastFrame = true;
fei.dengf7a0cd32023-08-29 09:36:37 +00001660}