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