blob: 6a4032aad5b99aab203ea9e2d5d118d9a67cca41 [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.dengf7a0cd32023-08-29 09:36:37 +0000642}
643
644WaylandDisplay::~WaylandDisplay()
645{
fei.dengb9a1a572023-09-13 01:33:57 +0000646 TRACE(mLogCategory,"desconstruct WaylandDisplay");
647 if (mPoll) {
648 delete mPoll;
649 mPoll = NULL;
650 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000651}
652
653char *WaylandDisplay::require_xdg_runtime_dir()
654{
655 char *val = getenv("XDG_RUNTIME_DIR");
fei.dengb9a1a572023-09-13 01:33:57 +0000656 INFO(mLogCategory,"XDG_RUNTIME_DIR=%s",val);
fei.dengdd910ef2024-06-07 10:25:30 +0800657 //if not set XDG_RUNTIME_DIR,set default value
658 // if (!val) {
659 // val = const_cast<char *>("/run/user/0");
660 // setenv("XDG_RUNTIME_DIR",val,0);
661 // WARNING(mLogCategory,"XDG_RUNTIME_DIR is not set,set default %s",val);
662 // }
fei.dengf7a0cd32023-08-29 09:36:37 +0000663
664 return val;
665}
666
667int WaylandDisplay::openDisplay()
668{
fei.dengdd910ef2024-06-07 10:25:30 +0800669 mNoBorderUpdate = false;
670 mReCommitAreaSurface = false;
671 mXdgSurfaceConfigured = false;
672 mUpdateRenderRectangle = false;
673 memset(&mRenderRect, 0, sizeof(struct Rectangle));
674 memset(&mVideoRect, 0, sizeof(struct Rectangle));
675 memset(&mWindowRect, 0, sizeof(struct Rectangle));
676 mFullScreen = true; //default is full screen
677 mAmlConfig = NULL;
678 //weston config private api
679 mAmlConfigAPIList.enableDropFrame = false;
680 mAmlConfigAPIList.enableKeepLastFrame = false;
681 mAmlConfigAPIList.enableSetPts = false;
682 mAmlConfigAPIList.enableSetVideoPlane = false;
683 mCurrentDisplayOutput = &mOutput[0];
684 for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
685 mOutput[i].wlOutput = NULL;
686 mOutput[i].offsetX = 0;
687 mOutput[i].offsetY = 0;
688 mOutput[i].width = 0;
689 mOutput[i].height = 0;
690 mOutput[i].refreshRate = 0;
691 mOutput[i].isPrimary = false;
692 mOutput[i].name = 0;
693 mOutput[i].crtcIndex = 0;
694 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000695 char *name = require_xdg_runtime_dir();
696 //DEBUG(mLogCategory,"name:%s",name);
fei.dengb9a1a572023-09-13 01:33:57 +0000697 DEBUG(mLogCategory,"openDisplay in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000698 mWlDisplay = wl_display_connect(NULL);
699 if (!mWlDisplay) {
fei.dengb9a1a572023-09-13 01:33:57 +0000700 ERROR(mLogCategory,"Failed to connect to the wayland display, XDG_RUNTIME_DIR='%s'",
fei.dengf7a0cd32023-08-29 09:36:37 +0000701 name ? name : "NULL");
fei.dengb9a1a572023-09-13 01:33:57 +0000702 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000703 }
704
705 mWlDisplayWrapper = (struct wl_display *)wl_proxy_create_wrapper ((void *)mWlDisplay);
706 mWlQueue = wl_display_create_queue (mWlDisplay);
707 wl_proxy_set_queue ((struct wl_proxy *)mWlDisplayWrapper, mWlQueue);
708
709 mRegistry = wl_display_get_registry (mWlDisplayWrapper);
710 wl_registry_add_listener (mRegistry, &registry_listener, (void *)this);
711
712 /* we need exactly 2 roundtrips to discover global objects and their state */
713 for (int i = 0; i < 2; i++) {
714 if (wl_display_roundtrip_queue (mWlDisplay, mWlQueue) < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000715 ERROR(mLogCategory,"Error communicating with the wayland display");
716 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000717 }
718 }
719
720 if (!mCompositor) {
fei.dengb9a1a572023-09-13 01:33:57 +0000721 ERROR(mLogCategory,"Could not bind to wl_compositor. Either it is not implemented in " \
fei.dengf7a0cd32023-08-29 09:36:37 +0000722 "the compositor, or the implemented version doesn't match");
fei.dengb9a1a572023-09-13 01:33:57 +0000723 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000724 }
725
726 if (!mDmabuf) {
fei.dengb9a1a572023-09-13 01:33:57 +0000727 ERROR(mLogCategory,"Could not bind to zwp_linux_dmabuf_v1");
728 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000729 }
730
731 if (!mXdgWmBase) {
732 /* If wl_surface and wl_display are passed via GstContext
733 * wl_shell, xdg_shell and zwp_fullscreen_shell are not used.
734 * In this case is correct to continue.
735 */
fei.dengb9a1a572023-09-13 01:33:57 +0000736 ERROR(mLogCategory,"Could not bind to either wl_shell, xdg_wm_base or "
fei.dengf7a0cd32023-08-29 09:36:37 +0000737 "zwp_fullscreen_shell, video display may not work properly.");
fei.dengb9a1a572023-09-13 01:33:57 +0000738 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000739 }
740
741 //create window surface
742 createCommonWindowSurface();
743 createXdgShellWindowSurface();
744
745 //config weston video plane
fei.deng640c3c92024-04-12 08:31:19 +0000746 if (mAmlConfigAPIList.enableSetVideoPlane) {
fei.dengb9a1a572023-09-13 01:33:57 +0000747 INFO(mLogCategory,"set weston video plane:%d",mPip);
fei.dengf7a0cd32023-08-29 09:36:37 +0000748 wl_surface_set_video_plane(mVideoSurfaceWrapper, mPip);
749 }
750
fei.dengdd910ef2024-06-07 10:25:30 +0800751 //set keep last frame or not when starting playback,1 keep last frame, 0 not
752 setKeepLastFrame(mKeepLastFrame);
753
fei.deng640c3c92024-04-12 08:31:19 +0000754
fei.dengf7a0cd32023-08-29 09:36:37 +0000755 //run wl display queue dispatch
fei.dengb9a1a572023-09-13 01:33:57 +0000756 DEBUG(mLogCategory,"To run wl display dispatch queue");
fei.dengdd910ef2024-06-07 10:25:30 +0800757 if (mPoll) {
758 mPoll->setFlushing(false);
759 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000760 run("display queue");
761 mRedrawingPending = false;
762
fei.dengb9a1a572023-09-13 01:33:57 +0000763 DEBUG(mLogCategory,"openDisplay out");
764 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000765}
766
767void WaylandDisplay::closeDisplay()
768{
fei.dengb9a1a572023-09-13 01:33:57 +0000769 DEBUG(mLogCategory,"closeDisplay in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000770
771 if (isRunning()) {
fei.dengb9a1a572023-09-13 01:33:57 +0000772 TRACE(mLogCategory,"try stop dispatch thread");
773 if (mPoll) {
774 mPoll->setFlushing(true);
775 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000776 requestExitAndWait();
777 }
778
fei.dengdd910ef2024-06-07 10:25:30 +0800779 //flush event to weston
780 if (mWlDisplay) {
781 wl_display_flush (mWlDisplay);
782 }
783
fei.dengf7a0cd32023-08-29 09:36:37 +0000784 //first destroy window surface
785 destroyWindowSurfaces();
786
787 if (mViewporter) {
788 wp_viewporter_destroy (mViewporter);
789 mViewporter = NULL;
790 }
791
792 if (mDmabuf) {
793 zwp_linux_dmabuf_v1_destroy (mDmabuf);
794 mDmabuf = NULL;
795 }
796
797 if (mXdgWmBase) {
798 xdg_wm_base_destroy (mXdgWmBase);
799 mXdgWmBase = NULL;
800 }
801
802 if (mCompositor) {
803 wl_compositor_destroy (mCompositor);
804 mCompositor = NULL;
805 }
806
807 if (mSubCompositor) {
808 wl_subcompositor_destroy (mSubCompositor);
809 mSubCompositor = NULL;
810 }
811
812 if (mRegistry) {
813 wl_registry_destroy (mRegistry);
814 mRegistry= NULL;
815 }
816
817 if (mWlDisplayWrapper) {
818 wl_proxy_wrapper_destroy (mWlDisplayWrapper);
819 mWlDisplayWrapper = NULL;
820 }
821
fei.deng640c3c92024-04-12 08:31:19 +0000822 if (mAmlConfig) {
823 aml_config_destroy(mAmlConfig);
824 mAmlConfig = NULL;
825 }
826
fei.dengf7a0cd32023-08-29 09:36:37 +0000827 if (mWlQueue) {
828 wl_event_queue_destroy (mWlQueue);
829 mWlQueue = NULL;
830 }
831
832 if (mWlDisplay) {
833 wl_display_flush (mWlDisplay);
834 wl_display_disconnect (mWlDisplay);
835 mWlDisplay = NULL;
836 }
837
fei.dengb9a1a572023-09-13 01:33:57 +0000838 DEBUG(mLogCategory,"closeDisplay out");
fei.dengf7a0cd32023-08-29 09:36:37 +0000839}
840
841int WaylandDisplay::toDmaBufferFormat(RenderVideoFormat format, uint32_t *outDmaformat /*out param*/, uint64_t *outDmaformatModifiers /*out param*/)
842{
843 if (!outDmaformat || !outDmaformatModifiers) {
fei.dengb9a1a572023-09-13 01:33:57 +0000844 WARNING(mLogCategory,"NULL params");
845 return ERROR_PARAM_NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000846 }
847
848 *outDmaformat = 0;
849 *outDmaformatModifiers = 0;
850
851 uint32_t dmaformat = video_format_to_wl_dmabuf_format (format);
852 if (dmaformat == -1) {
fei.dengb9a1a572023-09-13 01:33:57 +0000853 ERROR(mLogCategory,"Error not found render video format:%d to wl dmabuf format",format);
854 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000855 }
856
fei.dengb9a1a572023-09-13 01:33:57 +0000857 TRACE(mLogCategory,"render video format:%d -> dmabuf format:%d",format,dmaformat);
fei.dengf7a0cd32023-08-29 09:36:37 +0000858 *outDmaformat = (uint32_t)dmaformat;
859
860 /*get dmaformat and modifiers*/
861 auto item = mDmaBufferFormats.find(dmaformat);
862 if (item == mDmaBufferFormats.end()) { //not found
fei.dengb9a1a572023-09-13 01:33:57 +0000863 WARNING(mLogCategory,"Not found dmabuf for render video format :%d",format);
fei.dengf7a0cd32023-08-29 09:36:37 +0000864 *outDmaformatModifiers = 0;
fei.dengb9a1a572023-09-13 01:33:57 +0000865 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000866 }
867
868 *outDmaformatModifiers = (uint64_t)item->second;
869
fei.dengb9a1a572023-09-13 01:33:57 +0000870 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000871}
872
873int WaylandDisplay::toShmBufferFormat(RenderVideoFormat format, uint32_t *outformat)
874{
875 if (!outformat) {
fei.dengb9a1a572023-09-13 01:33:57 +0000876 WARNING(mLogCategory,"NULL params");
877 return ERROR_PARAM_NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000878 }
879
880 *outformat = 0;
881
882 int shmformat = (int)video_format_to_wl_shm_format(format);
883 if (shmformat < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000884 ERROR(mLogCategory,"Error not found render video format:%d to wl shmbuf format",format);
885 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000886 }
887
888 for (auto item = mShmFormats.begin(); item != mShmFormats.end(); ++item) {
889 uint32_t registFormat = (uint32_t)*item;
890 if (registFormat == (uint32_t)shmformat) {
891 *outformat = registFormat;
fei.dengb9a1a572023-09-13 01:33:57 +0000892 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000893 }
894 }
895
fei.dengb9a1a572023-09-13 01:33:57 +0000896 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000897}
898
899void WaylandDisplay::setVideoBufferFormat(RenderVideoFormat format)
900{
fei.dengb9a1a572023-09-13 01:33:57 +0000901 TRACE(mLogCategory,"set video buffer format: %d",format);
fei.dengf7a0cd32023-08-29 09:36:37 +0000902 mBufferFormat = format;
903};
904
905void WaylandDisplay::setDisplayOutput(int output)
906{
fei.dengb9a1a572023-09-13 01:33:57 +0000907 TRACE(mLogCategory,"select display output: %d",output);
fei.dengf7a0cd32023-08-29 09:36:37 +0000908 if (output < 0 || output >= DEFAULT_DISPLAY_OUTPUT_NUM) {
fei.dengb9a1a572023-09-13 01:33:57 +0000909 ERROR(mLogCategory, "display output index error,please set 0:primary or 1:extend,now:%d",output);
fei.dengf7a0cd32023-08-29 09:36:37 +0000910 return;
911 }
fei.deng26950832023-11-09 03:00:23 +0000912 //only do select output before video playing
913 if (mSelectOutputIndex != output) {
914 mSelectOutputIndex = output;
915 // if (mOutput[output].wlOutput) {
916 // mCurrentDisplayOutput = &mOutput[output];
917 // setRenderRectangle(mOutput[output].offsetX, mOutput[output].offsetY,
918 // mOutput[output].width, mOutput[output].height);
919 // }
fei.dengf7a0cd32023-08-29 09:36:37 +0000920 }
921}
922
923int WaylandDisplay::getDisplayOutput()
924{
fei.deng26950832023-11-09 03:00:23 +0000925 return mSelectOutputIndex == INVALID_OUTPUT_INDEX? 0: mSelectOutputIndex;
fei.dengf7a0cd32023-08-29 09:36:37 +0000926}
927
928void WaylandDisplay::setPip(int pip)
929{
fei.dengb9a1a572023-09-13 01:33:57 +0000930 INFO(mLogCategory,"set pip:%d",pip);
fei.dengf7a0cd32023-08-29 09:36:37 +0000931 mPip = pip;
932}
933
fei.deng26950832023-11-09 03:00:23 +0000934void WaylandDisplay::updateDisplayOutput()
935{
936 if (!mCurrentDisplayOutput->wlOutput || !mXdgToplevel || !mXdgSurface)
937 {
938 return;
939 }
940 if (mUpdateRenderRectangle) {
941 if (mFullScreen) {
942 DEBUG(mLogCategory,"unset full screen");
943 xdg_toplevel_unset_fullscreen (mXdgToplevel);
944 }
945
946 if (mXdgSurface) {
947 DEBUG(mLogCategory,"set geometry");
948 xdg_surface_set_window_geometry(mXdgSurface,
949 mCurrentDisplayOutput->offsetX,
950 mCurrentDisplayOutput->offsetY,
951 mCurrentDisplayOutput->width,
952 mCurrentDisplayOutput->height);
953 }
954
955 if (mFullScreen && mXdgToplevel) {
956 DEBUG(mLogCategory,"set full screen");
957 xdg_toplevel_set_fullscreen (mXdgToplevel, mCurrentDisplayOutput->wlOutput);
958 }
959 setRenderRectangle(mCurrentDisplayOutput->offsetX, mCurrentDisplayOutput->offsetY,
960 mCurrentDisplayOutput->width, mCurrentDisplayOutput->height);
961 mUpdateRenderRectangle = false;
962 }
963}
964
fei.dengf7a0cd32023-08-29 09:36:37 +0000965void WaylandDisplay::createCommonWindowSurface()
966{
967 struct wl_region *region;
968
969 mAreaSurface = wl_compositor_create_surface (mCompositor);
970 mVideoSurface = wl_compositor_create_surface (mCompositor);
971 mAreaSurfaceWrapper = (struct wl_surface *)wl_proxy_create_wrapper (mAreaSurface);
972 mVideoSurfaceWrapper = (struct wl_surface *)wl_proxy_create_wrapper (mVideoSurface);
973
974 wl_proxy_set_queue ((struct wl_proxy *) mAreaSurfaceWrapper, mWlQueue);
975 wl_proxy_set_queue ((struct wl_proxy *) mVideoSurfaceWrapper, mWlQueue);
976
977 /* embed video_surface in area_surface */
978 mVideoSubSurface = wl_subcompositor_get_subsurface (mSubCompositor, mVideoSurface, mAreaSurface);
979 wl_subsurface_set_desync (mVideoSubSurface);
980
981 if (mViewporter) {
982 mAreaViewport = wp_viewporter_get_viewport (mViewporter, mAreaSurface);
983 mVideoViewport = wp_viewporter_get_viewport (mViewporter, mVideoSurface);
984 }
985
986 /* do not accept input */
987 region = wl_compositor_create_region (mCompositor);
988 wl_surface_set_input_region (mAreaSurface, region);
989 wl_region_destroy (region);
990
991 region = wl_compositor_create_region (mCompositor);
992 wl_surface_set_input_region (mVideoSurface, region);
993 wl_region_destroy (region);
994}
995
996void WaylandDisplay::createXdgShellWindowSurface()
997{
998 /* Check which protocol we will use (in order of preference) */
999 if (mXdgWmBase) {
1000 /* First create the XDG surface */
1001 mXdgSurface= xdg_wm_base_get_xdg_surface (mXdgWmBase, mAreaSurface);
1002 if (!mXdgSurface) {
fei.dengb9a1a572023-09-13 01:33:57 +00001003 ERROR(mLogCategory,"Unable to get xdg_surface");
fei.dengf7a0cd32023-08-29 09:36:37 +00001004 return;
1005 }
1006 xdg_surface_add_listener (mXdgSurface, &xdg_surface_listener,(void *)this);
1007
1008 /* Then the toplevel */
1009 mXdgToplevel= xdg_surface_get_toplevel (mXdgSurface);
1010 if (!mXdgSurface) {
fei.dengb9a1a572023-09-13 01:33:57 +00001011 ERROR(mLogCategory,"Unable to get xdg_toplevel");
fei.dengf7a0cd32023-08-29 09:36:37 +00001012 return;
1013 }
1014 xdg_toplevel_add_listener (mXdgToplevel, &xdg_toplevel_listener, this);
1015
1016 /* Finally, commit the xdg_surface state as toplevel */
1017 mXdgSurfaceConfigured = false;
1018 wl_surface_commit (mAreaSurface);
1019 wl_display_flush (mWlDisplay);
1020 /* we need exactly 3 roundtrips to discover global objects and their state */
1021 for (int i = 0; i < 3; i++) {
1022 if (wl_display_roundtrip_queue(mWlDisplay, mWlQueue) < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +00001023 ERROR(mLogCategory,"Error communicating with the wayland display");
fei.dengf7a0cd32023-08-29 09:36:37 +00001024 }
1025 }
1026
1027 if (mXdgSurfaceConfigured) {
fei.dengb9a1a572023-09-13 01:33:57 +00001028 INFO(mLogCategory,"xdg surface had configured");
fei.dengf7a0cd32023-08-29 09:36:37 +00001029 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001030 WARNING(mLogCategory,"xdg surface not configured");
fei.dengf7a0cd32023-08-29 09:36:37 +00001031 }
1032
1033 //full screen show
fei.deng26950832023-11-09 03:00:23 +00001034 // if (mFullScreen && mCurrentDisplayOutput->wlOutput) {
1035 // //ensureFullscreen(mFullScreen);
fei.dengf7a0cd32023-08-29 09:36:37 +00001036 // }
1037 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001038 ERROR(mLogCategory,"Unable to use xdg_wm_base ");
fei.dengf7a0cd32023-08-29 09:36:37 +00001039 return;
1040 }
1041}
1042
1043void WaylandDisplay::destroyWindowSurfaces()
1044{
1045 if (mAreaShmBuffer) {
1046 delete mAreaShmBuffer;
1047 mAreaShmBuffer = NULL;
1048 }
1049
1050 //clean all wayland buffers
1051 cleanAllWaylandBuffer();
1052
1053 if (mXdgToplevel) {
1054 xdg_toplevel_destroy (mXdgToplevel);
1055 mXdgToplevel = NULL;
1056 }
1057
1058 if (mXdgSurface) {
1059 xdg_surface_destroy (mXdgSurface);
1060 mXdgSurface = NULL;
1061 }
1062
1063 if (mVideoSurfaceWrapper) {
1064 wl_proxy_wrapper_destroy (mVideoSurfaceWrapper);
1065 mVideoSurfaceWrapper = NULL;
1066 }
1067
1068 if (mVideoSubSurface) {
1069 wl_subsurface_destroy (mVideoSubSurface);
1070 mVideoSubSurface = NULL;
1071 }
1072
1073 if (mVideoSurface) {
1074 wl_surface_destroy (mVideoSurface);
1075 mVideoSurface = NULL;
1076 }
1077
1078 if (mAreaSurfaceWrapper) {
1079 wl_proxy_wrapper_destroy (mAreaSurfaceWrapper);
1080 mAreaSurfaceWrapper = NULL;
1081 }
1082
1083 if (mAreaSurface) {
1084 wl_surface_destroy (mAreaSurface);
1085 mAreaSurface = NULL;
1086 mReCommitAreaSurface = false;
1087 }
1088}
1089
1090void WaylandDisplay::ensureFullscreen(bool fullscreen)
1091{
1092 if (mXdgWmBase) {
fei.dengb9a1a572023-09-13 01:33:57 +00001093 DEBUG(mLogCategory,"full screen : %d",fullscreen);
fei.dengf7a0cd32023-08-29 09:36:37 +00001094 if (fullscreen) {
fei.deng26950832023-11-09 03:00:23 +00001095 xdg_toplevel_set_fullscreen (mXdgToplevel, mCurrentDisplayOutput->wlOutput);
fei.dengf7a0cd32023-08-29 09:36:37 +00001096 } else {
1097 xdg_toplevel_unset_fullscreen (mXdgToplevel);
1098 }
1099 }
1100}
1101
1102void WaylandDisplay::setRenderRectangle(int x, int y, int w, int h)
1103{
fei.dengb9a1a572023-09-13 01:33:57 +00001104 DEBUG(mLogCategory,"set render rect:x:%d,y:%d,w:%d,h:%d",x,y,w,h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001105
1106 if (w <= 0 || h <= 0) {
fei.dengb9a1a572023-09-13 01:33:57 +00001107 WARNING(mLogCategory, "wrong render width or height %dx%d",w,h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001108 return;
1109 }
1110
1111 mRenderRect.x = x;
1112 mRenderRect.y = y;
1113 mRenderRect.w = w;
1114 mRenderRect.h = h;
1115
1116 if (!mXdgSurfaceConfigured) {
fei.dengb9a1a572023-09-13 01:33:57 +00001117 WARNING(mLogCategory,"Not configured xdg");
fei.dengf7a0cd32023-08-29 09:36:37 +00001118 return;
1119 }
1120
1121 if (mAreaViewport) {
1122 wp_viewport_set_destination (mAreaViewport, w, h);
1123 }
1124
1125 updateBorders();
1126
1127 if (mVideoWidth != 0 && mVideoSurface) {
1128 wl_subsurface_set_sync (mVideoSubSurface);
1129 resizeVideoSurface(true);
1130 }
1131
1132 wl_surface_damage (mAreaSurfaceWrapper, 0, 0, w, h);
1133 wl_surface_commit (mAreaSurfaceWrapper);
1134
1135 if (mVideoWidth != 0) {
1136 wl_subsurface_set_desync (mVideoSubSurface);
1137 }
1138}
1139
1140
1141void WaylandDisplay::setFrameSize(int w, int h)
1142{
1143 mVideoWidth = w;
1144 mVideoHeight = h;
fei.dengb9a1a572023-09-13 01:33:57 +00001145 TRACE(mLogCategory,"frame w:%d,h:%d",mVideoWidth,mVideoHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +00001146 if (mRenderRect.w > 0 && mVideoSurface) {
1147 resizeVideoSurface(true);
1148 }
1149}
1150
1151void WaylandDisplay::setWindowSize(int x, int y, int w, int h)
1152{
1153 mWindowRect.x = x;
1154 mWindowRect.y = y;
1155 mWindowRect.w = w;
1156 mWindowRect.h = h;
fei.dengb9a1a572023-09-13 01:33:57 +00001157 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 +00001158 if (mWindowRect.w > 0 && mVideoWidth > 0 && mVideoSurface) {
1159 //if had full screen, unset it and set window size
1160 if (mFullScreen) {
1161 mFullScreen = false;
1162 ensureFullscreen(mFullScreen);
1163 }
1164 resizeVideoSurface(true);
1165 }
1166}
1167
1168
1169void WaylandDisplay::resizeVideoSurface(bool commit)
1170{
1171 Rectangle src = {0,};
1172 Rectangle dst = {0,};
1173 Rectangle res;
1174
1175 /* center the video_subsurface inside area_subsurface */
1176 src.w = mVideoWidth;
1177 src.h = mVideoHeight;
1178 /*if had set the window size, we will scall
1179 video surface to this window size*/
1180 if (mWindowRect.w > 0 && mWindowRect.h > 0) {
1181 dst.x = mWindowRect.x;
1182 dst.y = mWindowRect.y;
1183 dst.w = mWindowRect.w;
1184 dst.h = mWindowRect.h;
1185 if (mWindowRect.w > mRenderRect.w && mWindowRect.h > mRenderRect.h) {
fei.dengb9a1a572023-09-13 01:33:57 +00001186 WARNING(mLogCategory,"Error window size:%dx%d, but render size:%dx%d,reset to render size",
fei.dengf7a0cd32023-08-29 09:36:37 +00001187 mWindowRect.w,mWindowRect.h,mRenderRect.w,mRenderRect.h);
1188 dst.x = mRenderRect.x;
1189 dst.y = mRenderRect.y;
1190 dst.w = mRenderRect.w;
1191 dst.h = mRenderRect.h;
1192 }
1193 //to do,we need set geometry?
1194 //if (mXdgSurface) {
1195 // xdg_surface_set_window_geometry(mXdgSurface, mWindowRect.x, mWindowRect.y, mWindowRect.w, mWindowRect.h);
1196 //}
1197 } else { //scal video to full screen
1198 dst.w = mRenderRect.w;
1199 dst.h = mRenderRect.h;
1200 }
1201
1202 if (mViewporter) {
1203 videoCenterRect(src, dst, &res, true);
1204 } else {
1205 videoCenterRect(src, dst, &res, false);
1206 }
1207
1208 wl_subsurface_set_position (mVideoSubSurface, res.x, res.y);
1209
1210 if (commit) {
1211 wl_surface_damage (mVideoSurfaceWrapper, 0, 0, res.w, res.h);
1212 wl_surface_commit (mVideoSurfaceWrapper);
1213 }
1214
1215 //top level setting
1216 if (mXdgToplevel) {
1217 struct wl_region *region;
1218
1219 region = wl_compositor_create_region (mCompositor);
1220 wl_region_add (region, 0, 0, mRenderRect.w, mRenderRect.h);
1221 wl_surface_set_input_region (mAreaSurface, region);
1222 wl_region_destroy (region);
1223 }
1224
1225 /* this is saved for use in wl_surface_damage */
1226 mVideoRect.x = res.x;
1227 mVideoRect.y = res.y;
1228 mVideoRect.w = res.w;
1229 mVideoRect.h = res.h;
1230
1231 //to scale video surface to full screen
1232 wp_viewport_set_destination(mVideoViewport, res.w, res.h);
fei.dengb9a1a572023-09-13 01:33:57 +00001233 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 +00001234}
1235
1236void WaylandDisplay::setOpaque()
1237{
1238 struct wl_region *region;
1239
1240 /* Set area opaque */
1241 region = wl_compositor_create_region (mCompositor);
1242 wl_region_add (region, 0, 0, mRenderRect.w, mRenderRect.h);
1243 wl_surface_set_opaque_region (mAreaSurface, region);
1244 wl_region_destroy (region);
1245}
1246
1247int WaylandDisplay::prepareFrameBuffer(RenderBuffer * buf)
1248{
1249 WaylandBuffer *waylandBuf = NULL;
1250 int ret;
1251
fei.dengdd910ef2024-06-07 10:25:30 +08001252 if (!mDmabuf)
1253 {
1254 ERROR(mLogCategory,"Error zwp_linux_dmabuf_v1");
1255 return ERROR_UNKNOWN;
1256 }
1257
fei.dengf7a0cd32023-08-29 09:36:37 +00001258 waylandBuf = findWaylandBuffer(buf);
1259 if (waylandBuf == NULL) {
fei.dengb9a1a572023-09-13 01:33:57 +00001260 waylandBuf = new WaylandBuffer(this, mLogCategory);
fei.dengf7a0cd32023-08-29 09:36:37 +00001261 waylandBuf->setBufferFormat(mBufferFormat);
1262 ret = waylandBuf->constructWlBuffer(buf);
fei.dengb9a1a572023-09-13 01:33:57 +00001263 if (ret != NO_ERROR) {
1264 WARNING(mLogCategory,"dmabufConstructWlBuffer fail,release waylandbuf");
fei.dengf7a0cd32023-08-29 09:36:37 +00001265 //delete waylanBuf,WaylandBuffer object destruct will call release callback
1266 goto waylandbuf_fail;
1267 } else {
1268 addWaylandBuffer(buf, waylandBuf);
1269 }
1270 }
fei.dengb9a1a572023-09-13 01:33:57 +00001271 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +00001272waylandbuf_fail:
1273 //delete waylandbuf
1274 delete waylandBuf;
1275 waylandBuf = NULL;
fei.dengb9a1a572023-09-13 01:33:57 +00001276 return ERROR_UNKNOWN;
fei.dengf7a0cd32023-08-29 09:36:37 +00001277}
1278
1279void WaylandDisplay::displayFrameBuffer(RenderBuffer * buf, int64_t realDisplayTime)
1280{
1281 WaylandBuffer *waylandBuf = NULL;
1282 struct wl_buffer * wlbuffer = NULL;
1283 int ret;
1284
1285 if (!buf) {
fei.dengb9a1a572023-09-13 01:33:57 +00001286 ERROR(mLogCategory,"Error input params, waylandbuffer is null");
fei.dengf7a0cd32023-08-29 09:36:37 +00001287 return;
1288 }
1289
1290 //must commit areasurface first, because weston xdg surface maybe timeout
1291 //this cause video is not display,commit can resume xdg surface
1292 if (!mReCommitAreaSurface) {
1293 mReCommitAreaSurface = true;
1294 wl_surface_commit (mAreaSurface);
1295 }
1296
fei.dengdd910ef2024-06-07 10:25:30 +08001297 //TRACE(mLogCategory,"display renderBuffer:%p,PTS:%lld us,realtime:%lld",buf, buf->pts/1000, realDisplayTime);
fei.dengf7a0cd32023-08-29 09:36:37 +00001298
1299 if (buf->flag & BUFFER_FLAG_DMA_BUFFER) {
1300 if (buf->dma.width <=0 || buf->dma.height <=0) {
1301 buf->dma.width = mVideoWidth;
1302 buf->dma.height = mVideoHeight;
1303 }
1304 waylandBuf = findWaylandBuffer(buf);
1305 if (waylandBuf) {
1306 waylandBuf->setRenderRealTime(realDisplayTime);
1307 ret = waylandBuf->constructWlBuffer(buf);
fei.dengb9a1a572023-09-13 01:33:57 +00001308 if (ret != NO_ERROR) {
1309 WARNING(mLogCategory,"dmabufConstructWlBuffer fail,release waylandbuf");
fei.dengf7a0cd32023-08-29 09:36:37 +00001310 //delete waylanBuf,WaylandBuffer object destruct will call release callback
1311 goto waylandbuf_fail;
1312 }
1313 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001314 ERROR(mLogCategory,"NOT found wayland buffer,please prepare buffer first");
fei.dengf7a0cd32023-08-29 09:36:37 +00001315 goto waylandbuf_fail;
1316 }
1317 }
1318
1319 if (waylandBuf) {
1320 wlbuffer = waylandBuf->getWlBuffer();
1321 }
fei.deng1cfb2752023-10-26 08:01:25 +00001322 //if no wl_output, drop this buffer
fei.deng26950832023-11-09 03:00:23 +00001323 if (mCurrentDisplayOutput->wlOutput == NULL) {
fei.deng1cfb2752023-10-26 08:01:25 +00001324 TRACE(mLogCategory,"No wl_output");
1325 mWaylandPlugin->handleFrameDropped(buf);
1326 mWaylandPlugin->handleBufferRelease(buf);
1327 return;
1328 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001329 if (wlbuffer) {
fei.dengb9a1a572023-09-13 01:33:57 +00001330 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001331 ++mCommitCnt;
1332 uint32_t hiPts = realDisplayTime >> 32;
1333 uint32_t lowPts = realDisplayTime & 0xFFFFFFFF;
1334 //attach this wl_buffer to weston
fei.dengdd910ef2024-06-07 10:25:30 +08001335 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 +00001336 waylandBuf->attach(mVideoSurfaceWrapper);
1337
fei.deng640c3c92024-04-12 08:31:19 +00001338 if (mAmlConfigAPIList.enableSetPts) {
fei.dengb9a1a572023-09-13 01:33:57 +00001339 TRACE(mLogCategory,"display time:%lld,hiPts:%u,lowPts:%u",realDisplayTime, hiPts, lowPts);
fei.dengf7a0cd32023-08-29 09:36:37 +00001340 wl_surface_set_pts(mVideoSurfaceWrapper, hiPts, lowPts);
1341 }
1342
1343 wl_surface_damage (mVideoSurfaceWrapper, 0, 0, mVideoRect.w, mVideoRect.h);
1344 wl_surface_commit (mVideoSurfaceWrapper);
1345 //insert this buffer to committed weston buffer manager
1346 std::pair<int64_t, WaylandBuffer *> item(buf->pts, waylandBuf);
1347 mCommittedBufferMap.insert(item);
1348 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001349 WARNING(mLogCategory,"wlbuffer is NULL");
fei.dengf7a0cd32023-08-29 09:36:37 +00001350 /* clear both video and parent surfaces */
1351 cleanSurface();
1352 }
1353
1354 wl_display_flush (mWlDisplay);
1355
1356 return;
1357waylandbuf_fail:
1358 //notify dropped
1359 mWaylandPlugin->handleFrameDropped(buf);
1360 //notify app release this buf
1361 mWaylandPlugin->handleBufferRelease(buf);
1362 //delete waylandbuf
1363 delete waylandBuf;
1364 waylandBuf = NULL;
1365 return;
1366}
1367
1368void WaylandDisplay::handleBufferReleaseCallback(WaylandBuffer *buf)
1369{
1370 {
fei.dengb9a1a572023-09-13 01:33:57 +00001371 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001372 //remove buffer if this buffer is ready to release
1373 RenderBuffer *renderBuffer = buf->getRenderBuffer();
1374 auto item = mCommittedBufferMap.find(renderBuffer->pts);
1375 if (item != mCommittedBufferMap.end()) {
fei.dengdd910ef2024-06-07 10:25:30 +08001376 --mCommitCnt;
fei.dengf7a0cd32023-08-29 09:36:37 +00001377 mCommittedBufferMap.erase(item);
1378 } else {
fei.dengdd910ef2024-06-07 10:25:30 +08001379 WARNING(mLogCategory,"Can't find WaylandBuffer pts:%lld us in buffer map",renderBuffer->pts/1000);
fei.dengf7a0cd32023-08-29 09:36:37 +00001380 return;
1381 }
1382 }
1383 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengdd910ef2024-06-07 10:25:30 +08001384 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 +00001385 mWaylandPlugin->handleBufferRelease(renderBuffer);
1386}
1387
1388void WaylandDisplay::handleFrameDisplayedCallback(WaylandBuffer *buf)
1389{
1390 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengb9a1a572023-09-13 01:33:57 +00001391 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 +00001392 if (!mSignalFirstFramePts) {
1393 mSignalFirstFramePts = true;
1394 mWaylandPlugin->handleMsgNotify(MSG_FIRST_FRAME, (void*)&renderBuffer->pts);
1395 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001396 mWaylandPlugin->handleFrameDisplayed(renderBuffer);
1397}
1398
1399void WaylandDisplay::handleFrameDropedCallback(WaylandBuffer *buf)
1400{
1401 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengb9a1a572023-09-13 01:33:57 +00001402 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 +00001403 mWaylandPlugin->handleFrameDropped(renderBuffer);
1404}
1405
1406
1407void WaylandDisplay::readyToRun()
1408{
1409 mFd = wl_display_get_fd (mWlDisplay);
fei.dengb9a1a572023-09-13 01:33:57 +00001410 if (mPoll) {
1411 mPoll->addFd(mFd);
1412 mPoll->setFdReadable(mFd, true);
1413 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001414}
1415
fei.dengdd910ef2024-06-07 10:25:30 +08001416void WaylandDisplay::readyToExit()
1417{
1418 if (mPoll && mFd >= 0) {
1419 mPoll->removeFd(mFd);
1420 }
1421}
1422
fei.dengf7a0cd32023-08-29 09:36:37 +00001423bool WaylandDisplay::threadLoop()
1424{
1425 int ret;
fei.dengf7a0cd32023-08-29 09:36:37 +00001426
1427 while (wl_display_prepare_read_queue (mWlDisplay, mWlQueue) != 0) {
1428 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1429 }
1430
1431 wl_display_flush (mWlDisplay);
1432
1433 /*poll timeout value must > 300 ms,otherwise zwp_linux_dmabuf will create failed,
1434 so do use -1 to wait for ever*/
fei.dengb9a1a572023-09-13 01:33:57 +00001435 ret = mPoll->wait(-1); //wait for ever
fei.dengf7a0cd32023-08-29 09:36:37 +00001436 if (ret < 0) { //poll error
fei.dengb9a1a572023-09-13 01:33:57 +00001437 WARNING(mLogCategory,"poll error");
fei.dengf7a0cd32023-08-29 09:36:37 +00001438 wl_display_cancel_read(mWlDisplay);
1439 return false;
1440 } else if (ret == 0) { //poll time out
1441 return true; //run loop
1442 }
1443
1444 if (wl_display_read_events (mWlDisplay) == -1) {
1445 goto tag_error;
1446 }
1447
1448 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1449 return true;
1450tag_error:
fei.dengb9a1a572023-09-13 01:33:57 +00001451 ERROR(mLogCategory,"Error communicating with the wayland server");
fei.dengf7a0cd32023-08-29 09:36:37 +00001452 return false;
1453}
1454
1455void WaylandDisplay::videoCenterRect(Rectangle src, Rectangle dst, Rectangle *result, bool scaling)
1456{
1457 //if dst is a small window, we scale video to map window size,don't doing center
1458 if (mRenderRect.w != dst.w && mRenderRect.h != dst.h) {
1459 result->x = dst.x;
1460 result->y = dst.y;
1461 result->w = dst.w;
1462 result->h = dst.h;
fei.dengb9a1a572023-09-13 01:33:57 +00001463 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 +00001464 src.w, src.h, dst.w, dst.h, result->w, result->h, result->x, result->y);
1465 return;
1466 }
1467 if (!scaling) {
1468 result->w = MIN (src.w, dst.w);
1469 result->h = MIN (src.h, dst.h);
1470 result->x = dst.x + (dst.w - result->w) / 2;
1471 result->y = dst.y + (dst.h - result->h) / 2;
1472 } else {
1473 double src_ratio, dst_ratio;
1474
1475 src_ratio = (double) src.w / src.h;
1476 dst_ratio = (double) dst.w / dst.h;
1477
1478 if (src_ratio > dst_ratio) {
1479 result->w = dst.w;
1480 result->h = dst.w / src_ratio;
1481 result->x = dst.x;
1482 result->y = dst.y + (dst.h - result->h) / 2;
1483 } else if (src_ratio < dst_ratio) {
1484 result->w = dst.h * src_ratio;
1485 result->h = dst.h;
1486 result->x = dst.x + (dst.w - result->w) / 2;
1487 result->y = dst.y;
1488 } else {
1489 result->x = dst.x;
1490 result->y = dst.y;
1491 result->w = dst.w;
1492 result->h = dst.h;
1493 }
1494 }
1495
fei.dengb9a1a572023-09-13 01:33:57 +00001496 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 +00001497 src.w, src.h, dst.w, dst.h, result->w, result->h, result->x, result->y);
1498}
1499
1500void WaylandDisplay::updateBorders()
1501{
1502 int width,height;
1503
1504 if (mNoBorderUpdate)
1505 return;
1506
1507 if (mViewporter) {
1508 width = height = 1;
1509 mNoBorderUpdate = true;
1510 } else {
1511 width = mRenderRect.w;
1512 height = mRenderRect.h;
1513 }
1514
1515 RenderVideoFormat format = VIDEO_FORMAT_BGRA;
fei.dengb9a1a572023-09-13 01:33:57 +00001516 mAreaShmBuffer = new WaylandShmBuffer(this, mLogCategory);
fei.dengf7a0cd32023-08-29 09:36:37 +00001517 struct wl_buffer *wlbuf = mAreaShmBuffer->constructWlBuffer(width, height, format);
1518 if (wlbuf == NULL) {
1519 delete mAreaShmBuffer;
1520 mAreaShmBuffer = NULL;
1521 }
1522
1523 wl_surface_attach(mAreaSurfaceWrapper, wlbuf, 0, 0);
1524}
1525
1526std::size_t WaylandDisplay::calculateDmaBufferHash(RenderDmaBuffer &dmabuf)
1527{
1528 std::string hashString("");
1529 for (int i = 0; i < dmabuf.planeCnt; i++) {
1530 char hashtmp[1024];
1531 snprintf (hashtmp, 1024, "%d%d%d%d%d%d", dmabuf.width,dmabuf.height,dmabuf.planeCnt,
1532 dmabuf.stride[i],dmabuf.offset[i],dmabuf.fd[i]);
1533 std::string tmp(hashtmp);
1534 hashString += tmp;
1535 }
1536
1537 std::size_t hashval = std::hash<std::string>()(hashString);
fei.dengb9a1a572023-09-13 01:33:57 +00001538 //TRACE(mLogCategory,"hashstr:%s,val:%zu",hashString.c_str(),hashval);
fei.dengf7a0cd32023-08-29 09:36:37 +00001539 return hashval;
1540}
1541
1542void WaylandDisplay::addWaylandBuffer(RenderBuffer * buf, WaylandBuffer *waylandbuf)
1543{
1544 if (buf->flag & BUFFER_FLAG_DMA_BUFFER) {
1545 std::size_t hashval = calculateDmaBufferHash(buf->dma);
1546 std::pair<std::size_t, WaylandBuffer *> item(hashval, waylandbuf);
fei.dengb9a1a572023-09-13 01:33:57 +00001547 //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 +00001548 mWaylandBuffersMap.insert(item);
1549 }
1550
1551 //clean invalid wayland buffer,if video resolution changed
1552 for (auto item = mWaylandBuffersMap.begin(); item != mWaylandBuffersMap.end(); ) {
1553 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1554 if ((waylandbuf->getFrameWidth() != buf->dma.width ||
1555 waylandbuf->getFrameHeight() != buf->dma.height) &&
1556 waylandbuf->isFree()) {
fei.dengb9a1a572023-09-13 01:33:57 +00001557 TRACE(mLogCategory,"delete wayland buffer,width:%d(%d),height:%d(%d)",
fei.dengf7a0cd32023-08-29 09:36:37 +00001558 waylandbuf->getFrameWidth(),buf->dma.width,
1559 waylandbuf->getFrameHeight(),buf->dma.height);
1560 mWaylandBuffersMap.erase(item++);
1561 delete waylandbuf;
1562 } else {
1563 item++;
1564 }
1565 }
fei.dengb9a1a572023-09-13 01:33:57 +00001566 TRACE(mLogCategory,"mWaylandBuffersMap size:%d",mWaylandBuffersMap.size());
fei.dengf7a0cd32023-08-29 09:36:37 +00001567}
1568
1569WaylandBuffer* WaylandDisplay::findWaylandBuffer(RenderBuffer * buf)
1570{
1571 std::size_t hashval = calculateDmaBufferHash(buf->dma);
1572 auto item = mWaylandBuffersMap.find(hashval);
1573 if (item == mWaylandBuffersMap.end()) {
1574 return NULL;
1575 }
1576
1577 return (WaylandBuffer*) item->second;
1578}
1579
1580void WaylandDisplay::cleanAllWaylandBuffer()
1581{
1582 //free all obtain buff
1583 for (auto item = mWaylandBuffersMap.begin(); item != mWaylandBuffersMap.end(); ) {
1584 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1585 mWaylandBuffersMap.erase(item++);
1586 delete waylandbuf;
1587 }
1588}
1589
1590void WaylandDisplay::flushBuffers()
1591{
fei.dengb9a1a572023-09-13 01:33:57 +00001592 INFO(mLogCategory,"flushBuffers");
1593 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001594 for (auto item = mCommittedBufferMap.begin(); item != mCommittedBufferMap.end(); item++) {
1595 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1596 waylandbuf->forceRedrawing();
1597 handleFrameDisplayedCallback(waylandbuf);
1598 }
1599}
1600
1601void WaylandDisplay::cleanSurface()
1602{
1603 /* clear both video and parent surfaces */
1604 wl_surface_attach (mVideoSurfaceWrapper, NULL, 0, 0);
1605 wl_surface_commit (mVideoSurfaceWrapper);
1606 wl_surface_attach (mAreaSurfaceWrapper, NULL, 0, 0);
1607 wl_surface_commit (mAreaSurfaceWrapper);
fei.deng640c3c92024-04-12 08:31:19 +00001608}
1609
1610void WaylandDisplay::setKeepLastFrame(int keep)
1611{
1612 mKeepLastFrame = keep;
1613 if (mVideoSurfaceWrapper && mAmlConfigAPIList.enableKeepLastFrame) {
1614 INFO(mLogCategory,"keep last frame:%d",keep);
1615 wl_surface_keep_last_frame(mVideoSurfaceWrapper, keep);
1616 }
fei.dengdd910ef2024-06-07 10:25:30 +08001617 //flush event to weston
1618 if (mWlDisplay) {
1619 wl_display_flush (mWlDisplay);
1620 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001621}