blob: 54dba6754c29ead4951709c15f00f6165ceb8030 [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
Drew Davenport5951b112024-08-05 09:44:27 -060028DrmHwcThree::~DrmHwcThree() {
Normunds Rieksts425a6962024-03-11 14:46:07 +000029 /* Display deinit routine is handled by resource manager */
30 GetResMan().DeInit();
Drew Davenport5951b112024-08-05 09:44:27 -060031}
32
Drew Davenportade69652024-07-16 15:54:33 -060033void DrmHwcThree::Init(std::shared_ptr<IComposerCallback> callback) {
34 composer_callback_ = std::move(callback);
35 GetResMan().Init();
36}
37
38void DrmHwcThree::SendVsyncPeriodTimingChangedEventToClient(
39 uint64_t display_id, int64_t timestamp) const {
40 VsyncPeriodChangeTimeline timeline;
41 timeline.newVsyncAppliedTimeNanos = timestamp;
42 timeline.refreshRequired = false;
43 timeline.refreshTimeNanos = 0;
44
45 composer_callback_->onVsyncPeriodTimingChanged(static_cast<int64_t>(
46 display_id),
47 timeline);
48}
49
50void DrmHwcThree::SendRefreshEventToClient(uint64_t display_id) {
Drew Davenport5951b112024-08-05 09:44:27 -060051 composer_resources_->SetDisplayMustValidateState(display_id, true);
Drew Davenportade69652024-07-16 15:54:33 -060052 composer_callback_->onRefresh(static_cast<int64_t>(display_id));
53}
54
55void DrmHwcThree::SendVsyncEventToClient(uint64_t display_id, int64_t timestamp,
56 uint32_t vsync_period) const {
57 composer_callback_->onVsync(static_cast<int64_t>(display_id), timestamp,
58 static_cast<int32_t>(vsync_period));
59}
60
61void DrmHwcThree::SendHotplugEventToClient(hwc2_display_t display_id,
62 bool connected) {
Drew Davenport5951b112024-08-05 09:44:27 -060063 HandleDisplayHotplugEvent(static_cast<uint64_t>(display_id), connected);
Drew Davenportade69652024-07-16 15:54:33 -060064 composer_callback_->onHotplug(static_cast<int64_t>(display_id), connected);
65}
66
Normunds Rieksts425a6962024-03-11 14:46:07 +000067void DrmHwcThree::RemoveDisplay(uint64_t display_id) {
Drew Davenport5951b112024-08-05 09:44:27 -060068 DEBUG_FUNC();
69 HwcDisplay* display = GetDisplay(display_id);
70 if (display == nullptr) {
71 return;
72 }
Drew Davenport5951b112024-08-05 09:44:27 -060073 Displays().erase(display_id);
74}
75
76void DrmHwcThree::CleanDisplayResources(uint64_t display_id) {
77 DEBUG_FUNC();
78 HwcDisplay* display = GetDisplay(display_id);
79 if (display == nullptr) {
80 return;
81 }
82
83 display->SetPowerMode(static_cast<int32_t>(PowerMode::OFF));
84
85 size_t cache_size = 0;
86 auto err = composer_resources_->GetDisplayClientTargetCacheSize(display_id,
87 &cache_size);
88 if (err != hwc3::Error::kNone) {
89 ALOGE("%s: Could not clear target buffer cache for display: %" PRIu64,
90 __func__, display_id);
91 return;
92 }
93
94 for (size_t slot = 0; slot < cache_size; slot++) {
95 buffer_handle_t buffer_handle = nullptr;
96 auto buf_releaser = ComposerResources::CreateResourceReleaser(true);
97
98 Buffer buf{};
99 buf.slot = static_cast<int32_t>(slot);
100 err = composer_resources_->GetDisplayClientTarget(display_id, buf,
101 &buffer_handle,
102 buf_releaser.get());
103 if (err != hwc3::Error::kNone) {
104 continue;
105 }
106
107 err = Hwc2toHwc3Error(
108 display->SetClientTarget(buffer_handle, -1,
109 static_cast<int32_t>(
110 common::Dataspace::UNKNOWN),
111 {}));
112 if (err != hwc3::Error::kNone) {
113 ALOGE(
114 "%s: Could not clear slot %zu of the target buffer cache for "
115 "display %" PRIu64,
116 __func__, slot, display_id);
117 }
118 }
119}
120
121void DrmHwcThree::HandleDisplayHotplugEvent(uint64_t display_id,
122 bool connected) {
123 DEBUG_FUNC();
124 if (!connected) {
125 composer_resources_->RemoveDisplay(display_id);
Normunds Rieksts545096d2024-03-11 16:37:45 +0000126 RemoveDisplay(display_id);
Drew Davenport5951b112024-08-05 09:44:27 -0600127 return;
128 }
129
130 if (composer_resources_->HasDisplay(display_id)) {
131 /* Cleanup existing display resources */
132 CleanDisplayResources(display_id);
133 composer_resources_->RemoveDisplay(display_id);
Normunds Rieksts545096d2024-03-11 16:37:45 +0000134 RemoveDisplay(display_id);
Drew Davenport5951b112024-08-05 09:44:27 -0600135 }
136 composer_resources_->AddPhysicalDisplay(display_id);
137}
138
Drew Davenportade69652024-07-16 15:54:33 -0600139} // namespace aidl::android::hardware::graphics::composer3::impl