blob: 882c441f1798599cc47e66533e3076b4284103ac [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.deng640c3c92024-04-12 08:31:19 +0000455void WaylandDisplay::amlConfigure(void *data, struct aml_config *config, const char *list) {
456 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
457 TRACE(self->mLogCategory,"aml_config:%s",list);
458 if (list && strlen(list) > 0) {
459 if (strstr(list, "set_video_plane")) {
460 TRACE(self->mLogCategory,"weston enable set_video_plane");
461 self->mAmlConfigAPIList.enableSetVideoPlane = true;
462 }
463 if (strstr(list, "set_pts")) {
464 TRACE(self->mLogCategory,"weston enable set_pts");
465 self->mAmlConfigAPIList.enableSetPts = true;
466 }
467 if (strstr(list, "drop")) {
468 TRACE(self->mLogCategory,"weston enable drop");
469 self->mAmlConfigAPIList.enableDropFrame = true;
470 }
471 if (strstr(list, "keep_last_frame")) {
472 TRACE(self->mLogCategory,"weston enable keep_last_frame");
473 self->mAmlConfigAPIList.enableKeepLastFrame = true;
474 }
475 }
476}
477
478static const struct aml_config_listener aml_config_listener = {
479 WaylandDisplay::amlConfigure,
480};
481
fei.dengf7a0cd32023-08-29 09:36:37 +0000482void
483WaylandDisplay::registryHandleGlobal (void *data, struct wl_registry *registry,
fei.dengaf9b07d2023-10-10 07:38:40 +0000484 uint32_t name, const char *interface, uint32_t version)
fei.dengf7a0cd32023-08-29 09:36:37 +0000485{
486 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
fei.dengaf9b07d2023-10-10 07:38:40 +0000487 TRACE(self->mLogCategory,"registryHandleGlobal,name:%u,interface:%s,version:%d",name,interface,version);
fei.dengf7a0cd32023-08-29 09:36:37 +0000488
489 if (strcmp (interface, "wl_compositor") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000490 self->mCompositor = (struct wl_compositor *)wl_registry_bind (registry, name, &wl_compositor_interface, 1/*MIN (version, 3)*/);
fei.dengf7a0cd32023-08-29 09:36:37 +0000491 } else if (strcmp (interface, "wl_subcompositor") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000492 self->mSubCompositor = (struct wl_subcompositor *)wl_registry_bind (registry, name, &wl_subcompositor_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000493 } else if (strcmp (interface, "xdg_wm_base") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000494 self->mXdgWmBase = (struct xdg_wm_base *)wl_registry_bind (registry, name, &xdg_wm_base_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000495 xdg_wm_base_add_listener (self->mXdgWmBase, &xdg_wm_base_listener, (void *)self);
496 } else if (strcmp (interface, "wl_shm") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000497 self->mShm = (struct wl_shm *)wl_registry_bind (registry, name, &wl_shm_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000498 wl_shm_add_listener (self->mShm, &shm_listener, self);
499 } else if (strcmp (interface, "zwp_fullscreen_shell_v1") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000500 //self->mFullscreenShell = (struct zwp_fullscreen_shell_v1 *)wl_registry_bind (registry, name,
fei.dengf7a0cd32023-08-29 09:36:37 +0000501 // &zwp_fullscreen_shell_v1_interface, 1);
502 } else if (strcmp (interface, "wp_viewporter") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000503 self->mViewporter = (struct wp_viewporter *)wl_registry_bind (registry, name, &wp_viewporter_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000504 } else if (strcmp (interface, "zwp_linux_dmabuf_v1") == 0) {
505 if (version < 3)
506 return;
fei.dengaf9b07d2023-10-10 07:38:40 +0000507 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 +0000508 zwp_linux_dmabuf_v1_add_listener (self->mDmabuf, &dmabuf_listener, (void *)self);
509 } else if (strcmp (interface, "wl_output") == 0) {
fei.deng26950832023-11-09 03:00:23 +0000510 int i = 0;
511 uint32_t oriName = self->mCurrentDisplayOutput->name;
fei.dengaf9b07d2023-10-10 07:38:40 +0000512 for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
513 if (self->mOutput[i].wlOutput == NULL) {
514 self->mOutput[i].name = name;
515 self->mOutput[i].wlOutput = (struct wl_output*)wl_registry_bind(registry, name, &wl_output_interface, version);
fei.deng26950832023-11-09 03:00:23 +0000516 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 +0000517 wl_output_add_listener(self->mOutput[i].wlOutput, &outputListener, (void *)self);
518 if (i == 0) { //primary wl_output
519 self->mOutput[i].isPrimary = true;
520 }
fei.deng26950832023-11-09 03:00:23 +0000521 break;
fei.dengaf9b07d2023-10-10 07:38:40 +0000522 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000523 }
fei.deng26950832023-11-09 03:00:23 +0000524 if (i == DEFAULT_DISPLAY_OUTPUT_NUM) {
525 WARNING(self->mLogCategory,"Not enough free output");
526 }
527 //select current wl_output
528 if (self->mSelectOutputIndex != INVALID_OUTPUT_INDEX && !self->isRunning()) {
529 TRACE(self->mLogCategory,"select %d output",self->mSelectOutputIndex);
530 self->mCurrentDisplayOutput = &self->mOutput[self->mSelectOutputIndex];
531 }
532 //if user select a wrong output index, we using a suiteble wl_output
533 if (self->mCurrentDisplayOutput->wlOutput == NULL) {
534 WARNING(self->mLogCategory,"wl_output is null,we should find a suiteble output");
535 for (i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
536 if (self->mOutput[i].wlOutput) {
537 self->mCurrentDisplayOutput = &self->mOutput[i];
538 break;
539 }
540 }
541 }
542 //if current wl_output update, we should update render rectangle
543 if (self->mCurrentDisplayOutput->name != oriName) {
544 self->mUpdateRenderRectangle = true;
545 }
546 //if wl_output plugin,active sending frame
547 self->setRedrawingPending(false);
fei.dengf7a0cd32023-08-29 09:36:37 +0000548 } else if (strcmp(interface, "wl_seat") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000549 //self->mSeat = (struct wl_seat *)wl_registry_bind(registry, name, &wl_seat_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000550 //wl_seat_add_listener(self->mSeat, &seat_listener, (void *)self);
fei.deng31f93f12024-02-27 07:52:10 +0000551 } else if (strcmp(interface, "weston_direct_display_v1") == 0) {
552 self->mDirect_display = (struct weston_direct_display_v1 *)wl_registry_bind(registry,name, &weston_direct_display_v1_interface, 1);
fei.deng640c3c92024-04-12 08:31:19 +0000553 } else if (strcmp(interface, "aml_config") == 0) {
554 self->mAmlConfig = (struct aml_config*)wl_registry_bind(registry, name, &aml_config_interface, 1);
555 aml_config_add_listener(self->mAmlConfig, &aml_config_listener, (void *)self);
fei.dengf7a0cd32023-08-29 09:36:37 +0000556 }
557}
558
559void
560WaylandDisplay::registryHandleGlobalRemove (void *data, struct wl_registry *registry, uint32_t name)
561{
fei.deng26950832023-11-09 03:00:23 +0000562 int i;
fei.dengf7a0cd32023-08-29 09:36:37 +0000563 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
fei.dengaf9b07d2023-10-10 07:38:40 +0000564 /* check wl_output changed */
565 DEBUG(self->mLogCategory,"wayland display remove registry handle global,name:%u",name);
fei.deng26950832023-11-09 03:00:23 +0000566 //if user selected wl_output removed, reset selected output index
567 if (self->mSelectOutputIndex != INVALID_OUTPUT_INDEX &&
568 self->mOutput[self->mSelectOutputIndex].wlOutput &&
569 self->mOutput[self->mSelectOutputIndex].name == name) {
570 self->mSelectOutputIndex = INVALID_OUTPUT_INDEX;
571 }
572 for (i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000573 if (self->mOutput[i].name == name) {
fei.deng26950832023-11-09 03:00:23 +0000574 DEBUG(self->mLogCategory,"remove wl_output name:%u,wl_output:%p",name,self->mOutput[i].wlOutput);
fei.dengaf9b07d2023-10-10 07:38:40 +0000575 self->mOutput[i].name = 0;
576 self->mOutput[i].wlOutput = NULL;
fei.deng26950832023-11-09 03:00:23 +0000577 }
578 }
579 //if current output removed, select a suiteble output
580 if (self->mCurrentDisplayOutput->wlOutput == NULL) {
581 for (i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
582 if (self->mOutput[i].wlOutput) {
583 self->mCurrentDisplayOutput = &self->mOutput[i];
584 self->mUpdateRenderRectangle = true;
585 }
586 }
587 //set new output rectangle
588 if (self->mUpdateRenderRectangle) {
589 self->mUpdateRenderRectangle = false;
590 self->setRenderRectangle(self->mCurrentDisplayOutput->offsetX,
591 self->mCurrentDisplayOutput->offsetY,
592 self->mCurrentDisplayOutput->width,
593 self->mCurrentDisplayOutput->height);
fei.dengaf9b07d2023-10-10 07:38:40 +0000594 }
595 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000596}
597
598static const struct wl_registry_listener registry_listener = {
599 WaylandDisplay::registryHandleGlobal,
600 WaylandDisplay::registryHandleGlobalRemove
601};
602
fei.dengb9a1a572023-09-13 01:33:57 +0000603WaylandDisplay::WaylandDisplay(WaylandPlugin *plugin, int logCategory)
604 :mBufferMutex("bufferMutex"),
605 mWaylandPlugin(plugin),
606 mLogCategory(logCategory)
fei.dengf7a0cd32023-08-29 09:36:37 +0000607{
fei.dengb9a1a572023-09-13 01:33:57 +0000608 TRACE(mLogCategory,"construct WaylandDisplay");
fei.dengf7a0cd32023-08-29 09:36:37 +0000609 mWlDisplay = NULL;
610 mWlDisplayWrapper = NULL;
611 mWlQueue = NULL;
612 mRegistry = NULL;
613 mCompositor = NULL;
614 mXdgWmBase = NULL;
615 mViewporter = NULL;
616 mDmabuf = NULL;
617 mShm = NULL;
618 mSeat = NULL;
619 mPointer = NULL;
620 mTouch = NULL;
621 mKeyboard = NULL;
fei.deng31f93f12024-02-27 07:52:10 +0000622 mDirect_display = NULL;
fei.deng26950832023-11-09 03:00:23 +0000623 mSelectOutputIndex = INVALID_OUTPUT_INDEX;
fei.dengb9a1a572023-09-13 01:33:57 +0000624 mPoll = new Tls::Poll(true);
fei.dengf7a0cd32023-08-29 09:36:37 +0000625 //window
626 mVideoWidth = 0;
627 mVideoHeight = 0;
628 mVideoSurface = NULL;
629 mXdgSurface = NULL;
630 mXdgToplevel = NULL;
631 mAreaViewport = NULL;
632 mVideoViewport = NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000633 mAreaShmBuffer = NULL;
634 mCommitCnt = 0;
fei.dengf7a0cd32023-08-29 09:36:37 +0000635 mAreaSurface = NULL;
636 mAreaSurfaceWrapper = NULL;
637 mVideoSurfaceWrapper = NULL;
638 mVideoSubSurface = NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000639 mPip = 0;
fei.deng640c3c92024-04-12 08:31:19 +0000640 mKeepLastFrame = 0; //default is off keep last frame
fei.deng3287c082024-04-23 09:29:22 +0000641 mSignalFirstFramePts = false;
fei.deng4029e682024-06-26 17:06:31 +0800642 mToSendKeepLastFrame = false;
fei.dengf7a0cd32023-08-29 09:36:37 +0000643}
644
645WaylandDisplay::~WaylandDisplay()
646{
fei.dengb9a1a572023-09-13 01:33:57 +0000647 TRACE(mLogCategory,"desconstruct WaylandDisplay");
648 if (mPoll) {
649 delete mPoll;
650 mPoll = NULL;
651 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000652}
653
654char *WaylandDisplay::require_xdg_runtime_dir()
655{
656 char *val = getenv("XDG_RUNTIME_DIR");
fei.dengb9a1a572023-09-13 01:33:57 +0000657 INFO(mLogCategory,"XDG_RUNTIME_DIR=%s",val);
fei.dengdd910ef2024-06-07 10:25:30 +0800658 //if not set XDG_RUNTIME_DIR,set default value
659 // if (!val) {
660 // val = const_cast<char *>("/run/user/0");
661 // setenv("XDG_RUNTIME_DIR",val,0);
662 // WARNING(mLogCategory,"XDG_RUNTIME_DIR is not set,set default %s",val);
663 // }
fei.dengf7a0cd32023-08-29 09:36:37 +0000664
665 return val;
666}
667
668int WaylandDisplay::openDisplay()
669{
fei.dengdd910ef2024-06-07 10:25:30 +0800670 mNoBorderUpdate = false;
671 mReCommitAreaSurface = false;
672 mXdgSurfaceConfigured = false;
673 mUpdateRenderRectangle = false;
674 memset(&mRenderRect, 0, sizeof(struct Rectangle));
675 memset(&mVideoRect, 0, sizeof(struct Rectangle));
676 memset(&mWindowRect, 0, sizeof(struct Rectangle));
677 mFullScreen = true; //default is full screen
678 mAmlConfig = NULL;
679 //weston config private api
680 mAmlConfigAPIList.enableDropFrame = false;
681 mAmlConfigAPIList.enableKeepLastFrame = false;
682 mAmlConfigAPIList.enableSetPts = false;
683 mAmlConfigAPIList.enableSetVideoPlane = false;
684 mCurrentDisplayOutput = &mOutput[0];
685 for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
686 mOutput[i].wlOutput = NULL;
687 mOutput[i].offsetX = 0;
688 mOutput[i].offsetY = 0;
689 mOutput[i].width = 0;
690 mOutput[i].height = 0;
691 mOutput[i].refreshRate = 0;
692 mOutput[i].isPrimary = false;
693 mOutput[i].name = 0;
694 mOutput[i].crtcIndex = 0;
695 }
fei.deng4029e682024-06-26 17:06:31 +0800696 /*if mKeepLastFrame had set but mToSendKeepLastFrame is false,maybe
697 playback is doing FF/FW action,so we keep set it on new connection*/
698 if (!mToSendKeepLastFrame && mKeepLastFrame) {
699 mToSendKeepLastFrame = true;
700 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000701 char *name = require_xdg_runtime_dir();
702 //DEBUG(mLogCategory,"name:%s",name);
fei.dengb9a1a572023-09-13 01:33:57 +0000703 DEBUG(mLogCategory,"openDisplay in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000704 mWlDisplay = wl_display_connect(NULL);
705 if (!mWlDisplay) {
fei.dengb9a1a572023-09-13 01:33:57 +0000706 ERROR(mLogCategory,"Failed to connect to the wayland display, XDG_RUNTIME_DIR='%s'",
fei.dengf7a0cd32023-08-29 09:36:37 +0000707 name ? name : "NULL");
fei.dengb9a1a572023-09-13 01:33:57 +0000708 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000709 }
710
711 mWlDisplayWrapper = (struct wl_display *)wl_proxy_create_wrapper ((void *)mWlDisplay);
712 mWlQueue = wl_display_create_queue (mWlDisplay);
713 wl_proxy_set_queue ((struct wl_proxy *)mWlDisplayWrapper, mWlQueue);
714
715 mRegistry = wl_display_get_registry (mWlDisplayWrapper);
716 wl_registry_add_listener (mRegistry, &registry_listener, (void *)this);
717
718 /* we need exactly 2 roundtrips to discover global objects and their state */
719 for (int i = 0; i < 2; i++) {
720 if (wl_display_roundtrip_queue (mWlDisplay, mWlQueue) < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000721 ERROR(mLogCategory,"Error communicating with the wayland display");
722 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000723 }
724 }
725
726 if (!mCompositor) {
fei.dengb9a1a572023-09-13 01:33:57 +0000727 ERROR(mLogCategory,"Could not bind to wl_compositor. Either it is not implemented in " \
fei.dengf7a0cd32023-08-29 09:36:37 +0000728 "the compositor, or the implemented version doesn't match");
fei.dengb9a1a572023-09-13 01:33:57 +0000729 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000730 }
731
732 if (!mDmabuf) {
fei.dengb9a1a572023-09-13 01:33:57 +0000733 ERROR(mLogCategory,"Could not bind to zwp_linux_dmabuf_v1");
734 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000735 }
736
737 if (!mXdgWmBase) {
738 /* If wl_surface and wl_display are passed via GstContext
739 * wl_shell, xdg_shell and zwp_fullscreen_shell are not used.
740 * In this case is correct to continue.
741 */
fei.dengb9a1a572023-09-13 01:33:57 +0000742 ERROR(mLogCategory,"Could not bind to either wl_shell, xdg_wm_base or "
fei.dengf7a0cd32023-08-29 09:36:37 +0000743 "zwp_fullscreen_shell, video display may not work properly.");
fei.dengb9a1a572023-09-13 01:33:57 +0000744 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000745 }
746
747 //create window surface
748 createCommonWindowSurface();
749 createXdgShellWindowSurface();
750
751 //config weston video plane
fei.deng640c3c92024-04-12 08:31:19 +0000752 if (mAmlConfigAPIList.enableSetVideoPlane) {
fei.dengb9a1a572023-09-13 01:33:57 +0000753 INFO(mLogCategory,"set weston video plane:%d",mPip);
fei.dengf7a0cd32023-08-29 09:36:37 +0000754 wl_surface_set_video_plane(mVideoSurfaceWrapper, mPip);
755 }
756
757 //run wl display queue dispatch
fei.dengb9a1a572023-09-13 01:33:57 +0000758 DEBUG(mLogCategory,"To run wl display dispatch queue");
fei.dengdd910ef2024-06-07 10:25:30 +0800759 if (mPoll) {
760 mPoll->setFlushing(false);
761 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000762 run("display queue");
763 mRedrawingPending = false;
764
fei.dengb9a1a572023-09-13 01:33:57 +0000765 DEBUG(mLogCategory,"openDisplay out");
766 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000767}
768
769void WaylandDisplay::closeDisplay()
770{
fei.dengb9a1a572023-09-13 01:33:57 +0000771 DEBUG(mLogCategory,"closeDisplay in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000772
773 if (isRunning()) {
fei.dengb9a1a572023-09-13 01:33:57 +0000774 TRACE(mLogCategory,"try stop dispatch thread");
775 if (mPoll) {
776 mPoll->setFlushing(true);
777 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000778 requestExitAndWait();
779 }
780
fei.deng4029e682024-06-26 17:06:31 +0800781 //flush pending event to weston
fei.dengdd910ef2024-06-07 10:25:30 +0800782 if (mWlDisplay) {
783 wl_display_flush (mWlDisplay);
784 }
785
fei.dengf7a0cd32023-08-29 09:36:37 +0000786 //first destroy window surface
787 destroyWindowSurfaces();
788
789 if (mViewporter) {
790 wp_viewporter_destroy (mViewporter);
791 mViewporter = NULL;
792 }
793
794 if (mDmabuf) {
795 zwp_linux_dmabuf_v1_destroy (mDmabuf);
796 mDmabuf = NULL;
797 }
798
799 if (mXdgWmBase) {
800 xdg_wm_base_destroy (mXdgWmBase);
801 mXdgWmBase = NULL;
802 }
803
804 if (mCompositor) {
805 wl_compositor_destroy (mCompositor);
806 mCompositor = NULL;
807 }
808
809 if (mSubCompositor) {
810 wl_subcompositor_destroy (mSubCompositor);
811 mSubCompositor = NULL;
812 }
813
814 if (mRegistry) {
815 wl_registry_destroy (mRegistry);
816 mRegistry= NULL;
817 }
818
819 if (mWlDisplayWrapper) {
820 wl_proxy_wrapper_destroy (mWlDisplayWrapper);
821 mWlDisplayWrapper = NULL;
822 }
823
fei.deng640c3c92024-04-12 08:31:19 +0000824 if (mAmlConfig) {
825 aml_config_destroy(mAmlConfig);
826 mAmlConfig = NULL;
827 }
828
fei.dengf7a0cd32023-08-29 09:36:37 +0000829 if (mWlQueue) {
830 wl_event_queue_destroy (mWlQueue);
831 mWlQueue = NULL;
832 }
833
834 if (mWlDisplay) {
835 wl_display_flush (mWlDisplay);
836 wl_display_disconnect (mWlDisplay);
837 mWlDisplay = NULL;
838 }
839
fei.dengb9a1a572023-09-13 01:33:57 +0000840 DEBUG(mLogCategory,"closeDisplay out");
fei.dengf7a0cd32023-08-29 09:36:37 +0000841}
842
843int WaylandDisplay::toDmaBufferFormat(RenderVideoFormat format, uint32_t *outDmaformat /*out param*/, uint64_t *outDmaformatModifiers /*out param*/)
844{
845 if (!outDmaformat || !outDmaformatModifiers) {
fei.dengb9a1a572023-09-13 01:33:57 +0000846 WARNING(mLogCategory,"NULL params");
847 return ERROR_PARAM_NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000848 }
849
850 *outDmaformat = 0;
851 *outDmaformatModifiers = 0;
852
853 uint32_t dmaformat = video_format_to_wl_dmabuf_format (format);
854 if (dmaformat == -1) {
fei.dengb9a1a572023-09-13 01:33:57 +0000855 ERROR(mLogCategory,"Error not found render video format:%d to wl dmabuf format",format);
856 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000857 }
858
fei.dengb9a1a572023-09-13 01:33:57 +0000859 TRACE(mLogCategory,"render video format:%d -> dmabuf format:%d",format,dmaformat);
fei.dengf7a0cd32023-08-29 09:36:37 +0000860 *outDmaformat = (uint32_t)dmaformat;
861
862 /*get dmaformat and modifiers*/
863 auto item = mDmaBufferFormats.find(dmaformat);
864 if (item == mDmaBufferFormats.end()) { //not found
fei.dengb9a1a572023-09-13 01:33:57 +0000865 WARNING(mLogCategory,"Not found dmabuf for render video format :%d",format);
fei.dengf7a0cd32023-08-29 09:36:37 +0000866 *outDmaformatModifiers = 0;
fei.dengb9a1a572023-09-13 01:33:57 +0000867 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000868 }
869
870 *outDmaformatModifiers = (uint64_t)item->second;
871
fei.dengb9a1a572023-09-13 01:33:57 +0000872 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000873}
874
875int WaylandDisplay::toShmBufferFormat(RenderVideoFormat format, uint32_t *outformat)
876{
877 if (!outformat) {
fei.dengb9a1a572023-09-13 01:33:57 +0000878 WARNING(mLogCategory,"NULL params");
879 return ERROR_PARAM_NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000880 }
881
882 *outformat = 0;
883
884 int shmformat = (int)video_format_to_wl_shm_format(format);
885 if (shmformat < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000886 ERROR(mLogCategory,"Error not found render video format:%d to wl shmbuf format",format);
887 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000888 }
889
890 for (auto item = mShmFormats.begin(); item != mShmFormats.end(); ++item) {
891 uint32_t registFormat = (uint32_t)*item;
892 if (registFormat == (uint32_t)shmformat) {
893 *outformat = registFormat;
fei.dengb9a1a572023-09-13 01:33:57 +0000894 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000895 }
896 }
897
fei.dengb9a1a572023-09-13 01:33:57 +0000898 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000899}
900
901void WaylandDisplay::setVideoBufferFormat(RenderVideoFormat format)
902{
fei.dengb9a1a572023-09-13 01:33:57 +0000903 TRACE(mLogCategory,"set video buffer format: %d",format);
fei.dengf7a0cd32023-08-29 09:36:37 +0000904 mBufferFormat = format;
905};
906
907void WaylandDisplay::setDisplayOutput(int output)
908{
fei.dengb9a1a572023-09-13 01:33:57 +0000909 TRACE(mLogCategory,"select display output: %d",output);
fei.dengf7a0cd32023-08-29 09:36:37 +0000910 if (output < 0 || output >= DEFAULT_DISPLAY_OUTPUT_NUM) {
fei.dengb9a1a572023-09-13 01:33:57 +0000911 ERROR(mLogCategory, "display output index error,please set 0:primary or 1:extend,now:%d",output);
fei.dengf7a0cd32023-08-29 09:36:37 +0000912 return;
913 }
fei.deng26950832023-11-09 03:00:23 +0000914 //only do select output before video playing
915 if (mSelectOutputIndex != output) {
916 mSelectOutputIndex = output;
917 // if (mOutput[output].wlOutput) {
918 // mCurrentDisplayOutput = &mOutput[output];
919 // setRenderRectangle(mOutput[output].offsetX, mOutput[output].offsetY,
920 // mOutput[output].width, mOutput[output].height);
921 // }
fei.dengf7a0cd32023-08-29 09:36:37 +0000922 }
923}
924
925int WaylandDisplay::getDisplayOutput()
926{
fei.deng26950832023-11-09 03:00:23 +0000927 return mSelectOutputIndex == INVALID_OUTPUT_INDEX? 0: mSelectOutputIndex;
fei.dengf7a0cd32023-08-29 09:36:37 +0000928}
929
930void WaylandDisplay::setPip(int pip)
931{
fei.dengb9a1a572023-09-13 01:33:57 +0000932 INFO(mLogCategory,"set pip:%d",pip);
fei.dengf7a0cd32023-08-29 09:36:37 +0000933 mPip = pip;
934}
935
fei.deng26950832023-11-09 03:00:23 +0000936void WaylandDisplay::updateDisplayOutput()
937{
938 if (!mCurrentDisplayOutput->wlOutput || !mXdgToplevel || !mXdgSurface)
939 {
940 return;
941 }
942 if (mUpdateRenderRectangle) {
943 if (mFullScreen) {
944 DEBUG(mLogCategory,"unset full screen");
945 xdg_toplevel_unset_fullscreen (mXdgToplevel);
946 }
947
948 if (mXdgSurface) {
949 DEBUG(mLogCategory,"set geometry");
950 xdg_surface_set_window_geometry(mXdgSurface,
951 mCurrentDisplayOutput->offsetX,
952 mCurrentDisplayOutput->offsetY,
953 mCurrentDisplayOutput->width,
954 mCurrentDisplayOutput->height);
955 }
956
957 if (mFullScreen && mXdgToplevel) {
958 DEBUG(mLogCategory,"set full screen");
959 xdg_toplevel_set_fullscreen (mXdgToplevel, mCurrentDisplayOutput->wlOutput);
960 }
961 setRenderRectangle(mCurrentDisplayOutput->offsetX, mCurrentDisplayOutput->offsetY,
962 mCurrentDisplayOutput->width, mCurrentDisplayOutput->height);
963 mUpdateRenderRectangle = false;
964 }
965}
966
fei.dengf7a0cd32023-08-29 09:36:37 +0000967void WaylandDisplay::createCommonWindowSurface()
968{
969 struct wl_region *region;
970
971 mAreaSurface = wl_compositor_create_surface (mCompositor);
972 mVideoSurface = wl_compositor_create_surface (mCompositor);
973 mAreaSurfaceWrapper = (struct wl_surface *)wl_proxy_create_wrapper (mAreaSurface);
974 mVideoSurfaceWrapper = (struct wl_surface *)wl_proxy_create_wrapper (mVideoSurface);
975
976 wl_proxy_set_queue ((struct wl_proxy *) mAreaSurfaceWrapper, mWlQueue);
977 wl_proxy_set_queue ((struct wl_proxy *) mVideoSurfaceWrapper, mWlQueue);
978
979 /* embed video_surface in area_surface */
980 mVideoSubSurface = wl_subcompositor_get_subsurface (mSubCompositor, mVideoSurface, mAreaSurface);
981 wl_subsurface_set_desync (mVideoSubSurface);
982
983 if (mViewporter) {
984 mAreaViewport = wp_viewporter_get_viewport (mViewporter, mAreaSurface);
985 mVideoViewport = wp_viewporter_get_viewport (mViewporter, mVideoSurface);
986 }
987
988 /* do not accept input */
989 region = wl_compositor_create_region (mCompositor);
990 wl_surface_set_input_region (mAreaSurface, region);
991 wl_region_destroy (region);
992
993 region = wl_compositor_create_region (mCompositor);
994 wl_surface_set_input_region (mVideoSurface, region);
995 wl_region_destroy (region);
996}
997
998void WaylandDisplay::createXdgShellWindowSurface()
999{
1000 /* Check which protocol we will use (in order of preference) */
1001 if (mXdgWmBase) {
1002 /* First create the XDG surface */
1003 mXdgSurface= xdg_wm_base_get_xdg_surface (mXdgWmBase, mAreaSurface);
1004 if (!mXdgSurface) {
fei.dengb9a1a572023-09-13 01:33:57 +00001005 ERROR(mLogCategory,"Unable to get xdg_surface");
fei.dengf7a0cd32023-08-29 09:36:37 +00001006 return;
1007 }
1008 xdg_surface_add_listener (mXdgSurface, &xdg_surface_listener,(void *)this);
1009
1010 /* Then the toplevel */
1011 mXdgToplevel= xdg_surface_get_toplevel (mXdgSurface);
1012 if (!mXdgSurface) {
fei.dengb9a1a572023-09-13 01:33:57 +00001013 ERROR(mLogCategory,"Unable to get xdg_toplevel");
fei.dengf7a0cd32023-08-29 09:36:37 +00001014 return;
1015 }
1016 xdg_toplevel_add_listener (mXdgToplevel, &xdg_toplevel_listener, this);
1017
1018 /* Finally, commit the xdg_surface state as toplevel */
1019 mXdgSurfaceConfigured = false;
1020 wl_surface_commit (mAreaSurface);
1021 wl_display_flush (mWlDisplay);
1022 /* we need exactly 3 roundtrips to discover global objects and their state */
1023 for (int i = 0; i < 3; i++) {
1024 if (wl_display_roundtrip_queue(mWlDisplay, mWlQueue) < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +00001025 ERROR(mLogCategory,"Error communicating with the wayland display");
fei.dengf7a0cd32023-08-29 09:36:37 +00001026 }
1027 }
1028
1029 if (mXdgSurfaceConfigured) {
fei.dengb9a1a572023-09-13 01:33:57 +00001030 INFO(mLogCategory,"xdg surface had configured");
fei.dengf7a0cd32023-08-29 09:36:37 +00001031 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001032 WARNING(mLogCategory,"xdg surface not configured");
fei.dengf7a0cd32023-08-29 09:36:37 +00001033 }
1034
1035 //full screen show
fei.deng26950832023-11-09 03:00:23 +00001036 // if (mFullScreen && mCurrentDisplayOutput->wlOutput) {
1037 // //ensureFullscreen(mFullScreen);
fei.dengf7a0cd32023-08-29 09:36:37 +00001038 // }
1039 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001040 ERROR(mLogCategory,"Unable to use xdg_wm_base ");
fei.dengf7a0cd32023-08-29 09:36:37 +00001041 return;
1042 }
1043}
1044
1045void WaylandDisplay::destroyWindowSurfaces()
1046{
1047 if (mAreaShmBuffer) {
1048 delete mAreaShmBuffer;
1049 mAreaShmBuffer = NULL;
1050 }
1051
1052 //clean all wayland buffers
1053 cleanAllWaylandBuffer();
1054
1055 if (mXdgToplevel) {
1056 xdg_toplevel_destroy (mXdgToplevel);
1057 mXdgToplevel = NULL;
1058 }
1059
1060 if (mXdgSurface) {
1061 xdg_surface_destroy (mXdgSurface);
1062 mXdgSurface = NULL;
1063 }
1064
1065 if (mVideoSurfaceWrapper) {
1066 wl_proxy_wrapper_destroy (mVideoSurfaceWrapper);
1067 mVideoSurfaceWrapper = NULL;
1068 }
1069
1070 if (mVideoSubSurface) {
1071 wl_subsurface_destroy (mVideoSubSurface);
1072 mVideoSubSurface = NULL;
1073 }
1074
1075 if (mVideoSurface) {
1076 wl_surface_destroy (mVideoSurface);
1077 mVideoSurface = NULL;
1078 }
1079
1080 if (mAreaSurfaceWrapper) {
1081 wl_proxy_wrapper_destroy (mAreaSurfaceWrapper);
1082 mAreaSurfaceWrapper = NULL;
1083 }
1084
1085 if (mAreaSurface) {
1086 wl_surface_destroy (mAreaSurface);
1087 mAreaSurface = NULL;
1088 mReCommitAreaSurface = false;
1089 }
1090}
1091
1092void WaylandDisplay::ensureFullscreen(bool fullscreen)
1093{
1094 if (mXdgWmBase) {
fei.dengb9a1a572023-09-13 01:33:57 +00001095 DEBUG(mLogCategory,"full screen : %d",fullscreen);
fei.dengf7a0cd32023-08-29 09:36:37 +00001096 if (fullscreen) {
fei.deng26950832023-11-09 03:00:23 +00001097 xdg_toplevel_set_fullscreen (mXdgToplevel, mCurrentDisplayOutput->wlOutput);
fei.dengf7a0cd32023-08-29 09:36:37 +00001098 } else {
1099 xdg_toplevel_unset_fullscreen (mXdgToplevel);
1100 }
1101 }
1102}
1103
1104void WaylandDisplay::setRenderRectangle(int x, int y, int w, int h)
1105{
fei.dengb9a1a572023-09-13 01:33:57 +00001106 DEBUG(mLogCategory,"set render rect:x:%d,y:%d,w:%d,h:%d",x,y,w,h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001107
1108 if (w <= 0 || h <= 0) {
fei.dengb9a1a572023-09-13 01:33:57 +00001109 WARNING(mLogCategory, "wrong render width or height %dx%d",w,h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001110 return;
1111 }
1112
1113 mRenderRect.x = x;
1114 mRenderRect.y = y;
1115 mRenderRect.w = w;
1116 mRenderRect.h = h;
1117
1118 if (!mXdgSurfaceConfigured) {
fei.dengb9a1a572023-09-13 01:33:57 +00001119 WARNING(mLogCategory,"Not configured xdg");
fei.dengf7a0cd32023-08-29 09:36:37 +00001120 return;
1121 }
1122
1123 if (mAreaViewport) {
1124 wp_viewport_set_destination (mAreaViewport, w, h);
1125 }
1126
1127 updateBorders();
1128
1129 if (mVideoWidth != 0 && mVideoSurface) {
1130 wl_subsurface_set_sync (mVideoSubSurface);
1131 resizeVideoSurface(true);
1132 }
1133
1134 wl_surface_damage (mAreaSurfaceWrapper, 0, 0, w, h);
1135 wl_surface_commit (mAreaSurfaceWrapper);
1136
1137 if (mVideoWidth != 0) {
1138 wl_subsurface_set_desync (mVideoSubSurface);
1139 }
1140}
1141
1142
1143void WaylandDisplay::setFrameSize(int w, int h)
1144{
1145 mVideoWidth = w;
1146 mVideoHeight = h;
fei.dengb9a1a572023-09-13 01:33:57 +00001147 TRACE(mLogCategory,"frame w:%d,h:%d",mVideoWidth,mVideoHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +00001148 if (mRenderRect.w > 0 && mVideoSurface) {
1149 resizeVideoSurface(true);
1150 }
1151}
1152
1153void WaylandDisplay::setWindowSize(int x, int y, int w, int h)
1154{
1155 mWindowRect.x = x;
1156 mWindowRect.y = y;
1157 mWindowRect.w = w;
1158 mWindowRect.h = h;
fei.dengb9a1a572023-09-13 01:33:57 +00001159 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 +00001160 if (mWindowRect.w > 0 && mVideoWidth > 0 && mVideoSurface) {
1161 //if had full screen, unset it and set window size
1162 if (mFullScreen) {
1163 mFullScreen = false;
1164 ensureFullscreen(mFullScreen);
1165 }
1166 resizeVideoSurface(true);
1167 }
1168}
1169
1170
1171void WaylandDisplay::resizeVideoSurface(bool commit)
1172{
1173 Rectangle src = {0,};
1174 Rectangle dst = {0,};
1175 Rectangle res;
1176
1177 /* center the video_subsurface inside area_subsurface */
1178 src.w = mVideoWidth;
1179 src.h = mVideoHeight;
1180 /*if had set the window size, we will scall
1181 video surface to this window size*/
1182 if (mWindowRect.w > 0 && mWindowRect.h > 0) {
1183 dst.x = mWindowRect.x;
1184 dst.y = mWindowRect.y;
1185 dst.w = mWindowRect.w;
1186 dst.h = mWindowRect.h;
1187 if (mWindowRect.w > mRenderRect.w && mWindowRect.h > mRenderRect.h) {
fei.dengb9a1a572023-09-13 01:33:57 +00001188 WARNING(mLogCategory,"Error window size:%dx%d, but render size:%dx%d,reset to render size",
fei.dengf7a0cd32023-08-29 09:36:37 +00001189 mWindowRect.w,mWindowRect.h,mRenderRect.w,mRenderRect.h);
1190 dst.x = mRenderRect.x;
1191 dst.y = mRenderRect.y;
1192 dst.w = mRenderRect.w;
1193 dst.h = mRenderRect.h;
1194 }
1195 //to do,we need set geometry?
1196 //if (mXdgSurface) {
1197 // xdg_surface_set_window_geometry(mXdgSurface, mWindowRect.x, mWindowRect.y, mWindowRect.w, mWindowRect.h);
1198 //}
1199 } else { //scal video to full screen
1200 dst.w = mRenderRect.w;
1201 dst.h = mRenderRect.h;
1202 }
1203
1204 if (mViewporter) {
1205 videoCenterRect(src, dst, &res, true);
1206 } else {
1207 videoCenterRect(src, dst, &res, false);
1208 }
1209
1210 wl_subsurface_set_position (mVideoSubSurface, res.x, res.y);
1211
1212 if (commit) {
1213 wl_surface_damage (mVideoSurfaceWrapper, 0, 0, res.w, res.h);
1214 wl_surface_commit (mVideoSurfaceWrapper);
1215 }
1216
1217 //top level setting
1218 if (mXdgToplevel) {
1219 struct wl_region *region;
1220
1221 region = wl_compositor_create_region (mCompositor);
1222 wl_region_add (region, 0, 0, mRenderRect.w, mRenderRect.h);
1223 wl_surface_set_input_region (mAreaSurface, region);
1224 wl_region_destroy (region);
1225 }
1226
1227 /* this is saved for use in wl_surface_damage */
1228 mVideoRect.x = res.x;
1229 mVideoRect.y = res.y;
1230 mVideoRect.w = res.w;
1231 mVideoRect.h = res.h;
1232
1233 //to scale video surface to full screen
1234 wp_viewport_set_destination(mVideoViewport, res.w, res.h);
fei.dengb9a1a572023-09-13 01:33:57 +00001235 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 +00001236}
1237
1238void WaylandDisplay::setOpaque()
1239{
1240 struct wl_region *region;
1241
1242 /* Set area opaque */
1243 region = wl_compositor_create_region (mCompositor);
1244 wl_region_add (region, 0, 0, mRenderRect.w, mRenderRect.h);
1245 wl_surface_set_opaque_region (mAreaSurface, region);
1246 wl_region_destroy (region);
1247}
1248
1249int WaylandDisplay::prepareFrameBuffer(RenderBuffer * buf)
1250{
1251 WaylandBuffer *waylandBuf = NULL;
1252 int ret;
1253
fei.dengdd910ef2024-06-07 10:25:30 +08001254 if (!mDmabuf)
1255 {
1256 ERROR(mLogCategory,"Error zwp_linux_dmabuf_v1");
1257 return ERROR_UNKNOWN;
1258 }
1259
fei.dengf7a0cd32023-08-29 09:36:37 +00001260 waylandBuf = findWaylandBuffer(buf);
1261 if (waylandBuf == NULL) {
fei.dengb9a1a572023-09-13 01:33:57 +00001262 waylandBuf = new WaylandBuffer(this, mLogCategory);
fei.dengf7a0cd32023-08-29 09:36:37 +00001263 waylandBuf->setBufferFormat(mBufferFormat);
1264 ret = waylandBuf->constructWlBuffer(buf);
fei.dengb9a1a572023-09-13 01:33:57 +00001265 if (ret != NO_ERROR) {
1266 WARNING(mLogCategory,"dmabufConstructWlBuffer fail,release waylandbuf");
fei.dengf7a0cd32023-08-29 09:36:37 +00001267 //delete waylanBuf,WaylandBuffer object destruct will call release callback
1268 goto waylandbuf_fail;
1269 } else {
1270 addWaylandBuffer(buf, waylandBuf);
1271 }
1272 }
fei.dengb9a1a572023-09-13 01:33:57 +00001273 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +00001274waylandbuf_fail:
1275 //delete waylandbuf
1276 delete waylandBuf;
1277 waylandBuf = NULL;
fei.dengb9a1a572023-09-13 01:33:57 +00001278 return ERROR_UNKNOWN;
fei.dengf7a0cd32023-08-29 09:36:37 +00001279}
1280
1281void WaylandDisplay::displayFrameBuffer(RenderBuffer * buf, int64_t realDisplayTime)
1282{
1283 WaylandBuffer *waylandBuf = NULL;
1284 struct wl_buffer * wlbuffer = NULL;
1285 int ret;
1286
1287 if (!buf) {
fei.dengb9a1a572023-09-13 01:33:57 +00001288 ERROR(mLogCategory,"Error input params, waylandbuffer is null");
fei.dengf7a0cd32023-08-29 09:36:37 +00001289 return;
1290 }
1291
1292 //must commit areasurface first, because weston xdg surface maybe timeout
1293 //this cause video is not display,commit can resume xdg surface
1294 if (!mReCommitAreaSurface) {
1295 mReCommitAreaSurface = true;
1296 wl_surface_commit (mAreaSurface);
1297 }
1298
fei.dengdd910ef2024-06-07 10:25:30 +08001299 //TRACE(mLogCategory,"display renderBuffer:%p,PTS:%lld us,realtime:%lld",buf, buf->pts/1000, realDisplayTime);
fei.dengf7a0cd32023-08-29 09:36:37 +00001300
1301 if (buf->flag & BUFFER_FLAG_DMA_BUFFER) {
1302 if (buf->dma.width <=0 || buf->dma.height <=0) {
1303 buf->dma.width = mVideoWidth;
1304 buf->dma.height = mVideoHeight;
1305 }
1306 waylandBuf = findWaylandBuffer(buf);
1307 if (waylandBuf) {
1308 waylandBuf->setRenderRealTime(realDisplayTime);
1309 ret = waylandBuf->constructWlBuffer(buf);
fei.dengb9a1a572023-09-13 01:33:57 +00001310 if (ret != NO_ERROR) {
1311 WARNING(mLogCategory,"dmabufConstructWlBuffer fail,release waylandbuf");
fei.dengf7a0cd32023-08-29 09:36:37 +00001312 //delete waylanBuf,WaylandBuffer object destruct will call release callback
1313 goto waylandbuf_fail;
1314 }
1315 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001316 ERROR(mLogCategory,"NOT found wayland buffer,please prepare buffer first");
fei.dengf7a0cd32023-08-29 09:36:37 +00001317 goto waylandbuf_fail;
1318 }
1319 }
1320
1321 if (waylandBuf) {
1322 wlbuffer = waylandBuf->getWlBuffer();
1323 }
fei.deng1cfb2752023-10-26 08:01:25 +00001324 //if no wl_output, drop this buffer
fei.deng26950832023-11-09 03:00:23 +00001325 if (mCurrentDisplayOutput->wlOutput == NULL) {
fei.deng1cfb2752023-10-26 08:01:25 +00001326 TRACE(mLogCategory,"No wl_output");
1327 mWaylandPlugin->handleFrameDropped(buf);
1328 mWaylandPlugin->handleBufferRelease(buf);
1329 return;
1330 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001331 if (wlbuffer) {
fei.dengb9a1a572023-09-13 01:33:57 +00001332 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001333 ++mCommitCnt;
1334 uint32_t hiPts = realDisplayTime >> 32;
1335 uint32_t lowPts = realDisplayTime & 0xFFFFFFFF;
1336 //attach this wl_buffer to weston
fei.dengdd910ef2024-06-07 10:25:30 +08001337 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 +00001338 waylandBuf->attach(mVideoSurfaceWrapper);
1339
fei.deng640c3c92024-04-12 08:31:19 +00001340 if (mAmlConfigAPIList.enableSetPts) {
fei.dengb9a1a572023-09-13 01:33:57 +00001341 TRACE(mLogCategory,"display time:%lld,hiPts:%u,lowPts:%u",realDisplayTime, hiPts, lowPts);
fei.dengf7a0cd32023-08-29 09:36:37 +00001342 wl_surface_set_pts(mVideoSurfaceWrapper, hiPts, lowPts);
1343 }
1344
1345 wl_surface_damage (mVideoSurfaceWrapper, 0, 0, mVideoRect.w, mVideoRect.h);
1346 wl_surface_commit (mVideoSurfaceWrapper);
1347 //insert this buffer to committed weston buffer manager
fei.dengae8c90a2024-06-27 13:39:53 +08001348 std::pair<int64_t, WaylandBuffer *> item(realDisplayTime, waylandBuf);
fei.dengf7a0cd32023-08-29 09:36:37 +00001349 mCommittedBufferMap.insert(item);
1350 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001351 WARNING(mLogCategory,"wlbuffer is NULL");
fei.dengf7a0cd32023-08-29 09:36:37 +00001352 /* clear both video and parent surfaces */
1353 cleanSurface();
1354 }
1355
1356 wl_display_flush (mWlDisplay);
fei.deng4029e682024-06-26 17:06:31 +08001357 //set keep last frame or not when after send first buffer to weston,1 keep last frame, 0 not
1358 if (mToSendKeepLastFrame) {
1359 setKeepLastFrame(mKeepLastFrame);
1360 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001361
1362 return;
1363waylandbuf_fail:
1364 //notify dropped
1365 mWaylandPlugin->handleFrameDropped(buf);
1366 //notify app release this buf
1367 mWaylandPlugin->handleBufferRelease(buf);
1368 //delete waylandbuf
1369 delete waylandBuf;
1370 waylandBuf = NULL;
1371 return;
1372}
1373
1374void WaylandDisplay::handleBufferReleaseCallback(WaylandBuffer *buf)
1375{
1376 {
fei.dengb9a1a572023-09-13 01:33:57 +00001377 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001378 //remove buffer if this buffer is ready to release
1379 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengae8c90a2024-06-27 13:39:53 +08001380 auto item = mCommittedBufferMap.find(buf->getRenderRealTime());
fei.dengf7a0cd32023-08-29 09:36:37 +00001381 if (item != mCommittedBufferMap.end()) {
fei.dengdd910ef2024-06-07 10:25:30 +08001382 --mCommitCnt;
fei.dengf7a0cd32023-08-29 09:36:37 +00001383 mCommittedBufferMap.erase(item);
1384 } else {
fei.dengdd910ef2024-06-07 10:25:30 +08001385 WARNING(mLogCategory,"Can't find WaylandBuffer pts:%lld us in buffer map",renderBuffer->pts/1000);
fei.dengf7a0cd32023-08-29 09:36:37 +00001386 return;
1387 }
1388 }
1389 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengdd910ef2024-06-07 10:25:30 +08001390 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 +00001391 mWaylandPlugin->handleBufferRelease(renderBuffer);
1392}
1393
1394void WaylandDisplay::handleFrameDisplayedCallback(WaylandBuffer *buf)
1395{
1396 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengb9a1a572023-09-13 01:33:57 +00001397 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 +00001398 if (!mSignalFirstFramePts) {
1399 mSignalFirstFramePts = true;
1400 mWaylandPlugin->handleMsgNotify(MSG_FIRST_FRAME, (void*)&renderBuffer->pts);
1401 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001402 mWaylandPlugin->handleFrameDisplayed(renderBuffer);
1403}
1404
1405void WaylandDisplay::handleFrameDropedCallback(WaylandBuffer *buf)
1406{
1407 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengb9a1a572023-09-13 01:33:57 +00001408 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 +00001409 mWaylandPlugin->handleFrameDropped(renderBuffer);
1410}
1411
1412
1413void WaylandDisplay::readyToRun()
1414{
1415 mFd = wl_display_get_fd (mWlDisplay);
fei.dengb9a1a572023-09-13 01:33:57 +00001416 if (mPoll) {
1417 mPoll->addFd(mFd);
1418 mPoll->setFdReadable(mFd, true);
1419 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001420}
1421
fei.dengdd910ef2024-06-07 10:25:30 +08001422void WaylandDisplay::readyToExit()
1423{
1424 if (mPoll && mFd >= 0) {
1425 mPoll->removeFd(mFd);
1426 }
1427}
1428
fei.dengf7a0cd32023-08-29 09:36:37 +00001429bool WaylandDisplay::threadLoop()
1430{
1431 int ret;
fei.dengf7a0cd32023-08-29 09:36:37 +00001432
1433 while (wl_display_prepare_read_queue (mWlDisplay, mWlQueue) != 0) {
1434 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1435 }
1436
1437 wl_display_flush (mWlDisplay);
1438
1439 /*poll timeout value must > 300 ms,otherwise zwp_linux_dmabuf will create failed,
1440 so do use -1 to wait for ever*/
fei.dengb9a1a572023-09-13 01:33:57 +00001441 ret = mPoll->wait(-1); //wait for ever
fei.dengf7a0cd32023-08-29 09:36:37 +00001442 if (ret < 0) { //poll error
fei.dengb9a1a572023-09-13 01:33:57 +00001443 WARNING(mLogCategory,"poll error");
fei.dengf7a0cd32023-08-29 09:36:37 +00001444 wl_display_cancel_read(mWlDisplay);
1445 return false;
1446 } else if (ret == 0) { //poll time out
1447 return true; //run loop
1448 }
1449
1450 if (wl_display_read_events (mWlDisplay) == -1) {
1451 goto tag_error;
1452 }
1453
1454 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1455 return true;
1456tag_error:
fei.dengb9a1a572023-09-13 01:33:57 +00001457 ERROR(mLogCategory,"Error communicating with the wayland server");
fei.dengf7a0cd32023-08-29 09:36:37 +00001458 return false;
1459}
1460
1461void WaylandDisplay::videoCenterRect(Rectangle src, Rectangle dst, Rectangle *result, bool scaling)
1462{
1463 //if dst is a small window, we scale video to map window size,don't doing center
1464 if (mRenderRect.w != dst.w && mRenderRect.h != dst.h) {
1465 result->x = dst.x;
1466 result->y = dst.y;
1467 result->w = dst.w;
1468 result->h = dst.h;
fei.dengb9a1a572023-09-13 01:33:57 +00001469 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 +00001470 src.w, src.h, dst.w, dst.h, result->w, result->h, result->x, result->y);
1471 return;
1472 }
1473 if (!scaling) {
1474 result->w = MIN (src.w, dst.w);
1475 result->h = MIN (src.h, dst.h);
1476 result->x = dst.x + (dst.w - result->w) / 2;
1477 result->y = dst.y + (dst.h - result->h) / 2;
1478 } else {
1479 double src_ratio, dst_ratio;
1480
1481 src_ratio = (double) src.w / src.h;
1482 dst_ratio = (double) dst.w / dst.h;
1483
1484 if (src_ratio > dst_ratio) {
1485 result->w = dst.w;
1486 result->h = dst.w / src_ratio;
1487 result->x = dst.x;
1488 result->y = dst.y + (dst.h - result->h) / 2;
1489 } else if (src_ratio < dst_ratio) {
1490 result->w = dst.h * src_ratio;
1491 result->h = dst.h;
1492 result->x = dst.x + (dst.w - result->w) / 2;
1493 result->y = dst.y;
1494 } else {
1495 result->x = dst.x;
1496 result->y = dst.y;
1497 result->w = dst.w;
1498 result->h = dst.h;
1499 }
1500 }
1501
fei.dengb9a1a572023-09-13 01:33:57 +00001502 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 +00001503 src.w, src.h, dst.w, dst.h, result->w, result->h, result->x, result->y);
1504}
1505
1506void WaylandDisplay::updateBorders()
1507{
1508 int width,height;
1509
1510 if (mNoBorderUpdate)
1511 return;
1512
1513 if (mViewporter) {
1514 width = height = 1;
1515 mNoBorderUpdate = true;
1516 } else {
1517 width = mRenderRect.w;
1518 height = mRenderRect.h;
1519 }
1520
1521 RenderVideoFormat format = VIDEO_FORMAT_BGRA;
fei.dengb9a1a572023-09-13 01:33:57 +00001522 mAreaShmBuffer = new WaylandShmBuffer(this, mLogCategory);
fei.dengf7a0cd32023-08-29 09:36:37 +00001523 struct wl_buffer *wlbuf = mAreaShmBuffer->constructWlBuffer(width, height, format);
1524 if (wlbuf == NULL) {
1525 delete mAreaShmBuffer;
1526 mAreaShmBuffer = NULL;
1527 }
1528
1529 wl_surface_attach(mAreaSurfaceWrapper, wlbuf, 0, 0);
1530}
1531
1532std::size_t WaylandDisplay::calculateDmaBufferHash(RenderDmaBuffer &dmabuf)
1533{
1534 std::string hashString("");
1535 for (int i = 0; i < dmabuf.planeCnt; i++) {
1536 char hashtmp[1024];
1537 snprintf (hashtmp, 1024, "%d%d%d%d%d%d", dmabuf.width,dmabuf.height,dmabuf.planeCnt,
1538 dmabuf.stride[i],dmabuf.offset[i],dmabuf.fd[i]);
1539 std::string tmp(hashtmp);
1540 hashString += tmp;
1541 }
1542
1543 std::size_t hashval = std::hash<std::string>()(hashString);
fei.dengb9a1a572023-09-13 01:33:57 +00001544 //TRACE(mLogCategory,"hashstr:%s,val:%zu",hashString.c_str(),hashval);
fei.dengf7a0cd32023-08-29 09:36:37 +00001545 return hashval;
1546}
1547
1548void WaylandDisplay::addWaylandBuffer(RenderBuffer * buf, WaylandBuffer *waylandbuf)
1549{
1550 if (buf->flag & BUFFER_FLAG_DMA_BUFFER) {
1551 std::size_t hashval = calculateDmaBufferHash(buf->dma);
1552 std::pair<std::size_t, WaylandBuffer *> item(hashval, waylandbuf);
fei.dengb9a1a572023-09-13 01:33:57 +00001553 //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 +00001554 mWaylandBuffersMap.insert(item);
1555 }
1556
1557 //clean invalid wayland buffer,if video resolution changed
1558 for (auto item = mWaylandBuffersMap.begin(); item != mWaylandBuffersMap.end(); ) {
1559 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1560 if ((waylandbuf->getFrameWidth() != buf->dma.width ||
1561 waylandbuf->getFrameHeight() != buf->dma.height) &&
1562 waylandbuf->isFree()) {
fei.dengb9a1a572023-09-13 01:33:57 +00001563 TRACE(mLogCategory,"delete wayland buffer,width:%d(%d),height:%d(%d)",
fei.dengf7a0cd32023-08-29 09:36:37 +00001564 waylandbuf->getFrameWidth(),buf->dma.width,
1565 waylandbuf->getFrameHeight(),buf->dma.height);
1566 mWaylandBuffersMap.erase(item++);
1567 delete waylandbuf;
1568 } else {
1569 item++;
1570 }
1571 }
fei.dengb9a1a572023-09-13 01:33:57 +00001572 TRACE(mLogCategory,"mWaylandBuffersMap size:%d",mWaylandBuffersMap.size());
fei.dengf7a0cd32023-08-29 09:36:37 +00001573}
1574
1575WaylandBuffer* WaylandDisplay::findWaylandBuffer(RenderBuffer * buf)
1576{
1577 std::size_t hashval = calculateDmaBufferHash(buf->dma);
1578 auto item = mWaylandBuffersMap.find(hashval);
1579 if (item == mWaylandBuffersMap.end()) {
1580 return NULL;
1581 }
1582
1583 return (WaylandBuffer*) item->second;
1584}
1585
1586void WaylandDisplay::cleanAllWaylandBuffer()
1587{
1588 //free all obtain buff
1589 for (auto item = mWaylandBuffersMap.begin(); item != mWaylandBuffersMap.end(); ) {
1590 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1591 mWaylandBuffersMap.erase(item++);
1592 delete waylandbuf;
1593 }
1594}
1595
1596void WaylandDisplay::flushBuffers()
1597{
fei.dengb9a1a572023-09-13 01:33:57 +00001598 INFO(mLogCategory,"flushBuffers");
1599 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001600 for (auto item = mCommittedBufferMap.begin(); item != mCommittedBufferMap.end(); item++) {
1601 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1602 waylandbuf->forceRedrawing();
1603 handleFrameDisplayedCallback(waylandbuf);
1604 }
1605}
1606
1607void WaylandDisplay::cleanSurface()
1608{
1609 /* clear both video and parent surfaces */
1610 wl_surface_attach (mVideoSurfaceWrapper, NULL, 0, 0);
1611 wl_surface_commit (mVideoSurfaceWrapper);
1612 wl_surface_attach (mAreaSurfaceWrapper, NULL, 0, 0);
1613 wl_surface_commit (mAreaSurfaceWrapper);
fei.deng640c3c92024-04-12 08:31:19 +00001614}
1615
1616void WaylandDisplay::setKeepLastFrame(int keep)
1617{
1618 mKeepLastFrame = keep;
1619 if (mVideoSurfaceWrapper && mAmlConfigAPIList.enableKeepLastFrame) {
1620 INFO(mLogCategory,"keep last frame:%d",keep);
1621 wl_surface_keep_last_frame(mVideoSurfaceWrapper, keep);
fei.deng4029e682024-06-26 17:06:31 +08001622 mToSendKeepLastFrame = false;
1623 return;
fei.deng640c3c92024-04-12 08:31:19 +00001624 }
fei.deng4029e682024-06-26 17:06:31 +08001625 mToSendKeepLastFrame = true;
fei.dengf7a0cd32023-08-29 09:36:37 +00001626}