blob: 8e0afd0c3f16f044e241b2a28501183c3083f4e0 [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;
633 mNoBorderUpdate = false;
634 mAreaShmBuffer = NULL;
635 mCommitCnt = 0;
fei.dengf7a0cd32023-08-29 09:36:37 +0000636 mReCommitAreaSurface = false;
637 mAreaSurface = NULL;
638 mAreaSurfaceWrapper = NULL;
639 mVideoSurfaceWrapper = NULL;
640 mVideoSubSurface = NULL;
641 mXdgSurfaceConfigured = false;
642 mPip = 0;
fei.deng26950832023-11-09 03:00:23 +0000643 mCurrentDisplayOutput = &mOutput[0];
644 mUpdateRenderRectangle = false;
fei.dengf7a0cd32023-08-29 09:36:37 +0000645 memset(&mRenderRect, 0, sizeof(struct Rectangle));
646 memset(&mVideoRect, 0, sizeof(struct Rectangle));
647 memset(&mWindowRect, 0, sizeof(struct Rectangle));
648 mFullScreen = true; //default is full screen
fei.deng640c3c92024-04-12 08:31:19 +0000649 mAmlConfig = NULL;
650 mKeepLastFrame = 0; //default is off keep last frame
fei.deng3287c082024-04-23 09:29:22 +0000651 mSignalFirstFramePts = false;
fei.deng640c3c92024-04-12 08:31:19 +0000652 //weston config private api
653 mAmlConfigAPIList.enableDropFrame = false;
654 mAmlConfigAPIList.enableKeepLastFrame = false;
655 mAmlConfigAPIList.enableSetPts = false;
656 mAmlConfigAPIList.enableSetVideoPlane = false;
657
fei.dengf7a0cd32023-08-29 09:36:37 +0000658 for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
659 mOutput[i].wlOutput = NULL;
660 mOutput[i].offsetX = 0;
661 mOutput[i].offsetY = 0;
662 mOutput[i].width = 0;
663 mOutput[i].height = 0;
664 mOutput[i].refreshRate = 0;
665 mOutput[i].isPrimary = false;
fei.dengaf9b07d2023-10-10 07:38:40 +0000666 mOutput[i].name = 0;
fei.deng75e8d392024-01-04 05:36:14 +0000667 mOutput[i].crtcIndex = 0;
fei.dengf7a0cd32023-08-29 09:36:37 +0000668 }
669}
670
671WaylandDisplay::~WaylandDisplay()
672{
fei.dengb9a1a572023-09-13 01:33:57 +0000673 TRACE(mLogCategory,"desconstruct WaylandDisplay");
674 if (mPoll) {
675 delete mPoll;
676 mPoll = NULL;
677 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000678}
679
680char *WaylandDisplay::require_xdg_runtime_dir()
681{
682 char *val = getenv("XDG_RUNTIME_DIR");
fei.dengb9a1a572023-09-13 01:33:57 +0000683 INFO(mLogCategory,"XDG_RUNTIME_DIR=%s",val);
fei.dengf7a0cd32023-08-29 09:36:37 +0000684
685 return val;
686}
687
688int WaylandDisplay::openDisplay()
689{
690 char *name = require_xdg_runtime_dir();
691 //DEBUG(mLogCategory,"name:%s",name);
fei.dengb9a1a572023-09-13 01:33:57 +0000692 DEBUG(mLogCategory,"openDisplay in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000693 mWlDisplay = wl_display_connect(NULL);
694 if (!mWlDisplay) {
fei.dengb9a1a572023-09-13 01:33:57 +0000695 ERROR(mLogCategory,"Failed to connect to the wayland display, XDG_RUNTIME_DIR='%s'",
fei.dengf7a0cd32023-08-29 09:36:37 +0000696 name ? name : "NULL");
fei.dengb9a1a572023-09-13 01:33:57 +0000697 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000698 }
699
700 mWlDisplayWrapper = (struct wl_display *)wl_proxy_create_wrapper ((void *)mWlDisplay);
701 mWlQueue = wl_display_create_queue (mWlDisplay);
702 wl_proxy_set_queue ((struct wl_proxy *)mWlDisplayWrapper, mWlQueue);
703
704 mRegistry = wl_display_get_registry (mWlDisplayWrapper);
705 wl_registry_add_listener (mRegistry, &registry_listener, (void *)this);
706
707 /* we need exactly 2 roundtrips to discover global objects and their state */
708 for (int i = 0; i < 2; i++) {
709 if (wl_display_roundtrip_queue (mWlDisplay, mWlQueue) < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000710 ERROR(mLogCategory,"Error communicating with the wayland display");
711 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000712 }
713 }
714
715 if (!mCompositor) {
fei.dengb9a1a572023-09-13 01:33:57 +0000716 ERROR(mLogCategory,"Could not bind to wl_compositor. Either it is not implemented in " \
fei.dengf7a0cd32023-08-29 09:36:37 +0000717 "the compositor, or the implemented version doesn't match");
fei.dengb9a1a572023-09-13 01:33:57 +0000718 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000719 }
720
721 if (!mDmabuf) {
fei.dengb9a1a572023-09-13 01:33:57 +0000722 ERROR(mLogCategory,"Could not bind to zwp_linux_dmabuf_v1");
723 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000724 }
725
726 if (!mXdgWmBase) {
727 /* If wl_surface and wl_display are passed via GstContext
728 * wl_shell, xdg_shell and zwp_fullscreen_shell are not used.
729 * In this case is correct to continue.
730 */
fei.dengb9a1a572023-09-13 01:33:57 +0000731 ERROR(mLogCategory,"Could not bind to either wl_shell, xdg_wm_base or "
fei.dengf7a0cd32023-08-29 09:36:37 +0000732 "zwp_fullscreen_shell, video display may not work properly.");
fei.dengb9a1a572023-09-13 01:33:57 +0000733 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000734 }
735
736 //create window surface
737 createCommonWindowSurface();
738 createXdgShellWindowSurface();
739
740 //config weston video plane
fei.deng640c3c92024-04-12 08:31:19 +0000741 if (mAmlConfigAPIList.enableSetVideoPlane) {
fei.dengb9a1a572023-09-13 01:33:57 +0000742 INFO(mLogCategory,"set weston video plane:%d",mPip);
fei.dengf7a0cd32023-08-29 09:36:37 +0000743 wl_surface_set_video_plane(mVideoSurfaceWrapper, mPip);
744 }
745
fei.deng640c3c92024-04-12 08:31:19 +0000746 if (mKeepLastFrame) {
747 setKeepLastFrame(mKeepLastFrame);
748 }
749
fei.dengf7a0cd32023-08-29 09:36:37 +0000750 //run wl display queue dispatch
fei.dengb9a1a572023-09-13 01:33:57 +0000751 DEBUG(mLogCategory,"To run wl display dispatch queue");
fei.dengf7a0cd32023-08-29 09:36:37 +0000752 run("display queue");
753 mRedrawingPending = false;
754
fei.dengb9a1a572023-09-13 01:33:57 +0000755 DEBUG(mLogCategory,"openDisplay out");
756 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000757}
758
759void WaylandDisplay::closeDisplay()
760{
fei.dengb9a1a572023-09-13 01:33:57 +0000761 DEBUG(mLogCategory,"closeDisplay in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000762
763 if (isRunning()) {
fei.dengb9a1a572023-09-13 01:33:57 +0000764 TRACE(mLogCategory,"try stop dispatch thread");
765 if (mPoll) {
766 mPoll->setFlushing(true);
767 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000768 requestExitAndWait();
769 }
770
771 //first destroy window surface
772 destroyWindowSurfaces();
773
774 if (mViewporter) {
775 wp_viewporter_destroy (mViewporter);
776 mViewporter = NULL;
777 }
778
779 if (mDmabuf) {
780 zwp_linux_dmabuf_v1_destroy (mDmabuf);
781 mDmabuf = NULL;
782 }
783
784 if (mXdgWmBase) {
785 xdg_wm_base_destroy (mXdgWmBase);
786 mXdgWmBase = NULL;
787 }
788
789 if (mCompositor) {
790 wl_compositor_destroy (mCompositor);
791 mCompositor = NULL;
792 }
793
794 if (mSubCompositor) {
795 wl_subcompositor_destroy (mSubCompositor);
796 mSubCompositor = NULL;
797 }
798
799 if (mRegistry) {
800 wl_registry_destroy (mRegistry);
801 mRegistry= NULL;
802 }
803
804 if (mWlDisplayWrapper) {
805 wl_proxy_wrapper_destroy (mWlDisplayWrapper);
806 mWlDisplayWrapper = NULL;
807 }
808
fei.deng640c3c92024-04-12 08:31:19 +0000809 if (mAmlConfig) {
810 aml_config_destroy(mAmlConfig);
811 mAmlConfig = NULL;
812 }
813
fei.dengf7a0cd32023-08-29 09:36:37 +0000814 if (mWlQueue) {
815 wl_event_queue_destroy (mWlQueue);
816 mWlQueue = NULL;
817 }
818
819 if (mWlDisplay) {
820 wl_display_flush (mWlDisplay);
821 wl_display_disconnect (mWlDisplay);
822 mWlDisplay = NULL;
823 }
824
fei.dengb9a1a572023-09-13 01:33:57 +0000825 DEBUG(mLogCategory,"closeDisplay out");
fei.dengf7a0cd32023-08-29 09:36:37 +0000826}
827
828int WaylandDisplay::toDmaBufferFormat(RenderVideoFormat format, uint32_t *outDmaformat /*out param*/, uint64_t *outDmaformatModifiers /*out param*/)
829{
830 if (!outDmaformat || !outDmaformatModifiers) {
fei.dengb9a1a572023-09-13 01:33:57 +0000831 WARNING(mLogCategory,"NULL params");
832 return ERROR_PARAM_NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000833 }
834
835 *outDmaformat = 0;
836 *outDmaformatModifiers = 0;
837
838 uint32_t dmaformat = video_format_to_wl_dmabuf_format (format);
839 if (dmaformat == -1) {
fei.dengb9a1a572023-09-13 01:33:57 +0000840 ERROR(mLogCategory,"Error not found render video format:%d to wl dmabuf format",format);
841 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000842 }
843
fei.dengb9a1a572023-09-13 01:33:57 +0000844 TRACE(mLogCategory,"render video format:%d -> dmabuf format:%d",format,dmaformat);
fei.dengf7a0cd32023-08-29 09:36:37 +0000845 *outDmaformat = (uint32_t)dmaformat;
846
847 /*get dmaformat and modifiers*/
848 auto item = mDmaBufferFormats.find(dmaformat);
849 if (item == mDmaBufferFormats.end()) { //not found
fei.dengb9a1a572023-09-13 01:33:57 +0000850 WARNING(mLogCategory,"Not found dmabuf for render video format :%d",format);
fei.dengf7a0cd32023-08-29 09:36:37 +0000851 *outDmaformatModifiers = 0;
fei.dengb9a1a572023-09-13 01:33:57 +0000852 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000853 }
854
855 *outDmaformatModifiers = (uint64_t)item->second;
856
fei.dengb9a1a572023-09-13 01:33:57 +0000857 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000858}
859
860int WaylandDisplay::toShmBufferFormat(RenderVideoFormat format, uint32_t *outformat)
861{
862 if (!outformat) {
fei.dengb9a1a572023-09-13 01:33:57 +0000863 WARNING(mLogCategory,"NULL params");
864 return ERROR_PARAM_NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000865 }
866
867 *outformat = 0;
868
869 int shmformat = (int)video_format_to_wl_shm_format(format);
870 if (shmformat < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000871 ERROR(mLogCategory,"Error not found render video format:%d to wl shmbuf format",format);
872 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000873 }
874
875 for (auto item = mShmFormats.begin(); item != mShmFormats.end(); ++item) {
876 uint32_t registFormat = (uint32_t)*item;
877 if (registFormat == (uint32_t)shmformat) {
878 *outformat = registFormat;
fei.dengb9a1a572023-09-13 01:33:57 +0000879 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000880 }
881 }
882
fei.dengb9a1a572023-09-13 01:33:57 +0000883 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000884}
885
886void WaylandDisplay::setVideoBufferFormat(RenderVideoFormat format)
887{
fei.dengb9a1a572023-09-13 01:33:57 +0000888 TRACE(mLogCategory,"set video buffer format: %d",format);
fei.dengf7a0cd32023-08-29 09:36:37 +0000889 mBufferFormat = format;
890};
891
892void WaylandDisplay::setDisplayOutput(int output)
893{
fei.dengb9a1a572023-09-13 01:33:57 +0000894 TRACE(mLogCategory,"select display output: %d",output);
fei.dengf7a0cd32023-08-29 09:36:37 +0000895 if (output < 0 || output >= DEFAULT_DISPLAY_OUTPUT_NUM) {
fei.dengb9a1a572023-09-13 01:33:57 +0000896 ERROR(mLogCategory, "display output index error,please set 0:primary or 1:extend,now:%d",output);
fei.dengf7a0cd32023-08-29 09:36:37 +0000897 return;
898 }
fei.deng26950832023-11-09 03:00:23 +0000899 //only do select output before video playing
900 if (mSelectOutputIndex != output) {
901 mSelectOutputIndex = output;
902 // if (mOutput[output].wlOutput) {
903 // mCurrentDisplayOutput = &mOutput[output];
904 // setRenderRectangle(mOutput[output].offsetX, mOutput[output].offsetY,
905 // mOutput[output].width, mOutput[output].height);
906 // }
fei.dengf7a0cd32023-08-29 09:36:37 +0000907 }
908}
909
910int WaylandDisplay::getDisplayOutput()
911{
fei.deng26950832023-11-09 03:00:23 +0000912 return mSelectOutputIndex == INVALID_OUTPUT_INDEX? 0: mSelectOutputIndex;
fei.dengf7a0cd32023-08-29 09:36:37 +0000913}
914
915void WaylandDisplay::setPip(int pip)
916{
fei.dengb9a1a572023-09-13 01:33:57 +0000917 INFO(mLogCategory,"set pip:%d",pip);
fei.dengf7a0cd32023-08-29 09:36:37 +0000918 mPip = pip;
919}
920
fei.deng26950832023-11-09 03:00:23 +0000921void WaylandDisplay::updateDisplayOutput()
922{
923 if (!mCurrentDisplayOutput->wlOutput || !mXdgToplevel || !mXdgSurface)
924 {
925 return;
926 }
927 if (mUpdateRenderRectangle) {
928 if (mFullScreen) {
929 DEBUG(mLogCategory,"unset full screen");
930 xdg_toplevel_unset_fullscreen (mXdgToplevel);
931 }
932
933 if (mXdgSurface) {
934 DEBUG(mLogCategory,"set geometry");
935 xdg_surface_set_window_geometry(mXdgSurface,
936 mCurrentDisplayOutput->offsetX,
937 mCurrentDisplayOutput->offsetY,
938 mCurrentDisplayOutput->width,
939 mCurrentDisplayOutput->height);
940 }
941
942 if (mFullScreen && mXdgToplevel) {
943 DEBUG(mLogCategory,"set full screen");
944 xdg_toplevel_set_fullscreen (mXdgToplevel, mCurrentDisplayOutput->wlOutput);
945 }
946 setRenderRectangle(mCurrentDisplayOutput->offsetX, mCurrentDisplayOutput->offsetY,
947 mCurrentDisplayOutput->width, mCurrentDisplayOutput->height);
948 mUpdateRenderRectangle = false;
949 }
950}
951
fei.dengf7a0cd32023-08-29 09:36:37 +0000952void WaylandDisplay::createCommonWindowSurface()
953{
954 struct wl_region *region;
955
956 mAreaSurface = wl_compositor_create_surface (mCompositor);
957 mVideoSurface = wl_compositor_create_surface (mCompositor);
958 mAreaSurfaceWrapper = (struct wl_surface *)wl_proxy_create_wrapper (mAreaSurface);
959 mVideoSurfaceWrapper = (struct wl_surface *)wl_proxy_create_wrapper (mVideoSurface);
960
961 wl_proxy_set_queue ((struct wl_proxy *) mAreaSurfaceWrapper, mWlQueue);
962 wl_proxy_set_queue ((struct wl_proxy *) mVideoSurfaceWrapper, mWlQueue);
963
964 /* embed video_surface in area_surface */
965 mVideoSubSurface = wl_subcompositor_get_subsurface (mSubCompositor, mVideoSurface, mAreaSurface);
966 wl_subsurface_set_desync (mVideoSubSurface);
967
968 if (mViewporter) {
969 mAreaViewport = wp_viewporter_get_viewport (mViewporter, mAreaSurface);
970 mVideoViewport = wp_viewporter_get_viewport (mViewporter, mVideoSurface);
971 }
972
973 /* do not accept input */
974 region = wl_compositor_create_region (mCompositor);
975 wl_surface_set_input_region (mAreaSurface, region);
976 wl_region_destroy (region);
977
978 region = wl_compositor_create_region (mCompositor);
979 wl_surface_set_input_region (mVideoSurface, region);
980 wl_region_destroy (region);
981}
982
983void WaylandDisplay::createXdgShellWindowSurface()
984{
985 /* Check which protocol we will use (in order of preference) */
986 if (mXdgWmBase) {
987 /* First create the XDG surface */
988 mXdgSurface= xdg_wm_base_get_xdg_surface (mXdgWmBase, mAreaSurface);
989 if (!mXdgSurface) {
fei.dengb9a1a572023-09-13 01:33:57 +0000990 ERROR(mLogCategory,"Unable to get xdg_surface");
fei.dengf7a0cd32023-08-29 09:36:37 +0000991 return;
992 }
993 xdg_surface_add_listener (mXdgSurface, &xdg_surface_listener,(void *)this);
994
995 /* Then the toplevel */
996 mXdgToplevel= xdg_surface_get_toplevel (mXdgSurface);
997 if (!mXdgSurface) {
fei.dengb9a1a572023-09-13 01:33:57 +0000998 ERROR(mLogCategory,"Unable to get xdg_toplevel");
fei.dengf7a0cd32023-08-29 09:36:37 +0000999 return;
1000 }
1001 xdg_toplevel_add_listener (mXdgToplevel, &xdg_toplevel_listener, this);
1002
1003 /* Finally, commit the xdg_surface state as toplevel */
1004 mXdgSurfaceConfigured = false;
1005 wl_surface_commit (mAreaSurface);
1006 wl_display_flush (mWlDisplay);
1007 /* we need exactly 3 roundtrips to discover global objects and their state */
1008 for (int i = 0; i < 3; i++) {
1009 if (wl_display_roundtrip_queue(mWlDisplay, mWlQueue) < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +00001010 ERROR(mLogCategory,"Error communicating with the wayland display");
fei.dengf7a0cd32023-08-29 09:36:37 +00001011 }
1012 }
1013
1014 if (mXdgSurfaceConfigured) {
fei.dengb9a1a572023-09-13 01:33:57 +00001015 INFO(mLogCategory,"xdg surface had configured");
fei.dengf7a0cd32023-08-29 09:36:37 +00001016 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001017 WARNING(mLogCategory,"xdg surface not configured");
fei.dengf7a0cd32023-08-29 09:36:37 +00001018 }
1019
1020 //full screen show
fei.deng26950832023-11-09 03:00:23 +00001021 // if (mFullScreen && mCurrentDisplayOutput->wlOutput) {
1022 // //ensureFullscreen(mFullScreen);
fei.dengf7a0cd32023-08-29 09:36:37 +00001023 // }
1024 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001025 ERROR(mLogCategory,"Unable to use xdg_wm_base ");
fei.dengf7a0cd32023-08-29 09:36:37 +00001026 return;
1027 }
1028}
1029
1030void WaylandDisplay::destroyWindowSurfaces()
1031{
1032 if (mAreaShmBuffer) {
1033 delete mAreaShmBuffer;
1034 mAreaShmBuffer = NULL;
1035 }
1036
1037 //clean all wayland buffers
1038 cleanAllWaylandBuffer();
1039
1040 if (mXdgToplevel) {
1041 xdg_toplevel_destroy (mXdgToplevel);
1042 mXdgToplevel = NULL;
1043 }
1044
1045 if (mXdgSurface) {
1046 xdg_surface_destroy (mXdgSurface);
1047 mXdgSurface = NULL;
1048 }
1049
1050 if (mVideoSurfaceWrapper) {
1051 wl_proxy_wrapper_destroy (mVideoSurfaceWrapper);
1052 mVideoSurfaceWrapper = NULL;
1053 }
1054
1055 if (mVideoSubSurface) {
1056 wl_subsurface_destroy (mVideoSubSurface);
1057 mVideoSubSurface = NULL;
1058 }
1059
1060 if (mVideoSurface) {
1061 wl_surface_destroy (mVideoSurface);
1062 mVideoSurface = NULL;
1063 }
1064
1065 if (mAreaSurfaceWrapper) {
1066 wl_proxy_wrapper_destroy (mAreaSurfaceWrapper);
1067 mAreaSurfaceWrapper = NULL;
1068 }
1069
1070 if (mAreaSurface) {
1071 wl_surface_destroy (mAreaSurface);
1072 mAreaSurface = NULL;
1073 mReCommitAreaSurface = false;
1074 }
1075}
1076
1077void WaylandDisplay::ensureFullscreen(bool fullscreen)
1078{
1079 if (mXdgWmBase) {
fei.dengb9a1a572023-09-13 01:33:57 +00001080 DEBUG(mLogCategory,"full screen : %d",fullscreen);
fei.dengf7a0cd32023-08-29 09:36:37 +00001081 if (fullscreen) {
fei.deng26950832023-11-09 03:00:23 +00001082 xdg_toplevel_set_fullscreen (mXdgToplevel, mCurrentDisplayOutput->wlOutput);
fei.dengf7a0cd32023-08-29 09:36:37 +00001083 } else {
1084 xdg_toplevel_unset_fullscreen (mXdgToplevel);
1085 }
1086 }
1087}
1088
1089void WaylandDisplay::setRenderRectangle(int x, int y, int w, int h)
1090{
fei.dengb9a1a572023-09-13 01:33:57 +00001091 DEBUG(mLogCategory,"set render rect:x:%d,y:%d,w:%d,h:%d",x,y,w,h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001092
1093 if (w <= 0 || h <= 0) {
fei.dengb9a1a572023-09-13 01:33:57 +00001094 WARNING(mLogCategory, "wrong render width or height %dx%d",w,h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001095 return;
1096 }
1097
1098 mRenderRect.x = x;
1099 mRenderRect.y = y;
1100 mRenderRect.w = w;
1101 mRenderRect.h = h;
1102
1103 if (!mXdgSurfaceConfigured) {
fei.dengb9a1a572023-09-13 01:33:57 +00001104 WARNING(mLogCategory,"Not configured xdg");
fei.dengf7a0cd32023-08-29 09:36:37 +00001105 return;
1106 }
1107
1108 if (mAreaViewport) {
1109 wp_viewport_set_destination (mAreaViewport, w, h);
1110 }
1111
1112 updateBorders();
1113
1114 if (mVideoWidth != 0 && mVideoSurface) {
1115 wl_subsurface_set_sync (mVideoSubSurface);
1116 resizeVideoSurface(true);
1117 }
1118
1119 wl_surface_damage (mAreaSurfaceWrapper, 0, 0, w, h);
1120 wl_surface_commit (mAreaSurfaceWrapper);
1121
1122 if (mVideoWidth != 0) {
1123 wl_subsurface_set_desync (mVideoSubSurface);
1124 }
1125}
1126
1127
1128void WaylandDisplay::setFrameSize(int w, int h)
1129{
1130 mVideoWidth = w;
1131 mVideoHeight = h;
fei.dengb9a1a572023-09-13 01:33:57 +00001132 TRACE(mLogCategory,"frame w:%d,h:%d",mVideoWidth,mVideoHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +00001133 if (mRenderRect.w > 0 && mVideoSurface) {
1134 resizeVideoSurface(true);
1135 }
1136}
1137
1138void WaylandDisplay::setWindowSize(int x, int y, int w, int h)
1139{
1140 mWindowRect.x = x;
1141 mWindowRect.y = y;
1142 mWindowRect.w = w;
1143 mWindowRect.h = h;
fei.dengb9a1a572023-09-13 01:33:57 +00001144 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 +00001145 if (mWindowRect.w > 0 && mVideoWidth > 0 && mVideoSurface) {
1146 //if had full screen, unset it and set window size
1147 if (mFullScreen) {
1148 mFullScreen = false;
1149 ensureFullscreen(mFullScreen);
1150 }
1151 resizeVideoSurface(true);
1152 }
1153}
1154
1155
1156void WaylandDisplay::resizeVideoSurface(bool commit)
1157{
1158 Rectangle src = {0,};
1159 Rectangle dst = {0,};
1160 Rectangle res;
1161
1162 /* center the video_subsurface inside area_subsurface */
1163 src.w = mVideoWidth;
1164 src.h = mVideoHeight;
1165 /*if had set the window size, we will scall
1166 video surface to this window size*/
1167 if (mWindowRect.w > 0 && mWindowRect.h > 0) {
1168 dst.x = mWindowRect.x;
1169 dst.y = mWindowRect.y;
1170 dst.w = mWindowRect.w;
1171 dst.h = mWindowRect.h;
1172 if (mWindowRect.w > mRenderRect.w && mWindowRect.h > mRenderRect.h) {
fei.dengb9a1a572023-09-13 01:33:57 +00001173 WARNING(mLogCategory,"Error window size:%dx%d, but render size:%dx%d,reset to render size",
fei.dengf7a0cd32023-08-29 09:36:37 +00001174 mWindowRect.w,mWindowRect.h,mRenderRect.w,mRenderRect.h);
1175 dst.x = mRenderRect.x;
1176 dst.y = mRenderRect.y;
1177 dst.w = mRenderRect.w;
1178 dst.h = mRenderRect.h;
1179 }
1180 //to do,we need set geometry?
1181 //if (mXdgSurface) {
1182 // xdg_surface_set_window_geometry(mXdgSurface, mWindowRect.x, mWindowRect.y, mWindowRect.w, mWindowRect.h);
1183 //}
1184 } else { //scal video to full screen
1185 dst.w = mRenderRect.w;
1186 dst.h = mRenderRect.h;
1187 }
1188
1189 if (mViewporter) {
1190 videoCenterRect(src, dst, &res, true);
1191 } else {
1192 videoCenterRect(src, dst, &res, false);
1193 }
1194
1195 wl_subsurface_set_position (mVideoSubSurface, res.x, res.y);
1196
1197 if (commit) {
1198 wl_surface_damage (mVideoSurfaceWrapper, 0, 0, res.w, res.h);
1199 wl_surface_commit (mVideoSurfaceWrapper);
1200 }
1201
1202 //top level setting
1203 if (mXdgToplevel) {
1204 struct wl_region *region;
1205
1206 region = wl_compositor_create_region (mCompositor);
1207 wl_region_add (region, 0, 0, mRenderRect.w, mRenderRect.h);
1208 wl_surface_set_input_region (mAreaSurface, region);
1209 wl_region_destroy (region);
1210 }
1211
1212 /* this is saved for use in wl_surface_damage */
1213 mVideoRect.x = res.x;
1214 mVideoRect.y = res.y;
1215 mVideoRect.w = res.w;
1216 mVideoRect.h = res.h;
1217
1218 //to scale video surface to full screen
1219 wp_viewport_set_destination(mVideoViewport, res.w, res.h);
fei.dengb9a1a572023-09-13 01:33:57 +00001220 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 +00001221}
1222
1223void WaylandDisplay::setOpaque()
1224{
1225 struct wl_region *region;
1226
1227 /* Set area opaque */
1228 region = wl_compositor_create_region (mCompositor);
1229 wl_region_add (region, 0, 0, mRenderRect.w, mRenderRect.h);
1230 wl_surface_set_opaque_region (mAreaSurface, region);
1231 wl_region_destroy (region);
1232}
1233
1234int WaylandDisplay::prepareFrameBuffer(RenderBuffer * buf)
1235{
1236 WaylandBuffer *waylandBuf = NULL;
1237 int ret;
1238
1239 waylandBuf = findWaylandBuffer(buf);
1240 if (waylandBuf == NULL) {
fei.dengb9a1a572023-09-13 01:33:57 +00001241 waylandBuf = new WaylandBuffer(this, mLogCategory);
fei.dengf7a0cd32023-08-29 09:36:37 +00001242 waylandBuf->setBufferFormat(mBufferFormat);
1243 ret = waylandBuf->constructWlBuffer(buf);
fei.dengb9a1a572023-09-13 01:33:57 +00001244 if (ret != NO_ERROR) {
1245 WARNING(mLogCategory,"dmabufConstructWlBuffer fail,release waylandbuf");
fei.dengf7a0cd32023-08-29 09:36:37 +00001246 //delete waylanBuf,WaylandBuffer object destruct will call release callback
1247 goto waylandbuf_fail;
1248 } else {
1249 addWaylandBuffer(buf, waylandBuf);
1250 }
1251 }
fei.dengb9a1a572023-09-13 01:33:57 +00001252 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +00001253waylandbuf_fail:
1254 //delete waylandbuf
1255 delete waylandBuf;
1256 waylandBuf = NULL;
fei.dengb9a1a572023-09-13 01:33:57 +00001257 return ERROR_UNKNOWN;
fei.dengf7a0cd32023-08-29 09:36:37 +00001258}
1259
1260void WaylandDisplay::displayFrameBuffer(RenderBuffer * buf, int64_t realDisplayTime)
1261{
1262 WaylandBuffer *waylandBuf = NULL;
1263 struct wl_buffer * wlbuffer = NULL;
1264 int ret;
1265
1266 if (!buf) {
fei.dengb9a1a572023-09-13 01:33:57 +00001267 ERROR(mLogCategory,"Error input params, waylandbuffer is null");
fei.dengf7a0cd32023-08-29 09:36:37 +00001268 return;
1269 }
1270
1271 //must commit areasurface first, because weston xdg surface maybe timeout
1272 //this cause video is not display,commit can resume xdg surface
1273 if (!mReCommitAreaSurface) {
1274 mReCommitAreaSurface = true;
1275 wl_surface_commit (mAreaSurface);
1276 }
1277
fei.dengb9a1a572023-09-13 01:33:57 +00001278 TRACE(mLogCategory,"display renderBuffer:%p,PTS:%lld,realtime:%lld",buf, buf->pts, realDisplayTime);
fei.dengf7a0cd32023-08-29 09:36:37 +00001279
1280 if (buf->flag & BUFFER_FLAG_DMA_BUFFER) {
1281 if (buf->dma.width <=0 || buf->dma.height <=0) {
1282 buf->dma.width = mVideoWidth;
1283 buf->dma.height = mVideoHeight;
1284 }
1285 waylandBuf = findWaylandBuffer(buf);
1286 if (waylandBuf) {
1287 waylandBuf->setRenderRealTime(realDisplayTime);
1288 ret = waylandBuf->constructWlBuffer(buf);
fei.dengb9a1a572023-09-13 01:33:57 +00001289 if (ret != NO_ERROR) {
1290 WARNING(mLogCategory,"dmabufConstructWlBuffer fail,release waylandbuf");
fei.dengf7a0cd32023-08-29 09:36:37 +00001291 //delete waylanBuf,WaylandBuffer object destruct will call release callback
1292 goto waylandbuf_fail;
1293 }
1294 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001295 ERROR(mLogCategory,"NOT found wayland buffer,please prepare buffer first");
fei.dengf7a0cd32023-08-29 09:36:37 +00001296 goto waylandbuf_fail;
1297 }
1298 }
1299
1300 if (waylandBuf) {
1301 wlbuffer = waylandBuf->getWlBuffer();
1302 }
fei.deng1cfb2752023-10-26 08:01:25 +00001303 //if no wl_output, drop this buffer
fei.deng26950832023-11-09 03:00:23 +00001304 if (mCurrentDisplayOutput->wlOutput == NULL) {
fei.deng1cfb2752023-10-26 08:01:25 +00001305 TRACE(mLogCategory,"No wl_output");
1306 mWaylandPlugin->handleFrameDropped(buf);
1307 mWaylandPlugin->handleBufferRelease(buf);
1308 return;
1309 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001310 if (wlbuffer) {
fei.dengb9a1a572023-09-13 01:33:57 +00001311 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001312 ++mCommitCnt;
1313 uint32_t hiPts = realDisplayTime >> 32;
1314 uint32_t lowPts = realDisplayTime & 0xFFFFFFFF;
1315 //attach this wl_buffer to weston
fei.dengb9a1a572023-09-13 01:33:57 +00001316 TRACE(mLogCategory,"++attach,renderbuf:%p,wl_buffer:%p(0,0,%d,%d),pts:%lld,commitCnt:%d",buf,wlbuffer,mVideoRect.w,mVideoRect.h,buf->pts,mCommitCnt);
fei.dengf7a0cd32023-08-29 09:36:37 +00001317 waylandBuf->attach(mVideoSurfaceWrapper);
1318
fei.deng640c3c92024-04-12 08:31:19 +00001319 if (mAmlConfigAPIList.enableSetPts) {
fei.dengb9a1a572023-09-13 01:33:57 +00001320 TRACE(mLogCategory,"display time:%lld,hiPts:%u,lowPts:%u",realDisplayTime, hiPts, lowPts);
fei.dengf7a0cd32023-08-29 09:36:37 +00001321 wl_surface_set_pts(mVideoSurfaceWrapper, hiPts, lowPts);
1322 }
1323
1324 wl_surface_damage (mVideoSurfaceWrapper, 0, 0, mVideoRect.w, mVideoRect.h);
1325 wl_surface_commit (mVideoSurfaceWrapper);
1326 //insert this buffer to committed weston buffer manager
1327 std::pair<int64_t, WaylandBuffer *> item(buf->pts, waylandBuf);
1328 mCommittedBufferMap.insert(item);
1329 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001330 WARNING(mLogCategory,"wlbuffer is NULL");
fei.dengf7a0cd32023-08-29 09:36:37 +00001331 /* clear both video and parent surfaces */
1332 cleanSurface();
1333 }
1334
1335 wl_display_flush (mWlDisplay);
1336
1337 return;
1338waylandbuf_fail:
1339 //notify dropped
1340 mWaylandPlugin->handleFrameDropped(buf);
1341 //notify app release this buf
1342 mWaylandPlugin->handleBufferRelease(buf);
1343 //delete waylandbuf
1344 delete waylandBuf;
1345 waylandBuf = NULL;
1346 return;
1347}
1348
1349void WaylandDisplay::handleBufferReleaseCallback(WaylandBuffer *buf)
1350{
1351 {
fei.dengb9a1a572023-09-13 01:33:57 +00001352 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001353 --mCommitCnt;
1354 //remove buffer if this buffer is ready to release
1355 RenderBuffer *renderBuffer = buf->getRenderBuffer();
1356 auto item = mCommittedBufferMap.find(renderBuffer->pts);
1357 if (item != mCommittedBufferMap.end()) {
1358 mCommittedBufferMap.erase(item);
1359 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001360 WARNING(mLogCategory,"Can't find WaylandBuffer in buffer map");
fei.dengf7a0cd32023-08-29 09:36:37 +00001361 return;
1362 }
1363 }
1364 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengb9a1a572023-09-13 01:33:57 +00001365 TRACE(mLogCategory,"handle release renderBuffer :%p,priv:%p,PTS:%lld,realtime:%lld us,commitCnt:%d",renderBuffer,renderBuffer->priv,renderBuffer->pts/1000,buf->getRenderRealTime(),mCommitCnt);
fei.dengf7a0cd32023-08-29 09:36:37 +00001366 mWaylandPlugin->handleBufferRelease(renderBuffer);
1367}
1368
1369void WaylandDisplay::handleFrameDisplayedCallback(WaylandBuffer *buf)
1370{
1371 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengb9a1a572023-09-13 01:33:57 +00001372 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 +00001373 if (!mSignalFirstFramePts) {
1374 mSignalFirstFramePts = true;
1375 mWaylandPlugin->handleMsgNotify(MSG_FIRST_FRAME, (void*)&renderBuffer->pts);
1376 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001377 mWaylandPlugin->handleFrameDisplayed(renderBuffer);
1378}
1379
1380void WaylandDisplay::handleFrameDropedCallback(WaylandBuffer *buf)
1381{
1382 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengb9a1a572023-09-13 01:33:57 +00001383 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 +00001384 mWaylandPlugin->handleFrameDropped(renderBuffer);
1385}
1386
1387
1388void WaylandDisplay::readyToRun()
1389{
1390 mFd = wl_display_get_fd (mWlDisplay);
fei.dengb9a1a572023-09-13 01:33:57 +00001391 if (mPoll) {
1392 mPoll->addFd(mFd);
1393 mPoll->setFdReadable(mFd, true);
1394 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001395}
1396
1397bool WaylandDisplay::threadLoop()
1398{
1399 int ret;
fei.dengf7a0cd32023-08-29 09:36:37 +00001400
1401 while (wl_display_prepare_read_queue (mWlDisplay, mWlQueue) != 0) {
1402 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1403 }
1404
1405 wl_display_flush (mWlDisplay);
1406
1407 /*poll timeout value must > 300 ms,otherwise zwp_linux_dmabuf will create failed,
1408 so do use -1 to wait for ever*/
fei.dengb9a1a572023-09-13 01:33:57 +00001409 ret = mPoll->wait(-1); //wait for ever
fei.dengf7a0cd32023-08-29 09:36:37 +00001410 if (ret < 0) { //poll error
fei.dengb9a1a572023-09-13 01:33:57 +00001411 WARNING(mLogCategory,"poll error");
fei.dengf7a0cd32023-08-29 09:36:37 +00001412 wl_display_cancel_read(mWlDisplay);
1413 return false;
1414 } else if (ret == 0) { //poll time out
1415 return true; //run loop
1416 }
1417
1418 if (wl_display_read_events (mWlDisplay) == -1) {
1419 goto tag_error;
1420 }
1421
1422 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1423 return true;
1424tag_error:
fei.dengb9a1a572023-09-13 01:33:57 +00001425 ERROR(mLogCategory,"Error communicating with the wayland server");
fei.dengf7a0cd32023-08-29 09:36:37 +00001426 return false;
1427}
1428
1429void WaylandDisplay::videoCenterRect(Rectangle src, Rectangle dst, Rectangle *result, bool scaling)
1430{
1431 //if dst is a small window, we scale video to map window size,don't doing center
1432 if (mRenderRect.w != dst.w && mRenderRect.h != dst.h) {
1433 result->x = dst.x;
1434 result->y = dst.y;
1435 result->w = dst.w;
1436 result->h = dst.h;
fei.dengb9a1a572023-09-13 01:33:57 +00001437 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 +00001438 src.w, src.h, dst.w, dst.h, result->w, result->h, result->x, result->y);
1439 return;
1440 }
1441 if (!scaling) {
1442 result->w = MIN (src.w, dst.w);
1443 result->h = MIN (src.h, dst.h);
1444 result->x = dst.x + (dst.w - result->w) / 2;
1445 result->y = dst.y + (dst.h - result->h) / 2;
1446 } else {
1447 double src_ratio, dst_ratio;
1448
1449 src_ratio = (double) src.w / src.h;
1450 dst_ratio = (double) dst.w / dst.h;
1451
1452 if (src_ratio > dst_ratio) {
1453 result->w = dst.w;
1454 result->h = dst.w / src_ratio;
1455 result->x = dst.x;
1456 result->y = dst.y + (dst.h - result->h) / 2;
1457 } else if (src_ratio < dst_ratio) {
1458 result->w = dst.h * src_ratio;
1459 result->h = dst.h;
1460 result->x = dst.x + (dst.w - result->w) / 2;
1461 result->y = dst.y;
1462 } else {
1463 result->x = dst.x;
1464 result->y = dst.y;
1465 result->w = dst.w;
1466 result->h = dst.h;
1467 }
1468 }
1469
fei.dengb9a1a572023-09-13 01:33:57 +00001470 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 +00001471 src.w, src.h, dst.w, dst.h, result->w, result->h, result->x, result->y);
1472}
1473
1474void WaylandDisplay::updateBorders()
1475{
1476 int width,height;
1477
1478 if (mNoBorderUpdate)
1479 return;
1480
1481 if (mViewporter) {
1482 width = height = 1;
1483 mNoBorderUpdate = true;
1484 } else {
1485 width = mRenderRect.w;
1486 height = mRenderRect.h;
1487 }
1488
1489 RenderVideoFormat format = VIDEO_FORMAT_BGRA;
fei.dengb9a1a572023-09-13 01:33:57 +00001490 mAreaShmBuffer = new WaylandShmBuffer(this, mLogCategory);
fei.dengf7a0cd32023-08-29 09:36:37 +00001491 struct wl_buffer *wlbuf = mAreaShmBuffer->constructWlBuffer(width, height, format);
1492 if (wlbuf == NULL) {
1493 delete mAreaShmBuffer;
1494 mAreaShmBuffer = NULL;
1495 }
1496
1497 wl_surface_attach(mAreaSurfaceWrapper, wlbuf, 0, 0);
1498}
1499
1500std::size_t WaylandDisplay::calculateDmaBufferHash(RenderDmaBuffer &dmabuf)
1501{
1502 std::string hashString("");
1503 for (int i = 0; i < dmabuf.planeCnt; i++) {
1504 char hashtmp[1024];
1505 snprintf (hashtmp, 1024, "%d%d%d%d%d%d", dmabuf.width,dmabuf.height,dmabuf.planeCnt,
1506 dmabuf.stride[i],dmabuf.offset[i],dmabuf.fd[i]);
1507 std::string tmp(hashtmp);
1508 hashString += tmp;
1509 }
1510
1511 std::size_t hashval = std::hash<std::string>()(hashString);
fei.dengb9a1a572023-09-13 01:33:57 +00001512 //TRACE(mLogCategory,"hashstr:%s,val:%zu",hashString.c_str(),hashval);
fei.dengf7a0cd32023-08-29 09:36:37 +00001513 return hashval;
1514}
1515
1516void WaylandDisplay::addWaylandBuffer(RenderBuffer * buf, WaylandBuffer *waylandbuf)
1517{
1518 if (buf->flag & BUFFER_FLAG_DMA_BUFFER) {
1519 std::size_t hashval = calculateDmaBufferHash(buf->dma);
1520 std::pair<std::size_t, WaylandBuffer *> item(hashval, waylandbuf);
fei.dengb9a1a572023-09-13 01:33:57 +00001521 //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 +00001522 mWaylandBuffersMap.insert(item);
1523 }
1524
1525 //clean invalid wayland buffer,if video resolution changed
1526 for (auto item = mWaylandBuffersMap.begin(); item != mWaylandBuffersMap.end(); ) {
1527 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1528 if ((waylandbuf->getFrameWidth() != buf->dma.width ||
1529 waylandbuf->getFrameHeight() != buf->dma.height) &&
1530 waylandbuf->isFree()) {
fei.dengb9a1a572023-09-13 01:33:57 +00001531 TRACE(mLogCategory,"delete wayland buffer,width:%d(%d),height:%d(%d)",
fei.dengf7a0cd32023-08-29 09:36:37 +00001532 waylandbuf->getFrameWidth(),buf->dma.width,
1533 waylandbuf->getFrameHeight(),buf->dma.height);
1534 mWaylandBuffersMap.erase(item++);
1535 delete waylandbuf;
1536 } else {
1537 item++;
1538 }
1539 }
fei.dengb9a1a572023-09-13 01:33:57 +00001540 TRACE(mLogCategory,"mWaylandBuffersMap size:%d",mWaylandBuffersMap.size());
fei.dengf7a0cd32023-08-29 09:36:37 +00001541}
1542
1543WaylandBuffer* WaylandDisplay::findWaylandBuffer(RenderBuffer * buf)
1544{
1545 std::size_t hashval = calculateDmaBufferHash(buf->dma);
1546 auto item = mWaylandBuffersMap.find(hashval);
1547 if (item == mWaylandBuffersMap.end()) {
1548 return NULL;
1549 }
1550
1551 return (WaylandBuffer*) item->second;
1552}
1553
1554void WaylandDisplay::cleanAllWaylandBuffer()
1555{
1556 //free all obtain buff
1557 for (auto item = mWaylandBuffersMap.begin(); item != mWaylandBuffersMap.end(); ) {
1558 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1559 mWaylandBuffersMap.erase(item++);
1560 delete waylandbuf;
1561 }
1562}
1563
1564void WaylandDisplay::flushBuffers()
1565{
fei.dengb9a1a572023-09-13 01:33:57 +00001566 INFO(mLogCategory,"flushBuffers");
1567 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001568 for (auto item = mCommittedBufferMap.begin(); item != mCommittedBufferMap.end(); item++) {
1569 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1570 waylandbuf->forceRedrawing();
1571 handleFrameDisplayedCallback(waylandbuf);
1572 }
1573}
1574
1575void WaylandDisplay::cleanSurface()
1576{
1577 /* clear both video and parent surfaces */
1578 wl_surface_attach (mVideoSurfaceWrapper, NULL, 0, 0);
1579 wl_surface_commit (mVideoSurfaceWrapper);
1580 wl_surface_attach (mAreaSurfaceWrapper, NULL, 0, 0);
1581 wl_surface_commit (mAreaSurfaceWrapper);
fei.deng640c3c92024-04-12 08:31:19 +00001582}
1583
1584void WaylandDisplay::setKeepLastFrame(int keep)
1585{
1586 mKeepLastFrame = keep;
1587 if (mVideoSurfaceWrapper && mAmlConfigAPIList.enableKeepLastFrame) {
1588 INFO(mLogCategory,"keep last frame:%d",keep);
1589 wl_surface_keep_last_frame(mVideoSurfaceWrapper, keep);
1590 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001591}