• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "dbuffer_manager.h"
17 #include <buffer_handle_utils.h>
18 #include "distributed_hardware_log.h"
19 
20 namespace OHOS {
21 namespace DistributedHardware {
AcquireBuffer()22 std::shared_ptr<DImageBuffer> DBufferManager::AcquireBuffer()
23 {
24     std::unique_lock<std::mutex> l(lock_);
25 
26     if (!idleList_.empty()) {
27         auto it = idleList_.begin();
28         busyList_.splice(busyList_.begin(), idleList_, it);
29         DHLOGD("Acquire buffer success, index = %d", (*it)->GetIndex());
30         return *it;
31     }
32     return nullptr;
33 }
34 
AddBuffer(std::shared_ptr<DImageBuffer> & buffer)35 RetCode DBufferManager::AddBuffer(std::shared_ptr<DImageBuffer>& buffer)
36 {
37     std::unique_lock<std::mutex> l(lock_);
38     if (idleList_.size() + busyList_.size() >= BUFFER_QUEUE_SIZE) {
39         DHLOGI("Buffer list is full, cannot add buffer.");
40         return RC_ERROR;
41     }
42     idleList_.emplace_back(buffer);
43 
44     return RC_OK;
45 }
46 
RemoveBuffer(std::shared_ptr<DImageBuffer> & buffer)47 RetCode DBufferManager::RemoveBuffer(std::shared_ptr<DImageBuffer>& buffer)
48 {
49     std::unique_lock<std::mutex> l(lock_);
50 
51     auto it = std::find(busyList_.begin(), busyList_.end(), buffer);
52     if (it == busyList_.end()) {
53         DHLOGE("Busy list is empty, cannot remove buffer.");
54         return RC_ERROR;
55     }
56     busyList_.erase(it);
57 
58     return RC_OK;
59 }
60 
NotifyStop(bool state)61 void DBufferManager::NotifyStop(bool state)
62 {
63     streamStop_ = state;
64 }
65 
SurfaceBufferToDImageBuffer(const OHOS::sptr<OHOS::SurfaceBuffer> & surfaceBuffer,const std::shared_ptr<DImageBuffer> & buffer)66 RetCode DBufferManager::SurfaceBufferToDImageBuffer(const OHOS::sptr<OHOS::SurfaceBuffer> &surfaceBuffer,
67     const std::shared_ptr<DImageBuffer> &buffer)
68 {
69     if (surfaceBuffer == nullptr) {
70         DHLOGE("Convert surface buffer failed, surfaceBuffer is null.");
71         return RC_ERROR;
72     }
73 
74     BufferHandle *bufHandle = surfaceBuffer->GetBufferHandle();
75     if (bufHandle == nullptr) {
76         DHLOGE("Convert surface buffer failed, BufferHandle is null.");
77         return RC_ERROR;
78     }
79     if ((bufHandle->size <= 0) || (bufHandle->width <= 0) || (bufHandle->height <= 0) || (bufHandle->usage <= 0)) {
80         DHLOGE("Convert surface buffer failed, BufferHandle is invalid.");
81         return RC_ERROR;
82     }
83 
84     buffer->SetPhyAddress(bufHandle->phyAddr);
85     buffer->SetFileDescriptor(bufHandle->fd);
86     buffer->SetStride(bufHandle->stride);
87     buffer->SetWidth(bufHandle->width);
88     buffer->SetHeight(bufHandle->height);
89     buffer->SetFormat(PixelFormatToDCameraFormat(static_cast<PixelFormat>(bufHandle->format)));
90     buffer->SetUsage(CameraUsageToGrallocUsage(bufHandle->usage));
91     buffer->SetSize(static_cast<uint32_t>(bufHandle->size));
92     buffer->SetBufferHandle(bufHandle);
93 
94     return RC_OK;
95 }
96 
CameraUsageToGrallocUsage(const uint64_t cameraUsage)97 uint64_t DBufferManager::CameraUsageToGrallocUsage(const uint64_t cameraUsage)
98 {
99     uint64_t grallocUsage = 0;
100     uint64_t test = 1;
101     const uint32_t BYTE = 8;
102     for (uint32_t i = 0; i < sizeof(cameraUsage) * BYTE; i++) {
103         switch (cameraUsage & (test << i)) {
104             case CAMERA_USAGE_SW_READ_OFTEN:
105                 grallocUsage |= BUFFER_USAGE_CPU_READ;
106                 break;
107             case CAMERA_USAGE_SW_WRITE_OFTEN:
108                 grallocUsage |= BUFFER_USAGE_CPU_WRITE;
109                 break;
110             case CAMERA_USAGE_MEM_DMA:
111                 grallocUsage |= BUFFER_USAGE_MEM_DMA;
112                 break;
113             default:
114                 break;
115         }
116     }
117 
118     return grallocUsage;
119 }
120 
PixelFormatToDCameraFormat(const PixelFormat format)121 uint32_t DBufferManager::PixelFormatToDCameraFormat(const PixelFormat format)
122 {
123     uint32_t cameraFormat = OHOS_CAMERA_FORMAT_INVALID;
124     switch (format) {
125         case PIXEL_FMT_RGBA_8888:
126             cameraFormat = OHOS_CAMERA_FORMAT_RGBA_8888;
127             break;
128         case PIXEL_FMT_YCBCR_420_SP:
129             cameraFormat = OHOS_CAMERA_FORMAT_YCBCR_420_888;
130             break;
131         case PIXEL_FMT_YCRCB_420_SP:
132             cameraFormat = OHOS_CAMERA_FORMAT_YCRCB_420_SP;
133             break;
134         default:
135             cameraFormat = OHOS_CAMERA_FORMAT_INVALID;
136             break;
137     }
138 
139     return cameraFormat;
140 }
141 
DImageBufferToDCameraBuffer(const std::shared_ptr<DImageBuffer> & imageBuffer,DCameraBuffer & buffer)142 RetCode DBufferManager::DImageBufferToDCameraBuffer(const std::shared_ptr<DImageBuffer> &imageBuffer,
143     DCameraBuffer &buffer)
144 {
145     BufferHandle *bufHandle = imageBuffer->GetBufferHandle();
146     if (bufHandle == nullptr) {
147         DHLOGE("Convert image surface buffer failed, BufferHandle is null.");
148         return RC_ERROR;
149     }
150     if ((bufHandle->size <= 0) || (bufHandle->width <= 0) || (bufHandle->height <= 0) || (bufHandle->usage <= 0)) {
151         DHLOGE("Convert image surface buffer failed, BufferHandle is invalid.");
152         return RC_ERROR;
153     }
154     buffer.index_ = imageBuffer->GetIndex();
155     buffer.size_ = imageBuffer->GetSize();
156     buffer.bufferHandle_ = new NativeBuffer();
157     buffer.bufferHandle_->SetBufferHandle(bufHandle);
158     return RC_OK;
159 }
160 } // namespace DistributedHardware
161 } // namespace OHOS
162