| 1 |
/* |
| 2 |
* Copyright (C) 2017-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 "VideoLayerBridgeDRMPRIME.h" |
| 10 |
|
| 11 |
#include "ServiceBroker.h" |
| 12 |
#include "cores/VideoPlayer/Buffers/VideoBufferDRMPRIME.h" |
| 13 |
#include "utils/log.h" |
| 14 |
#include "windowing/WinSystem.h" |
| 15 |
#include "windowing/gbm/drm/DRMAtomic.h" |
| 16 |
|
| 17 |
#include <utility> |
| 18 |
|
| 19 |
using namespace KODI::WINDOWING::GBM; |
| 20 |
using namespace DRMPRIME; |
| 21 |
|
| 22 |
CVideoLayerBridgeDRMPRIME::CVideoLayerBridgeDRMPRIME(std::shared_ptr<CDRMAtomic> drm) |
| 23 |
: m_DRM(std::move(drm)) |
| 24 |
{ |
| 25 |
} |
| 26 |
|
| 27 |
CVideoLayerBridgeDRMPRIME::~CVideoLayerBridgeDRMPRIME() |
| 28 |
{ |
| 29 |
Release(m_prev_buffer); |
| 30 |
Release(m_buffer); |
| 31 |
} |
| 32 |
|
| 33 |
void CVideoLayerBridgeDRMPRIME::Disable() |
| 34 |
{ |
| 35 |
// disable video plane |
| 36 |
auto plane = m_DRM->GetVideoPlane(); |
| 37 |
m_DRM->AddProperty(plane, "FB_ID", 0); |
| 38 |
m_DRM->AddProperty(plane, "CRTC_ID", 0); |
| 39 |
|
| 40 |
auto winSystem = CServiceBroker::GetWinSystem(); |
| 41 |
if (!winSystem) |
| 42 |
return; |
| 43 |
|
| 44 |
// disable HDR metadata |
| 45 |
winSystem->SetHDR(nullptr); |
| 46 |
} |
| 47 |
|
| 48 |
void CVideoLayerBridgeDRMPRIME::Acquire(CVideoBufferDRMPRIME* buffer) |
| 49 |
{ |
| 50 |
// release the buffer that is no longer presented on screen |
| 51 |
Release(m_prev_buffer); |
| 52 |
|
| 53 |
// release the buffer currently being presented next call |
| 54 |
m_prev_buffer = m_buffer; |
| 55 |
|
| 56 |
// reference count the buffer that is going to be presented on screen |
| 57 |
m_buffer = buffer; |
| 58 |
m_buffer->Acquire(); |
| 59 |
} |
| 60 |
|
| 61 |
void CVideoLayerBridgeDRMPRIME::Release(CVideoBufferDRMPRIME* buffer) |
| 62 |
{ |
| 63 |
if (!buffer) |
| 64 |
return; |
| 65 |
|
| 66 |
Unmap(buffer); |
| 67 |
buffer->Release(); |
| 68 |
} |
| 69 |
|
| 70 |
bool CVideoLayerBridgeDRMPRIME::Map(CVideoBufferDRMPRIME* buffer) |
| 71 |
{ |
| 72 |
if (buffer->m_fb_id) |
| 73 |
return true; |
| 74 |
|
| 75 |
if (!buffer->AcquireDescriptor()) |
| 76 |
{ |
| 77 |
CLog::Log(LOGERROR, "CVideoLayerBridgeDRMPRIME::{} - failed to acquire descriptor", |
| 78 |
__FUNCTION__); |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
AVDRMFrameDescriptor* descriptor = buffer->GetDescriptor(); |
| 83 |
uint32_t handles[4] = {}, pitches[4] = {}, offsets[4] = {}, flags = 0; |
| 84 |
uint64_t modifier[4] = {}; |
| 85 |
int ret; |
| 86 |
|
| 87 |
// convert Prime FD to GEM handle |
| 88 |
for (int object = 0; object < descriptor->nb_objects; object++) |
| 89 |
{ |
| 90 |
ret = drmPrimeFDToHandle(m_DRM->GetFileDescriptor(), descriptor->objects[object].fd, |
| 91 |
&buffer->m_handles[object]); |
| 92 |
if (ret < 0) |
| 93 |
{ |
| 94 |
CLog::Log(LOGERROR, |
| 95 |
"CVideoLayerBridgeDRMPRIME::{} - failed to convert prime fd {} to gem handle {}, " |
| 96 |
"ret = {}", |
| 97 |
__FUNCTION__, descriptor->objects[object].fd, buffer->m_handles[object], ret); |
| 98 |
return false; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
AVDRMLayerDescriptor* layer = &descriptor->layers[0]; |
| 103 |
|
| 104 |
for (int plane = 0; plane < layer->nb_planes; plane++) |
| 105 |
{ |
| 106 |
int object = layer->planes[plane].object_index; |
| 107 |
uint32_t handle = buffer->m_handles[object]; |
| 108 |
if (handle) |
| 109 |
{ |
| 110 |
handles[plane] = handle; |
| 111 |
pitches[plane] = layer->planes[plane].pitch; |
| 112 |
offsets[plane] = layer->planes[plane].offset; |
| 113 |
modifier[plane] = descriptor->objects[object].format_modifier; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
if (modifier[0] && modifier[0] != DRM_FORMAT_MOD_INVALID) |
| 118 |
flags = DRM_MODE_FB_MODIFIERS; |
| 119 |
|
| 120 |
// add the video frame FB |
| 121 |
ret = drmModeAddFB2WithModifiers(m_DRM->GetFileDescriptor(), buffer->GetWidth(), |
| 122 |
buffer->GetHeight(), layer->format, handles, pitches, offsets, |
| 123 |
modifier, &buffer->m_fb_id, flags); |
| 124 |
if (ret < 0) |
| 125 |
{ |
| 126 |
CLog::Log(LOGERROR, "CVideoLayerBridgeDRMPRIME::{} - failed to add fb {}, ret = {}", |
| 127 |
__FUNCTION__, buffer->m_fb_id, ret); |
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
Acquire(buffer); |
| 132 |
return true; |
| 133 |
} |
| 134 |
|
| 135 |
void CVideoLayerBridgeDRMPRIME::Unmap(CVideoBufferDRMPRIME* buffer) |
| 136 |
{ |
| 137 |
if (buffer->m_fb_id) |
| 138 |
{ |
| 139 |
drmModeRmFB(m_DRM->GetFileDescriptor(), buffer->m_fb_id); |
| 140 |
buffer->m_fb_id = 0; |
| 141 |
} |
| 142 |
|
| 143 |
for (int i = 0; i < AV_DRM_MAX_PLANES; i++) |
| 144 |
{ |
| 145 |
if (buffer->m_handles[i]) |
| 146 |
{ |
| 147 |
struct drm_gem_close gem_close; |
| 148 |
gem_close.handle = buffer->m_handles[i]; |
| 149 |
drmIoctl(m_DRM->GetFileDescriptor(), DRM_IOCTL_GEM_CLOSE, &gem_close); |
| 150 |
buffer->m_handles[i] = 0; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
buffer->ReleaseDescriptor(); |
| 155 |
} |
| 156 |
|
| 157 |
void CVideoLayerBridgeDRMPRIME::Configure(CVideoBufferDRMPRIME* buffer) |
| 158 |
{ |
| 159 |
auto winSystem = CServiceBroker::GetWinSystem(); |
| 160 |
if (!winSystem) |
| 161 |
return; |
| 162 |
|
| 163 |
const VideoPicture& picture = buffer->GetPicture(); |
| 164 |
|
| 165 |
winSystem->SetHDR(&picture); |
| 166 |
|
| 167 |
auto plane = m_DRM->GetVideoPlane(); |
| 168 |
|
| 169 |
std::optional<uint64_t> colorEncoding = |
| 170 |
plane->GetPropertyValue("COLOR_ENCODING", GetColorEncoding(picture)); |
| 171 |
if (colorEncoding) |
| 172 |
m_DRM->AddProperty(plane, "COLOR_ENCODING", colorEncoding.value()); |
| 173 |
|
| 174 |
std::optional<uint64_t> colorRange = |
| 175 |
plane->GetPropertyValue("COLOR_RANGE", GetColorRange(picture)); |
| 176 |
if (colorRange) |
| 177 |
m_DRM->AddProperty(plane, "COLOR_RANGE", colorRange.value()); |
| 178 |
} |
| 179 |
|
| 180 |
void CVideoLayerBridgeDRMPRIME::SetVideoPlane(CVideoBufferDRMPRIME* buffer, const CRect& destRect) |
| 181 |
{ |
| 182 |
if (!Map(buffer)) |
| 183 |
{ |
| 184 |
Unmap(buffer); |
| 185 |
return; |
| 186 |
} |
| 187 |
|
| 188 |
auto plane = m_DRM->GetVideoPlane(); |
| 189 |
m_DRM->AddProperty(plane, "FB_ID", buffer->m_fb_id); |
| 190 |
m_DRM->AddProperty(plane, "CRTC_ID", m_DRM->GetCrtc()->GetCrtcId()); |
| 191 |
m_DRM->AddProperty(plane, "SRC_X", 0); |
| 192 |
m_DRM->AddProperty(plane, "SRC_Y", 0); |
| 193 |
m_DRM->AddProperty(plane, "SRC_W", buffer->GetWidth() << 16); |
| 194 |
m_DRM->AddProperty(plane, "SRC_H", buffer->GetHeight() << 16); |
| 195 |
m_DRM->AddProperty(plane, "CRTC_X", static_cast<int32_t>(destRect.x1) & ~1); |
| 196 |
m_DRM->AddProperty(plane, "CRTC_Y", static_cast<int32_t>(destRect.y1) & ~1); |
| 197 |
m_DRM->AddProperty(plane, "CRTC_W", (static_cast<uint32_t>(destRect.Width()) + 1) & ~1); |
| 198 |
m_DRM->AddProperty(plane, "CRTC_H", (static_cast<uint32_t>(destRect.Height()) + 1) & ~1); |
| 199 |
} |
| 200 |
|
| 201 |
void CVideoLayerBridgeDRMPRIME::UpdateVideoPlane() |
| 202 |
{ |
| 203 |
if (!m_buffer || !m_buffer->m_fb_id) |
| 204 |
return; |
| 205 |
|
| 206 |
auto plane = m_DRM->GetVideoPlane(); |
| 207 |
m_DRM->AddProperty(plane, "FB_ID", m_buffer->m_fb_id); |
| 208 |
m_DRM->AddProperty(plane, "CRTC_ID", m_DRM->GetCrtc()->GetCrtcId()); |
| 209 |
} |