1 /*
2 * Copyright (C) 2024 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
17 #define LOG_TAG "drmhwc"
18
19 #include "DrmHwcThree.h"
20
21 #include <cinttypes>
22
23 #include "Utils.h"
24 #include "aidl/android/hardware/graphics/common/Dataspace.h"
25 #if __ANDROID_API__ >= 35
26 #include "aidl/android/hardware/graphics/common/DisplayHotplugEvent.h"
27 #endif
28
29 namespace aidl::android::hardware::graphics::composer3::impl {
30
GetHwc3Display(::android::HwcDisplay & display)31 auto DrmHwcThree::GetHwc3Display(::android::HwcDisplay& display)
32 -> std::shared_ptr<Hwc3Display> {
33 auto frontend_private_data = display.GetFrontendPrivateData();
34 if (!frontend_private_data) {
35 frontend_private_data = std::make_shared<Hwc3Display>();
36 display.SetFrontendPrivateData(frontend_private_data);
37 }
38 return std::static_pointer_cast<Hwc3Display>(frontend_private_data);
39 }
40
~DrmHwcThree()41 DrmHwcThree::~DrmHwcThree() {
42 /* Display deinit routine is handled by resource manager */
43 GetResMan().DeInit();
44 }
45
Init(std::shared_ptr<IComposerCallback> callback)46 void DrmHwcThree::Init(std::shared_ptr<IComposerCallback> callback) {
47 composer_callback_ = std::move(callback);
48 GetResMan().Init();
49 }
50
SendVsyncPeriodTimingChangedEventToClient(uint64_t display_id,int64_t timestamp) const51 void DrmHwcThree::SendVsyncPeriodTimingChangedEventToClient(
52 uint64_t display_id, int64_t timestamp) const {
53 VsyncPeriodChangeTimeline timeline;
54 timeline.newVsyncAppliedTimeNanos = timestamp;
55 timeline.refreshRequired = false;
56 timeline.refreshTimeNanos = 0;
57
58 composer_callback_->onVsyncPeriodTimingChanged(static_cast<int64_t>(
59 display_id),
60 timeline);
61 }
62
SendRefreshEventToClient(uint64_t display_id)63 void DrmHwcThree::SendRefreshEventToClient(uint64_t display_id) {
64 {
65 const std::unique_lock lock(GetResMan().GetMainLock());
66 auto* idisplay = GetDisplay(display_id);
67 if (idisplay == nullptr) {
68 ALOGE("Failed to get display %" PRIu64, display_id);
69 return;
70 }
71 auto hwc3_display = GetHwc3Display(*idisplay);
72 hwc3_display->must_validate = true;
73 }
74 composer_callback_->onRefresh(static_cast<int64_t>(display_id));
75 }
76
SendVsyncEventToClient(uint64_t display_id,int64_t timestamp,uint32_t vsync_period) const77 void DrmHwcThree::SendVsyncEventToClient(uint64_t display_id, int64_t timestamp,
78 uint32_t vsync_period) const {
79 composer_callback_->onVsync(static_cast<int64_t>(display_id), timestamp,
80 static_cast<int32_t>(vsync_period));
81 }
82
83 #if __ANDROID_API__ >= 35
84
SendHotplugEventToClient(hwc2_display_t display_id,DrmHwc::DisplayStatus display_status)85 void DrmHwcThree::SendHotplugEventToClient(
86 hwc2_display_t display_id, DrmHwc::DisplayStatus display_status) {
87 common::DisplayHotplugEvent event = common::DisplayHotplugEvent::DISCONNECTED;
88 switch (display_status) {
89 case DrmHwc::kDisconnected:
90 event = common::DisplayHotplugEvent::DISCONNECTED;
91 break;
92 case DrmHwc::kConnected:
93 event = common::DisplayHotplugEvent::CONNECTED;
94 break;
95 case DrmHwc::kLinkTrainingFailed:
96 event = common::DisplayHotplugEvent::ERROR_INCOMPATIBLE_CABLE;
97 break;
98 }
99 composer_callback_->onHotplugEvent(static_cast<int64_t>(display_id), event);
100 }
101
102 #else
103
SendHotplugEventToClient(hwc2_display_t display_id,DrmHwc::DisplayStatus display_status)104 void DrmHwcThree::SendHotplugEventToClient(
105 hwc2_display_t display_id, DrmHwc::DisplayStatus display_status) {
106 bool connected = display_status != DrmHwc::kDisconnected;
107 composer_callback_->onHotplug(static_cast<int64_t>(display_id), connected);
108 }
109
110 #endif
111
112 } // namespace aidl::android::hardware::graphics::composer3::impl
113