The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

c/multi: Start and stop the native session

This depends on the number of active app sessions.

authored by

Jakob Bornecrantz and committed by
Jakob Bornecrantz
2632c223 601143bf

+160 -8
+17 -2
src/xrt/compositor/multi/comp_multi_compositor.c
··· 458 458 COMP_TRACE_MARKER(); 459 459 460 460 struct multi_compositor *mc = multi_compositor(xc); 461 - (void)mc; 461 + 462 + assert(!mc->state.session_active); 463 + if (!mc->state.session_active) { 464 + multi_system_compositor_update_session_status(mc->msc, true); 465 + mc->state.session_active = true; 466 + } 462 467 463 468 return XRT_SUCCESS; 464 469 } ··· 469 474 COMP_TRACE_MARKER(); 470 475 471 476 struct multi_compositor *mc = multi_compositor(xc); 472 - (void)mc; 477 + 478 + assert(mc->state.session_active); 479 + if (mc->state.session_active) { 480 + multi_system_compositor_update_session_status(mc->msc, false); 481 + mc->state.session_active = false; 482 + } 473 483 474 484 return XRT_SUCCESS; 475 485 } ··· 828 838 COMP_TRACE_MARKER(); 829 839 830 840 struct multi_compositor *mc = multi_compositor(xc); 841 + 842 + if (mc->state.session_active) { 843 + multi_system_compositor_update_session_status(mc->msc, false); 844 + mc->state.session_active = false; 845 + } 831 846 832 847 os_mutex_lock(&mc->msc->list_and_timing_lock); 833 848
+59
src/xrt/compositor/multi/comp_multi_private.h
··· 124 124 } current; 125 125 126 126 int64_t z_order; 127 + 128 + bool session_active; 127 129 } state; 128 130 129 131 struct ··· 225 227 */ 226 228 227 229 /*! 230 + * State of the multi compositor system, use to track the calling of native 231 + * compositor methods @ref xrt_comp_begin_session and @ref xrt_comp_end_session. 232 + * 233 + * It is driven by the number of active app sessions. 234 + * 235 + * @ingroup comp_multi 236 + */ 237 + enum multi_system_state 238 + { 239 + /*! 240 + * Initial state and post stopping state. 241 + * 242 + * The multi system compositor have called @ref xrt_comp_end_session 243 + * on it's @ref xrt_compositor_native. 244 + */ 245 + MULTI_SYSTEM_STATE_STOPPED, 246 + 247 + /*! 248 + * The main session is running. 249 + * 250 + * The multi system compositor have called @ref xrt_comp_begin_session 251 + * on it's @ref xrt_compositor_native. 252 + */ 253 + MULTI_SYSTEM_STATE_RUNNING, 254 + 255 + /*! 256 + * There are no active sessions and the multi compositor system is 257 + * drawing on or more clear frames. 258 + * 259 + * The multi system compositor have not yet @ref xrt_comp_begin_session 260 + * on it's @ref xrt_compositor_native. 261 + */ 262 + MULTI_SYSTEM_STATE_STOPPING, 263 + }; 264 + 265 + /*! 228 266 * The multi compositor system, multiplexes access to the native compositors 229 267 * and tracks some state needed. 230 268 * ··· 246 284 //! Render loop thread. 247 285 struct os_thread_helper oth; 248 286 287 + struct 288 + { 289 + /*! 290 + * The state of the multi compositor system, this is updated on 291 + * the oth thread, aka multi compositor system main thread. 292 + * It is driven by the active_count field. 293 + */ 294 + enum multi_system_state state; 295 + 296 + //! Number of active sessions, protected by oth. 297 + uint64_t active_count; 298 + } sessions; 299 + 249 300 /*! 250 301 * This mutex protects the list of client compositor 251 302 * and the rendering timings on it. ··· 268 319 return (struct multi_system_compositor *)xsc; 269 320 } 270 321 322 + /*! 323 + * The client compositor calls this function to update when it's session is 324 + * started or stopped. 325 + * 326 + * @ingroup comp_multi 327 + */ 328 + void 329 + multi_system_compositor_update_session_status(struct multi_system_compositor *msc, bool active); 271 330 272 331 273 332 #ifdef __cplusplus
+84 -6
src/xrt/compositor/multi/comp_multi_system.c
··· 353 353 xrt_comp_mark_frame(xc, frame_id, XRT_COMPOSITOR_FRAME_POINT_WOKE, now_ns); 354 354 } 355 355 356 + static void 357 + update_session_state_locked(struct multi_system_compositor *msc) 358 + { 359 + struct xrt_compositor *xc = &msc->xcn->base; 360 + 361 + //! @todo Don't make this a hack. 362 + enum xrt_view_type view_type = XRT_VIEW_TYPE_STEREO; 363 + 364 + switch (msc->sessions.state) { 365 + case MULTI_SYSTEM_STATE_STOPPED: 366 + if (msc->sessions.active_count == 0) { 367 + break; 368 + } 369 + 370 + U_LOG_I("Starting native session, %u active app session(s).", (uint32_t)msc->sessions.active_count); 371 + msc->sessions.state = MULTI_SYSTEM_STATE_RUNNING; 372 + xrt_comp_begin_session(xc, view_type); 373 + break; 374 + 375 + case MULTI_SYSTEM_STATE_RUNNING: 376 + if (msc->sessions.active_count > 0) { 377 + break; 378 + } 379 + U_LOG_I("Stopping main session, %u active app session(s).", (uint32_t)msc->sessions.active_count); 380 + msc->sessions.state = MULTI_SYSTEM_STATE_STOPPING; 381 + break; 382 + 383 + case MULTI_SYSTEM_STATE_STOPPING: 384 + U_LOG_I("Stopped main session, %u active app session(s).", (uint32_t)msc->sessions.active_count); 385 + msc->sessions.state = MULTI_SYSTEM_STATE_STOPPED; 386 + xrt_comp_end_session(xc); 387 + break; 388 + 389 + default: assert(false); 390 + } 391 + } 392 + 356 393 static int 357 394 multi_main_loop(struct multi_system_compositor *msc) 358 395 { ··· 362 399 363 400 struct xrt_compositor *xc = &msc->xcn->base; 364 401 365 - //! @todo Don't make this a hack. 366 - enum xrt_view_type view_type = XRT_VIEW_TYPE_STEREO; 367 - 368 - xrt_comp_begin_session(xc, view_type); 369 - 402 + // Protect the thread state and the sessions state. 370 403 os_thread_helper_lock(&msc->oth); 404 + 371 405 while (os_thread_helper_is_running_locked(&msc->oth)) { 406 + 407 + // Updates msc->sessions.active depending on active client sessions. 408 + update_session_state_locked(msc); 409 + 410 + if (msc->sessions.state == MULTI_SYSTEM_STATE_STOPPED) { 411 + // Sleep and wait to be signaled. 412 + os_thread_helper_wait_locked(&msc->oth); 413 + 414 + // Loop back to running and session check. 415 + continue; 416 + } 417 + 418 + // Unlock the thread after the checks has been done. 372 419 os_thread_helper_unlock(&msc->oth); 373 420 374 421 int64_t frame_id = -1; ··· 412 459 os_thread_helper_lock(&msc->oth); 413 460 } 414 461 415 - xrt_comp_end_session(xc); 462 + // Clean up the sessions state. 463 + switch (msc->sessions.state) { 464 + case MULTI_SYSTEM_STATE_RUNNING: 465 + case MULTI_SYSTEM_STATE_STOPPING: 466 + U_LOG_I("Stopped native session, shutting down."); 467 + xrt_comp_end_session(xc); 468 + break; 469 + case MULTI_SYSTEM_STATE_STOPPED: break; 470 + default: assert(false); 471 + } 472 + 416 473 os_thread_helper_unlock(&msc->oth); 417 474 418 475 return 0; ··· 524 581 * 525 582 */ 526 583 584 + void 585 + multi_system_compositor_update_session_status(struct multi_system_compositor *msc, bool active) 586 + { 587 + os_thread_helper_lock(&msc->oth); 588 + 589 + if (active) { 590 + assert(msc->sessions.active_count < UINT32_MAX); 591 + msc->sessions.active_count++; 592 + 593 + // If the thread is sleeping wake it up. 594 + os_thread_helper_signal_locked(&msc->oth); 595 + } else { 596 + assert(msc->sessions.active_count > 0); 597 + msc->sessions.active_count--; 598 + } 599 + 600 + os_thread_helper_unlock(&msc->oth); 601 + } 602 + 527 603 xrt_result_t 528 604 comp_multi_create_system_compositor(struct xrt_compositor_native *xcn, 529 605 struct u_pacing_app_factory *upaf, ··· 540 616 msc->base.info = *xsci; 541 617 msc->upaf = upaf; 542 618 msc->xcn = xcn; 619 + msc->sessions.state = MULTI_SYSTEM_STATE_STOPPED; 620 + msc->sessions.active_count = 0; 543 621 544 622 os_mutex_init(&msc->list_and_timing_lock); 545 623