mirror of
https://github.com/MobileGL-Dev/MobileGL
synced 2026-11-18 15:33:22 +09:00
[Feature] (Remote): expose a package-neutral embedded display server API
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
#include "EmbeddedServer.h"
|
||||
#include "ServerDisplay.h"
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
#include <android/native_window.h>
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
using namespace MobileGL::MG_Remote::Server;
|
||||
|
||||
int DetachResult(ServerWindowDetach result) {
|
||||
switch (result) {
|
||||
case ServerWindowDetach::NoWindow: return MOBILEGL_SERVER_WINDOW_NO_WINDOW;
|
||||
case ServerWindowDetach::Released: return MOBILEGL_SERVER_WINDOW_RELEASED;
|
||||
case ServerWindowDetach::ReleasedBySession: return MOBILEGL_SERVER_WINDOW_RELEASED_BY_SESSION;
|
||||
case ServerWindowDetach::TimedOut: return MOBILEGL_SERVER_WINDOW_TIMED_OUT;
|
||||
}
|
||||
return MOBILEGL_SERVER_WINDOW_TIMED_OUT;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int mobilegl_server_display_install(const struct mobilegl_server_display_hooks* hooks) {
|
||||
if (hooks == nullptr || hooks->acquire == nullptr || hooks->release == nullptr)
|
||||
return MOBILEGL_SERVER_DISPLAY_INVALID_ARGUMENT;
|
||||
auto& display = ServerDisplayInstance();
|
||||
if (display.HasDisplay() || display.Attached() || display.Leased())
|
||||
return MOBILEGL_SERVER_DISPLAY_ALREADY_INSTALLED;
|
||||
ServerDisplayHooks nativeHooks;
|
||||
nativeHooks.acquire = hooks->acquire;
|
||||
nativeHooks.release = hooks->release;
|
||||
nativeHooks.requestGeometry = hooks->request_geometry;
|
||||
nativeHooks.user = hooks->user;
|
||||
display.Install(nativeHooks);
|
||||
return MOBILEGL_SERVER_DISPLAY_OK;
|
||||
}
|
||||
|
||||
extern "C" int mobilegl_server_display_install_android(mobilegl_server_geometry_callback request_geometry, void* user) {
|
||||
#if defined(__ANDROID__)
|
||||
const auto androidHooks = AndroidNativeWindowHooks(request_geometry, user);
|
||||
const mobilegl_server_display_hooks hooks{androidHooks.acquire, androidHooks.release,
|
||||
androidHooks.requestGeometry, androidHooks.user};
|
||||
return mobilegl_server_display_install(&hooks);
|
||||
#else
|
||||
(void)request_geometry;
|
||||
(void)user;
|
||||
return MOBILEGL_SERVER_DISPLAY_UNSUPPORTED_PLATFORM;
|
||||
#endif
|
||||
}
|
||||
|
||||
extern "C" int mobilegl_server_display_attach(void* window, uint32_t width, uint32_t height) {
|
||||
if (window == nullptr) return MOBILEGL_SERVER_DISPLAY_INVALID_ARGUMENT;
|
||||
auto& display = ServerDisplayInstance();
|
||||
if (!display.HasDisplay()) return MOBILEGL_SERVER_DISPLAY_NOT_INSTALLED;
|
||||
display.Attach(window, width, height);
|
||||
return MOBILEGL_SERVER_DISPLAY_OK;
|
||||
}
|
||||
|
||||
extern "C" int mobilegl_server_display_attach_android(struct ANativeWindow* window, uint32_t width, uint32_t height) {
|
||||
#if defined(__ANDROID__)
|
||||
if (window == nullptr) return MOBILEGL_SERVER_DISPLAY_INVALID_ARGUMENT;
|
||||
if (width == 0) {
|
||||
const int32_t actual = ANativeWindow_getWidth(window);
|
||||
if (actual > 0) width = static_cast<uint32_t>(actual);
|
||||
}
|
||||
if (height == 0) {
|
||||
const int32_t actual = ANativeWindow_getHeight(window);
|
||||
if (actual > 0) height = static_cast<uint32_t>(actual);
|
||||
}
|
||||
return mobilegl_server_display_attach(window, width, height);
|
||||
#else
|
||||
(void)window;
|
||||
(void)width;
|
||||
(void)height;
|
||||
return MOBILEGL_SERVER_DISPLAY_UNSUPPORTED_PLATFORM;
|
||||
#endif
|
||||
}
|
||||
|
||||
extern "C" int mobilegl_server_display_detach(uint32_t timeout_ms) {
|
||||
return DetachResult(ServerDisplayInstance().Detach(timeout_ms));
|
||||
}
|
||||
|
||||
extern "C" int mobilegl_server_display_uninstall(uint32_t timeout_ms) {
|
||||
auto& display = ServerDisplayInstance();
|
||||
const int result = DetachResult(display.Detach(timeout_ms));
|
||||
// Detach has already removed the attached window even if its lease is still live. Uninstall
|
||||
// keeps the original hooks for EndLease's deferred release, without another timeout budget.
|
||||
display.Uninstall(0);
|
||||
return result;
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// Public C interface for an application that owns a MobileGL split server.
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#if defined(_WIN32)
|
||||
#define MOBILEGL_SERVER_EXPORT
|
||||
#else
|
||||
#define MOBILEGL_SERVER_EXPORT __attribute__((visibility("default")))
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
struct ANativeWindow;
|
||||
|
||||
typedef void (*mobilegl_server_geometry_callback)(void* user, uint32_t width, uint32_t height);
|
||||
|
||||
// Opaque-window hooks also let non-Android hosts embed the display without depending on
|
||||
// MobileGL's C++ headers. Both reference callbacks are required. Geometry may be null.
|
||||
struct mobilegl_server_display_hooks {
|
||||
void (*acquire)(void* user, void* window);
|
||||
void (*release)(void* user, void* window);
|
||||
mobilegl_server_geometry_callback request_geometry;
|
||||
void* user;
|
||||
};
|
||||
|
||||
enum mobilegl_server_display_result {
|
||||
MOBILEGL_SERVER_DISPLAY_OK = 0,
|
||||
MOBILEGL_SERVER_DISPLAY_INVALID_ARGUMENT = -1,
|
||||
MOBILEGL_SERVER_DISPLAY_ALREADY_INSTALLED = -2,
|
||||
MOBILEGL_SERVER_DISPLAY_NOT_INSTALLED = -3,
|
||||
MOBILEGL_SERVER_DISPLAY_UNSUPPORTED_PLATFORM = -4
|
||||
};
|
||||
|
||||
enum mobilegl_server_window_detach {
|
||||
MOBILEGL_SERVER_WINDOW_NO_WINDOW = 0,
|
||||
MOBILEGL_SERVER_WINDOW_RELEASED = 1,
|
||||
MOBILEGL_SERVER_WINDOW_RELEASED_BY_SESSION = 2,
|
||||
MOBILEGL_SERVER_WINDOW_TIMED_OUT = 3
|
||||
};
|
||||
|
||||
// Install once before serving; owner lifecycle calls must be serialized. A live display or
|
||||
// an old lease awaiting release refuses a replacement, so its window always uses its original
|
||||
// reference hooks. The geometry callback runs on the apply thread and must return promptly.
|
||||
// A positive size requests fixed buffer dimensions; 0/0 requests the host's layout dimensions.
|
||||
// Report the resulting dimensions with attach, for example from SurfaceHolder.surfaceChanged.
|
||||
// Callback user data must live until uninstall has returned and any timed-out lease has ended.
|
||||
MOBILEGL_SERVER_EXPORT int mobilegl_server_display_install(const struct mobilegl_server_display_hooks* hooks);
|
||||
MOBILEGL_SERVER_EXPORT int mobilegl_server_display_install_android(mobilegl_server_geometry_callback request_geometry,
|
||||
void* user);
|
||||
|
||||
// Attach retains its own reference. Attaching the same window updates its dimensions without
|
||||
// another retain. A different window detaches the previous one first (up to 3 seconds).
|
||||
// Android attach uses ANativeWindow_getWidth/Height for zero dimensions; the caller may release
|
||||
// its ANativeWindow_fromSurface reference immediately after this call.
|
||||
MOBILEGL_SERVER_EXPORT int mobilegl_server_display_attach(void* window, uint32_t width, uint32_t height);
|
||||
MOBILEGL_SERVER_EXPORT int mobilegl_server_display_attach_android(struct ANativeWindow* window, uint32_t width,
|
||||
uint32_t height);
|
||||
|
||||
// Returns mobilegl_server_window_detach. A timeout never drops a reference while the backend
|
||||
// is rendering: release happens when the apply thread ends its lease. Surface loss currently
|
||||
// ends the live split session; a fresh client session can use a newly attached window.
|
||||
MOBILEGL_SERVER_EXPORT int mobilegl_server_display_detach(uint32_t timeout_ms);
|
||||
MOBILEGL_SERVER_EXPORT int mobilegl_server_display_uninstall(uint32_t timeout_ms);
|
||||
|
||||
// Blocking serve on the caller's thread; stop is non-blocking and safe on the UI thread.
|
||||
// Endpoint: filesystem path, @abstract AF_UNIX name (SCM_RIGHTS + shared-memory rings), or
|
||||
// tcp://host:port. One client session per process; independent workers need distinct endpoints.
|
||||
// Before loading libMobileGL set MOBILEGL_IPC_ROLE=server and MOBILEGL_IPC_DIAL=no, and unset
|
||||
// MOBILEGL_TRANSPORT, MOBILEGL_IPC_SERVER_PATH, MOBILEGL_IPC_RING_MB, MOBILEGL_IPC_STAGE_MB.
|
||||
MOBILEGL_SERVER_EXPORT int mobilegl_server_serve_inprocess(const char* endpoint);
|
||||
MOBILEGL_SERVER_EXPORT void mobilegl_server_stop_inprocess(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user