blob: 9bf132724ba83e14d4936a9bc251cac1c1e94bb5 [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
498HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
499 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500500 supported(__func__);
501 // Since this callback is normally invoked twice (once to get the count, and
502 // once to populate configs), we don't really want to read the edid
503 // redundantly. Instead, only update the modes on the first invocation. While
504 // it's possible this will result in stale modes, it'll all come out in the
505 // wash when we try to set the active config later.
506 if (!configs) {
507 int ret = connector_->UpdateModes();
508 if (ret) {
509 ALOGE("Failed to update display modes %d", ret);
510 return HWC2::Error::BadDisplay;
511 }
Sean Paulac874152016-03-10 16:00:26 -0500512
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200513 hwc_configs_.clear();
514 preferred_config_id_ = 0;
515 int preferred_config_group_id_ = 0;
Neil Armstrongb67d0492019-06-20 09:00:21 +0000516
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200517 if (connector_->modes().empty()) {
518 ALOGE("No modes reported by KMS");
519 return HWC2::Error::BadDisplay;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000520 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000521
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200522 int last_config_id = 1;
523 int last_group_id = 1;
Neil Armstrongb67d0492019-06-20 09:00:21 +0000524
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200525 /* Group modes */
526 for (const auto &mode : connector_->modes()) {
527 /* Find group for the new mode or create new group */
528 int group_found = 0;
529 for (auto &hwc_config : hwc_configs_) {
530 if (mode.h_display() == hwc_config.second.mode.h_display() &&
531 mode.v_display() == hwc_config.second.mode.v_display()) {
532 group_found = hwc_config.second.group_id;
533 }
534 }
535 if (group_found == 0) {
536 group_found = last_group_id++;
537 }
538
539 bool disabled = false;
540 if (mode.flags() & DRM_MODE_FLAG_3D_MASK) {
541 ALOGI("Disabling display mode %s (Modes with 3D flag aren't supported)",
542 mode.name().c_str());
543 disabled = true;
544 }
545
546 /* Add config */
547 hwc_configs_[last_config_id] = {
548 .id = last_config_id,
549 .group_id = group_found,
550 .mode = mode,
551 .disabled = disabled,
552 };
553
554 /* Chwck if the mode is preferred */
555 if ((mode.type() & DRM_MODE_TYPE_PREFERRED) != 0 &&
556 preferred_config_id_ == 0) {
557 preferred_config_id_ = last_config_id;
558 preferred_config_group_id_ = group_found;
559 }
560
561 last_config_id++;
562 }
563
564 /* We must have preferred mode. Set first mode as preferred
565 * in case KMS haven't reported anything. */
566 if (preferred_config_id_ == 0) {
567 preferred_config_id_ = 1;
568 preferred_config_group_id_ = 1;
569 }
570
571 for (int group = 1; group < last_group_id; group++) {
572 bool has_interlaced = false;
573 bool has_progressive = false;
574 for (auto &hwc_config : hwc_configs_) {
575 if (hwc_config.second.group_id != group || hwc_config.second.disabled) {
576 continue;
577 }
578
579 if (hwc_config.second.IsInterlaced()) {
580 has_interlaced = true;
581 } else {
582 has_progressive = true;
583 }
584 }
585
586 bool has_both = has_interlaced && has_progressive;
587 if (!has_both) {
588 continue;
589 }
590
591 bool group_contains_preferred_interlaced = false;
592 if (group == preferred_config_group_id_ &&
593 hwc_configs_[preferred_config_id_].IsInterlaced()) {
594 group_contains_preferred_interlaced = true;
595 }
596
597 for (auto &hwc_config : hwc_configs_) {
598 if (hwc_config.second.group_id != group || hwc_config.second.disabled) {
599 continue;
600 }
601
602 bool disable = group_contains_preferred_interlaced
603 ? !hwc_config.second.IsInterlaced()
604 : hwc_config.second.IsInterlaced();
605
606 if (disable) {
607 ALOGI(
608 "Group %i: Disabling display mode %s (This group should consist "
609 "of %s modes)",
610 group, hwc_config.second.mode.name().c_str(),
611 group_contains_preferred_interlaced ? "interlaced"
612 : "progressive");
613
614 hwc_config.second.disabled = true;
615 }
616 }
617 }
618
619 /* Group should not contain 2 modes with FPS delta less than ~1HZ
620 * otherwise android.graphics.cts.SetFrameRateTest CTS will fail
621 */
622 for (int m1 = 1; m1 < last_config_id; m1++) {
623 for (int m2 = 1; m2 < last_config_id; m2++) {
624 if (m1 != m2 &&
625 hwc_configs_[m1].group_id == hwc_configs_[m2].group_id &&
626 !hwc_configs_[m1].disabled && !hwc_configs_[m2].disabled &&
627 fabsf(hwc_configs_[m1].mode.v_refresh() -
628 hwc_configs_[m2].mode.v_refresh()) < 1.0) {
629 ALOGI(
630 "Group %i: Disabling display mode %s (Refresh rate value is "
631 "too close to existing mode %s)",
632 hwc_configs_[m2].group_id, hwc_configs_[m2].mode.name().c_str(),
633 hwc_configs_[m1].mode.name().c_str());
634
635 hwc_configs_[m2].disabled = true;
636 }
637 }
638 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000639 }
640
641 uint32_t idx = 0;
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200642 for (auto &hwc_config : hwc_configs_) {
643 if (hwc_config.second.disabled) {
644 continue;
645 }
646
647 if (configs != nullptr) {
648 if (idx >= *num_configs) {
649 break;
650 }
651 configs[idx] = hwc_config.second.id;
652 }
653
654 idx++;
Sean Paulac874152016-03-10 16:00:26 -0500655 }
656 *num_configs = idx;
657 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500658}
659
660HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500661 supported(__func__);
662 std::ostringstream stream;
663 stream << "display-" << connector_->id();
664 std::string string = stream.str();
665 size_t length = string.length();
666 if (!name) {
667 *size = length;
668 return HWC2::Error::None;
669 }
670
671 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
672 strncpy(name, string.c_str(), *size);
673 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500674}
675
Sean Paulac874152016-03-10 16:00:26 -0500676HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
677 uint32_t *num_elements,
678 hwc2_layer_t *layers,
679 int32_t *layer_requests) {
680 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200681 // TODO(nobody): I think virtual display should request
Sean Paulac874152016-03-10 16:00:26 -0500682 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
683 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
684 *num_elements = 0;
685 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500686}
687
688HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500689 supported(__func__);
690 *type = static_cast<int32_t>(type_);
691 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500692}
693
694HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500695 supported(__func__);
696 *support = 0;
697 return HWC2::Error::None;
698}
699
700HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400701 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
702 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500703 supported(__func__);
704 *num_types = 0;
705 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500706}
707
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300708/* Find API details at:
709 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
710 */
Sean Pauled2ec4b2016-03-10 15:35:40 -0500711HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500712 hwc2_layer_t *layers,
713 int32_t *fences) {
714 supported(__func__);
715 uint32_t num_layers = 0;
716
717 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
718 ++num_layers;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200719 if (layers == nullptr || fences == nullptr)
Sean Paulac874152016-03-10 16:00:26 -0500720 continue;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200721
722 if (num_layers > *num_elements) {
Sean Paulac874152016-03-10 16:00:26 -0500723 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
724 return HWC2::Error::None;
725 }
726
727 layers[num_layers - 1] = l.first;
Roman Stratiienko0fade372021-02-20 13:59:55 +0200728 fences[num_layers - 1] = l.second.release_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500729 }
730 *num_elements = num_layers;
731 return HWC2::Error::None;
732}
733
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300734HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(AtomicCommitArgs &a_args) {
Sean Paulac874152016-03-10 16:00:26 -0500735 // order the layers by z-order
736 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100737 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500738 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
739 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200740 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500741 case HWC2::Composition::Device:
742 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
743 break;
744 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100745 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500746 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100747 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500748 break;
749 default:
750 continue;
751 }
752 }
753 if (use_client_layer)
754 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
755
Rob Herring4f6c62e2018-05-17 14:33:02 -0500756 if (z_map.empty())
757 return HWC2::Error::BadLayer;
758
Matvii Zorin5368b732021-01-12 10:53:08 +0200759 std::vector<DrmHwcLayer> composition_layers;
760
Sean Paulac874152016-03-10 16:00:26 -0500761 // now that they're ordered by z, add them to the composition
762 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
763 DrmHwcLayer layer;
764 l.second->PopulateDrmLayer(&layer);
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200765 int ret = layer.ImportBuffer(drm_);
Sean Paulac874152016-03-10 16:00:26 -0500766 if (ret) {
767 ALOGE("Failed to import layer, ret=%d", ret);
768 return HWC2::Error::NoResources;
769 }
Matvii Zorin5368b732021-01-12 10:53:08 +0200770 composition_layers.emplace_back(std::move(layer));
Sean Paulac874152016-03-10 16:00:26 -0500771 }
Sean Paulac874152016-03-10 16:00:26 -0500772
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300773 auto composition = std::make_shared<DrmDisplayComposition>(crtc_,
Matvii Zorin704ea0e2021-01-08 12:55:45 +0200774 planner_.get());
Sean Paulac874152016-03-10 16:00:26 -0500775
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200776 // TODO(nobody): Don't always assume geometry changed
Matvii Zorin5368b732021-01-12 10:53:08 +0200777 int ret = composition->SetLayers(composition_layers.data(),
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300778 composition_layers.size());
Sean Paulac874152016-03-10 16:00:26 -0500779 if (ret) {
780 ALOGE("Failed to set layers in the composition ret=%d", ret);
781 return HWC2::Error::BadLayer;
782 }
783
784 std::vector<DrmPlane *> primary_planes(primary_planes_);
785 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500786 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500787 if (ret) {
John Stultz66763d52021-08-24 04:59:25 +0000788 ALOGV("Failed to plan the composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500789 return HWC2::Error::BadConfig;
790 }
791
792 // Disable the planes we're not using
793 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
794 composition->AddPlaneDisable(*i);
795 i = primary_planes.erase(i);
796 }
797 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
798 composition->AddPlaneDisable(*i);
799 i = overlay_planes.erase(i);
800 }
801
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300802 a_args.composition = composition;
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300803 ret = compositor_.ExecuteAtomicCommit(a_args);
804
Sean Paulac874152016-03-10 16:00:26 -0500805 if (ret) {
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300806 if (!a_args.test_only)
John Stultz78c9f6c2018-05-24 16:43:35 -0700807 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500808 return HWC2::Error::BadParameter;
809 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500810 return HWC2::Error::None;
811}
812
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300813/* Find API details at:
814 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
815 */
Matteo Franchinc56eede2019-12-03 17:10:38 +0000816HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500817 supported(__func__);
818 HWC2::Error ret;
819
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200820 ++total_stats_.total_frames_;
821
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300822 AtomicCommitArgs a_args{};
823 ret = CreateComposition(a_args);
824
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200825 if (ret != HWC2::Error::None)
826 ++total_stats_.failed_kms_present_;
827
Rob Herring4f6c62e2018-05-17 14:33:02 -0500828 if (ret == HWC2::Error::BadLayer) {
829 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000830 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500831 return HWC2::Error::None;
832 }
833 if (ret != HWC2::Error::None)
834 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500835
Roman Stratiienko2a1f1ae2021-10-23 17:47:35 +0300836 *present_fence = a_args.out_fence.Release();
Sean Paulac874152016-03-10 16:00:26 -0500837
838 ++frame_no_;
839 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500840}
841
842HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500843 supported(__func__);
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200844
845 int conf = static_cast<int>(config);
846
847 if (hwc_configs_.count(conf) == 0) {
848 ALOGE("Could not find active mode for %d", conf);
Sean Paulac874152016-03-10 16:00:26 -0500849 return HWC2::Error::BadConfig;
850 }
851
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200852 auto &mode = hwc_configs_[conf].mode;
853
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300854 AtomicCommitArgs a_args = {
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200855 .display_mode = mode,
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300856 .clear_active_composition = true,
857 };
858
859 int err = compositor_.ExecuteAtomicCommit(a_args);
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300860 if (err != 0) {
861 ALOGE("Failed to queue mode changing commit %d", err);
Sean Paulac874152016-03-10 16:00:26 -0500862 return HWC2::Error::BadConfig;
863 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300864
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200865 active_config_id_ = conf;
Sean Paulac874152016-03-10 16:00:26 -0500866
867 // Setup the client layer's dimensions
868 hwc_rect_t display_frame = {.left = 0,
869 .top = 0,
Roman Stratiienkoa148f212021-11-16 18:23:18 +0200870 .right = static_cast<int>(mode.h_display()),
871 .bottom = static_cast<int>(mode.v_display())};
Sean Paulac874152016-03-10 16:00:26 -0500872 client_layer_.SetLayerDisplayFrame(display_frame);
Sean Paulac874152016-03-10 16:00:26 -0500873
874 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500875}
876
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300877/* Find API details at:
878 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
879 */
Sean Pauled2ec4b2016-03-10 15:35:40 -0500880HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
881 int32_t acquire_fence,
882 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600883 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500884 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500885
886 client_layer_.set_buffer(target);
John Stultz8adf5442021-07-31 00:19:50 +0000887 client_layer_.acquire_fence_ = UniqueFd(acquire_fence);
Sean Paulac874152016-03-10 16:00:26 -0500888 client_layer_.SetLayerDataspace(dataspace);
Roman Stratiienko33365c22020-10-10 23:06:36 +0300889
890 /* TODO: Do not update source_crop every call.
891 * It makes sense to do it once after every hotplug event. */
892 hwc_drm_bo bo{};
893 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
894
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200895 hwc_frect_t source_crop = {.left = 0.0F,
896 .top = 0.0F,
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300897 .right = static_cast<float>(bo.width),
898 .bottom = static_cast<float>(bo.height)};
Roman Stratiienko33365c22020-10-10 23:06:36 +0300899 client_layer_.SetLayerSourceCrop(source_crop);
900
Sean Paulac874152016-03-10 16:00:26 -0500901 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500902}
903
904HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500905 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800906
Roman Stratiienkod146d6d2020-10-04 23:56:46 +0300907 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
908 return HWC2::Error::BadParameter;
909
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800910 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +0300911 return HWC2::Error::Unsupported;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800912
913 color_mode_ = mode;
914 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500915}
916
917HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500918 int32_t hint) {
919 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200920 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
921 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
922 return HWC2::Error::BadParameter;
923
924 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
925 return HWC2::Error::BadParameter;
926
927 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
928 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
929 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
930
931 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500932}
933
934HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500935 int32_t release_fence) {
936 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200937 // TODO(nobody): Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500938 return unsupported(__func__, buffer, release_fence);
939}
940
Sean Paulac874152016-03-10 16:00:26 -0500941HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
942 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500943 auto mode = static_cast<HWC2::PowerMode>(mode_in);
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300944 AtomicCommitArgs a_args{};
945
Sean Paulac874152016-03-10 16:00:26 -0500946 switch (mode) {
947 case HWC2::PowerMode::Off:
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300948 a_args.active = false;
Sean Paulac874152016-03-10 16:00:26 -0500949 break;
950 case HWC2::PowerMode::On:
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300951 a_args.active = true;
Sean Paulac874152016-03-10 16:00:26 -0500952 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100953 case HWC2::PowerMode::Doze:
954 case HWC2::PowerMode::DozeSuspend:
955 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500956 default:
957 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100958 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500959 };
960
Roman Stratiienkodccc6fb2021-10-23 17:35:44 +0300961 int err = compositor_.ExecuteAtomicCommit(a_args);
962 if (err) {
963 ALOGE("Failed to apply the dpms composition err=%d", err);
Sean Paulac874152016-03-10 16:00:26 -0500964 return HWC2::Error::BadParameter;
965 }
966 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500967}
968
969HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500970 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300971 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500972 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500973}
974
975HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500976 uint32_t *num_requests) {
977 supported(__func__);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500978
Matvii Zorinef3c7972020-08-11 15:15:44 +0300979 return backend_->ValidateDisplay(this, num_types, num_requests);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500980}
981
Matvii Zorined90ef92021-01-29 18:32:06 +0200982std::vector<DrmHwcTwo::HwcLayer *>
983DrmHwcTwo::HwcDisplay::GetOrderLayersByZPos() {
984 std::vector<DrmHwcTwo::HwcLayer *> ordered_layers;
985 ordered_layers.reserve(layers_.size());
986
987 for (auto &[handle, layer] : layers_) {
988 ordered_layers.emplace_back(&layer);
989 }
990
991 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
992 [](const DrmHwcTwo::HwcLayer *lhs, const DrmHwcTwo::HwcLayer *rhs) {
993 return lhs->z_order() < rhs->z_order();
994 });
995
996 return ordered_layers;
997}
998
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300999#if PLATFORM_SDK_VERSION > 29
1000HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
1001 if (connector_->internal())
1002 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
1003 else if (connector_->external())
1004 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
1005 else
1006 return HWC2::Error::BadConfig;
1007
1008 return HWC2::Error::None;
1009}
1010
1011HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayVsyncPeriod(
1012 hwc2_vsync_period_t *outVsyncPeriod /* ns */) {
1013 supported(__func__);
Roman Stratiienkoa148f212021-11-16 18:23:18 +02001014 return GetDisplayAttribute(active_config_id_, HWC2_ATTRIBUTE_VSYNC_PERIOD,
1015 (int32_t *)(outVsyncPeriod));
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001016}
1017
1018HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfigWithConstraints(
1019 hwc2_config_t /*config*/,
1020 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
1021 hwc_vsync_period_change_timeline_t *outTimeline) {
1022 supported(__func__);
1023
1024 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
1025 return HWC2::Error::BadParameter;
1026 }
1027
1028 return HWC2::Error::BadConfig;
1029}
1030
1031HWC2::Error DrmHwcTwo::HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
1032 return HWC2::Error::Unsupported;
1033}
1034
1035HWC2::Error DrmHwcTwo::HwcDisplay::GetSupportedContentTypes(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001036 uint32_t *outNumSupportedContentTypes,
1037 const uint32_t *outSupportedContentTypes) {
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001038 if (outSupportedContentTypes == nullptr)
1039 *outNumSupportedContentTypes = 0;
1040
1041 return HWC2::Error::None;
1042}
1043
1044HWC2::Error DrmHwcTwo::HwcDisplay::SetContentType(int32_t contentType) {
1045 supported(__func__);
1046
1047 if (contentType != HWC2_CONTENT_TYPE_NONE)
1048 return HWC2::Error::Unsupported;
1049
1050 /* TODO: Map to the DRM Connector property:
1051 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
1052 */
1053
1054 return HWC2::Error::None;
1055}
1056#endif
1057
John Stultz8c7229d2020-02-07 21:31:08 +00001058#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001059HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
1060 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
1061 supported(__func__);
1062
Roman Stratiienko3e8ce572021-09-29 12:46:28 +03001063 auto blob = connector_->GetEdidBlob();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001064
Roman Stratiienko3e8ce572021-09-29 12:46:28 +03001065 if (!blob) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001066 ALOGE("Failed to get edid property value.");
1067 return HWC2::Error::Unsupported;
1068 }
1069
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +03001070 if (outData) {
1071 *outDataSize = std::min(*outDataSize, blob->length);
1072 memcpy(outData, blob->data, *outDataSize);
1073 } else {
1074 *outDataSize = blob->length;
1075 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001076 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001077
1078 return HWC2::Error::None;
1079}
1080
1081HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
1082 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
1083 unsupported(__func__, outCapabilities);
1084
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001085 if (outNumCapabilities == nullptr) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001086 return HWC2::Error::BadParameter;
1087 }
1088
1089 *outNumCapabilities = 0;
1090
1091 return HWC2::Error::None;
1092}
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001093
1094HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
1095 bool *supported) {
1096 *supported = false;
1097 return HWC2::Error::None;
1098}
1099
1100HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
1101 float /* brightness */) {
1102 return HWC2::Error::Unsupported;
1103}
1104
John Stultz8c7229d2020-02-07 21:31:08 +00001105#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001106
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001107#if PLATFORM_SDK_VERSION > 27
1108
1109HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
1110 int32_t mode, uint32_t *outNumIntents,
1111 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1112 if (mode != HAL_COLOR_MODE_NATIVE) {
1113 return HWC2::Error::BadParameter;
1114 }
1115
1116 if (outIntents == nullptr) {
1117 *outNumIntents = 1;
1118 return HWC2::Error::None;
1119 }
1120 *outNumIntents = 1;
1121 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1122 return HWC2::Error::None;
1123}
1124
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001125HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
1126 int32_t intent) {
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001127 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1128 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1129 return HWC2::Error::BadParameter;
1130
1131 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
1132 return HWC2::Error::BadParameter;
1133
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001134 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001135 return HWC2::Error::Unsupported;
1136
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001137 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001138 return HWC2::Error::Unsupported;
1139
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001140 color_mode_ = mode;
1141 return HWC2::Error::None;
1142}
1143
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001144#endif /* PLATFORM_SDK_VERSION > 27 */
1145
Roman Stratiienkoaaaa4692021-09-29 12:50:10 +03001146HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t /*x*/,
1147 int32_t /*y*/) {
Sean Paulac874152016-03-10 16:00:26 -05001148 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001149 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001150}
1151
1152HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001153 supported(__func__);
Roman Stratiienko5063d532021-09-29 12:47:31 +03001154 switch (static_cast<HWC2::BlendMode>(mode)) {
1155 case HWC2::BlendMode::None:
1156 blending_ = DrmHwcBlending::kNone;
1157 break;
1158 case HWC2::BlendMode::Premultiplied:
1159 blending_ = DrmHwcBlending::kPreMult;
1160 break;
1161 case HWC2::BlendMode::Coverage:
1162 blending_ = DrmHwcBlending::kCoverage;
1163 break;
1164 default:
1165 ALOGE("Unknown blending mode b=%d", blending_);
1166 blending_ = DrmHwcBlending::kNone;
1167 break;
1168 }
Sean Paulac874152016-03-10 16:00:26 -05001169 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001170}
1171
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +03001172/* Find API details at:
1173 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=2314
1174 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001175HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001176 int32_t acquire_fence) {
1177 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -05001178
Sean Paulac874152016-03-10 16:00:26 -05001179 set_buffer(buffer);
John Stultz8adf5442021-07-31 00:19:50 +00001180 acquire_fence_ = UniqueFd(acquire_fence);
Sean Paulac874152016-03-10 16:00:26 -05001181 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001182}
1183
Roman Stratiienkoaaaa4692021-09-29 12:50:10 +03001184HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t /*color*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001185 // TODO(nobody): Put to client composition here?
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001186 supported(__func__);
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001187 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001188}
1189
1190HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001191 sf_type_ = static_cast<HWC2::Composition>(type);
1192 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001193}
1194
1195HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001196 supported(__func__);
Roman Stratiienko515da8f2021-09-29 12:47:21 +03001197 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
1198 case HAL_DATASPACE_STANDARD_BT709:
1199 color_space_ = DrmHwcColorSpace::kItuRec709;
1200 break;
1201 case HAL_DATASPACE_STANDARD_BT601_625:
1202 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
1203 case HAL_DATASPACE_STANDARD_BT601_525:
1204 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
1205 color_space_ = DrmHwcColorSpace::kItuRec601;
1206 break;
1207 case HAL_DATASPACE_STANDARD_BT2020:
1208 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
1209 color_space_ = DrmHwcColorSpace::kItuRec2020;
1210 break;
1211 default:
1212 color_space_ = DrmHwcColorSpace::kUndefined;
1213 }
1214
1215 switch (dataspace & HAL_DATASPACE_RANGE_MASK) {
1216 case HAL_DATASPACE_RANGE_FULL:
1217 sample_range_ = DrmHwcSampleRange::kFullRange;
1218 break;
1219 case HAL_DATASPACE_RANGE_LIMITED:
1220 sample_range_ = DrmHwcSampleRange::kLimitedRange;
1221 break;
1222 default:
1223 sample_range_ = DrmHwcSampleRange::kUndefined;
1224 }
Sean Paulac874152016-03-10 16:00:26 -05001225 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001226}
1227
1228HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001229 supported(__func__);
1230 display_frame_ = frame;
1231 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001232}
1233
1234HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001235 supported(__func__);
1236 alpha_ = alpha;
1237 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001238}
1239
1240HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1241 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001242 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001243 // TODO(nobody): We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001244 return unsupported(__func__, stream);
1245}
1246
1247HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001248 supported(__func__);
1249 source_crop_ = crop;
1250 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001251}
1252
1253HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001254 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001255 // TODO(nobody): We don't use surface damage, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001256 unsupported(__func__, damage);
1257 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001258}
1259
1260HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001261 supported(__func__);
Roman Stratiienko2ed4cbe2021-09-29 12:47:35 +03001262
1263 uint32_t l_transform = 0;
1264
1265 // 270* and 180* cannot be combined with flips. More specifically, they
1266 // already contain both horizontal and vertical flips, so those fields are
1267 // redundant in this case. 90* rotation can be combined with either horizontal
1268 // flip or vertical flip, so treat it differently
1269 if (transform == HWC_TRANSFORM_ROT_270) {
1270 l_transform = DrmHwcTransform::kRotate270;
1271 } else if (transform == HWC_TRANSFORM_ROT_180) {
1272 l_transform = DrmHwcTransform::kRotate180;
1273 } else {
1274 if (transform & HWC_TRANSFORM_FLIP_H)
1275 l_transform |= DrmHwcTransform::kFlipH;
1276 if (transform & HWC_TRANSFORM_FLIP_V)
1277 l_transform |= DrmHwcTransform::kFlipV;
1278 if (transform & HWC_TRANSFORM_ROT_90)
1279 l_transform |= DrmHwcTransform::kRotate90;
1280 }
1281
1282 transform_ = static_cast<DrmHwcTransform>(l_transform);
Sean Paulac874152016-03-10 16:00:26 -05001283 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001284}
1285
1286HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001287 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001288 // TODO(nobody): We don't use this information, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001289 unsupported(__func__, visible);
1290 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001291}
1292
Sean Paulac874152016-03-10 16:00:26 -05001293HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1294 supported(__func__);
1295 z_order_ = order;
1296 return HWC2::Error::None;
1297}
1298
1299void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1300 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -05001301 layer->sf_handle = buffer_;
Roman Stratiienko0fade372021-02-20 13:59:55 +02001302 // TODO(rsglobal): Avoid extra fd duplication
1303 layer->acquire_fence = UniqueFd(fcntl(acquire_fence_.Get(), F_DUPFD_CLOEXEC));
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001304 layer->display_frame = display_frame_;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001305 layer->alpha = lround(65535.0F * alpha_);
Roman Stratiienko5063d532021-09-29 12:47:31 +03001306 layer->blending = blending_;
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001307 layer->source_crop = source_crop_;
Roman Stratiienko2ed4cbe2021-09-29 12:47:35 +03001308 layer->transform = transform_;
Roman Stratiienko515da8f2021-09-29 12:47:21 +03001309 layer->color_space = color_space_;
1310 layer->sample_range = sample_range_;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001311}
1312
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001313void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +03001314 const std::lock_guard<std::mutex> lock(callback_lock_);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001315
Roman Stratiienko863a3c22021-09-29 13:00:29 +03001316 if (hotplug_callback_.first != nullptr &&
1317 hotplug_callback_.second != nullptr) {
1318 hotplug_callback_.first(hotplug_callback_.second, displayid,
1319 state == DRM_MODE_CONNECTED
1320 ? HWC2_CONNECTION_CONNECTED
1321 : HWC2_CONNECTION_DISCONNECTED);
1322 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001323}
1324
1325void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
Roman Stratiienkod21071f2021-03-09 21:56:50 +02001326 for (const auto &conn : drmDevice->connectors()) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001327 if (conn->state() != DRM_MODE_CONNECTED)
1328 continue;
1329 HandleDisplayHotplug(conn->display(), conn->state());
1330 }
1331}
1332
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001333void DrmHwcTwo::HandleHotplugUEvent() {
1334 for (const auto &drm : resource_manager_.getDrmDevices()) {
1335 for (const auto &conn : drm->connectors()) {
1336 drmModeConnection old_state = conn->state();
1337 drmModeConnection cur_state = conn->UpdateModes()
1338 ? DRM_MODE_UNKNOWNCONNECTION
1339 : conn->state();
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001340
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001341 if (cur_state == old_state)
1342 continue;
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001343
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001344 ALOGI("%s event for connector %u on display %d",
1345 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", conn->id(),
1346 conn->display());
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001347
Roman Stratiienko1e053b42021-10-25 22:54:20 +03001348 int display_id = conn->display();
1349 if (cur_state == DRM_MODE_CONNECTED) {
1350 auto &display = displays_.at(display_id);
1351 display.ChosePreferredConfig();
1352 } else {
1353 auto &display = displays_.at(display_id);
1354 display.ClearDisplay();
1355 }
1356
1357 HandleDisplayHotplug(display_id, cur_state);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001358 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001359 }
1360}
1361
Sean Pauled2ec4b2016-03-10 15:35:40 -05001362// static
1363int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1364 unsupported(__func__);
1365 return 0;
1366}
1367
1368// static
1369void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001370 uint32_t *out_count,
1371 int32_t * /*out_capabilities*/) {
1372 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001373 *out_count = 0;
1374}
1375
1376// static
Sean Paulac874152016-03-10 16:00:26 -05001377hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1378 struct hwc2_device * /*dev*/, int32_t descriptor) {
1379 supported(__func__);
1380 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001381 switch (func) {
1382 // Device functions
1383 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1384 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1385 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1386 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001387 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001388 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1389 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1390 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1391 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1392 case HWC2::FunctionDescriptor::Dump:
1393 return ToHook<HWC2_PFN_DUMP>(
1394 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1395 uint32_t *, char *>);
1396 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1397 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1398 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1399 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1400 case HWC2::FunctionDescriptor::RegisterCallback:
1401 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1402 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1403 &DrmHwcTwo::RegisterCallback, int32_t,
1404 hwc2_callback_data_t, hwc2_function_pointer_t>);
1405
1406 // Display functions
1407 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1408 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1409 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1410 &HwcDisplay::AcceptDisplayChanges>);
1411 case HWC2::FunctionDescriptor::CreateLayer:
1412 return ToHook<HWC2_PFN_CREATE_LAYER>(
1413 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1414 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1415 case HWC2::FunctionDescriptor::DestroyLayer:
1416 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1417 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1418 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1419 case HWC2::FunctionDescriptor::GetActiveConfig:
1420 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1421 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1422 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1423 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1424 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1425 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1426 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1427 hwc2_layer_t *, int32_t *>);
1428 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1429 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1430 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1431 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1432 int32_t, int32_t>);
1433 case HWC2::FunctionDescriptor::GetColorModes:
1434 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1435 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1436 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1437 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001438 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1439 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1440 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1441 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001442 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001443 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1444 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1445 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1446 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001447 case HWC2::FunctionDescriptor::GetDisplayName:
1448 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1449 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1450 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1451 case HWC2::FunctionDescriptor::GetDisplayRequests:
1452 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1453 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1454 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1455 hwc2_layer_t *, int32_t *>);
1456 case HWC2::FunctionDescriptor::GetDisplayType:
1457 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1458 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1459 &HwcDisplay::GetDisplayType, int32_t *>);
1460 case HWC2::FunctionDescriptor::GetDozeSupport:
1461 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1462 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1463 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001464 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1465 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1466 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1467 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1468 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001469 case HWC2::FunctionDescriptor::GetReleaseFences:
1470 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1471 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1472 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1473 int32_t *>);
1474 case HWC2::FunctionDescriptor::PresentDisplay:
1475 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1476 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1477 &HwcDisplay::PresentDisplay, int32_t *>);
1478 case HWC2::FunctionDescriptor::SetActiveConfig:
1479 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1480 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1481 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1482 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001483 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1484 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1485 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1486 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001487 case HWC2::FunctionDescriptor::SetColorMode:
1488 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1489 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1490 &HwcDisplay::SetColorMode, int32_t>);
1491 case HWC2::FunctionDescriptor::SetColorTransform:
1492 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1493 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1494 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1495 case HWC2::FunctionDescriptor::SetOutputBuffer:
1496 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1497 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1498 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1499 case HWC2::FunctionDescriptor::SetPowerMode:
1500 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1501 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1502 &HwcDisplay::SetPowerMode, int32_t>);
1503 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1504 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1505 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1506 &HwcDisplay::SetVsyncEnabled, int32_t>);
1507 case HWC2::FunctionDescriptor::ValidateDisplay:
1508 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1509 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1510 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001511#if PLATFORM_SDK_VERSION > 27
1512 case HWC2::FunctionDescriptor::GetRenderIntents:
1513 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1514 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1515 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1516 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001517 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1518 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1519 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1520 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001521#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001522#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001523 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1524 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1525 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1526 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1527 uint32_t *, uint8_t *>);
1528 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1529 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1530 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1531 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1532 uint32_t *>);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001533 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1534 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1535 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1536 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1537 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1538 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1539 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1540 &HwcDisplay::SetDisplayBrightness, float>);
John Stultz8c7229d2020-02-07 21:31:08 +00001541#endif /* PLATFORM_SDK_VERSION > 28 */
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001542#if PLATFORM_SDK_VERSION > 29
1543 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
1544 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
1545 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
1546 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
1547 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
1548 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
1549 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
1550 &HwcDisplay::GetDisplayVsyncPeriod,
1551 hwc2_vsync_period_t *>);
1552 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
1553 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
1554 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
1555 &HwcDisplay::SetActiveConfigWithConstraints,
1556 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
1557 hwc_vsync_period_change_timeline_t *>);
1558 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
1559 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
1560 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
1561 &HwcDisplay::SetAutoLowLatencyMode, bool>);
1562 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
1563 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
1564 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
1565 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
1566 uint32_t *>);
1567 case HWC2::FunctionDescriptor::SetContentType:
1568 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
1569 DisplayHook<decltype(&HwcDisplay::SetContentType),
1570 &HwcDisplay::SetContentType, int32_t>);
1571#endif
Sean Pauled2ec4b2016-03-10 15:35:40 -05001572 // Layer functions
1573 case HWC2::FunctionDescriptor::SetCursorPosition:
1574 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1575 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1576 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1577 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1578 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1579 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1580 &HwcLayer::SetLayerBlendMode, int32_t>);
1581 case HWC2::FunctionDescriptor::SetLayerBuffer:
1582 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1583 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1584 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1585 case HWC2::FunctionDescriptor::SetLayerColor:
1586 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1587 LayerHook<decltype(&HwcLayer::SetLayerColor),
1588 &HwcLayer::SetLayerColor, hwc_color_t>);
1589 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1590 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1591 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1592 &HwcLayer::SetLayerCompositionType, int32_t>);
1593 case HWC2::FunctionDescriptor::SetLayerDataspace:
1594 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1595 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1596 &HwcLayer::SetLayerDataspace, int32_t>);
1597 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1598 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1599 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1600 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1601 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1602 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1603 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1604 &HwcLayer::SetLayerPlaneAlpha, float>);
1605 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001606 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1607 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1608 &HwcLayer::SetLayerSidebandStream,
1609 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001610 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1611 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1612 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1613 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1614 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1615 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1616 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1617 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1618 case HWC2::FunctionDescriptor::SetLayerTransform:
1619 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1620 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1621 &HwcLayer::SetLayerTransform, int32_t>);
1622 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1623 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1624 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1625 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1626 case HWC2::FunctionDescriptor::SetLayerZOrder:
1627 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1628 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1629 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001630 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001631 default:
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001632 return nullptr;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001633 }
1634}
Sean Paulac874152016-03-10 16:00:26 -05001635
1636// static
1637int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1638 struct hw_device_t **dev) {
1639 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001640 if (strcmp(name, HWC_HARDWARE_COMPOSER) != 0) {
Sean Paulac874152016-03-10 16:00:26 -05001641 ALOGE("Invalid module name- %s", name);
1642 return -EINVAL;
1643 }
1644
1645 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1646 if (!ctx) {
1647 ALOGE("Failed to allocate DrmHwcTwo");
1648 return -ENOMEM;
1649 }
1650
1651 HWC2::Error err = ctx->Init();
1652 if (err != HWC2::Error::None) {
1653 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1654 return -EINVAL;
1655 }
1656
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001657 ctx->common.module = (hw_module_t *)module;
Sean Paulac874152016-03-10 16:00:26 -05001658 *dev = &ctx->common;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001659 ctx.release(); // NOLINT(bugprone-unused-return-value)
Sean Paulac874152016-03-10 16:00:26 -05001660 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001661}
Sean Paulf72cccd2018-08-27 13:59:08 -04001662} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001663
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001664// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001665static struct hw_module_methods_t hwc2_module_methods = {
1666 .open = android::DrmHwcTwo::HookDevOpen,
1667};
1668
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001669// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001670hw_module_t HAL_MODULE_INFO_SYM = {
1671 .tag = HARDWARE_MODULE_TAG,
1672 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1673 .id = HWC_HARDWARE_MODULE_ID,
1674 .name = "DrmHwcTwo module",
1675 .author = "The Android Open Source Project",
1676 .methods = &hwc2_module_methods,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001677 .dso = nullptr,
Sean Paulac874152016-03-10 16:00:26 -05001678 .reserved = {0},
1679};