1 /*
2  * Copyright (c) 2014-2017, 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 <core/core_interface.h>
24 #include <utils/locker.h>
25 
26 #include "hwc_callbacks.h"
27 #include "hwc_layers.h"
28 #include "hwc_display.h"
29 #include "hwc_display_primary.h"
30 #include "hwc_display_external.h"
31 #include "hwc_display_virtual.h"
32 #include "hwc_color_manager.h"
33 #include "hwc_socket_handler.h"
34 
35 namespace sdm {
36 
37 class HWCSession : hwc2_device_t, public qClient::BnQClient {
38  public:
39   struct HWCModuleMethods : public hw_module_methods_t {
HWCModuleMethodsHWCModuleMethods40     HWCModuleMethods() { hw_module_methods_t::open = HWCSession::Open; }
41   };
42 
43   explicit HWCSession(const hw_module_t *module);
44   int Init();
45   int Deinit();
46   HWC2::Error CreateVirtualDisplayObject(uint32_t width, uint32_t height, int32_t *format);
47 
48   template <typename... Args>
CallDisplayFunction(hwc2_device_t * device,hwc2_display_t display,HWC2::Error (HWCDisplay::* member)(Args...),Args...args)49   static int32_t CallDisplayFunction(hwc2_device_t *device, hwc2_display_t display,
50                                      HWC2::Error (HWCDisplay::*member)(Args...), Args... args) {
51     if (!device) {
52       return HWC2_ERROR_BAD_DISPLAY;
53     }
54 
55     HWCSession *hwc_session = static_cast<HWCSession *>(device);
56     auto status = HWC2::Error::BadDisplay;
57     if (display < HWC_NUM_DISPLAY_TYPES && hwc_session->hwc_display_[display]) {
58       auto hwc_display = hwc_session->hwc_display_[display];
59       status = (hwc_display->*member)(std::forward<Args>(args)...);
60     }
61     return INT32(status);
62   }
63 
64   template <typename... Args>
CallLayerFunction(hwc2_device_t * device,hwc2_display_t display,hwc2_layer_t layer,HWC2::Error (HWCLayer::* member)(Args...),Args...args)65   static int32_t CallLayerFunction(hwc2_device_t *device, hwc2_display_t display,
66                                    hwc2_layer_t layer, HWC2::Error (HWCLayer::*member)(Args...),
67                                    Args... args) {
68     if (!device) {
69       return HWC2_ERROR_BAD_DISPLAY;
70     }
71 
72     HWCSession *hwc_session = static_cast<HWCSession *>(device);
73     auto status = HWC2::Error::BadDisplay;
74     if (display < HWC_NUM_DISPLAY_TYPES && hwc_session->hwc_display_[display]) {
75       status = HWC2::Error::BadLayer;
76       auto hwc_layer = hwc_session->hwc_display_[display]->GetHWCLayer(layer);
77       if (hwc_layer != nullptr) {
78         status = (hwc_layer->*member)(std::forward<Args>(args)...);
79         if (hwc_session->hwc_display_[display]->geometry_changes_) {
80           hwc_session->hwc_display_[display]->validated_ = false;
81         }
82       }
83     }
84     return INT32(status);
85   }
86 
87   // HWC2 Functions that require a concrete implementation in hwc session
88   // and hence need to be member functions
89   static int32_t AcceptDisplayChanges(hwc2_device_t *device, hwc2_display_t display);
90   static int32_t CreateLayer(hwc2_device_t *device, hwc2_display_t display,
91                              hwc2_layer_t *out_layer_id);
92   static int32_t CreateVirtualDisplay(hwc2_device_t *device, uint32_t width, uint32_t height,
93                                       int32_t *format, hwc2_display_t *out_display_id);
94   static int32_t DestroyLayer(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer);
95   static int32_t DestroyVirtualDisplay(hwc2_device_t *device, hwc2_display_t display);
96   static void Dump(hwc2_device_t *device, uint32_t *out_size, char *out_buffer);
97   static int32_t PresentDisplay(hwc2_device_t *device, hwc2_display_t display,
98                                 int32_t *out_retire_fence);
99   static int32_t RegisterCallback(hwc2_device_t *device, int32_t descriptor,
100                                   hwc2_callback_data_t callback_data,
101                                   hwc2_function_pointer_t pointer);
102   static int32_t SetOutputBuffer(hwc2_device_t *device, hwc2_display_t display,
103                                  buffer_handle_t buffer, int32_t releaseFence);
104   static int32_t SetLayerZOrder(hwc2_device_t *device, hwc2_display_t display, hwc2_layer_t layer,
105                                 uint32_t z);
106   static int32_t SetPowerMode(hwc2_device_t *device, hwc2_display_t display, int32_t int_mode);
107   static int32_t ValidateDisplay(hwc2_device_t *device, hwc2_display_t display,
108                                  uint32_t *out_num_types, uint32_t *out_num_requests);
109   static int32_t SetColorMode(hwc2_device_t *device, hwc2_display_t display,
110                               int32_t /*android_color_mode_t*/ int_mode);
111   static int32_t SetColorTransform(hwc2_device_t *device, hwc2_display_t display,
112                                    const float *matrix, int32_t /*android_color_transform_t*/ hint);
113 
114  private:
115   static const int kExternalConnectionTimeoutMs = 500;
116   static const int kPartialUpdateControlTimeoutMs = 100;
117 
118   // hwc methods
119   static int Open(const hw_module_t *module, const char *name, hw_device_t **device);
120   static int Close(hw_device_t *device);
121   static void GetCapabilities(struct hwc2_device *device, uint32_t *outCount,
122                               int32_t *outCapabilities);
123   static hwc2_function_pointer_t GetFunction(struct hwc2_device *device, int32_t descriptor);
124 
125   // Uevent thread
126   static void *HWCUeventThread(void *context);
127   void *HWCUeventThreadHandler();
128   int GetEventValue(const char *uevent_data, int length, const char *event_info);
129   int HotPlugHandler(bool connected);
130   void ResetPanel();
131   int32_t ConnectDisplay(int disp);
132   int DisconnectDisplay(int disp);
133   int GetVsyncPeriod(int disp);
134 
135   // QClient methods
136   virtual android::status_t notifyCallback(uint32_t command, const android::Parcel *input_parcel,
137                                            android::Parcel *output_parcel);
138   void DynamicDebug(const android::Parcel *input_parcel);
139   void SetFrameDumpConfig(const android::Parcel *input_parcel);
140   android::status_t SetMaxMixerStages(const android::Parcel *input_parcel);
141   android::status_t SetDisplayMode(const android::Parcel *input_parcel);
142   android::status_t SetSecondaryDisplayStatus(const android::Parcel *input_parcel,
143                                               android::Parcel *output_parcel);
144   android::status_t ToggleScreenUpdates(const android::Parcel *input_parcel,
145                                         android::Parcel *output_parcel);
146   android::status_t ConfigureRefreshRate(const android::Parcel *input_parcel);
147   android::status_t QdcmCMDHandler(const android::Parcel *input_parcel,
148                                    android::Parcel *output_parcel);
149   android::status_t ControlPartialUpdate(const android::Parcel *input_parcel, android::Parcel *out);
150   android::status_t OnMinHdcpEncryptionLevelChange(const android::Parcel *input_parcel,
151                                                    android::Parcel *output_parcel);
152   android::status_t SetPanelBrightness(const android::Parcel *input_parcel,
153                                        android::Parcel *output_parcel);
154   android::status_t GetPanelBrightness(const android::Parcel *input_parcel,
155                                        android::Parcel *output_parcel);
156   // These functions return the actual display config info as opposed to FB
157   android::status_t HandleSetActiveDisplayConfig(const android::Parcel *input_parcel,
158                                                  android::Parcel *output_parcel);
159   android::status_t HandleGetActiveDisplayConfig(const android::Parcel *input_parcel,
160                                                  android::Parcel *output_parcel);
161   android::status_t HandleGetDisplayConfigCount(const android::Parcel *input_parcel,
162                                                 android::Parcel *output_parcel);
163   android::status_t HandleGetDisplayAttributesForConfig(const android::Parcel *input_parcel,
164                                                         android::Parcel *output_parcel);
165   android::status_t GetVisibleDisplayRect(const android::Parcel *input_parcel,
166                                           android::Parcel *output_parcel);
167 
168   android::status_t SetDynamicBWForCamera(const android::Parcel *input_parcel,
169                                           android::Parcel *output_parcel);
170   android::status_t GetBWTransactionStatus(const android::Parcel *input_parcel,
171                                            android::Parcel *output_parcel);
172   android::status_t SetMixerResolution(const android::Parcel *input_parcel);
173 
174   android::status_t SetColorModeOverride(const android::Parcel *input_parcel);
175 
176   android::status_t SetColorModeById(const android::Parcel *input_parcel);
177 
178   static Locker locker_;
179   CoreInterface *core_intf_ = NULL;
180   HWCDisplay *hwc_display_[HWC_NUM_DISPLAY_TYPES] = {NULL};
181   HWCCallbacks callbacks_;
182   pthread_t uevent_thread_;
183   bool uevent_thread_exit_ = false;
184   const char *uevent_thread_name_ = "HWC_UeventThread";
185   HWCBufferAllocator *buffer_allocator_;
186   HWCBufferSyncHandler buffer_sync_handler_;
187   HWCColorManager *color_mgr_ = NULL;
188   bool reset_panel_ = false;
189   bool secure_display_active_ = false;
190   bool external_pending_connect_ = false;
191   bool new_bw_mode_ = false;
192   bool need_invalidate_ = false;
193   int bw_mode_release_fd_ = -1;
194   qService::QService *qservice_ = NULL;
195   HWCSocketHandler socket_handler_;
196 };
197 
198 }  // namespace sdm
199 
200 #endif  // __HWC_SESSION_H__
201