blob: c8235e9a56db9ded7aefb3dd6c97b7ffae329139 [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
17#define LOG_TAG "hwc-resource-manager"
18
Roman Stratiienko13cc3662020-08-29 21:35:39 +030019#include "ResourceManager.h"
Alexandru Gheorghec5463582018-03-27 15:52:02 +010020
Roman Stratiienkod21071f2021-03-09 21:56:50 +020021#include <fcntl.h>
Matvii Zorinec75ccd2020-07-17 12:08:45 +030022#include <sys/stat.h>
Roman Stratiienko13cc3662020-08-29 21:35:39 +030023
Roman Stratiienkod0c035b2022-01-21 15:12:56 +020024#include <ctime>
Alexandru Gheorghec5463582018-03-27 15:52:02 +010025#include <sstream>
Alexandru Gheorghec5463582018-03-27 15:52:02 +010026
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030027#include "bufferinfo/BufferInfoGetter.h"
Roman Stratiienko4e994052022-02-09 17:40:35 +020028#include "drm/DrmAtomicStateManager.h"
Roman Stratiienko24a7fc42021-12-23 16:25:20 +020029#include "drm/DrmDevice.h"
Roman Stratiienko19c162f2022-02-01 09:35:08 +020030#include "drm/DrmDisplayPipeline.h"
Roman Stratiienko24a7fc42021-12-23 16:25:20 +020031#include "drm/DrmPlane.h"
Roman Stratiienkod518a052021-02-25 19:15:14 +020032#include "utils/log.h"
Roman Stratiienkod21071f2021-03-09 21:56:50 +020033#include "utils/properties.h"
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030034
Alexandru Gheorghec5463582018-03-27 15:52:02 +010035namespace android {
36
Roman Stratiienko3dacd472022-01-11 19:18:34 +020037ResourceManager::ResourceManager(
38 PipelineToFrontendBindingInterface *p2f_bind_interface)
39 : frontend_interface_(p2f_bind_interface) {
40 if (uevent_listener_.Init() != 0) {
41 ALOGE("Can't initialize event listener");
42 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +010043}
44
Roman Stratiienko24a7fc42021-12-23 16:25:20 +020045ResourceManager::~ResourceManager() {
46 uevent_listener_.Exit();
47}
48
Roman Stratiienko3dacd472022-01-11 19:18:34 +020049void ResourceManager::Init() {
50 if (initialized_) {
51 ALOGE("Already initialized");
52 return;
53 }
54
Alexandru Gheorghec5463582018-03-27 15:52:02 +010055 char path_pattern[PROPERTY_VALUE_MAX];
56 // Could be a valid path or it can have at the end of it the wildcard %
57 // which means that it will try open all devices until an error is met.
Jason Macnakf1af9572020-08-20 11:49:51 -070058 int path_len = property_get("vendor.hwc.drm.device", path_pattern,
59 "/dev/dri/card%");
Alexandru Gheorghec5463582018-03-27 15:52:02 +010060 if (path_pattern[path_len - 1] != '%') {
Roman Stratiienko3dacd472022-01-11 19:18:34 +020061 AddDrmDevice(std::string(path_pattern));
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 Stratiienko3dacd472022-01-11 19:18:34 +020072 if (DrmDevice::IsKMSDev(path.str().c_str())) {
73 AddDrmDevice(path.str());
74 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +010075 }
76 }
77
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020078 char scale_with_gpu[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -070079 property_get("vendor.hwc.drm.scale_with_gpu", scale_with_gpu, "0");
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020080 scale_with_gpu_ = bool(strncmp(scale_with_gpu, "0", 1));
81
Roman Stratiienkofc014f52021-12-23 19:04:29 +020082 if (BufferInfoGetter::GetInstance() == nullptr) {
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030083 ALOGE("Failed to initialize BufferInfoGetter");
Roman Stratiienko3dacd472022-01-11 19:18:34 +020084 return;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030085 }
86
Roman Stratiienko3dacd472022-01-11 19:18:34 +020087 uevent_listener_.RegisterHotplugHandler([this] {
88 const std::lock_guard<std::mutex> lock(GetMainLock());
89 UpdateFrontendDisplays();
90 });
91
92 UpdateFrontendDisplays();
93
94 initialized_ = true;
95}
96
97void ResourceManager::DeInit() {
98 if (!initialized_) {
99 ALOGE("Not initialized");
100 return;
Roman Stratiienko1e053b42021-10-25 22:54:20 +0300101 }
102
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200103 uevent_listener_.RegisterHotplugHandler([] {});
104
105 DetachAllFrontendDisplays();
106 drms_.clear();
107
108 initialized_ = false;
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100109}
110
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200111int ResourceManager::AddDrmDevice(const std::string &path) {
112 auto drm = std::make_unique<DrmDevice>();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200113 int ret = drm->Init(path.c_str());
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100114 drms_.push_back(std::move(drm));
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100115 return ret;
116}
117
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200118auto ResourceManager::GetTimeMonotonicNs() -> int64_t {
119 struct timespec ts {};
120 clock_gettime(CLOCK_MONOTONIC, &ts);
121 constexpr int64_t kNsInSec = 1000000000LL;
122 return int64_t(ts.tv_sec) * kNsInSec + int64_t(ts.tv_nsec);
123}
124
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200125void ResourceManager::UpdateFrontendDisplays() {
126 auto ordered_connectors = GetOrderedConnectors();
127
128 for (auto *conn : ordered_connectors) {
129 conn->UpdateModes();
130 bool connected = conn->IsConnected();
131 bool attached = attached_pipelines_.count(conn) != 0;
132
133 if (connected != attached) {
134 ALOGI("%s connector %s", connected ? "Attaching" : "Detaching",
135 conn->GetName().c_str());
136
137 if (connected) {
138 auto pipeline = DrmDisplayPipeline::CreatePipeline(*conn);
139 frontend_interface_->BindDisplay(pipeline.get());
140 attached_pipelines_[conn] = std::move(pipeline);
141 } else {
142 auto &pipeline = attached_pipelines_[conn];
143 frontend_interface_->UnbindDisplay(pipeline.get());
144 attached_pipelines_.erase(conn);
145 }
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200146 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100147 }
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200148 frontend_interface_->FinalizeDisplayBinding();
149}
150
151void ResourceManager::DetachAllFrontendDisplays() {
152 for (auto &p : attached_pipelines_) {
153 frontend_interface_->UnbindDisplay(p.second.get());
154 }
155 attached_pipelines_.clear();
156 frontend_interface_->FinalizeDisplayBinding();
157}
158
159auto ResourceManager::GetOrderedConnectors() -> std::vector<DrmConnector *> {
160 /* Put internal displays first then external to
161 * ensure Internal will take Primary slot
162 */
163
164 std::vector<DrmConnector *> ordered_connectors;
165
166 for (auto &drm : drms_) {
167 for (const auto &conn : drm->GetConnectors()) {
168 if (conn->IsInternal()) {
169 ordered_connectors.emplace_back(conn.get());
170 }
171 }
172 }
173
174 for (auto &drm : drms_) {
175 for (const auto &conn : drm->GetConnectors()) {
176 if (conn->IsExternal()) {
177 ordered_connectors.emplace_back(conn.get());
178 }
179 }
180 }
181
182 return ordered_connectors;
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100183}
Sean Paulf72cccd2018-08-27 13:59:08 -0400184} // namespace android