diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..ad71dd6c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +CMakeLists.txt +.idea/ +.editorconfig +.clang-complete diff --git a/Android.mk b/Android.mk index 58146e16..1c16b908 100644 --- a/Android.mk +++ b/Android.mk @@ -2,7 +2,7 @@ display-hals := libgralloc libcopybit liblight libmemtrack libqservice libqdutil display-hals += hdmi_cec sdm-libs := sdm/libs -display-hals += $(sdm-libs)/utils $(sdm-libs)/core $(sdm-libs)/hwc +display-hals += $(sdm-libs)/utils $(sdm-libs)/core $(sdm-libs)/hwc $(sdm-libs)/hwc2 ifeq ($(call is-vendor-board-platform,QCOM),true) include $(call all-named-subdir-makefiles,$(display-hals)) diff --git a/common.mk b/common.mk index 77f62b12..62bd1286 100644 --- a/common.mk +++ b/common.mk @@ -1,6 +1,8 @@ #Common headers display_top := $(call my-dir) +use_hwc2 := false + common_includes := $(display_top)/libqdutils common_includes += $(display_top)/libqservice common_includes += $(display_top)/libcopybit diff --git a/sdm/.clang-format b/sdm/.clang-format new file mode 100644 index 00000000..9082c400 --- /dev/null +++ b/sdm/.clang-format @@ -0,0 +1,13 @@ +--- +Language: Cpp +BasedOnStyle: Google +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AllowShortFunctionsOnASingleLine: Inline +AllowShortBlocksOnASingleLine: false +ColumnLimit: 100 +ConstructorInitializerAllOnOneLineOrOnePerLine: true +ConstructorInitializerIndentWidth: 4 +DerivePointerAlignment: false +PointerAlignment: Right +#ReflowComments: false diff --git a/sdm/include/utils/debug.h b/sdm/include/utils/debug.h index 5ac2319b..dfb10c7a 100644 --- a/sdm/include/utils/debug.h +++ b/sdm/include/utils/debug.h @@ -45,6 +45,7 @@ #define DLOGV_IF(tag, format, ...) DLOG(tag, Verbose, format, ##__VA_ARGS__) #define DLOGE(format, ...) DLOGE_IF(kTagNone, format, ##__VA_ARGS__) +#define DLOGD(format, ...) DLOGD_IF(kTagNone, format, ##__VA_ARGS__) #define DLOGW(format, ...) DLOGW_IF(kTagNone, format, ##__VA_ARGS__) #define DLOGI(format, ...) DLOGI_IF(kTagNone, format, ##__VA_ARGS__) #define DLOGV(format, ...) DLOGV_IF(kTagNone, format, ##__VA_ARGS__) diff --git a/sdm/libs/hwc/Android.mk b/sdm/libs/hwc/Android.mk index 795ba399..259a727c 100644 --- a/sdm/libs/hwc/Android.mk +++ b/sdm/libs/hwc/Android.mk @@ -1,6 +1,7 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) include $(LOCAL_PATH)/../../../common.mk +ifeq ($(use_hwc2),false) LOCAL_MODULE := hwcomposer.$(TARGET_BOARD_PLATFORM) LOCAL_MODULE_RELATIVE_PATH := hw @@ -30,3 +31,4 @@ LOCAL_SRC_FILES := hwc_session.cpp \ cpuhint.cpp include $(BUILD_SHARED_LIBRARY) +endif diff --git a/sdm/libs/hwc2/Android.mk b/sdm/libs/hwc2/Android.mk new file mode 100644 index 00000000..f633d7e6 --- /dev/null +++ b/sdm/libs/hwc2/Android.mk @@ -0,0 +1,37 @@ +LOCAL_PATH := $(call my-dir) +include $(CLEAR_VARS) +include $(LOCAL_PATH)/../../../common.mk + +ifeq ($(use_hwc2),true) + +LOCAL_MODULE := hwcomposer.$(TARGET_BOARD_PLATFORM) +LOCAL_MODULE_RELATIVE_PATH := hw +LOCAL_MODULE_TAGS := optional +LOCAL_C_INCLUDES := $(common_includes) + +LOCAL_CFLAGS := -Wno-missing-field-initializers -Wno-unused-parameter \ + -std=c++11 -fcolor-diagnostics\ + -DLOG_TAG=\"SDM\" $(common_flags) \ + -I $(display_top)/sdm/libs/hwc +LOCAL_CLANG := true + +LOCAL_SHARED_LIBRARIES := libsdmcore libqservice libbinder libhardware libhardware_legacy \ + libutils libcutils libsync libmemalloc libqdutils libdl \ + libpowermanager libsdmutils libc++ + +LOCAL_SRC_FILES := hwc_session.cpp \ + hwc_display.cpp \ + hwc_display_primary.cpp \ + hwc_display_external.cpp \ + hwc_display_virtual.cpp \ + ../hwc/hwc_debugger.cpp \ + ../hwc/hwc_buffer_allocator.cpp \ + ../hwc/hwc_buffer_sync_handler.cpp \ + hwc_color_manager.cpp \ + hwc_layers.cpp \ + hwc_callbacks.cpp \ + ../hwc/blit_engine_c2d.cpp \ + ../hwc/cpuhint.cpp + +include $(BUILD_SHARED_LIBRARY) +endif diff --git a/sdm/libs/hwc2/hwc_callbacks.cpp b/sdm/libs/hwc2/hwc_callbacks.cpp new file mode 100644 index 00000000..48ae3981 --- /dev/null +++ b/sdm/libs/hwc2/hwc_callbacks.cpp @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2016, The Linux Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * * Neither the name of The Linux Foundation. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#include +#include +#include "hwc_callbacks.h" + +#define __CLASS__ "HWCCallbacks" + +namespace sdm { + +void HWCCallbacks::Hotplug(hwc2_display_t display, HWC2::Connection state) { + if (hotplug_) { + hotplug_(hotplug_data_, display, INT32(state)); + } +} + +void HWCCallbacks::Refresh(hwc2_display_t display) { + if (refresh_) { + refresh_(refresh_data_, display); + } +} + +void HWCCallbacks::Vsync(hwc2_display_t display, int64_t timestamp) { + if (vsync_) { + vsync_(vsync_data_, display, timestamp); + } +} + +HWC2::Error HWCCallbacks::Register(HWC2::Callback descriptor, hwc2_callback_data_t callback_data, + hwc2_function_pointer_t pointer) { + switch (descriptor) { + case HWC2::Callback::Hotplug: + hotplug_data_ = callback_data; + hotplug_ = reinterpret_cast(pointer); + break; + case HWC2::Callback::Refresh: + refresh_data_ = callback_data; + refresh_ = reinterpret_cast(pointer); + break; + case HWC2::Callback::Vsync: + vsync_data_ = callback_data; + vsync_ = reinterpret_cast(pointer); + break; + default: + return HWC2::Error::BadParameter; + } + return HWC2::Error::None; +} + +} // namespace sdm diff --git a/sdm/libs/hwc2/hwc_callbacks.h b/sdm/libs/hwc2/hwc_callbacks.h new file mode 100644 index 00000000..835a06a7 --- /dev/null +++ b/sdm/libs/hwc2/hwc_callbacks.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2016, The Linux Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * * Neither the name of The Linux Foundation. nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __HWC_CALLBACKS_H__ +#define __HWC_CALLBACKS_H__ + +#define HWC2_INCLUDE_STRINGIFICATION +#define HWC2_USE_CPP11 +#include +#undef HWC2_INCLUDE_STRINGIFICATION +#undef HWC2_USE_CPP11 + +namespace sdm { + +class HWCCallbacks { + public: + void Hotplug(hwc2_display_t display, HWC2::Connection state); + void Refresh(hwc2_display_t display); + void Vsync(hwc2_display_t display, int64_t timestamp); + HWC2::Error Register(HWC2::Callback, hwc2_callback_data_t callback_data, + hwc2_function_pointer_t pointer); + + private: + hwc2_callback_data_t hotplug_data_ = nullptr; + hwc2_callback_data_t refresh_data_ = nullptr; + hwc2_callback_data_t vsync_data_ = nullptr; + + HWC2_PFN_HOTPLUG hotplug_ = nullptr; + HWC2_PFN_REFRESH refresh_ = nullptr; + HWC2_PFN_VSYNC vsync_ = nullptr; +}; + +} // namespace sdm + +#endif // __HWC_CALLBACKS_H__ diff --git a/sdm/libs/hwc2/hwc_color_manager.cpp b/sdm/libs/hwc2/hwc_color_manager.cpp new file mode 100644 index 00000000..0be9724a --- /dev/null +++ b/sdm/libs/hwc2/hwc_color_manager.cpp @@ -0,0 +1,564 @@ +/* +* Copyright (c) 2015 - 2016, The Linux Foundation. All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are +* met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above +* copyright notice, this list of conditions and the following +* disclaimer in the documentation and/or other materials provided +* with the distribution. +* * Neither the name of The Linux Foundation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include "hwc_buffer_allocator.h" +#include "hwc_buffer_sync_handler.h" +#include "hwc_session.h" +#include "hwc_debugger.h" + +#define __CLASS__ "HWCColorManager" + +namespace sdm { + +uint32_t HWCColorManager::Get8BitsARGBColorValue(const PPColorFillParams ¶ms) { + uint32_t argb_color = ((params.color.r << 16) & 0xff0000) | ((params.color.g) & 0xff) | + ((params.color.b << 8) & 0xff00); + return argb_color; +} + +int HWCColorManager::CreatePayloadFromParcel(const android::Parcel &in, uint32_t *disp_id, + PPDisplayAPIPayload *sink) { + int ret = 0; + uint32_t id(0); + uint32_t size(0); + + id = UINT32(in.readInt32()); + size = UINT32(in.readInt32()); + if (size > 0 && size == in.dataAvail()) { + const void *data = in.readInplace(size); + const uint8_t *temp = reinterpret_cast(data); + + sink->size = size; + sink->payload = const_cast(temp); + *disp_id = id; + } else { + DLOGW("Failing size checking, size = %d", size); + ret = -EINVAL; + } + + return ret; +} + +void HWCColorManager::MarshallStructIntoParcel(const PPDisplayAPIPayload &data, + android::Parcel *out_parcel) { + out_parcel->writeInt32(INT32(data.size)); + if (data.payload) + out_parcel->write(data.payload, data.size); +} + +HWCColorManager *HWCColorManager::CreateColorManager() { + HWCColorManager *color_mgr = new HWCColorManager(); + + if (color_mgr) { + void *&color_lib = color_mgr->color_apis_lib_; + // Load display API interface library. And retrieve color API function tables. + color_lib = ::dlopen(DISPLAY_API_INTERFACE_LIBRARY_NAME, RTLD_NOW); + if (color_lib) { + color_mgr->color_apis_ = ::dlsym(color_lib, DISPLAY_API_FUNC_TABLES); + if (!color_mgr->color_apis_) { + DLOGE("Fail to retrieve = %s from %s", DISPLAY_API_FUNC_TABLES, + DISPLAY_API_INTERFACE_LIBRARY_NAME); + ::dlclose(color_lib); + delete color_mgr; + return NULL; + } + } else { + DLOGW("Unable to load = %s", DISPLAY_API_INTERFACE_LIBRARY_NAME); + delete color_mgr; + return NULL; + } + DLOGI("Successfully loaded %s", DISPLAY_API_INTERFACE_LIBRARY_NAME); + + // Load diagclient library and invokes its entry point to pass in display APIs. + void *&diag_lib = color_mgr->diag_client_lib_; + diag_lib = ::dlopen(QDCM_DIAG_CLIENT_LIBRARY_NAME, RTLD_NOW); + if (diag_lib) { + *(reinterpret_cast(&color_mgr->qdcm_diag_init_)) = + ::dlsym(diag_lib, INIT_QDCM_DIAG_CLIENT_NAME); + *(reinterpret_cast(&color_mgr->qdcm_diag_deinit_)) = + ::dlsym(diag_lib, DEINIT_QDCM_DIAG_CLIENT_NAME); + + if (!color_mgr->qdcm_diag_init_ || !color_mgr->qdcm_diag_deinit_) { + DLOGE("Fail to retrieve = %s from %s", INIT_QDCM_DIAG_CLIENT_NAME, + QDCM_DIAG_CLIENT_LIBRARY_NAME); + ::dlclose(diag_lib); + } else { + // invoke Diag Client entry point to initialize. + color_mgr->qdcm_diag_init_(color_mgr->color_apis_); + DLOGI("Successfully loaded %s and %s and diag_init'ed", DISPLAY_API_INTERFACE_LIBRARY_NAME, + QDCM_DIAG_CLIENT_LIBRARY_NAME); + } + } else { + DLOGW("Unable to load = %s", QDCM_DIAG_CLIENT_LIBRARY_NAME); + // only QDCM Diag client failed to be loaded and system still should function. + } + } else { + DLOGE("Unable to create HWCColorManager"); + return NULL; + } + + return color_mgr; +} + +HWCColorManager::~HWCColorManager() { +} + +void HWCColorManager::DestroyColorManager() { + if (qdcm_mode_mgr_) { + delete qdcm_mode_mgr_; + } + if (qdcm_diag_deinit_) { + qdcm_diag_deinit_(); + } + if (diag_client_lib_) { + ::dlclose(diag_client_lib_); + } + if (color_apis_lib_) { + ::dlclose(color_apis_lib_); + } + delete this; +} + +int HWCColorManager::EnableQDCMMode(bool enable, HWCDisplay *hwc_display) { + int ret = 0; + + if (!qdcm_mode_mgr_) { + qdcm_mode_mgr_ = HWCQDCMModeManager::CreateQDCMModeMgr(); + if (!qdcm_mode_mgr_) { + DLOGE("Unable to create QDCM operating mode manager."); + ret = -EFAULT; + } + } + + if (qdcm_mode_mgr_) { + ret = qdcm_mode_mgr_->EnableQDCMMode(enable, hwc_display); + } + + return ret; +} + +bool HWCColorManager::SolidFillLayersPrepare(hwc_display_contents_1_t **displays, + HWCDisplay *hwc_display) { + SCOPE_LOCK(locker_); + + // Query HWCColorManager if QDCM tool requesting SOLID_FILL mode. + uint32_t solid_fill_color = Get8BitsARGBColorValue(solid_fill_params_); + hwc_display_contents_1_t *layer_list = displays[HWC_DISPLAY_PRIMARY]; + + if (solid_fill_enable_ && solid_fill_layers_ && layer_list) { + // 1. shallow copy HWC_FRAMEBUFFER_TARGET layer info solid fill layer list. + solid_fill_layers_->hwLayers[1] = layer_list->hwLayers[layer_list->numHwLayers - 1]; + + // 2. continue the prepare<> on solid_fill_layers. + hwc_display->Perform(HWCDisplayPrimary::SET_QDCM_SOLID_FILL_INFO, solid_fill_color); + // TODO(user): Remove the display_contents generated here and + // use the solid fill layer support in HWC2 to set this up + // hwc_display->Prepare(solid_fill_layers_); // RECT info included. + + // 3. Set HWC_OVERLAY to all SF layers before returning to framework. + for (size_t i = 0; i < (layer_list->numHwLayers - 1); i++) { + hwc_layer_1_t *layer = &layer_list->hwLayers[i]; + layer->compositionType = HWC_OVERLAY; + } + + return true; + } else if (!solid_fill_enable_) { + hwc_display->Perform(HWCDisplayPrimary::UNSET_QDCM_SOLID_FILL_INFO, 0); + } + + return false; +} + +bool HWCColorManager::SolidFillLayersSet(hwc_display_contents_1_t **displays, + HWCDisplay *hwc_display) { + // Query HWCColorManager if QDCM tool requesting SOLID_FILL mode. + SCOPE_LOCK(locker_); + hwc_display_contents_1_t *layer_list = displays[HWC_DISPLAY_PRIMARY]; + if (solid_fill_enable_ && solid_fill_layers_ && layer_list) { + // TODO(user): Present solid fill + // hwc_display->Commit(solid_fill_layers_); + + // SurfaceFlinger layer stack is dropped in solid fill case and replaced with local layer stack + // Close acquire fence fds associated with SF layer stack + // Close release/retire fence fds returned along with local layer stack + for (size_t i = 0; i < (layer_list->numHwLayers - 1); i++) { + int &fence_fd = layer_list->hwLayers[i].acquireFenceFd; + if (fence_fd >= 0) { + close(fence_fd); + fence_fd = -1; + } + } + + for (size_t i = 0; i < (solid_fill_layers_->numHwLayers - 1); i++) { + int &fence_fd = solid_fill_layers_->hwLayers[i].releaseFenceFd; + if (fence_fd >= 0) { + close(fence_fd); + fence_fd = -1; + } + } + if (solid_fill_layers_->retireFenceFd >= 0) { + close(solid_fill_layers_->retireFenceFd); + solid_fill_layers_->retireFenceFd = -1; + } + + return true; + } + + return false; +} + +int HWCColorManager::CreateSolidFillLayers(HWCDisplay *hwc_display) { + int ret = 0; + + if (!solid_fill_layers_) { + uint32_t size = sizeof(hwc_display_contents_1) + kNumSolidFillLayers * sizeof(hwc_layer_1_t); + uint32_t primary_width = 0; + uint32_t primary_height = 0; + + hwc_display->GetPanelResolution(&primary_width, &primary_height); + uint8_t *buf = new uint8_t[size](); + // handle for solid fill layer with fd = -1. + private_handle_t *handle = new private_handle_t(-1, 0, private_handle_t::PRIV_FLAGS_FRAMEBUFFER, + BUFFER_TYPE_UI, HAL_PIXEL_FORMAT_RGBA_8888, + INT32(primary_width), INT32(primary_height)); + + if (!buf || !handle) { + DLOGE("Failed to allocate memory."); + if (buf) + delete[] buf; + if (handle) + delete handle; + + return -ENOMEM; + } + + solid_fill_layers_ = reinterpret_cast(buf); + hwc_layer_1_t &layer = solid_fill_layers_->hwLayers[0]; + layer.handle = handle; + } + + solid_fill_layers_->flags = HWC_GEOMETRY_CHANGED; + solid_fill_layers_->numHwLayers = kNumSolidFillLayers; + solid_fill_layers_->retireFenceFd = -1; + solid_fill_layers_->outbuf = NULL; + solid_fill_layers_->outbufAcquireFenceFd = -1; + + hwc_layer_1_t &layer = solid_fill_layers_->hwLayers[0]; + hwc_rect_t solid_fill_rect = { + INT(solid_fill_params_.rect.x), INT(solid_fill_params_.rect.y), + solid_fill_params_.rect.x + INT(solid_fill_params_.rect.width), + solid_fill_params_.rect.y + INT(solid_fill_params_.rect.height), + }; + + layer.compositionType = HWC_FRAMEBUFFER; + layer.blending = HWC_BLENDING_PREMULT; + layer.sourceCropf.left = solid_fill_params_.rect.x; + layer.sourceCropf.top = solid_fill_params_.rect.y; + layer.sourceCropf.right = UINT32(solid_fill_params_.rect.x) + solid_fill_params_.rect.width; + layer.sourceCropf.bottom = UINT32(solid_fill_params_.rect.y) + solid_fill_params_.rect.height; + layer.acquireFenceFd = -1; + layer.releaseFenceFd = -1; + layer.flags = 0; + layer.transform = 0; + layer.hints = 0; + layer.planeAlpha = 0xff; + layer.displayFrame = solid_fill_rect; + layer.visibleRegionScreen.numRects = 1; + layer.visibleRegionScreen.rects = &layer.displayFrame; + layer.surfaceDamage.numRects = 0; + + return ret; +} + +void HWCColorManager::DestroySolidFillLayers() { + if (solid_fill_layers_) { + hwc_layer_1_t &layer = solid_fill_layers_->hwLayers[0]; + uint8_t *buf = reinterpret_cast(solid_fill_layers_); + private_handle_t const *hnd = reinterpret_cast(layer.handle); + + if (hnd) + delete hnd; + + if (buf) + delete[] buf; + + solid_fill_layers_ = NULL; + } +} + +int HWCColorManager::SetSolidFill(const void *params, bool enable, HWCDisplay *hwc_display) { + SCOPE_LOCK(locker_); + int ret = 0; + + if (params) { + solid_fill_params_ = *reinterpret_cast(params); + } else { + solid_fill_params_ = PPColorFillParams(); + } + + if (enable) { + // will create solid fill layers for rendering if not present. + ret = CreateSolidFillLayers(hwc_display); + } else { + DestroySolidFillLayers(); + } + solid_fill_enable_ = enable; + + return ret; +} + +int HWCColorManager::SetFrameCapture(void *params, bool enable, HWCDisplay *hwc_display) { + SCOPE_LOCK(locker_); + int ret = 0; + + PPFrameCaptureData *frame_capture_data = reinterpret_cast(params); + + if (enable) { + std::memset(&buffer_info, 0x00, sizeof(buffer_info)); + hwc_display->GetFrameBufferResolution(&buffer_info.buffer_config.width, + &buffer_info.buffer_config.height); + if (frame_capture_data->input_params.out_pix_format == PP_PIXEL_FORMAT_RGB_888) { + buffer_info.buffer_config.format = kFormatRGB888; + } else if (frame_capture_data->input_params.out_pix_format == PP_PIXEL_FORMAT_RGB_2101010) { + // TODO(user): Complete the implementation + DLOGE("RGB 10-bit format NOT supported"); + return -EFAULT; + } else { + DLOGE("Pixel-format: %d NOT support.", frame_capture_data->input_params.out_pix_format); + return -EFAULT; + } + + buffer_info.buffer_config.buffer_count = 1; + buffer_info.alloc_buffer_info.fd = -1; + buffer_info.alloc_buffer_info.stride = 0; + buffer_info.alloc_buffer_info.size = 0; + + buffer_allocator_ = new HWCBufferAllocator(); + if (buffer_allocator_ == NULL) { + DLOGE("Memory allocation for buffer_allocator_ FAILED"); + return -ENOMEM; + } + + ret = buffer_allocator_->AllocateBuffer(&buffer_info); + if (ret != 0) { + DLOGE("Buffer allocation failed. ret: %d", ret); + delete[] buffer_allocator_; + buffer_allocator_ = NULL; + return -ENOMEM; + } else { + void *buffer = mmap(NULL, buffer_info.alloc_buffer_info.size, PROT_READ | PROT_WRITE, + MAP_SHARED, buffer_info.alloc_buffer_info.fd, 0); + + if (buffer == MAP_FAILED) { + DLOGE("mmap failed. err = %d", errno); + frame_capture_data->buffer = NULL; + ret = buffer_allocator_->FreeBuffer(&buffer_info); + delete[] buffer_allocator_; + buffer_allocator_ = NULL; + return -EFAULT; + } else { + frame_capture_data->buffer = reinterpret_cast(buffer); + frame_capture_data->buffer_stride = buffer_info.alloc_buffer_info.stride; + frame_capture_data->buffer_size = buffer_info.alloc_buffer_info.size; + } + // TODO(user): Call HWC interface to provide the buffer and rectangle information + } + } else { + if (frame_capture_data->buffer != NULL) { + if (munmap(frame_capture_data->buffer, buffer_info.alloc_buffer_info.size) != 0) { + DLOGE("munmap failed. err = %d", errno); + } + } + if (buffer_allocator_ != NULL) { + std::memset(frame_capture_data, 0x00, sizeof(PPFrameCaptureData)); + ret = buffer_allocator_->FreeBuffer(&buffer_info); + if (ret != 0) { + DLOGE("FreeBuffer failed. ret = %d", ret); + } + delete[] buffer_allocator_; + buffer_allocator_ = NULL; + } + } + return ret; +} + +const HWCQDCMModeManager::ActiveFeatureCMD HWCQDCMModeManager::kActiveFeatureCMD[] = { + HWCQDCMModeManager::ActiveFeatureCMD("cabl:on", "cabl:off", "cabl:status", "running"), + HWCQDCMModeManager::ActiveFeatureCMD("ad:on", "ad:off", "ad:query:status", "running"), + HWCQDCMModeManager::ActiveFeatureCMD("svi:on", "svi:off", "svi:status", "running"), +}; + +const char *const HWCQDCMModeManager::kSocketName = "pps"; +const char *const HWCQDCMModeManager::kTagName = "surfaceflinger"; +const char *const HWCQDCMModeManager::kPackageName = "colormanager"; + +HWCQDCMModeManager *HWCQDCMModeManager::CreateQDCMModeMgr() { + HWCQDCMModeManager *mode_mgr = new HWCQDCMModeManager(); + + if (!mode_mgr) { + DLOGW("No memory to create HWCQDCMModeManager."); + return NULL; + } else { + mode_mgr->socket_fd_ = + ::socket_local_client(kSocketName, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM); + if (mode_mgr->socket_fd_ < 0) { + // it should not be disastrous and we still can grab wakelock in QDCM mode. + DLOGW("Unable to connect to dpps socket!"); + } + + // retrieve system GPU idle timeout value for later to recover. + mode_mgr->entry_timeout_ = UINT32(HWCDebugHandler::GetIdleTimeoutMs()); + + // acquire the binder handle to Android system PowerManager for later use. + android::sp binder = + android::defaultServiceManager()->checkService(android::String16("power")); + if (binder == NULL) { + DLOGW("Application can't connect to power manager service"); + delete mode_mgr; + mode_mgr = NULL; + } else { + mode_mgr->power_mgr_ = android::interface_cast(binder); + } + } + + return mode_mgr; +} + +HWCQDCMModeManager::~HWCQDCMModeManager() { + if (socket_fd_ >= 0) + ::close(socket_fd_); +} + +int HWCQDCMModeManager::AcquireAndroidWakeLock(bool enable) { + int ret = 0; + + if (enable) { + if (wakelock_token_ == NULL) { + android::sp binder = new android::BBinder(); + android::status_t status = power_mgr_->acquireWakeLock( + (kFullWakeLock | kAcquireCauseWakeup | kONAfterRelease), binder, + android::String16(kTagName), android::String16(kPackageName)); + if (status == android::NO_ERROR) { + wakelock_token_ = binder; + } + } + } else { + if (wakelock_token_ != NULL && power_mgr_ != NULL) { + power_mgr_->releaseWakeLock(wakelock_token_, 0); + wakelock_token_.clear(); + wakelock_token_ = NULL; + } + } + + return ret; +} + +int HWCQDCMModeManager::EnableActiveFeatures(bool enable, + const HWCQDCMModeManager::ActiveFeatureCMD &cmds, + bool *was_running) { + int ret = 0; + ssize_t size = 0; + char response[kSocketCMDMaxLength] = { + 0, + }; + + if (socket_fd_ < 0) { + DLOGW("No socket connection available!"); + return -EFAULT; + } + + if (!enable) { // if client requesting to disable it. + // query CABL status, if off, no action. keep the status. + size = ::write(socket_fd_, cmds.cmd_query_status, strlen(cmds.cmd_query_status)); + if (size < 0) { + DLOGW("Unable to send data over socket %s", ::strerror(errno)); + ret = -EFAULT; + } else { + size = ::read(socket_fd_, response, kSocketCMDMaxLength); + if (size < 0) { + DLOGW("Unable to read data over socket %s", ::strerror(errno)); + ret = -EFAULT; + } else if (!strncmp(response, cmds.running, strlen(cmds.running))) { + *was_running = true; + } + } + + if (*was_running) { // if was running, it's requested to disable it. + size = ::write(socket_fd_, cmds.cmd_off, strlen(cmds.cmd_off)); + if (size < 0) { + DLOGW("Unable to send data over socket %s", ::strerror(errno)); + ret = -EFAULT; + } + } + } else { // if was running, need enable it back. + if (*was_running) { + size = ::write(socket_fd_, cmds.cmd_on, strlen(cmds.cmd_on)); + if (size < 0) { + DLOGW("Unable to send data over socket %s", ::strerror(errno)); + ret = -EFAULT; + } + } + } + + return ret; +} + +int HWCQDCMModeManager::EnableQDCMMode(bool enable, HWCDisplay *hwc_display) { + int ret = 0; + + ret = EnableActiveFeatures((enable ? false : true), kActiveFeatureCMD[kCABLFeature], + &cabl_was_running_); + ret = AcquireAndroidWakeLock(enable); + + // if enter QDCM mode, disable GPU fallback idle timeout. + if (hwc_display) { + uint32_t timeout = enable ? 0 : entry_timeout_; + hwc_display->SetIdleTimeoutMs(timeout); + } + + return ret; +} + +} // namespace sdm diff --git a/sdm/libs/hwc2/hwc_display.cpp b/sdm/libs/hwc2/hwc_display.cpp new file mode 100644 index 00000000..d9b3c733 --- /dev/null +++ b/sdm/libs/hwc2/hwc_display.cpp @@ -0,0 +1,1370 @@ +/* + * Copyright (c) 2014-2016, The Linux Foundation. All rights reserved. + * Not a Contribution. + * + * Copyright 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "hwc_display.h" +#include "hwc_debugger.h" +#include "blit_engine_c2d.h" + +#ifdef QTI_BSP +#include +#endif + +#define __CLASS__ "HWCDisplay" + +namespace sdm { + +static void ApplyDeInterlaceAdjustment(Layer *layer) { + // De-interlacing adjustment + if (layer->input_buffer->flags.interlace) { + float height = (layer->src_rect.bottom - layer->src_rect.top) / 2.0f; + layer->src_rect.top = ROUND_UP_ALIGN_DOWN(layer->src_rect.top / 2.0f, 2); + layer->src_rect.bottom = layer->src_rect.top + floorf(height); + } +} + +HWCDisplay::HWCDisplay(CoreInterface *core_intf, HWCCallbacks *callbacks, DisplayType type, + hwc2_display_t id, bool needs_blit, qService::QService *qservice, + DisplayClass display_class) + : core_intf_(core_intf), + callbacks_(callbacks), + type_(type), + id_(id), + needs_blit_(needs_blit), + qservice_(qservice), + display_class_(display_class) { +} + +int HWCDisplay::Init() { + DisplayError error = core_intf_->CreateDisplay(type_, this, &display_intf_); + if (error != kErrorNone) { + DLOGE("Display create failed. Error = %d display_type %d event_handler %p disp_intf %p", error, + type_, this, &display_intf_); + return -EINVAL; + } + + int property_swap_interval = 1; + HWCDebugHandler::Get()->GetProperty("debug.egl.swapinterval", &property_swap_interval); + if (property_swap_interval == 0) { + swap_interval_zero_ = true; + } + + framebuffer_config_ = new DisplayConfigVariableInfo(); + if (!framebuffer_config_) { + DLOGV("Failed to allocate memory for custom framebuffer config."); + core_intf_->DestroyDisplay(display_intf_); + return -EINVAL; + } + + client_target_ = new HWCLayer(id_); + int blit_enabled = 0; + HWCDebugHandler::Get()->GetProperty("persist.hwc.blit.comp", &blit_enabled); + if (needs_blit_ && blit_enabled) { + blit_engine_ = new BlitEngineC2d(); + if (!blit_engine_) { + DLOGI("Create Blit Engine C2D failed"); + } else { + if (blit_engine_->Init() < 0) { + DLOGI("Blit Engine Init failed, Blit Composition will not be used!!"); + delete blit_engine_; + blit_engine_ = NULL; + } + } + } + + display_intf_->GetRefreshRateRange(&min_refresh_rate_, &max_refresh_rate_); + current_refresh_rate_ = max_refresh_rate_; + DLOGI("Display created with id: %d", id_); + return 0; +} + +int HWCDisplay::Deinit() { + DisplayError error = core_intf_->DestroyDisplay(display_intf_); + if (error != kErrorNone) { + DLOGE("Display destroy failed. Error = %d", error); + return -EINVAL; + } + + delete framebuffer_config_; + delete client_target_; + + if (blit_engine_) { + blit_engine_->DeInit(); + delete blit_engine_; + blit_engine_ = NULL; + } + + return 0; +} + +// LayerStack operations +HWC2::Error HWCDisplay::CreateLayer(hwc2_layer_t *out_layer_id) { + auto layer = *layer_set_.emplace(new HWCLayer(id_)); + layer_map_.emplace(std::make_pair(layer->GetId(), layer)); + *out_layer_id = layer->GetId(); + geometry_changes_ = GeometryChanges::kAdded; + return HWC2::Error::None; +} + +HWCLayer *HWCDisplay::GetHWCLayer(hwc2_layer_t layer_id) { + const auto map_layer = layer_map_.find(layer_id); + if (map_layer == layer_map_.end()) { + DLOGE("[%" PRIu64 "] GetLayer(%" PRIu64 ") failed: no such layer", id_, layer_id); + return nullptr; + } else { + return map_layer->second; + } +} + +HWC2::Error HWCDisplay::DestroyLayer(hwc2_layer_t layer_id) { + const auto map_layer = layer_map_.find(layer_id); + if (map_layer == layer_map_.end()) { + DLOGE("[%" PRIu64 "] destroyLayer(%" PRIu64 ") failed: no such layer", id_, layer_id); + return HWC2::Error::BadLayer; + } + const auto layer = map_layer->second; + layer_map_.erase(map_layer); + const auto z_range = layer_set_.equal_range(layer); + for (auto current = z_range.first; current != z_range.second; ++current) { + if (*current == layer) { + current = layer_set_.erase(current); + break; + } + } + + geometry_changes_ = GeometryChanges::kRemoved; + return HWC2::Error::None; +} + +void HWCDisplay::BuildLayerStack() { + // TODO(user): Validate + validated_ = true; + layer_stack_ = LayerStack(); + display_rect_ = LayerRect(); + metadata_refresh_rate_ = 0; + + // Add one layer for fb target + // TODO(user): Add blit target layers + for (auto hwc_layer : layer_set_) { + Layer *layer = hwc_layer->GetSDMLayer(); + + if (swap_interval_zero_) { + if (layer->input_buffer->acquire_fence_fd >= 0) { + close(layer->input_buffer->acquire_fence_fd); + layer->input_buffer->acquire_fence_fd = -1; + } + } + + const private_handle_t *handle = + reinterpret_cast(layer->input_buffer->buffer_id); + if (handle) { + if (handle->bufferType == BUFFER_TYPE_VIDEO) { + layer_stack_.flags.video_present = true; + } + // TZ Protected Buffer - L1 + if (handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) { + layer_stack_.flags.secure_present = true; + } + // Gralloc Usage Protected Buffer - L3 - which needs to be treated as Secure & avoid fallback + if (handle->flags & private_handle_t::PRIV_FLAGS_PROTECTED_BUFFER) { + layer_stack_.flags.secure_present = true; + } + } + + if (layer->flags.skip) { + layer_stack_.flags.skip_present = true; + } + + if (layer->flags.cursor) { + layer_stack_.flags.cursor_present = true; + } + // TODO(user): Move to a getter if this is needed at other places + hwc_rect_t scaled_display_frame = {INT(layer->dst_rect.left), INT(layer->dst_rect.top), + INT(layer->dst_rect.right), INT(layer->dst_rect.bottom)}; + ScaleDisplayFrame(&scaled_display_frame); + ApplyScanAdjustment(&scaled_display_frame); + hwc_layer->SetLayerDisplayFrame(scaled_display_frame); + ApplyDeInterlaceAdjustment(layer); + // TODO(user): Verify if we still need to configure the solid fill layerbuffer, + // it should already have a valid dst_rect by this point + + if (layer->frame_rate > metadata_refresh_rate_) { + metadata_refresh_rate_ = SanitizeRefreshRate(layer->frame_rate); + } else { + layer->frame_rate = current_refresh_rate_; + } + display_rect_ = Union(display_rect_, layer->dst_rect); + // TODO(user): Set correctly when implementing caching + layer->flags.updating = true; + geometry_changes_ |= hwc_layer->GetGeometryChanges(); + + layer_stack_.layers.push_back(layer); + } + layer_stack_.flags.geometry_changed = UINT32(geometry_changes_ > 0); + // Append client target to the layer stack + layer_stack_.layers.push_back(client_target_->GetSDMLayer()); +} + +HWC2::Error HWCDisplay::SetLayerZOrder(hwc2_layer_t layer_id, uint32_t z) { + const auto map_layer = layer_map_.find(layer_id); + if (map_layer == layer_map_.end()) { + DLOGE("[%" PRIu64 "] updateLayerZ failed to find layer", id_); + return HWC2::Error::BadLayer; + } + + const auto layer = map_layer->second; + const auto z_range = layer_set_.equal_range(layer); + bool layer_on_display = false; + for (auto current = z_range.first; current != z_range.second; ++current) { + if (*current == layer) { + if ((*current)->GetZ() == z) { + // Don't change anything if the Z hasn't changed + return HWC2::Error::None; + } + current = layer_set_.erase(current); + layer_on_display = true; + break; + } + } + + if (!layer_on_display) { + DLOGE("[%" PRIu64 "] updateLayerZ failed to find layer on display", id_); + return HWC2::Error::BadLayer; + } + + layer->SetLayerZOrder(z); + layer_set_.emplace(layer); + return HWC2::Error::None; +} + +HWC2::Error HWCDisplay::SetVsyncEnabled(HWC2::Vsync enabled) { + DLOGV("Display ID: %d enabled: %s", id_, to_string(enabled).c_str()); + DisplayError error = kErrorNone; + + if (shutdown_pending_) { + return HWC2::Error::None; + } + + bool state; + if (enabled == HWC2::Vsync::Enable) + state = true; + else if (enabled == HWC2::Vsync::Disable) + state = false; + else + return HWC2::Error::BadParameter; + + error = display_intf_->SetVSyncState(state); + + if (error != kErrorNone) { + if (error == kErrorShutDown) { + shutdown_pending_ = true; + return HWC2::Error::None; + } + DLOGE("Failed. enabled = %s, error = %d", to_string(enabled).c_str(), error); + return HWC2::Error::BadDisplay; + } + + return HWC2::Error::None; +} + +HWC2::Error HWCDisplay::SetPowerMode(HWC2::PowerMode mode) { + DLOGV("display = %d, mode = %s", id_, to_string(mode).c_str()); + DisplayState state = kStateOff; + bool flush_on_error = flush_on_error_; + + if (shutdown_pending_) { + return HWC2::Error::None; + } + + switch (mode) { + case HWC2::PowerMode::Off: + // During power off, all of the buffers are released. + // Do not flush until a buffer is successfully submitted again. + flush_on_error = false; + state = kStateOff; + break; + case HWC2::PowerMode::On: + state = kStateOn; + last_power_mode_ = HWC2::PowerMode::On; + break; + case HWC2::PowerMode::Doze: + state = kStateDoze; + last_power_mode_ = HWC2::PowerMode::Doze; + break; + case HWC2::PowerMode::DozeSuspend: + state = kStateDozeSuspend; + last_power_mode_ = HWC2::PowerMode::DozeSuspend; + break; + default: + return HWC2::Error::BadParameter; + } + + DisplayError error = display_intf_->SetDisplayState(state); + if (error == kErrorNone) { + flush_on_error_ = flush_on_error; + } else { + if (error == kErrorShutDown) { + shutdown_pending_ = true; + return HWC2::Error::None; + } + DLOGE("Set state failed. Error = %d", error); + return HWC2::Error::BadParameter; + } + + return HWC2::Error::None; +} + +HWC2::Error HWCDisplay::GetClientTargetSupport(uint32_t width, uint32_t height, int32_t format, + int32_t dataspace) { + // TODO(user): Support scaled configurations, other formats and other dataspaces + if (format != HAL_PIXEL_FORMAT_RGBA_8888 || dataspace != HAL_DATASPACE_UNKNOWN || + width != framebuffer_config_->x_pixels || height != framebuffer_config_->y_pixels) { + return HWC2::Error::Unsupported; + } else { + return HWC2::Error::None; + } +} + +HWC2::Error HWCDisplay::GetDisplayConfigs(uint32_t *out_num_configs, hwc2_config_t *out_configs) { + // TODO(user): Actually handle multiple configs + if (out_configs == nullptr) { + *out_num_configs = 1; + } else { + *out_num_configs = 1; + out_configs[0] = 0; + } + return HWC2::Error::None; +} + +HWC2::Error HWCDisplay::GetDisplayAttribute(hwc2_config_t config, HWC2::Attribute attribute, + int32_t *out_value) { + DisplayConfigVariableInfo variable_config = *framebuffer_config_; + + switch (attribute) { + case HWC2::Attribute::VsyncPeriod: + *out_value = INT32(variable_config.vsync_period_ns); + break; + case HWC2::Attribute::Width: + *out_value = INT32(variable_config.x_pixels); + break; + case HWC2::Attribute::Height: + *out_value = INT32(variable_config.y_pixels); + break; + case HWC2::Attribute::DpiX: + *out_value = INT32(variable_config.x_dpi * 1000.0f); + break; + case HWC2::Attribute::DpiY: + *out_value = INT32(variable_config.y_dpi * 1000.0f); + break; + default: + DLOGW("Spurious attribute type = %s", to_string(attribute).c_str()); + return HWC2::Error::BadConfig; + } + + return HWC2::Error::None; +} + +HWC2::Error HWCDisplay::GetDisplayName(uint32_t *out_size, char *out_name) { + // TODO(user): Get panel name and EDID name and populate it here + if (out_name == nullptr) { + *out_size = 32; + } else { + std::string name; + switch (id_) { + case HWC_DISPLAY_PRIMARY: + name = "Primary Display"; + break; + case HWC_DISPLAY_EXTERNAL: + name = "External Display"; + break; + case HWC_DISPLAY_VIRTUAL: + name = "Virtual Display"; + break; + default: + name = "Unknown"; + break; + } + std::strncpy(out_name, name.c_str(), name.size()); + *out_size = UINT32(name.size()); + } + return HWC2::Error::None; +} + +HWC2::Error HWCDisplay::GetDisplayType(int32_t *out_type) { + if (out_type != nullptr) { + if (id_ == HWC_DISPLAY_VIRTUAL) { + *out_type = HWC2_DISPLAY_TYPE_VIRTUAL; + } else { + *out_type = HWC2_DISPLAY_TYPE_PHYSICAL; + } + return HWC2::Error::None; + } else { + return HWC2::Error::BadParameter; + } +} + +// TODO(user): Store configurations and hook them up here +HWC2::Error HWCDisplay::GetActiveConfig(hwc2_config_t *out_config) { + if (out_config != nullptr) { + *out_config = 0; + return HWC2::Error::None; + } else { + return HWC2::Error::BadParameter; + } +} + +HWC2::Error HWCDisplay::SetClientTarget(buffer_handle_t target, int32_t acquire_fence, + int32_t dataspace) { + client_target_->SetLayerBuffer(target, acquire_fence); + // Ignoring dataspace for now + return HWC2::Error::None; +} + +HWC2::Error HWCDisplay::SetActiveConfig(hwc2_config_t config) { + // We have only one config right now - do nothing + return HWC2::Error::None; +} + +void HWCDisplay::SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type) { + dump_frame_count_ = count; + dump_frame_index_ = 0; + dump_input_layers_ = ((bit_mask_layer_type & (1 << INPUT_LAYER_DUMP)) != 0); + + if (blit_engine_) { + blit_engine_->SetFrameDumpConfig(count); + } + + DLOGI("num_frame_dump %d, input_layer_dump_enable %d", dump_frame_count_, dump_input_layers_); +} + +HWC2::PowerMode HWCDisplay::GetLastPowerMode() { + return last_power_mode_; +} + +DisplayError HWCDisplay::VSync(const DisplayEventVSync &vsync) { + callbacks_->Vsync(id_, vsync.timestamp); + return kErrorNone; +} + +DisplayError HWCDisplay::Refresh() { + return kErrorNotSupported; +} + +DisplayError HWCDisplay::CECMessage(char *message) { + if (qservice_) { + qservice_->onCECMessageReceived(message, 0); + } else { + DLOGW("Qservice instance not available."); + } + + return kErrorNone; +} + +HWC2::Error HWCDisplay::PrepareLayerStack(uint32_t *out_num_types, uint32_t *out_num_requests) { + layer_changes_.clear(); + layer_requests_.clear(); + if (shutdown_pending_) { + return HWC2::Error::BadDisplay; + } + + if (!skip_prepare_) { + DisplayError error = display_intf_->Prepare(&layer_stack_); + if (error != kErrorNone) { + if (error == kErrorShutDown) { + shutdown_pending_ = true; + } else if (error != kErrorPermission) { + DLOGE("Prepare failed. Error = %d", error); + // To prevent surfaceflinger infinite wait, flush the previous frame during Commit() + // so that previous buffer and fences are released, and override the error. + flush_ = true; + } + return HWC2::Error::BadDisplay; + } + } else { + // Skip is not set + MarkLayersForGPUBypass(); + skip_prepare_ = false; + DLOGI("SecureDisplay %s, Skip Prepare/Commit and Flush", + secure_display_active_ ? "Starting" : "Stopping"); + flush_ = true; + } + + // If current draw cycle has different set of layers updating in comparison to previous cycle, + // cache content using GPU again. + // If set of updating layers remains same, use cached buffer and replace layers marked for GPU + // composition with SDE so that SurfaceFlinger does not compose them. Set cache inuse here. + bool needs_fb_refresh = NeedsFrameBufferRefresh(); + for (auto hwc_layer : layer_set_) { + Layer *layer = hwc_layer->GetSDMLayer(); + LayerComposition &composition = layer->composition; + + if ((composition == kCompositionSDE) || (composition == kCompositionHybrid) || + (composition == kCompositionBlit)) { + layer_requests_[hwc_layer->GetId()] = HWC2::LayerRequest::ClearClientTarget; + } + + if (!needs_fb_refresh && composition == kCompositionGPU) { + composition = kCompositionSDE; + } + hwc_layer->SetComposition(composition); + if (hwc_layer->CompositionChanged()) { + layer_changes_[hwc_layer->GetId()] = hwc_layer->GetCompositionType(); + } + } + *out_num_types = UINT32(layer_changes_.size()); + *out_num_requests = UINT32(layer_requests_.size()); + validated_ = true; + return HWC2::Error::None; +} + +HWC2::Error HWCDisplay::AcceptDisplayChanges() { + if (!validated_) { + return HWC2::Error::NotValidated; + } + return HWC2::Error::None; +} + +HWC2::Error HWCDisplay::GetChangedCompositionTypes(uint32_t *out_num_elements, + hwc2_layer_t *out_layers, int32_t *out_types) { + *out_num_elements = UINT32(layer_changes_.size()); + if (out_layers != nullptr && out_types != nullptr) { + int i = 0; + for (auto change : layer_changes_) { + out_layers[i] = change.first; + out_types[i] = INT32(change.second); + i++; + } + } + return HWC2::Error::None; +} + +HWC2::Error HWCDisplay::GetReleaseFences(uint32_t *out_num_elements, hwc2_layer_t *out_layers, + int32_t *out_fences) { + if (out_layers != nullptr && out_fences != nullptr) { + int i = 0; + for (auto hwc_layer : layer_set_) { + out_layers[i] = hwc_layer->GetId(); + out_fences[i] = hwc_layer->PopReleaseFence(); + i++; + } + } + *out_num_elements = UINT32(layer_set_.size()); + return HWC2::Error::None; +} + +HWC2::Error HWCDisplay::GetDisplayRequests(int32_t *out_display_requests, + uint32_t *out_num_elements, hwc2_layer_t *out_layers, + int32_t *out_layer_requests) { + // No display requests for now + // Use for sharing blit buffers and + // writing wfd buffer directly to output if there is full GPU composition + // and no color conversion needed + *out_display_requests = 0; + *out_num_elements = UINT32(layer_requests_.size()); + if (out_layers != nullptr && out_layer_requests != nullptr) { + int i = 0; + for (auto &request : layer_requests_) { + out_layers[i] = request.first; + out_layer_requests[i] = INT32(request.second); + i++; + } + } + return HWC2::Error::None; +} + +HWC2::Error HWCDisplay::CommitLayerStack(void) { + if (shutdown_pending_) { + return HWC2::Error::None; + } + + if (!validated_) { + DLOGW("Display is not validated"); + return HWC2::Error::NotValidated; + } + + DumpInputBuffers(); + + if (!flush_) { + DisplayError error = kErrorUndefined; + error = display_intf_->Commit(&layer_stack_); + + if (error == kErrorNone) { + // A commit is successfully submitted, start flushing on failure now onwards. + flush_on_error_ = true; + } else { + if (error == kErrorShutDown) { + shutdown_pending_ = true; + return HWC2::Error::Unsupported; + } else if (error != kErrorPermission) { + DLOGE("Commit failed. Error = %d", error); + // To prevent surfaceflinger infinite wait, flush the previous frame during Commit() + // so that previous buffer and fences are released, and override the error. + flush_ = true; + } + } + } + + return HWC2::Error::None; +} + +HWC2::Error HWCDisplay::PostCommitLayerStack(int32_t *out_retire_fence) { + auto status = HWC2::Error::None; + + // Do no call flush on errors, if a successful buffer is never submitted. + if (flush_ && flush_on_error_) { + display_intf_->Flush(); + } + + // TODO(user): No way to set the client target release fence on SF + int32_t &client_target_release_fence = + client_target_->GetSDMLayer()->input_buffer->release_fence_fd; + if (client_target_release_fence >= 0) { + close(client_target_release_fence); + client_target_release_fence = -1; + } + + for (auto hwc_layer : layer_set_) { + hwc_layer->ResetGeometryChanges(); + Layer *layer = hwc_layer->GetSDMLayer(); + LayerBuffer *layer_buffer = layer->input_buffer; + + if (!flush_) { + // If swapinterval property is set to 0 or for single buffer layers, do not update f/w + // release fences and discard fences from driver + if (swap_interval_zero_ || layer->flags.single_buffer) { + close(layer_buffer->release_fence_fd); + layer_buffer->release_fence_fd = -1; + } else if (layer->composition != kCompositionGPU) { + hwc_layer->PushReleaseFence(layer_buffer->release_fence_fd); + } + } + + if (layer_buffer->acquire_fence_fd >= 0) { + close(layer_buffer->acquire_fence_fd); + layer_buffer->acquire_fence_fd = -1; + } + } + + if (!flush_) { + // if swapinterval property is set to 0 then close and reset the list retire fence + if (swap_interval_zero_) { + close(layer_stack_.retire_fence_fd); + layer_stack_.retire_fence_fd = -1; + } + *out_retire_fence = stored_retire_fence_; + stored_retire_fence_ = layer_stack_.retire_fence_fd; + + if (dump_frame_count_) { + dump_frame_count_--; + dump_frame_index_++; + } + } + geometry_changes_ = GeometryChanges::kNone; + flush_ = false; + + return status; +} + +bool HWCDisplay::NeedsFrameBufferRefresh(void) { + // Frame buffer needs to be refreshed for the following reasons: + // 1. Any layer is marked skip in the current layer stack. + // 2. Any layer is added/removed/layer properties changes in the current layer stack. + // 3. Any layer handle is changed and it is marked for GPU composition + // 4. Any layer's current composition is different from previous composition. + if (layer_stack_.flags.skip_present || layer_stack_.flags.geometry_changed) { + return true; + } + + for (auto layer : layer_stack_.layers) { + // need FB refresh for s3d case + if (layer->input_buffer->s3d_format != kS3dFormatNone) { + return true; + } + + if (layer->composition == kCompositionGPUTarget || + layer->composition == kCompositionBlitTarget) { + continue; + } + } + + return false; +} + +void HWCDisplay::SetIdleTimeoutMs(uint32_t timeout_ms) { + return; +} + +DisplayError HWCDisplay::SetMaxMixerStages(uint32_t max_mixer_stages) { + DisplayError error = kErrorNone; + + if (display_intf_) { + error = display_intf_->SetMaxMixerStages(max_mixer_stages); + } + + return error; +} + +DisplayError HWCDisplay::ControlPartialUpdate(bool enable, uint32_t *pending) { + DisplayError error = kErrorNone; + + if (display_intf_) { + error = display_intf_->ControlPartialUpdate(enable, pending); + } + + return error; +} + +LayerBufferFormat HWCDisplay::GetSDMFormat(const int32_t &source, const int flags) { + LayerBufferFormat format = kFormatInvalid; + if (flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) { + switch (source) { + case HAL_PIXEL_FORMAT_RGBA_8888: + format = kFormatRGBA8888Ubwc; + break; + case HAL_PIXEL_FORMAT_RGBX_8888: + format = kFormatRGBX8888Ubwc; + break; + case HAL_PIXEL_FORMAT_BGR_565: + format = kFormatBGR565Ubwc; + break; + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC: + case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: + format = kFormatYCbCr420SPVenusUbwc; + break; + case HAL_PIXEL_FORMAT_RGBA_1010102: + format = kFormatRGBA1010102Ubwc; + break; + case HAL_PIXEL_FORMAT_RGBX_1010102: + format = kFormatRGBX1010102Ubwc; + break; + case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC: + format = kFormatYCbCr420TP10Ubwc; + break; + default: + DLOGE("Unsupported format type for UBWC %d", source); + return kFormatInvalid; + } + return format; + } + + switch (source) { + case HAL_PIXEL_FORMAT_RGBA_8888: + format = kFormatRGBA8888; + break; + case HAL_PIXEL_FORMAT_RGBA_5551: + format = kFormatRGBA5551; + break; + case HAL_PIXEL_FORMAT_RGBA_4444: + format = kFormatRGBA4444; + break; + case HAL_PIXEL_FORMAT_BGRA_8888: + format = kFormatBGRA8888; + break; + case HAL_PIXEL_FORMAT_RGBX_8888: + format = kFormatRGBX8888; + break; + case HAL_PIXEL_FORMAT_BGRX_8888: + format = kFormatBGRX8888; + break; + case HAL_PIXEL_FORMAT_RGB_888: + format = kFormatRGB888; + break; + case HAL_PIXEL_FORMAT_RGB_565: + format = kFormatRGB565; + break; + case HAL_PIXEL_FORMAT_BGR_565: + format = kFormatBGR565; + break; + case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: + format = kFormatYCbCr420SemiPlanarVenus; + break; + case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS: + format = kFormatYCrCb420SemiPlanarVenus; + break; + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC: + format = kFormatYCbCr420SPVenusUbwc; + break; + case HAL_PIXEL_FORMAT_YV12: + format = kFormatYCrCb420PlanarStride16; + break; + case HAL_PIXEL_FORMAT_YCrCb_420_SP: + format = kFormatYCrCb420SemiPlanar; + break; + case HAL_PIXEL_FORMAT_YCbCr_420_SP: + format = kFormatYCbCr420SemiPlanar; + break; + case HAL_PIXEL_FORMAT_YCbCr_422_SP: + format = kFormatYCbCr422H2V1SemiPlanar; + break; + case HAL_PIXEL_FORMAT_YCbCr_422_I: + format = kFormatYCbCr422H2V1Packed; + break; + case HAL_PIXEL_FORMAT_RGBA_1010102: + format = kFormatRGBA1010102; + break; + case HAL_PIXEL_FORMAT_ARGB_2101010: + format = kFormatARGB2101010; + break; + case HAL_PIXEL_FORMAT_RGBX_1010102: + format = kFormatRGBX1010102; + break; + case HAL_PIXEL_FORMAT_XRGB_2101010: + format = kFormatXRGB2101010; + break; + case HAL_PIXEL_FORMAT_BGRA_1010102: + format = kFormatBGRA1010102; + break; + case HAL_PIXEL_FORMAT_ABGR_2101010: + format = kFormatABGR2101010; + break; + case HAL_PIXEL_FORMAT_BGRX_1010102: + format = kFormatBGRX1010102; + break; + case HAL_PIXEL_FORMAT_XBGR_2101010: + format = kFormatXBGR2101010; + break; + case HAL_PIXEL_FORMAT_YCbCr_420_P010: + format = kFormatYCbCr420P010; + break; + case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC: + format = kFormatYCbCr420TP10Ubwc; + break; + default: + DLOGW("Unsupported format type = %d", source); + return kFormatInvalid; + } + + return format; +} + +void HWCDisplay::DumpInputBuffers() { + char dir_path[PATH_MAX]; + + if (!dump_frame_count_ || flush_ || !dump_input_layers_) { + return; + } + + snprintf(dir_path, sizeof(dir_path), "/data/misc/display/frame_dump_%s", GetDisplayString()); + + if (mkdir(dir_path, 0777) != 0 && errno != EEXIST) { + DLOGW("Failed to create %s directory errno = %d, desc = %s", dir_path, errno, strerror(errno)); + return; + } + + // if directory exists already, need to explicitly change the permission. + if (errno == EEXIST && chmod(dir_path, 0777) != 0) { + DLOGW("Failed to change permissions on %s directory", dir_path); + return; + } + + for (uint32_t i = 0; i < layer_stack_.layers.size(); i++) { + auto layer = layer_stack_.layers.at(i); + const private_handle_t *pvt_handle = + reinterpret_cast(layer->input_buffer->buffer_id); + auto acquire_fence_fd = layer->input_buffer->acquire_fence_fd; + + if (acquire_fence_fd >= 0) { + int error = sync_wait(acquire_fence_fd, 1000); + if (error < 0) { + DLOGW("sync_wait error errno = %d, desc = %s", errno, strerror(errno)); + return; + } + } + + if (pvt_handle && pvt_handle->base) { + char dump_file_name[PATH_MAX]; + size_t result = 0; + + snprintf(dump_file_name, sizeof(dump_file_name), "%s/input_layer%d_%dx%d_%s_frame%d.raw", + dir_path, i, pvt_handle->width, pvt_handle->height, + GetHALPixelFormatString(pvt_handle->format), dump_frame_index_); + + FILE *fp = fopen(dump_file_name, "w+"); + if (fp) { + result = fwrite(reinterpret_cast(pvt_handle->base), pvt_handle->size, 1, fp); + fclose(fp); + } + + DLOGI("Frame Dump %s: is %s", dump_file_name, result ? "Successful" : "Failed"); + } + } +} + +void HWCDisplay::DumpOutputBuffer(const BufferInfo &buffer_info, void *base, int fence) { + char dir_path[PATH_MAX]; + + snprintf(dir_path, sizeof(dir_path), "/data/misc/display/frame_dump_%s", GetDisplayString()); + + if (mkdir(dir_path, 777) != 0 && errno != EEXIST) { + DLOGW("Failed to create %s directory errno = %d, desc = %s", dir_path, errno, strerror(errno)); + return; + } + + // if directory exists already, need to explicitly change the permission. + if (errno == EEXIST && chmod(dir_path, 0777) != 0) { + DLOGW("Failed to change permissions on %s directory", dir_path); + return; + } + + if (base) { + char dump_file_name[PATH_MAX]; + size_t result = 0; + + if (fence >= 0) { + int error = sync_wait(fence, 1000); + if (error < 0) { + DLOGW("sync_wait error errno = %d, desc = %s", errno, strerror(errno)); + return; + } + } + + snprintf(dump_file_name, sizeof(dump_file_name), "%s/output_layer_%dx%d_%s_frame%d.raw", + dir_path, buffer_info.buffer_config.width, buffer_info.buffer_config.height, + GetFormatString(buffer_info.buffer_config.format), dump_frame_index_); + + FILE *fp = fopen(dump_file_name, "w+"); + if (fp) { + result = fwrite(base, buffer_info.alloc_buffer_info.size, 1, fp); + fclose(fp); + } + + DLOGI("Frame Dump of %s is %s", dump_file_name, result ? "Successful" : "Failed"); + } +} + +const char *HWCDisplay::GetHALPixelFormatString(int format) { + switch (format) { + case HAL_PIXEL_FORMAT_RGBA_8888: + return "RGBA_8888"; + case HAL_PIXEL_FORMAT_RGBX_8888: + return "RGBX_8888"; + case HAL_PIXEL_FORMAT_RGB_888: + return "RGB_888"; + case HAL_PIXEL_FORMAT_RGB_565: + return "RGB_565"; + case HAL_PIXEL_FORMAT_BGR_565: + return "BGR_565"; + case HAL_PIXEL_FORMAT_BGRA_8888: + return "BGRA_8888"; + case HAL_PIXEL_FORMAT_RGBA_5551: + return "RGBA_5551"; + case HAL_PIXEL_FORMAT_RGBA_4444: + return "RGBA_4444"; + case HAL_PIXEL_FORMAT_YV12: + return "YV12"; + case HAL_PIXEL_FORMAT_YCbCr_422_SP: + return "YCbCr_422_SP_NV16"; + case HAL_PIXEL_FORMAT_YCrCb_420_SP: + return "YCrCb_420_SP_NV21"; + case HAL_PIXEL_FORMAT_YCbCr_422_I: + return "YCbCr_422_I_YUY2"; + case HAL_PIXEL_FORMAT_YCrCb_422_I: + return "YCrCb_422_I_YVYU"; + case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: + return "NV12_ENCODEABLE"; + case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: + return "YCbCr_420_SP_TILED_TILE_4x2"; + case HAL_PIXEL_FORMAT_YCbCr_420_SP: + return "YCbCr_420_SP"; + case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: + return "YCrCb_420_SP_ADRENO"; + case HAL_PIXEL_FORMAT_YCrCb_422_SP: + return "YCrCb_422_SP"; + case HAL_PIXEL_FORMAT_R_8: + return "R_8"; + case HAL_PIXEL_FORMAT_RG_88: + return "RG_88"; + case HAL_PIXEL_FORMAT_INTERLACE: + return "INTERLACE"; + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: + return "YCbCr_420_SP_VENUS"; + case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS: + return "YCrCb_420_SP_VENUS"; + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC: + return "YCbCr_420_SP_VENUS_UBWC"; + case HAL_PIXEL_FORMAT_RGBA_1010102: + return "RGBA_1010102"; + case HAL_PIXEL_FORMAT_ARGB_2101010: + return "ARGB_2101010"; + case HAL_PIXEL_FORMAT_RGBX_1010102: + return "RGBX_1010102"; + case HAL_PIXEL_FORMAT_XRGB_2101010: + return "XRGB_2101010"; + case HAL_PIXEL_FORMAT_BGRA_1010102: + return "BGRA_1010102"; + case HAL_PIXEL_FORMAT_ABGR_2101010: + return "ABGR_2101010"; + case HAL_PIXEL_FORMAT_BGRX_1010102: + return "BGRX_1010102"; + case HAL_PIXEL_FORMAT_XBGR_2101010: + return "XBGR_2101010"; + case HAL_PIXEL_FORMAT_YCbCr_420_P010: + return "YCbCr_420_P010"; + case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC: + return "YCbCr_420_TP10_UBWC"; + default: + return "Unknown_format"; + } +} + +const char *HWCDisplay::GetDisplayString() { + switch (type_) { + case kPrimary: + return "primary"; + case kHDMI: + return "hdmi"; + case kVirtual: + return "virtual"; + default: + return "invalid"; + } +} + +int HWCDisplay::SetFrameBufferResolution(uint32_t x_pixels, uint32_t y_pixels) { + if (x_pixels <= 0 || y_pixels <= 0) { + DLOGV("Unsupported config: x_pixels=%d, y_pixels=%d", x_pixels, y_pixels); + return -EINVAL; + } + + if (framebuffer_config_->x_pixels == x_pixels && framebuffer_config_->y_pixels == y_pixels) { + return 0; + } + + DisplayConfigVariableInfo active_config; + uint32_t active_config_index = 0; + display_intf_->GetActiveConfig(&active_config_index); + DisplayError error = display_intf_->GetConfig(active_config_index, &active_config); + if (error != kErrorNone) { + DLOGV("GetConfig variable info failed. Error = %d", error); + return -EINVAL; + } + + if (active_config.x_pixels <= 0 || active_config.y_pixels <= 0) { + DLOGV("Invalid panel resolution (%dx%d)", active_config.x_pixels, active_config.y_pixels); + return -EINVAL; + } + + // Create rects to represent the new source and destination crops + LayerRect crop = LayerRect(0, 0, FLOAT(x_pixels), FLOAT(y_pixels)); + LayerRect dst = LayerRect(0, 0, FLOAT(active_config.x_pixels), FLOAT(active_config.y_pixels)); + // Set rotate90 to false since this is taken care of during regular composition. + bool rotate90 = false; + error = display_intf_->IsScalingValid(crop, dst, rotate90); + if (error != kErrorNone) { + DLOGV("Unsupported resolution: (%dx%d)", x_pixels, y_pixels); + return -EINVAL; + } + + framebuffer_config_->x_pixels = x_pixels; + framebuffer_config_->y_pixels = y_pixels; + framebuffer_config_->vsync_period_ns = active_config.vsync_period_ns; + framebuffer_config_->x_dpi = active_config.x_dpi; + framebuffer_config_->y_dpi = active_config.y_dpi; + + auto client_target_layer = client_target_->GetSDMLayer(); + client_target_layer->src_rect = crop; + client_target_layer->dst_rect = dst; + + int aligned_width; + int aligned_height; + int usage = GRALLOC_USAGE_HW_FB; + int format = HAL_PIXEL_FORMAT_RGBA_8888; + int ubwc_enabled = 0; + int flags = 0; + HWCDebugHandler::Get()->GetProperty("debug.gralloc.enable_fb_ubwc", &ubwc_enabled); + if (ubwc_enabled == 1) { + usage |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC; + flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED; + } + AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(INT(x_pixels), INT(y_pixels), format, usage, + aligned_width, aligned_height); + + // TODO(user): How does the dirty region get set on the client target? File bug on Google + client_target_layer->composition = kCompositionGPUTarget; + client_target_layer->input_buffer->format = GetSDMFormat(format, flags); + client_target_layer->input_buffer->width = UINT32(aligned_width); + client_target_layer->input_buffer->height = UINT32(aligned_height); + client_target_layer->plane_alpha = 255; + + DLOGI("New framebuffer resolution (%dx%d)", framebuffer_config_->x_pixels, + framebuffer_config_->y_pixels); + + return 0; +} + +void HWCDisplay::GetFrameBufferResolution(uint32_t *x_pixels, uint32_t *y_pixels) { + *x_pixels = framebuffer_config_->x_pixels; + *y_pixels = framebuffer_config_->y_pixels; +} + +void HWCDisplay::ScaleDisplayFrame(hwc_rect_t *display_frame) { + if (!IsFrameBufferScaled()) { + return; + } + + uint32_t active_config_index = 0; + display_intf_->GetActiveConfig(&active_config_index); + DisplayConfigVariableInfo active_config; + DisplayError error = display_intf_->GetConfig(active_config_index, &active_config); + if (error != kErrorNone) { + DLOGE("GetConfig variable info failed. Error = %d", error); + return; + } + + float custom_x_pixels = FLOAT(framebuffer_config_->x_pixels); + float custom_y_pixels = FLOAT(framebuffer_config_->y_pixels); + float active_x_pixels = FLOAT(active_config.x_pixels); + float active_y_pixels = FLOAT(active_config.y_pixels); + float x_pixels_ratio = active_x_pixels / custom_x_pixels; + float y_pixels_ratio = active_y_pixels / custom_y_pixels; + float layer_width = FLOAT(display_frame->right - display_frame->left); + float layer_height = FLOAT(display_frame->bottom - display_frame->top); + + display_frame->left = INT(x_pixels_ratio * FLOAT(display_frame->left)); + display_frame->top = INT(y_pixels_ratio * FLOAT(display_frame->top)); + display_frame->right = INT(FLOAT(display_frame->left) + layer_width * x_pixels_ratio); + display_frame->bottom = INT(FLOAT(display_frame->top) + layer_height * y_pixels_ratio); +} + +bool HWCDisplay::IsFrameBufferScaled() { + if (framebuffer_config_->x_pixels == 0 || framebuffer_config_->y_pixels == 0) { + return false; + } + uint32_t panel_x_pixels = 0; + uint32_t panel_y_pixels = 0; + GetPanelResolution(&panel_x_pixels, &panel_y_pixels); + return (framebuffer_config_->x_pixels != panel_x_pixels) || + (framebuffer_config_->y_pixels != panel_y_pixels); +} + +void HWCDisplay::GetPanelResolution(uint32_t *x_pixels, uint32_t *y_pixels) { + DisplayConfigVariableInfo active_config; + uint32_t active_config_index = 0; + display_intf_->GetActiveConfig(&active_config_index); + DisplayError error = display_intf_->GetConfig(active_config_index, &active_config); + if (error != kErrorNone) { + DLOGE("GetConfig variable info failed. Error = %d", error); + return; + } + *x_pixels = active_config.x_pixels; + *y_pixels = active_config.y_pixels; +} + +int HWCDisplay::SetDisplayStatus(uint32_t display_status) { + int status = 0; + + switch (display_status) { + case kDisplayStatusResume: + display_paused_ = false; + case kDisplayStatusOnline: + status = INT32(SetPowerMode(HWC2::PowerMode::On)); + break; + case kDisplayStatusPause: + display_paused_ = true; + case kDisplayStatusOffline: + status = INT32(SetPowerMode(HWC2::PowerMode::Off)); + break; + default: + DLOGW("Invalid display status %d", display_status); + return -EINVAL; + } + + if (display_status == kDisplayStatusResume || display_status == kDisplayStatusPause) { + callbacks_->Refresh(HWC_DISPLAY_PRIMARY); + } + + return status; +} + +HWC2::Error HWCDisplay::SetCursorPosition(hwc2_layer_t layer, int x, int y) { + if (shutdown_pending_) { + return HWC2::Error::None; + } + + // TODO(user): Validate layer + // TODO(user): Check if we're in a validate/present cycle + + auto error = display_intf_->SetCursorPosition(x, y); + if (error != kErrorNone) { + if (error == kErrorShutDown) { + shutdown_pending_ = true; + return HWC2::Error::None; + } + + DLOGE("Failed for x = %d y = %d, Error = %d", x, y, error); + return HWC2::Error::BadDisplay; + } + + return HWC2::Error::None; +} + +int HWCDisplay::OnMinHdcpEncryptionLevelChange(uint32_t min_enc_level) { + DisplayError error = display_intf_->OnMinHdcpEncryptionLevelChange(min_enc_level); + if (error != kErrorNone) { + DLOGE("Failed. Error = %d", error); + return -1; + } + + return 0; +} + +void HWCDisplay::MarkLayersForGPUBypass() { + for (auto hwc_layer : layer_set_) { + auto layer = hwc_layer->GetSDMLayer(); + layer->composition = kCompositionSDE; + } +} + +void HWCDisplay::ApplyScanAdjustment(hwc_rect_t *display_frame) { +} + +int HWCDisplay::SetPanelBrightness(int level) { + int ret = 0; + if (display_intf_) + ret = display_intf_->SetPanelBrightness(level); + else + ret = -EINVAL; + + return ret; +} + +int HWCDisplay::GetPanelBrightness(int *level) { + return display_intf_->GetPanelBrightness(level); +} + +int HWCDisplay::ToggleScreenUpdates(bool enable) { + display_paused_ = enable ? false : true; + callbacks_->Refresh(HWC_DISPLAY_PRIMARY); + return 0; +} + +int HWCDisplay::ColorSVCRequestRoute(const PPDisplayAPIPayload &in_payload, + PPDisplayAPIPayload *out_payload, + PPPendingParams *pending_action) { + int ret = 0; + + if (display_intf_) + ret = display_intf_->ColorSVCRequestRoute(in_payload, out_payload, pending_action); + else + ret = -EINVAL; + + return ret; +} + +int HWCDisplay::GetVisibleDisplayRect(hwc_rect_t *visible_rect) { + if (!IsValid(display_rect_)) { + return -EINVAL; + } + + visible_rect->left = INT(display_rect_.left); + visible_rect->top = INT(display_rect_.top); + visible_rect->right = INT(display_rect_.right); + visible_rect->bottom = INT(display_rect_.bottom); + DLOGI("Dpy = %d Visible Display Rect(%d %d %d %d)", visible_rect->left, visible_rect->top, + visible_rect->right, visible_rect->bottom); + + return 0; +} + +void HWCDisplay::SetSecureDisplay(bool secure_display_active) { + secure_display_active_ = secure_display_active; + return; +} + +int HWCDisplay::SetActiveDisplayConfig(int config) { + return display_intf_->SetActiveConfig(UINT32(config)) == kErrorNone ? 0 : -1; +} + +int HWCDisplay::GetActiveDisplayConfig(uint32_t *config) { + return display_intf_->GetActiveConfig(config) == kErrorNone ? 0 : -1; +} + +int HWCDisplay::GetDisplayConfigCount(uint32_t *count) { + return display_intf_->GetNumVariableInfoConfigs(count) == kErrorNone ? 0 : -1; +} + +int HWCDisplay::GetDisplayAttributesForConfig(int config, DisplayConfigVariableInfo *attributes) { + return display_intf_->GetConfig(UINT32(config), attributes) == kErrorNone ? 0 : -1; +} + +bool HWCDisplay::SingleLayerUpdating(void) { + uint32_t updating_count = 0; + + for (uint i = 0; i < layer_set_.size(); i++) { + auto layer = layer_stack_.layers.at(i); + if (layer->flags.updating) { + updating_count++; + } + } + + return (updating_count == 1); +} + +uint32_t HWCDisplay::SanitizeRefreshRate(uint32_t req_refresh_rate) { + uint32_t refresh_rate = req_refresh_rate; + + if (refresh_rate < min_refresh_rate_) { + // Pick the next multiple of request which is within the range + refresh_rate = + (((min_refresh_rate_ / refresh_rate) + ((min_refresh_rate_ % refresh_rate) ? 1 : 0)) * + refresh_rate); + } + + if (refresh_rate > max_refresh_rate_) { + refresh_rate = max_refresh_rate_; + } + + return refresh_rate; +} + +DisplayClass HWCDisplay::GetDisplayClass() { + return display_class_; +} + +void HWCDisplay::CloseAcquireFds() { + for (auto hwc_layer : layer_set_) { + auto layer = hwc_layer->GetSDMLayer(); + if (layer->input_buffer->acquire_fence_fd >= 0) { + close(layer->input_buffer->acquire_fence_fd); + } + } + int32_t &client_target_acquire_fence = + client_target_->GetSDMLayer()->input_buffer->acquire_fence_fd; + if (client_target_acquire_fence >= 0) { + close(client_target_acquire_fence); + client_target_acquire_fence = -1; + } +} + +} // namespace sdm diff --git a/sdm/libs/hwc2/hwc_display.h b/sdm/libs/hwc2/hwc_display.h new file mode 100644 index 00000000..64305382 --- /dev/null +++ b/sdm/libs/hwc2/hwc_display.h @@ -0,0 +1,215 @@ +/* + * Copyright (c) 2014-2016, The Linux Foundation. All rights reserved. + * Not a Contribution. + * + * Copyright 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __HWC_DISPLAY_H__ +#define __HWC_DISPLAY_H__ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "hwc_callbacks.h" +#include "hwc_layers.h" + +namespace sdm { + +class BlitEngine; + +// Subclasses set this to their type. This has to be different from DisplayType. +// This is to avoid RTTI and dynamic_cast +enum DisplayClass { + DISPLAY_CLASS_PRIMARY, + DISPLAY_CLASS_EXTERNAL, + DISPLAY_CLASS_VIRTUAL, + DISPLAY_CLASS_NULL +}; + +class HWCDisplay : public DisplayEventHandler { + public: + virtual ~HWCDisplay() {} + virtual int Init(); + virtual int Deinit(); + + // Framebuffer configurations + virtual void SetIdleTimeoutMs(uint32_t timeout_ms); + virtual void SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type); + virtual DisplayError SetMaxMixerStages(uint32_t max_mixer_stages); + virtual DisplayError ControlPartialUpdate(bool enable, uint32_t *pending); + virtual HWC2::PowerMode GetLastPowerMode(); + virtual int SetFrameBufferResolution(uint32_t x_pixels, uint32_t y_pixels); + virtual void GetFrameBufferResolution(uint32_t *x_pixels, uint32_t *y_pixels); + virtual void GetPanelResolution(uint32_t *x_pixels, uint32_t *y_pixels); + virtual int SetDisplayStatus(uint32_t display_status); + virtual int OnMinHdcpEncryptionLevelChange(uint32_t min_enc_level); + virtual int Perform(uint32_t operation, ...); + virtual void SetSecureDisplay(bool secure_display_active); + + // Captures frame output in the buffer specified by output_buffer_info. The API is + // non-blocking and the client is expected to check operation status later on. + // Returns -1 if the input is invalid. + virtual int FrameCaptureAsync(const BufferInfo &output_buffer_info, bool post_processed) { + return -1; + } + // Returns the status of frame capture operation requested with FrameCaptureAsync(). + // -EAGAIN : No status obtain yet, call API again after another frame. + // < 0 : Operation happened but failed. + // 0 : Success. + virtual int GetFrameCaptureStatus() { return -EAGAIN; } + + // Display Configurations + virtual int SetActiveDisplayConfig(int config); + virtual int GetActiveDisplayConfig(uint32_t *config); + virtual int GetDisplayConfigCount(uint32_t *count); + virtual int GetDisplayAttributesForConfig(int config, DisplayConfigVariableInfo *attributes); + + int SetPanelBrightness(int level); + int GetPanelBrightness(int *level); + int ToggleScreenUpdates(bool enable); + int ColorSVCRequestRoute(const PPDisplayAPIPayload &in_payload, PPDisplayAPIPayload *out_payload, + PPPendingParams *pending_action); + DisplayClass GetDisplayClass(); + int GetVisibleDisplayRect(hwc_rect_t *rect); + void BuildLayerStack(void); + HWCLayer *GetHWCLayer(hwc2_layer_t layer); + + // HWC2 APIs + virtual HWC2::Error AcceptDisplayChanges(void); + virtual HWC2::Error GetActiveConfig(hwc2_config_t *out_config); + virtual HWC2::Error SetActiveConfig(hwc2_config_t config); + virtual HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence, + int32_t dataspace); + virtual HWC2::Error GetDisplayConfigs(uint32_t *out_num_configs, hwc2_config_t *out_configs); + virtual HWC2::Error GetDisplayAttribute(hwc2_config_t config, HWC2::Attribute attribute, + int32_t *out_value); + virtual HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height, int32_t format, + int32_t dataspace); + virtual HWC2::Error GetChangedCompositionTypes(uint32_t *out_num_elements, + hwc2_layer_t *out_layers, int32_t *out_types); + virtual HWC2::Error GetDisplayRequests(int32_t *out_display_requests, uint32_t *out_num_elements, + hwc2_layer_t *out_layers, int32_t *out_layer_requests); + virtual HWC2::Error GetDisplayName(uint32_t *out_size, char *out_name); + virtual HWC2::Error GetDisplayType(int32_t *out_type); + virtual HWC2::Error SetCursorPosition(hwc2_layer_t layer, int x, int y); + virtual HWC2::Error SetVsyncEnabled(HWC2::Vsync enabled); + virtual HWC2::Error SetPowerMode(HWC2::PowerMode mode); + virtual HWC2::Error CreateLayer(hwc2_layer_t *out_layer_id); + virtual HWC2::Error DestroyLayer(hwc2_layer_t layer_id); + virtual HWC2::Error SetLayerZOrder(hwc2_layer_t layer_id, uint32_t z); + virtual HWC2::Error Validate(uint32_t *out_num_types, uint32_t *out_num_requests) = 0; + virtual HWC2::Error GetReleaseFences(uint32_t *out_num_elements, hwc2_layer_t *out_layers, + int32_t *out_fences); + virtual HWC2::Error Present(int32_t *out_retire_fence) = 0; + + protected: + enum DisplayStatus { + kDisplayStatusOffline = 0, + kDisplayStatusOnline, + kDisplayStatusPause, + kDisplayStatusResume, + }; + + // Maximum number of layers supported by display manager. + static const uint32_t kMaxLayerCount = 32; + + HWCDisplay(CoreInterface *core_intf, HWCCallbacks *callbacks, DisplayType type, hwc2_display_t id, + bool needs_blit, qService::QService *qservice, DisplayClass display_class); + + // DisplayEventHandler methods + virtual DisplayError VSync(const DisplayEventVSync &vsync); + virtual DisplayError Refresh(); + virtual DisplayError CECMessage(char *message); + virtual void DumpOutputBuffer(const BufferInfo &buffer_info, void *base, int fence); + virtual HWC2::Error PrepareLayerStack(uint32_t *out_num_types, uint32_t *out_num_requests); + virtual HWC2::Error CommitLayerStack(void); + virtual HWC2::Error PostCommitLayerStack(int32_t *out_retire_fence); + LayerBufferFormat GetSDMFormat(const int32_t &source, const int flags); + const char *GetHALPixelFormatString(int format); + const char *GetDisplayString(); + void ScaleDisplayFrame(hwc_rect_t *display_frame); + void MarkLayersForGPUBypass(void); + virtual void ApplyScanAdjustment(hwc_rect_t *display_frame); + bool NeedsFrameBufferRefresh(void); + bool SingleLayerUpdating(void); + uint32_t SanitizeRefreshRate(uint32_t req_refresh_rate); + virtual void CloseAcquireFds(); + + enum { + INPUT_LAYER_DUMP, + OUTPUT_LAYER_DUMP, + }; + + CoreInterface *core_intf_; + HWCCallbacks *callbacks_; + DisplayType type_; + hwc2_display_t id_; + bool needs_blit_ = false; + DisplayInterface *display_intf_ = NULL; + LayerStack layer_stack_; + HWCLayer *client_target_; // Also known as framebuffer target + std::map layer_map_; // Look up by Id - TODO + std::multiset layer_set_; // Maintain a set sorted by Z + std::map layer_changes_; + std::map layer_requests_; + bool flush_on_error_ = false; + bool flush_ = false; + uint32_t dump_frame_count_ = 0; + uint32_t dump_frame_index_ = 0; + bool dump_input_layers_ = false; + HWC2::PowerMode last_power_mode_; + bool swap_interval_zero_ = false; + DisplayConfigVariableInfo *framebuffer_config_ = NULL; + bool display_paused_ = false; + uint32_t min_refresh_rate_ = 0; + uint32_t max_refresh_rate_ = 0; + uint32_t current_refresh_rate_ = 0; + bool use_metadata_refresh_rate_ = false; + uint32_t metadata_refresh_rate_ = 0; + uint32_t force_refresh_rate_ = 0; + bool boot_animation_completed_ = false; + bool shutdown_pending_ = false; + bool use_blit_comp_ = false; + bool secure_display_active_ = false; + bool skip_prepare_ = false; + bool solid_fill_enable_ = false; + uint32_t solid_fill_color_ = 0; + LayerRect display_rect_; + bool validated_ = false; + + private: + bool IsFrameBufferScaled(); + void DumpInputBuffers(void); + BlitEngine *blit_engine_ = NULL; + qService::QService *qservice_ = NULL; + DisplayClass display_class_; + int32_t stored_retire_fence_; + uint32_t geometry_changes_ = GeometryChanges::kNone; +}; + +inline int HWCDisplay::Perform(uint32_t operation, ...) { + return 0; +} + +} // namespace sdm + +#endif // __HWC_DISPLAY_H__ diff --git a/sdm/libs/hwc2/hwc_display_external.cpp b/sdm/libs/hwc2/hwc_display_external.cpp new file mode 100644 index 00000000..73ed0e8c --- /dev/null +++ b/sdm/libs/hwc2/hwc_display_external.cpp @@ -0,0 +1,206 @@ +/* +* Copyright (c) 2014 - 2016, The Linux Foundation. All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are +* met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above +* copyright notice, this list of conditions and the following +* disclaimer in the documentation and/or other materials provided +* with the distribution. +* * Neither the name of The Linux Foundation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include +#include +#include + +#include "hwc_display_external.h" +#include "hwc_debugger.h" + +#define __CLASS__ "HWCDisplayExternal" + +namespace sdm { + +int HWCDisplayExternal::Create(CoreInterface *core_intf, HWCCallbacks *callbacks, + qService::QService *qservice, HWCDisplay **hwc_display) { + return Create(core_intf, callbacks, 0, 0, qservice, false, hwc_display); +} + +int HWCDisplayExternal::Create(CoreInterface *core_intf, HWCCallbacks *callbacks, + uint32_t primary_width, uint32_t primary_height, + qService::QService *qservice, bool use_primary_res, + HWCDisplay **hwc_display) { + uint32_t external_width = 0; + uint32_t external_height = 0; + + HWCDisplay *hwc_display_external = new HWCDisplayExternal(core_intf, callbacks, qservice); + int status = hwc_display_external->Init(); + if (status) { + delete hwc_display_external; + return status; + } + + hwc_display_external->GetPanelResolution(&external_width, &external_height); + + if (primary_width && primary_height) { + // use_primary_res means HWCDisplayExternal should directly set framebuffer resolution to the + // provided primary_width and primary_height + if (use_primary_res) { + external_width = primary_width; + external_height = primary_height; + } else { + int downscale_enabled = 0; + HWCDebugHandler::Get()->GetProperty("sdm.debug.downscale_external", &downscale_enabled); + if (downscale_enabled) { + GetDownscaleResolution(primary_width, primary_height, &external_width, &external_height); + } + } + } + + status = hwc_display_external->SetFrameBufferResolution(external_width, external_height); + if (status) { + Destroy(hwc_display_external); + return status; + } + + *hwc_display = hwc_display_external; + + return status; +} + +void HWCDisplayExternal::Destroy(HWCDisplay *hwc_display) { + hwc_display->Deinit(); + delete hwc_display; +} + +HWCDisplayExternal::HWCDisplayExternal(CoreInterface *core_intf, HWCCallbacks *callbacks, + qService::QService *qservice) + : HWCDisplay(core_intf, callbacks, kHDMI, HWC_DISPLAY_EXTERNAL, false, qservice, + DISPLAY_CLASS_EXTERNAL) { +} + +HWC2::Error HWCDisplayExternal::Validate(uint32_t *out_num_types, uint32_t *out_num_requests) { + auto status = HWC2::Error::None; + + if (secure_display_active_) { + MarkLayersForGPUBypass(); + return status; + } + + BuildLayerStack(); + + if (layer_set_.empty()) { + flush_ = true; + return status; + } + + status = PrepareLayerStack(out_num_types, out_num_requests); + return status; +} + +HWC2::Error HWCDisplayExternal::Present(int32_t *out_retire_fence) { + auto status = HWC2::Error::None; + + if (!secure_display_active_) { + status = HWCDisplay::CommitLayerStack(); + if (status == HWC2::Error::None) { + status = HWCDisplay::PostCommitLayerStack(out_retire_fence); + } + } + CloseAcquireFds(); + return status; +} + +void HWCDisplayExternal::ApplyScanAdjustment(hwc_rect_t *display_frame) { + if (display_intf_->IsUnderscanSupported()) { + return; + } + + // Read user defined width and height ratio + int width = 0, height = 0; + HWCDebugHandler::Get()->GetProperty("sdm.external_action_safe_width", &width); + float width_ratio = FLOAT(width) / 100.0f; + HWCDebugHandler::Get()->GetProperty("sdm.external_action_safe_height", &height); + float height_ratio = FLOAT(height) / 100.0f; + + if (width_ratio == 0.0f || height_ratio == 0.0f) { + return; + } + + uint32_t panel_width = 0; + uint32_t panel_height = 0; + GetPanelResolution(&panel_width, &panel_height); + + if (panel_width == 0 || panel_height == 0) { + DLOGV("Invalid panel dimensions (%d, %d)", panel_width, panel_height); + return; + } + + uint32_t new_panel_width = UINT32(panel_width * FLOAT(1.0f - width_ratio)); + uint32_t new_panel_height = UINT32(panel_height * FLOAT(1.0f - height_ratio)); + + int x_offset = INT((FLOAT(panel_width) * width_ratio) / 2.0f); + int y_offset = INT((FLOAT(panel_height) * height_ratio) / 2.0f); + + display_frame->left = + (display_frame->left * INT32(new_panel_width) / INT32(panel_width)) + x_offset; + display_frame->top = + (display_frame->top * INT32(new_panel_height) / INT32(panel_height)) + y_offset; + display_frame->right = + ((display_frame->right * INT32(new_panel_width)) / INT32(panel_width)) + x_offset; + display_frame->bottom = + ((display_frame->bottom * INT32(new_panel_height)) / INT32(panel_height)) + y_offset; +} + +void HWCDisplayExternal::SetSecureDisplay(bool secure_display_active) { + if (secure_display_active_ != secure_display_active) { + secure_display_active_ = secure_display_active; + + if (secure_display_active_) { + DisplayError error = display_intf_->Flush(); + if (error != kErrorNone) { + DLOGE("Flush failed. Error = %d", error); + } + } + } + return; +} + +static void AdjustSourceResolution(uint32_t dst_width, uint32_t dst_height, uint32_t *src_width, + uint32_t *src_height) { + *src_height = (dst_width * (*src_height)) / (*src_width); + *src_width = dst_width; +} + +void HWCDisplayExternal::GetDownscaleResolution(uint32_t primary_width, uint32_t primary_height, + uint32_t *non_primary_width, + uint32_t *non_primary_height) { + uint32_t primary_area = primary_width * primary_height; + uint32_t non_primary_area = (*non_primary_width) * (*non_primary_height); + + if (primary_area > non_primary_area) { + if (primary_height > primary_width) { + Swap(primary_height, primary_width); + } + AdjustSourceResolution(primary_width, primary_height, non_primary_width, non_primary_height); + } +} + +} // namespace sdm diff --git a/sdm/libs/hwc2/hwc_display_external.h b/sdm/libs/hwc2/hwc_display_external.h new file mode 100644 index 00000000..ce7547c0 --- /dev/null +++ b/sdm/libs/hwc2/hwc_display_external.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2014-2016, The Linux Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __HWC_DISPLAY_EXTERNAL_H__ +#define __HWC_DISPLAY_EXTERNAL_H__ + +#include "hwc_display.h" + +namespace sdm { + +class HWCDisplayExternal : public HWCDisplay { + public: + static int Create(CoreInterface *core_intf, HWCCallbacks *callbacks, uint32_t primary_width, + uint32_t primary_height, qService::QService *qservice, bool use_primary_res, + HWCDisplay **hwc_display); + static int Create(CoreInterface *core_intf, HWCCallbacks *callbacks, qService::QService *qservice, + HWCDisplay **hwc_display); + static void Destroy(HWCDisplay *hwc_display); + virtual HWC2::Error Validate(uint32_t *out_num_types, uint32_t *out_num_requests); + virtual HWC2::Error Present(int32_t *out_retire_fence); + virtual void SetSecureDisplay(bool secure_display_active); + + private: + HWCDisplayExternal(CoreInterface *core_intf, HWCCallbacks *callbacks, + qService::QService *qservice); + void ApplyScanAdjustment(hwc_rect_t *display_frame); + static void GetDownscaleResolution(uint32_t primary_width, uint32_t primary_height, + uint32_t *virtual_width, uint32_t *virtual_height); +}; + +} // namespace sdm + +#endif // __HWC_DISPLAY_EXTERNAL_H__ diff --git a/sdm/libs/hwc2/hwc_display_primary.cpp b/sdm/libs/hwc2/hwc_display_primary.cpp new file mode 100644 index 00000000..40ac723a --- /dev/null +++ b/sdm/libs/hwc2/hwc_display_primary.cpp @@ -0,0 +1,471 @@ +/* +* Copyright (c) 2014 - 2016, The Linux Foundation. All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are +* met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above +* copyright notice, this list of conditions and the following +* disclaimer in the documentation and/or other materials provided +* with the distribution. +* * Neither the name of The Linux Foundation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include +#include +#include +#include +#include +#include + +#include "hwc_display_primary.h" +#include "hwc_debugger.h" + +#define __CLASS__ "HWCDisplayPrimary" + +namespace sdm { + +int HWCDisplayPrimary::Create(CoreInterface *core_intf, BufferAllocator *buffer_allocator, + HWCCallbacks *callbacks, qService::QService *qservice, + HWCDisplay **hwc_display) { + int status = 0; + uint32_t primary_width = 0; + uint32_t primary_height = 0; + + HWCDisplay *hwc_display_primary = + new HWCDisplayPrimary(core_intf, buffer_allocator, callbacks, qservice); + status = hwc_display_primary->Init(); + if (status) { + delete hwc_display_primary; + return status; + } + + hwc_display_primary->GetPanelResolution(&primary_width, &primary_height); + int width = 0, height = 0; + HWCDebugHandler::Get()->GetProperty("sdm.fb_size_width", &width); + HWCDebugHandler::Get()->GetProperty("sdm.fb_size_height", &height); + if (width > 0 && height > 0) { + primary_width = UINT32(width); + primary_height = UINT32(height); + } + + status = hwc_display_primary->SetFrameBufferResolution(primary_width, primary_height); + if (status) { + Destroy(hwc_display_primary); + return status; + } + + *hwc_display = hwc_display_primary; + + return status; +} + +void HWCDisplayPrimary::Destroy(HWCDisplay *hwc_display) { + hwc_display->Deinit(); + delete hwc_display; +} + +HWCDisplayPrimary::HWCDisplayPrimary(CoreInterface *core_intf, BufferAllocator *buffer_allocator, + HWCCallbacks *callbacks, qService::QService *qservice) + : HWCDisplay(core_intf, callbacks, kPrimary, HWC_DISPLAY_PRIMARY, true, qservice, + DISPLAY_CLASS_PRIMARY), + buffer_allocator_(buffer_allocator), + cpu_hint_(NULL) { +} + +int HWCDisplayPrimary::Init() { + cpu_hint_ = new CPUHint(); + if (cpu_hint_->Init(static_cast(HWCDebugHandler::Get())) != kErrorNone) { + delete cpu_hint_; + cpu_hint_ = NULL; + } + + use_metadata_refresh_rate_ = true; + int disable_metadata_dynfps = 0; + HWCDebugHandler::Get()->GetProperty("persist.metadata_dynfps.disable", &disable_metadata_dynfps); + if (disable_metadata_dynfps) { + use_metadata_refresh_rate_ = false; + } + + return HWCDisplay::Init(); +} + +void HWCDisplayPrimary::ProcessBootAnimCompleted() { + uint32_t numBootUpLayers = 0; + // TODO(user): Remove this hack + + numBootUpLayers = static_cast(Debug::GetBootAnimLayerCount()); + + if (numBootUpLayers == 0) { + numBootUpLayers = 2; + } + /* All other checks namely "init.svc.bootanim" or + * HWC_GEOMETRY_CHANGED fail in correctly identifying the + * exact bootup transition to homescreen + */ + char cryptoState[PROPERTY_VALUE_MAX]; + char voldDecryptState[PROPERTY_VALUE_MAX]; + bool isEncrypted = false; + bool main_class_services_started = false; + if (property_get("ro.crypto.state", cryptoState, "unencrypted")) { + if (!strcmp(cryptoState, "encrypted")) { + isEncrypted = true; + if (property_get("vold.decrypt", voldDecryptState, "") && + !strcmp(voldDecryptState, "trigger_restart_framework")) + main_class_services_started = true; + } + } + if ((!isEncrypted || (isEncrypted && main_class_services_started)) && + (layer_set_.size() > numBootUpLayers)) { + boot_animation_completed_ = true; + // Applying default mode after bootanimation is finished And + // If Data is Encrypted, it is ready for access. + if (display_intf_) + display_intf_->ApplyDefaultDisplayMode(); + } +} + +HWC2::Error HWCDisplayPrimary::Validate(uint32_t *out_num_types, uint32_t *out_num_requests) { + auto status = HWC2::Error::None; + DisplayError error = kErrorNone; + + if (!boot_animation_completed_) + ProcessBootAnimCompleted(); + + if (display_paused_) { + MarkLayersForGPUBypass(); + return status; + } + + // Fill in the remaining blanks in the layers and add them to the SDM layerstack + BuildLayerStack(); + + bool pending_output_dump = dump_frame_count_ && dump_output_to_file_; + + if (frame_capture_buffer_queued_ || pending_output_dump) { + // RHS values were set in FrameCaptureAsync() called from a binder thread. They are picked up + // here in a subsequent draw round. + layer_stack_.output_buffer = &output_buffer_; + layer_stack_.flags.post_processed_output = post_processed_output_; + } + + bool one_updating_layer = SingleLayerUpdating(); + ToggleCPUHint(one_updating_layer); + + uint32_t refresh_rate = GetOptimalRefreshRate(one_updating_layer); + if (current_refresh_rate_ != refresh_rate) { + error = display_intf_->SetRefreshRate(refresh_rate); + } + + if (error == kErrorNone) { + // On success, set current refresh rate to new refresh rate + current_refresh_rate_ = refresh_rate; + } + + if (handle_idle_timeout_) { + handle_idle_timeout_ = false; + } + + // TODO(user): Validate this + if (layer_set_.empty()) { + flush_ = true; + return status; + } + + status = PrepareLayerStack(out_num_types, out_num_requests); + return status; +} + +HWC2::Error HWCDisplayPrimary::Present(int32_t *out_retire_fence) { + auto status = HWC2::Error::None; + if (display_paused_) { + // TODO(user): From old HWC implementation + // If we do not handle the frame set retireFenceFd to outbufAcquireFenceFd + // Revisit this when validating display_paused + DisplayError error = display_intf_->Flush(); + if (error != kErrorNone) { + DLOGE("Flush failed. Error = %d", error); + } + } else { + status = HWCDisplay::CommitLayerStack(); + if (status == HWC2::Error::None) { + HandleFrameOutput(); + status = HWCDisplay::PostCommitLayerStack(out_retire_fence); + } + } + CloseAcquireFds(); + return status; +} + +int HWCDisplayPrimary::Perform(uint32_t operation, ...) { + va_list args; + va_start(args, operation); + int val = va_arg(args, int32_t); + va_end(args); + switch (operation) { + case SET_METADATA_DYN_REFRESH_RATE: + SetMetaDataRefreshRateFlag(val); + break; + case SET_BINDER_DYN_REFRESH_RATE: + ForceRefreshRate(UINT32(val)); + break; + case SET_DISPLAY_MODE: + SetDisplayMode(UINT32(val)); + break; + case SET_QDCM_SOLID_FILL_INFO: + SetQDCMSolidFillInfo(true, UINT32(val)); + break; + case UNSET_QDCM_SOLID_FILL_INFO: + SetQDCMSolidFillInfo(false, UINT32(val)); + break; + default: + DLOGW("Invalid operation %d", operation); + return -EINVAL; + } + + return 0; +} + +DisplayError HWCDisplayPrimary::SetDisplayMode(uint32_t mode) { + DisplayError error = kErrorNone; + + if (display_intf_) { + error = display_intf_->SetDisplayMode(mode); + } + + return error; +} + +void HWCDisplayPrimary::SetMetaDataRefreshRateFlag(bool enable) { + int disable_metadata_dynfps = 0; + + HWCDebugHandler::Get()->GetProperty("persist.metadata_dynfps.disable", &disable_metadata_dynfps); + if (disable_metadata_dynfps) { + return; + } + use_metadata_refresh_rate_ = enable; +} + +void HWCDisplayPrimary::SetQDCMSolidFillInfo(bool enable, uint32_t color) { + solid_fill_enable_ = enable; + solid_fill_color_ = color; +} + +void HWCDisplayPrimary::ToggleCPUHint(bool set) { + if (!cpu_hint_) { + return; + } + + if (set) { + cpu_hint_->Set(); + } else { + cpu_hint_->Reset(); + } +} + +void HWCDisplayPrimary::SetSecureDisplay(bool secure_display_active) { + if (secure_display_active_ != secure_display_active) { + // Skip Prepare and call Flush for null commit + DLOGI("SecureDisplay state changed from %d to %d Needs Flush!!", secure_display_active_, + secure_display_active); + secure_display_active_ = secure_display_active; + skip_prepare_ = true; + } + return; +} + +void HWCDisplayPrimary::ForceRefreshRate(uint32_t refresh_rate) { + if ((refresh_rate && (refresh_rate < min_refresh_rate_ || refresh_rate > max_refresh_rate_)) || + force_refresh_rate_ == refresh_rate) { + // Cannot honor force refresh rate, as its beyond the range or new request is same + return; + } + + force_refresh_rate_ = refresh_rate; + + callbacks_->Refresh(HWC_DISPLAY_PRIMARY); + + return; +} + +uint32_t HWCDisplayPrimary::GetOptimalRefreshRate(bool one_updating_layer) { + if (force_refresh_rate_) { + return force_refresh_rate_; + } else if (handle_idle_timeout_) { + return min_refresh_rate_; + } else if (use_metadata_refresh_rate_ && one_updating_layer && metadata_refresh_rate_) { + return metadata_refresh_rate_; + } + + return max_refresh_rate_; +} + +DisplayError HWCDisplayPrimary::Refresh() { + DisplayError error = kErrorNone; + + callbacks_->Refresh(HWC_DISPLAY_PRIMARY); + handle_idle_timeout_ = true; + + return error; +} + +void HWCDisplayPrimary::SetIdleTimeoutMs(uint32_t timeout_ms) { + display_intf_->SetIdleTimeoutMs(timeout_ms); +} + +static void SetLayerBuffer(const BufferInfo &output_buffer_info, LayerBuffer *output_buffer) { + output_buffer->width = output_buffer_info.buffer_config.width; + output_buffer->height = output_buffer_info.buffer_config.height; + output_buffer->format = output_buffer_info.buffer_config.format; + output_buffer->planes[0].fd = output_buffer_info.alloc_buffer_info.fd; + output_buffer->planes[0].stride = output_buffer_info.alloc_buffer_info.stride; +} + +void HWCDisplayPrimary::HandleFrameOutput() { + if (frame_capture_buffer_queued_) { + HandleFrameCapture(); + } else if (dump_output_to_file_) { + HandleFrameDump(); + } +} + +void HWCDisplayPrimary::HandleFrameCapture() { + if (output_buffer_.release_fence_fd >= 0) { + frame_capture_status_ = sync_wait(output_buffer_.release_fence_fd, 1000); + ::close(output_buffer_.release_fence_fd); + output_buffer_.release_fence_fd = -1; + } + + frame_capture_buffer_queued_ = false; + post_processed_output_ = false; + output_buffer_ = {}; + + uint32_t pending = 0; // Just a temporary to satisfy the API + ControlPartialUpdate(true /* enable */, &pending); +} + +void HWCDisplayPrimary::HandleFrameDump() { + if (dump_frame_count_ && output_buffer_.release_fence_fd >= 0) { + int ret = sync_wait(output_buffer_.release_fence_fd, 1000); + ::close(output_buffer_.release_fence_fd); + output_buffer_.release_fence_fd = -1; + if (ret < 0) { + DLOGE("sync_wait error errno = %d, desc = %s", errno, strerror(errno)); + } else { + DumpOutputBuffer(output_buffer_info_, output_buffer_base_, layer_stack_.retire_fence_fd); + } + } + + if (0 == dump_frame_count_) { + dump_output_to_file_ = false; + // Unmap and Free buffer + if (munmap(output_buffer_base_, output_buffer_info_.alloc_buffer_info.size) != 0) { + DLOGE("unmap failed with err %d", errno); + } + if (buffer_allocator_->FreeBuffer(&output_buffer_info_) != 0) { + DLOGE("FreeBuffer failed"); + } + + post_processed_output_ = false; + output_buffer_ = {}; + output_buffer_info_ = {}; + output_buffer_base_ = nullptr; + + uint32_t pending = 0; // Just a temporary to satisfy the API + ControlPartialUpdate(true /* enable */, &pending); + } +} + +void HWCDisplayPrimary::SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type) { + HWCDisplay::SetFrameDumpConfig(count, bit_mask_layer_type); + dump_output_to_file_ = bit_mask_layer_type & (1 << OUTPUT_LAYER_DUMP); + DLOGI("output_layer_dump_enable %d", dump_output_to_file_); + + if (!count || !dump_output_to_file_) { + return; + } + + // Allocate and map output buffer + output_buffer_info_ = {}; + // Since we dump DSPP output use Panel resolution. + GetPanelResolution(&output_buffer_info_.buffer_config.width, + &output_buffer_info_.buffer_config.height); + output_buffer_info_.buffer_config.format = kFormatRGB888; + output_buffer_info_.buffer_config.buffer_count = 1; + if (buffer_allocator_->AllocateBuffer(&output_buffer_info_) != 0) { + DLOGE("Buffer allocation failed"); + output_buffer_info_ = {}; + return; + } + + void *buffer = mmap(NULL, output_buffer_info_.alloc_buffer_info.size, PROT_READ | PROT_WRITE, + MAP_SHARED, output_buffer_info_.alloc_buffer_info.fd, 0); + + if (buffer == MAP_FAILED) { + DLOGE("mmap failed with err %d", errno); + buffer_allocator_->FreeBuffer(&output_buffer_info_); + output_buffer_info_ = {}; + return; + } + + output_buffer_base_ = buffer; + post_processed_output_ = true; + uint32_t pending = 0; // Just a temporary to satisfy the API + ControlPartialUpdate(false /* enable */, &pending); +} + +int HWCDisplayPrimary::FrameCaptureAsync(const BufferInfo &output_buffer_info, + bool post_processed_output) { + // Note: This function is called in context of a binder thread and a lock is already held + if (output_buffer_info.alloc_buffer_info.fd < 0) { + DLOGE("Invalid fd %d", output_buffer_info.alloc_buffer_info.fd); + return -1; + } + + auto panel_width = 0u; + auto panel_height = 0u; + auto fb_width = 0u; + auto fb_height = 0u; + + GetPanelResolution(&panel_width, &panel_height); + GetFrameBufferResolution(&fb_width, &fb_height); + + if (post_processed_output && (output_buffer_info_.buffer_config.width < panel_width || + output_buffer_info_.buffer_config.height < panel_height)) { + DLOGE("Buffer dimensions should not be less than panel resolution"); + return -1; + } else if (!post_processed_output && (output_buffer_info_.buffer_config.width < fb_width || + output_buffer_info_.buffer_config.height < fb_height)) { + DLOGE("Buffer dimensions should not be less than FB resolution"); + return -1; + } + + SetLayerBuffer(output_buffer_info, &output_buffer_); + post_processed_output_ = post_processed_output; + frame_capture_buffer_queued_ = true; + // Status is only cleared on a new call to dump and remains valid otherwise + frame_capture_status_ = -EAGAIN; + + uint32_t pending = 0; // Just a temporary to satisfy the API + ControlPartialUpdate(false /* enable */, &pending); + + return 0; +} + +} // namespace sdm diff --git a/sdm/libs/hwc2/hwc_display_primary.h b/sdm/libs/hwc2/hwc_display_primary.h new file mode 100644 index 00000000..2779f1b2 --- /dev/null +++ b/sdm/libs/hwc2/hwc_display_primary.h @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2014-2016, The Linux Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __HWC_DISPLAY_PRIMARY_H__ +#define __HWC_DISPLAY_PRIMARY_H__ + +#include "cpuhint.h" +#include "hwc_display.h" + +namespace sdm { + +class HWCDisplayPrimary : public HWCDisplay { + public: + enum { + SET_METADATA_DYN_REFRESH_RATE, + SET_BINDER_DYN_REFRESH_RATE, + SET_DISPLAY_MODE, + SET_QDCM_SOLID_FILL_INFO, + UNSET_QDCM_SOLID_FILL_INFO, + }; + + static int Create(CoreInterface *core_intf, BufferAllocator *buffer_allocator, + HWCCallbacks *callbacks, qService::QService *qservice, + HWCDisplay **hwc_display); + static void Destroy(HWCDisplay *hwc_display); + virtual int Init(); + virtual HWC2::Error Validate(uint32_t *out_num_types, uint32_t *out_num_requests); + virtual HWC2::Error Present(int32_t *out_retire_fence); + virtual int Perform(uint32_t operation, ...); + virtual void SetSecureDisplay(bool secure_display_active); + virtual DisplayError Refresh(); + virtual void SetIdleTimeoutMs(uint32_t timeout_ms); + virtual void SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type); + virtual int FrameCaptureAsync(const BufferInfo &output_buffer_info, bool post_processed); + virtual int GetFrameCaptureStatus() { return frame_capture_status_; } + + private: + HWCDisplayPrimary(CoreInterface *core_intf, BufferAllocator *buffer_allocator, + HWCCallbacks *callbacks, qService::QService *qservice); + void SetMetaDataRefreshRateFlag(bool enable); + virtual DisplayError SetDisplayMode(uint32_t mode); + void ProcessBootAnimCompleted(void); + void SetQDCMSolidFillInfo(bool enable, uint32_t color); + void ToggleCPUHint(bool set); + void ForceRefreshRate(uint32_t refresh_rate); + uint32_t GetOptimalRefreshRate(bool one_updating_layer); + void HandleFrameOutput(); + void HandleFrameCapture(); + void HandleFrameDump(); + + BufferAllocator *buffer_allocator_ = nullptr; + CPUHint *cpu_hint_ = nullptr; + bool handle_idle_timeout_ = false; + + // Primary output buffer configuration + LayerBuffer output_buffer_ = {}; + bool post_processed_output_ = false; + + // Members for 1 frame capture in a client provided buffer + bool frame_capture_buffer_queued_ = false; + int frame_capture_status_ = -EAGAIN; + + // Members for N frame output dump to file + bool dump_output_to_file_ = false; + BufferInfo output_buffer_info_ = {}; + void *output_buffer_base_ = nullptr; +}; + +} // namespace sdm + +#endif // __HWC_DISPLAY_PRIMARY_H__ diff --git a/sdm/libs/hwc2/hwc_display_virtual.cpp b/sdm/libs/hwc2/hwc_display_virtual.cpp new file mode 100644 index 00000000..6c7edfb1 --- /dev/null +++ b/sdm/libs/hwc2/hwc_display_virtual.cpp @@ -0,0 +1,194 @@ +/* +* Copyright (c) 2014 - 2016, The Linux Foundation. All rights reserved. +* +* Redistribution and use in source and binary forms, with or without +* modification, are permitted provided that the following conditions are +* met: +* * Redistributions of source code must retain the above copyright +* notice, this list of conditions and the following disclaimer. +* * Redistributions in binary form must reproduce the above +* copyright notice, this list of conditions and the following +* disclaimer in the documentation and/or other materials provided +* with the distribution. +* * Neither the name of The Linux Foundation nor the names of its +* contributors may be used to endorse or promote products derived +* from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS +* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include +#include +#include +#include +#include + +#include "hwc_display_virtual.h" +#include "hwc_debugger.h" + +#define __CLASS__ "HWCDisplayVirtual" + +namespace sdm { + +int HWCDisplayVirtual::Create(CoreInterface *core_intf, HWCCallbacks *callbacks, uint32_t width, + uint32_t height, HWCDisplay **hwc_display) { + int status = 0; + HWCDisplayVirtual *hwc_display_virtual = new HWCDisplayVirtual(core_intf, callbacks); + uint32_t virtual_width = 0, virtual_height = 0; + + status = hwc_display_virtual->Init(); + if (status) { + delete hwc_display_virtual; + return status; + } + + status = INT32(hwc_display_virtual->SetPowerMode(HWC2::PowerMode::On)); + if (status) { + Destroy(hwc_display_virtual); + return status; + } + + hwc_display_virtual->GetPanelResolution(&virtual_width, &virtual_height); + + status = hwc_display_virtual->SetFrameBufferResolution(width, height); + + if (status) { + Destroy(hwc_display_virtual); + return status; + } + + *hwc_display = static_cast(hwc_display_virtual); + + return 0; +} + +void HWCDisplayVirtual::Destroy(HWCDisplay *hwc_display) { + hwc_display->Deinit(); + delete hwc_display; +} + +HWCDisplayVirtual::HWCDisplayVirtual(CoreInterface *core_intf, HWCCallbacks *callbacks) + : HWCDisplay(core_intf, callbacks, kVirtual, HWC_DISPLAY_VIRTUAL, false, NULL, + DISPLAY_CLASS_VIRTUAL) { +} + +int HWCDisplayVirtual::Init() { + return HWCDisplay::Init(); +} + +int HWCDisplayVirtual::Deinit() { + int status = 0; + + status = HWCDisplay::Deinit(); + + return status; +} + +HWC2::Error HWCDisplayVirtual::Validate(uint32_t *out_num_types, uint32_t *out_num_requests) { + auto status = HWC2::Error::None; + + if (display_paused_) { + MarkLayersForGPUBypass(); + return status; + } + + BuildLayerStack(); + status = PrepareLayerStack(out_num_types, out_num_requests); + return status; +} + +HWC2::Error HWCDisplayVirtual::Present(int32_t *out_retire_fence) { + auto status = HWC2::Error::None; + if (display_paused_) { + DisplayError error = display_intf_->Flush(); + if (error != kErrorNone) { + DLOGE("Flush failed. Error = %d", error); + } + } else { + status = HWCDisplay::CommitLayerStack(); + if (status == HWC2::Error::None) { + if (dump_frame_count_ && !flush_ && dump_output_layer_) { + if (output_handle_ && output_handle_->base) { + BufferInfo buffer_info; + const private_handle_t *output_handle = + reinterpret_cast(output_buffer_->buffer_id); + buffer_info.buffer_config.width = static_cast(output_handle->width); + buffer_info.buffer_config.height = static_cast(output_handle->height); + buffer_info.buffer_config.format = + GetSDMFormat(output_handle->format, output_handle->flags); + buffer_info.alloc_buffer_info.size = static_cast(output_handle->size); + DumpOutputBuffer(buffer_info, reinterpret_cast(output_handle->base), + layer_stack_.retire_fence_fd); + } + } + + status = HWCDisplay::PostCommitLayerStack(out_retire_fence); + } + } + CloseAcquireFds(); + return status; +} + +HWC2::Error HWCDisplayVirtual::SetOutputBuffer(buffer_handle_t buf, int32_t release_fence) { + const private_handle_t *output_handle = static_cast(buf); + + // Fill output buffer parameters (width, height, format, plane information, fence) + output_buffer_->acquire_fence_fd = dup(release_fence); + + if (output_handle) { + output_buffer_->buffer_id = reinterpret_cast(output_handle); + int output_handle_format = output_handle->format; + + if (output_handle_format == HAL_PIXEL_FORMAT_RGBA_8888) { + output_handle_format = HAL_PIXEL_FORMAT_RGBX_8888; + } + + output_buffer_->format = GetSDMFormat(output_handle_format, output_handle->flags); + + if (output_buffer_->format == kFormatInvalid) { + return HWC2::Error::BadParameter; + } + + int output_buffer_width, output_buffer_height; + AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(output_handle, output_buffer_width, + output_buffer_height); + + output_buffer_->width = UINT32(output_buffer_width); + output_buffer_->height = UINT32(output_buffer_height); + // TODO(mkavm): Handle DRC and metadata changes + output_buffer_->flags.secure = 0; + output_buffer_->flags.video = 0; + + // TZ Protected Buffer - L1 + if (output_handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) { + output_buffer_->flags.secure = 1; + } + + // ToDo: Need to extend for non-RGB formats + output_buffer_->planes[0].fd = output_handle->fd; + output_buffer_->planes[0].offset = output_handle->offset; + output_buffer_->planes[0].stride = UINT32(output_handle->width); + } + + layer_stack_.output_buffer = output_buffer_; + return HWC2::Error::None; +} + +void HWCDisplayVirtual::SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type) { + HWCDisplay::SetFrameDumpConfig(count, bit_mask_layer_type); + dump_output_layer_ = ((bit_mask_layer_type & (1 << OUTPUT_LAYER_DUMP)) != 0); + + DLOGI("output_layer_dump_enable %d", dump_output_layer_); +} + +} // namespace sdm diff --git a/sdm/libs/hwc2/hwc_display_virtual.h b/sdm/libs/hwc2/hwc_display_virtual.h new file mode 100644 index 00000000..271d15d9 --- /dev/null +++ b/sdm/libs/hwc2/hwc_display_virtual.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2014-2016, The Linux Foundation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + * Neither the name of The Linux Foundation nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __HWC_DISPLAY_VIRTUAL_H__ +#define __HWC_DISPLAY_VIRTUAL_H__ + +#include +#include +#include "hwc_display.h" + +namespace sdm { + +class HWCDisplayVirtual : public HWCDisplay { + public: + static int Create(CoreInterface *core_intf, HWCCallbacks *callbacks, uint32_t primary_width, + uint32_t primary_height, HWCDisplay **hwc_display); + static void Destroy(HWCDisplay *hwc_display); + virtual int Init(); + virtual int Deinit(); + virtual HWC2::Error Validate(uint32_t *out_num_types, uint32_t *out_num_requests); + virtual HWC2::Error Present(int32_t *out_retire_fence); + virtual void SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type); + HWC2::Error SetOutputBuffer(buffer_handle_t buf, int32_t release_fence); + + private: + HWCDisplayVirtual(CoreInterface *core_intf, HWCCallbacks *callbacks); + + bool dump_output_layer_ = false; + LayerBuffer *output_buffer_ = NULL; + const private_handle_t *output_handle_ = nullptr; +}; + +} // namespace sdm + +#endif // __HWC_DISPLAY_VIRTUAL_H__ diff --git a/sdm/libs/hwc2/hwc_layers.cpp b/sdm/libs/hwc2/hwc_layers.cpp new file mode 100644 index 00000000..9be23178 --- /dev/null +++ b/sdm/libs/hwc2/hwc_layers.cpp @@ -0,0 +1,500 @@ +/* + * Copyright (c) 2014-2016, The Linux Foundation. All rights reserved. + * Not a Contribution. + * + * Copyright 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "hwc_layers.h" +#include +#include +#include + +#define __CLASS__ "HWCLayer" + +namespace sdm { + +bool SortLayersByZ::operator()(const HWCLayer *lhs, const HWCLayer *rhs) { + return lhs->GetZ() < rhs->GetZ(); +} + +std::atomic HWCLayer::next_id_(1); + +// Layer operations +HWCLayer::HWCLayer(hwc2_display_t display_id) : id_(next_id_++), display_id_(display_id) { + layer_ = new Layer(); + layer_->input_buffer = new LayerBuffer(); + // Fences are deferred, so the first time this layer is presented, return -1 + // TODO(user): Verify that fences are properly obtained on suspend/resume + release_fences_.push(-1); +} + +HWCLayer::~HWCLayer() { + // Close any fences left for this layer + while (!release_fences_.empty()) { + close(release_fences_.front()); + release_fences_.pop(); + } + + if (layer_) { + if (layer_->input_buffer) { + delete (layer_->input_buffer); + } + delete layer_; + } +} + +HWC2::Error HWCLayer::SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence) { + if (!buffer) { + DLOGE("Invalid buffer handle: %p on layer: %d", buffer, id_); + return HWC2::Error::BadParameter; + } + + const private_handle_t *handle = static_cast(buffer); + LayerBuffer *layer_buffer = layer_->input_buffer; + layer_buffer->width = UINT32(handle->width); + layer_buffer->height = UINT32(handle->height); + layer_buffer->format = GetSDMFormat(handle->format, handle->flags); + if (SetMetaData(handle, layer_) != kErrorNone) { + return HWC2::Error::BadLayer; + } + + if (handle->bufferType == BUFFER_TYPE_VIDEO) { + layer_buffer->flags.video = true; + } + // TZ Protected Buffer - L1 + if (handle->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) { + layer_buffer->flags.secure = true; + } + if (handle->flags & private_handle_t::PRIV_FLAGS_SECURE_DISPLAY) { + layer_buffer->flags.secure_display = true; + } + + layer_buffer->planes[0].fd = handle->fd; + layer_buffer->planes[0].offset = handle->offset; + layer_buffer->planes[0].stride = UINT32(handle->width); + layer_buffer->acquire_fence_fd = acquire_fence; + layer_buffer->buffer_id = reinterpret_cast(handle); + + return HWC2::Error::None; +} + +HWC2::Error HWCLayer::SetLayerSurfaceDamage(hwc_region_t damage) { + auto num_dirty_rects = damage.numRects; + if (num_dirty_rects > 0) { + for (uint32_t i = 0; i <= damage.numRects; i++) { + LayerRect rect; + SetRect(damage.rects[i], &rect); + layer_->dirty_regions.push_back(rect); + } + } + return HWC2::Error::None; +} + +HWC2::Error HWCLayer::SetLayerBlendMode(HWC2::BlendMode mode) { + switch (mode) { + case HWC2::BlendMode::Coverage: + layer_->blending = kBlendingCoverage; + break; + case HWC2::BlendMode::Premultiplied: + layer_->blending = kBlendingPremultiplied; + break; + case HWC2::BlendMode::None: + layer_->blending = kBlendingOpaque; + break; + default: + return HWC2::Error::BadParameter; + } + geometry_changes_ |= kBlendMode; + return HWC2::Error::None; +} + +HWC2::Error HWCLayer::SetLayerColor(hwc_color_t color) { + layer_->solid_fill_color = GetUint32Color(color); + layer_->input_buffer->format = kFormatARGB8888; + DLOGD("Layer color set to: %u", layer_->solid_fill_color); + DLOGD("[%" PRIu64 "][%" PRIu64 "] Layer color set to %u %" PRIu64, display_id_, id_, + layer_->solid_fill_color); + return HWC2::Error::None; +} + +HWC2::Error HWCLayer::SetLayerCompositionType(HWC2::Composition type) { + layer_->flags = {}; // Reset earlier flags + composition_ = type; // Used to compute changes + switch (type) { + case HWC2::Composition::Client: + layer_->flags.skip = true; + break; + case HWC2::Composition::Device: + // We try and default to this in SDM + break; + case HWC2::Composition::SolidColor: + layer_->flags.solid_fill = true; + break; + case HWC2::Composition::Cursor: + layer_->flags.cursor = true; + break; + case HWC2::Composition::Invalid: + return HWC2::Error::BadParameter; + default: + return HWC2::Error::Unsupported; + } + // TODO(user): Check if this should be set here or somewhere else + layer_->flags.updating = true; + return HWC2::Error::None; +} + +HWC2::Error HWCLayer::SetLayerDataspace(int32_t dataspace) { + // TODO(user): Implement later + geometry_changes_ |= kDataspace; + return HWC2::Error::None; +} + +HWC2::Error HWCLayer::SetLayerDisplayFrame(hwc_rect_t frame) { + SetRect(frame, &layer_->dst_rect); + geometry_changes_ |= kDisplayFrame; + return HWC2::Error::None; +} + +HWC2::Error HWCLayer::SetLayerPlaneAlpha(float alpha) { + // Conversion of float alpha in range 0.0 to 1.0 similar to the HWC Adapter + layer_->plane_alpha = static_cast(255.0f * alpha + 0.5f); + geometry_changes_ |= kPlaneAlpha; + return HWC2::Error::None; +} + +HWC2::Error HWCLayer::SetLayerSourceCrop(hwc_frect_t crop) { + SetRect(crop, &layer_->src_rect); + geometry_changes_ |= kSourceCrop; + return HWC2::Error::None; +} + +HWC2::Error HWCLayer::SetLayerTransform(HWC2::Transform transform) { + switch (transform) { + case HWC2::Transform::FlipH: + layer_->transform.flip_horizontal = true; + break; + case HWC2::Transform::FlipV: + layer_->transform.flip_vertical = true; + break; + case HWC2::Transform::Rotate90: + layer_->transform.rotation = 90.0f; + break; + case HWC2::Transform::Rotate180: + layer_->transform.rotation = 180.0f; + break; + case HWC2::Transform::Rotate270: + layer_->transform.rotation = 270.0f; + break; + case HWC2::Transform::FlipHRotate90: + layer_->transform.rotation = 90.0f; + layer_->transform.flip_horizontal = true; + break; + case HWC2::Transform::FlipVRotate90: + layer_->transform.rotation = 90.0f; + layer_->transform.flip_vertical = true; + break; + default: + layer_->transform.rotation = 0.0f; + layer_->transform.flip_horizontal = false; + layer_->transform.flip_vertical = false; + } + geometry_changes_ |= kTransform; + return HWC2::Error::None; +} + +HWC2::Error HWCLayer::SetLayerVisibleRegion(hwc_region_t visible) { + auto num_dirty_rects = visible.numRects; + if (num_dirty_rects > 0) { + for (uint32_t i = 0; i <= visible.numRects; i++) { + LayerRect rect; + SetRect(visible.rects[i], &rect); + layer_->visible_regions.push_back(rect); + } + } + + return HWC2::Error::None; +} + +HWC2::Error HWCLayer::SetLayerZOrder(uint32_t z) { + z_ = z; + geometry_changes_ |= kZOrder; + return HWC2::Error::None; +} + +void HWCLayer::SetRect(const hwc_rect_t &source, LayerRect *target) { + target->left = FLOAT(source.left); + target->top = FLOAT(source.top); + target->right = FLOAT(source.right); + target->bottom = FLOAT(source.bottom); +} + +void HWCLayer::SetRect(const hwc_frect_t &source, LayerRect *target) { + // Recommended way of rounding as in hwcomposer2.h - SetLayerSourceCrop + target->left = std::ceil(source.left); + target->top = std::ceil(source.top); + target->right = std::floor(source.right); + target->bottom = std::floor(source.bottom); +} + +uint32_t HWCLayer::GetUint32Color(const hwc_color_t &source) { + // Returns 32 bit ARGB + uint32_t a = UINT32(source.a) << 24; + uint32_t r = UINT32(source.r) << 16; + uint32_t g = UINT32(source.g) << 8; + uint32_t b = UINT32(source.b); + uint32_t color = a & r & g & b; + return color; +} + +LayerBufferFormat HWCLayer::GetSDMFormat(const int32_t &source, const int flags) { + LayerBufferFormat format = kFormatInvalid; + if (flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) { + switch (source) { + case HAL_PIXEL_FORMAT_RGBA_8888: + format = kFormatRGBA8888Ubwc; + break; + case HAL_PIXEL_FORMAT_RGBX_8888: + format = kFormatRGBX8888Ubwc; + break; + case HAL_PIXEL_FORMAT_BGR_565: + format = kFormatBGR565Ubwc; + break; + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC: + case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: + format = kFormatYCbCr420SPVenusUbwc; + break; + default: + DLOGE("Unsupported format type for UBWC %d", source); + return kFormatInvalid; + } + return format; + } + + switch (source) { + case HAL_PIXEL_FORMAT_RGBA_8888: + format = kFormatRGBA8888; + break; + case HAL_PIXEL_FORMAT_RGBA_5551: + format = kFormatRGBA5551; + break; + case HAL_PIXEL_FORMAT_RGBA_4444: + format = kFormatRGBA4444; + break; + case HAL_PIXEL_FORMAT_BGRA_8888: + format = kFormatBGRA8888; + break; + case HAL_PIXEL_FORMAT_RGBX_8888: + format = kFormatRGBX8888; + break; + case HAL_PIXEL_FORMAT_BGRX_8888: + format = kFormatBGRX8888; + break; + case HAL_PIXEL_FORMAT_RGB_888: + format = kFormatRGB888; + break; + case HAL_PIXEL_FORMAT_RGB_565: + format = kFormatRGB565; + break; + case HAL_PIXEL_FORMAT_BGR_565: + format = kFormatBGR565; + break; + case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: + format = kFormatYCbCr420SemiPlanarVenus; + break; + case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS: + format = kFormatYCrCb420SemiPlanarVenus; + break; + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC: + format = kFormatYCbCr420SPVenusUbwc; + break; + case HAL_PIXEL_FORMAT_YV12: + format = kFormatYCrCb420PlanarStride16; + break; + case HAL_PIXEL_FORMAT_YCrCb_420_SP: + format = kFormatYCrCb420SemiPlanar; + break; + case HAL_PIXEL_FORMAT_YCbCr_420_SP: + format = kFormatYCbCr420SemiPlanar; + break; + case HAL_PIXEL_FORMAT_YCbCr_422_SP: + format = kFormatYCbCr422H2V1SemiPlanar; + break; + case HAL_PIXEL_FORMAT_YCbCr_422_I: + format = kFormatYCbCr422H2V1Packed; + break; + default: + DLOGW("Unsupported format type = %d", source); + return kFormatInvalid; + } + + return format; +} + +LayerBufferS3DFormat HWCLayer::GetS3DFormat(uint32_t s3d_format) { + LayerBufferS3DFormat sdm_s3d_format = kS3dFormatNone; + switch (s3d_format) { + case HAL_NO_3D: + sdm_s3d_format = kS3dFormatNone; + break; + case HAL_3D_SIDE_BY_SIDE_L_R: + sdm_s3d_format = kS3dFormatLeftRight; + break; + case HAL_3D_SIDE_BY_SIDE_R_L: + sdm_s3d_format = kS3dFormatRightLeft; + break; + case HAL_3D_TOP_BOTTOM: + sdm_s3d_format = kS3dFormatTopBottom; + break; + default: + DLOGW("Invalid S3D format %d", s3d_format); + } + return sdm_s3d_format; +} + +DisplayError HWCLayer::SetMetaData(const private_handle_t *pvt_handle, Layer *layer) { + const MetaData_t *meta_data = reinterpret_cast(pvt_handle->base_metadata); + LayerBuffer *layer_buffer = layer->input_buffer; + + if (!meta_data) { + return kErrorNone; + } + + if (meta_data->operation & UPDATE_COLOR_SPACE) { + if (SetCSC(meta_data->colorSpace, &layer->csc) != kErrorNone) { + return kErrorNotSupported; + } + } + + if (meta_data->operation & SET_IGC) { + if (SetIGC(meta_data->igc, &layer->igc) != kErrorNone) { + return kErrorNotSupported; + } + } + + if (meta_data->operation & UPDATE_REFRESH_RATE) { + layer->frame_rate = RoundToStandardFPS(meta_data->refreshrate); + } + + if ((meta_data->operation & PP_PARAM_INTERLACED) && meta_data->interlaced) { + layer_buffer->flags.interlace = true; + } + + if (meta_data->operation & LINEAR_FORMAT) { + layer_buffer->format = GetSDMFormat(INT32(meta_data->linearFormat), 0); + } + + if (meta_data->operation & UPDATE_BUFFER_GEOMETRY) { + int actual_width = pvt_handle->width; + int actual_height = pvt_handle->height; + AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(pvt_handle, actual_width, actual_height); + layer_buffer->width = UINT32(actual_width); + layer_buffer->height = UINT32(actual_height); + } + + if (meta_data->operation & S3D_FORMAT) { + layer_buffer->s3d_format = GetS3DFormat(meta_data->s3dFormat); + } + + return kErrorNone; +} + +DisplayError HWCLayer::SetCSC(ColorSpace_t source, LayerCSC *target) { + switch (source) { + case ITU_R_601: + *target = kCSCLimitedRange601; + break; + case ITU_R_601_FR: + *target = kCSCFullRange601; + break; + case ITU_R_709: + *target = kCSCLimitedRange709; + break; + default: + DLOGE("Unsupported CSC: %d", source); + return kErrorNotSupported; + } + + return kErrorNone; +} + +DisplayError HWCLayer::SetIGC(IGC_t source, LayerIGC *target) { + switch (source) { + case IGC_NotSpecified: + *target = kIGCNotSpecified; + break; + case IGC_sRGB: + *target = kIGCsRGB; + break; + default: + DLOGE("Unsupported IGC: %d", source); + return kErrorNotSupported; + } + + return kErrorNone; +} + +uint32_t HWCLayer::RoundToStandardFPS(uint32_t fps) { + static const uint32_t standard_fps[4] = {30, 24, 48, 60}; + + int count = INT(sizeof(standard_fps) / sizeof(standard_fps[0])); + for (int i = 0; i < count; i++) { + if ((standard_fps[i] - fps) < 2) { + // Most likely used for video, the fps can fluctuate + // Ex: b/w 29 and 30 for 30 fps clip + return standard_fps[i]; + } + } + + return fps; +} + +void HWCLayer::SetComposition(const LayerComposition &source) { + auto composition = HWC2::Composition::Invalid; + switch (source) { + case kCompositionGPU: + composition = HWC2::Composition::Client; + break; + case kCompositionHWCursor: + composition = HWC2::Composition::Cursor; + break; + default: + composition = HWC2::Composition::Device; + break; + } + // Update solid fill composition + if (layer_->composition == kCompositionSDE && layer_->flags.solid_fill != 0) { + composition = HWC2::Composition::SolidColor; + } + if (composition != composition_) { + // Composition changed for this layer + composition_changed_ = true; + composition_ = composition; + } +} +void HWCLayer::PushReleaseFence(int32_t fence) { + release_fences_.push(fence); +} +int32_t HWCLayer::PopReleaseFence(void) { + if (release_fences_.empty()) + return -1; + auto fence = release_fences_.front(); + release_fences_.pop(); + return fence; +} + +} // namespace sdm diff --git a/sdm/libs/hwc2/hwc_layers.h b/sdm/libs/hwc2/hwc_layers.h new file mode 100644 index 00000000..de3b7bbd --- /dev/null +++ b/sdm/libs/hwc2/hwc_layers.h @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2014-2016, The Linux Foundation. All rights reserved. + * Not a Contribution. + * + * Copyright 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __HWC_LAYERS_H__ +#define __HWC_LAYERS_H__ + +/* This class translates HWC2 Layer functions to the SDM LayerStack + */ + +#include +#include +#include +#define HWC2_INCLUDE_STRINGIFICATION +#define HWC2_USE_CPP11 +#include +#undef HWC2_INCLUDE_STRINGIFICATION +#undef HWC2_USE_CPP11 +#include +#include +#include + +namespace sdm { + +enum GeometryChanges { + kNone = 0x00, + kBlendMode = 0x01, + kDataspace = 0x02, + kDisplayFrame = 0x04, + kPlaneAlpha = 0x08, + kSourceCrop = 0x0A, + kTransform = 0x10, + kZOrder = 0x12, + kAdded = 0x14, + kRemoved = 0x18, +}; + +class HWCLayer { + public: + explicit HWCLayer(hwc2_display_t display_id); + ~HWCLayer(); + uint32_t GetZ() const { return z_; } + hwc2_layer_t GetId() const { return id_; } + Layer *GetSDMLayer() { return layer_; } + + HWC2::Error SetLayerBlendMode(HWC2::BlendMode mode); + HWC2::Error SetLayerBuffer(buffer_handle_t buffer, int32_t acquire_fence); + HWC2::Error SetLayerColor(hwc_color_t color); + HWC2::Error SetLayerCompositionType(HWC2::Composition type); + HWC2::Error SetLayerDataspace(int32_t dataspace); + HWC2::Error SetLayerDisplayFrame(hwc_rect_t frame); + HWC2::Error SetLayerPlaneAlpha(float alpha); + HWC2::Error SetLayerSourceCrop(hwc_frect_t crop); + HWC2::Error SetLayerSurfaceDamage(hwc_region_t damage); + HWC2::Error SetLayerTransform(HWC2::Transform transform); + HWC2::Error SetLayerVisibleRegion(hwc_region_t visible); + HWC2::Error SetLayerZOrder(uint32_t z); + void SetComposition(const LayerComposition &source); + bool CompositionChanged(void) { return composition_changed_; } + HWC2::Composition GetCompositionType() { return composition_; } + uint32_t GetGeometryChanges() { return geometry_changes_; } + void ResetGeometryChanges() { geometry_changes_ = GeometryChanges::kNone; } + void PushReleaseFence(int32_t fence); + int32_t PopReleaseFence(void); + + private: + Layer *layer_ = nullptr; + uint32_t z_ = 0; + const hwc2_layer_t id_; + const hwc2_display_t display_id_; + static std::atomic next_id_; + std::queue release_fences_; + + HWC2::Composition composition_ = HWC2::Composition::Device; + bool composition_changed_ = false; + uint32_t geometry_changes_ = GeometryChanges::kNone; + + void SetRect(const hwc_rect_t &source, LayerRect *target); + void SetRect(const hwc_frect_t &source, LayerRect *target); + uint32_t GetUint32Color(const hwc_color_t &source); + LayerBufferFormat GetSDMFormat(const int32_t &source, const int flags); + LayerBufferS3DFormat GetS3DFormat(uint32_t s3d_format); + DisplayError SetMetaData(const private_handle_t *pvt_handle, Layer *layer); + DisplayError SetCSC(ColorSpace_t source, LayerCSC *target); + DisplayError SetIGC(IGC_t source, LayerIGC *target); + uint32_t RoundToStandardFPS(uint32_t fps); +}; + +struct SortLayersByZ { + bool operator()(const HWCLayer *lhs, const HWCLayer *rhs); +}; + +} // namespace sdm +#endif // __HWC_LAYERS_H__ diff --git a/sdm/libs/hwc2/hwc_session.cpp b/sdm/libs/hwc2/hwc_session.cpp new file mode 100644 index 00000000..a7d20426 --- /dev/null +++ b/sdm/libs/hwc2/hwc_session.cpp @@ -0,0 +1,1487 @@ +/* + * Copyright (c) 2014-2016, The Linux Foundation. All rights reserved. + * Not a Contribution. + * + * Copyright 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "hwc_buffer_allocator.h" +#include "hwc_buffer_sync_handler.h" +#include "hwc_session.h" +#include "hwc_debugger.h" +#include "hwc_display_primary.h" +#include "hwc_display_virtual.h" + +#define __CLASS__ "HWCSession" + +#define HWC_UEVENT_SWITCH_HDMI "change@/devices/virtual/switch/hdmi" +#define HWC_UEVENT_GRAPHICS_FB0 "change@/devices/virtual/graphics/fb0" + +static sdm::HWCSession::HWCModuleMethods g_hwc_module_methods; + +hwc_module_t HAL_MODULE_INFO_SYM = { + .common = { + .tag = HARDWARE_MODULE_TAG, + .version_major = 3, + .version_minor = 0, + .id = HWC_HARDWARE_MODULE_ID, + .name = "QTI Hardware Composer Module", + .author = "CodeAurora Forum", + .methods = &g_hwc_module_methods, + .dso = 0, + .reserved = {0}, + } +}; + +namespace sdm { +Locker HWCSession::locker_; + +HWCSession::HWCSession(const hw_module_t *module) { + hwc2_device_t::common.tag = HARDWARE_DEVICE_TAG; + hwc2_device_t::common.version = HWC_DEVICE_API_VERSION_2_0; + hwc2_device_t::common.module = const_cast(module); + hwc2_device_t::common.close = Close; + hwc2_device_t::getCapabilities = GetCapabilities; + hwc2_device_t::getFunction = GetFunction; +} + +int HWCSession::Init() { + int status = -EINVAL; + const char *qservice_name = "display.qservice"; + + // Start QService and connect to it. + qService::QService::init(); + android::sp iqservice = android::interface_cast( + android::defaultServiceManager()->getService(android::String16(qservice_name))); + + if (iqservice.get()) { + iqservice->connect(android::sp(this)); + qservice_ = reinterpret_cast(iqservice.get()); + } else { + DLOGE("Failed to acquire %s", qservice_name); + return -EINVAL; + } + + buffer_allocator_ = new HWCBufferAllocator(); + if (buffer_allocator_ == NULL) { + DLOGE("Display core initialization failed due to no memory"); + return -ENOMEM; + } + + buffer_sync_handler_ = new HWCBufferSyncHandler(); + if (buffer_sync_handler_ == NULL) { + DLOGE("Display core initialization failed due to no memory"); + return -ENOMEM; + } + + DisplayError error = CoreInterface::CreateCore(HWCDebugHandler::Get(), buffer_allocator_, + buffer_sync_handler_, &core_intf_); + if (error != kErrorNone) { + DLOGE("Display core initialization failed. Error = %d", error); + return -EINVAL; + } + + // Read which display is first, and create it and store it in primary slot + // TODO(user): This will need to be redone for HWC2 - right now we validate only + // the primary physical path + HWDisplayInterfaceInfo hw_disp_info; + error = core_intf_->GetFirstDisplayInterfaceType(&hw_disp_info); + if (error == kErrorNone && hw_disp_info.type == kHDMI && hw_disp_info.is_connected) { + // HDMI is primary display. If already connected, then create it and store in + // primary display slot. If not connected, create a NULL display for now. + status = HWCDisplayExternal::Create(core_intf_, &callbacks_, qservice_, + &hwc_display_[HWC_DISPLAY_PRIMARY]); + } else { + // Create and power on primary display + status = HWCDisplayPrimary::Create(core_intf_, buffer_allocator_, &callbacks_, qservice_, + &hwc_display_[HWC_DISPLAY_PRIMARY]); + } + + if (status) { + CoreInterface::DestroyCore(); + return status; + } + + color_mgr_ = HWCColorManager::CreateColorManager(); + if (!color_mgr_) { + DLOGW("Failed to load HWCColorManager."); + } + + if (pthread_create(&uevent_thread_, NULL, &HWCUeventThread, this) < 0) { + DLOGE("Failed to start = %s, error = %s", uevent_thread_name_, strerror(errno)); + HWCDisplayPrimary::Destroy(hwc_display_[HWC_DISPLAY_PRIMARY]); + hwc_display_[HWC_DISPLAY_PRIMARY] = 0; + CoreInterface::DestroyCore(); + return -errno; + } + + return 0; +} + +int HWCSession::Deinit() { + HWCDisplayPrimary::Destroy(hwc_display_[HWC_DISPLAY_PRIMARY]); + hwc_display_[HWC_DISPLAY_PRIMARY] = 0; + if (color_mgr_) { + color_mgr_->DestroyColorManager(); + } + uevent_thread_exit_ = true; + pthread_join(uevent_thread_, NULL); + + DisplayError error = CoreInterface::DestroyCore(); + if (error != kErrorNone) { + DLOGE("Display core de-initialization failed. Error = %d", error); + } + + return 0; +} + +int HWCSession::Open(const hw_module_t *module, const char *name, hw_device_t **device) { + SEQUENCE_WAIT_SCOPE_LOCK(locker_); + + if (!module || !name || !device) { + DLOGE("Invalid parameters."); + return -EINVAL; + } + + if (!strcmp(name, HWC_HARDWARE_COMPOSER)) { + HWCSession *hwc_session = new HWCSession(module); + if (!hwc_session) { + return -ENOMEM; + } + + int status = hwc_session->Init(); + if (status != 0) { + delete hwc_session; + return status; + } + + hwc2_device_t *composer_device = hwc_session; + *device = reinterpret_cast(composer_device); + } + + return 0; +} + +int HWCSession::Close(hw_device_t *device) { + SEQUENCE_WAIT_SCOPE_LOCK(locker_); + + if (!device) { + return -EINVAL; + } + + hwc2_device_t *composer_device = reinterpret_cast(device); + HWCSession *hwc_session = static_cast(composer_device); + + hwc_session->Deinit(); + delete hwc_session; + + return 0; +} + +void HWCSession::GetCapabilities(struct hwc2_device *device, uint32_t *outCount, + int32_t *outCapabilities) { + if (outCapabilities == NULL) { + *outCount = 0; + } +} + +template +static hwc2_function_pointer_t AsFP(T function) { + static_assert(std::is_same::value, "Incompatible function pointer"); + return reinterpret_cast(function); +} + +// HWC2 functions returned in GetFunction +// Defined in the same order as in the HWC2 header + +static int32_t AcceptDisplayChanges(hwc2_device_t *device, hwc2_display_t display) { + return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::AcceptDisplayChanges); +} + +static int32_t CreateLayer(hwc2_device_t *device, hwc2_display_t display, + hwc2_layer_t *out_layer_id) { + return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::CreateLayer, out_layer_id); +} + +int32_t HWCSession::CreateVirtualDisplay(hwc2_device_t *device, uint32_t width, uint32_t height, + hwc2_display_t *out_display_id) { + if (!device) { + return HWC2_ERROR_BAD_DISPLAY; + } + + HWCSession *hwc_session = static_cast(device); + auto status = hwc_session->CreateVirtualDisplayObject(width, height); + if (status == HWC2::Error::None) + *out_display_id = HWC_DISPLAY_VIRTUAL; + return INT32(status); +} + +static int32_t DestroyLayer(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer) { + return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::DestroyLayer, layer); +} + +int32_t HWCSession::DestroyVirtualDisplay(hwc2_device_t *device, hwc2_display_t display) { + if (!device) { + return HWC2_ERROR_BAD_DISPLAY; + } + + auto *hwc_session = static_cast(device); + + if (hwc_session->hwc_display_[display]) { + HWCDisplayVirtual::Destroy(hwc_session->hwc_display_[display]); + return HWC2_ERROR_NONE; + } else { + return HWC2_ERROR_BAD_DISPLAY; + } +} + +static void Dump(hwc2_device_t *device, uint32_t *out_size, char *out_buffer) { + if (!device) { + return; + } + + if (out_buffer == nullptr) { + *out_size = 4096; // TODO(user): Adjust required dump size + } else { + DumpInterface::GetDump(out_buffer, 4096); // TODO(user): Fix this workaround + *out_size = sizeof(out_buffer); + } +} + +static int32_t GetActiveConfig(hwc2_device_t *device, hwc2_display_t display, + hwc2_config_t *out_config) { + return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::GetActiveConfig, out_config); +} + +static int32_t GetChangedCompositionTypes(hwc2_device_t *device, hwc2_display_t display, + uint32_t *out_num_elements, hwc2_layer_t *out_layers, + int32_t *out_types) { + return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::GetChangedCompositionTypes, + out_num_elements, out_layers, out_types); +} + +static int32_t GetClientTargetSupport(hwc2_device_t *device, hwc2_display_t display, uint32_t width, + uint32_t height, int32_t format, int32_t dataspace) { + return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::GetClientTargetSupport, + width, height, format, dataspace); +} + +// TODO(user): GetColorModes + +static int32_t GetDisplayAttribute(hwc2_device_t *device, hwc2_display_t display, + hwc2_config_t config, int32_t int_attribute, + int32_t *out_value) { + auto attribute = static_cast(int_attribute); + return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::GetDisplayAttribute, config, + attribute, out_value); +} + +static int32_t GetDisplayConfigs(hwc2_device_t *device, hwc2_display_t display, + uint32_t *out_num_configs, hwc2_config_t *out_configs) { + return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::GetDisplayConfigs, + out_num_configs, out_configs); +} + +static int32_t GetDisplayName(hwc2_device_t *device, hwc2_display_t display, uint32_t *out_size, + char *out_name) { + return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::GetDisplayName, out_size, + out_name); +} + +static int32_t GetDisplayRequests(hwc2_device_t *device, hwc2_display_t display, + int32_t *out_display_requests, uint32_t *out_num_elements, + hwc2_layer_t *out_layers, int32_t *out_layer_requests) { + return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::GetDisplayRequests, + out_display_requests, out_num_elements, out_layers, + out_layer_requests); +} + +static int32_t GetDisplayType(hwc2_device_t *device, hwc2_display_t display, int32_t *out_type) { + return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::GetDisplayType, out_type); +} + +static int32_t GetDozeSupport(hwc2_device_t *device, hwc2_display_t display, int32_t *out_support) { + // TODO(user): Check if it is an HDMI as primary display and disable support for it + if (display == HWC_DISPLAY_PRIMARY) { + *out_support = 1; + } else { + *out_support = 0; + } + return HWC2_ERROR_NONE; +} + +static uint32_t GetMaxVirtualDisplayCount(hwc2_device_t *device) { + return 1; +} + +static int32_t GetReleaseFences(hwc2_device_t *device, hwc2_display_t display, + uint32_t *out_num_elements, hwc2_layer_t *out_layers, + int32_t *out_fences) { + return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::GetReleaseFences, + out_num_elements, out_layers, out_fences); +} + +int32_t HWCSession::PresentDisplay(hwc2_device_t *device, hwc2_display_t display, + int32_t *out_retire_fence) { + HWCSession *hwc_session = static_cast(device); + DTRACE_SCOPED(); + SEQUENCE_EXIT_SCOPE_LOCK(locker_); + if (!device) { + return HWC2_ERROR_BAD_DISPLAY; + } + // TODO(user): Handle solid fill layers + auto status = HWC2::Error::BadDisplay; + // TODO(user): Handle virtual display/HDMI concurrency + + if (hwc_session->hwc_display_[display]) { + status = hwc_session->hwc_display_[display]->Present(out_retire_fence); + // This is only indicative of how many times SurfaceFlinger posts + // frames to the display. + CALC_FPS(); + } + + return INT32(status); +} + +int32_t HWCSession::RegisterCallback(hwc2_device_t *device, int32_t descriptor, + hwc2_callback_data_t callback_data, + hwc2_function_pointer_t pointer) { + HWCSession *hwc_session = static_cast(device); + if (!device) { + return HWC2_ERROR_BAD_DISPLAY; + } + auto desc = static_cast(descriptor); + auto error = hwc_session->callbacks_.Register(desc, callback_data, pointer); + DLOGD("Registering callback: %s", to_string(desc).c_str()); + // TODO(user): The hotplug should only be called when the HOTPLUG callback is registered + // However, this causes SurfaceFlinger to behave weirdly - investigate further. + hwc_session->callbacks_.Hotplug(HWC_DISPLAY_PRIMARY, HWC2::Connection::Connected); + return INT32(error); +} + +static int32_t SetActiveConfig(hwc2_device_t *device, hwc2_display_t display, + hwc2_config_t config) { + return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::SetActiveConfig, config); +} + +static int32_t SetClientTarget(hwc2_device_t *device, hwc2_display_t display, + buffer_handle_t target, int32_t acquire_fence, int32_t dataspace) { + return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::SetClientTarget, target, + acquire_fence, dataspace); +} + +// TODO(user): SetColorMode, SetColorTransform + +static int32_t SetCursorPosition(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer, + int32_t x, int32_t y) { + return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::SetCursorPosition, layer, x, + y); +} + +static int32_t SetLayerBlendMode(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer, + int32_t int_mode) { + auto mode = static_cast(int_mode); + return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerBlendMode, mode); +} + +static int32_t SetLayerBuffer(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer, + buffer_handle_t buffer, int32_t acquire_fence) { + return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerBuffer, buffer, + acquire_fence); +} + +static int32_t SetLayerColor(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer, + hwc_color_t color) { + return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerColor, color); +} + +static int32_t SetLayerCompositionType(hwc2_device_t *device, hwc2_display_t display, + hwc2_layer_t layer, int32_t int_type) { + auto type = static_cast(int_type); + return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerCompositionType, + type); +} + +static int32_t SetLayerDataspace(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer, + int32_t dataspace) { + return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerDataspace, + dataspace); +} + +static int32_t SetLayerDisplayFrame(hwc2_device_t *device, hwc2_display_t display, + hwc2_layer_t layer, hwc_rect_t frame) { + return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerDisplayFrame, + frame); +} + +static int32_t SetLayerPlaneAlpha(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer, + float alpha) { + return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerPlaneAlpha, + alpha); +} + +static int32_t SetLayerSourceCrop(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer, + hwc_frect_t crop) { + return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerSourceCrop, crop); +} + +static int32_t SetLayerSurfaceDamage(hwc2_device_t *device, hwc2_display_t display, + hwc2_layer_t layer, hwc_region_t damage) { + return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerSurfaceDamage, + damage); +} + +static int32_t SetLayerTransform(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer, + int32_t int_transform) { + auto transform = static_cast(int_transform); + return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerTransform, + transform); +} + +static int32_t SetLayerVisibleRegion(hwc2_device_t *device, hwc2_display_t display, + hwc2_layer_t layer, hwc_region_t visible) { + return HWCSession::CallLayerFunction(device, display, layer, &HWCLayer::SetLayerVisibleRegion, + visible); +} + +static int32_t SetLayerZOrder(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer, + uint32_t z) { + return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::SetLayerZOrder, layer, z); +} + +int32_t HWCSession::SetOutputBuffer(hwc2_device_t *device, hwc2_display_t display, + buffer_handle_t buffer, int32_t releaseFence) { + if (!device) { + return HWC2_ERROR_BAD_DISPLAY; + } + + auto *hwc_session = static_cast(device); + if (display == HWC_DISPLAY_VIRTUAL && hwc_session->hwc_display_[display]) { + auto vds = reinterpret_cast(hwc_session->hwc_display_[display]); + auto status = vds->SetOutputBuffer(buffer, releaseFence); + return INT32(status); + } else { + return HWC2_ERROR_BAD_DISPLAY; + } +} + +static int32_t SetPowerMode(hwc2_device_t *device, hwc2_display_t display, int32_t int_mode) { + auto mode = static_cast(int_mode); + return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::SetPowerMode, mode); +} + +static int32_t SetVsyncEnabled(hwc2_device_t *device, hwc2_display_t display, int32_t int_enabled) { + auto enabled = static_cast(int_enabled); + return HWCSession::CallDisplayFunction(device, display, &HWCDisplay::SetVsyncEnabled, enabled); +} + +int32_t HWCSession::ValidateDisplay(hwc2_device_t *device, hwc2_display_t display, + uint32_t *out_num_types, uint32_t *out_num_requests) { + DTRACE_SCOPED(); + HWCSession *hwc_session = static_cast(device); + if (!device) { + return HWC2_ERROR_BAD_DISPLAY; + } + + // TODO(user): Handle secure session, handle QDCM solid fill + // Handle external_pending_connect_ in CreateVirtualDisplay + auto status = HWC2::Error::BadDisplay; + if (hwc_session->hwc_display_[display]) { + SEQUENCE_ENTRY_SCOPE_LOCK(locker_); + if (display == HWC_DISPLAY_PRIMARY) { + // TODO(user): This can be moved to HWCDisplayPrimary + if (hwc_session->reset_panel_) { + DLOGW("panel is in bad state, resetting the panel"); + hwc_session->ResetPanel(); + } + + if (hwc_session->need_invalidate_) { + hwc_session->callbacks_.Refresh(display); + } + } + + status = hwc_session->hwc_display_[display]->Validate(out_num_types, out_num_requests); + } + return INT32(status); +} + +hwc2_function_pointer_t HWCSession::GetFunction(struct hwc2_device *device, + int32_t int_descriptor) { + auto descriptor = static_cast(int_descriptor); + + switch (descriptor) { + case HWC2::FunctionDescriptor::AcceptDisplayChanges: + return AsFP(AcceptDisplayChanges); + case HWC2::FunctionDescriptor::CreateLayer: + return AsFP(CreateLayer); + case HWC2::FunctionDescriptor::CreateVirtualDisplay: + return AsFP(HWCSession::CreateVirtualDisplay); + case HWC2::FunctionDescriptor::DestroyLayer: + return AsFP(DestroyLayer); + case HWC2::FunctionDescriptor::DestroyVirtualDisplay: + return AsFP(HWCSession::DestroyVirtualDisplay); + case HWC2::FunctionDescriptor::Dump: + return AsFP(Dump); + case HWC2::FunctionDescriptor::GetActiveConfig: + return AsFP(GetActiveConfig); + case HWC2::FunctionDescriptor::GetChangedCompositionTypes: + return AsFP(GetChangedCompositionTypes); + case HWC2::FunctionDescriptor::GetClientTargetSupport: + return AsFP(GetClientTargetSupport); + // case HWC2::FunctionDescriptor::GetColorModes: + // TODO(user): Support later + case HWC2::FunctionDescriptor::GetDisplayAttribute: + return AsFP(GetDisplayAttribute); + case HWC2::FunctionDescriptor::GetDisplayConfigs: + return AsFP(GetDisplayConfigs); + case HWC2::FunctionDescriptor::GetDisplayName: + return AsFP(GetDisplayName); + case HWC2::FunctionDescriptor::GetDisplayRequests: + return AsFP(GetDisplayRequests); + case HWC2::FunctionDescriptor::GetDisplayType: + return AsFP(GetDisplayType); + case HWC2::FunctionDescriptor::GetDozeSupport: + return AsFP(GetDozeSupport); + case HWC2::FunctionDescriptor::GetMaxVirtualDisplayCount: + return AsFP(GetMaxVirtualDisplayCount); + case HWC2::FunctionDescriptor::GetReleaseFences: + return AsFP(GetReleaseFences); + case HWC2::FunctionDescriptor::PresentDisplay: + return AsFP(PresentDisplay); + case HWC2::FunctionDescriptor::RegisterCallback: + return AsFP(RegisterCallback); + case HWC2::FunctionDescriptor::SetActiveConfig: + return AsFP(SetActiveConfig); + case HWC2::FunctionDescriptor::SetClientTarget: + return AsFP(SetClientTarget); + // TODO(user): Support later + // case HWC2::FunctionDescriptor::SetColorMode: + // case HWC2::FunctionDescriptor::SetColorTransform: + // break; + case HWC2::FunctionDescriptor::SetCursorPosition: + return AsFP(SetCursorPosition); + case HWC2::FunctionDescriptor::SetLayerBlendMode: + return AsFP(SetLayerBlendMode); + case HWC2::FunctionDescriptor::SetLayerBuffer: + return AsFP(SetLayerBuffer); + case HWC2::FunctionDescriptor::SetLayerColor: + return AsFP(SetLayerColor); + case HWC2::FunctionDescriptor::SetLayerCompositionType: + return AsFP(SetLayerCompositionType); + case HWC2::FunctionDescriptor::SetLayerDataspace: + return AsFP(SetLayerDataspace); + case HWC2::FunctionDescriptor::SetLayerDisplayFrame: + return AsFP(SetLayerDisplayFrame); + case HWC2::FunctionDescriptor::SetLayerPlaneAlpha: + return AsFP(SetLayerPlaneAlpha); + // Sideband stream is not supported + // case HWC2::FunctionDescriptor::SetLayerSidebandStream: + case HWC2::FunctionDescriptor::SetLayerSourceCrop: + return AsFP(SetLayerSourceCrop); + case HWC2::FunctionDescriptor::SetLayerSurfaceDamage: + return AsFP(SetLayerSurfaceDamage); + case HWC2::FunctionDescriptor::SetLayerTransform: + return AsFP(SetLayerTransform); + case HWC2::FunctionDescriptor::SetLayerVisibleRegion: + return AsFP(SetLayerVisibleRegion); + case HWC2::FunctionDescriptor::SetLayerZOrder: + return AsFP(SetLayerZOrder); + case HWC2::FunctionDescriptor::SetOutputBuffer: + return AsFP(SetOutputBuffer); + case HWC2::FunctionDescriptor::SetPowerMode: + return AsFP(SetPowerMode); + case HWC2::FunctionDescriptor::SetVsyncEnabled: + return AsFP(SetVsyncEnabled); + case HWC2::FunctionDescriptor::ValidateDisplay: + return AsFP(HWCSession::ValidateDisplay); + default: + DLOGD("Unknown/Unimplemented function descriptor: %d (%s)", int_descriptor, + to_string(descriptor).c_str()); + return nullptr; + } + return nullptr; +} + +// TODO(user): handle locking + +HWC2::Error HWCSession::CreateVirtualDisplayObject(uint32_t width, uint32_t height) { + if (hwc_display_[HWC_DISPLAY_VIRTUAL]) { + return HWC2::Error::NoResources; + } + auto status = HWCDisplayVirtual::Create(core_intf_, &callbacks_, width, height, + &hwc_display_[HWC_DISPLAY_VIRTUAL]); + // TODO(user): validate width and height support + if (status) + return HWC2::Error::Unsupported; + + return HWC2::Error::None; +} + +int32_t HWCSession::ConnectDisplay(int disp) { + DLOGI("Display = %d", disp); + + int status = 0; + uint32_t primary_width = 0; + uint32_t primary_height = 0; + + hwc_display_[HWC_DISPLAY_PRIMARY]->GetFrameBufferResolution(&primary_width, &primary_height); + + if (disp == HWC_DISPLAY_EXTERNAL) { + status = HWCDisplayExternal::Create(core_intf_, &callbacks_, primary_width, primary_height, + qservice_, false, &hwc_display_[disp]); + } else { + DLOGE("Invalid display type"); + return -1; + } + + if (!status) { + hwc_display_[disp]->SetSecureDisplay(secure_display_active_); + } + + return status; +} + +int HWCSession::DisconnectDisplay(int disp) { + DLOGI("Display = %d", disp); + + if (disp == HWC_DISPLAY_EXTERNAL) { + HWCDisplayExternal::Destroy(hwc_display_[disp]); + } else if (disp == HWC_DISPLAY_VIRTUAL) { + HWCDisplayVirtual::Destroy(hwc_display_[disp]); + } else { + DLOGE("Invalid display type"); + return -1; + } + + hwc_display_[disp] = NULL; + + return 0; +} + +// Qclient methods +android::status_t HWCSession::notifyCallback(uint32_t command, const android::Parcel *input_parcel, + android::Parcel *output_parcel) { + SEQUENCE_WAIT_SCOPE_LOCK(locker_); + + android::status_t status = 0; + + switch (command) { + case qService::IQService::DYNAMIC_DEBUG: + DynamicDebug(input_parcel); + break; + + case qService::IQService::SCREEN_REFRESH: + callbacks_.Refresh(HWC_DISPLAY_PRIMARY); + break; + + case qService::IQService::SET_IDLE_TIMEOUT: + if (hwc_display_[HWC_DISPLAY_PRIMARY]) { + uint32_t timeout = UINT32(input_parcel->readInt32()); + hwc_display_[HWC_DISPLAY_PRIMARY]->SetIdleTimeoutMs(timeout); + } + break; + + case qService::IQService::SET_FRAME_DUMP_CONFIG: + SetFrameDumpConfig(input_parcel); + break; + + case qService::IQService::SET_MAX_PIPES_PER_MIXER: + status = SetMaxMixerStages(input_parcel); + break; + + case qService::IQService::SET_DISPLAY_MODE: + status = SetDisplayMode(input_parcel); + break; + + case qService::IQService::SET_SECONDARY_DISPLAY_STATUS: + status = SetSecondaryDisplayStatus(input_parcel, output_parcel); + break; + + case qService::IQService::CONFIGURE_DYN_REFRESH_RATE: + status = ConfigureRefreshRate(input_parcel); + break; + + case qService::IQService::SET_VIEW_FRAME: + break; + + case qService::IQService::TOGGLE_SCREEN_UPDATES: + status = ToggleScreenUpdates(input_parcel, output_parcel); + break; + + case qService::IQService::QDCM_SVC_CMDS: + status = QdcmCMDHandler(input_parcel, output_parcel); + break; + + case qService::IQService::MIN_HDCP_ENCRYPTION_LEVEL_CHANGED: + status = OnMinHdcpEncryptionLevelChange(input_parcel, output_parcel); + break; + + case qService::IQService::CONTROL_PARTIAL_UPDATE: + status = ControlPartialUpdate(input_parcel, output_parcel); + break; + + case qService::IQService::SET_ACTIVE_CONFIG: + status = HandleSetActiveDisplayConfig(input_parcel, output_parcel); + break; + + case qService::IQService::GET_ACTIVE_CONFIG: + status = HandleGetActiveDisplayConfig(input_parcel, output_parcel); + break; + + case qService::IQService::GET_CONFIG_COUNT: + status = HandleGetDisplayConfigCount(input_parcel, output_parcel); + break; + + case qService::IQService::GET_DISPLAY_ATTRIBUTES_FOR_CONFIG: + status = HandleGetDisplayAttributesForConfig(input_parcel, output_parcel); + break; + + case qService::IQService::GET_PANEL_BRIGHTNESS: + status = GetPanelBrightness(input_parcel, output_parcel); + break; + + case qService::IQService::SET_PANEL_BRIGHTNESS: + status = SetPanelBrightness(input_parcel, output_parcel); + break; + + case qService::IQService::GET_DISPLAY_VISIBLE_REGION: + status = GetVisibleDisplayRect(input_parcel, output_parcel); + break; + + case qService::IQService::SET_CAMERA_STATUS: + status = SetDynamicBWForCamera(input_parcel, output_parcel); + break; + + case qService::IQService::GET_BW_TRANSACTION_STATUS: + status = GetBWTransactionStatus(input_parcel, output_parcel); + break; + + default: + DLOGW("QService command = %d is not supported", command); + return -EINVAL; + } + + return status; +} + +android::status_t HWCSession::ToggleScreenUpdates(const android::Parcel *input_parcel, + android::Parcel *output_parcel) { + int input = input_parcel->readInt32(); + int error = android::BAD_VALUE; + + if (hwc_display_[HWC_DISPLAY_PRIMARY] && (input <= 1) && (input >= 0)) { + error = hwc_display_[HWC_DISPLAY_PRIMARY]->ToggleScreenUpdates(input == 1); + if (error != 0) { + DLOGE("Failed to toggle screen updates = %d. Error = %d", input, error); + } + } + output_parcel->writeInt32(error); + + return error; +} + +android::status_t HWCSession::SetPanelBrightness(const android::Parcel *input_parcel, + android::Parcel *output_parcel) { + int level = input_parcel->readInt32(); + int error = android::BAD_VALUE; + + if (hwc_display_[HWC_DISPLAY_PRIMARY]) { + error = hwc_display_[HWC_DISPLAY_PRIMARY]->SetPanelBrightness(level); + if (error != 0) { + DLOGE("Failed to set the panel brightness = %d. Error = %d", level, error); + } + } + output_parcel->writeInt32(error); + + return error; +} + +android::status_t HWCSession::GetPanelBrightness(const android::Parcel *input_parcel, + android::Parcel *output_parcel) { + int error = android::BAD_VALUE; + int ret = error; + + if (hwc_display_[HWC_DISPLAY_PRIMARY]) { + error = hwc_display_[HWC_DISPLAY_PRIMARY]->GetPanelBrightness(&ret); + if (error != 0) { + ret = error; + DLOGE("Failed to get the panel brightness. Error = %d", error); + } + } + output_parcel->writeInt32(ret); + + return error; +} + +android::status_t HWCSession::ControlPartialUpdate(const android::Parcel *input_parcel, + android::Parcel *out) { + DisplayError error = kErrorNone; + int ret = 0; + uint32_t disp_id = UINT32(input_parcel->readInt32()); + uint32_t enable = UINT32(input_parcel->readInt32()); + + if (disp_id != HWC_DISPLAY_PRIMARY) { + DLOGW("CONTROL_PARTIAL_UPDATE is not applicable for display = %d", disp_id); + ret = -EINVAL; + out->writeInt32(ret); + return ret; + } + + if (!hwc_display_[HWC_DISPLAY_PRIMARY]) { + DLOGE("primary display object is not instantiated"); + ret = -EINVAL; + out->writeInt32(ret); + return ret; + } + + uint32_t pending = 0; + error = hwc_display_[HWC_DISPLAY_PRIMARY]->ControlPartialUpdate(enable, &pending); + + if (error == kErrorNone) { + if (!pending) { + out->writeInt32(ret); + return ret; + } + } else if (error == kErrorNotSupported) { + out->writeInt32(ret); + return ret; + } else { + ret = -EINVAL; + out->writeInt32(ret); + return ret; + } + + // Todo(user): Unlock it before sending events to client. It may cause deadlocks in future. + callbacks_.Refresh(HWC_DISPLAY_PRIMARY); + + // Wait until partial update control is complete + ret = locker_.WaitFinite(kPartialUpdateControlTimeoutMs); + + out->writeInt32(ret); + + return ret; +} + +android::status_t HWCSession::HandleSetActiveDisplayConfig(const android::Parcel *input_parcel, + android::Parcel *output_parcel) { + int config = input_parcel->readInt32(); + int dpy = input_parcel->readInt32(); + int error = android::BAD_VALUE; + + if (dpy > HWC_DISPLAY_VIRTUAL) { + return android::BAD_VALUE; + } + + if (hwc_display_[dpy]) { + error = hwc_display_[dpy]->SetActiveDisplayConfig(config); + if (error == 0) { + callbacks_.Refresh(0); + } + } + + return error; +} + +android::status_t HWCSession::HandleGetActiveDisplayConfig(const android::Parcel *input_parcel, + android::Parcel *output_parcel) { + int dpy = input_parcel->readInt32(); + int error = android::BAD_VALUE; + + if (dpy > HWC_DISPLAY_VIRTUAL) { + return android::BAD_VALUE; + } + + if (hwc_display_[dpy]) { + uint32_t config = 0; + error = hwc_display_[dpy]->GetActiveDisplayConfig(&config); + if (error == 0) { + output_parcel->writeInt32(INT(config)); + } + } + + return error; +} + +android::status_t HWCSession::HandleGetDisplayConfigCount(const android::Parcel *input_parcel, + android::Parcel *output_parcel) { + int dpy = input_parcel->readInt32(); + int error = android::BAD_VALUE; + + if (dpy > HWC_DISPLAY_VIRTUAL) { + return android::BAD_VALUE; + } + + uint32_t count = 0; + if (hwc_display_[dpy]) { + error = hwc_display_[dpy]->GetDisplayConfigCount(&count); + if (error == 0) { + output_parcel->writeInt32(INT(count)); + } + } + + return error; +} + +android::status_t HWCSession::HandleGetDisplayAttributesForConfig( + const android::Parcel *input_parcel, android::Parcel *output_parcel) { + int config = input_parcel->readInt32(); + int dpy = input_parcel->readInt32(); + int error = android::BAD_VALUE; + DisplayConfigVariableInfo attributes; + + if (dpy > HWC_DISPLAY_VIRTUAL) { + return android::BAD_VALUE; + } + + if (hwc_display_[dpy]) { + error = hwc_display_[dpy]->GetDisplayAttributesForConfig(config, &attributes); + if (error == 0) { + output_parcel->writeInt32(INT(attributes.vsync_period_ns)); + output_parcel->writeInt32(INT(attributes.x_pixels)); + output_parcel->writeInt32(INT(attributes.y_pixels)); + output_parcel->writeFloat(attributes.x_dpi); + output_parcel->writeFloat(attributes.y_dpi); + output_parcel->writeInt32(0); // Panel type, unsupported. + } + } + + return error; +} + +android::status_t HWCSession::SetSecondaryDisplayStatus(const android::Parcel *input_parcel, + android::Parcel *output_parcel) { + int ret = -EINVAL; + + uint32_t display_id = UINT32(input_parcel->readInt32()); + uint32_t display_status = UINT32(input_parcel->readInt32()); + + DLOGI("Display = %d, Status = %d", display_id, display_status); + + if (display_id >= HWC_NUM_DISPLAY_TYPES) { + DLOGE("Invalid display_id"); + } else if (display_id == HWC_DISPLAY_PRIMARY) { + DLOGE("Not supported for this display"); + } else if (!hwc_display_[display_id]) { + DLOGW("Display is not connected"); + } else { + ret = hwc_display_[display_id]->SetDisplayStatus(display_status); + } + + output_parcel->writeInt32(ret); + + return ret; +} + +android::status_t HWCSession::ConfigureRefreshRate(const android::Parcel *input_parcel) { + uint32_t operation = UINT32(input_parcel->readInt32()); + switch (operation) { + case qdutils::DISABLE_METADATA_DYN_REFRESH_RATE: + return hwc_display_[HWC_DISPLAY_PRIMARY]->Perform( + HWCDisplayPrimary::SET_METADATA_DYN_REFRESH_RATE, false); + case qdutils::ENABLE_METADATA_DYN_REFRESH_RATE: + return hwc_display_[HWC_DISPLAY_PRIMARY]->Perform( + HWCDisplayPrimary::SET_METADATA_DYN_REFRESH_RATE, true); + case qdutils::SET_BINDER_DYN_REFRESH_RATE: { + uint32_t refresh_rate = UINT32(input_parcel->readInt32()); + return hwc_display_[HWC_DISPLAY_PRIMARY]->Perform( + HWCDisplayPrimary::SET_BINDER_DYN_REFRESH_RATE, refresh_rate); + } + default: + DLOGW("Invalid operation %d", operation); + return -EINVAL; + } + + return 0; +} + +android::status_t HWCSession::SetDisplayMode(const android::Parcel *input_parcel) { + uint32_t mode = UINT32(input_parcel->readInt32()); + return hwc_display_[HWC_DISPLAY_PRIMARY]->Perform(HWCDisplayPrimary::SET_DISPLAY_MODE, mode); +} + +android::status_t HWCSession::SetMaxMixerStages(const android::Parcel *input_parcel) { + DisplayError error = kErrorNone; + uint32_t bit_mask_display_type = UINT32(input_parcel->readInt32()); + uint32_t max_mixer_stages = UINT32(input_parcel->readInt32()); + + if (IS_BIT_SET(bit_mask_display_type, HWC_DISPLAY_PRIMARY)) { + if (hwc_display_[HWC_DISPLAY_PRIMARY]) { + error = hwc_display_[HWC_DISPLAY_PRIMARY]->SetMaxMixerStages(max_mixer_stages); + if (error != kErrorNone) { + return -EINVAL; + } + } + } + + if (IS_BIT_SET(bit_mask_display_type, HWC_DISPLAY_EXTERNAL)) { + if (hwc_display_[HWC_DISPLAY_EXTERNAL]) { + error = hwc_display_[HWC_DISPLAY_EXTERNAL]->SetMaxMixerStages(max_mixer_stages); + if (error != kErrorNone) { + return -EINVAL; + } + } + } + + if (IS_BIT_SET(bit_mask_display_type, HWC_DISPLAY_VIRTUAL)) { + if (hwc_display_[HWC_DISPLAY_VIRTUAL]) { + error = hwc_display_[HWC_DISPLAY_VIRTUAL]->SetMaxMixerStages(max_mixer_stages); + if (error != kErrorNone) { + return -EINVAL; + } + } + } + + return 0; +} + +android::status_t HWCSession::SetDynamicBWForCamera(const android::Parcel *input_parcel, + android::Parcel *output_parcel) { + DisplayError error = kErrorNone; + uint32_t camera_status = UINT32(input_parcel->readInt32()); + HWBwModes mode = camera_status > 0 ? kBwCamera : kBwDefault; + + // trigger invalidate to apply new bw caps. + callbacks_.Refresh(HWC_DISPLAY_PRIMARY); + + error = core_intf_->SetMaxBandwidthMode(mode); + if (error != kErrorNone) { + return -EINVAL; + } + + new_bw_mode_ = true; + need_invalidate_ = true; + + return 0; +} + +android::status_t HWCSession::GetBWTransactionStatus(const android::Parcel *input_parcel, + android::Parcel *output_parcel) { + bool state = true; + + if (hwc_display_[HWC_DISPLAY_PRIMARY]) { + if (sync_wait(bw_mode_release_fd_, 0) < 0) { + DLOGI("bw_transaction_release_fd is not yet signalled: err= %s", strerror(errno)); + state = false; + } + output_parcel->writeInt32(state); + } + + return 0; +} + +void HWCSession::SetFrameDumpConfig(const android::Parcel *input_parcel) { + uint32_t frame_dump_count = UINT32(input_parcel->readInt32()); + uint32_t bit_mask_display_type = UINT32(input_parcel->readInt32()); + uint32_t bit_mask_layer_type = UINT32(input_parcel->readInt32()); + + if (IS_BIT_SET(bit_mask_display_type, HWC_DISPLAY_PRIMARY)) { + if (hwc_display_[HWC_DISPLAY_PRIMARY]) { + hwc_display_[HWC_DISPLAY_PRIMARY]->SetFrameDumpConfig(frame_dump_count, bit_mask_layer_type); + } + } + + if (IS_BIT_SET(bit_mask_display_type, HWC_DISPLAY_EXTERNAL)) { + if (hwc_display_[HWC_DISPLAY_EXTERNAL]) { + hwc_display_[HWC_DISPLAY_EXTERNAL]->SetFrameDumpConfig(frame_dump_count, bit_mask_layer_type); + } + } + + if (IS_BIT_SET(bit_mask_display_type, HWC_DISPLAY_VIRTUAL)) { + if (hwc_display_[HWC_DISPLAY_VIRTUAL]) { + hwc_display_[HWC_DISPLAY_VIRTUAL]->SetFrameDumpConfig(frame_dump_count, bit_mask_layer_type); + } + } +} + +void HWCSession::DynamicDebug(const android::Parcel *input_parcel) { + int type = input_parcel->readInt32(); + bool enable = (input_parcel->readInt32() > 0); + DLOGI("type = %d enable = %d", type, enable); + int verbose_level = input_parcel->readInt32(); + + switch (type) { + case qService::IQService::DEBUG_ALL: + HWCDebugHandler::DebugAll(enable, verbose_level); + break; + + case qService::IQService::DEBUG_MDPCOMP: + HWCDebugHandler::DebugStrategy(enable, verbose_level); + HWCDebugHandler::DebugCompManager(enable, verbose_level); + break; + + case qService::IQService::DEBUG_PIPE_LIFECYCLE: + HWCDebugHandler::DebugResources(enable, verbose_level); + break; + + case qService::IQService::DEBUG_DRIVER_CONFIG: + HWCDebugHandler::DebugDriverConfig(enable, verbose_level); + break; + + case qService::IQService::DEBUG_ROTATOR: + HWCDebugHandler::DebugResources(enable, verbose_level); + HWCDebugHandler::DebugDriverConfig(enable, verbose_level); + HWCDebugHandler::DebugRotator(enable, verbose_level); + break; + + case qService::IQService::DEBUG_QDCM: + HWCDebugHandler::DebugQdcm(enable, verbose_level); + break; + + default: + DLOGW("type = %d is not supported", type); + } +} + +android::status_t HWCSession::QdcmCMDHandler(const android::Parcel *input_parcel, + android::Parcel *output_parcel) { + int ret = 0; + int32_t *brightness_value = NULL; + uint32_t display_id(0); + PPPendingParams pending_action; + PPDisplayAPIPayload resp_payload, req_payload; + + if (!color_mgr_) { + return -1; + } + + pending_action.action = kNoAction; + pending_action.params = NULL; + + // Read display_id, payload_size and payload from in_parcel. + ret = HWCColorManager::CreatePayloadFromParcel(*input_parcel, &display_id, &req_payload); + if (!ret) { + if (HWC_DISPLAY_PRIMARY == display_id && hwc_display_[HWC_DISPLAY_PRIMARY]) + ret = hwc_display_[HWC_DISPLAY_PRIMARY]->ColorSVCRequestRoute(req_payload, &resp_payload, + &pending_action); + + if (HWC_DISPLAY_EXTERNAL == display_id && hwc_display_[HWC_DISPLAY_EXTERNAL]) + ret = hwc_display_[HWC_DISPLAY_EXTERNAL]->ColorSVCRequestRoute(req_payload, &resp_payload, + &pending_action); + } + + if (ret) { + output_parcel->writeInt32(ret); // first field in out parcel indicates return code. + req_payload.DestroyPayload(); + resp_payload.DestroyPayload(); + return ret; + } + + switch (pending_action.action) { + case kInvalidating: + callbacks_.Refresh(HWC_DISPLAY_PRIMARY); + break; + case kEnterQDCMMode: + ret = color_mgr_->EnableQDCMMode(true, hwc_display_[HWC_DISPLAY_PRIMARY]); + break; + case kExitQDCMMode: + ret = color_mgr_->EnableQDCMMode(false, hwc_display_[HWC_DISPLAY_PRIMARY]); + break; + case kApplySolidFill: + ret = + color_mgr_->SetSolidFill(pending_action.params, true, hwc_display_[HWC_DISPLAY_PRIMARY]); + callbacks_.Refresh(HWC_DISPLAY_PRIMARY); + break; + case kDisableSolidFill: + ret = + color_mgr_->SetSolidFill(pending_action.params, false, hwc_display_[HWC_DISPLAY_PRIMARY]); + callbacks_.Refresh(HWC_DISPLAY_PRIMARY); + break; + case kSetPanelBrightness: + brightness_value = reinterpret_cast(resp_payload.payload); + if (brightness_value == NULL) { + DLOGE("Brightness value is Null"); + return -EINVAL; + } + if (HWC_DISPLAY_PRIMARY == display_id) + ret = hwc_display_[HWC_DISPLAY_PRIMARY]->SetPanelBrightness(*brightness_value); + break; + case kEnableFrameCapture: + ret = color_mgr_->SetFrameCapture(pending_action.params, true, + hwc_display_[HWC_DISPLAY_PRIMARY]); + callbacks_.Refresh(HWC_DISPLAY_PRIMARY); + break; + case kDisableFrameCapture: + ret = color_mgr_->SetFrameCapture(pending_action.params, false, + hwc_display_[HWC_DISPLAY_PRIMARY]); + break; + case kNoAction: + break; + default: + DLOGW("Invalid pending action = %d!", pending_action.action); + break; + } + + // for display API getter case, marshall returned params into out_parcel. + output_parcel->writeInt32(ret); + HWCColorManager::MarshallStructIntoParcel(resp_payload, output_parcel); + req_payload.DestroyPayload(); + resp_payload.DestroyPayload(); + + return (ret ? -EINVAL : 0); +} + +android::status_t HWCSession::OnMinHdcpEncryptionLevelChange(const android::Parcel *input_parcel, + android::Parcel *output_parcel) { + int ret = -EINVAL; + uint32_t display_id = UINT32(input_parcel->readInt32()); + uint32_t min_enc_level = UINT32(input_parcel->readInt32()); + + DLOGI("Display %d", display_id); + + if (display_id >= HWC_NUM_DISPLAY_TYPES) { + DLOGE("Invalid display_id"); + } else if (display_id != HWC_DISPLAY_EXTERNAL) { + DLOGE("Not supported for display"); + } else if (!hwc_display_[display_id]) { + DLOGW("Display is not connected"); + } else { + ret = hwc_display_[display_id]->OnMinHdcpEncryptionLevelChange(min_enc_level); + } + + output_parcel->writeInt32(ret); + + return ret; +} + +void *HWCSession::HWCUeventThread(void *context) { + if (context) { + return reinterpret_cast(context)->HWCUeventThreadHandler(); + } + + return NULL; +} + +void *HWCSession::HWCUeventThreadHandler() { + static char uevent_data[PAGE_SIZE]; + int length = 0; + prctl(PR_SET_NAME, uevent_thread_name_, 0, 0, 0); + setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY); + if (!uevent_init()) { + DLOGE("Failed to init uevent"); + pthread_exit(0); + return NULL; + } + + while (!uevent_thread_exit_) { + // keep last 2 zeroes to ensure double 0 termination + length = uevent_next_event(uevent_data, INT32(sizeof(uevent_data)) - 2); + + if (strcasestr(HWC_UEVENT_SWITCH_HDMI, uevent_data)) { + DLOGI("Uevent HDMI = %s", uevent_data); + int connected = GetEventValue(uevent_data, length, "SWITCH_STATE="); + if (connected >= 0) { + DLOGI("HDMI = %s", connected ? "connected" : "disconnected"); + if (HotPlugHandler(connected) == -1) { + DLOGE("Failed handling Hotplug = %s", connected ? "connected" : "disconnected"); + } + } + } else if (strcasestr(HWC_UEVENT_GRAPHICS_FB0, uevent_data)) { + DLOGI("Uevent FB0 = %s", uevent_data); + int panel_reset = GetEventValue(uevent_data, length, "PANEL_ALIVE="); + if (panel_reset == 0) { + callbacks_.Refresh(0); + reset_panel_ = true; + } + } + } + pthread_exit(0); + + return NULL; +} + +int HWCSession::GetEventValue(const char *uevent_data, int length, const char *event_info) { + const char *iterator_str = uevent_data; + while (((iterator_str - uevent_data) <= length) && (*iterator_str)) { + char *pstr = strstr(iterator_str, event_info); + if (pstr != NULL) { + return (atoi(iterator_str + strlen(event_info))); + } + iterator_str += strlen(iterator_str) + 1; + } + + return -1; +} + +void HWCSession::ResetPanel() { + HWC2::Error status; + + DLOGI("Powering off primary"); + status = hwc_display_[HWC_DISPLAY_PRIMARY]->SetPowerMode(HWC2::PowerMode::Off); + if (status != HWC2::Error::None) { + DLOGE("power-off on primary failed with error = %d", status); + } + + DLOGI("Restoring power mode on primary"); + HWC2::PowerMode mode = hwc_display_[HWC_DISPLAY_PRIMARY]->GetLastPowerMode(); + status = hwc_display_[HWC_DISPLAY_PRIMARY]->SetPowerMode(mode); + if (status != HWC2::Error::None) { + DLOGE("Setting power mode = %d on primary failed with error = %d", mode, status); + } + + status = hwc_display_[HWC_DISPLAY_PRIMARY]->SetVsyncEnabled(HWC2::Vsync::Enable); + if (status != HWC2::Error::None) { + DLOGE("enabling vsync failed for primary with error = %d", status); + } + + reset_panel_ = false; +} + +int HWCSession::HotPlugHandler(bool connected) { + int status = 0; + bool notify_hotplug = false; + bool hdmi_primary = false; + + // To prevent sending events to client while a lock is held, acquire scope locks only within + // below scope so that those get automatically unlocked after the scope ends. + { + SEQUENCE_WAIT_SCOPE_LOCK(locker_); + + if (!hwc_display_[HWC_DISPLAY_PRIMARY]) { + DLOGE("Primary display is not connected."); + return -1; + } + + HWCDisplay *primary_display = hwc_display_[HWC_DISPLAY_PRIMARY]; + HWCDisplay *external_display = NULL; + + if (primary_display->GetDisplayClass() == DISPLAY_CLASS_EXTERNAL) { + external_display = static_cast(hwc_display_[HWC_DISPLAY_PRIMARY]); + hdmi_primary = true; + } + + // If primary display connected is a NULL display, then replace it with the external display + if (connected) { + // If we are in HDMI as primary and the primary display just got plugged in + if (hwc_display_[HWC_DISPLAY_EXTERNAL]) { + DLOGE("HDMI is already connected"); + return -1; + } + + // Connect external display if virtual display is not connected. + // Else, defer external display connection and process it when virtual display + // tears down; Do not notify SurfaceFlinger since connection is deferred now. + if (!hwc_display_[HWC_DISPLAY_VIRTUAL]) { + status = ConnectDisplay(HWC_DISPLAY_EXTERNAL); + if (status) { + return status; + } + notify_hotplug = true; + } else { + DLOGI("Virtual display is connected, pending connection"); + external_pending_connect_ = true; + } + } else { + // Do not return error if external display is not in connected status. + // Due to virtual display concurrency, external display connection might be still pending + // but hdmi got disconnected before pending connection could be processed. + + if (hdmi_primary) { + assert(external_display != NULL); + uint32_t x_res, y_res; + external_display->GetFrameBufferResolution(&x_res, &y_res); + // Need to manually disable VSYNC as SF is not aware of connect/disconnect cases + // for HDMI as primary + external_display->SetVsyncEnabled(HWC2::Vsync::Disable); + HWCDisplayExternal::Destroy(external_display); + + // In HWC2, primary displays can be hotplugged out + notify_hotplug = true; + } else { + if (hwc_display_[HWC_DISPLAY_EXTERNAL]) { + status = DisconnectDisplay(HWC_DISPLAY_EXTERNAL); + notify_hotplug = true; + } + external_pending_connect_ = false; + } + } + } + + if (connected && notify_hotplug) { + // trigger screen refresh to ensure sufficient resources are available to process new + // new display connection. + callbacks_.Refresh(0); + uint32_t vsync_period = UINT32(GetVsyncPeriod(HWC_DISPLAY_PRIMARY)); + usleep(vsync_period * 2 / 1000); + } + // notify client + // Handle HDMI as primary here + if (notify_hotplug) { + callbacks_.Hotplug(HWC_DISPLAY_EXTERNAL, + connected ? HWC2::Connection::Connected : HWC2::Connection::Disconnected); + } + + qservice_->onHdmiHotplug(INT(connected)); + + return 0; +} + +int HWCSession::GetVsyncPeriod(int disp) { + SCOPE_LOCK(locker_); + // default value + int32_t vsync_period = 1000000000l / 60; + auto attribute = HWC2::Attribute::VsyncPeriod; + + if (hwc_display_[disp]) { + hwc_display_[disp]->GetDisplayAttribute(0, attribute, &vsync_period); + } + + return vsync_period; +} + +android::status_t HWCSession::GetVisibleDisplayRect(const android::Parcel *input_parcel, + android::Parcel *output_parcel) { + int dpy = input_parcel->readInt32(); + + if (dpy < HWC_DISPLAY_PRIMARY || dpy > HWC_DISPLAY_VIRTUAL) { + return android::BAD_VALUE; + } + + if (!hwc_display_[dpy]) { + return android::NO_INIT; + } + + hwc_rect_t visible_rect = {0, 0, 0, 0}; + int error = hwc_display_[dpy]->GetVisibleDisplayRect(&visible_rect); + if (error < 0) { + return error; + } + + output_parcel->writeInt32(visible_rect.left); + output_parcel->writeInt32(visible_rect.top); + output_parcel->writeInt32(visible_rect.right); + output_parcel->writeInt32(visible_rect.bottom); + + return android::NO_ERROR; +} + +} // namespace sdm diff --git a/sdm/libs/hwc2/hwc_session.h b/sdm/libs/hwc2/hwc_session.h new file mode 100644 index 00000000..d52afda2 --- /dev/null +++ b/sdm/libs/hwc2/hwc_session.h @@ -0,0 +1,177 @@ +/* + * Copyright (c) 2014-2016, The Linux Foundation. All rights reserved. + * Not a Contribution. + * + * Copyright 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __HWC_SESSION_H__ +#define __HWC_SESSION_H__ + +#include +#include + +#include "hwc_callbacks.h" +#include "hwc_layers.h" +#include "hwc_display.h" +#include "hwc_display_primary.h" +#include "hwc_display_external.h" +#include "hwc_display_virtual.h" +#include "hwc_color_manager.h" + +namespace sdm { + +class HWCSession : hwc2_device_t, public qClient::BnQClient { + public: + struct HWCModuleMethods : public hw_module_methods_t { + HWCModuleMethods() { hw_module_methods_t::open = HWCSession::Open; } + }; + + explicit HWCSession(const hw_module_t *module); + int Init(); + int Deinit(); + HWC2::Error CreateVirtualDisplayObject(uint32_t width, uint32_t height); + + template + static int32_t CallDisplayFunction(hwc2_device_t *device, hwc2_display_t display, + HWC2::Error (HWCDisplay::*member)(Args...), Args... args) { + if (!device) { + return HWC2_ERROR_BAD_DISPLAY; + } + + HWCSession *hwc_session = static_cast(device); + auto status = HWC2::Error::BadDisplay; + if (hwc_session->hwc_display_[display]) { + auto hwc_display = hwc_session->hwc_display_[display]; + status = (hwc_display->*member)(std::forward(args)...); + } + return INT32(status); + } + + template + static int32_t CallLayerFunction(hwc2_device_t *device, hwc2_display_t display, + hwc2_layer_t layer, HWC2::Error (HWCLayer::*member)(Args...), + Args... args) { + if (!device) { + return HWC2_ERROR_BAD_DISPLAY; + } + + HWCSession *hwc_session = static_cast(device); + auto status = HWC2::Error::BadDisplay; + if (hwc_session->hwc_display_[display]) { + status = HWC2::Error::BadLayer; + auto hwc_layer = hwc_session->hwc_display_[display]->GetHWCLayer(layer); + if (hwc_layer != nullptr) { + status = (hwc_layer->*member)(std::forward(args)...); + } + } + return INT32(status); + } + + // HWC2 Functions that require a concrete implementation in hwc session + // and hence need to be member functions + static int32_t CreateVirtualDisplay(hwc2_device_t *device, uint32_t width, uint32_t height, + hwc2_display_t *out_display_id); + static int32_t DestroyVirtualDisplay(hwc2_device_t *device, hwc2_display_t display); + static int32_t PresentDisplay(hwc2_device_t *device, hwc2_display_t display, + int32_t *out_retire_fence); + static int32_t RegisterCallback(hwc2_device_t *device, int32_t descriptor, + hwc2_callback_data_t callback_data, + hwc2_function_pointer_t pointer); + static int32_t SetOutputBuffer(hwc2_device_t *device, hwc2_display_t display, + buffer_handle_t buffer, int32_t releaseFence); + static int32_t ValidateDisplay(hwc2_device_t *device, hwc2_display_t display, + uint32_t *out_num_types, uint32_t *out_num_requests); + + private: + static const int kExternalConnectionTimeoutMs = 500; + static const int kPartialUpdateControlTimeoutMs = 100; + + // hwc methods + static int Open(const hw_module_t *module, const char *name, hw_device_t **device); + static int Close(hw_device_t *device); + static void GetCapabilities(struct hwc2_device *device, uint32_t *outCount, + int32_t *outCapabilities); + static hwc2_function_pointer_t GetFunction(struct hwc2_device *device, int32_t descriptor); + + // Uevent thread + static void *HWCUeventThread(void *context); + void *HWCUeventThreadHandler(); + int GetEventValue(const char *uevent_data, int length, const char *event_info); + int HotPlugHandler(bool connected); + void ResetPanel(); + int32_t ConnectDisplay(int disp); + int DisconnectDisplay(int disp); + int GetVsyncPeriod(int disp); + + // QClient methods + virtual android::status_t notifyCallback(uint32_t command, const android::Parcel *input_parcel, + android::Parcel *output_parcel); + void DynamicDebug(const android::Parcel *input_parcel); + void SetFrameDumpConfig(const android::Parcel *input_parcel); + android::status_t SetMaxMixerStages(const android::Parcel *input_parcel); + android::status_t SetDisplayMode(const android::Parcel *input_parcel); + android::status_t SetSecondaryDisplayStatus(const android::Parcel *input_parcel, + android::Parcel *output_parcel); + android::status_t ToggleScreenUpdates(const android::Parcel *input_parcel, + android::Parcel *output_parcel); + android::status_t ConfigureRefreshRate(const android::Parcel *input_parcel); + android::status_t QdcmCMDHandler(const android::Parcel *input_parcel, + android::Parcel *output_parcel); + android::status_t ControlPartialUpdate(const android::Parcel *input_parcel, android::Parcel *out); + android::status_t OnMinHdcpEncryptionLevelChange(const android::Parcel *input_parcel, + android::Parcel *output_parcel); + android::status_t SetPanelBrightness(const android::Parcel *input_parcel, + android::Parcel *output_parcel); + android::status_t GetPanelBrightness(const android::Parcel *input_parcel, + android::Parcel *output_parcel); + // These functions return the actual display config info as opposed to FB + android::status_t HandleSetActiveDisplayConfig(const android::Parcel *input_parcel, + android::Parcel *output_parcel); + android::status_t HandleGetActiveDisplayConfig(const android::Parcel *input_parcel, + android::Parcel *output_parcel); + android::status_t HandleGetDisplayConfigCount(const android::Parcel *input_parcel, + android::Parcel *output_parcel); + android::status_t HandleGetDisplayAttributesForConfig(const android::Parcel *input_parcel, + android::Parcel *output_parcel); + android::status_t GetVisibleDisplayRect(const android::Parcel *input_parcel, + android::Parcel *output_parcel); + + android::status_t SetDynamicBWForCamera(const android::Parcel *input_parcel, + android::Parcel *output_parcel); + android::status_t GetBWTransactionStatus(const android::Parcel *input_parcel, + android::Parcel *output_parcel); + static Locker locker_; + CoreInterface *core_intf_ = NULL; + HWCDisplay *hwc_display_[HWC_NUM_DISPLAY_TYPES] = {NULL}; + HWCCallbacks callbacks_; + pthread_t uevent_thread_; + bool uevent_thread_exit_ = false; + const char *uevent_thread_name_ = "HWC_UeventThread"; + HWCBufferAllocator *buffer_allocator_ = NULL; + HWCBufferSyncHandler *buffer_sync_handler_ = NULL; + HWCColorManager *color_mgr_ = NULL; + bool reset_panel_ = false; + bool secure_display_active_ = false; + bool external_pending_connect_ = false; + bool new_bw_mode_ = false; + bool need_invalidate_ = false; + int bw_mode_release_fd_ = -1; + qService::QService *qservice_ = NULL; +}; + +} // namespace sdm + +#endif // __HWC_SESSION_H__