summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar bunnei2021-11-11 18:56:11 -0800
committerGravatar bunnei2022-03-24 18:13:33 -0700
commitbfff7b58fd06d738ef3d8d40de46290b1cc158a5 (patch)
tree1798b62ed721871be5fe886c299326cd93968434 /src
parenthle: nvflinger: Add implementation for BufferQueueConsumer class. (diff)
downloadyuzu-bfff7b58fd06d738ef3d8d40de46290b1cc158a5.tar.gz
yuzu-bfff7b58fd06d738ef3d8d40de46290b1cc158a5.tar.xz
yuzu-bfff7b58fd06d738ef3d8d40de46290b1cc158a5.zip
hle: nvflinger: Add implementation for BufferQueueCore class.
Diffstat (limited to 'src')
-rw-r--r--src/core/CMakeLists.txt2
-rw-r--r--src/core/hle/service/nvflinger/buffer_queue_core.cpp135
-rw-r--r--src/core/hle/service/nvflinger/buffer_queue_core.h98
3 files changed, 235 insertions, 0 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index c7ce6c1a6..5ba9b6c09 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -543,6 +543,8 @@ add_library(core STATIC
543 hle/service/nvflinger/buffer_item_consumer.h 543 hle/service/nvflinger/buffer_item_consumer.h
544 hle/service/nvflinger/buffer_queue_consumer.cpp 544 hle/service/nvflinger/buffer_queue_consumer.cpp
545 hle/service/nvflinger/buffer_queue_consumer.h 545 hle/service/nvflinger/buffer_queue_consumer.h
546 hle/service/nvflinger/buffer_queue_core.cpp
547 hle/service/nvflinger/buffer_queue_core.h
546 hle/service/nvflinger/buffer_queue_defs.h 548 hle/service/nvflinger/buffer_queue_defs.h
547 hle/service/nvflinger/buffer_slot.h 549 hle/service/nvflinger/buffer_slot.h
548 hle/service/nvflinger/buffer_transform_flags.h 550 hle/service/nvflinger/buffer_transform_flags.h
diff --git a/src/core/hle/service/nvflinger/buffer_queue_core.cpp b/src/core/hle/service/nvflinger/buffer_queue_core.cpp
new file mode 100644
index 000000000..a6ff46aa9
--- /dev/null
+++ b/src/core/hle/service/nvflinger/buffer_queue_core.cpp
@@ -0,0 +1,135 @@
1// SPDX-License-Identifier: GPL-3.0-or-later
2// Copyright 2021 yuzu Emulator Project
3// Copyright 2014 The Android Open Source Project
4// Parts of this implementation were base on:
5// https://cs.android.com/android/platform/superproject/+/android-5.1.1_r38:frameworks/native/libs/gui/BufferQueueCore.cpp
6
7#include "common/assert.h"
8
9#include "core/hle/service/nvflinger/buffer_queue_core.h"
10
11namespace android {
12
13BufferQueueCore::BufferQueueCore() : lock{mutex} {
14 // This is locked on creation, so unlock.
15 lock.unlock();
16
17 for (s32 slot = 0; slot < BufferQueueDefs::NUM_BUFFER_SLOTS; ++slot) {
18 free_slots.insert(slot);
19 }
20}
21
22void BufferQueueCore::NotifyShutdown() {
23 std::unique_lock<std::mutex> lk(mutex);
24
25 is_shutting_down = true;
26
27 SignalDequeueCondition();
28}
29
30void BufferQueueCore::SignalDequeueCondition() {
31 dequeue_condition.notify_all();
32}
33
34bool BufferQueueCore::WaitForDequeueCondition() {
35 if (is_shutting_down) {
36 return false;
37 }
38
39 dequeue_condition.wait(lock);
40
41 return true;
42}
43
44s32 BufferQueueCore::GetMinUndequeuedBufferCountLocked(bool async) const {
45 // If DequeueBuffer is allowed to error out, we don't have to add an extra buffer.
46 if (!use_async_buffer) {
47 return max_acquired_buffer_count;
48 }
49
50 if (dequeue_buffer_cannot_block || async) {
51 return max_acquired_buffer_count + 1;
52 }
53
54 return max_acquired_buffer_count;
55}
56
57s32 BufferQueueCore::GetMinMaxBufferCountLocked(bool async) const {
58 return GetMinUndequeuedBufferCountLocked(async) + 1;
59}
60
61s32 BufferQueueCore::GetMaxBufferCountLocked(bool async) const {
62 const auto min_buffer_count = GetMinMaxBufferCountLocked(async);
63 auto max_buffer_count = std::max(default_max_buffer_count, min_buffer_count);
64
65 if (override_max_buffer_count != 0) {
66 ASSERT(override_max_buffer_count >= min_buffer_count);
67 max_buffer_count = override_max_buffer_count;
68 }
69
70 // Any buffers that are dequeued by the producer or sitting in the queue waiting to be consumed
71 // need to have their slots preserved.
72 for (s32 slot = max_buffer_count; slot < BufferQueueDefs::NUM_BUFFER_SLOTS; ++slot) {
73 const auto state = slots[slot].buffer_state;
74 if (state == BufferState::Queued || state == BufferState::Dequeued) {
75 max_buffer_count = slot + 1;
76 }
77 }
78
79 return max_buffer_count;
80}
81
82s32 BufferQueueCore::GetPreallocatedBufferCountLocked() const {
83 return static_cast<s32>(std::count_if(slots.begin(), slots.end(),
84 [](const auto& slot) { return slot.is_preallocated; }));
85}
86
87void BufferQueueCore::FreeBufferLocked(s32 slot) {
88 LOG_DEBUG(Service_NVFlinger, "slot {}", slot);
89
90 const auto had_buffer = slots[slot].graphic_buffer != nullptr;
91
92 slots[slot].graphic_buffer.reset();
93
94 if (slots[slot].buffer_state == BufferState::Acquired) {
95 slots[slot].needs_cleanup_on_release = true;
96 }
97
98 if (slots[slot].buffer_state != BufferState::Free) {
99 free_slots.insert(slot);
100 } else if (had_buffer) {
101 // If the slot was FREE, but we had a buffer, we need to move this slot from the free
102 // buffers list to the the free slots list.
103 free_buffers.remove(slot);
104 free_slots.insert(slot);
105 }
106
107 slots[slot].buffer_state = BufferState::Free;
108 slots[slot].acquire_called = false;
109 slots[slot].frame_number = 0;
110 slots[slot].fence = Fence::NoFence();
111}
112
113void BufferQueueCore::FreeAllBuffersLocked() {
114 queue.clear();
115 buffer_has_been_queued = false;
116
117 for (s32 slot = 0; slot < BufferQueueDefs::NUM_BUFFER_SLOTS; ++slot) {
118 FreeBufferLocked(slot);
119 }
120}
121
122bool BufferQueueCore::StillTracking(const BufferItem* item) const {
123 const BufferSlot& slot = slots[item->slot];
124
125 return (slot.graphic_buffer != nullptr) && (item->graphic_buffer == slot.graphic_buffer);
126}
127
128void BufferQueueCore::WaitWhileAllocatingLocked() const {
129 while (is_allocating) {
130 std::unique_lock<std::mutex> lk(mutex);
131 is_allocating_condition.wait(lk);
132 }
133}
134
135} // namespace android
diff --git a/src/core/hle/service/nvflinger/buffer_queue_core.h b/src/core/hle/service/nvflinger/buffer_queue_core.h
new file mode 100644
index 000000000..8019b7024
--- /dev/null
+++ b/src/core/hle/service/nvflinger/buffer_queue_core.h
@@ -0,0 +1,98 @@
1// SPDX-License-Identifier: GPL-3.0-or-later
2// Copyright 2021 yuzu Emulator Project
3// Copyright 2014 The Android Open Source Project
4// Parts of this implementation were base on:
5// https://cs.android.com/android/platform/superproject/+/android-5.1.1_r38:frameworks/native/include/gui/BufferQueueCore.h
6
7#pragma once
8
9#include <condition_variable>
10#include <list>
11#include <memory>
12#include <mutex>
13#include <set>
14#include <vector>
15
16#include "core/hle/service/nvflinger/buffer_item.h"
17#include "core/hle/service/nvflinger/buffer_queue_defs.h"
18#include "core/hle/service/nvflinger/pixel_format.h"
19#include "core/hle/service/nvflinger/status.h"
20#include "core/hle/service/nvflinger/window.h"
21
22namespace android {
23
24class IConsumerListener;
25class IProducerListener;
26
27class BufferQueueCore final {
28 friend class BufferQueueProducer;
29 friend class BufferQueueConsumer;
30
31public:
32 static constexpr s32 INVALID_BUFFER_SLOT = BufferItem::INVALID_BUFFER_SLOT;
33
34 BufferQueueCore();
35
36 void NotifyShutdown();
37
38private:
39 void SignalDequeueCondition();
40 bool WaitForDequeueCondition();
41
42 s32 GetMinUndequeuedBufferCountLocked(bool async) const;
43 s32 GetMinMaxBufferCountLocked(bool async) const;
44 s32 GetMaxBufferCountLocked(bool async) const;
45 s32 GetPreallocatedBufferCountLocked() const;
46 void FreeBufferLocked(s32 slot);
47 void FreeAllBuffersLocked();
48 bool StillTracking(const BufferItem* item) const;
49 void WaitWhileAllocatingLocked() const;
50
51private:
52 class AutoLock final {
53 public:
54 AutoLock(std::shared_ptr<BufferQueueCore>& core_) : core{core_} {
55 core->lock.lock();
56 }
57
58 ~AutoLock() {
59 core->lock.unlock();
60 }
61
62 private:
63 std::shared_ptr<BufferQueueCore>& core;
64 };
65
66private:
67 mutable std::mutex mutex;
68 mutable std::unique_lock<std::mutex> lock;
69 bool is_abandoned{};
70 bool consumer_controlled_by_app{};
71 std::shared_ptr<IConsumerListener> consumer_listener;
72 u32 consumer_usage_bit{};
73 NativeWindowApi connected_api{NativeWindowApi::NoConnectedApi};
74 std::shared_ptr<IProducerListener> connected_producer_listener;
75 BufferQueueDefs::SlotsType slots{};
76 std::vector<BufferItem> queue;
77 std::set<s32> free_slots;
78 std::list<s32> free_buffers;
79 s32 override_max_buffer_count{};
80 mutable std::condition_variable dequeue_condition;
81 const bool use_async_buffer{}; // This is always disabled on HOS
82 bool dequeue_buffer_cannot_block{};
83 PixelFormat default_buffer_format{PixelFormat::Rgba8888};
84 u32 default_width{1};
85 u32 default_height{1};
86 s32 default_max_buffer_count{2};
87 const s32 max_acquired_buffer_count{}; // This is always zero on HOS
88 bool buffer_has_been_queued{};
89 u64 frame_counter{};
90 u32 transform_hint{};
91 bool is_allocating{};
92 mutable std::condition_variable is_allocating_condition;
93 bool allow_allocation{true};
94 u64 buffer_age{};
95 bool is_shutting_down{};
96};
97
98} // namespace android