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