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
|
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "core/core.h"
#include "core/hle/service/nvdrv/devices/nvdisp_disp0.h"
#include "core/hle/service/nvdrv/nvdrv_interface.h"
#include "core/hle/service/nvnflinger/display.h"
#include "core/hle/service/nvnflinger/hos_binder_driver_server.h"
#include "core/hle/service/nvnflinger/surface_flinger.h"
#include "core/hle/service/sm/sm.h"
#include "core/hle/service/nvnflinger/buffer_queue_consumer.h"
#include "core/hle/service/nvnflinger/buffer_queue_core.h"
#include "core/hle/service/nvnflinger/buffer_queue_producer.h"
namespace Service::Nvnflinger {
SurfaceFlinger::SurfaceFlinger(Core::System& system, HosBinderDriverServer& server)
: m_system(system), m_server(server), m_context(m_system, "SurfaceFlinger") {
nvdrv = m_system.ServiceManager().GetService<Nvidia::NVDRV>("nvdrv:s", true)->GetModule();
disp_fd = nvdrv->Open("/dev/nvdisp_disp0", {});
}
SurfaceFlinger::~SurfaceFlinger() {
nvdrv->Close(disp_fd);
}
void SurfaceFlinger::AddDisplay(u64 display_id) {
m_displays.emplace_back(display_id);
}
void SurfaceFlinger::RemoveDisplay(u64 display_id) {
std::erase_if(m_displays, [&](auto& display) { return display.id == display_id; });
}
bool SurfaceFlinger::ComposeDisplay(s32* out_swap_interval, f32* out_compose_speed_scale,
u64 display_id) {
auto* const display = this->FindDisplay(display_id);
if (!display || !display->stack.HasLayers()) {
return false;
}
*out_swap_interval =
m_composer.ComposeLocked(out_compose_speed_scale, *display,
*nvdrv->GetDevice<Nvidia::Devices::nvdisp_disp0>(disp_fd));
return true;
}
void SurfaceFlinger::CreateLayer(s32 consumer_binder_id) {
auto binder = std::static_pointer_cast<android::BufferQueueConsumer>(
m_server.TryGetBinder(consumer_binder_id));
if (!binder) {
return;
}
auto buffer_item_consumer = std::make_shared<android::BufferItemConsumer>(std::move(binder));
buffer_item_consumer->Connect(false);
m_layers.layers.emplace_back(
std::make_shared<Layer>(std::move(buffer_item_consumer), consumer_binder_id));
}
void SurfaceFlinger::DestroyLayer(s32 consumer_binder_id) {
std::erase_if(m_layers.layers,
[&](auto& layer) { return layer->consumer_id == consumer_binder_id; });
}
void SurfaceFlinger::AddLayerToDisplayStack(u64 display_id, s32 consumer_binder_id) {
auto* const display = this->FindDisplay(display_id);
auto layer = this->FindLayer(consumer_binder_id);
if (!display || !layer) {
return;
}
display->stack.layers.emplace_back(std::move(layer));
}
void SurfaceFlinger::RemoveLayerFromDisplayStack(u64 display_id, s32 consumer_binder_id) {
auto* const display = this->FindDisplay(display_id);
if (!display) {
return;
}
m_composer.RemoveLayerLocked(*display, consumer_binder_id);
std::erase_if(display->stack.layers,
[&](auto& layer) { return layer->consumer_id == consumer_binder_id; });
}
void SurfaceFlinger::SetLayerVisibility(s32 consumer_binder_id, bool visible) {
if (const auto layer = this->FindLayer(consumer_binder_id); layer != nullptr) {
layer->visible = visible;
return;
}
}
void SurfaceFlinger::SetLayerBlending(s32 consumer_binder_id, LayerBlending blending) {
if (const auto layer = this->FindLayer(consumer_binder_id); layer != nullptr) {
layer->blending = blending;
return;
}
}
Display* SurfaceFlinger::FindDisplay(u64 display_id) {
for (auto& display : m_displays) {
if (display.id == display_id) {
return &display;
}
}
return nullptr;
}
std::shared_ptr<Layer> SurfaceFlinger::FindLayer(s32 consumer_binder_id) {
for (auto& layer : m_layers.layers) {
if (layer->consumer_id == consumer_binder_id) {
return layer;
}
}
return nullptr;
}
void SurfaceFlinger::CreateBufferQueue(s32* out_consumer_binder_id, s32* out_producer_binder_id) {
auto& nvmap = nvdrv->GetContainer().GetNvMapFile();
auto core = std::make_shared<android::BufferQueueCore>();
auto producer = std::make_shared<android::BufferQueueProducer>(m_context, core, nvmap);
auto consumer = std::make_shared<android::BufferQueueConsumer>(core);
*out_consumer_binder_id = m_server.RegisterBinder(std::move(consumer));
*out_producer_binder_id = m_server.RegisterBinder(std::move(producer));
}
void SurfaceFlinger::DestroyBufferQueue(s32 consumer_binder_id, s32 producer_binder_id) {
m_server.UnregisterBinder(producer_binder_id);
m_server.UnregisterBinder(consumer_binder_id);
}
} // namespace Service::Nvnflinger
|