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 "Layer.h" 33 34 namespace android { 35 36 class Composer; 37 class Display; 38 39 class Device : public hwc2_device_t { 40 public: fromDevice(hw_device_t * device)41 static inline Device* fromDevice(hw_device_t* device) { 42 return reinterpret_cast<Device*>(device); 43 } 44 fromDevice(hwc2_device_t * device)45 static inline Device* fromDevice(hwc2_device_t* device) { 46 return static_cast<Device*>(device); 47 } 48 49 Device(); 50 ~Device(); 51 52 HWC2::Error init(); 53 54 HWC2::Error createDisplays(); 55 56 HWC2::Error createDisplay(uint32_t displayId, uint32_t width, uint32_t height, 57 uint32_t dpiX, uint32_t dpiY, uint32_t refreshRate); 58 59 Display* getDisplay(hwc2_display_t displayId); 60 61 private: 62 HWC2::Error destroyDisplays(); 63 64 bool handleHotplug(bool connected, uint32_t id, uint32_t width, 65 uint32_t height, uint32_t dpiX, uint32_t dpiY, 66 uint32_t refreshRate); 67 68 void getCapabilities(uint32_t* outCount, int32_t* outCapabilities); 69 static void getCapabilitiesHook(hwc2_device_t* device, uint32_t* outCount, 70 int32_t* outCapabilities); 71 72 hwc2_function_pointer_t getFunction(int32_t descriptor); 73 static hwc2_function_pointer_t getFunctionHook(hwc2_device_t* device, 74 int32_t descriptor); 75 76 // Wrapper to call a specific function on a specific device. 77 template <typename T, typename HookType, HookType func, typename... Args> DeviceHook(hwc2_device_t * dev,Args...args)78 static T DeviceHook(hwc2_device_t* dev, Args... args) { 79 Device* device = Device::fromDevice(dev); 80 return static_cast<T>(((*device).*func)(std::forward<Args>(args)...)); 81 } 82 83 // Wrapper to call a specific function on a specific display. 84 template <typename HookType, HookType func, typename... Args> displayHook(hwc2_device_t * dev,hwc2_display_t displayId,Args...args)85 static int32_t displayHook(hwc2_device_t* dev, hwc2_display_t displayId, 86 Args... args) { 87 Device* device = Device::fromDevice(dev); 88 89 Display* display = device->getDisplay(displayId); 90 if (display == nullptr) { 91 return static_cast<int32_t>(HWC2::Error::BadDisplay); 92 } 93 94 return static_cast<int32_t>((display->*func)(std::forward<Args>(args)...)); 95 } 96 97 // Wrapper to call a specific function on a specific layer. 98 template <typename HookType, HookType func, typename... Args> layerHook(hwc2_device_t * dev,hwc2_display_t displayId,hwc2_layer_t layerId,Args...args)99 static int32_t layerHook(hwc2_device_t* dev, hwc2_display_t displayId, 100 hwc2_layer_t layerId, Args... args) { 101 Device* device = Device::fromDevice(dev); 102 103 Display* display = device->getDisplay(displayId); 104 if (display == nullptr) { 105 return static_cast<int32_t>(HWC2::Error::BadDisplay); 106 } 107 108 Layer* layer = display->getLayer(layerId); 109 if (layer == nullptr) { 110 return static_cast<int32_t>(HWC2::Error::BadLayer); 111 } 112 113 return static_cast<int32_t>((layer->*func)(std::forward<Args>(args)...)); 114 } 115 116 // Device functions 117 HWC2::Error createVirtualDisplay(uint32_t width, uint32_t height, 118 int32_t* format, hwc2_display_t* outDisplay); 119 120 HWC2::Error destroyVirtualDisplay(hwc2_display_t display); 121 122 void dump(uint32_t* outSize, char* outBuffer); 123 124 uint32_t getMaxVirtualDisplayCount(); 125 126 HWC2::Error registerCallback(int32_t descriptor, 127 hwc2_callback_data_t callbackData, 128 hwc2_function_pointer_t pointer); 129 130 // These are potentially accessed from multiple threads, and are protected 131 // by this mutex. 132 std::mutex mStateMutex; 133 134 std::unique_ptr<Composer> mComposer; 135 136 std::unordered_set<HWC2::Capability> mCapabilities; 137 138 // For sharing Vsync callback with each displays Vsync thread. 139 friend class Display; 140 141 struct CallbackInfo { 142 hwc2_callback_data_t data; 143 hwc2_function_pointer_t pointer; 144 }; 145 std::unordered_map<HWC2::Callback, CallbackInfo> mCallbacks; 146 147 std::map<hwc2_display_t, std::unique_ptr<Display>> mDisplays; 148 }; 149 150 } // namespace android 151 #endif 152