blob: 8142e6c4477252822d3f64aa432343f7f2f060f6 [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;
82 for (int i = 0; i < resource_manager_.getDisplayCount(); i++) {
83 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 Stratiienkod21071f2021-03-09 21:56:50 +0200195 const auto &drm_devices = resource_manager_.getDrmDevices();
196 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 planner_ = Planner::CreateInstance(drm_);
244 if (!planner_) {
245 ALOGE("Failed to create planner instance for composition");
246 return HWC2::Error::NoResources;
247 }
248
249 int display = static_cast<int>(handle_);
Roman Stratiienkoe8c06792021-09-30 10:09:31 +0300250 int ret = compositor_.Init(resource_manager_, display);
Sean Paulac874152016-03-10 16:00:26 -0500251 if (ret) {
252 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
253 return HWC2::Error::NoResources;
254 }
255
256 // Split up the given display planes into primary and overlay to properly
257 // interface with the composition
258 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -0700259 property_get("vendor.hwc.drm.use_overlay_planes", use_overlay_planes_prop,
260 "1");
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200261 bool use_overlay_planes = strtol(use_overlay_planes_prop, nullptr, 10);
Sean Paulac874152016-03-10 16:00:26 -0500262 for (auto &plane : *planes) {
263 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
264 primary_planes_.push_back(plane);
265 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
266 overlay_planes_.push_back(plane);
267 }
268
269 crtc_ = drm_->GetCrtcForDisplay(display);
270 if (!crtc_) {
271 ALOGE("Failed to get crtc for display %d", display);
272 return HWC2::Error::BadDisplay;
273 }
274
275 connector_ = drm_->GetConnectorForDisplay(display);
276 if (!connector_) {
277 ALOGE("Failed to get connector for display %d", display);
278 return HWC2::Error::BadDisplay;
279 }
280
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300281 ret = vsync_worker_.Init(drm_, display, [this](int64_t timestamp) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300282 const std::lock_guard<std::mutex> lock(hwc2_->callback_lock_);
Roman Stratiienko11ef8c52021-09-29 13:01:39 +0300283 /* vsync callback */
284#if PLATFORM_SDK_VERSION > 29
285 if (hwc2_->vsync_2_4_callback_.first != nullptr &&
286 hwc2_->vsync_2_4_callback_.second != nullptr) {
287 hwc2_vsync_period_t period_ns{};
288 GetDisplayVsyncPeriod(&period_ns);
289 hwc2_->vsync_2_4_callback_.first(hwc2_->vsync_2_4_callback_.second,
290 handle_, timestamp, period_ns);
291 } else
292#endif
293 if (hwc2_->vsync_callback_.first != nullptr &&
294 hwc2_->vsync_callback_.second != nullptr) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300295 hwc2_->vsync_callback_.first(hwc2_->vsync_callback_.second, handle_,
296 timestamp);
297 }
298 });
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300299 if (ret) {
300 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
301 return HWC2::Error::BadDisplay;
302 }
303
Roman Stratiienkoe8c06792021-09-30 10:09:31 +0300304 ret = flattening_vsync_worker_.Init(drm_, display, [this](int64_t /*timestamp*/) {
305 const std::lock_guard<std::mutex> lock(hwc2_->callback_lock_);
306 /* Frontend flattening */
307 if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
308 --flattenning_state_ ==
309 ClientFlattenningState::ClientRefreshRequested &&
310 hwc2_->refresh_callback_.first != nullptr &&
311 hwc2_->refresh_callback_.second != nullptr) {
312 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
313 flattening_vsync_worker_.VSyncControl(false);
314 }
315 });
316 if (ret) {
317 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
318 return HWC2::Error::BadDisplay;
319 }
320
Matvii Zorinef3c7972020-08-11 15:15:44 +0300321 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
322 if (ret) {
323 ALOGE("Failed to set backend for d=%d %d\n", display, ret);
324 return HWC2::Error::BadDisplay;
325 }
326
Roman Stratiienko720f6522021-12-06 14:56:14 +0200327 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
328
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300329 return ChosePreferredConfig();
330}
331
332HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() {
Sean Paulac874152016-03-10 16:00:26 -0500333 // Fetch the number of modes from the display
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200334 uint32_t num_configs = 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200335 HWC2::Error err = GetDisplayConfigs(&num_configs, nullptr);
Sean Paulac874152016-03-10 16:00:26 -0500336 if (err != HWC2::Error::None || !num_configs)
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200337 return HWC2::Error::BadDisplay;
Sean Paulac874152016-03-10 16:00:26 -0500338
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200339 return SetActiveConfig(preferred_config_id_);
Sean Paulac874152016-03-10 16:00:26 -0500340}
341
Sean Pauled2ec4b2016-03-10 15:35:40 -0500342HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500343 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
344 l.second.accept_type_change();
345 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500346}
347
348HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500349 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
350 *layer = static_cast<hwc2_layer_t>(layer_idx_);
351 ++layer_idx_;
352 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500353}
354
355HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Vincent Donnefort9abec032019-10-09 15:43:43 +0100356 if (!get_layer(layer))
357 return HWC2::Error::BadLayer;
358
Sean Paulac874152016-03-10 16:00:26 -0500359 layers_.erase(layer);
360 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500361}
362
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200363HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(
364 hwc2_config_t *config) const {
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200365 if (hwc_configs_.count(active_config_id_) == 0)
Sean Paulac874152016-03-10 16:00:26 -0500366 return HWC2::Error::BadConfig;
367
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200368 *config = active_config_id_;
Sean Paulac874152016-03-10 16:00:26 -0500369 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500370}
371
372HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
373 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500374 uint32_t num_changes = 0;
375 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
376 if (l.second.type_changed()) {
377 if (layers && num_changes < *num_elements)
378 layers[num_changes] = l.first;
379 if (types && num_changes < *num_elements)
380 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
381 ++num_changes;
382 }
383 }
384 if (!layers && !types)
385 *num_elements = num_changes;
386 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500387}
388
389HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500390 uint32_t height,
391 int32_t /*format*/,
392 int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -0500393 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
394 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
395
396 if (width < min.first || height < min.second)
397 return HWC2::Error::Unsupported;
398
399 if (width > max.first || height > max.second)
400 return HWC2::Error::Unsupported;
401
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200402 if (dataspace != HAL_DATASPACE_UNKNOWN)
Sean Paulac874152016-03-10 16:00:26 -0500403 return HWC2::Error::Unsupported;
404
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200405 // TODO(nobody): Validate format can be handled by either GL or planes
Sean Paulac874152016-03-10 16:00:26 -0500406 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500407}
408
409HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500410 int32_t *modes) {
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800411 if (!modes)
412 *num_modes = 1;
413
414 if (modes)
415 *modes = HAL_COLOR_MODE_NATIVE;
416
417 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500418}
419
420HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500421 int32_t attribute_in,
422 int32_t *value) {
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200423 int conf = static_cast<int>(config);
424
425 if (hwc_configs_.count(conf) == 0) {
426 ALOGE("Could not find active mode for %d", conf);
Sean Paulac874152016-03-10 16:00:26 -0500427 return HWC2::Error::BadConfig;
428 }
429
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200430 auto &hwc_config = hwc_configs_[conf];
431
Sean Paulac874152016-03-10 16:00:26 -0500432 static const int32_t kUmPerInch = 25400;
433 uint32_t mm_width = connector_->mm_width();
434 uint32_t mm_height = connector_->mm_height();
435 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
436 switch (attribute) {
437 case HWC2::Attribute::Width:
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200438 *value = static_cast<int>(hwc_config.mode.h_display());
Sean Paulac874152016-03-10 16:00:26 -0500439 break;
440 case HWC2::Attribute::Height:
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200441 *value = static_cast<int>(hwc_config.mode.v_display());
Sean Paulac874152016-03-10 16:00:26 -0500442 break;
443 case HWC2::Attribute::VsyncPeriod:
444 // in nanoseconds
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200445 *value = static_cast<int>(1E9 / hwc_config.mode.v_refresh());
Sean Paulac874152016-03-10 16:00:26 -0500446 break;
447 case HWC2::Attribute::DpiX:
448 // Dots per 1000 inches
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200449 *value = mm_width ? static_cast<int>(hwc_config.mode.h_display() *
450 kUmPerInch / mm_width)
451 : -1;
Sean Paulac874152016-03-10 16:00:26 -0500452 break;
453 case HWC2::Attribute::DpiY:
454 // Dots per 1000 inches
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200455 *value = mm_height ? static_cast<int>(hwc_config.mode.v_display() *
456 kUmPerInch / mm_height)
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300457 : -1;
Sean Paulac874152016-03-10 16:00:26 -0500458 break;
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300459#if PLATFORM_SDK_VERSION > 29
460 case HWC2::Attribute::ConfigGroup:
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200461 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
462 * able to request it even if service @2.1 is used */
463 *value = hwc_config.group_id;
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300464 break;
465#endif
Sean Paulac874152016-03-10 16:00:26 -0500466 default:
467 *value = -1;
468 return HWC2::Error::BadConfig;
469 }
470 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500471}
472
Roman Stratiienko5f2f3ce2021-12-22 11:46:03 +0200473// NOLINTNEXTLINE (readability-function-cognitive-complexity): Fixme
Sean Pauled2ec4b2016-03-10 15:35:40 -0500474HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
475 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500476 // Since this callback is normally invoked twice (once to get the count, and
477 // once to populate configs), we don't really want to read the edid
478 // redundantly. Instead, only update the modes on the first invocation. While
479 // it's possible this will result in stale modes, it'll all come out in the
480 // wash when we try to set the active config later.
481 if (!configs) {
482 int ret = connector_->UpdateModes();
483 if (ret) {
484 ALOGE("Failed to update display modes %d", ret);
485 return HWC2::Error::BadDisplay;
486 }
Sean Paulac874152016-03-10 16:00:26 -0500487
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200488 hwc_configs_.clear();
489 preferred_config_id_ = 0;
490 int preferred_config_group_id_ = 0;
Neil Armstrongb67d0492019-06-20 09:00:21 +0000491
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200492 if (connector_->modes().empty()) {
493 ALOGE("No modes reported by KMS");
494 return HWC2::Error::BadDisplay;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000495 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000496
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200497 int last_config_id = 1;
498 int last_group_id = 1;
Neil Armstrongb67d0492019-06-20 09:00:21 +0000499
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200500 /* Group modes */
501 for (const auto &mode : connector_->modes()) {
502 /* Find group for the new mode or create new group */
503 int group_found = 0;
504 for (auto &hwc_config : hwc_configs_) {
505 if (mode.h_display() == hwc_config.second.mode.h_display() &&
506 mode.v_display() == hwc_config.second.mode.v_display()) {
507 group_found = hwc_config.second.group_id;
508 }
509 }
510 if (group_found == 0) {
511 group_found = last_group_id++;
512 }
513
514 bool disabled = false;
515 if (mode.flags() & DRM_MODE_FLAG_3D_MASK) {
516 ALOGI("Disabling display mode %s (Modes with 3D flag aren't supported)",
517 mode.name().c_str());
518 disabled = true;
519 }
520
521 /* Add config */
522 hwc_configs_[last_config_id] = {
523 .id = last_config_id,
524 .group_id = group_found,
525 .mode = mode,
526 .disabled = disabled,
527 };
528
529 /* Chwck if the mode is preferred */
530 if ((mode.type() & DRM_MODE_TYPE_PREFERRED) != 0 &&
531 preferred_config_id_ == 0) {
532 preferred_config_id_ = last_config_id;
533 preferred_config_group_id_ = group_found;
534 }
535
536 last_config_id++;
537 }
538
539 /* We must have preferred mode. Set first mode as preferred
540 * in case KMS haven't reported anything. */
541 if (preferred_config_id_ == 0) {
542 preferred_config_id_ = 1;
543 preferred_config_group_id_ = 1;
544 }
545
546 for (int group = 1; group < last_group_id; group++) {
547 bool has_interlaced = false;
548 bool has_progressive = false;
549 for (auto &hwc_config : hwc_configs_) {
550 if (hwc_config.second.group_id != group || hwc_config.second.disabled) {
551 continue;
552 }
553
554 if (hwc_config.second.IsInterlaced()) {
555 has_interlaced = true;
556 } else {
557 has_progressive = true;
558 }
559 }
560
561 bool has_both = has_interlaced && has_progressive;
562 if (!has_both) {
563 continue;
564 }
565
566 bool group_contains_preferred_interlaced = false;
567 if (group == preferred_config_group_id_ &&
568 hwc_configs_[preferred_config_id_].IsInterlaced()) {
569 group_contains_preferred_interlaced = true;
570 }
571
572 for (auto &hwc_config : hwc_configs_) {
573 if (hwc_config.second.group_id != group || hwc_config.second.disabled) {
574 continue;
575 }
576
577 bool disable = group_contains_preferred_interlaced
578 ? !hwc_config.second.IsInterlaced()
579 : hwc_config.second.IsInterlaced();
580
581 if (disable) {
582 ALOGI(
583 "Group %i: Disabling display mode %s (This group should consist "
584 "of %s modes)",
585 group, hwc_config.second.mode.name().c_str(),
586 group_contains_preferred_interlaced ? "interlaced"
587 : "progressive");
588
589 hwc_config.second.disabled = true;
590 }
591 }
592 }
593
594 /* Group should not contain 2 modes with FPS delta less than ~1HZ
595 * otherwise android.graphics.cts.SetFrameRateTest CTS will fail
596 */
597 for (int m1 = 1; m1 < last_config_id; m1++) {
598 for (int m2 = 1; m2 < last_config_id; m2++) {
599 if (m1 != m2 &&
600 hwc_configs_[m1].group_id == hwc_configs_[m2].group_id &&
601 !hwc_configs_[m1].disabled && !hwc_configs_[m2].disabled &&
602 fabsf(hwc_configs_[m1].mode.v_refresh() -
603 hwc_configs_[m2].mode.v_refresh()) < 1.0) {
604 ALOGI(
605 "Group %i: Disabling display mode %s (Refresh rate value is "
606 "too close to existing mode %s)",
607 hwc_configs_[m2].group_id, hwc_configs_[m2].mode.name().c_str(),
608 hwc_configs_[m1].mode.name().c_str());
609
610 hwc_configs_[m2].disabled = true;
611 }
612 }
613 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000614 }
615
616 uint32_t idx = 0;
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200617 for (auto &hwc_config : hwc_configs_) {
618 if (hwc_config.second.disabled) {
619 continue;
620 }
621
622 if (configs != nullptr) {
623 if (idx >= *num_configs) {
624 break;
625 }
626 configs[idx] = hwc_config.second.id;
627 }
628
629 idx++;
Sean Paulac874152016-03-10 16:00:26 -0500630 }
631 *num_configs = idx;
632 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500633}
634
635HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500636 std::ostringstream stream;
637 stream << "display-" << connector_->id();
638 std::string string = stream.str();
639 size_t length = string.length();
640 if (!name) {
641 *size = length;
642 return HWC2::Error::None;
643 }
644
645 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
646 strncpy(name, string.c_str(), *size);
647 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500648}
649
Roman Stratiienko7f576ec2021-12-22 17:34:18 +0200650HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(
651 int32_t * /*display_requests*/, uint32_t *num_elements,
652 hwc2_layer_t * /*layers*/, int32_t * /*layer_requests*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200653 // TODO(nobody): I think virtual display should request
Sean Paulac874152016-03-10 16:00:26 -0500654 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
Sean Paulac874152016-03-10 16:00:26 -0500655 *num_elements = 0;
656 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500657}
658
659HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500660 *type = static_cast<int32_t>(type_);
661 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500662}
663
664HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500665 *support = 0;
666 return HWC2::Error::None;
667}
668
669HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400670 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
671 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500672 *num_types = 0;
673 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500674}
675
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300676/* Find API details at:
677 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
678 */
Sean Pauled2ec4b2016-03-10 15:35:40 -0500679HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500680 hwc2_layer_t *layers,
681 int32_t *fences) {
Sean Paulac874152016-03-10 16:00:26 -0500682 uint32_t num_layers = 0;
683
684 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
685 ++num_layers;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200686 if (layers == nullptr || fences == nullptr)
Sean Paulac874152016-03-10 16:00:26 -0500687 continue;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200688
689 if (num_layers > *num_elements) {
Sean Paulac874152016-03-10 16:00:26 -0500690 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
691 return HWC2::Error::None;
692 }
693
694 layers[num_layers - 1] = l.first;
Roman Stratiienko0fade372021-02-20 13:59:55 +0200695 fences[num_layers - 1] = l.second.release_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500696 }
697 *num_elements = num_layers;
698 return HWC2::Error::None;
699}
700
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300701HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Sean Paulac874152016-03-10 16:00:26 -0500702 // order the layers by z-order
703 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100704 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500705 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
706 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200707 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500708 case HWC2::Composition::Device:
709 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
710 break;
711 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100712 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500713 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100714 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500715 break;
716 default:
717 continue;
718 }
719 }
720 if (use_client_layer)
721 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
722
Rob Herring4f6c62e2018-05-17 14:33:02 -0500723 if (z_map.empty())
724 return HWC2::Error::BadLayer;
725
Matvii Zorin5368b732021-01-12 10:53:08 +0200726 std::vector<DrmHwcLayer> composition_layers;
727
Sean Paulac874152016-03-10 16:00:26 -0500728 // now that they're ordered by z, add them to the composition
729 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
730 DrmHwcLayer layer;
731 l.second->PopulateDrmLayer(&layer);
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200732 int ret = layer.ImportBuffer(drm_);
Sean Paulac874152016-03-10 16:00:26 -0500733 if (ret) {
734 ALOGE("Failed to import layer, ret=%d", ret);
735 return HWC2::Error::NoResources;
736 }
Matvii Zorin5368b732021-01-12 10:53:08 +0200737 composition_layers.emplace_back(std::move(layer));
Sean Paulac874152016-03-10 16:00:26 -0500738 }
Sean Paulac874152016-03-10 16:00:26 -0500739
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300740 auto composition = std::make_shared<DrmDisplayComposition>(crtc_,
Matvii Zorin704ea0e2021-01-08 12:55:45 +0200741 planner_.get());
Sean Paulac874152016-03-10 16:00:26 -0500742
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200743 // TODO(nobody): Don't always assume geometry changed
Matvii Zorin5368b732021-01-12 10:53:08 +0200744 int ret = composition->SetLayers(composition_layers.data(),
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300745 composition_layers.size());
Sean Paulac874152016-03-10 16:00:26 -0500746 if (ret) {
747 ALOGE("Failed to set layers in the composition ret=%d", ret);
748 return HWC2::Error::BadLayer;
749 }
750
751 std::vector<DrmPlane *> primary_planes(primary_planes_);
752 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500753 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500754 if (ret) {
John Stultz66763d52021-08-24 04:59:25 +0000755 ALOGV("Failed to plan the composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500756 return HWC2::Error::BadConfig;
757 }
758
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300759 a_args.composition = composition;
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300760 ret = compositor_.ExecuteAtomicCommit(a_args);
761
Sean Paulac874152016-03-10 16:00:26 -0500762 if (ret) {
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300763 if (!a_args.test_only)
John Stultz78c9f6c2018-05-24 16:43:35 -0700764 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500765 return HWC2::Error::BadParameter;
766 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500767 return HWC2::Error::None;
768}
769
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300770/* Find API details at:
771 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
772 */
Matteo Franchinc56eede2019-12-03 17:10:38 +0000773HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500774 HWC2::Error ret;
775
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200776 ++total_stats_.total_frames_;
777
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300778 AtomicCommitArgs a_args{};
779 ret = CreateComposition(a_args);
780
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200781 if (ret != HWC2::Error::None)
782 ++total_stats_.failed_kms_present_;
783
Rob Herring4f6c62e2018-05-17 14:33:02 -0500784 if (ret == HWC2::Error::BadLayer) {
785 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000786 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500787 return HWC2::Error::None;
788 }
789 if (ret != HWC2::Error::None)
790 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500791
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300792 *present_fence = a_args.out_fence.Release();
Sean Paulac874152016-03-10 16:00:26 -0500793
794 ++frame_no_;
795 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500796}
797
798HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200799 int conf = static_cast<int>(config);
800
801 if (hwc_configs_.count(conf) == 0) {
802 ALOGE("Could not find active mode for %d", conf);
Sean Paulac874152016-03-10 16:00:26 -0500803 return HWC2::Error::BadConfig;
804 }
805
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200806 auto &mode = hwc_configs_[conf].mode;
807
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300808 AtomicCommitArgs a_args = {
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200809 .display_mode = mode,
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300810 .clear_active_composition = true,
811 };
812
813 int err = compositor_.ExecuteAtomicCommit(a_args);
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300814 if (err != 0) {
815 ALOGE("Failed to queue mode changing commit %d", err);
Sean Paulac874152016-03-10 16:00:26 -0500816 return HWC2::Error::BadConfig;
817 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300818
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200819 active_config_id_ = conf;
Sean Paulac874152016-03-10 16:00:26 -0500820
821 // Setup the client layer's dimensions
822 hwc_rect_t display_frame = {.left = 0,
823 .top = 0,
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200824 .right = static_cast<int>(mode.h_display()),
825 .bottom = static_cast<int>(mode.v_display())};
Sean Paulac874152016-03-10 16:00:26 -0500826 client_layer_.SetLayerDisplayFrame(display_frame);
Sean Paulac874152016-03-10 16:00:26 -0500827
828 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500829}
830
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300831/* Find API details at:
832 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
833 */
Sean Pauled2ec4b2016-03-10 15:35:40 -0500834HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
835 int32_t acquire_fence,
836 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600837 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500838 client_layer_.set_buffer(target);
John Stultz8adf5442021-07-31 00:19:50 +0000839 client_layer_.acquire_fence_ = UniqueFd(acquire_fence);
Sean Paulac874152016-03-10 16:00:26 -0500840 client_layer_.SetLayerDataspace(dataspace);
Roman Stratiienko33365c22020-10-10 23:06:36 +0300841
842 /* TODO: Do not update source_crop every call.
843 * It makes sense to do it once after every hotplug event. */
844 hwc_drm_bo bo{};
845 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
846
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200847 hwc_frect_t source_crop = {.left = 0.0F,
848 .top = 0.0F,
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300849 .right = static_cast<float>(bo.width),
850 .bottom = static_cast<float>(bo.height)};
Roman Stratiienko33365c22020-10-10 23:06:36 +0300851 client_layer_.SetLayerSourceCrop(source_crop);
852
Sean Paulac874152016-03-10 16:00:26 -0500853 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500854}
855
856HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Roman Stratiienkod146d6d2020-10-04 23:56:46 +0300857 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
858 return HWC2::Error::BadParameter;
859
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800860 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +0300861 return HWC2::Error::Unsupported;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800862
863 color_mode_ = mode;
864 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500865}
866
867HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500868 int32_t hint) {
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200869 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
870 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
871 return HWC2::Error::BadParameter;
872
873 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
874 return HWC2::Error::BadParameter;
875
876 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
877 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
878 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
879
880 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500881}
882
Roman Stratiienko7f576ec2021-12-22 17:34:18 +0200883HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t /*buffer*/,
884 int32_t /*release_fence*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200885 // TODO(nobody): Need virtual display support
Roman Stratiienko7f576ec2021-12-22 17:34:18 +0200886 return HWC2::Error::Unsupported;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500887}
888
Sean Paulac874152016-03-10 16:00:26 -0500889HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
Sean Paulac874152016-03-10 16:00:26 -0500890 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300891 AtomicCommitArgs a_args{};
892
Sean Paulac874152016-03-10 16:00:26 -0500893 switch (mode) {
894 case HWC2::PowerMode::Off:
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300895 a_args.active = false;
Sean Paulac874152016-03-10 16:00:26 -0500896 break;
897 case HWC2::PowerMode::On:
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300898 a_args.active = true;
Sean Paulac874152016-03-10 16:00:26 -0500899 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100900 case HWC2::PowerMode::Doze:
901 case HWC2::PowerMode::DozeSuspend:
902 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500903 default:
904 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100905 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500906 };
907
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300908 int err = compositor_.ExecuteAtomicCommit(a_args);
909 if (err) {
910 ALOGE("Failed to apply the dpms composition err=%d", err);
Sean Paulac874152016-03-10 16:00:26 -0500911 return HWC2::Error::BadParameter;
912 }
913 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500914}
915
916HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300917 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500918 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500919}
920
921HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500922 uint32_t *num_requests) {
Matvii Zorinef3c7972020-08-11 15:15:44 +0300923 return backend_->ValidateDisplay(this, num_types, num_requests);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500924}
925
Matvii Zorined90ef92021-01-29 18:32:06 +0200926std::vector<DrmHwcTwo::HwcLayer *>
927DrmHwcTwo::HwcDisplay::GetOrderLayersByZPos() {
928 std::vector<DrmHwcTwo::HwcLayer *> ordered_layers;
929 ordered_layers.reserve(layers_.size());
930
931 for (auto &[handle, layer] : layers_) {
932 ordered_layers.emplace_back(&layer);
933 }
934
935 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
936 [](const DrmHwcTwo::HwcLayer *lhs, const DrmHwcTwo::HwcLayer *rhs) {
937 return lhs->z_order() < rhs->z_order();
938 });
939
940 return ordered_layers;
941}
942
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300943#if PLATFORM_SDK_VERSION > 29
944HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
945 if (connector_->internal())
946 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
947 else if (connector_->external())
948 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
949 else
950 return HWC2::Error::BadConfig;
951
952 return HWC2::Error::None;
953}
954
955HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayVsyncPeriod(
956 hwc2_vsync_period_t *outVsyncPeriod /* ns */) {
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200957 return GetDisplayAttribute(active_config_id_, HWC2_ATTRIBUTE_VSYNC_PERIOD,
958 (int32_t *)(outVsyncPeriod));
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300959}
960
961HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfigWithConstraints(
962 hwc2_config_t /*config*/,
963 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
964 hwc_vsync_period_change_timeline_t *outTimeline) {
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300965 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
966 return HWC2::Error::BadParameter;
967 }
968
969 return HWC2::Error::BadConfig;
970}
971
972HWC2::Error DrmHwcTwo::HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
973 return HWC2::Error::Unsupported;
974}
975
976HWC2::Error DrmHwcTwo::HwcDisplay::GetSupportedContentTypes(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200977 uint32_t *outNumSupportedContentTypes,
978 const uint32_t *outSupportedContentTypes) {
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300979 if (outSupportedContentTypes == nullptr)
980 *outNumSupportedContentTypes = 0;
981
982 return HWC2::Error::None;
983}
984
985HWC2::Error DrmHwcTwo::HwcDisplay::SetContentType(int32_t contentType) {
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300986 if (contentType != HWC2_CONTENT_TYPE_NONE)
987 return HWC2::Error::Unsupported;
988
989 /* TODO: Map to the DRM Connector property:
990 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
991 */
992
993 return HWC2::Error::None;
994}
995#endif
996
John Stultz8c7229d2020-02-07 21:31:08 +0000997#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800998HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
999 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
Roman Stratiienko3e8ce572021-09-29 12:46:28 +03001000 auto blob = connector_->GetEdidBlob();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001001
Roman Stratiienko3e8ce572021-09-29 12:46:28 +03001002 if (!blob) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001003 ALOGE("Failed to get edid property value.");
1004 return HWC2::Error::Unsupported;
1005 }
1006
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +03001007 if (outData) {
1008 *outDataSize = std::min(*outDataSize, blob->length);
1009 memcpy(outData, blob->data, *outDataSize);
1010 } else {
1011 *outDataSize = blob->length;
1012 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001013 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001014
1015 return HWC2::Error::None;
1016}
1017
1018HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
Roman Stratiienko7f576ec2021-12-22 17:34:18 +02001019 uint32_t *outNumCapabilities, uint32_t * /*outCapabilities*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001020 if (outNumCapabilities == nullptr) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001021 return HWC2::Error::BadParameter;
1022 }
1023
1024 *outNumCapabilities = 0;
1025
1026 return HWC2::Error::None;
1027}
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001028
1029HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
1030 bool *supported) {
1031 *supported = false;
1032 return HWC2::Error::None;
1033}
1034
1035HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
1036 float /* brightness */) {
1037 return HWC2::Error::Unsupported;
1038}
1039
John Stultz8c7229d2020-02-07 21:31:08 +00001040#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001041
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001042#if PLATFORM_SDK_VERSION > 27
1043
1044HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
1045 int32_t mode, uint32_t *outNumIntents,
1046 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1047 if (mode != HAL_COLOR_MODE_NATIVE) {
1048 return HWC2::Error::BadParameter;
1049 }
1050
1051 if (outIntents == nullptr) {
1052 *outNumIntents = 1;
1053 return HWC2::Error::None;
1054 }
1055 *outNumIntents = 1;
1056 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1057 return HWC2::Error::None;
1058}
1059
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001060HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
1061 int32_t intent) {
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001062 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1063 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1064 return HWC2::Error::BadParameter;
1065
1066 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
1067 return HWC2::Error::BadParameter;
1068
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001069 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001070 return HWC2::Error::Unsupported;
1071
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001072 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001073 return HWC2::Error::Unsupported;
1074
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001075 color_mode_ = mode;
1076 return HWC2::Error::None;
1077}
1078
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001079#endif /* PLATFORM_SDK_VERSION > 27 */
1080
Roman Stratiienkoaaaa4692021-09-29 12:50:10 +03001081HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t /*x*/,
1082 int32_t /*y*/) {
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001083 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001084}
1085
1086HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Roman Stratiienko5063d532021-09-29 12:47:31 +03001087 switch (static_cast<HWC2::BlendMode>(mode)) {
1088 case HWC2::BlendMode::None:
1089 blending_ = DrmHwcBlending::kNone;
1090 break;
1091 case HWC2::BlendMode::Premultiplied:
1092 blending_ = DrmHwcBlending::kPreMult;
1093 break;
1094 case HWC2::BlendMode::Coverage:
1095 blending_ = DrmHwcBlending::kCoverage;
1096 break;
1097 default:
1098 ALOGE("Unknown blending mode b=%d", blending_);
1099 blending_ = DrmHwcBlending::kNone;
1100 break;
1101 }
Sean Paulac874152016-03-10 16:00:26 -05001102 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001103}
1104
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +03001105/* Find API details at:
1106 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=2314
1107 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001108HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001109 int32_t acquire_fence) {
Sean Paulac874152016-03-10 16:00:26 -05001110 set_buffer(buffer);
John Stultz8adf5442021-07-31 00:19:50 +00001111 acquire_fence_ = UniqueFd(acquire_fence);
Sean Paulac874152016-03-10 16:00:26 -05001112 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001113}
1114
Roman Stratiienkoaaaa4692021-09-29 12:50:10 +03001115HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t /*color*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001116 // TODO(nobody): Put to client composition here?
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001117 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001118}
1119
1120HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001121 sf_type_ = static_cast<HWC2::Composition>(type);
1122 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001123}
1124
1125HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Roman Stratiienko515da8f2021-09-29 12:47:21 +03001126 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
1127 case HAL_DATASPACE_STANDARD_BT709:
1128 color_space_ = DrmHwcColorSpace::kItuRec709;
1129 break;
1130 case HAL_DATASPACE_STANDARD_BT601_625:
1131 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
1132 case HAL_DATASPACE_STANDARD_BT601_525:
1133 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
1134 color_space_ = DrmHwcColorSpace::kItuRec601;
1135 break;
1136 case HAL_DATASPACE_STANDARD_BT2020:
1137 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
1138 color_space_ = DrmHwcColorSpace::kItuRec2020;
1139 break;
1140 default:
1141 color_space_ = DrmHwcColorSpace::kUndefined;
1142 }
1143
1144 switch (dataspace & HAL_DATASPACE_RANGE_MASK) {
1145 case HAL_DATASPACE_RANGE_FULL:
1146 sample_range_ = DrmHwcSampleRange::kFullRange;
1147 break;
1148 case HAL_DATASPACE_RANGE_LIMITED:
1149 sample_range_ = DrmHwcSampleRange::kLimitedRange;
1150 break;
1151 default:
1152 sample_range_ = DrmHwcSampleRange::kUndefined;
1153 }
Sean Paulac874152016-03-10 16:00:26 -05001154 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001155}
1156
1157HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001158 display_frame_ = frame;
1159 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001160}
1161
1162HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001163 alpha_ = alpha;
1164 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001165}
1166
1167HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
Roman Stratiienko7f576ec2021-12-22 17:34:18 +02001168 const native_handle_t * /*stream*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001169 // TODO(nobody): We don't support sideband
Roman Stratiienko7f576ec2021-12-22 17:34:18 +02001170 return HWC2::Error::Unsupported;
1171 ;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001172}
1173
1174HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001175 source_crop_ = crop;
1176 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001177}
1178
Roman Stratiienko7f576ec2021-12-22 17:34:18 +02001179HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(
1180 hwc_region_t /*damage*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001181 // TODO(nobody): We don't use surface damage, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001182 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001183}
1184
1185HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Roman Stratiienko2ed4cbe2021-09-29 12:47:35 +03001186 uint32_t l_transform = 0;
1187
1188 // 270* and 180* cannot be combined with flips. More specifically, they
1189 // already contain both horizontal and vertical flips, so those fields are
1190 // redundant in this case. 90* rotation can be combined with either horizontal
1191 // flip or vertical flip, so treat it differently
1192 if (transform == HWC_TRANSFORM_ROT_270) {
1193 l_transform = DrmHwcTransform::kRotate270;
1194 } else if (transform == HWC_TRANSFORM_ROT_180) {
1195 l_transform = DrmHwcTransform::kRotate180;
1196 } else {
1197 if (transform & HWC_TRANSFORM_FLIP_H)
1198 l_transform |= DrmHwcTransform::kFlipH;
1199 if (transform & HWC_TRANSFORM_FLIP_V)
1200 l_transform |= DrmHwcTransform::kFlipV;
1201 if (transform & HWC_TRANSFORM_ROT_90)
1202 l_transform |= DrmHwcTransform::kRotate90;
1203 }
1204
1205 transform_ = static_cast<DrmHwcTransform>(l_transform);
Sean Paulac874152016-03-10 16:00:26 -05001206 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001207}
1208
Roman Stratiienko7f576ec2021-12-22 17:34:18 +02001209HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(
1210 hwc_region_t /*visible*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001211 // TODO(nobody): We don't use this information, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001212 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001213}
1214
Sean Paulac874152016-03-10 16:00:26 -05001215HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
Sean Paulac874152016-03-10 16:00:26 -05001216 z_order_ = order;
1217 return HWC2::Error::None;
1218}
1219
1220void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
Sean Paulac874152016-03-10 16:00:26 -05001221 layer->sf_handle = buffer_;
Roman Stratiienko0fade372021-02-20 13:59:55 +02001222 // TODO(rsglobal): Avoid extra fd duplication
1223 layer->acquire_fence = UniqueFd(fcntl(acquire_fence_.Get(), F_DUPFD_CLOEXEC));
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001224 layer->display_frame = display_frame_;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001225 layer->alpha = lround(65535.0F * alpha_);
Roman Stratiienko5063d532021-09-29 12:47:31 +03001226 layer->blending = blending_;
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001227 layer->source_crop = source_crop_;
Roman Stratiienko2ed4cbe2021-09-29 12:47:35 +03001228 layer->transform = transform_;
Roman Stratiienko515da8f2021-09-29 12:47:21 +03001229 layer->color_space = color_space_;
1230 layer->sample_range = sample_range_;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001231}
1232
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001233void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +03001234 const std::lock_guard<std::mutex> lock(callback_lock_);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001235
Roman Stratiienko863a3c22021-09-29 13:00:29 +03001236 if (hotplug_callback_.first != nullptr &&
1237 hotplug_callback_.second != nullptr) {
1238 hotplug_callback_.first(hotplug_callback_.second, displayid,
1239 state == DRM_MODE_CONNECTED
1240 ? HWC2_CONNECTION_CONNECTED
1241 : HWC2_CONNECTION_DISCONNECTED);
1242 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001243}
1244
1245void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
Roman Stratiienkod21071f2021-03-09 21:56:50 +02001246 for (const auto &conn : drmDevice->connectors()) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001247 if (conn->state() != DRM_MODE_CONNECTED)
1248 continue;
1249 HandleDisplayHotplug(conn->display(), conn->state());
1250 }
1251}
1252
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001253void DrmHwcTwo::HandleHotplugUEvent() {
1254 for (const auto &drm : resource_manager_.getDrmDevices()) {
1255 for (const auto &conn : drm->connectors()) {
1256 drmModeConnection old_state = conn->state();
1257 drmModeConnection cur_state = conn->UpdateModes()
1258 ? DRM_MODE_UNKNOWNCONNECTION
1259 : conn->state();
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001260
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001261 if (cur_state == old_state)
1262 continue;
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001263
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001264 ALOGI("%s event for connector %u on display %d",
1265 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", conn->id(),
1266 conn->display());
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001267
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001268 int display_id = conn->display();
1269 if (cur_state == DRM_MODE_CONNECTED) {
1270 auto &display = displays_.at(display_id);
1271 display.ChosePreferredConfig();
1272 } else {
1273 auto &display = displays_.at(display_id);
1274 display.ClearDisplay();
1275 }
1276
1277 HandleDisplayHotplug(display_id, cur_state);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001278 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001279 }
1280}
1281
Sean Pauled2ec4b2016-03-10 15:35:40 -05001282// static
1283int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
Sean Pauled2ec4b2016-03-10 15:35:40 -05001284 return 0;
1285}
1286
1287// static
1288void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001289 uint32_t *out_count,
1290 int32_t * /*out_capabilities*/) {
Sean Pauled2ec4b2016-03-10 15:35:40 -05001291 *out_count = 0;
1292}
1293
1294// static
Sean Paulac874152016-03-10 16:00:26 -05001295hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1296 struct hwc2_device * /*dev*/, int32_t descriptor) {
Sean Paulac874152016-03-10 16:00:26 -05001297 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001298 switch (func) {
1299 // Device functions
1300 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1301 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1302 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1303 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001304 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001305 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1306 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1307 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1308 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1309 case HWC2::FunctionDescriptor::Dump:
1310 return ToHook<HWC2_PFN_DUMP>(
1311 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1312 uint32_t *, char *>);
1313 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1314 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1315 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1316 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1317 case HWC2::FunctionDescriptor::RegisterCallback:
1318 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1319 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1320 &DrmHwcTwo::RegisterCallback, int32_t,
1321 hwc2_callback_data_t, hwc2_function_pointer_t>);
1322
1323 // Display functions
1324 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1325 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1326 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1327 &HwcDisplay::AcceptDisplayChanges>);
1328 case HWC2::FunctionDescriptor::CreateLayer:
1329 return ToHook<HWC2_PFN_CREATE_LAYER>(
1330 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1331 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1332 case HWC2::FunctionDescriptor::DestroyLayer:
1333 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1334 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1335 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1336 case HWC2::FunctionDescriptor::GetActiveConfig:
1337 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1338 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1339 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1340 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1341 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1342 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1343 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1344 hwc2_layer_t *, int32_t *>);
1345 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1346 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1347 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1348 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1349 int32_t, int32_t>);
1350 case HWC2::FunctionDescriptor::GetColorModes:
1351 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1352 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1353 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1354 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001355 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1356 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1357 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1358 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001359 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001360 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1361 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1362 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1363 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001364 case HWC2::FunctionDescriptor::GetDisplayName:
1365 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1366 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1367 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1368 case HWC2::FunctionDescriptor::GetDisplayRequests:
1369 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1370 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1371 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1372 hwc2_layer_t *, int32_t *>);
1373 case HWC2::FunctionDescriptor::GetDisplayType:
1374 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1375 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1376 &HwcDisplay::GetDisplayType, int32_t *>);
1377 case HWC2::FunctionDescriptor::GetDozeSupport:
1378 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1379 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1380 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001381 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1382 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1383 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1384 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1385 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001386 case HWC2::FunctionDescriptor::GetReleaseFences:
1387 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1388 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1389 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1390 int32_t *>);
1391 case HWC2::FunctionDescriptor::PresentDisplay:
1392 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1393 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1394 &HwcDisplay::PresentDisplay, int32_t *>);
1395 case HWC2::FunctionDescriptor::SetActiveConfig:
1396 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1397 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1398 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1399 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001400 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1401 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1402 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1403 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001404 case HWC2::FunctionDescriptor::SetColorMode:
1405 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1406 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1407 &HwcDisplay::SetColorMode, int32_t>);
1408 case HWC2::FunctionDescriptor::SetColorTransform:
1409 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1410 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1411 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1412 case HWC2::FunctionDescriptor::SetOutputBuffer:
1413 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1414 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1415 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1416 case HWC2::FunctionDescriptor::SetPowerMode:
1417 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1418 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1419 &HwcDisplay::SetPowerMode, int32_t>);
1420 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1421 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1422 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1423 &HwcDisplay::SetVsyncEnabled, int32_t>);
1424 case HWC2::FunctionDescriptor::ValidateDisplay:
1425 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1426 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1427 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001428#if PLATFORM_SDK_VERSION > 27
1429 case HWC2::FunctionDescriptor::GetRenderIntents:
1430 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1431 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1432 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1433 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001434 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1435 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1436 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1437 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001438#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001439#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001440 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1441 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1442 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1443 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1444 uint32_t *, uint8_t *>);
1445 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1446 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1447 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1448 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1449 uint32_t *>);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001450 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1451 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1452 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1453 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1454 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1455 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1456 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1457 &HwcDisplay::SetDisplayBrightness, float>);
John Stultz8c7229d2020-02-07 21:31:08 +00001458#endif /* PLATFORM_SDK_VERSION > 28 */
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001459#if PLATFORM_SDK_VERSION > 29
1460 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
1461 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
1462 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
1463 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
1464 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
1465 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
1466 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
1467 &HwcDisplay::GetDisplayVsyncPeriod,
1468 hwc2_vsync_period_t *>);
1469 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
1470 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
1471 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
1472 &HwcDisplay::SetActiveConfigWithConstraints,
1473 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
1474 hwc_vsync_period_change_timeline_t *>);
1475 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
1476 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
1477 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
1478 &HwcDisplay::SetAutoLowLatencyMode, bool>);
1479 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
1480 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
1481 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
1482 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
1483 uint32_t *>);
1484 case HWC2::FunctionDescriptor::SetContentType:
1485 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
1486 DisplayHook<decltype(&HwcDisplay::SetContentType),
1487 &HwcDisplay::SetContentType, int32_t>);
1488#endif
Sean Pauled2ec4b2016-03-10 15:35:40 -05001489 // Layer functions
1490 case HWC2::FunctionDescriptor::SetCursorPosition:
1491 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1492 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1493 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1494 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1495 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1496 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1497 &HwcLayer::SetLayerBlendMode, int32_t>);
1498 case HWC2::FunctionDescriptor::SetLayerBuffer:
1499 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1500 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1501 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1502 case HWC2::FunctionDescriptor::SetLayerColor:
1503 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1504 LayerHook<decltype(&HwcLayer::SetLayerColor),
1505 &HwcLayer::SetLayerColor, hwc_color_t>);
1506 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1507 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1508 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1509 &HwcLayer::SetLayerCompositionType, int32_t>);
1510 case HWC2::FunctionDescriptor::SetLayerDataspace:
1511 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1512 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1513 &HwcLayer::SetLayerDataspace, int32_t>);
1514 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1515 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1516 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1517 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1518 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1519 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1520 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1521 &HwcLayer::SetLayerPlaneAlpha, float>);
1522 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001523 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1524 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1525 &HwcLayer::SetLayerSidebandStream,
1526 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001527 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1528 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1529 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1530 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1531 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1532 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1533 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1534 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1535 case HWC2::FunctionDescriptor::SetLayerTransform:
1536 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1537 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1538 &HwcLayer::SetLayerTransform, int32_t>);
1539 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1540 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1541 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1542 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1543 case HWC2::FunctionDescriptor::SetLayerZOrder:
1544 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1545 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1546 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001547 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001548 default:
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001549 return nullptr;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001550 }
1551}
Sean Paulac874152016-03-10 16:00:26 -05001552
1553// static
1554int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1555 struct hw_device_t **dev) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001556 if (strcmp(name, HWC_HARDWARE_COMPOSER) != 0) {
Sean Paulac874152016-03-10 16:00:26 -05001557 ALOGE("Invalid module name- %s", name);
1558 return -EINVAL;
1559 }
1560
1561 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1562 if (!ctx) {
1563 ALOGE("Failed to allocate DrmHwcTwo");
1564 return -ENOMEM;
1565 }
1566
1567 HWC2::Error err = ctx->Init();
1568 if (err != HWC2::Error::None) {
1569 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1570 return -EINVAL;
1571 }
1572
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001573 ctx->common.module = (hw_module_t *)module;
Sean Paulac874152016-03-10 16:00:26 -05001574 *dev = &ctx->common;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001575 ctx.release(); // NOLINT(bugprone-unused-return-value)
Sean Paulac874152016-03-10 16:00:26 -05001576 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001577}
Sean Paulf72cccd2018-08-27 13:59:08 -04001578} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001579
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001580// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001581static struct hw_module_methods_t hwc2_module_methods = {
1582 .open = android::DrmHwcTwo::HookDevOpen,
1583};
1584
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001585// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001586hw_module_t HAL_MODULE_INFO_SYM = {
1587 .tag = HARDWARE_MODULE_TAG,
1588 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1589 .id = HWC_HARDWARE_MODULE_ID,
1590 .name = "DrmHwcTwo module",
1591 .author = "The Android Open Source Project",
1592 .methods = &hwc2_module_methods,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001593 .dso = nullptr,
Sean Paulac874152016-03-10 16:00:26 -05001594 .reserved = {0},
1595};