2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
18 #define LOG_TAG "hwc-drm-two"
20 #include "DrmHwcTwo.h"
22 #include <hardware/hardware.h>
23 #include <hardware/hwcomposer2.h>
24 #include <sync/sync.h>
31 #include "backend/BackendManager.h"
32 #include "bufferinfo/BufferInfoGetter.h"
33 #include "compositor/DrmDisplayComposition.h"
34 #include "utils/log.h"
35 #include "utils/properties.h"
39 DrmHwcTwo::DrmHwcTwo() : hwc2_device() {
40 common.tag = HARDWARE_DEVICE_TAG;
41 common.version = HWC_DEVICE_API_VERSION_2_0;
42 common.close = HookDevClose;
43 getCapabilities = HookDevGetCapabilities;
44 getFunction = HookDevGetFunction;
47 HWC2::Error DrmHwcTwo::CreateDisplay(hwc2_display_t displ,
48 HWC2::DisplayType type) {
49 DrmDevice *drm = resource_manager_.GetDrmDevice(displ);
50 std::shared_ptr<Importer> importer = resource_manager_.GetImporter(displ);
51 if (!drm || !importer) {
52 ALOGE("Failed to get a valid drmresource and importer");
53 return HWC2::Error::NoResources;
55 displays_.emplace(std::piecewise_construct, std::forward_as_tuple(displ),
56 std::forward_as_tuple(&resource_manager_, drm, importer,
59 DrmCrtc *crtc = drm->GetCrtcForDisplay(static_cast<int>(displ));
61 ALOGE("Failed to get crtc for display %d", static_cast<int>(displ));
62 return HWC2::Error::BadDisplay;
64 auto display_planes = std::vector<DrmPlane *>();
65 for (const auto &plane : drm->planes()) {
66 if (plane->GetCrtcSupported(*crtc))
67 display_planes.push_back(plane.get());
69 displays_.at(displ).Init(&display_planes);
70 return HWC2::Error::None;
73 HWC2::Error DrmHwcTwo::Init() {
74 int rv = resource_manager_.Init();
76 ALOGE("Can't initialize the resource manager %d", rv);
77 return HWC2::Error::NoResources;
80 HWC2::Error ret = HWC2::Error::None;
81 for (int i = 0; i < resource_manager_.getDisplayCount(); i++) {
82 ret = CreateDisplay(i, HWC2::DisplayType::Physical);
83 if (ret != HWC2::Error::None) {
84 ALOGE("Failed to create display %d with error %d", i, ret);
89 const auto &drm_devices = resource_manager_.getDrmDevices();
90 for (const auto &device : drm_devices) {
91 // NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
92 device->RegisterHotplugHandler(new DrmHotplugHandler(this, device.get()));
97 template <typename... Args>
98 static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
99 ALOGV("Unsupported function: %s", func);
100 return HWC2::Error::Unsupported;
103 static inline void supported(char const *func) {
104 ALOGV("Supported function: %s", func);
107 HWC2::Error DrmHwcTwo::CreateVirtualDisplay(uint32_t width, uint32_t height,
109 hwc2_display_t *display) {
110 // TODO(nobody): Implement virtual display
111 return unsupported(__func__, width, height, format, display);
114 HWC2::Error DrmHwcTwo::DestroyVirtualDisplay(hwc2_display_t display) {
115 // TODO(nobody): Implement virtual display
116 return unsupported(__func__, display);
119 std::string DrmHwcTwo::HwcDisplay::DumpDelta(
120 DrmHwcTwo::HwcDisplay::Stats delta) {
121 if (delta.total_pixops_ == 0)
122 return "No stats yet";
123 double ratio = 1.0 - double(delta.gpu_pixops_) / double(delta.total_pixops_);
125 std::stringstream ss;
126 ss << " Total frames count: " << delta.total_frames_ << "\n"
127 << " Failed to test commit frames: " << delta.failed_kms_validate_ << "\n"
128 << " Failed to commit frames: " << delta.failed_kms_present_ << "\n"
129 << ((delta.failed_kms_present_ > 0)
130 ? " !!! Internal failure, FIX it please\n"
132 << " Flattened frames: " << delta.frames_flattened_ << "\n"
133 << " Pixel operations (free units)"
134 << " : [TOTAL: " << delta.total_pixops_ << " / GPU: " << delta.gpu_pixops_
136 << " Composition efficiency: " << ratio;
141 std::string DrmHwcTwo::HwcDisplay::Dump() {
142 std::stringstream ss;
143 ss << "- Display on: " << connector_->name() << "\n"
144 << " Flattening state: " << compositor_.GetFlatteningState() << "\n"
145 << "Statistics since system boot:\n"
146 << DumpDelta(total_stats_) << "\n\n"
147 << "Statistics since last dumpsys request:\n"
148 << DumpDelta(total_stats_.minus(prev_stats_)) << "\n\n";
150 memcpy(&prev_stats_, &total_stats_, sizeof(Stats));
154 void DrmHwcTwo::Dump(uint32_t *outSize, char *outBuffer) {
157 if (outBuffer != nullptr) {
158 auto copied_bytes = mDumpString.copy(outBuffer, *outSize);
159 *outSize = static_cast<uint32_t>(copied_bytes);
163 std::stringstream output;
165 output << "-- drm_hwcomposer --\n\n";
167 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &dp : displays_)
168 output << dp.second.Dump();
170 mDumpString = output.str();
171 *outSize = static_cast<uint32_t>(mDumpString.size());
174 uint32_t DrmHwcTwo::GetMaxVirtualDisplayCount() {
175 // TODO(nobody): Implement virtual display
176 unsupported(__func__);
180 HWC2::Error DrmHwcTwo::RegisterCallback(int32_t descriptor,
181 hwc2_callback_data_t data,
182 hwc2_function_pointer_t function) {
185 switch (static_cast<HWC2::Callback>(descriptor)) {
186 case HWC2::Callback::Hotplug: {
187 SetHotplugCallback(data, function);
188 const auto &drm_devices = resource_manager_.getDrmDevices();
189 for (const auto &device : drm_devices)
190 HandleInitialHotplugState(device.get());
193 case HWC2::Callback::Refresh: {
194 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
196 d.second.RegisterRefreshCallback(data, function);
199 case HWC2::Callback::Vsync: {
200 for (std::pair<const hwc2_display_t, DrmHwcTwo::HwcDisplay> &d :
202 d.second.RegisterVsyncCallback(data, function);
208 return HWC2::Error::None;
211 DrmHwcTwo::HwcDisplay::HwcDisplay(ResourceManager *resource_manager,
213 std::shared_ptr<Importer> importer,
214 hwc2_display_t handle, HWC2::DisplayType type)
215 : resource_manager_(resource_manager),
217 importer_(std::move(importer)),
220 color_transform_hint_(HAL_COLOR_TRANSFORM_IDENTITY) {
224 color_transform_matrix_ = {1.0, 0.0, 0.0, 0.0,
231 void DrmHwcTwo::HwcDisplay::ClearDisplay() {
232 compositor_.ClearDisplay();
235 HWC2::Error DrmHwcTwo::HwcDisplay::Init(std::vector<DrmPlane *> *planes) {
237 planner_ = Planner::CreateInstance(drm_);
239 ALOGE("Failed to create planner instance for composition");
240 return HWC2::Error::NoResources;
243 int display = static_cast<int>(handle_);
244 int ret = compositor_.Init(resource_manager_, display);
246 ALOGE("Failed display compositor init for display %d (%d)", display, ret);
247 return HWC2::Error::NoResources;
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];
253 property_get("vendor.hwc.drm.use_overlay_planes", use_overlay_planes_prop,
255 bool use_overlay_planes = strtol(use_overlay_planes_prop, nullptr, 10);
256 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);
263 crtc_ = drm_->GetCrtcForDisplay(display);
265 ALOGE("Failed to get crtc for display %d", display);
266 return HWC2::Error::BadDisplay;
269 connector_ = drm_->GetConnectorForDisplay(display);
271 ALOGE("Failed to get connector for display %d", display);
272 return HWC2::Error::BadDisplay;
275 ret = vsync_worker_.Init(drm_, display);
277 ALOGE("Failed to create event worker for d=%d %d\n", display, ret);
278 return HWC2::Error::BadDisplay;
281 ret = BackendManager::GetInstance().SetBackendForDisplay(this);
283 ALOGE("Failed to set backend for d=%d %d\n", display, ret);
284 return HWC2::Error::BadDisplay;
287 return ChosePreferredConfig();
290 HWC2::Error DrmHwcTwo::HwcDisplay::ChosePreferredConfig() {
291 // Fetch the number of modes from the display
292 uint32_t num_configs = 0;
293 HWC2::Error err = GetDisplayConfigs(&num_configs, nullptr);
294 if (err != HWC2::Error::None || !num_configs)
297 return SetActiveConfig(connector_->get_preferred_mode_id());
300 void DrmHwcTwo::HwcDisplay::RegisterVsyncCallback(
301 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
303 vsync_worker_.RegisterClientCallback(data, func);
306 void DrmHwcTwo::HwcDisplay::RegisterRefreshCallback(
307 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
309 compositor_.SetRefreshCallback(data, func);
312 HWC2::Error DrmHwcTwo::HwcDisplay::AcceptDisplayChanges() {
314 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_)
315 l.second.accept_type_change();
316 return HWC2::Error::None;
319 HWC2::Error DrmHwcTwo::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
321 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), HwcLayer());
322 *layer = static_cast<hwc2_layer_t>(layer_idx_);
324 return HWC2::Error::None;
327 HWC2::Error DrmHwcTwo::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
329 if (!get_layer(layer))
330 return HWC2::Error::BadLayer;
332 layers_.erase(layer);
333 return HWC2::Error::None;
336 HWC2::Error DrmHwcTwo::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
338 DrmMode const &mode = connector_->active_mode();
340 return HWC2::Error::BadConfig;
343 return HWC2::Error::None;
346 HWC2::Error DrmHwcTwo::HwcDisplay::GetChangedCompositionTypes(
347 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
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());
359 if (!layers && !types)
360 *num_elements = num_changes;
361 return HWC2::Error::None;
364 HWC2::Error DrmHwcTwo::HwcDisplay::GetClientTargetSupport(uint32_t width,
369 std::pair<uint32_t, uint32_t> min = drm_->min_resolution();
370 std::pair<uint32_t, uint32_t> max = drm_->max_resolution();
372 if (width < min.first || height < min.second)
373 return HWC2::Error::Unsupported;
375 if (width > max.first || height > max.second)
376 return HWC2::Error::Unsupported;
378 if (dataspace != HAL_DATASPACE_UNKNOWN)
379 return HWC2::Error::Unsupported;
381 // TODO(nobody): Validate format can be handled by either GL or planes
382 return HWC2::Error::None;
385 HWC2::Error DrmHwcTwo::HwcDisplay::GetColorModes(uint32_t *num_modes,
392 *modes = HAL_COLOR_MODE_NATIVE;
394 return HWC2::Error::None;
397 HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
398 int32_t attribute_in,
401 auto mode = std::find_if(connector_->modes().begin(),
402 connector_->modes().end(),
403 [config](DrmMode const &m) {
404 return m.id() == config;
406 if (mode == connector_->modes().end()) {
407 ALOGE("Could not find active mode for %d", config);
408 return HWC2::Error::BadConfig;
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);
416 case HWC2::Attribute::Width:
417 *value = mode->h_display();
419 case HWC2::Attribute::Height:
420 *value = mode->v_display();
422 case HWC2::Attribute::VsyncPeriod:
424 *value = 1000.0 * 1000.0 * 1000.0 / mode->v_refresh();
426 case HWC2::Attribute::DpiX:
427 // Dots per 1000 inches
428 *value = mm_width ? (mode->h_display() * kUmPerInch) / mm_width : -1;
430 case HWC2::Attribute::DpiY:
431 // Dots per 1000 inches
432 *value = mm_height ? (mode->v_display() * kUmPerInch) / mm_height : -1;
434 #if PLATFORM_SDK_VERSION > 29
435 case HWC2::Attribute::ConfigGroup:
436 *value = 0; /* TODO: Add support for config groups */
441 return HWC2::Error::BadConfig;
443 return HWC2::Error::None;
446 HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
447 hwc2_config_t *configs) {
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.
455 int ret = connector_->UpdateModes();
457 ALOGE("Failed to update display modes %d", ret);
458 return HWC2::Error::BadDisplay;
462 // 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()
470 // TODO(nobody): Remove the following block of code until AOSP handles all
472 std::vector<DrmMode> sel_modes;
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) {
478 connector_->get_preferred_mode_id();
480 if (mode != connector_->modes().end())
481 sel_modes.push_back(*mode);
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());
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)
489 for (const DrmMode &mode : connector_->modes()) {
490 // TODO(nobody): Remove this when 3D Attributes are in AOSP
491 if (mode.flags() & DRM_MODE_FLAG_3D_MASK)
494 // TODO(nobody): Remove this when the Interlaced attribute is in AOSP
495 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();
503 if (m == connector_->modes().end())
504 sel_modes.push_back(mode);
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
511 // TODO(nobody): Remove this when AOSP handles duplicates modes
512 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();
518 if (m == sel_modes.end())
519 sel_modes.push_back(mode);
522 auto num_modes = static_cast<uint32_t>(sel_modes.size());
524 *num_configs = num_modes;
525 return HWC2::Error::None;
529 for (const DrmMode &mode : sel_modes) {
530 if (idx >= *num_configs)
532 configs[idx++] = mode.id();
535 return HWC2::Error::None;
538 HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
540 std::ostringstream stream;
541 stream << "display-" << connector_->id();
542 std::string string = stream.str();
543 size_t length = string.length();
546 return HWC2::Error::None;
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;
554 HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
555 uint32_t *num_elements,
556 hwc2_layer_t *layers,
557 int32_t *layer_requests) {
559 // TODO(nobody): I think virtual display should request
560 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
561 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
563 return HWC2::Error::None;
566 HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayType(int32_t *type) {
568 *type = static_cast<int32_t>(type_);
569 return HWC2::Error::None;
572 HWC2::Error DrmHwcTwo::HwcDisplay::GetDozeSupport(int32_t *support) {
575 return HWC2::Error::None;
578 HWC2::Error DrmHwcTwo::HwcDisplay::GetHdrCapabilities(
579 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
580 float * /*max_average_luminance*/, float * /*min_luminance*/) {
583 return HWC2::Error::None;
586 HWC2::Error DrmHwcTwo::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
587 hwc2_layer_t *layers,
590 uint32_t num_layers = 0;
592 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
594 if (layers == nullptr || fences == nullptr)
597 if (num_layers > *num_elements) {
598 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
599 return HWC2::Error::None;
602 layers[num_layers - 1] = l.first;
603 fences[num_layers - 1] = l.second.take_release_fence();
605 *num_elements = num_layers;
606 return HWC2::Error::None;
609 void DrmHwcTwo::HwcDisplay::AddFenceToPresentFence(int fd) {
613 if (present_fence_.get() >= 0) {
614 int old_fence = present_fence_.get();
615 present_fence_.Set(sync_merge("dc_present", old_fence, fd));
618 present_fence_.Set(fd);
622 bool DrmHwcTwo::HwcDisplay::HardwareSupportsLayerType(
623 HWC2::Composition comp_type) {
624 return comp_type == HWC2::Composition::Device ||
625 comp_type == HWC2::Composition::Cursor;
628 HWC2::Error DrmHwcTwo::HwcDisplay::CreateComposition(bool test) {
629 // order the layers by z-order
630 bool use_client_layer = false;
631 uint32_t client_z_order = UINT32_MAX;
632 std::map<uint32_t, DrmHwcTwo::HwcLayer *> z_map;
633 for (std::pair<const hwc2_layer_t, DrmHwcTwo::HwcLayer> &l : layers_) {
634 switch (l.second.validated_type()) {
635 case HWC2::Composition::Device:
636 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
638 case HWC2::Composition::Client:
639 // Place it at the z_order of the lowest client layer
640 use_client_layer = true;
641 client_z_order = std::min(client_z_order, l.second.z_order());
647 if (use_client_layer)
648 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
651 return HWC2::Error::BadLayer;
653 std::vector<DrmHwcLayer> composition_layers;
655 // now that they're ordered by z, add them to the composition
656 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
658 l.second->PopulateDrmLayer(&layer);
659 int ret = layer.ImportBuffer(importer_.get());
661 ALOGE("Failed to import layer, ret=%d", ret);
662 return HWC2::Error::NoResources;
664 composition_layers.emplace_back(std::move(layer));
667 auto composition = std::make_unique<DrmDisplayComposition>(crtc_,
670 // TODO(nobody): Don't always assume geometry changed
671 int ret = composition->SetLayers(composition_layers.data(),
672 composition_layers.size(), true);
674 ALOGE("Failed to set layers in the composition ret=%d", ret);
675 return HWC2::Error::BadLayer;
678 std::vector<DrmPlane *> primary_planes(primary_planes_);
679 std::vector<DrmPlane *> overlay_planes(overlay_planes_);
680 ret = composition->Plan(&primary_planes, &overlay_planes);
682 ALOGE("Failed to plan the composition ret=%d", ret);
683 return HWC2::Error::BadConfig;
686 // Disable the planes we're not using
687 for (auto i = primary_planes.begin(); i != primary_planes.end();) {
688 composition->AddPlaneDisable(*i);
689 i = primary_planes.erase(i);
691 for (auto i = overlay_planes.begin(); i != overlay_planes.end();) {
692 composition->AddPlaneDisable(*i);
693 i = overlay_planes.erase(i);
697 ret = compositor_.TestComposition(composition.get());
699 ret = compositor_.ApplyComposition(std::move(composition));
700 AddFenceToPresentFence(compositor_.TakeOutFence());
704 ALOGE("Failed to apply the frame composition ret=%d", ret);
705 return HWC2::Error::BadParameter;
707 return HWC2::Error::None;
710 HWC2::Error DrmHwcTwo::HwcDisplay::PresentDisplay(int32_t *present_fence) {
714 ++total_stats_.total_frames_;
716 ret = CreateComposition(false);
717 if (ret != HWC2::Error::None)
718 ++total_stats_.failed_kms_present_;
720 if (ret == HWC2::Error::BadLayer) {
721 // Can we really have no client or device layers?
723 return HWC2::Error::None;
725 if (ret != HWC2::Error::None)
728 *present_fence = present_fence_.Release();
731 return HWC2::Error::None;
734 HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
736 auto mode = std::find_if(connector_->modes().begin(),
737 connector_->modes().end(),
738 [config](DrmMode const &m) {
739 return m.id() == config;
741 if (mode == connector_->modes().end()) {
742 ALOGE("Could not find active mode for %d", config);
743 return HWC2::Error::BadConfig;
746 auto composition = std::make_unique<DrmDisplayComposition>(crtc_,
748 int ret = composition->SetDisplayMode(*mode);
750 return HWC2::Error::BadConfig;
752 ret = compositor_.ApplyComposition(std::move(composition));
754 ALOGE("Failed to queue dpms composition on %d", ret);
755 return HWC2::Error::BadConfig;
758 connector_->set_active_mode(*mode);
760 // Setup the client layer's dimensions
761 hwc_rect_t display_frame = {.left = 0,
763 .right = static_cast<int>(mode->h_display()),
764 .bottom = static_cast<int>(mode->v_display())};
765 client_layer_.SetLayerDisplayFrame(display_frame);
767 return HWC2::Error::None;
770 HWC2::Error DrmHwcTwo::HwcDisplay::SetClientTarget(buffer_handle_t target,
771 int32_t acquire_fence,
773 hwc_region_t /*damage*/) {
775 UniqueFd uf(acquire_fence);
777 client_layer_.set_buffer(target);
778 client_layer_.set_acquire_fence(uf.get());
779 client_layer_.SetLayerDataspace(dataspace);
781 /* TODO: Do not update source_crop every call.
782 * It makes sense to do it once after every hotplug event. */
784 BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
786 hwc_frect_t source_crop = {.left = 0.0F,
788 .right = bo.width + 0.0F,
789 .bottom = bo.height + 0.0F};
790 client_layer_.SetLayerSourceCrop(source_crop);
792 return HWC2::Error::None;
795 HWC2::Error DrmHwcTwo::HwcDisplay::SetColorMode(int32_t mode) {
798 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
799 return HWC2::Error::BadParameter;
801 if (mode != HAL_COLOR_MODE_NATIVE)
802 return HWC2::Error::Unsupported;
805 return HWC2::Error::None;
808 HWC2::Error DrmHwcTwo::HwcDisplay::SetColorTransform(const float *matrix,
811 if (hint < HAL_COLOR_TRANSFORM_IDENTITY ||
812 hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
813 return HWC2::Error::BadParameter;
815 if (!matrix && hint == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
816 return HWC2::Error::BadParameter;
818 color_transform_hint_ = static_cast<android_color_transform_t>(hint);
819 if (color_transform_hint_ == HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX)
820 std::copy(matrix, matrix + MATRIX_SIZE, color_transform_matrix_.begin());
822 return HWC2::Error::None;
825 HWC2::Error DrmHwcTwo::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
826 int32_t release_fence) {
828 // TODO(nobody): Need virtual display support
829 return unsupported(__func__, buffer, release_fence);
832 HWC2::Error DrmHwcTwo::HwcDisplay::SetPowerMode(int32_t mode_in) {
834 uint64_t dpms_value = 0;
835 auto mode = static_cast<HWC2::PowerMode>(mode_in);
837 case HWC2::PowerMode::Off:
838 dpms_value = DRM_MODE_DPMS_OFF;
840 case HWC2::PowerMode::On:
841 dpms_value = DRM_MODE_DPMS_ON;
843 case HWC2::PowerMode::Doze:
844 case HWC2::PowerMode::DozeSuspend:
845 return HWC2::Error::Unsupported;
847 ALOGI("Power mode %d is unsupported\n", mode);
848 return HWC2::Error::BadParameter;
851 auto composition = std::make_unique<DrmDisplayComposition>(crtc_,
853 composition->SetDpmsMode(dpms_value);
854 int ret = compositor_.ApplyComposition(std::move(composition));
856 ALOGE("Failed to apply the dpms composition ret=%d", ret);
857 return HWC2::Error::BadParameter;
859 return HWC2::Error::None;
862 HWC2::Error DrmHwcTwo::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
864 vsync_worker_.VSyncControl(HWC2_VSYNC_ENABLE == enabled);
865 return HWC2::Error::None;
868 void DrmHwcTwo::HwcDisplay::MarkValidated(
869 std::map<uint32_t, DrmHwcTwo::HwcLayer *> &z_map, size_t client_first_z,
870 size_t client_size) {
871 for (std::pair<const uint32_t, DrmHwcTwo::HwcLayer *> &l : z_map) {
872 if (l.first >= client_first_z && l.first < client_first_z + client_size)
873 l.second->set_validated_type(HWC2::Composition::Client);
875 l.second->set_validated_type(HWC2::Composition::Device);
879 HWC2::Error DrmHwcTwo::HwcDisplay::ValidateDisplay(uint32_t *num_types,
880 uint32_t *num_requests) {
883 return backend_->ValidateDisplay(this, num_types, num_requests);
886 #if PLATFORM_SDK_VERSION > 29
887 HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayConnectionType(uint32_t *outType) {
888 if (connector_->internal())
889 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::Internal);
890 else if (connector_->external())
891 *outType = static_cast<uint32_t>(HWC2::DisplayConnectionType::External);
893 return HWC2::Error::BadConfig;
895 return HWC2::Error::None;
898 HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayVsyncPeriod(
899 hwc2_vsync_period_t *outVsyncPeriod /* ns */) {
901 DrmMode const &mode = connector_->active_mode();
903 return HWC2::Error::BadConfig;
905 *outVsyncPeriod = 1E9 / mode.v_refresh();
906 return HWC2::Error::None;
909 HWC2::Error DrmHwcTwo::HwcDisplay::SetActiveConfigWithConstraints(
910 hwc2_config_t /*config*/,
911 hwc_vsync_period_change_constraints_t *vsyncPeriodChangeConstraints,
912 hwc_vsync_period_change_timeline_t *outTimeline) {
915 if (vsyncPeriodChangeConstraints == nullptr || outTimeline == nullptr) {
916 return HWC2::Error::BadParameter;
919 return HWC2::Error::BadConfig;
922 HWC2::Error DrmHwcTwo::HwcDisplay::SetAutoLowLatencyMode(bool /*on*/) {
923 return HWC2::Error::Unsupported;
926 HWC2::Error DrmHwcTwo::HwcDisplay::GetSupportedContentTypes(
927 uint32_t *outNumSupportedContentTypes,
928 const uint32_t *outSupportedContentTypes) {
929 if (outSupportedContentTypes == nullptr)
930 *outNumSupportedContentTypes = 0;
932 return HWC2::Error::None;
935 HWC2::Error DrmHwcTwo::HwcDisplay::SetContentType(int32_t contentType) {
938 if (contentType != HWC2_CONTENT_TYPE_NONE)
939 return HWC2::Error::Unsupported;
941 /* TODO: Map to the DRM Connector property:
942 * https://elixir.bootlin.com/linux/v5.4-rc5/source/drivers/gpu/drm/drm_connector.c#L809
945 return HWC2::Error::None;
949 #if PLATFORM_SDK_VERSION > 28
950 HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayIdentificationData(
951 uint8_t *outPort, uint32_t *outDataSize, uint8_t *outData) {
954 drmModePropertyBlobPtr blob = nullptr;
956 if (connector_->GetEdidBlob(blob)) {
957 ALOGE("Failed to get edid property value.");
958 return HWC2::Error::Unsupported;
962 *outDataSize = std::min(*outDataSize, blob->length);
963 memcpy(outData, blob->data, *outDataSize);
965 *outDataSize = blob->length;
967 *outPort = connector_->id();
969 return HWC2::Error::None;
972 HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayCapabilities(
973 uint32_t *outNumCapabilities, uint32_t *outCapabilities) {
974 unsupported(__func__, outCapabilities);
976 if (outNumCapabilities == nullptr) {
977 return HWC2::Error::BadParameter;
980 *outNumCapabilities = 0;
982 return HWC2::Error::None;
985 HWC2::Error DrmHwcTwo::HwcDisplay::GetDisplayBrightnessSupport(
988 return HWC2::Error::None;
991 HWC2::Error DrmHwcTwo::HwcDisplay::SetDisplayBrightness(
992 float /* brightness */) {
993 return HWC2::Error::Unsupported;
996 #endif /* PLATFORM_SDK_VERSION > 28 */
998 #if PLATFORM_SDK_VERSION > 27
1000 HWC2::Error DrmHwcTwo::HwcDisplay::GetRenderIntents(
1001 int32_t mode, uint32_t *outNumIntents,
1002 int32_t * /*android_render_intent_v1_1_t*/ outIntents) {
1003 if (mode != HAL_COLOR_MODE_NATIVE) {
1004 return HWC2::Error::BadParameter;
1007 if (outIntents == nullptr) {
1009 return HWC2::Error::None;
1012 outIntents[0] = HAL_RENDER_INTENT_COLORIMETRIC;
1013 return HWC2::Error::None;
1016 HWC2::Error DrmHwcTwo::HwcDisplay::SetColorModeWithIntent(int32_t mode,
1018 if (intent < HAL_RENDER_INTENT_COLORIMETRIC ||
1019 intent > HAL_RENDER_INTENT_TONE_MAP_ENHANCE)
1020 return HWC2::Error::BadParameter;
1022 if (mode < HAL_COLOR_MODE_NATIVE || mode > HAL_COLOR_MODE_BT2100_HLG)
1023 return HWC2::Error::BadParameter;
1025 if (mode != HAL_COLOR_MODE_NATIVE)
1026 return HWC2::Error::Unsupported;
1028 if (intent != HAL_RENDER_INTENT_COLORIMETRIC)
1029 return HWC2::Error::Unsupported;
1032 return HWC2::Error::None;
1035 #endif /* PLATFORM_SDK_VERSION > 27 */
1037 HWC2::Error DrmHwcTwo::HwcLayer::SetCursorPosition(int32_t x, int32_t y) {
1038 supported(__func__);
1041 return HWC2::Error::None;
1044 HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBlendMode(int32_t mode) {
1045 supported(__func__);
1046 blending_ = static_cast<HWC2::BlendMode>(mode);
1047 return HWC2::Error::None;
1050 HWC2::Error DrmHwcTwo::HwcLayer::SetLayerBuffer(buffer_handle_t buffer,
1051 int32_t acquire_fence) {
1052 supported(__func__);
1053 UniqueFd uf(acquire_fence);
1056 set_acquire_fence(uf.get());
1057 return HWC2::Error::None;
1060 HWC2::Error DrmHwcTwo::HwcLayer::SetLayerColor(hwc_color_t color) {
1061 // TODO(nobody): Put to client composition here?
1062 supported(__func__);
1063 layer_color_ = color;
1064 return HWC2::Error::None;
1067 HWC2::Error DrmHwcTwo::HwcLayer::SetLayerCompositionType(int32_t type) {
1068 sf_type_ = static_cast<HWC2::Composition>(type);
1069 return HWC2::Error::None;
1072 HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDataspace(int32_t dataspace) {
1073 supported(__func__);
1074 dataspace_ = static_cast<android_dataspace_t>(dataspace);
1075 return HWC2::Error::None;
1078 HWC2::Error DrmHwcTwo::HwcLayer::SetLayerDisplayFrame(hwc_rect_t frame) {
1079 supported(__func__);
1080 display_frame_ = frame;
1081 return HWC2::Error::None;
1084 HWC2::Error DrmHwcTwo::HwcLayer::SetLayerPlaneAlpha(float alpha) {
1085 supported(__func__);
1087 return HWC2::Error::None;
1090 HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSidebandStream(
1091 const native_handle_t *stream) {
1092 supported(__func__);
1093 // TODO(nobody): We don't support sideband
1094 return unsupported(__func__, stream);
1097 HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSourceCrop(hwc_frect_t crop) {
1098 supported(__func__);
1099 source_crop_ = crop;
1100 return HWC2::Error::None;
1103 HWC2::Error DrmHwcTwo::HwcLayer::SetLayerSurfaceDamage(hwc_region_t damage) {
1104 supported(__func__);
1105 // TODO(nobody): We don't use surface damage, marking as unsupported
1106 unsupported(__func__, damage);
1107 return HWC2::Error::None;
1110 HWC2::Error DrmHwcTwo::HwcLayer::SetLayerTransform(int32_t transform) {
1111 supported(__func__);
1112 transform_ = static_cast<HWC2::Transform>(transform);
1113 return HWC2::Error::None;
1116 HWC2::Error DrmHwcTwo::HwcLayer::SetLayerVisibleRegion(hwc_region_t visible) {
1117 supported(__func__);
1118 // TODO(nobody): We don't use this information, marking as unsupported
1119 unsupported(__func__, visible);
1120 return HWC2::Error::None;
1123 HWC2::Error DrmHwcTwo::HwcLayer::SetLayerZOrder(uint32_t order) {
1124 supported(__func__);
1126 return HWC2::Error::None;
1129 void DrmHwcTwo::HwcLayer::PopulateDrmLayer(DrmHwcLayer *layer) {
1130 supported(__func__);
1131 switch (blending_) {
1132 case HWC2::BlendMode::None:
1133 layer->blending = DrmHwcBlending::kNone;
1135 case HWC2::BlendMode::Premultiplied:
1136 layer->blending = DrmHwcBlending::kPreMult;
1138 case HWC2::BlendMode::Coverage:
1139 layer->blending = DrmHwcBlending::kCoverage;
1142 ALOGE("Unknown blending mode b=%d", blending_);
1143 layer->blending = DrmHwcBlending::kNone;
1147 OutputFd release_fence = release_fence_output();
1149 layer->sf_handle = buffer_;
1150 layer->acquire_fence = acquire_fence_.Release();
1151 layer->release_fence = std::move(release_fence);
1152 layer->SetDisplayFrame(display_frame_);
1153 layer->alpha = lround(65535.0F * alpha_);
1154 layer->SetSourceCrop(source_crop_);
1155 layer->SetTransform(static_cast<int32_t>(transform_));
1156 layer->dataspace = dataspace_;
1159 void DrmHwcTwo::HandleDisplayHotplug(hwc2_display_t displayid, int state) {
1160 const std::lock_guard<std::mutex> lock(hotplug_callback_lock);
1162 if (hotplug_callback_hook_ && hotplug_callback_data_)
1163 hotplug_callback_hook_(hotplug_callback_data_, displayid,
1164 state == DRM_MODE_CONNECTED
1165 ? HWC2_CONNECTION_CONNECTED
1166 : HWC2_CONNECTION_DISCONNECTED);
1169 void DrmHwcTwo::HandleInitialHotplugState(DrmDevice *drmDevice) {
1170 for (const auto &conn : drmDevice->connectors()) {
1171 if (conn->state() != DRM_MODE_CONNECTED)
1173 HandleDisplayHotplug(conn->display(), conn->state());
1177 void DrmHwcTwo::DrmHotplugHandler::HandleEvent(uint64_t timestamp_us) {
1178 for (const auto &conn : drm_->connectors()) {
1179 drmModeConnection old_state = conn->state();
1180 drmModeConnection cur_state = conn->UpdateModes()
1181 ? DRM_MODE_UNKNOWNCONNECTION
1184 if (cur_state == old_state)
1187 ALOGI("%s event @%" PRIu64 " for connector %u on display %d",
1188 cur_state == DRM_MODE_CONNECTED ? "Plug" : "Unplug", timestamp_us,
1189 conn->id(), conn->display());
1191 int display_id = conn->display();
1192 if (cur_state == DRM_MODE_CONNECTED) {
1193 auto &display = hwc2_->displays_.at(display_id);
1194 display.ChosePreferredConfig();
1196 auto &display = hwc2_->displays_.at(display_id);
1197 display.ClearDisplay();
1200 hwc2_->HandleDisplayHotplug(display_id, cur_state);
1205 int DrmHwcTwo::HookDevClose(hw_device_t * /*dev*/) {
1206 unsupported(__func__);
1211 void DrmHwcTwo::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
1212 uint32_t *out_count,
1213 int32_t * /*out_capabilities*/) {
1214 supported(__func__);
1219 hwc2_function_pointer_t DrmHwcTwo::HookDevGetFunction(
1220 struct hwc2_device * /*dev*/, int32_t descriptor) {
1221 supported(__func__);
1222 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
1225 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
1226 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
1227 DeviceHook<int32_t, decltype(&DrmHwcTwo::CreateVirtualDisplay),
1228 &DrmHwcTwo::CreateVirtualDisplay, uint32_t, uint32_t,
1229 int32_t *, hwc2_display_t *>);
1230 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
1231 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
1232 DeviceHook<int32_t, decltype(&DrmHwcTwo::DestroyVirtualDisplay),
1233 &DrmHwcTwo::DestroyVirtualDisplay, hwc2_display_t>);
1234 case HWC2::FunctionDescriptor::Dump:
1235 return ToHook<HWC2_PFN_DUMP>(
1236 DeviceHook<void, decltype(&DrmHwcTwo::Dump), &DrmHwcTwo::Dump,
1237 uint32_t *, char *>);
1238 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
1239 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
1240 DeviceHook<uint32_t, decltype(&DrmHwcTwo::GetMaxVirtualDisplayCount),
1241 &DrmHwcTwo::GetMaxVirtualDisplayCount>);
1242 case HWC2::FunctionDescriptor::RegisterCallback:
1243 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
1244 DeviceHook<int32_t, decltype(&DrmHwcTwo::RegisterCallback),
1245 &DrmHwcTwo::RegisterCallback, int32_t,
1246 hwc2_callback_data_t, hwc2_function_pointer_t>);
1248 // Display functions
1249 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
1250 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
1251 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
1252 &HwcDisplay::AcceptDisplayChanges>);
1253 case HWC2::FunctionDescriptor::CreateLayer:
1254 return ToHook<HWC2_PFN_CREATE_LAYER>(
1255 DisplayHook<decltype(&HwcDisplay::CreateLayer),
1256 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
1257 case HWC2::FunctionDescriptor::DestroyLayer:
1258 return ToHook<HWC2_PFN_DESTROY_LAYER>(
1259 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
1260 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
1261 case HWC2::FunctionDescriptor::GetActiveConfig:
1262 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
1263 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
1264 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
1265 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
1266 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
1267 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
1268 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
1269 hwc2_layer_t *, int32_t *>);
1270 case HWC2::FunctionDescriptor::GetClientTargetSupport:
1271 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
1272 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
1273 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1275 case HWC2::FunctionDescriptor::GetColorModes:
1276 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1277 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1278 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1279 case HWC2::FunctionDescriptor::GetDisplayAttribute:
1280 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(
1281 DisplayHook<decltype(&HwcDisplay::GetDisplayAttribute),
1282 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t,
1284 case HWC2::FunctionDescriptor::GetDisplayConfigs:
1285 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(
1286 DisplayHook<decltype(&HwcDisplay::GetDisplayConfigs),
1287 &HwcDisplay::GetDisplayConfigs, uint32_t *,
1289 case HWC2::FunctionDescriptor::GetDisplayName:
1290 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1291 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1292 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1293 case HWC2::FunctionDescriptor::GetDisplayRequests:
1294 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1295 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1296 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1297 hwc2_layer_t *, int32_t *>);
1298 case HWC2::FunctionDescriptor::GetDisplayType:
1299 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1300 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1301 &HwcDisplay::GetDisplayType, int32_t *>);
1302 case HWC2::FunctionDescriptor::GetDozeSupport:
1303 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1304 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1305 &HwcDisplay::GetDozeSupport, int32_t *>);
1306 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1307 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1308 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1309 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1310 float *, float *, float *>);
1311 case HWC2::FunctionDescriptor::GetReleaseFences:
1312 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1313 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1314 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1316 case HWC2::FunctionDescriptor::PresentDisplay:
1317 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1318 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1319 &HwcDisplay::PresentDisplay, int32_t *>);
1320 case HWC2::FunctionDescriptor::SetActiveConfig:
1321 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1322 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1323 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1324 case HWC2::FunctionDescriptor::SetClientTarget:
1325 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(
1326 DisplayHook<decltype(&HwcDisplay::SetClientTarget),
1327 &HwcDisplay::SetClientTarget, buffer_handle_t, int32_t,
1328 int32_t, hwc_region_t>);
1329 case HWC2::FunctionDescriptor::SetColorMode:
1330 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1331 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1332 &HwcDisplay::SetColorMode, int32_t>);
1333 case HWC2::FunctionDescriptor::SetColorTransform:
1334 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1335 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1336 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1337 case HWC2::FunctionDescriptor::SetOutputBuffer:
1338 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1339 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1340 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1341 case HWC2::FunctionDescriptor::SetPowerMode:
1342 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1343 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1344 &HwcDisplay::SetPowerMode, int32_t>);
1345 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1346 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1347 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1348 &HwcDisplay::SetVsyncEnabled, int32_t>);
1349 case HWC2::FunctionDescriptor::ValidateDisplay:
1350 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1351 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1352 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
1353 #if PLATFORM_SDK_VERSION > 27
1354 case HWC2::FunctionDescriptor::GetRenderIntents:
1355 return ToHook<HWC2_PFN_GET_RENDER_INTENTS>(
1356 DisplayHook<decltype(&HwcDisplay::GetRenderIntents),
1357 &HwcDisplay::GetRenderIntents, int32_t, uint32_t *,
1359 case HWC2::FunctionDescriptor::SetColorModeWithRenderIntent:
1360 return ToHook<HWC2_PFN_SET_COLOR_MODE_WITH_RENDER_INTENT>(
1361 DisplayHook<decltype(&HwcDisplay::SetColorModeWithIntent),
1362 &HwcDisplay::SetColorModeWithIntent, int32_t, int32_t>);
1364 #if PLATFORM_SDK_VERSION > 28
1365 case HWC2::FunctionDescriptor::GetDisplayIdentificationData:
1366 return ToHook<HWC2_PFN_GET_DISPLAY_IDENTIFICATION_DATA>(
1367 DisplayHook<decltype(&HwcDisplay::GetDisplayIdentificationData),
1368 &HwcDisplay::GetDisplayIdentificationData, uint8_t *,
1369 uint32_t *, uint8_t *>);
1370 case HWC2::FunctionDescriptor::GetDisplayCapabilities:
1371 return ToHook<HWC2_PFN_GET_DISPLAY_CAPABILITIES>(
1372 DisplayHook<decltype(&HwcDisplay::GetDisplayCapabilities),
1373 &HwcDisplay::GetDisplayCapabilities, uint32_t *,
1375 case HWC2::FunctionDescriptor::GetDisplayBrightnessSupport:
1376 return ToHook<HWC2_PFN_GET_DISPLAY_BRIGHTNESS_SUPPORT>(
1377 DisplayHook<decltype(&HwcDisplay::GetDisplayBrightnessSupport),
1378 &HwcDisplay::GetDisplayBrightnessSupport, bool *>);
1379 case HWC2::FunctionDescriptor::SetDisplayBrightness:
1380 return ToHook<HWC2_PFN_SET_DISPLAY_BRIGHTNESS>(
1381 DisplayHook<decltype(&HwcDisplay::SetDisplayBrightness),
1382 &HwcDisplay::SetDisplayBrightness, float>);
1383 #endif /* PLATFORM_SDK_VERSION > 28 */
1384 #if PLATFORM_SDK_VERSION > 29
1385 case HWC2::FunctionDescriptor::GetDisplayConnectionType:
1386 return ToHook<HWC2_PFN_GET_DISPLAY_CONNECTION_TYPE>(
1387 DisplayHook<decltype(&HwcDisplay::GetDisplayConnectionType),
1388 &HwcDisplay::GetDisplayConnectionType, uint32_t *>);
1389 case HWC2::FunctionDescriptor::GetDisplayVsyncPeriod:
1390 return ToHook<HWC2_PFN_GET_DISPLAY_VSYNC_PERIOD>(
1391 DisplayHook<decltype(&HwcDisplay::GetDisplayVsyncPeriod),
1392 &HwcDisplay::GetDisplayVsyncPeriod,
1393 hwc2_vsync_period_t *>);
1394 case HWC2::FunctionDescriptor::SetActiveConfigWithConstraints:
1395 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG_WITH_CONSTRAINTS>(
1396 DisplayHook<decltype(&HwcDisplay::SetActiveConfigWithConstraints),
1397 &HwcDisplay::SetActiveConfigWithConstraints,
1398 hwc2_config_t, hwc_vsync_period_change_constraints_t *,
1399 hwc_vsync_period_change_timeline_t *>);
1400 case HWC2::FunctionDescriptor::SetAutoLowLatencyMode:
1401 return ToHook<HWC2_PFN_SET_AUTO_LOW_LATENCY_MODE>(
1402 DisplayHook<decltype(&HwcDisplay::SetAutoLowLatencyMode),
1403 &HwcDisplay::SetAutoLowLatencyMode, bool>);
1404 case HWC2::FunctionDescriptor::GetSupportedContentTypes:
1405 return ToHook<HWC2_PFN_GET_SUPPORTED_CONTENT_TYPES>(
1406 DisplayHook<decltype(&HwcDisplay::GetSupportedContentTypes),
1407 &HwcDisplay::GetSupportedContentTypes, uint32_t *,
1409 case HWC2::FunctionDescriptor::SetContentType:
1410 return ToHook<HWC2_PFN_SET_CONTENT_TYPE>(
1411 DisplayHook<decltype(&HwcDisplay::SetContentType),
1412 &HwcDisplay::SetContentType, int32_t>);
1415 case HWC2::FunctionDescriptor::SetCursorPosition:
1416 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1417 LayerHook<decltype(&HwcLayer::SetCursorPosition),
1418 &HwcLayer::SetCursorPosition, int32_t, int32_t>);
1419 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1420 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1421 LayerHook<decltype(&HwcLayer::SetLayerBlendMode),
1422 &HwcLayer::SetLayerBlendMode, int32_t>);
1423 case HWC2::FunctionDescriptor::SetLayerBuffer:
1424 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1425 LayerHook<decltype(&HwcLayer::SetLayerBuffer),
1426 &HwcLayer::SetLayerBuffer, buffer_handle_t, int32_t>);
1427 case HWC2::FunctionDescriptor::SetLayerColor:
1428 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1429 LayerHook<decltype(&HwcLayer::SetLayerColor),
1430 &HwcLayer::SetLayerColor, hwc_color_t>);
1431 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1432 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1433 LayerHook<decltype(&HwcLayer::SetLayerCompositionType),
1434 &HwcLayer::SetLayerCompositionType, int32_t>);
1435 case HWC2::FunctionDescriptor::SetLayerDataspace:
1436 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1437 LayerHook<decltype(&HwcLayer::SetLayerDataspace),
1438 &HwcLayer::SetLayerDataspace, int32_t>);
1439 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1440 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1441 LayerHook<decltype(&HwcLayer::SetLayerDisplayFrame),
1442 &HwcLayer::SetLayerDisplayFrame, hwc_rect_t>);
1443 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1444 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1445 LayerHook<decltype(&HwcLayer::SetLayerPlaneAlpha),
1446 &HwcLayer::SetLayerPlaneAlpha, float>);
1447 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
1448 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(
1449 LayerHook<decltype(&HwcLayer::SetLayerSidebandStream),
1450 &HwcLayer::SetLayerSidebandStream,
1451 const native_handle_t *>);
1452 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1453 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1454 LayerHook<decltype(&HwcLayer::SetLayerSourceCrop),
1455 &HwcLayer::SetLayerSourceCrop, hwc_frect_t>);
1456 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1457 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1458 LayerHook<decltype(&HwcLayer::SetLayerSurfaceDamage),
1459 &HwcLayer::SetLayerSurfaceDamage, hwc_region_t>);
1460 case HWC2::FunctionDescriptor::SetLayerTransform:
1461 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1462 LayerHook<decltype(&HwcLayer::SetLayerTransform),
1463 &HwcLayer::SetLayerTransform, int32_t>);
1464 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1465 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1466 LayerHook<decltype(&HwcLayer::SetLayerVisibleRegion),
1467 &HwcLayer::SetLayerVisibleRegion, hwc_region_t>);
1468 case HWC2::FunctionDescriptor::SetLayerZOrder:
1469 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1470 LayerHook<decltype(&HwcLayer::SetLayerZOrder),
1471 &HwcLayer::SetLayerZOrder, uint32_t>);
1472 case HWC2::FunctionDescriptor::Invalid:
1479 int DrmHwcTwo::HookDevOpen(const struct hw_module_t *module, const char *name,
1480 struct hw_device_t **dev) {
1481 supported(__func__);
1482 if (strcmp(name, HWC_HARDWARE_COMPOSER) != 0) {
1483 ALOGE("Invalid module name- %s", name);
1487 std::unique_ptr<DrmHwcTwo> ctx(new DrmHwcTwo());
1489 ALOGE("Failed to allocate DrmHwcTwo");
1493 HWC2::Error err = ctx->Init();
1494 if (err != HWC2::Error::None) {
1495 ALOGE("Failed to initialize DrmHwcTwo err=%d\n", err);
1499 ctx->common.module = (hw_module_t *)module;
1500 *dev = &ctx->common;
1501 ctx.release(); // NOLINT(bugprone-unused-return-value)
1504 } // namespace android
1506 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
1507 static struct hw_module_methods_t hwc2_module_methods = {
1508 .open = android::DrmHwcTwo::HookDevOpen,
1511 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
1512 hw_module_t HAL_MODULE_INFO_SYM = {
1513 .tag = HARDWARE_MODULE_TAG,
1514 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1515 .id = HWC_HARDWARE_MODULE_ID,
1516 .name = "DrmHwcTwo module",
1517 .author = "The Android Open Source Project",
1518 .methods = &hwc2_module_methods,