[feat] use SDL_WaitEventTimeout to refresh frames and reduce CPU usage

This commit is contained in:
dijunkun
2025-09-02 16:45:04 +08:00
parent 6e7e2697b5
commit b6fe0da581
3 changed files with 157 additions and 154 deletions

View File

@@ -982,8 +982,12 @@ void Render::MainLoop() {
CreateConnectionPeer(); CreateConnectionPeer();
} }
SDL_Event event;
if (SDL_WaitEventTimeout(&event, sdl_refresh_ms_)) {
ProcessSdlEvent(event);
}
UpdateLabels(); UpdateLabels();
ProcessSdlEvent();
HandleRecentConnections(); HandleRecentConnections();
HandleStreamWindow(); HandleStreamWindow();
@@ -1229,9 +1233,7 @@ void Render::UpdateRenderRect() {
} }
} }
void Render::ProcessSdlEvent() { void Render::ProcessSdlEvent(const SDL_Event& event) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (main_ctx_) { if (main_ctx_) {
ImGui::SetCurrentContext(main_ctx_); ImGui::SetCurrentContext(main_ctx_);
ImGui_ImplSDL3_ProcessEvent(&event); ImGui_ImplSDL3_ProcessEvent(&event);
@@ -1340,18 +1342,18 @@ void Render::ProcessSdlEvent() {
ProcessMouseEvent(event); ProcessMouseEvent(event);
} }
break; break;
}
default:
if (event.type == STREAM_REFRESH_EVENT) { if (event.type == STREAM_REFRESH_EVENT) {
auto* props = static_cast<SubStreamWindowProperties*>(event.user.data1); auto* props = static_cast<SubStreamWindowProperties*>(event.user.data1);
if (!props) { if (!props) {
continue; break;
} }
if (props->video_width_ <= 0 || props->video_height_ <= 0) { if (props->video_width_ <= 0 || props->video_height_ <= 0) {
continue; break;
} }
if (!props->dst_buffer_) { if (!props->dst_buffer_) {
continue; break;
} }
// use libyuv to convert NV12 to ARGB in order to fix SDL3 NV12 texture // use libyuv to convert NV12 to ARGB in order to fix SDL3 NV12 texture
@@ -1363,10 +1365,10 @@ void Render::ProcessSdlEvent() {
props->texture_height_ = props->video_height_; props->texture_height_ = props->video_height_;
SDL_DestroyTexture(props->stream_texture_); SDL_DestroyTexture(props->stream_texture_);
props->stream_texture_ = props->stream_texture_ = SDL_CreateTexture(
SDL_CreateTexture(stream_renderer_, SDL_PIXELFORMAT_ARGB8888, stream_renderer_, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING, SDL_TEXTUREACCESS_STREAMING, props->texture_width_,
props->texture_width_, props->texture_height_); props->texture_height_);
} }
} else { } else {
props->texture_width_ = props->video_width_; props->texture_width_ = props->video_width_;
@@ -1398,7 +1400,7 @@ void Render::ProcessSdlEvent() {
SDL_UpdateTexture(props->stream_texture_, NULL, props->argb_buffer_, SDL_UpdateTexture(props->stream_texture_, NULL, props->argb_buffer_,
static_cast<int>(argb_stride)); static_cast<int>(argb_stride));
}
break; break;
} }
} }
}

View File

@@ -138,7 +138,7 @@ class Render {
void CleanSubStreamWindowProperties( void CleanSubStreamWindowProperties(
std::shared_ptr<SubStreamWindowProperties> props); std::shared_ptr<SubStreamWindowProperties> props);
void UpdateRenderRect(); void UpdateRenderRect();
void ProcessSdlEvent(); void ProcessSdlEvent(const SDL_Event &event);
private: private:
int CreateStreamRenderWindow(); int CreateStreamRenderWindow();
@@ -213,7 +213,7 @@ class Render {
private: private:
int SendKeyCommand(int key_code, bool is_down); int SendKeyCommand(int key_code, bool is_down);
int ProcessMouseEvent(SDL_Event &event); int ProcessMouseEvent(const SDL_Event &event);
static void SdlCaptureAudioIn(void *userdata, Uint8 *stream, int len); static void SdlCaptureAudioIn(void *userdata, Uint8 *stream, int len);
static void SdlCaptureAudioOut(void *userdata, Uint8 *stream, int len); static void SdlCaptureAudioOut(void *userdata, Uint8 *stream, int len);
@@ -290,6 +290,7 @@ class Render {
SDL_Renderer *main_renderer_ = nullptr; SDL_Renderer *main_renderer_ = nullptr;
ImGuiContext *main_ctx_ = nullptr; ImGuiContext *main_ctx_ = nullptr;
bool exit_ = false; bool exit_ = false;
const int sdl_refresh_ms_ = 16; // ~60 FPS
// main window properties // main window properties
bool start_mouse_controller_ = false; bool start_mouse_controller_ = false;

View File

@@ -35,7 +35,7 @@ int Render::SendKeyCommand(int key_code, bool is_down) {
return 0; return 0;
} }
int Render::ProcessMouseEvent(SDL_Event &event) { int Render::ProcessMouseEvent(const SDL_Event &event) {
controlled_remote_id_ = ""; controlled_remote_id_ = "";
int video_width, video_height = 0; int video_width, video_height = 0;
int render_width, render_height = 0; int render_width, render_height = 0;