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