blob: 3f6afaa50d865418b06a1e7d0b7cf2015f8db2ae [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.dengf7a0cd32023-08-29 09:36:37 +0000182static const struct wl_output_listener outputListener = {
183 WaylandDisplay::outputHandleGeometry,
184 WaylandDisplay::outputHandleMode,
185 WaylandDisplay::outputHandleDone,
fei.dengf1f5fc32023-12-06 06:22:20 +0000186 WaylandDisplay::outputHandleScale,
187 WaylandDisplay::outputHandleCrtcIndex,
fei.dengf7a0cd32023-08-29 09:36:37 +0000188};
189
190void WaylandDisplay::pointerHandleEnter(void *data, struct wl_pointer *pointer,
191 uint32_t serial, struct wl_surface *surface,
192 wl_fixed_t sx, wl_fixed_t sy)
193{
194 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
195
196 if (self->mFullScreen) {
197 wl_pointer_set_cursor(pointer, serial, NULL, 0, 0);
198 }
199}
200
201void WaylandDisplay::pointerHandleLeave(void *data, struct wl_pointer *pointer,
202 uint32_t serial, struct wl_surface *surface)
203{
204 UNUSED_PARAM(data);
205 UNUSED_PARAM(pointer);
206 UNUSED_PARAM(serial);
207 UNUSED_PARAM(surface);
208}
209
210void WaylandDisplay::pointerHandleMotion(void *data, struct wl_pointer *pointer,
211 uint32_t time, wl_fixed_t sx, wl_fixed_t sy)
212{
213 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
214 int x = wl_fixed_to_int(sx);
215 int y = wl_fixed_to_int(sy);
fei.dengb9a1a572023-09-13 01:33:57 +0000216 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 +0000217}
218
219void WaylandDisplay::pointerHandleButton(void *data, struct wl_pointer *wl_pointer,
220 uint32_t serial, uint32_t time, uint32_t button, uint32_t state)
221{
222 UNUSED_PARAM(data);
223 UNUSED_PARAM(wl_pointer);
224 UNUSED_PARAM(time);
225 UNUSED_PARAM(serial);
226 UNUSED_PARAM(button);
227 UNUSED_PARAM(state);
228}
229
230void WaylandDisplay::pointerHandleAxis(void *data, struct wl_pointer *wl_pointer,
231 uint32_t time, uint32_t axis, wl_fixed_t value)
232{
233 UNUSED_PARAM(data);
234 UNUSED_PARAM(wl_pointer);
235 UNUSED_PARAM(time);
236 UNUSED_PARAM(axis);
237 UNUSED_PARAM(value);
238}
239
240static const struct wl_pointer_listener pointer_listener = {
241 WaylandDisplay::pointerHandleEnter,
242 WaylandDisplay::pointerHandleLeave,
243 WaylandDisplay::pointerHandleMotion,
244 WaylandDisplay::pointerHandleButton,
245 WaylandDisplay::pointerHandleAxis,
246};
247
248void WaylandDisplay::touchHandleDown(void *data, struct wl_touch *wl_touch,
249 uint32_t serial, uint32_t time, struct wl_surface *surface,
250 int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
251{
252 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
253}
254
255void WaylandDisplay::touchHandleUp(void *data, struct wl_touch *wl_touch,
256 uint32_t serial, uint32_t time, int32_t id)
257{
258 UNUSED_PARAM(data);
259 UNUSED_PARAM(wl_touch);
260 UNUSED_PARAM(serial);
261 UNUSED_PARAM(time);
262 UNUSED_PARAM(id);
263}
264
265void WaylandDisplay::touchHandleMotion(void *data, struct wl_touch *wl_touch,
266 uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w)
267{
268 UNUSED_PARAM(data);
269 UNUSED_PARAM(wl_touch);
270 UNUSED_PARAM(time);
271 UNUSED_PARAM(id);
272 UNUSED_PARAM(x_w);
273 UNUSED_PARAM(y_w);
274}
275
276void WaylandDisplay::touchHandleFrame(void *data, struct wl_touch *wl_touch)
277{
278 UNUSED_PARAM(data);
279 UNUSED_PARAM(wl_touch);
280}
281
282void WaylandDisplay::touchHandleCancel(void *data, struct wl_touch *wl_touch)
283{
284 UNUSED_PARAM(data);
285 UNUSED_PARAM(wl_touch);
286}
287
288static const struct wl_touch_listener touch_listener = {
289 WaylandDisplay::touchHandleDown,
290 WaylandDisplay::touchHandleUp,
291 WaylandDisplay::touchHandleMotion,
292 WaylandDisplay::touchHandleFrame,
293 WaylandDisplay::touchHandleCancel,
294};
295
296void WaylandDisplay::keyboardHandleKeymap(void *data, struct wl_keyboard *keyboard,
297 uint32_t format, int fd, uint32_t size)
298{
299 UNUSED_PARAM(data);
300 UNUSED_PARAM(keyboard);
301 UNUSED_PARAM(format);
302 UNUSED_PARAM(fd);
303 UNUSED_PARAM(size);
304}
305
306void WaylandDisplay::keyboardHandleEnter(void *data, struct wl_keyboard *keyboard,
307 uint32_t serial, struct wl_surface *surface, struct wl_array *keys)
308{
309 UNUSED_PARAM(data);
310 UNUSED_PARAM(keyboard);
311 UNUSED_PARAM(serial);
312 UNUSED_PARAM(surface);
313 UNUSED_PARAM(keys);
314}
315
316void WaylandDisplay::keyboardHandleLeave(void *data, struct wl_keyboard *keyboard,
317 uint32_t serial, struct wl_surface *surface)
318{
319 UNUSED_PARAM(data);
320 UNUSED_PARAM(keyboard);
321 UNUSED_PARAM(serial);
322 UNUSED_PARAM(surface);
323}
324
325void WaylandDisplay::keyboardHandleKey(void *data, struct wl_keyboard *keyboard,
326 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
327{
328 UNUSED_PARAM(data);
329 UNUSED_PARAM(keyboard);
330 UNUSED_PARAM(serial);
331 UNUSED_PARAM(time);
332 UNUSED_PARAM(key);
333 UNUSED_PARAM(state);
334}
335
336void WaylandDisplay::keyboardHandleModifiers(void *data, struct wl_keyboard *keyboard,
337 uint32_t serial, uint32_t mods_depressed,
338 uint32_t mods_latched, uint32_t mods_locked,
339 uint32_t group)
340{
341 UNUSED_PARAM(data);
342 UNUSED_PARAM(keyboard);
343 UNUSED_PARAM(serial);
344 UNUSED_PARAM(mods_depressed);
345 UNUSED_PARAM(mods_latched);
346 UNUSED_PARAM(mods_locked);
347 UNUSED_PARAM(group);
348}
349
350static const struct wl_keyboard_listener keyboard_listener = {
351 WaylandDisplay::keyboardHandleKeymap,
352 WaylandDisplay::keyboardHandleEnter,
353 WaylandDisplay::keyboardHandleLeave,
354 WaylandDisplay::keyboardHandleKey,
355 WaylandDisplay::keyboardHandleModifiers,
356};
357
358void WaylandDisplay::seatHandleCapabilities(void *data, struct wl_seat *seat,
359 uint32_t caps)
360{
361 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
362 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !self->mPointer) {
363 self->mPointer = wl_seat_get_pointer(seat);
364 wl_pointer_add_listener(self->mPointer, &pointer_listener, data);
365 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && self->mPointer) {
366 wl_pointer_destroy(self->mPointer);
367 self->mPointer = NULL;
368 }
369
370 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !self->mKeyboard) {
371 self->mKeyboard = wl_seat_get_keyboard(seat);
372 wl_keyboard_add_listener(self->mKeyboard, &keyboard_listener, data);
373 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && self->mKeyboard) {
374 wl_keyboard_destroy(self->mKeyboard);
375 self->mKeyboard = NULL;
376 }
377
378 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !self->mTouch) {
379 self->mTouch = wl_seat_get_touch(seat);
380 wl_touch_set_user_data(self->mTouch, data);
381 wl_touch_add_listener(self->mTouch, &touch_listener, data);
382 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && self->mTouch) {
383 wl_touch_destroy(self->mTouch);
384 self->mTouch = NULL;
385 }
386}
387
388static const struct wl_seat_listener seat_listener = {
389 WaylandDisplay::seatHandleCapabilities,
390};
391
392
393void WaylandDisplay::handleXdgToplevelClose (void *data, struct xdg_toplevel *xdg_toplevel)
394{
395 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
396
fei.dengb9a1a572023-09-13 01:33:57 +0000397 INFO(self->mLogCategory,"XDG toplevel got a close event.");
fei.dengf7a0cd32023-08-29 09:36:37 +0000398}
399
400void WaylandDisplay::handleXdgToplevelConfigure (void *data, struct xdg_toplevel *xdg_toplevel,
401 int32_t width, int32_t height, struct wl_array *states)
402{
403 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
404 uint32_t *state;
405
fei.dengb9a1a572023-09-13 01:33:57 +0000406 INFO(self->mLogCategory, "XDG toplevel got a configure event, width:height [ %d, %d ].", width, height);
fei.dengf7a0cd32023-08-29 09:36:37 +0000407 /*
408 wl_array_for_each (state, states) {
409 switch (*state) {
410 case XDG_TOPLEVEL_STATE_FULLSCREEN:
411 case XDG_TOPLEVEL_STATE_MAXIMIZED:
412 case XDG_TOPLEVEL_STATE_RESIZING:
413 case XDG_TOPLEVEL_STATE_ACTIVATED:
414 break;
415 }
416 }
417 */
418
419 if (width <= 0 || height <= 0)
420 return;
421
fei.deng26950832023-11-09 03:00:23 +0000422 if (width == self->mCurrentDisplayOutput->width && height == self->mCurrentDisplayOutput->height && self->mUpdateRenderRectangle) {
423 self->mUpdateRenderRectangle = false;
424 self->setRenderRectangle(self->mCurrentDisplayOutput->offsetX,
425 self->mCurrentDisplayOutput->offsetY,
426 self->mCurrentDisplayOutput->width,
427 self->mCurrentDisplayOutput->height);
fei.dengf7a0cd32023-08-29 09:36:37 +0000428 } else{
429 self->setRenderRectangle(self->mRenderRect.x, self->mRenderRect.y, width, height);
430 }
431}
432
433static const struct xdg_toplevel_listener xdg_toplevel_listener = {
434 WaylandDisplay::handleXdgToplevelConfigure,
435 WaylandDisplay::handleXdgToplevelClose,
436};
437
438void WaylandDisplay::handleXdgSurfaceConfigure (void *data, struct xdg_surface *xdg_surface,
439 uint32_t serial)
440{
441 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
442 xdg_surface_ack_configure (xdg_surface, serial);
443
fei.dengb9a1a572023-09-13 01:33:57 +0000444 TRACE(self->mLogCategory,"handleXdgSurfaceConfigure");
445 Tls::Mutex::Autolock _l(self->mConfigureMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +0000446 self->mXdgSurfaceConfigured = true;
fei.deng26950832023-11-09 03:00:23 +0000447 self->updateDisplayOutput();
fei.dengf7a0cd32023-08-29 09:36:37 +0000448}
449
450static const struct xdg_surface_listener xdg_surface_listener = {
451 WaylandDisplay::handleXdgSurfaceConfigure,
452};
453
454void
455WaylandDisplay::registryHandleGlobal (void *data, struct wl_registry *registry,
fei.dengaf9b07d2023-10-10 07:38:40 +0000456 uint32_t name, const char *interface, uint32_t version)
fei.dengf7a0cd32023-08-29 09:36:37 +0000457{
458 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
fei.dengaf9b07d2023-10-10 07:38:40 +0000459 TRACE(self->mLogCategory,"registryHandleGlobal,name:%u,interface:%s,version:%d",name,interface,version);
fei.dengf7a0cd32023-08-29 09:36:37 +0000460
461 if (strcmp (interface, "wl_compositor") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000462 self->mCompositor = (struct wl_compositor *)wl_registry_bind (registry, name, &wl_compositor_interface, 1/*MIN (version, 3)*/);
fei.dengf7a0cd32023-08-29 09:36:37 +0000463 } else if (strcmp (interface, "wl_subcompositor") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000464 self->mSubCompositor = (struct wl_subcompositor *)wl_registry_bind (registry, name, &wl_subcompositor_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000465 } else if (strcmp (interface, "xdg_wm_base") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000466 self->mXdgWmBase = (struct xdg_wm_base *)wl_registry_bind (registry, name, &xdg_wm_base_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000467 xdg_wm_base_add_listener (self->mXdgWmBase, &xdg_wm_base_listener, (void *)self);
468 } else if (strcmp (interface, "wl_shm") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000469 self->mShm = (struct wl_shm *)wl_registry_bind (registry, name, &wl_shm_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000470 wl_shm_add_listener (self->mShm, &shm_listener, self);
471 } else if (strcmp (interface, "zwp_fullscreen_shell_v1") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000472 //self->mFullscreenShell = (struct zwp_fullscreen_shell_v1 *)wl_registry_bind (registry, name,
fei.dengf7a0cd32023-08-29 09:36:37 +0000473 // &zwp_fullscreen_shell_v1_interface, 1);
474 } else if (strcmp (interface, "wp_viewporter") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000475 self->mViewporter = (struct wp_viewporter *)wl_registry_bind (registry, name, &wp_viewporter_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000476 } else if (strcmp (interface, "zwp_linux_dmabuf_v1") == 0) {
477 if (version < 3)
478 return;
fei.dengaf9b07d2023-10-10 07:38:40 +0000479 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 +0000480 zwp_linux_dmabuf_v1_add_listener (self->mDmabuf, &dmabuf_listener, (void *)self);
481 } else if (strcmp (interface, "wl_output") == 0) {
fei.deng26950832023-11-09 03:00:23 +0000482 int i = 0;
483 uint32_t oriName = self->mCurrentDisplayOutput->name;
fei.dengaf9b07d2023-10-10 07:38:40 +0000484 for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
485 if (self->mOutput[i].wlOutput == NULL) {
486 self->mOutput[i].name = name;
487 self->mOutput[i].wlOutput = (struct wl_output*)wl_registry_bind(registry, name, &wl_output_interface, version);
fei.deng26950832023-11-09 03:00:23 +0000488 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 +0000489 wl_output_add_listener(self->mOutput[i].wlOutput, &outputListener, (void *)self);
490 if (i == 0) { //primary wl_output
491 self->mOutput[i].isPrimary = true;
492 }
fei.deng26950832023-11-09 03:00:23 +0000493 break;
fei.dengaf9b07d2023-10-10 07:38:40 +0000494 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000495 }
fei.deng26950832023-11-09 03:00:23 +0000496 if (i == DEFAULT_DISPLAY_OUTPUT_NUM) {
497 WARNING(self->mLogCategory,"Not enough free output");
498 }
499 //select current wl_output
500 if (self->mSelectOutputIndex != INVALID_OUTPUT_INDEX && !self->isRunning()) {
501 TRACE(self->mLogCategory,"select %d output",self->mSelectOutputIndex);
502 self->mCurrentDisplayOutput = &self->mOutput[self->mSelectOutputIndex];
503 }
504 //if user select a wrong output index, we using a suiteble wl_output
505 if (self->mCurrentDisplayOutput->wlOutput == NULL) {
506 WARNING(self->mLogCategory,"wl_output is null,we should find a suiteble output");
507 for (i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
508 if (self->mOutput[i].wlOutput) {
509 self->mCurrentDisplayOutput = &self->mOutput[i];
510 break;
511 }
512 }
513 }
514 //if current wl_output update, we should update render rectangle
515 if (self->mCurrentDisplayOutput->name != oriName) {
516 self->mUpdateRenderRectangle = true;
517 }
518 //if wl_output plugin,active sending frame
519 self->setRedrawingPending(false);
fei.dengf7a0cd32023-08-29 09:36:37 +0000520 } else if (strcmp(interface, "wl_seat") == 0) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000521 //self->mSeat = (struct wl_seat *)wl_registry_bind(registry, name, &wl_seat_interface, 1);
fei.dengf7a0cd32023-08-29 09:36:37 +0000522 //wl_seat_add_listener(self->mSeat, &seat_listener, (void *)self);
523 }
524}
525
526void
527WaylandDisplay::registryHandleGlobalRemove (void *data, struct wl_registry *registry, uint32_t name)
528{
fei.deng26950832023-11-09 03:00:23 +0000529 int i;
fei.dengf7a0cd32023-08-29 09:36:37 +0000530 WaylandDisplay *self = static_cast<WaylandDisplay *>(data);
fei.dengaf9b07d2023-10-10 07:38:40 +0000531 /* check wl_output changed */
532 DEBUG(self->mLogCategory,"wayland display remove registry handle global,name:%u",name);
fei.deng26950832023-11-09 03:00:23 +0000533 //if user selected wl_output removed, reset selected output index
534 if (self->mSelectOutputIndex != INVALID_OUTPUT_INDEX &&
535 self->mOutput[self->mSelectOutputIndex].wlOutput &&
536 self->mOutput[self->mSelectOutputIndex].name == name) {
537 self->mSelectOutputIndex = INVALID_OUTPUT_INDEX;
538 }
539 for (i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
fei.dengaf9b07d2023-10-10 07:38:40 +0000540 if (self->mOutput[i].name == name) {
fei.deng26950832023-11-09 03:00:23 +0000541 DEBUG(self->mLogCategory,"remove wl_output name:%u,wl_output:%p",name,self->mOutput[i].wlOutput);
fei.dengaf9b07d2023-10-10 07:38:40 +0000542 self->mOutput[i].name = 0;
543 self->mOutput[i].wlOutput = NULL;
fei.deng26950832023-11-09 03:00:23 +0000544 }
545 }
546 //if current output removed, select a suiteble output
547 if (self->mCurrentDisplayOutput->wlOutput == NULL) {
548 for (i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
549 if (self->mOutput[i].wlOutput) {
550 self->mCurrentDisplayOutput = &self->mOutput[i];
551 self->mUpdateRenderRectangle = true;
552 }
553 }
554 //set new output rectangle
555 if (self->mUpdateRenderRectangle) {
556 self->mUpdateRenderRectangle = false;
557 self->setRenderRectangle(self->mCurrentDisplayOutput->offsetX,
558 self->mCurrentDisplayOutput->offsetY,
559 self->mCurrentDisplayOutput->width,
560 self->mCurrentDisplayOutput->height);
fei.dengaf9b07d2023-10-10 07:38:40 +0000561 }
562 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000563}
564
565static const struct wl_registry_listener registry_listener = {
566 WaylandDisplay::registryHandleGlobal,
567 WaylandDisplay::registryHandleGlobalRemove
568};
569
fei.dengb9a1a572023-09-13 01:33:57 +0000570WaylandDisplay::WaylandDisplay(WaylandPlugin *plugin, int logCategory)
571 :mBufferMutex("bufferMutex"),
572 mWaylandPlugin(plugin),
573 mLogCategory(logCategory)
fei.dengf7a0cd32023-08-29 09:36:37 +0000574{
fei.dengb9a1a572023-09-13 01:33:57 +0000575 TRACE(mLogCategory,"construct WaylandDisplay");
fei.dengf7a0cd32023-08-29 09:36:37 +0000576 mWlDisplay = NULL;
577 mWlDisplayWrapper = NULL;
578 mWlQueue = NULL;
579 mRegistry = NULL;
580 mCompositor = NULL;
581 mXdgWmBase = NULL;
582 mViewporter = NULL;
583 mDmabuf = NULL;
584 mShm = NULL;
585 mSeat = NULL;
586 mPointer = NULL;
587 mTouch = NULL;
588 mKeyboard = NULL;
fei.deng26950832023-11-09 03:00:23 +0000589 mSelectOutputIndex = INVALID_OUTPUT_INDEX;
fei.dengb9a1a572023-09-13 01:33:57 +0000590 mPoll = new Tls::Poll(true);
fei.dengf7a0cd32023-08-29 09:36:37 +0000591 //window
592 mVideoWidth = 0;
593 mVideoHeight = 0;
594 mVideoSurface = NULL;
595 mXdgSurface = NULL;
596 mXdgToplevel = NULL;
597 mAreaViewport = NULL;
598 mVideoViewport = NULL;
599 mNoBorderUpdate = false;
600 mAreaShmBuffer = NULL;
601 mCommitCnt = 0;
602 mIsSendPtsToWeston = false;
603 mReCommitAreaSurface = false;
604 mAreaSurface = NULL;
605 mAreaSurfaceWrapper = NULL;
606 mVideoSurfaceWrapper = NULL;
607 mVideoSubSurface = NULL;
608 mXdgSurfaceConfigured = false;
609 mPip = 0;
610 mIsSendVideoPlaneId = true;
fei.deng26950832023-11-09 03:00:23 +0000611 mCurrentDisplayOutput = &mOutput[0];
612 mUpdateRenderRectangle = false;
fei.dengf7a0cd32023-08-29 09:36:37 +0000613 memset(&mRenderRect, 0, sizeof(struct Rectangle));
614 memset(&mVideoRect, 0, sizeof(struct Rectangle));
615 memset(&mWindowRect, 0, sizeof(struct Rectangle));
616 mFullScreen = true; //default is full screen
617 char *env = getenv("VIDEO_RENDER_SEND_PTS_TO_WESTON");
618 if (env) {
619 int isSet = atoi(env);
620 if (isSet > 0) {
621 mIsSendPtsToWeston = true;
fei.dengb9a1a572023-09-13 01:33:57 +0000622 INFO(mLogCategory,"set send pts to weston");
fei.dengf7a0cd32023-08-29 09:36:37 +0000623 } else {
624 mIsSendPtsToWeston = false;
fei.dengb9a1a572023-09-13 01:33:57 +0000625 INFO(mLogCategory,"do not send pts to weston");
fei.dengf7a0cd32023-08-29 09:36:37 +0000626 }
627 }
628 env = getenv("VIDEO_RENDER_SEND_VIDEO_PLANE_ID_TO_WESTON");
629 if (env) {
630 int isSet = atoi(env);
631 if (isSet == 0) {
632 mIsSendVideoPlaneId = false;
fei.dengb9a1a572023-09-13 01:33:57 +0000633 INFO(mLogCategory,"do not send video plane id to weston");
fei.dengf7a0cd32023-08-29 09:36:37 +0000634 } else {
635 mIsSendVideoPlaneId = true;
fei.dengb9a1a572023-09-13 01:33:57 +0000636 INFO(mLogCategory,"send video plane id to weston");
fei.dengf7a0cd32023-08-29 09:36:37 +0000637 }
638 }
639 for (int i = 0; i < DEFAULT_DISPLAY_OUTPUT_NUM; i++) {
640 mOutput[i].wlOutput = NULL;
641 mOutput[i].offsetX = 0;
642 mOutput[i].offsetY = 0;
643 mOutput[i].width = 0;
644 mOutput[i].height = 0;
645 mOutput[i].refreshRate = 0;
646 mOutput[i].isPrimary = false;
fei.dengaf9b07d2023-10-10 07:38:40 +0000647 mOutput[i].name = 0;
fei.deng75e8d392024-01-04 05:36:14 +0000648 mOutput[i].crtcIndex = 0;
fei.dengf7a0cd32023-08-29 09:36:37 +0000649 }
650}
651
652WaylandDisplay::~WaylandDisplay()
653{
fei.dengb9a1a572023-09-13 01:33:57 +0000654 TRACE(mLogCategory,"desconstruct WaylandDisplay");
655 if (mPoll) {
656 delete mPoll;
657 mPoll = NULL;
658 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000659}
660
661char *WaylandDisplay::require_xdg_runtime_dir()
662{
663 char *val = getenv("XDG_RUNTIME_DIR");
fei.dengb9a1a572023-09-13 01:33:57 +0000664 INFO(mLogCategory,"XDG_RUNTIME_DIR=%s",val);
fei.dengf7a0cd32023-08-29 09:36:37 +0000665
666 return val;
667}
668
669int WaylandDisplay::openDisplay()
670{
671 char *name = require_xdg_runtime_dir();
672 //DEBUG(mLogCategory,"name:%s",name);
fei.dengb9a1a572023-09-13 01:33:57 +0000673 DEBUG(mLogCategory,"openDisplay in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000674 mWlDisplay = wl_display_connect(NULL);
675 if (!mWlDisplay) {
fei.dengb9a1a572023-09-13 01:33:57 +0000676 ERROR(mLogCategory,"Failed to connect to the wayland display, XDG_RUNTIME_DIR='%s'",
fei.dengf7a0cd32023-08-29 09:36:37 +0000677 name ? name : "NULL");
fei.dengb9a1a572023-09-13 01:33:57 +0000678 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000679 }
680
681 mWlDisplayWrapper = (struct wl_display *)wl_proxy_create_wrapper ((void *)mWlDisplay);
682 mWlQueue = wl_display_create_queue (mWlDisplay);
683 wl_proxy_set_queue ((struct wl_proxy *)mWlDisplayWrapper, mWlQueue);
684
685 mRegistry = wl_display_get_registry (mWlDisplayWrapper);
686 wl_registry_add_listener (mRegistry, &registry_listener, (void *)this);
687
688 /* we need exactly 2 roundtrips to discover global objects and their state */
689 for (int i = 0; i < 2; i++) {
690 if (wl_display_roundtrip_queue (mWlDisplay, mWlQueue) < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000691 ERROR(mLogCategory,"Error communicating with the wayland display");
692 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000693 }
694 }
695
696 if (!mCompositor) {
fei.dengb9a1a572023-09-13 01:33:57 +0000697 ERROR(mLogCategory,"Could not bind to wl_compositor. Either it is not implemented in " \
fei.dengf7a0cd32023-08-29 09:36:37 +0000698 "the compositor, or the implemented version doesn't match");
fei.dengb9a1a572023-09-13 01:33:57 +0000699 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000700 }
701
702 if (!mDmabuf) {
fei.dengb9a1a572023-09-13 01:33:57 +0000703 ERROR(mLogCategory,"Could not bind to zwp_linux_dmabuf_v1");
704 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000705 }
706
707 if (!mXdgWmBase) {
708 /* If wl_surface and wl_display are passed via GstContext
709 * wl_shell, xdg_shell and zwp_fullscreen_shell are not used.
710 * In this case is correct to continue.
711 */
fei.dengb9a1a572023-09-13 01:33:57 +0000712 ERROR(mLogCategory,"Could not bind to either wl_shell, xdg_wm_base or "
fei.dengf7a0cd32023-08-29 09:36:37 +0000713 "zwp_fullscreen_shell, video display may not work properly.");
fei.dengb9a1a572023-09-13 01:33:57 +0000714 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000715 }
716
717 //create window surface
718 createCommonWindowSurface();
719 createXdgShellWindowSurface();
720
721 //config weston video plane
722 if (mIsSendVideoPlaneId) {
fei.dengb9a1a572023-09-13 01:33:57 +0000723 INFO(mLogCategory,"set weston video plane:%d",mPip);
fei.dengf7a0cd32023-08-29 09:36:37 +0000724 wl_surface_set_video_plane(mVideoSurfaceWrapper, mPip);
725 }
726
727 //run wl display queue dispatch
fei.dengb9a1a572023-09-13 01:33:57 +0000728 DEBUG(mLogCategory,"To run wl display dispatch queue");
fei.dengf7a0cd32023-08-29 09:36:37 +0000729 run("display queue");
730 mRedrawingPending = false;
731
fei.dengb9a1a572023-09-13 01:33:57 +0000732 DEBUG(mLogCategory,"openDisplay out");
733 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000734}
735
736void WaylandDisplay::closeDisplay()
737{
fei.dengb9a1a572023-09-13 01:33:57 +0000738 DEBUG(mLogCategory,"closeDisplay in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000739
740 if (isRunning()) {
fei.dengb9a1a572023-09-13 01:33:57 +0000741 TRACE(mLogCategory,"try stop dispatch thread");
742 if (mPoll) {
743 mPoll->setFlushing(true);
744 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000745 requestExitAndWait();
746 }
747
748 //first destroy window surface
749 destroyWindowSurfaces();
750
751 if (mViewporter) {
752 wp_viewporter_destroy (mViewporter);
753 mViewporter = NULL;
754 }
755
756 if (mDmabuf) {
757 zwp_linux_dmabuf_v1_destroy (mDmabuf);
758 mDmabuf = NULL;
759 }
760
761 if (mXdgWmBase) {
762 xdg_wm_base_destroy (mXdgWmBase);
763 mXdgWmBase = NULL;
764 }
765
766 if (mCompositor) {
767 wl_compositor_destroy (mCompositor);
768 mCompositor = NULL;
769 }
770
771 if (mSubCompositor) {
772 wl_subcompositor_destroy (mSubCompositor);
773 mSubCompositor = NULL;
774 }
775
776 if (mRegistry) {
777 wl_registry_destroy (mRegistry);
778 mRegistry= NULL;
779 }
780
781 if (mWlDisplayWrapper) {
782 wl_proxy_wrapper_destroy (mWlDisplayWrapper);
783 mWlDisplayWrapper = NULL;
784 }
785
786 if (mWlQueue) {
787 wl_event_queue_destroy (mWlQueue);
788 mWlQueue = NULL;
789 }
790
791 if (mWlDisplay) {
792 wl_display_flush (mWlDisplay);
793 wl_display_disconnect (mWlDisplay);
794 mWlDisplay = NULL;
795 }
796
fei.dengb9a1a572023-09-13 01:33:57 +0000797 DEBUG(mLogCategory,"closeDisplay out");
fei.dengf7a0cd32023-08-29 09:36:37 +0000798}
799
800int WaylandDisplay::toDmaBufferFormat(RenderVideoFormat format, uint32_t *outDmaformat /*out param*/, uint64_t *outDmaformatModifiers /*out param*/)
801{
802 if (!outDmaformat || !outDmaformatModifiers) {
fei.dengb9a1a572023-09-13 01:33:57 +0000803 WARNING(mLogCategory,"NULL params");
804 return ERROR_PARAM_NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000805 }
806
807 *outDmaformat = 0;
808 *outDmaformatModifiers = 0;
809
810 uint32_t dmaformat = video_format_to_wl_dmabuf_format (format);
811 if (dmaformat == -1) {
fei.dengb9a1a572023-09-13 01:33:57 +0000812 ERROR(mLogCategory,"Error not found render video format:%d to wl dmabuf format",format);
813 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000814 }
815
fei.dengb9a1a572023-09-13 01:33:57 +0000816 TRACE(mLogCategory,"render video format:%d -> dmabuf format:%d",format,dmaformat);
fei.dengf7a0cd32023-08-29 09:36:37 +0000817 *outDmaformat = (uint32_t)dmaformat;
818
819 /*get dmaformat and modifiers*/
820 auto item = mDmaBufferFormats.find(dmaformat);
821 if (item == mDmaBufferFormats.end()) { //not found
fei.dengb9a1a572023-09-13 01:33:57 +0000822 WARNING(mLogCategory,"Not found dmabuf for render video format :%d",format);
fei.dengf7a0cd32023-08-29 09:36:37 +0000823 *outDmaformatModifiers = 0;
fei.dengb9a1a572023-09-13 01:33:57 +0000824 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000825 }
826
827 *outDmaformatModifiers = (uint64_t)item->second;
828
fei.dengb9a1a572023-09-13 01:33:57 +0000829 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000830}
831
832int WaylandDisplay::toShmBufferFormat(RenderVideoFormat format, uint32_t *outformat)
833{
834 if (!outformat) {
fei.dengb9a1a572023-09-13 01:33:57 +0000835 WARNING(mLogCategory,"NULL params");
836 return ERROR_PARAM_NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000837 }
838
839 *outformat = 0;
840
841 int shmformat = (int)video_format_to_wl_shm_format(format);
842 if (shmformat < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000843 ERROR(mLogCategory,"Error not found render video format:%d to wl shmbuf format",format);
844 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000845 }
846
847 for (auto item = mShmFormats.begin(); item != mShmFormats.end(); ++item) {
848 uint32_t registFormat = (uint32_t)*item;
849 if (registFormat == (uint32_t)shmformat) {
850 *outformat = registFormat;
fei.dengb9a1a572023-09-13 01:33:57 +0000851 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000852 }
853 }
854
fei.dengb9a1a572023-09-13 01:33:57 +0000855 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000856}
857
858void WaylandDisplay::setVideoBufferFormat(RenderVideoFormat format)
859{
fei.dengb9a1a572023-09-13 01:33:57 +0000860 TRACE(mLogCategory,"set video buffer format: %d",format);
fei.dengf7a0cd32023-08-29 09:36:37 +0000861 mBufferFormat = format;
862};
863
864void WaylandDisplay::setDisplayOutput(int output)
865{
fei.dengb9a1a572023-09-13 01:33:57 +0000866 TRACE(mLogCategory,"select display output: %d",output);
fei.dengf7a0cd32023-08-29 09:36:37 +0000867 if (output < 0 || output >= DEFAULT_DISPLAY_OUTPUT_NUM) {
fei.dengb9a1a572023-09-13 01:33:57 +0000868 ERROR(mLogCategory, "display output index error,please set 0:primary or 1:extend,now:%d",output);
fei.dengf7a0cd32023-08-29 09:36:37 +0000869 return;
870 }
fei.deng26950832023-11-09 03:00:23 +0000871 //only do select output before video playing
872 if (mSelectOutputIndex != output) {
873 mSelectOutputIndex = output;
874 // if (mOutput[output].wlOutput) {
875 // mCurrentDisplayOutput = &mOutput[output];
876 // setRenderRectangle(mOutput[output].offsetX, mOutput[output].offsetY,
877 // mOutput[output].width, mOutput[output].height);
878 // }
fei.dengf7a0cd32023-08-29 09:36:37 +0000879 }
880}
881
882int WaylandDisplay::getDisplayOutput()
883{
fei.deng26950832023-11-09 03:00:23 +0000884 return mSelectOutputIndex == INVALID_OUTPUT_INDEX? 0: mSelectOutputIndex;
fei.dengf7a0cd32023-08-29 09:36:37 +0000885}
886
887void WaylandDisplay::setPip(int pip)
888{
fei.dengb9a1a572023-09-13 01:33:57 +0000889 INFO(mLogCategory,"set pip:%d",pip);
fei.dengf7a0cd32023-08-29 09:36:37 +0000890 mPip = pip;
891}
892
fei.deng26950832023-11-09 03:00:23 +0000893void WaylandDisplay::updateDisplayOutput()
894{
895 if (!mCurrentDisplayOutput->wlOutput || !mXdgToplevel || !mXdgSurface)
896 {
897 return;
898 }
899 if (mUpdateRenderRectangle) {
900 if (mFullScreen) {
901 DEBUG(mLogCategory,"unset full screen");
902 xdg_toplevel_unset_fullscreen (mXdgToplevel);
903 }
904
905 if (mXdgSurface) {
906 DEBUG(mLogCategory,"set geometry");
907 xdg_surface_set_window_geometry(mXdgSurface,
908 mCurrentDisplayOutput->offsetX,
909 mCurrentDisplayOutput->offsetY,
910 mCurrentDisplayOutput->width,
911 mCurrentDisplayOutput->height);
912 }
913
914 if (mFullScreen && mXdgToplevel) {
915 DEBUG(mLogCategory,"set full screen");
916 xdg_toplevel_set_fullscreen (mXdgToplevel, mCurrentDisplayOutput->wlOutput);
917 }
918 setRenderRectangle(mCurrentDisplayOutput->offsetX, mCurrentDisplayOutput->offsetY,
919 mCurrentDisplayOutput->width, mCurrentDisplayOutput->height);
920 mUpdateRenderRectangle = false;
921 }
922}
923
fei.dengf7a0cd32023-08-29 09:36:37 +0000924void WaylandDisplay::createCommonWindowSurface()
925{
926 struct wl_region *region;
927
928 mAreaSurface = wl_compositor_create_surface (mCompositor);
929 mVideoSurface = wl_compositor_create_surface (mCompositor);
930 mAreaSurfaceWrapper = (struct wl_surface *)wl_proxy_create_wrapper (mAreaSurface);
931 mVideoSurfaceWrapper = (struct wl_surface *)wl_proxy_create_wrapper (mVideoSurface);
932
933 wl_proxy_set_queue ((struct wl_proxy *) mAreaSurfaceWrapper, mWlQueue);
934 wl_proxy_set_queue ((struct wl_proxy *) mVideoSurfaceWrapper, mWlQueue);
935
936 /* embed video_surface in area_surface */
937 mVideoSubSurface = wl_subcompositor_get_subsurface (mSubCompositor, mVideoSurface, mAreaSurface);
938 wl_subsurface_set_desync (mVideoSubSurface);
939
940 if (mViewporter) {
941 mAreaViewport = wp_viewporter_get_viewport (mViewporter, mAreaSurface);
942 mVideoViewport = wp_viewporter_get_viewport (mViewporter, mVideoSurface);
943 }
944
945 /* do not accept input */
946 region = wl_compositor_create_region (mCompositor);
947 wl_surface_set_input_region (mAreaSurface, region);
948 wl_region_destroy (region);
949
950 region = wl_compositor_create_region (mCompositor);
951 wl_surface_set_input_region (mVideoSurface, region);
952 wl_region_destroy (region);
953}
954
955void WaylandDisplay::createXdgShellWindowSurface()
956{
957 /* Check which protocol we will use (in order of preference) */
958 if (mXdgWmBase) {
959 /* First create the XDG surface */
960 mXdgSurface= xdg_wm_base_get_xdg_surface (mXdgWmBase, mAreaSurface);
961 if (!mXdgSurface) {
fei.dengb9a1a572023-09-13 01:33:57 +0000962 ERROR(mLogCategory,"Unable to get xdg_surface");
fei.dengf7a0cd32023-08-29 09:36:37 +0000963 return;
964 }
965 xdg_surface_add_listener (mXdgSurface, &xdg_surface_listener,(void *)this);
966
967 /* Then the toplevel */
968 mXdgToplevel= xdg_surface_get_toplevel (mXdgSurface);
969 if (!mXdgSurface) {
fei.dengb9a1a572023-09-13 01:33:57 +0000970 ERROR(mLogCategory,"Unable to get xdg_toplevel");
fei.dengf7a0cd32023-08-29 09:36:37 +0000971 return;
972 }
973 xdg_toplevel_add_listener (mXdgToplevel, &xdg_toplevel_listener, this);
974
975 /* Finally, commit the xdg_surface state as toplevel */
976 mXdgSurfaceConfigured = false;
977 wl_surface_commit (mAreaSurface);
978 wl_display_flush (mWlDisplay);
979 /* we need exactly 3 roundtrips to discover global objects and their state */
980 for (int i = 0; i < 3; i++) {
981 if (wl_display_roundtrip_queue(mWlDisplay, mWlQueue) < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000982 ERROR(mLogCategory,"Error communicating with the wayland display");
fei.dengf7a0cd32023-08-29 09:36:37 +0000983 }
984 }
985
986 if (mXdgSurfaceConfigured) {
fei.dengb9a1a572023-09-13 01:33:57 +0000987 INFO(mLogCategory,"xdg surface had configured");
fei.dengf7a0cd32023-08-29 09:36:37 +0000988 } else {
fei.dengb9a1a572023-09-13 01:33:57 +0000989 WARNING(mLogCategory,"xdg surface not configured");
fei.dengf7a0cd32023-08-29 09:36:37 +0000990 }
991
992 //full screen show
fei.deng26950832023-11-09 03:00:23 +0000993 // if (mFullScreen && mCurrentDisplayOutput->wlOutput) {
994 // //ensureFullscreen(mFullScreen);
fei.dengf7a0cd32023-08-29 09:36:37 +0000995 // }
996 } else {
fei.dengb9a1a572023-09-13 01:33:57 +0000997 ERROR(mLogCategory,"Unable to use xdg_wm_base ");
fei.dengf7a0cd32023-08-29 09:36:37 +0000998 return;
999 }
1000}
1001
1002void WaylandDisplay::destroyWindowSurfaces()
1003{
1004 if (mAreaShmBuffer) {
1005 delete mAreaShmBuffer;
1006 mAreaShmBuffer = NULL;
1007 }
1008
1009 //clean all wayland buffers
1010 cleanAllWaylandBuffer();
1011
1012 if (mXdgToplevel) {
1013 xdg_toplevel_destroy (mXdgToplevel);
1014 mXdgToplevel = NULL;
1015 }
1016
1017 if (mXdgSurface) {
1018 xdg_surface_destroy (mXdgSurface);
1019 mXdgSurface = NULL;
1020 }
1021
1022 if (mVideoSurfaceWrapper) {
1023 wl_proxy_wrapper_destroy (mVideoSurfaceWrapper);
1024 mVideoSurfaceWrapper = NULL;
1025 }
1026
1027 if (mVideoSubSurface) {
1028 wl_subsurface_destroy (mVideoSubSurface);
1029 mVideoSubSurface = NULL;
1030 }
1031
1032 if (mVideoSurface) {
1033 wl_surface_destroy (mVideoSurface);
1034 mVideoSurface = NULL;
1035 }
1036
1037 if (mAreaSurfaceWrapper) {
1038 wl_proxy_wrapper_destroy (mAreaSurfaceWrapper);
1039 mAreaSurfaceWrapper = NULL;
1040 }
1041
1042 if (mAreaSurface) {
1043 wl_surface_destroy (mAreaSurface);
1044 mAreaSurface = NULL;
1045 mReCommitAreaSurface = false;
1046 }
1047}
1048
1049void WaylandDisplay::ensureFullscreen(bool fullscreen)
1050{
1051 if (mXdgWmBase) {
fei.dengb9a1a572023-09-13 01:33:57 +00001052 DEBUG(mLogCategory,"full screen : %d",fullscreen);
fei.dengf7a0cd32023-08-29 09:36:37 +00001053 if (fullscreen) {
fei.deng26950832023-11-09 03:00:23 +00001054 xdg_toplevel_set_fullscreen (mXdgToplevel, mCurrentDisplayOutput->wlOutput);
fei.dengf7a0cd32023-08-29 09:36:37 +00001055 } else {
1056 xdg_toplevel_unset_fullscreen (mXdgToplevel);
1057 }
1058 }
1059}
1060
1061void WaylandDisplay::setRenderRectangle(int x, int y, int w, int h)
1062{
fei.dengb9a1a572023-09-13 01:33:57 +00001063 DEBUG(mLogCategory,"set render rect:x:%d,y:%d,w:%d,h:%d",x,y,w,h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001064
1065 if (w <= 0 || h <= 0) {
fei.dengb9a1a572023-09-13 01:33:57 +00001066 WARNING(mLogCategory, "wrong render width or height %dx%d",w,h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001067 return;
1068 }
1069
1070 mRenderRect.x = x;
1071 mRenderRect.y = y;
1072 mRenderRect.w = w;
1073 mRenderRect.h = h;
1074
1075 if (!mXdgSurfaceConfigured) {
fei.dengb9a1a572023-09-13 01:33:57 +00001076 WARNING(mLogCategory,"Not configured xdg");
fei.dengf7a0cd32023-08-29 09:36:37 +00001077 return;
1078 }
1079
1080 if (mAreaViewport) {
1081 wp_viewport_set_destination (mAreaViewport, w, h);
1082 }
1083
1084 updateBorders();
1085
1086 if (mVideoWidth != 0 && mVideoSurface) {
1087 wl_subsurface_set_sync (mVideoSubSurface);
1088 resizeVideoSurface(true);
1089 }
1090
1091 wl_surface_damage (mAreaSurfaceWrapper, 0, 0, w, h);
1092 wl_surface_commit (mAreaSurfaceWrapper);
1093
1094 if (mVideoWidth != 0) {
1095 wl_subsurface_set_desync (mVideoSubSurface);
1096 }
1097}
1098
1099
1100void WaylandDisplay::setFrameSize(int w, int h)
1101{
1102 mVideoWidth = w;
1103 mVideoHeight = h;
fei.dengb9a1a572023-09-13 01:33:57 +00001104 TRACE(mLogCategory,"frame w:%d,h:%d",mVideoWidth,mVideoHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +00001105 if (mRenderRect.w > 0 && mVideoSurface) {
1106 resizeVideoSurface(true);
1107 }
1108}
1109
1110void WaylandDisplay::setWindowSize(int x, int y, int w, int h)
1111{
1112 mWindowRect.x = x;
1113 mWindowRect.y = y;
1114 mWindowRect.w = w;
1115 mWindowRect.h = h;
fei.dengb9a1a572023-09-13 01:33:57 +00001116 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 +00001117 if (mWindowRect.w > 0 && mVideoWidth > 0 && mVideoSurface) {
1118 //if had full screen, unset it and set window size
1119 if (mFullScreen) {
1120 mFullScreen = false;
1121 ensureFullscreen(mFullScreen);
1122 }
1123 resizeVideoSurface(true);
1124 }
1125}
1126
1127
1128void WaylandDisplay::resizeVideoSurface(bool commit)
1129{
1130 Rectangle src = {0,};
1131 Rectangle dst = {0,};
1132 Rectangle res;
1133
1134 /* center the video_subsurface inside area_subsurface */
1135 src.w = mVideoWidth;
1136 src.h = mVideoHeight;
1137 /*if had set the window size, we will scall
1138 video surface to this window size*/
1139 if (mWindowRect.w > 0 && mWindowRect.h > 0) {
1140 dst.x = mWindowRect.x;
1141 dst.y = mWindowRect.y;
1142 dst.w = mWindowRect.w;
1143 dst.h = mWindowRect.h;
1144 if (mWindowRect.w > mRenderRect.w && mWindowRect.h > mRenderRect.h) {
fei.dengb9a1a572023-09-13 01:33:57 +00001145 WARNING(mLogCategory,"Error window size:%dx%d, but render size:%dx%d,reset to render size",
fei.dengf7a0cd32023-08-29 09:36:37 +00001146 mWindowRect.w,mWindowRect.h,mRenderRect.w,mRenderRect.h);
1147 dst.x = mRenderRect.x;
1148 dst.y = mRenderRect.y;
1149 dst.w = mRenderRect.w;
1150 dst.h = mRenderRect.h;
1151 }
1152 //to do,we need set geometry?
1153 //if (mXdgSurface) {
1154 // xdg_surface_set_window_geometry(mXdgSurface, mWindowRect.x, mWindowRect.y, mWindowRect.w, mWindowRect.h);
1155 //}
1156 } else { //scal video to full screen
1157 dst.w = mRenderRect.w;
1158 dst.h = mRenderRect.h;
1159 }
1160
1161 if (mViewporter) {
1162 videoCenterRect(src, dst, &res, true);
1163 } else {
1164 videoCenterRect(src, dst, &res, false);
1165 }
1166
1167 wl_subsurface_set_position (mVideoSubSurface, res.x, res.y);
1168
1169 if (commit) {
1170 wl_surface_damage (mVideoSurfaceWrapper, 0, 0, res.w, res.h);
1171 wl_surface_commit (mVideoSurfaceWrapper);
1172 }
1173
1174 //top level setting
1175 if (mXdgToplevel) {
1176 struct wl_region *region;
1177
1178 region = wl_compositor_create_region (mCompositor);
1179 wl_region_add (region, 0, 0, mRenderRect.w, mRenderRect.h);
1180 wl_surface_set_input_region (mAreaSurface, region);
1181 wl_region_destroy (region);
1182 }
1183
1184 /* this is saved for use in wl_surface_damage */
1185 mVideoRect.x = res.x;
1186 mVideoRect.y = res.y;
1187 mVideoRect.w = res.w;
1188 mVideoRect.h = res.h;
1189
1190 //to scale video surface to full screen
1191 wp_viewport_set_destination(mVideoViewport, res.w, res.h);
fei.dengb9a1a572023-09-13 01:33:57 +00001192 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 +00001193}
1194
1195void WaylandDisplay::setOpaque()
1196{
1197 struct wl_region *region;
1198
1199 /* Set area opaque */
1200 region = wl_compositor_create_region (mCompositor);
1201 wl_region_add (region, 0, 0, mRenderRect.w, mRenderRect.h);
1202 wl_surface_set_opaque_region (mAreaSurface, region);
1203 wl_region_destroy (region);
1204}
1205
1206int WaylandDisplay::prepareFrameBuffer(RenderBuffer * buf)
1207{
1208 WaylandBuffer *waylandBuf = NULL;
1209 int ret;
1210
1211 waylandBuf = findWaylandBuffer(buf);
1212 if (waylandBuf == NULL) {
fei.dengb9a1a572023-09-13 01:33:57 +00001213 waylandBuf = new WaylandBuffer(this, mLogCategory);
fei.dengf7a0cd32023-08-29 09:36:37 +00001214 waylandBuf->setBufferFormat(mBufferFormat);
1215 ret = waylandBuf->constructWlBuffer(buf);
fei.dengb9a1a572023-09-13 01:33:57 +00001216 if (ret != NO_ERROR) {
1217 WARNING(mLogCategory,"dmabufConstructWlBuffer fail,release waylandbuf");
fei.dengf7a0cd32023-08-29 09:36:37 +00001218 //delete waylanBuf,WaylandBuffer object destruct will call release callback
1219 goto waylandbuf_fail;
1220 } else {
1221 addWaylandBuffer(buf, waylandBuf);
1222 }
1223 }
fei.dengb9a1a572023-09-13 01:33:57 +00001224 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +00001225waylandbuf_fail:
1226 //delete waylandbuf
1227 delete waylandBuf;
1228 waylandBuf = NULL;
fei.dengb9a1a572023-09-13 01:33:57 +00001229 return ERROR_UNKNOWN;
fei.dengf7a0cd32023-08-29 09:36:37 +00001230}
1231
1232void WaylandDisplay::displayFrameBuffer(RenderBuffer * buf, int64_t realDisplayTime)
1233{
1234 WaylandBuffer *waylandBuf = NULL;
1235 struct wl_buffer * wlbuffer = NULL;
1236 int ret;
1237
1238 if (!buf) {
fei.dengb9a1a572023-09-13 01:33:57 +00001239 ERROR(mLogCategory,"Error input params, waylandbuffer is null");
fei.dengf7a0cd32023-08-29 09:36:37 +00001240 return;
1241 }
1242
1243 //must commit areasurface first, because weston xdg surface maybe timeout
1244 //this cause video is not display,commit can resume xdg surface
1245 if (!mReCommitAreaSurface) {
1246 mReCommitAreaSurface = true;
1247 wl_surface_commit (mAreaSurface);
1248 }
1249
fei.dengb9a1a572023-09-13 01:33:57 +00001250 TRACE(mLogCategory,"display renderBuffer:%p,PTS:%lld,realtime:%lld",buf, buf->pts, realDisplayTime);
fei.dengf7a0cd32023-08-29 09:36:37 +00001251
1252 if (buf->flag & BUFFER_FLAG_DMA_BUFFER) {
1253 if (buf->dma.width <=0 || buf->dma.height <=0) {
1254 buf->dma.width = mVideoWidth;
1255 buf->dma.height = mVideoHeight;
1256 }
1257 waylandBuf = findWaylandBuffer(buf);
1258 if (waylandBuf) {
1259 waylandBuf->setRenderRealTime(realDisplayTime);
1260 ret = waylandBuf->constructWlBuffer(buf);
fei.dengb9a1a572023-09-13 01:33:57 +00001261 if (ret != NO_ERROR) {
1262 WARNING(mLogCategory,"dmabufConstructWlBuffer fail,release waylandbuf");
fei.dengf7a0cd32023-08-29 09:36:37 +00001263 //delete waylanBuf,WaylandBuffer object destruct will call release callback
1264 goto waylandbuf_fail;
1265 }
1266 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001267 ERROR(mLogCategory,"NOT found wayland buffer,please prepare buffer first");
fei.dengf7a0cd32023-08-29 09:36:37 +00001268 goto waylandbuf_fail;
1269 }
1270 }
1271
1272 if (waylandBuf) {
1273 wlbuffer = waylandBuf->getWlBuffer();
1274 }
fei.deng1cfb2752023-10-26 08:01:25 +00001275 //if no wl_output, drop this buffer
fei.deng26950832023-11-09 03:00:23 +00001276 if (mCurrentDisplayOutput->wlOutput == NULL) {
fei.deng1cfb2752023-10-26 08:01:25 +00001277 TRACE(mLogCategory,"No wl_output");
1278 mWaylandPlugin->handleFrameDropped(buf);
1279 mWaylandPlugin->handleBufferRelease(buf);
1280 return;
1281 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001282 if (wlbuffer) {
fei.dengb9a1a572023-09-13 01:33:57 +00001283 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001284 ++mCommitCnt;
1285 uint32_t hiPts = realDisplayTime >> 32;
1286 uint32_t lowPts = realDisplayTime & 0xFFFFFFFF;
1287 //attach this wl_buffer to weston
fei.dengb9a1a572023-09-13 01:33:57 +00001288 TRACE(mLogCategory,"++attach,renderbuf:%p,wl_buffer:%p(0,0,%d,%d),pts:%lld,commitCnt:%d",buf,wlbuffer,mVideoRect.w,mVideoRect.h,buf->pts,mCommitCnt);
fei.dengf7a0cd32023-08-29 09:36:37 +00001289 waylandBuf->attach(mVideoSurfaceWrapper);
1290
1291 if (mIsSendPtsToWeston) {
fei.dengb9a1a572023-09-13 01:33:57 +00001292 TRACE(mLogCategory,"display time:%lld,hiPts:%u,lowPts:%u",realDisplayTime, hiPts, lowPts);
fei.dengf7a0cd32023-08-29 09:36:37 +00001293 wl_surface_set_pts(mVideoSurfaceWrapper, hiPts, lowPts);
1294 }
1295
1296 wl_surface_damage (mVideoSurfaceWrapper, 0, 0, mVideoRect.w, mVideoRect.h);
1297 wl_surface_commit (mVideoSurfaceWrapper);
1298 //insert this buffer to committed weston buffer manager
1299 std::pair<int64_t, WaylandBuffer *> item(buf->pts, waylandBuf);
1300 mCommittedBufferMap.insert(item);
1301 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001302 WARNING(mLogCategory,"wlbuffer is NULL");
fei.dengf7a0cd32023-08-29 09:36:37 +00001303 /* clear both video and parent surfaces */
1304 cleanSurface();
1305 }
1306
1307 wl_display_flush (mWlDisplay);
1308
1309 return;
1310waylandbuf_fail:
1311 //notify dropped
1312 mWaylandPlugin->handleFrameDropped(buf);
1313 //notify app release this buf
1314 mWaylandPlugin->handleBufferRelease(buf);
1315 //delete waylandbuf
1316 delete waylandBuf;
1317 waylandBuf = NULL;
1318 return;
1319}
1320
1321void WaylandDisplay::handleBufferReleaseCallback(WaylandBuffer *buf)
1322{
1323 {
fei.dengb9a1a572023-09-13 01:33:57 +00001324 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001325 --mCommitCnt;
1326 //remove buffer if this buffer is ready to release
1327 RenderBuffer *renderBuffer = buf->getRenderBuffer();
1328 auto item = mCommittedBufferMap.find(renderBuffer->pts);
1329 if (item != mCommittedBufferMap.end()) {
1330 mCommittedBufferMap.erase(item);
1331 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001332 WARNING(mLogCategory,"Can't find WaylandBuffer in buffer map");
fei.dengf7a0cd32023-08-29 09:36:37 +00001333 return;
1334 }
1335 }
1336 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengb9a1a572023-09-13 01:33:57 +00001337 TRACE(mLogCategory,"handle release renderBuffer :%p,priv:%p,PTS:%lld,realtime:%lld us,commitCnt:%d",renderBuffer,renderBuffer->priv,renderBuffer->pts/1000,buf->getRenderRealTime(),mCommitCnt);
fei.dengf7a0cd32023-08-29 09:36:37 +00001338 mWaylandPlugin->handleBufferRelease(renderBuffer);
1339}
1340
1341void WaylandDisplay::handleFrameDisplayedCallback(WaylandBuffer *buf)
1342{
1343 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengb9a1a572023-09-13 01:33:57 +00001344 TRACE(mLogCategory,"handle displayed renderBuffer :%p,PTS:%lld us,realtime:%lld us",renderBuffer,renderBuffer->pts/1000,buf->getRenderRealTime());
fei.dengf7a0cd32023-08-29 09:36:37 +00001345 mWaylandPlugin->handleFrameDisplayed(renderBuffer);
1346}
1347
1348void WaylandDisplay::handleFrameDropedCallback(WaylandBuffer *buf)
1349{
1350 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengb9a1a572023-09-13 01:33:57 +00001351 TRACE(mLogCategory,"handle droped renderBuffer :%p,PTS:%lld us,realtime:%lld us",renderBuffer,renderBuffer->pts/1000,buf->getRenderRealTime());
fei.dengf7a0cd32023-08-29 09:36:37 +00001352 mWaylandPlugin->handleFrameDropped(renderBuffer);
1353}
1354
1355
1356void WaylandDisplay::readyToRun()
1357{
1358 mFd = wl_display_get_fd (mWlDisplay);
fei.dengb9a1a572023-09-13 01:33:57 +00001359 if (mPoll) {
1360 mPoll->addFd(mFd);
1361 mPoll->setFdReadable(mFd, true);
1362 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001363}
1364
1365bool WaylandDisplay::threadLoop()
1366{
1367 int ret;
fei.dengf7a0cd32023-08-29 09:36:37 +00001368
1369 while (wl_display_prepare_read_queue (mWlDisplay, mWlQueue) != 0) {
1370 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1371 }
1372
1373 wl_display_flush (mWlDisplay);
1374
1375 /*poll timeout value must > 300 ms,otherwise zwp_linux_dmabuf will create failed,
1376 so do use -1 to wait for ever*/
fei.dengb9a1a572023-09-13 01:33:57 +00001377 ret = mPoll->wait(-1); //wait for ever
fei.dengf7a0cd32023-08-29 09:36:37 +00001378 if (ret < 0) { //poll error
fei.dengb9a1a572023-09-13 01:33:57 +00001379 WARNING(mLogCategory,"poll error");
fei.dengf7a0cd32023-08-29 09:36:37 +00001380 wl_display_cancel_read(mWlDisplay);
1381 return false;
1382 } else if (ret == 0) { //poll time out
1383 return true; //run loop
1384 }
1385
1386 if (wl_display_read_events (mWlDisplay) == -1) {
1387 goto tag_error;
1388 }
1389
1390 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1391 return true;
1392tag_error:
fei.dengb9a1a572023-09-13 01:33:57 +00001393 ERROR(mLogCategory,"Error communicating with the wayland server");
fei.dengf7a0cd32023-08-29 09:36:37 +00001394 return false;
1395}
1396
1397void WaylandDisplay::videoCenterRect(Rectangle src, Rectangle dst, Rectangle *result, bool scaling)
1398{
1399 //if dst is a small window, we scale video to map window size,don't doing center
1400 if (mRenderRect.w != dst.w && mRenderRect.h != dst.h) {
1401 result->x = dst.x;
1402 result->y = dst.y;
1403 result->w = dst.w;
1404 result->h = dst.h;
fei.dengb9a1a572023-09-13 01:33:57 +00001405 TRACE(mLogCategory,"small window source is %dx%d dest is %dx%d, result is %dx%d with x,y %dx%d",
fei.dengf7a0cd32023-08-29 09:36:37 +00001406 src.w, src.h, dst.w, dst.h, result->w, result->h, result->x, result->y);
1407 return;
1408 }
1409 if (!scaling) {
1410 result->w = MIN (src.w, dst.w);
1411 result->h = MIN (src.h, dst.h);
1412 result->x = dst.x + (dst.w - result->w) / 2;
1413 result->y = dst.y + (dst.h - result->h) / 2;
1414 } else {
1415 double src_ratio, dst_ratio;
1416
1417 src_ratio = (double) src.w / src.h;
1418 dst_ratio = (double) dst.w / dst.h;
1419
1420 if (src_ratio > dst_ratio) {
1421 result->w = dst.w;
1422 result->h = dst.w / src_ratio;
1423 result->x = dst.x;
1424 result->y = dst.y + (dst.h - result->h) / 2;
1425 } else if (src_ratio < dst_ratio) {
1426 result->w = dst.h * src_ratio;
1427 result->h = dst.h;
1428 result->x = dst.x + (dst.w - result->w) / 2;
1429 result->y = dst.y;
1430 } else {
1431 result->x = dst.x;
1432 result->y = dst.y;
1433 result->w = dst.w;
1434 result->h = dst.h;
1435 }
1436 }
1437
fei.dengb9a1a572023-09-13 01:33:57 +00001438 TRACE(mLogCategory,"source is %dx%d dest is %dx%d, result is %dx%d with x,y %dx%d",
fei.dengf7a0cd32023-08-29 09:36:37 +00001439 src.w, src.h, dst.w, dst.h, result->w, result->h, result->x, result->y);
1440}
1441
1442void WaylandDisplay::updateBorders()
1443{
1444 int width,height;
1445
1446 if (mNoBorderUpdate)
1447 return;
1448
1449 if (mViewporter) {
1450 width = height = 1;
1451 mNoBorderUpdate = true;
1452 } else {
1453 width = mRenderRect.w;
1454 height = mRenderRect.h;
1455 }
1456
1457 RenderVideoFormat format = VIDEO_FORMAT_BGRA;
fei.dengb9a1a572023-09-13 01:33:57 +00001458 mAreaShmBuffer = new WaylandShmBuffer(this, mLogCategory);
fei.dengf7a0cd32023-08-29 09:36:37 +00001459 struct wl_buffer *wlbuf = mAreaShmBuffer->constructWlBuffer(width, height, format);
1460 if (wlbuf == NULL) {
1461 delete mAreaShmBuffer;
1462 mAreaShmBuffer = NULL;
1463 }
1464
1465 wl_surface_attach(mAreaSurfaceWrapper, wlbuf, 0, 0);
1466}
1467
1468std::size_t WaylandDisplay::calculateDmaBufferHash(RenderDmaBuffer &dmabuf)
1469{
1470 std::string hashString("");
1471 for (int i = 0; i < dmabuf.planeCnt; i++) {
1472 char hashtmp[1024];
1473 snprintf (hashtmp, 1024, "%d%d%d%d%d%d", dmabuf.width,dmabuf.height,dmabuf.planeCnt,
1474 dmabuf.stride[i],dmabuf.offset[i],dmabuf.fd[i]);
1475 std::string tmp(hashtmp);
1476 hashString += tmp;
1477 }
1478
1479 std::size_t hashval = std::hash<std::string>()(hashString);
fei.dengb9a1a572023-09-13 01:33:57 +00001480 //TRACE(mLogCategory,"hashstr:%s,val:%zu",hashString.c_str(),hashval);
fei.dengf7a0cd32023-08-29 09:36:37 +00001481 return hashval;
1482}
1483
1484void WaylandDisplay::addWaylandBuffer(RenderBuffer * buf, WaylandBuffer *waylandbuf)
1485{
1486 if (buf->flag & BUFFER_FLAG_DMA_BUFFER) {
1487 std::size_t hashval = calculateDmaBufferHash(buf->dma);
1488 std::pair<std::size_t, WaylandBuffer *> item(hashval, waylandbuf);
fei.dengb9a1a572023-09-13 01:33:57 +00001489 //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 +00001490 mWaylandBuffersMap.insert(item);
1491 }
1492
1493 //clean invalid wayland buffer,if video resolution changed
1494 for (auto item = mWaylandBuffersMap.begin(); item != mWaylandBuffersMap.end(); ) {
1495 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1496 if ((waylandbuf->getFrameWidth() != buf->dma.width ||
1497 waylandbuf->getFrameHeight() != buf->dma.height) &&
1498 waylandbuf->isFree()) {
fei.dengb9a1a572023-09-13 01:33:57 +00001499 TRACE(mLogCategory,"delete wayland buffer,width:%d(%d),height:%d(%d)",
fei.dengf7a0cd32023-08-29 09:36:37 +00001500 waylandbuf->getFrameWidth(),buf->dma.width,
1501 waylandbuf->getFrameHeight(),buf->dma.height);
1502 mWaylandBuffersMap.erase(item++);
1503 delete waylandbuf;
1504 } else {
1505 item++;
1506 }
1507 }
fei.dengb9a1a572023-09-13 01:33:57 +00001508 TRACE(mLogCategory,"mWaylandBuffersMap size:%d",mWaylandBuffersMap.size());
fei.dengf7a0cd32023-08-29 09:36:37 +00001509}
1510
1511WaylandBuffer* WaylandDisplay::findWaylandBuffer(RenderBuffer * buf)
1512{
1513 std::size_t hashval = calculateDmaBufferHash(buf->dma);
1514 auto item = mWaylandBuffersMap.find(hashval);
1515 if (item == mWaylandBuffersMap.end()) {
1516 return NULL;
1517 }
1518
1519 return (WaylandBuffer*) item->second;
1520}
1521
1522void WaylandDisplay::cleanAllWaylandBuffer()
1523{
1524 //free all obtain buff
1525 for (auto item = mWaylandBuffersMap.begin(); item != mWaylandBuffersMap.end(); ) {
1526 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1527 mWaylandBuffersMap.erase(item++);
1528 delete waylandbuf;
1529 }
1530}
1531
1532void WaylandDisplay::flushBuffers()
1533{
fei.dengb9a1a572023-09-13 01:33:57 +00001534 INFO(mLogCategory,"flushBuffers");
1535 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001536 for (auto item = mCommittedBufferMap.begin(); item != mCommittedBufferMap.end(); item++) {
1537 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1538 waylandbuf->forceRedrawing();
1539 handleFrameDisplayedCallback(waylandbuf);
1540 }
1541}
1542
1543void WaylandDisplay::cleanSurface()
1544{
1545 /* clear both video and parent surfaces */
1546 wl_surface_attach (mVideoSurfaceWrapper, NULL, 0, 0);
1547 wl_surface_commit (mVideoSurfaceWrapper);
1548 wl_surface_attach (mAreaSurfaceWrapper, NULL, 0, 0);
1549 wl_surface_commit (mAreaSurfaceWrapper);
1550}