blob: da1a2db95508f5757a93097be384252c372c0c26 [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
19#include "resourcemanager.h"
20
21#include <cutils/properties.h>
22#include <log/log.h>
23#include <sstream>
24#include <string>
25
26namespace android {
27
28ResourceManager::ResourceManager() : num_displays_(0), gralloc_(NULL) {
29}
30
31int ResourceManager::Init() {
32 char path_pattern[PROPERTY_VALUE_MAX];
33 // Could be a valid path or it can have at the end of it the wildcard %
34 // which means that it will try open all devices until an error is met.
35 int path_len = property_get("hwc.drm.device", path_pattern, "/dev/dri/card0");
36 int ret = 0;
37 if (path_pattern[path_len - 1] != '%') {
38 ret = AddDrmDevice(std::string(path_pattern));
39 } else {
40 path_pattern[path_len - 1] = '\0';
41 for (int idx = 0; !ret; ++idx) {
42 std::ostringstream path;
43 path << path_pattern << idx;
44 ret = AddDrmDevice(path.str());
45 }
46 }
47
48 if (!num_displays_) {
49 ALOGE("Failed to initialize any displays");
50 return ret ? -EINVAL : ret;
51 }
52
Roman Stratiienko65f2ba82019-12-20 17:04:01 +020053 char scale_with_gpu[PROPERTY_VALUE_MAX];
54 property_get("hwc.drm.scale_with_gpu", scale_with_gpu, "0");
55 scale_with_gpu_ = bool(strncmp(scale_with_gpu, "0", 1));
56
Alexandru Gheorghec5463582018-03-27 15:52:02 +010057 return hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
58 (const hw_module_t **)&gralloc_);
59}
60
61int ResourceManager::AddDrmDevice(std::string path) {
62 std::unique_ptr<DrmDevice> drm = std::make_unique<DrmDevice>();
63 int displays_added, ret;
64 std::tie(ret, displays_added) = drm->Init(path.c_str(), num_displays_);
65 if (ret)
66 return ret;
67 std::shared_ptr<Importer> importer;
68 importer.reset(Importer::CreateInstance(drm.get()));
69 if (!importer) {
70 ALOGE("Failed to create importer instance");
71 return -ENODEV;
72 }
73 importers_.push_back(importer);
74 drms_.push_back(std::move(drm));
75 num_displays_ += displays_added;
76 return ret;
77}
78
Alexandru Gheorgheb6a675e2018-03-27 16:10:55 +010079DrmConnector *ResourceManager::AvailableWritebackConnector(int display) {
80 DrmDevice *drm_device = GetDrmDevice(display);
81 DrmConnector *writeback_conn = NULL;
82 if (drm_device) {
83 writeback_conn = drm_device->AvailableWritebackConnector(display);
84 if (writeback_conn)
85 return writeback_conn;
86 }
87 for (auto &drm : drms_) {
88 if (drm.get() == drm_device)
89 continue;
90 writeback_conn = drm->AvailableWritebackConnector(display);
91 if (writeback_conn)
92 return writeback_conn;
93 }
94 return writeback_conn;
95}
96
Alexandru Gheorghec5463582018-03-27 15:52:02 +010097DrmDevice *ResourceManager::GetDrmDevice(int display) {
98 for (auto &drm : drms_) {
99 if (drm->HandlesDisplay(display))
100 return drm.get();
101 }
102 return NULL;
103}
104
105std::shared_ptr<Importer> ResourceManager::GetImporter(int display) {
106 for (unsigned int i = 0; i < drms_.size(); i++) {
107 if (drms_[i]->HandlesDisplay(display))
108 return importers_[i];
109 }
110 return NULL;
111}
112
113const gralloc_module_t *ResourceManager::gralloc() {
114 return gralloc_;
115}
Sean Paulf72cccd2018-08-27 13:59:08 -0400116} // namespace android