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>
40 class IAVsyncCallback : public hwcomposer::VsyncCallback {
42 IAVsyncCallback(hwc2_callback_data_t data, hwc2_function_pointer_t hook)
43 : data_(data), hook_(hook) {
46 void Callback(uint32_t display, int64_t timestamp) {
47 auto hook = reinterpret_cast<HWC2_PFN_VSYNC>(hook_);
48 hook(data_, display, timestamp);
52 hwc2_callback_data_t data_;
53 hwc2_function_pointer_t hook_;
56 class IARefreshCallback : public hwcomposer::RefreshCallback {
58 IARefreshCallback(hwc2_callback_data_t data, hwc2_function_pointer_t hook)
59 : data_(data), hook_(hook) {
62 void Callback(uint32_t display) {
63 auto hook = reinterpret_cast<HWC2_PFN_REFRESH>(hook_);
68 hwc2_callback_data_t data_;
69 hwc2_function_pointer_t hook_;
72 class IAHotPlugEventCallback : public hwcomposer::HotPlugCallback {
74 IAHotPlugEventCallback(hwc2_callback_data_t data,
75 hwc2_function_pointer_t hook)
76 : data_(data), hook_(hook) {
79 void Callback(uint32_t display, bool connected) {
80 auto hook = reinterpret_cast<HWC2_PFN_HOTPLUG>(hook_);
81 int32_t status = static_cast<int32_t>(HWC2::Connection::Connected);
83 status = static_cast<int32_t>(HWC2::Connection::Disconnected);
87 "IAHotPlugEventCallback called displayid: %d status: %d \n", display,
89 hook(data_, display, status);
93 hwc2_callback_data_t data_;
94 hwc2_function_pointer_t hook_;
98 common.tag = HARDWARE_DEVICE_TAG;
99 common.version = HWC_DEVICE_API_VERSION_2_0;
100 common.close = HookDevClose;
101 getCapabilities = HookDevGetCapabilities;
102 getFunction = HookDevGetFunction;
105 HWC2::Error IAHWC2::Init() {
106 char value[PROPERTY_VALUE_MAX];
107 property_get("board.disable.explicit.sync", value, "0");
108 disable_explicit_sync_ = atoi(value);
109 if (disable_explicit_sync_)
110 ALOGI("EXPLICIT SYNC support is disabled");
112 ALOGI("EXPLICIT SYNC support is enabled");
114 if (!device_.Initialize()) {
115 ALOGE("Can't initialize drm object.");
116 return HWC2::Error::NoResources;
119 std::vector<NativeDisplay *> displays = device_.GetAllDisplays();
120 size_t size = displays.size();
121 NativeDisplay *primary_display = NULL;
122 for (size_t i = 0; i < size; i++) {
123 hwcomposer::NativeDisplay *display = displays.at(i);
124 if (display->IsConnected()) {
125 primary_display = display;
130 if (!primary_display) {
131 primary_display = displays.at(0);
134 primary_display_.Init(primary_display, 0, disable_explicit_sync_);
136 uint32_t external_display_id = 1;
137 for (size_t i = 0; i < size; ++i) {
138 hwcomposer::NativeDisplay *display = displays.at(i);
139 if (primary_display == display) {
143 std::unique_ptr<HwcDisplay> temp(new HwcDisplay());
144 temp->Init(display, external_display_id, disable_explicit_sync_);
145 extended_displays_.emplace_back(std::move(temp));
146 external_display_id++;
149 // Start the hwc service
150 // FIXME(IAHWC-76): On Android, with userdebug on Joule this is causing
151 // to hang in a loop while cold boot before going to home screen
152 // hwcService_.Start(*this);
154 return HWC2::Error::None;
157 template <typename... Args>
158 static inline HWC2::Error unsupported(char const *func, Args... /*args*/) {
159 ALOGV("Unsupported function: %s", func);
160 return HWC2::Error::Unsupported;
163 static inline void supported(char const *func) {
164 ALOGV("supported function: %s", func);
167 HWC2::Error IAHWC2::CreateVirtualDisplay(uint32_t width, uint32_t height,
169 hwc2_display_t *display) {
170 *display = (hwc2_display_t)HWC_DISPLAY_VIRTUAL;
171 virtual_display_.InitVirtualDisplay(device_.GetVirtualDisplay(), width,
172 height, disable_explicit_sync_);
173 if (*format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
174 // fallback to RGBA_8888, align with framework requirement
175 *format = HAL_PIXEL_FORMAT_RGBA_8888;
178 return HWC2::Error::None;
181 HWC2::Error IAHWC2::DestroyVirtualDisplay(hwc2_display_t display) {
182 if (display != (hwc2_display_t)HWC_DISPLAY_VIRTUAL) {
183 ALOGE("Not Virtual Display Type in DestroyVirtualDisplay");
184 return HWC2::Error::BadDisplay;
187 return HWC2::Error::None;
190 void IAHWC2::Dump(uint32_t *size, char *buffer) {
191 // TODO: Implement dump
192 unsupported(__func__, size, buffer);
195 uint32_t IAHWC2::GetMaxVirtualDisplayCount() {
199 HWC2::Error IAHWC2::RegisterCallback(int32_t descriptor,
200 hwc2_callback_data_t data,
201 hwc2_function_pointer_t function) {
203 auto callback = static_cast<HWC2::Callback>(descriptor);
204 size_t size = extended_displays_.size();
207 case HWC2::Callback::Hotplug: {
208 primary_display_.RegisterHotPlugCallback(data, function);
209 for (size_t i = 0; i < size; ++i) {
210 IAHWC2::HwcDisplay *display = extended_displays_.at(i).get();
211 display->RegisterHotPlugCallback(data, function);
212 // TODO: Remove this when we want to add support for more than 2
219 case HWC2::Callback::Vsync: {
220 primary_display_.RegisterVsyncCallback(data, function);
221 for (size_t i = 0; i < size; ++i) {
222 IAHWC2::HwcDisplay *display = extended_displays_.at(i).get();
223 display->RegisterVsyncCallback(data, function);
228 case HWC2::Callback::Refresh: {
229 primary_display_.RegisterRefreshCallback(data, function);
230 for (size_t i = 0; i < size; ++i) {
231 IAHWC2::HwcDisplay *display = extended_displays_.at(i).get();
232 display->RegisterRefreshCallback(data, function);
240 return HWC2::Error::None;
243 IAHWC2::HwcDisplay::HwcDisplay() {
247 // This function will be called only for Virtual Display Init
248 HWC2::Error IAHWC2::HwcDisplay::InitVirtualDisplay(
249 hwcomposer::NativeDisplay *display, uint32_t width, uint32_t height,
250 bool disable_explicit_sync) {
253 type_ = HWC2::DisplayType::Virtual;
254 handle_ = HWC_DISPLAY_VIRTUAL;
255 display_->InitVirtualDisplay(width, height);
256 disable_explicit_sync_ = disable_explicit_sync;
257 display_->SetExplicitSyncSupport(disable_explicit_sync_);
258 return HWC2::Error::None;
261 HWC2::Error IAHWC2::HwcDisplay::Init(hwcomposer::NativeDisplay *display,
263 bool disable_explicit_sync) {
266 type_ = HWC2::DisplayType::Physical;
267 if (display_index == 0) {
268 handle_ = HWC_DISPLAY_PRIMARY;
270 handle_ = HWC_DISPLAY_EXTERNAL;
273 disable_explicit_sync_ = disable_explicit_sync;
274 display_->SetExplicitSyncSupport(disable_explicit_sync_);
275 if (!display_->IsConnected()) {
276 return HWC2::Error::None;
278 // Fetch the number of modes from the display
279 uint32_t num_configs;
280 HWC2::Error err = GetDisplayConfigs(&num_configs, NULL);
281 if (err != HWC2::Error::None || !num_configs)
284 // Grab the first mode, we'll choose this as the active mode
285 hwc2_config_t default_config;
287 err = GetDisplayConfigs(&num_configs, &default_config);
288 if (err != HWC2::Error::None)
291 return SetActiveConfig(default_config);
294 HWC2::Error IAHWC2::HwcDisplay::RegisterVsyncCallback(
295 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
297 auto callback = std::make_shared<IAVsyncCallback>(data, func);
298 int ret = display_->RegisterVsyncCallback(std::move(callback),
299 static_cast<int>(handle_));
301 ALOGE("Failed to register callback d=%" PRIu64 " ret=%d", handle_, ret);
302 return HWC2::Error::BadDisplay;
304 return HWC2::Error::None;
307 HWC2::Error IAHWC2::HwcDisplay::RegisterRefreshCallback(
308 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
310 auto callback = std::make_shared<IARefreshCallback>(data, func);
311 display_->RegisterRefreshCallback(std::move(callback),
312 static_cast<int>(handle_));
313 return HWC2::Error::None;
316 HWC2::Error IAHWC2::HwcDisplay::RegisterHotPlugCallback(
317 hwc2_callback_data_t data, hwc2_function_pointer_t func) {
319 auto callback = std::make_shared<IAHotPlugEventCallback>(data, func);
320 display_->RegisterHotPlugCallback(std::move(callback),
321 static_cast<int>(handle_));
322 return HWC2::Error::None;
325 HWC2::Error IAHWC2::HwcDisplay::AcceptDisplayChanges() {
327 if (!checkValidateDisplay) {
328 ALOGV("AcceptChanges failed, not validated");
329 return HWC2::Error::NotValidated;
332 for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_)
333 l.second.accept_type_change();
335 // reset the value to false
336 checkValidateDisplay = false;
337 return HWC2::Error::None;
340 HWC2::Error IAHWC2::HwcDisplay::CreateLayer(hwc2_layer_t *layer) {
342 layers_.emplace(static_cast<hwc2_layer_t>(layer_idx_), IAHWC2::Hwc2Layer());
343 *layer = static_cast<hwc2_layer_t>(layer_idx_);
345 return HWC2::Error::None;
348 HWC2::Error IAHWC2::HwcDisplay::DestroyLayer(hwc2_layer_t layer) {
350 layers_.erase(layer);
351 return HWC2::Error::None;
354 HWC2::Error IAHWC2::HwcDisplay::GetActiveConfig(hwc2_config_t *config) {
356 if (!display_->GetActiveConfig(config))
357 return HWC2::Error::BadConfig;
359 return HWC2::Error::None;
362 HWC2::Error IAHWC2::HwcDisplay::GetChangedCompositionTypes(
363 uint32_t *num_elements, hwc2_layer_t *layers, int32_t *types) {
365 uint32_t num_changes = 0;
366 for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_) {
367 if (l.second.type_changed()) {
368 if (layers && num_changes < *num_elements)
369 layers[num_changes] = l.first;
370 if (types && num_changes < *num_elements)
371 types[num_changes] = static_cast<int32_t>(l.second.validated_type());
375 if (!layers && !types)
376 *num_elements = num_changes;
377 return HWC2::Error::None;
380 HWC2::Error IAHWC2::HwcDisplay::GetClientTargetSupport(uint32_t width,
384 if (width != display_->Width() || height != display_->Height()) {
385 return HWC2::Error::Unsupported;
388 if (format == HAL_PIXEL_FORMAT_RGBA_8888 &&
389 (dataspace == HAL_DATASPACE_UNKNOWN ||
390 dataspace == HAL_DATASPACE_STANDARD_UNSPECIFIED)) {
391 return HWC2::Error::None;
393 // Convert HAL to fourcc-based DRM formats
394 uint32_t drm_format = GetDrmFormatFromHALFormat(format);
395 if (display_->CheckPlaneFormat(drm_format) &&
396 (dataspace == HAL_DATASPACE_UNKNOWN ||
397 dataspace == HAL_DATASPACE_STANDARD_UNSPECIFIED))
398 return HWC2::Error::None;
401 return HWC2::Error::Unsupported;
404 HWC2::Error IAHWC2::HwcDisplay::GetColorModes(uint32_t *num_modes,
409 #ifndef DISABLE_NATIVE_COLOR_MODES
411 *modes = HAL_COLOR_MODE_NATIVE;
414 return HWC2::Error::None;
417 HWC2::Error IAHWC2::HwcDisplay::GetDisplayAttribute(hwc2_config_t config,
418 int32_t attribute_in,
421 auto attribute = static_cast<HWC2::Attribute>(attribute_in);
423 case HWC2::Attribute::Width:
424 display_->GetDisplayAttribute(
425 config, hwcomposer::HWCDisplayAttribute::kWidth, value);
427 case HWC2::Attribute::Height:
428 display_->GetDisplayAttribute(
429 config, hwcomposer::HWCDisplayAttribute::kHeight, value);
431 case HWC2::Attribute::VsyncPeriod:
433 display_->GetDisplayAttribute(
434 config, hwcomposer::HWCDisplayAttribute::kRefreshRate, value);
436 case HWC2::Attribute::DpiX:
437 // Dots per 1000 inches
438 display_->GetDisplayAttribute(
439 config, hwcomposer::HWCDisplayAttribute::kDpiX, value);
441 case HWC2::Attribute::DpiY:
442 // Dots per 1000 inches
443 display_->GetDisplayAttribute(
444 config, hwcomposer::HWCDisplayAttribute::kDpiY, value);
448 return HWC2::Error::BadConfig;
450 return HWC2::Error::None;
453 HWC2::Error IAHWC2::HwcDisplay::GetDisplayConfigs(uint32_t *num_configs,
454 hwc2_config_t *configs) {
456 if (!display_->GetDisplayConfigs(num_configs, configs))
457 return HWC2::Error::BadDisplay;
459 return HWC2::Error::None;
462 HWC2::Error IAHWC2::HwcDisplay::GetDisplayName(uint32_t *size, char *name) {
464 if (!display_->GetDisplayName(size, name))
465 return HWC2::Error::BadDisplay;
467 return HWC2::Error::None;
470 HWC2::Error IAHWC2::HwcDisplay::GetDisplayRequests(int32_t *display_requests,
471 uint32_t *num_elements,
472 hwc2_layer_t *layers,
473 int32_t *layer_requests) {
475 // TODO: I think virtual display should request
476 // HWC2_DISPLAY_REQUEST_WRITE_CLIENT_TARGET_TO_OUTPUT here
477 unsupported(__func__, display_requests, num_elements, layers, layer_requests);
479 return HWC2::Error::None;
482 HWC2::Error IAHWC2::HwcDisplay::GetDisplayType(int32_t *type) {
484 *type = static_cast<int32_t>(type_);
485 return HWC2::Error::None;
488 HWC2::Error IAHWC2::HwcDisplay::GetDozeSupport(int32_t *support) {
491 return HWC2::Error::None;
494 HWC2::Error IAHWC2::HwcDisplay::GetHdrCapabilities(
495 uint32_t *num_types, int32_t * /*types*/, float * /*max_luminance*/,
496 float * /*max_average_luminance*/, float * /*min_luminance*/) {
499 return HWC2::Error::None;
502 HWC2::Error IAHWC2::HwcDisplay::GetReleaseFences(uint32_t *num_elements,
503 hwc2_layer_t *layers,
506 if (layers == NULL || fences == NULL) {
507 *num_elements = layers_.size();
508 return HWC2::Error::None;
511 uint32_t num_layers = 0;
512 for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_) {
514 if (num_layers > *num_elements) {
515 ALOGW("Overflow num_elements %d/%d", num_layers, *num_elements);
516 return HWC2::Error::None;
519 layers[num_layers - 1] = l.first;
520 fences[num_layers - 1] = l.second.GetLayer()->GetReleaseFence();
523 *num_elements = num_layers;
524 return HWC2::Error::None;
527 HWC2::Error IAHWC2::HwcDisplay::PresentDisplay(int32_t *retire_fence) {
529 // order the layers by z-order
530 bool use_client_layer = false;
531 uint32_t client_z_order = 0;
532 bool use_cursor_layer = false;
533 uint32_t cursor_z_order = 0;
534 IAHWC2::Hwc2Layer *cursor_layer;
536 std::map<uint32_t, IAHWC2::Hwc2Layer *> z_map;
538 // if the power mode is doze suspend then its the hint that the drawing
539 // into the display has suspended and remain in the low power state and
540 // continue displaying the current state and stop applying display
541 // update from the client
542 if (display_->PowerMode() == HWC2_POWER_MODE_DOZE_SUSPEND)
543 return HWC2::Error::None;
544 for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_) {
545 switch (l.second.validated_type()) {
546 case HWC2::Composition::Device:
547 z_map.emplace(std::make_pair(l.second.z_order(), &l.second));
549 case HWC2::Composition::Cursor:
550 use_cursor_layer = true;
551 cursor_layer = &l.second;
552 cursor_z_order = l.second.z_order();
554 case HWC2::Composition::Client:
555 // Place it at the z_order of the highest client layer
556 use_client_layer = true;
557 client_z_order = std::max(client_z_order, l.second.z_order());
563 if (use_client_layer && client_layer_.GetLayer() &&
564 client_layer_.GetLayer()->GetNativeHandle() &&
565 client_layer_.GetLayer()->GetNativeHandle()->handle_) {
566 z_map.emplace(std::make_pair(client_z_order, &client_layer_));
569 // Place the cursor at the highest z-order
570 if (use_cursor_layer) {
572 if (z_map.rbegin()->second->z_order() > cursor_z_order)
573 cursor_z_order = (z_map.rbegin()->second->z_order()) + 1;
575 z_map.emplace(std::make_pair(cursor_z_order, cursor_layer));
578 std::vector<hwcomposer::HwcLayer *> layers;
579 // now that they're ordered by z, add them to the composition
580 for (std::pair<const uint32_t, IAHWC2::Hwc2Layer *> &l : z_map) {
581 layers.emplace_back(l.second->GetLayer());
585 return HWC2::Error::None;
587 bool success = display_->Present(layers, retire_fence);
589 ALOGE("Failed to set layers in the composition");
590 return HWC2::Error::BadLayer;
595 return HWC2::Error::None;
598 HWC2::Error IAHWC2::HwcDisplay::SetActiveConfig(hwc2_config_t config) {
600 if (!display_->SetActiveConfig(config)) {
601 ALOGE("Could not find active mode for %d", config);
602 return HWC2::Error::BadConfig;
605 // Setup the client layer's dimensions
606 hwc_rect_t display_frame = {.left = 0,
608 .right = static_cast<int>(display_->Width()),
609 .bottom = static_cast<int>(display_->Height())};
610 client_layer_.SetLayerDisplayFrame(display_frame);
611 hwc_frect_t source_crop = {.left = 0.0f,
613 .right = display_->Width() + 0.0f,
614 .bottom = display_->Height() + 0.0f};
615 client_layer_.SetLayerSourceCrop(source_crop);
617 return HWC2::Error::None;
620 HWC2::Error IAHWC2::HwcDisplay::SetClientTarget(buffer_handle_t target,
621 int32_t acquire_fence,
623 hwc_region_t damage) {
625 client_layer_.set_buffer(target);
626 client_layer_.set_acquire_fence(acquire_fence);
627 client_layer_.SetLayerDataspace(dataspace);
628 client_layer_.SetLayerSurfaceDamage(damage);
630 return HWC2::Error::None;
633 HWC2::Error IAHWC2::HwcDisplay::SetColorMode(int32_t mode) {
636 return HWC2::Error::None;
639 HWC2::Error IAHWC2::HwcDisplay::SetColorTransform(const float *matrix,
642 // TODO: Force client composition if we get this
643 return unsupported(__func__, matrix, hint);
646 HWC2::Error IAHWC2::HwcDisplay::SetOutputBuffer(buffer_handle_t buffer,
647 int32_t release_fence) {
650 struct gralloc_handle *temp = new struct gralloc_handle();
651 temp->handle_ = buffer;
652 display_->SetOutputBuffer(temp, release_fence);
653 return HWC2::Error::None;
656 HWC2::Error IAHWC2::HwcDisplay::SetPowerMode(int32_t mode_in) {
658 uint32_t power_mode = 0;
659 auto mode = static_cast<HWC2::PowerMode>(mode_in);
661 case HWC2::PowerMode::Off:
662 power_mode = HWC2_POWER_MODE_OFF;
664 case HWC2::PowerMode::Doze:
665 power_mode = HWC2_POWER_MODE_DOZE;
667 case HWC2::PowerMode::DozeSuspend:
668 power_mode = HWC2_POWER_MODE_DOZE_SUSPEND;
670 case HWC2::PowerMode::On:
671 power_mode = HWC2_POWER_MODE_ON;
674 ALOGI("Power mode %d is unsupported\n", mode);
675 return HWC2::Error::BadParameter;
678 display_->SetPowerMode(power_mode);
680 return HWC2::Error::None;
683 HWC2::Error IAHWC2::HwcDisplay::SetVsyncEnabled(int32_t enabled) {
685 display_->VSyncControl(enabled);
686 return HWC2::Error::None;
689 HWC2::Error IAHWC2::HwcDisplay::ValidateDisplay(uint32_t *num_types,
690 uint32_t *num_requests) {
694 for (std::pair<const hwc2_layer_t, IAHWC2::Hwc2Layer> &l : layers_) {
695 IAHWC2::Hwc2Layer &layer = l.second;
696 switch (layer.sf_type()) {
697 case HWC2::Composition::Sideband:
698 layer.set_validated_type(HWC2::Composition::Client);
701 case HWC2::Composition::Cursor:
702 layer.set_validated_type(HWC2::Composition::Device);
706 if (disable_explicit_sync_ ||
707 display_->PowerMode() == HWC2_POWER_MODE_DOZE_SUSPEND) {
708 layer.set_validated_type(HWC2::Composition::Client);
710 layer.set_validated_type(layer.sf_type());
716 checkValidateDisplay = true;
717 return HWC2::Error::None;
720 HWC2::Error IAHWC2::Hwc2Layer::SetCursorPosition(int32_t x, int32_t y) {
724 return HWC2::Error::None;
727 HWC2::Error IAHWC2::Hwc2Layer::SetLayerBlendMode(int32_t mode) {
729 switch (static_cast<HWC2::BlendMode>(mode)) {
730 case HWC2::BlendMode::None:
731 hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingNone);
733 case HWC2::BlendMode::Premultiplied:
734 hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingPremult);
736 case HWC2::BlendMode::Coverage:
737 hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingCoverage);
740 ALOGE("Unknown blending mode b=%d", mode);
741 hwc_layer_.SetBlending(hwcomposer::HWCBlending::kBlendingNone);
744 return HWC2::Error::None;
747 HWC2::Error IAHWC2::Hwc2Layer::SetLayerBuffer(buffer_handle_t buffer,
748 int32_t acquire_fence) {
751 // The buffer and acquire_fence are handled elsewhere
752 if (sf_type_ == HWC2::Composition::Client ||
753 sf_type_ == HWC2::Composition::Sideband)
754 return HWC2::Error::None;
756 native_handle_.handle_ = buffer;
757 hwc_layer_.SetNativeHandle(&native_handle_);
758 if (acquire_fence > 0)
759 hwc_layer_.SetAcquireFence(dup(acquire_fence));
760 return HWC2::Error::None;
763 HWC2::Error IAHWC2::Hwc2Layer::SetLayerColor(hwc_color_t color) {
764 // We only support Opaque colors so far.
765 if (color.r == 0 && color.g == 0 && color.b == 0 && color.a == 255) {
766 sf_type_ = HWC2::Composition::SolidColor;
767 return HWC2::Error::None;
770 sf_type_ = HWC2::Composition::Client;
771 return HWC2::Error::None;
774 HWC2::Error IAHWC2::Hwc2Layer::SetLayerCompositionType(int32_t type) {
775 sf_type_ = static_cast<HWC2::Composition>(type);
776 return HWC2::Error::None;
779 HWC2::Error IAHWC2::Hwc2Layer::SetLayerDataspace(int32_t dataspace) {
781 dataspace_ = static_cast<android_dataspace_t>(dataspace);
782 return HWC2::Error::None;
785 HWC2::Error IAHWC2::Hwc2Layer::SetLayerDisplayFrame(hwc_rect_t frame) {
787 hwc_layer_.SetDisplayFrame(hwcomposer::HwcRect<int>(
788 frame.left, frame.top, frame.right, frame.bottom));
789 return HWC2::Error::None;
792 HWC2::Error IAHWC2::Hwc2Layer::SetLayerPlaneAlpha(float alpha) {
794 hwc_layer_.SetAlpha(static_cast<uint8_t>(255.0f * alpha + 0.5f));
795 return HWC2::Error::None;
798 HWC2::Error IAHWC2::Hwc2Layer::SetLayerSidebandStream(
799 const native_handle_t *stream) {
801 // TODO: We don't support sideband
802 return unsupported(__func__, stream);
805 HWC2::Error IAHWC2::Hwc2Layer::SetLayerSourceCrop(hwc_frect_t crop) {
807 hwc_layer_.SetSourceCrop(
808 hwcomposer::HwcRect<float>(crop.left, crop.top, crop.right, crop.bottom));
809 return HWC2::Error::None;
812 HWC2::Error IAHWC2::Hwc2Layer::SetLayerSurfaceDamage(hwc_region_t damage) {
813 uint32_t num_rects = damage.numRects;
814 hwcomposer::HwcRegion hwc_region;
816 for (size_t rect = 0; rect < num_rects; ++rect) {
817 hwc_region.emplace_back(damage.rects[rect].left, damage.rects[rect].top,
818 damage.rects[rect].right,
819 damage.rects[rect].bottom);
822 hwc_layer_.SetSurfaceDamage(hwc_region);
824 return HWC2::Error::None;
827 HWC2::Error IAHWC2::Hwc2Layer::SetLayerTransform(int32_t transform) {
829 // 270* and 180* cannot be combined with flips. More specifically, they
830 // already contain both horizontal and vertical flips, so those fields are
831 // redundant in this case. 90* rotation can be combined with either horizontal
832 // flip or vertical flip, so treat it differently
833 if (transform == HWC_TRANSFORM_ROT_270) {
834 transform = hwcomposer::HWCTransform::kRotate270;
835 } else if (transform == HWC_TRANSFORM_ROT_180) {
836 transform = hwcomposer::HWCTransform::kRotate180;
838 if (transform & HWC_TRANSFORM_FLIP_H)
839 transform |= hwcomposer::HWCTransform::kReflectX;
840 if (transform & HWC_TRANSFORM_FLIP_V)
841 transform |= hwcomposer::HWCTransform::kReflectY;
842 if (transform & HWC_TRANSFORM_ROT_90)
843 transform |= hwcomposer::HWCTransform::kRotate90;
845 hwc_layer_.SetTransform(transform);
846 return HWC2::Error::None;
849 HWC2::Error IAHWC2::Hwc2Layer::SetLayerVisibleRegion(hwc_region_t visible) {
850 uint32_t num_rects = visible.numRects;
851 hwcomposer::HwcRegion hwc_region;
853 for (size_t rect = 0; rect < num_rects; ++rect) {
854 hwc_region.emplace_back(visible.rects[rect].left, visible.rects[rect].top,
855 visible.rects[rect].right,
856 visible.rects[rect].bottom);
859 hwc_layer_.SetVisibleRegion(hwc_region);
860 return HWC2::Error::None;
863 HWC2::Error IAHWC2::Hwc2Layer::SetLayerZOrder(uint32_t order) {
866 hwc_layer_.SetLayerZOrder(order);
867 return HWC2::Error::None;
871 int IAHWC2::HookDevClose(hw_device_t * /*dev*/) {
872 unsupported(__func__);
877 void IAHWC2::HookDevGetCapabilities(hwc2_device_t * /*dev*/,
879 int32_t * /*out_capabilities*/) {
885 hwc2_function_pointer_t IAHWC2::HookDevGetFunction(struct hwc2_device * /*dev*/,
886 int32_t descriptor) {
888 auto func = static_cast<HWC2::FunctionDescriptor>(descriptor);
891 case HWC2::FunctionDescriptor::CreateVirtualDisplay:
892 return ToHook<HWC2_PFN_CREATE_VIRTUAL_DISPLAY>(
893 DeviceHook<int32_t, decltype(&IAHWC2::CreateVirtualDisplay),
894 &IAHWC2::CreateVirtualDisplay, uint32_t, uint32_t,
895 int32_t *, hwc2_display_t *>);
896 case HWC2::FunctionDescriptor::DestroyVirtualDisplay:
897 return ToHook<HWC2_PFN_DESTROY_VIRTUAL_DISPLAY>(
898 DeviceHook<int32_t, decltype(&IAHWC2::DestroyVirtualDisplay),
899 &IAHWC2::DestroyVirtualDisplay, hwc2_display_t>);
900 case HWC2::FunctionDescriptor::Dump:
901 return ToHook<HWC2_PFN_DUMP>(DeviceHook<
902 void, decltype(&IAHWC2::Dump), &IAHWC2::Dump, uint32_t *, char *>);
903 case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount:
904 return ToHook<HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT>(
905 DeviceHook<uint32_t, decltype(&IAHWC2::GetMaxVirtualDisplayCount),
906 &IAHWC2::GetMaxVirtualDisplayCount>);
907 case HWC2::FunctionDescriptor::RegisterCallback:
908 return ToHook<HWC2_PFN_REGISTER_CALLBACK>(
909 DeviceHook<int32_t, decltype(&IAHWC2::RegisterCallback),
910 &IAHWC2::RegisterCallback, int32_t, hwc2_callback_data_t,
911 hwc2_function_pointer_t>);
914 case HWC2::FunctionDescriptor::AcceptDisplayChanges:
915 return ToHook<HWC2_PFN_ACCEPT_DISPLAY_CHANGES>(
916 DisplayHook<decltype(&HwcDisplay::AcceptDisplayChanges),
917 &HwcDisplay::AcceptDisplayChanges>);
918 case HWC2::FunctionDescriptor::CreateLayer:
919 return ToHook<HWC2_PFN_CREATE_LAYER>(
920 DisplayHook<decltype(&HwcDisplay::CreateLayer),
921 &HwcDisplay::CreateLayer, hwc2_layer_t *>);
922 case HWC2::FunctionDescriptor::DestroyLayer:
923 return ToHook<HWC2_PFN_DESTROY_LAYER>(
924 DisplayHook<decltype(&HwcDisplay::DestroyLayer),
925 &HwcDisplay::DestroyLayer, hwc2_layer_t>);
926 case HWC2::FunctionDescriptor::GetActiveConfig:
927 return ToHook<HWC2_PFN_GET_ACTIVE_CONFIG>(
928 DisplayHook<decltype(&HwcDisplay::GetActiveConfig),
929 &HwcDisplay::GetActiveConfig, hwc2_config_t *>);
930 case HWC2::FunctionDescriptor::GetChangedCompositionTypes:
931 return ToHook<HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES>(
932 DisplayHook<decltype(&HwcDisplay::GetChangedCompositionTypes),
933 &HwcDisplay::GetChangedCompositionTypes, uint32_t *,
934 hwc2_layer_t *, int32_t *>);
935 case HWC2::FunctionDescriptor::GetClientTargetSupport:
936 return ToHook<HWC2_PFN_GET_CLIENT_TARGET_SUPPORT>(
937 DisplayHook<decltype(&HwcDisplay::GetClientTargetSupport),
938 &HwcDisplay::GetClientTargetSupport, uint32_t, uint32_t,
940 case HWC2::FunctionDescriptor::GetColorModes:
941 return ToHook<HWC2_PFN_GET_COLOR_MODES>(
942 DisplayHook<decltype(&HwcDisplay::GetColorModes),
943 &HwcDisplay::GetColorModes, uint32_t *, int32_t *>);
944 case HWC2::FunctionDescriptor::GetDisplayAttribute:
945 return ToHook<HWC2_PFN_GET_DISPLAY_ATTRIBUTE>(DisplayHook<
946 decltype(&HwcDisplay::GetDisplayAttribute),
947 &HwcDisplay::GetDisplayAttribute, hwc2_config_t, int32_t, int32_t *>);
948 case HWC2::FunctionDescriptor::GetDisplayConfigs:
949 return ToHook<HWC2_PFN_GET_DISPLAY_CONFIGS>(DisplayHook<
950 decltype(&HwcDisplay::GetDisplayConfigs),
951 &HwcDisplay::GetDisplayConfigs, uint32_t *, hwc2_config_t *>);
952 case HWC2::FunctionDescriptor::GetDisplayName:
953 return ToHook<HWC2_PFN_GET_DISPLAY_NAME>(
954 DisplayHook<decltype(&HwcDisplay::GetDisplayName),
955 &HwcDisplay::GetDisplayName, uint32_t *, char *>);
956 case HWC2::FunctionDescriptor::GetDisplayRequests:
957 return ToHook<HWC2_PFN_GET_DISPLAY_REQUESTS>(
958 DisplayHook<decltype(&HwcDisplay::GetDisplayRequests),
959 &HwcDisplay::GetDisplayRequests, int32_t *, uint32_t *,
960 hwc2_layer_t *, int32_t *>);
961 case HWC2::FunctionDescriptor::GetDisplayType:
962 return ToHook<HWC2_PFN_GET_DISPLAY_TYPE>(
963 DisplayHook<decltype(&HwcDisplay::GetDisplayType),
964 &HwcDisplay::GetDisplayType, int32_t *>);
965 case HWC2::FunctionDescriptor::GetDozeSupport:
966 return ToHook<HWC2_PFN_GET_DOZE_SUPPORT>(
967 DisplayHook<decltype(&HwcDisplay::GetDozeSupport),
968 &HwcDisplay::GetDozeSupport, int32_t *>);
969 case HWC2::FunctionDescriptor::GetHdrCapabilities:
970 return ToHook<HWC2_PFN_GET_HDR_CAPABILITIES>(
971 DisplayHook<decltype(&HwcDisplay::GetHdrCapabilities),
972 &HwcDisplay::GetHdrCapabilities, uint32_t *, int32_t *,
973 float *, float *, float *>);
974 case HWC2::FunctionDescriptor::GetReleaseFences:
975 return ToHook<HWC2_PFN_GET_RELEASE_FENCES>(
976 DisplayHook<decltype(&HwcDisplay::GetReleaseFences),
977 &HwcDisplay::GetReleaseFences, uint32_t *, hwc2_layer_t *,
979 case HWC2::FunctionDescriptor::PresentDisplay:
980 return ToHook<HWC2_PFN_PRESENT_DISPLAY>(
981 DisplayHook<decltype(&HwcDisplay::PresentDisplay),
982 &HwcDisplay::PresentDisplay, int32_t *>);
983 case HWC2::FunctionDescriptor::SetActiveConfig:
984 return ToHook<HWC2_PFN_SET_ACTIVE_CONFIG>(
985 DisplayHook<decltype(&HwcDisplay::SetActiveConfig),
986 &HwcDisplay::SetActiveConfig, hwc2_config_t>);
987 case HWC2::FunctionDescriptor::SetClientTarget:
988 return ToHook<HWC2_PFN_SET_CLIENT_TARGET>(DisplayHook<
989 decltype(&HwcDisplay::SetClientTarget), &HwcDisplay::SetClientTarget,
990 buffer_handle_t, int32_t, int32_t, hwc_region_t>);
991 case HWC2::FunctionDescriptor::SetColorMode:
992 return ToHook<HWC2_PFN_SET_COLOR_MODE>(
993 DisplayHook<decltype(&HwcDisplay::SetColorMode),
994 &HwcDisplay::SetColorMode, int32_t>);
995 case HWC2::FunctionDescriptor::SetColorTransform:
996 return ToHook<HWC2_PFN_SET_COLOR_TRANSFORM>(
997 DisplayHook<decltype(&HwcDisplay::SetColorTransform),
998 &HwcDisplay::SetColorTransform, const float *, int32_t>);
999 case HWC2::FunctionDescriptor::SetOutputBuffer:
1000 return ToHook<HWC2_PFN_SET_OUTPUT_BUFFER>(
1001 DisplayHook<decltype(&HwcDisplay::SetOutputBuffer),
1002 &HwcDisplay::SetOutputBuffer, buffer_handle_t, int32_t>);
1003 case HWC2::FunctionDescriptor::SetPowerMode:
1004 return ToHook<HWC2_PFN_SET_POWER_MODE>(
1005 DisplayHook<decltype(&HwcDisplay::SetPowerMode),
1006 &HwcDisplay::SetPowerMode, int32_t>);
1007 case HWC2::FunctionDescriptor::SetVsyncEnabled:
1008 return ToHook<HWC2_PFN_SET_VSYNC_ENABLED>(
1009 DisplayHook<decltype(&HwcDisplay::SetVsyncEnabled),
1010 &HwcDisplay::SetVsyncEnabled, int32_t>);
1011 case HWC2::FunctionDescriptor::ValidateDisplay:
1012 return ToHook<HWC2_PFN_VALIDATE_DISPLAY>(
1013 DisplayHook<decltype(&HwcDisplay::ValidateDisplay),
1014 &HwcDisplay::ValidateDisplay, uint32_t *, uint32_t *>);
1017 case HWC2::FunctionDescriptor::SetCursorPosition:
1018 return ToHook<HWC2_PFN_SET_CURSOR_POSITION>(
1019 LayerHook<decltype(&Hwc2Layer::SetCursorPosition),
1020 &Hwc2Layer::SetCursorPosition, int32_t, int32_t>);
1021 case HWC2::FunctionDescriptor::SetLayerBlendMode:
1022 return ToHook<HWC2_PFN_SET_LAYER_BLEND_MODE>(
1023 LayerHook<decltype(&Hwc2Layer::SetLayerBlendMode),
1024 &Hwc2Layer::SetLayerBlendMode, int32_t>);
1025 case HWC2::FunctionDescriptor::SetLayerBuffer:
1026 return ToHook<HWC2_PFN_SET_LAYER_BUFFER>(
1027 LayerHook<decltype(&Hwc2Layer::SetLayerBuffer),
1028 &Hwc2Layer::SetLayerBuffer, buffer_handle_t, int32_t>);
1029 case HWC2::FunctionDescriptor::SetLayerColor:
1030 return ToHook<HWC2_PFN_SET_LAYER_COLOR>(
1031 LayerHook<decltype(&Hwc2Layer::SetLayerColor),
1032 &Hwc2Layer::SetLayerColor, hwc_color_t>);
1033 case HWC2::FunctionDescriptor::SetLayerCompositionType:
1034 return ToHook<HWC2_PFN_SET_LAYER_COMPOSITION_TYPE>(
1035 LayerHook<decltype(&Hwc2Layer::SetLayerCompositionType),
1036 &Hwc2Layer::SetLayerCompositionType, int32_t>);
1037 case HWC2::FunctionDescriptor::SetLayerDataspace:
1038 return ToHook<HWC2_PFN_SET_LAYER_DATASPACE>(
1039 LayerHook<decltype(&Hwc2Layer::SetLayerDataspace),
1040 &Hwc2Layer::SetLayerDataspace, int32_t>);
1041 case HWC2::FunctionDescriptor::SetLayerDisplayFrame:
1042 return ToHook<HWC2_PFN_SET_LAYER_DISPLAY_FRAME>(
1043 LayerHook<decltype(&Hwc2Layer::SetLayerDisplayFrame),
1044 &Hwc2Layer::SetLayerDisplayFrame, hwc_rect_t>);
1045 case HWC2::FunctionDescriptor::SetLayerPlaneAlpha:
1046 return ToHook<HWC2_PFN_SET_LAYER_PLANE_ALPHA>(
1047 LayerHook<decltype(&Hwc2Layer::SetLayerPlaneAlpha),
1048 &Hwc2Layer::SetLayerPlaneAlpha, float>);
1049 case HWC2::FunctionDescriptor::SetLayerSidebandStream:
1050 return ToHook<HWC2_PFN_SET_LAYER_SIDEBAND_STREAM>(LayerHook<
1051 decltype(&Hwc2Layer::SetLayerSidebandStream),
1052 &Hwc2Layer::SetLayerSidebandStream, const native_handle_t *>);
1053 case HWC2::FunctionDescriptor::SetLayerSourceCrop:
1054 return ToHook<HWC2_PFN_SET_LAYER_SOURCE_CROP>(
1055 LayerHook<decltype(&Hwc2Layer::SetLayerSourceCrop),
1056 &Hwc2Layer::SetLayerSourceCrop, hwc_frect_t>);
1057 case HWC2::FunctionDescriptor::SetLayerSurfaceDamage:
1058 return ToHook<HWC2_PFN_SET_LAYER_SURFACE_DAMAGE>(
1059 LayerHook<decltype(&Hwc2Layer::SetLayerSurfaceDamage),
1060 &Hwc2Layer::SetLayerSurfaceDamage, hwc_region_t>);
1061 case HWC2::FunctionDescriptor::SetLayerTransform:
1062 return ToHook<HWC2_PFN_SET_LAYER_TRANSFORM>(
1063 LayerHook<decltype(&Hwc2Layer::SetLayerTransform),
1064 &Hwc2Layer::SetLayerTransform, int32_t>);
1065 case HWC2::FunctionDescriptor::SetLayerVisibleRegion:
1066 return ToHook<HWC2_PFN_SET_LAYER_VISIBLE_REGION>(
1067 LayerHook<decltype(&Hwc2Layer::SetLayerVisibleRegion),
1068 &Hwc2Layer::SetLayerVisibleRegion, hwc_region_t>);
1069 case HWC2::FunctionDescriptor::SetLayerZOrder:
1070 return ToHook<HWC2_PFN_SET_LAYER_Z_ORDER>(
1071 LayerHook<decltype(&Hwc2Layer::SetLayerZOrder),
1072 &Hwc2Layer::SetLayerZOrder, uint32_t>);
1073 case HWC2::FunctionDescriptor::Invalid:
1080 int IAHWC2::HookDevOpen(const struct hw_module_t *module, const char *name,
1081 struct hw_device_t **dev) {
1082 supported(__func__);
1083 if (strcmp(name, HWC_HARDWARE_COMPOSER)) {
1084 ALOGE("Invalid module name- %s", name);
1088 std::unique_ptr<IAHWC2> ctx(new IAHWC2());
1090 ALOGE("Failed to allocate IAHWC2");
1094 HWC2::Error err = ctx->Init();
1095 if (err != HWC2::Error::None) {
1096 ALOGE("Failed to initialize IAHWC2 err=%d\n", err);
1100 ctx->common.module = const_cast<hw_module_t *>(module);
1101 *dev = &ctx->common;
1106 hwcomposer::NativeDisplay *IAHWC2::GetExtendedDisplay(uint32_t dispIndex) {
1107 return extended_displays_.at(dispIndex)->GetDisplay();
1110 hwcomposer::NativeDisplay *IAHWC2::GetPrimaryDisplay() {
1111 return primary_display_.GetDisplay();
1114 hwcomposer::NativeDisplay *IAHWC2::HwcDisplay::GetDisplay() {
1118 } // namespace android
1120 static struct hw_module_methods_t hwc2_module_methods = {
1121 .open = android::IAHWC2::HookDevOpen,
1124 hw_module_t HAL_MODULE_INFO_SYM = {
1125 .tag = HARDWARE_MODULE_TAG,
1126 .module_api_version = HARDWARE_MODULE_API_VERSION(2, 0),
1127 .id = HWC_HARDWARE_MODULE_ID,
1128 .name = "IA-Hardware-Composer",
1129 .author = "The Android Open Source Project",
1130 .methods = &hwc2_module_methods,