From 00f254ae71e8c4d0e57f1e53ea272902d969b2d6 Mon Sep 17 00:00:00 2001 From: sladro Date: Mon, 12 Jan 2026 20:12:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=80=A7=E8=83=BD=E4=BC=98=E5=8C=963?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- configs/multi_security_instances.json | 3 + configs/sample_cam1.json | 6 ++ ..._cam1_strict_minio_alarm_5ch_rtsp_hls.json | 3 + .../test_cam1_face_det_recog_rtsp_server.json | 3 + configs/test_cam1_face_det_rtsp_server.json | 3 + ...t_cam1_strict_minio_alarm_rtsp_server.json | 3 + plugins/preprocess/preprocess_node.cpp | 95 +++++++++++++++---- 7 files changed, 97 insertions(+), 19 deletions(-) diff --git a/configs/multi_security_instances.json b/configs/multi_security_instances.json index 8e147cc..9420a91 100644 --- a/configs/multi_security_instances.json +++ b/configs/multi_security_instances.json @@ -30,7 +30,9 @@ "dst_w": 640, "dst_h": 640, "dst_format": "rgb", + "dst_packed": true, "keep_ratio": false, + "rga_gate": "${name}", "use_rga": false }, { @@ -66,6 +68,7 @@ "dst_h": "${src_h}", "dst_format": "nv12", "keep_ratio": false, + "rga_gate": "${name}", "use_rga": false }, { diff --git a/configs/sample_cam1.json b/configs/sample_cam1.json index d2c754b..15851ba 100644 --- a/configs/sample_cam1.json +++ b/configs/sample_cam1.json @@ -27,7 +27,9 @@ "dst_w": 640, "dst_h": 640, "dst_format": "rgb", + "dst_packed": true, "keep_ratio": false, + "rga_gate": "cam1_sample_full_pipeline", "use_rga": true }, { @@ -114,6 +116,7 @@ "dst_h": 720, "dst_format": "nv12", "keep_ratio": false, + "rga_gate": "cam1_sample_full_pipeline", "use_rga": true }, { @@ -230,7 +233,9 @@ "dst_w": 640, "dst_h": 640, "dst_format": "rgb", + "dst_packed": true, "keep_ratio": false, + "rga_gate": "cam2_sample_full_pipeline", "use_rga": true }, { @@ -317,6 +322,7 @@ "dst_h": 720, "dst_format": "nv12", "keep_ratio": false, + "rga_gate": "cam2_sample_full_pipeline", "use_rga": true }, { diff --git a/configs/stress_cam1_strict_minio_alarm_5ch_rtsp_hls.json b/configs/stress_cam1_strict_minio_alarm_5ch_rtsp_hls.json index eccc304..dab32ee 100644 --- a/configs/stress_cam1_strict_minio_alarm_5ch_rtsp_hls.json +++ b/configs/stress_cam1_strict_minio_alarm_5ch_rtsp_hls.json @@ -31,7 +31,9 @@ "dst_w": 640, "dst_h": 640, "dst_format": "rgb", + "dst_packed": true, "keep_ratio": false, + "rga_gate": "${name}", "use_rga": true }, { @@ -68,6 +70,7 @@ "dst_h": "${src_h}", "dst_format": "nv12", "keep_ratio": false, + "rga_gate": "${name}", "use_rga": true }, { diff --git a/configs/test_cam1_face_det_recog_rtsp_server.json b/configs/test_cam1_face_det_recog_rtsp_server.json index ff036e5..1228749 100644 --- a/configs/test_cam1_face_det_recog_rtsp_server.json +++ b/configs/test_cam1_face_det_recog_rtsp_server.json @@ -27,7 +27,9 @@ "dst_w": 1280, "dst_h": 720, "dst_format": "rgb", + "dst_packed": true, "keep_ratio": false, + "rga_gate": "cam1_face_det_recog_test", "use_rga": true }, { @@ -83,6 +85,7 @@ "dst_h": 720, "dst_format": "nv12", "keep_ratio": false, + "rga_gate": "cam1_face_det_recog_test", "use_rga": true }, { diff --git a/configs/test_cam1_face_det_rtsp_server.json b/configs/test_cam1_face_det_rtsp_server.json index 7d51bf2..f8d0bb8 100644 --- a/configs/test_cam1_face_det_rtsp_server.json +++ b/configs/test_cam1_face_det_rtsp_server.json @@ -27,7 +27,9 @@ "dst_w": 320, "dst_h": 320, "dst_format": "rgb", + "dst_packed": true, "keep_ratio": false, + "rga_gate": "cam1_face_det_test", "use_rga": true }, { @@ -62,6 +64,7 @@ "dst_h": 720, "dst_format": "nv12", "keep_ratio": false, + "rga_gate": "cam1_face_det_test", "use_rga": true }, { diff --git a/configs/test_cam1_strict_minio_alarm_rtsp_server.json b/configs/test_cam1_strict_minio_alarm_rtsp_server.json index 71a383c..4498d64 100644 --- a/configs/test_cam1_strict_minio_alarm_rtsp_server.json +++ b/configs/test_cam1_strict_minio_alarm_rtsp_server.json @@ -27,7 +27,9 @@ "dst_w": 640, "dst_h": 640, "dst_format": "rgb", + "dst_packed": true, "keep_ratio": false, + "rga_gate": "cam1_strict_minio_alarm", "use_rga": true }, { @@ -63,6 +65,7 @@ "dst_h": 720, "dst_format": "nv12", "keep_ratio": false, + "rga_gate": "cam1_strict_minio_alarm", "use_rga": true }, { diff --git a/plugins/preprocess/preprocess_node.cpp b/plugins/preprocess/preprocess_node.cpp index b24366c..cecfa44 100644 --- a/plugins/preprocess/preprocess_node.cpp +++ b/plugins/preprocess/preprocess_node.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "node.h" @@ -64,18 +65,13 @@ int GlobalRgaMaxInflight() { return v > 0 ? v : 1; } -void SetGlobalRgaMaxInflight(int v) { - if (v <= 0) return; - if (v > 32) v = 32; - GlobalRgaMaxInflightRef().store(v); -} - class RgaGate { public: + explicit RgaGate(int max_inflight) : max_inflight_(max_inflight > 0 ? max_inflight : 1) {} + void Acquire() { - const int max_inflight = GlobalRgaMaxInflight(); std::unique_lock lock(mu_); - cv_.wait(lock, [&]() { return in_flight_ < max_inflight; }); + cv_.wait(lock, [&]() { return in_flight_ < max_inflight_; }); ++in_flight_; } @@ -87,23 +83,64 @@ public: cv_.notify_one(); } + void SetMaxInflight(int v) { + if (v <= 0) return; + if (v > 32) v = 32; + { + std::lock_guard lock(mu_); + max_inflight_ = v; + } + cv_.notify_all(); + } + + int MaxInflight() const { + std::lock_guard lock(mu_); + return max_inflight_; + } + private: - std::mutex mu_; + mutable std::mutex mu_; std::condition_variable cv_; int in_flight_ = 0; + int max_inflight_ = 1; }; -RgaGate& GlobalRgaGate() { - static RgaGate* g = new RgaGate(); - return *g; +class RgaGateRegistry { +public: + static RgaGateRegistry& Instance() { + static RgaGateRegistry* inst = new RgaGateRegistry(); + return *inst; + } + + RgaGate& Get(const std::string& key) { + std::lock_guard lock(mu_); + auto it = gates_.find(key); + if (it != gates_.end()) return *it->second; + auto gate = std::make_unique(GlobalRgaMaxInflight()); + RgaGate& ref = *gate; + gates_.emplace(key, std::move(gate)); + return ref; + } + +private: + std::mutex mu_; + std::unordered_map> gates_; +}; + +RgaGate& GetRgaGate(const std::string& key) { + const std::string k = key.empty() ? "global" : key; + return RgaGateRegistry::Instance().Get(k); } class ScopedRgaGate { public: - ScopedRgaGate() { GlobalRgaGate().Acquire(); } - ~ScopedRgaGate() { GlobalRgaGate().Release(); } + explicit ScopedRgaGate(const std::string& key) : gate_(&GetRgaGate(key)) { gate_->Acquire(); } + ~ScopedRgaGate() { gate_->Release(); } ScopedRgaGate(const ScopedRgaGate&) = delete; ScopedRgaGate& operator=(const ScopedRgaGate&) = delete; + +private: + RgaGate* gate_ = nullptr; }; void EnsureRgaInitializedOnce() { @@ -276,16 +313,25 @@ public: dst_w_ = config.ValueOr("dst_w", 640); dst_h_ = config.ValueOr("dst_h", 640); keep_ratio_ = config.ValueOr("keep_ratio", false); - dst_packed_ = config.ValueOr("dst_packed", false); + + if (config.Find("dst_packed")) { + dst_packed_ = config.ValueOr("dst_packed", false); + dst_packed_explicit_ = true; + } else { + dst_packed_ = false; + dst_packed_explicit_ = false; + } + std::string fmt_str = config.ValueOr("dst_format", ""); if (!fmt_str.empty()) { dst_fmt_ = ParseFormat(fmt_str); } #if defined(RK3588_ENABLE_RGA) + rga_gate_ = config.ValueOr("rga_gate", "global"); const int rga_max_inflight = config.ValueOr("rga_max_inflight", 0); if (rga_max_inflight > 0) { - SetGlobalRgaMaxInflight(rga_max_inflight); + GetRgaGate(rga_gate_).SetMaxInflight(rga_max_inflight); } #endif const bool requested_use_rga = config.ValueOr("use_rga", true); @@ -324,8 +370,14 @@ public: } bool Start() override { + std::string extra; +#if defined(RK3588_ENABLE_RGA) + if (use_rga_) { + extra = " gate=" + rga_gate_ + " max_inflight=" + std::to_string(GetRgaGate(rga_gate_).MaxInflight()); + } +#endif LogInfo("[preprocess] start id=" + id_ + " dst=" + std::to_string(dst_w_) + "x" + std::to_string(dst_h_) + - (use_rga_ ? " (rga)" : " (swscale)")); + (use_rga_ ? " (rga)" : " (swscale)") + extra); return true; } @@ -447,7 +499,9 @@ private: int dst_wstride = Align16(out_w); // For AI input (RGB/BGR), allow a tightly packed output to avoid an extra per-frame memcpy // in downstream nodes (e.g. ai_yolo). - if (dst_packed_ && (out_fmt == PixelFormat::RGB || out_fmt == PixelFormat::BGR)) { + const bool want_packed_rgb = (out_fmt == PixelFormat::RGB || out_fmt == PixelFormat::BGR) && + (!dst_packed_explicit_ || dst_packed_); + if (want_packed_rgb) { dst_wstride = out_w; } int dst_hstride = Align16(out_h); @@ -541,7 +595,7 @@ private: auto RunRgaOnce = [&](int src_fd, std::string& err) -> bool { // Serialize/limit librga/im2d usage; multiple pipelines call RGA concurrently. - ScopedRgaGate guard; + ScopedRgaGate guard(rga_gate_); src_buf = wrapbuffer_fd_t(src_fd, frame->width, frame->height, src_wstride, src_hstride, src_fmt_rga); @@ -854,8 +908,11 @@ private: bool keep_ratio_ = false; PixelFormat dst_fmt_ = PixelFormat::UNKNOWN; bool dst_packed_ = false; + bool dst_packed_explicit_ = false; bool use_rga_ = true; + std::string rga_gate_ = "global"; + bool stats_log_ = false; uint64_t stats_interval_ = 100;