自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

OH-v3.0-LTS Camera相機(jī)驅(qū)動框架(L2)解析之創(chuàng)建流

原創(chuàng)
系統(tǒng) OpenHarmony
本篇接著上章的代碼繼續(xù)往下看,將創(chuàng)建好的StreamInfo作為參數(shù)丟給StreamOperator進(jìn)行流的創(chuàng)建

??想了解更多內(nèi)容,請訪問:??

??51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)??

??https://harmonyos.51cto.com??

接著上章的代碼繼續(xù)往下看,將創(chuàng)建好的StreamInfo作為參數(shù)丟給StreamOperator進(jìn)行流的創(chuàng)建

程序調(diào)用流程圖

1. StreamOperator::CreateStreams()

創(chuàng)建過程

  • 實例化一個stream對象,用stremInfos中的參數(shù)信息對相關(guān)成員進(jìn)行賦值。
  • 創(chuàng)建一個StreamTunnel對象,把StreamTunnel和stream進(jìn)行綁定。
  • 把創(chuàng)建好的每一個stream 對應(yīng)它的stremId 放在 streamMap_里。
//drivers\peripheral\camera\hal\hdi_impl\src\stream_operator\stream_operator.cpp
CamRetCode StreamOperator::CreateStreams(const std::vector<std::shared_ptr<StreamInfo>>& streamInfos)
{
...
for (auto it : streamInfos) {
...

std::shared_ptr<IStream> stream = StreamFactory::Instance().CreateShared(
IStream::g_avaliableStreamType[it->intent_], it->streamId_, it->intent_, pipelineCore_, messenger_);

...

StreamConfiguration scg;
scg.id = it->streamId_;
scg.type = it->intent_;
scg.width = it->width_;
scg.height = it->height_;
PixelFormat pf = static_cast<PixelFormat>(it->format_);
scg.format = BufferAdapter::PixelFormatToCameraFormat(pf);
scg.dataspace = it->datasapce_; // fix misspell
scg.tunnelMode = it->tunneledMode_;
scg.minFrameDuration = it->minFrameDuration_;
scg.encodeType = it->encodeType_;

RetCode rc = stream->ConfigStream(scg);

...

if (it->bufferQueue_ != nullptr) {
auto tunnel = std::make_shared<StreamTunnel>();
CHECK_IF_PTR_NULL_RETURN_VALUE(tunnel, INSUFFICIENT_RESOURCES);
RetCode rc = tunnel->AttachBufferQueue(it->bufferQueue_);
CHECK_IF_NOT_EQUAL_RETURN_VALUE(rc, RC_OK, INVALID_ARGUMENT);
if (stream->AttachStreamTunnel(tunnel) != RC_OK) {
CAMERA_LOGE("attach buffer queue to stream [id = %{public}d] failed", it->streamId_);
return INVALID_ARGUMENT;
}
}
{
std::lock_guard<std::mutex> l(streamLock_);
streamMap_[stream->GetStreamId()] = stream;
}
}
return NO_ERROR;
}

這里需要注意的一個地方RetCode rc = tunnel->AttachBufferQueue(it->bufferQueue_);單獨把這個代碼帖出來看下

//drivers\peripheral\camera\hal\hdi_impl\src\stream_operator\stream_tunnel.cpp
RetCode StreamTunnel::AttachBufferQueue(OHOS::sptr<OHOS::IBufferProducer>& producer)
{
CHECK_IF_PTR_NULL_RETURN_VALUE(producer, RC_ERROR);
bufferQueue_ = OHOS::Surface::CreateSurfaceAsProducer(producer); //Buffer的生產(chǎn)者使用該函數(shù)創(chuàng)建一個Surface,只能使用與生產(chǎn)相關(guān)的接口
CHECK_IF_PTR_NULL_RETURN_VALUE(bufferQueue_, RC_ERROR);
return RC_OK;
}

這個成員函數(shù)的作用就是把應(yīng)用層中的StreamCustomer::CreateProducer()獲取的producer作為參數(shù)創(chuàng)建與“消費者”對應(yīng)的“生成型”Surface。 而StreamTunnel類就是對這個生成型Surface的一個功能封裝。

2. StreamOperator::CommitStreams()

源碼有點長 去掉一些參數(shù)的校驗和準(zhǔn)備相關(guān)的代碼

一共調(diào)用 StreamBase::CommitStream() 、StreamPipelineCore::PreConfig()、StreamPipelineCore::CreatePipeline()三個函數(shù)

下面一個個來看

