blob: f8e9cabc716ed5e8a46d87576b2f4a8513a8a152 [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 Stratiienkod518a052021-02-25 19:15:14 +020027#include "utils/log.h"
Roman Stratiienkod21071f2021-03-09 21:56:50 +020028#include "utils/properties.h"
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030029
Alexandru Gheorghec5463582018-03-27 15:52:02 +010030namespace android {
31
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020032ResourceManager::ResourceManager() : num_displays_(0), gralloc_(nullptr) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +010033}
34
35int ResourceManager::Init() {
36 char path_pattern[PROPERTY_VALUE_MAX];
37 // Could be a valid path or it can have at the end of it the wildcard %
38 // which means that it will try open all devices until an error is met.
Jason Macnakf1af9572020-08-20 11:49:51 -070039 int path_len = property_get("vendor.hwc.drm.device", path_pattern,
40 "/dev/dri/card%");
Alexandru Gheorghec5463582018-03-27 15:52:02 +010041 int ret = 0;
42 if (path_pattern[path_len - 1] != '%') {
43 ret = AddDrmDevice(std::string(path_pattern));
44 } else {
45 path_pattern[path_len - 1] = '\0';
46 for (int idx = 0; !ret; ++idx) {
47 std::ostringstream path;
48 path << path_pattern << idx;
Matvii Zorinec75ccd2020-07-17 12:08:45 +030049
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020050 struct stat buf {};
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020051 if (stat(path.str().c_str(), &buf))
Matvii Zorinec75ccd2020-07-17 12:08:45 +030052 break;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020053
54 if (IsKMSDev(path.str().c_str()))
Matvii Zorinec75ccd2020-07-17 12:08:45 +030055 ret = AddDrmDevice(path.str());
Alexandru Gheorghec5463582018-03-27 15:52:02 +010056 }
57 }
58
59 if (!num_displays_) {
60 ALOGE("Failed to initialize any displays");
61 return ret ? -EINVAL : ret;
62 }
63
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020064 char scale_with_gpu[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -070065 property_get("vendor.hwc.drm.scale_with_gpu", scale_with_gpu, "0");
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020066 scale_with_gpu_ = bool(strncmp(scale_with_gpu, "0", 1));
67
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030068 if (!BufferInfoGetter::GetInstance()) {
69 ALOGE("Failed to initialize BufferInfoGetter");
70 return -EINVAL;
71 }
72
Alexandru Gheorghec5463582018-03-27 15:52:02 +010073 return hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
74 (const hw_module_t **)&gralloc_);
75}
76
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020077int ResourceManager::AddDrmDevice(std::string const &path) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +010078 std::unique_ptr<DrmDevice> drm = std::make_unique<DrmDevice>();
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020079 int displays_added = 0;
80 int ret = 0;
Alexandru Gheorghec5463582018-03-27 15:52:02 +010081 std::tie(ret, displays_added) = drm->Init(path.c_str(), num_displays_);
82 if (ret)
83 return ret;
84 std::shared_ptr<Importer> importer;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020085 importer = std::make_shared<DrmGenericImporter>(drm.get());
Alexandru Gheorghec5463582018-03-27 15:52:02 +010086 if (!importer) {
87 ALOGE("Failed to create importer instance");
88 return -ENODEV;
89 }
90 importers_.push_back(importer);
91 drms_.push_back(std::move(drm));
92 num_displays_ += displays_added;
93 return ret;
94}
95
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010096DrmConnector *ResourceManager::AvailableWritebackConnector(int display) {
97 DrmDevice *drm_device = GetDrmDevice(display);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020098 DrmConnector *writeback_conn = nullptr;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010099 if (drm_device) {
100 writeback_conn = drm_device->AvailableWritebackConnector(display);
101 if (writeback_conn)
102 return writeback_conn;
103 }
104 for (auto &drm : drms_) {
105 if (drm.get() == drm_device)
106 continue;
107 writeback_conn = drm->AvailableWritebackConnector(display);
108 if (writeback_conn)
109 return writeback_conn;
110 }
111 return writeback_conn;
112}
113
Matvii Zorinec75ccd2020-07-17 12:08:45 +0300114bool ResourceManager::IsKMSDev(const char *path) {
115 int fd = open(path, O_RDWR | O_CLOEXEC);
116 if (fd < 0)
117 return false;
118
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200119 auto *res = drmModeGetResources(fd);
Matvii Zorinec75ccd2020-07-17 12:08:45 +0300120 if (!res) {
121 close(fd);
122 return false;
123 }
124
125 bool is_kms = res->count_crtcs > 0 && res->count_connectors > 0 &&
126 res->count_encoders > 0;
127
128 drmModeFreeResources(res);
129 close(fd);
130
131 return is_kms;
132}
133
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100134DrmDevice *ResourceManager::GetDrmDevice(int display) {
135 for (auto &drm : drms_) {
136 if (drm->HandlesDisplay(display))
137 return drm.get();
138 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200139 return nullptr;
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100140}
141
142std::shared_ptr<Importer> ResourceManager::GetImporter(int display) {
143 for (unsigned int i = 0; i < drms_.size(); i++) {
144 if (drms_[i]->HandlesDisplay(display))
145 return importers_[i];
146 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200147 return nullptr;
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100148}
149
150const gralloc_module_t *ResourceManager::gralloc() {
151 return gralloc_;
152}
Sean Paulf72cccd2018-08-27 13:59:08 -0400153} // namespace android