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>
29 #include <gpudevice.h>
31 #include <nativedisplay.h>
32 #include "mosiacdisplay.h"
41 class IAVsyncCallback : public hwcomposer::VsyncCallback {
43 IAVsyncCallback(hwc2_callback_data_t data, hwc2_function_pointer_t hook)
44 : data_(data), hook_(hook) {
47 void Callback(uint32_t display, int64_t timestamp) {
49 auto hook = reinterpret_cast<HWC2_PFN_VSYNC>(hook_);
50 hook(data_, display, timestamp);
55 hwc2_callback_data_t data_;
56 hwc2_function_pointer_t hook_;
59 class IARefreshCallback : public hwcomposer::RefreshCallback {
61 IARefreshCallback(hwc2_callback_data_t data, hwc2_function_pointer_t hook)
62 : data_(data), hook_(hook) {
65 void Callback(uint32_t display) {
67 auto hook = reinterpret_cast<HWC2_PFN_REFRESH>(hook_);
73 hwc2_callback_data_t data_;
74 hwc2_function_pointer_t hook_;
77 class IAHotPlugEventCallback : public hwcomposer::HotPlugCallback {
79 IAHotPlugEventCallback(hwc2_callback_data_t data,
80 hwc2_function_pointer_t hook,
81 IAHWC2::HwcDisplay *display)
82 : data_(data), hook_(hook), display_(display) {
85 void Callback(uint32_t display, bool connected) {
87 // SF expects primary display to be always
88 // connected. Let's notify it first time and ignore
89 // any followup status changes.
98 auto hook = reinterpret_cast<HWC2_PFN_HOTPLUG>(hook_);
99 int32_t status = static_cast<int32_t>(HWC2::Connection::Connected);
101 status = static_cast<int32_t>(HWC2::Connection::Disconnected);
105 "IAHotPlugEventCallback called displayid: %d status: %d \n", display,
108 hook(data_, display, status);
110 // FIXME: SurfaceFlinger doesn't seem to reset layers correctly
111 // when display is connected/disconnected. We force it here.
112 // Remove this workaround once fixed correctly in SurfaceFlinger.
113 if (!connected && display > 0) {
114 display_->FreeAllLayers();
119 hwc2_callback_data_t data_;
120 hwc2_function_pointer_t hook_;
121 IAHWC2::HwcDisplay *display_;
122 bool notified_ = false;
126 common.tag = HARDWARE_DEVICE_TAG;
127 common.version = HWC_DEVICE_API_VERSION_2_0;
128 common.close = HookDevClose;
129 getCapabilities = HookDevGetCapabilities;
130 getFunction = HookDevGetFunction;
133 HWC2::Error IAHWC2::Init() {
134 char value[PROPERTY_VALUE_MAX];
135 property_get("board.disable.explicit.sync", value, "0");
136 disable_explicit_sync_ = atoi(value);
137 if (disable_explicit_sync_)
138 ALOGI("EXPLICIT SYNC support is disabled");
140 ALOGI("EXPLICIT SYNC support is enabled");
142 if (!device_.Initialize()) {
143 ALOGE("Can't initialize drm object.");
144 return HWC2::Error::NoResources;
147 std::vector<NativeDisplay *> displays = device_.GetAllDisplays();
148 size_t size = displays.size();
149 NativeDisplay *primary_display = NULL;
150 for (size_t i = 0; i < size; i++) {
151 hwcomposer::NativeDisplay *display = displays.at(i);
152 if (display->IsConnected()) {
153 primary_display = display;
158 if (!primary_display) {
159 primary_display = displays.at(0);
162 uint32_t external_display_id = 1;
163 primary_display_.Init(primary_display, 0, disable_explicit_sync_);
165 for (size_t i = 0; i < size; ++i) {
166 hwcomposer::NativeDisplay *display = displays.at(i);
167 if (display == primary_display)
170 std::unique_ptr<HwcDisplay> temp(new HwcDisplay());
171 temp->Init(display, external_display_id, disable_explicit_sync_);
172 extended_displays_.emplace_back(std::move(temp));
173 external_display_id++;
174 // Let's not confuse things with Virtual.
175 if (external_display_id == 2)
176 external_display_id = 3;
179 // Start the hwc service
180 // FIXME(IAHWC-76): On Android, with userdebug on Joule this is causing
181 // to hang in a loop while cold boot before going to home screen
182 // hwcService_.Start(*this);
184 return HWC2::Error::None;
187 template <typename... Args>
188 static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
189 ALOGV("Unsupported function: %s", func);
190 return HWC2::Error::Unsupported;
193 static inline void supported(char const *func) {
194 ALOGV("supported function: %s", func);
197 HWC2::Error IAHWC2::CreateVirtualDisplay(uint32_t width, uint32_t height,
199 hwc2_display_t *display) {
200 *display = (hwc2_display_t)HWC_DISPLAY_VIRTUAL;
201 virtual_display_.InitVirtualDisplay(device_.GetVirtualDisplay(), width,
202 height, disable_explicit_sync_);
203 if (*format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
204 // fallback to RGBA_8888, align with framework requirement
205 *format = HAL_PIXEL_FORMAT_RGBA_8888;
208 return HWC2::Error::None;
211 HWC2::Error IAHWC2::DestroyVirtualDisplay(hwc2_display_t display) {
212 if (display != (hwc2_display_t)HWC_DISPLAY_VIRTUAL) {
213 ALOGE("Not Virtual Display Type in DestroyVirtualDisplay");
214 return HWC2::Error::BadDisplay;
217 return HWC2::Error::None;
220 void IAHWC2::Dump(uint32_t *size, char *buffer) {
221 // TODO: Implement dump
222 unsupported(__func__, size, buffer);
225 uint32_t IAHWC2::GetMaxVirtualDisplayCount() {
229 HWC2::Error IAHWC2::RegisterCallback(int32_t descriptor,
230 hwc2_callback_data_t data,
231 hwc2_function_pointer_t function) {
233 auto callback = static_cast<HWC2::Callback>(descriptor);
234 size_t size = extended_displays_.size();
237 case HWC2::Callback::Hotplug: {
238 primary_display_.RegisterHotPlugCallback(data, function);
239 for (size_t i = 0; i < size; ++i) {
240 IAHWC2::HwcDisplay *display = extended_displays_.at(i).get();
241 display->RegisterHotPlugCallback(data, function);
246 case HWC2::Callback::Vsync: {
247 primary_display_.RegisterVsyncCallback(data, function);
248 for (size_t i = 0; i < size; ++i) {
249 IAHWC2::HwcDisplay *display = extended_displays_.at(i).get();
250 display->RegisterVsyncCallback(data, function);
255 case HWC2::Callback::Refresh: {
256 primary_display_.RegisterRefreshCallback(data, function);
257 for (size_t i = 0; i < size; ++i) {
258 IAHWC2::HwcDisplay *display = extended_displays_.at(i).get();
259 display->RegisterRefreshCallback(data, function);
267 return HWC2::Error::None;
270 IAHWC2::HwcDisplay::HwcDisplay() {
274 // This function will be called only for Virtual Display Init
275 HWC2::Error IAHWC2::HwcDisplay::InitVirtualDisplay(
276 hwcomposer::NativeDisplay *display, uint32_t width, uint32_t height,
277 bool disable_explicit_sync) {
280 type_ = HWC2::DisplayType::Virtual;
281 handle_ = HWC_DISPLAY_VIRTUAL;
282 display_->InitVirtualDisplay(width, height);
283 disable_explicit_sync_ = disable_explicit_sync;
284 display_->SetExplicitSyncSupport(disable_explicit_sync_);
285 return HWC2::Error::None;
288 HWC2::Error IAHWC2::HwcDisplay::Init(hwcomposer::NativeDisplay *display,
290 bool disable_explicit_sync) {
293 type_ = HWC2::DisplayType::Physical;
294 handle_ = display_index;
296 disable_explicit_sync_ = disable_explicit_sync;
297 display_->SetExplicitSyncSupport(disable_explicit_sync_);
298 if (!display_->IsConnected()) {
299 return HWC2::Error::None;
301 // Fetch the number of modes from the display
302 uint32_t num_configs;
303 HWC2::Error err = GetDisplayConfigs(&num_configs, NULL);
304 if (err != HWC2::Error::None || !num_configs)
307 // Grab the first mode, we'll choose this as the active mode
308 hwc2_config_t default_config;
310 err = GetDisplayConfigs(&num_configs, &default_config);
311 if (err != HWC2::Error::None)
314 return SetActiveConfig(default_config);
317 HWC2::Error IAHWC2::HwcDisplay::RegisterVsyncCallback(
318 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
320 auto callback = std::make_shared<IAVsyncCallback>(data, func);
321 int ret = display_->RegisterVsyncCallback(std::move(callback),
322 static_cast<int>(handle_));
324 ALOGE("Failed to register callback d=%" PRIu64 " ret=%d", handle_, ret);
325 return HWC2::Error::BadDisplay;
327 return HWC2::Error::None;
330 HWC2::Error IAHWC2::HwcDisplay::RegisterRefreshCallback(
331 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
333 auto callback = std::make_shared<IARefreshCallback>(data, func);
334 display_->RegisterRefreshCallback(std::move(callback),
335 static_cast<int>(handle_));
336 return HWC2::Error::None;
339 HWC2::Error IAHWC2::HwcDisplay::RegisterHotPlugCallback(
340 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
342 auto callback = std::make_shared<IAHotPlugEventCallback>(data, func, this);
343 display_->RegisterHotPlugCallback(std::move(callback),
344 static_cast<int>(handle_));
345 return HWC2::Error::None;
348 HWC2::Error IAHWC2::HwcDisplay::AcceptDisplayChanges() {
350 if (!checkValidateDisplay) {
351 ALOGV("AcceptChanges failed, not validated");
352 return HWC2::Error::NotValidated;
355 for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_)
356 l.second.accept_type_change();
358 // reset the value to false
359 checkValidateDisplay = false;
360 return HWC2::Error::None;
363 HWC2::Error IAHWC2::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
365 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), IAHWC2::Hwc2Layer());
366 layers_.at(layer_idx_).XTranslateCoordinates(display_->GetXTranslation());
367 *layer = static_cast<hwc2_layer_t>(layer_idx_);
369 return HWC2::Error::None;
372 HWC2::Error IAHWC2::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
375 return HWC2::Error::None;
377 layers_.erase(layer);
378 return HWC2::Error::None;
381 void IAHWC2::HwcDisplay::FreeAllLayers() {
385 std::map<hwc2_layer_t, Hwc2Layer>().swap(layers_);
389 HWC2::Error IAHWC2::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
391 IHOTPLUGEVENTTRACE("GetActiveConfig called for Display: %p \n", display_);
393 if (!display_->GetActiveConfig(config))
394 return HWC2::Error::BadConfig;
396 return HWC2::Error::None;
399 HWC2::Error IAHWC2::HwcDisplay::GetChangedCompositionTypes(
400 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
402 uint32_t num_changes = 0;
403 for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_) {
404 if (l.second.type_changed()) {
405 if (layers && num_changes < *num_elements)
406 layers[num_changes] = l.first;
407 if (types && num_changes < *num_elements)
408 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
412 if (!layers && !types)
413 *num_elements = num_changes;
414 return HWC2::Error::None;
417 HWC2::Error IAHWC2::HwcDisplay::GetClientTargetSupport(uint32_t width,
421 if (width != display_->Width() || height != display_->Height()) {
422 return HWC2::Error::Unsupported;
425 if (format == HAL_PIXEL_FORMAT_RGBA_8888 &&
426 (dataspace == HAL_DATASPACE_UNKNOWN ||
427 dataspace == HAL_DATASPACE_STANDARD_UNSPECIFIED)) {
428 return HWC2::Error::None;
430 // Convert HAL to fourcc-based DRM formats
431 uint32_t drm_format = GetDrmFormatFromHALFormat(format);
432 if (display_->CheckPlaneFormat(drm_format) &&
433 (dataspace == HAL_DATASPACE_UNKNOWN ||
434 dataspace == HAL_DATASPACE_STANDARD_UNSPECIFIED))
435 return HWC2::Error::None;
438 return HWC2::Error::Unsupported;
441 HWC2::Error IAHWC2::HwcDisplay::GetColorModes(uint32_t *num_modes,
446 #ifndef DISABLE_NATIVE_COLOR_MODES
448 *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;
614 z_map.emplace(std::make_pair(cursor_z_order, cursor_layer));
617 std::vector<hwcomposer::HwcLayer *> layers;
618 // now that they're ordered by z, add them to the composition
619 for (std::pair<const uint32_t, IAHWC2::Hwc2Layer *> &l : z_map) {
620 layers.emplace_back(l.second->GetLayer());
624 return HWC2::Error::None;
626 IHOTPLUGEVENTTRACE("PhysicalDisplay called for Display: %p \n", display_);
628 bool success = display_->Present(layers, retire_fence);
630 ALOGE("Failed to set layers in the composition");
631 return HWC2::Error::BadLayer;
636 return HWC2::Error::None;
639 HWC2::Error IAHWC2::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
641 if (!display_->SetActiveConfig(config)) {
642 ALOGE("Could not find active mode for %d", config);
643 return HWC2::Error::BadConfig;
646 // Setup the client layer's dimensions
647 hwc_rect_t display_frame = {.left = 0,
649 .right = static_cast<int>(display_->Width()),
650 .bottom = static_cast<int>(display_->Height())};
651 client_layer_.SetLayerDisplayFrame(display_frame);
652 hwc_frect_t source_crop = {.left = 0.0f,
654 .right = display_->Width() + 0.0f,
655 .bottom = display_->Height() + 0.0f};
656 client_layer_.SetLayerSourceCrop(source_crop);
658 return HWC2::Error::None;
661 HWC2::Error IAHWC2::HwcDisplay::SetClientTarget(buffer_handle_t target,
662 int32_t acquire_fence,
664 hwc_region_t damage) {
666 client_layer_.set_buffer(target);
667 client_layer_.set_acquire_fence(acquire_fence);
668 client_layer_.SetLayerDataspace(dataspace);
669 client_layer_.SetLayerSurfaceDamage(damage);
671 return HWC2::Error::None;
674 HWC2::Error IAHWC2::HwcDisplay::SetColorMode(int32_t mode) {
677 return HWC2::Error::None;
680 HWC2::Error IAHWC2::HwcDisplay::SetColorTransform(const float *matrix,
683 // TODO: Force client composition if we get this
684 return unsupported(__func__, matrix, hint);
687 HWC2::Error IAHWC2::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
688 int32_t release_fence) {
691 struct gralloc_handle *temp = new struct gralloc_handle();
692 temp->handle_ = buffer;
693 display_->SetOutputBuffer(temp, release_fence);
694 return HWC2::Error::None;
697 HWC2::Error IAHWC2::HwcDisplay::SetPowerMode(int32_t mode_in) {
699 uint32_t power_mode = 0;
700 auto mode = static_cast<HWC2::PowerMode>(mode_in);
702 case HWC2::PowerMode::Off:
703 power_mode = HWC2_POWER_MODE_OFF;
705 case HWC2::PowerMode::Doze:
706 power_mode = HWC2_POWER_MODE_DOZE;
708 case HWC2::PowerMode::DozeSuspend:
709 power_mode = HWC2_POWER_MODE_DOZE_SUSPEND;
711 case HWC2::PowerMode::On:
712 power_mode = HWC2_POWER_MODE_ON;
715 ALOGI("Power mode %d is unsupported\n", mode);
716 return HWC2::Error::BadParameter;
719 display_->SetPowerMode(power_mode);
721 return HWC2::Error::None;
724 HWC2::Error IAHWC2::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
726 display_->VSyncControl(enabled);
727 return HWC2::Error::None;
730 HWC2::Error IAHWC2::HwcDisplay::ValidateDisplay(uint32_t *num_types,
731 uint32_t *num_requests) {
735 for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_) {
736 IAHWC2::Hwc2Layer &layer = l.second;
737 switch (layer.sf_type()) {
738 case HWC2::Composition::Sideband:
739 layer.set_validated_type(HWC2::Composition::Client);
742 case HWC2::Composition::Cursor:
743 layer.set_validated_type(HWC2::Composition::Device);
747 if (disable_explicit_sync_ ||
748 display_->PowerMode() == HWC2_POWER_MODE_DOZE_SUSPEND) {
749 layer.set_validated_type(HWC2::Composition::Client);
751 layer.set_validated_type(layer.sf_type());
757 checkValidateDisplay = true;
758 return HWC2::Error::None;
761 HWC2::Error IAHWC2::Hwc2Layer::SetCursorPosition(int32_t /*x*/, int32_t /*y*/) {
763 return HWC2::Error::None;
766 HWC2::Error IAHWC2::Hwc2Layer::SetLayerBlendMode(int32_t mode) {
768 switch (static_cast<HWC2::BlendMode>(mode)) {
769 case HWC2::BlendMode::None:
770 hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingNone);
772 case HWC2::BlendMode::Premultiplied:
773 hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingPremult);
775 case HWC2::BlendMode::Coverage:
776 hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingCoverage);
779 ALOGE("Unknown blending mode b=%d", mode);
780 hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingNone);
783 return HWC2::Error::None;
786 HWC2::Error IAHWC2::Hwc2Layer::SetLayerBuffer(buffer_handle_t buffer,
787 int32_t acquire_fence) {
790 // The buffer and acquire_fence are handled elsewhere
791 if (sf_type_ == HWC2::Composition::Client ||
792 sf_type_ == HWC2::Composition::Sideband)
793 return HWC2::Error::None;
795 native_handle_.handle_ = buffer;
796 hwc_layer_.SetNativeHandle(&native_handle_);
797 if (acquire_fence > 0)
798 hwc_layer_.SetAcquireFence(acquire_fence);
799 return HWC2::Error::None;
802 HWC2::Error IAHWC2::Hwc2Layer::SetLayerColor(hwc_color_t color) {
803 // We only support Opaque colors so far.
804 if (color.r == 0 && color.g == 0 && color.b == 0 && color.a == 255) {
805 sf_type_ = HWC2::Composition::SolidColor;
806 return HWC2::Error::None;
809 sf_type_ = HWC2::Composition::Client;
810 return HWC2::Error::None;
813 HWC2::Error IAHWC2::Hwc2Layer::SetLayerCompositionType(int32_t type) {
814 sf_type_ = static_cast<HWC2::Composition>(type);
815 if (sf_type_ == HWC2::Composition::Cursor) {
816 is_cursor_layer_ = true;
819 return HWC2::Error::None;
822 HWC2::Error IAHWC2::Hwc2Layer::SetLayerDataspace(int32_t dataspace) {
824 dataspace_ = static_cast<android_dataspace_t>(dataspace);
825 return HWC2::Error::None;
828 HWC2::Error IAHWC2::Hwc2Layer::SetLayerDisplayFrame(hwc_rect_t frame) {
830 hwc_layer_.SetDisplayFrame(
831 hwcomposer::HwcRect<int>(frame.left, frame.top, frame.right,
834 return HWC2::Error::None;
837 HWC2::Error IAHWC2::Hwc2Layer::SetLayerPlaneAlpha(float alpha) {
839 hwc_layer_.SetAlpha(static_cast<uint8_t>(255.0f * alpha + 0.5f));
840 return HWC2::Error::None;
843 HWC2::Error IAHWC2::Hwc2Layer::SetLayerSidebandStream(
844 const native_handle_t *stream) {
846 // TODO: We don't support sideband
847 return unsupported(__func__, stream);
850 HWC2::Error IAHWC2::Hwc2Layer::SetLayerSourceCrop(hwc_frect_t crop) {
852 hwc_layer_.SetSourceCrop(
853 hwcomposer::HwcRect<float>(crop.left, crop.top, crop.right, crop.bottom));
854 return HWC2::Error::None;
857 HWC2::Error IAHWC2::Hwc2Layer::SetLayerSurfaceDamage(hwc_region_t damage) {
858 uint32_t num_rects = damage.numRects;
859 hwcomposer::HwcRegion hwc_region;
861 for (size_t rect = 0; rect < num_rects; ++rect) {
862 hwc_region.emplace_back(damage.rects[rect].left, damage.rects[rect].top,
863 damage.rects[rect].right,
864 damage.rects[rect].bottom);
867 hwc_layer_.SetSurfaceDamage(hwc_region);
869 return HWC2::Error::None;
872 HWC2::Error IAHWC2::Hwc2Layer::SetLayerTransform(int32_t transform) {
874 // 270* and 180* cannot be combined with flips. More specifically, they
875 // already contain both horizontal and vertical flips, so those fields are
876 // redundant in this case. 90* rotation can be combined with either horizontal
877 // flip or vertical flip, so treat it differently
878 if (transform == HWC_TRANSFORM_ROT_270) {
879 transform = hwcomposer::HWCTransform::kRotate270;
880 } else if (transform == HWC_TRANSFORM_ROT_180) {
881 transform = hwcomposer::HWCTransform::kRotate180;
883 if (transform & HWC_TRANSFORM_FLIP_H)
884 transform |= hwcomposer::HWCTransform::kReflectX;
885 if (transform & HWC_TRANSFORM_FLIP_V)
886 transform |= hwcomposer::HWCTransform::kReflectY;
887 if (transform & HWC_TRANSFORM_ROT_90)
888 transform |= hwcomposer::HWCTransform::kRotate90;
890 hwc_layer_.SetTransform(transform);
891 return HWC2::Error::None;
894 HWC2::Error IAHWC2::Hwc2Layer::SetLayerVisibleRegion(hwc_region_t visible) {
895 uint32_t num_rects = visible.numRects;
896 hwcomposer::HwcRegion hwc_region;
898 for (size_t rect = 0; rect < num_rects; ++rect) {
899 hwc_region.emplace_back(visible.rects[rect].left, visible.rects[rect].top,
900 visible.rects[rect].right,
901 visible.rects[rect].bottom);
904 hwc_layer_.SetVisibleRegion(hwc_region);
905 return HWC2::Error::None;
908 HWC2::Error IAHWC2::Hwc2Layer::SetLayerZOrder(uint32_t order) {
911 hwc_layer_.SetLayerZOrder(order);
912 return HWC2::Error::None;
916 int IAHWC2::HookDevClose(hw_device_t * /*dev*/) {
917 unsupported(__func__);
922 void IAHWC2::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
924 int32_t * /*out_capabilities*/) {
930 hwc2_function_pointer_t IAHWC2::HookDevGetFunction(struct hwc2_device * /*dev*/,
931 int32_t descriptor) {
933 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
936 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
937 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
938 DeviceHook<int32_t, decltype(&IAHWC2::CreateVirtualDisplay),
939 &IAHWC2::CreateVirtualDisplay, uint32_t, uint32_t,
940 int32_t *, hwc2_display_t *>);
941 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
942 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
943 DeviceHook<int32_t, decltype(&IAHWC2::DestroyVirtualDisplay),
944 &IAHWC2::DestroyVirtualDisplay, hwc2_display_t>);
945 case HWC2::FunctionDescriptor::Dump:
946 return ToHook<HWC2_PFN_DUMP>(DeviceHook<
947 void, decltype(&IAHWC2::Dump), &IAHWC2::Dump, uint32_t *, char *>);
948 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
949 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
950 DeviceHook<uint32_t, decltype(&IAHWC2::GetMaxVirtualDisplayCount),
951 &IAHWC2::GetMaxVirtualDisplayCount>);
952 case HWC2::FunctionDescriptor::RegisterCallback:
953 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
954 DeviceHook<int32_t, decltype(&IAHWC2::RegisterCallback),
955 &IAHWC2::RegisterCallback, int32_t, hwc2_callback_data_t,
956 hwc2_function_pointer_t>);
959 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
960 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
961 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
962 &HwcDisplay::AcceptDisplayChanges>);
963 case HWC2::FunctionDescriptor::CreateLayer:
964 return ToHook<HWC2_PFN_CREATE_LAYER>(
965 DisplayHook<decltype(&HwcDisplay::CreateLayer),
966 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
967 case HWC2::FunctionDescriptor::DestroyLayer:
968 return ToHook<HWC2_PFN_DESTROY_LAYER>(
969 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
970 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
971 case HWC2::FunctionDescriptor::GetActiveConfig:
972 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
973 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
974 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
975 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
976 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
977 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
978 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
979 hwc2_layer_t *, int32_t *>);
980 case HWC2::FunctionDescriptor::GetClientTargetSupport:
981 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
982 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
983 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
985 case HWC2::FunctionDescriptor::GetColorModes:
986 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
987 DisplayHook<decltype(&HwcDisplay::GetColorModes),
988 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
989 case HWC2::FunctionDescriptor::GetDisplayAttribute:
990 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(DisplayHook<
991 decltype(&HwcDisplay::GetDisplayAttribute),
992 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t, int32_t *>);
993 case HWC2::FunctionDescriptor::GetDisplayConfigs:
994 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(DisplayHook<
995 decltype(&HwcDisplay::GetDisplayConfigs),
996 &HwcDisplay::GetDisplayConfigs, uint32_t *, hwc2_config_t *>);
997 case HWC2::FunctionDescriptor::GetDisplayName:
998 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
999 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
1000 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
1001 case HWC2::FunctionDescriptor::GetDisplayRequests:
1002 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
1003 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
1004 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
1005 hwc2_layer_t *, int32_t *>);
1006 case HWC2::FunctionDescriptor::GetDisplayType:
1007 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
1008 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
1009 &HwcDisplay::GetDisplayType, int32_t *>);
1010 case HWC2::FunctionDescriptor::GetDozeSupport:
1011 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
1012 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
1013 &HwcDisplay::GetDozeSupport, int32_t *>);
1014 case HWC2::FunctionDescriptor::GetHdrCapabilities:
1015 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
1016 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
1017 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
1018 float *, float *, float *>);
1019 case HWC2::FunctionDescriptor::GetReleaseFences:
1020 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
1021 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
1022 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
1024 case HWC2::FunctionDescriptor::PresentDisplay:
1025 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
1026 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
1027 &HwcDisplay::PresentDisplay, int32_t *>);
1028 case HWC2::FunctionDescriptor::SetActiveConfig:
1029 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
1030 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
1031 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
1032 case HWC2::FunctionDescriptor::SetClientTarget:
1033 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(DisplayHook<
1034 decltype(&HwcDisplay::SetClientTarget), &HwcDisplay::SetClientTarget,
1035 buffer_handle_t, int32_t, int32_t, hwc_region_t>);
1036 case HWC2::FunctionDescriptor::SetColorMode:
1037 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
1038 DisplayHook<decltype(&HwcDisplay::SetColorMode),
1039 &HwcDisplay::SetColorMode, int32_t>);
1040 case HWC2::FunctionDescriptor::SetColorTransform:
1041 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
1042 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
1043 &HwcDisplay::SetColorTransform, const float *, int32_t>);
1044 case HWC2::FunctionDescriptor::SetOutputBuffer:
1045 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1046 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1047 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1048 case HWC2::FunctionDescriptor::SetPowerMode:
1049 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1050 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1051 &HwcDisplay::SetPowerMode, int32_t>);
1052 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1053 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1054 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1055 &HwcDisplay::SetVsyncEnabled, int32_t>);
1056 case HWC2::FunctionDescriptor::ValidateDisplay:
1057 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1058 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1059 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
1062 case HWC2::FunctionDescriptor::SetCursorPosition:
1063 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1064 LayerHook<decltype(&Hwc2Layer::SetCursorPosition),
1065 &Hwc2Layer::SetCursorPosition, int32_t, int32_t>);
1066 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1067 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1068 LayerHook<decltype(&Hwc2Layer::SetLayerBlendMode),
1069 &Hwc2Layer::SetLayerBlendMode, int32_t>);
1070 case HWC2::FunctionDescriptor::SetLayerBuffer:
1071 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1072 LayerHook<decltype(&Hwc2Layer::SetLayerBuffer),
1073 &Hwc2Layer::SetLayerBuffer, buffer_handle_t, int32_t>);
1074 case HWC2::FunctionDescriptor::SetLayerColor:
1075 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1076 LayerHook<decltype(&Hwc2Layer::SetLayerColor),
1077 &Hwc2Layer::SetLayerColor, hwc_color_t>);
1078 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1079 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1080 LayerHook<decltype(&Hwc2Layer::SetLayerCompositionType),
1081 &Hwc2Layer::SetLayerCompositionType, int32_t>);
1082 case HWC2::FunctionDescriptor::SetLayerDataspace:
1083 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1084 LayerHook<decltype(&Hwc2Layer::SetLayerDataspace),
1085 &Hwc2Layer::SetLayerDataspace, int32_t>);
1086 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1087 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1088 LayerHook<decltype(&Hwc2Layer::SetLayerDisplayFrame),
1089 &Hwc2Layer::SetLayerDisplayFrame, hwc_rect_t>);
1090 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1091 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1092 LayerHook<decltype(&Hwc2Layer::SetLayerPlaneAlpha),
1093 &Hwc2Layer::SetLayerPlaneAlpha, float>);
1094 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
1095 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(LayerHook<
1096 decltype(&Hwc2Layer::SetLayerSidebandStream),
1097 &Hwc2Layer::SetLayerSidebandStream, const native_handle_t *>);
1098 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1099 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1100 LayerHook<decltype(&Hwc2Layer::SetLayerSourceCrop),
1101 &Hwc2Layer::SetLayerSourceCrop, hwc_frect_t>);
1102 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1103 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1104 LayerHook<decltype(&Hwc2Layer::SetLayerSurfaceDamage),
1105 &Hwc2Layer::SetLayerSurfaceDamage, hwc_region_t>);
1106 case HWC2::FunctionDescriptor::SetLayerTransform:
1107 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1108 LayerHook<decltype(&Hwc2Layer::SetLayerTransform),
1109 &Hwc2Layer::SetLayerTransform, int32_t>);
1110 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1111 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1112 LayerHook<decltype(&Hwc2Layer::SetLayerVisibleRegion),
1113 &Hwc2Layer::SetLayerVisibleRegion, hwc_region_t>);
1114 case HWC2::FunctionDescriptor::SetLayerZOrder:
1115 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1116 LayerHook<decltype(&Hwc2Layer::SetLayerZOrder),
1117 &Hwc2Layer::SetLayerZOrder, uint32_t>);
1118 case HWC2::FunctionDescriptor::Invalid:
1125 int IAHWC2::HookDevOpen(const struct hw_module_t *module, const char *name,
1126 struct hw_device_t **dev) {
1127 supported(__func__);
1128 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1129 ALOGE("Invalid module name- %s", name);
1133 std::unique_ptr<IAHWC2> ctx(new IAHWC2());
1135 ALOGE("Failed to allocate IAHWC2");
1139 HWC2::Error err = ctx->Init();
1140 if (err != HWC2::Error::None) {
1141 ALOGE("Failed to initialize IAHWC2 err=%d\n", err);
1145 ctx->common.module = const_cast<hw_module_t *>(module);
1146 *dev = &ctx->common;
1151 hwcomposer::NativeDisplay *IAHWC2::GetExtendedDisplay(uint32_t dispIndex) {
1152 return extended_displays_.at(dispIndex)->GetDisplay();
1155 hwcomposer::NativeDisplay *IAHWC2::GetPrimaryDisplay() {
1156 return primary_display_.GetDisplay();
1159 hwcomposer::NativeDisplay *IAHWC2::HwcDisplay::GetDisplay() {
1163 } // namespace android
1165 static struct hw_module_methods_t hwc2_module_methods = {
1166 .open = android::IAHWC2::HookDevOpen,
1169 hw_module_t HAL_MODULE_INFO_SYM = {
1170 .tag = HARDWARE_MODULE_TAG,
1171 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1172 .id = HWC_HARDWARE_MODULE_ID,
1173 .name = "IA-Hardware-Composer",
1174 .author = "The Android Open Source Project",
1175 .methods = &hwc2_module_methods,