-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer_utils.cpp
More file actions
108 lines (86 loc) · 3.48 KB
/
buffer_utils.cpp
File metadata and controls
108 lines (86 loc) · 3.48 KB
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
#include <android/hardware_buffer.h>
#include <string.h>
#include <unistd.h>
#include "drm_fourcc.h"
#include "vndk/hardware_buffer.h"
#include "buffer_utils.h"
#include <fcntl.h>
#include "cros_gralloc_handle.h"
#include "cros_gralloc_util.h"
extern "C" {
#include <wlr/interfaces/wlr_buffer.h>
#include <wlr/util/log.h>
}
// Convert Android format to DRM format
uint32_t android_to_drm_format(uint32_t android_format) {
switch(android_format) {
case AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM:
return DRM_FORMAT_ABGR8888;
case AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM:
return DRM_FORMAT_XBGR8888;
// Add other formats as needed
default:
return DRM_FORMAT_INVALID;
}
}
// Get DMA-BUF attributes from AHardwareBuffer
bool AHardwareBuffer_getDmabufAttributes(AHardwareBuffer *ahb,
struct wlr_dmabuf_attributes *outAtrribs) {
const buffer_handle_t handle = AHardwareBuffer_getNativeHandle(ahb);
if (!handle) {
wlr_log(WLR_ERROR, "Failed to get handle from AHardwareBuffer");
return false;
}
cros_gralloc_handle* crosHandle = (cros_gralloc_handle*)handle;
log_cros_gralloc_handle(crosHandle);
struct wlr_dmabuf_attributes attribs = cros_gralloc_to_wlr_dmabuf(crosHandle);
// dmabuf->fd[0] = gralloc_handle(handle)->prime_fd;
if (attribs.fd[0] < 0) {
wlr_log(WLR_ERROR, "Failed to get dmabuf fd from AHardwareBuffer");
return false;
}
wlr_log(WLR_DEBUG, "wlr_dmabuf_attributes:");
wlr_log(WLR_DEBUG, " %dx%d, format: 0x%x, modifier: 0x%lx",
attribs.width, attribs.height, attribs.format, attribs.modifier);
for (int i = 0; i < attribs.n_planes; ++i)
wlr_log(WLR_DEBUG, " Plane %d: fd=%d, stride=%u, offset=%u",
i, attribs.fd[i], attribs.stride[i], attribs.offset[i]);
wlr_dmabuf_attributes_copy(outAtrribs, &attribs);
return true;
}
void ANativeWindow_sendWlrBuffer(struct wlr_buffer *wlr_buffer, BufferManager *buffer_presenter) {
struct wlr_dmabuf_attributes dmabuf_attrs = {0};
if (!wlr_buffer_get_dmabuf(wlr_buffer, &dmabuf_attrs)) {
wlr_log(WLR_ERROR, "wlr_buffer_get_dmabuf failed");
return;
}
// Create AHardwareBuffer from DMA-BUF
AHardwareBuffer_Desc ahb_desc = {
.width = static_cast<uint32_t>(wlr_buffer->width),
.height = static_cast<uint32_t>(wlr_buffer->height),
.layers = 1,
.format = AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM,
.usage = AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE | AHARDWAREBUFFER_USAGE_GPU_FRAMEBUFFER,
};
struct cros_gralloc_handle handle = {
.width = static_cast<uint32_t>(dmabuf_attrs.width),
.height = static_cast<uint32_t>(dmabuf_attrs.height),
.format = dmabuf_attrs.format,
.format_modifier = dmabuf_attrs.modifier,
.num_planes = static_cast<uint32_t>(dmabuf_attrs.n_planes),
};
for (int i = 0; i < dmabuf_attrs.n_planes; i++) {
handle.fds[i] = fcntl(dmabuf_attrs.fd[i], F_DUPFD_CLOEXEC, 0);;
handle.strides[i] = dmabuf_attrs.stride[i];
handle.offsets[i] = dmabuf_attrs.offset[i];
}
AHardwareBuffer *ahb;
int ret = AHardwareBuffer_createFromHandle(
&ahb_desc, &handle, AHARDWAREBUFFER_CREATE_FROM_HANDLE_METHOD_REGISTER, &ahb
);
if (ret != 0) {
wlr_log(WLR_ERROR, "Failed to create AHardwareBuffer from handle: %s (%d)", strerror(ret), ret);
return;
}
buffer_presenter_send_buffer(buffer_presenter, ahb, -1, NULL, NULL);
}