Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ Release notes on GitHub are taken from the matching section here.

## [Unreleased]

### Fixed

- With Vulkan on NVIDIA under Wayland, joining a game no longer crashes when the window changes size
during the join. Since 0.5.6, the join left two Vulkan surfaces on the window; Nightcap now keeps one
surface for the window and gives it back to Roblox each time.
- Resizing the window while a game loads no longer sends you back to the home screen. The new size now
goes to the game once it has started.

## [0.5.6] - 2026-12-01

### Changed
Expand Down
19 changes: 19 additions & 0 deletions include/mocktail/graphics/swapchain_retirement.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ struct KeptSurface {
explicit operator bool() const { return surface != VK_NULL_HANDLE; }
};

// The one host surface for the SDL window. NVIDIA's Wayland driver crashes on
// present once a second VkSurfaceKHR exists for the window, which Roblox makes
// on every game join, so later creates get this one back and the game's
// destroys keep it until its instance goes.
class SharedWindowSurface final {
public:
VkSurfaceKHR Acquire(VkInstance instance,
const VkAllocationCallbacks* allocator);
void Store(VkInstance instance, VkSurfaceKHR surface,
const VkAllocationCallbacks* allocator);
bool Holds(VkSurfaceKHR surface);
VkSurfaceKHR Release(VkInstance instance);

private:
std::mutex mutex_;
VkInstance instance_ = VK_NULL_HANDLE;
VkSurfaceKHR surface_ = VK_NULL_HANDLE;
};

