• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_HWC_DEVICE_H
18 #define ANDROID_HWC_DEVICE_H
19 
20 #include <atomic>
21 #include <map>
22 #include <memory>
23 #include <mutex>
24 #include <set>
25 #include <unordered_map>
26 #include <unordered_set>
27 #include <vector>
28 
29 #include "Common.h"
30 #include "Composer.h"
31 #include "Display.h"
32 #include "DrmPresenter.h"
33 #include "Layer.h"
34 
35 namespace android {
36 
37 class Composer;
38 class Display;
39 
40 class Device : public hwc2_device_t {
41  public:
fromDevice(hw_device_t * device)42   static inline Device* fromDevice(hw_device_t* device) {
43     return reinterpret_cast<Device*>(device);
44   }
45 
fromDevice(hwc2_device_t * device)46   static inline Device* fromDevice(hwc2_device_t* device) {
47     return static_cast<Device*>(device);
48   }
49 
50   Device();
51   ~Device();
52 
53   HWC2::Error init();
54 
55   HWC2::Error createDisplays();
56 
57   HWC2::Error createDisplay(hwc2_display_t displayId,
58                             hwc2_config_t activeConfigId,
59                             const std::vector<DisplayConfig>& configs);
60 
61   Display* getDisplay(hwc2_display_t displayId);
62 
63  private:
64   HWC2::Error destroyDisplays();
65 
66   bool handleHotplug(bool connected, uint32_t id, uint32_t width,
67                      uint32_t height, uint32_t dpiX, uint32_t dpiY,
68                      uint32_t refreshRate);
69 
70   void getCapabilities(uint32_t* outCount, int32_t* outCapabilities);
71   static void getCapabilitiesHook(hwc2_device_t* device, uint32_t* outCount,
72                                   int32_t* outCapabilities);
73 
74   hwc2_function_pointer_t getFunction(int32_t descriptor);
75   static hwc2_function_pointer_t getFunctionHook(hwc2_device_t* device,
76                                                  int32_t descriptor);
77 
78   // Wrapper to call a specific function on a specific device.
79   template <typename T, typename HookType, HookType func, typename... Args>
DeviceHook(hwc2_device_t * dev,Args...args)80   static T DeviceHook(hwc2_device_t* dev, Args... args) {
81     Device* device = Device::fromDevice(dev);
82     return static_cast<T>(((*device).*func)(std::forward<Args>(args)...));
83   }
84 
85   // Wrapper to call a specific function on a specific display.
86   template <typename HookType, HookType func, typename... Args>
displayHook(hwc2_device_t * dev,hwc2_display_t displayId,Args...args)87   static int32_t displayHook(hwc2_device_t* dev, hwc2_display_t displayId,
88                              Args... args) {
89     Device* device = Device::fromDevice(dev);
90 
91     Display* display = device->getDisplay(displayId);
92     if (display == nullptr) {
93       return static_cast<int32_t>(HWC2::Error::BadDisplay);
94     }
95 
96     return static_cast<int32_t>((display->*func)(std::forward<Args>(args)...));
97   }
98 
99   // Wrapper to call a specific function on a specific layer.
100   template <typename HookType, HookType func, typename... Args>
layerHook(hwc2_device_t * dev,hwc2_display_t displayId,hwc2_layer_t layerId,Args...args)101   static int32_t layerHook(hwc2_device_t* dev, hwc2_display_t displayId,
102                            hwc2_layer_t layerId, Args... args) {
103     Device* device = Device::fromDevice(dev);
104 
105     Display* display = device->getDisplay(displayId);
106     if (display == nullptr) {
107       return static_cast<int32_t>(HWC2::Error::BadDisplay);
108     }
109 
110     Layer* layer = display->getLayer(layerId);
111     if (layer == nullptr) {
112       return static_cast<int32_t>(HWC2::Error::BadLayer);
113     }
114 
115     return static_cast<int32_t>((layer->*func)(std::forward<Args>(args)...));
116   }
117 
118   // Device functions
119   HWC2::Error createVirtualDisplay(uint32_t width, uint32_t height,
120                                    int32_t* format, hwc2_display_t* outDisplay);
121 
122   HWC2::Error destroyVirtualDisplay(hwc2_display_t display);
123 
124   void dump(uint32_t* outSize, char* outBuffer);
125 
126   uint32_t getMaxVirtualDisplayCount();
127 
128   HWC2::Error registerCallback(int32_t descriptor,
129                                hwc2_callback_data_t callbackData,
130                                hwc2_function_pointer_t pointer);
131 
132   // These are potentially accessed from multiple threads, and are protected
133   // by this mutex.
134   std::mutex mStateMutex;
135 
136   std::unique_ptr<Composer> mComposer;
137 
138   std::unordered_set<HWC2::Capability> mCapabilities;
139 
140   // For sharing Vsync callback with each displays Vsync thread.
141   friend class Display;
142 
143   struct CallbackInfo {
144     hwc2_callback_data_t data;
145     hwc2_function_pointer_t pointer;
146   };
147   std::unordered_map<HWC2::Callback, CallbackInfo> mCallbacks;
148   std::map<hwc2_display_t, std::unique_ptr<Display>> mDisplays;
149 
150   std::unique_ptr<DrmPresenter> mDrmPresenter;
151 };
152 
153 }  // namespace android
154 #endif
155