blob: c120b6365ac84d08005371542ee2c5eaf6a0b152 [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,
Drew Davenport93443182023-12-14 09:25:45 +000072 bool connected) {
Roman Stratiienko74923582022-01-17 11:24:21 +020073 auto hc = hotplug_callback_;
74 if (hc.first != nullptr && hc.second != nullptr) {
Roman Stratiienko9e2a2cd2022-12-28 20:47:29 +020075 /* For some reason HWC Service will call HWC2 API in hotplug callback
76 * handler. This is the reason we're using recursive mutex.
Roman Stratiienko74923582022-01-17 11:24:21 +020077 */
Roman Stratiienko74923582022-01-17 11:24:21 +020078 hc.first(hc.second, displayid,
Drew Davenport4520df12024-07-15 12:19:55 -060079 connected ? HWC2_CONNECTION_CONNECTED
80 : HWC2_CONNECTION_DISCONNECTED);
Roman Stratiienko863a3c22021-09-29 13:00:29 +030081 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030082}
83
Roman Stratiienko099c3112022-01-20 11:50:54 +020084void DrmHwcTwo::SendVsyncEventToClient(
85 hwc2_display_t displayid, int64_t timestamp,
86 [[maybe_unused]] uint32_t vsync_period) const {
87 /* vsync callback */
Roman Stratiienko6b405052022-12-10 19:09:10 +020088#if __ANDROID_API__ > 29
Roman Stratiienko099c3112022-01-20 11:50:54 +020089 if (vsync_2_4_callback_.first != nullptr &&
90 vsync_2_4_callback_.second != nullptr) {
91 vsync_2_4_callback_.first(vsync_2_4_callback_.second, displayid, timestamp,
92 vsync_period);
93 } else
94#endif
95 if (vsync_callback_.first != nullptr &&
96 vsync_callback_.second != nullptr) {
97 vsync_callback_.first(vsync_callback_.second, displayid, timestamp);
98 }
99}
100
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200101void DrmHwcTwo::SendVsyncPeriodTimingChangedEventToClient(
102 [[maybe_unused]] hwc2_display_t displayid,
103 [[maybe_unused]] int64_t timestamp) const {
Roman Stratiienko6b405052022-12-10 19:09:10 +0200104#if __ANDROID_API__ > 29
Roman Stratiienkod0c035b2022-01-21 15:12:56 +0200105 hwc_vsync_period_change_timeline_t timeline = {
106 .newVsyncAppliedTimeNanos = timestamp,
107 .refreshRequired = false,
108 .refreshTimeNanos = 0,
109 };
110 if (period_timing_changed_callback_.first != nullptr &&
111 period_timing_changed_callback_.second != nullptr) {
112 period_timing_changed_callback_
113 .first(period_timing_changed_callback_.second, displayid, &timeline);
114 }
115#endif
116}
117
Drew Davenport93443182023-12-14 09:25:45 +0000118void DrmHwcTwo::SendRefreshEventToClient(hwc2_display_t displayid) {
119 if (refresh_callback_.first != nullptr &&
120 refresh_callback_.second != nullptr) {
121 refresh_callback_.first(refresh_callback_.second, displayid);
122 }
123}
124
Sean Paulf72cccd2018-08-27 13:59:08 -0400125} // namespace android