-
Notifications
You must be signed in to change notification settings - Fork 6
/
DrmDisplay.cpp
334 lines (276 loc) · 9.36 KB
/
DrmDisplay.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2019 Stephan Gerhold
#define LOG_TAG "drmfb-display"
#include <array>
#include <xf86drm.h>
#include <android-base/logging.h>
#include "drm_unique_ptr.h"
#include "DrmDisplay.h"
#include "DrmDevice.h"
namespace android {
namespace hardware {
namespace graphics {
namespace composer {
namespace V2_1 {
namespace drmfb {
namespace {
constexpr std::array<std::string_view, 19> CONNECTOR_TYPE_NAMES{
"Unknown", "VGA", "DVI-I", "DVI-D", "DVI-A", "Composite", "SVIDEO",
"LVDS", "Component", "DIN", "DP", "HDMI-A", "HDMI-B", "TV",
"eDP", "Virtual", "DSI", "DPI", "Writeback",
};
constexpr std::string_view connectorTypeName(uint32_t connectorType) {
if (connectorType >= CONNECTOR_TYPE_NAMES.size()) {
return CONNECTOR_TYPE_NAMES.front();
}
return CONNECTOR_TYPE_NAMES[connectorType];
}
std::ostream& operator<<(std::ostream& os, const drmModeModeInfo& mode) {
return os << mode.name;
}
constexpr int32_t SECOND_NANOS = 1'000'000'000;
constexpr int32_t KINCH_MILLIMETER = 25400;
}
DrmDisplay::DrmDisplay(DrmDevice& device, uint32_t connectorId)
: mDevice(device), mConnector(connectorId), mVsyncThread(*this) {
update();
}
DrmDisplay::~DrmDisplay() {
if (mCrtc)
mDevice.freeCrtc(mPipe);
}
int32_t DrmDisplay::width(unsigned mode) const {
return mode < mModes.size() ? mModes[mode].hdisplay : -1;
}
int32_t DrmDisplay::height(unsigned mode) const {
return mode < mModes.size() ? mModes[mode].vdisplay : -1;
}
int32_t DrmDisplay::vsyncPeriod(unsigned mode) const {
if (mode >= mModes.size()) return -1;
auto refresh = mModes[mode].vrefresh;
return refresh > 0 ? SECOND_NANOS / mModes[mode].vrefresh : 0;
}
int32_t DrmDisplay::dpiX(unsigned mode) const {
if (mode >= mModes.size()) return -1;
return mmWidth > 0 ? mModes[mode].hdisplay * KINCH_MILLIMETER / mmWidth : 0;
}
int32_t DrmDisplay::dpiY(unsigned mode) const {
if (mode >= mModes.size()) return -1;
return mmHeight > 0 ? mModes[mode].vdisplay * KINCH_MILLIMETER / mmHeight : 0;
}
void DrmDisplay::update() {
drm::mode::unique_connector_ptr connector{
drmModeGetConnector(mDevice.fd(), mConnector)};
auto connected = false;
if (connector) {
connected = connector->connection == DRM_MODE_CONNECTED
&& connector->count_modes > 0;
} else {
PLOG(ERROR) << "Failed to get DRM connector " << mConnector;
}
if (connected == mConnected)
return; // Only update on hotplug
mConnected = connected;
if (connected) {
mType = connector->connector_type;
mName = connectorTypeName(connector->connector_type);
mName += '-' + std::to_string(connector->connector_type_id);
mmWidth = connector->mmWidth;
mmHeight = connector->mmHeight;
setModes(connector->modes, connector->modes + connector->count_modes);
LOG(INFO) << "Display " << *this << " connected, "
<< mModes.size() << " mode(s), "
<< "default: " << mModes[mCurrentMode];
report();
} else {
LOG(INFO) << "Display " << *this << " disconnected";
disableVsync();
if (mCrtc)
mDevice.freeCrtc(mPipe);
mFlipPending = false;
mModeSet = false;
mCrtc = 0;
mFramebuffers.clear();
mModes.clear();
report();
mVsyncThread.stop();
}
}
void DrmDisplay::setModes(const drmModeModeInfo* begin, const drmModeModeInfo* end) {
mCurrentMode = 0;
mModes.clear();
/*
* Originally, this simply exposed all modes provided by DRM as
* available display configs. However, the Android framework
* skips modes that do not differ by any attributes it is
* concerned with (e.g. only DRM flags instead of width/height/vrefresh).
* This may cause it to use the wrong (e.g. interlaced) mode later.
*
* To avoid this, only the first mode for each combination of
* width/height/vrefresh is exposed.
*/
// Add the preferred mode first
auto preferred = std::find_if(begin, end,
[] (const auto& mode) { return mode.type & DRM_MODE_TYPE_PREFERRED; });
if (preferred != end) {
mModes.emplace_back(*preferred);
}
// Add all other modes; each combination only once
for (auto mode = begin; mode < end; ++mode) {
if (std::find_if(mModes.begin(), mModes.end(),
[mode] (const auto& mode2) {
return mode->hdisplay == mode2.hdisplay
&& mode->vdisplay == mode2.vdisplay
&& mode->vrefresh == mode2.vrefresh;
}) == mModes.end()) {
mModes.emplace_back(*mode);
}
}
}
void DrmDisplay::report() {
if (auto callback = mDevice.callback(); callback) {
callback->onHotplug(*this, mConnected);
}
}
void DrmDisplay::vsync(int64_t timestamp) {
if (auto callback = mDevice.callback(); callback) {
callback->onVsync(*this, timestamp);
}
}
bool DrmDisplay::setMode(unsigned mode) {
if (mCurrentMode == mode)
return true;
if (mode >= mModes.size())
return false;
LOG(INFO) << "Setting mode " << mModes[mode] << " for display " << *this;
mCurrentMode = mode;
mModeSet = false;
return true;
}
namespace {
void handlePageFlip(int /*fd*/, unsigned int /*sequence*/,
unsigned int /*tv_sec */, unsigned int /*tv_usec*/, void* user_data) {
auto display = static_cast<DrmDisplay*>(user_data);
display->handlePageFlip();
}
drmEventContext pageFlipEvCtx = {
.version = DRM_EVENT_CONTEXT_VERSION,
.page_flip_handler = handlePageFlip,
};
}
void DrmDisplay::handlePageFlip() {
if (mFlipPending) {
mFlipPending = false;
} else if (mConnected) {
LOG(WARNING) << "handlePageFlip() called for display " << *this
<< " without flip pending";
}
}
void DrmDisplay::awaitPageFlip() const {
// Wait for the last page flip to complete
while (mFlipPending) {
if (drmHandleEvent(mDevice.fd(), &pageFlipEvCtx)) {
PLOG(ERROR) << "Failed to handle DRM event";
return;
}
}
}
bool DrmDisplay::enable() {
if (enabled())
return true;
if (!mConnected)
return false;
drm::mode::unique_connector_ptr connector{
drmModeGetConnector(mDevice.fd(), mConnector)};
// Verify that the display is still connected, just to be sure
if (!connector || connector->connection != DRM_MODE_CONNECTED) {
update();
return false;
}
LOG(INFO) << "Enabling display " << *this;
// Attempt to find a CRTC that is not used by any other display
for (auto i = 0; i < connector->count_encoders; ++i) {
drm::mode::unique_encoder_ptr encoder{
drmModeGetEncoder(mDevice.fd(), connector->encoders[i])};
if (!encoder) {
PLOG(ERROR) << "Failed to get encoder " << connector->encoders[i];
continue;
}
for (mPipe = 0; mPipe < mDevice.crtcs().size(); ++mPipe) {
if (!(encoder->possible_crtcs & (1 << mPipe)))
continue;
mCrtc = mDevice.reserveCrtc(mPipe);
if (mCrtc) {
LOG(INFO) << "Using CRTC " << mCrtc << " for display " << *this;
return true;
} else {
LOG(WARNING) << "CRTC " << mDevice.crtcs()[mPipe]
<< " for display " << *this
<< " is already in use by another display";
}
}
}
LOG(ERROR) << "Failed to find CRTC for display " << *this;
return false;
}
void DrmDisplay::disable() {
if (!enabled())
return;
LOG(INFO) << "Disabling display " << *this;
if (mModeSet) {
mVsyncThread.disable();
awaitPageFlip();
if (drmModeSetCrtc(mDevice.fd(), mCrtc, 0, 0, 0, nullptr, 0, nullptr)) {
PLOG(ERROR) << "Failed to disable display " << *this;
}
mModeSet = false;
}
mDevice.freeCrtc(mPipe);
mCrtc = 0;
}
void DrmDisplay::enableVsync() {
mVsyncEnabled = true;
if (mModeSet) {
mVsyncThread.enable();
}
}
void DrmDisplay::disableVsync() {
mVsyncEnabled = false;
mVsyncThread.disable();
}
void DrmDisplay::present(buffer_handle_t buffer) {
if (!enabled())
return;
auto& fb = mFramebuffers.try_emplace(buffer, mDevice, buffer).first->second;
if (!fb.id()) {
// The framebuffer error was already logged
return;
}
awaitPageFlip();
if (mModeSet) {
mFlipPending = true;
if (drmModePageFlip(mDevice.fd(), mCrtc, fb.id(), DRM_MODE_PAGE_FLIP_EVENT, this)) {
PLOG(ERROR) << "Failed to perform page flip for display " << *this;
mFlipPending = false;
}
} else {
auto mode = &mModes[mCurrentMode];
if (drmModeSetCrtc(mDevice.fd(), mCrtc, fb.id(), 0, 0, &mConnector, 1, mode)) {
PLOG(ERROR) << "Failed to enable CRTC " << mCrtc
<< " for display " << *this;
} else {
mModeSet = true;
if (mVsyncEnabled)
mVsyncThread.enable();
}
}
}
std::ostream& operator<<(std::ostream& os, const DrmDisplay& display) {
return os << display.mConnector << " (" << display.mName << ")";
}
} // namespace drmfb
} // namespace V2_1
} // namespace composer
} // namespace graphics
} // namespace hardware
} // namespace android