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,hwc_procs_t const ** hwc_procs,qService::QService * qservice,HWCDisplay ** hwc_display)42 int HWCDisplayExternal::Create(CoreInterface *core_intf, hwc_procs_t const **hwc_procs,
43 qService::QService *qservice, HWCDisplay **hwc_display) {
44 return Create(core_intf, hwc_procs, 0, 0, qservice, false, hwc_display);
45 }
46
Create(CoreInterface * core_intf,hwc_procs_t const ** hwc_procs,uint32_t primary_width,uint32_t primary_height,qService::QService * qservice,bool use_primary_res,HWCDisplay ** hwc_display)47 int HWCDisplayExternal::Create(CoreInterface *core_intf, hwc_procs_t const **hwc_procs,
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 int drc_enabled = 0;
54 DisplayError error = kErrorNone;
55
56 HWCDisplay *hwc_display_external = new HWCDisplayExternal(core_intf, hwc_procs, qservice);
57 int status = hwc_display_external->Init();
58 if (status) {
59 delete hwc_display_external;
60 return status;
61 }
62
63 error = hwc_display_external->GetMixerResolution(&external_width, &external_height);
64 if (error != kErrorNone) {
65 return -EINVAL;
66 }
67
68 if (primary_width && primary_height) {
69 // use_primary_res means HWCDisplayExternal should directly set framebuffer resolution to the
70 // provided primary_width and primary_height
71 if (use_primary_res) {
72 external_width = primary_width;
73 external_height = primary_height;
74 } else {
75 int downscale_enabled = 0;
76 HWCDebugHandler::Get()->GetProperty("sdm.debug.downscale_external", &downscale_enabled);
77 if (downscale_enabled) {
78 GetDownscaleResolution(primary_width, primary_height, &external_width, &external_height);
79 }
80 }
81 }
82
83 status = hwc_display_external->SetFrameBufferResolution(external_width, external_height);
84 if (status) {
85 Destroy(hwc_display_external);
86 return status;
87 }
88
89 HWCDebugHandler::Get()->GetProperty("sdm.hdmi.drc_enabled", &(drc_enabled));
90 reinterpret_cast<HWCDisplayExternal *>(hwc_display_external)->drc_enabled_ = drc_enabled;
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,hwc_procs_t const ** hwc_procs,qService::QService * qservice)102 HWCDisplayExternal::HWCDisplayExternal(CoreInterface *core_intf, hwc_procs_t const **hwc_procs,
103 qService::QService *qservice)
104 : HWCDisplay(core_intf, hwc_procs, kHDMI, HWC_DISPLAY_EXTERNAL, false, qservice,
105 DISPLAY_CLASS_EXTERNAL) {
106 }
107
Prepare(hwc_display_contents_1_t * content_list)108 int HWCDisplayExternal::Prepare(hwc_display_contents_1_t *content_list) {
109 int status = 0;
110 DisplayError error = kErrorNone;
111
112 if (secure_display_active_) {
113 MarkLayersForGPUBypass(content_list);
114 return status;
115 }
116
117 status = AllocateLayerStack(content_list);
118 if (status) {
119 return status;
120 }
121
122 status = PrePrepareLayerStack(content_list);
123 if (status) {
124 return status;
125 }
126
127 if (content_list->numHwLayers <= 1) {
128 flush_ = true;
129 return 0;
130 }
131
132 bool one_video_updating_layer = SingleVideoLayerUpdating(UINT32(content_list->numHwLayers - 1));
133
134 if (current_refresh_rate_ != metadata_refresh_rate_ && one_video_updating_layer && drc_enabled_) {
135 error = display_intf_->SetRefreshRate(metadata_refresh_rate_);
136 }
137
138 if (error == kErrorNone) {
139 // On success, set current refresh rate to new refresh rate
140 current_refresh_rate_ = metadata_refresh_rate_;
141 }
142
143
144 status = PrepareLayerStack(content_list);
145 if (status) {
146 return status;
147 }
148
149 return 0;
150 }
151
Commit(hwc_display_contents_1_t * content_list)152 int HWCDisplayExternal::Commit(hwc_display_contents_1_t *content_list) {
153 int status = 0;
154
155 if (secure_display_active_) {
156 return status;
157 }
158
159 status = HWCDisplay::CommitLayerStack(content_list);
160 if (status) {
161 return status;
162 }
163
164 status = HWCDisplay::PostCommitLayerStack(content_list);
165 if (status) {
166 return status;
167 }
168
169 return 0;
170 }
171
ApplyScanAdjustment(hwc_rect_t * display_frame)172 void HWCDisplayExternal::ApplyScanAdjustment(hwc_rect_t *display_frame) {
173 if (display_intf_->IsUnderscanSupported()) {
174 return;
175 }
176
177 // Read user defined width and height ratio
178 int width = 0, height = 0;
179 HWCDebugHandler::Get()->GetProperty("sdm.external_action_safe_width", &width);
180 float width_ratio = FLOAT(width) / 100.0f;
181 HWCDebugHandler::Get()->GetProperty("sdm.external_action_safe_height", &height);
182 float height_ratio = FLOAT(height) / 100.0f;
183
184 if (width_ratio == 0.0f || height_ratio == 0.0f) {
185 return;
186 }
187
188 uint32_t mixer_width = 0;
189 uint32_t mixer_height = 0;
190 GetMixerResolution(&mixer_width, &mixer_height);
191
192 if (mixer_width == 0 || mixer_height == 0) {
193 DLOGV("Invalid mixer dimensions (%d, %d)", mixer_width, mixer_height);
194 return;
195 }
196
197 uint32_t new_mixer_width = UINT32(mixer_width * FLOAT(1.0f - width_ratio));
198 uint32_t new_mixer_height = UINT32(mixer_height * FLOAT(1.0f - height_ratio));
199
200 int x_offset = INT((FLOAT(mixer_width) * width_ratio) / 2.0f);
201 int y_offset = INT((FLOAT(mixer_height) * height_ratio) / 2.0f);
202
203 display_frame->left = (display_frame->left * INT32(new_mixer_width) / INT32(mixer_width))
204 + x_offset;
205 display_frame->top = (display_frame->top * INT32(new_mixer_height) / INT32(mixer_height)) +
206 y_offset;
207 display_frame->right = ((display_frame->right * INT32(new_mixer_width)) / INT32(mixer_width)) +
208 x_offset;
209 display_frame->bottom = ((display_frame->bottom * INT32(new_mixer_height)) / INT32(mixer_height))
210 + y_offset;
211 }
212
SetSecureDisplay(bool secure_display_active)213 void HWCDisplayExternal::SetSecureDisplay(bool secure_display_active) {
214 if (secure_display_active_ != secure_display_active) {
215 secure_display_active_ = secure_display_active;
216
217 if (secure_display_active_) {
218 DisplayError error = display_intf_->Flush();
219 if (error != kErrorNone) {
220 DLOGE("Flush failed. Error = %d", error);
221 }
222 }
223 }
224 return;
225 }
226
AdjustSourceResolution(uint32_t dst_width,uint32_t dst_height,uint32_t * src_width,uint32_t * src_height)227 static void AdjustSourceResolution(uint32_t dst_width, uint32_t dst_height, uint32_t *src_width,
228 uint32_t *src_height) {
229 *src_height = (dst_width * (*src_height)) / (*src_width);
230 *src_width = dst_width;
231 }
232
GetDownscaleResolution(uint32_t primary_width,uint32_t primary_height,uint32_t * non_primary_width,uint32_t * non_primary_height)233 void HWCDisplayExternal::GetDownscaleResolution(uint32_t primary_width, uint32_t primary_height,
234 uint32_t *non_primary_width, uint32_t *non_primary_height) {
235 uint32_t primary_area = primary_width * primary_height;
236 uint32_t non_primary_area = (*non_primary_width) * (*non_primary_height);
237
238 if (primary_area > non_primary_area) {
239 if (primary_height > primary_width) {
240 std::swap(primary_height, primary_width);
241 }
242 AdjustSourceResolution(primary_width, primary_height, non_primary_width, non_primary_height);
243 }
244 }
245
RoundToStandardFPS(float fps)246 uint32_t HWCDisplayExternal::RoundToStandardFPS(float fps) {
247 static const uint32_t standard_fps[] = {23976, 24000, 25000, 29970, 30000, 50000, 59940, 60000};
248 static const uint32_t mapping_fps[] = {59940, 60000, 60000, 59940, 60000, 50000, 59940, 60000};
249 uint32_t frame_rate = (uint32_t)(fps * 1000);
250
251 int count = INT(sizeof(standard_fps) / sizeof(standard_fps[0]));
252 for (int i = 0; i < count; i++) {
253 // Most likely used for video, the fps for frames should be stable from video side.
254 if (standard_fps[i] > frame_rate) {
255 if (i > 0) {
256 if ((standard_fps[i] - frame_rate) > (frame_rate - standard_fps[i-1])) {
257 return mapping_fps[i-1];
258 } else {
259 return mapping_fps[i];
260 }
261 } else {
262 return mapping_fps[i];
263 }
264 }
265 }
266
267 return standard_fps[count - 1];
268 }
269
PrepareDynamicRefreshRate(Layer * layer)270 void HWCDisplayExternal::PrepareDynamicRefreshRate(Layer *layer) {
271 if (layer->input_buffer->flags.video) {
272 metadata_refresh_rate_ = SanitizeRefreshRate(layer->frame_rate);
273 layer->frame_rate = current_refresh_rate_;
274 }
275 }
276
277 } // namespace sdm
278
279