hc
2023-11-22 f743a7adbd6e230d66a6206fa115b59fec2d88eb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * drm_buffer.cpp - DRM Buffer Implementation
 *
 *  Copyright (c) 2021 Rockchip Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
#include "drm_buffer.h"
 
#include <drm/drm.h>
#include <sys/mman.h>
#include <xf86drm.h>
 
#include <memory>
#include <string>
 
#include "dma_buffer.h"
#include "drm_device.h"
#include "xcam_defs.h"
#include "xcam_log.h"
 
namespace XCam {
 
DrmBuffer::~DrmBuffer() {
    for (auto it = dma_bufs_.begin(); it != dma_bufs_.end();) {
        dma_bufs_.erase(it);
    }
    auto dev = drm_device_.lock();
    if (dev != nullptr) {
        dev->DestroyDumbObject(dumb_object_);
    }
}
 
DrmBuffer::DrmBuffer(const std::shared_ptr<DrmDevice>& dev,
                     std::unique_ptr<DrmDumbObject> dumb_object)
    : drm_device_(std::weak_ptr<DrmDevice>(dev)) {
    dumb_object_ = std::move(dumb_object);
    for (int i = 0; i < dumb_object_->num_planes; i++) {
        dma_bufs_.push_back(std::unique_ptr<DmaBuffer>(
            new DmaBuffer(dumb_object_->fds[i], dumb_object_->sizes[i])));
    }
}
 
int DrmBuffer::numPlanes() { return dma_bufs_.size(); }
 
uint8_t* DrmBuffer::map(unsigned int plane) {
    if (plane >= dma_bufs_.size()) {
        return nullptr;
    }
    auto&& buf = dma_bufs_.at(plane);
    if (!buf->mapped()) {
        auto dev = drm_device_.lock();
        dev->RequestMapDumbObject(dumb_object_, plane);
    }
    auto ptr = reinterpret_cast<uint8_t*>(buf->map());
    buf->beginCpuAccess(DmaBufferDirection::kBidirectional);
    return ptr;
}
 
uint8_t* DrmBuffer::map() { return map(0); }
 
bool DrmBuffer::unmap(unsigned int plane) {
    if (plane >= dma_bufs_.size()) {
        return false;
    }
    auto&& buf = dma_bufs_.at(plane);
    buf->endCpuAccess(DmaBufferDirection::kBidirectional);
    buf->unmap();
    return true;
}
 
bool DrmBuffer::unmap() { return unmap(0); }
 
int DrmBuffer::getFd(unsigned int plane) {
    if (plane >= dma_bufs_.size()) {
        return -1;
    }
    return dma_bufs_.at(plane)->getFd();
}
 
size_t DrmBuffer::getSize() { return getSize(0); }
 
size_t DrmBuffer::getSize(unsigned int plane) { return dma_bufs_.at(plane)->getSize(); }
 
int DrmBuffer::get_fd() { return getFd(0); }
 
DrmDumbObject* DrmBuffer::get_bo() { return dumb_object_.get(); }
 
DrmBufferProxy::DrmBufferProxy(const VideoBufferInfo& info, const SmartPtr<DrmBuffer>& data)
    : BufferProxy(info, data) {
    XCAM_ASSERT(data.ptr());
}
 
DrmDumbObject* DrmBufferProxy::get_bo() {
    auto data   = get_buffer_data();
    auto buffer = data.dynamic_cast_ptr<DrmBuffer>();
 
    XCAM_FAIL_RETURN(WARNING, buffer.ptr(), NULL, "DrmBuffer get_buffer_data failed with NULL");
 
    return buffer->get_bo();
}
 
const int DrmBufferProxy::GetFd() {
    auto data   = get_buffer_data();
    auto buffer = data.dynamic_cast_ptr<DrmBuffer>();
 
    return buffer->get_fd();
}
 
DrmBufferPool::DrmBufferPool(std::shared_ptr<DrmDevice> device) : drm_device_(device) {}
 
bool DrmBufferPool::fixate_video_info(VideoBufferInfo& info) {
    VideoBufferInfo out_info;
 
    out_info.init(info.format, info.width, info.height, info.aligned_width, info.aligned_height);
 
    info = out_info;
 
    return true;
}
 
SmartPtr<BufferData> DrmBufferPool::allocate_data(const VideoBufferInfo& buffer_info) {
    SmartPtr<DrmBuffer> buffer_data;
    auto bo = drm_device_->CreateDumbObject(buffer_info.aligned_width, buffer_info.aligned_height,
                                            buffer_info.color_bits, 1);
    if (bo != nullptr) {
        buffer_data = new DrmBuffer(drm_device_, std::move(bo));
    }
    return buffer_data;
}
 
SmartPtr<BufferProxy> DrmBufferPool::create_buffer_from_data(SmartPtr<BufferData>& data) {
    const VideoBufferInfo& info     = get_video_info();
    SmartPtr<DrmBuffer> buffer_data = data.dynamic_cast_ptr<DrmBuffer>();
    XCAM_ASSERT(buffer_data.ptr());
 
    SmartPtr<DrmBufferProxy> out_buf = new DrmBufferProxy(info, buffer_data);
    XCAM_ASSERT(out_buf.ptr());
    return out_buf;
}
 
}  // namespace XCam