1 /* 2 * Copyright 2022 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 #pragma once 18 19 #include <android-base/unique_fd.h> 20 #include <cutils/native_handle.h> 21 22 #include <memory> 23 #include <tuple> 24 #include <vector> 25 26 #include "Common.h" 27 #include "DrmAtomicRequest.h" 28 #include "DrmBuffer.h" 29 #include "DrmConnector.h" 30 #include "DrmCrtc.h" 31 #include "DrmDisplay.h" 32 #include "DrmEventListener.h" 33 #include "DrmMode.h" 34 #include "DrmPlane.h" 35 #include "DrmProperty.h" 36 #include "LruCache.h" 37 #include "aemu/base/synchronization/AndroidLock.h" 38 39 namespace aidl::android::hardware::graphics::composer3::impl { 40 41 class DrmClient { 42 public: 43 DrmClient() = default; 44 ~DrmClient(); 45 46 DrmClient(const DrmClient&) = delete; 47 DrmClient& operator=(const DrmClient&) = delete; 48 49 DrmClient(DrmClient&&) = delete; 50 DrmClient& operator=(DrmClient&&) = delete; 51 52 HWC3::Error init(); 53 54 struct DisplayConfig { 55 uint32_t id; 56 uint32_t width; 57 uint32_t height; 58 uint32_t dpiX; 59 uint32_t dpiY; 60 uint32_t refreshRateHz; 61 }; 62 63 HWC3::Error getDisplayConfigs(std::vector<DisplayConfig>* configs) const; 64 65 using HotplugCallback = std::function<void(bool /*connected*/, // 66 uint32_t /*id*/, // 67 uint32_t /*width*/, // 68 uint32_t /*height*/, // 69 uint32_t /*dpiX*/, // 70 uint32_t /*dpiY*/, // 71 uint32_t /*refreshRate*/)>; 72 73 HWC3::Error registerOnHotplugCallback(const HotplugCallback& cb); 74 HWC3::Error unregisterOnHotplugCallback(); 75 refreshRate()76 uint32_t refreshRate() const { return mDisplays[0]->getRefreshRateUint(); } 77 78 std::tuple<HWC3::Error, std::shared_ptr<DrmBuffer>> create(const native_handle_t* handle); 79 80 std::tuple<HWC3::Error, ::android::base::unique_fd> flushToDisplay( 81 int display, const std::shared_ptr<DrmBuffer>& buffer, 82 ::android::base::borrowed_fd inWaitSyncFd); 83 84 std::optional<std::vector<uint8_t>> getEdid(uint32_t id); 85 86 private: 87 using DrmPrimeBufferHandle = uint32_t; 88 89 // Grant visibility to destroyDrmFramebuffer to DrmBuffer. 90 friend class DrmBuffer; 91 HWC3::Error destroyDrmFramebuffer(DrmBuffer* buffer); 92 93 // Grant visibility for handleHotplug to DrmEventListener. 94 bool handleHotplug(); 95 96 bool loadDrmDisplays(); 97 98 // Drm device. 99 ::android::base::unique_fd mFd; 100 101 mutable ::android::base::guest::ReadWriteLock mDisplaysMutex; 102 std::vector<std::unique_ptr<DrmDisplay>> mDisplays; 103 104 std::optional<HotplugCallback> mHotplugCallback; 105 106 std::unique_ptr<DrmEventListener> mDrmEventListener; 107 }; 108 109 } // namespace aidl::android::hardware::graphics::composer3::impl 110