blob: c866263a2c85616de6b3ad8ea73cadca836759c0 [file] [log] [blame]
Alexandru Gheorghec5463582018-03-27 15:52:02 +01001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Sean Paul468a7542024-07-16 19:50:58 +000017#define LOG_TAG "drmhwc"
Alexandru Gheorghec5463582018-03-27 15:52:02 +010018
Roman Stratiienko13cc3662020-08-29 21:35:39 +030019#include "ResourceManager.h"
Alexandru Gheorghec5463582018-03-27 15:52:02 +010020
Matvii Zorinec75ccd2020-07-17 12:08:45 +030021#include <sys/stat.h>
Roman Stratiienko13cc3662020-08-29 21:35:39 +030022
Roman Stratiienkod0c035b2022-01-21 15:12:56 +020023#include <ctime>
Alexandru Gheorghec5463582018-03-27 15:52:02 +010024#include <sstream>
Alexandru Gheorghec5463582018-03-27 15:52:02 +010025
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030026#include "bufferinfo/BufferInfoGetter.h"
Roman Stratiienko4e994052022-02-09 17:40:35 +020027#include "drm/DrmAtomicStateManager.h"
Roman Stratiienko24a7fc42021-12-23 16:25:20 +020028#include "drm/DrmDevice.h"
Roman Stratiienko19c162f2022-02-01 09:35:08 +020029#include "drm/DrmDisplayPipeline.h"
Roman Stratiienko24a7fc42021-12-23 16:25:20 +020030#include "drm/DrmPlane.h"
Roman Stratiienkod518a052021-02-25 19:15:14 +020031#include "utils/log.h"
Roman Stratiienkod21071f2021-03-09 21:56:50 +020032#include "utils/properties.h"
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030033
Alexandru Gheorghec5463582018-03-27 15:52:02 +010034namespace android {
35
Roman Stratiienko3dacd472022-01-11 19:18:34 +020036ResourceManager::ResourceManager(
37 PipelineToFrontendBindingInterface *p2f_bind_interface)
38 : frontend_interface_(p2f_bind_interface) {
Roman Stratiienko4719abb2022-12-28 18:51:37 +020039 uevent_listener_ = UEventListener::CreateInstance();
Roman Stratiienko24a7fc42021-12-23 16:25:20 +020040}
41
Drew Davenportb8526052024-08-28 10:24:33 -060042ResourceManager::~ResourceManager() {
43 uevent_listener_->StopThread();
44}
45
Roman Stratiienko3dacd472022-01-11 19:18:34 +020046void ResourceManager::Init() {
47 if (initialized_) {
48 ALOGE("Already initialized");
49 return;
50 }
51
Alexandru Gheorghec5463582018-03-27 15:52:02 +010052 char path_pattern[PROPERTY_VALUE_MAX];
53 // Could be a valid path or it can have at the end of it the wildcard %
54 // which means that it will try open all devices until an error is met.
Roman Stratiienkoa7913de2022-10-20 13:18:57 +030055 auto path_len = property_get("vendor.hwc.drm.device", path_pattern,
56 "/dev/dri/card%");
Alexandru Gheorghec5463582018-03-27 15:52:02 +010057 if (path_pattern[path_len - 1] != '%') {
Roman Stratiienko5f21dbc2022-05-03 18:31:13 +030058 auto dev = DrmDevice::CreateInstance(path_pattern, this);
59 if (dev) {
60 drms_.emplace_back(std::move(dev));
61 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +010062 } else {
63 path_pattern[path_len - 1] = '\0';
Roman Stratiienko3dacd472022-01-11 19:18:34 +020064 for (int idx = 0;; ++idx) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +010065 std::ostringstream path;
66 path << path_pattern << idx;
Matvii Zorinec75ccd2020-07-17 12:08:45 +030067
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020068 struct stat buf {};
Roman Stratiienkofc014f52021-12-23 19:04:29 +020069 if (stat(path.str().c_str(), &buf) != 0)
Matvii Zorinec75ccd2020-07-17 12:08:45 +030070 break;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020071
Roman Stratiienko5f21dbc2022-05-03 18:31:13 +030072 auto dev = DrmDevice::CreateInstance(path.str(), this);
73 if (dev) {
74 drms_.emplace_back(std::move(dev));
Roman Stratiienko3dacd472022-01-11 19:18:34 +020075 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +010076 }
77 }
78
Roman Stratiienkobf131802024-12-11 01:23:56 +020079 scale_with_gpu_ = Properties::ScaleWithGpu();
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020080
Roman Stratiienkobf131802024-12-11 01:23:56 +020081 char proptext[PROPERTY_VALUE_MAX];
Roman Stratiienko0da91bf2023-01-17 18:06:04 +020082 constexpr char kDrmOrGpu[] = "DRM_OR_GPU";
83 constexpr char kDrmOrIgnore[] = "DRM_OR_IGNORE";
84 property_get("vendor.hwc.drm.ctm", proptext, kDrmOrGpu);
85 if (strncmp(proptext, kDrmOrGpu, sizeof(kDrmOrGpu)) == 0) {
86 ctm_handling_ = CtmHandling::kDrmOrGpu;
87 } else if (strncmp(proptext, kDrmOrIgnore, sizeof(kDrmOrIgnore)) == 0) {
88 ctm_handling_ = CtmHandling::kDrmOrIgnore;
89 } else {
90 ALOGE("Invalid value for vendor.hwc.drm.ctm: %s", proptext);
91 ctm_handling_ = CtmHandling::kDrmOrGpu;
92 }
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020093
Roman Stratiienkofc014f52021-12-23 19:04:29 +020094 if (BufferInfoGetter::GetInstance() == nullptr) {
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030095 ALOGE("Failed to initialize BufferInfoGetter");
Roman Stratiienko3dacd472022-01-11 19:18:34 +020096 return;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030097 }
98
Roman Stratiienko4719abb2022-12-28 18:51:37 +020099 uevent_listener_->RegisterHotplugHandler([this] {
Roman Stratiienko9e2a2cd2022-12-28 20:47:29 +0200100 const std::unique_lock lock(GetMainLock());
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200101 UpdateFrontendDisplays();
102 });
103
104 UpdateFrontendDisplays();
105
106 initialized_ = true;
107}
108
109void ResourceManager::DeInit() {
110 if (!initialized_) {
111 ALOGE("Not initialized");
112 return;
Roman Stratiienko1e053b42021-10-25 22:54:20 +0300113 }
114
Roman Stratiienko4719abb2022-12-28 18:51:37 +0200115 uevent_listener_->RegisterHotplugHandler({});
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200116
117 DetachAllFrontendDisplays();
118 drms_.clear();
119
120 initialized_ = false;
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100121}
122
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200123auto ResourceManager::GetTimeMonotonicNs() -> int64_t {
124 struct timespec ts {};
125 clock_gettime(CLOCK_MONOTONIC, &ts);
126 constexpr int64_t kNsInSec = 1000000000LL;
Roman Stratiienko88bd6a22025-01-24 23:55:44 +0200127 return (int64_t(ts.tv_sec) * kNsInSec) + int64_t(ts.tv_nsec);
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200128}
129
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200130void ResourceManager::UpdateFrontendDisplays() {
131 auto ordered_connectors = GetOrderedConnectors();
132
133 for (auto *conn : ordered_connectors) {
134 conn->UpdateModes();
Roman Stratiienkoa7913de2022-10-20 13:18:57 +0300135 auto connected = conn->IsConnected();
136 auto attached = attached_pipelines_.count(conn) != 0;
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200137
138 if (connected != attached) {
139 ALOGI("%s connector %s", connected ? "Attaching" : "Detaching",
140 conn->GetName().c_str());
141
142 if (connected) {
Roman Stratiienko63762a92023-09-18 22:33:45 +0300143 std::shared_ptr<DrmDisplayPipeline>
144 pipeline = DrmDisplayPipeline::CreatePipeline(*conn);
145
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200146 if (pipeline) {
Roman Stratiienko63762a92023-09-18 22:33:45 +0300147 frontend_interface_->BindDisplay(pipeline);
Roman Stratiienkod0494d92022-03-15 18:02:04 +0200148 attached_pipelines_[conn] = std::move(pipeline);
149 }
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200150 } else {
151 auto &pipeline = attached_pipelines_[conn];
Roman Stratiienko63762a92023-09-18 22:33:45 +0300152 frontend_interface_->UnbindDisplay(pipeline);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200153 attached_pipelines_.erase(conn);
154 }
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200155 }
Manasi Navare3f0c01a2024-10-04 18:01:55 +0000156 if (connected) {
157 if (!conn->IsLinkStatusGood())
158 frontend_interface_->NotifyDisplayLinkStatus(attached_pipelines_[conn]);
159 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100160 }
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200161 frontend_interface_->FinalizeDisplayBinding();
162}
163
164void ResourceManager::DetachAllFrontendDisplays() {
165 for (auto &p : attached_pipelines_) {
Roman Stratiienko63762a92023-09-18 22:33:45 +0300166 frontend_interface_->UnbindDisplay(p.second);
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200167 }
168 attached_pipelines_.clear();
169 frontend_interface_->FinalizeDisplayBinding();
170}
171
172auto ResourceManager::GetOrderedConnectors() -> std::vector<DrmConnector *> {
173 /* Put internal displays first then external to
174 * ensure Internal will take Primary slot
175 */
176
177 std::vector<DrmConnector *> ordered_connectors;
178
179 for (auto &drm : drms_) {
180 for (const auto &conn : drm->GetConnectors()) {
181 if (conn->IsInternal()) {
182 ordered_connectors.emplace_back(conn.get());
183 }
184 }
185 }
186
187 for (auto &drm : drms_) {
188 for (const auto &conn : drm->GetConnectors()) {
189 if (conn->IsExternal()) {
190 ordered_connectors.emplace_back(conn.get());
191 }
192 }
193 }
194
195 return ordered_connectors;
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100196}
Roman Stratiienkof2c060f2023-09-18 22:46:08 +0300197
198auto ResourceManager::GetVirtualDisplayPipeline()
199 -> std::shared_ptr<DrmDisplayPipeline> {
200 for (auto &drm : drms_) {
201 for (const auto &conn : drm->GetWritebackConnectors()) {
202 auto pipeline = DrmDisplayPipeline::CreatePipeline(*conn);
203 if (!pipeline) {
204 ALOGE("Failed to create pipeline for writeback connector %s",
205 conn->GetName().c_str());
206 }
207 if (pipeline) {
208 return pipeline;
209 }
210 }
211 }
212 return {};
213}
214
215auto ResourceManager::GetWritebackConnectorsCount() -> uint32_t {
216 uint32_t count = 0;
217 for (auto &drm : drms_) {
218 count += drm->GetWritebackConnectors().size();
219 }
220 return count;
221}
222
Sean Paulf72cccd2018-08-27 13:59:08 -0400223} // namespace android