-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer_presenter.cpp
More file actions
164 lines (139 loc) · 5.16 KB
/
buffer_presenter.cpp
File metadata and controls
164 lines (139 loc) · 5.16 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
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
154
155
156
157
158
159
160
161
162
163
164
#include "buffer_presenter.h"
#include <android/api-level.h>
#include <android/surface_control.h>
#include <android/log.h>
#include <cstddef>
#include <cstdlib>
#include <new>
#include <unistd.h> // For close()
#include <android/api-level.h> // For close()
extern "C" {
#include <wlr/util/log.h>
}
#define ALOGE(...) wlr_log(WLR_ERROR, __VA_ARGS__)
#define ALOGI(...) wlr_log(WLR_DEBUG, __VA_ARGS__)
/**
* @brief The internal state of the BufferManager.
*/
struct BufferManager {
ASurfaceControl* surface_control;
};
/**
* This struct is used on older APIs to pass the user's callback
* and context through the ASurfaceTransaction_setOnComplete mechanism
* to our adapter
*/
struct OnCompleteContext {
BufferManager_OnReleaseCallback user_callback;
void* user_context;
};
/**
* @brief OnComplete callback for APIs < 36.
*
* This function is registered with ASurfaceTransaction_setOnComplete.
*/
static void on_complete_pre_api36(void* context, ASurfaceTransactionStats* stats) {
if (!context || !stats) {
if (context) delete static_cast<OnCompleteContext*>(context);
ALOGE("Pre API 36 adapter callback received null context or stats.");
return;
}
OnCompleteContext* wrapped_context = static_cast<OnCompleteContext*>(context);
if (wrapped_context->user_callback) {
wrapped_context->user_callback(wrapped_context->user_context, -1 /* Already released */);
}
// Clean up the context wrapper we allocated.
delete wrapped_context;
}
// --- End of API-specific implementation details ---
BufferManager* buffer_presenter_create(ANativeWindow* window) {
if (!window) {
ALOGE("Cannot create BufferManager with a null ANativeWindow.");
return nullptr;
}
ASurfaceControl* surface_control = ASurfaceControl_createFromWindow(window, "BufferManagerSurfaceControl");
if (!surface_control) {
ALOGE("Failed to create ASurfaceControl from window.");
return nullptr;
}
BufferManager* manager = new (std::nothrow) BufferManager();
if (!manager) {
ALOGE("Failed to allocate memory for BufferManager.");
ASurfaceControl_release(surface_control);
return nullptr;
}
manager->surface_control = surface_control;
ALOGI("BufferManager created for window %p with SurfaceControl %p", window, surface_control);
return manager;
}
void buffer_presenter_destroy(BufferManager* manager) {
if (!manager) {
return;
}
if (manager->surface_control) {
ALOGI("Releasing ASurfaceControl");
ASurfaceControl_release(manager->surface_control);
manager->surface_control = nullptr;
}
delete manager;
ALOGI("BufferManager destroyed.");
}
void buffer_presenter_send_buffer(BufferManager* manager,
AHardwareBuffer* buffer,
int acquire_fence_fd,
BufferManager_OnReleaseCallback on_release_callback,
void* context) {
if (manager->surface_control == nullptr) {
ALOGE("ASurfaceControl is null");
// We are now responsible for the fence fd.
if (acquire_fence_fd >= 0) {
close(acquire_fence_fd);
}
return;
}
if (!buffer) {
ALOGE("Input AHardwareBuffer is null.");
// We are now responsible for the fence fd.
if (acquire_fence_fd >= 0) {
close(acquire_fence_fd);
}
return;
}
ASurfaceTransaction* transaction = ASurfaceTransaction_create();
if (!transaction) {
ALOGE("Failed to create ASurfaceTransaction.");
// We are now responsible for the fence fd.
if (acquire_fence_fd >= 0) {
close(acquire_fence_fd);
}
return;
}
// TODO: When termux upgrades ndk-sysroot to r28
/*if (android_get_device_api_level() > __ANDROID_API_V__)
if (on_release_callback) {
ASurfaceTransaction_setBufferWithRelease(
transaction, manager->surface_control, buffer, acquire_fence_fd,
context, on_release_callback);
} else {
// If no callback is needed, use the simpler function.
ASurfaceTransaction_setBuffer(transaction, manager->surface_control, buffer, acquire_fence_fd);
}
else {*/
ASurfaceTransaction_setBuffer(transaction, manager->surface_control, buffer, acquire_fence_fd);
if (on_release_callback) {
// We must allocate a temporary context to pass user callback and context.
OnCompleteContext* callback_context = new (std::nothrow) OnCompleteContext{
.user_callback = on_release_callback,
.user_context = context,
};
if (callback_context) {
ASurfaceTransaction_setOnComplete(transaction, callback_context, on_complete_pre_api36);
} else {
ALOGE("Failed to allocate memory for OnCompleteContext wrapper.");
// We can't set the callback, but we can still try to apply the transaction below.
}
}
//}
ASurfaceTransaction_apply(transaction);
ASurfaceTransaction_delete(transaction);
}