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 "drmhwc"
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 "pl111",
31 };
32
GetInstance()33 BackendManager &BackendManager::GetInstance() {
34 static BackendManager backend_manager;
35
36 return backend_manager;
37 }
38
RegisterBackend(const std::string & name,BackendConstructorT backend_constructor)39 int BackendManager::RegisterBackend(const std::string &name,
40 BackendConstructorT backend_constructor) {
41 available_backends_[name] = std::move(backend_constructor);
42 return 0;
43 }
44
SetBackendForDisplay(HwcDisplay * display)45 int BackendManager::SetBackendForDisplay(HwcDisplay *display) {
46 auto driver_name(display->GetPipe().device->GetName());
47 char backend_override[PROPERTY_VALUE_MAX];
48 property_get("vendor.hwc.backend_override", backend_override,
49 driver_name.c_str());
50 std::string backend_name(backend_override);
51
52 display->set_backend(GetBackendByName(backend_name));
53 if (display->backend() == nullptr) {
54 ALOGE("Failed to set backend '%s' for '%s' and driver '%s'",
55 backend_name.c_str(),
56 display->GetPipe().connector->Get()->GetName().c_str(),
57 driver_name.c_str());
58 return -EINVAL;
59 }
60
61 ALOGI("Backend '%s' for '%s' and driver '%s' was successfully set",
62 backend_name.c_str(),
63 display->GetPipe().connector->Get()->GetName().c_str(),
64 driver_name.c_str());
65
66 return 0;
67 }
68
GetBackendByName(std::string & name)69 std::unique_ptr<Backend> BackendManager::GetBackendByName(std::string &name) {
70 if (available_backends_.empty()) {
71 ALOGE("No backends are specified");
72 return nullptr;
73 }
74
75 auto it = available_backends_.find(name);
76 if (it == available_backends_.end()) {
77 auto it = std::find(kClientDevices.begin(), kClientDevices.end(), name);
78 name = it == kClientDevices.end() ? "generic" : "client";
79 }
80
81 return available_backends_[name]();
82 }
83 } // namespace android
84