blob: b57d2f311651dde13fa4535e9f8ea67ec9d64996 [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
Sean Pauled2ec4b2016-03-10 15:35:40 -050096template <typename... Args>
97static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
98 ALOGV("Unsupported function: %s", func);
99 return HWC2::Error::Unsupported;
100}
101
Sean Paulac874152016-03-10 16:00:26 -0500102static inline void supported(char const *func) {
103 ALOGV("Supported function: %s", func);
104}
105
Sean Pauled2ec4b2016-03-10 15:35:40 -0500106HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t width, uint32_t height,
107 int32_t *format,
108 hwc2_display_t *display) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200109 // TODO(nobody): Implement virtual display
Sean Paulac874152016-03-10 16:00:26 -0500110 return unsupported(__func__, width, height, format, display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500111}
112
113HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t display) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200114 // TODO(nobody): Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500115 return unsupported(__func__, display);
116}
117
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200118std::string DrmHwcTwo::HwcDisplay::DumpDelta(
119 DrmHwcTwo::HwcDisplay::Stats delta) {
120 if (delta.total_pixops_ == 0)
121 return "No stats yet";
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200122 double ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200123
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200124 std::stringstream ss;
125 ss << " Total frames count: " << delta.total_frames_ << "\n"
126 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
127 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
128 << ((delta.failed_kms_present_ > 0)
129 ? " !!! Internal failure, FIX it please\n"
130 : "")
131 << " Flattened frames: " << delta.frames_flattened_ << "\n"
132 << " Pixel operations (free units)"
133 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
134 << "]\n"
135 << " Composition efficiency: " << ratio;
136
137 return ss.str();
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200138}
139
140std::string DrmHwcTwo::HwcDisplay::Dump() {
Roman Stratiienkoe8c06792021-09-30 10:09:31 +0300141 std::string flattening_state_str;
142 switch (flattenning_state_) {
143 case ClientFlattenningState::Disabled:
144 flattening_state_str = "Disabled";
145 break;
146 case ClientFlattenningState::NotRequired:
147 flattening_state_str = "Not needed";
148 break;
149 case ClientFlattenningState::Flattened:
150 flattening_state_str = "Active";
151 break;
152 case ClientFlattenningState::ClientRefreshRequested:
153 flattening_state_str = "Refresh requested";
154 break;
155 default:
156 flattening_state_str = std::to_string(flattenning_state_) +
157 " VSync remains";
158 }
159
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200160 std::stringstream ss;
161 ss << "- Display on: " << connector_->name() << "\n"
Roman Stratiienkoe8c06792021-09-30 10:09:31 +0300162 << " Flattening state: " << flattening_state_str << "\n"
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200163 << "Statistics since system boot:\n"
164 << DumpDelta(total_stats_) << "\n\n"
165 << "Statistics since last dumpsys request:\n"
166 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200167
168 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200169 return ss.str();
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200170}
171
172void DrmHwcTwo::Dump(uint32_t *outSize, char *outBuffer) {
173 supported(__func__);
174
175 if (outBuffer != nullptr) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200176 auto copied_bytes = mDumpString.copy(outBuffer, *outSize);
177 *outSize = static_cast<uint32_t>(copied_bytes);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200178 return;
179 }
180
181 std::stringstream output;
182
183 output << "-- drm_hwcomposer --\n\n";
184
185 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &dp : displays_)
186 output << dp.second.Dump();
187
188 mDumpString = output.str();
189 *outSize = static_cast<uint32_t>(mDumpString.size());
Sean Pauled2ec4b2016-03-10 15:35:40 -0500190}
191
192uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200193 // TODO(nobody): Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500194 unsupported(__func__);
195 return 0;
196}
197
198HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500199 hwc2_callback_data_t data,
200 hwc2_function_pointer_t function) {
201 supported(__func__);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300202
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300203 std::unique_lock<std::mutex> lock(callback_lock_);
204
Roman Stratiienko23701092020-09-26 02:08:41 +0300205 switch (static_cast<HWC2::Callback>(descriptor)) {
Sean Paulac874152016-03-10 16:00:26 -0500206 case HWC2::Callback::Hotplug: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300207 hotplug_callback_ = std::make_pair(HWC2_PFN_HOTPLUG(function), data);
208 lock.unlock();
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200209 const auto &drm_devices = resource_manager_.getDrmDevices();
210 for (const auto &device : drm_devices)
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300211 HandleInitialHotplugState(device.get());
Sean Paulac874152016-03-10 16:00:26 -0500212 break;
213 }
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200214 case HWC2::Callback::Refresh: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300215 refresh_callback_ = std::make_pair(HWC2_PFN_REFRESH(function), data);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200216 break;
217 }
Sean Paulac874152016-03-10 16:00:26 -0500218 case HWC2::Callback::Vsync: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300219 vsync_callback_ = std::make_pair(HWC2_PFN_VSYNC(function), data);
Sean Paulac874152016-03-10 16:00:26 -0500220 break;
221 }
Roman Stratiienko11ef8c52021-09-29 13:01:39 +0300222#if PLATFORM_SDK_VERSION > 29
223 case HWC2::Callback::Vsync_2_4: {
224 vsync_2_4_callback_ = std::make_pair(HWC2_PFN_VSYNC_2_4(function), data);
225 break;
226 }
227#endif
Sean Paulac874152016-03-10 16:00:26 -0500228 default:
229 break;
230 }
231 return HWC2::Error::None;
232}
233
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100234DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200235 DrmDevice *drm, hwc2_display_t handle,
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300236 HWC2::DisplayType type, DrmHwcTwo *hwc2)
237 : hwc2_(hwc2),
238 resource_manager_(resource_manager),
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100239 drm_(drm),
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100240 handle_(handle),
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200241 type_(type),
242 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
Sean Paulac874152016-03-10 16:00:26 -0500243 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200244
245 // clang-format off
246 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
247 0.0, 1.0, 0.0, 0.0,
248 0.0, 0.0, 1.0, 0.0,
249 0.0, 0.0, 0.0, 1.0};
250 // clang-format on
Sean Paulac874152016-03-10 16:00:26 -0500251}
252
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300253void DrmHwcTwo::HwcDisplay::ClearDisplay() {
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300254 AtomicCommitArgs a_args = {.clear_active_composition = true};
255 compositor_.ExecuteAtomicCommit(a_args);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300256}
257
Sean Paulac874152016-03-10 16:00:26 -0500258HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
259 supported(__func__);
260 planner_ = Planner::CreateInstance(drm_);
261 if (!planner_) {
262 ALOGE("Failed to create planner instance for composition");
263 return HWC2::Error::NoResources;
264 }
265
266 int display = static_cast<int>(handle_);
Roman Stratiienkoe8c06792021-09-30 10:09:31 +0300267 int ret = compositor_.Init(resource_manager_, display);
Sean Paulac874152016-03-10 16:00:26 -0500268 if (ret) {
269 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
270 return HWC2::Error::NoResources;
271 }
272
273 // Split up the given display planes into primary and overlay to properly
274 // interface with the composition
275 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -0700276 property_get("vendor.hwc.drm.use_overlay_planes", use_overlay_planes_prop,
277 "1");
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200278 bool use_overlay_planes = strtol(use_overlay_planes_prop, nullptr, 10);
Sean Paulac874152016-03-10 16:00:26 -0500279 for (auto &plane : *planes) {
280 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
281 primary_planes_.push_back(plane);
282 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
283 overlay_planes_.push_back(plane);
284 }
285
286 crtc_ = drm_->GetCrtcForDisplay(display);
287 if (!crtc_) {
288 ALOGE("Failed to get crtc for display %d", display);
289 return HWC2::Error::BadDisplay;
290 }
291
292 connector_ = drm_->GetConnectorForDisplay(display);
293 if (!connector_) {
294 ALOGE("Failed to get connector for display %d", display);
295 return HWC2::Error::BadDisplay;
296 }
297
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300298 ret = vsync_worker_.Init(drm_, display, [this](int64_t timestamp) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300299 const std::lock_guard<std::mutex> lock(hwc2_->callback_lock_);
Roman Stratiienko11ef8c52021-09-29 13:01:39 +0300300 /* vsync callback */
301#if PLATFORM_SDK_VERSION > 29
302 if (hwc2_->vsync_2_4_callback_.first != nullptr &&
303 hwc2_->vsync_2_4_callback_.second != nullptr) {
304 hwc2_vsync_period_t period_ns{};
305 GetDisplayVsyncPeriod(&period_ns);
306 hwc2_->vsync_2_4_callback_.first(hwc2_->vsync_2_4_callback_.second,
307 handle_, timestamp, period_ns);
308 } else
309#endif
310 if (hwc2_->vsync_callback_.first != nullptr &&
311 hwc2_->vsync_callback_.second != nullptr) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300312 hwc2_->vsync_callback_.first(hwc2_->vsync_callback_.second, handle_,
313 timestamp);
314 }
315 });
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300316 if (ret) {
317 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
318 return HWC2::Error::BadDisplay;
319 }
320
Roman Stratiienkoe8c06792021-09-30 10:09:31 +0300321 ret = flattening_vsync_worker_.Init(drm_, display, [this](int64_t /*timestamp*/) {
322 const std::lock_guard<std::mutex> lock(hwc2_->callback_lock_);
323 /* Frontend flattening */
324 if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
325 --flattenning_state_ ==
326 ClientFlattenningState::ClientRefreshRequested &&
327 hwc2_->refresh_callback_.first != nullptr &&
328 hwc2_->refresh_callback_.second != nullptr) {
329 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
330 flattening_vsync_worker_.VSyncControl(false);
331 }
332 });
333 if (ret) {
334 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
335 return HWC2::Error::BadDisplay;
336 }
337
Matvii Zorinef3c7972020-08-11 15:15:44 +0300338 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
339 if (ret) {
340 ALOGE("Failed to set backend for d=%d %d\n", display, ret);
341 return HWC2::Error::BadDisplay;
342 }
343
Roman Stratiienko720f6522021-12-06 14:56:14 +0200344 client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);
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)
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200354 return HWC2::Error::BadDisplay;
Sean Paulac874152016-03-10 16:00:26 -0500355
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200356 return SetActiveConfig(preferred_config_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
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200383HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(
384 hwc2_config_t *config) const {
Sean Paulac874152016-03-10 16:00:26 -0500385 supported(__func__);
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200386 if (hwc_configs_.count(active_config_id_) == 0)
Sean Paulac874152016-03-10 16:00:26 -0500387 return HWC2::Error::BadConfig;
388
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200389 *config = active_config_id_;
Sean Paulac874152016-03-10 16:00:26 -0500390 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__);
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200448 int conf = static_cast<int>(config);
449
450 if (hwc_configs_.count(conf) == 0) {
451 ALOGE("Could not find active mode for %d", conf);
Sean Paulac874152016-03-10 16:00:26 -0500452 return HWC2::Error::BadConfig;
453 }
454
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200455 auto &hwc_config = hwc_configs_[conf];
456
Sean Paulac874152016-03-10 16:00:26 -0500457 static const int32_t kUmPerInch = 25400;
458 uint32_t mm_width = connector_->mm_width();
459 uint32_t mm_height = connector_->mm_height();
460 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
461 switch (attribute) {
462 case HWC2::Attribute::Width:
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200463 *value = static_cast<int>(hwc_config.mode.h_display());
Sean Paulac874152016-03-10 16:00:26 -0500464 break;
465 case HWC2::Attribute::Height:
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200466 *value = static_cast<int>(hwc_config.mode.v_display());
Sean Paulac874152016-03-10 16:00:26 -0500467 break;
468 case HWC2::Attribute::VsyncPeriod:
469 // in nanoseconds
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200470 *value = static_cast<int>(1E9 / hwc_config.mode.v_refresh());
Sean Paulac874152016-03-10 16:00:26 -0500471 break;
472 case HWC2::Attribute::DpiX:
473 // Dots per 1000 inches
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200474 *value = mm_width ? static_cast<int>(hwc_config.mode.h_display() *
475 kUmPerInch / mm_width)
476 : -1;
Sean Paulac874152016-03-10 16:00:26 -0500477 break;
478 case HWC2::Attribute::DpiY:
479 // Dots per 1000 inches
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200480 *value = mm_height ? static_cast<int>(hwc_config.mode.v_display() *
481 kUmPerInch / mm_height)
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300482 : -1;
Sean Paulac874152016-03-10 16:00:26 -0500483 break;
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300484#if PLATFORM_SDK_VERSION > 29
485 case HWC2::Attribute::ConfigGroup:
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200486 /* Dispite ConfigGroup is a part of HWC2.4 API, framework
487 * able to request it even if service @2.1 is used */
488 *value = hwc_config.group_id;
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300489 break;
490#endif
Sean Paulac874152016-03-10 16:00:26 -0500491 default:
492 *value = -1;
493 return HWC2::Error::BadConfig;
494 }
495 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500496}
497
Roman Stratiienko5f2f3ce2021-12-22 11:46:03 +0200498// NOLINTNEXTLINE (readability-function-cognitive-complexity): Fixme
Sean Pauled2ec4b2016-03-10 15:35:40 -0500499HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
500 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500501 supported(__func__);
502 // Since this callback is normally invoked twice (once to get the count, and
503 // once to populate configs), we don't really want to read the edid
504 // redundantly. Instead, only update the modes on the first invocation. While
505 // it's possible this will result in stale modes, it'll all come out in the
506 // wash when we try to set the active config later.
507 if (!configs) {
508 int ret = connector_->UpdateModes();
509 if (ret) {
510 ALOGE("Failed to update display modes %d", ret);
511 return HWC2::Error::BadDisplay;
512 }
Sean Paulac874152016-03-10 16:00:26 -0500513
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200514 hwc_configs_.clear();
515 preferred_config_id_ = 0;
516 int preferred_config_group_id_ = 0;
Neil Armstrongb67d0492019-06-20 09:00:21 +0000517
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200518 if (connector_->modes().empty()) {
519 ALOGE("No modes reported by KMS");
520 return HWC2::Error::BadDisplay;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000521 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000522
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200523 int last_config_id = 1;
524 int last_group_id = 1;
Neil Armstrongb67d0492019-06-20 09:00:21 +0000525
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200526 /* Group modes */
527 for (const auto &mode : connector_->modes()) {
528 /* Find group for the new mode or create new group */
529 int group_found = 0;
530 for (auto &hwc_config : hwc_configs_) {
531 if (mode.h_display() == hwc_config.second.mode.h_display() &&
532 mode.v_display() == hwc_config.second.mode.v_display()) {
533 group_found = hwc_config.second.group_id;
534 }
535 }
536 if (group_found == 0) {
537 group_found = last_group_id++;
538 }
539
540 bool disabled = false;
541 if (mode.flags() & DRM_MODE_FLAG_3D_MASK) {
542 ALOGI("Disabling display mode %s (Modes with 3D flag aren't supported)",
543 mode.name().c_str());
544 disabled = true;
545 }
546
547 /* Add config */
548 hwc_configs_[last_config_id] = {
549 .id = last_config_id,
550 .group_id = group_found,
551 .mode = mode,
552 .disabled = disabled,
553 };
554
555 /* Chwck if the mode is preferred */
556 if ((mode.type() & DRM_MODE_TYPE_PREFERRED) != 0 &&
557 preferred_config_id_ == 0) {
558 preferred_config_id_ = last_config_id;
559 preferred_config_group_id_ = group_found;
560 }
561
562 last_config_id++;
563 }
564
565 /* We must have preferred mode. Set first mode as preferred
566 * in case KMS haven't reported anything. */
567 if (preferred_config_id_ == 0) {
568 preferred_config_id_ = 1;
569 preferred_config_group_id_ = 1;
570 }
571
572 for (int group = 1; group < last_group_id; group++) {
573 bool has_interlaced = false;
574 bool has_progressive = false;
575 for (auto &hwc_config : hwc_configs_) {
576 if (hwc_config.second.group_id != group || hwc_config.second.disabled) {
577 continue;
578 }
579
580 if (hwc_config.second.IsInterlaced()) {
581 has_interlaced = true;
582 } else {
583 has_progressive = true;
584 }
585 }
586
587 bool has_both = has_interlaced && has_progressive;
588 if (!has_both) {
589 continue;
590 }
591
592 bool group_contains_preferred_interlaced = false;
593 if (group == preferred_config_group_id_ &&
594 hwc_configs_[preferred_config_id_].IsInterlaced()) {
595 group_contains_preferred_interlaced = true;
596 }
597
598 for (auto &hwc_config : hwc_configs_) {
599 if (hwc_config.second.group_id != group || hwc_config.second.disabled) {
600 continue;
601 }
602
603 bool disable = group_contains_preferred_interlaced
604 ? !hwc_config.second.IsInterlaced()
605 : hwc_config.second.IsInterlaced();
606
607 if (disable) {
608 ALOGI(
609 "Group %i: Disabling display mode %s (This group should consist "
610 "of %s modes)",
611 group, hwc_config.second.mode.name().c_str(),
612 group_contains_preferred_interlaced ? "interlaced"
613 : "progressive");
614
615 hwc_config.second.disabled = true;
616 }
617 }
618 }
619
620 /* Group should not contain 2 modes with FPS delta less than ~1HZ
621 * otherwise android.graphics.cts.SetFrameRateTest CTS will fail
622 */
623 for (int m1 = 1; m1 < last_config_id; m1++) {
624 for (int m2 = 1; m2 < last_config_id; m2++) {
625 if (m1 != m2 &&
626 hwc_configs_[m1].group_id == hwc_configs_[m2].group_id &&
627 !hwc_configs_[m1].disabled && !hwc_configs_[m2].disabled &&
628 fabsf(hwc_configs_[m1].mode.v_refresh() -
629 hwc_configs_[m2].mode.v_refresh()) < 1.0) {
630 ALOGI(
631 "Group %i: Disabling display mode %s (Refresh rate value is "
632 "too close to existing mode %s)",
633 hwc_configs_[m2].group_id, hwc_configs_[m2].mode.name().c_str(),
634 hwc_configs_[m1].mode.name().c_str());
635
636 hwc_configs_[m2].disabled = true;
637 }
638 }
639 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000640 }
641
642 uint32_t idx = 0;
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200643 for (auto &hwc_config : hwc_configs_) {
644 if (hwc_config.second.disabled) {
645 continue;
646 }
647
648 if (configs != nullptr) {
649 if (idx >= *num_configs) {
650 break;
651 }
652 configs[idx] = hwc_config.second.id;
653 }
654
655 idx++;
Sean Paulac874152016-03-10 16:00:26 -0500656 }
657 *num_configs = idx;
658 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500659}
660
661HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500662 supported(__func__);
663 std::ostringstream stream;
664 stream << "display-" << connector_->id();
665 std::string string = stream.str();
666 size_t length = string.length();
667 if (!name) {
668 *size = length;
669 return HWC2::Error::None;
670 }
671
672 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
673 strncpy(name, string.c_str(), *size);
674 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500675}
676
Sean Paulac874152016-03-10 16:00:26 -0500677HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
678 uint32_t *num_elements,
679 hwc2_layer_t *layers,
680 int32_t *layer_requests) {
681 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200682 // TODO(nobody): I think virtual display should request
Sean Paulac874152016-03-10 16:00:26 -0500683 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
684 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
685 *num_elements = 0;
686 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500687}
688
689HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500690 supported(__func__);
691 *type = static_cast<int32_t>(type_);
692 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500693}
694
695HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500696 supported(__func__);
697 *support = 0;
698 return HWC2::Error::None;
699}
700
701HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400702 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
703 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500704 supported(__func__);
705 *num_types = 0;
706 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500707}
708
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300709/* Find API details at:
710 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
711 */
Sean Pauled2ec4b2016-03-10 15:35:40 -0500712HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500713 hwc2_layer_t *layers,
714 int32_t *fences) {
715 supported(__func__);
716 uint32_t num_layers = 0;
717
718 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
719 ++num_layers;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200720 if (layers == nullptr || fences == nullptr)
Sean Paulac874152016-03-10 16:00:26 -0500721 continue;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200722
723 if (num_layers > *num_elements) {
Sean Paulac874152016-03-10 16:00:26 -0500724 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
725 return HWC2::Error::None;
726 }
727
728 layers[num_layers - 1] = l.first;
Roman Stratiienko0fade372021-02-20 13:59:55 +0200729 fences[num_layers - 1] = l.second.release_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500730 }
731 *num_elements = num_layers;
732 return HWC2::Error::None;
733}
734
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300735HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Sean Paulac874152016-03-10 16:00:26 -0500736 // order the layers by z-order
737 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100738 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500739 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
740 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200741 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500742 case HWC2::Composition::Device:
743 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
744 break;
745 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100746 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500747 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100748 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500749 break;
750 default:
751 continue;
752 }
753 }
754 if (use_client_layer)
755 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
756
Rob Herring4f6c62e2018-05-17 14:33:02 -0500757 if (z_map.empty())
758 return HWC2::Error::BadLayer;
759
Matvii Zorin5368b732021-01-12 10:53:08 +0200760 std::vector<DrmHwcLayer> composition_layers;
761
Sean Paulac874152016-03-10 16:00:26 -0500762 // now that they're ordered by z, add them to the composition
763 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
764 DrmHwcLayer layer;
765 l.second->PopulateDrmLayer(&layer);
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200766 int ret = layer.ImportBuffer(drm_);
Sean Paulac874152016-03-10 16:00:26 -0500767 if (ret) {
768 ALOGE("Failed to import layer, ret=%d", ret);
769 return HWC2::Error::NoResources;
770 }
Matvii Zorin5368b732021-01-12 10:53:08 +0200771 composition_layers.emplace_back(std::move(layer));
Sean Paulac874152016-03-10 16:00:26 -0500772 }
Sean Paulac874152016-03-10 16:00:26 -0500773
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300774 auto composition = std::make_shared<DrmDisplayComposition>(crtc_,
Matvii Zorin704ea0e2021-01-08 12:55:45 +0200775 planner_.get());
Sean Paulac874152016-03-10 16:00:26 -0500776
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200777 // TODO(nobody): Don't always assume geometry changed
Matvii Zorin5368b732021-01-12 10:53:08 +0200778 int ret = composition->SetLayers(composition_layers.data(),
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300779 composition_layers.size());
Sean Paulac874152016-03-10 16:00:26 -0500780 if (ret) {
781 ALOGE("Failed to set layers in the composition ret=%d", ret);
782 return HWC2::Error::BadLayer;
783 }
784
785 std::vector<DrmPlane *> primary_planes(primary_planes_);
786 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500787 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500788 if (ret) {
John Stultz66763d52021-08-24 04:59:25 +0000789 ALOGV("Failed to plan the composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500790 return HWC2::Error::BadConfig;
791 }
792
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300793 a_args.composition = composition;
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300794 ret = compositor_.ExecuteAtomicCommit(a_args);
795
Sean Paulac874152016-03-10 16:00:26 -0500796 if (ret) {
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300797 if (!a_args.test_only)
John Stultz78c9f6c2018-05-24 16:43:35 -0700798 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500799 return HWC2::Error::BadParameter;
800 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500801 return HWC2::Error::None;
802}
803
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300804/* Find API details at:
805 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
806 */
Matteo Franchinc56eede2019-12-03 17:10:38 +0000807HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500808 supported(__func__);
809 HWC2::Error ret;
810
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200811 ++total_stats_.total_frames_;
812
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300813 AtomicCommitArgs a_args{};
814 ret = CreateComposition(a_args);
815
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200816 if (ret != HWC2::Error::None)
817 ++total_stats_.failed_kms_present_;
818
Rob Herring4f6c62e2018-05-17 14:33:02 -0500819 if (ret == HWC2::Error::BadLayer) {
820 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000821 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500822 return HWC2::Error::None;
823 }
824 if (ret != HWC2::Error::None)
825 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500826
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300827 *present_fence = a_args.out_fence.Release();
Sean Paulac874152016-03-10 16:00:26 -0500828
829 ++frame_no_;
830 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500831}
832
833HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500834 supported(__func__);
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200835
836 int conf = static_cast<int>(config);
837
838 if (hwc_configs_.count(conf) == 0) {
839 ALOGE("Could not find active mode for %d", conf);
Sean Paulac874152016-03-10 16:00:26 -0500840 return HWC2::Error::BadConfig;
841 }
842
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200843 auto &mode = hwc_configs_[conf].mode;
844
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300845 AtomicCommitArgs a_args = {
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200846 .display_mode = mode,
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300847 .clear_active_composition = true,
848 };
849
850 int err = compositor_.ExecuteAtomicCommit(a_args);
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300851 if (err != 0) {
852 ALOGE("Failed to queue mode changing commit %d", err);
Sean Paulac874152016-03-10 16:00:26 -0500853 return HWC2::Error::BadConfig;
854 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300855
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200856 active_config_id_ = conf;
Sean Paulac874152016-03-10 16:00:26 -0500857
858 // Setup the client layer's dimensions
859 hwc_rect_t display_frame = {.left = 0,
860 .top = 0,
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200861 .right = static_cast<int>(mode.h_display()),
862 .bottom = static_cast<int>(mode.v_display())};
Sean Paulac874152016-03-10 16:00:26 -0500863 client_layer_.SetLayerDisplayFrame(display_frame);
Sean Paulac874152016-03-10 16:00:26 -0500864
865 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500866}
867
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300868/* Find API details at:
869 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
870 */
Sean Pauled2ec4b2016-03-10 15:35:40 -0500871HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
872 int32_t acquire_fence,
873 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600874 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500875 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500876
877 client_layer_.set_buffer(target);
John Stultz8adf5442021-07-31 00:19:50 +0000878 client_layer_.acquire_fence_ = UniqueFd(acquire_fence);
Sean Paulac874152016-03-10 16:00:26 -0500879 client_layer_.SetLayerDataspace(dataspace);
Roman Stratiienko33365c22020-10-10 23:06:36 +0300880
881 /* TODO: Do not update source_crop every call.
882 * It makes sense to do it once after every hotplug event. */
883 hwc_drm_bo bo{};
884 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
885
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200886 hwc_frect_t source_crop = {.left = 0.0F,
887 .top = 0.0F,
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300888 .right = static_cast<float>(bo.width),
889 .bottom = static_cast<float>(bo.height)};
Roman Stratiienko33365c22020-10-10 23:06:36 +0300890 client_layer_.SetLayerSourceCrop(source_crop);
891
Sean Paulac874152016-03-10 16:00:26 -0500892 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500893}
894
895HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500896 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800897
Roman Stratiienkod146d6d2020-10-04 23:56:46 +0300898 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
899 return HWC2::Error::BadParameter;
900
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800901 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +0300902 return HWC2::Error::Unsupported;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800903
904 color_mode_ = mode;
905 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500906}
907
908HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500909 int32_t hint) {
910 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200911 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
912 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
913 return HWC2::Error::BadParameter;
914
915 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
916 return HWC2::Error::BadParameter;
917
918 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
919 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
920 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
921
922 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500923}
924
925HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500926 int32_t release_fence) {
927 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200928 // TODO(nobody): Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500929 return unsupported(__func__, buffer, release_fence);
930}
931
Sean Paulac874152016-03-10 16:00:26 -0500932HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
933 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500934 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300935 AtomicCommitArgs a_args{};
936
Sean Paulac874152016-03-10 16:00:26 -0500937 switch (mode) {
938 case HWC2::PowerMode::Off:
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300939 a_args.active = false;
Sean Paulac874152016-03-10 16:00:26 -0500940 break;
941 case HWC2::PowerMode::On:
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300942 a_args.active = true;
Sean Paulac874152016-03-10 16:00:26 -0500943 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100944 case HWC2::PowerMode::Doze:
945 case HWC2::PowerMode::DozeSuspend:
946 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500947 default:
948 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100949 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500950 };
951
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300952 int err = compositor_.ExecuteAtomicCommit(a_args);
953 if (err) {
954 ALOGE("Failed to apply the dpms composition err=%d", err);
Sean Paulac874152016-03-10 16:00:26 -0500955 return HWC2::Error::BadParameter;
956 }
957 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500958}
959
960HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500961 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300962 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500963 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500964}
965
966HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500967 uint32_t *num_requests) {
968 supported(__func__);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500969
Matvii Zorinef3c7972020-08-11 15:15:44 +0300970 return backend_->ValidateDisplay(this, num_types, num_requests);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500971}
972
Matvii Zorined90ef92021-01-29 18:32:06 +0200973std::vector<DrmHwcTwo::HwcLayer *>
974DrmHwcTwo::HwcDisplay::GetOrderLayersByZPos() {
975 std::vector<DrmHwcTwo::HwcLayer *> ordered_layers;
976 ordered_layers.reserve(layers_.size());
977
978 for (auto &[handle, layer] : layers_) {
979 ordered_layers.emplace_back(&layer);
980 }
981
982 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
983 [](const DrmHwcTwo::HwcLayer *lhs, const DrmHwcTwo::HwcLayer *rhs) {
984 return lhs->z_order() < rhs->z_order();
985 });
986
987 return ordered_layers;
988}
989
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300990#if PLATFORM_SDK_VERSION > 29
991HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
992 if (connector_->internal())
993 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
994 else if (connector_->external())
995 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
996 else
997 return HWC2::Error::BadConfig;
998
999 return HWC2::Error::None;
1000}
1001
1002HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayVsyncPeriod(
1003 hwc2_vsync_period_t *outVsyncPeriod /* ns */) {
1004 supported(__func__);
Roman Stratiienkoa148f212021-11-16 18:23:18 +02001005 return GetDisplayAttribute(active_config_id_, HWC2_ATTRIBUTE_VSYNC_PERIOD,
1006 (int32_t *)(outVsyncPeriod));
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001007}
1008
1009HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfigWithConstraints(
1010 hwc2_config_t /*config*/,
1011 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1012 hwc_vsync_period_change_timeline_t *outTimeline) {
1013 supported(__func__);
1014
1015 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1016 return HWC2::Error::BadParameter;
1017 }
1018
1019 return HWC2::Error::BadConfig;
1020}
1021
1022HWC2::Error DrmHwcTwo::HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1023 return HWC2::Error::Unsupported;
1024}
1025
1026HWC2::Error DrmHwcTwo::HwcDisplay::GetSupportedContentTypes(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001027 uint32_t *outNumSupportedContentTypes,
1028 const uint32_t *outSupportedContentTypes) {
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001029 if (outSupportedContentTypes == nullptr)
1030 *outNumSupportedContentTypes = 0;
1031
1032 return HWC2::Error::None;
1033}
1034
1035HWC2::Error DrmHwcTwo::HwcDisplay::SetContentType(int32_t contentType) {
1036 supported(__func__);
1037
1038 if (contentType != HWC2_CONTENT_TYPE_NONE)
1039 return HWC2::Error::Unsupported;
1040
1041 /* TODO: Map to the DRM Connector property:
1042 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
1043 */
1044
1045 return HWC2::Error::None;
1046}
1047#endif
1048
John Stultz8c7229d2020-02-07 21:31:08 +00001049#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001050HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
1051 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
1052 supported(__func__);
1053
Roman Stratiienko3e8ce572021-09-29 12:46:28 +03001054 auto blob = connector_->GetEdidBlob();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001055
Roman Stratiienko3e8ce572021-09-29 12:46:28 +03001056 if (!blob) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001057 ALOGE("Failed to get edid property value.");
1058 return HWC2::Error::Unsupported;
1059 }
1060
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +03001061 if (outData) {
1062 *outDataSize = std::min(*outDataSize, blob->length);
1063 memcpy(outData, blob->data, *outDataSize);
1064 } else {
1065 *outDataSize = blob->length;
1066 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001067 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001068
1069 return HWC2::Error::None;
1070}
1071
1072HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
1073 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
1074 unsupported(__func__, outCapabilities);
1075
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001076 if (outNumCapabilities == nullptr) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001077 return HWC2::Error::BadParameter;
1078 }
1079
1080 *outNumCapabilities = 0;
1081
1082 return HWC2::Error::None;
1083}
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001084
1085HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
1086 bool *supported) {
1087 *supported = false;
1088 return HWC2::Error::None;
1089}
1090
1091HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
1092 float /* brightness */) {
1093 return HWC2::Error::Unsupported;
1094}
1095
John Stultz8c7229d2020-02-07 21:31:08 +00001096#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001097
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001098#if PLATFORM_SDK_VERSION > 27
1099
1100HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
1101 int32_t mode, uint32_t *outNumIntents,
1102 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1103 if (mode != HAL_COLOR_MODE_NATIVE) {
1104 return HWC2::Error::BadParameter;
1105 }
1106
1107 if (outIntents == nullptr) {
1108 *outNumIntents = 1;
1109 return HWC2::Error::None;
1110 }
1111 *outNumIntents = 1;
1112 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1113 return HWC2::Error::None;
1114}
1115
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001116HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
1117 int32_t intent) {
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001118 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1119 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1120 return HWC2::Error::BadParameter;
1121
1122 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
1123 return HWC2::Error::BadParameter;
1124
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001125 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001126 return HWC2::Error::Unsupported;
1127
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001128 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001129 return HWC2::Error::Unsupported;
1130
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001131 color_mode_ = mode;
1132 return HWC2::Error::None;
1133}
1134
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001135#endif /* PLATFORM_SDK_VERSION > 27 */
1136
Roman Stratiienkoaaaa4692021-09-29 12:50:10 +03001137HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t /*x*/,
1138 int32_t /*y*/) {
Sean Paulac874152016-03-10 16:00:26 -05001139 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001140 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001141}
1142
1143HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001144 supported(__func__);
Roman Stratiienko5063d532021-09-29 12:47:31 +03001145 switch (static_cast<HWC2::BlendMode>(mode)) {
1146 case HWC2::BlendMode::None:
1147 blending_ = DrmHwcBlending::kNone;
1148 break;
1149 case HWC2::BlendMode::Premultiplied:
1150 blending_ = DrmHwcBlending::kPreMult;
1151 break;
1152 case HWC2::BlendMode::Coverage:
1153 blending_ = DrmHwcBlending::kCoverage;
1154 break;
1155 default:
1156 ALOGE("Unknown blending mode b=%d", blending_);
1157 blending_ = DrmHwcBlending::kNone;
1158 break;
1159 }
Sean Paulac874152016-03-10 16:00:26 -05001160 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001161}
1162
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +03001163/* Find API details at:
1164 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=2314
1165 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001166HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001167 int32_t acquire_fence) {
1168 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -05001169
Sean Paulac874152016-03-10 16:00:26 -05001170 set_buffer(buffer);
John Stultz8adf5442021-07-31 00:19:50 +00001171 acquire_fence_ = UniqueFd(acquire_fence);
Sean Paulac874152016-03-10 16:00:26 -05001172 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001173}
1174
Roman Stratiienkoaaaa4692021-09-29 12:50:10 +03001175HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t /*color*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001176 // TODO(nobody): Put to client composition here?
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001177 supported(__func__);
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001178 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001179}
1180
1181HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001182 sf_type_ = static_cast<HWC2::Composition>(type);
1183 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001184}
1185
1186HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001187 supported(__func__);
Roman Stratiienko515da8f2021-09-29 12:47:21 +03001188 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
1189 case HAL_DATASPACE_STANDARD_BT709:
1190 color_space_ = DrmHwcColorSpace::kItuRec709;
1191 break;
1192 case HAL_DATASPACE_STANDARD_BT601_625:
1193 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
1194 case HAL_DATASPACE_STANDARD_BT601_525:
1195 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
1196 color_space_ = DrmHwcColorSpace::kItuRec601;
1197 break;
1198 case HAL_DATASPACE_STANDARD_BT2020:
1199 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
1200 color_space_ = DrmHwcColorSpace::kItuRec2020;
1201 break;
1202 default:
1203 color_space_ = DrmHwcColorSpace::kUndefined;
1204 }
1205
1206 switch (dataspace & HAL_DATASPACE_RANGE_MASK) {
1207 case HAL_DATASPACE_RANGE_FULL:
1208 sample_range_ = DrmHwcSampleRange::kFullRange;
1209 break;
1210 case HAL_DATASPACE_RANGE_LIMITED:
1211 sample_range_ = DrmHwcSampleRange::kLimitedRange;
1212 break;
1213 default:
1214 sample_range_ = DrmHwcSampleRange::kUndefined;
1215 }
Sean Paulac874152016-03-10 16:00:26 -05001216 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001217}
1218
1219HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001220 supported(__func__);
1221 display_frame_ = frame;
1222 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001223}
1224
1225HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001226 supported(__func__);
1227 alpha_ = alpha;
1228 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001229}
1230
1231HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1232 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001233 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001234 // TODO(nobody): We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001235 return unsupported(__func__, stream);
1236}
1237
1238HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001239 supported(__func__);
1240 source_crop_ = crop;
1241 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001242}
1243
1244HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001245 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001246 // TODO(nobody): We don't use surface damage, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001247 unsupported(__func__, damage);
1248 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001249}
1250
1251HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001252 supported(__func__);
Roman Stratiienko2ed4cbe2021-09-29 12:47:35 +03001253
1254 uint32_t l_transform = 0;
1255
1256 // 270* and 180* cannot be combined with flips. More specifically, they
1257 // already contain both horizontal and vertical flips, so those fields are
1258 // redundant in this case. 90* rotation can be combined with either horizontal
1259 // flip or vertical flip, so treat it differently
1260 if (transform == HWC_TRANSFORM_ROT_270) {
1261 l_transform = DrmHwcTransform::kRotate270;
1262 } else if (transform == HWC_TRANSFORM_ROT_180) {
1263 l_transform = DrmHwcTransform::kRotate180;
1264 } else {
1265 if (transform & HWC_TRANSFORM_FLIP_H)
1266 l_transform |= DrmHwcTransform::kFlipH;
1267 if (transform & HWC_TRANSFORM_FLIP_V)
1268 l_transform |= DrmHwcTransform::kFlipV;
1269 if (transform & HWC_TRANSFORM_ROT_90)
1270 l_transform |= DrmHwcTransform::kRotate90;
1271 }
1272
1273 transform_ = static_cast<DrmHwcTransform>(l_transform);
Sean Paulac874152016-03-10 16:00:26 -05001274 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001275}
1276
1277HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001278 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001279 // TODO(nobody): We don't use this information, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001280 unsupported(__func__, visible);
1281 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001282}
1283
Sean Paulac874152016-03-10 16:00:26 -05001284HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1285 supported(__func__);
1286 z_order_ = order;
1287 return HWC2::Error::None;
1288}
1289
1290void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1291 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -05001292 layer->sf_handle = buffer_;
Roman Stratiienko0fade372021-02-20 13:59:55 +02001293 // TODO(rsglobal): Avoid extra fd duplication
1294 layer->acquire_fence = UniqueFd(fcntl(acquire_fence_.Get(), F_DUPFD_CLOEXEC));
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001295 layer->display_frame = display_frame_;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001296 layer->alpha = lround(65535.0F * alpha_);
Roman Stratiienko5063d532021-09-29 12:47:31 +03001297 layer->blending = blending_;
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001298 layer->source_crop = source_crop_;
Roman Stratiienko2ed4cbe2021-09-29 12:47:35 +03001299 layer->transform = transform_;
Roman Stratiienko515da8f2021-09-29 12:47:21 +03001300 layer->color_space = color_space_;
1301 layer->sample_range = sample_range_;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001302}
1303
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001304void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +03001305 const std::lock_guard<std::mutex> lock(callback_lock_);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001306
Roman Stratiienko863a3c22021-09-29 13:00:29 +03001307 if (hotplug_callback_.first != nullptr &&
1308 hotplug_callback_.second != nullptr) {
1309 hotplug_callback_.first(hotplug_callback_.second, displayid,
1310 state == DRM_MODE_CONNECTED
1311 ? HWC2_CONNECTION_CONNECTED
1312 : HWC2_CONNECTION_DISCONNECTED);
1313 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001314}
1315
1316void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
Roman Stratiienkod21071f2021-03-09 21:56:50 +02001317 for (const auto &conn : drmDevice->connectors()) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001318 if (conn->state() != DRM_MODE_CONNECTED)
1319 continue;
1320 HandleDisplayHotplug(conn->display(), conn->state());
1321 }
1322}
1323
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001324void DrmHwcTwo::HandleHotplugUEvent() {
1325 for (const auto &drm : resource_manager_.getDrmDevices()) {
1326 for (const auto &conn : drm->connectors()) {
1327 drmModeConnection old_state = conn->state();
1328 drmModeConnection cur_state = conn->UpdateModes()
1329 ? DRM_MODE_UNKNOWNCONNECTION
1330 : conn->state();
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001331
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001332 if (cur_state == old_state)
1333 continue;
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001334
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001335 ALOGI("%s event for connector %u on display %d",
1336 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", conn->id(),
1337 conn->display());
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001338
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001339 int display_id = conn->display();
1340 if (cur_state == DRM_MODE_CONNECTED) {
1341 auto &display = displays_.at(display_id);
1342 display.ChosePreferredConfig();
1343 } else {
1344 auto &display = displays_.at(display_id);
1345 display.ClearDisplay();
1346 }
1347
1348 HandleDisplayHotplug(display_id, cur_state);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001349 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001350 }
1351}
1352
Sean Pauled2ec4b2016-03-10 15:35:40 -05001353// static
1354int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1355 unsupported(__func__);
1356 return 0;
1357}
1358
1359// static
1360void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001361 uint32_t *out_count,
1362 int32_t * /*out_capabilities*/) {
1363 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001364 *out_count = 0;
1365}
1366
1367// static
Sean Paulac874152016-03-10 16:00:26 -05001368hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1369 struct hwc2_device * /*dev*/, int32_t descriptor) {
1370 supported(__func__);
1371 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001372 switch (func) {
1373 // Device functions
1374 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1375 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1376 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1377 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001378 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001379 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1380 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1381 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1382 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1383 case HWC2::FunctionDescriptor::Dump:
1384 return ToHook<HWC2_PFN_DUMP>(
1385 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1386 uint32_t *, char *>);
1387 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1388 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1389 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1390 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1391 case HWC2::FunctionDescriptor::RegisterCallback:
1392 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1393 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1394 &DrmHwcTwo::RegisterCallback, int32_t,
1395 hwc2_callback_data_t, hwc2_function_pointer_t>);
1396
1397 // Display functions
1398 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1399 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1400 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1401 &HwcDisplay::AcceptDisplayChanges>);
1402 case HWC2::FunctionDescriptor::CreateLayer:
1403 return ToHook<HWC2_PFN_CREATE_LAYER>(
1404 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1405 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1406 case HWC2::FunctionDescriptor::DestroyLayer:
1407 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1408 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1409 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1410 case HWC2::FunctionDescriptor::GetActiveConfig:
1411 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1412 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1413 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1414 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1415 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1416 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1417 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1418 hwc2_layer_t *, int32_t *>);
1419 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1420 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1421 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1422 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1423 int32_t, int32_t>);
1424 case HWC2::FunctionDescriptor::GetColorModes:
1425 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1426 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1427 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1428 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001429 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1430 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1431 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1432 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001433 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001434 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1435 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1436 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1437 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001438 case HWC2::FunctionDescriptor::GetDisplayName:
1439 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1440 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1441 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1442 case HWC2::FunctionDescriptor::GetDisplayRequests:
1443 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1444 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1445 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1446 hwc2_layer_t *, int32_t *>);
1447 case HWC2::FunctionDescriptor::GetDisplayType:
1448 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1449 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1450 &HwcDisplay::GetDisplayType, int32_t *>);
1451 case HWC2::FunctionDescriptor::GetDozeSupport:
1452 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1453 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1454 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001455 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1456 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1457 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1458 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1459 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001460 case HWC2::FunctionDescriptor::GetReleaseFences:
1461 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1462 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1463 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1464 int32_t *>);
1465 case HWC2::FunctionDescriptor::PresentDisplay:
1466 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1467 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1468 &HwcDisplay::PresentDisplay, int32_t *>);
1469 case HWC2::FunctionDescriptor::SetActiveConfig:
1470 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1471 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1472 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1473 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001474 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1475 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1476 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1477 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001478 case HWC2::FunctionDescriptor::SetColorMode:
1479 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1480 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1481 &HwcDisplay::SetColorMode, int32_t>);
1482 case HWC2::FunctionDescriptor::SetColorTransform:
1483 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1484 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1485 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1486 case HWC2::FunctionDescriptor::SetOutputBuffer:
1487 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1488 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1489 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1490 case HWC2::FunctionDescriptor::SetPowerMode:
1491 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1492 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1493 &HwcDisplay::SetPowerMode, int32_t>);
1494 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1495 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1496 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1497 &HwcDisplay::SetVsyncEnabled, int32_t>);
1498 case HWC2::FunctionDescriptor::ValidateDisplay:
1499 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1500 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1501 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001502#if PLATFORM_SDK_VERSION > 27
1503 case HWC2::FunctionDescriptor::GetRenderIntents:
1504 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1505 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1506 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1507 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001508 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1509 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1510 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1511 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001512#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001513#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001514 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1515 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1516 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1517 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1518 uint32_t *, uint8_t *>);
1519 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1520 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1521 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1522 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1523 uint32_t *>);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001524 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1525 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1526 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1527 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1528 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1529 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1530 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1531 &HwcDisplay::SetDisplayBrightness, float>);
John Stultz8c7229d2020-02-07 21:31:08 +00001532#endif /* PLATFORM_SDK_VERSION > 28 */
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001533#if PLATFORM_SDK_VERSION > 29
1534 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
1535 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
1536 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
1537 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
1538 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
1539 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
1540 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
1541 &HwcDisplay::GetDisplayVsyncPeriod,
1542 hwc2_vsync_period_t *>);
1543 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
1544 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
1545 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
1546 &HwcDisplay::SetActiveConfigWithConstraints,
1547 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
1548 hwc_vsync_period_change_timeline_t *>);
1549 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
1550 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
1551 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
1552 &HwcDisplay::SetAutoLowLatencyMode, bool>);
1553 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
1554 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
1555 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
1556 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
1557 uint32_t *>);
1558 case HWC2::FunctionDescriptor::SetContentType:
1559 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
1560 DisplayHook<decltype(&HwcDisplay::SetContentType),
1561 &HwcDisplay::SetContentType, int32_t>);
1562#endif
Sean Pauled2ec4b2016-03-10 15:35:40 -05001563 // Layer functions
1564 case HWC2::FunctionDescriptor::SetCursorPosition:
1565 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1566 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1567 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1568 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1569 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1570 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1571 &HwcLayer::SetLayerBlendMode, int32_t>);
1572 case HWC2::FunctionDescriptor::SetLayerBuffer:
1573 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1574 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1575 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1576 case HWC2::FunctionDescriptor::SetLayerColor:
1577 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1578 LayerHook<decltype(&HwcLayer::SetLayerColor),
1579 &HwcLayer::SetLayerColor, hwc_color_t>);
1580 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1581 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1582 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1583 &HwcLayer::SetLayerCompositionType, int32_t>);
1584 case HWC2::FunctionDescriptor::SetLayerDataspace:
1585 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1586 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1587 &HwcLayer::SetLayerDataspace, int32_t>);
1588 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1589 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1590 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1591 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1592 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1593 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1594 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1595 &HwcLayer::SetLayerPlaneAlpha, float>);
1596 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001597 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1598 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1599 &HwcLayer::SetLayerSidebandStream,
1600 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001601 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1602 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1603 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1604 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1605 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1606 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1607 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1608 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1609 case HWC2::FunctionDescriptor::SetLayerTransform:
1610 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1611 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1612 &HwcLayer::SetLayerTransform, int32_t>);
1613 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1614 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1615 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1616 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1617 case HWC2::FunctionDescriptor::SetLayerZOrder:
1618 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1619 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1620 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001621 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001622 default:
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001623 return nullptr;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001624 }
1625}
Sean Paulac874152016-03-10 16:00:26 -05001626
1627// static
1628int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1629 struct hw_device_t **dev) {
1630 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001631 if (strcmp(name, HWC_HARDWARE_COMPOSER) != 0) {
Sean Paulac874152016-03-10 16:00:26 -05001632 ALOGE("Invalid module name- %s", name);
1633 return -EINVAL;
1634 }
1635
1636 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1637 if (!ctx) {
1638 ALOGE("Failed to allocate DrmHwcTwo");
1639 return -ENOMEM;
1640 }
1641
1642 HWC2::Error err = ctx->Init();
1643 if (err != HWC2::Error::None) {
1644 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1645 return -EINVAL;
1646 }
1647
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001648 ctx->common.module = (hw_module_t *)module;
Sean Paulac874152016-03-10 16:00:26 -05001649 *dev = &ctx->common;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001650 ctx.release(); // NOLINT(bugprone-unused-return-value)
Sean Paulac874152016-03-10 16:00:26 -05001651 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001652}
Sean Paulf72cccd2018-08-27 13:59:08 -04001653} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001654
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001655// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001656static struct hw_module_methods_t hwc2_module_methods = {
1657 .open = android::DrmHwcTwo::HookDevOpen,
1658};
1659
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001660// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001661hw_module_t HAL_MODULE_INFO_SYM = {
1662 .tag = HARDWARE_MODULE_TAG,
1663 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1664 .id = HWC_HARDWARE_MODULE_ID,
1665 .name = "DrmHwcTwo module",
1666 .author = "The Android Open Source Project",
1667 .methods = &hwc2_module_methods,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001668 .dso = nullptr,
Sean Paulac874152016-03-10 16:00:26 -05001669 .reserved = {0},
1670};