blob: 5bbb48af9f1cb67e4da1dbfddfa360c3264619e3 [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 Pauled2ec4b2016-03-10 15:35:40 -050017#define LOG_TAG "hwc-drm-two"
18
Roman Stratiienko13cc3662020-08-29 21:35:39 +030019#include "DrmHwcTwo.h"
Sean Pauled2ec4b2016-03-10 15:35:40 -050020
Roman Stratiienko3627beb2022-01-04 16:02:55 +020021#include "backend/Backend.h"
Roman Stratiienkod21071f2021-03-09 21:56:50 +020022#include "utils/log.h"
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030023
Sean Pauled2ec4b2016-03-10 15:35:40 -050024namespace android {
25
Roman Stratiienko26fd2b22022-01-04 12:59:29 +020026DrmHwcTwo::DrmHwcTwo() = default;
Sean Pauled2ec4b2016-03-10 15:35:40 -050027
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030028HWC2::Error DrmHwcTwo::CreateDisplay(hwc2_display_t displ,
29 HWC2::DisplayType type) {
Roman Stratiienkod26619b2021-08-04 19:55:37 +030030 DrmDevice *drm = resource_manager_.GetDrmDevice(static_cast<int>(displ));
Roman Stratiienko8666dc92021-02-09 17:49:55 +020031 if (!drm) {
32 ALOGE("Failed to get a valid drmresource");
Sean Paulac874152016-03-10 16:00:26 -050033 return HWC2::Error::NoResources;
34 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030035 displays_.emplace(std::piecewise_construct, std::forward_as_tuple(displ),
Roman Stratiienko863a3c22021-09-29 13:00:29 +030036 std::forward_as_tuple(&resource_manager_, drm, displ, type,
37 this));
Sean Paulac874152016-03-10 16:00:26 -050038
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030039 DrmCrtc *crtc = drm->GetCrtcForDisplay(static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050040 if (!crtc) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030041 ALOGE("Failed to get crtc for display %d", static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050042 return HWC2::Error::BadDisplay;
43 }
Roman Stratiienkod21071f2021-03-09 21:56:50 +020044 auto display_planes = std::vector<DrmPlane *>();
45 for (const auto &plane : drm->planes()) {
Sean Paulac874152016-03-10 16:00:26 -050046 if (plane->GetCrtcSupported(*crtc))
47 display_planes.push_back(plane.get());
48 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030049 displays_.at(displ).Init(&display_planes);
Sean Paulac874152016-03-10 16:00:26 -050050 return HWC2::Error::None;
51}
52
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030053HWC2::Error DrmHwcTwo::Init() {
54 int rv = resource_manager_.Init();
55 if (rv) {
56 ALOGE("Can't initialize the resource manager %d", rv);
57 return HWC2::Error::NoResources;
58 }
59
60 HWC2::Error ret = HWC2::Error::None;
Roman Stratiienkofc014f52021-12-23 19:04:29 +020061 for (int i = 0; i < resource_manager_.GetDisplayCount(); i++) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030062 ret = CreateDisplay(i, HWC2::DisplayType::Physical);
63 if (ret != HWC2::Error::None) {
64 ALOGE("Failed to create display %d with error %d", i, ret);
65 return ret;
66 }
67 }
68
Roman Stratiienko1e053b42021-10-25 22:54:20 +030069 resource_manager_.GetUEventListener()->RegisterHotplugHandler(
70 [this] { HandleHotplugUEvent(); });
71
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030072 return ret;
73}
74
Roman Stratiienko7f576ec2021-12-22 17:34:18 +020075HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t /*width*/,
76 uint32_t /*height*/,
77 int32_t * /*format*/,
78 hwc2_display_t * /*display*/) {
79 // TODO(nobody): Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -050080 return HWC2::Error::Unsupported;
81}
82
Roman Stratiienko7f576ec2021-12-22 17:34:18 +020083HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t /*display*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020084 // TODO(nobody): Implement virtual display
Roman Stratiienko7f576ec2021-12-22 17:34:18 +020085 return HWC2::Error::Unsupported;
Sean Pauled2ec4b2016-03-10 15:35:40 -050086}
87
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +020088void DrmHwcTwo::Dump(uint32_t *outSize, char *outBuffer) {
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +020089 if (outBuffer != nullptr) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020090 auto copied_bytes = mDumpString.copy(outBuffer, *outSize);
91 *outSize = static_cast<uint32_t>(copied_bytes);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +020092 return;
93 }
94
95 std::stringstream output;
96
97 output << "-- drm_hwcomposer --\n\n";
98
Roman Stratiienko3627beb2022-01-04 16:02:55 +020099 for (std::pair<const hwc2_display_t, HwcDisplay> &dp : displays_)
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200100 output << dp.second.Dump();
101
102 mDumpString = output.str();
103 *outSize = static_cast<uint32_t>(mDumpString.size());
Sean Pauled2ec4b2016-03-10 15:35:40 -0500104}
105
106uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200107 // TODO(nobody): Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500108 return 0;
109}
110
111HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500112 hwc2_callback_data_t data,
113 hwc2_function_pointer_t function) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300114 std::unique_lock<std::mutex> lock(callback_lock_);
115
Roman Stratiienko23701092020-09-26 02:08:41 +0300116 switch (static_cast<HWC2::Callback>(descriptor)) {
Sean Paulac874152016-03-10 16:00:26 -0500117 case HWC2::Callback::Hotplug: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300118 hotplug_callback_ = std::make_pair(HWC2_PFN_HOTPLUG(function), data);
119 lock.unlock();
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200120 const auto &drm_devices = resource_manager_.GetDrmDevices();
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200121 for (const auto &device : drm_devices)
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300122 HandleInitialHotplugState(device.get());
Sean Paulac874152016-03-10 16:00:26 -0500123 break;
124 }
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200125 case HWC2::Callback::Refresh: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300126 refresh_callback_ = std::make_pair(HWC2_PFN_REFRESH(function), data);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200127 break;
128 }
Sean Paulac874152016-03-10 16:00:26 -0500129 case HWC2::Callback::Vsync: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300130 vsync_callback_ = std::make_pair(HWC2_PFN_VSYNC(function), data);
Sean Paulac874152016-03-10 16:00:26 -0500131 break;
132 }
Roman Stratiienko11ef8c52021-09-29 13:01:39 +0300133#if PLATFORM_SDK_VERSION > 29
134 case HWC2::Callback::Vsync_2_4: {
135 vsync_2_4_callback_ = std::make_pair(HWC2_PFN_VSYNC_2_4(function), data);
136 break;
137 }
138#endif
Sean Paulac874152016-03-10 16:00:26 -0500139 default:
140 break;
141 }
142 return HWC2::Error::None;
143}
144
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300145void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300146 const std::lock_guard<std::mutex> lock(callback_lock_);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300147
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300148 if (hotplug_callback_.first != nullptr &&
149 hotplug_callback_.second != nullptr) {
150 hotplug_callback_.first(hotplug_callback_.second, displayid,
151 state == DRM_MODE_CONNECTED
152 ? HWC2_CONNECTION_CONNECTED
153 : HWC2_CONNECTION_DISCONNECTED);
154 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300155}
156
157void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200158 for (const auto &conn : drmDevice->connectors()) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300159 if (conn->state() != DRM_MODE_CONNECTED)
160 continue;
161 HandleDisplayHotplug(conn->display(), conn->state());
162 }
163}
164
Roman Stratiienko1e053b42021-10-25 22:54:20 +0300165void DrmHwcTwo::HandleHotplugUEvent() {
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200166 for (const auto &drm : resource_manager_.GetDrmDevices()) {
Roman Stratiienko1e053b42021-10-25 22:54:20 +0300167 for (const auto &conn : drm->connectors()) {
168 drmModeConnection old_state = conn->state();
169 drmModeConnection cur_state = conn->UpdateModes()
170 ? DRM_MODE_UNKNOWNCONNECTION
171 : conn->state();
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300172
Roman Stratiienko1e053b42021-10-25 22:54:20 +0300173 if (cur_state == old_state)
174 continue;
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300175
Roman Stratiienko1e053b42021-10-25 22:54:20 +0300176 ALOGI("%s event for connector %u on display %d",
177 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", conn->id(),
178 conn->display());
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300179
Roman Stratiienko1e053b42021-10-25 22:54:20 +0300180 int display_id = conn->display();
181 if (cur_state == DRM_MODE_CONNECTED) {
182 auto &display = displays_.at(display_id);
183 display.ChosePreferredConfig();
184 } else {
185 auto &display = displays_.at(display_id);
186 display.ClearDisplay();
187 }
188
189 HandleDisplayHotplug(display_id, cur_state);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300190 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300191 }
192}
193
Sean Paulf72cccd2018-08-27 13:59:08 -0400194} // namespace android