blob: 0adecf40cdcc47b823c1c2ef15a6ad138d7432a6 [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
Drew Davenport5951b112024-08-05 09:44:27 -060067void DrmHwcThree::CleanDisplayResources(uint64_t display_id) {
68 DEBUG_FUNC();
69 HwcDisplay* display = GetDisplay(display_id);
70 if (display == nullptr) {
71 return;
72 }
73
74 display->SetPowerMode(static_cast<int32_t>(PowerMode::OFF));
75
76 size_t cache_size = 0;
77 auto err = composer_resources_->GetDisplayClientTargetCacheSize(display_id,
78 &cache_size);
79 if (err != hwc3::Error::kNone) {
80 ALOGE("%s: Could not clear target buffer cache for display: %" PRIu64,
81 __func__, display_id);
82 return;
83 }
84
85 for (size_t slot = 0; slot < cache_size; slot++) {
86 buffer_handle_t buffer_handle = nullptr;
87 auto buf_releaser = ComposerResources::CreateResourceReleaser(true);
88
89 Buffer buf{};
90 buf.slot = static_cast<int32_t>(slot);
91 err = composer_resources_->GetDisplayClientTarget(display_id, buf,
92 &buffer_handle,
93 buf_releaser.get());
94 if (err != hwc3::Error::kNone) {
95 continue;
96 }
97
98 err = Hwc2toHwc3Error(
99 display->SetClientTarget(buffer_handle, -1,
100 static_cast<int32_t>(
101 common::Dataspace::UNKNOWN),
102 {}));
103 if (err != hwc3::Error::kNone) {
104 ALOGE(
105 "%s: Could not clear slot %zu of the target buffer cache for "
106 "display %" PRIu64,
107 __func__, slot, display_id);
108 }
109 }
110}
111
112void DrmHwcThree::HandleDisplayHotplugEvent(uint64_t display_id,
113 bool connected) {
114 DEBUG_FUNC();
115 if (!connected) {
116 composer_resources_->RemoveDisplay(display_id);
Drew Davenport0bf6c2b2024-08-05 11:54:52 -0600117 Displays().erase(display_id);
Drew Davenport5951b112024-08-05 09:44:27 -0600118 return;
119 }
120
121 if (composer_resources_->HasDisplay(display_id)) {
122 /* Cleanup existing display resources */
123 CleanDisplayResources(display_id);
124 composer_resources_->RemoveDisplay(display_id);
Drew Davenport0bf6c2b2024-08-05 11:54:52 -0600125 Displays().erase(display_id);
Drew Davenport5951b112024-08-05 09:44:27 -0600126 }
127 composer_resources_->AddPhysicalDisplay(display_id);
128}
129
Drew Davenportade69652024-07-16 15:54:33 -0600130} // namespace aidl::android::hardware::graphics::composer3::impl