Paul Keith | 7193ce2 | 2018-08-12 20:05:57 +0200 | [diff] [blame] | 1 | /* |
dianlujitao | 7468c41 | 2020-06-23 22:16:50 +0800 | [diff] [blame] | 2 | * Copyright (C) 2019-2020 The LineageOS Project |
Paul Keith | 7193ce2 | 2018-08-12 20:05:57 +0200 | [diff] [blame] | 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 | |
dianlujitao | 7468c41 | 2020-06-23 22:16:50 +0800 | [diff] [blame] | 17 | #ifdef LIVES_IN_SYSTEM |
| 18 | #define LOG_TAG "lineage.livedisplay@2.0-impl-sdm" |
| 19 | #else |
| 20 | #define LOG_TAG "vendor.lineage.livedisplay@2.0-impl-sdm" |
| 21 | #endif |
| 22 | |
dianlujitao | 44e1a1f | 2020-06-24 22:51:34 +0800 | [diff] [blame] | 23 | #include "livedisplay/sdm/DisplayModes.h" |
LuK1337 | 588974a | 2020-01-07 14:15:27 +0100 | [diff] [blame] | 24 | |
dianlujitao | 7468c41 | 2020-06-23 22:16:50 +0800 | [diff] [blame] | 25 | #include <android-base/logging.h> |
dianlujitao | a15a1f3 | 2020-06-23 23:42:05 +0800 | [diff] [blame] | 26 | #include <android/hidl/manager/1.0/IServiceManager.h> |
| 27 | #include <hidl/ServiceManagement.h> |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 28 | |
dianlujitao | 44e1a1f | 2020-06-24 22:51:34 +0800 | [diff] [blame] | 29 | #include "livedisplay/sdm/Utils.h" |
Paul Keith | 3f26562 | 2018-08-12 19:56:29 +0200 | [diff] [blame] | 30 | |
| 31 | namespace vendor { |
| 32 | namespace lineage { |
| 33 | namespace livedisplay { |
| 34 | namespace V2_0 { |
Paul Keith | 986bd64 | 2019-01-19 16:35:57 +0100 | [diff] [blame] | 35 | namespace sdm { |
Paul Keith | 3f26562 | 2018-08-12 19:56:29 +0200 | [diff] [blame] | 36 | |
dianlujitao | 7468c41 | 2020-06-23 22:16:50 +0800 | [diff] [blame] | 37 | using ::android::OK; |
dianlujitao | 4afd662 | 2020-06-24 22:09:13 +0800 | [diff] [blame] | 38 | using ::android::hardware::Void; |
TheScarastic | fd1713f | 2019-06-01 12:06:59 +0530 | [diff] [blame] | 39 | |
dianlujitao | 7468c41 | 2020-06-23 22:16:50 +0800 | [diff] [blame] | 40 | DisplayModes::DisplayModes(std::shared_ptr<SDMController> controller) |
| 41 | : controller_(std::move(controller)) { |
dianlujitao | a15a1f3 | 2020-06-23 23:42:05 +0800 | [diff] [blame] | 42 | if (!isReady()) { |
| 43 | LOG(FATAL) << "DisplayModes backend not ready, exiting."; |
| 44 | } |
| 45 | |
dianlujitao | 7468c41 | 2020-06-23 22:16:50 +0800 | [diff] [blame] | 46 | DisplayMode mode = getDefaultDisplayModeInternal(); |
| 47 | if (mode.id >= 0) { |
| 48 | setDisplayMode(mode.id, false); |
TheScarastic | fd1713f | 2019-06-01 12:06:59 +0530 | [diff] [blame] | 49 | } |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 50 | } |
| 51 | |
dianlujitao | a15a1f3 | 2020-06-23 23:42:05 +0800 | [diff] [blame] | 52 | bool DisplayModes::isSupported(const hidl_string& name) { |
| 53 | using ::android::hidl::manager::V1_0::IServiceManager; |
| 54 | auto sm = ::android::hardware::defaultServiceManager(); |
| 55 | /* |
| 56 | * We MUST NOT use DisplayModes::isReady to check the availability here, |
| 57 | * but check the existence in manifest instead. Because in certain cases |
| 58 | * QDCM backend might fail to initialize thus isReady would return false, |
| 59 | * even though the device does support the feature and the interface is |
| 60 | * declared in manifest. Under the circumstance, the HAL will abort and |
| 61 | * hopefully recover from the failure when started again. |
| 62 | */ |
| 63 | auto transport = sm->getTransport(descriptor, name); |
| 64 | return transport != IServiceManager::Transport::EMPTY; |
| 65 | } |
| 66 | |
| 67 | bool DisplayModes::isReady() { |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 68 | static int supported = -1; |
| 69 | |
| 70 | if (supported >= 0) { |
dianlujitao | 7468c41 | 2020-06-23 22:16:50 +0800 | [diff] [blame] | 71 | return supported; |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 72 | } |
| 73 | |
dianlujitao | bb5ff4a | 2020-06-24 19:07:04 +0800 | [diff] [blame] | 74 | if (utils::CheckFeatureVersion(controller_, utils::FEATURE_VER_SW_SAVEMODES_API) != OK) { |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 75 | supported = 0; |
dianlujitao | 7468c41 | 2020-06-23 22:16:50 +0800 | [diff] [blame] | 76 | return false; |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 77 | } |
| 78 | |
dianlujitao | 7468c41 | 2020-06-23 22:16:50 +0800 | [diff] [blame] | 79 | int32_t count = 0; |
| 80 | if (controller_->getNumDisplayModes(&count) != OK) { |
| 81 | count = 0; |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 82 | } |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 83 | supported = (count > 0); |
dianlujitao | 7468c41 | 2020-06-23 22:16:50 +0800 | [diff] [blame] | 84 | |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 85 | return supported; |
| 86 | } |
| 87 | |
| 88 | std::vector<DisplayMode> DisplayModes::getDisplayModesInternal() { |
| 89 | std::vector<DisplayMode> modes; |
| 90 | int32_t count = 0; |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 91 | |
dianlujitao | 7468c41 | 2020-06-23 22:16:50 +0800 | [diff] [blame] | 92 | if (controller_->getNumDisplayModes(&count) != OK || count == 0) { |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 93 | return modes; |
| 94 | } |
| 95 | |
dianlujitao | 143150e | 2020-06-24 17:41:07 +0800 | [diff] [blame] | 96 | std::vector<SdmDispMode> tmp_modes(count); |
dianlujitao | 7468c41 | 2020-06-23 22:16:50 +0800 | [diff] [blame] | 97 | if (controller_->getDisplayModes(tmp_modes.data(), count) == OK) { |
| 98 | for (auto&& mode : tmp_modes) { |
| 99 | modes.push_back({mode.id, mode.name}); |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 100 | } |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | return modes; |
| 104 | } |
| 105 | |
| 106 | DisplayMode DisplayModes::getDisplayModeById(int32_t id) { |
| 107 | std::vector<DisplayMode> modes = getDisplayModesInternal(); |
| 108 | |
dianlujitao | 4afd662 | 2020-06-24 22:09:13 +0800 | [diff] [blame] | 109 | for (auto&& mode : modes) { |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 110 | if (mode.id == id) { |
| 111 | return mode; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return DisplayMode{-1, ""}; |
| 116 | } |
| 117 | |
| 118 | DisplayMode DisplayModes::getCurrentDisplayModeInternal() { |
| 119 | int32_t id = 0; |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 120 | |
dianlujitao | 7468c41 | 2020-06-23 22:16:50 +0800 | [diff] [blame] | 121 | if (controller_->getActiveDisplayMode(&id) == OK && id >= 0) { |
| 122 | return getDisplayModeById(id); |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | return DisplayMode{-1, ""}; |
| 126 | } |
| 127 | |
| 128 | DisplayMode DisplayModes::getDefaultDisplayModeInternal() { |
| 129 | int32_t id = 0; |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 130 | |
dianlujitao | 7468c41 | 2020-06-23 22:16:50 +0800 | [diff] [blame] | 131 | if (controller_->getDefaultDisplayMode(&id) == OK && id >= 0) { |
| 132 | return getDisplayModeById(id); |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | return DisplayMode{-1, ""}; |
| 136 | } |
| 137 | |
Paul Keith | 3f26562 | 2018-08-12 19:56:29 +0200 | [diff] [blame] | 138 | // Methods from ::vendor::lineage::livedisplay::V2_0::IDisplayModes follow. |
| 139 | Return<void> DisplayModes::getDisplayModes(getDisplayModes_cb _hidl_cb) { |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 140 | _hidl_cb(getDisplayModesInternal()); |
Paul Keith | 3f26562 | 2018-08-12 19:56:29 +0200 | [diff] [blame] | 141 | return Void(); |
| 142 | } |
| 143 | |
| 144 | Return<void> DisplayModes::getCurrentDisplayMode(getCurrentDisplayMode_cb _hidl_cb) { |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 145 | _hidl_cb(getCurrentDisplayModeInternal()); |
Paul Keith | 3f26562 | 2018-08-12 19:56:29 +0200 | [diff] [blame] | 146 | return Void(); |
| 147 | } |
| 148 | |
| 149 | Return<void> DisplayModes::getDefaultDisplayMode(getDefaultDisplayMode_cb _hidl_cb) { |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 150 | _hidl_cb(getDefaultDisplayModeInternal()); |
Paul Keith | 3f26562 | 2018-08-12 19:56:29 +0200 | [diff] [blame] | 151 | return Void(); |
| 152 | } |
| 153 | |
dianlujitao | 143150e | 2020-06-24 17:41:07 +0800 | [diff] [blame] | 154 | Return<bool> DisplayModes::setDisplayMode(int32_t mode_id, bool make_default) { |
| 155 | DisplayMode current_mode = getCurrentDisplayModeInternal(); |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 156 | |
dianlujitao | 143150e | 2020-06-24 17:41:07 +0800 | [diff] [blame] | 157 | if (current_mode.id >= 0 && current_mode.id == mode_id) { |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 158 | return true; |
| 159 | } |
| 160 | |
dianlujitao | 143150e | 2020-06-24 17:41:07 +0800 | [diff] [blame] | 161 | DisplayMode mode = getDisplayModeById(mode_id); |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 162 | if (mode.id < 0) { |
| 163 | return false; |
| 164 | } |
| 165 | |
dianlujitao | 143150e | 2020-06-24 17:41:07 +0800 | [diff] [blame] | 166 | if (controller_->setActiveDisplayMode(mode_id) != OK) { |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 167 | return false; |
| 168 | } |
| 169 | |
dianlujitao | 143150e | 2020-06-24 17:41:07 +0800 | [diff] [blame] | 170 | if (make_default && controller_->setDefaultDisplayMode(mode_id) != OK) { |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 171 | return false; |
| 172 | } |
| 173 | |
dianlujitao | 9d5a9c9 | 2020-06-24 00:28:12 +0800 | [diff] [blame] | 174 | if (on_display_mode_set_) { |
| 175 | on_display_mode_set_(); |
| 176 | } |
Paul Keith | 7a5da24 | 2019-01-20 04:24:30 +0100 | [diff] [blame] | 177 | |
| 178 | return true; |
Paul Keith | 3f26562 | 2018-08-12 19:56:29 +0200 | [diff] [blame] | 179 | } |
| 180 | |
dianlujitao | 9d5a9c9 | 2020-06-24 00:28:12 +0800 | [diff] [blame] | 181 | void DisplayModes::registerDisplayModeSetCallback(DisplayModeSetCallback callback) { |
| 182 | on_display_mode_set_ = callback; |
| 183 | } |
| 184 | |
Paul Keith | 986bd64 | 2019-01-19 16:35:57 +0100 | [diff] [blame] | 185 | } // namespace sdm |
Paul Keith | 3f26562 | 2018-08-12 19:56:29 +0200 | [diff] [blame] | 186 | } // namespace V2_0 |
| 187 | } // namespace livedisplay |
| 188 | } // namespace lineage |
| 189 | } // namespace vendor |