[Feature] (SharedImage): sample NV12/P010 dma-bufs through GL_OES_EGL_image_external on both backends, copying foreign buffers into server YUV images

This commit is contained in:
2026-10-03 22:46:00 -04:00
parent 4633ca223c
commit 7218bacb49
58 changed files with 2972 additions and 153 deletions
+43
View File
@@ -1517,6 +1517,8 @@ namespace MobileGL::MG_Remote::Server {
reply.Format = image.Fourcc;
reply.Stride = image.Stride;
reply.Offset = image.Offset;
reply.Plane1Stride = image.Plane1Stride;
reply.Plane1Offset = image.Plane1Offset;
};
switch (op.Op) {
case MG_Pipe::kMGPSharedImageAllocate: {
@@ -1554,6 +1556,33 @@ namespace MobileGL::MG_Remote::Server {
return false;
}
SI::ImageRef image = SI::Identify(fd, why);
// YUV: the planes and colour hints ride in Damage (MGPImportPlaneWord). A buffer this
// server did not allocate is taken too, through the labelled CPU-copy fallback
// (SharedImageRegistry.h, Image::ForeignSource); RGBA ones never are.
const Bool yuv = SI::FourccIsYuv(op.Format) && op.DamageCount == MG_Pipe::kMGPImportPlaneWords;
if (image == nullptr && yuv) {
SI::PlaneLayout layout;
layout.Count = static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportPlaneCount]);
layout.Offset[0] = static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportPlane0Offset]);
layout.Offset[1] = static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportPlane1Offset]);
layout.Pitch[0] = static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportPlane0Pitch]);
layout.Pitch[1] = static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportPlane1Pitch]);
// The copy reads rows: only a buffer said to be linear (or said nothing) is one.
const Uint64 modifier =
static_cast<Uint64>(static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportModifierLo])) |
(static_cast<Uint64>(static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportModifierHi])) << 32);
if (op.Damage[MG_Pipe::kMGPImportHasModifier] != 0 && modifier != SI::kModifierLinear &&
modifier != SI::kModifierInvalid) {
char text[96];
std::snprintf(text, sizeof(text), "; a foreign buffer of modifier 0x%016llx is not linear",
static_cast<unsigned long long>(modifier));
why += text;
} else {
std::string foreignWhy;
image = SI::ImportForeignYuv(fd, op.Width, op.Height, op.Format, layout, foreignWhy);
if (image == nullptr) why += "; as a foreign YUV buffer: " + foreignWhy;
}
}
#if !defined(_WIN32)
::close(fd);
#endif
@@ -1561,6 +1590,20 @@ namespace MobileGL::MG_Remote::Server {
MGLOG_W_ONCE("MG_Remote server: a dma-buf import was refused: %s", why.c_str());
return false;
}
if (SI::FourccIsYuv(image->Fourcc) != SI::FourccIsYuv(op.Format) ||
(SI::FourccIsYuv(op.Format) && image->Fourcc != op.Format)) {
MGLOG_W("MG_Remote server: a dma-buf import of image %llu (fourcc 0x%08x) as fourcc 0x%08x was refused",
static_cast<unsigned long long>(image->Id), image->Fourcc, op.Format);
return false;
}
if (yuv) {
SI::Yuv::Hints hints;
hints.ColorSpace = static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportYuvColorSpace]);
hints.Range = static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportYuvRange]);
hints.SitingX = static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportYuvSiting]) & 0xffffu;
hints.SitingY = static_cast<Uint32>(op.Damage[MG_Pipe::kMGPImportYuvSiting]) >> 16;
SI::SetYuvHints(*image, hints);
}
if (image->Width != op.Width || image->Height != op.Height || !SI::FourccSupported(op.Format)) {
MGLOG_W("MG_Remote server: a dma-buf import of image %llu (%ux%u) as %ux%u fourcc 0x%08x was refused",
static_cast<unsigned long long>(image->Id), image->Width, image->Height, op.Width, op.Height,
+382 -31
View File
@@ -19,10 +19,13 @@
#if defined(__ANDROID__)
#include <android/hardware_buffer.h>
#include <dlfcn.h>
#endif
#if !defined(_WIN32)
#include <fcntl.h>
#include <linux/dma-buf.h>
#include <poll.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
@@ -43,6 +46,8 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
std::atomic<Uint64> nextId{1};
UnorderedMap<Uint64, std::weak_ptr<const Image>> byId;
std::map<std::pair<Uint64, Uint64>, std::weak_ptr<const Image>> byIdentity;
// Foreign YUV images, by the identity of the buffer they copy from.
std::map<std::pair<Uint64, Uint64>, std::weak_ptr<const Image>> foreignByIdentity;
// Set once two live images were seen with one identity: the kernel shares an inode
// between dma-bufs, so a descriptor can no longer say which image it is.
Bool identityAmbiguous = false;
@@ -107,14 +112,93 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
return true;
}
// The two AHardwareBuffer entry points newer than this library's minimum platform (29),
// resolved at run time. Null on an older platform: YUV images are then not allocatable.
struct AhbPlaneEntryPoints {
using LockPlanesFn = int (*)(AHardwareBuffer*, uint64_t, int32_t, const ARect*, AHardwareBuffer_Planes*);
using IsSupportedFn = int (*)(const AHardwareBuffer_Desc*);
LockPlanesFn LockPlanes = nullptr;
IsSupportedFn IsSupported = nullptr;
};
const AhbPlaneEntryPoints& AhbPlanes() {
static const AhbPlaneEntryPoints entry = [] {
AhbPlaneEntryPoints e;
void* lib = dlopen("libnativewindow.so", RTLD_NOW | RTLD_NOLOAD);
if (lib == nullptr) lib = dlopen("libnativewindow.so", RTLD_NOW);
if (lib != nullptr) {
e.LockPlanes =
reinterpret_cast<AhbPlaneEntryPoints::LockPlanesFn>(dlsym(lib, "AHardwareBuffer_lockPlanes"));
e.IsSupported =
reinterpret_cast<AhbPlaneEntryPoints::IsSupportedFn>(dlsym(lib, "AHardwareBuffer_isSupported"));
}
return e;
}();
return entry;
}
// A YUV image's buffer: sampled by the GPU, written by the CPU (a foreign image's copy). The
// CPU usage also keeps the layout one the platform can lock plane by plane, which is how
// every layout question about it is answered (AHardwareBuffer_lockPlanes) - never by
// assuming one.
constexpr uint64_t kYuvUsage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE |
AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN | AHARDWAREBUFFER_USAGE_CPU_READ_RARELY;
uint32_t AhbFormatOf(Uint32 fourcc) {
if (fourcc == kFourccNv12) return AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420;
if (fourcc == kFourccP010) return AHARDWAREBUFFER_FORMAT_YCbCr_P010;
return FourccIgnoresAlpha(fourcc) ? AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM
: AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
}
Bool YuvAllocatableNative(Uint32 fourcc) {
const AhbPlaneEntryPoints& entry = AhbPlanes();
if (entry.LockPlanes == nullptr || entry.IsSupported == nullptr) return false;
AHardwareBuffer_Desc desc{};
desc.width = 64;
desc.height = 64;
desc.layers = 1;
desc.format = AhbFormatOf(fourcc);
desc.usage = kYuvUsage;
return entry.IsSupported(&desc) == 1;
}
// The planes of a locked YUV buffer as the plane copy addresses them.
Bool LockYuvPlanes(AHardwareBuffer* ahb, Yuv::PlanarLayout& out, std::string& why) {
const AhbPlaneEntryPoints& entry = AhbPlanes();
if (entry.LockPlanes == nullptr) {
why = "AHardwareBuffer_lockPlanes is not available";
return false;
}
AHardwareBuffer_Planes planes{};
const int locked = entry.LockPlanes(ahb, AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, -1, nullptr, &planes);
if (locked != 0) {
why = Format("AHardwareBuffer_lockPlanes rc=%lld", locked);
return false;
}
if (planes.planeCount < 3) {
AHardwareBuffer_unlock(ahb, nullptr);
why = Format("the locked buffer has %lld planes, not 3", planes.planeCount);
return false;
}
const auto view = [](const AHardwareBuffer_Plane& plane) {
return Yuv::PlaneView{static_cast<Uint8*>(plane.data), static_cast<Int64>(plane.rowStride),
plane.pixelStride};
};
out.Y = view(planes.planes[0]);
out.Cb = view(planes.planes[1]);
out.Cr = view(planes.planes[2]);
return true;
}
Bool AllocateNative(Image& image, std::string& why) {
AHardwareBuffer_Desc desc{};
desc.width = image.Width;
desc.height = image.Height;
desc.layers = 1;
desc.format = FourccIgnoresAlpha(image.Fourcc) ? AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM
: AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM;
desc.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT;
desc.format = AhbFormatOf(image.Fourcc);
desc.usage = FourccIsYuv(image.Fourcc)
? kYuvUsage
: AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | AHARDWAREBUFFER_USAGE_GPU_COLOR_OUTPUT;
AHardwareBuffer* ahb = nullptr;
const int allocated = AHardwareBuffer_allocate(&desc, &ahb);
if (allocated != 0 || ahb == nullptr) {
@@ -131,8 +215,32 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
image.Native = ahb;
image.Fd = fds[0];
for (SizeT i = 1; i < fds.size(); ++i) ::close(fds[i]);
image.Stride = described.stride * 4;
image.Offset = 0;
if (!FourccIsYuv(image.Fourcc)) {
image.Stride = described.stride * 4;
return true;
}
// Plane positions as the platform reports them when the buffer is locked: offsets from
// the luma plane's start, the Cb Cr plane at whichever of Cb and Cr comes first.
const Uint32 sampleBytes = FourccYuvSampleBytes(image.Fourcc);
image.Stride = described.stride * sampleBytes;
image.Plane1Stride = image.Stride;
image.Plane1Offset = image.Stride * image.Height;
Yuv::PlanarLayout planes;
std::string lockWhy;
if (LockYuvPlanes(ahb, planes, lockWhy)) {
const Uint8* chroma = std::min(planes.Cb.Data, planes.Cr.Data);
if (planes.Y.RowStride > 0 && chroma > planes.Y.Data) {
image.Stride = static_cast<Uint32>(planes.Y.RowStride);
image.Plane1Stride = static_cast<Uint32>(planes.Cb.RowStride);
image.Plane1Offset = static_cast<Uint32>(chroma - planes.Y.Data);
}
AHardwareBuffer_unlock(ahb, nullptr);
} else {
MGLOG_W_ONCE("MG_Remote server: a YUV shared image's planes could not be located (%s); its "
"plane offsets are nominal",
lockWhy.c_str());
}
return true;
}
@@ -143,15 +251,29 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
// A host has no AHardwareBuffer. A memfd stands in for the dma-buf so the registry, the
// descriptor round trip and identification can be exercised by the unit tests; no backend
// binds one.
Bool YuvAllocatableNative(Uint32) { return true; }
Bool AllocateNative(Image& image, std::string& why) {
image.Stride = image.Width * 4;
Uint64 bytes = 0;
image.Offset = 0;
if (FourccIsYuv(image.Fourcc)) {
// A tight semi-planar layout stands in for the platform's.
const Uint32 sampleBytes = FourccYuvSampleBytes(image.Fourcc);
image.Stride = image.Width * sampleBytes;
image.Plane1Stride = ((image.Width + 1) / 2) * 2 * sampleBytes;
image.Plane1Offset = image.Stride * image.Height;
bytes = static_cast<Uint64>(image.Plane1Offset) +
static_cast<Uint64>(image.Plane1Stride) * ((image.Height + 1) / 2);
} else {
image.Stride = image.Width * 4;
bytes = static_cast<Uint64>(image.Stride) * image.Height;
}
const int fd = ::memfd_create("mobilegl-shared-image", MFD_CLOEXEC);
if (fd < 0) {
why = Format("memfd_create errno=%lld", errno);
return false;
}
if (::ftruncate(fd, static_cast<off_t>(image.Stride) * image.Height) != 0) {
if (::ftruncate(fd, static_cast<off_t>(bytes)) != 0) {
why = Format("ftruncate errno=%lld", errno);
::close(fd);
return false;
@@ -162,6 +284,8 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
void ReleaseNative(void*) {}
#else
Bool YuvAllocatableNative(Uint32) { return false; }
Bool AllocateNative(Image&, std::string& why) {
why = "shared images need POSIX descriptors";
return false;
@@ -169,6 +293,66 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
void ReleaseNative(void*) {}
#endif
#if !defined(_WIN32)
// The planes of a YUV image as the CPU writes them, held until the guard goes: the locked
// AHardwareBuffer on Android, a mapping of the stand-in memfd on a host.
class YuvWriteAccess {
public:
YuvWriteAccess(const Image& image, std::string& why) : m_image(image) {
#if defined(__ANDROID__)
auto* ahb = static_cast<AHardwareBuffer*>(image.Native);
m_ok = ahb != nullptr && LockYuvPlanes(ahb, m_planes, why);
#else
const SizeT bytes = static_cast<SizeT>(image.Plane1Offset) +
static_cast<SizeT>(image.Plane1Stride) * ((image.Height + 1) / 2);
void* map = ::mmap(nullptr, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, image.Fd, 0);
if (map == MAP_FAILED) {
why = Format("mmap of the stand-in buffer errno=%lld", errno);
return;
}
m_map = map;
m_mapSize = bytes;
const Uint32 sampleBytes = FourccYuvSampleBytes(image.Fourcc);
auto* base = static_cast<Uint8*>(map);
m_planes.Y = {base, image.Stride, sampleBytes};
m_planes.Cb = {base + image.Plane1Offset, image.Plane1Stride, 2 * sampleBytes};
m_planes.Cr = {base + image.Plane1Offset + sampleBytes, image.Plane1Stride, 2 * sampleBytes};
m_ok = true;
#endif
}
~YuvWriteAccess() {
#if defined(__ANDROID__)
if (m_ok) AHardwareBuffer_unlock(static_cast<AHardwareBuffer*>(m_image.Native), nullptr);
#else
if (m_map != nullptr) ::munmap(m_map, m_mapSize);
#endif
}
YuvWriteAccess(const YuvWriteAccess&) = delete;
YuvWriteAccess& operator=(const YuvWriteAccess&) = delete;
Bool Ok() const { return m_ok; }
const Yuv::PlanarLayout& Planes() const { return m_planes; }
private:
const Image& m_image;
Yuv::PlanarLayout m_planes;
Bool m_ok = false;
#if !defined(__ANDROID__)
void* m_map = nullptr;
SizeT m_mapSize = 0;
#endif
};
// DMA_BUF_IOCTL_SYNC around a CPU read of a foreign buffer: the producer's CPU-side writes
// are made visible (cache maintenance) and its implicit fences waited for. A buffer that
// does not take the ioctl (not a dma-buf: a host test's memfd) is read as it is.
void DmaBufSync(int fd, Bool start) {
dma_buf_sync sync{};
sync.flags = DMA_BUF_SYNC_READ | (start ? DMA_BUF_SYNC_START : DMA_BUF_SYNC_END);
while (::ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync) != 0 && (errno == EINTR || errno == EAGAIN)) {
}
}
#endif
} // namespace
Image::~Image() {
@@ -181,8 +365,18 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
const auto key = std::make_pair(IdentityDev, IdentityIno);
if (auto it = reg.byIdentity.find(key); it != reg.byIdentity.end() && it->second.expired())
reg.byIdentity.erase(it);
if (Foreign != nullptr) {
const auto source = std::make_pair(Foreign->IdentityDev, Foreign->IdentityIno);
if (auto it = reg.foreignByIdentity.find(source);
it != reg.foreignByIdentity.end() && it->second.expired())
reg.foreignByIdentity.erase(it);
}
}
#if !defined(_WIN32)
if (Foreign != nullptr) {
if (Foreign->Map != nullptr) ::munmap(Foreign->Map, Foreign->MapSize);
if (Foreign->Fd >= 0) ::close(Foreign->Fd);
}
if (Fd >= 0) ::close(Fd);
if (WriteFence >= 0) ::close(WriteFence);
for (const int fence : ReadFences) ::close(fence);
@@ -190,49 +384,206 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
ReleaseNative(Native);
}
Bool FourccIsYuv(Uint32 fourcc) { return fourcc == kFourccNv12 || fourcc == kFourccP010; }
Uint32 FourccYuvSampleBytes(Uint32 fourcc) {
if (fourcc == kFourccNv12) return 1;
if (fourcc == kFourccP010) return 2;
return 0;
}
Bool YuvFourccAllocatable(Uint32 fourcc) {
if (!FourccIsYuv(fourcc)) return false;
static std::mutex mutex;
static std::map<Uint32, Bool> answers;
const std::lock_guard<std::mutex> lock(mutex);
const auto it = answers.find(fourcc);
if (it != answers.end()) return it->second;
const Bool answer = YuvAllocatableNative(fourcc);
answers.emplace(fourcc, answer);
return answer;
}
Bool FourccSupported(Uint32 fourcc) {
return fourcc == kFourccAbgr8888 || fourcc == kFourccXbgr8888 || fourcc == kFourccArgb8888 ||
fourcc == kFourccXrgb8888;
fourcc == kFourccXrgb8888 || YuvFourccAllocatable(fourcc);
}
Bool FourccIgnoresAlpha(Uint32 fourcc) { return fourcc == kFourccXbgr8888 || fourcc == kFourccXrgb8888; }
namespace {
// Allocate's body. `foreign`, when given, is on the image before anything can find it.
ImageRef AllocateImage(Uint32 width, Uint32 height, Uint32 fourcc, UniquePtr<Image::ForeignSource> foreign,
std::string& why) {
if (width == 0 || height == 0 || width > 16384 || height > 16384) {
why = Format("%lldx%lld is not an image size", width, height);
return nullptr;
}
if (!FourccSupported(fourcc)) {
why = Format("fourcc 0x%08llx is not a shared-image format", fourcc);
return nullptr;
}
Registry& reg = Reg();
auto image = std::make_shared<Image>();
image->Id = reg.nextId.fetch_add(1, std::memory_order_relaxed);
image->Width = width;
image->Height = height;
image->Fourcc = fourcc;
image->Foreign = std::move(foreign);
if (!AllocateNative(*image, why)) return nullptr;
#if !defined(_WIN32)
if (!IdentityOf(image->Fd, &image->IdentityDev, &image->IdentityIno)) {
why = Format("fstat on the exported descriptor errno=%lld", errno);
return nullptr;
}
#endif
const std::lock_guard<std::mutex> lock(reg.mutex);
reg.byId[image->Id] = image;
const auto key = std::make_pair(image->IdentityDev, image->IdentityIno);
if (auto it = reg.byIdentity.find(key); it != reg.byIdentity.end() && !it->second.expired()) {
if (!reg.identityAmbiguous) {
MGLOG_E("MG_Remote server: two live shared images have one dma-buf identity (dev %llu ino %llu); "
"this kernel does not keep dma-buf inodes unique, so a returned descriptor cannot name its "
"image and every shared-image import is refused from here on",
static_cast<unsigned long long>(key.first), static_cast<unsigned long long>(key.second));
}
reg.identityAmbiguous = true;
}
reg.byIdentity[key] = image;
if (image->Foreign != nullptr) {
reg.foreignByIdentity[std::make_pair(image->Foreign->IdentityDev, image->Foreign->IdentityIno)] =
image;
}
return image;
}
} // namespace
ImageRef Allocate(Uint32 width, Uint32 height, Uint32 fourcc, std::string& why) {
return AllocateImage(width, height, fourcc, nullptr, why);
}
ImageRef ImportForeignYuv(int fd, Uint32 width, Uint32 height, Uint32 fourcc, const PlaneLayout& layout,
std::string& why) {
#if defined(_WIN32)
(void)fd, (void)width, (void)height, (void)fourcc, (void)layout;
why = "shared images need POSIX descriptors";
return nullptr;
#else
if (!FourccIsYuv(fourcc) || !YuvFourccAllocatable(fourcc)) {
why = Format("fourcc 0x%08llx is not a YUV format this server can hold", fourcc);
return nullptr;
}
if (width == 0 || height == 0 || width > 16384 || height > 16384) {
why = Format("%lldx%lld is not an image size", width, height);
return nullptr;
}
if (!FourccSupported(fourcc)) {
why = Format("fourcc 0x%08llx is not a shared-image format", fourcc);
if (layout.Count != 2) {
why = Format("a YUV import needs 2 planes, not %lld", layout.Count);
return nullptr;
}
Uint64 dev = 0, ino = 0;
if (!IdentityOf(fd, &dev, &ino)) {
why = Format("fstat on the imported descriptor errno=%lld", errno);
return nullptr;
}
Registry& reg = Reg();
auto image = std::make_shared<Image>();
image->Id = reg.nextId.fetch_add(1, std::memory_order_relaxed);
image->Width = width;
image->Height = height;
image->Fourcc = fourcc;
if (!AllocateNative(*image, why)) return nullptr;
#if !defined(_WIN32)
if (!IdentityOf(image->Fd, &image->IdentityDev, &image->IdentityIno)) {
why = Format("fstat on the exported descriptor errno=%lld", errno);
{
const std::lock_guard<std::mutex> lock(reg.mutex);
const auto it = reg.foreignByIdentity.find(std::make_pair(dev, ino));
ImageRef existing = it != reg.foreignByIdentity.end() ? it->second.lock() : nullptr;
if (existing != nullptr && existing->Width == width && existing->Height == height &&
existing->Fourcc == fourcc && existing->Foreign->Layout.Offset[0] == layout.Offset[0] &&
existing->Foreign->Layout.Offset[1] == layout.Offset[1] &&
existing->Foreign->Layout.Pitch[0] == layout.Pitch[0] &&
existing->Foreign->Layout.Pitch[1] == layout.Pitch[1])
return existing;
}
// The planes must fit the buffer: every row the copy reads is inside the mapping.
const Uint32 sampleBytes = FourccYuvSampleBytes(fourcc);
const off_t end = ::lseek(fd, 0, SEEK_END);
if (end <= 0) {
why = Format("the buffer's size is unknown (lseek errno=%lld)", errno);
return nullptr;
}
#endif
const std::lock_guard<std::mutex> lock(reg.mutex);
reg.byId[image->Id] = image;
const auto key = std::make_pair(image->IdentityDev, image->IdentityIno);
if (auto it = reg.byIdentity.find(key); it != reg.byIdentity.end() && !it->second.expired()) {
if (!reg.identityAmbiguous) {
MGLOG_E("MG_Remote server: two live shared images have one dma-buf identity (dev %llu ino %llu); "
"this kernel does not keep dma-buf inodes unique, so a returned descriptor cannot name its "
"image and every shared-image import is refused from here on",
static_cast<unsigned long long>(key.first), static_cast<unsigned long long>(key.second));
}
reg.identityAmbiguous = true;
const SizeT size = static_cast<SizeT>(end);
const Uint32 chromaHeight = (height + 1) / 2;
const Uint64 chromaRow = static_cast<Uint64>((width + 1) / 2) * 2 * sampleBytes;
const Uint64 lumaEnd = static_cast<Uint64>(layout.Offset[0]) +
static_cast<Uint64>(layout.Pitch[0]) * (height - 1) +
static_cast<Uint64>(width) * sampleBytes;
const Uint64 chromaEnd =
static_cast<Uint64>(layout.Offset[1]) + static_cast<Uint64>(layout.Pitch[1]) * (chromaHeight - 1) + chromaRow;
if (layout.Pitch[0] < width * sampleBytes || layout.Pitch[1] < chromaRow || lumaEnd > size ||
chromaEnd > size) {
why = Format("its planes do not fit the %lld-byte buffer", static_cast<long long>(size));
return nullptr;
}
reg.byIdentity[key] = image;
void* map = ::mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) {
why = Format("the buffer cannot be mapped for reading (errno=%lld)", errno);
return nullptr;
}
auto foreign = MakeUnique<Image::ForeignSource>();
foreign->Fd = ::fcntl(fd, F_DUPFD_CLOEXEC, 0);
foreign->Layout = layout;
foreign->Map = map;
foreign->MapSize = size;
foreign->IdentityDev = dev;
foreign->IdentityIno = ino;
if (foreign->Fd < 0) {
::munmap(map, size);
why = Format("dup errno=%lld", errno);
return nullptr;
}
// A failed allocation drops the half-built image, and ~Image unmaps and closes the source.
ImageRef image = AllocateImage(width, height, fourcc, std::move(foreign), why);
if (image == nullptr) return nullptr;
MGLOG_I("MG_Remote server: a foreign %ux%u %s dma-buf is sampled through image %llu, refreshed by a CPU "
"copy (the fallback for a buffer this platform cannot import)",
width, height, fourcc == kFourccNv12 ? "NV12" : "P010", static_cast<unsigned long long>(image->Id));
return image;
#endif
}
Bool RefreshForeign(const Image& image, std::string& why) {
#if defined(_WIN32)
(void)image;
why = "shared images need POSIX descriptors";
return false;
#else
if (image.Foreign == nullptr) return true;
Image::ForeignSource& source = *image.Foreign;
// WRITE AFTER READ: what a frame still samples of the old content finishes first. The copy
// is the CPU's, so this is a CPU wait - bounded, like every wait on another session.
if (!WaitForReads(image, 1000)) {
MGLOG_W_ONCE("MG_Remote server: a foreign image's readers did not finish within 1 s; refreshed anyway");
}
const std::lock_guard<std::mutex> lock(source.Mutex);
YuvWriteAccess access(image, why);
if (!access.Ok()) return false;
const auto* base = static_cast<const Uint8*>(source.Map);
DmaBufSync(source.Fd, true);
const Bool copied = Yuv::CopySemiPlanar(base + source.Layout.Offset[0], source.Layout.Pitch[0],
base + source.Layout.Offset[1], source.Layout.Pitch[1], image.Width,
image.Height, FourccYuvSampleBytes(image.Fourcc), access.Planes());
DmaBufSync(source.Fd, false);
if (!copied) {
why = "the locked buffer's planes cannot hold the samples";
return false;
}
++source.Copies;
return true;
#endif
}
void SetYuvHints(const Image& image, const Yuv::Hints& hints) {
const std::lock_guard<std::mutex> lock(image.SyncMutex);
image.YuvHints = hints;
}
Yuv::Hints GetYuvHints(const Image& image) {
const std::lock_guard<std::mutex> lock(image.SyncMutex);
return image.YuvHints;
}
ImageRef Identify(int fd, std::string& why) {
@@ -25,6 +25,8 @@
#pragma once
#include <Includes.h>
#include "SharedImageYuv.h"
#include <mutex>
#include <string>
@@ -44,6 +46,11 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
// client renders as red samples as red, whichever of the four names it allocated under.
inline constexpr Uint32 kFourccArgb8888 = FourCC('A', 'R', '2', '4');
inline constexpr Uint32 kFourccXrgb8888 = FourCC('X', 'R', '2', '4');
// YUV 4:2:0, two planes: Y, then Cb Cr interleaved at half resolution (8-bit samples; P010 the
// same with 10 significant bits in the high end of 16). A YUV image is never rendered to: it is
// SAMPLED, converted into RGBA by whoever binds it (SharedImageYuv.h).
inline constexpr Uint32 kFourccNv12 = FourCC('N', 'V', '1', '2');
inline constexpr Uint32 kFourccP010 = FourCC('P', '0', '1', '0');
// "The layout is the allocator's": what an AHardwareBuffer is to anybody but its allocator.
inline constexpr Uint64 kModifierInvalid = 0x00ffffffffffffffull;
inline constexpr Uint64 kModifierLinear = 0;
@@ -52,6 +59,20 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
Bool FourccSupported(Uint32 fourcc);
// The X formats: alpha reads as 1 whatever is stored.
Bool FourccIgnoresAlpha(Uint32 fourcc);
// NV12 / P010.
Bool FourccIsYuv(Uint32 fourcc);
// Bytes of one sample of a YUV fourcc (1, or 2 for P010); 0 for anything else.
Uint32 FourccYuvSampleBytes(Uint32 fourcc);
// Whether this server can allocate images of a YUV fourcc (the platform's allocator takes the
// format for sampling). Asked once per format, cached.
Bool YuvFourccAllocatable(Uint32 fourcc);
// Where an imported dma-buf's planes are: byte offset and row pitch of each.
struct PlaneLayout {
Uint32 Count = 1;
Uint32 Offset[2] = {0, 0};
Uint32 Pitch[2] = {0, 0};
};
struct Image {
Uint64 Id = 0;
@@ -61,6 +82,11 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
Uint32 Stride = 0; // bytes
Uint32 Offset = 0;
Uint64 Modifier = kModifierInvalid;
// A YUV image's second plane (Cb Cr): same descriptor, this offset and pitch. The layout is
// still the allocator's - these are what it reported when the buffer was locked, so a
// client can name the plane, not a promise that the descriptor maps that way.
Uint32 Plane1Stride = 0;
Uint32 Plane1Offset = 0;
// AHardwareBuffer* on Android; null on a host, where the memory is a memfd (unit tests).
void* Native = nullptr;
// The descriptor clients receive duplicates of. Owned.
@@ -77,6 +103,31 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
mutable Uint64 WriteGeneration = 0;
mutable Vector<int> ReadFences;
// FOREIGN YUV - THE CPU-COPY FALLBACK. A YUV dma-buf this server did not allocate (a video
// decoder's frame) cannot be imported vendor-neutrally: neither the platform's EGL nor its
// Vulkan takes a raw dma-buf. Such an import becomes a YUV image of this server's (Native,
// an AHardwareBuffer the CPU can write) whose content is COPIED from a CPU mapping of the
// foreign buffer (RefreshForeign) at a reader's first use of it in each of its frames - the
// moments the producer's content can have moved for that reader. Every other image is
// zero-copy; this one costs a frame's worth of memcpy per refresh, and its log says so.
struct ForeignSource {
int Fd = -1; // a duplicate, owned
PlaneLayout Layout;
void* Map = nullptr;
SizeT MapSize = 0;
std::mutex Mutex; // one refresh at a time
Uint64 Copies = 0;
// The foreign buffer's identity, under which a later import of it finds this image.
Uint64 IdentityDev = 0;
Uint64 IdentityIno = 0;
};
UniquePtr<ForeignSource> Foreign;
Bool IsForeign() const { return Foreign != nullptr; }
// How the samples of a YUV image convert to RGB: the EGL_EXT_image_dma_buf_import hints
// of its last import. Guarded by SyncMutex.
mutable Yuv::Hints YuvHints;
Image() = default;
Image(const Image&) = delete;
Image& operator=(const Image&) = delete;
@@ -91,6 +142,22 @@ namespace MobileGL::MG_Remote::Server::SharedImages {
// this server never exported, or one whose identity the kernel does not keep unique.
ImageRef Identify(int fd, std::string& why);
// A YUV import of a dma-buf this server did not export (Image::ForeignSource): the live
// foreign image already made from the same buffer with the same shape, or a new one, which
// keeps a duplicate of `fd`. Null with `why` when the buffer cannot be mapped, its planes do
// not fit in it, or no YUV image can be allocated to copy it into.
ImageRef ImportForeignYuv(int fd, Uint32 width, Uint32 height, Uint32 fourcc, const PlaneLayout& layout,
std::string& why);
// A foreign image's content brought up to date from its source (the copy). Waits for the
// image's pending reads first, so a frame still sampling the old content keeps it. True for an
// image that is not foreign: there is nothing to do.
Bool RefreshForeign(const Image& image, std::string& why);
// The colour hints a YUV image is converted with.
void SetYuvHints(const Image& image, const Yuv::Hints& hints);
Yuv::Hints GetYuvHints(const Image& image);
// The live image with this id, or null.
ImageRef Find(Uint64 id);
+158
View File
@@ -0,0 +1,158 @@
// MobileGL - MobileGL/MG_Remote/Server/SharedImageYuv.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
// YUV SHARED IMAGES: the pure parts - plane copies and the YCbCr -> RGB conversion an image's
// colour hints select - kept free of Android and of any backend so the host tests can check them.
//
// A YUV image is sampled as RGBA: whoever binds it to a texture gets the image CONVERTED into the
// texture's own RGBA8 storage at its first use in each of the reader's frames (the backends do
// that on the GPU, from the image's AHardwareBuffer). The conversion is the one the EGLImage's
// EGL_EXT_image_dma_buf_import hints name - colour space, sample range and chroma siting - with
// that extension's defaults (ITU-R BT.601, narrow range, siting 0) where a hint is absent.
#pragma once
#include <Includes.h>
#include <cstring>
namespace MobileGL::MG_Remote::Server::SharedImages::Yuv {
// EGL_EXT_image_dma_buf_import's hint values.
inline constexpr Uint32 kEglItuRec601 = 0x327F;
inline constexpr Uint32 kEglItuRec709 = 0x3280;
inline constexpr Uint32 kEglItuRec2020 = 0x3281;
inline constexpr Uint32 kEglYuvFullRange = 0x3282;
inline constexpr Uint32 kEglYuvNarrowRange = 0x3283;
inline constexpr Uint32 kEglYuvChromaSiting0 = 0x3284;
inline constexpr Uint32 kEglYuvChromaSiting0_5 = 0x3285;
// What an import said about its samples. 0 = not given (the extension's default applies).
struct Hints {
Uint32 ColorSpace = 0;
Uint32 Range = 0;
Uint32 SitingX = 0;
Uint32 SitingY = 0;
Uint32 EffectiveColorSpace() const {
return ColorSpace == kEglItuRec709 || ColorSpace == kEglItuRec2020 ? ColorSpace : kEglItuRec601;
}
Bool FullRange() const { return Range == kEglYuvFullRange; }
Bool MidpointX() const { return SitingX == kEglYuvChromaSiting0_5; }
Bool MidpointY() const { return SitingY == kEglYuvChromaSiting0_5; }
Bool operator==(const Hints& other) const {
return ColorSpace == other.ColorSpace && Range == other.Range && SitingX == other.SitingX &&
SitingY == other.SitingY;
}
};
// rgb = Matrix * (ycbcr - Offset), every value normalized to [0, 1] as a UNORM texture of the
// image's sample size returns it. Matrix is COLUMN-major (a GLSL mat3 uniform as is).
struct Conversion {
float Matrix[9];
float Offset[3];
};
// `sampleMax` is what a full-scale code reads as before normalization: 255 for 8-bit samples,
// 65535/64 for P010 (10 bits in the high end of 16, read as UNORM16).
inline Conversion ConversionFor(const Hints& hints, double sampleMax) {
double kr = 0.299, kb = 0.114;
if (hints.EffectiveColorSpace() == kEglItuRec709) {
kr = 0.2126;
kb = 0.0722;
} else if (hints.EffectiveColorSpace() == kEglItuRec2020) {
kr = 0.2627;
kb = 0.0593;
}
const double kg = 1.0 - kr - kb;
// Code values of an 8-bit-equivalent scale, multiplied up for deeper samples.
const double scale = sampleMax / 255.0;
double yOffset = 0.0, yScale = 1.0, cScale = 1.0;
const double cOffset = 128.0 * scale / sampleMax;
if (!hints.FullRange()) {
yOffset = 16.0 * scale / sampleMax;
yScale = sampleMax / (219.0 * scale);
cScale = sampleMax / (224.0 * scale);
}
Conversion c{};
// Columns: Y, Cb, Cr.
const double m[9] = {
yScale,
yScale,
yScale,
0.0,
-cScale * 2.0 * kb * (1.0 - kb) / kg,
cScale * 2.0 * (1.0 - kb),
cScale * 2.0 * (1.0 - kr),
-cScale * 2.0 * kr * (1.0 - kr) / kg,
0.0,
};
for (SizeT i = 0; i < 9; ++i) c.Matrix[i] = static_cast<float>(m[i]);
c.Offset[0] = static_cast<float>(yOffset);
c.Offset[1] = static_cast<float>(cOffset);
c.Offset[2] = static_cast<float>(cOffset);
return c;
}
// One component plane of a destination: where (x, y)'s sample of it lives is
// Data + y * RowStride + x * PixelStride.
struct PlaneView {
Uint8* Data = nullptr;
Int64 RowStride = 0;
Uint32 PixelStride = 0;
};
struct PlanarLayout {
PlaneView Y, Cb, Cr;
};
// A two-plane semi-planar source (NV12: Y, then Cb Cr interleaved at half resolution; P010
// the same with 16-bit samples, `sampleBytes` = 2) into any destination layout: semi-planar
// Cb-first rows are copied as they are, everything else (Cr first, fully planar) sample by
// sample. False when the destination cannot hold the samples (null planes, a pixel stride
// smaller than a sample).
inline Bool CopySemiPlanar(const Uint8* srcY, Int64 srcYPitch, const Uint8* srcCbCr, Int64 srcCbCrPitch,
Uint32 width, Uint32 height, Uint32 sampleBytes, const PlanarLayout& dst) {
if (srcY == nullptr || srcCbCr == nullptr || dst.Y.Data == nullptr || dst.Cb.Data == nullptr ||
dst.Cr.Data == nullptr || (sampleBytes != 1 && sampleBytes != 2) || dst.Y.PixelStride < sampleBytes ||
dst.Cb.PixelStride < sampleBytes || dst.Cr.PixelStride < sampleBytes)
return false;
const Uint32 chromaWidth = (width + 1) / 2;
const Uint32 chromaHeight = (height + 1) / 2;
// Luma.
for (Uint32 y = 0; y < height; ++y) {
const Uint8* from = srcY + static_cast<Int64>(y) * srcYPitch;
Uint8* to = dst.Y.Data + static_cast<Int64>(y) * dst.Y.RowStride;
if (dst.Y.PixelStride == sampleBytes) {
std::memcpy(to, from, static_cast<SizeT>(width) * sampleBytes);
} else {
for (Uint32 x = 0; x < width; ++x)
std::memcpy(to + static_cast<SizeT>(x) * dst.Y.PixelStride, from + static_cast<SizeT>(x) * sampleBytes,
sampleBytes);
}
}
// Chroma.
const Bool sameInterleave = dst.Cb.PixelStride == 2 * sampleBytes && dst.Cr.PixelStride == 2 * sampleBytes &&
dst.Cr.Data == dst.Cb.Data + sampleBytes && dst.Cr.RowStride == dst.Cb.RowStride;
for (Uint32 y = 0; y < chromaHeight; ++y) {
const Uint8* from = srcCbCr + static_cast<Int64>(y) * srcCbCrPitch;
if (sameInterleave) {
std::memcpy(dst.Cb.Data + static_cast<Int64>(y) * dst.Cb.RowStride, from,
static_cast<SizeT>(chromaWidth) * 2 * sampleBytes);
continue;
}
Uint8* cb = dst.Cb.Data + static_cast<Int64>(y) * dst.Cb.RowStride;
Uint8* cr = dst.Cr.Data + static_cast<Int64>(y) * dst.Cr.RowStride;
for (Uint32 x = 0; x < chromaWidth; ++x) {
const Uint8* pair = from + static_cast<SizeT>(x) * 2 * sampleBytes;
std::memcpy(cb + static_cast<SizeT>(x) * dst.Cb.PixelStride, pair, sampleBytes);
std::memcpy(cr + static_cast<SizeT>(x) * dst.Cr.PixelStride, pair + sampleBytes, sampleBytes);
}
}
return true;
}
} // namespace MobileGL::MG_Remote::Server::SharedImages::Yuv