blob: 69dac3793599f8f4991bfaeebfa6b2f23b478134 [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.deng640c3c92024-04-12 08:31:19 +0000496 }
497}
498
499static const struct aml_config_listener aml_config_listener = {
500 WaylandDisplay::amlConfigure,
501};
502
fei.dengf7a0cd32023-08-29 09:36:37 +0000503void
504WaylandDisplay::registryHandleGlobal (void *data, struct wl_registry *registry,
fei.dengaf9b07d2023-10-10 07:38:40 +0000505 uint32_t name, const char *interface, uint32_t version)
fei.dengf7a0cd32023-08-29 09:36:37 +0000506{
507 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
fei.dengaf9b07d2023-10-10 07:38:40 +0000508 TRACE(self->mLogCategory,"registryHandleGlobal,name:%u,interface:%s,version:%d",name,interface,version);
fei.dengf7a0cd32023-08-29 09:36:37 +0000509
510 if (strcmp (interface, "wl_compositor") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000511 self->mCompositor = (struct wl_compositor *)wl_registry_bind (registry, name, &wl_compositor_interface, 1/*MIN (version, 3)*/);
fei.dengf7a0cd32023-08-29 09:36:37 +0000512 } else if (strcmp (interface, "wl_subcompositor") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000513 self->mSubCompositor = (struct wl_subcompositor *)wl_registry_bind (registry, name, &wl_subcompositor_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000514 } else if (strcmp (interface, "xdg_wm_base") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000515 self->mXdgWmBase = (struct xdg_wm_base *)wl_registry_bind (registry, name, &xdg_wm_base_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000516 xdg_wm_base_add_listener (self->mXdgWmBase, &xdg_wm_base_listener, (void *)self);
517 } else if (strcmp (interface, "wl_shm") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000518 self->mShm = (struct wl_shm *)wl_registry_bind (registry, name, &wl_shm_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000519 wl_shm_add_listener (self->mShm, &shm_listener, self);
520 } else if (strcmp (interface, "zwp_fullscreen_shell_v1") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000521 //self->mFullscreenShell = (struct zwp_fullscreen_shell_v1 *)wl_registry_bind (registry, name,
fei.dengf7a0cd32023-08-29 09:36:37 +0000522 // &zwp_fullscreen_shell_v1_interface, 1);
523 } else if (strcmp (interface, "wp_viewporter") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000524 self->mViewporter = (struct wp_viewporter *)wl_registry_bind (registry, name, &wp_viewporter_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000525 } else if (strcmp (interface, "zwp_linux_dmabuf_v1") == 0) {
526 if (version < 3)
527 return;
fei.dengaf9b07d2023-10-10 07:38:40 +0000528 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 +0000529 zwp_linux_dmabuf_v1_add_listener (self->mDmabuf, &dmabuf_listener, (void *)self);
530 } else if (strcmp (interface, "wl_output") == 0) {
fei.deng26950832023-11-09 03:00:23 +0000531 int i = 0;
532 uint32_t oriName = self->mCurrentDisplayOutput->name;
fei.dengaf9b07d2023-10-10 07:38:40 +0000533 for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
534 if (self->mOutput[i].wlOutput == NULL) {
535 self->mOutput[i].name = name;
536 self->mOutput[i].wlOutput = (struct wl_output*)wl_registry_bind(registry, name, &wl_output_interface, version);
fei.deng26950832023-11-09 03:00:23 +0000537 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 +0000538 wl_output_add_listener(self->mOutput[i].wlOutput, &outputListener, (void *)self);
539 if (i == 0) { //primary wl_output
540 self->mOutput[i].isPrimary = true;
541 }
fei.deng26950832023-11-09 03:00:23 +0000542 break;
fei.dengaf9b07d2023-10-10 07:38:40 +0000543 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000544 }
fei.deng26950832023-11-09 03:00:23 +0000545 if (i == DEFAULT_DISPLAY_OUTPUT_NUM) {
546 WARNING(self->mLogCategory,"Not enough free output");
547 }
548 //select current wl_output
549 if (self->mSelectOutputIndex != INVALID_OUTPUT_INDEX && !self->isRunning()) {
550 TRACE(self->mLogCategory,"select %d output",self->mSelectOutputIndex);
551 self->mCurrentDisplayOutput = &self->mOutput[self->mSelectOutputIndex];
552 }
553 //if user select a wrong output index, we using a suiteble wl_output
554 if (self->mCurrentDisplayOutput->wlOutput == NULL) {
555 WARNING(self->mLogCategory,"wl_output is null,we should find a suiteble output");
556 for (i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
557 if (self->mOutput[i].wlOutput) {
558 self->mCurrentDisplayOutput = &self->mOutput[i];
559 break;
560 }
561 }
562 }
563 //if current wl_output update, we should update render rectangle
564 if (self->mCurrentDisplayOutput->name != oriName) {
565 self->mUpdateRenderRectangle = true;
566 }
567 //if wl_output plugin,active sending frame
568 self->setRedrawingPending(false);
fei.dengf7a0cd32023-08-29 09:36:37 +0000569 } else if (strcmp(interface, "wl_seat") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000570 //self->mSeat = (struct wl_seat *)wl_registry_bind(registry, name, &wl_seat_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000571 //wl_seat_add_listener(self->mSeat, &seat_listener, (void *)self);
fei.deng31f93f12024-02-27 07:52:10 +0000572 } else if (strcmp(interface, "weston_direct_display_v1") == 0) {
573 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 +0000574 } else if (strcmp(interface, "aml_config") == 0) {
575 self->mAmlConfig = (struct aml_config*)wl_registry_bind(registry, name, &aml_config_interface, 1);
576 aml_config_add_listener(self->mAmlConfig, &aml_config_listener, (void *)self);
fei.dengf7a0cd32023-08-29 09:36:37 +0000577 }
578}
579
580void
581WaylandDisplay::registryHandleGlobalRemove (void *data, struct wl_registry *registry, uint32_t name)
582{
fei.deng26950832023-11-09 03:00:23 +0000583 int i;
fei.dengf7a0cd32023-08-29 09:36:37 +0000584 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
fei.dengaf9b07d2023-10-10 07:38:40 +0000585 /* check wl_output changed */
586 DEBUG(self->mLogCategory,"wayland display remove registry handle global,name:%u",name);
fei.deng26950832023-11-09 03:00:23 +0000587 //if user selected wl_output removed, reset selected output index
588 if (self->mSelectOutputIndex != INVALID_OUTPUT_INDEX &&
589 self->mOutput[self->mSelectOutputIndex].wlOutput &&
590 self->mOutput[self->mSelectOutputIndex].name == name) {
591 self->mSelectOutputIndex = INVALID_OUTPUT_INDEX;
592 }
593 for (i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000594 if (self->mOutput[i].name == name) {
fei.deng26950832023-11-09 03:00:23 +0000595 DEBUG(self->mLogCategory,"remove wl_output name:%u,wl_output:%p",name,self->mOutput[i].wlOutput);
fei.dengaf9b07d2023-10-10 07:38:40 +0000596 self->mOutput[i].name = 0;
597 self->mOutput[i].wlOutput = NULL;
fei.deng26950832023-11-09 03:00:23 +0000598 }
599 }
600 //if current output removed, select a suiteble output
601 if (self->mCurrentDisplayOutput->wlOutput == NULL) {
602 for (i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
603 if (self->mOutput[i].wlOutput) {
604 self->mCurrentDisplayOutput = &self->mOutput[i];
605 self->mUpdateRenderRectangle = true;
606 }
607 }
608 //set new output rectangle
609 if (self->mUpdateRenderRectangle) {
610 self->mUpdateRenderRectangle = false;
611 self->setRenderRectangle(self->mCurrentDisplayOutput->offsetX,
612 self->mCurrentDisplayOutput->offsetY,
613 self->mCurrentDisplayOutput->width,
614 self->mCurrentDisplayOutput->height);
fei.dengaf9b07d2023-10-10 07:38:40 +0000615 }
616 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000617}
618
619static const struct wl_registry_listener registry_listener = {
620 WaylandDisplay::registryHandleGlobal,
621 WaylandDisplay::registryHandleGlobalRemove
622};
623
fei.dengb9a1a572023-09-13 01:33:57 +0000624WaylandDisplay::WaylandDisplay(WaylandPlugin *plugin, int logCategory)
625 :mBufferMutex("bufferMutex"),
626 mWaylandPlugin(plugin),
627 mLogCategory(logCategory)
fei.dengf7a0cd32023-08-29 09:36:37 +0000628{
fei.dengb9a1a572023-09-13 01:33:57 +0000629 TRACE(mLogCategory,"construct WaylandDisplay");
fei.dengf7a0cd32023-08-29 09:36:37 +0000630 mWlDisplay = NULL;
631 mWlDisplayWrapper = NULL;
632 mWlQueue = NULL;
633 mRegistry = NULL;
634 mCompositor = NULL;
635 mXdgWmBase = NULL;
636 mViewporter = NULL;
637 mDmabuf = NULL;
638 mShm = NULL;
639 mSeat = NULL;
640 mPointer = NULL;
641 mTouch = NULL;
642 mKeyboard = NULL;
fei.deng31f93f12024-02-27 07:52:10 +0000643 mDirect_display = NULL;
fei.deng26950832023-11-09 03:00:23 +0000644 mSelectOutputIndex = INVALID_OUTPUT_INDEX;
fei.dengb9a1a572023-09-13 01:33:57 +0000645 mPoll = new Tls::Poll(true);
fei.dengf7a0cd32023-08-29 09:36:37 +0000646 //window
647 mVideoWidth = 0;
648 mVideoHeight = 0;
649 mVideoSurface = NULL;
650 mXdgSurface = NULL;
651 mXdgToplevel = NULL;
652 mAreaViewport = NULL;
653 mVideoViewport = NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000654 mAreaShmBuffer = NULL;
655 mCommitCnt = 0;
fei.dengf7a0cd32023-08-29 09:36:37 +0000656 mAreaSurface = NULL;
657 mAreaSurfaceWrapper = NULL;
658 mVideoSurfaceWrapper = NULL;
659 mVideoSubSurface = NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000660 mPip = 0;
fei.deng640c3c92024-04-12 08:31:19 +0000661 mKeepLastFrame = 0; //default is off keep last frame
fei.deng3287c082024-04-23 09:29:22 +0000662 mSignalFirstFramePts = false;
fei.deng4029e682024-06-26 17:06:31 +0800663 mToSendKeepLastFrame = false;
fei.dengf7a0cd32023-08-29 09:36:37 +0000664}
665
666WaylandDisplay::~WaylandDisplay()
667{
fei.dengb9a1a572023-09-13 01:33:57 +0000668 TRACE(mLogCategory,"desconstruct WaylandDisplay");
669 if (mPoll) {
670 delete mPoll;
671 mPoll = NULL;
672 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000673}
674
675char *WaylandDisplay::require_xdg_runtime_dir()
676{
677 char *val = getenv("XDG_RUNTIME_DIR");
fei.dengb9a1a572023-09-13 01:33:57 +0000678 INFO(mLogCategory,"XDG_RUNTIME_DIR=%s",val);
fei.dengdd910ef2024-06-07 10:25:30 +0800679 //if not set XDG_RUNTIME_DIR,set default value
680 // if (!val) {
681 // val = const_cast<char *>("/run/user/0");
682 // setenv("XDG_RUNTIME_DIR",val,0);
683 // WARNING(mLogCategory,"XDG_RUNTIME_DIR is not set,set default %s",val);
684 // }
fei.dengf7a0cd32023-08-29 09:36:37 +0000685
686 return val;
687}
688
689int WaylandDisplay::openDisplay()
690{
fei.denga4abbd52024-07-11 19:17:50 +0800691 mPixelAspectRatio = 1.0;
692 mUpdateVideoSurface = false;
fei.dengdd910ef2024-06-07 10:25:30 +0800693 mNoBorderUpdate = false;
694 mReCommitAreaSurface = false;
695 mXdgSurfaceConfigured = false;
696 mUpdateRenderRectangle = false;
fei.deng6c425232024-07-19 16:15:31 +0800697 mFrameRateFractionNum = 0;
698 mFrameRateFractionDenom = 1;
699 mFrameRateChanged = false;
fei.dengdd910ef2024-06-07 10:25:30 +0800700 memset(&mRenderRect, 0, sizeof(struct Rectangle));
701 memset(&mVideoRect, 0, sizeof(struct Rectangle));
702 memset(&mWindowRect, 0, sizeof(struct Rectangle));
703 mFullScreen = true; //default is full screen
704 mAmlConfig = NULL;
705 //weston config private api
706 mAmlConfigAPIList.enableDropFrame = false;
707 mAmlConfigAPIList.enableKeepLastFrame = false;
708 mAmlConfigAPIList.enableSetPts = false;
709 mAmlConfigAPIList.enableSetVideoPlane = false;
fei.dengd0da4e22024-07-04 11:29:13 +0800710 mAmlConfigAPIList.enableSurfaceDestroyCallback = false;
fei.deng6c425232024-07-19 16:15:31 +0800711 mAmlConfigAPIList.enableSetDisplayRate = false;
fei.dengdd910ef2024-06-07 10:25:30 +0800712 mCurrentDisplayOutput = &mOutput[0];
713 for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
714 mOutput[i].wlOutput = NULL;
715 mOutput[i].offsetX = 0;
716 mOutput[i].offsetY = 0;
717 mOutput[i].width = 0;
718 mOutput[i].height = 0;
719 mOutput[i].refreshRate = 0;
720 mOutput[i].isPrimary = false;
721 mOutput[i].name = 0;
722 mOutput[i].crtcIndex = 0;
723 }
fei.deng4029e682024-06-26 17:06:31 +0800724 /*if mKeepLastFrame had set but mToSendKeepLastFrame is false,maybe
725 playback is doing FF/FW action,so we keep set it on new connection*/
726 if (!mToSendKeepLastFrame && mKeepLastFrame) {
727 mToSendKeepLastFrame = true;
728 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000729 char *name = require_xdg_runtime_dir();
730 //DEBUG(mLogCategory,"name:%s",name);
fei.dengb9a1a572023-09-13 01:33:57 +0000731 DEBUG(mLogCategory,"openDisplay in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000732 mWlDisplay = wl_display_connect(NULL);
733 if (!mWlDisplay) {
fei.dengb9a1a572023-09-13 01:33:57 +0000734 ERROR(mLogCategory,"Failed to connect to the wayland display, XDG_RUNTIME_DIR='%s'",
fei.dengf7a0cd32023-08-29 09:36:37 +0000735 name ? name : "NULL");
fei.dengb9a1a572023-09-13 01:33:57 +0000736 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000737 }
738
739 mWlDisplayWrapper = (struct wl_display *)wl_proxy_create_wrapper ((void *)mWlDisplay);
740 mWlQueue = wl_display_create_queue (mWlDisplay);
741 wl_proxy_set_queue ((struct wl_proxy *)mWlDisplayWrapper, mWlQueue);
742
743 mRegistry = wl_display_get_registry (mWlDisplayWrapper);
744 wl_registry_add_listener (mRegistry, &registry_listener, (void *)this);
745
746 /* we need exactly 2 roundtrips to discover global objects and their state */
747 for (int i = 0; i < 2; i++) {
748 if (wl_display_roundtrip_queue (mWlDisplay, mWlQueue) < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000749 ERROR(mLogCategory,"Error communicating with the wayland display");
750 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000751 }
752 }
753
754 if (!mCompositor) {
fei.dengb9a1a572023-09-13 01:33:57 +0000755 ERROR(mLogCategory,"Could not bind to wl_compositor. Either it is not implemented in " \
fei.dengf7a0cd32023-08-29 09:36:37 +0000756 "the compositor, or the implemented version doesn't match");
fei.dengb9a1a572023-09-13 01:33:57 +0000757 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000758 }
759
760 if (!mDmabuf) {
fei.dengb9a1a572023-09-13 01:33:57 +0000761 ERROR(mLogCategory,"Could not bind to zwp_linux_dmabuf_v1");
762 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000763 }
764
765 if (!mXdgWmBase) {
766 /* If wl_surface and wl_display are passed via GstContext
767 * wl_shell, xdg_shell and zwp_fullscreen_shell are not used.
768 * In this case is correct to continue.
769 */
fei.dengb9a1a572023-09-13 01:33:57 +0000770 ERROR(mLogCategory,"Could not bind to either wl_shell, xdg_wm_base or "
fei.dengf7a0cd32023-08-29 09:36:37 +0000771 "zwp_fullscreen_shell, video display may not work properly.");
fei.dengb9a1a572023-09-13 01:33:57 +0000772 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000773 }
774
775 //create window surface
776 createCommonWindowSurface();
777 createXdgShellWindowSurface();
778
779 //config weston video plane
fei.deng640c3c92024-04-12 08:31:19 +0000780 if (mAmlConfigAPIList.enableSetVideoPlane) {
fei.dengb9a1a572023-09-13 01:33:57 +0000781 INFO(mLogCategory,"set weston video plane:%d",mPip);
fei.dengf7a0cd32023-08-29 09:36:37 +0000782 wl_surface_set_video_plane(mVideoSurfaceWrapper, mPip);
783 }
784
785 //run wl display queue dispatch
fei.dengb9a1a572023-09-13 01:33:57 +0000786 DEBUG(mLogCategory,"To run wl display dispatch queue");
fei.dengdd910ef2024-06-07 10:25:30 +0800787 if (mPoll) {
788 mPoll->setFlushing(false);
789 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000790 run("display queue");
791 mRedrawingPending = false;
792
fei.dengb9a1a572023-09-13 01:33:57 +0000793 DEBUG(mLogCategory,"openDisplay out");
794 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000795}
796
797void WaylandDisplay::closeDisplay()
798{
fei.dengb9a1a572023-09-13 01:33:57 +0000799 DEBUG(mLogCategory,"closeDisplay in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000800
fei.dengd0da4e22024-07-04 11:29:13 +0800801 //first destroy window surface
802 destroyWindowSurfaces();
fei.dengf7a0cd32023-08-29 09:36:37 +0000803
fei.deng4029e682024-06-26 17:06:31 +0800804 //flush pending event to weston
fei.dengdd910ef2024-06-07 10:25:30 +0800805 if (mWlDisplay) {
806 wl_display_flush (mWlDisplay);
807 }
808
fei.dengd0da4e22024-07-04 11:29:13 +0800809 //wait video surface destroyed or 50ms timeout
810 if (mAmlConfigAPIList.enableSurfaceDestroyCallback) {
811 INFO(mLogCategory,"waiting surface_destroy_cb from weston");
812 Tls::Mutex::Autolock _l(mMutex);
813 if (ERROR_TIMED_OUT == mCondition.waitRelative(mMutex, 50/*microsecond*/)) {
814 WARNING(mLogCategory,"waited surface_destroy_cb timeout");
815 }
816 }
817
818 //we should receive all event from weston before stopped dispatch queue
819 if (isRunning()) {
820 TRACE(mLogCategory,"try stop dispatch thread");
821 if (mPoll) {
822 mPoll->setFlushing(true);
823 }
824 requestExitAndWait();
825 }
826 //after destroyed surface,then destroy buffers,otherwise maybe crash
827 if (mAreaShmBuffer) {
828 delete mAreaShmBuffer;
829 mAreaShmBuffer = NULL;
830 }
831
832 //clean all wayland buffers
833 cleanAllWaylandBuffer();
fei.dengf7a0cd32023-08-29 09:36:37 +0000834
835 if (mViewporter) {
836 wp_viewporter_destroy (mViewporter);
837 mViewporter = NULL;
838 }
839
840 if (mDmabuf) {
841 zwp_linux_dmabuf_v1_destroy (mDmabuf);
842 mDmabuf = NULL;
843 }
844
845 if (mXdgWmBase) {
846 xdg_wm_base_destroy (mXdgWmBase);
847 mXdgWmBase = NULL;
848 }
849
850 if (mCompositor) {
851 wl_compositor_destroy (mCompositor);
852 mCompositor = NULL;
853 }
854
855 if (mSubCompositor) {
856 wl_subcompositor_destroy (mSubCompositor);
857 mSubCompositor = NULL;
858 }
859
860 if (mRegistry) {
861 wl_registry_destroy (mRegistry);
862 mRegistry= NULL;
863 }
864
865 if (mWlDisplayWrapper) {
866 wl_proxy_wrapper_destroy (mWlDisplayWrapper);
867 mWlDisplayWrapper = NULL;
868 }
869
fei.deng640c3c92024-04-12 08:31:19 +0000870 if (mAmlConfig) {
871 aml_config_destroy(mAmlConfig);
872 mAmlConfig = NULL;
873 }
874
fei.dengf7a0cd32023-08-29 09:36:37 +0000875 if (mWlQueue) {
876 wl_event_queue_destroy (mWlQueue);
877 mWlQueue = NULL;
878 }
879
880 if (mWlDisplay) {
881 wl_display_flush (mWlDisplay);
882 wl_display_disconnect (mWlDisplay);
883 mWlDisplay = NULL;
884 }
885
fei.dengb9a1a572023-09-13 01:33:57 +0000886 DEBUG(mLogCategory,"closeDisplay out");
fei.dengf7a0cd32023-08-29 09:36:37 +0000887}
888
889int WaylandDisplay::toDmaBufferFormat(RenderVideoFormat format, uint32_t *outDmaformat /*out param*/, uint64_t *outDmaformatModifiers /*out param*/)
890{
891 if (!outDmaformat || !outDmaformatModifiers) {
fei.dengb9a1a572023-09-13 01:33:57 +0000892 WARNING(mLogCategory,"NULL params");
893 return ERROR_PARAM_NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000894 }
895
896 *outDmaformat = 0;
897 *outDmaformatModifiers = 0;
898
899 uint32_t dmaformat = video_format_to_wl_dmabuf_format (format);
900 if (dmaformat == -1) {
fei.dengb9a1a572023-09-13 01:33:57 +0000901 ERROR(mLogCategory,"Error not found render video format:%d to wl dmabuf format",format);
902 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000903 }
904
fei.dengb9a1a572023-09-13 01:33:57 +0000905 TRACE(mLogCategory,"render video format:%d -> dmabuf format:%d",format,dmaformat);
fei.dengf7a0cd32023-08-29 09:36:37 +0000906 *outDmaformat = (uint32_t)dmaformat;
907
908 /*get dmaformat and modifiers*/
909 auto item = mDmaBufferFormats.find(dmaformat);
910 if (item == mDmaBufferFormats.end()) { //not found
fei.dengb9a1a572023-09-13 01:33:57 +0000911 WARNING(mLogCategory,"Not found dmabuf for render video format :%d",format);
fei.dengf7a0cd32023-08-29 09:36:37 +0000912 *outDmaformatModifiers = 0;
fei.dengb9a1a572023-09-13 01:33:57 +0000913 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000914 }
915
916 *outDmaformatModifiers = (uint64_t)item->second;
917
fei.dengb9a1a572023-09-13 01:33:57 +0000918 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000919}
920
921int WaylandDisplay::toShmBufferFormat(RenderVideoFormat format, uint32_t *outformat)
922{
923 if (!outformat) {
fei.dengb9a1a572023-09-13 01:33:57 +0000924 WARNING(mLogCategory,"NULL params");
925 return ERROR_PARAM_NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000926 }
927
928 *outformat = 0;
929
930 int shmformat = (int)video_format_to_wl_shm_format(format);
931 if (shmformat < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000932 ERROR(mLogCategory,"Error not found render video format:%d to wl shmbuf format",format);
933 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000934 }
935
936 for (auto item = mShmFormats.begin(); item != mShmFormats.end(); ++item) {
937 uint32_t registFormat = (uint32_t)*item;
938 if (registFormat == (uint32_t)shmformat) {
939 *outformat = registFormat;
fei.dengb9a1a572023-09-13 01:33:57 +0000940 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000941 }
942 }
943
fei.dengb9a1a572023-09-13 01:33:57 +0000944 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000945}
946
947void WaylandDisplay::setVideoBufferFormat(RenderVideoFormat format)
948{
fei.dengb9a1a572023-09-13 01:33:57 +0000949 TRACE(mLogCategory,"set video buffer format: %d",format);
fei.dengf7a0cd32023-08-29 09:36:37 +0000950 mBufferFormat = format;
951};
952
953void WaylandDisplay::setDisplayOutput(int output)
954{
fei.dengb9a1a572023-09-13 01:33:57 +0000955 TRACE(mLogCategory,"select display output: %d",output);
fei.dengf7a0cd32023-08-29 09:36:37 +0000956 if (output < 0 || output >= DEFAULT_DISPLAY_OUTPUT_NUM) {
fei.dengb9a1a572023-09-13 01:33:57 +0000957 ERROR(mLogCategory, "display output index error,please set 0:primary or 1:extend,now:%d",output);
fei.dengf7a0cd32023-08-29 09:36:37 +0000958 return;
959 }
fei.deng26950832023-11-09 03:00:23 +0000960 //only do select output before video playing
961 if (mSelectOutputIndex != output) {
962 mSelectOutputIndex = output;
963 // if (mOutput[output].wlOutput) {
964 // mCurrentDisplayOutput = &mOutput[output];
965 // setRenderRectangle(mOutput[output].offsetX, mOutput[output].offsetY,
966 // mOutput[output].width, mOutput[output].height);
967 // }
fei.dengf7a0cd32023-08-29 09:36:37 +0000968 }
969}
970
971int WaylandDisplay::getDisplayOutput()
972{
fei.deng26950832023-11-09 03:00:23 +0000973 return mSelectOutputIndex == INVALID_OUTPUT_INDEX? 0: mSelectOutputIndex;
fei.dengf7a0cd32023-08-29 09:36:37 +0000974}
975
976void WaylandDisplay::setPip(int pip)
977{
fei.dengb9a1a572023-09-13 01:33:57 +0000978 INFO(mLogCategory,"set pip:%d",pip);
fei.dengf7a0cd32023-08-29 09:36:37 +0000979 mPip = pip;
980}
981
fei.deng26950832023-11-09 03:00:23 +0000982void WaylandDisplay::updateDisplayOutput()
983{
984 if (!mCurrentDisplayOutput->wlOutput || !mXdgToplevel || !mXdgSurface)
985 {
986 return;
987 }
988 if (mUpdateRenderRectangle) {
989 if (mFullScreen) {
990 DEBUG(mLogCategory,"unset full screen");
991 xdg_toplevel_unset_fullscreen (mXdgToplevel);
992 }
993
994 if (mXdgSurface) {
995 DEBUG(mLogCategory,"set geometry");
996 xdg_surface_set_window_geometry(mXdgSurface,
997 mCurrentDisplayOutput->offsetX,
998 mCurrentDisplayOutput->offsetY,
999 mCurrentDisplayOutput->width,
1000 mCurrentDisplayOutput->height);
1001 }
1002
1003 if (mFullScreen && mXdgToplevel) {
1004 DEBUG(mLogCategory,"set full screen");
1005 xdg_toplevel_set_fullscreen (mXdgToplevel, mCurrentDisplayOutput->wlOutput);
1006 }
1007 setRenderRectangle(mCurrentDisplayOutput->offsetX, mCurrentDisplayOutput->offsetY,
1008 mCurrentDisplayOutput->width, mCurrentDisplayOutput->height);
1009 mUpdateRenderRectangle = false;
1010 }
1011}
1012
fei.dengf7a0cd32023-08-29 09:36:37 +00001013void WaylandDisplay::createCommonWindowSurface()
1014{
1015 struct wl_region *region;
1016
1017 mAreaSurface = wl_compositor_create_surface (mCompositor);
1018 mVideoSurface = wl_compositor_create_surface (mCompositor);
1019 mAreaSurfaceWrapper = (struct wl_surface *)wl_proxy_create_wrapper (mAreaSurface);
1020 mVideoSurfaceWrapper = (struct wl_surface *)wl_proxy_create_wrapper (mVideoSurface);
1021
1022 wl_proxy_set_queue ((struct wl_proxy *) mAreaSurfaceWrapper, mWlQueue);
1023 wl_proxy_set_queue ((struct wl_proxy *) mVideoSurfaceWrapper, mWlQueue);
1024
1025 /* embed video_surface in area_surface */
1026 mVideoSubSurface = wl_subcompositor_get_subsurface (mSubCompositor, mVideoSurface, mAreaSurface);
1027 wl_subsurface_set_desync (mVideoSubSurface);
1028
fei.dengd0da4e22024-07-04 11:29:13 +08001029 //add video surface callback to weston if weston enable this feature
1030 if (mVideoSurface && mAmlConfigAPIList.enableSurfaceDestroyCallback) {
1031 struct wl_callback *surfaceDestroyCb = wl_surface_destroy_callback(mVideoSurface);
1032 wl_callback_add_listener(surfaceDestroyCb, &surface_destroy_listener, this);
1033 }
1034
fei.dengf7a0cd32023-08-29 09:36:37 +00001035 if (mViewporter) {
1036 mAreaViewport = wp_viewporter_get_viewport (mViewporter, mAreaSurface);
1037 mVideoViewport = wp_viewporter_get_viewport (mViewporter, mVideoSurface);
1038 }
1039
1040 /* do not accept input */
1041 region = wl_compositor_create_region (mCompositor);
1042 wl_surface_set_input_region (mAreaSurface, region);
1043 wl_region_destroy (region);
1044
1045 region = wl_compositor_create_region (mCompositor);
1046 wl_surface_set_input_region (mVideoSurface, region);
1047 wl_region_destroy (region);
1048}
1049
1050void WaylandDisplay::createXdgShellWindowSurface()
1051{
1052 /* Check which protocol we will use (in order of preference) */
1053 if (mXdgWmBase) {
1054 /* First create the XDG surface */
1055 mXdgSurface= xdg_wm_base_get_xdg_surface (mXdgWmBase, mAreaSurface);
1056 if (!mXdgSurface) {
fei.dengb9a1a572023-09-13 01:33:57 +00001057 ERROR(mLogCategory,"Unable to get xdg_surface");
fei.dengf7a0cd32023-08-29 09:36:37 +00001058 return;
1059 }
1060 xdg_surface_add_listener (mXdgSurface, &xdg_surface_listener,(void *)this);
1061
1062 /* Then the toplevel */
1063 mXdgToplevel= xdg_surface_get_toplevel (mXdgSurface);
1064 if (!mXdgSurface) {
fei.dengb9a1a572023-09-13 01:33:57 +00001065 ERROR(mLogCategory,"Unable to get xdg_toplevel");
fei.dengf7a0cd32023-08-29 09:36:37 +00001066 return;
1067 }
1068 xdg_toplevel_add_listener (mXdgToplevel, &xdg_toplevel_listener, this);
1069
1070 /* Finally, commit the xdg_surface state as toplevel */
1071 mXdgSurfaceConfigured = false;
1072 wl_surface_commit (mAreaSurface);
1073 wl_display_flush (mWlDisplay);
1074 /* we need exactly 3 roundtrips to discover global objects and their state */
1075 for (int i = 0; i < 3; i++) {
1076 if (wl_display_roundtrip_queue(mWlDisplay, mWlQueue) < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +00001077 ERROR(mLogCategory,"Error communicating with the wayland display");
fei.dengf7a0cd32023-08-29 09:36:37 +00001078 }
1079 }
1080
1081 if (mXdgSurfaceConfigured) {
fei.dengb9a1a572023-09-13 01:33:57 +00001082 INFO(mLogCategory,"xdg surface had configured");
fei.dengf7a0cd32023-08-29 09:36:37 +00001083 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001084 WARNING(mLogCategory,"xdg surface not configured");
fei.dengf7a0cd32023-08-29 09:36:37 +00001085 }
1086
1087 //full screen show
fei.deng26950832023-11-09 03:00:23 +00001088 // if (mFullScreen && mCurrentDisplayOutput->wlOutput) {
1089 // //ensureFullscreen(mFullScreen);
fei.dengf7a0cd32023-08-29 09:36:37 +00001090 // }
1091 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001092 ERROR(mLogCategory,"Unable to use xdg_wm_base ");
fei.dengf7a0cd32023-08-29 09:36:37 +00001093 return;
1094 }
1095}
1096
1097void WaylandDisplay::destroyWindowSurfaces()
1098{
fei.dengf7a0cd32023-08-29 09:36:37 +00001099 if (mXdgToplevel) {
1100 xdg_toplevel_destroy (mXdgToplevel);
1101 mXdgToplevel = NULL;
1102 }
1103
1104 if (mXdgSurface) {
1105 xdg_surface_destroy (mXdgSurface);
1106 mXdgSurface = NULL;
1107 }
1108
1109 if (mVideoSurfaceWrapper) {
1110 wl_proxy_wrapper_destroy (mVideoSurfaceWrapper);
1111 mVideoSurfaceWrapper = NULL;
1112 }
1113
1114 if (mVideoSubSurface) {
1115 wl_subsurface_destroy (mVideoSubSurface);
1116 mVideoSubSurface = NULL;
1117 }
1118
1119 if (mVideoSurface) {
1120 wl_surface_destroy (mVideoSurface);
1121 mVideoSurface = NULL;
1122 }
1123
1124 if (mAreaSurfaceWrapper) {
1125 wl_proxy_wrapper_destroy (mAreaSurfaceWrapper);
1126 mAreaSurfaceWrapper = NULL;
1127 }
1128
1129 if (mAreaSurface) {
1130 wl_surface_destroy (mAreaSurface);
1131 mAreaSurface = NULL;
1132 mReCommitAreaSurface = false;
1133 }
1134}
1135
1136void WaylandDisplay::ensureFullscreen(bool fullscreen)
1137{
1138 if (mXdgWmBase) {
fei.dengb9a1a572023-09-13 01:33:57 +00001139 DEBUG(mLogCategory,"full screen : %d",fullscreen);
fei.dengf7a0cd32023-08-29 09:36:37 +00001140 if (fullscreen) {
fei.deng26950832023-11-09 03:00:23 +00001141 xdg_toplevel_set_fullscreen (mXdgToplevel, mCurrentDisplayOutput->wlOutput);
fei.dengf7a0cd32023-08-29 09:36:37 +00001142 } else {
1143 xdg_toplevel_unset_fullscreen (mXdgToplevel);
1144 }
1145 }
1146}
1147
1148void WaylandDisplay::setRenderRectangle(int x, int y, int w, int h)
1149{
fei.dengb9a1a572023-09-13 01:33:57 +00001150 DEBUG(mLogCategory,"set render rect:x:%d,y:%d,w:%d,h:%d",x,y,w,h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001151
1152 if (w <= 0 || h <= 0) {
fei.dengb9a1a572023-09-13 01:33:57 +00001153 WARNING(mLogCategory, "wrong render width or height %dx%d",w,h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001154 return;
1155 }
1156
1157 mRenderRect.x = x;
1158 mRenderRect.y = y;
1159 mRenderRect.w = w;
1160 mRenderRect.h = h;
1161
1162 if (!mXdgSurfaceConfigured) {
fei.dengb9a1a572023-09-13 01:33:57 +00001163 WARNING(mLogCategory,"Not configured xdg");
fei.dengf7a0cd32023-08-29 09:36:37 +00001164 return;
1165 }
1166
1167 if (mAreaViewport) {
1168 wp_viewport_set_destination (mAreaViewport, w, h);
1169 }
1170
1171 updateBorders();
1172
1173 if (mVideoWidth != 0 && mVideoSurface) {
1174 wl_subsurface_set_sync (mVideoSubSurface);
1175 resizeVideoSurface(true);
1176 }
1177
1178 wl_surface_damage (mAreaSurfaceWrapper, 0, 0, w, h);
1179 wl_surface_commit (mAreaSurfaceWrapper);
1180
1181 if (mVideoWidth != 0) {
1182 wl_subsurface_set_desync (mVideoSubSurface);
1183 }
1184}
1185
fei.dengf7a0cd32023-08-29 09:36:37 +00001186void WaylandDisplay::setFrameSize(int w, int h)
1187{
1188 mVideoWidth = w;
1189 mVideoHeight = h;
fei.denga4abbd52024-07-11 19:17:50 +08001190 mUpdateVideoSurface = true;
fei.dengb9a1a572023-09-13 01:33:57 +00001191 TRACE(mLogCategory,"frame w:%d,h:%d",mVideoWidth,mVideoHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +00001192}
1193
1194void WaylandDisplay::setWindowSize(int x, int y, int w, int h)
1195{
1196 mWindowRect.x = x;
1197 mWindowRect.y = y;
1198 mWindowRect.w = w;
1199 mWindowRect.h = h;
fei.denga4abbd52024-07-11 19:17:50 +08001200 mUpdateVideoSurface = true;
fei.dengb9a1a572023-09-13 01:33:57 +00001201 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 +00001202}
1203
fei.denga4abbd52024-07-11 19:17:50 +08001204void WaylandDisplay::setPixelAspectRatio(double ratio)
1205{
1206 mPixelAspectRatio = ratio;
1207 mUpdateVideoSurface = true;
1208}
fei.dengf7a0cd32023-08-29 09:36:37 +00001209
1210void WaylandDisplay::resizeVideoSurface(bool commit)
1211{
1212 Rectangle src = {0,};
1213 Rectangle dst = {0,};
1214 Rectangle res;
1215
1216 /* center the video_subsurface inside area_subsurface */
1217 src.w = mVideoWidth;
1218 src.h = mVideoHeight;
1219 /*if had set the window size, we will scall
1220 video surface to this window size*/
1221 if (mWindowRect.w > 0 && mWindowRect.h > 0) {
1222 dst.x = mWindowRect.x;
1223 dst.y = mWindowRect.y;
1224 dst.w = mWindowRect.w;
1225 dst.h = mWindowRect.h;
1226 if (mWindowRect.w > mRenderRect.w && mWindowRect.h > mRenderRect.h) {
fei.dengb9a1a572023-09-13 01:33:57 +00001227 WARNING(mLogCategory,"Error window size:%dx%d, but render size:%dx%d,reset to render size",
fei.dengf7a0cd32023-08-29 09:36:37 +00001228 mWindowRect.w,mWindowRect.h,mRenderRect.w,mRenderRect.h);
1229 dst.x = mRenderRect.x;
1230 dst.y = mRenderRect.y;
1231 dst.w = mRenderRect.w;
1232 dst.h = mRenderRect.h;
1233 }
1234 //to do,we need set geometry?
1235 //if (mXdgSurface) {
1236 // xdg_surface_set_window_geometry(mXdgSurface, mWindowRect.x, mWindowRect.y, mWindowRect.w, mWindowRect.h);
1237 //}
1238 } else { //scal video to full screen
1239 dst.w = mRenderRect.w;
1240 dst.h = mRenderRect.h;
1241 }
1242
1243 if (mViewporter) {
1244 videoCenterRect(src, dst, &res, true);
1245 } else {
1246 videoCenterRect(src, dst, &res, false);
1247 }
1248
1249 wl_subsurface_set_position (mVideoSubSurface, res.x, res.y);
1250
1251 if (commit) {
1252 wl_surface_damage (mVideoSurfaceWrapper, 0, 0, res.w, res.h);
1253 wl_surface_commit (mVideoSurfaceWrapper);
1254 }
1255
1256 //top level setting
1257 if (mXdgToplevel) {
1258 struct wl_region *region;
1259
1260 region = wl_compositor_create_region (mCompositor);
1261 wl_region_add (region, 0, 0, mRenderRect.w, mRenderRect.h);
1262 wl_surface_set_input_region (mAreaSurface, region);
1263 wl_region_destroy (region);
1264 }
1265
1266 /* this is saved for use in wl_surface_damage */
1267 mVideoRect.x = res.x;
1268 mVideoRect.y = res.y;
1269 mVideoRect.w = res.w;
1270 mVideoRect.h = res.h;
1271
fei.denga4abbd52024-07-11 19:17:50 +08001272 //to scale video surface
fei.dengf7a0cd32023-08-29 09:36:37 +00001273 wp_viewport_set_destination(mVideoViewport, res.w, res.h);
fei.denga4abbd52024-07-11 19:17:50 +08001274 wl_display_flush (mWlDisplay);
fei.dengb9a1a572023-09-13 01:33:57 +00001275 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 +00001276}
1277
1278void WaylandDisplay::setOpaque()
1279{
1280 struct wl_region *region;
1281
1282 /* Set area opaque */
1283 region = wl_compositor_create_region (mCompositor);
1284 wl_region_add (region, 0, 0, mRenderRect.w, mRenderRect.h);
1285 wl_surface_set_opaque_region (mAreaSurface, region);
1286 wl_region_destroy (region);
1287}
1288
1289int WaylandDisplay::prepareFrameBuffer(RenderBuffer * buf)
1290{
1291 WaylandBuffer *waylandBuf = NULL;
1292 int ret;
fei.dengda0cd9f2024-07-24 09:25:11 +08001293 bool isNew = false;
fei.dengf7a0cd32023-08-29 09:36:37 +00001294
fei.dengdd910ef2024-06-07 10:25:30 +08001295 if (!mDmabuf)
1296 {
1297 ERROR(mLogCategory,"Error zwp_linux_dmabuf_v1");
1298 return ERROR_UNKNOWN;
1299 }
1300
fei.dengf7a0cd32023-08-29 09:36:37 +00001301 waylandBuf = findWaylandBuffer(buf);
1302 if (waylandBuf == NULL) {
fei.dengb9a1a572023-09-13 01:33:57 +00001303 waylandBuf = new WaylandBuffer(this, mLogCategory);
fei.dengda0cd9f2024-07-24 09:25:11 +08001304 isNew = true;
1305 }
1306 waylandBuf->setBufferFormat(mBufferFormat);
1307 ret = waylandBuf->constructWlBuffer(buf);
1308 if (ret != NO_ERROR) {
1309 WARNING(mLogCategory,"dmabufConstructWlBuffer fail,release waylandbuf");
1310 //delete waylanBuf,WaylandBuffer object destruct will call release callback
1311 goto waylandbuf_fail;
1312 }
1313 if (isNew) {
1314 addWaylandBuffer(buf, waylandBuf);
fei.dengf7a0cd32023-08-29 09:36:37 +00001315 }
fei.dengb9a1a572023-09-13 01:33:57 +00001316 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +00001317waylandbuf_fail:
1318 //delete waylandbuf
1319 delete waylandBuf;
1320 waylandBuf = NULL;
fei.dengb9a1a572023-09-13 01:33:57 +00001321 return ERROR_UNKNOWN;
fei.dengf7a0cd32023-08-29 09:36:37 +00001322}
1323
1324void WaylandDisplay::displayFrameBuffer(RenderBuffer * buf, int64_t realDisplayTime)
1325{
fei.deng6c425232024-07-19 16:15:31 +08001326 //set frame rate to weston,it lets weston to select suitable mode
1327 if (mFrameRateChanged && mAmlConfigAPIList.enableSetDisplayRate) {
1328 mFrameRateChanged = false;
1329 TRACE(mLogCategory,"set frame rate %d/%d to weston", mFrameRateFractionNum, mFrameRateFractionDenom);
1330 wl_surface_set_display_rate(mVideoSurfaceWrapper, mFrameRateFractionNum, mFrameRateFractionDenom);
1331 }
fei.denga4abbd52024-07-11 19:17:50 +08001332 //update video surface size
1333 if (mUpdateVideoSurface && mVideoSurface &&
1334 buf->dma.width == mVideoWidth && buf->dma.height == mVideoHeight) {
1335 mUpdateVideoSurface = false;
1336 //if had full screen, unset it and set window size
1337 if (mFullScreen) {
1338 mFullScreen = false;
1339 ensureFullscreen(mFullScreen);
1340 }
1341 resizeVideoSurface(true);
fei.dengda0cd9f2024-07-24 09:25:11 +08001342 /*clean wayland buffers those allocated
1343 before resolution changed and had release by weston */
1344 cleanWaylandBufferBeforeResChanged();
fei.denga4abbd52024-07-11 19:17:50 +08001345 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001346 WaylandBuffer *waylandBuf = NULL;
1347 struct wl_buffer * wlbuffer = NULL;
1348 int ret;
1349
1350 if (!buf) {
fei.dengb9a1a572023-09-13 01:33:57 +00001351 ERROR(mLogCategory,"Error input params, waylandbuffer is null");
fei.dengf7a0cd32023-08-29 09:36:37 +00001352 return;
1353 }
1354
1355 //must commit areasurface first, because weston xdg surface maybe timeout
1356 //this cause video is not display,commit can resume xdg surface
1357 if (!mReCommitAreaSurface) {
1358 mReCommitAreaSurface = true;
1359 wl_surface_commit (mAreaSurface);
1360 }
1361
fei.dengdd910ef2024-06-07 10:25:30 +08001362 //TRACE(mLogCategory,"display renderBuffer:%p,PTS:%lld us,realtime:%lld",buf, buf->pts/1000, realDisplayTime);
fei.dengf7a0cd32023-08-29 09:36:37 +00001363
1364 if (buf->flag & BUFFER_FLAG_DMA_BUFFER) {
1365 if (buf->dma.width <=0 || buf->dma.height <=0) {
1366 buf->dma.width = mVideoWidth;
1367 buf->dma.height = mVideoHeight;
1368 }
1369 waylandBuf = findWaylandBuffer(buf);
1370 if (waylandBuf) {
1371 waylandBuf->setRenderRealTime(realDisplayTime);
fei.dengf7a0cd32023-08-29 09:36:37 +00001372 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001373 ERROR(mLogCategory,"NOT found wayland buffer,please prepare buffer first");
fei.dengf7a0cd32023-08-29 09:36:37 +00001374 goto waylandbuf_fail;
1375 }
1376 }
1377
1378 if (waylandBuf) {
1379 wlbuffer = waylandBuf->getWlBuffer();
1380 }
fei.deng1cfb2752023-10-26 08:01:25 +00001381 //if no wl_output, drop this buffer
fei.deng26950832023-11-09 03:00:23 +00001382 if (mCurrentDisplayOutput->wlOutput == NULL) {
fei.deng1cfb2752023-10-26 08:01:25 +00001383 TRACE(mLogCategory,"No wl_output");
1384 mWaylandPlugin->handleFrameDropped(buf);
1385 mWaylandPlugin->handleBufferRelease(buf);
1386 return;
1387 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001388 if (wlbuffer) {
fei.dengb9a1a572023-09-13 01:33:57 +00001389 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001390 ++mCommitCnt;
1391 uint32_t hiPts = realDisplayTime >> 32;
1392 uint32_t lowPts = realDisplayTime & 0xFFFFFFFF;
1393 //attach this wl_buffer to weston
fei.denga4abbd52024-07-11 19:17:50 +08001394 TRACE(mLogCategory,"++attach,renderbuf:%p,wl_buffer:%p(%d,%d,%d,%d),pts:%lld us,commitCnt:%d",
1395 buf,wlbuffer,mVideoRect.x,mVideoRect.y,mVideoRect.w,mVideoRect.h,buf->pts/1000,mCommitCnt);
fei.dengf7a0cd32023-08-29 09:36:37 +00001396 waylandBuf->attach(mVideoSurfaceWrapper);
1397
fei.deng640c3c92024-04-12 08:31:19 +00001398 if (mAmlConfigAPIList.enableSetPts) {
fei.dengb9a1a572023-09-13 01:33:57 +00001399 TRACE(mLogCategory,"display time:%lld,hiPts:%u,lowPts:%u",realDisplayTime, hiPts, lowPts);
fei.dengf7a0cd32023-08-29 09:36:37 +00001400 wl_surface_set_pts(mVideoSurfaceWrapper, hiPts, lowPts);
1401 }
1402
1403 wl_surface_damage (mVideoSurfaceWrapper, 0, 0, mVideoRect.w, mVideoRect.h);
1404 wl_surface_commit (mVideoSurfaceWrapper);
1405 //insert this buffer to committed weston buffer manager
fei.dengae8c90a2024-06-27 13:39:53 +08001406 std::pair<int64_t, WaylandBuffer *> item(realDisplayTime, waylandBuf);
fei.dengf7a0cd32023-08-29 09:36:37 +00001407 mCommittedBufferMap.insert(item);
1408 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001409 WARNING(mLogCategory,"wlbuffer is NULL");
fei.dengf7a0cd32023-08-29 09:36:37 +00001410 /* clear both video and parent surfaces */
1411 cleanSurface();
1412 }
1413
1414 wl_display_flush (mWlDisplay);
fei.deng4029e682024-06-26 17:06:31 +08001415 //set keep last frame or not when after send first buffer to weston,1 keep last frame, 0 not
1416 if (mToSendKeepLastFrame) {
1417 setKeepLastFrame(mKeepLastFrame);
1418 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001419
1420 return;
1421waylandbuf_fail:
1422 //notify dropped
1423 mWaylandPlugin->handleFrameDropped(buf);
1424 //notify app release this buf
1425 mWaylandPlugin->handleBufferRelease(buf);
fei.dengf7a0cd32023-08-29 09:36:37 +00001426 return;
1427}
1428
1429void WaylandDisplay::handleBufferReleaseCallback(WaylandBuffer *buf)
1430{
fei.denga4abbd52024-07-11 19:17:50 +08001431 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengf7a0cd32023-08-29 09:36:37 +00001432 {
fei.dengb9a1a572023-09-13 01:33:57 +00001433 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001434 //remove buffer if this buffer is ready to release
fei.dengae8c90a2024-06-27 13:39:53 +08001435 auto item = mCommittedBufferMap.find(buf->getRenderRealTime());
fei.dengf7a0cd32023-08-29 09:36:37 +00001436 if (item != mCommittedBufferMap.end()) {
fei.dengdd910ef2024-06-07 10:25:30 +08001437 --mCommitCnt;
fei.dengf7a0cd32023-08-29 09:36:37 +00001438 mCommittedBufferMap.erase(item);
1439 } else {
fei.denga4abbd52024-07-11 19:17:50 +08001440 TRACE(mLogCategory,"Error,Can't find WaylandBuffer pts:%lld us (%lld) in buffer map",
1441 renderBuffer->pts/1000,buf->getRenderRealTime());
fei.dengf7a0cd32023-08-29 09:36:37 +00001442 return;
1443 }
1444 }
fei.denga4abbd52024-07-11 19:17:50 +08001445
1446 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 +00001447 mWaylandPlugin->handleBufferRelease(renderBuffer);
1448}
1449
1450void WaylandDisplay::handleFrameDisplayedCallback(WaylandBuffer *buf)
1451{
1452 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.denga4abbd52024-07-11 19:17:50 +08001453 TRACE(mLogCategory,"renderBuffer :%p,PTS:%lld us,realtime:%lld us",renderBuffer,renderBuffer->pts/1000,buf->getRenderRealTime());
fei.deng3287c082024-04-23 09:29:22 +00001454 if (!mSignalFirstFramePts) {
1455 mSignalFirstFramePts = true;
1456 mWaylandPlugin->handleMsgNotify(MSG_FIRST_FRAME, (void*)&renderBuffer->pts);
1457 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001458 mWaylandPlugin->handleFrameDisplayed(renderBuffer);
1459}
1460
1461void WaylandDisplay::handleFrameDropedCallback(WaylandBuffer *buf)
1462{
1463 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.denga4abbd52024-07-11 19:17:50 +08001464 TRACE(mLogCategory,"renderBuffer :%p,PTS:%lld us,realtime:%lld us",renderBuffer,renderBuffer->pts/1000,buf->getRenderRealTime());
fei.dengf7a0cd32023-08-29 09:36:37 +00001465 mWaylandPlugin->handleFrameDropped(renderBuffer);
1466}
1467
1468
1469void WaylandDisplay::readyToRun()
1470{
1471 mFd = wl_display_get_fd (mWlDisplay);
fei.dengb9a1a572023-09-13 01:33:57 +00001472 if (mPoll) {
1473 mPoll->addFd(mFd);
1474 mPoll->setFdReadable(mFd, true);
1475 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001476}
1477
fei.dengdd910ef2024-06-07 10:25:30 +08001478void WaylandDisplay::readyToExit()
1479{
1480 if (mPoll && mFd >= 0) {
1481 mPoll->removeFd(mFd);
1482 }
1483}
1484
fei.dengf7a0cd32023-08-29 09:36:37 +00001485bool WaylandDisplay::threadLoop()
1486{
1487 int ret;
fei.dengf7a0cd32023-08-29 09:36:37 +00001488
1489 while (wl_display_prepare_read_queue (mWlDisplay, mWlQueue) != 0) {
1490 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1491 }
1492
1493 wl_display_flush (mWlDisplay);
1494
1495 /*poll timeout value must > 300 ms,otherwise zwp_linux_dmabuf will create failed,
1496 so do use -1 to wait for ever*/
fei.dengb9a1a572023-09-13 01:33:57 +00001497 ret = mPoll->wait(-1); //wait for ever
fei.dengf7a0cd32023-08-29 09:36:37 +00001498 if (ret < 0) { //poll error
fei.dengb9a1a572023-09-13 01:33:57 +00001499 WARNING(mLogCategory,"poll error");
fei.dengf7a0cd32023-08-29 09:36:37 +00001500 wl_display_cancel_read(mWlDisplay);
1501 return false;
1502 } else if (ret == 0) { //poll time out
1503 return true; //run loop
1504 }
1505
1506 if (wl_display_read_events (mWlDisplay) == -1) {
1507 goto tag_error;
1508 }
1509
1510 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1511 return true;
1512tag_error:
fei.dengb9a1a572023-09-13 01:33:57 +00001513 ERROR(mLogCategory,"Error communicating with the wayland server");
fei.dengf7a0cd32023-08-29 09:36:37 +00001514 return false;
1515}
1516
1517void WaylandDisplay::videoCenterRect(Rectangle src, Rectangle dst, Rectangle *result, bool scaling)
1518{
1519 //if dst is a small window, we scale video to map window size,don't doing center
fei.dengda0cd9f2024-07-24 09:25:11 +08001520 // if (mRenderRect.w != dst.w && mRenderRect.h != dst.h) {
1521 // result->x = dst.x;
1522 // result->y = dst.y;
1523 // result->w = dst.w;
1524 // result->h = dst.h;
1525 // TRACE(mLogCategory,"small window source is %dx%d dest is %dx%d, result is %d,%d,%d,%d",
1526 // src.w, src.h, dst.w, dst.h, result->x, result->y, result->w, result->h);
1527 // return;
1528 // }
fei.dengf7a0cd32023-08-29 09:36:37 +00001529 if (!scaling) {
1530 result->w = MIN (src.w, dst.w);
1531 result->h = MIN (src.h, dst.h);
1532 result->x = dst.x + (dst.w - result->w) / 2;
1533 result->y = dst.y + (dst.h - result->h) / 2;
1534 } else {
1535 double src_ratio, dst_ratio;
1536
fei.denga4abbd52024-07-11 19:17:50 +08001537 src_ratio = (double) (src.w * mPixelAspectRatio) / src.h;
fei.dengf7a0cd32023-08-29 09:36:37 +00001538 dst_ratio = (double) dst.w / dst.h;
1539
1540 if (src_ratio > dst_ratio) {
1541 result->w = dst.w;
1542 result->h = dst.w / src_ratio;
1543 result->x = dst.x;
1544 result->y = dst.y + (dst.h - result->h) / 2;
1545 } else if (src_ratio < dst_ratio) {
1546 result->w = dst.h * src_ratio;
1547 result->h = dst.h;
1548 result->x = dst.x + (dst.w - result->w) / 2;
1549 result->y = dst.y;
1550 } else {
1551 result->x = dst.x;
1552 result->y = dst.y;
1553 result->w = dst.w;
1554 result->h = dst.h;
1555 }
1556 }
1557
fei.denga4abbd52024-07-11 19:17:50 +08001558 TRACE(mLogCategory,"source is %dx%d dest is %dx%d, result is %d,%d,%d,%d",
1559 src.w, src.h, dst.w, dst.h, result->x, result->y,result->w, result->h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001560}
1561
1562void WaylandDisplay::updateBorders()
1563{
1564 int width,height;
1565
1566 if (mNoBorderUpdate)
1567 return;
1568
1569 if (mViewporter) {
1570 width = height = 1;
1571 mNoBorderUpdate = true;
1572 } else {
1573 width = mRenderRect.w;
1574 height = mRenderRect.h;
1575 }
1576
1577 RenderVideoFormat format = VIDEO_FORMAT_BGRA;
fei.dengb9a1a572023-09-13 01:33:57 +00001578 mAreaShmBuffer = new WaylandShmBuffer(this, mLogCategory);
fei.dengf7a0cd32023-08-29 09:36:37 +00001579 struct wl_buffer *wlbuf = mAreaShmBuffer->constructWlBuffer(width, height, format);
1580 if (wlbuf == NULL) {
1581 delete mAreaShmBuffer;
1582 mAreaShmBuffer = NULL;
1583 }
1584
1585 wl_surface_attach(mAreaSurfaceWrapper, wlbuf, 0, 0);
1586}
1587
1588std::size_t WaylandDisplay::calculateDmaBufferHash(RenderDmaBuffer &dmabuf)
1589{
1590 std::string hashString("");
1591 for (int i = 0; i < dmabuf.planeCnt; i++) {
1592 char hashtmp[1024];
fei.dengda0cd9f2024-07-24 09:25:11 +08001593 snprintf (hashtmp, 1024, "%d%d%d%d%d%d%d",i,dmabuf.width,dmabuf.height,dmabuf.planeCnt,
fei.dengf7a0cd32023-08-29 09:36:37 +00001594 dmabuf.stride[i],dmabuf.offset[i],dmabuf.fd[i]);
1595 std::string tmp(hashtmp);
1596 hashString += tmp;
1597 }
1598
1599 std::size_t hashval = std::hash<std::string>()(hashString);
fei.dengb9a1a572023-09-13 01:33:57 +00001600 //TRACE(mLogCategory,"hashstr:%s,val:%zu",hashString.c_str(),hashval);
fei.dengf7a0cd32023-08-29 09:36:37 +00001601 return hashval;
1602}
1603
1604void WaylandDisplay::addWaylandBuffer(RenderBuffer * buf, WaylandBuffer *waylandbuf)
1605{
1606 if (buf->flag & BUFFER_FLAG_DMA_BUFFER) {
1607 std::size_t hashval = calculateDmaBufferHash(buf->dma);
1608 std::pair<std::size_t, WaylandBuffer *> item(hashval, waylandbuf);
fei.dengb9a1a572023-09-13 01:33:57 +00001609 //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 +00001610 mWaylandBuffersMap.insert(item);
1611 }
fei.dengb9a1a572023-09-13 01:33:57 +00001612 TRACE(mLogCategory,"mWaylandBuffersMap size:%d",mWaylandBuffersMap.size());
fei.dengf7a0cd32023-08-29 09:36:37 +00001613}
1614
1615WaylandBuffer* WaylandDisplay::findWaylandBuffer(RenderBuffer * buf)
1616{
1617 std::size_t hashval = calculateDmaBufferHash(buf->dma);
1618 auto item = mWaylandBuffersMap.find(hashval);
1619 if (item == mWaylandBuffersMap.end()) {
1620 return NULL;
1621 }
1622
1623 return (WaylandBuffer*) item->second;
1624}
1625
1626void WaylandDisplay::cleanAllWaylandBuffer()
1627{
1628 //free all obtain buff
1629 for (auto item = mWaylandBuffersMap.begin(); item != mWaylandBuffersMap.end(); ) {
1630 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1631 mWaylandBuffersMap.erase(item++);
1632 delete waylandbuf;
1633 }
1634}
1635
fei.dengda0cd9f2024-07-24 09:25:11 +08001636
1637/**
1638 * @brief clean wayland buffers those malloc before resolution changed
1639 *
1640 */
1641void WaylandDisplay::cleanWaylandBufferBeforeResChanged()
1642{
1643 for (auto item = mWaylandBuffersMap.begin(); item != mWaylandBuffersMap.end(); ) {
1644 WaylandBuffer *wlbuf = (WaylandBuffer*)item->second;
1645 if (wlbuf->isFree()) {
1646 mWaylandBuffersMap.erase(item++);
1647 delete wlbuf;
1648 } else {
1649 item++;
1650 }
1651 }
1652}
1653
fei.dengf7a0cd32023-08-29 09:36:37 +00001654void WaylandDisplay::flushBuffers()
1655{
fei.dengb9a1a572023-09-13 01:33:57 +00001656 INFO(mLogCategory,"flushBuffers");
1657 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001658 for (auto item = mCommittedBufferMap.begin(); item != mCommittedBufferMap.end(); item++) {
1659 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1660 waylandbuf->forceRedrawing();
1661 handleFrameDisplayedCallback(waylandbuf);
1662 }
1663}
1664
1665void WaylandDisplay::cleanSurface()
1666{
1667 /* clear both video and parent surfaces */
1668 wl_surface_attach (mVideoSurfaceWrapper, NULL, 0, 0);
1669 wl_surface_commit (mVideoSurfaceWrapper);
1670 wl_surface_attach (mAreaSurfaceWrapper, NULL, 0, 0);
1671 wl_surface_commit (mAreaSurfaceWrapper);
fei.deng640c3c92024-04-12 08:31:19 +00001672}
1673
1674void WaylandDisplay::setKeepLastFrame(int keep)
1675{
1676 mKeepLastFrame = keep;
1677 if (mVideoSurfaceWrapper && mAmlConfigAPIList.enableKeepLastFrame) {
1678 INFO(mLogCategory,"keep last frame:%d",keep);
1679 wl_surface_keep_last_frame(mVideoSurfaceWrapper, keep);
fei.deng4029e682024-06-26 17:06:31 +08001680 mToSendKeepLastFrame = false;
1681 return;
fei.deng640c3c92024-04-12 08:31:19 +00001682 }
fei.deng4029e682024-06-26 17:06:31 +08001683 mToSendKeepLastFrame = true;
fei.deng6c425232024-07-19 16:15:31 +08001684}
1685
1686void WaylandDisplay::setFrameRate(int frameRateNum, int frameRateDenom)
1687{
1688 mFrameRateFractionNum = frameRateNum;
1689 mFrameRateFractionDenom = frameRateDenom;
1690 if (mFrameRateFractionDenom == 0) {
1691 mFrameRateFractionDenom = 1;
1692 }
1693 /*cobalt is not set frame rate to gst pipeline. we set default 59.94
1694 frame rate to weston*/
1695 if (mFrameRateFractionNum == 0 && mFrameRateFractionDenom == 1) {
1696 mFrameRateFractionNum = 59940;
1697 mFrameRateFractionDenom = 1000;
1698 }
1699 mFrameRateChanged = true;
1700 INFO(mLogCategory,"num:%d,denom:%d",mFrameRateFractionNum, mFrameRateFractionDenom);
fei.dengf7a0cd32023-08-29 09:36:37 +00001701}