blob: 071f3bf7378d92c7ba86ef5dd44dcb5e7b1a3ded [file] [log] [blame]
Sean Pauled2ec4b2016-03-10 15:35:40 -05001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18#define LOG_TAG "hwc-drm-two"
19
Roman Stratiienko13cc3662020-08-29 21:35:39 +030020#include "DrmHwcTwo.h"
Sean Pauled2ec4b2016-03-10 15:35:40 -050021
Roman Stratiienko0fade372021-02-20 13:59:55 +020022#include <fcntl.h>
Sean Paulac874152016-03-10 16:00:26 -050023#include <hardware/hardware.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050024#include <hardware/hwcomposer2.h>
Roman Stratiienko74774712021-02-05 16:32:47 +020025#include <sync/sync.h>
Roman Stratiienko0fade372021-02-20 13:59:55 +020026#include <unistd.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050027
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020028#include <cinttypes>
Roman Stratiienkod21071f2021-03-09 21:56:50 +020029#include <iostream>
30#include <sstream>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030031#include <string>
32
Roman Stratiienko13cc3662020-08-29 21:35:39 +030033#include "backend/BackendManager.h"
Roman Stratiienko33365c22020-10-10 23:06:36 +030034#include "bufferinfo/BufferInfoGetter.h"
Roman Stratiienko13cc3662020-08-29 21:35:39 +030035#include "compositor/DrmDisplayComposition.h"
Roman Stratiienkod21071f2021-03-09 21:56:50 +020036#include "utils/log.h"
37#include "utils/properties.h"
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030038
Sean Pauled2ec4b2016-03-10 15:35:40 -050039namespace android {
40
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020041DrmHwcTwo::DrmHwcTwo() : hwc2_device() {
Sean Paulac874152016-03-10 16:00:26 -050042 common.tag = HARDWARE_DEVICE_TAG;
43 common.version = HWC_DEVICE_API_VERSION_2_0;
Sean Pauled2ec4b2016-03-10 15:35:40 -050044 common.close = HookDevClose;
45 getCapabilities = HookDevGetCapabilities;
46 getFunction = HookDevGetFunction;
47}
48
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030049HWC2::Error DrmHwcTwo::CreateDisplay(hwc2_display_t displ,
50 HWC2::DisplayType type) {
Roman Stratiienkod26619b2021-08-04 19:55:37 +030051 DrmDevice *drm = resource_manager_.GetDrmDevice(static_cast<int>(displ));
Roman Stratiienko8666dc92021-02-09 17:49:55 +020052 if (!drm) {
53 ALOGE("Failed to get a valid drmresource");
Sean Paulac874152016-03-10 16:00:26 -050054 return HWC2::Error::NoResources;
55 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030056 displays_.emplace(std::piecewise_construct, std::forward_as_tuple(displ),
Roman Stratiienko863a3c22021-09-29 13:00:29 +030057 std::forward_as_tuple(&resource_manager_, drm, displ, type,
58 this));
Sean Paulac874152016-03-10 16:00:26 -050059
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030060 DrmCrtc *crtc = drm->GetCrtcForDisplay(static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050061 if (!crtc) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030062 ALOGE("Failed to get crtc for display %d", static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050063 return HWC2::Error::BadDisplay;
64 }
Roman Stratiienkod21071f2021-03-09 21:56:50 +020065 auto display_planes = std::vector<DrmPlane *>();
66 for (const auto &plane : drm->planes()) {
Sean Paulac874152016-03-10 16:00:26 -050067 if (plane->GetCrtcSupported(*crtc))
68 display_planes.push_back(plane.get());
69 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030070 displays_.at(displ).Init(&display_planes);
Sean Paulac874152016-03-10 16:00:26 -050071 return HWC2::Error::None;
72}
73
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030074HWC2::Error DrmHwcTwo::Init() {
75 int rv = resource_manager_.Init();
76 if (rv) {
77 ALOGE("Can't initialize the resource manager %d", rv);
78 return HWC2::Error::NoResources;
79 }
80
81 HWC2::Error ret = HWC2::Error::None;
82 for (int i = 0; i < resource_manager_.getDisplayCount(); i++) {
83 ret = CreateDisplay(i, HWC2::DisplayType::Physical);
84 if (ret != HWC2::Error::None) {
85 ALOGE("Failed to create display %d with error %d", i, ret);
86 return ret;
87 }
88 }
89
Roman Stratiienkod21071f2021-03-09 21:56:50 +020090 const auto &drm_devices = resource_manager_.getDrmDevices();
91 for (const auto &device : drm_devices) {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020092 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030093 device->RegisterHotplugHandler(new DrmHotplugHandler(this, device.get()));
94 }
95 return ret;
96}
97
Sean Pauled2ec4b2016-03-10 15:35:40 -050098template <typename... Args>
99static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
100 ALOGV("Unsupported function: %s", func);
101 return HWC2::Error::Unsupported;
102}
103
Sean Paulac874152016-03-10 16:00:26 -0500104static inline void supported(char const *func) {
105 ALOGV("Supported function: %s", func);
106}
107
Sean Pauled2ec4b2016-03-10 15:35:40 -0500108HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t width, uint32_t height,
109 int32_t *format,
110 hwc2_display_t *display) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200111 // TODO(nobody): Implement virtual display
Sean Paulac874152016-03-10 16:00:26 -0500112 return unsupported(__func__, width, height, format, display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500113}
114
115HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t display) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200116 // TODO(nobody): Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500117 return unsupported(__func__, display);
118}
119
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200120std::string DrmHwcTwo::HwcDisplay::DumpDelta(
121 DrmHwcTwo::HwcDisplay::Stats delta) {
122 if (delta.total_pixops_ == 0)
123 return "No stats yet";
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200124 double ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200125
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200126 std::stringstream ss;
127 ss << " Total frames count: " << delta.total_frames_ << "\n"
128 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
129 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
130 << ((delta.failed_kms_present_ > 0)
131 ? " !!! Internal failure, FIX it please\n"
132 : "")
133 << " Flattened frames: " << delta.frames_flattened_ << "\n"
134 << " Pixel operations (free units)"
135 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
136 << "]\n"
137 << " Composition efficiency: " << ratio;
138
139 return ss.str();
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200140}
141
142std::string DrmHwcTwo::HwcDisplay::Dump() {
Roman Stratiienkoe8c06792021-09-30 10:09:31 +0300143 std::string flattening_state_str;
144 switch (flattenning_state_) {
145 case ClientFlattenningState::Disabled:
146 flattening_state_str = "Disabled";
147 break;
148 case ClientFlattenningState::NotRequired:
149 flattening_state_str = "Not needed";
150 break;
151 case ClientFlattenningState::Flattened:
152 flattening_state_str = "Active";
153 break;
154 case ClientFlattenningState::ClientRefreshRequested:
155 flattening_state_str = "Refresh requested";
156 break;
157 default:
158 flattening_state_str = std::to_string(flattenning_state_) +
159 " VSync remains";
160 }
161
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200162 std::stringstream ss;
163 ss << "- Display on: " << connector_->name() << "\n"
Roman Stratiienkoe8c06792021-09-30 10:09:31 +0300164 << " Flattening state: " << flattening_state_str << "\n"
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200165 << "Statistics since system boot:\n"
166 << DumpDelta(total_stats_) << "\n\n"
167 << "Statistics since last dumpsys request:\n"
168 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200169
170 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200171 return ss.str();
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200172}
173
174void DrmHwcTwo::Dump(uint32_t *outSize, char *outBuffer) {
175 supported(__func__);
176
177 if (outBuffer != nullptr) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200178 auto copied_bytes = mDumpString.copy(outBuffer, *outSize);
179 *outSize = static_cast<uint32_t>(copied_bytes);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200180 return;
181 }
182
183 std::stringstream output;
184
185 output << "-- drm_hwcomposer --\n\n";
186
187 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &dp : displays_)
188 output << dp.second.Dump();
189
190 mDumpString = output.str();
191 *outSize = static_cast<uint32_t>(mDumpString.size());
Sean Pauled2ec4b2016-03-10 15:35:40 -0500192}
193
194uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200195 // TODO(nobody): Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500196 unsupported(__func__);
197 return 0;
198}
199
200HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500201 hwc2_callback_data_t data,
202 hwc2_function_pointer_t function) {
203 supported(__func__);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300204
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300205 std::unique_lock<std::mutex> lock(callback_lock_);
206
Roman Stratiienko23701092020-09-26 02:08:41 +0300207 switch (static_cast<HWC2::Callback>(descriptor)) {
Sean Paulac874152016-03-10 16:00:26 -0500208 case HWC2::Callback::Hotplug: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300209 hotplug_callback_ = std::make_pair(HWC2_PFN_HOTPLUG(function), data);
210 lock.unlock();
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200211 const auto &drm_devices = resource_manager_.getDrmDevices();
212 for (const auto &device : drm_devices)
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300213 HandleInitialHotplugState(device.get());
Sean Paulac874152016-03-10 16:00:26 -0500214 break;
215 }
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200216 case HWC2::Callback::Refresh: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300217 refresh_callback_ = std::make_pair(HWC2_PFN_REFRESH(function), data);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200218 break;
219 }
Sean Paulac874152016-03-10 16:00:26 -0500220 case HWC2::Callback::Vsync: {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300221 vsync_callback_ = std::make_pair(HWC2_PFN_VSYNC(function), data);
Sean Paulac874152016-03-10 16:00:26 -0500222 break;
223 }
Roman Stratiienko11ef8c52021-09-29 13:01:39 +0300224#if PLATFORM_SDK_VERSION > 29
225 case HWC2::Callback::Vsync_2_4: {
226 vsync_2_4_callback_ = std::make_pair(HWC2_PFN_VSYNC_2_4(function), data);
227 break;
228 }
229#endif
Sean Paulac874152016-03-10 16:00:26 -0500230 default:
231 break;
232 }
233 return HWC2::Error::None;
234}
235
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100236DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200237 DrmDevice *drm, hwc2_display_t handle,
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300238 HWC2::DisplayType type, DrmHwcTwo *hwc2)
239 : hwc2_(hwc2),
240 resource_manager_(resource_manager),
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100241 drm_(drm),
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100242 handle_(handle),
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200243 type_(type),
244 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
Sean Paulac874152016-03-10 16:00:26 -0500245 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200246
247 // clang-format off
248 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
249 0.0, 1.0, 0.0, 0.0,
250 0.0, 0.0, 1.0, 0.0,
251 0.0, 0.0, 0.0, 1.0};
252 // clang-format on
Sean Paulac874152016-03-10 16:00:26 -0500253}
254
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300255void DrmHwcTwo::HwcDisplay::ClearDisplay() {
256 compositor_.ClearDisplay();
257}
258
Sean Paulac874152016-03-10 16:00:26 -0500259HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
260 supported(__func__);
261 planner_ = Planner::CreateInstance(drm_);
262 if (!planner_) {
263 ALOGE("Failed to create planner instance for composition");
264 return HWC2::Error::NoResources;
265 }
266
267 int display = static_cast<int>(handle_);
Roman Stratiienkoe8c06792021-09-30 10:09:31 +0300268 int ret = compositor_.Init(resource_manager_, display);
Sean Paulac874152016-03-10 16:00:26 -0500269 if (ret) {
270 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
271 return HWC2::Error::NoResources;
272 }
273
274 // Split up the given display planes into primary and overlay to properly
275 // interface with the composition
276 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -0700277 property_get("vendor.hwc.drm.use_overlay_planes", use_overlay_planes_prop,
278 "1");
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200279 bool use_overlay_planes = strtol(use_overlay_planes_prop, nullptr, 10);
Sean Paulac874152016-03-10 16:00:26 -0500280 for (auto &plane : *planes) {
281 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
282 primary_planes_.push_back(plane);
283 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
284 overlay_planes_.push_back(plane);
285 }
286
287 crtc_ = drm_->GetCrtcForDisplay(display);
288 if (!crtc_) {
289 ALOGE("Failed to get crtc for display %d", display);
290 return HWC2::Error::BadDisplay;
291 }
292
293 connector_ = drm_->GetConnectorForDisplay(display);
294 if (!connector_) {
295 ALOGE("Failed to get connector for display %d", display);
296 return HWC2::Error::BadDisplay;
297 }
298
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300299 ret = vsync_worker_.Init(drm_, display, [this](int64_t timestamp) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300300 const std::lock_guard<std::mutex> lock(hwc2_->callback_lock_);
Roman Stratiienko11ef8c52021-09-29 13:01:39 +0300301 /* vsync callback */
302#if PLATFORM_SDK_VERSION > 29
303 if (hwc2_->vsync_2_4_callback_.first != nullptr &&
304 hwc2_->vsync_2_4_callback_.second != nullptr) {
305 hwc2_vsync_period_t period_ns{};
306 GetDisplayVsyncPeriod(&period_ns);
307 hwc2_->vsync_2_4_callback_.first(hwc2_->vsync_2_4_callback_.second,
308 handle_, timestamp, period_ns);
309 } else
310#endif
311 if (hwc2_->vsync_callback_.first != nullptr &&
312 hwc2_->vsync_callback_.second != nullptr) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +0300313 hwc2_->vsync_callback_.first(hwc2_->vsync_callback_.second, handle_,
314 timestamp);
315 }
316 });
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300317 if (ret) {
318 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
319 return HWC2::Error::BadDisplay;
320 }
321
Roman Stratiienkoe8c06792021-09-30 10:09:31 +0300322 ret = flattening_vsync_worker_.Init(drm_, display, [this](int64_t /*timestamp*/) {
323 const std::lock_guard<std::mutex> lock(hwc2_->callback_lock_);
324 /* Frontend flattening */
325 if (flattenning_state_ > ClientFlattenningState::ClientRefreshRequested &&
326 --flattenning_state_ ==
327 ClientFlattenningState::ClientRefreshRequested &&
328 hwc2_->refresh_callback_.first != nullptr &&
329 hwc2_->refresh_callback_.second != nullptr) {
330 hwc2_->refresh_callback_.first(hwc2_->refresh_callback_.second, handle_);
331 flattening_vsync_worker_.VSyncControl(false);
332 }
333 });
334 if (ret) {
335 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
336 return HWC2::Error::BadDisplay;
337 }
338
Matvii Zorinef3c7972020-08-11 15:15:44 +0300339 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
340 if (ret) {
341 ALOGE("Failed to set backend for d=%d %d\n", display, ret);
342 return HWC2::Error::BadDisplay;
343 }
344
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300345 return ChosePreferredConfig();
346}
347
348HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() {
Sean Paulac874152016-03-10 16:00:26 -0500349 // Fetch the number of modes from the display
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200350 uint32_t num_configs = 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200351 HWC2::Error err = GetDisplayConfigs(&num_configs, nullptr);
Sean Paulac874152016-03-10 16:00:26 -0500352 if (err != HWC2::Error::None || !num_configs)
353 return err;
354
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200355 return SetActiveConfig(connector_->get_preferred_mode_id());
Sean Paulac874152016-03-10 16:00:26 -0500356}
357
Sean Pauled2ec4b2016-03-10 15:35:40 -0500358HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500359 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500360 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
361 l.second.accept_type_change();
362 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500363}
364
365HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500366 supported(__func__);
367 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
368 *layer = static_cast<hwc2_layer_t>(layer_idx_);
369 ++layer_idx_;
370 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500371}
372
373HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500374 supported(__func__);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100375 if (!get_layer(layer))
376 return HWC2::Error::BadLayer;
377
Sean Paulac874152016-03-10 16:00:26 -0500378 layers_.erase(layer);
379 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500380}
381
382HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500383 supported(__func__);
384 DrmMode const &mode = connector_->active_mode();
385 if (mode.id() == 0)
386 return HWC2::Error::BadConfig;
387
388 *config = mode.id();
389 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500390}
391
392HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
393 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500394 supported(__func__);
395 uint32_t num_changes = 0;
396 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
397 if (l.second.type_changed()) {
398 if (layers && num_changes < *num_elements)
399 layers[num_changes] = l.first;
400 if (types && num_changes < *num_elements)
401 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
402 ++num_changes;
403 }
404 }
405 if (!layers && !types)
406 *num_elements = num_changes;
407 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500408}
409
410HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500411 uint32_t height,
412 int32_t /*format*/,
413 int32_t dataspace) {
414 supported(__func__);
415 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
416 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
417
418 if (width < min.first || height < min.second)
419 return HWC2::Error::Unsupported;
420
421 if (width > max.first || height > max.second)
422 return HWC2::Error::Unsupported;
423
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200424 if (dataspace != HAL_DATASPACE_UNKNOWN)
Sean Paulac874152016-03-10 16:00:26 -0500425 return HWC2::Error::Unsupported;
426
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200427 // TODO(nobody): Validate format can be handled by either GL or planes
Sean Paulac874152016-03-10 16:00:26 -0500428 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500429}
430
431HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500432 int32_t *modes) {
433 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800434 if (!modes)
435 *num_modes = 1;
436
437 if (modes)
438 *modes = HAL_COLOR_MODE_NATIVE;
439
440 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500441}
442
443HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500444 int32_t attribute_in,
445 int32_t *value) {
446 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400447 auto mode = std::find_if(connector_->modes().begin(),
448 connector_->modes().end(),
449 [config](DrmMode const &m) {
450 return m.id() == config;
451 });
Sean Paulac874152016-03-10 16:00:26 -0500452 if (mode == connector_->modes().end()) {
453 ALOGE("Could not find active mode for %d", config);
454 return HWC2::Error::BadConfig;
455 }
456
457 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 Stratiienkod26619b2021-08-04 19:55:37 +0300463 *value = static_cast<int>(mode->h_display());
Sean Paulac874152016-03-10 16:00:26 -0500464 break;
465 case HWC2::Attribute::Height:
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300466 *value = static_cast<int>(mode->v_display());
Sean Paulac874152016-03-10 16:00:26 -0500467 break;
468 case HWC2::Attribute::VsyncPeriod:
469 // in nanoseconds
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300470 *value = static_cast<int>(1E9 / mode->v_refresh());
Sean Paulac874152016-03-10 16:00:26 -0500471 break;
472 case HWC2::Attribute::DpiX:
473 // Dots per 1000 inches
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300474 *value = mm_width
475 ? static_cast<int>(mode->h_display() * 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 Stratiienkod26619b2021-08-04 19:55:37 +0300480 *value = mm_height ? static_cast<int>(mode->v_display() * kUmPerInch /
481 mm_height)
482 : -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:
486 *value = 0; /* TODO: Add support for config groups */
487 break;
488#endif
Sean Paulac874152016-03-10 16:00:26 -0500489 default:
490 *value = -1;
491 return HWC2::Error::BadConfig;
492 }
493 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500494}
495
496HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
497 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500498 supported(__func__);
499 // Since this callback is normally invoked twice (once to get the count, and
500 // once to populate configs), we don't really want to read the edid
501 // redundantly. Instead, only update the modes on the first invocation. While
502 // it's possible this will result in stale modes, it'll all come out in the
503 // wash when we try to set the active config later.
504 if (!configs) {
505 int ret = connector_->UpdateModes();
506 if (ret) {
507 ALOGE("Failed to update display modes %d", ret);
508 return HWC2::Error::BadDisplay;
509 }
510 }
511
Neil Armstrongb67d0492019-06-20 09:00:21 +0000512 // Since the upper layers only look at vactive/hactive/refresh, height and
513 // width, it doesn't differentiate interlaced from progressive and other
514 // similar modes. Depending on the order of modes we return to SF, it could
515 // end up choosing a suboptimal configuration and dropping the preferred
516 // mode. To workaround this, don't offer interlaced modes to SF if there is
517 // at least one non-interlaced alternative and only offer a single WxH@R
518 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
519
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200520 // TODO(nobody): Remove the following block of code until AOSP handles all
521 // modes
Neil Armstrongb67d0492019-06-20 09:00:21 +0000522 std::vector<DrmMode> sel_modes;
523
524 // Add the preferred mode first to be sure it's not dropped
525 auto mode = std::find_if(connector_->modes().begin(),
526 connector_->modes().end(), [&](DrmMode const &m) {
527 return m.id() ==
528 connector_->get_preferred_mode_id();
529 });
530 if (mode != connector_->modes().end())
531 sel_modes.push_back(*mode);
532
533 // Add the active mode if different from preferred mode
534 if (connector_->active_mode().id() != connector_->get_preferred_mode_id())
535 sel_modes.push_back(connector_->active_mode());
536
537 // Cycle over the modes and filter out "similar" modes, keeping only the
538 // first ones in the order given by DRM (from CEA ids and timings order)
Sean Paulac874152016-03-10 16:00:26 -0500539 for (const DrmMode &mode : connector_->modes()) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200540 // TODO(nobody): Remove this when 3D Attributes are in AOSP
Neil Armstrongb67d0492019-06-20 09:00:21 +0000541 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
542 continue;
543
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200544 // TODO(nobody): Remove this when the Interlaced attribute is in AOSP
Neil Armstrong4c027a72019-06-04 14:48:02 +0000545 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
546 auto m = std::find_if(connector_->modes().begin(),
547 connector_->modes().end(),
548 [&mode](DrmMode const &m) {
549 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
550 m.h_display() == mode.h_display() &&
551 m.v_display() == mode.v_display();
552 });
Neil Armstrongb67d0492019-06-20 09:00:21 +0000553 if (m == connector_->modes().end())
554 sel_modes.push_back(mode);
555
556 continue;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000557 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000558
559 // Search for a similar WxH@R mode in the filtered list and drop it if
560 // another mode with the same WxH@R has already been selected
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200561 // TODO(nobody): Remove this when AOSP handles duplicates modes
Neil Armstrongb67d0492019-06-20 09:00:21 +0000562 auto m = std::find_if(sel_modes.begin(), sel_modes.end(),
563 [&mode](DrmMode const &m) {
564 return m.h_display() == mode.h_display() &&
565 m.v_display() == mode.v_display() &&
566 m.v_refresh() == mode.v_refresh();
567 });
568 if (m == sel_modes.end())
569 sel_modes.push_back(mode);
570 }
571
572 auto num_modes = static_cast<uint32_t>(sel_modes.size());
573 if (!configs) {
574 *num_configs = num_modes;
575 return HWC2::Error::None;
576 }
577
578 uint32_t idx = 0;
579 for (const DrmMode &mode : sel_modes) {
580 if (idx >= *num_configs)
581 break;
582 configs[idx++] = mode.id();
Sean Paulac874152016-03-10 16:00:26 -0500583 }
584 *num_configs = idx;
585 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500586}
587
588HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500589 supported(__func__);
590 std::ostringstream stream;
591 stream << "display-" << connector_->id();
592 std::string string = stream.str();
593 size_t length = string.length();
594 if (!name) {
595 *size = length;
596 return HWC2::Error::None;
597 }
598
599 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
600 strncpy(name, string.c_str(), *size);
601 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500602}
603
Sean Paulac874152016-03-10 16:00:26 -0500604HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
605 uint32_t *num_elements,
606 hwc2_layer_t *layers,
607 int32_t *layer_requests) {
608 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200609 // TODO(nobody): I think virtual display should request
Sean Paulac874152016-03-10 16:00:26 -0500610 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
611 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
612 *num_elements = 0;
613 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500614}
615
616HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500617 supported(__func__);
618 *type = static_cast<int32_t>(type_);
619 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500620}
621
622HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500623 supported(__func__);
624 *support = 0;
625 return HWC2::Error::None;
626}
627
628HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400629 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
630 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500631 supported(__func__);
632 *num_types = 0;
633 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500634}
635
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300636/* Find API details at:
637 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1767
638 */
Sean Pauled2ec4b2016-03-10 15:35:40 -0500639HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500640 hwc2_layer_t *layers,
641 int32_t *fences) {
642 supported(__func__);
643 uint32_t num_layers = 0;
644
645 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
646 ++num_layers;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200647 if (layers == nullptr || fences == nullptr)
Sean Paulac874152016-03-10 16:00:26 -0500648 continue;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200649
650 if (num_layers > *num_elements) {
Sean Paulac874152016-03-10 16:00:26 -0500651 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
652 return HWC2::Error::None;
653 }
654
655 layers[num_layers - 1] = l.first;
Roman Stratiienko0fade372021-02-20 13:59:55 +0200656 fences[num_layers - 1] = l.second.release_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500657 }
658 *num_elements = num_layers;
659 return HWC2::Error::None;
660}
661
Roman Stratiienko0fade372021-02-20 13:59:55 +0200662void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(UniqueFd fd) {
663 if (!fd) {
Sean Paulac874152016-03-10 16:00:26 -0500664 return;
Roman Stratiienko0fade372021-02-20 13:59:55 +0200665 }
Sean Paulac874152016-03-10 16:00:26 -0500666
Roman Stratiienko0fade372021-02-20 13:59:55 +0200667 if (present_fence_) {
668 present_fence_ = UniqueFd(
669 sync_merge("dc_present", present_fence_.Get(), fd.Get()));
Sean Paulac874152016-03-10 16:00:26 -0500670 } else {
Roman Stratiienko0fade372021-02-20 13:59:55 +0200671 present_fence_ = std::move(fd);
Sean Paulac874152016-03-10 16:00:26 -0500672 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500673}
674
Rob Herring4f6c62e2018-05-17 14:33:02 -0500675HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500676 // order the layers by z-order
677 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100678 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500679 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
680 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200681 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500682 case HWC2::Composition::Device:
683 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
684 break;
685 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100686 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500687 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100688 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500689 break;
690 default:
691 continue;
692 }
693 }
694 if (use_client_layer)
695 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
696
Rob Herring4f6c62e2018-05-17 14:33:02 -0500697 if (z_map.empty())
698 return HWC2::Error::BadLayer;
699
Matvii Zorin5368b732021-01-12 10:53:08 +0200700 std::vector<DrmHwcLayer> composition_layers;
701
Sean Paulac874152016-03-10 16:00:26 -0500702 // now that they're ordered by z, add them to the composition
703 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
704 DrmHwcLayer layer;
705 l.second->PopulateDrmLayer(&layer);
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200706 int ret = layer.ImportBuffer(drm_);
Sean Paulac874152016-03-10 16:00:26 -0500707 if (ret) {
708 ALOGE("Failed to import layer, ret=%d", ret);
709 return HWC2::Error::NoResources;
710 }
Matvii Zorin5368b732021-01-12 10:53:08 +0200711 composition_layers.emplace_back(std::move(layer));
Sean Paulac874152016-03-10 16:00:26 -0500712 }
Sean Paulac874152016-03-10 16:00:26 -0500713
Matvii Zorin704ea0e2021-01-08 12:55:45 +0200714 auto composition = std::make_unique<DrmDisplayComposition>(crtc_,
715 planner_.get());
Sean Paulac874152016-03-10 16:00:26 -0500716
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200717 // TODO(nobody): Don't always assume geometry changed
Matvii Zorin5368b732021-01-12 10:53:08 +0200718 int ret = composition->SetLayers(composition_layers.data(),
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300719 composition_layers.size());
Sean Paulac874152016-03-10 16:00:26 -0500720 if (ret) {
721 ALOGE("Failed to set layers in the composition ret=%d", ret);
722 return HWC2::Error::BadLayer;
723 }
724
725 std::vector<DrmPlane *> primary_planes(primary_planes_);
726 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500727 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500728 if (ret) {
John Stultz66763d52021-08-24 04:59:25 +0000729 ALOGV("Failed to plan the composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500730 return HWC2::Error::BadConfig;
731 }
732
733 // Disable the planes we're not using
734 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
735 composition->AddPlaneDisable(*i);
736 i = primary_planes.erase(i);
737 }
738 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
739 composition->AddPlaneDisable(*i);
740 i = overlay_planes.erase(i);
741 }
742
Rob Herring4f6c62e2018-05-17 14:33:02 -0500743 if (test) {
744 ret = compositor_.TestComposition(composition.get());
745 } else {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500746 ret = compositor_.ApplyComposition(std::move(composition));
Matteo Franchinc56eede2019-12-03 17:10:38 +0000747 AddFenceToPresentFence(compositor_.TakeOutFence());
Rob Herring4f6c62e2018-05-17 14:33:02 -0500748 }
Sean Paulac874152016-03-10 16:00:26 -0500749 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700750 if (!test)
751 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500752 return HWC2::Error::BadParameter;
753 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500754 return HWC2::Error::None;
755}
756
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300757/* Find API details at:
758 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1805
759 */
Matteo Franchinc56eede2019-12-03 17:10:38 +0000760HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500761 supported(__func__);
762 HWC2::Error ret;
763
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200764 ++total_stats_.total_frames_;
765
Rob Herring4f6c62e2018-05-17 14:33:02 -0500766 ret = CreateComposition(false);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200767 if (ret != HWC2::Error::None)
768 ++total_stats_.failed_kms_present_;
769
Rob Herring4f6c62e2018-05-17 14:33:02 -0500770 if (ret == HWC2::Error::BadLayer) {
771 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000772 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500773 return HWC2::Error::None;
774 }
775 if (ret != HWC2::Error::None)
776 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500777
Matteo Franchinc56eede2019-12-03 17:10:38 +0000778 *present_fence = present_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500779
780 ++frame_no_;
781 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500782}
783
784HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500785 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400786 auto mode = std::find_if(connector_->modes().begin(),
787 connector_->modes().end(),
788 [config](DrmMode const &m) {
789 return m.id() == config;
790 });
Sean Paulac874152016-03-10 16:00:26 -0500791 if (mode == connector_->modes().end()) {
792 ALOGE("Could not find active mode for %d", config);
793 return HWC2::Error::BadConfig;
794 }
795
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300796 if (!compositor_.SetDisplayMode(*mode)) {
Roman Stratiienko6a10c4c2021-02-15 11:25:23 +0200797 return HWC2::Error::BadConfig;
798 }
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300799 int err = compositor_.ApplyComposition(
800 compositor_.CreateInitializedComposition());
801 if (err != 0) {
802 ALOGE("Failed to queue mode changing commit %d", err);
Sean Paulac874152016-03-10 16:00:26 -0500803 return HWC2::Error::BadConfig;
804 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300805
806 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500807
808 // Setup the client layer's dimensions
809 hwc_rect_t display_frame = {.left = 0,
810 .top = 0,
811 .right = static_cast<int>(mode->h_display()),
812 .bottom = static_cast<int>(mode->v_display())};
813 client_layer_.SetLayerDisplayFrame(display_frame);
Sean Paulac874152016-03-10 16:00:26 -0500814
815 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500816}
817
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +0300818/* Find API details at:
819 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=1861
820 */
Sean Pauled2ec4b2016-03-10 15:35:40 -0500821HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
822 int32_t acquire_fence,
823 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600824 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500825 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500826
827 client_layer_.set_buffer(target);
John Stultz8adf5442021-07-31 00:19:50 +0000828 client_layer_.acquire_fence_ = UniqueFd(acquire_fence);
Sean Paulac874152016-03-10 16:00:26 -0500829 client_layer_.SetLayerDataspace(dataspace);
Roman Stratiienko33365c22020-10-10 23:06:36 +0300830
831 /* TODO: Do not update source_crop every call.
832 * It makes sense to do it once after every hotplug event. */
833 hwc_drm_bo bo{};
834 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
835
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200836 hwc_frect_t source_crop = {.left = 0.0F,
837 .top = 0.0F,
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300838 .right = static_cast<float>(bo.width),
839 .bottom = static_cast<float>(bo.height)};
Roman Stratiienko33365c22020-10-10 23:06:36 +0300840 client_layer_.SetLayerSourceCrop(source_crop);
841
Sean Paulac874152016-03-10 16:00:26 -0500842 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500843}
844
845HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500846 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800847
Roman Stratiienkod146d6d2020-10-04 23:56:46 +0300848 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
849 return HWC2::Error::BadParameter;
850
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800851 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +0300852 return HWC2::Error::Unsupported;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800853
854 color_mode_ = mode;
855 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500856}
857
858HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500859 int32_t hint) {
860 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200861 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
862 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
863 return HWC2::Error::BadParameter;
864
865 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
866 return HWC2::Error::BadParameter;
867
868 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
869 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
870 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
871
872 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500873}
874
875HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500876 int32_t release_fence) {
877 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200878 // TODO(nobody): Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500879 return unsupported(__func__, buffer, release_fence);
880}
881
Sean Paulac874152016-03-10 16:00:26 -0500882HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
883 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500884 auto mode = static_cast<HWC2::PowerMode>(mode_in);
885 switch (mode) {
886 case HWC2::PowerMode::Off:
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300887 compositor_.SetDisplayActive(false);
Sean Paulac874152016-03-10 16:00:26 -0500888 break;
889 case HWC2::PowerMode::On:
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300890 compositor_.SetDisplayActive(true);
Sean Paulac874152016-03-10 16:00:26 -0500891 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100892 case HWC2::PowerMode::Doze:
893 case HWC2::PowerMode::DozeSuspend:
894 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500895 default:
896 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100897 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500898 };
899
Roman Stratiienko36a7f282021-10-05 18:17:53 +0300900 int ret = compositor_.ApplyComposition(
901 compositor_.CreateInitializedComposition());
Sean Paulac874152016-03-10 16:00:26 -0500902 if (ret) {
903 ALOGE("Failed to apply the dpms composition ret=%d", ret);
904 return HWC2::Error::BadParameter;
905 }
906 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500907}
908
909HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500910 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300911 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500912 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500913}
914
915HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500916 uint32_t *num_requests) {
917 supported(__func__);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500918
Matvii Zorinef3c7972020-08-11 15:15:44 +0300919 return backend_->ValidateDisplay(this, num_types, num_requests);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500920}
921
Matvii Zorined90ef92021-01-29 18:32:06 +0200922std::vector<DrmHwcTwo::HwcLayer *>
923DrmHwcTwo::HwcDisplay::GetOrderLayersByZPos() {
924 std::vector<DrmHwcTwo::HwcLayer *> ordered_layers;
925 ordered_layers.reserve(layers_.size());
926
927 for (auto &[handle, layer] : layers_) {
928 ordered_layers.emplace_back(&layer);
929 }
930
931 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
932 [](const DrmHwcTwo::HwcLayer *lhs, const DrmHwcTwo::HwcLayer *rhs) {
933 return lhs->z_order() < rhs->z_order();
934 });
935
936 return ordered_layers;
937}
938
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300939#if PLATFORM_SDK_VERSION > 29
940HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
941 if (connector_->internal())
942 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
943 else if (connector_->external())
944 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
945 else
946 return HWC2::Error::BadConfig;
947
948 return HWC2::Error::None;
949}
950
951HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayVsyncPeriod(
952 hwc2_vsync_period_t *outVsyncPeriod /* ns */) {
953 supported(__func__);
954 DrmMode const &mode = connector_->active_mode();
955 if (mode.id() == 0)
956 return HWC2::Error::BadConfig;
957
Roman Stratiienkod26619b2021-08-04 19:55:37 +0300958 *outVsyncPeriod = static_cast<hwc2_vsync_period_t>(1E9 / mode.v_refresh());
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300959 return HWC2::Error::None;
960}
961
962HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfigWithConstraints(
963 hwc2_config_t /*config*/,
964 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
965 hwc_vsync_period_change_timeline_t *outTimeline) {
966 supported(__func__);
967
968 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
969 return HWC2::Error::BadParameter;
970 }
971
972 return HWC2::Error::BadConfig;
973}
974
975HWC2::Error DrmHwcTwo::HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
976 return HWC2::Error::Unsupported;
977}
978
979HWC2::Error DrmHwcTwo::HwcDisplay::GetSupportedContentTypes(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200980 uint32_t *outNumSupportedContentTypes,
981 const uint32_t *outSupportedContentTypes) {
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300982 if (outSupportedContentTypes == nullptr)
983 *outNumSupportedContentTypes = 0;
984
985 return HWC2::Error::None;
986}
987
988HWC2::Error DrmHwcTwo::HwcDisplay::SetContentType(int32_t contentType) {
989 supported(__func__);
990
991 if (contentType != HWC2_CONTENT_TYPE_NONE)
992 return HWC2::Error::Unsupported;
993
994 /* TODO: Map to the DRM Connector property:
995 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
996 */
997
998 return HWC2::Error::None;
999}
1000#endif
1001
John Stultz8c7229d2020-02-07 21:31:08 +00001002#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001003HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
1004 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
1005 supported(__func__);
1006
Roman Stratiienko3e8ce572021-09-29 12:46:28 +03001007 auto blob = connector_->GetEdidBlob();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001008
Roman Stratiienko3e8ce572021-09-29 12:46:28 +03001009 if (!blob) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001010 ALOGE("Failed to get edid property value.");
1011 return HWC2::Error::Unsupported;
1012 }
1013
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +03001014 if (outData) {
1015 *outDataSize = std::min(*outDataSize, blob->length);
1016 memcpy(outData, blob->data, *outDataSize);
1017 } else {
1018 *outDataSize = blob->length;
1019 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001020 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001021
1022 return HWC2::Error::None;
1023}
1024
1025HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
1026 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
1027 unsupported(__func__, outCapabilities);
1028
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001029 if (outNumCapabilities == nullptr) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001030 return HWC2::Error::BadParameter;
1031 }
1032
1033 *outNumCapabilities = 0;
1034
1035 return HWC2::Error::None;
1036}
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001037
1038HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
1039 bool *supported) {
1040 *supported = false;
1041 return HWC2::Error::None;
1042}
1043
1044HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
1045 float /* brightness */) {
1046 return HWC2::Error::Unsupported;
1047}
1048
John Stultz8c7229d2020-02-07 21:31:08 +00001049#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001050
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001051#if PLATFORM_SDK_VERSION > 27
1052
1053HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
1054 int32_t mode, uint32_t *outNumIntents,
1055 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1056 if (mode != HAL_COLOR_MODE_NATIVE) {
1057 return HWC2::Error::BadParameter;
1058 }
1059
1060 if (outIntents == nullptr) {
1061 *outNumIntents = 1;
1062 return HWC2::Error::None;
1063 }
1064 *outNumIntents = 1;
1065 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1066 return HWC2::Error::None;
1067}
1068
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001069HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
1070 int32_t intent) {
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001071 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1072 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1073 return HWC2::Error::BadParameter;
1074
1075 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
1076 return HWC2::Error::BadParameter;
1077
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001078 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001079 return HWC2::Error::Unsupported;
1080
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001081 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001082 return HWC2::Error::Unsupported;
1083
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001084 color_mode_ = mode;
1085 return HWC2::Error::None;
1086}
1087
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001088#endif /* PLATFORM_SDK_VERSION > 27 */
1089
Roman Stratiienkoaaaa4692021-09-29 12:50:10 +03001090HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t /*x*/,
1091 int32_t /*y*/) {
Sean Paulac874152016-03-10 16:00:26 -05001092 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001093 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001094}
1095
1096HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001097 supported(__func__);
Roman Stratiienko5063d532021-09-29 12:47:31 +03001098 switch (static_cast<HWC2::BlendMode>(mode)) {
1099 case HWC2::BlendMode::None:
1100 blending_ = DrmHwcBlending::kNone;
1101 break;
1102 case HWC2::BlendMode::Premultiplied:
1103 blending_ = DrmHwcBlending::kPreMult;
1104 break;
1105 case HWC2::BlendMode::Coverage:
1106 blending_ = DrmHwcBlending::kCoverage;
1107 break;
1108 default:
1109 ALOGE("Unknown blending mode b=%d", blending_);
1110 blending_ = DrmHwcBlending::kNone;
1111 break;
1112 }
Sean Paulac874152016-03-10 16:00:26 -05001113 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001114}
1115
Roman Stratiienko3e2f9e72021-05-21 14:33:28 +03001116/* Find API details at:
1117 * https://cs.android.com/android/platform/superproject/+/android-11.0.0_r3:hardware/libhardware/include/hardware/hwcomposer2.h;l=2314
1118 */
Sean Pauled2ec4b2016-03-10 15:35:40 -05001119HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001120 int32_t acquire_fence) {
1121 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -05001122
Sean Paulac874152016-03-10 16:00:26 -05001123 set_buffer(buffer);
John Stultz8adf5442021-07-31 00:19:50 +00001124 acquire_fence_ = UniqueFd(acquire_fence);
Sean Paulac874152016-03-10 16:00:26 -05001125 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001126}
1127
Roman Stratiienkoaaaa4692021-09-29 12:50:10 +03001128HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t /*color*/) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001129 // TODO(nobody): Put to client composition here?
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001130 supported(__func__);
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001131 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001132}
1133
1134HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001135 sf_type_ = static_cast<HWC2::Composition>(type);
1136 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001137}
1138
1139HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001140 supported(__func__);
Roman Stratiienko515da8f2021-09-29 12:47:21 +03001141 switch (dataspace & HAL_DATASPACE_STANDARD_MASK) {
1142 case HAL_DATASPACE_STANDARD_BT709:
1143 color_space_ = DrmHwcColorSpace::kItuRec709;
1144 break;
1145 case HAL_DATASPACE_STANDARD_BT601_625:
1146 case HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED:
1147 case HAL_DATASPACE_STANDARD_BT601_525:
1148 case HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED:
1149 color_space_ = DrmHwcColorSpace::kItuRec601;
1150 break;
1151 case HAL_DATASPACE_STANDARD_BT2020:
1152 case HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE:
1153 color_space_ = DrmHwcColorSpace::kItuRec2020;
1154 break;
1155 default:
1156 color_space_ = DrmHwcColorSpace::kUndefined;
1157 }
1158
1159 switch (dataspace & HAL_DATASPACE_RANGE_MASK) {
1160 case HAL_DATASPACE_RANGE_FULL:
1161 sample_range_ = DrmHwcSampleRange::kFullRange;
1162 break;
1163 case HAL_DATASPACE_RANGE_LIMITED:
1164 sample_range_ = DrmHwcSampleRange::kLimitedRange;
1165 break;
1166 default:
1167 sample_range_ = DrmHwcSampleRange::kUndefined;
1168 }
Sean Paulac874152016-03-10 16:00:26 -05001169 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001170}
1171
1172HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001173 supported(__func__);
1174 display_frame_ = frame;
1175 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001176}
1177
1178HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001179 supported(__func__);
1180 alpha_ = alpha;
1181 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001182}
1183
1184HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1185 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001186 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001187 // TODO(nobody): We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001188 return unsupported(__func__, stream);
1189}
1190
1191HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001192 supported(__func__);
1193 source_crop_ = crop;
1194 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001195}
1196
1197HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001198 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001199 // TODO(nobody): We don't use surface damage, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001200 unsupported(__func__, damage);
1201 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001202}
1203
1204HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001205 supported(__func__);
Roman Stratiienko2ed4cbe2021-09-29 12:47:35 +03001206
1207 uint32_t l_transform = 0;
1208
1209 // 270* and 180* cannot be combined with flips. More specifically, they
1210 // already contain both horizontal and vertical flips, so those fields are
1211 // redundant in this case. 90* rotation can be combined with either horizontal
1212 // flip or vertical flip, so treat it differently
1213 if (transform == HWC_TRANSFORM_ROT_270) {
1214 l_transform = DrmHwcTransform::kRotate270;
1215 } else if (transform == HWC_TRANSFORM_ROT_180) {
1216 l_transform = DrmHwcTransform::kRotate180;
1217 } else {
1218 if (transform & HWC_TRANSFORM_FLIP_H)
1219 l_transform |= DrmHwcTransform::kFlipH;
1220 if (transform & HWC_TRANSFORM_FLIP_V)
1221 l_transform |= DrmHwcTransform::kFlipV;
1222 if (transform & HWC_TRANSFORM_ROT_90)
1223 l_transform |= DrmHwcTransform::kRotate90;
1224 }
1225
1226 transform_ = static_cast<DrmHwcTransform>(l_transform);
Sean Paulac874152016-03-10 16:00:26 -05001227 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001228}
1229
1230HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001231 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001232 // TODO(nobody): We don't use this information, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001233 unsupported(__func__, visible);
1234 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001235}
1236
Sean Paulac874152016-03-10 16:00:26 -05001237HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1238 supported(__func__);
1239 z_order_ = order;
1240 return HWC2::Error::None;
1241}
1242
1243void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1244 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -05001245 layer->sf_handle = buffer_;
Roman Stratiienko0fade372021-02-20 13:59:55 +02001246 // TODO(rsglobal): Avoid extra fd duplication
1247 layer->acquire_fence = UniqueFd(fcntl(acquire_fence_.Get(), F_DUPFD_CLOEXEC));
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001248 layer->display_frame = display_frame_;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001249 layer->alpha = lround(65535.0F * alpha_);
Roman Stratiienko5063d532021-09-29 12:47:31 +03001250 layer->blending = blending_;
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001251 layer->source_crop = source_crop_;
Roman Stratiienko2ed4cbe2021-09-29 12:47:35 +03001252 layer->transform = transform_;
Roman Stratiienko515da8f2021-09-29 12:47:21 +03001253 layer->color_space = color_space_;
1254 layer->sample_range = sample_range_;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001255}
1256
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001257void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
Roman Stratiienko863a3c22021-09-29 13:00:29 +03001258 const std::lock_guard<std::mutex> lock(callback_lock_);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001259
Roman Stratiienko863a3c22021-09-29 13:00:29 +03001260 if (hotplug_callback_.first != nullptr &&
1261 hotplug_callback_.second != nullptr) {
1262 hotplug_callback_.first(hotplug_callback_.second, displayid,
1263 state == DRM_MODE_CONNECTED
1264 ? HWC2_CONNECTION_CONNECTED
1265 : HWC2_CONNECTION_DISCONNECTED);
1266 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001267}
1268
1269void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
Roman Stratiienkod21071f2021-03-09 21:56:50 +02001270 for (const auto &conn : drmDevice->connectors()) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001271 if (conn->state() != DRM_MODE_CONNECTED)
1272 continue;
1273 HandleDisplayHotplug(conn->display(), conn->state());
1274 }
1275}
1276
1277void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
Roman Stratiienkod21071f2021-03-09 21:56:50 +02001278 for (const auto &conn : drm_->connectors()) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001279 drmModeConnection old_state = conn->state();
1280 drmModeConnection cur_state = conn->UpdateModes()
1281 ? DRM_MODE_UNKNOWNCONNECTION
1282 : conn->state();
1283
1284 if (cur_state == old_state)
1285 continue;
1286
1287 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1288 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1289 conn->id(), conn->display());
1290
1291 int display_id = conn->display();
1292 if (cur_state == DRM_MODE_CONNECTED) {
1293 auto &display = hwc2_->displays_.at(display_id);
1294 display.ChosePreferredConfig();
1295 } else {
1296 auto &display = hwc2_->displays_.at(display_id);
1297 display.ClearDisplay();
1298 }
1299
1300 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1301 }
1302}
1303
Sean Pauled2ec4b2016-03-10 15:35:40 -05001304// static
1305int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1306 unsupported(__func__);
1307 return 0;
1308}
1309
1310// static
1311void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001312 uint32_t *out_count,
1313 int32_t * /*out_capabilities*/) {
1314 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001315 *out_count = 0;
1316}
1317
1318// static
Sean Paulac874152016-03-10 16:00:26 -05001319hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1320 struct hwc2_device * /*dev*/, int32_t descriptor) {
1321 supported(__func__);
1322 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001323 switch (func) {
1324 // Device functions
1325 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1326 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1327 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1328 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001329 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001330 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1331 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1332 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1333 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1334 case HWC2::FunctionDescriptor::Dump:
1335 return ToHook<HWC2_PFN_DUMP>(
1336 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1337 uint32_t *, char *>);
1338 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1339 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1340 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1341 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1342 case HWC2::FunctionDescriptor::RegisterCallback:
1343 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1344 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1345 &DrmHwcTwo::RegisterCallback, int32_t,
1346 hwc2_callback_data_t, hwc2_function_pointer_t>);
1347
1348 // Display functions
1349 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1350 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1351 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1352 &HwcDisplay::AcceptDisplayChanges>);
1353 case HWC2::FunctionDescriptor::CreateLayer:
1354 return ToHook<HWC2_PFN_CREATE_LAYER>(
1355 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1356 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1357 case HWC2::FunctionDescriptor::DestroyLayer:
1358 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1359 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1360 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1361 case HWC2::FunctionDescriptor::GetActiveConfig:
1362 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1363 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1364 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1365 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1366 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1367 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1368 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1369 hwc2_layer_t *, int32_t *>);
1370 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1371 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1372 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1373 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1374 int32_t, int32_t>);
1375 case HWC2::FunctionDescriptor::GetColorModes:
1376 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1377 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1378 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1379 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001380 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1381 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1382 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1383 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001384 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001385 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1386 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1387 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1388 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001389 case HWC2::FunctionDescriptor::GetDisplayName:
1390 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1391 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1392 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1393 case HWC2::FunctionDescriptor::GetDisplayRequests:
1394 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1395 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1396 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1397 hwc2_layer_t *, int32_t *>);
1398 case HWC2::FunctionDescriptor::GetDisplayType:
1399 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1400 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1401 &HwcDisplay::GetDisplayType, int32_t *>);
1402 case HWC2::FunctionDescriptor::GetDozeSupport:
1403 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1404 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1405 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001406 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1407 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1408 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1409 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1410 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001411 case HWC2::FunctionDescriptor::GetReleaseFences:
1412 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1413 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1414 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1415 int32_t *>);
1416 case HWC2::FunctionDescriptor::PresentDisplay:
1417 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1418 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1419 &HwcDisplay::PresentDisplay, int32_t *>);
1420 case HWC2::FunctionDescriptor::SetActiveConfig:
1421 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1422 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1423 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1424 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001425 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1426 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1427 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1428 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001429 case HWC2::FunctionDescriptor::SetColorMode:
1430 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1431 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1432 &HwcDisplay::SetColorMode, int32_t>);
1433 case HWC2::FunctionDescriptor::SetColorTransform:
1434 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1435 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1436 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1437 case HWC2::FunctionDescriptor::SetOutputBuffer:
1438 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1439 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1440 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1441 case HWC2::FunctionDescriptor::SetPowerMode:
1442 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1443 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1444 &HwcDisplay::SetPowerMode, int32_t>);
1445 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1446 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1447 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1448 &HwcDisplay::SetVsyncEnabled, int32_t>);
1449 case HWC2::FunctionDescriptor::ValidateDisplay:
1450 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1451 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1452 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001453#if PLATFORM_SDK_VERSION > 27
1454 case HWC2::FunctionDescriptor::GetRenderIntents:
1455 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1456 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1457 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1458 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001459 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1460 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1461 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1462 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001463#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001464#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001465 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1466 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1467 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1468 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1469 uint32_t *, uint8_t *>);
1470 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1471 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1472 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1473 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1474 uint32_t *>);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001475 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1476 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1477 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1478 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1479 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1480 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1481 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1482 &HwcDisplay::SetDisplayBrightness, float>);
John Stultz8c7229d2020-02-07 21:31:08 +00001483#endif /* PLATFORM_SDK_VERSION > 28 */
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001484#if PLATFORM_SDK_VERSION > 29
1485 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
1486 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
1487 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
1488 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
1489 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
1490 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
1491 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
1492 &HwcDisplay::GetDisplayVsyncPeriod,
1493 hwc2_vsync_period_t *>);
1494 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
1495 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
1496 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
1497 &HwcDisplay::SetActiveConfigWithConstraints,
1498 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
1499 hwc_vsync_period_change_timeline_t *>);
1500 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
1501 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
1502 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
1503 &HwcDisplay::SetAutoLowLatencyMode, bool>);
1504 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
1505 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
1506 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
1507 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
1508 uint32_t *>);
1509 case HWC2::FunctionDescriptor::SetContentType:
1510 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
1511 DisplayHook<decltype(&HwcDisplay::SetContentType),
1512 &HwcDisplay::SetContentType, int32_t>);
1513#endif
Sean Pauled2ec4b2016-03-10 15:35:40 -05001514 // Layer functions
1515 case HWC2::FunctionDescriptor::SetCursorPosition:
1516 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1517 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1518 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1519 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1520 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1521 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1522 &HwcLayer::SetLayerBlendMode, int32_t>);
1523 case HWC2::FunctionDescriptor::SetLayerBuffer:
1524 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1525 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1526 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1527 case HWC2::FunctionDescriptor::SetLayerColor:
1528 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1529 LayerHook<decltype(&HwcLayer::SetLayerColor),
1530 &HwcLayer::SetLayerColor, hwc_color_t>);
1531 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1532 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1533 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1534 &HwcLayer::SetLayerCompositionType, int32_t>);
1535 case HWC2::FunctionDescriptor::SetLayerDataspace:
1536 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1537 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1538 &HwcLayer::SetLayerDataspace, int32_t>);
1539 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1540 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1541 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1542 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1543 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1544 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1545 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1546 &HwcLayer::SetLayerPlaneAlpha, float>);
1547 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001548 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1549 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1550 &HwcLayer::SetLayerSidebandStream,
1551 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001552 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1553 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1554 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1555 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1556 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1557 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1558 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1559 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1560 case HWC2::FunctionDescriptor::SetLayerTransform:
1561 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1562 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1563 &HwcLayer::SetLayerTransform, int32_t>);
1564 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1565 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1566 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1567 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1568 case HWC2::FunctionDescriptor::SetLayerZOrder:
1569 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1570 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1571 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001572 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001573 default:
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001574 return nullptr;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001575 }
1576}
Sean Paulac874152016-03-10 16:00:26 -05001577
1578// static
1579int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1580 struct hw_device_t **dev) {
1581 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001582 if (strcmp(name, HWC_HARDWARE_COMPOSER) != 0) {
Sean Paulac874152016-03-10 16:00:26 -05001583 ALOGE("Invalid module name- %s", name);
1584 return -EINVAL;
1585 }
1586
1587 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1588 if (!ctx) {
1589 ALOGE("Failed to allocate DrmHwcTwo");
1590 return -ENOMEM;
1591 }
1592
1593 HWC2::Error err = ctx->Init();
1594 if (err != HWC2::Error::None) {
1595 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1596 return -EINVAL;
1597 }
1598
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001599 ctx->common.module = (hw_module_t *)module;
Sean Paulac874152016-03-10 16:00:26 -05001600 *dev = &ctx->common;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001601 ctx.release(); // NOLINT(bugprone-unused-return-value)
Sean Paulac874152016-03-10 16:00:26 -05001602 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001603}
Sean Paulf72cccd2018-08-27 13:59:08 -04001604} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001605
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001606// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001607static struct hw_module_methods_t hwc2_module_methods = {
1608 .open = android::DrmHwcTwo::HookDevOpen,
1609};
1610
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001611// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001612hw_module_t HAL_MODULE_INFO_SYM = {
1613 .tag = HARDWARE_MODULE_TAG,
1614 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1615 .id = HWC_HARDWARE_MODULE_ID,
1616 .name = "DrmHwcTwo module",
1617 .author = "The Android Open Source Project",
1618 .methods = &hwc2_module_methods,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001619 .dso = nullptr,
Sean Paulac874152016-03-10 16:00:26 -05001620 .reserved = {0},
1621};