blob: b39d8349bfd9b2506f05b2fc183b6912cb02746b [file] [log] [blame]
Drew Davenportade69652024-07-16 15:54:33 -06001/*
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#include "DrmHwcThree.h"
18
19#include <cinttypes>
20
Drew Davenport5951b112024-08-05 09:44:27 -060021#include "Utils.h"
22#include "aidl/android/hardware/graphics/common/Dataspace.h"
23
Drew Davenportade69652024-07-16 15:54:33 -060024namespace aidl::android::hardware::graphics::composer3::impl {
25
Drew Davenport5951b112024-08-05 09:44:27 -060026using ::android::HwcDisplay;
27
28// NOLINTNEXTLINE(bugprone-exception-escape)
29DrmHwcThree::~DrmHwcThree() {
30 std::vector<uint64_t> display_ids;
31 display_ids.reserve(Displays().size());
32 for (auto& [display_id, display] : Displays()) {
33 display_ids.push_back(display_id);
34 }
35
36 for (auto display_id : display_ids) {
37 RemoveAndDestroyDisplay(display_id);
38 }
39}
40
Drew Davenportade69652024-07-16 15:54:33 -060041void DrmHwcThree::Init(std::shared_ptr<IComposerCallback> callback) {
42 composer_callback_ = std::move(callback);
43 GetResMan().Init();
44}
45
46void DrmHwcThree::SendVsyncPeriodTimingChangedEventToClient(
47 uint64_t display_id, int64_t timestamp) const {
48 VsyncPeriodChangeTimeline timeline;
49 timeline.newVsyncAppliedTimeNanos = timestamp;
50 timeline.refreshRequired = false;
51 timeline.refreshTimeNanos = 0;
52
53 composer_callback_->onVsyncPeriodTimingChanged(static_cast<int64_t>(
54 display_id),
55 timeline);
56}
57
58void DrmHwcThree::SendRefreshEventToClient(uint64_t display_id) {
Drew Davenport5951b112024-08-05 09:44:27 -060059 composer_resources_->SetDisplayMustValidateState(display_id, true);
Drew Davenportade69652024-07-16 15:54:33 -060060 composer_callback_->onRefresh(static_cast<int64_t>(display_id));
61}
62
63void DrmHwcThree::SendVsyncEventToClient(uint64_t display_id, int64_t timestamp,
64 uint32_t vsync_period) const {
65 composer_callback_->onVsync(static_cast<int64_t>(display_id), timestamp,
66 static_cast<int32_t>(vsync_period));
67}
68
69void DrmHwcThree::SendHotplugEventToClient(hwc2_display_t display_id,
70 bool connected) {
Drew Davenport5951b112024-08-05 09:44:27 -060071 HandleDisplayHotplugEvent(static_cast<uint64_t>(display_id), connected);
Drew Davenportade69652024-07-16 15:54:33 -060072 composer_callback_->onHotplug(static_cast<int64_t>(display_id), connected);
73}
74
Drew Davenport5951b112024-08-05 09:44:27 -060075void DrmHwcThree::RemoveAndDestroyDisplay(uint64_t display_id) {
76 DEBUG_FUNC();
77 HwcDisplay* display = GetDisplay(display_id);
78 if (display == nullptr) {
79 return;
80 }
81
82 display->SetPowerMode(static_cast<int32_t>(HWC2::PowerMode::Off));
83 display->Deinit();
84
85 composer_resources_->RemoveDisplay(static_cast<int64_t>(display_id));
86 Displays().erase(display_id);
87}
88
89void DrmHwcThree::CleanDisplayResources(uint64_t display_id) {
90 DEBUG_FUNC();
91 HwcDisplay* display = GetDisplay(display_id);
92 if (display == nullptr) {
93 return;
94 }
95
96 display->SetPowerMode(static_cast<int32_t>(PowerMode::OFF));
97
98 size_t cache_size = 0;
99 auto err = composer_resources_->GetDisplayClientTargetCacheSize(display_id,
100 &cache_size);
101 if (err != hwc3::Error::kNone) {
102 ALOGE("%s: Could not clear target buffer cache for display: %" PRIu64,
103 __func__, display_id);
104 return;
105 }
106
107 for (size_t slot = 0; slot < cache_size; slot++) {
108 buffer_handle_t buffer_handle = nullptr;
109 auto buf_releaser = ComposerResources::CreateResourceReleaser(true);
110
111 Buffer buf{};
112 buf.slot = static_cast<int32_t>(slot);
113 err = composer_resources_->GetDisplayClientTarget(display_id, buf,
114 &buffer_handle,
115 buf_releaser.get());
116 if (err != hwc3::Error::kNone) {
117 continue;
118 }
119
120 err = Hwc2toHwc3Error(
121 display->SetClientTarget(buffer_handle, -1,
122 static_cast<int32_t>(
123 common::Dataspace::UNKNOWN),
124 {}));
125 if (err != hwc3::Error::kNone) {
126 ALOGE(
127 "%s: Could not clear slot %zu of the target buffer cache for "
128 "display %" PRIu64,
129 __func__, slot, display_id);
130 }
131 }
132}
133
134void DrmHwcThree::HandleDisplayHotplugEvent(uint64_t display_id,
135 bool connected) {
136 DEBUG_FUNC();
137 if (!connected) {
138 composer_resources_->RemoveDisplay(display_id);
139 return;
140 }
141
142 if (composer_resources_->HasDisplay(display_id)) {
143 /* Cleanup existing display resources */
144 CleanDisplayResources(display_id);
145 composer_resources_->RemoveDisplay(display_id);
146 }
147 composer_resources_->AddPhysicalDisplay(display_id);
148}
149
Drew Davenportade69652024-07-16 15:54:33 -0600150} // namespace aidl::android::hardware::graphics::composer3::impl