blob: a7d99ee310a0ee385a5d93ff4a3bd8fe0a1eb0a3 [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 Stratiienko19c162f2022-02-01 09:35:08 +020027#include "compositor/DrmDisplayCompositor.h"
Roman Stratiienko24a7fc42021-12-23 16:25:20 +020028#include "drm/DrmDevice.h"
Roman Stratiienko19c162f2022-02-01 09:35:08 +020029#include "drm/DrmDisplayPipeline.h"
Roman Stratiienko24a7fc42021-12-23 16:25:20 +020030#include "drm/DrmPlane.h"
Roman Stratiienkod518a052021-02-25 19:15:14 +020031#include "utils/log.h"
Roman Stratiienkod21071f2021-03-09 21:56:50 +020032#include "utils/properties.h"
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030033
Alexandru Gheorghec5463582018-03-27 15:52:02 +010034namespace android {
35
Roman Stratiienko123cdb02021-09-29 12:47:00 +030036ResourceManager::ResourceManager() : num_displays_(0) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +010037}
38
Roman Stratiienko24a7fc42021-12-23 16:25:20 +020039ResourceManager::~ResourceManager() {
40 uevent_listener_.Exit();
41}
42
Alexandru Gheorghec5463582018-03-27 15:52:02 +010043int ResourceManager::Init() {
44 char path_pattern[PROPERTY_VALUE_MAX];
45 // Could be a valid path or it can have at the end of it the wildcard %
46 // which means that it will try open all devices until an error is met.
Jason Macnakf1af9572020-08-20 11:49:51 -070047 int path_len = property_get("vendor.hwc.drm.device", path_pattern,
48 "/dev/dri/card%");
Alexandru Gheorghec5463582018-03-27 15:52:02 +010049 int ret = 0;
50 if (path_pattern[path_len - 1] != '%') {
51 ret = AddDrmDevice(std::string(path_pattern));
52 } else {
53 path_pattern[path_len - 1] = '\0';
Roman Stratiienkofc014f52021-12-23 19:04:29 +020054 for (int idx = 0; ret == 0; ++idx) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +010055 std::ostringstream path;
56 path << path_pattern << idx;
Matvii Zorinec75ccd2020-07-17 12:08:45 +030057
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020058 struct stat buf {};
Roman Stratiienkofc014f52021-12-23 19:04:29 +020059 if (stat(path.str().c_str(), &buf) != 0)
Matvii Zorinec75ccd2020-07-17 12:08:45 +030060 break;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020061
Roman Stratiienko56f4adc2021-09-29 12:47:12 +030062 if (DrmDevice::IsKMSDev(path.str().c_str()))
Matvii Zorinec75ccd2020-07-17 12:08:45 +030063 ret = AddDrmDevice(path.str());
Alexandru Gheorghec5463582018-03-27 15:52:02 +010064 }
65 }
66
Roman Stratiienkofc014f52021-12-23 19:04:29 +020067 if (num_displays_ == 0) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +010068 ALOGE("Failed to initialize any displays");
Roman Stratiienkofc014f52021-12-23 19:04:29 +020069 return ret != 0 ? -EINVAL : ret;
Alexandru Gheorghec5463582018-03-27 15:52:02 +010070 }
71
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020072 char scale_with_gpu[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -070073 property_get("vendor.hwc.drm.scale_with_gpu", scale_with_gpu, "0");
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020074 scale_with_gpu_ = bool(strncmp(scale_with_gpu, "0", 1));
75
Roman Stratiienkofc014f52021-12-23 19:04:29 +020076 if (BufferInfoGetter::GetInstance() == nullptr) {
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030077 ALOGE("Failed to initialize BufferInfoGetter");
78 return -EINVAL;
79 }
80
Roman Stratiienko1e053b42021-10-25 22:54:20 +030081 ret = uevent_listener_.Init();
Roman Stratiienkofc014f52021-12-23 19:04:29 +020082 if (ret != 0) {
Roman Stratiienko1e053b42021-10-25 22:54:20 +030083 ALOGE("Can't initialize event listener %d", ret);
84 return ret;
85 }
86
Roman Stratiienko123cdb02021-09-29 12:47:00 +030087 return 0;
Alexandru Gheorghec5463582018-03-27 15:52:02 +010088}
89
Roman Stratiienko8666dc92021-02-09 17:49:55 +020090int ResourceManager::AddDrmDevice(const std::string &path) {
91 auto drm = std::make_unique<DrmDevice>();
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020092 int displays_added = 0;
93 int ret = 0;
Alexandru Gheorghec5463582018-03-27 15:52:02 +010094 std::tie(ret, displays_added) = drm->Init(path.c_str(), num_displays_);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010095 drms_.push_back(std::move(drm));
96 num_displays_ += displays_added;
97 return ret;
98}
99
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200100DrmDisplayPipeline *ResourceManager::GetPipeline(int display) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100101 for (auto &drm : drms_) {
Roman Stratiienko19c162f2022-02-01 09:35:08 +0200102 auto *pipe = drm->GetPipelineForDisplay(display);
103 if (pipe != nullptr) {
104 return pipe;
105 }
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100106 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200107 return nullptr;
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100108}
Sean Paulf72cccd2018-08-27 13:59:08 -0400109} // namespace android