From 25fc03fa91e53aa487168de91e769330b72df2ee Mon Sep 17 00:00:00 2001 From: sladro Date: Fri, 16 Jan 2026 20:29:32 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E8=80=A6=E5=BC=80=E5=8F=916?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/hw/atlas_defaults.h | 98 ++++++++++++++++++++++++++++++++++++ include/hw/jetson_defaults.h | 98 ++++++++++++++++++++++++++++++++++++ src/hw_factory.cpp | 60 ++++++++++++++++++++-- tests/test_hw_factory.cpp | 41 +++++++++++++++ 4 files changed, 294 insertions(+), 3 deletions(-) create mode 100644 include/hw/atlas_defaults.h create mode 100644 include/hw/jetson_defaults.h diff --git a/include/hw/atlas_defaults.h b/include/hw/atlas_defaults.h new file mode 100644 index 0000000..b054bb3 --- /dev/null +++ b/include/hw/atlas_defaults.h @@ -0,0 +1,98 @@ +#pragma once + +#include +#include +#include + +#include "hw/i_decoder.h" +#include "hw/i_encoder.h" +#include "hw/i_image_processor.h" +#include "hw/i_infer_backend.h" +#include "utils/result.h" + +namespace rk3588 { + +class AtlasInferBackend : public IInferBackend { +public: + ModelHandle LoadModel(const std::string& /*model_path*/, std::string& err) override { + err = "atlas infer backend not supported"; + return kInvalidModelHandle; + } + + void UnloadModel(ModelHandle /*handle*/) override {} + + bool GetModelInfo(ModelHandle /*handle*/, ModelInfo& /*info*/) const override { + return false; + } + + InferResult Infer(ModelHandle /*handle*/, const InferInput& /*input*/) override { + InferResult res; + res.success = false; + res.error = "atlas infer backend not supported"; + return res; + } + + AiScheduler::BorrowedInferResult InferBorrowed(ModelHandle /*handle*/, const InferInput& /*input*/) override { + AiScheduler::BorrowedInferResult res; + res.success = false; + res.error = "atlas infer backend not supported"; + return res; + } +}; + +class AtlasImageProcessor : public IImageProcessor { +public: + Status Resize(const Frame& /*src*/, Frame& /*dst*/) override { + return FailStatus("atlas image processor not supported"); + } + + Status CvtColor(const Frame& /*src*/, Frame& /*dst*/, PixelFormat /*dst_format*/) override { + return FailStatus("atlas image processor not supported"); + } + + Status Normalize(const Frame& /*src*/, std::vector& /*out*/, + const std::vector& /*mean*/, + const std::vector& /*std*/) override { + return FailStatus("atlas image processor not supported"); + } +}; + +class AtlasDecoder : public IDecoder { +public: + Status Open(const SimpleJson& /*config*/) override { + return FailStatus("atlas decoder not supported"); + } + + Status Send(const DecodePacket& /*packet*/) override { + return FailStatus("atlas decoder not supported"); + } + + Result> Receive() override { + return MakeError>("atlas decoder not supported"); + } + + void Close() override {} +}; + +class AtlasEncoder : public IEncoder { +public: + Status Open(const SimpleJson& /*config*/) override { + return FailStatus("atlas encoder not supported"); + } + + Status Send(const std::shared_ptr& /*frame*/) override { + return FailStatus("atlas encoder not supported"); + } + + Result Receive() override { + return MakeError("atlas encoder not supported"); + } + + std::vector ExtraData() const override { + return {}; + } + + void Close() override {} +}; + +} // namespace rk3588 diff --git a/include/hw/jetson_defaults.h b/include/hw/jetson_defaults.h new file mode 100644 index 0000000..12819f6 --- /dev/null +++ b/include/hw/jetson_defaults.h @@ -0,0 +1,98 @@ +#pragma once + +#include +#include +#include + +#include "hw/i_decoder.h" +#include "hw/i_encoder.h" +#include "hw/i_image_processor.h" +#include "hw/i_infer_backend.h" +#include "utils/result.h" + +namespace rk3588 { + +class JetsonInferBackend : public IInferBackend { +public: + ModelHandle LoadModel(const std::string& /*model_path*/, std::string& err) override { + err = "jetson infer backend not supported"; + return kInvalidModelHandle; + } + + void UnloadModel(ModelHandle /*handle*/) override {} + + bool GetModelInfo(ModelHandle /*handle*/, ModelInfo& /*info*/) const override { + return false; + } + + InferResult Infer(ModelHandle /*handle*/, const InferInput& /*input*/) override { + InferResult res; + res.success = false; + res.error = "jetson infer backend not supported"; + return res; + } + + AiScheduler::BorrowedInferResult InferBorrowed(ModelHandle /*handle*/, const InferInput& /*input*/) override { + AiScheduler::BorrowedInferResult res; + res.success = false; + res.error = "jetson infer backend not supported"; + return res; + } +}; + +class JetsonImageProcessor : public IImageProcessor { +public: + Status Resize(const Frame& /*src*/, Frame& /*dst*/) override { + return FailStatus("jetson image processor not supported"); + } + + Status CvtColor(const Frame& /*src*/, Frame& /*dst*/, PixelFormat /*dst_format*/) override { + return FailStatus("jetson image processor not supported"); + } + + Status Normalize(const Frame& /*src*/, std::vector& /*out*/, + const std::vector& /*mean*/, + const std::vector& /*std*/) override { + return FailStatus("jetson image processor not supported"); + } +}; + +class JetsonDecoder : public IDecoder { +public: + Status Open(const SimpleJson& /*config*/) override { + return FailStatus("jetson decoder not supported"); + } + + Status Send(const DecodePacket& /*packet*/) override { + return FailStatus("jetson decoder not supported"); + } + + Result> Receive() override { + return MakeError>("jetson decoder not supported"); + } + + void Close() override {} +}; + +class JetsonEncoder : public IEncoder { +public: + Status Open(const SimpleJson& /*config*/) override { + return FailStatus("jetson encoder not supported"); + } + + Status Send(const std::shared_ptr& /*frame*/) override { + return FailStatus("jetson encoder not supported"); + } + + Result Receive() override { + return MakeError("jetson encoder not supported"); + } + + std::vector ExtraData() const override { + return {}; + } + + void Close() override {} +}; + +} // namespace rk3588 diff --git a/src/hw_factory.cpp b/src/hw_factory.cpp index 9d4199d..ecad69c 100644 --- a/src/hw_factory.cpp +++ b/src/hw_factory.cpp @@ -1,22 +1,76 @@ #include "hw/hw_factory.h" +#include + +#include "hw/atlas_defaults.h" +#include "hw/jetson_defaults.h" #include "hw/rk3588_defaults.h" namespace rk3588 { -std::shared_ptr HwFactory::CreateInferBackend(const SimpleJson& /*config*/) { +namespace { + +std::string NormalizePlatform(const SimpleJson& config) { + std::string platform = config.ValueOr("platform", ""); + if (platform.empty()) { + platform = config.ValueOr("hw_platform", ""); + } + if (platform.empty()) { + return "rk3588"; + } + std::string normalized; + normalized.reserve(platform.size()); + for (char c : platform) { + if (!std::isspace(static_cast(c))) { + normalized.push_back(static_cast(std::tolower(static_cast(c)))); + } + } + return normalized.empty() ? "rk3588" : normalized; +} + +} // namespace + +std::shared_ptr HwFactory::CreateInferBackend(const SimpleJson& config) { + const std::string platform = NormalizePlatform(config); + if (platform == "atlas") { + return std::make_shared(); + } + if (platform == "jetson") { + return std::make_shared(); + } return std::make_shared(); } std::shared_ptr HwFactory::CreateImageProcessor(const SimpleJson& config) { + const std::string platform = NormalizePlatform(config); + if (platform == "atlas") { + return std::make_shared(); + } + if (platform == "jetson") { + return std::make_shared(); + } return std::make_shared(config); } -std::shared_ptr HwFactory::CreateDecoder(const SimpleJson& /*config*/) { +std::shared_ptr HwFactory::CreateDecoder(const SimpleJson& config) { + const std::string platform = NormalizePlatform(config); + if (platform == "atlas") { + return std::make_shared(); + } + if (platform == "jetson") { + return std::make_shared(); + } return std::make_shared(); } -std::shared_ptr HwFactory::CreateEncoder(const SimpleJson& /*config*/) { +std::shared_ptr HwFactory::CreateEncoder(const SimpleJson& config) { + const std::string platform = NormalizePlatform(config); + if (platform == "atlas") { + return std::make_shared(); + } + if (platform == "jetson") { + return std::make_shared(); + } return std::make_shared(); } diff --git a/tests/test_hw_factory.cpp b/tests/test_hw_factory.cpp index 3dc3184..43f33f1 100644 --- a/tests/test_hw_factory.cpp +++ b/tests/test_hw_factory.cpp @@ -1,9 +1,12 @@ #include +#include #include +#include "hw/atlas_defaults.h" #include "hw/frame_buffer.h" #include "hw/hw_factory.h" +#include "hw/jetson_defaults.h" #include "hw/rk3588_defaults.h" namespace rk3588 { @@ -28,6 +31,44 @@ TEST(HwFactoryTest, DefaultsReturnsRk3588Impls) { EXPECT_NE(std::dynamic_pointer_cast(encoder), nullptr); } +TEST(HwFactoryTest, SelectsBackendByConfig) { + SimpleJson::Object cfg_obj; + cfg_obj["platform"] = SimpleJson(std::string("atlas")); + SimpleJson config(std::move(cfg_obj)); + + auto infer = HwFactory::CreateInferBackend(config); + auto image = HwFactory::CreateImageProcessor(config); + auto decoder = HwFactory::CreateDecoder(config); + auto encoder = HwFactory::CreateEncoder(config); + + EXPECT_NE(std::dynamic_pointer_cast(infer), nullptr); + EXPECT_NE(std::dynamic_pointer_cast(image), nullptr); + EXPECT_NE(std::dynamic_pointer_cast(decoder), nullptr); + EXPECT_NE(std::dynamic_pointer_cast(encoder), nullptr); +} + +TEST(HwFactoryTest, PlatformImplsSmokeConstruct) { + auto atlas_infer = std::make_shared(); + auto atlas_image = std::make_shared(); + auto atlas_decoder = std::make_shared(); + auto atlas_encoder = std::make_shared(); + + auto jetson_infer = std::make_shared(); + auto jetson_image = std::make_shared(); + auto jetson_decoder = std::make_shared(); + auto jetson_encoder = std::make_shared(); + + EXPECT_NE(atlas_infer, nullptr); + EXPECT_NE(atlas_image, nullptr); + EXPECT_NE(atlas_decoder, nullptr); + EXPECT_NE(atlas_encoder, nullptr); + + EXPECT_NE(jetson_infer, nullptr); + EXPECT_NE(jetson_image, nullptr); + EXPECT_NE(jetson_decoder, nullptr); + EXPECT_NE(jetson_encoder, nullptr); +} + TEST(FrameBufferTest, MetadataPreserved) { FrameBuffer::Plane plane0; plane0.data = reinterpret_cast(0x1000);