mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-16 14:33:24 +09:00
[Feature] (gbm): the server sends each frame's dma-buf fd to the container
The container's GBM hands out buffers that are the display host's frames, and this is the phone end of that: the server takes a frame from the host, offers its descriptor to the container over a unix socket at a path both sides can reach, and takes the release back when the compositor is done with it. The channel is optional and silent by design. A compositor must render whether or not a container is listening, and the render path must never wait on a socket - so it is opened lazily, every send is bounded, and any error simply marks it down.
This commit is contained in:
@@ -437,6 +437,7 @@ set(SOURCE_FILES
|
||||
MobileGL/MG_Backend/BackendObject.cpp
|
||||
|
||||
MobileGL/MG_Backend/HostFrameBridge.cpp
|
||||
MobileGL/MG_Backend/GbmFrameChannel.cpp
|
||||
|
||||
MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp
|
||||
MobileGL/MG_Backend/DirectGLES/BackendObject_DirectGLES.cpp
|
||||
|
||||
@@ -0,0 +1,568 @@
|
||||
// MobileGL - MobileGL/MG_Backend/GbmFrameChannel.cpp
|
||||
// Copyright (c) 2025-2026 MobileGL-Dev
|
||||
// Licensed under the GNU Lesser General Public License v3.0:
|
||||
// https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
// https://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// End of Source File Header
|
||||
|
||||
#include "GbmFrameChannel.h"
|
||||
|
||||
#if MOBILEGL_BUILD_DISAGGREGATED && defined(__ANDROID__)
|
||||
|
||||
#include <android/hardware_buffer.h>
|
||||
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <poll.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/un.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// WHAT THIS FILE IS WRITTEN AGAINST, AND WHAT HAS NOT BEEN RUN.
|
||||
//
|
||||
// The other end exists and is tested on this machine: MobileGL/MG_Gbm/GbmHostChannel.cpp dials
|
||||
// this socket with SOCK_SEQPACKET, reads one message per frame, computes that message's expected
|
||||
// length from the handle's own two counts, and refuses any other length. Every byte below is
|
||||
// written to that parser. There is no phone attached to the machine this was written on, so the
|
||||
// file is compile-verified for aarch64 Android and nothing more: the first run on a device is what
|
||||
// will say whether the relay in RelayHandle() reads back the handle's bytes exactly as
|
||||
// AHardwareBuffer_sendHandleToUnixSocket writes them, which is why that path is written to
|
||||
// survive both layouts the platform's serializer could have (see the note there).
|
||||
|
||||
namespace MobileGL::MG_Backend {
|
||||
namespace {
|
||||
// THE LAYOUT IS A PROTOCOL, SO ITS SIZES ARE ASSERTED AND NOT ASSUMED. The container
|
||||
// derives the length of a frame message from these three sizes plus the handle's counts,
|
||||
// so a field added here, or the packing attribute dropped, would not be a compile error
|
||||
// over there - it would be every frame skipped with a message about its length.
|
||||
static_assert(sizeof(GbmFrameOffer) == 10 * sizeof(Uint32), "the offer is ten 32 bit fields on the wire");
|
||||
static_assert(sizeof(GbmFrameRelease) == 3 * sizeof(Uint32), "the release is three 32 bit fields on the wire");
|
||||
static_assert(sizeof(GbmNativeHandleHeader) == 3 * sizeof(Int32),
|
||||
"a native handle begins with a version and two counts, on every Android ABI");
|
||||
|
||||
// The container reads one message per frame into a 4 KiB buffer and skips a message that
|
||||
// does not fit in it (GbmHostMessageMax, GbmHostChannel.cpp), so a message larger than this
|
||||
// is refused here rather than built and dropped there.
|
||||
constexpr SizeT kMessageMax = 4096;
|
||||
// The descriptors one handle may carry, which is the container's own cap (GbmHostMaxFds).
|
||||
// Every AHardwareBuffer handle this project has seen carries one - the dma-buf - and both
|
||||
// ends refuse a frame whose handle names more than they can take rather than forward a
|
||||
// truncated list into a buffer of unknown memory.
|
||||
constexpr Int kMaxHandleFds = 8;
|
||||
// How many release messages one call reads before it returns to the frame. A cap and not a
|
||||
// loop to exhaustion: the messages left behind stay in the socket, whose buffer the kernel
|
||||
// bounds, and the next frame drains them.
|
||||
constexpr Int kDrainMax = 64;
|
||||
// How long the relay read waits for bytes the send one statement earlier has already put in
|
||||
// the socket. It exists only so that an invariant broken somewhere else cannot park the
|
||||
// apply thread; on a local socketpair the wait is zero.
|
||||
constexpr Int kRelayWaitMs = 2;
|
||||
|
||||
// close() may overwrite the errno a caller is in the middle of reporting, and every caller
|
||||
// here is reporting a failure errno describes.
|
||||
void CloseQuietly(Int fd) {
|
||||
if (fd < 0) return;
|
||||
const Int savedErrno = errno;
|
||||
::close(fd);
|
||||
errno = savedErrno;
|
||||
}
|
||||
}
|
||||
|
||||
GbmFrameChannel::~GbmFrameChannel() { Close(); }
|
||||
|
||||
Bool GbmFrameChannel::Open() {
|
||||
if (m_listen >= 0) return true;
|
||||
|
||||
const char* configured = ::getenv(kGbmFrameSocketEnv);
|
||||
const char* path = (configured != nullptr && configured[0] != 0) ? configured : kGbmFrameSocketPath;
|
||||
constexpr SizeT pathMax = sizeof(((struct sockaddr_un*)nullptr)->sun_path);
|
||||
if (std::strlen(path) >= pathMax) {
|
||||
MGLOG_E_ONCE("gbm frame channel: the container socket path %s does not fit in sun_path (%zu bytes); no "
|
||||
"container can be served",
|
||||
path, pathMax);
|
||||
return false;
|
||||
}
|
||||
|
||||
// SOCK_SEQPACKET because that is what the container dials with (GbmHostChannel::EnsureOpen
|
||||
// asks for exactly this), and a stream listener answers it EPROTOTYPE - the container
|
||||
// prints that as "a socket of a different type". SOCK_NONBLOCK because the accept below
|
||||
// runs on the apply thread and may not wait for a container that is not there; SOCK_CLOEXEC
|
||||
// because every process this server starts would otherwise inherit a socket that receives
|
||||
// the phone's frames.
|
||||
const Int fd = ::socket(AF_UNIX, SOCK_SEQPACKET | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
|
||||
if (fd < 0) {
|
||||
MGLOG_E_ONCE("gbm frame channel: socket(AF_UNIX, SOCK_SEQPACKET): %s; the frames are drawn and presented "
|
||||
"without a container",
|
||||
std::strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
// A node left behind by a server that died is not a reason to refuse a container: bind
|
||||
// would answer EADDRINUSE for a socket nobody is listening on.
|
||||
::unlink(path);
|
||||
|
||||
struct sockaddr_un address = {};
|
||||
address.sun_family = AF_UNIX;
|
||||
// The terminator travels with the name: sun_path is a C string for a pathname socket, and a
|
||||
// name without one is a name the kernel reads past the end of.
|
||||
std::memcpy(address.sun_path, path, std::strlen(path) + 1);
|
||||
if (::bind(fd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)) != 0) {
|
||||
MGLOG_E_ONCE("gbm frame channel: bind(%s): %s; the frames are drawn and presented without a container",
|
||||
path, std::strerror(errno));
|
||||
CloseQuietly(fd);
|
||||
return false;
|
||||
}
|
||||
// connect() to a pathname socket needs write permission on the node, and the container is
|
||||
// not this process's user, so the umask's usual 0755 would leave it refused for a reason it
|
||||
// could not see. 0666 is what makes the node reachable by the other half of a split run; a
|
||||
// chmod that fails is not fatal, because a peer running as this uid still dials it.
|
||||
if (::chmod(path, 0666) != 0) {
|
||||
MGLOG_W_ONCE("gbm frame channel: chmod(%s, 0666): %s; a container running as another user cannot dial "
|
||||
"this socket",
|
||||
path, std::strerror(errno));
|
||||
}
|
||||
if (::listen(fd, 1) != 0) {
|
||||
MGLOG_E_ONCE("gbm frame channel: listen(%s): %s; the frames are drawn and presented without a container",
|
||||
path, std::strerror(errno));
|
||||
::unlink(path);
|
||||
CloseQuietly(fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
m_listen = fd;
|
||||
m_path = path;
|
||||
MGLOG_I("gbm frame channel: listening on %s for one container (SOCK_SEQPACKET, one message per frame)", path);
|
||||
return true;
|
||||
}
|
||||
|
||||
Bool GbmFrameChannel::EnsureRelay() {
|
||||
if (m_relay[0] >= 0 && m_relay[1] >= 0) return true;
|
||||
// Both ends are dropped before a new pair is made: half a socketpair is a socket with bytes
|
||||
// in it that nothing will ever read, and the next handle read from it would be the tail of
|
||||
// the last one.
|
||||
DropRelay();
|
||||
Int pair[2] = {-1, -1};
|
||||
if (::socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0, pair) != 0) {
|
||||
MGLOG_E_ONCE("gbm frame channel: socketpair for the handle relay: %s; no frame can be offered until one "
|
||||
"can be made",
|
||||
std::strerror(errno));
|
||||
return false;
|
||||
}
|
||||
m_relay[0] = pair[0];
|
||||
m_relay[1] = pair[1];
|
||||
return true;
|
||||
}
|
||||
|
||||
void GbmFrameChannel::DropRelay() {
|
||||
CloseQuietly(m_relay[0]);
|
||||
CloseQuietly(m_relay[1]);
|
||||
m_relay[0] = -1;
|
||||
m_relay[1] = -1;
|
||||
}
|
||||
|
||||
Bool GbmFrameChannel::RelayHandle(struct AHardwareBuffer* buffer, Uint8* payload, SizeT capacity, SizeT& payloadBytes,
|
||||
Int* fds, Int& fdCount) {
|
||||
payloadBytes = 0;
|
||||
fdCount = 0;
|
||||
if (!EnsureRelay()) return false;
|
||||
|
||||
// WHY A FRAME'S HANDLE GOES THROUGH A SOCKETPAIR OF THIS CHANNEL'S OWN.
|
||||
//
|
||||
// The container takes ONE message per frame - the offer, then the handle's counts, then the
|
||||
// arrays those counts describe, with the descriptors in that same message's SCM_RIGHTS
|
||||
// control data - and it refuses a message of any other length. Two sends cannot be one
|
||||
// message on a message-oriented socket, so AHardwareBuffer_sendHandleToUnixSocket, which
|
||||
// sends a handle as a message of its own, is pointed at a socketpair this channel owns, and
|
||||
// what comes out of the other end is both halves of what the container wants: the handle's
|
||||
// own bytes, read here, and its descriptors, as SCM_RIGHTS.
|
||||
//
|
||||
// The alternative does not exist: the NDK's <android/hardware_buffer.h> declares no way to
|
||||
// ask a buffer for its descriptors. AHardwareBuffer_getNativeHandle is a platform API and
|
||||
// is absent from the NDK's copy of that header (checked against NDK 27.3.13750724 in this
|
||||
// tree's build-termux toolchain), so this is the one route from an AHardwareBuffer to a
|
||||
// dma-buf descriptor that an app can take.
|
||||
const Int code = AHardwareBuffer_sendHandleToUnixSocket(buffer, m_relay[0]);
|
||||
if (code != 0) {
|
||||
MGLOG_W_ONCE("gbm frame channel: the frame's handle did not cross the relay socket (rc %d, errno %d); the "
|
||||
"frame is not offered to the container",
|
||||
code, errno);
|
||||
// The relay is dropped rather than kept: a send that failed part way through would
|
||||
// leave bytes that the next handle's read would take for its own header.
|
||||
DropRelay();
|
||||
return false;
|
||||
}
|
||||
|
||||
struct iovec vector = {};
|
||||
vector.iov_base = payload;
|
||||
vector.iov_len = capacity;
|
||||
Uint8 control[CMSG_SPACE(sizeof(Int) * (SizeT)kMaxHandleFds)] = {};
|
||||
struct msghdr message = {};
|
||||
message.msg_iov = &vector;
|
||||
message.msg_iovlen = 1;
|
||||
message.msg_control = control;
|
||||
message.msg_controllen = sizeof(control);
|
||||
|
||||
// MSG_DONTWAIT for the promise at the top of this file, and MSG_CMSG_CLOEXEC so a descriptor
|
||||
// read out of the relay does not survive into a child of this process, which would then hold
|
||||
// the phone's frame until it died.
|
||||
ssize_t got = ::recvmsg(m_relay[1], &message, MSG_DONTWAIT | MSG_CMSG_CLOEXEC);
|
||||
if (got < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
|
||||
struct pollfd waiting = {};
|
||||
waiting.fd = m_relay[1];
|
||||
waiting.events = POLLIN;
|
||||
if (::poll(&waiting, 1, kRelayWaitMs) > 0) {
|
||||
// The kernel rewrites these on every call, so a retry starts from the same state as
|
||||
// the first attempt.
|
||||
message.msg_controllen = sizeof(control);
|
||||
message.msg_flags = 0;
|
||||
got = ::recvmsg(m_relay[1], &message, MSG_DONTWAIT | MSG_CMSG_CLOEXEC);
|
||||
}
|
||||
}
|
||||
if (got < 0) {
|
||||
MGLOG_W_ONCE("gbm frame channel: a relayed handle could not be read back (%d: %s); the frame is not "
|
||||
"offered to the container",
|
||||
errno, std::strerror(errno));
|
||||
DropRelay();
|
||||
return false;
|
||||
}
|
||||
if (got == 0) {
|
||||
// Zero bytes on a connected socket is the end of it. Only this process holds either end
|
||||
// of this socketpair, so this is a bug being reported rather than a peer going away.
|
||||
MGLOG_W_ONCE("gbm frame channel: the handle relay is closed; the frame is not offered to the container");
|
||||
DropRelay();
|
||||
return false;
|
||||
}
|
||||
|
||||
// The descriptors come out of the control data before anything is decided about the
|
||||
// message, because they belong to this process from the moment they were received: a path
|
||||
// that returns without closing them leaks a descriptor per frame.
|
||||
Int collected = 0;
|
||||
for (struct cmsghdr* header = CMSG_FIRSTHDR(&message); header != nullptr;
|
||||
header = CMSG_NXTHDR(&message, header)) {
|
||||
if (header->cmsg_level != SOL_SOCKET || header->cmsg_type != SCM_RIGHTS) continue;
|
||||
const SizeT bytes = (SizeT)header->cmsg_len - CMSG_LEN(0);
|
||||
const Int count = (Int)(bytes / sizeof(Int));
|
||||
const Int* descriptors = reinterpret_cast<const Int*>(CMSG_DATA(header));
|
||||
for (Int i = 0; i < count; ++i) {
|
||||
if (collected < kMaxHandleFds) {
|
||||
fds[collected++] = descriptors[i];
|
||||
} else {
|
||||
// More descriptors than a frame can carry: they are this process's now, so they
|
||||
// are closed here rather than leaked.
|
||||
CloseQuietly(descriptors[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ((message.msg_flags & MSG_CTRUNC) != 0) {
|
||||
MGLOG_W_ONCE("gbm frame channel: a frame's control data did not fit in %zu bytes; the frame is not "
|
||||
"offered because its descriptor list is incomplete",
|
||||
sizeof(control));
|
||||
for (Int i = 0; i < collected; ++i) CloseQuietly(fds[i]);
|
||||
DropRelay();
|
||||
return false;
|
||||
}
|
||||
if ((message.msg_flags & MSG_TRUNC) != 0 || (SizeT)got < sizeof(GbmNativeHandleHeader)) {
|
||||
// A message longer than a handle can be, or shorter than the three counts every handle
|
||||
// begins with. Either way it cannot describe a frame, and a relay that has been read
|
||||
// past the end of one message must not carry the next frame's header: it is recreated.
|
||||
MGLOG_W_ONCE("gbm frame channel: a relayed handle is %zd bytes and %s; the frame is not offered", got,
|
||||
((message.msg_flags & MSG_TRUNC) != 0) ? "longer than a handle can be"
|
||||
: "too short to hold its own counts");
|
||||
for (Int i = 0; i < collected; ++i) CloseQuietly(fds[i]);
|
||||
DropRelay();
|
||||
return false;
|
||||
}
|
||||
|
||||
payloadBytes = (SizeT)got;
|
||||
fdCount = collected;
|
||||
return true;
|
||||
}
|
||||
|
||||
void GbmFrameChannel::AcceptDialer() {
|
||||
if (m_listen < 0) return;
|
||||
// accept4, so the connection is close-on-exec and non-blocking from the moment it exists: a
|
||||
// container that stops reading must not be able to park a send on the apply thread.
|
||||
const Int fd = ::accept4(m_listen, nullptr, nullptr, SOCK_CLOEXEC | SOCK_NONBLOCK);
|
||||
if (fd < 0) {
|
||||
// EAGAIN is nobody dialling, which is the ordinary state of a phone whose container is
|
||||
// not running. The rest are worth one line between them: the listener outlives them and
|
||||
// the next frame tries again.
|
||||
if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR && errno != ECONNABORTED) {
|
||||
MGLOG_W_ONCE("gbm frame channel: accept on %s failed (%d: %s); a container cannot connect until the "
|
||||
"next frame tries again",
|
||||
m_path.c_str(), errno, std::strerror(errno));
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (m_client < 0) {
|
||||
m_client = fd;
|
||||
MGLOG_I("gbm frame channel: a container is connected on %s; every frame the host offers is sent to it as "
|
||||
"the dma-buf descriptor behind it",
|
||||
m_path.c_str());
|
||||
return;
|
||||
}
|
||||
// ONE CLIENT, which is what the protocol is written for: a second dialer is closed rather
|
||||
// than left connected, so that it learns it is not the one being served instead of holding a
|
||||
// connection whose messages nobody reads.
|
||||
CloseQuietly(fd);
|
||||
MGLOG_W_ONCE("gbm frame channel: a second peer dialled %s while a container is connected; it is closed and "
|
||||
"the frames stay with the connected one",
|
||||
m_path.c_str());
|
||||
}
|
||||
|
||||
void GbmFrameChannel::MarkDown(const char* what, Int error) {
|
||||
if (m_client < 0) return;
|
||||
CloseQuietly(m_client);
|
||||
m_client = -1;
|
||||
if (error != 0) {
|
||||
MGLOG_W("gbm frame channel: the container connection is down: %s failed (%d: %s); the frames are still "
|
||||
"drawn and presented, and the container stops receiving them until it dials again",
|
||||
what, error, std::strerror(error));
|
||||
} else {
|
||||
MGLOG_W("gbm frame channel: the container connection is down: %s; the frames are still drawn and "
|
||||
"presented, and the container stops receiving them until it dials again",
|
||||
what);
|
||||
}
|
||||
}
|
||||
|
||||
Bool GbmFrameChannel::OfferFrame(struct AHardwareBuffer* buffer, const HostFrameOffer& offer) {
|
||||
if (buffer == nullptr) return false;
|
||||
// Open() does nothing once the socket exists, so this is an integer test in the common case.
|
||||
Open();
|
||||
|
||||
// Releases first, and the accept after them: a container that has died is noticed and its
|
||||
// replacement taken before this frame is handed to a socket the kernel has already given up
|
||||
// on, which is what lets a compositor that restarted receive frames from the very next one
|
||||
// rather than from the frame after it.
|
||||
Poll();
|
||||
AcceptDialer();
|
||||
if (m_client < 0) return false;
|
||||
|
||||
Uint8 payload[kMessageMax];
|
||||
SizeT payloadBytes = 0;
|
||||
Int fds[kMaxHandleFds];
|
||||
for (Int i = 0; i < kMaxHandleFds; ++i) fds[i] = -1;
|
||||
Int fdCount = 0;
|
||||
if (!RelayHandle(buffer, payload, sizeof(payload), payloadBytes, fds, fdCount)) return false;
|
||||
|
||||
GbmNativeHandleHeader handle = {};
|
||||
std::memcpy(&handle, payload, sizeof(handle));
|
||||
const SizeT tail = ((SizeT)handle.NumFds + (SizeT)handle.NumInts) * sizeof(Int32);
|
||||
const SizeT messageBytes = sizeof(GbmFrameOffer) + sizeof(GbmNativeHandleHeader) + tail;
|
||||
|
||||
// EVERY REASON THIS CHANNEL REFUSES A FRAME, IN ONE PLACE. Each of them is a frame the
|
||||
// container would refuse anyway - it checks the same counts and the same geometry - and a
|
||||
// frame that is refused here costs nothing: no message, no sequence number, and no entry in
|
||||
// the count of frames waiting to be released.
|
||||
const char* refusal = nullptr;
|
||||
if (offer.Width == 0 || offer.Height == 0 || offer.StrideBytes == 0) {
|
||||
refusal = "the host described it with a width, height or stride of zero, and a buffer with no size is "
|
||||
"not one a compositor can render into";
|
||||
} else if (handle.NumFds < 1) {
|
||||
refusal = "its handle carries no descriptor, and a frame without a dma-buf is not one the container "
|
||||
"can render into";
|
||||
} else if (handle.NumFds > kMaxHandleFds) {
|
||||
refusal = "its handle carries more descriptors than either end takes";
|
||||
} else if (handle.NumInts < 0) {
|
||||
refusal = "its handle claims a negative number of integers";
|
||||
} else if ((SizeT)handle.NumInts > kMessageMax / sizeof(Int32)) {
|
||||
// This is what keeps the message's length arithmetic from wrapping on a 32 bit ABI: a
|
||||
// handle whose integer array alone is longer than a whole message cannot be carried by
|
||||
// this protocol whatever the rest of it says, and the check below would otherwise be
|
||||
// reading a number that had already turned into a small and plausible one.
|
||||
refusal = "its handle names more integers than a whole message could carry";
|
||||
} else if (fdCount < handle.NumFds) {
|
||||
refusal = "fewer descriptors arrived with it than its handle names";
|
||||
} else if (messageBytes > kMessageMax) {
|
||||
refusal = "the message it makes is longer than the container reads";
|
||||
}
|
||||
|
||||
Bool offered = false;
|
||||
if (refusal != nullptr) {
|
||||
// ONCE, not per frame: a host that describes every frame the same broken way must not
|
||||
// turn this into a line per frame on the apply thread.
|
||||
MGLOG_W_ONCE("gbm frame channel: frame %u (%ux%u) is not offered to the container: %s", offer.Index,
|
||||
offer.Width, offer.Height, refusal);
|
||||
} else {
|
||||
// One message, built whole: the offer, then the handle's own three counts, then the two
|
||||
// arrays the counts describe. What came back from the relay is copied in as far as it
|
||||
// goes and the remainder is left zero, because the container checks the LENGTH it
|
||||
// derives from those counts and never reads the arrays themselves - it takes the
|
||||
// descriptors from the control data and hands the first one to GBM. So a handle whose
|
||||
// bytes are shorter than its own counts imply is padded rather than refused, and one
|
||||
// longer than they imply is cut to what the counts say; either way the length the
|
||||
// container computes is the length it receives.
|
||||
Uint8 message[kMessageMax] = {};
|
||||
GbmFrameOffer out = {};
|
||||
out.Magic = kGbmFrameOfferMagic;
|
||||
out.Version = kGbmFrameVersion;
|
||||
out.Seq = m_nextSeq;
|
||||
out.Index = offer.Index;
|
||||
out.Width = offer.Width;
|
||||
out.Height = offer.Height;
|
||||
out.StrideBytes = offer.StrideBytes;
|
||||
out.Format = offer.Format;
|
||||
out.UsageLo = offer.UsageLo;
|
||||
out.UsageHi = offer.UsageHi;
|
||||
std::memcpy(message, &out, sizeof(out));
|
||||
const SizeT handleBytes = (payloadBytes < sizeof(GbmNativeHandleHeader) + tail)
|
||||
? payloadBytes
|
||||
: (sizeof(GbmNativeHandleHeader) + tail);
|
||||
std::memcpy(message + sizeof(out), payload, handleBytes);
|
||||
|
||||
struct iovec vector = {};
|
||||
vector.iov_base = message;
|
||||
vector.iov_len = messageBytes;
|
||||
Uint8 control[CMSG_SPACE(sizeof(Int) * (SizeT)kMaxHandleFds)] = {};
|
||||
struct msghdr outgoing = {};
|
||||
outgoing.msg_iov = &vector;
|
||||
outgoing.msg_iovlen = 1;
|
||||
outgoing.msg_control = control;
|
||||
outgoing.msg_controllen = CMSG_SPACE(sizeof(Int) * (SizeT)handle.NumFds);
|
||||
struct cmsghdr* rights = CMSG_FIRSTHDR(&outgoing);
|
||||
rights->cmsg_level = SOL_SOCKET;
|
||||
rights->cmsg_type = SCM_RIGHTS;
|
||||
rights->cmsg_len = CMSG_LEN(sizeof(Int) * (SizeT)handle.NumFds);
|
||||
std::memcpy(CMSG_DATA(rights), fds, sizeof(Int) * (SizeT)handle.NumFds);
|
||||
|
||||
// MSG_DONTWAIT for the promise at the top of this file, and MSG_NOSIGNAL because a
|
||||
// server killed by SIGPIPE when the container goes away takes the frames it was drawing
|
||||
// with it. A full socket, a reset peer and a broken pipe all say the same thing here:
|
||||
// this connection can no longer be trusted with a frame, so it is closed and the next
|
||||
// container to dial is served.
|
||||
const ssize_t sent = ::sendmsg(m_client, &outgoing, MSG_DONTWAIT | MSG_NOSIGNAL);
|
||||
if (sent != (ssize_t)messageBytes) {
|
||||
MarkDown("sendmsg", (sent < 0) ? errno : 0);
|
||||
} else {
|
||||
m_nextSeq = out.Seq + 1;
|
||||
++m_offered;
|
||||
++m_outstanding;
|
||||
offered = true;
|
||||
// The first offer is the one worth a line, and it carries the host's own numbers:
|
||||
// the format in particular is the field the two ends could disagree about without
|
||||
// either failing, so the value the host actually sends is put where a run on a
|
||||
// device can be read afterwards.
|
||||
MGLOG_I_ONCE("gbm frame channel: the first frame is offered to the container (seq %u index %u %ux%u "
|
||||
"stride %u format 0x%08x)",
|
||||
out.Seq, out.Index, out.Width, out.Height, out.StrideBytes, out.Format);
|
||||
MGLOG_D("gbm frame channel: frame seq=%u index=%u offered (%zu bytes, %d descriptor(s), %llu "
|
||||
"outstanding)",
|
||||
out.Seq, out.Index, messageBytes, handle.NumFds, (unsigned long long)m_outstanding);
|
||||
}
|
||||
}
|
||||
|
||||
// The kernel took its own copies of these when the message was sent, and one that was not
|
||||
// sent is not the container's to hold: every path closes what the relay handed over.
|
||||
for (Int i = 0; i < fdCount; ++i) CloseQuietly(fds[i]);
|
||||
return offered;
|
||||
}
|
||||
|
||||
void GbmFrameChannel::Poll() {
|
||||
if (m_client < 0) return;
|
||||
for (Int drained = 0; drained < kDrainMax; ++drained) {
|
||||
GbmFrameRelease release = {};
|
||||
struct iovec vector = {};
|
||||
vector.iov_base = &release;
|
||||
vector.iov_len = sizeof(release);
|
||||
struct msghdr message = {};
|
||||
message.msg_iov = &vector;
|
||||
message.msg_iovlen = 1;
|
||||
// recvmsg and not recv, for MSG_TRUNC: a message longer than a release can only be a
|
||||
// peer speaking something else, and a release read out of the first twelve bytes of it
|
||||
// would name a frame nobody released.
|
||||
const ssize_t got = ::recvmsg(m_client, &message, MSG_DONTWAIT);
|
||||
if (got < 0) {
|
||||
if (errno == EINTR) continue;
|
||||
// Nothing said, which is what a container that is between frames looks like.
|
||||
if (errno == EAGAIN || errno == EWOULDBLOCK) return;
|
||||
MarkDown("recvmsg", errno);
|
||||
return;
|
||||
}
|
||||
if (got == 0) {
|
||||
// Zero bytes on a connected socket is the end of it, whether the container exited or
|
||||
// the kernel gave up on it. Its replacement is taken by the next frame.
|
||||
MarkDown("the container closed the connection", 0);
|
||||
return;
|
||||
}
|
||||
if ((message.msg_flags & MSG_TRUNC) != 0 || got != (ssize_t)sizeof(release)) {
|
||||
// The message is skipped, not the connection: on a message-oriented socket a
|
||||
// message too long for the buffer is consumed whole, so the next one is still a
|
||||
// message rather than the tail of this one.
|
||||
MGLOG_W_ONCE("gbm frame channel: the container sent a message that is not a %zu byte release; it is "
|
||||
"skipped",
|
||||
sizeof(release));
|
||||
continue;
|
||||
}
|
||||
if (release.Magic != kGbmFrameReleaseMagic) {
|
||||
MGLOG_W_ONCE("gbm frame channel: a message from the container begins with 0x%08x rather than 'MGGR'; "
|
||||
"it is skipped",
|
||||
release.Magic);
|
||||
continue;
|
||||
}
|
||||
if (release.Version != kGbmFrameVersion) {
|
||||
// Refused rather than acted on: the version is what says how the rest of the message
|
||||
// is laid out, and a release read with the wrong layout names a frame the container
|
||||
// did not mean.
|
||||
MGLOG_E_ONCE("gbm frame channel: the container speaks protocol version %u where this server speaks "
|
||||
"version %u; its releases are refused until the two agree",
|
||||
release.Version, (Uint32)kGbmFrameVersion);
|
||||
continue;
|
||||
}
|
||||
++m_released;
|
||||
if (m_outstanding > 0) --m_outstanding;
|
||||
// The first release is the only one worth a line: it is the proof that the container
|
||||
// took the descriptor and its compositor retired the frame, where every later one says
|
||||
// the same thing sixty times a second.
|
||||
MGLOG_I_ONCE("gbm frame channel: the container is releasing frames (first: seq %u)", release.Seq);
|
||||
}
|
||||
// Anything past this cap stays in the socket, whose buffer the kernel bounds, and goes out
|
||||
// with the next frame: a peer that says nothing but releases may not spend the apply thread
|
||||
// here.
|
||||
}
|
||||
|
||||
void GbmFrameChannel::Close() {
|
||||
// The connection is closed quietly rather than marked down: this is a teardown, not a
|
||||
// failure, and a warning about it would be noise in a log read for the failures.
|
||||
CloseQuietly(m_client);
|
||||
m_client = -1;
|
||||
DropRelay();
|
||||
if (m_listen >= 0) {
|
||||
// The node is this channel's to remove only while it still IS this channel's socket: a
|
||||
// server that was restarted has bound the same path with a socket of its own, and a
|
||||
// teardown that unlinked it would take the successor's node away with it. st_dev and
|
||||
// st_ino against the listening descriptor is the cheapest way to ask, and a check that
|
||||
// fails only leaves a name behind that the next Open() unlinks anyway.
|
||||
struct stat node = {};
|
||||
struct stat socket = {};
|
||||
if (!m_path.empty() && ::stat(m_path.c_str(), &node) == 0 && ::fstat(m_listen, &socket) == 0 &&
|
||||
node.st_dev == socket.st_dev && node.st_ino == socket.st_ino) {
|
||||
::unlink(m_path.c_str());
|
||||
}
|
||||
CloseQuietly(m_listen);
|
||||
m_listen = -1;
|
||||
}
|
||||
m_path.clear();
|
||||
// The counters are history and stay: a caller that logs Outstanding() after a teardown is
|
||||
// asking what happened, and IsUp() is what answers what is connected now.
|
||||
}
|
||||
}
|
||||
|
||||
#else // !(MOBILEGL_BUILD_DISAGGREGATED && __ANDROID__)
|
||||
|
||||
namespace MobileGL::MG_Backend {
|
||||
// A build that is not the Android render server has no AHardwareBuffer to offer and no
|
||||
// container beside it. The class exists so that callers do not have to be compiled
|
||||
// differently, and every entry point answers the way an absent container does.
|
||||
GbmFrameChannel::~GbmFrameChannel() = default;
|
||||
Bool GbmFrameChannel::Open() { return false; }
|
||||
Bool GbmFrameChannel::OfferFrame(struct AHardwareBuffer*, const HostFrameOffer&) { return false; }
|
||||
void GbmFrameChannel::Poll() {}
|
||||
void GbmFrameChannel::Close() {}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
// End of Source File Header
|
||||
@@ -0,0 +1,171 @@
|
||||
// MobileGL - MobileGL/MG_Backend/GbmFrameChannel.h
|
||||
// Copyright (c) 2025-2026 MobileGL-Dev
|
||||
// Licensed under the GNU Lesser General Public License v3.0:
|
||||
// https://www.gnu.org/licenses/gpl-3.0.txt
|
||||
// https://www.gnu.org/licenses/lgpl-3.0.txt
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// End of Source File Header
|
||||
|
||||
#pragma once
|
||||
#include <Includes.h>
|
||||
|
||||
#include "HostFrameBridge.h"
|
||||
|
||||
struct AHardwareBuffer;
|
||||
|
||||
namespace MobileGL::MG_Backend {
|
||||
// THE FRAME THE HOST HANDED OVER, HANDED ON TO THE CONTAINER AS A dma-buf.
|
||||
//
|
||||
// The display host owns a pool of AHardwareBuffers: it allocates them, this side imports the
|
||||
// one it offers as a draw target (HostFrameBridge, DirectGLES::HostFrameTarget), and the host
|
||||
// puts it on the glass when this side answers that it is drawn. The Wayland compositor in the
|
||||
// Linux container has to render into THOSE SAME frames - it runs this project's own GBM, and a
|
||||
// gbm_bo_create there answers with the phone's frame instead of a dma-heap allocation of its
|
||||
// own - so every frame this side takes has to reach the container as that frame's dma-buf
|
||||
// descriptor. That, and the release that comes back, is all this channel carries.
|
||||
//
|
||||
// The container's end is MobileGL/MG_Gbm/GbmHostChannel.cpp. It reads exactly the bytes
|
||||
// declared below and answers with a release once its compositor no longer reads the frame.
|
||||
// The three shapes ARE that file's GbmFrameOffer/GbmFrameRelease and GbmInternal.h's
|
||||
// AHBNativeHandleHeader, field for field and packing included. They are restated here rather
|
||||
// than shared because the two ends are built into different libraries from different build
|
||||
// configurations: MG_Backend is the Android render server and never sees libgbm's headers,
|
||||
// while MG_Gbm is built for the container and never sees Android's. A change on either side
|
||||
// is a protocol change, and the magic is what makes a mismatched pair fail loudly instead of
|
||||
// being read as plausible numbers.
|
||||
//
|
||||
// ONE MESSAGE PER FRAME, on SOCK_SEQPACKET, and that is part of the protocol rather than an
|
||||
// implementation detail: an offer and its handle only mean anything together, and the message
|
||||
// boundary is what lets the reader take the two as one thing. The container derives the
|
||||
// length of the message it expects from the handle's own two counts and refuses any other
|
||||
// length, so this end sends the offer immediately followed by the handle's counts and its two
|
||||
// arrays, with the descriptors in the SCM_RIGHTS control data of that same sendmsg.
|
||||
//
|
||||
// The channel is OPTIONAL, and that is the rule the implementation is built around: a
|
||||
// container that is not running, has died, or has stopped reading may cost the render path a
|
||||
// syscall or two, and may never block it, fail it, or grow without bound. Every entry point
|
||||
// below is non-blocking and answers silence with silence.
|
||||
inline constexpr Uint32 kGbmFrameOfferMagic = 0x4647474Du; // 'M','G','G','F' in memory order
|
||||
inline constexpr Uint32 kGbmFrameReleaseMagic = 0x5247474Du; // 'M','G','G','R' in memory order
|
||||
inline constexpr Uint32 kGbmFrameVersion = 1u;
|
||||
// WHERE THE CONTAINER DIALS. /data/local/tmp is the one directory both halves of a split run
|
||||
// can be pointed at on a device, and the default lives on this end because this end is the
|
||||
// server. The container's compiled-in default is /run/mobilegl-gbm.sock - the same protocol
|
||||
// with the path each side can reach unaided - so a deployment that puts the two in different
|
||||
// mount namespaces sets MOBILEGL_GBM_SOCKET on both; it is read here and in
|
||||
// GbmHostChannelCreate, from the same variable, for that reason.
|
||||
inline constexpr const char* kGbmFrameSocketPath = "/data/local/tmp/mobilegl-gbm.sock";
|
||||
inline constexpr const char* kGbmFrameSocketEnv = "MOBILEGL_GBM_SOCKET";
|
||||
|
||||
// One frame the host offered, described for the container. Width, Height, StrideBytes and
|
||||
// Format are the host's own numbers for the frame (HostFrameOffer), carried through
|
||||
// unchanged: they describe the memory the descriptor points at, and this end is not the place
|
||||
// where a number the other two ends agreed on gets rewritten.
|
||||
struct GbmFrameOffer {
|
||||
Uint32 Magic;
|
||||
Uint32 Version;
|
||||
Uint32 Seq; // this channel's, one per frame offered
|
||||
Uint32 Index; // the host's own buffer index, echoed so the phone can match a frame
|
||||
Uint32 Width;
|
||||
Uint32 Height;
|
||||
Uint32 StrideBytes;
|
||||
Uint32 Format;
|
||||
Uint32 UsageLo;
|
||||
Uint32 UsageHi;
|
||||
} __attribute__((packed));
|
||||
|
||||
// What the container says once it is done with a frame. It may arrive late, it may never
|
||||
// arrive, and both are cheap here: the channel keeps a count and no per-frame record (see
|
||||
// Outstanding below), so nothing is leaked and nothing grows when one is missed.
|
||||
struct GbmFrameRelease {
|
||||
Uint32 Magic;
|
||||
Uint32 Version;
|
||||
Uint32 Seq;
|
||||
} __attribute__((packed));
|
||||
|
||||
// An AHardwareBuffer native handle as it appears on the wire: the platform's native_handle_t
|
||||
// (version, then the two counts, then the descriptor array followed by the integer array),
|
||||
// written by AHardwareBuffer_sendHandleToUnixSocket and read by the container's
|
||||
// AHBNativeHandleHeader. The counts are 32 bit on every Android ABI, which is why they are
|
||||
// spelled out here rather than taken from a platform header this library cannot include.
|
||||
struct GbmNativeHandleHeader {
|
||||
Int32 Version;
|
||||
Int32 NumFds;
|
||||
Int32 NumInts;
|
||||
} __attribute__((packed));
|
||||
|
||||
// ONE CONTAINER, ONE LISTENING SOCKET, ONE THREAD.
|
||||
//
|
||||
// There is no lock in here on purpose: the channel is opened, offered to and closed from the
|
||||
// apply thread, which is the one thread that takes the host's frames and draws them, so there
|
||||
// is no second thread to order against - the same reason HostFrameBridge has none.
|
||||
class GbmFrameChannel {
|
||||
public:
|
||||
GbmFrameChannel() = default;
|
||||
GbmFrameChannel(const GbmFrameChannel&) = delete;
|
||||
GbmFrameChannel& operator=(const GbmFrameChannel&) = delete;
|
||||
~GbmFrameChannel();
|
||||
|
||||
// Creates the listening socket if it is not there yet, and does nothing otherwise. It
|
||||
// never waits for a container: the socket is bound, listen(1) is called, and the accept
|
||||
// happens later, when a frame is actually offered. A caller that wants the container to
|
||||
// find the socket before the first frame - the container dials once at its own device
|
||||
// creation and retries about once a second - can call this when the host-framed surface
|
||||
// is initialized, beside HostFrameBridge::Open.
|
||||
Bool Open();
|
||||
|
||||
// Offers one frame the host just handed over, as the dma-buf descriptor behind it.
|
||||
// False means the frame did not go out, for any of the reasons this channel treats as
|
||||
// ordinary: no container is connected, the container just died, or the frame could not be
|
||||
// described. None of them is the caller's problem and none of them is fatal - the frame
|
||||
// is drawn and presented exactly as it would have been without a channel at all.
|
||||
Bool OfferFrame(struct AHardwareBuffer* buffer, const HostFrameOffer& offer);
|
||||
|
||||
// Reads whatever the container has said since the last call and marks the channel down if
|
||||
// the connection has ended. OfferFrame already does this once per frame, so a caller only
|
||||
// needs it if it wants releases drained on a path that has no new frame in it.
|
||||
void Poll();
|
||||
|
||||
// True while a container is connected and the frames are being offered to it.
|
||||
Bool IsUp() const { return m_client >= 0; }
|
||||
|
||||
// How many frames have been offered and not released, as a count of messages and not a
|
||||
// record of them. It is here to be logged, and it is deliberately the only per-frame state
|
||||
// the channel keeps: a release that never arrives costs a number that is too high, and
|
||||
// nothing else, which is what makes a container that stops reading harmless.
|
||||
Uint64 Outstanding() const { return m_outstanding; }
|
||||
|
||||
// Closes the connection, the listening socket and the node it created. Idempotent, safe
|
||||
// from a destructor, and safe to call while a container is connected: the container sees
|
||||
// the connection end and goes back to allocating its frames locally, which is the state it
|
||||
// starts in.
|
||||
void Close();
|
||||
|
||||
private:
|
||||
// The socketpair a frame's handle is handed to and read back from. Recreated on demand,
|
||||
// because the one call the NDK offers for a buffer's descriptors sends them as a message
|
||||
// of its own and the container takes one message per frame - see the note in the .cpp.
|
||||
Bool EnsureRelay();
|
||||
void DropRelay();
|
||||
Bool RelayHandle(struct AHardwareBuffer* buffer, Uint8* payload, SizeT capacity, SizeT& payloadBytes,
|
||||
Int* fds, Int& fdCount);
|
||||
|
||||
// Takes the one container the protocol allows, and closes anyone else who dials while it
|
||||
// is connected rather than leaving them to believe they are being served.
|
||||
void AcceptDialer();
|
||||
|
||||
// Closes the connection and says why, once. The channel stays open for the next container.
|
||||
void MarkDown(const char* what, Int error);
|
||||
|
||||
Int m_listen = -1;
|
||||
Int m_client = -1;
|
||||
Int m_relay[2] = {-1, -1};
|
||||
String m_path;
|
||||
// Bumped only for a frame that actually went out, so a sequence number is never reused and
|
||||
// a release can never be read as belonging to a frame of an earlier connection.
|
||||
Uint32 m_nextSeq = 0;
|
||||
Uint64 m_offered = 0;
|
||||
Uint64 m_released = 0;
|
||||
Uint64 m_outstanding = 0;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user