blob: b243199e62cc771d894186bce3968d1ffede2b21 [file] [log] [blame]
Sean Pauled2ec4b2016-03-10 15:35:40 -05001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Sean Paul468a7542024-07-16 19:50:58 +000017#define LOG_TAG "drmhwc"
Sean Pauled2ec4b2016-03-10 15:35:40 -050018
Roman Stratiienko13cc3662020-08-29 21:35:39 +030019#include "DrmHwcTwo.h"
Sean Pauled2ec4b2016-03-10 15:35:40 -050020
Roman Stratiienko3dacd472022-01-11 19:18:34 +020021#include <cinttypes>
22
Roman Stratiienko3627beb2022-01-04 16:02:55 +020023#include "backend/Backend.h"
Roman Stratiienkod21071f2021-03-09 21:56:50 +020024#include "utils/log.h"
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030025
Sean Pauled2ec4b2016-03-10 15:35:40 -050026namespace android {
27
Sean Pauled2ec4b2016-03-10 15:35:40 -050028HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -050029 hwc2_callback_data_t data,
30 hwc2_function_pointer_t function) {
Roman Stratiienko23701092020-09-26 02:08:41 +030031 switch (static_cast<HWC2::Callback>(descriptor)) {
Sean Paulac874152016-03-10 16:00:26 -050032 case HWC2::Callback::Hotplug: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +030033 hotplug_callback_ = std::make_pair(HWC2_PFN_HOTPLUG(function), data);
Roman Stratiienko3dacd472022-01-11 19:18:34 +020034 if (function != nullptr) {
Drew Davenport93443182023-12-14 09:25:45 +000035 GetResMan().Init();
Roman Stratiienko3dacd472022-01-11 19:18:34 +020036 } else {
Drew Davenport93443182023-12-14 09:25:45 +000037 GetResMan().DeInit();
Roman Stratiienkod0494d92022-03-15 18:02:04 +020038 /* Headless display may still be here. Remove it! */
Drew Davenport93443182023-12-14 09:25:45 +000039 if (Displays().count(kPrimaryDisplay) != 0) {
40 Displays()[kPrimaryDisplay]->Deinit();
41 Displays().erase(kPrimaryDisplay);
Roman Stratiienkod0494d92022-03-15 18:02:04 +020042 }
Roman Stratiienko3dacd472022-01-11 19:18:34 +020043 }
Sean Paulac874152016-03-10 16:00:26 -050044 break;
45 }
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020046 case HWC2::Callback::Refresh: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +030047 refresh_callback_ = std::make_pair(HWC2_PFN_REFRESH(function), data);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +020048 break;
49 }
Sean Paulac874152016-03-10 16:00:26 -050050 case HWC2::Callback::Vsync: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +030051 vsync_callback_ = std::make_pair(HWC2_PFN_VSYNC(function), data);
Sean Paulac874152016-03-10 16:00:26 -050052 break;
53 }
Roman Stratiienko6b405052022-12-10 19:09:10 +020054#if __ANDROID_API__ > 29
Roman Stratiienko11ef8c52021-09-29 13:01:39 +030055 case HWC2::Callback::Vsync_2_4: {
56 vsync_2_4_callback_ = std::make_pair(HWC2_PFN_VSYNC_2_4(function), data);
57 break;
58 }
Roman Stratiienkod0c035b2022-01-21 15:12:56 +020059 case HWC2::Callback::VsyncPeriodTimingChanged: {
60 period_timing_changed_callback_ = std::
61 make_pair(HWC2_PFN_VSYNC_PERIOD_TIMING_CHANGED(function), data);
62 break;
63 }
Roman Stratiienko11ef8c52021-09-29 13:01:39 +030064#endif
Sean Paulac874152016-03-10 16:00:26 -050065 default:
66 break;
67 }
68 return HWC2::Error::None;
69}
70
Roman Stratiienko3dacd472022-01-11 19:18:34 +020071void DrmHwcTwo::SendHotplugEventToClient(hwc2_display_t displayid,
Manasi Navare3f0c01a2024-10-04 18:01:55 +000072 DisplayStatus display_status) {
Roman Stratiienko74923582022-01-17 11:24:21 +020073 auto hc = hotplug_callback_;
Manasi Navare3f0c01a2024-10-04 18:01:55 +000074
Roman Stratiienko74923582022-01-17 11:24:21 +020075 if (hc.first != nullptr && hc.second != nullptr) {
Roman Stratiienko9e2a2cd2022-12-28 20:47:29 +020076 /* For some reason HWC Service will call HWC2 API in hotplug callback
77 * handler. This is the reason we're using recursive mutex.
Roman Stratiienko74923582022-01-17 11:24:21 +020078 */
Roman Stratiienko74923582022-01-17 11:24:21 +020079 hc.first(hc.second, displayid,
Manasi Navare3f0c01a2024-10-04 18:01:55 +000080 display_status ? HWC2_CONNECTION_CONNECTED
81 : HWC2_CONNECTION_DISCONNECTED);
Roman Stratiienko863a3c22021-09-29 13:00:29 +030082 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030083}
84
Roman Stratiienko099c3112022-01-20 11:50:54 +020085void DrmHwcTwo::SendVsyncEventToClient(
86 hwc2_display_t displayid, int64_t timestamp,
87 [[maybe_unused]] uint32_t vsync_period) const {
88 /* vsync callback */
Roman Stratiienko6b405052022-12-10 19:09:10 +020089#if __ANDROID_API__ > 29
Roman Stratiienko099c3112022-01-20 11:50:54 +020090 if (vsync_2_4_callback_.first != nullptr &&
91 vsync_2_4_callback_.second != nullptr) {
92 vsync_2_4_callback_.first(vsync_2_4_callback_.second, displayid, timestamp,
93 vsync_period);
94 } else
95#endif
96 if (vsync_callback_.first != nullptr &&
97 vsync_callback_.second != nullptr) {
98 vsync_callback_.first(vsync_callback_.second, displayid, timestamp);
99 }
100}
101
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200102void DrmHwcTwo::SendVsyncPeriodTimingChangedEventToClient(
103 [[maybe_unused]] hwc2_display_t displayid,
104 [[maybe_unused]] int64_t timestamp) const {
Roman Stratiienko6b405052022-12-10 19:09:10 +0200105#if __ANDROID_API__ > 29
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200106 hwc_vsync_period_change_timeline_t timeline = {
107 .newVsyncAppliedTimeNanos = timestamp,
108 .refreshRequired = false,
109 .refreshTimeNanos = 0,
110 };
111 if (period_timing_changed_callback_.first != nullptr &&
112 period_timing_changed_callback_.second != nullptr) {
113 period_timing_changed_callback_
114 .first(period_timing_changed_callback_.second, displayid, &timeline);
115 }
116#endif
117}
118
Drew Davenport93443182023-12-14 09:25:45 +0000119void DrmHwcTwo::SendRefreshEventToClient(hwc2_display_t displayid) {
120 if (refresh_callback_.first != nullptr &&
121 refresh_callback_.second != nullptr) {
122 refresh_callback_.first(refresh_callback_.second, displayid);
123 }
124}
125
Sean Paulf72cccd2018-08-27 13:59:08 -0400126} // namespace android