blob: 789eca3b308fd67a22242042e42c674b1fb35967 [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
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 Stratiienko19c162f2022-02-01 09:35:08 +020027#include "compositor/DrmDisplayCompositor.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) {
39 if (uevent_listener_.Init() != 0) {
40 ALOGE("Can't initialize event listener");
41 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +010042}
43
Roman Stratiienko24a7fc42021-12-23 16:25:20 +020044ResourceManager::~ResourceManager() {
45 uevent_listener_.Exit();
46}
47
Roman Stratiienko3dacd472022-01-11 19:18:34 +020048void ResourceManager::Init() {
49 if (initialized_) {
50 ALOGE("Already initialized");
51 return;
52 }
53
Alexandru Gheorghec5463582018-03-27 15:52:02 +010054 char path_pattern[PROPERTY_VALUE_MAX];
55 // Could be a valid path or it can have at the end of it the wildcard %
56 // which means that it will try open all devices until an error is met.
Jason Macnakf1af9572020-08-20 11:49:51 -070057 int path_len = property_get("vendor.hwc.drm.device", path_pattern,
58 "/dev/dri/card%");
Alexandru Gheorghec5463582018-03-27 15:52:02 +010059 if (path_pattern[path_len - 1] != '%') {
Roman Stratiienko3dacd472022-01-11 19:18:34 +020060 AddDrmDevice(std::string(path_pattern));
Alexandru Gheorghec5463582018-03-27 15:52:02 +010061 } else {
62 path_pattern[path_len - 1] = '\0';
Roman Stratiienko3dacd472022-01-11 19:18:34 +020063 for (int idx = 0;; ++idx) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +010064 std::ostringstream path;
65 path << path_pattern << idx;
Matvii Zorinec75ccd2020-07-17 12:08:45 +030066
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020067 struct stat buf {};
Roman Stratiienkofc014f52021-12-23 19:04:29 +020068 if (stat(path.str().c_str(), &buf) != 0)
Matvii Zorinec75ccd2020-07-17 12:08:45 +030069 break;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020070
Roman Stratiienko3dacd472022-01-11 19:18:34 +020071 if (DrmDevice::IsKMSDev(path.str().c_str())) {
72 AddDrmDevice(path.str());
73 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +010074 }
75 }
76
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020077 char scale_with_gpu[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -070078 property_get("vendor.hwc.drm.scale_with_gpu", scale_with_gpu, "0");
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020079 scale_with_gpu_ = bool(strncmp(scale_with_gpu, "0", 1));
80
Roman Stratiienkofc014f52021-12-23 19:04:29 +020081 if (BufferInfoGetter::GetInstance() == nullptr) {
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030082 ALOGE("Failed to initialize BufferInfoGetter");
Roman Stratiienko3dacd472022-01-11 19:18:34 +020083 return;
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030084 }
85
Roman Stratiienko3dacd472022-01-11 19:18:34 +020086 uevent_listener_.RegisterHotplugHandler([this] {
87 const std::lock_guard<std::mutex> lock(GetMainLock());
88 UpdateFrontendDisplays();
89 });
90
91 UpdateFrontendDisplays();
92
93 initialized_ = true;
94}
95
96void ResourceManager::DeInit() {
97 if (!initialized_) {
98 ALOGE("Not initialized");
99 return;
Roman Stratiienko1e053b42021-10-25 22:54:20 +0300100 }
101
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200102 uevent_listener_.RegisterHotplugHandler([] {});
103
104 DetachAllFrontendDisplays();
105 drms_.clear();
106
107 initialized_ = false;
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100108}
109
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200110int ResourceManager::AddDrmDevice(const std::string &path) {
111 auto drm = std::make_unique<DrmDevice>();
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200112 int ret = drm->Init(path.c_str());
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100113 drms_.push_back(std::move(drm));
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100114 return ret;
115}
116
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200117void ResourceManager::UpdateFrontendDisplays() {
118 auto ordered_connectors = GetOrderedConnectors();
119
120 for (auto *conn : ordered_connectors) {
121 conn->UpdateModes();
122 bool connected = conn->IsConnected();
123 bool attached = attached_pipelines_.count(conn) != 0;
124
125 if (connected != attached) {
126 ALOGI("%s connector %s", connected ? "Attaching" : "Detaching",
127 conn->GetName().c_str());
128
129 if (connected) {
130 auto pipeline = DrmDisplayPipeline::CreatePipeline(*conn);
131 frontend_interface_->BindDisplay(pipeline.get());
132 attached_pipelines_[conn] = std::move(pipeline);
133 } else {
134 auto &pipeline = attached_pipelines_[conn];
135 frontend_interface_->UnbindDisplay(pipeline.get());
136 attached_pipelines_.erase(conn);
137 }
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200138 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100139 }
Roman Stratiienko3dacd472022-01-11 19:18:34 +0200140 frontend_interface_->FinalizeDisplayBinding();
141}
142
143void ResourceManager::DetachAllFrontendDisplays() {
144 for (auto &p : attached_pipelines_) {
145 frontend_interface_->UnbindDisplay(p.second.get());
146 }
147 attached_pipelines_.clear();
148 frontend_interface_->FinalizeDisplayBinding();
149}
150
151auto ResourceManager::GetOrderedConnectors() -> std::vector<DrmConnector *> {
152 /* Put internal displays first then external to
153 * ensure Internal will take Primary slot
154 */
155
156 std::vector<DrmConnector *> ordered_connectors;
157
158 for (auto &drm : drms_) {
159 for (const auto &conn : drm->GetConnectors()) {
160 if (conn->IsInternal()) {
161 ordered_connectors.emplace_back(conn.get());
162 }
163 }
164 }
165
166 for (auto &drm : drms_) {
167 for (const auto &conn : drm->GetConnectors()) {
168 if (conn->IsExternal()) {
169 ordered_connectors.emplace_back(conn.get());
170 }
171 }
172 }
173
174 return ordered_connectors;
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100175}
Sean Paulf72cccd2018-08-27 13:59:08 -0400176} // namespace android