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/logging.h> 20 #include <android-base/unique_fd.h> 21 #include <xf86drm.h> 22 #include <xf86drmMode.h> 23 24 #include <cstdint> 25 #include <memory> 26 #include <string> 27 #include <unordered_map> 28 29 #include "Common.h" 30 #include "DrmMode.h" 31 #include "DrmProperty.h" 32 33 namespace aidl::android::hardware::graphics::composer3::impl { 34 35 // A "cable" to the display (HDMI, DisplayPort, etc). 36 class DrmConnector { 37 public: 38 static std::unique_ptr<DrmConnector> create(::android::base::borrowed_fd drmFd, 39 uint32_t connectorId); ~DrmConnector()40 ~DrmConnector(){}; 41 getId()42 uint32_t getId() const { return mId; } 43 44 uint32_t getWidth() const; 45 uint32_t getHeight() const; 46 47 int32_t getDpiX() const; 48 int32_t getDpiY() const; 49 50 float getRefreshRate() const; getRefreshRateUint()51 uint32_t getRefreshRateUint() const { return (uint32_t)(getRefreshRate() + 0.5f); } 52 isConnected()53 bool isConnected() const { return mStatus == DRM_MODE_CONNECTED; } 54 getEdid()55 std::optional<std::vector<uint8_t>> getEdid() const { return mEdid; } 56 getCrtcProperty()57 const DrmProperty& getCrtcProperty() const { return mCrtc; } getDefaultMode()58 const DrmMode* getDefaultMode() const { return mModes[0].get(); } 59 60 bool update(::android::base::borrowed_fd drmFd); 61 62 private: DrmConnector(uint32_t id)63 DrmConnector(uint32_t id) : mId(id) {} 64 65 bool loadEdid(::android::base::borrowed_fd drmFd); 66 67 const uint32_t mId; 68 69 drmModeConnection mStatus = DRM_MODE_UNKNOWNCONNECTION; 70 uint32_t mWidthMillimeters = -1; 71 uint32_t mHeightMillimeters = -1; 72 std::vector<std::unique_ptr<DrmMode>> mModes; 73 74 DrmProperty mCrtc; 75 DrmProperty mEdidProp; 76 std::optional<std::vector<uint8_t>> mEdid; 77 GetPropertiesMap()78 static const auto& GetPropertiesMap() { 79 static const auto* sMap = []() { 80 return new DrmPropertyMemberMap<DrmConnector>{ 81 {"CRTC_ID", &DrmConnector::mCrtc}, 82 {"EDID", &DrmConnector::mEdidProp}, 83 }; 84 }(); 85 return *sMap; 86 } 87 }; 88 89 } // namespace aidl::android::hardware::graphics::composer3::impl 90