1 /* 2 * Copyright (c) 2014 - 2016, The Linux Foundation. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above 10 * copyright notice, this list of conditions and the following 11 * disclaimer in the documentation and/or other materials provided 12 * with the distribution. 13 * * Neither the name of The Linux Foundation nor the names of its 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <cutils/properties.h> 31 #include <utils/constants.h> 32 #include <utils/debug.h> 33 #include <algorithm> 34 35 #include "hwc_display_external.h" 36 #include "hwc_debugger.h" 37 38 #define __CLASS__ "HWCDisplayExternal" 39 40 namespace sdm { 41 42 int HWCDisplayExternal::Create(CoreInterface *core_intf, HWCCallbacks *callbacks, 43 qService::QService *qservice, HWCDisplay **hwc_display) { 44 return Create(core_intf, callbacks, 0, 0, qservice, false, hwc_display); 45 } 46 47 int HWCDisplayExternal::Create(CoreInterface *core_intf, HWCCallbacks *callbacks, 48 uint32_t primary_width, uint32_t primary_height, 49 qService::QService *qservice, bool use_primary_res, 50 HWCDisplay **hwc_display) { 51 uint32_t external_width = 0; 52 uint32_t external_height = 0; 53 DisplayError error = kErrorNone; 54 55 HWCDisplay *hwc_display_external = new HWCDisplayExternal(core_intf, callbacks, qservice); 56 int status = hwc_display_external->Init(); 57 if (status) { 58 delete hwc_display_external; 59 return status; 60 } 61 62 error = hwc_display_external->GetMixerResolution(&external_width, &external_height); 63 if (error != kErrorNone) { 64 return -EINVAL; 65 } 66 67 if (primary_width && primary_height) { 68 // use_primary_res means HWCDisplayExternal should directly set framebuffer resolution to the 69 // provided primary_width and primary_height 70 if (use_primary_res) { 71 external_width = primary_width; 72 external_height = primary_height; 73 } else { 74 int downscale_enabled = 0; 75 HWCDebugHandler::Get()->GetProperty("sdm.debug.downscale_external", &downscale_enabled); 76 if (downscale_enabled) { 77 GetDownscaleResolution(primary_width, primary_height, &external_width, &external_height); 78 } 79 } 80 } 81 82 status = hwc_display_external->SetFrameBufferResolution(external_width, external_height); 83 if (status) { 84 Destroy(hwc_display_external); 85 return status; 86 } 87 88 *hwc_display = hwc_display_external; 89 90 return status; 91 } 92 93 void HWCDisplayExternal::Destroy(HWCDisplay *hwc_display) { 94 hwc_display->Deinit(); 95 delete hwc_display; 96 } 97 98 HWCDisplayExternal::HWCDisplayExternal(CoreInterface *core_intf, HWCCallbacks *callbacks, 99 qService::QService *qservice) 100 : HWCDisplay(core_intf, callbacks, kHDMI, HWC_DISPLAY_EXTERNAL, false, qservice, 101 DISPLAY_CLASS_EXTERNAL) { 102 } 103 104 HWC2::Error HWCDisplayExternal::Validate(uint32_t *out_num_types, uint32_t *out_num_requests) { 105 auto status = HWC2::Error::None; 106 107 if (secure_display_active_) { 108 MarkLayersForGPUBypass(); 109 return status; 110 } 111 112 BuildLayerStack(); 113 114 if (layer_set_.empty()) { 115 flush_ = true; 116 return status; 117 } 118 119 status = PrepareLayerStack(out_num_types, out_num_requests); 120 return status; 121 } 122 123 HWC2::Error HWCDisplayExternal::Present(int32_t *out_retire_fence) { 124 auto status = HWC2::Error::None; 125 126 if (!secure_display_active_) { 127 status = HWCDisplay::CommitLayerStack(); 128 if (status == HWC2::Error::None) { 129 status = HWCDisplay::PostCommitLayerStack(out_retire_fence); 130 } 131 } 132 CloseAcquireFds(); 133 return status; 134 } 135 136 void HWCDisplayExternal::ApplyScanAdjustment(hwc_rect_t *display_frame) { 137 if (display_intf_->IsUnderscanSupported()) { 138 return; 139 } 140 141 // Read user defined width and height ratio 142 int width = 0, height = 0; 143 HWCDebugHandler::Get()->GetProperty("sdm.external_action_safe_width", &width); 144 float width_ratio = FLOAT(width) / 100.0f; 145 HWCDebugHandler::Get()->GetProperty("sdm.external_action_safe_height", &height); 146 float height_ratio = FLOAT(height) / 100.0f; 147 148 if (width_ratio == 0.0f || height_ratio == 0.0f) { 149 return; 150 } 151 152 uint32_t mixer_width = 0; 153 uint32_t mixer_height = 0; 154 GetMixerResolution(&mixer_width, &mixer_height); 155 156 if (mixer_width == 0 || mixer_height == 0) { 157 DLOGV("Invalid mixer dimensions (%d, %d)", mixer_width, mixer_height); 158 return; 159 } 160 161 uint32_t new_mixer_width = UINT32(mixer_width * FLOAT(1.0f - width_ratio)); 162 uint32_t new_mixer_height = UINT32(mixer_height * FLOAT(1.0f - height_ratio)); 163 164 int x_offset = INT((FLOAT(mixer_width) * width_ratio) / 2.0f); 165 int y_offset = INT((FLOAT(mixer_height) * height_ratio) / 2.0f); 166 167 display_frame->left = (display_frame->left * INT32(new_mixer_width) / INT32(mixer_width)) 168 + x_offset; 169 display_frame->top = (display_frame->top * INT32(new_mixer_height) / INT32(mixer_height)) + 170 y_offset; 171 display_frame->right = ((display_frame->right * INT32(new_mixer_width)) / INT32(mixer_width)) + 172 x_offset; 173 display_frame->bottom = ((display_frame->bottom * INT32(new_mixer_height)) / INT32(mixer_height)) 174 + y_offset; 175 } 176 177 void HWCDisplayExternal::SetSecureDisplay(bool secure_display_active) { 178 if (secure_display_active_ != secure_display_active) { 179 secure_display_active_ = secure_display_active; 180 181 if (secure_display_active_) { 182 DisplayError error = display_intf_->Flush(); 183 if (error != kErrorNone) { 184 DLOGE("Flush failed. Error = %d", error); 185 } 186 } 187 } 188 return; 189 } 190 191 static void AdjustSourceResolution(uint32_t dst_width, uint32_t dst_height, uint32_t *src_width, 192 uint32_t *src_height) { 193 *src_height = (dst_width * (*src_height)) / (*src_width); 194 *src_width = dst_width; 195 } 196 197 void HWCDisplayExternal::GetDownscaleResolution(uint32_t primary_width, uint32_t primary_height, 198 uint32_t *non_primary_width, 199 uint32_t *non_primary_height) { 200 uint32_t primary_area = primary_width * primary_height; 201 uint32_t non_primary_area = (*non_primary_width) * (*non_primary_height); 202 203 if (primary_area > non_primary_area) { 204 if (primary_height > primary_width) { 205 std::swap(primary_height, primary_width); 206 } 207 AdjustSourceResolution(primary_width, primary_height, non_primary_width, non_primary_height); 208 } 209 } 210 211 } // namespace sdm 212