blob: 9332fbab169f8eafbdf1ae7d8d32b8a5ce016fe9 [file] [log] [blame]
fei.dengf7a0cd32023-08-29 09:36:37 +00001/*
2 * Copyright (C) 2021 Amlogic Corporation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include <string>
17#include "wayland_display.h"
fei.dengb9a1a572023-09-13 01:33:57 +000018#include "ErrorCode.h"
fei.dengf7a0cd32023-08-29 09:36:37 +000019#include "Logger.h"
20#include "wayland_plugin.h"
21#include "wayland_videoformat.h"
22#include "wayland_shm.h"
23#include "wayland_dma.h"
24#include "wayland_buffer.h"
25
26#ifndef MAX
27# define MAX(a,b) ((a) > (b)? (a) : (b))
28# define MIN(a,b) ((a) < (b)? (a) : (b))
29#endif
30
31#define UNUSED_PARAM(x) ((void)(x))
fei.deng26950832023-11-09 03:00:23 +000032#define INVALID_OUTPUT_INDEX (-1)
fei.dengf7a0cd32023-08-29 09:36:37 +000033
34#define TAG "rlib:wayland_display"
35
36void WaylandDisplay::dmabuf_modifiers(void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf,
37 uint32_t format, uint32_t modifier_hi, uint32_t modifier_lo)
38{
39 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
fei.dengb9a1a572023-09-13 01:33:57 +000040 Tls::Mutex::Autolock _l(self->mMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +000041 if (wl_dmabuf_format_to_video_format (format) != VIDEO_FORMAT_UNKNOWN) {
fei.dengb9a1a572023-09-13 01:33:57 +000042 TRACE(self->mLogCategory,"regist dmabuffer format:%d (%s) hi:%x,lo:%x",format,print_dmabuf_format_name(format),modifier_hi,modifier_lo);
fei.dengf7a0cd32023-08-29 09:36:37 +000043 uint64_t modifier = ((uint64_t)modifier_hi << 32) | modifier_lo;
44 auto item = self->mDmaBufferFormats.find(format);
45 if (item == self->mDmaBufferFormats.end()) {
46 std::pair<uint32_t ,uint64_t> item(format, modifier);
47 self->mDmaBufferFormats.insert(item);
48 } else { //found format
49 item->second = modifier;
50 }
51 }
52}
53
54void
55WaylandDisplay::dmaBufferFormat (void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf,
56 uint32_t format)
57{
58#if 0
59 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
60
61 if (wl_dmabuf_format_to_video_format (format) != VIDEO_FORMAT_UNKNOWN) {
62 TRACE(mLogCategory,"regist dmabuffer format:%d : %s",format);
63 //self->mDmaBufferFormats.push_back(format);
64 }
65#endif
66 /* XXX: deprecated */
67}
68
69static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
70 WaylandDisplay::dmaBufferFormat,
71 WaylandDisplay::dmabuf_modifiers
72};
73
74static void
75handle_xdg_wm_base_ping (void *user_data, struct xdg_wm_base *xdg_wm_base,
76 uint32_t serial)
77{
78 xdg_wm_base_pong (xdg_wm_base, serial);
79}
80
81static const struct xdg_wm_base_listener xdg_wm_base_listener = {
82 handle_xdg_wm_base_ping
83};
84
85void WaylandDisplay::shmFormat(void *data, struct wl_shm *wl_shm, uint32_t format)
86{
87 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
88 self->mShmFormats.push_back(format);
89}
90
91static const struct wl_shm_listener shm_listener = {
92 WaylandDisplay::shmFormat
93};
94
95void WaylandDisplay::outputHandleGeometry( void *data,
96 struct wl_output *output,
97 int x,
98 int y,
99 int physicalWidth,
100 int physicalHeight,
101 int subPixel,
102 const char *make,
103 const char *model,
104 int transform )
105{
106 UNUSED_PARAM(make);
107 UNUSED_PARAM(model);
108
109 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
fei.dengb9a1a572023-09-13 01:33:57 +0000110 DEBUG(self->mLogCategory,"wl_output %p x:%d,y:%d,physicalWidth:%d,physicalHeight:%d,subPixel:%d,trans:%d",
111 output,x, y,physicalWidth, physicalHeight,subPixel,transform);
112 Tls::Mutex::Autolock _l(self->mMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +0000113 for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
114 if (output == self->mOutput[i].wlOutput) {
115 self->mOutput[i].offsetX = x;
116 self->mOutput[i].offsetY = y;
117 }
118 }
119}
120
121void WaylandDisplay::outputHandleMode( void *data,
122 struct wl_output *output,
123 uint32_t flags,
124 int width,
125 int height,
126 int refreshRate )
127{
128 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
129
130 if ( flags & WL_OUTPUT_MODE_CURRENT ) {
fei.dengb9a1a572023-09-13 01:33:57 +0000131 Tls::Mutex::Autolock _l(self->mMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +0000132 for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
133 if (output == self->mOutput[i].wlOutput) {
134 self->mOutput[i].width = width;
135 self->mOutput[i].height = height;
136 self->mOutput[i].refreshRate = refreshRate;
137 }
138 }
fei.deng26950832023-11-09 03:00:23 +0000139
140 DEBUG(self->mLogCategory,"wl_output: %p (%dx%d) refreshrate:%d,select output index %d",output, width, height,refreshRate,self->mSelectOutputIndex);
141 if (self->mCurrentDisplayOutput->width > 0 &&
142 self->mCurrentDisplayOutput->height > 0) {
143 self->updateDisplayOutput();
fei.dengf7a0cd32023-08-29 09:36:37 +0000144 }
145 }
146}
147
148void WaylandDisplay::outputHandleDone( void *data,
149 struct wl_output *output )
150{
fei.dengf1f5fc32023-12-06 06:22:20 +0000151 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
152 DEBUG(self->mLogCategory,"wl_output: %p",output);
fei.dengf7a0cd32023-08-29 09:36:37 +0000153 UNUSED_PARAM(data);
154 UNUSED_PARAM(output);
155}
156
157void WaylandDisplay::outputHandleScale( void *data,
158 struct wl_output *output,
159 int32_t scale )
160{
fei.dengf1f5fc32023-12-06 06:22:20 +0000161 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
162 DEBUG(self->mLogCategory,"wl_output: %p scale %d",output, scale);
fei.dengf7a0cd32023-08-29 09:36:37 +0000163 UNUSED_PARAM(data);
164 UNUSED_PARAM(output);
165 UNUSED_PARAM(scale);
166}
167
fei.dengf1f5fc32023-12-06 06:22:20 +0000168void WaylandDisplay::outputHandleCrtcIndex( void *data,
169 struct wl_output *output,
170 int32_t index )
171{
172 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
173 DEBUG(self->mLogCategory,"wl_output: %p crtc index %d",output, index);
174 Tls::Mutex::Autolock _l(self->mMutex);
175 for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
176 if (output == self->mOutput[i].wlOutput) {
177 self->mOutput[i].crtcIndex = index;
178 }
179 }
180}
181
fei.deng640c3c92024-04-12 08:31:19 +0000182//aml weston always add crtcIndex in callbacks.
fei.dengf7a0cd32023-08-29 09:36:37 +0000183static const struct wl_output_listener outputListener = {
184 WaylandDisplay::outputHandleGeometry,
185 WaylandDisplay::outputHandleMode,
186 WaylandDisplay::outputHandleDone,
fei.dengf1f5fc32023-12-06 06:22:20 +0000187 WaylandDisplay::outputHandleScale,
188 WaylandDisplay::outputHandleCrtcIndex,
fei.dengf7a0cd32023-08-29 09:36:37 +0000189};
190
191void WaylandDisplay::pointerHandleEnter(void *data, struct wl_pointer *pointer,
192 uint32_t serial, struct wl_surface *surface,
193 wl_fixed_t sx, wl_fixed_t sy)
194{
195 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
196
197 if (self->mFullScreen) {
198 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
199 }
200}
201
202void WaylandDisplay::pointerHandleLeave(void *data, struct wl_pointer *pointer,
203 uint32_t serial, struct wl_surface *surface)
204{
205 UNUSED_PARAM(data);
206 UNUSED_PARAM(pointer);
207 UNUSED_PARAM(serial);
208 UNUSED_PARAM(surface);
209}
210
211void WaylandDisplay::pointerHandleMotion(void *data, struct wl_pointer *pointer,
212 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
213{
214 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
215 int x = wl_fixed_to_int(sx);
216 int y = wl_fixed_to_int(sy);
fei.dengb9a1a572023-09-13 01:33:57 +0000217 DEBUG(self->mLogCategory,"pointer motion fixed[%d, %d] to-int: x[%d] y[%d]\n", sx, sy, x, y);
fei.dengf7a0cd32023-08-29 09:36:37 +0000218}
219
220void WaylandDisplay::pointerHandleButton(void *data, struct wl_pointer *wl_pointer,
221 uint32_t serial, uint32_t time, uint32_t button, uint32_t state)
222{
223 UNUSED_PARAM(data);
224 UNUSED_PARAM(wl_pointer);
225 UNUSED_PARAM(time);
226 UNUSED_PARAM(serial);
227 UNUSED_PARAM(button);
228 UNUSED_PARAM(state);
229}
230
231void WaylandDisplay::pointerHandleAxis(void *data, struct wl_pointer *wl_pointer,
232 uint32_t time, uint32_t axis, wl_fixed_t value)
233{
234 UNUSED_PARAM(data);
235 UNUSED_PARAM(wl_pointer);
236 UNUSED_PARAM(time);
237 UNUSED_PARAM(axis);
238 UNUSED_PARAM(value);
239}
240
241static const struct wl_pointer_listener pointer_listener = {
242 WaylandDisplay::pointerHandleEnter,
243 WaylandDisplay::pointerHandleLeave,
244 WaylandDisplay::pointerHandleMotion,
245 WaylandDisplay::pointerHandleButton,
246 WaylandDisplay::pointerHandleAxis,
247};
248
249void WaylandDisplay::touchHandleDown(void *data, struct wl_touch *wl_touch,
250 uint32_t serial, uint32_t time, struct wl_surface *surface,
251 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
252{
253 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
254}
255
256void WaylandDisplay::touchHandleUp(void *data, struct wl_touch *wl_touch,
257 uint32_t serial, uint32_t time, int32_t id)
258{
259 UNUSED_PARAM(data);
260 UNUSED_PARAM(wl_touch);
261 UNUSED_PARAM(serial);
262 UNUSED_PARAM(time);
263 UNUSED_PARAM(id);
264}
265
266void WaylandDisplay::touchHandleMotion(void *data, struct wl_touch *wl_touch,
267 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
268{
269 UNUSED_PARAM(data);
270 UNUSED_PARAM(wl_touch);
271 UNUSED_PARAM(time);
272 UNUSED_PARAM(id);
273 UNUSED_PARAM(x_w);
274 UNUSED_PARAM(y_w);
275}
276
277void WaylandDisplay::touchHandleFrame(void *data, struct wl_touch *wl_touch)
278{
279 UNUSED_PARAM(data);
280 UNUSED_PARAM(wl_touch);
281}
282
283void WaylandDisplay::touchHandleCancel(void *data, struct wl_touch *wl_touch)
284{
285 UNUSED_PARAM(data);
286 UNUSED_PARAM(wl_touch);
287}
288
289static const struct wl_touch_listener touch_listener = {
290 WaylandDisplay::touchHandleDown,
291 WaylandDisplay::touchHandleUp,
292 WaylandDisplay::touchHandleMotion,
293 WaylandDisplay::touchHandleFrame,
294 WaylandDisplay::touchHandleCancel,
295};
296
297void WaylandDisplay::keyboardHandleKeymap(void *data, struct wl_keyboard *keyboard,
298 uint32_t format, int fd, uint32_t size)
299{
300 UNUSED_PARAM(data);
301 UNUSED_PARAM(keyboard);
302 UNUSED_PARAM(format);
303 UNUSED_PARAM(fd);
304 UNUSED_PARAM(size);
305}
306
307void WaylandDisplay::keyboardHandleEnter(void *data, struct wl_keyboard *keyboard,
308 uint32_t serial, struct wl_surface *surface, struct wl_array *keys)
309{
310 UNUSED_PARAM(data);
311 UNUSED_PARAM(keyboard);
312 UNUSED_PARAM(serial);
313 UNUSED_PARAM(surface);
314 UNUSED_PARAM(keys);
315}
316
317void WaylandDisplay::keyboardHandleLeave(void *data, struct wl_keyboard *keyboard,
318 uint32_t serial, struct wl_surface *surface)
319{
320 UNUSED_PARAM(data);
321 UNUSED_PARAM(keyboard);
322 UNUSED_PARAM(serial);
323 UNUSED_PARAM(surface);
324}
325
326void WaylandDisplay::keyboardHandleKey(void *data, struct wl_keyboard *keyboard,
327 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
328{
329 UNUSED_PARAM(data);
330 UNUSED_PARAM(keyboard);
331 UNUSED_PARAM(serial);
332 UNUSED_PARAM(time);
333 UNUSED_PARAM(key);
334 UNUSED_PARAM(state);
335}
336
337void WaylandDisplay::keyboardHandleModifiers(void *data, struct wl_keyboard *keyboard,
338 uint32_t serial, uint32_t mods_depressed,
339 uint32_t mods_latched, uint32_t mods_locked,
340 uint32_t group)
341{
342 UNUSED_PARAM(data);
343 UNUSED_PARAM(keyboard);
344 UNUSED_PARAM(serial);
345 UNUSED_PARAM(mods_depressed);
346 UNUSED_PARAM(mods_latched);
347 UNUSED_PARAM(mods_locked);
348 UNUSED_PARAM(group);
349}
350
351static const struct wl_keyboard_listener keyboard_listener = {
352 WaylandDisplay::keyboardHandleKeymap,
353 WaylandDisplay::keyboardHandleEnter,
354 WaylandDisplay::keyboardHandleLeave,
355 WaylandDisplay::keyboardHandleKey,
356 WaylandDisplay::keyboardHandleModifiers,
357};
358
359void WaylandDisplay::seatHandleCapabilities(void *data, struct wl_seat *seat,
360 uint32_t caps)
361{
362 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
363 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !self->mPointer) {
364 self->mPointer = wl_seat_get_pointer(seat);
365 wl_pointer_add_listener(self->mPointer, &pointer_listener, data);
366 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && self->mPointer) {
367 wl_pointer_destroy(self->mPointer);
368 self->mPointer = NULL;
369 }
370
371 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !self->mKeyboard) {
372 self->mKeyboard = wl_seat_get_keyboard(seat);
373 wl_keyboard_add_listener(self->mKeyboard, &keyboard_listener, data);
374 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && self->mKeyboard) {
375 wl_keyboard_destroy(self->mKeyboard);
376 self->mKeyboard = NULL;
377 }
378
379 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !self->mTouch) {
380 self->mTouch = wl_seat_get_touch(seat);
381 wl_touch_set_user_data(self->mTouch, data);
382 wl_touch_add_listener(self->mTouch, &touch_listener, data);
383 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && self->mTouch) {
384 wl_touch_destroy(self->mTouch);
385 self->mTouch = NULL;
386 }
387}
388
389static const struct wl_seat_listener seat_listener = {
390 WaylandDisplay::seatHandleCapabilities,
391};
392
393
394void WaylandDisplay::handleXdgToplevelClose (void *data, struct xdg_toplevel *xdg_toplevel)
395{
396 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
397
fei.dengb9a1a572023-09-13 01:33:57 +0000398 INFO(self->mLogCategory,"XDG toplevel got a close event.");
fei.dengf7a0cd32023-08-29 09:36:37 +0000399}
400
401void WaylandDisplay::handleXdgToplevelConfigure (void *data, struct xdg_toplevel *xdg_toplevel,
402 int32_t width, int32_t height, struct wl_array *states)
403{
404 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
405 uint32_t *state;
406
fei.dengb9a1a572023-09-13 01:33:57 +0000407 INFO(self->mLogCategory, "XDG toplevel got a configure event, width:height [ %d, %d ].", width, height);
fei.dengf7a0cd32023-08-29 09:36:37 +0000408 /*
409 wl_array_for_each (state, states) {
410 switch (*state) {
411 case XDG_TOPLEVEL_STATE_FULLSCREEN:
412 case XDG_TOPLEVEL_STATE_MAXIMIZED:
413 case XDG_TOPLEVEL_STATE_RESIZING:
414 case XDG_TOPLEVEL_STATE_ACTIVATED:
415 break;
416 }
417 }
418 */
419
420 if (width <= 0 || height <= 0)
421 return;
422
fei.deng26950832023-11-09 03:00:23 +0000423 if (width == self->mCurrentDisplayOutput->width && height == self->mCurrentDisplayOutput->height && self->mUpdateRenderRectangle) {
424 self->mUpdateRenderRectangle = false;
425 self->setRenderRectangle(self->mCurrentDisplayOutput->offsetX,
426 self->mCurrentDisplayOutput->offsetY,
427 self->mCurrentDisplayOutput->width,
428 self->mCurrentDisplayOutput->height);
fei.dengf7a0cd32023-08-29 09:36:37 +0000429 } else{
430 self->setRenderRectangle(self->mRenderRect.x, self->mRenderRect.y, width, height);
431 }
432}
433
434static const struct xdg_toplevel_listener xdg_toplevel_listener = {
435 WaylandDisplay::handleXdgToplevelConfigure,
436 WaylandDisplay::handleXdgToplevelClose,
437};
438
439void WaylandDisplay::handleXdgSurfaceConfigure (void *data, struct xdg_surface *xdg_surface,
440 uint32_t serial)
441{
442 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
443 xdg_surface_ack_configure (xdg_surface, serial);
444
fei.dengb9a1a572023-09-13 01:33:57 +0000445 TRACE(self->mLogCategory,"handleXdgSurfaceConfigure");
446 Tls::Mutex::Autolock _l(self->mConfigureMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +0000447 self->mXdgSurfaceConfigured = true;
fei.deng26950832023-11-09 03:00:23 +0000448 self->updateDisplayOutput();
fei.dengf7a0cd32023-08-29 09:36:37 +0000449}
450
451static const struct xdg_surface_listener xdg_surface_listener = {
452 WaylandDisplay::handleXdgSurfaceConfigure,
453};
454
fei.dengd0da4e22024-07-04 11:29:13 +0800455void WaylandDisplay::handleSurfaceDestroy(void *data, struct wl_callback *callback, uint32_t time)
456{
457 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
458 INFO(self->mLogCategory,"handle video surface destroy");
459 Tls::Mutex::Autolock _l(self->mMutex);
460 self->mCondition.signal();
461 wl_callback_destroy (callback);
462}
463
464static const struct wl_callback_listener surface_destroy_listener = {
465 WaylandDisplay::handleSurfaceDestroy,
466};
467
fei.deng640c3c92024-04-12 08:31:19 +0000468void WaylandDisplay::amlConfigure(void *data, struct aml_config *config, const char *list) {
469 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
470 TRACE(self->mLogCategory,"aml_config:%s",list);
471 if (list && strlen(list) > 0) {
472 if (strstr(list, "set_video_plane")) {
473 TRACE(self->mLogCategory,"weston enable set_video_plane");
474 self->mAmlConfigAPIList.enableSetVideoPlane = true;
475 }
476 if (strstr(list, "set_pts")) {
477 TRACE(self->mLogCategory,"weston enable set_pts");
478 self->mAmlConfigAPIList.enableSetPts = true;
479 }
480 if (strstr(list, "drop")) {
481 TRACE(self->mLogCategory,"weston enable drop");
482 self->mAmlConfigAPIList.enableDropFrame = true;
483 }
484 if (strstr(list, "keep_last_frame")) {
485 TRACE(self->mLogCategory,"weston enable keep_last_frame");
486 self->mAmlConfigAPIList.enableKeepLastFrame = true;
487 }
fei.dengd0da4e22024-07-04 11:29:13 +0800488 if (strstr(list, "surface_destroy_cb")) {
489 TRACE(self->mLogCategory,"weston enable surface_destroy_cb");
490 self->mAmlConfigAPIList.enableSurfaceDestroyCallback = true;
491 }
fei.deng6c425232024-07-19 16:15:31 +0800492 if (strstr(list, "set_display_rate")) {
493 TRACE(self->mLogCategory,"weston enable set_display_rate");
494 self->mAmlConfigAPIList.enableSetDisplayRate = true;
495 }
fei.deng9bf724e2024-08-01 11:34:52 +0800496 if (strstr(list, "set_surface_invisible")) {
497 TRACE(self->mLogCategory,"weston enable set_surface_invisible");
498 self->mAmlConfigAPIList.enableSetSurfaceInvisible = true;
499 }
fei.deng640c3c92024-04-12 08:31:19 +0000500 }
501}
502
503static const struct aml_config_listener aml_config_listener = {
504 WaylandDisplay::amlConfigure,
505};
506
fei.dengf7a0cd32023-08-29 09:36:37 +0000507void
508WaylandDisplay::registryHandleGlobal (void *data, struct wl_registry *registry,
fei.dengaf9b07d2023-10-10 07:38:40 +0000509 uint32_t name, const char *interface, uint32_t version)
fei.dengf7a0cd32023-08-29 09:36:37 +0000510{
511 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
fei.dengaf9b07d2023-10-10 07:38:40 +0000512 TRACE(self->mLogCategory,"registryHandleGlobal,name:%u,interface:%s,version:%d",name,interface,version);
fei.dengf7a0cd32023-08-29 09:36:37 +0000513
514 if (strcmp (interface, "wl_compositor") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000515 self->mCompositor = (struct wl_compositor *)wl_registry_bind (registry, name, &wl_compositor_interface, 1/*MIN (version, 3)*/);
fei.dengf7a0cd32023-08-29 09:36:37 +0000516 } else if (strcmp (interface, "wl_subcompositor") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000517 self->mSubCompositor = (struct wl_subcompositor *)wl_registry_bind (registry, name, &wl_subcompositor_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000518 } else if (strcmp (interface, "xdg_wm_base") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000519 self->mXdgWmBase = (struct xdg_wm_base *)wl_registry_bind (registry, name, &xdg_wm_base_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000520 xdg_wm_base_add_listener (self->mXdgWmBase, &xdg_wm_base_listener, (void *)self);
521 } else if (strcmp (interface, "wl_shm") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000522 self->mShm = (struct wl_shm *)wl_registry_bind (registry, name, &wl_shm_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000523 wl_shm_add_listener (self->mShm, &shm_listener, self);
524 } else if (strcmp (interface, "zwp_fullscreen_shell_v1") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000525 //self->mFullscreenShell = (struct zwp_fullscreen_shell_v1 *)wl_registry_bind (registry, name,
fei.dengf7a0cd32023-08-29 09:36:37 +0000526 // &zwp_fullscreen_shell_v1_interface, 1);
527 } else if (strcmp (interface, "wp_viewporter") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000528 self->mViewporter = (struct wp_viewporter *)wl_registry_bind (registry, name, &wp_viewporter_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000529 } else if (strcmp (interface, "zwp_linux_dmabuf_v1") == 0) {
530 if (version < 3)
531 return;
fei.dengaf9b07d2023-10-10 07:38:40 +0000532 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 +0000533 zwp_linux_dmabuf_v1_add_listener (self->mDmabuf, &dmabuf_listener, (void *)self);
534 } else if (strcmp (interface, "wl_output") == 0) {
fei.deng26950832023-11-09 03:00:23 +0000535 int i = 0;
536 uint32_t oriName = self->mCurrentDisplayOutput->name;
fei.dengaf9b07d2023-10-10 07:38:40 +0000537 for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
538 if (self->mOutput[i].wlOutput == NULL) {
539 self->mOutput[i].name = name;
540 self->mOutput[i].wlOutput = (struct wl_output*)wl_registry_bind(registry, name, &wl_output_interface, version);
fei.deng26950832023-11-09 03:00:23 +0000541 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 +0000542 wl_output_add_listener(self->mOutput[i].wlOutput, &outputListener, (void *)self);
543 if (i == 0) { //primary wl_output
544 self->mOutput[i].isPrimary = true;
545 }
fei.deng26950832023-11-09 03:00:23 +0000546 break;
fei.dengaf9b07d2023-10-10 07:38:40 +0000547 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000548 }
fei.deng26950832023-11-09 03:00:23 +0000549 if (i == DEFAULT_DISPLAY_OUTPUT_NUM) {
550 WARNING(self->mLogCategory,"Not enough free output");
551 }
552 //select current wl_output
553 if (self->mSelectOutputIndex != INVALID_OUTPUT_INDEX && !self->isRunning()) {
554 TRACE(self->mLogCategory,"select %d output",self->mSelectOutputIndex);
555 self->mCurrentDisplayOutput = &self->mOutput[self->mSelectOutputIndex];
556 }
557 //if user select a wrong output index, we using a suiteble wl_output
558 if (self->mCurrentDisplayOutput->wlOutput == NULL) {
559 WARNING(self->mLogCategory,"wl_output is null,we should find a suiteble output");
560 for (i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
561 if (self->mOutput[i].wlOutput) {
562 self->mCurrentDisplayOutput = &self->mOutput[i];
563 break;
564 }
565 }
566 }
567 //if current wl_output update, we should update render rectangle
568 if (self->mCurrentDisplayOutput->name != oriName) {
569 self->mUpdateRenderRectangle = true;
570 }
571 //if wl_output plugin,active sending frame
572 self->setRedrawingPending(false);
fei.dengf7a0cd32023-08-29 09:36:37 +0000573 } else if (strcmp(interface, "wl_seat") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000574 //self->mSeat = (struct wl_seat *)wl_registry_bind(registry, name, &wl_seat_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000575 //wl_seat_add_listener(self->mSeat, &seat_listener, (void *)self);
fei.deng31f93f12024-02-27 07:52:10 +0000576 } else if (strcmp(interface, "weston_direct_display_v1") == 0) {
577 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 +0000578 } else if (strcmp(interface, "aml_config") == 0) {
579 self->mAmlConfig = (struct aml_config*)wl_registry_bind(registry, name, &aml_config_interface, 1);
580 aml_config_add_listener(self->mAmlConfig, &aml_config_listener, (void *)self);
fei.dengf7a0cd32023-08-29 09:36:37 +0000581 }
582}
583
584void
585WaylandDisplay::registryHandleGlobalRemove (void *data, struct wl_registry *registry, uint32_t name)
586{
fei.deng26950832023-11-09 03:00:23 +0000587 int i;
fei.dengf7a0cd32023-08-29 09:36:37 +0000588 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
fei.dengaf9b07d2023-10-10 07:38:40 +0000589 /* check wl_output changed */
590 DEBUG(self->mLogCategory,"wayland display remove registry handle global,name:%u",name);
fei.deng26950832023-11-09 03:00:23 +0000591 //if user selected wl_output removed, reset selected output index
592 if (self->mSelectOutputIndex != INVALID_OUTPUT_INDEX &&
593 self->mOutput[self->mSelectOutputIndex].wlOutput &&
594 self->mOutput[self->mSelectOutputIndex].name == name) {
595 self->mSelectOutputIndex = INVALID_OUTPUT_INDEX;
596 }
597 for (i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000598 if (self->mOutput[i].name == name) {
fei.deng26950832023-11-09 03:00:23 +0000599 DEBUG(self->mLogCategory,"remove wl_output name:%u,wl_output:%p",name,self->mOutput[i].wlOutput);
fei.dengaf9b07d2023-10-10 07:38:40 +0000600 self->mOutput[i].name = 0;
601 self->mOutput[i].wlOutput = NULL;
fei.deng26950832023-11-09 03:00:23 +0000602 }
603 }
604 //if current output removed, select a suiteble output
605 if (self->mCurrentDisplayOutput->wlOutput == NULL) {
606 for (i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
607 if (self->mOutput[i].wlOutput) {
608 self->mCurrentDisplayOutput = &self->mOutput[i];
609 self->mUpdateRenderRectangle = true;
610 }
611 }
612 //set new output rectangle
613 if (self->mUpdateRenderRectangle) {
614 self->mUpdateRenderRectangle = false;
615 self->setRenderRectangle(self->mCurrentDisplayOutput->offsetX,
616 self->mCurrentDisplayOutput->offsetY,
617 self->mCurrentDisplayOutput->width,
618 self->mCurrentDisplayOutput->height);
fei.dengaf9b07d2023-10-10 07:38:40 +0000619 }
620 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000621}
622
623static const struct wl_registry_listener registry_listener = {
624 WaylandDisplay::registryHandleGlobal,
625 WaylandDisplay::registryHandleGlobalRemove
626};
627
fei.dengb9a1a572023-09-13 01:33:57 +0000628WaylandDisplay::WaylandDisplay(WaylandPlugin *plugin, int logCategory)
629 :mBufferMutex("bufferMutex"),
630 mWaylandPlugin(plugin),
631 mLogCategory(logCategory)
fei.dengf7a0cd32023-08-29 09:36:37 +0000632{
fei.dengb9a1a572023-09-13 01:33:57 +0000633 TRACE(mLogCategory,"construct WaylandDisplay");
fei.dengf7a0cd32023-08-29 09:36:37 +0000634 mWlDisplay = NULL;
635 mWlDisplayWrapper = NULL;
636 mWlQueue = NULL;
637 mRegistry = NULL;
638 mCompositor = NULL;
639 mXdgWmBase = NULL;
640 mViewporter = NULL;
641 mDmabuf = NULL;
642 mShm = NULL;
643 mSeat = NULL;
644 mPointer = NULL;
645 mTouch = NULL;
646 mKeyboard = NULL;
fei.deng31f93f12024-02-27 07:52:10 +0000647 mDirect_display = NULL;
fei.deng26950832023-11-09 03:00:23 +0000648 mSelectOutputIndex = INVALID_OUTPUT_INDEX;
fei.dengb9a1a572023-09-13 01:33:57 +0000649 mPoll = new Tls::Poll(true);
fei.dengf7a0cd32023-08-29 09:36:37 +0000650 //window
651 mVideoWidth = 0;
652 mVideoHeight = 0;
653 mVideoSurface = NULL;
654 mXdgSurface = NULL;
655 mXdgToplevel = NULL;
656 mAreaViewport = NULL;
657 mVideoViewport = NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000658 mAreaShmBuffer = NULL;
659 mCommitCnt = 0;
fei.dengf7a0cd32023-08-29 09:36:37 +0000660 mAreaSurface = NULL;
661 mAreaSurfaceWrapper = NULL;
662 mVideoSurfaceWrapper = NULL;
663 mVideoSubSurface = NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000664 mPip = 0;
fei.deng640c3c92024-04-12 08:31:19 +0000665 mKeepLastFrame = 0; //default is off keep last frame
fei.deng3287c082024-04-23 09:29:22 +0000666 mSignalFirstFramePts = false;
fei.deng4029e682024-06-26 17:06:31 +0800667 mToSendKeepLastFrame = false;
fei.dengf7a0cd32023-08-29 09:36:37 +0000668}
669
670WaylandDisplay::~WaylandDisplay()
671{
fei.dengb9a1a572023-09-13 01:33:57 +0000672 TRACE(mLogCategory,"desconstruct WaylandDisplay");
673 if (mPoll) {
674 delete mPoll;
675 mPoll = NULL;
676 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000677}
678
679char *WaylandDisplay::require_xdg_runtime_dir()
680{
681 char *val = getenv("XDG_RUNTIME_DIR");
fei.dengb9a1a572023-09-13 01:33:57 +0000682 INFO(mLogCategory,"XDG_RUNTIME_DIR=%s",val);
fei.dengdd910ef2024-06-07 10:25:30 +0800683 //if not set XDG_RUNTIME_DIR,set default value
684 // if (!val) {
685 // val = const_cast<char *>("/run/user/0");
686 // setenv("XDG_RUNTIME_DIR",val,0);
687 // WARNING(mLogCategory,"XDG_RUNTIME_DIR is not set,set default %s",val);
688 // }
fei.dengf7a0cd32023-08-29 09:36:37 +0000689
690 return val;
691}
692
693int WaylandDisplay::openDisplay()
694{
fei.denga4abbd52024-07-11 19:17:50 +0800695 mPixelAspectRatio = 1.0;
696 mUpdateVideoSurface = false;
fei.dengdd910ef2024-06-07 10:25:30 +0800697 mNoBorderUpdate = false;
698 mReCommitAreaSurface = false;
699 mXdgSurfaceConfigured = false;
700 mUpdateRenderRectangle = false;
fei.deng6c425232024-07-19 16:15:31 +0800701 mFrameRateFractionNum = 0;
702 mFrameRateFractionDenom = 1;
703 mFrameRateChanged = false;
fei.dengdd910ef2024-06-07 10:25:30 +0800704 memset(&mRenderRect, 0, sizeof(struct Rectangle));
705 memset(&mVideoRect, 0, sizeof(struct Rectangle));
706 memset(&mWindowRect, 0, sizeof(struct Rectangle));
707 mFullScreen = true; //default is full screen
708 mAmlConfig = NULL;
709 //weston config private api
710 mAmlConfigAPIList.enableDropFrame = false;
711 mAmlConfigAPIList.enableKeepLastFrame = false;
712 mAmlConfigAPIList.enableSetPts = false;
713 mAmlConfigAPIList.enableSetVideoPlane = false;
fei.dengd0da4e22024-07-04 11:29:13 +0800714 mAmlConfigAPIList.enableSurfaceDestroyCallback = false;
fei.deng6c425232024-07-19 16:15:31 +0800715 mAmlConfigAPIList.enableSetDisplayRate = false;
fei.deng9bf724e2024-08-01 11:34:52 +0800716 mAmlConfigAPIList.enableSetSurfaceInvisible = false;
fei.dengdd910ef2024-06-07 10:25:30 +0800717 mCurrentDisplayOutput = &mOutput[0];
718 for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
719 mOutput[i].wlOutput = NULL;
720 mOutput[i].offsetX = 0;
721 mOutput[i].offsetY = 0;
722 mOutput[i].width = 0;
723 mOutput[i].height = 0;
724 mOutput[i].refreshRate = 0;
725 mOutput[i].isPrimary = false;
726 mOutput[i].name = 0;
727 mOutput[i].crtcIndex = 0;
728 }
fei.deng4029e682024-06-26 17:06:31 +0800729 /*if mKeepLastFrame had set but mToSendKeepLastFrame is false,maybe
730 playback is doing FF/FW action,so we keep set it on new connection*/
731 if (!mToSendKeepLastFrame && mKeepLastFrame) {
732 mToSendKeepLastFrame = true;
733 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000734 char *name = require_xdg_runtime_dir();
735 //DEBUG(mLogCategory,"name:%s",name);
fei.dengb9a1a572023-09-13 01:33:57 +0000736 DEBUG(mLogCategory,"openDisplay in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000737 mWlDisplay = wl_display_connect(NULL);
738 if (!mWlDisplay) {
fei.dengb9a1a572023-09-13 01:33:57 +0000739 ERROR(mLogCategory,"Failed to connect to the wayland display, XDG_RUNTIME_DIR='%s'",
fei.dengf7a0cd32023-08-29 09:36:37 +0000740 name ? name : "NULL");
fei.dengb9a1a572023-09-13 01:33:57 +0000741 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000742 }
743
744 mWlDisplayWrapper = (struct wl_display *)wl_proxy_create_wrapper ((void *)mWlDisplay);
745 mWlQueue = wl_display_create_queue (mWlDisplay);
746 wl_proxy_set_queue ((struct wl_proxy *)mWlDisplayWrapper, mWlQueue);
747
748 mRegistry = wl_display_get_registry (mWlDisplayWrapper);
749 wl_registry_add_listener (mRegistry, &registry_listener, (void *)this);
750
751 /* we need exactly 2 roundtrips to discover global objects and their state */
752 for (int i = 0; i < 2; i++) {
753 if (wl_display_roundtrip_queue (mWlDisplay, mWlQueue) < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000754 ERROR(mLogCategory,"Error communicating with the wayland display");
755 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000756 }
757 }
758
759 if (!mCompositor) {
fei.dengb9a1a572023-09-13 01:33:57 +0000760 ERROR(mLogCategory,"Could not bind to wl_compositor. Either it is not implemented in " \
fei.dengf7a0cd32023-08-29 09:36:37 +0000761 "the compositor, or the implemented version doesn't match");
fei.dengb9a1a572023-09-13 01:33:57 +0000762 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000763 }
764
765 if (!mDmabuf) {
fei.dengb9a1a572023-09-13 01:33:57 +0000766 ERROR(mLogCategory,"Could not bind to zwp_linux_dmabuf_v1");
767 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000768 }
769
770 if (!mXdgWmBase) {
771 /* If wl_surface and wl_display are passed via GstContext
772 * wl_shell, xdg_shell and zwp_fullscreen_shell are not used.
773 * In this case is correct to continue.
774 */
fei.dengb9a1a572023-09-13 01:33:57 +0000775 ERROR(mLogCategory,"Could not bind to either wl_shell, xdg_wm_base or "
fei.dengf7a0cd32023-08-29 09:36:37 +0000776 "zwp_fullscreen_shell, video display may not work properly.");
fei.dengb9a1a572023-09-13 01:33:57 +0000777 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000778 }
779
780 //create window surface
781 createCommonWindowSurface();
782 createXdgShellWindowSurface();
783
784 //config weston video plane
fei.deng640c3c92024-04-12 08:31:19 +0000785 if (mAmlConfigAPIList.enableSetVideoPlane) {
fei.dengb9a1a572023-09-13 01:33:57 +0000786 INFO(mLogCategory,"set weston video plane:%d",mPip);
fei.dengf7a0cd32023-08-29 09:36:37 +0000787 wl_surface_set_video_plane(mVideoSurfaceWrapper, mPip);
788 }
789
790 //run wl display queue dispatch
fei.dengb9a1a572023-09-13 01:33:57 +0000791 DEBUG(mLogCategory,"To run wl display dispatch queue");
fei.dengdd910ef2024-06-07 10:25:30 +0800792 if (mPoll) {
793 mPoll->setFlushing(false);
794 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000795 run("display queue");
796 mRedrawingPending = false;
797
fei.dengb9a1a572023-09-13 01:33:57 +0000798 DEBUG(mLogCategory,"openDisplay out");
799 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000800}
801
802void WaylandDisplay::closeDisplay()
803{
fei.dengb9a1a572023-09-13 01:33:57 +0000804 DEBUG(mLogCategory,"closeDisplay in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000805
fei.dengd0da4e22024-07-04 11:29:13 +0800806 //first destroy window surface
807 destroyWindowSurfaces();
fei.dengf7a0cd32023-08-29 09:36:37 +0000808
fei.deng4029e682024-06-26 17:06:31 +0800809 //flush pending event to weston
fei.dengdd910ef2024-06-07 10:25:30 +0800810 if (mWlDisplay) {
811 wl_display_flush (mWlDisplay);
812 }
813
fei.dengd0da4e22024-07-04 11:29:13 +0800814 //wait video surface destroyed or 50ms timeout
815 if (mAmlConfigAPIList.enableSurfaceDestroyCallback) {
816 INFO(mLogCategory,"waiting surface_destroy_cb from weston");
817 Tls::Mutex::Autolock _l(mMutex);
818 if (ERROR_TIMED_OUT == mCondition.waitRelative(mMutex, 50/*microsecond*/)) {
819 WARNING(mLogCategory,"waited surface_destroy_cb timeout");
820 }
821 }
822
823 //we should receive all event from weston before stopped dispatch queue
824 if (isRunning()) {
825 TRACE(mLogCategory,"try stop dispatch thread");
826 if (mPoll) {
827 mPoll->setFlushing(true);
828 }
829 requestExitAndWait();
830 }
831 //after destroyed surface,then destroy buffers,otherwise maybe crash
832 if (mAreaShmBuffer) {
833 delete mAreaShmBuffer;
834 mAreaShmBuffer = NULL;
835 }
836
837 //clean all wayland buffers
838 cleanAllWaylandBuffer();
fei.dengf7a0cd32023-08-29 09:36:37 +0000839
840 if (mViewporter) {
841 wp_viewporter_destroy (mViewporter);
842 mViewporter = NULL;
843 }
844
845 if (mDmabuf) {
846 zwp_linux_dmabuf_v1_destroy (mDmabuf);
847 mDmabuf = NULL;
848 }
849
850 if (mXdgWmBase) {
851 xdg_wm_base_destroy (mXdgWmBase);
852 mXdgWmBase = NULL;
853 }
854
855 if (mCompositor) {
856 wl_compositor_destroy (mCompositor);
857 mCompositor = NULL;
858 }
859
860 if (mSubCompositor) {
861 wl_subcompositor_destroy (mSubCompositor);
862 mSubCompositor = NULL;
863 }
864
865 if (mRegistry) {
866 wl_registry_destroy (mRegistry);
867 mRegistry= NULL;
868 }
869
870 if (mWlDisplayWrapper) {
871 wl_proxy_wrapper_destroy (mWlDisplayWrapper);
872 mWlDisplayWrapper = NULL;
873 }
874
fei.deng640c3c92024-04-12 08:31:19 +0000875 if (mAmlConfig) {
876 aml_config_destroy(mAmlConfig);
877 mAmlConfig = NULL;
878 }
879
fei.dengf7a0cd32023-08-29 09:36:37 +0000880 if (mWlQueue) {
881 wl_event_queue_destroy (mWlQueue);
882 mWlQueue = NULL;
883 }
884
885 if (mWlDisplay) {
886 wl_display_flush (mWlDisplay);
887 wl_display_disconnect (mWlDisplay);
888 mWlDisplay = NULL;
889 }
890
fei.dengb9a1a572023-09-13 01:33:57 +0000891 DEBUG(mLogCategory,"closeDisplay out");
fei.dengf7a0cd32023-08-29 09:36:37 +0000892}
893
894int WaylandDisplay::toDmaBufferFormat(RenderVideoFormat format, uint32_t *outDmaformat /*out param*/, uint64_t *outDmaformatModifiers /*out param*/)
895{
896 if (!outDmaformat || !outDmaformatModifiers) {
fei.dengb9a1a572023-09-13 01:33:57 +0000897 WARNING(mLogCategory,"NULL params");
898 return ERROR_PARAM_NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000899 }
900
901 *outDmaformat = 0;
902 *outDmaformatModifiers = 0;
903
904 uint32_t dmaformat = video_format_to_wl_dmabuf_format (format);
905 if (dmaformat == -1) {
fei.dengb9a1a572023-09-13 01:33:57 +0000906 ERROR(mLogCategory,"Error not found render video format:%d to wl dmabuf format",format);
907 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000908 }
909
fei.dengb9a1a572023-09-13 01:33:57 +0000910 TRACE(mLogCategory,"render video format:%d -> dmabuf format:%d",format,dmaformat);
fei.dengf7a0cd32023-08-29 09:36:37 +0000911 *outDmaformat = (uint32_t)dmaformat;
912
913 /*get dmaformat and modifiers*/
914 auto item = mDmaBufferFormats.find(dmaformat);
915 if (item == mDmaBufferFormats.end()) { //not found
fei.dengb9a1a572023-09-13 01:33:57 +0000916 WARNING(mLogCategory,"Not found dmabuf for render video format :%d",format);
fei.dengf7a0cd32023-08-29 09:36:37 +0000917 *outDmaformatModifiers = 0;
fei.dengb9a1a572023-09-13 01:33:57 +0000918 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000919 }
920
921 *outDmaformatModifiers = (uint64_t)item->second;
922
fei.dengb9a1a572023-09-13 01:33:57 +0000923 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000924}
925
926int WaylandDisplay::toShmBufferFormat(RenderVideoFormat format, uint32_t *outformat)
927{
928 if (!outformat) {
fei.dengb9a1a572023-09-13 01:33:57 +0000929 WARNING(mLogCategory,"NULL params");
930 return ERROR_PARAM_NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000931 }
932
933 *outformat = 0;
934
935 int shmformat = (int)video_format_to_wl_shm_format(format);
936 if (shmformat < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000937 ERROR(mLogCategory,"Error not found render video format:%d to wl shmbuf format",format);
938 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000939 }
940
941 for (auto item = mShmFormats.begin(); item != mShmFormats.end(); ++item) {
942 uint32_t registFormat = (uint32_t)*item;
943 if (registFormat == (uint32_t)shmformat) {
944 *outformat = registFormat;
fei.dengb9a1a572023-09-13 01:33:57 +0000945 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000946 }
947 }
948
fei.dengb9a1a572023-09-13 01:33:57 +0000949 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000950}
951
952void WaylandDisplay::setVideoBufferFormat(RenderVideoFormat format)
953{
fei.dengb9a1a572023-09-13 01:33:57 +0000954 TRACE(mLogCategory,"set video buffer format: %d",format);
fei.dengf7a0cd32023-08-29 09:36:37 +0000955 mBufferFormat = format;
956};
957
958void WaylandDisplay::setDisplayOutput(int output)
959{
fei.dengb9a1a572023-09-13 01:33:57 +0000960 TRACE(mLogCategory,"select display output: %d",output);
fei.dengf7a0cd32023-08-29 09:36:37 +0000961 if (output < 0 || output >= DEFAULT_DISPLAY_OUTPUT_NUM) {
fei.dengb9a1a572023-09-13 01:33:57 +0000962 ERROR(mLogCategory, "display output index error,please set 0:primary or 1:extend,now:%d",output);
fei.dengf7a0cd32023-08-29 09:36:37 +0000963 return;
964 }
fei.deng26950832023-11-09 03:00:23 +0000965 //only do select output before video playing
966 if (mSelectOutputIndex != output) {
967 mSelectOutputIndex = output;
968 // if (mOutput[output].wlOutput) {
969 // mCurrentDisplayOutput = &mOutput[output];
970 // setRenderRectangle(mOutput[output].offsetX, mOutput[output].offsetY,
971 // mOutput[output].width, mOutput[output].height);
972 // }
fei.dengf7a0cd32023-08-29 09:36:37 +0000973 }
974}
975
976int WaylandDisplay::getDisplayOutput()
977{
fei.deng26950832023-11-09 03:00:23 +0000978 return mSelectOutputIndex == INVALID_OUTPUT_INDEX? 0: mSelectOutputIndex;
fei.dengf7a0cd32023-08-29 09:36:37 +0000979}
980
981void WaylandDisplay::setPip(int pip)
982{
fei.dengb9a1a572023-09-13 01:33:57 +0000983 INFO(mLogCategory,"set pip:%d",pip);
fei.dengf7a0cd32023-08-29 09:36:37 +0000984 mPip = pip;
985}
986
fei.deng26950832023-11-09 03:00:23 +0000987void WaylandDisplay::updateDisplayOutput()
988{
989 if (!mCurrentDisplayOutput->wlOutput || !mXdgToplevel || !mXdgSurface)
990 {
991 return;
992 }
993 if (mUpdateRenderRectangle) {
994 if (mFullScreen) {
995 DEBUG(mLogCategory,"unset full screen");
996 xdg_toplevel_unset_fullscreen (mXdgToplevel);
997 }
998
999 if (mXdgSurface) {
1000 DEBUG(mLogCategory,"set geometry");
1001 xdg_surface_set_window_geometry(mXdgSurface,
1002 mCurrentDisplayOutput->offsetX,
1003 mCurrentDisplayOutput->offsetY,
1004 mCurrentDisplayOutput->width,
1005 mCurrentDisplayOutput->height);
1006 }
1007
1008 if (mFullScreen && mXdgToplevel) {
1009 DEBUG(mLogCategory,"set full screen");
1010 xdg_toplevel_set_fullscreen (mXdgToplevel, mCurrentDisplayOutput->wlOutput);
1011 }
1012 setRenderRectangle(mCurrentDisplayOutput->offsetX, mCurrentDisplayOutput->offsetY,
1013 mCurrentDisplayOutput->width, mCurrentDisplayOutput->height);
1014 mUpdateRenderRectangle = false;
1015 }
1016}
1017
fei.dengf7a0cd32023-08-29 09:36:37 +00001018void WaylandDisplay::createCommonWindowSurface()
1019{
1020 struct wl_region *region;
1021
1022 mAreaSurface = wl_compositor_create_surface (mCompositor);
1023 mVideoSurface = wl_compositor_create_surface (mCompositor);
1024 mAreaSurfaceWrapper = (struct wl_surface *)wl_proxy_create_wrapper (mAreaSurface);
1025 mVideoSurfaceWrapper = (struct wl_surface *)wl_proxy_create_wrapper (mVideoSurface);
1026
1027 wl_proxy_set_queue ((struct wl_proxy *) mAreaSurfaceWrapper, mWlQueue);
1028 wl_proxy_set_queue ((struct wl_proxy *) mVideoSurfaceWrapper, mWlQueue);
1029
1030 /* embed video_surface in area_surface */
1031 mVideoSubSurface = wl_subcompositor_get_subsurface (mSubCompositor, mVideoSurface, mAreaSurface);
1032 wl_subsurface_set_desync (mVideoSubSurface);
1033
fei.dengd0da4e22024-07-04 11:29:13 +08001034 //add video surface callback to weston if weston enable this feature
1035 if (mVideoSurface && mAmlConfigAPIList.enableSurfaceDestroyCallback) {
1036 struct wl_callback *surfaceDestroyCb = wl_surface_destroy_callback(mVideoSurface);
1037 wl_callback_add_listener(surfaceDestroyCb, &surface_destroy_listener, this);
1038 }
1039
fei.dengf7a0cd32023-08-29 09:36:37 +00001040 if (mViewporter) {
1041 mAreaViewport = wp_viewporter_get_viewport (mViewporter, mAreaSurface);
1042 mVideoViewport = wp_viewporter_get_viewport (mViewporter, mVideoSurface);
1043 }
1044
fei.deng9bf724e2024-08-01 11:34:52 +08001045 /*set area surface to invisible. prevent frame droped at start playback,
1046 wl_surface_set_invisible must called before mAreaSurface commit called*/
1047 if (mAmlConfigAPIList.enableSetSurfaceInvisible) {
1048 int invisible = 1;
1049 INFO(mLogCategory,"set surface invisible:%d",invisible);
1050 wl_surface_set_invisible(mAreaSurfaceWrapper, invisible);
1051 }
1052
fei.dengf7a0cd32023-08-29 09:36:37 +00001053 /* do not accept input */
1054 region = wl_compositor_create_region (mCompositor);
1055 wl_surface_set_input_region (mAreaSurface, region);
1056 wl_region_destroy (region);
1057
1058 region = wl_compositor_create_region (mCompositor);
1059 wl_surface_set_input_region (mVideoSurface, region);
1060 wl_region_destroy (region);
1061}
1062
1063void WaylandDisplay::createXdgShellWindowSurface()
1064{
1065 /* Check which protocol we will use (in order of preference) */
1066 if (mXdgWmBase) {
1067 /* First create the XDG surface */
1068 mXdgSurface= xdg_wm_base_get_xdg_surface (mXdgWmBase, mAreaSurface);
1069 if (!mXdgSurface) {
fei.dengb9a1a572023-09-13 01:33:57 +00001070 ERROR(mLogCategory,"Unable to get xdg_surface");
fei.dengf7a0cd32023-08-29 09:36:37 +00001071 return;
1072 }
1073 xdg_surface_add_listener (mXdgSurface, &xdg_surface_listener,(void *)this);
1074
1075 /* Then the toplevel */
1076 mXdgToplevel= xdg_surface_get_toplevel (mXdgSurface);
1077 if (!mXdgSurface) {
fei.dengb9a1a572023-09-13 01:33:57 +00001078 ERROR(mLogCategory,"Unable to get xdg_toplevel");
fei.dengf7a0cd32023-08-29 09:36:37 +00001079 return;
1080 }
1081 xdg_toplevel_add_listener (mXdgToplevel, &xdg_toplevel_listener, this);
1082
1083 /* Finally, commit the xdg_surface state as toplevel */
1084 mXdgSurfaceConfigured = false;
1085 wl_surface_commit (mAreaSurface);
1086 wl_display_flush (mWlDisplay);
1087 /* we need exactly 3 roundtrips to discover global objects and their state */
1088 for (int i = 0; i < 3; i++) {
1089 if (wl_display_roundtrip_queue(mWlDisplay, mWlQueue) < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +00001090 ERROR(mLogCategory,"Error communicating with the wayland display");
fei.dengf7a0cd32023-08-29 09:36:37 +00001091 }
1092 }
1093
1094 if (mXdgSurfaceConfigured) {
fei.dengb9a1a572023-09-13 01:33:57 +00001095 INFO(mLogCategory,"xdg surface had configured");
fei.dengf7a0cd32023-08-29 09:36:37 +00001096 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001097 WARNING(mLogCategory,"xdg surface not configured");
fei.dengf7a0cd32023-08-29 09:36:37 +00001098 }
1099
1100 //full screen show
fei.deng26950832023-11-09 03:00:23 +00001101 // if (mFullScreen && mCurrentDisplayOutput->wlOutput) {
1102 // //ensureFullscreen(mFullScreen);
fei.dengf7a0cd32023-08-29 09:36:37 +00001103 // }
1104 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001105 ERROR(mLogCategory,"Unable to use xdg_wm_base ");
fei.dengf7a0cd32023-08-29 09:36:37 +00001106 return;
1107 }
1108}
1109
1110void WaylandDisplay::destroyWindowSurfaces()
1111{
fei.dengf7a0cd32023-08-29 09:36:37 +00001112 if (mXdgToplevel) {
1113 xdg_toplevel_destroy (mXdgToplevel);
1114 mXdgToplevel = NULL;
1115 }
1116
1117 if (mXdgSurface) {
1118 xdg_surface_destroy (mXdgSurface);
1119 mXdgSurface = NULL;
1120 }
1121
1122 if (mVideoSurfaceWrapper) {
1123 wl_proxy_wrapper_destroy (mVideoSurfaceWrapper);
1124 mVideoSurfaceWrapper = NULL;
1125 }
1126
1127 if (mVideoSubSurface) {
1128 wl_subsurface_destroy (mVideoSubSurface);
1129 mVideoSubSurface = NULL;
1130 }
1131
1132 if (mVideoSurface) {
1133 wl_surface_destroy (mVideoSurface);
1134 mVideoSurface = NULL;
1135 }
1136
1137 if (mAreaSurfaceWrapper) {
1138 wl_proxy_wrapper_destroy (mAreaSurfaceWrapper);
1139 mAreaSurfaceWrapper = NULL;
1140 }
1141
1142 if (mAreaSurface) {
1143 wl_surface_destroy (mAreaSurface);
1144 mAreaSurface = NULL;
1145 mReCommitAreaSurface = false;
1146 }
1147}
1148
1149void WaylandDisplay::ensureFullscreen(bool fullscreen)
1150{
1151 if (mXdgWmBase) {
fei.dengb9a1a572023-09-13 01:33:57 +00001152 DEBUG(mLogCategory,"full screen : %d",fullscreen);
fei.dengf7a0cd32023-08-29 09:36:37 +00001153 if (fullscreen) {
fei.deng26950832023-11-09 03:00:23 +00001154 xdg_toplevel_set_fullscreen (mXdgToplevel, mCurrentDisplayOutput->wlOutput);
fei.dengf7a0cd32023-08-29 09:36:37 +00001155 } else {
1156 xdg_toplevel_unset_fullscreen (mXdgToplevel);
1157 }
1158 }
1159}
1160
1161void WaylandDisplay::setRenderRectangle(int x, int y, int w, int h)
1162{
fei.dengb9a1a572023-09-13 01:33:57 +00001163 DEBUG(mLogCategory,"set render rect:x:%d,y:%d,w:%d,h:%d",x,y,w,h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001164
1165 if (w <= 0 || h <= 0) {
fei.dengb9a1a572023-09-13 01:33:57 +00001166 WARNING(mLogCategory, "wrong render width or height %dx%d",w,h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001167 return;
1168 }
1169
1170 mRenderRect.x = x;
1171 mRenderRect.y = y;
1172 mRenderRect.w = w;
1173 mRenderRect.h = h;
1174
1175 if (!mXdgSurfaceConfigured) {
fei.dengb9a1a572023-09-13 01:33:57 +00001176 WARNING(mLogCategory,"Not configured xdg");
fei.dengf7a0cd32023-08-29 09:36:37 +00001177 return;
1178 }
1179
1180 if (mAreaViewport) {
1181 wp_viewport_set_destination (mAreaViewport, w, h);
1182 }
1183
1184 updateBorders();
1185
1186 if (mVideoWidth != 0 && mVideoSurface) {
1187 wl_subsurface_set_sync (mVideoSubSurface);
1188 resizeVideoSurface(true);
1189 }
1190
1191 wl_surface_damage (mAreaSurfaceWrapper, 0, 0, w, h);
1192 wl_surface_commit (mAreaSurfaceWrapper);
1193
1194 if (mVideoWidth != 0) {
1195 wl_subsurface_set_desync (mVideoSubSurface);
1196 }
1197}
1198
fei.dengf7a0cd32023-08-29 09:36:37 +00001199void WaylandDisplay::setFrameSize(int w, int h)
1200{
1201 mVideoWidth = w;
1202 mVideoHeight = h;
fei.denga4abbd52024-07-11 19:17:50 +08001203 mUpdateVideoSurface = true;
fei.dengb9a1a572023-09-13 01:33:57 +00001204 TRACE(mLogCategory,"frame w:%d,h:%d",mVideoWidth,mVideoHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +00001205}
1206
1207void WaylandDisplay::setWindowSize(int x, int y, int w, int h)
1208{
1209 mWindowRect.x = x;
1210 mWindowRect.y = y;
1211 mWindowRect.w = w;
1212 mWindowRect.h = h;
fei.denga4abbd52024-07-11 19:17:50 +08001213 mUpdateVideoSurface = true;
fei.dengb9a1a572023-09-13 01:33:57 +00001214 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 +00001215}
1216
fei.denga4abbd52024-07-11 19:17:50 +08001217void WaylandDisplay::setPixelAspectRatio(double ratio)
1218{
1219 mPixelAspectRatio = ratio;
1220 mUpdateVideoSurface = true;
1221}
fei.dengf7a0cd32023-08-29 09:36:37 +00001222
1223void WaylandDisplay::resizeVideoSurface(bool commit)
1224{
1225 Rectangle src = {0,};
1226 Rectangle dst = {0,};
1227 Rectangle res;
1228
1229 /* center the video_subsurface inside area_subsurface */
1230 src.w = mVideoWidth;
1231 src.h = mVideoHeight;
1232 /*if had set the window size, we will scall
1233 video surface to this window size*/
1234 if (mWindowRect.w > 0 && mWindowRect.h > 0) {
1235 dst.x = mWindowRect.x;
1236 dst.y = mWindowRect.y;
1237 dst.w = mWindowRect.w;
1238 dst.h = mWindowRect.h;
1239 if (mWindowRect.w > mRenderRect.w && mWindowRect.h > mRenderRect.h) {
fei.dengb9a1a572023-09-13 01:33:57 +00001240 WARNING(mLogCategory,"Error window size:%dx%d, but render size:%dx%d,reset to render size",
fei.dengf7a0cd32023-08-29 09:36:37 +00001241 mWindowRect.w,mWindowRect.h,mRenderRect.w,mRenderRect.h);
1242 dst.x = mRenderRect.x;
1243 dst.y = mRenderRect.y;
1244 dst.w = mRenderRect.w;
1245 dst.h = mRenderRect.h;
1246 }
1247 //to do,we need set geometry?
1248 //if (mXdgSurface) {
1249 // xdg_surface_set_window_geometry(mXdgSurface, mWindowRect.x, mWindowRect.y, mWindowRect.w, mWindowRect.h);
1250 //}
1251 } else { //scal video to full screen
1252 dst.w = mRenderRect.w;
1253 dst.h = mRenderRect.h;
1254 }
1255
1256 if (mViewporter) {
1257 videoCenterRect(src, dst, &res, true);
1258 } else {
1259 videoCenterRect(src, dst, &res, false);
1260 }
1261
1262 wl_subsurface_set_position (mVideoSubSurface, res.x, res.y);
1263
1264 if (commit) {
1265 wl_surface_damage (mVideoSurfaceWrapper, 0, 0, res.w, res.h);
1266 wl_surface_commit (mVideoSurfaceWrapper);
1267 }
1268
1269 //top level setting
1270 if (mXdgToplevel) {
1271 struct wl_region *region;
1272
1273 region = wl_compositor_create_region (mCompositor);
1274 wl_region_add (region, 0, 0, mRenderRect.w, mRenderRect.h);
1275 wl_surface_set_input_region (mAreaSurface, region);
1276 wl_region_destroy (region);
1277 }
1278
1279 /* this is saved for use in wl_surface_damage */
1280 mVideoRect.x = res.x;
1281 mVideoRect.y = res.y;
1282 mVideoRect.w = res.w;
1283 mVideoRect.h = res.h;
1284
fei.denga4abbd52024-07-11 19:17:50 +08001285 //to scale video surface
fei.dengf7a0cd32023-08-29 09:36:37 +00001286 wp_viewport_set_destination(mVideoViewport, res.w, res.h);
fei.denga4abbd52024-07-11 19:17:50 +08001287 wl_display_flush (mWlDisplay);
fei.dengb9a1a572023-09-13 01:33:57 +00001288 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 +00001289}
1290
1291void WaylandDisplay::setOpaque()
1292{
1293 struct wl_region *region;
1294
1295 /* Set area opaque */
1296 region = wl_compositor_create_region (mCompositor);
1297 wl_region_add (region, 0, 0, mRenderRect.w, mRenderRect.h);
1298 wl_surface_set_opaque_region (mAreaSurface, region);
1299 wl_region_destroy (region);
1300}
1301
1302int WaylandDisplay::prepareFrameBuffer(RenderBuffer * buf)
1303{
1304 WaylandBuffer *waylandBuf = NULL;
1305 int ret;
fei.dengda0cd9f2024-07-24 09:25:11 +08001306 bool isNew = false;
fei.dengf7a0cd32023-08-29 09:36:37 +00001307
fei.dengdd910ef2024-06-07 10:25:30 +08001308 if (!mDmabuf)
1309 {
1310 ERROR(mLogCategory,"Error zwp_linux_dmabuf_v1");
1311 return ERROR_UNKNOWN;
1312 }
1313
fei.dengf7a0cd32023-08-29 09:36:37 +00001314 waylandBuf = findWaylandBuffer(buf);
1315 if (waylandBuf == NULL) {
fei.dengb9a1a572023-09-13 01:33:57 +00001316 waylandBuf = new WaylandBuffer(this, mLogCategory);
fei.dengda0cd9f2024-07-24 09:25:11 +08001317 isNew = true;
1318 }
1319 waylandBuf->setBufferFormat(mBufferFormat);
1320 ret = waylandBuf->constructWlBuffer(buf);
1321 if (ret != NO_ERROR) {
1322 WARNING(mLogCategory,"dmabufConstructWlBuffer fail,release waylandbuf");
1323 //delete waylanBuf,WaylandBuffer object destruct will call release callback
1324 goto waylandbuf_fail;
1325 }
1326 if (isNew) {
1327 addWaylandBuffer(buf, waylandBuf);
fei.dengf7a0cd32023-08-29 09:36:37 +00001328 }
fei.dengb9a1a572023-09-13 01:33:57 +00001329 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +00001330waylandbuf_fail:
1331 //delete waylandbuf
1332 delete waylandBuf;
1333 waylandBuf = NULL;
fei.dengb9a1a572023-09-13 01:33:57 +00001334 return ERROR_UNKNOWN;
fei.dengf7a0cd32023-08-29 09:36:37 +00001335}
1336
1337void WaylandDisplay::displayFrameBuffer(RenderBuffer * buf, int64_t realDisplayTime)
1338{
fei.deng19b48692024-08-13 14:17:55 +08001339 WaylandBuffer *waylandBuf = NULL;
1340 struct wl_buffer * wlbuffer = NULL;
1341 int ret;
1342
1343 if (!buf) {
1344 ERROR(mLogCategory,"Error input params, RenderBuffer is null");
1345 return;
1346 }
1347
1348 if (buf->flag & BUFFER_FLAG_DMA_BUFFER) {
1349 if (buf->dma.width <=0 || buf->dma.height <=0) {
1350 buf->dma.width = mVideoWidth;
1351 buf->dma.height = mVideoHeight;
1352 }
1353
1354 waylandBuf = findWaylandBuffer(buf);
1355 if (waylandBuf) {
1356 waylandBuf->setRenderRealTime(realDisplayTime);
1357 } else {
1358 ERROR(mLogCategory,"NOT found wayland buffer,please prepare buffer first");
1359 goto waylandbuf_fail;
1360 }
1361 }
1362
1363 //if no wl_output, drop this buffer
1364 if (mCurrentDisplayOutput->wlOutput == NULL) {
1365 TRACE(mLogCategory,"No wl_output");
1366 //insert this buffer to committed weston buffer manager
1367 std::pair<int64_t, WaylandBuffer *> item(realDisplayTime, waylandBuf);
1368 mCommittedBufferMap.insert(item);
1369 mWaylandPlugin->handleFrameDropped(buf);
1370 WaylandBuffer::bufferRelease(waylandBuf,NULL);
1371 return;
1372 }
1373
1374 //must commit areasurface first, because weston xdg surface maybe timeout
1375 //this cause video is not display,commit can resume xdg surface
1376 if (!mReCommitAreaSurface) {
1377 mReCommitAreaSurface = true;
1378 wl_surface_commit (mAreaSurface);
1379 }
1380
fei.deng6c425232024-07-19 16:15:31 +08001381 //set frame rate to weston,it lets weston to select suitable mode
1382 if (mFrameRateChanged && mAmlConfigAPIList.enableSetDisplayRate) {
1383 mFrameRateChanged = false;
1384 TRACE(mLogCategory,"set frame rate %d/%d to weston", mFrameRateFractionNum, mFrameRateFractionDenom);
1385 wl_surface_set_display_rate(mVideoSurfaceWrapper, mFrameRateFractionNum, mFrameRateFractionDenom);
1386 }
fei.denga4abbd52024-07-11 19:17:50 +08001387 //update video surface size
1388 if (mUpdateVideoSurface && mVideoSurface &&
1389 buf->dma.width == mVideoWidth && buf->dma.height == mVideoHeight) {
1390 mUpdateVideoSurface = false;
1391 //if had full screen, unset it and set window size
1392 if (mFullScreen) {
1393 mFullScreen = false;
1394 ensureFullscreen(mFullScreen);
1395 }
1396 resizeVideoSurface(true);
fei.dengda0cd9f2024-07-24 09:25:11 +08001397 /*clean wayland buffers those allocated
1398 before resolution changed and had release by weston */
1399 cleanWaylandBufferBeforeResChanged();
fei.denga4abbd52024-07-11 19:17:50 +08001400 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001401
fei.dengdd910ef2024-06-07 10:25:30 +08001402 //TRACE(mLogCategory,"display renderBuffer:%p,PTS:%lld us,realtime:%lld",buf, buf->pts/1000, realDisplayTime);
fei.dengf7a0cd32023-08-29 09:36:37 +00001403
fei.dengf7a0cd32023-08-29 09:36:37 +00001404 if (waylandBuf) {
1405 wlbuffer = waylandBuf->getWlBuffer();
1406 }
fei.deng19b48692024-08-13 14:17:55 +08001407
fei.dengf7a0cd32023-08-29 09:36:37 +00001408 if (wlbuffer) {
fei.dengb9a1a572023-09-13 01:33:57 +00001409 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001410 ++mCommitCnt;
1411 uint32_t hiPts = realDisplayTime >> 32;
1412 uint32_t lowPts = realDisplayTime & 0xFFFFFFFF;
1413 //attach this wl_buffer to weston
fei.denga4abbd52024-07-11 19:17:50 +08001414 TRACE(mLogCategory,"++attach,renderbuf:%p,wl_buffer:%p(%d,%d,%d,%d),pts:%lld us,commitCnt:%d",
1415 buf,wlbuffer,mVideoRect.x,mVideoRect.y,mVideoRect.w,mVideoRect.h,buf->pts/1000,mCommitCnt);
fei.dengf7a0cd32023-08-29 09:36:37 +00001416 waylandBuf->attach(mVideoSurfaceWrapper);
1417
fei.deng640c3c92024-04-12 08:31:19 +00001418 if (mAmlConfigAPIList.enableSetPts) {
fei.dengb9a1a572023-09-13 01:33:57 +00001419 TRACE(mLogCategory,"display time:%lld,hiPts:%u,lowPts:%u",realDisplayTime, hiPts, lowPts);
fei.dengf7a0cd32023-08-29 09:36:37 +00001420 wl_surface_set_pts(mVideoSurfaceWrapper, hiPts, lowPts);
1421 }
1422
1423 wl_surface_damage (mVideoSurfaceWrapper, 0, 0, mVideoRect.w, mVideoRect.h);
1424 wl_surface_commit (mVideoSurfaceWrapper);
1425 //insert this buffer to committed weston buffer manager
fei.dengae8c90a2024-06-27 13:39:53 +08001426 std::pair<int64_t, WaylandBuffer *> item(realDisplayTime, waylandBuf);
fei.dengf7a0cd32023-08-29 09:36:37 +00001427 mCommittedBufferMap.insert(item);
1428 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001429 WARNING(mLogCategory,"wlbuffer is NULL");
fei.dengf7a0cd32023-08-29 09:36:37 +00001430 /* clear both video and parent surfaces */
fei.deng19b48692024-08-13 14:17:55 +08001431 //cleanSurface();
1432 goto waylandbuf_fail;
fei.dengf7a0cd32023-08-29 09:36:37 +00001433 }
1434
1435 wl_display_flush (mWlDisplay);
fei.deng4029e682024-06-26 17:06:31 +08001436 //set keep last frame or not when after send first buffer to weston,1 keep last frame, 0 not
1437 if (mToSendKeepLastFrame) {
1438 setKeepLastFrame(mKeepLastFrame);
1439 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001440
1441 return;
1442waylandbuf_fail:
1443 //notify dropped
1444 mWaylandPlugin->handleFrameDropped(buf);
1445 //notify app release this buf
1446 mWaylandPlugin->handleBufferRelease(buf);
fei.dengf7a0cd32023-08-29 09:36:37 +00001447 return;
1448}
1449
1450void WaylandDisplay::handleBufferReleaseCallback(WaylandBuffer *buf)
1451{
fei.denga4abbd52024-07-11 19:17:50 +08001452 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengf7a0cd32023-08-29 09:36:37 +00001453 {
fei.dengb9a1a572023-09-13 01:33:57 +00001454 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001455 //remove buffer if this buffer is ready to release
fei.dengae8c90a2024-06-27 13:39:53 +08001456 auto item = mCommittedBufferMap.find(buf->getRenderRealTime());
fei.dengf7a0cd32023-08-29 09:36:37 +00001457 if (item != mCommittedBufferMap.end()) {
fei.dengdd910ef2024-06-07 10:25:30 +08001458 --mCommitCnt;
fei.dengf7a0cd32023-08-29 09:36:37 +00001459 mCommittedBufferMap.erase(item);
1460 } else {
fei.denga4abbd52024-07-11 19:17:50 +08001461 TRACE(mLogCategory,"Error,Can't find WaylandBuffer pts:%lld us (%lld) in buffer map",
1462 renderBuffer->pts/1000,buf->getRenderRealTime());
fei.dengf7a0cd32023-08-29 09:36:37 +00001463 return;
1464 }
1465 }
fei.denga4abbd52024-07-11 19:17:50 +08001466
1467 TRACE(mLogCategory,"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 +00001468 mWaylandPlugin->handleBufferRelease(renderBuffer);
1469}
1470
1471void WaylandDisplay::handleFrameDisplayedCallback(WaylandBuffer *buf)
1472{
1473 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.denga4abbd52024-07-11 19:17:50 +08001474 TRACE(mLogCategory,"renderBuffer :%p,PTS:%lld us,realtime:%lld us",renderBuffer,renderBuffer->pts/1000,buf->getRenderRealTime());
fei.deng3287c082024-04-23 09:29:22 +00001475 if (!mSignalFirstFramePts) {
1476 mSignalFirstFramePts = true;
1477 mWaylandPlugin->handleMsgNotify(MSG_FIRST_FRAME, (void*)&renderBuffer->pts);
1478 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001479 mWaylandPlugin->handleFrameDisplayed(renderBuffer);
1480}
1481
1482void WaylandDisplay::handleFrameDropedCallback(WaylandBuffer *buf)
1483{
1484 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.denga4abbd52024-07-11 19:17:50 +08001485 TRACE(mLogCategory,"renderBuffer :%p,PTS:%lld us,realtime:%lld us",renderBuffer,renderBuffer->pts/1000,buf->getRenderRealTime());
fei.dengf7a0cd32023-08-29 09:36:37 +00001486 mWaylandPlugin->handleFrameDropped(renderBuffer);
1487}
1488
1489
1490void WaylandDisplay::readyToRun()
1491{
1492 mFd = wl_display_get_fd (mWlDisplay);
fei.dengb9a1a572023-09-13 01:33:57 +00001493 if (mPoll) {
1494 mPoll->addFd(mFd);
1495 mPoll->setFdReadable(mFd, true);
1496 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001497}
1498
fei.dengdd910ef2024-06-07 10:25:30 +08001499void WaylandDisplay::readyToExit()
1500{
1501 if (mPoll && mFd >= 0) {
1502 mPoll->removeFd(mFd);
1503 }
1504}
1505
fei.dengf7a0cd32023-08-29 09:36:37 +00001506bool WaylandDisplay::threadLoop()
1507{
1508 int ret;
fei.dengf7a0cd32023-08-29 09:36:37 +00001509
1510 while (wl_display_prepare_read_queue (mWlDisplay, mWlQueue) != 0) {
1511 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1512 }
1513
1514 wl_display_flush (mWlDisplay);
1515
1516 /*poll timeout value must > 300 ms,otherwise zwp_linux_dmabuf will create failed,
1517 so do use -1 to wait for ever*/
fei.dengb9a1a572023-09-13 01:33:57 +00001518 ret = mPoll->wait(-1); //wait for ever
fei.dengf7a0cd32023-08-29 09:36:37 +00001519 if (ret < 0) { //poll error
fei.dengb9a1a572023-09-13 01:33:57 +00001520 WARNING(mLogCategory,"poll error");
fei.dengf7a0cd32023-08-29 09:36:37 +00001521 wl_display_cancel_read(mWlDisplay);
1522 return false;
1523 } else if (ret == 0) { //poll time out
1524 return true; //run loop
1525 }
1526
1527 if (wl_display_read_events (mWlDisplay) == -1) {
1528 goto tag_error;
1529 }
1530
1531 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1532 return true;
1533tag_error:
fei.dengb9a1a572023-09-13 01:33:57 +00001534 ERROR(mLogCategory,"Error communicating with the wayland server");
fei.dengf7a0cd32023-08-29 09:36:37 +00001535 return false;
1536}
1537
1538void WaylandDisplay::videoCenterRect(Rectangle src, Rectangle dst, Rectangle *result, bool scaling)
1539{
1540 //if dst is a small window, we scale video to map window size,don't doing center
fei.dengda0cd9f2024-07-24 09:25:11 +08001541 // if (mRenderRect.w != dst.w && mRenderRect.h != dst.h) {
1542 // result->x = dst.x;
1543 // result->y = dst.y;
1544 // result->w = dst.w;
1545 // result->h = dst.h;
1546 // TRACE(mLogCategory,"small window source is %dx%d dest is %dx%d, result is %d,%d,%d,%d",
1547 // src.w, src.h, dst.w, dst.h, result->x, result->y, result->w, result->h);
1548 // return;
1549 // }
fei.dengf7a0cd32023-08-29 09:36:37 +00001550 if (!scaling) {
1551 result->w = MIN (src.w, dst.w);
1552 result->h = MIN (src.h, dst.h);
1553 result->x = dst.x + (dst.w - result->w) / 2;
1554 result->y = dst.y + (dst.h - result->h) / 2;
1555 } else {
1556 double src_ratio, dst_ratio;
1557
fei.denga4abbd52024-07-11 19:17:50 +08001558 src_ratio = (double) (src.w * mPixelAspectRatio) / src.h;
fei.dengf7a0cd32023-08-29 09:36:37 +00001559 dst_ratio = (double) dst.w / dst.h;
1560
1561 if (src_ratio > dst_ratio) {
1562 result->w = dst.w;
1563 result->h = dst.w / src_ratio;
1564 result->x = dst.x;
1565 result->y = dst.y + (dst.h - result->h) / 2;
1566 } else if (src_ratio < dst_ratio) {
1567 result->w = dst.h * src_ratio;
1568 result->h = dst.h;
1569 result->x = dst.x + (dst.w - result->w) / 2;
1570 result->y = dst.y;
1571 } else {
1572 result->x = dst.x;
1573 result->y = dst.y;
1574 result->w = dst.w;
1575 result->h = dst.h;
1576 }
1577 }
1578
fei.denga4abbd52024-07-11 19:17:50 +08001579 TRACE(mLogCategory,"source is %dx%d dest is %dx%d, result is %d,%d,%d,%d",
1580 src.w, src.h, dst.w, dst.h, result->x, result->y,result->w, result->h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001581}
1582
1583void WaylandDisplay::updateBorders()
1584{
1585 int width,height;
1586
1587 if (mNoBorderUpdate)
1588 return;
1589
1590 if (mViewporter) {
1591 width = height = 1;
1592 mNoBorderUpdate = true;
1593 } else {
1594 width = mRenderRect.w;
1595 height = mRenderRect.h;
1596 }
1597
1598 RenderVideoFormat format = VIDEO_FORMAT_BGRA;
fei.dengb9a1a572023-09-13 01:33:57 +00001599 mAreaShmBuffer = new WaylandShmBuffer(this, mLogCategory);
fei.dengf7a0cd32023-08-29 09:36:37 +00001600 struct wl_buffer *wlbuf = mAreaShmBuffer->constructWlBuffer(width, height, format);
1601 if (wlbuf == NULL) {
1602 delete mAreaShmBuffer;
1603 mAreaShmBuffer = NULL;
1604 }
1605
1606 wl_surface_attach(mAreaSurfaceWrapper, wlbuf, 0, 0);
1607}
1608
1609std::size_t WaylandDisplay::calculateDmaBufferHash(RenderDmaBuffer &dmabuf)
1610{
1611 std::string hashString("");
1612 for (int i = 0; i < dmabuf.planeCnt; i++) {
1613 char hashtmp[1024];
fei.dengda0cd9f2024-07-24 09:25:11 +08001614 snprintf (hashtmp, 1024, "%d%d%d%d%d%d%d",i,dmabuf.width,dmabuf.height,dmabuf.planeCnt,
fei.dengf7a0cd32023-08-29 09:36:37 +00001615 dmabuf.stride[i],dmabuf.offset[i],dmabuf.fd[i]);
1616 std::string tmp(hashtmp);
1617 hashString += tmp;
1618 }
1619
1620 std::size_t hashval = std::hash<std::string>()(hashString);
fei.dengb9a1a572023-09-13 01:33:57 +00001621 //TRACE(mLogCategory,"hashstr:%s,val:%zu",hashString.c_str(),hashval);
fei.dengf7a0cd32023-08-29 09:36:37 +00001622 return hashval;
1623}
1624
1625void WaylandDisplay::addWaylandBuffer(RenderBuffer * buf, WaylandBuffer *waylandbuf)
1626{
fei.deng19b48692024-08-13 14:17:55 +08001627 Tls::Mutex::Autolock _l(mBufferMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001628 if (buf->flag & BUFFER_FLAG_DMA_BUFFER) {
1629 std::size_t hashval = calculateDmaBufferHash(buf->dma);
1630 std::pair<std::size_t, WaylandBuffer *> item(hashval, waylandbuf);
fei.dengb9a1a572023-09-13 01:33:57 +00001631 //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 +00001632 mWaylandBuffersMap.insert(item);
1633 }
fei.dengb9a1a572023-09-13 01:33:57 +00001634 TRACE(mLogCategory,"mWaylandBuffersMap size:%d",mWaylandBuffersMap.size());
fei.dengf7a0cd32023-08-29 09:36:37 +00001635}
1636
1637WaylandBuffer* WaylandDisplay::findWaylandBuffer(RenderBuffer * buf)
1638{
fei.deng19b48692024-08-13 14:17:55 +08001639 Tls::Mutex::Autolock _l(mBufferMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001640 std::size_t hashval = calculateDmaBufferHash(buf->dma);
1641 auto item = mWaylandBuffersMap.find(hashval);
1642 if (item == mWaylandBuffersMap.end()) {
1643 return NULL;
1644 }
1645
1646 return (WaylandBuffer*) item->second;
1647}
1648
1649void WaylandDisplay::cleanAllWaylandBuffer()
1650{
fei.deng19b48692024-08-13 14:17:55 +08001651 Tls::Mutex::Autolock _l(mBufferMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001652 //free all obtain buff
1653 for (auto item = mWaylandBuffersMap.begin(); item != mWaylandBuffersMap.end(); ) {
1654 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1655 mWaylandBuffersMap.erase(item++);
1656 delete waylandbuf;
1657 }
1658}
1659
fei.dengda0cd9f2024-07-24 09:25:11 +08001660
1661/**
1662 * @brief clean wayland buffers those malloc before resolution changed
1663 *
1664 */
1665void WaylandDisplay::cleanWaylandBufferBeforeResChanged()
1666{
fei.deng19b48692024-08-13 14:17:55 +08001667 Tls::Mutex::Autolock _l(mBufferMutex);
fei.dengda0cd9f2024-07-24 09:25:11 +08001668 for (auto item = mWaylandBuffersMap.begin(); item != mWaylandBuffersMap.end(); ) {
1669 WaylandBuffer *wlbuf = (WaylandBuffer*)item->second;
1670 if (wlbuf->isFree()) {
1671 mWaylandBuffersMap.erase(item++);
1672 delete wlbuf;
1673 } else {
1674 item++;
1675 }
1676 }
1677}
1678
fei.dengf7a0cd32023-08-29 09:36:37 +00001679void WaylandDisplay::flushBuffers()
1680{
fei.dengb9a1a572023-09-13 01:33:57 +00001681 INFO(mLogCategory,"flushBuffers");
1682 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001683 for (auto item = mCommittedBufferMap.begin(); item != mCommittedBufferMap.end(); item++) {
1684 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1685 waylandbuf->forceRedrawing();
1686 handleFrameDisplayedCallback(waylandbuf);
1687 }
1688}
1689
1690void WaylandDisplay::cleanSurface()
1691{
1692 /* clear both video and parent surfaces */
1693 wl_surface_attach (mVideoSurfaceWrapper, NULL, 0, 0);
1694 wl_surface_commit (mVideoSurfaceWrapper);
1695 wl_surface_attach (mAreaSurfaceWrapper, NULL, 0, 0);
1696 wl_surface_commit (mAreaSurfaceWrapper);
fei.deng640c3c92024-04-12 08:31:19 +00001697}
1698
1699void WaylandDisplay::setKeepLastFrame(int keep)
1700{
1701 mKeepLastFrame = keep;
1702 if (mVideoSurfaceWrapper && mAmlConfigAPIList.enableKeepLastFrame) {
1703 INFO(mLogCategory,"keep last frame:%d",keep);
1704 wl_surface_keep_last_frame(mVideoSurfaceWrapper, keep);
fei.deng4029e682024-06-26 17:06:31 +08001705 mToSendKeepLastFrame = false;
1706 return;
fei.deng640c3c92024-04-12 08:31:19 +00001707 }
fei.deng4029e682024-06-26 17:06:31 +08001708 mToSendKeepLastFrame = true;
fei.deng6c425232024-07-19 16:15:31 +08001709}
1710
1711void WaylandDisplay::setFrameRate(int frameRateNum, int frameRateDenom)
1712{
1713 mFrameRateFractionNum = frameRateNum;
1714 mFrameRateFractionDenom = frameRateDenom;
1715 if (mFrameRateFractionDenom == 0) {
1716 mFrameRateFractionDenom = 1;
1717 }
1718 /*cobalt is not set frame rate to gst pipeline. we set default 59.94
1719 frame rate to weston*/
fei.deng19b48692024-08-13 14:17:55 +08001720 if (mFrameRateFractionNum == 0) {
fei.deng6c425232024-07-19 16:15:31 +08001721 mFrameRateFractionNum = 59940;
1722 mFrameRateFractionDenom = 1000;
1723 }
1724 mFrameRateChanged = true;
1725 INFO(mLogCategory,"num:%d,denom:%d",mFrameRateFractionNum, mFrameRateFractionDenom);
fei.dengf7a0cd32023-08-29 09:36:37 +00001726}