1 /*
2  * Copyright (c) 2014-2021, 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 #ifndef __HWC_DISPLAY_BUILTIN_H__
31 #define __HWC_DISPLAY_BUILTIN_H__
32 
33 #include <thermal_client.h>
34 #include <mutex>
35 #include <limits>
36 #include <string>
37 #include <vector>
38 
39 #include "utils/sync_task.h"
40 #include "utils/constants.h"
41 #include "cpuhint.h"
42 #include "hwc_display.h"
43 #include "hwc_layers.h"
44 #include "display_null.h"
45 
46 #include "gl_layer_stitch.h"
47 
48 namespace sdm {
49 
50 enum class LayerStitchTaskCode : int32_t {
51   kCodeGetInstance,
52   kCodeStitch,
53   kCodeDestroyInstance,
54 };
55 
56 struct LayerStitchGetInstanceContext : public SyncTask<LayerStitchTaskCode>::TaskContext {
57   LayerBuffer *output_buffer = NULL;
58 };
59 
60 struct LayerStitchContext : public SyncTask<LayerStitchTaskCode>::TaskContext {
61   vector<StitchParams> stitch_params;
62   shared_ptr<Fence> src_acquire_fence = nullptr;
63   shared_ptr<Fence> dst_acquire_fence = nullptr;
64   shared_ptr<Fence> release_fence = nullptr;
65 };
66 
67 class HWCDisplayBuiltIn : public HWCDisplay, public SyncTask<LayerStitchTaskCode>::TaskHandler {
68  public:
69   enum {
70     SET_METADATA_DYN_REFRESH_RATE,
71     SET_BINDER_DYN_REFRESH_RATE,
72     SET_DISPLAY_MODE,
73     SET_QDCM_SOLID_FILL_INFO,
74     UNSET_QDCM_SOLID_FILL_INFO,
75     SET_QDCM_SOLID_FILL_RECT,
76   };
77 
78   static int Create(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
79                     HWCCallbacks *callbacks,  HWCDisplayEventHandler *event_handler,
80                     qService::QService *qservice, hwc2_display_t id, int32_t sdm_id,
81                     HWCDisplay **hwc_display);
82   static void Destroy(HWCDisplay *hwc_display);
83   virtual int Init();
84   virtual HWC2::Error Validate(uint32_t *out_num_types, uint32_t *out_num_requests);
85   virtual HWC2::Error Present(shared_ptr<Fence> *out_retire_fence);
86   virtual HWC2::Error CommitLayerStack();
87   virtual HWC2::Error GetColorModes(uint32_t *out_num_modes, ColorMode *out_modes);
88   virtual HWC2::Error SetColorMode(ColorMode mode);
89   virtual HWC2::Error GetRenderIntents(ColorMode mode, uint32_t *out_num_intents,
90                                        RenderIntent *out_intents);
91   virtual HWC2::Error SetColorModeWithRenderIntent(ColorMode mode, RenderIntent intent);
92   virtual HWC2::Error SetColorModeById(int32_t color_mode_id);
93   virtual HWC2::Error SetColorModeFromClientApi(int32_t color_mode_id);
94   virtual HWC2::Error SetColorTransform(const float *matrix, android_color_transform_t hint);
95   virtual HWC2::Error RestoreColorTransform();
96   virtual int Perform(uint32_t operation, ...);
97   virtual int HandleSecureSession(const std::bitset<kSecureMax> &secure_session,
98                                   bool *power_on_pending, bool is_active_secure_display);
99   virtual void SetIdleTimeoutMs(uint32_t timeout_ms);
100   virtual HWC2::Error SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type,
101                                          int32_t format, bool post_processed);
102   virtual int FrameCaptureAsync(const BufferInfo &output_buffer_info, bool post_processed);
GetFrameCaptureStatus()103   virtual int GetFrameCaptureStatus() { return frame_capture_status_; }
104   virtual DisplayError SetDetailEnhancerConfig(const DisplayDetailEnhancerData &de_data);
105   virtual DisplayError ControlPartialUpdate(bool enable, uint32_t *pending);
106   virtual HWC2::Error SetReadbackBuffer(const native_handle_t *buffer,
107                                         shared_ptr<Fence> acquire_fence,
108                                         bool post_processed_output, CWBClient client);
109   virtual HWC2::Error GetReadbackBufferFence(shared_ptr<Fence> *release_fence);
110   virtual HWC2::Error SetQSyncMode(QSyncMode qsync_mode);
111   virtual DisplayError ControlIdlePowerCollapse(bool enable, bool synchronous);
112   virtual HWC2::Error SetDisplayDppsAdROI(uint32_t h_start, uint32_t h_end, uint32_t v_start,
113                                           uint32_t v_end, uint32_t factor_in, uint32_t factor_out);
114   virtual DisplayError SetDynamicDSIClock(uint64_t bitclk);
115   virtual DisplayError GetDynamicDSIClock(uint64_t *bitclk);
116   virtual DisplayError GetSupportedDSIClock(std::vector<uint64_t> *bitclk_rates);
117   virtual DisplayError SetStandByMode(bool enable);
118   virtual HWC2::Error UpdateDisplayId(hwc2_display_t id);
119   virtual HWC2::Error SetPendingRefresh();
120   virtual HWC2::Error SetPanelBrightness(float brightness);
121   virtual HWC2::Error GetPanelBrightness(float *brightness);
122   virtual HWC2::Error GetPanelMaxBrightness(uint32_t *max_brightness_level);
123   virtual DisplayError SetCurrentPanelGammaSource(enum PanelGammaSource source) override;
GetCurrentPanelGammaSource()124   virtual PanelGammaSource GetCurrentPanelGammaSource() const override { return current_panel_gamma_source_; };
125   virtual DisplayError TeardownConcurrentWriteback(void);
SetFastPathComposition(bool enable)126   virtual void SetFastPathComposition(bool enable) {
127     fast_path_composition_ = enable && !readback_buffer_queued_;
128   }
129   virtual HWC2::Error SetFrameTriggerMode(uint32_t mode);
130   virtual HWC2::Error SetBLScale(uint32_t level);
131   virtual HWC2::Error UpdatePowerMode(HWC2::PowerMode mode);
132   virtual HWC2::Error SetClientTarget(buffer_handle_t target, shared_ptr<Fence> acquire_fence,
133                                       int32_t dataspace, hwc_region_t damage);
134   virtual bool IsSmartPanelConfig(uint32_t config_id);
135   virtual bool HasSmartPanelConfig(void);
136   virtual int Deinit();
137   virtual bool IsQsyncCallbackNeeded(bool *qsync_enabled, int32_t *refresh_rate,
138                                      int32_t *qsync_refresh_rate);
139   virtual int PostInit();
140 
141   virtual HWC2::Error SetDisplayedContentSamplingEnabledVndService(bool enabled);
142   virtual HWC2::Error SetDisplayedContentSamplingEnabled(int32_t enabled, uint8_t component_mask,
143                                                          uint64_t max_frames);
144   virtual HWC2::Error GetDisplayedContentSamplingAttributes(int32_t *format, int32_t *dataspace,
145                                                             uint8_t *supported_components);
146   virtual HWC2::Error GetDisplayedContentSample(
147       uint64_t max_frames, uint64_t timestamp, uint64_t *numFrames,
148       int32_t samples_size[NUM_HISTOGRAM_COLOR_COMPONENTS],
149       uint64_t *samples[NUM_HISTOGRAM_COLOR_COMPONENTS]);
150   void Dump(std::ostringstream *os) override;
151   virtual HWC2::Error SetPowerMode(HWC2::PowerMode mode, bool teardown);
152   virtual bool HasReadBackBufferSupport();
153   virtual bool IsDisplayIdle();
154 
155   virtual bool IsHbmSupported() override;
156   virtual HWC2::Error SetHbm(HbmState state, HbmClient client) override;
157   virtual HbmState GetHbm() override;
158 
159  private:
160   HWCDisplayBuiltIn(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
161                     HWCCallbacks *callbacks, HWCDisplayEventHandler *event_handler,
162                     qService::QService *qservice, hwc2_display_t id, int32_t sdm_id);
163   void SetMetaDataRefreshRateFlag(bool enable);
164   virtual DisplayError SetDisplayMode(uint32_t mode);
165   virtual DisplayError DisablePartialUpdateOneFrame();
166   void ProcessBootAnimCompleted(void);
167   void SetQDCMSolidFillInfo(bool enable, const LayerSolidFill &color);
168   void ToggleCPUHint(bool set);
169   void ForceRefreshRate(uint32_t refresh_rate);
170   uint32_t GetOptimalRefreshRate(bool one_updating_layer);
171   void HandleFrameOutput();
172   void HandleFrameDump();
173   void HandleFrameCapture();
174   bool CanSkipCommit();
175   DisplayError SetMixerResolution(uint32_t width, uint32_t height);
176   DisplayError GetMixerResolution(uint32_t *width, uint32_t *height);
177   HWC2::Error CommitStitchLayers();
178   void AppendStitchLayer();
179   bool InitLayerStitch();
180   void InitStitchTarget();
181   bool AllocateStitchBuffer();
182   void CacheAvrStatus();
183   void PostCommitStitchLayers();
184   void SetCpuPerfHintLargeCompCycle();
185   int GetBwCode(const DisplayConfigVariableInfo &attr);
186   void SetBwLimitHint(bool enable);
187   void SetPartialUpdate(DisplayConfigFixedInfo fixed_info);
188   uint32_t GetUpdatingAppLayersCount();
189   void ValidateUiScaling();
190   HWC2::Error ApplyHbmLocked() REQUIRES(hbm_mutex);
191 
192   // SyncTask methods.
193   void OnTask(const LayerStitchTaskCode &task_code,
194               SyncTask<LayerStitchTaskCode>::TaskContext *task_context);
195 
196   constexpr static int kBwLow = 2;
197   constexpr static int kBwMedium = 3;
198   constexpr static int kBwHigh = 4;
199   const int kPerfHintLargeCompCycle = 0x00001097;
200   BufferAllocator *buffer_allocator_ = nullptr;
201   CPUHint *cpu_hint_ = nullptr;
202   CWBClient cwb_client_ = kCWBClientNone;
203 
204   // Builtin readback buffer configuration
205   LayerBuffer output_buffer_ = {};
206   bool post_processed_output_ = false;
207   bool readback_buffer_queued_ = false;
208   bool readback_configured_ = false;
209 
210   // Members for N frame output dump to file
211   bool dump_output_to_file_ = false;
212   BufferInfo output_buffer_info_ = {};
213   void *output_buffer_base_ = nullptr;
214   bool pending_refresh_ = true;
215   bool enable_optimize_refresh_ = false;
216   bool enable_poms_during_doze_ = false;
217 
218   // Members for 1 frame capture in a client provided buffer
219   bool frame_capture_buffer_queued_ = false;
220   int frame_capture_status_ = -EAGAIN;
221   bool is_primary_ = false;
222   bool disable_layer_stitch_ = true;
223   HWCLayer* stitch_target_ = nullptr;
224   SyncTask<LayerStitchTaskCode> layer_stitch_task_;
225   GLLayerStitch* gl_layer_stitch_ = nullptr;
226   BufferInfo buffer_info_ = {};
227   DisplayConfigVariableInfo fb_config_ = {};
228 
229   enum PanelGammaSource current_panel_gamma_source_ = kGammaDefault;
230 
231   bool qsync_enabled_ = false;
232   bool qsync_reconfigured_ = false;
233   // Members for Color sampling feature
234   DisplayError HistogramEvent(int fd, uint32_t blob_id) override;
235   histogram::HistogramCollector histogram;
236   std::mutex sampling_mutex;
237   bool api_sampling_vote = false;
238   bool vndservice_sampling_vote = false;
239   int perf_hint_window_ = 0;
240   int perf_hint_large_comp_cycle_ = 0;
241   int curr_refresh_rate_ = 0;
242   bool is_smart_panel_ = false;
243   const char *kDisplayBwName = "display_bw";
244   bool enable_bw_limits_ = false;
245   bool disable_dyn_fps_ = false;
246   bool enhance_idle_time_ = false;
247   bool force_reset_validate_ = false;
248 
249   // NULL display
250   DisplayNull display_null_;
251   DisplayInterface *stored_display_intf_ = NULL;
252 
253   // Members for HBM feature
254   static constexpr const char kHighBrightnessModeNode[] =
255       "/sys/class/backlight/panel0-backlight/hbm_mode";
256   static constexpr float hbm_threshold_pct_ = 0.5f;
257   const bool mHasHbmNode = !access(kHighBrightnessModeNode, F_OK);
258   std::mutex hbm_mutex;
259   float hbm_threshold_px_ = std::numeric_limits<float>::max();
260   bool has_config_hbm_threshold_ = false;
261   bool high_brightness_mode_ = false;
262   HbmState mHbmSates[CLIENT_MAX] GUARDED_BY(hbm_mutex) = {HbmState::OFF};
263   HbmState mCurHbmState GUARDED_BY(hbm_mutex) = HbmState::OFF;
264 };
265 
266 }  // namespace sdm
267 
268 #endif  // __HWC_DISPLAY_BUILTIN_H__
269