xref: /OK3568_Linux_fs/external/camera_engine_rkaiq/rkaiq/xcore/drm_buffer.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * DrmBuffer.h - DRM Buffer Implementation
3  *
4  *  Copyright (c) 2021 Rockchip Corporation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 #ifndef _DRM_BUFFER_H_
20 #define _DRM_BUFFER_H_
21 
22 extern "C" {
23 #include <drm/drm_fourcc.h>
24 }
25 #include <sys/types.h>
26 
27 #include <memory>
28 #include <vector>
29 
30 #include "buffer_pool.h"
31 
32 extern "C" {
33 struct DrmDumbObject;
34 }
35 
36 namespace XCam {
37 
38 class DmaBuffer;
39 class DrmDevice;
40 class DrmBufferProxy;
41 class DrmBufferPool;
42 
43 class DrmBuffer : public BufferData {
44  public:
45     DrmBuffer()                 = delete;
46     DrmBuffer(const DrmBuffer&) = delete;
47     DrmBuffer& operator=(const DrmBuffer&) = delete;
48     DrmBuffer(const std::shared_ptr<DrmDevice>& dev, std::unique_ptr<DrmDumbObject> dumb_object);
49     virtual ~DrmBuffer();
50 
51     virtual uint8_t* map();
52     virtual bool unmap();
53     virtual int get_fd();
54     size_t getSize();
55     // default single plane
56     uint8_t* map(unsigned int plane);
57     bool unmap(unsigned int plane);
58     int getFd(unsigned int plane);
59     size_t getSize(unsigned int plane);
60     int numPlanes();
61     DrmDumbObject* get_bo();
62 
63  private:
64     std::weak_ptr<DrmDevice> drm_device_;
65     std::unique_ptr<DrmDumbObject> dumb_object_;
66     std::vector<std::unique_ptr<DmaBuffer>> dma_bufs_;
67 };
68 
69 class DrmBufferPool : public BufferPool {
70     friend class DrmBuffer;
71 
72  public:
73     explicit DrmBufferPool(std::shared_ptr<DrmDevice> device);
74     virtual ~DrmBufferPool() = default;
75     DrmBufferPool(const DrmBufferPool&) = delete;
76     DrmBufferPool& operator=(const DrmBufferPool&) = delete;
77 
78  protected:
79     virtual bool fixate_video_info (VideoBufferInfo &info);
80     virtual SmartPtr<BufferData> allocate_data(const VideoBufferInfo& buffer_info);
81     virtual SmartPtr<BufferProxy> create_buffer_from_data(SmartPtr<BufferData>& data);
82 
83  private:
84     std::shared_ptr<DrmDevice> drm_device_;
85 };
86 
87 class DrmBufferProxy : public virtual BufferProxy {
88     friend class DrmBufferPool;
89     friend class DrmDevice;
90 
91  public:
92     explicit DrmBufferProxy(const VideoBufferInfo& info, const SmartPtr<DrmBuffer>& data);
93     virtual ~DrmBufferProxy() = default;
94     DrmDumbObject* get_bo();
95 
96     int GetFd();
97 };
98 
99 }  // namespace XCam
100 
101 #endif  // _DRM_BUFFER_H_
102 
103