// Keeps the driver from destroying and creating swapchains Roblox rebuilds
// unchanged. NVIDIA on X11 with PRIME blocks ~22 s in each swapchain destroy or
// retiring create. Roblox rebuilds an identical swapchain on every view change
Expand Down
7 changes: 7 additions & 0 deletions include/runtime/roblox_experience_composition.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ class RobloxExperienceComposition final {
jobject surface_object, jobject platform_params, jobject activity,
jobject* start_app_params);
Status RebindLuaAppSurface(const GameSurface& surface);
void ApplySurfaceChangedDuringLaunch(
const GameSurface& surface, uint64_t launch_generation,
bool game_started,
const std::shared_ptr<RobloxFreshGameLaunchController>& controller);
Status RestartLuaAppSurface(const GameSurface& surface);
Status ReleaseLuaAppGlobalObjects();
Status ReleaseGlobalObjects();
Expand Down Expand Up @@ -297,6 +301,9 @@ class RobloxExperienceComposition final {
bool lua_app_return_pending_ = false;
bool controlled_switch_waiting_for_return_ = false;
bool lua_app_surface_recreation_pending_ = false;
// Roblox is already in the game stage while a launch runs, so a LuaApp
// surface update then abandons the join; the game gets it once started.
std::optional<GameSurface> surface_changed_during_launch_;
bool late_surface_tracking_ = false;
GameSurface late_surface_snapshot_;
jnivm::VM* late_lifecycle_vm_ = nullptr;
Expand Down
26 changes: 26 additions & 0 deletions src/graphics/bionic_vulkan_loader_adapter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ mocktail::graphics::SwapchainRetirement& Retirement() {
return retirement;
}

mocktail::graphics::SharedWindowSurface& WindowSurface() {
static mocktail::graphics::SharedWindowSurface surface;
return surface;
}

void NoteSwapchainResult(VkResult result, const char* call) {
if (result != VK_ERROR_OUT_OF_DATE_KHR &&
result != VK_ERROR_SURFACE_LOST_KHR) {
Expand Down Expand Up @@ -1890,6 +1895,9 @@ vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks* allocator) {
parent_of_kept_device = state.kept_device.Held() != VK_NULL_HANDLE &&
state.kept_device_instance == instance;
}
// A leaked instance leaks its surface too; the next instance makes its own.
const VkSurfaceKHR window_surface =
kept ? VK_NULL_HANDLE : WindowSurface().Release(instance);
if (kept) {
std::fprintf(stderr,
" [vulkan] keeping the host VkInstance for the next "
Expand All @@ -1903,6 +1911,9 @@ vkDestroyInstance(VkInstance instance, const VkAllocationCallbacks* allocator) {
" [vulkan] keeping the old Vulkan instance; destroying it "
"after the new one crashes the NVIDIA driver\n");
} else if (host_destroy != nullptr) {
if (window_surface != VK_NULL_HANDLE) {
State().android_wsi.DestroySurface(instance, window_surface, nullptr);
}
host_destroy(instance, allocator);
}
AdapterState& state = State();
Expand Down Expand Up @@ -1942,9 +1953,18 @@ VKAPI_ATTR VkResult VKAPI_CALL vkCreateAndroidSurfaceKHR(
if (!EnsureInitialized()) {
return VK_ERROR_INITIALIZATION_FAILED;
}
if (const VkSurfaceKHR shared = WindowSurface().Acquire(instance, allocator);
shared != VK_NULL_HANDLE && surface != nullptr) {
*surface = shared;
std::fprintf(stderr,
" [vulkan] reusing the window's host surface for the new "
"Android surface\n");
return VK_SUCCESS;
}
const auto result = State().android_wsi.CreateAndroidSurface(
instance, create_info, allocator, surface);
if (result == VK_SUCCESS) {
WindowSurface().Store(instance, *surface, allocator);
std::fprintf(stderr, " [vulkan] Android surface mapped to SDL WSI\n");
}
return static_cast<VkResult>(result);
Expand All @@ -1956,6 +1976,12 @@ vkDestroySurfaceKHR(VkInstance instance, VkSurfaceKHR surface,
if (!EnsureInitialized()) {
return;
}
if (WindowSurface().Holds(surface)) {
std::fprintf(stderr,
" [vulkan] keeping the window's host surface for the next "
"Android surface\n");
return;
}
if (Retirement().SurfaceDestroyed(surface, instance, allocator)) {
std::fprintf(stderr,
" [vulkan] keeping the old surface until its swapchain is "
Expand Down
30 changes: 30 additions & 0 deletions src/graphics/swapchain_retirement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,36 @@ bool SwapchainShape::operator==(const SwapchainShape& other) const {
present_mode == other.present_mode && clipped == other.clipped;
}

VkSurfaceKHR SharedWindowSurface::Acquire(
VkInstance instance, const VkAllocationCallbacks* allocator) {
std::lock_guard<std::mutex> lock(mutex_);
return allocator == nullptr && instance == instance_ ? surface_
: VK_NULL_HANDLE;
}

void SharedWindowSurface::Store(VkInstance instance, VkSurfaceKHR surface,
const VkAllocationCallbacks* allocator) {
std::lock_guard<std::mutex> lock(mutex_);
if (allocator == nullptr && surface_ == VK_NULL_HANDLE) {
instance_ = instance;
surface_ = surface;
}
}

bool SharedWindowSurface::Holds(VkSurfaceKHR surface) {
std::lock_guard<std::mutex> lock(mutex_);
return surface != VK_NULL_HANDLE && surface == surface_;
}

VkSurfaceKHR SharedWindowSurface::Release(VkInstance instance) {
std::lock_guard<std::mutex> lock(mutex_);
if (instance != instance_) {
return VK_NULL_HANDLE;
}
instance_ = VK_NULL_HANDLE;
return std::exchange(surface_, VK_NULL_HANDLE);
}

void SwapchainRetirement::Created(VkSwapchainKHR swapchain,
VkSurfaceKHR surface,
VkSwapchainKHR old_swapchain,
Expand Down
39 changes: 39 additions & 0 deletions src/runtime/roblox_experience_composition.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,9 @@ void RobloxExperienceComposition::NotifyLuaAppDidReturn() {
Status RobloxExperienceComposition::DrainLaunchRequests() {
Status completed_result = Status::Ok();
if (launch_worker_.joinable()) {
std::optional<GameSurface> deferred_surface;
uint64_t launch_generation = 0;
std::shared_ptr<RobloxFreshGameLaunchController> launch_controller;
const OwnedPthreadWaitResult wait = launch_worker_.WaitFor(0, 1);
if (wait.status == OwnedPthreadWaitStatus::kTimedOut)
return Status::Ok();
Expand All @@ -1417,6 +1420,10 @@ Status RobloxExperienceComposition::DrainLaunchRequests() {
launch_in_progress_ = false;
if (active_launch_ != nullptr) {
completed_result = active_launch_->result;
launch_generation = active_launch_->context.game_surface.generation;
deferred_surface = std::exchange(surface_changed_during_launch_,
std::nullopt);
launch_controller = controller_;
game_active_ = completed_result.ok();
window::SetGameSessionActive(game_active_);
active_game_canonical_json_ =
Expand All @@ -1430,6 +1437,11 @@ Status RobloxExperienceComposition::DrainLaunchRequests() {
playing_presence_published_ = false;
}
}
if (deferred_surface.has_value()) {
ApplySurfaceChangedDuringLaunch(*deferred_surface, launch_generation,
completed_result.ok(),
launch_controller);
}
if (!completed_result.ok()) {
NotifyPresence(RobloxExperiencePresencePhase::kBrowsing, nullptr);
return completed_result;
Expand Down Expand Up @@ -1678,6 +1690,11 @@ GameSessionUpdateResult RobloxExperienceComposition::SurfaceChanged(
std::lock_guard<std::mutex> lock(mutex_);
controller = controller_;
forward = game_active_;
if (!forward && (launch_in_progress_ || active_launch_ != nullptr)) {
readiness_.surface = surface;
surface_changed_during_launch_ = surface;
return AcceptedLuaAppSurface("surface kept for the game being launched");
}
recreate_lua_app = !forward && lua_app_surface_recreation_pending_;
}
GameSessionUpdateResult result =
Expand All @@ -1701,6 +1718,28 @@ GameSessionUpdateResult RobloxExperienceComposition::SurfaceChanged(
return result;
}

void RobloxExperienceComposition::ApplySurfaceChangedDuringLaunch(
const GameSurface& surface, uint64_t launch_generation, bool game_started,
const std::shared_ptr<RobloxFreshGameLaunchController>& controller) {
std::lock_guard<std::mutex> operation_lock(surface_operation_mutex_);
if (!game_started) {
(void)RebindLuaAppSurface(surface);
return;
}
if (controller == nullptr) {
return;
}
if (surface.generation != launch_generation) {
(void)controller->SurfaceCreated(surface.generation);
}
const GameSessionUpdateResult result = controller->RebindSurface(surface);
std::fprintf(stderr,
" [surface] game got the surface from its launch generation=%llu "
"pixels=%ux%u ok=%d\n",
static_cast<unsigned long long>(surface.generation),
surface.width, surface.height, result.ok() ? 1 : 0);
}

GameSessionUpdateResult RobloxExperienceComposition::SurfaceDestroyed(
uint64_t generation) {
std::lock_guard<std::mutex> operation_lock(surface_operation_mutex_);
Expand Down
73 changes: 73 additions & 0 deletions tests/roblox_experience_composition_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,79 @@ TEST(RobloxExperienceCompositionTest,
g_probe = nullptr;
}

// Roblox is in the UGCGame stage during StartGame; a LuaApp surface update
// then restarts the LuaApp and abandons the join.
TEST(RobloxExperienceCompositionTest,
ResizeDuringLaunchGoesToTheGameNotTheLuaApp) {
jnivm::VM vm;
Probe probe{&vm};
probe.publish_during_subscribe = true;
probe.block_start.store(true);
g_probe = &probe;
for (const char* class_name : {
"com/roblox/engine/jni/NativeGLInterface",
"android/view/Surface",
"com/roblox/engine/jni/model/PlatformParams",
"android/app/Activity",
"com/roblox/engine/jni/autovalue/AutoValue_StartGameParams",
"com/roblox/engine/jni/autovalue/StartAppParams",
"com/roblox/universalapp/messagebus/MessageBus",
"com/roblox/universalapp/messagebus/Connection",
"com/roblox/engine/jni/memstorage/MemStorage",
"com/roblox/engine/jni/memstorage/Connection",
"com/roblox/engine/jni/memstorage/Callback",
}) {
vm.RegisterClass(class_name);
}
RobloxExperienceComposition composition(
{vm.GetJavaVM(), &vm, Prepare}, {GetLaunchId, Subscribe, Disconnect},
{GetWebViewOpenId, GetWebViewHandleWindowCloseId, GetWebViewProtocolName,
GetWebViewAvailabilityId, GetWebViewMessageId, InitializeWebViewProtocol,
Subscribe, Disconnect, SetRequestHandler, ClearNativeRequestHandler,
PublishRaw, BroadcastDataModelFocus, GetWebViewMutateId,
GetWebViewCloseId, SignalWebViewJavascriptCallback,
UpdateCookieSetHandler},
BrowserServiceSymbols(), PermissionsSymbols(),
{Foreground, Start, Update, PauseGame, ResumeGame, Leave, PauseApp,
DestroyApp, UpdateApp, StartApp, CallMessagesFromMainThread},
JniFactory(&probe), {&probe, RegisterObserver, ClearObserver}, {},
nullptr, {}, {&probe, ObservePresence});
RobloxLuaAppExperienceReadiness readiness;
readiness.principal = {GameSessionPrincipalKind::kAuthenticated, 4, "42",
"https://www.roblox.com"};
readiness.surface = {7, 0x1234, 1280, 720};
readiness.username = "typed-user";

ASSERT_TRUE(composition.InitializePlatformProtocols().ok());
ASSERT_TRUE(composition.OnLuaAppReady(readiness).ok());
ASSERT_TRUE(composition.DrainLaunchRequests().ok());
for (int attempt = 0; attempt < 1000 && !probe.start_entered.load();
++attempt) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
ASSERT_TRUE(probe.start_entered.load());
const int app_updates = probe.app_updates;
const int app_starts = probe.app_starts;
const int game_updates = probe.updates;

EXPECT_TRUE(composition.SurfaceChanged({7, 0x1234, 1400, 872}).ok());
EXPECT_EQ(probe.app_updates, app_updates);
EXPECT_EQ(probe.app_starts, app_starts);

probe.release_start.store(true);
DrainUntilPresented(&composition);
for (int attempt = 0; attempt < 1000 && probe.updates == game_updates;
++attempt) {
ASSERT_TRUE(composition.DrainLaunchRequests().ok());
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
EXPECT_EQ(probe.app_updates, app_updates);
EXPECT_EQ(probe.app_starts, app_starts);
EXPECT_GT(probe.updates, game_updates);
EXPECT_TRUE(composition.Shutdown().ok());
g_probe = nullptr;
}

TEST(RobloxExperienceCompositionTest,
RejectsIncompleteReadinessBeforeSubscribe) {
jnivm::VM vm;
Expand Down
41 changes: 41 additions & 0 deletions tests/swapchain_retirement_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace {

using mocktail::graphics::HeldSwapchain;
using mocktail::graphics::KeptSurface;
using mocktail::graphics::SharedWindowSurface;
using mocktail::graphics::SwapchainRetirement;
using mocktail::graphics::SwapchainShape;

Expand Down Expand Up @@ -179,6 +180,46 @@ TEST(SurfaceLifetimeTest, ASurfaceWaitsForEverySwapchainOnIt) {
EXPECT_EQ(retirement.SwapchainGone(kSurface).surface, kSurface);
}

// NVIDIA's Wayland driver crashes on present once a second VkSurfaceKHR exists
// for the window, so every game surface is the one host surface.
TEST(SharedWindowSurfaceTest, EveryCreateGetsTheFirstSurfaceBack) {
SharedWindowSurface shared;
EXPECT_EQ(shared.Acquire(kInstance, nullptr), VK_NULL_HANDLE);
shared.Store(kInstance, kSurface, nullptr);
EXPECT_EQ(shared.Acquire(kInstance, nullptr), kSurface);
EXPECT_EQ(shared.Acquire(kInstance, nullptr), kSurface);
}

TEST(SharedWindowSurfaceTest, TheGamesDestroyKeepsIt) {
SharedWindowSurface shared;
shared.Store(kInstance, kSurface, nullptr);
EXPECT_TRUE(shared.Holds(kSurface));
EXPECT_FALSE(shared.Holds(kOtherSurface));
EXPECT_EQ(shared.Acquire(kInstance, nullptr), kSurface);
}

TEST(SharedWindowSurfaceTest, ItGoesWithItsInstance) {
SharedWindowSurface shared;
shared.Store(kInstance, kSurface, nullptr);
EXPECT_EQ(shared.Release(Fake<VkInstance>(0x41)), VK_NULL_HANDLE);
EXPECT_EQ(shared.Release(kInstance), kSurface);
EXPECT_FALSE(shared.Holds(kSurface));
EXPECT_EQ(shared.Acquire(kInstance, nullptr), VK_NULL_HANDLE);
}

TEST(SharedWindowSurfaceTest, OnlyOneInstanceAndNoCustomAllocator) {
SharedWindowSurface shared;
VkAllocationCallbacks callbacks{};
shared.Store(kInstance, kSurface, &callbacks);
EXPECT_EQ(shared.Acquire(kInstance, nullptr), VK_NULL_HANDLE);

shared.Store(kInstance, kSurface, nullptr);
shared.Store(Fake<VkInstance>(0x41), kOtherSurface, nullptr);
EXPECT_EQ(shared.Acquire(Fake<VkInstance>(0x41), nullptr), VK_NULL_HANDLE);
EXPECT_EQ(shared.Acquire(kInstance, &callbacks), VK_NULL_HANDLE);
EXPECT_EQ(shared.Acquire(kInstance, nullptr), kSurface);
}

TEST(SwapchainShapeTest, ChainedOrSharedCreatesAreNeverReused) {
VkSwapchainCreateInfoKHR info{};
info.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
Expand Down