OSDN Git Service

drm_hwcomposer: Remove unused AvailableWritebackConnector()
[android-x86/external-drm_hwcomposer.git] / drm / ResourceManager.cpp
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
19 #include "ResourceManager.h"
20
21 #include <fcntl.h>
22 #include <sys/stat.h>
23
24 #include <sstream>
25
26 #include "bufferinfo/BufferInfoGetter.h"
27 #include "utils/log.h"
28 #include "utils/properties.h"
29
30 namespace android {
31
32 ResourceManager::ResourceManager() : num_displays_(0) {
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.
39   int path_len = property_get("vendor.hwc.drm.device", path_pattern,
40                               "/dev/dri/card%");
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;
49
50       struct stat buf {};
51       if (stat(path.str().c_str(), &buf))
52         break;
53
54       if (IsKMSDev(path.str().c_str()))
55         ret = AddDrmDevice(path.str());
56     }
57   }
58
59   if (!num_displays_) {
60     ALOGE("Failed to initialize any displays");
61     return ret ? -EINVAL : ret;
62   }
63
64   char scale_with_gpu[PROPERTY_VALUE_MAX];
65   property_get("vendor.hwc.drm.scale_with_gpu", scale_with_gpu, "0");
66   scale_with_gpu_ = bool(strncmp(scale_with_gpu, "0", 1));
67
68   if (!BufferInfoGetter::GetInstance()) {
69     ALOGE("Failed to initialize BufferInfoGetter");
70     return -EINVAL;
71   }
72
73   return 0;
74 }
75
76 int ResourceManager::AddDrmDevice(const std::string &path) {
77   auto drm = std::make_unique<DrmDevice>();
78   int displays_added = 0;
79   int ret = 0;
80   std::tie(ret, displays_added) = drm->Init(path.c_str(), num_displays_);
81   drms_.push_back(std::move(drm));
82   num_displays_ += displays_added;
83   return ret;
84 }
85
86 bool ResourceManager::IsKMSDev(const char *path) {
87   auto fd = UniqueFd(open(path, O_RDWR | O_CLOEXEC));
88   if (!fd) {
89     return false;
90   }
91
92   auto res = MakeDrmModeResUnique(fd.Get());
93   if (!res) {
94     return false;
95   }
96
97   bool is_kms = res->count_crtcs > 0 && res->count_connectors > 0 &&
98                 res->count_encoders > 0;
99
100   return is_kms;
101 }
102
103 DrmDevice *ResourceManager::GetDrmDevice(int display) {
104   for (auto &drm : drms_) {
105     if (drm->HandlesDisplay(display))
106       return drm.get();
107   }
108   return nullptr;
109 }
110 }  // namespace android