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 
Create(CoreInterface * core_intf,HWCBufferAllocator * buffer_allocator,HWCCallbacks * callbacks,qService::QService * qservice,HWCDisplay ** hwc_display)42 int HWCDisplayExternal::Create(CoreInterface *core_intf, HWCBufferAllocator *buffer_allocator,
43                                HWCCallbacks *callbacks, qService::QService *qservice,
44                                HWCDisplay **hwc_display) {
45   return Create(core_intf, buffer_allocator, callbacks, 0, 0, qservice, false, hwc_display);
46 }
47 
Create(CoreInterface * core_intf,HWCBufferAllocator * buffer_allocator,HWCCallbacks * callbacks,uint32_t primary_width,uint32_t primary_height,qService::QService * qservice,bool use_primary_res,HWCDisplay ** hwc_display)48 int HWCDisplayExternal::Create(CoreInterface *core_intf, HWCBufferAllocator *buffer_allocator,
49                                HWCCallbacks *callbacks,
50                                uint32_t primary_width, uint32_t primary_height,
51                                qService::QService *qservice, bool use_primary_res,
52                                HWCDisplay **hwc_display) {
53   uint32_t external_width = 0;
54   uint32_t external_height = 0;
55   DisplayError error = kErrorNone;
56 
57   HWCDisplay *hwc_display_external = new HWCDisplayExternal(core_intf, buffer_allocator, callbacks,
58                                                             qservice);
59   int status = hwc_display_external->Init();
60   if (status) {
61     delete hwc_display_external;
62     return status;
63   }
64 
65   error = hwc_display_external->GetMixerResolution(&external_width, &external_height);
66   if (error != kErrorNone) {
67     return -EINVAL;
68   }
69 
70   if (primary_width && primary_height) {
71     // use_primary_res means HWCDisplayExternal should directly set framebuffer resolution to the
72     // provided primary_width and primary_height
73     if (use_primary_res) {
74       external_width = primary_width;
75       external_height = primary_height;
76     } else {
77       int downscale_enabled = 0;
78       HWCDebugHandler::Get()->GetProperty("sdm.debug.downscale_external", &downscale_enabled);
79       if (downscale_enabled) {
80         GetDownscaleResolution(primary_width, primary_height, &external_width, &external_height);
81       }
82     }
83   }
84 
85   status = hwc_display_external->SetFrameBufferResolution(external_width, external_height);
86   if (status) {
87     Destroy(hwc_display_external);
88     return status;
89   }
90 
91   *hwc_display = hwc_display_external;
92 
93   return status;
94 }
95 
Destroy(HWCDisplay * hwc_display)96 void HWCDisplayExternal::Destroy(HWCDisplay *hwc_display) {
97   hwc_display->Deinit();
98   delete hwc_display;
99 }
100 
HWCDisplayExternal(CoreInterface * core_intf,HWCBufferAllocator * buffer_allocator,HWCCallbacks * callbacks,qService::QService * qservice)101 HWCDisplayExternal::HWCDisplayExternal(CoreInterface *core_intf,
102                                        HWCBufferAllocator *buffer_allocator,
103                                        HWCCallbacks *callbacks,
104                                        qService::QService *qservice)
105     : HWCDisplay(core_intf, callbacks, kHDMI, HWC_DISPLAY_EXTERNAL, false, qservice,
106                  DISPLAY_CLASS_EXTERNAL, buffer_allocator) {
107 }
108 
Validate(uint32_t * out_num_types,uint32_t * out_num_requests)109 HWC2::Error HWCDisplayExternal::Validate(uint32_t *out_num_types, uint32_t *out_num_requests) {
110   auto status = HWC2::Error::None;
111 
112   if (secure_display_active_) {
113     MarkLayersForGPUBypass();
114     return status;
115   }
116 
117   BuildLayerStack();
118 
119   if (layer_set_.empty()) {
120     flush_ = true;
121     return status;
122   }
123 
124   status = PrepareLayerStack(out_num_types, out_num_requests);
125   return status;
126 }
127 
Present(int32_t * out_retire_fence)128 HWC2::Error HWCDisplayExternal::Present(int32_t *out_retire_fence) {
129   auto status = HWC2::Error::None;
130 
131   if (!secure_display_active_) {
132     status = HWCDisplay::CommitLayerStack();
133     if (status == HWC2::Error::None) {
134       status = HWCDisplay::PostCommitLayerStack(out_retire_fence);
135     }
136   }
137   return status;
138 }
139 
ApplyScanAdjustment(hwc_rect_t * display_frame)140 void HWCDisplayExternal::ApplyScanAdjustment(hwc_rect_t *display_frame) {
141   if ((underscan_width_ <= 0) || (underscan_height_ <= 0)) {
142     return;
143   }
144 
145   float width_ratio = FLOAT(underscan_width_) / 100.0f;
146   float height_ratio = FLOAT(underscan_height_) / 100.0f;
147 
148   uint32_t mixer_width = 0;
149   uint32_t mixer_height = 0;
150   GetMixerResolution(&mixer_width, &mixer_height);
151 
152   if (mixer_width == 0 || mixer_height == 0) {
153     DLOGV("Invalid mixer dimensions (%d, %d)", mixer_width, mixer_height);
154     return;
155   }
156 
157   uint32_t new_mixer_width = UINT32(FLOAT(mixer_width) * (1.0f - width_ratio));
158   uint32_t new_mixer_height = UINT32(FLOAT(mixer_height) * (1.0f - height_ratio));
159 
160   int x_offset = INT((FLOAT(mixer_width) * width_ratio) / 2.0f);
161   int y_offset = INT((FLOAT(mixer_height) * height_ratio) / 2.0f);
162 
163   display_frame->left = (display_frame->left * INT32(new_mixer_width) / INT32(mixer_width))
164                         + x_offset;
165   display_frame->top = (display_frame->top * INT32(new_mixer_height) / INT32(mixer_height)) +
166                        y_offset;
167   display_frame->right = ((display_frame->right * INT32(new_mixer_width)) / INT32(mixer_width)) +
168                          x_offset;
169   display_frame->bottom = ((display_frame->bottom * INT32(new_mixer_height)) / INT32(mixer_height))
170                           + y_offset;
171 }
172 
SetSecureDisplay(bool secure_display_active)173 void HWCDisplayExternal::SetSecureDisplay(bool secure_display_active) {
174   if (secure_display_active_ != secure_display_active) {
175     secure_display_active_ = secure_display_active;
176 
177     if (secure_display_active_) {
178       DisplayError error = display_intf_->Flush();
179       if (error != kErrorNone) {
180         DLOGE("Flush failed. Error = %d", error);
181       }
182     }
183   }
184   return;
185 }
186 
AdjustSourceResolution(uint32_t dst_width,uint32_t dst_height,uint32_t * src_width,uint32_t * src_height)187 static void AdjustSourceResolution(uint32_t dst_width, uint32_t dst_height, uint32_t *src_width,
188                                    uint32_t *src_height) {
189   *src_height = (dst_width * (*src_height)) / (*src_width);
190   *src_width = dst_width;
191 }
192 
GetDownscaleResolution(uint32_t primary_width,uint32_t primary_height,uint32_t * non_primary_width,uint32_t * non_primary_height)193 void HWCDisplayExternal::GetDownscaleResolution(uint32_t primary_width, uint32_t primary_height,
194                                                 uint32_t *non_primary_width,
195                                                 uint32_t *non_primary_height) {
196   uint32_t primary_area = primary_width * primary_height;
197   uint32_t non_primary_area = (*non_primary_width) * (*non_primary_height);
198 
199   if (primary_area > non_primary_area) {
200     if (primary_height > primary_width) {
201       std::swap(primary_height, primary_width);
202     }
203     AdjustSourceResolution(primary_width, primary_height, non_primary_width, non_primary_height);
204   }
205 }
206 
GetUnderScanConfig()207 void HWCDisplayExternal::GetUnderScanConfig() {
208   if (!display_intf_->IsUnderscanSupported()) {
209     // Read user defined underscan width and height
210     HWCDebugHandler::Get()->GetProperty("sdm.external_action_safe_width", &underscan_width_);
211     HWCDebugHandler::Get()->GetProperty("sdm.external_action_safe_height", &underscan_height_);
212   }
213 }
214 
215 }  // namespace sdm
216