blob: 694a7c5dbce7310d343bb37c4227c0bd7e03269f [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>
Sean Pauled2ec4b2016-03-10 15:35:40 -050026
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020027#include <cinttypes>
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030028#include <string>
29
Roman Stratiienko13cc3662020-08-29 21:35:39 +030030#include "backend/BackendManager.h"
Roman Stratiienko33365c22020-10-10 23:06:36 +030031#include "bufferinfo/BufferInfoGetter.h"
Roman Stratiienko13cc3662020-08-29 21:35:39 +030032#include "compositor/DrmDisplayComposition.h"
Roman Stratiienkoaa3cd542020-08-29 11:26:16 +030033
Sean Pauled2ec4b2016-03-10 15:35:40 -050034namespace android {
35
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020036DrmHwcTwo::DrmHwcTwo() : hwc2_device() {
Sean Paulac874152016-03-10 16:00:26 -050037 common.tag = HARDWARE_DEVICE_TAG;
38 common.version = HWC_DEVICE_API_VERSION_2_0;
Sean Pauled2ec4b2016-03-10 15:35:40 -050039 common.close = HookDevClose;
40 getCapabilities = HookDevGetCapabilities;
41 getFunction = HookDevGetFunction;
42}
43
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030044HWC2::Error DrmHwcTwo::CreateDisplay(hwc2_display_t displ,
45 HWC2::DisplayType type) {
46 DrmDevice *drm = resource_manager_.GetDrmDevice(displ);
47 std::shared_ptr<Importer> importer = resource_manager_.GetImporter(displ);
Alexandru Gheorghec5463582018-03-27 15:52:02 +010048 if (!drm || !importer) {
49 ALOGE("Failed to get a valid drmresource and importer");
Sean Paulac874152016-03-10 16:00:26 -050050 return HWC2::Error::NoResources;
51 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030052 displays_.emplace(std::piecewise_construct, std::forward_as_tuple(displ),
Sean Paulf72cccd2018-08-27 13:59:08 -040053 std::forward_as_tuple(&resource_manager_, drm, importer,
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030054 displ, type));
Sean Paulac874152016-03-10 16:00:26 -050055
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030056 DrmCrtc *crtc = drm->GetCrtcForDisplay(static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050057 if (!crtc) {
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030058 ALOGE("Failed to get crtc for display %d", static_cast<int>(displ));
Sean Paulac874152016-03-10 16:00:26 -050059 return HWC2::Error::BadDisplay;
60 }
Sean Paulac874152016-03-10 16:00:26 -050061 std::vector<DrmPlane *> display_planes;
Alexandru Gheorghec5463582018-03-27 15:52:02 +010062 for (auto &plane : drm->planes()) {
Sean Paulac874152016-03-10 16:00:26 -050063 if (plane->GetCrtcSupported(*crtc))
64 display_planes.push_back(plane.get());
65 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030066 displays_.at(displ).Init(&display_planes);
Sean Paulac874152016-03-10 16:00:26 -050067 return HWC2::Error::None;
68}
69
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030070HWC2::Error DrmHwcTwo::Init() {
71 int rv = resource_manager_.Init();
72 if (rv) {
73 ALOGE("Can't initialize the resource manager %d", rv);
74 return HWC2::Error::NoResources;
75 }
76
77 HWC2::Error ret = HWC2::Error::None;
78 for (int i = 0; i < resource_manager_.getDisplayCount(); i++) {
79 ret = CreateDisplay(i, HWC2::DisplayType::Physical);
80 if (ret != HWC2::Error::None) {
81 ALOGE("Failed to create display %d with error %d", i, ret);
82 return ret;
83 }
84 }
85
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +020086 auto &drm_devices = resource_manager_.getDrmDevices();
87 for (auto &device : drm_devices) {
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +020088 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +030089 device->RegisterHotplugHandler(new DrmHotplugHandler(this, device.get()));
90 }
91 return ret;
92}
93
Sean Pauled2ec4b2016-03-10 15:35:40 -050094template <typename... Args>
95static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
96 ALOGV("Unsupported function: %s", func);
97 return HWC2::Error::Unsupported;
98}
99
Sean Paulac874152016-03-10 16:00:26 -0500100static inline void supported(char const *func) {
101 ALOGV("Supported function: %s", func);
102}
103
Sean Pauled2ec4b2016-03-10 15:35:40 -0500104HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t width, uint32_t height,
105 int32_t *format,
106 hwc2_display_t *display) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200107 // TODO(nobody): Implement virtual display
Sean Paulac874152016-03-10 16:00:26 -0500108 return unsupported(__func__, width, height, format, display);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500109}
110
111HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t display) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200112 // TODO(nobody): Implement virtual display
Sean Pauled2ec4b2016-03-10 15:35:40 -0500113 return unsupported(__func__, display);
114}
115
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200116std::string DrmHwcTwo::HwcDisplay::DumpDelta(
117 DrmHwcTwo::HwcDisplay::Stats delta) {
118 if (delta.total_pixops_ == 0)
119 return "No stats yet";
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200120 double ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200121
122 return (std::stringstream()
123 << " Total frames count: " << delta.total_frames_ << "\n"
124 << " Failed to test commit frames: " << delta.failed_kms_validate_
125 << "\n"
126 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
127 << ((delta.failed_kms_present_ > 0)
128 ? " !!! Internal failure, FIX it please\n"
129 : "")
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200130 << " Flattened frames: " << delta.frames_flattened_ << "\n"
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200131 << " Pixel operations (free units)"
132 << " : [TOTAL: " << delta.total_pixops_
133 << " / GPU: " << delta.gpu_pixops_ << "]\n"
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200134 << " Composition efficiency: " << ratio)
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200135 .str();
136}
137
138std::string DrmHwcTwo::HwcDisplay::Dump() {
139 auto out = (std::stringstream()
140 << "- Display on: " << connector_->name() << "\n"
Roman Kovalivskyi9170b312020-02-03 18:13:57 +0200141 << " Flattening state: " << compositor_.GetFlatteningState()
142 << "\n"
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200143 << "Statistics since system boot:\n"
144 << DumpDelta(total_stats_) << "\n\n"
145 << "Statistics since last dumpsys request:\n"
146 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n")
147 .str();
148
149 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
150 return out;
151}
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 Stratiienkoe2f2c922021-02-13 10:57:47 +0200187 auto &drm_devices = resource_manager_.getDrmDevices();
188 for (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,
211 DrmDevice *drm,
Sean Paulac874152016-03-10 16:00:26 -0500212 std::shared_ptr<Importer> importer,
Sean Paulac874152016-03-10 16:00:26 -0500213 hwc2_display_t handle, HWC2::DisplayType type)
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100214 : resource_manager_(resource_manager),
215 drm_(drm),
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200216 importer_(std::move(importer)),
Alexandru Gheorghe6f0030f2018-05-01 17:25:48 +0100217 handle_(handle),
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200218 type_(type),
219 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
Sean Paulac874152016-03-10 16:00:26 -0500220 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200221
222 // clang-format off
223 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
224 0.0, 1.0, 0.0, 0.0,
225 0.0, 0.0, 1.0, 0.0,
226 0.0, 0.0, 0.0, 1.0};
227 // clang-format on
Sean Paulac874152016-03-10 16:00:26 -0500228}
229
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300230void DrmHwcTwo::HwcDisplay::ClearDisplay() {
231 compositor_.ClearDisplay();
232}
233
Sean Paulac874152016-03-10 16:00:26 -0500234HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
235 supported(__func__);
236 planner_ = Planner::CreateInstance(drm_);
237 if (!planner_) {
238 ALOGE("Failed to create planner instance for composition");
239 return HWC2::Error::NoResources;
240 }
241
242 int display = static_cast<int>(handle_);
Alexandru Gheorghe62e2d2c2018-05-11 11:40:53 +0100243 int ret = compositor_.Init(resource_manager_, display);
Sean Paulac874152016-03-10 16:00:26 -0500244 if (ret) {
245 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
246 return HWC2::Error::NoResources;
247 }
248
249 // Split up the given display planes into primary and overlay to properly
250 // interface with the composition
251 char use_overlay_planes_prop[PROPERTY_VALUE_MAX];
Jason Macnakf1af9572020-08-20 11:49:51 -0700252 property_get("vendor.hwc.drm.use_overlay_planes", use_overlay_planes_prop,
253 "1");
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200254 bool use_overlay_planes = strtol(use_overlay_planes_prop, nullptr, 10);
Sean Paulac874152016-03-10 16:00:26 -0500255 for (auto &plane : *planes) {
256 if (plane->type() == DRM_PLANE_TYPE_PRIMARY)
257 primary_planes_.push_back(plane);
258 else if (use_overlay_planes && (plane)->type() == DRM_PLANE_TYPE_OVERLAY)
259 overlay_planes_.push_back(plane);
260 }
261
262 crtc_ = drm_->GetCrtcForDisplay(display);
263 if (!crtc_) {
264 ALOGE("Failed to get crtc for display %d", display);
265 return HWC2::Error::BadDisplay;
266 }
267
268 connector_ = drm_->GetConnectorForDisplay(display);
269 if (!connector_) {
270 ALOGE("Failed to get connector for display %d", display);
271 return HWC2::Error::BadDisplay;
272 }
273
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300274 ret = vsync_worker_.Init(drm_, display);
275 if (ret) {
276 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
277 return HWC2::Error::BadDisplay;
278 }
279
Matvii Zorinef3c7972020-08-11 15:15:44 +0300280 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
281 if (ret) {
282 ALOGE("Failed to set backend for d=%d %d\n", display, ret);
283 return HWC2::Error::BadDisplay;
284 }
285
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300286 return ChosePreferredConfig();
287}
288
289HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() {
Sean Paulac874152016-03-10 16:00:26 -0500290 // Fetch the number of modes from the display
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200291 uint32_t num_configs = 0;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200292 HWC2::Error err = GetDisplayConfigs(&num_configs, nullptr);
Sean Paulac874152016-03-10 16:00:26 -0500293 if (err != HWC2::Error::None || !num_configs)
294 return err;
295
Andrii Chepurnyi1b1e35e2019-02-19 21:38:13 +0200296 return SetActiveConfig(connector_->get_preferred_mode_id());
Sean Paulac874152016-03-10 16:00:26 -0500297}
298
Roman Stratiienko23701092020-09-26 02:08:41 +0300299void DrmHwcTwo::HwcDisplay::RegisterVsyncCallback(
Sean Paulac874152016-03-10 16:00:26 -0500300 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
301 supported(__func__);
Roman Stratiienko23701092020-09-26 02:08:41 +0300302 vsync_worker_.RegisterClientCallback(data, func);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500303}
304
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200305void DrmHwcTwo::HwcDisplay::RegisterRefreshCallback(
306 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
307 supported(__func__);
Roman Stratiienko23701092020-09-26 02:08:41 +0300308 compositor_.SetRefreshCallback(data, func);
Roman Kovalivskyi8fae1562020-01-30 20:20:47 +0200309}
310
Sean Pauled2ec4b2016-03-10 15:35:40 -0500311HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
Sean Paulac874152016-03-10 16:00:26 -0500312 supported(__func__);
Sean Paulac874152016-03-10 16:00:26 -0500313 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
314 l.second.accept_type_change();
315 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500316}
317
318HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
Sean Paulac874152016-03-10 16:00:26 -0500319 supported(__func__);
320 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
321 *layer = static_cast<hwc2_layer_t>(layer_idx_);
322 ++layer_idx_;
323 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500324}
325
326HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
Sean Paulac874152016-03-10 16:00:26 -0500327 supported(__func__);
Vincent Donnefort9abec032019-10-09 15:43:43 +0100328 if (!get_layer(layer))
329 return HWC2::Error::BadLayer;
330
Sean Paulac874152016-03-10 16:00:26 -0500331 layers_.erase(layer);
332 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500333}
334
335HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
Sean Paulac874152016-03-10 16:00:26 -0500336 supported(__func__);
337 DrmMode const &mode = connector_->active_mode();
338 if (mode.id() == 0)
339 return HWC2::Error::BadConfig;
340
341 *config = mode.id();
342 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500343}
344
345HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
346 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
Sean Paulac874152016-03-10 16:00:26 -0500347 supported(__func__);
348 uint32_t num_changes = 0;
349 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
350 if (l.second.type_changed()) {
351 if (layers && num_changes < *num_elements)
352 layers[num_changes] = l.first;
353 if (types && num_changes < *num_elements)
354 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
355 ++num_changes;
356 }
357 }
358 if (!layers && !types)
359 *num_elements = num_changes;
360 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500361}
362
363HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
Sean Paulac874152016-03-10 16:00:26 -0500364 uint32_t height,
365 int32_t /*format*/,
366 int32_t dataspace) {
367 supported(__func__);
368 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
369 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
370
371 if (width < min.first || height < min.second)
372 return HWC2::Error::Unsupported;
373
374 if (width > max.first || height > max.second)
375 return HWC2::Error::Unsupported;
376
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200377 if (dataspace != HAL_DATASPACE_UNKNOWN)
Sean Paulac874152016-03-10 16:00:26 -0500378 return HWC2::Error::Unsupported;
379
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200380 // TODO(nobody): Validate format can be handled by either GL or planes
Sean Paulac874152016-03-10 16:00:26 -0500381 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500382}
383
384HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
Sean Paulac874152016-03-10 16:00:26 -0500385 int32_t *modes) {
386 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800387 if (!modes)
388 *num_modes = 1;
389
390 if (modes)
391 *modes = HAL_COLOR_MODE_NATIVE;
392
393 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500394}
395
396HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
Sean Paulac874152016-03-10 16:00:26 -0500397 int32_t attribute_in,
398 int32_t *value) {
399 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400400 auto mode = std::find_if(connector_->modes().begin(),
401 connector_->modes().end(),
402 [config](DrmMode const &m) {
403 return m.id() == config;
404 });
Sean Paulac874152016-03-10 16:00:26 -0500405 if (mode == connector_->modes().end()) {
406 ALOGE("Could not find active mode for %d", config);
407 return HWC2::Error::BadConfig;
408 }
409
410 static const int32_t kUmPerInch = 25400;
411 uint32_t mm_width = connector_->mm_width();
412 uint32_t mm_height = connector_->mm_height();
413 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
414 switch (attribute) {
415 case HWC2::Attribute::Width:
416 *value = mode->h_display();
417 break;
418 case HWC2::Attribute::Height:
419 *value = mode->v_display();
420 break;
421 case HWC2::Attribute::VsyncPeriod:
422 // in nanoseconds
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200423 *value = 1000.0 * 1000.0 * 1000.0 / mode->v_refresh();
Sean Paulac874152016-03-10 16:00:26 -0500424 break;
425 case HWC2::Attribute::DpiX:
426 // Dots per 1000 inches
427 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
428 break;
429 case HWC2::Attribute::DpiY:
430 // Dots per 1000 inches
431 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
432 break;
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300433#if PLATFORM_SDK_VERSION > 29
434 case HWC2::Attribute::ConfigGroup:
435 *value = 0; /* TODO: Add support for config groups */
436 break;
437#endif
Sean Paulac874152016-03-10 16:00:26 -0500438 default:
439 *value = -1;
440 return HWC2::Error::BadConfig;
441 }
442 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500443}
444
445HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
446 hwc2_config_t *configs) {
Sean Paulac874152016-03-10 16:00:26 -0500447 supported(__func__);
448 // Since this callback is normally invoked twice (once to get the count, and
449 // once to populate configs), we don't really want to read the edid
450 // redundantly. Instead, only update the modes on the first invocation. While
451 // it's possible this will result in stale modes, it'll all come out in the
452 // wash when we try to set the active config later.
453 if (!configs) {
454 int ret = connector_->UpdateModes();
455 if (ret) {
456 ALOGE("Failed to update display modes %d", ret);
457 return HWC2::Error::BadDisplay;
458 }
459 }
460
Neil Armstrongb67d0492019-06-20 09:00:21 +0000461 // Since the upper layers only look at vactive/hactive/refresh, height and
462 // width, it doesn't differentiate interlaced from progressive and other
463 // similar modes. Depending on the order of modes we return to SF, it could
464 // end up choosing a suboptimal configuration and dropping the preferred
465 // mode. To workaround this, don't offer interlaced modes to SF if there is
466 // at least one non-interlaced alternative and only offer a single WxH@R
467 // mode with at least the prefered mode from in DrmConnector::UpdateModes()
468
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200469 // TODO(nobody): Remove the following block of code until AOSP handles all
470 // modes
Neil Armstrongb67d0492019-06-20 09:00:21 +0000471 std::vector<DrmMode> sel_modes;
472
473 // Add the preferred mode first to be sure it's not dropped
474 auto mode = std::find_if(connector_->modes().begin(),
475 connector_->modes().end(), [&](DrmMode const &m) {
476 return m.id() ==
477 connector_->get_preferred_mode_id();
478 });
479 if (mode != connector_->modes().end())
480 sel_modes.push_back(*mode);
481
482 // Add the active mode if different from preferred mode
483 if (connector_->active_mode().id() != connector_->get_preferred_mode_id())
484 sel_modes.push_back(connector_->active_mode());
485
486 // Cycle over the modes and filter out "similar" modes, keeping only the
487 // first ones in the order given by DRM (from CEA ids and timings order)
Sean Paulac874152016-03-10 16:00:26 -0500488 for (const DrmMode &mode : connector_->modes()) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200489 // TODO(nobody): Remove this when 3D Attributes are in AOSP
Neil Armstrongb67d0492019-06-20 09:00:21 +0000490 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
491 continue;
492
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200493 // TODO(nobody): Remove this when the Interlaced attribute is in AOSP
Neil Armstrong4c027a72019-06-04 14:48:02 +0000494 if (mode.flags() & DRM_MODE_FLAG_INTERLACE) {
495 auto m = std::find_if(connector_->modes().begin(),
496 connector_->modes().end(),
497 [&mode](DrmMode const &m) {
498 return !(m.flags() & DRM_MODE_FLAG_INTERLACE) &&
499 m.h_display() == mode.h_display() &&
500 m.v_display() == mode.v_display();
501 });
Neil Armstrongb67d0492019-06-20 09:00:21 +0000502 if (m == connector_->modes().end())
503 sel_modes.push_back(mode);
504
505 continue;
Neil Armstrong4c027a72019-06-04 14:48:02 +0000506 }
Neil Armstrongb67d0492019-06-20 09:00:21 +0000507
508 // Search for a similar WxH@R mode in the filtered list and drop it if
509 // another mode with the same WxH@R has already been selected
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200510 // TODO(nobody): Remove this when AOSP handles duplicates modes
Neil Armstrongb67d0492019-06-20 09:00:21 +0000511 auto m = std::find_if(sel_modes.begin(), sel_modes.end(),
512 [&mode](DrmMode const &m) {
513 return m.h_display() == mode.h_display() &&
514 m.v_display() == mode.v_display() &&
515 m.v_refresh() == mode.v_refresh();
516 });
517 if (m == sel_modes.end())
518 sel_modes.push_back(mode);
519 }
520
521 auto num_modes = static_cast<uint32_t>(sel_modes.size());
522 if (!configs) {
523 *num_configs = num_modes;
524 return HWC2::Error::None;
525 }
526
527 uint32_t idx = 0;
528 for (const DrmMode &mode : sel_modes) {
529 if (idx >= *num_configs)
530 break;
531 configs[idx++] = mode.id();
Sean Paulac874152016-03-10 16:00:26 -0500532 }
533 *num_configs = idx;
534 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500535}
536
537HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
Sean Paulac874152016-03-10 16:00:26 -0500538 supported(__func__);
539 std::ostringstream stream;
540 stream << "display-" << connector_->id();
541 std::string string = stream.str();
542 size_t length = string.length();
543 if (!name) {
544 *size = length;
545 return HWC2::Error::None;
546 }
547
548 *size = std::min<uint32_t>(static_cast<uint32_t>(length - 1), *size);
549 strncpy(name, string.c_str(), *size);
550 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500551}
552
Sean Paulac874152016-03-10 16:00:26 -0500553HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
554 uint32_t *num_elements,
555 hwc2_layer_t *layers,
556 int32_t *layer_requests) {
557 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200558 // TODO(nobody): I think virtual display should request
Sean Paulac874152016-03-10 16:00:26 -0500559 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
560 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
561 *num_elements = 0;
562 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500563}
564
565HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
Sean Paulac874152016-03-10 16:00:26 -0500566 supported(__func__);
567 *type = static_cast<int32_t>(type_);
568 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500569}
570
571HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
Sean Paulac874152016-03-10 16:00:26 -0500572 supported(__func__);
573 *support = 0;
574 return HWC2::Error::None;
575}
576
577HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
Sean Paulf72cccd2018-08-27 13:59:08 -0400578 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
579 float * /*max_average_luminance*/, float * /*min_luminance*/) {
Sean Paulac874152016-03-10 16:00:26 -0500580 supported(__func__);
581 *num_types = 0;
582 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500583}
584
585HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
Sean Paulac874152016-03-10 16:00:26 -0500586 hwc2_layer_t *layers,
587 int32_t *fences) {
588 supported(__func__);
589 uint32_t num_layers = 0;
590
591 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
592 ++num_layers;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200593 if (layers == nullptr || fences == nullptr)
Sean Paulac874152016-03-10 16:00:26 -0500594 continue;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200595
596 if (num_layers > *num_elements) {
Sean Paulac874152016-03-10 16:00:26 -0500597 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
598 return HWC2::Error::None;
599 }
600
601 layers[num_layers - 1] = l.first;
602 fences[num_layers - 1] = l.second.take_release_fence();
603 }
604 *num_elements = num_layers;
605 return HWC2::Error::None;
606}
607
Matteo Franchinc56eede2019-12-03 17:10:38 +0000608void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(int fd) {
Sean Paulac874152016-03-10 16:00:26 -0500609 if (fd < 0)
610 return;
611
Matteo Franchinc56eede2019-12-03 17:10:38 +0000612 if (present_fence_.get() >= 0) {
613 int old_fence = present_fence_.get();
614 present_fence_.Set(sync_merge("dc_present", old_fence, fd));
615 close(fd);
Sean Paulac874152016-03-10 16:00:26 -0500616 } else {
Matteo Franchinc56eede2019-12-03 17:10:38 +0000617 present_fence_.Set(fd);
Sean Paulac874152016-03-10 16:00:26 -0500618 }
Sean Pauled2ec4b2016-03-10 15:35:40 -0500619}
620
Roman Stratiienkoafb36892019-11-08 17:16:11 +0200621bool DrmHwcTwo::HwcDisplay::HardwareSupportsLayerType(
622 HWC2::Composition comp_type) {
623 return comp_type == HWC2::Composition::Device ||
624 comp_type == HWC2::Composition::Cursor;
625}
626
Rob Herring4f6c62e2018-05-17 14:33:02 -0500627HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
Sean Paulac874152016-03-10 16:00:26 -0500628 std::vector<DrmCompositionDisplayLayersMap> layers_map;
629 layers_map.emplace_back();
630 DrmCompositionDisplayLayersMap &map = layers_map.back();
631
632 map.display = static_cast<int>(handle_);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200633 map.geometry_changed = true; // TODO(nobody): Fix this
Sean Paulac874152016-03-10 16:00:26 -0500634
635 // order the layers by z-order
636 bool use_client_layer = false;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100637 uint32_t client_z_order = UINT32_MAX;
Sean Paulac874152016-03-10 16:00:26 -0500638 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
639 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
Roman Stratiienkof2647232019-11-21 01:58:35 +0200640 switch (l.second.validated_type()) {
Sean Paulac874152016-03-10 16:00:26 -0500641 case HWC2::Composition::Device:
642 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
643 break;
644 case HWC2::Composition::Client:
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100645 // Place it at the z_order of the lowest client layer
Sean Paulac874152016-03-10 16:00:26 -0500646 use_client_layer = true;
Alexandru Gheorghe1542d292018-06-13 16:46:36 +0100647 client_z_order = std::min(client_z_order, l.second.z_order());
Sean Paulac874152016-03-10 16:00:26 -0500648 break;
649 default:
650 continue;
651 }
652 }
653 if (use_client_layer)
654 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
655
Rob Herring4f6c62e2018-05-17 14:33:02 -0500656 if (z_map.empty())
657 return HWC2::Error::BadLayer;
658
Sean Paulac874152016-03-10 16:00:26 -0500659 // now that they're ordered by z, add them to the composition
660 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
661 DrmHwcLayer layer;
662 l.second->PopulateDrmLayer(&layer);
Andrii Chepurnyidc1278c2018-03-20 19:41:18 +0200663 int ret = layer.ImportBuffer(importer_.get());
Sean Paulac874152016-03-10 16:00:26 -0500664 if (ret) {
665 ALOGE("Failed to import layer, ret=%d", ret);
666 return HWC2::Error::NoResources;
667 }
668 map.layers.emplace_back(std::move(layer));
669 }
Sean Paulac874152016-03-10 16:00:26 -0500670
Sean Paulf72cccd2018-08-27 13:59:08 -0400671 std::unique_ptr<DrmDisplayComposition> composition = compositor_
672 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500673 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
674
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200675 // TODO(nobody): Don't always assume geometry changed
Sean Paulac874152016-03-10 16:00:26 -0500676 int ret = composition->SetLayers(map.layers.data(), map.layers.size(), true);
677 if (ret) {
678 ALOGE("Failed to set layers in the composition ret=%d", ret);
679 return HWC2::Error::BadLayer;
680 }
681
682 std::vector<DrmPlane *> primary_planes(primary_planes_);
683 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
Rob Herringaf0d9752018-05-04 16:34:19 -0500684 ret = composition->Plan(&primary_planes, &overlay_planes);
Sean Paulac874152016-03-10 16:00:26 -0500685 if (ret) {
686 ALOGE("Failed to plan the composition ret=%d", ret);
687 return HWC2::Error::BadConfig;
688 }
689
690 // Disable the planes we're not using
691 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
692 composition->AddPlaneDisable(*i);
693 i = primary_planes.erase(i);
694 }
695 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
696 composition->AddPlaneDisable(*i);
697 i = overlay_planes.erase(i);
698 }
699
Rob Herring4f6c62e2018-05-17 14:33:02 -0500700 if (test) {
701 ret = compositor_.TestComposition(composition.get());
702 } else {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500703 ret = compositor_.ApplyComposition(std::move(composition));
Matteo Franchinc56eede2019-12-03 17:10:38 +0000704 AddFenceToPresentFence(compositor_.TakeOutFence());
Rob Herring4f6c62e2018-05-17 14:33:02 -0500705 }
Sean Paulac874152016-03-10 16:00:26 -0500706 if (ret) {
John Stultz78c9f6c2018-05-24 16:43:35 -0700707 if (!test)
708 ALOGE("Failed to apply the frame composition ret=%d", ret);
Sean Paulac874152016-03-10 16:00:26 -0500709 return HWC2::Error::BadParameter;
710 }
Rob Herring4f6c62e2018-05-17 14:33:02 -0500711 return HWC2::Error::None;
712}
713
Matteo Franchinc56eede2019-12-03 17:10:38 +0000714HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
Rob Herring4f6c62e2018-05-17 14:33:02 -0500715 supported(__func__);
716 HWC2::Error ret;
717
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200718 ++total_stats_.total_frames_;
719
Rob Herring4f6c62e2018-05-17 14:33:02 -0500720 ret = CreateComposition(false);
Roman Stratiienko0d1a2cd2019-11-28 17:51:16 +0200721 if (ret != HWC2::Error::None)
722 ++total_stats_.failed_kms_present_;
723
Rob Herring4f6c62e2018-05-17 14:33:02 -0500724 if (ret == HWC2::Error::BadLayer) {
725 // Can we really have no client or device layers?
Matteo Franchinc56eede2019-12-03 17:10:38 +0000726 *present_fence = -1;
Rob Herring4f6c62e2018-05-17 14:33:02 -0500727 return HWC2::Error::None;
728 }
729 if (ret != HWC2::Error::None)
730 return ret;
Sean Paulac874152016-03-10 16:00:26 -0500731
Matteo Franchinc56eede2019-12-03 17:10:38 +0000732 *present_fence = present_fence_.Release();
Sean Paulac874152016-03-10 16:00:26 -0500733
734 ++frame_no_;
735 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500736}
737
738HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
Sean Paulac874152016-03-10 16:00:26 -0500739 supported(__func__);
Sean Paulf72cccd2018-08-27 13:59:08 -0400740 auto mode = std::find_if(connector_->modes().begin(),
741 connector_->modes().end(),
742 [config](DrmMode const &m) {
743 return m.id() == config;
744 });
Sean Paulac874152016-03-10 16:00:26 -0500745 if (mode == connector_->modes().end()) {
746 ALOGE("Could not find active mode for %d", config);
747 return HWC2::Error::BadConfig;
748 }
749
Sean Paulf72cccd2018-08-27 13:59:08 -0400750 std::unique_ptr<DrmDisplayComposition> composition = compositor_
751 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500752 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
753 int ret = composition->SetDisplayMode(*mode);
Roman Stratiienko6a10c4c2021-02-15 11:25:23 +0200754 if (ret) {
755 return HWC2::Error::BadConfig;
756 }
Sean Pauled45a8e2017-02-28 13:17:34 -0500757 ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500758 if (ret) {
759 ALOGE("Failed to queue dpms composition on %d", ret);
760 return HWC2::Error::BadConfig;
761 }
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +0300762
763 connector_->set_active_mode(*mode);
Sean Paulac874152016-03-10 16:00:26 -0500764
765 // Setup the client layer's dimensions
766 hwc_rect_t display_frame = {.left = 0,
767 .top = 0,
768 .right = static_cast<int>(mode->h_display()),
769 .bottom = static_cast<int>(mode->v_display())};
770 client_layer_.SetLayerDisplayFrame(display_frame);
Sean Paulac874152016-03-10 16:00:26 -0500771
772 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500773}
774
775HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
776 int32_t acquire_fence,
777 int32_t dataspace,
Rob Herring1b2685c2017-11-29 10:19:57 -0600778 hwc_region_t /*damage*/) {
Sean Paulac874152016-03-10 16:00:26 -0500779 supported(__func__);
780 UniqueFd uf(acquire_fence);
781
782 client_layer_.set_buffer(target);
783 client_layer_.set_acquire_fence(uf.get());
784 client_layer_.SetLayerDataspace(dataspace);
Roman Stratiienko33365c22020-10-10 23:06:36 +0300785
786 /* TODO: Do not update source_crop every call.
787 * It makes sense to do it once after every hotplug event. */
788 hwc_drm_bo bo{};
789 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
790
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200791 hwc_frect_t source_crop = {.left = 0.0F,
792 .top = 0.0F,
793 .right = bo.width + 0.0F,
794 .bottom = bo.height + 0.0F};
Roman Stratiienko33365c22020-10-10 23:06:36 +0300795 client_layer_.SetLayerSourceCrop(source_crop);
796
Sean Paulac874152016-03-10 16:00:26 -0500797 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500798}
799
800HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -0500801 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800802
Roman Stratiienkod146d6d2020-10-04 23:56:46 +0300803 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
804 return HWC2::Error::BadParameter;
805
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800806 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +0300807 return HWC2::Error::Unsupported;
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -0800808
809 color_mode_ = mode;
810 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500811}
812
813HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
Sean Paulac874152016-03-10 16:00:26 -0500814 int32_t hint) {
815 supported(__func__);
Roman Kovalivskyi12b91a32019-12-11 19:09:51 +0200816 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
817 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
818 return HWC2::Error::BadParameter;
819
820 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
821 return HWC2::Error::BadParameter;
822
823 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
824 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
825 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
826
827 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500828}
829
830HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -0500831 int32_t release_fence) {
832 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200833 // TODO(nobody): Need virtual display support
Sean Pauled2ec4b2016-03-10 15:35:40 -0500834 return unsupported(__func__, buffer, release_fence);
835}
836
Sean Paulac874152016-03-10 16:00:26 -0500837HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
838 supported(__func__);
839 uint64_t dpms_value = 0;
840 auto mode = static_cast<HWC2::PowerMode>(mode_in);
841 switch (mode) {
842 case HWC2::PowerMode::Off:
843 dpms_value = DRM_MODE_DPMS_OFF;
844 break;
845 case HWC2::PowerMode::On:
846 dpms_value = DRM_MODE_DPMS_ON;
847 break;
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100848 case HWC2::PowerMode::Doze:
849 case HWC2::PowerMode::DozeSuspend:
850 return HWC2::Error::Unsupported;
Sean Paulac874152016-03-10 16:00:26 -0500851 default:
852 ALOGI("Power mode %d is unsupported\n", mode);
Vincent Donnefort60ef7eb2019-10-09 11:39:28 +0100853 return HWC2::Error::BadParameter;
Sean Paulac874152016-03-10 16:00:26 -0500854 };
855
Sean Paulf72cccd2018-08-27 13:59:08 -0400856 std::unique_ptr<DrmDisplayComposition> composition = compositor_
857 .CreateComposition();
Sean Paulac874152016-03-10 16:00:26 -0500858 composition->Init(drm_, crtc_, importer_.get(), planner_.get(), frame_no_);
859 composition->SetDpmsMode(dpms_value);
Sean Pauled45a8e2017-02-28 13:17:34 -0500860 int ret = compositor_.ApplyComposition(std::move(composition));
Sean Paulac874152016-03-10 16:00:26 -0500861 if (ret) {
862 ALOGE("Failed to apply the dpms composition ret=%d", ret);
863 return HWC2::Error::BadParameter;
864 }
865 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500866}
867
868HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
Sean Paulac874152016-03-10 16:00:26 -0500869 supported(__func__);
Andrii Chepurnyi4bdd0fe2018-07-27 15:14:37 +0300870 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
Sean Paulac874152016-03-10 16:00:26 -0500871 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -0500872}
873
Roman Stratiienkob7b81cf2019-12-13 19:28:56 +0200874uint32_t DrmHwcTwo::HwcDisplay::CalcPixOps(
875 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t first_z,
876 size_t size) {
877 uint32_t pixops = 0;
878 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
879 if (l.first >= first_z && l.first < first_z + size) {
880 hwc_rect_t df = l.second->display_frame();
881 pixops += (df.right - df.left) * (df.bottom - df.top);
882 }
883 }
884 return pixops;
885}
886
887void DrmHwcTwo::HwcDisplay::MarkValidated(
888 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t client_first_z,
889 size_t client_size) {
890 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
891 if (l.first >= client_first_z && l.first < client_first_z + client_size)
892 l.second->set_validated_type(HWC2::Composition::Client);
893 else
894 l.second->set_validated_type(HWC2::Composition::Device);
895 }
896}
897
Sean Pauled2ec4b2016-03-10 15:35:40 -0500898HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
Sean Paulac874152016-03-10 16:00:26 -0500899 uint32_t *num_requests) {
900 supported(__func__);
Rob Herring4f6c62e2018-05-17 14:33:02 -0500901
Matvii Zorinef3c7972020-08-11 15:15:44 +0300902 return backend_->ValidateDisplay(this, num_types, num_requests);
Sean Pauled2ec4b2016-03-10 15:35:40 -0500903}
904
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300905#if PLATFORM_SDK_VERSION > 29
906HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
907 if (connector_->internal())
908 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
909 else if (connector_->external())
910 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
911 else
912 return HWC2::Error::BadConfig;
913
914 return HWC2::Error::None;
915}
916
917HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayVsyncPeriod(
918 hwc2_vsync_period_t *outVsyncPeriod /* ns */) {
919 supported(__func__);
920 DrmMode const &mode = connector_->active_mode();
921 if (mode.id() == 0)
922 return HWC2::Error::BadConfig;
923
924 *outVsyncPeriod = 1E9 / mode.v_refresh();
925 return HWC2::Error::None;
926}
927
928HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfigWithConstraints(
929 hwc2_config_t /*config*/,
930 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
931 hwc_vsync_period_change_timeline_t *outTimeline) {
932 supported(__func__);
933
934 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
935 return HWC2::Error::BadParameter;
936 }
937
938 return HWC2::Error::BadConfig;
939}
940
941HWC2::Error DrmHwcTwo::HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
942 return HWC2::Error::Unsupported;
943}
944
945HWC2::Error DrmHwcTwo::HwcDisplay::GetSupportedContentTypes(
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200946 uint32_t *outNumSupportedContentTypes,
947 const uint32_t *outSupportedContentTypes) {
Roman Stratiienko6f5df172020-09-27 22:48:08 +0300948 if (outSupportedContentTypes == nullptr)
949 *outNumSupportedContentTypes = 0;
950
951 return HWC2::Error::None;
952}
953
954HWC2::Error DrmHwcTwo::HwcDisplay::SetContentType(int32_t contentType) {
955 supported(__func__);
956
957 if (contentType != HWC2_CONTENT_TYPE_NONE)
958 return HWC2::Error::Unsupported;
959
960 /* TODO: Map to the DRM Connector property:
961 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
962 */
963
964 return HWC2::Error::None;
965}
966#endif
967
John Stultz8c7229d2020-02-07 21:31:08 +0000968#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800969HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
970 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
971 supported(__func__);
972
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +0200973 drmModePropertyBlobPtr blob = nullptr;
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800974
Andrii Chepurnyiadc5d822020-07-10 16:07:03 +0300975 if (connector_->GetEdidBlob(blob)) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800976 ALOGE("Failed to get edid property value.");
977 return HWC2::Error::Unsupported;
978 }
979
Andrii Chepurnyi8115dbe2020-04-14 13:03:57 +0300980 if (outData) {
981 *outDataSize = std::min(*outDataSize, blob->length);
982 memcpy(outData, blob->data, *outDataSize);
983 } else {
984 *outDataSize = blob->length;
985 }
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800986 *outPort = connector_->id();
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800987
988 return HWC2::Error::None;
989}
990
991HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
992 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
993 unsupported(__func__, outCapabilities);
994
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +0200995 if (outNumCapabilities == nullptr) {
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +0800996 return HWC2::Error::BadParameter;
997 }
998
999 *outNumCapabilities = 0;
1000
1001 return HWC2::Error::None;
1002}
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001003
1004HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
1005 bool *supported) {
1006 *supported = false;
1007 return HWC2::Error::None;
1008}
1009
1010HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
1011 float /* brightness */) {
1012 return HWC2::Error::Unsupported;
1013}
1014
John Stultz8c7229d2020-02-07 21:31:08 +00001015#endif /* PLATFORM_SDK_VERSION > 28 */
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001016
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001017#if PLATFORM_SDK_VERSION > 27
1018
1019HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
1020 int32_t mode, uint32_t *outNumIntents,
1021 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1022 if (mode != HAL_COLOR_MODE_NATIVE) {
1023 return HWC2::Error::BadParameter;
1024 }
1025
1026 if (outIntents == nullptr) {
1027 *outNumIntents = 1;
1028 return HWC2::Error::None;
1029 }
1030 *outNumIntents = 1;
1031 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1032 return HWC2::Error::None;
1033}
1034
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001035HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
1036 int32_t intent) {
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001037 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1038 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1039 return HWC2::Error::BadParameter;
1040
1041 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
1042 return HWC2::Error::BadParameter;
1043
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001044 if (mode != HAL_COLOR_MODE_NATIVE)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001045 return HWC2::Error::Unsupported;
1046
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001047 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
Roman Stratiienko27d2ed62020-09-26 23:59:19 +03001048 return HWC2::Error::Unsupported;
1049
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001050 color_mode_ = mode;
1051 return HWC2::Error::None;
1052}
1053
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001054#endif /* PLATFORM_SDK_VERSION > 27 */
1055
Sean Pauled2ec4b2016-03-10 15:35:40 -05001056HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
Sean Paulac874152016-03-10 16:00:26 -05001057 supported(__func__);
Kalyan Kondapallyda5839c2016-11-10 10:59:50 -08001058 cursor_x_ = x;
1059 cursor_y_ = y;
1060 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001061}
1062
1063HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
Sean Paulac874152016-03-10 16:00:26 -05001064 supported(__func__);
1065 blending_ = static_cast<HWC2::BlendMode>(mode);
1066 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001067}
1068
1069HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
Sean Paulac874152016-03-10 16:00:26 -05001070 int32_t acquire_fence) {
1071 supported(__func__);
1072 UniqueFd uf(acquire_fence);
1073
Sean Paulac874152016-03-10 16:00:26 -05001074 set_buffer(buffer);
1075 set_acquire_fence(uf.get());
1076 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001077}
1078
1079HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001080 // TODO(nobody): Put to client composition here?
Roman Kovalivskyibb375692019-12-11 17:48:44 +02001081 supported(__func__);
1082 layer_color_ = color;
1083 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001084}
1085
1086HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
Sean Paulac874152016-03-10 16:00:26 -05001087 sf_type_ = static_cast<HWC2::Composition>(type);
1088 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001089}
1090
1091HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
Sean Paulac874152016-03-10 16:00:26 -05001092 supported(__func__);
1093 dataspace_ = static_cast<android_dataspace_t>(dataspace);
1094 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001095}
1096
1097HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
Sean Paulac874152016-03-10 16:00:26 -05001098 supported(__func__);
1099 display_frame_ = frame;
1100 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001101}
1102
1103HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
Sean Paulac874152016-03-10 16:00:26 -05001104 supported(__func__);
1105 alpha_ = alpha;
1106 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001107}
1108
1109HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1110 const native_handle_t *stream) {
Sean Paulac874152016-03-10 16:00:26 -05001111 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001112 // TODO(nobody): We don't support sideband
Sean Pauled2ec4b2016-03-10 15:35:40 -05001113 return unsupported(__func__, stream);
1114}
1115
1116HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
Sean Paulac874152016-03-10 16:00:26 -05001117 supported(__func__);
1118 source_crop_ = crop;
1119 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001120}
1121
1122HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
Sean Paulac874152016-03-10 16:00:26 -05001123 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001124 // TODO(nobody): We don't use surface damage, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001125 unsupported(__func__, damage);
1126 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001127}
1128
1129HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
Sean Paulac874152016-03-10 16:00:26 -05001130 supported(__func__);
1131 transform_ = static_cast<HWC2::Transform>(transform);
1132 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001133}
1134
1135HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
Sean Paulac874152016-03-10 16:00:26 -05001136 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001137 // TODO(nobody): We don't use this information, marking as unsupported
Sean Paulac874152016-03-10 16:00:26 -05001138 unsupported(__func__, visible);
1139 return HWC2::Error::None;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001140}
1141
Sean Paulac874152016-03-10 16:00:26 -05001142HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1143 supported(__func__);
1144 z_order_ = order;
1145 return HWC2::Error::None;
1146}
1147
1148void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1149 supported(__func__);
1150 switch (blending_) {
1151 case HWC2::BlendMode::None:
1152 layer->blending = DrmHwcBlending::kNone;
1153 break;
1154 case HWC2::BlendMode::Premultiplied:
1155 layer->blending = DrmHwcBlending::kPreMult;
1156 break;
1157 case HWC2::BlendMode::Coverage:
1158 layer->blending = DrmHwcBlending::kCoverage;
1159 break;
1160 default:
1161 ALOGE("Unknown blending mode b=%d", blending_);
1162 layer->blending = DrmHwcBlending::kNone;
1163 break;
1164 }
1165
1166 OutputFd release_fence = release_fence_output();
1167
1168 layer->sf_handle = buffer_;
1169 layer->acquire_fence = acquire_fence_.Release();
1170 layer->release_fence = std::move(release_fence);
1171 layer->SetDisplayFrame(display_frame_);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001172 layer->alpha = lround(65535.0F * alpha_);
Sean Paulac874152016-03-10 16:00:26 -05001173 layer->SetSourceCrop(source_crop_);
1174 layer->SetTransform(static_cast<int32_t>(transform_));
Matvii Zorin8338c342020-09-08 16:12:51 +03001175 layer->dataspace = dataspace_;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001176}
1177
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001178void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
Roman Stratiienko23701092020-09-26 02:08:41 +03001179 const std::lock_guard<std::mutex> lock(hotplug_callback_lock);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001180
Roman Stratiienko23701092020-09-26 02:08:41 +03001181 if (hotplug_callback_hook_ && hotplug_callback_data_)
1182 hotplug_callback_hook_(hotplug_callback_data_, displayid,
1183 state == DRM_MODE_CONNECTED
1184 ? HWC2_CONNECTION_CONNECTED
1185 : HWC2_CONNECTION_DISCONNECTED);
Andrii Chepurnyi495e4cc2018-08-01 17:42:56 +03001186}
1187
1188void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
1189 for (auto &conn : drmDevice->connectors()) {
1190 if (conn->state() != DRM_MODE_CONNECTED)
1191 continue;
1192 HandleDisplayHotplug(conn->display(), conn->state());
1193 }
1194}
1195
1196void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
1197 for (auto &conn : drm_->connectors()) {
1198 drmModeConnection old_state = conn->state();
1199 drmModeConnection cur_state = conn->UpdateModes()
1200 ? DRM_MODE_UNKNOWNCONNECTION
1201 : conn->state();
1202
1203 if (cur_state == old_state)
1204 continue;
1205
1206 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1207 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1208 conn->id(), conn->display());
1209
1210 int display_id = conn->display();
1211 if (cur_state == DRM_MODE_CONNECTED) {
1212 auto &display = hwc2_->displays_.at(display_id);
1213 display.ChosePreferredConfig();
1214 } else {
1215 auto &display = hwc2_->displays_.at(display_id);
1216 display.ClearDisplay();
1217 }
1218
1219 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1220 }
1221}
1222
Sean Pauled2ec4b2016-03-10 15:35:40 -05001223// static
1224int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1225 unsupported(__func__);
1226 return 0;
1227}
1228
1229// static
1230void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
Sean Paulac874152016-03-10 16:00:26 -05001231 uint32_t *out_count,
1232 int32_t * /*out_capabilities*/) {
1233 supported(__func__);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001234 *out_count = 0;
1235}
1236
1237// static
Sean Paulac874152016-03-10 16:00:26 -05001238hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1239 struct hwc2_device * /*dev*/, int32_t descriptor) {
1240 supported(__func__);
1241 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001242 switch (func) {
1243 // Device functions
1244 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1245 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1246 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1247 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
Sean Paulf72cccd2018-08-27 13:59:08 -04001248 int32_t *, hwc2_display_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001249 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1250 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1251 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1252 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1253 case HWC2::FunctionDescriptor::Dump:
1254 return ToHook<HWC2_PFN_DUMP>(
1255 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1256 uint32_t *, char *>);
1257 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1258 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1259 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1260 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1261 case HWC2::FunctionDescriptor::RegisterCallback:
1262 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1263 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1264 &DrmHwcTwo::RegisterCallback, int32_t,
1265 hwc2_callback_data_t, hwc2_function_pointer_t>);
1266
1267 // Display functions
1268 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1269 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1270 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1271 &HwcDisplay::AcceptDisplayChanges>);
1272 case HWC2::FunctionDescriptor::CreateLayer:
1273 return ToHook<HWC2_PFN_CREATE_LAYER>(
1274 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1275 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1276 case HWC2::FunctionDescriptor::DestroyLayer:
1277 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1278 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1279 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1280 case HWC2::FunctionDescriptor::GetActiveConfig:
1281 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1282 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1283 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1284 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1285 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1286 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1287 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1288 hwc2_layer_t *, int32_t *>);
1289 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1290 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1291 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1292 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1293 int32_t, int32_t>);
1294 case HWC2::FunctionDescriptor::GetColorModes:
1295 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1296 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1297 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1298 case HWC2::FunctionDescriptor::GetDisplayAttribute:
Sean Paulf72cccd2018-08-27 13:59:08 -04001299 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1300 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1301 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1302 int32_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001303 case HWC2::FunctionDescriptor::GetDisplayConfigs:
Sean Paulf72cccd2018-08-27 13:59:08 -04001304 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1305 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1306 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1307 hwc2_config_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001308 case HWC2::FunctionDescriptor::GetDisplayName:
1309 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1310 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1311 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1312 case HWC2::FunctionDescriptor::GetDisplayRequests:
1313 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1314 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1315 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1316 hwc2_layer_t *, int32_t *>);
1317 case HWC2::FunctionDescriptor::GetDisplayType:
1318 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1319 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1320 &HwcDisplay::GetDisplayType, int32_t *>);
1321 case HWC2::FunctionDescriptor::GetDozeSupport:
1322 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1323 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1324 &HwcDisplay::GetDozeSupport, int32_t *>);
Sean Paulac874152016-03-10 16:00:26 -05001325 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1326 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1327 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1328 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1329 float *, float *, float *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001330 case HWC2::FunctionDescriptor::GetReleaseFences:
1331 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1332 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1333 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1334 int32_t *>);
1335 case HWC2::FunctionDescriptor::PresentDisplay:
1336 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1337 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1338 &HwcDisplay::PresentDisplay, int32_t *>);
1339 case HWC2::FunctionDescriptor::SetActiveConfig:
1340 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1341 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1342 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1343 case HWC2::FunctionDescriptor::SetClientTarget:
Sean Paulf72cccd2018-08-27 13:59:08 -04001344 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1345 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1346 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1347 int32_t, hwc_region_t>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001348 case HWC2::FunctionDescriptor::SetColorMode:
1349 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1350 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1351 &HwcDisplay::SetColorMode, int32_t>);
1352 case HWC2::FunctionDescriptor::SetColorTransform:
1353 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1354 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1355 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1356 case HWC2::FunctionDescriptor::SetOutputBuffer:
1357 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1358 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1359 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1360 case HWC2::FunctionDescriptor::SetPowerMode:
1361 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1362 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1363 &HwcDisplay::SetPowerMode, int32_t>);
1364 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1365 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1366 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1367 &HwcDisplay::SetVsyncEnabled, int32_t>);
1368 case HWC2::FunctionDescriptor::ValidateDisplay:
1369 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1370 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1371 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001372#if PLATFORM_SDK_VERSION > 27
1373 case HWC2::FunctionDescriptor::GetRenderIntents:
1374 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1375 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1376 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1377 int32_t *>);
Andrii Chepurnyi857a53f2020-04-29 23:15:28 +03001378 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1379 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1380 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1381 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
Andrii Chepurnyi50d37452020-04-24 14:20:24 +03001382#endif
John Stultz8c7229d2020-02-07 21:31:08 +00001383#if PLATFORM_SDK_VERSION > 28
Lowry Li (Arm Technology China)b3d81782019-12-18 14:28:22 +08001384 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1385 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1386 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1387 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1388 uint32_t *, uint8_t *>);
1389 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1390 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1391 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1392 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1393 uint32_t *>);
Andrii Chepurnyi2619aab2020-07-03 11:21:33 +03001394 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1395 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1396 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1397 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1398 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1399 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1400 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1401 &HwcDisplay::SetDisplayBrightness, float>);
John Stultz8c7229d2020-02-07 21:31:08 +00001402#endif /* PLATFORM_SDK_VERSION > 28 */
Roman Stratiienko6f5df172020-09-27 22:48:08 +03001403#if PLATFORM_SDK_VERSION > 29
1404 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
1405 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
1406 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
1407 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
1408 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
1409 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
1410 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
1411 &HwcDisplay::GetDisplayVsyncPeriod,
1412 hwc2_vsync_period_t *>);
1413 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
1414 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
1415 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
1416 &HwcDisplay::SetActiveConfigWithConstraints,
1417 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
1418 hwc_vsync_period_change_timeline_t *>);
1419 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
1420 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
1421 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
1422 &HwcDisplay::SetAutoLowLatencyMode, bool>);
1423 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
1424 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
1425 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
1426 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
1427 uint32_t *>);
1428 case HWC2::FunctionDescriptor::SetContentType:
1429 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
1430 DisplayHook<decltype(&HwcDisplay::SetContentType),
1431 &HwcDisplay::SetContentType, int32_t>);
1432#endif
Sean Pauled2ec4b2016-03-10 15:35:40 -05001433 // Layer functions
1434 case HWC2::FunctionDescriptor::SetCursorPosition:
1435 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1436 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1437 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1438 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1439 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1440 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1441 &HwcLayer::SetLayerBlendMode, int32_t>);
1442 case HWC2::FunctionDescriptor::SetLayerBuffer:
1443 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1444 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1445 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1446 case HWC2::FunctionDescriptor::SetLayerColor:
1447 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1448 LayerHook<decltype(&HwcLayer::SetLayerColor),
1449 &HwcLayer::SetLayerColor, hwc_color_t>);
1450 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1451 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1452 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1453 &HwcLayer::SetLayerCompositionType, int32_t>);
1454 case HWC2::FunctionDescriptor::SetLayerDataspace:
1455 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1456 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1457 &HwcLayer::SetLayerDataspace, int32_t>);
1458 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1459 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1460 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1461 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1462 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1463 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1464 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1465 &HwcLayer::SetLayerPlaneAlpha, float>);
1466 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
Sean Paulf72cccd2018-08-27 13:59:08 -04001467 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1468 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1469 &HwcLayer::SetLayerSidebandStream,
1470 const native_handle_t *>);
Sean Pauled2ec4b2016-03-10 15:35:40 -05001471 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1472 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1473 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1474 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1475 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1476 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1477 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1478 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1479 case HWC2::FunctionDescriptor::SetLayerTransform:
1480 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1481 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1482 &HwcLayer::SetLayerTransform, int32_t>);
1483 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1484 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1485 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1486 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1487 case HWC2::FunctionDescriptor::SetLayerZOrder:
1488 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1489 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1490 &HwcLayer::SetLayerZOrder, uint32_t>);
Sean Paulac874152016-03-10 16:00:26 -05001491 case HWC2::FunctionDescriptor::Invalid:
Sean Pauled2ec4b2016-03-10 15:35:40 -05001492 default:
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001493 return nullptr;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001494 }
1495}
Sean Paulac874152016-03-10 16:00:26 -05001496
1497// static
1498int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1499 struct hw_device_t **dev) {
1500 supported(__func__);
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001501 if (strcmp(name, HWC_HARDWARE_COMPOSER) != 0) {
Sean Paulac874152016-03-10 16:00:26 -05001502 ALOGE("Invalid module name- %s", name);
1503 return -EINVAL;
1504 }
1505
1506 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1507 if (!ctx) {
1508 ALOGE("Failed to allocate DrmHwcTwo");
1509 return -ENOMEM;
1510 }
1511
1512 HWC2::Error err = ctx->Init();
1513 if (err != HWC2::Error::None) {
1514 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1515 return -EINVAL;
1516 }
1517
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001518 ctx->common.module = (hw_module_t *)module;
Sean Paulac874152016-03-10 16:00:26 -05001519 *dev = &ctx->common;
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001520 ctx.release(); // NOLINT(bugprone-unused-return-value)
Sean Paulac874152016-03-10 16:00:26 -05001521 return 0;
Sean Pauled2ec4b2016-03-10 15:35:40 -05001522}
Sean Paulf72cccd2018-08-27 13:59:08 -04001523} // namespace android
Sean Paulac874152016-03-10 16:00:26 -05001524
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001525// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001526static struct hw_module_methods_t hwc2_module_methods = {
1527 .open = android::DrmHwcTwo::HookDevOpen,
1528};
1529
Roman Stratiienkob3b5c1e2021-02-15 13:44:19 +02001530// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Sean Paulac874152016-03-10 16:00:26 -05001531hw_module_t HAL_MODULE_INFO_SYM = {
1532 .tag = HARDWARE_MODULE_TAG,
1533 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1534 .id = HWC_HARDWARE_MODULE_ID,
1535 .name = "DrmHwcTwo module",
1536 .author = "The Android Open Source Project",
1537 .methods = &hwc2_module_methods,
Roman Stratiienkoe2f2c922021-02-13 10:57:47 +02001538 .dso = nullptr,
Sean Paulac874152016-03-10 16:00:26 -05001539 .reserved = {0},
1540};