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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-FileCopyrightText: Copyright 2014 The Android Open Source Project
// SPDX-License-Identifier: GPL-3.0-or-later
// Parts of this implementation were based on:
// https://cs.android.com/android/platform/superproject/+/android-5.1.1_r38:frameworks/native/libs/gui/BufferQueueConsumer.cpp
#include "common/assert.h"
#include "common/logging/log.h"
#include "core/hle/service/nvnflinger/buffer_item.h"
#include "core/hle/service/nvnflinger/buffer_queue_consumer.h"
#include "core/hle/service/nvnflinger/buffer_queue_core.h"
#include "core/hle/service/nvnflinger/parcel.h"
#include "core/hle/service/nvnflinger/producer_listener.h"
namespace Service::android {
BufferQueueConsumer::BufferQueueConsumer(std::shared_ptr<BufferQueueCore> core_)
: core{std::move(core_)}, slots{core->slots} {}
BufferQueueConsumer::~BufferQueueConsumer() = default;
Status BufferQueueConsumer::AcquireBuffer(BufferItem* out_buffer,
std::chrono::nanoseconds expected_present) {
std::scoped_lock lock{core->mutex};
// Check that the consumer doesn't currently have the maximum number of buffers acquired.
const s32 num_acquired_buffers{
static_cast<s32>(std::count_if(slots.begin(), slots.end(), [](const auto& slot) {
return slot.buffer_state == BufferState::Acquired;
}))};
if (num_acquired_buffers >= core->max_acquired_buffer_count + 1) {
LOG_ERROR(Service_Nvnflinger, "max acquired buffer count reached: {} (max {})",
num_acquired_buffers, core->max_acquired_buffer_count);
return Status::InvalidOperation;
}
// Check if the queue is empty.
if (core->queue.empty()) {
return Status::NoBufferAvailable;
}
auto front(core->queue.begin());
// If expected_present is specified, we may not want to return a buffer yet.
if (expected_present.count() != 0) {
constexpr auto MAX_REASONABLE_NSEC = 1000000000LL; // 1 second
// The expected_present argument indicates when the buffer is expected to be presented
// on-screen.
while (core->queue.size() > 1 && !core->queue[0].is_auto_timestamp) {
const auto& buffer_item{core->queue[1]};
// If entry[1] is timely, drop entry[0] (and repeat).
const auto desired_present = buffer_item.timestamp;
if (desired_present < expected_present.count() - MAX_REASONABLE_NSEC ||
desired_present > expected_present.count()) {
// This buffer is set to display in the near future, or desired_present is garbage.
LOG_DEBUG(Service_Nvnflinger, "nodrop desire={} expect={}", desired_present,
expected_present.count());
break;
}
LOG_DEBUG(Service_Nvnflinger, "drop desire={} expect={} size={}", desired_present,
expected_present.count(), core->queue.size());
if (core->StillTracking(*front)) {
// Front buffer is still in mSlots, so mark the slot as free
slots[front->slot].buffer_state = BufferState::Free;
}
core->queue.erase(front);
front = core->queue.begin();
}
// See if the front buffer is ready to be acquired.
const auto desired_present = front->timestamp;
if (desired_present > expected_present.count() &&
desired_present < expected_present.count() + MAX_REASONABLE_NSEC) {
LOG_DEBUG(Service_Nvnflinger, "defer desire={} expect={}", desired_present,
expected_present.count());
return Status::PresentLater;
}
LOG_DEBUG(Service_Nvnflinger, "accept desire={} expect={}", desired_present,
expected_present.count());
}
const auto slot = front->slot;
*out_buffer = *front;
LOG_DEBUG(Service_Nvnflinger, "acquiring slot={}", slot);
// If the front buffer is still being tracked, update its slot state
if (core->StillTracking(*front)) {
slots[slot].acquire_called = true;
slots[slot].needs_cleanup_on_release = false;
slots[slot].buffer_state = BufferState::Acquired;
// TODO: for now, avoid resetting the fence, so that when we next return this
// slot to the producer, it will wait for the fence to pass. We should fix this
// by properly waiting for the fence in the BufferItemConsumer.
// slots[slot].fence = Fence::NoFence();
}
// If the buffer has previously been acquired by the consumer, set graphic_buffer to nullptr to
// avoid unnecessarily remapping this buffer on the consumer side.
if (out_buffer->acquire_called) {
out_buffer->graphic_buffer = nullptr;
}
core->queue.erase(front);
// We might have freed a slot while dropping old buffers, or the producer may be blocked
// waiting for the number of buffers in the queue to decrease.
core->SignalDequeueCondition();
return Status::NoError;
}
Status BufferQueueConsumer::ReleaseBuffer(s32 slot, u64 frame_number, const Fence& release_fence) {
if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
LOG_ERROR(Service_Nvnflinger, "slot {} out of range", slot);
return Status::BadValue;
}
std::shared_ptr<IProducerListener> listener;
{
std::scoped_lock lock{core->mutex};
// If the frame number has changed because the buffer has been reallocated, we can ignore
// this ReleaseBuffer for the old buffer.
if (frame_number != slots[slot].frame_number) {
return Status::StaleBufferSlot;
}
// Make sure this buffer hasn't been queued while acquired by the consumer.
auto current(core->queue.begin());
while (current != core->queue.end()) {
if (current->slot == slot) {
LOG_ERROR(Service_Nvnflinger, "buffer slot {} pending release is currently queued",
slot);
return Status::BadValue;
}
++current;
}
if (slots[slot].buffer_state == BufferState::Acquired) {
// TODO: for now, avoid resetting the fence, so that when we next return this
// slot to the producer, it can wait for its own fence to pass. We should fix this
// by properly waiting for the fence in the BufferItemConsumer.
// slots[slot].fence = release_fence;
slots[slot].buffer_state = BufferState::Free;
listener = core->connected_producer_listener;
LOG_DEBUG(Service_Nvnflinger, "releasing slot {}", slot);
} else if (slots[slot].needs_cleanup_on_release) {
LOG_DEBUG(Service_Nvnflinger, "releasing a stale buffer slot {} (state = {})", slot,
slots[slot].buffer_state);
slots[slot].needs_cleanup_on_release = false;
return Status::StaleBufferSlot;
} else {
LOG_ERROR(Service_Nvnflinger,
"attempted to release buffer slot {} but its state was {}", slot,
slots[slot].buffer_state);
return Status::BadValue;
}
core->SignalDequeueCondition();
}
// Call back without lock held
if (listener != nullptr) {
listener->OnBufferReleased();
}
return Status::NoError;
}
Status BufferQueueConsumer::Connect(std::shared_ptr<IConsumerListener> consumer_listener,
bool controlled_by_app) {
if (consumer_listener == nullptr) {
LOG_ERROR(Service_Nvnflinger, "consumer_listener may not be nullptr");
return Status::BadValue;
}
LOG_DEBUG(Service_Nvnflinger, "controlled_by_app={}", controlled_by_app);
std::scoped_lock lock{core->mutex};
if (core->is_abandoned) {
LOG_ERROR(Service_Nvnflinger, "BufferQueue has been abandoned");
return Status::NoInit;
}
core->consumer_listener = std::move(consumer_listener);
core->consumer_controlled_by_app = controlled_by_app;
return Status::NoError;
}
Status BufferQueueConsumer::Disconnect() {
LOG_DEBUG(Service_Nvnflinger, "called");
std::scoped_lock lock{core->mutex};
if (core->consumer_listener == nullptr) {
LOG_ERROR(Service_Nvnflinger, "no consumer is connected");
return Status::BadValue;
}
core->is_abandoned = true;
core->consumer_listener = nullptr;
core->queue.clear();
core->FreeAllBuffersLocked();
core->SignalDequeueCondition();
return Status::NoError;
}
Status BufferQueueConsumer::GetReleasedBuffers(u64* out_slot_mask) {
if (out_slot_mask == nullptr) {
LOG_ERROR(Service_Nvnflinger, "out_slot_mask may not be nullptr");
return Status::BadValue;
}
std::scoped_lock lock{core->mutex};
if (core->is_abandoned) {
LOG_ERROR(Service_Nvnflinger, "BufferQueue has been abandoned");
return Status::NoInit;
}
u64 mask = 0;
for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
if (!slots[s].acquire_called) {
mask |= (1ULL << s);
}
}
// Remove from the mask queued buffers for which acquire has been called, since the consumer
// will not receive their buffer addresses and so must retain their cached information
auto current(core->queue.begin());
while (current != core->queue.end()) {
if (current->acquire_called) {
mask &= ~(1ULL << current->slot);
}
++current;
}
LOG_DEBUG(Service_Nvnflinger, "returning mask {}", mask);
*out_slot_mask = mask;
return Status::NoError;
}
void BufferQueueConsumer::Transact(u32 code, std::span<const u8> parcel_data,
std::span<u8> parcel_reply, u32 flags) {
// Values used by BnGraphicBufferConsumer onTransact
enum class TransactionId {
AcquireBuffer = 1,
DetachBuffer = 2,
AttachBuffer = 3,
ReleaseBuffer = 4,
ConsumerConnect = 5,
ConsumerDisconnect = 6,
GetReleasedBuffers = 7,
SetDefaultBufferSize = 8,
SetDefaultMaxBufferCount = 9,
DisableAsyncBuffer = 10,
SetMaxAcquiredBufferCount = 11,
SetConsumerName = 12,
SetDefaultBufferFormat = 13,
SetConsumerUsageBits = 14,
SetTransformHint = 15,
GetSidebandStream = 16,
Unknown18 = 18,
Unknown20 = 20,
};
Status status{Status::NoError};
InputParcel parcel_in{parcel_data};
OutputParcel parcel_out{};
switch (static_cast<TransactionId>(code)) {
case TransactionId::AcquireBuffer: {
BufferItem item;
const s64 present_when = parcel_in.Read<s64>();
status = AcquireBuffer(&item, std::chrono::nanoseconds{present_when});
// TODO: can't write this directly, needs a flattener for the sp<GraphicBuffer>
// parcel_out.WriteFlattened(item);
UNREACHABLE();
}
case TransactionId::ReleaseBuffer: {
const s32 slot = parcel_in.Read<s32>();
const u64 frame_number = parcel_in.Read<u64>();
const auto release_fence = parcel_in.ReadFlattened<Fence>();
status = ReleaseBuffer(slot, frame_number, release_fence);
break;
}
case TransactionId::GetReleasedBuffers: {
u64 slot_mask = 0;
status = GetReleasedBuffers(&slot_mask);
parcel_out.Write(slot_mask);
break;
}
default:
ASSERT_MSG(false, "called, code={} flags={}", code, flags);
break;
}
parcel_out.Write(status);
const auto serialized = parcel_out.Serialize();
std::memcpy(parcel_reply.data(), serialized.data(),
std::min(parcel_reply.size(), serialized.size()));
}
Kernel::KReadableEvent* BufferQueueConsumer::GetNativeHandle(u32 type_id) {
ASSERT_MSG(false, "called, type_id={}", type_id);
return nullptr;
}
} // namespace Service::android
|