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 /*
31  * Changes from Qualcomm Innovation Center are provided under the following license:
32  *
33  * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted (subject to the limitations in the
37  * disclaimer below) provided that the following conditions are met:
38  *
39  *    * Redistributions of source code must retain the above copyright
40  *      notice, this list of conditions and the following disclaimer.
41  *
42  *    * Redistributions in binary form must reproduce the above
43  *      copyright notice, this list of conditions and the following
44  *      disclaimer in the documentation and/or other materials provided
45  *      with the distribution.
46  *
47  *    * Neither the name of Qualcomm Innovation Center, Inc. nor the names of its
48  *      contributors may be used to endorse or promote products derived
49  *      from this software without specific prior written permission.
50  *
51  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
52  * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
53  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
54  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
55  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
56  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
57  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
59  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
61  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
62  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
63  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  */
65 
66 #ifndef __HWC_DISPLAY_BUILTIN_H__
67 #define __HWC_DISPLAY_BUILTIN_H__
68 
69 #include <thermal_client.h>
70 #include <mutex>
71 #include <limits>
72 #include <string>
73 #include <vector>
74 
75 #include "utils/sync_task.h"
76 #include "utils/constants.h"
77 #include "cpuhint.h"
78 #include "hwc_display.h"
79 #include "hwc_layers.h"
80 #include "display_null.h"
81 
82 #include "gl_layer_stitch.h"
83 
84 namespace sdm {
85 
86 enum class LayerStitchTaskCode : int32_t {
87   kCodeGetInstance,
88   kCodeStitch,
89   kCodeDestroyInstance,
90 };
91 
92 struct LayerStitchGetInstanceContext : public SyncTask<LayerStitchTaskCode>::TaskContext {
93   LayerBuffer *output_buffer = NULL;
94 };
95 
96 struct LayerStitchContext : public SyncTask<LayerStitchTaskCode>::TaskContext {
97   vector<StitchParams> stitch_params;
98   shared_ptr<Fence> src_acquire_fence = nullptr;
99   shared_ptr<Fence> dst_acquire_fence = nullptr;
100   shared_ptr<Fence> release_fence = nullptr;
101 };
102 
103 class HWCDisplayBuiltIn : public HWCDisplay, public SyncTask<LayerStitchTaskCode>::TaskHandler {
104  public:
105   enum {
106     SET_METADATA_DYN_REFRESH_RATE,
107     SET_BINDER_DYN_REFRESH_RATE,
108     SET_DISPLAY_MODE,
109     SET_QDCM_SOLID_FILL_INFO,
110     UNSET_QDCM_SOLID_FILL_INFO,
111     SET_QDCM_SOLID_FILL_RECT,
112   };
113 
114   static int Create(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
115                     HWCCallbacks *callbacks,  HWCDisplayEventHandler *event_handler,
116                     qService::QService *qservice, hwc2_display_t id, int32_t sdm_id,
117                     HWCDisplay **hwc_display);
118   static void Destroy(HWCDisplay *hwc_display);
119   virtual int Init();
120   virtual HWC2::Error Validate(uint32_t *out_num_types, uint32_t *out_num_requests);
121   virtual HWC2::Error Present(shared_ptr<Fence> *out_retire_fence);
122   virtual HWC2::Error CommitLayerStack();
123   virtual HWC2::Error GetColorModes(uint32_t *out_num_modes, ColorMode *out_modes);
124   virtual HWC2::Error SetColorMode(ColorMode mode);
125   virtual HWC2::Error GetRenderIntents(ColorMode mode, uint32_t *out_num_intents,
126                                        RenderIntent *out_intents);
127   virtual HWC2::Error SetColorModeWithRenderIntent(ColorMode mode, RenderIntent intent);
128   virtual HWC2::Error SetColorModeById(int32_t color_mode_id);
129   virtual HWC2::Error SetColorModeFromClientApi(int32_t color_mode_id);
130   virtual HWC2::Error SetColorTransform(const float *matrix, android_color_transform_t hint);
131   virtual HWC2::Error RestoreColorTransform();
132   virtual int Perform(uint32_t operation, ...);
133   virtual int HandleSecureSession(const std::bitset<kSecureMax> &secure_session,
134                                   bool *power_on_pending, bool is_active_secure_display);
135   virtual void SetIdleTimeoutMs(uint32_t timeout_ms, uint32_t inactive_ms);
136   virtual HWC2::Error SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type,
137                                          int32_t format, bool post_processed);
138   virtual int FrameCaptureAsync(const BufferInfo &output_buffer_info, bool post_processed);
GetFrameCaptureStatus()139   virtual int GetFrameCaptureStatus() { return frame_capture_status_; }
140   virtual DisplayError SetDetailEnhancerConfig(const DisplayDetailEnhancerData &de_data);
141   virtual DisplayError SetHWDetailedEnhancerConfig(void *params);
142   virtual DisplayError ControlPartialUpdate(bool enable, uint32_t *pending);
143   virtual HWC2::Error SetReadbackBuffer(const native_handle_t *buffer,
144                                         shared_ptr<Fence> acquire_fence,
145                                         bool post_processed_output, CWBClient client);
146   virtual HWC2::Error GetReadbackBufferFence(shared_ptr<Fence> *release_fence);
147   virtual HWC2::Error SetQSyncMode(QSyncMode qsync_mode);
148   virtual DisplayError ControlIdlePowerCollapse(bool enable, bool synchronous);
149   virtual HWC2::Error SetDisplayDppsAdROI(uint32_t h_start, uint32_t h_end, uint32_t v_start,
150                                           uint32_t v_end, uint32_t factor_in, uint32_t factor_out);
151   virtual DisplayError SetDynamicDSIClock(uint64_t bitclk);
152   virtual DisplayError GetDynamicDSIClock(uint64_t *bitclk);
153   virtual DisplayError GetSupportedDSIClock(std::vector<uint64_t> *bitclk_rates);
154   virtual DisplayError SetStandByMode(bool enable, bool is_twm);
155   virtual DisplayError DelayFirstCommit();
156   virtual HWC2::Error UpdateDisplayId(hwc2_display_t id);
157   virtual HWC2::Error SetPendingRefresh();
158   virtual HWC2::Error SetPanelBrightness(float brightness);
159   virtual HWC2::Error GetPanelBrightness(float *brightness);
160   virtual HWC2::Error GetPanelMaxBrightness(uint32_t *max_brightness_level);
161   virtual DisplayError SetCurrentPanelGammaSource(enum PanelGammaSource source) override;
GetCurrentPanelGammaSource()162   virtual PanelGammaSource GetCurrentPanelGammaSource() const override { return current_panel_gamma_source_; };
163   virtual DisplayError TeardownConcurrentWriteback(void);
SetFastPathComposition(bool enable)164   virtual void SetFastPathComposition(bool enable) {
165     fast_path_composition_ = enable && !readback_buffer_queued_;
166   }
167   virtual HWC2::Error SetFrameTriggerMode(uint32_t mode);
168   virtual HWC2::Error SetBLScale(uint32_t level);
169   virtual HWC2::Error UpdatePowerMode(HWC2::PowerMode mode);
170   virtual HWC2::Error SetClientTarget(buffer_handle_t target, shared_ptr<Fence> acquire_fence,
171                                       int32_t dataspace, hwc_region_t damage);
172   virtual bool IsSmartPanelConfig(uint32_t config_id);
173   virtual bool HasSmartPanelConfig(void);
174   virtual int Deinit();
175   virtual bool IsQsyncCallbackNeeded(bool *qsync_enabled, int32_t *refresh_rate,
176                                      int32_t *qsync_refresh_rate);
177   virtual int PostInit();
178 
179   virtual HWC2::Error SetDisplayedContentSamplingEnabledVndService(bool enabled);
180   virtual HWC2::Error SetDisplayedContentSamplingEnabled(int32_t enabled, uint8_t component_mask,
181                                                          uint64_t max_frames);
182   virtual HWC2::Error GetDisplayedContentSamplingAttributes(int32_t *format, int32_t *dataspace,
183                                                             uint8_t *supported_components);
184   virtual HWC2::Error GetDisplayedContentSample(
185       uint64_t max_frames, uint64_t timestamp, uint64_t *numFrames,
186       int32_t samples_size[NUM_HISTOGRAM_COLOR_COMPONENTS],
187       uint64_t *samples[NUM_HISTOGRAM_COLOR_COMPONENTS]);
188   void Dump(std::ostringstream *os) override;
189   virtual HWC2::Error SetPowerMode(HWC2::PowerMode mode, bool teardown);
190   virtual bool HasReadBackBufferSupport();
191   virtual bool IsDisplayIdle();
192 
193   virtual bool IsHbmSupported() override;
194   virtual HWC2::Error SetHbm(HbmState state, HbmClient client) override;
195   virtual HbmState GetHbm() override;
196 
197  private:
198   HWCDisplayBuiltIn(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
199                     HWCCallbacks *callbacks, HWCDisplayEventHandler *event_handler,
200                     qService::QService *qservice, hwc2_display_t id, int32_t sdm_id);
201   void SetMetaDataRefreshRateFlag(bool enable);
202   virtual DisplayError SetDisplayMode(uint32_t mode);
203   virtual DisplayError DisablePartialUpdateOneFrame();
204   void ProcessBootAnimCompleted(void);
205   void SetQDCMSolidFillInfo(bool enable, const LayerSolidFill &color);
206   void ToggleCPUHint(bool set);
207   void ForceRefreshRate(uint32_t refresh_rate);
208   uint32_t GetOptimalRefreshRate(bool one_updating_layer);
209   void HandleFrameOutput();
210   void HandleFrameDump();
211   void HandleFrameCapture();
212   bool CanSkipCommit();
213   DisplayError SetMixerResolution(uint32_t width, uint32_t height);
214   DisplayError GetMixerResolution(uint32_t *width, uint32_t *height);
215   HWC2::Error CommitStitchLayers();
216   void AppendStitchLayer();
217   bool InitLayerStitch();
218   void InitStitchTarget();
219   bool AllocateStitchBuffer();
220   void CacheAvrStatus();
221   void PostCommitStitchLayers();
222   void SetCpuPerfHintLargeCompCycle();
223   int GetBwCode(const DisplayConfigVariableInfo &attr);
224   void SetBwLimitHint(bool enable);
225   void SetPartialUpdate(DisplayConfigFixedInfo fixed_info);
226   uint32_t GetUpdatingAppLayersCount();
227   void ValidateUiScaling();
228   HWC2::Error ApplyHbmLocked() REQUIRES(hbm_mutex);
229 
230   // SyncTask methods.
231   void OnTask(const LayerStitchTaskCode &task_code,
232               SyncTask<LayerStitchTaskCode>::TaskContext *task_context);
233 
234   constexpr static int kBwLow = 2;
235   constexpr static int kBwMedium = 3;
236   constexpr static int kBwHigh = 4;
237   const int kPerfHintLargeCompCycle = 0x00001097;
238   BufferAllocator *buffer_allocator_ = nullptr;
239   CPUHint *cpu_hint_ = nullptr;
240   CWBClient cwb_client_ = kCWBClientNone;
241 
242   // Builtin readback buffer configuration
243   LayerBuffer output_buffer_ = {};
244   bool post_processed_output_ = false;
245   bool readback_buffer_queued_ = false;
246   bool readback_configured_ = false;
247 
248   // Members for N frame output dump to file
249   bool dump_output_to_file_ = false;
250   BufferInfo output_buffer_info_ = {};
251   void *output_buffer_base_ = nullptr;
252   bool pending_refresh_ = true;
253   bool enable_optimize_refresh_ = false;
254 
255   // Members for 1 frame capture in a client provided buffer
256   bool frame_capture_buffer_queued_ = false;
257   int frame_capture_status_ = -EAGAIN;
258   bool is_primary_ = false;
259   bool disable_layer_stitch_ = true;
260   HWCLayer* stitch_target_ = nullptr;
261   SyncTask<LayerStitchTaskCode> layer_stitch_task_;
262   GLLayerStitch* gl_layer_stitch_ = nullptr;
263   BufferInfo buffer_info_ = {};
264   DisplayConfigVariableInfo fb_config_ = {};
265 
266   enum PanelGammaSource current_panel_gamma_source_ = kGammaDefault;
267 
268   bool qsync_enabled_ = false;
269   bool qsync_reconfigured_ = false;
270   // Members for Color sampling feature
271   DisplayError HistogramEvent(int fd, uint32_t blob_id) override;
272 #ifndef TARGET_HEADLESS
273   histogram::HistogramCollector histogram;
274 #endif
275   std::mutex sampling_mutex;
276   bool api_sampling_vote = false;
277   bool vndservice_sampling_vote = false;
278   int perf_hint_window_ = 0;
279   int perf_hint_large_comp_cycle_ = 0;
280   int curr_refresh_rate_ = 0;
281   bool is_smart_panel_ = false;
282   const char *kDisplayBwName = "display_bw";
283   bool enable_bw_limits_ = false;
284   bool disable_dyn_fps_ = false;
285   bool enhance_idle_time_ = false;
286   bool force_reset_validate_ = false;
287 
288   // NULL display
289   DisplayNull display_null_;
290   DisplayInterface *stored_display_intf_ = NULL;
291 
292   // Members for HBM feature
293   static constexpr const char kHighBrightnessModeNode[] =
294       "/sys/class/backlight/panel0-backlight/hbm_mode";
295   static constexpr float hbm_threshold_pct_ = 0.5f;
296   const bool mHasHbmNode = !access(kHighBrightnessModeNode, F_OK);
297   std::mutex hbm_mutex;
298   float hbm_threshold_px_ = std::numeric_limits<float>::max();
299   bool has_config_hbm_threshold_ = false;
300   bool high_brightness_mode_ = false;
301   HbmState mHbmSates[CLIENT_MAX] GUARDED_BY(hbm_mutex) = {HbmState::OFF};
302   HbmState mCurHbmState GUARDED_BY(hbm_mutex) = HbmState::OFF;
303 };
304 
305 }  // namespace sdm
306 
307 #endif  // __HWC_DISPLAY_BUILTIN_H__
308