1 /* 2 * Copyright (c) 2021 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 #ifndef HOS_CAMERA_V4L2_BUFFER_H 17 #define HOS_CAMERA_V4L2_BUFFER_H 18 19 #include <mutex> 20 #include <map> 21 #include <cstring> 22 #include <linux/videodev2.h> 23 #include <sys/ioctl.h> 24 #include <memory> 25 #include "v4l2_common.h" 26 #if defined(V4L2_UTEST) || defined (V4L2_MAIN_TEST) 27 #include "v4l2_temp.h" 28 #else 29 #include <stream.h> 30 #include <camera.h> 31 #endif 32 33 namespace OHOS::Camera { 34 struct AdapterBuff { 35 void* start; 36 uint32_t length; 37 uint32_t offset; 38 void* userBufPtr; 39 int32_t heapfd; 40 int32_t dmafd; 41 }; 42 class HosV4L2Buffers : public std::enable_shared_from_this<HosV4L2Buffers> { 43 // hide construct function 44 HosV4L2Buffers(enum v4l2_memory memType, enum v4l2_buf_type bufferType); 45 public: CreateHosV4L2Buffers(enum v4l2_memory memType,enum v4l2_buf_type bufferType)46 static std::shared_ptr<HosV4L2Buffers> CreateHosV4L2Buffers(enum v4l2_memory memType, 47 enum v4l2_buf_type bufferType) 48 { 49 struct HosV4L2BuffersHelper : public HosV4L2Buffers { 50 HosV4L2BuffersHelper(enum v4l2_memory memType, enum v4l2_buf_type bufferType) 51 : HosV4L2Buffers(memType, bufferType) {} 52 }; 53 // online code commit rule requires using std::make_shared 54 return std::make_shared<HosV4L2BuffersHelper>(memType, bufferType); 55 } 56 57 virtual ~HosV4L2Buffers(); 58 59 RetCode V4L2ReqBuffers(int fd, int unsigned buffCont); 60 RetCode V4L2ReleaseBuffers(int fd); 61 62 RetCode V4L2QueueBuffer(int fd, const std::shared_ptr<FrameSpec>& frameSpec); 63 RetCode V4L2DequeueBuffer(int fd); 64 65 RetCode V4L2AllocBuffer(int fd, const std::shared_ptr<FrameSpec>& frameSpec); 66 67 void SetV4L2BuffersCallback(BufCallback cb); 68 69 RetCode Flush(int fd); 70 71 private: 72 RetCode SetAdapterBuffer(int fd, struct v4l2_buffer &buf, const std::shared_ptr<FrameSpec>& frameSpec); 73 RetCode SetDmabufOn(struct v4l2_buffer &buf, const std::shared_ptr<FrameSpec>& frameSpec); 74 void MakeInqueueBuffer(struct v4l2_buffer &buf, const std::shared_ptr<FrameSpec>& frameSpec); 75 void SetInqueueBuffer(struct v4l2_buffer &buf, const std::shared_ptr<FrameSpec>& frameSpec); 76 void SetMmapInqueueBuffer(struct v4l2_buffer &buf, const std::shared_ptr<FrameSpec>& frameSpec); 77 void SetDmaInqueueBuffer(struct v4l2_buffer &buf, const std::shared_ptr<FrameSpec>& frameSpec); 78 79 private: 80 BufCallback dequeueBuffer_; 81 82 using FrameMap = std::map<unsigned int, std::shared_ptr<FrameSpec>>; 83 std::map<int, FrameMap> queueBuffers_; 84 85 using AdapterBuffer = struct AdapterBuff; 86 std::map<uint32_t, AdapterBuffer> adapterBufferMap_; 87 88 std::mutex bufferLock_; 89 90 enum v4l2_memory memoryType_; 91 enum v4l2_buf_type bufferType_; 92 93 uint32_t buffLong_; 94 }; 95 } // namespace OHOS::Camera 96 #endif // HOS_CAMERA_V4L2_BUFFER_H 97