1 /*
2 * Copyright (c) 2014-2020, 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, 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_ = !client_connected_;
132     validated_ = true;
133     return status;
134   }
135 
136   // Apply current Color Mode and Render Intent.
137   status = color_mode_->ApplyCurrentColorModeWithRenderIntent(
138                                                  static_cast<bool>(layer_stack_.flags.hdr_present));
139   if (status != HWC2::Error::None || has_color_tranform_) {
140     // Fallback to GPU Composition if Color Mode can't be applied or if a color tranform needs to be
141     // applied.
142     MarkLayersForClientComposition();
143   }
144 
145   // TODO(user): SetRefreshRate need to follow new interface when added.
146 
147   status = PrepareLayerStack(out_num_types, out_num_requests);
148   return status;
149 }
150 
Present(shared_ptr<Fence> * out_retire_fence)151 HWC2::Error HWCDisplayPluggable::Present(shared_ptr<Fence> *out_retire_fence) {
152   auto status = HWC2::Error::None;
153 
154   if (!active_secure_sessions_[kSecureDisplay]) {
155     status = HWCDisplay::CommitLayerStack();
156     if (status == HWC2::Error::None) {
157       status = HWCDisplay::PostCommitLayerStack(out_retire_fence);
158     }
159   }
160   return status;
161 }
162 
ApplyScanAdjustment(hwc_rect_t * display_frame)163 void HWCDisplayPluggable::ApplyScanAdjustment(hwc_rect_t *display_frame) {
164   if ((underscan_width_ <= 0) || (underscan_height_ <= 0)) {
165     return;
166   }
167 
168   float width_ratio = FLOAT(underscan_width_) / 100.0f;
169   float height_ratio = FLOAT(underscan_height_) / 100.0f;
170 
171   uint32_t mixer_width = 0;
172   uint32_t mixer_height = 0;
173   GetMixerResolution(&mixer_width, &mixer_height);
174 
175   if (mixer_width == 0 || mixer_height == 0) {
176     DLOGV("Invalid mixer dimensions (%d, %d)", mixer_width, mixer_height);
177     return;
178   }
179 
180   uint32_t new_mixer_width = UINT32(mixer_width * FLOAT(1.0f - width_ratio));
181   uint32_t new_mixer_height = UINT32(mixer_height * FLOAT(1.0f - height_ratio));
182 
183   int x_offset = INT((FLOAT(mixer_width) * width_ratio) / 2.0f);
184   int y_offset = INT((FLOAT(mixer_height) * height_ratio) / 2.0f);
185 
186   display_frame->left = (display_frame->left * INT32(new_mixer_width) / INT32(mixer_width))
187                         + x_offset;
188   display_frame->top = (display_frame->top * INT32(new_mixer_height) / INT32(mixer_height)) +
189                        y_offset;
190   display_frame->right = ((display_frame->right * INT32(new_mixer_width)) / INT32(mixer_width)) +
191                          x_offset;
192   display_frame->bottom = ((display_frame->bottom * INT32(new_mixer_height)) / INT32(mixer_height))
193                           + y_offset;
194 }
195 
AdjustSourceResolution(uint32_t dst_width,uint32_t dst_height,uint32_t * src_width,uint32_t * src_height)196 static void AdjustSourceResolution(uint32_t dst_width, uint32_t dst_height, uint32_t *src_width,
197                                    uint32_t *src_height) {
198   *src_height = (dst_width * (*src_height)) / (*src_width);
199   *src_width = dst_width;
200 }
201 
GetDownscaleResolution(uint32_t primary_width,uint32_t primary_height,uint32_t * non_primary_width,uint32_t * non_primary_height)202 void HWCDisplayPluggable::GetDownscaleResolution(uint32_t primary_width, uint32_t primary_height,
203                                                 uint32_t *non_primary_width,
204                                                 uint32_t *non_primary_height) {
205   uint32_t primary_area = primary_width * primary_height;
206   uint32_t non_primary_area = (*non_primary_width) * (*non_primary_height);
207 
208   if (primary_area > non_primary_area) {
209     if (primary_height > primary_width) {
210       std::swap(primary_height, primary_width);
211     }
212     AdjustSourceResolution(primary_width, primary_height, non_primary_width, non_primary_height);
213   }
214 }
215 
SetState(bool connected)216 int HWCDisplayPluggable::SetState(bool connected) {
217   DisplayError error = kErrorNone;
218   DisplayState state = kStateOff;
219   DisplayConfigVariableInfo fb_config = {};
220 
221   if (connected) {
222     if (display_null_.IsActive()) {
223       error = core_intf_->CreateDisplay(type_, this, &display_intf_);
224       if (error != kErrorNone) {
225         DLOGE("Display create failed. Error = %d display_type %d event_handler %p disp_intf %p",
226               error, type_, this, &display_intf_);
227         return -EINVAL;
228       }
229 
230       // Restore HDMI attributes when display is reconnected.
231       // This is to ensure that surfaceflinger & sdm are in sync.
232       display_null_.GetFrameBufferConfig(&fb_config);
233       int status = SetFrameBufferResolution(fb_config.x_pixels, fb_config.y_pixels);
234       if (status) {
235         DLOGW("Set frame buffer config failed. Error = %d", error);
236         return -1;
237       }
238       shared_ptr<Fence> release_fence = nullptr;
239       display_null_.GetDisplayState(&state);
240       display_intf_->SetDisplayState(state, false /* teardown */, &release_fence);
241       validated_ = false;
242 
243       SetVsyncEnabled(HWC2::Vsync::Enable);
244 
245       display_null_.SetActive(false);
246       DLOGI("Display is connected successfully.");
247     } else {
248       DLOGI("Display is already connected.");
249     }
250   } else {
251     if (!display_null_.IsActive()) {
252       shared_ptr<Fence> release_fence = nullptr;
253       // Preserve required attributes of HDMI display that surfaceflinger sees.
254       // Restore HDMI attributes when display is reconnected.
255       display_intf_->GetDisplayState(&state);
256       display_null_.SetDisplayState(state, false /* teardown */, &release_fence);
257 
258       error = display_intf_->GetFrameBufferConfig(&fb_config);
259       if (error != kErrorNone) {
260         DLOGW("Get frame buffer config failed. Error = %d", error);
261         return -1;
262       }
263       display_null_.SetFrameBufferConfig(fb_config);
264 
265       SetVsyncEnabled(HWC2::Vsync::Disable);
266       core_intf_->DestroyDisplay(display_intf_);
267       display_intf_ = &display_null_;
268 
269       display_null_.SetActive(true);
270       DLOGI("Display is disconnected successfully.");
271     } else {
272       DLOGI("Display is already disconnected.");
273     }
274   }
275 
276   return 0;
277 }
278 
GetUnderScanConfig()279 void HWCDisplayPluggable::GetUnderScanConfig() {
280   if (!display_intf_->IsUnderscanSupported()) {
281     // Read user defined underscan width and height
282     HWCDebugHandler::Get()->GetProperty(EXTERNAL_ACTION_SAFE_WIDTH_PROP, &underscan_width_);
283     HWCDebugHandler::Get()->GetProperty(EXTERNAL_ACTION_SAFE_HEIGHT_PROP, &underscan_height_);
284   }
285 }
286 
Flush()287 DisplayError HWCDisplayPluggable::Flush() {
288   return display_intf_->Flush(&layer_stack_);
289 }
290 
GetColorModes(uint32_t * out_num_modes,ColorMode * out_modes)291 HWC2::Error HWCDisplayPluggable::GetColorModes(uint32_t *out_num_modes, ColorMode *out_modes) {
292   if (out_modes == nullptr) {
293     *out_num_modes = color_mode_->GetColorModeCount();
294   } else {
295     color_mode_->GetColorModes(out_num_modes, out_modes);
296   }
297   return HWC2::Error::None;
298 }
299 
GetRenderIntents(ColorMode mode,uint32_t * out_num_intents,RenderIntent * out_intents)300 HWC2::Error HWCDisplayPluggable::GetRenderIntents(ColorMode mode, uint32_t *out_num_intents,
301                                                  RenderIntent *out_intents) {
302   if (out_intents == nullptr) {
303     *out_num_intents = color_mode_->GetRenderIntentCount(mode);
304   } else {
305     color_mode_->GetRenderIntents(mode, out_num_intents, out_intents);
306   }
307   return HWC2::Error::None;
308 }
309 
SetColorMode(ColorMode mode)310 HWC2::Error HWCDisplayPluggable::SetColorMode(ColorMode mode) {
311   return SetColorModeWithRenderIntent(mode, RenderIntent::COLORIMETRIC);
312 }
313 
SetColorModeWithRenderIntent(ColorMode mode,RenderIntent intent)314 HWC2::Error HWCDisplayPluggable::SetColorModeWithRenderIntent(ColorMode mode, RenderIntent intent) {
315   auto status = color_mode_->CacheColorModeWithRenderIntent(mode, intent);
316   if (status != HWC2::Error::None) {
317     DLOGE("failed for mode = %d intent = %d", mode, intent);
318     return status;
319   }
320 
321   callbacks_->Refresh(id_);
322   validated_ = false;
323 
324   return status;
325 }
326 
UpdatePowerMode(HWC2::PowerMode mode)327 HWC2::Error HWCDisplayPluggable::UpdatePowerMode(HWC2::PowerMode mode) {
328   current_power_mode_ = mode;
329   validated_ = false;
330   return HWC2::Error::None;
331 }
332 
SetColorTransform(const float * matrix,android_color_transform_t hint)333 HWC2::Error HWCDisplayPluggable::SetColorTransform(const float *matrix,
334                                                    android_color_transform_t hint) {
335   if (HAL_COLOR_TRANSFORM_IDENTITY == hint) {
336     has_color_tranform_ = false;
337     // From 2.1 IComposerClient.hal:
338     // If the device is not capable of either using the hint or the matrix to apply the desired
339     // color transform, it must force all layers to client composition during VALIDATE_DISPLAY.
340   } else {
341     // Also, interpret HAL_COLOR_TRANSFORM_ARBITRARY_MATRIX hint as non-identity matrix.
342     has_color_tranform_ = true;
343   }
344 
345   callbacks_->Refresh(id_);
346   validated_ = false;
347 
348   return HWC2::Error::None;
349 }
350 
351 }  // namespace sdm
352