blob: 2b2831a1e728926e55c3e71f0ff8d23450c803c1 [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.dengf7a0cd32023-08-29 09:36:37 +0000648 }
649}
650
651WaylandDisplay::~WaylandDisplay()
652{
fei.dengb9a1a572023-09-13 01:33:57 +0000653 TRACE(mLogCategory,"desconstruct WaylandDisplay");
654 if (mPoll) {
655 delete mPoll;
656 mPoll = NULL;
657 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000658}
659
660char *WaylandDisplay::require_xdg_runtime_dir()
661{
662 char *val = getenv("XDG_RUNTIME_DIR");
fei.dengb9a1a572023-09-13 01:33:57 +0000663 INFO(mLogCategory,"XDG_RUNTIME_DIR=%s",val);
fei.dengf7a0cd32023-08-29 09:36:37 +0000664
665 return val;
666}
667
668int WaylandDisplay::openDisplay()
669{
670 char *name = require_xdg_runtime_dir();
671 //DEBUG(mLogCategory,"name:%s",name);
fei.dengb9a1a572023-09-13 01:33:57 +0000672 DEBUG(mLogCategory,"openDisplay in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000673 mWlDisplay = wl_display_connect(NULL);
674 if (!mWlDisplay) {
fei.dengb9a1a572023-09-13 01:33:57 +0000675 ERROR(mLogCategory,"Failed to connect to the wayland display, XDG_RUNTIME_DIR='%s'",
fei.dengf7a0cd32023-08-29 09:36:37 +0000676 name ? name : "NULL");
fei.dengb9a1a572023-09-13 01:33:57 +0000677 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000678 }
679
680 mWlDisplayWrapper = (struct wl_display *)wl_proxy_create_wrapper ((void *)mWlDisplay);
681 mWlQueue = wl_display_create_queue (mWlDisplay);
682 wl_proxy_set_queue ((struct wl_proxy *)mWlDisplayWrapper, mWlQueue);
683
684 mRegistry = wl_display_get_registry (mWlDisplayWrapper);
685 wl_registry_add_listener (mRegistry, &registry_listener, (void *)this);
686
687 /* we need exactly 2 roundtrips to discover global objects and their state */
688 for (int i = 0; i < 2; i++) {
689 if (wl_display_roundtrip_queue (mWlDisplay, mWlQueue) < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000690 ERROR(mLogCategory,"Error communicating with the wayland display");
691 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000692 }
693 }
694
695 if (!mCompositor) {
fei.dengb9a1a572023-09-13 01:33:57 +0000696 ERROR(mLogCategory,"Could not bind to wl_compositor. Either it is not implemented in " \
fei.dengf7a0cd32023-08-29 09:36:37 +0000697 "the compositor, or the implemented version doesn't match");
fei.dengb9a1a572023-09-13 01:33:57 +0000698 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000699 }
700
701 if (!mDmabuf) {
fei.dengb9a1a572023-09-13 01:33:57 +0000702 ERROR(mLogCategory,"Could not bind to zwp_linux_dmabuf_v1");
703 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000704 }
705
706 if (!mXdgWmBase) {
707 /* If wl_surface and wl_display are passed via GstContext
708 * wl_shell, xdg_shell and zwp_fullscreen_shell are not used.
709 * In this case is correct to continue.
710 */
fei.dengb9a1a572023-09-13 01:33:57 +0000711 ERROR(mLogCategory,"Could not bind to either wl_shell, xdg_wm_base or "
fei.dengf7a0cd32023-08-29 09:36:37 +0000712 "zwp_fullscreen_shell, video display may not work properly.");
fei.dengb9a1a572023-09-13 01:33:57 +0000713 return ERROR_OPEN_FAIL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000714 }
715
716 //create window surface
717 createCommonWindowSurface();
718 createXdgShellWindowSurface();
719
720 //config weston video plane
721 if (mIsSendVideoPlaneId) {
fei.dengb9a1a572023-09-13 01:33:57 +0000722 INFO(mLogCategory,"set weston video plane:%d",mPip);
fei.dengf7a0cd32023-08-29 09:36:37 +0000723 wl_surface_set_video_plane(mVideoSurfaceWrapper, mPip);
724 }
725
726 //run wl display queue dispatch
fei.dengb9a1a572023-09-13 01:33:57 +0000727 DEBUG(mLogCategory,"To run wl display dispatch queue");
fei.dengf7a0cd32023-08-29 09:36:37 +0000728 run("display queue");
729 mRedrawingPending = false;
730
fei.dengb9a1a572023-09-13 01:33:57 +0000731 DEBUG(mLogCategory,"openDisplay out");
732 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000733}
734
735void WaylandDisplay::closeDisplay()
736{
fei.dengb9a1a572023-09-13 01:33:57 +0000737 DEBUG(mLogCategory,"closeDisplay in");
fei.dengf7a0cd32023-08-29 09:36:37 +0000738
739 if (isRunning()) {
fei.dengb9a1a572023-09-13 01:33:57 +0000740 TRACE(mLogCategory,"try stop dispatch thread");
741 if (mPoll) {
742 mPoll->setFlushing(true);
743 }
fei.dengf7a0cd32023-08-29 09:36:37 +0000744 requestExitAndWait();
745 }
746
747 //first destroy window surface
748 destroyWindowSurfaces();
749
750 if (mViewporter) {
751 wp_viewporter_destroy (mViewporter);
752 mViewporter = NULL;
753 }
754
755 if (mDmabuf) {
756 zwp_linux_dmabuf_v1_destroy (mDmabuf);
757 mDmabuf = NULL;
758 }
759
760 if (mXdgWmBase) {
761 xdg_wm_base_destroy (mXdgWmBase);
762 mXdgWmBase = NULL;
763 }
764
765 if (mCompositor) {
766 wl_compositor_destroy (mCompositor);
767 mCompositor = NULL;
768 }
769
770 if (mSubCompositor) {
771 wl_subcompositor_destroy (mSubCompositor);
772 mSubCompositor = NULL;
773 }
774
775 if (mRegistry) {
776 wl_registry_destroy (mRegistry);
777 mRegistry= NULL;
778 }
779
780 if (mWlDisplayWrapper) {
781 wl_proxy_wrapper_destroy (mWlDisplayWrapper);
782 mWlDisplayWrapper = NULL;
783 }
784
785 if (mWlQueue) {
786 wl_event_queue_destroy (mWlQueue);
787 mWlQueue = NULL;
788 }
789
790 if (mWlDisplay) {
791 wl_display_flush (mWlDisplay);
792 wl_display_disconnect (mWlDisplay);
793 mWlDisplay = NULL;
794 }
795
fei.dengb9a1a572023-09-13 01:33:57 +0000796 DEBUG(mLogCategory,"closeDisplay out");
fei.dengf7a0cd32023-08-29 09:36:37 +0000797}
798
799int WaylandDisplay::toDmaBufferFormat(RenderVideoFormat format, uint32_t *outDmaformat /*out param*/, uint64_t *outDmaformatModifiers /*out param*/)
800{
801 if (!outDmaformat || !outDmaformatModifiers) {
fei.dengb9a1a572023-09-13 01:33:57 +0000802 WARNING(mLogCategory,"NULL params");
803 return ERROR_PARAM_NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000804 }
805
806 *outDmaformat = 0;
807 *outDmaformatModifiers = 0;
808
809 uint32_t dmaformat = video_format_to_wl_dmabuf_format (format);
810 if (dmaformat == -1) {
fei.dengb9a1a572023-09-13 01:33:57 +0000811 ERROR(mLogCategory,"Error not found render video format:%d to wl dmabuf format",format);
812 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000813 }
814
fei.dengb9a1a572023-09-13 01:33:57 +0000815 TRACE(mLogCategory,"render video format:%d -> dmabuf format:%d",format,dmaformat);
fei.dengf7a0cd32023-08-29 09:36:37 +0000816 *outDmaformat = (uint32_t)dmaformat;
817
818 /*get dmaformat and modifiers*/
819 auto item = mDmaBufferFormats.find(dmaformat);
820 if (item == mDmaBufferFormats.end()) { //not found
fei.dengb9a1a572023-09-13 01:33:57 +0000821 WARNING(mLogCategory,"Not found dmabuf for render video format :%d",format);
fei.dengf7a0cd32023-08-29 09:36:37 +0000822 *outDmaformatModifiers = 0;
fei.dengb9a1a572023-09-13 01:33:57 +0000823 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000824 }
825
826 *outDmaformatModifiers = (uint64_t)item->second;
827
fei.dengb9a1a572023-09-13 01:33:57 +0000828 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000829}
830
831int WaylandDisplay::toShmBufferFormat(RenderVideoFormat format, uint32_t *outformat)
832{
833 if (!outformat) {
fei.dengb9a1a572023-09-13 01:33:57 +0000834 WARNING(mLogCategory,"NULL params");
835 return ERROR_PARAM_NULL;
fei.dengf7a0cd32023-08-29 09:36:37 +0000836 }
837
838 *outformat = 0;
839
840 int shmformat = (int)video_format_to_wl_shm_format(format);
841 if (shmformat < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000842 ERROR(mLogCategory,"Error not found render video format:%d to wl shmbuf format",format);
843 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000844 }
845
846 for (auto item = mShmFormats.begin(); item != mShmFormats.end(); ++item) {
847 uint32_t registFormat = (uint32_t)*item;
848 if (registFormat == (uint32_t)shmformat) {
849 *outformat = registFormat;
fei.dengb9a1a572023-09-13 01:33:57 +0000850 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +0000851 }
852 }
853
fei.dengb9a1a572023-09-13 01:33:57 +0000854 return ERROR_NOT_FOUND;
fei.dengf7a0cd32023-08-29 09:36:37 +0000855}
856
857void WaylandDisplay::setVideoBufferFormat(RenderVideoFormat format)
858{
fei.dengb9a1a572023-09-13 01:33:57 +0000859 TRACE(mLogCategory,"set video buffer format: %d",format);
fei.dengf7a0cd32023-08-29 09:36:37 +0000860 mBufferFormat = format;
861};
862
863void WaylandDisplay::setDisplayOutput(int output)
864{
fei.dengb9a1a572023-09-13 01:33:57 +0000865 TRACE(mLogCategory,"select display output: %d",output);
fei.dengf7a0cd32023-08-29 09:36:37 +0000866 if (output < 0 || output >= DEFAULT_DISPLAY_OUTPUT_NUM) {
fei.dengb9a1a572023-09-13 01:33:57 +0000867 ERROR(mLogCategory, "display output index error,please set 0:primary or 1:extend,now:%d",output);
fei.dengf7a0cd32023-08-29 09:36:37 +0000868 return;
869 }
fei.deng26950832023-11-09 03:00:23 +0000870 //only do select output before video playing
871 if (mSelectOutputIndex != output) {
872 mSelectOutputIndex = output;
873 // if (mOutput[output].wlOutput) {
874 // mCurrentDisplayOutput = &mOutput[output];
875 // setRenderRectangle(mOutput[output].offsetX, mOutput[output].offsetY,
876 // mOutput[output].width, mOutput[output].height);
877 // }
fei.dengf7a0cd32023-08-29 09:36:37 +0000878 }
879}
880
881int WaylandDisplay::getDisplayOutput()
882{
fei.deng26950832023-11-09 03:00:23 +0000883 return mSelectOutputIndex == INVALID_OUTPUT_INDEX? 0: mSelectOutputIndex;
fei.dengf7a0cd32023-08-29 09:36:37 +0000884}
885
886void WaylandDisplay::setPip(int pip)
887{
fei.dengb9a1a572023-09-13 01:33:57 +0000888 INFO(mLogCategory,"set pip:%d",pip);
fei.dengf7a0cd32023-08-29 09:36:37 +0000889 mPip = pip;
890}
891
fei.deng26950832023-11-09 03:00:23 +0000892void WaylandDisplay::updateDisplayOutput()
893{
894 if (!mCurrentDisplayOutput->wlOutput || !mXdgToplevel || !mXdgSurface)
895 {
896 return;
897 }
898 if (mUpdateRenderRectangle) {
899 if (mFullScreen) {
900 DEBUG(mLogCategory,"unset full screen");
901 xdg_toplevel_unset_fullscreen (mXdgToplevel);
902 }
903
904 if (mXdgSurface) {
905 DEBUG(mLogCategory,"set geometry");
906 xdg_surface_set_window_geometry(mXdgSurface,
907 mCurrentDisplayOutput->offsetX,
908 mCurrentDisplayOutput->offsetY,
909 mCurrentDisplayOutput->width,
910 mCurrentDisplayOutput->height);
911 }
912
913 if (mFullScreen && mXdgToplevel) {
914 DEBUG(mLogCategory,"set full screen");
915 xdg_toplevel_set_fullscreen (mXdgToplevel, mCurrentDisplayOutput->wlOutput);
916 }
917 setRenderRectangle(mCurrentDisplayOutput->offsetX, mCurrentDisplayOutput->offsetY,
918 mCurrentDisplayOutput->width, mCurrentDisplayOutput->height);
919 mUpdateRenderRectangle = false;
920 }
921}
922
fei.dengf7a0cd32023-08-29 09:36:37 +0000923void WaylandDisplay::createCommonWindowSurface()
924{
925 struct wl_region *region;
926
927 mAreaSurface = wl_compositor_create_surface (mCompositor);
928 mVideoSurface = wl_compositor_create_surface (mCompositor);
929 mAreaSurfaceWrapper = (struct wl_surface *)wl_proxy_create_wrapper (mAreaSurface);
930 mVideoSurfaceWrapper = (struct wl_surface *)wl_proxy_create_wrapper (mVideoSurface);
931
932 wl_proxy_set_queue ((struct wl_proxy *) mAreaSurfaceWrapper, mWlQueue);
933 wl_proxy_set_queue ((struct wl_proxy *) mVideoSurfaceWrapper, mWlQueue);
934
935 /* embed video_surface in area_surface */
936 mVideoSubSurface = wl_subcompositor_get_subsurface (mSubCompositor, mVideoSurface, mAreaSurface);
937 wl_subsurface_set_desync (mVideoSubSurface);
938
939 if (mViewporter) {
940 mAreaViewport = wp_viewporter_get_viewport (mViewporter, mAreaSurface);
941 mVideoViewport = wp_viewporter_get_viewport (mViewporter, mVideoSurface);
942 }
943
944 /* do not accept input */
945 region = wl_compositor_create_region (mCompositor);
946 wl_surface_set_input_region (mAreaSurface, region);
947 wl_region_destroy (region);
948
949 region = wl_compositor_create_region (mCompositor);
950 wl_surface_set_input_region (mVideoSurface, region);
951 wl_region_destroy (region);
952}
953
954void WaylandDisplay::createXdgShellWindowSurface()
955{
956 /* Check which protocol we will use (in order of preference) */
957 if (mXdgWmBase) {
958 /* First create the XDG surface */
959 mXdgSurface= xdg_wm_base_get_xdg_surface (mXdgWmBase, mAreaSurface);
960 if (!mXdgSurface) {
fei.dengb9a1a572023-09-13 01:33:57 +0000961 ERROR(mLogCategory,"Unable to get xdg_surface");
fei.dengf7a0cd32023-08-29 09:36:37 +0000962 return;
963 }
964 xdg_surface_add_listener (mXdgSurface, &xdg_surface_listener,(void *)this);
965
966 /* Then the toplevel */
967 mXdgToplevel= xdg_surface_get_toplevel (mXdgSurface);
968 if (!mXdgSurface) {
fei.dengb9a1a572023-09-13 01:33:57 +0000969 ERROR(mLogCategory,"Unable to get xdg_toplevel");
fei.dengf7a0cd32023-08-29 09:36:37 +0000970 return;
971 }
972 xdg_toplevel_add_listener (mXdgToplevel, &xdg_toplevel_listener, this);
973
974 /* Finally, commit the xdg_surface state as toplevel */
975 mXdgSurfaceConfigured = false;
976 wl_surface_commit (mAreaSurface);
977 wl_display_flush (mWlDisplay);
978 /* we need exactly 3 roundtrips to discover global objects and their state */
979 for (int i = 0; i < 3; i++) {
980 if (wl_display_roundtrip_queue(mWlDisplay, mWlQueue) < 0) {
fei.dengb9a1a572023-09-13 01:33:57 +0000981 ERROR(mLogCategory,"Error communicating with the wayland display");
fei.dengf7a0cd32023-08-29 09:36:37 +0000982 }
983 }
984
985 if (mXdgSurfaceConfigured) {
fei.dengb9a1a572023-09-13 01:33:57 +0000986 INFO(mLogCategory,"xdg surface had configured");
fei.dengf7a0cd32023-08-29 09:36:37 +0000987 } else {
fei.dengb9a1a572023-09-13 01:33:57 +0000988 WARNING(mLogCategory,"xdg surface not configured");
fei.dengf7a0cd32023-08-29 09:36:37 +0000989 }
990
991 //full screen show
fei.deng26950832023-11-09 03:00:23 +0000992 // if (mFullScreen && mCurrentDisplayOutput->wlOutput) {
993 // //ensureFullscreen(mFullScreen);
fei.dengf7a0cd32023-08-29 09:36:37 +0000994 // }
995 } else {
fei.dengb9a1a572023-09-13 01:33:57 +0000996 ERROR(mLogCategory,"Unable to use xdg_wm_base ");
fei.dengf7a0cd32023-08-29 09:36:37 +0000997 return;
998 }
999}
1000
1001void WaylandDisplay::destroyWindowSurfaces()
1002{
1003 if (mAreaShmBuffer) {
1004 delete mAreaShmBuffer;
1005 mAreaShmBuffer = NULL;
1006 }
1007
1008 //clean all wayland buffers
1009 cleanAllWaylandBuffer();
1010
1011 if (mXdgToplevel) {
1012 xdg_toplevel_destroy (mXdgToplevel);
1013 mXdgToplevel = NULL;
1014 }
1015
1016 if (mXdgSurface) {
1017 xdg_surface_destroy (mXdgSurface);
1018 mXdgSurface = NULL;
1019 }
1020
1021 if (mVideoSurfaceWrapper) {
1022 wl_proxy_wrapper_destroy (mVideoSurfaceWrapper);
1023 mVideoSurfaceWrapper = NULL;
1024 }
1025
1026 if (mVideoSubSurface) {
1027 wl_subsurface_destroy (mVideoSubSurface);
1028 mVideoSubSurface = NULL;
1029 }
1030
1031 if (mVideoSurface) {
1032 wl_surface_destroy (mVideoSurface);
1033 mVideoSurface = NULL;
1034 }
1035
1036 if (mAreaSurfaceWrapper) {
1037 wl_proxy_wrapper_destroy (mAreaSurfaceWrapper);
1038 mAreaSurfaceWrapper = NULL;
1039 }
1040
1041 if (mAreaSurface) {
1042 wl_surface_destroy (mAreaSurface);
1043 mAreaSurface = NULL;
1044 mReCommitAreaSurface = false;
1045 }
1046}
1047
1048void WaylandDisplay::ensureFullscreen(bool fullscreen)
1049{
1050 if (mXdgWmBase) {
fei.dengb9a1a572023-09-13 01:33:57 +00001051 DEBUG(mLogCategory,"full screen : %d",fullscreen);
fei.dengf7a0cd32023-08-29 09:36:37 +00001052 if (fullscreen) {
fei.deng26950832023-11-09 03:00:23 +00001053 xdg_toplevel_set_fullscreen (mXdgToplevel, mCurrentDisplayOutput->wlOutput);
fei.dengf7a0cd32023-08-29 09:36:37 +00001054 } else {
1055 xdg_toplevel_unset_fullscreen (mXdgToplevel);
1056 }
1057 }
1058}
1059
1060void WaylandDisplay::setRenderRectangle(int x, int y, int w, int h)
1061{
fei.dengb9a1a572023-09-13 01:33:57 +00001062 DEBUG(mLogCategory,"set render rect:x:%d,y:%d,w:%d,h:%d",x,y,w,h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001063
1064 if (w <= 0 || h <= 0) {
fei.dengb9a1a572023-09-13 01:33:57 +00001065 WARNING(mLogCategory, "wrong render width or height %dx%d",w,h);
fei.dengf7a0cd32023-08-29 09:36:37 +00001066 return;
1067 }
1068
1069 mRenderRect.x = x;
1070 mRenderRect.y = y;
1071 mRenderRect.w = w;
1072 mRenderRect.h = h;
1073
1074 if (!mXdgSurfaceConfigured) {
fei.dengb9a1a572023-09-13 01:33:57 +00001075 WARNING(mLogCategory,"Not configured xdg");
fei.dengf7a0cd32023-08-29 09:36:37 +00001076 return;
1077 }
1078
1079 if (mAreaViewport) {
1080 wp_viewport_set_destination (mAreaViewport, w, h);
1081 }
1082
1083 updateBorders();
1084
1085 if (mVideoWidth != 0 && mVideoSurface) {
1086 wl_subsurface_set_sync (mVideoSubSurface);
1087 resizeVideoSurface(true);
1088 }
1089
1090 wl_surface_damage (mAreaSurfaceWrapper, 0, 0, w, h);
1091 wl_surface_commit (mAreaSurfaceWrapper);
1092
1093 if (mVideoWidth != 0) {
1094 wl_subsurface_set_desync (mVideoSubSurface);
1095 }
1096}
1097
1098
1099void WaylandDisplay::setFrameSize(int w, int h)
1100{
1101 mVideoWidth = w;
1102 mVideoHeight = h;
fei.dengb9a1a572023-09-13 01:33:57 +00001103 TRACE(mLogCategory,"frame w:%d,h:%d",mVideoWidth,mVideoHeight);
fei.dengf7a0cd32023-08-29 09:36:37 +00001104 if (mRenderRect.w > 0 && mVideoSurface) {
1105 resizeVideoSurface(true);
1106 }
1107}
1108
1109void WaylandDisplay::setWindowSize(int x, int y, int w, int h)
1110{
1111 mWindowRect.x = x;
1112 mWindowRect.y = y;
1113 mWindowRect.w = w;
1114 mWindowRect.h = h;
fei.dengb9a1a572023-09-13 01:33:57 +00001115 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 +00001116 if (mWindowRect.w > 0 && mVideoWidth > 0 && mVideoSurface) {
1117 //if had full screen, unset it and set window size
1118 if (mFullScreen) {
1119 mFullScreen = false;
1120 ensureFullscreen(mFullScreen);
1121 }
1122 resizeVideoSurface(true);
1123 }
1124}
1125
1126
1127void WaylandDisplay::resizeVideoSurface(bool commit)
1128{
1129 Rectangle src = {0,};
1130 Rectangle dst = {0,};
1131 Rectangle res;
1132
1133 /* center the video_subsurface inside area_subsurface */
1134 src.w = mVideoWidth;
1135 src.h = mVideoHeight;
1136 /*if had set the window size, we will scall
1137 video surface to this window size*/
1138 if (mWindowRect.w > 0 && mWindowRect.h > 0) {
1139 dst.x = mWindowRect.x;
1140 dst.y = mWindowRect.y;
1141 dst.w = mWindowRect.w;
1142 dst.h = mWindowRect.h;
1143 if (mWindowRect.w > mRenderRect.w && mWindowRect.h > mRenderRect.h) {
fei.dengb9a1a572023-09-13 01:33:57 +00001144 WARNING(mLogCategory,"Error window size:%dx%d, but render size:%dx%d,reset to render size",
fei.dengf7a0cd32023-08-29 09:36:37 +00001145 mWindowRect.w,mWindowRect.h,mRenderRect.w,mRenderRect.h);
1146 dst.x = mRenderRect.x;
1147 dst.y = mRenderRect.y;
1148 dst.w = mRenderRect.w;
1149 dst.h = mRenderRect.h;
1150 }
1151 //to do,we need set geometry?
1152 //if (mXdgSurface) {
1153 // xdg_surface_set_window_geometry(mXdgSurface, mWindowRect.x, mWindowRect.y, mWindowRect.w, mWindowRect.h);
1154 //}
1155 } else { //scal video to full screen
1156 dst.w = mRenderRect.w;
1157 dst.h = mRenderRect.h;
1158 }
1159
1160 if (mViewporter) {
1161 videoCenterRect(src, dst, &res, true);
1162 } else {
1163 videoCenterRect(src, dst, &res, false);
1164 }
1165
1166 wl_subsurface_set_position (mVideoSubSurface, res.x, res.y);
1167
1168 if (commit) {
1169 wl_surface_damage (mVideoSurfaceWrapper, 0, 0, res.w, res.h);
1170 wl_surface_commit (mVideoSurfaceWrapper);
1171 }
1172
1173 //top level setting
1174 if (mXdgToplevel) {
1175 struct wl_region *region;
1176
1177 region = wl_compositor_create_region (mCompositor);
1178 wl_region_add (region, 0, 0, mRenderRect.w, mRenderRect.h);
1179 wl_surface_set_input_region (mAreaSurface, region);
1180 wl_region_destroy (region);
1181 }
1182
1183 /* this is saved for use in wl_surface_damage */
1184 mVideoRect.x = res.x;
1185 mVideoRect.y = res.y;
1186 mVideoRect.w = res.w;
1187 mVideoRect.h = res.h;
1188
1189 //to scale video surface to full screen
1190 wp_viewport_set_destination(mVideoViewport, res.w, res.h);
fei.dengb9a1a572023-09-13 01:33:57 +00001191 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 +00001192}
1193
1194void WaylandDisplay::setOpaque()
1195{
1196 struct wl_region *region;
1197
1198 /* Set area opaque */
1199 region = wl_compositor_create_region (mCompositor);
1200 wl_region_add (region, 0, 0, mRenderRect.w, mRenderRect.h);
1201 wl_surface_set_opaque_region (mAreaSurface, region);
1202 wl_region_destroy (region);
1203}
1204
1205int WaylandDisplay::prepareFrameBuffer(RenderBuffer * buf)
1206{
1207 WaylandBuffer *waylandBuf = NULL;
1208 int ret;
1209
1210 waylandBuf = findWaylandBuffer(buf);
1211 if (waylandBuf == NULL) {
fei.dengb9a1a572023-09-13 01:33:57 +00001212 waylandBuf = new WaylandBuffer(this, mLogCategory);
fei.dengf7a0cd32023-08-29 09:36:37 +00001213 waylandBuf->setBufferFormat(mBufferFormat);
1214 ret = waylandBuf->constructWlBuffer(buf);
fei.dengb9a1a572023-09-13 01:33:57 +00001215 if (ret != NO_ERROR) {
1216 WARNING(mLogCategory,"dmabufConstructWlBuffer fail,release waylandbuf");
fei.dengf7a0cd32023-08-29 09:36:37 +00001217 //delete waylanBuf,WaylandBuffer object destruct will call release callback
1218 goto waylandbuf_fail;
1219 } else {
1220 addWaylandBuffer(buf, waylandBuf);
1221 }
1222 }
fei.dengb9a1a572023-09-13 01:33:57 +00001223 return NO_ERROR;
fei.dengf7a0cd32023-08-29 09:36:37 +00001224waylandbuf_fail:
1225 //delete waylandbuf
1226 delete waylandBuf;
1227 waylandBuf = NULL;
fei.dengb9a1a572023-09-13 01:33:57 +00001228 return ERROR_UNKNOWN;
fei.dengf7a0cd32023-08-29 09:36:37 +00001229}
1230
1231void WaylandDisplay::displayFrameBuffer(RenderBuffer * buf, int64_t realDisplayTime)
1232{
1233 WaylandBuffer *waylandBuf = NULL;
1234 struct wl_buffer * wlbuffer = NULL;
1235 int ret;
1236
1237 if (!buf) {
fei.dengb9a1a572023-09-13 01:33:57 +00001238 ERROR(mLogCategory,"Error input params, waylandbuffer is null");
fei.dengf7a0cd32023-08-29 09:36:37 +00001239 return;
1240 }
1241
1242 //must commit areasurface first, because weston xdg surface maybe timeout
1243 //this cause video is not display,commit can resume xdg surface
1244 if (!mReCommitAreaSurface) {
1245 mReCommitAreaSurface = true;
1246 wl_surface_commit (mAreaSurface);
1247 }
1248
fei.dengb9a1a572023-09-13 01:33:57 +00001249 TRACE(mLogCategory,"display renderBuffer:%p,PTS:%lld,realtime:%lld",buf, buf->pts, realDisplayTime);
fei.dengf7a0cd32023-08-29 09:36:37 +00001250
1251 if (buf->flag & BUFFER_FLAG_DMA_BUFFER) {
1252 if (buf->dma.width <=0 || buf->dma.height <=0) {
1253 buf->dma.width = mVideoWidth;
1254 buf->dma.height = mVideoHeight;
1255 }
1256 waylandBuf = findWaylandBuffer(buf);
1257 if (waylandBuf) {
1258 waylandBuf->setRenderRealTime(realDisplayTime);
1259 ret = waylandBuf->constructWlBuffer(buf);
fei.dengb9a1a572023-09-13 01:33:57 +00001260 if (ret != NO_ERROR) {
1261 WARNING(mLogCategory,"dmabufConstructWlBuffer fail,release waylandbuf");
fei.dengf7a0cd32023-08-29 09:36:37 +00001262 //delete waylanBuf,WaylandBuffer object destruct will call release callback
1263 goto waylandbuf_fail;
1264 }
1265 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001266 ERROR(mLogCategory,"NOT found wayland buffer,please prepare buffer first");
fei.dengf7a0cd32023-08-29 09:36:37 +00001267 goto waylandbuf_fail;
1268 }
1269 }
1270
1271 if (waylandBuf) {
1272 wlbuffer = waylandBuf->getWlBuffer();
1273 }
fei.deng1cfb2752023-10-26 08:01:25 +00001274 //if no wl_output, drop this buffer
fei.deng26950832023-11-09 03:00:23 +00001275 if (mCurrentDisplayOutput->wlOutput == NULL) {
fei.deng1cfb2752023-10-26 08:01:25 +00001276 TRACE(mLogCategory,"No wl_output");
1277 mWaylandPlugin->handleFrameDropped(buf);
1278 mWaylandPlugin->handleBufferRelease(buf);
1279 return;
1280 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001281 if (wlbuffer) {
fei.dengb9a1a572023-09-13 01:33:57 +00001282 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001283 ++mCommitCnt;
1284 uint32_t hiPts = realDisplayTime >> 32;
1285 uint32_t lowPts = realDisplayTime & 0xFFFFFFFF;
1286 //attach this wl_buffer to weston
fei.dengb9a1a572023-09-13 01:33:57 +00001287 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 +00001288 waylandBuf->attach(mVideoSurfaceWrapper);
1289
1290 if (mIsSendPtsToWeston) {
fei.dengb9a1a572023-09-13 01:33:57 +00001291 TRACE(mLogCategory,"display time:%lld,hiPts:%u,lowPts:%u",realDisplayTime, hiPts, lowPts);
fei.dengf7a0cd32023-08-29 09:36:37 +00001292 wl_surface_set_pts(mVideoSurfaceWrapper, hiPts, lowPts);
1293 }
1294
1295 wl_surface_damage (mVideoSurfaceWrapper, 0, 0, mVideoRect.w, mVideoRect.h);
1296 wl_surface_commit (mVideoSurfaceWrapper);
1297 //insert this buffer to committed weston buffer manager
1298 std::pair<int64_t, WaylandBuffer *> item(buf->pts, waylandBuf);
1299 mCommittedBufferMap.insert(item);
1300 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001301 WARNING(mLogCategory,"wlbuffer is NULL");
fei.dengf7a0cd32023-08-29 09:36:37 +00001302 /* clear both video and parent surfaces */
1303 cleanSurface();
1304 }
1305
1306 wl_display_flush (mWlDisplay);
1307
1308 return;
1309waylandbuf_fail:
1310 //notify dropped
1311 mWaylandPlugin->handleFrameDropped(buf);
1312 //notify app release this buf
1313 mWaylandPlugin->handleBufferRelease(buf);
1314 //delete waylandbuf
1315 delete waylandBuf;
1316 waylandBuf = NULL;
1317 return;
1318}
1319
1320void WaylandDisplay::handleBufferReleaseCallback(WaylandBuffer *buf)
1321{
1322 {
fei.dengb9a1a572023-09-13 01:33:57 +00001323 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001324 --mCommitCnt;
1325 //remove buffer if this buffer is ready to release
1326 RenderBuffer *renderBuffer = buf->getRenderBuffer();
1327 auto item = mCommittedBufferMap.find(renderBuffer->pts);
1328 if (item != mCommittedBufferMap.end()) {
1329 mCommittedBufferMap.erase(item);
1330 } else {
fei.dengb9a1a572023-09-13 01:33:57 +00001331 WARNING(mLogCategory,"Can't find WaylandBuffer in buffer map");
fei.dengf7a0cd32023-08-29 09:36:37 +00001332 return;
1333 }
1334 }
1335 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengb9a1a572023-09-13 01:33:57 +00001336 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 +00001337 mWaylandPlugin->handleBufferRelease(renderBuffer);
1338}
1339
1340void WaylandDisplay::handleFrameDisplayedCallback(WaylandBuffer *buf)
1341{
1342 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengb9a1a572023-09-13 01:33:57 +00001343 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 +00001344 mWaylandPlugin->handleFrameDisplayed(renderBuffer);
1345}
1346
1347void WaylandDisplay::handleFrameDropedCallback(WaylandBuffer *buf)
1348{
1349 RenderBuffer *renderBuffer = buf->getRenderBuffer();
fei.dengb9a1a572023-09-13 01:33:57 +00001350 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 +00001351 mWaylandPlugin->handleFrameDropped(renderBuffer);
1352}
1353
1354
1355void WaylandDisplay::readyToRun()
1356{
1357 mFd = wl_display_get_fd (mWlDisplay);
fei.dengb9a1a572023-09-13 01:33:57 +00001358 if (mPoll) {
1359 mPoll->addFd(mFd);
1360 mPoll->setFdReadable(mFd, true);
1361 }
fei.dengf7a0cd32023-08-29 09:36:37 +00001362}
1363
1364bool WaylandDisplay::threadLoop()
1365{
1366 int ret;
fei.dengf7a0cd32023-08-29 09:36:37 +00001367
1368 while (wl_display_prepare_read_queue (mWlDisplay, mWlQueue) != 0) {
1369 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1370 }
1371
1372 wl_display_flush (mWlDisplay);
1373
1374 /*poll timeout value must > 300 ms,otherwise zwp_linux_dmabuf will create failed,
1375 so do use -1 to wait for ever*/
fei.dengb9a1a572023-09-13 01:33:57 +00001376 ret = mPoll->wait(-1); //wait for ever
fei.dengf7a0cd32023-08-29 09:36:37 +00001377 if (ret < 0) { //poll error
fei.dengb9a1a572023-09-13 01:33:57 +00001378 WARNING(mLogCategory,"poll error");
fei.dengf7a0cd32023-08-29 09:36:37 +00001379 wl_display_cancel_read(mWlDisplay);
1380 return false;
1381 } else if (ret == 0) { //poll time out
1382 return true; //run loop
1383 }
1384
1385 if (wl_display_read_events (mWlDisplay) == -1) {
1386 goto tag_error;
1387 }
1388
1389 wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue);
1390 return true;
1391tag_error:
fei.dengb9a1a572023-09-13 01:33:57 +00001392 ERROR(mLogCategory,"Error communicating with the wayland server");
fei.dengf7a0cd32023-08-29 09:36:37 +00001393 return false;
1394}
1395
1396void WaylandDisplay::videoCenterRect(Rectangle src, Rectangle dst, Rectangle *result, bool scaling)
1397{
1398 //if dst is a small window, we scale video to map window size,don't doing center
1399 if (mRenderRect.w != dst.w && mRenderRect.h != dst.h) {
1400 result->x = dst.x;
1401 result->y = dst.y;
1402 result->w = dst.w;
1403 result->h = dst.h;
fei.dengb9a1a572023-09-13 01:33:57 +00001404 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 +00001405 src.w, src.h, dst.w, dst.h, result->w, result->h, result->x, result->y);
1406 return;
1407 }
1408 if (!scaling) {
1409 result->w = MIN (src.w, dst.w);
1410 result->h = MIN (src.h, dst.h);
1411 result->x = dst.x + (dst.w - result->w) / 2;
1412 result->y = dst.y + (dst.h - result->h) / 2;
1413 } else {
1414 double src_ratio, dst_ratio;
1415
1416 src_ratio = (double) src.w / src.h;
1417 dst_ratio = (double) dst.w / dst.h;
1418
1419 if (src_ratio > dst_ratio) {
1420 result->w = dst.w;
1421 result->h = dst.w / src_ratio;
1422 result->x = dst.x;
1423 result->y = dst.y + (dst.h - result->h) / 2;
1424 } else if (src_ratio < dst_ratio) {
1425 result->w = dst.h * src_ratio;
1426 result->h = dst.h;
1427 result->x = dst.x + (dst.w - result->w) / 2;
1428 result->y = dst.y;
1429 } else {
1430 result->x = dst.x;
1431 result->y = dst.y;
1432 result->w = dst.w;
1433 result->h = dst.h;
1434 }
1435 }
1436
fei.dengb9a1a572023-09-13 01:33:57 +00001437 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 +00001438 src.w, src.h, dst.w, dst.h, result->w, result->h, result->x, result->y);
1439}
1440
1441void WaylandDisplay::updateBorders()
1442{
1443 int width,height;
1444
1445 if (mNoBorderUpdate)
1446 return;
1447
1448 if (mViewporter) {
1449 width = height = 1;
1450 mNoBorderUpdate = true;
1451 } else {
1452 width = mRenderRect.w;
1453 height = mRenderRect.h;
1454 }
1455
1456 RenderVideoFormat format = VIDEO_FORMAT_BGRA;
fei.dengb9a1a572023-09-13 01:33:57 +00001457 mAreaShmBuffer = new WaylandShmBuffer(this, mLogCategory);
fei.dengf7a0cd32023-08-29 09:36:37 +00001458 struct wl_buffer *wlbuf = mAreaShmBuffer->constructWlBuffer(width, height, format);
1459 if (wlbuf == NULL) {
1460 delete mAreaShmBuffer;
1461 mAreaShmBuffer = NULL;
1462 }
1463
1464 wl_surface_attach(mAreaSurfaceWrapper, wlbuf, 0, 0);
1465}
1466
1467std::size_t WaylandDisplay::calculateDmaBufferHash(RenderDmaBuffer &dmabuf)
1468{
1469 std::string hashString("");
1470 for (int i = 0; i < dmabuf.planeCnt; i++) {
1471 char hashtmp[1024];
1472 snprintf (hashtmp, 1024, "%d%d%d%d%d%d", dmabuf.width,dmabuf.height,dmabuf.planeCnt,
1473 dmabuf.stride[i],dmabuf.offset[i],dmabuf.fd[i]);
1474 std::string tmp(hashtmp);
1475 hashString += tmp;
1476 }
1477
1478 std::size_t hashval = std::hash<std::string>()(hashString);
fei.dengb9a1a572023-09-13 01:33:57 +00001479 //TRACE(mLogCategory,"hashstr:%s,val:%zu",hashString.c_str(),hashval);
fei.dengf7a0cd32023-08-29 09:36:37 +00001480 return hashval;
1481}
1482
1483void WaylandDisplay::addWaylandBuffer(RenderBuffer * buf, WaylandBuffer *waylandbuf)
1484{
1485 if (buf->flag & BUFFER_FLAG_DMA_BUFFER) {
1486 std::size_t hashval = calculateDmaBufferHash(buf->dma);
1487 std::pair<std::size_t, WaylandBuffer *> item(hashval, waylandbuf);
fei.dengb9a1a572023-09-13 01:33:57 +00001488 //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 +00001489 mWaylandBuffersMap.insert(item);
1490 }
1491
1492 //clean invalid wayland buffer,if video resolution changed
1493 for (auto item = mWaylandBuffersMap.begin(); item != mWaylandBuffersMap.end(); ) {
1494 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1495 if ((waylandbuf->getFrameWidth() != buf->dma.width ||
1496 waylandbuf->getFrameHeight() != buf->dma.height) &&
1497 waylandbuf->isFree()) {
fei.dengb9a1a572023-09-13 01:33:57 +00001498 TRACE(mLogCategory,"delete wayland buffer,width:%d(%d),height:%d(%d)",
fei.dengf7a0cd32023-08-29 09:36:37 +00001499 waylandbuf->getFrameWidth(),buf->dma.width,
1500 waylandbuf->getFrameHeight(),buf->dma.height);
1501 mWaylandBuffersMap.erase(item++);
1502 delete waylandbuf;
1503 } else {
1504 item++;
1505 }
1506 }
fei.dengb9a1a572023-09-13 01:33:57 +00001507 TRACE(mLogCategory,"mWaylandBuffersMap size:%d",mWaylandBuffersMap.size());
fei.dengf7a0cd32023-08-29 09:36:37 +00001508}
1509
1510WaylandBuffer* WaylandDisplay::findWaylandBuffer(RenderBuffer * buf)
1511{
1512 std::size_t hashval = calculateDmaBufferHash(buf->dma);
1513 auto item = mWaylandBuffersMap.find(hashval);
1514 if (item == mWaylandBuffersMap.end()) {
1515 return NULL;
1516 }
1517
1518 return (WaylandBuffer*) item->second;
1519}
1520
1521void WaylandDisplay::cleanAllWaylandBuffer()
1522{
1523 //free all obtain buff
1524 for (auto item = mWaylandBuffersMap.begin(); item != mWaylandBuffersMap.end(); ) {
1525 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1526 mWaylandBuffersMap.erase(item++);
1527 delete waylandbuf;
1528 }
1529}
1530
1531void WaylandDisplay::flushBuffers()
1532{
fei.dengb9a1a572023-09-13 01:33:57 +00001533 INFO(mLogCategory,"flushBuffers");
1534 Tls::Mutex::Autolock _l(mRenderMutex);
fei.dengf7a0cd32023-08-29 09:36:37 +00001535 for (auto item = mCommittedBufferMap.begin(); item != mCommittedBufferMap.end(); item++) {
1536 WaylandBuffer *waylandbuf = (WaylandBuffer*)item->second;
1537 waylandbuf->forceRedrawing();
1538 handleFrameDisplayedCallback(waylandbuf);
1539 }
1540}
1541
1542void WaylandDisplay::cleanSurface()
1543{
1544 /* clear both video and parent surfaces */
1545 wl_surface_attach (mVideoSurfaceWrapper, NULL, 0, 0);
1546 wl_surface_commit (mVideoSurfaceWrapper);
1547 wl_surface_attach (mAreaSurfaceWrapper, NULL, 0, 0);
1548 wl_surface_commit (mAreaSurfaceWrapper);
1549}