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