[fix] fix SDL3 texture display error

This commit is contained in:
dijunkun
2025-08-26 16:26:06 +08:00
parent d60fdf9050
commit 062568dc96
2 changed files with 41 additions and 16 deletions

View File

@@ -1,5 +1,7 @@
#include "render.h" #include "render.h"
#include <libyuv.h>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
@@ -126,7 +128,7 @@ SDL_HitTestResult Render::HitTestCallback(SDL_Window* window,
} }
int window_width, window_height; int window_width, window_height;
SDL_GetWindowSizeInPixels(window, &window_width, &window_height); SDL_GetWindowSize(window, &window_width, &window_height);
if (area->y < 30 && area->y > MOUSE_GRAB_PADDING && if (area->y < 30 && area->y > MOUSE_GRAB_PADDING &&
area->x < window_width - 120 && area->x > MOUSE_GRAB_PADDING && area->x < window_width - 120 && area->x > MOUSE_GRAB_PADDING &&
@@ -693,8 +695,7 @@ int Render::SetupFontAndStyle() {
// Setup Dear ImGui style // Setup Dear ImGui style
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
imgui_cache_path_ = cache_path_ + "/crossdesk.ini"; io.IniFilename = NULL; // disable imgui.ini
io.IniFilename = imgui_cache_path_.c_str();
io.ConfigFlags |= io.ConfigFlags |=
ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
@@ -1105,7 +1106,7 @@ void Render::CleanupFactories() {
} }
if (device_controller_factory_) { if (device_controller_factory_) {
SDL_FlushEvent(STREAM_REFRESH_EVENT); delete device_controller_factory_;
device_controller_factory_ = nullptr; device_controller_factory_ = nullptr;
} }
} }
@@ -1170,8 +1171,8 @@ void Render::UpdateRenderRect() {
} }
int stream_window_width, stream_window_height; int stream_window_width, stream_window_height;
SDL_GetWindowSizeInPixels(stream_window_, &stream_window_width, SDL_GetWindowSize(stream_window_, &stream_window_width,
&stream_window_height); &stream_window_height);
stream_window_width_ = (float)stream_window_width; stream_window_width_ = (float)stream_window_width;
stream_window_height_ = (float)stream_window_height; stream_window_height_ = (float)stream_window_height;
@@ -1253,7 +1254,7 @@ void Render::ProcessSdlEvent() {
props->streaming_ = false; props->streaming_ = false;
props->remember_password_ = false; props->remember_password_ = false;
SDL_FlushEvents(STREAM_REFRESH_EVENT, STREAM_REFRESH_EVENT); props->connection_established_ = false;
props->audio_capture_button_pressed_ = false; props->audio_capture_button_pressed_ = false;
memset(&props->net_traffic_stats_, 0, memset(&props->net_traffic_stats_, 0,
@@ -1331,6 +1332,8 @@ void Render::ProcessSdlEvent() {
continue; continue;
} }
// use libyuv to convert NV12 to ARGB in order to fix SDL3 NV12 texture
// display issue
if (props->stream_texture_) { if (props->stream_texture_) {
if (props->video_width_ != props->texture_width_ || if (props->video_width_ != props->texture_width_ ||
props->video_height_ != props->texture_height_) { props->video_height_ != props->texture_height_) {
@@ -1338,20 +1341,41 @@ 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_ = SDL_CreateTexture( props->stream_texture_ =
stream_renderer_, stream_pixformat_, SDL_TEXTUREACCESS_STREAMING, SDL_CreateTexture(stream_renderer_, SDL_PIXELFORMAT_ARGB8888,
props->texture_width_, props->texture_height_); SDL_TEXTUREACCESS_STREAMING,
props->texture_width_, props->texture_height_);
} }
} else { } else {
props->texture_width_ = props->video_width_; props->texture_width_ = props->video_width_;
props->texture_height_ = props->video_height_; props->texture_height_ = props->video_height_;
props->stream_texture_ = SDL_CreateTexture( props->stream_texture_ =
stream_renderer_, stream_pixformat_, SDL_TEXTUREACCESS_STREAMING, SDL_CreateTexture(stream_renderer_, SDL_PIXELFORMAT_ARGB8888,
props->texture_width_, props->texture_height_); SDL_TEXTUREACCESS_STREAMING,
props->texture_width_, props->texture_height_);
} }
SDL_UpdateTexture(props->stream_texture_, NULL, props->dst_buffer_, size_t argb_stride = props->texture_width_ * 4;
props->texture_width_); size_t argb_size = argb_stride * props->texture_height_;
if (!props->argb_buffer_ || props->argb_buffer_size_ != argb_size) {
if (props->argb_buffer_) {
delete[] props->argb_buffer_;
}
props->argb_buffer_ = new uint8_t[argb_size];
props->argb_buffer_size_ = argb_size;
}
uint8_t* src_y = reinterpret_cast<uint8_t*>(props->dst_buffer_);
int src_stride_y = props->texture_width_;
uint8_t* src_uv = src_y + src_stride_y * props->texture_height_;
int src_stride_uv = props->texture_width_;
libyuv::NV12ToARGB(src_y, src_stride_y, src_uv, src_stride_uv,
props->argb_buffer_, static_cast<int>(argb_stride),
props->texture_width_, props->texture_height_);
SDL_UpdateTexture(props->stream_texture_, NULL, props->argb_buffer_,
static_cast<int>(argb_stride));
break; break;
} }
} }

View File

@@ -103,6 +103,8 @@ class Render {
std::string remote_host_name_ = ""; std::string remote_host_name_ = "";
std::vector<DisplayInfo> display_info_list_; std::vector<DisplayInfo> display_info_list_;
SDL_Texture *stream_texture_ = nullptr; SDL_Texture *stream_texture_ = nullptr;
uint8_t *argb_buffer_ = nullptr;
int argb_buffer_size_ = 0;
SDL_Rect stream_render_rect_; SDL_Rect stream_render_rect_;
SDL_Rect stream_render_rect_last_; SDL_Rect stream_render_rect_last_;
ImVec2 control_window_pos_; ImVec2 control_window_pos_;
@@ -262,7 +264,6 @@ class Render {
std::string exec_log_path_; std::string exec_log_path_;
std::string dll_log_path_; std::string dll_log_path_;
std::string cache_path_; std::string cache_path_;
std::string imgui_cache_path_;
int localization_language_index_ = -1; int localization_language_index_ = -1;
int localization_language_index_last_ = -1; int localization_language_index_last_ = -1;
bool modules_inited_ = false; bool modules_inited_ = false;