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
20 #include "utils_android.h"
24 #include <cutils/log.h>
25 #include <cutils/properties.h>
26 #include <hardware/hardware.h>
27 #include <hardware/hwcomposer2.h>
28 #include <system/graphics.h>
30 #include <gpudevice.h>
32 #include <nativedisplay.h>
33 #include "mosaicdisplay.h"
42 class IAVsyncCallback : public hwcomposer::VsyncCallback {
44 IAVsyncCallback(hwc2_callback_data_t data, hwc2_function_pointer_t hook)
45 : data_(data), hook_(hook) {
48 void Callback(uint32_t display, int64_t timestamp) {
50 auto hook = reinterpret_cast<HWC2_PFN_VSYNC>(hook_);
51 hook(data_, display, timestamp);
56 hwc2_callback_data_t data_;
57 hwc2_function_pointer_t hook_;
60 class IARefreshCallback : public hwcomposer::RefreshCallback {
62 IARefreshCallback(hwc2_callback_data_t data, hwc2_function_pointer_t hook)
63 : data_(data), hook_(hook) {
66 void Callback(uint32_t display) {
68 auto hook = reinterpret_cast<HWC2_PFN_REFRESH>(hook_);
74 hwc2_callback_data_t data_;
75 hwc2_function_pointer_t hook_;
78 class IAHotPlugEventCallback : public hwcomposer::HotPlugCallback {
80 IAHotPlugEventCallback(hwc2_callback_data_t data,
81 hwc2_function_pointer_t hook,
82 IAHWC2::HwcDisplay *display)
83 : data_(data), hook_(hook), display_(display) {
86 void Callback(uint32_t display, bool connected) {
88 // SF expects primary display to be always
89 // connected. Let's notify it first time and ignore
90 // any followup status changes.
99 auto hook = reinterpret_cast<HWC2_PFN_HOTPLUG>(hook_);
100 int32_t status = static_cast<int32_t>(HWC2::Connection::Connected);
102 status = static_cast<int32_t>(HWC2::Connection::Disconnected);
106 "IAHotPlugEventCallback called displayid: %d status: %d \n", display,
109 hook(data_, display, status);
111 // FIXME: SurfaceFlinger doesn't seem to reset layers correctly
112 // when display is connected/disconnected. We force it here.
113 // Remove this workaround once fixed correctly in SurfaceFlinger.
114 if (!connected && display > 0) {
115 display_->FreeAllLayers();
120 hwc2_callback_data_t data_;
121 hwc2_function_pointer_t hook_;
122 IAHWC2::HwcDisplay *display_;
123 bool notified_ = false;
127 common.tag = HARDWARE_DEVICE_TAG;
128 common.version = HWC_DEVICE_API_VERSION_2_0;
129 common.close = HookDevClose;
130 getCapabilities = HookDevGetCapabilities;
131 getFunction = HookDevGetFunction;
134 HWC2::Error IAHWC2::Init() {
135 char value[PROPERTY_VALUE_MAX];
136 property_get("board.disable.explicit.sync", value, "0");
137 disable_explicit_sync_ = atoi(value);
138 if (disable_explicit_sync_)
139 ALOGI("EXPLICIT SYNC support is disabled");
141 ALOGI("EXPLICIT SYNC support is enabled");
143 if (!device_.Initialize()) {
144 ALOGE("Can't initialize drm object.");
145 return HWC2::Error::NoResources;
148 std::vector<NativeDisplay *> displays = device_.GetAllDisplays();
149 size_t size = displays.size();
150 NativeDisplay *primary_display = NULL;
151 for (size_t i = 0; i < size; i++) {
152 hwcomposer::NativeDisplay *display = displays.at(i);
153 if (display->IsConnected()) {
154 primary_display = display;
159 if (!primary_display) {
160 primary_display = displays.at(0);
163 uint32_t external_display_id = 1;
164 primary_display_.Init(primary_display, 0, disable_explicit_sync_);
166 for (size_t i = 0; i < size; ++i) {
167 hwcomposer::NativeDisplay *display = displays.at(i);
168 if (display == primary_display)
171 std::unique_ptr<HwcDisplay> temp(new HwcDisplay());
172 temp->Init(display, external_display_id, disable_explicit_sync_);
173 extended_displays_.emplace_back(std::move(temp));
174 external_display_id++;
175 // Let's not confuse things with Virtual Display.
176 if (external_display_id == HWC_DISPLAY_VIRTUAL)
177 external_display_id = HWC_DISPLAY_VIRTUAL + 1;
180 // Start the hwc service
181 // FIXME(IAHWC-76): On Android, with userdebug on Joule this is causing
182 // to hang in a loop while cold boot before going to home screen
183 // hwcService_.Start(*this);
185 return HWC2::Error::None;
188 template <typename... Args>
189 static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
190 ALOGV("Unsupported function: %s", func);
191 return HWC2::Error::Unsupported;
194 static inline void supported(char const *func) {
195 ALOGV("supported function: %s", func);
198 HWC2::Error IAHWC2::CreateVirtualDisplay(uint32_t width, uint32_t height,
200 hwc2_display_t *display) {
201 *display = (hwc2_display_t)HWC_DISPLAY_VIRTUAL;
202 virtual_display_.InitVirtualDisplay(device_.GetVirtualDisplay(), width,
203 height, disable_explicit_sync_);
204 if (*format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
205 // fallback to RGBA_8888, align with framework requirement
206 *format = HAL_PIXEL_FORMAT_RGBA_8888;
209 return HWC2::Error::None;
212 HWC2::Error IAHWC2::DestroyVirtualDisplay(hwc2_display_t display) {
213 if (display != (hwc2_display_t)HWC_DISPLAY_VIRTUAL) {
214 ALOGE("Not Virtual Display Type in DestroyVirtualDisplay");
215 return HWC2::Error::BadDisplay;
218 return HWC2::Error::None;
221 void IAHWC2::Dump(uint32_t *size, char *buffer) {
222 // TODO: Implement dump
223 unsupported(__func__, size, buffer);
226 uint32_t IAHWC2::GetMaxVirtualDisplayCount() {
230 HWC2::Error IAHWC2::RegisterCallback(int32_t descriptor,
231 hwc2_callback_data_t data,
232 hwc2_function_pointer_t function) {
234 auto callback = static_cast<HWC2::Callback>(descriptor);
235 size_t size = extended_displays_.size();
238 case HWC2::Callback::Hotplug: {
239 primary_display_.RegisterHotPlugCallback(data, function);
240 for (size_t i = 0; i < size; ++i) {
241 IAHWC2::HwcDisplay *display = extended_displays_.at(i).get();
242 display->RegisterHotPlugCallback(data, function);
247 case HWC2::Callback::Vsync: {
248 primary_display_.RegisterVsyncCallback(data, function);
249 for (size_t i = 0; i < size; ++i) {
250 IAHWC2::HwcDisplay *display = extended_displays_.at(i).get();
251 display->RegisterVsyncCallback(data, function);
256 case HWC2::Callback::Refresh: {
257 primary_display_.RegisterRefreshCallback(data, function);
258 for (size_t i = 0; i < size; ++i) {
259 IAHWC2::HwcDisplay *display = extended_displays_.at(i).get();
260 display->RegisterRefreshCallback(data, function);
268 return HWC2::Error::None;
271 IAHWC2::HwcDisplay::HwcDisplay() {
275 // This function will be called only for Virtual Display Init
276 HWC2::Error IAHWC2::HwcDisplay::InitVirtualDisplay(
277 hwcomposer::NativeDisplay *display, uint32_t width, uint32_t height,
278 bool disable_explicit_sync) {
281 type_ = HWC2::DisplayType::Virtual;
282 handle_ = HWC_DISPLAY_VIRTUAL;
283 display_->InitVirtualDisplay(width, height);
284 disable_explicit_sync_ = disable_explicit_sync;
285 display_->SetExplicitSyncSupport(disable_explicit_sync_);
286 return HWC2::Error::None;
289 HWC2::Error IAHWC2::HwcDisplay::Init(hwcomposer::NativeDisplay *display,
291 bool disable_explicit_sync) {
294 type_ = HWC2::DisplayType::Physical;
295 handle_ = display_index;
297 disable_explicit_sync_ = disable_explicit_sync;
298 display_->SetExplicitSyncSupport(disable_explicit_sync_);
299 if (!display_->IsConnected()) {
300 return HWC2::Error::None;
302 // Fetch the number of modes from the display
303 uint32_t num_configs;
304 HWC2::Error err = GetDisplayConfigs(&num_configs, NULL);
305 if (err != HWC2::Error::None || !num_configs)
308 // Grab the first mode, we'll choose this as the active mode
309 hwc2_config_t default_config;
311 err = GetDisplayConfigs(&num_configs, &default_config);
312 if (err != HWC2::Error::None)
315 return SetActiveConfig(default_config);
318 HWC2::Error IAHWC2::HwcDisplay::RegisterVsyncCallback(
319 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
321 auto callback = std::make_shared<IAVsyncCallback>(data, func);
322 int ret = display_->RegisterVsyncCallback(std::move(callback),
323 static_cast<int>(handle_));
325 ALOGE("Failed to register callback d=%" PRIu64 " ret=%d", handle_, ret);
326 return HWC2::Error::BadDisplay;
328 return HWC2::Error::None;
331 HWC2::Error IAHWC2::HwcDisplay::RegisterRefreshCallback(
332 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
334 auto callback = std::make_shared<IARefreshCallback>(data, func);
335 display_->RegisterRefreshCallback(std::move(callback),
336 static_cast<int>(handle_));
337 return HWC2::Error::None;
340 HWC2::Error IAHWC2::HwcDisplay::RegisterHotPlugCallback(
341 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
343 auto callback = std::make_shared<IAHotPlugEventCallback>(data, func, this);
344 display_->RegisterHotPlugCallback(std::move(callback),
345 static_cast<int>(handle_));
346 return HWC2::Error::None;
349 HWC2::Error IAHWC2::HwcDisplay::AcceptDisplayChanges() {
351 if (!checkValidateDisplay) {
352 ALOGV("AcceptChanges failed, not validated");
353 return HWC2::Error::NotValidated;
356 for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_)
357 l.second.accept_type_change();
359 // reset the value to false
360 checkValidateDisplay = false;
361 return HWC2::Error::None;
364 HWC2::Error IAHWC2::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
366 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), IAHWC2::Hwc2Layer());
367 layers_.at(layer_idx_).XTranslateCoordinates(display_->GetXTranslation());
368 *layer = static_cast<hwc2_layer_t>(layer_idx_);
370 return HWC2::Error::None;
373 HWC2::Error IAHWC2::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
376 return HWC2::Error::None;
378 layers_.erase(layer);
379 return HWC2::Error::None;
382 void IAHWC2::HwcDisplay::FreeAllLayers() {
386 std::map<hwc2_layer_t, Hwc2Layer>().swap(layers_);
390 HWC2::Error IAHWC2::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
392 IHOTPLUGEVENTTRACE("GetActiveConfig called for Display: %p \n", display_);
394 if (!display_->GetActiveConfig(config))
395 return HWC2::Error::BadConfig;
397 return HWC2::Error::None;
400 HWC2::Error IAHWC2::HwcDisplay::GetChangedCompositionTypes(
401 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
403 uint32_t num_changes = 0;
404 for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_) {
405 if (l.second.type_changed()) {
406 if (layers && num_changes < *num_elements)
407 layers[num_changes] = l.first;
408 if (types && num_changes < *num_elements)
409 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
413 if (!layers && !types)
414 *num_elements = num_changes;
415 return HWC2::Error::None;
418 HWC2::Error IAHWC2::HwcDisplay::GetClientTargetSupport(uint32_t width,
422 if (width != display_->Width() || height != display_->Height()) {
423 return HWC2::Error::Unsupported;
426 if (format == HAL_PIXEL_FORMAT_RGBA_8888 &&
427 (dataspace == HAL_DATASPACE_UNKNOWN ||
428 dataspace == HAL_DATASPACE_STANDARD_UNSPECIFIED)) {
429 return HWC2::Error::None;
431 // Convert HAL to fourcc-based DRM formats
432 uint32_t drm_format = GetDrmFormatFromHALFormat(format);
433 if (display_->CheckPlaneFormat(drm_format) &&
434 (dataspace == HAL_DATASPACE_UNKNOWN ||
435 dataspace == HAL_DATASPACE_STANDARD_UNSPECIFIED))
436 return HWC2::Error::None;
439 return HWC2::Error::Unsupported;
442 HWC2::Error IAHWC2::HwcDisplay::GetColorModes(uint32_t *num_modes,
449 *modes = HAL_COLOR_MODE_NATIVE;
451 return HWC2::Error::None;
454 HWC2::Error IAHWC2::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
455 int32_t attribute_in,
458 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
460 case HWC2::Attribute::Width:
461 display_->GetDisplayAttribute(
462 config, hwcomposer::HWCDisplayAttribute::kWidth, value);
464 case HWC2::Attribute::Height:
465 display_->GetDisplayAttribute(
466 config, hwcomposer::HWCDisplayAttribute::kHeight, value);
468 case HWC2::Attribute::VsyncPeriod:
470 display_->GetDisplayAttribute(
471 config, hwcomposer::HWCDisplayAttribute::kRefreshRate, value);
473 case HWC2::Attribute::DpiX:
474 // Dots per 1000 inches
475 display_->GetDisplayAttribute(
476 config, hwcomposer::HWCDisplayAttribute::kDpiX, value);
478 case HWC2::Attribute::DpiY:
479 // Dots per 1000 inches
480 display_->GetDisplayAttribute(
481 config, hwcomposer::HWCDisplayAttribute::kDpiY, value);
485 return HWC2::Error::BadConfig;
487 return HWC2::Error::None;
490 HWC2::Error IAHWC2::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
491 hwc2_config_t *configs) {
493 if (!display_->GetDisplayConfigs(num_configs, configs))
494 return HWC2::Error::BadDisplay;
496 return HWC2::Error::None;
499 HWC2::Error IAHWC2::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
501 if (!display_->GetDisplayName(size, name))
502 return HWC2::Error::BadDisplay;
504 return HWC2::Error::None;
507 HWC2::Error IAHWC2::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
508 uint32_t *num_elements,
509 hwc2_layer_t *layers,
510 int32_t *layer_requests) {
512 // TODO: I think virtual display should request
513 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
514 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
516 return HWC2::Error::None;
519 HWC2::Error IAHWC2::HwcDisplay::GetDisplayType(int32_t *type) {
521 *type = static_cast<int32_t>(type_);
522 return HWC2::Error::None;
525 HWC2::Error IAHWC2::HwcDisplay::GetDozeSupport(int32_t *support) {
528 return HWC2::Error::None;
531 HWC2::Error IAHWC2::HwcDisplay::GetHdrCapabilities(
532 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
533 float * /*max_average_luminance*/, float * /*min_luminance*/) {
536 return HWC2::Error::None;
539 HWC2::Error IAHWC2::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
540 hwc2_layer_t *layers,
543 if (layers == NULL || fences == NULL) {
544 *num_elements = layers_.size();
545 return HWC2::Error::None;
548 uint32_t num_layers = 0;
549 for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_) {
551 if (num_layers > *num_elements) {
552 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
553 return HWC2::Error::None;
556 layers[num_layers - 1] = l.first;
557 fences[num_layers - 1] = l.second.GetLayer()->GetReleaseFence();
560 *num_elements = num_layers;
561 return HWC2::Error::None;
564 HWC2::Error IAHWC2::HwcDisplay::PresentDisplay(int32_t *retire_fence) {
566 // order the layers by z-order
567 bool use_client_layer = false;
568 uint32_t client_z_order = 0;
569 bool use_cursor_layer = false;
570 uint32_t cursor_z_order = 0;
571 IAHWC2::Hwc2Layer *cursor_layer;
573 std::map<uint32_t, IAHWC2::Hwc2Layer *> z_map;
575 // if the power mode is doze suspend then its the hint that the drawing
576 // into the display has suspended and remain in the low power state and
577 // continue displaying the current state and stop applying display
578 // update from the client
579 if (display_->PowerMode() == HWC2_POWER_MODE_DOZE_SUSPEND)
580 return HWC2::Error::None;
581 for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_) {
582 if (l.second.IsCursorLayer()) {
583 use_cursor_layer = true;
584 cursor_layer = &l.second;
585 cursor_z_order = l.second.z_order();
589 switch (l.second.validated_type()) {
590 case HWC2::Composition::Device:
591 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
593 case HWC2::Composition::Client:
594 // Place it at the z_order of the highest client layer
595 use_client_layer = true;
596 client_z_order = std::max(client_z_order, l.second.z_order());
602 if (use_client_layer && client_layer_.GetLayer() &&
603 client_layer_.GetLayer()->GetNativeHandle() &&
604 client_layer_.GetLayer()->GetNativeHandle()->handle_) {
605 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
608 // Place the cursor at the highest z-order
609 if (use_cursor_layer) {
611 if (z_map.rbegin()->second->z_order() > cursor_z_order)
612 cursor_z_order = z_map.rbegin()->second->z_order() + 1;
613 else if (client_z_order > cursor_z_order)
614 cursor_z_order = client_z_order + 1;
616 z_map.emplace(std::make_pair(cursor_z_order, cursor_layer));
619 std::vector<hwcomposer::HwcLayer *> layers;
620 // now that they're ordered by z, add them to the composition
621 for (std::pair<const uint32_t, IAHWC2::Hwc2Layer *> &l : z_map) {
622 layers.emplace_back(l.second->GetLayer());
626 return HWC2::Error::None;
628 IHOTPLUGEVENTTRACE("PhysicalDisplay called for Display: %p \n", display_);
630 bool success = display_->Present(layers, retire_fence);
632 ALOGE("Failed to set layers in the composition");
633 return HWC2::Error::BadLayer;
638 return HWC2::Error::None;
641 HWC2::Error IAHWC2::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
643 if (!display_->SetActiveConfig(config)) {
644 ALOGE("Could not find active mode for %d", config);
645 return HWC2::Error::BadConfig;
648 // Setup the client layer's dimensions
649 hwc_rect_t display_frame = {.left = 0,
651 .right = static_cast<int>(display_->Width()),
652 .bottom = static_cast<int>(display_->Height())};
653 client_layer_.SetLayerDisplayFrame(display_frame);
654 hwc_frect_t source_crop = {.left = 0.0f,
656 .right = display_->Width() + 0.0f,
657 .bottom = display_->Height() + 0.0f};
658 client_layer_.SetLayerSourceCrop(source_crop);
660 return HWC2::Error::None;
663 HWC2::Error IAHWC2::HwcDisplay::SetClientTarget(buffer_handle_t target,
664 int32_t acquire_fence,
666 hwc_region_t damage) {
668 client_layer_.set_buffer(target);
669 client_layer_.set_acquire_fence(acquire_fence);
670 client_layer_.SetLayerDataspace(dataspace);
671 client_layer_.SetLayerSurfaceDamage(damage);
673 return HWC2::Error::None;
676 HWC2::Error IAHWC2::HwcDisplay::SetColorMode(int32_t mode) {
679 return HWC2::Error::None;
682 HWC2::Error IAHWC2::HwcDisplay::SetColorTransform(const float *matrix,
685 // TODO: Force client composition if we get this
687 if (hint != HAL_COLOR_TRANSFORM_IDENTITY &&
688 hint != HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX &&
689 hint != HAL_COLOR_TRANSFORM_VALUE_INVERSE &&
690 hint != HAL_COLOR_TRANSFORM_GRAYSCALE &&
691 hint != HAL_COLOR_TRANSFORM_CORRECT_PROTANOPIA &&
692 hint != HAL_COLOR_TRANSFORM_CORRECT_DEUTERANOPIA &&
693 hint != HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA)
694 return HWC2::Error::BadParameter;
696 display_->SetColorTransform(matrix, (HWCColorTransform)hint);
698 return HWC2::Error::None;
701 HWC2::Error IAHWC2::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
702 int32_t release_fence) {
705 struct gralloc_handle *temp = new struct gralloc_handle();
706 temp->handle_ = buffer;
707 display_->SetOutputBuffer(temp, release_fence);
708 return HWC2::Error::None;
711 HWC2::Error IAHWC2::HwcDisplay::SetPowerMode(int32_t mode_in) {
713 uint32_t power_mode = 0;
714 auto mode = static_cast<HWC2::PowerMode>(mode_in);
716 case HWC2::PowerMode::Off:
717 power_mode = HWC2_POWER_MODE_OFF;
719 case HWC2::PowerMode::Doze:
720 power_mode = HWC2_POWER_MODE_DOZE;
722 case HWC2::PowerMode::DozeSuspend:
723 power_mode = HWC2_POWER_MODE_DOZE_SUSPEND;
725 case HWC2::PowerMode::On:
726 power_mode = HWC2_POWER_MODE_ON;
729 ALOGI("Power mode %d is unsupported\n", mode);
730 return HWC2::Error::BadParameter;
733 display_->SetPowerMode(power_mode);
735 return HWC2::Error::None;
738 HWC2::Error IAHWC2::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
740 display_->VSyncControl(enabled);
741 return HWC2::Error::None;
744 HWC2::Error IAHWC2::HwcDisplay::ValidateDisplay(uint32_t *num_types,
745 uint32_t *num_requests) {
749 for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_) {
750 IAHWC2::Hwc2Layer &layer = l.second;
751 switch (layer.sf_type()) {
752 case HWC2::Composition::Sideband:
753 layer.set_validated_type(HWC2::Composition::Client);
756 case HWC2::Composition::Cursor:
757 layer.set_validated_type(HWC2::Composition::Device);
761 if (disable_explicit_sync_ ||
762 display_->PowerMode() == HWC2_POWER_MODE_DOZE_SUSPEND) {
763 layer.set_validated_type(HWC2::Composition::Client);
765 layer.set_validated_type(layer.sf_type());
771 checkValidateDisplay = true;
772 return HWC2::Error::None;
775 HWC2::Error IAHWC2::Hwc2Layer::SetCursorPosition(int32_t /*x*/, int32_t /*y*/) {
777 return HWC2::Error::None;
780 HWC2::Error IAHWC2::Hwc2Layer::SetLayerBlendMode(int32_t mode) {
782 switch (static_cast<HWC2::BlendMode>(mode)) {
783 case HWC2::BlendMode::None:
784 hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingNone);
786 case HWC2::BlendMode::Premultiplied:
787 hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingPremult);
789 case HWC2::BlendMode::Coverage:
790 hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingCoverage);
793 ALOGE("Unknown blending mode b=%d", mode);
794 hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingNone);
797 return HWC2::Error::None;
800 HWC2::Error IAHWC2::Hwc2Layer::SetLayerBuffer(buffer_handle_t buffer,
801 int32_t acquire_fence) {
804 // The buffer and acquire_fence are handled elsewhere
805 if (sf_type_ == HWC2::Composition::Client ||
806 sf_type_ == HWC2::Composition::Sideband)
807 return HWC2::Error::None;
809 native_handle_.handle_ = buffer;
810 hwc_layer_.SetNativeHandle(&native_handle_);
811 if (acquire_fence > 0)
812 hwc_layer_.SetAcquireFence(acquire_fence);
813 return HWC2::Error::None;
816 HWC2::Error IAHWC2::Hwc2Layer::SetLayerColor(hwc_color_t color) {
817 // We only support Opaque colors so far.
818 if (color.r == 0 && color.g == 0 && color.b == 0 && color.a == 255) {
819 sf_type_ = HWC2::Composition::SolidColor;
820 return HWC2::Error::None;
823 sf_type_ = HWC2::Composition::Client;
824 return HWC2::Error::None;
827 HWC2::Error IAHWC2::Hwc2Layer::SetLayerCompositionType(int32_t type) {
828 sf_type_ = static_cast<HWC2::Composition>(type);
829 if (sf_type_ == HWC2::Composition::Cursor) {
830 is_cursor_layer_ = true;
833 return HWC2::Error::None;
836 HWC2::Error IAHWC2::Hwc2Layer::SetLayerDataspace(int32_t dataspace) {
838 dataspace_ = static_cast<android_dataspace_t>(dataspace);
839 return HWC2::Error::None;
842 HWC2::Error IAHWC2::Hwc2Layer::SetLayerDisplayFrame(hwc_rect_t frame) {
844 hwc_layer_.SetDisplayFrame(
845 hwcomposer::HwcRect<int>(frame.left, frame.top, frame.right,
848 return HWC2::Error::None;
851 HWC2::Error IAHWC2::Hwc2Layer::SetLayerPlaneAlpha(float alpha) {
853 hwc_layer_.SetAlpha(static_cast<uint8_t>(255.0f * alpha + 0.5f));
854 return HWC2::Error::None;
857 HWC2::Error IAHWC2::Hwc2Layer::SetLayerSidebandStream(
858 const native_handle_t *stream) {
860 // TODO: We don't support sideband
861 return unsupported(__func__, stream);
864 HWC2::Error IAHWC2::Hwc2Layer::SetLayerSourceCrop(hwc_frect_t crop) {
866 hwc_layer_.SetSourceCrop(
867 hwcomposer::HwcRect<float>(crop.left, crop.top, crop.right, crop.bottom));
868 return HWC2::Error::None;
871 HWC2::Error IAHWC2::Hwc2Layer::SetLayerSurfaceDamage(hwc_region_t damage) {
872 uint32_t num_rects = damage.numRects;
873 hwcomposer::HwcRegion hwc_region;
875 for (size_t rect = 0; rect < num_rects; ++rect) {
876 hwc_region.emplace_back(damage.rects[rect].left, damage.rects[rect].top,
877 damage.rects[rect].right,
878 damage.rects[rect].bottom);
881 hwc_layer_.SetSurfaceDamage(hwc_region);
883 return HWC2::Error::None;
886 HWC2::Error IAHWC2::Hwc2Layer::SetLayerTransform(int32_t transform) {
888 // 270* and 180* cannot be combined with flips. More specifically, they
889 // already contain both horizontal and vertical flips, so those fields are
890 // redundant in this case. 90* rotation can be combined with either horizontal
891 // flip or vertical flip, so treat it differently
893 if (transform == HWC_TRANSFORM_ROT_270) {
894 temp = hwcomposer::HWCTransform::kRotate270;
895 } else if (transform == HWC_TRANSFORM_ROT_180) {
896 temp = hwcomposer::HWCTransform::kRotate180;
898 if (transform & HWC_TRANSFORM_FLIP_H)
899 temp |= hwcomposer::HWCTransform::kReflectX;
900 if (transform & HWC_TRANSFORM_FLIP_V)
901 temp |= hwcomposer::HWCTransform::kReflectY;
902 if (transform & HWC_TRANSFORM_ROT_90)
903 temp |= hwcomposer::HWCTransform::kRotate90;
905 hwc_layer_.SetTransform(temp);
906 return HWC2::Error::None;
909 HWC2::Error IAHWC2::Hwc2Layer::SetLayerVisibleRegion(hwc_region_t visible) {
910 uint32_t num_rects = visible.numRects;
911 hwcomposer::HwcRegion hwc_region;
913 for (size_t rect = 0; rect < num_rects; ++rect) {
914 hwc_region.emplace_back(visible.rects[rect].left, visible.rects[rect].top,
915 visible.rects[rect].right,
916 visible.rects[rect].bottom);
919 hwc_layer_.SetVisibleRegion(hwc_region);
920 return HWC2::Error::None;
923 HWC2::Error IAHWC2::Hwc2Layer::SetLayerZOrder(uint32_t order) {
926 hwc_layer_.SetLayerZOrder(order);
927 return HWC2::Error::None;
931 int IAHWC2::HookDevClose(hw_device_t * /*dev*/) {
932 unsupported(__func__);
937 void IAHWC2::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
939 int32_t * /*out_capabilities*/) {
945 hwc2_function_pointer_t IAHWC2::HookDevGetFunction(struct hwc2_device * /*dev*/,
946 int32_t descriptor) {
948 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
951 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
952 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
953 DeviceHook<int32_t, decltype(&IAHWC2::CreateVirtualDisplay),
954 &IAHWC2::CreateVirtualDisplay, uint32_t, uint32_t,
955 int32_t *, hwc2_display_t *>);
956 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
957 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
958 DeviceHook<int32_t, decltype(&IAHWC2::DestroyVirtualDisplay),
959 &IAHWC2::DestroyVirtualDisplay, hwc2_display_t>);
960 case HWC2::FunctionDescriptor::Dump:
961 return ToHook<HWC2_PFN_DUMP>(DeviceHook<
962 void, decltype(&IAHWC2::Dump), &IAHWC2::Dump, uint32_t *, char *>);
963 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
964 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
965 DeviceHook<uint32_t, decltype(&IAHWC2::GetMaxVirtualDisplayCount),
966 &IAHWC2::GetMaxVirtualDisplayCount>);
967 case HWC2::FunctionDescriptor::RegisterCallback:
968 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
969 DeviceHook<int32_t, decltype(&IAHWC2::RegisterCallback),
970 &IAHWC2::RegisterCallback, int32_t, hwc2_callback_data_t,
971 hwc2_function_pointer_t>);
974 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
975 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
976 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
977 &HwcDisplay::AcceptDisplayChanges>);
978 case HWC2::FunctionDescriptor::CreateLayer:
979 return ToHook<HWC2_PFN_CREATE_LAYER>(
980 DisplayHook<decltype(&HwcDisplay::CreateLayer),
981 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
982 case HWC2::FunctionDescriptor::DestroyLayer:
983 return ToHook<HWC2_PFN_DESTROY_LAYER>(
984 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
985 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
986 case HWC2::FunctionDescriptor::GetActiveConfig:
987 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
988 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
989 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
990 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
991 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
992 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
993 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
994 hwc2_layer_t *, int32_t *>);
995 case HWC2::FunctionDescriptor::GetClientTargetSupport:
996 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
997 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
998 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
1000 case HWC2::FunctionDescriptor::GetColorModes:
1001 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
1002 DisplayHook<decltype(&HwcDisplay::GetColorModes),
1003 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
1004 case HWC2::FunctionDescriptor::GetDisplayAttribute:
1005 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(DisplayHook<
1006 decltype(&HwcDisplay::GetDisplayAttribute),
1007 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t, int32_t *>);
1008 case HWC2::FunctionDescriptor::GetDisplayConfigs:
1009 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(DisplayHook<
1010 decltype(&HwcDisplay::GetDisplayConfigs),
1011 &HwcDisplay::GetDisplayConfigs, uint32_t *, hwc2_config_t *>);
1012 case HWC2::FunctionDescriptor::GetDisplayName:
1013 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
1014 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1015 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1016 case HWC2::FunctionDescriptor::GetDisplayRequests:
1017 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1018 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1019 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1020 hwc2_layer_t *, int32_t *>);
1021 case HWC2::FunctionDescriptor::GetDisplayType:
1022 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1023 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1024 &HwcDisplay::GetDisplayType, int32_t *>);
1025 case HWC2::FunctionDescriptor::GetDozeSupport:
1026 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1027 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1028 &HwcDisplay::GetDozeSupport, int32_t *>);
1029 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1030 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1031 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1032 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1033 float *, float *, float *>);
1034 case HWC2::FunctionDescriptor::GetReleaseFences:
1035 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1036 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1037 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1039 case HWC2::FunctionDescriptor::PresentDisplay:
1040 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1041 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1042 &HwcDisplay::PresentDisplay, int32_t *>);
1043 case HWC2::FunctionDescriptor::SetActiveConfig:
1044 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1045 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1046 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1047 case HWC2::FunctionDescriptor::SetClientTarget:
1048 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(DisplayHook<
1049 decltype(&HwcDisplay::SetClientTarget), &HwcDisplay::SetClientTarget,
1050 buffer_handle_t, int32_t, int32_t, hwc_region_t>);
1051 case HWC2::FunctionDescriptor::SetColorMode:
1052 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1053 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1054 &HwcDisplay::SetColorMode, int32_t>);
1055 case HWC2::FunctionDescriptor::SetColorTransform:
1056 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1057 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1058 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1059 case HWC2::FunctionDescriptor::SetOutputBuffer:
1060 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1061 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1062 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1063 case HWC2::FunctionDescriptor::SetPowerMode:
1064 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1065 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1066 &HwcDisplay::SetPowerMode, int32_t>);
1067 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1068 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1069 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1070 &HwcDisplay::SetVsyncEnabled, int32_t>);
1071 case HWC2::FunctionDescriptor::ValidateDisplay:
1072 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1073 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1074 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
1077 case HWC2::FunctionDescriptor::SetCursorPosition:
1078 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1079 LayerHook<decltype(&Hwc2Layer::SetCursorPosition),
1080 &Hwc2Layer::SetCursorPosition, int32_t, int32_t>);
1081 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1082 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1083 LayerHook<decltype(&Hwc2Layer::SetLayerBlendMode),
1084 &Hwc2Layer::SetLayerBlendMode, int32_t>);
1085 case HWC2::FunctionDescriptor::SetLayerBuffer:
1086 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1087 LayerHook<decltype(&Hwc2Layer::SetLayerBuffer),
1088 &Hwc2Layer::SetLayerBuffer, buffer_handle_t, int32_t>);
1089 case HWC2::FunctionDescriptor::SetLayerColor:
1090 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1091 LayerHook<decltype(&Hwc2Layer::SetLayerColor),
1092 &Hwc2Layer::SetLayerColor, hwc_color_t>);
1093 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1094 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1095 LayerHook<decltype(&Hwc2Layer::SetLayerCompositionType),
1096 &Hwc2Layer::SetLayerCompositionType, int32_t>);
1097 case HWC2::FunctionDescriptor::SetLayerDataspace:
1098 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1099 LayerHook<decltype(&Hwc2Layer::SetLayerDataspace),
1100 &Hwc2Layer::SetLayerDataspace, int32_t>);
1101 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1102 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1103 LayerHook<decltype(&Hwc2Layer::SetLayerDisplayFrame),
1104 &Hwc2Layer::SetLayerDisplayFrame, hwc_rect_t>);
1105 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1106 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1107 LayerHook<decltype(&Hwc2Layer::SetLayerPlaneAlpha),
1108 &Hwc2Layer::SetLayerPlaneAlpha, float>);
1109 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
1110 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(LayerHook<
1111 decltype(&Hwc2Layer::SetLayerSidebandStream),
1112 &Hwc2Layer::SetLayerSidebandStream, const native_handle_t *>);
1113 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1114 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1115 LayerHook<decltype(&Hwc2Layer::SetLayerSourceCrop),
1116 &Hwc2Layer::SetLayerSourceCrop, hwc_frect_t>);
1117 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1118 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1119 LayerHook<decltype(&Hwc2Layer::SetLayerSurfaceDamage),
1120 &Hwc2Layer::SetLayerSurfaceDamage, hwc_region_t>);
1121 case HWC2::FunctionDescriptor::SetLayerTransform:
1122 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1123 LayerHook<decltype(&Hwc2Layer::SetLayerTransform),
1124 &Hwc2Layer::SetLayerTransform, int32_t>);
1125 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1126 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1127 LayerHook<decltype(&Hwc2Layer::SetLayerVisibleRegion),
1128 &Hwc2Layer::SetLayerVisibleRegion, hwc_region_t>);
1129 case HWC2::FunctionDescriptor::SetLayerZOrder:
1130 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1131 LayerHook<decltype(&Hwc2Layer::SetLayerZOrder),
1132 &Hwc2Layer::SetLayerZOrder, uint32_t>);
1133 case HWC2::FunctionDescriptor::Invalid:
1140 int IAHWC2::HookDevOpen(const struct hw_module_t *module, const char *name,
1141 struct hw_device_t **dev) {
1142 supported(__func__);
1143 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1144 ALOGE("Invalid module name- %s", name);
1148 std::unique_ptr<IAHWC2> ctx(new IAHWC2());
1150 ALOGE("Failed to allocate IAHWC2");
1154 HWC2::Error err = ctx->Init();
1155 if (err != HWC2::Error::None) {
1156 ALOGE("Failed to initialize IAHWC2 err=%d\n", err);
1160 ctx->common.module = const_cast<hw_module_t *>(module);
1161 *dev = &ctx->common;
1166 hwcomposer::NativeDisplay *IAHWC2::GetExtendedDisplay(uint32_t dispIndex) {
1167 return extended_displays_.at(dispIndex)->GetDisplay();
1170 hwcomposer::NativeDisplay *IAHWC2::GetPrimaryDisplay() {
1171 return primary_display_.GetDisplay();
1174 hwcomposer::NativeDisplay *IAHWC2::HwcDisplay::GetDisplay() {
1178 } // namespace android
1180 static struct hw_module_methods_t hwc2_module_methods = {
1181 .open = android::IAHWC2::HookDevOpen,
1184 hw_module_t HAL_MODULE_INFO_SYM = {
1185 .tag = HARDWARE_MODULE_TAG,
1186 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1187 .id = HWC_HARDWARE_MODULE_ID,
1188 .name = "IA-Hardware-Composer",
1189 .author = "The Android Open Source Project",
1190 .methods = &hwc2_module_methods,