blob: 1e2d09cef0a117b325faefafd245e3e57642112f [file] [log] [blame]
Paul Keith7193ce22018-08-12 20:05:57 +02001/*
dianlujitao7468c412020-06-23 22:16:50 +08002 * Copyright (C) 2019-2020 The LineageOS Project
Paul Keith7193ce22018-08-12 20:05:57 +02003 *
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
dianlujitao7468c412020-06-23 22:16:50 +080017#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
dianlujitao44e1a1f2020-06-24 22:51:34 +080023#include "livedisplay/sdm/DisplayModes.h"
LuK1337588974a2020-01-07 14:15:27 +010024
dianlujitao7468c412020-06-23 22:16:50 +080025#include <android-base/logging.h>
dianlujitaoa15a1f32020-06-23 23:42:05 +080026#include <android/hidl/manager/1.0/IServiceManager.h>
27#include <hidl/ServiceManagement.h>
Paul Keith7a5da242019-01-20 04:24:30 +010028
dianlujitao44e1a1f2020-06-24 22:51:34 +080029#include "livedisplay/sdm/Utils.h"
Paul Keith3f265622018-08-12 19:56:29 +020030
31namespace vendor {
32namespace lineage {
33namespace livedisplay {
34namespace V2_0 {
Paul Keith986bd642019-01-19 16:35:57 +010035namespace sdm {
Paul Keith3f265622018-08-12 19:56:29 +020036
dianlujitao7468c412020-06-23 22:16:50 +080037using ::android::OK;
dianlujitao4afd6622020-06-24 22:09:13 +080038using ::android::hardware::Void;
TheScarasticfd1713f2019-06-01 12:06:59 +053039
dianlujitao7468c412020-06-23 22:16:50 +080040DisplayModes::DisplayModes(std::shared_ptr<SDMController> controller)
41 : controller_(std::move(controller)) {
dianlujitaoa15a1f32020-06-23 23:42:05 +080042 if (!isReady()) {
43 LOG(FATAL) << "DisplayModes backend not ready, exiting.";
44 }
45
dianlujitao7468c412020-06-23 22:16:50 +080046 DisplayMode mode = getDefaultDisplayModeInternal();
47 if (mode.id >= 0) {
48 setDisplayMode(mode.id, false);
TheScarasticfd1713f2019-06-01 12:06:59 +053049 }
Paul Keith7a5da242019-01-20 04:24:30 +010050}
51
dianlujitaoa15a1f32020-06-23 23:42:05 +080052bool 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
67bool DisplayModes::isReady() {
Paul Keith7a5da242019-01-20 04:24:30 +010068 static int supported = -1;
69
70 if (supported >= 0) {
dianlujitao7468c412020-06-23 22:16:50 +080071 return supported;
Paul Keith7a5da242019-01-20 04:24:30 +010072 }
73
dianlujitaobb5ff4a2020-06-24 19:07:04 +080074 if (utils::CheckFeatureVersion(controller_, utils::FEATURE_VER_SW_SAVEMODES_API) != OK) {
Paul Keith7a5da242019-01-20 04:24:30 +010075 supported = 0;
dianlujitao7468c412020-06-23 22:16:50 +080076 return false;
Paul Keith7a5da242019-01-20 04:24:30 +010077 }
78
dianlujitao7468c412020-06-23 22:16:50 +080079 int32_t count = 0;
80 if (controller_->getNumDisplayModes(&count) != OK) {
81 count = 0;
Paul Keith7a5da242019-01-20 04:24:30 +010082 }
Paul Keith7a5da242019-01-20 04:24:30 +010083 supported = (count > 0);
dianlujitao7468c412020-06-23 22:16:50 +080084
Paul Keith7a5da242019-01-20 04:24:30 +010085 return supported;
86}
87
88std::vector<DisplayMode> DisplayModes::getDisplayModesInternal() {
89 std::vector<DisplayMode> modes;
90 int32_t count = 0;
Paul Keith7a5da242019-01-20 04:24:30 +010091
dianlujitao7468c412020-06-23 22:16:50 +080092 if (controller_->getNumDisplayModes(&count) != OK || count == 0) {
Paul Keith7a5da242019-01-20 04:24:30 +010093 return modes;
94 }
95
dianlujitao143150e2020-06-24 17:41:07 +080096 std::vector<SdmDispMode> tmp_modes(count);
dianlujitao7468c412020-06-23 22:16:50 +080097 if (controller_->getDisplayModes(tmp_modes.data(), count) == OK) {
98 for (auto&& mode : tmp_modes) {
99 modes.push_back({mode.id, mode.name});
Paul Keith7a5da242019-01-20 04:24:30 +0100100 }
Paul Keith7a5da242019-01-20 04:24:30 +0100101 }
102
103 return modes;
104}
105
106DisplayMode DisplayModes::getDisplayModeById(int32_t id) {
107 std::vector<DisplayMode> modes = getDisplayModesInternal();
108
dianlujitao4afd6622020-06-24 22:09:13 +0800109 for (auto&& mode : modes) {
Paul Keith7a5da242019-01-20 04:24:30 +0100110 if (mode.id == id) {
111 return mode;
112 }
113 }
114
115 return DisplayMode{-1, ""};
116}
117
118DisplayMode DisplayModes::getCurrentDisplayModeInternal() {
119 int32_t id = 0;
Paul Keith7a5da242019-01-20 04:24:30 +0100120
dianlujitao7468c412020-06-23 22:16:50 +0800121 if (controller_->getActiveDisplayMode(&id) == OK && id >= 0) {
122 return getDisplayModeById(id);
Paul Keith7a5da242019-01-20 04:24:30 +0100123 }
124
125 return DisplayMode{-1, ""};
126}
127
128DisplayMode DisplayModes::getDefaultDisplayModeInternal() {
129 int32_t id = 0;
Paul Keith7a5da242019-01-20 04:24:30 +0100130
dianlujitao7468c412020-06-23 22:16:50 +0800131 if (controller_->getDefaultDisplayMode(&id) == OK && id >= 0) {
132 return getDisplayModeById(id);
Paul Keith7a5da242019-01-20 04:24:30 +0100133 }
134
135 return DisplayMode{-1, ""};
136}
137
Paul Keith3f265622018-08-12 19:56:29 +0200138// Methods from ::vendor::lineage::livedisplay::V2_0::IDisplayModes follow.
139Return<void> DisplayModes::getDisplayModes(getDisplayModes_cb _hidl_cb) {
Paul Keith7a5da242019-01-20 04:24:30 +0100140 _hidl_cb(getDisplayModesInternal());
Paul Keith3f265622018-08-12 19:56:29 +0200141 return Void();
142}
143
144Return<void> DisplayModes::getCurrentDisplayMode(getCurrentDisplayMode_cb _hidl_cb) {
Paul Keith7a5da242019-01-20 04:24:30 +0100145 _hidl_cb(getCurrentDisplayModeInternal());
Paul Keith3f265622018-08-12 19:56:29 +0200146 return Void();
147}
148
149Return<void> DisplayModes::getDefaultDisplayMode(getDefaultDisplayMode_cb _hidl_cb) {
Paul Keith7a5da242019-01-20 04:24:30 +0100150 _hidl_cb(getDefaultDisplayModeInternal());
Paul Keith3f265622018-08-12 19:56:29 +0200151 return Void();
152}
153
dianlujitao143150e2020-06-24 17:41:07 +0800154Return<bool> DisplayModes::setDisplayMode(int32_t mode_id, bool make_default) {
155 DisplayMode current_mode = getCurrentDisplayModeInternal();
Paul Keith7a5da242019-01-20 04:24:30 +0100156
dianlujitao143150e2020-06-24 17:41:07 +0800157 if (current_mode.id >= 0 && current_mode.id == mode_id) {
Paul Keith7a5da242019-01-20 04:24:30 +0100158 return true;
159 }
160
dianlujitao143150e2020-06-24 17:41:07 +0800161 DisplayMode mode = getDisplayModeById(mode_id);
Paul Keith7a5da242019-01-20 04:24:30 +0100162 if (mode.id < 0) {
163 return false;
164 }
165
dianlujitao143150e2020-06-24 17:41:07 +0800166 if (controller_->setActiveDisplayMode(mode_id) != OK) {
Paul Keith7a5da242019-01-20 04:24:30 +0100167 return false;
168 }
169
dianlujitao143150e2020-06-24 17:41:07 +0800170 if (make_default && controller_->setDefaultDisplayMode(mode_id) != OK) {
Paul Keith7a5da242019-01-20 04:24:30 +0100171 return false;
172 }
173
dianlujitao9d5a9c92020-06-24 00:28:12 +0800174 if (on_display_mode_set_) {
175 on_display_mode_set_();
176 }
Paul Keith7a5da242019-01-20 04:24:30 +0100177
178 return true;
Paul Keith3f265622018-08-12 19:56:29 +0200179}
180
dianlujitao9d5a9c92020-06-24 00:28:12 +0800181void DisplayModes::registerDisplayModeSetCallback(DisplayModeSetCallback callback) {
182 on_display_mode_set_ = callback;
183}
184
Paul Keith986bd642019-01-19 16:35:57 +0100185} // namespace sdm
Paul Keith3f265622018-08-12 19:56:29 +0200186} // namespace V2_0
187} // namespace livedisplay
188} // namespace lineage
189} // namespace vendor