• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "DrmConnector.h"
18 
19 namespace aidl::android::hardware::graphics::composer3::impl {
20 namespace {
21 
22 static constexpr const float kMillimetersPerInch = 25.4;
23 
24 }  // namespace
25 
create(::android::base::borrowed_fd drmFd,uint32_t connectorId)26 std::unique_ptr<DrmConnector> DrmConnector::create(::android::base::borrowed_fd drmFd,
27                                                    uint32_t connectorId) {
28     std::unique_ptr<DrmConnector> connector(new DrmConnector(connectorId));
29 
30     if (!LoadDrmProperties(drmFd, connectorId, DRM_MODE_OBJECT_CONNECTOR, GetPropertiesMap(),
31                            connector.get())) {
32         ALOGE("%s: Failed to load connector properties.", __FUNCTION__);
33         return nullptr;
34     }
35 
36     if (!connector->update(drmFd)) {
37         return nullptr;
38     }
39 
40     return connector;
41 }
42 
update(::android::base::borrowed_fd drmFd)43 bool DrmConnector::update(::android::base::borrowed_fd drmFd) {
44     DEBUG_LOG("%s: Loading properties for connector:%" PRIu32, __FUNCTION__, mId);
45 
46     drmModeConnector* drmConnector = drmModeGetConnector(drmFd.get(), mId);
47     if (!drmConnector) {
48         ALOGE("%s: Failed to load connector.", __FUNCTION__);
49         return false;
50     }
51 
52     mStatus = drmConnector->connection;
53 
54     mModes.clear();
55     for (uint32_t i = 0; i < drmConnector->count_modes; i++) {
56         auto mode = DrmMode::create(drmFd, drmConnector->modes[i]);
57         if (!mode) {
58             ALOGE("%s: Failed to create mode for connector.", __FUNCTION__);
59             return false;
60         }
61 
62         mModes.push_back(std::move(mode));
63     }
64 
65     drmModeFreeConnector(drmConnector);
66 
67     if (mStatus == DRM_MODE_CONNECTED) {
68         if (!loadEdid(drmFd)) {
69             return false;
70         }
71     }
72 
73     DEBUG_LOG("%s: connector:%" PRIu32 " widthMillimeters:%" PRIu32 " heightMillimeters:%" PRIu32,
74               __FUNCTION__, mId, mWidthMillimeters, mHeightMillimeters);
75 
76     return true;
77 }
78 
loadEdid(::android::base::borrowed_fd drmFd)79 bool DrmConnector::loadEdid(::android::base::borrowed_fd drmFd) {
80     DEBUG_LOG("%s: display:%" PRIu32, __FUNCTION__, mId);
81 
82     mWidthMillimeters = 0;
83     mHeightMillimeters = 0;
84 
85     const uint64_t edidBlobId = mEdidProp.getValue();
86     if (edidBlobId == -1) {
87         ALOGW("%s: display:%" PRIu32 " does not have EDID.", __FUNCTION__, mId);
88         return true;
89     }
90 
91     auto blob = drmModeGetPropertyBlob(drmFd.get(), edidBlobId);
92     if (!blob) {
93         ALOGE("%s: display:%" PRIu32 " failed to read EDID blob (%" PRIu64 "): %s", __FUNCTION__,
94               mId, edidBlobId, strerror(errno));
95         return false;
96     }
97 
98     const uint8_t* blobStart = static_cast<uint8_t*>(blob->data);
99     mEdid = std::vector<uint8_t>(blobStart, blobStart + blob->length);
100 
101     drmModeFreePropertyBlob(blob);
102 
103     using byte_view = std::basic_string_view<uint8_t>;
104 
105     constexpr size_t kEdidDescriptorOffset = 54;
106     constexpr size_t kEdidDescriptorLength = 18;
107 
108     byte_view edid(mEdid->data(), mEdid->size());
109     edid.remove_prefix(kEdidDescriptorOffset);
110 
111     byte_view descriptor(edid.data(), kEdidDescriptorLength);
112     if (descriptor[0] == 0 && descriptor[1] == 0) {
113         ALOGE("%s: display:%" PRIu32 " is missing preferred detailed timing descriptor.",
114               __FUNCTION__, mId);
115         return -1;
116     }
117 
118     const uint8_t w_mm_lsb = descriptor[12];
119     const uint8_t h_mm_lsb = descriptor[13];
120     const uint8_t w_and_h_mm_msb = descriptor[14];
121 
122     mWidthMillimeters = w_mm_lsb | (w_and_h_mm_msb & 0xf0) << 4;
123     mHeightMillimeters = h_mm_lsb | (w_and_h_mm_msb & 0xf) << 8;
124 
125     return true;
126 }
127 
getWidth() const128 uint32_t DrmConnector::getWidth() const {
129     DEBUG_LOG("%s: connector:%" PRIu32, __FUNCTION__, mId);
130 
131     if (mModes.empty()) {
132         return 0;
133     }
134     return mModes[0]->hdisplay;
135 }
136 
getHeight() const137 uint32_t DrmConnector::getHeight() const {
138     DEBUG_LOG("%s: connector:%" PRIu32, __FUNCTION__, mId);
139 
140     if (mModes.empty()) {
141         return 0;
142     }
143     return mModes[0]->vdisplay;
144 }
145 
getDpiX() const146 int32_t DrmConnector::getDpiX() const {
147     DEBUG_LOG("%s: connector:%" PRIu32, __FUNCTION__, mId);
148 
149     if (mModes.empty()) {
150         return -1;
151     }
152 
153     const auto& mode = mModes[0];
154     if (mWidthMillimeters) {
155         const int32_t dpi = static_cast<int32_t>(
156             (static_cast<float>(mode->hdisplay) / static_cast<float>(mWidthMillimeters)) *
157             kMillimetersPerInch);
158         DEBUG_LOG("%s: connector:%" PRIu32 " has dpi-x:%" PRId32, __FUNCTION__, mId, dpi);
159         return dpi;
160     }
161 
162     return -1;
163 }
164 
getDpiY() const165 int32_t DrmConnector::getDpiY() const {
166     DEBUG_LOG("%s: connector:%" PRIu32, __FUNCTION__, mId);
167 
168     if (mModes.empty()) {
169         return -1;
170     }
171 
172     const auto& mode = mModes[0];
173     if (mHeightMillimeters) {
174         const int32_t dpi = static_cast<int32_t>(
175             (static_cast<float>(mode->vdisplay) / static_cast<float>(mHeightMillimeters)) *
176             kMillimetersPerInch);
177         DEBUG_LOG("%s: connector:%" PRIu32 " has dpi-x:%" PRId32, __FUNCTION__, mId, dpi);
178         return dpi;
179     }
180 
181     return -1;
182 }
183 
getRefreshRate() const184 float DrmConnector::getRefreshRate() const {
185     DEBUG_LOG("%s: connector:%" PRIu32, __FUNCTION__, mId);
186 
187     if (!mModes.empty()) {
188         const auto& mode = mModes[0];
189         return 1000.0f * mode->clock / ((float)mode->vtotal * (float)mode->htotal);
190     }
191 
192     return -1.0f;
193 }
194 
195 }  // namespace aidl::android::hardware::graphics::composer3::impl
196