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