Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions core/app/glfw_app_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ struct WindowState : app::AppRunner {
struct ManagedWindow {
GLFWwindow* window = nullptr;
WindowState state;
bool shown = false;
app::DslWindowRuntime content;
std::unique_ptr<core::render::RenderBackend> renderBackend;
};
Expand Down Expand Up @@ -86,6 +87,12 @@ float getPointerScale(GLFWwindow* window) {
return (scaleX + scaleY) * 0.5f;
}

app::MainWindowMetrics windowMetrics(GLFWwindow* window) {
int width = 0, height = 0;
glfwGetFramebufferSize(window, &width, &height);
return {width, height, getDpiScale(window), getPointerScale(window)};
}

GLFWmonitor* getWindowMonitor(GLFWwindow* window) {
if (GLFWmonitor* monitor = glfwGetWindowMonitor(window)) {
return monitor;
Expand Down Expand Up @@ -247,6 +254,7 @@ std::unique_ptr<ManagedWindow> createManagedWindow(const app::DslWindowRequest&
GLFWwindow* parentWindow,
core::render::RenderBackend& shareBackend) {
core::window::WindowCreateRequest windowRequest;
windowRequest.visible = false;
windowRequest.width = request.width;
windowRequest.height = request.height;
windowRequest.title = request.title.c_str();
Expand Down Expand Up @@ -346,6 +354,14 @@ bool updateManagedWindow(ManagedWindow& managed, float deltaSeconds, bool update
managed.state.paintRequested = true;
}

const app::MainWindowMetrics metrics{framebufferWidth, framebufferHeight, dpiScale, pointerScale};
if (!(metrics == windowMetrics(managed.window))) {
managed.state.paintRequested = true;
managed.content.requestFullPaint();
core::platform::requestFrame();
return true;
}

if (managed.state.paintRequested || managed.content.paintRequested()) {
managed.renderBackend->beginFrame({
managed.window,
Expand All @@ -355,6 +371,15 @@ bool updateManagedWindow(ManagedWindow& managed, float deltaSeconds, bool update
dpiScale
});
managed.content.render(*managed.renderBackend, framebufferWidth, framebufferHeight, dpiScale);
if (!managed.shown) {
glfwShowWindow(managed.window);
managed.shown = true;
managed.content.requestFullPaint();
core::platform::requestFrame();
if (managed.content.request().modal) {
glfwFocusWindow(managed.window);
}
}
managed.renderBackend->present();
managed.state.paintRequested = false;
++managed.state.renderedFrames;
Expand Down Expand Up @@ -412,6 +437,7 @@ int eui_app_run() {
TimerResolutionGuard timerResolution;

core::window::WindowCreateRequest windowRequest;
windowRequest.visible = false;
windowRequest.width = app::initialWindowWidth();
windowRequest.height = app::initialWindowHeight();
windowRequest.x = app::initialWindowX();
Expand Down Expand Up @@ -467,7 +493,11 @@ int eui_app_run() {
cleanupMainWindow();
return -1;
}
app::MainWindowRuntime mainWindowRuntime(windowState);
app::MainWindowRuntime mainWindowRuntime(windowState, [&] {
glfwShowWindow(window);
app::detail::requestFullPaint();
core::platform::requestFrame();
});
windowState.initializeTray();
glfwSetWindowCloseCallback(window, [](GLFWwindow* currentWindow) {
WindowState* state = static_cast<WindowState*>(glfwGetWindowUserPointer(currentWindow));
Expand Down Expand Up @@ -552,14 +582,12 @@ int eui_app_run() {
continue;
}

const float dpiScale = getDpiScale(window);
const float pointerScale = getPointerScale(window);
const bool mainInputEnabled = windowState.modalChildWindow == nullptr;

mainWindowRuntime.runFrame(
window,
*renderBackend,
{framebufferWidth, framebufferHeight, dpiScale, pointerScale},
[&] { return windowMetrics(window); },
currentFrameTime,
getWindowRefreshRate(window),
mainInputEnabled,
Expand Down
59 changes: 47 additions & 12 deletions core/app/main_window_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "core/window/window_backend.h"

#include <chrono>
#include <functional>
#include <thread>
#include <utility>

Expand All @@ -20,19 +21,25 @@ struct MainWindowMetrics {
float dpiScale = 1.0f;
float pointerScale = 1.0f;

bool operator==(const MainWindowMetrics& other) const {
return framebufferWidth == other.framebufferWidth && framebufferHeight == other.framebufferHeight &&
dpiScale == other.dpiScale && pointerScale == other.pointerScale;
}

bool valid() const {
return framebufferWidth > 0 && framebufferHeight > 0 && dpiScale > 0.0f && pointerScale > 0.0f;
}
};

class MainWindowRuntime {
public:
explicit MainWindowRuntime(AppRunner& runner) : runner_(runner) {}
explicit MainWindowRuntime(AppRunner& runner, std::function<void()> firstFrameReady = {})
: runner_(runner), firstFrameReady_(std::move(firstFrameReady)) {}

template <typename AfterUpdateFn, typename UpdateChildrenFn, typename SetTitleFn, typename ChildAnimatingFn>
template <typename ReadMetricsFn, typename AfterUpdateFn, typename UpdateChildrenFn, typename SetTitleFn, typename ChildAnimatingFn>
void runFrame(core::window::Handle window,
core::render::RenderBackend& renderBackend,
const MainWindowMetrics& metrics,
ReadMetricsFn&& readMetrics,
double now,
double refreshRate,
bool inputEnabled,
Expand All @@ -48,7 +55,7 @@ class MainWindowRuntime {
updateRequested ||
frameWakeRequested ||
runner_.anyAnimating(childAnimating()) ||
core::hasPendingPointerInput(window, metrics.pointerScale);
core::hasPendingPointerInput(window, readMetrics().pointerScale);
while (frameRequested) {
const double remaining = runner_.nextFrameTime - core::window::timeSeconds();
if (remaining <= 0.0) {
Expand All @@ -62,11 +69,12 @@ class MainWindowRuntime {

updateAndRender(window,
renderBackend,
metrics,
readMetrics(),
deltaSeconds,
updateRequested,
inputEnabled,
std::forward<AfterUpdateFn>(afterUpdate));
std::forward<AfterUpdateFn>(afterUpdate),
readMetrics);

updateChildren(deltaSeconds, updateRequested);
runner_.updateFrameTitle(core::window::timeSeconds(), std::forward<SetTitleFn>(setTitle));
Expand All @@ -78,14 +86,15 @@ class MainWindowRuntime {
runner_.resetTiming(now);
}

template <typename AfterUpdateFn>
template <typename AfterUpdateFn, typename ReadMetricsFn>
bool updateAndRender(core::window::Handle window,
core::render::RenderBackend& renderBackend,
const MainWindowMetrics& metrics,
float deltaSeconds,
bool updateRequested,
bool inputEnabled,
AfterUpdateFn&& afterUpdate) {
AfterUpdateFn&& afterUpdate,
ReadMetricsFn&& readMetrics) {
if (!metrics.valid()) {
runner_.paintRequested = true;
return false;
Expand All @@ -106,6 +115,25 @@ class MainWindowRuntime {

afterUpdate();

// Input/compose callbacks may resize the drawable. Recompose once with
// fresh metrics and zero elapsed time; consumed input is not replayed.
MainWindowMetrics renderMetrics = readMetrics();
if (!(metrics == renderMetrics)) {
runner_.paintRequested = true;
app::detail::requestFullPaint();
if (renderMetrics.valid()) {
renderBackend.makeCurrent();
app::update(window, 0.0f, renderMetrics.framebufferWidth, renderMetrics.framebufferHeight,
renderMetrics.dpiScale, renderMetrics.pointerScale, false, inputEnabled);
}
// A second geometry change is deferred rather than spinning inside
// one frame. Keep both the repaint and an explicit wake for idle apps.
if (!renderMetrics.valid() || !(renderMetrics == readMetrics())) {
core::platform::requestFrame();
return false;
}
}

if (!runner_.paintRequested) {
return false;
}
Expand All @@ -114,11 +142,17 @@ class MainWindowRuntime {
renderBackend.beginFrame({
window,
core::window::nativeWindowInfo(window),
metrics.framebufferWidth,
metrics.framebufferHeight,
metrics.dpiScale
renderMetrics.framebufferWidth,
renderMetrics.framebufferHeight,
renderMetrics.dpiScale
});
app::render(metrics.framebufferWidth, metrics.framebufferHeight, metrics.dpiScale);
app::render(renderMetrics.framebufferWidth, renderMetrics.framebufferHeight, renderMetrics.dpiScale);
// Map only after drawing, immediately before swapping the prepared
// buffer. Swapping while hidden can discard its contents on X11.
if (firstFrameReady_) {
auto ready = std::move(firstFrameReady_);
ready();
}
renderBackend.present();
core::render::publishRenderFrameStats();
runner_.recordRenderStats(core::render::lastRenderFrameStats());
Expand All @@ -130,6 +164,7 @@ class MainWindowRuntime {

private:
AppRunner& runner_;
std::function<void()> firstFrameReady_;
};

} // namespace app
36 changes: 32 additions & 4 deletions core/app/sdl2_app_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct WindowState : app::AppRunner {
struct ManagedWindow {
SDL_Window* window = nullptr;
bool closeRequested = false;
bool shown = false;
SDL_Window* parentWindow = nullptr;
app::DslWindowRuntime content;
std::unique_ptr<core::render::RenderBackend> renderBackend;
Expand Down Expand Up @@ -129,6 +130,12 @@ float dpiScale(SDL_Window* window) {
return x11Scale > 0.0f ? x11Scale : drawableRatio;
}

app::MainWindowMetrics windowMetrics(SDL_Window* window) {
int width = 0, height = 0;
getDrawableSize(window, width, height);
return {width, height, dpiScale(window), pointerScale(window)};
}

void attachNativeChildWindow(SDL_Window* parentWindow, SDL_Window* childWindow) {
eui_set_native_child_window(core::window::nativeWindowInfo(parentWindow).platformWindow,
core::window::nativeWindowInfo(childWindow).platformWindow,
Expand Down Expand Up @@ -436,6 +443,7 @@ std::unique_ptr<ManagedWindow> createManagedWindow(const app::DslWindowRequest&
SDL_Window* parentWindow,
core::render::RenderBackend& shareBackend) {
core::window::WindowCreateRequest windowRequest;
windowRequest.visible = false;
windowRequest.width = request.width;
windowRequest.height = request.height;
windowRequest.title = request.title.c_str();
Expand Down Expand Up @@ -568,6 +576,13 @@ bool updateManagedWindow(ManagedWindow& managed, float deltaSeconds, bool update

core::render::ScopedRenderBackend scopedRenderBackend(*managed.renderBackend);
managed.content.update(managed.window, deltaSeconds, logicalWidth, logicalHeight, pointer, dpi, updateRequested);
const app::MainWindowMetrics metrics{drawableWidth, drawableHeight, dpi, pointer};
if (!(metrics == windowMetrics(managed.window))) {
managed.content.requestFullPaint();
core::platform::requestFrame();
return true;
}

if (managed.content.paintRequested()) {
managed.renderBackend->beginFrame({
managed.window,
Expand All @@ -577,6 +592,15 @@ bool updateManagedWindow(ManagedWindow& managed, float deltaSeconds, bool update
dpi
});
managed.content.render(*managed.renderBackend, drawableWidth, drawableHeight, dpi);
if (!managed.shown) {
SDL_ShowWindow(managed.window);
managed.shown = true;
managed.content.requestFullPaint();
core::platform::requestFrame();
if (managed.content.request().modal) {
SDL_RaiseWindow(managed.window);
}
}
managed.renderBackend->present();
}
return true;
Expand All @@ -601,6 +625,7 @@ int eui_app_run() {
TimerResolutionGuard timerResolution;

core::window::WindowCreateRequest windowRequest;
windowRequest.visible = false;
windowRequest.width = app::initialWindowWidth();
windowRequest.height = app::initialWindowHeight();
windowRequest.x = app::initialWindowX();
Expand Down Expand Up @@ -650,7 +675,11 @@ int eui_app_run() {
updateFrameInterval(window, state);
state.initializeTray();
state.renderBackend = renderBackend.get();
app::MainWindowRuntime mainWindowRuntime(state);
app::MainWindowRuntime mainWindowRuntime(state, [&] {
SDL_ShowWindow(window);
app::detail::requestFullPaint();
core::platform::requestFrame();
});

app::DslWindowManager<ManagedWindow> childWindows;
while (state.running) {
Expand Down Expand Up @@ -728,8 +757,6 @@ int eui_app_run() {
mainWindowRuntime.markUnavailableFrame(core::window::timeSeconds());
continue;
}
const float dpi = dpiScale(window);
const float pointer = pointerScale(window);
#if defined(EUI_RENDER_BACKEND_OPENGL) && (defined(_WIN32) || defined(__APPLE__))
if (state.startupFullPaintFrames > 0) {
state.paintRequested = true;
Expand All @@ -740,7 +767,7 @@ int eui_app_run() {
mainWindowRuntime.runFrame(
window,
*renderBackend,
{drawableWidth, drawableHeight, dpi, pointer},
[&] { return windowMetrics(window); },
now,
refreshRate(window),
findModalWindow(childWindows) == nullptr,
Expand All @@ -759,6 +786,7 @@ int eui_app_run() {
[&] {
return childWindows.anyAnimating();
});

}

childWindows.destroyAll(destroyManagedWindow);
Expand Down
3 changes: 2 additions & 1 deletion core/window/window_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ Handle createWindow(const WindowCreateRequest& request) {
configureOpenGLWindowAttributes();
}

Uint32 flags = 0;
Uint32 flags = request.visible ? 0 : SDL_WINDOW_HIDDEN;
if (request.highDpi) {
flags |= SDL_WINDOW_ALLOW_HIGHDPI;
}
Expand Down Expand Up @@ -574,6 +574,7 @@ Handle createWindow(const WindowCreateRequest& request) {
configureOpenGLWindowHints();
shareContext = static_cast<GLFWwindow*>(request.parent);
}
glfwWindowHint(GLFW_VISIBLE, request.visible ? GLFW_TRUE : GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, request.resizable ? GLFW_TRUE : GLFW_FALSE);
glfwWindowHint(GLFW_DECORATED, request.decorated ? GLFW_TRUE : GLFW_FALSE);
glfwWindowHint(GLFW_FLOATING, request.alwaysOnTop ? GLFW_TRUE : GLFW_FALSE);
Expand Down
2 changes: 2 additions & 0 deletions core/window/window_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ struct WindowCreateRequest {
int maxHeight = 0;
const char* title = "";
std::string appId;
// App runners defer visibility until their first valid frame is ready.
bool visible = true;
bool resizable = true;
bool highDpi = true;
bool decorated = true;
Expand Down
5 changes: 5 additions & 0 deletions docs/集成指南.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ main.cpp

EUI-NEO 会通过 `eui_neo_configure_app()` 自动加入窗口入口、输入处理、渲染后端、平台链接选项和运行资源;应用不需要引用 `core/`,也不需要自行编写窗口主循环。

应用主窗口和 DSL 子窗口在首次有效内容绘制完成后、紧接首次提交之前显示,`onStart` 执行时窗口仍隐藏。
请让入口管理初次显示,避免在 `onStart` 或首次 `compose` 中直接调用原生显示窗口接口。
输入或 `compose` 回调改变窗口尺寸时,入口会按新尺寸进行一次零时间更新后再绘制;若尺寸再次变化,则保留重绘请求并唤醒下一帧,
不会把旧尺寸帧提交到新 drawable。显示后立即提交已准备的帧,并请求完整重绘以处理映射可能造成的缓冲失效。

## 选择接入方式

| 方式 | 适合场景 | EUI-NEO 来源 |
Expand Down
Loading
Loading