解耦开发6

This commit is contained in:
sladro 2026-01-16 20:29:32 +08:00
parent d2a40d97cf
commit 25fc03fa91
4 changed files with 294 additions and 3 deletions

View File

@ -0,0 +1,98 @@
#pragma once
#include <memory>
#include <string>
#include <vector>
#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<float>& /*out*/,
const std::vector<float>& /*mean*/,
const std::vector<float>& /*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<std::shared_ptr<Frame>> Receive() override {
return MakeError<std::shared_ptr<Frame>>("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>& /*frame*/) override {
return FailStatus("atlas encoder not supported");
}
Result<EncodePacket> Receive() override {
return MakeError<EncodePacket>("atlas encoder not supported");
}
std::vector<uint8_t> ExtraData() const override {
return {};
}
void Close() override {}
};
} // namespace rk3588

View File

@ -0,0 +1,98 @@
#pragma once
#include <memory>
#include <string>
#include <vector>
#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<float>& /*out*/,
const std::vector<float>& /*mean*/,
const std::vector<float>& /*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<std::shared_ptr<Frame>> Receive() override {
return MakeError<std::shared_ptr<Frame>>("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>& /*frame*/) override {
return FailStatus("jetson encoder not supported");
}
Result<EncodePacket> Receive() override {
return MakeError<EncodePacket>("jetson encoder not supported");
}
std::vector<uint8_t> ExtraData() const override {
return {};
}
void Close() override {}
};
} // namespace rk3588

View File

@ -1,22 +1,76 @@
#include "hw/hw_factory.h"
#include <cctype>
#include "hw/atlas_defaults.h"
#include "hw/jetson_defaults.h"
#include "hw/rk3588_defaults.h"
namespace rk3588 {
std::shared_ptr<IInferBackend> HwFactory::CreateInferBackend(const SimpleJson& /*config*/) {
namespace {
std::string NormalizePlatform(const SimpleJson& config) {
std::string platform = config.ValueOr<std::string>("platform", "");
if (platform.empty()) {
platform = config.ValueOr<std::string>("hw_platform", "");
}
if (platform.empty()) {
return "rk3588";
}
std::string normalized;
normalized.reserve(platform.size());
for (char c : platform) {
if (!std::isspace(static_cast<unsigned char>(c))) {
normalized.push_back(static_cast<char>(std::tolower(static_cast<unsigned char>(c))));
}
}
return normalized.empty() ? "rk3588" : normalized;
}
} // namespace
std::shared_ptr<IInferBackend> HwFactory::CreateInferBackend(const SimpleJson& config) {
const std::string platform = NormalizePlatform(config);
if (platform == "atlas") {
return std::make_shared<AtlasInferBackend>();
}
if (platform == "jetson") {
return std::make_shared<JetsonInferBackend>();
}
return std::make_shared<Rk3588InferBackend>();
}
std::shared_ptr<IImageProcessor> HwFactory::CreateImageProcessor(const SimpleJson& config) {
const std::string platform = NormalizePlatform(config);
if (platform == "atlas") {
return std::make_shared<AtlasImageProcessor>();
}
if (platform == "jetson") {
return std::make_shared<JetsonImageProcessor>();
}
return std::make_shared<Rk3588ImageProcessor>(config);
}
std::shared_ptr<IDecoder> HwFactory::CreateDecoder(const SimpleJson& /*config*/) {
std::shared_ptr<IDecoder> HwFactory::CreateDecoder(const SimpleJson& config) {
const std::string platform = NormalizePlatform(config);
if (platform == "atlas") {
return std::make_shared<AtlasDecoder>();
}
if (platform == "jetson") {
return std::make_shared<JetsonDecoder>();
}
return std::make_shared<Rk3588Decoder>();
}
std::shared_ptr<IEncoder> HwFactory::CreateEncoder(const SimpleJson& /*config*/) {
std::shared_ptr<IEncoder> HwFactory::CreateEncoder(const SimpleJson& config) {
const std::string platform = NormalizePlatform(config);
if (platform == "atlas") {
return std::make_shared<AtlasEncoder>();
}
if (platform == "jetson") {
return std::make_shared<JetsonEncoder>();
}
return std::make_shared<Rk3588Encoder>();
}

View File

@ -1,9 +1,12 @@
#include <gtest/gtest.h>
#include <memory>
#include <vector>
#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<Rk3588Encoder>(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<AtlasInferBackend>(infer), nullptr);
EXPECT_NE(std::dynamic_pointer_cast<AtlasImageProcessor>(image), nullptr);
EXPECT_NE(std::dynamic_pointer_cast<AtlasDecoder>(decoder), nullptr);
EXPECT_NE(std::dynamic_pointer_cast<AtlasEncoder>(encoder), nullptr);
}
TEST(HwFactoryTest, PlatformImplsSmokeConstruct) {
auto atlas_infer = std::make_shared<AtlasInferBackend>();
auto atlas_image = std::make_shared<AtlasImageProcessor>();
auto atlas_decoder = std::make_shared<AtlasDecoder>();
auto atlas_encoder = std::make_shared<AtlasEncoder>();
auto jetson_infer = std::make_shared<JetsonInferBackend>();
auto jetson_image = std::make_shared<JetsonImageProcessor>();
auto jetson_decoder = std::make_shared<JetsonDecoder>();
auto jetson_encoder = std::make_shared<JetsonEncoder>();
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<uint8_t*>(0x1000);