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 
34 #include "hwc_display_external.h"
35 #include "hwc_debugger.h"
36 
37 #define __CLASS__ "HWCDisplayExternal"
38 
39 namespace sdm {
40 
Create(CoreInterface * core_intf,hwc_procs_t const ** hwc_procs,uint32_t primary_width,uint32_t primary_height,HWCDisplay ** hwc_display)41 int HWCDisplayExternal::Create(CoreInterface *core_intf, hwc_procs_t const **hwc_procs,
42                                uint32_t primary_width, uint32_t primary_height,
43                                HWCDisplay **hwc_display) {
44   uint32_t external_width = 0;
45   uint32_t external_height = 0;
46 
47   HWCDisplay *hwc_display_external = new HWCDisplayExternal(core_intf, hwc_procs);
48   int status = hwc_display_external->Init();
49   if (status) {
50     delete hwc_display_external;
51     return status;
52   }
53 
54   hwc_display_external->GetPanelResolution(&external_width, &external_height);
55 
56   int downscale_enabled = 0;
57   HWCDebugHandler::Get()->GetProperty("sdm.debug.downscale_external", &downscale_enabled);
58   if (downscale_enabled) {
59     GetDownscaleResolution(primary_width, primary_height, &external_width, &external_height);
60   }
61 
62   status = hwc_display_external->SetFrameBufferResolution(external_width, external_height);
63   if (status) {
64     Destroy(hwc_display_external);
65     return status;
66   }
67 
68   *hwc_display = hwc_display_external;
69 
70   return status;
71 }
72 
Destroy(HWCDisplay * hwc_display)73 void HWCDisplayExternal::Destroy(HWCDisplay *hwc_display) {
74   hwc_display->Deinit();
75   delete hwc_display;
76 }
77 
HWCDisplayExternal(CoreInterface * core_intf,hwc_procs_t const ** hwc_procs)78 HWCDisplayExternal::HWCDisplayExternal(CoreInterface *core_intf, hwc_procs_t const **hwc_procs)
79   : HWCDisplay(core_intf, hwc_procs, kHDMI, HWC_DISPLAY_EXTERNAL, false) {
80 }
81 
Prepare(hwc_display_contents_1_t * content_list)82 int HWCDisplayExternal::Prepare(hwc_display_contents_1_t *content_list) {
83   int status = 0;
84 
85   if (secure_display_active_) {
86     MarkLayersForGPUBypass(content_list);
87     return status;
88   }
89 
90   status = AllocateLayerStack(content_list);
91   if (status) {
92     return status;
93   }
94 
95   status = PrePrepareLayerStack(content_list);
96   if (status) {
97     return status;
98   }
99 
100   if (content_list->numHwLayers <= 1) {
101     flush_ = true;
102     return 0;
103   }
104 
105   status = PrepareLayerStack(content_list);
106   if (status) {
107     return status;
108   }
109 
110   return 0;
111 }
112 
Commit(hwc_display_contents_1_t * content_list)113 int HWCDisplayExternal::Commit(hwc_display_contents_1_t *content_list) {
114   int status = 0;
115 
116   if (secure_display_active_) {
117     return status;
118   }
119 
120   status = HWCDisplay::CommitLayerStack(content_list);
121   if (status) {
122     return status;
123   }
124 
125   status = HWCDisplay::PostCommitLayerStack(content_list);
126   if (status) {
127     return status;
128   }
129 
130   return 0;
131 }
132 
ApplyScanAdjustment(hwc_rect_t * display_frame)133 void HWCDisplayExternal::ApplyScanAdjustment(hwc_rect_t *display_frame) {
134   if (display_intf_->IsUnderscanSupported()) {
135     return;
136   }
137 
138   // Read user defined width and height ratio
139   int width = 0, height = 0;
140   HWCDebugHandler::Get()->GetProperty("sdm.external_action_safe_width", &width);
141   float width_ratio = FLOAT(width) / 100.0f;
142   HWCDebugHandler::Get()->GetProperty("sdm.external_action_safe_height", &height);
143   float height_ratio = FLOAT(height) / 100.0f;
144 
145   if (width_ratio == 0.0f ||  height_ratio == 0.0f) {
146     return;
147   }
148 
149   uint32_t panel_width = 0;
150   uint32_t panel_height = 0;
151   GetPanelResolution(&panel_width, &panel_height);
152 
153   if (panel_width == 0 || panel_height == 0) {
154     DLOGV("Invalid panel dimensions (%d, %d)", panel_width, panel_height);
155     return;
156   }
157 
158   uint32_t new_panel_width = panel_width * UINT32(1.0f - width_ratio);
159   uint32_t new_panel_height = panel_height * UINT32(1.0f - height_ratio);
160 
161   int x_offset = INT((FLOAT(panel_width) * width_ratio) / 2.0f);
162   int y_offset = INT((FLOAT(panel_height) * height_ratio) / 2.0f);
163 
164   display_frame->left = (display_frame->left * INT32(new_panel_width) / INT32(panel_width))
165                         + x_offset;
166   display_frame->top = (display_frame->top * INT32(new_panel_height) / INT32(panel_height)) +
167                        y_offset;
168   display_frame->right = ((display_frame->right * INT32(new_panel_width)) / INT32(panel_width)) +
169                          x_offset;
170   display_frame->bottom = ((display_frame->bottom * INT32(new_panel_height)) / INT32(panel_height))
171                           + y_offset;
172 }
173 
SetSecureDisplay(bool secure_display_active)174 void HWCDisplayExternal::SetSecureDisplay(bool secure_display_active) {
175   if (secure_display_active_ != secure_display_active) {
176     secure_display_active_ = secure_display_active;
177 
178     if (secure_display_active_) {
179       DisplayError error = display_intf_->Flush();
180       if (error != kErrorNone) {
181         DLOGE("Flush failed. Error = %d", error);
182       }
183     }
184   }
185   return;
186 }
187 
AdjustSourceResolution(uint32_t dst_width,uint32_t dst_height,uint32_t * src_width,uint32_t * src_height)188 static void AdjustSourceResolution(uint32_t dst_width, uint32_t dst_height, uint32_t *src_width,
189                                    uint32_t *src_height) {
190   *src_height = (dst_width * (*src_height)) / (*src_width);
191   *src_width = dst_width;
192 }
193 
GetDownscaleResolution(uint32_t primary_width,uint32_t primary_height,uint32_t * non_primary_width,uint32_t * non_primary_height)194 void HWCDisplayExternal::GetDownscaleResolution(uint32_t primary_width, uint32_t primary_height,
195                                         uint32_t *non_primary_width, 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       Swap(primary_height, primary_width);
202     }
203     AdjustSourceResolution(primary_width, primary_height, non_primary_width, non_primary_height);
204   }
205 }
206 
207 }  // namespace sdm
208 
209