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