1 /*
2  * Copyright (c) 2014-2017, 2019, 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 <QService.h>
24 #include <core/core_interface.h>
25 #include <hardware/hwcomposer.h>
26 #include <private/color_params.h>
27 #include <qdMetaData.h>
28 #include <map>
29 #include <queue>
30 #include <set>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 #include "hwc_buffer_allocator.h"
36 #include "hwc_callbacks.h"
37 #include "hwc_layers.h"
38 
39 namespace sdm {
40 
41 class BlitEngine;
42 class HWCToneMapper;
43 
44 // Subclasses set this to their type. This has to be different from DisplayType.
45 // This is to avoid RTTI and dynamic_cast
46 enum DisplayClass {
47   DISPLAY_CLASS_PRIMARY,
48   DISPLAY_CLASS_EXTERNAL,
49   DISPLAY_CLASS_VIRTUAL,
50   DISPLAY_CLASS_NULL
51 };
52 
53 class HWCColorMode {
54  public:
55   explicit HWCColorMode(DisplayInterface *display_intf);
~HWCColorMode()56   ~HWCColorMode() {}
57   HWC2::Error Init();
58   HWC2::Error DeInit();
59   void Dump(std::ostringstream* os);
60   uint32_t GetColorModeCount();
61   HWC2::Error GetColorModes(uint32_t *out_num_modes, android_color_mode_t *out_modes);
62   HWC2::Error SetColorMode(android_color_mode_t mode);
63   HWC2::Error SetColorModeById(int32_t color_mode_id);
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:
~HWCDisplay()95   virtual ~HWCDisplay() {}
96   virtual int Init();
97   virtual int Deinit();
98 
99   // Framebuffer configurations
100   virtual void SetIdleTimeoutMs(uint32_t timeout_ms);
101   virtual void SetFrameDumpConfig(uint32_t count, uint32_t bit_mask_layer_type);
102   virtual DisplayError SetMaxMixerStages(uint32_t max_mixer_stages);
ControlPartialUpdate(bool enable,uint32_t * pending)103   virtual DisplayError ControlPartialUpdate(bool enable, uint32_t *pending) {
104     return kErrorNotSupported;
105   }
106   virtual HWC2::PowerMode GetLastPowerMode();
107   virtual int SetFrameBufferResolution(uint32_t x_pixels, uint32_t y_pixels);
108   virtual void GetFrameBufferResolution(uint32_t *x_pixels, uint32_t *y_pixels);
109   virtual int SetDisplayStatus(uint32_t display_status);
110   virtual int OnMinHdcpEncryptionLevelChange(uint32_t min_enc_level);
111   virtual int Perform(uint32_t operation, ...);
112   virtual void SetSecureDisplay(bool secure_display_active);
113   virtual DisplayError SetMixerResolution(uint32_t width, uint32_t height);
114   virtual DisplayError GetMixerResolution(uint32_t *width, uint32_t *height);
115   virtual void GetPanelResolution(uint32_t *width, uint32_t *height);
116   virtual std::string Dump(void);
117 
118   // Captures frame output in the buffer specified by output_buffer_info. The API is
119   // non-blocking and the client is expected to check operation status later on.
120   // Returns -1 if the input is invalid.
FrameCaptureAsync(const BufferInfo & output_buffer_info,bool post_processed)121   virtual int FrameCaptureAsync(const BufferInfo &output_buffer_info, bool post_processed) {
122     return -1;
123   }
124   // Returns the status of frame capture operation requested with FrameCaptureAsync().
125   // -EAGAIN : No status obtain yet, call API again after another frame.
126   // < 0 : Operation happened but failed.
127   // 0 : Success.
GetFrameCaptureStatus()128   virtual int GetFrameCaptureStatus() { return -EAGAIN; }
129 
SetDetailEnhancerConfig(const DisplayDetailEnhancerData & de_data)130   virtual DisplayError SetDetailEnhancerConfig(const DisplayDetailEnhancerData &de_data) {
131     return kErrorNotSupported;
132   }
133 
134   // Display Configurations
135   virtual int SetActiveDisplayConfig(int config);
136   virtual int GetActiveDisplayConfig(uint32_t *config);
137   virtual int GetDisplayConfigCount(uint32_t *count);
138   virtual int GetDisplayAttributesForConfig(int config,
139                                             DisplayConfigVariableInfo *display_attributes);
140 
141   int SetPanelBrightness(int level);
142   int GetPanelBrightness(int *level);
143   int ToggleScreenUpdates(bool enable);
144   int ColorSVCRequestRoute(const PPDisplayAPIPayload &in_payload, PPDisplayAPIPayload *out_payload,
145                            PPPendingParams *pending_action);
146   void SolidFillPrepare();
147   void SolidFillCommit();
148   DisplayClass GetDisplayClass();
149   int GetVisibleDisplayRect(hwc_rect_t *rect);
150   void BuildLayerStack(void);
151   void BuildSolidFillStack(void);
152   HWCLayer *GetHWCLayer(hwc2_layer_t layer);
153 
154   // HWC2 APIs
155   virtual HWC2::Error AcceptDisplayChanges(void);
156   virtual HWC2::Error GetActiveConfig(hwc2_config_t *out_config);
157   virtual HWC2::Error SetActiveConfig(hwc2_config_t config);
158   virtual HWC2::Error SetClientTarget(buffer_handle_t target, int32_t acquire_fence,
159                                       int32_t dataspace, hwc_region_t damage);
SetColorMode(android_color_mode_t mode)160   virtual HWC2::Error SetColorMode(android_color_mode_t mode) {
161     return HWC2::Error::Unsupported;
162   }
SetColorModeById(int32_t color_mode_id)163   virtual HWC2::Error SetColorModeById(int32_t color_mode_id) {
164     return HWC2::Error::Unsupported;
165   }
SetColorTransform(const float * matrix,android_color_transform_t hint)166   virtual HWC2::Error SetColorTransform(const float *matrix, android_color_transform_t hint) {
167     return HWC2::Error::Unsupported;
168   }
HandleColorModeTransform(android_color_mode_t mode,android_color_transform_t hint,const double * matrix)169   virtual HWC2::Error HandleColorModeTransform(android_color_mode_t mode,
170                                                android_color_transform_t hint,
171                                                const double *matrix) {
172     return HWC2::Error::Unsupported;
173   }
174   virtual HWC2::Error GetDisplayConfigs(uint32_t *out_num_configs, hwc2_config_t *out_configs);
175   virtual HWC2::Error GetDisplayAttribute(hwc2_config_t config, HWC2::Attribute attribute,
176                                           int32_t *out_value);
177   virtual HWC2::Error GetClientTargetSupport(uint32_t width, uint32_t height, int32_t format,
178                                              int32_t dataspace);
179   virtual HWC2::Error GetColorModes(uint32_t *outNumModes, android_color_mode_t *outModes);
180   virtual HWC2::Error GetChangedCompositionTypes(uint32_t *out_num_elements,
181                                                  hwc2_layer_t *out_layers, int32_t *out_types);
182   virtual HWC2::Error GetDisplayRequests(int32_t *out_display_requests, uint32_t *out_num_elements,
183                                          hwc2_layer_t *out_layers, int32_t *out_layer_requests);
184   virtual HWC2::Error GetDisplayName(uint32_t *out_size, char *out_name);
185   virtual HWC2::Error GetDisplayType(int32_t *out_type);
186   virtual HWC2::Error SetCursorPosition(hwc2_layer_t layer, int x, int y);
187   virtual HWC2::Error SetVsyncEnabled(HWC2::Vsync enabled);
188   virtual HWC2::Error SetPowerMode(HWC2::PowerMode mode);
189   virtual HWC2::Error CreateLayer(hwc2_layer_t *out_layer_id);
190   virtual HWC2::Error DestroyLayer(hwc2_layer_t layer_id);
191   virtual HWC2::Error SetLayerZOrder(hwc2_layer_t layer_id, uint32_t z);
192   virtual HWC2::Error Validate(uint32_t *out_num_types, uint32_t *out_num_requests) = 0;
193   virtual HWC2::Error GetReleaseFences(uint32_t *out_num_elements, hwc2_layer_t *out_layers,
194                                        int32_t *out_fences);
195   virtual HWC2::Error Present(int32_t *out_retire_fence) = 0;
196   virtual HWC2::Error GetHdrCapabilities(uint32_t *out_num_types, int32_t* out_types,
197                                          float* out_max_luminance,
198                                          float* out_max_average_luminance,
199                                          float* out_min_luminance);
200 
201  bool validated_ = false;
202  bool skip_validate_ = false;
203  uint32_t geometry_changes_ = GeometryChanges::kNone;
204 
205  protected:
206   enum DisplayStatus {
207     kDisplayStatusOffline = 0,
208     kDisplayStatusOnline,
209     kDisplayStatusPause,
210     kDisplayStatusResume,
211   };
212 
213   // Maximum number of layers supported by display manager.
214   static const uint32_t kMaxLayerCount = 32;
215 
216   HWCDisplay(CoreInterface *core_intf, HWCCallbacks *callbacks, DisplayType type, hwc2_display_t id,
217              bool needs_blit, qService::QService *qservice, DisplayClass display_class,
218              BufferAllocator *buffer_allocator);
219 
220   // DisplayEventHandler methods
221   virtual DisplayError VSync(const DisplayEventVSync &vsync);
222   virtual DisplayError Refresh();
223   virtual DisplayError CECMessage(char *message);
224   virtual void DumpOutputBuffer(const BufferInfo &buffer_info, void *base, int fence);
225   virtual HWC2::Error PrepareLayerStack(uint32_t *out_num_types, uint32_t *out_num_requests);
226   virtual HWC2::Error CommitLayerStack(void);
227   virtual HWC2::Error PostCommitLayerStack(int32_t *out_retire_fence);
DisablePartialUpdateOneFrame()228   virtual DisplayError DisablePartialUpdateOneFrame() {
229     return kErrorNotSupported;
230   }
231   LayerBufferFormat GetSDMFormat(const int32_t &source, const int flags);
232   const char *GetDisplayString();
233   void MarkLayersForGPUBypass(void);
234   void MarkLayersForClientComposition(void);
235   virtual void ApplyScanAdjustment(hwc_rect_t *display_frame);
236   bool SingleLayerUpdating(void);
237   bool IsSurfaceUpdated(const std::vector<LayerRect> &dirty_regions);
238   bool IsLayerUpdating(HWCLayer *layer);
239   uint32_t SanitizeRefreshRate(uint32_t req_refresh_rate);
GetUnderScanConfig()240   virtual void GetUnderScanConfig() { }
241 
242   enum {
243     INPUT_LAYER_DUMP,
244     OUTPUT_LAYER_DUMP,
245   };
246 
247   CoreInterface *core_intf_ = nullptr;
248   HWCCallbacks *callbacks_  = nullptr;
249   HWCBufferAllocator *buffer_allocator_ = NULL;
250   DisplayType type_;
251   hwc2_display_t id_;
252   bool needs_blit_ = false;
253   DisplayInterface *display_intf_ = NULL;
254   LayerStack layer_stack_;
255   HWCLayer *client_target_ = nullptr;                   // Also known as framebuffer target
256   std::map<hwc2_layer_t, HWCLayer *> layer_map_;        // Look up by Id - TODO
257   std::multiset<HWCLayer *, SortLayersByZ> layer_set_;  // Maintain a set sorted by Z
258   std::map<hwc2_layer_t, HWC2::Composition> layer_changes_;
259   std::map<hwc2_layer_t, HWC2::LayerRequest> layer_requests_;
260   bool flush_on_error_ = false;
261   bool flush_ = false;
262   uint32_t dump_frame_count_ = 0;
263   uint32_t dump_frame_index_ = 0;
264   bool dump_input_layers_ = false;
265   HWC2::PowerMode last_power_mode_;
266   bool swap_interval_zero_ = false;
267   bool display_paused_ = false;
268   uint32_t min_refresh_rate_ = 0;
269   uint32_t max_refresh_rate_ = 0;
270   uint32_t current_refresh_rate_ = 0;
271   bool use_metadata_refresh_rate_ = false;
272   uint32_t metadata_refresh_rate_ = 0;
273   uint32_t force_refresh_rate_ = 0;
274   bool boot_animation_completed_ = false;
275   bool shutdown_pending_ = false;
276   bool use_blit_comp_ = false;
277   bool secure_display_active_ = false;
278   bool skip_prepare_ = false;
279   bool solid_fill_enable_ = false;
280   Layer *solid_fill_layer_ = NULL;
281   LayerRect solid_fill_rect_ = {};
282   uint32_t solid_fill_color_ = 0;
283   LayerRect display_rect_;
284   bool color_tranform_failed_ = false;
285   HWCColorMode *color_mode_ = NULL;
286   HWCToneMapper *tone_mapper_ = nullptr;
287   int disable_hdr_handling_ = 0;  // disables HDR handling.
288 
289  private:
290   void DumpInputBuffers(void);
291   bool CanSkipValidate();
292   qService::QService *qservice_ = NULL;
293   DisplayClass display_class_;
294   bool partial_update_enabled_ = false;
295 };
296 
Perform(uint32_t operation,...)297 inline int HWCDisplay::Perform(uint32_t operation, ...) {
298   return 0;
299 }
300 
301 }  // namespace sdm
302 
303 #endif  // __HWC_DISPLAY_H__
304