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 #ifndef __HWC_DISPLAY_BUILTIN_H__
31 #define __HWC_DISPLAY_BUILTIN_H__
32 
33 #include <hardware/google/light/1.0/ILight.h>
34 #include <limits>
35 #include <string>
36 #include <vector>
37 #include <mutex>
38 
39 #include "cpuhint.h"
40 #include "hwc_display.h"
41 
42 namespace sdm {
43 
44 class HWCDisplayBuiltIn : public HWCDisplay {
45  public:
46   enum {
47     SET_METADATA_DYN_REFRESH_RATE,
48     SET_BINDER_DYN_REFRESH_RATE,
49     SET_DISPLAY_MODE,
50     SET_QDCM_SOLID_FILL_INFO,
51     UNSET_QDCM_SOLID_FILL_INFO,
52     SET_QDCM_SOLID_FILL_RECT,
53   };
54 
55   static int Create(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
56                     HWCCallbacks *callbacks,  HWCDisplayEventHandler *event_handler,
57                     qService::QService *qservice, hwc2_display_t id, int32_t sdm_id,
58                     HWCDisplay **hwc_display);
59   static void Destroy(HWCDisplay *hwc_display);
60   virtual int Init();
61   virtual int Deinit() override;
62   virtual HWC2::Error Validate(uint32_t *out_num_types, uint32_t *out_num_requests);
63   virtual HWC2::Error Present(int32_t *out_retire_fence);
64   virtual HWC2::Error CommitLayerStack();
65   virtual HWC2::Error GetColorModes(uint32_t *out_num_modes, ColorMode *out_modes);
66   virtual HWC2::Error SetColorMode(ColorMode mode);
67   virtual HWC2::Error GetRenderIntents(ColorMode mode, uint32_t *out_num_intents,
68                                        RenderIntent *out_intents);
69   virtual HWC2::Error SetColorModeWithRenderIntent(ColorMode mode, RenderIntent intent);
70   virtual HWC2::Error SetColorModeById(int32_t color_mode_id);
71   virtual HWC2::Error SetColorModeFromClientApi(int32_t color_mode_id);
72   virtual HWC2::Error SetColorTransform(const float *matrix, android_color_transform_t hint);
73   virtual HWC2::Error RestoreColorTransform();
74   virtual int Perform(uint32_t operation, ...);
75   virtual int HandleSecureSession(const std::bitset<kSecureMax> &secure_session,
76                                   bool *power_on_pending);
77   virtual DisplayError Refresh();
78   virtual void SetIdleTimeoutMs(uint32_t timeout_ms);
79   virtual HWC2::Error SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type,
80                                          int32_t format, bool post_processed);
81   virtual int FrameCaptureAsync(const BufferInfo &output_buffer_info, bool post_processed);
GetFrameCaptureStatus()82   virtual int GetFrameCaptureStatus() { return frame_capture_status_; }
83   virtual DisplayError SetDetailEnhancerConfig(const DisplayDetailEnhancerData &de_data);
84   virtual DisplayError ControlPartialUpdate(bool enable, uint32_t *pending);
85   virtual HWC2::Error SetReadbackBuffer(const native_handle_t *buffer, int32_t acquire_fence,
86                                         bool post_processed_output);
87   virtual HWC2::Error GetReadbackBufferFence(int32_t *release_fence);
88   virtual HWC2::Error SetQSyncMode(QSyncMode qsync_mode);
89   virtual DisplayError ControlIdlePowerCollapse(bool enable, bool synchronous);
90   virtual HWC2::Error SetDisplayDppsAdROI(uint32_t h_start, uint32_t h_end, uint32_t v_start,
91                                           uint32_t v_end, uint32_t factor_in, uint32_t factor_out);
92   virtual DisplayError SetDynamicDSIClock(uint64_t bitclk);
93   virtual DisplayError GetDynamicDSIClock(uint64_t *bitclk);
94   virtual DisplayError GetSupportedDSIClock(std::vector<uint64_t> *bitclk_rates);
95   virtual HWC2::Error UpdateDisplayId(hwc2_display_t id);
96   virtual HWC2::Error SetPendingRefresh();
97   virtual HWC2::Error SetPanelBrightness(int32_t level) override;
98   virtual HWC2::Error GetPanelBrightness(int32_t &level) const override;
99   virtual HWC2::Error GetPanelMaxBrightness(int32_t &max_brightness_level) const override;
100   virtual DisplayError TeardownConcurrentWriteback(void);
SetFastPathComposition(bool enable)101   virtual void SetFastPathComposition(bool enable) {
102     fast_path_composition_ = enable && !readback_buffer_queued_;
103   }
104   virtual HWC2::Error PostCommitLayerStack(int32_t *out_retire_fence);
105 
106   virtual HWC2::Error SetDisplayedContentSamplingEnabledVndService(bool enabled);
107   virtual HWC2::Error SetDisplayedContentSamplingEnabled(int32_t enabled, uint8_t component_mask, uint64_t max_frames) override;
108   virtual HWC2::Error GetDisplayedContentSamplingAttributes(int32_t* format, int32_t* dataspace,
109                                                             uint8_t* supported_components) override;
110   virtual HWC2::Error GetDisplayedContentSample(uint64_t max_frames,
111                                                 uint64_t timestamp, uint64_t* numFrames,
112                                                 int32_t samples_size[NUM_HISTOGRAM_COLOR_COMPONENTS],
113                                                 uint64_t* samples[NUM_HISTOGRAM_COLOR_COMPONENTS]) override;
114   virtual HWC2::Error GetDisplayBrightnessSupport(bool *out_support) override;
115   virtual HWC2::Error GetProtectedContentsSupport(bool *out_support) override;
116   std::string Dump() override;
117   virtual HWC2::Error UpdatePowerMode(HWC2::PowerMode mode);
118 
119  private:
120   HWCDisplayBuiltIn(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
121                     HWCCallbacks *callbacks, HWCDisplayEventHandler *event_handler,
122                     qService::QService *qservice, hwc2_display_t id, int32_t sdm_id);
123   void SetMetaDataRefreshRateFlag(bool enable);
124   virtual DisplayError SetDisplayMode(uint32_t mode);
125   virtual DisplayError DisablePartialUpdateOneFrame();
126   void SetQDCMSolidFillInfo(bool enable, const LayerSolidFill &color);
127   void ToggleCPUHint(bool set);
128   void ForceRefreshRate(uint32_t refresh_rate);
129   uint32_t GetOptimalRefreshRate(bool one_updating_layer);
130   void HandleFrameOutput();
131   void HandleFrameDump();
132   void HandleFrameCapture();
133   bool CanSkipCommit();
134   DisplayError SetMixerResolution(uint32_t width, uint32_t height);
135   DisplayError GetMixerResolution(uint32_t *width, uint32_t *height);
136   class PMICInterface {
137    public:
PMICInterface()138     PMICInterface() { }
~PMICInterface()139     ~PMICInterface() { }
140     DisplayError Init();
141     void Deinit();
142     DisplayError Notify(SecureEvent event);
143 
144    private:
145     int fd_lcd_bias_ = -1;
146     int fd_wled_ = -1;
147   };
148 
149   BufferAllocator *buffer_allocator_ = nullptr;
150   CPUHint *cpu_hint_ = nullptr;
151 
152   // Builtin readback buffer configuration
153   LayerBuffer output_buffer_ = {};
154   bool post_processed_output_ = false;
155   bool readback_buffer_queued_ = false;
156   bool readback_configured_ = false;
157 
158   // Members for N frame output dump to file
159   bool dump_output_to_file_ = false;
160   BufferInfo output_buffer_info_ = {};
161   void *output_buffer_base_ = nullptr;
162   int default_mode_status_ = 0;
163   bool pending_refresh_ = true;
164   bool enable_optimize_refresh_ = false;
165   bool hdr_present_ = false;
166 
167   // Members for 1 frame capture in a client provided buffer
168   bool frame_capture_buffer_queued_ = false;
169   int frame_capture_status_ = -EAGAIN;
170 
171   // Members for HBM feature
172   static constexpr float hbm_threshold_pct_ = 0.5f;
173   float hbm_threshold_px_ = std::numeric_limits<float>::max();
174   android::sp<hardware::google::light::V1_0::ILight> hardware_ILight_ = nullptr;
175   bool has_init_light_server_ = false;
176   bool high_brightness_mode_ = false;
177 
178   // Members for Color sampling feature
179   DisplayError HistogramEvent(int fd, uint32_t blob_id) override;
180   histogram::HistogramCollector histogram;
181   std::mutex sampling_mutex;
182   bool api_sampling_vote = false;
183   bool vndservice_sampling_vote = false;
184 
185   // PMIC interface to notify secure display start/end
186   PMICInterface *pmic_intf_ = nullptr;
187   bool pmic_notification_pending_ = false;
188 };
189 
190 }  // namespace sdm
191 
192 #endif  // __HWC_DISPLAY_BUILTIN_H__
193