1 /*
2  * Copyright (c) 2014-2018, 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_PRIMARY_H__
31 #define __HWC_DISPLAY_PRIMARY_H__
32 
33 #include <hardware/google/light/1.0/ILight.h>
34 #include <limits>
35 #include <string>
36 #include <mutex>
37 
38 #include "cpuhint.h"
39 #include "hwc_display.h"
40 
41 namespace sdm {
42 
43 class HWCDisplayPrimary : public HWCDisplay {
44  public:
45   enum {
46     SET_METADATA_DYN_REFRESH_RATE,
47     SET_BINDER_DYN_REFRESH_RATE,
48     SET_DISPLAY_MODE,
49     SET_QDCM_SOLID_FILL_INFO,
50     UNSET_QDCM_SOLID_FILL_INFO,
51     SET_QDCM_SOLID_FILL_RECT,
52   };
53 
54   static int Create(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
55                     HWCCallbacks *callbacks, qService::QService *qservice,
56                     HWCDisplay **hwc_display);
57   static void Destroy(HWCDisplay *hwc_display);
58   virtual int Init();
59   virtual int Deinit() override;
60   virtual HWC2::Error Validate(uint32_t *out_num_types, uint32_t *out_num_requests);
61   virtual HWC2::Error Present(int32_t *out_retire_fence);
62   virtual HWC2::Error GetColorModes(uint32_t *out_num_modes, ColorMode *out_modes);
63   virtual HWC2::Error SetColorMode(ColorMode mode);
64   virtual HWC2::Error SetWhiteCompensation(bool enabled);
65   virtual HWC2::Error GetRenderIntents(ColorMode mode, uint32_t *out_num_intents,
66                                        RenderIntent *out_intents);
67   virtual HWC2::Error SetColorModeWithRenderIntent(ColorMode mode, RenderIntent intent);
68   virtual HWC2::Error SetColorModeById(int32_t color_mode_id);
69   virtual HWC2::Error SetColorTransform(const float *matrix, android_color_transform_t hint);
70   virtual HWC2::Error RestoreColorTransform();
71   virtual int Perform(uint32_t operation, ...);
72   virtual void SetSecureDisplay(bool secure_display_active);
73   virtual DisplayError Refresh();
74   virtual void SetIdleTimeoutMs(uint32_t timeout_ms);
75   virtual HWC2::Error SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type,
76                                          int32_t format, bool post_processed);
77   virtual int FrameCaptureAsync(const BufferInfo &output_buffer_info, bool post_processed);
78   virtual bool GetFrameCaptureFence(int32_t *release_fence);
79   virtual DisplayError SetDetailEnhancerConfig(const DisplayDetailEnhancerData &de_data);
80   virtual DisplayError ControlPartialUpdate(bool enable, uint32_t *pending);
81   virtual HWC2::Error SetReadbackBuffer(const native_handle_t *buffer, int32_t acquire_fence,
82                                         bool post_processed_output);
83   virtual HWC2::Error GetReadbackBufferFence(int32_t *release_fence);
84   virtual HWC2::Error PostCommitLayerStack(int32_t *out_retire_fence);
85   virtual HWC2::Error ControlIdlePowerCollapse(bool enable, bool synchronous);
86 
87   virtual HWC2::Error SetDisplayedContentSamplingEnabledVndService(bool enabled);
88   virtual HWC2::Error SetDisplayedContentSamplingEnabled(int32_t enabled, uint8_t component_mask, uint64_t max_frames) override;
89   virtual HWC2::Error GetDisplayedContentSamplingAttributes(int32_t* format, int32_t* dataspace,
90                                                             uint8_t* supported_components) override;
91   virtual HWC2::Error GetDisplayedContentSample(uint64_t max_frames,
92                                                 uint64_t timestamp, uint64_t* numFrames,
93                                                 int32_t samples_size[NUM_HISTOGRAM_COLOR_COMPONENTS],
94                                                 uint64_t* samples[NUM_HISTOGRAM_COLOR_COMPONENTS]) override;
95   std::string Dump() override;
96   virtual DisplayError TeardownConcurrentWriteback(void);
97 
98  private:
99   HWCDisplayPrimary(CoreInterface *core_intf, BufferAllocator *buffer_allocator,
100                     HWCCallbacks *callbacks, qService::QService *qservice);
101   void SetMetaDataRefreshRateFlag(bool enable);
102   virtual DisplayError SetDisplayMode(uint32_t mode);
103   virtual DisplayError DisablePartialUpdateOneFrame();
104   void ProcessBootAnimCompleted(void);
105   void SetQDCMSolidFillInfo(bool enable, const LayerSolidFill &color);
106   void ToggleCPUHint(bool set);
107   void ForceRefreshRate(uint32_t refresh_rate);
108   uint32_t GetOptimalRefreshRate(bool one_updating_layer);
109   void HandleFrameOutput();
110   void HandleFrameDump();
111   DisplayError SetMixerResolution(uint32_t width, uint32_t height);
112   DisplayError GetMixerResolution(uint32_t *width, uint32_t *height);
113   class PMICInterface {
114    public:
PMICInterface()115     PMICInterface() { }
~PMICInterface()116     ~PMICInterface() { }
117     DisplayError Init();
118     void Deinit();
119     DisplayError Notify(bool secure_display_start);
120 
121    private:
122     int fd_lcd_bias_ = -1;
123     int fd_wled_ = -1;
124   };
125 
126   BufferAllocator *buffer_allocator_ = nullptr;
127   CPUHint *cpu_hint_ = nullptr;
128 
129   // Primary readback buffer configuration
130   LayerBuffer output_buffer_ = {};
131   bool post_processed_output_ = false;
132   bool readback_buffer_queued_ = false;
133   bool readback_configured_ = false;
134 
135   // Members for N frame output dump to file
136   bool dump_output_to_file_ = false;
137   BufferInfo output_buffer_info_ = {};
138   void *output_buffer_base_ = nullptr;
139   int default_mode_status_ = 0;
140 
141   // Members for HBM feature
142   static constexpr float hbm_threshold_pct_ = 0.5f;
143   float hbm_threshold_px_ = std::numeric_limits<float>::max();
144   android::sp<hardware::google::light::V1_0::ILight> vendor_ILight_ = nullptr;
145   bool has_init_light_server_ = false;
146   bool high_brightness_mode_ = false;
147 
148   // Members for Color sampling feature
149   histogram::HistogramCollector histogram;
150   std::mutex sampling_mutex;
151   bool api_sampling_vote = false;
152   bool vndservice_sampling_vote = false;
153 
154   // PMIC interface to notify secure display start/end
155   PMICInterface *pmic_intf_ = nullptr;
156   bool pmic_notification_pending_ = false;
157 };
158 
159 }  // namespace sdm
160 
161 #endif  // __HWC_DISPLAY_PRIMARY_H__
162