1 /*
2 * Copyright (c) 2014-2019, 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_pluggable.h"
36 #include "hwc_debugger.h"
37
38 #define __CLASS__ "HWCDisplayPluggable"
39
40 namespace sdm {
41
Create(CoreInterface * core_intf,HWCBufferAllocator * buffer_allocator,HWCCallbacks * callbacks,HWCDisplayEventHandler * event_handler,qService::QService * qservice,hwc2_display_t id,int32_t sdm_id,uint32_t primary_width,uint32_t primary_height,bool use_primary_res,HWCDisplay ** hwc_display)42 int HWCDisplayPluggable::Create(CoreInterface *core_intf, HWCBufferAllocator *buffer_allocator,
43 HWCCallbacks *callbacks, HWCDisplayEventHandler *event_handler,
44 qService::QService *qservice, hwc2_display_t id, int32_t sdm_id,
45 uint32_t primary_width, uint32_t primary_height,
46 bool use_primary_res, HWCDisplay **hwc_display) {
47 uint32_t pluggable_width = 0;
48 uint32_t pluggable_height = 0;
49 DisplayError error = kErrorNone;
50
51 HWCDisplay *hwc_display_pluggable = new HWCDisplayPluggable(core_intf, buffer_allocator,
52 callbacks, event_handler, qservice, id, sdm_id);
53 int status = hwc_display_pluggable->Init();
54 if (status) {
55 delete hwc_display_pluggable;
56 return status;
57 }
58
59 error = hwc_display_pluggable->GetMixerResolution(&pluggable_width, &pluggable_height);
60 if (error != kErrorNone) {
61 Destroy(hwc_display_pluggable);
62 return -EINVAL;
63 }
64
65 if (primary_width && primary_height) {
66 // use_primary_res means HWCDisplayPluggable should directly set framebuffer resolution to the
67 // provided primary_width and primary_height
68 if (use_primary_res) {
69 pluggable_width = primary_width;
70 pluggable_height = primary_height;
71 } else {
72 int downscale_enabled = 0;
73 HWCDebugHandler::Get()->GetProperty(ENABLE_EXTERNAL_DOWNSCALE_PROP, &downscale_enabled);
74 if (downscale_enabled) {
75 GetDownscaleResolution(primary_width, primary_height, &pluggable_width, &pluggable_height);
76 }
77 }
78 }
79
80 status = hwc_display_pluggable->SetFrameBufferResolution(pluggable_width, pluggable_height);
81 if (status) {
82 Destroy(hwc_display_pluggable);
83 return status;
84 }
85
86 *hwc_display = hwc_display_pluggable;
87
88 return status;
89 }
90
Init()91 int HWCDisplayPluggable::Init() {
92 int status = HWCDisplay::Init();
93 if (status) {
94 return status;
95 }
96 color_mode_ = new HWCColorMode(display_intf_);
97 color_mode_->Init();
98
99 return status;
100 }
101
Destroy(HWCDisplay * hwc_display)102 void HWCDisplayPluggable::Destroy(HWCDisplay *hwc_display) {
103 // Flush the display to have outstanding fences signaled.
104 hwc_display->Flush();
105 hwc_display->Deinit();
106 delete hwc_display;
107 }
108
HWCDisplayPluggable(CoreInterface * core_intf,HWCBufferAllocator * buffer_allocator,HWCCallbacks * callbacks,HWCDisplayEventHandler * event_handler,qService::QService * qservice,hwc2_display_t id,int32_t sdm_id)109 HWCDisplayPluggable::HWCDisplayPluggable(CoreInterface *core_intf,
110 HWCBufferAllocator *buffer_allocator,
111 HWCCallbacks *callbacks,
112 HWCDisplayEventHandler *event_handler,
113 qService::QService *qservice,
114 hwc2_display_t id,
115 int32_t sdm_id)
116 : HWCDisplay(core_intf, buffer_allocator, callbacks, event_handler, qservice, kPluggable, id,
117 sdm_id, false, DISPLAY_CLASS_PLUGGABLE) {
118 }
119
Validate(uint32_t * out_num_types,uint32_t * out_num_requests)120 HWC2::Error HWCDisplayPluggable::Validate(uint32_t *out_num_types, uint32_t *out_num_requests) {
121 auto status = HWC2::Error::None;
122
123 if (active_secure_sessions_[kSecureDisplay]) {
124 MarkLayersForGPUBypass();
125 return status;
126 }
127
128 BuildLayerStack();
129
130 if (layer_set_.empty()) {
131 flush_ = true;
132 validated_ = true;
133 return status;
134 }
135
136 // Apply current Color Mode and Render Intent.
137 if (color_mode_->ApplyCurrentColorModeWithRenderIntent(
138 static_cast<bool>(layer_stack_.flags.hdr_present)) != HWC2::Error::None) {
139 // Fallback to GPU Composition, if Color Mode can't be applied.
140 MarkLayersForClientComposition();
141 }
142
143 // TODO(user): SetRefreshRate need to follow new interface when added.
144
145 status = PrepareLayerStack(out_num_types, out_num_requests);
146 return status;
147 }
148
Present(int32_t * out_retire_fence)149 HWC2::Error HWCDisplayPluggable::Present(int32_t *out_retire_fence) {
150 auto status = HWC2::Error::None;
151
152 if (!active_secure_sessions_[kSecureDisplay]) {
153 status = HWCDisplay::CommitLayerStack();
154 if (status == HWC2::Error::None) {
155 status = HWCDisplay::PostCommitLayerStack(out_retire_fence);
156 }
157 }
158 return status;
159 }
160
ApplyScanAdjustment(hwc_rect_t * display_frame)161 void HWCDisplayPluggable::ApplyScanAdjustment(hwc_rect_t *display_frame) {
162 if ((underscan_width_ <= 0) || (underscan_height_ <= 0)) {
163 return;
164 }
165
166 float width_ratio = FLOAT(underscan_width_) / 100.0f;
167 float height_ratio = FLOAT(underscan_height_) / 100.0f;
168
169 uint32_t mixer_width = 0;
170 uint32_t mixer_height = 0;
171 GetMixerResolution(&mixer_width, &mixer_height);
172
173 if (mixer_width == 0 || mixer_height == 0) {
174 DLOGV("Invalid mixer dimensions (%d, %d)", mixer_width, mixer_height);
175 return;
176 }
177
178 uint32_t new_mixer_width = UINT32(mixer_width * FLOAT(1.0f - width_ratio));
179 uint32_t new_mixer_height = UINT32(mixer_height * FLOAT(1.0f - height_ratio));
180
181 int x_offset = INT((FLOAT(mixer_width) * width_ratio) / 2.0f);
182 int y_offset = INT((FLOAT(mixer_height) * height_ratio) / 2.0f);
183
184 display_frame->left = (display_frame->left * INT32(new_mixer_width) / INT32(mixer_width))
185 + x_offset;
186 display_frame->top = (display_frame->top * INT32(new_mixer_height) / INT32(mixer_height)) +
187 y_offset;
188 display_frame->right = ((display_frame->right * INT32(new_mixer_width)) / INT32(mixer_width)) +
189 x_offset;
190 display_frame->bottom = ((display_frame->bottom * INT32(new_mixer_height)) / INT32(mixer_height))
191 + y_offset;
192 }
193
AdjustSourceResolution(uint32_t dst_width,uint32_t dst_height,uint32_t * src_width,uint32_t * src_height)194 static void AdjustSourceResolution(uint32_t dst_width, uint32_t dst_height, uint32_t *src_width,
195 uint32_t *src_height) {
196 *src_height = (dst_width * (*src_height)) / (*src_width);
197 *src_width = dst_width;
198 }
199
GetDownscaleResolution(uint32_t primary_width,uint32_t primary_height,uint32_t * non_primary_width,uint32_t * non_primary_height)200 void HWCDisplayPluggable::GetDownscaleResolution(uint32_t primary_width, uint32_t primary_height,
201 uint32_t *non_primary_width,
202 uint32_t *non_primary_height) {
203 uint32_t primary_area = primary_width * primary_height;
204 uint32_t non_primary_area = (*non_primary_width) * (*non_primary_height);
205
206 if (primary_area > non_primary_area) {
207 if (primary_height > primary_width) {
208 std::swap(primary_height, primary_width);
209 }
210 AdjustSourceResolution(primary_width, primary_height, non_primary_width, non_primary_height);
211 }
212 }
213
SetState(bool connected)214 int HWCDisplayPluggable::SetState(bool connected) {
215 DisplayError error = kErrorNone;
216 DisplayState state = kStateOff;
217 DisplayConfigVariableInfo fb_config = {};
218
219 if (connected) {
220 if (display_null_.IsActive()) {
221 error = core_intf_->CreateDisplay(type_, this, &display_intf_);
222 if (error != kErrorNone) {
223 DLOGE("Display create failed. Error = %d display_type %d event_handler %p disp_intf %p",
224 error, type_, this, &display_intf_);
225 return -EINVAL;
226 }
227
228 // Restore HDMI attributes when display is reconnected.
229 // This is to ensure that surfaceflinger & sdm are in sync.
230 display_null_.GetFrameBufferConfig(&fb_config);
231 int status = SetFrameBufferResolution(fb_config.x_pixels, fb_config.y_pixels);
232 if (status) {
233 DLOGW("Set frame buffer config failed. Error = %d", error);
234 return -1;
235 }
236 int release_fence = -1;
237 display_null_.GetDisplayState(&state);
238 display_intf_->SetDisplayState(state, false /* teardown */, &release_fence);
239 if (release_fence >= 0) {
240 ::close(release_fence);
241 }
242 validated_ = false;
243
244 SetVsyncEnabled(HWC2::Vsync::Enable);
245
246 display_null_.SetActive(false);
247 DLOGI("Display is connected successfully.");
248 } else {
249 DLOGI("Display is already connected.");
250 }
251 } else {
252 if (!display_null_.IsActive()) {
253 int release_fence = -1;
254 // Preserve required attributes of HDMI display that surfaceflinger sees.
255 // Restore HDMI attributes when display is reconnected.
256 display_intf_->GetDisplayState(&state);
257 display_null_.SetDisplayState(state, false /* teardown */, &release_fence);
258 if (release_fence >= 0) {
259 ::close(release_fence);
260 }
261
262 error = display_intf_->GetFrameBufferConfig(&fb_config);
263 if (error != kErrorNone) {
264 DLOGW("Get frame buffer config failed. Error = %d", error);
265 return -1;
266 }
267 display_null_.SetFrameBufferConfig(fb_config);
268
269 SetVsyncEnabled(HWC2::Vsync::Disable);
270 core_intf_->DestroyDisplay(display_intf_);
271 display_intf_ = &display_null_;
272
273 display_null_.SetActive(true);
274 DLOGI("Display is disconnected successfully.");
275 } else {
276 DLOGI("Display is already disconnected.");
277 }
278 }
279
280 return 0;
281 }
282
GetUnderScanConfig()283 void HWCDisplayPluggable::GetUnderScanConfig() {
284 if (!display_intf_->IsUnderscanSupported()) {
285 // Read user defined underscan width and height
286 HWCDebugHandler::Get()->GetProperty(EXTERNAL_ACTION_SAFE_WIDTH_PROP, &underscan_width_);
287 HWCDebugHandler::Get()->GetProperty(EXTERNAL_ACTION_SAFE_HEIGHT_PROP, &underscan_height_);
288 }
289 }
290
Flush()291 DisplayError HWCDisplayPluggable::Flush() {
292 return display_intf_->Flush(&layer_stack_);
293 }
294
GetColorModes(uint32_t * out_num_modes,ColorMode * out_modes)295 HWC2::Error HWCDisplayPluggable::GetColorModes(uint32_t *out_num_modes, ColorMode *out_modes) {
296 if (out_modes == nullptr) {
297 *out_num_modes = color_mode_->GetColorModeCount();
298 } else {
299 color_mode_->GetColorModes(out_num_modes, out_modes);
300 }
301 return HWC2::Error::None;
302 }
303
GetRenderIntents(ColorMode mode,uint32_t * out_num_intents,RenderIntent * out_intents)304 HWC2::Error HWCDisplayPluggable::GetRenderIntents(ColorMode mode, uint32_t *out_num_intents,
305 RenderIntent *out_intents) {
306 if (out_intents == nullptr) {
307 *out_num_intents = color_mode_->GetRenderIntentCount(mode);
308 } else {
309 color_mode_->GetRenderIntents(mode, out_num_intents, out_intents);
310 }
311 return HWC2::Error::None;
312 }
313
SetColorMode(ColorMode mode)314 HWC2::Error HWCDisplayPluggable::SetColorMode(ColorMode mode) {
315 return SetColorModeWithRenderIntent(mode, RenderIntent::COLORIMETRIC);
316 }
317
SetColorModeWithRenderIntent(ColorMode mode,RenderIntent intent)318 HWC2::Error HWCDisplayPluggable::SetColorModeWithRenderIntent(ColorMode mode, RenderIntent intent) {
319 auto status = color_mode_->CacheColorModeWithRenderIntent(mode, intent);
320 if (status != HWC2::Error::None) {
321 DLOGE("failed for mode = %d intent = %d", mode, intent);
322 return status;
323 }
324
325 callbacks_->Refresh(id_);
326 validated_ = false;
327
328 return status;
329 }
330
331 } // namespace sdm
332