[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();
}
SDL_Event event;
if (SDL_WaitEventTimeout(&event, sdl_refresh_ms_)) {
ProcessSdlEvent(event);
}
UpdateLabels();
ProcessSdlEvent();
HandleRecentConnections();
HandleStreamWindow();
@@ -1229,9 +1233,7 @@ void Render::UpdateRenderRect() {
}
}
void Render::ProcessSdlEvent() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
void Render::ProcessSdlEvent(const SDL_Event& event) {
if (main_ctx_) {
ImGui::SetCurrentContext(main_ctx_);
ImGui_ImplSDL3_ProcessEvent(&event);
@@ -1340,18 +1342,18 @@ void Render::ProcessSdlEvent() {
ProcessMouseEvent(event);
}
break;
}
default:
if (event.type == STREAM_REFRESH_EVENT) {
auto* props = static_cast<SubStreamWindowProperties*>(event.user.data1);
if (!props) {
continue;
break;
}
if (props->video_width_ <= 0 || props->video_height_ <= 0) {
continue;
break;
}
if (!props->dst_buffer_) {
continue;
break;
}
// 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_;
SDL_DestroyTexture(props->stream_texture_);
props->stream_texture_ =
SDL_CreateTexture(stream_renderer_, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
props->texture_width_, props->texture_height_);
props->stream_texture_ = SDL_CreateTexture(
stream_renderer_, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING, props->texture_width_,
props->texture_height_);
}
} else {
props->texture_width_ = props->video_width_;
@@ -1398,7 +1400,7 @@ void Render::ProcessSdlEvent() {
SDL_UpdateTexture(props->stream_texture_, NULL, props->argb_buffer_,
static_cast<int>(argb_stride));
}
break;
}
}
}

View File

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

View File

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