OSDN Git Service

drm_hwcomposer: Tidy-up DrmConnector class
[android-x86/external-drm_hwcomposer.git] / backend / BackendManager.cpp
1 /*
2  * Copyright (C) 2020 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-backend"
18
19 #include "BackendManager.h"
20
21 #include "utils/log.h"
22 #include "utils/properties.h"
23
24 namespace android {
25
26 // NOLINTNEXTLINE(cert-err58-cpp)
27 const std::vector<std::string> BackendManager::kClientDevices = {
28     "kirin",
29     "mediatek-drm",
30 };
31
32 BackendManager &BackendManager::GetInstance() {
33   static BackendManager backend_manager;
34
35   return backend_manager;
36 }
37
38 int BackendManager::RegisterBackend(const std::string &name,
39                                     BackendConstructorT backend_constructor) {
40   available_backends_[name] = std::move(backend_constructor);
41   return 0;
42 }
43
44 int BackendManager::SetBackendForDisplay(HwcDisplay *display) {
45   std::string driver_name(display->drm()->GetName());
46   char backend_override[PROPERTY_VALUE_MAX];
47   property_get("vendor.hwc.backend_override", backend_override,
48                driver_name.c_str());
49   std::string backend_name(backend_override);
50
51   display->set_backend(GetBackendByName(backend_name));
52   if (display->backend() == nullptr) {
53     ALOGE("Failed to set backend '%s' for '%s' and driver '%s'",
54           backend_name.c_str(), display->connector()->GetName().c_str(),
55           driver_name.c_str());
56     return -EINVAL;
57   }
58
59   ALOGI("Backend '%s' for '%s' and driver '%s' was successfully set",
60         backend_name.c_str(), display->connector()->GetName().c_str(),
61         driver_name.c_str());
62
63   return 0;
64 }
65
66 std::unique_ptr<Backend> BackendManager::GetBackendByName(std::string &name) {
67   if (available_backends_.empty()) {
68     ALOGE("No backends are specified");
69     return nullptr;
70   }
71
72   auto it = available_backends_.find(name);
73   if (it == available_backends_.end()) {
74     auto it = std::find(kClientDevices.begin(), kClientDevices.end(), name);
75     name = it == kClientDevices.end() ? "generic" : "client";
76   }
77
78   return available_backends_[name]();
79 }
80 }  // namespace android