[Fix] (P12): publish the server window's extent from the display on every resize, at once, and let eglQuerySurface take it in

This commit is contained in:
2026-10-02 12:03:52 -04:00
parent 16b07cf4b5
commit 258b946086
21 changed files with 392 additions and 89 deletions
+20 -2
View File
@@ -100,12 +100,18 @@ namespace MobileGL::MG_Remote::Server {
if (window == m_window) {
// surfaceChanged: the same window at a new extent. This is also how a geometry request
// is answered, so a waiting AcquireFor is woken.
const Bool resized = width != m_width || height != m_height;
m_width = width;
m_height = height;
NoteExtentReportLocked(width, height);
// The session rendering into it publishes the new extent to its client NOW - not at its
// next present, which a compositor with nothing to redraw never makes.
const Bool told = resized && m_leaseHolder != nullptr && m_onResized != nullptr;
if (told) m_onResized(m_leaseHolder);
lock.unlock();
m_cv.notify_all();
MGLOG_I("MG_Remote server: server window %p is now %ux%u", window, width, height);
MGLOG_I("MG_Remote server: server window %p is now %ux%u%s", window, width, height,
told ? "; its session is told" : "");
return;
}
if (m_window != nullptr) {
@@ -184,7 +190,8 @@ namespace MobileGL::MG_Remote::Server {
ServerWindowAcquire ServerDisplay::AcquireFor(Uint32 width, Uint32 height, Uint32 timeoutMs, void* holder,
ServerWindowLostHook onLost, ServerWindowWaitCancel cancel,
void* cancelUser, ServerWindowLease* out) {
void* cancelUser, ServerWindowLease* out,
ServerWindowResizedHook onResized) {
ServerDisplayHooks hooks{};
Uint64 interrupts = 0;
const Bool sizeRequested = width != 0 && height != 0;
@@ -240,6 +247,7 @@ namespace MobileGL::MG_Remote::Server {
if (sizeOk || now >= geometryDeadline) {
m_leaseHolder = holder;
m_onLost = onLost;
m_onResized = onResized;
m_lostRequested = false;
// A suspended holder that leased a window again waits for no other.
m_suspended.erase(std::remove_if(m_suspended.begin(), m_suspended.end(),
@@ -281,6 +289,7 @@ namespace MobileGL::MG_Remote::Server {
if (holder == nullptr || m_leaseHolder != holder) return false;
m_leaseHolder = nullptr;
m_onLost = nullptr;
m_onResized = nullptr;
m_lostRequested = false;
releases->swap(m_releaseAfterLease);
*hooks = m_hooks;
@@ -338,6 +347,15 @@ namespace MobileGL::MG_Remote::Server {
m_suspended.end());
}
Bool ServerDisplay::WindowExtent(void* holder, void** window, Uint32* width, Uint32* height) const {
const std::lock_guard<std::mutex> lock(m_mutex);
if (holder == nullptr || m_leaseHolder != holder || m_window == nullptr) return false;
if (window != nullptr) *window = m_window;
if (width != nullptr) *width = m_width;
if (height != nullptr) *height = m_height;
return true;
}
Uint32 ServerDisplay::SuspendedHolders() const {
const std::lock_guard<std::mutex> lock(m_mutex);
return static_cast<Uint32>(m_suspended.size());
+12 -2
View File
@@ -72,6 +72,9 @@ namespace MobileGL::MG_Remote::Server {
// A new window was attached while `holder` was suspended (SuspendLease). Same rules as the lost
// hook: called with the display's lock held.
using ServerWindowAttachedHook = void (*)(void* holder);
// The leased window changed size (surfaceChanged of the SAME window: the app's layout moved it).
// Same rules: called with the display's lock held; the holder reads the new extent with WindowExtent.
using ServerWindowResizedHook = void (*)(void* holder);
// Asked about every 50 ms while AcquireFor waits; true ends the wait (Cancelled).
using ServerWindowWaitCancel = Bool (*)(void* user);
@@ -139,10 +142,16 @@ namespace MobileGL::MG_Remote::Server {
// The apply thread's side. Requests the geometry (w, h; 0/0 = the window's own), then waits
// until a window is attached - and, for a requested size, has reached it or the geometry
// grace ran out - and leases it to `holder`, whose `onLost` Detach will call. The same holder
// may lease again while it holds the lease (a surface re-created at a new size).
// may lease again while it holds the lease (a surface re-created at a new size). `onResized`, when
// given, is called whenever the leased window changes size while the lease lasts.
ServerWindowAcquire AcquireFor(Uint32 width, Uint32 height, Uint32 timeoutMs, void* holder,
ServerWindowLostHook onLost, ServerWindowWaitCancel cancel,
void* cancelUser, ServerWindowLease* out);
void* cancelUser, ServerWindowLease* out,
ServerWindowResizedHook onResized = nullptr);
// THE WINDOW'S EXTENT AS THE PLATFORM LAST REPORTED IT (surfaceCreated / surfaceChanged): the one
// source of truth for the size a session publishes to its client. False unless `holder` holds
// the lease on an attached window.
Bool WindowExtent(void* holder, void** window, Uint32* width, Uint32* height) const;
// The holder has released everything that referenced the window (its backend surface, or the
// whole backend). Wakes a waiting Detach; releases a reference Detach left behind on timeout.
// A holder that holds no lease is a no-op.
@@ -196,6 +205,7 @@ namespace MobileGL::MG_Remote::Server {
Uint64 m_extentReports = 0;
void* m_leaseHolder = nullptr;
ServerWindowLostHook m_onLost = nullptr;
ServerWindowResizedHook m_onResized = nullptr;
Bool m_lostRequested = false;
// References a timed-out Detach could not release yet; EndLease releases them.
std::vector<void*> m_releaseAfterLease;
+49 -6
View File
@@ -371,6 +371,9 @@ namespace MobileGL::MG_Remote::Server {
m_serverWindowsResumed.store(0, std::memory_order_release);
m_serverWindowWantWidth = 0;
m_serverWindowWantHeight = 0;
m_publishedWindowWidth = 0;
m_publishedWindowHeight = 0;
m_serverWindowResizes.store(0, std::memory_order_release);
m_testLogFloodLines = ApplyLogFloodLinesForTest();
{
const std::lock_guard<std::mutex> lock(m_exitMutex);
@@ -1748,6 +1751,15 @@ namespace MobileGL::MG_Remote::Server {
}
}
void ServerLoop::ServerWindowResizedThunk(void* self) {
auto* loop = static_cast<ServerLoop*>(self);
// As the lost hook: called under the display's lock while this loop holds the lease.
loop->m_windowRequests.fetch_or(kWindowResized, std::memory_order_acq_rel);
if (loop->m_session != nullptr && loop->m_session->DataLink() != nullptr) {
loop->m_session->DataLink()->ConsumerBell().Notify();
}
}
Bool ServerLoop::ServerWindowWaitCancelled(void* self) {
const auto* loop = static_cast<const ServerLoop*>(self);
// A window-lost request also ends the wait: this thread holds a lease on a window that is
@@ -1762,7 +1774,8 @@ namespace MobileGL::MG_Remote::Server {
ServerWindowLease local;
ServerWindowLease* const lease = out != nullptr ? out : &local;
const ServerWindowAcquire acquired = display.AcquireFor(width, height, timeoutMs, this, &ServerWindowLostThunk,
&ServerWindowWaitCancelled, this, lease);
&ServerWindowWaitCancelled, this, lease,
&ServerWindowResizedThunk);
switch (acquired) {
case ServerWindowAcquire::Acquired:
m_holdsWindowLease = true;
@@ -1841,7 +1854,7 @@ namespace MobileGL::MG_Remote::Server {
window.Height = lease.height;
// Review fix: the backend is told which window is the server's own, so its surface init
// reports the extent for this window and for no window a client named (MGPipeServerOwnedWindow).
MG_Pipe::MGPipeServerSetOwnedWindow(lease.window);
NoteServerWindowExtent(lease.window, lease.width, lease.height);
frame.ok = backend->CreateEGLWindowSurface(HandleFromToken<EGLSurface>(frame.surface), window);
if (!frame.ok) {
MGLOG_E("MG_Remote server: the backend could not create a window surface on the server window %p "
@@ -1912,6 +1925,7 @@ namespace MobileGL::MG_Remote::Server {
}
m_serverWindowWantWidth = wantWidth;
m_serverWindowWantHeight = wantHeight;
NoteServerWindowExtent(lease.window, lease.width, lease.height);
frame.ok = backend->ResizeEGLWindowSurface(HandleFromToken<EGLSurface>(frame.surface), lease.width, lease.height);
frame.width = static_cast<Int>(lease.width);
frame.height = static_cast<Int>(lease.height);
@@ -1925,7 +1939,7 @@ namespace MobileGL::MG_Remote::Server {
if (!m_holdsWindowLease) return;
m_holdsWindowLease = false;
m_leasedWindow = nullptr;
MG_Pipe::MGPipeServerSetOwnedWindow(nullptr);
NoteServerWindowExtent(nullptr, 0, 0);
ServerDisplayInstance().EndLease(this);
}
@@ -1942,6 +1956,35 @@ namespace MobileGL::MG_Remote::Server {
const Uint32 requests = m_windowRequests.exchange(0, std::memory_order_acq_rel);
if ((requests & kWindowLost) != 0) ReleaseLostServerWindow();
if ((requests & kWindowAttached) != 0 && !SessionLatched()) ResumeSuspendedServerWindow();
if ((requests & kWindowResized) != 0 && !SessionLatched()) PublishServerWindowResize();
}
void ServerLoop::NoteServerWindowExtent(void* window, Uint32 width, Uint32 height) {
MG_Pipe::MGPipeServerSetOwnedWindow(window);
MG_Pipe::MGPipeServerSetOwnedWindowExtent(width, height);
m_publishedWindowWidth = width;
m_publishedWindowHeight = height;
}
void ServerLoop::PublishServerWindowResize() {
if (!m_holdsWindowLease || m_backend == nullptr) return;
// THE DISPLAY'S EXTENT, READ NOW: the latest report wins over any a burst of them left queued.
void* window = nullptr;
Uint32 width = 0;
Uint32 height = 0;
if (!ServerDisplayInstance().WindowExtent(this, &window, &width, &height) || window != m_leasedWindow ||
width == 0 || height == 0)
return;
if (width == m_publishedWindowWidth && height == m_publishedWindowHeight) return;
const Uint32 previousWidth = m_publishedWindowWidth;
const Uint32 previousHeight = m_publishedWindowHeight;
NoteServerWindowExtent(window, width, height);
const Bool published = m_backend->ServerWindowResized(window, width, height);
if (published) m_serverWindowResizes.fetch_add(1, std::memory_order_acq_rel);
MGLOG_I("MG_Remote server: server window %p resized %ux%u -> %ux%u; %s", window, previousWidth, previousHeight,
width, height,
published ? "the new extent is published to the client"
: "the backend published nothing (no surface on the window, or it follows at its present)");
}
void ServerLoop::LatchServerWindowLost(const char* why) {
@@ -1974,7 +2017,7 @@ namespace MobileGL::MG_Remote::Server {
const Uint64 suspends = m_serverWindowsSuspended.fetch_add(1, std::memory_order_acq_rel) + 1;
m_holdsWindowLease = false;
m_leasedWindow = nullptr;
MG_Pipe::MGPipeServerSetOwnedWindow(nullptr);
NoteServerWindowExtent(nullptr, 0, 0);
MGLOG_I("MG_Remote server: server window %p lost - the on-screen session is SUSPENDED, not ended: what "
"was bound to the window was released on mgl-srv-apply, its surfaces draw into an offscreen "
"placeholder, and it resumes on the next window (suspend #%llu)",
@@ -1997,7 +2040,7 @@ namespace MobileGL::MG_Remote::Server {
const ServerWindowAcquire acquired =
ServerDisplayInstance().AcquireFor(m_serverWindowWantWidth, m_serverWindowWantHeight,
ServerDisplay::kGeometryGraceMs, this, &ServerWindowLostThunk,
&ServerWindowWaitCancelled, this, &lease);
&ServerWindowWaitCancelled, this, &lease, &ServerWindowResizedThunk);
if (acquired != ServerWindowAcquire::Acquired) {
// Still registered: the next new window wakes this again.
MGLOG_I("MG_Remote server: a suspended on-screen session did not get the new server window (%s); it "
@@ -2012,7 +2055,7 @@ namespace MobileGL::MG_Remote::Server {
Bool ServerLoop::ResumeServerWindowOnLease(const ServerWindowLease& lease) {
// Before the backend: its republished extent is the server window's (MGPipeServerOwnedWindow).
MG_Pipe::MGPipeServerSetOwnedWindow(lease.window);
NoteServerWindowExtent(lease.window, lease.width, lease.height);
if (m_backend == nullptr || !m_backend->ResumeServerWindow(lease.window, lease.width, lease.height)) {
LatchServerWindowLost("the suspended on-screen session's surfaces could not be rebuilt on the new server "
"window");
+16
View File
@@ -440,6 +440,9 @@ namespace MobileGL::MG_Remote::Server {
// a placeholder until the display attaches a window again.
Uint64 ServerWindowsSuspended() const { return m_serverWindowsSuspended.load(std::memory_order_acquire); }
Uint64 ServerWindowsResumed() const { return m_serverWindowsResumed.load(std::memory_order_acquire); }
// How many times a resize of the leased window was published to the client (the display's
// surfaceChanged, answered on the apply thread without waiting for a present).
Uint64 ServerWindowResizesPublished() const { return m_serverWindowResizes.load(std::memory_order_acquire); }
Bool ServerWindowSuspended() const { return m_serverWindowSuspended.load(std::memory_order_acquire); }
// TEST SEAM: the backend this loop owns, instead of CreateBackend's (a fake that can or cannot
@@ -484,6 +487,14 @@ namespace MobileGL::MG_Remote::Server {
static void ServerWindowLostThunk(void* self);
// ServerDisplay's attached hook for a suspended session: the same discipline as the lost one.
static void ServerWindowAttachedThunk(void* self);
// ServerDisplay's resized hook for the lease holder: the same discipline again.
static void ServerWindowResizedThunk(void* self);
// Apply thread: the leased window's new extent (the display's) is handed to the backend, which
// publishes it to the client now.
void PublishServerWindowResize();
// Apply thread: the extent a backend publishes for the server window from here on (MGPipe's), and
// what this session last handed it.
void NoteServerWindowExtent(void* window, Uint32 width, Uint32 height);
// AcquireFor's cancel predicate: the loop is stopping, or the session latched.
static Bool ServerWindowWaitCancelled(void* self);
// D4: latched at the session's first successful surface creation; reset by Start(). Per
@@ -500,6 +511,7 @@ namespace MobileGL::MG_Remote::Server {
// apply thread wakes for it.
static constexpr Uint32 kWindowLost = 1u;
static constexpr Uint32 kWindowAttached = 2u;
static constexpr Uint32 kWindowResized = 4u;
std::atomic<Uint32> m_windowRequests{0};
std::atomic<Uint64> m_serverWindowsLost{0};
// Written by the apply thread, read by tests.
@@ -510,6 +522,10 @@ namespace MobileGL::MG_Remote::Server {
// layout's), which a resume asks for again.
Uint32 m_serverWindowWantWidth = 0;
Uint32 m_serverWindowWantHeight = 0;
// Apply thread only: the leased window's extent this session last published.
Uint32 m_publishedWindowWidth = 0;
Uint32 m_publishedWindowHeight = 0;
std::atomic<Uint64> m_serverWindowResizes{0};
void ApplyThreadMain();
// Runs a posted control frame, if there is one. Returns true if it ran one.