diff --git a/core/app/glfw_app_main.cpp b/core/app/glfw_app_main.cpp index a93e3777..f85e5527 100644 --- a/core/app/glfw_app_main.cpp +++ b/core/app/glfw_app_main.cpp @@ -450,6 +450,7 @@ int eui_app_run() { windowRequest.resizable = app::windowResizable(); windowRequest.highDpi = app::windowHighDpi(); windowRequest.decorated = app::windowDecorated(); + windowRequest.transparent = app::windowTransparent(); windowRequest.alwaysOnTop = app::windowAlwaysOnTop(); windowRequest.maximized = app::windowMaximized(); windowRequest.title = app::windowTitle(); @@ -477,7 +478,7 @@ int eui_app_run() { glfwTerminate(); }; - auto renderBackend = core::render::createRenderBackend(window); + auto renderBackend = core::render::createRenderBackend(window, nullptr, app::windowTransparent()); if (!renderBackend) { cleanupMainWindow(); return -1; diff --git a/core/app/sdl2_app_main.cpp b/core/app/sdl2_app_main.cpp index 7d15918d..fed30b6b 100644 --- a/core/app/sdl2_app_main.cpp +++ b/core/app/sdl2_app_main.cpp @@ -638,6 +638,7 @@ int eui_app_run() { windowRequest.resizable = app::windowResizable(); windowRequest.highDpi = app::windowHighDpi(); windowRequest.decorated = app::windowDecorated(); + windowRequest.transparent = app::windowTransparent(); windowRequest.alwaysOnTop = app::windowAlwaysOnTop(); windowRequest.maximized = app::windowMaximized(); windowRequest.title = app::windowTitle(); @@ -649,7 +650,7 @@ int eui_app_run() { return -1; } - auto renderBackend = core::render::createRenderBackend(window); + auto renderBackend = core::render::createRenderBackend(window, nullptr, app::windowTransparent()); if (!renderBackend) { core::window::destroyWindow(window); SDL_Quit(); diff --git a/core/render/render_backend.cpp b/core/render/render_backend.cpp index 615cde9d..d8bb78f8 100644 --- a/core/render/render_backend.cpp +++ b/core/render/render_backend.cpp @@ -69,14 +69,16 @@ core::window::RenderApi windowRenderApi() { #endif } -std::unique_ptr createRenderBackend(core::window::Handle window, RenderBackend* shareBackend) { +std::unique_ptr createRenderBackend(core::window::Handle window, RenderBackend* shareBackend, bool transparent) { #if defined(EUI_RENDER_BACKEND_OPENGL) + (void)transparent; // GL composites through the framebuffer alpha channel as-is. return std::make_unique(window, shareBackend); #elif defined(EUI_RENDER_BACKEND_VULKAN) - return std::make_unique(window, shareBackend); + return std::make_unique(window, shareBackend, transparent); #else (void)window; (void)shareBackend; + (void)transparent; return {}; #endif } diff --git a/core/render/render_backend.h b/core/render/render_backend.h index 8b4e6d6e..53f9710b 100644 --- a/core/render/render_backend.h +++ b/core/render/render_backend.h @@ -343,7 +343,7 @@ class RenderBackend { const std::uint64_t deviceIdentity_ = nextDeviceIdentity_.fetch_add(1, std::memory_order_relaxed); }; -std::unique_ptr createRenderBackend(core::window::Handle window, RenderBackend* shareBackend = nullptr); +std::unique_ptr createRenderBackend(core::window::Handle window, RenderBackend* shareBackend = nullptr, bool transparent = false); core::window::RenderApi windowRenderApi(); void initializeRenderBackendLoader(); diff --git a/core/render/vulkan/vulkan_backend.cpp b/core/render/vulkan/vulkan_backend.cpp index a8f29b74..16f50176 100644 --- a/core/render/vulkan/vulkan_backend.cpp +++ b/core/render/vulkan/vulkan_backend.cpp @@ -246,7 +246,8 @@ bool VulkanRenderBackend::applyDrawViewportAndScissor(int windowWidth, int windo return true; } -VulkanRenderBackend::VulkanRenderBackend(core::window::Handle window, RenderBackend*) : window_(window) {} +VulkanRenderBackend::VulkanRenderBackend(core::window::Handle window, RenderBackend*, bool transparent) + : window_(window), transparent_(transparent) {} VulkanRenderBackend::~VulkanRenderBackend() { destroy(); @@ -670,7 +671,16 @@ bool VulkanRenderBackend::recreateSwapchain(const RenderSurface& surface) { } swapchainInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; swapchainInfo.preTransform = capabilities.currentTransform; - swapchainInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; + // Transparent windows need a composite mode that honors the framebuffer + // alpha; the renderer's content is premultiplied, so prefer + // POST_MULTIPLIED and degrade gracefully instead of failing creation. + if (transparent_ && (capabilities.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR)) { + swapchainInfo.compositeAlpha = VK_COMPOSITE_ALPHA_POST_MULTIPLIED_BIT_KHR; + } else if (transparent_ && (capabilities.supportedCompositeAlpha & VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR)) { + swapchainInfo.compositeAlpha = VK_COMPOSITE_ALPHA_INHERIT_BIT_KHR; + } else { + swapchainInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; + } swapchainInfo.presentMode = presentMode; swapchainInfo.clipped = VK_TRUE; // 保留旧交换链至新链创建调用结束,使 WSI 能复用呈现资源;调用失败也会使旧链退役。 diff --git a/core/render/vulkan/vulkan_backend.h b/core/render/vulkan/vulkan_backend.h index 1c50769e..9aa7e690 100644 --- a/core/render/vulkan/vulkan_backend.h +++ b/core/render/vulkan/vulkan_backend.h @@ -13,7 +13,7 @@ namespace core::render::vulkan { class VulkanRenderBackend final : public RenderBackend { public: - explicit VulkanRenderBackend(core::window::Handle window, RenderBackend* shareBackend = nullptr); + explicit VulkanRenderBackend(core::window::Handle window, RenderBackend* shareBackend = nullptr, bool transparent = false); ~VulkanRenderBackend() override; VulkanRenderBackend(const VulkanRenderBackend&) = delete; @@ -304,6 +304,7 @@ class VulkanRenderBackend final : public RenderBackend { core::Rect scissorRect_{}; core::Rect cacheRenderArea_{}; core::Color clearColor_{0.0f, 0.0f, 0.0f, 1.0f}; + bool transparent_ = false; VkDescriptorSetLayout roundedRectDescriptorSetLayout_ = VK_NULL_HANDLE; VkDescriptorPool roundedRectDescriptorPool_ = VK_NULL_HANDLE; diff --git a/core/window/window_backend.cpp b/core/window/window_backend.cpp index 4f7f7b42..3ec9238c 100644 --- a/core/window/window_backend.cpp +++ b/core/window/window_backend.cpp @@ -557,6 +557,12 @@ void setImeCursorRect(Handle window, float x, float y, float width, float height #endif } +bool framebufferTransparent(Handle) { + // SDL2 has no transparent-window flag (it arrived in SDL3), so the + // request.transparent hint cannot be honored on this backend. + return false; +} + bool beginWindowMove(Handle window) { return sendX11MoveResizeForWindow(window, kNetWmMoveResizeMove); } @@ -635,6 +641,7 @@ Handle createWindow(const WindowCreateRequest& request) { 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_TRANSPARENT_FRAMEBUFFER, request.transparent ? GLFW_TRUE : GLFW_FALSE); glfwWindowHint(GLFW_FLOATING, request.alwaysOnTop ? GLFW_TRUE : GLFW_FALSE); glfwWindowHint(GLFW_MAXIMIZED, request.maximized ? GLFW_TRUE : GLFW_FALSE); #if defined(GLFW_WAYLAND_APP_ID) || defined(GLFW_X11_CLASS_NAME) @@ -739,6 +746,13 @@ void setImeCursorRect(Handle window, float x, float y, float width, float height eui_ime_set_cursor_rect_with_font(static_cast(window), x, y, width, height, height); } +bool framebufferTransparent(Handle window) { + // Reports what the platform actually granted: without a compositor an + // X11 window cannot honor the transparent hint. + return window != nullptr && + glfwGetWindowAttrib(static_cast(window), GLFW_TRANSPARENT_FRAMEBUFFER) == GLFW_TRUE; +} + bool beginWindowMove(Handle window) { return sendX11MoveResizeForWindow(window, kNetWmMoveResizeMove); } diff --git a/core/window/window_backend.h b/core/window/window_backend.h index 616b92fe..ba542be5 100644 --- a/core/window/window_backend.h +++ b/core/window/window_backend.h @@ -29,6 +29,9 @@ void destroyCursor(CursorHandle cursor); void setWindowIcon(Handle window, int width, int height, unsigned char* pixels); void setImeCursorRect(Handle window, float x, float y, float width, float height); +// Reports whether the created framebuffer actually carries alpha (GLFW +// honors the transparent request; SDL2 and compositor-less X11 do not). +bool framebufferTransparent(Handle window); // Hand an interactive move/resize to the window manager (EWMH // _NET_WM_MOVERESIZE on X11). Per-frame programmatic moves arrive as // ConfigureRequests and can be clamped back into the workarea, so undecorated diff --git a/core/window/window_types.h b/core/window/window_types.h index 609db704..c74480c7 100644 --- a/core/window/window_types.h +++ b/core/window/window_types.h @@ -48,6 +48,7 @@ struct WindowCreateRequest { bool resizable = true; bool highDpi = true; bool decorated = true; + bool transparent = false; bool alwaysOnTop = false; bool maximized = false; bool modal = false; diff --git a/docs/DSL.md b/docs/DSL.md index 8ae68aea..0c5861d2 100644 --- a/docs/DSL.md +++ b/docs/DSL.md @@ -81,6 +81,7 @@ static const DslAppConfig config = DslAppConfig{} .resizable(true) .highDpi(true) .decorated(true) + .transparent(false) .alwaysOnTop(false) .maximized(false) .debugTitleInterval(1.0) @@ -91,7 +92,9 @@ static const DslAppConfig config = DslAppConfig{} }); ``` -`minWindowSize` 和 `maxWindowSize` 中的 `0` 表示对应方向不限制;窗口尺寸约束由 GLFW/SDL2 后端执行。`centerWindow()` 会清除显式位置并恢复居中。`highDpi` 在 SDL2 中控制 `SDL_WINDOW_ALLOW_HIGHDPI`;GLFW 的 DPI 感知由其初始化阶段按平台设置,是进程级行为,不能安全地按单个窗口关闭。全屏、透明窗口和 VSync 不属于 `DslAppConfig`,它们会改变平台窗口或渲染后端生命周期,应通过专用平台/渲染配置处理。 +`minWindowSize` 和 `maxWindowSize` 中的 `0` 表示对应方向不限制;窗口尺寸约束由 GLFW/SDL2 后端执行。`centerWindow()` 会清除显式位置并恢复居中。`highDpi` 在 SDL2 中控制 `SDL_WINDOW_ALLOW_HIGHDPI`;GLFW 的 DPI 感知由其初始化阶段按平台设置,是进程级行为,不能安全地按单个窗口关闭。全屏和 VSync 不属于 `DslAppConfig`,它们会改变平台窗口或渲染后端生命周期,应通过专用平台/渲染配置处理。 + +`.transparent(true)` 与 `decorated` 同属创建期窗口属性:GLFW 后端以 `GLFW_TRANSPARENT_FRAMEBUFFER` 请求带 alpha 的 framebuffer,Vulkan 后端据此选择受支持的 composite-alpha 模式,用于无边框自绘圆角窗口。平台是否真的授予以 `eui::window::framebufferTransparent(window)` 为准(SDL2 后端不支持;X11 无合成器时会拒绝)。启用后应用应把 `clearColor` 的 alpha 设为 0,并自绘圆角根表面;未启用时行为与从前逐像素一致。 Debug 配置只控制诊断输出,不参与业务状态。`showDebugStatsInTitle` 控制窗口标题中的 FPS、CPU/GPU 和渲染统计;`debugTitleInterval` 控制标题统计刷新间隔(秒)。`showDebugOverlay` 与 `onDebugOverlay` 用于注入布局边界、性能标记等调试框,回调在每次页面 compose 后执行;未设置回调时不会绘制任何额外内容。Debug 构建默认开启标题统计和覆盖层开关,Release 构建默认关闭。 diff --git a/include/eui/app.h b/include/eui/app.h index 231c74ae..05db070e 100644 --- a/include/eui/app.h +++ b/include/eui/app.h @@ -40,6 +40,7 @@ int maximumWindowHeight(); bool windowResizable(); bool windowHighDpi(); bool windowDecorated(); +bool windowTransparent(); bool windowAlwaysOnTop(); bool windowMaximized(); float uiScale(); diff --git a/include/eui/detail/dsl_app_impl.h b/include/eui/detail/dsl_app_impl.h index 5dde9fad..ab970081 100644 --- a/include/eui/detail/dsl_app_impl.h +++ b/include/eui/detail/dsl_app_impl.h @@ -237,6 +237,10 @@ bool windowDecorated() { return dslAppConfig().decoratedValue; } +bool windowTransparent() { + return dslAppConfig().transparentValue; +} + bool windowAlwaysOnTop() { return dslAppConfig().alwaysOnTopValue; } diff --git a/include/eui/dsl_app.h b/include/eui/dsl_app.h index a6578f7c..afc3e59f 100644 --- a/include/eui/dsl_app.h +++ b/include/eui/dsl_app.h @@ -27,6 +27,7 @@ struct DslAppConfig { bool resizableValue = true; bool highDpiValue = true; bool decoratedValue = true; + bool transparentValue = false; bool alwaysOnTopValue = false; bool maximizedValue = false; float uiScaleValue = 1.0f; @@ -85,6 +86,8 @@ struct DslAppConfig { DslAppConfig& resizable(bool value = true) { resizableValue = value; return *this; } DslAppConfig& highDpi(bool value = true) { highDpiValue = value; return *this; } DslAppConfig& decorated(bool value = true) { decoratedValue = value; return *this; } + /** @brief 请求带 alpha 的 framebuffer(自绘圆角窗口)。平台不支持时 framebufferTransparent 返回 false。 */ + DslAppConfig& transparent(bool value = true) { transparentValue = value; return *this; } DslAppConfig& alwaysOnTop(bool value = true) { alwaysOnTopValue = value; return *this; } DslAppConfig& maximized(bool value = true) { maximizedValue = value; return *this; } DslAppConfig& uiScale(float value) { diff --git a/include/eui/window.h b/include/eui/window.h index a49651de..2d86fd55 100644 --- a/include/eui/window.h +++ b/include/eui/window.h @@ -17,4 +17,8 @@ using WindowResizeEdge = core::window::WindowResizeEdge; using core::window::beginWindowMove; using core::window::beginWindowResize; +// Reports whether the created framebuffer actually carries alpha (GLFW honors +// the transparent request; SDL2 and compositor-less X11 do not). +using core::window::framebufferTransparent; + } // namespace eui::window diff --git a/tests/fixtures/transparent_window_viewer.cpp b/tests/fixtures/transparent_window_viewer.cpp new file mode 100644 index 00000000..e3d2fd7a --- /dev/null +++ b/tests/fixtures/transparent_window_viewer.cpp @@ -0,0 +1,73 @@ +#include "eui_neo.h" + +#if defined(EUI_WINDOW_BACKEND_SDL2) +#include +#else +#define GLFW_INCLUDE_NONE +#include +#endif + +namespace app { +namespace { + +#if defined(EUI_WINDOW_BACKEND_SDL2) +SDL_Window* window = nullptr; +#else +GLFWwindow* window = nullptr; +#endif + +void capture() { + if (window) return; +#if defined(EUI_WINDOW_BACKEND_SDL2) + window = SDL_GetKeyboardFocus(); +#else + // 通过当前 OpenGL context 获取应用窗口,不替换框架事件回调。 + window = glfwGetCurrentContext(); +#endif +} + +} // namespace + +const DslAppConfig& dslAppConfig() { +#if defined(EUI_WINDOW_BACKEND_SDL2) + constexpr const char* title = "EUI Transparent Window Test - SDL2"; +#else + constexpr const char* title = "EUI Transparent Window Test - GLFW"; +#endif + static const auto config = DslAppConfig{}.title(title).windowSize(760, 480) + .decorated(false).transparent(true).clearColor({0.0f, 0.0f, 0.0f, 0.0f}); + return config; +} + +void compose(eui::Ui& ui, const eui::Screen& screen) { + capture(); + const bool granted = eui::window::framebufferTransparent(window); + const float margin = 36.f; + const float inset = 40.f; + ui.stack("root").size(screen.width, screen.height).content([&] { + ui.rect("panel") + .position(margin, margin) + .size(screen.width - margin * 2.f, screen.height - margin * 2.f) + .radius(28.f) + .color("#0F172A") + .build(); + ui.column("content") + .position(margin + inset, margin + 36.f) + .size(screen.width - (margin + inset) * 2.f, screen.height - (margin + 36.f) * 2.f) + .gap(12.f) + .content([&] { + ui.text("headline").size(520.f, 34.f).fontSize(24.f).color("#E2E8F0") + .text("Transparent window").build(); + ui.text("status").size(620.f, 24.f).fontSize(15.f) + .color(granted ? "#4ADE80" : "#F87171") + .text(granted + ? "framebufferTransparent: true - desktop shows through corners and margins" + : "framebufferTransparent: false - platform did not grant an alpha surface") + .build(); + ui.text("hint").size(620.f, 24.f).fontSize(14.f).color("#94A3B8") + .text("Rounded corners and the outer margin are fully transparent.").build(); + }).build(); + }).build(); +} + +} // namespace app