blob: dada168f99cd84a3495c131afec1bab1968c9ae9 [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 Stratiienkod21071f2021-03-09 21:56:50 +020090 const auto &drm_devices = resource_manager_.getDrmDevices();
91 for (const auto &device : drm_devices) {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020092 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030093 device->RegisterHotplugHandler(new DrmHotplugHandler(this, device.get()));
94 }
95 return ret;
96}
97
Sean Pauled2ec4b2016-03-10 15:35:40 -050098template <typename... Args>
99static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
100 ALOGV("Unsupported function: %s", func);
101 return HWC2::Error::Unsupported;
102}
103
Sean Paulac874152016-03-10 16:00:26 -0500104static inline void supported(char const *func) {
105 ALOGV("Supported function: %s", func);
106}
107
Sean Pauled2ec4b2016-03-10 15:35:40 -0500108HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t width, uint32_t height,
109 int32_t *format,
110 hwc2_display_t *display) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200111 // TODO(nobody): Implement virtual display
Sean Paulac874152016-03-10 16:00:26 -0500112 return unsupported(__func__, width, height, format, display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500113}
114
115HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t display) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200116 // TODO(nobody): Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500117 return unsupported(__func__, display);
118}
119
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200120std::string DrmHwcTwo::HwcDisplay::DumpDelta(
121 DrmHwcTwo::HwcDisplay::Stats delta) {
122 if (delta.total_pixops_ == 0)
123 return "No stats yet";
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200124 double ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200125
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200126 std::stringstream ss;
127 ss << " Total frames count: " << delta.total_frames_ << "\n"
128 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
129 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
130 << ((delta.failed_kms_present_ > 0)
131 ? " !!! Internal failure, FIX it please\n"
132 : "")
133 << " Flattened frames: " << delta.frames_flattened_ << "\n"
134 << " Pixel operations (free units)"
135 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
136 << "]\n"
137 << " Composition efficiency: " << ratio;
138
139 return ss.str();
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200140}
141
142std::string DrmHwcTwo::HwcDisplay::Dump() {
Roman Stratiienkoe8c06792021-09-30 10:09:31 +0300143 std::string flattening_state_str;
144 switch (flattenning_state_) {
145 case ClientFlattenningState::Disabled:
146 flattening_state_str = "Disabled";
147 break;
148 case ClientFlattenningState::NotRequired:
149 flattening_state_str = "Not needed";
150 break;
151 case ClientFlattenningState::Flattened:
152 flattening_state_str = "Active";
153 break;
154 case ClientFlattenningState::ClientRefreshRequested:
155 flattening_state_str = "Refresh requested";
156 break;
157 default:
158 flattening_state_str = std::to_string(flattenning_state_) +
159 " VSync remains";
160 }
161
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200162 std::stringstream ss;
163 ss << "- Display on: " << connector_->name() << "\n"
Roman Stratiienkoe8c06792021-09-30 10:09:31 +0300164 << " Flattening state: " << flattening_state_str << "\n"
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200165 << "Statistics since system boot:\n"
166 << DumpDelta(total_stats_) << "\n\n"
167 << "Statistics since last dumpsys request:\n"
168 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200169
170 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200171 return ss.str();
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200172}
173
174void DrmHwcTwo::Dump(uint32_t *outSize, char *outBuffer) {
175 supported(__func__);
176
177 if (outBuffer != nullptr) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200178 auto copied_bytes = mDumpString.copy(outBuffer, *outSize);
179 *outSize = static_cast<uint32_t>(copied_bytes);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200180 return;
181 }
182
183 std::stringstream output;
184
185 output << "-- drm_hwcomposer --\n\n";
186
187 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &dp : displays_)
188 output << dp.second.Dump();
189
190 mDumpString = output.str();
191 *outSize = static_cast<uint32_t>(mDumpString.size());
Sean Pauled2ec4b2016-03-10 15:35:40 -0500192}
193
194uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200195 // TODO(nobody): Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500196 unsupported(__func__);
197 return 0;
198}
199
200HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500201 hwc2_callback_data_t data,
202 hwc2_function_pointer_t function) {
203 supported(__func__);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300204
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300205 std::unique_lock<std::mutex> lock(callback_lock_);
206
Roman Stratiienko23701092020-09-26 02:08:41 +0300207 switch (static_cast<HWC2::Callback>(descriptor)) {
Sean Paulac874152016-03-10 16:00:26 -0500208 case HWC2::Callback::Hotplug: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300209 hotplug_callback_ = std::make_pair(HWC2_PFN_HOTPLUG(function), data);
210 lock.unlock();
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200211 const auto &drm_devices = resource_manager_.getDrmDevices();
212 for (const auto &device : drm_devices)
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300213 HandleInitialHotplugState(device.get());
Sean Paulac874152016-03-10 16:00:26 -0500214 break;
215 }
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200216 case HWC2::Callback::Refresh: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300217 refresh_callback_ = std::make_pair(HWC2_PFN_REFRESH(function), data);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200218 break;
219 }
Sean Paulac874152016-03-10 16:00:26 -0500220 case HWC2::Callback::Vsync: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300221 vsync_callback_ = std::make_pair(HWC2_PFN_VSYNC(function), data);
Sean Paulac874152016-03-10 16:00:26 -0500222 break;
223 }
Roman Stratiienko11ef8c52021-09-29 13:01:39 +0300224#if PLATFORM_SDK_VERSION > 29
225 case HWC2::Callback::Vsync_2_4: {
226 vsync_2_4_callback_ = std::make_pair(HWC2_PFN_VSYNC_2_4(function), data);
227 break;
228 }
229#endif
Sean Paulac874152016-03-10 16:00:26 -0500230 default:
231 break;
232 }
233 return HWC2::Error::None;
234}
235
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100236DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200237 DrmDevice *drm, hwc2_display_t handle,
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300238 HWC2::DisplayType type, DrmHwcTwo *hwc2)
239 : hwc2_(hwc2),
240 resource_manager_(resource_manager),
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100241 drm_(drm),
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100242 handle_(handle),
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200243 type_(type),
244 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
Sean Paulac874152016-03-10 16:00:26 -0500245 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200246
247 // clang-format off
248 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
249 0.0, 1.0, 0.0, 0.0,
250 0.0, 0.0, 1.0, 0.0,
251 0.0, 0.0, 0.0, 1.0};
252 // clang-format on
Sean Paulac874152016-03-10 16:00:26 -0500253}
254
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300255void DrmHwcTwo::HwcDisplay::ClearDisplay() {
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300256 AtomicCommitArgs a_args = {.clear_active_composition = true};
257 compositor_.ExecuteAtomicCommit(a_args);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300258}
259
Sean Paulac874152016-03-10 16:00:26 -0500260HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
261 supported(__func__);
262 planner_ = Planner::CreateInstance(drm_);
263 if (!planner_) {
264 ALOGE("Failed to create planner instance for composition");
265 return HWC2::Error::NoResources;
266 }
267
268 int display = static_cast<int>(handle_);
Roman Stratiienkoe8c06792021-09-30 10:09:31 +0300269 int ret = compositor_.Init(resource_manager_, display);
Sean Paulac874152016-03-10 16:00:26 -0500270 if (ret) {
271 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
272 return HWC2::Error::NoResources;
273 }
274
275 // Split up the given display planes into primary and overlay to properly
276 // interface with the composition
277 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -0700278 property_get("vendor.hwc.drm.use_overlay_planes", use_overlay_planes_prop,
279 "1");
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200280 bool use_overlay_planes = strtol(use_overlay_planes_prop, nullptr, 10);
Sean Paulac874152016-03-10 16:00:26 -0500281 for (auto &plane : *planes) {
282 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
283 primary_planes_.push_back(plane);
284 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
285 overlay_planes_.push_back(plane);
286 }
287
288 crtc_ = drm_->GetCrtcForDisplay(display);
289 if (!crtc_) {
290 ALOGE("Failed to get crtc for display %d", display);
291 return HWC2::Error::BadDisplay;
292 }
293
294 connector_ = drm_->GetConnectorForDisplay(display);
295 if (!connector_) {
296 ALOGE("Failed to get connector for display %d", display);
297 return HWC2::Error::BadDisplay;
298 }
299
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300300 ret = vsync_worker_.Init(drm_, display, [this](int64_t timestamp) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300301 const std::lock_guard<std::mutex> lock(hwc2_->callback_lock_);
Roman Stratiienko11ef8c52021-09-29 13:01:39 +0300302 /* vsync callback */
303#if PLATFORM_SDK_VERSION > 29
304 if (hwc2_->vsync_2_4_callback_.first != nullptr &&
305 hwc2_->vsync_2_4_callback_.second != nullptr) {
306 hwc2_vsync_period_t period_ns{};
307 GetDisplayVsyncPeriod(&period_ns);
308 hwc2_->vsync_2_4_callback_.first(hwc2_->vsync_2_4_callback_.second,
309 handle_, timestamp, period_ns);
310 } else
311#endif
312 if (hwc2_->vsync_callback_.first != nullptr &&
313 hwc2_->vsync_callback_.second != nullptr) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300314 hwc2_->vsync_callback_.first(hwc2_->vsync_callback_.second, handle_,
315 timestamp);
316 }
317 });
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300318 if (ret) {
319 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
320 return HWC2::Error::BadDisplay;
321 }
322
Roman Stratiienkoe8c06792021-09-30 10:09:31 +0300323 ret = flattening_vsync_worker_.Init(drm_, display, [this](int64_t /*timestamp*/) {
324 const std::lock_guard<std::mutex> lock(hwc2_->callback_lock_);
325 /* Frontend flattening */
326 if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
327 --flattenning_state_ ==
328 ClientFlattenningState::ClientRefreshRequested &&
329 hwc2_->refresh_callback_.first != nullptr &&
330 hwc2_->refresh_callback_.second != nullptr) {
331 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
332 flattening_vsync_worker_.VSyncControl(false);
333 }
334 });
335 if (ret) {
336 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
337 return HWC2::Error::BadDisplay;
338 }
339
Matvii Zorinef3c7972020-08-11 15:15:44 +0300340 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
341 if (ret) {
342 ALOGE("Failed to set backend for d=%d %d\n", display, ret);
343 return HWC2::Error::BadDisplay;
344 }
345
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300346 return ChosePreferredConfig();
347}
348
349HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() {
Sean Paulac874152016-03-10 16:00:26 -0500350 // Fetch the number of modes from the display
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200351 uint32_t num_configs = 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200352 HWC2::Error err = GetDisplayConfigs(&num_configs, nullptr);
Sean Paulac874152016-03-10 16:00:26 -0500353 if (err != HWC2::Error::None || !num_configs)
354 return err;
355
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200356 return SetActiveConfig(connector_->get_preferred_mode_id());
Sean Paulac874152016-03-10 16:00:26 -0500357}
358
Sean Pauled2ec4b2016-03-10 15:35:40 -0500359HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500360 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500361 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
362 l.second.accept_type_change();
363 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500364}
365
366HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500367 supported(__func__);
368 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
369 *layer = static_cast<hwc2_layer_t>(layer_idx_);
370 ++layer_idx_;
371 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500372}
373
374HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500375 supported(__func__);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100376 if (!get_layer(layer))
377 return HWC2::Error::BadLayer;
378
Sean Paulac874152016-03-10 16:00:26 -0500379 layers_.erase(layer);
380 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500381}
382
383HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500384 supported(__func__);
385 DrmMode const &mode = connector_->active_mode();
386 if (mode.id() == 0)
387 return HWC2::Error::BadConfig;
388
389 *config = mode.id();
390 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500391}
392
393HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
394 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500395 supported(__func__);
396 uint32_t num_changes = 0;
397 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
398 if (l.second.type_changed()) {
399 if (layers && num_changes < *num_elements)
400 layers[num_changes] = l.first;
401 if (types && num_changes < *num_elements)
402 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
403 ++num_changes;
404 }
405 }
406 if (!layers && !types)
407 *num_elements = num_changes;
408 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500409}
410
411HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500412 uint32_t height,
413 int32_t /*format*/,
414 int32_t dataspace) {
415 supported(__func__);
416 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
417 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
418
419 if (width < min.first || height < min.second)
420 return HWC2::Error::Unsupported;
421
422 if (width > max.first || height > max.second)
423 return HWC2::Error::Unsupported;
424
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200425 if (dataspace != HAL_DATASPACE_UNKNOWN)
Sean Paulac874152016-03-10 16:00:26 -0500426 return HWC2::Error::Unsupported;
427
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200428 // TODO(nobody): Validate format can be handled by either GL or planes
Sean Paulac874152016-03-10 16:00:26 -0500429 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500430}
431
432HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500433 int32_t *modes) {
434 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800435 if (!modes)
436 *num_modes = 1;
437
438 if (modes)
439 *modes = HAL_COLOR_MODE_NATIVE;
440
441 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500442}
443
444HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500445 int32_t attribute_in,
446 int32_t *value) {
447 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400448 auto mode = std::find_if(connector_->modes().begin(),
449 connector_->modes().end(),
450 [config](DrmMode const &m) {
451 return m.id() == config;
452 });
Sean Paulac874152016-03-10 16:00:26 -0500453 if (mode == connector_->modes().end()) {
454 ALOGE("Could not find active mode for %d", config);
455 return HWC2::Error::BadConfig;
456 }
457
458 static const int32_t kUmPerInch = 25400;
459 uint32_t mm_width = connector_->mm_width();
460 uint32_t mm_height = connector_->mm_height();
461 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
462 switch (attribute) {
463 case HWC2::Attribute::Width:
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300464 *value = static_cast<int>(mode->h_display());
Sean Paulac874152016-03-10 16:00:26 -0500465 break;
466 case HWC2::Attribute::Height:
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300467 *value = static_cast<int>(mode->v_display());
Sean Paulac874152016-03-10 16:00:26 -0500468 break;
469 case HWC2::Attribute::VsyncPeriod:
470 // in nanoseconds
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300471 *value = static_cast<int>(1E9 / mode->v_refresh());
Sean Paulac874152016-03-10 16:00:26 -0500472 break;
473 case HWC2::Attribute::DpiX:
474 // Dots per 1000 inches
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300475 *value = mm_width
476 ? static_cast<int>(mode->h_display() * kUmPerInch / mm_width)
477 : -1;
Sean Paulac874152016-03-10 16:00:26 -0500478 break;
479 case HWC2::Attribute::DpiY:
480 // Dots per 1000 inches
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300481 *value = mm_height ? static_cast<int>(mode->v_display() * kUmPerInch /
482 mm_height)
483 : -1;
Sean Paulac874152016-03-10 16:00:26 -0500484 break;
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300485#if PLATFORM_SDK_VERSION > 29
486 case HWC2::Attribute::ConfigGroup:
487 *value = 0; /* TODO: Add support for config groups */
488 break;
489#endif
Sean Paulac874152016-03-10 16:00:26 -0500490 default:
491 *value = -1;
492 return HWC2::Error::BadConfig;
493 }
494 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500495}
496
497HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
498 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500499 supported(__func__);
500 // Since this callback is normally invoked twice (once to get the count, and
501 // once to populate configs), we don't really want to read the edid
502 // redundantly. Instead, only update the modes on the first invocation. While
503 // it's possible this will result in stale modes, it'll all come out in the
504 // wash when we try to set the active config later.
505 if (!configs) {
506 int ret = connector_->UpdateModes();
507 if (ret) {
508 ALOGE("Failed to update display modes %d", ret);
509 return HWC2::Error::BadDisplay;
510 }
511 }
512
Neil Armstrongb67d0492019-06-20 09:00:21 +0000513 // Since the upper layers only look at vactive/hactive/refresh, height and
514 // width, it doesn't differentiate interlaced from progressive and other
515 // similar modes. Depending on the order of modes we return to SF, it could
516 // end up choosing a suboptimal configuration and dropping the preferred
517 // mode. To workaround this, don't offer interlaced modes to SF if there is
518 // at least one non-interlaced alternative and only offer a single WxH@R
519 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
520
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200521 // TODO(nobody): Remove the following block of code until AOSP handles all
522 // modes
Neil Armstrongb67d0492019-06-20 09:00:21 +0000523 std::vector<DrmMode> sel_modes;
524
525 // Add the preferred mode first to be sure it's not dropped
526 auto mode = std::find_if(connector_->modes().begin(),
527 connector_->modes().end(), [&](DrmMode const &m) {
528 return m.id() ==
529 connector_->get_preferred_mode_id();
530 });
531 if (mode != connector_->modes().end())
532 sel_modes.push_back(*mode);
533
534 // Add the active mode if different from preferred mode
535 if (connector_->active_mode().id() != connector_->get_preferred_mode_id())
536 sel_modes.push_back(connector_->active_mode());
537
538 // Cycle over the modes and filter out "similar" modes, keeping only the
539 // first ones in the order given by DRM (from CEA ids and timings order)
Sean Paulac874152016-03-10 16:00:26 -0500540 for (const DrmMode &mode : connector_->modes()) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200541 // TODO(nobody): Remove this when 3D Attributes are in AOSP
Neil Armstrongb67d0492019-06-20 09:00:21 +0000542 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
543 continue;
544
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200545 // TODO(nobody): Remove this when the Interlaced attribute is in AOSP
Neil Armstrong4c027a72019-06-04 14:48:02 +0000546 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
547 auto m = std::find_if(connector_->modes().begin(),
548 connector_->modes().end(),
549 [&mode](DrmMode const &m) {
550 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
551 m.h_display() == mode.h_display() &&
552 m.v_display() == mode.v_display();
553 });
Neil Armstrongb67d0492019-06-20 09:00:21 +0000554 if (m == connector_->modes().end())
555 sel_modes.push_back(mode);
556
557 continue;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000558 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000559
560 // Search for a similar WxH@R mode in the filtered list and drop it if
561 // another mode with the same WxH@R has already been selected
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200562 // TODO(nobody): Remove this when AOSP handles duplicates modes
Neil Armstrongb67d0492019-06-20 09:00:21 +0000563 auto m = std::find_if(sel_modes.begin(), sel_modes.end(),
564 [&mode](DrmMode const &m) {
565 return m.h_display() == mode.h_display() &&
566 m.v_display() == mode.v_display() &&
567 m.v_refresh() == mode.v_refresh();
568 });
569 if (m == sel_modes.end())
570 sel_modes.push_back(mode);
571 }
572
573 auto num_modes = static_cast<uint32_t>(sel_modes.size());
574 if (!configs) {
575 *num_configs = num_modes;
576 return HWC2::Error::None;
577 }
578
579 uint32_t idx = 0;
580 for (const DrmMode &mode : sel_modes) {
581 if (idx >= *num_configs)
582 break;
583 configs[idx++] = mode.id();
Sean Paulac874152016-03-10 16:00:26 -0500584 }
585 *num_configs = idx;
586 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500587}
588
589HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500590 supported(__func__);
591 std::ostringstream stream;
592 stream << "display-" << connector_->id();
593 std::string string = stream.str();
594 size_t length = string.length();
595 if (!name) {
596 *size = length;
597 return HWC2::Error::None;
598 }
599
600 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
601 strncpy(name, string.c_str(), *size);
602 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500603}
604
Sean Paulac874152016-03-10 16:00:26 -0500605HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
606 uint32_t *num_elements,
607 hwc2_layer_t *layers,
608 int32_t *layer_requests) {
609 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200610 // TODO(nobody): I think virtual display should request
Sean Paulac874152016-03-10 16:00:26 -0500611 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
612 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
613 *num_elements = 0;
614 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500615}
616
617HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500618 supported(__func__);
619 *type = static_cast<int32_t>(type_);
620 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500621}
622
623HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500624 supported(__func__);
625 *support = 0;
626 return HWC2::Error::None;
627}
628
629HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400630 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
631 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500632 supported(__func__);
633 *num_types = 0;
634 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500635}
636
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300637/* Find API details at:
638 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
639 */
Sean Pauled2ec4b2016-03-10 15:35:40 -0500640HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500641 hwc2_layer_t *layers,
642 int32_t *fences) {
643 supported(__func__);
644 uint32_t num_layers = 0;
645
646 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
647 ++num_layers;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200648 if (layers == nullptr || fences == nullptr)
Sean Paulac874152016-03-10 16:00:26 -0500649 continue;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200650
651 if (num_layers > *num_elements) {
Sean Paulac874152016-03-10 16:00:26 -0500652 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
653 return HWC2::Error::None;
654 }
655
656 layers[num_layers - 1] = l.first;
Roman Stratiienko0fade372021-02-20 13:59:55 +0200657 fences[num_layers - 1] = l.second.release_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500658 }
659 *num_elements = num_layers;
660 return HWC2::Error::None;
661}
662
Roman Stratiienko0fade372021-02-20 13:59:55 +0200663void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(UniqueFd fd) {
664 if (!fd) {
Sean Paulac874152016-03-10 16:00:26 -0500665 return;
Roman Stratiienko0fade372021-02-20 13:59:55 +0200666 }
Sean Paulac874152016-03-10 16:00:26 -0500667
Roman Stratiienko0fade372021-02-20 13:59:55 +0200668 if (present_fence_) {
669 present_fence_ = UniqueFd(
670 sync_merge("dc_present", present_fence_.Get(), fd.Get()));
Sean Paulac874152016-03-10 16:00:26 -0500671 } else {
Roman Stratiienko0fade372021-02-20 13:59:55 +0200672 present_fence_ = std::move(fd);
Sean Paulac874152016-03-10 16:00:26 -0500673 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500674}
675
Rob Herring4f6c62e2018-05-17 14:33:02 -0500676HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500677 // order the layers by z-order
678 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100679 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500680 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
681 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200682 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500683 case HWC2::Composition::Device:
684 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
685 break;
686 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100687 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500688 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100689 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500690 break;
691 default:
692 continue;
693 }
694 }
695 if (use_client_layer)
696 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
697
Rob Herring4f6c62e2018-05-17 14:33:02 -0500698 if (z_map.empty())
699 return HWC2::Error::BadLayer;
700
Matvii Zorin5368b732021-01-12 10:53:08 +0200701 std::vector<DrmHwcLayer> composition_layers;
702
Sean Paulac874152016-03-10 16:00:26 -0500703 // now that they're ordered by z, add them to the composition
704 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
705 DrmHwcLayer layer;
706 l.second->PopulateDrmLayer(&layer);
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200707 int ret = layer.ImportBuffer(drm_);
Sean Paulac874152016-03-10 16:00:26 -0500708 if (ret) {
709 ALOGE("Failed to import layer, ret=%d", ret);
710 return HWC2::Error::NoResources;
711 }
Matvii Zorin5368b732021-01-12 10:53:08 +0200712 composition_layers.emplace_back(std::move(layer));
Sean Paulac874152016-03-10 16:00:26 -0500713 }
Sean Paulac874152016-03-10 16:00:26 -0500714
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300715 auto composition = std::make_shared<DrmDisplayComposition>(crtc_,
Matvii Zorin704ea0e2021-01-08 12:55:45 +0200716 planner_.get());
Sean Paulac874152016-03-10 16:00:26 -0500717
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200718 // TODO(nobody): Don't always assume geometry changed
Matvii Zorin5368b732021-01-12 10:53:08 +0200719 int ret = composition->SetLayers(composition_layers.data(),
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300720 composition_layers.size());
Sean Paulac874152016-03-10 16:00:26 -0500721 if (ret) {
722 ALOGE("Failed to set layers in the composition ret=%d", ret);
723 return HWC2::Error::BadLayer;
724 }
725
726 std::vector<DrmPlane *> primary_planes(primary_planes_);
727 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500728 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500729 if (ret) {
John Stultz66763d52021-08-24 04:59:25 +0000730 ALOGV("Failed to plan the composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500731 return HWC2::Error::BadConfig;
732 }
733
734 // Disable the planes we're not using
735 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
736 composition->AddPlaneDisable(*i);
737 i = primary_planes.erase(i);
738 }
739 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
740 composition->AddPlaneDisable(*i);
741 i = overlay_planes.erase(i);
742 }
743
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300744 AtomicCommitArgs a_args = {
745 .test_only = test,
746 .composition = composition,
747 };
748
749 ret = compositor_.ExecuteAtomicCommit(a_args);
750
Sean Paulac874152016-03-10 16:00:26 -0500751 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700752 if (!test)
753 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500754 return HWC2::Error::BadParameter;
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300755 } else {
756 if (!test) {
757 AddFenceToPresentFence(std::move(a_args.out_fence));
758 }
Sean Paulac874152016-03-10 16:00:26 -0500759 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500760 return HWC2::Error::None;
761}
762
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300763/* Find API details at:
764 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
765 */
Matteo Franchinc56eede2019-12-03 17:10:38 +0000766HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500767 supported(__func__);
768 HWC2::Error ret;
769
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200770 ++total_stats_.total_frames_;
771
Rob Herring4f6c62e2018-05-17 14:33:02 -0500772 ret = CreateComposition(false);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200773 if (ret != HWC2::Error::None)
774 ++total_stats_.failed_kms_present_;
775
Rob Herring4f6c62e2018-05-17 14:33:02 -0500776 if (ret == HWC2::Error::BadLayer) {
777 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000778 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500779 return HWC2::Error::None;
780 }
781 if (ret != HWC2::Error::None)
782 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500783
Matteo Franchinc56eede2019-12-03 17:10:38 +0000784 *present_fence = present_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500785
786 ++frame_no_;
787 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500788}
789
790HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500791 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400792 auto mode = std::find_if(connector_->modes().begin(),
793 connector_->modes().end(),
794 [config](DrmMode const &m) {
795 return m.id() == config;
796 });
Sean Paulac874152016-03-10 16:00:26 -0500797 if (mode == connector_->modes().end()) {
798 ALOGE("Could not find active mode for %d", config);
799 return HWC2::Error::BadConfig;
800 }
801
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300802 AtomicCommitArgs a_args = {
803 .display_mode = *mode,
804 .clear_active_composition = true,
805 };
806
807 int err = compositor_.ExecuteAtomicCommit(a_args);
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300808 if (err != 0) {
809 ALOGE("Failed to queue mode changing commit %d", err);
Sean Paulac874152016-03-10 16:00:26 -0500810 return HWC2::Error::BadConfig;
811 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300812
813 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500814
815 // Setup the client layer's dimensions
816 hwc_rect_t display_frame = {.left = 0,
817 .top = 0,
818 .right = static_cast<int>(mode->h_display()),
819 .bottom = static_cast<int>(mode->v_display())};
820 client_layer_.SetLayerDisplayFrame(display_frame);
Sean Paulac874152016-03-10 16:00:26 -0500821
822 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500823}
824
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300825/* Find API details at:
826 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
827 */
Sean Pauled2ec4b2016-03-10 15:35:40 -0500828HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
829 int32_t acquire_fence,
830 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600831 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500832 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500833
834 client_layer_.set_buffer(target);
John Stultz8adf5442021-07-31 00:19:50 +0000835 client_layer_.acquire_fence_ = UniqueFd(acquire_fence);
Sean Paulac874152016-03-10 16:00:26 -0500836 client_layer_.SetLayerDataspace(dataspace);
Roman Stratiienko33365c22020-10-10 23:06:36 +0300837
838 /* TODO: Do not update source_crop every call.
839 * It makes sense to do it once after every hotplug event. */
840 hwc_drm_bo bo{};
841 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
842
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200843 hwc_frect_t source_crop = {.left = 0.0F,
844 .top = 0.0F,
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300845 .right = static_cast<float>(bo.width),
846 .bottom = static_cast<float>(bo.height)};
Roman Stratiienko33365c22020-10-10 23:06:36 +0300847 client_layer_.SetLayerSourceCrop(source_crop);
848
Sean Paulac874152016-03-10 16:00:26 -0500849 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500850}
851
852HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500853 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800854
Roman Stratiienkod146d6d2020-10-04 23:56:46 +0300855 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
856 return HWC2::Error::BadParameter;
857
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800858 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +0300859 return HWC2::Error::Unsupported;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800860
861 color_mode_ = mode;
862 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500863}
864
865HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500866 int32_t hint) {
867 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200868 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
869 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
870 return HWC2::Error::BadParameter;
871
872 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
873 return HWC2::Error::BadParameter;
874
875 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
876 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
877 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
878
879 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500880}
881
882HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500883 int32_t release_fence) {
884 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200885 // TODO(nobody): Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500886 return unsupported(__func__, buffer, release_fence);
887}
888
Sean Paulac874152016-03-10 16:00:26 -0500889HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
890 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500891 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300892 AtomicCommitArgs a_args{};
893
Sean Paulac874152016-03-10 16:00:26 -0500894 switch (mode) {
895 case HWC2::PowerMode::Off:
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300896 a_args.active = false;
Sean Paulac874152016-03-10 16:00:26 -0500897 break;
898 case HWC2::PowerMode::On:
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300899 a_args.active = true;
Sean Paulac874152016-03-10 16:00:26 -0500900 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100901 case HWC2::PowerMode::Doze:
902 case HWC2::PowerMode::DozeSuspend:
903 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500904 default:
905 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100906 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500907 };
908
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300909 int err = compositor_.ExecuteAtomicCommit(a_args);
910 if (err) {
911 ALOGE("Failed to apply the dpms composition err=%d", err);
Sean Paulac874152016-03-10 16:00:26 -0500912 return HWC2::Error::BadParameter;
913 }
914 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500915}
916
917HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500918 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300919 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500920 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500921}
922
923HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500924 uint32_t *num_requests) {
925 supported(__func__);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500926
Matvii Zorinef3c7972020-08-11 15:15:44 +0300927 return backend_->ValidateDisplay(this, num_types, num_requests);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500928}
929
Matvii Zorined90ef92021-01-29 18:32:06 +0200930std::vector<DrmHwcTwo::HwcLayer *>
931DrmHwcTwo::HwcDisplay::GetOrderLayersByZPos() {
932 std::vector<DrmHwcTwo::HwcLayer *> ordered_layers;
933 ordered_layers.reserve(layers_.size());
934
935 for (auto &[handle, layer] : layers_) {
936 ordered_layers.emplace_back(&layer);
937 }
938
939 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
940 [](const DrmHwcTwo::HwcLayer *lhs, const DrmHwcTwo::HwcLayer *rhs) {
941 return lhs->z_order() < rhs->z_order();
942 });
943
944 return ordered_layers;
945}
946
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300947#if PLATFORM_SDK_VERSION > 29
948HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
949 if (connector_->internal())
950 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
951 else if (connector_->external())
952 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
953 else
954 return HWC2::Error::BadConfig;
955
956 return HWC2::Error::None;
957}
958
959HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayVsyncPeriod(
960 hwc2_vsync_period_t *outVsyncPeriod /* ns */) {
961 supported(__func__);
962 DrmMode const &mode = connector_->active_mode();
963 if (mode.id() == 0)
964 return HWC2::Error::BadConfig;
965
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300966 *outVsyncPeriod = static_cast<hwc2_vsync_period_t>(1E9 / mode.v_refresh());
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300967 return HWC2::Error::None;
968}
969
970HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfigWithConstraints(
971 hwc2_config_t /*config*/,
972 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
973 hwc_vsync_period_change_timeline_t *outTimeline) {
974 supported(__func__);
975
976 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
977 return HWC2::Error::BadParameter;
978 }
979
980 return HWC2::Error::BadConfig;
981}
982
983HWC2::Error DrmHwcTwo::HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
984 return HWC2::Error::Unsupported;
985}
986
987HWC2::Error DrmHwcTwo::HwcDisplay::GetSupportedContentTypes(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200988 uint32_t *outNumSupportedContentTypes,
989 const uint32_t *outSupportedContentTypes) {
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300990 if (outSupportedContentTypes == nullptr)
991 *outNumSupportedContentTypes = 0;
992
993 return HWC2::Error::None;
994}
995
996HWC2::Error DrmHwcTwo::HwcDisplay::SetContentType(int32_t contentType) {
997 supported(__func__);
998
999 if (contentType != HWC2_CONTENT_TYPE_NONE)
1000 return HWC2::Error::Unsupported;
1001
1002 /* TODO: Map to the DRM Connector property:
1003 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
1004 */
1005
1006 return HWC2::Error::None;
1007}
1008#endif
1009
John Stultz8c7229d2020-02-07 21:31:08 +00001010#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001011HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
1012 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
1013 supported(__func__);
1014
Roman Stratiienko3e8ce572021-09-29 12:46:28 +03001015 auto blob = connector_->GetEdidBlob();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001016
Roman Stratiienko3e8ce572021-09-29 12:46:28 +03001017 if (!blob) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001018 ALOGE("Failed to get edid property value.");
1019 return HWC2::Error::Unsupported;
1020 }
1021
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +03001022 if (outData) {
1023 *outDataSize = std::min(*outDataSize, blob->length);
1024 memcpy(outData, blob->data, *outDataSize);
1025 } else {
1026 *outDataSize = blob->length;
1027 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001028 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001029
1030 return HWC2::Error::None;
1031}
1032
1033HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
1034 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
1035 unsupported(__func__, outCapabilities);
1036
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001037 if (outNumCapabilities == nullptr) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001038 return HWC2::Error::BadParameter;
1039 }
1040
1041 *outNumCapabilities = 0;
1042
1043 return HWC2::Error::None;
1044}
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001045
1046HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
1047 bool *supported) {
1048 *supported = false;
1049 return HWC2::Error::None;
1050}
1051
1052HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
1053 float /* brightness */) {
1054 return HWC2::Error::Unsupported;
1055}
1056
John Stultz8c7229d2020-02-07 21:31:08 +00001057#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001058
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001059#if PLATFORM_SDK_VERSION > 27
1060
1061HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
1062 int32_t mode, uint32_t *outNumIntents,
1063 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1064 if (mode != HAL_COLOR_MODE_NATIVE) {
1065 return HWC2::Error::BadParameter;
1066 }
1067
1068 if (outIntents == nullptr) {
1069 *outNumIntents = 1;
1070 return HWC2::Error::None;
1071 }
1072 *outNumIntents = 1;
1073 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1074 return HWC2::Error::None;
1075}
1076
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001077HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
1078 int32_t intent) {
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001079 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1080 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1081 return HWC2::Error::BadParameter;
1082
1083 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
1084 return HWC2::Error::BadParameter;
1085
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001086 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001087 return HWC2::Error::Unsupported;
1088
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001089 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001090 return HWC2::Error::Unsupported;
1091
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001092 color_mode_ = mode;
1093 return HWC2::Error::None;
1094}
1095
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001096#endif /* PLATFORM_SDK_VERSION > 27 */
1097
Roman Stratiienkoaaaa4692021-09-29 12:50:10 +03001098HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t /*x*/,
1099 int32_t /*y*/) {
Sean Paulac874152016-03-10 16:00:26 -05001100 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001101 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001102}
1103
1104HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001105 supported(__func__);
Roman Stratiienko5063d532021-09-29 12:47:31 +03001106 switch (static_cast<HWC2::BlendMode>(mode)) {
1107 case HWC2::BlendMode::None:
1108 blending_ = DrmHwcBlending::kNone;
1109 break;
1110 case HWC2::BlendMode::Premultiplied:
1111 blending_ = DrmHwcBlending::kPreMult;
1112 break;
1113 case HWC2::BlendMode::Coverage:
1114 blending_ = DrmHwcBlending::kCoverage;
1115 break;
1116 default:
1117 ALOGE("Unknown blending mode b=%d", blending_);
1118 blending_ = DrmHwcBlending::kNone;
1119 break;
1120 }
Sean Paulac874152016-03-10 16:00:26 -05001121 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001122}
1123
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +03001124/* Find API details at:
1125 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=2314
1126 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001127HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001128 int32_t acquire_fence) {
1129 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -05001130
Sean Paulac874152016-03-10 16:00:26 -05001131 set_buffer(buffer);
John Stultz8adf5442021-07-31 00:19:50 +00001132 acquire_fence_ = UniqueFd(acquire_fence);
Sean Paulac874152016-03-10 16:00:26 -05001133 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001134}
1135
Roman Stratiienkoaaaa4692021-09-29 12:50:10 +03001136HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t /*color*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001137 // TODO(nobody): Put to client composition here?
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001138 supported(__func__);
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001139 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001140}
1141
1142HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001143 sf_type_ = static_cast<HWC2::Composition>(type);
1144 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001145}
1146
1147HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001148 supported(__func__);
Roman Stratiienko515da8f2021-09-29 12:47:21 +03001149 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
1150 case HAL_DATASPACE_STANDARD_BT709:
1151 color_space_ = DrmHwcColorSpace::kItuRec709;
1152 break;
1153 case HAL_DATASPACE_STANDARD_BT601_625:
1154 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
1155 case HAL_DATASPACE_STANDARD_BT601_525:
1156 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
1157 color_space_ = DrmHwcColorSpace::kItuRec601;
1158 break;
1159 case HAL_DATASPACE_STANDARD_BT2020:
1160 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
1161 color_space_ = DrmHwcColorSpace::kItuRec2020;
1162 break;
1163 default:
1164 color_space_ = DrmHwcColorSpace::kUndefined;
1165 }
1166
1167 switch (dataspace & HAL_DATASPACE_RANGE_MASK) {
1168 case HAL_DATASPACE_RANGE_FULL:
1169 sample_range_ = DrmHwcSampleRange::kFullRange;
1170 break;
1171 case HAL_DATASPACE_RANGE_LIMITED:
1172 sample_range_ = DrmHwcSampleRange::kLimitedRange;
1173 break;
1174 default:
1175 sample_range_ = DrmHwcSampleRange::kUndefined;
1176 }
Sean Paulac874152016-03-10 16:00:26 -05001177 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001178}
1179
1180HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001181 supported(__func__);
1182 display_frame_ = frame;
1183 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001184}
1185
1186HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001187 supported(__func__);
1188 alpha_ = alpha;
1189 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001190}
1191
1192HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1193 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001194 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001195 // TODO(nobody): We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001196 return unsupported(__func__, stream);
1197}
1198
1199HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001200 supported(__func__);
1201 source_crop_ = crop;
1202 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001203}
1204
1205HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001206 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001207 // TODO(nobody): We don't use surface damage, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001208 unsupported(__func__, damage);
1209 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001210}
1211
1212HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001213 supported(__func__);
Roman Stratiienko2ed4cbe2021-09-29 12:47:35 +03001214
1215 uint32_t l_transform = 0;
1216
1217 // 270* and 180* cannot be combined with flips. More specifically, they
1218 // already contain both horizontal and vertical flips, so those fields are
1219 // redundant in this case. 90* rotation can be combined with either horizontal
1220 // flip or vertical flip, so treat it differently
1221 if (transform == HWC_TRANSFORM_ROT_270) {
1222 l_transform = DrmHwcTransform::kRotate270;
1223 } else if (transform == HWC_TRANSFORM_ROT_180) {
1224 l_transform = DrmHwcTransform::kRotate180;
1225 } else {
1226 if (transform & HWC_TRANSFORM_FLIP_H)
1227 l_transform |= DrmHwcTransform::kFlipH;
1228 if (transform & HWC_TRANSFORM_FLIP_V)
1229 l_transform |= DrmHwcTransform::kFlipV;
1230 if (transform & HWC_TRANSFORM_ROT_90)
1231 l_transform |= DrmHwcTransform::kRotate90;
1232 }
1233
1234 transform_ = static_cast<DrmHwcTransform>(l_transform);
Sean Paulac874152016-03-10 16:00:26 -05001235 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001236}
1237
1238HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001239 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001240 // TODO(nobody): We don't use this information, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001241 unsupported(__func__, visible);
1242 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001243}
1244
Sean Paulac874152016-03-10 16:00:26 -05001245HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1246 supported(__func__);
1247 z_order_ = order;
1248 return HWC2::Error::None;
1249}
1250
1251void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1252 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -05001253 layer->sf_handle = buffer_;
Roman Stratiienko0fade372021-02-20 13:59:55 +02001254 // TODO(rsglobal): Avoid extra fd duplication
1255 layer->acquire_fence = UniqueFd(fcntl(acquire_fence_.Get(), F_DUPFD_CLOEXEC));
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001256 layer->display_frame = display_frame_;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001257 layer->alpha = lround(65535.0F * alpha_);
Roman Stratiienko5063d532021-09-29 12:47:31 +03001258 layer->blending = blending_;
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001259 layer->source_crop = source_crop_;
Roman Stratiienko2ed4cbe2021-09-29 12:47:35 +03001260 layer->transform = transform_;
Roman Stratiienko515da8f2021-09-29 12:47:21 +03001261 layer->color_space = color_space_;
1262 layer->sample_range = sample_range_;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001263}
1264
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001265void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +03001266 const std::lock_guard<std::mutex> lock(callback_lock_);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001267
Roman Stratiienko863a3c22021-09-29 13:00:29 +03001268 if (hotplug_callback_.first != nullptr &&
1269 hotplug_callback_.second != nullptr) {
1270 hotplug_callback_.first(hotplug_callback_.second, displayid,
1271 state == DRM_MODE_CONNECTED
1272 ? HWC2_CONNECTION_CONNECTED
1273 : HWC2_CONNECTION_DISCONNECTED);
1274 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001275}
1276
1277void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
Roman Stratiienkod21071f2021-03-09 21:56:50 +02001278 for (const auto &conn : drmDevice->connectors()) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001279 if (conn->state() != DRM_MODE_CONNECTED)
1280 continue;
1281 HandleDisplayHotplug(conn->display(), conn->state());
1282 }
1283}
1284
1285void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
Roman Stratiienkod21071f2021-03-09 21:56:50 +02001286 for (const auto &conn : drm_->connectors()) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001287 drmModeConnection old_state = conn->state();
1288 drmModeConnection cur_state = conn->UpdateModes()
1289 ? DRM_MODE_UNKNOWNCONNECTION
1290 : conn->state();
1291
1292 if (cur_state == old_state)
1293 continue;
1294
1295 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1296 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1297 conn->id(), conn->display());
1298
1299 int display_id = conn->display();
1300 if (cur_state == DRM_MODE_CONNECTED) {
1301 auto &display = hwc2_->displays_.at(display_id);
1302 display.ChosePreferredConfig();
1303 } else {
1304 auto &display = hwc2_->displays_.at(display_id);
1305 display.ClearDisplay();
1306 }
1307
1308 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1309 }
1310}
1311
Sean Pauled2ec4b2016-03-10 15:35:40 -05001312// static
1313int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1314 unsupported(__func__);
1315 return 0;
1316}
1317
1318// static
1319void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001320 uint32_t *out_count,
1321 int32_t * /*out_capabilities*/) {
1322 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001323 *out_count = 0;
1324}
1325
1326// static
Sean Paulac874152016-03-10 16:00:26 -05001327hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1328 struct hwc2_device * /*dev*/, int32_t descriptor) {
1329 supported(__func__);
1330 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001331 switch (func) {
1332 // Device functions
1333 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1334 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1335 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1336 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001337 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001338 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1339 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1340 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1341 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1342 case HWC2::FunctionDescriptor::Dump:
1343 return ToHook<HWC2_PFN_DUMP>(
1344 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1345 uint32_t *, char *>);
1346 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1347 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1348 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1349 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1350 case HWC2::FunctionDescriptor::RegisterCallback:
1351 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1352 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1353 &DrmHwcTwo::RegisterCallback, int32_t,
1354 hwc2_callback_data_t, hwc2_function_pointer_t>);
1355
1356 // Display functions
1357 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1358 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1359 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1360 &HwcDisplay::AcceptDisplayChanges>);
1361 case HWC2::FunctionDescriptor::CreateLayer:
1362 return ToHook<HWC2_PFN_CREATE_LAYER>(
1363 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1364 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1365 case HWC2::FunctionDescriptor::DestroyLayer:
1366 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1367 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1368 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1369 case HWC2::FunctionDescriptor::GetActiveConfig:
1370 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1371 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1372 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1373 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1374 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1375 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1376 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1377 hwc2_layer_t *, int32_t *>);
1378 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1379 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1380 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1381 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1382 int32_t, int32_t>);
1383 case HWC2::FunctionDescriptor::GetColorModes:
1384 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1385 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1386 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1387 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001388 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1389 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1390 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1391 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001392 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001393 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1394 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1395 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1396 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001397 case HWC2::FunctionDescriptor::GetDisplayName:
1398 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1399 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1400 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1401 case HWC2::FunctionDescriptor::GetDisplayRequests:
1402 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1403 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1404 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1405 hwc2_layer_t *, int32_t *>);
1406 case HWC2::FunctionDescriptor::GetDisplayType:
1407 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1408 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1409 &HwcDisplay::GetDisplayType, int32_t *>);
1410 case HWC2::FunctionDescriptor::GetDozeSupport:
1411 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1412 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1413 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001414 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1415 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1416 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1417 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1418 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001419 case HWC2::FunctionDescriptor::GetReleaseFences:
1420 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1421 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1422 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1423 int32_t *>);
1424 case HWC2::FunctionDescriptor::PresentDisplay:
1425 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1426 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1427 &HwcDisplay::PresentDisplay, int32_t *>);
1428 case HWC2::FunctionDescriptor::SetActiveConfig:
1429 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1430 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1431 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1432 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001433 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1434 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1435 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1436 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001437 case HWC2::FunctionDescriptor::SetColorMode:
1438 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1439 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1440 &HwcDisplay::SetColorMode, int32_t>);
1441 case HWC2::FunctionDescriptor::SetColorTransform:
1442 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1443 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1444 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1445 case HWC2::FunctionDescriptor::SetOutputBuffer:
1446 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1447 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1448 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1449 case HWC2::FunctionDescriptor::SetPowerMode:
1450 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1451 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1452 &HwcDisplay::SetPowerMode, int32_t>);
1453 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1454 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1455 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1456 &HwcDisplay::SetVsyncEnabled, int32_t>);
1457 case HWC2::FunctionDescriptor::ValidateDisplay:
1458 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1459 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1460 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001461#if PLATFORM_SDK_VERSION > 27
1462 case HWC2::FunctionDescriptor::GetRenderIntents:
1463 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1464 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1465 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1466 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001467 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1468 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1469 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1470 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001471#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001472#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001473 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1474 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1475 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1476 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1477 uint32_t *, uint8_t *>);
1478 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1479 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1480 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1481 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1482 uint32_t *>);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001483 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1484 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1485 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1486 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1487 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1488 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1489 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1490 &HwcDisplay::SetDisplayBrightness, float>);
John Stultz8c7229d2020-02-07 21:31:08 +00001491#endif /* PLATFORM_SDK_VERSION > 28 */
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001492#if PLATFORM_SDK_VERSION > 29
1493 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
1494 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
1495 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
1496 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
1497 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
1498 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
1499 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
1500 &HwcDisplay::GetDisplayVsyncPeriod,
1501 hwc2_vsync_period_t *>);
1502 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
1503 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
1504 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
1505 &HwcDisplay::SetActiveConfigWithConstraints,
1506 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
1507 hwc_vsync_period_change_timeline_t *>);
1508 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
1509 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
1510 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
1511 &HwcDisplay::SetAutoLowLatencyMode, bool>);
1512 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
1513 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
1514 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
1515 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
1516 uint32_t *>);
1517 case HWC2::FunctionDescriptor::SetContentType:
1518 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
1519 DisplayHook<decltype(&HwcDisplay::SetContentType),
1520 &HwcDisplay::SetContentType, int32_t>);
1521#endif
Sean Pauled2ec4b2016-03-10 15:35:40 -05001522 // Layer functions
1523 case HWC2::FunctionDescriptor::SetCursorPosition:
1524 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1525 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1526 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1527 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1528 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1529 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1530 &HwcLayer::SetLayerBlendMode, int32_t>);
1531 case HWC2::FunctionDescriptor::SetLayerBuffer:
1532 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1533 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1534 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1535 case HWC2::FunctionDescriptor::SetLayerColor:
1536 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1537 LayerHook<decltype(&HwcLayer::SetLayerColor),
1538 &HwcLayer::SetLayerColor, hwc_color_t>);
1539 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1540 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1541 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1542 &HwcLayer::SetLayerCompositionType, int32_t>);
1543 case HWC2::FunctionDescriptor::SetLayerDataspace:
1544 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1545 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1546 &HwcLayer::SetLayerDataspace, int32_t>);
1547 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1548 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1549 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1550 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1551 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1552 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1553 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1554 &HwcLayer::SetLayerPlaneAlpha, float>);
1555 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001556 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1557 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1558 &HwcLayer::SetLayerSidebandStream,
1559 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001560 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1561 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1562 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1563 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1564 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1565 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1566 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1567 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1568 case HWC2::FunctionDescriptor::SetLayerTransform:
1569 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1570 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1571 &HwcLayer::SetLayerTransform, int32_t>);
1572 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1573 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1574 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1575 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1576 case HWC2::FunctionDescriptor::SetLayerZOrder:
1577 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1578 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1579 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001580 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001581 default:
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001582 return nullptr;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001583 }
1584}
Sean Paulac874152016-03-10 16:00:26 -05001585
1586// static
1587int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1588 struct hw_device_t **dev) {
1589 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001590 if (strcmp(name, HWC_HARDWARE_COMPOSER) != 0) {
Sean Paulac874152016-03-10 16:00:26 -05001591 ALOGE("Invalid module name- %s", name);
1592 return -EINVAL;
1593 }
1594
1595 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1596 if (!ctx) {
1597 ALOGE("Failed to allocate DrmHwcTwo");
1598 return -ENOMEM;
1599 }
1600
1601 HWC2::Error err = ctx->Init();
1602 if (err != HWC2::Error::None) {
1603 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1604 return -EINVAL;
1605 }
1606
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001607 ctx->common.module = (hw_module_t *)module;
Sean Paulac874152016-03-10 16:00:26 -05001608 *dev = &ctx->common;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001609 ctx.release(); // NOLINT(bugprone-unused-return-value)
Sean Paulac874152016-03-10 16:00:26 -05001610 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001611}
Sean Paulf72cccd2018-08-27 13:59:08 -04001612} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001613
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001614// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001615static struct hw_module_methods_t hwc2_module_methods = {
1616 .open = android::DrmHwcTwo::HookDevOpen,
1617};
1618
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001619// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001620hw_module_t HAL_MODULE_INFO_SYM = {
1621 .tag = HARDWARE_MODULE_TAG,
1622 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1623 .id = HWC_HARDWARE_MODULE_ID,
1624 .name = "DrmHwcTwo module",
1625 .author = "The Android Open Source Project",
1626 .methods = &hwc2_module_methods,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001627 .dso = nullptr,
Sean Paulac874152016-03-10 16:00:26 -05001628 .reserved = {0},
1629};