//drivers\peripheral\camera\hal\hdi_impl\src\stream_operator\stream_operator.cpp
CamRetCode StreamOperator::CommitStreams(OperationMode mode,
const std::shared_ptr<CameraStandard::CameraMetadata>& modeSetting)
{
...
{
std::lock_guard<std::mutex> l(streamLock_);
for (auto it : streamMap_) {
if (it.second->CommitStream() != RC_OK) {
CAMERA_LOGE("commit stream [id = %{public}d] failed.", it.first);
return DEVICE_ERROR;
}
}
}
RetCode rc = streamPipeline_->PreConfig(modeSetting);
if (rc != RC_OK) {
CAMERA_LOGE("prepare mode settings failed");
return DEVICE_ERROR;
}
rc = streamPipeline_->CreatePipeline(mode);
if (rc != RC_OK) {
CAMERA_LOGE("create pipeline failed.");
return INVALID_ARGUMENT;
}

DFX_LOCAL_HITRACE_END;
return NO_ERROR;
}

2.1 StreamBase::CommitStream()

按照代碼LOG中的說明commit a stream to pipeline。大家可以自行理解,本人的理解是

  • 通過BufferManager創(chuàng)建并初始化BufferPool 數(shù)據(jù)緩存池
  • 給這個Stream對應(yīng)的pipeline 創(chuàng)建一個StreamMgr,讓pipeline可以通過StreamMgr對Stream進(jìn)行對應(yīng)的控制。
//drivers\peripheral\camera\hal\hdi_impl\src\stream_operator\stream_base.cpp
RetCode StreamBase::CommitStream()
{
...

HostStreamInfo info;
info.type_ = static_cast<StreamIntent>(streamType_);
info.streamId_ = streamId_;
info.width_ = streamConfig_.width;
info.height_ = streamConfig_.height;
info.format_ = streamConfig_.format;
info.usage_ = streamConfig_.usage;
info.encodeType_ = streamConfig_.encodeType;

if (streamConfig_.tunnelMode) {
BufferManager* mgr = BufferManager::GetInstance();
CHECK_IF_PTR_NULL_RETURN_VALUE(mgr, RC_ERROR);

if (bufferPool_ == nullptr) {
poolId_ = mgr->GenerateBufferPoolId();
CHECK_IF_EQUAL_RETURN_VALUE(poolId_, 0, RC_ERROR);

bufferPool_ = mgr->GetBufferPool(poolId_);
if (bufferPool_ == nullptr) {
CAMERA_LOGE("stream [id:%{public}d] get buffer pool failed.", streamId_);
return RC_ERROR;
}
}

info.bufferPoolId_ = poolId_;
info.bufferCount_ = GetBufferCount();
RetCode rc = bufferPool_->Init(streamConfig_.width, streamConfig_.height, streamConfig_.usage,
streamConfig_.format, GetBufferCount(), CAMERA_BUFFER_SOURCE_TYPE_EXTERNAL);
if (rc != RC_OK) {
CAMERA_LOGE("stream [id:%{public}d] initialize buffer pool failed.", streamId_);
return RC_ERROR;
}
}

RetCode rc = hostStreamMgr_->CreateHostStream(info, [this](std::shared_ptr<IBuffer> buffer) {
HandleResult(buffer);
return;
});

...

return RC_OK;
}

2.2 StreamPipelineCore::PreConfig()

這里的代碼比較簡單,根據(jù)參數(shù)信息,調(diào)用deviceManager的PreConfig接口從來配置下面的硬件設(shè)備。

deviceManager在前面PowerUp的解析已經(jīng)說明了,Hi3516實際上是將硬件適配層相關(guān)的代碼都封閉在了so里面,

所以這里實際調(diào)用就一行代碼sysObject_->PreConfig(meta, settings);

//drivers\peripheral\camera\hal\pipeline_core\pipeline_impl\src\stream_pipeline_core.cpp
RetCode StreamPipelineCore::PreConfig(const ModeMeta& meta)
{
auto deviceManager = IDeviceManager::GetInstance();
CHECK_IF_PTR_NULL_RETURN_VALUE(deviceManager, RC_ERROR);

std::vector<DeviceStreamSetting> settings = {};
std::vector<int32_t> ids = {};
context_->streamMgr_->GetStreamIds(ids);
for (auto i : ids) {
auto info = context_->streamMgr_->GetStreamInfo(i);
DeviceStreamSetting setting = {info.streamId_, info.bufferCount_, info.width_, info.height_,
info.format_, info.usage_, static_cast<CameraEncodeType>(info.encodeType_)};
settings.emplace_back(setting);
}
return deviceManager->PreConfig(meta, settings);
}

2.3 StreamPipelineCore::CreatePipeline()

