Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 1 | /* |
| 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 Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 19 | #include "ResourceManager.h" |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 20 | |
Roman Stratiienko | d21071f | 2021-03-09 21:56:50 +0200 | [diff] [blame] | 21 | #include <fcntl.h> |
Matvii Zorin | ec75ccd | 2020-07-17 12:08:45 +0300 | [diff] [blame] | 22 | #include <sys/stat.h> |
Roman Stratiienko | 13cc366 | 2020-08-29 21:35:39 +0300 | [diff] [blame] | 23 | |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 24 | #include <sstream> |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 25 | |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 26 | #include "bufferinfo/BufferInfoGetter.h" |
Roman Stratiienko | d518a05 | 2021-02-25 19:15:14 +0200 | [diff] [blame] | 27 | #include "utils/log.h" |
Roman Stratiienko | d21071f | 2021-03-09 21:56:50 +0200 | [diff] [blame] | 28 | #include "utils/properties.h" |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 29 | |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 30 | namespace android { |
| 31 | |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 32 | ResourceManager::ResourceManager() : num_displays_(0), gralloc_(nullptr) { |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | int 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 Macnak | f1af957 | 2020-08-20 11:49:51 -0700 | [diff] [blame] | 39 | int path_len = property_get("vendor.hwc.drm.device", path_pattern, |
| 40 | "/dev/dri/card%"); |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 41 | 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 Zorin | ec75ccd | 2020-07-17 12:08:45 +0300 | [diff] [blame] | 49 | |
Roman Stratiienko | b3b5c1e | 2021-02-15 13:44:19 +0200 | [diff] [blame] | 50 | struct stat buf {}; |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 51 | if (stat(path.str().c_str(), &buf)) |
Matvii Zorin | ec75ccd | 2020-07-17 12:08:45 +0300 | [diff] [blame] | 52 | break; |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 53 | |
| 54 | if (IsKMSDev(path.str().c_str())) |
Matvii Zorin | ec75ccd | 2020-07-17 12:08:45 +0300 | [diff] [blame] | 55 | ret = AddDrmDevice(path.str()); |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | if (!num_displays_) { |
| 60 | ALOGE("Failed to initialize any displays"); |
| 61 | return ret ? -EINVAL : ret; |
| 62 | } |
| 63 | |
Roman Stratiienko | 65f2ba8 | 2019-12-20 17:04:01 +0200 | [diff] [blame] | 64 | char scale_with_gpu[PROPERTY_VALUE_MAX]; |
Jason Macnak | f1af957 | 2020-08-20 11:49:51 -0700 | [diff] [blame] | 65 | property_get("vendor.hwc.drm.scale_with_gpu", scale_with_gpu, "0"); |
Roman Stratiienko | 65f2ba8 | 2019-12-20 17:04:01 +0200 | [diff] [blame] | 66 | scale_with_gpu_ = bool(strncmp(scale_with_gpu, "0", 1)); |
| 67 | |
Roman Stratiienko | b2e9fe2 | 2020-10-03 10:52:36 +0300 | [diff] [blame] | 68 | if (!BufferInfoGetter::GetInstance()) { |
| 69 | ALOGE("Failed to initialize BufferInfoGetter"); |
| 70 | return -EINVAL; |
| 71 | } |
| 72 | |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 73 | return hw_get_module(GRALLOC_HARDWARE_MODULE_ID, |
| 74 | (const hw_module_t **)&gralloc_); |
| 75 | } |
| 76 | |
Roman Stratiienko | 8666dc9 | 2021-02-09 17:49:55 +0200 | [diff] [blame^] | 77 | int ResourceManager::AddDrmDevice(const std::string &path) { |
| 78 | auto drm = std::make_unique<DrmDevice>(); |
Roman Stratiienko | b3b5c1e | 2021-02-15 13:44:19 +0200 | [diff] [blame] | 79 | int displays_added = 0; |
| 80 | int ret = 0; |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 81 | std::tie(ret, displays_added) = drm->Init(path.c_str(), num_displays_); |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 82 | drms_.push_back(std::move(drm)); |
| 83 | num_displays_ += displays_added; |
| 84 | return ret; |
| 85 | } |
| 86 | |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 87 | DrmConnector *ResourceManager::AvailableWritebackConnector(int display) { |
| 88 | DrmDevice *drm_device = GetDrmDevice(display); |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 89 | DrmConnector *writeback_conn = nullptr; |
Alexandru Gheorghe | b6a675e | 2018-03-27 16:10:55 +0100 | [diff] [blame] | 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 Zorin | ec75ccd | 2020-07-17 12:08:45 +0300 | [diff] [blame] | 105 | bool ResourceManager::IsKMSDev(const char *path) { |
| 106 | int fd = open(path, O_RDWR | O_CLOEXEC); |
| 107 | if (fd < 0) |
| 108 | return false; |
| 109 | |
Roman Stratiienko | d21071f | 2021-03-09 21:56:50 +0200 | [diff] [blame] | 110 | auto *res = drmModeGetResources(fd); |
Matvii Zorin | ec75ccd | 2020-07-17 12:08:45 +0300 | [diff] [blame] | 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 Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 125 | DrmDevice *ResourceManager::GetDrmDevice(int display) { |
| 126 | for (auto &drm : drms_) { |
| 127 | if (drm->HandlesDisplay(display)) |
| 128 | return drm.get(); |
| 129 | } |
Roman Stratiienko | e2f2c92 | 2021-02-13 10:57:47 +0200 | [diff] [blame] | 130 | return nullptr; |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 131 | } |
| 132 | |
Alexandru Gheorghe | c546358 | 2018-03-27 15:52:02 +0100 | [diff] [blame] | 133 | const gralloc_module_t *ResourceManager::gralloc() { |
| 134 | return gralloc_; |
| 135 | } |
Sean Paul | f72cccd | 2018-08-27 13:59:08 -0400 | [diff] [blame] | 136 | } // namespace android |