blob: 50c33aa2b37be6afd819217441d29ce441374205 [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
21#include <cutils/properties.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 Stratiienkob2e9fe22020-10-03 10:52:36 +030028
Alexandru Gheorghec5463582018-03-27 15:52:02 +010029namespace android {
30
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020031ResourceManager::ResourceManager() : num_displays_(0), gralloc_(nullptr) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +010032}
33
34int ResourceManager::Init() {
35 char path_pattern[PROPERTY_VALUE_MAX];
36 // Could be a valid path or it can have at the end of it the wildcard %
37 // which means that it will try open all devices until an error is met.
Jason Macnakf1af9572020-08-20 11:49:51 -070038 int path_len = property_get("vendor.hwc.drm.device", path_pattern,
39 "/dev/dri/card%");
Alexandru Gheorghec5463582018-03-27 15:52:02 +010040 int ret = 0;
41 if (path_pattern[path_len - 1] != '%') {
42 ret = AddDrmDevice(std::string(path_pattern));
43 } else {
44 path_pattern[path_len - 1] = '\0';
45 for (int idx = 0; !ret; ++idx) {
46 std::ostringstream path;
47 path << path_pattern << idx;
Matvii Zorinec75ccd2020-07-17 12:08:45 +030048
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020049 struct stat buf {};
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020050 if (stat(path.str().c_str(), &buf))
Matvii Zorinec75ccd2020-07-17 12:08:45 +030051 break;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020052
53 if (IsKMSDev(path.str().c_str()))
Matvii Zorinec75ccd2020-07-17 12:08:45 +030054 ret = AddDrmDevice(path.str());
Alexandru Gheorghec5463582018-03-27 15:52:02 +010055 }
56 }
57
58 if (!num_displays_) {
59 ALOGE("Failed to initialize any displays");
60 return ret ? -EINVAL : ret;
61 }
62
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020063 char scale_with_gpu[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -070064 property_get("vendor.hwc.drm.scale_with_gpu", scale_with_gpu, "0");
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020065 scale_with_gpu_ = bool(strncmp(scale_with_gpu, "0", 1));
66
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030067 if (!BufferInfoGetter::GetInstance()) {
68 ALOGE("Failed to initialize BufferInfoGetter");
69 return -EINVAL;
70 }
71
Alexandru Gheorghec5463582018-03-27 15:52:02 +010072 return hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
73 (const hw_module_t **)&gralloc_);
74}
75
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020076int ResourceManager::AddDrmDevice(std::string const &path) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +010077 std::unique_ptr<DrmDevice> drm = std::make_unique<DrmDevice>();
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020078 int displays_added = 0;
79 int ret = 0;
Alexandru Gheorghec5463582018-03-27 15:52:02 +010080 std::tie(ret, displays_added) = drm->Init(path.c_str(), num_displays_);
81 if (ret)
82 return ret;
83 std::shared_ptr<Importer> importer;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020084 importer = std::make_shared<DrmGenericImporter>(drm.get());
Alexandru Gheorghec5463582018-03-27 15:52:02 +010085 if (!importer) {
86 ALOGE("Failed to create importer instance");
87 return -ENODEV;
88 }
89 importers_.push_back(importer);
90 drms_.push_back(std::move(drm));
91 num_displays_ += displays_added;
92 return ret;
93}
94
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010095DrmConnector *ResourceManager::AvailableWritebackConnector(int display) {
96 DrmDevice *drm_device = GetDrmDevice(display);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020097 DrmConnector *writeback_conn = nullptr;
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010098 if (drm_device) {
99 writeback_conn = drm_device->AvailableWritebackConnector(display);
100 if (writeback_conn)
101 return writeback_conn;
102 }
103 for (auto &drm : drms_) {
104 if (drm.get() == drm_device)
105 continue;
106 writeback_conn = drm->AvailableWritebackConnector(display);
107 if (writeback_conn)
108 return writeback_conn;
109 }
110 return writeback_conn;
111}
112
Matvii Zorinec75ccd2020-07-17 12:08:45 +0300113bool ResourceManager::IsKMSDev(const char *path) {
114 int fd = open(path, O_RDWR | O_CLOEXEC);
115 if (fd < 0)
116 return false;
117
118 auto res = drmModeGetResources(fd);
119 if (!res) {
120 close(fd);
121 return false;
122 }
123
124 bool is_kms = res->count_crtcs > 0 && res->count_connectors > 0 &&
125 res->count_encoders > 0;
126
127 drmModeFreeResources(res);
128 close(fd);
129
130 return is_kms;
131}
132
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100133DrmDevice *ResourceManager::GetDrmDevice(int display) {
134 for (auto &drm : drms_) {
135 if (drm->HandlesDisplay(display))
136 return drm.get();
137 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200138 return nullptr;
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100139}
140
141std::shared_ptr<Importer> ResourceManager::GetImporter(int display) {
142 for (unsigned int i = 0; i < drms_.size(); i++) {
143 if (drms_[i]->HandlesDisplay(display))
144 return importers_[i];
145 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200146 return nullptr;
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100147}
148
149const gralloc_module_t *ResourceManager::gralloc() {
150 return gralloc_;
151}
Sean Paulf72cccd2018-08-27 13:59:08 -0400152} // namespace android