[Feature] (SharedImage): zero-copy Wayland windows via linux-dmabuf, EGL dma-buf import, GBM backend, and Espryt/Magma AHB present and texture binding

This commit is contained in:
2026-10-02 08:39:59 -04:00
parent 38842985f8
commit 3d17e8c10b
39 changed files with 2868 additions and 54 deletions
+163
View File
@@ -1,8 +1,10 @@
#include <gtest/gtest.h>
#include <MG_State/EGLState/Core.h>
#include <EGL/eglext.h>
#include <future>
#include <memory>
#include <thread>
#include <vector>
namespace {
using StateContext = MobileGL::MG_State::EGLState::EGLContext;
@@ -132,3 +134,164 @@ TEST(EGLStateProfile, CompatibilityProfileRequiresExplicitCompatBit) {
EXPECT_FALSE(fixture->State.IsCurrentContextOpenGLCompatibilityProfile());
EXPECT_EQ(fixture->State.ConsumeError(), EGL_SUCCESS);
}
namespace {
constexpr EGLAttrib kAbgr8888 = StateContext::kDrmFourccAbgr8888;
constexpr EGLAttrib kXbgr8888 = StateContext::kDrmFourccXbgr8888;
constexpr EGLAttrib kArgb8888 = 0x34325241; // 'AR24': not a shared-image byte order
// A complete single-plane import, as a compositor builds it for a linux-dmabuf wl_buffer.
std::vector<EGLAttrib> DmaBufAttribs(EGLAttrib fourcc = kAbgr8888) {
return {EGL_WIDTH, 64, EGL_HEIGHT, 32, EGL_LINUX_DRM_FOURCC_EXT, fourcc, EGL_DMA_BUF_PLANE0_FD_EXT, 7,
EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0, EGL_DMA_BUF_PLANE0_PITCH_EXT, 256, EGL_NONE};
}
std::vector<EGLAttrib> WithAttrib(std::vector<EGLAttrib> attribs, EGLAttrib name, EGLAttrib value) {
attribs.insert(attribs.end() - 1, {name, value});
return attribs;
}
std::vector<EGLAttrib> WithoutAttrib(const std::vector<EGLAttrib>& attribs, EGLAttrib name) {
std::vector<EGLAttrib> out;
for (size_t i = 0; attribs[i] != EGL_NONE; i += 2) {
if (attribs[i] != name) out.insert(out.end(), {attribs[i], attribs[i + 1]});
}
out.push_back(EGL_NONE);
return out;
}
EGLint PrepareError(EGLFixture& fixture, const std::vector<EGLAttrib>& attribs,
StateContext::EGLContextHandle context = EGL_NO_CONTEXT, EGLClientBuffer buffer = nullptr) {
StateContext::DmaBufImportAttribs parsed;
const bool ok = fixture.State.PrepareDmaBufImport(fixture.Display, context, buffer, attribs.data(), &parsed);
const EGLint error = fixture.State.ConsumeError();
EXPECT_EQ(ok, error == EGL_SUCCESS);
return error;
}
} // namespace
TEST(EGLStateDmaBufImport, ParsesACompleteSinglePlaneImport) {
auto fixture = CreateFixture();
const auto attribs = WithAttrib(WithAttrib(DmaBufAttribs(kXbgr8888), EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT,
static_cast<EGLAttrib>(0xffffffffu)),
EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, 0x00ffffff);
StateContext::DmaBufImportAttribs parsed;
ASSERT_TRUE(fixture->State.PrepareDmaBufImport(fixture->Display, EGL_NO_CONTEXT, nullptr, attribs.data(), &parsed));
EXPECT_EQ(fixture->State.ConsumeError(), EGL_SUCCESS);
EXPECT_EQ(parsed.Width, 64);
EXPECT_EQ(parsed.Height, 32);
EXPECT_EQ(parsed.Fourcc, static_cast<uint32_t>(kXbgr8888));
EXPECT_EQ(parsed.Fd, 7);
EXPECT_EQ(parsed.Offset, 0);
EXPECT_EQ(parsed.Pitch, 256);
EXPECT_TRUE(parsed.HasModifier);
EXPECT_EQ(parsed.Modifier, 0x00ffffffffffffffull);
// The hints that only mean something for YUV are taken and ignored.
EXPECT_EQ(PrepareError(*fixture, WithAttrib(DmaBufAttribs(), EGL_SAMPLE_RANGE_HINT_EXT, EGL_YUV_FULL_RANGE_EXT)),
EGL_SUCCESS);
}
TEST(EGLStateDmaBufImport, RefusesWhatTheExtensionRefuses) {
auto fixture = CreateFixture();
// Incomplete lists.
for (const EGLAttrib required : {EGL_WIDTH, EGL_HEIGHT, EGL_LINUX_DRM_FOURCC_EXT, EGL_DMA_BUF_PLANE0_FD_EXT,
EGL_DMA_BUF_PLANE0_OFFSET_EXT, EGL_DMA_BUF_PLANE0_PITCH_EXT}) {
EXPECT_EQ(PrepareError(*fixture, WithoutAttrib(DmaBufAttribs(), required)), EGL_BAD_PARAMETER)
<< "missing 0x" << std::hex << required;
}
EXPECT_EQ(PrepareError(*fixture, WithAttrib(DmaBufAttribs(), EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT, 0)),
EGL_BAD_PARAMETER);
// A context or a buffer, which this target takes neither of.
EXPECT_EQ(PrepareError(*fixture, DmaBufAttribs(), fixture->Context), EGL_BAD_PARAMETER);
int notABuffer = 0;
EXPECT_EQ(PrepareError(*fixture, DmaBufAttribs(), EGL_NO_CONTEXT, &notABuffer), EGL_BAD_PARAMETER);
// An attribute no dma-buf import has.
EXPECT_EQ(PrepareError(*fixture, WithAttrib(DmaBufAttribs(), EGL_CONFIG_ID, 1)), EGL_BAD_PARAMETER);
// Formats outside the shared images' byte order, and planes a single-plane format lacks.
EXPECT_EQ(PrepareError(*fixture, DmaBufAttribs(kArgb8888)), EGL_BAD_MATCH);
EXPECT_EQ(PrepareError(*fixture, WithAttrib(DmaBufAttribs(), EGL_DMA_BUF_PLANE1_FD_EXT, 8)), EGL_BAD_ATTRIBUTE);
// Values no image can have.
auto zeroWidth = WithoutAttrib(DmaBufAttribs(), EGL_WIDTH);
zeroWidth = WithAttrib(zeroWidth, EGL_WIDTH, 0);
EXPECT_EQ(PrepareError(*fixture, zeroWidth), EGL_BAD_PARAMETER);
auto badFd = WithAttrib(WithoutAttrib(DmaBufAttribs(), EGL_DMA_BUF_PLANE0_FD_EXT), EGL_DMA_BUF_PLANE0_FD_EXT, -1);
EXPECT_EQ(PrepareError(*fixture, badFd), EGL_BAD_PARAMETER);
auto badPitch =
WithAttrib(WithoutAttrib(DmaBufAttribs(), EGL_DMA_BUF_PLANE0_PITCH_EXT), EGL_DMA_BUF_PLANE0_PITCH_EXT, 0);
EXPECT_EQ(PrepareError(*fixture, badPitch), EGL_BAD_ACCESS);
// An uninitialized display.
ASSERT_TRUE(fixture->State.TerminateDisplay(fixture->Display));
EXPECT_EQ(PrepareError(*fixture, DmaBufAttribs()), EGL_NOT_INITIALIZED);
}
TEST(EGLStateDmaBufImport, SharedImageLifetime) {
auto fixture = CreateFixture();
const StateContext::SharedImageInfo info{.Id = 42, .Width = 64, .Height = 32, .Fourcc = StateContext::kDrmFourccAbgr8888};
const EGLImage image = fixture->State.CreateSharedImage(fixture->Display, info);
ASSERT_NE(image, EGL_NO_IMAGE);
StateContext::SharedImageInfo found;
ASSERT_TRUE(fixture->State.GetSharedImage(image, &found));
EXPECT_EQ(found.Id, 42u);
EXPECT_EQ(found.Width, 64);
EXPECT_EQ(found.Height, 32);
// An image of another target names no shared image, and destroying it releases none.
const EGLImage plain =
fixture->State.CreateImage(fixture->Display, EGL_NO_CONTEXT, EGL_GL_TEXTURE_2D, nullptr, nullptr);
ASSERT_NE(plain, EGL_NO_IMAGE);
EXPECT_FALSE(fixture->State.GetSharedImage(plain, nullptr));
uint64_t released = 99;
EXPECT_TRUE(fixture->State.DestroyImage(fixture->Display, plain, &released));
EXPECT_EQ(released, 0u);
EXPECT_TRUE(fixture->State.DestroyImage(fixture->Display, image, &released));
EXPECT_EQ(released, 42u);
EXPECT_FALSE(fixture->State.GetSharedImage(image, nullptr));
EXPECT_FALSE(fixture->State.DestroyImage(fixture->Display, image, &released));
EXPECT_EQ(fixture->State.ConsumeError(), EGL_BAD_PARAMETER);
// eglTerminate drops the display's images; their shared images are handed back once.
ASSERT_NE(fixture->State.CreateSharedImage(fixture->Display, {.Id = 7, .Width = 1, .Height = 1}), EGL_NO_IMAGE);
EXPECT_TRUE(fixture->State.TakeOrphanedSharedImages().empty());
ASSERT_TRUE(fixture->State.TerminateDisplay(fixture->Display));
EXPECT_EQ(fixture->State.TakeOrphanedSharedImages(), std::vector<uint64_t>{7});
EXPECT_TRUE(fixture->State.TakeOrphanedSharedImages().empty());
}
TEST(EGLStateDmaBufImport, FormatAndModifierQueries) {
auto fixture = CreateFixture();
EGLint count = -1;
ASSERT_TRUE(fixture->State.QueryDmaBufFormats(fixture->Display, true, 0, nullptr, &count));
EXPECT_EQ(count, 2);
EGLint formats[4] = {};
ASSERT_TRUE(fixture->State.QueryDmaBufFormats(fixture->Display, true, 4, formats, &count));
ASSERT_EQ(count, 2);
EXPECT_EQ(formats[0], static_cast<EGLint>(kAbgr8888));
EXPECT_EQ(formats[1], static_cast<EGLint>(kXbgr8888));
ASSERT_TRUE(fixture->State.QueryDmaBufFormats(fixture->Display, true, 1, formats, &count));
EXPECT_EQ(count, 1);
// Without shared images there is nothing to import.
ASSERT_TRUE(fixture->State.QueryDmaBufFormats(fixture->Display, false, 0, nullptr, &count));
EXPECT_EQ(count, 0);
EXPECT_FALSE(fixture->State.QueryDmaBufFormats(fixture->Display, true, -1, formats, &count));
EXPECT_EQ(fixture->State.ConsumeError(), EGL_BAD_PARAMETER);
// No explicit modifiers: importers fall back to the implicit one.
uint64_t modifiers[2] = {};
EGLBoolean externalOnly[2] = {};
count = -1;
ASSERT_TRUE(fixture->State.QueryDmaBufModifiers(fixture->Display, true, static_cast<EGLint>(kAbgr8888), 2,
modifiers, externalOnly, &count));
EXPECT_EQ(count, 0);
EXPECT_FALSE(fixture->State.QueryDmaBufModifiers(fixture->Display, true, static_cast<EGLint>(kArgb8888), 0,
nullptr, nullptr, &count));
EXPECT_EQ(fixture->State.ConsumeError(), EGL_BAD_PARAMETER);
ASSERT_TRUE(fixture->State.TerminateDisplay(fixture->Display));
EXPECT_FALSE(fixture->State.QueryDmaBufFormats(fixture->Display, true, 0, nullptr, &count));
EXPECT_EQ(fixture->State.ConsumeError(), EGL_NOT_INITIALIZED);
}
+21 -8
View File
@@ -151,7 +151,7 @@ TEST(PipeCatalogue, GeneratedTablesHoldTheWholeCatalogue) {
// + P14 S1's bind_context (docs/Disaggregated/design/11-state-ownership.md), the in-band
// context binding - a kScreen row for applier_reset's reason: a context switch is a
// whole-session edge, not a context-family verb.
EXPECT_EQ(ClassCount<kScreen>(), 13u);
EXPECT_EQ(ClassCount<kScreen>(), 14u); // + shared_image (opcode 85), a kScreen table row
EXPECT_EQ(ClassCount<kCtxQuery>(), 8u);
EXPECT_EQ(ClassCount<kCtxCso>(), 13u);
EXPECT_EQ(ClassCount<kCtxState>(), 19u);
@@ -652,7 +652,13 @@ TEST(PipeCatalogue, LateArrivalsAreAppendedWithoutRenumbering) {
// kScreen control record with the applier_reset shape - one POD, no blob, no tail, no reply
// and no MGPipeApply* entry point - so the row beside it is pinned the same way.
EXPECT_EQ(static_cast<Uint16>(MGPWireOp::BindContext), 84);
EXPECT_EQ(static_cast<Uint16>(MGPWireOp::kOpCount), 85);
// Shared images (docs/Disaggregated/notes/anland/plan-ahb-dmabuf.md) appended opcode 85 by the
// same rule: a kScreen row with a reply slot - every operation is answered.
EXPECT_EQ(static_cast<Uint16>(MGPWireOp::SharedImage), 85);
EXPECT_EQ(static_cast<Uint16>(MGPWireOp::kOpCount), 86);
EXPECT_EQ(MGPipeCallFlagsFor(MGPWireOp::SharedImage), static_cast<Uint32>(kReplySlot));
EXPECT_EQ(MGPipeCallClassFor(MGPWireOp::SharedImage), kScreen);
EXPECT_EQ(MGPipeWaitClassFor(MGPWireOp::SharedImage), kWaitReply);
EXPECT_EQ(MGPipeCallFlagsFor(MGPWireOp::ReadPixelsToBuffer), static_cast<Uint32>(kNone));
EXPECT_EQ(MGPipeCallFlagsFor(MGPWireOp::GetTextureImageToBuffer), static_cast<Uint32>(kNone));
EXPECT_EQ(sizeof(MGPReadbackToBuffer), 104u);
@@ -693,6 +699,11 @@ TEST(PipeCatalogue, LateArrivalsAreAppendedWithoutRenumbering) {
// the two planes cannot disagree about how much of a token is real.
EXPECT_EQ(sizeof(MGPBindContext), 8u);
EXPECT_EQ(sizeof(MGPBindContext::ClientContextToken), 8u);
// Shared images: the request, its answer and the aux-socket sideband are fixed-width PODs; the
// sideband is the T0 Offer's size so one inbox reads both.
EXPECT_EQ(sizeof(MGPSharedImageOp), 32u);
EXPECT_EQ(sizeof(MGPSharedImageReply), 40u);
EXPECT_EQ(sizeof(MGPSharedImageFdOffer), 32u);
EXPECT_EQ(sizeof(MGPHandleOnly), 16u);
// rv's two (§5.3/§7.6): the residual-value POD - 2 + 15 Uint32s, 2 Uint8s and 2 pad bytes,
// then the three Uint64s - and the AMENDED attribute carrier, which grew 24 -> 56 to carry
@@ -826,7 +837,7 @@ TEST(PipeCatalogue, EveryRowCarriesTheWaitClassTheContractGivesIt) {
EXPECT_EQ(MGPipeWaitClassFor(op), kWaitNone) << WireOpNameForDiag(op);
}
// The partition, by count. 14 + 1 + 10 = 25 rows wait; every other row of the catalogue does
// The partition, by count. 15 + 1 + 10 = 26 rows wait (shared_image made the reply rows 15); every other row of the catalogue does
// not. A row that changed class moves two of these numbers at once - which is why the
// kWaitApplied count went 9 -> 10 and the kWaitNone offset 24 -> 25 in the SAME commit that
// moved resource_copy_region (P5e gl, ID-118).
@@ -840,10 +851,10 @@ TEST(PipeCatalogue, EveryRowCarriesTheWaitClassTheContractGivesIt) {
default: ++other; break;
}
}
EXPECT_EQ(reply, 14u);
EXPECT_EQ(reply, 15u);
EXPECT_EQ(applied, 10u);
EXPECT_EQ(present, 1u);
EXPECT_EQ(none, static_cast<SizeT>(kMGPipeCallCount) - 25u);
EXPECT_EQ(none, static_cast<SizeT>(kMGPipeCallCount) - 26u);
EXPECT_EQ(other, 0u) << "a row carries the kWaitClassCount terminator as its class";
// And the reply half of the partition BOTH WAYS, over the whole catalogue: exactly the rows
@@ -1059,7 +1070,8 @@ TEST(PipeCatalogue, SixValueStructsHaveFieldLists) {
// appended bind_context's MGPBindContext (docs/Disaggregated/design/11-state-ownership.md):
// 84. A payload without a list here is one the comparator cannot see, so the row is not
// optional bookkeeping - gen_pipe.py refuses the build without it.
EXPECT_EQ(kMGPipeVerifiedPayloadCount, 84u);
// + shared_image's MGPSharedImageOp: 85.
EXPECT_EQ(kMGPipeVerifiedPayloadCount, 85u);
static_assert(MGPipeHasFieldVerifier<RenderStateParameters>::value);
static_assert(MGPipeHasFieldVerifier<PixelStoreParameters>::value);
static_assert(MGPipeHasFieldVerifier<PerBufferBlendState>::value);
@@ -1345,13 +1357,14 @@ TEST(PipeCatalogue, ResourceRespecifyAcksOnlyImmutableStorage) {
// FOURTEEN. Ten answers that were always declared - get_caps, map_persistent, the two fence
// reads, the three query reads, the two readbacks and read_pixels - plus the FOUR ACCEPTANCE
// ROWS, whose applier entry points return a Bool the client acts on destructively and which
// carried no flag because in monolith that answer is a direct call's return value.
// carried no flag because in monolith that answer is a direct call's return value. FIFTEEN since
// shared_image, whose every operation is answered.
Uint32 replySlotCalls = 0;
#define MGP_COUNT_REPLY_SLOT_CALLS(Name, Payload, Class, Flags, Wait) \
if ((static_cast<Uint32>(Flags) & static_cast<Uint32>(kReplySlot)) != 0) ++replySlotCalls;
MGP_CALL_LIST(MGP_COUNT_REPLY_SLOT_CALLS)
#undef MGP_COUNT_REPLY_SLOT_CALLS
EXPECT_EQ(replySlotCalls, 14u);
EXPECT_EQ(replySlotCalls, 15u);
// And the four by name, because a count alone would let a row lose the flag while another
// gained one. These are exactly the MGPipeApply* entry points that return Bool
+3
View File
@@ -48,6 +48,9 @@ if (NOT WIN32)
# the in-process queue and a real SCM_RIGHTS socket pair: in order, out of order, late, stale,
# missing (bounded), not-an-Offer.
list(APPEND MOBILEGL_WIRE_TESTS AdoptInboxTest)
# Shared images: the registry (memfd-backed on a host), identification of a descriptor that
# crossed a socket, session holders, and the inbox keeping their descriptors apart from T0's.
list(APPEND MOBILEGL_WIRE_TESTS SharedImageTest)
# P9 W3: the graded log forward - the level mapping both ways, the logger's level reaching the
# forwarder, and the channel against a transport whose writes can be held (and a real socket
# pair nobody reads). POSIX for the socket pair.
+158
View File
@@ -0,0 +1,158 @@
// MobileGL - MobileGL/MG_Test/Wire/SharedImageTest.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
// Shared images (docs/Disaggregated/notes/anland/plan-ahb-dmabuf.md): the server's registry, a
// session's references, and the aux-socket inbox that carries an import's descriptor beside T0's
// Offers. On a host the image memory is a memfd, which is enough for everything here: what is
// under test is that a descriptor which travelled through SCM_RIGHTS still names its image.
#include <MG_Pipe/MGPipeTypes.h>
#include <MG_Remote/Server/AdoptInbox.h>
#include <MG_Remote/Server/SharedImageRegistry.h>
#include <MG_Remote/Transport/AdoptT0.h>
#include <MG_Remote/Transport/FdPassing.h>
#include <MG_Remote/Transport/SocketTransport.h>
#include <gtest/gtest.h>
#include <memory>
#include <string>
#include <sys/mman.h>
#include <unistd.h>
using namespace MobileGL;
using namespace MobileGL::MG_Remote;
namespace SI = MobileGL::MG_Remote::Server::SharedImages;
namespace {
// A descriptor for `image` as a peer process would hold it: through a real socket pair.
int ThroughSocket(int fd) {
std::unique_ptr<Transport::SocketTransport> a, b;
EXPECT_EQ(Transport::SocketTransport::CreatePair(a, b), MOBILEGL_OK);
MG_Pipe::MGPSharedImageFdOffer offer{MG_Pipe::kMGPSharedImageFdMagic, 1, 1, 0, 0};
EXPECT_EQ(a->ShareFd(fd, MobileGLByteSpan{&offer, sizeof(offer)}), MOBILEGL_OK);
int received = -1;
Uint8 sideband[Transport::FdPassing::kMaxSidebandBytes] = {};
std::uint64_t size = 0;
EXPECT_EQ(b->ReceiveFd(&received, MobileGLMutableByteSpan{sideband, sizeof(sideband)}, &size, 1000),
MOBILEGL_OK);
return received;
}
} // namespace
TEST(SharedImage, AnExportedDescriptorIdentifiesItsImageAfterCrossingASocket) {
std::string why;
SI::ImageRef image = SI::Allocate(64, 32, SI::kFourccAbgr8888, why);
ASSERT_NE(image, nullptr) << why;
EXPECT_EQ(image->Width, 64u);
EXPECT_EQ(image->Height, 32u);
EXPECT_GE(image->Stride, 64u * 4u);
EXPECT_EQ(image->Modifier, SI::kModifierInvalid);
const int peer = ThroughSocket(image->Fd);
ASSERT_GE(peer, 0);
SI::ImageRef found = SI::Identify(peer, why);
::close(peer);
ASSERT_NE(found, nullptr) << why;
EXPECT_EQ(found->Id, image->Id);
EXPECT_EQ(SI::Find(image->Id), image);
}
// The negative control: a descriptor of the same kind this server never exported is refused.
TEST(SharedImage, AForeignDescriptorIsNotIdentified) {
std::string why;
SI::ImageRef image = SI::Allocate(16, 16, SI::kFourccXbgr8888, why);
ASSERT_NE(image, nullptr) << why;
const int foreign = ::memfd_create("not-a-shared-image", MFD_CLOEXEC);
ASSERT_GE(foreign, 0);
EXPECT_EQ(SI::Identify(foreign, why), nullptr);
EXPECT_FALSE(why.empty());
::close(foreign);
}
TEST(SharedImage, AnImageDiesWithItsLastReferenceAndIsNoLongerIdentified) {
std::string why;
SI::ImageRef image = SI::Allocate(8, 8, SI::kFourccAbgr8888, why);
ASSERT_NE(image, nullptr) << why;
const Uint64 id = image->Id;
const int kept = ::dup(image->Fd);
const SizeT live = SI::LiveCount();
image.reset();
EXPECT_EQ(SI::LiveCount(), live - 1);
EXPECT_EQ(SI::Find(id), nullptr);
EXPECT_EQ(SI::Identify(kept, why), nullptr);
::close(kept);
}
TEST(SharedImage, UnsupportedFormatsAndSizesAreRefused) {
std::string why;
EXPECT_EQ(SI::Allocate(16, 16, SI::FourCC('N', 'V', '1', '2'), why), nullptr);
EXPECT_EQ(SI::Allocate(0, 16, SI::kFourccAbgr8888, why), nullptr);
EXPECT_EQ(SI::Allocate(16, 1u << 20, SI::kFourccAbgr8888, why), nullptr);
}
// A session's holder keeps an image alive until its release or the session's end, and releases
// only its own reference: another holder's stays.
TEST(SharedImage, AHolderKeepsItsImagesUntilReleasedOrCleared) {
std::string why;
SI::SessionHolder producer, consumer;
SI::ImageRef image = SI::Allocate(32, 32, SI::kFourccAbgr8888, why);
ASSERT_NE(image, nullptr) << why;
const Uint64 id = image->Id;
producer.Hold(image);
consumer.Hold(image);
image.reset();
EXPECT_NE(SI::Find(id), nullptr);
EXPECT_TRUE(producer.Release(id));
EXPECT_FALSE(producer.Release(id));
EXPECT_NE(SI::Find(id), nullptr);
EXPECT_NE(consumer.Get(id), nullptr);
consumer.Clear();
EXPECT_EQ(consumer.Count(), 0u);
EXPECT_EQ(SI::Find(id), nullptr);
}
// The aux socket carries T0 Offers and shared-image descriptors at once; each Take finds its own
// kind for its own record, keeping the other for later.
TEST(SharedImage, TheInboxKeepsSharedImageAndT0DescriptorsApart) {
std::unique_ptr<Transport::SocketTransport> client, server;
ASSERT_EQ(Transport::SocketTransport::CreatePair(client, server), MOBILEGL_OK);
Server::AdoptInbox inbox;
inbox.Attach(server.get());
std::string why;
SI::ImageRef image = SI::Allocate(16, 16, SI::kFourccAbgr8888, why);
ASSERT_NE(image, nullptr) << why;
// The T0 Offer for the LATER record (6) arrives first; the import (5) is awaited first, as
// records apply in order, and the Offer must be kept for its own record.
int pipeFds[2] = {-1, -1};
ASSERT_EQ(::pipe(pipeFds), 0);
Transport::AdoptT0::Offer t0{};
t0.magic = Transport::AdoptT0::kOfferMagic;
t0.version = Transport::AdoptT0::kOfferVersion;
t0.seq = 6;
ASSERT_EQ(client->ShareFd(pipeFds[0], MobileGLByteSpan{&t0, sizeof(t0)}), MOBILEGL_OK);
MG_Pipe::MGPSharedImageFdOffer offer{MG_Pipe::kMGPSharedImageFdMagic, 1, 5, 0, 0};
ASSERT_EQ(client->ShareFd(image->Fd, MobileGLByteSpan{&offer, sizeof(offer)}), MOBILEGL_OK);
::close(pipeFds[0]);
::close(pipeFds[1]);
int fd = -1;
ASSERT_EQ(inbox.TakeSharedImageFd(5, 1000, &fd, why), Server::AdoptInbox::Outcome::Taken) << why;
SI::ImageRef found = SI::Identify(fd, why);
::close(fd);
ASSERT_NE(found, nullptr) << why;
EXPECT_EQ(found->Id, image->Id);
EXPECT_EQ(inbox.Kept(), 1u);
Transport::AdoptT0::Offer taken{};
ASSERT_EQ(inbox.Take(6, 0, &fd, &taken, why), Server::AdoptInbox::Outcome::Taken) << why;
EXPECT_EQ(taken.seq, 6u);
::close(fd);
// A shared-image record whose descriptor never comes is a bounded wait, not a hang.
EXPECT_EQ(inbox.TakeSharedImageFd(7, 20, &fd, why), Server::AdoptInbox::Outcome::TimedOut);
}