[Feature] (P14): state ownership - multi-session, multi-context, share groups

Breaks the one process = one server = one session = one context equation
that the whole stack's correctness used to rest on. Design and per-slice
shape: docs/Disaggregated/design/11-state-ownership.md.

S1 wire: bind_context ring record (opcode 84) announces the current
context at the make-current edge; CreateContext/DestroyContext join the
SurfaceOp control frames; control revision 5 -> 6; the server keeps a
per-session token table and refuses unknown tokens by name.

S2 server: SessionRuntime per session with thread-scoped resolution
(singletons become fallbacks), per-thread gPipeInputs, per-session latch
domains, per-thread segment resolvers; the in-process unix supervisor
serves up to MOBILEGL_IPC_INPROC_MAX_SESSIONS (16) concurrent sessions;
surface mode is declared per session and the display lease refuses a
second on-screen session by name.

S3 frontend: ShareGroupState owns the shared object stores and name
generators (buffer/texture/renderbuffer/sampler/program), GLContext keeps
per-context bindings/errors/container objects; pGLContext becomes
thread-local with a process default context preserving the single-context
world verbatim; eglCreateContext builds a real GLContext and joins the
sharing context's group.

S4 backend: native EGL tuples keyed by (session, context token);
InitDisplayAndContext's unconditional teardown is gone - creating a
second context or surface no longer destroys the first; eglCreateContext
sharing is wired to the native driver; bind_context is the native switch
point.

S5 applier: MGPipeApplierState splits into MGPipeObjectRecords (per
share group) and MGPipeWorkingState (per context), resolved through a
{session, token} registry with a thread-local cached fast path.

Tests: MultiSessionTest (incl. two sessions x two contexts interleaved
with real clear/readback witnesses), ApplierOwnershipTest (with a
null-resolver negative control), ContextStateOwnershipTest,
ServerLoopEglTest native-context liveness; 99/99 of the acceptance set
green on the host build.

Known debts (documented in the design doc): twin registries still keyed
by handle alone, no per-record ContextSlot, process-global backend
generations, MakeCurrent dedup per current binding.
This commit is contained in:
2026-10-01 19:16:41 -04:00
parent ebc552a048
commit 4ed1c9cef2
80 changed files with 4677 additions and 949 deletions
+47 -4
View File
@@ -16,6 +16,7 @@
#include "ProgramState/ProgramState.h"
#include "ProgramState/ProgramPipelineObject.h"
#include "SamplerState/SamplerState.h"
#include "ShareGroupState.h"
#include "TextureState/TextureState.h"
#include "FramebufferState/FramebufferState.h"
#include "VertexArrayState/VertexArrayState.h"
@@ -76,7 +77,19 @@ namespace MobileGL {
class GLContext {
public:
// A context with a share group of its own. Created through EGL, a context
// either starts a new group or joins the one its share context belongs to;
// this constructor is the standalone shape the tests and the process default
// context use.
GLContext();
explicit GLContext(SharedPtr<ShareGroupState> shareGroup);
// The GL objects this context shares with every context that shares with it.
const SharedPtr<ShareGroupState>& GetShareGroup() const { return m_shareGroup; }
// Installs the process-wide default framebuffer (GL name 0) into this context:
// its table entry and the Draw/Read bindings the context starts with.
void InstallDefaultFramebuffer(const SharedPtr<FramebufferObject>& framebuffer);
// Error
void RecordError(ErrorCode code, UniquePtr<ErrorInfo> info);
@@ -188,7 +201,7 @@ namespace MobileGL {
// P1 stage 6: the per-context index of adoptable compile nodes, for its
// adoption counter. Diagnostics and tests only - no GL entry point reads it.
ShaderCompileAdoptionMap& GetShaderCompileAdoptionMap() {
return m_programState.GetShaderCompileAdoptionMap();
return Group().Programs().GetShaderCompileAdoptionMap();
}
void UseProgram(Uint program);
const SharedPtr<ProgramObject>& GetCurrentProgram();
@@ -606,6 +619,10 @@ namespace MobileGL {
void InvalidateCompileEnv();
private:
ShareGroupState& Group() const { return *m_shareGroup; }
// The GL objects shared with every context that shares with this one.
SharedPtr<ShareGroupState> m_shareGroup;
// State Components
ErrorState m_errorState;
BufferState m_bufferState;
@@ -685,10 +702,11 @@ namespace MobileGL {
IndexGenerator<Uint> m_programPipelineNames;
Uint m_boundProgramPipeline = 0;
TextureState m_textureState;
ProgramState m_programState;
// The program this context has in use. Program STATE (the objects) belongs to
// the share group; which of them is current does not.
SharedPtr<ProgramObject> m_currentProgram;
RenderState m_renderState;
FramebufferState m_framebufferState;
SamplerState m_samplerState;
RenderbufferState m_renderbufferState;
mutable SharedPtr<const MG_Util::ShaderTranspiler::CompileEnv> m_compileEnv;
@@ -698,7 +716,32 @@ namespace MobileGL {
};
} // namespace GLState
extern UniquePtr<GLState::GLContext>& pGLContext;
// P14 S3 (docs/Disaggregated/design/11-state-ownership.md): the GL state the CALLING
// THREAD's GL entry points read and write. eglMakeCurrent points it at that EGL
// context's GLContext, and the EGL release edge puts it back to whatever it was, so a
// thread that never crossed an eglMakeCurrent keeps the process default MG_State::Init()
// gave it. NULL on a thread for which neither has happened, the same answer a process
// that never reached MG_State::Init() (the spawn server image) has always given.
extern thread_local SharedPtr<GLState::GLContext> pGLContext;
// The fallback pGLContext starts at and an EGL release edge restores to: the process's
// one context for every thread that never bound an EGL context to itself, created on
// demand and owned here (leak-at-exit, like every other process global).
const SharedPtr<GLState::GLContext>& ProcessDefaultGLContext();
// Drops the process default context and this thread's binding to it, and the default
// framebuffer with them. Called on the teardown edge (the last eglTerminate, before
// MobileGL::Destroy()) so the objects MG_State::Init() built do not outlive
// glslang::FinalizeProcess() through a thread that kept its fallback binding.
void ReleaseProcessDefaultGLContext();
// P14 S3. GL framebuffer 0, built by MG_Impl once the backend is up (it needs texture
// objects, which need the backend's resource plumbing) and handed back here so every
// GLContext a later eglCreateContext builds starts with the same default framebuffer
// bound. Until the backend grows per-context native tuples there is exactly one such
// object, which is what the backend's own default-framebuffer identity tests assume.
void SetDefaultFramebuffer(SharedPtr<GLState::FramebufferObject> framebuffer);
const SharedPtr<GLState::FramebufferObject>& DefaultFramebuffer();
// True when relaxed GL semantics apply. Strict core rules are enforced only when the
// current EGL context explicitly requested a core profile (core bit in