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 <sync/sync.h>
32 #include <utils/constants.h>
33 #include <utils/debug.h>
34 #include <utils/utils.h>
35 #include <stdarg.h>
36 #include <sys/mman.h>
37 
38 #include <map>
39 #include <string>
40 #include <vector>
41 
42 #include "hwc_display_builtin.h"
43 #include "hwc_debugger.h"
44 #include "hwc_session.h"
45 
46 #define __CLASS__ "HWCDisplayBuiltIn"
47 
48 namespace sdm {
49 
Create(CoreInterface * core_intf,BufferAllocator * buffer_allocator,HWCCallbacks * callbacks,HWCDisplayEventHandler * event_handler,qService::QService * qservice,hwc2_display_t id,int32_t sdm_id,HWCDisplay ** hwc_display)50 int HWCDisplayBuiltIn::Create(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
51                               HWCCallbacks *callbacks, HWCDisplayEventHandler *event_handler,
52                               qService::QService *qservice, hwc2_display_t id, int32_t sdm_id,
53                               HWCDisplay **hwc_display) {
54   int status = 0;
55   uint32_t builtin_width = 0;
56   uint32_t builtin_height = 0;
57 
58   HWCDisplay *hwc_display_builtin =
59       new HWCDisplayBuiltIn(core_intf, buffer_allocator, callbacks, event_handler, qservice, id,
60                             sdm_id);
61   status = hwc_display_builtin->Init();
62   if (status) {
63     delete hwc_display_builtin;
64     return status;
65   }
66 
67   hwc_display_builtin->GetMixerResolution(&builtin_width, &builtin_height);
68   int width = 0, height = 0;
69   HWCDebugHandler::Get()->GetProperty(FB_WIDTH_PROP, &width);
70   HWCDebugHandler::Get()->GetProperty(FB_HEIGHT_PROP, &height);
71   if (width > 0 && height > 0) {
72     builtin_width = UINT32(width);
73     builtin_height = UINT32(height);
74   }
75 
76   status = hwc_display_builtin->SetFrameBufferResolution(builtin_width, builtin_height);
77   if (status) {
78     Destroy(hwc_display_builtin);
79     return status;
80   }
81 
82   *hwc_display = hwc_display_builtin;
83 
84   return status;
85 }
86 
Destroy(HWCDisplay * hwc_display)87 void HWCDisplayBuiltIn::Destroy(HWCDisplay *hwc_display) {
88   hwc_display->Deinit();
89   delete hwc_display;
90 }
91 
HWCDisplayBuiltIn(CoreInterface * core_intf,BufferAllocator * buffer_allocator,HWCCallbacks * callbacks,HWCDisplayEventHandler * event_handler,qService::QService * qservice,hwc2_display_t id,int32_t sdm_id)92 HWCDisplayBuiltIn::HWCDisplayBuiltIn(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
93                                      HWCCallbacks *callbacks, HWCDisplayEventHandler *event_handler,
94                                      qService::QService *qservice, hwc2_display_t id,
95                                      int32_t sdm_id)
96     : HWCDisplay(core_intf, buffer_allocator, callbacks, event_handler, qservice, kBuiltIn, id,
97                  sdm_id, true, DISPLAY_CLASS_BUILTIN),
98       buffer_allocator_(buffer_allocator),
99       cpu_hint_(NULL) {
100 }
101 
Init()102 int HWCDisplayBuiltIn::Init() {
103   cpu_hint_ = new CPUHint();
104   if (cpu_hint_->Init(static_cast<HWCDebugHandler *>(HWCDebugHandler::Get())) != kErrorNone) {
105     delete cpu_hint_;
106     cpu_hint_ = NULL;
107   }
108 
109   use_metadata_refresh_rate_ = true;
110   int disable_metadata_dynfps = 0;
111   HWCDebugHandler::Get()->GetProperty(DISABLE_METADATA_DYNAMIC_FPS_PROP, &disable_metadata_dynfps);
112   if (disable_metadata_dynfps) {
113     use_metadata_refresh_rate_ = false;
114   }
115 
116   int status = HWCDisplay::Init();
117   if (status) {
118     return status;
119   }
120   color_mode_ = new HWCColorMode(display_intf_);
121   color_mode_->Init();
122   HWCDebugHandler::Get()->GetProperty(ENABLE_DEFAULT_COLOR_MODE,
123                                       &default_mode_status_);
124 
125   int drop_refresh = 0;
126   HWCDebugHandler::Get()->GetProperty(ENABLE_DROP_REFRESH, &drop_refresh);
127   enable_drop_refresh_ = (drop_refresh == 1);
128   if (enable_drop_refresh_) {
129     DLOGI("Drop redundant drawcycles %d", id_);
130   }
131 
132   return status;
133 }
134 
Deinit()135 int HWCDisplayBuiltIn::Deinit() {
136     histogram.stop();
137     return HWCDisplay::Deinit();
138 }
139 
Dump()140 std::string HWCDisplayBuiltIn::Dump() {
141     return HWCDisplay::Dump() + histogram.Dump();
142 }
143 
Validate(uint32_t * out_num_types,uint32_t * out_num_requests)144 HWC2::Error HWCDisplayBuiltIn::Validate(uint32_t *out_num_types, uint32_t *out_num_requests) {
145   auto status = HWC2::Error::None;
146   DisplayError error = kErrorNone;
147 
148   DTRACE_SCOPED();
149   if (display_paused_) {
150     MarkLayersForGPUBypass();
151     return status;
152   }
153 
154   if (color_tranform_failed_) {
155     // Must fall back to client composition
156     MarkLayersForClientComposition();
157   }
158 
159   // Fill in the remaining blanks in the layers and add them to the SDM layerstack
160   BuildLayerStack();
161   // Checks and replaces layer stack for solid fill
162   SolidFillPrepare();
163 
164   // Apply current Color Mode and Render Intent.
165   if (color_mode_->ApplyCurrentColorModeWithRenderIntent(
166       static_cast<bool>(layer_stack_.flags.hdr_present)) != HWC2::Error::None) {
167     // Fallback to GPU Composition, if Color Mode can't be applied.
168     MarkLayersForClientComposition();
169   }
170 
171   bool pending_output_dump = dump_frame_count_ && dump_output_to_file_;
172 
173   if (readback_buffer_queued_ || pending_output_dump) {
174     CloseFd(&output_buffer_.release_fence_fd);
175     // RHS values were set in FrameCaptureAsync() called from a binder thread. They are picked up
176     // here in a subsequent draw round. Readback is not allowed for any secure use case.
177     readback_configured_ = !layer_stack_.flags.secure_present;
178     if (readback_configured_) {
179       DisablePartialUpdateOneFrame();
180       layer_stack_.output_buffer = &output_buffer_;
181       layer_stack_.flags.post_processed_output = post_processed_output_;
182     }
183   }
184 
185   uint32_t num_updating_layers = GetUpdatingLayersCount();
186   bool one_updating_layer = (num_updating_layers == 1);
187   if (num_updating_layers != 0) {
188     ToggleCPUHint(one_updating_layer);
189   }
190 
191   uint32_t refresh_rate = GetOptimalRefreshRate(one_updating_layer);
192   bool final_rate = force_refresh_rate_ ? true : false;
193   error = display_intf_->SetRefreshRate(refresh_rate, final_rate);
194   if (error == kErrorNone) {
195     // On success, set current refresh rate to new refresh rate
196     current_refresh_rate_ = refresh_rate;
197   }
198 
199   if (layer_set_.empty()) {
200     // Avoid flush on command mode with secure display
201     flush_ = !(IsDisplayCommandMode() &&
202         active_secure_sessions_.test(kSecureDisplay));
203     validated_ = true;
204     return status;
205   }
206 
207   status = PrepareLayerStack(out_num_types, out_num_requests);
208   pending_commit_ = true;
209   return status;
210 }
211 
CommitLayerStack()212 HWC2::Error HWCDisplayBuiltIn::CommitLayerStack() {
213   skip_commit_ = CanSkipCommit();
214   return HWCDisplay::CommitLayerStack();
215 }
216 
CanSkipCommit()217 bool HWCDisplayBuiltIn::CanSkipCommit() {
218   if (layer_stack_invalid_) {
219     return false;
220   }
221 
222   // Reject repeated drawcycle requests if it satisfies all conditions.
223   // 1. None of the layerstack attributes changed.
224   // 2. No new buffer latched.
225   // 3. No refresh request triggered by HWC.
226   // 4. This display is not source of vsync.
227   bool buffers_latched = false;
228   for (auto &hwc_layer : layer_set_) {
229     buffers_latched |= hwc_layer->BufferLatched();
230     hwc_layer->ResetBufferFlip();
231   }
232 
233   bool vsync_source = (callbacks_->GetVsyncSource() == id_);
234   bool skip_commit = enable_drop_refresh_ && !pending_commit_ && !buffers_latched &&
235                      !pending_refresh_ && !vsync_source;
236   pending_refresh_ = false;
237 
238   return skip_commit;
239 }
240 
Present(int32_t * out_retire_fence)241 HWC2::Error HWCDisplayBuiltIn::Present(int32_t *out_retire_fence) {
242   auto status = HWC2::Error::None;
243 
244   DTRACE_SCOPED();
245   ATRACE_INT("PartialUpdate", partial_update_enabled_);
246   ATRACE_INT("FastPath", layer_stack_.flags.fast_path);
247   ATRACE_INT("GeometryChanged", layer_stack_.flags.geometry_changed);
248   ATRACE_INT("NumLayers", static_cast <int32_t> (layer_stack_.layers.size()));
249   ATRACE_INT("HasClientComposition", HasClientComposition());
250 
251   if (display_paused_) {
252     DisplayError error = display_intf_->Flush(&layer_stack_);
253     validated_ = false;
254     if (error != kErrorNone) {
255       DLOGE("Flush failed. Error = %d", error);
256     }
257   } else {
258     status = CommitLayerStack();
259     if (status == HWC2::Error::None) {
260       HandleFrameOutput();
261       SolidFillCommit();
262       status = HWCDisplay::PostCommitLayerStack(out_retire_fence);
263     }
264   }
265 
266   if (CC_UNLIKELY(!has_init_light_server_)) {
267     using ILight = ::hardware::google::light::V1_0::ILight;
268     hardware_ILight_ = ILight::getService();
269     if (hardware_ILight_ != nullptr) {
270       hardware_ILight_->setHbm(false);
271     } else {
272       DLOGE("failed to get vendor light service");
273     }
274 
275     uint32_t panel_x, panel_y;
276     GetPanelResolution(&panel_x, &panel_y);
277     hbm_threshold_px_ = float(panel_x * panel_y) * hbm_threshold_pct_;
278     DLOGI("Configure hbm_threshold_px_ to %f", hbm_threshold_px_);
279 
280     has_init_light_server_ = true;
281   }
282 
283   const bool enable_hbm(hdr_largest_layer_px_ > hbm_threshold_px_);
284   if (high_brightness_mode_ != enable_hbm && hardware_ILight_ != nullptr) {
285     using ::android::hardware::light::V2_0::Status;
286     if (Status::SUCCESS == hardware_ILight_->setHbm(enable_hbm)) {
287       high_brightness_mode_ = enable_hbm;
288     } else {
289       DLOGE("failed to setHbm to %d", enable_hbm);
290     }
291   }
292 
293   CloseFd(&output_buffer_.acquire_fence_fd);
294   pending_commit_ = false;
295   return status;
296 }
297 
GetColorModes(uint32_t * out_num_modes,ColorMode * out_modes)298 HWC2::Error HWCDisplayBuiltIn::GetColorModes(uint32_t *out_num_modes, ColorMode *out_modes) {
299   if (out_modes == nullptr) {
300     *out_num_modes = color_mode_->GetColorModeCount();
301   } else {
302     color_mode_->GetColorModes(out_num_modes, out_modes);
303   }
304 
305   return HWC2::Error::None;
306 }
307 
GetRenderIntents(ColorMode mode,uint32_t * out_num_intents,RenderIntent * out_intents)308 HWC2::Error HWCDisplayBuiltIn::GetRenderIntents(ColorMode mode, uint32_t *out_num_intents,
309                                                 RenderIntent *out_intents) {
310   if (out_intents == nullptr) {
311     *out_num_intents = color_mode_->GetRenderIntentCount(mode);
312   } else {
313     color_mode_->GetRenderIntents(mode, out_num_intents, out_intents);
314   }
315   return HWC2::Error::None;
316 }
317 
SetColorMode(ColorMode mode)318 HWC2::Error HWCDisplayBuiltIn::SetColorMode(ColorMode mode) {
319   return SetColorModeWithRenderIntent(mode, RenderIntent::COLORIMETRIC);
320 }
321 
SetColorModeWithRenderIntent(ColorMode mode,RenderIntent intent)322 HWC2::Error HWCDisplayBuiltIn::SetColorModeWithRenderIntent(ColorMode mode, RenderIntent intent) {
323   auto status = color_mode_->CacheColorModeWithRenderIntent(mode, intent);
324   if (status != HWC2::Error::None) {
325     DLOGE("failed for mode = %d intent = %d", mode, intent);
326     return status;
327   }
328   callbacks_->Refresh(id_);
329   validated_ = false;
330   return status;
331 }
332 
SetColorModeById(int32_t color_mode_id)333 HWC2::Error HWCDisplayBuiltIn::SetColorModeById(int32_t color_mode_id) {
334   auto status = color_mode_->SetColorModeById(color_mode_id);
335   if (status != HWC2::Error::None) {
336     DLOGE("failed for mode = %d", color_mode_id);
337     return status;
338   }
339 
340   callbacks_->Refresh(id_);
341   validated_ = false;
342 
343   return status;
344 }
345 
SetColorModeFromClientApi(int32_t color_mode_id)346 HWC2::Error HWCDisplayBuiltIn::SetColorModeFromClientApi(int32_t color_mode_id) {
347   DisplayError error = kErrorNone;
348   std::string mode_string;
349 
350   error = display_intf_->GetColorModeName(color_mode_id, &mode_string);
351   if (error) {
352     DLOGE("Failed to get mode name for mode %d", color_mode_id);
353     return HWC2::Error::BadParameter;
354   }
355 
356   auto status = color_mode_->SetColorModeFromClientApi(mode_string);
357   if (status != HWC2::Error::None) {
358     DLOGE("Failed to set mode = %d", color_mode_id);
359     return status;
360   }
361 
362   return status;
363 }
364 
RestoreColorTransform()365 HWC2::Error HWCDisplayBuiltIn::RestoreColorTransform() {
366   auto status = color_mode_->RestoreColorTransform();
367   if (status != HWC2::Error::None) {
368     DLOGE("failed to RestoreColorTransform");
369     return status;
370   }
371 
372   callbacks_->Refresh(id_);
373 
374   return status;
375 }
376 
SetColorTransform(const float * matrix,android_color_transform_t hint)377 HWC2::Error HWCDisplayBuiltIn::SetColorTransform(const float *matrix,
378                                                  android_color_transform_t hint) {
379   if (!matrix) {
380     return HWC2::Error::BadParameter;
381   }
382 
383   auto status = color_mode_->SetColorTransform(matrix, hint);
384   if (status != HWC2::Error::None) {
385     DLOGE("failed for hint = %d", hint);
386     color_tranform_failed_ = true;
387     return status;
388   }
389 
390   callbacks_->Refresh(id_);
391   color_tranform_failed_ = false;
392   validated_ = false;
393 
394   return status;
395 }
396 
SetReadbackBuffer(const native_handle_t * buffer,int32_t acquire_fence,bool post_processed_output)397 HWC2::Error HWCDisplayBuiltIn::SetReadbackBuffer(const native_handle_t *buffer,
398                                                  int32_t acquire_fence,
399                                                  bool post_processed_output) {
400   const private_handle_t *handle = reinterpret_cast<const private_handle_t *>(buffer);
401   if (!handle || (handle->fd < 0)) {
402     return HWC2::Error::BadParameter;
403   }
404 
405   // Configure the output buffer as Readback buffer
406   output_buffer_.width = UINT32(handle->width);
407   output_buffer_.height = UINT32(handle->height);
408   output_buffer_.unaligned_width = UINT32(handle->unaligned_width);
409   output_buffer_.unaligned_height = UINT32(handle->unaligned_height);
410   output_buffer_.format = HWCLayer::GetSDMFormat(handle->format, handle->flags);
411   output_buffer_.planes[0].fd = handle->fd;
412   output_buffer_.planes[0].stride = UINT32(handle->width);
413   output_buffer_.acquire_fence_fd = dup(acquire_fence);
414   output_buffer_.release_fence_fd = -1;
415   output_buffer_.handle_id = handle->id;
416 
417   post_processed_output_ = post_processed_output;
418   readback_buffer_queued_ = true;
419   readback_configured_ = false;
420   validated_ = false;
421 
422   return HWC2::Error::None;
423 }
424 
GetReadbackBufferFence(int32_t * release_fence)425 HWC2::Error HWCDisplayBuiltIn::GetReadbackBufferFence(int32_t *release_fence) {
426   auto status = HWC2::Error::None;
427 
428   if (readback_configured_ && (output_buffer_.release_fence_fd >= 0)) {
429     *release_fence = output_buffer_.release_fence_fd;
430   } else {
431     status = HWC2::Error::Unsupported;
432     *release_fence = -1;
433   }
434 
435   post_processed_output_ = false;
436   readback_buffer_queued_ = false;
437   readback_configured_ = false;
438   output_buffer_ = {};
439 
440   return status;
441 }
442 
TeardownConcurrentWriteback(void)443 DisplayError HWCDisplayBuiltIn::TeardownConcurrentWriteback(void) {
444   DisplayError error = kErrorNotSupported;
445 
446   if (output_buffer_.release_fence_fd >= 0) {
447     int32_t release_fence_fd = dup(output_buffer_.release_fence_fd);
448     int ret = sync_wait(output_buffer_.release_fence_fd, 1000);
449     if (ret < 0) {
450       DLOGE("sync_wait error errno = %d, desc = %s", errno, strerror(errno));
451     }
452 
453     ::close(release_fence_fd);
454     if (ret)
455       return kErrorResources;
456   }
457 
458   if (display_intf_) {
459     error = display_intf_->TeardownConcurrentWriteback();
460   }
461 
462   return error;
463 }
464 
SetDisplayDppsAdROI(uint32_t h_start,uint32_t h_end,uint32_t v_start,uint32_t v_end,uint32_t factor_in,uint32_t factor_out)465 HWC2::Error HWCDisplayBuiltIn::SetDisplayDppsAdROI(uint32_t h_start, uint32_t h_end,
466                                                    uint32_t v_start, uint32_t v_end,
467                                                    uint32_t factor_in, uint32_t factor_out) {
468   DisplayError error = kErrorNone;
469   DisplayDppsAd4RoiCfg dpps_ad4_roi_cfg = {};
470   uint32_t panel_width = 0, panel_height = 0;
471   constexpr uint16_t kMaxFactorVal = 0xffff;
472 
473   if (h_start >= h_end || v_start >= v_end || factor_in > kMaxFactorVal ||
474       factor_out > kMaxFactorVal) {
475     DLOGE("Invalid roi region = [%u, %u, %u, %u, %u, %u]",
476            h_start, h_end, v_start, v_end, factor_in, factor_out);
477     return HWC2::Error::BadParameter;
478   }
479 
480   GetPanelResolution(&panel_width, &panel_height);
481 
482   if (h_start >= panel_width || h_end > panel_width ||
483       v_start >= panel_height || v_end > panel_height) {
484     DLOGE("Invalid roi region = [%u, %u, %u, %u], panel resolution = [%u, %u]",
485            h_start, h_end, v_start, v_end, panel_width, panel_height);
486     return HWC2::Error::BadParameter;
487   }
488 
489   dpps_ad4_roi_cfg.h_start = h_start;
490   dpps_ad4_roi_cfg.h_end = h_end;
491   dpps_ad4_roi_cfg.v_start = v_start;
492   dpps_ad4_roi_cfg.v_end = v_end;
493   dpps_ad4_roi_cfg.factor_in = factor_in;
494   dpps_ad4_roi_cfg.factor_out = factor_out;
495 
496   error = display_intf_->SetDisplayDppsAdROI(&dpps_ad4_roi_cfg);
497   if (error)
498     return HWC2::Error::BadConfig;
499 
500   callbacks_->Refresh(id_);
501 
502   return HWC2::Error::None;
503 }
504 
Perform(uint32_t operation,...)505 int HWCDisplayBuiltIn::Perform(uint32_t operation, ...) {
506   va_list args;
507   va_start(args, operation);
508   int val = 0;
509   LayerSolidFill *solid_fill_color;
510   LayerRect *rect = NULL;
511 
512   switch (operation) {
513     case SET_METADATA_DYN_REFRESH_RATE:
514       val = va_arg(args, int32_t);
515       SetMetaDataRefreshRateFlag(val);
516       break;
517     case SET_BINDER_DYN_REFRESH_RATE:
518       val = va_arg(args, int32_t);
519       ForceRefreshRate(UINT32(val));
520       break;
521     case SET_DISPLAY_MODE:
522       val = va_arg(args, int32_t);
523       SetDisplayMode(UINT32(val));
524       break;
525     case SET_QDCM_SOLID_FILL_INFO:
526       solid_fill_color = va_arg(args, LayerSolidFill*);
527       SetQDCMSolidFillInfo(true, *solid_fill_color);
528       break;
529     case UNSET_QDCM_SOLID_FILL_INFO:
530       solid_fill_color = va_arg(args, LayerSolidFill*);
531       SetQDCMSolidFillInfo(false, *solid_fill_color);
532       break;
533     case SET_QDCM_SOLID_FILL_RECT:
534       rect = va_arg(args, LayerRect*);
535       solid_fill_rect_ = *rect;
536       break;
537     default:
538       DLOGW("Invalid operation %d", operation);
539       va_end(args);
540       return -EINVAL;
541   }
542   va_end(args);
543   validated_ = false;
544 
545   return 0;
546 }
547 
SetDisplayMode(uint32_t mode)548 DisplayError HWCDisplayBuiltIn::SetDisplayMode(uint32_t mode) {
549   DisplayError error = kErrorNone;
550 
551   if (display_intf_) {
552     error = display_intf_->SetDisplayMode(mode);
553     if (error == kErrorNone) {
554       DisplayConfigFixedInfo fixed_info = {};
555       display_intf_->GetConfig(&fixed_info);
556       is_cmd_mode_ = fixed_info.is_cmdmode;
557       partial_update_enabled_ = fixed_info.partial_update;
558       for (auto hwc_layer : layer_set_) {
559         hwc_layer->SetPartialUpdate(partial_update_enabled_);
560       }
561       client_target_->SetPartialUpdate(partial_update_enabled_);
562     }
563   }
564 
565   return error;
566 }
567 
SetMetaDataRefreshRateFlag(bool enable)568 void HWCDisplayBuiltIn::SetMetaDataRefreshRateFlag(bool enable) {
569   int disable_metadata_dynfps = 0;
570 
571   HWCDebugHandler::Get()->GetProperty(DISABLE_METADATA_DYNAMIC_FPS_PROP, &disable_metadata_dynfps);
572   if (disable_metadata_dynfps) {
573     return;
574   }
575   use_metadata_refresh_rate_ = enable;
576 }
577 
SetQDCMSolidFillInfo(bool enable,const LayerSolidFill & color)578 void HWCDisplayBuiltIn::SetQDCMSolidFillInfo(bool enable, const LayerSolidFill &color) {
579   solid_fill_enable_ = enable;
580   solid_fill_color_ = color;
581 }
582 
ToggleCPUHint(bool set)583 void HWCDisplayBuiltIn::ToggleCPUHint(bool set) {
584   if (!cpu_hint_) {
585     return;
586   }
587 
588   if (set) {
589     cpu_hint_->Set();
590   } else {
591     cpu_hint_->Reset();
592   }
593 }
594 
HandleSecureSession(const std::bitset<kSecureMax> & secure_sessions,bool * power_on_pending)595 int HWCDisplayBuiltIn::HandleSecureSession(const std::bitset<kSecureMax> &secure_sessions,
596                                            bool *power_on_pending) {
597   if (!power_on_pending) {
598     return -EINVAL;
599   }
600 
601   if (current_power_mode_ != HWC2::PowerMode::On) {
602     return 0;
603   }
604 
605   if (active_secure_sessions_[kSecureDisplay] != secure_sessions[kSecureDisplay]) {
606     SecureEvent secure_event =
607         secure_sessions.test(kSecureDisplay) ? kSecureDisplayStart : kSecureDisplayEnd;
608     DisplayError err = display_intf_->HandleSecureEvent(secure_event, &layer_stack_);
609     if (err != kErrorNone) {
610       DLOGE("Set secure event failed");
611       return err;
612     }
613 
614     DLOGI("SecureDisplay state changed from %d to %d for display %d",
615           active_secure_sessions_.test(kSecureDisplay), secure_sessions.test(kSecureDisplay),
616           type_);
617   }
618   active_secure_sessions_ = secure_sessions;
619   *power_on_pending = false;
620   return 0;
621 }
622 
ForceRefreshRate(uint32_t refresh_rate)623 void HWCDisplayBuiltIn::ForceRefreshRate(uint32_t refresh_rate) {
624   if ((refresh_rate && (refresh_rate < min_refresh_rate_ || refresh_rate > max_refresh_rate_)) ||
625       force_refresh_rate_ == refresh_rate) {
626     // Cannot honor force refresh rate, as its beyond the range or new request is same
627     return;
628   }
629 
630   force_refresh_rate_ = refresh_rate;
631 
632   callbacks_->Refresh(id_);
633 
634   return;
635 }
636 
GetOptimalRefreshRate(bool one_updating_layer)637 uint32_t HWCDisplayBuiltIn::GetOptimalRefreshRate(bool one_updating_layer) {
638   if (force_refresh_rate_) {
639     return force_refresh_rate_;
640   } else if (use_metadata_refresh_rate_ && one_updating_layer && metadata_refresh_rate_) {
641     return metadata_refresh_rate_;
642   }
643 
644   return max_refresh_rate_;
645 }
646 
Refresh()647 DisplayError HWCDisplayBuiltIn::Refresh() {
648   DisplayError error = kErrorNone;
649 
650   callbacks_->Refresh(id_);
651 
652   return error;
653 }
654 
SetIdleTimeoutMs(uint32_t timeout_ms)655 void HWCDisplayBuiltIn::SetIdleTimeoutMs(uint32_t timeout_ms) {
656   display_intf_->SetIdleTimeoutMs(timeout_ms);
657   validated_ = false;
658 }
659 
HandleFrameOutput()660 void HWCDisplayBuiltIn::HandleFrameOutput() {
661   if (readback_buffer_queued_) {
662     validated_ = false;
663   }
664 
665   if (frame_capture_buffer_queued_) {
666     HandleFrameCapture();
667   } else if (dump_output_to_file_) {
668     HandleFrameDump();
669   }
670 }
671 
HandleFrameCapture()672 void HWCDisplayBuiltIn::HandleFrameCapture() {
673   if (readback_configured_ && (output_buffer_.release_fence_fd >= 0)) {
674     frame_capture_status_ = sync_wait(output_buffer_.release_fence_fd, 1000);
675     ::close(output_buffer_.release_fence_fd);
676     output_buffer_.release_fence_fd = -1;
677   }
678 
679   frame_capture_buffer_queued_ = false;
680   readback_buffer_queued_ = false;
681   post_processed_output_ = false;
682   readback_configured_ = false;
683   output_buffer_ = {};
684 }
685 
HandleFrameDump()686 void HWCDisplayBuiltIn::HandleFrameDump() {
687   if (!readback_configured_) {
688     dump_frame_count_ = 0;
689   }
690 
691   if (dump_frame_count_ && output_buffer_.release_fence_fd >= 0) {
692     int ret = sync_wait(output_buffer_.release_fence_fd, 1000);
693     ::close(output_buffer_.release_fence_fd);
694     output_buffer_.release_fence_fd = -1;
695     if (ret < 0) {
696       DLOGE("sync_wait error errno = %d, desc = %s", errno, strerror(errno));
697     } else {
698       DumpOutputBuffer(output_buffer_info_, output_buffer_base_, layer_stack_.retire_fence_fd);
699       readback_buffer_queued_ = false;
700       validated_ = false;
701     }
702   }
703 
704   if (0 == dump_frame_count_) {
705     dump_output_to_file_ = false;
706     // Unmap and Free buffer
707     if (munmap(output_buffer_base_, output_buffer_info_.alloc_buffer_info.size) != 0) {
708       DLOGE("unmap failed with err %d", errno);
709     }
710     if (buffer_allocator_->FreeBuffer(&output_buffer_info_) != 0) {
711       DLOGE("FreeBuffer failed");
712     }
713 
714     readback_buffer_queued_ = false;
715     post_processed_output_ = false;
716     readback_configured_ = false;
717 
718     output_buffer_ = {};
719     output_buffer_info_ = {};
720     output_buffer_base_ = nullptr;
721   }
722 }
723 
SetFrameDumpConfig(uint32_t count,uint32_t bit_mask_layer_type,int32_t format,bool post_processed)724 HWC2::Error HWCDisplayBuiltIn::SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type,
725                                                   int32_t format, bool post_processed) {
726   HWCDisplay::SetFrameDumpConfig(count, bit_mask_layer_type, format, post_processed);
727   dump_output_to_file_ = bit_mask_layer_type & (1 << OUTPUT_LAYER_DUMP);
728   DLOGI("output_layer_dump_enable %d", dump_output_to_file_);
729 
730   if (!count || !dump_output_to_file_ || (output_buffer_info_.alloc_buffer_info.fd >= 0)) {
731     return HWC2::Error::None;
732   }
733 
734   // Allocate and map output buffer
735   if (post_processed) {
736     // To dump post-processed (DSPP) output, use Panel resolution.
737     GetPanelResolution(&output_buffer_info_.buffer_config.width,
738                        &output_buffer_info_.buffer_config.height);
739   } else {
740     // To dump Layer Mixer output, use FrameBuffer resolution.
741     GetFrameBufferResolution(&output_buffer_info_.buffer_config.width,
742                              &output_buffer_info_.buffer_config.height);
743   }
744 
745   output_buffer_info_.buffer_config.format = HWCLayer::GetSDMFormat(format, 0);
746   output_buffer_info_.buffer_config.buffer_count = 1;
747   if (buffer_allocator_->AllocateBuffer(&output_buffer_info_) != 0) {
748     DLOGE("Buffer allocation failed");
749     output_buffer_info_ = {};
750     return HWC2::Error::NoResources;
751   }
752 
753   void *buffer = mmap(NULL, output_buffer_info_.alloc_buffer_info.size, PROT_READ | PROT_WRITE,
754                       MAP_SHARED, output_buffer_info_.alloc_buffer_info.fd, 0);
755 
756   if (buffer == MAP_FAILED) {
757     DLOGE("mmap failed with err %d", errno);
758     buffer_allocator_->FreeBuffer(&output_buffer_info_);
759     output_buffer_info_ = {};
760     return HWC2::Error::NoResources;
761   }
762 
763   output_buffer_base_ = buffer;
764   const native_handle_t *handle = static_cast<native_handle_t *>(output_buffer_info_.private_data);
765   SetReadbackBuffer(handle, -1, post_processed);
766 
767   return HWC2::Error::None;
768 }
769 
FrameCaptureAsync(const BufferInfo & output_buffer_info,bool post_processed_output)770 int HWCDisplayBuiltIn::FrameCaptureAsync(const BufferInfo &output_buffer_info,
771                                          bool post_processed_output) {
772   // Note: This function is called in context of a binder thread and a lock is already held
773   if (output_buffer_info.alloc_buffer_info.fd < 0) {
774     DLOGE("Invalid fd %d", output_buffer_info.alloc_buffer_info.fd);
775     return -1;
776   }
777 
778   auto panel_width = 0u;
779   auto panel_height = 0u;
780   auto fb_width = 0u;
781   auto fb_height = 0u;
782 
783   GetPanelResolution(&panel_width, &panel_height);
784   GetFrameBufferResolution(&fb_width, &fb_height);
785 
786   if (post_processed_output && (output_buffer_info.buffer_config.width < panel_width ||
787                                 output_buffer_info.buffer_config.height < panel_height)) {
788     DLOGE("Buffer dimensions should not be less than panel resolution");
789     return -1;
790   } else if (!post_processed_output && (output_buffer_info.buffer_config.width < fb_width ||
791                                         output_buffer_info.buffer_config.height < fb_height)) {
792     DLOGE("Buffer dimensions should not be less than FB resolution");
793     return -1;
794   }
795 
796   const native_handle_t *buffer = static_cast<native_handle_t *>(output_buffer_info.private_data);
797   SetReadbackBuffer(buffer, -1, post_processed_output);
798   frame_capture_buffer_queued_ = true;
799   frame_capture_status_ = -EAGAIN;
800 
801   return 0;
802 }
803 
SetDetailEnhancerConfig(const DisplayDetailEnhancerData & de_data)804 DisplayError HWCDisplayBuiltIn::SetDetailEnhancerConfig
805                                    (const DisplayDetailEnhancerData &de_data) {
806   DisplayError error = kErrorNotSupported;
807 
808   if (display_intf_) {
809     error = display_intf_->SetDetailEnhancerData(de_data);
810     validated_ = false;
811   }
812   return error;
813 }
814 
ControlPartialUpdate(bool enable,uint32_t * pending)815 DisplayError HWCDisplayBuiltIn::ControlPartialUpdate(bool enable, uint32_t *pending) {
816   DisplayError error = kErrorNone;
817 
818   if (display_intf_) {
819     error = display_intf_->ControlPartialUpdate(enable, pending);
820     validated_ = false;
821   }
822 
823   return error;
824 }
825 
DisablePartialUpdateOneFrame()826 DisplayError HWCDisplayBuiltIn::DisablePartialUpdateOneFrame() {
827   DisplayError error = kErrorNone;
828 
829   if (display_intf_) {
830     error = display_intf_->DisablePartialUpdateOneFrame();
831     validated_ = false;
832   }
833 
834   return error;
835 }
836 
SetDisplayedContentSamplingEnabledVndService(bool enabled)837 HWC2::Error HWCDisplayBuiltIn::SetDisplayedContentSamplingEnabledVndService(bool enabled) {
838   std::unique_lock<decltype(sampling_mutex)> lk(sampling_mutex);
839   vndservice_sampling_vote = enabled;
840   if (api_sampling_vote || vndservice_sampling_vote) {
841     histogram.start();
842     display_intf_->colorSamplingOn();
843   } else {
844     display_intf_->colorSamplingOff();
845     histogram.stop();
846   }
847   return HWC2::Error::None;
848 }
849 
SetDisplayedContentSamplingEnabled(int32_t enabled,uint8_t component_mask,uint64_t max_frames)850 HWC2::Error HWCDisplayBuiltIn::SetDisplayedContentSamplingEnabled(int32_t enabled, uint8_t component_mask, uint64_t max_frames) {
851     if ((enabled != HWC2_DISPLAYED_CONTENT_SAMPLING_ENABLE) &&
852         (enabled != HWC2_DISPLAYED_CONTENT_SAMPLING_DISABLE))
853       return HWC2::Error::BadParameter;
854 
855     std::unique_lock<decltype(sampling_mutex)> lk(sampling_mutex);
856     if (enabled == HWC2_DISPLAYED_CONTENT_SAMPLING_ENABLE) {
857       api_sampling_vote = true;
858     } else {
859       api_sampling_vote = false;
860     }
861 
862     auto start = api_sampling_vote || vndservice_sampling_vote;
863     if (start && max_frames == 0) {
864       histogram.start();
865       display_intf_->colorSamplingOn();
866     } else if (start) {
867       histogram.start(max_frames);
868       display_intf_->colorSamplingOn();
869     } else {
870       display_intf_->colorSamplingOff();
871       histogram.stop();
872     }
873     return HWC2::Error::None;
874 }
875 
GetDisplayedContentSamplingAttributes(int32_t * format,int32_t * dataspace,uint8_t * supported_components)876 HWC2::Error HWCDisplayBuiltIn::GetDisplayedContentSamplingAttributes(int32_t* format,
877                                                                      int32_t* dataspace,
878                                                                      uint8_t* supported_components) {
879     return histogram.getAttributes(format, dataspace, supported_components);
880 }
881 
GetDisplayedContentSample(uint64_t max_frames,uint64_t timestamp,uint64_t * numFrames,int32_t samples_size[NUM_HISTOGRAM_COLOR_COMPONENTS],uint64_t * samples[NUM_HISTOGRAM_COLOR_COMPONENTS])882 HWC2::Error HWCDisplayBuiltIn::GetDisplayedContentSample(uint64_t max_frames,
883                                                          uint64_t timestamp,
884                                                          uint64_t* numFrames,
885                                                          int32_t samples_size[NUM_HISTOGRAM_COLOR_COMPONENTS],
886                                                          uint64_t* samples[NUM_HISTOGRAM_COLOR_COMPONENTS])
887 {
888     histogram.collect(max_frames, timestamp, samples_size, samples, numFrames);
889     return HWC2::Error::None;
890 }
891 
SetMixerResolution(uint32_t width,uint32_t height)892 DisplayError HWCDisplayBuiltIn::SetMixerResolution(uint32_t width, uint32_t height) {
893   DisplayError error = display_intf_->SetMixerResolution(width, height);
894   validated_ = false;
895   return error;
896 }
897 
GetMixerResolution(uint32_t * width,uint32_t * height)898 DisplayError HWCDisplayBuiltIn::GetMixerResolution(uint32_t *width, uint32_t *height) {
899   return display_intf_->GetMixerResolution(width, height);
900 }
901 
SetQSyncMode(QSyncMode qsync_mode)902 HWC2::Error HWCDisplayBuiltIn::SetQSyncMode(QSyncMode qsync_mode) {
903   auto err = display_intf_->SetQSyncMode(qsync_mode);
904   if (err != kErrorNone) {
905     return HWC2::Error::Unsupported;
906   }
907 
908   validated_ = false;
909   return HWC2::Error::None;
910 }
911 
ControlIdlePowerCollapse(bool enable,bool synchronous)912 DisplayError HWCDisplayBuiltIn::ControlIdlePowerCollapse(bool enable, bool synchronous) {
913   DisplayError error = kErrorNone;
914 
915   if (display_intf_) {
916     error = display_intf_->ControlIdlePowerCollapse(enable, synchronous);
917     validated_ = false;
918   }
919   return error;
920 }
921 
SetDynamicDSIClock(uint64_t bitclk)922 DisplayError HWCDisplayBuiltIn::SetDynamicDSIClock(uint64_t bitclk) {
923   {
924     SEQUENCE_WAIT_SCOPE_LOCK(HWCSession::locker_[type_]);
925     DisplayError error = display_intf_->SetDynamicDSIClock(bitclk);
926     if (error != kErrorNone) {
927       DLOGE(" failed: Clk: %llu Error: %d", bitclk, error);
928       return error;
929     }
930   }
931 
932   callbacks_->Refresh(id_);
933   validated_ = false;
934 
935   return kErrorNone;
936 }
937 
GetDynamicDSIClock(uint64_t * bitclk)938 DisplayError HWCDisplayBuiltIn::GetDynamicDSIClock(uint64_t *bitclk) {
939   SEQUENCE_WAIT_SCOPE_LOCK(HWCSession::locker_[type_]);
940   if (display_intf_) {
941     return display_intf_->GetDynamicDSIClock(bitclk);
942   }
943 
944   return kErrorNotSupported;
945 }
946 
GetSupportedDSIClock(std::vector<uint64_t> * bitclk_rates)947 DisplayError HWCDisplayBuiltIn::GetSupportedDSIClock(std::vector<uint64_t> *bitclk_rates) {
948   if (display_intf_) {
949     return display_intf_->GetSupportedDSIClock(bitclk_rates);
950   }
951 
952   return kErrorNotSupported;
953 }
954 
HistogramEvent(int fd,uint32_t blob_id)955 DisplayError HWCDisplayBuiltIn::HistogramEvent(int fd, uint32_t blob_id) {
956   histogram.notify_histogram_event(fd, blob_id);
957   return kErrorNone;
958 }
959 
UpdateDisplayId(hwc2_display_t id)960 HWC2::Error HWCDisplayBuiltIn::UpdateDisplayId(hwc2_display_t id) {
961   id_ = id;
962   return HWC2::Error::None;
963 }
964 
SetPendingRefresh()965 HWC2::Error HWCDisplayBuiltIn::SetPendingRefresh() {
966   pending_refresh_ = true;
967   return HWC2::Error::None;
968 }
969 
970 }  // namespace sdm
971