blob: 2cd393f8819f86ffc475c788d44d0b808c27bdda [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 Stratiienko24a7fc42021-12-23 16:25:20 +020027#include "drm/DrmDevice.h"
28#include "drm/DrmPlane.h"
Roman Stratiienkod518a052021-02-25 19:15:14 +020029#include "utils/log.h"
Roman Stratiienkod21071f2021-03-09 21:56:50 +020030#include "utils/properties.h"
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030031
Alexandru Gheorghec5463582018-03-27 15:52:02 +010032namespace android {
33
Roman Stratiienko123cdb02021-09-29 12:47:00 +030034ResourceManager::ResourceManager() : num_displays_(0) {
Alexandru Gheorghec5463582018-03-27 15:52:02 +010035}
36
Roman Stratiienko24a7fc42021-12-23 16:25:20 +020037ResourceManager::~ResourceManager() {
38 uevent_listener_.Exit();
39}
40
Alexandru Gheorghec5463582018-03-27 15:52:02 +010041int ResourceManager::Init() {
42 char path_pattern[PROPERTY_VALUE_MAX];
43 // Could be a valid path or it can have at the end of it the wildcard %
44 // which means that it will try open all devices until an error is met.
Jason Macnakf1af9572020-08-20 11:49:51 -070045 int path_len = property_get("vendor.hwc.drm.device", path_pattern,
46 "/dev/dri/card%");
Alexandru Gheorghec5463582018-03-27 15:52:02 +010047 int ret = 0;
48 if (path_pattern[path_len - 1] != '%') {
49 ret = AddDrmDevice(std::string(path_pattern));
50 } else {
51 path_pattern[path_len - 1] = '\0';
52 for (int idx = 0; !ret; ++idx) {
53 std::ostringstream path;
54 path << path_pattern << idx;
Matvii Zorinec75ccd2020-07-17 12:08:45 +030055
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020056 struct stat buf {};
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020057 if (stat(path.str().c_str(), &buf))
Matvii Zorinec75ccd2020-07-17 12:08:45 +030058 break;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020059
Roman Stratiienko56f4adc2021-09-29 12:47:12 +030060 if (DrmDevice::IsKMSDev(path.str().c_str()))
Matvii Zorinec75ccd2020-07-17 12:08:45 +030061 ret = AddDrmDevice(path.str());
Alexandru Gheorghec5463582018-03-27 15:52:02 +010062 }
63 }
64
65 if (!num_displays_) {
66 ALOGE("Failed to initialize any displays");
67 return ret ? -EINVAL : ret;
68 }
69
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020070 char scale_with_gpu[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -070071 property_get("vendor.hwc.drm.scale_with_gpu", scale_with_gpu, "0");
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020072 scale_with_gpu_ = bool(strncmp(scale_with_gpu, "0", 1));
73
Roman Stratiienkob2e9fe22020-10-03 10:52:36 +030074 if (!BufferInfoGetter::GetInstance()) {
75 ALOGE("Failed to initialize BufferInfoGetter");
76 return -EINVAL;
77 }
78
Roman Stratiienko1e053b42021-10-25 22:54:20 +030079 ret = uevent_listener_.Init();
80 if (ret) {
81 ALOGE("Can't initialize event listener %d", ret);
82 return ret;
83 }
84
Roman Stratiienko123cdb02021-09-29 12:47:00 +030085 return 0;
Alexandru Gheorghec5463582018-03-27 15:52:02 +010086}
87
Roman Stratiienko8666dc92021-02-09 17:49:55 +020088int ResourceManager::AddDrmDevice(const std::string &path) {
89 auto drm = std::make_unique<DrmDevice>();
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020090 int displays_added = 0;
91 int ret = 0;
Alexandru Gheorghec5463582018-03-27 15:52:02 +010092 std::tie(ret, displays_added) = drm->Init(path.c_str(), num_displays_);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010093 drms_.push_back(std::move(drm));
94 num_displays_ += displays_added;
95 return ret;
96}
97
Alexandru Gheorghec5463582018-03-27 15:52:02 +010098DrmDevice *ResourceManager::GetDrmDevice(int display) {
99 for (auto &drm : drms_) {
100 if (drm->HandlesDisplay(display))
101 return drm.get();
102 }
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200103 return nullptr;
Alexandru Gheorghec5463582018-03-27 15:52:02 +0100104}
Sean Paulf72cccd2018-08-27 13:59:08 -0400105} // namespace android