blob: c2de931f51c8edbf9cc8a4f75a624305a4945553 [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 <cutils/properties.h>
23#include <hardware/hardware.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050024#include <hardware/hwcomposer2.h>
Sean Paulf72cccd2018-08-27 13:59:08 -040025#include <log/log.h>
Roman Stratiienko74774712021-02-05 16:32:47 +020026#include <sync/sync.h>
Sean Pauled2ec4b2016-03-10 15:35:40 -050027
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020028#include <cinttypes>
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 Stratiienkoaa3cd542020-08-29 11:26:16 +030034
Sean Pauled2ec4b2016-03-10 15:35:40 -050035namespace android {
36
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020037DrmHwcTwo::DrmHwcTwo() : hwc2_device() {
Sean Paulac874152016-03-10 16:00:26 -050038 common.tag = HARDWARE_DEVICE_TAG;
39 common.version = HWC_DEVICE_API_VERSION_2_0;
Sean Pauled2ec4b2016-03-10 15:35:40 -050040 common.close = HookDevClose;
41 getCapabilities = HookDevGetCapabilities;
42 getFunction = HookDevGetFunction;
43}
44
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030045HWC2::Error DrmHwcTwo::CreateDisplay(hwc2_display_t displ,
46 HWC2::DisplayType type) {
47 DrmDevice *drm = resource_manager_.GetDrmDevice(displ);
48 std::shared_ptr<Importer> importer = resource_manager_.GetImporter(displ);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010049 if (!drm || !importer) {
50 ALOGE("Failed to get a valid drmresource and importer");
Sean Paulac874152016-03-10 16:00:26 -050051 return HWC2::Error::NoResources;
52 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030053 displays_.emplace(std::piecewise_construct, std::forward_as_tuple(displ),
Sean Paulf72cccd2018-08-27 13:59:08 -040054 std::forward_as_tuple(&resource_manager_, drm, importer,
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030055 displ, type));
Sean Paulac874152016-03-10 16:00:26 -050056
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030057 DrmCrtc *crtc = drm->GetCrtcForDisplay(static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050058 if (!crtc) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030059 ALOGE("Failed to get crtc for display %d", static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050060 return HWC2::Error::BadDisplay;
61 }
Sean Paulac874152016-03-10 16:00:26 -050062 std::vector<DrmPlane *> display_planes;
Alexandru Gheorghec5463582018-03-27 15:52:02 +010063 for (auto &plane : drm->planes()) {
Sean Paulac874152016-03-10 16:00:26 -050064 if (plane->GetCrtcSupported(*crtc))
65 display_planes.push_back(plane.get());
66 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030067 displays_.at(displ).Init(&display_planes);
Sean Paulac874152016-03-10 16:00:26 -050068 return HWC2::Error::None;
69}
70
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030071HWC2::Error DrmHwcTwo::Init() {
72 int rv = resource_manager_.Init();
73 if (rv) {
74 ALOGE("Can't initialize the resource manager %d", rv);
75 return HWC2::Error::NoResources;
76 }
77
78 HWC2::Error ret = HWC2::Error::None;
79 for (int i = 0; i < resource_manager_.getDisplayCount(); i++) {
80 ret = CreateDisplay(i, HWC2::DisplayType::Physical);
81 if (ret != HWC2::Error::None) {
82 ALOGE("Failed to create display %d with error %d", i, ret);
83 return ret;
84 }
85 }
86
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020087 auto &drm_devices = resource_manager_.getDrmDevices();
88 for (auto &device : drm_devices) {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020089 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030090 device->RegisterHotplugHandler(new DrmHotplugHandler(this, device.get()));
91 }
92 return ret;
93}
94
Sean Pauled2ec4b2016-03-10 15:35:40 -050095template <typename... Args>
96static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
97 ALOGV("Unsupported function: %s", func);
98 return HWC2::Error::Unsupported;
99}
100
Sean Paulac874152016-03-10 16:00:26 -0500101static inline void supported(char const *func) {
102 ALOGV("Supported function: %s", func);
103}
104
Sean Pauled2ec4b2016-03-10 15:35:40 -0500105HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t width, uint32_t height,
106 int32_t *format,
107 hwc2_display_t *display) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200108 // TODO(nobody): Implement virtual display
Sean Paulac874152016-03-10 16:00:26 -0500109 return unsupported(__func__, width, height, format, display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500110}
111
112HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t display) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200113 // TODO(nobody): Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500114 return unsupported(__func__, display);
115}
116
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200117std::string DrmHwcTwo::HwcDisplay::DumpDelta(
118 DrmHwcTwo::HwcDisplay::Stats delta) {
119 if (delta.total_pixops_ == 0)
120 return "No stats yet";
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200121 double ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200122
123 return (std::stringstream()
124 << " Total frames count: " << delta.total_frames_ << "\n"
125 << " Failed to test commit frames: " << delta.failed_kms_validate_
126 << "\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 : "")
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200131 << " Flattened frames: " << delta.frames_flattened_ << "\n"
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200132 << " Pixel operations (free units)"
133 << " : [TOTAL: " << delta.total_pixops_
134 << " / GPU: " << delta.gpu_pixops_ << "]\n"
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200135 << " Composition efficiency: " << ratio)
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200136 .str();
137}
138
139std::string DrmHwcTwo::HwcDisplay::Dump() {
140 auto out = (std::stringstream()
141 << "- Display on: " << connector_->name() << "\n"
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200142 << " Flattening state: " << compositor_.GetFlatteningState()
143 << "\n"
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200144 << "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")
148 .str();
149
150 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
151 return out;
152}
153
154void DrmHwcTwo::Dump(uint32_t *outSize, char *outBuffer) {
155 supported(__func__);
156
157 if (outBuffer != nullptr) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200158 auto copied_bytes = mDumpString.copy(outBuffer, *outSize);
159 *outSize = static_cast<uint32_t>(copied_bytes);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200160 return;
161 }
162
163 std::stringstream output;
164
165 output << "-- drm_hwcomposer --\n\n";
166
167 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &dp : displays_)
168 output << dp.second.Dump();
169
170 mDumpString = output.str();
171 *outSize = static_cast<uint32_t>(mDumpString.size());
Sean Pauled2ec4b2016-03-10 15:35:40 -0500172}
173
174uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200175 // TODO(nobody): Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500176 unsupported(__func__);
177 return 0;
178}
179
180HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
Sean Paulac874152016-03-10 16:00:26 -0500181 hwc2_callback_data_t data,
182 hwc2_function_pointer_t function) {
183 supported(__func__);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300184
Roman Stratiienko23701092020-09-26 02:08:41 +0300185 switch (static_cast<HWC2::Callback>(descriptor)) {
Sean Paulac874152016-03-10 16:00:26 -0500186 case HWC2::Callback::Hotplug: {
Roman Stratiienko23701092020-09-26 02:08:41 +0300187 SetHotplugCallback(data, function);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200188 auto &drm_devices = resource_manager_.getDrmDevices();
189 for (auto &device : drm_devices)
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300190 HandleInitialHotplugState(device.get());
Sean Paulac874152016-03-10 16:00:26 -0500191 break;
192 }
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200193 case HWC2::Callback::Refresh: {
194 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
195 displays_)
196 d.second.RegisterRefreshCallback(data, function);
197 break;
198 }
Sean Paulac874152016-03-10 16:00:26 -0500199 case HWC2::Callback::Vsync: {
200 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
201 displays_)
202 d.second.RegisterVsyncCallback(data, function);
203 break;
204 }
205 default:
206 break;
207 }
208 return HWC2::Error::None;
209}
210
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100211DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
212 DrmDevice *drm,
Sean Paulac874152016-03-10 16:00:26 -0500213 std::shared_ptr<Importer> importer,
Sean Paulac874152016-03-10 16:00:26 -0500214 hwc2_display_t handle, HWC2::DisplayType type)
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100215 : resource_manager_(resource_manager),
216 drm_(drm),
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200217 importer_(std::move(importer)),
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100218 handle_(handle),
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200219 type_(type),
220 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
Sean Paulac874152016-03-10 16:00:26 -0500221 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200222
223 // clang-format off
224 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
225 0.0, 1.0, 0.0, 0.0,
226 0.0, 0.0, 1.0, 0.0,
227 0.0, 0.0, 0.0, 1.0};
228 // clang-format on
Sean Paulac874152016-03-10 16:00:26 -0500229}
230
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300231void DrmHwcTwo::HwcDisplay::ClearDisplay() {
232 compositor_.ClearDisplay();
233}
234
Sean Paulac874152016-03-10 16:00:26 -0500235HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
236 supported(__func__);
237 planner_ = Planner::CreateInstance(drm_);
238 if (!planner_) {
239 ALOGE("Failed to create planner instance for composition");
240 return HWC2::Error::NoResources;
241 }
242
243 int display = static_cast<int>(handle_);
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100244 int ret = compositor_.Init(resource_manager_, display);
Sean Paulac874152016-03-10 16:00:26 -0500245 if (ret) {
246 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
247 return HWC2::Error::NoResources;
248 }
249
250 // Split up the given display planes into primary and overlay to properly
251 // interface with the composition
252 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -0700253 property_get("vendor.hwc.drm.use_overlay_planes", use_overlay_planes_prop,
254 "1");
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200255 bool use_overlay_planes = strtol(use_overlay_planes_prop, nullptr, 10);
Sean Paulac874152016-03-10 16:00:26 -0500256 for (auto &plane : *planes) {
257 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
258 primary_planes_.push_back(plane);
259 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
260 overlay_planes_.push_back(plane);
261 }
262
263 crtc_ = drm_->GetCrtcForDisplay(display);
264 if (!crtc_) {
265 ALOGE("Failed to get crtc for display %d", display);
266 return HWC2::Error::BadDisplay;
267 }
268
269 connector_ = drm_->GetConnectorForDisplay(display);
270 if (!connector_) {
271 ALOGE("Failed to get connector for display %d", display);
272 return HWC2::Error::BadDisplay;
273 }
274
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300275 ret = vsync_worker_.Init(drm_, display);
276 if (ret) {
277 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
278 return HWC2::Error::BadDisplay;
279 }
280
Matvii Zorinef3c7972020-08-11 15:15:44 +0300281 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
282 if (ret) {
283 ALOGE("Failed to set backend for d=%d %d\n", display, ret);
284 return HWC2::Error::BadDisplay;
285 }
286
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300287 return ChosePreferredConfig();
288}
289
290HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() {
Sean Paulac874152016-03-10 16:00:26 -0500291 // Fetch the number of modes from the display
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200292 uint32_t num_configs = 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200293 HWC2::Error err = GetDisplayConfigs(&num_configs, nullptr);
Sean Paulac874152016-03-10 16:00:26 -0500294 if (err != HWC2::Error::None || !num_configs)
295 return err;
296
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200297 return SetActiveConfig(connector_->get_preferred_mode_id());
Sean Paulac874152016-03-10 16:00:26 -0500298}
299
Roman Stratiienko23701092020-09-26 02:08:41 +0300300void DrmHwcTwo::HwcDisplay::RegisterVsyncCallback(
Sean Paulac874152016-03-10 16:00:26 -0500301 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
302 supported(__func__);
Roman Stratiienko23701092020-09-26 02:08:41 +0300303 vsync_worker_.RegisterClientCallback(data, func);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500304}
305
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200306void DrmHwcTwo::HwcDisplay::RegisterRefreshCallback(
307 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
308 supported(__func__);
Roman Stratiienko23701092020-09-26 02:08:41 +0300309 compositor_.SetRefreshCallback(data, func);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200310}
311
Sean Pauled2ec4b2016-03-10 15:35:40 -0500312HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500313 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500314 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
315 l.second.accept_type_change();
316 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500317}
318
319HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500320 supported(__func__);
321 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
322 *layer = static_cast<hwc2_layer_t>(layer_idx_);
323 ++layer_idx_;
324 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500325}
326
327HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500328 supported(__func__);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100329 if (!get_layer(layer))
330 return HWC2::Error::BadLayer;
331
Sean Paulac874152016-03-10 16:00:26 -0500332 layers_.erase(layer);
333 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500334}
335
336HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500337 supported(__func__);
338 DrmMode const &mode = connector_->active_mode();
339 if (mode.id() == 0)
340 return HWC2::Error::BadConfig;
341
342 *config = mode.id();
343 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500344}
345
346HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
347 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500348 supported(__func__);
349 uint32_t num_changes = 0;
350 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
351 if (l.second.type_changed()) {
352 if (layers && num_changes < *num_elements)
353 layers[num_changes] = l.first;
354 if (types && num_changes < *num_elements)
355 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
356 ++num_changes;
357 }
358 }
359 if (!layers && !types)
360 *num_elements = num_changes;
361 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500362}
363
364HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500365 uint32_t height,
366 int32_t /*format*/,
367 int32_t dataspace) {
368 supported(__func__);
369 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
370 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
371
372 if (width < min.first || height < min.second)
373 return HWC2::Error::Unsupported;
374
375 if (width > max.first || height > max.second)
376 return HWC2::Error::Unsupported;
377
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200378 if (dataspace != HAL_DATASPACE_UNKNOWN)
Sean Paulac874152016-03-10 16:00:26 -0500379 return HWC2::Error::Unsupported;
380
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200381 // TODO(nobody): Validate format can be handled by either GL or planes
Sean Paulac874152016-03-10 16:00:26 -0500382 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500383}
384
385HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500386 int32_t *modes) {
387 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800388 if (!modes)
389 *num_modes = 1;
390
391 if (modes)
392 *modes = HAL_COLOR_MODE_NATIVE;
393
394 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500395}
396
397HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500398 int32_t attribute_in,
399 int32_t *value) {
400 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400401 auto mode = std::find_if(connector_->modes().begin(),
402 connector_->modes().end(),
403 [config](DrmMode const &m) {
404 return m.id() == config;
405 });
Sean Paulac874152016-03-10 16:00:26 -0500406 if (mode == connector_->modes().end()) {
407 ALOGE("Could not find active mode for %d", config);
408 return HWC2::Error::BadConfig;
409 }
410
411 static const int32_t kUmPerInch = 25400;
412 uint32_t mm_width = connector_->mm_width();
413 uint32_t mm_height = connector_->mm_height();
414 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
415 switch (attribute) {
416 case HWC2::Attribute::Width:
417 *value = mode->h_display();
418 break;
419 case HWC2::Attribute::Height:
420 *value = mode->v_display();
421 break;
422 case HWC2::Attribute::VsyncPeriod:
423 // in nanoseconds
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200424 *value = 1000.0 * 1000.0 * 1000.0 / mode->v_refresh();
Sean Paulac874152016-03-10 16:00:26 -0500425 break;
426 case HWC2::Attribute::DpiX:
427 // Dots per 1000 inches
428 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
429 break;
430 case HWC2::Attribute::DpiY:
431 // Dots per 1000 inches
432 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
433 break;
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300434#if PLATFORM_SDK_VERSION > 29
435 case HWC2::Attribute::ConfigGroup:
436 *value = 0; /* TODO: Add support for config groups */
437 break;
438#endif
Sean Paulac874152016-03-10 16:00:26 -0500439 default:
440 *value = -1;
441 return HWC2::Error::BadConfig;
442 }
443 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500444}
445
446HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
447 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500448 supported(__func__);
449 // Since this callback is normally invoked twice (once to get the count, and
450 // once to populate configs), we don't really want to read the edid
451 // redundantly. Instead, only update the modes on the first invocation. While
452 // it's possible this will result in stale modes, it'll all come out in the
453 // wash when we try to set the active config later.
454 if (!configs) {
455 int ret = connector_->UpdateModes();
456 if (ret) {
457 ALOGE("Failed to update display modes %d", ret);
458 return HWC2::Error::BadDisplay;
459 }
460 }
461
Neil Armstrongb67d0492019-06-20 09:00:21 +0000462 // Since the upper layers only look at vactive/hactive/refresh, height and
463 // width, it doesn't differentiate interlaced from progressive and other
464 // similar modes. Depending on the order of modes we return to SF, it could
465 // end up choosing a suboptimal configuration and dropping the preferred
466 // mode. To workaround this, don't offer interlaced modes to SF if there is
467 // at least one non-interlaced alternative and only offer a single WxH@R
468 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
469
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200470 // TODO(nobody): Remove the following block of code until AOSP handles all
471 // modes
Neil Armstrongb67d0492019-06-20 09:00:21 +0000472 std::vector<DrmMode> sel_modes;
473
474 // Add the preferred mode first to be sure it's not dropped
475 auto mode = std::find_if(connector_->modes().begin(),
476 connector_->modes().end(), [&](DrmMode const &m) {
477 return m.id() ==
478 connector_->get_preferred_mode_id();
479 });
480 if (mode != connector_->modes().end())
481 sel_modes.push_back(*mode);
482
483 // Add the active mode if different from preferred mode
484 if (connector_->active_mode().id() != connector_->get_preferred_mode_id())
485 sel_modes.push_back(connector_->active_mode());
486
487 // Cycle over the modes and filter out "similar" modes, keeping only the
488 // first ones in the order given by DRM (from CEA ids and timings order)
Sean Paulac874152016-03-10 16:00:26 -0500489 for (const DrmMode &mode : connector_->modes()) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200490 // TODO(nobody): Remove this when 3D Attributes are in AOSP
Neil Armstrongb67d0492019-06-20 09:00:21 +0000491 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
492 continue;
493
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200494 // TODO(nobody): Remove this when the Interlaced attribute is in AOSP
Neil Armstrong4c027a72019-06-04 14:48:02 +0000495 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
496 auto m = std::find_if(connector_->modes().begin(),
497 connector_->modes().end(),
498 [&mode](DrmMode const &m) {
499 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
500 m.h_display() == mode.h_display() &&
501 m.v_display() == mode.v_display();
502 });
Neil Armstrongb67d0492019-06-20 09:00:21 +0000503 if (m == connector_->modes().end())
504 sel_modes.push_back(mode);
505
506 continue;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000507 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000508
509 // Search for a similar WxH@R mode in the filtered list and drop it if
510 // another mode with the same WxH@R has already been selected
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200511 // TODO(nobody): Remove this when AOSP handles duplicates modes
Neil Armstrongb67d0492019-06-20 09:00:21 +0000512 auto m = std::find_if(sel_modes.begin(), sel_modes.end(),
513 [&mode](DrmMode const &m) {
514 return m.h_display() == mode.h_display() &&
515 m.v_display() == mode.v_display() &&
516 m.v_refresh() == mode.v_refresh();
517 });
518 if (m == sel_modes.end())
519 sel_modes.push_back(mode);
520 }
521
522 auto num_modes = static_cast<uint32_t>(sel_modes.size());
523 if (!configs) {
524 *num_configs = num_modes;
525 return HWC2::Error::None;
526 }
527
528 uint32_t idx = 0;
529 for (const DrmMode &mode : sel_modes) {
530 if (idx >= *num_configs)
531 break;
532 configs[idx++] = mode.id();
Sean Paulac874152016-03-10 16:00:26 -0500533 }
534 *num_configs = idx;
535 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500536}
537
538HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500539 supported(__func__);
540 std::ostringstream stream;
541 stream << "display-" << connector_->id();
542 std::string string = stream.str();
543 size_t length = string.length();
544 if (!name) {
545 *size = length;
546 return HWC2::Error::None;
547 }
548
549 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
550 strncpy(name, string.c_str(), *size);
551 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500552}
553
Sean Paulac874152016-03-10 16:00:26 -0500554HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
555 uint32_t *num_elements,
556 hwc2_layer_t *layers,
557 int32_t *layer_requests) {
558 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200559 // TODO(nobody): I think virtual display should request
Sean Paulac874152016-03-10 16:00:26 -0500560 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
561 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
562 *num_elements = 0;
563 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500564}
565
566HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500567 supported(__func__);
568 *type = static_cast<int32_t>(type_);
569 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500570}
571
572HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500573 supported(__func__);
574 *support = 0;
575 return HWC2::Error::None;
576}
577
578HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400579 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
580 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500581 supported(__func__);
582 *num_types = 0;
583 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500584}
585
586HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500587 hwc2_layer_t *layers,
588 int32_t *fences) {
589 supported(__func__);
590 uint32_t num_layers = 0;
591
592 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
593 ++num_layers;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200594 if (layers == nullptr || fences == nullptr)
Sean Paulac874152016-03-10 16:00:26 -0500595 continue;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200596
597 if (num_layers > *num_elements) {
Sean Paulac874152016-03-10 16:00:26 -0500598 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
599 return HWC2::Error::None;
600 }
601
602 layers[num_layers - 1] = l.first;
603 fences[num_layers - 1] = l.second.take_release_fence();
604 }
605 *num_elements = num_layers;
606 return HWC2::Error::None;
607}
608
Matteo Franchinc56eede2019-12-03 17:10:38 +0000609void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(int fd) {
Sean Paulac874152016-03-10 16:00:26 -0500610 if (fd < 0)
611 return;
612
Matteo Franchinc56eede2019-12-03 17:10:38 +0000613 if (present_fence_.get() >= 0) {
614 int old_fence = present_fence_.get();
615 present_fence_.Set(sync_merge("dc_present", old_fence, fd));
616 close(fd);
Sean Paulac874152016-03-10 16:00:26 -0500617 } else {
Matteo Franchinc56eede2019-12-03 17:10:38 +0000618 present_fence_.Set(fd);
Sean Paulac874152016-03-10 16:00:26 -0500619 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500620}
621
Roman Stratiienkoafb36892019-11-08 17:16:11 +0200622bool DrmHwcTwo::HwcDisplay::HardwareSupportsLayerType(
623 HWC2::Composition comp_type) {
624 return comp_type == HWC2::Composition::Device ||
625 comp_type == HWC2::Composition::Cursor;
626}
627
Rob Herring4f6c62e2018-05-17 14:33:02 -0500628HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500629 std::vector<DrmCompositionDisplayLayersMap> layers_map;
630 layers_map.emplace_back();
631 DrmCompositionDisplayLayersMap &map = layers_map.back();
632
633 map.display = static_cast<int>(handle_);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200634 map.geometry_changed = true; // TODO(nobody): Fix this
Sean Paulac874152016-03-10 16:00:26 -0500635
636 // order the layers by z-order
637 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100638 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500639 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
640 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200641 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500642 case HWC2::Composition::Device:
643 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
644 break;
645 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100646 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500647 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100648 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500649 break;
650 default:
651 continue;
652 }
653 }
654 if (use_client_layer)
655 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
656
Rob Herring4f6c62e2018-05-17 14:33:02 -0500657 if (z_map.empty())
658 return HWC2::Error::BadLayer;
659
Sean Paulac874152016-03-10 16:00:26 -0500660 // now that they're ordered by z, add them to the composition
661 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
662 DrmHwcLayer layer;
663 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200664 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500665 if (ret) {
666 ALOGE("Failed to import layer, ret=%d", ret);
667 return HWC2::Error::NoResources;
668 }
669 map.layers.emplace_back(std::move(layer));
670 }
Sean Paulac874152016-03-10 16:00:26 -0500671
Sean Paulf72cccd2018-08-27 13:59:08 -0400672 std::unique_ptr<DrmDisplayComposition> composition = compositor_
673 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500674 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
675
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200676 // TODO(nobody): Don't always assume geometry changed
Sean Paulac874152016-03-10 16:00:26 -0500677 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
678 if (ret) {
679 ALOGE("Failed to set layers in the composition ret=%d", ret);
680 return HWC2::Error::BadLayer;
681 }
682
683 std::vector<DrmPlane *> primary_planes(primary_planes_);
684 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500685 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500686 if (ret) {
687 ALOGE("Failed to plan the composition ret=%d", ret);
688 return HWC2::Error::BadConfig;
689 }
690
691 // Disable the planes we're not using
692 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
693 composition->AddPlaneDisable(*i);
694 i = primary_planes.erase(i);
695 }
696 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
697 composition->AddPlaneDisable(*i);
698 i = overlay_planes.erase(i);
699 }
700
Rob Herring4f6c62e2018-05-17 14:33:02 -0500701 if (test) {
702 ret = compositor_.TestComposition(composition.get());
703 } else {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500704 ret = compositor_.ApplyComposition(std::move(composition));
Matteo Franchinc56eede2019-12-03 17:10:38 +0000705 AddFenceToPresentFence(compositor_.TakeOutFence());
Rob Herring4f6c62e2018-05-17 14:33:02 -0500706 }
Sean Paulac874152016-03-10 16:00:26 -0500707 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700708 if (!test)
709 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500710 return HWC2::Error::BadParameter;
711 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500712 return HWC2::Error::None;
713}
714
Matteo Franchinc56eede2019-12-03 17:10:38 +0000715HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500716 supported(__func__);
717 HWC2::Error ret;
718
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200719 ++total_stats_.total_frames_;
720
Rob Herring4f6c62e2018-05-17 14:33:02 -0500721 ret = CreateComposition(false);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200722 if (ret != HWC2::Error::None)
723 ++total_stats_.failed_kms_present_;
724
Rob Herring4f6c62e2018-05-17 14:33:02 -0500725 if (ret == HWC2::Error::BadLayer) {
726 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000727 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500728 return HWC2::Error::None;
729 }
730 if (ret != HWC2::Error::None)
731 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500732
Matteo Franchinc56eede2019-12-03 17:10:38 +0000733 *present_fence = present_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500734
735 ++frame_no_;
736 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500737}
738
739HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500740 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400741 auto mode = std::find_if(connector_->modes().begin(),
742 connector_->modes().end(),
743 [config](DrmMode const &m) {
744 return m.id() == config;
745 });
Sean Paulac874152016-03-10 16:00:26 -0500746 if (mode == connector_->modes().end()) {
747 ALOGE("Could not find active mode for %d", config);
748 return HWC2::Error::BadConfig;
749 }
750
Sean Paulf72cccd2018-08-27 13:59:08 -0400751 std::unique_ptr<DrmDisplayComposition> composition = compositor_
752 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500753 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
754 int ret = composition->SetDisplayMode(*mode);
Roman Stratiienko6a10c4c2021-02-15 11:25:23 +0200755 if (ret) {
756 return HWC2::Error::BadConfig;
757 }
Sean Pauled45a8e2017-02-28 13:17:34 -0500758 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500759 if (ret) {
760 ALOGE("Failed to queue dpms composition on %d", ret);
761 return HWC2::Error::BadConfig;
762 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300763
764 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500765
766 // Setup the client layer's dimensions
767 hwc_rect_t display_frame = {.left = 0,
768 .top = 0,
769 .right = static_cast<int>(mode->h_display()),
770 .bottom = static_cast<int>(mode->v_display())};
771 client_layer_.SetLayerDisplayFrame(display_frame);
Sean Paulac874152016-03-10 16:00:26 -0500772
773 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500774}
775
776HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
777 int32_t acquire_fence,
778 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600779 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500780 supported(__func__);
781 UniqueFd uf(acquire_fence);
782
783 client_layer_.set_buffer(target);
784 client_layer_.set_acquire_fence(uf.get());
785 client_layer_.SetLayerDataspace(dataspace);
Roman Stratiienko33365c22020-10-10 23:06:36 +0300786
787 /* TODO: Do not update source_crop every call.
788 * It makes sense to do it once after every hotplug event. */
789 hwc_drm_bo bo{};
790 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
791
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200792 hwc_frect_t source_crop = {.left = 0.0F,
793 .top = 0.0F,
794 .right = bo.width + 0.0F,
795 .bottom = bo.height + 0.0F};
Roman Stratiienko33365c22020-10-10 23:06:36 +0300796 client_layer_.SetLayerSourceCrop(source_crop);
797
Sean Paulac874152016-03-10 16:00:26 -0500798 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500799}
800
801HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500802 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800803
Roman Stratiienkod146d6d2020-10-04 23:56:46 +0300804 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
805 return HWC2::Error::BadParameter;
806
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800807 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +0300808 return HWC2::Error::Unsupported;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800809
810 color_mode_ = mode;
811 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500812}
813
814HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500815 int32_t hint) {
816 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200817 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
818 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
819 return HWC2::Error::BadParameter;
820
821 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
822 return HWC2::Error::BadParameter;
823
824 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
825 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
826 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
827
828 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500829}
830
831HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500832 int32_t release_fence) {
833 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200834 // TODO(nobody): Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500835 return unsupported(__func__, buffer, release_fence);
836}
837
Sean Paulac874152016-03-10 16:00:26 -0500838HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
839 supported(__func__);
840 uint64_t dpms_value = 0;
841 auto mode = static_cast<HWC2::PowerMode>(mode_in);
842 switch (mode) {
843 case HWC2::PowerMode::Off:
844 dpms_value = DRM_MODE_DPMS_OFF;
845 break;
846 case HWC2::PowerMode::On:
847 dpms_value = DRM_MODE_DPMS_ON;
848 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100849 case HWC2::PowerMode::Doze:
850 case HWC2::PowerMode::DozeSuspend:
851 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500852 default:
853 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100854 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500855 };
856
Sean Paulf72cccd2018-08-27 13:59:08 -0400857 std::unique_ptr<DrmDisplayComposition> composition = compositor_
858 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500859 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
860 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500861 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500862 if (ret) {
863 ALOGE("Failed to apply the dpms composition ret=%d", ret);
864 return HWC2::Error::BadParameter;
865 }
866 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500867}
868
869HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500870 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300871 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500872 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500873}
874
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200875uint32_t DrmHwcTwo::HwcDisplay::CalcPixOps(
876 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t first_z,
877 size_t size) {
878 uint32_t pixops = 0;
879 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
880 if (l.first >= first_z && l.first < first_z + size) {
881 hwc_rect_t df = l.second->display_frame();
882 pixops += (df.right - df.left) * (df.bottom - df.top);
883 }
884 }
885 return pixops;
886}
887
888void DrmHwcTwo::HwcDisplay::MarkValidated(
889 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t client_first_z,
890 size_t client_size) {
891 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
892 if (l.first >= client_first_z && l.first < client_first_z + client_size)
893 l.second->set_validated_type(HWC2::Composition::Client);
894 else
895 l.second->set_validated_type(HWC2::Composition::Device);
896 }
897}
898
Sean Pauled2ec4b2016-03-10 15:35:40 -0500899HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500900 uint32_t *num_requests) {
901 supported(__func__);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500902
Matvii Zorinef3c7972020-08-11 15:15:44 +0300903 return backend_->ValidateDisplay(this, num_types, num_requests);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500904}
905
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300906#if PLATFORM_SDK_VERSION > 29
907HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
908 if (connector_->internal())
909 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
910 else if (connector_->external())
911 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
912 else
913 return HWC2::Error::BadConfig;
914
915 return HWC2::Error::None;
916}
917
918HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayVsyncPeriod(
919 hwc2_vsync_period_t *outVsyncPeriod /* ns */) {
920 supported(__func__);
921 DrmMode const &mode = connector_->active_mode();
922 if (mode.id() == 0)
923 return HWC2::Error::BadConfig;
924
925 *outVsyncPeriod = 1E9 / mode.v_refresh();
926 return HWC2::Error::None;
927}
928
929HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfigWithConstraints(
930 hwc2_config_t /*config*/,
931 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
932 hwc_vsync_period_change_timeline_t *outTimeline) {
933 supported(__func__);
934
935 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
936 return HWC2::Error::BadParameter;
937 }
938
939 return HWC2::Error::BadConfig;
940}
941
942HWC2::Error DrmHwcTwo::HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
943 return HWC2::Error::Unsupported;
944}
945
946HWC2::Error DrmHwcTwo::HwcDisplay::GetSupportedContentTypes(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200947 uint32_t *outNumSupportedContentTypes,
948 const uint32_t *outSupportedContentTypes) {
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300949 if (outSupportedContentTypes == nullptr)
950 *outNumSupportedContentTypes = 0;
951
952 return HWC2::Error::None;
953}
954
955HWC2::Error DrmHwcTwo::HwcDisplay::SetContentType(int32_t contentType) {
956 supported(__func__);
957
958 if (contentType != HWC2_CONTENT_TYPE_NONE)
959 return HWC2::Error::Unsupported;
960
961 /* TODO: Map to the DRM Connector property:
962 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
963 */
964
965 return HWC2::Error::None;
966}
967#endif
968
John Stultz8c7229d2020-02-07 21:31:08 +0000969#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800970HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
971 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
972 supported(__func__);
973
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200974 drmModePropertyBlobPtr blob = nullptr;
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800975
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300976 if (connector_->GetEdidBlob(blob)) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800977 ALOGE("Failed to get edid property value.");
978 return HWC2::Error::Unsupported;
979 }
980
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +0300981 if (outData) {
982 *outDataSize = std::min(*outDataSize, blob->length);
983 memcpy(outData, blob->data, *outDataSize);
984 } else {
985 *outDataSize = blob->length;
986 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800987 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800988
989 return HWC2::Error::None;
990}
991
992HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
993 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
994 unsupported(__func__, outCapabilities);
995
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200996 if (outNumCapabilities == nullptr) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800997 return HWC2::Error::BadParameter;
998 }
999
1000 *outNumCapabilities = 0;
1001
1002 return HWC2::Error::None;
1003}
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001004
1005HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
1006 bool *supported) {
1007 *supported = false;
1008 return HWC2::Error::None;
1009}
1010
1011HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
1012 float /* brightness */) {
1013 return HWC2::Error::Unsupported;
1014}
1015
John Stultz8c7229d2020-02-07 21:31:08 +00001016#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001017
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001018#if PLATFORM_SDK_VERSION > 27
1019
1020HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
1021 int32_t mode, uint32_t *outNumIntents,
1022 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1023 if (mode != HAL_COLOR_MODE_NATIVE) {
1024 return HWC2::Error::BadParameter;
1025 }
1026
1027 if (outIntents == nullptr) {
1028 *outNumIntents = 1;
1029 return HWC2::Error::None;
1030 }
1031 *outNumIntents = 1;
1032 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1033 return HWC2::Error::None;
1034}
1035
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001036HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
1037 int32_t intent) {
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001038 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1039 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1040 return HWC2::Error::BadParameter;
1041
1042 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
1043 return HWC2::Error::BadParameter;
1044
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001045 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001046 return HWC2::Error::Unsupported;
1047
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001048 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001049 return HWC2::Error::Unsupported;
1050
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001051 color_mode_ = mode;
1052 return HWC2::Error::None;
1053}
1054
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001055#endif /* PLATFORM_SDK_VERSION > 27 */
1056
Sean Pauled2ec4b2016-03-10 15:35:40 -05001057HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -05001058 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001059 cursor_x_ = x;
1060 cursor_y_ = y;
1061 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001062}
1063
1064HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001065 supported(__func__);
1066 blending_ = static_cast<HWC2::BlendMode>(mode);
1067 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001068}
1069
1070HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001071 int32_t acquire_fence) {
1072 supported(__func__);
1073 UniqueFd uf(acquire_fence);
1074
Sean Paulac874152016-03-10 16:00:26 -05001075 set_buffer(buffer);
1076 set_acquire_fence(uf.get());
1077 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001078}
1079
1080HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001081 // TODO(nobody): Put to client composition here?
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001082 supported(__func__);
1083 layer_color_ = color;
1084 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001085}
1086
1087HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001088 sf_type_ = static_cast<HWC2::Composition>(type);
1089 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001090}
1091
1092HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001093 supported(__func__);
1094 dataspace_ = static_cast<android_dataspace_t>(dataspace);
1095 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001096}
1097
1098HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001099 supported(__func__);
1100 display_frame_ = frame;
1101 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001102}
1103
1104HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001105 supported(__func__);
1106 alpha_ = alpha;
1107 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001108}
1109
1110HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1111 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001112 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001113 // TODO(nobody): We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001114 return unsupported(__func__, stream);
1115}
1116
1117HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001118 supported(__func__);
1119 source_crop_ = crop;
1120 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001121}
1122
1123HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001124 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001125 // TODO(nobody): We don't use surface damage, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001126 unsupported(__func__, damage);
1127 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001128}
1129
1130HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001131 supported(__func__);
1132 transform_ = static_cast<HWC2::Transform>(transform);
1133 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001134}
1135
1136HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001137 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001138 // TODO(nobody): We don't use this information, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001139 unsupported(__func__, visible);
1140 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001141}
1142
Sean Paulac874152016-03-10 16:00:26 -05001143HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1144 supported(__func__);
1145 z_order_ = order;
1146 return HWC2::Error::None;
1147}
1148
1149void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1150 supported(__func__);
1151 switch (blending_) {
1152 case HWC2::BlendMode::None:
1153 layer->blending = DrmHwcBlending::kNone;
1154 break;
1155 case HWC2::BlendMode::Premultiplied:
1156 layer->blending = DrmHwcBlending::kPreMult;
1157 break;
1158 case HWC2::BlendMode::Coverage:
1159 layer->blending = DrmHwcBlending::kCoverage;
1160 break;
1161 default:
1162 ALOGE("Unknown blending mode b=%d", blending_);
1163 layer->blending = DrmHwcBlending::kNone;
1164 break;
1165 }
1166
1167 OutputFd release_fence = release_fence_output();
1168
1169 layer->sf_handle = buffer_;
1170 layer->acquire_fence = acquire_fence_.Release();
1171 layer->release_fence = std::move(release_fence);
1172 layer->SetDisplayFrame(display_frame_);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001173 layer->alpha = lround(65535.0F * alpha_);
Sean Paulac874152016-03-10 16:00:26 -05001174 layer->SetSourceCrop(source_crop_);
1175 layer->SetTransform(static_cast<int32_t>(transform_));
Matvii Zorin8338c342020-09-08 16:12:51 +03001176 layer->dataspace = dataspace_;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001177}
1178
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001179void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
Roman Stratiienko23701092020-09-26 02:08:41 +03001180 const std::lock_guard<std::mutex> lock(hotplug_callback_lock);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001181
Roman Stratiienko23701092020-09-26 02:08:41 +03001182 if (hotplug_callback_hook_ && hotplug_callback_data_)
1183 hotplug_callback_hook_(hotplug_callback_data_, displayid,
1184 state == DRM_MODE_CONNECTED
1185 ? HWC2_CONNECTION_CONNECTED
1186 : HWC2_CONNECTION_DISCONNECTED);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001187}
1188
1189void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
1190 for (auto &conn : drmDevice->connectors()) {
1191 if (conn->state() != DRM_MODE_CONNECTED)
1192 continue;
1193 HandleDisplayHotplug(conn->display(), conn->state());
1194 }
1195}
1196
1197void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
1198 for (auto &conn : drm_->connectors()) {
1199 drmModeConnection old_state = conn->state();
1200 drmModeConnection cur_state = conn->UpdateModes()
1201 ? DRM_MODE_UNKNOWNCONNECTION
1202 : conn->state();
1203
1204 if (cur_state == old_state)
1205 continue;
1206
1207 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1208 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1209 conn->id(), conn->display());
1210
1211 int display_id = conn->display();
1212 if (cur_state == DRM_MODE_CONNECTED) {
1213 auto &display = hwc2_->displays_.at(display_id);
1214 display.ChosePreferredConfig();
1215 } else {
1216 auto &display = hwc2_->displays_.at(display_id);
1217 display.ClearDisplay();
1218 }
1219
1220 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1221 }
1222}
1223
Sean Pauled2ec4b2016-03-10 15:35:40 -05001224// static
1225int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1226 unsupported(__func__);
1227 return 0;
1228}
1229
1230// static
1231void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001232 uint32_t *out_count,
1233 int32_t * /*out_capabilities*/) {
1234 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001235 *out_count = 0;
1236}
1237
1238// static
Sean Paulac874152016-03-10 16:00:26 -05001239hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1240 struct hwc2_device * /*dev*/, int32_t descriptor) {
1241 supported(__func__);
1242 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001243 switch (func) {
1244 // Device functions
1245 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1246 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1247 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1248 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001249 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001250 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1251 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1252 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1253 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1254 case HWC2::FunctionDescriptor::Dump:
1255 return ToHook<HWC2_PFN_DUMP>(
1256 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1257 uint32_t *, char *>);
1258 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1259 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1260 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1261 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1262 case HWC2::FunctionDescriptor::RegisterCallback:
1263 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1264 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1265 &DrmHwcTwo::RegisterCallback, int32_t,
1266 hwc2_callback_data_t, hwc2_function_pointer_t>);
1267
1268 // Display functions
1269 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1270 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1271 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1272 &HwcDisplay::AcceptDisplayChanges>);
1273 case HWC2::FunctionDescriptor::CreateLayer:
1274 return ToHook<HWC2_PFN_CREATE_LAYER>(
1275 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1276 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1277 case HWC2::FunctionDescriptor::DestroyLayer:
1278 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1279 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1280 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1281 case HWC2::FunctionDescriptor::GetActiveConfig:
1282 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1283 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1284 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1285 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1286 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1287 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1288 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1289 hwc2_layer_t *, int32_t *>);
1290 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1291 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1292 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1293 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1294 int32_t, int32_t>);
1295 case HWC2::FunctionDescriptor::GetColorModes:
1296 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1297 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1298 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1299 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001300 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1301 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1302 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1303 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001304 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001305 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1306 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1307 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1308 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001309 case HWC2::FunctionDescriptor::GetDisplayName:
1310 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1311 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1312 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1313 case HWC2::FunctionDescriptor::GetDisplayRequests:
1314 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1315 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1316 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1317 hwc2_layer_t *, int32_t *>);
1318 case HWC2::FunctionDescriptor::GetDisplayType:
1319 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1320 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1321 &HwcDisplay::GetDisplayType, int32_t *>);
1322 case HWC2::FunctionDescriptor::GetDozeSupport:
1323 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1324 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1325 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001326 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1327 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1328 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1329 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1330 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001331 case HWC2::FunctionDescriptor::GetReleaseFences:
1332 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1333 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1334 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1335 int32_t *>);
1336 case HWC2::FunctionDescriptor::PresentDisplay:
1337 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1338 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1339 &HwcDisplay::PresentDisplay, int32_t *>);
1340 case HWC2::FunctionDescriptor::SetActiveConfig:
1341 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1342 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1343 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1344 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001345 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1346 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1347 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1348 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001349 case HWC2::FunctionDescriptor::SetColorMode:
1350 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1351 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1352 &HwcDisplay::SetColorMode, int32_t>);
1353 case HWC2::FunctionDescriptor::SetColorTransform:
1354 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1355 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1356 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1357 case HWC2::FunctionDescriptor::SetOutputBuffer:
1358 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1359 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1360 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1361 case HWC2::FunctionDescriptor::SetPowerMode:
1362 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1363 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1364 &HwcDisplay::SetPowerMode, int32_t>);
1365 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1366 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1367 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1368 &HwcDisplay::SetVsyncEnabled, int32_t>);
1369 case HWC2::FunctionDescriptor::ValidateDisplay:
1370 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1371 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1372 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001373#if PLATFORM_SDK_VERSION > 27
1374 case HWC2::FunctionDescriptor::GetRenderIntents:
1375 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1376 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1377 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1378 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001379 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1380 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1381 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1382 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001383#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001384#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001385 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1386 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1387 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1388 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1389 uint32_t *, uint8_t *>);
1390 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1391 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1392 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1393 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1394 uint32_t *>);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001395 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1396 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1397 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1398 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1399 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1400 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1401 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1402 &HwcDisplay::SetDisplayBrightness, float>);
John Stultz8c7229d2020-02-07 21:31:08 +00001403#endif /* PLATFORM_SDK_VERSION > 28 */
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001404#if PLATFORM_SDK_VERSION > 29
1405 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
1406 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
1407 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
1408 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
1409 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
1410 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
1411 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
1412 &HwcDisplay::GetDisplayVsyncPeriod,
1413 hwc2_vsync_period_t *>);
1414 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
1415 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
1416 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
1417 &HwcDisplay::SetActiveConfigWithConstraints,
1418 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
1419 hwc_vsync_period_change_timeline_t *>);
1420 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
1421 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
1422 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
1423 &HwcDisplay::SetAutoLowLatencyMode, bool>);
1424 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
1425 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
1426 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
1427 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
1428 uint32_t *>);
1429 case HWC2::FunctionDescriptor::SetContentType:
1430 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
1431 DisplayHook<decltype(&HwcDisplay::SetContentType),
1432 &HwcDisplay::SetContentType, int32_t>);
1433#endif
Sean Pauled2ec4b2016-03-10 15:35:40 -05001434 // Layer functions
1435 case HWC2::FunctionDescriptor::SetCursorPosition:
1436 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1437 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1438 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1439 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1440 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1441 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1442 &HwcLayer::SetLayerBlendMode, int32_t>);
1443 case HWC2::FunctionDescriptor::SetLayerBuffer:
1444 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1445 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1446 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1447 case HWC2::FunctionDescriptor::SetLayerColor:
1448 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1449 LayerHook<decltype(&HwcLayer::SetLayerColor),
1450 &HwcLayer::SetLayerColor, hwc_color_t>);
1451 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1452 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1453 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1454 &HwcLayer::SetLayerCompositionType, int32_t>);
1455 case HWC2::FunctionDescriptor::SetLayerDataspace:
1456 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1457 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1458 &HwcLayer::SetLayerDataspace, int32_t>);
1459 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1460 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1461 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1462 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1463 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1464 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1465 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1466 &HwcLayer::SetLayerPlaneAlpha, float>);
1467 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001468 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1469 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1470 &HwcLayer::SetLayerSidebandStream,
1471 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001472 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1473 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1474 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1475 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1476 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1477 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1478 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1479 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1480 case HWC2::FunctionDescriptor::SetLayerTransform:
1481 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1482 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1483 &HwcLayer::SetLayerTransform, int32_t>);
1484 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1485 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1486 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1487 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1488 case HWC2::FunctionDescriptor::SetLayerZOrder:
1489 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1490 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1491 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001492 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001493 default:
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001494 return nullptr;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001495 }
1496}
Sean Paulac874152016-03-10 16:00:26 -05001497
1498// static
1499int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1500 struct hw_device_t **dev) {
1501 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001502 if (strcmp(name, HWC_HARDWARE_COMPOSER) != 0) {
Sean Paulac874152016-03-10 16:00:26 -05001503 ALOGE("Invalid module name- %s", name);
1504 return -EINVAL;
1505 }
1506
1507 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1508 if (!ctx) {
1509 ALOGE("Failed to allocate DrmHwcTwo");
1510 return -ENOMEM;
1511 }
1512
1513 HWC2::Error err = ctx->Init();
1514 if (err != HWC2::Error::None) {
1515 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1516 return -EINVAL;
1517 }
1518
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001519 ctx->common.module = (hw_module_t *)module;
Sean Paulac874152016-03-10 16:00:26 -05001520 *dev = &ctx->common;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001521 ctx.release(); // NOLINT(bugprone-unused-return-value)
Sean Paulac874152016-03-10 16:00:26 -05001522 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001523}
Sean Paulf72cccd2018-08-27 13:59:08 -04001524} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001525
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001526// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001527static struct hw_module_methods_t hwc2_module_methods = {
1528 .open = android::DrmHwcTwo::HookDevOpen,
1529};
1530
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001531// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001532hw_module_t HAL_MODULE_INFO_SYM = {
1533 .tag = HARDWARE_MODULE_TAG,
1534 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1535 .id = HWC_HARDWARE_MODULE_ID,
1536 .name = "DrmHwcTwo module",
1537 .author = "The Android Open Source Project",
1538 .methods = &hwc2_module_methods,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001539 .dso = nullptr,
Sean Paulac874152016-03-10 16:00:26 -05001540 .reserved = {0},
1541};