blob: 382a85ae158dc4c531be97a57c96cc92ac6dd567 [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
19#include "resourcemanager.h"
20
21#include <cutils/properties.h>
22#include <log/log.h>
Matvii Zorinec75ccd2020-07-17 12:08:45 +030023#include <sys/stat.h>
Alexandru Gheorghec5463582018-03-27 15:52:02 +010024#include <sstream>
25#include <string>
26
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.
Matvii Zorinec75ccd2020-07-17 12:08:45 +030036 int path_len = property_get("hwc.drm.device", path_pattern, "/dev/dri/card%");
Alexandru Gheorghec5463582018-03-27 15:52:02 +010037 int ret = 0;
38 if (path_pattern[path_len - 1] != '%') {
39 ret = AddDrmDevice(std::string(path_pattern));
40 } else {
41 path_pattern[path_len - 1] = '\0';
42 for (int idx = 0; !ret; ++idx) {
43 std::ostringstream path;
44 path << path_pattern << idx;
Matvii Zorinec75ccd2020-07-17 12:08:45 +030045
46 struct stat buf;
47 if (stat(path.str().c_str(), &buf)) {
48 break;
49 } else if (IsKMSDev(path.str().c_str())) {
50 ret = AddDrmDevice(path.str());
51 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +010052 }
53 }
54
55 if (!num_displays_) {
56 ALOGE("Failed to initialize any displays");
57 return ret ? -EINVAL : ret;
58 }
59
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020060 char scale_with_gpu[PROPERTY_VALUE_MAX];
61 property_get("hwc.drm.scale_with_gpu", scale_with_gpu, "0");
62 scale_with_gpu_ = bool(strncmp(scale_with_gpu, "0", 1));
63
Alexandru Gheorghec5463582018-03-27 15:52:02 +010064 return hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
65 (const hw_module_t **)&gralloc_);
66}
67
68int ResourceManager::AddDrmDevice(std::string path) {
69 std::unique_ptr<DrmDevice> drm = std::make_unique<DrmDevice>();
70 int displays_added, ret;
71 std::tie(ret, displays_added) = drm->Init(path.c_str(), num_displays_);
72 if (ret)
73 return ret;
74 std::shared_ptr<Importer> importer;
75 importer.reset(Importer::CreateInstance(drm.get()));
76 if (!importer) {
77 ALOGE("Failed to create importer instance");
78 return -ENODEV;
79 }
80 importers_.push_back(importer);
81 drms_.push_back(std::move(drm));
82 num_displays_ += displays_added;
83 return ret;
84}
85
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010086DrmConnector *ResourceManager::AvailableWritebackConnector(int display) {
87 DrmDevice *drm_device = GetDrmDevice(display);
88 DrmConnector *writeback_conn = NULL;
89 if (drm_device) {
90 writeback_conn = drm_device->AvailableWritebackConnector(display);
91 if (writeback_conn)
92 return writeback_conn;
93 }
94 for (auto &drm : drms_) {
95 if (drm.get() == drm_device)
96 continue;
97 writeback_conn = drm->AvailableWritebackConnector(display);
98 if (writeback_conn)
99 return writeback_conn;
100 }
101 return writeback_conn;
102}
103
Matvii Zorinec75ccd2020-07-17 12:08:45 +0300104bool ResourceManager::IsKMSDev(const char *path) {
105 int fd = open(path, O_RDWR | O_CLOEXEC);
106 if (fd < 0)
107 return false;
108
109 auto res = drmModeGetResources(fd);
110 if (!res) {
111 close(fd);
112 return false;
113 }
114
115 bool is_kms = res->count_crtcs > 0 && res->count_connectors > 0 &&
116 res->count_encoders > 0;
117
118 drmModeFreeResources(res);
119 close(fd);
120
121 return is_kms;
122}
123
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100124DrmDevice *ResourceManager::GetDrmDevice(int display) {
125 for (auto &drm : drms_) {
126 if (drm->HandlesDisplay(display))
127 return drm.get();
128 }
129 return NULL;
130}
131
132std::shared_ptr<Importer> ResourceManager::GetImporter(int display) {
133 for (unsigned int i = 0; i < drms_.size(); i++) {
134 if (drms_[i]->HandlesDisplay(display))
135 return importers_[i];
136 }
137 return NULL;
138}
139
140const gralloc_module_t *ResourceManager::gralloc() {
141 return gralloc_;
142}
Sean Paulf72cccd2018-08-27 13:59:08 -0400143} // namespace android