载入中...
搜索中...
未找到
EventSink.cpp
浏览该文件的文档.
1// eve-hot-path
2// Keeps the window settings and the render surface aligned with the platform.
3//
4// Two things used to live in the SDL event pump and forced it to depend on both
5// window and graphics: resize handling, and the app background/foreground
6// callback that tears down and rebuilds the Vulkan surface on mobile. Desktop
7// minimize / restore is handled here too so present skips a zero-extent
8// swapchain while the window stays iconified.
9
10#include "common/Capability.h"
13#include "window/Window.h"
15#include "window/sdl/Window.h"
16
17#include <SDL2/SDL.h>
18#include <SDL2/SDL_vulkan.h>
19
20#include <algorithm>
21
23namespace {
24
25eve::window::Window *g_owner = nullptr;
26
28void syncWindowPixelSize() {
29 auto *win = g_owner;
30 if (!win) return;
31 auto *sdlWin = dynamic_cast<Window *>(win);
32 if (!sdlWin) return;
33 SDL_Window *native = static_cast<SDL_Window *>(sdlWin->getHandle());
34 if (!native) return;
35
36 int lw = 0, lh = 0, pw = 0, ph = 0;
37 SDL_GetWindowSize(native, &lw, &lh);
38 SDL_Vulkan_GetDrawableSize(native, &pw, &ph);
39 if (pw <= 0 || ph <= 0) {
40 pw = lw;
41 ph = lh;
42 }
43 WindowSettings s = win->getWindowSettings();
44 s.width = static_cast<uint16_t>(std::max(lw, 1));
45 s.height = static_cast<uint16_t>(std::max(lh, 1));
46 // Keep settings + graphics viewport aligned after rotation / resume.
47 sdlWin->updateSettings(s, true);
48}
49
51void pausePresenting(IWindowSurfaceHost *surfaceHost) {
52 if (surfaceHost) surfaceHost->setActive(false);
53}
54
56void resumePresenting(IWindowSurfaceHost *surfaceHost, bool recreateSurface) {
57 if (!surfaceHost) return;
58 // SHOWN can race with MINIMIZED on some WMs; stay paused while iconified.
59 if (g_owner && g_owner->isMinimized()) return;
60 if (recreateSurface) surfaceHost->requestSurfaceRecreate();
61 surfaceHost->setActive(true);
62 syncWindowPixelSize();
63}
64
72int SDLCALL watchAppEvents(void * /*udata*/, SDL_Event *event) {
73 auto *surfaceHost = eve::cap::query<IWindowSurfaceHost>();
74 if (!surfaceHost) return 1;
75
76 switch (event->type) {
77 case SDL_APP_DIDENTERBACKGROUND:
78 // Stop presenting: the native surface is being torn down.
79 pausePresenting(surfaceHost);
80 break;
81 case SDL_APP_WILLENTERFOREGROUND:
82 case SDL_APP_DIDENTERFOREGROUND:
83 // The native window is recreated on resume; rebuild the render
84 // surface and swapchain, then resume presenting.
85 resumePresenting(surfaceHost, /*recreateSurface=*/true);
86 break;
87 case SDL_WINDOWEVENT:
88 switch (event->window.event) {
89 case SDL_WINDOWEVENT_MINIMIZED:
90 case SDL_WINDOWEVENT_HIDDEN:
91 // Desktop minimize / hide: surface usually stays alive, but
92 // drawable extent may become 0 — pause presenting.
93 pausePresenting(surfaceHost);
94 break;
95 case SDL_WINDOWEVENT_RESTORED:
96 case SDL_WINDOWEVENT_SHOWN:
97 // Desktop restore: mark swapchain dirty via setActive(true).
98 // Do not tear down the VkSurfaceKHR (unlike mobile resume).
99 resumePresenting(surfaceHost, /*recreateSurface=*/false);
100 break;
101 default: break;
102 }
103 break;
104 default:
105 break;
106 }
107 return 1;
108}
109
110class WindowEventSink : public eve::platform_event::IPlatformEventSink {
111public:
112 bool shouldConsumePlatformEvent(const void *nativeEvent) override {
113 const auto &e = *static_cast<const SDL_Event *>(nativeEvent);
114 if (e.type == SDL_WINDOWEVENT && (e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED ||
115 e.window.event == SDL_WINDOWEVENT_RESIZED))
116 syncWindowPixelSize();
117 // Never consume: SDL_WINDOWEVENT_CLOSE still has to reach the pump,
118 // which turns it into "quit".
119 return false;
120 }
121
122 void onPumpFinished() override {
123 // Deferred to the first pump because the watch can only be added once
124 // SDL is initialized, which is later than static registration.
125 if (watchInstalled_) return;
126 watchInstalled_ = true;
127 SDL_AddEventWatch(watchAppEvents, nullptr);
128 }
129
130private:
131 bool watchInstalled_ = false;
132};
133
134struct Register {
135 Register() {
136 static WindowEventSink sink;
137 eve::cap::addListener<eve::platform_event::IPlatformEventSink>(
139 }
140} g_register;
141
142} // namespace
143
145
146} // namespace eve::window::sdl
const std::string & s
std::shared_ptr< OceanGpuSpectrum > owner
Render-surface host for the window module (provided by graphics).
virtual void requestSurfaceRecreate()=0
Rebuild the render surface on the next frame (mobile foreground; surface was destroyed).
virtual void setActive(bool active)=0
Pause/resume presenting (mobile background/foreground, desktop minimize/restore).
EVENGINE_API_FOUNDATION public API.
Platform window interface (SDL implementation on desktop/mobile). Script: win <- eve....
Definition Window.h:47
virtual bool isMinimized() const =0
True when the window is minimized.
void bindWindowEventSinkOwner(eve::window::Window *owner) noexcept
Bind the live Window module into the SDL event sink. @ownership Borrowed; cleared when the Window mod...
Display settings for window creation/resize. width/height == 0 selects the desktop display mode size.
Definition Window.h:18