Dan Stoza | 651bf31 | 2015-10-23 17:03:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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_SF_HWC2_H |
| 18 | #define ANDROID_SF_HWC2_H |
| 19 | |
| 20 | #define HWC2_INCLUDE_STRINGIFICATION |
| 21 | #define HWC2_USE_CPP11 |
| 22 | #include <hardware/hwcomposer2.h> |
| 23 | #undef HWC2_INCLUDE_STRINGIFICATION |
| 24 | #undef HWC2_USE_CPP11 |
| 25 | |
| 26 | #include <utils/Log.h> |
| 27 | #include <utils/StrongPointer.h> |
| 28 | #include <utils/Timers.h> |
| 29 | |
| 30 | #include <functional> |
| 31 | #include <string> |
| 32 | #include <unordered_map> |
| 33 | #include <vector> |
| 34 | |
| 35 | namespace android { |
| 36 | class Fence; |
| 37 | class FloatRect; |
| 38 | class GraphicBuffer; |
| 39 | class Rect; |
| 40 | class Region; |
| 41 | } |
| 42 | |
| 43 | namespace HWC2 { |
| 44 | |
| 45 | class Display; |
| 46 | class Layer; |
| 47 | |
| 48 | typedef std::function<void(std::shared_ptr<Display>, Connection)> |
| 49 | HotplugCallback; |
| 50 | typedef std::function<void(std::shared_ptr<Display>)> RefreshCallback; |
| 51 | typedef std::function<void(std::shared_ptr<Display>, nsecs_t)> VsyncCallback; |
| 52 | |
| 53 | class Device |
| 54 | { |
| 55 | public: |
| 56 | Device(hwc2_device_t* device); |
| 57 | ~Device(); |
| 58 | |
| 59 | friend class HWC2::Display; |
| 60 | friend class HWC2::Layer; |
| 61 | |
| 62 | // Required by HWC2 |
| 63 | |
| 64 | std::string dump() const; |
| 65 | |
| 66 | const std::vector<Capability>& getCapabilities() const { |
| 67 | return mCapabilities; |
| 68 | }; |
| 69 | |
| 70 | uint32_t getMaxVirtualDisplayCount() const; |
| 71 | Error createVirtualDisplay(uint32_t width, uint32_t height, |
| 72 | std::shared_ptr<Display>* outDisplay); |
| 73 | |
| 74 | void registerHotplugCallback(HotplugCallback hotplug); |
| 75 | void registerRefreshCallback(RefreshCallback refresh); |
| 76 | void registerVsyncCallback(VsyncCallback vsync); |
| 77 | |
| 78 | // For use by callbacks |
| 79 | |
| 80 | void callHotplug(std::shared_ptr<Display> display, Connection connected); |
| 81 | void callRefresh(std::shared_ptr<Display> display); |
| 82 | void callVsync(std::shared_ptr<Display> display, nsecs_t timestamp); |
| 83 | |
| 84 | // Other Device methods |
| 85 | |
| 86 | // This will create a Display if one is not found, but it will not be marked |
| 87 | // as connected |
| 88 | std::shared_ptr<Display> getDisplayById(hwc2_display_t id); |
| 89 | |
| 90 | private: |
| 91 | // Initialization methods |
| 92 | |
| 93 | template <typename PFN> |
| 94 | [[clang::warn_unused_result]] bool loadFunctionPointer( |
| 95 | FunctionDescriptor desc, PFN& outPFN) { |
| 96 | auto intDesc = static_cast<int32_t>(desc); |
| 97 | auto pfn = mHwcDevice->getFunction(mHwcDevice, intDesc); |
| 98 | if (pfn != nullptr) { |
| 99 | outPFN = reinterpret_cast<PFN>(pfn); |
| 100 | return true; |
| 101 | } else { |
| 102 | ALOGE("Failed to load function %s", to_string(desc).c_str()); |
| 103 | return false; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | template <typename PFN, typename HOOK> |
| 108 | void registerCallback(Callback callback, HOOK hook) { |
| 109 | static_assert(std::is_same<PFN, HOOK>::value, |
| 110 | "Incompatible function pointer"); |
| 111 | auto intCallback = static_cast<int32_t>(callback); |
| 112 | auto callbackData = static_cast<hwc2_callback_data_t>(this); |
| 113 | auto pfn = reinterpret_cast<hwc2_function_pointer_t>(hook); |
| 114 | mRegisterCallback(mHwcDevice, intCallback, callbackData, pfn); |
| 115 | } |
| 116 | |
| 117 | void loadCapabilities(); |
| 118 | void loadFunctionPointers(); |
| 119 | void registerCallbacks(); |
| 120 | |
| 121 | // For use by Display |
| 122 | |
| 123 | void destroyVirtualDisplay(hwc2_display_t display); |
| 124 | |
| 125 | // Member variables |
| 126 | |
| 127 | hwc2_device_t* mHwcDevice; |
| 128 | |
| 129 | // Device function pointers |
| 130 | HWC2_PFN_CREATE_VIRTUAL_DISPLAY mCreateVirtualDisplay; |
| 131 | HWC2_PFN_DESTROY_VIRTUAL_DISPLAY mDestroyVirtualDisplay; |
| 132 | HWC2_PFN_DUMP mDump; |
| 133 | HWC2_PFN_GET_MAX_VIRTUAL_DISPLAY_COUNT mGetMaxVirtualDisplayCount; |
| 134 | HWC2_PFN_REGISTER_CALLBACK mRegisterCallback; |
| 135 | |
| 136 | // Display function pointers |
| 137 | HWC2_PFN_ACCEPT_DISPLAY_CHANGES mAcceptDisplayChanges; |
| 138 | HWC2_PFN_CREATE_LAYER mCreateLayer; |
| 139 | HWC2_PFN_DESTROY_LAYER mDestroyLayer; |
| 140 | HWC2_PFN_GET_ACTIVE_CONFIG mGetActiveConfig; |
| 141 | HWC2_PFN_GET_CHANGED_COMPOSITION_TYPES mGetChangedCompositionTypes; |
| 142 | HWC2_PFN_GET_DISPLAY_ATTRIBUTE mGetDisplayAttribute; |
| 143 | HWC2_PFN_GET_DISPLAY_CONFIGS mGetDisplayConfigs; |
| 144 | HWC2_PFN_GET_DISPLAY_NAME mGetDisplayName; |
| 145 | HWC2_PFN_GET_DISPLAY_REQUESTS mGetDisplayRequests; |
| 146 | HWC2_PFN_GET_DISPLAY_TYPE mGetDisplayType; |
| 147 | HWC2_PFN_GET_DOZE_SUPPORT mGetDozeSupport; |
| 148 | HWC2_PFN_GET_RELEASE_FENCES mGetReleaseFences; |
| 149 | HWC2_PFN_PRESENT_DISPLAY mPresentDisplay; |
| 150 | HWC2_PFN_SET_ACTIVE_CONFIG mSetActiveConfig; |
| 151 | HWC2_PFN_SET_CLIENT_TARGET mSetClientTarget; |
| 152 | HWC2_PFN_SET_OUTPUT_BUFFER mSetOutputBuffer; |
| 153 | HWC2_PFN_SET_POWER_MODE mSetPowerMode; |
| 154 | HWC2_PFN_SET_VSYNC_ENABLED mSetVsyncEnabled; |
| 155 | HWC2_PFN_VALIDATE_DISPLAY mValidateDisplay; |
| 156 | |
| 157 | // Layer function pointers |
| 158 | HWC2_PFN_SET_CURSOR_POSITION mSetCursorPosition; |
| 159 | HWC2_PFN_SET_LAYER_BUFFER mSetLayerBuffer; |
| 160 | HWC2_PFN_SET_LAYER_SURFACE_DAMAGE mSetLayerSurfaceDamage; |
| 161 | HWC2_PFN_SET_LAYER_BLEND_MODE mSetLayerBlendMode; |
| 162 | HWC2_PFN_SET_LAYER_COLOR mSetLayerColor; |
| 163 | HWC2_PFN_SET_LAYER_COMPOSITION_TYPE mSetLayerCompositionType; |
| 164 | HWC2_PFN_SET_LAYER_DISPLAY_FRAME mSetLayerDisplayFrame; |
| 165 | HWC2_PFN_SET_LAYER_PLANE_ALPHA mSetLayerPlaneAlpha; |
| 166 | HWC2_PFN_SET_LAYER_SIDEBAND_STREAM mSetLayerSidebandStream; |
| 167 | HWC2_PFN_SET_LAYER_SOURCE_CROP mSetLayerSourceCrop; |
| 168 | HWC2_PFN_SET_LAYER_TRANSFORM mSetLayerTransform; |
| 169 | HWC2_PFN_SET_LAYER_VISIBLE_REGION mSetLayerVisibleRegion; |
| 170 | HWC2_PFN_SET_LAYER_Z_ORDER mSetLayerZOrder; |
| 171 | |
| 172 | std::vector<Capability> mCapabilities; |
| 173 | std::unordered_map<hwc2_display_t, std::shared_ptr<Display>> mDisplays; |
| 174 | |
| 175 | HotplugCallback mHotplug; |
| 176 | std::vector<std::pair<std::shared_ptr<Display>, Connection>> |
| 177 | mPendingHotplugs; |
| 178 | RefreshCallback mRefresh; |
| 179 | std::vector<std::shared_ptr<Display>> mPendingRefreshes; |
| 180 | VsyncCallback mVsync; |
| 181 | std::vector<std::pair<std::shared_ptr<Display>, nsecs_t>> mPendingVsyncs; |
| 182 | }; |
| 183 | |
| 184 | class Display : public std::enable_shared_from_this<Display> |
| 185 | { |
| 186 | public: |
| 187 | Display(Device& device, hwc2_display_t id); |
| 188 | ~Display(); |
| 189 | |
| 190 | friend class HWC2::Device; |
| 191 | friend class HWC2::Layer; |
| 192 | |
| 193 | class Config |
| 194 | { |
| 195 | public: |
| 196 | class Builder |
| 197 | { |
| 198 | public: |
| 199 | Builder(Display& display, hwc2_config_t id); |
| 200 | |
| 201 | std::shared_ptr<const Config> build() { |
| 202 | return std::const_pointer_cast<const Config>( |
| 203 | std::move(mConfig)); |
| 204 | } |
| 205 | |
| 206 | Builder& setWidth(int32_t width) { |
| 207 | mConfig->mWidth = width; |
| 208 | return *this; |
| 209 | } |
| 210 | Builder& setHeight(int32_t height) { |
| 211 | mConfig->mHeight = height; |
| 212 | return *this; |
| 213 | } |
| 214 | Builder& setVsyncPeriod(int32_t vsyncPeriod) { |
| 215 | mConfig->mVsyncPeriod = vsyncPeriod; |
| 216 | return *this; |
| 217 | } |
| 218 | Builder& setDpiX(int32_t dpiX) { |
| 219 | if (dpiX == -1) { |
| 220 | mConfig->mDpiX = getDefaultDensity(); |
| 221 | } else { |
| 222 | mConfig->mDpiX = dpiX / 1000.0f; |
| 223 | } |
| 224 | return *this; |
| 225 | } |
| 226 | Builder& setDpiY(int32_t dpiY) { |
| 227 | if (dpiY == -1) { |
| 228 | mConfig->mDpiY = getDefaultDensity(); |
| 229 | } else { |
| 230 | mConfig->mDpiY = dpiY / 1000.0f; |
| 231 | } |
| 232 | return *this; |
| 233 | } |
| 234 | |
| 235 | private: |
| 236 | float getDefaultDensity(); |
| 237 | std::shared_ptr<Config> mConfig; |
| 238 | }; |
| 239 | |
| 240 | hwc2_display_t getDisplayId() const { return mDisplay.getId(); } |
| 241 | hwc2_config_t getId() const { return mId; } |
| 242 | |
| 243 | int32_t getWidth() const { return mWidth; } |
| 244 | int32_t getHeight() const { return mHeight; } |
| 245 | nsecs_t getVsyncPeriod() const { return mVsyncPeriod; } |
| 246 | float getDpiX() const { return mDpiX; } |
| 247 | float getDpiY() const { return mDpiY; } |
| 248 | |
| 249 | private: |
| 250 | Config(Display& display, hwc2_config_t id); |
| 251 | |
| 252 | Display& mDisplay; |
| 253 | hwc2_config_t mId; |
| 254 | |
| 255 | int32_t mWidth; |
| 256 | int32_t mHeight; |
| 257 | nsecs_t mVsyncPeriod; |
| 258 | float mDpiX; |
| 259 | float mDpiY; |
| 260 | }; |
| 261 | |
| 262 | // Required by HWC2 |
| 263 | |
| 264 | [[clang::warn_unused_result]] Error acceptChanges(); |
| 265 | [[clang::warn_unused_result]] Error createLayer( |
| 266 | std::shared_ptr<Layer>* outLayer); |
| 267 | [[clang::warn_unused_result]] Error getActiveConfig( |
| 268 | std::shared_ptr<const Config>* outConfig) const; |
| 269 | [[clang::warn_unused_result]] Error getChangedCompositionTypes( |
| 270 | std::unordered_map<std::shared_ptr<Layer>, Composition>* outTypes); |
| 271 | |
| 272 | // Doesn't call into the HWC2 device, so no errors are possible |
| 273 | std::vector<std::shared_ptr<const Config>> getConfigs() const; |
| 274 | |
| 275 | [[clang::warn_unused_result]] Error getName(std::string* outName) const; |
| 276 | [[clang::warn_unused_result]] Error getRequests( |
| 277 | DisplayRequest* outDisplayRequests, |
| 278 | std::unordered_map<std::shared_ptr<Layer>, LayerRequest>* |
| 279 | outLayerRequests); |
| 280 | [[clang::warn_unused_result]] Error getType(DisplayType* outType) const; |
| 281 | [[clang::warn_unused_result]] Error supportsDoze(bool* outSupport) const; |
| 282 | [[clang::warn_unused_result]] Error getReleaseFences( |
| 283 | std::unordered_map<std::shared_ptr<Layer>, |
| 284 | android::sp<android::Fence>>* outFences) const; |
| 285 | [[clang::warn_unused_result]] Error present( |
| 286 | android::sp<android::Fence>* outRetireFence); |
| 287 | [[clang::warn_unused_result]] Error setActiveConfig( |
| 288 | const std::shared_ptr<const Config>& config); |
| 289 | [[clang::warn_unused_result]] Error setClientTarget( |
| 290 | buffer_handle_t target, |
| 291 | const android::sp<android::Fence>& acquireFence, |
| 292 | android_dataspace_t dataspace); |
| 293 | [[clang::warn_unused_result]] Error setOutputBuffer( |
| 294 | const android::sp<android::GraphicBuffer>& buffer, |
| 295 | const android::sp<android::Fence>& releaseFence); |
| 296 | [[clang::warn_unused_result]] Error setPowerMode(PowerMode mode); |
| 297 | [[clang::warn_unused_result]] Error setVsyncEnabled(Vsync enabled); |
| 298 | [[clang::warn_unused_result]] Error validate(uint32_t* outNumTypes, |
| 299 | uint32_t* outNumRequests); |
| 300 | |
| 301 | // Other Display methods |
| 302 | |
| 303 | Device& getDevice() const { return mDevice; } |
| 304 | hwc2_display_t getId() const { return mId; } |
| 305 | bool isConnected() const { return mIsConnected; } |
| 306 | |
| 307 | private: |
| 308 | // For use by Device |
| 309 | |
| 310 | // Virtual displays are always connected |
| 311 | void setVirtual() { |
| 312 | mIsVirtual = true; |
| 313 | mIsConnected = true; |
| 314 | } |
| 315 | |
| 316 | void setConnected(bool connected) { mIsConnected = connected; } |
| 317 | int32_t getAttribute(hwc2_config_t configId, Attribute attribute); |
| 318 | void loadConfig(hwc2_config_t configId); |
| 319 | void loadConfigs(); |
| 320 | |
| 321 | // For use by Layer |
| 322 | void destroyLayer(hwc2_layer_t layerId); |
| 323 | |
| 324 | // This may fail (and return a null pointer) if no layer with this ID exists |
| 325 | // on this display |
| 326 | std::shared_ptr<Layer> getLayerById(hwc2_layer_t id) const; |
| 327 | |
| 328 | // Member variables |
| 329 | |
| 330 | Device& mDevice; |
| 331 | hwc2_display_t mId; |
| 332 | bool mIsConnected; |
| 333 | bool mIsVirtual; |
| 334 | std::unordered_map<hwc2_layer_t, std::weak_ptr<Layer>> mLayers; |
| 335 | std::unordered_map<hwc2_config_t, std::shared_ptr<const Config>> mConfigs; |
| 336 | }; |
| 337 | |
| 338 | class Layer |
| 339 | { |
| 340 | public: |
| 341 | Layer(const std::shared_ptr<Display>& display, hwc2_layer_t id); |
| 342 | ~Layer(); |
| 343 | |
| 344 | bool isAbandoned() const { return mDisplay.expired(); } |
| 345 | hwc2_layer_t getId() const { return mId; } |
| 346 | |
| 347 | [[clang::warn_unused_result]] Error setCursorPosition(int32_t x, int32_t y); |
| 348 | [[clang::warn_unused_result]] Error setBuffer(buffer_handle_t buffer, |
| 349 | const android::sp<android::Fence>& acquireFence); |
| 350 | [[clang::warn_unused_result]] Error setSurfaceDamage( |
| 351 | const android::Region& damage); |
| 352 | |
| 353 | [[clang::warn_unused_result]] Error setBlendMode(BlendMode mode); |
| 354 | [[clang::warn_unused_result]] Error setColor(hwc_color_t color); |
| 355 | [[clang::warn_unused_result]] Error setCompositionType(Composition type); |
| 356 | [[clang::warn_unused_result]] Error setDisplayFrame( |
| 357 | const android::Rect& frame); |
| 358 | [[clang::warn_unused_result]] Error setPlaneAlpha(float alpha); |
| 359 | [[clang::warn_unused_result]] Error setSidebandStream( |
| 360 | const native_handle_t* stream); |
| 361 | [[clang::warn_unused_result]] Error setSourceCrop( |
| 362 | const android::FloatRect& crop); |
| 363 | [[clang::warn_unused_result]] Error setTransform(Transform transform); |
| 364 | [[clang::warn_unused_result]] Error setVisibleRegion( |
| 365 | const android::Region& region); |
| 366 | [[clang::warn_unused_result]] Error setZOrder(uint32_t z); |
| 367 | |
| 368 | private: |
| 369 | std::weak_ptr<Display> mDisplay; |
| 370 | hwc2_display_t mDisplayId; |
| 371 | Device& mDevice; |
| 372 | hwc2_layer_t mId; |
| 373 | }; |
| 374 | |
| 375 | } // namespace HWC2 |
| 376 | |
| 377 | #endif // ANDROID_SF_HWC2_H |