Files
reims-vgpu/guest/kext/tests/display_modes_test.cpp
T
Anees Iqbal 2d5ff0b8eb 🐛 Answer a mode the framebuffer lacks as Apple's framebuffer does
WindowServer asks the framebuffer for the pixel information of mode 0. Apple's
paravirtual framebuffer (AppleParavirtFramebuffer, macOS 26.7.1, read from its
code) numbers its modes 0 to its count - 1, so mode 0 is a mode there. Ours are
never 0, and every call that names a mode we do not list answered
kIOReturnBadArgument, a code Apple's framebuffer never gives for one.

What the guest now sees, for a mode the framebuffer does not list, or a depth
other than its one:
- getPixelInformation answers kIOReturnUnsupportedMode, as Apple's does, and as
  ours already did for a depth or an aperture it lacks;
- getInformationForDisplayMode, getTimingInfoForDisplayMode, setDisplayMode
  and setStartupDisplayMode answer kIOReturnError, as Apple's do (set and
  setStartup answered kIOReturnUnsupportedMode for a depth before).
A null information pointer still answers kIOReturnBadArgument.
2026-11-05 21:59:31 -08:00

218 lines
9.1 KiB
C++

// The native test of RgfxDisplayModes.hpp: across a change of the device's modes at any
// moment of WindowServer's display setup, the framebuffer's answers stay consistent: the mode
// it answers as current is always one it lists, the startup mode follows the device, a change
// before IOFramebuffer registers its connect interrupt fires once it does, and the mode
// configuration a WindowServer left goes at the next server connection after a change.
#include "RgfxDisplayModes.hpp"
#include <cstdio>
#include <cstdlib>
namespace {
int checks = 0;
void check(bool ok, const char *what)
{
checks++;
if (!ok) {
std::fprintf(stderr, "display_modes_test: FAILED: %s\n", what);
std::exit(1);
}
}
// The OS's timing, which the rules only keep.
struct Timing {
uint32_t tag;
};
constexpr uint32_t kMax = 4;
using Modes = RgfxDisplayModes<Timing, kMax>;
constexpr int32_t kReservedBase = static_cast<int32_t>(0x80000000u);
constexpr int32_t kProgrammed = static_cast<int32_t>(0x80001000u);
// The host's modes: the window before its first draw, and its size after, at 120 Hz.
constexpr RgfxMode kOffered = {0, 1920, 1080, 120000};
constexpr RgfxMode kWindow = {0, 3840, 2400, 120000};
constexpr RgfxMode kResized = {0, 2560, 1600, 120000};
bool lists(const Modes &m, int32_t id)
{
int32_t ids[kMax + 1];
uint32_t n = m.list(ids);
for (uint32_t i = 0; i < n; i++) {
if (ids[i] == id)
return true;
}
return false;
}
// What WindowServer reads at its display setup: the mode set (getCurrentDisplayMode), which
// the list holds (getDisplayModes) and whose information the framebuffer gives.
bool consistent(const Modes &m)
{
int32_t current = m.haveCurrent ? m.current.id : m.startupId;
RgfxMode info;
return lists(m, current) && m.modeFor(current, &info) && m.modeFor(m.startupId, &info);
}
// The framebuffer's start: the device's first list, its startup mode set (enableController).
Modes started(const RgfxMode &first)
{
Modes m;
m.reset();
check(m.take(&first, 1, 0), "the first list is a change");
check(m.startupId == m.preferredId && m.preferredId != 0, "the start takes the preferred mode");
RgfxMode startup;
check(m.modeFor(m.startupId, &startup), "the startup mode has its information");
m.set(startup);
check(consistent(m), "the started framebuffer's answers are consistent");
return m;
}
void aChangeBeforeTheConnectInterruptIsRegistered()
{
// The window draws only after the start's GET_DISPLAYS, so the device's list changes right
// after the start's first mode is set, before IOFramebuffer registers its connect
// interrupt; WindowServer is already waiting to open the framebuffer.
Modes m = started(kOffered);
int32_t offered = m.current.id;
RgfxConnectChange connect = {};
check(m.take(&kWindow, 1, 0), "the window's size is a change");
check(!connect.changed(false), "no interrupt registered: the change waits");
int32_t window = m.preferredId;
check(window != offered, "the window's mode has an ID of its own");
check(m.current.id == offered && lists(m, offered), "the mode set stays listed");
check(lists(m, window), "the device's mode is listed");
check(m.list(nullptr) == 2, "both, and nothing else");
check(m.startupId == window, "the startup mode follows the device");
check(consistent(m), "the answers stay consistent");
RgfxMode info;
check(m.modeFor(offered, &info) && !m.isPreferred(info), "the mode set is not preferred");
check(m.modeFor(window, &info) && m.isPreferred(info), "the device's mode is");
// IOFramebuffer registers the interrupt, off, then turns it on: the change fires once.
check(!connect.armed(false), "registered off: it still waits");
check(connect.armed(true), "on: the change fires");
check(!connect.armed(true), "once");
// WindowServer sets the device's mode: the old one is no longer listed.
RgfxMode shown;
check(m.modeFor(window, &shown), "the window's mode has its information");
m.set(shown);
check(!lists(m, offered) && m.list(nullptr) == 1, "the old mode drops once another is set");
check(consistent(m), "and the answers stay consistent");
}
void aChangeBetweenTheCurrentModeAndTheList()
{
// WindowServer reads the count and the mode set, then the list changes before it reads
// the list: the list it is given in the count it was told still names the mode set.
Modes m = started(kWindow);
int32_t window = m.current.id;
uint32_t reported = m.list(nullptr);
check(reported == 1, "one mode reported");
RgfxConnectChange connect = {};
check(m.take(&kResized, 1, 0), "the resize is a change");
check(connect.changed(true), "the registered interrupt fires at once");
int32_t out[kMax + 1] = {};
m.fill(out, reported);
check(out[0] == window, "the reported list holds the mode set");
// The connect interrupt has WindowServer ask again: both, the device's preferred.
reported = m.list(nullptr);
check(reported == 2, "the mode set and the device's mode");
m.fill(out, reported);
check((out[0] == window || out[1] == window) && out[0] != out[1], "each once");
check(m.startupId == m.preferredId && m.preferredId != window, "the startup follows");
check(consistent(m), "the answers stay consistent");
}
void aChangeAfterTheStartupModeIsSet()
{
// WindowServer programs the window's timing, sets it and makes it the startup mode; then
// the window is resized, and that WindowServer ends before it takes the change in.
Modes m = started(kWindow);
RgfxModeConfig config = {};
config.listChanged(); // the start's first list
check(config.serverOpens(), "the first server connection: nothing left to keep");
Timing timing = {7};
m.clearProgrammed();
check(m.addProgrammed(kProgrammed, kWindow, timing, kReservedBase), "the timing is programmed");
check(!m.addProgrammed(kProgrammed, kWindow, timing, kReservedBase), "an ID once");
check(!m.addProgrammed(5, kWindow, timing, kReservedBase), "only from the reserved base");
m.followProgrammed();
check(m.current.id == kProgrammed, "the mode set takes the programmed ID");
check(m.list(nullptr) == 1 && lists(m, kProgrammed), "the list is the programmed timing");
RgfxMode set;
check(m.modeFor(kProgrammed, &set) && m.isPreferred(set), "which is the preferred mode's");
m.set(set);
m.startupId = kProgrammed;
check(consistent(m), "consistent before the change");
check(m.take(&kResized, 1, 0), "the resize is a change");
config.listChanged();
check(lists(m, kProgrammed), "the programmed mode set stays listed");
check(lists(m, m.preferredId), "the device's new mode is listed");
check(m.list(nullptr) == 2, "both, and nothing else");
check(m.startupId == m.preferredId, "the startup mode follows the device");
check(consistent(m), "the answers stay consistent");
const Modes::Programmed *p = m.programmedFor(kProgrammed);
check(p && p->timing.tag == 7, "the programmed timing is kept");
// The next WindowServer's server connection drops the configuration made from the old list.
check(config.serverOpens(), "a changed list: the configuration goes");
check(!config.serverOpens(), "an unchanged one: it stays");
}
void idsStayWithTheirModes()
{
Modes m = started(kWindow);
int32_t window = m.current.id;
RgfxMode both[2] = {kResized, kWindow};
check(m.take(both, 2, 0), "a new mode listed is a change");
int32_t resized = m.preferredId;
check(resized != window, "the new mode has a new ID");
check(lists(m, window) && m.list(nullptr) == 2, "a listed mode keeps its ID");
check(!m.take(both, 2, 0), "the same list again is no change");
check(m.take(both, 2, 1), "another preferred mode is a change");
check(m.preferredId == window, "the preferred mode is the one named");
check(m.take(&kResized, 1, 7), "a dropped mode is a change");
check(m.preferredId == resized, "no preferred mode named: the first");
check(m.modeCount == 1 && m.current.id == window, "the mode set stays set");
check(m.take(both, 2, 0), "listed again");
check(m.modes[1].id == window, "the mode set's mode takes back its ID");
}
// A mode or depth the framebuffer does not hold is answered as Apple's paravirtual framebuffer
// answers it: kIOReturnUnsupportedMode for the pixel information, kIOReturnError elsewhere.
void anUnknownModeIsAnsweredAsApplesFramebufferDoes()
{
check(rgfxNoSuchModeStatus(RgfxModeCall::PixelInformation) == 0xe00002e6u,
"the pixel information of no mode: kIOReturnUnsupportedMode");
check(rgfxNoSuchModeStatus(RgfxModeCall::ModeInformation) == 0xe00002bcu,
"the mode information of no mode: kIOReturnError");
check(rgfxNoSuchModeStatus(RgfxModeCall::TimingInformation) == 0xe00002bcu,
"the timing information of no mode: kIOReturnError");
check(rgfxNoSuchModeStatus(RgfxModeCall::SetMode) == 0xe00002bcu,
"setting no mode: kIOReturnError");
check(rgfxNoSuchModeStatus(RgfxModeCall::SetStartupMode) == 0xe00002bcu,
"setting no startup mode: kIOReturnError");
Modes m = started(kWindow);
RgfxMode none;
check(!m.modeFor(0, &none), "mode 0, which WindowServer asks about, is none of ours");
}
} // namespace
int main()
{
aChangeBeforeTheConnectInterruptIsRegistered();
aChangeBetweenTheCurrentModeAndTheList();
aChangeAfterTheStartupModeIsSet();
idsStayWithTheirModes();
anUnknownModeIsAnsweredAsApplesFramebufferDoes();
std::printf("display_modes_test: OK (%d checks)\n", checks);
return 0;
}