blob: d4a7053f8048fb86ad2be533eb07771c796c8e0a [file] [log] [blame]
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001/*
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +05302 * Copyright (c) 2014-2020, The Linux Foundation. All rights reserved.
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05003 * Not a Contribution.
4 *
5 * Copyright 2015 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050020#include <QService.h>
Naseer Ahmede7a77982018-06-04 10:56:04 -040021#include <binder/Parcel.h>
22#include <core/buffer_allocator.h>
23#include <cutils/properties.h>
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050024#include <display_config.h>
Naseer Ahmede7a77982018-06-04 10:56:04 -040025#include <hardware_legacy/uevent.h>
26#include <private/color_params.h>
Ramakant Singhfae59892017-09-13 10:40:22 +053027#include <qd_utils.h>
Naseer Ahmede7a77982018-06-04 10:56:04 -040028#include <sync/sync.h>
29#include <sys/prctl.h>
30#include <sys/resource.h>
31#include <utils/String16.h>
32#include <utils/constants.h>
33#include <utils/debug.h>
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +053034#include <QService.h>
Ramakant Singhfae59892017-09-13 10:40:22 +053035#include <utils/utils.h>
Naseer Ahmed66d5b492017-02-23 20:30:17 -050036#include <algorithm>
Mahesh Aiaf3a1f992019-01-16 13:07:42 -080037#include <utility>
Dileep Marchya7290bd22016-06-09 11:37:20 +053038#include <bitset>
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +053039#include <iterator>
Dileep Marchya550967a2017-05-24 17:03:30 +053040#include <memory>
Naseer Ahmede7a77982018-06-04 10:56:04 -040041#include <string>
42#include <thread>
43#include <vector>
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050044
45#include "hwc_buffer_allocator.h"
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050046#include "hwc_session.h"
47#include "hwc_debugger.h"
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050048
49#define __CLASS__ "HWCSession"
50
51#define HWC_UEVENT_SWITCH_HDMI "change@/devices/virtual/switch/hdmi"
Prabhanjan Kandula34ffec02017-04-25 11:56:39 -070052#define HWC_UEVENT_DRM_EXT_HOTPLUG "mdss_mdp/drm/card"
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050053
Naseer Ahmed8584a0f2020-02-19 22:23:30 -050054using HwcAttribute = composer_V2_4::IComposerClient::Attribute;
55
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050056namespace sdm {
Dileep Marchya550967a2017-05-24 17:03:30 +053057
58static HWCUEvent g_hwc_uevent_;
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -070059Locker HWCSession::locker_[HWCCallbacks::kNumDisplays];
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +053060bool HWCSession::pending_power_mode_[HWCCallbacks::kNumDisplays];
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +053061Locker HWCSession::power_state_[HWCCallbacks::kNumDisplays];
Pullakavi Srinivas1ae4acb2019-11-18 19:33:45 +053062Locker HWCSession::hdr_locker_[HWCCallbacks::kNumDisplays];
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +053063Locker HWCSession::display_config_locker_;
Gopikrishnaiah Anandana340b702018-01-16 15:37:15 -080064static const int kSolidFillDelay = 100 * 1000;
Varun Aroraad2719a2018-10-25 10:13:53 -070065int HWCSession::null_display_mode_ = 0;
Xu Yang45d0abf2019-07-05 11:34:06 +080066static const uint32_t kBrightnessScaleMax = 100;
67static const uint32_t kSvBlScaleMax = 65535;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -050068
Sushil Chauhanfea3e902018-07-31 15:34:48 -070069// Map the known color modes to dataspace.
Arun Kumar K.R5b821552018-12-07 08:00:15 +053070int32_t GetDataspaceFromColorMode(ColorMode mode) {
Sushil Chauhanfea3e902018-07-31 15:34:48 -070071 switch (mode) {
72 case ColorMode::SRGB:
73 case ColorMode::NATIVE:
74 return HAL_DATASPACE_V0_SRGB;
75 case ColorMode::DCI_P3:
76 return HAL_DATASPACE_DCI_P3;
77 case ColorMode::DISPLAY_P3:
78 return HAL_DATASPACE_DISPLAY_P3;
79 case ColorMode::BT2100_PQ:
80 return HAL_DATASPACE_BT2020_PQ;
81 case ColorMode::BT2100_HLG:
82 return HAL_DATASPACE_BT2020_HLG;
Gousemoodhin Nadaf0e566872019-05-13 16:14:54 +053083 case ColorMode::DISPLAY_BT2020:
84 return HAL_DATASPACE_DISPLAY_BT2020;
Sushil Chauhanfea3e902018-07-31 15:34:48 -070085 default:
86 return HAL_DATASPACE_UNKNOWN;
87 }
88}
89
Ramkumar Radhakrishnan048dcc42017-06-09 16:20:56 -070090void HWCUEvent::UEventThread(HWCUEvent *hwc_uevent) {
Dileep Marchya550967a2017-05-24 17:03:30 +053091 const char *uevent_thread_name = "HWC_UeventThread";
92
93 prctl(PR_SET_NAME, uevent_thread_name, 0, 0, 0);
94 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
95
Ramkumar Radhakrishnan048dcc42017-06-09 16:20:56 -070096 int status = uevent_init();
97 if (!status) {
98 std::unique_lock<std::mutex> caller_lock(hwc_uevent->mutex_);
99 hwc_uevent->caller_cv_.notify_one();
100 DLOGE("Failed to init uevent with err %d", status);
101 return;
102 }
103
104 {
105 // Signal caller thread that worker thread is ready to listen to events.
106 std::unique_lock<std::mutex> caller_lock(hwc_uevent->mutex_);
107 hwc_uevent->init_done_ = true;
108 hwc_uevent->caller_cv_.notify_one();
Dileep Marchya550967a2017-05-24 17:03:30 +0530109 }
110
111 while (1) {
112 char uevent_data[PAGE_SIZE] = {};
113
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530114 // keep last 2 zeros to ensure double 0 termination
Dileep Marchya550967a2017-05-24 17:03:30 +0530115 int length = uevent_next_event(uevent_data, INT32(sizeof(uevent_data)) - 2);
116
117 // scope of lock to this block only, so that caller is free to set event handler to nullptr;
118 {
Ramkumar Radhakrishnan048dcc42017-06-09 16:20:56 -0700119 std::lock_guard<std::mutex> guard(hwc_uevent->mutex_);
120 if (hwc_uevent->uevent_listener_) {
121 hwc_uevent->uevent_listener_->UEventHandler(uevent_data, length);
Dileep Marchya550967a2017-05-24 17:03:30 +0530122 } else {
123 DLOGW("UEvent dropped. No uevent listener.");
124 }
125 }
126 }
Dileep Marchya550967a2017-05-24 17:03:30 +0530127}
128
129HWCUEvent::HWCUEvent() {
Ramkumar Radhakrishnan048dcc42017-06-09 16:20:56 -0700130 std::unique_lock<std::mutex> caller_lock(mutex_);
Dileep Marchya550967a2017-05-24 17:03:30 +0530131 std::thread thread(HWCUEvent::UEventThread, this);
132 thread.detach();
Ramkumar Radhakrishnan048dcc42017-06-09 16:20:56 -0700133 caller_cv_.wait(caller_lock);
Dileep Marchya550967a2017-05-24 17:03:30 +0530134}
135
Ramkumar Radhakrishnan048dcc42017-06-09 16:20:56 -0700136void HWCUEvent::Register(HWCUEventListener *uevent_listener) {
137 DLOGI("Set uevent listener = %p", uevent_listener);
Dileep Marchya550967a2017-05-24 17:03:30 +0530138
139 std::lock_guard<std::mutex> obj(mutex_);
Ramkumar Radhakrishnan048dcc42017-06-09 16:20:56 -0700140 uevent_listener_ = uevent_listener;
Dileep Marchya550967a2017-05-24 17:03:30 +0530141}
142
Dileep Marchyacb2a3a92019-06-21 17:30:50 +0530143HWCSession::HWCSession() : cwb_(this) {}
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530144
145HWCSession *HWCSession::GetInstance() {
146 // executed only once for the very first call.
147 // GetInstance called multiple times from Composer and ComposerClient
148 static HWCSession *hwc_session = new HWCSession();
149 return hwc_session;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500150}
151
152int HWCSession::Init() {
Saurabh Shahc5c687a2017-08-24 16:30:14 -0700153 SCOPE_LOCK(locker_[HWC_DISPLAY_PRIMARY]);
154
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500155 int status = -EINVAL;
156 const char *qservice_name = "display.qservice";
157
Ramkumar Radhakrishnan048dcc42017-06-09 16:20:56 -0700158 if (!g_hwc_uevent_.InitDone()) {
159 return status;
160 }
161
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530162
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500163 // Start QService and connect to it.
164 qService::QService::init();
165 android::sp<qService::IQService> iqservice = android::interface_cast<qService::IQService>(
166 android::defaultServiceManager()->getService(android::String16(qservice_name)));
167
168 if (iqservice.get()) {
169 iqservice->connect(android::sp<qClient::IQClient>(this));
170 qservice_ = reinterpret_cast<qService::QService *>(iqservice.get());
171 } else {
Dileep Marchya4cb86b42018-05-01 19:34:51 +0530172 DLOGE("Failed to acquire %s", qservice_name);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500173 return -EINVAL;
174 }
175
Uday Kiran Pichika5e656b22018-05-15 18:48:24 +0530176 HWCDebugHandler::Get()->GetProperty(ENABLE_NULL_DISPLAY_PROP, &null_display_mode_);
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +0530177 HWCDebugHandler::Get()->GetProperty(DISABLE_HOTPLUG_BWCHECK, &disable_hotplug_bwcheck_);
Pullakavi Srinivas2fbff8d2019-04-22 18:32:52 -0700178 HWCDebugHandler::Get()->GetProperty(DISABLE_MASK_LAYER_HINT, &disable_mask_layer_hint_);
Ramkumar Radhakrishnanddbf83c2017-06-28 20:01:59 -0700179
Pullakavi Srinivasf2a31852019-04-23 14:32:08 -0700180 if (!null_display_mode_) {
Varun Arora429bf402018-10-01 13:32:33 -0700181 g_hwc_uevent_.Register(this);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500182 }
183
Pullakavi Srinivas5cb87bd2020-04-02 22:30:55 +0530184 int value = 0; // Default value when property is not present.
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +0530185 Debug::Get()->GetProperty(ENABLE_ASYNC_POWERMODE, &value);
186 async_powermode_ = (value == 1);
187 DLOGI("builtin_powermode_override: %d", async_powermode_);
188
Padmanabhan Komanduruf5b7f992019-07-08 13:33:44 +0530189 value = 0;
190 Debug::Get()->GetProperty(ENABLE_ASYNC_VDS_CREATION, &value);
191 async_vds_creation_ = (value == 1);
192 DLOGI("async_vds_creation: %d", async_vds_creation_);
193
Pullakavi Srinivasf2a31852019-04-23 14:32:08 -0700194 InitSupportedDisplaySlots();
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530195 // Create primary display here. Remaining builtin displays will be created after client has set
196 // display indexes which may happen sometime before callback is registered.
197 status = CreatePrimaryDisplay();
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500198 if (status) {
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530199 Deinit();
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500200 return status;
201 }
202
Prabhanjan Kandula63a613f2017-09-14 19:29:46 -0700203 is_composer_up_ = true;
Ramakant Singh2b8f5ec2019-09-20 09:43:29 +0530204 StartServices();
Dileep Marchyad16da3e2017-05-20 01:56:21 +0530205
Pullakavi Srinivasdb2b84a2019-11-21 14:47:35 +0530206 PostInit();
207
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500208 return 0;
209}
210
Pullakavi Srinivasdb2b84a2019-11-21 14:47:35 +0530211void HWCSession::PostInit() {
212 if (null_display_mode_) {
213 return;
214 }
215
216 // Start services which need IDisplayConfig to be up.
217 // This avoids deadlock between composer and its clients.
218 auto hwc_display = hwc_display_[HWC_DISPLAY_PRIMARY];
219 hwc_display->PostInit();
220}
221
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500222int HWCSession::Deinit() {
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530223 // Destroy all connected displays
224 DestroyDisplay(&map_info_primary_);
Saurabh Shahc5c687a2017-08-24 16:30:14 -0700225
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530226 for (auto &map_info : map_info_builtin_) {
227 DestroyDisplay(&map_info);
Dileep Marchyad16da3e2017-05-20 01:56:21 +0530228 }
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530229
230 for (auto &map_info : map_info_pluggable_) {
231 DestroyDisplay(&map_info);
232 }
233
234 for (auto &map_info : map_info_virtual_) {
235 DestroyDisplay(&map_info);
236 }
Dileep Marchyad16da3e2017-05-20 01:56:21 +0530237
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500238 if (color_mgr_) {
239 color_mgr_->DestroyColorManager();
240 }
Dileep Marchyad16da3e2017-05-20 01:56:21 +0530241
Varun Arora465c1f72018-02-08 16:18:50 -0800242 if (!null_display_mode_) {
243 g_hwc_uevent_.Register(nullptr);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500244
Varun Arora465c1f72018-02-08 16:18:50 -0800245 DisplayError error = CoreInterface::DestroyCore();
246 if (error != kErrorNone) {
Dileep Marchya4cb86b42018-05-01 19:34:51 +0530247 DLOGE("Display core de-initialization failed. Error = %d", error);
Varun Arora465c1f72018-02-08 16:18:50 -0800248 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500249 }
250
251 return 0;
252}
253
Pullakavi Srinivasf2a31852019-04-23 14:32:08 -0700254void HWCSession::InitSupportedDisplaySlots() {
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530255 // Default slots:
Pullakavi Srinivasf2a31852019-04-23 14:32:08 -0700256 // Primary = 0, External = 1
257 // Additional external displays 2,3,...max_pluggable_count.
258 // Additional builtin displays max_pluggable_count + 1, max_pluggable_count + 2,...
259 // Last slots for virtual displays.
260 // Virtual display id is only for SF <--> HWC communication.
261 // It need not align with hwccomposer_defs
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530262
263 map_info_primary_.client_id = qdutils::DISPLAY_PRIMARY;
264
Pullakavi Srinivasf2a31852019-04-23 14:32:08 -0700265 if (null_display_mode_) {
Varun Arorad7d8c912019-08-16 11:45:19 -0700266 InitSupportedNullDisplaySlots();
Pullakavi Srinivasf2a31852019-04-23 14:32:08 -0700267 return;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530268 }
269
Dileep Marchya1b37f8c2020-01-20 19:14:12 +0530270 DisplayError error = CoreInterface::CreateCore(&buffer_allocator_, nullptr,
Pullakavi Srinivasf2a31852019-04-23 14:32:08 -0700271 &socket_handler_, &core_intf_);
272 if (error != kErrorNone) {
273 DLOGE("Failed to create CoreInterface");
274 return;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530275 }
276
Pullakavi Srinivasf2a31852019-04-23 14:32:08 -0700277 HWDisplayInterfaceInfo hw_disp_info = {};
278 error = core_intf_->GetFirstDisplayInterfaceType(&hw_disp_info);
279 if (error != kErrorNone) {
280 CoreInterface::DestroyCore();
281 DLOGE("Primary display type not recognized. Error = %d", error);
282 return;
283 }
284
285 int max_builtin = 0;
286 int max_pluggable = 0;
287 int max_virtual = 0;
288
289 error = core_intf_->GetMaxDisplaysSupported(kBuiltIn, &max_builtin);
290 if (error != kErrorNone) {
291 CoreInterface::DestroyCore();
292 DLOGE("Could not find maximum built-in displays supported. Error = %d", error);
293 return;
294 }
295
296 error = core_intf_->GetMaxDisplaysSupported(kPluggable, &max_pluggable);
297 if (error != kErrorNone) {
298 CoreInterface::DestroyCore();
299 DLOGE("Could not find maximum pluggable displays supported. Error = %d", error);
300 return;
301 }
302
303 error = core_intf_->GetMaxDisplaysSupported(kVirtual, &max_virtual);
304 if (error != kErrorNone) {
305 CoreInterface::DestroyCore();
306 DLOGE("Could not find maximum virtual displays supported. Error = %d", error);
307 return;
308 }
309
Pullakavi Srinivas17495f12019-09-16 20:25:20 +0530310 if (max_virtual == 0) {
311 // Check if WB using GPU is supported.
312 max_virtual += virtual_display_factory_.IsGPUColorConvertSupported() ? 1 : 0;
313 }
314
Pullakavi Srinivasf2a31852019-04-23 14:32:08 -0700315 if (kPluggable == hw_disp_info.type) {
316 // If primary is a pluggable display, we have already used one pluggable display interface.
317 max_pluggable--;
318 } else {
319 max_builtin--;
320 }
321
322 // Init slots in accordance to h/w capability.
323 uint32_t disp_count = UINT32(std::min(max_pluggable, HWCCallbacks::kNumPluggable));
324 hwc2_display_t base_id = qdutils::DISPLAY_EXTERNAL;
325 map_info_pluggable_.resize(disp_count);
326 for (auto &map_info : map_info_pluggable_) {
327 map_info.client_id = base_id++;
328 }
329
330 disp_count = UINT32(std::min(max_builtin, HWCCallbacks::kNumBuiltIn));
331 map_info_builtin_.resize(disp_count);
332 for (auto &map_info : map_info_builtin_) {
333 map_info.client_id = base_id++;
334 }
335
336 disp_count = UINT32(std::min(max_virtual, HWCCallbacks::kNumVirtual));
337 map_info_virtual_.resize(disp_count);
338 for (auto &map_info : map_info_virtual_) {
339 map_info.client_id = base_id++;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530340 }
Archit Srivastavafe060f72019-05-27 15:51:58 +0530341
342 // resize HDR supported map to total number of displays.
343 is_hdr_display_.resize(UINT32(base_id));
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +0530344
345 if (!async_powermode_) {
346 return;
347 }
348
349 int start_index = HWCCallbacks::kNumRealDisplays;
350 std::vector<DisplayMapInfo> map_info = {map_info_primary_};
351 std::copy(map_info_builtin_.begin(), map_info_builtin_.end(), std::back_inserter(map_info));
352 std::copy(map_info_pluggable_.begin(), map_info_pluggable_.end(), std::back_inserter(map_info));
353 for (auto &map : map_info) {
354 DLOGI("Display Pairs: map.client_id: %d, start_index: %d", map.client_id, start_index);
355 map_hwc_display_.insert(std::make_pair(map.client_id, start_index++));
356 }
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530357}
358
Varun Arorad7d8c912019-08-16 11:45:19 -0700359void HWCSession::InitSupportedNullDisplaySlots() {
360 if (!null_display_mode_) {
361 DLOGE("Should only be invoked during null display");
362 return;
363 }
364
365 map_info_primary_.client_id = 0;
366 // Resize HDR supported map to total number of displays
367 is_hdr_display_.resize(1);
368
369 if (!async_powermode_) {
370 return;
371 }
372
373 DLOGI("Display Pairs: map.client_id: %d, start_index: %d", map_info_primary_.client_id,
374 HWCCallbacks::kNumRealDisplays);
375 map_hwc_display_.insert(std::make_pair(map_info_primary_.client_id,
376 HWCCallbacks::kNumRealDisplays));
377}
378
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530379int HWCSession::GetDisplayIndex(int dpy) {
380 DisplayMapInfo *map_info = nullptr;
381 switch (dpy) {
382 case qdutils::DISPLAY_PRIMARY:
383 map_info = &map_info_primary_;
384 break;
385 case qdutils::DISPLAY_EXTERNAL:
386 map_info = map_info_pluggable_.size() ? &map_info_pluggable_[0] : nullptr;
387 break;
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +0530388 case qdutils::DISPLAY_EXTERNAL_2:
389 map_info = (map_info_pluggable_.size() > 1) ? &map_info_pluggable_[1] : nullptr;
390 break;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530391 case qdutils::DISPLAY_VIRTUAL:
392 map_info = map_info_virtual_.size() ? &map_info_virtual_[0] : nullptr;
393 break;
Dileep Marchya35e27532018-06-29 10:50:16 +0530394 case qdutils::DISPLAY_BUILTIN_2:
395 map_info = map_info_builtin_.size() ? &map_info_builtin_[0] : nullptr;
396 break;
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +0530397 default:
398 DLOGW("Unknown display %d.", dpy);
399 break;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530400 }
401
402 if (!map_info) {
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +0530403 DLOGE("Display index not found for display %d.", dpy);
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530404 return -1;
405 }
406
407 return INT(map_info->client_id);
408}
409
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530410void HWCSession::GetCapabilities(uint32_t *outCount, int32_t *outCapabilities) {
Ramkumar Radhakrishnan82af7e92017-09-21 14:29:42 -0700411 if (!outCount) {
412 return;
413 }
414
Sushil Chauhan409e8442017-06-12 17:43:25 -0700415 int value = 0;
416 bool disable_skip_validate = false;
Uday Kiran Pichika5e656b22018-05-15 18:48:24 +0530417 if (Debug::Get()->GetProperty(DISABLE_SKIP_VALIDATE_PROP, &value) == kErrorNone) {
Sushil Chauhan409e8442017-06-12 17:43:25 -0700418 disable_skip_validate = (value == 1);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500419 }
Sushil Chauhan409e8442017-06-12 17:43:25 -0700420 uint32_t count = 1 + (disable_skip_validate ? 0 : 1);
421
422 if (outCapabilities != nullptr && (*outCount >= count)) {
423 outCapabilities[0] = HWC2_CAPABILITY_SKIP_CLIENT_COLOR_TRANSFORM;
424 if (!disable_skip_validate) {
425 outCapabilities[1] = HWC2_CAPABILITY_SKIP_VALIDATE;
426 }
427 }
428 *outCount = count;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500429}
430
431template <typename PFN, typename T>
432static hwc2_function_pointer_t AsFP(T function) {
433 static_assert(std::is_same<PFN, T>::value, "Incompatible function pointer");
434 return reinterpret_cast<hwc2_function_pointer_t>(function);
435}
436
437// HWC2 functions returned in GetFunction
438// Defined in the same order as in the HWC2 header
439
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530440int32_t HWCSession::AcceptDisplayChanges(hwc2_display_t display) {
441 return CallDisplayFunction(display, &HWCDisplay::AcceptDisplayChanges);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500442}
443
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530444int32_t HWCSession::CreateLayer(hwc2_display_t display,
Naseer Ahmedaddcabf2016-05-04 15:49:29 -0400445 hwc2_layer_t *out_layer_id) {
Ramkumar Radhakrishnan82af7e92017-09-21 14:29:42 -0700446 if (!out_layer_id) {
447 return HWC2_ERROR_BAD_PARAMETER;
448 }
Saurabh Shah04f932b2017-10-12 18:47:45 -0700449
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530450 return CallDisplayFunction(display, &HWCDisplay::CreateLayer, out_layer_id);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500451}
452
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530453int32_t HWCSession::CreateVirtualDisplay(uint32_t width, uint32_t height, int32_t *format,
454 hwc2_display_t *out_display_id) {
Naseer Ahmed42fc8d12016-05-11 18:33:57 -0400455 // TODO(user): Handle concurrency with HDMI
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500456
Ramkumar Radhakrishnan82af7e92017-09-21 14:29:42 -0700457 if (!out_display_id || !width || !height || !format) {
458 return HWC2_ERROR_BAD_PARAMETER;
459 }
460
Varun Arorad7d8c912019-08-16 11:45:19 -0700461 if (null_display_mode_) {
462 return 0;
463 }
464
Padmanabhan Komanduruf5b7f992019-07-08 13:33:44 +0530465 if (async_vds_creation_ && virtual_id_ != HWCCallbacks::kNumDisplays) {
466 *out_display_id = virtual_id_;
467 return HWC2_ERROR_NONE;
468 }
469
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530470 auto status = CreateVirtualDisplayObj(width, height, format, out_display_id);
Naseer Ahmed23633942016-07-13 19:46:56 -0400471 if (status == HWC2::Error::None) {
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530472 DLOGI("Created virtual display id:% " PRIu64 ", res: %dx%d", *out_display_id, width, height);
Naseer Ahmed23633942016-07-13 19:46:56 -0400473 } else {
474 DLOGE("Failed to create virtual display: %s", to_string(status).c_str());
475 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500476 return INT32(status);
477}
478
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530479int32_t HWCSession::DestroyLayer(hwc2_display_t display, hwc2_layer_t layer) {
480 return CallDisplayFunction(display, &HWCDisplay::DestroyLayer, layer);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500481}
482
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530483int32_t HWCSession::DestroyVirtualDisplay(hwc2_display_t display) {
484 if (display >= HWCCallbacks::kNumDisplays) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500485 return HWC2_ERROR_BAD_DISPLAY;
486 }
487
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530488 hwc2_display_t active_builtin_disp_id = GetActiveBuiltinDisplay();
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500489
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -0700490 if (active_builtin_disp_id < HWCCallbacks::kNumDisplays) {
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -0800491 Locker::ScopeLock lock_a(locker_[active_builtin_disp_id]);
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +0530492 std::bitset<kSecureMax> secure_sessions = 0;
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530493 hwc_display_[active_builtin_disp_id]->GetActiveSecureSession(&secure_sessions);
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -0700494 if (secure_sessions.any()) {
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -0800495 DLOGW("Secure session is active, defer destruction of virtual display id:%" PRIu64, display);
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530496 destroy_virtual_disp_pending_ = true;
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -0700497 return HWC2_ERROR_NONE;
498 }
499 }
500
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530501 for (auto &map_info : map_info_virtual_) {
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530502 if (map_info.client_id == display) {
503 DLOGI("Destroying virtual display id:%" PRIu64, display);
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530504 DestroyDisplay(&map_info);
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530505 break;
506 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500507 }
Padmanabhan Komanduruf5b7f992019-07-08 13:33:44 +0530508 virtual_id_ = HWCCallbacks::kNumDisplays;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530509
510 return HWC2_ERROR_NONE;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500511}
512
Pullakavi Srinivasa3c292c2019-12-01 13:27:50 +0530513int32_t HWCSession::GetVirtualDisplayId() {
514 HWDisplaysInfo hw_displays_info = {};
515 core_intf_->GetDisplaysStatus(&hw_displays_info);
516 for (auto &iter : hw_displays_info) {
517 auto &info = iter.second;
518 if (info.display_type != kVirtual) {
519 continue;
520 }
521
522 return info.display_id;
523 }
524
525 return -1;
526}
527
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530528void HWCSession::Dump(uint32_t *out_size, char *out_buffer) {
529 if (!out_size) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500530 return;
531 }
Saurabh Shah9c224a32017-11-09 12:17:44 -0800532
Mathew Joseph Karimpanal50dfb992019-09-30 12:04:11 +0530533 const size_t max_dump_size = 16384; // 16 kB
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500534
535 if (out_buffer == nullptr) {
Naseer Ahmed66d5b492017-02-23 20:30:17 -0500536 *out_size = max_dump_size;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500537 } else {
Dileep Marchya58928122020-01-28 12:16:51 +0530538 std::ostringstream os;
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +0530539 for (int id = 0; id < HWCCallbacks::kNumRealDisplays; id++) {
Saurabh Shah04f932b2017-10-12 18:47:45 -0700540 SCOPE_LOCK(locker_[id]);
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530541 if (hwc_display_[id]) {
Dileep Marchya58928122020-01-28 12:16:51 +0530542 hwc_display_[id]->Dump(&os);
Naseer Ahmed0f5b51c2016-06-22 19:39:38 -0400543 }
544 }
Dileep Marchya58928122020-01-28 12:16:51 +0530545 Fence::Dump(&os);
546
547 std::string s = os.str();
Naseer Ahmed66d5b492017-02-23 20:30:17 -0500548 auto copied = s.copy(out_buffer, std::min(s.size(), max_dump_size), 0);
549 *out_size = UINT32(copied);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500550 }
551}
552
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530553uint32_t HWCSession::GetMaxVirtualDisplayCount() {
Pullakavi Srinivasa3c292c2019-12-01 13:27:50 +0530554 return map_info_virtual_.size();
Varun Aroraad2719a2018-10-25 10:13:53 -0700555}
556
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530557int32_t HWCSession::GetActiveConfig(hwc2_display_t display, hwc2_config_t *out_config) {
558 return CallDisplayFunction(display, &HWCDisplay::GetActiveConfig, out_config);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500559}
560
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530561int32_t HWCSession::GetChangedCompositionTypes(hwc2_display_t display, uint32_t *out_num_elements,
562 hwc2_layer_t *out_layers, int32_t *out_types) {
Ramkumar Radhakrishnan82af7e92017-09-21 14:29:42 -0700563 // null_ptr check only for out_num_elements, as out_layers and out_types can be null.
564 if (!out_num_elements) {
565 return HWC2_ERROR_BAD_PARAMETER;
566 }
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530567 return CallDisplayFunction(display, &HWCDisplay::GetChangedCompositionTypes, out_num_elements,
568 out_layers, out_types);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500569}
570
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530571int32_t HWCSession::GetClientTargetSupport(hwc2_display_t display, uint32_t width, uint32_t height,
572 int32_t format, int32_t dataspace) {
573 return CallDisplayFunction(display, &HWCDisplay::GetClientTargetSupport, width, height, format,
574 dataspace);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500575}
576
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530577int32_t HWCSession::GetColorModes(hwc2_display_t display, uint32_t *out_num_modes,
578 int32_t /*ColorMode*/ *int_out_modes) {
Naseer Ahmede7a77982018-06-04 10:56:04 -0400579 auto out_modes = reinterpret_cast<ColorMode *>(int_out_modes);
Arun Kumar K.R0e10fd22017-10-11 14:42:11 +0530580 if (out_num_modes == nullptr) {
581 return HWC2_ERROR_BAD_PARAMETER;
582 }
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530583 return CallDisplayFunction(display, &HWCDisplay::GetColorModes, out_num_modes, out_modes);
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700584}
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500585
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530586int32_t HWCSession::GetRenderIntents(hwc2_display_t display, int32_t /*ColorMode*/ int_mode,
587 uint32_t *out_num_intents,
588 int32_t /*RenderIntent*/ *int_out_intents) {
Naseer Ahmede7a77982018-06-04 10:56:04 -0400589 auto mode = static_cast<ColorMode>(int_mode);
590 auto out_intents = reinterpret_cast<RenderIntent *>(int_out_intents);
591 if (out_num_intents == nullptr) {
592 return HWC2_ERROR_BAD_PARAMETER;
593 }
Gurpreet Singh Dhamiec0fa512018-08-29 16:51:26 -0400594
Gousemoodhin Nadaf0e566872019-05-13 16:14:54 +0530595 if (mode < ColorMode::NATIVE || mode > ColorMode::DISPLAY_BT2020) {
Gurpreet Singh Dhamiec0fa512018-08-29 16:51:26 -0400596 DLOGE("Invalid ColorMode: %d", mode);
597 return HWC2_ERROR_BAD_PARAMETER;
598 }
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530599 return CallDisplayFunction(display, &HWCDisplay::GetRenderIntents, mode, out_num_intents,
600 out_intents);
Naseer Ahmede7a77982018-06-04 10:56:04 -0400601}
602
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530603int32_t HWCSession::GetDataspaceSaturationMatrix(int32_t /*Dataspace*/ int_dataspace,
604 float *out_matrix) {
Naseer Ahmede7a77982018-06-04 10:56:04 -0400605 auto dataspace = static_cast<Dataspace>(int_dataspace);
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530606 if (out_matrix == nullptr || dataspace != Dataspace::SRGB_LINEAR) {
Naseer Ahmede7a77982018-06-04 10:56:04 -0400607 return HWC2_ERROR_BAD_PARAMETER;
608 }
609 // We only have the matrix for sRGB
610 float saturation_matrix[kDataspaceSaturationMatrixCount] = { 1.0, 0.0, 0.0, 0.0, \
611 0.0, 1.0, 0.0, 0.0, \
612 0.0, 0.0, 1.0, 0.0, \
613 0.0, 0.0, 0.0, 1.0 };
614
Naseer Ahmede7a77982018-06-04 10:56:04 -0400615 for (int32_t i = 0; i < kDataspaceSaturationMatrixCount; i += 4) {
616 DLOGD("%f %f %f %f", saturation_matrix[i], saturation_matrix[i + 1], saturation_matrix[i + 2],
617 saturation_matrix[i + 3]);
618 }
619 for (uint32_t i = 0; i < kDataspaceSaturationMatrixCount; i++) {
620 out_matrix[i] = saturation_matrix[i];
621 }
622 return HWC2_ERROR_NONE;
623}
624
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530625int32_t HWCSession::GetPerFrameMetadataKeys(hwc2_display_t display, uint32_t *out_num_keys,
626 int32_t *int_out_keys) {
Naseer Ahmeda6782e42018-06-04 10:56:04 -0400627 auto out_keys = reinterpret_cast<PerFrameMetadataKey *>(int_out_keys);
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530628 return CallDisplayFunction(display, &HWCDisplay::GetPerFrameMetadataKeys, out_num_keys,
629 out_keys);
Naseer Ahmeda6782e42018-06-04 10:56:04 -0400630}
631
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530632int32_t HWCSession::SetLayerPerFrameMetadata(hwc2_display_t display, hwc2_layer_t layer,
633 uint32_t num_elements, const int32_t *int_keys,
634 const float *metadata) {
Naseer Ahmeda6782e42018-06-04 10:56:04 -0400635 auto keys = reinterpret_cast<const PerFrameMetadataKey *>(int_keys);
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530636 return CallLayerFunction(display, layer, &HWCLayer::SetLayerPerFrameMetadata, num_elements,
637 keys, metadata);
Naseer Ahmeda6782e42018-06-04 10:56:04 -0400638}
639
Gousemoodhin Nadaf4b096d02019-10-24 15:39:59 +0530640int32_t HWCSession:: SetLayerPerFrameMetadataBlobs(hwc2_display_t display,
641 hwc2_layer_t layer, uint32_t num_elements,
642 const int32_t *int_keys, const uint32_t *sizes,
643 const uint8_t *metadata) {
644 auto keys = reinterpret_cast<const PerFrameMetadataKey *>(int_keys);
645 return CallLayerFunction(display, layer, &HWCLayer::SetLayerPerFrameMetadataBlobs,
646 num_elements, keys, sizes, metadata);
647}
648
Lakshmi Narayana Kalavala25fe12c2019-12-11 20:44:55 -0800649int32_t HWCSession::SetDisplayedContentSamplingEnabled(hwc2_display_t display, int32_t enabled,
650 uint8_t component_mask,
651 uint64_t max_frames) {
652 static constexpr int32_t validComponentMask = HWC2_FORMAT_COMPONENT_0 | HWC2_FORMAT_COMPONENT_1 |
653 HWC2_FORMAT_COMPONENT_2 | HWC2_FORMAT_COMPONENT_3;
654 if (component_mask & ~validComponentMask)
655 return HWC2_ERROR_BAD_PARAMETER;
656 return CallDisplayFunction(display, &HWCDisplay::SetDisplayedContentSamplingEnabled, enabled,
657 component_mask, max_frames);
658}
659
660int32_t HWCSession::GetDisplayedContentSamplingAttributes(hwc2_display_t display, int32_t *format,
661 int32_t *dataspace,
662 uint8_t *supported_components) {
663 return CallDisplayFunction(display, &HWCDisplay::GetDisplayedContentSamplingAttributes, format,
664 dataspace, supported_components);
665}
666
667int32_t HWCSession::GetDisplayedContentSample(hwc2_display_t display, uint64_t max_frames,
668 uint64_t timestamp, uint64_t *numFrames,
669 int32_t samples_size[NUM_HISTOGRAM_COLOR_COMPONENTS],
670 uint64_t *samples[NUM_HISTOGRAM_COLOR_COMPONENTS]) {
671 return CallDisplayFunction(display, &HWCDisplay::GetDisplayedContentSample, max_frames, timestamp,
672 numFrames, samples_size, samples);
673}
674
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530675int32_t HWCSession::GetDisplayAttribute(hwc2_display_t display, hwc2_config_t config,
Naseer Ahmed8584a0f2020-02-19 22:23:30 -0500676 HwcAttribute attribute, int32_t *out_value) {
677 if (out_value == nullptr) {
Arun Kumar K.R0e10fd22017-10-11 14:42:11 +0530678 return HWC2_ERROR_BAD_PARAMETER;
679 }
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530680 return CallDisplayFunction(display, &HWCDisplay::GetDisplayAttribute, config, attribute,
681 out_value);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500682}
683
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530684int32_t HWCSession::GetDisplayConfigs(hwc2_display_t display, uint32_t *out_num_configs,
685 hwc2_config_t *out_configs) {
686 return CallDisplayFunction(display, &HWCDisplay::GetDisplayConfigs, out_num_configs,
687 out_configs);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500688}
689
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530690int32_t HWCSession::GetDisplayName(hwc2_display_t display, uint32_t *out_size, char *out_name) {
691 return CallDisplayFunction(display, &HWCDisplay::GetDisplayName, out_size, out_name);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500692}
693
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530694int32_t HWCSession::GetDisplayRequests(hwc2_display_t display, int32_t *out_display_requests,
695 uint32_t *out_num_elements, hwc2_layer_t *out_layers,
696 int32_t *out_layer_requests) {
697 return CallDisplayFunction(display, &HWCDisplay::GetDisplayRequests, out_display_requests,
698 out_num_elements, out_layers, out_layer_requests);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500699}
700
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530701int32_t HWCSession::GetDisplayType(hwc2_display_t display, int32_t *out_type) {
702 return CallDisplayFunction(display, &HWCDisplay::GetDisplayType, out_type);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500703}
704
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500705
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530706int32_t HWCSession::GetHdrCapabilities(hwc2_display_t display, uint32_t* out_num_types,
707 int32_t* out_types, float* out_max_luminance,
708 float* out_max_average_luminance,
709 float* out_min_luminance) {
710 return CallDisplayFunction(display, &HWCDisplay::GetHdrCapabilities, out_num_types, out_types,
711 out_max_luminance, out_max_average_luminance, out_min_luminance);
Naseer Ahmed77907722016-05-03 18:27:54 -0400712}
713
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500714
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530715int32_t HWCSession::GetReleaseFences(hwc2_display_t display, uint32_t *out_num_elements,
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530716 hwc2_layer_t *out_layers,
717 std::vector<shared_ptr<Fence>> *out_fences) {
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530718 return CallDisplayFunction(display, &HWCDisplay::GetReleaseFences, out_num_elements, out_layers,
719 out_fences);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500720}
721
Padmanabhan Komanduru6693fcb2019-11-22 21:06:52 +0530722void HWCSession::PerformQsyncCallback(hwc2_display_t display) {
Padmanabhan Komanduru9e971622020-02-10 13:09:34 +0530723 std::shared_ptr<DisplayConfig::ConfigCallback> callback = qsync_callback_.lock();
724 if (!callback) {
Padmanabhan Komanduru6693fcb2019-11-22 21:06:52 +0530725 return;
726 }
727
728 bool qsync_enabled = 0;
729 int32_t refresh_rate = 0, qsync_refresh_rate = 0;
730 if (hwc_display_[display]->IsQsyncCallbackNeeded(&qsync_enabled,
731 &refresh_rate, &qsync_refresh_rate)) {
Padmanabhan Komanduru9e971622020-02-10 13:09:34 +0530732 callback->NotifyQsyncChange(qsync_enabled, refresh_rate, qsync_refresh_rate);
Padmanabhan Komanduru6693fcb2019-11-22 21:06:52 +0530733 }
734}
735
Dileep Marchyaad668432019-12-02 10:44:52 +0530736int32_t HWCSession::PresentDisplay(hwc2_display_t display, shared_ptr<Fence> *out_retire_fence) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500737 auto status = HWC2::Error::BadDisplay;
Baldev Sahua5e48362017-08-31 15:36:44 +0530738 DTRACE_SCOPED();
Saurabh Shah04f932b2017-10-12 18:47:45 -0700739
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530740 if (display >= HWCCallbacks::kNumDisplays) {
741 DLOGW("Invalid Display : display = %" PRIu64, display);
Saurabh Shah04f932b2017-10-12 18:47:45 -0700742 return HWC2_ERROR_BAD_DISPLAY;
743 }
744
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530745 HandleSecureSession();
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +0530746
747
748 hwc2_display_t target_display = display;
749
Baldev Sahua5e48362017-08-31 15:36:44 +0530750 {
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +0530751 SCOPE_LOCK(power_state_[display]);
752 if (power_state_transition_[display]) {
753 // Route all interactions with client to dummy display.
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +0530754 target_display = map_hwc_display_.find(display)->second;
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +0530755 }
756 }
757
758 {
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +0530759 SEQUENCE_EXIT_SCOPE_LOCK(locker_[target_display]);
760 if (!hwc_display_[target_display]) {
761 DLOGW("Removed Display : display = %" PRIu64, target_display);
Baldev Sahua5e48362017-08-31 15:36:44 +0530762 return HWC2_ERROR_BAD_DISPLAY;
763 }
764
765 if (out_retire_fence == nullptr) {
766 return HWC2_ERROR_BAD_PARAMETER;
767 }
768
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +0530769 if (pending_power_mode_[display]) {
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +0530770 status = HWC2::Error::None;
Gurpreet Singh Dhamia9478232018-08-21 12:19:47 -0400771 } else {
Sushil Chauhandbd8db42020-02-28 13:07:48 -0800772 hwc_display_[target_display]->ProcessActiveConfigChange();
Dileep Marchyaad668432019-12-02 10:44:52 +0530773 status = PresentDisplayInternal(target_display);
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -0700774 if (status == HWC2::Error::None) {
775 // Check if hwc's refresh trigger is getting exercised.
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530776 if (callbacks_.NeedsRefresh(display)) {
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +0530777 hwc_display_[target_display]->SetPendingRefresh();
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530778 callbacks_.ResetRefresh(display);
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -0700779 }
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +0530780 status = hwc_display_[target_display]->Present(out_retire_fence);
Padmanabhan Komanduru6693fcb2019-11-22 21:06:52 +0530781 if (status == HWC2::Error::None) {
782 PerformQsyncCallback(target_display);
783 }
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -0700784 }
Baldev Sahua5e48362017-08-31 15:36:44 +0530785 }
786 }
787
Saurabh Shah04f932b2017-10-12 18:47:45 -0700788 if (status != HWC2::Error::None && status != HWC2::Error::NotValidated) {
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +0530789 SEQUENCE_CANCEL_SCOPE_LOCK(locker_[target_display]);
Saurabh Shah04f932b2017-10-12 18:47:45 -0700790 }
791
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +0530792 HandlePendingPowerMode(display, *out_retire_fence);
793 HandlePendingHotplug(display, *out_retire_fence);
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530794 HandlePendingRefresh();
Dileep Marchyacb2a3a92019-06-21 17:30:50 +0530795 cwb_.PresentDisplayDone(display);
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +0530796 display_ready_.set(UINT32(display));
Padmanabhan Komanduruf5b7f992019-07-08 13:33:44 +0530797 {
798 std::unique_lock<std::mutex> caller_lock(hotplug_mutex_);
799 hotplug_cv_.notify_one();
800 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500801
802 return INT32(status);
803}
804
Padmanabhan Komanduru1649f222019-03-05 11:25:00 +0530805void HWCSession::HandlePendingRefresh() {
806 if (pending_refresh_.none()) {
807 return;
808 }
809
810 for (size_t i = 0; i < pending_refresh_.size(); i++) {
811 if (pending_refresh_.test(i)) {
Varun Aroradff7d8d2019-08-05 18:12:43 -0700812 callbacks_.Refresh(i);
Padmanabhan Komanduru1649f222019-03-05 11:25:00 +0530813 }
814 break;
815 }
816
817 pending_refresh_.reset();
818}
819
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530820void HWCSession::RegisterCallback(int32_t descriptor, hwc2_callback_data_t callback_data,
821 hwc2_function_pointer_t pointer) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500822 auto desc = static_cast<HWC2::Callback>(descriptor);
Varun Arora2daeb072019-08-06 15:09:28 -0700823
824 // Detect if client died and now is back
825 bool already_connected = false;
826 vector<hwc2_display_t> pending_hotplugs;
Gousemoodhin Nadaf0db182e2019-09-05 23:02:31 +0530827 if (descriptor == HWC2_CALLBACK_HOTPLUG && pointer) {
Varun Arora2daeb072019-08-06 15:09:28 -0700828 already_connected = callbacks_.IsClientConnected();
829 if (already_connected) {
830 for (auto& map_info : map_info_builtin_) {
831 SCOPE_LOCK(locker_[map_info.client_id]);
832 if (hwc_display_[map_info.client_id]) {
833 pending_hotplugs.push_back(static_cast<hwc2_display_t>(map_info.client_id));
834 }
835 }
836 for (auto& map_info : map_info_pluggable_) {
837 SCOPE_LOCK(locker_[map_info.client_id]);
838 if (hwc_display_[map_info.client_id]) {
839 pending_hotplugs.push_back(static_cast<hwc2_display_t>(map_info.client_id));
840 }
841 }
842 }
843 }
844
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530845 auto error = callbacks_.Register(desc, callback_data, pointer);
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530846 if (error != HWC2::Error::None) {
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530847 return;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +0530848 }
849
Varun Aroradff7d8d2019-08-05 18:12:43 -0700850 DLOGI("%s callback: %s", pointer ? "Registering" : "Deregistering", to_string(desc).c_str());
Gousemoodhin Nadaf0db182e2019-09-05 23:02:31 +0530851 if (descriptor == HWC2_CALLBACK_HOTPLUG && pointer) {
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530852 if (hwc_display_[HWC_DISPLAY_PRIMARY]) {
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +0530853 DLOGI("Hotplugging primary...");
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530854 callbacks_.Hotplug(HWC_DISPLAY_PRIMARY, HWC2::Connection::Connected);
Dileep Marchyad16da3e2017-05-20 01:56:21 +0530855 }
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +0530856 // Create displays since they should now have their final display indices set.
857 DLOGI("Handling built-in displays...");
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530858 if (HandleBuiltInDisplays()) {
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +0530859 DLOGW("Failed handling built-in displays.");
860 }
861 DLOGI("Handling pluggable displays...");
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530862 int32_t err = HandlePluggableDisplays(false);
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +0530863 if (err) {
864 DLOGW("All displays could not be created. Error %d '%s'. Hotplug handling %s.", err,
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +0530865 strerror(abs(err)), pending_hotplug_event_ == kHotPlugEvent ? "deferred" :
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +0530866 "dropped");
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +0530867 }
Varun Arora2daeb072019-08-06 15:09:28 -0700868
869 // If previously registered, call hotplug for all connected displays to refresh
870 if (already_connected) {
Gousemoodhin Nadaf0db182e2019-09-05 23:02:31 +0530871 std::vector<hwc2_display_t> updated_pending_hotplugs;
Varun Arora2daeb072019-08-06 15:09:28 -0700872 for (auto client_id : pending_hotplugs) {
873 SCOPE_LOCK(locker_[client_id]);
Gousemoodhin Nadaf0db182e2019-09-05 23:02:31 +0530874 // check if the display is unregistered
Varun Arora2daeb072019-08-06 15:09:28 -0700875 if (hwc_display_[client_id]) {
Gousemoodhin Nadaf0db182e2019-09-05 23:02:31 +0530876 updated_pending_hotplugs.push_back(client_id);
Varun Arora2daeb072019-08-06 15:09:28 -0700877 }
878 }
Gousemoodhin Nadaf0db182e2019-09-05 23:02:31 +0530879 for (auto client_id : updated_pending_hotplugs) {
880 DLOGI("Re-hotplug display connected: client id = %d", client_id);
881 callbacks_.Hotplug(client_id, HWC2::Connection::Connected);
882 }
Varun Arora2daeb072019-08-06 15:09:28 -0700883 }
Rajavenu Kyatham59b67862019-11-26 21:02:56 +0530884 }
885
886 if (descriptor == HWC2_CALLBACK_HOTPLUG) {
Pullakavi Srinivas2d55f3a2019-07-16 14:49:06 +0530887 client_connected_ = !!pointer;
888 // Notfify all displays.
889 NotifyClientStatus(client_connected_);
Dileep Marchyad16da3e2017-05-20 01:56:21 +0530890 }
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530891 need_invalidate_ = false;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500892}
893
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530894int32_t HWCSession::SetActiveConfig(hwc2_display_t display, hwc2_config_t config) {
895 return CallDisplayFunction(display, &HWCDisplay::SetActiveConfig, config);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500896}
897
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530898int32_t HWCSession::SetClientTarget(hwc2_display_t display, buffer_handle_t target,
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530899 const shared_ptr<Fence> acquire_fence, int32_t dataspace,
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530900 hwc_region_t damage) {
901 return CallDisplayFunction(display, &HWCDisplay::SetClientTarget, target, acquire_fence,
902 dataspace, damage);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500903}
904
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530905int32_t HWCSession::SetColorMode(hwc2_display_t display, int32_t /*ColorMode*/ int_mode) {
Naseer Ahmede7a77982018-06-04 10:56:04 -0400906 auto mode = static_cast<ColorMode>(int_mode);
Gousemoodhin Nadaf0e566872019-05-13 16:14:54 +0530907 if (mode < ColorMode::NATIVE || mode > ColorMode::DISPLAY_BT2020) {
Saurabh Dubey25069372017-10-10 14:21:22 +0530908 return HWC2_ERROR_BAD_PARAMETER;
909 }
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530910 return CallDisplayFunction(display, &HWCDisplay::SetColorMode, mode);
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700911}
912
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530913int32_t HWCSession::SetColorModeWithRenderIntent(hwc2_display_t display,
Naseer Ahmede7a77982018-06-04 10:56:04 -0400914 int32_t /*ColorMode*/ int_mode,
915 int32_t /*RenderIntent*/ int_render_intent) {
916 auto mode = static_cast<ColorMode>(int_mode);
Gousemoodhin Nadaf0e566872019-05-13 16:14:54 +0530917 if (mode < ColorMode::NATIVE || mode > ColorMode::DISPLAY_BT2020) {
Naseer Ahmede7a77982018-06-04 10:56:04 -0400918 return HWC2_ERROR_BAD_PARAMETER;
919 }
Xu Yang47f44092019-02-13 14:09:53 +0800920
921 if ((int_render_intent < 0) || (int_render_intent > MAX_EXTENDED_RENDER_INTENT)) {
922 DLOGE("Invalid RenderIntent: %d", int_render_intent);
Gurpreet Singh Dhamiec0fa512018-08-29 16:51:26 -0400923 return HWC2_ERROR_BAD_PARAMETER;
924 }
Xu Yang47f44092019-02-13 14:09:53 +0800925
926 auto render_intent = static_cast<RenderIntent>(int_render_intent);
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530927 return CallDisplayFunction(display, &HWCDisplay::SetColorModeWithRenderIntent, mode,
928 render_intent);
Naseer Ahmede7a77982018-06-04 10:56:04 -0400929}
930
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530931int32_t HWCSession::SetColorTransform(hwc2_display_t display, const float *matrix,
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700932 int32_t /*android_color_transform_t*/ hint) {
Saurabh Dubey25069372017-10-10 14:21:22 +0530933 if (!matrix || hint < HAL_COLOR_TRANSFORM_IDENTITY ||
934 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA) {
935 return HWC2_ERROR_BAD_PARAMETER;
936 }
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700937 android_color_transform_t transform_hint = static_cast<android_color_transform_t>(hint);
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530938 return CallDisplayFunction(display, &HWCDisplay::SetColorTransform, matrix, transform_hint);
Arun Kumar K.R29cd6582016-05-10 19:12:45 -0700939}
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500940
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530941int32_t HWCSession::SetCursorPosition(hwc2_display_t display, hwc2_layer_t layer, int32_t x,
942 int32_t y) {
Pullakavi Srinivas68a166c2017-05-24 12:17:07 +0530943 auto status = INT32(HWC2::Error::None);
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530944 status = CallDisplayFunction(display, &HWCDisplay::SetCursorPosition, layer, x, y);
Pullakavi Srinivas68a166c2017-05-24 12:17:07 +0530945 if (status == INT32(HWC2::Error::None)) {
946 // Update cursor position
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530947 CallLayerFunction(display, layer, &HWCLayer::SetCursorPosition, x, y);
Pullakavi Srinivas68a166c2017-05-24 12:17:07 +0530948 }
949 return status;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500950}
951
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530952int32_t HWCSession::SetLayerBlendMode(hwc2_display_t display, hwc2_layer_t layer,
953 int32_t int_mode) {
Saurabh Dubey25069372017-10-10 14:21:22 +0530954 if (int_mode < HWC2_BLEND_MODE_INVALID || int_mode > HWC2_BLEND_MODE_COVERAGE) {
955 return HWC2_ERROR_BAD_PARAMETER;
956 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500957 auto mode = static_cast<HWC2::BlendMode>(int_mode);
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530958 return CallLayerFunction(display, layer, &HWCLayer::SetLayerBlendMode, mode);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500959}
960
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530961int32_t HWCSession::SetLayerBuffer(hwc2_display_t display, hwc2_layer_t layer,
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +0530962 buffer_handle_t buffer,
963 const shared_ptr<Fence> &acquire_fence) {
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530964 return CallLayerFunction(display, layer, &HWCLayer::SetLayerBuffer, buffer, acquire_fence);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500965}
966
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530967int32_t HWCSession::SetLayerColor(hwc2_display_t display, hwc2_layer_t layer, hwc_color_t color) {
968 return CallLayerFunction(display, layer, &HWCLayer::SetLayerColor, color);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500969}
970
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530971int32_t HWCSession::SetLayerCompositionType(hwc2_display_t display, hwc2_layer_t layer,
972 int32_t int_type) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500973 auto type = static_cast<HWC2::Composition>(int_type);
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530974 return CallLayerFunction(display, layer, &HWCLayer::SetLayerCompositionType, type);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500975}
976
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530977int32_t HWCSession::SetLayerDataspace(hwc2_display_t display, hwc2_layer_t layer,
978 int32_t dataspace) {
979 return CallLayerFunction(display, layer, &HWCLayer::SetLayerDataspace, dataspace);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500980}
981
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530982int32_t HWCSession::SetLayerDisplayFrame(hwc2_display_t display, hwc2_layer_t layer,
983 hwc_rect_t frame) {
984 return CallLayerFunction(display, layer, &HWCLayer::SetLayerDisplayFrame, frame);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500985}
986
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530987int32_t HWCSession::SetLayerPlaneAlpha(hwc2_display_t display, hwc2_layer_t layer, float alpha) {
988 return CallLayerFunction(display, layer, &HWCLayer::SetLayerPlaneAlpha, alpha);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500989}
990
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530991int32_t HWCSession::SetLayerSourceCrop(hwc2_display_t display, hwc2_layer_t layer,
992 hwc_frect_t crop) {
993 return HWCSession::CallLayerFunction(display, layer, &HWCLayer::SetLayerSourceCrop, crop);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500994}
995
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +0530996int32_t HWCSession::SetLayerSurfaceDamage(hwc2_display_t display, hwc2_layer_t layer,
997 hwc_region_t damage) {
998 return CallLayerFunction(display, layer, &HWCLayer::SetLayerSurfaceDamage, damage);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -0500999}
1000
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301001int32_t HWCSession::SetLayerTransform(hwc2_display_t display, hwc2_layer_t layer,
1002 int32_t int_transform) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001003 auto transform = static_cast<HWC2::Transform>(int_transform);
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301004 return CallLayerFunction(display, layer, &HWCLayer::SetLayerTransform, transform);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001005}
1006
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301007int32_t HWCSession::SetLayerVisibleRegion(hwc2_display_t display, hwc2_layer_t layer,
1008 hwc_region_t visible) {
1009 return CallLayerFunction(display, layer, &HWCLayer::SetLayerVisibleRegion, visible);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001010}
1011
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301012int32_t HWCSession::SetLayerZOrder(hwc2_display_t display, hwc2_layer_t layer, uint32_t z) {
1013 return CallDisplayFunction(display, &HWCDisplay::SetLayerZOrder, layer, z);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001014}
1015
Ray Zhang2ee82472019-09-05 02:53:29 +08001016int32_t HWCSession::SetLayerType(hwc2_display_t display, hwc2_layer_t layer,
1017 IQtiComposerClient::LayerType type) {
1018 return CallDisplayFunction(display, &HWCDisplay::SetLayerType, layer, type);
1019}
1020
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301021int32_t HWCSession::SetLayerColorTransform(hwc2_display_t display, hwc2_layer_t layer,
1022 const float *matrix) {
1023 return CallLayerFunction(display, layer, &HWCLayer::SetLayerColorTransform, matrix);
Tharaga Balachandran209db582019-03-25 18:56:50 -04001024}
1025
Padmanabhan Komanduru13b5e102019-12-03 12:35:09 +05301026int32_t HWCSession::SetDisplayElapseTime(hwc2_display_t display, uint64_t time) {
1027 return CallDisplayFunction(display, &HWCDisplay::SetDisplayElapseTime, time);
1028}
1029
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301030int32_t HWCSession::SetOutputBuffer(hwc2_display_t display, buffer_handle_t buffer,
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +05301031 const shared_ptr<Fence> &release_fence) {
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301032 if (INT32(display) != GetDisplayIndex(qdutils::DISPLAY_VIRTUAL)) {
Arun Kumar K.R1018d5b2017-06-08 13:51:05 +05301033 return HWC2_ERROR_UNSUPPORTED;
1034 }
1035
Saurabh Shahc5c687a2017-08-24 16:30:14 -07001036 SCOPE_LOCK(locker_[display]);
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301037 if (hwc_display_[display]) {
1038 auto vds = reinterpret_cast<HWCDisplayVirtual *>(hwc_display_[display]);
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +05301039 auto status = vds->SetOutputBuffer(buffer, release_fence);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001040 return INT32(status);
1041 } else {
1042 return HWC2_ERROR_BAD_DISPLAY;
1043 }
1044}
1045
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301046int32_t HWCSession::SetPowerMode(hwc2_display_t display, int32_t int_mode) {
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07001047 if (display >= HWCCallbacks::kNumDisplays) {
Varun Arora2c23e582017-10-10 15:07:13 -07001048 return HWC2_ERROR_BAD_DISPLAY;
1049 }
1050
1051 // validate device and also avoid undefined behavior in cast to HWC2::PowerMode
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301052 if (int_mode < HWC2_POWER_MODE_OFF || int_mode > HWC2_POWER_MODE_DOZE_SUSPEND) {
Varun Arora2c23e582017-10-10 15:07:13 -07001053 return HWC2_ERROR_BAD_PARAMETER;
1054 }
1055
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05301056 if (pending_power_mode_[display]) {
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -07001057 DLOGW("Set power mode is not allowed during secure display session");
1058 return HWC2_ERROR_UNSUPPORTED;
1059 }
1060
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001061 auto mode = static_cast<HWC2::PowerMode>(int_mode);
Varun Arora2c23e582017-10-10 15:07:13 -07001062
1063 // all displays support on/off. Check for doze modes
1064 int support = 0;
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301065 auto status = GetDozeSupport(display, &support);
Gurpreet Singh Dhamia9478232018-08-21 12:19:47 -04001066 if (status != HWC2_ERROR_NONE) {
1067 DLOGE("Failed to get doze support Error = %d", status);
1068 return INT32(status);
1069 }
1070
Varun Arora2c23e582017-10-10 15:07:13 -07001071 if (!support && (mode == HWC2::PowerMode::Doze || mode == HWC2::PowerMode::DozeSuspend)) {
1072 return HWC2_ERROR_UNSUPPORTED;
1073 }
1074
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +05301075 bool override_mode = async_powermode_ && display_ready_.test(UINT32(display));
1076 if (!override_mode) {
1077 auto error = CallDisplayFunction(display, &HWCDisplay::SetPowerMode, mode,
1078 false /* teardown */);
1079 if (INT32(error) != HWC2_ERROR_NONE) {
1080 return INT32(error);
1081 }
1082 } else {
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05301083 Locker::ScopeLock lock_disp(locker_[display]);
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +05301084 // Update hwc state for now. Actual poweron will handled through DisplayConfig.
1085 hwc_display_[display]->UpdatePowerMode(mode);
Srinivas Pullakavia747d832018-05-22 13:52:06 +05301086 }
Ramkumar Radhakrishnanf985d482018-07-23 18:10:41 -07001087 // Reset idle pc ref count on suspend, as we enable idle pc during suspend.
1088 if (mode == HWC2::PowerMode::Off) {
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301089 idle_pc_ref_cnt_ = 0;
Ramkumar Radhakrishnanf985d482018-07-23 18:10:41 -07001090 }
Srinivas Pullakavia747d832018-05-22 13:52:06 +05301091
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301092
1093 UpdateThrottlingRate();
Rajavenu Kyatham9d509582019-02-13 15:29:55 +05301094
Rajavenu Kyatham9d509582019-02-13 15:29:55 +05301095 if (mode == HWC2::PowerMode::Doze) {
Padmanabhan Komanduru1649f222019-03-05 11:25:00 +05301096 // Trigger one more refresh for PP features to take effect.
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301097 pending_refresh_.set(UINT32(display));
Rajavenu Kyatham9d509582019-02-13 15:29:55 +05301098 }
1099
Srinivas Pullakavia747d832018-05-22 13:52:06 +05301100 return HWC2_ERROR_NONE;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001101}
1102
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301103int32_t HWCSession::SetVsyncEnabled(hwc2_display_t display, int32_t int_enabled) {
Varun Arora2c23e582017-10-10 15:07:13 -07001104 // avoid undefined behavior in cast to HWC2::Vsync
1105 if (int_enabled < HWC2_VSYNC_INVALID || int_enabled > HWC2_VSYNC_DISABLE) {
1106 return HWC2_ERROR_BAD_PARAMETER;
1107 }
1108
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001109 auto enabled = static_cast<HWC2::Vsync>(int_enabled);
Srinivas Pullakavia747d832018-05-22 13:52:06 +05301110
Padmanabhan Komanduruf5e6c5d2019-04-04 13:56:31 +05301111 if (int_enabled == HWC2_VSYNC_ENABLE) {
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301112 callbacks_.UpdateVsyncSource(display);
Padmanabhan Komanduruf5e6c5d2019-04-04 13:56:31 +05301113 }
Srinivas Pullakavia747d832018-05-22 13:52:06 +05301114
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301115 return CallDisplayFunction(display, &HWCDisplay::SetVsyncEnabled, enabled);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001116}
1117
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301118int32_t HWCSession::GetDozeSupport(hwc2_display_t display, int32_t *out_support) {
1119 if (!out_support) {
Pullakavi Srinivas04e52422018-07-31 15:12:57 +05301120 return HWC2_ERROR_BAD_PARAMETER;
1121 }
1122
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301123 if (display >= HWCCallbacks::kNumDisplays || (hwc_display_[display] == nullptr)) {
1124 DLOGE("Invalid Display %d Handle %s ", display, hwc_display_[display] ?
Gurpreet Singh Dhamia9478232018-08-21 12:19:47 -04001125 "Valid" : "NULL");
Pullakavi Srinivas04e52422018-07-31 15:12:57 +05301126 return HWC2_ERROR_BAD_DISPLAY;
1127 }
1128
Pullakavi Srinivas04e52422018-07-31 15:12:57 +05301129 *out_support = 0;
Pullakavi Srinivas5cb87bd2020-04-02 22:30:55 +05301130 uint32_t config = 0;
1131 GetActiveConfigIndex(display, &config);
1132 *out_support = isSmartPanelConfig(display, config) ? 1 : 0;
Pullakavi Srinivas04e52422018-07-31 15:12:57 +05301133
1134 return HWC2_ERROR_NONE;
1135}
1136
Pullakavi Srinivas5cb87bd2020-04-02 22:30:55 +05301137bool HWCSession::isSmartPanelConfig(uint32_t disp_id, uint32_t config_id) {
1138 if (disp_id != qdutils::DISPLAY_PRIMARY) {
1139 return false;
1140 }
1141
1142 SCOPE_LOCK(locker_[disp_id]);
1143 if (!hwc_display_[disp_id]) {
1144 DLOGE("Display %d is not created yet.", disp_id);
1145 return false;
1146 }
1147
1148 return hwc_display_[disp_id]->IsSmartPanelConfig(config_id);
1149}
1150
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301151int32_t HWCSession::ValidateDisplay(hwc2_display_t display, uint32_t *out_num_types,
1152 uint32_t *out_num_requests) {
Varun Arora2c23e582017-10-10 15:07:13 -07001153 // out_num_types and out_num_requests will be non-NULL
Varun Arora2c23e582017-10-10 15:07:13 -07001154
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07001155 if (display >= HWCCallbacks::kNumDisplays) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001156 return HWC2_ERROR_BAD_DISPLAY;
1157 }
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +05301158
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05301159 hwc2_display_t target_display = display;
1160
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +05301161 {
1162 SCOPE_LOCK(power_state_[display]);
1163 if (power_state_transition_[display]) {
1164 // Route all interactions with client to dummy display.
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05301165 target_display = map_hwc_display_.find(display)->second;
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +05301166 }
1167 }
Varun Arora2c23e582017-10-10 15:07:13 -07001168 DTRACE_SCOPED();
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001169 // TODO(user): Handle secure session, handle QDCM solid fill
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001170 auto status = HWC2::Error::BadDisplay;
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301171 HandleSecureSession();
Saurabh Shahc5c687a2017-08-24 16:30:14 -07001172 {
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05301173 SEQUENCE_ENTRY_SCOPE_LOCK(locker_[target_display]);
1174 if (pending_power_mode_[display]) {
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +05301175 status = HWC2::Error::None;
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05301176 } else if (hwc_display_[target_display]) {
Sushil Chauhanba8e4652020-03-20 10:07:47 -07001177 hwc_display_[target_display]->ProcessActiveConfigChange();
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05301178 hwc_display_[target_display]->SetFastPathComposition(false);
1179 status = ValidateDisplayInternal(target_display, out_num_types, out_num_requests);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001180 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001181 }
Saurabh Shahc5c687a2017-08-24 16:30:14 -07001182
Saurabh Shah04f932b2017-10-12 18:47:45 -07001183 // Sequence locking currently begins on Validate, so cancel the sequence lock on failures
Saurabh Shah998ad472018-01-18 11:44:08 -08001184 if (status != HWC2::Error::None && status != HWC2::Error::HasChanges) {
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05301185 SEQUENCE_CANCEL_SCOPE_LOCK(locker_[target_display]);
1186 }
1187
1188 if (display != target_display) {
1189 // Validate done on a dummy display. Assume present is complete.
1190 SEQUENCE_EXIT_SCOPE_LOCK(locker_[target_display]);
Saurabh Shahc5c687a2017-08-24 16:30:14 -07001191 }
1192
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001193 return INT32(status);
1194}
1195
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05301196HWC2::Error HWCSession::CreateVirtualDisplayObj(uint32_t width, uint32_t height, int32_t *format,
1197 hwc2_display_t *out_display_id) {
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08001198 hwc2_display_t active_builtin_disp_id = GetActiveBuiltinDisplay();
Padmanabhan Komanduruf5b7f992019-07-08 13:33:44 +05301199 hwc2_display_t client_id = HWCCallbacks::kNumDisplays;
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07001200 if (active_builtin_disp_id < HWCCallbacks::kNumDisplays) {
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08001201 SEQUENCE_WAIT_SCOPE_LOCK(locker_[active_builtin_disp_id]);
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +05301202 std::bitset<kSecureMax> secure_sessions = 0;
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08001203 if (hwc_display_[active_builtin_disp_id]) {
1204 hwc_display_[active_builtin_disp_id]->GetActiveSecureSession(&secure_sessions);
1205 }
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +05301206 if (secure_sessions.any()) {
1207 DLOGE("Secure session is active, cannot create virtual display.");
1208 return HWC2::Error::Unsupported;
1209 } else if (IsPluggableDisplayConnected()) {
1210 DLOGE("External session is active, cannot create virtual display.");
1211 return HWC2::Error::Unsupported;
1212 }
1213 }
1214
Pullakavi Srinivasf2a31852019-04-23 14:32:08 -07001215 if (hwc_display_[HWC_DISPLAY_PRIMARY]) {
1216 DisplayError error = hwc_display_[HWC_DISPLAY_PRIMARY]->TeardownConcurrentWriteback();
1217 if (error) {
1218 return HWC2::Error::NoResources;
1219 }
1220 }
1221
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05301222 // Lock confined to this scope
1223 int status = -EINVAL;
Pullakavi Srinivasa3c292c2019-12-01 13:27:50 +05301224 for (auto &map_info : map_info_virtual_) {
1225 client_id = map_info.client_id;
1226 {
1227 SCOPE_LOCK(locker_[client_id]);
1228 auto &hwc_display = hwc_display_[client_id];
1229 if (hwc_display) {
1230 continue;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05301231 }
Pullakavi Srinivasa3c292c2019-12-01 13:27:50 +05301232
1233 int32_t display_id = GetVirtualDisplayId();
1234 status = virtual_display_factory_.Create(core_intf_, &buffer_allocator_, &callbacks_,
1235 client_id, display_id, width, height,
1236 format, set_min_lum_, set_max_lum_, &hwc_display);
1237 // TODO(user): validate width and height support
1238 if (status) {
1239 return HWC2::Error::NoResources;
1240 }
1241
1242 {
1243 SCOPE_LOCK(hdr_locker_[client_id]);
1244 is_hdr_display_[UINT32(client_id)] = HasHDRSupport(hwc_display);
1245 }
1246
1247 DLOGI("Created virtual display id:% " PRIu64 " with res: %dx%d", client_id, width, height);
1248
1249 *out_display_id = client_id;
1250 map_info.disp_type = kVirtual;
1251 map_info.sdm_id = display_id;
1252 break;
Saurabh Shaha307e8c2017-09-28 18:05:40 -07001253 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001254 }
Saurabh Shaha307e8c2017-09-28 18:05:40 -07001255
Padmanabhan Komanduruf5b7f992019-07-08 13:33:44 +05301256 // Active builtin display needs revalidation
1257 if (!async_vds_creation_ && active_builtin_disp_id < HWCCallbacks::kNumRealDisplays) {
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08001258 SEQUENCE_WAIT_SCOPE_LOCK(locker_[active_builtin_disp_id]);
1259 hwc_display_[active_builtin_disp_id]->ResetValidation();
1260 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001261
1262 return HWC2::Error::None;
1263}
1264
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +05301265bool HWCSession::IsPluggableDisplayConnected() {
1266 for (auto &map_info : map_info_pluggable_) {
1267 if (hwc_display_[map_info.client_id]) {
1268 return true;
1269 }
1270 }
1271 return false;
Pullakavi Srinivas04e52422018-07-31 15:12:57 +05301272}
1273
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001274// Qclient methods
1275android::status_t HWCSession::notifyCallback(uint32_t command, const android::Parcel *input_parcel,
1276 android::Parcel *output_parcel) {
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301277 android::status_t status = -EINVAL;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001278
1279 switch (command) {
1280 case qService::IQService::DYNAMIC_DEBUG:
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301281 if (!input_parcel) {
1282 DLOGE("QService command = %d: input_parcel needed.", command);
1283 break;
1284 }
1285 status = 0;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001286 DynamicDebug(input_parcel);
1287 break;
1288
1289 case qService::IQService::SCREEN_REFRESH:
Dileep Marchya35e27532018-06-29 10:50:16 +05301290 if (!input_parcel) {
1291 DLOGE("QService command = %d: input_parcel needed.", command);
1292 break;
1293 }
1294 status = RefreshScreen(input_parcel);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001295 break;
1296
1297 case qService::IQService::SET_IDLE_TIMEOUT:
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301298 if (!input_parcel) {
1299 DLOGE("QService command = %d: input_parcel needed.", command);
1300 break;
1301 }
Padmanabhan Komanduru9e971622020-02-10 13:09:34 +05301302 status = SetIdleTimeout(UINT32(input_parcel->readInt32()));
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001303 break;
1304
1305 case qService::IQService::SET_FRAME_DUMP_CONFIG:
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301306 if (!input_parcel) {
1307 DLOGE("QService command = %d: input_parcel needed.", command);
1308 break;
1309 }
1310 status = SetFrameDumpConfig(input_parcel);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001311 break;
1312
1313 case qService::IQService::SET_MAX_PIPES_PER_MIXER:
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301314 if (!input_parcel) {
1315 DLOGE("QService command = %d: input_parcel needed.", command);
1316 break;
1317 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001318 status = SetMaxMixerStages(input_parcel);
1319 break;
1320
1321 case qService::IQService::SET_DISPLAY_MODE:
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301322 if (!input_parcel) {
1323 DLOGE("QService command = %d: input_parcel needed.", command);
1324 break;
1325 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001326 status = SetDisplayMode(input_parcel);
1327 break;
1328
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301329 case qService::IQService::SET_SECONDARY_DISPLAY_STATUS: {
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301330 if (!input_parcel || !output_parcel) {
1331 DLOGE("QService command = %d: input_parcel and output_parcel needed.", command);
1332 break;
1333 }
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301334 int disp_id = INT(input_parcel->readInt32());
1335 HWCDisplay::DisplayStatus disp_status =
1336 static_cast<HWCDisplay::DisplayStatus>(input_parcel->readInt32());
Padmanabhan Komanduru9e971622020-02-10 13:09:34 +05301337 status = SetDisplayStatus(disp_id, disp_status);
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301338 output_parcel->writeInt32(status);
1339 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001340 break;
1341
1342 case qService::IQService::CONFIGURE_DYN_REFRESH_RATE:
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301343 if (!input_parcel) {
1344 DLOGE("QService command = %d: input_parcel needed.", command);
1345 break;
1346 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001347 status = ConfigureRefreshRate(input_parcel);
1348 break;
1349
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301350 case qService::IQService::TOGGLE_SCREEN_UPDATES: {
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301351 if (!input_parcel || !output_parcel) {
1352 DLOGE("QService command = %d: input_parcel and output_parcel needed.", command);
1353 break;
1354 }
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301355 int32_t input = input_parcel->readInt32();
Padmanabhan Komanduru9e971622020-02-10 13:09:34 +05301356 status = ToggleScreenUpdate(input == 1);
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301357 output_parcel->writeInt32(status);
1358 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001359 break;
1360
1361 case qService::IQService::QDCM_SVC_CMDS:
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301362 if (!input_parcel || !output_parcel) {
1363 DLOGE("QService command = %d: input_parcel and output_parcel needed.", command);
1364 break;
1365 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001366 status = QdcmCMDHandler(input_parcel, output_parcel);
1367 break;
1368
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301369 case qService::IQService::MIN_HDCP_ENCRYPTION_LEVEL_CHANGED: {
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301370 if (!input_parcel || !output_parcel) {
1371 DLOGE("QService command = %d: input_parcel and output_parcel needed.", command);
1372 break;
1373 }
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301374 int disp_id = input_parcel->readInt32();
1375 uint32_t min_enc_level = UINT32(input_parcel->readInt32());
1376 status = MinHdcpEncryptionLevelChanged(disp_id, min_enc_level);
1377 output_parcel->writeInt32(status);
1378 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001379 break;
1380
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301381 case qService::IQService::CONTROL_PARTIAL_UPDATE: {
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301382 if (!input_parcel || !output_parcel) {
1383 DLOGE("QService command = %d: input_parcel and output_parcel needed.", command);
1384 break;
1385 }
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301386 int disp_id = input_parcel->readInt32();
1387 uint32_t enable = UINT32(input_parcel->readInt32());
1388 status = ControlPartialUpdate(disp_id, enable == 1);
1389 output_parcel->writeInt32(status);
1390 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001391 break;
1392
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301393 case qService::IQService::SET_ACTIVE_CONFIG: {
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301394 if (!input_parcel) {
1395 DLOGE("QService command = %d: input_parcel needed.", command);
1396 break;
1397 }
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301398 uint32_t config = UINT32(input_parcel->readInt32());
1399 int disp_id = input_parcel->readInt32();
1400 status = SetActiveConfigIndex(disp_id, config);
1401 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001402 break;
1403
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301404 case qService::IQService::GET_ACTIVE_CONFIG: {
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301405 if (!input_parcel || !output_parcel) {
1406 DLOGE("QService command = %d: input_parcel and output_parcel needed.", command);
1407 break;
1408 }
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301409 int disp_id = input_parcel->readInt32();
1410 uint32_t config = 0;
1411 status = GetActiveConfigIndex(disp_id, &config);
1412 output_parcel->writeInt32(INT(config));
1413 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001414 break;
1415
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301416 case qService::IQService::GET_CONFIG_COUNT: {
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301417 if (!input_parcel || !output_parcel) {
1418 DLOGE("QService command = %d: input_parcel and output_parcel needed.", command);
1419 break;
1420 }
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301421 int disp_id = input_parcel->readInt32();
1422 uint32_t count = 0;
1423 status = GetConfigCount(disp_id, &count);
1424 output_parcel->writeInt32(INT(count));
1425 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001426 break;
1427
1428 case qService::IQService::GET_DISPLAY_ATTRIBUTES_FOR_CONFIG:
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301429 if (!input_parcel || !output_parcel) {
1430 DLOGE("QService command = %d: input_parcel and output_parcel needed.", command);
1431 break;
1432 }
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05301433 status = GetDisplayAttributesForConfig(input_parcel, output_parcel);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001434 break;
1435
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301436 case qService::IQService::GET_PANEL_BRIGHTNESS: {
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301437 if (!output_parcel) {
1438 DLOGE("QService command = %d: output_parcel needed.", command);
1439 break;
1440 }
Yuchao Maada1c402019-11-12 14:47:11 +08001441
Varun Arora75c05f02019-05-14 14:53:37 -07001442 uint32_t display = input_parcel->readUint32();
Yuchao Maada1c402019-11-12 14:47:11 +08001443 uint32_t max_brightness_level = 0;
1444 status = getDisplayMaxBrightness(display, &max_brightness_level);
1445 if (status || !max_brightness_level) {
1446 output_parcel->writeInt32(max_brightness_level);
1447 DLOGE("Failed to get max brightness %u, status %d", max_brightness_level, status);
1448 break;
1449 }
1450 DLOGV("Panel Max brightness is %u", max_brightness_level);
1451
1452 float brightness_precent = -1.0f;
1453 status = getDisplayBrightness(display, &brightness_precent);
1454 if (brightness_precent == -1.0f) {
Varun Arora75c05f02019-05-14 14:53:37 -07001455 output_parcel->writeInt32(0);
1456 } else {
Yuchao Maada1c402019-11-12 14:47:11 +08001457 output_parcel->writeInt32(INT32(brightness_precent*(max_brightness_level - 1) + 1));
Varun Arora75c05f02019-05-14 14:53:37 -07001458 }
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301459 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001460 break;
1461
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301462 case qService::IQService::SET_PANEL_BRIGHTNESS: {
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301463 if (!input_parcel || !output_parcel) {
1464 DLOGE("QService command = %d: input_parcel and output_parcel needed.", command);
1465 break;
1466 }
Yuchao Maada1c402019-11-12 14:47:11 +08001467
1468 uint32_t max_brightness_level = 0;
1469 uint32_t display = HWC_DISPLAY_PRIMARY;
1470 status = getDisplayMaxBrightness(display, &max_brightness_level);
1471 if (status || max_brightness_level <= 1) {
1472 output_parcel->writeInt32(max_brightness_level);
1473 DLOGE("Failed to get max brightness %u, status %d", max_brightness_level, status);
1474 break;
1475 }
1476 DLOGV("Panel Max brightness is %u", max_brightness_level);
1477
Varun Arora75c05f02019-05-14 14:53:37 -07001478 int level = input_parcel->readInt32();
Varun Arora75c05f02019-05-14 14:53:37 -07001479 if (level == 0) {
Yuchao Maada1c402019-11-12 14:47:11 +08001480 status = SetDisplayBrightness(display, -1.0f);
Varun Arora75c05f02019-05-14 14:53:37 -07001481 } else {
Yuchao Maada1c402019-11-12 14:47:11 +08001482 status = SetDisplayBrightness(display,
1483 (level - 1)/(static_cast<float>(max_brightness_level - 1)));
Varun Arora75c05f02019-05-14 14:53:37 -07001484 }
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301485 output_parcel->writeInt32(status);
1486 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001487 break;
1488
1489 case qService::IQService::GET_DISPLAY_VISIBLE_REGION:
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301490 if (!input_parcel || !output_parcel) {
1491 DLOGE("QService command = %d: input_parcel and output_parcel needed.", command);
1492 break;
1493 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001494 status = GetVisibleDisplayRect(input_parcel, output_parcel);
1495 break;
1496
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301497 case qService::IQService::SET_CAMERA_STATUS: {
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301498 if (!input_parcel) {
1499 DLOGE("QService command = %d: input_parcel needed.", command);
1500 break;
1501 }
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301502 uint32_t camera_status = UINT32(input_parcel->readInt32());
Padmanabhan Komanduru9e971622020-02-10 13:09:34 +05301503 status = SetCameraLaunchStatus(camera_status);
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301504 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001505 break;
1506
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301507 case qService::IQService::GET_BW_TRANSACTION_STATUS: {
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301508 if (!output_parcel) {
1509 DLOGE("QService command = %d: output_parcel needed.", command);
1510 break;
1511 }
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301512 bool state = true;
1513 status = DisplayBWTransactionPending(&state);
1514 output_parcel->writeInt32(state);
1515 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001516 break;
1517
Naseer Ahmedad495442016-06-27 14:59:18 -04001518 case qService::IQService::SET_LAYER_MIXER_RESOLUTION:
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301519 if (!input_parcel) {
1520 DLOGE("QService command = %d: input_parcel needed.", command);
1521 break;
1522 }
Naseer Ahmedad495442016-06-27 14:59:18 -04001523 status = SetMixerResolution(input_parcel);
1524 break;
1525
1526 case qService::IQService::SET_COLOR_MODE:
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301527 if (!input_parcel) {
1528 DLOGE("QService command = %d: input_parcel needed.", command);
1529 break;
1530 }
Naseer Ahmedad495442016-06-27 14:59:18 -04001531 status = SetColorModeOverride(input_parcel);
1532 break;
Arun Kumar K.R8da7f502016-06-07 17:45:50 -07001533
Naseer Ahmede7a77982018-06-04 10:56:04 -04001534 case qService::IQService::SET_COLOR_MODE_WITH_RENDER_INTENT:
1535 if (!input_parcel) {
1536 DLOGE("QService command = %d: input_parcel needed.", command);
1537 break;
1538 }
1539 status = SetColorModeWithRenderIntentOverride(input_parcel);
1540 break;
1541
Naseer Ahmed6776dae2017-05-09 11:49:41 -04001542 case qService::IQService::SET_COLOR_MODE_BY_ID:
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301543 if (!input_parcel) {
1544 DLOGE("QService command = %d: input_parcel needed.", command);
1545 break;
1546 }
Naseer Ahmed6776dae2017-05-09 11:49:41 -04001547 status = SetColorModeById(input_parcel);
1548 break;
1549
Prabhanjan Kandula63a613f2017-09-14 19:29:46 -07001550 case qService::IQService::GET_COMPOSER_STATUS:
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301551 if (!output_parcel) {
1552 DLOGE("QService command = %d: output_parcel needed.", command);
1553 break;
1554 }
1555 status = 0;
Prabhanjan Kandula63a613f2017-09-14 19:29:46 -07001556 output_parcel->writeInt32(getComposerStatus());
1557 break;
1558
Sushil Chauhan380a59d2018-03-19 15:47:50 -07001559 case qService::IQService::SET_QSYNC_MODE:
1560 if (!input_parcel) {
1561 DLOGE("QService command = %d: input_parcel needed.", command);
1562 break;
1563 }
1564 status = SetQSyncMode(input_parcel);
1565 break;
1566
Lakshmi Narayana Kalavala25fe12c2019-12-11 20:44:55 -08001567 case qService::IQService::SET_COLOR_SAMPLING_ENABLED:
1568 if (!input_parcel) {
1569 DLOGE("QService command = %d: input_parcel needed.", command);
1570 break;
1571 }
1572 status = setColorSamplingEnabled(input_parcel);
1573 break;
1574
Ramkumar Radhakrishnanf985d482018-07-23 18:10:41 -07001575 case qService::IQService::SET_IDLE_PC:
1576 if (!input_parcel) {
1577 DLOGE("QService command = %d: input_parcel needed.", command);
1578 break;
1579 }
1580 status = SetIdlePC(input_parcel);
1581 break;
1582
Yuchao Ma577f0f72018-07-09 11:20:00 +08001583 case qService::IQService::SET_DPPS_AD4_ROI_CONFIG:
1584 if (!input_parcel) {
1585 DLOGE("QService command = %d: input_parcel needed.", command);
1586 break;
1587 }
1588 status = SetAd4RoiConfig(input_parcel);
1589 break;
1590
Pullakavi Srinivas9189e602018-12-19 16:58:07 +05301591 case qService::IQService::SET_DSI_CLK:
1592 if (!input_parcel) {
1593 DLOGE("QService command = %d: input_parcel needed.", command);
1594 break;
1595 }
1596 status = SetDsiClk(input_parcel);
1597 break;
1598
1599 case qService::IQService::GET_DSI_CLK:
1600 if (!input_parcel || !output_parcel) {
1601 DLOGE("QService command = %d: input_parcel and output_parcel needed.", command);
1602 break;
1603 }
1604 status = GetDsiClk(input_parcel, output_parcel);
1605 break;
1606
1607 case qService::IQService::GET_SUPPORTED_DSI_CLK:
1608 if (!input_parcel || !output_parcel) {
1609 DLOGE("QService command = %d: input_parcel and output_parcel needed.", command);
1610 break;
1611 }
1612 status = GetSupportedDsiClk(input_parcel, output_parcel);
1613 break;
1614
Archit Srivastavaa59c4ff2019-05-20 10:54:22 +05301615 case qService::IQService::SET_PANEL_LUMINANCE:
1616 if (!input_parcel) {
1617 DLOGE("QService command = %d: input_parcel needed.", command);
1618 break;
1619 }
1620 status = SetPanelLuminanceAttributes(input_parcel);
1621 break;
1622
Qing Huangda3a91d2019-01-15 14:34:11 +08001623 case qService::IQService::SET_COLOR_MODE_FROM_CLIENT:
1624 if (!input_parcel) {
1625 DLOGE("QService command = %d: input_parcel needed.", command);
1626 break;
1627 }
1628 status = SetColorModeFromClient(input_parcel);
1629 break;
1630
Xu Yang9dacc872018-12-25 11:04:28 +08001631 case qService::IQService::SET_FRAME_TRIGGER_MODE:
1632 if (!input_parcel) {
1633 DLOGE("QService command = %d: input_parcel needed.", command);
1634 break;
1635 }
1636 status = SetFrameTriggerMode(input_parcel);
1637 break;
1638
Xu Yang45d0abf2019-07-05 11:34:06 +08001639 case qService::IQService::SET_BRIGHTNESS_SCALE:
1640 if (!input_parcel) {
1641 DLOGE("QService command = %d: input_parcel needed.", command);
1642 break;
1643 }
1644 status = SetDisplayBrightnessScale(input_parcel);
1645 break;
1646
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001647 default:
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301648 DLOGW("QService command = %d is not supported.", command);
1649 break;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001650 }
1651
1652 return status;
1653}
1654
Prabhanjan Kandula63a613f2017-09-14 19:29:46 -07001655android::status_t HWCSession::getComposerStatus() {
1656 return is_composer_up_;
1657}
1658
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05301659android::status_t HWCSession::GetDisplayAttributesForConfig(const android::Parcel *input_parcel,
1660 android::Parcel *output_parcel) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001661 int config = input_parcel->readInt32();
1662 int dpy = input_parcel->readInt32();
1663 int error = android::BAD_VALUE;
Arun Kumar K.R8da7f502016-06-07 17:45:50 -07001664 DisplayConfigVariableInfo display_attributes;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001665
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05301666 int disp_idx = GetDisplayIndex(dpy);
1667 if (disp_idx == -1 || config < 0) {
1668 DLOGE("Invalid display = %d, or config = %d", dpy, config);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001669 return android::BAD_VALUE;
1670 }
1671
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05301672 SEQUENCE_WAIT_SCOPE_LOCK(locker_[disp_idx]);
1673 if (hwc_display_[disp_idx]) {
1674 error = hwc_display_[disp_idx]->GetDisplayAttributesForConfig(config, &display_attributes);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001675 if (error == 0) {
Arun Kumar K.R8da7f502016-06-07 17:45:50 -07001676 output_parcel->writeInt32(INT(display_attributes.vsync_period_ns));
1677 output_parcel->writeInt32(INT(display_attributes.x_pixels));
1678 output_parcel->writeInt32(INT(display_attributes.y_pixels));
1679 output_parcel->writeFloat(display_attributes.x_dpi);
1680 output_parcel->writeFloat(display_attributes.y_dpi);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001681 output_parcel->writeInt32(0); // Panel type, unsupported.
1682 }
1683 }
1684
1685 return error;
1686}
1687
Lakshmi Narayana Kalavala25fe12c2019-12-11 20:44:55 -08001688android::status_t HWCSession::setColorSamplingEnabled(const android::Parcel *input_parcel) {
1689 int dpy = input_parcel->readInt32();
1690 int enabled_cmd = input_parcel->readInt32();
1691 if (dpy < HWC_DISPLAY_PRIMARY || dpy >= HWC_NUM_DISPLAY_TYPES || enabled_cmd < 0 ||
1692 enabled_cmd > 1) {
1693 return android::BAD_VALUE;
1694 }
1695
1696 SEQUENCE_WAIT_SCOPE_LOCK(locker_[dpy]);
1697 if (!hwc_display_[dpy]) {
1698 DLOGW("No display id %i active to enable histogram event", dpy);
1699 return android::BAD_VALUE;
1700 }
1701
1702 auto error = hwc_display_[dpy]->SetDisplayedContentSamplingEnabledVndService(enabled_cmd);
1703 return (error == HWC2::Error::None) ? android::OK : android::BAD_VALUE;
1704}
1705
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001706android::status_t HWCSession::ConfigureRefreshRate(const android::Parcel *input_parcel) {
Saurabh Shahc5c687a2017-08-24 16:30:14 -07001707 SEQUENCE_WAIT_SCOPE_LOCK(locker_[HWC_DISPLAY_PRIMARY]);
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301708
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001709 uint32_t operation = UINT32(input_parcel->readInt32());
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301710 HWCDisplay *hwc_display = hwc_display_[HWC_DISPLAY_PRIMARY];
1711
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301712 if (!hwc_display) {
1713 DLOGW("Display = %d is not connected.", HWC_DISPLAY_PRIMARY);
1714 return -ENODEV;
1715 }
1716
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001717 switch (operation) {
1718 case qdutils::DISABLE_METADATA_DYN_REFRESH_RATE:
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05301719 return hwc_display->Perform(HWCDisplayBuiltIn::SET_METADATA_DYN_REFRESH_RATE, false);
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301720
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001721 case qdutils::ENABLE_METADATA_DYN_REFRESH_RATE:
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05301722 return hwc_display->Perform(HWCDisplayBuiltIn::SET_METADATA_DYN_REFRESH_RATE, true);
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301723
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001724 case qdutils::SET_BINDER_DYN_REFRESH_RATE: {
1725 uint32_t refresh_rate = UINT32(input_parcel->readInt32());
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05301726 return hwc_display->Perform(HWCDisplayBuiltIn::SET_BINDER_DYN_REFRESH_RATE, refresh_rate);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001727 }
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301728
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001729 default:
1730 DLOGW("Invalid operation %d", operation);
1731 return -EINVAL;
1732 }
1733
1734 return 0;
1735}
1736
1737android::status_t HWCSession::SetDisplayMode(const android::Parcel *input_parcel) {
Saurabh Shahc5c687a2017-08-24 16:30:14 -07001738 SEQUENCE_WAIT_SCOPE_LOCK(locker_[HWC_DISPLAY_PRIMARY]);
Dileep Marchya6860b2b2017-04-07 15:56:47 +05301739
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301740 if (!hwc_display_[HWC_DISPLAY_PRIMARY]) {
1741 DLOGW("Display = %d is not connected.", HWC_DISPLAY_PRIMARY);
1742 return -ENODEV;
1743 }
1744
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001745 uint32_t mode = UINT32(input_parcel->readInt32());
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05301746 return hwc_display_[HWC_DISPLAY_PRIMARY]->Perform(HWCDisplayBuiltIn::SET_DISPLAY_MODE, mode);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001747}
1748
1749android::status_t HWCSession::SetMaxMixerStages(const android::Parcel *input_parcel) {
1750 DisplayError error = kErrorNone;
Dileep Marchya7290bd22016-06-09 11:37:20 +05301751 std::bitset<32> bit_mask_display_type = UINT32(input_parcel->readInt32());
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001752 uint32_t max_mixer_stages = UINT32(input_parcel->readInt32());
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301753 android::status_t status = 0;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001754
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05301755 for (uint32_t i = 0; i < 32 && bit_mask_display_type[i]; i++) {
1756 int disp_idx = GetDisplayIndex(INT(i));
1757 if (disp_idx == -1) {
1758 continue;
1759 }
1760 SEQUENCE_WAIT_SCOPE_LOCK(locker_[disp_idx]);
1761 auto &hwc_display = hwc_display_[disp_idx];
1762 if (!hwc_display) {
1763 DLOGW("Display = %d is not connected.", disp_idx);
1764 status = (status)? status : -ENODEV; // Return higher priority error.
1765 continue;
1766 }
1767
1768 error = hwc_display->SetMaxMixerStages(max_mixer_stages);
1769 if (error != kErrorNone) {
1770 status = -EINVAL;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001771 }
1772 }
1773
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301774 return status;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001775}
1776
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301777android::status_t HWCSession::SetFrameDumpConfig(const android::Parcel *input_parcel) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001778 uint32_t frame_dump_count = UINT32(input_parcel->readInt32());
Dileep Marchya7290bd22016-06-09 11:37:20 +05301779 std::bitset<32> bit_mask_display_type = UINT32(input_parcel->readInt32());
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001780 uint32_t bit_mask_layer_type = UINT32(input_parcel->readInt32());
Sushil Chauhan267a6192018-06-06 18:41:47 -07001781 int32_t output_format = HAL_PIXEL_FORMAT_RGB_888;
1782 bool post_processed = true;
1783
Sushil Chauhan0a1b15d2019-08-23 12:58:18 -07001784 // Output buffer dump is not supported, if External or Virtual display is present.
1785 bool output_buffer_dump = bit_mask_layer_type & (1 << OUTPUT_LAYER_DUMP);
1786 if (output_buffer_dump) {
1787 int external_dpy_index = GetDisplayIndex(qdutils::DISPLAY_EXTERNAL);
1788 int virtual_dpy_index = GetDisplayIndex(qdutils::DISPLAY_VIRTUAL);
1789 if (((external_dpy_index != -1) && hwc_display_[external_dpy_index]) ||
1790 ((virtual_dpy_index != -1) && hwc_display_[virtual_dpy_index])) {
1791 DLOGW("Output buffer dump is not supported with External or Virtual display!");
1792 return -EINVAL;
1793 }
1794 }
1795
Sushil Chauhan267a6192018-06-06 18:41:47 -07001796 // Read optional user preferences: output_format and post_processed.
1797 if (input_parcel->dataPosition() != input_parcel->dataSize()) {
1798 // HAL Pixel Format for output buffer
1799 output_format = input_parcel->readInt32();
1800 }
1801 if (input_parcel->dataPosition() != input_parcel->dataSize()) {
1802 // Option to dump Layer Mixer output (0) or DSPP output (1)
1803 post_processed = (input_parcel->readInt32() != 0);
1804 }
1805
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301806 android::status_t status = 0;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001807
Mathew Joseph Karimpanal0a2fff52018-10-17 12:11:07 +05301808 for (uint32_t i = 0; i < bit_mask_display_type.size(); i++) {
1809 if (!bit_mask_display_type[i]) {
1810 continue;
1811 }
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05301812 int disp_idx = GetDisplayIndex(INT(i));
1813 if (disp_idx == -1) {
1814 continue;
1815 }
1816 SEQUENCE_WAIT_SCOPE_LOCK(locker_[disp_idx]);
1817 auto &hwc_display = hwc_display_[disp_idx];
1818 if (!hwc_display) {
1819 DLOGW("Display = %d is not connected.", disp_idx);
1820 status = (status)? status : -ENODEV; // Return higher priority error.
1821 continue;
1822 }
1823
Sushil Chauhan267a6192018-06-06 18:41:47 -07001824 HWC2::Error error = hwc_display->SetFrameDumpConfig(frame_dump_count, bit_mask_layer_type,
1825 output_format, post_processed);
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05301826 if (error != HWC2::Error::None) {
1827 status = (HWC2::Error::NoResources == error) ? -ENOMEM : -EINVAL;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001828 }
1829 }
1830
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301831 return status;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001832}
1833
Arun Kumar K.R8da7f502016-06-07 17:45:50 -07001834android::status_t HWCSession::SetMixerResolution(const android::Parcel *input_parcel) {
1835 DisplayError error = kErrorNone;
1836 uint32_t dpy = UINT32(input_parcel->readInt32());
1837
1838 if (dpy != HWC_DISPLAY_PRIMARY) {
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301839 DLOGW("Resolution change not supported for this display = %d", dpy);
Arun Kumar K.R8da7f502016-06-07 17:45:50 -07001840 return -EINVAL;
1841 }
1842
Saurabh Shahc5c687a2017-08-24 16:30:14 -07001843 SEQUENCE_WAIT_SCOPE_LOCK(locker_[HWC_DISPLAY_PRIMARY]);
Arun Kumar K.R8da7f502016-06-07 17:45:50 -07001844 if (!hwc_display_[HWC_DISPLAY_PRIMARY]) {
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05301845 DLOGW("Primary display is not initialized");
1846 return -ENODEV;
Arun Kumar K.R8da7f502016-06-07 17:45:50 -07001847 }
1848
1849 uint32_t width = UINT32(input_parcel->readInt32());
1850 uint32_t height = UINT32(input_parcel->readInt32());
1851
1852 error = hwc_display_[HWC_DISPLAY_PRIMARY]->SetMixerResolution(width, height);
1853 if (error != kErrorNone) {
1854 return -EINVAL;
1855 }
1856
1857 return 0;
1858}
1859
Naseer Ahmedad495442016-06-27 14:59:18 -04001860android::status_t HWCSession::SetColorModeOverride(const android::Parcel *input_parcel) {
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05301861 int display = static_cast<int>(input_parcel->readInt32());
Naseer Ahmede7a77982018-06-04 10:56:04 -04001862 auto mode = static_cast<ColorMode>(input_parcel->readInt32());
Saurabh Shahc5c687a2017-08-24 16:30:14 -07001863
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05301864 int disp_idx = GetDisplayIndex(display);
1865 if (disp_idx == -1) {
1866 DLOGE("Invalid display = %d", display);
1867 return -EINVAL;
1868 }
1869
Gousemoodhin Nadaf0e566872019-05-13 16:14:54 +05301870 if (mode < ColorMode::NATIVE || mode > ColorMode::DISPLAY_BT2020) {
Gurpreet Singh Dhamiec0fa512018-08-29 16:51:26 -04001871 DLOGE("Invalid ColorMode: %d", mode);
1872 return HWC2_ERROR_BAD_PARAMETER;
1873 }
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301874 auto err = CallDisplayFunction(static_cast<hwc2_display_t>(disp_idx), &HWCDisplay::SetColorMode,
1875 mode);
Naseer Ahmedad495442016-06-27 14:59:18 -04001876 if (err != HWC2_ERROR_NONE)
1877 return -EINVAL;
Saurabh Shah04f932b2017-10-12 18:47:45 -07001878
Naseer Ahmedad495442016-06-27 14:59:18 -04001879 return 0;
1880}
1881
Yuchao Ma577f0f72018-07-09 11:20:00 +08001882android::status_t HWCSession::SetAd4RoiConfig(const android::Parcel *input_parcel) {
1883 auto display_id = static_cast<uint32_t>(input_parcel->readInt32());
1884 auto h_s = static_cast<uint32_t>(input_parcel->readInt32());
1885 auto h_e = static_cast<uint32_t>(input_parcel->readInt32());
1886 auto v_s = static_cast<uint32_t>(input_parcel->readInt32());
1887 auto v_e = static_cast<uint32_t>(input_parcel->readInt32());
1888 auto f_in = static_cast<uint32_t>(input_parcel->readInt32());
1889 auto f_out = static_cast<uint32_t>(input_parcel->readInt32());
1890
Padmanabhan Komanduru9e971622020-02-10 13:09:34 +05301891 return static_cast<android::status_t>(SetDisplayDppsAdROI(display_id, h_s, h_e, v_s,
1892 v_e, f_in, f_out));
Yuchao Ma577f0f72018-07-09 11:20:00 +08001893}
1894
Xu Yang9dacc872018-12-25 11:04:28 +08001895android::status_t HWCSession::SetFrameTriggerMode(const android::Parcel *input_parcel) {
1896 auto display_id = static_cast<int>(input_parcel->readInt32());
1897 auto mode = static_cast<uint32_t>(input_parcel->readInt32());
1898
1899 int disp_idx = GetDisplayIndex(display_id);
1900 if (disp_idx == -1) {
1901 DLOGE("Invalid display = %d", display_id);
1902 return -EINVAL;
1903 }
1904
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301905 auto err = CallDisplayFunction(static_cast<hwc2_display_t>(disp_idx),
Xu Yang9dacc872018-12-25 11:04:28 +08001906 &HWCDisplay::SetFrameTriggerMode, mode);
1907 if (err != HWC2_ERROR_NONE)
1908 return -EINVAL;
1909
1910 return 0;
1911}
1912
Naseer Ahmede7a77982018-06-04 10:56:04 -04001913android::status_t HWCSession::SetColorModeWithRenderIntentOverride(
1914 const android::Parcel *input_parcel) {
1915 auto display = static_cast<hwc2_display_t>(input_parcel->readInt32());
1916 auto mode = static_cast<ColorMode>(input_parcel->readInt32());
Xu Yang47f44092019-02-13 14:09:53 +08001917 auto int_intent = static_cast<int>(input_parcel->readInt32());
Naseer Ahmede7a77982018-06-04 10:56:04 -04001918
Gousemoodhin Nadaf0e566872019-05-13 16:14:54 +05301919 if (mode < ColorMode::NATIVE || mode > ColorMode::DISPLAY_BT2020) {
Gurpreet Singh Dhamiec0fa512018-08-29 16:51:26 -04001920 DLOGE("Invalid ColorMode: %d", mode);
1921 return HWC2_ERROR_BAD_PARAMETER;
1922 }
1923
Xu Yang47f44092019-02-13 14:09:53 +08001924 if ((int_intent < 0) || (int_intent > MAX_EXTENDED_RENDER_INTENT)) {
1925 DLOGE("Invalid RenderIntent: %d", int_intent);
Gurpreet Singh Dhamiec0fa512018-08-29 16:51:26 -04001926 return HWC2_ERROR_BAD_PARAMETER;
1927 }
1928
Xu Yang47f44092019-02-13 14:09:53 +08001929 auto intent = static_cast<RenderIntent>(int_intent);
Naseer Ahmede7a77982018-06-04 10:56:04 -04001930 auto err =
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301931 CallDisplayFunction(display, &HWCDisplay::SetColorModeWithRenderIntent, mode, intent);
Naseer Ahmede7a77982018-06-04 10:56:04 -04001932 if (err != HWC2_ERROR_NONE)
1933 return -EINVAL;
1934
1935 return 0;
1936}
Naseer Ahmed6776dae2017-05-09 11:49:41 -04001937android::status_t HWCSession::SetColorModeById(const android::Parcel *input_parcel) {
Dileep Marchya35e27532018-06-29 10:50:16 +05301938 int display = input_parcel->readInt32();
Naseer Ahmed6776dae2017-05-09 11:49:41 -04001939 auto mode = input_parcel->readInt32();
Naseer Ahmedab768202017-07-20 12:20:27 -04001940
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05301941 int disp_idx = GetDisplayIndex(display);
1942 if (disp_idx == -1) {
1943 DLOGE("Invalid display = %d", display);
1944 return -EINVAL;
1945 }
1946
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301947 auto err = CallDisplayFunction(static_cast<hwc2_display_t>(disp_idx),
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05301948 &HWCDisplay::SetColorModeById, mode);
Naseer Ahmed6776dae2017-05-09 11:49:41 -04001949 if (err != HWC2_ERROR_NONE)
1950 return -EINVAL;
Saurabh Shah04f932b2017-10-12 18:47:45 -07001951
Naseer Ahmed6776dae2017-05-09 11:49:41 -04001952 return 0;
1953}
1954
Qing Huangda3a91d2019-01-15 14:34:11 +08001955android::status_t HWCSession::SetColorModeFromClient(const android::Parcel *input_parcel) {
1956 int display = input_parcel->readInt32();
1957 auto mode = input_parcel->readInt32();
Qing Huangda3a91d2019-01-15 14:34:11 +08001958
1959 int disp_idx = GetDisplayIndex(display);
1960 if (disp_idx == -1) {
1961 DLOGE("Invalid display = %d", display);
1962 return -EINVAL;
1963 }
1964
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05301965 auto err = CallDisplayFunction(static_cast<hwc2_display_t>(disp_idx),
Qing Huangda3a91d2019-01-15 14:34:11 +08001966 &HWCDisplay::SetColorModeFromClientApi, mode);
1967 if (err != HWC2_ERROR_NONE)
1968 return -EINVAL;
1969
Varun Aroradff7d8d2019-08-05 18:12:43 -07001970 callbacks_.Refresh(static_cast<hwc2_display_t>(disp_idx));
Qing Huangda3a91d2019-01-15 14:34:11 +08001971
1972 return 0;
1973}
1974
Dileep Marchya35e27532018-06-29 10:50:16 +05301975android::status_t HWCSession::RefreshScreen(const android::Parcel *input_parcel) {
1976 int display = input_parcel->readInt32();
1977
1978 int disp_idx = GetDisplayIndex(display);
1979 if (disp_idx == -1) {
1980 DLOGE("Invalid display = %d", display);
1981 return -EINVAL;
1982 }
1983
Varun Aroradff7d8d2019-08-05 18:12:43 -07001984 callbacks_.Refresh(static_cast<hwc2_display_t>(disp_idx));
Dileep Marchya35e27532018-06-29 10:50:16 +05301985
1986 return 0;
1987}
1988
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05001989void HWCSession::DynamicDebug(const android::Parcel *input_parcel) {
1990 int type = input_parcel->readInt32();
1991 bool enable = (input_parcel->readInt32() > 0);
1992 DLOGI("type = %d enable = %d", type, enable);
1993 int verbose_level = input_parcel->readInt32();
1994
1995 switch (type) {
1996 case qService::IQService::DEBUG_ALL:
1997 HWCDebugHandler::DebugAll(enable, verbose_level);
1998 break;
1999
2000 case qService::IQService::DEBUG_MDPCOMP:
2001 HWCDebugHandler::DebugStrategy(enable, verbose_level);
2002 HWCDebugHandler::DebugCompManager(enable, verbose_level);
2003 break;
2004
2005 case qService::IQService::DEBUG_PIPE_LIFECYCLE:
2006 HWCDebugHandler::DebugResources(enable, verbose_level);
Varun Aroracc301f12018-12-13 14:32:24 -08002007 HWCDebugHandler::DebugQos(enable, verbose_level);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002008 break;
2009
2010 case qService::IQService::DEBUG_DRIVER_CONFIG:
2011 HWCDebugHandler::DebugDriverConfig(enable, verbose_level);
2012 break;
2013
2014 case qService::IQService::DEBUG_ROTATOR:
2015 HWCDebugHandler::DebugResources(enable, verbose_level);
2016 HWCDebugHandler::DebugDriverConfig(enable, verbose_level);
2017 HWCDebugHandler::DebugRotator(enable, verbose_level);
Varun Aroracc301f12018-12-13 14:32:24 -08002018 HWCDebugHandler::DebugQos(enable, verbose_level);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002019 break;
2020
2021 case qService::IQService::DEBUG_QDCM:
2022 HWCDebugHandler::DebugQdcm(enable, verbose_level);
2023 break;
2024
Rohit Kulkarniaa5111b2017-06-13 10:26:39 -07002025 case qService::IQService::DEBUG_SCALAR:
2026 HWCDebugHandler::DebugScalar(enable, verbose_level);
2027 break;
2028
Saurabh Dubeyd90a6a42017-10-24 16:28:01 +05302029 case qService::IQService::DEBUG_CLIENT:
2030 HWCDebugHandler::DebugClient(enable, verbose_level);
2031 break;
2032
2033 case qService::IQService::DEBUG_DISPLAY:
2034 HWCDebugHandler::DebugDisplay(enable, verbose_level);
2035 break;
2036
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002037 default:
2038 DLOGW("type = %d is not supported", type);
2039 }
2040}
2041
Xu Yang2a979af2018-05-07 17:10:49 +08002042android::status_t HWCSession::QdcmCMDDispatch(uint32_t display_id,
2043 const PPDisplayAPIPayload &req_payload,
2044 PPDisplayAPIPayload *resp_payload,
2045 PPPendingParams *pending_action) {
2046 int ret = 0;
2047 bool is_physical_display = false;
2048
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07002049 if (display_id >= HWCCallbacks::kNumDisplays || !hwc_display_[display_id]) {
Xu Yang2a979af2018-05-07 17:10:49 +08002050 DLOGW("Invalid display id or display = %d is not connected.", display_id);
2051 return -ENODEV;
2052 }
2053
2054 if (display_id == map_info_primary_.client_id) {
2055 is_physical_display = true;
2056 } else {
2057 for (auto &map_info : map_info_builtin_) {
2058 if (map_info.client_id == display_id) {
2059 is_physical_display = true;
2060 break;
2061 }
2062 }
2063 }
2064
2065 if (!is_physical_display) {
2066 DLOGW("Skipping QDCM command dispatch on display = %d", display_id);
2067 return ret;
2068 }
2069
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05302070 ret = hwc_display_[display_id]->ColorSVCRequestRoute(req_payload, resp_payload, pending_action);
Xu Yang2a979af2018-05-07 17:10:49 +08002071
2072 return ret;
2073}
2074
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002075android::status_t HWCSession::QdcmCMDHandler(const android::Parcel *input_parcel,
2076 android::Parcel *output_parcel) {
2077 int ret = 0;
Varun Arora75c05f02019-05-14 14:53:37 -07002078 float *brightness = NULL;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002079 uint32_t display_id(0);
2080 PPPendingParams pending_action;
2081 PPDisplayAPIPayload resp_payload, req_payload;
Xu Yang2a979af2018-05-07 17:10:49 +08002082 uint8_t *disp_id = NULL;
Qing Huang02285c32018-12-06 16:30:38 +08002083 int32_t *mode_id = NULL;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002084
2085 if (!color_mgr_) {
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05302086 DLOGW("color_mgr_ not initialized.");
2087 return -ENOENT;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002088 }
2089
2090 pending_action.action = kNoAction;
2091 pending_action.params = NULL;
2092
2093 // Read display_id, payload_size and payload from in_parcel.
2094 ret = HWCColorManager::CreatePayloadFromParcel(*input_parcel, &display_id, &req_payload);
2095 if (!ret) {
Xu Yang2a979af2018-05-07 17:10:49 +08002096 ret = QdcmCMDDispatch(display_id, req_payload, &resp_payload, &pending_action);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002097 }
2098
2099 if (ret) {
2100 output_parcel->writeInt32(ret); // first field in out parcel indicates return code.
2101 req_payload.DestroyPayload();
2102 resp_payload.DestroyPayload();
2103 return ret;
2104 }
2105
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05302106 if (kNoAction != pending_action.action) {
Ch Ganesh Kumar5d43ff62017-10-13 19:01:47 +05302107 int32_t action = pending_action.action;
2108 int count = -1;
2109 while (action > 0) {
2110 count++;
2111 int32_t bit = (action & 1);
2112 action = action >> 1;
2113
2114 if (!bit)
2115 continue;
2116
Xu Yang2a979af2018-05-07 17:10:49 +08002117 DLOGV_IF(kTagQDCM, "pending action = %d, display_id = %d", BITMAP(count), display_id);
Ch Ganesh Kumar5d43ff62017-10-13 19:01:47 +05302118 switch (BITMAP(count)) {
2119 case kInvalidating:
Ray Zhang2ee82472019-09-05 02:53:29 +08002120 callbacks_.Refresh(display_id);
Ch Ganesh Kumar5d43ff62017-10-13 19:01:47 +05302121 break;
2122 case kEnterQDCMMode:
Xu Yang2a979af2018-05-07 17:10:49 +08002123 ret = color_mgr_->EnableQDCMMode(true, hwc_display_[display_id]);
Ch Ganesh Kumar5d43ff62017-10-13 19:01:47 +05302124 break;
2125 case kExitQDCMMode:
Xu Yang2a979af2018-05-07 17:10:49 +08002126 ret = color_mgr_->EnableQDCMMode(false, hwc_display_[display_id]);
Ch Ganesh Kumar5d43ff62017-10-13 19:01:47 +05302127 break;
2128 case kApplySolidFill:
Gopikrishnaiah Anandana340b702018-01-16 15:37:15 -08002129 {
Xu Yang2a979af2018-05-07 17:10:49 +08002130 SCOPE_LOCK(locker_[display_id]);
Gopikrishnaiah Anandana340b702018-01-16 15:37:15 -08002131 ret = color_mgr_->SetSolidFill(pending_action.params,
Xu Yang2a979af2018-05-07 17:10:49 +08002132 true, hwc_display_[display_id]);
Gopikrishnaiah Anandana340b702018-01-16 15:37:15 -08002133 }
Varun Aroradff7d8d2019-08-05 18:12:43 -07002134 callbacks_.Refresh(display_id);
Gopikrishnaiah Anandana340b702018-01-16 15:37:15 -08002135 usleep(kSolidFillDelay);
Ch Ganesh Kumar5d43ff62017-10-13 19:01:47 +05302136 break;
2137 case kDisableSolidFill:
Gopikrishnaiah Anandana340b702018-01-16 15:37:15 -08002138 {
Xu Yang2a979af2018-05-07 17:10:49 +08002139 SCOPE_LOCK(locker_[display_id]);
Gopikrishnaiah Anandana340b702018-01-16 15:37:15 -08002140 ret = color_mgr_->SetSolidFill(pending_action.params,
Xu Yang2a979af2018-05-07 17:10:49 +08002141 false, hwc_display_[display_id]);
Gopikrishnaiah Anandana340b702018-01-16 15:37:15 -08002142 }
Varun Aroradff7d8d2019-08-05 18:12:43 -07002143 callbacks_.Refresh(display_id);
Gopikrishnaiah Anandana340b702018-01-16 15:37:15 -08002144 usleep(kSolidFillDelay);
Ch Ganesh Kumar5d43ff62017-10-13 19:01:47 +05302145 break;
2146 case kSetPanelBrightness:
Varun Arora75c05f02019-05-14 14:53:37 -07002147 ret = -EINVAL;
2148 brightness = reinterpret_cast<float *>(resp_payload.payload);
2149 if (brightness == NULL) {
2150 DLOGE("Brightness payload is Null");
Ch Ganesh Kumar5d43ff62017-10-13 19:01:47 +05302151 } else {
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05302152 ret = INT(SetDisplayBrightness(static_cast<hwc2_display_t>(display_id), *brightness));
Ch Ganesh Kumar5d43ff62017-10-13 19:01:47 +05302153 }
2154 break;
2155 case kEnableFrameCapture:
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05302156 ret = color_mgr_->SetFrameCapture(pending_action.params, true, hwc_display_[display_id]);
Varun Aroradff7d8d2019-08-05 18:12:43 -07002157 callbacks_.Refresh(display_id);
Ch Ganesh Kumar5d43ff62017-10-13 19:01:47 +05302158 break;
2159 case kDisableFrameCapture:
2160 ret = color_mgr_->SetFrameCapture(pending_action.params, false,
Xu Yang2a979af2018-05-07 17:10:49 +08002161 hwc_display_[display_id]);
Ch Ganesh Kumar5d43ff62017-10-13 19:01:47 +05302162 break;
2163 case kConfigureDetailedEnhancer:
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05302164 ret = color_mgr_->SetDetailedEnhancer(pending_action.params, hwc_display_[display_id]);
Varun Aroradff7d8d2019-08-05 18:12:43 -07002165 callbacks_.Refresh(display_id);
Ch Ganesh Kumar5d43ff62017-10-13 19:01:47 +05302166 break;
2167 case kModeSet:
2168 ret = static_cast<int>
Xu Yang2a979af2018-05-07 17:10:49 +08002169 (hwc_display_[display_id]->RestoreColorTransform());
Varun Aroradff7d8d2019-08-05 18:12:43 -07002170 callbacks_.Refresh(display_id);
Ch Ganesh Kumar5d43ff62017-10-13 19:01:47 +05302171 break;
2172 case kNoAction:
2173 break;
Xu Yang2a979af2018-05-07 17:10:49 +08002174 case kMultiDispProc:
2175 for (auto &map_info : map_info_builtin_) {
2176 uint32_t id = UINT32(map_info.client_id);
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07002177 if (id < HWCCallbacks::kNumDisplays && hwc_display_[id]) {
Xu Yang2a979af2018-05-07 17:10:49 +08002178 int result = 0;
2179 resp_payload.DestroyPayload();
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05302180 result = hwc_display_[id]->ColorSVCRequestRoute(req_payload, &resp_payload,
Xu Yang2a979af2018-05-07 17:10:49 +08002181 &pending_action);
2182 if (result) {
2183 DLOGW("Failed to dispatch action to disp %d ret %d", id, result);
2184 ret = result;
2185 }
2186 }
2187 }
2188 break;
2189 case kMultiDispGetId:
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07002190 ret = resp_payload.CreatePayloadBytes(HWCCallbacks::kNumDisplays, &disp_id);
Xu Yang2a979af2018-05-07 17:10:49 +08002191 if (ret) {
2192 DLOGW("Unable to create response payload!");
2193 } else {
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07002194 for (int i = 0; i < HWCCallbacks::kNumDisplays; i++) {
2195 disp_id[i] = HWCCallbacks::kNumDisplays;
Xu Yang2a979af2018-05-07 17:10:49 +08002196 }
2197 if (hwc_display_[HWC_DISPLAY_PRIMARY]) {
2198 disp_id[HWC_DISPLAY_PRIMARY] = HWC_DISPLAY_PRIMARY;
2199 }
2200 for (auto &map_info : map_info_builtin_) {
2201 uint64_t id = map_info.client_id;
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07002202 if (id < HWCCallbacks::kNumDisplays && hwc_display_[id]) {
Xu Yang2a979af2018-05-07 17:10:49 +08002203 disp_id[id] = (uint8_t)id;
2204 }
2205 }
2206 }
2207 break;
Qing Huang02285c32018-12-06 16:30:38 +08002208 case kSetModeFromClient:
2209 {
2210 SCOPE_LOCK(locker_[display_id]);
2211 mode_id = reinterpret_cast<int32_t *>(resp_payload.payload);
2212 if (mode_id) {
2213 ret = static_cast<int>(hwc_display_[display_id]->SetColorModeFromClientApi(*mode_id));
2214 } else {
2215 DLOGE("mode_id is Null");
2216 ret = -EINVAL;
2217 }
2218 }
2219 if (!ret) {
Varun Aroradff7d8d2019-08-05 18:12:43 -07002220 callbacks_.Refresh(display_id);
Qing Huang02285c32018-12-06 16:30:38 +08002221 }
2222 break;
Ch Ganesh Kumar5d43ff62017-10-13 19:01:47 +05302223 default:
2224 DLOGW("Invalid pending action = %d!", pending_action.action);
2225 break;
2226 }
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05302227 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002228 }
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002229 // for display API getter case, marshall returned params into out_parcel.
2230 output_parcel->writeInt32(ret);
2231 HWCColorManager::MarshallStructIntoParcel(resp_payload, output_parcel);
2232 req_payload.DestroyPayload();
2233 resp_payload.DestroyPayload();
Sushil Chauhand6429af2018-11-15 11:34:27 -08002234
2235 SEQUENCE_WAIT_SCOPE_LOCK(locker_[display_id]);
Ray Zhang2ee82472019-09-05 02:53:29 +08002236 hwc_display_[display_id]->ResetValidation();
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002237
Mathew Joseph Karimpanaldbd64f82017-10-06 10:13:38 +05302238 return ret;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002239}
2240
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302241int GetEventValue(const char *uevent_data, int length, const char *event_info) {
2242 const char *iterator_str = uevent_data;
2243 while (((iterator_str - uevent_data) <= length) && (*iterator_str)) {
2244 const char *pstr = strstr(iterator_str, event_info);
2245 if (pstr != NULL) {
2246 return (atoi(iterator_str + strlen(event_info)));
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002247 }
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302248 iterator_str += strlen(iterator_str) + 1;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002249 }
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302250
2251 return -1;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002252}
2253
Prabhanjan Kandula02367712017-08-10 20:09:25 -07002254const char *GetTokenValue(const char *uevent_data, int length, const char *token) {
Prabhanjan Kandula34ffec02017-04-25 11:56:39 -07002255 const char *iterator_str = uevent_data;
Prabhanjan Kandula34ffec02017-04-25 11:56:39 -07002256 const char *pstr = NULL;
2257 while (((iterator_str - uevent_data) <= length) && (*iterator_str)) {
Prabhanjan Kandula02367712017-08-10 20:09:25 -07002258 pstr = strstr(iterator_str, token);
2259 if (pstr) {
Prabhanjan Kandula34ffec02017-04-25 11:56:39 -07002260 break;
2261 }
2262 iterator_str += strlen(iterator_str) + 1;
2263 }
2264
Prabhanjan Kandula02367712017-08-10 20:09:25 -07002265 if (pstr)
2266 pstr = pstr+strlen(token);
2267
2268 return pstr;
2269}
2270
Pullakavi Srinivas9189e602018-12-19 16:58:07 +05302271android::status_t HWCSession::SetDsiClk(const android::Parcel *input_parcel) {
2272 int disp_id = input_parcel->readInt32();
2273 uint64_t clk = UINT64(input_parcel->readInt64());
Dileep Marchyaafa45bc2019-06-21 17:05:31 +05302274 if (disp_id < 0) {
2275 return -EINVAL;
2276 }
2277
2278 SEQUENCE_WAIT_SCOPE_LOCK(locker_[disp_id]);
2279 if (!hwc_display_[disp_id]) {
Pullakavi Srinivas9189e602018-12-19 16:58:07 +05302280 return -EINVAL;
2281 }
2282
2283 return hwc_display_[disp_id]->SetDynamicDSIClock(clk);
2284}
2285
2286android::status_t HWCSession::GetDsiClk(const android::Parcel *input_parcel,
2287 android::Parcel *output_parcel) {
2288 int disp_id = input_parcel->readInt32();
Dileep Marchyaafa45bc2019-06-21 17:05:31 +05302289 if (disp_id < 0) {
2290 return -EINVAL;
2291 }
2292
2293 SEQUENCE_WAIT_SCOPE_LOCK(locker_[disp_id]);
2294 if (!hwc_display_[disp_id]) {
Pullakavi Srinivas9189e602018-12-19 16:58:07 +05302295 return -EINVAL;
2296 }
2297
2298 uint64_t bitrate = 0;
2299 hwc_display_[disp_id]->GetDynamicDSIClock(&bitrate);
2300 output_parcel->writeUint64(bitrate);
2301
2302 return 0;
2303}
2304
2305android::status_t HWCSession::GetSupportedDsiClk(const android::Parcel *input_parcel,
2306 android::Parcel *output_parcel) {
2307 int disp_id = input_parcel->readInt32();
Dileep Marchyaafa45bc2019-06-21 17:05:31 +05302308 if (disp_id < 0) {
2309 return -EINVAL;
2310 }
2311
2312 SCOPE_LOCK(locker_[disp_id]);
2313 if (!hwc_display_[disp_id]) {
Pullakavi Srinivas9189e602018-12-19 16:58:07 +05302314 return -EINVAL;
2315 }
2316
2317 std::vector<uint64_t> bit_rates;
2318 hwc_display_[disp_id]->GetSupportedDSIClock(&bit_rates);
2319 output_parcel->writeInt32(INT32(bit_rates.size()));
2320 for (auto &bit_rate : bit_rates) {
2321 output_parcel->writeUint64(bit_rate);
2322 }
2323
2324 return 0;
2325}
2326
Archit Srivastavaa59c4ff2019-05-20 10:54:22 +05302327android::status_t HWCSession::SetPanelLuminanceAttributes(const android::Parcel *input_parcel) {
2328 int disp_id = input_parcel->readInt32();
2329
2330 // currently doing only for virtual display
2331 if (disp_id != qdutils::DISPLAY_VIRTUAL) {
2332 return -EINVAL;
2333 }
2334
Padmanabhan Komandurue4d31fc2019-09-12 19:13:17 +05302335 float min_lum = input_parcel->readFloat();
2336 float max_lum = input_parcel->readFloat();
2337
2338 // check for out of range luminance values
2339 if (min_lum <= 0.0f || min_lum >= 1.0f || max_lum <= 100.0f || max_lum >= 1000.0f) {
2340 return -EINVAL;
2341 }
2342
Archit Srivastavaa59c4ff2019-05-20 10:54:22 +05302343 std::lock_guard<std::mutex> obj(mutex_lum_);
Padmanabhan Komandurue4d31fc2019-09-12 19:13:17 +05302344 set_min_lum_ = min_lum;
2345 set_max_lum_ = max_lum;
Archit Srivastavaa59c4ff2019-05-20 10:54:22 +05302346 DLOGI("set max_lum %f, min_lum %f", set_max_lum_, set_min_lum_);
2347
2348 return 0;
2349}
2350
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302351void HWCSession::UEventHandler(const char *uevent_data, int length) {
Mathew Joseph Karimpanal12892292019-03-21 16:07:59 +05302352 // Drop hotplug uevents until SurfaceFlinger (the client) is connected. The equivalent of hotplug
2353 // uevent handling will be done once when SurfaceFlinger connects, at RegisterCallback(). Since
2354 // HandlePluggableDisplays() reads the latest connection states of all displays, no uevent is
2355 // lost.
Varun Aroradff7d8d2019-08-05 18:12:43 -07002356 if (callbacks_.IsClientConnected() && strcasestr(uevent_data, HWC_UEVENT_DRM_EXT_HOTPLUG)) {
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302357 // MST hotplug will not carry connection status/test pattern etc.
2358 // Pluggable display handler will check all connection status' and take action accordingly.
2359 const char *str_status = GetTokenValue(uevent_data, length, "status=");
2360 const char *str_mst = GetTokenValue(uevent_data, length, "MST_HOTPLUG=");
2361 if (!str_status && !str_mst) {
2362 return;
Ramakant Singhfae59892017-09-13 10:40:22 +05302363 }
Prabhanjan Kandula34ffec02017-04-25 11:56:39 -07002364
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302365 hpd_bpp_ = GetEventValue(uevent_data, length, "bpp=");
2366 hpd_pattern_ = GetEventValue(uevent_data, length, "pattern=");
Varun Arorad366c202018-09-26 14:00:56 -07002367 DLOGI("Uevent = %s, status = %s, MST_HOTPLUG = %s, bpp = %d, pattern = %d", uevent_data,
2368 str_status ? str_status : "NULL", str_mst ? str_mst : "NULL", hpd_bpp_, hpd_pattern_);
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +05302369
2370 hwc2_display_t virtual_display_index =
2371 (hwc2_display_t)GetDisplayIndex(qdutils::DISPLAY_VIRTUAL);
2372
2373 std::bitset<kSecureMax> secure_sessions = 0;
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08002374 hwc2_display_t active_builtin_disp_id = GetActiveBuiltinDisplay();
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07002375 if (active_builtin_disp_id < HWCCallbacks::kNumDisplays) {
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08002376 Locker::ScopeLock lock_a(locker_[active_builtin_disp_id]);
2377 hwc_display_[active_builtin_disp_id]->GetActiveSecureSession(&secure_sessions);
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +05302378 }
2379 if (secure_sessions[kSecureDisplay] || hwc_display_[virtual_display_index]) {
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05302380 // Defer hotplug handling.
2381 SCOPE_LOCK(pluggable_handler_lock_);
2382 DLOGI("Marking hotplug pending...");
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05302383 pending_hotplug_event_ = kHotPlugEvent;
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05302384 } else {
2385 // Handle hotplug.
2386 int32_t err = HandlePluggableDisplays(true);
2387 if (err) {
2388 DLOGW("Hotplug handling failed. Error %d '%s'. Hotplug handling %s.", err,
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05302389 strerror(abs(err)), (pending_hotplug_event_ == kHotPlugEvent) ?
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05302390 "deferred" : "dropped");
2391 }
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302392 }
2393
2394 if (str_status) {
2395 bool connected = (strncmp(str_status, "connected", strlen("connected")) == 0);
2396 DLOGI("Connected = %d", connected);
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +05302397 // Pass on legacy HDMI hot-plug event.
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302398 qservice_->onHdmiHotplug(INT(connected));
2399 }
Prabhanjan Kandula34ffec02017-04-25 11:56:39 -07002400 }
2401}
2402
Naseer Ahmed8584a0f2020-02-19 22:23:30 -05002403int32_t HWCSession::GetVsyncPeriod(hwc2_display_t disp, uint32_t *vsync_period) {
2404 if (disp >= HWCCallbacks::kNumDisplays) {
2405 DLOGW("Invalid Display : display = %" PRIu64, disp);
2406 return HWC2_ERROR_BAD_DISPLAY;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002407 }
2408
Naseer Ahmed8584a0f2020-02-19 22:23:30 -05002409 SCOPE_LOCK(locker_[(int)disp]);
2410 // default value
2411 *vsync_period = 1000000000ul / 60;
2412
2413 if (hwc_display_[disp]) {
2414 hwc_display_[disp]->GetDisplayAttribute(0, HwcAttribute::VSYNC_PERIOD, (int32_t *)vsync_period);
2415 }
2416
2417 return HWC2_ERROR_NONE;
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002418}
2419
Mathew Joseph Karimpanalf9297472019-08-21 15:49:15 +05302420void HWCSession::Refresh(hwc2_display_t display) {
2421 callbacks_.Refresh(display);
2422}
2423
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002424android::status_t HWCSession::GetVisibleDisplayRect(const android::Parcel *input_parcel,
2425 android::Parcel *output_parcel) {
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302426 int disp_idx = GetDisplayIndex(input_parcel->readInt32());
2427 if (disp_idx == -1) {
2428 DLOGE("Invalid display = %d", disp_idx);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002429 return android::BAD_VALUE;
2430 }
2431
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302432 SEQUENCE_WAIT_SCOPE_LOCK(locker_[disp_idx]);
2433 if (!hwc_display_[disp_idx]) {
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002434 return android::NO_INIT;
2435 }
2436
2437 hwc_rect_t visible_rect = {0, 0, 0, 0};
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302438 int error = hwc_display_[disp_idx]->GetVisibleDisplayRect(&visible_rect);
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05002439 if (error < 0) {
2440 return error;
2441 }
2442
2443 output_parcel->writeInt32(visible_rect.left);
2444 output_parcel->writeInt32(visible_rect.top);
2445 output_parcel->writeInt32(visible_rect.right);
2446 output_parcel->writeInt32(visible_rect.bottom);
2447
2448 return android::NO_ERROR;
2449}
2450
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302451int HWCSession::CreatePrimaryDisplay() {
2452 int status = -EINVAL;
2453 HWDisplaysInfo hw_displays_info = {};
Ramakant Singhfae59892017-09-13 10:40:22 +05302454
Varun Aroraad2719a2018-10-25 10:13:53 -07002455 if (null_display_mode_) {
2456 HWDisplayInfo hw_info = {};
2457 hw_info.display_type = kBuiltIn;
2458 hw_info.is_connected = 1;
2459 hw_info.is_primary = 1;
2460 hw_info.is_wb_ubwc_supported = 0;
2461 hw_info.display_id = 1;
2462 hw_displays_info[hw_info.display_id] = hw_info;
2463 } else {
2464 DisplayError error = core_intf_->GetDisplaysStatus(&hw_displays_info);
2465 if (error != kErrorNone) {
2466 DLOGE("Failed to get connected display list. Error = %d", error);
2467 return status;
2468 }
Ramakant Singhfae59892017-09-13 10:40:22 +05302469 }
2470
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302471 for (auto &iter : hw_displays_info) {
2472 auto &info = iter.second;
2473 if (!info.is_primary) {
2474 continue;
2475 }
2476
2477 // todo (user): If primary display is not connected (e.g. hdmi as primary), a NULL display
2478 // need to be created. SF expects primary display hotplug during callback registration unlike
2479 // previous implementation where first hotplug could be notified anytime.
2480 if (!info.is_connected) {
2481 DLOGE("Primary display is not connected. Not supported at present.");
2482 break;
2483 }
2484
2485 auto hwc_display = &hwc_display_[HWC_DISPLAY_PRIMARY];
2486 hwc2_display_t client_id = map_info_primary_.client_id;
2487
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302488 if (info.display_type == kBuiltIn) {
2489 status = HWCDisplayBuiltIn::Create(core_intf_, &buffer_allocator_, &callbacks_, this,
2490 qservice_, client_id, info.display_id, hwc_display);
2491 } else if (info.display_type == kPluggable) {
2492 status = HWCDisplayPluggable::Create(core_intf_, &buffer_allocator_, &callbacks_, this,
2493 qservice_, client_id, info.display_id, 0, 0, false,
2494 hwc_display);
2495 } else {
2496 DLOGE("Spurious primary display type = %d", info.display_type);
2497 break;
2498 }
2499
2500 if (!status) {
Varun Arorad7d8c912019-08-16 11:45:19 -07002501 DLOGI("Created primary display type = %d, sdm id = %d, client id = %d", info.display_type,
Pullakavi Srinivas1ae4acb2019-11-18 19:33:45 +05302502 info.display_id, client_id);
2503 {
2504 SCOPE_LOCK(hdr_locker_[client_id]);
2505 is_hdr_display_[UINT32(client_id)] = HasHDRSupport(*hwc_display);
2506 }
2507
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302508 map_info_primary_.disp_type = info.display_type;
2509 map_info_primary_.sdm_id = info.display_id;
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +05302510 CreateDummyDisplay(HWC_DISPLAY_PRIMARY);
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302511 color_mgr_ = HWCColorManager::CreateColorManager(&buffer_allocator_);
2512 if (!color_mgr_) {
2513 DLOGW("Failed to load HWCColorManager.");
2514 }
2515 } else {
Varun Arorad7d8c912019-08-16 11:45:19 -07002516 DLOGE("Primary display creation has failed! status = %d", status);
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302517 }
2518
2519 // Primary display is found, no need to parse more.
2520 break;
Ramakant Singhfae59892017-09-13 10:40:22 +05302521 }
2522
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302523 return status;
Ramakant Singhfae59892017-09-13 10:40:22 +05302524}
2525
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +05302526void HWCSession::CreateDummyDisplay(hwc2_display_t client_id) {
2527 if (!async_powermode_) {
2528 return;
2529 }
2530
2531 hwc2_display_t dummy_disp_id = map_hwc_display_.find(client_id)->second;
2532 auto hwc_display_dummy = &hwc_display_[dummy_disp_id];
2533 HWCDisplayDummy::Create(core_intf_, &buffer_allocator_, &callbacks_, this, qservice_,
2534 0, 0, hwc_display_dummy);
2535 if (!*hwc_display_dummy) {
2536 DLOGE("Dummy display creation failed for %d display\n", client_id);
2537 }
2538}
2539
Varun Aroraad2719a2018-10-25 10:13:53 -07002540int HWCSession::HandleBuiltInDisplays() {
2541 if (null_display_mode_) {
2542 DLOGW("Skipped BuiltIn display handling in null-display mode");
2543 return 0;
2544 }
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302545
Varun Aroraad2719a2018-10-25 10:13:53 -07002546 HWDisplaysInfo hw_displays_info = {};
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302547 DisplayError error = core_intf_->GetDisplaysStatus(&hw_displays_info);
2548 if (error != kErrorNone) {
2549 DLOGE("Failed to get connected display list. Error = %d", error);
2550 return -EINVAL;
2551 }
2552
2553 int status = 0;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302554 for (auto &iter : hw_displays_info) {
2555 auto &info = iter.second;
2556
2557 // Do not recreate primary display.
2558 if (info.is_primary || info.display_type != kBuiltIn) {
2559 continue;
2560 }
2561
Pullakavi Srinivasf2a31852019-04-23 14:32:08 -07002562 for (auto &map_info : map_info_builtin_) {
2563 hwc2_display_t client_id = map_info.client_id;
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +05302564
Pullakavi Srinivasf2a31852019-04-23 14:32:08 -07002565 {
2566 SCOPE_LOCK(locker_[client_id]);
2567 // Lock confined to this scope
2568 if (hwc_display_[client_id]) {
2569 continue;
2570 }
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302571
Pullakavi Srinivasf2a31852019-04-23 14:32:08 -07002572 DLOGI("Create builtin display, sdm id = %d, client id = %d", info.display_id, client_id);
2573 status = HWCDisplayBuiltIn::Create(core_intf_, &buffer_allocator_, &callbacks_, this,
2574 qservice_, client_id, info.display_id,
2575 &hwc_display_[client_id]);
2576 if (status) {
2577 DLOGE("Builtin display creation failed.");
2578 break;
2579 }
Pullakavi Srinivas1ae4acb2019-11-18 19:33:45 +05302580
2581 {
2582 SCOPE_LOCK(hdr_locker_[client_id]);
2583 is_hdr_display_[UINT32(client_id)] = HasHDRSupport(hwc_display_[client_id]);
2584 }
2585
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +05302586 DLOGI("Builtin display created: sdm id = %d, client id = %d", info.display_id, client_id);
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302587 map_info.disp_type = info.display_type;
2588 map_info.sdm_id = info.display_id;
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +05302589 CreateDummyDisplay(client_id);
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302590 }
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302591
Pullakavi Srinivasf2a31852019-04-23 14:32:08 -07002592 DLOGI("Hotplugging builtin display, sdm id = %d, client id = %d", info.display_id, client_id);
2593 callbacks_.Hotplug(client_id, HWC2::Connection::Connected);
2594 break;
2595 }
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302596 }
2597
2598 return status;
Saurabh Shah04f932b2017-10-12 18:47:45 -07002599}
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302600
Varun Arorad366c202018-09-26 14:00:56 -07002601int HWCSession::HandlePluggableDisplays(bool delay_hotplug) {
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05302602 SCOPE_LOCK(pluggable_handler_lock_);
Varun Aroraad2719a2018-10-25 10:13:53 -07002603 if (null_display_mode_) {
2604 DLOGW("Skipped pluggable display handling in null-display mode");
2605 return 0;
2606 }
2607
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05302608 DLOGI("Handling hotplug...");
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302609 HWDisplaysInfo hw_displays_info = {};
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302610 DisplayError error = core_intf_->GetDisplaysStatus(&hw_displays_info);
2611 if (error != kErrorNone) {
2612 DLOGE("Failed to get connected display list. Error = %d", error);
2613 return -EINVAL;
2614 }
2615
2616 int status = HandleDisconnectedDisplays(&hw_displays_info);
2617 if (status) {
2618 DLOGE("All displays could not be disconnected.");
2619 return status;
2620 }
2621
2622 status = HandleConnectedDisplays(&hw_displays_info, delay_hotplug);
2623 if (status) {
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05302624 switch (status) {
2625 case -EAGAIN:
2626 case -ENODEV:
2627 // Errors like device removal or deferral for which we want to try another hotplug handling.
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05302628 pending_hotplug_event_ = kHotPlugEvent;
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05302629 status = 0;
2630 break;
2631 default:
2632 // Real errors we want to flag and stop hotplug handling.
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05302633 pending_hotplug_event_ = kHotPlugNone;
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05302634 DLOGE("All displays could not be connected. Error %d '%s'.", status, strerror(abs(status)));
2635 }
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05302636 DLOGI("Handling hotplug... %s", (kHotPlugNone == pending_hotplug_event_) ?
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05302637 "Stopped." : "Done. Hotplug events pending.");
2638 return status;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302639 }
2640
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05302641 pending_hotplug_event_ = kHotPlugNone;
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05302642
2643 DLOGI("Handling hotplug... Done.");
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302644 return 0;
2645}
2646
2647int HWCSession::HandleConnectedDisplays(HWDisplaysInfo *hw_displays_info, bool delay_hotplug) {
2648 int status = 0;
2649 std::vector<hwc2_display_t> pending_hotplugs = {};
Padmanabhan Komanduruf5b7f992019-07-08 13:33:44 +05302650 hwc2_display_t client_id = 0;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302651
2652 for (auto &iter : *hw_displays_info) {
2653 auto &info = iter.second;
2654
2655 // Do not recreate primary display or if display is not connected.
2656 if (info.is_primary || info.display_type != kPluggable || !info.is_connected) {
2657 continue;
2658 }
2659
Mathew Joseph Karimpanalbbc700b2018-07-19 19:54:24 +05302660 // Check if we are already using the display.
2661 auto display_used = std::find_if(map_info_pluggable_.begin(), map_info_pluggable_.end(),
2662 [&](auto &p) {
2663 return (p.sdm_id == info.display_id);
2664 });
2665 if (display_used != map_info_pluggable_.end()) {
2666 // Display is already used in a slot.
2667 continue;
2668 }
2669
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05302670 // Count active pluggable display slots and slots with no commits.
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05302671 bool first_commit_pending = false;
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +05302672 std::for_each(map_info_pluggable_.begin(), map_info_pluggable_.end(),
2673 [&](auto &p) {
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05302674 SCOPE_LOCK(locker_[p.client_id]);
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +05302675 if (hwc_display_[p.client_id]) {
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05302676 if (!hwc_display_[p.client_id]->IsFirstCommitDone()) {
2677 DLOGI("Display commit pending on display %d-1", p.sdm_id);
2678 first_commit_pending = true;
2679 }
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +05302680 }
2681 });
2682
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05302683 if (!disable_hotplug_bwcheck_ && first_commit_pending) {
2684 // Hotplug bandwidth check is accomplished by creating and hotplugging a new display after
2685 // a display commit has happened on previous hotplugged displays. This allows the driver to
2686 // return updated modes for the new display based on available link bandwidth.
2687 DLOGI("Pending display commit on one of the displays. Deferring display creation.");
2688 status = -EAGAIN;
Varun Aroradff7d8d2019-08-05 18:12:43 -07002689 if (callbacks_.IsClientConnected()) {
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05302690 // Trigger a display refresh since we depend on PresentDisplay() to handle pending hotplugs.
2691 hwc2_display_t active_builtin_disp_id = GetActiveBuiltinDisplay();
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07002692 if (active_builtin_disp_id >= HWCCallbacks::kNumDisplays) {
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05302693 active_builtin_disp_id = HWC_DISPLAY_PRIMARY;
2694 }
Varun Aroradff7d8d2019-08-05 18:12:43 -07002695 callbacks_.Refresh(active_builtin_disp_id);
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05302696 }
2697 break;
2698 }
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +05302699
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302700 // find an empty slot to create display.
2701 for (auto &map_info : map_info_pluggable_) {
Padmanabhan Komanduruf5b7f992019-07-08 13:33:44 +05302702 client_id = map_info.client_id;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302703
2704 // Lock confined to this scope
2705 {
2706 SCOPE_LOCK(locker_[client_id]);
2707 auto &hwc_display = hwc_display_[client_id];
2708 if (hwc_display) {
Mathew Joseph Karimpanalbbc700b2018-07-19 19:54:24 +05302709 // Display slot is already used.
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302710 continue;
2711 }
2712
2713 DLOGI("Create pluggable display, sdm id = %d, client id = %d", info.display_id, client_id);
2714
2715 // Test pattern generation ?
2716 map_info.test_pattern = (hpd_bpp_ > 0) && (hpd_pattern_ > 0);
Mathew Joseph Karimpanal23e2da02018-11-13 17:44:43 +05302717 int err = 0;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302718 if (!map_info.test_pattern) {
Mathew Joseph Karimpanal23e2da02018-11-13 17:44:43 +05302719 err = HWCDisplayPluggable::Create(core_intf_, &buffer_allocator_,
2720 &callbacks_, this, qservice_, client_id,
2721 info.display_id, 0, 0, false, &hwc_display);
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302722 } else {
Mathew Joseph Karimpanal23e2da02018-11-13 17:44:43 +05302723 err = HWCDisplayPluggableTest::Create(core_intf_, &buffer_allocator_,
2724 &callbacks_, this, qservice_, client_id,
2725 info.display_id, UINT32(hpd_bpp_),
2726 UINT32(hpd_pattern_), &hwc_display);
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302727 }
2728
Mathew Joseph Karimpanal23e2da02018-11-13 17:44:43 +05302729 if (err) {
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05302730 DLOGW("Pluggable display creation failed/aborted. Error %d '%s'.", err,
2731 strerror(abs(err)));
Mathew Joseph Karimpanal23e2da02018-11-13 17:44:43 +05302732 status = err;
2733 // Attempt creating remaining pluggable displays.
2734 break;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302735 }
2736
Pullakavi Srinivas1ae4acb2019-11-18 19:33:45 +05302737 {
2738 SCOPE_LOCK(hdr_locker_[client_id]);
2739 is_hdr_display_[UINT32(client_id)] = HasHDRSupport(hwc_display);
2740 }
2741
Mathew Joseph Karimpanalbbc700b2018-07-19 19:54:24 +05302742 DLOGI("Created pluggable display successfully: sdm id = %d, client id = %d",
2743 info.display_id, client_id);
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +05302744 CreateDummyDisplay(client_id);
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302745 }
2746
2747 map_info.disp_type = info.display_type;
2748 map_info.sdm_id = info.display_id;
2749
2750 pending_hotplugs.push_back((hwc2_display_t)client_id);
2751
2752 // Display is created for this sdm id, move to next connected display.
2753 break;
2754 }
2755 }
2756
2757 // No display was created.
2758 if (!pending_hotplugs.size()) {
Mathew Joseph Karimpanal23e2da02018-11-13 17:44:43 +05302759 return status;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302760 }
2761
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08002762 // Active builtin display needs revalidation
2763 hwc2_display_t active_builtin_disp_id = GetActiveBuiltinDisplay();
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07002764 if (active_builtin_disp_id < HWCCallbacks::kNumDisplays) {
Padmanabhan Komanduruf5b7f992019-07-08 13:33:44 +05302765 WaitForResources(delay_hotplug, active_builtin_disp_id, client_id);
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302766 }
2767
2768 for (auto client_id : pending_hotplugs) {
Mathew Joseph Karimpanalbbc700b2018-07-19 19:54:24 +05302769 DLOGI("Notify hotplug display connected: client id = %d", client_id);
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302770 callbacks_.Hotplug(client_id, HWC2::Connection::Connected);
2771 }
2772
Mathew Joseph Karimpanal23e2da02018-11-13 17:44:43 +05302773 return status;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302774}
2775
Archit Srivastavafe060f72019-05-27 15:51:58 +05302776bool HWCSession::HasHDRSupport(HWCDisplay *hwc_display) {
2777 // query number of hdr types
2778 uint32_t out_num_types = 0;
2779 float out_max_luminance = 0.0f;
2780 float out_max_average_luminance = 0.0f;
2781 float out_min_luminance = 0.0f;
2782 if (hwc_display->GetHdrCapabilities(&out_num_types, nullptr, &out_max_luminance,
2783 &out_max_average_luminance, &out_min_luminance)
2784 != HWC2::Error::None) {
2785 return false;
2786 }
2787
2788 return (out_num_types > 0);
2789}
2790
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302791int HWCSession::HandleDisconnectedDisplays(HWDisplaysInfo *hw_displays_info) {
2792 // Destroy pluggable displays which were connected earlier but got disconnected now.
2793 for (auto &map_info : map_info_pluggable_) {
2794 bool disconnect = true; // disconnect in case display id is not found in list.
2795
2796 for (auto &iter : *hw_displays_info) {
2797 auto &info = iter.second;
2798 if (info.display_id != map_info.sdm_id) {
2799 continue;
2800 }
2801 if (info.is_connected) {
2802 disconnect = false;
2803 }
Mathew Joseph Karimpanalbbc700b2018-07-19 19:54:24 +05302804 break;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302805 }
2806
2807 if (disconnect) {
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +05302808 DestroyDisplay(&map_info);
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302809 }
2810 }
2811
2812 return 0;
2813}
2814
2815void HWCSession::DestroyDisplay(DisplayMapInfo *map_info) {
Ramakant Singh07254e02018-10-31 20:45:01 +05302816 switch (map_info->disp_type) {
2817 case kPluggable:
2818 DestroyPluggableDisplay(map_info);
2819 break;
2820 default:
2821 DestroyNonPluggableDisplay(map_info);
2822 break;
2823 }
2824}
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302825
Ramakant Singh07254e02018-10-31 20:45:01 +05302826void HWCSession::DestroyPluggableDisplay(DisplayMapInfo *map_info) {
2827 hwc2_display_t client_id = map_info->client_id;
2828
2829 DLOGI("Notify hotplug display disconnected: client id = %d", client_id);
2830 callbacks_.Hotplug(client_id, HWC2::Connection::Disconnected);
Rajavenu Kyatham418acc22019-02-25 15:36:25 +05302831
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302832 {
2833 SCOPE_LOCK(locker_[client_id]);
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302834 auto &hwc_display = hwc_display_[client_id];
2835 if (!hwc_display) {
2836 return;
2837 }
Mathew Joseph Karimpanalbbc700b2018-07-19 19:54:24 +05302838 DLOGI("Destroy display %d-%d, client id = %d", map_info->sdm_id, map_info->disp_type,
Ramakant Singh07254e02018-10-31 20:45:01 +05302839 client_id);
Pullakavi Srinivas1ae4acb2019-11-18 19:33:45 +05302840 {
2841 SCOPE_LOCK(hdr_locker_[client_id]);
2842 is_hdr_display_[UINT32(client_id)] = false;
2843 }
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302844
Ramakant Singh07254e02018-10-31 20:45:01 +05302845 if (!map_info->test_pattern) {
2846 HWCDisplayPluggable::Destroy(hwc_display);
2847 } else {
2848 HWCDisplayPluggableTest::Destroy(hwc_display);
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302849 }
Ramakant Singh07254e02018-10-31 20:45:01 +05302850
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +05302851 if (async_powermode_) {
2852 hwc2_display_t dummy_disp_id = map_hwc_display_.find(client_id)->second;
2853 auto &hwc_display_dummy = hwc_display_[dummy_disp_id];
2854 display_ready_.reset(UINT32(dummy_disp_id));
2855 if (hwc_display_dummy) {
2856 HWCDisplayDummy::Destroy(hwc_display_dummy);
2857 hwc_display_dummy = nullptr;
2858 }
2859 }
2860 display_ready_.reset(UINT32(client_id));
Rajavenu Kyathamb0352242019-12-17 12:08:55 +05302861 pending_power_mode_[client_id] = false;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302862 hwc_display = nullptr;
2863 map_info->Reset();
2864 }
Ramakant Singh07254e02018-10-31 20:45:01 +05302865}
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302866
Ramakant Singh07254e02018-10-31 20:45:01 +05302867void HWCSession::DestroyNonPluggableDisplay(DisplayMapInfo *map_info) {
2868 hwc2_display_t client_id = map_info->client_id;
2869
2870 SCOPE_LOCK(locker_[client_id]);
2871 auto &hwc_display = hwc_display_[client_id];
2872 if (!hwc_display) {
2873 return;
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302874 }
Ramakant Singh07254e02018-10-31 20:45:01 +05302875 DLOGI("Destroy display %d-%d, client id = %d", map_info->sdm_id, map_info->disp_type,
2876 client_id);
Pullakavi Srinivas1ae4acb2019-11-18 19:33:45 +05302877 {
2878 SCOPE_LOCK(hdr_locker_[client_id]);
2879 is_hdr_display_[UINT32(client_id)] = false;
2880 }
2881
Ramakant Singh07254e02018-10-31 20:45:01 +05302882 switch (map_info->disp_type) {
2883 case kBuiltIn:
2884 HWCDisplayBuiltIn::Destroy(hwc_display);
2885 break;
2886 default:
Pullakavi Srinivas17495f12019-09-16 20:25:20 +05302887 virtual_display_factory_.Destroy(hwc_display);
Ramakant Singh07254e02018-10-31 20:45:01 +05302888 break;
2889 }
2890
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +05302891 if (async_powermode_ && map_info->disp_type == kBuiltIn) {
2892 hwc2_display_t dummy_disp_id = map_hwc_display_.find(client_id)->second;
2893 auto &hwc_display_dummy = hwc_display_[dummy_disp_id];
2894 display_ready_.reset(UINT32(dummy_disp_id));
2895 if (hwc_display_dummy) {
2896 HWCDisplayDummy::Destroy(hwc_display_dummy);
2897 hwc_display_dummy = nullptr;
2898 }
2899 }
Rajavenu Kyathamb0352242019-12-17 12:08:55 +05302900 pending_power_mode_[client_id] = false;
Ramakant Singh07254e02018-10-31 20:45:01 +05302901 hwc_display = nullptr;
Srinivas Pullakavi3c4337f2019-07-03 11:24:31 +05302902 display_ready_.reset(UINT32(client_id));
Ramakant Singh07254e02018-10-31 20:45:01 +05302903 map_info->Reset();
Dileep Marchyaf3ce11f2018-04-30 23:35:46 +05302904}
Saurabh Shah04f932b2017-10-12 18:47:45 -07002905
Sushil Chauhane3a9fd02017-10-12 18:00:05 -07002906HWC2::Error HWCSession::ValidateDisplayInternal(hwc2_display_t display, uint32_t *out_num_types,
2907 uint32_t *out_num_requests) {
2908 HWCDisplay *hwc_display = hwc_display_[display];
Gurpreet Singh Dhamia4276882019-04-12 10:30:58 -07002909
2910 DTRACE_SCOPED();
Sushil Chauhane3a9fd02017-10-12 18:00:05 -07002911 if (hwc_display->IsInternalValidateState()) {
2912 // Internal Validation has already been done on display, get the Output params.
2913 return hwc_display->GetValidateDisplayOutput(out_num_types, out_num_requests);
2914 }
2915
2916 if (display == HWC_DISPLAY_PRIMARY) {
2917 // TODO(user): This can be moved to HWCDisplayPrimary
Sushil Chauhane3a9fd02017-10-12 18:00:05 -07002918 if (need_invalidate_) {
Varun Aroradff7d8d2019-08-05 18:12:43 -07002919 callbacks_.Refresh(display);
Sushil Chauhane3a9fd02017-10-12 18:00:05 -07002920 need_invalidate_ = false;
2921 }
2922
2923 if (color_mgr_) {
2924 color_mgr_->SetColorModeDetailEnhancer(hwc_display_[display]);
2925 }
2926 }
2927
2928 return hwc_display->Validate(out_num_types, out_num_requests);
2929}
2930
Dileep Marchyaad668432019-12-02 10:44:52 +05302931HWC2::Error HWCSession::PresentDisplayInternal(hwc2_display_t display) {
Sushil Chauhane3a9fd02017-10-12 18:00:05 -07002932 HWCDisplay *hwc_display = hwc_display_[display];
Gurpreet Singh Dhamia4276882019-04-12 10:30:58 -07002933
2934 DTRACE_SCOPED();
Sushil Chauhane3a9fd02017-10-12 18:00:05 -07002935 // If display is in Skip-Validate state and Validate cannot be skipped, do Internal
2936 // Validation to optimize for the frames which don't require the Client composition.
2937 if (hwc_display->IsSkipValidateState() && !hwc_display->CanSkipValidate()) {
2938 uint32_t out_num_types = 0, out_num_requests = 0;
Sushil Chauhan7bd20662018-08-20 17:51:32 -07002939 hwc_display->SetFastPathComposition(true);
Sushil Chauhane3a9fd02017-10-12 18:00:05 -07002940 HWC2::Error error = ValidateDisplayInternal(display, &out_num_types, &out_num_requests);
Sushil Chauhan181d9502018-12-13 16:40:58 -08002941 if ((error != HWC2::Error::None) || hwc_display->HWCClientNeedsValidate()) {
Sushil Chauhane3a9fd02017-10-12 18:00:05 -07002942 hwc_display->SetValidationState(HWCDisplay::kInternalValidate);
Sushil Chauhan7bd20662018-08-20 17:51:32 -07002943 hwc_display->SetFastPathComposition(false);
Sushil Chauhane3a9fd02017-10-12 18:00:05 -07002944 return HWC2::Error::NotValidated;
2945 }
2946 }
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07002947 return HWC2::Error::None;
Sushil Chauhane3a9fd02017-10-12 18:00:05 -07002948}
2949
Varun Arora7c8ee542018-05-01 20:58:16 -07002950void HWCSession::DisplayPowerReset() {
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +05302951 // Acquire lock on all displays.
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07002952 for (hwc2_display_t display = HWC_DISPLAY_PRIMARY;
2953 display < HWCCallbacks::kNumDisplays; display++) {
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +05302954 locker_[display].Lock();
2955 }
Varun Arora7c8ee542018-05-01 20:58:16 -07002956
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +05302957 HWC2::Error status = HWC2::Error::None;
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07002958 HWC2::PowerMode last_power_mode[HWCCallbacks::kNumDisplays] = {};
Varun Arora7c8ee542018-05-01 20:58:16 -07002959
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07002960 for (hwc2_display_t display = HWC_DISPLAY_PRIMARY;
2961 display < HWCCallbacks::kNumDisplays; display++) {
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +05302962 if (hwc_display_[display] != NULL) {
2963 last_power_mode[display] = hwc_display_[display]->GetCurrentPowerMode();
2964 DLOGI("Powering off display = %d", display);
2965 status = hwc_display_[display]->SetPowerMode(HWC2::PowerMode::Off,
2966 true /* teardown */);
2967 if (status != HWC2::Error::None) {
2968 DLOGE("Power off for display = %d failed with error = %d", display, status);
Varun Arora7c8ee542018-05-01 20:58:16 -07002969 }
2970 }
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +05302971 }
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07002972 for (hwc2_display_t display = HWC_DISPLAY_PRIMARY;
2973 display < HWCCallbacks::kNumDisplays; display++) {
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +05302974 if (hwc_display_[display] != NULL) {
2975 HWC2::PowerMode mode = last_power_mode[display];
2976 DLOGI("Setting display %d to mode = %d", display, mode);
2977 status = hwc_display_[display]->SetPowerMode(mode, false /* teardown */);
2978 if (status != HWC2::Error::None) {
2979 DLOGE("%d mode for display = %d failed with error = %d", mode, display, status);
2980 }
2981 ColorMode color_mode = hwc_display_[display]->GetCurrentColorMode();
2982 status = hwc_display_[display]->SetColorMode(color_mode);
2983 if (status != HWC2::Error::None) {
2984 DLOGE("SetColorMode failed for display = %d error = %d", display, status);
Varun Arora7c8ee542018-05-01 20:58:16 -07002985 }
2986 }
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +05302987 }
Varun Arora7c8ee542018-05-01 20:58:16 -07002988
Padmanabhan Komanduruf5e6c5d2019-04-04 13:56:31 +05302989 hwc2_display_t vsync_source = callbacks_.GetVsyncSource();
Pullakavi Srinivasf2a31852019-04-23 14:32:08 -07002990 status = hwc_display_[vsync_source]->SetVsyncEnabled(HWC2::Vsync::Enable);
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +05302991 if (status != HWC2::Error::None) {
Pullakavi Srinivasf2a31852019-04-23 14:32:08 -07002992 DLOGE("Enabling vsync failed for disp: %" PRIu64 " with error = %d", vsync_source, status);
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +05302993 }
2994
2995 // Release lock on all displays.
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07002996 for (hwc2_display_t display = HWC_DISPLAY_PRIMARY;
2997 display < HWCCallbacks::kNumDisplays; display++) {
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +05302998 locker_[display].Unlock();
Varun Arora7c8ee542018-05-01 20:58:16 -07002999 }
Pullakavi Srinivasf2a31852019-04-23 14:32:08 -07003000
Varun Aroradff7d8d2019-08-05 18:12:43 -07003001 callbacks_.Refresh(vsync_source);
Varun Arora7c8ee542018-05-01 20:58:16 -07003002}
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -07003003
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003004void HWCSession::HandleSecureSession() {
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +05303005 std::bitset<kSecureMax> secure_sessions = 0;
3006 {
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05303007 // TODO(user): Revisit if supporting secure display on non-primary.
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003008 hwc2_display_t active_builtin_disp_id = GetActiveBuiltinDisplay();
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07003009 if (active_builtin_disp_id >= HWCCallbacks::kNumDisplays) {
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003010 return;
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +05303011 }
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05303012 Locker::ScopeLock lock_pwr(power_state_[active_builtin_disp_id]);
3013 if (power_state_transition_[active_builtin_disp_id]) {
3014 // Route all interactions with client to dummy display.
3015 active_builtin_disp_id = map_hwc_display_.find(active_builtin_disp_id)->second;
3016 }
3017 Locker::ScopeLock lock_d(locker_[active_builtin_disp_id]);
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003018 hwc_display_[active_builtin_disp_id]->GetActiveSecureSession(&secure_sessions);
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -07003019 }
3020
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05303021 if (secure_sessions.any()) {
3022 secure_session_active_ = true;
3023 } else if (!secure_session_active_) {
3024 // No secure session active. No secure session transition to handle. Skip remaining steps.
3025 return;
3026 }
3027
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003028 // If it is called during primary prepare/commit, we need to pause any ongoing commit on
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -07003029 // external/virtual display.
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07003030 for (hwc2_display_t display = HWC_DISPLAY_PRIMARY;
3031 display < HWCCallbacks::kNumDisplays; display++) {
Mathew Joseph Karimpanal8abb6e02018-11-20 09:52:49 +05303032 Locker::ScopeLock lock_d(locker_[display]);
3033 if (hwc_display_[display]) {
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05303034 hwc_display_[display]->HandleSecureSession(secure_sessions, &pending_power_mode_[display]);
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -07003035 }
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -07003036 }
3037}
3038
Dileep Marchyaad668432019-12-02 10:44:52 +05303039void HWCSession::HandlePendingPowerMode(hwc2_display_t disp_id,
3040 const shared_ptr<Fence> &retire_fence) {
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05303041 if (!secure_session_active_) {
3042 // No secure session active. Skip remaining steps.
3043 return;
3044 }
3045
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003046 hwc2_display_t active_builtin_disp_id = GetActiveBuiltinDisplay();
3047 if (disp_id != active_builtin_disp_id) {
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -07003048 return;
3049 }
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +05303050
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05303051 Locker::ScopeLock lock_d(locker_[active_builtin_disp_id]);
3052 bool pending_power_mode = false;
3053 for (hwc2_display_t display = HWC_DISPLAY_PRIMARY + 1;
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07003054 display < HWCCallbacks::kNumDisplays; display++) {
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003055 if (display != active_builtin_disp_id) {
3056 Locker::ScopeLock lock_d(locker_[display]);
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05303057 if (pending_power_mode_[display]) {
3058 pending_power_mode = true;
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003059 break;
3060 }
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -07003061 }
3062 }
Dileep Marchyaad668432019-12-02 10:44:52 +05303063
3064 if (!pending_power_mode) {
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +05303065 return;
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -07003066 }
3067
Dileep Marchyaad668432019-12-02 10:44:52 +05303068 // retire fence is set only after successful primary commit, So check for retire fence to know
3069 // non secure commit went through to notify driver to change the CRTC mode to non secure.
3070 // Otherwise any commit to non-primary display would fail.
3071 if (retire_fence == nullptr) {
3072 return;
3073 }
3074
3075 Fence::Wait(retire_fence);
3076
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05303077 for (hwc2_display_t display = HWC_DISPLAY_PRIMARY + 1;
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07003078 display < HWCCallbacks::kNumDisplays; display++) {
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003079 if (display != active_builtin_disp_id) {
3080 Locker::ScopeLock lock_d(locker_[display]);
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05303081 if (pending_power_mode_[display] && hwc_display_[display]) {
3082 HWC2::Error error =
3083 hwc_display_[display]->SetPowerMode(hwc_display_[display]->GetPendingPowerMode(), false);
3084 if (HWC2::Error::None == error) {
3085 pending_power_mode_[display] = false;
3086 hwc_display_[display]->ClearPendingPowerMode();
3087 pending_refresh_.set(UINT32(HWC_DISPLAY_PRIMARY));
3088 } else {
3089 DLOGE("SetDisplayStatus error = %d (%s)", error, to_string(error).c_str());
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003090 }
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -07003091 }
3092 }
3093 }
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05303094 secure_session_active_ = false;
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -07003095}
3096
Dileep Marchyaad668432019-12-02 10:44:52 +05303097void HWCSession::HandlePendingHotplug(hwc2_display_t disp_id,
3098 const shared_ptr<Fence> &retire_fence) {
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003099 hwc2_display_t active_builtin_disp_id = GetActiveBuiltinDisplay();
3100 if (disp_id != active_builtin_disp_id ||
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05303101 (kHotPlugNone == pending_hotplug_event_ && !destroy_virtual_disp_pending_)) {
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -07003102 return;
3103 }
Rajavenu Kyathame45d1b72018-06-27 14:46:44 +05303104
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -07003105 std :: bitset < kSecureMax > secure_sessions = 0;
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07003106 if (active_builtin_disp_id < HWCCallbacks::kNumDisplays) {
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05303107 Locker::ScopeLock lock_d(locker_[active_builtin_disp_id]);
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003108 hwc_display_[active_builtin_disp_id]->GetActiveSecureSession(&secure_sessions);
3109 }
3110
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07003111 if (secure_sessions.any() || active_builtin_disp_id >= HWCCallbacks::kNumDisplays) {
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003112 return;
3113 }
3114
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05303115 if (destroy_virtual_disp_pending_ || kHotPlugEvent == pending_hotplug_event_) {
Dileep Marchyaad668432019-12-02 10:44:52 +05303116 Fence::Wait(retire_fence);
3117
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003118 // Destroy the pending virtual display if secure session not present.
3119 if (destroy_virtual_disp_pending_) {
3120 for (auto &map_info : map_info_virtual_) {
3121 DestroyDisplay(&map_info);
3122 destroy_virtual_disp_pending_ = false;
Padmanabhan Komanduruf5b7f992019-07-08 13:33:44 +05303123 virtual_id_ = HWCCallbacks::kNumDisplays;
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003124 }
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -07003125 }
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003126 // Handle connect/disconnect hotplugs if secure session is not present.
3127 hwc2_display_t virtual_display_idx = (hwc2_display_t)GetDisplayIndex(qdutils::DISPLAY_VIRTUAL);
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05303128 if (!hwc_display_[virtual_display_idx] && kHotPlugEvent == pending_hotplug_event_) {
Mathew Joseph Karimpanalabe3b3d2018-11-27 17:25:40 +05303129 // Handle deferred hotplug event.
3130 int32_t err = pluggable_handler_lock_.TryLock();
3131 if (!err) {
3132 // Do hotplug handling in a different thread to avoid blocking PresentDisplay.
3133 std::thread(&HWCSession::HandlePluggableDisplays, this, true).detach();
3134 pluggable_handler_lock_.Unlock();
3135 } else {
3136 // EBUSY means another thread is already handling hotplug. Skip deferred hotplug handling.
3137 if (EBUSY != err) {
3138 DLOGW("Failed to acquire pluggable display handler lock. Error %d '%s'.", err,
3139 strerror(abs(err)));
3140 }
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003141 }
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +05303142 }
Mathew Joseph Karimpanal71ac8a72018-09-25 22:27:22 +05303143 }
Ramkumar Radhakrishnana38b7602018-03-15 14:49:52 -07003144}
3145
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05303146int32_t HWCSession::GetReadbackBufferAttributes(hwc2_display_t display, int32_t *format,
3147 int32_t *dataspace) {
3148 if (!format || !dataspace) {
Sushil Chauhan06521582018-03-19 14:00:23 -07003149 return HWC2_ERROR_BAD_PARAMETER;
3150 }
3151
3152 if (display != HWC_DISPLAY_PRIMARY) {
3153 return HWC2_ERROR_BAD_DISPLAY;
3154 }
3155
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05303156 HWCDisplay *hwc_display = hwc_display_[display];
Venkat Thogaru8e9b1242020-03-30 19:25:24 +05303157 if (hwc_display == nullptr) {
3158 return HWC2_ERROR_BAD_DISPLAY;
3159 } else if (!hwc_display->HasReadBackBufferSupport()) {
3160 return HWC2_ERROR_UNSUPPORTED;
Sushil Chauhanfea3e902018-07-31 15:34:48 -07003161 }
3162
Venkat Thogaru8e9b1242020-03-30 19:25:24 +05303163 *format = HAL_PIXEL_FORMAT_RGB_888;
3164 *dataspace = GetDataspaceFromColorMode(hwc_display->GetCurrentColorMode());
3165
3166 return HWC2_ERROR_NONE;
Sushil Chauhan06521582018-03-19 14:00:23 -07003167}
3168
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05303169int32_t HWCSession::SetReadbackBuffer(hwc2_display_t display, const native_handle_t *buffer,
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +05303170 const shared_ptr<Fence> &acquire_fence) {
Sushil Chauhan06521582018-03-19 14:00:23 -07003171 if (!buffer) {
3172 return HWC2_ERROR_BAD_PARAMETER;
3173 }
3174
3175 if (display != HWC_DISPLAY_PRIMARY) {
3176 return HWC2_ERROR_BAD_DISPLAY;
3177 }
3178
Sushil Chauhan0a1b15d2019-08-23 12:58:18 -07003179 int external_dpy_index = GetDisplayIndex(qdutils::DISPLAY_EXTERNAL);
3180 int virtual_dpy_index = GetDisplayIndex(qdutils::DISPLAY_VIRTUAL);
3181 if (((external_dpy_index != -1) && hwc_display_[external_dpy_index]) ||
3182 ((virtual_dpy_index != -1) && hwc_display_[virtual_dpy_index])) {
Sushil Chauhan062a7a42018-04-25 22:27:52 -07003183 return HWC2_ERROR_UNSUPPORTED;
3184 }
3185
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05303186 return CallDisplayFunction(display, &HWCDisplay::SetReadbackBuffer, buffer, acquire_fence,
Dileep Marchyacb2a3a92019-06-21 17:30:50 +05303187 false, kCWBClientComposer);
Sushil Chauhan06521582018-03-19 14:00:23 -07003188}
3189
Rajavenu Kyatham7338b9e2019-12-30 19:52:16 +05303190int32_t HWCSession::GetReadbackBufferFence(hwc2_display_t display,
3191 shared_ptr<Fence> *release_fence) {
Sushil Chauhan06521582018-03-19 14:00:23 -07003192 if (!release_fence) {
3193 return HWC2_ERROR_BAD_PARAMETER;
3194 }
3195
3196 if (display != HWC_DISPLAY_PRIMARY) {
3197 return HWC2_ERROR_BAD_DISPLAY;
3198 }
3199
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05303200 return CallDisplayFunction(display, &HWCDisplay::GetReadbackBufferFence, release_fence);
Sushil Chauhan06521582018-03-19 14:00:23 -07003201}
3202
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05303203int32_t HWCSession::GetDisplayIdentificationData(hwc2_display_t display, uint8_t *outPort,
3204 uint32_t *outDataSize, uint8_t *outData) {
Varun Arora6ba9eda2019-02-07 17:59:32 -08003205 if (!outPort || !outDataSize) {
3206 return HWC2_ERROR_BAD_PARAMETER;
3207 }
3208
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07003209 if (display >= HWCCallbacks::kNumDisplays) {
Varun Arora6ba9eda2019-02-07 17:59:32 -08003210 return HWC2_ERROR_BAD_DISPLAY;
3211 }
3212
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05303213 return CallDisplayFunction(display, &HWCDisplay::GetDisplayIdentificationData, outPort,
Varun Arora6ba9eda2019-02-07 17:59:32 -08003214 outDataSize, outData);
3215}
3216
Naseer Ahmed8584a0f2020-02-19 22:23:30 -05003217int32_t HWCSession::GetDisplayCapabilities(hwc2_display_t display,
3218 hidl_vec<HwcDisplayCapability> *capabilities) {
3219 if (!capabilities) {
Varun Arora9da54ef2019-05-03 16:17:22 -07003220 return HWC2_ERROR_BAD_PARAMETER;
3221 }
3222
3223 if (display >= HWCCallbacks::kNumDisplays) {
3224 return HWC2_ERROR_BAD_DISPLAY;
3225 }
3226
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05303227 if (!hwc_display_[display]) {
Varun Arora9da54ef2019-05-03 16:17:22 -07003228 DLOGE("Expected valid hwc_display");
3229 return HWC2_ERROR_BAD_PARAMETER;
3230 }
Naseer Ahmed8584a0f2020-02-19 22:23:30 -05003231
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05303232 bool isBuiltin = (hwc_display_[display]->GetDisplayClass() == DISPLAY_CLASS_BUILTIN);
Pullakavi Srinivas5cb87bd2020-04-02 22:30:55 +05303233 int32_t has_doze_support = 0;
3234 GetDozeSupport(display, &has_doze_support);
Naseer Ahmed8584a0f2020-02-19 22:23:30 -05003235 if (isBuiltin) {
3236 capabilities->resize(4);
3237 // TODO(user): Handle SKIP_CLIENT_COLOR_TRANSFORM based on DSPP availability
Pullakavi Srinivas5cb87bd2020-04-02 22:30:55 +05303238 std::vector<HwcDisplayCapability> caps;
3239 if (has_doze_support) {
3240 caps = {
3241 HwcDisplayCapability::SKIP_CLIENT_COLOR_TRANSFORM, HwcDisplayCapability::DOZE,
3242 HwcDisplayCapability::BRIGHTNESS, HwcDisplayCapability::PROTECTED_CONTENTS};
3243 }
3244 else {
3245 caps = {
3246 HwcDisplayCapability::SKIP_CLIENT_COLOR_TRANSFORM,
3247 HwcDisplayCapability::BRIGHTNESS, HwcDisplayCapability::PROTECTED_CONTENTS};
3248 }
Naseer Ahmed8584a0f2020-02-19 22:23:30 -05003249 capabilities->setToExternal(caps.data(), caps.size());
Varun Arora9da54ef2019-05-03 16:17:22 -07003250 }
Naseer Ahmed8584a0f2020-02-19 22:23:30 -05003251 return HWC2_ERROR_NONE;
3252}
3253
3254int32_t HWCSession::GetDisplayConnectionType(hwc2_display_t display,
3255 HwcDisplayConnectionType *type) {
3256 if (display >= HWCCallbacks::kNumDisplays) {
3257 return HWC2_ERROR_BAD_DISPLAY;
3258 }
3259
3260 if (!type) {
3261 return HWC2_ERROR_BAD_PARAMETER;
3262 }
3263
3264 if (!hwc_display_[display]) {
3265 DLOGE("Expected valid hwc_display");
3266 return HWC2_ERROR_BAD_PARAMETER;
3267 }
3268 *type = HwcDisplayConnectionType::EXTERNAL;
3269 if (hwc_display_[display]->GetDisplayClass() == DISPLAY_CLASS_BUILTIN) {
3270 *type = HwcDisplayConnectionType::INTERNAL;
3271 }
3272
3273 return HWC2_ERROR_NONE;
Varun Arora9da54ef2019-05-03 16:17:22 -07003274}
3275
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05303276int32_t HWCSession::GetDisplayBrightnessSupport(hwc2_display_t display, bool *outSupport) {
3277 if (!outSupport) {
Varun Arora9da54ef2019-05-03 16:17:22 -07003278 return HWC2_ERROR_BAD_PARAMETER;
3279 }
3280
3281 if (display >= HWCCallbacks::kNumDisplays) {
3282 return HWC2_ERROR_BAD_DISPLAY;
3283 }
3284
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05303285 if (!hwc_display_[display]) {
Varun Arora9da54ef2019-05-03 16:17:22 -07003286 DLOGE("Expected valid hwc_display");
3287 return HWC2_ERROR_BAD_PARAMETER;
3288 }
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05303289 *outSupport = (hwc_display_[display]->GetDisplayClass() == DISPLAY_CLASS_BUILTIN);
Varun Arora9da54ef2019-05-03 16:17:22 -07003290 return HWC2_ERROR_NONE;
3291}
3292
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05303293int32_t HWCSession::SetDisplayBrightness(hwc2_display_t display, float brightness) {
Sushil Chauhanab3c5c52019-07-12 14:54:29 -07003294 if (display >= HWCCallbacks::kNumDisplays) {
3295 return HWC2_ERROR_BAD_DISPLAY;
3296 }
3297
3298 if (!hwc_display_[display]) {
3299 return HWC2_ERROR_BAD_PARAMETER;
3300 }
3301
3302 return INT32(hwc_display_[display]->SetPanelBrightness(brightness));
Varun Arora75c05f02019-05-14 14:53:37 -07003303}
3304
Sushil Chauhan380a59d2018-03-19 15:47:50 -07003305android::status_t HWCSession::SetQSyncMode(const android::Parcel *input_parcel) {
3306 auto mode = input_parcel->readInt32();
Sushil Chauhan380a59d2018-03-19 15:47:50 -07003307
3308 QSyncMode qsync_mode = kQSyncModeNone;
3309 switch (mode) {
3310 case qService::IQService::QSYNC_MODE_NONE:
3311 qsync_mode = kQSyncModeNone;
3312 break;
3313 case qService::IQService::QSYNC_MODE_CONTINUOUS:
3314 qsync_mode = kQSyncModeContinuous;
3315 break;
3316 case qService::IQService::QSYNC_MODE_ONESHOT:
3317 qsync_mode = kQsyncModeOneShot;
3318 break;
3319 default:
3320 DLOGE("Qsync mode not supported %d", mode);
3321 return -EINVAL;
3322 }
Rajavenu Kyatham42d11cb2019-04-16 13:03:29 +05303323 return CallDisplayFunction(HWC_DISPLAY_PRIMARY, &HWCDisplay::SetQSyncMode, qsync_mode);
Sushil Chauhan380a59d2018-03-19 15:47:50 -07003324}
3325
Mahesh Aiaf3a1f992019-01-16 13:07:42 -08003326void HWCSession::UpdateThrottlingRate() {
3327 uint32_t new_min = 0;
3328
Padmanabhan Komandurue74cf4f2019-04-25 17:38:43 -07003329 for (int i=0; i < HWCCallbacks::kNumDisplays; i++) {
Mahesh Aiaf3a1f992019-01-16 13:07:42 -08003330 auto &display = hwc_display_[i];
3331 if (!display)
3332 continue;
3333 if (display->GetCurrentPowerMode() != HWC2::PowerMode::Off)
3334 new_min = (new_min == 0) ? display->GetMaxRefreshRate() :
3335 std::min(new_min, display->GetMaxRefreshRate());
3336 }
3337
3338 SetNewThrottlingRate(new_min);
3339}
3340
3341void HWCSession::SetNewThrottlingRate(const uint32_t new_rate) {
3342 if (new_rate !=0 && throttling_refresh_rate_ != new_rate) {
3343 HWCDisplay::SetThrottlingRefreshRate(new_rate);
3344 throttling_refresh_rate_ = new_rate;
3345 }
3346}
3347
Ramkumar Radhakrishnanf985d482018-07-23 18:10:41 -07003348android::status_t HWCSession::SetIdlePC(const android::Parcel *input_parcel) {
3349 auto enable = input_parcel->readInt32();
3350 auto synchronous = input_parcel->readInt32();
3351
Padmanabhan Komanduru9e971622020-02-10 13:09:34 +05303352 return static_cast<android::status_t>(ControlIdlePowerCollapse(enable, synchronous));
Ramkumar Radhakrishnanf985d482018-07-23 18:10:41 -07003353}
3354
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003355hwc2_display_t HWCSession::GetActiveBuiltinDisplay() {
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05303356 hwc2_display_t active_display = HWCCallbacks::kNumDisplays;
Mathew Joseph Karimpanal1d5a23f2019-04-25 15:46:11 -07003357 // Get first active display among primary and built-in displays.
3358 std::vector<DisplayMapInfo> map_info = {map_info_primary_};
3359 std::copy(map_info_builtin_.begin(), map_info_builtin_.end(), std::back_inserter(map_info));
3360
3361 for (auto &info : map_info) {
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05303362 hwc2_display_t target_display = info.client_id;
3363 SCOPE_LOCK(power_state_[target_display]);
3364 if (power_state_transition_[target_display]) {
3365 // Route all interactions with client to dummy display.
3366 target_display = map_hwc_display_.find(target_display)->second;
3367 }
3368 Locker::ScopeLock lock_d(locker_[target_display]);
3369 auto &hwc_display = hwc_display_[target_display];
Mathew Joseph Karimpanal1d5a23f2019-04-25 15:46:11 -07003370 if (hwc_display && hwc_display->GetCurrentPowerMode() != HWC2::PowerMode::Off) {
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05303371 active_display = info.client_id;
Mathew Joseph Karimpanal1d5a23f2019-04-25 15:46:11 -07003372 break;
3373 }
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003374 }
Mathew Joseph Karimpanal1d5a23f2019-04-25 15:46:11 -07003375
Mathew Joseph Karimpanalb1c15862019-07-04 11:14:24 +05303376 return active_display;
Ramkumar Radhakrishnan44c64362018-12-12 13:03:59 -08003377}
3378
Xu Yang45d0abf2019-07-05 11:34:06 +08003379int32_t HWCSession::SetDisplayBrightnessScale(const android::Parcel *input_parcel) {
3380 auto display = input_parcel->readInt32();
3381 auto level = input_parcel->readInt32();
3382 if (level < 0 || level > kBrightnessScaleMax) {
3383 DLOGE("Invalid backlight scale level %d", level);
3384 return -EINVAL;
3385 }
3386 auto bl_scale = level * kSvBlScaleMax / kBrightnessScaleMax;
3387 auto error = CallDisplayFunction(display, &HWCDisplay::SetBLScale, (uint32_t)bl_scale);
3388 if (INT32(error) == HWC2_ERROR_NONE) {
3389 callbacks_.Refresh(display);
3390 }
3391
3392 return INT32(error);
3393}
3394
Pullakavi Srinivas2d55f3a2019-07-16 14:49:06 +05303395void HWCSession::NotifyClientStatus(bool connected) {
3396 for (uint32_t i = 0; i < HWCCallbacks::kNumDisplays; i++) {
3397 if (!hwc_display_[i]) {
3398 continue;
3399 }
3400 SCOPE_LOCK(locker_[i]);
3401 hwc_display_[i]->NotifyClientStatus(connected);
3402 }
3403}
3404
Padmanabhan Komanduruf5b7f992019-07-08 13:33:44 +05303405void HWCSession::WaitForResources(bool wait_for_resources, hwc2_display_t active_builtin_id,
3406 hwc2_display_t display_id) {
3407 {
3408 SEQUENCE_WAIT_SCOPE_LOCK(locker_[active_builtin_id]);
3409 hwc_display_[active_builtin_id]->ResetValidation();
3410 }
3411
3412 if (wait_for_resources) {
3413 bool res_wait = true;
3414 do {
3415 if (client_connected_) {
3416 Refresh(active_builtin_id);
3417 }
3418 {
3419 std::unique_lock<std::mutex> caller_lock(hotplug_mutex_);
3420 hotplug_cv_.wait(caller_lock);
3421 }
3422 res_wait = hwc_display_[display_id]->CheckResourceState();
3423 } while (res_wait);
3424 }
3425}
Padmanabhan Komanduru9e971622020-02-10 13:09:34 +05303426
Sushil Chauhandbd8db42020-02-28 13:07:48 -08003427int32_t HWCSession::GetDisplayVsyncPeriod(hwc2_display_t disp, VsyncPeriodNanos *vsync_period) {
3428 if (vsync_period == nullptr) {
3429 return HWC2_ERROR_BAD_PARAMETER;
3430 }
3431
3432 return CallDisplayFunction(disp, &HWCDisplay::GetDisplayVsyncPeriod, vsync_period);
3433}
3434
3435int32_t HWCSession::SetActiveConfigWithConstraints(
3436 hwc2_display_t display, hwc2_config_t config,
3437 const VsyncPeriodChangeConstraints *vsync_period_change_constraints,
3438 VsyncPeriodChangeTimeline *out_timeline) {
3439 if ((vsync_period_change_constraints == nullptr) || (out_timeline == nullptr)) {
3440 return HWC2_ERROR_BAD_PARAMETER;
3441 }
3442
3443 return CallDisplayFunction(display, &HWCDisplay::SetActiveConfigWithConstraints, config,
3444 vsync_period_change_constraints, out_timeline);
3445}
3446
Naseer Ahmedb92e73f2016-03-12 02:03:48 -05003447} // namespace sdm