| 1 |
/* |
| 2 |
* Copyright (C) 2005-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "DRMUtils.h" |
| 10 |
|
| 11 |
#include "settings/Settings.h" |
| 12 |
#include "settings/SettingsComponent.h" |
| 13 |
#include "utils/DRMHelpers.h" |
| 14 |
#include "utils/StringUtils.h" |
| 15 |
#include "utils/log.h" |
| 16 |
#include "windowing/GraphicContext.h" |
| 17 |
|
| 18 |
#include "PlatformDefs.h" |
| 19 |
|
| 20 |
using namespace KODI::WINDOWING::GBM; |
| 21 |
|
| 22 |
namespace |
| 23 |
{ |
| 24 |
const std::string SETTING_VIDEOSCREEN_LIMITGUISIZE = "videoscreen.limitguisize"; |
| 25 |
|
| 26 |
void DrmFbDestroyCallback(gbm_bo* bo, void* data) |
| 27 |
{ |
| 28 |
drm_fb* fb = static_cast<drm_fb*>(data); |
| 29 |
|
| 30 |
if (fb->fb_id > 0) |
| 31 |
{ |
| 32 |
CLog::Log(LOGDEBUG, "CDRMUtils::{} - removing framebuffer: {}", __FUNCTION__, fb->fb_id); |
| 33 |
int drm_fd = gbm_device_get_fd(gbm_bo_get_device(bo)); |
| 34 |
drmModeRmFB(drm_fd, fb->fb_id); |
| 35 |
} |
| 36 |
|
| 37 |
delete fb; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
CDRMUtils::~CDRMUtils() |
| 42 |
{ |
| 43 |
DestroyDrm(); |
| 44 |
} |
| 45 |
|
| 46 |
bool CDRMUtils::SetMode(const RESOLUTION_INFO& res) |
| 47 |
{ |
| 48 |
if (!m_connector->CheckConnector()) |
| 49 |
return false; |
| 50 |
|
| 51 |
m_mode = m_connector->GetModeForIndex(std::atoi(res.strId.c_str())); |
| 52 |
m_width = res.iWidth; |
| 53 |
m_height = res.iHeight; |
| 54 |
|
| 55 |
CLog::Log(LOGDEBUG, "CDRMUtils::{} - found crtc mode: {}x{}{} @ {} Hz", __FUNCTION__, |
| 56 |
m_mode->hdisplay, m_mode->vdisplay, m_mode->flags & DRM_MODE_FLAG_INTERLACE ? "i" : "", |
| 57 |
m_mode->vrefresh); |
| 58 |
|
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
drm_fb * CDRMUtils::DrmFbGetFromBo(struct gbm_bo *bo) |
| 63 |
{ |
| 64 |
{ |
| 65 |
struct drm_fb *fb = static_cast<drm_fb *>(gbm_bo_get_user_data(bo)); |
| 66 |
if(fb) |
| 67 |
{ |
| 68 |
if (m_gui_plane->GetFormat() == fb->format) |
| 69 |
return fb; |
| 70 |
else |
| 71 |
DrmFbDestroyCallback(bo, gbm_bo_get_user_data(bo)); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
struct drm_fb *fb = new drm_fb; |
| 76 |
fb->bo = bo; |
| 77 |
fb->format = m_gui_plane->GetFormat(); |
| 78 |
|
| 79 |
uint32_t width, |
| 80 |
height, |
| 81 |
handles[4] = {0}, |
| 82 |
strides[4] = {0}, |
| 83 |
offsets[4] = {0}; |
| 84 |
|
| 85 |
uint64_t modifiers[4] = {0}; |
| 86 |
|
| 87 |
width = gbm_bo_get_width(bo); |
| 88 |
height = gbm_bo_get_height(bo); |
| 89 |
|
| 90 |
#if defined(HAS_GBM_MODIFIERS) |
| 91 |
for (int i = 0; i < gbm_bo_get_plane_count(bo); i++) |
| 92 |
{ |
| 93 |
handles[i] = gbm_bo_get_handle_for_plane(bo, i).u32; |
| 94 |
strides[i] = gbm_bo_get_stride_for_plane(bo, i); |
| 95 |
offsets[i] = gbm_bo_get_offset(bo, i); |
| 96 |
modifiers[i] = gbm_bo_get_modifier(bo); |
| 97 |
} |
| 98 |
#else |
| 99 |
handles[0] = gbm_bo_get_handle(bo).u32; |
| 100 |
strides[0] = gbm_bo_get_stride(bo); |
| 101 |
memset(offsets, 0, 16); |
| 102 |
#endif |
| 103 |
|
| 104 |
uint32_t flags = 0; |
| 105 |
|
| 106 |
if (modifiers[0] && modifiers[0] != DRM_FORMAT_MOD_INVALID) |
| 107 |
{ |
| 108 |
flags |= DRM_MODE_FB_MODIFIERS; |
| 109 |
CLog::Log(LOGDEBUG, "CDRMUtils::{} - using modifier: {}", __FUNCTION__, |
| 110 |
DRMHELPERS::ModifierToString(modifiers[0])); |
| 111 |
} |
| 112 |
|
| 113 |
int ret = drmModeAddFB2WithModifiers(m_fd, |
| 114 |
width, |
| 115 |
height, |
| 116 |
fb->format, |
| 117 |
handles, |
| 118 |
strides, |
| 119 |
offsets, |
| 120 |
modifiers, |
| 121 |
&fb->fb_id, |
| 122 |
flags); |
| 123 |
|
| 124 |
if(ret < 0) |
| 125 |
{ |
| 126 |
ret = drmModeAddFB2(m_fd, |
| 127 |
width, |
| 128 |
height, |
| 129 |
fb->format, |
| 130 |
handles, |
| 131 |
strides, |
| 132 |
offsets, |
| 133 |
&fb->fb_id, |
| 134 |
flags); |
| 135 |
|
| 136 |
if (ret < 0) |
| 137 |
{ |
| 138 |
delete (fb); |
| 139 |
CLog::Log(LOGDEBUG, "CDRMUtils::{} - failed to add framebuffer: {} ({})", __FUNCTION__, strerror(errno), errno); |
| 140 |
return nullptr; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
gbm_bo_set_user_data(bo, fb, DrmFbDestroyCallback); |
| 145 |
|
| 146 |
return fb; |
| 147 |
} |
| 148 |
|
| 149 |
bool CDRMUtils::FindPreferredMode() |
| 150 |
{ |
| 151 |
if (m_mode) |
| 152 |
return true; |
| 153 |
|
| 154 |
for (int i = 0, area = 0; i < m_connector->GetModesCount(); i++) |
| 155 |
{ |
| 156 |
drmModeModeInfo* current_mode = m_connector->GetModeForIndex(i); |
| 157 |
|
| 158 |
if(current_mode->type & DRM_MODE_TYPE_PREFERRED) |
| 159 |
{ |
| 160 |
m_mode = current_mode; |
| 161 |
CLog::Log(LOGDEBUG, "CDRMUtils::{} - found preferred mode: {}x{}{} @ {} Hz", __FUNCTION__, |
| 162 |
m_mode->hdisplay, m_mode->vdisplay, |
| 163 |
m_mode->flags & DRM_MODE_FLAG_INTERLACE ? "i" : "", m_mode->vrefresh); |
| 164 |
break; |
| 165 |
} |
| 166 |
|
| 167 |
auto current_area = current_mode->hdisplay * current_mode->vdisplay; |
| 168 |
if (current_area > area) |
| 169 |
{ |
| 170 |
m_mode = current_mode; |
| 171 |
area = current_area; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
if(!m_mode) |
| 176 |
{ |
| 177 |
CLog::Log(LOGDEBUG, "CDRMUtils::{} - failed to find preferred mode", __FUNCTION__); |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
return true; |
| 182 |
} |
| 183 |
|
| 184 |
bool CDRMUtils::FindPlanes() |
| 185 |
{ |
| 186 |
for (size_t i = 0; i < m_crtcs.size(); i++) |
| 187 |
{ |
| 188 |
if (!(m_encoder->GetPossibleCrtcs() & (1 << i))) |
| 189 |
continue; |
| 190 |
|
| 191 |
auto videoPlane = std::find_if(m_planes.begin(), m_planes.end(), [&i](auto& plane) { |
| 192 |
if (plane->GetPossibleCrtcs() & (1 << i)) |
| 193 |
{ |
| 194 |
return plane->SupportsFormat(DRM_FORMAT_NV12); |
| 195 |
} |
| 196 |
return false; |
| 197 |
}); |
| 198 |
|
| 199 |
uint32_t videoPlaneId{0}; |
| 200 |
|
| 201 |
if (videoPlane != m_planes.end()) |
| 202 |
videoPlaneId = videoPlane->get()->GetPlaneId(); |
| 203 |
|
| 204 |
auto guiPlane = |
| 205 |
std::find_if(m_planes.begin(), m_planes.end(), [&i, &videoPlaneId](auto& plane) { |
| 206 |
if (plane->GetPossibleCrtcs() & (1 << i)) |
| 207 |
{ |
| 208 |
return (plane->GetPlaneId() != videoPlaneId && |
| 209 |
(videoPlaneId == 0 || plane->SupportsFormat(DRM_FORMAT_ARGB8888)) && |
| 210 |
(plane->SupportsFormat(DRM_FORMAT_XRGB2101010) || |
| 211 |
plane->SupportsFormat(DRM_FORMAT_XRGB8888))); |
| 212 |
} |
| 213 |
return false; |
| 214 |
}); |
| 215 |
|
| 216 |
if (videoPlane != m_planes.end() && guiPlane != m_planes.end()) |
| 217 |
{ |
| 218 |
m_crtc = m_crtcs[i].get(); |
| 219 |
m_video_plane = videoPlane->get(); |
| 220 |
m_gui_plane = guiPlane->get(); |
| 221 |
break; |
| 222 |
} |
| 223 |
|
| 224 |
if (guiPlane != m_planes.end()) |
| 225 |
{ |
| 226 |
if (!m_crtc && m_encoder->GetCrtcId() == m_crtcs[i]->GetCrtcId()) |
| 227 |
{ |
| 228 |
m_crtc = m_crtcs[i].get(); |
| 229 |
m_gui_plane = guiPlane->get(); |
| 230 |
m_video_plane = nullptr; |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
CLog::Log(LOGINFO, "CDRMUtils::{} - using crtc: {}", __FUNCTION__, m_crtc->GetCrtcId()); |
| 236 |
|
| 237 |
// video plane may not be available |
| 238 |
if (m_video_plane) |
| 239 |
CLog::Log(LOGDEBUG, "CDRMUtils::{} - using video plane {}", __FUNCTION__, |
| 240 |
m_video_plane->GetPlaneId()); |
| 241 |
|
| 242 |
if (m_gui_plane->SupportsFormat(DRM_FORMAT_XRGB2101010)) |
| 243 |
{ |
| 244 |
m_gui_plane->SetFormat(DRM_FORMAT_XRGB2101010); |
| 245 |
CLog::Log(LOGDEBUG, "CDRMUtils::{} - using 10bit gui plane {}", __FUNCTION__, |
| 246 |
m_gui_plane->GetPlaneId()); |
| 247 |
} |
| 248 |
else |
| 249 |
{ |
| 250 |
m_gui_plane->SetFormat(DRM_FORMAT_XRGB8888); |
| 251 |
CLog::Log(LOGDEBUG, "CDRMUtils::{} - using gui plane {}", __FUNCTION__, |
| 252 |
m_gui_plane->GetPlaneId()); |
| 253 |
} |
| 254 |
|
| 255 |
return true; |
| 256 |
} |
| 257 |
|
| 258 |
void CDRMUtils::PrintDrmDeviceInfo(drmDevicePtr device) |
| 259 |
{ |
| 260 |
std::string message; |
| 261 |
|
| 262 |
// clang-format off |
| 263 |
message.append(fmt::format("CDRMUtils::{} - DRM Device Info:", __FUNCTION__)); |
| 264 |
message.append(fmt::format("\n available_nodes: {:#04x}", device->available_nodes)); |
| 265 |
message.append("\n nodes:"); |
| 266 |
|
| 267 |
for (int i = 0; i < DRM_NODE_MAX; i++) |
| 268 |
{ |
| 269 |
if (device->available_nodes & 1 << i) |
| 270 |
message.append(fmt::format("\n nodes[{}]: {}", i, device->nodes[i])); |
| 271 |
} |
| 272 |
|
| 273 |
message.append(fmt::format("\n bustype: {:#04x}", device->bustype)); |
| 274 |
|
| 275 |
if (device->bustype == DRM_BUS_PCI) |
| 276 |
{ |
| 277 |
message.append("\n pci:"); |
| 278 |
message.append(fmt::format("\n domain: {:#04x}", device->businfo.pci->domain)); |
| 279 |
message.append(fmt::format("\n bus: {:#02x}", device->businfo.pci->bus)); |
| 280 |
message.append(fmt::format("\n dev: {:#02x}", device->businfo.pci->dev)); |
| 281 |
message.append(fmt::format("\n func: {:#1}", device->businfo.pci->func)); |
| 282 |
|
| 283 |
message.append("\n deviceinfo:"); |
| 284 |
message.append("\n pci:"); |
| 285 |
message.append(fmt::format("\n vendor_id: {:#04x}", device->deviceinfo.pci->vendor_id)); |
| 286 |
message.append(fmt::format("\n device_id: {:#04x}", device->deviceinfo.pci->device_id)); |
| 287 |
message.append(fmt::format("\n subvendor_id: {:#04x}", device->deviceinfo.pci->subvendor_id)); |
| 288 |
message.append(fmt::format("\n subdevice_id: {:#04x}", device->deviceinfo.pci->subdevice_id)); |
| 289 |
} |
| 290 |
else if (device->bustype == DRM_BUS_USB) |
| 291 |
{ |
| 292 |
message.append("\n usb:"); |
| 293 |
message.append(fmt::format("\n bus: {:#03x}", device->businfo.usb->bus)); |
| 294 |
message.append(fmt::format("\n dev: {:#03x}", device->businfo.usb->dev)); |
| 295 |
|
| 296 |
message.append("\n deviceinfo:"); |
| 297 |
message.append("\n usb:"); |
| 298 |
message.append(fmt::format("\n vendor: {:#04x}", device->deviceinfo.usb->vendor)); |
| 299 |
message.append(fmt::format("\n product: {:#04x}", device->deviceinfo.usb->product)); |
| 300 |
} |
| 301 |
else if (device->bustype == DRM_BUS_PLATFORM) |
| 302 |
{ |
| 303 |
message.append("\n platform:"); |
| 304 |
message.append(fmt::format("\n fullname: {}", device->businfo.platform->fullname)); |
| 305 |
} |
| 306 |
else if (device->bustype == DRM_BUS_HOST1X) |
| 307 |
{ |
| 308 |
message.append("\n host1x:"); |
| 309 |
message.append(fmt::format("\n fullname: {}", device->businfo.host1x->fullname)); |
| 310 |
} |
| 311 |
else |
| 312 |
message.append("\n unhandled bus type"); |
| 313 |
// clang-format on |
| 314 |
|
| 315 |
CLog::Log(LOGDEBUG, "{}", message); |
| 316 |
} |
| 317 |
|
| 318 |
bool CDRMUtils::OpenDrm(bool needConnector) |
| 319 |
{ |
| 320 |
int numDevices = drmGetDevices2(0, nullptr, 0); |
| 321 |
if (numDevices <= 0) |
| 322 |
{ |
| 323 |
CLog::Log(LOGERROR, "CDRMUtils::{} - no drm devices found: ({})", __FUNCTION__, |
| 324 |
strerror(errno)); |
| 325 |
return false; |
| 326 |
} |
| 327 |
|
| 328 |
CLog::Log(LOGDEBUG, "CDRMUtils::{} - drm devices found: {}", __FUNCTION__, numDevices); |
| 329 |
|
| 330 |
std::vector<drmDevicePtr> devices(numDevices); |
| 331 |
|
| 332 |
int ret = drmGetDevices2(0, devices.data(), devices.size()); |
| 333 |
if (ret < 0) |
| 334 |
{ |
| 335 |
CLog::Log(LOGERROR, "CDRMUtils::{} - drmGetDevices2 return an error: ({})", __FUNCTION__, |
| 336 |
strerror(errno)); |
| 337 |
return false; |
| 338 |
} |
| 339 |
|
| 340 |
for (const auto device : devices) |
| 341 |
{ |
| 342 |
if (!(device->available_nodes & 1 << DRM_NODE_PRIMARY)) |
| 343 |
continue; |
| 344 |
|
| 345 |
close(m_fd); |
| 346 |
m_fd = open(device->nodes[DRM_NODE_PRIMARY], O_RDWR | O_CLOEXEC); |
| 347 |
if (m_fd < 0) |
| 348 |
continue; |
| 349 |
|
| 350 |
if (needConnector) |
| 351 |
{ |
| 352 |
auto resources = drmModeGetResources(m_fd); |
| 353 |
if (!resources) |
| 354 |
continue; |
| 355 |
|
| 356 |
m_connectors.clear(); |
| 357 |
for (int i = 0; i < resources->count_connectors; i++) |
| 358 |
m_connectors.emplace_back(std::make_unique<CDRMConnector>(m_fd, resources->connectors[i])); |
| 359 |
|
| 360 |
drmModeFreeResources(resources); |
| 361 |
|
| 362 |
if (!FindConnector()) |
| 363 |
continue; |
| 364 |
} |
| 365 |
|
| 366 |
CLog::Log(LOGDEBUG, "CDRMUtils::{} - opened device: {}", __FUNCTION__, |
| 367 |
device->nodes[DRM_NODE_PRIMARY]); |
| 368 |
|
| 369 |
PrintDrmDeviceInfo(device); |
| 370 |
|
| 371 |
const char* renderPath = drmGetRenderDeviceNameFromFd(m_fd); |
| 372 |
|
| 373 |
if (!renderPath) |
| 374 |
renderPath = drmGetDeviceNameFromFd2(m_fd); |
| 375 |
|
| 376 |
if (!renderPath) |
| 377 |
renderPath = drmGetDeviceNameFromFd(m_fd); |
| 378 |
|
| 379 |
if (renderPath) |
| 380 |
{ |
| 381 |
m_renderDevicePath = renderPath; |
| 382 |
m_renderFd = open(renderPath, O_RDWR | O_CLOEXEC); |
| 383 |
if (m_renderFd != 0) |
| 384 |
CLog::Log(LOGDEBUG, "CDRMUtils::{} - opened render node: {}", __FUNCTION__, renderPath); |
| 385 |
} |
| 386 |
|
| 387 |
drmFreeDevices(devices.data(), devices.size()); |
| 388 |
return true; |
| 389 |
} |
| 390 |
|
| 391 |
drmFreeDevices(devices.data(), devices.size()); |
| 392 |
return false; |
| 393 |
} |
| 394 |
|
| 395 |
bool CDRMUtils::InitDrm() |
| 396 |
{ |
| 397 |
if (m_fd < 0) |
| 398 |
return false; |
| 399 |
|
| 400 |
/* caps need to be set before allocating connectors, encoders, crtcs, and planes */ |
| 401 |
int ret = drmSetClientCap(m_fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1); |
| 402 |
if (ret) |
| 403 |
{ |
| 404 |
CLog::Log(LOGERROR, "CDRMUtils::{} - failed to set universal planes capability: {}", |
| 405 |
__FUNCTION__, strerror(errno)); |
| 406 |
return false; |
| 407 |
} |
| 408 |
|
| 409 |
ret = drmSetClientCap(m_fd, DRM_CLIENT_CAP_STEREO_3D, 1); |
| 410 |
if (ret) |
| 411 |
{ |
| 412 |
CLog::Log(LOGERROR, "CDRMUtils::{} - failed to set stereo 3d capability: {}", __FUNCTION__, |
| 413 |
strerror(errno)); |
| 414 |
return false; |
| 415 |
} |
| 416 |
|
| 417 |
#if defined(DRM_CLIENT_CAP_ASPECT_RATIO) |
| 418 |
ret = drmSetClientCap(m_fd, DRM_CLIENT_CAP_ASPECT_RATIO, 0); |
| 419 |
if (ret != 0) |
| 420 |
CLog::Log(LOGERROR, "CDRMUtils::{} - aspect ratio capability is not supported: {}", |
| 421 |
__FUNCTION__, strerror(errno)); |
| 422 |
#endif |
| 423 |
|
| 424 |
auto resources = drmModeGetResources(m_fd); |
| 425 |
if (!resources) |
| 426 |
{ |
| 427 |
CLog::Log(LOGERROR, "CDRMUtils::{} - failed to get drm resources: {}", __FUNCTION__, strerror(errno)); |
| 428 |
return false; |
| 429 |
} |
| 430 |
|
| 431 |
m_connectors.clear(); |
| 432 |
for (int i = 0; i < resources->count_connectors; i++) |
| 433 |
m_connectors.emplace_back(std::make_unique<CDRMConnector>(m_fd, resources->connectors[i])); |
| 434 |
|
| 435 |
m_encoders.clear(); |
| 436 |
for (int i = 0; i < resources->count_encoders; i++) |
| 437 |
m_encoders.emplace_back(std::make_unique<CDRMEncoder>(m_fd, resources->encoders[i])); |
| 438 |
|
| 439 |
m_crtcs.clear(); |
| 440 |
for (int i = 0; i < resources->count_crtcs; i++) |
| 441 |
m_crtcs.emplace_back(std::make_unique<CDRMCrtc>(m_fd, resources->crtcs[i])); |
| 442 |
|
| 443 |
drmModeFreeResources(resources); |
| 444 |
|
| 445 |
auto planeResources = drmModeGetPlaneResources(m_fd); |
| 446 |
if (!planeResources) |
| 447 |
{ |
| 448 |
CLog::Log(LOGERROR, "CDRMUtils::{} - failed to get drm plane resources: {}", __FUNCTION__, strerror(errno)); |
| 449 |
return false; |
| 450 |
} |
| 451 |
|
| 452 |
m_planes.clear(); |
| 453 |
for (uint32_t i = 0; i < planeResources->count_planes; i++) |
| 454 |
{ |
| 455 |
m_planes.emplace_back(std::make_unique<CDRMPlane>(m_fd, planeResources->planes[i])); |
| 456 |
m_planes[i]->FindModifiers(); |
| 457 |
} |
| 458 |
|
| 459 |
drmModeFreePlaneResources(planeResources); |
| 460 |
|
| 461 |
if (!FindConnector()) |
| 462 |
return false; |
| 463 |
|
| 464 |
if (!FindEncoder()) |
| 465 |
return false; |
| 466 |
|
| 467 |
if (!FindCrtc()) |
| 468 |
return false; |
| 469 |
|
| 470 |
if (!FindPlanes()) |
| 471 |
return false; |
| 472 |
|
| 473 |
if (!FindPreferredMode()) |
| 474 |
return false; |
| 475 |
|
| 476 |
ret = drmSetMaster(m_fd); |
| 477 |
if (ret < 0) |
| 478 |
{ |
| 479 |
CLog::Log(LOGDEBUG, |
| 480 |
"CDRMUtils::{} - failed to set drm master, will try to authorize instead: {}", |
| 481 |
__FUNCTION__, strerror(errno)); |
| 482 |
|
| 483 |
drm_magic_t magic; |
| 484 |
|
| 485 |
ret = drmGetMagic(m_fd, &magic); |
| 486 |
if (ret < 0) |
| 487 |
{ |
| 488 |
CLog::Log(LOGERROR, "CDRMUtils::{} - failed to get drm magic: {}", __FUNCTION__, |
| 489 |
strerror(errno)); |
| 490 |
return false; |
| 491 |
} |
| 492 |
|
| 493 |
ret = drmAuthMagic(m_fd, magic); |
| 494 |
if (ret < 0) |
| 495 |
{ |
| 496 |
CLog::Log(LOGERROR, "CDRMUtils::{} - failed to authorize drm magic: {}", __FUNCTION__, |
| 497 |
strerror(errno)); |
| 498 |
return false; |
| 499 |
} |
| 500 |
|
| 501 |
CLog::Log(LOGINFO, "CDRMUtils::{} - successfully authorized drm magic", __FUNCTION__); |
| 502 |
} |
| 503 |
|
| 504 |
return true; |
| 505 |
} |
| 506 |
|
| 507 |
bool CDRMUtils::FindConnector() |
| 508 |
{ |
| 509 |
auto settingsComponent = CServiceBroker::GetSettingsComponent(); |
| 510 |
if (!settingsComponent) |
| 511 |
return false; |
| 512 |
|
| 513 |
auto settings = settingsComponent->GetSettings(); |
| 514 |
if (!settings) |
| 515 |
return false; |
| 516 |
|
| 517 |
std::vector<std::unique_ptr<CDRMConnector>>::iterator connector; |
| 518 |
|
| 519 |
std::string connectorName = settings->GetString(CSettings::SETTING_VIDEOSCREEN_MONITOR); |
| 520 |
if (connectorName != "Default") |
| 521 |
{ |
| 522 |
connector = std::find_if(m_connectors.begin(), m_connectors.end(), |
| 523 |
[&connectorName](auto& connector) |
| 524 |
{ |
| 525 |
return connector->GetEncoderId() > 0 && connector->IsConnected() && |
| 526 |
connector->GetName() == connectorName; |
| 527 |
}); |
| 528 |
|
| 529 |
if (connector == m_connectors.end()) |
| 530 |
{ |
| 531 |
CLog::Log(LOGDEBUG, "CDRMUtils::{} - failed to find specified connector: {}, trying default", |
| 532 |
__FUNCTION__, connectorName); |
| 533 |
connectorName = "Default"; |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
if (connectorName == "Default") |
| 538 |
{ |
| 539 |
connector = std::find_if(m_connectors.begin(), m_connectors.end(), |
| 540 |
[](auto& connector) |
| 541 |
{ return connector->GetEncoderId() > 0 && connector->IsConnected(); }); |
| 542 |
} |
| 543 |
|
| 544 |
if (connector == m_connectors.end()) |
| 545 |
{ |
| 546 |
CLog::Log(LOGDEBUG, "CDRMUtils::{} - failed to find connected connector", __FUNCTION__); |
| 547 |
return false; |
| 548 |
} |
| 549 |
|
| 550 |
CLog::Log(LOGINFO, "CDRMUtils::{} - using connector: {}", __FUNCTION__, |
| 551 |
connector->get()->GetName()); |
| 552 |
|
| 553 |
m_connector = connector->get(); |
| 554 |
return true; |
| 555 |
} |
| 556 |
|
| 557 |
bool CDRMUtils::FindEncoder() |
| 558 |
{ |
| 559 |
auto encoder = std::find_if(m_encoders.begin(), m_encoders.end(), [this](auto& encoder) { |
| 560 |
return encoder->GetEncoderId() == m_connector->GetEncoderId(); |
| 561 |
}); |
| 562 |
|
| 563 |
if (encoder == m_encoders.end()) |
| 564 |
{ |
| 565 |
CLog::Log(LOGDEBUG, "CDRMUtils::{} - failed to find encoder for connector id: {}", __FUNCTION__, |
| 566 |
*m_connector->GetConnectorId()); |
| 567 |
return false; |
| 568 |
} |
| 569 |
|
| 570 |
CLog::Log(LOGINFO, "CDRMUtils::{} - using encoder: {}", __FUNCTION__, |
| 571 |
encoder->get()->GetEncoderId()); |
| 572 |
|
| 573 |
m_encoder = encoder->get(); |
| 574 |
return true; |
| 575 |
} |
| 576 |
|
| 577 |
bool CDRMUtils::FindCrtc() |
| 578 |
{ |
| 579 |
for (size_t i = 0; i < m_crtcs.size(); i++) |
| 580 |
{ |
| 581 |
if (m_encoder->GetPossibleCrtcs() & (1 << i)) |
| 582 |
{ |
| 583 |
if (m_crtcs[i]->GetCrtcId() == m_encoder->GetCrtcId()) |
| 584 |
{ |
| 585 |
m_orig_crtc = m_crtcs[i].get(); |
| 586 |
if (m_orig_crtc->GetModeValid()) |
| 587 |
{ |
| 588 |
m_mode = m_orig_crtc->GetMode(); |
| 589 |
CLog::Log(LOGDEBUG, "CDRMUtils::{} - original crtc mode: {}x{}{} @ {} Hz", __FUNCTION__, |
| 590 |
m_mode->hdisplay, m_mode->vdisplay, |
| 591 |
m_mode->flags & DRM_MODE_FLAG_INTERLACE ? "i" : "", m_mode->vrefresh); |
| 592 |
} |
| 593 |
return true; |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
|
| 598 |
return false; |
| 599 |
} |
| 600 |
|
| 601 |
bool CDRMUtils::RestoreOriginalMode() |
| 602 |
{ |
| 603 |
if(!m_orig_crtc) |
| 604 |
{ |
| 605 |
return false; |
| 606 |
} |
| 607 |
|
| 608 |
auto ret = drmModeSetCrtc(m_fd, m_orig_crtc->GetCrtcId(), m_orig_crtc->GetBufferId(), |
| 609 |
m_orig_crtc->GetX(), m_orig_crtc->GetY(), m_connector->GetConnectorId(), |
| 610 |
1, m_orig_crtc->GetMode()); |
| 611 |
|
| 612 |
if(ret) |
| 613 |
{ |
| 614 |
CLog::Log(LOGERROR, "CDRMUtils::{} - failed to set original crtc mode", __FUNCTION__); |
| 615 |
return false; |
| 616 |
} |
| 617 |
|
| 618 |
CLog::Log(LOGDEBUG, "CDRMUtils::{} - set original crtc mode", __FUNCTION__); |
| 619 |
|
| 620 |
return true; |
| 621 |
} |
| 622 |
|
| 623 |
void CDRMUtils::DestroyDrm() |
| 624 |
{ |
| 625 |
RestoreOriginalMode(); |
| 626 |
|
| 627 |
if (drmAuthMagic(m_fd, 0) == EINVAL) |
| 628 |
drmDropMaster(m_fd); |
| 629 |
|
| 630 |
close(m_renderFd); |
| 631 |
close(m_fd); |
| 632 |
|
| 633 |
m_connector = nullptr; |
| 634 |
m_encoder = nullptr; |
| 635 |
m_crtc = nullptr; |
| 636 |
m_orig_crtc = nullptr; |
| 637 |
m_video_plane = nullptr; |
| 638 |
m_gui_plane = nullptr; |
| 639 |
} |
| 640 |
|
| 641 |
RESOLUTION_INFO CDRMUtils::GetResolutionInfo(drmModeModeInfoPtr mode) |
| 642 |
{ |
| 643 |
RESOLUTION_INFO res; |
| 644 |
res.iScreenWidth = mode->hdisplay; |
| 645 |
res.iScreenHeight = mode->vdisplay; |
| 646 |
res.iWidth = res.iScreenWidth; |
| 647 |
res.iHeight = res.iScreenHeight; |
| 648 |
|
| 649 |
int limit = CServiceBroker::GetSettingsComponent()->GetSettings()->GetInt( |
| 650 |
SETTING_VIDEOSCREEN_LIMITGUISIZE); |
| 651 |
if (limit > 0 && res.iScreenWidth > 1920 && res.iScreenHeight > 1080) |
| 652 |
{ |
| 653 |
switch (limit) |
| 654 |
{ |
| 655 |
case 1: // 720p |
| 656 |
res.iWidth = 1280; |
| 657 |
res.iHeight = 720; |
| 658 |
break; |
| 659 |
case 2: // 1080p / 720p (>30hz) |
| 660 |
res.iWidth = mode->vrefresh > 30 ? 1280 : 1920; |
| 661 |
res.iHeight = mode->vrefresh > 30 ? 720 : 1080; |
| 662 |
break; |
| 663 |
case 3: // 1080p |
| 664 |
res.iWidth = 1920; |
| 665 |
res.iHeight = 1080; |
| 666 |
break; |
| 667 |
case 4: // Unlimited / 1080p (>30hz) |
| 668 |
res.iWidth = mode->vrefresh > 30 ? 1920 : res.iScreenWidth; |
| 669 |
res.iHeight = mode->vrefresh > 30 ? 1080 : res.iScreenHeight; |
| 670 |
break; |
| 671 |
} |
| 672 |
} |
| 673 |
|
| 674 |
if (mode->clock % 5 != 0) |
| 675 |
res.fRefreshRate = static_cast<float>(mode->vrefresh) * (1000.0f/1001.0f); |
| 676 |
else |
| 677 |
res.fRefreshRate = mode->vrefresh; |
| 678 |
res.iSubtitles = res.iHeight; |
| 679 |
res.fPixelRatio = 1.0f; |
| 680 |
res.bFullScreen = true; |
| 681 |
|
| 682 |
if (mode->flags & DRM_MODE_FLAG_3D_MASK) |
| 683 |
{ |
| 684 |
if (mode->flags & DRM_MODE_FLAG_3D_TOP_AND_BOTTOM) |
| 685 |
res.dwFlags = D3DPRESENTFLAG_MODE3DTB; |
| 686 |
else if (mode->flags & DRM_MODE_FLAG_3D_SIDE_BY_SIDE_HALF) |
| 687 |
res.dwFlags = D3DPRESENTFLAG_MODE3DSBS; |
| 688 |
} |
| 689 |
else if (mode->flags & DRM_MODE_FLAG_INTERLACE) |
| 690 |
res.dwFlags = D3DPRESENTFLAG_INTERLACED; |
| 691 |
else |
| 692 |
res.dwFlags = D3DPRESENTFLAG_PROGRESSIVE; |
| 693 |
|
| 694 |
res.strMode = |
| 695 |
StringUtils::Format("{}x{}{} @ {:.6f} Hz", res.iScreenWidth, res.iScreenHeight, |
| 696 |
res.dwFlags & D3DPRESENTFLAG_INTERLACED ? "i" : "", res.fRefreshRate); |
| 697 |
return res; |
| 698 |
} |
| 699 |
|
| 700 |
RESOLUTION_INFO CDRMUtils::GetCurrentMode() |
| 701 |
{ |
| 702 |
return GetResolutionInfo(m_mode); |
| 703 |
} |
| 704 |
|
| 705 |
std::vector<RESOLUTION_INFO> CDRMUtils::GetModes() |
| 706 |
{ |
| 707 |
std::vector<RESOLUTION_INFO> resolutions; |
| 708 |
resolutions.reserve(m_connector->GetModesCount()); |
| 709 |
|
| 710 |
for (auto i = 0; i < m_connector->GetModesCount(); i++) |
| 711 |
{ |
| 712 |
RESOLUTION_INFO res = GetResolutionInfo(m_connector->GetModeForIndex(i)); |
| 713 |
res.strId = std::to_string(i); |
| 714 |
resolutions.push_back(res); |
| 715 |
} |
| 716 |
|
| 717 |
return resolutions; |
| 718 |
} |
| 719 |
|
| 720 |
std::vector<std::string> CDRMUtils::GetConnectedConnectorNames() |
| 721 |
{ |
| 722 |
std::vector<std::string> connectorNames; |
| 723 |
for (const auto& connector : m_connectors) |
| 724 |
{ |
| 725 |
if (connector->IsConnected()) |
| 726 |
connectorNames.emplace_back(connector->GetName()); |
| 727 |
} |
| 728 |
|
| 729 |
return connectorNames; |
| 730 |
} |
| 731 |
|
| 732 |
uint32_t CDRMUtils::FourCCWithAlpha(uint32_t fourcc) |
| 733 |
{ |
| 734 |
return (fourcc & 0xFFFFFF00) | static_cast<uint32_t>('A'); |
| 735 |
} |
| 736 |
|
| 737 |
uint32_t CDRMUtils::FourCCWithoutAlpha(uint32_t fourcc) |
| 738 |
{ |
| 739 |
return (fourcc & 0xFFFFFF00) | static_cast<uint32_t>('X'); |
| 740 |
} |