blob: ad4936b4895271cc3fe9cd6559c403ab1750dc22 [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
17#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18#define LOG_TAG "hwc-drm-two"
19
Roman Stratiienko13cc3662020-08-29 21:35:39 +030020#include "DrmHwcTwo.h"
Sean Pauled2ec4b2016-03-10 15:35:40 -050021
Roman Stratiienko0fade372021-02-20 13:59:55 +020022#include <fcntl.h>
Sean Paulac874152016-03-10 16:00:26 -050023#include <hardware/hardware.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050024#include <hardware/hwcomposer2.h>
Roman Stratiienko74774712021-02-05 16:32:47 +020025#include <sync/sync.h>
Roman Stratiienko0fade372021-02-20 13:59:55 +020026#include <unistd.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050027
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020028#include <cinttypes>
Roman Stratiienkod21071f2021-03-09 21:56:50 +020029#include <iostream>
30#include <sstream>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030031#include <string>
32
Roman Stratiienko13cc3662020-08-29 21:35:39 +030033#include "backend/BackendManager.h"
Roman Stratiienko33365c22020-10-10 23:06:36 +030034#include "bufferinfo/BufferInfoGetter.h"
Roman Stratiienko13cc3662020-08-29 21:35:39 +030035#include "compositor/DrmDisplayComposition.h"
Roman Stratiienkod21071f2021-03-09 21:56:50 +020036#include "utils/log.h"
37#include "utils/properties.h"
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030038
Sean Pauled2ec4b2016-03-10 15:35:40 -050039namespace android {
40
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020041DrmHwcTwo::DrmHwcTwo() : hwc2_device() {
Sean Paulac874152016-03-10 16:00:26 -050042 common.tag = HARDWARE_DEVICE_TAG;
43 common.version = HWC_DEVICE_API_VERSION_2_0;
Sean Pauled2ec4b2016-03-10 15:35:40 -050044 common.close = HookDevClose;
45 getCapabilities = HookDevGetCapabilities;
46 getFunction = HookDevGetFunction;
47}
48
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030049HWC2::Error DrmHwcTwo::CreateDisplay(hwc2_display_t displ,
50 HWC2::DisplayType type) {
Roman Stratiienkod26619b2021-08-04 19:55:37 +030051 DrmDevice *drm = resource_manager_.GetDrmDevice(static_cast<int>(displ));
Roman Stratiienko8666dc92021-02-09 17:49:55 +020052 if (!drm) {
53 ALOGE("Failed to get a valid drmresource");
Sean Paulac874152016-03-10 16:00:26 -050054 return HWC2::Error::NoResources;
55 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030056 displays_.emplace(std::piecewise_construct, std::forward_as_tuple(displ),
Roman Stratiienko863a3c22021-09-29 13:00:29 +030057 std::forward_as_tuple(&resource_manager_, drm, displ, type,
58 this));
Sean Paulac874152016-03-10 16:00:26 -050059
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030060 DrmCrtc *crtc = drm->GetCrtcForDisplay(static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050061 if (!crtc) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030062 ALOGE("Failed to get crtc for display %d", static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050063 return HWC2::Error::BadDisplay;
64 }
Roman Stratiienkod21071f2021-03-09 21:56:50 +020065 auto display_planes = std::vector<DrmPlane *>();
66 for (const auto &plane : drm->planes()) {
Sean Paulac874152016-03-10 16:00:26 -050067 if (plane->GetCrtcSupported(*crtc))
68 display_planes.push_back(plane.get());
69 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030070 displays_.at(displ).Init(&display_planes);
Sean Paulac874152016-03-10 16:00:26 -050071 return HWC2::Error::None;
72}
73
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030074HWC2::Error DrmHwcTwo::Init() {
75 int rv = resource_manager_.Init();
76 if (rv) {
77 ALOGE("Can't initialize the resource manager %d", rv);
78 return HWC2::Error::NoResources;
79 }
80
81 HWC2::Error ret = HWC2::Error::None;
Roman Stratiienkofc014f52021-12-23 19:04:29 +020082 for (int i = 0; i < resource_manager_.GetDisplayCount(); i++) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030083 ret = CreateDisplay(i, HWC2::DisplayType::Physical);
84 if (ret != HWC2::Error::None) {
85 ALOGE("Failed to create display %d with error %d", i, ret);
86 return ret;
87 }
88 }
89
Roman Stratiienko1e053b42021-10-25 22:54:20 +030090 resource_manager_.GetUEventListener()->RegisterHotplugHandler(
91 [this] { HandleHotplugUEvent(); });
92
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030093 return ret;
94}
95
Roman Stratiienko7f576ec2021-12-22 17:34:18 +020096HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t /*width*/,
97 uint32_t /*height*/,
98 int32_t * /*format*/,
99 hwc2_display_t * /*display*/) {
100 // TODO(nobody): Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500101 return HWC2::Error::Unsupported;
102}
103
Roman Stratiienko7f576ec2021-12-22 17:34:18 +0200104HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t /*display*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200105 // TODO(nobody): Implement virtual display
Roman Stratiienko7f576ec2021-12-22 17:34:18 +0200106 return HWC2::Error::Unsupported;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500107}
108
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200109std::string DrmHwcTwo::HwcDisplay::DumpDelta(
110 DrmHwcTwo::HwcDisplay::Stats delta) {
111 if (delta.total_pixops_ == 0)
112 return "No stats yet";
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200113 double ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200114
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200115 std::stringstream ss;
116 ss << " Total frames count: " << delta.total_frames_ << "\n"
117 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
118 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
119 << ((delta.failed_kms_present_ > 0)
120 ? " !!! Internal failure, FIX it please\n"
121 : "")
122 << " Flattened frames: " << delta.frames_flattened_ << "\n"
123 << " Pixel operations (free units)"
124 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
125 << "]\n"
126 << " Composition efficiency: " << ratio;
127
128 return ss.str();
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200129}
130
131std::string DrmHwcTwo::HwcDisplay::Dump() {
Roman Stratiienkoe8c06792021-09-30 10:09:31 +0300132 std::string flattening_state_str;
133 switch (flattenning_state_) {
134 case ClientFlattenningState::Disabled:
135 flattening_state_str = "Disabled";
136 break;
137 case ClientFlattenningState::NotRequired:
138 flattening_state_str = "Not needed";
139 break;
140 case ClientFlattenningState::Flattened:
141 flattening_state_str = "Active";
142 break;
143 case ClientFlattenningState::ClientRefreshRequested:
144 flattening_state_str = "Refresh requested";
145 break;
146 default:
147 flattening_state_str = std::to_string(flattenning_state_) +
148 " VSync remains";
149 }
150
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200151 std::stringstream ss;
152 ss << "- Display on: " << connector_->name() << "\n"
Roman Stratiienkoe8c06792021-09-30 10:09:31 +0300153 << " Flattening state: " << flattening_state_str << "\n"
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200154 << "Statistics since system boot:\n"
155 << DumpDelta(total_stats_) << "\n\n"
156 << "Statistics since last dumpsys request:\n"
157 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200158
159 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200160 return ss.str();
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200161}
162
163void DrmHwcTwo::Dump(uint32_t *outSize, char *outBuffer) {
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200164 if (outBuffer != nullptr) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200165 auto copied_bytes = mDumpString.copy(outBuffer, *outSize);
166 *outSize = static_cast<uint32_t>(copied_bytes);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200167 return;
168 }
169
170 std::stringstream output;
171
172 output << "-- drm_hwcomposer --\n\n";
173
174 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &dp : displays_)
175 output << dp.second.Dump();
176
177 mDumpString = output.str();
178 *outSize = static_cast<uint32_t>(mDumpString.size());
Sean Pauled2ec4b2016-03-10 15:35:40 -0500179}
180
181uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200182 // TODO(nobody): Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500183 return 0;
184}
185
186HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500187 hwc2_callback_data_t data,
188 hwc2_function_pointer_t function) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300189 std::unique_lock<std::mutex> lock(callback_lock_);
190
Roman Stratiienko23701092020-09-26 02:08:41 +0300191 switch (static_cast<HWC2::Callback>(descriptor)) {
Sean Paulac874152016-03-10 16:00:26 -0500192 case HWC2::Callback::Hotplug: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300193 hotplug_callback_ = std::make_pair(HWC2_PFN_HOTPLUG(function), data);
194 lock.unlock();
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200195 const auto &drm_devices = resource_manager_.GetDrmDevices();
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200196 for (const auto &device : drm_devices)
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300197 HandleInitialHotplugState(device.get());
Sean Paulac874152016-03-10 16:00:26 -0500198 break;
199 }
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200200 case HWC2::Callback::Refresh: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300201 refresh_callback_ = std::make_pair(HWC2_PFN_REFRESH(function), data);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200202 break;
203 }
Sean Paulac874152016-03-10 16:00:26 -0500204 case HWC2::Callback::Vsync: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300205 vsync_callback_ = std::make_pair(HWC2_PFN_VSYNC(function), data);
Sean Paulac874152016-03-10 16:00:26 -0500206 break;
207 }
Roman Stratiienko11ef8c52021-09-29 13:01:39 +0300208#if PLATFORM_SDK_VERSION > 29
209 case HWC2::Callback::Vsync_2_4: {
210 vsync_2_4_callback_ = std::make_pair(HWC2_PFN_VSYNC_2_4(function), data);
211 break;
212 }
213#endif
Sean Paulac874152016-03-10 16:00:26 -0500214 default:
215 break;
216 }
217 return HWC2::Error::None;
218}
219
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100220DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200221 DrmDevice *drm, hwc2_display_t handle,
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300222 HWC2::DisplayType type, DrmHwcTwo *hwc2)
223 : hwc2_(hwc2),
224 resource_manager_(resource_manager),
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100225 drm_(drm),
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100226 handle_(handle),
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200227 type_(type),
228 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200229 // clang-format off
230 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
231 0.0, 1.0, 0.0, 0.0,
232 0.0, 0.0, 1.0, 0.0,
233 0.0, 0.0, 0.0, 1.0};
234 // clang-format on
Sean Paulac874152016-03-10 16:00:26 -0500235}
236
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300237void DrmHwcTwo::HwcDisplay::ClearDisplay() {
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300238 AtomicCommitArgs a_args = {.clear_active_composition = true};
239 compositor_.ExecuteAtomicCommit(a_args);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300240}
241
Sean Paulac874152016-03-10 16:00:26 -0500242HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
Sean Paulac874152016-03-10 16:00:26 -0500243 int display = static_cast<int>(handle_);
Roman Stratiienkoe8c06792021-09-30 10:09:31 +0300244 int ret = compositor_.Init(resource_manager_, display);
Sean Paulac874152016-03-10 16:00:26 -0500245 if (ret) {
246 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
247 return HWC2::Error::NoResources;
248 }
249
250 // Split up the given display planes into primary and overlay to properly
251 // interface with the composition
252 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -0700253 property_get("vendor.hwc.drm.use_overlay_planes", use_overlay_planes_prop,
254 "1");
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200255 bool use_overlay_planes = strtol(use_overlay_planes_prop, nullptr, 10);
Sean Paulac874152016-03-10 16:00:26 -0500256 for (auto &plane : *planes) {
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200257 if (plane->GetType() == DRM_PLANE_TYPE_PRIMARY)
Sean Paulac874152016-03-10 16:00:26 -0500258 primary_planes_.push_back(plane);
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200259 else if (use_overlay_planes && (plane)->GetType() == DRM_PLANE_TYPE_OVERLAY)
Sean Paulac874152016-03-10 16:00:26 -0500260 overlay_planes_.push_back(plane);
261 }
262
263 crtc_ = drm_->GetCrtcForDisplay(display);
264 if (!crtc_) {
265 ALOGE("Failed to get crtc for display %d", display);
266 return HWC2::Error::BadDisplay;
267 }
268
269 connector_ = drm_->GetConnectorForDisplay(display);
270 if (!connector_) {
271 ALOGE("Failed to get connector for display %d", display);
272 return HWC2::Error::BadDisplay;
273 }
274
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300275 ret = vsync_worker_.Init(drm_, display, [this](int64_t timestamp) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300276 const std::lock_guard<std::mutex> lock(hwc2_->callback_lock_);
Roman Stratiienko11ef8c52021-09-29 13:01:39 +0300277 /* vsync callback */
278#if PLATFORM_SDK_VERSION > 29
279 if (hwc2_->vsync_2_4_callback_.first != nullptr &&
280 hwc2_->vsync_2_4_callback_.second != nullptr) {
281 hwc2_vsync_period_t period_ns{};
282 GetDisplayVsyncPeriod(&period_ns);
283 hwc2_->vsync_2_4_callback_.first(hwc2_->vsync_2_4_callback_.second,
284 handle_, timestamp, period_ns);
285 } else
286#endif
287 if (hwc2_->vsync_callback_.first != nullptr &&
288 hwc2_->vsync_callback_.second != nullptr) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300289 hwc2_->vsync_callback_.first(hwc2_->vsync_callback_.second, handle_,
290 timestamp);
291 }
292 });
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300293 if (ret) {
294 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
295 return HWC2::Error::BadDisplay;
296 }
297
Roman Stratiienkoe8c06792021-09-30 10:09:31 +0300298 ret = flattening_vsync_worker_.Init(drm_, display, [this](int64_t /*timestamp*/) {
299 const std::lock_guard<std::mutex> lock(hwc2_->callback_lock_);
300 /* Frontend flattening */
301 if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
302 --flattenning_state_ ==
303 ClientFlattenningState::ClientRefreshRequested &&
304 hwc2_->refresh_callback_.first != nullptr &&
305 hwc2_->refresh_callback_.second != nullptr) {
306 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
307 flattening_vsync_worker_.VSyncControl(false);
308 }
309 });
310 if (ret) {
311 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
312 return HWC2::Error::BadDisplay;
313 }
314
Matvii Zorinef3c7972020-08-11 15:15:44 +0300315 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
316 if (ret) {
317 ALOGE("Failed to set backend for d=%d %d\n", display, ret);
318 return HWC2::Error::BadDisplay;
319 }
320
Roman Stratiienko720f6522021-12-06 14:56:14 +0200321 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
322
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300323 return ChosePreferredConfig();
324}
325
326HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() {
Sean Paulac874152016-03-10 16:00:26 -0500327 // Fetch the number of modes from the display
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200328 uint32_t num_configs = 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200329 HWC2::Error err = GetDisplayConfigs(&num_configs, nullptr);
Sean Paulac874152016-03-10 16:00:26 -0500330 if (err != HWC2::Error::None || !num_configs)
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200331 return HWC2::Error::BadDisplay;
Sean Paulac874152016-03-10 16:00:26 -0500332
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200333 return SetActiveConfig(preferred_config_id_);
Sean Paulac874152016-03-10 16:00:26 -0500334}
335
Sean Pauled2ec4b2016-03-10 15:35:40 -0500336HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500337 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
338 l.second.accept_type_change();
339 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500340}
341
342HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500343 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
344 *layer = static_cast<hwc2_layer_t>(layer_idx_);
345 ++layer_idx_;
346 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500347}
348
349HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Vincent Donnefort9abec032019-10-09 15:43:43 +0100350 if (!get_layer(layer))
351 return HWC2::Error::BadLayer;
352
Sean Paulac874152016-03-10 16:00:26 -0500353 layers_.erase(layer);
354 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500355}
356
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200357HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(
358 hwc2_config_t *config) const {
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200359 if (hwc_configs_.count(active_config_id_) == 0)
Sean Paulac874152016-03-10 16:00:26 -0500360 return HWC2::Error::BadConfig;
361
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200362 *config = active_config_id_;
Sean Paulac874152016-03-10 16:00:26 -0500363 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500364}
365
366HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
367 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500368 uint32_t num_changes = 0;
369 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
370 if (l.second.type_changed()) {
371 if (layers && num_changes < *num_elements)
372 layers[num_changes] = l.first;
373 if (types && num_changes < *num_elements)
374 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
375 ++num_changes;
376 }
377 }
378 if (!layers && !types)
379 *num_elements = num_changes;
380 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500381}
382
383HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500384 uint32_t height,
385 int32_t /*format*/,
386 int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -0500387 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
388 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
389
390 if (width < min.first || height < min.second)
391 return HWC2::Error::Unsupported;
392
393 if (width > max.first || height > max.second)
394 return HWC2::Error::Unsupported;
395
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200396 if (dataspace != HAL_DATASPACE_UNKNOWN)
Sean Paulac874152016-03-10 16:00:26 -0500397 return HWC2::Error::Unsupported;
398
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200399 // TODO(nobody): Validate format can be handled by either GL or planes
Sean Paulac874152016-03-10 16:00:26 -0500400 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500401}
402
403HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500404 int32_t *modes) {
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800405 if (!modes)
406 *num_modes = 1;
407
408 if (modes)
409 *modes = HAL_COLOR_MODE_NATIVE;
410
411 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500412}
413
414HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500415 int32_t attribute_in,
416 int32_t *value) {
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200417 int conf = static_cast<int>(config);
418
419 if (hwc_configs_.count(conf) == 0) {
420 ALOGE("Could not find active mode for %d", conf);
Sean Paulac874152016-03-10 16:00:26 -0500421 return HWC2::Error::BadConfig;
422 }
423
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200424 auto &hwc_config = hwc_configs_[conf];
425
Sean Paulac874152016-03-10 16:00:26 -0500426 static const int32_t kUmPerInch = 25400;
427 uint32_t mm_width = connector_->mm_width();
428 uint32_t mm_height = connector_->mm_height();
429 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
430 switch (attribute) {
431 case HWC2::Attribute::Width:
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200432 *value = static_cast<int>(hwc_config.mode.h_display());
Sean Paulac874152016-03-10 16:00:26 -0500433 break;
434 case HWC2::Attribute::Height:
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200435 *value = static_cast<int>(hwc_config.mode.v_display());
Sean Paulac874152016-03-10 16:00:26 -0500436 break;
437 case HWC2::Attribute::VsyncPeriod:
438 // in nanoseconds
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200439 *value = static_cast<int>(1E9 / hwc_config.mode.v_refresh());
Sean Paulac874152016-03-10 16:00:26 -0500440 break;
441 case HWC2::Attribute::DpiX:
442 // Dots per 1000 inches
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200443 *value = mm_width ? static_cast<int>(hwc_config.mode.h_display() *
444 kUmPerInch / mm_width)
445 : -1;
Sean Paulac874152016-03-10 16:00:26 -0500446 break;
447 case HWC2::Attribute::DpiY:
448 // Dots per 1000 inches
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200449 *value = mm_height ? static_cast<int>(hwc_config.mode.v_display() *
450 kUmPerInch / mm_height)
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300451 : -1;
Sean Paulac874152016-03-10 16:00:26 -0500452 break;
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300453#if PLATFORM_SDK_VERSION > 29
454 case HWC2::Attribute::ConfigGroup:
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200455 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
456 * able to request it even if service @2.1 is used */
457 *value = hwc_config.group_id;
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300458 break;
459#endif
Sean Paulac874152016-03-10 16:00:26 -0500460 default:
461 *value = -1;
462 return HWC2::Error::BadConfig;
463 }
464 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500465}
466
Roman Stratiienko5f2f3ce2021-12-22 11:46:03 +0200467// NOLINTNEXTLINE (readability-function-cognitive-complexity): Fixme
Sean Pauled2ec4b2016-03-10 15:35:40 -0500468HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
469 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500470 // Since this callback is normally invoked twice (once to get the count, and
471 // once to populate configs), we don't really want to read the edid
472 // redundantly. Instead, only update the modes on the first invocation. While
473 // it's possible this will result in stale modes, it'll all come out in the
474 // wash when we try to set the active config later.
475 if (!configs) {
476 int ret = connector_->UpdateModes();
477 if (ret) {
478 ALOGE("Failed to update display modes %d", ret);
479 return HWC2::Error::BadDisplay;
480 }
Sean Paulac874152016-03-10 16:00:26 -0500481
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200482 hwc_configs_.clear();
483 preferred_config_id_ = 0;
484 int preferred_config_group_id_ = 0;
Neil Armstrongb67d0492019-06-20 09:00:21 +0000485
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200486 if (connector_->modes().empty()) {
487 ALOGE("No modes reported by KMS");
488 return HWC2::Error::BadDisplay;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000489 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000490
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200491 int last_config_id = 1;
492 int last_group_id = 1;
Neil Armstrongb67d0492019-06-20 09:00:21 +0000493
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200494 /* Group modes */
495 for (const auto &mode : connector_->modes()) {
496 /* Find group for the new mode or create new group */
497 int group_found = 0;
498 for (auto &hwc_config : hwc_configs_) {
499 if (mode.h_display() == hwc_config.second.mode.h_display() &&
500 mode.v_display() == hwc_config.second.mode.v_display()) {
501 group_found = hwc_config.second.group_id;
502 }
503 }
504 if (group_found == 0) {
505 group_found = last_group_id++;
506 }
507
508 bool disabled = false;
509 if (mode.flags() & DRM_MODE_FLAG_3D_MASK) {
510 ALOGI("Disabling display mode %s (Modes with 3D flag aren't supported)",
511 mode.name().c_str());
512 disabled = true;
513 }
514
515 /* Add config */
516 hwc_configs_[last_config_id] = {
517 .id = last_config_id,
518 .group_id = group_found,
519 .mode = mode,
520 .disabled = disabled,
521 };
522
523 /* Chwck if the mode is preferred */
524 if ((mode.type() & DRM_MODE_TYPE_PREFERRED) != 0 &&
525 preferred_config_id_ == 0) {
526 preferred_config_id_ = last_config_id;
527 preferred_config_group_id_ = group_found;
528 }
529
530 last_config_id++;
531 }
532
533 /* We must have preferred mode. Set first mode as preferred
534 * in case KMS haven't reported anything. */
535 if (preferred_config_id_ == 0) {
536 preferred_config_id_ = 1;
537 preferred_config_group_id_ = 1;
538 }
539
540 for (int group = 1; group < last_group_id; group++) {
541 bool has_interlaced = false;
542 bool has_progressive = false;
543 for (auto &hwc_config : hwc_configs_) {
544 if (hwc_config.second.group_id != group || hwc_config.second.disabled) {
545 continue;
546 }
547
548 if (hwc_config.second.IsInterlaced()) {
549 has_interlaced = true;
550 } else {
551 has_progressive = true;
552 }
553 }
554
555 bool has_both = has_interlaced && has_progressive;
556 if (!has_both) {
557 continue;
558 }
559
560 bool group_contains_preferred_interlaced = false;
561 if (group == preferred_config_group_id_ &&
562 hwc_configs_[preferred_config_id_].IsInterlaced()) {
563 group_contains_preferred_interlaced = true;
564 }
565
566 for (auto &hwc_config : hwc_configs_) {
567 if (hwc_config.second.group_id != group || hwc_config.second.disabled) {
568 continue;
569 }
570
571 bool disable = group_contains_preferred_interlaced
572 ? !hwc_config.second.IsInterlaced()
573 : hwc_config.second.IsInterlaced();
574
575 if (disable) {
576 ALOGI(
577 "Group %i: Disabling display mode %s (This group should consist "
578 "of %s modes)",
579 group, hwc_config.second.mode.name().c_str(),
580 group_contains_preferred_interlaced ? "interlaced"
581 : "progressive");
582
583 hwc_config.second.disabled = true;
584 }
585 }
586 }
587
588 /* Group should not contain 2 modes with FPS delta less than ~1HZ
589 * otherwise android.graphics.cts.SetFrameRateTest CTS will fail
590 */
591 for (int m1 = 1; m1 < last_config_id; m1++) {
592 for (int m2 = 1; m2 < last_config_id; m2++) {
593 if (m1 != m2 &&
594 hwc_configs_[m1].group_id == hwc_configs_[m2].group_id &&
595 !hwc_configs_[m1].disabled && !hwc_configs_[m2].disabled &&
596 fabsf(hwc_configs_[m1].mode.v_refresh() -
597 hwc_configs_[m2].mode.v_refresh()) < 1.0) {
598 ALOGI(
599 "Group %i: Disabling display mode %s (Refresh rate value is "
600 "too close to existing mode %s)",
601 hwc_configs_[m2].group_id, hwc_configs_[m2].mode.name().c_str(),
602 hwc_configs_[m1].mode.name().c_str());
603
604 hwc_configs_[m2].disabled = true;
605 }
606 }
607 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000608 }
609
610 uint32_t idx = 0;
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200611 for (auto &hwc_config : hwc_configs_) {
612 if (hwc_config.second.disabled) {
613 continue;
614 }
615
616 if (configs != nullptr) {
617 if (idx >= *num_configs) {
618 break;
619 }
620 configs[idx] = hwc_config.second.id;
621 }
622
623 idx++;
Sean Paulac874152016-03-10 16:00:26 -0500624 }
625 *num_configs = idx;
626 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500627}
628
629HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500630 std::ostringstream stream;
631 stream << "display-" << connector_->id();
632 std::string string = stream.str();
633 size_t length = string.length();
634 if (!name) {
635 *size = length;
636 return HWC2::Error::None;
637 }
638
639 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
640 strncpy(name, string.c_str(), *size);
641 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500642}
643
Roman Stratiienko7f576ec2021-12-22 17:34:18 +0200644HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(
645 int32_t * /*display_requests*/, uint32_t *num_elements,
646 hwc2_layer_t * /*layers*/, int32_t * /*layer_requests*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200647 // TODO(nobody): I think virtual display should request
Sean Paulac874152016-03-10 16:00:26 -0500648 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
Sean Paulac874152016-03-10 16:00:26 -0500649 *num_elements = 0;
650 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500651}
652
653HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500654 *type = static_cast<int32_t>(type_);
655 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500656}
657
658HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500659 *support = 0;
660 return HWC2::Error::None;
661}
662
663HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400664 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
665 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500666 *num_types = 0;
667 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500668}
669
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300670/* Find API details at:
671 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
672 */
Sean Pauled2ec4b2016-03-10 15:35:40 -0500673HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500674 hwc2_layer_t *layers,
675 int32_t *fences) {
Sean Paulac874152016-03-10 16:00:26 -0500676 uint32_t num_layers = 0;
677
678 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
679 ++num_layers;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200680 if (layers == nullptr || fences == nullptr)
Sean Paulac874152016-03-10 16:00:26 -0500681 continue;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200682
683 if (num_layers > *num_elements) {
Sean Paulac874152016-03-10 16:00:26 -0500684 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
685 return HWC2::Error::None;
686 }
687
688 layers[num_layers - 1] = l.first;
Roman Stratiienko0fade372021-02-20 13:59:55 +0200689 fences[num_layers - 1] = l.second.release_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500690 }
691 *num_elements = num_layers;
692 return HWC2::Error::None;
693}
694
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300695HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Sean Paulac874152016-03-10 16:00:26 -0500696 // order the layers by z-order
697 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100698 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500699 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
700 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200701 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500702 case HWC2::Composition::Device:
703 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
704 break;
705 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100706 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500707 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100708 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500709 break;
710 default:
711 continue;
712 }
713 }
714 if (use_client_layer)
715 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
716
Rob Herring4f6c62e2018-05-17 14:33:02 -0500717 if (z_map.empty())
718 return HWC2::Error::BadLayer;
719
Matvii Zorin5368b732021-01-12 10:53:08 +0200720 std::vector<DrmHwcLayer> composition_layers;
721
Sean Paulac874152016-03-10 16:00:26 -0500722 // now that they're ordered by z, add them to the composition
723 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
724 DrmHwcLayer layer;
725 l.second->PopulateDrmLayer(&layer);
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200726 int ret = layer.ImportBuffer(drm_);
Sean Paulac874152016-03-10 16:00:26 -0500727 if (ret) {
728 ALOGE("Failed to import layer, ret=%d", ret);
729 return HWC2::Error::NoResources;
730 }
Matvii Zorin5368b732021-01-12 10:53:08 +0200731 composition_layers.emplace_back(std::move(layer));
Sean Paulac874152016-03-10 16:00:26 -0500732 }
Sean Paulac874152016-03-10 16:00:26 -0500733
Roman Stratiienko753d1072021-12-30 18:05:27 +0200734 auto composition = std::make_shared<DrmDisplayComposition>(crtc_);
Sean Paulac874152016-03-10 16:00:26 -0500735
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200736 // TODO(nobody): Don't always assume geometry changed
Matvii Zorin5368b732021-01-12 10:53:08 +0200737 int ret = composition->SetLayers(composition_layers.data(),
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300738 composition_layers.size());
Sean Paulac874152016-03-10 16:00:26 -0500739 if (ret) {
740 ALOGE("Failed to set layers in the composition ret=%d", ret);
741 return HWC2::Error::BadLayer;
742 }
743
744 std::vector<DrmPlane *> primary_planes(primary_planes_);
745 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500746 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500747 if (ret) {
John Stultz66763d52021-08-24 04:59:25 +0000748 ALOGV("Failed to plan the composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500749 return HWC2::Error::BadConfig;
750 }
751
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300752 a_args.composition = composition;
Roman Stratiienkof81d2c82022-01-11 19:47:24 +0200753 if (staged_mode) {
754 a_args.display_mode = *staged_mode;
755 }
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300756 ret = compositor_.ExecuteAtomicCommit(a_args);
757
Sean Paulac874152016-03-10 16:00:26 -0500758 if (ret) {
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300759 if (!a_args.test_only)
John Stultz78c9f6c2018-05-24 16:43:35 -0700760 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500761 return HWC2::Error::BadParameter;
762 }
Roman Stratiienkof81d2c82022-01-11 19:47:24 +0200763
764 if (!a_args.test_only) {
765 staged_mode.reset();
766 }
767
Rob Herring4f6c62e2018-05-17 14:33:02 -0500768 return HWC2::Error::None;
769}
770
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300771/* Find API details at:
772 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
773 */
Matteo Franchinc56eede2019-12-03 17:10:38 +0000774HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500775 HWC2::Error ret;
776
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200777 ++total_stats_.total_frames_;
778
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300779 AtomicCommitArgs a_args{};
780 ret = CreateComposition(a_args);
781
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200782 if (ret != HWC2::Error::None)
783 ++total_stats_.failed_kms_present_;
784
Rob Herring4f6c62e2018-05-17 14:33:02 -0500785 if (ret == HWC2::Error::BadLayer) {
786 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000787 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500788 return HWC2::Error::None;
789 }
790 if (ret != HWC2::Error::None)
791 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500792
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300793 *present_fence = a_args.out_fence.Release();
Sean Paulac874152016-03-10 16:00:26 -0500794
795 ++frame_no_;
796 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500797}
798
799HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200800 int conf = static_cast<int>(config);
801
802 if (hwc_configs_.count(conf) == 0) {
803 ALOGE("Could not find active mode for %d", conf);
Sean Paulac874152016-03-10 16:00:26 -0500804 return HWC2::Error::BadConfig;
805 }
806
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200807 auto &mode = hwc_configs_[conf].mode;
808
Roman Stratiienkof81d2c82022-01-11 19:47:24 +0200809 staged_mode = mode;
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300810
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200811 active_config_id_ = conf;
Sean Paulac874152016-03-10 16:00:26 -0500812
813 // Setup the client layer's dimensions
814 hwc_rect_t display_frame = {.left = 0,
815 .top = 0,
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200816 .right = static_cast<int>(mode.h_display()),
817 .bottom = static_cast<int>(mode.v_display())};
Sean Paulac874152016-03-10 16:00:26 -0500818 client_layer_.SetLayerDisplayFrame(display_frame);
Sean Paulac874152016-03-10 16:00:26 -0500819
820 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500821}
822
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300823/* Find API details at:
824 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
825 */
Sean Pauled2ec4b2016-03-10 15:35:40 -0500826HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
827 int32_t acquire_fence,
828 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600829 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500830 client_layer_.set_buffer(target);
John Stultz8adf5442021-07-31 00:19:50 +0000831 client_layer_.acquire_fence_ = UniqueFd(acquire_fence);
Sean Paulac874152016-03-10 16:00:26 -0500832 client_layer_.SetLayerDataspace(dataspace);
Roman Stratiienko33365c22020-10-10 23:06:36 +0300833
834 /* TODO: Do not update source_crop every call.
835 * It makes sense to do it once after every hotplug event. */
Roman Stratiienkofc014f52021-12-23 19:04:29 +0200836 HwcDrmBo bo{};
Roman Stratiienko33365c22020-10-10 23:06:36 +0300837 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
838
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200839 hwc_frect_t source_crop = {.left = 0.0F,
840 .top = 0.0F,
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300841 .right = static_cast<float>(bo.width),
842 .bottom = static_cast<float>(bo.height)};
Roman Stratiienko33365c22020-10-10 23:06:36 +0300843 client_layer_.SetLayerSourceCrop(source_crop);
844
Sean Paulac874152016-03-10 16:00:26 -0500845 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500846}
847
848HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Roman Stratiienkod146d6d2020-10-04 23:56:46 +0300849 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
850 return HWC2::Error::BadParameter;
851
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800852 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +0300853 return HWC2::Error::Unsupported;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800854
855 color_mode_ = mode;
856 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500857}
858
859HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500860 int32_t hint) {
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200861 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
862 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
863 return HWC2::Error::BadParameter;
864
865 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
866 return HWC2::Error::BadParameter;
867
868 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
869 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
870 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
871
872 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500873}
874
Roman Stratiienko7f576ec2021-12-22 17:34:18 +0200875HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/,
876 int32_t /*release_fence*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200877 // TODO(nobody): Need virtual display support
Roman Stratiienko7f576ec2021-12-22 17:34:18 +0200878 return HWC2::Error::Unsupported;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500879}
880
Sean Paulac874152016-03-10 16:00:26 -0500881HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
Sean Paulac874152016-03-10 16:00:26 -0500882 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300883 AtomicCommitArgs a_args{};
884
Sean Paulac874152016-03-10 16:00:26 -0500885 switch (mode) {
886 case HWC2::PowerMode::Off:
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300887 a_args.active = false;
Sean Paulac874152016-03-10 16:00:26 -0500888 break;
889 case HWC2::PowerMode::On:
Roman Stratiienkof81d2c82022-01-11 19:47:24 +0200890 /*
891 * Setting the display to active before we have a composition
892 * can break some drivers, so skip setting a_args.active to
893 * true, as the next composition frame will implicitly activate
894 * the display
895 */
896 return HWC2::Error::None;
Sean Paulac874152016-03-10 16:00:26 -0500897 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100898 case HWC2::PowerMode::Doze:
899 case HWC2::PowerMode::DozeSuspend:
900 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500901 default:
902 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100903 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500904 };
905
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300906 int err = compositor_.ExecuteAtomicCommit(a_args);
907 if (err) {
908 ALOGE("Failed to apply the dpms composition err=%d", err);
Sean Paulac874152016-03-10 16:00:26 -0500909 return HWC2::Error::BadParameter;
910 }
911 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500912}
913
914HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300915 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500916 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500917}
918
919HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500920 uint32_t *num_requests) {
Matvii Zorinef3c7972020-08-11 15:15:44 +0300921 return backend_->ValidateDisplay(this, num_types, num_requests);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500922}
923
Matvii Zorined90ef92021-01-29 18:32:06 +0200924std::vector<DrmHwcTwo::HwcLayer *>
925DrmHwcTwo::HwcDisplay::GetOrderLayersByZPos() {
926 std::vector<DrmHwcTwo::HwcLayer *> ordered_layers;
927 ordered_layers.reserve(layers_.size());
928
929 for (auto &[handle, layer] : layers_) {
930 ordered_layers.emplace_back(&layer);
931 }
932
933 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
934 [](const DrmHwcTwo::HwcLayer *lhs, const DrmHwcTwo::HwcLayer *rhs) {
935 return lhs->z_order() < rhs->z_order();
936 });
937
938 return ordered_layers;
939}
940
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300941#if PLATFORM_SDK_VERSION > 29
942HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
943 if (connector_->internal())
944 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
945 else if (connector_->external())
946 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
947 else
948 return HWC2::Error::BadConfig;
949
950 return HWC2::Error::None;
951}
952
953HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayVsyncPeriod(
954 hwc2_vsync_period_t *outVsyncPeriod /* ns */) {
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200955 return GetDisplayAttribute(active_config_id_, HWC2_ATTRIBUTE_VSYNC_PERIOD,
956 (int32_t *)(outVsyncPeriod));
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300957}
958
959HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfigWithConstraints(
960 hwc2_config_t /*config*/,
961 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
962 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300963 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
964 return HWC2::Error::BadParameter;
965 }
966
967 return HWC2::Error::BadConfig;
968}
969
970HWC2::Error DrmHwcTwo::HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
971 return HWC2::Error::Unsupported;
972}
973
974HWC2::Error DrmHwcTwo::HwcDisplay::GetSupportedContentTypes(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200975 uint32_t *outNumSupportedContentTypes,
976 const uint32_t *outSupportedContentTypes) {
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300977 if (outSupportedContentTypes == nullptr)
978 *outNumSupportedContentTypes = 0;
979
980 return HWC2::Error::None;
981}
982
983HWC2::Error DrmHwcTwo::HwcDisplay::SetContentType(int32_t contentType) {
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300984 if (contentType != HWC2_CONTENT_TYPE_NONE)
985 return HWC2::Error::Unsupported;
986
987 /* TODO: Map to the DRM Connector property:
988 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
989 */
990
991 return HWC2::Error::None;
992}
993#endif
994
John Stultz8c7229d2020-02-07 21:31:08 +0000995#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800996HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
997 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +0300998 auto blob = connector_->GetEdidBlob();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800999
Roman Stratiienko3e8ce572021-09-29 12:46:28 +03001000 if (!blob) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001001 ALOGE("Failed to get edid property value.");
1002 return HWC2::Error::Unsupported;
1003 }
1004
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +03001005 if (outData) {
1006 *outDataSize = std::min(*outDataSize, blob->length);
1007 memcpy(outData, blob->data, *outDataSize);
1008 } else {
1009 *outDataSize = blob->length;
1010 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001011 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001012
1013 return HWC2::Error::None;
1014}
1015
1016HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
Roman Stratiienko7f576ec2021-12-22 17:34:18 +02001017 uint32_t *outNumCapabilities, uint32_t * /*outCapabilities*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001018 if (outNumCapabilities == nullptr) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001019 return HWC2::Error::BadParameter;
1020 }
1021
1022 *outNumCapabilities = 0;
1023
1024 return HWC2::Error::None;
1025}
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001026
1027HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
1028 bool *supported) {
1029 *supported = false;
1030 return HWC2::Error::None;
1031}
1032
1033HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
1034 float /* brightness */) {
1035 return HWC2::Error::Unsupported;
1036}
1037
John Stultz8c7229d2020-02-07 21:31:08 +00001038#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001039
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001040#if PLATFORM_SDK_VERSION > 27
1041
1042HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
1043 int32_t mode, uint32_t *outNumIntents,
1044 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1045 if (mode != HAL_COLOR_MODE_NATIVE) {
1046 return HWC2::Error::BadParameter;
1047 }
1048
1049 if (outIntents == nullptr) {
1050 *outNumIntents = 1;
1051 return HWC2::Error::None;
1052 }
1053 *outNumIntents = 1;
1054 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1055 return HWC2::Error::None;
1056}
1057
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001058HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
1059 int32_t intent) {
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001060 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1061 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1062 return HWC2::Error::BadParameter;
1063
1064 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
1065 return HWC2::Error::BadParameter;
1066
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001067 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001068 return HWC2::Error::Unsupported;
1069
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001070 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001071 return HWC2::Error::Unsupported;
1072
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001073 color_mode_ = mode;
1074 return HWC2::Error::None;
1075}
1076
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001077#endif /* PLATFORM_SDK_VERSION > 27 */
1078
Roman Stratiienko24a7fc42021-12-23 16:25:20 +02001079const Backend *DrmHwcTwo::HwcDisplay::backend() const {
1080 return backend_.get();
1081}
1082
1083void DrmHwcTwo::HwcDisplay::set_backend(std::unique_ptr<Backend> backend) {
1084 backend_ = std::move(backend);
1085}
1086
Roman Stratiienkoaaaa4692021-09-29 12:50:10 +03001087HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t /*x*/,
1088 int32_t /*y*/) {
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001089 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001090}
1091
1092HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Roman Stratiienko5063d532021-09-29 12:47:31 +03001093 switch (static_cast<HWC2::BlendMode>(mode)) {
1094 case HWC2::BlendMode::None:
1095 blending_ = DrmHwcBlending::kNone;
1096 break;
1097 case HWC2::BlendMode::Premultiplied:
1098 blending_ = DrmHwcBlending::kPreMult;
1099 break;
1100 case HWC2::BlendMode::Coverage:
1101 blending_ = DrmHwcBlending::kCoverage;
1102 break;
1103 default:
1104 ALOGE("Unknown blending mode b=%d", blending_);
1105 blending_ = DrmHwcBlending::kNone;
1106 break;
1107 }
Sean Paulac874152016-03-10 16:00:26 -05001108 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001109}
1110
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +03001111/* Find API details at:
1112 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=2314
1113 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001114HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001115 int32_t acquire_fence) {
Sean Paulac874152016-03-10 16:00:26 -05001116 set_buffer(buffer);
John Stultz8adf5442021-07-31 00:19:50 +00001117 acquire_fence_ = UniqueFd(acquire_fence);
Sean Paulac874152016-03-10 16:00:26 -05001118 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001119}
1120
Roman Stratiienkoaaaa4692021-09-29 12:50:10 +03001121HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t /*color*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001122 // TODO(nobody): Put to client composition here?
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001123 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001124}
1125
1126HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001127 sf_type_ = static_cast<HWC2::Composition>(type);
1128 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001129}
1130
1131HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Roman Stratiienko515da8f2021-09-29 12:47:21 +03001132 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
1133 case HAL_DATASPACE_STANDARD_BT709:
1134 color_space_ = DrmHwcColorSpace::kItuRec709;
1135 break;
1136 case HAL_DATASPACE_STANDARD_BT601_625:
1137 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
1138 case HAL_DATASPACE_STANDARD_BT601_525:
1139 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
1140 color_space_ = DrmHwcColorSpace::kItuRec601;
1141 break;
1142 case HAL_DATASPACE_STANDARD_BT2020:
1143 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
1144 color_space_ = DrmHwcColorSpace::kItuRec2020;
1145 break;
1146 default:
1147 color_space_ = DrmHwcColorSpace::kUndefined;
1148 }
1149
1150 switch (dataspace & HAL_DATASPACE_RANGE_MASK) {
1151 case HAL_DATASPACE_RANGE_FULL:
1152 sample_range_ = DrmHwcSampleRange::kFullRange;
1153 break;
1154 case HAL_DATASPACE_RANGE_LIMITED:
1155 sample_range_ = DrmHwcSampleRange::kLimitedRange;
1156 break;
1157 default:
1158 sample_range_ = DrmHwcSampleRange::kUndefined;
1159 }
Sean Paulac874152016-03-10 16:00:26 -05001160 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001161}
1162
1163HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001164 display_frame_ = frame;
1165 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001166}
1167
1168HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001169 alpha_ = alpha;
1170 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001171}
1172
1173HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
Roman Stratiienko7f576ec2021-12-22 17:34:18 +02001174 const native_handle_t * /*stream*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001175 // TODO(nobody): We don't support sideband
Roman Stratiienko7f576ec2021-12-22 17:34:18 +02001176 return HWC2::Error::Unsupported;
1177 ;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001178}
1179
1180HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001181 source_crop_ = crop;
1182 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001183}
1184
Roman Stratiienko7f576ec2021-12-22 17:34:18 +02001185HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(
1186 hwc_region_t /*damage*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001187 // TODO(nobody): We don't use surface damage, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001188 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001189}
1190
1191HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Roman Stratiienko2ed4cbe2021-09-29 12:47:35 +03001192 uint32_t l_transform = 0;
1193
1194 // 270* and 180* cannot be combined with flips. More specifically, they
1195 // already contain both horizontal and vertical flips, so those fields are
1196 // redundant in this case. 90* rotation can be combined with either horizontal
1197 // flip or vertical flip, so treat it differently
1198 if (transform == HWC_TRANSFORM_ROT_270) {
1199 l_transform = DrmHwcTransform::kRotate270;
1200 } else if (transform == HWC_TRANSFORM_ROT_180) {
1201 l_transform = DrmHwcTransform::kRotate180;
1202 } else {
1203 if (transform & HWC_TRANSFORM_FLIP_H)
1204 l_transform |= DrmHwcTransform::kFlipH;
1205 if (transform & HWC_TRANSFORM_FLIP_V)
1206 l_transform |= DrmHwcTransform::kFlipV;
1207 if (transform & HWC_TRANSFORM_ROT_90)
1208 l_transform |= DrmHwcTransform::kRotate90;
1209 }
1210
1211 transform_ = static_cast<DrmHwcTransform>(l_transform);
Sean Paulac874152016-03-10 16:00:26 -05001212 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001213}
1214
Roman Stratiienko7f576ec2021-12-22 17:34:18 +02001215HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(
1216 hwc_region_t /*visible*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001217 // TODO(nobody): We don't use this information, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001218 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001219}
1220
Sean Paulac874152016-03-10 16:00:26 -05001221HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
Sean Paulac874152016-03-10 16:00:26 -05001222 z_order_ = order;
1223 return HWC2::Error::None;
1224}
1225
1226void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
Sean Paulac874152016-03-10 16:00:26 -05001227 layer->sf_handle = buffer_;
Roman Stratiienko0fade372021-02-20 13:59:55 +02001228 // TODO(rsglobal): Avoid extra fd duplication
1229 layer->acquire_fence = UniqueFd(fcntl(acquire_fence_.Get(), F_DUPFD_CLOEXEC));
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001230 layer->display_frame = display_frame_;
Roman Stratiienkoe78235c2021-12-23 17:36:12 +02001231 layer->alpha = std::lround(65535.0F * alpha_);
Roman Stratiienko5063d532021-09-29 12:47:31 +03001232 layer->blending = blending_;
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001233 layer->source_crop = source_crop_;
Roman Stratiienko2ed4cbe2021-09-29 12:47:35 +03001234 layer->transform = transform_;
Roman Stratiienko515da8f2021-09-29 12:47:21 +03001235 layer->color_space = color_space_;
1236 layer->sample_range = sample_range_;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001237}
1238
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001239void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +03001240 const std::lock_guard<std::mutex> lock(callback_lock_);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001241
Roman Stratiienko863a3c22021-09-29 13:00:29 +03001242 if (hotplug_callback_.first != nullptr &&
1243 hotplug_callback_.second != nullptr) {
1244 hotplug_callback_.first(hotplug_callback_.second, displayid,
1245 state == DRM_MODE_CONNECTED
1246 ? HWC2_CONNECTION_CONNECTED
1247 : HWC2_CONNECTION_DISCONNECTED);
1248 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001249}
1250
1251void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
Roman Stratiienkod21071f2021-03-09 21:56:50 +02001252 for (const auto &conn : drmDevice->connectors()) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001253 if (conn->state() != DRM_MODE_CONNECTED)
1254 continue;
1255 HandleDisplayHotplug(conn->display(), conn->state());
1256 }
1257}
1258
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001259void DrmHwcTwo::HandleHotplugUEvent() {
Roman Stratiienkofc014f52021-12-23 19:04:29 +02001260 for (const auto &drm : resource_manager_.GetDrmDevices()) {
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001261 for (const auto &conn : drm->connectors()) {
1262 drmModeConnection old_state = conn->state();
1263 drmModeConnection cur_state = conn->UpdateModes()
1264 ? DRM_MODE_UNKNOWNCONNECTION
1265 : conn->state();
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001266
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001267 if (cur_state == old_state)
1268 continue;
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001269
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001270 ALOGI("%s event for connector %u on display %d",
1271 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", conn->id(),
1272 conn->display());
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001273
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001274 int display_id = conn->display();
1275 if (cur_state == DRM_MODE_CONNECTED) {
1276 auto &display = displays_.at(display_id);
1277 display.ChosePreferredConfig();
1278 } else {
1279 auto &display = displays_.at(display_id);
1280 display.ClearDisplay();
1281 }
1282
1283 HandleDisplayHotplug(display_id, cur_state);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001284 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001285 }
1286}
1287
Sean Pauled2ec4b2016-03-10 15:35:40 -05001288// static
Roman Stratiienko7c915682021-12-23 12:59:20 +02001289int DrmHwcTwo::HookDevClose(hw_device_t *dev) {
1290 // NOLINTNEXTLINE (cppcoreguidelines-pro-type-reinterpret-cast): Safe
1291 auto *hwc2_dev = reinterpret_cast<hwc2_device_t *>(dev);
1292 std::unique_ptr<DrmHwcTwo> ctx(toDrmHwcTwo(hwc2_dev));
Sean Pauled2ec4b2016-03-10 15:35:40 -05001293 return 0;
1294}
1295
1296// static
1297void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001298 uint32_t *out_count,
1299 int32_t * /*out_capabilities*/) {
Sean Pauled2ec4b2016-03-10 15:35:40 -05001300 *out_count = 0;
1301}
1302
1303// static
Sean Paulac874152016-03-10 16:00:26 -05001304hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1305 struct hwc2_device * /*dev*/, int32_t descriptor) {
Sean Paulac874152016-03-10 16:00:26 -05001306 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001307 switch (func) {
1308 // Device functions
1309 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1310 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1311 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1312 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001313 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001314 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1315 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1316 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1317 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1318 case HWC2::FunctionDescriptor::Dump:
1319 return ToHook<HWC2_PFN_DUMP>(
1320 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1321 uint32_t *, char *>);
1322 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1323 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1324 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1325 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1326 case HWC2::FunctionDescriptor::RegisterCallback:
1327 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1328 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1329 &DrmHwcTwo::RegisterCallback, int32_t,
1330 hwc2_callback_data_t, hwc2_function_pointer_t>);
1331
1332 // Display functions
1333 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1334 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1335 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1336 &HwcDisplay::AcceptDisplayChanges>);
1337 case HWC2::FunctionDescriptor::CreateLayer:
1338 return ToHook<HWC2_PFN_CREATE_LAYER>(
1339 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1340 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1341 case HWC2::FunctionDescriptor::DestroyLayer:
1342 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1343 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1344 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1345 case HWC2::FunctionDescriptor::GetActiveConfig:
1346 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1347 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1348 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1349 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1350 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1351 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1352 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1353 hwc2_layer_t *, int32_t *>);
1354 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1355 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1356 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1357 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1358 int32_t, int32_t>);
1359 case HWC2::FunctionDescriptor::GetColorModes:
1360 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1361 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1362 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1363 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001364 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1365 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1366 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1367 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001368 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001369 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1370 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1371 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1372 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001373 case HWC2::FunctionDescriptor::GetDisplayName:
1374 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1375 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1376 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1377 case HWC2::FunctionDescriptor::GetDisplayRequests:
1378 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1379 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1380 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1381 hwc2_layer_t *, int32_t *>);
1382 case HWC2::FunctionDescriptor::GetDisplayType:
1383 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1384 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1385 &HwcDisplay::GetDisplayType, int32_t *>);
1386 case HWC2::FunctionDescriptor::GetDozeSupport:
1387 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1388 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1389 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001390 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1391 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1392 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1393 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1394 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001395 case HWC2::FunctionDescriptor::GetReleaseFences:
1396 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1397 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1398 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1399 int32_t *>);
1400 case HWC2::FunctionDescriptor::PresentDisplay:
1401 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1402 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1403 &HwcDisplay::PresentDisplay, int32_t *>);
1404 case HWC2::FunctionDescriptor::SetActiveConfig:
1405 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1406 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1407 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1408 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001409 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1410 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1411 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1412 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001413 case HWC2::FunctionDescriptor::SetColorMode:
1414 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1415 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1416 &HwcDisplay::SetColorMode, int32_t>);
1417 case HWC2::FunctionDescriptor::SetColorTransform:
1418 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1419 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1420 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1421 case HWC2::FunctionDescriptor::SetOutputBuffer:
1422 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1423 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1424 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1425 case HWC2::FunctionDescriptor::SetPowerMode:
1426 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1427 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1428 &HwcDisplay::SetPowerMode, int32_t>);
1429 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1430 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1431 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1432 &HwcDisplay::SetVsyncEnabled, int32_t>);
1433 case HWC2::FunctionDescriptor::ValidateDisplay:
1434 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1435 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1436 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001437#if PLATFORM_SDK_VERSION > 27
1438 case HWC2::FunctionDescriptor::GetRenderIntents:
1439 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1440 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1441 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1442 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001443 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1444 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1445 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1446 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001447#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001448#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001449 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1450 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1451 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1452 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1453 uint32_t *, uint8_t *>);
1454 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1455 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1456 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1457 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1458 uint32_t *>);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001459 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1460 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1461 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1462 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1463 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1464 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1465 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1466 &HwcDisplay::SetDisplayBrightness, float>);
John Stultz8c7229d2020-02-07 21:31:08 +00001467#endif /* PLATFORM_SDK_VERSION > 28 */
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001468#if PLATFORM_SDK_VERSION > 29
1469 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
1470 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
1471 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
1472 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
1473 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
1474 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
1475 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
1476 &HwcDisplay::GetDisplayVsyncPeriod,
1477 hwc2_vsync_period_t *>);
1478 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
1479 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
1480 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
1481 &HwcDisplay::SetActiveConfigWithConstraints,
1482 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
1483 hwc_vsync_period_change_timeline_t *>);
1484 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
1485 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
1486 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
1487 &HwcDisplay::SetAutoLowLatencyMode, bool>);
1488 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
1489 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
1490 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
1491 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
1492 uint32_t *>);
1493 case HWC2::FunctionDescriptor::SetContentType:
1494 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
1495 DisplayHook<decltype(&HwcDisplay::SetContentType),
1496 &HwcDisplay::SetContentType, int32_t>);
1497#endif
Sean Pauled2ec4b2016-03-10 15:35:40 -05001498 // Layer functions
1499 case HWC2::FunctionDescriptor::SetCursorPosition:
1500 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1501 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1502 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1503 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1504 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1505 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1506 &HwcLayer::SetLayerBlendMode, int32_t>);
1507 case HWC2::FunctionDescriptor::SetLayerBuffer:
1508 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1509 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1510 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1511 case HWC2::FunctionDescriptor::SetLayerColor:
1512 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1513 LayerHook<decltype(&HwcLayer::SetLayerColor),
1514 &HwcLayer::SetLayerColor, hwc_color_t>);
1515 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1516 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1517 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1518 &HwcLayer::SetLayerCompositionType, int32_t>);
1519 case HWC2::FunctionDescriptor::SetLayerDataspace:
1520 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1521 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1522 &HwcLayer::SetLayerDataspace, int32_t>);
1523 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1524 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1525 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1526 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1527 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1528 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1529 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1530 &HwcLayer::SetLayerPlaneAlpha, float>);
1531 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001532 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1533 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1534 &HwcLayer::SetLayerSidebandStream,
1535 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001536 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1537 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1538 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1539 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1540 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1541 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1542 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1543 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1544 case HWC2::FunctionDescriptor::SetLayerTransform:
1545 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1546 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1547 &HwcLayer::SetLayerTransform, int32_t>);
1548 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1549 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1550 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1551 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1552 case HWC2::FunctionDescriptor::SetLayerZOrder:
1553 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1554 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1555 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001556 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001557 default:
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001558 return nullptr;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001559 }
1560}
Sean Paulac874152016-03-10 16:00:26 -05001561
1562// static
1563int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1564 struct hw_device_t **dev) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001565 if (strcmp(name, HWC_HARDWARE_COMPOSER) != 0) {
Sean Paulac874152016-03-10 16:00:26 -05001566 ALOGE("Invalid module name- %s", name);
1567 return -EINVAL;
1568 }
1569
1570 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1571 if (!ctx) {
1572 ALOGE("Failed to allocate DrmHwcTwo");
1573 return -ENOMEM;
1574 }
1575
1576 HWC2::Error err = ctx->Init();
1577 if (err != HWC2::Error::None) {
1578 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1579 return -EINVAL;
1580 }
1581
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001582 ctx->common.module = (hw_module_t *)module;
Roman Stratiienko7c915682021-12-23 12:59:20 +02001583 *dev = &ctx.release()->common;
1584
Sean Paulac874152016-03-10 16:00:26 -05001585 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001586}
Sean Paulf72cccd2018-08-27 13:59:08 -04001587} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001588
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001589// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001590static struct hw_module_methods_t hwc2_module_methods = {
1591 .open = android::DrmHwcTwo::HookDevOpen,
1592};
1593
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001594// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001595hw_module_t HAL_MODULE_INFO_SYM = {
1596 .tag = HARDWARE_MODULE_TAG,
1597 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1598 .id = HWC_HARDWARE_MODULE_ID,
1599 .name = "DrmHwcTwo module",
1600 .author = "The Android Open Source Project",
1601 .methods = &hwc2_module_methods,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001602 .dso = nullptr,
Sean Paulac874152016-03-10 16:00:26 -05001603 .reserved = {0},
1604};