有關(guān)stategy、builder、dispatcher實現(xiàn)的函數(shù)代碼比較多,大家感興趣自行閱讀??偟恼f一下整體功能:

  • StreamPipelineStrategy 負(fù)責(zé)根據(jù)mode從對應(yīng)的配置文件中創(chuàng)建pipeline。
  • StreamPipelineBuilder 負(fù)責(zé)創(chuàng)建Node并鏈接
  • StreamPipelineDispatcher 負(fù)責(zé)管理創(chuàng)建好的pipeline
  • 最終建立的StreamPipeline是由Node(節(jié)點)組成,Node(節(jié)點)由Port(端口)組成。Port(端口)分為in端口和out端口(可以有多個)。out鏈接in最終構(gòu)建出整個StreamPipeline

Demo案例建立了兩個streamPipeline

//drivers\peripheral\camera\hal\pipeline_core\pipeline_impl\src\stream_pipeline_core.cpp
RetCode StreamPipelineCore::CreatePipeline(const int32_t& mode)
{
std::lock_guard<std::mutex> l(mutex_);
std::shared_ptr<PipelineSpec> spec = strategy_->GeneratePipelineSpec(mode);
if (spec == nullptr) {
return RC_ERROR;
}
std::shared_ptr<Pipeline> pipeline = builder_->Build(spec);
if (pipeline == nullptr) {
return RC_ERROR;
}
return dispatcher_->Update(pipeline);
}

需要注意幾個問題

  1. pipeline的配置文件在drivers\peripheral\camera\hal\pipeline_core\pipeline_impl\src\strategy\config目錄下的config.c和params.c
  2. 這兩個文件是由BUILD.gn 腳本生成的。
//drivers\peripheral\camera\hal\adapter\chipset\hispark_taurus\BUILD.gn
source =
"$camera_path/pipeline_core/pipeline_impl/src/strategy/config/config.c"
exec_script(
"http://drivers/framework/tools/hc-gen/build_hcs.py",
[
"-o",
rebase_path(
"$camera_path/pipeline_core/pipeline_impl/src/strategy/config/config.c"),
"-t",
rebase_path(
"http://vendor/hisilicon/Hi3516DV300/hdf_config/uhdf/camera/hal/mpp/hispark_taurus/pipeline_core/config.hcs"),
],
"")
}

ohos_prebuilt_etc("params.c") {
source =
"$camera_path/pipeline_core/pipeline_impl/src/strategy/config/params.c"
exec_script(
"http://drivers/framework/tools/hc-gen/build_hcs.py",
[
"-o",
rebase_path(
"$camera_path/pipeline_core/pipeline_impl/src/strategy/config/params.c"),
"-t",
rebase_path(
"http://vendor/hisilicon/Hi3516DV300/hdf_config/uhdf/camera/hal/mpp/hispark_taurus/pipeline_core/params.hcs"),
],
"")
}

3. 小結(jié)

到此流的創(chuàng)建也完成了,最后一章節(jié)會把圖像采集的代碼講解完。

??想了解更多內(nèi)容,請訪問:??

??51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)??

??https://harmonyos.51cto.com??

責(zé)任編輯:jianghua 來源: 鴻蒙社區(qū)
相關(guān)推薦

2022-02-25 15:33:45

圖像采集鴻蒙Camera相機(jī)

2022-02-08 15:07:45

OpenHarmor鴻蒙操作系統(tǒng)

2022-01-06 16:16:21

鴻蒙HarmonyOS應(yīng)用

2022-02-21 15:38:57

Openharmon操作系統(tǒng)鴻蒙

2022-02-17 16:47:40

OpenharmonIPC通信鴻蒙

2014-11-25 13:28:17

openstackneutronDVR

2022-06-22 09:14:23

事件打點HiSysEvent

2021-10-20 19:14:30

緩存CacheCPU

2022-04-21 11:26:31

鴻蒙操作系統(tǒng)

2015-01-20 13:19:52

OpenStack網(wǎng)絡(luò)層數(shù)據(jù)鏈路層

2023-11-27 08:21:49

Camera2API,

2023-02-13 15:54:49

2022-07-04 16:41:16

IPC通信HiTrace

2022-07-14 19:03:33

IPC服務(wù)鴻蒙

2021-11-01 17:31:21

Camera2 相機(jī)開發(fā)

2023-01-31 09:12:16

CPU芯片緩存

2021-10-27 11:29:49

Linux框架內(nèi)核

2023-02-20 08:00:00

2023-10-10 15:33:55

機(jī)器學(xué)習(xí)相似性度量

2022-08-26 14:58:43

區(qū)塊鏈比特幣架構(gòu)
點贊
收藏

51CTO技術(shù)棧公眾號