1 /*
2 * Copyright (c) 2014-2018, 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     Destroy(hwc_display_external);
68     return -EINVAL;
69   }
70 
71   if (primary_width && primary_height) {
72     // use_primary_res means HWCDisplayExternal should directly set framebuffer resolution to the
73     // provided primary_width and primary_height
74     if (use_primary_res) {
75       external_width = primary_width;
76       external_height = primary_height;
77     } else {
78       int downscale_enabled = 0;
79       HWCDebugHandler::Get()->GetProperty(ENABLE_EXTERNAL_DOWNSCALE_PROP, &downscale_enabled);
80       if (downscale_enabled) {
81         GetDownscaleResolution(primary_width, primary_height, &external_width, &external_height);
82       }
83     }
84   }
85 
86   status = hwc_display_external->SetFrameBufferResolution(external_width, external_height);
87   if (status) {
88     Destroy(hwc_display_external);
89     return status;
90   }
91 
92   *hwc_display = hwc_display_external;
93 
94   return status;
95 }
96 
Destroy(HWCDisplay * hwc_display)97 void HWCDisplayExternal::Destroy(HWCDisplay *hwc_display) {
98   hwc_display->Deinit();
99   delete hwc_display;
100 }
101 
HWCDisplayExternal(CoreInterface * core_intf,HWCBufferAllocator * buffer_allocator,HWCCallbacks * callbacks,qService::QService * qservice)102 HWCDisplayExternal::HWCDisplayExternal(CoreInterface *core_intf,
103                                        HWCBufferAllocator *buffer_allocator,
104                                        HWCCallbacks *callbacks,
105                                        qService::QService *qservice)
106     : HWCDisplay(core_intf, callbacks, kHDMI, HWC_DISPLAY_EXTERNAL, false, qservice,
107                  DISPLAY_CLASS_EXTERNAL, buffer_allocator) {
108 }
109 
Validate(uint32_t * out_num_types,uint32_t * out_num_requests)110 HWC2::Error HWCDisplayExternal::Validate(uint32_t *out_num_types, uint32_t *out_num_requests) {
111   auto status = HWC2::Error::None;
112 
113   if (secure_display_active_) {
114     MarkLayersForGPUBypass();
115     return status;
116   }
117 
118   if (config_pending_) {
119     if (display_intf_->SetActiveConfig(display_config_) != kErrorNone) {
120       DLOGW("Invalid display config %d", display_config_);
121       // Reset the display config with active config
122       display_intf_->GetActiveConfig(&display_config_);
123     }
124   }
125 
126   BuildLayerStack();
127 
128   if (layer_set_.empty()) {
129     flush_ = true;
130     validated_ = true;
131     return status;
132   }
133 
134   // TODO(user): SetRefreshRate need to follow new interface when added.
135 
136   status = PrepareLayerStack(out_num_types, out_num_requests);
137   return status;
138 }
139 
Present(int32_t * out_retire_fence)140 HWC2::Error HWCDisplayExternal::Present(int32_t *out_retire_fence) {
141   auto status = HWC2::Error::None;
142 
143   if (!secure_display_active_) {
144     status = HWCDisplay::CommitLayerStack();
145     if (status == HWC2::Error::None) {
146       status = HWCDisplay::PostCommitLayerStack(out_retire_fence);
147     }
148   }
149   return status;
150 }
151 
ApplyScanAdjustment(hwc_rect_t * display_frame)152 void HWCDisplayExternal::ApplyScanAdjustment(hwc_rect_t *display_frame) {
153   if ((underscan_width_ <= 0) || (underscan_height_ <= 0)) {
154     return;
155   }
156 
157   float width_ratio = FLOAT(underscan_width_) / 100.0f;
158   float height_ratio = FLOAT(underscan_height_) / 100.0f;
159 
160   uint32_t mixer_width = 0;
161   uint32_t mixer_height = 0;
162   GetMixerResolution(&mixer_width, &mixer_height);
163 
164   if (mixer_width == 0 || mixer_height == 0) {
165     DLOGV("Invalid mixer dimensions (%d, %d)", mixer_width, mixer_height);
166     return;
167   }
168 
169   uint32_t new_mixer_width = UINT32(mixer_width * FLOAT(1.0f - width_ratio));
170   uint32_t new_mixer_height = UINT32(mixer_height * FLOAT(1.0f - height_ratio));
171 
172   int x_offset = INT((FLOAT(mixer_width) * width_ratio) / 2.0f);
173   int y_offset = INT((FLOAT(mixer_height) * height_ratio) / 2.0f);
174 
175   display_frame->left = (display_frame->left * INT32(new_mixer_width) / INT32(mixer_width))
176                         + x_offset;
177   display_frame->top = (display_frame->top * INT32(new_mixer_height) / INT32(mixer_height)) +
178                        y_offset;
179   display_frame->right = ((display_frame->right * INT32(new_mixer_width)) / INT32(mixer_width)) +
180                          x_offset;
181   display_frame->bottom = ((display_frame->bottom * INT32(new_mixer_height)) / INT32(mixer_height))
182                           + y_offset;
183 }
184 
SetSecureDisplay(bool secure_display_active)185 void HWCDisplayExternal::SetSecureDisplay(bool secure_display_active) {
186   if (secure_display_active_ != secure_display_active) {
187     secure_display_active_ = secure_display_active;
188 
189     if (secure_display_active_) {
190       DisplayError error = display_intf_->Flush();
191       validated_ = false;
192       if (error != kErrorNone) {
193         DLOGE("Flush failed. Error = %d", error);
194       }
195     }
196   }
197   return;
198 }
199 
AdjustSourceResolution(uint32_t dst_width,uint32_t dst_height,uint32_t * src_width,uint32_t * src_height)200 static void AdjustSourceResolution(uint32_t dst_width, uint32_t dst_height, uint32_t *src_width,
201                                    uint32_t *src_height) {
202   *src_height = (dst_width * (*src_height)) / (*src_width);
203   *src_width = dst_width;
204 }
205 
GetDownscaleResolution(uint32_t primary_width,uint32_t primary_height,uint32_t * non_primary_width,uint32_t * non_primary_height)206 void HWCDisplayExternal::GetDownscaleResolution(uint32_t primary_width, uint32_t primary_height,
207                                                 uint32_t *non_primary_width,
208                                                 uint32_t *non_primary_height) {
209   uint32_t primary_area = primary_width * primary_height;
210   uint32_t non_primary_area = (*non_primary_width) * (*non_primary_height);
211 
212   if (primary_area > non_primary_area) {
213     if (primary_height > primary_width) {
214       std::swap(primary_height, primary_width);
215     }
216     AdjustSourceResolution(primary_width, primary_height, non_primary_width, non_primary_height);
217   }
218 }
219 
SetState(bool connected)220 int HWCDisplayExternal::SetState(bool connected) {
221   DisplayError error = kErrorNone;
222   DisplayState state = kStateOff;
223   DisplayConfigVariableInfo fb_config = {};
224 
225   if (connected) {
226     if (display_null_.IsActive()) {
227       error = core_intf_->CreateDisplay(type_, this, &display_intf_);
228       if (error != kErrorNone) {
229         DLOGE("Display create failed. Error = %d display_type %d event_handler %p disp_intf %p",
230               error, type_, this, &display_intf_);
231         return -EINVAL;
232       }
233 
234       // Restore HDMI attributes when display is reconnected.
235       // This is to ensure that surfaceflinger & sdm are in sync.
236       display_null_.GetFrameBufferConfig(&fb_config);
237       int status = SetFrameBufferResolution(fb_config.x_pixels, fb_config.y_pixels);
238       if (status) {
239         DLOGW("Set frame buffer config failed. Error = %d", error);
240         return -1;
241       }
242       int release_fence = -1;
243       display_null_.GetDisplayState(&state);
244       display_intf_->SetDisplayState(state, &release_fence);
245       if (release_fence >= 0) {
246         ::close(release_fence);
247       }
248       validated_ = false;
249 
250       SetVsyncEnabled(HWC2::Vsync::Enable);
251 
252       display_null_.SetActive(false);
253       DLOGI("Display is connected successfully.");
254     } else {
255       DLOGI("Display is already connected.");
256     }
257   } else {
258     if (!display_null_.IsActive()) {
259       int release_fence = -1;
260       // Preserve required attributes of HDMI display that surfaceflinger sees.
261       // Restore HDMI attributes when display is reconnected.
262       display_intf_->GetDisplayState(&state);
263       display_null_.SetDisplayState(state, &release_fence);
264       if (release_fence >= 0) {
265         ::close(release_fence);
266       }
267 
268       error = display_intf_->GetFrameBufferConfig(&fb_config);
269       if (error != kErrorNone) {
270         DLOGW("Get frame buffer config failed. Error = %d", error);
271         return -1;
272       }
273       display_null_.SetFrameBufferConfig(fb_config);
274 
275       SetVsyncEnabled(HWC2::Vsync::Disable);
276       core_intf_->DestroyDisplay(display_intf_);
277       display_intf_ = &display_null_;
278 
279       display_null_.SetActive(true);
280       DLOGI("Display is disconnected successfully.");
281     } else {
282       DLOGI("Display is already disconnected.");
283     }
284   }
285 
286   return 0;
287 }
288 
GetUnderScanConfig()289 void HWCDisplayExternal::GetUnderScanConfig() {
290   if (!display_intf_->IsUnderscanSupported()) {
291     // Read user defined underscan width and height
292     HWCDebugHandler::Get()->GetProperty(EXTERNAL_ACTION_SAFE_WIDTH_PROP, &underscan_width_);
293     HWCDebugHandler::Get()->GetProperty(EXTERNAL_ACTION_SAFE_HEIGHT_PROP, &underscan_height_);
294   }
295 }
296 
Flush()297 DisplayError HWCDisplayExternal::Flush() {
298   return display_intf_->Flush();
299 }
300 
301 }  // namespace sdm
302