blob: 4a7a4164d1cc5bf741b0fe8d7e5915387ca92196 [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
Sean Paulac874152016-03-10 16:00:26 -050022#include <hardware/hardware.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050023#include <hardware/hwcomposer2.h>
Roman Stratiienko74774712021-02-05 16:32:47 +020024#include <sync/sync.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050025
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020026#include <cinttypes>
Roman Stratiienkod21071f2021-03-09 21:56:50 +020027#include <iostream>
28#include <sstream>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030029#include <string>
30
Roman Stratiienko13cc3662020-08-29 21:35:39 +030031#include "backend/BackendManager.h"
Roman Stratiienko33365c22020-10-10 23:06:36 +030032#include "bufferinfo/BufferInfoGetter.h"
Roman Stratiienko13cc3662020-08-29 21:35:39 +030033#include "compositor/DrmDisplayComposition.h"
Roman Stratiienkod21071f2021-03-09 21:56:50 +020034#include "utils/log.h"
35#include "utils/properties.h"
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030036
Sean Pauled2ec4b2016-03-10 15:35:40 -050037namespace android {
38
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020039DrmHwcTwo::DrmHwcTwo() : hwc2_device() {
Sean Paulac874152016-03-10 16:00:26 -050040 common.tag = HARDWARE_DEVICE_TAG;
41 common.version = HWC_DEVICE_API_VERSION_2_0;
Sean Pauled2ec4b2016-03-10 15:35:40 -050042 common.close = HookDevClose;
43 getCapabilities = HookDevGetCapabilities;
44 getFunction = HookDevGetFunction;
45}
46
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030047HWC2::Error DrmHwcTwo::CreateDisplay(hwc2_display_t displ,
48 HWC2::DisplayType type) {
49 DrmDevice *drm = resource_manager_.GetDrmDevice(displ);
Roman Stratiienko8666dc92021-02-09 17:49:55 +020050 if (!drm) {
51 ALOGE("Failed to get a valid drmresource");
Sean Paulac874152016-03-10 16:00:26 -050052 return HWC2::Error::NoResources;
53 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030054 displays_.emplace(std::piecewise_construct, std::forward_as_tuple(displ),
Roman Stratiienko8666dc92021-02-09 17:49:55 +020055 std::forward_as_tuple(&resource_manager_, drm, displ,
56 type));
Sean Paulac874152016-03-10 16:00:26 -050057
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030058 DrmCrtc *crtc = drm->GetCrtcForDisplay(static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050059 if (!crtc) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030060 ALOGE("Failed to get crtc for display %d", static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050061 return HWC2::Error::BadDisplay;
62 }
Roman Stratiienkod21071f2021-03-09 21:56:50 +020063 auto display_planes = std::vector<DrmPlane *>();
64 for (const auto &plane : drm->planes()) {
Sean Paulac874152016-03-10 16:00:26 -050065 if (plane->GetCrtcSupported(*crtc))
66 display_planes.push_back(plane.get());
67 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030068 displays_.at(displ).Init(&display_planes);
Sean Paulac874152016-03-10 16:00:26 -050069 return HWC2::Error::None;
70}
71
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030072HWC2::Error DrmHwcTwo::Init() {
73 int rv = resource_manager_.Init();
74 if (rv) {
75 ALOGE("Can't initialize the resource manager %d", rv);
76 return HWC2::Error::NoResources;
77 }
78
79 HWC2::Error ret = HWC2::Error::None;
80 for (int i = 0; i < resource_manager_.getDisplayCount(); i++) {
81 ret = CreateDisplay(i, HWC2::DisplayType::Physical);
82 if (ret != HWC2::Error::None) {
83 ALOGE("Failed to create display %d with error %d", i, ret);
84 return ret;
85 }
86 }
87
Roman Stratiienkod21071f2021-03-09 21:56:50 +020088 const auto &drm_devices = resource_manager_.getDrmDevices();
89 for (const auto &device : drm_devices) {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020090 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030091 device->RegisterHotplugHandler(new DrmHotplugHandler(this, device.get()));
92 }
93 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 Stratiienkod21071f2021-03-09 21:56:50 +0200141 std::stringstream ss;
142 ss << "- Display on: " << connector_->name() << "\n"
143 << " Flattening state: " << compositor_.GetFlatteningState() << "\n"
144 << "Statistics since system boot:\n"
145 << DumpDelta(total_stats_) << "\n\n"
146 << "Statistics since last dumpsys request:\n"
147 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200148
149 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200150 return ss.str();
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200151}
152
153void DrmHwcTwo::Dump(uint32_t *outSize, char *outBuffer) {
154 supported(__func__);
155
156 if (outBuffer != nullptr) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200157 auto copied_bytes = mDumpString.copy(outBuffer, *outSize);
158 *outSize = static_cast<uint32_t>(copied_bytes);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200159 return;
160 }
161
162 std::stringstream output;
163
164 output << "-- drm_hwcomposer --\n\n";
165
166 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &dp : displays_)
167 output << dp.second.Dump();
168
169 mDumpString = output.str();
170 *outSize = static_cast<uint32_t>(mDumpString.size());
Sean Pauled2ec4b2016-03-10 15:35:40 -0500171}
172
173uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200174 // TODO(nobody): Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500175 unsupported(__func__);
176 return 0;
177}
178
179HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500180 hwc2_callback_data_t data,
181 hwc2_function_pointer_t function) {
182 supported(__func__);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300183
Roman Stratiienko23701092020-09-26 02:08:41 +0300184 switch (static_cast<HWC2::Callback>(descriptor)) {
Sean Paulac874152016-03-10 16:00:26 -0500185 case HWC2::Callback::Hotplug: {
Roman Stratiienko23701092020-09-26 02:08:41 +0300186 SetHotplugCallback(data, function);
Roman Stratiienkod21071f2021-03-09 21:56:50 +0200187 const auto &drm_devices = resource_manager_.getDrmDevices();
188 for (const auto &device : drm_devices)
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300189 HandleInitialHotplugState(device.get());
Sean Paulac874152016-03-10 16:00:26 -0500190 break;
191 }
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200192 case HWC2::Callback::Refresh: {
193 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
194 displays_)
195 d.second.RegisterRefreshCallback(data, function);
196 break;
197 }
Sean Paulac874152016-03-10 16:00:26 -0500198 case HWC2::Callback::Vsync: {
199 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
200 displays_)
201 d.second.RegisterVsyncCallback(data, function);
202 break;
203 }
204 default:
205 break;
206 }
207 return HWC2::Error::None;
208}
209
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100210DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200211 DrmDevice *drm, hwc2_display_t handle,
212 HWC2::DisplayType type)
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100213 : resource_manager_(resource_manager),
214 drm_(drm),
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100215 handle_(handle),
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200216 type_(type),
217 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
Sean Paulac874152016-03-10 16:00:26 -0500218 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200219
220 // clang-format off
221 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
222 0.0, 1.0, 0.0, 0.0,
223 0.0, 0.0, 1.0, 0.0,
224 0.0, 0.0, 0.0, 1.0};
225 // clang-format on
Sean Paulac874152016-03-10 16:00:26 -0500226}
227
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300228void DrmHwcTwo::HwcDisplay::ClearDisplay() {
229 compositor_.ClearDisplay();
230}
231
Sean Paulac874152016-03-10 16:00:26 -0500232HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
233 supported(__func__);
234 planner_ = Planner::CreateInstance(drm_);
235 if (!planner_) {
236 ALOGE("Failed to create planner instance for composition");
237 return HWC2::Error::NoResources;
238 }
239
240 int display = static_cast<int>(handle_);
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100241 int ret = compositor_.Init(resource_manager_, display);
Sean Paulac874152016-03-10 16:00:26 -0500242 if (ret) {
243 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
244 return HWC2::Error::NoResources;
245 }
246
247 // Split up the given display planes into primary and overlay to properly
248 // interface with the composition
249 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -0700250 property_get("vendor.hwc.drm.use_overlay_planes", use_overlay_planes_prop,
251 "1");
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200252 bool use_overlay_planes = strtol(use_overlay_planes_prop, nullptr, 10);
Sean Paulac874152016-03-10 16:00:26 -0500253 for (auto &plane : *planes) {
254 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
255 primary_planes_.push_back(plane);
256 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
257 overlay_planes_.push_back(plane);
258 }
259
260 crtc_ = drm_->GetCrtcForDisplay(display);
261 if (!crtc_) {
262 ALOGE("Failed to get crtc for display %d", display);
263 return HWC2::Error::BadDisplay;
264 }
265
266 connector_ = drm_->GetConnectorForDisplay(display);
267 if (!connector_) {
268 ALOGE("Failed to get connector for display %d", display);
269 return HWC2::Error::BadDisplay;
270 }
271
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300272 ret = vsync_worker_.Init(drm_, display);
273 if (ret) {
274 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
275 return HWC2::Error::BadDisplay;
276 }
277
Matvii Zorinef3c7972020-08-11 15:15:44 +0300278 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
279 if (ret) {
280 ALOGE("Failed to set backend for d=%d %d\n", display, ret);
281 return HWC2::Error::BadDisplay;
282 }
283
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300284 return ChosePreferredConfig();
285}
286
287HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() {
Sean Paulac874152016-03-10 16:00:26 -0500288 // Fetch the number of modes from the display
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200289 uint32_t num_configs = 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200290 HWC2::Error err = GetDisplayConfigs(&num_configs, nullptr);
Sean Paulac874152016-03-10 16:00:26 -0500291 if (err != HWC2::Error::None || !num_configs)
292 return err;
293
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200294 return SetActiveConfig(connector_->get_preferred_mode_id());
Sean Paulac874152016-03-10 16:00:26 -0500295}
296
Roman Stratiienko23701092020-09-26 02:08:41 +0300297void DrmHwcTwo::HwcDisplay::RegisterVsyncCallback(
Sean Paulac874152016-03-10 16:00:26 -0500298 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
299 supported(__func__);
Roman Stratiienko23701092020-09-26 02:08:41 +0300300 vsync_worker_.RegisterClientCallback(data, func);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500301}
302
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200303void DrmHwcTwo::HwcDisplay::RegisterRefreshCallback(
304 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
305 supported(__func__);
Roman Stratiienko23701092020-09-26 02:08:41 +0300306 compositor_.SetRefreshCallback(data, func);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200307}
308
Sean Pauled2ec4b2016-03-10 15:35:40 -0500309HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500310 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500311 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
312 l.second.accept_type_change();
313 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500314}
315
316HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500317 supported(__func__);
318 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
319 *layer = static_cast<hwc2_layer_t>(layer_idx_);
320 ++layer_idx_;
321 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500322}
323
324HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500325 supported(__func__);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100326 if (!get_layer(layer))
327 return HWC2::Error::BadLayer;
328
Sean Paulac874152016-03-10 16:00:26 -0500329 layers_.erase(layer);
330 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500331}
332
333HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500334 supported(__func__);
335 DrmMode const &mode = connector_->active_mode();
336 if (mode.id() == 0)
337 return HWC2::Error::BadConfig;
338
339 *config = mode.id();
340 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500341}
342
343HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
344 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500345 supported(__func__);
346 uint32_t num_changes = 0;
347 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
348 if (l.second.type_changed()) {
349 if (layers && num_changes < *num_elements)
350 layers[num_changes] = l.first;
351 if (types && num_changes < *num_elements)
352 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
353 ++num_changes;
354 }
355 }
356 if (!layers && !types)
357 *num_elements = num_changes;
358 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500359}
360
361HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500362 uint32_t height,
363 int32_t /*format*/,
364 int32_t dataspace) {
365 supported(__func__);
366 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
367 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
368
369 if (width < min.first || height < min.second)
370 return HWC2::Error::Unsupported;
371
372 if (width > max.first || height > max.second)
373 return HWC2::Error::Unsupported;
374
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200375 if (dataspace != HAL_DATASPACE_UNKNOWN)
Sean Paulac874152016-03-10 16:00:26 -0500376 return HWC2::Error::Unsupported;
377
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200378 // TODO(nobody): Validate format can be handled by either GL or planes
Sean Paulac874152016-03-10 16:00:26 -0500379 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500380}
381
382HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500383 int32_t *modes) {
384 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800385 if (!modes)
386 *num_modes = 1;
387
388 if (modes)
389 *modes = HAL_COLOR_MODE_NATIVE;
390
391 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500392}
393
394HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500395 int32_t attribute_in,
396 int32_t *value) {
397 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400398 auto mode = std::find_if(connector_->modes().begin(),
399 connector_->modes().end(),
400 [config](DrmMode const &m) {
401 return m.id() == config;
402 });
Sean Paulac874152016-03-10 16:00:26 -0500403 if (mode == connector_->modes().end()) {
404 ALOGE("Could not find active mode for %d", config);
405 return HWC2::Error::BadConfig;
406 }
407
408 static const int32_t kUmPerInch = 25400;
409 uint32_t mm_width = connector_->mm_width();
410 uint32_t mm_height = connector_->mm_height();
411 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
412 switch (attribute) {
413 case HWC2::Attribute::Width:
414 *value = mode->h_display();
415 break;
416 case HWC2::Attribute::Height:
417 *value = mode->v_display();
418 break;
419 case HWC2::Attribute::VsyncPeriod:
420 // in nanoseconds
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200421 *value = 1000.0 * 1000.0 * 1000.0 / mode->v_refresh();
Sean Paulac874152016-03-10 16:00:26 -0500422 break;
423 case HWC2::Attribute::DpiX:
424 // Dots per 1000 inches
425 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
426 break;
427 case HWC2::Attribute::DpiY:
428 // Dots per 1000 inches
429 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
430 break;
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300431#if PLATFORM_SDK_VERSION > 29
432 case HWC2::Attribute::ConfigGroup:
433 *value = 0; /* TODO: Add support for config groups */
434 break;
435#endif
Sean Paulac874152016-03-10 16:00:26 -0500436 default:
437 *value = -1;
438 return HWC2::Error::BadConfig;
439 }
440 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500441}
442
443HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
444 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500445 supported(__func__);
446 // Since this callback is normally invoked twice (once to get the count, and
447 // once to populate configs), we don't really want to read the edid
448 // redundantly. Instead, only update the modes on the first invocation. While
449 // it's possible this will result in stale modes, it'll all come out in the
450 // wash when we try to set the active config later.
451 if (!configs) {
452 int ret = connector_->UpdateModes();
453 if (ret) {
454 ALOGE("Failed to update display modes %d", ret);
455 return HWC2::Error::BadDisplay;
456 }
457 }
458
Neil Armstrongb67d0492019-06-20 09:00:21 +0000459 // Since the upper layers only look at vactive/hactive/refresh, height and
460 // width, it doesn't differentiate interlaced from progressive and other
461 // similar modes. Depending on the order of modes we return to SF, it could
462 // end up choosing a suboptimal configuration and dropping the preferred
463 // mode. To workaround this, don't offer interlaced modes to SF if there is
464 // at least one non-interlaced alternative and only offer a single WxH@R
465 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
466
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200467 // TODO(nobody): Remove the following block of code until AOSP handles all
468 // modes
Neil Armstrongb67d0492019-06-20 09:00:21 +0000469 std::vector<DrmMode> sel_modes;
470
471 // Add the preferred mode first to be sure it's not dropped
472 auto mode = std::find_if(connector_->modes().begin(),
473 connector_->modes().end(), [&](DrmMode const &m) {
474 return m.id() ==
475 connector_->get_preferred_mode_id();
476 });
477 if (mode != connector_->modes().end())
478 sel_modes.push_back(*mode);
479
480 // Add the active mode if different from preferred mode
481 if (connector_->active_mode().id() != connector_->get_preferred_mode_id())
482 sel_modes.push_back(connector_->active_mode());
483
484 // Cycle over the modes and filter out "similar" modes, keeping only the
485 // first ones in the order given by DRM (from CEA ids and timings order)
Sean Paulac874152016-03-10 16:00:26 -0500486 for (const DrmMode &mode : connector_->modes()) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200487 // TODO(nobody): Remove this when 3D Attributes are in AOSP
Neil Armstrongb67d0492019-06-20 09:00:21 +0000488 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
489 continue;
490
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200491 // TODO(nobody): Remove this when the Interlaced attribute is in AOSP
Neil Armstrong4c027a72019-06-04 14:48:02 +0000492 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
493 auto m = std::find_if(connector_->modes().begin(),
494 connector_->modes().end(),
495 [&mode](DrmMode const &m) {
496 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
497 m.h_display() == mode.h_display() &&
498 m.v_display() == mode.v_display();
499 });
Neil Armstrongb67d0492019-06-20 09:00:21 +0000500 if (m == connector_->modes().end())
501 sel_modes.push_back(mode);
502
503 continue;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000504 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000505
506 // Search for a similar WxH@R mode in the filtered list and drop it if
507 // another mode with the same WxH@R has already been selected
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200508 // TODO(nobody): Remove this when AOSP handles duplicates modes
Neil Armstrongb67d0492019-06-20 09:00:21 +0000509 auto m = std::find_if(sel_modes.begin(), sel_modes.end(),
510 [&mode](DrmMode const &m) {
511 return m.h_display() == mode.h_display() &&
512 m.v_display() == mode.v_display() &&
513 m.v_refresh() == mode.v_refresh();
514 });
515 if (m == sel_modes.end())
516 sel_modes.push_back(mode);
517 }
518
519 auto num_modes = static_cast<uint32_t>(sel_modes.size());
520 if (!configs) {
521 *num_configs = num_modes;
522 return HWC2::Error::None;
523 }
524
525 uint32_t idx = 0;
526 for (const DrmMode &mode : sel_modes) {
527 if (idx >= *num_configs)
528 break;
529 configs[idx++] = mode.id();
Sean Paulac874152016-03-10 16:00:26 -0500530 }
531 *num_configs = idx;
532 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500533}
534
535HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500536 supported(__func__);
537 std::ostringstream stream;
538 stream << "display-" << connector_->id();
539 std::string string = stream.str();
540 size_t length = string.length();
541 if (!name) {
542 *size = length;
543 return HWC2::Error::None;
544 }
545
546 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
547 strncpy(name, string.c_str(), *size);
548 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500549}
550
Sean Paulac874152016-03-10 16:00:26 -0500551HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
552 uint32_t *num_elements,
553 hwc2_layer_t *layers,
554 int32_t *layer_requests) {
555 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200556 // TODO(nobody): I think virtual display should request
Sean Paulac874152016-03-10 16:00:26 -0500557 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
558 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
559 *num_elements = 0;
560 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500561}
562
563HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500564 supported(__func__);
565 *type = static_cast<int32_t>(type_);
566 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500567}
568
569HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500570 supported(__func__);
571 *support = 0;
572 return HWC2::Error::None;
573}
574
575HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400576 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
577 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500578 supported(__func__);
579 *num_types = 0;
580 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500581}
582
583HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500584 hwc2_layer_t *layers,
585 int32_t *fences) {
586 supported(__func__);
587 uint32_t num_layers = 0;
588
589 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
590 ++num_layers;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200591 if (layers == nullptr || fences == nullptr)
Sean Paulac874152016-03-10 16:00:26 -0500592 continue;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200593
594 if (num_layers > *num_elements) {
Sean Paulac874152016-03-10 16:00:26 -0500595 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
596 return HWC2::Error::None;
597 }
598
599 layers[num_layers - 1] = l.first;
600 fences[num_layers - 1] = l.second.take_release_fence();
601 }
602 *num_elements = num_layers;
603 return HWC2::Error::None;
604}
605
Matteo Franchinc56eede2019-12-03 17:10:38 +0000606void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(int fd) {
Sean Paulac874152016-03-10 16:00:26 -0500607 if (fd < 0)
608 return;
609
Matteo Franchinc56eede2019-12-03 17:10:38 +0000610 if (present_fence_.get() >= 0) {
611 int old_fence = present_fence_.get();
612 present_fence_.Set(sync_merge("dc_present", old_fence, fd));
613 close(fd);
Sean Paulac874152016-03-10 16:00:26 -0500614 } else {
Matteo Franchinc56eede2019-12-03 17:10:38 +0000615 present_fence_.Set(fd);
Sean Paulac874152016-03-10 16:00:26 -0500616 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500617}
618
Rob Herring4f6c62e2018-05-17 14:33:02 -0500619HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500620 // order the layers by z-order
621 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100622 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500623 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
624 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200625 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500626 case HWC2::Composition::Device:
627 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
628 break;
629 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100630 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500631 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100632 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500633 break;
634 default:
635 continue;
636 }
637 }
638 if (use_client_layer)
639 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
640
Rob Herring4f6c62e2018-05-17 14:33:02 -0500641 if (z_map.empty())
642 return HWC2::Error::BadLayer;
643
Matvii Zorin5368b732021-01-12 10:53:08 +0200644 std::vector<DrmHwcLayer> composition_layers;
645
Sean Paulac874152016-03-10 16:00:26 -0500646 // now that they're ordered by z, add them to the composition
647 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
648 DrmHwcLayer layer;
649 l.second->PopulateDrmLayer(&layer);
Roman Stratiienko8666dc92021-02-09 17:49:55 +0200650 int ret = layer.ImportBuffer(drm_);
Sean Paulac874152016-03-10 16:00:26 -0500651 if (ret) {
652 ALOGE("Failed to import layer, ret=%d", ret);
653 return HWC2::Error::NoResources;
654 }
Matvii Zorin5368b732021-01-12 10:53:08 +0200655 composition_layers.emplace_back(std::move(layer));
Sean Paulac874152016-03-10 16:00:26 -0500656 }
Sean Paulac874152016-03-10 16:00:26 -0500657
Matvii Zorin704ea0e2021-01-08 12:55:45 +0200658 auto composition = std::make_unique<DrmDisplayComposition>(crtc_,
659 planner_.get());
Sean Paulac874152016-03-10 16:00:26 -0500660
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200661 // TODO(nobody): Don't always assume geometry changed
Matvii Zorin5368b732021-01-12 10:53:08 +0200662 int ret = composition->SetLayers(composition_layers.data(),
663 composition_layers.size(), true);
Sean Paulac874152016-03-10 16:00:26 -0500664 if (ret) {
665 ALOGE("Failed to set layers in the composition ret=%d", ret);
666 return HWC2::Error::BadLayer;
667 }
668
669 std::vector<DrmPlane *> primary_planes(primary_planes_);
670 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500671 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500672 if (ret) {
673 ALOGE("Failed to plan the composition ret=%d", ret);
674 return HWC2::Error::BadConfig;
675 }
676
677 // Disable the planes we're not using
678 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
679 composition->AddPlaneDisable(*i);
680 i = primary_planes.erase(i);
681 }
682 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
683 composition->AddPlaneDisable(*i);
684 i = overlay_planes.erase(i);
685 }
686
Rob Herring4f6c62e2018-05-17 14:33:02 -0500687 if (test) {
688 ret = compositor_.TestComposition(composition.get());
689 } else {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500690 ret = compositor_.ApplyComposition(std::move(composition));
Matteo Franchinc56eede2019-12-03 17:10:38 +0000691 AddFenceToPresentFence(compositor_.TakeOutFence());
Rob Herring4f6c62e2018-05-17 14:33:02 -0500692 }
Sean Paulac874152016-03-10 16:00:26 -0500693 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700694 if (!test)
695 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500696 return HWC2::Error::BadParameter;
697 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500698 return HWC2::Error::None;
699}
700
Matteo Franchinc56eede2019-12-03 17:10:38 +0000701HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500702 supported(__func__);
703 HWC2::Error ret;
704
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200705 ++total_stats_.total_frames_;
706
Rob Herring4f6c62e2018-05-17 14:33:02 -0500707 ret = CreateComposition(false);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200708 if (ret != HWC2::Error::None)
709 ++total_stats_.failed_kms_present_;
710
Rob Herring4f6c62e2018-05-17 14:33:02 -0500711 if (ret == HWC2::Error::BadLayer) {
712 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000713 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500714 return HWC2::Error::None;
715 }
716 if (ret != HWC2::Error::None)
717 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500718
Matteo Franchinc56eede2019-12-03 17:10:38 +0000719 *present_fence = present_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500720
721 ++frame_no_;
722 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500723}
724
725HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500726 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400727 auto mode = std::find_if(connector_->modes().begin(),
728 connector_->modes().end(),
729 [config](DrmMode const &m) {
730 return m.id() == config;
731 });
Sean Paulac874152016-03-10 16:00:26 -0500732 if (mode == connector_->modes().end()) {
733 ALOGE("Could not find active mode for %d", config);
734 return HWC2::Error::BadConfig;
735 }
736
Matvii Zorin704ea0e2021-01-08 12:55:45 +0200737 auto composition = std::make_unique<DrmDisplayComposition>(crtc_,
738 planner_.get());
Sean Paulac874152016-03-10 16:00:26 -0500739 int ret = composition->SetDisplayMode(*mode);
Roman Stratiienko6a10c4c2021-02-15 11:25:23 +0200740 if (ret) {
741 return HWC2::Error::BadConfig;
742 }
Sean Pauled45a8e2017-02-28 13:17:34 -0500743 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500744 if (ret) {
745 ALOGE("Failed to queue dpms composition on %d", ret);
746 return HWC2::Error::BadConfig;
747 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300748
749 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500750
751 // Setup the client layer's dimensions
752 hwc_rect_t display_frame = {.left = 0,
753 .top = 0,
754 .right = static_cast<int>(mode->h_display()),
755 .bottom = static_cast<int>(mode->v_display())};
756 client_layer_.SetLayerDisplayFrame(display_frame);
Sean Paulac874152016-03-10 16:00:26 -0500757
758 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500759}
760
761HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
762 int32_t acquire_fence,
763 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600764 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500765 supported(__func__);
766 UniqueFd uf(acquire_fence);
767
768 client_layer_.set_buffer(target);
769 client_layer_.set_acquire_fence(uf.get());
770 client_layer_.SetLayerDataspace(dataspace);
Roman Stratiienko33365c22020-10-10 23:06:36 +0300771
772 /* TODO: Do not update source_crop every call.
773 * It makes sense to do it once after every hotplug event. */
774 hwc_drm_bo bo{};
775 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
776
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200777 hwc_frect_t source_crop = {.left = 0.0F,
778 .top = 0.0F,
779 .right = bo.width + 0.0F,
780 .bottom = bo.height + 0.0F};
Roman Stratiienko33365c22020-10-10 23:06:36 +0300781 client_layer_.SetLayerSourceCrop(source_crop);
782
Sean Paulac874152016-03-10 16:00:26 -0500783 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500784}
785
786HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500787 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800788
Roman Stratiienkod146d6d2020-10-04 23:56:46 +0300789 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
790 return HWC2::Error::BadParameter;
791
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800792 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +0300793 return HWC2::Error::Unsupported;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800794
795 color_mode_ = mode;
796 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500797}
798
799HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500800 int32_t hint) {
801 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200802 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
803 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
804 return HWC2::Error::BadParameter;
805
806 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
807 return HWC2::Error::BadParameter;
808
809 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
810 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
811 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
812
813 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500814}
815
816HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500817 int32_t release_fence) {
818 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200819 // TODO(nobody): Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500820 return unsupported(__func__, buffer, release_fence);
821}
822
Sean Paulac874152016-03-10 16:00:26 -0500823HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
824 supported(__func__);
825 uint64_t dpms_value = 0;
826 auto mode = static_cast<HWC2::PowerMode>(mode_in);
827 switch (mode) {
828 case HWC2::PowerMode::Off:
829 dpms_value = DRM_MODE_DPMS_OFF;
830 break;
831 case HWC2::PowerMode::On:
832 dpms_value = DRM_MODE_DPMS_ON;
833 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100834 case HWC2::PowerMode::Doze:
835 case HWC2::PowerMode::DozeSuspend:
836 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500837 default:
838 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100839 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500840 };
841
Matvii Zorin704ea0e2021-01-08 12:55:45 +0200842 auto composition = std::make_unique<DrmDisplayComposition>(crtc_,
843 planner_.get());
Sean Paulac874152016-03-10 16:00:26 -0500844 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500845 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500846 if (ret) {
847 ALOGE("Failed to apply the dpms composition ret=%d", ret);
848 return HWC2::Error::BadParameter;
849 }
850 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500851}
852
853HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500854 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300855 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500856 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500857}
858
859HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500860 uint32_t *num_requests) {
861 supported(__func__);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500862
Matvii Zorinef3c7972020-08-11 15:15:44 +0300863 return backend_->ValidateDisplay(this, num_types, num_requests);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500864}
865
Matvii Zorined90ef92021-01-29 18:32:06 +0200866std::vector<DrmHwcTwo::HwcLayer *>
867DrmHwcTwo::HwcDisplay::GetOrderLayersByZPos() {
868 std::vector<DrmHwcTwo::HwcLayer *> ordered_layers;
869 ordered_layers.reserve(layers_.size());
870
871 for (auto &[handle, layer] : layers_) {
872 ordered_layers.emplace_back(&layer);
873 }
874
875 std::sort(std::begin(ordered_layers), std::end(ordered_layers),
876 [](const DrmHwcTwo::HwcLayer *lhs, const DrmHwcTwo::HwcLayer *rhs) {
877 return lhs->z_order() < rhs->z_order();
878 });
879
880 return ordered_layers;
881}
882
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300883#if PLATFORM_SDK_VERSION > 29
884HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
885 if (connector_->internal())
886 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
887 else if (connector_->external())
888 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
889 else
890 return HWC2::Error::BadConfig;
891
892 return HWC2::Error::None;
893}
894
895HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayVsyncPeriod(
896 hwc2_vsync_period_t *outVsyncPeriod /* ns */) {
897 supported(__func__);
898 DrmMode const &mode = connector_->active_mode();
899 if (mode.id() == 0)
900 return HWC2::Error::BadConfig;
901
902 *outVsyncPeriod = 1E9 / mode.v_refresh();
903 return HWC2::Error::None;
904}
905
906HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfigWithConstraints(
907 hwc2_config_t /*config*/,
908 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
909 hwc_vsync_period_change_timeline_t *outTimeline) {
910 supported(__func__);
911
912 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
913 return HWC2::Error::BadParameter;
914 }
915
916 return HWC2::Error::BadConfig;
917}
918
919HWC2::Error DrmHwcTwo::HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
920 return HWC2::Error::Unsupported;
921}
922
923HWC2::Error DrmHwcTwo::HwcDisplay::GetSupportedContentTypes(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200924 uint32_t *outNumSupportedContentTypes,
925 const uint32_t *outSupportedContentTypes) {
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300926 if (outSupportedContentTypes == nullptr)
927 *outNumSupportedContentTypes = 0;
928
929 return HWC2::Error::None;
930}
931
932HWC2::Error DrmHwcTwo::HwcDisplay::SetContentType(int32_t contentType) {
933 supported(__func__);
934
935 if (contentType != HWC2_CONTENT_TYPE_NONE)
936 return HWC2::Error::Unsupported;
937
938 /* TODO: Map to the DRM Connector property:
939 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
940 */
941
942 return HWC2::Error::None;
943}
944#endif
945
John Stultz8c7229d2020-02-07 21:31:08 +0000946#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800947HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
948 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
949 supported(__func__);
950
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200951 drmModePropertyBlobPtr blob = nullptr;
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800952
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300953 if (connector_->GetEdidBlob(blob)) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800954 ALOGE("Failed to get edid property value.");
955 return HWC2::Error::Unsupported;
956 }
957
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +0300958 if (outData) {
959 *outDataSize = std::min(*outDataSize, blob->length);
960 memcpy(outData, blob->data, *outDataSize);
961 } else {
962 *outDataSize = blob->length;
963 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800964 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800965
966 return HWC2::Error::None;
967}
968
969HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
970 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
971 unsupported(__func__, outCapabilities);
972
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200973 if (outNumCapabilities == nullptr) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800974 return HWC2::Error::BadParameter;
975 }
976
977 *outNumCapabilities = 0;
978
979 return HWC2::Error::None;
980}
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +0300981
982HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
983 bool *supported) {
984 *supported = false;
985 return HWC2::Error::None;
986}
987
988HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
989 float /* brightness */) {
990 return HWC2::Error::Unsupported;
991}
992
John Stultz8c7229d2020-02-07 21:31:08 +0000993#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800994
Andrii Chepurnyi50d37452020-04-24 14:20:24 +0300995#if PLATFORM_SDK_VERSION > 27
996
997HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
998 int32_t mode, uint32_t *outNumIntents,
999 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1000 if (mode != HAL_COLOR_MODE_NATIVE) {
1001 return HWC2::Error::BadParameter;
1002 }
1003
1004 if (outIntents == nullptr) {
1005 *outNumIntents = 1;
1006 return HWC2::Error::None;
1007 }
1008 *outNumIntents = 1;
1009 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1010 return HWC2::Error::None;
1011}
1012
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001013HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
1014 int32_t intent) {
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001015 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1016 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1017 return HWC2::Error::BadParameter;
1018
1019 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
1020 return HWC2::Error::BadParameter;
1021
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001022 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001023 return HWC2::Error::Unsupported;
1024
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001025 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001026 return HWC2::Error::Unsupported;
1027
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001028 color_mode_ = mode;
1029 return HWC2::Error::None;
1030}
1031
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001032#endif /* PLATFORM_SDK_VERSION > 27 */
1033
Sean Pauled2ec4b2016-03-10 15:35:40 -05001034HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -05001035 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001036 cursor_x_ = x;
1037 cursor_y_ = y;
1038 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001039}
1040
1041HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001042 supported(__func__);
1043 blending_ = static_cast<HWC2::BlendMode>(mode);
1044 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001045}
1046
1047HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001048 int32_t acquire_fence) {
1049 supported(__func__);
1050 UniqueFd uf(acquire_fence);
1051
Sean Paulac874152016-03-10 16:00:26 -05001052 set_buffer(buffer);
1053 set_acquire_fence(uf.get());
1054 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001055}
1056
1057HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001058 // TODO(nobody): Put to client composition here?
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001059 supported(__func__);
1060 layer_color_ = color;
1061 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001062}
1063
1064HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001065 sf_type_ = static_cast<HWC2::Composition>(type);
1066 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001067}
1068
1069HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001070 supported(__func__);
1071 dataspace_ = static_cast<android_dataspace_t>(dataspace);
1072 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001073}
1074
1075HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001076 supported(__func__);
1077 display_frame_ = frame;
1078 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001079}
1080
1081HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001082 supported(__func__);
1083 alpha_ = alpha;
1084 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001085}
1086
1087HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1088 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001089 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001090 // TODO(nobody): We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001091 return unsupported(__func__, stream);
1092}
1093
1094HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001095 supported(__func__);
1096 source_crop_ = crop;
1097 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001098}
1099
1100HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001101 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001102 // TODO(nobody): We don't use surface damage, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001103 unsupported(__func__, damage);
1104 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001105}
1106
1107HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001108 supported(__func__);
1109 transform_ = static_cast<HWC2::Transform>(transform);
1110 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001111}
1112
1113HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001114 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001115 // TODO(nobody): We don't use this information, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001116 unsupported(__func__, visible);
1117 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001118}
1119
Sean Paulac874152016-03-10 16:00:26 -05001120HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1121 supported(__func__);
1122 z_order_ = order;
1123 return HWC2::Error::None;
1124}
1125
1126void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1127 supported(__func__);
1128 switch (blending_) {
1129 case HWC2::BlendMode::None:
1130 layer->blending = DrmHwcBlending::kNone;
1131 break;
1132 case HWC2::BlendMode::Premultiplied:
1133 layer->blending = DrmHwcBlending::kPreMult;
1134 break;
1135 case HWC2::BlendMode::Coverage:
1136 layer->blending = DrmHwcBlending::kCoverage;
1137 break;
1138 default:
1139 ALOGE("Unknown blending mode b=%d", blending_);
1140 layer->blending = DrmHwcBlending::kNone;
1141 break;
1142 }
1143
1144 OutputFd release_fence = release_fence_output();
1145
1146 layer->sf_handle = buffer_;
1147 layer->acquire_fence = acquire_fence_.Release();
1148 layer->release_fence = std::move(release_fence);
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001149 layer->display_frame = display_frame_;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001150 layer->alpha = lround(65535.0F * alpha_);
Roman Kovalivskyib8652272021-01-24 20:32:37 +02001151 layer->source_crop = source_crop_;
Sean Paulac874152016-03-10 16:00:26 -05001152 layer->SetTransform(static_cast<int32_t>(transform_));
Matvii Zorin8338c342020-09-08 16:12:51 +03001153 layer->dataspace = dataspace_;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001154}
1155
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001156void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
Roman Stratiienko23701092020-09-26 02:08:41 +03001157 const std::lock_guard<std::mutex> lock(hotplug_callback_lock);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001158
Roman Stratiienko23701092020-09-26 02:08:41 +03001159 if (hotplug_callback_hook_ && hotplug_callback_data_)
1160 hotplug_callback_hook_(hotplug_callback_data_, displayid,
1161 state == DRM_MODE_CONNECTED
1162 ? HWC2_CONNECTION_CONNECTED
1163 : HWC2_CONNECTION_DISCONNECTED);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001164}
1165
1166void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
Roman Stratiienkod21071f2021-03-09 21:56:50 +02001167 for (const auto &conn : drmDevice->connectors()) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001168 if (conn->state() != DRM_MODE_CONNECTED)
1169 continue;
1170 HandleDisplayHotplug(conn->display(), conn->state());
1171 }
1172}
1173
1174void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
Roman Stratiienkod21071f2021-03-09 21:56:50 +02001175 for (const auto &conn : drm_->connectors()) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001176 drmModeConnection old_state = conn->state();
1177 drmModeConnection cur_state = conn->UpdateModes()
1178 ? DRM_MODE_UNKNOWNCONNECTION
1179 : conn->state();
1180
1181 if (cur_state == old_state)
1182 continue;
1183
1184 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1185 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1186 conn->id(), conn->display());
1187
1188 int display_id = conn->display();
1189 if (cur_state == DRM_MODE_CONNECTED) {
1190 auto &display = hwc2_->displays_.at(display_id);
1191 display.ChosePreferredConfig();
1192 } else {
1193 auto &display = hwc2_->displays_.at(display_id);
1194 display.ClearDisplay();
1195 }
1196
1197 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1198 }
1199}
1200
Sean Pauled2ec4b2016-03-10 15:35:40 -05001201// static
1202int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1203 unsupported(__func__);
1204 return 0;
1205}
1206
1207// static
1208void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001209 uint32_t *out_count,
1210 int32_t * /*out_capabilities*/) {
1211 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001212 *out_count = 0;
1213}
1214
1215// static
Sean Paulac874152016-03-10 16:00:26 -05001216hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1217 struct hwc2_device * /*dev*/, int32_t descriptor) {
1218 supported(__func__);
1219 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001220 switch (func) {
1221 // Device functions
1222 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1223 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1224 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1225 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001226 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001227 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1228 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1229 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1230 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1231 case HWC2::FunctionDescriptor::Dump:
1232 return ToHook<HWC2_PFN_DUMP>(
1233 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1234 uint32_t *, char *>);
1235 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1236 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1237 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1238 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1239 case HWC2::FunctionDescriptor::RegisterCallback:
1240 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1241 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1242 &DrmHwcTwo::RegisterCallback, int32_t,
1243 hwc2_callback_data_t, hwc2_function_pointer_t>);
1244
1245 // Display functions
1246 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1247 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1248 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1249 &HwcDisplay::AcceptDisplayChanges>);
1250 case HWC2::FunctionDescriptor::CreateLayer:
1251 return ToHook<HWC2_PFN_CREATE_LAYER>(
1252 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1253 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1254 case HWC2::FunctionDescriptor::DestroyLayer:
1255 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1256 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1257 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1258 case HWC2::FunctionDescriptor::GetActiveConfig:
1259 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1260 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1261 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1262 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1263 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1264 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1265 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1266 hwc2_layer_t *, int32_t *>);
1267 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1268 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1269 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1270 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1271 int32_t, int32_t>);
1272 case HWC2::FunctionDescriptor::GetColorModes:
1273 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1274 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1275 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1276 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001277 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1278 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1279 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1280 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001281 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001282 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1283 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1284 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1285 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001286 case HWC2::FunctionDescriptor::GetDisplayName:
1287 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1288 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1289 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1290 case HWC2::FunctionDescriptor::GetDisplayRequests:
1291 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1292 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1293 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1294 hwc2_layer_t *, int32_t *>);
1295 case HWC2::FunctionDescriptor::GetDisplayType:
1296 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1297 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1298 &HwcDisplay::GetDisplayType, int32_t *>);
1299 case HWC2::FunctionDescriptor::GetDozeSupport:
1300 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1301 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1302 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001303 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1304 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1305 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1306 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1307 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001308 case HWC2::FunctionDescriptor::GetReleaseFences:
1309 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1310 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1311 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1312 int32_t *>);
1313 case HWC2::FunctionDescriptor::PresentDisplay:
1314 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1315 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1316 &HwcDisplay::PresentDisplay, int32_t *>);
1317 case HWC2::FunctionDescriptor::SetActiveConfig:
1318 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1319 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1320 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1321 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001322 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1323 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1324 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1325 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001326 case HWC2::FunctionDescriptor::SetColorMode:
1327 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1328 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1329 &HwcDisplay::SetColorMode, int32_t>);
1330 case HWC2::FunctionDescriptor::SetColorTransform:
1331 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1332 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1333 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1334 case HWC2::FunctionDescriptor::SetOutputBuffer:
1335 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1336 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1337 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1338 case HWC2::FunctionDescriptor::SetPowerMode:
1339 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1340 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1341 &HwcDisplay::SetPowerMode, int32_t>);
1342 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1343 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1344 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1345 &HwcDisplay::SetVsyncEnabled, int32_t>);
1346 case HWC2::FunctionDescriptor::ValidateDisplay:
1347 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1348 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1349 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001350#if PLATFORM_SDK_VERSION > 27
1351 case HWC2::FunctionDescriptor::GetRenderIntents:
1352 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1353 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1354 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1355 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001356 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1357 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1358 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1359 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001360#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001361#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001362 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1363 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1364 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1365 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1366 uint32_t *, uint8_t *>);
1367 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1368 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1369 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1370 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1371 uint32_t *>);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001372 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1373 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1374 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1375 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1376 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1377 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1378 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1379 &HwcDisplay::SetDisplayBrightness, float>);
John Stultz8c7229d2020-02-07 21:31:08 +00001380#endif /* PLATFORM_SDK_VERSION > 28 */
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001381#if PLATFORM_SDK_VERSION > 29
1382 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
1383 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
1384 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
1385 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
1386 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
1387 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
1388 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
1389 &HwcDisplay::GetDisplayVsyncPeriod,
1390 hwc2_vsync_period_t *>);
1391 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
1392 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
1393 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
1394 &HwcDisplay::SetActiveConfigWithConstraints,
1395 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
1396 hwc_vsync_period_change_timeline_t *>);
1397 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
1398 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
1399 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
1400 &HwcDisplay::SetAutoLowLatencyMode, bool>);
1401 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
1402 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
1403 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
1404 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
1405 uint32_t *>);
1406 case HWC2::FunctionDescriptor::SetContentType:
1407 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
1408 DisplayHook<decltype(&HwcDisplay::SetContentType),
1409 &HwcDisplay::SetContentType, int32_t>);
1410#endif
Sean Pauled2ec4b2016-03-10 15:35:40 -05001411 // Layer functions
1412 case HWC2::FunctionDescriptor::SetCursorPosition:
1413 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1414 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1415 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1416 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1417 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1418 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1419 &HwcLayer::SetLayerBlendMode, int32_t>);
1420 case HWC2::FunctionDescriptor::SetLayerBuffer:
1421 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1422 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1423 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1424 case HWC2::FunctionDescriptor::SetLayerColor:
1425 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1426 LayerHook<decltype(&HwcLayer::SetLayerColor),
1427 &HwcLayer::SetLayerColor, hwc_color_t>);
1428 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1429 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1430 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1431 &HwcLayer::SetLayerCompositionType, int32_t>);
1432 case HWC2::FunctionDescriptor::SetLayerDataspace:
1433 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1434 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1435 &HwcLayer::SetLayerDataspace, int32_t>);
1436 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1437 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1438 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1439 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1440 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1441 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1442 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1443 &HwcLayer::SetLayerPlaneAlpha, float>);
1444 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001445 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1446 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1447 &HwcLayer::SetLayerSidebandStream,
1448 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001449 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1450 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1451 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1452 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1453 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1454 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1455 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1456 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1457 case HWC2::FunctionDescriptor::SetLayerTransform:
1458 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1459 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1460 &HwcLayer::SetLayerTransform, int32_t>);
1461 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1462 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1463 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1464 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1465 case HWC2::FunctionDescriptor::SetLayerZOrder:
1466 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1467 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1468 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001469 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001470 default:
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001471 return nullptr;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001472 }
1473}
Sean Paulac874152016-03-10 16:00:26 -05001474
1475// static
1476int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1477 struct hw_device_t **dev) {
1478 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001479 if (strcmp(name, HWC_HARDWARE_COMPOSER) != 0) {
Sean Paulac874152016-03-10 16:00:26 -05001480 ALOGE("Invalid module name- %s", name);
1481 return -EINVAL;
1482 }
1483
1484 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1485 if (!ctx) {
1486 ALOGE("Failed to allocate DrmHwcTwo");
1487 return -ENOMEM;
1488 }
1489
1490 HWC2::Error err = ctx->Init();
1491 if (err != HWC2::Error::None) {
1492 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1493 return -EINVAL;
1494 }
1495
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001496 ctx->common.module = (hw_module_t *)module;
Sean Paulac874152016-03-10 16:00:26 -05001497 *dev = &ctx->common;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001498 ctx.release(); // NOLINT(bugprone-unused-return-value)
Sean Paulac874152016-03-10 16:00:26 -05001499 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001500}
Sean Paulf72cccd2018-08-27 13:59:08 -04001501} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001502
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001503// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001504static struct hw_module_methods_t hwc2_module_methods = {
1505 .open = android::DrmHwcTwo::HookDevOpen,
1506};
1507
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001508// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001509hw_module_t HAL_MODULE_INFO_SYM = {
1510 .tag = HARDWARE_MODULE_TAG,
1511 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1512 .id = HWC_HARDWARE_MODULE_ID,
1513 .name = "DrmHwcTwo module",
1514 .author = "The Android Open Source Project",
1515 .methods = &hwc2_module_methods,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001516 .dso = nullptr,
Sean Paulac874152016-03-10 16:00:26 -05001517 .reserved = {0},
1518};