| 1 |
/* |
| 2 |
* Copyright (C) 2005-2020 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 "DRMPlane.h" |
| 10 |
|
| 11 |
#include "DRMUtils.h" |
| 12 |
#include "utils/DRMHelpers.h" |
| 13 |
#include "utils/StringUtils.h" |
| 14 |
#include "utils/log.h" |
| 15 |
|
| 16 |
#include <algorithm> |
| 17 |
|
| 18 |
using namespace KODI::WINDOWING::GBM; |
| 19 |
|
| 20 |
CDRMPlane::CDRMPlane(int fd, uint32_t plane) : CDRMObject(fd), m_plane(drmModeGetPlane(m_fd, plane)) |
| 21 |
{ |
| 22 |
if (!m_plane) |
| 23 |
throw std::runtime_error("drmModeGetPlane failed: " + std::string{strerror(errno)}); |
| 24 |
|
| 25 |
if (!GetProperties(m_plane->plane_id, DRM_MODE_OBJECT_PLANE)) |
| 26 |
throw std::runtime_error("failed to get properties for plane: " + |
| 27 |
std::to_string(m_plane->plane_id)); |
| 28 |
} |
| 29 |
|
| 30 |
bool CDRMPlane::SupportsFormat(uint32_t format) |
| 31 |
{ |
| 32 |
for (uint32_t i = 0; i < m_plane->count_formats; i++) |
| 33 |
if (m_plane->formats[i] == format) |
| 34 |
return true; |
| 35 |
|
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
bool CDRMPlane::SupportsFormatAndModifier(uint32_t format, uint64_t modifier) |
| 40 |
{ |
| 41 |
/* |
| 42 |
* Some broadcom modifiers have parameters encoded which need to be |
| 43 |
* masked out before comparing with reported modifiers. |
| 44 |
*/ |
| 45 |
if (modifier >> 56 == DRM_FORMAT_MOD_VENDOR_BROADCOM) |
| 46 |
modifier = fourcc_mod_broadcom_mod(modifier); |
| 47 |
|
| 48 |
if (modifier == DRM_FORMAT_MOD_LINEAR) |
| 49 |
{ |
| 50 |
if (!SupportsFormat(format)) |
| 51 |
{ |
| 52 |
CLog::Log(LOGDEBUG, "CDRMPlane::{} - format not supported: {}", __FUNCTION__, |
| 53 |
DRMHELPERS::FourCCToString(format)); |
| 54 |
return false; |
| 55 |
} |
| 56 |
} |
| 57 |
else |
| 58 |
{ |
| 59 |
auto formatModifiers = &m_modifiers_map[format]; |
| 60 |
if (formatModifiers->empty()) |
| 61 |
{ |
| 62 |
CLog::Log(LOGDEBUG, "CDRMPlane::{} - format not supported: {}", __FUNCTION__, |
| 63 |
DRMHELPERS::FourCCToString(format)); |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
auto formatModifier = std::find(formatModifiers->begin(), formatModifiers->end(), modifier); |
| 68 |
if (formatModifier == formatModifiers->end()) |
| 69 |
{ |
| 70 |
CLog::Log(LOGDEBUG, "CDRMPlane::{} - modifier ({}) not supported for format ({})", |
| 71 |
__FUNCTION__, DRMHELPERS::ModifierToString(modifier), |
| 72 |
DRMHELPERS::FourCCToString(format)); |
| 73 |
return false; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
CLog::Log(LOGDEBUG, "CDRMPlane::{} - found plane format ({}) and modifier ({})", __FUNCTION__, |
| 78 |
DRMHELPERS::FourCCToString(format), DRMHELPERS::ModifierToString(modifier)); |
| 79 |
|
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
void CDRMPlane::FindModifiers() |
| 84 |
{ |
| 85 |
auto property = std::find_if(m_propsInfo.begin(), m_propsInfo.end(), [](auto& prop) { |
| 86 |
return StringUtils::EqualsNoCase(prop->name, "IN_FORMATS"); |
| 87 |
}); |
| 88 |
|
| 89 |
uint64_t blob_id = 0; |
| 90 |
if (property != m_propsInfo.end()) |
| 91 |
blob_id = m_props->prop_values[std::distance(m_propsInfo.begin(), property)]; |
| 92 |
|
| 93 |
if (blob_id == 0) |
| 94 |
return; |
| 95 |
|
| 96 |
drmModePropertyBlobPtr blob = drmModeGetPropertyBlob(m_fd, blob_id); |
| 97 |
if (!blob) |
| 98 |
return; |
| 99 |
|
| 100 |
drm_format_modifier_blob* header = static_cast<drm_format_modifier_blob*>(blob->data); |
| 101 |
uint32_t* formats = |
| 102 |
reinterpret_cast<uint32_t*>(reinterpret_cast<char*>(header) + header->formats_offset); |
| 103 |
drm_format_modifier* mod = reinterpret_cast<drm_format_modifier*>( |
| 104 |
reinterpret_cast<char*>(header) + header->modifiers_offset); |
| 105 |
|
| 106 |
for (uint32_t i = 0; i < header->count_formats; i++) |
| 107 |
{ |
| 108 |
std::vector<uint64_t> modifiers; |
| 109 |
for (uint32_t j = 0; j < header->count_modifiers; j++) |
| 110 |
{ |
| 111 |
if (mod[j].formats & 1ULL << i) |
| 112 |
modifiers.emplace_back(mod[j].modifier); |
| 113 |
} |
| 114 |
|
| 115 |
m_modifiers_map.emplace(formats[i], modifiers); |
| 116 |
} |
| 117 |
|
| 118 |
if (blob) |
| 119 |
drmModeFreePropertyBlob(blob); |
| 120 |
} |