• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "v4l2_common.h"
25 #if defined(V4L2_UTEST) || defined (V4L2_MAIN_TEST)
26 #include "v4l2_temp.h"
27 #else
28 #include <stream.h>
29 #include <camera.h>
30 #endif
31 
32 namespace OHOS::Camera {
33 struct AdapterBuff {
34     void* start;
35     uint32_t length;
36     uint32_t offset;
37     void* userBufPtr;
38     int32_t heapfd;
39     int32_t dmafd;
40 };
41 class HosV4L2Buffers {
42 public:
43     HosV4L2Buffers(enum v4l2_memory memType, enum v4l2_buf_type bufferType);
44     ~HosV4L2Buffers();
45 
46     RetCode V4L2ReqBuffers(int fd, int unsigned buffCont);
47     RetCode V4L2ReleaseBuffers(int fd);
48 
49     RetCode V4L2QueueBuffer(int fd, const std::shared_ptr<FrameSpec>& frameSpec);
50     RetCode V4L2DequeueBuffer(int fd);
51 
52     RetCode V4L2AllocBuffer(int fd, const std::shared_ptr<FrameSpec>& frameSpec);
53 
54     void SetCallback(BufCallback cb);
55 
56     RetCode Flush(int fd);
57 
58 private:
59     RetCode SetAdapterBuffer(int fd, struct v4l2_buffer &buf, const std::shared_ptr<FrameSpec>& frameSpec);
60     RetCode SetDmabufOn(struct v4l2_buffer &buf, const std::shared_ptr<FrameSpec>& frameSpec);
61     void MakeInqueueBuffer(struct v4l2_buffer &buf, const std::shared_ptr<FrameSpec>& frameSpec);
62     void SetInqueueBuffer(struct v4l2_buffer &buf, const std::shared_ptr<FrameSpec>& frameSpec);
63     void SetMmapInqueueBuffer(struct v4l2_buffer &buf, const std::shared_ptr<FrameSpec>& frameSpec);
64     void SetDmaInqueueBuffer(struct v4l2_buffer &buf, const std::shared_ptr<FrameSpec>& frameSpec);
65 
66 private:
67     BufCallback dequeueBuffer_;
68 
69     using FrameMap = std::map<unsigned int, std::shared_ptr<FrameSpec>>;
70     std::map<int, FrameMap> queueBuffers_;
71 
72     using AdapterBuffer = struct AdapterBuff;
73     std::map<uint32_t, AdapterBuffer> adapterBufferMap_;
74 
75     std::mutex bufferLock_;
76 
77     enum v4l2_memory memoryType_;
78     enum v4l2_buf_type bufferType_;
79 };
80 } // namespace OHOS::Camera
81 #endif // HOS_CAMERA_V4L2_BUFFER_H
82