blob: 67ef7f87244bb5d2b86cfe0840346899ddb85336 [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>
22#include <log/log.h>
Matvii Zorinec75ccd2020-07-17 12:08:45 +030023#include <sys/stat.h>
Roman Stratiienko13cc3662020-08-29 21:35:39 +030024
Alexandru Gheorghec5463582018-03-27 15:52:02 +010025#include <sstream>
Alexandru Gheorghec5463582018-03-27 15:52:02 +010026
27namespace android {
28
29ResourceManager::ResourceManager() : num_displays_(0), gralloc_(NULL) {
30}
31
32int ResourceManager::Init() {
33 char path_pattern[PROPERTY_VALUE_MAX];
34 // Could be a valid path or it can have at the end of it the wildcard %
35 // which means that it will try open all devices until an error is met.
Jason Macnakf1af9572020-08-20 11:49:51 -070036 int path_len = property_get("vendor.hwc.drm.device", path_pattern,
37 "/dev/dri/card%");
Alexandru Gheorghec5463582018-03-27 15:52:02 +010038 int ret = 0;
39 if (path_pattern[path_len - 1] != '%') {
40 ret = AddDrmDevice(std::string(path_pattern));
41 } else {
42 path_pattern[path_len - 1] = '\0';
43 for (int idx = 0; !ret; ++idx) {
44 std::ostringstream path;
45 path << path_pattern << idx;
Matvii Zorinec75ccd2020-07-17 12:08:45 +030046
47 struct stat buf;
48 if (stat(path.str().c_str(), &buf)) {
49 break;
50 } else if (IsKMSDev(path.str().c_str())) {
51 ret = AddDrmDevice(path.str());
52 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +010053 }
54 }
55
56 if (!num_displays_) {
57 ALOGE("Failed to initialize any displays");
58 return ret ? -EINVAL : ret;
59 }
60
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020061 char scale_with_gpu[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -070062 property_get("vendor.hwc.drm.scale_with_gpu", scale_with_gpu, "0");
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020063 scale_with_gpu_ = bool(strncmp(scale_with_gpu, "0", 1));
64
Alexandru Gheorghec5463582018-03-27 15:52:02 +010065 return hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
66 (const hw_module_t **)&gralloc_);
67}
68
69int ResourceManager::AddDrmDevice(std::string path) {
70 std::unique_ptr<DrmDevice> drm = std::make_unique<DrmDevice>();
71 int displays_added, ret;
72 std::tie(ret, displays_added) = drm->Init(path.c_str(), num_displays_);
73 if (ret)
74 return ret;
75 std::shared_ptr<Importer> importer;
76 importer.reset(Importer::CreateInstance(drm.get()));
77 if (!importer) {
78 ALOGE("Failed to create importer instance");
79 return -ENODEV;
80 }
81 importers_.push_back(importer);
82 drms_.push_back(std::move(drm));
83 num_displays_ += displays_added;
84 return ret;
85}
86
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010087DrmConnector *ResourceManager::AvailableWritebackConnector(int display) {
88 DrmDevice *drm_device = GetDrmDevice(display);
89 DrmConnector *writeback_conn = NULL;
90 if (drm_device) {
91 writeback_conn = drm_device->AvailableWritebackConnector(display);
92 if (writeback_conn)
93 return writeback_conn;
94 }
95 for (auto &drm : drms_) {
96 if (drm.get() == drm_device)
97 continue;
98 writeback_conn = drm->AvailableWritebackConnector(display);
99 if (writeback_conn)
100 return writeback_conn;
101 }
102 return writeback_conn;
103}
104
Matvii Zorinec75ccd2020-07-17 12:08:45 +0300105bool ResourceManager::IsKMSDev(const char *path) {
106 int fd = open(path, O_RDWR | O_CLOEXEC);
107 if (fd < 0)
108 return false;
109
110 auto res = drmModeGetResources(fd);
111 if (!res) {
112 close(fd);
113 return false;
114 }
115
116 bool is_kms = res->count_crtcs > 0 && res->count_connectors > 0 &&
117 res->count_encoders > 0;
118
119 drmModeFreeResources(res);
120 close(fd);
121
122 return is_kms;
123}
124
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100125DrmDevice *ResourceManager::GetDrmDevice(int display) {
126 for (auto &drm : drms_) {
127 if (drm->HandlesDisplay(display))
128 return drm.get();
129 }
130 return NULL;
131}
132
133std::shared_ptr<Importer> ResourceManager::GetImporter(int display) {
134 for (unsigned int i = 0; i < drms_.size(); i++) {
135 if (drms_[i]->HandlesDisplay(display))
136 return importers_[i];
137 }
138 return NULL;
139}
140
141const gralloc_module_t *ResourceManager::gralloc() {
142 return gralloc_;
143}
Sean Paulf72cccd2018-08-27 13:59:08 -0400144} // namespace android