blob: abf24e1b745e9a215c3b96c580d34f4c901960df [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
793 // Disable the planes we're not using
794 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
795 composition->AddPlaneDisable(*i);
796 i = primary_planes.erase(i);
797 }
798 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
799 composition->AddPlaneDisable(*i);
800 i = overlay_planes.erase(i);
801 }
802
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300803 a_args.composition = composition;
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300804 ret = compositor_.ExecuteAtomicCommit(a_args);
805
Sean Paulac874152016-03-10 16:00:26 -0500806 if (ret) {
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300807 if (!a_args.test_only)
John Stultz78c9f6c2018-05-24 16:43:35 -0700808 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500809 return HWC2::Error::BadParameter;
810 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500811 return HWC2::Error::None;
812}
813
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300814/* Find API details at:
815 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
816 */
Matteo Franchinc56eede2019-12-03 17:10:38 +0000817HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500818 supported(__func__);
819 HWC2::Error ret;
820
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200821 ++total_stats_.total_frames_;
822
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300823 AtomicCommitArgs a_args{};
824 ret = CreateComposition(a_args);
825
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200826 if (ret != HWC2::Error::None)
827 ++total_stats_.failed_kms_present_;
828
Rob Herring4f6c62e2018-05-17 14:33:02 -0500829 if (ret == HWC2::Error::BadLayer) {
830 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000831 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500832 return HWC2::Error::None;
833 }
834 if (ret != HWC2::Error::None)
835 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500836
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300837 *present_fence = a_args.out_fence.Release();
Sean Paulac874152016-03-10 16:00:26 -0500838
839 ++frame_no_;
840 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500841}
842
843HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500844 supported(__func__);
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200845
846 int conf = static_cast<int>(config);
847
848 if (hwc_configs_.count(conf) == 0) {
849 ALOGE("Could not find active mode for %d", conf);
Sean Paulac874152016-03-10 16:00:26 -0500850 return HWC2::Error::BadConfig;
851 }
852
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200853 auto &mode = hwc_configs_[conf].mode;
854
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300855 AtomicCommitArgs a_args = {
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200856 .display_mode = mode,
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300857 .clear_active_composition = true,
858 };
859
860 int err = compositor_.ExecuteAtomicCommit(a_args);
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300861 if (err != 0) {
862 ALOGE("Failed to queue mode changing commit %d", err);
Sean Paulac874152016-03-10 16:00:26 -0500863 return HWC2::Error::BadConfig;
864 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300865
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200866 active_config_id_ = conf;
Sean Paulac874152016-03-10 16:00:26 -0500867
868 // Setup the client layer's dimensions
869 hwc_rect_t display_frame = {.left = 0,
870 .top = 0,
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200871 .right = static_cast<int>(mode.h_display()),
872 .bottom = static_cast<int>(mode.v_display())};
Sean Paulac874152016-03-10 16:00:26 -0500873 client_layer_.SetLayerDisplayFrame(display_frame);
Sean Paulac874152016-03-10 16:00:26 -0500874
875 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500876}
877
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300878/* Find API details at:
879 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
880 */
Sean Pauled2ec4b2016-03-10 15:35:40 -0500881HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
882 int32_t acquire_fence,
883 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600884 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500885 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500886
887 client_layer_.set_buffer(target);
John Stultz8adf5442021-07-31 00:19:50 +0000888 client_layer_.acquire_fence_ = UniqueFd(acquire_fence);
Sean Paulac874152016-03-10 16:00:26 -0500889 client_layer_.SetLayerDataspace(dataspace);
Roman Stratiienko33365c22020-10-10 23:06:36 +0300890
891 /* TODO: Do not update source_crop every call.
892 * It makes sense to do it once after every hotplug event. */
893 hwc_drm_bo bo{};
894 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
895
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200896 hwc_frect_t source_crop = {.left = 0.0F,
897 .top = 0.0F,
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300898 .right = static_cast<float>(bo.width),
899 .bottom = static_cast<float>(bo.height)};
Roman Stratiienko33365c22020-10-10 23:06:36 +0300900 client_layer_.SetLayerSourceCrop(source_crop);
901
Sean Paulac874152016-03-10 16:00:26 -0500902 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500903}
904
905HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500906 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800907
Roman Stratiienkod146d6d2020-10-04 23:56:46 +0300908 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
909 return HWC2::Error::BadParameter;
910
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800911 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +0300912 return HWC2::Error::Unsupported;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800913
914 color_mode_ = mode;
915 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500916}
917
918HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500919 int32_t hint) {
920 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200921 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
922 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
923 return HWC2::Error::BadParameter;
924
925 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
926 return HWC2::Error::BadParameter;
927
928 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
929 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
930 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
931
932 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500933}
934
935HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500936 int32_t release_fence) {
937 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200938 // TODO(nobody): Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500939 return unsupported(__func__, buffer, release_fence);
940}
941
Sean Paulac874152016-03-10 16:00:26 -0500942HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
943 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500944 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300945 AtomicCommitArgs a_args{};
946
Sean Paulac874152016-03-10 16:00:26 -0500947 switch (mode) {
948 case HWC2::PowerMode::Off:
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300949 a_args.active = false;
Sean Paulac874152016-03-10 16:00:26 -0500950 break;
951 case HWC2::PowerMode::On:
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300952 a_args.active = true;
Sean Paulac874152016-03-10 16:00:26 -0500953 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100954 case HWC2::PowerMode::Doze:
955 case HWC2::PowerMode::DozeSuspend:
956 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500957 default:
958 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100959 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500960 };
961
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300962 int err = compositor_.ExecuteAtomicCommit(a_args);
963 if (err) {
964 ALOGE("Failed to apply the dpms composition err=%d", err);
Sean Paulac874152016-03-10 16:00:26 -0500965 return HWC2::Error::BadParameter;
966 }
967 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500968}
969
970HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500971 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300972 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500973 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500974}
975
976HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500977 uint32_t *num_requests) {
978 supported(__func__);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500979
Matvii Zorinef3c7972020-08-11 15:15:44 +0300980 return backend_->ValidateDisplay(this, num_types, num_requests);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500981}
982
Matvii Zorined90ef92021-01-29 18:32:06 +0200983std::vector<DrmHwcTwo::HwcLayer *>
984DrmHwcTwo::HwcDisplay::GetOrderLayersByZPos() {
985 std::vector<DrmHwcTwo::HwcLayer *> ordered_layers;
986 ordered_layers.reserve(layers_.size());
987
988 for (auto &[handle, layer] : layers_) {
989 ordered_layers.emplace_back(&layer);
990 }
991
992 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
993 [](const DrmHwcTwo::HwcLayer *lhs, const DrmHwcTwo::HwcLayer *rhs) {
994 return lhs->z_order() < rhs->z_order();
995 });
996
997 return ordered_layers;
998}
999
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001000#if PLATFORM_SDK_VERSION > 29
1001HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
1002 if (connector_->internal())
1003 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
1004 else if (connector_->external())
1005 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
1006 else
1007 return HWC2::Error::BadConfig;
1008
1009 return HWC2::Error::None;
1010}
1011
1012HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayVsyncPeriod(
1013 hwc2_vsync_period_t *outVsyncPeriod /* ns */) {
1014 supported(__func__);
Roman Stratiienkoa148f212021-11-16 18:23:18 +02001015 return GetDisplayAttribute(active_config_id_, HWC2_ATTRIBUTE_VSYNC_PERIOD,
1016 (int32_t *)(outVsyncPeriod));
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001017}
1018
1019HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfigWithConstraints(
1020 hwc2_config_t /*config*/,
1021 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1022 hwc_vsync_period_change_timeline_t *outTimeline) {
1023 supported(__func__);
1024
1025 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1026 return HWC2::Error::BadParameter;
1027 }
1028
1029 return HWC2::Error::BadConfig;
1030}
1031
1032HWC2::Error DrmHwcTwo::HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1033 return HWC2::Error::Unsupported;
1034}
1035
1036HWC2::Error DrmHwcTwo::HwcDisplay::GetSupportedContentTypes(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001037 uint32_t *outNumSupportedContentTypes,
1038 const uint32_t *outSupportedContentTypes) {
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001039 if (outSupportedContentTypes == nullptr)
1040 *outNumSupportedContentTypes = 0;
1041
1042 return HWC2::Error::None;
1043}
1044
1045HWC2::Error DrmHwcTwo::HwcDisplay::SetContentType(int32_t contentType) {
1046 supported(__func__);
1047
1048 if (contentType != HWC2_CONTENT_TYPE_NONE)
1049 return HWC2::Error::Unsupported;
1050
1051 /* TODO: Map to the DRM Connector property:
1052 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
1053 */
1054
1055 return HWC2::Error::None;
1056}
1057#endif
1058
John Stultz8c7229d2020-02-07 21:31:08 +00001059#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001060HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
1061 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
1062 supported(__func__);
1063
Roman Stratiienko3e8ce572021-09-29 12:46:28 +03001064 auto blob = connector_->GetEdidBlob();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001065
Roman Stratiienko3e8ce572021-09-29 12:46:28 +03001066 if (!blob) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001067 ALOGE("Failed to get edid property value.");
1068 return HWC2::Error::Unsupported;
1069 }
1070
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +03001071 if (outData) {
1072 *outDataSize = std::min(*outDataSize, blob->length);
1073 memcpy(outData, blob->data, *outDataSize);
1074 } else {
1075 *outDataSize = blob->length;
1076 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001077 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001078
1079 return HWC2::Error::None;
1080}
1081
1082HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
1083 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
1084 unsupported(__func__, outCapabilities);
1085
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001086 if (outNumCapabilities == nullptr) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001087 return HWC2::Error::BadParameter;
1088 }
1089
1090 *outNumCapabilities = 0;
1091
1092 return HWC2::Error::None;
1093}
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001094
1095HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
1096 bool *supported) {
1097 *supported = false;
1098 return HWC2::Error::None;
1099}
1100
1101HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
1102 float /* brightness */) {
1103 return HWC2::Error::Unsupported;
1104}
1105
John Stultz8c7229d2020-02-07 21:31:08 +00001106#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001107
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001108#if PLATFORM_SDK_VERSION > 27
1109
1110HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
1111 int32_t mode, uint32_t *outNumIntents,
1112 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1113 if (mode != HAL_COLOR_MODE_NATIVE) {
1114 return HWC2::Error::BadParameter;
1115 }
1116
1117 if (outIntents == nullptr) {
1118 *outNumIntents = 1;
1119 return HWC2::Error::None;
1120 }
1121 *outNumIntents = 1;
1122 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1123 return HWC2::Error::None;
1124}
1125
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001126HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
1127 int32_t intent) {
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001128 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1129 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1130 return HWC2::Error::BadParameter;
1131
1132 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
1133 return HWC2::Error::BadParameter;
1134
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001135 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001136 return HWC2::Error::Unsupported;
1137
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001138 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001139 return HWC2::Error::Unsupported;
1140
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001141 color_mode_ = mode;
1142 return HWC2::Error::None;
1143}
1144
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001145#endif /* PLATFORM_SDK_VERSION > 27 */
1146
Roman Stratiienkoaaaa4692021-09-29 12:50:10 +03001147HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t /*x*/,
1148 int32_t /*y*/) {
Sean Paulac874152016-03-10 16:00:26 -05001149 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001150 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001151}
1152
1153HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001154 supported(__func__);
Roman Stratiienko5063d532021-09-29 12:47:31 +03001155 switch (static_cast<HWC2::BlendMode>(mode)) {
1156 case HWC2::BlendMode::None:
1157 blending_ = DrmHwcBlending::kNone;
1158 break;
1159 case HWC2::BlendMode::Premultiplied:
1160 blending_ = DrmHwcBlending::kPreMult;
1161 break;
1162 case HWC2::BlendMode::Coverage:
1163 blending_ = DrmHwcBlending::kCoverage;
1164 break;
1165 default:
1166 ALOGE("Unknown blending mode b=%d", blending_);
1167 blending_ = DrmHwcBlending::kNone;
1168 break;
1169 }
Sean Paulac874152016-03-10 16:00:26 -05001170 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001171}
1172
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +03001173/* Find API details at:
1174 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=2314
1175 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001176HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001177 int32_t acquire_fence) {
1178 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -05001179
Sean Paulac874152016-03-10 16:00:26 -05001180 set_buffer(buffer);
John Stultz8adf5442021-07-31 00:19:50 +00001181 acquire_fence_ = UniqueFd(acquire_fence);
Sean Paulac874152016-03-10 16:00:26 -05001182 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001183}
1184
Roman Stratiienkoaaaa4692021-09-29 12:50:10 +03001185HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t /*color*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001186 // TODO(nobody): Put to client composition here?
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001187 supported(__func__);
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001188 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001189}
1190
1191HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001192 sf_type_ = static_cast<HWC2::Composition>(type);
1193 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001194}
1195
1196HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001197 supported(__func__);
Roman Stratiienko515da8f2021-09-29 12:47:21 +03001198 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
1199 case HAL_DATASPACE_STANDARD_BT709:
1200 color_space_ = DrmHwcColorSpace::kItuRec709;
1201 break;
1202 case HAL_DATASPACE_STANDARD_BT601_625:
1203 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
1204 case HAL_DATASPACE_STANDARD_BT601_525:
1205 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
1206 color_space_ = DrmHwcColorSpace::kItuRec601;
1207 break;
1208 case HAL_DATASPACE_STANDARD_BT2020:
1209 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
1210 color_space_ = DrmHwcColorSpace::kItuRec2020;
1211 break;
1212 default:
1213 color_space_ = DrmHwcColorSpace::kUndefined;
1214 }
1215
1216 switch (dataspace & HAL_DATASPACE_RANGE_MASK) {
1217 case HAL_DATASPACE_RANGE_FULL:
1218 sample_range_ = DrmHwcSampleRange::kFullRange;
1219 break;
1220 case HAL_DATASPACE_RANGE_LIMITED:
1221 sample_range_ = DrmHwcSampleRange::kLimitedRange;
1222 break;
1223 default:
1224 sample_range_ = DrmHwcSampleRange::kUndefined;
1225 }
Sean Paulac874152016-03-10 16:00:26 -05001226 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001227}
1228
1229HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001230 supported(__func__);
1231 display_frame_ = frame;
1232 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001233}
1234
1235HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001236 supported(__func__);
1237 alpha_ = alpha;
1238 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001239}
1240
1241HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1242 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001243 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001244 // TODO(nobody): We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001245 return unsupported(__func__, stream);
1246}
1247
1248HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001249 supported(__func__);
1250 source_crop_ = crop;
1251 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001252}
1253
1254HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001255 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001256 // TODO(nobody): We don't use surface damage, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001257 unsupported(__func__, damage);
1258 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001259}
1260
1261HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001262 supported(__func__);
Roman Stratiienko2ed4cbe2021-09-29 12:47:35 +03001263
1264 uint32_t l_transform = 0;
1265
1266 // 270* and 180* cannot be combined with flips. More specifically, they
1267 // already contain both horizontal and vertical flips, so those fields are
1268 // redundant in this case. 90* rotation can be combined with either horizontal
1269 // flip or vertical flip, so treat it differently
1270 if (transform == HWC_TRANSFORM_ROT_270) {
1271 l_transform = DrmHwcTransform::kRotate270;
1272 } else if (transform == HWC_TRANSFORM_ROT_180) {
1273 l_transform = DrmHwcTransform::kRotate180;
1274 } else {
1275 if (transform & HWC_TRANSFORM_FLIP_H)
1276 l_transform |= DrmHwcTransform::kFlipH;
1277 if (transform & HWC_TRANSFORM_FLIP_V)
1278 l_transform |= DrmHwcTransform::kFlipV;
1279 if (transform & HWC_TRANSFORM_ROT_90)
1280 l_transform |= DrmHwcTransform::kRotate90;
1281 }
1282
1283 transform_ = static_cast<DrmHwcTransform>(l_transform);
Sean Paulac874152016-03-10 16:00:26 -05001284 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001285}
1286
1287HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001288 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001289 // TODO(nobody): We don't use this information, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001290 unsupported(__func__, visible);
1291 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001292}
1293
Sean Paulac874152016-03-10 16:00:26 -05001294HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1295 supported(__func__);
1296 z_order_ = order;
1297 return HWC2::Error::None;
1298}
1299
1300void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1301 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -05001302 layer->sf_handle = buffer_;
Roman Stratiienko0fade372021-02-20 13:59:55 +02001303 // TODO(rsglobal): Avoid extra fd duplication
1304 layer->acquire_fence = UniqueFd(fcntl(acquire_fence_.Get(), F_DUPFD_CLOEXEC));
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001305 layer->display_frame = display_frame_;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001306 layer->alpha = lround(65535.0F * alpha_);
Roman Stratiienko5063d532021-09-29 12:47:31 +03001307 layer->blending = blending_;
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001308 layer->source_crop = source_crop_;
Roman Stratiienko2ed4cbe2021-09-29 12:47:35 +03001309 layer->transform = transform_;
Roman Stratiienko515da8f2021-09-29 12:47:21 +03001310 layer->color_space = color_space_;
1311 layer->sample_range = sample_range_;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001312}
1313
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001314void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +03001315 const std::lock_guard<std::mutex> lock(callback_lock_);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001316
Roman Stratiienko863a3c22021-09-29 13:00:29 +03001317 if (hotplug_callback_.first != nullptr &&
1318 hotplug_callback_.second != nullptr) {
1319 hotplug_callback_.first(hotplug_callback_.second, displayid,
1320 state == DRM_MODE_CONNECTED
1321 ? HWC2_CONNECTION_CONNECTED
1322 : HWC2_CONNECTION_DISCONNECTED);
1323 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001324}
1325
1326void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
Roman Stratiienkod21071f2021-03-09 21:56:50 +02001327 for (const auto &conn : drmDevice->connectors()) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001328 if (conn->state() != DRM_MODE_CONNECTED)
1329 continue;
1330 HandleDisplayHotplug(conn->display(), conn->state());
1331 }
1332}
1333
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001334void DrmHwcTwo::HandleHotplugUEvent() {
1335 for (const auto &drm : resource_manager_.getDrmDevices()) {
1336 for (const auto &conn : drm->connectors()) {
1337 drmModeConnection old_state = conn->state();
1338 drmModeConnection cur_state = conn->UpdateModes()
1339 ? DRM_MODE_UNKNOWNCONNECTION
1340 : conn->state();
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001341
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001342 if (cur_state == old_state)
1343 continue;
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001344
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001345 ALOGI("%s event for connector %u on display %d",
1346 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", conn->id(),
1347 conn->display());
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001348
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001349 int display_id = conn->display();
1350 if (cur_state == DRM_MODE_CONNECTED) {
1351 auto &display = displays_.at(display_id);
1352 display.ChosePreferredConfig();
1353 } else {
1354 auto &display = displays_.at(display_id);
1355 display.ClearDisplay();
1356 }
1357
1358 HandleDisplayHotplug(display_id, cur_state);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001359 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001360 }
1361}
1362
Sean Pauled2ec4b2016-03-10 15:35:40 -05001363// static
1364int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1365 unsupported(__func__);
1366 return 0;
1367}
1368
1369// static
1370void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001371 uint32_t *out_count,
1372 int32_t * /*out_capabilities*/) {
1373 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001374 *out_count = 0;
1375}
1376
1377// static
Sean Paulac874152016-03-10 16:00:26 -05001378hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1379 struct hwc2_device * /*dev*/, int32_t descriptor) {
1380 supported(__func__);
1381 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001382 switch (func) {
1383 // Device functions
1384 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1385 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1386 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1387 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001388 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001389 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1390 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1391 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1392 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1393 case HWC2::FunctionDescriptor::Dump:
1394 return ToHook<HWC2_PFN_DUMP>(
1395 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1396 uint32_t *, char *>);
1397 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1398 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1399 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1400 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1401 case HWC2::FunctionDescriptor::RegisterCallback:
1402 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1403 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1404 &DrmHwcTwo::RegisterCallback, int32_t,
1405 hwc2_callback_data_t, hwc2_function_pointer_t>);
1406
1407 // Display functions
1408 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1409 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1410 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1411 &HwcDisplay::AcceptDisplayChanges>);
1412 case HWC2::FunctionDescriptor::CreateLayer:
1413 return ToHook<HWC2_PFN_CREATE_LAYER>(
1414 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1415 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1416 case HWC2::FunctionDescriptor::DestroyLayer:
1417 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1418 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1419 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1420 case HWC2::FunctionDescriptor::GetActiveConfig:
1421 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1422 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1423 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1424 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1425 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1426 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1427 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1428 hwc2_layer_t *, int32_t *>);
1429 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1430 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1431 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1432 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1433 int32_t, int32_t>);
1434 case HWC2::FunctionDescriptor::GetColorModes:
1435 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1436 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1437 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1438 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001439 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1440 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1441 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1442 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001443 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001444 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1445 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1446 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1447 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001448 case HWC2::FunctionDescriptor::GetDisplayName:
1449 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1450 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1451 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1452 case HWC2::FunctionDescriptor::GetDisplayRequests:
1453 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1454 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1455 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1456 hwc2_layer_t *, int32_t *>);
1457 case HWC2::FunctionDescriptor::GetDisplayType:
1458 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1459 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1460 &HwcDisplay::GetDisplayType, int32_t *>);
1461 case HWC2::FunctionDescriptor::GetDozeSupport:
1462 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1463 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1464 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001465 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1466 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1467 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1468 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1469 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001470 case HWC2::FunctionDescriptor::GetReleaseFences:
1471 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1472 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1473 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1474 int32_t *>);
1475 case HWC2::FunctionDescriptor::PresentDisplay:
1476 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1477 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1478 &HwcDisplay::PresentDisplay, int32_t *>);
1479 case HWC2::FunctionDescriptor::SetActiveConfig:
1480 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1481 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1482 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1483 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001484 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1485 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1486 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1487 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001488 case HWC2::FunctionDescriptor::SetColorMode:
1489 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1490 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1491 &HwcDisplay::SetColorMode, int32_t>);
1492 case HWC2::FunctionDescriptor::SetColorTransform:
1493 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1494 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1495 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1496 case HWC2::FunctionDescriptor::SetOutputBuffer:
1497 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1498 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1499 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1500 case HWC2::FunctionDescriptor::SetPowerMode:
1501 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1502 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1503 &HwcDisplay::SetPowerMode, int32_t>);
1504 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1505 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1506 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1507 &HwcDisplay::SetVsyncEnabled, int32_t>);
1508 case HWC2::FunctionDescriptor::ValidateDisplay:
1509 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1510 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1511 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001512#if PLATFORM_SDK_VERSION > 27
1513 case HWC2::FunctionDescriptor::GetRenderIntents:
1514 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1515 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1516 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1517 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001518 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1519 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1520 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1521 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001522#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001523#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001524 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1525 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1526 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1527 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1528 uint32_t *, uint8_t *>);
1529 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1530 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1531 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1532 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1533 uint32_t *>);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001534 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1535 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1536 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1537 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1538 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1539 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1540 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1541 &HwcDisplay::SetDisplayBrightness, float>);
John Stultz8c7229d2020-02-07 21:31:08 +00001542#endif /* PLATFORM_SDK_VERSION > 28 */
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001543#if PLATFORM_SDK_VERSION > 29
1544 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
1545 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
1546 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
1547 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
1548 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
1549 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
1550 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
1551 &HwcDisplay::GetDisplayVsyncPeriod,
1552 hwc2_vsync_period_t *>);
1553 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
1554 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
1555 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
1556 &HwcDisplay::SetActiveConfigWithConstraints,
1557 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
1558 hwc_vsync_period_change_timeline_t *>);
1559 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
1560 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
1561 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
1562 &HwcDisplay::SetAutoLowLatencyMode, bool>);
1563 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
1564 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
1565 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
1566 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
1567 uint32_t *>);
1568 case HWC2::FunctionDescriptor::SetContentType:
1569 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
1570 DisplayHook<decltype(&HwcDisplay::SetContentType),
1571 &HwcDisplay::SetContentType, int32_t>);
1572#endif
Sean Pauled2ec4b2016-03-10 15:35:40 -05001573 // Layer functions
1574 case HWC2::FunctionDescriptor::SetCursorPosition:
1575 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1576 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1577 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1578 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1579 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1580 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1581 &HwcLayer::SetLayerBlendMode, int32_t>);
1582 case HWC2::FunctionDescriptor::SetLayerBuffer:
1583 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1584 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1585 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1586 case HWC2::FunctionDescriptor::SetLayerColor:
1587 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1588 LayerHook<decltype(&HwcLayer::SetLayerColor),
1589 &HwcLayer::SetLayerColor, hwc_color_t>);
1590 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1591 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1592 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1593 &HwcLayer::SetLayerCompositionType, int32_t>);
1594 case HWC2::FunctionDescriptor::SetLayerDataspace:
1595 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1596 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1597 &HwcLayer::SetLayerDataspace, int32_t>);
1598 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1599 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1600 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1601 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1602 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1603 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1604 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1605 &HwcLayer::SetLayerPlaneAlpha, float>);
1606 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001607 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1608 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1609 &HwcLayer::SetLayerSidebandStream,
1610 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001611 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1612 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1613 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1614 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1615 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1616 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1617 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1618 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1619 case HWC2::FunctionDescriptor::SetLayerTransform:
1620 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1621 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1622 &HwcLayer::SetLayerTransform, int32_t>);
1623 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1624 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1625 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1626 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1627 case HWC2::FunctionDescriptor::SetLayerZOrder:
1628 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1629 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1630 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001631 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001632 default:
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001633 return nullptr;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001634 }
1635}
Sean Paulac874152016-03-10 16:00:26 -05001636
1637// static
1638int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1639 struct hw_device_t **dev) {
1640 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001641 if (strcmp(name, HWC_HARDWARE_COMPOSER) != 0) {
Sean Paulac874152016-03-10 16:00:26 -05001642 ALOGE("Invalid module name- %s", name);
1643 return -EINVAL;
1644 }
1645
1646 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1647 if (!ctx) {
1648 ALOGE("Failed to allocate DrmHwcTwo");
1649 return -ENOMEM;
1650 }
1651
1652 HWC2::Error err = ctx->Init();
1653 if (err != HWC2::Error::None) {
1654 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1655 return -EINVAL;
1656 }
1657
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001658 ctx->common.module = (hw_module_t *)module;
Sean Paulac874152016-03-10 16:00:26 -05001659 *dev = &ctx->common;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001660 ctx.release(); // NOLINT(bugprone-unused-return-value)
Sean Paulac874152016-03-10 16:00:26 -05001661 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001662}
Sean Paulf72cccd2018-08-27 13:59:08 -04001663} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001664
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001665// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001666static struct hw_module_methods_t hwc2_module_methods = {
1667 .open = android::DrmHwcTwo::HookDevOpen,
1668};
1669
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001670// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001671hw_module_t HAL_MODULE_INFO_SYM = {
1672 .tag = HARDWARE_MODULE_TAG,
1673 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1674 .id = HWC_HARDWARE_MODULE_ID,
1675 .name = "DrmHwcTwo module",
1676 .author = "The Android Open Source Project",
1677 .methods = &hwc2_module_methods,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001678 .dso = nullptr,
Sean Paulac874152016-03-10 16:00:26 -05001679 .reserved = {0},
1680};