| 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 "GBMUtils.h" |
| 10 |
|
| 11 |
#include "utils/log.h" |
| 12 |
|
| 13 |
#include <mutex> |
| 14 |
|
| 15 |
using namespace KODI::WINDOWING::GBM; |
| 16 |
|
| 17 |
namespace |
| 18 |
{ |
| 19 |
std::once_flag flag; |
| 20 |
} |
| 21 |
|
| 22 |
bool CGBMUtils::CreateDevice(int fd) |
| 23 |
{ |
| 24 |
auto device = gbm_create_device(fd); |
| 25 |
if (!device) |
| 26 |
{ |
| 27 |
CLog::Log(LOGERROR, "CGBMUtils::{} - failed to create device: {}", __FUNCTION__, |
| 28 |
strerror(errno)); |
| 29 |
return false; |
| 30 |
} |
| 31 |
|
| 32 |
m_device.reset(new CGBMDevice(device)); |
| 33 |
|
| 34 |
return true; |
| 35 |
} |
| 36 |
|
| 37 |
CGBMUtils::CGBMDevice::CGBMDevice(gbm_device* device) : m_device(device) |
| 38 |
{ |
| 39 |
} |
| 40 |
|
| 41 |
bool CGBMUtils::CGBMDevice::CreateSurface( |
| 42 |
int width, int height, uint32_t format, const uint64_t* modifiers, const int modifiers_count) |
| 43 |
{ |
| 44 |
gbm_surface* surface{nullptr}; |
| 45 |
#if defined(HAS_GBM_MODIFIERS) |
| 46 |
if (modifiers) |
| 47 |
{ |
| 48 |
surface = gbm_surface_create_with_modifiers(m_device, width, height, format, modifiers, |
| 49 |
modifiers_count); |
| 50 |
} |
| 51 |
#endif |
| 52 |
if (!surface) |
| 53 |
{ |
| 54 |
surface = gbm_surface_create(m_device, width, height, format, |
| 55 |
GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); |
| 56 |
} |
| 57 |
|
| 58 |
if (!surface) |
| 59 |
{ |
| 60 |
CLog::Log(LOGERROR, "CGBMUtils::{} - failed to create surface: {}", __FUNCTION__, |
| 61 |
strerror(errno)); |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
CLog::Log(LOGDEBUG, "CGBMUtils::{} - created surface with size {}x{}", __FUNCTION__, width, |
| 66 |
height); |
| 67 |
|
| 68 |
m_surface.reset(new CGBMSurface(surface)); |
| 69 |
|
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
CGBMUtils::CGBMDevice::CGBMSurface::CGBMSurface(gbm_surface* surface) : m_surface(surface) |
| 74 |
{ |
| 75 |
} |
| 76 |
|
| 77 |
CGBMUtils::CGBMDevice::CGBMSurface::CGBMSurfaceBuffer& CGBMUtils::CGBMDevice::CGBMSurface:: |
| 78 |
LockFrontBuffer() |
| 79 |
{ |
| 80 |
m_buffers.emplace(std::make_unique<CGBMSurfaceBuffer>(m_surface)); |
| 81 |
|
| 82 |
if (!static_cast<bool>(gbm_surface_has_free_buffers(m_surface))) |
| 83 |
{ |
| 84 |
/* |
| 85 |
* We want to use call_once here because we want it to be logged the first time that |
| 86 |
* we have to release buffers. This means that the maximum amount of buffers had been reached. |
| 87 |
* For mesa this should be 4 buffers but it may vary across other implementations. |
| 88 |
*/ |
| 89 |
std::call_once( |
| 90 |
flag, [this]() { CLog::Log(LOGDEBUG, "CGBMUtils - using {} buffers", m_buffers.size()); }); |
| 91 |
|
| 92 |
m_buffers.pop(); |
| 93 |
} |
| 94 |
|
| 95 |
return *m_buffers.back(); |
| 96 |
} |
| 97 |
|
| 98 |
CGBMUtils::CGBMDevice::CGBMSurface::CGBMSurfaceBuffer::CGBMSurfaceBuffer(gbm_surface* surface) |
| 99 |
: m_surface(surface), m_buffer(gbm_surface_lock_front_buffer(surface)) |
| 100 |
{ |
| 101 |
} |
| 102 |
|
| 103 |
CGBMUtils::CGBMDevice::CGBMSurface::CGBMSurfaceBuffer::~CGBMSurfaceBuffer() |
| 104 |
{ |
| 105 |
if (m_surface && m_buffer) |
| 106 |
gbm_surface_release_buffer(m_surface, m_buffer); |
| 107 |
} |