1 /* 2 * Copyright (c) 2014-2020, 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_SESSION_H__ 21 #define __HWC_SESSION_H__ 22 23 #include <vendor/display/config/1.11/IDisplayConfig.h> 24 25 #include <android/hardware/graphics/composer/2.4/IComposerClient.h> 26 #include <core/core_interface.h> 27 #include <utils/locker.h> 28 #include <qd_utils.h> 29 #include <display_config.h> 30 #include <vector> 31 #include <utility> 32 #include <map> 33 34 #include "hwc_callbacks.h" 35 #include "hwc_layers.h" 36 #include "hwc_display.h" 37 #include "hwc_display_builtin.h" 38 #include "hwc_display_pluggable.h" 39 #include "hwc_display_dummy.h" 40 #include "hwc_display_virtual.h" 41 #include "hwc_display_pluggable_test.h" 42 #include "hwc_color_manager.h" 43 #include "hwc_socket_handler.h" 44 #include "hwc_display_event_handler.h" 45 #include "hwc_buffer_sync_handler.h" 46 47 namespace sdm { 48 49 using vendor::display::config::V1_11::IDisplayConfig; 50 using vendor::display::config::V1_10::IDisplayCWBCallback; 51 52 using ::android::hardware::Return; 53 using ::android::hardware::hidl_string; 54 using ::android::hardware::graphics::composer::V2_4::IComposerClient; 55 using android::hardware::hidl_handle; 56 using ::android::hardware::hidl_vec; 57 58 int32_t GetDataspaceFromColorMode(ColorMode mode); 59 60 // Create a singleton uevent listener thread valid for life of hardware composer process. 61 // This thread blocks on uevents poll inside uevent library implementation. This poll exits 62 // only when there is a valid uevent, it can not be interrupted otherwise. Tieing life cycle 63 // of this thread with HWC session cause HWC deinitialization to wait infinitely for the 64 // thread to exit. 65 class HWCUEventListener { 66 public: ~HWCUEventListener()67 virtual ~HWCUEventListener() {} 68 virtual void UEventHandler(const char *uevent_data, int length) = 0; 69 }; 70 71 class HWCUEvent { 72 public: 73 HWCUEvent(); 74 static void UEventThread(HWCUEvent *hwc_event); 75 void Register(HWCUEventListener *uevent_listener); InitDone()76 inline bool InitDone() { return init_done_; } 77 78 private: 79 std::mutex mutex_; 80 std::condition_variable caller_cv_; 81 HWCUEventListener *uevent_listener_ = nullptr; 82 bool init_done_ = false; 83 }; 84 85 constexpr int32_t kDataspaceSaturationMatrixCount = 16; 86 constexpr int32_t kDataspaceSaturationPropertyElements = 9; 87 constexpr int32_t kPropertyMax = 256; 88 89 class HWCSession : hwc2_device_t, HWCUEventListener, IDisplayConfig, public qClient::BnQClient, 90 public HWCDisplayEventHandler { 91 public: 92 struct HWCModuleMethods : public hw_module_methods_t { HWCModuleMethodsHWCModuleMethods93 HWCModuleMethods() { hw_module_methods_t::open = HWCSession::Open; } 94 }; 95 96 enum HotPlugEvent { 97 kHotPlugNone, 98 kHotPlugEvent, 99 }; 100 101 explicit HWCSession(const hw_module_t *module); 102 int Init(); 103 int Deinit(); 104 HWC2::Error CreateVirtualDisplayObj(uint32_t width, uint32_t height, int32_t *format, 105 hwc2_display_t *out_display_id); 106 107 template <typename... Args> CallDisplayFunction(hwc2_device_t * device,hwc2_display_t display,HWC2::Error (HWCDisplay::* member)(Args...),Args...args)108 static int32_t CallDisplayFunction(hwc2_device_t *device, hwc2_display_t display, 109 HWC2::Error (HWCDisplay::*member)(Args...), Args... args) { 110 if (!device) { 111 return HWC2_ERROR_BAD_PARAMETER; 112 } 113 114 if (display >= HWCCallbacks::kNumDisplays) { 115 return HWC2_ERROR_BAD_DISPLAY; 116 } 117 118 HWCSession *hwc_session = static_cast<HWCSession *>(device); 119 { 120 // Power state transition start. 121 SCOPE_LOCK(power_state_[display]); 122 if (hwc_session->power_state_transition_[display]) { 123 display = hwc_session->map_hwc_display_.find(display)->second; 124 } 125 } 126 127 SCOPE_LOCK(locker_[display]); 128 auto status = HWC2::Error::BadDisplay; 129 if (hwc_session->hwc_display_[display]) { 130 auto hwc_display = hwc_session->hwc_display_[display]; 131 status = (hwc_display->*member)(std::forward<Args>(args)...); 132 } 133 return INT32(status); 134 } 135 136 template <typename... Args> CallLayerFunction(hwc2_device_t * device,hwc2_display_t display,hwc2_layer_t layer,HWC2::Error (HWCLayer::* member)(Args...),Args...args)137 static int32_t CallLayerFunction(hwc2_device_t *device, hwc2_display_t display, 138 hwc2_layer_t layer, HWC2::Error (HWCLayer::*member)(Args...), 139 Args... args) { 140 if (!device) { 141 return HWC2_ERROR_BAD_PARAMETER; 142 } 143 144 if (display >= HWCCallbacks::kNumDisplays) { 145 return HWC2_ERROR_BAD_DISPLAY; 146 } 147 148 HWCSession *hwc_session = static_cast<HWCSession *>(device); 149 { 150 // Power state transition start. 151 SCOPE_LOCK(power_state_[display]); 152 if (hwc_session->power_state_transition_[display]) { 153 display = hwc_session->map_hwc_display_.find(display)->second; 154 } 155 } 156 157 SCOPE_LOCK(locker_[display]); 158 auto status = HWC2::Error::BadDisplay; 159 if (hwc_session->hwc_display_[display]) { 160 status = HWC2::Error::BadLayer; 161 auto hwc_layer = hwc_session->hwc_display_[display]->GetHWCLayer(layer); 162 if (hwc_layer != nullptr) { 163 status = (hwc_layer->*member)(std::forward<Args>(args)...); 164 if (hwc_session->hwc_display_[display]->GetGeometryChanges()) { 165 hwc_session->hwc_display_[display]->ResetValidation(); 166 } 167 } 168 } 169 return INT32(status); 170 } 171 172 // HWC2 Functions that require a concrete implementation in hwc session 173 // and hence need to be member functions 174 static int32_t AcceptDisplayChanges(hwc2_device_t *device, hwc2_display_t display); 175 static int32_t CreateLayer(hwc2_device_t *device, hwc2_display_t display, 176 hwc2_layer_t *out_layer_id); 177 static int32_t CreateVirtualDisplay(hwc2_device_t *device, uint32_t width, uint32_t height, 178 int32_t *format, hwc2_display_t *out_display_id); 179 static int32_t DestroyLayer(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer); 180 static int32_t DestroyVirtualDisplay(hwc2_device_t *device, hwc2_display_t display); 181 static void Dump(hwc2_device_t *device, uint32_t *out_size, char *out_buffer); 182 static int32_t PresentDisplay(hwc2_device_t *device, hwc2_display_t display, 183 int32_t *out_retire_fence); 184 static int32_t RegisterCallback(hwc2_device_t *device, int32_t descriptor, 185 hwc2_callback_data_t callback_data, 186 hwc2_function_pointer_t pointer); 187 static int32_t SetOutputBuffer(hwc2_device_t *device, hwc2_display_t display, 188 buffer_handle_t buffer, int32_t releaseFence); 189 static int32_t SetPowerMode(hwc2_device_t *device, hwc2_display_t display, int32_t int_mode); 190 static int32_t ValidateDisplay(hwc2_device_t *device, hwc2_display_t display, 191 uint32_t *out_num_types, uint32_t *out_num_requests); 192 static int32_t SetColorMode(hwc2_device_t *device, hwc2_display_t display, 193 int32_t /*ColorMode*/ int_mode); 194 static int32_t SetColorModeWithRenderIntent(hwc2_device_t *device, hwc2_display_t display, 195 int32_t /*ColorMode*/ int_mode, 196 int32_t /*RenderIntent*/ int_render_intent); 197 static int32_t SetColorTransform(hwc2_device_t *device, hwc2_display_t display, 198 const float *matrix, int32_t /*android_color_transform_t*/ hint); 199 static int32_t GetReadbackBufferAttributes(hwc2_device_t *device, hwc2_display_t display, 200 int32_t *format, int32_t *dataspace); 201 static int32_t SetReadbackBuffer(hwc2_device_t *device, hwc2_display_t display, 202 const native_handle_t *buffer, int32_t acquire_fence); 203 static int32_t GetReadbackBufferFence(hwc2_device_t *device, hwc2_display_t display, 204 int32_t *release_fence); 205 static uint32_t GetMaxVirtualDisplayCount(hwc2_device_t *device); 206 static int32_t GetDisplayCapabilities(hwc2_device_t* device, hwc2_display_t display, 207 uint32_t* outNumCapabilities, uint32_t* outCapabilities); 208 static int32_t SetDisplayBrightness(hwc2_device_t *device, hwc2_display_t display, 209 float brightness); 210 static int32_t GetDisplayIdentificationData(hwc2_device_t *device, hwc2_display_t display, 211 uint8_t *outPort, uint32_t *outDataSize, 212 uint8_t *outData); 213 214 // HWCDisplayEventHandler 215 virtual void DisplayPowerReset(); 216 217 static int32_t SetVsyncEnabled(hwc2_device_t *device, hwc2_display_t display, 218 int32_t int_enabled); 219 static int32_t GetDozeSupport(hwc2_device_t *device, hwc2_display_t display, 220 int32_t *out_support); 221 222 static Locker locker_[HWCCallbacks::kNumDisplays]; 223 static Locker power_state_[HWCCallbacks::kNumDisplays]; 224 static Locker display_config_locker_; 225 226 private: 227 struct DisplayMapInfo { 228 hwc2_display_t client_id = HWCCallbacks::kNumDisplays; // mapped sf id for this display 229 int32_t sdm_id = -1; // sdm id for this display 230 sdm:: DisplayType disp_type = kDisplayTypeMax; // sdm display type 231 bool test_pattern = false; // display will show test pattern ResetDisplayMapInfo232 void Reset() { 233 // Do not clear client id 234 sdm_id = -1; 235 disp_type = kDisplayTypeMax; 236 test_pattern = false; 237 } 238 }; 239 240 static const int kExternalConnectionTimeoutMs = 500; 241 static const int kCommitDoneTimeoutMs = 100; 242 uint32_t throttling_refresh_rate_ = 60; 243 void UpdateThrottlingRate(); 244 void SetNewThrottlingRate(uint32_t new_rate); 245 // hwc methods 246 static int Open(const hw_module_t *module, const char *name, hw_device_t **device); 247 static int Close(hw_device_t *device); 248 static void GetCapabilities(struct hwc2_device *device, uint32_t *outCount, 249 int32_t *outCapabilities); 250 static int32_t GetDisplayBrightnessSupport(hwc2_device_t *device, hwc2_display_t display, 251 bool *out_support); 252 static hwc2_function_pointer_t GetFunction(struct hwc2_device *device, int32_t descriptor); 253 254 // Uevent handler 255 virtual void UEventHandler(const char *uevent_data, int length); 256 void ResetPanel(); 257 void InitSupportedDisplaySlots(); 258 int GetDisplayIndex(int dpy); 259 int CreatePrimaryDisplay(); 260 void CreateDummyDisplay(hwc2_display_t client_id); 261 int HandleBuiltInDisplays(); 262 int HandlePluggableDisplays(bool delay_hotplug); 263 int HandleConnectedDisplays(HWDisplaysInfo *hw_displays_info, bool delay_hotplug); 264 int HandleDisconnectedDisplays(HWDisplaysInfo *hw_displays_info); 265 void DestroyDisplay(DisplayMapInfo *map_info); 266 void DestroyPluggableDisplay(DisplayMapInfo *map_info); 267 void DestroyNonPluggableDisplay(DisplayMapInfo *map_info); 268 int GetVsyncPeriod(int disp); 269 int32_t GetConfigCount(int disp_id, uint32_t *count); 270 int32_t GetActiveConfigIndex(int disp_id, uint32_t *config); 271 int32_t SetActiveConfigIndex(int disp_id, uint32_t config); 272 int32_t ControlPartialUpdate(int dpy, bool enable); 273 int32_t DisplayBWTransactionPending(bool *status); 274 int32_t SetSecondaryDisplayStatus(int disp_id, HWCDisplay::DisplayStatus status); 275 int32_t GetPanelBrightness(int32_t &level) const; 276 int32_t GetPanelMaxBrightness(int32_t &max_brightness_level) const; 277 int32_t MinHdcpEncryptionLevelChanged(int disp_id, uint32_t min_enc_level); 278 int32_t IsWbUbwcSupported(int *value); 279 int32_t SetDynamicDSIClock(int64_t disp_id, uint32_t bitrate); 280 bool HasHDRSupport(HWCDisplay *hwc_display); 281 282 // service methods 283 void StartServices(); 284 285 // Methods from ::android::hardware::display::config::V1_0::IDisplayConfig follow. 286 Return<void> isDisplayConnected(IDisplayConfig::DisplayType dpy, 287 isDisplayConnected_cb _hidl_cb) override; 288 Return<int32_t> setSecondayDisplayStatus(IDisplayConfig::DisplayType dpy, 289 IDisplayConfig::DisplayExternalStatus status) override; 290 Return<int32_t> configureDynRefeshRate(IDisplayConfig::DisplayDynRefreshRateOp op, 291 uint32_t refreshRate) override; 292 Return<void> getConfigCount(IDisplayConfig::DisplayType dpy, 293 getConfigCount_cb _hidl_cb) override; 294 Return<void> getActiveConfig(IDisplayConfig::DisplayType dpy, 295 getActiveConfig_cb _hidl_cb) override; 296 Return<int32_t> setActiveConfig(IDisplayConfig::DisplayType dpy, uint32_t config) override; 297 Return<void> getDisplayAttributes(uint32_t configIndex, IDisplayConfig::DisplayType dpy, 298 getDisplayAttributes_cb _hidl_cb) override; 299 Return<int32_t> setPanelBrightness(uint32_t level) override; 300 Return<void> getPanelBrightness(getPanelBrightness_cb _hidl_cb) override; 301 Return<int32_t> minHdcpEncryptionLevelChanged(IDisplayConfig::DisplayType dpy, 302 uint32_t min_enc_level) override; 303 Return<int32_t> refreshScreen() override; 304 Return<int32_t> controlPartialUpdate(IDisplayConfig::DisplayType dpy, bool enable) override; 305 Return<int32_t> toggleScreenUpdate(bool on) override; 306 Return<int32_t> setIdleTimeout(uint32_t value) override; 307 Return<void> getHDRCapabilities(IDisplayConfig::DisplayType dpy, 308 getHDRCapabilities_cb _hidl_cb) override; 309 Return<int32_t> setCameraLaunchStatus(uint32_t on) override; 310 Return<void> displayBWTransactionPending(displayBWTransactionPending_cb _hidl_cb) override; 311 Return<int32_t> setDisplayAnimating(uint64_t display_id, bool animating) override; 312 Return<int32_t> setDisplayIndex(IDisplayConfig::DisplayTypeExt disp_type, 313 uint32_t base, uint32_t count) override; 314 Return<int32_t> controlIdlePowerCollapse(bool enable, bool synchronous) override; 315 Return<void> getWriteBackCapabilities(getWriteBackCapabilities_cb _hidl_cb) override; 316 Return<int32_t> SetDisplayDppsAdROI(uint32_t dispaly_id, uint32_t h_start, uint32_t h_end, 317 uint32_t v_start, uint32_t v_end, uint32_t factor_in, 318 uint32_t factor_out) override; 319 Return<int32_t> updateVSyncSourceOnPowerModeOff() override; 320 Return<int32_t> updateVSyncSourceOnPowerModeDoze() override; 321 Return<int32_t> setPowerMode(uint32_t disp_id, PowerMode power_mode) override; 322 Return<bool> isPowerModeOverrideSupported(uint32_t disp_id) override; 323 Return<bool> isHDRSupported(uint32_t disp_id) override; 324 Return<bool> isWCGSupported(uint32_t disp_id) override; 325 Return<int32_t> setLayerAsMask(uint32_t disp_id, uint64_t layer_id) override; 326 Return<void> getDebugProperty(const hidl_string &prop_name, 327 getDebugProperty_cb _hidl_cb) override; 328 Return<void> getActiveBuiltinDisplayAttributes(getDisplayAttributes_cb _hidl_cb) override; 329 Return<int32_t> setPanelLuminanceAttributes(uint32_t disp_id, float min_lum, 330 float max_lum) override; 331 Return<bool> isBuiltInDisplay(uint32_t disp_id) override; 332 Return<void> getSupportedDSIBitClks(uint32_t disp_id, 333 getSupportedDSIBitClks_cb _hidl_cb) override; 334 Return<uint64_t> getDSIClk(uint32_t disp_id) override; 335 Return<int32_t> setDSIClk(uint32_t disp_id, uint64_t bit_clk) override; 336 Return<int32_t> setCWBOutputBuffer(const ::android::sp<IDisplayCWBCallback> &callback, 337 uint32_t disp_id, const Rect &rect, bool post_processed, 338 const hidl_handle& buffer) override; 339 Return<int32_t> setQsyncMode(uint32_t disp_id, IDisplayConfig::QsyncMode mode) override; 340 341 // QClient methods 342 virtual android::status_t notifyCallback(uint32_t command, const android::Parcel *input_parcel, 343 android::Parcel *output_parcel); 344 void DynamicDebug(const android::Parcel *input_parcel); 345 android::status_t SetFrameDumpConfig(const android::Parcel *input_parcel); 346 android::status_t SetMaxMixerStages(const android::Parcel *input_parcel); 347 android::status_t SetDisplayMode(const android::Parcel *input_parcel); 348 android::status_t ConfigureRefreshRate(const android::Parcel *input_parcel); 349 android::status_t QdcmCMDHandler(const android::Parcel *input_parcel, 350 android::Parcel *output_parcel); 351 android::status_t QdcmCMDDispatch(uint32_t display_id, 352 const PPDisplayAPIPayload &req_payload, 353 PPDisplayAPIPayload *resp_payload, 354 PPPendingParams *pending_action); 355 android::status_t GetDisplayAttributesForConfig(const android::Parcel *input_parcel, 356 android::Parcel *output_parcel); 357 android::status_t GetVisibleDisplayRect(const android::Parcel *input_parcel, 358 android::Parcel *output_parcel); 359 android::status_t SetMixerResolution(const android::Parcel *input_parcel); 360 android::status_t SetColorModeOverride(const android::Parcel *input_parcel); 361 android::status_t SetColorModeWithRenderIntentOverride(const android::Parcel *input_parcel); 362 363 android::status_t SetColorModeById(const android::Parcel *input_parcel); 364 android::status_t SetColorModeFromClient(const android::Parcel *input_parcel); 365 android::status_t getComposerStatus(); 366 android::status_t SetQSyncMode(const android::Parcel *input_parcel); 367 android::status_t SetIdlePC(const android::Parcel *input_parcel); 368 android::status_t RefreshScreen(const android::Parcel *input_parcel); 369 android::status_t SetAd4RoiConfig(const android::Parcel *input_parcel); 370 android::status_t SetDsiClk(const android::Parcel *input_parcel); 371 android::status_t GetDsiClk(const android::Parcel *input_parcel, android::Parcel *output_parcel); 372 android::status_t GetSupportedDsiClk(const android::Parcel *input_parcel, 373 android::Parcel *output_parcel); 374 android::status_t SetPanelLuminanceAttributes(const android::Parcel *input_parcel); 375 376 android::status_t SetDisplayDeviceStatus(const android::Parcel* input_parcel); 377 378 android::status_t setColorSamplingEnabled(const android::Parcel *input_parcel); 379 380 void Refresh(hwc2_display_t display); 381 void HotPlug(hwc2_display_t display, HWC2::Connection state); 382 383 // Internal methods 384 HWC2::Error ValidateDisplayInternal(hwc2_display_t display, uint32_t *out_num_types, 385 uint32_t *out_num_requests); 386 HWC2::Error PresentDisplayInternal(hwc2_display_t display, int32_t *out_retire_fence); 387 void HandleSecureSession(); 388 void HandlePowerOnPending(hwc2_display_t display, int retire_fence); 389 void HandleHotplugPending(hwc2_display_t disp_id, int retire_fence); 390 bool IsPluggableDisplayConnected(); 391 hwc2_display_t GetActiveBuiltinDisplay(); 392 void HandlePendingRefresh(); 393 void NotifyClientStatus(bool connected); 394 395 CoreInterface *core_intf_ = nullptr; 396 HWCDisplay *hwc_display_[HWCCallbacks::kNumDisplays] = {nullptr}; 397 HWCCallbacks callbacks_; 398 HWCBufferAllocator buffer_allocator_; 399 HWCBufferSyncHandler buffer_sync_handler_; 400 HWCColorManager *color_mgr_ = nullptr; 401 DisplayMapInfo map_info_primary_; // Primary display (either builtin or pluggable) 402 std::vector<DisplayMapInfo> map_info_builtin_; // Builtin displays excluding primary 403 std::vector<DisplayMapInfo> map_info_pluggable_; // Pluggable displays excluding primary 404 std::vector<DisplayMapInfo> map_info_virtual_; // Virtual displays 405 std::vector<bool> is_hdr_display_; // info on HDR supported 406 std::map <hwc2_display_t, hwc2_display_t> map_hwc_display_; // Real and dummy display pairs. 407 bool reset_panel_ = false; 408 bool client_connected_ = false; 409 bool new_bw_mode_ = false; 410 bool need_invalidate_ = false; 411 int bw_mode_release_fd_ = -1; 412 qService::QService *qservice_ = nullptr; 413 HWCSocketHandler socket_handler_; 414 bool hdmi_is_primary_ = false; 415 bool is_composer_up_ = false; 416 Locker callbacks_lock_; 417 std::mutex mutex_lum_; 418 int hpd_bpp_ = 0; 419 int hpd_pattern_ = 0; 420 static bool power_on_pending_[HWCCallbacks::kNumDisplays]; 421 static int null_display_mode_; 422 HotPlugEvent hotplug_pending_event_ = kHotPlugNone; 423 Locker pluggable_handler_lock_; 424 bool destroy_virtual_disp_pending_ = false; 425 uint32_t idle_pc_ref_cnt_ = 0; 426 int32_t disable_hotplug_bwcheck_ = 0; 427 int32_t disable_mask_layer_hint_ = 0; 428 float set_max_lum_ = -1.0; 429 float set_min_lum_ = -1.0; 430 std::bitset<HWCCallbacks::kNumDisplays> pending_refresh_; 431 bool async_powermode_ = false; 432 bool power_state_transition_[HWCCallbacks::kNumDisplays] = {}; // +1 to account for primary. 433 std::bitset<HWCCallbacks::kNumDisplays> display_ready_; 434 }; 435 436 } // namespace sdm 437 438 #endif // __HWC_SESSION_H__ 439