1 /*
2  * Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
3  * Not a Contribution.
4  *
5  * Copyright 2015 The Android Open Source Project
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef __HWC_DISPLAY_H__
21 #define __HWC_DISPLAY_H__
22 
23 #include <sys/stat.h>
24 #include <QService.h>
25 #include <core/core_interface.h>
26 #include <hardware/hwcomposer.h>
27 #include <private/color_params.h>
28 #include <qdMetaData.h>
29 #include <map>
30 #include <queue>
31 #include <set>
32 #include <string>
33 #include <utility>
34 #include <vector>
35 
36 #include "hwc_buffer_allocator.h"
37 #include "hwc_callbacks.h"
38 #include "hwc_layers.h"
39 
40 namespace sdm {
41 
42 class BlitEngine;
43 class HWCToneMapper;
44 
45 // Subclasses set this to their type. This has to be different from DisplayType.
46 // This is to avoid RTTI and dynamic_cast
47 enum DisplayClass {
48   DISPLAY_CLASS_PRIMARY,
49   DISPLAY_CLASS_EXTERNAL,
50   DISPLAY_CLASS_VIRTUAL,
51   DISPLAY_CLASS_NULL
52 };
53 
54 class HWCColorMode {
55  public:
56   explicit HWCColorMode(DisplayInterface *display_intf);
~HWCColorMode()57   ~HWCColorMode() {}
58   HWC2::Error Init();
59   HWC2::Error DeInit();
60   void Dump(std::ostringstream* os);
61   uint32_t GetColorModeCount();
62   HWC2::Error GetColorModes(uint32_t *out_num_modes, android_color_mode_t *out_modes);
63   HWC2::Error SetColorMode(android_color_mode_t mode);
64   HWC2::Error SetColorTransform(const float *matrix, android_color_transform_t hint);
65 
66  private:
67   static const uint32_t kColorTransformMatrixCount = 16;
68 
69   HWC2::Error HandleColorModeTransform(android_color_mode_t mode,
70                                        android_color_transform_t hint, const double *matrix);
71   void PopulateColorModes();
72   void PopulateTransform(const android_color_mode_t &mode,
73                          const std::string &color_mode, const std::string &color_transform);
74   template <class T>
CopyColorTransformMatrix(const T * input_matrix,double * output_matrix)75   void CopyColorTransformMatrix(const T *input_matrix, double *output_matrix) {
76     for (uint32_t i = 0; i < kColorTransformMatrixCount; i++) {
77       output_matrix[i] = static_cast<double>(input_matrix[i]);
78     }
79   }
80   HWC2::Error ApplyDefaultColorMode();
81 
82   DisplayInterface *display_intf_ = NULL;
83   android_color_mode_t current_color_mode_ = HAL_COLOR_MODE_NATIVE;
84   android_color_transform_t current_color_transform_ = HAL_COLOR_TRANSFORM_IDENTITY;
85   typedef std::map<android_color_transform_t, std::string> TransformMap;
86   std::map<android_color_mode_t, TransformMap> color_mode_transform_map_ = {};
87   double color_matrix_[kColorTransformMatrixCount] = { 1.0, 0.0, 0.0, 0.0, \
88                                                        0.0, 1.0, 0.0, 0.0, \
89                                                        0.0, 0.0, 1.0, 0.0, \
90                                                        0.0, 0.0, 0.0, 1.0 };
91 };
92 
93 class HWCDisplay : public DisplayEventHandler {
94  public:
95   enum DisplayStatus {
96     kDisplayStatusInvalid = -1,
97     kDisplayStatusOffline,
98     kDisplayStatusOnline,
99     kDisplayStatusPause,
100     kDisplayStatusResume,
101   };
102 
~HWCDisplay()103   virtual ~HWCDisplay() {}
104   virtual int Init();
105   virtual int Deinit();
106 
107   // Framebuffer configurations
108   virtual void SetIdleTimeoutMs(uint32_t timeout_ms);
109   virtual void SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type);
110   virtual DisplayError SetMaxMixerStages(uint32_t max_mixer_stages);
ControlPartialUpdate(bool enable,uint32_t * pending)111   virtual DisplayError ControlPartialUpdate(bool enable, uint32_t *pending) {
112     return kErrorNotSupported;
113   }
114   virtual HWC2::PowerMode GetLastPowerMode();
115   virtual int SetFrameBufferResolution(uint32_t x_pixels, uint32_t y_pixels);
116   virtual void GetFrameBufferResolution(uint32_t *x_pixels, uint32_t *y_pixels);
117   virtual int SetDisplayStatus(DisplayStatus display_status);
118   virtual int OnMinHdcpEncryptionLevelChange(uint32_t min_enc_level);
119   virtual int Perform(uint32_t operation, ...);
120   virtual void SetSecureDisplay(bool secure_display_active);
121   virtual DisplayError SetMixerResolution(uint32_t width, uint32_t height);
122   virtual DisplayError GetMixerResolution(uint32_t *width, uint32_t *height);
123   virtual void GetPanelResolution(uint32_t *width, uint32_t *height);
124   virtual std::string Dump(void);
125 
126   // Captures frame output in the buffer specified by output_buffer_info. The API is
127   // non-blocking and the client is expected to check operation status later on.
128   // Returns -1 if the input is invalid.
FrameCaptureAsync(const BufferInfo & output_buffer_info,bool post_processed)129   virtual int FrameCaptureAsync(const BufferInfo &output_buffer_info, bool post_processed) {
130     return -1;
131   }
132   // Returns the status of frame capture operation requested with FrameCaptureAsync().
133   // -EAGAIN : No status obtain yet, call API again after another frame.
134   // < 0 : Operation happened but failed.
135   // 0 : Success.
GetFrameCaptureStatus()136   virtual int GetFrameCaptureStatus() { return -EAGAIN; }
137 
SetDetailEnhancerConfig(const DisplayDetailEnhancerData & de_data)138   virtual DisplayError SetDetailEnhancerConfig(const DisplayDetailEnhancerData &de_data) {
139     return kErrorNotSupported;
140   }
141 
142   // Display Configurations
143   virtual int SetActiveDisplayConfig(uint32_t config);
144   virtual int GetActiveDisplayConfig(uint32_t *config);
145   virtual int GetDisplayConfigCount(uint32_t *count);
146   virtual int GetDisplayAttributesForConfig(int config,
147                                             DisplayConfigVariableInfo *display_attributes);
SetState(bool connected)148   virtual int SetState(bool connected) {
149     return kErrorNotSupported;
150   }
SetStandByMode(bool enable)151   virtual DisplayError SetStandByMode(bool enable) {
152     return kErrorNotSupported;
153   }
154   int SetPanelBrightness(int level);
155   int GetPanelBrightness(int *level);
156   int ToggleScreenUpdates(bool enable);
157   int ColorSVCRequestRoute(const PPDisplayAPIPayload &in_payload, PPDisplayAPIPayload *out_payload,
158                            PPPendingParams *pending_action);
159   void SolidFillPrepare();
160   void SolidFillCommit();
161   DisplayClass GetDisplayClass();
162   int GetVisibleDisplayRect(hwc_rect_t *rect);
163   void BuildLayerStack(void);
164   void BuildSolidFillStack(void);
165   HWCLayer *GetHWCLayer(hwc2_layer_t layer);
ResetValidation()166   void ResetValidation() { validated_.reset(); }
GetGeometryChanges()167   uint32_t GetGeometryChanges() { return geometry_changes_; }
168 
169   // HWC2 APIs
170   virtual HWC2::Error AcceptDisplayChanges(void);
171   virtual HWC2::Error GetActiveConfig(hwc2_config_t *out_config);
172   virtual HWC2::Error SetActiveConfig(hwc2_config_t config);
173   virtual HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
174                                       int32_t dataspace, hwc_region_t damage);
SetColorMode(android_color_mode_t mode)175   virtual HWC2::Error SetColorMode(android_color_mode_t mode) {
176     return HWC2::Error::Unsupported;
177   }
SetColorTransform(const float * matrix,android_color_transform_t hint)178   virtual HWC2::Error SetColorTransform(const float *matrix, android_color_transform_t hint) {
179     return HWC2::Error::Unsupported;
180   }
HandleColorModeTransform(android_color_mode_t mode,android_color_transform_t hint,const double * matrix)181   virtual HWC2::Error HandleColorModeTransform(android_color_mode_t mode,
182                                                android_color_transform_t hint,
183                                                const double *matrix) {
184     return HWC2::Error::Unsupported;
185   }
186   virtual HWC2::Error GetDisplayConfigs(uint32_t *out_num_configs, hwc2_config_t *out_configs);
187   virtual HWC2::Error GetDisplayAttribute(hwc2_config_t config, HWC2::Attribute attribute,
188                                           int32_t *out_value);
189   virtual HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height, int32_t format,
190                                              int32_t dataspace);
191   virtual HWC2::Error GetColorModes(uint32_t *outNumModes, android_color_mode_t *outModes);
192   virtual HWC2::Error GetChangedCompositionTypes(uint32_t *out_num_elements,
193                                                  hwc2_layer_t *out_layers, int32_t *out_types);
194   virtual HWC2::Error GetDisplayRequests(int32_t *out_display_requests, uint32_t *out_num_elements,
195                                          hwc2_layer_t *out_layers, int32_t *out_layer_requests);
196   virtual HWC2::Error GetDisplayName(uint32_t *out_size, char *out_name);
197   virtual HWC2::Error GetDisplayType(int32_t *out_type);
198   virtual HWC2::Error SetCursorPosition(hwc2_layer_t layer, int x, int y);
199   virtual HWC2::Error SetVsyncEnabled(HWC2::Vsync enabled);
200   virtual HWC2::Error SetPowerMode(HWC2::PowerMode mode);
201   virtual HWC2::Error CreateLayer(hwc2_layer_t *out_layer_id);
202   virtual HWC2::Error DestroyLayer(hwc2_layer_t layer_id);
203   virtual HWC2::Error SetLayerZOrder(hwc2_layer_t layer_id, uint32_t z);
204   virtual HWC2::Error Validate(uint32_t *out_num_types, uint32_t *out_num_requests) = 0;
205   virtual HWC2::Error GetReleaseFences(uint32_t *out_num_elements, hwc2_layer_t *out_layers,
206                                        int32_t *out_fences);
207   virtual HWC2::Error Present(int32_t *out_retire_fence) = 0;
208   virtual HWC2::Error GetHdrCapabilities(uint32_t *out_num_types, int32_t* out_types,
209                                          float* out_max_luminance,
210                                          float* out_max_average_luminance,
211                                          float* out_min_luminance);
212 
213  protected:
214   // Maximum number of layers supported by display manager.
215   static const uint32_t kMaxLayerCount = 32;
216 
217   HWCDisplay(CoreInterface *core_intf, HWCCallbacks *callbacks, DisplayType type, hwc2_display_t id,
218              bool needs_blit, qService::QService *qservice, DisplayClass display_class,
219              BufferAllocator *buffer_allocator);
220 
221   // DisplayEventHandler methods
222   virtual DisplayError VSync(const DisplayEventVSync &vsync);
223   virtual DisplayError Refresh();
224   virtual DisplayError CECMessage(char *message);
225   virtual DisplayError HandleEvent(DisplayEvent event);
226   virtual void DumpOutputBuffer(const BufferInfo &buffer_info, void *base, int fence);
227   virtual HWC2::Error PrepareLayerStack(uint32_t *out_num_types, uint32_t *out_num_requests);
228   virtual HWC2::Error CommitLayerStack(void);
229   virtual HWC2::Error PostCommitLayerStack(int32_t *out_retire_fence);
DisablePartialUpdateOneFrame()230   virtual DisplayError DisablePartialUpdateOneFrame() {
231     return kErrorNotSupported;
232   }
233   LayerBufferFormat GetSDMFormat(const int32_t &source, const int flags);
234   const char *GetDisplayString();
235   void MarkLayersForGPUBypass(void);
236   void MarkLayersForClientComposition(void);
237   virtual void ApplyScanAdjustment(hwc_rect_t *display_frame);
238   bool SingleLayerUpdating(void);
239   bool IsSurfaceUpdated(const std::vector<LayerRect> &dirty_regions);
240   bool IsLayerUpdating(const Layer *layer);
241   uint32_t SanitizeRefreshRate(uint32_t req_refresh_rate);
242   virtual void CloseAcquireFds();
GetUnderScanConfig()243   virtual void GetUnderScanConfig() { }
244 
245   enum {
246     INPUT_LAYER_DUMP,
247     OUTPUT_LAYER_DUMP,
248   };
249 
250   static std::bitset<kDisplayMax> validated_;
251   CoreInterface *core_intf_ = nullptr;
252   HWCCallbacks *callbacks_  = nullptr;
253   HWCBufferAllocator *buffer_allocator_ = NULL;
254   DisplayType type_;
255   hwc2_display_t id_;
256   bool needs_blit_ = false;
257   DisplayInterface *display_intf_ = NULL;
258   LayerStack layer_stack_;
259   HWCLayer *client_target_ = nullptr;                   // Also known as framebuffer target
260   std::map<hwc2_layer_t, HWCLayer *> layer_map_;        // Look up by Id - TODO
261   std::multiset<HWCLayer *, SortLayersByZ> layer_set_;  // Maintain a set sorted by Z
262   std::map<hwc2_layer_t, HWC2::Composition> layer_changes_;
263   std::map<hwc2_layer_t, HWC2::LayerRequest> layer_requests_;
264   bool flush_on_error_ = false;
265   bool flush_ = false;
266   uint32_t dump_frame_count_ = 0;
267   uint32_t dump_frame_index_ = 0;
268   bool dump_input_layers_ = false;
269   HWC2::PowerMode last_power_mode_;
270   bool swap_interval_zero_ = false;
271   bool display_paused_ = false;
272   uint32_t min_refresh_rate_ = 0;
273   uint32_t max_refresh_rate_ = 0;
274   uint32_t current_refresh_rate_ = 0;
275   bool use_metadata_refresh_rate_ = false;
276   uint32_t metadata_refresh_rate_ = 0;
277   uint32_t force_refresh_rate_ = 0;
278   bool boot_animation_completed_ = false;
279   bool shutdown_pending_ = false;
280   bool use_blit_comp_ = false;
281   bool secure_display_active_ = false;
282   bool skip_prepare_ = false;
283   bool solid_fill_enable_ = false;
284   Layer *solid_fill_layer_ = NULL;
285   LayerRect solid_fill_rect_ = {};
286   uint32_t solid_fill_color_ = 0;
287   LayerRect display_rect_;
288   bool color_tranform_failed_ = false;
289   HWCColorMode *color_mode_ = NULL;
290   HWCToneMapper *tone_mapper_ = nullptr;
291   uint32_t num_configs_ = 0;
292   int disable_hdr_handling_ = 0;  // disables HDR handling.
293 
294  private:
295   void DumpInputBuffers(void);
296   bool CanSkipValidate();
297   qService::QService *qservice_ = NULL;
298   DisplayClass display_class_;
299   uint32_t geometry_changes_ = GeometryChanges::kNone;
300   bool skip_validate_ = false;
301 };
302 
Perform(uint32_t operation,...)303 inline int HWCDisplay::Perform(uint32_t operation, ...) {
304   return 0;
305 }
306 
307 }  // namespace sdm
308 
309 #endif  // __HWC_DISPLAY_H__
310