fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1 | /* |
| 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 <cstring> |
| 17 | #include "wstclient_wayland.h" |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 18 | #include "ErrorCode.h" |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 19 | #include "Logger.h" |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 20 | #include "wstclient_plugin.h" |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 21 | |
| 22 | #ifndef MAX |
| 23 | # define MAX(a,b) ((a) > (b)? (a) : (b)) |
| 24 | # define MIN(a,b) ((a) < (b)? (a) : (b)) |
| 25 | #endif |
| 26 | |
| 27 | #define WESTEROS_UNUSED(x) ((void)(x)) |
| 28 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 29 | #define TAG "rlib:wstclient_wayland" |
| 30 | |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 31 | #define DEFAULT_WINDOW_X (0) |
| 32 | #define DEFAULT_WINDOW_Y (0) |
| 33 | #define DEFAULT_WINDOW_WIDTH (1920) |
| 34 | #define DEFAULT_WINDOW_HEIGHT (1080) |
| 35 | //discuss with display team,outpu resolution is a fixed value(1920x1080) |
| 36 | #define DEFAULT_OUTPUT_WIDTH (1920) |
| 37 | #define DEFAULT_OUTPUT_HEIGHT (1080) |
| 38 | |
| 39 | #define AV_SYNC_SESSION_V_MONO 64 |
| 40 | |
| 41 | enum |
| 42 | { |
| 43 | ZOOM_NONE, |
| 44 | ZOOM_DIRECT, |
| 45 | ZOOM_NORMAL, |
| 46 | ZOOM_16_9_STRETCH, |
| 47 | ZOOM_4_3_PILLARBOX, |
| 48 | ZOOM_ZOOM, |
| 49 | ZOOM_GLOBAL |
| 50 | }; |
| 51 | |
| 52 | #define needBounds() (mForceAspectRatio || (mZoomMode != ZOOM_NONE)) |
| 53 | |
| 54 | void WstClientWayland::shellSurfaceId(void *data, |
| 55 | struct wl_simple_shell *wl_simple_shell, |
| 56 | struct wl_surface *surface, |
| 57 | uint32_t surfaceId) |
| 58 | { |
| 59 | WstClientWayland *self = static_cast<WstClientWayland *>(data); |
| 60 | self->mWlShellSurfaceId = surfaceId; |
| 61 | char name[32]; |
| 62 | wl_fixed_t z, op; |
| 63 | WESTEROS_UNUSED(wl_simple_shell); |
| 64 | WESTEROS_UNUSED(surface); |
| 65 | |
| 66 | sprintf( name, "westeros-surface-%x", surfaceId ); |
| 67 | wl_simple_shell_set_name(self->mWlShell, surfaceId, name); |
| 68 | if ( (self->mWindowWidth == 0) || (self->mWindowHeight == 0) ) |
| 69 | { |
| 70 | wl_simple_shell_set_visible(self->mWlShell, self->mWlShellSurfaceId, false); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | wl_simple_shell_set_visible(self->mWlShell, self->mWlShellSurfaceId, true); |
| 75 | if (!self->mWlVpc) |
| 76 | { |
| 77 | wl_simple_shell_set_geometry(self->mWlShell, self->mWlShellSurfaceId, self->mWindowX, self->mWindowY, self->mWindowWidth, self->mWindowHeight ); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | z = wl_fixed_from_double(self->mZorder); |
| 82 | wl_simple_shell_set_zorder(self->mWlShell, self->mWlShellSurfaceId, z); |
| 83 | op = wl_fixed_from_double(self->mOpacity); |
| 84 | wl_simple_shell_set_opacity(self->mWlShell, self->mWlShellSurfaceId, op); |
| 85 | wl_simple_shell_get_status(self->mWlShell, self->mWlShellSurfaceId); |
| 86 | |
| 87 | wl_display_flush(self->mWlDisplay); |
| 88 | } |
| 89 | |
| 90 | void WstClientWayland::shellSurfaceCreated(void *data, |
| 91 | struct wl_simple_shell *wl_simple_shell, |
| 92 | uint32_t surfaceId, |
| 93 | const char *name) |
| 94 | { |
| 95 | WESTEROS_UNUSED(data); |
| 96 | WESTEROS_UNUSED(wl_simple_shell); |
| 97 | WESTEROS_UNUSED(surfaceId); |
| 98 | WESTEROS_UNUSED(name); |
| 99 | } |
| 100 | |
| 101 | void WstClientWayland::shellSurfaceDestroyed(void *data, |
| 102 | struct wl_simple_shell *wl_simple_shell, |
| 103 | uint32_t surfaceId, |
| 104 | const char *name) |
| 105 | { |
| 106 | WESTEROS_UNUSED(data); |
| 107 | WESTEROS_UNUSED(wl_simple_shell); |
| 108 | WESTEROS_UNUSED(surfaceId); |
| 109 | WESTEROS_UNUSED(name); |
| 110 | } |
| 111 | |
| 112 | void WstClientWayland::shellSurfaceStatus(void *data, |
| 113 | struct wl_simple_shell *wl_simple_shell, |
| 114 | uint32_t surfaceId, |
| 115 | const char *name, |
| 116 | uint32_t visible, |
| 117 | int32_t x, |
| 118 | int32_t y, |
| 119 | int32_t width, |
| 120 | int32_t height, |
| 121 | wl_fixed_t opacity, |
| 122 | wl_fixed_t zorder) |
| 123 | { |
| 124 | WstClientWayland *self = static_cast<WstClientWayland *>(data); |
| 125 | WESTEROS_UNUSED(wl_simple_shell); |
| 126 | WESTEROS_UNUSED(surfaceId); |
| 127 | WESTEROS_UNUSED(name); |
| 128 | WESTEROS_UNUSED(x); |
| 129 | WESTEROS_UNUSED(y); |
| 130 | WESTEROS_UNUSED(width); |
| 131 | WESTEROS_UNUSED(height); |
| 132 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 133 | INFO(self->mLogCategory,"opacity: %d,zorder:%d", opacity,zorder); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 134 | self->mWindowChange = true; |
| 135 | self->mOpacity = opacity; |
| 136 | self->mZorder = zorder; |
| 137 | } |
| 138 | |
| 139 | void WstClientWayland::shellGetSurfacesDone(void *data, struct wl_simple_shell *wl_simple_shell) |
| 140 | { |
| 141 | WESTEROS_UNUSED(data); |
| 142 | WESTEROS_UNUSED(wl_simple_shell); |
| 143 | } |
| 144 | |
| 145 | static const struct wl_simple_shell_listener shellListener = |
| 146 | { |
| 147 | WstClientWayland::shellSurfaceId, |
| 148 | WstClientWayland::shellSurfaceCreated, |
| 149 | WstClientWayland::shellSurfaceDestroyed, |
| 150 | WstClientWayland::shellSurfaceStatus, |
| 151 | WstClientWayland::shellGetSurfacesDone |
| 152 | }; |
| 153 | |
| 154 | void WstClientWayland::vpcVideoPathChange(void *data, |
| 155 | struct wl_vpc_surface *wl_vpc_surface, |
| 156 | uint32_t new_pathway ) |
| 157 | { |
| 158 | WESTEROS_UNUSED(wl_vpc_surface); |
| 159 | WstClientWayland *self = static_cast<WstClientWayland *>(data); |
| 160 | |
sheng.liu | a7baed7 | 2024-01-24 07:31:24 +0000 | [diff] [blame] | 161 | INFO(self->mLogCategory,"new pathway: %d", new_pathway); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 162 | self->setVideoPath(new_pathway == WL_VPC_SURFACE_PATHWAY_GRAPHICS); |
| 163 | } |
| 164 | |
| 165 | void WstClientWayland::vpcVideoXformChange(void *data, |
| 166 | struct wl_vpc_surface *wl_vpc_surface, |
| 167 | int32_t x_translation, |
| 168 | int32_t y_translation, |
| 169 | uint32_t x_scale_num, |
| 170 | uint32_t x_scale_denom, |
| 171 | uint32_t y_scale_num, |
| 172 | uint32_t y_scale_denom, |
| 173 | uint32_t output_width, |
| 174 | uint32_t output_height) |
| 175 | { |
| 176 | WESTEROS_UNUSED(wl_vpc_surface); |
| 177 | WstClientWayland *self = static_cast<WstClientWayland *>(data); |
| 178 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 179 | TRACE(self->mLogCategory,"x_trans:%d,y_trans:%d,x_scale_num:%d,x_scale_denom:%d,y_scale_num:%d,y_scale_denom:%d",\ |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 180 | x_translation,y_translation,x_scale_num,x_scale_denom,y_scale_num,y_scale_denom); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 181 | TRACE(self->mLogCategory,"output_width:%d,output_height:%d",output_width,output_height); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 182 | self->mTransX= x_translation; |
| 183 | self->mTransY= y_translation; |
| 184 | if ( x_scale_denom ) |
| 185 | { |
| 186 | self->mScaleXNum= x_scale_num; |
| 187 | self->mScaleXDenom= x_scale_denom; |
| 188 | } |
| 189 | if ( y_scale_denom ) |
| 190 | { |
| 191 | self->mScaleYNum = y_scale_num; |
| 192 | self->mScaleYDenom = y_scale_denom; |
| 193 | } |
| 194 | self->mOutputWidth= (int)output_width; |
| 195 | self->mOutputHeight= (int)output_height; |
| 196 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 197 | Tls::Mutex::Autolock _l(self->mMutex); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 198 | self->updateVideoPosition(); |
| 199 | } |
| 200 | |
| 201 | static const struct wl_vpc_surface_listener vpcListener= { |
| 202 | WstClientWayland::vpcVideoPathChange, |
| 203 | WstClientWayland::vpcVideoXformChange |
| 204 | }; |
| 205 | |
| 206 | void WstClientWayland::outputHandleGeometry( void *data, |
| 207 | struct wl_output *output, |
| 208 | int x, |
| 209 | int y, |
| 210 | int mmWidth, |
| 211 | int mmHeight, |
| 212 | int subPixel, |
| 213 | const char *make, |
| 214 | const char *model, |
| 215 | int transform ) |
| 216 | { |
| 217 | WESTEROS_UNUSED(data); |
| 218 | WESTEROS_UNUSED(output); |
| 219 | WESTEROS_UNUSED(x); |
| 220 | WESTEROS_UNUSED(y); |
| 221 | WESTEROS_UNUSED(mmWidth); |
| 222 | WESTEROS_UNUSED(mmHeight); |
| 223 | WESTEROS_UNUSED(subPixel); |
| 224 | WESTEROS_UNUSED(make); |
| 225 | WESTEROS_UNUSED(model); |
| 226 | WESTEROS_UNUSED(transform); |
| 227 | } |
| 228 | |
| 229 | void WstClientWayland::outputHandleMode( void *data, |
| 230 | struct wl_output *output, |
| 231 | uint32_t flags, |
| 232 | int width, |
| 233 | int height, |
| 234 | int refreshRate ) |
| 235 | { |
| 236 | WstClientWayland *self = static_cast<WstClientWayland *>(data); |
| 237 | |
| 238 | if ( flags & WL_OUTPUT_MODE_CURRENT ) |
| 239 | { |
| 240 | //output resolution is a fixed value(1920x1080) |
| 241 | width = DEFAULT_OUTPUT_WIDTH; |
| 242 | height = DEFAULT_OUTPUT_HEIGHT; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 243 | Tls::Mutex::Autolock _l(self->mMutex); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 244 | self->mDisplayWidth = width; |
| 245 | self->mDisplayHeight = height; |
sheng.liu | a7baed7 | 2024-01-24 07:31:24 +0000 | [diff] [blame] | 246 | DEBUG(self->mLogCategory,"compositor sets window to (%dx%d)", width, height); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 247 | if (!self->mWindowSet) |
| 248 | { |
| 249 | self->mWindowWidth = width; |
| 250 | self->mWindowHeight = height; |
| 251 | if (self->mWlVpcSurface) |
| 252 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 253 | TRACE(self->mLogCategory,"set window geometry:x:%d,y:%d,w:%d,h:%d",self->mWindowX,self->mWindowY,self->mWindowWidth,self->mWindowHeight); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 254 | wl_vpc_surface_set_geometry(self->mWlVpcSurface, self->mWindowX, self->mWindowY, self->mWindowWidth, self->mWindowHeight); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | void WstClientWayland::outputHandleDone( void *data, |
| 261 | struct wl_output *output ) |
| 262 | { |
| 263 | WESTEROS_UNUSED(data); |
| 264 | WESTEROS_UNUSED(output); |
| 265 | } |
| 266 | |
| 267 | void WstClientWayland::outputHandleScale( void *data, |
| 268 | struct wl_output *output, |
| 269 | int32_t scale ) |
| 270 | { |
| 271 | WESTEROS_UNUSED(data); |
| 272 | WESTEROS_UNUSED(output); |
| 273 | WESTEROS_UNUSED(scale); |
| 274 | } |
| 275 | |
| 276 | static const struct wl_output_listener outputListener = { |
| 277 | WstClientWayland::outputHandleGeometry, |
| 278 | WstClientWayland::outputHandleMode, |
| 279 | WstClientWayland::outputHandleDone, |
| 280 | WstClientWayland::outputHandleScale |
| 281 | }; |
| 282 | |
| 283 | void WstClientWayland::sbFormat(void *data, struct wl_sb *wl_sb, uint32_t format) |
| 284 | { |
| 285 | WstClientWayland *self = static_cast<WstClientWayland *>(data); |
| 286 | WESTEROS_UNUSED(wl_sb); |
| 287 | WESTEROS_UNUSED(data); |
sheng.liu | a7baed7 | 2024-01-24 07:31:24 +0000 | [diff] [blame] | 288 | TRACE(self->mLogCategory,"registry: sbFormat: %X", format); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static const struct wl_sb_listener sbListener = { |
| 292 | WstClientWayland::sbFormat |
| 293 | }; |
| 294 | |
| 295 | void |
| 296 | WstClientWayland::registryHandleGlobal (void *data, struct wl_registry *registry, |
| 297 | uint32_t id, const char *interface, uint32_t version) |
| 298 | { |
| 299 | WstClientWayland *self = static_cast<WstClientWayland *>(data); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 300 | TRACE(self->mLogCategory,"registryHandleGlobal,interface:%s,version:%d",interface,version); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 301 | |
| 302 | if (strcmp (interface, "wl_compositor") == 0) { |
| 303 | self->mWlCompositor = (struct wl_compositor *)wl_registry_bind (registry, id, &wl_compositor_interface, 1/*MIN (version, 3)*/); |
| 304 | wl_proxy_set_queue((struct wl_proxy*)self->mWlCompositor, self->mWlQueue); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 305 | TRACE(self->mLogCategory,"registry: compositor %p\n", (void*)self->mWlCompositor); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 306 | } else if (strcmp (interface, "wl_simple_shell") == 0) { |
| 307 | self->mWlShell = (struct wl_simple_shell*)wl_registry_bind(registry, id, &wl_simple_shell_interface, 1); |
| 308 | wl_proxy_set_queue((struct wl_proxy*)self->mWlShell, self->mWlQueue); |
| 309 | wl_simple_shell_add_listener(self->mWlShell, &shellListener, (void *)self); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 310 | TRACE(self->mLogCategory,"registry: simple shell %p\n", (void*)self->mWlShell); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 311 | } else if (strcmp (interface, "wl_vpc") == 0) { |
| 312 | self->mWlVpc = (struct wl_vpc*)wl_registry_bind(registry, id, &wl_vpc_interface, 1); |
| 313 | wl_proxy_set_queue((struct wl_proxy*)self->mWlVpc, self->mWlQueue); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 314 | TRACE(self->mLogCategory,"registry: vpc %p\n", (void*)self->mWlVpc); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 315 | } else if (strcmp (interface, "wl_output") == 0) { |
| 316 | self->mWlOutput = (struct wl_output*)wl_registry_bind(registry, id, &wl_output_interface, 2); |
| 317 | wl_proxy_set_queue((struct wl_proxy*)self->mWlOutput, self->mWlQueue); |
| 318 | wl_output_add_listener(self->mWlOutput, &outputListener, (void *)self); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 319 | TRACE(self->mLogCategory,"registry: output %p\n", (void*)self->mWlOutput); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 320 | } else if (strcmp (interface, "wl_sb") == 0) { |
| 321 | self->mWlSb = (struct wl_sb*)wl_registry_bind(registry, id, &wl_sb_interface, version); |
| 322 | wl_proxy_set_queue((struct wl_proxy*)self->mWlSb, self->mWlQueue); |
| 323 | wl_sb_add_listener(self->mWlSb, &sbListener, (void *)self); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 324 | TRACE(self->mLogCategory,"registry: sb %p\n", (void*)self->mWlSb); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
| 328 | void |
| 329 | WstClientWayland::registryHandleGlobalRemove (void *data, struct wl_registry *registry, uint32_t name) |
| 330 | { |
| 331 | WstClientWayland *self = static_cast<WstClientWayland *>(data); |
| 332 | /* temporarily do nothing */ |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 333 | DEBUG(self->mLogCategory,"wayland display remove registry handle global"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static const struct wl_registry_listener registry_listener = { |
| 337 | WstClientWayland::registryHandleGlobal, |
| 338 | WstClientWayland::registryHandleGlobalRemove |
| 339 | }; |
| 340 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 341 | WstClientWayland::WstClientWayland(WstClientPlugin *plugin, int logCategory) |
| 342 | :mMutex("bufferMutex"), |
| 343 | mPlugin(plugin), |
| 344 | mLogCategory(logCategory) |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 345 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 346 | TRACE(mLogCategory,"construct WstClientWayland"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 347 | mWlDisplay = NULL; |
| 348 | mWlQueue = NULL; |
| 349 | mWlRegistry = NULL; |
| 350 | mWlCompositor = NULL; |
| 351 | mWlSurface = NULL; |
| 352 | mWlVpc = NULL; |
| 353 | mWlVpcSurface = NULL; |
| 354 | mWlOutput = NULL; |
| 355 | mWlShell = NULL; |
| 356 | mWlSb = NULL; |
| 357 | mDisplayWidth = DEFAULT_OUTPUT_WIDTH; |
| 358 | mDisplayHeight = DEFAULT_OUTPUT_HEIGHT; |
| 359 | mWindowX = DEFAULT_WINDOW_X; |
| 360 | mWindowY = DEFAULT_WINDOW_Y; |
| 361 | mWindowWidth = DEFAULT_WINDOW_WIDTH; |
| 362 | mWindowHeight = DEFAULT_WINDOW_HEIGHT; |
| 363 | mVideoX = mWindowX; |
| 364 | mVideoY = mWindowY; |
| 365 | mVideoWidth = mWindowWidth; |
| 366 | mVideoHeight = mWindowHeight; |
| 367 | mWlShellSurfaceId = 0; |
| 368 | mWindowChange = false; |
| 369 | mWindowSet = false; |
| 370 | mWindowSizeOverride = false; |
| 371 | mZoomMode = ZOOM_NONE; |
| 372 | mZoomModeGlobal = false; |
| 373 | mAllow4kZoom = false; |
| 374 | mFrameWidth = 0; |
| 375 | mFrameHeight = 0; |
| 376 | mForceAspectRatio = false; |
| 377 | mPixelAspectRatio = 1.0; |
| 378 | mScaleXDenom = 0; |
| 379 | mScaleYDenom = 0; |
| 380 | mForceFullScreen = false; |
sheng.liu | a7baed7 | 2024-01-24 07:31:24 +0000 | [diff] [blame] | 381 | mVideoPaused = false; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 382 | mPoll = new Tls::Poll(true); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | WstClientWayland::~WstClientWayland() |
| 386 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 387 | TRACE(mLogCategory,"deconstruct WstClientWayland"); |
| 388 | if (mPoll) { |
| 389 | delete mPoll; |
| 390 | mPoll = NULL; |
| 391 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | int WstClientWayland::connectToWayland() |
| 395 | { |
| 396 | const char *xdgEnv = NULL; |
| 397 | const char *displayEnv = NULL; |
| 398 | const char *displayName = "rlib-display"; |
| 399 | const char *fullScreenEnV = NULL; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 400 | DEBUG(mLogCategory,"in"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 401 | xdgEnv = getenv("XDG_RUNTIME_DIR"); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 402 | INFO(mLogCategory,"XDG_RUNTIME_DIR:%s",xdgEnv != NULL?xdgEnv:"NULL"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 403 | if (!xdgEnv) { |
| 404 | xdgEnv = "/run"; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 405 | ERROR(mLogCategory,"XDG_RUNTIME_DIR is not set,set default %s",xdgEnv); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 406 | } |
| 407 | displayEnv= getenv("WAYLAND_DISPLAY"); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 408 | INFO(mLogCategory,"WAYLAND_DISPLAY:%s",displayEnv != NULL?displayEnv:"NULL"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 409 | |
| 410 | fullScreenEnV= getenv("VIDEO_RENDER_FORCE_FULLSCREEN"); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 411 | INFO(mLogCategory,"VIDEO_RENDER_FORCE_FULLSCREEN:%s",fullScreenEnV != NULL?fullScreenEnV:"NULL"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 412 | if (fullScreenEnV) { |
| 413 | int force = atoi(fullScreenEnV); |
| 414 | mForceFullScreen = force != 0? true: false; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 415 | INFO(mLogCategory,"VIDEO_RENDER_FORCE_FULLSCREEN:%d",mForceFullScreen); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | mWlDisplay = wl_display_connect(NULL); |
| 419 | if (!mWlDisplay) { |
| 420 | char waylandDisplay[64]; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 421 | ERROR(mLogCategory,"Failed connect to default wayland display"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 422 | memset(waylandDisplay, 0, 64); |
| 423 | strcpy(waylandDisplay, xdgEnv); |
| 424 | strcat(waylandDisplay, "/" ); |
| 425 | strcat(waylandDisplay, displayName); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 426 | INFO(mLogCategory,"detect rlib wayland display %s",waylandDisplay); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 427 | if (access(waylandDisplay,F_OK) == 0) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 428 | INFO(mLogCategory,"try to connect to %s",waylandDisplay); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 429 | mWlDisplay = wl_display_connect(displayName); |
| 430 | } |
| 431 | /*try to create wayland display my self*/ |
| 432 | if (!mWlDisplay && xdgEnv) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 433 | WARNING(mLogCategory,"try to create and connect rlib-display display"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 434 | std::string cmdCreateDisplay = R"(curl 'http://127.0.0.1:9998/jsonrpc' -d '{"jsonrpc": "2.0","id": 4,"method": |
| 435 | "org.rdk.RDKShell.1.createDisplay","params": { "client": "rlib-display", "displayName": "rlib-display" }}';echo)"; |
| 436 | std::string cmdMoveDisplay = R"(curl 'http://127.0.0.1:9998/jsonrpc' -d '{"jsonrpc": "2.0","id": 4,"method": |
| 437 | "org.rdk.RDKShell.1.moveToBack", "params": { "client": "rlib-display" }}';echo)"; |
| 438 | |
| 439 | executeCmd(cmdCreateDisplay.c_str()); |
| 440 | executeCmd(cmdMoveDisplay.c_str()); |
| 441 | //try to connect again |
| 442 | mWlDisplay = wl_display_connect(displayName); |
| 443 | } |
| 444 | |
| 445 | if (!mWlDisplay) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 446 | ERROR(mLogCategory, "wayland connect rlib-display fail"); |
| 447 | return ERROR_OPEN_FAIL; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 448 | } |
fei.deng | a8f4b90 | 2024-02-22 03:49:07 +0000 | [diff] [blame^] | 449 | setenv("XDG_RUNTIME_DIR",xdgEnv,0); |
| 450 | setenv("WAYLAND_DISPLAY",displayName,0); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 451 | INFO(mLogCategory,"wayland connected to rlib-display"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | mWlQueue = wl_display_create_queue (mWlDisplay); |
| 455 | wl_proxy_set_queue ((struct wl_proxy *)mWlDisplay, mWlQueue); |
| 456 | |
| 457 | mWlRegistry = wl_display_get_registry (mWlDisplay); |
| 458 | wl_registry_add_listener (mWlRegistry, ®istry_listener, (void *)this); |
| 459 | |
| 460 | /* we need exactly 2 roundtrips to discover global objects and their state */ |
| 461 | for (int i = 0; i < 2; i++) { |
| 462 | if (wl_display_roundtrip_queue (mWlDisplay, mWlQueue) < 0) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 463 | ERROR(mLogCategory,"Error communicating with the wayland display"); |
| 464 | return ERROR_OPEN_FAIL; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
| 468 | //create surface from compositor |
| 469 | if (mWlCompositor) { |
| 470 | mWlSurface = wl_compositor_create_surface(mWlCompositor); |
| 471 | wl_proxy_set_queue((struct wl_proxy*)mWlSurface, mWlQueue); |
| 472 | wl_display_flush( mWlDisplay ); |
| 473 | } |
| 474 | |
| 475 | if (mWlVpc && mWlSurface) { |
| 476 | mWlVpcSurface = wl_vpc_get_vpc_surface( mWlVpc, mWlSurface ); |
| 477 | if (mWlVpcSurface) { |
| 478 | wl_vpc_surface_add_listener( mWlVpcSurface, &vpcListener, (void *)this); |
| 479 | wl_proxy_set_queue((struct wl_proxy*)mWlVpcSurface, mWlQueue); |
| 480 | wl_vpc_surface_set_geometry( mWlVpcSurface, mWindowX, mWindowY, mWindowWidth, mWindowHeight); |
| 481 | wl_display_flush(mWlDisplay); |
| 482 | } |
| 483 | } |
| 484 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 485 | INFO(mLogCategory,"out"); |
| 486 | return NO_ERROR; |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | void WstClientWayland::disconnectFromWayland() |
| 490 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 491 | INFO(mLogCategory,"in"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 492 | |
| 493 | if (isRunning()) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 494 | TRACE(mLogCategory,"try stop dispatch thread"); |
| 495 | if (mPoll) { |
| 496 | mPoll->setFlushing(true); |
| 497 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 498 | requestExitAndWait(); |
| 499 | } |
| 500 | |
| 501 | if (mWlShell) { |
| 502 | wl_simple_shell_destroy(mWlShell); |
| 503 | mWlShell = NULL; |
| 504 | } |
| 505 | |
| 506 | if (mWlVpcSurface) { |
| 507 | wl_vpc_surface_destroy(mWlVpcSurface); |
| 508 | mWlVpcSurface = NULL; |
| 509 | } |
| 510 | |
| 511 | if (mWlVpc) { |
| 512 | wl_vpc_destroy(mWlVpc); |
| 513 | mWlVpc = NULL; |
| 514 | } |
| 515 | |
| 516 | if (mWlSurface) { |
| 517 | wl_surface_destroy(mWlSurface); |
| 518 | mWlSurface = NULL; |
| 519 | } |
| 520 | |
| 521 | if (mWlOutput) { |
| 522 | wl_output_destroy(mWlOutput); |
| 523 | mWlOutput = NULL; |
| 524 | } |
| 525 | |
| 526 | if (mWlSb) { |
| 527 | wl_sb_destroy(mWlSb); |
| 528 | mWlSb = NULL; |
| 529 | } |
| 530 | |
| 531 | if (mWlCompositor) { |
| 532 | wl_compositor_destroy (mWlCompositor); |
| 533 | mWlCompositor = NULL; |
| 534 | } |
| 535 | |
| 536 | if (mWlRegistry) { |
| 537 | wl_registry_destroy (mWlRegistry); |
| 538 | mWlRegistry= NULL; |
| 539 | } |
| 540 | |
| 541 | if (mWlQueue) { |
| 542 | wl_event_queue_destroy (mWlQueue); |
| 543 | mWlQueue = NULL; |
| 544 | } |
| 545 | |
| 546 | if (mWlDisplay) { |
| 547 | wl_display_flush (mWlDisplay); |
| 548 | wl_display_disconnect (mWlDisplay); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 549 | INFO(mLogCategory,"wl display disconnect"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 550 | mWlDisplay = NULL; |
| 551 | } |
| 552 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 553 | INFO(mLogCategory,"out"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | void WstClientWayland::setWindowSize(int x, int y, int w, int h) |
| 557 | { |
| 558 | if (x == 0 && y == 0 && w == 0 && h == 0) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 559 | WARNING(mLogCategory, "set full screen? %dx%dx%dx%d",x,y,w,h); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 560 | return; |
| 561 | } |
| 562 | mWindowX = x; |
| 563 | mWindowY = y; |
| 564 | mWindowWidth = w; |
| 565 | mWindowHeight = h; |
| 566 | mWindowChange = true; |
| 567 | mWindowSet = true; |
| 568 | mWindowSizeOverride = true; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 569 | DEBUG(mLogCategory,"set window size:x:%d,y:%d,w:%d,h:%d",x,y,w,h); |
sheng.liu | a7baed7 | 2024-01-24 07:31:24 +0000 | [diff] [blame] | 570 | |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 571 | if (mWlVpcSurface) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 572 | DEBUG(mLogCategory, "wl_vpc_surface_set_geometry(%d,%d,%d,%d)",mWindowX,mWindowY,mWindowWidth,mWindowHeight); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 573 | wl_vpc_surface_set_geometry(mWlVpcSurface, mWindowX, mWindowY, mWindowWidth, mWindowHeight); |
| 574 | } |
| 575 | //if window size is updated, update video position |
sheng.liu | a7baed7 | 2024-01-24 07:31:24 +0000 | [diff] [blame] | 576 | if (mVideoPaused && mWlVpcSurface && mWindowChange && mScaleXDenom != 0 && mScaleYDenom != 0) { |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 577 | mWindowChange = false; |
| 578 | updateVideoPosition(); |
| 579 | } |
sheng.liu | a7baed7 | 2024-01-24 07:31:24 +0000 | [diff] [blame] | 580 | |
| 581 | if (mWlDisplay) { |
| 582 | wl_display_flush( mWlDisplay ); |
| 583 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | void WstClientWayland::setFrameSize(int frameWidth, int frameHeight) { |
| 587 | mFrameWidth = frameWidth; |
| 588 | mFrameHeight = frameHeight; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 589 | DEBUG(mLogCategory, "set frame size:%dx%d",mFrameWidth, mFrameHeight); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | void WstClientWayland::setZoomMode(int zoomMode, bool globalZoomActive, bool allow4kZoom) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 593 | DEBUG(mLogCategory, "zoomMode:%d, globalZoomActive:%d, allow4kZoom:%d",zoomMode,globalZoomActive,allow4kZoom); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 594 | mZoomModeGlobal = globalZoomActive; |
| 595 | if ( !globalZoomActive ) |
| 596 | { |
| 597 | mZoomMode = ZOOM_NONE; |
| 598 | } |
| 599 | mAllow4kZoom= allow4kZoom; |
| 600 | if ( mZoomModeGlobal == true ) |
| 601 | { |
| 602 | if ( (zoomMode >= ZOOM_NONE) && (zoomMode <= ZOOM_ZOOM) ) |
| 603 | { |
| 604 | mZoomMode = zoomMode; |
| 605 | mPixelAspectRatioChanged = true; |
| 606 | } |
| 607 | } else { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 608 | DEBUG(mLogCategory, "global zoom disabled: ignore server value"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
| 612 | void WstClientWayland::getVideoBounds(int *x, int *y, int *w, int *h) |
| 613 | { |
| 614 | int vx, vy, vw, vh; |
| 615 | int frameWidth, frameHeight; |
| 616 | int zoomMode; |
| 617 | double contentWidth, contentHeight; |
| 618 | double roix, roiy, roiw, roih; |
| 619 | double arf, ard; |
| 620 | double hfactor= 1.0, vfactor= 1.0; |
| 621 | |
| 622 | //if wayland open fail,use the default window size |
| 623 | if (!mWlDisplay) { |
| 624 | *x = mWindowX; |
| 625 | *y = mWindowY; |
| 626 | *w = mWindowWidth; |
| 627 | *h = mWindowHeight; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 628 | TRACE(mLogCategory, "no wldisplay, %d,%d,%d,%d",mVideoX,mVideoY,mVideoWidth,mVideoHeight); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 629 | return; |
| 630 | } |
| 631 | |
| 632 | //if window size is updated, update video position |
| 633 | if (mWlVpcSurface && mWindowChange) { |
| 634 | mWindowChange = false; |
| 635 | updateVideoPosition(); |
| 636 | } |
| 637 | |
| 638 | vx = mVideoX; |
| 639 | vy = mVideoY; |
| 640 | vw = mVideoWidth; |
| 641 | vh = mVideoHeight; |
| 642 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 643 | TRACE(mLogCategory, "videoX:%d,videoY:%d,videoW:%d,videoH:%d",mVideoX,mVideoY,mVideoWidth,mVideoHeight); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 644 | //if the window size is smaller than display size,we scall video to map window size |
| 645 | if (mWindowWidth != mDisplayWidth || mWindowHeight != mDisplayHeight) { |
| 646 | *x= vx; |
| 647 | *y= vy; |
| 648 | *w= vw; |
| 649 | *h= vh; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 650 | TRACE(mLogCategory, "small window vrect %d, %d, %d, %d", vx, vy, vw, vh); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 651 | return; |
| 652 | } |
| 653 | if (mForceFullScreen) { |
| 654 | *x= vx; |
| 655 | *y= vy; |
| 656 | *w= vw; |
| 657 | *h= vh; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 658 | TRACE(mLogCategory, "force window vrect %d, %d, %d, %d", vx, vy, vw, vh); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 659 | return; |
| 660 | } |
| 661 | |
| 662 | frameWidth = mFrameWidth; |
| 663 | frameHeight = mFrameHeight; |
| 664 | contentWidth = frameWidth * mPixelAspectRatio; |
| 665 | contentHeight = frameHeight; |
| 666 | |
| 667 | if (mPixelAspectRatioChanged) |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 668 | DEBUG(mLogCategory,"pixelAspectRatio: %f zoom-mode %d", mPixelAspectRatio, mZoomMode); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 669 | |
| 670 | ard = (double)mVideoWidth/(double)mVideoHeight; |
| 671 | arf = (double)contentWidth/(double)contentHeight; |
| 672 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 673 | TRACE(mLogCategory, "frameWidth:%d,frameHeight:%d,contentWidth:%f,contentHeight:%f",frameWidth,frameHeight,contentWidth,contentHeight); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 674 | |
| 675 | /* Establish region of interest */ |
| 676 | roix = 0; |
| 677 | roiy = 0; |
| 678 | roiw = contentWidth; |
| 679 | roih = contentHeight; |
| 680 | |
| 681 | zoomMode = mZoomMode; |
| 682 | if ((mFrameWidth > 1920) || (mFrameHeight > 1080)) |
| 683 | { |
| 684 | zoomMode= ZOOM_NORMAL; |
| 685 | } |
| 686 | //if (mPixelAspectRatioChanged ) |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 687 | TRACE(mLogCategory, "ard %f arf %f", ard, arf); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 688 | switch ( zoomMode ) |
| 689 | { |
| 690 | case ZOOM_NORMAL: |
| 691 | { |
| 692 | if ( arf >= ard ) |
| 693 | { |
| 694 | vw = mVideoWidth * (1.0); |
| 695 | vh = (roih * vw) / roiw; |
| 696 | vx = vx+(mVideoWidth-vw)/2; |
| 697 | vy = vy+(mVideoHeight-vh)/2; |
| 698 | } |
| 699 | else |
| 700 | { |
| 701 | vh = mVideoHeight * (1.0); |
| 702 | vw = (roiw * vh) / roih; |
| 703 | vx = vx+(mVideoWidth-vw)/2; |
| 704 | vy = vy+(mVideoHeight-vh)/2; |
| 705 | } |
| 706 | } |
| 707 | break; |
| 708 | case ZOOM_NONE: |
| 709 | case ZOOM_DIRECT: |
| 710 | { |
| 711 | if ( arf >= ard ) |
| 712 | { |
| 713 | vh = (contentHeight * mVideoWidth) / contentWidth; |
| 714 | vy = vy+(mVideoHeight-vh)/2; |
| 715 | } |
| 716 | else |
| 717 | { |
| 718 | vw = (contentWidth * mVideoHeight) / contentHeight; |
| 719 | vx = vx+(mVideoWidth-vw)/2; |
| 720 | } |
| 721 | } |
| 722 | break; |
| 723 | case ZOOM_16_9_STRETCH: |
| 724 | { |
| 725 | if ( approxEqual(arf, ard) && approxEqual(arf, 1.777) ) |
| 726 | { |
| 727 | /* For 16:9 content on a 16:9 display, stretch as though 4:3 */ |
| 728 | hfactor= 4.0/3.0; |
| 729 | if (mPixelAspectRatioChanged ) |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 730 | DEBUG(mLogCategory, "stretch apply vfactor %f hfactor %f", vfactor, hfactor); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 731 | } |
| 732 | vh = mVideoHeight * (1.0); |
| 733 | vw = vh*hfactor*16/9; |
| 734 | vx = vx+(mVideoWidth-vw)/2; |
| 735 | vy = vy+(mVideoHeight-vh)/2; |
| 736 | } |
| 737 | break; |
| 738 | case ZOOM_4_3_PILLARBOX: |
| 739 | { |
| 740 | vh = mVideoHeight * (1.0); |
| 741 | vw = vh*4/3; |
| 742 | vx = vx+(mVideoWidth-vw)/2; |
| 743 | vy = vy+(mVideoHeight-vh)/2; |
| 744 | } |
| 745 | break; |
| 746 | case ZOOM_ZOOM: |
| 747 | { |
| 748 | if ( arf >= ard ) |
| 749 | { |
| 750 | if (approxEqual(arf, ard) && approxEqual( arf, 1.777) ) |
| 751 | { |
| 752 | /* For 16:9 content on a 16:9 display, enlarge as though 4:3 */ |
| 753 | vfactor= 4.0/3.0; |
| 754 | hfactor= 1.0; |
| 755 | if (mPixelAspectRatioChanged ) |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 756 | DEBUG(mLogCategory, "zoom apply vfactor %f hfactor %f", vfactor, hfactor); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 757 | } |
| 758 | vh = mVideoHeight * vfactor * (1.0); |
| 759 | vw = (roiw * vh) * hfactor / roih; |
| 760 | vx = vx+(mVideoWidth-vw)/2; |
| 761 | vy = vy+(mVideoHeight-vh)/2; |
| 762 | } |
| 763 | else |
| 764 | { |
| 765 | vw = mVideoWidth * (1.0); |
| 766 | vh = (roih * vw) / roiw; |
| 767 | vx = vx+(mVideoWidth-vw)/2; |
| 768 | vy = vy+(mVideoHeight-vh)/2; |
| 769 | } |
| 770 | } |
| 771 | break; |
| 772 | } |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 773 | if (mPixelAspectRatioChanged) DEBUG(mLogCategory, "vrect %d, %d, %d, %d", vx, vy, vw, vh); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 774 | if (mPixelAspectRatioChanged) |
| 775 | { |
| 776 | if (mWlDisplay && mWlVpcSurface ) |
| 777 | { |
| 778 | wl_vpc_surface_set_geometry(mWlVpcSurface, mWindowX, mWindowY, mWindowWidth, mWindowHeight); |
| 779 | wl_display_flush(mWlDisplay); |
| 780 | } |
| 781 | } |
| 782 | mPixelAspectRatioChanged = false; |
| 783 | *x= vx; |
| 784 | *y= vy; |
| 785 | *w= vw; |
| 786 | *h= vh; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 787 | TRACE(mLogCategory, "vrect %d, %d, %d, %d", vx, vy, vw, vh); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | void WstClientWayland::setTextureCrop(int vx, int vy, int vw, int vh) |
| 791 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 792 | DEBUG(mLogCategory, "vx %d vy %d vw %d vh %d window(%d, %d, %d, %d) display(%dx%d)", \ |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 793 | vx, vy, vw, vh, mWindowX, mWindowY, mWindowWidth, mWindowHeight, mDisplayWidth, mDisplayHeight); |
| 794 | if ( (mDisplayWidth != -1) && (mDisplayHeight != -1) && |
| 795 | ( (vx < 0) || (vx+vw > mDisplayWidth) || |
| 796 | (vy < 0) || (vy+vh > mDisplayHeight) ) ) |
| 797 | { |
| 798 | int cropx, cropy, cropw, croph; |
| 799 | int wx1, wx2, wy1, wy2; |
| 800 | cropx= 0; |
| 801 | cropw= mWindowWidth; |
| 802 | cropy= 0; |
| 803 | croph= mWindowHeight; |
| 804 | if ( (vx < mWindowX) || (vx+vw > mWindowX+mWindowWidth) ) |
| 805 | { |
| 806 | cropx= (mWindowX-vx)*mWindowWidth/vw; |
| 807 | cropw= (mWindowX+mWindowWidth-vx)*mWindowWidth/vw - cropx; |
| 808 | } |
| 809 | else if ( vx < 0 ) |
| 810 | { |
| 811 | cropx= -vx*mWindowWidth/vw; |
| 812 | cropw= (vw+vx)*mWindowWidth/vw; |
| 813 | } |
| 814 | else if ( vx+vw > mWindowWidth ) |
| 815 | { |
| 816 | cropx= 0; |
| 817 | cropw= (mWindowWidth-vx)*mWindowWidth/vw; |
| 818 | } |
| 819 | |
| 820 | if ( (vy < mWindowY) || (vy+vh > mWindowY+mWindowHeight) ) |
| 821 | { |
| 822 | cropy= (mWindowY-vy)*mWindowHeight/vh; |
| 823 | croph= (mWindowY+mWindowHeight-vy)*mWindowHeight/vh - cropy; |
| 824 | } |
| 825 | else if ( vy < 0 ) |
| 826 | { |
| 827 | cropy= -vy*mWindowHeight/vh; |
| 828 | croph= (vh+vy)*mWindowHeight/vh; |
| 829 | } |
| 830 | else if ( vy+vh > mWindowHeight ) |
| 831 | { |
| 832 | cropy= 0; |
| 833 | croph= (mWindowHeight-vy)*mWindowHeight/vh; |
| 834 | } |
| 835 | |
| 836 | wx1 = vx; |
| 837 | wx2 = vx+vw; |
| 838 | wy1 = vy; |
| 839 | wy2 = vy+vh; |
| 840 | vx = mWindowX; |
| 841 | vy = mWindowY; |
| 842 | vw = mWindowWidth; |
| 843 | vh = mWindowHeight; |
| 844 | if ( (wx1 > vx) && (wx1 > 0) ) |
| 845 | { |
| 846 | vx= wx1; |
| 847 | } |
| 848 | else if ( (wx1 >= vx) && (wx1 < 0) ) |
| 849 | { |
| 850 | vw += wx1; |
| 851 | vx= 0; |
| 852 | } |
| 853 | else if ( wx2 < vx+vw ) |
| 854 | { |
| 855 | vw= wx2-vx; |
| 856 | } |
| 857 | if ( (wx1 >= 0) && (wx2 > vw) ) |
| 858 | { |
| 859 | vw= vw-wx1; |
| 860 | } |
| 861 | else if ( wx2 < vx+vw ) |
| 862 | { |
| 863 | vw= wx2-vx; |
| 864 | } |
| 865 | |
| 866 | if ( (wy1 > vy) && (wy1 > 0) ) |
| 867 | { |
| 868 | vy= wy1; |
| 869 | } |
| 870 | else if ( (wy1 >= vy) && (wy1 < 0) ) |
| 871 | { |
| 872 | vy= 0; |
| 873 | } |
| 874 | else if ( (wy1 < vy) && (wy1 > 0) ) |
| 875 | { |
| 876 | vh -= wy1; |
| 877 | } |
| 878 | if ( (wy1 >= 0) && (wy2 > vh) ) |
| 879 | { |
| 880 | vh= vh-wy1; |
| 881 | } |
| 882 | else if ( wy2 < vy+vh ) |
| 883 | { |
| 884 | vh= wy2-vy; |
| 885 | } |
| 886 | if ( vw < 0 ) vw= 0; |
| 887 | if ( vh < 0 ) vh= 0; |
| 888 | cropx= (cropx*WL_VPC_SURFACE_CROP_DENOM)/mWindowWidth; |
| 889 | cropy= (cropy*WL_VPC_SURFACE_CROP_DENOM)/mWindowHeight; |
| 890 | cropw= (cropw*WL_VPC_SURFACE_CROP_DENOM)/mWindowWidth; |
| 891 | croph= (croph*WL_VPC_SURFACE_CROP_DENOM)/mWindowHeight; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 892 | DEBUG(mLogCategory,"%d, %d, %d, %d - %d, %d, %d, %d", vx, vy, vw, vh, cropx, cropy, cropw, croph); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 893 | if (mWlVpcSurface) { |
| 894 | wl_vpc_surface_set_geometry_with_crop(mWlVpcSurface, vx, vy, vw, vh, cropx, cropy, cropw, croph ); |
| 895 | } |
| 896 | } |
| 897 | else |
| 898 | { |
| 899 | if (mWlVpcSurface) { |
| 900 | wl_vpc_surface_set_geometry(mWlVpcSurface, mWindowX, mWindowY, mWindowWidth, mWindowHeight); |
| 901 | } |
| 902 | } |
| 903 | } |
| 904 | |
| 905 | void WstClientWayland::setForceAspectRatio(bool force) |
| 906 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 907 | DEBUG(mLogCategory, "force aspect ratio:%d",force); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 908 | mForceAspectRatio = force; |
| 909 | } |
| 910 | |
| 911 | void WstClientWayland::setPixelAspectRatio(double ratio) |
| 912 | { |
| 913 | mPixelAspectRatio = ratio; |
| 914 | mPixelAspectRatioChanged = true; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 915 | INFO(mLogCategory, "set aspect ratio:%f",ratio); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | void WstClientWayland::updateVideoPosition() |
| 919 | { |
| 920 | bool needUpdate= true; |
| 921 | int vx, vy, vw, vh; |
| 922 | vx= mVideoX; |
| 923 | vy= mVideoY; |
| 924 | vw= mVideoWidth; |
| 925 | vh= mVideoHeight; |
| 926 | |
| 927 | if (mWindowSizeOverride) |
| 928 | { |
| 929 | mVideoX= ((mWindowX*mScaleXNum)/mScaleXDenom) + mTransX; |
| 930 | mVideoY= ((mWindowY*mScaleYNum)/mScaleYDenom) + mTransY; |
| 931 | mVideoWidth= (mWindowWidth*mScaleXNum)/mScaleXDenom; |
| 932 | mVideoHeight= (mWindowHeight*mScaleYNum)/mScaleYDenom; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 933 | DEBUG(mLogCategory, "window override video rectangle(%d,%d,%d,%d)",mVideoX,mVideoY,mVideoWidth,mVideoHeight); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 934 | } |
| 935 | else |
| 936 | { |
| 937 | mVideoX = mTransX; |
| 938 | mVideoY = mTransY; |
| 939 | mVideoWidth = (mOutputWidth*mScaleXNum)/mScaleXDenom; |
| 940 | mVideoHeight = (mOutputHeight*mScaleYNum)/mScaleYDenom; |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 941 | DEBUG(mLogCategory, "video rectangle(%d,%d,%d,%d)",mVideoX,mVideoY,mVideoWidth,mVideoHeight); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | if (vx == mVideoX && vy == mVideoY && vw == mVideoWidth && vh == mVideoHeight) { |
| 945 | needUpdate = false; |
| 946 | } |
| 947 | |
| 948 | if (needUpdate) { |
| 949 | /* Send a buffer to compositor to update hole punch geometry */ |
| 950 | if (mWlSb) |
| 951 | { |
| 952 | struct wl_buffer *buff; |
| 953 | |
| 954 | buff = wl_sb_create_buffer( mWlSb, |
| 955 | 0, |
| 956 | mWindowWidth, |
| 957 | mWindowHeight, |
| 958 | mWindowWidth*4, |
| 959 | WL_SB_FORMAT_ARGB8888 ); |
| 960 | wl_surface_attach(mWlSurface, buff, mWindowX, mWindowY); |
| 961 | wl_surface_damage(mWlSurface, 0, 0, mWindowWidth, mWindowHeight); |
| 962 | wl_surface_commit(mWlSurface); |
| 963 | } |
sheng.liu | a7baed7 | 2024-01-24 07:31:24 +0000 | [diff] [blame] | 964 | } |
| 965 | if (mPlugin) |
| 966 | { |
| 967 | mPlugin->setVideoRect(mVideoX, mVideoY, mVideoWidth, mVideoHeight); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 968 | } |
| 969 | } |
| 970 | |
| 971 | void WstClientWayland::setVideoPath(bool useGfxPath ) |
| 972 | { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 973 | INFO(mLogCategory,"useGfxPath:%d",useGfxPath); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 974 | if ( needBounds() && mWlVpcSurface ) |
| 975 | { |
| 976 | /* Use nominal display size provided to us by |
| 977 | * the compositor to calculate the video bounds |
| 978 | * we should use when we transition to graphics path. |
| 979 | * Save and restore current HW video rectangle. */ |
| 980 | int vx, vy, vw, vh; |
| 981 | int tx, ty, tw, th; |
| 982 | tx = mVideoX; |
| 983 | ty = mVideoY; |
| 984 | tw = mVideoWidth; |
| 985 | th = mVideoHeight; |
| 986 | mVideoX = mWindowX; |
| 987 | mVideoY = mWindowY; |
| 988 | mVideoWidth = mWindowWidth; |
| 989 | mVideoHeight = mWindowHeight; |
| 990 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 991 | DEBUG(mLogCategory, "video rect (%d,%d,%d,%d)",mVideoX,mVideoY,mVideoWidth,mVideoHeight); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 992 | |
| 993 | if (mFrameWidth > 0 && mFrameHeight > 0) { |
| 994 | getVideoBounds(&vx, &vy, &vw, &vh); |
| 995 | setTextureCrop(vx, vy, vw, vh); |
| 996 | } |
| 997 | |
| 998 | mVideoX = tx; |
| 999 | mVideoY = ty; |
| 1000 | mVideoWidth = tw; |
| 1001 | mVideoHeight = th; |
| 1002 | } |
| 1003 | } |
| 1004 | |
| 1005 | bool WstClientWayland::approxEqual( double v1, double v2 ) |
| 1006 | { |
| 1007 | bool result= false; |
| 1008 | if ( v1 >= v2 ) |
| 1009 | { |
| 1010 | if ( (v1-v2) < 0.001 ) |
| 1011 | { |
| 1012 | result= true; |
| 1013 | } |
| 1014 | } |
| 1015 | else |
| 1016 | { |
| 1017 | if ( (v2-v1) < 0.001 ) |
| 1018 | { |
| 1019 | result= true; |
| 1020 | } |
| 1021 | } |
| 1022 | return result; |
| 1023 | } |
| 1024 | |
| 1025 | void WstClientWayland::readyToRun() |
| 1026 | { |
| 1027 | mFd = wl_display_get_fd (mWlDisplay); |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1028 | if (mPoll) { |
| 1029 | mPoll->addFd(mFd); |
| 1030 | mPoll->setFdReadable(mFd, true); |
| 1031 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | bool WstClientWayland::threadLoop() |
| 1035 | { |
| 1036 | struct pollfd pfd; |
| 1037 | int ret; |
| 1038 | |
| 1039 | while (wl_display_prepare_read_queue (mWlDisplay, mWlQueue) != 0) { |
| 1040 | wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue); |
| 1041 | } |
| 1042 | |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1043 | wl_display_flush (mWlDisplay); |
| 1044 | |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1045 | /*poll timeout value must > 300 ms,otherwise zwp_linux_dmabuf will create failed, |
| 1046 | so do use -1 to wait for ever*/ |
| 1047 | ret = mPoll->wait(-1); //wait for ever |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1048 | if (ret < 0) { //poll error |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1049 | WARNING(mLogCategory,"poll error"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1050 | wl_display_cancel_read(mWlDisplay); |
| 1051 | return false; |
| 1052 | } else if (ret == 0) { //poll time out |
| 1053 | return true; //run loop |
| 1054 | } |
| 1055 | |
| 1056 | if (wl_display_read_events (mWlDisplay) == -1) { |
| 1057 | goto tag_error; |
| 1058 | } |
| 1059 | |
| 1060 | wl_display_dispatch_queue_pending (mWlDisplay, mWlQueue); |
| 1061 | return true; |
| 1062 | tag_error: |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1063 | ERROR(mLogCategory,"Error communicating with the wayland server"); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1064 | return false; |
| 1065 | } |
| 1066 | |
| 1067 | void WstClientWayland::executeCmd(const char *cmd) { |
fei.deng | b9a1a57 | 2023-09-13 01:33:57 +0000 | [diff] [blame] | 1068 | INFO(mLogCategory,"%s", cmd); |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1069 | FILE* pFile = popen(cmd, "r"); |
fei.deng | a8f4b90 | 2024-02-22 03:49:07 +0000 | [diff] [blame^] | 1070 | if (pFile) { |
| 1071 | char buf[128]; |
| 1072 | char* retStr = fgets(buf, sizeof(buf), pFile); |
| 1073 | INFO(mLogCategory,"ret= %s", retStr); |
| 1074 | pclose(pFile); |
| 1075 | } |
fei.deng | f7a0cd3 | 2023-08-29 09:36:37 +0000 | [diff] [blame] | 1076 | } |