• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cutils/properties.h>
22 #include <log/log.h>
23 #include <sys/stat.h>
24 
25 #include <sstream>
26 
27 #include "bufferinfo/BufferInfoGetter.h"
28 
29 namespace android {
30 
ResourceManager()31 ResourceManager::ResourceManager() : num_displays_(0), gralloc_(NULL) {
32 }
33 
Init()34 int ResourceManager::Init() {
35   char path_pattern[PROPERTY_VALUE_MAX];
36   // Could be a valid path or it can have at the end of it the wildcard %
37   // which means that it will try open all devices until an error is met.
38   int path_len = property_get("vendor.hwc.drm.device", path_pattern,
39                               "/dev/dri/card%");
40   int ret = 0;
41   if (path_pattern[path_len - 1] != '%') {
42     ret = AddDrmDevice(std::string(path_pattern));
43   } else {
44     path_pattern[path_len - 1] = '\0';
45     for (int idx = 0; !ret; ++idx) {
46       std::ostringstream path;
47       path << path_pattern << idx;
48 
49       struct stat buf;
50       if (stat(path.str().c_str(), &buf)) {
51         break;
52       } else if (IsKMSDev(path.str().c_str())) {
53         ret = AddDrmDevice(path.str());
54       }
55     }
56   }
57 
58   if (!num_displays_) {
59     ALOGE("Failed to initialize any displays");
60     return ret ? -EINVAL : ret;
61   }
62 
63   char scale_with_gpu[PROPERTY_VALUE_MAX];
64   property_get("vendor.hwc.drm.scale_with_gpu", scale_with_gpu, "0");
65   scale_with_gpu_ = bool(strncmp(scale_with_gpu, "0", 1));
66 
67   if (!BufferInfoGetter::GetInstance()) {
68     ALOGE("Failed to initialize BufferInfoGetter");
69     return -EINVAL;
70   }
71 
72   return hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
73                        (const hw_module_t **)&gralloc_);
74 }
75 
AddDrmDevice(std::string path)76 int ResourceManager::AddDrmDevice(std::string path) {
77   std::unique_ptr<DrmDevice> drm = std::make_unique<DrmDevice>();
78   int displays_added, ret;
79   std::tie(ret, displays_added) = drm->Init(path.c_str(), num_displays_);
80   if (ret)
81     return ret;
82   std::shared_ptr<Importer> importer;
83   importer.reset(new DrmGenericImporter(drm.get()));
84   if (!importer) {
85     ALOGE("Failed to create importer instance");
86     return -ENODEV;
87   }
88   importers_.push_back(importer);
89   drms_.push_back(std::move(drm));
90   num_displays_ += displays_added;
91   return ret;
92 }
93 
AvailableWritebackConnector(int display)94 DrmConnector *ResourceManager::AvailableWritebackConnector(int display) {
95   DrmDevice *drm_device = GetDrmDevice(display);
96   DrmConnector *writeback_conn = NULL;
97   if (drm_device) {
98     writeback_conn = drm_device->AvailableWritebackConnector(display);
99     if (writeback_conn)
100       return writeback_conn;
101   }
102   for (auto &drm : drms_) {
103     if (drm.get() == drm_device)
104       continue;
105     writeback_conn = drm->AvailableWritebackConnector(display);
106     if (writeback_conn)
107       return writeback_conn;
108   }
109   return writeback_conn;
110 }
111 
IsKMSDev(const char * path)112 bool ResourceManager::IsKMSDev(const char *path) {
113   int fd = open(path, O_RDWR | O_CLOEXEC);
114   if (fd < 0)
115     return false;
116 
117   auto res = drmModeGetResources(fd);
118   if (!res) {
119     close(fd);
120     return false;
121   }
122 
123   bool is_kms = res->count_crtcs > 0 && res->count_connectors > 0 &&
124                 res->count_encoders > 0;
125 
126   drmModeFreeResources(res);
127   close(fd);
128 
129   return is_kms;
130 }
131 
GetDrmDevice(int display)132 DrmDevice *ResourceManager::GetDrmDevice(int display) {
133   for (auto &drm : drms_) {
134     if (drm->HandlesDisplay(display))
135       return drm.get();
136   }
137   return NULL;
138 }
139 
GetImporter(int display)140 std::shared_ptr<Importer> ResourceManager::GetImporter(int display) {
141   for (unsigned int i = 0; i < drms_.size(); i++) {
142     if (drms_[i]->HandlesDisplay(display))
143       return importers_[i];
144   }
145   return NULL;
146 }
147 
gralloc()148 const gralloc_module_t *ResourceManager::gralloc() {
149   return gralloc_;
150 }
151 }  // namespace android
152